evolcore 0.0.41 → 0.0.42

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.
Files changed (190) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/bin/codex-managed-hook.mjs +9 -5
  3. package/dist/agents/baseagent.js +156 -16
  4. package/dist/agents/claude-runner.js +10 -3
  5. package/dist/agents/codex/catalog.js +8 -4
  6. package/dist/agents/codex-runner.js +236 -59
  7. package/dist/agents/ecagent-runner.js +82 -13
  8. package/dist/aun/aid/agentmd.js +106 -27
  9. package/dist/aun/aid/client.js +1 -1
  10. package/dist/aun/aid/control-aid.js +65 -13
  11. package/dist/aun/aid/identity.js +70 -4
  12. package/dist/aun/aid/index.js +2 -2
  13. package/dist/aun/service-proxy.js +25 -1
  14. package/dist/channels/aun.js +242 -209
  15. package/dist/channels/dingtalk.js +1 -1
  16. package/dist/channels/feishu.js +1 -1
  17. package/dist/channels/qqbot.js +1 -1
  18. package/dist/channels/wechat.js +1 -1
  19. package/dist/channels/wecom.js +1 -1
  20. package/dist/cli/agent-command.js +183 -3
  21. package/dist/cli/agent-create.js +92 -86
  22. package/dist/cli/agent.js +136 -1
  23. package/dist/cli/aun-commands.js +137 -35
  24. package/dist/cli/bench.js +8 -2
  25. package/dist/cli/command-log.js +1 -1
  26. package/dist/cli/daemon-commands.js +235 -51
  27. package/dist/cli/debug-mode.js +1 -1
  28. package/dist/cli/directory.js +170 -0
  29. package/dist/cli/index.js +40 -30
  30. package/dist/cli/init-channel.js +4 -4
  31. package/dist/cli/init.js +47 -32
  32. package/dist/cli/managed-command-guard.js +4 -1
  33. package/dist/cli/model.js +27 -18
  34. package/dist/cli/pa-workspace-command.js +191 -0
  35. package/dist/cli/{contact.js → peer.js} +19 -25
  36. package/dist/cli/restart-monitor.js +11 -4
  37. package/dist/cli/thread-command.js +47 -18
  38. package/dist/cli/version.js +18 -18
  39. package/dist/config/access-policy.js +29 -12
  40. package/dist/config/boot-log.js +65 -10
  41. package/dist/config/builtin-roles.js +2 -4
  42. package/dist/config/config-field-policy.js +3 -0
  43. package/dist/config/config-manager.js +28 -20
  44. package/dist/config/{contact-bind-code.js → peer-bind-code.js} +6 -6
  45. package/dist/config/{contact-book-store.js → peer-book-store.js} +75 -75
  46. package/dist/config/{contact-book-v2-startup.js → peer-book-v2-startup.js} +2 -2
  47. package/dist/config/{contact-book.js → peer-book.js} +47 -47
  48. package/dist/config/peer-operation-service.js +132 -0
  49. package/dist/config/{contact-request-service.js → peer-request-service.js} +90 -90
  50. package/dist/config/peer-role-resolver.js +3 -3
  51. package/dist/config/providers-config.js +17 -8
  52. package/dist/config/providers.js +1 -1
  53. package/dist/config/schema-migration.js +336 -39
  54. package/dist/core/agent-application-service.js +8 -2
  55. package/dist/core/agent-create-status.js +4 -5
  56. package/dist/core/agent-ipc-registration.js +5 -4
  57. package/dist/core/agent-runtime.js +30 -17
  58. package/dist/core/auth/operation-authorizer.js +0 -24
  59. package/dist/core/auth/operation-catalog.js +4 -20
  60. package/dist/core/baseagent-loader.js +27 -3
  61. package/dist/core/bootstrap-service.js +5 -22
  62. package/dist/core/capability/capability-manager.js +52 -35
  63. package/dist/core/channel-loader.js +43 -2
  64. package/dist/core/channel-runtime.js +2 -2
  65. package/dist/core/command/agent-control.js +54 -18
  66. package/dist/core/command/command-handler.js +92 -72
  67. package/dist/core/command/connect-menu.js +35 -42
  68. package/dist/core/command/daemon-diagnostics.js +41 -5
  69. package/dist/core/command/data-root-error.js +10 -0
  70. package/dist/core/command/data-root-resolver.js +53 -13
  71. package/dist/core/command/menu-catalog.js +1 -0
  72. package/dist/core/command/menu-handler.js +163 -65
  73. package/dist/core/command/menu-protocol.js +52 -0
  74. package/dist/core/command/pa-workspace-calendar.js +223 -0
  75. package/dist/core/command/pa-workspace-store.js +459 -0
  76. package/dist/core/command/role-menu.js +2 -2
  77. package/dist/core/command/slash-gate.js +1 -1
  78. package/dist/core/command/slash-handler.js +57 -41
  79. package/dist/core/daemon-lifecycle.js +90 -31
  80. package/dist/core/evolagent.js +6 -10
  81. package/dist/core/handoff/runtime.js +11 -4
  82. package/dist/core/host-snapshot.js +208 -0
  83. package/dist/core/initialization/service.js +152 -20
  84. package/dist/core/ipc-registration.js +33 -4
  85. package/dist/core/memory/batch-meta.js +27 -0
  86. package/dist/core/memory/consolidation-ops-records.js +44 -1
  87. package/dist/core/memory/consolidation.js +169 -27
  88. package/dist/core/message/aun-ipc-sender.js +2 -8
  89. package/dist/core/message/im-renderer.js +56 -3
  90. package/dist/core/message/message-admission.js +121 -124
  91. package/dist/core/message/message-bridge.js +38 -54
  92. package/dist/core/message/message-queue.js +7 -0
  93. package/dist/core/message/outbound.js +34 -9
  94. package/dist/core/message/response-engine.js +154 -73
  95. package/dist/core/model/catalog-service.js +1 -1
  96. package/dist/core/model/config-scope.js +2 -2
  97. package/dist/core/model/model-catalog.js +5 -4
  98. package/dist/core/model/model-diagnostics.js +6 -5
  99. package/dist/core/model/model-fallback.js +3 -6
  100. package/dist/core/model/model-permission.js +1 -1
  101. package/dist/core/ownership/owner-resolver.js +9 -9
  102. package/dist/core/permission/approval-gateway.js +1 -1
  103. package/dist/core/permission/ec-command-parser.js +2 -2
  104. package/dist/core/permission/tool-canonical.js +96 -46
  105. package/dist/core/permission/tool-error-code.js +41 -0
  106. package/dist/core/permission/tool-policy.js +71 -47
  107. package/dist/core/role/runtime-policy.js +1 -1
  108. package/dist/core/session/session-idle-monitor.js +10 -0
  109. package/dist/core/session/session-manager.js +2 -4
  110. package/dist/core/session/session-title.js +36 -8
  111. package/dist/core/system-session/memory.js +2 -7
  112. package/dist/core/thread/command-parser.js +6 -2
  113. package/dist/core/thread/ipc-operation.js +51 -36
  114. package/dist/core/thread/thread-operation.js +29 -61
  115. package/dist/core/thread/thread-service.js +57 -13
  116. package/dist/directory/canonical-digest.js +54 -0
  117. package/dist/directory/owner-directory-admission.js +114 -43
  118. package/dist/directory/owner-directory-agentmd-sync.js +89 -0
  119. package/dist/directory/owner-directory-cache.js +847 -459
  120. package/dist/directory/owner-directory-markdown.js +272 -0
  121. package/dist/directory/owner-directory-query-operation.js +195 -0
  122. package/dist/directory/owner-directory-remark-operation.js +147 -0
  123. package/dist/directory/owner-directory-remarks.js +145 -0
  124. package/dist/directory/owner-directory-scope.js +55 -0
  125. package/dist/directory/owner-directory-search.js +148 -0
  126. package/dist/directory/owner-directory-store.js +227 -0
  127. package/dist/eck/kit-renderer.js +1 -0
  128. package/dist/ecweb/basic-settings-service.js +4 -1
  129. package/dist/ecweb/message-context-service.js +13 -9
  130. package/dist/index.js +195 -37
  131. package/dist/ipc.js +86 -22
  132. package/dist/paths.js +11 -8
  133. package/dist/product.js +2 -0
  134. package/dist/proxy/manager.js +13 -2
  135. package/dist/proxy/service-supervisor.js +8 -1
  136. package/dist/trigger/history.js +23 -0
  137. package/dist/trigger/scheduler.js +3 -0
  138. package/dist/utils/atomic-write.js +21 -0
  139. package/dist/utils/cross-platform.js +35 -0
  140. package/dist/utils/ecweb-supervisor.js +42 -0
  141. package/dist/utils/error-utils.js +2 -1
  142. package/dist/utils/evolcore-version.js +19 -0
  143. package/dist/utils/host-info.js +242 -5
  144. package/dist/utils/instance-registry.js +28 -6
  145. package/dist/utils/logger.js +59 -14
  146. package/dist/utils/machine-info.js +46 -0
  147. package/dist/utils/process-introspect.js +166 -2
  148. package/dist/utils/startup-trace.js +245 -0
  149. package/ecagent/dist/agent-loop.js +27 -13
  150. package/ecagent/dist/harness/agent-harness.js +3 -3
  151. package/ecagent/dist/harness/types.d.ts +1 -0
  152. package/ecagent/dist/types.d.ts +1 -0
  153. package/kits/docs/INDEX.md +1 -1
  154. 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
  155. package/kits/docs/evolcore/INDEX.md +3 -2
  156. package/kits/docs/evolcore/agent-create.md +17 -27
  157. package/kits/docs/evolcore/agent.md +4 -1
  158. package/kits/docs/evolcore/aid.md +3 -1
  159. package/kits/docs/evolcore/config.md +16 -5
  160. package/kits/docs/evolcore/ctl.md +9 -0
  161. package/kits/docs/evolcore/ec-capabilities.md +19 -0
  162. package/kits/docs/evolcore/memory-consolidation-batches.md +79 -5
  163. package/kits/docs/evolcore/menu-exec-schema-commands.md +2 -1
  164. package/kits/docs/evolcore/menu-protocol.md +1 -0
  165. package/kits/docs/evolcore/model.md +13 -4
  166. package/kits/docs/evolcore/msg.md +1 -1
  167. package/kits/docs/evolcore/pa-workspace.md +51 -0
  168. package/kits/docs/evolcore/peer.md +73 -0
  169. package/kits/docs/evolcore/thread-chain.md +4 -1
  170. package/kits/eck_manifest.json +21 -0
  171. package/kits/rules/06-channel.md +1 -1
  172. package/kits/schemas/_meta.json +12 -1
  173. package/kits/schemas/agent-config.schema.16.json +6 -0
  174. package/kits/schemas/agent-config.schema.17.json +897 -0
  175. package/kits/schemas/daemon.schema.9.json +8 -1
  176. package/kits/skills/ec-agent-create/SKILL.md +2 -2
  177. package/kits/skills/ec-identity/SKILL.md +45 -27
  178. package/kits/skills/ec-service-proxy/SKILL.md +7 -2
  179. package/kits/skills/personal-assistant/references/03-data-flow-and-gaps.md +4 -5
  180. package/kits/skills/thread-ops/SKILL.md +11 -11
  181. package/kits/templates/bootstrap-welcome.md +3 -15
  182. package/kits/templates/message-fragments/item.md +1 -1
  183. package/kits/templates/system-fragments/bootstrap.md +24 -23
  184. package/kits/templates/system-fragments/commands.md +12 -5
  185. package/kits/templates/system-fragments/thread.md +18 -3
  186. package/package.json +6 -4
  187. package/dist/config/contact-operation-service.js +0 -235
  188. package/dist/directory/owner-directory-authorization.js +0 -309
  189. package/kits/docs/evolcore/contact.md +0 -78
  190. /package/dist/config/{contact-alias.js → peer-alias.js} +0 -0
