@shareai-lab/kode-sdk 1.0.0-beta.8 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +115 -273
- package/README.zh-CN.md +114 -0
- package/dist/core/agent/breakpoint-manager.d.ts +16 -0
- package/dist/core/agent/breakpoint-manager.js +36 -0
- package/dist/core/agent/message-queue.d.ts +26 -0
- package/dist/core/agent/message-queue.js +57 -0
- package/dist/core/agent/permission-manager.d.ts +9 -0
- package/dist/core/agent/permission-manager.js +32 -0
- package/dist/core/agent/todo-manager.d.ts +26 -0
- package/dist/core/agent/todo-manager.js +91 -0
- package/dist/core/agent/tool-runner.d.ts +9 -0
- package/dist/core/agent/tool-runner.js +45 -0
- package/dist/core/agent.d.ts +228 -62
- package/dist/core/agent.js +1890 -615
- package/dist/core/config.d.ts +10 -0
- package/dist/core/config.js +2 -0
- package/dist/core/context-manager.d.ts +82 -0
- package/dist/core/context-manager.js +241 -0
- package/dist/core/errors.d.ts +22 -0
- package/dist/core/errors.js +49 -0
- package/dist/core/events.d.ts +41 -10
- package/dist/core/events.js +270 -68
- package/dist/core/file-pool.d.ts +41 -0
- package/dist/core/file-pool.js +102 -0
- package/dist/core/hooks.d.ts +3 -3
- package/dist/core/hooks.js +1 -1
- package/dist/core/permission-modes.d.ts +31 -0
- package/dist/core/permission-modes.js +61 -0
- package/dist/core/pool.d.ts +56 -13
- package/dist/core/pool.js +244 -34
- package/dist/core/room.d.ts +2 -2
- package/dist/core/room.js +10 -10
- package/dist/core/scheduler.d.ts +30 -23
- package/dist/core/scheduler.js +42 -168
- package/dist/core/skills/index.d.ts +10 -0
- package/dist/core/skills/index.js +20 -0
- package/dist/core/skills/management-manager.d.ts +130 -0
- package/dist/core/skills/management-manager.js +557 -0
- package/dist/core/skills/manager.d.ts +47 -0
- package/dist/core/skills/manager.js +243 -0
- package/dist/core/skills/operation-queue.d.ts +87 -0
- package/dist/core/skills/operation-queue.js +113 -0
- package/dist/core/skills/sandbox-file-manager.d.ts +82 -0
- package/dist/core/skills/sandbox-file-manager.js +183 -0
- package/dist/core/skills/types.d.ts +120 -0
- package/dist/core/skills/types.js +9 -0
- package/dist/core/skills/xml-generator.d.ts +13 -0
- package/dist/core/skills/xml-generator.js +70 -0
- package/dist/core/template.d.ts +57 -0
- package/dist/core/template.js +35 -0
- package/dist/core/time-bridge.d.ts +18 -0
- package/dist/core/time-bridge.js +100 -0
- package/dist/core/todo.d.ts +34 -0
- package/dist/core/todo.js +89 -0
- package/dist/core/types.d.ts +311 -114
- package/dist/core/types.js +1 -12
- package/dist/index.d.ts +47 -9
- package/dist/index.js +108 -15
- package/dist/infra/db/postgres/postgres-store.d.ts +97 -0
- package/dist/infra/db/postgres/postgres-store.js +1073 -0
- package/dist/infra/db/sqlite/sqlite-store.d.ts +84 -0
- package/dist/infra/db/sqlite/sqlite-store.js +800 -0
- package/dist/infra/e2b/e2b-fs.d.ts +29 -0
- package/dist/infra/e2b/e2b-fs.js +128 -0
- package/dist/infra/e2b/e2b-sandbox.d.ts +37 -0
- package/dist/infra/e2b/e2b-sandbox.js +156 -0
- package/dist/infra/e2b/e2b-template.d.ts +24 -0
- package/dist/infra/e2b/e2b-template.js +105 -0
- package/dist/infra/e2b/index.d.ts +4 -0
- package/dist/infra/e2b/index.js +9 -0
- package/dist/infra/e2b/types.d.ts +46 -0
- package/dist/infra/e2b/types.js +2 -0
- package/dist/infra/provider.d.ts +17 -58
- package/dist/infra/provider.js +65 -116
- package/dist/infra/providers/anthropic.d.ts +42 -0
- package/dist/infra/providers/anthropic.js +308 -0
- package/dist/infra/providers/core/errors.d.ts +230 -0
- package/dist/infra/providers/core/errors.js +353 -0
- package/dist/infra/providers/core/fork.d.ts +106 -0
- package/dist/infra/providers/core/fork.js +418 -0
- package/dist/infra/providers/core/index.d.ts +10 -0
- package/dist/infra/providers/core/index.js +76 -0
- package/dist/infra/providers/core/logger.d.ts +186 -0
- package/dist/infra/providers/core/logger.js +191 -0
- package/dist/infra/providers/core/retry.d.ts +62 -0
- package/dist/infra/providers/core/retry.js +189 -0
- package/dist/infra/providers/core/usage.d.ts +151 -0
- package/dist/infra/providers/core/usage.js +376 -0
- package/dist/infra/providers/gemini.d.ts +49 -0
- package/dist/infra/providers/gemini.js +493 -0
- package/dist/infra/providers/index.d.ts +25 -0
- package/dist/infra/providers/index.js +83 -0
- package/dist/infra/providers/openai.d.ts +123 -0
- package/dist/infra/providers/openai.js +662 -0
- package/dist/infra/providers/types.d.ts +334 -0
- package/dist/infra/providers/types.js +20 -0
- package/dist/infra/providers/utils.d.ts +53 -0
- package/dist/infra/providers/utils.js +400 -0
- package/dist/infra/sandbox-factory.d.ts +13 -0
- package/dist/infra/sandbox-factory.js +30 -0
- package/dist/infra/sandbox.d.ts +35 -6
- package/dist/infra/sandbox.js +174 -8
- package/dist/infra/store/factory.d.ts +45 -0
- package/dist/infra/store/factory.js +80 -0
- package/dist/infra/store/index.d.ts +3 -0
- package/dist/infra/store/index.js +26 -0
- package/dist/infra/store/json-store.d.ts +67 -0
- package/dist/infra/store/json-store.js +606 -0
- package/dist/infra/store/types.d.ts +342 -0
- package/dist/infra/store/types.js +2 -0
- package/dist/infra/store.d.ts +12 -32
- package/dist/infra/store.js +27 -130
- package/dist/tools/bash_kill/index.d.ts +1 -0
- package/dist/tools/bash_kill/index.js +35 -0
- package/dist/tools/bash_kill/prompt.d.ts +2 -0
- package/dist/tools/bash_kill/prompt.js +14 -0
- package/dist/tools/bash_logs/index.d.ts +1 -0
- package/dist/tools/bash_logs/index.js +40 -0
- package/dist/tools/bash_logs/prompt.d.ts +2 -0
- package/dist/tools/bash_logs/prompt.js +14 -0
- package/dist/tools/bash_run/index.d.ts +16 -0
- package/dist/tools/bash_run/index.js +61 -0
- package/dist/tools/bash_run/prompt.d.ts +2 -0
- package/dist/tools/bash_run/prompt.js +18 -0
- package/dist/tools/builtin.d.ts +7 -13
- package/dist/tools/builtin.js +19 -90
- package/dist/tools/define.d.ts +101 -0
- package/dist/tools/define.js +214 -0
- package/dist/tools/fs_edit/index.d.ts +1 -0
- package/dist/tools/fs_edit/index.js +62 -0
- package/dist/tools/fs_edit/prompt.d.ts +2 -0
- package/dist/tools/fs_edit/prompt.js +15 -0
- package/dist/tools/fs_glob/index.d.ts +1 -0
- package/dist/tools/fs_glob/index.js +40 -0
- package/dist/tools/fs_glob/prompt.d.ts +2 -0
- package/dist/tools/fs_glob/prompt.js +15 -0
- package/dist/tools/fs_grep/index.d.ts +1 -0
- package/dist/tools/fs_grep/index.js +66 -0
- package/dist/tools/fs_grep/prompt.d.ts +2 -0
- package/dist/tools/fs_grep/prompt.js +16 -0
- package/dist/tools/fs_multi_edit/index.d.ts +1 -0
- package/dist/tools/fs_multi_edit/index.js +106 -0
- package/dist/tools/fs_multi_edit/prompt.d.ts +2 -0
- package/dist/tools/fs_multi_edit/prompt.js +16 -0
- package/dist/tools/fs_read/index.d.ts +1 -0
- package/dist/tools/fs_read/index.js +40 -0
- package/dist/tools/fs_read/prompt.d.ts +2 -0
- package/dist/tools/fs_read/prompt.js +16 -0
- package/dist/tools/fs_write/index.d.ts +1 -0
- package/dist/tools/fs_write/index.js +40 -0
- package/dist/tools/fs_write/prompt.d.ts +2 -0
- package/dist/tools/fs_write/prompt.js +15 -0
- package/dist/tools/index.d.ts +11 -0
- package/dist/tools/index.js +61 -0
- package/dist/tools/mcp.d.ts +69 -0
- package/dist/tools/mcp.js +185 -0
- package/dist/tools/registry.d.ts +29 -0
- package/dist/tools/registry.js +26 -0
- package/dist/tools/scripts.d.ts +22 -0
- package/dist/tools/scripts.js +205 -0
- package/dist/tools/skills.d.ts +20 -0
- package/dist/tools/skills.js +115 -0
- package/dist/tools/task_run/index.d.ts +7 -0
- package/dist/tools/task_run/index.js +58 -0
- package/dist/tools/task_run/prompt.d.ts +5 -0
- package/dist/tools/task_run/prompt.js +25 -0
- package/dist/tools/todo_read/index.d.ts +1 -0
- package/dist/tools/todo_read/index.js +29 -0
- package/dist/tools/todo_read/prompt.d.ts +2 -0
- package/dist/tools/todo_read/prompt.js +18 -0
- package/dist/tools/todo_write/index.d.ts +1 -0
- package/dist/tools/todo_write/index.js +42 -0
- package/dist/tools/todo_write/prompt.d.ts +2 -0
- package/dist/tools/todo_write/prompt.js +23 -0
- package/dist/tools/tool.d.ts +43 -0
- package/dist/tools/tool.js +211 -0
- package/dist/tools/toolkit.d.ts +69 -0
- package/dist/tools/toolkit.js +98 -0
- package/dist/tools/type-inference.d.ts +127 -0
- package/dist/tools/type-inference.js +207 -0
- package/dist/utils/agent-id.d.ts +1 -0
- package/dist/utils/agent-id.js +28 -0
- package/dist/utils/logger.d.ts +15 -0
- package/dist/utils/logger.js +44 -0
- package/dist/utils/session-id.js +16 -16
- package/package.json +35 -11
- package/dist/tools/bash.d.ts +0 -63
- package/dist/tools/bash.js +0 -92
- package/dist/tools/fs.d.ts +0 -96
- package/dist/tools/fs.js +0 -100
- package/dist/tools/task.d.ts +0 -38
- package/dist/tools/task.js +0 -45
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 shareAI-lab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,311 +1,153 @@
|
|
|
1
|
-
#
|
|
1
|
+
# KODE SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[English](./README.md) | [中文](./README.zh-CN.md)
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
Transform the experience of collaborating with colleagues into a minimal yet sufficient API for **sending messages, giving instructions, interrupting, forking, and resuming** with long-running online Agents.
|
|
5
|
+
> Event-driven, long-running AI Agent framework with enterprise-grade persistence and multi-agent collaboration.
|
|
8
6
|
|
|
9
7
|
## Features
|
|
10
8
|
|
|
11
|
-
- **Event-Driven
|
|
12
|
-
- **
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
- **
|
|
16
|
-
- **
|
|
17
|
-
- **Extensible**: MCP tools, Sandbox drivers, Provider adapters, Store backends, Scheduler DSL
|
|
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
|
+
- **Cloud Sandbox** - [E2B](https://e2b.dev) integration for isolated remote code execution
|
|
14
|
+
- **Extensible Ecosystem** - MCP tools, custom Providers, Skills system
|
|
18
15
|
|
|
19
16
|
## Quick Start
|
|
20
17
|
|
|
21
|
-
|
|
22
|
-
npm install kode-sdk
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
```typescript
|
|
26
|
-
import { Agent, JSONStore, LocalSandbox, AnthropicProvider, builtin } from 'kode-sdk';
|
|
27
|
-
|
|
28
|
-
const agent = new Agent({
|
|
29
|
-
sessionId: 'agent:assistant/session:demo',
|
|
30
|
-
provider: new AnthropicProvider(process.env.ANTHROPIC_API_KEY),
|
|
31
|
-
store: new JSONStore('./data'),
|
|
32
|
-
sandbox: LocalSandbox.local({ workDir: './workspace' }),
|
|
33
|
-
tools: [...builtin.fs({ workDir: './workspace' }), ...builtin.bash()],
|
|
34
|
-
system: 'You are a helpful assistant.',
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
// Send message
|
|
38
|
-
await agent.send('Please list all files in the workspace');
|
|
39
|
-
|
|
40
|
-
// Subscribe to events
|
|
41
|
-
for await (const event of agent.subscribe()) {
|
|
42
|
-
if (event.type === 'text') {
|
|
43
|
-
console.log('Agent:', event.text);
|
|
44
|
-
}
|
|
45
|
-
if (event.type === 'state' && event.state === 'READY') {
|
|
46
|
-
break;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
## Core Concepts
|
|
52
|
-
|
|
53
|
-
### 1. Agent States
|
|
54
|
-
|
|
55
|
-
- **READY**: Waiting for user input
|
|
56
|
-
- **BUSY**: Processing request or executing tools
|
|
57
|
-
- **PAUSED**: Waiting for permission approval
|
|
58
|
-
|
|
59
|
-
### 2. Safe-Fork-Points (SFP)
|
|
60
|
-
|
|
61
|
-
SFPs are created when:
|
|
62
|
-
- Tool results are written (`tool_result` blocks)
|
|
63
|
-
- Assistant provides text-only response (no tools)
|
|
64
|
-
|
|
65
|
-
Use SFPs to:
|
|
66
|
-
- Fork sessions at safe states
|
|
67
|
-
- Create bookmarks for rollback
|
|
68
|
-
- Branch conversations
|
|
69
|
-
|
|
70
|
-
### 3. Event System
|
|
71
|
-
|
|
72
|
-
**MINIMAL Event Kinds** (default subscription):
|
|
73
|
-
- `text_chunk`: Streaming text delta
|
|
74
|
-
- `text`: Complete text content
|
|
75
|
-
- `tool_use`: Tool invocation
|
|
76
|
-
- `tool_result`: Tool execution result
|
|
77
|
-
- `usage`: Token/cost metrics
|
|
78
|
-
- `error`: Typed errors
|
|
79
|
-
- `messages_update`: Message history changed
|
|
80
|
-
|
|
81
|
-
**Additional Events** (opt-in):
|
|
82
|
-
- `state`: Agent state changes
|
|
83
|
-
- `commit`: SFP created
|
|
84
|
-
- `permission_ask`: Approval required
|
|
85
|
-
- `permission_decision`: Approval result
|
|
86
|
-
- `resume`: Recovery from crash
|
|
87
|
-
- `forked`: New session created
|
|
88
|
-
|
|
89
|
-
### 4. Hooks
|
|
90
|
-
|
|
91
|
-
Intercept and modify tool execution:
|
|
92
|
-
|
|
93
|
-
```typescript
|
|
94
|
-
agent.use({
|
|
95
|
-
preToolUse(call, ctx) {
|
|
96
|
-
// Validate, modify args, or deny
|
|
97
|
-
if (!ctx.sandbox.fs.isInside(call.args.file)) {
|
|
98
|
-
return { decision: 'deny', reason: 'path out of sandbox' };
|
|
99
|
-
}
|
|
100
|
-
// Request approval
|
|
101
|
-
return { decision: 'ask', meta: { title: 'File Access', path: call.args.file } };
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
postToolUse(outcome, ctx) {
|
|
105
|
-
// Trim large results
|
|
106
|
-
if (String(outcome.content).length > 100_000) {
|
|
107
|
-
const path = ctx.sandbox.fs.temp(`tool-${outcome.id}.log`);
|
|
108
|
-
ctx.sandbox.fs.write(path, outcome.content);
|
|
109
|
-
return { update: { content: `[Full output at ./${path}]` } };
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
});
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
### 5. Scheduler
|
|
116
|
-
|
|
117
|
-
Time-based and step-based triggers:
|
|
118
|
-
|
|
119
|
-
```typescript
|
|
120
|
-
agent.schedule()
|
|
121
|
-
.every('10m', () => agent.send('Status check'))
|
|
122
|
-
.everySteps(20, () => agent.send('Reminder: review security guidelines'))
|
|
123
|
-
.daily('09:00', () => agent.send('Daily report'))
|
|
124
|
-
.weekly('Mon 09:00', () => agent.send('Weekly summary'));
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### 6. AgentPool
|
|
128
|
-
|
|
129
|
-
Manage multiple agent instances:
|
|
130
|
-
|
|
131
|
-
```typescript
|
|
132
|
-
const pool = new AgentPool({
|
|
133
|
-
store: new JSONStore('./data'),
|
|
134
|
-
maxAgents: 50,
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
const agent = pool.create(sessionId, template, options);
|
|
138
|
-
const existing = pool.get(sessionId);
|
|
139
|
-
const agents = pool.list({ prefix: 'org:acme/' });
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
### 7. Room (Group Chat)
|
|
18
|
+
**One-liner setup** (install dependencies and build):
|
|
143
19
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
```typescript
|
|
147
|
-
const room = new Room(pool);
|
|
148
|
-
room.join('alice', 'agent:pm/session:alice');
|
|
149
|
-
room.join('bob', 'agent:dev/session:bob');
|
|
150
|
-
|
|
151
|
-
// Direct mention
|
|
152
|
-
await room.say('alice', '@bob Please review the PR');
|
|
153
|
-
|
|
154
|
-
// Broadcast (excludes sender)
|
|
155
|
-
await room.say('alice', 'Meeting at 3pm');
|
|
20
|
+
```bash
|
|
21
|
+
./quickstart.sh
|
|
156
22
|
```
|
|
157
23
|
|
|
158
|
-
|
|
24
|
+
Or install as a dependency:
|
|
159
25
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
```typescript
|
|
163
|
-
builtin.fs({ base: './workspace' })
|
|
26
|
+
```bash
|
|
27
|
+
npm install @shareai-lab/kode-sdk
|
|
164
28
|
```
|
|
165
29
|
|
|
166
|
-
|
|
167
|
-
- `Fs.Write`: Write/create files
|
|
168
|
-
- `Fs.Edit`: Replace text in files
|
|
30
|
+
Set environment variables:
|
|
169
31
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
approval: true,
|
|
177
|
-
})
|
|
32
|
+
<!-- tabs:start -->
|
|
33
|
+
#### **Linux / macOS**
|
|
34
|
+
```bash
|
|
35
|
+
export ANTHROPIC_API_KEY=sk-...
|
|
36
|
+
export ANTHROPIC_MODEL_ID=claude-sonnet-4-20250514 # optional, default: claude-sonnet-4-20250514
|
|
37
|
+
export ANTHROPIC_BASE_URL=https://api.anthropic.com # optional, default: https://api.anthropic.com
|
|
178
38
|
```
|
|
179
39
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
```typescript
|
|
187
|
-
builtin.task({ subAgents: [FrontendAssistant, BackendAssistant] })
|
|
40
|
+
#### **Windows (PowerShell)**
|
|
41
|
+
```powershell
|
|
42
|
+
$env:ANTHROPIC_API_KEY="sk-..."
|
|
43
|
+
$env:ANTHROPIC_MODEL_ID="claude-sonnet-4-20250514" # optional, default: claude-sonnet-4-20250514
|
|
44
|
+
$env:ANTHROPIC_BASE_URL="https://api.anthropic.com" # optional, default: https://api.anthropic.com
|
|
188
45
|
```
|
|
46
|
+
<!-- tabs:end -->
|
|
189
47
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
## API Reference
|
|
193
|
-
|
|
194
|
-
### Agent
|
|
48
|
+
Minimal example:
|
|
195
49
|
|
|
196
50
|
```typescript
|
|
197
|
-
|
|
198
|
-
send(text: string): Promise<string>
|
|
199
|
-
|
|
200
|
-
// Subscribe to events
|
|
201
|
-
subscribe(opts?: { since?: number; kinds?: AgentEventKind[] }): AsyncIterable<AgentEvent>
|
|
202
|
-
|
|
203
|
-
// Convenience: send + subscribe
|
|
204
|
-
chat(text: string): AsyncIterable<AgentEvent>
|
|
205
|
-
|
|
206
|
-
// Blocking: wait for complete response
|
|
207
|
-
reply(text: string): Promise<string>
|
|
208
|
-
|
|
209
|
-
// One-off LLM query
|
|
210
|
-
askLLM(text: string, opts?): Promise<{ text: string; sessionId: string }>
|
|
51
|
+
import { Agent, AnthropicProvider, JSONStore } from '@shareai-lab/kode-sdk';
|
|
211
52
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
53
|
+
const provider = new AnthropicProvider(
|
|
54
|
+
process.env.ANTHROPIC_API_KEY!,
|
|
55
|
+
process.env.ANTHROPIC_MODEL_ID
|
|
56
|
+
);
|
|
215
57
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
58
|
+
const agent = await Agent.create({
|
|
59
|
+
provider,
|
|
60
|
+
store: new JSONStore('./.kode'),
|
|
61
|
+
systemPrompt: 'You are a helpful assistant.'
|
|
62
|
+
});
|
|
219
63
|
|
|
220
|
-
//
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
64
|
+
// Subscribe to progress events
|
|
65
|
+
for await (const envelope of agent.subscribe(['progress'])) {
|
|
66
|
+
if (envelope.event.type === 'text_chunk') {
|
|
67
|
+
process.stdout.write(envelope.event.delta);
|
|
68
|
+
}
|
|
69
|
+
if (envelope.event.type === 'done') break;
|
|
70
|
+
}
|
|
224
71
|
|
|
225
|
-
|
|
226
|
-
use(hooks: Hooks): this
|
|
227
|
-
getHooks(): ReadonlyArray<RegisteredHook>
|
|
228
|
-
registerTools(tools: Tool[]): this
|
|
229
|
-
schedule(): AgentSchedulerHandle
|
|
230
|
-
on(event: 'permission_ask' | 'error' | 'messages_update', handler: Function): this
|
|
72
|
+
await agent.send('Hello!');
|
|
231
73
|
```
|
|
232
74
|
|
|
233
|
-
|
|
75
|
+
Run examples:
|
|
234
76
|
|
|
235
|
-
```
|
|
236
|
-
|
|
77
|
+
```bash
|
|
78
|
+
npm run example:getting-started # Minimal chat
|
|
79
|
+
npm run example:agent-inbox # Event-driven inbox
|
|
80
|
+
npm run example:approval # Tool approval workflow
|
|
81
|
+
npm run example:room # Multi-agent collaboration
|
|
237
82
|
```
|
|
238
83
|
|
|
239
|
-
|
|
240
|
-
- `agent:assistant/session:abc123`
|
|
241
|
-
- `org:acme/team:eng/user:42/agent:pm/session:xyz789`
|
|
242
|
-
- `agent:dev/session:main/fork:branch1/fork:branch2`
|
|
84
|
+
## Architecture for Scale
|
|
243
85
|
|
|
244
|
-
|
|
245
|
-
- `{sessionId}@sfp:{index}`
|
|
246
|
-
- `{sessionId}@label:{slug}`
|
|
247
|
-
|
|
248
|
-
## Examples
|
|
249
|
-
|
|
250
|
-
See `examples/` directory:
|
|
251
|
-
|
|
252
|
-
- **U1**: Next.js backend (send + subscribe via SSE)
|
|
253
|
-
- **U2**: Permission approval flow
|
|
254
|
-
- **U3**: Hook for path guard and result trimming
|
|
255
|
-
- **U4**: Scheduler with time and step triggers
|
|
256
|
-
- **U5**: Sub-agent task delegation
|
|
257
|
-
- **U6**: Room group chat
|
|
258
|
-
- **U7**: ChatDev team collaboration
|
|
259
|
-
|
|
260
|
-
## Architecture
|
|
86
|
+
For production deployments serving many users, we recommend the **Worker Microservice Pattern**:
|
|
261
87
|
|
|
262
88
|
```
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
89
|
+
+------------------+
|
|
90
|
+
| Frontend | Next.js / SvelteKit (Vercel OK)
|
|
91
|
+
+--------+---------+
|
|
92
|
+
|
|
|
93
|
+
+--------v---------+
|
|
94
|
+
| API Gateway | Auth, routing, queue push
|
|
95
|
+
+--------+---------+
|
|
96
|
+
|
|
|
97
|
+
+--------v---------+
|
|
98
|
+
| Message Queue | Redis / SQS / NATS
|
|
99
|
+
+--------+---------+
|
|
100
|
+
|
|
|
101
|
+
+--------------------+--------------------+
|
|
102
|
+
| | |
|
|
103
|
+
+--------v-------+ +--------v-------+ +--------v-------+
|
|
104
|
+
| Worker 1 | | Worker 2 | | Worker N |
|
|
105
|
+
| (KODE SDK) | | (KODE SDK) | | (KODE SDK) |
|
|
106
|
+
| Long-running | | Long-running | | Long-running |
|
|
107
|
+
+--------+-------+ +--------+-------+ +--------+-------+
|
|
108
|
+
| | |
|
|
109
|
+
+--------------------+--------------------+
|
|
110
|
+
|
|
|
111
|
+
+--------v---------+
|
|
112
|
+
| Distributed Store| PostgreSQL / Redis
|
|
113
|
+
+------------------+
|
|
276
114
|
```
|
|
277
115
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
-
|
|
302
|
-
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
116
|
+
**Key Principles:**
|
|
117
|
+
1. **API layer is stateless** - Can run on serverless
|
|
118
|
+
2. **Workers are stateful** - Run KODE SDK, need long-running processes
|
|
119
|
+
3. **Store is shared** - Single source of truth for agent state
|
|
120
|
+
4. **Queue decouples** - Request handling from agent execution
|
|
121
|
+
|
|
122
|
+
See [docs/en/guides/architecture.md](./docs/en/guides/architecture.md) for detailed deployment guides.
|
|
123
|
+
|
|
124
|
+
## Supported Providers
|
|
125
|
+
|
|
126
|
+
| Provider | Streaming | Tools | Reasoning | Files |
|
|
127
|
+
|----------|-----------|-------|-----------|-------|
|
|
128
|
+
| Anthropic | ✅ | ✅ | ✅ Extended Thinking | ✅ |
|
|
129
|
+
| OpenAI | ✅ | ✅ | ✅ | ✅ |
|
|
130
|
+
| Gemini | ✅ | ✅ | ✅ | ✅ |
|
|
131
|
+
|
|
132
|
+
> **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.
|
|
133
|
+
|
|
134
|
+
## Documentation
|
|
135
|
+
|
|
136
|
+
| Section | Description |
|
|
137
|
+
|---------|-------------|
|
|
138
|
+
| **Getting Started** | |
|
|
139
|
+
| [Installation](./docs/en/getting-started/installation.md) | Setup and configuration |
|
|
140
|
+
| [Quickstart](./docs/en/getting-started/quickstart.md) | Build your first Agent |
|
|
141
|
+
| [Concepts](./docs/en/getting-started/concepts.md) | Core concepts explained |
|
|
142
|
+
| **Guides** | |
|
|
143
|
+
| [Events](./docs/en/guides/events.md) | Three-channel event system |
|
|
144
|
+
| [Tools](./docs/en/guides/tools.md) | Built-in tools & custom tools |
|
|
145
|
+
| [Providers](./docs/en/guides/providers.md) | Model provider configuration |
|
|
146
|
+
| [Database](./docs/en/guides/database.md) | SQLite/PostgreSQL persistence |
|
|
147
|
+
| [Resume & Fork](./docs/en/guides/resume-fork.md) | Crash recovery & branching |
|
|
148
|
+
| **Reference** | |
|
|
149
|
+
| [API Reference](./docs/en/reference/api.md) | Complete API documentation |
|
|
150
|
+
| [Examples](./docs/en/examples/playbooks.md) | All examples explained |
|
|
309
151
|
|
|
310
152
|
## License
|
|
311
153
|
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# KODE SDK
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | [中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
> 事件驱动的长时运行 AI Agent 框架,支持企业级持久化和多 Agent 协作。
|
|
6
|
+
|
|
7
|
+
## 核心特性
|
|
8
|
+
|
|
9
|
+
- **事件驱动架构** - 三通道系统 (Progress/Control/Monitor) 清晰分离关注点
|
|
10
|
+
- **长时运行与恢复** - 七段断点机制,支持 Safe-Fork-Point 崩溃恢复
|
|
11
|
+
- **多 Agent 协作** - AgentPool、Room 消息、任务委派
|
|
12
|
+
- **企业级持久化** - 支持 SQLite/PostgreSQL,统一 WAL 日志
|
|
13
|
+
- **云端沙箱** - 集成 [E2B](https://e2b.dev),提供隔离的远程代码执行环境
|
|
14
|
+
- **可扩展生态** - MCP 工具、自定义 Provider、Skills 系统
|
|
15
|
+
|
|
16
|
+
## 快速开始
|
|
17
|
+
|
|
18
|
+
**一键启动**(安装依赖并构建):
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
./quickstart.sh
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
或作为依赖安装:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @shareai-lab/kode-sdk
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
设置环境变量:
|
|
31
|
+
|
|
32
|
+
<!-- tabs:start -->
|
|
33
|
+
#### **Linux / macOS**
|
|
34
|
+
```bash
|
|
35
|
+
export ANTHROPIC_API_KEY=sk-...
|
|
36
|
+
export ANTHROPIC_MODEL_ID=claude-sonnet-4-20250514 # 可选,默认: claude-sonnet-4-20250514
|
|
37
|
+
export ANTHROPIC_BASE_URL=https://api.anthropic.com # 可选,默认: https://api.anthropic.com
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
#### **Windows (PowerShell)**
|
|
41
|
+
```powershell
|
|
42
|
+
$env:ANTHROPIC_API_KEY="sk-..."
|
|
43
|
+
$env:ANTHROPIC_MODEL_ID="claude-sonnet-4-20250514" # 可选,默认: claude-sonnet-4-20250514
|
|
44
|
+
$env:ANTHROPIC_BASE_URL="https://api.anthropic.com" # 可选,默认: https://api.anthropic.com
|
|
45
|
+
```
|
|
46
|
+
<!-- tabs:end -->
|
|
47
|
+
|
|
48
|
+
最简示例:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { Agent, AnthropicProvider, JSONStore } from '@shareai-lab/kode-sdk';
|
|
52
|
+
|
|
53
|
+
const provider = new AnthropicProvider(
|
|
54
|
+
process.env.ANTHROPIC_API_KEY!,
|
|
55
|
+
process.env.ANTHROPIC_MODEL_ID
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const agent = await Agent.create({
|
|
59
|
+
provider,
|
|
60
|
+
store: new JSONStore('./.kode'),
|
|
61
|
+
systemPrompt: '你是一个乐于助人的助手。'
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// 订阅 progress 事件
|
|
65
|
+
for await (const envelope of agent.subscribe(['progress'])) {
|
|
66
|
+
if (envelope.event.type === 'text_chunk') {
|
|
67
|
+
process.stdout.write(envelope.event.delta);
|
|
68
|
+
}
|
|
69
|
+
if (envelope.event.type === 'done') break;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
await agent.send('你好!');
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
运行示例:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
npm run example:getting-started # 最简对话
|
|
79
|
+
npm run example:agent-inbox # 事件驱动收件箱
|
|
80
|
+
npm run example:approval # 工具审批流程
|
|
81
|
+
npm run example:room # 多Agent协作
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## 支持的 Provider
|
|
85
|
+
|
|
86
|
+
| Provider | 流式输出 | 工具调用 | 推理 | 文件 |
|
|
87
|
+
|----------|---------|---------|------|------|
|
|
88
|
+
| Anthropic | ✅ | ✅ | ✅ Extended Thinking | ✅ |
|
|
89
|
+
| OpenAI | ✅ | ✅ | ✅ | ✅ |
|
|
90
|
+
| Gemini | ✅ | ✅ | ✅ | ✅ |
|
|
91
|
+
|
|
92
|
+
> **说明**:OpenAI 兼容的服务(DeepSeek、GLM、Qwen、Minimax、OpenRouter 等)可以通过 `OpenAIProvider` 配置自定义 `baseURL` 来使用。详见 [Provider 配置指南](./docs/zh-CN/guides/providers.md)。
|
|
93
|
+
|
|
94
|
+
## 文档
|
|
95
|
+
|
|
96
|
+
| 章节 | 说明 |
|
|
97
|
+
|------|------|
|
|
98
|
+
| **入门指南** | |
|
|
99
|
+
| [安装配置](./docs/zh-CN/getting-started/installation.md) | 环境配置与安装 |
|
|
100
|
+
| [快速上手](./docs/zh-CN/getting-started/quickstart.md) | 创建第一个 Agent |
|
|
101
|
+
| [核心概念](./docs/zh-CN/getting-started/concepts.md) | 核心概念详解 |
|
|
102
|
+
| **使用指南** | |
|
|
103
|
+
| [事件系统](./docs/zh-CN/guides/events.md) | 三通道事件系统 |
|
|
104
|
+
| [工具系统](./docs/zh-CN/guides/tools.md) | 内置工具与自定义工具 |
|
|
105
|
+
| [Provider 配置](./docs/zh-CN/guides/providers.md) | 模型 Provider 配置 |
|
|
106
|
+
| [数据库存储](./docs/zh-CN/guides/database.md) | SQLite/PostgreSQL 持久化 |
|
|
107
|
+
| [恢复与分叉](./docs/zh-CN/guides/resume-fork.md) | 崩溃恢复与分支 |
|
|
108
|
+
| **参考** | |
|
|
109
|
+
| [API 参考](./docs/zh-CN/reference/api.md) | 完整 API 文档 |
|
|
110
|
+
| [示例集](./docs/zh-CN/examples/playbooks.md) | 所有示例详解 |
|
|
111
|
+
|
|
112
|
+
## 许可证
|
|
113
|
+
|
|
114
|
+
MIT
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BreakpointState } from '../types';
|
|
2
|
+
export interface BreakpointEntry {
|
|
3
|
+
state: BreakpointState;
|
|
4
|
+
timestamp: number;
|
|
5
|
+
note?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class BreakpointManager {
|
|
8
|
+
private readonly onChange?;
|
|
9
|
+
private current;
|
|
10
|
+
private history;
|
|
11
|
+
constructor(onChange?: ((previous: BreakpointState, next: BreakpointState, entry: BreakpointEntry) => void) | undefined);
|
|
12
|
+
getCurrent(): BreakpointState;
|
|
13
|
+
getHistory(): ReadonlyArray<BreakpointEntry>;
|
|
14
|
+
set(state: BreakpointState, note?: string): void;
|
|
15
|
+
reset(state?: BreakpointState): void;
|
|
16
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BreakpointManager = void 0;
|
|
4
|
+
class BreakpointManager {
|
|
5
|
+
constructor(onChange) {
|
|
6
|
+
this.onChange = onChange;
|
|
7
|
+
this.current = 'READY';
|
|
8
|
+
this.history = [];
|
|
9
|
+
}
|
|
10
|
+
getCurrent() {
|
|
11
|
+
return this.current;
|
|
12
|
+
}
|
|
13
|
+
getHistory() {
|
|
14
|
+
return this.history;
|
|
15
|
+
}
|
|
16
|
+
set(state, note) {
|
|
17
|
+
if (this.current === state)
|
|
18
|
+
return;
|
|
19
|
+
const entry = {
|
|
20
|
+
state,
|
|
21
|
+
timestamp: Date.now(),
|
|
22
|
+
note,
|
|
23
|
+
};
|
|
24
|
+
const previous = this.current;
|
|
25
|
+
this.current = state;
|
|
26
|
+
this.history.push(entry);
|
|
27
|
+
if (this.onChange) {
|
|
28
|
+
this.onChange(previous, state, entry);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
reset(state = 'READY') {
|
|
32
|
+
this.current = state;
|
|
33
|
+
this.history = [];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.BreakpointManager = BreakpointManager;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ContentBlock, Message } from '../types';
|
|
2
|
+
import { ReminderOptions } from '../types';
|
|
3
|
+
export type PendingKind = 'user' | 'reminder';
|
|
4
|
+
export interface PendingMessage {
|
|
5
|
+
message: Message;
|
|
6
|
+
kind: PendingKind;
|
|
7
|
+
metadata?: Record<string, any>;
|
|
8
|
+
}
|
|
9
|
+
export interface SendOptions {
|
|
10
|
+
kind?: PendingKind;
|
|
11
|
+
metadata?: Record<string, any>;
|
|
12
|
+
reminder?: ReminderOptions;
|
|
13
|
+
}
|
|
14
|
+
export interface MessageQueueOptions {
|
|
15
|
+
wrapReminder(content: string, options?: ReminderOptions): string;
|
|
16
|
+
addMessage(message: Message, kind: PendingKind): void;
|
|
17
|
+
persist(): Promise<void>;
|
|
18
|
+
ensureProcessing(): void;
|
|
19
|
+
}
|
|
20
|
+
export declare class MessageQueue {
|
|
21
|
+
private readonly options;
|
|
22
|
+
private pending;
|
|
23
|
+
constructor(options: MessageQueueOptions);
|
|
24
|
+
send(content: string | ContentBlock[], opts?: SendOptions): string;
|
|
25
|
+
flush(): Promise<void>;
|
|
26
|
+
}
|