diff --git a/packages/coding-agent/src/core/sdk.ts b/packages/coding-agent/src/core/sdk.ts index ab1c13ed..1dd44834 100644 --- a/packages/coding-agent/src/core/sdk.ts +++ b/packages/coding-agent/src/core/sdk.ts @@ -66,18 +66,14 @@ import { writeTool, } from "./tools/index.js"; -// ============================================================================ // Types -// ============================================================================ export interface CreateAgentSessionOptions { - // === Environment === /** Working directory for project-local discovery. Default: process.cwd() */ cwd?: string; /** Global config directory. Default: ~/.pi/agent */ agentDir?: string; - // === Model & Thinking === /** Model to use. Default: from settings, else first available */ model?: Model; /** Thinking level. Default: from settings, else 'off' (clamped to model capabilities) */ @@ -85,15 +81,12 @@ export interface CreateAgentSessionOptions { /** Models available for cycling (Ctrl+P in interactive mode) */ scopedModels?: Array<{ model: Model; thinkingLevel: ThinkingLevel }>; - // === API Key === /** API key resolver. Default: defaultGetApiKey() */ getApiKey?: (model: Model) => Promise; - // === System Prompt === /** System prompt. String replaces default, function receives default and returns final. */ systemPrompt?: string | ((defaultPrompt: string) => string); - // === Tools === /** Built-in tools to use. Default: codingTools [read, bash, edit, write] */ tools?: Tool[]; /** Custom tools (replaces discovery). */ @@ -101,13 +94,11 @@ export interface CreateAgentSessionOptions { /** Additional custom tool paths to load (merged with discovery). */ additionalCustomToolPaths?: string[]; - // === Hooks === /** Hooks (replaces discovery). */ hooks?: Array<{ path?: string; factory: HookFactory }>; /** Additional hook paths to load (merged with discovery). */ additionalHookPaths?: string[]; - // === Context === /** Skills. Default: discovered from multiple locations */ skills?: Skill[]; /** Context files (AGENTS.md content). Default: discovered walking up from cwd */ @@ -115,7 +106,6 @@ export interface CreateAgentSessionOptions { /** Slash commands. Default: discovered from cwd/.pi/commands/ + agentDir/commands/ */ slashCommands?: FileSlashCommand[]; - // === Session === /** Session file path, or false to disable persistence. Default: auto in agentDir/sessions/ */ sessionFile?: string | false; /** Continue most recent session for cwd. */ @@ -123,7 +113,6 @@ export interface CreateAgentSessionOptions { /** Restore model/thinking from session (default: true when continuing). */ restoreFromSession?: boolean; - // === Settings === /** Settings overrides (merged with agentDir/settings.json) */ settings?: Partial; } @@ -141,9 +130,7 @@ export interface CreateAgentSessionResult { modelFallbackMessage?: string; } -// ============================================================================ // Re-exports -// ============================================================================ export type { CustomAgentTool } from "./custom-tools/types.js"; export type { HookAPI, HookFactory } from "./hooks/types.js"; @@ -165,17 +152,13 @@ export { allTools as allBuiltInTools, }; -// ============================================================================ // Helper Functions -// ============================================================================ function getDefaultAgentDir(): string { return getAgentDir(); } -// ============================================================================ // Discovery Functions -// ============================================================================ /** * Get all models (built-in + custom from models.json). @@ -292,9 +275,7 @@ export function discoverSlashCommands(cwd?: string, agentDir?: string): FileSlas }); } -// ============================================================================ // API Key Helpers -// ============================================================================ /** * Create the default API key resolver. @@ -305,9 +286,7 @@ export function defaultGetApiKey(): (model: Model) => Promise void }; if (options.customTools !== undefined) { // Use provided custom tools @@ -566,7 +530,6 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {} customToolsResult = result; } - // === Hooks === let hookRunner: HookRunner | null = null; if (options.hooks !== undefined) { if (options.hooks.length > 0) { @@ -585,13 +548,11 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {} } } - // === Combine and wrap tools === let allToolsArray: Tool[] = [...builtInTools, ...customToolsResult.tools.map((lt) => lt.tool as unknown as Tool)]; if (hookRunner) { allToolsArray = wrapToolsWithHooks(allToolsArray, hookRunner) as Tool[]; } - // === System Prompt === let systemPrompt: string; const defaultPrompt = buildSystemPromptInternal({ cwd, @@ -608,10 +569,8 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {} systemPrompt = options.systemPrompt(defaultPrompt); } - // === Slash Commands === const slashCommands = options.slashCommands ?? discoverSlashCommands(cwd, agentDir); - // === Create Agent === const agent = new Agent({ initialState: { systemPrompt, @@ -636,7 +595,6 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {} }), }); - // === Load messages if continuing session === if (shouldRestoreFromSession) { const messages = sessionManager.loadMessages(); if (messages.length > 0) { @@ -644,7 +602,6 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {} } } - // === Create Session === const session = new AgentSession({ agent, sessionManager, diff --git a/packages/coding-agent/src/main.ts b/packages/coding-agent/src/main.ts index 3c1e2a68..b02ae8a2 100644 --- a/packages/coding-agent/src/main.ts +++ b/packages/coding-agent/src/main.ts @@ -272,8 +272,6 @@ export async function main(args: string[]) { const parsed = parseArgs(args); - // === Early exits === - if (parsed.version) { console.log(VERSION); return; @@ -303,15 +301,11 @@ export async function main(args: string[]) { } } - // === Validation === - if (parsed.mode === "rpc" && parsed.fileArgs.length > 0) { console.error(chalk.red("Error: @file arguments are not supported in RPC mode")); process.exit(1); } - // === Prepare inputs === - const { initialMessage, initialAttachments } = await prepareInitialMessage(parsed); const isInteractive = !parsed.print && parsed.mode === undefined; const mode = parsed.mode || "text"; @@ -338,8 +332,6 @@ export async function main(args: string[]) { sessionFileFromResume = selectedSession; } - // === Build session options === - const sessionOptions = await buildSessionOptions(parsed, scopedModels); // Apply resume session file @@ -348,12 +340,8 @@ export async function main(args: string[]) { sessionOptions.restoreFromSession = true; } - // === Create session === - const { session, customToolsResult, modelFallbackMessage } = await createAgentSession(sessionOptions); - // === Validate for non-interactive mode === - if (!isInteractive && !session.model) { console.error(chalk.red("No models available.")); console.error(chalk.yellow("\nSet an API key environment variable:")); @@ -375,8 +363,6 @@ export async function main(args: string[]) { } } - // === Route to mode === - if (mode === "rpc") { await runRpcMode(session); } else if (isInteractive) {