Fix SDK docs: AgentSession interface, findModel usage, settings example

This commit is contained in:
Mario Zechner 2025-12-22 12:32:25 +01:00
parent 1e6a23ab3d
commit 5d290f048e
2 changed files with 39 additions and 17 deletions

View file

@ -79,18 +79,32 @@ interface AgentSession {
sessionId: string; sessionId: string;
// Model control // Model control
setModel(model: Model, thinkingLevel?: ThinkingLevel): void; setModel(model: Model): Promise<void>;
setThinkingLevel(level: ThinkingLevel): void; setThinkingLevel(level: ThinkingLevel): void;
cycleModel(): Promise<ModelCycleResult | null>;
cycleThinkingLevel(): ThinkingLevel | null;
// Access underlying agent // State access
agent: Agent; agent: Agent;
model: Model | null;
thinkingLevel: ThinkingLevel;
messages: AppMessage[];
isStreaming: boolean;
// Session management // Session management
reset(): void; reset(): Promise<void>;
branch(targetTurnIndex: number): Promise<void>; branch(entryIndex: number): Promise<{ selectedText: string; skipped: boolean }>;
switchSession(sessionPath: string): Promise<void>;
// Compaction
compact(customInstructions?: string): Promise<CompactionResult>;
abortCompaction(): void;
// Abort current operation // Abort current operation
abort(): void; abort(): Promise<void>;
// Cleanup
dispose(): void;
} }
``` ```
@ -215,15 +229,17 @@ const { session } = await createAgentSession({
```typescript ```typescript
import { findModel, discoverAvailableModels } from "@mariozechner/pi-coding-agent"; import { findModel, discoverAvailableModels } from "@mariozechner/pi-coding-agent";
// Find specific model // Find specific model (returns { model, error })
const { model } = findModel("anthropic", "claude-sonnet-4-20250514"); const { model, error } = findModel("anthropic", "claude-sonnet-4-20250514");
if (error) throw new Error(error);
if (!model) throw new Error("Model not found");
// Or get all models with valid API keys // Or get all models with valid API keys
const available = await discoverAvailableModels(); const available = await discoverAvailableModels();
const { session } = await createAgentSession({ const { session } = await createAgentSession({
model: model, model: model,
thinkingLevel: "medium", // off, low, medium, high thinkingLevel: "medium", // off, minimal, low, medium, high, xhigh
// Models for cycling (Ctrl+P in interactive mode) // Models for cycling (Ctrl+P in interactive mode)
scopedModels: [ scopedModels: [
@ -580,8 +596,8 @@ const contextFiles = discoverContextFiles(cwd, agentDir);
// Slash commands // Slash commands
const commands = discoverSlashCommands(cwd, agentDir); const commands = discoverSlashCommands(cwd, agentDir);
// Settings // Settings (global + project merged)
const settings = loadSettings(agentDir); const settings = loadSettings(cwd, agentDir);
// Build system prompt manually // Build system prompt manually
const prompt = buildSystemPrompt({ const prompt = buildSystemPrompt({
@ -622,6 +638,7 @@ import {
defaultGetApiKey, defaultGetApiKey,
findModel, findModel,
SessionManager, SessionManager,
SettingsManager,
readTool, readTool,
bashTool, bashTool,
type HookFactory, type HookFactory,
@ -660,9 +677,16 @@ const statusTool: CustomAgentTool = {
}), }),
}; };
const { model } = findModel("anthropic", "claude-sonnet-4-20250514"); const { model, error } = findModel("anthropic", "claude-sonnet-4-20250514");
if (error) throw new Error(error);
if (!model) throw new Error("Model not found"); if (!model) throw new Error("Model not found");
// In-memory settings with overrides
const settingsManager = SettingsManager.inMemory({
compaction: { enabled: false },
retry: { enabled: true, maxRetries: 2 },
});
const { session } = await createAgentSession({ const { session } = await createAgentSession({
cwd: process.cwd(), cwd: process.cwd(),
agentDir: "/custom/agent", agentDir: "/custom/agent",
@ -681,11 +705,7 @@ const { session } = await createAgentSession({
slashCommands: [], slashCommands: [],
sessionManager: SessionManager.inMemory(), sessionManager: SessionManager.inMemory(),
settingsManager,
settings: {
compaction: { enabled: false },
retry: { enabled: true, maxRetries: 2 },
},
}); });
session.subscribe((event) => { session.subscribe((event) => {
@ -744,6 +764,7 @@ buildSystemPrompt
// Session management // Session management
SessionManager SessionManager
SettingsManager
// Built-in tools // Built-in tools
codingTools codingTools

View file

@ -17,6 +17,7 @@ import { getModelsPath, VERSION } from "./config.js";
import type { AgentSession } from "./core/agent-session.js"; import type { AgentSession } from "./core/agent-session.js";
import type { LoadedCustomTool } from "./core/custom-tools/index.js"; import type { LoadedCustomTool } from "./core/custom-tools/index.js";
import { exportFromFile } from "./core/export-html.js"; import { exportFromFile } from "./core/export-html.js";
import type { HookUIContext } from "./core/index.js";
import { findModel } from "./core/model-config.js"; import { findModel } from "./core/model-config.js";
import { resolveModelScope, type ScopedModel } from "./core/model-resolver.js"; import { resolveModelScope, type ScopedModel } from "./core/model-resolver.js";
import { type CreateAgentSessionOptions, configureOAuthStorage, createAgentSession } from "./core/sdk.js"; import { type CreateAgentSessionOptions, configureOAuthStorage, createAgentSession } from "./core/sdk.js";
@ -54,7 +55,7 @@ async function runInteractiveMode(
versionCheckPromise: Promise<string | null>, versionCheckPromise: Promise<string | null>,
initialMessages: string[], initialMessages: string[],
customTools: LoadedCustomTool[], customTools: LoadedCustomTool[],
setToolUIContext: (uiContext: import("./core/hooks/types.js").HookUIContext, hasUI: boolean) => void, setToolUIContext: (uiContext: HookUIContext, hasUI: boolean) => void,
initialMessage?: string, initialMessage?: string,
initialAttachments?: Attachment[], initialAttachments?: Attachment[],
fdPath: string | null = null, fdPath: string | null = null,