refactor: avoid double name property for tools. only use "name"
This commit is contained in:
parent
c0e292e390
commit
52cefcfa02
32 changed files with 392 additions and 318 deletions
|
|
@ -6,26 +6,26 @@ import { ToolPermissionPolicy } from "./types.js";
|
|||
*/
|
||||
export const DEFAULT_TOOL_POLICIES: ToolPermissionPolicy[] = [
|
||||
// Read-only tools are generally safe to allow
|
||||
{ tool: "read_file", permission: "allow" },
|
||||
{ tool: "list_files", permission: "allow" },
|
||||
{ tool: "search_code", permission: "allow" },
|
||||
{ tool: "fetch", permission: "allow" },
|
||||
{ tool: "Read", permission: "allow" },
|
||||
{ tool: "List", permission: "allow" },
|
||||
{ tool: "Search", permission: "allow" },
|
||||
{ tool: "Fetch", permission: "allow" },
|
||||
|
||||
// Write operations should require confirmation
|
||||
{ tool: "write_file", permission: "ask" },
|
||||
{ tool: "edit_file", permission: "ask" },
|
||||
{ tool: "Write", permission: "ask" },
|
||||
{ tool: "Edit", permission: "ask" },
|
||||
|
||||
// Write to a checklist
|
||||
{ tool: "write_checklist", permission: "allow" },
|
||||
{ tool: "Checklist", permission: "allow" },
|
||||
|
||||
// Terminal commands should require confirmation by default
|
||||
{ tool: "run_terminal_command", permission: "ask" },
|
||||
{ tool: "Bash", permission: "ask" },
|
||||
|
||||
// Exit tool is generally safe (headless mode only)
|
||||
{ tool: "exit", permission: "allow" },
|
||||
{ tool: "Exit", permission: "allow" },
|
||||
|
||||
// View diff is read-only
|
||||
{ tool: "view_diff", permission: "allow" },
|
||||
{ tool: "Diff", permission: "allow" },
|
||||
|
||||
// Default fallback - ask for any unmatched tools
|
||||
{ tool: "*", permission: "ask" },
|
||||
|
|
|
|||
|
|
@ -34,13 +34,13 @@ describe("Headless Permissions Integration", () => {
|
|||
const permissions = { policies: DEFAULT_TOOL_POLICIES };
|
||||
|
||||
// Read-only tools should be allowed
|
||||
expect(checkToolPermission({ name: "read_file", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "list_files", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "search_code", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "Read", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "List", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "Search", arguments: {} }, permissions).permission).toBe("allow");
|
||||
|
||||
// Write operations should require permission
|
||||
expect(checkToolPermission({ name: "write_file", arguments: {} }, permissions).permission).toBe("ask");
|
||||
expect(checkToolPermission({ name: "run_terminal_command", arguments: {} }, permissions).permission).toBe("ask");
|
||||
expect(checkToolPermission({ name: "Write", arguments: {} }, permissions).permission).toBe("ask");
|
||||
expect(checkToolPermission({ name: "Bash", arguments: {} }, permissions).permission).toBe("ask");
|
||||
|
||||
// Unknown tools should default to ask
|
||||
expect(checkToolPermission({ name: "unknown_tool", arguments: {} }, permissions).permission).toBe("ask");
|
||||
|
|
@ -61,8 +61,8 @@ describe("Headless Permissions Integration", () => {
|
|||
expect(excludedResult.permission).toBe("exclude");
|
||||
|
||||
// Default tools should follow their normal rules
|
||||
expect(checkToolPermission({ name: "read_file", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "write_file", arguments: {} }, permissions).permission).toBe("ask");
|
||||
expect(checkToolPermission({ name: "Read", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "Write", arguments: {} }, permissions).permission).toBe("ask");
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -74,7 +74,7 @@ describe("Headless Permissions Integration", () => {
|
|||
// Still exclude truly dangerous operations
|
||||
exclude: ["rm", "sudo"],
|
||||
// Allow specific tools that would normally require confirmation
|
||||
allow: ["write_file", "run_terminal_command"],
|
||||
allow: ["Write", "Bash"],
|
||||
},
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
|
|
@ -87,11 +87,11 @@ describe("Headless Permissions Integration", () => {
|
|||
expect(checkToolPermission({ name: "sudo", arguments: {} }, permissions).permission).toBe("exclude");
|
||||
|
||||
// Explicitly allowed tools should be allowed
|
||||
expect(checkToolPermission({ name: "write_file", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "run_terminal_command", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "Write", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "Bash", arguments: {} }, permissions).permission).toBe("allow");
|
||||
|
||||
// Read-only tools should still be allowed by default
|
||||
expect(checkToolPermission({ name: "read_file", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "Read", arguments: {} }, permissions).permission).toBe("allow");
|
||||
|
||||
// Unknown tools should default to ask (since no wildcard allow override was provided)
|
||||
expect(checkToolPermission({ name: "unknown_tool", arguments: {} }, permissions).permission).toBe("ask");
|
||||
|
|
@ -109,8 +109,8 @@ describe("Headless Permissions Integration", () => {
|
|||
const permissions = { policies };
|
||||
|
||||
// All tools should be allowed due to wildcard override
|
||||
expect(checkToolPermission({ name: "write_file", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "run_terminal_command", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "Write", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "Bash", arguments: {} }, permissions).permission).toBe("allow");
|
||||
expect(checkToolPermission({ name: "unknown_tool", arguments: {} }, permissions).permission).toBe("allow");
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -22,13 +22,13 @@ describe('Permission Integration Tests', () => {
|
|||
// Check that the service is in headless mode
|
||||
expect(state.isHeadless).toBe(true);
|
||||
|
||||
// Check permission for edit_file (the actual tool name)
|
||||
// Check permission for Edit tool
|
||||
const result = checkToolPermission(
|
||||
{ name: 'edit_file', arguments: {} },
|
||||
{ name: 'Edit', arguments: {} },
|
||||
state.permissions
|
||||
);
|
||||
|
||||
// Should be allowed because --allow Edit normalizes to edit_file
|
||||
// Should be allowed because --allow Edit is specified
|
||||
expect(result.permission).toBe('allow');
|
||||
});
|
||||
|
||||
|
|
@ -41,23 +41,23 @@ describe('Permission Integration Tests', () => {
|
|||
|
||||
const state = service.getState();
|
||||
|
||||
// Check Edit (edit_file)
|
||||
// Check Edit
|
||||
const editResult = checkToolPermission(
|
||||
{ name: 'edit_file', arguments: {} },
|
||||
{ name: 'Edit', arguments: {} },
|
||||
state.permissions
|
||||
);
|
||||
expect(editResult.permission).toBe('allow');
|
||||
|
||||
// Check Write (write_file)
|
||||
// Check Write
|
||||
const writeResult = checkToolPermission(
|
||||
{ name: 'write_file', arguments: {} },
|
||||
{ name: 'Write', arguments: {} },
|
||||
state.permissions
|
||||
);
|
||||
expect(writeResult.permission).toBe('allow');
|
||||
|
||||
// Check Bash (run_terminal_command)
|
||||
// Check Bash
|
||||
const bashResult = checkToolPermission(
|
||||
{ name: 'run_terminal_command', arguments: {} },
|
||||
{ name: 'Bash', arguments: {} },
|
||||
state.permissions
|
||||
);
|
||||
expect(bashResult.permission).toBe('allow');
|
||||
|
|
@ -74,14 +74,14 @@ describe('Permission Integration Tests', () => {
|
|||
|
||||
// Check that Read is allowed
|
||||
const readResult = checkToolPermission(
|
||||
{ name: 'read_file', arguments: {} },
|
||||
{ name: 'Read', arguments: {} },
|
||||
state.permissions
|
||||
);
|
||||
expect(readResult.permission).toBe('allow');
|
||||
|
||||
// Check that Edit is excluded
|
||||
const editResult = checkToolPermission(
|
||||
{ name: 'edit_file', arguments: {} },
|
||||
{ name: 'Edit', arguments: {} },
|
||||
state.permissions
|
||||
);
|
||||
expect(editResult.permission).toBe('exclude');
|
||||
|
|
@ -99,7 +99,7 @@ describe('Permission Integration Tests', () => {
|
|||
|
||||
// In plan mode with --allow Edit override
|
||||
const editResult = checkToolPermission(
|
||||
{ name: 'edit_file', arguments: {} },
|
||||
{ name: 'Edit', arguments: {} },
|
||||
state.permissions
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -68,10 +68,10 @@ describe("Permission Checker", () => {
|
|||
});
|
||||
|
||||
describe("Bash command patterns", () => {
|
||||
it("should match Bash command patterns with run_terminal_command", () => {
|
||||
expect(matchesToolPattern("run_terminal_command", "Bash(ls*)", { command: "ls" })).toBe(true);
|
||||
expect(matchesToolPattern("run_terminal_command", "Bash(ls*)", { command: "ls -la" })).toBe(true);
|
||||
expect(matchesToolPattern("run_terminal_command", "Bash(ls*)", { command: "pwd" })).toBe(false);
|
||||
it("should match Bash command patterns with Bash tool", () => {
|
||||
expect(matchesToolPattern("Bash", "Bash(ls*)", { command: "ls" })).toBe(true);
|
||||
expect(matchesToolPattern("Bash", "Bash(ls*)", { command: "ls -la" })).toBe(true);
|
||||
expect(matchesToolPattern("Bash", "Bash(ls*)", { command: "pwd" })).toBe(false);
|
||||
});
|
||||
|
||||
it("should match Bash command patterns with Bash display name", () => {
|
||||
|
|
@ -81,24 +81,24 @@ describe("Permission Checker", () => {
|
|||
});
|
||||
|
||||
it("should match exact Bash commands", () => {
|
||||
expect(matchesToolPattern("run_terminal_command", "Bash(ls)", { command: "ls" })).toBe(true);
|
||||
expect(matchesToolPattern("run_terminal_command", "Bash(ls)", { command: "ls -la" })).toBe(false);
|
||||
expect(matchesToolPattern("Bash", "Bash(ls)", { command: "ls" })).toBe(true);
|
||||
expect(matchesToolPattern("Bash", "Bash(ls)", { command: "ls -la" })).toBe(false);
|
||||
});
|
||||
|
||||
it("should not match Bash patterns for non-bash tools", () => {
|
||||
expect(matchesToolPattern("read_file", "Bash(ls*)", { command: "ls" })).toBe(false);
|
||||
expect(matchesToolPattern("write_file", "Bash(git*)", { command: "git status" })).toBe(false);
|
||||
expect(matchesToolPattern("Read", "Bash(ls*)", { command: "ls" })).toBe(false);
|
||||
expect(matchesToolPattern("Write", "Bash(git*)", { command: "git status" })).toBe(false);
|
||||
});
|
||||
|
||||
it("should not match Bash patterns without command argument", () => {
|
||||
expect(matchesToolPattern("run_terminal_command", "Bash(ls*)", {})).toBe(false);
|
||||
expect(matchesToolPattern("run_terminal_command", "Bash(ls*)", { other: "value" })).toBe(false);
|
||||
expect(matchesToolPattern("Bash", "Bash(ls*)", {})).toBe(false);
|
||||
expect(matchesToolPattern("Bash", "Bash(ls*)", { other: "value" })).toBe(false);
|
||||
});
|
||||
|
||||
it("should handle complex Bash command patterns", () => {
|
||||
expect(matchesToolPattern("run_terminal_command", "Bash(npm*)", { command: "npm install" })).toBe(true);
|
||||
expect(matchesToolPattern("run_terminal_command", "Bash(npm*)", { command: "npm run build" })).toBe(true);
|
||||
expect(matchesToolPattern("run_terminal_command", "Bash(git*commit*)", { command: "git commit -m 'test'" })).toBe(true);
|
||||
expect(matchesToolPattern("Bash", "Bash(npm*)", { command: "npm install" })).toBe(true);
|
||||
expect(matchesToolPattern("Bash", "Bash(npm*)", { command: "npm run build" })).toBe(true);
|
||||
expect(matchesToolPattern("Bash", "Bash(git*commit*)", { command: "git commit -m 'test'" })).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -393,11 +393,11 @@ describe("Permission Checker", () => {
|
|||
|
||||
it("should use default policies when no custom permissions provided", () => {
|
||||
const result = checkToolPermission({
|
||||
name: "read_file",
|
||||
name: "Read",
|
||||
arguments: { path: "/test.txt" },
|
||||
});
|
||||
|
||||
// Should match the default policies (read_file should be "allow")
|
||||
// Should match the default policies (Read should be "allow")
|
||||
expect(result.permission).toBe("allow");
|
||||
});
|
||||
|
||||
|
|
@ -419,13 +419,13 @@ describe("Permission Checker", () => {
|
|||
policies: [
|
||||
{ tool: "Bash(ls*)", permission: "allow" },
|
||||
{ tool: "Bash(git*)", permission: "ask" },
|
||||
{ tool: "run_terminal_command", permission: "ask" }, // Fallback for other commands
|
||||
{ tool: "Bash", permission: "ask" }, // Fallback for other commands
|
||||
],
|
||||
};
|
||||
|
||||
// Should match "Bash(ls*)" pattern and allow
|
||||
const lsResult = checkToolPermission(
|
||||
{ name: "run_terminal_command", arguments: { command: "ls -la" } },
|
||||
{ name: "Bash", arguments: { command: "ls -la" } },
|
||||
permissions
|
||||
);
|
||||
expect(lsResult.permission).toBe("allow");
|
||||
|
|
@ -433,7 +433,7 @@ describe("Permission Checker", () => {
|
|||
|
||||
// Should match "Bash(git*)" pattern and ask
|
||||
const gitResult = checkToolPermission(
|
||||
{ name: "run_terminal_command", arguments: { command: "git status" } },
|
||||
{ name: "Bash", arguments: { command: "git status" } },
|
||||
permissions
|
||||
);
|
||||
expect(gitResult.permission).toBe("ask");
|
||||
|
|
@ -441,28 +441,28 @@ describe("Permission Checker", () => {
|
|||
|
||||
// Should match general fallback for other commands
|
||||
const npmResult = checkToolPermission(
|
||||
{ name: "run_terminal_command", arguments: { command: "npm install" } },
|
||||
{ name: "Bash", arguments: { command: "npm install" } },
|
||||
permissions
|
||||
);
|
||||
expect(npmResult.permission).toBe("ask");
|
||||
expect(npmResult.matchedPolicy?.tool).toBe("run_terminal_command");
|
||||
expect(npmResult.matchedPolicy?.tool).toBe("Bash");
|
||||
});
|
||||
|
||||
it("should match argument patterns with glob patterns", () => {
|
||||
const permissions: ToolPermissions = {
|
||||
policies: [
|
||||
{
|
||||
tool: "run_terminal_command",
|
||||
tool: "Bash",
|
||||
permission: "exclude",
|
||||
argumentMatches: { command: "rm*" }
|
||||
},
|
||||
{
|
||||
tool: "write_file",
|
||||
tool: "Write",
|
||||
permission: "ask",
|
||||
argumentMatches: { file_path: "**/*.ts" }
|
||||
},
|
||||
{
|
||||
tool: "write_file",
|
||||
tool: "Write",
|
||||
permission: "allow"
|
||||
}
|
||||
],
|
||||
|
|
@ -470,7 +470,7 @@ describe("Permission Checker", () => {
|
|||
|
||||
// Should exclude dangerous rm commands
|
||||
const rmResult = checkToolPermission(
|
||||
{ name: "run_terminal_command", arguments: { command: "rm -rf /" } },
|
||||
{ name: "Bash", arguments: { command: "rm -rf /" } },
|
||||
permissions
|
||||
);
|
||||
expect(rmResult.permission).toBe("exclude");
|
||||
|
|
@ -478,7 +478,7 @@ describe("Permission Checker", () => {
|
|||
|
||||
// Should ask for TypeScript files
|
||||
const tsResult = checkToolPermission(
|
||||
{ name: "write_file", arguments: { file_path: "/src/components/Button.ts" } },
|
||||
{ name: "Write", arguments: { file_path: "/src/components/Button.ts" } },
|
||||
permissions
|
||||
);
|
||||
expect(tsResult.permission).toBe("ask");
|
||||
|
|
@ -486,7 +486,7 @@ describe("Permission Checker", () => {
|
|||
|
||||
// Should allow other file writes
|
||||
const jsResult = checkToolPermission(
|
||||
{ name: "write_file", arguments: { file_path: "/src/utils/helper.js" } },
|
||||
{ name: "Write", arguments: { file_path: "/src/utils/helper.js" } },
|
||||
permissions
|
||||
);
|
||||
expect(jsResult.permission).toBe("allow");
|
||||
|
|
|
|||
|
|
@ -24,8 +24,8 @@ export function matchesToolPattern(
|
|||
// Handle special Bash command patterns like "Bash(ls*)"
|
||||
const bashCommandMatch = pattern.match(/^Bash\((.+)\)$/);
|
||||
if (bashCommandMatch) {
|
||||
// Check if this is a bash/terminal tool (either normalized name or display name)
|
||||
const isBashTool = toolName === "run_terminal_command" || toolName === "Bash";
|
||||
// Check if this is a bash/terminal tool
|
||||
const isBashTool = toolName === "Bash";
|
||||
if (isBashTool && toolArguments?.command) {
|
||||
const commandPattern = bashCommandMatch[1];
|
||||
const command = toolArguments.command;
|
||||
|
|
|
|||
|
|
@ -12,24 +12,24 @@ describe("permissionsYamlLoader", () => {
|
|||
const policies = yamlConfigToPolicies(config);
|
||||
|
||||
expect(policies).toEqual([
|
||||
{ tool: "run_terminal_command", permission: "exclude" },
|
||||
{ tool: "write_file", permission: "ask" },
|
||||
{ tool: "read_file", permission: "allow" },
|
||||
{ tool: "list_files", permission: "allow" },
|
||||
{ tool: "Bash", permission: "exclude" },
|
||||
{ tool: "Write", permission: "ask" },
|
||||
{ tool: "Read", permission: "allow" },
|
||||
{ tool: "List", permission: "allow" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("should normalize tool names", () => {
|
||||
const config = {
|
||||
allow: ["read", "READ", "read_file"],
|
||||
allow: ["read", "READ", "Read"],
|
||||
};
|
||||
|
||||
const policies = yamlConfigToPolicies(config);
|
||||
|
||||
expect(policies).toEqual([
|
||||
{ tool: "read_file", permission: "allow" },
|
||||
{ tool: "read_file", permission: "allow" },
|
||||
{ tool: "read_file", permission: "allow" },
|
||||
{ tool: "Read", permission: "allow" },
|
||||
{ tool: "Read", permission: "allow" },
|
||||
{ tool: "Read", permission: "allow" },
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
@ -63,7 +63,7 @@ describe("permissionsYamlLoader", () => {
|
|||
|
||||
const policies = yamlConfigToPolicies(config);
|
||||
|
||||
expect(policies).toEqual([{ tool: "read_file", permission: "allow" }]);
|
||||
expect(policies).toEqual([{ tool: "Read", permission: "allow" }]);
|
||||
});
|
||||
|
||||
it("should handle all permission types", () => {
|
||||
|
|
@ -76,12 +76,12 @@ describe("permissionsYamlLoader", () => {
|
|||
const policies = yamlConfigToPolicies(config);
|
||||
|
||||
expect(policies).toEqual([
|
||||
{ tool: "fetch", permission: "exclude" },
|
||||
{ tool: "exit", permission: "exclude" },
|
||||
{ tool: "write_file", permission: "ask" },
|
||||
{ tool: "run_terminal_command", permission: "ask" },
|
||||
{ tool: "read_file", permission: "allow" },
|
||||
{ tool: "list_files", permission: "allow" },
|
||||
{ tool: "Fetch", permission: "exclude" },
|
||||
{ tool: "Exit", permission: "exclude" },
|
||||
{ tool: "Write", permission: "ask" },
|
||||
{ tool: "Bash", permission: "ask" },
|
||||
{ tool: "Read", permission: "allow" },
|
||||
{ tool: "List", permission: "allow" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
@ -91,7 +91,7 @@ describe("permissionsYamlLoader", () => {
|
|||
const policy = parseToolPattern("Write", "allow");
|
||||
|
||||
expect(policy).toEqual({
|
||||
tool: "write_file",
|
||||
tool: "Write",
|
||||
permission: "allow"
|
||||
});
|
||||
});
|
||||
|
|
@ -100,7 +100,7 @@ describe("permissionsYamlLoader", () => {
|
|||
const policy = parseToolPattern("Write(**/*.ts)", "exclude");
|
||||
|
||||
expect(policy).toEqual({
|
||||
tool: "write_file",
|
||||
tool: "Write",
|
||||
permission: "exclude",
|
||||
argumentMatches: {
|
||||
file_path: "**/*.ts"
|
||||
|
|
@ -112,7 +112,7 @@ describe("permissionsYamlLoader", () => {
|
|||
const policy = parseToolPattern("Bash(npm install)", "ask");
|
||||
|
||||
expect(policy).toEqual({
|
||||
tool: "run_terminal_command",
|
||||
tool: "Bash",
|
||||
permission: "ask",
|
||||
argumentMatches: {
|
||||
command: "npm install"
|
||||
|
|
@ -124,19 +124,19 @@ describe("permissionsYamlLoader", () => {
|
|||
const testCases = [
|
||||
{
|
||||
pattern: "Read(src/**/*.js)",
|
||||
expected: { tool: "read_file", permission: "allow", argumentMatches: { file_path: "src/**/*.js" } }
|
||||
expected: { tool: "Read", permission: "allow", argumentMatches: { file_path: "src/**/*.js" } }
|
||||
},
|
||||
{
|
||||
pattern: "List(/home/user)",
|
||||
expected: { tool: "list_files", permission: "allow", argumentMatches: { path: "/home/user" } }
|
||||
expected: { tool: "List", permission: "allow", argumentMatches: { path: "/home/user" } }
|
||||
},
|
||||
{
|
||||
pattern: "Search(TODO)",
|
||||
expected: { tool: "search_code", permission: "allow", argumentMatches: { query: "TODO" } }
|
||||
expected: { tool: "Search", permission: "allow", argumentMatches: { query: "TODO" } }
|
||||
},
|
||||
{
|
||||
pattern: "Fetch(https://api.example.com)",
|
||||
expected: { tool: "fetch", permission: "allow", argumentMatches: { url: "https://api.example.com" } }
|
||||
expected: { tool: "Fetch", permission: "allow", argumentMatches: { url: "https://api.example.com" } }
|
||||
}
|
||||
];
|
||||
|
||||
|
|
@ -150,7 +150,7 @@ describe("permissionsYamlLoader", () => {
|
|||
const policy = parseToolPattern("Write()", "allow");
|
||||
|
||||
expect(policy).toEqual({
|
||||
tool: "write_file",
|
||||
tool: "Write",
|
||||
permission: "allow"
|
||||
});
|
||||
});
|
||||
|
|
@ -159,7 +159,7 @@ describe("permissionsYamlLoader", () => {
|
|||
const policy = parseToolPattern("Bash( npm run test )", "ask");
|
||||
|
||||
expect(policy).toEqual({
|
||||
tool: "run_terminal_command",
|
||||
tool: "Bash",
|
||||
permission: "ask",
|
||||
argumentMatches: {
|
||||
command: "npm run test"
|
||||
|
|
@ -187,7 +187,7 @@ describe("permissionsYamlLoader", () => {
|
|||
const policy = parseToolPattern("write(**/*.ts)", "allow");
|
||||
|
||||
expect(policy).toEqual({
|
||||
tool: "write_file",
|
||||
tool: "Write",
|
||||
permission: "allow",
|
||||
argumentMatches: {
|
||||
file_path: "**/*.ts"
|
||||
|
|
|
|||
|
|
@ -70,9 +70,9 @@ export function loadPermissionsYaml(): PermissionsYamlConfig | null {
|
|||
/**
|
||||
* Parses a pattern string into a ToolPermissionPolicy
|
||||
* Supports formats like:
|
||||
* - "Write" -> { tool: "write_file", permission }
|
||||
* - "Write(pattern)" -> { tool: "write_file", permission, argumentMatches: { file_path: "pattern" } }
|
||||
* - "Bash(npm install)" -> { tool: "run_terminal_command", permission, argumentMatches: { command: "npm install" } }
|
||||
* - "Write" -> { tool: "Write", permission }
|
||||
* - "Write(pattern)" -> { tool: "Write", permission, argumentMatches: { file_path: "pattern" } }
|
||||
* - "Bash(npm install)" -> { tool: "Bash", permission, argumentMatches: { command: "npm install" } }
|
||||
*/
|
||||
export function parseToolPattern(
|
||||
pattern: string,
|
||||
|
|
@ -96,14 +96,14 @@ export function parseToolPattern(
|
|||
if (trimmedArgs) {
|
||||
// Map tool names to their primary argument parameter
|
||||
const toolArgMappings: Record<string, string> = {
|
||||
write_file: "file_path",
|
||||
edit_file: "file_path",
|
||||
read_file: "file_path",
|
||||
list_files: "path",
|
||||
search_code: "query",
|
||||
run_terminal_command: "command",
|
||||
fetch: "url",
|
||||
view_diff: "file_path",
|
||||
Write: "file_path",
|
||||
Edit: "file_path",
|
||||
Read: "file_path",
|
||||
List: "path",
|
||||
Search: "query",
|
||||
Bash: "command",
|
||||
Fetch: "url",
|
||||
Diff: "file_path",
|
||||
};
|
||||
|
||||
const argKey = toolArgMappings[normalizedName] || "pattern";
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ import logger from "../util/logger.js";
|
|||
export function generatePolicyRule(toolName: string, toolArgs: any): string {
|
||||
const normalizedName = normalizeToolName(toolName);
|
||||
|
||||
// For Bash tool (run_terminal_command), create command-specific policies
|
||||
if (normalizedName === "run_terminal_command" && toolArgs?.command) {
|
||||
// For Bash tool, create command-specific policies
|
||||
if (normalizedName === "Bash" && toolArgs?.command) {
|
||||
const command = toolArgs.command.trim();
|
||||
// Extract the first command (before pipes, &&, ||, etc.)
|
||||
const firstCommand = command.split(/[;&|]|&&|\|\|/)[0]?.trim();
|
||||
|
|
|
|||
23
src/permissions/policyWriter.verify.test.ts
Normal file
23
src/permissions/policyWriter.verify.test.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import { generatePolicyRule } from "./policyWriter.js";
|
||||
|
||||
describe("policyWriter verification", () => {
|
||||
it("should return display names for all tools", () => {
|
||||
// Test Edit tool
|
||||
expect(generatePolicyRule("Edit", { filepath: "test.txt" })).toBe("Edit");
|
||||
|
||||
// Test Write tool
|
||||
expect(generatePolicyRule("Write", { filepath: "test.txt" })).toBe("Write");
|
||||
|
||||
// Test Read tool
|
||||
expect(generatePolicyRule("Read", { filepath: "test.txt" })).toBe("Read");
|
||||
|
||||
// Test Bash tool with commands
|
||||
expect(generatePolicyRule("Bash", { command: "ls -la" })).toBe("Bash(ls*)");
|
||||
});
|
||||
|
||||
it("should handle mixed case tool names", () => {
|
||||
expect(generatePolicyRule("edit", { filepath: "test.txt" })).toBe("Edit");
|
||||
expect(generatePolicyRule("EDIT", { filepath: "test.txt" })).toBe("Edit");
|
||||
expect(generatePolicyRule("Edit", { filepath: "test.txt" })).toBe("Edit");
|
||||
});
|
||||
});
|
||||
|
|
@ -17,16 +17,16 @@ describe("precedenceResolver", () => {
|
|||
it("should prioritize command line flags over defaults", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
commandLineFlags: {
|
||||
exclude: ["read_file"],
|
||||
allow: ["write_file"],
|
||||
exclude: ["Read"],
|
||||
allow: ["Write"],
|
||||
},
|
||||
personalSettings: false,
|
||||
useDefaults: true,
|
||||
});
|
||||
|
||||
// Command line flags should come first
|
||||
expect(policies[0]).toEqual({ tool: "read_file", permission: "exclude" });
|
||||
expect(policies[1]).toEqual({ tool: "write_file", permission: "allow" });
|
||||
expect(policies[0]).toEqual({ tool: "Read", permission: "exclude" });
|
||||
expect(policies[1]).toEqual({ tool: "Write", permission: "allow" });
|
||||
});
|
||||
|
||||
it("should handle wildcard patterns", () => {
|
||||
|
|
@ -55,9 +55,9 @@ describe("precedenceResolver", () => {
|
|||
});
|
||||
|
||||
expect(policies).toEqual([
|
||||
{ tool: "read_file", permission: "allow" },
|
||||
{ tool: "write_file", permission: "allow" },
|
||||
{ tool: "run_terminal_command", permission: "allow" },
|
||||
{ tool: "Read", permission: "allow" },
|
||||
{ tool: "Write", permission: "allow" },
|
||||
{ tool: "Bash", permission: "allow" },
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
@ -84,24 +84,24 @@ describe("precedenceResolver", () => {
|
|||
|
||||
// Order should be: exclude, ask, allow (as specified in the function)
|
||||
expect(policies).toEqual([
|
||||
{ tool: "write_file", permission: "exclude" },
|
||||
{ tool: "run_terminal_command", permission: "exclude" },
|
||||
{ tool: "list_files", permission: "ask" },
|
||||
{ tool: "search_code", permission: "ask" },
|
||||
{ tool: "read_file", permission: "allow" },
|
||||
{ tool: "fetch", permission: "allow" },
|
||||
{ tool: "Write", permission: "exclude" },
|
||||
{ tool: "Bash", permission: "exclude" },
|
||||
{ tool: "List", permission: "ask" },
|
||||
{ tool: "Search", permission: "ask" },
|
||||
{ tool: "Read", permission: "allow" },
|
||||
{ tool: "Fetch", permission: "allow" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("should apply config permissions with proper precedence", () => {
|
||||
const configPolicies: ToolPermissionPolicy[] = [
|
||||
{ tool: "write_file", permission: "allow" },
|
||||
{ tool: "read_file", permission: "ask" },
|
||||
{ tool: "Write", permission: "allow" },
|
||||
{ tool: "Read", permission: "ask" },
|
||||
];
|
||||
|
||||
const policies = resolvePermissionPrecedence({
|
||||
commandLineFlags: {
|
||||
exclude: ["read_file"], // Should override config
|
||||
exclude: ["Read"], // Should override config
|
||||
},
|
||||
configPermissions: configPolicies,
|
||||
personalSettings: false,
|
||||
|
|
@ -109,14 +109,14 @@ describe("precedenceResolver", () => {
|
|||
});
|
||||
|
||||
// CLI flag should override config
|
||||
expect(policies[0]).toEqual({ tool: "read_file", permission: "exclude" });
|
||||
expect(policies[0]).toEqual({ tool: "Read", permission: "exclude" });
|
||||
// Config policy should be present
|
||||
expect(policies[1]).toEqual({ tool: "write_file", permission: "allow" });
|
||||
expect(policies[1]).toEqual({ tool: "Write", permission: "allow" });
|
||||
});
|
||||
|
||||
it("should handle all layers with proper precedence", () => {
|
||||
const configPolicies: ToolPermissionPolicy[] = [
|
||||
{ tool: "search_code", permission: "ask" },
|
||||
{ tool: "Search", permission: "ask" },
|
||||
];
|
||||
|
||||
const policies = resolvePermissionPrecedence({
|
||||
|
|
@ -128,16 +128,16 @@ describe("precedenceResolver", () => {
|
|||
useDefaults: true,
|
||||
});
|
||||
|
||||
// Find the write_file policy - should be from CLI (allow)
|
||||
const writePolicy = policies.find(p => p.tool === "write_file");
|
||||
// Find the Write policy - should be from CLI (allow)
|
||||
const writePolicy = policies.find(p => p.tool === "Write");
|
||||
expect(writePolicy?.permission).toBe("allow");
|
||||
|
||||
// Find the search_code policy - should be from config
|
||||
const searchPolicy = policies.find(p => p.tool === "search_code");
|
||||
// Find the Search policy - should be from config
|
||||
const searchPolicy = policies.find(p => p.tool === "Search");
|
||||
expect(searchPolicy?.permission).toBe("ask");
|
||||
|
||||
// Should still have default policies
|
||||
const readPolicy = policies.find(p => p.tool === "read_file");
|
||||
const readPolicy = policies.find(p => p.tool === "Read");
|
||||
expect(readPolicy).toBeDefined();
|
||||
});
|
||||
|
||||
|
|
@ -154,21 +154,21 @@ describe("precedenceResolver", () => {
|
|||
it("should allow CLI flags to override default policies", () => {
|
||||
const policies = resolvePermissionPrecedence({
|
||||
commandLineFlags: {
|
||||
exclude: ["write_file"],
|
||||
exclude: ["Write"],
|
||||
},
|
||||
useDefaults: true,
|
||||
personalSettings: false,
|
||||
});
|
||||
|
||||
// CLI exclusion should come first
|
||||
expect(policies[0]).toEqual({ tool: "write_file", permission: "exclude" });
|
||||
expect(policies[0]).toEqual({ tool: "Write", permission: "exclude" });
|
||||
// Default policies should follow
|
||||
expect(policies.slice(1)).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
});
|
||||
|
||||
it("should allow config policies to override defaults", () => {
|
||||
const configPolicies: ToolPermissionPolicy[] = [
|
||||
{ tool: "run_terminal_command", permission: "allow" },
|
||||
{ tool: "Bash", permission: "allow" },
|
||||
];
|
||||
|
||||
const policies = resolvePermissionPrecedence({
|
||||
|
|
@ -178,7 +178,7 @@ describe("precedenceResolver", () => {
|
|||
});
|
||||
|
||||
// Config policy should override default
|
||||
expect(policies[0]).toEqual({ tool: "run_terminal_command", permission: "allow" });
|
||||
expect(policies[0]).toEqual({ tool: "Bash", permission: "allow" });
|
||||
// Default policies should follow
|
||||
expect(policies.slice(1)).toEqual(DEFAULT_TOOL_POLICIES);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,48 +1,38 @@
|
|||
/**
|
||||
* Maps common display names and variations to actual tool names
|
||||
* Maps common variations to standardized tool names
|
||||
*/
|
||||
const TOOL_NAME_MAPPINGS: Record<string, string> = {
|
||||
// Display name mappings
|
||||
Read: "read_file",
|
||||
Write: "write_file",
|
||||
List: "list_files",
|
||||
Search: "search_code",
|
||||
Bash: "run_terminal_command",
|
||||
Terminal: "run_terminal_command",
|
||||
Fetch: "fetch",
|
||||
Exit: "exit",
|
||||
Diff: "view_diff",
|
||||
Edit: "edit_file",
|
||||
Checklist: "write_checklist",
|
||||
// Standard tool names (passthrough)
|
||||
Read: "Read",
|
||||
Write: "Write",
|
||||
List: "List",
|
||||
Search: "Search",
|
||||
Bash: "Bash",
|
||||
Fetch: "Fetch",
|
||||
Exit: "Exit",
|
||||
Diff: "Diff",
|
||||
Edit: "Edit",
|
||||
Checklist: "Checklist",
|
||||
|
||||
// Common variations (case insensitive)
|
||||
read: "read_file",
|
||||
write: "write_file",
|
||||
list: "list_files",
|
||||
search: "search_code",
|
||||
bash: "run_terminal_command",
|
||||
terminal: "run_terminal_command",
|
||||
cmd: "run_terminal_command",
|
||||
command: "run_terminal_command",
|
||||
fetch: "fetch",
|
||||
exit: "exit",
|
||||
diff: "view_diff",
|
||||
edit: "edit_file",
|
||||
checklist: "write_checklist",
|
||||
|
||||
// Actual tool names (passthrough)
|
||||
read_file: "read_file",
|
||||
write_file: "write_file",
|
||||
list_files: "list_files",
|
||||
search_code: "search_code",
|
||||
run_terminal_command: "run_terminal_command",
|
||||
view_diff: "view_diff",
|
||||
edit_file: "edit_file",
|
||||
write_checklist: "write_checklist",
|
||||
read: "Read",
|
||||
write: "Write",
|
||||
list: "List",
|
||||
search: "Search",
|
||||
bash: "Bash",
|
||||
terminal: "Bash",
|
||||
Terminal: "Bash",
|
||||
cmd: "Bash",
|
||||
command: "Bash",
|
||||
fetch: "Fetch",
|
||||
exit: "Exit",
|
||||
diff: "Diff",
|
||||
edit: "Edit",
|
||||
checklist: "Checklist",
|
||||
};
|
||||
|
||||
/**
|
||||
* Normalizes a tool name from various formats (display name, variations) to the actual tool name
|
||||
* Normalizes a tool name from various formats (variations, legacy names) to the standardized tool name
|
||||
*/
|
||||
export function normalizeToolName(input: string): string {
|
||||
// First try exact match (case sensitive)
|
||||
|
|
@ -63,22 +53,11 @@ export function normalizeToolName(input: string): string {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets the display name for a normalized tool name
|
||||
* Returns the preferred display name (e.g., "read_file" -> "Read")
|
||||
* Gets the display name for a tool name
|
||||
* Since we now use display names as the primary names, this mostly returns the input
|
||||
* but handles any remaining edge cases
|
||||
*/
|
||||
export function getDisplayName(normalizedName: string): string {
|
||||
// Create a reverse mapping from normalized names to display names
|
||||
const reverseMapping: Record<string, string> = {
|
||||
read_file: "Read",
|
||||
write_file: "Write",
|
||||
list_files: "List",
|
||||
search_code: "Search",
|
||||
run_terminal_command: "Bash",
|
||||
fetch: "Fetch",
|
||||
exit: "Exit",
|
||||
view_diff: "Diff",
|
||||
write_checklist: "Checklist",
|
||||
};
|
||||
|
||||
return reverseMapping[normalizedName] || normalizedName;
|
||||
export function getDisplayName(toolName: string): string {
|
||||
// Simply normalize and return - the tool name is already the display name
|
||||
return normalizeToolName(toolName);
|
||||
}
|
||||
|
|
|
|||
81
src/permissions/yamlPermissions.test.ts
Normal file
81
src/permissions/yamlPermissions.test.ts
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import * as fs from 'fs';
|
||||
import { loadPermissionsYaml, yamlConfigToPolicies } from './permissionsYamlLoader.js';
|
||||
import { checkToolPermission } from './permissionChecker.js';
|
||||
|
||||
// Mock fs module
|
||||
vi.mock('fs');
|
||||
|
||||
describe('YAML Permissions - Edit Tool Bug', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should allow Edit tool when it is in the allow list', () => {
|
||||
// Test the exact YAML content from the user
|
||||
const yamlContent = `allow:
|
||||
- Edit
|
||||
ask: []
|
||||
exclude: []`;
|
||||
|
||||
// Parse the YAML config
|
||||
const config = {
|
||||
allow: ['Edit'],
|
||||
ask: [],
|
||||
exclude: []
|
||||
};
|
||||
|
||||
// Convert to policies
|
||||
const policies = yamlConfigToPolicies(config);
|
||||
console.log('Converted policies:', policies);
|
||||
|
||||
// The policies should have Edit as allowed
|
||||
expect(policies).toEqual([
|
||||
{ tool: 'Edit', permission: 'allow' }
|
||||
]);
|
||||
|
||||
// Test permission check with tool call named "Edit"
|
||||
const toolCall1 = {
|
||||
name: 'Edit',
|
||||
arguments: { filepath: 'test.txt' }
|
||||
};
|
||||
const result1 = checkToolPermission(toolCall1, { policies });
|
||||
console.log('Permission check for "Edit":', result1);
|
||||
expect(result1.permission).toBe('allow');
|
||||
|
||||
// Legacy names should NOT work anymore
|
||||
const toolCall2 = {
|
||||
name: 'search_and_replace_in_file',
|
||||
arguments: { filepath: 'test.txt' }
|
||||
};
|
||||
const result2 = checkToolPermission(toolCall2, { policies });
|
||||
console.log('Permission check for "search_and_replace_in_file":', result2);
|
||||
expect(result2.permission).toBe('ask'); // Should not match, falls back to ask
|
||||
|
||||
// Legacy names should NOT work anymore
|
||||
const toolCall3 = {
|
||||
name: 'edit_file',
|
||||
arguments: { filepath: 'test.txt' }
|
||||
};
|
||||
const result3 = checkToolPermission(toolCall3, { policies });
|
||||
console.log('Permission check for "edit_file":', result3);
|
||||
expect(result3.permission).toBe('ask'); // Should not match, falls back to ask
|
||||
});
|
||||
|
||||
it('should check what normalizeToolName returns for Edit variations', async () => {
|
||||
const { normalizeToolName } = await import('./toolNameMapping.js');
|
||||
|
||||
console.log('normalizeToolName("Edit"):', normalizeToolName("Edit"));
|
||||
console.log('normalizeToolName("edit"):', normalizeToolName("edit"));
|
||||
console.log('normalizeToolName("edit_file"):', normalizeToolName("edit_file"));
|
||||
console.log('normalizeToolName("search_and_replace_in_file"):', normalizeToolName("search_and_replace_in_file"));
|
||||
|
||||
// Only proper names and case variations should normalize
|
||||
expect(normalizeToolName("Edit")).toBe("Edit");
|
||||
expect(normalizeToolName("edit")).toBe("Edit");
|
||||
|
||||
// Legacy snake_case names should not normalize anymore
|
||||
expect(normalizeToolName("edit_file")).toBe("edit_file");
|
||||
expect(normalizeToolName("search_and_replace_in_file")).toBe("search_and_replace_in_file");
|
||||
});
|
||||
});
|
||||
|
|
@ -235,7 +235,7 @@ describe('ModeService', () => {
|
|||
describe('Integration with ToolPermissionService', () => {
|
||||
beforeEach(async () => {
|
||||
await modeService.initialize({
|
||||
allow: ['read_file'],
|
||||
allow: ['Read'],
|
||||
exclude: ['dangerous_tool']
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -24,14 +24,13 @@ describe("ToolPermissionService - Debug Tool Permissions", () => {
|
|||
// console.log(`${index + 1}. tool: "${policy.tool}", permission: "${policy.permission}"`);
|
||||
// });
|
||||
|
||||
const writeFilePolicy = policies.find((p) => p.tool === "write_file");
|
||||
const writePolicy = policies.find((p) => p.tool === "Write");
|
||||
const wildcardPolicy = policies.find((p) => p.tool === "*");
|
||||
|
||||
if (mode === "plan") {
|
||||
// Plan mode should explicitly exclude write_file
|
||||
expect(writeFilePolicy?.permission).toBe("exclude");
|
||||
console.log(`✓ write_file tool correctly excluded in plan mode`);
|
||||
// Plan mode should explicitly exclude Write
|
||||
expect(writePolicy?.permission).toBe("exclude");
|
||||
console.log(`✓ Write tool correctly excluded in plan mode`);
|
||||
} else if (mode === "auto") {
|
||||
// Auto mode should allow everything
|
||||
expect(wildcardPolicy?.permission).toBe("allow");
|
||||
|
|
@ -39,8 +38,8 @@ describe("ToolPermissionService - Debug Tool Permissions", () => {
|
|||
} else {
|
||||
// Normal mode behavior depends on user config
|
||||
console.log(
|
||||
`Normal mode - write_file policy: ${
|
||||
writeFilePolicy?.permission || "none"
|
||||
`Normal mode - Write policy: ${
|
||||
writePolicy?.permission || "none"
|
||||
}`
|
||||
);
|
||||
}
|
||||
|
|
@ -49,9 +48,9 @@ describe("ToolPermissionService - Debug Tool Permissions", () => {
|
|||
it(`should check tool resolution order in ${mode} mode`, () => {
|
||||
const policies = service.getPermissions().policies;
|
||||
|
||||
// Find all policies that could affect write_file tool
|
||||
// Find all policies that could affect Write tool
|
||||
const writeRelevantPolicies = policies.filter(
|
||||
(p) => p.tool === "write_file" || p.tool === "Write" || p.tool === "*"
|
||||
(p) => p.tool === "Write" || p.tool === "*"
|
||||
);
|
||||
|
||||
console.log(
|
||||
|
|
@ -66,8 +65,8 @@ describe("ToolPermissionService - Debug Tool Permissions", () => {
|
|||
});
|
||||
|
||||
if (mode === "plan") {
|
||||
// Should have write_file exclude policy BEFORE any wildcard
|
||||
const writeIndex = policies.findIndex((p) => p.tool === "write_file");
|
||||
// Should have Write exclude policy BEFORE any wildcard
|
||||
const writeIndex = policies.findIndex((p) => p.tool === "Write");
|
||||
const wildcardIndex = policies.findIndex((p) => p.tool === "*");
|
||||
|
||||
expect(writeIndex).toBeGreaterThanOrEqual(0);
|
||||
|
|
@ -110,7 +109,7 @@ describe("ToolPermissionService - Debug Tool Permissions", () => {
|
|||
);
|
||||
});
|
||||
|
||||
const writeFilePolicy = policies.find((p) => p.tool === "write_file");
|
||||
expect(writeFilePolicy?.permission).toBe("exclude");
|
||||
const writePolicy = policies.find((p) => p.tool === "Write");
|
||||
expect(writePolicy?.permission).toBe("exclude");
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -63,8 +63,8 @@ describe('ToolPermissionService - Mode Functionality', () => {
|
|||
|
||||
// Plan mode should have write exclusions and read allowances
|
||||
const planPolicies = planState.permissions.policies;
|
||||
expect(planPolicies.some(p => p.tool === 'write_file' && p.permission === 'exclude')).toBe(true);
|
||||
expect(planPolicies.some(p => p.tool === 'read_file' && p.permission === 'allow')).toBe(true);
|
||||
expect(planPolicies.some(p => p.tool === 'Write' && p.permission === 'exclude')).toBe(true);
|
||||
expect(planPolicies.some(p => p.tool === 'Read' && p.permission === 'allow')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -90,7 +90,7 @@ describe('ToolPermissionService - Mode Functionality', () => {
|
|||
const planPolicies = service.getPermissions().policies;
|
||||
|
||||
expect(planPolicies).not.toEqual(normalPolicies);
|
||||
expect(planPolicies.some(p => p.tool === 'write_file' && p.permission === 'exclude')).toBe(true);
|
||||
expect(planPolicies.some(p => p.tool === 'Write' && p.permission === 'exclude')).toBe(true);
|
||||
});
|
||||
|
||||
it('should use absolute override for plan mode (ignore non-mode policies)', () => {
|
||||
|
|
@ -107,9 +107,9 @@ describe('ToolPermissionService - Mode Functionality', () => {
|
|||
const newPolicies = service.getPermissions().policies;
|
||||
|
||||
// Plan mode should use ONLY mode policies (absolute override)
|
||||
// Should contain write_file exclude policy for plan mode
|
||||
const writeFilePolicy = newPolicies.find(p => p.tool === 'write_file');
|
||||
expect(writeFilePolicy?.permission).toBe('exclude');
|
||||
// Should contain Write exclude policy for plan mode
|
||||
const writePolicy = newPolicies.find(p => p.tool === 'Write');
|
||||
expect(writePolicy?.permission).toBe('exclude');
|
||||
|
||||
// Should not contain any user-defined policies from the initial setup
|
||||
const userAllowPolicy = newPolicies.find(p => p.tool === 'allowedTool');
|
||||
|
|
|
|||
|
|
@ -14,63 +14,63 @@ describe('ToolPermissionService - Real Tool Permission Test', () => {
|
|||
service.initializeSync({ mode: 'plan' });
|
||||
});
|
||||
|
||||
it('should deny write_file tool in plan mode', () => {
|
||||
it('should deny Write tool in plan mode', () => {
|
||||
const permissions = service.getPermissions();
|
||||
const toolCall = {
|
||||
name: 'write_file',
|
||||
name: 'Write',
|
||||
arguments: { path: 'test.txt', content: 'test' }
|
||||
};
|
||||
const result = checkToolPermission(toolCall, permissions);
|
||||
|
||||
console.log(`write_file permission check result:`, result);
|
||||
console.log(`Write permission check result:`, result);
|
||||
expect(result.permission).toBe('exclude');
|
||||
});
|
||||
|
||||
it('should deny search_and_replace_in_file tool in plan mode', () => {
|
||||
it('should deny Edit tool in plan mode', () => {
|
||||
const permissions = service.getPermissions();
|
||||
const toolCall = {
|
||||
name: 'search_and_replace_in_file',
|
||||
name: 'Edit',
|
||||
arguments: { path: 'test.txt', old_str: 'old', new_str: 'new' }
|
||||
};
|
||||
const result = checkToolPermission(toolCall, permissions);
|
||||
|
||||
console.log(`search_and_replace_in_file permission check result:`, result);
|
||||
console.log(`Edit permission check result:`, result);
|
||||
expect(result.permission).toBe('exclude');
|
||||
});
|
||||
|
||||
it('should deny run_terminal_command tool in plan mode', () => {
|
||||
it('should deny Bash tool in plan mode', () => {
|
||||
const permissions = service.getPermissions();
|
||||
const toolCall = {
|
||||
name: 'run_terminal_command',
|
||||
name: 'Bash',
|
||||
arguments: { command: 'ls' }
|
||||
};
|
||||
const result = checkToolPermission(toolCall, permissions);
|
||||
|
||||
console.log(`run_terminal_command permission check result:`, result);
|
||||
console.log(`Bash permission check result:`, result);
|
||||
expect(result.permission).toBe('exclude');
|
||||
});
|
||||
|
||||
it('should allow read_file tool in plan mode', () => {
|
||||
it('should allow Read tool in plan mode', () => {
|
||||
const permissions = service.getPermissions();
|
||||
const toolCall = {
|
||||
name: 'read_file',
|
||||
name: 'Read',
|
||||
arguments: { path: 'test.txt' }
|
||||
};
|
||||
const result = checkToolPermission(toolCall, permissions);
|
||||
|
||||
console.log(`read_file permission check result:`, result);
|
||||
console.log(`Read permission check result:`, result);
|
||||
expect(result.permission).toBe('allow');
|
||||
});
|
||||
|
||||
it('should allow list_files tool in plan mode', () => {
|
||||
it('should allow List tool in plan mode', () => {
|
||||
const permissions = service.getPermissions();
|
||||
const toolCall = {
|
||||
name: 'list_files',
|
||||
name: 'List',
|
||||
arguments: { path: '.' }
|
||||
};
|
||||
const result = checkToolPermission(toolCall, permissions);
|
||||
|
||||
console.log(`list_files permission check result:`, result);
|
||||
console.log(`List permission check result:`, result);
|
||||
expect(result.permission).toBe('allow');
|
||||
});
|
||||
|
||||
|
|
@ -92,27 +92,27 @@ describe('ToolPermissionService - Real Tool Permission Test', () => {
|
|||
service.initializeSync({ mode: 'auto' });
|
||||
});
|
||||
|
||||
it('should allow write_file tool in auto mode', () => {
|
||||
it('should allow Write tool in auto mode', () => {
|
||||
const permissions = service.getPermissions();
|
||||
const toolCall = {
|
||||
name: 'write_file',
|
||||
name: 'Write',
|
||||
arguments: { path: 'test.txt', content: 'test' }
|
||||
};
|
||||
const result = checkToolPermission(toolCall, permissions);
|
||||
|
||||
console.log(`Auto mode write_file permission check result:`, result);
|
||||
console.log(`Auto mode Write permission check result:`, result);
|
||||
expect(result.permission).toBe('allow');
|
||||
});
|
||||
|
||||
it('should allow run_terminal_command tool in auto mode', () => {
|
||||
it('should allow Bash tool in auto mode', () => {
|
||||
const permissions = service.getPermissions();
|
||||
const toolCall = {
|
||||
name: 'run_terminal_command',
|
||||
name: 'Bash',
|
||||
arguments: { command: 'ls' }
|
||||
};
|
||||
const result = checkToolPermission(toolCall, permissions);
|
||||
|
||||
console.log(`Auto mode run_terminal_command permission check result:`, result);
|
||||
console.log(`Auto mode Bash permission check result:`, result);
|
||||
expect(result.permission).toBe('allow');
|
||||
});
|
||||
|
||||
|
|
@ -131,20 +131,20 @@ describe('ToolPermissionService - Real Tool Permission Test', () => {
|
|||
|
||||
describe('Mode Override Test', () => {
|
||||
it('should override user permissions when switching to plan mode', () => {
|
||||
// Start with user explicitly allowing write_file
|
||||
// Start with user explicitly allowing Write
|
||||
service.initializeSync({
|
||||
allow: ['write_file'],
|
||||
allow: ['Write'],
|
||||
mode: 'normal'
|
||||
});
|
||||
|
||||
// Verify write_file is allowed in normal mode
|
||||
// Verify Write is allowed in normal mode
|
||||
let permissions = service.getPermissions();
|
||||
const toolCall = {
|
||||
name: 'write_file',
|
||||
name: 'Write',
|
||||
arguments: { path: 'test.txt', content: 'test' }
|
||||
};
|
||||
let result = checkToolPermission(toolCall, permissions);
|
||||
console.log(`Normal mode with user allow - write_file result:`, result);
|
||||
console.log(`Normal mode with user allow - Write result:`, result);
|
||||
expect(result.permission).toBe('allow');
|
||||
|
||||
// Switch to plan mode - should OVERRIDE user config completely
|
||||
|
|
|
|||
|
|
@ -37,26 +37,18 @@ export class ToolPermissionService extends BaseService<ToolPermissionServiceStat
|
|||
case "plan":
|
||||
// Plan mode: Complete override - exclude all write operations, allow only reads
|
||||
return [
|
||||
// Exclude all write tools with absolute priority (using actual tool names)
|
||||
{ tool: "write_file", permission: "exclude" },
|
||||
{ tool: "edit_file", permission: "exclude" },
|
||||
{ tool: "write_checklist", permission: "allow" },
|
||||
{ tool: "search_and_replace_in_file", permission: "exclude" },
|
||||
{ tool: "run_terminal_command", permission: "exclude" },
|
||||
// Also exclude capitalized versions in case they exist
|
||||
// Exclude all write tools with absolute priority
|
||||
{ tool: "Write", permission: "exclude" },
|
||||
{ tool: "Edit", permission: "exclude" },
|
||||
{ tool: "MultiEdit", permission: "exclude" },
|
||||
{ tool: "NotebookEdit", permission: "exclude" },
|
||||
{ tool: "Bash", permission: "exclude" },
|
||||
// Allow all read tools (using actual tool names)
|
||||
{ tool: "read_file", permission: "allow" },
|
||||
{ tool: "list_files", permission: "allow" },
|
||||
{ tool: "search_code", permission: "allow" },
|
||||
{ tool: "fetch", permission: "allow" },
|
||||
{ tool: "view_diff", permission: "allow" },
|
||||
// Also allow capitalized versions in case they exist
|
||||
// Allow all read tools
|
||||
{ tool: "Read", permission: "allow" },
|
||||
{ tool: "List", permission: "allow" },
|
||||
{ tool: "Search", permission: "allow" },
|
||||
{ tool: "Fetch", permission: "allow" },
|
||||
{ tool: "Diff", permission: "allow" },
|
||||
{ tool: "Checklist", permission: "allow" },
|
||||
{ tool: "NotebookRead", permission: "allow" },
|
||||
{ tool: "LS", permission: "allow" },
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@ describe("processStreamingResponse - content preservation", () => {
|
|||
it("content after tool calls is preserved", async () => {
|
||||
chunks = [
|
||||
contentChunk(""),
|
||||
toolCallChunk("call_123", "read_file", '{"filepath": "/README.md"}'),
|
||||
toolCallChunk("call_123", "Read", '{"filepath": "/README.md"}'),
|
||||
contentChunk(""),
|
||||
contentChunk("I'll read the README file for you."),
|
||||
];
|
||||
|
|
@ -180,7 +180,7 @@ describe("processStreamingResponse - content preservation", () => {
|
|||
it("handles the exact problematic sequence from the logs", async () => {
|
||||
chunks = [
|
||||
contentChunk(""),
|
||||
toolCallChunk("toolu_vrtx_01Jj9m9VbtwVZwcU6A311g6A", "read_file", ""),
|
||||
toolCallChunk("toolu_vrtx_01Jj9m9VbtwVZwcU6A311g6A", "Read", ""),
|
||||
toolCallChunk(
|
||||
"toolu_vrtx_01Jj9m9VbtwVZwcU6A311g6A",
|
||||
undefined,
|
||||
|
|
@ -225,7 +225,7 @@ describe("processStreamingResponse - content preservation", () => {
|
|||
expect(result.finalContent).toBe("I'll read the README file for you.");
|
||||
|
||||
// Verify the tool call was assembled correctly
|
||||
expect(result.toolCalls[0].name).toBe("read_file");
|
||||
expect(result.toolCalls[0].name).toBe("Read");
|
||||
expect(result.toolCalls[0].argumentsStr).toBe(
|
||||
'{"filepath": "/Users/nate/gh/continuedev/cli/README.md"}'
|
||||
);
|
||||
|
|
@ -255,7 +255,7 @@ describe("processStreamingResponse - content preservation", () => {
|
|||
toolCallChunkWithIndex(
|
||||
0,
|
||||
"toolu_vrtx_01ULx6UjdJ7B7bjf5WPHTXGz",
|
||||
"read_file",
|
||||
"Read",
|
||||
""
|
||||
),
|
||||
// Subsequent chunks only have index, no ID
|
||||
|
|
@ -286,7 +286,7 @@ describe("processStreamingResponse - content preservation", () => {
|
|||
);
|
||||
|
||||
// Tool call arguments are preserved using index mapping
|
||||
expect(result.toolCalls[0].name).toBe("read_file");
|
||||
expect(result.toolCalls[0].name).toBe("Read");
|
||||
expect(result.toolCalls[0].argumentsStr).toBe(
|
||||
'{"filepath": "/Users/nate/gh/continuedev/cli/README.md"}'
|
||||
);
|
||||
|
|
@ -300,7 +300,7 @@ describe("processStreamingResponse - content preservation", () => {
|
|||
toolCallChunkWithIndex(
|
||||
0,
|
||||
"toolu_vrtx_01ULx6UjdJ7B7bjf5WPHTXGz",
|
||||
"read_file",
|
||||
"Read",
|
||||
""
|
||||
),
|
||||
// Subsequent chunks fragment the JSON arguments
|
||||
|
|
@ -331,7 +331,7 @@ describe("processStreamingResponse - content preservation", () => {
|
|||
);
|
||||
|
||||
// 2. Tool call arguments are assembled correctly
|
||||
expect(result.toolCalls[0].name).toBe("read_file");
|
||||
expect(result.toolCalls[0].name).toBe("Read");
|
||||
expect(result.toolCalls[0].argumentsStr).toBe(
|
||||
'{"filepath": "/Users/nate/gh/continuedev/cli/README.md"}'
|
||||
);
|
||||
|
|
@ -353,7 +353,7 @@ describe.skip("preprocessStreamedToolCalls", () => {
|
|||
const toolCalls: ToolCall[] = [
|
||||
{
|
||||
id: "call_123",
|
||||
name: "read_file",
|
||||
name: "Read",
|
||||
arguments: { filepath: "/path/to/file.txt" },
|
||||
argumentsStr: '{"filepath": "/path/to/file.txt"}',
|
||||
startNotified: false,
|
||||
|
|
@ -371,7 +371,7 @@ describe.skip("preprocessStreamedToolCalls", () => {
|
|||
// Should have one preprocessed call and no errors
|
||||
expect(preprocessedCalls).toHaveLength(1);
|
||||
expect(errorChatEntries).toHaveLength(0);
|
||||
expect(preprocessedCalls[0].name).toBe("read_file");
|
||||
expect(preprocessedCalls[0].name).toBe("Read");
|
||||
expect(preprocessedCalls[0].tool).toBeDefined();
|
||||
expect(preprocessedCalls[0].preprocessResult).toBeDefined();
|
||||
|
||||
|
|
@ -392,7 +392,7 @@ describe.skip("preprocessStreamedToolCalls", () => {
|
|||
const toolCalls: ToolCall[] = [
|
||||
{
|
||||
id: "call_456",
|
||||
name: "read_file",
|
||||
name: "Read",
|
||||
arguments: {}, // Missing required filepath
|
||||
argumentsStr: "{}",
|
||||
startNotified: false,
|
||||
|
|
@ -413,10 +413,10 @@ describe.skip("preprocessStreamedToolCalls", () => {
|
|||
expect(errorChatEntries[0].content).toContain("Missing required argument");
|
||||
|
||||
// Callbacks should be called for errors
|
||||
expect(callbacks.onToolStart).toHaveBeenCalledWith("read_file", {});
|
||||
expect(callbacks.onToolStart).toHaveBeenCalledWith("Read", {});
|
||||
expect(callbacks.onToolError).toHaveBeenCalledWith(
|
||||
expect.stringContaining("Missing required argument"),
|
||||
"read_file"
|
||||
"Read"
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -468,7 +468,7 @@ describe.skip("executeStreamedToolCalls", () => {
|
|||
const preprocessedCalls: PreprocessedToolCall[] = [
|
||||
{
|
||||
id: "call_123",
|
||||
name: "read_file",
|
||||
name: "Read",
|
||||
arguments: { filepath: "/test.txt" },
|
||||
argumentsStr: '{"filepath": "/test.txt"}',
|
||||
startNotified: false,
|
||||
|
|
@ -495,7 +495,7 @@ describe.skip("executeStreamedToolCalls", () => {
|
|||
});
|
||||
expect(callbacks.onToolResult).toHaveBeenCalledWith(
|
||||
"Tool execution successful",
|
||||
"read_file"
|
||||
"Read"
|
||||
);
|
||||
expect(mockedExecuteToolCall).toHaveBeenCalledWith(preprocessedCalls[0]);
|
||||
});
|
||||
|
|
@ -520,7 +520,7 @@ describe.skip("executeStreamedToolCalls", () => {
|
|||
callback({
|
||||
requestId: "req_123",
|
||||
toolCall: {
|
||||
name: "write_file",
|
||||
name: "Write",
|
||||
arguments: {
|
||||
filepath: "/test.txt",
|
||||
content: "data",
|
||||
|
|
@ -536,7 +536,7 @@ describe.skip("executeStreamedToolCalls", () => {
|
|||
const preprocessedCalls: PreprocessedToolCall[] = [
|
||||
{
|
||||
id: "call_456",
|
||||
name: "write_file",
|
||||
name: "Write",
|
||||
arguments: { filepath: "/test.txt", content: "data" },
|
||||
argumentsStr: '{"filepath": "/test.txt", "content": "data"}',
|
||||
startNotified: false,
|
||||
|
|
@ -544,7 +544,7 @@ describe.skip("executeStreamedToolCalls", () => {
|
|||
},
|
||||
{
|
||||
id: "call_457",
|
||||
name: "write_file",
|
||||
name: "Write",
|
||||
arguments: { filepath: "/test.txt", content: "data" },
|
||||
argumentsStr: '{"filepath": "/test.txt", "content": "data"}',
|
||||
startNotified: false,
|
||||
|
|
@ -570,16 +570,16 @@ describe.skip("executeStreamedToolCalls", () => {
|
|||
expect(chatHistoryEntries[1].content).toBe(
|
||||
"Cancelled due to previous tool rejection"
|
||||
);
|
||||
expect(callbacks.onToolStart).toHaveBeenCalledWith("write_file", {
|
||||
expect(callbacks.onToolStart).toHaveBeenCalledWith("Write", {
|
||||
filepath: "/test.txt",
|
||||
content: "data",
|
||||
});
|
||||
expect(callbacks.onToolResult).toHaveBeenCalledWith(
|
||||
"Permission denied by user",
|
||||
"write_file"
|
||||
"Write"
|
||||
);
|
||||
expect(callbacks.onToolPermissionRequest).toHaveBeenCalledWith(
|
||||
"write_file",
|
||||
"Write",
|
||||
{ filepath: "/test.txt", content: "data" },
|
||||
"req_123",
|
||||
undefined
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { Tool } from "./types.js";
|
||||
|
||||
export const exitTool: Tool = {
|
||||
name: "exit",
|
||||
name: "Exit",
|
||||
displayName: "Exit",
|
||||
description:
|
||||
"Exit the current process with status code 1, indicating a failure or error",
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ turndownService.addRule("pre", {
|
|||
});
|
||||
|
||||
export const fetchTool: Tool = {
|
||||
name: "fetch",
|
||||
name: "Fetch",
|
||||
displayName: "Fetch",
|
||||
description: "Fetches content from a URL and converts it to markdown format",
|
||||
parameters: {
|
||||
|
|
|
|||
|
|
@ -13,29 +13,29 @@ describe("formatToolCall", () => {
|
|||
});
|
||||
|
||||
it("should format tool name without arguments", () => {
|
||||
expect(formatToolCall("write_file")).toBe("Write");
|
||||
expect(formatToolCall("read_file", {})).toBe("Read");
|
||||
expect(formatToolCall("Write")).toBe("Write");
|
||||
expect(formatToolCall("Read", {})).toBe("Read");
|
||||
expect(formatToolCall("non_existent_tool", null)).toBe("non_existent_tool");
|
||||
});
|
||||
|
||||
it("should format tool name with relative path argument", () => {
|
||||
expect(formatToolCall("write_file", { filepath: "README.md" })).toBe(
|
||||
expect(formatToolCall("Write", { filepath: "README.md" })).toBe(
|
||||
"Write(README.md)"
|
||||
);
|
||||
expect(formatToolCall("read_file", { filepath: "src/index.ts" })).toBe(
|
||||
expect(formatToolCall("Read", { filepath: "src/index.ts" })).toBe(
|
||||
"Read(src/index.ts)"
|
||||
);
|
||||
});
|
||||
|
||||
it("should convert absolute paths to relative paths", () => {
|
||||
expect(
|
||||
formatToolCall("write_file", {
|
||||
formatToolCall("Write", {
|
||||
filepath: "/Users/test/project/README.md",
|
||||
})
|
||||
).toBe("Write(README.md)");
|
||||
|
||||
expect(
|
||||
formatToolCall("read_file", {
|
||||
formatToolCall("Read", {
|
||||
filepath: "/Users/test/project/src/components/App.tsx",
|
||||
})
|
||||
).toBe("Read(src/components/App.tsx)");
|
||||
|
|
@ -43,20 +43,20 @@ describe("formatToolCall", () => {
|
|||
|
||||
it("should handle absolute paths outside the project", () => {
|
||||
expect(
|
||||
formatToolCall("write_file", { filepath: "/Users/other/file.txt" })
|
||||
formatToolCall("Write", { filepath: "/Users/other/file.txt" })
|
||||
).toBe("Write(../../other/file.txt)");
|
||||
});
|
||||
|
||||
it("should handle non-path arguments", () => {
|
||||
expect(formatToolCall("search_code", { pattern: "TODO" })).toBe(
|
||||
expect(formatToolCall("Search", { pattern: "TODO" })).toBe(
|
||||
"Search(TODO)"
|
||||
);
|
||||
expect(formatToolCall("search_code", { pattern: 123 })).toBe("Search(123)");
|
||||
expect(formatToolCall("Search", { pattern: 123 })).toBe("Search(123)");
|
||||
});
|
||||
|
||||
it("should use first argument when multiple are provided", () => {
|
||||
expect(
|
||||
formatToolCall("write_file", {
|
||||
formatToolCall("Write", {
|
||||
filepath: "test.txt",
|
||||
content: "Hello world",
|
||||
})
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ import { getToolDisplayName, extractToolCalls } from "./index.js";
|
|||
describe("tools/index utilities", () => {
|
||||
describe("getToolDisplayName", () => {
|
||||
it("should return display name for known tool", () => {
|
||||
// The actual implementation returns the first part of the displayName
|
||||
const result = getToolDisplayName("read_file");
|
||||
// The actual implementation returns the displayName when tool is found
|
||||
const result = getToolDisplayName("Read");
|
||||
expect(result).toBe("Read");
|
||||
});
|
||||
|
||||
|
|
@ -22,13 +22,13 @@ describe("tools/index utilities", () => {
|
|||
describe("extractToolCalls", () => {
|
||||
it("should extract single tool call", () => {
|
||||
const response = `Here's the result:
|
||||
<tool>{"name": "read_file", "arguments": {"filepath": "/path/to/file.txt"}}</tool>
|
||||
<tool>{"name": "Read", "arguments": {"filepath": "/path/to/file.txt"}}</tool>
|
||||
Done.`;
|
||||
|
||||
const result = extractToolCalls(response);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
name: "read_file",
|
||||
name: "Read",
|
||||
arguments: { filepath: "/path/to/file.txt" },
|
||||
},
|
||||
]);
|
||||
|
|
@ -36,31 +36,31 @@ Done.`;
|
|||
|
||||
it("should extract multiple tool calls", () => {
|
||||
const response = `First call:
|
||||
<tool>{"name": "read_file", "arguments": {"filepath": "/file1.txt"}}</tool>
|
||||
<tool>{"name": "Read", "arguments": {"filepath": "/file1.txt"}}</tool>
|
||||
Second call:
|
||||
<tool>{"name": "write_file", "arguments": {"filepath": "/file2.txt", "content": "hello"}}</tool>
|
||||
<tool>{"name": "Write", "arguments": {"filepath": "/file2.txt", "content": "hello"}}</tool>
|
||||
Done.`;
|
||||
|
||||
const result = extractToolCalls(response);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
name: "read_file",
|
||||
name: "Read",
|
||||
arguments: { filepath: "/file1.txt" },
|
||||
},
|
||||
{
|
||||
name: "write_file",
|
||||
name: "Write",
|
||||
arguments: { filepath: "/file2.txt", content: "hello" },
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("should handle tool calls with complex arguments", () => {
|
||||
const response = `<tool>{"name": "search_code", "arguments": {"pattern": "function.*test", "path": "/src", "options": {"recursive": true, "ignoreCase": false}}}</tool>`;
|
||||
const response = `<tool>{"name": "Search", "arguments": {"pattern": "function.*test", "path": "/src", "options": {"recursive": true, "ignoreCase": false}}}</tool>`;
|
||||
|
||||
const result = extractToolCalls(response);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
name: "search_code",
|
||||
name: "Search",
|
||||
arguments: {
|
||||
pattern: "function.*test",
|
||||
path: "/src",
|
||||
|
|
@ -72,7 +72,7 @@ Done.`;
|
|||
|
||||
it("should handle multiline tool calls", () => {
|
||||
const response = `<tool>{
|
||||
"name": "write_file",
|
||||
"name": "Write",
|
||||
"arguments": {
|
||||
"filepath": "/path/to/file.txt",
|
||||
"content": "Line 1\\nLine 2\\nLine 3"
|
||||
|
|
@ -82,7 +82,7 @@ Done.`;
|
|||
const result = extractToolCalls(response);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
name: "write_file",
|
||||
name: "Write",
|
||||
arguments: {
|
||||
filepath: "/path/to/file.txt",
|
||||
content: "Line 1\nLine 2\nLine 3",
|
||||
|
|
@ -111,20 +111,20 @@ Done.`;
|
|||
console.error = () => {};
|
||||
|
||||
const response = `Good call:
|
||||
<tool>{"name": "read_file", "arguments": {"filepath": "/file.txt"}}</tool>
|
||||
<tool>{"name": "Read", "arguments": {"filepath": "/file.txt"}}</tool>
|
||||
Bad call:
|
||||
<tool>{"name": "invalid_json", "arguments": {malformed json}}</tool>
|
||||
Another good call:
|
||||
<tool>{"name": "write_file", "arguments": {"filepath": "/file2.txt", "content": "test"}}</tool>`;
|
||||
<tool>{"name": "Write", "arguments": {"filepath": "/file2.txt", "content": "test"}}</tool>`;
|
||||
|
||||
const result = extractToolCalls(response);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
name: "read_file",
|
||||
name: "Read",
|
||||
arguments: { filepath: "/file.txt" },
|
||||
},
|
||||
{
|
||||
name: "write_file",
|
||||
name: "Write",
|
||||
arguments: { filepath: "/file2.txt", content: "test" },
|
||||
},
|
||||
]);
|
||||
|
|
@ -137,26 +137,26 @@ Another good call:
|
|||
const response = `Missing name:
|
||||
<tool>{"arguments": {"filepath": "/file.txt"}}</tool>
|
||||
Missing arguments:
|
||||
<tool>{"name": "read_file"}</tool>
|
||||
<tool>{"name": "Read"}</tool>
|
||||
Valid call:
|
||||
<tool>{"name": "read_file", "arguments": {"filepath": "/file.txt"}}</tool>`;
|
||||
<tool>{"name": "Read", "arguments": {"filepath": "/file.txt"}}</tool>`;
|
||||
|
||||
const result = extractToolCalls(response);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
name: "read_file",
|
||||
name: "Read",
|
||||
arguments: { filepath: "/file.txt" },
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("should handle tool calls with empty arguments", () => {
|
||||
const response = `<tool>{"name": "list_files", "arguments": {}}</tool>`;
|
||||
const response = `<tool>{"name": "List", "arguments": {}}</tool>`;
|
||||
|
||||
const result = extractToolCalls(response);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
name: "list_files",
|
||||
name: "List",
|
||||
arguments: {},
|
||||
},
|
||||
]);
|
||||
|
|
@ -170,12 +170,12 @@ Valid call:
|
|||
});
|
||||
|
||||
it("should handle nested tool tags", () => {
|
||||
const response = `<tool>{"name": "write_file", "arguments": {"filepath": "/file.txt", "content": "Some content with <tool> tags inside"}}</tool>`;
|
||||
const response = `<tool>{"name": "Write", "arguments": {"filepath": "/file.txt", "content": "Some content with <tool> tags inside"}}</tool>`;
|
||||
|
||||
const result = extractToolCalls(response);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
name: "write_file",
|
||||
name: "Write",
|
||||
arguments: {
|
||||
filepath: "/file.txt",
|
||||
content: "Some content with <tool> tags inside",
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { formatToolArgument } from "./formatters.js";
|
|||
|
||||
// List files in a directory
|
||||
export const listFilesTool: Tool = {
|
||||
name: "list_files",
|
||||
name: "List",
|
||||
displayName: "List",
|
||||
description: "List files in a directory",
|
||||
parameters: {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { Tool } from "./types.js";
|
|||
import { formatToolArgument } from "./formatters.js";
|
||||
|
||||
export const readFileTool: Tool = {
|
||||
name: "read_file",
|
||||
name: "Read",
|
||||
displayName: "Read",
|
||||
description: "Read the contents of a file at the specified path",
|
||||
parameters: {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import {
|
|||
import { Tool } from "./types.js";
|
||||
|
||||
export const runTerminalCommandTool: Tool = {
|
||||
name: "run_terminal_command",
|
||||
name: "Bash",
|
||||
displayName: "Bash",
|
||||
description: "Executes a terminal command and returns the output",
|
||||
parameters: {
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ export const NO_PARALLEL_TOOL_CALLING_INSRUCTION =
|
|||
* Our starting point is heavily inspired by Cline's `replace_in_file` tool: https://github.com/cline/cline/blob/2709ccefcddc616e89a70962f017bcbbca1f17bf/src/core/prompts/system.ts#L87-L121
|
||||
*/
|
||||
export const searchAndReplaceInFileTool: Tool = {
|
||||
name: "edit_file",
|
||||
name: "Edit",
|
||||
displayName: "Edit",
|
||||
readonly: false,
|
||||
isBuiltIn: true,
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ const execPromise = util.promisify(child_process.exec);
|
|||
const DEFAULT_MAX_RESULTS = 100;
|
||||
|
||||
export const searchCodeTool: Tool = {
|
||||
name: "search_code",
|
||||
name: "Search",
|
||||
displayName: "Search",
|
||||
description: "Search the codebase using ripgrep (rg) for a specific pattern",
|
||||
parameters: {
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import { Tool } from "./types.js";
|
|||
const execPromise = util.promisify(child_process.exec);
|
||||
|
||||
export const viewDiffTool: Tool = {
|
||||
name: "view_diff",
|
||||
name: "Diff",
|
||||
displayName: "Diff",
|
||||
description: "View all uncommitted changes in the git repository",
|
||||
parameters: {
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ describe("writeChecklistTool", () => {
|
|||
});
|
||||
|
||||
it("should have correct tool properties", () => {
|
||||
expect(writeChecklistTool.name).toBe("write_checklist");
|
||||
expect(writeChecklistTool.name).toBe("Checklist");
|
||||
expect(writeChecklistTool.displayName).toBe("Checklist");
|
||||
expect(writeChecklistTool.readonly).toBe(false);
|
||||
expect(writeChecklistTool.isBuiltIn).toBe(true);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import type { Tool } from "./types.js";
|
||||
|
||||
export const writeChecklistTool: Tool = {
|
||||
name: "write_checklist",
|
||||
name: "Checklist",
|
||||
displayName: "Checklist",
|
||||
description:
|
||||
"Create or update a task checklist. The old checklist can be seen in the chat history if it exists. Use this tool to write a new checklist or edit the existing one.",
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ export function generateDiff(
|
|||
}
|
||||
|
||||
export const writeFileTool: Tool = {
|
||||
name: "write_file",
|
||||
name: "Write",
|
||||
displayName: "Write",
|
||||
description: "Write content to a file at the specified path",
|
||||
parameters: {
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
|
|||
const chars = content.length;
|
||||
const displayName = toolName ? getToolDisplayName(toolName) : "Tool";
|
||||
|
||||
// Handle write_checklist specially with styled display
|
||||
if (toolName === "write_checklist") {
|
||||
// Handle Checklist specially with styled display
|
||||
if (toolName === "Checklist") {
|
||||
return (
|
||||
<Box flexDirection="column">
|
||||
<Box marginBottom={1}>
|
||||
|
|
@ -40,9 +40,9 @@ const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
|
|||
);
|
||||
}
|
||||
|
||||
// Handle write_file with diff specially
|
||||
// Handle Write/Edit with diff specially
|
||||
if (
|
||||
(toolName === "write_file" || toolName === "edit_file") &&
|
||||
(toolName === "Write" || toolName === "Edit") &&
|
||||
content.includes("Diff:\n")
|
||||
) {
|
||||
const diffSection = content.split("Diff:\n")[1];
|
||||
|
|
@ -52,7 +52,7 @@ const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
|
|||
<Box>
|
||||
<Text color="gray">⎿ </Text>
|
||||
<Text color="green">
|
||||
{toolName === "edit_file"
|
||||
{toolName === "Edit"
|
||||
? " File edited successfully"
|
||||
: " File written successfully"}
|
||||
</Text>
|
||||
|
|
@ -64,7 +64,7 @@ const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
|
|||
}
|
||||
|
||||
// Handle terminal command output specially
|
||||
if (toolName === "run_terminal_command") {
|
||||
if (toolName === "Bash") {
|
||||
const isStderr = content.startsWith("Stderr:");
|
||||
const actualOutput = isStderr ? content.slice(7).trim() : content;
|
||||
const outputLines = actualOutput.split("\n");
|
||||
|
|
@ -121,7 +121,7 @@ const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
|
|||
|
||||
// Handle specific tool output formatting
|
||||
switch (toolName) {
|
||||
case "read_file":
|
||||
case "Read":
|
||||
// Try to extract file path from content if it contains line numbers
|
||||
if (content.includes("→")) {
|
||||
const pathMatch = content.match(/^(.+?):/);
|
||||
|
|
@ -130,23 +130,23 @@ const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
|
|||
}
|
||||
return `${displayName} tool output (${lines} lines)`;
|
||||
|
||||
case "write_file":
|
||||
case "Write":
|
||||
return content.includes("Successfully created file")
|
||||
? "File created successfully"
|
||||
: "File written successfully";
|
||||
|
||||
case "edit_file":
|
||||
case "Edit":
|
||||
return content.includes("Successfully created file")
|
||||
? "File created successfully"
|
||||
: "File edited successfully";
|
||||
|
||||
case "list_files":
|
||||
case "List":
|
||||
return `Listed ${lines} ${lines === 1 ? "item" : "items"}`;
|
||||
|
||||
case "search_code":
|
||||
case "Search":
|
||||
return `Found ${lines} ${lines === 1 ? "match" : "matches"}`;
|
||||
|
||||
case "view_diff":
|
||||
case "Diff":
|
||||
return `Diff output (${lines} lines)`;
|
||||
|
||||
default:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue