d6d15a2937
- Consolidated fetch tool into read tool with URL reading capability and caching support. - Removed standalone fetch tool from all agent prompts and CLI documentation. - Extended read tool schema with timeout and raw parameters for URL fetch control. - Added URL caching mechanism to prevent redundant network requests during read operations. - Refactored fetch module from class-based tool to standalone executeReadUrl function. - Updated read tool documentation to describe multi-purpose capabilities including web pages, GitHub, Stack Overflow, Wikipedia, Reddit, NPM, arXiv, blogs, and feeds.
283 lines
8.5 KiB
TypeScript
283 lines
8.5 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "bun:test";
|
|
import { type SettingPath, Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
|
|
import { createTools, HIDDEN_TOOLS, type ToolSession } from "@oh-my-pi/pi-coding-agent/tools";
|
|
|
|
Bun.env.PI_PYTHON_SKIP_CHECK = "1";
|
|
|
|
function createTestSession(overrides: Partial<ToolSession> = {}): ToolSession {
|
|
return {
|
|
cwd: "/tmp/test",
|
|
hasUI: false,
|
|
getSessionFile: () => null,
|
|
getSessionSpawns: () => "*",
|
|
settings: Settings.isolated(),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function createSettingsWithOverrides(overrides: Partial<Record<SettingPath, unknown>> = {}): Settings {
|
|
return Settings.isolated({
|
|
"lsp.formatOnWrite": true,
|
|
"bashInterceptor.enabled": true,
|
|
...overrides,
|
|
});
|
|
}
|
|
|
|
function createDiscoverySessionHooks(): Partial<ToolSession> {
|
|
const selected: string[] = [];
|
|
return {
|
|
isMCPDiscoveryEnabled: () => true,
|
|
getDiscoverableMCPTools: () => [],
|
|
getSelectedMCPToolNames: () => [...selected],
|
|
activateDiscoveredMCPTools: async toolNames => {
|
|
const activated: string[] = [];
|
|
for (const name of toolNames) {
|
|
if (!selected.includes(name)) {
|
|
selected.push(name);
|
|
activated.push(name);
|
|
}
|
|
}
|
|
return activated;
|
|
},
|
|
};
|
|
}
|
|
|
|
describe("createTools", () => {
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
it("creates all builtin tools by default", async () => {
|
|
const session = createTestSession();
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
// Core tools should always be present
|
|
expect(names).toContain("python");
|
|
expect(names).toContain("bash");
|
|
expect(names).toContain("read");
|
|
expect(names).toContain("edit");
|
|
expect(names).toContain("write");
|
|
expect(names).toContain("grep");
|
|
expect(names).toContain("find");
|
|
expect(names).toContain("lsp");
|
|
expect(names).toContain("notebook");
|
|
expect(names).toContain("task");
|
|
expect(names).toContain("todo_write");
|
|
expect(names).toContain("web_search");
|
|
expect(names).toContain("exit_plan_mode");
|
|
expect(names).not.toContain("fetch");
|
|
});
|
|
|
|
it("includes bash and python when python mode is both", async () => {
|
|
const session = createTestSession({
|
|
settings: createSettingsWithOverrides({
|
|
"python.toolMode": "both",
|
|
"python.kernelMode": "session",
|
|
}),
|
|
});
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).toContain("bash");
|
|
expect(names).toContain("python");
|
|
});
|
|
|
|
it("includes bash only when python mode is bash-only", async () => {
|
|
const session = createTestSession({
|
|
settings: createSettingsWithOverrides({
|
|
"python.toolMode": "bash-only",
|
|
"python.kernelMode": "session",
|
|
}),
|
|
});
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).toContain("bash");
|
|
expect(names).not.toContain("python");
|
|
});
|
|
|
|
it("excludes lsp tool when session disables LSP", async () => {
|
|
const session = createTestSession({ enableLsp: false });
|
|
const tools = await createTools(session, ["read", "lsp", "write"]);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).toEqual(["read", "write", "exit_plan_mode"]);
|
|
});
|
|
|
|
it("excludes lsp tool when disabled", async () => {
|
|
const session = createTestSession({ enableLsp: false });
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).not.toContain("lsp");
|
|
});
|
|
|
|
it("respects requested tool subset", async () => {
|
|
const session = createTestSession();
|
|
const tools = await createTools(session, ["read", "write"]);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).toEqual(["read", "write", "exit_plan_mode"]);
|
|
});
|
|
|
|
it("lowercases requested tool subset", async () => {
|
|
const session = createTestSession();
|
|
const tools = await createTools(session, ["Read", "Write"]);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).toEqual(["read", "write", "exit_plan_mode"]);
|
|
});
|
|
|
|
it("includes hidden tools when explicitly requested", async () => {
|
|
const session = createTestSession();
|
|
const tools = await createTools(session, ["report_finding"]);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).toEqual(["report_finding", "exit_plan_mode"]);
|
|
});
|
|
|
|
it("includes submit_result tool when required", async () => {
|
|
const session = createTestSession({ requireSubmitResultTool: true });
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).toContain("submit_result");
|
|
});
|
|
|
|
it("excludes ask tool when hasUI is false", async () => {
|
|
const session = createTestSession({ hasUI: false });
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).not.toContain("ask");
|
|
});
|
|
|
|
it("includes ask tool when hasUI is true", async () => {
|
|
const session = createTestSession({ hasUI: true });
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).toContain("ask");
|
|
});
|
|
|
|
it("excludes render_mermaid tool by default", async () => {
|
|
const session = createTestSession();
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).not.toContain("render_mermaid");
|
|
});
|
|
|
|
it("includes render_mermaid tool when enabled", async () => {
|
|
const session = createTestSession({
|
|
settings: createSettingsWithOverrides({
|
|
"renderMermaid.enabled": true,
|
|
}),
|
|
});
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).toContain("render_mermaid");
|
|
});
|
|
|
|
it("excludes GitHub CLI tools by default", async () => {
|
|
const session = createTestSession();
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).not.toContain("gh_repo_view");
|
|
expect(names).not.toContain("gh_issue_view");
|
|
expect(names).not.toContain("gh_pr_view");
|
|
expect(names).not.toContain("gh_pr_diff");
|
|
expect(names).not.toContain("gh_pr_checkout");
|
|
expect(names).not.toContain("gh_pr_push");
|
|
expect(names).not.toContain("gh_run_watch");
|
|
expect(names).not.toContain("gh_search_issues");
|
|
expect(names).not.toContain("gh_search_prs");
|
|
});
|
|
|
|
it("includes GitHub CLI tools when enabled and gh is available", async () => {
|
|
vi.spyOn(Bun, "which").mockImplementation(command => (command === "gh" ? "/usr/bin/gh" : null));
|
|
const session = createTestSession({
|
|
settings: createSettingsWithOverrides({
|
|
"github.enabled": true,
|
|
}),
|
|
});
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).toContain("gh_repo_view");
|
|
expect(names).toContain("gh_issue_view");
|
|
expect(names).toContain("gh_pr_view");
|
|
expect(names).toContain("gh_pr_diff");
|
|
expect(names).toContain("gh_pr_checkout");
|
|
expect(names).toContain("gh_pr_push");
|
|
expect(names).toContain("gh_run_watch");
|
|
expect(names).toContain("gh_search_issues");
|
|
expect(names).toContain("gh_search_prs");
|
|
});
|
|
|
|
it("excludes inspect_image tool by default", async () => {
|
|
const session = createTestSession();
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).not.toContain("inspect_image");
|
|
});
|
|
|
|
it("includes inspect_image tool when enabled", async () => {
|
|
const session = createTestSession({
|
|
settings: createSettingsWithOverrides({
|
|
"inspect_image.enabled": true,
|
|
}),
|
|
});
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).toContain("inspect_image");
|
|
});
|
|
|
|
it("excludes search_tool_bm25 by default", async () => {
|
|
const session = createTestSession();
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).not.toContain("search_tool_bm25");
|
|
});
|
|
|
|
it("excludes search_tool_bm25 when MCP tool discovery lacks execution hooks", async () => {
|
|
const session = createTestSession({
|
|
settings: createSettingsWithOverrides({
|
|
"mcp.discoveryMode": true,
|
|
}),
|
|
});
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).not.toContain("search_tool_bm25");
|
|
});
|
|
|
|
it("includes search_tool_bm25 when MCP tool discovery is enabled and executable", async () => {
|
|
const session = createTestSession({
|
|
settings: createSettingsWithOverrides({
|
|
"mcp.discoveryMode": true,
|
|
}),
|
|
...createDiscoverySessionHooks(),
|
|
});
|
|
const tools = await createTools(session);
|
|
const names = tools.map(t => t.name);
|
|
|
|
expect(names).toContain("search_tool_bm25");
|
|
});
|
|
|
|
it("HIDDEN_TOOLS contains review tools", () => {
|
|
expect(Object.keys(HIDDEN_TOOLS).sort()).toEqual([
|
|
"exit_plan_mode",
|
|
"report_finding",
|
|
"resolve",
|
|
"submit_result",
|
|
]);
|
|
});
|
|
});
|