| |
| |
| |
| |
| |
| |
|
|
| import type { ChatSession, ChatMessageMetadata, SessionStatus } from '@/lib/types/chat'; |
| import type { UIMessage } from 'ai'; |
| import { db, type ChatSessionRecord } from './database'; |
|
|
| |
| const MAX_MESSAGES_PER_SESSION = 200; |
|
|
| |
| |
| |
| |
| |
| |
| export async function saveChatSessions(stageId: string, sessions: ChatSession[]): Promise<void> { |
| if (!sessions || sessions.length === 0) { |
| |
| await db.chatSessions.where('stageId').equals(stageId).delete(); |
| return; |
| } |
|
|
| const records: ChatSessionRecord[] = sessions.map((session) => ({ |
| id: session.id, |
| stageId, |
| type: session.type, |
| title: session.title, |
| |
| status: (session.status === 'active' ? 'interrupted' : session.status) as SessionStatus, |
| |
| messages: session.messages.slice(-MAX_MESSAGES_PER_SESSION), |
| config: session.config, |
| toolCalls: session.toolCalls, |
| pendingToolCalls: [], |
| createdAt: session.createdAt, |
| updatedAt: session.updatedAt, |
| sceneId: session.sceneId, |
| lastActionIndex: session.lastActionIndex, |
| })); |
|
|
| await db.transaction('rw', db.chatSessions, async () => { |
| |
| await db.chatSessions.where('stageId').equals(stageId).delete(); |
| await db.chatSessions.bulkPut(records); |
| }); |
| } |
|
|
| |
| |
| |
| |
| export async function loadChatSessions(stageId: string): Promise<ChatSession[]> { |
| const records = await db.chatSessions.where('stageId').equals(stageId).sortBy('createdAt'); |
|
|
| return records.map((record) => ({ |
| id: record.id, |
| type: record.type, |
| title: record.title, |
| status: record.status, |
| messages: record.messages as UIMessage<ChatMessageMetadata>[], |
| config: record.config, |
| toolCalls: record.toolCalls, |
| pendingToolCalls: record.pendingToolCalls, |
| createdAt: record.createdAt, |
| updatedAt: record.updatedAt, |
| sceneId: record.sceneId, |
| lastActionIndex: record.lastActionIndex, |
| })); |
| } |
|
|
| |
| |
| |
| export async function deleteChatSessions(stageId: string): Promise<void> { |
| await db.chatSessions.where('stageId').equals(stageId).delete(); |
| } |
|
|