evolcore 0.0.1 → 0.0.2
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 +813 -0
- package/LICENSE +21 -0
- package/MIGRATION-0.5.0.md +378 -0
- package/README.md +318 -14
- package/ROLE_ACCESS_CONTROL.md +174 -0
- package/assets/.env.template +4 -0
- package/assets/brand/evolcore/README.md +19 -0
- package/assets/brand/evolcore/evolcore-app-icon.png +0 -0
- package/assets/brand/evolcore/evolcore-app-icon.svg +13 -0
- package/assets/brand/evolcore/evolcore-brand-board.png +0 -0
- package/assets/brand/evolcore/evolcore-brand-board.svg +126 -0
- package/assets/brand/evolcore/evolcore-logo-kit.zip +0 -0
- package/assets/brand/evolcore/evolcore-logo-reverse.png +0 -0
- package/assets/brand/evolcore/evolcore-logo-reverse.svg +14 -0
- package/assets/brand/evolcore/evolcore-logo.png +0 -0
- package/assets/brand/evolcore/evolcore-logo.svg +14 -0
- package/assets/brand/evolcore/evolcore-mark.png +0 -0
- package/assets/brand/evolcore/evolcore-mark.svg +10 -0
- package/bin/ec-safe-output.js +161 -0
- package/bin/ec.js +29 -0
- package/dist/agents/baseagent.js +163 -0
- package/dist/agents/claude-runner.js +2385 -0
- package/dist/agents/codex-app-server-client.js +448 -0
- package/dist/agents/codex-runner.js +2639 -0
- package/dist/agents/gemini-runner.js +666 -0
- package/dist/agents/runner-types.js +75 -0
- package/dist/aun/aid/agentmd.js +216 -0
- package/dist/aun/aid/client.js +132 -0
- package/dist/aun/aid/control-aid.js +91 -0
- package/dist/aun/aid/identity.js +518 -0
- package/dist/aun/aid/index.js +4 -0
- package/dist/aun/aid/store.js +74 -0
- package/dist/aun/aid/types.js +1 -0
- package/dist/aun/aid/validation.js +21 -0
- package/dist/aun/group-identity.js +10 -0
- package/dist/aun/msg/group-index.js +6 -0
- package/dist/aun/msg/group.js +1231 -0
- package/dist/aun/msg/history.js +123 -0
- package/dist/aun/msg/index.js +5 -0
- package/dist/aun/msg/p2p.js +393 -0
- package/dist/aun/msg/payload-type.js +27 -0
- package/dist/aun/msg/upload.js +137 -0
- package/dist/aun/outbox.js +160 -0
- package/dist/aun/rpc/caller.js +42 -0
- package/dist/aun/rpc/connection.js +25 -0
- package/dist/aun/rpc/index.js +2 -0
- package/dist/aun/service-proxy.js +225 -0
- package/dist/aun/storage/download.js +29 -0
- package/dist/aun/storage/index.js +3 -0
- package/dist/aun/storage/manage.js +10 -0
- package/dist/aun/storage/upload.js +68 -0
- package/dist/channels/aun.js +4147 -0
- package/dist/channels/daemon.js +422 -0
- package/dist/channels/dingtalk.js +649 -0
- package/dist/channels/feishu.js +1789 -0
- package/dist/channels/qqbot.js +409 -0
- package/dist/channels/wechat.js +817 -0
- package/dist/channels/wecom.js +565 -0
- package/dist/cli/agent-command.js +641 -0
- package/dist/cli/agent.js +1059 -0
- package/dist/cli/aun-commands.js +1948 -0
- package/dist/cli/bench.js +1228 -0
- package/dist/cli/cli-argv.js +66 -0
- package/dist/cli/code-stats.js +329 -0
- package/dist/cli/command-log.js +82 -0
- package/dist/cli/config-selector.js +69 -0
- package/dist/cli/config.js +261 -0
- package/dist/cli/ctl-command.js +62 -0
- package/dist/cli/daemon-commands.js +2887 -0
- package/dist/cli/fs-command.js +1447 -0
- package/dist/cli/handoff-command.js +302 -0
- package/dist/cli/help.js +31 -0
- package/dist/cli/index.js +358 -0
- package/dist/cli/init-channel.js +1377 -0
- package/dist/cli/init.js +553 -0
- package/dist/cli/link-rules.js +240 -0
- package/dist/cli/model.js +590 -0
- package/dist/cli/net-check.js +723 -0
- package/dist/cli/queue-command.js +126 -0
- package/dist/cli/response.js +345 -0
- package/dist/cli/restart-monitor.js +456 -0
- package/dist/cli/stats.js +607 -0
- package/dist/cli/task-context.js +80 -0
- package/dist/cli/trigger-command.js +505 -0
- package/dist/cli/version.js +88 -0
- package/dist/cli/watch-logs.js +33 -0
- package/dist/cli/watch-msg.js +673 -0
- package/dist/config/boot-log.js +266 -0
- package/dist/config/builtin-role-templates.js +30 -0
- package/dist/config/builtin-roles.js +85 -0
- package/dist/config/config-batch-get.js +11 -0
- package/dist/config/config-field-policy.js +261 -0
- package/dist/config/config-manager.js +916 -0
- package/dist/config/config-operation-service.js +384 -0
- package/dist/config/contact-book.js +371 -0
- package/dist/config/gateway-config.js +858 -0
- package/dist/config/lifecycle.js +17 -0
- package/dist/config/mention-mode.js +27 -0
- package/dist/config/merge.js +161 -0
- package/dist/config/owner-policy.js +4 -0
- package/dist/config/peer-role-resolver.js +139 -0
- package/dist/config/resolved-config-op.js +483 -0
- package/dist/config/role-config-v4-startup.js +32 -0
- package/dist/config/role-config-v5-startup.js +27 -0
- package/dist/config/role-schema.js +105 -0
- package/dist/config/role-service.js +159 -0
- package/dist/config/role-store.js +204 -0
- package/dist/config/roles.js +55 -0
- package/dist/config/schema-registry.js +154 -0
- package/dist/config/snapshot.js +598 -0
- package/dist/config-store.js +503 -0
- package/dist/core/auth/agent-delegation.js +111 -0
- package/dist/core/auth/auth-gateway.js +166 -0
- package/dist/core/auth/authenticated-actor.js +6 -0
- package/dist/core/auth/authorization-audit.js +110 -0
- package/dist/core/auth/operation-authorizer.js +718 -0
- package/dist/core/auth/operation-catalog.js +675 -0
- package/dist/core/baseagent-loader.js +54 -0
- package/dist/core/bootstrap-service.js +175 -0
- package/dist/core/capability/capability-manager.js +316 -0
- package/dist/core/capability/providers/claude-capability-provider.js +176 -0
- package/dist/core/capability/providers/codex-capability-provider.js +148 -0
- package/dist/core/capability/providers/gemini-capability-provider.js +10 -0
- package/dist/core/capability/types.js +27 -0
- package/dist/core/causation/audit.js +103 -0
- package/dist/core/causation/aun-association.js +111 -0
- package/dist/core/causation/context.js +93 -0
- package/dist/core/causation/index.js +4 -0
- package/dist/core/causation/types.js +2 -0
- package/dist/core/channel-loader.js +277 -0
- package/dist/core/command/agent-control.js +616 -0
- package/dist/core/command/cli-intent-parser.js +225 -0
- package/dist/core/command/command-handler.js +1638 -0
- package/dist/core/command/menu-handler.js +3354 -0
- package/dist/core/command/menu-protocol.js +247 -0
- package/dist/core/command/role-menu.js +1524 -0
- package/dist/core/command/slash-gate.js +148 -0
- package/dist/core/command/slash-handler.js +3056 -0
- package/dist/core/daemon-file-cache.js +216 -0
- package/dist/core/event-bus.js +32 -0
- package/dist/core/event-catalog.js +740 -0
- package/dist/core/evolagent-registry.js +546 -0
- package/dist/core/evolagent.js +331 -0
- package/dist/core/handoff/dispatcher.js +229 -0
- package/dist/core/handoff/mutex.js +46 -0
- package/dist/core/handoff/runtime.js +324 -0
- package/dist/core/handoff/store.js +537 -0
- package/dist/core/handoff/types.js +12 -0
- package/dist/core/inference/text-inference.js +173 -0
- package/dist/core/interaction-registration.js +10 -0
- package/dist/core/interaction-router.js +278 -0
- package/dist/core/message/create-status.js +67 -0
- package/dist/core/message/im-renderer.js +657 -0
- package/dist/core/message/items-formatter.js +76 -0
- package/dist/core/message/logical-queue-bridge.js +123 -0
- package/dist/core/message/message-bridge.js +803 -0
- package/dist/core/message/message-cache.js +56 -0
- package/dist/core/message/message-log.js +339 -0
- package/dist/core/message/message-processor.js +4 -0
- package/dist/core/message/message-queue.js +1436 -0
- package/dist/core/message/message-utils.js +70 -0
- package/dist/core/message/peer-mode.js +105 -0
- package/dist/core/message/pending-hints.js +232 -0
- package/dist/core/message/response-depth.js +33 -0
- package/dist/core/message/response-engine.js +3776 -0
- package/dist/core/message/response-snapshot.js +83 -0
- package/dist/core/message/stream-debouncer.js +130 -0
- package/dist/core/message/stream-idle-monitor.js +124 -0
- package/dist/core/model/config-scope.js +162 -0
- package/dist/core/model/field-scope.js +78 -0
- package/dist/core/model/model-catalog.js +227 -0
- package/dist/core/model/model-diagnostics.js +182 -0
- package/dist/core/model/model-permission.js +90 -0
- package/dist/core/permission/approval-gateway.js +1017 -0
- package/dist/core/permission/ec-command-parser.js +347 -0
- package/dist/core/permission/execution-sandbox.js +16 -0
- package/dist/core/permission/index.js +6 -0
- package/dist/core/permission/mode.js +24 -0
- package/dist/core/permission/sandbox-runtime.js +265 -0
- package/dist/core/permission/tool-policy.js +987 -0
- package/dist/core/permission/unix-socket-policy.js +99 -0
- package/dist/core/protected-paths.js +330 -0
- package/dist/core/relation/peer-identity.js +222 -0
- package/dist/core/relation/peer-key.js +1 -0
- package/dist/core/role/runtime-policy.js +141 -0
- package/dist/core/session/adapters/claude-session-file-adapter.js +218 -0
- package/dist/core/session/adapters/codex-session-file-adapter.js +333 -0
- package/dist/core/session/adapters/gemini-session-file-adapter.js +181 -0
- package/dist/core/session/session-file-adapter.js +7 -0
- package/dist/core/session/session-file-health.js +45 -0
- package/dist/core/session/session-fs-store.js +232 -0
- package/dist/core/session/session-key.js +24 -0
- package/dist/core/session/session-manager.js +1587 -0
- package/dist/core/session/session-mapper.js +100 -0
- package/dist/core/session/session-renew.js +314 -0
- package/dist/core/session/session-title.js +128 -0
- package/dist/core/session/session-turn-coordinator.js +205 -0
- package/dist/core/session/session-turns.js +67 -0
- package/dist/core/system-channels.js +29 -0
- package/dist/eck/baseagent-caps.js +18 -0
- package/dist/eck/detect.js +47 -0
- package/dist/eck/group-rules-sync.js +345 -0
- package/dist/eck/init.js +77 -0
- package/dist/eck/kit-renderer.js +359 -0
- package/dist/eck/manifest-engine.js +446 -0
- package/dist/eck/message-renderer.js +199 -0
- package/dist/eck/rules-loader.js +28 -0
- package/dist/index.js +2870 -4
- package/dist/ipc.js +748 -0
- package/dist/paths.js +262 -0
- package/dist/product.js +18 -0
- package/dist/response-system/context-builder.js +71 -0
- package/dist/response-system/coordinator.js +117 -0
- package/dist/response-system/decision-executor.js +86 -0
- package/dist/response-system/engines/v1/index.js +21 -0
- package/dist/response-system/engines/v1/interactive-flow.js +27 -0
- package/dist/response-system/engines/v1/proactive-flow.js +137 -0
- package/dist/response-system/engines/v1/types.js +1 -0
- package/dist/response-system/extensions.js +41 -0
- package/dist/response-system/index.js +6 -0
- package/dist/response-system/modes/index.js +7 -0
- package/dist/response-system/modes/single-session/index.js +72 -0
- package/dist/response-system/queues/fifo-queue.js +44 -0
- package/dist/response-system/queues/index.js +6 -0
- package/dist/response-system/queues/lifo-queue.js +42 -0
- package/dist/response-system/queues/priority-queue.js +63 -0
- package/dist/response-system/registry.js +97 -0
- package/dist/response-system/resolver.js +37 -0
- package/dist/response-system/selector.js +23 -0
- package/dist/response-system/types.js +7 -0
- package/dist/stats/billing.js +151 -0
- package/dist/stats/budget.js +93 -0
- package/dist/stats/db.js +403 -0
- package/dist/stats/eck-vars.js +89 -0
- package/dist/stats/index.js +11 -0
- package/dist/stats/normalizer.js +80 -0
- package/dist/stats/price-resolver.js +138 -0
- package/dist/stats/query.js +763 -0
- package/dist/stats/role-budget.js +168 -0
- package/dist/stats/writer.js +151 -0
- package/dist/trigger/anomaly-store.js +258 -0
- package/dist/trigger/audit.js +152 -0
- package/dist/trigger/event-source.js +119 -0
- package/dist/trigger/feedback.js +685 -0
- package/dist/trigger/history.js +290 -0
- package/dist/trigger/manager.js +291 -0
- package/dist/trigger/parser.js +520 -0
- package/dist/trigger/patch.js +148 -0
- package/dist/trigger/scheduler.js +1600 -0
- package/dist/trigger/script-executor.js +155 -0
- package/dist/trigger/state.js +145 -0
- package/dist/trigger/types.js +1 -0
- package/dist/trigger/validation.js +621 -0
- package/dist/types.js +12 -0
- package/dist/utils/aid-bind.js +299 -0
- package/dist/utils/atomic-write.js +95 -0
- package/dist/utils/avatar-upload.js +123 -0
- package/dist/utils/cross-platform.js +297 -0
- package/dist/utils/ecweb-utils.js +73 -0
- package/dist/utils/error-dict.json +153 -0
- package/dist/utils/error-utils.js +349 -0
- package/dist/utils/instance-registry.js +437 -0
- package/dist/utils/locale.js +21 -0
- package/dist/utils/log-writer.js +224 -0
- package/dist/utils/logger.js +89 -0
- package/dist/utils/markdown-to-plain-text.js +20 -0
- package/dist/utils/media-cache.js +271 -0
- package/dist/utils/model-prices.jsonl +17 -0
- package/dist/utils/npm-ops.js +210 -0
- package/dist/utils/process-introspect.js +133 -0
- package/dist/utils/process-tree-stats.js +271 -0
- package/dist/utils/project-path.js +74 -0
- package/dist/utils/stats.js +410 -0
- package/dist/utils/tool-summary.js +284 -0
- package/dist/utils/welcome.js +268 -0
- package/kits/docs/GUIDE.md +20 -0
- package/kits/docs/INDEX.md +65 -0
- package/kits/docs/aun/CHEATSHEET.md +19 -0
- package/kits/docs/aun/SYNC_PROTOCOL.md +15 -0
- package/kits/docs/channels/aun.md +65 -0
- package/kits/docs/channels/feishu.md +56 -0
- package/kits/docs/context-assembly.md +366 -0
- package/kits/docs/eck_templates/GUIDE.template.md +22 -0
- package/kits/docs/eck_templates/INDEX.template.md +28 -0
- package/kits/docs/eck_templates/path-registry.template.md +33 -0
- package/kits/docs/eck_templates/runtime.template.md +19 -0
- package/kits/docs/evolcore/INDEX.md +66 -0
- package/kits/docs/evolcore/agent.md +69 -0
- package/kits/docs/evolcore/aid.md +49 -0
- package/kits/docs/evolcore/config.md +149 -0
- package/kits/docs/evolcore/ctl.md +46 -0
- package/kits/docs/evolcore/event.md +216 -0
- package/kits/docs/evolcore/fs-architecture.md +1215 -0
- package/kits/docs/evolcore/fs.md +101 -0
- package/kits/docs/evolcore/group-fs.md +17 -0
- package/kits/docs/evolcore/group-rules.md +226 -0
- package/kits/docs/evolcore/group.md +141 -0
- package/kits/docs/evolcore/model.md +47 -0
- package/kits/docs/evolcore/msg.md +130 -0
- package/kits/docs/evolcore/response.md +80 -0
- package/kits/docs/evolcore/rpc.md +35 -0
- package/kits/docs/evolcore/self-summary.md +29 -0
- package/kits/docs/evolcore/stats.md +70 -0
- package/kits/docs/evolcore/storage.md +49 -0
- package/kits/docs/evolcore/trigger.md +524 -0
- package/kits/docs/identity/AID_PROFILE_SPEC.md +26 -0
- package/kits/docs/identity/PATH_OPS.md +16 -0
- package/kits/docs/identity/ROLE_DETAIL.md +23 -0
- package/kits/docs/identity/identity-tools.md +26 -0
- package/kits/docs/path-registry.md +43 -0
- package/kits/docs/prompt-loading-architecture.md +266 -0
- package/kits/docs/venues/aun-group.md +45 -0
- package/kits/docs/venues/aun-private.md +10 -0
- package/kits/docs/venues/client-desktop.md +10 -0
- package/kits/docs/venues/client-mobile.md +10 -0
- package/kits/docs/venues/feishu-group.md +13 -0
- package/kits/docs/venues/feishu-private.md +9 -0
- package/kits/docs/venues/group.md +25 -0
- package/kits/docs/venues/private.md +10 -0
- package/kits/eck_manifest.auxiliary.json +43 -0
- package/kits/eck_manifest.json +191 -0
- package/kits/eck_message_manifest.json +63 -0
- package/kits/migrations/README-role-config-v4.md +32 -0
- package/kits/migrations/migrate-role-config-v4.mjs +623 -0
- package/kits/migrations/migrate-role-config-v5.mjs +346 -0
- package/kits/migrations/rename-config-file.mjs +99 -0
- package/kits/rules/01-overview.md +142 -0
- package/kits/rules/02-navigation.md +76 -0
- package/kits/rules/03-identity.md +34 -0
- package/kits/rules/04-relation.md +59 -0
- package/kits/rules/05-venue.md +44 -0
- package/kits/rules/06-channel.md +59 -0
- package/kits/schemas/_meta.json +29 -0
- package/kits/schemas/agent-config.schema.1.json +177 -0
- package/kits/schemas/agent-config.schema.2.json +239 -0
- package/kits/schemas/agent-config.schema.3.json +119 -0
- package/kits/schemas/agent-config.schema.4.json +208 -0
- package/kits/schemas/agent-config.schema.5.json +326 -0
- package/kits/schemas/contact-book.schema.1.json +36 -0
- package/kits/schemas/daemon.schema.1.json +90 -0
- package/kits/schemas/defaults.schema.1.json +81 -0
- package/kits/schemas/menu-exec-schema-commands.md +208 -0
- package/kits/schemas/migrations/README.md +28 -0
- package/kits/schemas/relation-config.schema.1.json +158 -0
- package/kits/schemas/relation-config.schema.2.json +73 -0
- package/kits/schemas/relation-config.schema.3.json +50 -0
- package/kits/schemas/relation-config.schema.4.json +47 -0
- package/kits/schemas/role-config.schema.1.json +200 -0
- package/kits/schemas/role-registry.schema.1.json +35 -0
- package/kits/schemas/single-session.schema.1.json +31 -0
- package/kits/templates/bootstrap-welcome.md +15 -0
- package/kits/templates/message-fragments/handoff-request-to-target.md +13 -0
- package/kits/templates/message-fragments/handoff-response-to-origin.md +10 -0
- package/kits/templates/message-fragments/inject-default.md +2 -0
- package/kits/templates/message-fragments/item.md +2 -0
- package/kits/templates/roles/admin.json +8 -0
- package/kits/templates/roles/member.json +40 -0
- package/kits/templates/roles/owner.json +8 -0
- package/kits/templates/roles/visitor.json +39 -0
- package/kits/templates/system-fragments/baseagent.md +14 -0
- package/kits/templates/system-fragments/bootstrap.md +16 -0
- package/kits/templates/system-fragments/channel.md +48 -0
- package/kits/templates/system-fragments/commands.md +26 -0
- package/kits/templates/system-fragments/identity.md +11 -0
- package/kits/templates/system-fragments/relation.md +19 -0
- package/kits/templates/system-fragments/session.md +53 -0
- package/kits/templates/system-fragments/venue.md +31 -0
- package/package.json +50 -15
- package/dist/index.d.ts +0 -7
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 消息缓存
|
|
3
|
+
* 用于缓存后台任务的关键事件(完成、错误)
|
|
4
|
+
*/
|
|
5
|
+
export class MessageCache {
|
|
6
|
+
cache = new Map();
|
|
7
|
+
/**
|
|
8
|
+
* 添加事件到缓存
|
|
9
|
+
*/
|
|
10
|
+
addEvent(sessionId, event) {
|
|
11
|
+
if (!this.cache.has(sessionId)) {
|
|
12
|
+
this.cache.set(sessionId, []);
|
|
13
|
+
}
|
|
14
|
+
this.cache.get(sessionId).push(event);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 获取指定会话的所有缓存事件
|
|
18
|
+
*/
|
|
19
|
+
getEvents(sessionId) {
|
|
20
|
+
return this.cache.get(sessionId) || [];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 获取指定会话的缓存事件数量
|
|
24
|
+
*/
|
|
25
|
+
getCount(sessionId) {
|
|
26
|
+
return this.cache.get(sessionId)?.length || 0;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 检查指定会话是否有缓存事件
|
|
30
|
+
*/
|
|
31
|
+
hasMessages(sessionId) {
|
|
32
|
+
return this.getCount(sessionId) > 0;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 清空指定会话的缓存事件
|
|
36
|
+
*/
|
|
37
|
+
clearEvents(sessionId) {
|
|
38
|
+
this.cache.delete(sessionId);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 清理过期的缓存事件
|
|
42
|
+
* @param maxAge 最大保留时间(毫秒),默认 72 小时
|
|
43
|
+
*/
|
|
44
|
+
cleanupExpired(maxAge = 72 * 60 * 60 * 1000) {
|
|
45
|
+
const now = Date.now();
|
|
46
|
+
for (const [sessionId, events] of this.cache.entries()) {
|
|
47
|
+
const filtered = events.filter(e => now - e.timestamp < maxAge);
|
|
48
|
+
if (filtered.length === 0) {
|
|
49
|
+
this.cache.delete(sessionId);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
this.cache.set(sessionId, filtered);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { appendJsonl, chatDirPath } from '../session/session-fs-store.js';
|
|
4
|
+
import { logger } from '../../utils/logger.js';
|
|
5
|
+
const TRANSIENT_PROTOCOL_TYPE_PREFIXES = [
|
|
6
|
+
'menu.',
|
|
7
|
+
'status.',
|
|
8
|
+
'event.',
|
|
9
|
+
'events.',
|
|
10
|
+
'task.status.',
|
|
11
|
+
'activity.',
|
|
12
|
+
];
|
|
13
|
+
const TRANSIENT_PROTOCOL_TYPES = new Set([
|
|
14
|
+
'status',
|
|
15
|
+
'event',
|
|
16
|
+
'events',
|
|
17
|
+
'task.status',
|
|
18
|
+
'activity',
|
|
19
|
+
'thought',
|
|
20
|
+
'handoff_state',
|
|
21
|
+
'handoff_result',
|
|
22
|
+
]);
|
|
23
|
+
const KNOWN_AUN_PAYLOAD_TYPES = new Set([
|
|
24
|
+
'text',
|
|
25
|
+
'quote',
|
|
26
|
+
'thought',
|
|
27
|
+
'voice',
|
|
28
|
+
'image',
|
|
29
|
+
'video',
|
|
30
|
+
'file',
|
|
31
|
+
'location',
|
|
32
|
+
'link',
|
|
33
|
+
'action_card',
|
|
34
|
+
'action_card_reply',
|
|
35
|
+
'merge',
|
|
36
|
+
'personal_card',
|
|
37
|
+
'status',
|
|
38
|
+
'event',
|
|
39
|
+
'json',
|
|
40
|
+
'tool_call',
|
|
41
|
+
'tool_result',
|
|
42
|
+
'custom',
|
|
43
|
+
]);
|
|
44
|
+
const PAYLOAD_SUMMARY_TEXT_LIMIT = 240;
|
|
45
|
+
const PAYLOAD_SUMMARY_FIELD_LIMIT = 160;
|
|
46
|
+
function truncatePayloadSummary(value, limit) {
|
|
47
|
+
const normalized = value.replace(/\s+/g, ' ').trim();
|
|
48
|
+
return normalized.length > limit ? `${normalized.slice(0, limit - 1)}…` : normalized;
|
|
49
|
+
}
|
|
50
|
+
function firstPayloadString(payload, keys) {
|
|
51
|
+
const value = firstPayloadContentString(payload, keys);
|
|
52
|
+
return value === undefined ? undefined : truncatePayloadSummary(value, PAYLOAD_SUMMARY_FIELD_LIMIT);
|
|
53
|
+
}
|
|
54
|
+
function firstPayloadContentString(payload, keys) {
|
|
55
|
+
for (const key of keys) {
|
|
56
|
+
const value = payload[key];
|
|
57
|
+
if (typeof value === 'string' && value.trim()) {
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
function payloadAttachmentFilename(payload) {
|
|
64
|
+
const direct = firstPayloadString(payload, ['filename', 'file_name', 'name']);
|
|
65
|
+
if (direct)
|
|
66
|
+
return direct;
|
|
67
|
+
const attachments = Array.isArray(payload.attachments) ? payload.attachments : [];
|
|
68
|
+
for (const attachment of attachments) {
|
|
69
|
+
if (!attachment || typeof attachment !== 'object' || Array.isArray(attachment))
|
|
70
|
+
continue;
|
|
71
|
+
const filename = firstPayloadString(attachment, ['filename', 'file_name', 'name']);
|
|
72
|
+
if (filename)
|
|
73
|
+
return filename;
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
function payloadArrayCount(payload, keys) {
|
|
78
|
+
for (const key of keys) {
|
|
79
|
+
const value = payload[key];
|
|
80
|
+
if (Array.isArray(value))
|
|
81
|
+
return value.length;
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
function compactPayloadSummary(summary) {
|
|
86
|
+
return Object.values(summary).some((value) => value !== undefined) ? summary : undefined;
|
|
87
|
+
}
|
|
88
|
+
function prefixedPayloadSummary(prefix, value) {
|
|
89
|
+
return value
|
|
90
|
+
? `[${prefix}] ${truncatePayloadSummary(value, PAYLOAD_SUMMARY_TEXT_LIMIT)}`
|
|
91
|
+
: `[${prefix}]`;
|
|
92
|
+
}
|
|
93
|
+
export function classifyAunPayloadForLog(payload) {
|
|
94
|
+
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
|
|
95
|
+
const content = typeof payload === 'string' ? payload : String(payload ?? '');
|
|
96
|
+
return { msgType: 'text', content };
|
|
97
|
+
}
|
|
98
|
+
const record = payload;
|
|
99
|
+
const rawType = typeof record.type === 'string' && record.type.trim()
|
|
100
|
+
? record.type.trim()
|
|
101
|
+
: undefined;
|
|
102
|
+
if (!rawType) {
|
|
103
|
+
return {
|
|
104
|
+
msgType: 'text',
|
|
105
|
+
content: firstPayloadContentString(record, ['text', 'fallback_text', 'title']) ?? '[payload]',
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
const msgType = KNOWN_AUN_PAYLOAD_TYPES.has(rawType)
|
|
109
|
+
? rawType
|
|
110
|
+
: 'custom';
|
|
111
|
+
const title = firstPayloadString(record, ['title', 'name']);
|
|
112
|
+
const text = firstPayloadString(record, ['text', 'fallback_text', 'description', 'body']);
|
|
113
|
+
const contentText = firstPayloadContentString(record, ['text', 'fallback_text', 'description', 'body']);
|
|
114
|
+
const filename = payloadAttachmentFilename(record);
|
|
115
|
+
const url = firstPayloadString(record, ['url', 'href']);
|
|
116
|
+
const kind = firstPayloadString(record, ['kind', 'event', 'status']);
|
|
117
|
+
const actionCount = payloadArrayCount(record, ['actions', 'buttons']);
|
|
118
|
+
const attachmentCount = payloadArrayCount(record, ['attachments']);
|
|
119
|
+
const payloadSummary = compactPayloadSummary({ title, text, filename, url, kind, actionCount, attachmentCount });
|
|
120
|
+
let content;
|
|
121
|
+
switch (msgType) {
|
|
122
|
+
case 'text':
|
|
123
|
+
case 'quote':
|
|
124
|
+
case 'thought':
|
|
125
|
+
content = contentText ?? `[${msgType}]`;
|
|
126
|
+
break;
|
|
127
|
+
case 'voice':
|
|
128
|
+
content = prefixedPayloadSummary('voice', firstPayloadContentString(record, ['transcript']) ?? filename ?? contentText);
|
|
129
|
+
break;
|
|
130
|
+
case 'image':
|
|
131
|
+
content = prefixedPayloadSummary('image', firstPayloadString(record, ['alt', 'title']) ?? filename ?? text);
|
|
132
|
+
break;
|
|
133
|
+
case 'video':
|
|
134
|
+
content = prefixedPayloadSummary('video', title ?? filename ?? text);
|
|
135
|
+
break;
|
|
136
|
+
case 'file':
|
|
137
|
+
content = prefixedPayloadSummary('file', filename ?? text);
|
|
138
|
+
break;
|
|
139
|
+
case 'location': {
|
|
140
|
+
const coordinates = typeof record.latitude === 'number' && typeof record.longitude === 'number'
|
|
141
|
+
? `${record.latitude},${record.longitude}`
|
|
142
|
+
: typeof record.lat === 'number' && typeof record.lng === 'number'
|
|
143
|
+
? `${record.lat},${record.lng}`
|
|
144
|
+
: undefined;
|
|
145
|
+
content = prefixedPayloadSummary('location', firstPayloadString(record, ['name', 'address']) ?? coordinates ?? text);
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
case 'link':
|
|
149
|
+
content = prefixedPayloadSummary('link', title ?? url ?? text);
|
|
150
|
+
break;
|
|
151
|
+
case 'action_card':
|
|
152
|
+
content = prefixedPayloadSummary('card', title ?? text);
|
|
153
|
+
break;
|
|
154
|
+
case 'action_card_reply':
|
|
155
|
+
content = firstPayloadString(record, ['text', 'action_value', 'value', 'action_label', 'label']) ?? '[action_card_reply]';
|
|
156
|
+
break;
|
|
157
|
+
case 'merge':
|
|
158
|
+
content = prefixedPayloadSummary('merge', title ?? (Array.isArray(record.items) ? `${record.items.length} items` : text));
|
|
159
|
+
break;
|
|
160
|
+
case 'personal_card':
|
|
161
|
+
content = prefixedPayloadSummary('personal_card', title ?? firstPayloadString(record, ['aid', 'agent_id']) ?? text);
|
|
162
|
+
break;
|
|
163
|
+
case 'status':
|
|
164
|
+
content = prefixedPayloadSummary('status', firstPayloadString(record, ['status', 'text']) ?? kind);
|
|
165
|
+
break;
|
|
166
|
+
case 'event':
|
|
167
|
+
content = prefixedPayloadSummary('event', kind ?? title ?? text);
|
|
168
|
+
break;
|
|
169
|
+
case 'json':
|
|
170
|
+
content = prefixedPayloadSummary('json', kind ?? title ?? text);
|
|
171
|
+
break;
|
|
172
|
+
case 'tool_call':
|
|
173
|
+
content = prefixedPayloadSummary('tool_call', firstPayloadString(record, ['name', 'tool_name']) ?? text);
|
|
174
|
+
break;
|
|
175
|
+
case 'tool_result': {
|
|
176
|
+
const name = firstPayloadString(record, ['name', 'tool_name']);
|
|
177
|
+
const status = firstPayloadString(record, ['status', 'error']);
|
|
178
|
+
content = prefixedPayloadSummary('tool_result', [name, status].filter(Boolean).join(' '));
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
case 'custom':
|
|
182
|
+
content = contentText ?? prefixedPayloadSummary(rawType === 'custom' ? 'custom' : `payload:${rawType}`);
|
|
183
|
+
break;
|
|
184
|
+
default:
|
|
185
|
+
content = text ?? `[${msgType}]`;
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
msgType,
|
|
189
|
+
payloadType: rawType,
|
|
190
|
+
content,
|
|
191
|
+
payloadSummary,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
const MESSAGE_LOG_FILE = 'messages.jsonl';
|
|
195
|
+
// 入方向去重:最近 msgId 缓存(LRU 风格,最多 200 条)
|
|
196
|
+
const recentMsgIds = new Set();
|
|
197
|
+
const DEDUP_MAX = 200;
|
|
198
|
+
function messageLogDedupKey(chatDir, msgId) {
|
|
199
|
+
return `${path.resolve(chatDir)}\u0000${msgId}`;
|
|
200
|
+
}
|
|
201
|
+
function hasDuplicate(chatDir, msgId) {
|
|
202
|
+
if (!msgId)
|
|
203
|
+
return false;
|
|
204
|
+
return recentMsgIds.has(messageLogDedupKey(chatDir, msgId));
|
|
205
|
+
}
|
|
206
|
+
function rememberMessageId(chatDir, msgId) {
|
|
207
|
+
if (!msgId)
|
|
208
|
+
return;
|
|
209
|
+
const dedupKey = messageLogDedupKey(chatDir, msgId);
|
|
210
|
+
if (recentMsgIds.has(dedupKey))
|
|
211
|
+
return;
|
|
212
|
+
if (recentMsgIds.size >= DEDUP_MAX) {
|
|
213
|
+
const first = recentMsgIds.values().next().value;
|
|
214
|
+
recentMsgIds.delete(first);
|
|
215
|
+
}
|
|
216
|
+
recentMsgIds.add(dedupKey);
|
|
217
|
+
}
|
|
218
|
+
function formatTimestampMs(epochMs) {
|
|
219
|
+
const d = new Date(epochMs);
|
|
220
|
+
const yyyy = d.getFullYear();
|
|
221
|
+
const mo = String(d.getMonth() + 1).padStart(2, '0');
|
|
222
|
+
const dd = String(d.getDate()).padStart(2, '0');
|
|
223
|
+
const hh = String(d.getHours()).padStart(2, '0');
|
|
224
|
+
const mi = String(d.getMinutes()).padStart(2, '0');
|
|
225
|
+
const ss = String(d.getSeconds()).padStart(2, '0');
|
|
226
|
+
const ms = String(d.getMilliseconds()).padStart(3, '0');
|
|
227
|
+
return `${yyyy}-${mo}-${dd} ${hh}:${mi}:${ss}.${ms}`;
|
|
228
|
+
}
|
|
229
|
+
export function messageLogPath(chatDir) {
|
|
230
|
+
return path.join(chatDir, MESSAGE_LOG_FILE);
|
|
231
|
+
}
|
|
232
|
+
export function resolveChatDir(sessionsDir, channelType, channelId, selfAID) {
|
|
233
|
+
return chatDirPath(sessionsDir, channelType, channelId, selfAID);
|
|
234
|
+
}
|
|
235
|
+
/** Append conversation history only. Protocol telemetry stays in its dedicated logs/stores. */
|
|
236
|
+
export function appendMessageLog(chatDir, entry) {
|
|
237
|
+
if (isTransientProtocolMessage(entry))
|
|
238
|
+
return;
|
|
239
|
+
if (entry.dir === 'in' && hasDuplicate(chatDir, entry.msgId)) {
|
|
240
|
+
logger.debug(`[MessageLog] Duplicate msgId skipped: ${entry.msgId}`);
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
try {
|
|
244
|
+
fs.mkdirSync(chatDir, { recursive: true });
|
|
245
|
+
appendJsonl(messageLogPath(chatDir), entry);
|
|
246
|
+
if (entry.dir === 'in')
|
|
247
|
+
rememberMessageId(chatDir, entry.msgId);
|
|
248
|
+
}
|
|
249
|
+
catch (e) {
|
|
250
|
+
logger.warn(`[MessageLog] Failed to write message log: ${e}`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/** Strict conversation-history append used by handoff reply binding. */
|
|
254
|
+
export function appendMessageLogStrict(chatDir, entry) {
|
|
255
|
+
if (isTransientProtocolMessage(entry))
|
|
256
|
+
return;
|
|
257
|
+
if (entry.dir === 'in' && hasDuplicate(chatDir, entry.msgId))
|
|
258
|
+
return;
|
|
259
|
+
fs.mkdirSync(chatDir, { recursive: true });
|
|
260
|
+
appendJsonl(messageLogPath(chatDir), entry);
|
|
261
|
+
if (entry.dir === 'in')
|
|
262
|
+
rememberMessageId(chatDir, entry.msgId);
|
|
263
|
+
}
|
|
264
|
+
export function buildInboundEntry(opts) {
|
|
265
|
+
const ts = opts.timestamp || Date.now();
|
|
266
|
+
const isCommandCapable = !opts.msgType || opts.msgType === 'text' || opts.msgType === 'quote' || opts.msgType === 'action_card_reply';
|
|
267
|
+
const isCommand = isCommandCapable && opts.content.startsWith('/');
|
|
268
|
+
return {
|
|
269
|
+
ts,
|
|
270
|
+
time: formatTimestampMs(ts),
|
|
271
|
+
sessionId: opts.sessionId,
|
|
272
|
+
threadId: opts.threadId ?? null,
|
|
273
|
+
dir: 'in',
|
|
274
|
+
from: opts.from,
|
|
275
|
+
to: opts.to,
|
|
276
|
+
chatType: opts.chatType,
|
|
277
|
+
groupId: opts.groupId ?? null,
|
|
278
|
+
msgId: opts.msgId ?? null,
|
|
279
|
+
msgType: isCommand ? 'command' : (opts.msgType ?? 'text'),
|
|
280
|
+
payloadType: opts.payloadType,
|
|
281
|
+
payloadSummary: opts.payloadSummary,
|
|
282
|
+
content: opts.content,
|
|
283
|
+
replyTo: opts.replyTo ?? null,
|
|
284
|
+
agent: null,
|
|
285
|
+
model: null,
|
|
286
|
+
permMode: opts.permMode ?? null,
|
|
287
|
+
cmdParsed: isCommand ? opts.content.split(/\s/)[0] : null,
|
|
288
|
+
durationMs: null,
|
|
289
|
+
encrypt: opts.encrypt,
|
|
290
|
+
chatmode: opts.chatmode,
|
|
291
|
+
peerName: opts.peerName,
|
|
292
|
+
peerType: opts.peerType,
|
|
293
|
+
source: opts.source,
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
export function buildOutboundEntry(opts) {
|
|
297
|
+
const ts = opts.timestamp || Date.now();
|
|
298
|
+
return {
|
|
299
|
+
ts,
|
|
300
|
+
time: formatTimestampMs(ts),
|
|
301
|
+
sessionId: opts.sessionId,
|
|
302
|
+
threadId: opts.threadId ?? null,
|
|
303
|
+
dir: 'out',
|
|
304
|
+
from: opts.from,
|
|
305
|
+
to: opts.to,
|
|
306
|
+
chatType: opts.chatType,
|
|
307
|
+
groupId: opts.groupId ?? null,
|
|
308
|
+
msgId: opts.msgId ?? null,
|
|
309
|
+
msgType: opts.msgType ?? 'text',
|
|
310
|
+
payloadType: opts.payloadType,
|
|
311
|
+
payloadSummary: opts.payloadSummary,
|
|
312
|
+
content: opts.content,
|
|
313
|
+
replyTo: opts.replyTo ?? null,
|
|
314
|
+
agent: opts.agent ?? null,
|
|
315
|
+
model: opts.model ?? null,
|
|
316
|
+
permMode: null,
|
|
317
|
+
cmdParsed: null,
|
|
318
|
+
durationMs: opts.durationMs ?? null,
|
|
319
|
+
numTurns: opts.numTurns ?? null,
|
|
320
|
+
usage: opts.usage ?? null,
|
|
321
|
+
encrypt: opts.encrypt,
|
|
322
|
+
chatmode: opts.chatmode,
|
|
323
|
+
peerType: opts.peerType,
|
|
324
|
+
source: opts.source ?? 'daemon',
|
|
325
|
+
handoff_trace: opts.handoff_trace,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
export function isTransientProtocolType(value) {
|
|
329
|
+
const type = value?.trim().toLowerCase();
|
|
330
|
+
if (!type)
|
|
331
|
+
return false;
|
|
332
|
+
return TRANSIENT_PROTOCOL_TYPES.has(type)
|
|
333
|
+
|| TRANSIENT_PROTOCOL_TYPE_PREFIXES.some(prefix => type.startsWith(prefix));
|
|
334
|
+
}
|
|
335
|
+
export function isTransientProtocolMessage(entry) {
|
|
336
|
+
if (!entry)
|
|
337
|
+
return false;
|
|
338
|
+
return isTransientProtocolType(entry.payloadType) || isTransientProtocolType(entry.msgType);
|
|
339
|
+
}
|