evolcore 0.0.1 → 0.0.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 +64 -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/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 +2565 -0
- package/dist/agents/codex-app-server-client.js +448 -0
- package/dist/agents/codex-runner.js +2682 -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 +168 -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 +4164 -0
- package/dist/channels/contact-bind-code.js +134 -0
- package/dist/channels/daemon.js +422 -0
- package/dist/channels/dingtalk.js +1479 -0
- package/dist/channels/feishu.js +1865 -0
- package/dist/channels/qqbot.js +409 -0
- package/dist/channels/wechat.js +817 -0
- package/dist/channels/wecom-card.js +101 -0
- package/dist/channels/wecom-onboarding.js +82 -0
- package/dist/channels/wecom-state.js +191 -0
- package/dist/channels/wecom.js +1157 -0
- package/dist/cli/agent-command.js +642 -0
- package/dist/cli/agent.js +1059 -0
- package/dist/cli/aun-commands.js +2003 -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/contact.js +71 -0
- package/dist/cli/ctl-command.js +62 -0
- package/dist/cli/daemon-commands.js +2750 -0
- package/dist/cli/fs-command.js +1447 -0
- package/dist/cli/handoff-command.js +302 -0
- package/dist/cli/help.js +35 -0
- package/dist/cli/index.js +374 -0
- package/dist/cli/init-channel.js +1372 -0
- package/dist/cli/init.js +590 -0
- package/dist/cli/link-rules.js +240 -0
- package/dist/cli/model.js +591 -0
- package/dist/cli/net-check.js +723 -0
- package/dist/cli/queue-command.js +150 -0
- package/dist/cli/raw-key-input.js +25 -0
- package/dist/cli/response.js +344 -0
- package/dist/cli/restart-monitor.js +480 -0
- package/dist/cli/stats.js +609 -0
- package/dist/cli/task-context.js +80 -0
- package/dist/cli/trigger-command.js +545 -0
- package/dist/cli/version.js +93 -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 +42 -0
- package/dist/config/builtin-roles.js +91 -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 +1120 -0
- package/dist/config/config-operation-service.js +384 -0
- package/dist/config/contact-alias.js +68 -0
- package/dist/config/contact-book-store.js +454 -0
- package/dist/config/contact-book-v2-startup.js +35 -0
- package/dist/config/contact-book.js +224 -0
- package/dist/config/contact-operation-service.js +110 -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 +218 -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-ranks.js +18 -0
- package/dist/config/role-schema.js +105 -0
- package/dist/config/role-service.js +156 -0
- package/dist/config/role-store.js +215 -0
- package/dist/config/roles.js +64 -0
- package/dist/config/schema-registry.js +154 -0
- package/dist/config/snapshot.js +598 -0
- package/dist/config-store.js +501 -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 +119 -0
- package/dist/core/auth/operation-authorizer.js +720 -0
- package/dist/core/auth/operation-catalog.js +731 -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 +1733 -0
- package/dist/core/command/connect-menu.js +374 -0
- package/dist/core/command/menu-handler.js +3452 -0
- package/dist/core/command/menu-protocol.js +247 -0
- package/dist/core/command/role-menu.js +1623 -0
- package/dist/core/command/slash-gate.js +148 -0
- package/dist/core/command/slash-handler.js +3066 -0
- package/dist/core/daemon-file-cache.js +222 -0
- package/dist/core/event-bus.js +32 -0
- package/dist/core/event-catalog.js +810 -0
- package/dist/core/evolagent-registry.js +545 -0
- package/dist/core/evolagent.js +342 -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 +659 -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 +874 -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 +1446 -0
- package/dist/core/message/message-utils.js +76 -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 +4020 -0
- package/dist/core/message/response-snapshot.js +83 -0
- package/dist/core/message/send-receipt.js +24 -0
- package/dist/core/message/stream-debouncer.js +139 -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 +1019 -0
- package/dist/core/permission/unix-socket-policy.js +99 -0
- package/dist/core/protected-paths.js +332 -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 +273 -0
- package/dist/core/session/session-key.js +24 -0
- package/dist/core/session/session-manager.js +1643 -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 +2926 -4
- package/dist/ipc.js +777 -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 +163 -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 +294 -0
- package/dist/trigger/parser.js +595 -0
- package/dist/trigger/patch.js +155 -0
- package/dist/trigger/scheduler.js +1602 -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 +634 -0
- package/dist/types.js +12 -0
- package/dist/utils/aid-bind.js +313 -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 +444 -0
- package/dist/utils/locale.js +21 -0
- package/dist/utils/log-writer.js +270 -0
- package/dist/utils/logger.js +89 -0
- package/dist/utils/markdown-to-plain-text.js +20 -0
- package/dist/utils/media-cache.js +274 -0
- package/dist/utils/model-prices.jsonl +20 -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/restart-safety.js +31 -0
- package/dist/utils/stats.js +410 -0
- package/dist/utils/system-memory.js +62 -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 +66 -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 +68 -0
- package/kits/docs/evolcore/agent.md +77 -0
- package/kits/docs/evolcore/aid.md +52 -0
- package/kits/docs/evolcore/config.md +149 -0
- package/kits/docs/evolcore/contact.md +57 -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 +110 -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 +150 -0
- package/kits/docs/evolcore/model.md +50 -0
- package/kits/docs/evolcore/msg.md +136 -0
- package/kits/docs/evolcore/response.md +75 -0
- package/kits/docs/evolcore/rpc.md +37 -0
- package/kits/docs/evolcore/self-summary.md +29 -0
- package/kits/docs/evolcore/stats.md +83 -0
- package/kits/docs/evolcore/storage.md +50 -0
- package/kits/docs/evolcore/trigger.md +539 -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 +203 -0
- package/kits/eck_message_manifest.json +63 -0
- package/kits/migrations/README-role-config-v4.md +32 -0
- package/kits/migrations/migrate-contact-book-v2.mjs +747 -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 +32 -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/agent-config.schema.6.json +322 -0
- package/kits/schemas/contact-book.schema.1.json +36 -0
- package/kits/schemas/contact-book.schema.2.json +43 -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/relation-config.schema.5.json +47 -0
- package/kits/schemas/role-config.schema.1.json +201 -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 +9 -0
- package/kits/templates/roles/member.json +41 -0
- package/kits/templates/roles/owner.json +9 -0
- package/kits/templates/roles/visitor.json +40 -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 +28 -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,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* eck-vars.ts — 组装 $STATS_* ECK 注入变量。
|
|
3
|
+
*
|
|
4
|
+
* 分层原则(保护 prompt cache):
|
|
5
|
+
* - system prompt 层:今日累计、预算状态(变化慢,稳定可缓存)
|
|
6
|
+
* - 消息提示词层:会话级动态数据(每轮变,不注入 system)
|
|
7
|
+
*/
|
|
8
|
+
import { openReadonlyDb, getDbPath } from './db.js';
|
|
9
|
+
import { getBudgetStatus } from './budget.js';
|
|
10
|
+
function _todayRange() {
|
|
11
|
+
const now = new Date();
|
|
12
|
+
return { from_ts: Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()) };
|
|
13
|
+
}
|
|
14
|
+
function _calcTodayCosts(evolcoreHome, agentAid, peerKey) {
|
|
15
|
+
const db = openReadonlyDb(getDbPath(evolcoreHome));
|
|
16
|
+
if (!db)
|
|
17
|
+
return { usd: 0, cny: 0, input: 0, output: 0, cacheRead: 0, calls: 0 };
|
|
18
|
+
const { from_ts } = _todayRange();
|
|
19
|
+
const conds = ['ts >= ?'];
|
|
20
|
+
const params = [from_ts];
|
|
21
|
+
if (agentAid) {
|
|
22
|
+
conds.push('agent_aid = ?');
|
|
23
|
+
params.push(agentAid);
|
|
24
|
+
}
|
|
25
|
+
if (peerKey) {
|
|
26
|
+
conds.push('peer_key = ?');
|
|
27
|
+
params.push(peerKey);
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const row = db.prepare(`
|
|
31
|
+
SELECT
|
|
32
|
+
SUM(input_tokens) AS input,
|
|
33
|
+
SUM(output_tokens) AS output,
|
|
34
|
+
SUM(cache_read_tokens) AS cacheRead,
|
|
35
|
+
COUNT(*) AS calls,
|
|
36
|
+
COALESCE(SUM(cost_gateway_usd), 0) AS usd,
|
|
37
|
+
COALESCE(SUM(cost_gateway_cny), 0) AS cny
|
|
38
|
+
FROM usage_events WHERE ${conds.join(' AND ')}
|
|
39
|
+
`).get(...params);
|
|
40
|
+
return {
|
|
41
|
+
usd: row?.usd ?? 0,
|
|
42
|
+
cny: row?.cny ?? 0,
|
|
43
|
+
input: row?.input ?? 0,
|
|
44
|
+
output: row?.output ?? 0,
|
|
45
|
+
cacheRead: row?.cacheRead ?? 0,
|
|
46
|
+
calls: row?.calls ?? 0,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
db.close();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** 注入 system prompt 的变量(变化慢,可缓存)。 */
|
|
54
|
+
export function buildSystemVars(evolcoreHome, agentAid, peerKey) {
|
|
55
|
+
const agent = _calcTodayCosts(evolcoreHome, agentAid);
|
|
56
|
+
const peer = peerKey ? _calcTodayCosts(evolcoreHome, agentAid, peerKey) : { usd: 0, cny: 0 };
|
|
57
|
+
const totalIn = agent.input + agent.cacheRead;
|
|
58
|
+
const hitRate = totalIn > 0 ? ((agent.cacheRead / totalIn) * 100).toFixed(1) + '%' : '0%';
|
|
59
|
+
const budget = getBudgetStatus(evolcoreHome, agentAid, peerKey);
|
|
60
|
+
return {
|
|
61
|
+
STATS_TODAY_INPUT_TOKENS: String(agent.input),
|
|
62
|
+
STATS_TODAY_OUTPUT_TOKENS: String(agent.output),
|
|
63
|
+
STATS_TODAY_CACHE_READ_TOKENS: String(agent.cacheRead),
|
|
64
|
+
STATS_TODAY_CACHE_HIT_RATE: hitRate,
|
|
65
|
+
STATS_TODAY_COST_USD: agent.usd.toFixed(4),
|
|
66
|
+
STATS_TODAY_COST_CNY: agent.cny.toFixed(4),
|
|
67
|
+
STATS_TODAY_CALL_COUNT: String(agent.calls),
|
|
68
|
+
STATS_PEER_TODAY_COST_USD: peer.usd?.toFixed(4) ?? '0',
|
|
69
|
+
STATS_PEER_TODAY_COST_CNY: peer.cny?.toFixed(4) ?? '0',
|
|
70
|
+
STATS_BUDGET_DAILY_LIMIT_USD: budget.daily_limit_usd >= 0 ? budget.daily_limit_usd.toFixed(2) : 'unlimited',
|
|
71
|
+
STATS_BUDGET_DAILY_USED_USD: budget.daily_used_usd.toFixed(4),
|
|
72
|
+
STATS_BUDGET_DAILY_REMAINING_USD: budget.daily_remaining_usd >= 0 ? budget.daily_remaining_usd.toFixed(2) : 'unlimited',
|
|
73
|
+
STATS_BUDGET_PCT_USED: budget.pct_used.toFixed(1),
|
|
74
|
+
STATS_BUDGET_WARN: String(budget.soft_warn),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** 注入消息提示词的变量(每轮变,不进 system prompt)。 */
|
|
78
|
+
export function buildTurnVars(opts) {
|
|
79
|
+
return {
|
|
80
|
+
SESSION_TURN_COUNT: String(opts.sessionTurnCount),
|
|
81
|
+
SESSION_LLM_CALL_COUNT: String(opts.sessionLlmCallCount),
|
|
82
|
+
STATS_CTX_TOTAL_TOKENS: String(opts.ctxTotalTokens ?? 0),
|
|
83
|
+
STATS_CTX_PCT: opts.ctxPct != null ? opts.ctxPct.toFixed(1) : '0',
|
|
84
|
+
STATS_CTX_SYSTEM_TOKENS: String(opts.ctxSystemTokens ?? 0),
|
|
85
|
+
STATS_CTX_MESSAGES_TOKENS: String(opts.ctxMessagesTokens ?? 0),
|
|
86
|
+
STATS_CTX_TOOLS_TOKENS: String(opts.ctxToolsTokens ?? 0),
|
|
87
|
+
STATS_BUDGET_AUTO_WARN: String(opts.autoBudgetWarn),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/stats/index.ts — Stats 模块公开 API。
|
|
3
|
+
*/
|
|
4
|
+
export { getDb, openReadonlyDb, getStatsDir, getDbPath, closeStatsDb, archiveOldData, listArchivePaths, rebuildDailyRollup } from './db.js';
|
|
5
|
+
export { normalizeUsage } from './normalizer.js';
|
|
6
|
+
export { insertUsageEvent, insertContextBreakdown, insertMessageEvent, insertModelCalls } from './writer.js';
|
|
7
|
+
export { calcCost, resolvePriceRow, resolveCanonicalModel, registerBillingFn } from './billing.js';
|
|
8
|
+
export { queryAggregated, queryTodaySummary, querySessionTurns, queryContextBreakdown, queryTopPeers, queryTopModels, queryMessageAggregated, queryMessageTodaySummary, queryPeerList, querySummary, queryPeerDaily, queryTaskModelCalls, querySessionModelCalls } from './query.js';
|
|
9
|
+
export { getBudgetStatus } from './budget.js';
|
|
10
|
+
export { getRoleBudgetStatus, formatUsageSubjectKey } from './role-budget.js';
|
|
11
|
+
export { buildSystemVars, buildTurnVars } from './eck-vars.js';
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* normalizer.ts — 各模型 raw usage 字段 → 归一化 UsageEvent,同时推断 billing_fn。
|
|
3
|
+
* 按实际有什么字段智能探测,不假设接入方式。
|
|
4
|
+
*/
|
|
5
|
+
export function normalizeUsage(raw, meta) {
|
|
6
|
+
let billing_fn;
|
|
7
|
+
let input_tokens = 0;
|
|
8
|
+
let output_tokens = 0;
|
|
9
|
+
let cache_creation_tokens = 0;
|
|
10
|
+
let cache_read_tokens = 0;
|
|
11
|
+
let cache_hit_tokens;
|
|
12
|
+
let cache_miss_tokens;
|
|
13
|
+
let image_tokens;
|
|
14
|
+
let total_context_tokens;
|
|
15
|
+
if (raw.cache_hit_tokens != null || raw.cache_miss_tokens != null) {
|
|
16
|
+
// DeepSeek 口径
|
|
17
|
+
billing_fn = 'per_token_deepseek_v1';
|
|
18
|
+
output_tokens = raw.output_tokens ?? raw.completion_tokens ?? 0;
|
|
19
|
+
cache_hit_tokens = raw.cache_hit_tokens ?? 0;
|
|
20
|
+
cache_miss_tokens = raw.cache_miss_tokens ?? 0;
|
|
21
|
+
input_tokens = cache_miss_tokens; // 未命中部分才算 input(计费口径)
|
|
22
|
+
// 实际上下文长度 = 命中 + 未命中(total KV cache input)
|
|
23
|
+
total_context_tokens = cache_hit_tokens + cache_miss_tokens;
|
|
24
|
+
}
|
|
25
|
+
else if (raw.promptTokenCount != null || raw.candidatesTokenCount != null) {
|
|
26
|
+
// Gemini 原生
|
|
27
|
+
billing_fn = 'per_token_tiered_v1';
|
|
28
|
+
input_tokens = raw.promptTokenCount ?? 0;
|
|
29
|
+
output_tokens = raw.candidatesTokenCount ?? 0;
|
|
30
|
+
cache_read_tokens = raw.cachedContentTokenCount ?? 0;
|
|
31
|
+
total_context_tokens = raw.totalTokenCount ?? (input_tokens + output_tokens);
|
|
32
|
+
}
|
|
33
|
+
else if (raw.image_tokens != null) {
|
|
34
|
+
// 视觉模型(Qwen-VL 等)
|
|
35
|
+
billing_fn = 'per_token_image_v1';
|
|
36
|
+
input_tokens = raw.input_tokens ?? raw.prompt_tokens ?? 0;
|
|
37
|
+
output_tokens = raw.output_tokens ?? raw.completion_tokens ?? 0;
|
|
38
|
+
image_tokens = raw.image_tokens;
|
|
39
|
+
}
|
|
40
|
+
else if (raw.cache_creation_input_tokens != null || raw.cache_read_input_tokens != null) {
|
|
41
|
+
// Anthropic / Claude 原生
|
|
42
|
+
billing_fn = 'per_token_v1';
|
|
43
|
+
input_tokens = raw.input_tokens ?? 0;
|
|
44
|
+
output_tokens = raw.output_tokens ?? 0;
|
|
45
|
+
cache_creation_tokens = raw.cache_creation_input_tokens ?? 0;
|
|
46
|
+
cache_read_tokens = raw.cache_read_input_tokens ?? 0;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// OpenAI 兼容降级(Kimi / MiniMax / 截断网关等)
|
|
50
|
+
billing_fn = 'per_token_v1';
|
|
51
|
+
input_tokens = raw.input_tokens ?? raw.prompt_tokens ?? 0;
|
|
52
|
+
output_tokens = raw.output_tokens ?? raw.completion_tokens ?? 0;
|
|
53
|
+
cache_read_tokens = raw.prompt_tokens_details?.cached_tokens ?? 0;
|
|
54
|
+
}
|
|
55
|
+
if (total_context_tokens == null) {
|
|
56
|
+
total_context_tokens = input_tokens + output_tokens + cache_creation_tokens + cache_read_tokens || undefined;
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
ts: meta.ts,
|
|
60
|
+
agent_aid: meta.agent_aid,
|
|
61
|
+
peer_key: meta.peer_key,
|
|
62
|
+
usage_subject_key: meta.usage_subject_key,
|
|
63
|
+
role: meta.role,
|
|
64
|
+
peer_type: meta.peer_type,
|
|
65
|
+
session_id: meta.session_id,
|
|
66
|
+
model: meta.model,
|
|
67
|
+
billing_fn,
|
|
68
|
+
input_tokens,
|
|
69
|
+
output_tokens,
|
|
70
|
+
cache_creation_tokens,
|
|
71
|
+
cache_read_tokens,
|
|
72
|
+
cache_hit_tokens,
|
|
73
|
+
cache_miss_tokens,
|
|
74
|
+
image_tokens,
|
|
75
|
+
total_context_tokens,
|
|
76
|
+
turns: meta.turns ?? 1,
|
|
77
|
+
duration_ms: meta.duration_ms,
|
|
78
|
+
context_window_pct: meta.context_window_pct,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* price-resolver.ts — 统一价格解析逻辑,支持官方价格和网关价格。
|
|
3
|
+
* 供 writer.ts 写入时调用,供 rebuild 命令回填历史数据。
|
|
4
|
+
*/
|
|
5
|
+
import { resolvePriceRow, BILLING_FNS } from './billing.js';
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
// 网关价格表缓存(从用户覆盖层 model-prices-gateway.jsonl 读取)
|
|
9
|
+
let _gatewayPriceCache = null;
|
|
10
|
+
let _gatewayPriceCacheTs = 0;
|
|
11
|
+
const PRICE_CACHE_TTL = 5 * 60 * 1000;
|
|
12
|
+
/**
|
|
13
|
+
* 从用户覆盖层读取网关价格表(model-prices-gateway.jsonl)。
|
|
14
|
+
* 这是用户通过 ECWeb "改价" 功能手动设置的网关价格。
|
|
15
|
+
*/
|
|
16
|
+
function loadGatewayPrices(evolcoreHome) {
|
|
17
|
+
const now = Date.now();
|
|
18
|
+
if (_gatewayPriceCache && now - _gatewayPriceCacheTs < PRICE_CACHE_TTL)
|
|
19
|
+
return _gatewayPriceCache;
|
|
20
|
+
const userFile = path.join(evolcoreHome, 'data', 'stats', 'model-prices-gateway.jsonl');
|
|
21
|
+
if (!fs.existsSync(userFile)) {
|
|
22
|
+
_gatewayPriceCache = [];
|
|
23
|
+
_gatewayPriceCacheTs = now;
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const lines = fs.readFileSync(userFile, 'utf-8').split('\n').filter(Boolean);
|
|
28
|
+
_gatewayPriceCache = lines.map(l => JSON.parse(l));
|
|
29
|
+
_gatewayPriceCacheTs = now;
|
|
30
|
+
return _gatewayPriceCache;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
_gatewayPriceCache = [];
|
|
34
|
+
_gatewayPriceCacheTs = now;
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 从用户覆盖层查找网关价格(优先级高于网关接口返回)。
|
|
40
|
+
*/
|
|
41
|
+
function resolveGatewayPriceRow(evolcoreHome, model, ts) {
|
|
42
|
+
const prices = loadGatewayPrices(evolcoreHome);
|
|
43
|
+
const candidates = prices.filter(p => p.model === model && p.effective_from <= ts);
|
|
44
|
+
if (!candidates.length)
|
|
45
|
+
return null;
|
|
46
|
+
return candidates.reduce((a, b) => a.effective_from >= b.effective_from ? a : b);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 从 PriceQuad 构造一个符合 billing.ts 规范的 PriceRecord。
|
|
50
|
+
* 用于将网关接口返回的价格四元组转换为可用于 calcCost() 的格式。
|
|
51
|
+
*/
|
|
52
|
+
function priceQuadToRecord(model, ts, quad, billing_fn, currency = 'USD') {
|
|
53
|
+
return {
|
|
54
|
+
model,
|
|
55
|
+
effective_from: ts,
|
|
56
|
+
billing_fn,
|
|
57
|
+
currency,
|
|
58
|
+
price_input: quad.input ?? 0,
|
|
59
|
+
price_output: quad.output ?? 0,
|
|
60
|
+
price_cache_creation: quad.cache_write ?? 0,
|
|
61
|
+
price_cache_read: quad.cache_read ?? 0,
|
|
62
|
+
price_cache_hit: quad.cache_hit,
|
|
63
|
+
price_cache_miss: quad.cache_miss,
|
|
64
|
+
price_image: quad.image,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* 解析官方价格和网关价格。
|
|
69
|
+
*
|
|
70
|
+
* @param evolcoreHome - EvolCore 根目录
|
|
71
|
+
* @param event - 使用事件(含 model, billing_fn, ts, token 量)
|
|
72
|
+
* @param gatewayPricing - 可选的网关价格缓存(从网关接口获取)
|
|
73
|
+
* @returns 官方价格和网关价格的费用对
|
|
74
|
+
*/
|
|
75
|
+
export function resolvePrices(evolcoreHome, event, gatewayPricing) {
|
|
76
|
+
// ── 1. 官方价格(Official Price)──────────────────────────────────────────
|
|
77
|
+
let officialCost = null;
|
|
78
|
+
// 优先级 1: 网关接口返回的 pricing 字段
|
|
79
|
+
if (gatewayPricing?.official.has(event.model)) {
|
|
80
|
+
const quad = gatewayPricing.official.get(event.model);
|
|
81
|
+
const priceRow = priceQuadToRecord(event.model, event.ts, quad, event.billing_fn);
|
|
82
|
+
const fn = BILLING_FNS[event.billing_fn];
|
|
83
|
+
if (fn) {
|
|
84
|
+
officialCost = fn(event, priceRow);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// 优先级 2: 本地 model-prices.jsonl(包基线 + 用户覆盖层)
|
|
88
|
+
if (!officialCost) {
|
|
89
|
+
const priceRow = resolvePriceRow(evolcoreHome, event.model, event.ts);
|
|
90
|
+
if (priceRow) {
|
|
91
|
+
const fn = BILLING_FNS[event.billing_fn] ?? BILLING_FNS[priceRow.billing_fn];
|
|
92
|
+
if (fn) {
|
|
93
|
+
officialCost = fn(event, priceRow);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// ── 2. 网关价格(Gateway Effective Price)────────────────────────────────
|
|
98
|
+
let gatewayCost = null;
|
|
99
|
+
// 优先级 1: 用户手动设置的网关价格(model-prices-gateway.jsonl)
|
|
100
|
+
const gatewayPriceRow = resolveGatewayPriceRow(evolcoreHome, event.model, event.ts);
|
|
101
|
+
if (gatewayPriceRow) {
|
|
102
|
+
const fn = BILLING_FNS[event.billing_fn] ?? BILLING_FNS[gatewayPriceRow.billing_fn];
|
|
103
|
+
if (fn) {
|
|
104
|
+
gatewayCost = fn(event, gatewayPriceRow);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// 优先级 2: 网关接口返回的 effective_pricing 字段
|
|
108
|
+
if (!gatewayCost && gatewayPricing?.gateway.has(event.model)) {
|
|
109
|
+
const quad = gatewayPricing.gateway.get(event.model);
|
|
110
|
+
const priceRow = priceQuadToRecord(event.model, event.ts, quad, event.billing_fn);
|
|
111
|
+
const fn = BILLING_FNS[event.billing_fn];
|
|
112
|
+
if (fn) {
|
|
113
|
+
gatewayCost = fn(event, priceRow);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// 优先级 3: 回退到官方价格(实际收费 = 成本价)
|
|
117
|
+
if (!gatewayCost) {
|
|
118
|
+
gatewayCost = officialCost;
|
|
119
|
+
}
|
|
120
|
+
// ── 3. 转换为统一格式 ───────────────────────────────────────────────────
|
|
121
|
+
// 计费函数按价格行币种只产出一种货币(接口价恒为 USD)。用网关汇率(usd_to_cny,缺省 7)
|
|
122
|
+
// 补齐另一种货币:有 usd 缺 cny → cny=usd*rate;有 cny 缺 usd → usd=cny/rate。
|
|
123
|
+
const rate = (typeof gatewayPricing?.usdToCny === 'number' && gatewayPricing.usdToCny > 0)
|
|
124
|
+
? gatewayPricing.usdToCny : 7;
|
|
125
|
+
const toPair = (c) => {
|
|
126
|
+
if (!c)
|
|
127
|
+
return null;
|
|
128
|
+
const hasUsd = typeof c.usd === 'number';
|
|
129
|
+
const hasCny = typeof c.cny === 'number';
|
|
130
|
+
const usd = hasUsd ? c.usd : (hasCny ? c.cny / rate : 0);
|
|
131
|
+
const cny = hasCny ? c.cny : (hasUsd ? c.usd * rate : 0);
|
|
132
|
+
return { usd, cny };
|
|
133
|
+
};
|
|
134
|
+
return {
|
|
135
|
+
official: toPair(officialCost),
|
|
136
|
+
gateway: toPair(gatewayCost),
|
|
137
|
+
};
|
|
138
|
+
}
|