evolcore 0.0.41 → 0.0.42-test.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/CHANGELOG.md +36 -0
- package/bin/codex-managed-hook.mjs +9 -5
- package/dist/agents/baseagent.js +156 -16
- package/dist/agents/claude-runner.js +10 -3
- package/dist/agents/codex/catalog.js +8 -4
- package/dist/agents/codex-runner.js +236 -59
- package/dist/agents/ecagent-runner.js +82 -13
- package/dist/aun/aid/agentmd.js +106 -27
- package/dist/aun/aid/client.js +1 -1
- package/dist/aun/aid/control-aid.js +65 -13
- package/dist/aun/aid/identity.js +70 -4
- package/dist/aun/aid/index.js +2 -2
- package/dist/aun/service-proxy.js +25 -1
- package/dist/channels/aun.js +242 -209
- package/dist/channels/dingtalk.js +1 -1
- package/dist/channels/feishu.js +1 -1
- package/dist/channels/qqbot.js +1 -1
- package/dist/channels/wechat.js +1 -1
- package/dist/channels/wecom.js +1 -1
- package/dist/cli/agent-command.js +183 -3
- package/dist/cli/agent-create.js +92 -86
- package/dist/cli/agent.js +136 -1
- package/dist/cli/aun-commands.js +137 -35
- package/dist/cli/bench.js +8 -2
- package/dist/cli/command-log.js +1 -1
- package/dist/cli/daemon-commands.js +235 -51
- package/dist/cli/debug-mode.js +1 -1
- package/dist/cli/directory.js +170 -0
- package/dist/cli/index.js +40 -30
- package/dist/cli/init-channel.js +4 -4
- package/dist/cli/init.js +47 -32
- package/dist/cli/managed-command-guard.js +4 -1
- package/dist/cli/model.js +27 -18
- package/dist/cli/pa-workspace-command.js +191 -0
- package/dist/cli/{contact.js → peer.js} +19 -25
- package/dist/cli/restart-monitor.js +11 -4
- package/dist/cli/thread-command.js +47 -18
- package/dist/cli/version.js +18 -18
- package/dist/config/access-policy.js +29 -12
- package/dist/config/boot-log.js +65 -10
- package/dist/config/builtin-roles.js +2 -4
- package/dist/config/config-field-policy.js +3 -0
- package/dist/config/config-manager.js +28 -20
- package/dist/config/{contact-bind-code.js → peer-bind-code.js} +6 -6
- package/dist/config/{contact-book-store.js → peer-book-store.js} +75 -75
- package/dist/config/{contact-book-v2-startup.js → peer-book-v2-startup.js} +2 -2
- package/dist/config/{contact-book.js → peer-book.js} +47 -47
- package/dist/config/peer-operation-service.js +132 -0
- package/dist/config/{contact-request-service.js → peer-request-service.js} +90 -90
- package/dist/config/peer-role-resolver.js +3 -3
- package/dist/config/providers-config.js +17 -8
- package/dist/config/providers.js +1 -1
- package/dist/config/schema-migration.js +336 -39
- package/dist/core/agent-application-service.js +8 -2
- package/dist/core/agent-create-status.js +4 -5
- package/dist/core/agent-ipc-registration.js +5 -4
- package/dist/core/agent-runtime.js +30 -17
- package/dist/core/auth/operation-authorizer.js +0 -24
- package/dist/core/auth/operation-catalog.js +4 -20
- package/dist/core/baseagent-loader.js +27 -3
- package/dist/core/bootstrap-service.js +5 -22
- package/dist/core/capability/capability-manager.js +52 -35
- package/dist/core/channel-loader.js +43 -2
- package/dist/core/channel-runtime.js +2 -2
- package/dist/core/command/agent-control.js +54 -18
- package/dist/core/command/command-handler.js +92 -72
- package/dist/core/command/connect-menu.js +35 -42
- package/dist/core/command/daemon-diagnostics.js +41 -5
- package/dist/core/command/data-root-error.js +10 -0
- package/dist/core/command/data-root-resolver.js +53 -13
- package/dist/core/command/menu-catalog.js +1 -0
- package/dist/core/command/menu-handler.js +163 -65
- package/dist/core/command/menu-protocol.js +52 -0
- package/dist/core/command/pa-workspace-calendar.js +223 -0
- package/dist/core/command/pa-workspace-store.js +459 -0
- package/dist/core/command/role-menu.js +2 -2
- package/dist/core/command/slash-gate.js +1 -1
- package/dist/core/command/slash-handler.js +57 -41
- package/dist/core/daemon-lifecycle.js +90 -31
- package/dist/core/evolagent.js +6 -10
- package/dist/core/handoff/runtime.js +11 -4
- package/dist/core/host-snapshot.js +208 -0
- package/dist/core/initialization/service.js +152 -20
- package/dist/core/ipc-registration.js +33 -4
- package/dist/core/memory/batch-meta.js +27 -0
- package/dist/core/memory/consolidation-ops-records.js +44 -1
- package/dist/core/memory/consolidation.js +169 -27
- package/dist/core/message/aun-ipc-sender.js +2 -8
- package/dist/core/message/im-renderer.js +56 -3
- package/dist/core/message/message-admission.js +121 -124
- package/dist/core/message/message-bridge.js +38 -54
- package/dist/core/message/message-queue.js +7 -0
- package/dist/core/message/outbound.js +34 -9
- package/dist/core/message/response-engine.js +154 -73
- package/dist/core/model/catalog-service.js +1 -1
- package/dist/core/model/config-scope.js +2 -2
- package/dist/core/model/model-catalog.js +5 -4
- package/dist/core/model/model-diagnostics.js +6 -5
- package/dist/core/model/model-fallback.js +3 -6
- package/dist/core/model/model-permission.js +1 -1
- package/dist/core/ownership/owner-resolver.js +9 -9
- package/dist/core/permission/approval-gateway.js +1 -1
- package/dist/core/permission/ec-command-parser.js +2 -2
- package/dist/core/permission/tool-canonical.js +96 -46
- package/dist/core/permission/tool-error-code.js +41 -0
- package/dist/core/permission/tool-policy.js +71 -47
- package/dist/core/role/runtime-policy.js +1 -1
- package/dist/core/session/session-idle-monitor.js +10 -0
- package/dist/core/session/session-manager.js +2 -4
- package/dist/core/session/session-title.js +36 -8
- package/dist/core/system-session/memory.js +2 -7
- package/dist/core/thread/command-parser.js +6 -2
- package/dist/core/thread/ipc-operation.js +51 -36
- package/dist/core/thread/thread-operation.js +29 -61
- package/dist/core/thread/thread-service.js +57 -13
- package/dist/directory/canonical-digest.js +54 -0
- package/dist/directory/owner-directory-admission.js +114 -43
- package/dist/directory/owner-directory-agentmd-sync.js +89 -0
- package/dist/directory/owner-directory-cache.js +847 -459
- package/dist/directory/owner-directory-markdown.js +272 -0
- package/dist/directory/owner-directory-query-operation.js +195 -0
- package/dist/directory/owner-directory-remark-operation.js +147 -0
- package/dist/directory/owner-directory-remarks.js +145 -0
- package/dist/directory/owner-directory-scope.js +55 -0
- package/dist/directory/owner-directory-search.js +148 -0
- package/dist/directory/owner-directory-store.js +227 -0
- package/dist/eck/kit-renderer.js +1 -0
- package/dist/ecweb/basic-settings-service.js +4 -1
- package/dist/ecweb/message-context-service.js +13 -9
- package/dist/index.js +195 -37
- package/dist/ipc.js +86 -22
- package/dist/paths.js +11 -8
- package/dist/product.js +2 -0
- package/dist/proxy/manager.js +13 -2
- package/dist/proxy/service-supervisor.js +8 -1
- package/dist/trigger/history.js +23 -0
- package/dist/trigger/scheduler.js +3 -0
- package/dist/utils/atomic-write.js +21 -0
- package/dist/utils/cross-platform.js +35 -0
- package/dist/utils/ecweb-supervisor.js +42 -0
- package/dist/utils/error-utils.js +2 -1
- package/dist/utils/evolcore-version.js +19 -0
- package/dist/utils/host-info.js +242 -5
- package/dist/utils/instance-registry.js +28 -6
- package/dist/utils/logger.js +59 -14
- package/dist/utils/machine-info.js +46 -0
- package/dist/utils/process-introspect.js +166 -2
- package/dist/utils/startup-trace.js +245 -0
- package/ecagent/dist/agent-loop.js +27 -13
- package/ecagent/dist/harness/agent-harness.js +3 -3
- package/ecagent/dist/harness/types.d.ts +1 -0
- package/ecagent/dist/types.d.ts +1 -0
- package/kits/docs/INDEX.md +1 -1
- package/kits/docs/aun/AUN/346/266/210/346/201/257/347/261/273/345/236/213/344/270/216/345/261/236/346/200/247.md +72 -0
- package/kits/docs/evolcore/INDEX.md +3 -2
- package/kits/docs/evolcore/agent-create.md +17 -27
- package/kits/docs/evolcore/agent.md +4 -1
- package/kits/docs/evolcore/aid.md +3 -1
- package/kits/docs/evolcore/config.md +16 -5
- package/kits/docs/evolcore/ctl.md +9 -0
- package/kits/docs/evolcore/ec-capabilities.md +19 -0
- package/kits/docs/evolcore/memory-consolidation-batches.md +79 -5
- package/kits/docs/evolcore/menu-exec-schema-commands.md +2 -1
- package/kits/docs/evolcore/menu-protocol.md +1 -0
- package/kits/docs/evolcore/model.md +13 -4
- package/kits/docs/evolcore/msg.md +1 -1
- package/kits/docs/evolcore/pa-workspace.md +51 -0
- package/kits/docs/evolcore/peer.md +73 -0
- package/kits/docs/evolcore/thread-chain.md +4 -1
- package/kits/eck_manifest.json +21 -0
- package/kits/rules/06-channel.md +1 -1
- package/kits/schemas/_meta.json +12 -1
- package/kits/schemas/agent-config.schema.16.json +6 -0
- package/kits/schemas/agent-config.schema.17.json +897 -0
- package/kits/schemas/daemon.schema.9.json +8 -1
- package/kits/skills/ec-agent-create/SKILL.md +2 -2
- package/kits/skills/ec-identity/SKILL.md +45 -27
- package/kits/skills/ec-service-proxy/SKILL.md +7 -2
- package/kits/skills/personal-assistant/references/03-data-flow-and-gaps.md +4 -5
- package/kits/skills/thread-ops/SKILL.md +11 -11
- package/kits/templates/bootstrap-welcome.md +3 -15
- package/kits/templates/message-fragments/item.md +1 -1
- package/kits/templates/system-fragments/bootstrap.md +24 -23
- package/kits/templates/system-fragments/commands.md +12 -5
- package/kits/templates/system-fragments/thread.md +18 -3
- package/package.json +6 -4
- package/dist/config/contact-operation-service.js +0 -235
- package/dist/directory/owner-directory-authorization.js +0 -309
- package/kits/docs/evolcore/contact.md +0 -78
- /package/dist/config/{contact-alias.js → peer-alias.js} +0 -0
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { agentPeerConfig } from '../paths.js';
|
|
2
2
|
import { isValidAid } from '../aun/aid/validation.js';
|
|
3
3
|
import { ConfigTarget, read, validateConfig } from './config-manager.js';
|
|
4
|
-
import {
|
|
5
|
-
import { formatContactAlias, normalizeContactChannelKey, parseContactAlias, } from './
|
|
4
|
+
import { PeerMutationError, peerRevision, resolvePeerAidKind, mutatePeerBookSync } from './peer-book-store.js';
|
|
5
|
+
import { formatContactAlias, normalizeContactChannelKey, parseContactAlias, } from './peer-alias.js';
|
|
6
6
|
import { getOwners } from '../core/ownership/owner-resolver.js';
|
|
7
7
|
import { readAunPeerKind } from '../core/relation/peer-identity.js';
|
|
8
|
-
export class
|
|
8
|
+
export class PeerBookError extends Error {
|
|
9
9
|
code;
|
|
10
10
|
constructor(code, message) {
|
|
11
11
|
super(message);
|
|
12
12
|
this.code = code;
|
|
13
|
-
this.name = '
|
|
13
|
+
this.name = 'PeerBookError';
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
let aliasMapCache = new WeakMap();
|
|
@@ -19,13 +19,13 @@ const EMPTY_ALIAS_MAP = {
|
|
|
19
19
|
aliasToPrimary: new Map(),
|
|
20
20
|
conflictingAliases: new Set(),
|
|
21
21
|
};
|
|
22
|
-
export function
|
|
23
|
-
return
|
|
22
|
+
export function agentPeerConfigPath(aid) {
|
|
23
|
+
return agentPeerConfig(aid);
|
|
24
24
|
}
|
|
25
|
-
export function
|
|
25
|
+
export function readPeerBook(selfAid, opts = {}) {
|
|
26
26
|
if (!selfAid)
|
|
27
27
|
return null;
|
|
28
|
-
return read(ConfigTarget.
|
|
28
|
+
return read(ConfigTarget.Peer, { self: selfAid }, { cache: opts.cache ?? true });
|
|
29
29
|
}
|
|
30
30
|
export function resolvePrincipal(selfAid, channelType, actorId, channelKey) {
|
|
31
31
|
const actor = String(actorId || '').trim();
|
|
@@ -57,82 +57,82 @@ export function resolvePrimaryId(selfAid, channelType, actorId, channelKey) {
|
|
|
57
57
|
const actor = String(actorId || '').trim();
|
|
58
58
|
return resolvePrincipal(selfAid, channelType, actor, channelKey).principalId ?? actor;
|
|
59
59
|
}
|
|
60
|
-
export function
|
|
60
|
+
export function bindPeerAlias(selfAid, primaryId, channelType, actorId, channelKey) {
|
|
61
61
|
const primary = normalizePrimaryId(primaryId, selfAid);
|
|
62
62
|
const type = String(channelType || '').trim().toLowerCase();
|
|
63
63
|
const actor = String(actorId || '').trim();
|
|
64
64
|
if (type === 'aun') {
|
|
65
65
|
if (!isValidAid(actor) || actor !== primary) {
|
|
66
|
-
throw new
|
|
66
|
+
throw new PeerBookError('NATIVE_AID_REMAP_FORBIDDEN', `Native AID "${actor}" cannot be remapped to "${primary}"`);
|
|
67
67
|
}
|
|
68
68
|
return false;
|
|
69
69
|
}
|
|
70
70
|
const canonicalChannelKey = normalizeContactChannelKey(selfAid, type, channelKey);
|
|
71
71
|
if (!canonicalChannelKey || !actor) {
|
|
72
|
-
throw new
|
|
72
|
+
throw new PeerBookError('INVALID_ALIAS', `Invalid contact alias: ${String(channelKey)}:${String(actorId)}`);
|
|
73
73
|
}
|
|
74
|
-
if (!
|
|
75
|
-
throw new
|
|
74
|
+
if (!getPeerSnapshot(selfAid).contacts.has(primary)) {
|
|
75
|
+
throw new PeerBookError('NOT_FOUND', `Contact was not found: ${primary}`);
|
|
76
76
|
}
|
|
77
77
|
const alias = formatContactAlias(canonicalChannelKey, actor);
|
|
78
78
|
try {
|
|
79
|
-
return
|
|
79
|
+
return mutatePeerBookSync({
|
|
80
80
|
selfAid,
|
|
81
81
|
mutation: { type: 'bind-alias', primaryId: primary, alias },
|
|
82
82
|
}).changed;
|
|
83
83
|
}
|
|
84
84
|
catch (error) {
|
|
85
|
-
if (error instanceof
|
|
86
|
-
throw new
|
|
85
|
+
if (error instanceof PeerMutationError)
|
|
86
|
+
throw new PeerBookError(error.code, error.message);
|
|
87
87
|
throw error;
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
-
export function
|
|
90
|
+
export function unbindPeerAlias(selfAid, primaryId, channelType, actorId, channelKey) {
|
|
91
91
|
const primary = normalizePrimaryId(primaryId, selfAid, true);
|
|
92
92
|
const type = String(channelType || '').trim().toLowerCase();
|
|
93
93
|
const actor = String(actorId || '').trim();
|
|
94
94
|
const canonicalChannelKey = normalizeContactChannelKey(selfAid, type, channelKey);
|
|
95
95
|
if (!canonicalChannelKey || type === 'aun' || !actor) {
|
|
96
|
-
throw new
|
|
96
|
+
throw new PeerBookError('INVALID_ALIAS', `Invalid contact alias: ${String(channelKey)}:${String(actorId)}`);
|
|
97
97
|
}
|
|
98
98
|
const alias = formatContactAlias(canonicalChannelKey, actor);
|
|
99
99
|
try {
|
|
100
|
-
return
|
|
100
|
+
return mutatePeerBookSync({
|
|
101
101
|
selfAid,
|
|
102
102
|
mutation: { type: 'unbind-alias', primaryId: primary, alias },
|
|
103
103
|
}).changed;
|
|
104
104
|
}
|
|
105
105
|
catch (error) {
|
|
106
|
-
if (error instanceof
|
|
107
|
-
throw new
|
|
106
|
+
if (error instanceof PeerMutationError)
|
|
107
|
+
throw new PeerBookError(error.code, error.message);
|
|
108
108
|
throw error;
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
|
-
export function
|
|
111
|
+
export function clearPeerBookCache() {
|
|
112
112
|
aliasMapCache = new WeakMap();
|
|
113
113
|
snapshotByBook = new WeakMap();
|
|
114
114
|
}
|
|
115
|
-
export function
|
|
116
|
-
const book =
|
|
115
|
+
export function getPeerSnapshot(selfAid) {
|
|
116
|
+
const book = readPeerBook(selfAid, { cache: true }) ?? { $schema_version: 2, contacts: {} };
|
|
117
117
|
const cached = snapshotByBook.get(book);
|
|
118
118
|
if (cached)
|
|
119
119
|
return cached;
|
|
120
|
-
const errors = validateConfig(ConfigTarget.
|
|
120
|
+
const errors = validateConfig(ConfigTarget.Peer, book);
|
|
121
121
|
if (errors.length) {
|
|
122
|
-
throw new
|
|
122
|
+
throw new PeerBookError('INVALID_CONTACT_BOOK', `Invalid contact book for ${selfAid}: ${errors.join('; ')}`);
|
|
123
123
|
}
|
|
124
124
|
const aliases = buildAliasMap(book, selfAid);
|
|
125
125
|
if (aliases.conflictingAliases.size) {
|
|
126
|
-
throw new
|
|
126
|
+
throw new PeerBookError('ALIAS_CONFLICT', `Conflicting contact aliases: ${[...aliases.conflictingAliases].join(', ')}`);
|
|
127
127
|
}
|
|
128
128
|
const snapshot = buildSnapshot(book, selfAid, aliases);
|
|
129
129
|
snapshotByBook.set(book, snapshot);
|
|
130
130
|
return snapshot;
|
|
131
131
|
}
|
|
132
|
-
export function
|
|
132
|
+
export function resolvePeerView(selfAid, primaryId) {
|
|
133
133
|
const id = String(primaryId || '').trim();
|
|
134
134
|
const owner = listAgentOwners(selfAid).includes(id);
|
|
135
|
-
const entry =
|
|
135
|
+
const entry = getPeerSnapshot(selfAid).contacts.get(id);
|
|
136
136
|
const status = owner ? 'active' : entry?.status ?? 'active';
|
|
137
137
|
const managementRole = owner ? 'owner' : entry?.managementRole === 'admin' ? 'admin' : undefined;
|
|
138
138
|
return {
|
|
@@ -143,15 +143,15 @@ export function resolveContactView(selfAid, primaryId) {
|
|
|
143
143
|
status,
|
|
144
144
|
blocked: status === 'blocked',
|
|
145
145
|
isOwner: owner,
|
|
146
|
-
source: owner ? 'owner' : entry ? '
|
|
146
|
+
source: owner ? 'owner' : entry ? 'peer' : 'none',
|
|
147
147
|
};
|
|
148
148
|
}
|
|
149
|
-
export function
|
|
150
|
-
return
|
|
149
|
+
export function isBlockedPeer(selfAid, primaryId) {
|
|
150
|
+
return resolvePeerView(selfAid, primaryId).blocked;
|
|
151
151
|
}
|
|
152
|
-
export function
|
|
152
|
+
export function isAcceptedPeer(selfAid, primaryId, routeKind) {
|
|
153
153
|
const id = String(primaryId || '').trim();
|
|
154
|
-
const snapshot =
|
|
154
|
+
const snapshot = getPeerSnapshot(selfAid);
|
|
155
155
|
const entry = snapshot.contacts.get(id);
|
|
156
156
|
return !!entry && (entry.status === undefined || entry.status === 'active')
|
|
157
157
|
&& readAunPeerKind(selfAid, id, { routeKind }).kind === 'private';
|
|
@@ -172,7 +172,7 @@ export function resolvePeerDisplayLabel(selfAid, channelType, actorId, channelKe
|
|
|
172
172
|
const { principalId } = resolvePrincipal(selfAid, channelType, actor, channelKey);
|
|
173
173
|
if (!principalId)
|
|
174
174
|
return undefined;
|
|
175
|
-
return
|
|
175
|
+
return resolvePeerView(selfAid, principalId).displayName || principalId;
|
|
176
176
|
}
|
|
177
177
|
catch {
|
|
178
178
|
// 联系人簿损坏(校验失败/alias 冲突)不应影响消息处理
|
|
@@ -184,12 +184,12 @@ function buildSnapshot(book, selfAid, aliases) {
|
|
|
184
184
|
const blockedIndex = new Set();
|
|
185
185
|
const owners = new Set(listAgentOwners(selfAid));
|
|
186
186
|
for (const [primaryId, rawEntry] of Object.entries(book.contacts)) {
|
|
187
|
-
const kind =
|
|
187
|
+
const kind = resolvePeerAidKind(selfAid, primaryId);
|
|
188
188
|
if (!isValidAid(primaryId) || kind.kind === 'group' || (kind.kind === 'unknown' && kind.reason !== 'missing')) {
|
|
189
|
-
throw new
|
|
189
|
+
throw new PeerBookError('INVALID_CONTACT_BOOK', `Invalid contact AID: ${primaryId}`);
|
|
190
190
|
}
|
|
191
191
|
if (rawEntry.status === 'blocked' && owners.has(primaryId)) {
|
|
192
|
-
throw new
|
|
192
|
+
throw new PeerBookError('INVALID_CONTACT_BOOK', `Owner contact ${primaryId} cannot be blocked`);
|
|
193
193
|
}
|
|
194
194
|
const entry = Object.freeze({
|
|
195
195
|
...rawEntry,
|
|
@@ -204,19 +204,19 @@ function buildSnapshot(book, selfAid, aliases) {
|
|
|
204
204
|
aliasIndex: aliases.aliasToPrimary,
|
|
205
205
|
conflictingAliases: aliases.conflictingAliases,
|
|
206
206
|
blockedIndex,
|
|
207
|
-
|
|
207
|
+
peerRevision: peerRevision(book),
|
|
208
208
|
});
|
|
209
209
|
}
|
|
210
210
|
function loadAliasMap(selfAid) {
|
|
211
|
-
const book =
|
|
211
|
+
const book = readPeerBook(selfAid, { cache: true });
|
|
212
212
|
if (!book)
|
|
213
213
|
return EMPTY_ALIAS_MAP;
|
|
214
214
|
const cached = aliasMapCache.get(book);
|
|
215
215
|
if (cached)
|
|
216
216
|
return cached;
|
|
217
|
-
const errors = validateConfig(ConfigTarget.
|
|
217
|
+
const errors = validateConfig(ConfigTarget.Peer, book);
|
|
218
218
|
if (errors.length) {
|
|
219
|
-
throw new
|
|
219
|
+
throw new PeerBookError('INVALID_CONTACT_BOOK', `Invalid contact book for ${selfAid}: ${errors.join('; ')}`);
|
|
220
220
|
}
|
|
221
221
|
const built = buildAliasMap(book, selfAid);
|
|
222
222
|
aliasMapCache.set(book, built);
|
|
@@ -226,14 +226,14 @@ function buildAliasMap(book, selfAid) {
|
|
|
226
226
|
const aliasToPrimary = new Map();
|
|
227
227
|
const conflictingAliases = new Set();
|
|
228
228
|
for (const [primaryId, entry] of Object.entries(book?.contacts ?? {})) {
|
|
229
|
-
const kind =
|
|
229
|
+
const kind = resolvePeerAidKind(selfAid, primaryId);
|
|
230
230
|
if (!isValidAid(primaryId) || kind.kind === 'group' || (kind.kind === 'unknown' && kind.reason !== 'missing')
|
|
231
231
|
|| !entry || typeof entry !== 'object')
|
|
232
232
|
continue;
|
|
233
233
|
for (const rawAlias of entry.aliases ?? []) {
|
|
234
234
|
const parsed = parseContactAlias(rawAlias, selfAid);
|
|
235
235
|
if (!parsed) {
|
|
236
|
-
throw new
|
|
236
|
+
throw new PeerBookError('INVALID_CONTACT_BOOK', `Invalid contact alias for ${selfAid}: ${String(rawAlias)}`);
|
|
237
237
|
}
|
|
238
238
|
const alias = parsed.alias;
|
|
239
239
|
if (conflictingAliases.has(alias))
|
|
@@ -252,9 +252,9 @@ function buildAliasMap(book, selfAid) {
|
|
|
252
252
|
}
|
|
253
253
|
function normalizePrimaryId(value, selfAid, allowMissing = false) {
|
|
254
254
|
const id = String(value || '').trim();
|
|
255
|
-
const type =
|
|
255
|
+
const type = resolvePeerAidKind(selfAid, id);
|
|
256
256
|
if (!isValidAid(id) || (type.kind !== 'private' && !(allowMissing && type.reason === 'missing'))) {
|
|
257
|
-
throw new
|
|
257
|
+
throw new PeerBookError('INVALID_PRIMARY_ID', `Invalid individual contact AID: ${String(value)}`);
|
|
258
258
|
}
|
|
259
259
|
return id;
|
|
260
260
|
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { isValidAid } from '../aun/aid/validation.js';
|
|
3
|
+
import { agentConfig } from '../paths.js';
|
|
4
|
+
import { getPeerSnapshot, resolvePeerView } from './peer-book.js';
|
|
5
|
+
import { PeerMutationError } from './peer-book-store.js';
|
|
6
|
+
import { mutatePeerWithOperation } from './peer-request-service.js';
|
|
7
|
+
/**
|
|
8
|
+
* `ec peer` 命令面。
|
|
9
|
+
*
|
|
10
|
+
* 只保留本 Agent 能自主决定的两件事:查看已有对端身份、拉黑/解除拉黑。
|
|
11
|
+
* 添加联系人、审批联系人申请都归 Owner——Owner 在其名录里直接维护,
|
|
12
|
+
* Agent 侧不再有自助入口,也不再持有 pending 状态机。
|
|
13
|
+
*/
|
|
14
|
+
export function resolvePeerCommand(argv) {
|
|
15
|
+
const args = argv[0] === 'peer' ? argv.slice(1) : [...argv];
|
|
16
|
+
const selfAid = String(argValue(args, '--self') || '').trim();
|
|
17
|
+
if (!isValidAid(selfAid))
|
|
18
|
+
return invalid('INVALID_SELF', '--self must be a valid Agent AID');
|
|
19
|
+
const positional = stripOptions(args);
|
|
20
|
+
const [command, subcommand, ...extra] = positional;
|
|
21
|
+
if (extra.length > 0)
|
|
22
|
+
return invalid('INVALID_ARGUMENT', `Unexpected peer arguments: ${extra.join(' ')}`);
|
|
23
|
+
if (command === 'list') {
|
|
24
|
+
if (subcommand !== undefined) {
|
|
25
|
+
return invalid('INVALID_ARGUMENT', 'peer list does not accept a positional argument');
|
|
26
|
+
}
|
|
27
|
+
const filter = args.includes('--blocked')
|
|
28
|
+
? 'blocked'
|
|
29
|
+
: args.includes('--all') ? 'all' : undefined;
|
|
30
|
+
if (!filter)
|
|
31
|
+
return invalid('INVALID_ARGUMENT', 'peer list requires --blocked or --all');
|
|
32
|
+
return {
|
|
33
|
+
ok: true,
|
|
34
|
+
command: { kind: 'list', operationId: 'peer.read', selfAid, filter, canonicalArgv: canonicalArgv(argv) },
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if (command === 'block' || command === 'unblock') {
|
|
38
|
+
if (!isValidAid(subcommand || '')) {
|
|
39
|
+
return invalid('INVALID_AID', 'primaryId must be a valid individual AID');
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
ok: true,
|
|
43
|
+
command: {
|
|
44
|
+
kind: 'set-blocked',
|
|
45
|
+
operationId: 'peer.block',
|
|
46
|
+
selfAid,
|
|
47
|
+
primaryId: subcommand,
|
|
48
|
+
blocked: command === 'block',
|
|
49
|
+
canonicalArgv: canonicalArgv(argv),
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
return invalid('UNKNOWN_COMMAND', `Unknown peer command: ${positional.join(' ')}`);
|
|
54
|
+
}
|
|
55
|
+
export async function executeResolvedPeerCommand(command, actor = 'local-cli') {
|
|
56
|
+
if (!fs.existsSync(agentConfig(command.selfAid))) {
|
|
57
|
+
return { ok: false, code: 'AGENT_NOT_FOUND', error: `Agent does not exist: ${command.selfAid}` };
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
if (command.kind === 'list') {
|
|
61
|
+
const snapshot = getPeerSnapshot(command.selfAid);
|
|
62
|
+
const ids = command.filter === 'blocked'
|
|
63
|
+
? [...snapshot.blockedIndex]
|
|
64
|
+
: [...snapshot.contacts.keys()];
|
|
65
|
+
const items = ids.sort().map(id => resolvePeerView(command.selfAid, id));
|
|
66
|
+
return {
|
|
67
|
+
ok: true,
|
|
68
|
+
operation: 'list',
|
|
69
|
+
selfAid: command.selfAid,
|
|
70
|
+
filter: command.filter,
|
|
71
|
+
peerRevision: snapshot.peerRevision,
|
|
72
|
+
items,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (command.kind === 'set-blocked') {
|
|
76
|
+
if (!getPeerSnapshot(command.selfAid).contacts.has(command.primaryId)) {
|
|
77
|
+
return { ok: false, code: 'NOT_FOUND', error: `Peer was not found: ${command.primaryId}` };
|
|
78
|
+
}
|
|
79
|
+
const result = await mutatePeerWithOperation({
|
|
80
|
+
selfAid: command.selfAid,
|
|
81
|
+
actorId: actor,
|
|
82
|
+
operation: command.blocked ? 'block' : 'unblock',
|
|
83
|
+
primaryId: command.primaryId,
|
|
84
|
+
mutation: {
|
|
85
|
+
type: 'set-status',
|
|
86
|
+
primaryId: command.primaryId,
|
|
87
|
+
status: command.blocked ? 'blocked' : 'active',
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
return {
|
|
91
|
+
ok: true,
|
|
92
|
+
operation: 'set-blocked',
|
|
93
|
+
selfAid: command.selfAid,
|
|
94
|
+
primaryId: command.primaryId,
|
|
95
|
+
blocked: command.blocked,
|
|
96
|
+
changed: result.changed,
|
|
97
|
+
peerRevision: result.peerRevision,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
return { ok: false, code: 'PEER_OPERATION_FAILED', error: 'Unsupported peer operation' };
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
if (error instanceof PeerMutationError) {
|
|
104
|
+
return { ok: false, code: error.code, error: error.message, ...(error.data ? { data: error.data } : {}) };
|
|
105
|
+
}
|
|
106
|
+
return { ok: false, code: 'PEER_OPERATION_FAILED', error: error instanceof Error ? error.message : String(error) };
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function argValue(args, flag) {
|
|
110
|
+
const index = args.indexOf(flag);
|
|
111
|
+
return index >= 0 ? args[index + 1] : undefined;
|
|
112
|
+
}
|
|
113
|
+
function stripOptions(args) {
|
|
114
|
+
const result = [];
|
|
115
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
116
|
+
const arg = args[index];
|
|
117
|
+
if (arg === '--self' || arg === '--format') {
|
|
118
|
+
index += 1;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (arg === '--blocked' || arg === '--all')
|
|
122
|
+
continue;
|
|
123
|
+
result.push(arg);
|
|
124
|
+
}
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
function canonicalArgv(argv) {
|
|
128
|
+
return argv[0] === 'peer' ? [...argv] : ['peer', ...argv];
|
|
129
|
+
}
|
|
130
|
+
function invalid(code, reason) {
|
|
131
|
+
return { ok: false, code, reason };
|
|
132
|
+
}
|