|
1 | 1 | # KODE SDK |
2 | 2 |
|
3 | | -> **Stateful Agent Runtime Kernel** - The engine that powers your AI agents with persistence, recovery, and trajectory exploration. |
| 3 | +[English](./README.md) | [中文](./README.zh-CN.md) |
4 | 4 |
|
5 | | -``` |
6 | | - +------------------+ |
7 | | - | Your App | CLI / Desktop / IDE / Server |
8 | | - +--------+---------+ |
9 | | - | |
10 | | - +--------v---------+ |
11 | | - | KODE SDK | Agent Runtime Kernel |
12 | | - | +-----------+ | |
13 | | - | | Agent | | Lifecycle + State + Events |
14 | | - | +-----------+ | |
15 | | - | | Store | | Persistence (Pluggable) |
16 | | - | +-----------+ | |
17 | | - | | Sandbox | | Execution Isolation |
18 | | - | +-----------+ | |
19 | | - +------------------+ |
20 | | -``` |
21 | | - |
22 | | ---- |
23 | | - |
24 | | -## What is KODE SDK? |
25 | | - |
26 | | -KODE SDK is an **Agent Runtime Kernel** - think of it like V8 for JavaScript, but for AI agents. It handles the complex lifecycle management so you can focus on building your agent's capabilities. |
27 | | - |
28 | | -**Core Capabilities:** |
29 | | -- **Crash Recovery**: WAL-protected persistence with 7-stage breakpoint recovery |
30 | | -- **Fork & Resume**: Explore different agent trajectories from any checkpoint |
31 | | -- **Event Streams**: Progress/Control/Monitor channels for real-time UI updates |
32 | | -- **Tool Governance**: Permission system, approval workflows, audit trails |
33 | | - |
34 | | -**What KODE SDK is NOT:** |
35 | | -- Not a cloud platform (you deploy it) |
36 | | -- Not an HTTP server (you add that layer) |
37 | | -- Not a multi-tenant SaaS framework (you build that on top) |
38 | | - |
39 | | ---- |
40 | | - |
41 | | -## When to Use KODE SDK |
42 | | - |
43 | | -### Perfect Fit (Use directly) |
44 | | - |
45 | | -| Scenario | Why It Works | |
46 | | -|----------|--------------| |
47 | | -| **CLI Agent Tools** | Single process, local filesystem, zero config | |
48 | | -| **Desktop Apps** (Electron/Tauri) | Full system access, long-running process | |
49 | | -| **IDE Plugins** (VSCode/JetBrains) | Single user, workspace integration | |
50 | | -| **Local Development** | Fast iteration, instant persistence | |
51 | | - |
52 | | -### Good Fit (With architecture) |
53 | | - |
54 | | -| Scenario | What You Need | |
55 | | -|----------|---------------| |
56 | | -| **Self-hosted Server** | Add HTTP layer (Express/Fastify/Hono) | |
57 | | -| **Small-scale Backend** (<1K users) | Implement PostgresStore, add user isolation | |
58 | | -| **Kubernetes Deployment** | Implement distributed Store + locks | |
59 | | - |
60 | | -### Needs Custom Architecture |
61 | | - |
62 | | -| Scenario | Recommended Approach | |
63 | | -|----------|---------------------| |
64 | | -| **Large-scale ToC** (10K+ users) | Worker microservice pattern (see [Architecture Guide](./docs/ARCHITECTURE.md)) | |
65 | | -| **Serverless** (Vercel/Cloudflare) | API layer on serverless + Worker pool for agents | |
66 | | -| **Multi-tenant SaaS** | Tenant isolation layer + distributed Store | |
| 5 | +> Event-driven, long-running AI Agent framework with enterprise-grade persistence and multi-agent collaboration. |
67 | 6 |
|
68 | | -### Not Designed For |
| 7 | +## Features |
69 | 8 |
|
70 | | -| Scenario | Reason | |
71 | | -|----------|--------| |
72 | | -| **Pure browser runtime** | No filesystem, no process execution | |
73 | | -| **Edge functions only** | Agent loops need long-running processes | |
74 | | -| **Stateless microservices** | Agents are inherently stateful | |
| 9 | +- **Event-Driven Architecture** - Three-channel system (Progress/Control/Monitor) for clean separation of concerns |
| 10 | +- **Long-Running & Resumable** - Seven-stage checkpoints with Safe-Fork-Point for crash recovery |
| 11 | +- **Multi-Agent Collaboration** - AgentPool, Room messaging, and task delegation |
| 12 | +- **Enterprise Persistence** - SQLite/PostgreSQL support with unified WAL |
| 13 | +- **Extensible Ecosystem** - MCP tools, custom Providers, Skills system |
75 | 14 |
|
76 | | -> **Rule of Thumb**: If your agents need to run for more than a few seconds, execute tools, and remember state - KODE SDK is for you. If you just need stateless LLM calls, use the provider APIs directly. |
| 15 | +## Quick Start |
77 | 16 |
|
78 | | ---- |
79 | | - |
80 | | -## 60-Second Quick Start |
| 17 | +**One-liner setup** (install dependencies and build): |
81 | 18 |
|
82 | 19 | ```bash |
83 | | -npm install @anthropic/kode-sdk |
84 | | - |
85 | | -# Set your API key |
86 | | -export ANTHROPIC_API_KEY=sk-... |
87 | | - |
88 | | -# Run the example |
89 | | -npx ts-node examples/getting-started.ts |
90 | | -``` |
91 | | - |
92 | | -```typescript |
93 | | -import { Agent, AnthropicProvider, LocalSandbox } from '@anthropic/kode-sdk'; |
94 | | - |
95 | | -const agent = await Agent.create({ |
96 | | - agentId: 'my-first-agent', |
97 | | - template: { systemPrompt: 'You are a helpful assistant.' }, |
98 | | - deps: { |
99 | | - modelProvider: new AnthropicProvider(process.env.ANTHROPIC_API_KEY!), |
100 | | - sandbox: new LocalSandbox({ workDir: './workspace' }), |
101 | | - }, |
102 | | -}); |
103 | | - |
104 | | -// Subscribe to events |
105 | | -agent.subscribeProgress({ kinds: ['text_chunk'] }, (event) => { |
106 | | - process.stdout.write(event.text); |
107 | | -}); |
108 | | - |
109 | | -// Chat with the agent |
110 | | -await agent.chat('Hello! What can you help me with?'); |
| 20 | +./quickstart.sh |
111 | 21 | ``` |
112 | 22 |
|
113 | | ---- |
114 | | - |
115 | | -## Core Concepts |
| 23 | +Or install as a dependency: |
116 | 24 |
|
117 | | -### 1. Three-Channel Event System |
118 | | - |
119 | | -``` |
120 | | -+-------------+ +-------------+ +-------------+ |
121 | | -| Progress | | Control | | Monitor | |
122 | | -+-------------+ +-------------+ +-------------+ |
123 | | -| text_chunk | | permission | | tool_audit | |
124 | | -| tool:start | | _required | | state_change| |
125 | | -| tool:complete| | approval | | token_usage | |
126 | | -| done | | _response | | error | |
127 | | -+-------------+ +-------------+ +-------------+ |
128 | | - | | | |
129 | | - v v v |
130 | | - Your UI Approval Service Observability |
| 25 | +```bash |
| 26 | +npm install @shareai-lab/kode-sdk |
131 | 27 | ``` |
132 | 28 |
|
133 | | -### 2. Crash Recovery & Breakpoints |
| 29 | +Set environment variables: |
134 | 30 |
|
| 31 | +<!-- tabs:start --> |
| 32 | +#### **Linux / macOS** |
| 33 | +```bash |
| 34 | +export ANTHROPIC_API_KEY=sk-... |
| 35 | +export ANTHROPIC_MODEL_ID=claude-sonnet-4-20250514 # optional, default: claude-sonnet-4-20250514 |
| 36 | +export ANTHROPIC_BASE_URL=https://api.anthropic.com # optional, default: https://api.anthropic.com |
135 | 37 | ``` |
136 | | -Agent Execution Flow: |
137 | | -
|
138 | | - READY -> PRE_MODEL -> STREAMING -> TOOL_PENDING -> PRE_TOOL -> EXECUTING -> POST_TOOL |
139 | | - | | | | | | | |
140 | | - +-------- WAL Protected State -------+-- Approval --+---- Tool Execution ---+ |
141 | 38 |
|
142 | | -On crash: Resume from last safe breakpoint, auto-seal incomplete tool calls |
| 39 | +#### **Windows (PowerShell)** |
| 40 | +```powershell |
| 41 | +$env:ANTHROPIC_API_KEY="sk-..." |
| 42 | +$env:ANTHROPIC_MODEL_ID="claude-sonnet-4-20250514" # optional, default: claude-sonnet-4-20250514 |
| 43 | +$env:ANTHROPIC_BASE_URL="https://api.anthropic.com" # optional, default: https://api.anthropic.com |
143 | 44 | ``` |
| 45 | +<!-- tabs:end --> |
144 | 46 |
|
145 | | -### 3. Fork & Trajectory Exploration |
| 47 | +Minimal example: |
146 | 48 |
|
147 | 49 | ```typescript |
148 | | -// Create a snapshot at current state |
149 | | -const snapshotId = await agent.snapshot('before-decision'); |
| 50 | +import { Agent, AnthropicProvider, JSONStore } from '@shareai-lab/kode-sdk'; |
150 | 51 |
|
151 | | -// Fork to explore different paths |
152 | | -const explorerA = await agent.fork(snapshotId); |
153 | | -const explorerB = await agent.fork(snapshotId); |
| 52 | +const provider = new AnthropicProvider( |
| 53 | + process.env.ANTHROPIC_API_KEY!, |
| 54 | + process.env.ANTHROPIC_MODEL_ID |
| 55 | +); |
154 | 56 |
|
155 | | -await explorerA.chat('Try approach A'); |
156 | | -await explorerB.chat('Try approach B'); |
157 | | -``` |
| 57 | +const agent = await Agent.create({ |
| 58 | + provider, |
| 59 | + store: new JSONStore('./.kode'), |
| 60 | + systemPrompt: 'You are a helpful assistant.' |
| 61 | +}); |
158 | 62 |
|
159 | | ---- |
| 63 | +// Subscribe to progress events |
| 64 | +for await (const envelope of agent.subscribe(['progress'])) { |
| 65 | + if (envelope.event.type === 'text_chunk') { |
| 66 | + process.stdout.write(envelope.event.delta); |
| 67 | + } |
| 68 | + if (envelope.event.type === 'done') break; |
| 69 | +} |
160 | 70 |
|
161 | | -## Examples |
| 71 | +await agent.send('Hello!'); |
| 72 | +``` |
162 | 73 |
|
163 | | -| Example | Description | Key Features | |
164 | | -|---------|-------------|--------------| |
165 | | -| `npm run example:getting-started` | Minimal chat loop | Progress stream, basic setup | |
166 | | -| `npm run example:agent-inbox` | Event-driven inbox | Todo management, tool concurrency | |
167 | | -| `npm run example:approval` | Approval workflow | Control channel, hooks, policies | |
168 | | -| `npm run example:room` | Multi-agent collaboration | AgentPool, Room, Fork | |
169 | | -| `npm run example:scheduler` | Long-running with reminders | Scheduler, step triggers | |
170 | | -| `npm run example:nextjs` | Next.js API integration | Resume-or-create, SSE streaming | |
| 74 | +Run examples: |
171 | 75 |
|
172 | | ---- |
| 76 | +```bash |
| 77 | +npm run example:getting-started # Minimal chat |
| 78 | +npm run example:agent-inbox # Event-driven inbox |
| 79 | +npm run example:approval # Tool approval workflow |
| 80 | +npm run example:room # Multi-agent collaboration |
| 81 | +``` |
173 | 82 |
|
174 | 83 | ## Architecture for Scale |
175 | 84 |
|
@@ -209,76 +118,36 @@ For production deployments serving many users, we recommend the **Worker Microse |
209 | 118 | 3. **Store is shared** - Single source of truth for agent state |
210 | 119 | 4. **Queue decouples** - Request handling from agent execution |
211 | 120 |
|
212 | | -See [docs/ARCHITECTURE.md](./docs/ARCHITECTURE.md) for detailed deployment guides. |
213 | | - |
214 | | ---- |
215 | | - |
216 | | -## Documentation |
217 | | - |
218 | | -| Document | Description | |
219 | | -|----------|-------------| |
220 | | -| [Architecture Guide](./docs/ARCHITECTURE.md) | Mental model, deployment patterns, scaling strategies | |
221 | | -| [Quickstart](./docs/quickstart.md) | Step-by-step first agent | |
222 | | -| [Events System](./docs/events.md) | Three-channel event model | |
223 | | -| [API Reference](./docs/api.md) | Core types and interfaces | |
224 | | -| [Playbooks](./docs/playbooks.md) | Common patterns and recipes | |
225 | | -| [Deployment](./docs/DEPLOYMENT.md) | Production deployment guide | |
226 | | -| [Roadmap](./docs/ROADMAP.md) | Future development plans | |
227 | | - |
228 | | -### Scenario Guides |
229 | | - |
230 | | -| Scenario | Guide | |
231 | | -|----------|-------| |
232 | | -| CLI Tools | [docs/scenarios/cli-tools.md](./docs/scenarios/cli-tools.md) | |
233 | | -| Desktop Apps | [docs/scenarios/desktop-apps.md](./docs/scenarios/desktop-apps.md) | |
234 | | -| IDE Plugins | [docs/scenarios/ide-plugins.md](./docs/scenarios/ide-plugins.md) | |
235 | | -| Web Backend | [docs/scenarios/web-backend.md](./docs/scenarios/web-backend.md) | |
236 | | -| Large-scale ToC | [docs/scenarios/large-scale-toc.md](./docs/scenarios/large-scale-toc.md) | |
237 | | - |
238 | | ---- |
| 121 | +See [docs/en/guides/architecture.md](./docs/en/guides/architecture.md) for detailed deployment guides. |
239 | 122 |
|
240 | 123 | ## Supported Providers |
241 | 124 |
|
242 | | -| Provider | Streaming | Tool Calling | Thinking/Reasoning | |
243 | | -|----------|-----------|--------------|-------------------| |
244 | | -| **Anthropic** | SSE | Native | Extended Thinking | |
245 | | -| **OpenAI** | SSE | Function Calling | o1/o3 reasoning | |
246 | | -| **Gemini** | SSE | Function Calling | thinkingLevel | |
247 | | -| **DeepSeek** | SSE | OpenAI-compatible | reasoning_content | |
248 | | -| **Qwen** | SSE | OpenAI-compatible | thinking_budget | |
249 | | -| **Groq/Cerebras** | SSE | OpenAI-compatible | - | |
250 | | - |
251 | | ---- |
252 | | - |
253 | | -## Roadmap |
| 125 | +| Provider | Streaming | Tools | Reasoning | Files | |
| 126 | +|----------|-----------|-------|-----------|-------| |
| 127 | +| Anthropic | ✅ | ✅ | ✅ Extended Thinking | ✅ | |
| 128 | +| OpenAI | ✅ | ✅ | ✅ | ✅ | |
| 129 | +| Gemini | ✅ | ✅ | ✅ | ✅ | |
254 | 130 |
|
255 | | -### v2.8 - Storage Foundation |
256 | | -- PostgresStore with connection pooling |
257 | | -- Distributed locking (Advisory Lock) |
258 | | -- Graceful shutdown support |
| 131 | +> **Note**: OpenAI-compatible services (DeepSeek, GLM, Qwen, Minimax, OpenRouter, etc.) can be used via `OpenAIProvider` with custom `baseURL` configuration. See [Providers Guide](./docs/en/guides/providers.md) for details. |
259 | 132 |
|
260 | | -### v3.0 - Performance |
261 | | -- Incremental message storage (append-only) |
262 | | -- Copy-on-Write fork optimization |
263 | | -- Event sampling and aggregation |
264 | | - |
265 | | -### v3.5 - Distributed |
266 | | -- Agent Scheduler with LRU caching |
267 | | -- Distributed EventBus (Redis Pub/Sub) |
268 | | -- Worker mode helpers |
269 | | - |
270 | | -See [docs/ROADMAP.md](./docs/ROADMAP.md) for the complete roadmap. |
271 | | - |
272 | | ---- |
273 | | - |
274 | | -## Contributing |
| 133 | +## Documentation |
275 | 134 |
|
276 | | -We welcome contributions! Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines. |
| 135 | +| Section | Description | |
| 136 | +|---------|-------------| |
| 137 | +| **Getting Started** | | |
| 138 | +| [Installation](./docs/en/getting-started/installation.md) | Setup and configuration | |
| 139 | +| [Quickstart](./docs/en/getting-started/quickstart.md) | Build your first Agent | |
| 140 | +| [Concepts](./docs/en/getting-started/concepts.md) | Core concepts explained | |
| 141 | +| **Guides** | | |
| 142 | +| [Events](./docs/en/guides/events.md) | Three-channel event system | |
| 143 | +| [Tools](./docs/en/guides/tools.md) | Built-in tools & custom tools | |
| 144 | +| [Providers](./docs/en/guides/providers.md) | Model provider configuration | |
| 145 | +| [Database](./docs/en/guides/database.md) | SQLite/PostgreSQL persistence | |
| 146 | +| [Resume & Fork](./docs/en/guides/resume-fork.md) | Crash recovery & branching | |
| 147 | +| **Reference** | | |
| 148 | +| [API Reference](./docs/en/reference/api.md) | Complete API documentation | |
| 149 | +| [Examples](./docs/en/examples/playbooks.md) | All examples explained | |
277 | 150 |
|
278 | 151 | ## License |
279 | 152 |
|
280 | | -MIT License - see [LICENSE](./LICENSE) for details. |
281 | | - |
282 | | ---- |
283 | | - |
284 | | -**KODE SDK** - *The runtime kernel that lets you build agents that persist, recover, and explore.* |
| 153 | +MIT |
0 commit comments