Skip to content

Commit 12a5b7f

Browse files
authored
Merge pull request #224 from drivecore/fix/issue-223-relative-paths
fix: convert absolute paths to relative paths in textEditor log output
2 parents 929c3c8 + a5ea845 commit 12a5b7f

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

packages/agent/src/tools/io/textEditor.test.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { mkdtemp, readFile } from 'fs/promises';
33
import { tmpdir } from 'os';
44
import { join } from 'path';
55

6-
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
6+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
77

88
import { ToolContext } from '../../core/types.js';
9+
import { MockLogger } from '../../utils/mockLogger.js';
910
import { getMockToolContext } from '../getTools.test.js';
1011
import { shellExecuteTool } from '../system/shellExecute.js';
1112

@@ -384,4 +385,63 @@ describe('textEditor', () => {
384385
content = await readFile(testPath, 'utf8');
385386
expect(content).toBe(initialContent);
386387
});
388+
389+
it('should convert absolute paths to relative paths in log messages', () => {
390+
// Create a mock logger with a spy on the info method
391+
const mockLogger = new MockLogger();
392+
const infoSpy = vi.spyOn(mockLogger, 'info');
393+
394+
// Create a context with a specific working directory
395+
const contextWithWorkingDir: ToolContext = {
396+
...toolContext,
397+
logger: mockLogger,
398+
workingDirectory: '/home/user/project',
399+
};
400+
401+
// Test with an absolute path within the working directory
402+
const absolutePath = '/home/user/project/packages/agent/src/file.ts';
403+
textEditorTool.logParameters?.(
404+
{
405+
command: 'view',
406+
path: absolutePath,
407+
description: 'test path conversion',
408+
},
409+
contextWithWorkingDir,
410+
);
411+
412+
// Verify the log message contains the relative path
413+
expect(infoSpy).toHaveBeenCalledWith(
414+
expect.stringContaining('./packages/agent/src/file.ts'),
415+
);
416+
417+
// Test with an absolute path outside the working directory
418+
infoSpy.mockClear();
419+
const externalPath = '/etc/config.json';
420+
textEditorTool.logParameters?.(
421+
{
422+
command: 'view',
423+
path: externalPath,
424+
description: 'test external path',
425+
},
426+
contextWithWorkingDir,
427+
);
428+
429+
// Verify the log message keeps the absolute path
430+
expect(infoSpy).toHaveBeenCalledWith(expect.stringContaining(externalPath));
431+
432+
// Test with a relative path
433+
infoSpy.mockClear();
434+
const relativePath = 'src/file.ts';
435+
textEditorTool.logParameters?.(
436+
{
437+
command: 'view',
438+
path: relativePath,
439+
description: 'test relative path',
440+
},
441+
contextWithWorkingDir,
442+
);
443+
444+
// Verify the log message keeps the relative path as is
445+
expect(infoSpy).toHaveBeenCalledWith(expect.stringContaining(relativePath));
446+
});
387447
});

packages/agent/src/tools/io/textEditor.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,19 @@ export const textEditorTool: Tool<Parameters, ReturnType> = {
302302
throw new Error(`Unknown command: ${command}`);
303303
}
304304
},
305-
logParameters: (input, { logger }) => {
305+
logParameters: (input, { logger, workingDirectory }) => {
306+
// Convert absolute path to relative path if possible
307+
let displayPath = input.path;
308+
if (workingDirectory && path.isAbsolute(input.path)) {
309+
// Check if the path is within the working directory
310+
if (input.path.startsWith(workingDirectory)) {
311+
// Convert to relative path with ./ prefix
312+
displayPath = './' + path.relative(workingDirectory, input.path);
313+
}
314+
}
315+
306316
logger.info(
307-
`${input.command} operation on "${input.path}", ${input.description}`,
317+
`${input.command} operation on "${displayPath}", ${input.description}`,
308318
);
309319
},
310320
logReturns: (result, { logger }) => {

0 commit comments

Comments
 (0)