fix: 工具相对路径解析基于工作空间目录而非进程 CWD

所有文件系统工具(read_file/write_file/list_directory/search_files/create_directory/delete_file)
的相对路径此前用 path.resolve() 解析到进程当前工作目录,导致 AI 创建的文件实际写到了错误位置。
新增 resolvePath() 辅助函数,相对路径统一基于 getWorkspaceDir() 解析。
This commit is contained in:
thzxx
2026-04-08 14:18:19 +08:00
parent a67995166c
commit 2fafa357b1
2 changed files with 14 additions and 8 deletions
+2 -2
View File
@@ -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",
+12 -6
View File
@@ -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<ToolResult> {
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<ToolResult> {
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<ToolResult> {
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<ToolResult> {
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<ToolResult> {
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<ToolRes
export async function handleDeleteFile(params: { path: string; recursive?: boolean }): Promise<ToolResult> {
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 };