@f2a/network 0.1.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/.github/workflows/ci.yml +113 -0
- package/.github/workflows/publish.yml +60 -0
- package/LICENSE +21 -0
- package/MONOREPO.md +58 -0
- package/README.md +280 -0
- package/SKILL.md +137 -0
- package/dist/adapters/openclaw.d.ts +103 -0
- package/dist/adapters/openclaw.d.ts.map +1 -0
- package/dist/adapters/openclaw.js +297 -0
- package/dist/adapters/openclaw.js.map +1 -0
- package/dist/cli/commands.d.ts +17 -0
- package/dist/cli/commands.d.ts.map +1 -0
- package/dist/cli/commands.js +107 -0
- package/dist/cli/commands.js.map +1 -0
- package/dist/cli/index.d.ts +6 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +203 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/core/autonomous-economy.d.ts +136 -0
- package/dist/core/autonomous-economy.d.ts.map +1 -0
- package/dist/core/autonomous-economy.js +255 -0
- package/dist/core/autonomous-economy.js.map +1 -0
- package/dist/core/connection-manager.d.ts +80 -0
- package/dist/core/connection-manager.d.ts.map +1 -0
- package/dist/core/connection-manager.js +235 -0
- package/dist/core/connection-manager.js.map +1 -0
- package/dist/core/connection-manager.test.d.ts +2 -0
- package/dist/core/connection-manager.test.d.ts.map +1 -0
- package/dist/core/connection-manager.test.js +52 -0
- package/dist/core/connection-manager.test.js.map +1 -0
- package/dist/core/e2ee-crypto.d.ts +90 -0
- package/dist/core/e2ee-crypto.d.ts.map +1 -0
- package/dist/core/e2ee-crypto.js +190 -0
- package/dist/core/e2ee-crypto.js.map +1 -0
- package/dist/core/f2a.d.ts +126 -0
- package/dist/core/f2a.d.ts.map +1 -0
- package/dist/core/f2a.js +425 -0
- package/dist/core/f2a.js.map +1 -0
- package/dist/core/identity.d.ts +47 -0
- package/dist/core/identity.d.ts.map +1 -0
- package/dist/core/identity.js +130 -0
- package/dist/core/identity.js.map +1 -0
- package/dist/core/identity.test.d.ts +2 -0
- package/dist/core/identity.test.d.ts.map +1 -0
- package/dist/core/identity.test.js +43 -0
- package/dist/core/identity.test.js.map +1 -0
- package/dist/core/p2p-network.d.ts +242 -0
- package/dist/core/p2p-network.d.ts.map +1 -0
- package/dist/core/p2p-network.js +1182 -0
- package/dist/core/p2p-network.js.map +1 -0
- package/dist/core/reputation-security.d.ts +168 -0
- package/dist/core/reputation-security.d.ts.map +1 -0
- package/dist/core/reputation-security.js +369 -0
- package/dist/core/reputation-security.js.map +1 -0
- package/dist/core/reputation.d.ts +179 -0
- package/dist/core/reputation.d.ts.map +1 -0
- package/dist/core/reputation.js +472 -0
- package/dist/core/reputation.js.map +1 -0
- package/dist/core/review-committee.d.ts +130 -0
- package/dist/core/review-committee.d.ts.map +1 -0
- package/dist/core/review-committee.js +251 -0
- package/dist/core/review-committee.js.map +1 -0
- package/dist/core/serverless.d.ts +155 -0
- package/dist/core/serverless.d.ts.map +1 -0
- package/dist/core/serverless.js +615 -0
- package/dist/core/serverless.js.map +1 -0
- package/dist/core/token-manager.d.ts +42 -0
- package/dist/core/token-manager.d.ts.map +1 -0
- package/dist/core/token-manager.js +122 -0
- package/dist/core/token-manager.js.map +1 -0
- package/dist/daemon/control-server.d.ts +55 -0
- package/dist/daemon/control-server.d.ts.map +1 -0
- package/dist/daemon/control-server.js +262 -0
- package/dist/daemon/control-server.js.map +1 -0
- package/dist/daemon/index.d.ts +35 -0
- package/dist/daemon/index.d.ts.map +1 -0
- package/dist/daemon/index.js +69 -0
- package/dist/daemon/index.js.map +1 -0
- package/dist/daemon/main.d.ts +6 -0
- package/dist/daemon/main.d.ts.map +1 -0
- package/dist/daemon/main.js +38 -0
- package/dist/daemon/main.js.map +1 -0
- package/dist/daemon/start.d.ts +6 -0
- package/dist/daemon/start.d.ts.map +1 -0
- package/dist/daemon/start.js +25 -0
- package/dist/daemon/start.js.map +1 -0
- package/dist/daemon/webhook.d.ts +30 -0
- package/dist/daemon/webhook.d.ts.map +1 -0
- package/dist/daemon/webhook.js +86 -0
- package/dist/daemon/webhook.js.map +1 -0
- package/dist/daemon/webhook.test.d.ts +2 -0
- package/dist/daemon/webhook.test.d.ts.map +1 -0
- package/dist/daemon/webhook.test.js +24 -0
- package/dist/daemon/webhook.test.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol/messages.d.ts +739 -0
- package/dist/protocol/messages.d.ts.map +1 -0
- package/dist/protocol/messages.js +188 -0
- package/dist/protocol/messages.js.map +1 -0
- package/dist/protocol/messages.test.d.ts +2 -0
- package/dist/protocol/messages.test.d.ts.map +1 -0
- package/dist/protocol/messages.test.js +55 -0
- package/dist/protocol/messages.test.js.map +1 -0
- package/dist/types/index.d.ts +247 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +10 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/result.d.ts +28 -0
- package/dist/types/result.d.ts.map +1 -0
- package/dist/types/result.js +16 -0
- package/dist/types/result.js.map +1 -0
- package/dist/utils/benchmark.d.ts +67 -0
- package/dist/utils/benchmark.d.ts.map +1 -0
- package/dist/utils/benchmark.js +179 -0
- package/dist/utils/benchmark.js.map +1 -0
- package/dist/utils/logger.d.ts +105 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +275 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/middleware.d.ts +85 -0
- package/dist/utils/middleware.d.ts.map +1 -0
- package/dist/utils/middleware.js +173 -0
- package/dist/utils/middleware.js.map +1 -0
- package/dist/utils/rate-limiter.d.ts +71 -0
- package/dist/utils/rate-limiter.d.ts.map +1 -0
- package/dist/utils/rate-limiter.js +160 -0
- package/dist/utils/rate-limiter.js.map +1 -0
- package/dist/utils/signature.d.ts +57 -0
- package/dist/utils/signature.d.ts.map +1 -0
- package/dist/utils/signature.js +102 -0
- package/dist/utils/signature.js.map +1 -0
- package/dist/utils/validation.d.ts +504 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +159 -0
- package/dist/utils/validation.js.map +1 -0
- package/docs/F2A-PROTOCOL.md +61 -0
- package/docs/MOBILE_BOOTSTRAP_DESIGN.md +126 -0
- package/docs/a2a-lessons.md +316 -0
- package/docs/middleware-guide.md +448 -0
- package/docs/readme-update-checklist.md +90 -0
- package/docs/reputation-guide.md +396 -0
- package/docs/rfcs/001-reputation-system.md +712 -0
- package/docs/security-design.md +247 -0
- package/install.sh +231 -0
- package/package.json +64 -0
- package/packages/openclaw-adapter/README.md +510 -0
- package/packages/openclaw-adapter/openclaw.plugin.json +106 -0
- package/packages/openclaw-adapter/package.json +40 -0
- package/packages/openclaw-adapter/src/announcement-queue.test.ts +449 -0
- package/packages/openclaw-adapter/src/announcement-queue.ts +403 -0
- package/packages/openclaw-adapter/src/capability-detector.test.ts +99 -0
- package/packages/openclaw-adapter/src/capability-detector.ts +183 -0
- package/packages/openclaw-adapter/src/claim-handlers.test.ts +974 -0
- package/packages/openclaw-adapter/src/claim-handlers.ts +482 -0
- package/packages/openclaw-adapter/src/connector.business.test.ts +583 -0
- package/packages/openclaw-adapter/src/connector.ts +795 -0
- package/packages/openclaw-adapter/src/index.test.ts +82 -0
- package/packages/openclaw-adapter/src/index.ts +18 -0
- package/packages/openclaw-adapter/src/integration.e2e.test.ts +829 -0
- package/packages/openclaw-adapter/src/logger.ts +51 -0
- package/packages/openclaw-adapter/src/network-client.test.ts +266 -0
- package/packages/openclaw-adapter/src/network-client.ts +251 -0
- package/packages/openclaw-adapter/src/network-recovery.test.ts +465 -0
- package/packages/openclaw-adapter/src/node-manager.test.ts +136 -0
- package/packages/openclaw-adapter/src/node-manager.ts +429 -0
- package/packages/openclaw-adapter/src/plugin.test.ts +439 -0
- package/packages/openclaw-adapter/src/plugin.ts +104 -0
- package/packages/openclaw-adapter/src/reputation.test.ts +221 -0
- package/packages/openclaw-adapter/src/reputation.ts +368 -0
- package/packages/openclaw-adapter/src/task-guard.test.ts +502 -0
- package/packages/openclaw-adapter/src/task-guard.ts +860 -0
- package/packages/openclaw-adapter/src/task-queue.concurrency.test.ts +462 -0
- package/packages/openclaw-adapter/src/task-queue.edge-cases.test.ts +284 -0
- package/packages/openclaw-adapter/src/task-queue.persistence.test.ts +408 -0
- package/packages/openclaw-adapter/src/task-queue.ts +668 -0
- package/packages/openclaw-adapter/src/tool-handlers.test.ts +906 -0
- package/packages/openclaw-adapter/src/tool-handlers.ts +574 -0
- package/packages/openclaw-adapter/src/types.ts +361 -0
- package/packages/openclaw-adapter/src/webhook-pusher.test.ts +188 -0
- package/packages/openclaw-adapter/src/webhook-pusher.ts +220 -0
- package/packages/openclaw-adapter/src/webhook-server.test.ts +580 -0
- package/packages/openclaw-adapter/src/webhook-server.ts +202 -0
- package/packages/openclaw-adapter/tsconfig.json +20 -0
- package/src/cli/commands.test.ts +157 -0
- package/src/cli/commands.ts +129 -0
- package/src/cli/index.test.ts +77 -0
- package/src/cli/index.ts +234 -0
- package/src/core/autonomous-economy.test.ts +291 -0
- package/src/core/autonomous-economy.ts +428 -0
- package/src/core/e2ee-crypto.test.ts +125 -0
- package/src/core/e2ee-crypto.ts +246 -0
- package/src/core/f2a.test.ts +269 -0
- package/src/core/f2a.ts +618 -0
- package/src/core/p2p-network.test.ts +199 -0
- package/src/core/p2p-network.ts +1432 -0
- package/src/core/reputation-security.test.ts +403 -0
- package/src/core/reputation-security.ts +562 -0
- package/src/core/reputation.test.ts +260 -0
- package/src/core/reputation.ts +576 -0
- package/src/core/review-committee.test.ts +380 -0
- package/src/core/review-committee.ts +401 -0
- package/src/core/token-manager.test.ts +133 -0
- package/src/core/token-manager.ts +140 -0
- package/src/daemon/control-server.test.ts +216 -0
- package/src/daemon/control-server.ts +292 -0
- package/src/daemon/index.test.ts +85 -0
- package/src/daemon/index.ts +89 -0
- package/src/daemon/main.ts +44 -0
- package/src/daemon/start.ts +29 -0
- package/src/daemon/webhook.test.ts +68 -0
- package/src/daemon/webhook.ts +105 -0
- package/src/index.test.ts +436 -0
- package/src/index.ts +72 -0
- package/src/types/index.test.ts +87 -0
- package/src/types/index.ts +341 -0
- package/src/types/result.ts +68 -0
- package/src/utils/benchmark.ts +237 -0
- package/src/utils/logger.ts +331 -0
- package/src/utils/middleware.ts +229 -0
- package/src/utils/rate-limiter.ts +207 -0
- package/src/utils/signature.ts +136 -0
- package/src/utils/validation.ts +186 -0
- package/tests/docker/Dockerfile.node +23 -0
- package/tests/docker/Dockerfile.runner +18 -0
- package/tests/docker/docker-compose.test.yml +73 -0
- package/tests/integration/message-passing.test.ts +109 -0
- package/tests/integration/multi-node.test.ts +92 -0
- package/tests/integration/p2p-connection.test.ts +83 -0
- package/tests/integration/test-config.ts +32 -0
- package/tsconfig.json +21 -0
- package/vitest.config.ts +26 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# F2A Protocol Specification
|
|
2
|
+
|
|
3
|
+
F2A (Federated Agent Architecture) Protocol 是 Agent 之间的通用通信协议。
|
|
4
|
+
|
|
5
|
+
## 版本
|
|
6
|
+
|
|
7
|
+
当前版本: **1.0.0**
|
|
8
|
+
|
|
9
|
+
## 消息格式
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
interface F2AMessage {
|
|
13
|
+
version: "1.0";
|
|
14
|
+
type: MessageType;
|
|
15
|
+
from: string;
|
|
16
|
+
to?: string;
|
|
17
|
+
timestamp: number;
|
|
18
|
+
payload: unknown;
|
|
19
|
+
signature?: string;
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 消息类型
|
|
24
|
+
|
|
25
|
+
- `discover` - 发现 Agents
|
|
26
|
+
- `announce` - 广播任务(认领模式)
|
|
27
|
+
- `claim` - 认领任务
|
|
28
|
+
- `delegate` - 明确委托
|
|
29
|
+
- `accept` / `reject` - 接受/拒绝委托
|
|
30
|
+
- `response` - 任务响应
|
|
31
|
+
- `review` - 评审提交
|
|
32
|
+
- `heartbeat` - 心跳
|
|
33
|
+
|
|
34
|
+
## 委托模式
|
|
35
|
+
|
|
36
|
+
### 1. 直接委托
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
Agent A --delegate--> Agent B --response--> Agent A
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 2. 广播并行
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
Agent A --delegate--> All Agents --response--> Agent A
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 3. 广播认领
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
Agent A --announce--> All Agents
|
|
52
|
+
Agent B --claim--> Agent A
|
|
53
|
+
Agent A --delegate--> Agent B --response--> Agent A
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## 安全机制
|
|
57
|
+
|
|
58
|
+
- 白名单/黑名单
|
|
59
|
+
- 信誉系统
|
|
60
|
+
- 评审团(高价值任务)
|
|
61
|
+
- 消息签名验证
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# 手机作为移动引导节点设计提案
|
|
2
|
+
|
|
3
|
+
## 背景
|
|
4
|
+
|
|
5
|
+
当前 F2A 的 P2P 网络局限于单一局域网,无法实现跨网络(如家里和公司)的 Agent 发现和通信。
|
|
6
|
+
|
|
7
|
+
## 设计目标
|
|
8
|
+
|
|
9
|
+
让手机作为**移动引导节点(Mobile Bootstrap Node)**,连接不同网络的 Agent:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
场景1: 家里
|
|
13
|
+
┌─────────────────────────┐
|
|
14
|
+
│ 家里的 WiFi 网络 │
|
|
15
|
+
│ ┌─────┐ ┌─────┐ ┌─────┐│
|
|
16
|
+
│ │Agent│ │Agent│ │Agent││
|
|
17
|
+
│ │ 1 │ │ 2 │ │ 3 ││
|
|
18
|
+
│ └──┬──┘ └──┬──┘ └──┬──┘│
|
|
19
|
+
│ └────────┼───────┘ │
|
|
20
|
+
│ ▼ │
|
|
21
|
+
│ ┌─────────┐ │
|
|
22
|
+
│ │ 手机 │◄──────┐
|
|
23
|
+
│ │引导节点 │ │
|
|
24
|
+
│ └─────────┘ │
|
|
25
|
+
└─────────────────────────┘
|
|
26
|
+
|
|
27
|
+
场景2: 公司
|
|
28
|
+
┌─────────────────────────┐
|
|
29
|
+
│ 公司的 WiFi 网络 │
|
|
30
|
+
│ ┌─────┐ ┌─────┐ │
|
|
31
|
+
│ │Agent│ │Agent│ │
|
|
32
|
+
│ │ 4 │ │ 5 │ │
|
|
33
|
+
│ └──┬──┘ └──┬──┘ │
|
|
34
|
+
│ └────────┼───────┐ │
|
|
35
|
+
│ ▼ │ │
|
|
36
|
+
│ ┌─────────┐ │ │
|
|
37
|
+
│ │ 手机 │◄─┘ │
|
|
38
|
+
│ │引导节点 │ │
|
|
39
|
+
│ └────┬────┘ │
|
|
40
|
+
└──────────────┼──────────┘
|
|
41
|
+
│
|
|
42
|
+
▼
|
|
43
|
+
家里的3个Agent 和 公司的2个Agent
|
|
44
|
+
通过手机节点串联
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 核心机制
|
|
48
|
+
|
|
49
|
+
### 1. 手机作为 DHT 节点
|
|
50
|
+
|
|
51
|
+
- 手机运行 F2A 节点,启用 DHT 服务器模式
|
|
52
|
+
- 在家时:发现并记录家里的 Agent 到 DHT
|
|
53
|
+
- 在公司时:发现并记录公司的 Agent 到 DHT
|
|
54
|
+
- DHT 路由表自动同步,实现跨网络发现
|
|
55
|
+
|
|
56
|
+
### 2. NAT 穿透
|
|
57
|
+
|
|
58
|
+
- 启用 libp2p 的 autoNAT 和 upnpNAT 服务
|
|
59
|
+
- 自动检测 NAT 类型,尝试端口映射
|
|
60
|
+
- 为跨网络直连提供技术支持
|
|
61
|
+
|
|
62
|
+
### 3. 发现流程
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
Agent 4 (公司) 想联系 Agent 1 (家里)
|
|
66
|
+
|
|
67
|
+
1. Agent 4 通过 DHT 查询 Agent 1
|
|
68
|
+
2. 手机作为 DHT 节点,返回 Agent 1 的地址信息
|
|
69
|
+
3. Agent 4 尝试 NAT 穿透直连 Agent 1
|
|
70
|
+
4. 成功:直接 P2P 通信
|
|
71
|
+
5. 失败:需要公共中继节点(未来扩展)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## 技术方案
|
|
75
|
+
|
|
76
|
+
### 依赖添加
|
|
77
|
+
|
|
78
|
+
```json
|
|
79
|
+
{
|
|
80
|
+
"@libp2p/autonat": "^1.0.0",
|
|
81
|
+
"@libp2p/upnp-nat": "^1.0.0"
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### P2P 网络配置
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
services.autonat = autoNAT();
|
|
89
|
+
services.upnp = upnpNAT();
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 部署方式
|
|
93
|
+
|
|
94
|
+
- **Termux** (Android):使用脚本启动
|
|
95
|
+
- **iSH** (iOS):Alpine Linux 环境
|
|
96
|
+
- **原生 App**:未来可选
|
|
97
|
+
|
|
98
|
+
## 待解决问题
|
|
99
|
+
|
|
100
|
+
| 问题 | 状态 | 方案 |
|
|
101
|
+
|------|------|------|
|
|
102
|
+
| NAT 穿透成功率 | 🔴 未验证 | 依赖 autoNAT/upnpNAT,需测试 |
|
|
103
|
+
| 手机后台运行 | 🟡 部分可行 | Termux + 关闭电池优化 |
|
|
104
|
+
| 跨网络直连 | 🔴 未验证 | 需要至少一方有可穿透的 NAT |
|
|
105
|
+
| 公共中继备份 | 🔴 未实现 | 打洞失败时的备选方案 |
|
|
106
|
+
|
|
107
|
+
## 实现状态
|
|
108
|
+
|
|
109
|
+
🚧 **设计阶段** - 等待本地测试验证
|
|
110
|
+
|
|
111
|
+
## 下一步
|
|
112
|
+
|
|
113
|
+
1. 本地局域网测试验证基础功能
|
|
114
|
+
2. Termux 真机测试
|
|
115
|
+
3. NAT 穿透成功率测试
|
|
116
|
+
4. 根据测试结果决定是否继续推进
|
|
117
|
+
|
|
118
|
+
## 相关资源
|
|
119
|
+
|
|
120
|
+
- [Termux 安装指南](./docs/TERMUX.md)(待创建)
|
|
121
|
+
- [libp2p NAT 穿透文档](https://docs.libp2p.io/concepts/nat/)
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
*Created: 2026-03-05*
|
|
126
|
+
*Status: Design Proposal*
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
# F2A 借鉴 A2A 协议的设计改进
|
|
2
|
+
|
|
3
|
+
> 本文档记录从 Google A2A (Agent2Agent) 协议中可以借鉴的设计思路,用于改进 F2A 协议。
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 1. Agent Card 概念
|
|
8
|
+
|
|
9
|
+
### 现状
|
|
10
|
+
当前 F2A 的发现消息仅包含基础信息:
|
|
11
|
+
```javascript
|
|
12
|
+
{
|
|
13
|
+
type: 'F2A_DISCOVER',
|
|
14
|
+
agentId: 'xxx',
|
|
15
|
+
publicKey: 'xxx',
|
|
16
|
+
port: 9000
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### 借鉴 A2A 的改进
|
|
21
|
+
将发现消息扩展为包含更多元数据,类似 A2A 的 Agent Card:
|
|
22
|
+
```javascript
|
|
23
|
+
{
|
|
24
|
+
type: 'F2A_DISCOVER',
|
|
25
|
+
agentId: 'xxx',
|
|
26
|
+
publicKey: 'xxx',
|
|
27
|
+
port: 9000,
|
|
28
|
+
displayName: 'WeatherAgent',
|
|
29
|
+
capabilities: ['chat', 'file_transfer', 'skill_invocation'],
|
|
30
|
+
skills: [
|
|
31
|
+
{
|
|
32
|
+
name: 'weather',
|
|
33
|
+
description: 'Get weather info for a location',
|
|
34
|
+
parameters: {
|
|
35
|
+
location: { type: 'string', required: true },
|
|
36
|
+
units: { type: 'string', enum: ['celsius', 'fahrenheit'], default: 'celsius' }
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: 'translate',
|
|
41
|
+
description: 'Translate text between languages'
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
authentication: {
|
|
45
|
+
requireConfirmation: true,
|
|
46
|
+
securityLevel: 'medium'
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 好处
|
|
52
|
+
- **能力发现**: 其他 Agent 可以在连接前了解你能做什么
|
|
53
|
+
- **动态协商**: 支持交互模式的协商(文本、文件、结构化数据)
|
|
54
|
+
- **自描述**: 减少人工配置,实现真正的即插即用
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 2. 任务生命周期管理
|
|
59
|
+
|
|
60
|
+
### 现状
|
|
61
|
+
F2A 目前只有简单的消息发送,缺乏任务状态跟踪:
|
|
62
|
+
```javascript
|
|
63
|
+
p2p.sendToPeer(peerId, {
|
|
64
|
+
type: 'message',
|
|
65
|
+
content: '...'
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 借鉴 A2A 的改进
|
|
70
|
+
引入 A2A 的任务状态机模型:
|
|
71
|
+
```
|
|
72
|
+
submitted → working → input-required → completed
|
|
73
|
+
↓
|
|
74
|
+
failed / canceled
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 实现示例
|
|
78
|
+
```javascript
|
|
79
|
+
// 发送任务
|
|
80
|
+
const taskId = await p2p.sendTask(peerId, {
|
|
81
|
+
skill: 'weather',
|
|
82
|
+
parameters: { location: 'Beijing' }
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// 任务状态流转
|
|
86
|
+
p2p.on('task_update', ({ taskId, status, artifact }) => {
|
|
87
|
+
switch (status) {
|
|
88
|
+
case 'working':
|
|
89
|
+
console.log(`任务 ${taskId} 处理中...`);
|
|
90
|
+
break;
|
|
91
|
+
case 'input-required':
|
|
92
|
+
// 需要用户提供额外信息
|
|
93
|
+
console.log(`需要更多信息: ${artifact.description}`);
|
|
94
|
+
break;
|
|
95
|
+
case 'completed':
|
|
96
|
+
console.log(`任务完成: ${artifact.content}`);
|
|
97
|
+
break;
|
|
98
|
+
case 'failed':
|
|
99
|
+
console.log(`任务失败: ${artifact.error}`);
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// 取消任务
|
|
105
|
+
await p2p.cancelTask(peerId, taskId);
|
|
106
|
+
|
|
107
|
+
// 查询任务状态
|
|
108
|
+
const task = await p2p.getTask(peerId, taskId);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### 好处
|
|
112
|
+
- **长任务支持**: 适合需要长时间运行的任务
|
|
113
|
+
- **人机协作**: 支持 human-in-the-loop 场景
|
|
114
|
+
- **可观测性**: 可以跟踪任务进度和状态
|
|
115
|
+
- **容错性**: 支持任务取消和失败处理
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## 3. 标准化消息格式
|
|
120
|
+
|
|
121
|
+
### 现状
|
|
122
|
+
F2A 使用简单的 JSON 消息,缺乏严格的结构定义:
|
|
123
|
+
```javascript
|
|
124
|
+
{
|
|
125
|
+
type: 'message',
|
|
126
|
+
content: '...'
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 借鉴 A2A 的改进
|
|
131
|
+
参考 A2A 的 Protocol Buffer 定义,设计更严格的 JSON Schema:
|
|
132
|
+
|
|
133
|
+
#### 3.1 消息结构标准化
|
|
134
|
+
```javascript
|
|
135
|
+
// 基础消息结构
|
|
136
|
+
{
|
|
137
|
+
// 协议元数据
|
|
138
|
+
"protocol": {
|
|
139
|
+
"name": "F2A",
|
|
140
|
+
"version": "1.0.0"
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
// 消息元数据
|
|
144
|
+
"meta": {
|
|
145
|
+
"messageId": "uuid",
|
|
146
|
+
"timestamp": 1700000000000,
|
|
147
|
+
"senderId": "f2a-xxx-xxx",
|
|
148
|
+
"correlationId": "uuid" // 用于关联请求和响应
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
// 消息负载
|
|
152
|
+
"payload": {
|
|
153
|
+
"type": "task_request", // task_request | task_response | notification | etc.
|
|
154
|
+
"data": { ... }
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
// 扩展字段
|
|
158
|
+
"extensions": {
|
|
159
|
+
"customField": "value"
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
#### 3.2 Part 结构(内容单元)
|
|
165
|
+
借鉴 A2A 的 Part 概念,统一内容表示:
|
|
166
|
+
```javascript
|
|
167
|
+
// 文本
|
|
168
|
+
{ "type": "text", "content": "Hello" }
|
|
169
|
+
|
|
170
|
+
// 文件
|
|
171
|
+
{
|
|
172
|
+
"type": "file",
|
|
173
|
+
"filename": "report.pdf",
|
|
174
|
+
"mimeType": "application/pdf",
|
|
175
|
+
"size": 1024000,
|
|
176
|
+
"fileId": "uuid" // 引用文件传输系统的 ID
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// 结构化数据
|
|
180
|
+
{
|
|
181
|
+
"type": "data",
|
|
182
|
+
"mimeType": "application/json",
|
|
183
|
+
"content": { "temperature": 25, "unit": "celsius" }
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// 表单/输入请求
|
|
187
|
+
{
|
|
188
|
+
"type": "form",
|
|
189
|
+
"fields": [
|
|
190
|
+
{ "name": "location", "type": "text", "required": true },
|
|
191
|
+
{ "name": "units", "type": "select", "options": ["C", "F"] }
|
|
192
|
+
]
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
#### 3.3 版本兼容性处理
|
|
197
|
+
```javascript
|
|
198
|
+
{
|
|
199
|
+
"protocol": {
|
|
200
|
+
"name": "F2A",
|
|
201
|
+
"version": "1.1.0", // 发送方版本
|
|
202
|
+
"minCompatibleVersion": "1.0.0" // 最低兼容版本
|
|
203
|
+
},
|
|
204
|
+
|
|
205
|
+
// 接收方如果不支持某个字段,可以安全忽略
|
|
206
|
+
"payload": {
|
|
207
|
+
"type": "task_request",
|
|
208
|
+
"data": { ... },
|
|
209
|
+
"deprecatedField": "..." // 标记废弃的字段
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
#### 3.4 扩展机制
|
|
215
|
+
```javascript
|
|
216
|
+
{
|
|
217
|
+
"payload": {
|
|
218
|
+
"type": "task_request",
|
|
219
|
+
"data": { ... }
|
|
220
|
+
},
|
|
221
|
+
|
|
222
|
+
// 厂商/应用特定扩展
|
|
223
|
+
"extensions": {
|
|
224
|
+
"com.example.customFeature": { ... },
|
|
225
|
+
"org.f2a.streaming": { "enabled": true }
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### 好处
|
|
231
|
+
- **互操作性**: 严格的结构定义确保不同实现能正确通信
|
|
232
|
+
- **向前兼容**: 版本机制支持平滑升级
|
|
233
|
+
- **可扩展性**: 扩展机制允许自定义功能而不破坏核心协议
|
|
234
|
+
- **类型安全**: 明确的字段类型减少解析错误
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
## 实施建议
|
|
239
|
+
|
|
240
|
+
### 优先级
|
|
241
|
+
1. **高**: Agent Card - 立即提升发现机制的能力
|
|
242
|
+
2. **中**: 任务生命周期 - 支持更复杂的协作场景
|
|
243
|
+
3. **低**: 消息格式标准化 - 在需要与其他系统集成时实施
|
|
244
|
+
|
|
245
|
+
### 向后兼容
|
|
246
|
+
- 新字段使用可选(optional)方式添加
|
|
247
|
+
- 保留旧版发现消息解析支持
|
|
248
|
+
- 版本协商机制:连接时交换支持的协议版本
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## 参考
|
|
253
|
+
|
|
254
|
+
- [A2A Protocol Specification](https://a2a-protocol.org/latest/specification/)
|
|
255
|
+
- [A2A GitHub Repository](https://github.com/a2aproject/A2A)
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
# F2A 未来改进方向
|
|
260
|
+
|
|
261
|
+
## 1. mDNS 服务发现
|
|
262
|
+
|
|
263
|
+
### 现状
|
|
264
|
+
当前 F2A 使用 UDP 多播(239.255.255.250:8768)进行设备发现,但部分路由器会阻止该多播地址。
|
|
265
|
+
|
|
266
|
+
### mDNS 优势
|
|
267
|
+
```
|
|
268
|
+
mDNS (Multicast DNS) - 224.0.0.251:5353
|
|
269
|
+
- 更可靠:路由器通常允许(用于 AirDrop、打印机发现)
|
|
270
|
+
- 服务类型:只发现 F2A Agent,不混杂其他流量
|
|
271
|
+
- 元数据:可以携带 Agent ID、公钥、版本等 TXT 记录
|
|
272
|
+
- 冲突解决:自动处理主机名冲突
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### 推荐方案:混合发现机制
|
|
276
|
+
```javascript
|
|
277
|
+
// 发现优先级
|
|
278
|
+
1. mDNS (Bonjour/Avahi) ← 首选
|
|
279
|
+
2. UDP 多播 239.255.0.250 ← 备用
|
|
280
|
+
3. UDP 广播 255.255.255.255 ← 最后手段
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### 实现示例
|
|
284
|
+
```javascript
|
|
285
|
+
const bonjour = require('bonjour')();
|
|
286
|
+
|
|
287
|
+
// 发布 F2A 服务
|
|
288
|
+
bonjour.publish({
|
|
289
|
+
name: `F2A-${agentId.slice(0, 8)}`,
|
|
290
|
+
type: 'f2a',
|
|
291
|
+
port: 9000,
|
|
292
|
+
txt: {
|
|
293
|
+
agentId: 'f2a-xxx-xxx',
|
|
294
|
+
publicKey: 'xxx...',
|
|
295
|
+
version: '1.0.0',
|
|
296
|
+
capabilities: 'chat,file_transfer'
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
// 发现其他 F2A Agent
|
|
301
|
+
bonjour.find({ type: 'f2a' }, (service) => {
|
|
302
|
+
console.log('发现:', service.name);
|
|
303
|
+
console.log('地址:', service.referer.address);
|
|
304
|
+
console.log('端口:', service.port);
|
|
305
|
+
console.log('元数据:', service.txt);
|
|
306
|
+
});
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### 参考
|
|
310
|
+
- [Bonjour](https://github.com/watson/bonjour) - Node.js mDNS 实现
|
|
311
|
+
- [Avahi](https://www.avahi.org/) - Linux mDNS/DNS-SD 实现
|
|
312
|
+
- [RFC 6762](https://tools.ietf.org/html/rfc6762) - mDNS 标准
|
|
313
|
+
|
|
314
|
+
---
|
|
315
|
+
|
|
316
|
+
*最后更新:2026-03-02*
|