@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
package/src/index.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* F2A SDK 入口
|
|
3
|
+
* P2P networking protocol for OpenClaw Agents
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// 核心 P2P 网络
|
|
7
|
+
export { F2A } from './core/f2a.js';
|
|
8
|
+
export { P2PNetwork } from './core/p2p-network.js';
|
|
9
|
+
export { TokenManager, defaultTokenManager } from './core/token-manager.js';
|
|
10
|
+
export { E2EECrypto, defaultE2EECrypto } from './core/e2ee-crypto.js';
|
|
11
|
+
|
|
12
|
+
// 信誉系统 (Phase 1-4)
|
|
13
|
+
export { ReputationManager, REPUTATION_TIERS } from './core/reputation.js';
|
|
14
|
+
export type {
|
|
15
|
+
ReputationEntry,
|
|
16
|
+
ReputationEvent,
|
|
17
|
+
ReputationLevel,
|
|
18
|
+
ReputationTier,
|
|
19
|
+
ReputationConfig,
|
|
20
|
+
ReputationStorage
|
|
21
|
+
} from './core/reputation.js';
|
|
22
|
+
|
|
23
|
+
export { ReviewCommittee } from './core/review-committee.js';
|
|
24
|
+
export type {
|
|
25
|
+
TaskReview,
|
|
26
|
+
ReviewResult,
|
|
27
|
+
ReviewDimensions,
|
|
28
|
+
RiskFlag,
|
|
29
|
+
ReviewCommitteeConfig,
|
|
30
|
+
PendingReview
|
|
31
|
+
} from './core/review-committee.js';
|
|
32
|
+
|
|
33
|
+
export { AutonomousEconomy } from './core/autonomous-economy.js';
|
|
34
|
+
export type {
|
|
35
|
+
TaskRequest,
|
|
36
|
+
TaskCost,
|
|
37
|
+
TaskReward,
|
|
38
|
+
EconomyConfig,
|
|
39
|
+
PriorityQueueItem,
|
|
40
|
+
TaskExecutionResult
|
|
41
|
+
} from './core/autonomous-economy.js';
|
|
42
|
+
|
|
43
|
+
// 信誉安全机制 (Phase 3)
|
|
44
|
+
export {
|
|
45
|
+
ChainSignatureManager,
|
|
46
|
+
InvitationManager,
|
|
47
|
+
ChallengeManager
|
|
48
|
+
} from './core/reputation-security.js';
|
|
49
|
+
export type {
|
|
50
|
+
SignedReputationEvent,
|
|
51
|
+
ReviewerSignature,
|
|
52
|
+
InvitationRecord,
|
|
53
|
+
InvitationConfig,
|
|
54
|
+
ChallengeRecord,
|
|
55
|
+
ChallengeResult
|
|
56
|
+
} from './core/reputation-security.js';
|
|
57
|
+
|
|
58
|
+
// 工具模块
|
|
59
|
+
export { Logger } from './utils/logger.js';
|
|
60
|
+
export { RateLimiter } from './utils/rate-limiter.js';
|
|
61
|
+
export { RequestSigner } from './utils/signature.js';
|
|
62
|
+
export {
|
|
63
|
+
createMessageSizeLimitMiddleware,
|
|
64
|
+
createMessageTypeFilterMiddleware
|
|
65
|
+
} from './utils/middleware.js';
|
|
66
|
+
export type { Middleware, MiddlewareContext, MiddlewareResult } from './utils/middleware.js';
|
|
67
|
+
|
|
68
|
+
// 类型定义
|
|
69
|
+
export * from './types/index.js';
|
|
70
|
+
|
|
71
|
+
// 版本号
|
|
72
|
+
export const VERSION = '1.0.1';
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
AgentCapability,
|
|
4
|
+
AgentInfo,
|
|
5
|
+
F2AMessage,
|
|
6
|
+
F2AOptions,
|
|
7
|
+
Result,
|
|
8
|
+
TaskDelegateOptions,
|
|
9
|
+
WebhookConfig
|
|
10
|
+
} from './index.js';
|
|
11
|
+
|
|
12
|
+
describe('Types', () => {
|
|
13
|
+
it('should allow creating AgentCapability', () => {
|
|
14
|
+
const cap: AgentCapability = {
|
|
15
|
+
name: 'test-cap',
|
|
16
|
+
description: 'Test capability',
|
|
17
|
+
tools: ['tool1'],
|
|
18
|
+
parameters: {
|
|
19
|
+
param1: { type: 'string', required: true }
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
expect(cap.name).toBe('test-cap');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should allow creating AgentInfo', () => {
|
|
26
|
+
const info: AgentInfo = {
|
|
27
|
+
peerId: 'test-peer-id',
|
|
28
|
+
displayName: 'Test Agent',
|
|
29
|
+
agentType: 'openclaw',
|
|
30
|
+
version: '1.0.0',
|
|
31
|
+
capabilities: [],
|
|
32
|
+
protocolVersion: 'f2a/1.0',
|
|
33
|
+
lastSeen: Date.now(),
|
|
34
|
+
multiaddrs: []
|
|
35
|
+
};
|
|
36
|
+
expect(info.peerId).toBe('test-peer-id');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should allow creating F2AOptions', () => {
|
|
40
|
+
const options: F2AOptions = {
|
|
41
|
+
displayName: 'Test',
|
|
42
|
+
agentType: 'openclaw',
|
|
43
|
+
network: {
|
|
44
|
+
listenPort: 9000,
|
|
45
|
+
enableMDNS: true
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
expect(options.displayName).toBe('Test');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should allow creating Result types', () => {
|
|
52
|
+
const successResult: Result<string> = {
|
|
53
|
+
success: true,
|
|
54
|
+
data: 'test'
|
|
55
|
+
};
|
|
56
|
+
expect(successResult.success).toBe(true);
|
|
57
|
+
|
|
58
|
+
const errorResult: Result<string> = {
|
|
59
|
+
success: false,
|
|
60
|
+
error: 'Error message'
|
|
61
|
+
};
|
|
62
|
+
expect(errorResult.success).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should allow creating TaskDelegateOptions', () => {
|
|
66
|
+
const options: TaskDelegateOptions = {
|
|
67
|
+
capability: 'test-cap',
|
|
68
|
+
description: 'Test task',
|
|
69
|
+
parameters: { key: 'value' },
|
|
70
|
+
timeout: 30000,
|
|
71
|
+
parallel: true,
|
|
72
|
+
minResponses: 2
|
|
73
|
+
};
|
|
74
|
+
expect(options.capability).toBe('test-cap');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should allow creating WebhookConfig', () => {
|
|
78
|
+
const config: WebhookConfig = {
|
|
79
|
+
url: 'http://localhost:8080',
|
|
80
|
+
token: 'test-token',
|
|
81
|
+
timeout: 5000,
|
|
82
|
+
retries: 3,
|
|
83
|
+
retryDelay: 1000
|
|
84
|
+
};
|
|
85
|
+
expect(config.url).toBe('http://localhost:8080');
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* F2A P2P 网络核心类型定义
|
|
3
|
+
* 基于 libp2p 的 Agent 协作网络
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Multiaddr } from '@multiformats/multiaddr';
|
|
7
|
+
import { EventEmitter } from 'eventemitter3';
|
|
8
|
+
|
|
9
|
+
// ============================================================================
|
|
10
|
+
// 基础类型
|
|
11
|
+
// ============================================================================
|
|
12
|
+
|
|
13
|
+
export type SecurityLevel = 'low' | 'medium' | 'high';
|
|
14
|
+
|
|
15
|
+
export type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR';
|
|
16
|
+
|
|
17
|
+
// ============================================================================
|
|
18
|
+
// Agent 能力与身份
|
|
19
|
+
// ============================================================================
|
|
20
|
+
|
|
21
|
+
export interface AgentCapability {
|
|
22
|
+
/** 能力名称,如 "file-operation", "web-browsing", "code-generation" */
|
|
23
|
+
name: string;
|
|
24
|
+
/** 能力描述 */
|
|
25
|
+
description: string;
|
|
26
|
+
/** 支持的工具/操作 */
|
|
27
|
+
tools: string[];
|
|
28
|
+
/** 能力参数schema(可选) */
|
|
29
|
+
parameters?: Record<string, ParameterSchema>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ParameterSchema {
|
|
33
|
+
type: 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
34
|
+
required?: boolean;
|
|
35
|
+
default?: unknown;
|
|
36
|
+
description?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface AgentInfo {
|
|
40
|
+
/** libp2p PeerID */
|
|
41
|
+
peerId: string;
|
|
42
|
+
/** 可读名称 */
|
|
43
|
+
displayName?: string;
|
|
44
|
+
/** Agent 类型 */
|
|
45
|
+
agentType: 'openclaw' | 'claude-code' | 'codex' | 'custom';
|
|
46
|
+
/** 版本 */
|
|
47
|
+
version: string;
|
|
48
|
+
/** 支持的能力列表 */
|
|
49
|
+
capabilities: AgentCapability[];
|
|
50
|
+
/** 支持的协议版本 */
|
|
51
|
+
protocolVersion: string;
|
|
52
|
+
/** 最后活跃时间 */
|
|
53
|
+
lastSeen: number;
|
|
54
|
+
/** 网络地址 */
|
|
55
|
+
multiaddrs: string[];
|
|
56
|
+
/** 端到端加密公钥 (base64) */
|
|
57
|
+
encryptionPublicKey?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ============================================================================
|
|
61
|
+
// P2P 网络配置
|
|
62
|
+
// ============================================================================
|
|
63
|
+
|
|
64
|
+
export interface P2PNetworkConfig {
|
|
65
|
+
/** 监听端口 */
|
|
66
|
+
listenPort?: number;
|
|
67
|
+
/** 监听地址 */
|
|
68
|
+
listenAddresses?: string[];
|
|
69
|
+
/** 引导节点列表 */
|
|
70
|
+
bootstrapPeers?: string[];
|
|
71
|
+
/** 是否启用 MDNS 本地发现 */
|
|
72
|
+
enableMDNS?: boolean;
|
|
73
|
+
/** 是否启用 DHT (默认 true) */
|
|
74
|
+
enableDHT?: boolean;
|
|
75
|
+
/** DHT 服务器模式 (默认 false,即客户端模式) */
|
|
76
|
+
dhtServerMode?: boolean;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface F2AOptions {
|
|
80
|
+
/** 节点可读名称 */
|
|
81
|
+
displayName?: string;
|
|
82
|
+
/** Agent 类型 */
|
|
83
|
+
agentType?: string;
|
|
84
|
+
/** P2P 网络配置 */
|
|
85
|
+
network?: P2PNetworkConfig;
|
|
86
|
+
/** 安全级别 */
|
|
87
|
+
security?: SecurityConfig;
|
|
88
|
+
/** 日志级别 */
|
|
89
|
+
logLevel?: LogLevel;
|
|
90
|
+
/** 数据目录 */
|
|
91
|
+
dataDir?: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface SecurityConfig {
|
|
95
|
+
level: SecurityLevel;
|
|
96
|
+
/** 是否要求确认连接 */
|
|
97
|
+
requireConfirmation: boolean;
|
|
98
|
+
/** 是否验证签名 */
|
|
99
|
+
verifySignatures: boolean;
|
|
100
|
+
/** 白名单 */
|
|
101
|
+
whitelist?: Set<string>;
|
|
102
|
+
/** 黑名单 */
|
|
103
|
+
blacklist?: Set<string>;
|
|
104
|
+
/** 速率限制 */
|
|
105
|
+
rateLimit?: RateLimitConfig;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface RateLimitConfig {
|
|
109
|
+
maxRequests: number;
|
|
110
|
+
windowMs: number;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ============================================================================
|
|
114
|
+
// F2A 消息协议
|
|
115
|
+
// ============================================================================
|
|
116
|
+
|
|
117
|
+
export type F2AMessageType =
|
|
118
|
+
| 'DISCOVER' // 发现广播
|
|
119
|
+
| 'DISCOVER_RESP' // 发现响应
|
|
120
|
+
| 'CAPABILITY_QUERY' // 查询能力
|
|
121
|
+
| 'CAPABILITY_RESPONSE' // 能力响应
|
|
122
|
+
| 'TASK_REQUEST' // 任务请求
|
|
123
|
+
| 'TASK_RESPONSE' // 任务响应
|
|
124
|
+
| 'TASK_DELEGATE' // 任务转委托
|
|
125
|
+
| 'DECRYPT_FAILED' // 解密失败通知
|
|
126
|
+
| 'PING' // 心跳
|
|
127
|
+
| 'PONG'; // 心跳响应
|
|
128
|
+
|
|
129
|
+
export interface F2AMessage {
|
|
130
|
+
/** 消息ID */
|
|
131
|
+
id: string;
|
|
132
|
+
/** 消息类型 */
|
|
133
|
+
type: F2AMessageType;
|
|
134
|
+
/** 发送方 PeerID */
|
|
135
|
+
from: string;
|
|
136
|
+
/** 目标 PeerID(广播可为空) */
|
|
137
|
+
to?: string;
|
|
138
|
+
/** 时间戳 */
|
|
139
|
+
timestamp: number;
|
|
140
|
+
/** TTL */
|
|
141
|
+
ttl?: number;
|
|
142
|
+
/** 载荷 */
|
|
143
|
+
payload: unknown;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 发现广播
|
|
147
|
+
export interface DiscoverPayload {
|
|
148
|
+
agentInfo: AgentInfo;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 能力查询
|
|
152
|
+
export interface CapabilityQueryPayload {
|
|
153
|
+
/** 查询特定能力,空表示查询所有 */
|
|
154
|
+
capabilityName?: string;
|
|
155
|
+
/** 查询特定工具 */
|
|
156
|
+
toolName?: string;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// 能力响应
|
|
160
|
+
export interface CapabilityResponsePayload {
|
|
161
|
+
agentInfo: AgentInfo;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// 任务请求
|
|
165
|
+
export interface TaskRequestPayload {
|
|
166
|
+
/** 任务ID */
|
|
167
|
+
taskId: string;
|
|
168
|
+
/** 任务类型 */
|
|
169
|
+
taskType: string;
|
|
170
|
+
/** 任务描述 */
|
|
171
|
+
description: string;
|
|
172
|
+
/** 任务参数 */
|
|
173
|
+
parameters?: Record<string, unknown>;
|
|
174
|
+
/** 超时时间(秒) */
|
|
175
|
+
timeout?: number;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// 任务响应
|
|
179
|
+
export interface TaskResponsePayload {
|
|
180
|
+
/** 任务ID */
|
|
181
|
+
taskId: string;
|
|
182
|
+
/** 状态 */
|
|
183
|
+
status: 'success' | 'error' | 'rejected' | 'delegated';
|
|
184
|
+
/** 结果数据 */
|
|
185
|
+
result?: unknown;
|
|
186
|
+
/** 错误信息 */
|
|
187
|
+
error?: string;
|
|
188
|
+
/** 如果转委托,指向新节点 */
|
|
189
|
+
delegatedTo?: string;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ============================================================================
|
|
193
|
+
// 事件定义
|
|
194
|
+
// ============================================================================
|
|
195
|
+
|
|
196
|
+
export interface PeerDiscoveredEvent {
|
|
197
|
+
peerId: string;
|
|
198
|
+
agentInfo: AgentInfo;
|
|
199
|
+
multiaddrs: Multiaddr[];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export interface PeerConnectedEvent {
|
|
203
|
+
peerId: string;
|
|
204
|
+
direction: 'inbound' | 'outbound';
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface PeerDisconnectedEvent {
|
|
208
|
+
peerId: string;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export interface TaskRequestEvent {
|
|
212
|
+
taskId: string;
|
|
213
|
+
from: string;
|
|
214
|
+
taskType: string;
|
|
215
|
+
description: string;
|
|
216
|
+
parameters?: Record<string, unknown>;
|
|
217
|
+
timeout?: number;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export interface TaskResponseEvent {
|
|
221
|
+
taskId: string;
|
|
222
|
+
from: string;
|
|
223
|
+
status: 'success' | 'error' | 'rejected' | 'delegated';
|
|
224
|
+
result?: unknown;
|
|
225
|
+
error?: string;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export interface NetworkStartedEvent {
|
|
229
|
+
peerId: string;
|
|
230
|
+
listenAddresses: string[];
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export interface F2AEvents {
|
|
234
|
+
'peer:discovered': (event: PeerDiscoveredEvent) => void;
|
|
235
|
+
'peer:connected': (event: PeerConnectedEvent) => void;
|
|
236
|
+
'peer:disconnected': (event: PeerDisconnectedEvent) => void;
|
|
237
|
+
'task:request': (event: TaskRequestEvent) => void;
|
|
238
|
+
'task:response': (event: TaskResponseEvent) => void;
|
|
239
|
+
'network:started': (event: NetworkStartedEvent) => void;
|
|
240
|
+
'network:stopped': () => void;
|
|
241
|
+
'error': (error: Error) => void;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export type F2AEventEmitter = EventEmitter<F2AEvents>;
|
|
245
|
+
|
|
246
|
+
// ============================================================================
|
|
247
|
+
// 结果类型 (从 result.ts 重新导出)
|
|
248
|
+
// ============================================================================
|
|
249
|
+
|
|
250
|
+
// Result 和 AsyncResult 从 ./result 导出
|
|
251
|
+
|
|
252
|
+
// ============================================================================
|
|
253
|
+
// Webhook 配置
|
|
254
|
+
// ============================================================================
|
|
255
|
+
|
|
256
|
+
export interface WebhookConfig {
|
|
257
|
+
url: string;
|
|
258
|
+
token: string;
|
|
259
|
+
timeout?: number;
|
|
260
|
+
retries?: number;
|
|
261
|
+
retryDelay?: number;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// ============================================================================
|
|
265
|
+
// 任务委托
|
|
266
|
+
// ============================================================================
|
|
267
|
+
|
|
268
|
+
/** 任务委托重试选项 */
|
|
269
|
+
export interface TaskRetryOptions {
|
|
270
|
+
/** 最大重试次数(默认 3) */
|
|
271
|
+
maxRetries?: number;
|
|
272
|
+
/** 重试间隔毫秒(默认 1000) */
|
|
273
|
+
retryDelayMs?: number;
|
|
274
|
+
/** 发现超时毫秒(默认 5000) */
|
|
275
|
+
discoverTimeoutMs?: number;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export interface TaskDelegateOptions {
|
|
279
|
+
/** 目标能力 */
|
|
280
|
+
capability: string;
|
|
281
|
+
/** 任务描述 */
|
|
282
|
+
description: string;
|
|
283
|
+
/** 任务参数 */
|
|
284
|
+
parameters?: Record<string, unknown>;
|
|
285
|
+
/** 超时时间(秒) */
|
|
286
|
+
timeout?: number;
|
|
287
|
+
/** 是否允许多方并行执行 */
|
|
288
|
+
parallel?: boolean;
|
|
289
|
+
/** 最少响应数(parallel=true时) */
|
|
290
|
+
minResponses?: number;
|
|
291
|
+
/** 重试选项 */
|
|
292
|
+
retryOptions?: TaskRetryOptions;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export interface TaskDelegateResult {
|
|
296
|
+
taskId: string;
|
|
297
|
+
results: {
|
|
298
|
+
peerId: string;
|
|
299
|
+
status: 'success' | 'error' | 'timeout';
|
|
300
|
+
result?: unknown;
|
|
301
|
+
error?: string;
|
|
302
|
+
latency: number;
|
|
303
|
+
}[];
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// ============================================================================
|
|
307
|
+
// 路由表
|
|
308
|
+
// ============================================================================
|
|
309
|
+
|
|
310
|
+
export interface PeerInfo {
|
|
311
|
+
peerId: string;
|
|
312
|
+
agentInfo?: AgentInfo;
|
|
313
|
+
multiaddrs: Multiaddr[];
|
|
314
|
+
connected: boolean;
|
|
315
|
+
latency?: number;
|
|
316
|
+
/** 信誉分 */
|
|
317
|
+
reputation: number;
|
|
318
|
+
/** 连接时间 */
|
|
319
|
+
connectedAt?: number;
|
|
320
|
+
/** 最后活跃 */
|
|
321
|
+
lastSeen: number;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// ============================================================================
|
|
325
|
+
// 能力注册
|
|
326
|
+
// ============================================================================
|
|
327
|
+
|
|
328
|
+
export interface CapabilityHandler {
|
|
329
|
+
(params: Record<string, unknown>): Promise<unknown>;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export interface RegisteredCapability extends AgentCapability {
|
|
333
|
+
handler: CapabilityHandler;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// ============================================================================
|
|
337
|
+
// 统一 Result 类型
|
|
338
|
+
// 从 result.ts 重新导出,用于统一的错误处理模式
|
|
339
|
+
// ============================================================================
|
|
340
|
+
|
|
341
|
+
export * from './result.js';
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 统一的错误类型和错误码
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export type ErrorCode =
|
|
6
|
+
// 网络错误
|
|
7
|
+
| 'NETWORK_NOT_STARTED'
|
|
8
|
+
| 'NETWORK_ALREADY_RUNNING'
|
|
9
|
+
| 'PEER_NOT_FOUND'
|
|
10
|
+
| 'CONNECTION_FAILED'
|
|
11
|
+
| 'TIMEOUT'
|
|
12
|
+
// DHT 错误
|
|
13
|
+
| 'DHT_NOT_AVAILABLE'
|
|
14
|
+
| 'DHT_LOOKUP_FAILED'
|
|
15
|
+
// 加密错误
|
|
16
|
+
| 'ENCRYPTION_NOT_READY'
|
|
17
|
+
| 'ENCRYPTION_FAILED'
|
|
18
|
+
// 任务错误
|
|
19
|
+
| 'TASK_NOT_FOUND'
|
|
20
|
+
| 'TASK_REJECTED'
|
|
21
|
+
| 'TASK_FAILED'
|
|
22
|
+
| 'CAPABILITY_NOT_SUPPORTED'
|
|
23
|
+
| 'INVALID_OPTIONS'
|
|
24
|
+
// 安全错误
|
|
25
|
+
| 'UNAUTHORIZED'
|
|
26
|
+
| 'FORBIDDEN'
|
|
27
|
+
| 'RATE_LIMITED'
|
|
28
|
+
// 通用错误
|
|
29
|
+
| 'INVALID_PARAMS'
|
|
30
|
+
| 'INTERNAL_ERROR'
|
|
31
|
+
| 'UNKNOWN';
|
|
32
|
+
|
|
33
|
+
export interface F2AError {
|
|
34
|
+
code: ErrorCode;
|
|
35
|
+
message: string;
|
|
36
|
+
details?: Record<string, unknown>;
|
|
37
|
+
cause?: Error;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function createError(
|
|
41
|
+
code: ErrorCode,
|
|
42
|
+
message: string,
|
|
43
|
+
details?: Record<string, unknown>,
|
|
44
|
+
cause?: Error
|
|
45
|
+
): F2AError {
|
|
46
|
+
return { code, message, details, cause };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 统一的 Result 类型
|
|
51
|
+
*/
|
|
52
|
+
export type Result<T> =
|
|
53
|
+
| { success: true; data: T; error?: never }
|
|
54
|
+
| { success: false; error: F2AError; data?: never };
|
|
55
|
+
|
|
56
|
+
export function success<T>(data: T): Result<T> {
|
|
57
|
+
return { success: true, data };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function failure<T>(error: F2AError): Result<T> {
|
|
61
|
+
return { success: false, error };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function failureFromError<T>(code: ErrorCode, message: string, cause?: Error): Result<T> {
|
|
65
|
+
return { success: false, error: createError(code, message, undefined, cause) };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type AsyncResult<T> = Promise<Result<T>>;
|