Add SDK usage examples

12 examples showing increasing levels of customization:
- 01-minimal: all defaults
- 02-custom-model: model and thinking level
- 03-custom-prompt: replace or modify prompt
- 04-skills: discover, filter, merge skills
- 05-tools: built-in tools, custom tools
- 06-hooks: logging, blocking, result modification
- 07-context-files: AGENTS.md files
- 08-slash-commands: file-based commands
- 09-api-keys-and-oauth: API key resolution, OAuth config
- 10-settings: compaction, retry, terminal settings
- 11-sessions: persistence options
- 12-full-control: replace everything

Also exports FileSlashCommand type from index.ts
This commit is contained in:
Mario Zechner 2025-12-22 03:14:30 +01:00
parent 86cfe6a436
commit 56121dcac1
15 changed files with 723 additions and 0 deletions

View file

@ -0,0 +1,46 @@
/**
* Session Management
*
* Control session persistence: in-memory, new file, continue, or open specific.
*/
import { createAgentSession, SessionManager } from "../../src/index.js";
// In-memory (no persistence)
const { session: inMemory } = await createAgentSession({
sessionManager: SessionManager.inMemory(),
});
console.log("In-memory session:", inMemory.sessionFile ?? "(none)");
// New persistent session
const { session: newSession } = await createAgentSession({
sessionManager: SessionManager.create(process.cwd()),
});
console.log("New session file:", newSession.sessionFile);
// Continue most recent session (or create new if none)
const { session: continued, modelFallbackMessage } = await createAgentSession({
sessionManager: SessionManager.continueRecent(process.cwd()),
});
if (modelFallbackMessage) console.log("Note:", modelFallbackMessage);
console.log("Continued session:", continued.sessionFile);
// List and open specific session
const sessions = SessionManager.list(process.cwd());
console.log(`\nFound ${sessions.length} sessions:`);
for (const info of sessions.slice(0, 3)) {
console.log(` ${info.id.slice(0, 8)}... - "${info.firstMessage.slice(0, 30)}..."`);
}
if (sessions.length > 0) {
const { session: opened } = await createAgentSession({
sessionManager: SessionManager.open(sessions[0].path),
});
console.log(`\nOpened: ${opened.sessionId}`);
}
// Custom session directory
// const { session } = await createAgentSession({
// agentDir: "/custom/agent",
// sessionManager: SessionManager.create(process.cwd(), "/custom/agent"),
// });