Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion packages/agent/src/tools/io/textEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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));
});
});
14 changes: 12 additions & 2 deletions packages/agent/src/tools/io/textEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,19 @@ export const textEditorTool: Tool<Parameters, ReturnType> = {
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 }) => {
Expand Down
Loading