From 2fafa357b180b39320ef395da1a259c2c2c3a166 Mon Sep 17 00:00:00 2001 From: thzxx Date: Wed, 8 Apr 2026 14:18:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B7=A5=E5=85=B7=E7=9B=B8=E5=AF=B9?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E8=A7=A3=E6=9E=90=E5=9F=BA=E4=BA=8E=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E7=A9=BA=E9=97=B4=E7=9B=AE=E5=BD=95=E8=80=8C=E9=9D=9E?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=20CWD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 所有文件系统工具(read_file/write_file/list_directory/search_files/create_directory/delete_file) 的相对路径此前用 path.resolve() 解析到进程当前工作目录,导致 AI 创建的文件实际写到了错误位置。 新增 resolvePath() 辅助函数,相对路径统一基于 getWorkspaceDir() 解析。 --- package-lock.json | 4 ++-- src/main/tool-handlers.ts | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2029c0c..d64ec4f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "metona-ollama-desktop", - "version": "3.2.2", + "version": "3.2.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "metona-ollama-desktop", - "version": "3.2.2", + "version": "3.2.4", "license": "MIT", "devDependencies": { "@types/node": "^20.17.0", diff --git a/src/main/tool-handlers.ts b/src/main/tool-handlers.ts index 6d1ff3d..5720906 100644 --- a/src/main/tool-handlers.ts +++ b/src/main/tool-handlers.ts @@ -15,6 +15,12 @@ function sendLog(level: 'info' | 'success' | 'warn' | 'error' | 'debug', message mainWindow?.webContents.send('main:log', { level, message, detail }); } +/** 解析路径:相对路径基于工作空间目录 */ +function resolvePath(inputPath: string): string { + if (path.isAbsolute(inputPath)) return path.resolve(inputPath); + return path.resolve(getWorkspaceDir(), inputPath); +} + export interface ToolResult { success: boolean; [key: string]: unknown; @@ -22,7 +28,7 @@ export interface ToolResult { export async function handleReadFile(params: { path: string; encoding?: string; start_line?: number; end_line?: number }): Promise { try { - const filePath = path.resolve(params.path); + const filePath = resolvePath(params.path); const allowed = checkPathAllowed(filePath, 'read'); if (!allowed.ok) return { success: false, error: allowed.reason }; @@ -68,7 +74,7 @@ export async function handleReadFile(params: { path: string; encoding?: string; export async function handleWriteFile(params: { path: string; content: string; encoding?: string }): Promise { try { - const filePath = path.resolve(params.path); + const filePath = resolvePath(params.path); const allowed = checkPathAllowed(filePath, 'write'); if (!allowed.ok) return { success: false, error: allowed.reason }; @@ -101,7 +107,7 @@ export async function handleWriteFile(params: { path: string; content: string; e export async function handleListDir(params: { path: string; recursive?: boolean; max_depth?: number; include_hidden?: boolean; filter_extension?: string }): Promise { try { - const dirPath = path.resolve(params.path); + const dirPath = resolvePath(params.path); const allowed = checkPathAllowed(dirPath, 'read'); if (!allowed.ok) return { success: false, error: allowed.reason }; @@ -143,7 +149,7 @@ export async function handleListDir(params: { path: string; recursive?: boolean; export async function handleSearchFiles(params: { path: string; query: string; search_type?: string; case_sensitive?: boolean; max_results?: number; file_extensions?: string[] }): Promise { try { - const rootPath = path.resolve(params.path); + const rootPath = resolvePath(params.path); const allowed = checkPathAllowed(rootPath, 'read'); if (!allowed.ok) return { success: false, error: allowed.reason }; @@ -235,7 +241,7 @@ export async function handleSearchFiles(params: { path: string; query: string; s export async function handleCreateDir(params: { path: string }): Promise { try { - const dirPath = path.resolve(params.path); + const dirPath = resolvePath(params.path); const allowed = checkPathAllowed(dirPath, 'write'); if (!allowed.ok) return { success: false, error: allowed.reason }; @@ -248,7 +254,7 @@ export async function handleCreateDir(params: { path: string }): Promise { try { - const filePath = path.resolve(params.path); + const filePath = resolvePath(params.path); const allowed = checkPathAllowed(filePath, 'write'); if (!allowed.ok) return { success: false, error: allowed.reason };