evolcore 0.0.43-test.6 → 0.0.43-test.7
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 +48 -0
- package/bin/ecmsg.js +171 -54
- package/dist/agents/baseagent-availability.js +1 -1
- package/dist/agents/codex/app-server-client.js +22 -0
- package/dist/agents/codex-runner.js +69 -2
- package/dist/aun/msg/p2p.js +6 -1
- package/dist/channels/aun.js +234 -21
- package/dist/cli/agent-create.js +2 -2
- package/dist/cli/daemon-commands.js +6 -66
- package/dist/cli/daemon-lifecycle-client.js +61 -0
- package/dist/cli/ecmsg-gateway.js +35 -2
- package/dist/cli/events-command.js +73 -0
- package/dist/cli/index.js +31 -14
- package/dist/cli/invoke-command.js +182 -0
- package/dist/cli/managed-command-guard.js +6 -1
- package/dist/cli/restart-monitor.js +45 -47
- package/dist/cli/services-command.js +111 -0
- package/dist/cli/team-workspace-command.js +134 -0
- package/dist/cli/thread-command.js +6 -2
- package/dist/cli/watch-msg.js +1 -1
- package/dist/config/config-field-policy.js +2 -2
- package/dist/config/config-manager.js +0 -7
- package/dist/config/invoke-ipc-service.js +118 -0
- package/dist/config/schema-migration.js +6 -1
- package/dist/config-store.js +2 -1
- package/dist/core/agent-reload-coordinator.js +6 -4
- package/dist/core/agent-runtime.js +104 -10
- package/dist/core/auth/delegation-authorization.js +66 -0
- package/dist/core/auth/operation-catalog.js +20 -0
- package/dist/core/baseagent-loader.js +60 -130
- package/dist/core/channel-loader.js +10 -1
- package/dist/core/channel-runtime.js +4 -3
- package/dist/core/command/agent-control.js +1 -1
- package/dist/core/command/agent-storage-menu.js +1 -1
- package/dist/core/command/chatmode-config.js +2 -36
- package/dist/core/command/command-handler.js +53 -11
- package/dist/core/command/data-root-resolver.js +32 -23
- package/dist/core/command/eventcenter-menu.js +338 -0
- package/dist/core/command/menu-catalog.js +8 -0
- package/dist/core/command/menu-handler.js +198 -84
- package/dist/core/command/menu-protocol.js +60 -11
- package/dist/core/command/menu-query-batch.js +194 -0
- package/dist/core/command/slash-gate.js +1 -1
- package/dist/core/command/slash-handler.js +34 -42
- package/dist/core/command/team-workspace-bootstrap.js +350 -0
- package/dist/core/command/{pa-workspace-calendar.js → team-workspace-calendar.js} +7 -7
- package/dist/core/command/team-workspace-initializer.js +2 -0
- package/dist/core/command/{pa-workspace-items.js → team-workspace-items.js} +83 -68
- package/dist/core/command/team-workspace-job-store.js +88 -0
- package/dist/core/command/team-workspace-management-service.js +2 -0
- package/dist/core/command/team-workspace-management-store.js +96 -0
- package/dist/core/command/team-workspace-management.js +265 -0
- package/dist/core/command/team-workspace-members-menu.js +88 -0
- package/dist/core/command/team-workspace-store.js +612 -0
- package/dist/core/command/team-workspace-team.js +193 -0
- package/dist/core/daemon-control.js +8 -5
- package/dist/core/daemon-lifecycle.js +132 -73
- package/dist/core/handoff/runtime.js +3 -3
- package/dist/core/invoke/executors/index.js +13 -0
- package/dist/core/invoke/executors/media.js +283 -0
- package/dist/core/invoke/executors/text.js +198 -0
- package/dist/core/invoke/invoke-service.js +157 -0
- package/dist/core/invoke/style-router.js +48 -0
- package/dist/core/invoke/types.js +19 -0
- package/dist/core/ipc-registration.js +46 -7
- package/dist/core/memory/consolidation.js +7 -5
- package/dist/core/message/aun-ipc-sender.js +147 -17
- package/dist/core/message/im-renderer.js +28 -84
- package/dist/core/message/message-bridge.js +4 -0
- package/dist/core/message/message-utils.js +10 -2
- package/dist/core/message/peer-mode.js +7 -34
- package/dist/core/message/response-engine.js +157 -54
- package/dist/core/message/send-options.js +7 -4
- package/dist/core/message/send-routing.js +43 -5
- package/dist/core/ownership/owner-resolver.js +7 -0
- package/dist/core/permission/ec-command-parser.js +3 -2
- package/dist/core/permission/protected-paths.js +12 -2
- package/dist/core/permission/shell-environment.js +9 -0
- package/dist/core/session/session-file-health.js +243 -0
- package/dist/core/session/session-fs-store.js +2 -3
- package/dist/core/task/task-recovery-service.js +13 -2
- package/dist/core/thread/command-parser.js +9 -2
- package/dist/core/thread/thread-operation.js +21 -2
- package/dist/core/thread/thread-route.js +13 -2
- package/dist/directory/owner-directory-admission.js +40 -53
- package/dist/directory/owner-directory-cache.js +89 -113
- package/dist/directory/owner-directory-query-operation.js +8 -10
- package/dist/eck/kit-renderer.js +7 -5
- package/dist/ecweb/basic-settings-service.js +1 -1
- package/dist/ecweb/message-context-service.js +0 -4
- package/dist/ecweb/runtime/lifecycle.js +6 -2
- package/dist/ecweb/runtime/supervisor.js +19 -4
- package/dist/event-center/admission-materializer.js +114 -0
- package/dist/event-center/bus-gate.js +136 -0
- package/dist/event-center/bus-host.js +290 -0
- package/dist/event-center/bus-resolver.js +120 -0
- package/dist/event-center/center-config.js +100 -0
- package/dist/event-center/client-factory.js +50 -0
- package/dist/event-center/daemon.js +45 -0
- package/dist/event-center/directory-source.js +53 -0
- package/dist/event-center/dispatcher.js +157 -0
- package/dist/event-center/event-log.js +110 -0
- package/dist/event-center/index-generator.js +63 -0
- package/dist/event-center/ipc-operation.js +308 -0
- package/dist/event-center/ledger.js +52 -0
- package/dist/event-center/pack-loader.js +202 -0
- package/dist/event-center/pack-manager.js +85 -0
- package/dist/event-center/registry.js +238 -0
- package/dist/event-center/runtime.js +641 -0
- package/dist/event-center/script-runner.js +356 -0
- package/dist/event-center/types.js +35 -0
- package/dist/event-center/validation.js +413 -0
- package/dist/index.js +285 -99
- package/dist/ipc.js +66 -12
- package/dist/paths.js +20 -0
- package/dist/product.js +14 -0
- package/dist/proxy/manager.js +5 -1
- package/dist/proxy/service-supervisor.js +13 -4
- package/dist/response-system/engines/v1/index.js +0 -1
- package/dist/response-system/modes/single-session/index.js +1 -1
- package/dist/services/dispatcher.js +100 -0
- package/dist/services/idempotency.js +90 -0
- package/dist/services/identity.js +52 -0
- package/dist/services/index-generator.js +70 -0
- package/dist/services/index.js +449 -0
- package/dist/services/ipc-operation.js +458 -0
- package/dist/services/ledger.js +52 -0
- package/dist/services/message-log.js +112 -0
- package/dist/services/pack-manager.js +204 -0
- package/dist/services/reply.js +55 -0
- package/dist/services/router.js +95 -0
- package/dist/services/script-runner.js +422 -0
- package/dist/services/service-payload.js +70 -0
- package/dist/services/types.js +18 -0
- package/dist/services/validation.js +297 -0
- package/dist/trigger/application-service.js +18 -0
- package/dist/trigger/event-source.js +1 -1
- package/dist/trigger/ipc-service.js +3 -1
- package/dist/trigger/script-sandbox.js +118 -22
- package/dist/utils/cross-platform.js +24 -10
- package/dist/utils/evolcore-version.js +69 -27
- package/dist/utils/instance-registry.js +19 -13
- package/dist/utils/process-introspect.js +45 -9
- package/dist/utils/startup-trace.js +74 -37
- package/dist/utils/windows-shell-trust.js +80 -6
- package/kits/docs/INDEX.md +2 -2
- package/kits/docs/aun/ABILITY_CALLING.md +7 -14
- package/kits/docs/context-assembly.md +1 -1
- package/kits/docs/evolcore/INDEX.md +2 -0
- package/kits/docs/evolcore/ability.md +4 -0
- package/kits/docs/evolcore/aid.md +1 -1
- package/kits/docs/evolcore/event-center.md +207 -0
- package/kits/docs/evolcore/file-navigation.md +10 -264
- package/kits/docs/evolcore/fs.md +3 -2
- package/kits/docs/evolcore/group.md +1 -1
- package/kits/docs/evolcore/memory-architecture.md +2 -0
- package/kits/docs/evolcore/menu-protocol.md +21 -0
- package/kits/docs/evolcore/msg.md +15 -3
- package/kits/docs/evolcore/provider.md +3 -1
- package/kits/docs/evolcore/response.md +5 -5
- package/kits/docs/evolcore/services.md +277 -0
- package/kits/docs/evolcore/task.md +2 -0
- package/kits/docs/evolcore/team-workspace.md +41 -0
- package/kits/eck_manifest.json +26 -0
- package/kits/rules/02-navigation.md +10 -1
- package/kits/schemas/_meta.json +17 -6
- package/kits/schemas/agent-config.schema.19.json +5 -7
- package/kits/schemas/agent-config.schema.20.json +936 -0
- package/kits/schemas/relation-config.schema.12.json +2 -3
- package/kits/schemas/role-config.schema.2.json +4 -4
- package/kits/skills/.switches.json +1 -5
- package/kits/skills/ability/SKILL.md +56 -0
- package/kits/skills/ability/references/calling.md +56 -0
- package/kits/skills/{ec-ability → ability}/references/contract-checklist.md +1 -1
- package/kits/skills/{ec-ability → ability}/references/development.md +9 -33
- package/kits/skills/{ec-ability → ability}/references/management.md +2 -2
- package/kits/skills/{ec-ability → ability}/references/recovery.md +1 -1
- package/kits/skills/{ec-ability → ability}/references/runtime-protocol.md +2 -0
- package/kits/skills/ec-agent-create/SKILL.md +0 -1
- package/kits/skills/ec-service-proxy/SKILL.md +34 -5
- package/kits/skills/personal-assistant/SKILL.md +6 -6
- package/kits/skills/personal-assistant/references/02-bootstrap.md +4 -4
- package/kits/skills/personal-assistant/scripts/clean.mjs +4 -4
- package/kits/skills/personal-assistant/scripts/init.mjs +2 -2
- package/kits/skills/personal-assistant/scripts/pa.mjs +4 -4
- package/kits/templates/system-fragments/channel.md +3 -3
- package/kits/templates/system-fragments/commands.md +5 -3
- package/kits/templates/system-fragments/reply.md +0 -5
- package/kits/templates/system-fragments/thread.md +10 -8
- package/package.json +3 -5
- package/dist/cli/pa-workspace-command.js +0 -225
- package/dist/core/command/pa-workspace-store.js +0 -468
- package/dist/core/relation/pa-team.js +0 -132
- package/kits/docs/evolcore/pa-workspace.md +0 -51
- package/kits/skills/aid-ability/SKILL.md +0 -244
- package/kits/skills/ec-ability/SKILL.md +0 -67
- package/kits/skills/ec-ability/references/workflows.md +0 -15
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pack.json / center.json 校验。
|
|
3
|
+
* 约束 1:1 映射 docs/event-center/schema/{pack,center}.schema.json;Schema 为权威,两者不一致改这里并同步 Schema。
|
|
4
|
+
* 手写校验器(不引 ajv),失败逐条报错。
|
|
5
|
+
*/
|
|
6
|
+
export const AID_PATTERN = /^[a-z0-9][a-z0-9.-]*\.[a-z]{2,}$/;
|
|
7
|
+
export const EVENT_NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
8
|
+
export const ROLE_NAME_PATTERN = /^[a-z0-9_-]+$/;
|
|
9
|
+
export const SCRIPT_EXTENSIONS = ['mjs', 'cjs', 'js', 'py', 'sh', 'ps1'];
|
|
10
|
+
export const RUNTIME_WHITELIST = ['node', 'python', 'bash', 'powershell'];
|
|
11
|
+
const TTL_CEILING = 3600;
|
|
12
|
+
const TIMEOUT_MIN = 1000;
|
|
13
|
+
const TIMEOUT_MAX = 300_000;
|
|
14
|
+
const TIMEOUT_DEFAULT = 30_000;
|
|
15
|
+
function isRecord(value) {
|
|
16
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
17
|
+
}
|
|
18
|
+
function optionalString(value, field, errors, max) {
|
|
19
|
+
if (value === undefined)
|
|
20
|
+
return undefined;
|
|
21
|
+
if (typeof value !== 'string') {
|
|
22
|
+
errors.push(`${field} must be a string`);
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
if (max !== undefined && value.length > max)
|
|
26
|
+
errors.push(`${field} is too long (max ${max})`);
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
function intInRange(value, field, min, max, errors) {
|
|
30
|
+
if (value === undefined)
|
|
31
|
+
return undefined;
|
|
32
|
+
if (typeof value !== 'number' || !Number.isInteger(value) || value < min || (max !== undefined && value > max)) {
|
|
33
|
+
errors.push(max === undefined ? `${field} must be an integer >= ${min}` : `${field} must be an integer between ${min} and ${max}`);
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
function isBusRef(value) {
|
|
39
|
+
if (typeof value !== 'string')
|
|
40
|
+
return false;
|
|
41
|
+
if (value === '$self' || value === '$owner' || value === '$pa')
|
|
42
|
+
return true;
|
|
43
|
+
if (/^\$pa\.[a-z0-9_-]+$/.test(value))
|
|
44
|
+
return true;
|
|
45
|
+
return AID_PATTERN.test(value);
|
|
46
|
+
}
|
|
47
|
+
function isEventName(value) {
|
|
48
|
+
return typeof value === 'string' && EVENT_NAME_PATTERN.test(value);
|
|
49
|
+
}
|
|
50
|
+
/** handler.script 结构校验(不含存在性/越界检查,那由 pack-loader 做文件系统校验)。 */
|
|
51
|
+
function validateScriptPath(script, errors) {
|
|
52
|
+
if (typeof script !== 'string' || !script) {
|
|
53
|
+
errors.push('handler.script is required');
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
if (script.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(script)) {
|
|
57
|
+
errors.push(`handler.script must be a relative path: ${script}`);
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
if (script.includes('..')) {
|
|
61
|
+
errors.push(`handler.script must not contain '..': ${script}`);
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
if (script.includes('\\')) {
|
|
65
|
+
errors.push(`handler.script must use forward slashes: ${script}`);
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
if (!script.startsWith('scripts/')) {
|
|
69
|
+
errors.push(`handler.script must be under scripts/: ${script}`);
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
const ext = script.split('.').pop() ?? '';
|
|
73
|
+
if (!SCRIPT_EXTENSIONS.includes(ext)) {
|
|
74
|
+
errors.push(`handler.script extension must be one of ${SCRIPT_EXTENSIONS.join('/')}: ${script}`);
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
return script;
|
|
78
|
+
}
|
|
79
|
+
/** trigger source.filter 语法结构校验(语义求值由 matchTriggerEvent 做)。 */
|
|
80
|
+
export function validateFilterShape(value, errors) {
|
|
81
|
+
if (value === undefined)
|
|
82
|
+
return;
|
|
83
|
+
if (!isRecord(value)) {
|
|
84
|
+
errors.push('filter must be an object');
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const extra = Object.keys(value).filter(key => key !== 'match');
|
|
88
|
+
if (extra.length)
|
|
89
|
+
errors.push(`filter only supports "match", got: ${extra.join(', ')}`);
|
|
90
|
+
const match = value.match;
|
|
91
|
+
if (match === undefined)
|
|
92
|
+
return;
|
|
93
|
+
if (!isRecord(match)) {
|
|
94
|
+
errors.push('filter.match must be an object');
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
for (const [key, expected] of Object.entries(match)) {
|
|
98
|
+
if (!/^[A-Za-z0-9_.-]+$/.test(key)) {
|
|
99
|
+
errors.push(`filter.match key contains invalid characters: ${key}`);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (isRecord(expected)) {
|
|
103
|
+
const ops = Object.keys(expected);
|
|
104
|
+
for (const op of ops) {
|
|
105
|
+
if (!['$in', '$regex', '$gt', '$gte', '$lt', '$lte', '$exists'].includes(op)) {
|
|
106
|
+
errors.push(`filter.match.${key} has unsupported operator: ${op}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if ('$in' in expected && !Array.isArray(expected.$in))
|
|
110
|
+
errors.push(`filter.match.${key}.$in must be an array`);
|
|
111
|
+
if ('$regex' in expected && typeof expected.$regex !== 'string')
|
|
112
|
+
errors.push(`filter.match.${key}.$regex must be a string`);
|
|
113
|
+
if ('$exists' in expected && typeof expected.$exists !== 'boolean')
|
|
114
|
+
errors.push(`filter.match.${key}.$exists must be a boolean`);
|
|
115
|
+
for (const op of ['$gt', '$gte', '$lt', '$lte']) {
|
|
116
|
+
if (op in expected && typeof expected[op] !== 'number')
|
|
117
|
+
errors.push(`filter.match.${key}.${op} must be a number`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
else if (!['string', 'number', 'boolean'].includes(typeof expected)) {
|
|
121
|
+
errors.push(`filter.match.${key} must be a primitive or operator object`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function validateRegisterEntry(raw, errors) {
|
|
126
|
+
if (!isRecord(raw)) {
|
|
127
|
+
errors.push('register[] entries must be objects');
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
const extra = Object.keys(raw).filter(key => !['event', 'description', 'ttl', 'maxTtl', 'schemaVersion', 'contentSchema'].includes(key));
|
|
131
|
+
if (extra.length)
|
|
132
|
+
errors.push(`register entry has unknown fields: ${extra.join(', ')}`);
|
|
133
|
+
const event = raw.event;
|
|
134
|
+
if (!isEventName(event))
|
|
135
|
+
errors.push(`register.event must match ${EVENT_NAME_PATTERN}: ${String(event)}`);
|
|
136
|
+
const description = optionalString(raw.description, 'register.description', errors, 200);
|
|
137
|
+
const ttl = raw.ttl === undefined ? 30 : intInRange(raw.ttl, 'register.ttl', 1, TTL_CEILING, errors);
|
|
138
|
+
const maxTtl = intInRange(raw.maxTtl, 'register.maxTtl', 1, TTL_CEILING, errors);
|
|
139
|
+
const schemaVersion = intInRange(raw.schemaVersion, 'register.schemaVersion', 1, undefined, errors);
|
|
140
|
+
if (raw.contentSchema !== undefined && !isRecord(raw.contentSchema))
|
|
141
|
+
errors.push('register.contentSchema must be an object');
|
|
142
|
+
if (ttl !== undefined && maxTtl !== undefined && maxTtl < ttl)
|
|
143
|
+
errors.push('register.maxTtl must be >= ttl');
|
|
144
|
+
if (event === undefined || typeof event !== 'string')
|
|
145
|
+
return undefined;
|
|
146
|
+
return {
|
|
147
|
+
event,
|
|
148
|
+
...(description !== undefined ? { description } : {}),
|
|
149
|
+
ttl: ttl ?? 30,
|
|
150
|
+
...(maxTtl !== undefined ? { maxTtl } : {}),
|
|
151
|
+
...(schemaVersion !== undefined ? { schemaVersion } : {}),
|
|
152
|
+
...(raw.contentSchema !== undefined ? { contentSchema: raw.contentSchema } : {}),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function validateHandler(raw, errors) {
|
|
156
|
+
if (!isRecord(raw)) {
|
|
157
|
+
errors.push('handler must be an object');
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
const extra = Object.keys(raw).filter(key => !['script', 'runtime', 'args', 'timeoutMs'].includes(key));
|
|
161
|
+
if (extra.length)
|
|
162
|
+
errors.push(`handler has unknown fields: ${extra.join(', ')}`);
|
|
163
|
+
const script = validateScriptPath(raw.script, errors);
|
|
164
|
+
if (raw.runtime !== undefined && !RUNTIME_WHITELIST.includes(raw.runtime)) {
|
|
165
|
+
errors.push(`handler.runtime must be one of ${RUNTIME_WHITELIST.join('/')}`);
|
|
166
|
+
}
|
|
167
|
+
if (raw.args !== undefined) {
|
|
168
|
+
if (!Array.isArray(raw.args) || raw.args.some(item => typeof item !== 'string'))
|
|
169
|
+
errors.push('handler.args must be an array of strings');
|
|
170
|
+
}
|
|
171
|
+
const timeoutMs = raw.timeoutMs === undefined ? TIMEOUT_DEFAULT : intInRange(raw.timeoutMs, 'handler.timeoutMs', TIMEOUT_MIN, TIMEOUT_MAX, errors);
|
|
172
|
+
if (script === undefined)
|
|
173
|
+
return undefined;
|
|
174
|
+
return {
|
|
175
|
+
script,
|
|
176
|
+
...(raw.runtime !== undefined ? { runtime: raw.runtime } : {}),
|
|
177
|
+
...(Array.isArray(raw.args) ? { args: raw.args } : {}),
|
|
178
|
+
timeoutMs: timeoutMs ?? TIMEOUT_DEFAULT,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
function validatePublishEntry(raw, errors) {
|
|
182
|
+
if (!isRecord(raw)) {
|
|
183
|
+
errors.push('publish[] entries must be objects');
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
const extra = Object.keys(raw).filter(key => !['bus', 'event', 'schemaVersion', 'ttl'].includes(key));
|
|
187
|
+
if (extra.length)
|
|
188
|
+
errors.push(`publish entry has unknown fields: ${extra.join(', ')}`);
|
|
189
|
+
if (!isBusRef(raw.bus))
|
|
190
|
+
errors.push(`publish.bus must be a placeholder or literal AID: ${String(raw.bus)}`);
|
|
191
|
+
if (!isEventName(raw.event))
|
|
192
|
+
errors.push(`publish.event must match ${EVENT_NAME_PATTERN}: ${String(raw.event)}`);
|
|
193
|
+
const ttl = intInRange(raw.ttl, 'publish.ttl', 1, TTL_CEILING, errors);
|
|
194
|
+
const schemaVersion = intInRange(raw.schemaVersion, 'publish.schemaVersion', 1, undefined, errors);
|
|
195
|
+
if (typeof raw.bus !== 'string' || typeof raw.event !== 'string')
|
|
196
|
+
return undefined;
|
|
197
|
+
return {
|
|
198
|
+
bus: raw.bus,
|
|
199
|
+
event: raw.event,
|
|
200
|
+
...(ttl !== undefined ? { ttl } : {}),
|
|
201
|
+
...(schemaVersion !== undefined ? { schemaVersion } : {}),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function validateSubscribeEntry(raw, errors) {
|
|
205
|
+
if (!isRecord(raw)) {
|
|
206
|
+
errors.push('subscribe[] entries must be objects');
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
const extra = Object.keys(raw).filter(key => !['bus', 'event', 'handler', 'filter'].includes(key));
|
|
210
|
+
if (extra.length)
|
|
211
|
+
errors.push(`subscribe entry has unknown fields: ${extra.join(', ')}`);
|
|
212
|
+
if (!isBusRef(raw.bus))
|
|
213
|
+
errors.push(`subscribe.bus must be a placeholder or literal AID: ${String(raw.bus)}`);
|
|
214
|
+
if (!isEventName(raw.event))
|
|
215
|
+
errors.push(`subscribe.event must match ${EVENT_NAME_PATTERN}: ${String(raw.event)}`);
|
|
216
|
+
validateFilterShape(raw.filter, errors);
|
|
217
|
+
const handler = validateHandler(raw.handler, errors);
|
|
218
|
+
if (typeof raw.bus !== 'string' || typeof raw.event !== 'string' || handler === undefined)
|
|
219
|
+
return undefined;
|
|
220
|
+
return {
|
|
221
|
+
bus: raw.bus,
|
|
222
|
+
event: raw.event,
|
|
223
|
+
...(isRecord(raw.filter) ? { filter: raw.filter } : {}),
|
|
224
|
+
handler,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/** 校验 pack.json 原始对象(不做文件系统检查)。 */
|
|
228
|
+
export function validatePackJson(raw) {
|
|
229
|
+
const errors = [];
|
|
230
|
+
if (!isRecord(raw)) {
|
|
231
|
+
return { ok: false, errors: ['pack.json must be a JSON object'] };
|
|
232
|
+
}
|
|
233
|
+
const extra = Object.keys(raw).filter(key => !['v', 'name', 'enabled', 'busResolve', 'register', 'publish', 'subscribe'].includes(key));
|
|
234
|
+
if (extra.length)
|
|
235
|
+
errors.push(`unknown top-level fields: ${extra.join(', ')}`);
|
|
236
|
+
if (raw.v !== 1)
|
|
237
|
+
errors.push('v must be 1');
|
|
238
|
+
if (typeof raw.name !== 'string' || !raw.name || raw.name.length > 64)
|
|
239
|
+
errors.push('name must be a non-empty string (max 64)');
|
|
240
|
+
if (raw.enabled !== undefined && typeof raw.enabled !== 'boolean')
|
|
241
|
+
errors.push('enabled must be a boolean');
|
|
242
|
+
if (raw.busResolve !== undefined && !['strict', 'keep-last'].includes(raw.busResolve)) {
|
|
243
|
+
errors.push('busResolve must be "strict" or "keep-last"');
|
|
244
|
+
}
|
|
245
|
+
const register = [];
|
|
246
|
+
if (raw.register !== undefined) {
|
|
247
|
+
if (!Array.isArray(raw.register))
|
|
248
|
+
errors.push('register must be an array');
|
|
249
|
+
else
|
|
250
|
+
for (const item of raw.register) {
|
|
251
|
+
const entry = validateRegisterEntry(item, errors);
|
|
252
|
+
if (entry)
|
|
253
|
+
register.push(entry);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
const publish = [];
|
|
257
|
+
if (raw.publish !== undefined) {
|
|
258
|
+
if (!Array.isArray(raw.publish))
|
|
259
|
+
errors.push('publish must be an array');
|
|
260
|
+
else
|
|
261
|
+
for (const item of raw.publish) {
|
|
262
|
+
const entry = validatePublishEntry(item, errors);
|
|
263
|
+
if (entry)
|
|
264
|
+
publish.push(entry);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const subscribe = [];
|
|
268
|
+
if (raw.subscribe !== undefined) {
|
|
269
|
+
if (!Array.isArray(raw.subscribe))
|
|
270
|
+
errors.push('subscribe must be an array');
|
|
271
|
+
else
|
|
272
|
+
for (const item of raw.subscribe) {
|
|
273
|
+
const entry = validateSubscribeEntry(item, errors);
|
|
274
|
+
if (entry)
|
|
275
|
+
subscribe.push(entry);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
// 同包内 (bus, event) 在各自列表里唯一。
|
|
279
|
+
// publish 与 subscribe 分别判重:二者方向不同,一个包既发布又订阅同一事件
|
|
280
|
+
// (自环回执、幂等校验)是正当用法,共表会把它误判成 invalid。
|
|
281
|
+
for (const [kind, entries] of [['publish', publish], ['subscribe', subscribe]]) {
|
|
282
|
+
const seen = new Set();
|
|
283
|
+
for (const entry of entries) {
|
|
284
|
+
const key = `${entry.bus}\u0000${entry.event}`;
|
|
285
|
+
if (seen.has(key))
|
|
286
|
+
errors.push(`duplicate (${entry.bus}, ${entry.event}) in ${kind}[]`);
|
|
287
|
+
else
|
|
288
|
+
seen.add(key);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (errors.length)
|
|
292
|
+
return { ok: false, errors };
|
|
293
|
+
return {
|
|
294
|
+
ok: true,
|
|
295
|
+
errors: [],
|
|
296
|
+
value: {
|
|
297
|
+
name: raw.name,
|
|
298
|
+
enabled: raw.enabled !== false,
|
|
299
|
+
busResolve: raw.busResolve ?? 'strict',
|
|
300
|
+
register,
|
|
301
|
+
publish,
|
|
302
|
+
subscribe,
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
function validateAidList(value, field, errors) {
|
|
307
|
+
if (value === undefined)
|
|
308
|
+
return [];
|
|
309
|
+
if (!Array.isArray(value)) {
|
|
310
|
+
errors.push(`${field} must be an array`);
|
|
311
|
+
return undefined;
|
|
312
|
+
}
|
|
313
|
+
const out = [];
|
|
314
|
+
for (const item of value) {
|
|
315
|
+
if (typeof item !== 'string' || !AID_PATTERN.test(item)) {
|
|
316
|
+
errors.push(`${field} contains an invalid AID: ${String(item)}`);
|
|
317
|
+
return undefined;
|
|
318
|
+
}
|
|
319
|
+
if (!out.includes(item))
|
|
320
|
+
out.push(item);
|
|
321
|
+
}
|
|
322
|
+
return out;
|
|
323
|
+
}
|
|
324
|
+
/** 校验 center.json 原始对象。 */
|
|
325
|
+
export function validateCenterConfig(raw) {
|
|
326
|
+
const errors = [];
|
|
327
|
+
if (!isRecord(raw)) {
|
|
328
|
+
return { ok: false, errors: ['center.json must be a JSON object'] };
|
|
329
|
+
}
|
|
330
|
+
const extra = Object.keys(raw).filter(key => !['v', 'enabled', 'authMode', 'admission', 'allowlist'].includes(key));
|
|
331
|
+
if (extra.length)
|
|
332
|
+
errors.push(`unknown top-level fields: ${extra.join(', ')}`);
|
|
333
|
+
if (raw.v !== 1)
|
|
334
|
+
errors.push('v must be 1');
|
|
335
|
+
if (raw.enabled !== undefined && typeof raw.enabled !== 'boolean')
|
|
336
|
+
errors.push('enabled must be a boolean');
|
|
337
|
+
if (raw.authMode !== undefined && raw.authMode !== 'allowlist') {
|
|
338
|
+
errors.push('authMode must be "allowlist"');
|
|
339
|
+
}
|
|
340
|
+
const admissionRaw = raw.admission === undefined ? {} : raw.admission;
|
|
341
|
+
let coupling = 'on';
|
|
342
|
+
let rematerializeMs = 300_000;
|
|
343
|
+
if (!isRecord(admissionRaw)) {
|
|
344
|
+
errors.push('admission must be an object');
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
const admissionExtra = Object.keys(admissionRaw).filter(key => !['coupling', 'rematerializeMs'].includes(key));
|
|
348
|
+
if (admissionExtra.length)
|
|
349
|
+
errors.push(`admission has unknown fields: ${admissionExtra.join(', ')}`);
|
|
350
|
+
if (admissionRaw.coupling !== undefined && admissionRaw.coupling !== 'on')
|
|
351
|
+
errors.push('admission.coupling must be "on"');
|
|
352
|
+
const parsedRematerializeMs = intInRange(admissionRaw.rematerializeMs, 'admission.rematerializeMs', 60_000, 3_600_000, errors);
|
|
353
|
+
if (parsedRematerializeMs !== undefined)
|
|
354
|
+
rematerializeMs = parsedRematerializeMs;
|
|
355
|
+
}
|
|
356
|
+
const allowlist = {};
|
|
357
|
+
if (raw.allowlist !== undefined) {
|
|
358
|
+
if (!isRecord(raw.allowlist))
|
|
359
|
+
errors.push('allowlist must be an object');
|
|
360
|
+
else {
|
|
361
|
+
for (const [event, entry] of Object.entries(raw.allowlist)) {
|
|
362
|
+
if (!isEventName(event)) {
|
|
363
|
+
errors.push(`allowlist key must be an event name: ${event}`);
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (!isRecord(entry)) {
|
|
367
|
+
errors.push(`allowlist.${event} must be an object`);
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
const entryExtra = Object.keys(entry).filter(key => !['allow', 'deny'].includes(key));
|
|
371
|
+
if (entryExtra.length)
|
|
372
|
+
errors.push(`allowlist.${event} has unknown fields: ${entryExtra.join(', ')}`);
|
|
373
|
+
const validateRules = (kind) => {
|
|
374
|
+
const rawRules = entry[kind];
|
|
375
|
+
if (rawRules === undefined)
|
|
376
|
+
return { publish: [], subscribe: [] };
|
|
377
|
+
if (!isRecord(rawRules)) {
|
|
378
|
+
errors.push(`allowlist.${event}.${kind} must be an object`);
|
|
379
|
+
return undefined;
|
|
380
|
+
}
|
|
381
|
+
const rulesExtra = Object.keys(rawRules).filter(key => !['publish', 'subscribe'].includes(key));
|
|
382
|
+
if (rulesExtra.length)
|
|
383
|
+
errors.push(`allowlist.${event}.${kind} has unknown fields: ${rulesExtra.join(', ')}`);
|
|
384
|
+
const publish = validateAidList(rawRules.publish, `allowlist.${event}.${kind}.publish`, errors);
|
|
385
|
+
const subscribe = validateAidList(rawRules.subscribe, `allowlist.${event}.${kind}.subscribe`, errors);
|
|
386
|
+
return publish && subscribe ? { publish, subscribe } : undefined;
|
|
387
|
+
};
|
|
388
|
+
const allow = validateRules('allow');
|
|
389
|
+
const deny = validateRules('deny');
|
|
390
|
+
if (allow && deny) {
|
|
391
|
+
for (const direction of ['publish', 'subscribe']) {
|
|
392
|
+
const overlap = allow[direction].filter(aid => deny[direction].includes(aid));
|
|
393
|
+
if (overlap.length)
|
|
394
|
+
errors.push(`allowlist.${event}.${direction} contains the same AID in allow and deny: ${overlap.join(', ')}`);
|
|
395
|
+
}
|
|
396
|
+
allowlist[event] = { allow, deny };
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (errors.length)
|
|
402
|
+
return { ok: false, errors };
|
|
403
|
+
return {
|
|
404
|
+
ok: true,
|
|
405
|
+
errors: [],
|
|
406
|
+
value: {
|
|
407
|
+
enabled: raw.enabled === true,
|
|
408
|
+
authMode: 'allowlist',
|
|
409
|
+
admission: { coupling, rematerializeMs },
|
|
410
|
+
allowlist,
|
|
411
|
+
},
|
|
412
|
+
};
|
|
413
|
+
}
|