mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-11-05 23:37:25 +00:00
feat(coding-agent): add configurable default tools
This commit is contained in:
parent
47b5119d09
commit
4d9aa837c2
6 changed files with 136 additions and 6 deletions
86
packages/coding-agent/test/default-tools-setting.test.ts
Normal file
86
packages/coding-agent/test/default-tools-setting.test.ts
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import { existsSync, mkdirSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { getModel } from "@earendil-works/pi-ai/compat";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { createAgentSessionFromServices, createAgentSessionServices } from "../src/core/agent-session-services.ts";
|
||||
import { DefaultResourceLoader } from "../src/core/resource-loader.ts";
|
||||
import { type CreateAgentSessionOptions, createAgentSession } from "../src/core/sdk.ts";
|
||||
import { SessionManager } from "../src/core/session-manager.ts";
|
||||
import { SettingsManager } from "../src/core/settings-manager.ts";
|
||||
|
||||
type ToolOptions = Pick<CreateAgentSessionOptions, "tools" | "excludeTools" | "noTools">;
|
||||
|
||||
describe("defaultTools setting", () => {
|
||||
let tempDir: string;
|
||||
let agentDir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tempDir = join(tmpdir(), `pi-default-tools-${Date.now()}-${Math.random().toString(36).slice(2)}`);
|
||||
agentDir = join(tempDir, "agent");
|
||||
mkdirSync(agentDir, { recursive: true });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (tempDir && existsSync(tempDir)) {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
async function createSession(defaultTools: string[], options: ToolOptions = {}) {
|
||||
const settingsManager = SettingsManager.inMemory({ defaultTools });
|
||||
const resourceLoader = new DefaultResourceLoader({ cwd: tempDir, agentDir, settingsManager });
|
||||
await resourceLoader.reload();
|
||||
|
||||
return (
|
||||
await createAgentSession({
|
||||
cwd: tempDir,
|
||||
agentDir,
|
||||
model: getModel("anthropic", "claude-sonnet-4-5")!,
|
||||
settingsManager,
|
||||
sessionManager: SessionManager.inMemory(tempDir),
|
||||
resourceLoader,
|
||||
...options,
|
||||
})
|
||||
).session;
|
||||
}
|
||||
|
||||
it("uses the configured list as the initial tool allowlist", async () => {
|
||||
const session = await createSession(["grep", "find"]);
|
||||
|
||||
expect(session.getAllTools().map((tool) => tool.name)).toEqual(["grep", "find"]);
|
||||
expect(session.getActiveToolNames()).toEqual(["grep", "find"]);
|
||||
expect(session.systemPrompt).toContain("- grep:");
|
||||
expect(session.systemPrompt).not.toContain("- read:");
|
||||
session.dispose();
|
||||
});
|
||||
|
||||
it("preserves explicit tool option precedence", async () => {
|
||||
const allowlistedSession = await createSession(["grep"], { tools: ["read"] });
|
||||
expect(allowlistedSession.getActiveToolNames()).toEqual(["read"]);
|
||||
allowlistedSession.dispose();
|
||||
|
||||
const excludedSession = await createSession(["read", "grep"], { excludeTools: ["read"] });
|
||||
expect(excludedSession.getActiveToolNames()).toEqual(["grep"]);
|
||||
excludedSession.dispose();
|
||||
|
||||
const toolLessSession = await createSession(["read"], { noTools: "all" });
|
||||
expect(toolLessSession.getAllTools()).toEqual([]);
|
||||
expect(toolLessSession.getActiveToolNames()).toEqual([]);
|
||||
toolLessSession.dispose();
|
||||
});
|
||||
|
||||
it("applies through service-based session creation", async () => {
|
||||
const settingsManager = SettingsManager.inMemory({ defaultTools: ["ls"] });
|
||||
const services = await createAgentSessionServices({ cwd: tempDir, agentDir, settingsManager });
|
||||
const { session } = await createAgentSessionFromServices({
|
||||
services,
|
||||
sessionManager: SessionManager.inMemory(tempDir),
|
||||
model: getModel("anthropic", "claude-sonnet-4-5")!,
|
||||
});
|
||||
|
||||
expect(session.getAllTools().map((tool) => tool.name)).toEqual(["ls"]);
|
||||
expect(session.getActiveToolNames()).toEqual(["ls"]);
|
||||
session.dispose();
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue