@malette/agent-sdk 0.1.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/README.md +404 -0
- package/dist/index.js +21040 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +20899 -0
- package/dist/index.mjs.map +1 -0
- package/dist/styles.css +307 -0
- package/package.json +63 -0
package/README.md
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
# @malette/agent-sdk
|
|
2
|
+
|
|
3
|
+
Agent SDK for Malette - Complete agent conversation capabilities
|
|
4
|
+
|
|
5
|
+
## 介绍
|
|
6
|
+
|
|
7
|
+
`@malette/agent-sdk` 是一个专注于 **Agent 对话交互** 的端到端 SDK,提供了完整的会话管理、消息流式传输、工具确认、Plan Mode 等对话相关功能。
|
|
8
|
+
|
|
9
|
+
### 设计边界
|
|
10
|
+
|
|
11
|
+
**SDK 包含:**
|
|
12
|
+
- ✅ 会话管理(创建、列表、消息历史)
|
|
13
|
+
- ✅ 消息发送与流式传输(SSE)
|
|
14
|
+
- ✅ 工具确认与批量确认
|
|
15
|
+
- ✅ Plan Mode(计划生成、确认、执行、人工输入)
|
|
16
|
+
- ✅ Multi-Agent 思考过程展示
|
|
17
|
+
- ✅ 文件上传与媒体处理
|
|
18
|
+
- ✅ 会话分享与回放
|
|
19
|
+
- ✅ 完整的 UI 组件(ChatWindow, MessageBubble 等)
|
|
20
|
+
|
|
21
|
+
**SDK 不包含:**
|
|
22
|
+
- ❌ Agent CRUD(创建、更新、删除 Agent)
|
|
23
|
+
- ❌ Tool CRUD(创建、更新、删除工具)
|
|
24
|
+
- ❌ Agent-Tool 绑定管理
|
|
25
|
+
|
|
26
|
+
> **设计决策**:Agent/Tool 管理属于配置层面,对话交互属于运行时层面。将两者分离可以让 SDK 更专注于对话体验,同时保持灵活性。
|
|
27
|
+
|
|
28
|
+
## 安装
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install @malette/agent-sdk
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## 快速开始
|
|
35
|
+
|
|
36
|
+
### 1. 使用通用 AgentChat 组件(推荐)
|
|
37
|
+
|
|
38
|
+
最简单的方式是直接使用 `AgentChat` 组件,它包含了完整的会话管理、对话、分享和回放功能:
|
|
39
|
+
|
|
40
|
+
```tsx
|
|
41
|
+
'use client';
|
|
42
|
+
|
|
43
|
+
import { AgentChat } from '@malette/agent-sdk';
|
|
44
|
+
|
|
45
|
+
export default function AgentPage() {
|
|
46
|
+
return (
|
|
47
|
+
<AgentChat
|
|
48
|
+
agentId="your-agent-id"
|
|
49
|
+
projectId="your-project-id"
|
|
50
|
+
agentName="我的 Agent"
|
|
51
|
+
agentType="BUILTIN"
|
|
52
|
+
welcomeMessage="你好!我是你的 AI 助手"
|
|
53
|
+
tools={[]}
|
|
54
|
+
onNavigateBack={() => window.history.back()}
|
|
55
|
+
onNavigateToSettings={() => window.location.href = '/settings'}
|
|
56
|
+
onNavigateToReplay={(sessionId) => {
|
|
57
|
+
window.location.href = `/replay?sessionId=${sessionId}`;
|
|
58
|
+
}}
|
|
59
|
+
/>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 2. 使用 ShareReplayPage 组件
|
|
65
|
+
|
|
66
|
+
用于分享回放页面:
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
'use client';
|
|
70
|
+
|
|
71
|
+
import { ShareReplayPage } from '@malette/agent-sdk';
|
|
72
|
+
|
|
73
|
+
export default function SharePage({ params }: { params: { shareId: string } }) {
|
|
74
|
+
return (
|
|
75
|
+
<ShareReplayPage
|
|
76
|
+
shareId={params.shareId}
|
|
77
|
+
onNavigateBack={() => window.history.back()}
|
|
78
|
+
/>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 3. 基础对话
|
|
84
|
+
|
|
85
|
+
如果你需要更细粒度的控制,可以使用底层组件和服务:
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
'use client';
|
|
89
|
+
|
|
90
|
+
import {
|
|
91
|
+
useAgentStore,
|
|
92
|
+
sessionService,
|
|
93
|
+
messageService,
|
|
94
|
+
useSSE,
|
|
95
|
+
ChatWindow,
|
|
96
|
+
createAndStartSession
|
|
97
|
+
} from '@malette/agent-sdk';
|
|
98
|
+
|
|
99
|
+
export default function Playground() {
|
|
100
|
+
const { currentSession, setCurrentSession, addSession } = useAgentStore();
|
|
101
|
+
const { sendMessage } = useSSE();
|
|
102
|
+
|
|
103
|
+
// 创建会话
|
|
104
|
+
const handleNew = async () => {
|
|
105
|
+
const session = await createAndStartSession(projectId, agentId, '新对话');
|
|
106
|
+
if (session) {
|
|
107
|
+
addSession(session);
|
|
108
|
+
setCurrentSession(session);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// 发送消息
|
|
113
|
+
const handleSend = async (content: string) => {
|
|
114
|
+
if (currentSession) {
|
|
115
|
+
await sendMessage(currentSession.sessionId, content);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<div>
|
|
121
|
+
<button onClick={handleNew}>新建对话</button>
|
|
122
|
+
{currentSession && (
|
|
123
|
+
<ChatWindow sessionId={currentSession.sessionId} />
|
|
124
|
+
)}
|
|
125
|
+
</div>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 2. 使用组件
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
import {
|
|
134
|
+
ChatWindow,
|
|
135
|
+
MessageBubble,
|
|
136
|
+
ToolConfirmDialog,
|
|
137
|
+
PlanConfirmDialog,
|
|
138
|
+
ShareModal
|
|
139
|
+
} from '@malette/agent-sdk';
|
|
140
|
+
|
|
141
|
+
// 完整的对话窗口
|
|
142
|
+
<ChatWindow sessionId="xxx" />
|
|
143
|
+
|
|
144
|
+
// 单个消息气泡
|
|
145
|
+
<MessageBubble message={message} isStreaming={false} />
|
|
146
|
+
|
|
147
|
+
// 工具确认弹窗
|
|
148
|
+
<ToolConfirmDialog
|
|
149
|
+
open={showConfirm}
|
|
150
|
+
onConfirm={handleConfirm}
|
|
151
|
+
onCancel={handleCancel}
|
|
152
|
+
/>
|
|
153
|
+
|
|
154
|
+
// Plan 确认弹窗
|
|
155
|
+
<PlanConfirmDialog
|
|
156
|
+
plan={currentPlan}
|
|
157
|
+
onConfirm={handlePlanConfirm}
|
|
158
|
+
onReject={handlePlanReject}
|
|
159
|
+
/>
|
|
160
|
+
|
|
161
|
+
// 分享弹窗
|
|
162
|
+
<ShareModal
|
|
163
|
+
sessionId="xxx"
|
|
164
|
+
isOpen={shareOpen}
|
|
165
|
+
onClose={() => setShareOpen(false)}
|
|
166
|
+
/>
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### 3. 文件上传
|
|
170
|
+
|
|
171
|
+
```tsx
|
|
172
|
+
import { uploadOSS, getFileUrl, getImageUrl } from '@malette/agent-sdk';
|
|
173
|
+
|
|
174
|
+
// 上传文件到 OSS
|
|
175
|
+
const handleUpload = async (file: File) => {
|
|
176
|
+
const result = await uploadOSS(file);
|
|
177
|
+
const url = await getFileUrl(result.key);
|
|
178
|
+
console.log('文件 URL:', url);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// 获取图片缩略图
|
|
182
|
+
const imageUrl = getImageUrl('file-key');
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## API 概览
|
|
186
|
+
|
|
187
|
+
### 服务层
|
|
188
|
+
|
|
189
|
+
#### Session 服务
|
|
190
|
+
```typescript
|
|
191
|
+
sessionService.create(data) // 创建会话
|
|
192
|
+
sessionService.get(sessionId) // 获取会话详情
|
|
193
|
+
sessionService.update(sessionId, data) // 更新会话
|
|
194
|
+
sessionService.delete(sessionId) // 删除会话
|
|
195
|
+
sessionService.list(params) // 获取会话列表
|
|
196
|
+
sessionService.getMessages(sessionId, params) // 获取消息列表
|
|
197
|
+
sessionService.updateTitle(sessionId, title) // 更新会话标题
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
#### Message 服务
|
|
201
|
+
```typescript
|
|
202
|
+
messageService.send(data) // 发送消息(非流式)
|
|
203
|
+
messageService.sendStream(data) // 发送消息(流式 SSE)
|
|
204
|
+
messageService.getThoughtsStream(messageId) // 获取思考过程流
|
|
205
|
+
messageService.cancel(messageId) // 取消消息执行
|
|
206
|
+
messageService.getDetail(messageId) // 获取消息详情
|
|
207
|
+
messageService.delete(messageId) // 删除消息
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
#### Tool Call 服务
|
|
211
|
+
```typescript
|
|
212
|
+
toolCallService.confirm(data) // 确认/拒绝工具调用
|
|
213
|
+
toolCallService.confirmBatch(ids, approved) // 批量确认
|
|
214
|
+
toolCallService.getPending(toolCallId) // 获取待确认的工具调用
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
#### Plan 服务
|
|
218
|
+
```typescript
|
|
219
|
+
planService.confirm(planId, data) // 确认/拒绝计划
|
|
220
|
+
planService.provideHumanInput(planId, stepId, data) // 提供人工输入
|
|
221
|
+
planService.pause(planId) // 暂停计划
|
|
222
|
+
planService.resume(planId) // 恢复计划
|
|
223
|
+
planService.cancel(planId) // 取消计划
|
|
224
|
+
planService.retry(planId) // 重试失败步骤
|
|
225
|
+
planService.skip(planId) // 跳过失败步骤
|
|
226
|
+
planService.getDetail(planId) // 获取计划详情
|
|
227
|
+
planService.getStatus(planId) // 获取计划状态
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
#### Share 服务
|
|
231
|
+
```typescript
|
|
232
|
+
shareService.create(data) // 创建分享
|
|
233
|
+
shareService.get(shareId) // 获取分享信息
|
|
234
|
+
shareService.update(data) // 更新分享
|
|
235
|
+
shareService.delete(shareId) // 删除分享
|
|
236
|
+
shareService.listMy() // 获取我的分享列表
|
|
237
|
+
shareService.listBySession(sessionId) // 获取会话的分享列表
|
|
238
|
+
shareService.getPublicContent(shareId, token) // 获取公开分享内容
|
|
239
|
+
shareService.isPasswordRequired(shareId) // 检查是否需要密码
|
|
240
|
+
shareService.verifyPassword(data) // 验证分享密码
|
|
241
|
+
shareService.isValid(shareId) // 检查分享是否有效
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
#### 便捷方法
|
|
245
|
+
```typescript
|
|
246
|
+
createAndStartSession(projectId, agentId, title) // 快速创建并进入会话
|
|
247
|
+
getSessionWithMessages(sessionId, params) // 获取会话及其消息历史
|
|
248
|
+
confirmAllPendingToolCalls(toolCallIds) // 确认所有待确认的工具调用
|
|
249
|
+
rejectAllPendingToolCalls(toolCallIds, reason) // 拒绝所有待确认的工具调用
|
|
250
|
+
generateSessionTitle(userMessage, assistantMessage) // 生成会话标题
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Store 管理
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
// 会话状态
|
|
257
|
+
useSessions() // 获取会话列表
|
|
258
|
+
useCurrentSession() // 获取当前会话
|
|
259
|
+
useSessionsLoading() // 获取加载状态
|
|
260
|
+
|
|
261
|
+
// 消息状态
|
|
262
|
+
useMessages() // 获取所有消息
|
|
263
|
+
useCurrentThoughts() // 获取当前思考过程
|
|
264
|
+
usePendingToolCalls() // 获取待确认的工具调用
|
|
265
|
+
|
|
266
|
+
// Chat UI 状态
|
|
267
|
+
useIsStreaming() // 是否正在流式输出
|
|
268
|
+
useIsThinking() // 是否正在思考
|
|
269
|
+
useStreamingContent() // 流式内容
|
|
270
|
+
useChatError() // 聊天错误信息
|
|
271
|
+
|
|
272
|
+
// Plan Mode 状态
|
|
273
|
+
useChatUI().currentPlan // 当前计划
|
|
274
|
+
useChatUI().showPlanConfirmDialog // 是否显示计划确认弹窗
|
|
275
|
+
useChatUI().showHumanInputDialog // 是否显示人工输入弹窗
|
|
276
|
+
|
|
277
|
+
// 工具函数
|
|
278
|
+
findMessageById(messageId) // 根据 ID 查找消息
|
|
279
|
+
findSessionById(sessionId) // 根据 ID 查找会话
|
|
280
|
+
hasPendingToolCalls() // 检查是否有待确认的工具调用
|
|
281
|
+
isConversationActive() // 检查是否正在进行对话
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### Hooks
|
|
285
|
+
|
|
286
|
+
```typescript
|
|
287
|
+
// SSE 流式传输
|
|
288
|
+
const { sendMessage, reconnect, abort, isConnected } = useSSE({
|
|
289
|
+
onComplete: (content) => { /* 消息完成回调 */ }
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
// SSE 连接状态
|
|
293
|
+
const { isConnected, connect, disconnect } = useSSEConnection();
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### 组件
|
|
297
|
+
|
|
298
|
+
| 组件 | 说明 |
|
|
299
|
+
|------|------|
|
|
300
|
+
| `AgentChat` | **通用对话组件** - 包含完整的会话管理、对话、分享功能(推荐使用) |
|
|
301
|
+
| `ShareReplayPage` | **分享回放页面** - 用于公开分享的回放功能 |
|
|
302
|
+
| `ChatWindow` | 完整的对话窗口组件 |
|
|
303
|
+
| `MessageBubble` | 消息气泡组件 |
|
|
304
|
+
| `MessageImage` | 图片渲染组件 |
|
|
305
|
+
| `MessageVideo` | 视频渲染组件 |
|
|
306
|
+
| `ImagePreview` | 图片预览弹窗 |
|
|
307
|
+
| `ThinkingProcess` | 思考过程展示组件 |
|
|
308
|
+
| `MultiAgentThoughts` | Multi-Agent 思考时间线 |
|
|
309
|
+
| `ToolConfirmDialog` | 工具确认弹窗 |
|
|
310
|
+
| `ToolConfirmCard` | 工具确认卡片 |
|
|
311
|
+
| `PlanCard` | Plan 步骤卡片 |
|
|
312
|
+
| `PlanConfirmDialog` | Plan 确认弹窗 |
|
|
313
|
+
| `PlanProgressPanel` | Plan 进度面板 |
|
|
314
|
+
| `HumanInputDialog` | 人工输入弹窗 |
|
|
315
|
+
| `ShareModal` | **分享弹窗** - 用于创建和管理分享 |
|
|
316
|
+
| `ParameterDisplay` | 参数展示组件 |
|
|
317
|
+
| `StreamingJsonDisplay` | 流式 JSON 展示 |
|
|
318
|
+
|
|
319
|
+
## 类型定义
|
|
320
|
+
|
|
321
|
+
SDK 提供了完整的 TypeScript 类型支持:
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
import type {
|
|
325
|
+
AgentSession,
|
|
326
|
+
AgentMessage,
|
|
327
|
+
AgentThought,
|
|
328
|
+
ToolCall,
|
|
329
|
+
ExecutionPlan,
|
|
330
|
+
PlanStep,
|
|
331
|
+
ChatUIState,
|
|
332
|
+
// ... 更多类型
|
|
333
|
+
} from '@malette/agent-sdk';
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
## 文件服务
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
import {
|
|
340
|
+
uploadOSS,
|
|
341
|
+
getFileUrl,
|
|
342
|
+
getImageUrl,
|
|
343
|
+
getHDImageUrl,
|
|
344
|
+
resolveMediaUrl,
|
|
345
|
+
embedding
|
|
346
|
+
} from '@malette/agent-sdk';
|
|
347
|
+
|
|
348
|
+
// 上传文件
|
|
349
|
+
const result = await uploadOSS(file, (progress) => {
|
|
350
|
+
console.log('上传进度:', progress.percent);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
// 获取文件/图片 URL
|
|
354
|
+
const fileUrl = await getFileUrl('file-key');
|
|
355
|
+
const imageUrl = getImageUrl('image-key');
|
|
356
|
+
const hdUrl = getHDImageUrl('image-key');
|
|
357
|
+
|
|
358
|
+
// 解析媒体 URL
|
|
359
|
+
const { url, hdUrl, isVideo } = resolveMediaUrl({ fileId: 'xxx' });
|
|
360
|
+
|
|
361
|
+
// 图片 embedding
|
|
362
|
+
const embeddingData = await embedding('image-path');
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
## 开发
|
|
366
|
+
|
|
367
|
+
```bash
|
|
368
|
+
# 安装依赖
|
|
369
|
+
npm install
|
|
370
|
+
|
|
371
|
+
# 开发模式(watch)
|
|
372
|
+
npm run dev
|
|
373
|
+
|
|
374
|
+
# 构建
|
|
375
|
+
npm run build
|
|
376
|
+
|
|
377
|
+
# 清理
|
|
378
|
+
npm run clean
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
## 依赖
|
|
382
|
+
|
|
383
|
+
```json
|
|
384
|
+
{
|
|
385
|
+
"dependencies": {
|
|
386
|
+
"zustand": "^5.0.2",
|
|
387
|
+
"js-cookie": "^3.0.5",
|
|
388
|
+
"ali-oss": "^6.18.0"
|
|
389
|
+
},
|
|
390
|
+
"peerDependencies": {
|
|
391
|
+
"react": "^18",
|
|
392
|
+
"react-dom": "^18",
|
|
393
|
+
"antd": "^5.0.0",
|
|
394
|
+
"lucide-react": "^0.453.0",
|
|
395
|
+
"react-markdown": "^10.0.0",
|
|
396
|
+
"marked": "^15.0.0",
|
|
397
|
+
"highlight.js": "^11.0.0"
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
## 许可证
|
|
403
|
+
|
|
404
|
+
MIT
|