@@ -1,16 +1,16 @@
1
- import { agentContactConfig } from '../paths.js';
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 { ContactMutationError, contactRevision, resolveContactAidKind, mutateContactBookSync } from './contact-book-store.js';
5
- import { formatContactAlias, normalizeContactChannelKey, parseContactAlias, } from './contact-alias.js';
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 ContactBookError extends Error {
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 = 'ContactBookError';
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 agentContactConfigPath(aid) {
23
- return agentContactConfig(aid);
22
+ export function agentPeerConfigPath(aid) {
23
+ return agentPeerConfig(aid);
24
24
  }
25
- export function readContactBook(selfAid, opts = {}) {
25
+ export function readPeerBook(selfAid, opts = {}) {
26
26
  if (!selfAid)
27
27
  return null;
28
- return read(ConfigTarget.Contact, { self: selfAid }, { cache: opts.cache ?? true });
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 bindContactAlias(selfAid, primaryId, channelType, actorId, channelKey) {
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 ContactBookError('NATIVE_AID_REMAP_FORBIDDEN', `Native AID "${actor}" cannot be remapped to "${primary}"`);
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 ContactBookError('INVALID_ALIAS', `Invalid contact alias: ${String(channelKey)}:${String(actorId)}`);
72
+ throw new PeerBookError('INVALID_ALIAS', `Invalid contact alias: ${String(channelKey)}:${String(actorId)}`);
73
73
  }
74
- if (!getContactSnapshot(selfAid).contacts.has(primary)) {
75
- throw new ContactBookError('NOT_FOUND', `Contact was not found: ${primary}`);
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 mutateContactBookSync({
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 ContactMutationError)
86
- throw new ContactBookError(error.code, error.message);
85
+ if (error instanceof PeerMutationError)
86
+ throw new PeerBookError(error.code, error.message);
87
87
  throw error;
88
88
  }
89
89
  }
90
- export function unbindContactAlias(selfAid, primaryId, channelType, actorId, channelKey) {
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 ContactBookError('INVALID_ALIAS', `Invalid contact alias: ${String(channelKey)}:${String(actorId)}`);
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 mutateContactBookSync({
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 ContactMutationError)
107
- throw new ContactBookError(error.code, error.message);
106
+ if (error instanceof PeerMutationError)
107
+ throw new PeerBookError(error.code, error.message);
108
108
  throw error;
109
109
  }
110
110
  }
111
- export function clearContactBookCache() {
111
+ export function clearPeerBookCache() {
112
112
  aliasMapCache = new WeakMap();
113
113
  snapshotByBook = new WeakMap();
114
114
  }
115
- export function getContactSnapshot(selfAid) {
116
- const book = readContactBook(selfAid, { cache: true }) ?? { $schema_version: 2, contacts: {} };
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.Contact, book);
120
+ const errors = validateConfig(ConfigTarget.Peer, book);
121
121
  if (errors.length) {
122
- throw new ContactBookError('INVALID_CONTACT_BOOK', `Invalid contact book for ${selfAid}: ${errors.join('; ')}`);
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 ContactBookError('ALIAS_CONFLICT', `Conflicting contact aliases: ${[...aliases.conflictingAliases].join(', ')}`);
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 resolveContactView(selfAid, primaryId) {
132
+ export function resolvePeerView(selfAid, primaryId) {
133
133
  const id = String(primaryId || '').trim();
134
134
  const owner = listAgentOwners(selfAid).includes(id);
135
- const entry = getContactSnapshot(selfAid).contacts.get(id);
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 ? 'contact' : 'none',
146
+ source: owner ? 'owner' : entry ? 'peer' : 'none',
147
147
  };
148
148
  }
149
- export function isBlockedContact(selfAid, primaryId) {
150
- return resolveContactView(selfAid, primaryId).blocked;
149
+ export function isBlockedPeer(selfAid, primaryId) {
150
+ return resolvePeerView(selfAid, primaryId).blocked;
151
151
  }
152
- export function isAcceptedContact(selfAid, primaryId, routeKind) {
152
+ export function isAcceptedPeer(selfAid, primaryId, routeKind) {
153
153
  const id = String(primaryId || '').trim();
154
- const snapshot = getContactSnapshot(selfAid);
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 resolveContactView(selfAid, principalId).displayName || principalId;
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 = resolveContactAidKind(selfAid, primaryId);
187
+ const kind = resolvePeerAidKind(selfAid, primaryId);
188
188
  if (!isValidAid(primaryId) || kind.kind === 'group' || (kind.kind === 'unknown' && kind.reason !== 'missing')) {
189
- throw new ContactBookError('INVALID_CONTACT_BOOK', `Invalid contact AID: ${primaryId}`);
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 ContactBookError('INVALID_CONTACT_BOOK', `Owner contact ${primaryId} cannot be blocked`);
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
- contactRevision: contactRevision(book),
207
+ peerRevision: peerRevision(book),
208
208
  });
209
209
  }
210
210
  function loadAliasMap(selfAid) {
211
- const book = readContactBook(selfAid, { cache: true });
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.Contact, book);
217
+ const errors = validateConfig(ConfigTarget.Peer, book);
218
218
  if (errors.length) {
219
- throw new ContactBookError('INVALID_CONTACT_BOOK', `Invalid contact book for ${selfAid}: ${errors.join('; ')}`);
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 = resolveContactAidKind(selfAid, primaryId);
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 ContactBookError('INVALID_CONTACT_BOOK', `Invalid contact alias for ${selfAid}: ${String(rawAlias)}`);
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 = resolveContactAidKind(selfAid, id);
255
+ const type = resolvePeerAidKind(selfAid, id);
256
256
  if (!isValidAid(id) || (type.kind !== 'private' && !(allowMissing && type.reason === 'missing'))) {
257
- throw new ContactBookError('INVALID_PRIMARY_ID', `Invalid individual contact AID: ${String(value)}`);
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
+ }