evolcore 0.0.43-test.1 → 0.0.43-test.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -1
- package/README.md +22 -0
- package/dist/agents/claude-runner.js +26 -2
- package/dist/agents/codex/app-server-client.js +12 -0
- package/dist/agents/codex-runner.js +49 -8
- package/dist/agents/ecagent-runner.js +39 -15
- package/dist/agents/gemini-runner.js +15 -12
- package/dist/agents/tool-time-context.js +55 -0
- package/dist/aun/aid/agentmd.js +21 -1
- package/dist/aun/menu/client.js +1 -1
- package/dist/aun/model-headers.js +17 -0
- package/dist/aun/msg/p2p.js +7 -2
- package/dist/channels/aun.js +522 -50
- package/dist/channels/builtin-plugins.js +16 -11
- package/dist/channels/daemon.js +4 -0
- package/dist/channels/dingtalk.js +83 -31
- package/dist/channels/feishu.js +29 -13
- package/dist/channels/qqbot.js +32 -11
- package/dist/channels/wechat.js +13 -3
- package/dist/channels/wecom.js +67 -27
- package/dist/cli/agent-command.js +3 -86
- package/dist/cli/agent-create.js +20 -6
- package/dist/cli/agent.js +1 -79
- package/dist/cli/aun-commands.js +18 -19
- package/dist/cli/config.js +1 -1
- package/dist/cli/daemon-commands.js +47 -2
- package/dist/cli/data-command.js +9 -1
- package/dist/cli/debug-mode.js +1 -1
- package/dist/cli/diag.js +1 -1
- package/dist/cli/directory.js +149 -91
- package/dist/cli/getmeta-command.js +1 -1
- package/dist/cli/index.js +79 -22
- package/dist/cli/init.js +1 -23
- package/dist/cli/link-rules.js +1 -1
- package/dist/cli/managed-command-guard.js +0 -1
- package/dist/cli/pa-workspace-command.js +36 -2
- package/dist/cli/peer.js +3 -2
- package/dist/cli/restart-monitor.js +17 -3
- package/dist/cli/role.js +1 -1
- package/dist/cli/session-command.js +1 -1
- package/dist/cli/stats.js +2 -2
- package/dist/cli/task-context.js +3 -1
- package/dist/cli/thread-command.js +1 -1
- package/dist/cli/watch-msg.js +1 -1
- package/dist/config/builtin-role-templates.js +2 -2
- package/dist/config/builtin-roles.js +2 -1
- package/dist/config/config-field-policy.js +7 -2
- package/dist/config/config-manager.js +32 -10
- package/dist/config/peer-role-resolver.js +23 -1
- package/dist/config/role-schema.js +29 -4
- package/dist/config/role-service.js +12 -7
- package/dist/config/role-store.js +30 -8
- package/dist/config-store.js +75 -4
- package/dist/core/agent-ipc-registration.js +16 -59
- package/dist/core/auth/auth-gateway.js +14 -4
- package/dist/core/auth/authorization-audit.js +52 -14
- package/dist/core/auth/operation-authorizer.js +17 -11
- package/dist/core/auth/operation-catalog.js +5 -13
- package/dist/core/channel-loader.js +152 -16
- package/dist/core/channel-runtime.js +5 -0
- package/dist/core/command/agent-control.js +2 -1
- package/dist/core/command/agentmd-menu-request.js +50 -0
- package/dist/core/command/agentmd-menu.js +156 -0
- package/dist/core/command/cli-intent-parser.js +0 -8
- package/dist/core/command/command-handler.js +47 -40
- package/dist/core/command/connect-menu.js +7 -3
- package/dist/core/command/menu-catalog.js +16 -2
- package/dist/core/command/menu-handler.js +107 -14
- package/dist/core/command/menu-protocol.js +28 -0
- package/dist/core/command/role-menu.js +23 -9
- package/dist/core/command/slash-handler.js +139 -51
- package/dist/core/daemon-file-cache.js +3 -3
- package/dist/core/daemon-lifecycle.js +9 -1
- package/dist/core/evolagent-registry.js +5 -4
- package/dist/core/evolagent.js +10 -12
- package/dist/core/handoff/argv-parse.js +115 -0
- package/dist/core/handoff/ipc-registration.js +1 -1
- package/dist/core/ipc-registration.js +19 -2
- package/dist/core/lifecycle-notice.js +185 -0
- package/dist/core/memory/consolidation.js +8 -1
- package/dist/core/memory/relation-sweep.js +20 -8
- package/dist/core/message/aun-ipc-sender.js +35 -2
- package/dist/core/message/contact-admission.js +530 -0
- package/dist/core/message/im-renderer.js +111 -16
- package/dist/core/message/message-admission.js +29 -54
- package/dist/core/message/message-bridge.js +62 -8
- package/dist/core/message/message-utils.js +9 -5
- package/dist/core/message/peer-mode.js +55 -13
- package/dist/core/message/response-engine.js +298 -78
- package/dist/core/message/retry-scheduler.js +4 -0
- package/dist/core/ownership/owner-resolver.js +72 -34
- package/dist/core/permission/ec-command-parser.js +25 -38
- package/dist/core/permission/readonly-shell-query.js +34 -8
- package/dist/core/permission/tool-policy.js +50 -5
- package/dist/core/relation/pa-team.js +127 -0
- package/dist/core/role/runtime-policy.js +18 -5
- package/dist/core/session/session-manager.js +9 -1
- package/dist/core/soul.js +66 -0
- package/dist/core/system-session/memory.js +2 -2
- package/dist/core/thread/thread-service.js +6 -4
- package/dist/directory/contact-admission-paths.js +9 -0
- package/dist/directory/owner-directory-admission.js +125 -29
- package/dist/directory/owner-directory-cache.js +554 -58
- package/dist/directory/owner-directory-identity.js +60 -0
- package/dist/directory/owner-directory-markdown.js +154 -58
- package/dist/directory/owner-directory-policy.js +108 -0
- package/dist/directory/owner-directory-query-operation.js +399 -90
- package/dist/directory/owner-directory-remarks.js +29 -2
- package/dist/directory/owner-directory-request-operation.js +76 -0
- package/dist/directory/owner-directory-scope.js +35 -4
- package/dist/directory/owner-directory-search.js +118 -33
- package/dist/directory/owner-directory-store.js +117 -30
- package/dist/directory/owner-directory-text.js +60 -0
- package/dist/eck/kit-renderer.js +18 -8
- package/dist/eck/manifest-engine.js +55 -6
- package/dist/ecweb/basic-settings-service.js +2 -2
- package/dist/ecweb/message-context-service.js +28 -16
- package/dist/index.js +20 -5
- package/dist/ipc.js +1 -1
- package/dist/paths.js +29 -1
- package/dist/product.js +1 -0
- package/dist/response-system/config-resolver.js +1 -1
- package/dist/response-system/engines/v1/index.js +2 -1
- package/dist/response-system/modes/single-session/index.js +7 -2
- package/dist/trigger/scheduler.js +7 -4
- package/dist/trigger/validation.js +4 -0
- package/dist/utils/error-utils.js +12 -7
- package/dist/utils/logger.js +13 -4
- package/dist/utils/npm-ops.js +2 -2
- package/dist/utils/windows-autostart.js +1 -1
- package/ecagent/README.md +18 -0
- package/ecagent/dist/agent-loop.js +43 -1
- package/ecagent/dist/harness/agent-harness.d.ts +2 -0
- package/ecagent/dist/harness/agent-harness.js +24 -8
- package/ecagent/dist/harness/compaction/compaction.js +3 -0
- package/ecagent/dist/harness/types.d.ts +1 -0
- package/kits/docs/INDEX.md +1 -0
- package/kits/docs/channels/aun.md +1 -1
- package/kits/docs/context-assembly.md +21 -6
- package/kits/docs/eck_templates/INDEX.template.md +1 -2
- package/kits/docs/evolcore/INDEX.md +4 -1
- package/kits/docs/evolcore/agent-create.md +5 -5
- package/kits/docs/evolcore/agent.md +4 -9
- package/kits/docs/evolcore/directory.md +227 -0
- package/kits/docs/evolcore/ecweb-lifecycle.md +26 -0
- package/kits/docs/evolcore/file-navigation.md +265 -0
- package/kits/docs/evolcore/menu-protocol.md +55 -0
- package/kits/docs/evolcore/msg.md +1 -0
- package/kits/docs/evolcore/peer.md +2 -1
- package/kits/docs/evolcore/response.md +7 -1
- package/kits/docs/evolcore/task.md +2 -0
- package/kits/docs/evolcore/tool-authorization.md +1 -1
- package/kits/docs/evolcore/trigger.md +4 -0
- package/kits/docs/identity/ROLE_DETAIL.md +2 -0
- package/kits/docs/prompt-loading-architecture.md +6 -2
- package/kits/eck_manifest.auxiliary.json +1 -0
- package/kits/eck_manifest.json +64 -7
- package/kits/eck_manifest.memory.json +93 -4
- package/kits/rules/01-overview.md +2 -2
- package/kits/rules/03-identity.md +10 -9
- package/kits/rules/05-venue.md +2 -4
- package/kits/rules/06-channel.md +1 -1
- package/kits/schemas/_meta.json +44 -3
- package/kits/schemas/agent-config.schema.18.json +5 -0
- package/kits/schemas/agent-config.schema.19.json +924 -0
- package/kits/schemas/agent-config.schema.20.json +925 -0
- package/kits/schemas/relation-config.schema.12.json +289 -0
- package/kits/schemas/relation-config.schema.13.json +289 -0
- package/kits/schemas/role-config.schema.2.json +215 -0
- package/kits/schemas/role-config.schema.3.json +1038 -0
- package/kits/schemas/role-registry.schema.1.json +3 -2
- package/kits/skills/ec-skill/scripts/cli.mjs +3 -2
- package/kits/skills/ec-trigger/SKILL.md +4 -4
- package/kits/skills/personal-assistant/SKILL.md +109 -53
- package/kits/skills/personal-assistant/references/02-bootstrap.md +73 -30
- package/kits/skills/personal-assistant/scripts/clean.mjs +35 -18
- package/kits/skills/personal-assistant/scripts/init.mjs +41 -2
- package/kits/skills/personal-assistant/scripts/lib/persona.mjs +185 -0
- package/kits/skills/personal-assistant/scripts/migrate.mjs +55 -4
- package/kits/skills/personal-assistant/scripts/pa.mjs +173 -0
- package/kits/skills/personal-assistant/scripts/uninstall.mjs +130 -0
- package/kits/skills/personal-assistant/templates/persona-section.md +3 -3
- package/kits/templates/message-fragments/handoff-request-to-target.md +1 -1
- package/kits/templates/soul/00-GUIDE.md +39 -0
- package/kits/templates/soul/01-identity.md +5 -0
- package/kits/templates/soul/02-persona.md +11 -0
- package/kits/templates/soul/03-duties.md +5 -0
- package/kits/templates/soul/04-working-style.md +5 -0
- package/kits/templates/system-fragments/bootstrap.md +7 -7
- package/kits/templates/system-fragments/channel.md +2 -2
- package/kits/templates/system-fragments/commands.md +3 -2
- package/kits/templates/system-fragments/directory-contacts.md +9 -0
- package/kits/templates/system-fragments/memory.md +1 -1
- package/kits/templates/system-fragments/personal-assistant.md +50 -0
- package/kits/templates/system-fragments/reply.md +7 -0
- package/kits/templates/system-fragments/session.md +6 -2
- package/package.json +3 -2
- package/dist/cli/handoff-command.js +0 -353
- package/dist/directory/owner-directory-remark-operation.js +0 -147
- package/kits/docs/evolcore/self-summary.md +0 -29
|
@@ -170,7 +170,8 @@ export function sessionFullAccessStatus(session, principalId) {
|
|
|
170
170
|
// takes precedence over any per-main-session or per-topic /fa state.
|
|
171
171
|
const override = readSessionFullAccessOverride(session);
|
|
172
172
|
const localEnabled = override?.enabled === true;
|
|
173
|
-
const
|
|
173
|
+
const source = isTopicSession(session) ? 'session' : 'relation';
|
|
174
|
+
const scope = source === 'session' ? 'topic-session' : 'relation-main';
|
|
174
175
|
if (agentStatus.enabled) {
|
|
175
176
|
return {
|
|
176
177
|
...agentStatus, effective: agentStatus.effective && session.chatType === 'private',
|
|
@@ -180,7 +181,7 @@ export function sessionFullAccessStatus(session, principalId) {
|
|
|
180
181
|
if (!override) {
|
|
181
182
|
return {
|
|
182
183
|
enabled: false, eligible: agentStatus.eligible, effective: false,
|
|
183
|
-
inherited: false, globalEnabled: false, localEnabled: false, scope, source
|
|
184
|
+
inherited: false, globalEnabled: false, localEnabled: false, scope, source, mutable: true,
|
|
184
185
|
};
|
|
185
186
|
}
|
|
186
187
|
const eligible = !!session.selfAID && isAgentFullAccessOwner(session.selfAID, principalId);
|
|
@@ -193,7 +194,7 @@ export function sessionFullAccessStatus(session, principalId) {
|
|
|
193
194
|
globalEnabled: false,
|
|
194
195
|
localEnabled,
|
|
195
196
|
scope,
|
|
196
|
-
source
|
|
197
|
+
source,
|
|
197
198
|
mutable: true,
|
|
198
199
|
authorizedBy: override.authorizedBy,
|
|
199
200
|
};
|
|
@@ -218,7 +219,11 @@ export function readSessionFullAccessOverride(session) {
|
|
|
218
219
|
const config = value;
|
|
219
220
|
return typeof config.enabled === 'boolean'
|
|
220
221
|
&& typeof config.authorizedBy === 'string' && !!config.authorizedBy.trim()
|
|
221
|
-
? {
|
|
222
|
+
? {
|
|
223
|
+
enabled: config.enabled,
|
|
224
|
+
authorizedBy: config.authorizedBy,
|
|
225
|
+
...(typeof config.promptedSessionId === 'string' ? { promptedSessionId: config.promptedSessionId } : {}),
|
|
226
|
+
}
|
|
222
227
|
: undefined;
|
|
223
228
|
}
|
|
224
229
|
function mainSessionFullAccessSelector(session) {
|
|
@@ -237,7 +242,15 @@ export function updateRelationSessionFullAccess(session, config) {
|
|
|
237
242
|
throw Object.assign(new Error('invalid session fullaccess configuration'), { code: 'INVALID_ARGUMENT' });
|
|
238
243
|
}
|
|
239
244
|
mutateConfig(ConfigTarget.Relation, selector, current => {
|
|
240
|
-
current.sessionFullAccess
|
|
245
|
+
const previous = current.sessionFullAccess;
|
|
246
|
+
const promptedSessionId = config.promptedSessionId
|
|
247
|
+
?? (config.enabled && previous?.enabled && previous.authorizedBy === config.authorizedBy
|
|
248
|
+
? previous.promptedSessionId : undefined);
|
|
249
|
+
current.sessionFullAccess = {
|
|
250
|
+
enabled: config.enabled,
|
|
251
|
+
authorizedBy: config.authorizedBy,
|
|
252
|
+
...(config.enabled && promptedSessionId ? { promptedSessionId } : {}),
|
|
253
|
+
};
|
|
241
254
|
});
|
|
242
255
|
}
|
|
243
256
|
export function createSessionFullAccessAuthorization(input) {
|
|
@@ -1574,7 +1574,15 @@ export class SessionManager {
|
|
|
1574
1574
|
if (!isTopicSession(current)) {
|
|
1575
1575
|
throw Object.assign(new Error('session fullaccess requires a topic session'), { code: 'INVALID_CONTEXT' });
|
|
1576
1576
|
}
|
|
1577
|
-
|
|
1577
|
+
const previous = current.metadata?.sessionFullAccess;
|
|
1578
|
+
const promptedSessionId = config.promptedSessionId
|
|
1579
|
+
?? (config.enabled && previous?.enabled && previous.authorizedBy === config.authorizedBy
|
|
1580
|
+
? previous.promptedSessionId : undefined);
|
|
1581
|
+
current.metadata = { ...(current.metadata ?? {}), sessionFullAccess: {
|
|
1582
|
+
enabled: config.enabled,
|
|
1583
|
+
authorizedBy: config.authorizedBy,
|
|
1584
|
+
...(config.enabled && promptedSessionId ? { promptedSessionId } : {}),
|
|
1585
|
+
} };
|
|
1578
1586
|
this.persistSession(current, 'none', { sessionFullAccessMutation: true });
|
|
1579
1587
|
return current;
|
|
1580
1588
|
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { agentSoulDir, kitsTemplatesDir, SOUL_GUIDE_FILE } from '../paths.js';
|
|
4
|
+
import { fileCache } from './daemon-file-cache.js';
|
|
5
|
+
/** 分册骨架的模板目录(与运行时目录同名同文件名)。 */
|
|
6
|
+
const SOUL_TEMPLATE_DIR = 'soul';
|
|
7
|
+
/**
|
|
8
|
+
* 判断一个分册是否「还没被写过」。
|
|
9
|
+
*
|
|
10
|
+
* 新建 agent 时每个分册都会带一份占位说明(写什么、谁写、不写什么),
|
|
11
|
+
* 方便 agent 直接看到自己该往哪写。但占位说明不是设定,不能算作已写入:
|
|
12
|
+
* - 去掉 HTML 注释后为空 → 未写入;
|
|
13
|
+
* - 与 `kits/templates/soul/<同名文件>` 完全一致 → 仍是未改动的骨架;
|
|
14
|
+
* - 每条非标题行都是 `>` 引用说明 → 只写了占位提示。
|
|
15
|
+
*/
|
|
16
|
+
export function isSoulPlaceholder(name, body) {
|
|
17
|
+
const meaningful = body
|
|
18
|
+
.replace(/<!--[\s\S]*?-->/g, '')
|
|
19
|
+
.split('\n')
|
|
20
|
+
.map(line => line.trim())
|
|
21
|
+
.filter(line => line !== '' && !line.startsWith('#') && !line.startsWith('>'));
|
|
22
|
+
if (meaningful.length === 0)
|
|
23
|
+
return true;
|
|
24
|
+
try {
|
|
25
|
+
// 模板是随包发布的只读文件,走 kits 组缓存(reload 后才刷新)。
|
|
26
|
+
const template = fileCache.getText(path.join(kitsTemplatesDir(), SOUL_TEMPLATE_DIR, name), { policy: 'on-reload', group: 'kits' });
|
|
27
|
+
return template !== null && body.trim() === template.trim();
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 读取 soul 目录内容。
|
|
35
|
+
*
|
|
36
|
+
* - 跳过机制指南 `00-GUIDE.md`:它是写文件的说明,不是自我定义。
|
|
37
|
+
* - 跳过未写入的分册(见 `isSoulPlaceholder`)。
|
|
38
|
+
* - 目录不存在返回空结果。
|
|
39
|
+
*/
|
|
40
|
+
export function readSoulFiles(aid, group = `agent-files:${aid}`) {
|
|
41
|
+
const soulDir = agentSoulDir(aid);
|
|
42
|
+
let names;
|
|
43
|
+
try {
|
|
44
|
+
names = fs.readdirSync(soulDir).filter(name => name.toLowerCase().endsWith('.md')).sort();
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return { names: [], content: null };
|
|
48
|
+
}
|
|
49
|
+
const parts = [];
|
|
50
|
+
for (const name of names) {
|
|
51
|
+
if (name === SOUL_GUIDE_FILE)
|
|
52
|
+
continue;
|
|
53
|
+
const raw = fileCache.getText(path.join(soulDir, name), { policy: 'mtime', group });
|
|
54
|
+
if (raw === null)
|
|
55
|
+
continue;
|
|
56
|
+
const body = raw.replace(/<!--[\s\S]*?-->/g, '').trim();
|
|
57
|
+
if (!body || isSoulPlaceholder(name, raw))
|
|
58
|
+
continue;
|
|
59
|
+
parts.push(body);
|
|
60
|
+
}
|
|
61
|
+
return { names, content: parts.length > 0 ? parts.join('\n\n') : null };
|
|
62
|
+
}
|
|
63
|
+
/** 只要能拼进 prompt / 喂给判定模型的正文。 */
|
|
64
|
+
export function readSoulContent(aid) {
|
|
65
|
+
return readSoulFiles(aid).content;
|
|
66
|
+
}
|
|
@@ -252,8 +252,8 @@ function readonlyDenialHint(command, policyCode, ctx) {
|
|
|
252
252
|
case 'unsupported-command-or-option':
|
|
253
253
|
return '命令或选项不在只读查询白名单内。不需要 `cd`:把绝对路径直接写进命令里。'
|
|
254
254
|
+ (isPowerShell
|
|
255
|
-
?
|
|
256
|
-
: '
|
|
255
|
+
? '列目录请改用 Glob 工具(path 传绝对路径);读单个文件请用 Read 工具或 `pwsh -NoProfile -Command "Get-Content -LiteralPath <绝对路径> [-Raw]"`;包装内不接受 `Get-ChildItem` / `ls`,也不接受 `;` 连接的多段查询。'
|
|
256
|
+
: '列文件请改用 Glob 工具(path 传绝对路径);读文件用 cat 或 Read 工具;有文件工具时优先用 Read / Glob / Grep。');
|
|
257
257
|
case 'unsupported-pipeline-filter':
|
|
258
258
|
return '管道 `|` 后面的命令不是纯 stdin 过滤命令。';
|
|
259
259
|
case 'pipeline-filter-must-use-stdin':
|
|
@@ -332,15 +332,17 @@ export class ThreadService {
|
|
|
332
332
|
if (entry.name === sanitized)
|
|
333
333
|
return { changed: false, name: sanitized };
|
|
334
334
|
const nameUpdatedAt = Math.max(Date.now(), Number(entry.nameUpdatedAt ?? entry.createdAt ?? 0) + 1);
|
|
335
|
+
const session = await this.sessionManager.getThreadSession(entryChannelType(entry), entry.peer, threadId, selfAid);
|
|
336
|
+
if (session) {
|
|
337
|
+
const renamed = await this.sessionManager.renameSession(session.id, sanitized, nameUpdatedAt);
|
|
338
|
+
if (!renamed)
|
|
339
|
+
throw new ThreadServiceError(`话题会话改名失败: ${session.id}`, 'RENAME_FAILED');
|
|
340
|
+
}
|
|
335
341
|
entry.name = sanitized;
|
|
336
342
|
entry.nameUpdatedAt = nameUpdatedAt;
|
|
337
343
|
entry.lastActiveAt = nameUpdatedAt;
|
|
338
344
|
this.writeThreadIndex(selfAid, index);
|
|
339
345
|
this.appendThreadSnapshot(selfAid, entry);
|
|
340
|
-
const session = await this.sessionManager.getThreadSession(entryChannelType(entry), entry.peer, threadId, selfAid);
|
|
341
|
-
if (session) {
|
|
342
|
-
await this.sessionManager.renameSession(session.id, sanitized, nameUpdatedAt);
|
|
343
|
-
}
|
|
344
346
|
return { changed: true, name: sanitized, nameUpdatedAt, peerKind: entry.peerKind };
|
|
345
347
|
}
|
|
346
348
|
/**
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { resolveRoot } from '../paths.js';
|
|
3
|
+
import { isValidAid } from '../aun/aid/validation.js';
|
|
4
|
+
/** Runtime authority is daemon-owned H-class data, outside Agent-editable content. */
|
|
5
|
+
export function contactAdmissionDirectory(selfAid) {
|
|
6
|
+
if (!isValidAid(selfAid))
|
|
7
|
+
throw new TypeError('contact admission requires a valid Agent AID');
|
|
8
|
+
return path.join(resolveRoot(), 'data', 'contact-admission', selfAid);
|
|
9
|
+
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
1
2
|
import path from 'node:path';
|
|
2
3
|
import { agentDir } from '../paths.js';
|
|
3
4
|
import { existsAtomic } from '../utils/atomic-write.js';
|
|
4
5
|
import { ownerResolver } from '../core/ownership/owner-resolver.js';
|
|
5
|
-
import {
|
|
6
|
+
import { logger } from '../utils/logger.js';
|
|
6
7
|
import { isInternalEntry, ownerRefAid } from './owner-directory-scope.js';
|
|
8
|
+
import { isPersonalAssistantSnapshot, resolveAllowExternalContacts } from './owner-directory-policy.js';
|
|
7
9
|
import { OwnerDirectoryCache, OWNER_DIRECTORY_INITIAL_WAIT_MS, OWNER_DIRECTORY_SCOPES, } from './owner-directory-cache.js';
|
|
8
10
|
const liveDirectories = new Map();
|
|
9
11
|
// Coalesce negative lookups from unrelated senders as well as concurrent calls.
|
|
@@ -30,21 +32,96 @@ export function liveOwnerDirectoryCache(selfAid) {
|
|
|
30
32
|
return live.cache;
|
|
31
33
|
}
|
|
32
34
|
/**
|
|
33
|
-
*
|
|
35
|
+
* `access` 开关变更后重算 `directory.md` 与 `identity.json`。
|
|
34
36
|
*
|
|
35
|
-
*
|
|
36
|
-
* 准入读的是 `config.json
|
|
37
|
-
*
|
|
37
|
+
* 会改变注入内容的开关有五个:内部档、外部档、总开关、白名单、黑名单。
|
|
38
|
+
* 准入读的是 `config.json`(每次现读,立刻生效),而注入内容来自渲染产物,
|
|
39
|
+
* 不重算就会停在旧版本——Owner 打开开关后,来客能联系了,agent 却看不到是谁。
|
|
38
40
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
+
* 优先复用 channel 注册着的活缓存;没有活缓存(channel 未连、命令走 CLI 进程)
|
|
42
|
+
* 时按 AID 临时读一次本地快照重新渲染,产出同样的字节。后者多一次读盘,
|
|
43
|
+
* 但保证"配置一改,注入就变",不必等到下一条入站消息。
|
|
44
|
+
*
|
|
45
|
+
* **ownerAid 必须取自 authorization.json,不能用 `ownerResolver` 猜。** 离线时
|
|
46
|
+
* 解析器只能给出 known owner(可能来自过期缓存或另一条关系),一旦与授权文件里的
|
|
47
|
+
* grantee/owner 对不上,`scopeAccess` 就不是 authorized,`directoryGone()` 随之
|
|
48
|
+
* 判真,`refreshMarkdown` 会把 `directory.md` 删掉——一个只读的开关变更变成删注入源。
|
|
49
|
+
* 授权文件缺失时直接返回 false:没有授权就没有可渲染的内容,也不该动现有文件。
|
|
41
50
|
*/
|
|
42
51
|
export function refreshDirectoryMarkdown(selfAid) {
|
|
43
|
-
const
|
|
44
|
-
if (
|
|
52
|
+
const live = liveOwnerDirectoryCache(selfAid);
|
|
53
|
+
if (live) {
|
|
54
|
+
live.refreshMarkdown();
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
const directory = path.join(agentDir(selfAid), 'directory');
|
|
58
|
+
const ownerAid = authorizedOwnerAid(directory, selfAid);
|
|
59
|
+
if (!ownerAid)
|
|
60
|
+
return false;
|
|
61
|
+
try {
|
|
62
|
+
const cache = new OwnerDirectoryCache({ selfAid, ownerAid, directory });
|
|
63
|
+
cache.refreshMarkdown();
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
45
67
|
return false;
|
|
46
|
-
|
|
47
|
-
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 从 `authorization.json` 读出授权是的哪个 Owner。只认与本 agent 匹配的凭证。
|
|
72
|
+
*
|
|
73
|
+
* 与 `OwnerDirectoryCache.loadGrant` 同一条判据,但不依赖缓存实例——这里只是
|
|
74
|
+
* 为了在无活缓存时找到一个与磁盘授权一致的 ownerAid,好让 `scopeAccess` 有据可查。
|
|
75
|
+
*/
|
|
76
|
+
export function authorizedOwnerAid(directory, selfAid) {
|
|
77
|
+
try {
|
|
78
|
+
const raw = JSON.parse(fs.readFileSync(path.join(directory, 'authorization.json'), 'utf8'));
|
|
79
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
80
|
+
return undefined;
|
|
81
|
+
const record = raw;
|
|
82
|
+
if (record.granteeAid !== selfAid)
|
|
83
|
+
return undefined;
|
|
84
|
+
const ownerAid = typeof record.ownerAid === 'string' ? record.ownerAid.trim() : '';
|
|
85
|
+
return ownerAid || undefined;
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* `ec directory refresh`:拉一次最新名录并重算注入文件。
|
|
93
|
+
*
|
|
94
|
+
* 两件事分开看:
|
|
95
|
+
* - **重算**(`directory.md` / `identity.json`)是纯本地 I/O,永远能做,不依赖连接。
|
|
96
|
+
* 直接改写 `remarks/*.md` 之后调它,改动立刻进注入。
|
|
97
|
+
* - **拉取**(远端 → 共享缓存 → 落地)需要活的 channel;没有连接时跳过,
|
|
98
|
+
* 但重算照做,返回 `pulled: false` 让调用方知道"没拉到新的"。
|
|
99
|
+
*/
|
|
100
|
+
export async function refreshOwnerDirectory(selfAid) {
|
|
101
|
+
const live = liveOwnerDirectoryCache(selfAid);
|
|
102
|
+
const scope = OWNER_DIRECTORY_SCOPES[0];
|
|
103
|
+
if (live) {
|
|
104
|
+
if (live.scopeAccess(scope) !== 'authorized')
|
|
105
|
+
return { pulled: false, regenerated: false };
|
|
106
|
+
let timer;
|
|
107
|
+
try {
|
|
108
|
+
// 空 digests = 无条件拉取,不做摘要比对。
|
|
109
|
+
await Promise.race([
|
|
110
|
+
live.pullAndApply({}).catch(() => undefined),
|
|
111
|
+
new Promise(resolve => { timer = setTimeout(resolve, OWNER_DIRECTORY_INITIAL_WAIT_MS); }),
|
|
112
|
+
]);
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
if (timer)
|
|
116
|
+
clearTimeout(timer);
|
|
117
|
+
}
|
|
118
|
+
// 落地本身会重算;再显式算一次兜住"拉取失败但备注改过"的情形。
|
|
119
|
+
const regenerated = refreshDirectoryMarkdown(selfAid);
|
|
120
|
+
const snapshot = live.readSnapshot(scope);
|
|
121
|
+
return { pulled: !!snapshot, ...(snapshot ? { total: snapshot.total } : {}), regenerated };
|
|
122
|
+
}
|
|
123
|
+
// 没有连接:拉不动,但本地重算照做——手工改备注之后最需要的正是这一步。
|
|
124
|
+
return { pulled: false, regenerated: refreshDirectoryMarkdown(selfAid) };
|
|
48
125
|
}
|
|
49
126
|
/**
|
|
50
127
|
* 触发一次有界同步:处理待办的 pending 标记(拉取 + 落盘),最多等
|
|
@@ -114,18 +191,10 @@ export async function prepareOwnerDirectoryAdmission(selfAid, aid) {
|
|
|
114
191
|
}
|
|
115
192
|
}
|
|
116
193
|
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* 个人助理要联系 Owner 之外的人,所以对自己默认打开;其余 agent 默认关闭,
|
|
120
|
-
* 只有明确有外部业务需求时才由 Owner 打开。准入、注入、查询三处共用这一个判据。
|
|
194
|
+
* 外部联系开关的最终值、个人助理判据都搬到了 `owner-directory-policy`,
|
|
195
|
+
* 这里只做转出:老调用方(channel、查询命令、测试)仍从本模块 import。
|
|
121
196
|
*/
|
|
122
|
-
export
|
|
123
|
-
/** 显式配置;缺省时从配置文件读。测试用来固定优先级。 */
|
|
124
|
-
explicit = readExplicitAllowExternalContacts(selfAid)) {
|
|
125
|
-
if (explicit !== undefined)
|
|
126
|
-
return explicit;
|
|
127
|
-
return !!snapshot?.personalAssistant?.aid && snapshot.personalAssistant.aid === selfAid;
|
|
128
|
-
}
|
|
197
|
+
export { isPersonalAssistantSnapshot, resolveAllowExternalContacts };
|
|
129
198
|
/**
|
|
130
199
|
* 本地判定一条 AID 在名录中的身份。只读本地快照,不发起网络请求。
|
|
131
200
|
*
|
|
@@ -136,26 +205,44 @@ explicit = readExplicitAllowExternalContacts(selfAid)) {
|
|
|
136
205
|
* 返回 undefined 表示这个 Agent 从未接入名录(无授权也无快照),调用方应保持
|
|
137
206
|
* 旧的准入行为。
|
|
138
207
|
*/
|
|
139
|
-
export function readOwnerDirectoryMembership(selfAid, aid) {
|
|
208
|
+
export function readOwnerDirectoryMembership(selfAid, aid, appOwned = false) {
|
|
140
209
|
const directory = path.join(agentDir(selfAid), 'directory');
|
|
141
210
|
// "从未接入名录"与"接入过但当前不可用"是两种结论,判据是盘上有没有痕迹。
|
|
142
211
|
// 原子写的热备名(`*.json_`)由 existsAtomic 负责,业务层不关心后缀。
|
|
143
212
|
const configured = existsAtomic(path.join(directory, 'authorization.json'))
|
|
144
213
|
|| existsAtomic(path.join(directory, 'contacts.json'));
|
|
145
|
-
if (!configured)
|
|
214
|
+
if (!configured && !appOwned)
|
|
146
215
|
return undefined;
|
|
147
216
|
const ownerAid = ownerResolver.owner(selfAid);
|
|
148
|
-
if (!ownerAid)
|
|
217
|
+
if (!ownerAid) {
|
|
218
|
+
logDirectoryAdmissionUnavailable(selfAid, aid, 'owner_unresolved');
|
|
149
219
|
return { category: 'unavailable' };
|
|
220
|
+
}
|
|
150
221
|
try {
|
|
151
|
-
const
|
|
222
|
+
const relationVersion = appOwned ? ownerResolver.snapshot(selfAid).consensus?.relation_version : undefined;
|
|
223
|
+
if (appOwned && (!Number.isSafeInteger(relationVersion) || (relationVersion ?? 0) <= 0))
|
|
224
|
+
return { category: 'unavailable' };
|
|
225
|
+
const cache = new OwnerDirectoryCache({ selfAid, ownerAid, directory, relationVersion });
|
|
152
226
|
const scope = OWNER_DIRECTORY_SCOPES[0];
|
|
153
|
-
|
|
227
|
+
const accessReason = cache.scopeAccessUnavailableReason(scope);
|
|
228
|
+
if (accessReason) {
|
|
229
|
+
logDirectoryAdmissionUnavailable(selfAid, aid, accessReason, ownerAid);
|
|
154
230
|
return { category: 'unavailable' };
|
|
231
|
+
}
|
|
155
232
|
const snapshot = cache.readSnapshot(scope);
|
|
156
|
-
if (!snapshot)
|
|
233
|
+
if (!snapshot) {
|
|
234
|
+
logDirectoryAdmissionUnavailable(selfAid, aid, existsAtomic(path.join(directory, `${scope}.json`)) ? 'snapshot_invalid' : 'snapshot_missing', ownerAid);
|
|
157
235
|
return { category: 'unavailable' };
|
|
236
|
+
}
|
|
158
237
|
const effectiveAllowExternalContacts = resolveAllowExternalContacts(selfAid, snapshot);
|
|
238
|
+
if (appOwned) {
|
|
239
|
+
const entry = snapshot.entries.find(item => item.aid === aid);
|
|
240
|
+
const blocked = snapshot.blockedAids?.includes(aid) ?? false;
|
|
241
|
+
const scopeExcluded = entry?.agent_scope?.mode === 'selected' && !entry.agent_scope.agent_aids.includes(selfAid);
|
|
242
|
+
const internal = entry ? isInternalEntry(entry, ownerRefAid(snapshot, ownerAid)) : undefined;
|
|
243
|
+
return { category: entry ? (entry.type === 'agent' ? 'agent' : entry.type === 'human' ? 'human' : 'other') : 'not_found',
|
|
244
|
+
entry, blocked, scopeExcluded, internal, effectiveAllowExternalContacts };
|
|
245
|
+
}
|
|
159
246
|
if (aid === ownerAid || snapshot.owner?.aid === aid) {
|
|
160
247
|
return { category: 'owner', internal: true, effectiveAllowExternalContacts };
|
|
161
248
|
}
|
|
@@ -180,7 +267,16 @@ export function readOwnerDirectoryMembership(selfAid, aid) {
|
|
|
180
267
|
// 准入仍按内外分档(与宿主类型一致),只是不假装知道它是什么。
|
|
181
268
|
return { category: 'other', internal, entry, effectiveAllowExternalContacts };
|
|
182
269
|
}
|
|
183
|
-
catch {
|
|
270
|
+
catch (error) {
|
|
271
|
+
const code = error && typeof error === 'object' && 'code' in error
|
|
272
|
+
&& typeof error.code === 'string' && /^[A-Z][A-Z0-9_]{0,63}$/.test(error.code)
|
|
273
|
+
? error.code
|
|
274
|
+
: error instanceof Error ? error.name : 'unknown';
|
|
275
|
+
logDirectoryAdmissionUnavailable(selfAid, aid, 'read_error', ownerAid, code);
|
|
184
276
|
return { category: 'unavailable' };
|
|
185
277
|
}
|
|
186
278
|
}
|
|
279
|
+
function logDirectoryAdmissionUnavailable(selfAid, targetAid, reason, ownerAid, errorCode) {
|
|
280
|
+
logger.warn(`[OwnerDirectoryAdmission] unavailable self=${selfAid} target=${targetAid}`
|
|
281
|
+
+ ` owner=${ownerAid ?? '<unresolved>'} reason=${reason}${errorCode ? ` error=${errorCode}` : ''}`);
|
|
282
|
+
}
|