From a5ea845c32bc569cda4330f59f1bf1553a236aea Mon Sep 17 00:00:00 2001 From: "Ben Houston (via MyCoder)" Date: Wed, 12 Mar 2025 13:34:58 +0000 Subject: [PATCH] fix: convert absolute paths to relative paths in textEditor log output --- .../agent/src/tools/io/textEditor.test.ts | 62 ++++++++++++++++++- packages/agent/src/tools/io/textEditor.ts | 14 ++++- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/packages/agent/src/tools/io/textEditor.test.ts b/packages/agent/src/tools/io/textEditor.test.ts index a8c9e85..0bae64d 100644 --- a/packages/agent/src/tools/io/textEditor.test.ts +++ b/packages/agent/src/tools/io/textEditor.test.ts @@ -3,9 +3,10 @@ import { mkdtemp, readFile } from 'fs/promises'; import { tmpdir } from 'os'; import { join } from 'path'; -import { describe, it, expect, beforeEach, afterEach } from 'vitest'; +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { ToolContext } from '../../core/types.js'; +import { MockLogger } from '../../utils/mockLogger.js'; import { getMockToolContext } from '../getTools.test.js'; import { shellExecuteTool } from '../system/shellExecute.js'; @@ -384,4 +385,63 @@ describe('textEditor', () => { content = await readFile(testPath, 'utf8'); expect(content).toBe(initialContent); }); + + it('should convert absolute paths to relative paths in log messages', () => { + // Create a mock logger with a spy on the info method + const mockLogger = new MockLogger(); + const infoSpy = vi.spyOn(mockLogger, 'info'); + + // Create a context with a specific working directory + const contextWithWorkingDir: ToolContext = { + ...toolContext, + logger: mockLogger, + workingDirectory: '/home/user/project', + }; + + // Test with an absolute path within the working directory + const absolutePath = '/home/user/project/packages/agent/src/file.ts'; + textEditorTool.logParameters?.( + { + command: 'view', + path: absolutePath, + description: 'test path conversion', + }, + contextWithWorkingDir, + ); + + // Verify the log message contains the relative path + expect(infoSpy).toHaveBeenCalledWith( + expect.stringContaining('./packages/agent/src/file.ts'), + ); + + // Test with an absolute path outside the working directory + infoSpy.mockClear(); + const externalPath = '/etc/config.json'; + textEditorTool.logParameters?.( + { + command: 'view', + path: externalPath, + description: 'test external path', + }, + contextWithWorkingDir, + ); + + // Verify the log message keeps the absolute path + expect(infoSpy).toHaveBeenCalledWith(expect.stringContaining(externalPath)); + + // Test with a relative path + infoSpy.mockClear(); + const relativePath = 'src/file.ts'; + textEditorTool.logParameters?.( + { + command: 'view', + path: relativePath, + description: 'test relative path', + }, + contextWithWorkingDir, + ); + + // Verify the log message keeps the relative path as is + expect(infoSpy).toHaveBeenCalledWith(expect.stringContaining(relativePath)); + }); }); diff --git a/packages/agent/src/tools/io/textEditor.ts b/packages/agent/src/tools/io/textEditor.ts index 1147468..f881ed9 100644 --- a/packages/agent/src/tools/io/textEditor.ts +++ b/packages/agent/src/tools/io/textEditor.ts @@ -302,9 +302,19 @@ export const textEditorTool: Tool = { throw new Error(`Unknown command: ${command}`); } }, - logParameters: (input, { logger }) => { + logParameters: (input, { logger, workingDirectory }) => { + // Convert absolute path to relative path if possible + let displayPath = input.path; + if (workingDirectory && path.isAbsolute(input.path)) { + // Check if the path is within the working directory + if (input.path.startsWith(workingDirectory)) { + // Convert to relative path with ./ prefix + displayPath = './' + path.relative(workingDirectory, input.path); + } + } + logger.info( - `${input.command} operation on "${input.path}", ${input.description}`, + `${input.command} operation on "${displayPath}", ${input.description}`, ); }, logReturns: (result, { logger }) => {