@blade-ai/agent-sdk 0.1.4 → 0.1.6
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/CHANGELOG.md +10 -0
- package/README.md +277 -27
- package/dist/index.js +138 -138
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
本文件记录 @blade-ai/agent-sdk 的所有重要变更。
|
|
4
4
|
|
|
5
|
+
## [0.1.6] - 2026-02-12
|
|
6
|
+
|
|
7
|
+
- d3708fe chore: update bun.lock
|
|
8
|
+
- d3754d3 fix: connectServer/reconnectServer 成功后显式设置 CONNECTED 状态
|
|
9
|
+
- 3acf9f2 feat(mcp): 添加对进程内MCP服务器的支持
|
|
10
|
+
- 8e01a9f test: 添加多个测试文件覆盖核心功能模块
|
|
11
|
+
- bf48a2f docs: 添加文档文件并更新README
|
|
12
|
+
## [0.1.5] - 2026-02-08
|
|
13
|
+
|
|
14
|
+
- 无相关变更
|
|
5
15
|
## [0.1.4] - 2026-02-08
|
|
6
16
|
|
|
7
17
|
- e65fad4 feat(session): 添加会话分叉功能
|
package/README.md
CHANGED
|
@@ -1,40 +1,72 @@
|
|
|
1
1
|
# Blade Agent SDK
|
|
2
2
|
|
|
3
|
-
面向 Node.js 与 TypeScript 的多轮会话 Agent SDK,提供标准的 send/stream
|
|
3
|
+
面向 Node.js 与 TypeScript 的多轮会话 Agent SDK,提供标准的 send/stream 会话模式、工具调用、会话恢复、文件检查点、沙箱执行与自动清理。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- 🔄 **多轮会话** - send/stream 模式,支持流式输出
|
|
8
|
+
- 🔧 **工具调用** - 内置文件、命令行、MCP 等工具
|
|
9
|
+
- 💾 **会话管理** - 恢复、分叉会话
|
|
10
|
+
- 📁 **文件检查点** - 追踪文件变更,支持回滚
|
|
11
|
+
- 🔒 **沙箱执行** - OS 级别安全隔离
|
|
12
|
+
- 🔌 **MCP 集成** - 支持 Model Context Protocol
|
|
13
|
+
- 🪝 **Hooks 系统** - 生命周期钩子,自定义行为
|
|
14
|
+
- 🎯 **结构化输出** - 支持 JSON Schema 输出格式
|
|
15
|
+
- 🤖 **自定义 Agent** - 定义专用子 Agent
|
|
4
16
|
|
|
5
17
|
## 安装
|
|
6
18
|
|
|
7
19
|
```bash
|
|
8
20
|
npm install @blade-ai/agent-sdk
|
|
21
|
+
# 或
|
|
22
|
+
pnpm add @blade-ai/agent-sdk
|
|
9
23
|
```
|
|
10
24
|
|
|
11
|
-
##
|
|
25
|
+
## 快速开始
|
|
12
26
|
|
|
13
27
|
```typescript
|
|
14
28
|
import { createSession } from '@blade-ai/agent-sdk';
|
|
15
29
|
|
|
16
30
|
const session = await createSession({
|
|
17
|
-
provider: { type: 'openai-compatible', apiKey: process.env.
|
|
31
|
+
provider: { type: 'openai-compatible', apiKey: process.env.API_KEY },
|
|
18
32
|
model: 'gpt-4o-mini',
|
|
19
33
|
});
|
|
20
34
|
|
|
21
|
-
await session.send('
|
|
35
|
+
await session.send('你好,你能帮我做什么?');
|
|
22
36
|
for await (const msg of session.stream()) {
|
|
23
37
|
if (msg.type === 'content') {
|
|
24
38
|
process.stdout.write(msg.delta);
|
|
25
39
|
}
|
|
26
40
|
}
|
|
41
|
+
|
|
42
|
+
session.close();
|
|
27
43
|
```
|
|
28
44
|
|
|
29
|
-
##
|
|
45
|
+
## 核心概念
|
|
46
|
+
|
|
47
|
+
### Send/Stream 模式
|
|
30
48
|
|
|
31
|
-
|
|
49
|
+
Blade Agent SDK 使用 send/stream 分离模式:
|
|
32
50
|
|
|
33
51
|
```typescript
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
52
|
+
// 1. 发送消息(非阻塞)
|
|
53
|
+
await session.send('分析这段代码');
|
|
54
|
+
|
|
55
|
+
// 2. 流式接收响应
|
|
56
|
+
for await (const msg of session.stream()) {
|
|
57
|
+
switch (msg.type) {
|
|
58
|
+
case 'content':
|
|
59
|
+
process.stdout.write(msg.delta);
|
|
60
|
+
break;
|
|
61
|
+
case 'tool_use':
|
|
62
|
+
console.log(`调用工具: ${msg.name}`);
|
|
63
|
+
break;
|
|
64
|
+
case 'tool_result':
|
|
65
|
+
console.log(`工具结果: ${msg.output}`);
|
|
66
|
+
break;
|
|
67
|
+
case 'result':
|
|
68
|
+
console.log('完成:', msg.subtype);
|
|
69
|
+
break;
|
|
38
70
|
}
|
|
39
71
|
}
|
|
40
72
|
```
|
|
@@ -45,11 +77,13 @@ for await (const msg of session.stream({ includeThinking: false })) {
|
|
|
45
77
|
import { prompt } from '@blade-ai/agent-sdk';
|
|
46
78
|
|
|
47
79
|
const result = await prompt('2+2 等于多少?', {
|
|
48
|
-
provider: { type: 'openai-compatible', apiKey: process.env.
|
|
80
|
+
provider: { type: 'openai-compatible', apiKey: process.env.API_KEY },
|
|
49
81
|
model: 'gpt-4o-mini',
|
|
50
82
|
});
|
|
51
83
|
|
|
52
|
-
console.log(result.result);
|
|
84
|
+
console.log(result.result); // 响应内容
|
|
85
|
+
console.log(result.usage); // Token 使用
|
|
86
|
+
console.log(result.duration); // 耗时(毫秒)
|
|
53
87
|
```
|
|
54
88
|
|
|
55
89
|
### 恢复会话
|
|
@@ -58,15 +92,34 @@ console.log(result.result);
|
|
|
58
92
|
import { resumeSession } from '@blade-ai/agent-sdk';
|
|
59
93
|
|
|
60
94
|
const session = await resumeSession({
|
|
61
|
-
sessionId: '
|
|
62
|
-
provider: { type: 'openai-compatible', apiKey: process.env.
|
|
95
|
+
sessionId: 'existing-session-id',
|
|
96
|
+
provider: { type: 'openai-compatible', apiKey: process.env.API_KEY },
|
|
63
97
|
model: 'gpt-4o-mini',
|
|
64
98
|
});
|
|
65
99
|
|
|
66
100
|
await session.send('继续上次的话题');
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### 分叉会话
|
|
104
|
+
|
|
105
|
+
从现有会话创建分支,保留历史但独立演进:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { forkSession } from '@blade-ai/agent-sdk';
|
|
109
|
+
|
|
110
|
+
// 方式1: 从 Session 实例分叉
|
|
111
|
+
const forkedSession = await session.fork();
|
|
112
|
+
|
|
113
|
+
// 方式2: 从特定消息点分叉
|
|
114
|
+
const forkedSession2 = await session.fork({ messageId: 'msg-uuid-123' });
|
|
115
|
+
|
|
116
|
+
// 方式3: 从已存储的会话 ID 分叉
|
|
117
|
+
const forkedSession3 = await forkSession({
|
|
118
|
+
sessionId: 'existing-session-id',
|
|
119
|
+
messageId: 'msg-uuid-456',
|
|
120
|
+
provider: { type: 'openai-compatible', apiKey: process.env.API_KEY },
|
|
121
|
+
model: 'gpt-4o-mini',
|
|
122
|
+
});
|
|
70
123
|
```
|
|
71
124
|
|
|
72
125
|
### 自动清理
|
|
@@ -74,36 +127,233 @@ for await (const msg of session.stream()) {
|
|
|
74
127
|
TypeScript 5.2+ 支持 `using` 自动清理:
|
|
75
128
|
|
|
76
129
|
```typescript
|
|
77
|
-
|
|
130
|
+
await using session = await createSession({ provider, model });
|
|
131
|
+
// 作用域结束时自动关闭会话
|
|
132
|
+
```
|
|
78
133
|
|
|
79
|
-
|
|
80
|
-
|
|
134
|
+
## 文件检查点
|
|
135
|
+
|
|
136
|
+
启用文件检查点功能,追踪 Agent 对文件的修改,支持回滚:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
const session = await createSession({
|
|
140
|
+
provider: { type: 'openai-compatible', apiKey: process.env.API_KEY },
|
|
81
141
|
model: 'gpt-4o-mini',
|
|
142
|
+
enableFileCheckpointing: true,
|
|
82
143
|
});
|
|
83
144
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
145
|
+
// Agent 执行文件操作
|
|
146
|
+
await session.send('请帮我重构 src/utils.ts 文件');
|
|
147
|
+
|
|
148
|
+
// 不满意时回滚到指定消息点
|
|
149
|
+
const result = await session.rewindFiles('user-message-uuid');
|
|
150
|
+
if (result.success) {
|
|
151
|
+
console.log('已恢复文件:', result.restoredFiles);
|
|
152
|
+
console.log('已删除文件:', result.deletedFiles);
|
|
87
153
|
}
|
|
154
|
+
|
|
155
|
+
// 查看检查点统计
|
|
156
|
+
const stats = session.getCheckpointStatistics();
|
|
157
|
+
console.log('检查点数量:', stats?.checkpointCount);
|
|
88
158
|
```
|
|
89
159
|
|
|
90
|
-
|
|
160
|
+
## 沙箱执行
|
|
161
|
+
|
|
162
|
+
启用沙箱功能,在安全隔离环境中执行命令:
|
|
91
163
|
|
|
92
164
|
```typescript
|
|
93
|
-
const session = await createSession({
|
|
94
|
-
|
|
165
|
+
const session = await createSession({
|
|
166
|
+
provider: { type: 'openai-compatible', apiKey: process.env.API_KEY },
|
|
167
|
+
model: 'gpt-4o-mini',
|
|
168
|
+
sandbox: {
|
|
169
|
+
enabled: true,
|
|
170
|
+
autoAllowBashIfSandboxed: true,
|
|
171
|
+
allowUnsandboxedCommands: ['git', 'npm'],
|
|
172
|
+
network: {
|
|
173
|
+
allowLocalBinding: true,
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
**支持的沙箱技术:**
|
|
180
|
+
- **Linux**: Bubblewrap (bwrap)
|
|
181
|
+
- **macOS**: Seatbelt (sandbox-exec)
|
|
182
|
+
|
|
183
|
+
## MCP 集成
|
|
184
|
+
|
|
185
|
+
连接 MCP (Model Context Protocol) 服务器:
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
const session = await createSession({
|
|
189
|
+
provider: { type: 'openai-compatible', apiKey: process.env.API_KEY },
|
|
190
|
+
model: 'gpt-4o-mini',
|
|
191
|
+
mcpServers: {
|
|
192
|
+
filesystem: {
|
|
193
|
+
type: 'stdio',
|
|
194
|
+
command: 'npx',
|
|
195
|
+
args: ['-y', '@anthropic-ai/mcp-server-filesystem', '/path/to/workspace'],
|
|
196
|
+
},
|
|
197
|
+
github: {
|
|
198
|
+
type: 'stdio',
|
|
199
|
+
command: 'npx',
|
|
200
|
+
args: ['-y', '@anthropic-ai/mcp-server-github'],
|
|
201
|
+
env: { GITHUB_TOKEN: process.env.GITHUB_TOKEN },
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// 查看 MCP 服务器状态
|
|
207
|
+
const status = await session.mcpServerStatus();
|
|
208
|
+
|
|
209
|
+
// 列出可用的 MCP 工具
|
|
210
|
+
const tools = await session.mcpListTools();
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## 权限控制
|
|
214
|
+
|
|
215
|
+
使用 `canUseTool` 回调控制工具权限:
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
const session = await createSession({
|
|
219
|
+
provider: { type: 'openai-compatible', apiKey: process.env.API_KEY },
|
|
220
|
+
model: 'gpt-4o-mini',
|
|
221
|
+
canUseTool: async (toolName, input, options) => {
|
|
222
|
+
// 只读工具自动批准
|
|
223
|
+
if (options.toolKind === 'readonly') {
|
|
224
|
+
return { behavior: 'allow' };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// 危险命令拒绝
|
|
228
|
+
if (toolName === 'Bash' && input.command?.includes('rm -rf')) {
|
|
229
|
+
return { behavior: 'deny', message: '禁止危险命令' };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// 其他情况询问用户
|
|
233
|
+
return { behavior: 'ask' };
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Hooks 系统
|
|
239
|
+
|
|
240
|
+
在 Agent 生命周期的特定点注入自定义逻辑:
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
const session = await createSession({
|
|
244
|
+
provider: { type: 'openai-compatible', apiKey: process.env.API_KEY },
|
|
245
|
+
model: 'gpt-4o-mini',
|
|
246
|
+
hooks: {
|
|
247
|
+
enabled: true,
|
|
248
|
+
PreToolUse: [
|
|
249
|
+
{
|
|
250
|
+
matcher: 'Bash',
|
|
251
|
+
hooks: [
|
|
252
|
+
{
|
|
253
|
+
type: 'command',
|
|
254
|
+
command: './scripts/validate-bash.sh',
|
|
255
|
+
timeout: 10,
|
|
256
|
+
},
|
|
257
|
+
],
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## 自定义工具
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
import { z } from 'zod';
|
|
268
|
+
|
|
269
|
+
const customTool = {
|
|
270
|
+
name: 'MyTool',
|
|
271
|
+
description: '自定义工具',
|
|
272
|
+
inputSchema: z.object({
|
|
273
|
+
query: z.string().describe('要处理的查询')
|
|
274
|
+
}),
|
|
275
|
+
execute: async (input, context) => {
|
|
276
|
+
return `处理结果: ${input.query}`;
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
const session = await createSession({
|
|
281
|
+
provider: { type: 'openai-compatible', apiKey: process.env.API_KEY },
|
|
282
|
+
model: 'gpt-4o-mini',
|
|
283
|
+
tools: [customTool],
|
|
284
|
+
});
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
## 自定义 Agent
|
|
288
|
+
|
|
289
|
+
```typescript
|
|
290
|
+
const searchAgent = {
|
|
291
|
+
name: 'search',
|
|
292
|
+
description: '搜索代码库中的信息',
|
|
293
|
+
systemPrompt: '你是一个代码搜索专家',
|
|
294
|
+
tools: ['Read', 'Grep', 'Glob'],
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
const session = await createSession({
|
|
298
|
+
provider: { type: 'openai-compatible', apiKey: process.env.API_KEY },
|
|
299
|
+
model: 'gpt-4o-mini',
|
|
300
|
+
agents: [searchAgent],
|
|
301
|
+
});
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
## 结构化输出
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
import { z } from 'zod';
|
|
308
|
+
|
|
309
|
+
const session = await createSession({
|
|
310
|
+
provider: { type: 'openai-compatible', apiKey: process.env.API_KEY },
|
|
311
|
+
model: 'gpt-4o-mini',
|
|
312
|
+
outputFormat: {
|
|
313
|
+
type: 'json_schema',
|
|
314
|
+
schema: z.object({
|
|
315
|
+
summary: z.string(),
|
|
316
|
+
confidence: z.number().min(0).max(1),
|
|
317
|
+
}),
|
|
318
|
+
name: 'AnalysisResult',
|
|
319
|
+
strict: true,
|
|
320
|
+
},
|
|
321
|
+
});
|
|
95
322
|
```
|
|
96
323
|
|
|
97
324
|
## 主要类型
|
|
98
325
|
|
|
99
326
|
```typescript
|
|
100
|
-
import type {
|
|
327
|
+
import type {
|
|
328
|
+
ISession,
|
|
329
|
+
SessionOptions,
|
|
330
|
+
StreamMessage,
|
|
331
|
+
PromptResult,
|
|
332
|
+
ForkOptions,
|
|
333
|
+
ForkSessionOptions,
|
|
334
|
+
RewindResult,
|
|
335
|
+
SandboxSettings,
|
|
336
|
+
McpServerConfig,
|
|
337
|
+
ToolDefinition,
|
|
338
|
+
AgentDefinition,
|
|
339
|
+
} from '@blade-ai/agent-sdk';
|
|
101
340
|
```
|
|
102
341
|
|
|
342
|
+
## 文档
|
|
343
|
+
|
|
344
|
+
- [API 参考](./docs/blade-agent-sdk.md) - 完整 API 文档
|
|
345
|
+
- [会话管理](./docs/session.md) - 会话 API 完整指南
|
|
346
|
+
- [文件检查点](./docs/checkpoint.md) - 文件变更追踪与回滚
|
|
347
|
+
- [沙箱功能](./docs/sandbox.md) - 安全隔离执行
|
|
348
|
+
- [MCP 集成](./docs/mcp.md) - Model Context Protocol 集成
|
|
349
|
+
- [Hooks 系统](./docs/hooks.md) - 生命周期钩子详解
|
|
350
|
+
|
|
103
351
|
## 运行环境
|
|
104
352
|
|
|
105
353
|
- Node.js >= 20
|
|
106
354
|
- TypeScript >= 5.2(可选,用于 `using` 自动清理)
|
|
355
|
+
- Linux: Bubblewrap(可选,用于沙箱)
|
|
356
|
+
- macOS: 内置 Seatbelt 支持
|
|
107
357
|
|
|
108
358
|
## 许可证
|
|
109
359
|
|