Fix SDK tools to respect cwd option

Core tools now properly use the cwd passed to createAgentSession().
Added tool factory functions for SDK users who specify custom cwd with explicit tools.

Fixes #279
This commit is contained in:
Mario Zechner 2025-12-22 16:17:55 +01:00
parent 42bc368e70
commit face745f3d
16 changed files with 1243 additions and 1044 deletions

View file

@ -2,6 +2,10 @@
* Tools Configuration
*
* Use built-in tool sets, individual tools, or add custom tools.
*
* IMPORTANT: When using a custom `cwd`, you must use the tool factory functions
* (createCodingTools, createReadOnlyTools, createReadTool, etc.) to ensure
* tools resolve paths relative to your cwd, not process.cwd().
*/
import { Type } from "@sinclair/typebox";
@ -9,28 +13,50 @@ import {
createAgentSession,
discoverCustomTools,
SessionManager,
codingTools, // read, bash, edit, write (default)
readOnlyTools, // read, bash
codingTools, // read, bash, edit, write - uses process.cwd()
readOnlyTools, // read, grep, find, ls - uses process.cwd()
createCodingTools, // Factory: creates tools for specific cwd
createReadOnlyTools, // Factory: creates tools for specific cwd
createReadTool,
createBashTool,
createGrepTool,
readTool,
bashTool,
grepTool,
type CustomAgentTool,
} from "../../src/index.js";
// Read-only mode (no edit/write)
// Read-only mode (no edit/write) - uses process.cwd()
const { session: readOnly } = await createAgentSession({
tools: readOnlyTools,
sessionManager: SessionManager.inMemory(),
});
console.log("Read-only session created");
// Custom tool selection
// Custom tool selection - uses process.cwd()
const { session: custom } = await createAgentSession({
tools: [readTool, bashTool, grepTool],
sessionManager: SessionManager.inMemory(),
});
console.log("Custom tools session created");
// With custom cwd - MUST use factory functions!
const customCwd = "/path/to/project";
const { session: customCwdSession } = await createAgentSession({
cwd: customCwd,
tools: createCodingTools(customCwd), // Tools resolve paths relative to customCwd
sessionManager: SessionManager.inMemory(),
});
console.log("Custom cwd session created");
// Or pick specific tools for custom cwd
const { session: specificTools } = await createAgentSession({
cwd: customCwd,
tools: [createReadTool(customCwd), createBashTool(customCwd), createGrepTool(customCwd)],
sessionManager: SessionManager.inMemory(),
});
console.log("Specific tools with custom cwd session created");
// Inline custom tool (needs TypeBox schema)
const weatherTool: CustomAgentTool = {
name: "get_weather",

View file

@ -3,6 +3,10 @@
*
* Replace everything - no discovery, explicit configuration.
* Still uses OAuth from ~/.pi/agent for convenience.
*
* IMPORTANT: When providing `tools` with a custom `cwd`, use the tool factory
* functions (createReadTool, createBashTool, etc.) to ensure tools resolve
* paths relative to your cwd.
*/
import { Type } from "@sinclair/typebox";
@ -13,8 +17,8 @@ import {
findModel,
SessionManager,
SettingsManager,
readTool,
bashTool,
createReadTool,
createBashTool,
type HookFactory,
type CustomAgentTool,
} from "../../src/index.js";
@ -60,8 +64,11 @@ const settingsManager = SettingsManager.inMemory({
retry: { enabled: true, maxRetries: 2 },
});
// When using a custom cwd with explicit tools, use the factory functions
const cwd = process.cwd();
const { session } = await createAgentSession({
cwd: process.cwd(),
cwd,
agentDir: "/tmp/my-agent",
model,
@ -71,7 +78,8 @@ const { session } = await createAgentSession({
systemPrompt: `You are a minimal assistant.
Available: read, bash, status. Be concise.`,
tools: [readTool, bashTool],
// Use factory functions with the same cwd to ensure path resolution works correctly
tools: [createReadTool(cwd), createBashTool(cwd)],
customTools: [{ tool: statusTool }],
hooks: [{ factory: auditHook }],
skills: [],