From d3a28822b374e36cd8ada213ba2b5e7332c47636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B4=AB=E5=BD=B1233?= Date: Wed, 24 Jun 2026 15:10:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20memory=20=E5=B7=A5=E5=85=B7=20required?= =?UTF-8?q?=20=E6=95=B0=E7=BB=84=E6=B7=BB=E5=8A=A0=20type=20=E2=80=94=20?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=A8=A1=E5=9E=8B=E6=8C=81=E7=BB=AD=E4=B8=8D?= =?UTF-8?q?=E4=BC=A0=20type=20=E5=8F=82=E6=95=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ollama function calling 依赖 JSON Schema 的 required 数组判断参数必要性, type 不在 required 中导致模型认为它是可选的,即使 error 提示了也不传。 将其加入 required 数组后,模型将被强制要求每次调用都提供 type 参数。 --- src/main/main.ts | 23 ++++++++++++++++++++++- src/renderer/services/tool-registry.ts | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/main.ts b/src/main/main.ts index ac2b96b..d138ab1 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -194,7 +194,7 @@ app.on('window-all-closed', () => { } }); -app.on('before-quit', () => { +app.on('before-quit', async () => { isQuitting = true; // 清理浏览器 browserClose(); @@ -204,4 +204,25 @@ app.on('before-quit', () => { killAllProcesses(); // 通知渲染进程释放显存 mainWindow?.webContents.send('app-quit'); + // 主进程直接调用 Ollama API 释放显存(更可靠,不依赖渲染进程) + try { + const OLlama_URL = 'http://127.0.0.1:11434'; + const psResp = await fetch(`${OLlama_URL}/api/ps`); + if (psResp.ok) { + const psData = await psResp.json() as { models?: Array<{ name: string }> }; + const models = psData.models || []; + for (const m of models) { + try { + await fetch(`${OLlama_URL}/api/generate`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ model: m.name, keep_alive: 0 }), + }); + } catch { /* 忽略单个模型释放失败 */ } + } + if (models.length > 0) { + console.log(`[before-quit] 已释放 ${models.length} 个模型显存`); + } + } + } catch { /* 释放显存失败不影响退出 */ } }); diff --git a/src/renderer/services/tool-registry.ts b/src/renderer/services/tool-registry.ts index 915b64a..ec3ef6b 100644 --- a/src/renderer/services/tool-registry.ts +++ b/src/renderer/services/tool-registry.ts @@ -367,7 +367,7 @@ export const TOOL_DEFINITIONS: ToolDefinition[] = [ CRITICAL: For add action, you MUST include both "type" (fact/preference/rule) and "content" fields. If you omit them, the call will fail.`, parameters: { type: 'object', - required: ['action'], + required: ['action', 'type'], properties: { action: { type: 'string', enum: ['search', 'add', 'replace', 'remove', 'read_all'], description: 'Which operation to perform.' }, query: { type: 'string', description: '[search] Keywords to search for.' },