acp-ts 1.0.0 → 1.0.2
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 +61 -93
- package/dist/cli.js +11 -11
- package/dist/server.js +5 -5
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -1,14 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# acp-ts
|
|
2
|
+
|
|
3
|
+
基于 WebSocket 的智能体通信库,提供智能体身份管理和实时通信功能。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install acp-ts
|
|
9
|
+
```
|
|
2
10
|
|
|
3
11
|
## 简介
|
|
4
12
|
|
|
5
|
-
|
|
13
|
+
acp-ts 是一个基于 WebSocket 的智能体通信库,提供了智能体身份管理(AgentCP)和 WebSocket 通信(AgentWS)功能。通过 AgentManager 统一管理这些功能,使用更加便捷。
|
|
6
14
|
|
|
7
15
|
## 快速开始
|
|
8
16
|
|
|
9
17
|
### 1. 初始化 AgentManager
|
|
10
18
|
|
|
11
19
|
```typescript
|
|
20
|
+
import { AgentManager } from 'acp-ts';
|
|
21
|
+
|
|
12
22
|
// 获取 AgentManager 单例
|
|
13
23
|
const manager = AgentManager.getInstance();
|
|
14
24
|
|
|
@@ -67,7 +77,7 @@ const aws = await manager.initAWS(aid, config);
|
|
|
67
77
|
await aws.startWebSocket();
|
|
68
78
|
|
|
69
79
|
// 快速连接到指定智能体(推荐方式)
|
|
70
|
-
aws.connectTo("target-aid",
|
|
80
|
+
aws.connectTo("target-aid",
|
|
71
81
|
(sessionInfo) => {
|
|
72
82
|
console.log("会话创建成功:", sessionInfo.sessionId);
|
|
73
83
|
console.log("邀请码:", sessionInfo.identifyingCode);
|
|
@@ -114,11 +124,11 @@ aws.disconnect();
|
|
|
114
124
|
aws.createSession((sessionRes) => {
|
|
115
125
|
console.log("会话ID:", sessionRes.sessionId);
|
|
116
126
|
console.log("邀请码:", sessionRes.identifyingCode);
|
|
117
|
-
|
|
127
|
+
|
|
118
128
|
// 手动邀请智能体
|
|
119
129
|
aws.invite(
|
|
120
|
-
"target-agent-id",
|
|
121
|
-
sessionRes.sessionId,
|
|
130
|
+
"target-agent-id",
|
|
131
|
+
sessionRes.sessionId,
|
|
122
132
|
sessionRes.identifyingCode,
|
|
123
133
|
(inviteStatus) => {
|
|
124
134
|
if (inviteStatus === 'success') {
|
|
@@ -135,7 +145,7 @@ aws.createSession((sessionRes) => {
|
|
|
135
145
|
|
|
136
146
|
```typescript
|
|
137
147
|
import React, { useEffect, useState } from 'react';
|
|
138
|
-
import { AgentManager } from '
|
|
148
|
+
import { AgentManager } from 'acp-ts';
|
|
139
149
|
|
|
140
150
|
const ChatComponent: React.FC = () => {
|
|
141
151
|
const [aws, setAws] = useState<any>(null);
|
|
@@ -147,30 +157,30 @@ const ChatComponent: React.FC = () => {
|
|
|
147
157
|
try {
|
|
148
158
|
const manager = AgentManager.getInstance();
|
|
149
159
|
const acp = await manager.initACP("https://api.example.com");
|
|
150
|
-
|
|
160
|
+
|
|
151
161
|
// 加载或创建身份
|
|
152
162
|
let aid = await acp.loadCurrentAid();
|
|
153
163
|
if (!aid) {
|
|
154
164
|
aid = await acp.loadGuestAid();
|
|
155
165
|
}
|
|
156
|
-
|
|
166
|
+
|
|
157
167
|
// 获取连接配置并初始化WebSocket
|
|
158
168
|
const config = await acp.online();
|
|
159
169
|
const agentWS = await manager.initAWS(aid, config);
|
|
160
|
-
|
|
170
|
+
|
|
161
171
|
// 注册事件监听器
|
|
162
172
|
agentWS.onStatusChange((status) => {
|
|
163
173
|
setConnectionStatus(status);
|
|
164
174
|
});
|
|
165
|
-
|
|
175
|
+
|
|
166
176
|
agentWS.onMessage((message) => {
|
|
167
177
|
setMessages(prev => [...prev, message.content]);
|
|
168
178
|
});
|
|
169
|
-
|
|
179
|
+
|
|
170
180
|
// 启动连接
|
|
171
181
|
await agentWS.startWebSocket();
|
|
172
182
|
setAws(agentWS);
|
|
173
|
-
|
|
183
|
+
|
|
174
184
|
} catch (error) {
|
|
175
185
|
console.error("初始化失败:", error);
|
|
176
186
|
}
|
|
@@ -206,7 +216,6 @@ const ChatComponent: React.FC = () => {
|
|
|
206
216
|
<div key={index}>{msg}</div>
|
|
207
217
|
))}
|
|
208
218
|
</div>
|
|
209
|
-
{/* 发送消息和连接的UI组件 */}
|
|
210
219
|
</div>
|
|
211
220
|
);
|
|
212
221
|
};
|
|
@@ -214,11 +223,28 @@ const ChatComponent: React.FC = () => {
|
|
|
214
223
|
|
|
215
224
|
## API 参考
|
|
216
225
|
|
|
217
|
-
###
|
|
226
|
+
### AgentManager
|
|
227
|
+
|
|
228
|
+
- `getInstance(): AgentManager` - 获取单例实例
|
|
229
|
+
- `initACP(apiUrl, seedPassword?): Promise<AgentCP>` - 初始化身份管理
|
|
230
|
+
- `initAWS(aid, config): Promise<AgentWS>` - 初始化 WebSocket 连接
|
|
231
|
+
|
|
232
|
+
### AgentCP
|
|
233
|
+
|
|
234
|
+
- `createAid(aid): Promise<string>` - 创建新身份
|
|
235
|
+
- `loadAid(aid): Promise<boolean>` - 加载指定身份
|
|
236
|
+
- `loadCurrentAid(): Promise<string | null>` - 加载当前身份
|
|
237
|
+
- `loadGuestAid(): Promise<string>` - 加载访客身份
|
|
238
|
+
- `loadAidList(): Promise<string[]>` - 获取身份列表
|
|
239
|
+
- `importAid(identity, seedPassword?): Promise<void>` - 导入身份
|
|
240
|
+
- `getCertInfo(aid): Promise<CertInfo>` - 获取证书信息
|
|
241
|
+
- `online(): Promise<Config>` - 上线获取连接配置
|
|
242
|
+
|
|
243
|
+
### AgentWS
|
|
218
244
|
|
|
219
245
|
#### 方法
|
|
220
246
|
|
|
221
|
-
- `startWebSocket(): Promise<void>` - 启动WebSocket连接
|
|
247
|
+
- `startWebSocket(): Promise<void>` - 启动 WebSocket 连接
|
|
222
248
|
- `connectTo(receiver, onSessionCreated?, onInviteStatus?): void` - 快捷连接到指定智能体
|
|
223
249
|
- `createSession(callback): void` - 创建会话
|
|
224
250
|
- `invite(receiver, sessionId, identifyingCode, callback?): void` - 邀请智能体加入会话
|
|
@@ -228,16 +254,12 @@ const ChatComponent: React.FC = () => {
|
|
|
228
254
|
- `onMessage(callback): void` - 注册消息接收监听器
|
|
229
255
|
- `disconnect(): void` - 断开连接
|
|
230
256
|
|
|
231
|
-
####
|
|
257
|
+
#### 类型定义
|
|
232
258
|
|
|
233
259
|
```typescript
|
|
234
260
|
type ConnectionStatus = 'connecting' | 'connected' | 'disconnected' | 'reconnecting' | 'error';
|
|
235
261
|
type InviteStatus = 'success' | 'error';
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
#### 消息类型
|
|
239
262
|
|
|
240
|
-
```typescript
|
|
241
263
|
type ACPMessageResponse = {
|
|
242
264
|
type: 'success' | 'error';
|
|
243
265
|
content: string;
|
|
@@ -249,11 +271,11 @@ type ACPMessageSessionResponse = {
|
|
|
249
271
|
}
|
|
250
272
|
```
|
|
251
273
|
|
|
252
|
-
### WSClient
|
|
274
|
+
### WSClient
|
|
253
275
|
|
|
254
|
-
底层WebSocket客户端,提供更精细的控制:
|
|
276
|
+
底层 WebSocket 客户端,提供更精细的控制:
|
|
255
277
|
|
|
256
|
-
- `connectToServer(wsServer, aid, signature): Promise<void>` - 连接到WebSocket服务器
|
|
278
|
+
- `connectToServer(wsServer, aid, signature): Promise<void>` - 连接到 WebSocket 服务器
|
|
257
279
|
- `createSession(callback): void` - 创建会话(自动清理监听器)
|
|
258
280
|
- `invite(receiver, sessionId, identifyingCode, callback?): void` - 发送邀请
|
|
259
281
|
- `onStatusChange(callback): () => void` - 注册状态监听器,返回清理函数
|
|
@@ -264,34 +286,18 @@ type ACPMessageSessionResponse = {
|
|
|
264
286
|
|
|
265
287
|
## 错误处理
|
|
266
288
|
|
|
267
|
-
所有关键操作都有适当的错误处理:
|
|
268
|
-
|
|
269
|
-
- 身份验证失败
|
|
270
|
-
- 连接超时
|
|
271
|
-
- 消息发送失败
|
|
272
|
-
- WebSocket 连接断开
|
|
273
|
-
- 参数验证错误
|
|
274
|
-
|
|
275
|
-
建议使用 try-catch 包装关键操作:
|
|
276
|
-
|
|
277
289
|
```typescript
|
|
278
290
|
try {
|
|
279
291
|
await aws.startWebSocket();
|
|
280
292
|
} catch (error) {
|
|
281
293
|
console.error(`WebSocket 连接失败: ${error.message}`);
|
|
282
|
-
// 实现重连逻辑
|
|
283
294
|
}
|
|
284
|
-
```
|
|
285
295
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
```typescript
|
|
289
|
-
// 连接错误处理
|
|
296
|
+
// 连接状态监听
|
|
290
297
|
aws.onStatusChange((status) => {
|
|
291
298
|
switch (status) {
|
|
292
299
|
case 'error':
|
|
293
300
|
console.error("连接出错,尝试重连...");
|
|
294
|
-
// 实现重连逻辑
|
|
295
301
|
break;
|
|
296
302
|
case 'disconnected':
|
|
297
303
|
console.warn("连接断开");
|
|
@@ -306,9 +312,7 @@ aws.onStatusChange((status) => {
|
|
|
306
312
|
aws.onMessage((message) => {
|
|
307
313
|
if (message.type === 'error') {
|
|
308
314
|
console.error("收到错误消息:", message.content);
|
|
309
|
-
// 处理错误情况
|
|
310
315
|
} else {
|
|
311
|
-
// 处理正常消息
|
|
312
316
|
console.log("收到消息:", message.content);
|
|
313
317
|
}
|
|
314
318
|
});
|
|
@@ -316,59 +320,23 @@ aws.onMessage((message) => {
|
|
|
316
320
|
|
|
317
321
|
## 最佳实践
|
|
318
322
|
|
|
319
|
-
|
|
320
|
-
-
|
|
321
|
-
-
|
|
322
|
-
-
|
|
323
|
-
|
|
324
|
-
### 2. 事件处理
|
|
325
|
-
- 在初始化 WebSocket 连接后再注册事件监听器
|
|
326
|
-
- 使用 WSClient 时记得调用返回的清理函数防止内存泄漏
|
|
327
|
-
- 避免在回调函数中执行耗时操作,以免阻塞消息处理
|
|
328
|
-
|
|
329
|
-
### 3. 异步操作
|
|
330
|
-
- 使用 async/await 处理异步操作
|
|
331
|
-
- 实现适当的重连机制和错误恢复策略
|
|
332
|
-
- 对关键操作添加超时控制
|
|
323
|
+
1. **资源管理**
|
|
324
|
+
- 使用 AgentManager 管理实例
|
|
325
|
+
- 退出时调用 `disconnect()` 清理资源
|
|
326
|
+
- React 组件中使用 useEffect 清理函数
|
|
333
327
|
|
|
334
|
-
|
|
335
|
-
-
|
|
336
|
-
-
|
|
337
|
-
- 使用HTTPS/WSS协议进行通信
|
|
328
|
+
2. **事件处理**
|
|
329
|
+
- 初始化连接后再注册监听器
|
|
330
|
+
- 使用 WSClient 时调用返回的清理函数防止内存泄漏
|
|
338
331
|
|
|
339
|
-
|
|
340
|
-
-
|
|
341
|
-
-
|
|
342
|
-
- 对大量消息的场景考虑消息批处理
|
|
332
|
+
3. **安全性**
|
|
333
|
+
- 妥善保管 seedPassword 和私钥
|
|
334
|
+
- 使用 HTTPS/WSS 协议通信
|
|
343
335
|
|
|
344
|
-
|
|
345
|
-
- 启用适当的日志记录
|
|
346
|
-
- 监控连接状态和消息传输
|
|
347
|
-
- 实现健康检查机制
|
|
336
|
+
## 相关项目
|
|
348
337
|
|
|
349
|
-
|
|
338
|
+
- [acp-py](https://www.npmjs.com/package/acp-py) - Python 版本的智能体通信库
|
|
350
339
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
1. **连接失败**: 检查网络连接、服务器地址和身份验证信息
|
|
354
|
-
2. **消息发送失败**: 确认WebSocket连接状态和会话状态
|
|
355
|
-
3. **监听器未清理**: 使用WSClient时记得调用返回的清理函数
|
|
356
|
-
4. **重复监听器**: createSession和invite方法已自动处理重复注册问题
|
|
357
|
-
|
|
358
|
-
### 调试技巧
|
|
359
|
-
|
|
360
|
-
```typescript
|
|
361
|
-
// 启用详细日志
|
|
362
|
-
aws.onStatusChange((status) => {
|
|
363
|
-
console.log(`[${new Date().toISOString()}] 状态变更: ${status}`);
|
|
364
|
-
});
|
|
365
|
-
|
|
366
|
-
// 监控消息传输
|
|
367
|
-
aws.onMessage((message) => {
|
|
368
|
-
console.log(`[${new Date().toISOString()}] 收到消息:`, message);
|
|
369
|
-
});
|
|
340
|
+
## License
|
|
370
341
|
|
|
371
|
-
|
|
372
|
-
const currentStatus = aws.msgClient.getCurrentStatus();
|
|
373
|
-
console.log("当前连接状态:", currentStatus);
|
|
374
|
-
```
|
|
342
|
+
MIT
|
package/dist/cli.js
CHANGED
|
@@ -61,7 +61,7 @@ function update() {
|
|
|
61
61
|
const npm = getNpmCommand();
|
|
62
62
|
try {
|
|
63
63
|
// 获取最新版本号
|
|
64
|
-
const result = (0, child_process_1.spawnSync)(npm, ['view', '
|
|
64
|
+
const result = (0, child_process_1.spawnSync)(npm, ['view', 'acp-ts', 'version'], {
|
|
65
65
|
encoding: 'utf-8',
|
|
66
66
|
timeout: 30000
|
|
67
67
|
});
|
|
@@ -80,7 +80,7 @@ function update() {
|
|
|
80
80
|
console.log(`发现新版本 v${latestVersion},当前版本 v${currentVersion}`);
|
|
81
81
|
console.log('正在更新...');
|
|
82
82
|
// 执行全局更新
|
|
83
|
-
const installResult = (0, child_process_1.spawnSync)(npm, ['install', '-g', '
|
|
83
|
+
const installResult = (0, child_process_1.spawnSync)(npm, ['install', '-g', 'acp-ts@latest'], {
|
|
84
84
|
stdio: 'inherit',
|
|
85
85
|
shell: true
|
|
86
86
|
});
|
|
@@ -91,7 +91,7 @@ function update() {
|
|
|
91
91
|
// 可能是权限问题
|
|
92
92
|
if (!isWindows) {
|
|
93
93
|
console.log('\n更新失败,可能需要管理员权限,请尝试运行:');
|
|
94
|
-
console.log(' sudo npm install -g
|
|
94
|
+
console.log(' sudo npm install -g acp-ts@latest');
|
|
95
95
|
}
|
|
96
96
|
else {
|
|
97
97
|
console.log('\n更新失败,请尝试以管理员身份运行命令提示符后重试');
|
|
@@ -114,7 +114,7 @@ for (let i = 0; i < args.length; i++) {
|
|
|
114
114
|
process.exit(0);
|
|
115
115
|
}
|
|
116
116
|
else if (args[i] === '-v' || args[i] === '--version') {
|
|
117
|
-
console.log(`
|
|
117
|
+
console.log(`acp-ts v${getVersion()}`);
|
|
118
118
|
process.exit(0);
|
|
119
119
|
}
|
|
120
120
|
else if (args[i] === '-p' || args[i] === '--port') {
|
|
@@ -127,11 +127,11 @@ for (let i = 0; i < args.length; i++) {
|
|
|
127
127
|
}
|
|
128
128
|
else if (args[i] === '-h' || args[i] === '--help') {
|
|
129
129
|
console.log(`
|
|
130
|
-
|
|
130
|
+
acp-ts - 智能体通信调试工具 v${getVersion()}
|
|
131
131
|
|
|
132
132
|
用法:
|
|
133
|
-
|
|
134
|
-
|
|
133
|
+
acp-ts [选项]
|
|
134
|
+
acp-ts update 更新到最新版本
|
|
135
135
|
|
|
136
136
|
选项:
|
|
137
137
|
-v, --version 显示版本号
|
|
@@ -140,12 +140,12 @@ agent-acp - 智能体通信调试工具 v${getVersion()}
|
|
|
140
140
|
-h, --help 显示帮助信息
|
|
141
141
|
|
|
142
142
|
安装:
|
|
143
|
-
npm install -g
|
|
143
|
+
npm install -g acp-ts
|
|
144
144
|
|
|
145
145
|
示例:
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
146
|
+
acp-ts
|
|
147
|
+
acp-ts -p 8080
|
|
148
|
+
acp-ts update
|
|
149
149
|
`);
|
|
150
150
|
process.exit(0);
|
|
151
151
|
}
|
package/dist/server.js
CHANGED
|
@@ -68,7 +68,7 @@ const indexHtml = `<!DOCTYPE html>
|
|
|
68
68
|
<meta charset="UTF-8">
|
|
69
69
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
70
70
|
<link rel="icon" href="/favicon.ico" type="image/x-icon">
|
|
71
|
-
<title>
|
|
71
|
+
<title>acp-ts 智能体管理</title>
|
|
72
72
|
<style>
|
|
73
73
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
74
74
|
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f5f5f5; min-height: 100vh; display: flex; justify-content: center; align-items: center; }
|
|
@@ -103,7 +103,7 @@ const indexHtml = `<!DOCTYPE html>
|
|
|
103
103
|
</head>
|
|
104
104
|
<body>
|
|
105
105
|
<div class="container">
|
|
106
|
-
<h1>
|
|
106
|
+
<h1>acp-ts 智能体管理</h1>
|
|
107
107
|
|
|
108
108
|
<div class="aid-section" id="aidSection" style="display:none;">
|
|
109
109
|
<div class="aid-label">当前 AID</div>
|
|
@@ -232,7 +232,7 @@ const chatHtml = `<!DOCTYPE html>
|
|
|
232
232
|
<meta charset="UTF-8">
|
|
233
233
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
234
234
|
<link rel="icon" href="/favicon.ico" type="image/x-icon">
|
|
235
|
-
<title>
|
|
235
|
+
<title>acp-ts 聊天</title>
|
|
236
236
|
<style>
|
|
237
237
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
238
238
|
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f5f5f5; height: 100vh; display: flex; flex-direction: column; }
|
|
@@ -280,7 +280,7 @@ const chatHtml = `<!DOCTYPE html>
|
|
|
280
280
|
<body>
|
|
281
281
|
<div class="header">
|
|
282
282
|
<div>
|
|
283
|
-
<h1>
|
|
283
|
+
<h1>acp-ts 聊天</h1>
|
|
284
284
|
<div class="aid" id="myAid">加载中...</div>
|
|
285
285
|
</div>
|
|
286
286
|
<a href="/" class="back">返回</a>
|
|
@@ -866,7 +866,7 @@ function startServer(port, apiUrl) {
|
|
|
866
866
|
process.on('SIGINT', cleanup);
|
|
867
867
|
process.on('SIGTERM', cleanup);
|
|
868
868
|
server.listen(port, () => {
|
|
869
|
-
console.log(`\n
|
|
869
|
+
console.log(`\n acp-ts 调试服务已启动`);
|
|
870
870
|
console.log(` ─────────────────────────`);
|
|
871
871
|
console.log(` 本地地址: http://localhost:${port}`);
|
|
872
872
|
console.log(` API 服务: ${apiUrl}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "acp-ts",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "基于 WebSocket 的智能体通信库,提供智能体身份管理和实时通信功能",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -33,7 +33,6 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@react-native-async-storage/async-storage": "^2.1.2",
|
|
35
35
|
"acp-ws-evol": "^1.0.0",
|
|
36
|
-
"agent-acp": "^1.0.0",
|
|
37
36
|
"axios": "^1.9.0",
|
|
38
37
|
"jsrsasign": "^11.1.0",
|
|
39
38
|
"mitt": "^3.0.1",
|