From f2a71981181de1e42c8f09f5ed64964502807d98 Mon Sep 17 00:00:00 2001 From: thzxx Date: Thu, 18 Jun 2026 13:42:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20SQLite=20=E6=8C=81=E4=B9=85=E5=8C=96=20f?= =?UTF-8?q?iles=20=E5=92=8C=20=5Fvideos=EF=BC=8C=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E4=B8=8D=E5=86=8D=E4=B8=A2=E5=A4=B1=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E6=8C=87=E7=A4=BA=E7=89=8C=E5=92=8C=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ALTER TABLE 新增 attachments TEXT 列 - saveMessage 序列化 files/_videos 为 JSON - getMessages 反序列化还原 --- src/main/db/sqlite.ts | 13 ++++++++----- src/renderer/db/chat-db.ts | 29 ++++++++++++++++++----------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/main/db/sqlite.ts b/src/main/db/sqlite.ts index 987273e..06c69f9 100644 --- a/src/main/db/sqlite.ts +++ b/src/main/db/sqlite.ts @@ -223,8 +223,10 @@ export async function initDatabase(): Promise { CREATE INDEX IF NOT EXISTS idx_skills_updated ON skills(updated_at DESC); `); - // 兼容迁移:为已有 messages 表补充 prompt_eval_count 列(新库已有,旧库需要 ALTER) - try { db.run('ALTER TABLE messages ADD COLUMN prompt_eval_count INTEGER'); } catch { /* 列已存在,忽略 */ } + // 兼容迁移:为已有 messages 表补充 attachments 列(文件/视频等附件 JSON) + try { db.run('ALTER TABLE messages ADD COLUMN attachments TEXT'); } catch { /* 列已存在,忽略 */ } + + // 兼容迁移:为已有 messages 表补充 prompt_eval_count 列 // 兼容迁移:为已有 skills 表补充 summary 列 try { db.run("ALTER TABLE skills ADD COLUMN summary TEXT DEFAULT ''"); } catch { /* 列已存在,忽略 */ } @@ -272,6 +274,7 @@ export interface MessageRow { images: string | null; tool_calls: string | null; tool_name: string | null; + attachments: string | null; eval_count: number | null; prompt_eval_count: number | null; total_duration: number | null; @@ -374,10 +377,10 @@ export function clearAllSessions(): void { export function saveMessage(msg: MessageRow): string { const d = getDb(); - runExec(d, `INSERT OR REPLACE INTO messages (id, session_id, role, content, thinking, images, tool_calls, tool_name, eval_count, prompt_eval_count, total_duration, created_at) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + runExec(d, `INSERT OR REPLACE INTO messages (id, session_id, role, content, thinking, images, tool_calls, tool_name, attachments, eval_count, prompt_eval_count, total_duration, created_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [msg.id, msg.session_id, msg.role, msg.content, msg.thinking, msg.images, - msg.tool_calls, msg.tool_name, msg.eval_count, msg.prompt_eval_count, msg.total_duration, msg.created_at] + msg.tool_calls, msg.tool_name, msg.attachments, msg.eval_count, msg.prompt_eval_count, msg.total_duration, msg.created_at] ); persist(); return msg.id; diff --git a/src/renderer/db/chat-db.ts b/src/renderer/db/chat-db.ts index fcca55a..e2e52f0 100644 --- a/src/renderer/db/chat-db.ts +++ b/src/renderer/db/chat-db.ts @@ -89,6 +89,7 @@ export class ChatDB { images: msg.images?.length ? JSON.stringify(msg.images) : null, tool_calls: msg.toolCalls?.length ? JSON.stringify(msg.toolCalls) : null, tool_name: null, + attachments: JSON.stringify({ files: msg.files, videos: (msg as any)._videos }), eval_count: msg.eval_count || null, prompt_eval_count: msg.prompt_eval_count || null, total_duration: msg.total_duration || null, @@ -106,17 +107,23 @@ export class ChatDB { const row = await dbBridge().getSession(id); if (!row) return null; const msgRows = await dbBridge().getMessages(id); - const messages = msgRows.map((r: any) => ({ - role: r.role, - content: r.content || '', - timestamp: r.created_at, - think: r.thinking || undefined, - images: r.images ? JSON.parse(r.images) : undefined, - eval_count: r.eval_count || undefined, - prompt_eval_count: r.prompt_eval_count || undefined, - total_duration: r.total_duration || undefined, - toolCalls: r.tool_calls ? JSON.parse(r.tool_calls) : undefined - })); + const messages = msgRows.map((r: any) => { + let files, videos; + try { const a = JSON.parse(r.attachments || '{}'); files = a.files; videos = a.videos; } catch { /* ignore */ } + return { + role: r.role, + content: r.content || '', + timestamp: r.created_at, + think: r.thinking || undefined, + images: r.images ? JSON.parse(r.images) : undefined, + eval_count: r.eval_count || undefined, + prompt_eval_count: r.prompt_eval_count || undefined, + total_duration: r.total_duration || undefined, + toolCalls: r.tool_calls ? JSON.parse(r.tool_calls) : undefined, + ...(files?.length && { files }), + ...(videos?.length && { _videos: videos }) + }; + }); return { id: row.id, title: row.title,