File size: 1,064 Bytes
a94ab76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Chat } from '../types';

const STORAGE_KEY = 'turkish-ai-chats';

export const saveChats = (chats: Chat[]): void => {
  try {
    localStorage.setItem(STORAGE_KEY, JSON.stringify(chats));
  } catch (error) {
    console.error('Error saving chats:', error);
  }
};

export const loadChats = (): Chat[] => {
  try {
    const stored = localStorage.getItem(STORAGE_KEY);
    return stored ? JSON.parse(stored) : [];
  } catch (error) {
    console.error('Error loading chats:', error);
    return [];
  }
};

export const createNewChat = (modelName: string): Chat => {
  return {
    id: Date.now().toString(),
    title: 'Yeni Sohbet',
    messages: [],
    createdAt: Date.now(),
    updatedAt: Date.now(),
    modelName
  };
};

export const updateChatTitle = (chat: Chat): string => {
  const firstUserMessage = chat.messages.find(m => m.role === 'user');
  if (firstUserMessage) {
    const title = firstUserMessage.content.slice(0, 50);
    return title.length < firstUserMessage.content.length ? `${title}...` : title;
  }
  return 'Yeni Sohbet';
};