@f2a/network 0.1.2 → 0.2.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 +278 -63
- package/dist/cli/commands.d.ts.map +1 -1
- package/dist/cli/commands.js +29 -2
- package/dist/cli/commands.js.map +1 -1
- package/dist/cli/config.d.ts +176 -0
- package/dist/cli/config.d.ts.map +1 -0
- package/dist/cli/config.js +386 -0
- package/dist/cli/config.js.map +1 -0
- package/dist/cli/daemon.d.ts +54 -0
- package/dist/cli/daemon.d.ts.map +1 -0
- package/dist/cli/daemon.js +572 -0
- package/dist/cli/daemon.js.map +1 -0
- package/dist/cli/index.js +90 -16
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/init.d.ts +13 -0
- package/dist/cli/init.d.ts.map +1 -0
- package/dist/cli/init.js +352 -0
- package/dist/cli/init.js.map +1 -0
- package/dist/core/e2ee-crypto.d.ts +127 -1
- package/dist/core/e2ee-crypto.d.ts.map +1 -1
- package/dist/core/e2ee-crypto.js +446 -12
- package/dist/core/e2ee-crypto.js.map +1 -1
- package/dist/core/f2a.d.ts +2 -1
- package/dist/core/f2a.d.ts.map +1 -1
- package/dist/core/f2a.js +6 -2
- package/dist/core/f2a.js.map +1 -1
- package/dist/core/identity/encrypted-key-store.d.ts +19 -0
- package/dist/core/identity/encrypted-key-store.d.ts.map +1 -0
- package/dist/core/identity/encrypted-key-store.js +72 -0
- package/dist/core/identity/encrypted-key-store.js.map +1 -0
- package/dist/core/identity/identity-manager.d.ts +133 -0
- package/dist/core/identity/identity-manager.d.ts.map +1 -0
- package/dist/core/identity/identity-manager.js +454 -0
- package/dist/core/identity/identity-manager.js.map +1 -0
- package/dist/core/identity/index.d.ts +8 -0
- package/dist/core/identity/index.d.ts.map +1 -0
- package/dist/core/identity/index.js +7 -0
- package/dist/core/identity/index.js.map +1 -0
- package/dist/core/identity/types.d.ts +70 -0
- package/dist/core/identity/types.d.ts.map +1 -0
- package/dist/core/identity/types.js +17 -0
- package/dist/core/identity/types.js.map +1 -0
- package/dist/core/p2p-network.d.ts +26 -0
- package/dist/core/p2p-network.d.ts.map +1 -1
- package/dist/core/p2p-network.js +434 -105
- package/dist/core/p2p-network.js.map +1 -1
- package/dist/core/reputation-security.d.ts +15 -0
- package/dist/core/reputation-security.d.ts.map +1 -1
- package/dist/core/reputation-security.js +73 -3
- package/dist/core/reputation-security.js.map +1 -1
- package/dist/core/reputation.d.ts +129 -4
- package/dist/core/reputation.d.ts.map +1 -1
- package/dist/core/reputation.js +294 -1
- package/dist/core/reputation.js.map +1 -1
- package/dist/core/review-committee.d.ts +2 -2
- package/dist/core/review-committee.d.ts.map +1 -1
- package/dist/core/review-committee.js +17 -0
- package/dist/core/review-committee.js.map +1 -1
- package/dist/daemon/control-server.d.ts.map +1 -1
- package/dist/daemon/control-server.js +44 -1
- package/dist/daemon/control-server.js.map +1 -1
- package/dist/daemon/webhook.d.ts +3 -0
- package/dist/daemon/webhook.d.ts.map +1 -1
- package/dist/daemon/webhook.js +318 -6
- package/dist/daemon/webhook.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -3
- package/dist/index.js.map +1 -1
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/result.d.ts +1 -1
- package/dist/types/result.d.ts.map +1 -1
- package/dist/types/result.js.map +1 -1
- package/dist/utils/crypto-utils.d.ts +17 -0
- package/dist/utils/crypto-utils.d.ts.map +1 -0
- package/dist/utils/crypto-utils.js +28 -0
- package/dist/utils/crypto-utils.js.map +1 -0
- package/dist/utils/logger.d.ts +1 -0
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +9 -3
- package/dist/utils/logger.js.map +1 -1
- package/dist/utils/rate-limiter.d.ts.map +1 -1
- package/dist/utils/rate-limiter.js +3 -1
- package/dist/utils/rate-limiter.js.map +1 -1
- package/dist/utils/signature.d.ts +47 -1
- package/dist/utils/signature.d.ts.map +1 -1
- package/dist/utils/signature.js +166 -11
- package/dist/utils/signature.js.map +1 -1
- package/package.json +9 -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
package/src/index.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
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';
|
package/src/types/index.test.ts
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
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
|
-
});
|
package/src/types/index.ts
DELETED
|
@@ -1,341 +0,0 @@
|
|
|
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';
|
package/src/types/result.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
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>>;
|