/** * TypeScript interfaces (copied from lib/models for type safety) */ export interface Message { id: number; type: string; message: string; date: string; } export interface Conversation { id: string; title?: string; date?: string; type?: string; status: number; totalPrice: number; messages: Message[]; } export interface ConversationProps { id: string; mode: string; title: string; startDate: string; status: number; } /** * Database row types (snake_case from SQLite) */ export interface DBConversation { conversation_id: string; book_id: string; mode: string; title: string; start_date: number; // Unix timestamp status: number; user_id: string; summary?: string; convo_meta: string; synced?: number; } export interface DBMessage { message_id: string; conversation_id: string; role: string; // 'user' or 'model' message: string; message_date: number; // Unix timestamp meta_message: string; synced?: number; } /** * MAPPERS: DB → TypeScript Interfaces */ export function dbToConversation(dbConvo: DBConversation, messages: DBMessage[] = []): Conversation { return { id: dbConvo.conversation_id, title: dbConvo.title, date: new Date(dbConvo.start_date).toISOString(), type: dbConvo.mode as any, status: dbConvo.status, totalPrice: 0, // Computed from messages if needed messages: messages.map(dbToMessage) }; } export function dbToConversationProps(dbConvo: DBConversation): ConversationProps { return { id: dbConvo.conversation_id, mode: dbConvo.mode, title: dbConvo.title, startDate: new Date(dbConvo.start_date).toISOString(), status: dbConvo.status }; } export function dbToMessage(dbMessage: DBMessage): Message { return { id: parseInt(dbMessage.message_id, 10) || 0, type: dbMessage.role as any, message: dbMessage.message, date: new Date(dbMessage.message_date).toISOString() }; } /** * MAPPERS: TypeScript Interfaces → DB */ export function conversationToDb(conversation: Conversation, bookId: string, userId: string, synced: number = 0): DBConversation { return { conversation_id: conversation.id, book_id: bookId, mode: conversation.type || 'chatbot', title: conversation.title || 'Untitled Conversation', start_date: conversation.date ? new Date(conversation.date).getTime() : Date.now(), status: conversation.status, user_id: userId, summary: '', convo_meta: '', synced }; } export function messageToDb(message: Message, conversationId: string, synced: number = 0): DBMessage { return { message_id: message.id.toString(), conversation_id: conversationId, role: message.type, message: message.message, message_date: message.date ? new Date(message.date).getTime() : Date.now(), meta_message: '', synced }; }