@f2a/network 0.1.2 → 0.1.3
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/package.json +8 -1
- package/.github/workflows/ci.yml +0 -113
- package/.github/workflows/publish.yml +0 -60
- package/MONOREPO.md +0 -58
- package/SKILL.md +0 -137
- package/dist/adapters/openclaw.d.ts +0 -103
- package/dist/adapters/openclaw.d.ts.map +0 -1
- package/dist/adapters/openclaw.js +0 -297
- package/dist/adapters/openclaw.js.map +0 -1
- package/dist/core/connection-manager.d.ts +0 -80
- package/dist/core/connection-manager.d.ts.map +0 -1
- package/dist/core/connection-manager.js +0 -235
- package/dist/core/connection-manager.js.map +0 -1
- package/dist/core/connection-manager.test.d.ts +0 -2
- package/dist/core/connection-manager.test.d.ts.map +0 -1
- package/dist/core/connection-manager.test.js +0 -52
- package/dist/core/connection-manager.test.js.map +0 -1
- package/dist/core/identity.d.ts +0 -47
- package/dist/core/identity.d.ts.map +0 -1
- package/dist/core/identity.js +0 -130
- package/dist/core/identity.js.map +0 -1
- package/dist/core/identity.test.d.ts +0 -2
- package/dist/core/identity.test.d.ts.map +0 -1
- package/dist/core/identity.test.js +0 -43
- package/dist/core/identity.test.js.map +0 -1
- package/dist/core/serverless.d.ts +0 -155
- package/dist/core/serverless.d.ts.map +0 -1
- package/dist/core/serverless.js +0 -615
- package/dist/core/serverless.js.map +0 -1
- package/dist/daemon/webhook.test.d.ts +0 -2
- package/dist/daemon/webhook.test.d.ts.map +0 -1
- package/dist/daemon/webhook.test.js +0 -24
- package/dist/daemon/webhook.test.js.map +0 -1
- package/dist/protocol/messages.d.ts +0 -739
- package/dist/protocol/messages.d.ts.map +0 -1
- package/dist/protocol/messages.js +0 -188
- package/dist/protocol/messages.js.map +0 -1
- package/dist/protocol/messages.test.d.ts +0 -2
- package/dist/protocol/messages.test.d.ts.map +0 -1
- package/dist/protocol/messages.test.js +0 -55
- package/dist/protocol/messages.test.js.map +0 -1
- package/docs/F2A-PROTOCOL.md +0 -61
- package/docs/MOBILE_BOOTSTRAP_DESIGN.md +0 -126
- package/docs/a2a-lessons.md +0 -316
- package/docs/middleware-guide.md +0 -448
- package/docs/readme-update-checklist.md +0 -90
- package/docs/reputation-guide.md +0 -396
- package/docs/rfcs/001-reputation-system.md +0 -712
- package/docs/security-design.md +0 -247
- package/install.sh +0 -231
- package/packages/openclaw-adapter/README.md +0 -510
- package/packages/openclaw-adapter/openclaw.plugin.json +0 -106
- package/packages/openclaw-adapter/package.json +0 -40
- package/packages/openclaw-adapter/src/announcement-queue.test.ts +0 -449
- package/packages/openclaw-adapter/src/announcement-queue.ts +0 -403
- package/packages/openclaw-adapter/src/capability-detector.test.ts +0 -99
- package/packages/openclaw-adapter/src/capability-detector.ts +0 -183
- package/packages/openclaw-adapter/src/claim-handlers.test.ts +0 -974
- package/packages/openclaw-adapter/src/claim-handlers.ts +0 -482
- package/packages/openclaw-adapter/src/connector.business.test.ts +0 -583
- package/packages/openclaw-adapter/src/connector.ts +0 -795
- package/packages/openclaw-adapter/src/index.test.ts +0 -82
- package/packages/openclaw-adapter/src/index.ts +0 -18
- package/packages/openclaw-adapter/src/integration.e2e.test.ts +0 -829
- package/packages/openclaw-adapter/src/logger.ts +0 -51
- package/packages/openclaw-adapter/src/network-client.test.ts +0 -266
- package/packages/openclaw-adapter/src/network-client.ts +0 -251
- package/packages/openclaw-adapter/src/network-recovery.test.ts +0 -465
- package/packages/openclaw-adapter/src/node-manager.test.ts +0 -136
- package/packages/openclaw-adapter/src/node-manager.ts +0 -429
- package/packages/openclaw-adapter/src/plugin.test.ts +0 -439
- package/packages/openclaw-adapter/src/plugin.ts +0 -104
- package/packages/openclaw-adapter/src/reputation.test.ts +0 -221
- package/packages/openclaw-adapter/src/reputation.ts +0 -368
- package/packages/openclaw-adapter/src/task-guard.test.ts +0 -502
- package/packages/openclaw-adapter/src/task-guard.ts +0 -860
- package/packages/openclaw-adapter/src/task-queue.concurrency.test.ts +0 -462
- package/packages/openclaw-adapter/src/task-queue.edge-cases.test.ts +0 -284
- package/packages/openclaw-adapter/src/task-queue.persistence.test.ts +0 -408
- package/packages/openclaw-adapter/src/task-queue.ts +0 -668
- package/packages/openclaw-adapter/src/tool-handlers.test.ts +0 -906
- package/packages/openclaw-adapter/src/tool-handlers.ts +0 -574
- package/packages/openclaw-adapter/src/types.ts +0 -361
- package/packages/openclaw-adapter/src/webhook-pusher.test.ts +0 -188
- package/packages/openclaw-adapter/src/webhook-pusher.ts +0 -220
- package/packages/openclaw-adapter/src/webhook-server.test.ts +0 -580
- package/packages/openclaw-adapter/src/webhook-server.ts +0 -202
- package/packages/openclaw-adapter/tsconfig.json +0 -20
- package/src/cli/commands.test.ts +0 -157
- package/src/cli/commands.ts +0 -129
- package/src/cli/index.test.ts +0 -77
- package/src/cli/index.ts +0 -234
- package/src/core/autonomous-economy.test.ts +0 -291
- package/src/core/autonomous-economy.ts +0 -428
- package/src/core/e2ee-crypto.test.ts +0 -125
- package/src/core/e2ee-crypto.ts +0 -246
- package/src/core/f2a.test.ts +0 -269
- package/src/core/f2a.ts +0 -618
- package/src/core/p2p-network.test.ts +0 -199
- package/src/core/p2p-network.ts +0 -1432
- package/src/core/reputation-security.test.ts +0 -403
- package/src/core/reputation-security.ts +0 -562
- package/src/core/reputation.test.ts +0 -260
- package/src/core/reputation.ts +0 -576
- package/src/core/review-committee.test.ts +0 -380
- package/src/core/review-committee.ts +0 -401
- package/src/core/token-manager.test.ts +0 -133
- package/src/core/token-manager.ts +0 -140
- package/src/daemon/control-server.test.ts +0 -216
- package/src/daemon/control-server.ts +0 -292
- package/src/daemon/index.test.ts +0 -85
- package/src/daemon/index.ts +0 -89
- package/src/daemon/main.ts +0 -44
- package/src/daemon/start.ts +0 -29
- package/src/daemon/webhook.test.ts +0 -68
- package/src/daemon/webhook.ts +0 -105
- package/src/index.test.ts +0 -436
- package/src/index.ts +0 -72
- package/src/types/index.test.ts +0 -87
- package/src/types/index.ts +0 -341
- package/src/types/result.ts +0 -68
- package/src/utils/benchmark.ts +0 -237
- package/src/utils/logger.ts +0 -331
- package/src/utils/middleware.ts +0 -229
- package/src/utils/rate-limiter.ts +0 -207
- package/src/utils/signature.ts +0 -136
- package/src/utils/validation.ts +0 -186
- package/tests/docker/Dockerfile.node +0 -23
- package/tests/docker/Dockerfile.runner +0 -18
- package/tests/docker/docker-compose.test.yml +0 -73
- package/tests/integration/message-passing.test.ts +0 -109
- package/tests/integration/multi-node.test.ts +0 -92
- package/tests/integration/p2p-connection.test.ts +0 -83
- package/tests/integration/test-config.ts +0 -32
- package/tsconfig.json +0 -21
- package/vitest.config.ts +0 -26
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* F2A Webhook Pusher
|
|
3
|
-
* 优先使用 webhook 推送任务到 OpenClaw,失败时退化为轮询
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { QueuedTask } from './task-queue.js';
|
|
7
|
-
import { queueLogger as logger } from './logger.js';
|
|
8
|
-
|
|
9
|
-
export interface WebhookPushConfig {
|
|
10
|
-
/** OpenClaw webhook URL */
|
|
11
|
-
url: string;
|
|
12
|
-
/** Webhook 认证 token */
|
|
13
|
-
token: string;
|
|
14
|
-
/** 推送超时(毫秒) */
|
|
15
|
-
timeout?: number;
|
|
16
|
-
/** 是否启用 webhook 推送 */
|
|
17
|
-
enabled?: boolean;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface WebhookPushResult {
|
|
21
|
-
success: boolean;
|
|
22
|
-
error?: string;
|
|
23
|
-
latency?: number;
|
|
24
|
-
/** 是否处于降级模式(冷却期但允许轮询) */
|
|
25
|
-
degraded?: boolean;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export class WebhookPusher {
|
|
29
|
-
private config: WebhookPushConfig;
|
|
30
|
-
private consecutiveFailures = 0;
|
|
31
|
-
private lastFailureTime = 0;
|
|
32
|
-
private degradedMode = false;
|
|
33
|
-
|
|
34
|
-
// 连续失败后暂停推送一段时间
|
|
35
|
-
private readonly FAILURE_THRESHOLD = 3;
|
|
36
|
-
private readonly BASE_COOLDOWN_MS = 10000; // 基础冷却期 10 秒
|
|
37
|
-
private readonly MAX_COOLDOWN_MS = 300000; // 最大冷却期 5 分钟
|
|
38
|
-
|
|
39
|
-
constructor(config: WebhookPushConfig) {
|
|
40
|
-
this.config = {
|
|
41
|
-
timeout: 5000,
|
|
42
|
-
enabled: true,
|
|
43
|
-
...config
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 计算当前冷却期(指数退避)
|
|
49
|
-
*/
|
|
50
|
-
private getCooldownMs(): number {
|
|
51
|
-
if (this.consecutiveFailures <= this.FAILURE_THRESHOLD) {
|
|
52
|
-
return this.BASE_COOLDOWN_MS;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// 指数退避:冷却期随失败次数增加,但有上限
|
|
56
|
-
const multiplier = Math.pow(2, this.consecutiveFailures - this.FAILURE_THRESHOLD);
|
|
57
|
-
const cooldown = Math.min(this.BASE_COOLDOWN_MS * multiplier, this.MAX_COOLDOWN_MS);
|
|
58
|
-
|
|
59
|
-
logger.debug(`冷却期计算: 失败次数=${this.consecutiveFailures}, 冷却期=${Math.round(cooldown / 1000)}秒`);
|
|
60
|
-
return cooldown;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 推送任务到 OpenClaw webhook
|
|
65
|
-
*/
|
|
66
|
-
async pushTask(task: QueuedTask): Promise<WebhookPushResult> {
|
|
67
|
-
if (!this.config.enabled) {
|
|
68
|
-
return { success: false, error: 'Webhook push disabled' };
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 检查是否在冷却期
|
|
72
|
-
if (this.isInCooldown()) {
|
|
73
|
-
const remainingMs = this.getCooldownMs() - (Date.now() - this.lastFailureTime);
|
|
74
|
-
|
|
75
|
-
// 降级机制:冷却期内仍返回特殊结果,让调用方知道可以通过轮询处理
|
|
76
|
-
// 返回 degraded: true 表示处于降级模式,任务需要通过轮询机制处理
|
|
77
|
-
return {
|
|
78
|
-
success: false,
|
|
79
|
-
error: `In cooldown (${Math.round(remainingMs / 1000)}s remaining)`,
|
|
80
|
-
degraded: true
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const start = Date.now();
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
const response = await fetch(`${this.config.url}/hooks/agent`, {
|
|
88
|
-
method: 'POST',
|
|
89
|
-
headers: {
|
|
90
|
-
'Authorization': `Bearer ${this.config.token}`,
|
|
91
|
-
'Content-Type': 'application/json'
|
|
92
|
-
},
|
|
93
|
-
body: JSON.stringify({
|
|
94
|
-
message: `F2A 远程任务: ${task.taskType || 'unknown'}`,
|
|
95
|
-
name: 'F2A',
|
|
96
|
-
sessionKey: `f2a:${task.taskId}`,
|
|
97
|
-
wakeMode: 'now',
|
|
98
|
-
deliver: false,
|
|
99
|
-
timeoutSeconds: 120
|
|
100
|
-
}),
|
|
101
|
-
signal: AbortSignal.timeout(this.config.timeout!)
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
const latency = Date.now() - start;
|
|
105
|
-
|
|
106
|
-
if (response.ok || response.status === 202) {
|
|
107
|
-
// 成功,重置失败计数和降级模式
|
|
108
|
-
this.consecutiveFailures = 0;
|
|
109
|
-
this.degradedMode = false;
|
|
110
|
-
return { success: true, latency };
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// 失败
|
|
114
|
-
this.recordFailure();
|
|
115
|
-
return {
|
|
116
|
-
success: false,
|
|
117
|
-
error: `HTTP ${response.status}: ${response.statusText}`,
|
|
118
|
-
latency
|
|
119
|
-
};
|
|
120
|
-
} catch (error) {
|
|
121
|
-
this.recordFailure();
|
|
122
|
-
return {
|
|
123
|
-
success: false,
|
|
124
|
-
error: error instanceof Error ? error.message : 'Unknown error',
|
|
125
|
-
latency: Date.now() - start
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* 批量推送任务
|
|
132
|
-
*/
|
|
133
|
-
async pushTasks(tasks: QueuedTask[]): Promise<Map<string, WebhookPushResult>> {
|
|
134
|
-
const results = new Map<string, WebhookPushResult>();
|
|
135
|
-
|
|
136
|
-
for (const task of tasks) {
|
|
137
|
-
const result = await this.pushTask(task);
|
|
138
|
-
results.set(task.taskId, result);
|
|
139
|
-
|
|
140
|
-
// 如果进入冷却期,停止推送但标记剩余任务为可轮询
|
|
141
|
-
if (result.degraded) {
|
|
142
|
-
// 剩余任务标记为需要轮询
|
|
143
|
-
const remainingTasks = tasks.filter(t => !results.has(t.taskId));
|
|
144
|
-
for (const remaining of remainingTasks) {
|
|
145
|
-
results.set(remaining.taskId, {
|
|
146
|
-
success: false,
|
|
147
|
-
error: 'Skipped due to cooldown',
|
|
148
|
-
degraded: true
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
break;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return results;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* 检查是否在冷却期
|
|
160
|
-
*/
|
|
161
|
-
private isInCooldown(): boolean {
|
|
162
|
-
if (this.consecutiveFailures < this.FAILURE_THRESHOLD) {
|
|
163
|
-
return false;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const elapsed = Date.now() - this.lastFailureTime;
|
|
167
|
-
const cooldownMs = this.getCooldownMs();
|
|
168
|
-
return elapsed < cooldownMs;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* 记录失败
|
|
173
|
-
*/
|
|
174
|
-
private recordFailure(): void {
|
|
175
|
-
this.consecutiveFailures++;
|
|
176
|
-
this.lastFailureTime = Date.now();
|
|
177
|
-
this.degradedMode = true;
|
|
178
|
-
|
|
179
|
-
if (this.consecutiveFailures >= this.FAILURE_THRESHOLD) {
|
|
180
|
-
const cooldownSec = Math.round(this.getCooldownMs() / 1000);
|
|
181
|
-
logger.warn(`连续失败 ${this.consecutiveFailures} 次,进入 ${cooldownSec} 秒冷却期(降级模式启用)`);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* 手动重置冷却期(用于外部干预)
|
|
187
|
-
*/
|
|
188
|
-
resetCooldown(): void {
|
|
189
|
-
this.consecutiveFailures = 0;
|
|
190
|
-
this.lastFailureTime = 0;
|
|
191
|
-
this.degradedMode = false;
|
|
192
|
-
logger.info('冷却期已手动重置');
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* 获取状态
|
|
197
|
-
*/
|
|
198
|
-
getStatus(): {
|
|
199
|
-
enabled: boolean;
|
|
200
|
-
consecutiveFailures: number;
|
|
201
|
-
inCooldown: boolean;
|
|
202
|
-
currentCooldownMs: number;
|
|
203
|
-
degradedMode: boolean;
|
|
204
|
-
} {
|
|
205
|
-
return {
|
|
206
|
-
enabled: this.config.enabled ?? true,
|
|
207
|
-
consecutiveFailures: this.consecutiveFailures,
|
|
208
|
-
inCooldown: this.isInCooldown(),
|
|
209
|
-
currentCooldownMs: this.isInCooldown() ? this.getCooldownMs() : 0,
|
|
210
|
-
degradedMode: this.degradedMode
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* 更新配置
|
|
216
|
-
*/
|
|
217
|
-
updateConfig(config: Partial<WebhookPushConfig>): void {
|
|
218
|
-
this.config = { ...this.config, ...config };
|
|
219
|
-
}
|
|
220
|
-
}
|