@octopi-agent/engine 0.98.4 → 0.121.10
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/dist/builtin-model-info.js +28 -7
- package/dist/config-admin/agent-presets.d.ts +32 -0
- package/dist/config-admin/agent-presets.js +133 -0
- package/dist/config-admin/discover.d.ts +33 -4
- package/dist/config-admin/discover.js +101 -6
- package/dist/config-admin/env.d.ts +11 -0
- package/dist/config-admin/env.js +41 -0
- package/dist/config-admin/error-copy.d.ts +106 -0
- package/dist/config-admin/error-copy.js +287 -0
- package/dist/config-admin/i18n.d.ts +21 -0
- package/dist/config-admin/i18n.js +19 -0
- package/dist/config-admin/index.d.ts +6 -2
- package/dist/config-admin/index.js +3 -1
- package/dist/config-admin/model-presets.d.ts +14 -0
- package/dist/config-admin/model-presets.js +595 -70
- package/dist/config-admin/onboarding-apply.d.ts +71 -0
- package/dist/config-admin/onboarding-apply.js +235 -0
- package/dist/config-admin/onboarding.d.ts +154 -0
- package/dist/config-admin/onboarding.js +270 -0
- package/dist/config-admin/redact.d.ts +42 -9
- package/dist/config-admin/redact.js +69 -5
- package/dist/config-schema/agent.d.ts +40 -6
- package/dist/config-schema/agent.js +44 -6
- package/dist/config-schema/browser.d.ts +62 -0
- package/dist/config-schema/browser.js +89 -0
- package/dist/config-schema/context.d.ts +12 -37
- package/dist/config-schema/context.js +10 -31
- package/dist/config-schema/engine.d.ts +140 -164
- package/dist/config-schema/engine.js +10 -7
- package/dist/config-schema/governance.d.ts +20 -2
- package/dist/config-schema/governance.js +18 -3
- package/dist/config-schema/models.d.ts +10 -12
- package/dist/config-schema/models.js +16 -3
- package/dist/config-schema/run.d.ts +6 -2
- package/dist/config-schema/run.js +12 -2
- package/dist/config-schema/shared.d.ts +1 -3
- package/dist/config-schema/shared.js +4 -4
- package/dist/config-schema/substrate.d.ts +19 -88
- package/dist/config-schema/substrate.js +30 -107
- package/dist/config.d.ts +161 -123
- package/dist/config.js +109 -7
- package/dist/file-mode.d.ts +67 -0
- package/dist/file-mode.js +182 -0
- package/dist/fork-child.d.ts +82 -0
- package/dist/fork-child.js +123 -0
- package/dist/harness/activation/delegated-run.d.ts +42 -0
- package/dist/harness/activation/delegated-run.js +71 -0
- package/dist/harness/activation/dispatcher.js +11 -0
- package/dist/harness/activation/inbox.d.ts +3 -3
- package/dist/harness/activation/inbox.js +3 -3
- package/dist/harness/activation/index.d.ts +1 -0
- package/dist/harness/activation/run-backend.d.ts +92 -0
- package/dist/harness/activation/run-backend.js +16 -0
- package/dist/harness/activation/run-protocol.d.ts +392 -0
- package/dist/harness/activation/run-protocol.js +19 -0
- package/dist/harness/activation/runtime.d.ts +33 -0
- package/dist/harness/activation/runtime.js +130 -31
- package/dist/harness/activation/types.d.ts +20 -3
- package/dist/harness/agent/builder.d.ts +36 -3
- package/dist/harness/agent/builder.js +206 -46
- package/dist/harness/agent/config-bridge.d.ts +10 -1
- package/dist/harness/agent/config-bridge.js +24 -5
- package/dist/harness/agent/config-integrity.js +19 -2
- package/dist/harness/agent/registered-tool-adapter.d.ts +27 -0
- package/dist/harness/agent/registered-tool-adapter.js +67 -0
- package/dist/harness/agent/wire-agent-builder.d.ts +13 -3
- package/dist/harness/agent/wire-agent-builder.js +35 -9
- package/dist/harness/capabilities/document/backends/plain.js +2 -1
- package/dist/harness/capabilities/document/factory.d.ts +5 -0
- package/dist/harness/capabilities/document/factory.js +1 -0
- package/dist/harness/capabilities/document/legacy/factory.js +3 -2
- package/dist/harness/capabilities/document/port.js +6 -3
- package/dist/harness/capabilities/document/types.d.ts +5 -0
- package/dist/harness/collaboration/autonomous-subsystem/audit/reader.d.ts +17 -0
- package/dist/harness/collaboration/autonomous-subsystem/audit/reader.js +66 -36
- package/dist/harness/collaboration/autonomous-subsystem/audit/writer.d.ts +6 -2
- package/dist/harness/collaboration/autonomous-subsystem/audit/writer.js +27 -7
- package/dist/harness/collaboration/autonomous-subsystem/boundary/validator.js +13 -0
- package/dist/harness/collaboration/autonomous-subsystem/loader.js +2 -1
- package/dist/harness/collaboration/autonomous-subsystem/runtime.d.ts +11 -0
- package/dist/harness/collaboration/autonomous-subsystem/runtime.js +17 -0
- package/dist/harness/collaboration/autonomous-subsystem/sense/engine.d.ts +14 -4
- package/dist/harness/collaboration/autonomous-subsystem/sense/engine.js +50 -41
- package/dist/harness/collaboration/autonomous-subsystem/sense/input-builder.js +4 -4
- package/dist/harness/collaboration/autonomous-subsystem/sense/metrics.d.ts +2 -2
- package/dist/harness/collaboration/autonomous-subsystem/sense/metrics.js +2 -2
- package/dist/harness/collaboration/autonomous-subsystem/signal/bus.d.ts +6 -0
- package/dist/harness/collaboration/autonomous-subsystem/signal/bus.js +25 -4
- package/dist/harness/collaboration/autonomous-subsystem/think/executor.js +4 -1
- package/dist/harness/collaboration/autonomous-subsystem/types.d.ts +7 -3
- package/dist/harness/collaboration/delegation/child-run.d.ts +10 -1
- package/dist/harness/collaboration/delegation/child-run.js +38 -6
- package/dist/harness/collaboration/delegation/index.d.ts +1 -1
- package/dist/harness/collaboration/delegation/index.js +1 -1
- package/dist/harness/collaboration/delegation/notifier.d.ts +6 -1
- package/dist/harness/collaboration/delegation/notifier.js +8 -1
- package/dist/harness/collaboration/delegation/registry-types.d.ts +2 -0
- package/dist/harness/collaboration/delegation/service.d.ts +11 -4
- package/dist/harness/collaboration/delegation/service.js +35 -13
- package/dist/harness/collaboration/delegation/store.d.ts +4 -0
- package/dist/harness/collaboration/delegation/store.js +3 -1
- package/dist/harness/collaboration/delegation/types.d.ts +34 -2
- package/dist/harness/collaboration/delegation/types.js +22 -0
- package/dist/harness/collaboration/delegation/wiring.d.ts +2 -0
- package/dist/harness/collaboration/delegation/wiring.js +12 -1
- package/dist/harness/context/budget-allocator.d.ts +26 -18
- package/dist/harness/context/budget-allocator.js +67 -32
- package/dist/harness/context/capabilities/compact/engine.d.ts +44 -11
- package/dist/harness/context/capabilities/compact/engine.js +296 -160
- package/dist/harness/context/capabilities/compact/handover.d.ts +5 -3
- package/dist/harness/context/capabilities/compact/handover.js +7 -5
- package/dist/harness/context/capabilities/compact/index.d.ts +7 -2
- package/dist/harness/context/capabilities/compact/index.js +5 -1
- package/dist/harness/context/capabilities/compact/mechanical.d.ts +130 -0
- package/dist/harness/context/capabilities/compact/mechanical.js +268 -0
- package/dist/harness/context/capabilities/compact/messages.d.ts +60 -0
- package/dist/harness/context/capabilities/compact/messages.js +91 -0
- package/dist/harness/context/capabilities/compact/types.d.ts +134 -19
- package/dist/harness/context/capabilities/compact/types.js +11 -1
- package/dist/harness/context/capabilities/compact/view-split.d.ts +36 -0
- package/dist/harness/context/capabilities/compact/view-split.js +72 -0
- package/dist/harness/context/capabilities/summary/gate.d.ts +19 -2
- package/dist/harness/context/capabilities/summary/gate.js +53 -6
- package/dist/harness/context/capabilities/summary/port.js +0 -1
- package/dist/harness/context/capabilities/summary/registry.js +20 -0
- package/dist/harness/context/capabilities/summary/resolver.d.ts +1 -1
- package/dist/harness/context/capabilities/summary/resolver.js +1 -6
- package/dist/harness/context/capabilities/summary/tool-binding.d.ts +1 -1
- package/dist/harness/context/capabilities/summary/tool-binding.js +16 -12
- package/dist/harness/context/capabilities/summary/tool-bindings.d.ts +13 -0
- package/dist/harness/context/capabilities/summary/tool-bindings.js +22 -0
- package/dist/harness/context/capabilities/summary/types.d.ts +19 -6
- package/dist/harness/context/constitution/default-agents.md +85 -0
- package/dist/harness/context/context-size-ledger.d.ts +143 -0
- package/dist/harness/context/context-size-ledger.js +243 -0
- package/dist/harness/context/default-context-engine.d.ts +290 -120
- package/dist/harness/context/default-context-engine.js +1555 -764
- package/dist/harness/context/index.d.ts +3 -2
- package/dist/harness/context/index.js +2 -1
- package/dist/harness/context/layer-health.d.ts +4 -0
- package/dist/harness/context/layer-health.js +4 -0
- package/dist/harness/context/layer-types.d.ts +72 -0
- package/dist/harness/context/layer-types.js +87 -2
- package/dist/harness/context/layers.d.ts +17 -15
- package/dist/harness/context/layers.js +54 -17
- package/dist/harness/context/message-selector.d.ts +10 -8
- package/dist/harness/context/message-selector.js +49 -27
- package/dist/harness/context/prefix-fingerprint.d.ts +46 -0
- package/dist/harness/context/prefix-fingerprint.js +52 -0
- package/dist/harness/context/summarize.d.ts +2 -2
- package/dist/harness/context/summarize.js +27 -2
- package/dist/harness/context/system-prompt-assembler.d.ts +4 -0
- package/dist/harness/context/system-prompt-assembler.js +7 -3
- package/dist/harness/context/token-constants.d.ts +8 -6
- package/dist/harness/context/token-constants.js +8 -7
- package/dist/harness/context/token-estimate-fns.d.ts +37 -27
- package/dist/harness/context/token-estimate-fns.js +85 -66
- package/dist/harness/context/token-estimator.d.ts +27 -12
- package/dist/harness/context/token-estimator.js +120 -66
- package/dist/harness/context/token-segments.d.ts +88 -0
- package/dist/harness/context/token-segments.js +103 -0
- package/dist/harness/context/tool-result-aging.d.ts +109 -16
- package/dist/harness/context/tool-result-aging.js +220 -55
- package/dist/harness/context/tool-result-body.js +2 -0
- package/dist/harness/context/transform/anchors.d.ts +66 -0
- package/dist/harness/context/transform/anchors.js +125 -0
- package/dist/harness/context/transform/fold.d.ts +56 -0
- package/dist/harness/context/transform/fold.js +135 -0
- package/dist/harness/context/transform/migrate.d.ts +41 -0
- package/dist/harness/context/transform/migrate.js +69 -0
- package/dist/harness/context/transform/types.d.ts +129 -0
- package/dist/harness/context/transform/types.js +19 -0
- package/dist/harness/context/turn-envelope.d.ts +6 -3
- package/dist/harness/context/types.d.ts +196 -119
- package/dist/harness/context/types.js +25 -1
- package/dist/harness/extension/execution-environment/effect-policy.d.ts +4 -3
- package/dist/harness/extension/execution-environment/effect-policy.js +2 -2
- package/dist/harness/extension/execution-environment/ops-audit.d.ts +1 -1
- package/dist/harness/extension/execution-environment/privileged-exec.js +4 -1
- package/dist/harness/extension/execution-environment/resource-limits.d.ts +1 -1
- package/dist/harness/extension/execution-environment/resource-limits.js +1 -1
- package/dist/harness/extension/execution-environment/workspace.js +1 -1
- package/dist/harness/extension/plugin-ecosystem/channel-types.d.ts +28 -2
- package/dist/harness/extension/plugin-ecosystem/channel-types.js +11 -2
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.d.ts +12 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.js +25 -0
- package/dist/harness/extension/plugin-ecosystem/commands/builtin.js +28 -1
- package/dist/harness/extension/plugin-ecosystem/commands/index.d.ts +3 -1
- package/dist/harness/extension/plugin-ecosystem/commands/index.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/commands/plugin-bridge.d.ts +12 -1
- package/dist/harness/extension/plugin-ecosystem/commands/plugin-bridge.js +14 -4
- package/dist/harness/extension/plugin-ecosystem/commands/router.d.ts +5 -4
- package/dist/harness/extension/plugin-ecosystem/commands/router.js +23 -14
- package/dist/harness/extension/plugin-ecosystem/commands/types.d.ts +8 -1
- package/dist/harness/extension/plugin-ecosystem/commands/types.js +2 -1
- package/dist/harness/extension/plugin-ecosystem/hooks/adapter.d.ts +54 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/adapter.js +397 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/compaction-integration.d.ts +16 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/compaction-integration.js +66 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/index.d.ts +4 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/index.js +2 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/iteration-snapshot.d.ts +11 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/iteration-snapshot.js +54 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/journal.d.ts +9 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/journal.js +22 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/policy-error.d.ts +12 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/policy-error.js +19 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/run-integration.d.ts +36 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/run-integration.js +338 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/services.d.ts +24 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/services.js +139 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/types.d.ts +229 -0
- package/dist/harness/extension/plugin-ecosystem/hooks/types.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/index.d.ts +1 -0
- package/dist/harness/extension/plugin-ecosystem/index.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/api.d.ts +223 -222
- package/dist/harness/extension/plugin-ecosystem/plugins/api.js +218 -203
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-instances.d.ts +95 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-instances.js +170 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-kinds.d.ts +178 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-kinds.js +314 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.d.ts +78 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/contribution-registry.js +80 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/engine-compatibility.d.ts +4 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/engine-compatibility.js +18 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/entry.d.ts +7 -85
- package/dist/harness/extension/plugin-ecosystem/plugins/entry.js +8 -50
- package/dist/harness/extension/plugin-ecosystem/plugins/index.d.ts +6 -23
- package/dist/harness/extension/plugin-ecosystem/plugins/index.js +3 -23
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.d.ts +57 -114
- package/dist/harness/extension/plugin-ecosystem/plugins/loader.js +279 -282
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.d.ts +46 -484
- package/dist/harness/extension/plugin-ecosystem/plugins/manager.js +123 -345
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.d.ts +50 -223
- package/dist/harness/extension/plugin-ecosystem/plugins/manifest.js +144 -20
- package/dist/harness/extension/plugin-ecosystem/plugins/staging.d.ts +17 -0
- package/dist/harness/extension/plugin-ecosystem/plugins/staging.js +15 -0
- package/dist/harness/extension/plugin-ecosystem/skills/manager.d.ts +6 -1
- package/dist/harness/extension/plugin-ecosystem/skills/manager.js +17 -1
- package/dist/harness/extension/plugin-ecosystem/tools/ask-user.d.ts +3 -0
- package/dist/harness/extension/plugin-ecosystem/tools/ask-user.js +46 -21
- package/dist/harness/extension/plugin-ecosystem/tools/browser-types.d.ts +333 -0
- package/dist/harness/extension/plugin-ecosystem/tools/browser-types.js +16 -0
- package/dist/harness/extension/plugin-ecosystem/tools/browser.d.ts +64 -0
- package/dist/harness/extension/plugin-ecosystem/tools/browser.js +692 -0
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.d.ts +0 -37
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +108 -4
- package/dist/harness/extension/plugin-ecosystem/tools/delegation.js +36 -8
- package/dist/harness/extension/plugin-ecosystem/tools/file-edit.js +34 -0
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.d.ts +0 -6
- package/dist/harness/extension/plugin-ecosystem/tools/file-search.js +57 -4
- package/dist/harness/extension/plugin-ecosystem/tools/git-tools.js +81 -13
- package/dist/harness/extension/plugin-ecosystem/tools/http.js +5 -0
- package/dist/harness/extension/plugin-ecosystem/tools/knowledge.js +13 -7
- package/dist/harness/extension/plugin-ecosystem/tools/package-tools.js +126 -14
- package/dist/harness/extension/plugin-ecosystem/tools/platform.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/tools/platform.js +44 -5
- package/dist/harness/extension/plugin-ecosystem/tools/plugin-tool.d.ts +11 -0
- package/dist/harness/extension/plugin-ecosystem/tools/plugin-tool.js +39 -0
- package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.d.ts +1 -1
- package/dist/harness/extension/plugin-ecosystem/tools/tool-bus.js +19 -24
- package/dist/harness/extension/plugin-ecosystem/tools/tool-set.d.ts +7 -0
- package/dist/harness/extension/plugin-ecosystem/tools/tool-set.js +4 -0
- package/dist/harness/governance/accounting/usage-ledger.d.ts +13 -1
- package/dist/harness/governance/accounting/usage-ledger.js +23 -1
- package/dist/harness/governance/credentials/db.d.ts +9 -0
- package/dist/harness/governance/credentials/db.js +122 -31
- package/dist/harness/governance/credentials/store.d.ts +68 -7
- package/dist/harness/governance/credentials/store.js +177 -64
- package/dist/harness/governance/credentials/types.d.ts +13 -2
- package/dist/harness/governance/human-in-the-loop/decision-cache.d.ts +3 -3
- package/dist/harness/governance/human-in-the-loop/decision-cache.js +2 -2
- package/dist/harness/governance/index.d.ts +1 -0
- package/dist/harness/governance/index.js +1 -0
- package/dist/harness/governance/registry/db.d.ts +32 -0
- package/dist/harness/governance/registry/db.js +142 -0
- package/dist/harness/governance/registry/index.d.ts +9 -0
- package/dist/harness/governance/registry/index.js +9 -0
- package/dist/harness/governance/registry/service.d.ts +381 -0
- package/dist/harness/governance/registry/service.js +825 -0
- package/dist/harness/governance/registry/types.d.ts +77 -0
- package/dist/harness/governance/registry/types.js +7 -0
- package/dist/harness/governance/security/capability-enforcer.js +1 -2
- package/dist/harness/governance/security/default-security-guard.d.ts +3 -0
- package/dist/harness/governance/security/default-security-guard.js +12 -1
- package/dist/harness/governance/security/risk-evaluator.d.ts +13 -3
- package/dist/harness/governance/security/risk-evaluator.js +307 -21
- package/dist/harness/governance/security/shell-parser-treesitter.js +12 -4
- package/dist/harness/governance/session-acl/tool-rights.d.ts +1 -0
- package/dist/harness/index.d.ts +17 -15
- package/dist/harness/index.js +9 -9
- package/dist/harness/knowledge/boot-config.d.ts +57 -0
- package/dist/harness/knowledge/boot-config.js +52 -0
- package/dist/harness/knowledge/catalog-types.d.ts +2 -0
- package/dist/harness/knowledge/client.d.ts +77 -9
- package/dist/harness/knowledge/client.js +95 -24
- package/dist/harness/knowledge/db.d.ts +16 -0
- package/dist/harness/knowledge/db.js +112 -41
- package/dist/harness/knowledge/describe-sample.d.ts +23 -0
- package/dist/harness/knowledge/describe-sample.js +58 -0
- package/dist/harness/knowledge/describe.d.ts +8 -0
- package/dist/harness/knowledge/describe.js +18 -2
- package/dist/harness/knowledge/engine-thread.js +11 -7
- package/dist/harness/knowledge/grounding.js +7 -2
- package/dist/harness/knowledge/http-app.d.ts +15 -0
- package/dist/harness/knowledge/http-app.js +117 -25
- package/dist/harness/knowledge/http-bridge.d.ts +15 -0
- package/dist/harness/knowledge/http-bridge.js +19 -0
- package/dist/harness/knowledge/http-params.d.ts +18 -0
- package/dist/harness/knowledge/http-params.js +24 -0
- package/dist/harness/knowledge/index.d.ts +2 -0
- package/dist/harness/knowledge/index.js +1 -0
- package/dist/harness/knowledge/knowledge-serve-child.js +5 -3
- package/dist/harness/knowledge/purge.js +5 -2
- package/dist/harness/knowledge/query-service.d.ts +25 -4
- package/dist/harness/knowledge/query-service.js +79 -43
- package/dist/harness/knowledge/query-worker-client.d.ts +14 -2
- package/dist/harness/knowledge/query-worker-client.js +8 -3
- package/dist/harness/knowledge/query-worker.js +9 -5
- package/dist/harness/knowledge/read-http.d.ts +2 -0
- package/dist/harness/knowledge/read-http.js +14 -3
- package/dist/harness/knowledge/retriever.d.ts +1 -1
- package/dist/harness/knowledge/retriever.js +1 -2
- package/dist/harness/knowledge/serve.d.ts +14 -8
- package/dist/harness/knowledge/serve.js +14 -6
- package/dist/harness/knowledge/source-store.d.ts +101 -5
- package/dist/harness/knowledge/source-store.js +323 -74
- package/dist/harness/knowledge/start-service-process.d.ts +41 -14
- package/dist/harness/knowledge/start-service-process.js +67 -96
- package/dist/harness/knowledge/tenant-gate.d.ts +81 -0
- package/dist/harness/knowledge/tenant-gate.js +167 -0
- package/dist/harness/knowledge/types.d.ts +7 -0
- package/dist/harness/knowledge/writer-service.d.ts +55 -11
- package/dist/harness/knowledge/writer-service.js +200 -30
- package/dist/harness/knowledge/writer-worker-client.d.ts +19 -11
- package/dist/harness/knowledge/writer-worker-client.js +40 -5
- package/dist/harness/knowledge/writer-worker.js +20 -5
- package/dist/harness/memory/backfill-trigger.d.ts +10 -3
- package/dist/harness/memory/backfill-trigger.js +19 -19
- package/dist/harness/memory/cognition-seeds.d.ts +45 -0
- package/dist/harness/memory/cognition-seeds.js +67 -0
- package/dist/harness/memory/cognition.d.ts +12 -3
- package/dist/harness/memory/cognition.js +55 -35
- package/dist/harness/memory/conceptualizer.d.ts +11 -1
- package/dist/harness/memory/conceptualizer.js +10 -1
- package/dist/harness/memory/context-slice.d.ts +42 -0
- package/dist/harness/memory/context-slice.js +197 -0
- package/dist/harness/memory/health-probe.d.ts +8 -2
- package/dist/harness/memory/health-probe.js +10 -13
- package/dist/harness/memory/index.d.ts +5 -1
- package/dist/harness/memory/index.js +3 -1
- package/dist/harness/memory/sqlite/agent-db.d.ts +1 -0
- package/dist/harness/memory/sqlite/agent-db.js +9 -8
- package/dist/harness/memory/sqlite/cognition-store.d.ts +26 -3
- package/dist/harness/memory/sqlite/cognition-store.js +103 -45
- package/dist/harness/memory/sqlite/memory-store.js +4 -0
- package/dist/harness/memory/sqlite/sqlite-vec.d.ts +24 -1
- package/dist/harness/memory/sqlite/sqlite-vec.js +37 -1
- package/dist/harness/memory/sqlite/wisdom-store.js +35 -10
- package/dist/harness/memory/types.d.ts +24 -6
- package/dist/harness/memory/wisdom-formation.d.ts +14 -3
- package/dist/harness/memory/wisdom-formation.js +85 -22
- package/dist/harness/memory/wisdom-gates.d.ts +14 -0
- package/dist/harness/memory/wisdom-gates.js +59 -19
- package/dist/harness/memory/wisdom.js +9 -0
- package/dist/harness/observability/observer/hub.d.ts +10 -0
- package/dist/harness/observability/observer/hub.js +120 -17
- package/dist/harness/observability/observer/types.d.ts +10 -6
- package/dist/harness/run/agent/agent.d.ts +40 -21
- package/dist/harness/run/agent/agent.js +67 -13
- package/dist/harness/run/agent/index.d.ts +1 -1
- package/dist/harness/run/budget/budget.d.ts +28 -10
- package/dist/harness/run/budget/budget.js +38 -15
- package/dist/harness/run/concurrency/tool-validator.js +4 -3
- package/dist/harness/run/model/index.d.ts +3 -1
- package/dist/harness/run/model/index.js +2 -1
- package/dist/harness/run/model/resolver.d.ts +4 -1
- package/dist/harness/run/model/resolver.js +36 -2
- package/dist/harness/run/model/run-scope.d.ts +2 -0
- package/dist/harness/run/model/run-scope.js +7 -0
- package/dist/harness/run/model/types.d.ts +22 -5
- package/dist/harness/run/model/types.js +3 -2
- package/dist/harness/run/model/window-truth.d.ts +37 -0
- package/dist/harness/run/model/window-truth.js +39 -0
- package/dist/harness/run/reliability/context-length-feedback.d.ts +77 -0
- package/dist/harness/run/reliability/context-length-feedback.js +128 -0
- package/dist/harness/run/reliability/harness-events.d.ts +3 -3
- package/dist/harness/run/reliability/model-error-policy.js +4 -0
- package/dist/harness/run/reliability/run-agent.d.ts +32 -4
- package/dist/harness/run/reliability/run-agent.js +216 -63
- package/dist/harness/run/reliability/run-metrics-collector.d.ts +19 -8
- package/dist/harness/run/reliability/run-metrics-collector.js +38 -23
- package/dist/harness/run/run-guard/default-run-guard.js +8 -8
- package/dist/harness/run/run-host.d.ts +302 -0
- package/dist/harness/run/run-host.js +978 -0
- package/dist/harness/run/run-scope.d.ts +48 -4
- package/dist/harness/run/run-scope.js +19 -1
- package/dist/harness/run/runner.d.ts +181 -39
- package/dist/harness/run/runner.js +654 -119
- package/dist/harness/session/attachments/index.d.ts +1 -0
- package/dist/harness/session/attachments/index.js +1 -0
- package/dist/harness/session/attachments/inject.d.ts +21 -10
- package/dist/harness/session/attachments/inject.js +76 -24
- package/dist/harness/session/attachments/recall-index.d.ts +70 -0
- package/dist/harness/session/attachments/recall-index.js +219 -0
- package/dist/harness/session/attachments/service.d.ts +21 -0
- package/dist/harness/session/attachments/service.js +258 -139
- package/dist/harness/session/attachments/types.d.ts +6 -0
- package/dist/harness/session/in-memory-store.js +13 -1
- package/dist/harness/session/schedules/service.d.ts +10 -2
- package/dist/harness/session/schedules/service.js +13 -2
- package/dist/harness/session/tasks/tools.js +12 -0
- package/dist/harness/session/transform-store.d.ts +136 -0
- package/dist/harness/session/transform-store.js +302 -0
- package/dist/harness/session/types.d.ts +11 -15
- package/dist/harness/shared/events/agent-event-map.d.ts +75 -13
- package/dist/harness/shared/events/agent-event-map.js +3 -2
- package/dist/harness/shared/events/scenario-events.d.ts +6 -6
- package/dist/harness/shared/types/agent-definition.d.ts +7 -2
- package/dist/index.d.ts +16 -18
- package/dist/index.js +9 -10
- package/dist/integration/agent-runtime/channel-message-source.d.ts +3 -1
- package/dist/integration/agent-runtime/channel-message-source.js +4 -3
- package/dist/integration/browser/camoufox.d.ts +40 -0
- package/dist/integration/browser/camoufox.js +80 -0
- package/dist/integration/browser/index.d.ts +91 -0
- package/dist/integration/browser/index.js +110 -0
- package/dist/integration/browser/install.d.ts +47 -0
- package/dist/integration/browser/install.js +214 -0
- package/dist/integration/browser/playwright.d.ts +224 -0
- package/dist/integration/browser/playwright.js +925 -0
- package/dist/integration/browser/service.d.ts +41 -0
- package/dist/integration/browser/service.js +181 -0
- package/dist/integration/browser/stealth.d.ts +42 -0
- package/dist/integration/browser/stealth.js +104 -0
- package/dist/integration/observability/metrics.d.ts +6 -6
- package/dist/integration/observability/metrics.js +13 -13
- package/dist/integration/observability/observer-bridge.js +2 -2
- package/dist/integration/observability/trace-collector.d.ts +5 -5
- package/dist/integration/observability/trace-collector.js +34 -35
- package/dist/integration/observability/trace-events.d.ts +3 -3
- package/dist/integration/observability/trace-events.js +3 -3
- package/dist/integration/providers/anthropic-message-normalize.d.ts +20 -0
- package/dist/integration/providers/anthropic-message-normalize.js +48 -0
- package/dist/integration/providers/anthropic.js +41 -3
- package/dist/integration/providers/fetch-timeout.d.ts +17 -0
- package/dist/integration/providers/fetch-timeout.js +24 -0
- package/dist/integration/providers/openai.d.ts +8 -0
- package/dist/integration/providers/openai.js +48 -2
- package/dist/integration/providers/usage.d.ts +1 -1
- package/dist/integration/providers/wire-dump-diff.js +2 -1
- package/dist/integration/providers/wire-dump.d.ts +57 -3
- package/dist/integration/providers/wire-dump.js +119 -6
- package/dist/integration/storage/archive-manager.d.ts +20 -2
- package/dist/integration/storage/archive-manager.js +34 -8
- package/dist/integration/storage/atomic-tmp.d.ts +12 -0
- package/dist/integration/storage/atomic-tmp.js +36 -0
- package/dist/integration/storage/jsonl.d.ts +10 -1
- package/dist/integration/storage/jsonl.js +155 -54
- package/dist/integration/storage/session-index.d.ts +5 -0
- package/dist/integration/storage/session-index.js +25 -4
- package/dist/integration/tools/assemble.d.ts +50 -0
- package/dist/integration/tools/assemble.js +137 -0
- package/dist/paths.d.ts +18 -0
- package/dist/paths.js +36 -0
- package/dist/subsystems/cognition-steward/conceptualize/config.yaml +3 -1
- package/dist/subsystems/cognition-steward/conceptualize/handler.js +74 -5
- package/dist/subsystems/cognition-steward/consolidate/SUBSYSTEM.md +3 -2
- package/dist/subsystems/cognition-steward/consolidate/config.yaml +11 -2
- package/dist/subsystems/cognition-steward/consolidate/handler.d.ts +6 -3
- package/dist/subsystems/cognition-steward/consolidate/handler.js +266 -28
- package/dist/subsystems/memory-steward/backfill/handler.js +3 -2
- package/dist/subsystems/memory-steward/govern/config.yaml +6 -1
- package/dist/subsystems/wisdom-steward/attribute/config.yaml +5 -1
- package/dist/subsystems/wisdom-steward/formation/SUBSYSTEM.md +8 -1
- package/dist/subsystems/wisdom-steward/formation/config.yaml +4 -0
- package/dist/subsystems/wisdom-steward/formation/handler.js +27 -5
- package/dist/subsystems/wisdom-steward/govern/config.yaml +6 -1
- package/package.json +14 -15
- package/dist/harness/context/hybrid-compressor.d.ts +0 -41
- package/dist/harness/context/hybrid-compressor.js +0 -159
- package/dist/harness/context/llm-summarizer.d.ts +0 -46
- package/dist/harness/context/llm-summarizer.js +0 -249
- package/dist/harness/context/smart-router.d.ts +0 -75
- package/dist/harness/context/smart-router.js +0 -118
- package/dist/harness/context/truncate-compressor.d.ts +0 -28
- package/dist/harness/context/truncate-compressor.js +0 -107
- package/dist/harness/extension/plugin-ecosystem/plugins/capability.d.ts +0 -97
- package/dist/harness/extension/plugin-ecosystem/plugins/capability.js +0 -155
- package/dist/harness/extension/plugin-ecosystem/plugins/hooks.d.ts +0 -7
- package/dist/harness/extension/plugin-ecosystem/plugins/hooks.js +0 -7
- package/dist/harness/extension/plugin-ecosystem/plugins/lifecycle.d.ts +0 -86
- package/dist/harness/extension/plugin-ecosystem/plugins/lifecycle.js +0 -139
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 缓存前缀指纹(可观测护栏)
|
|
3
|
+
*
|
|
4
|
+
* 事故背景(2026-11-12):摘要请求的前缀与主请求"看起来同形",实际整包 tools 缺失,
|
|
5
|
+
* provider 只回报 `inputCachedTokens: 0`——从读数完全看不出**是谁变了**,只能靠人工
|
|
6
|
+
* 逐字节比对 wire dump 才定位。本模块把"前缀是否同形"变成可判定、可打印的事实。
|
|
7
|
+
*
|
|
8
|
+
* 指纹刻意只覆盖**前缀**(tools → system → messages 的保护段),不含尾部指令:
|
|
9
|
+
* 摘要请求本来就要在尾部追加一条指令,那一段不可能也不应该相同。
|
|
10
|
+
*
|
|
11
|
+
* @module harness/context/prefix-fingerprint
|
|
12
|
+
*/
|
|
13
|
+
import type { RequestView } from '@octopi-agent/core/loop/types.js';
|
|
14
|
+
/**
|
|
15
|
+
* 前缀指纹:三项独立哈希 + 前缀消息条数。
|
|
16
|
+
*
|
|
17
|
+
* 三项分开而不用一个大哈希——差异定位需要知道**是 tools 还是 system 还是 messages 变了**。
|
|
18
|
+
*/
|
|
19
|
+
export interface PrefixFingerprint {
|
|
20
|
+
/** 工具面哈希(provider 格式 JSON 的 sha1 前 16 位) */
|
|
21
|
+
toolsHash: string;
|
|
22
|
+
/** 托管 system 文本哈希(messages 中第一条 system) */
|
|
23
|
+
systemHash: string;
|
|
24
|
+
/** 前缀消息(不含尾部指令)整体哈希 */
|
|
25
|
+
prefixHash: string;
|
|
26
|
+
/** 参与前缀的消息条数 */
|
|
27
|
+
prefixCount: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 计算请求视图的前缀指纹
|
|
31
|
+
*
|
|
32
|
+
* @param view - 请求视图
|
|
33
|
+
* @param input.tailLength - 尾部追加的消息条数(摘要指令);从指纹范围中剔除
|
|
34
|
+
* @returns 前缀指纹
|
|
35
|
+
*/
|
|
36
|
+
export declare function fingerprintRequestView(view: RequestView, input?: {
|
|
37
|
+
tailLength?: number;
|
|
38
|
+
}): PrefixFingerprint;
|
|
39
|
+
/**
|
|
40
|
+
* 比对两个前缀指纹,返回**具体是哪一项不同**(同形时返回空数组)。
|
|
41
|
+
*
|
|
42
|
+
* @param a - 会话请求的前缀指纹
|
|
43
|
+
* @param b - 摘要请求的前缀指纹
|
|
44
|
+
* @returns 差异项名称列表(空 = 前缀同形,缓存可命中)
|
|
45
|
+
*/
|
|
46
|
+
export declare function diffPrefixFingerprint(a: PrefixFingerprint, b: PrefixFingerprint): Array<'tools' | 'system' | 'messages'>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 缓存前缀指纹(可观测护栏)
|
|
3
|
+
*
|
|
4
|
+
* 事故背景(2026-11-12):摘要请求的前缀与主请求"看起来同形",实际整包 tools 缺失,
|
|
5
|
+
* provider 只回报 `inputCachedTokens: 0`——从读数完全看不出**是谁变了**,只能靠人工
|
|
6
|
+
* 逐字节比对 wire dump 才定位。本模块把"前缀是否同形"变成可判定、可打印的事实。
|
|
7
|
+
*
|
|
8
|
+
* 指纹刻意只覆盖**前缀**(tools → system → messages 的保护段),不含尾部指令:
|
|
9
|
+
* 摘要请求本来就要在尾部追加一条指令,那一段不可能也不应该相同。
|
|
10
|
+
*
|
|
11
|
+
* @module harness/context/prefix-fingerprint
|
|
12
|
+
*/
|
|
13
|
+
import { createHash } from 'node:crypto';
|
|
14
|
+
function sha1(text) {
|
|
15
|
+
return createHash('sha1').update(text, 'utf8').digest('hex').slice(0, 16);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 计算请求视图的前缀指纹
|
|
19
|
+
*
|
|
20
|
+
* @param view - 请求视图
|
|
21
|
+
* @param input.tailLength - 尾部追加的消息条数(摘要指令);从指纹范围中剔除
|
|
22
|
+
* @returns 前缀指纹
|
|
23
|
+
*/
|
|
24
|
+
export function fingerprintRequestView(view, input) {
|
|
25
|
+
const tailLength = Math.max(0, input?.tailLength ?? 0);
|
|
26
|
+
const prefix = tailLength > 0 ? view.messages.slice(0, -tailLength) : view.messages;
|
|
27
|
+
const system = prefix.find((m) => m.role === 'system');
|
|
28
|
+
return {
|
|
29
|
+
toolsHash: sha1(JSON.stringify(view.tools)),
|
|
30
|
+
systemHash: sha1(typeof system?.content === 'string' ? system.content : JSON.stringify(system?.content ?? '')),
|
|
31
|
+
prefixHash: sha1(JSON.stringify(prefix)),
|
|
32
|
+
prefixCount: prefix.length,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 比对两个前缀指纹,返回**具体是哪一项不同**(同形时返回空数组)。
|
|
37
|
+
*
|
|
38
|
+
* @param a - 会话请求的前缀指纹
|
|
39
|
+
* @param b - 摘要请求的前缀指纹
|
|
40
|
+
* @returns 差异项名称列表(空 = 前缀同形,缓存可命中)
|
|
41
|
+
*/
|
|
42
|
+
export function diffPrefixFingerprint(a, b) {
|
|
43
|
+
const out = [];
|
|
44
|
+
if (a.toolsHash !== b.toolsHash)
|
|
45
|
+
out.push('tools');
|
|
46
|
+
if (a.systemHash !== b.systemHash)
|
|
47
|
+
out.push('system');
|
|
48
|
+
// tools/system 不同必然导致整体不同,避免重复报告 messages
|
|
49
|
+
if (out.length === 0 && a.prefixHash !== b.prefixHash)
|
|
50
|
+
out.push('messages');
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
@@ -17,9 +17,9 @@ export interface CreateProviderSummarizeOptions {
|
|
|
17
17
|
/**
|
|
18
18
|
* 创建基于 ModelProvider 的摘要函数
|
|
19
19
|
*
|
|
20
|
-
* @param provider -
|
|
20
|
+
* @param provider - 用于调用的模型提供者
|
|
21
21
|
* @param options - 模型名与温度
|
|
22
|
-
* @returns SummarizeFunction;调用失败向上抛出,由
|
|
22
|
+
* @returns SummarizeFunction;调用失败向上抛出,由 compact 能力机械降级阶梯兜底
|
|
23
23
|
*/
|
|
24
24
|
export declare function createProviderSummarize(provider: ModelProvider, options?: CreateProviderSummarizeOptions): SummarizeFunction;
|
|
25
25
|
/**
|
|
@@ -7,23 +7,48 @@
|
|
|
7
7
|
/**
|
|
8
8
|
* 创建基于 ModelProvider 的摘要函数
|
|
9
9
|
*
|
|
10
|
-
* @param provider -
|
|
10
|
+
* @param provider - 用于调用的模型提供者
|
|
11
11
|
* @param options - 模型名与温度
|
|
12
|
-
* @returns SummarizeFunction;调用失败向上抛出,由
|
|
12
|
+
* @returns SummarizeFunction;调用失败向上抛出,由 compact 能力机械降级阶梯兜底
|
|
13
13
|
*/
|
|
14
14
|
export function createProviderSummarize(provider, options) {
|
|
15
15
|
return async (messages, opts) => {
|
|
16
16
|
const response = await provider.chat({
|
|
17
17
|
messages,
|
|
18
18
|
model: options?.model,
|
|
19
|
+
// 摘要请求带会话同形工具面:前缀缓存是 tools → system → messages,缺 tools 会整段冷 prefill。
|
|
20
|
+
// 不带 toolChoice(2026-11-13 实测):'none' 在部分 provider(DeepSeek 系)会让服务端
|
|
21
|
+
// 把 tools 从 prompt 渲染中剔除,反而落到另一个缓存空间、前缀零重叠。「禁止调用」由
|
|
22
|
+
// 摘要指令 + summarize_tool_call_emitted fail-loud 守卫承担,不依赖 provider 的软提示。
|
|
23
|
+
...(opts?.tools && opts.tools.length > 0 ? { tools: opts.tools } : {}),
|
|
19
24
|
// 未显式配置即不发送:由模型配置(ModelInfo.temperature)或服务端默认决定
|
|
20
25
|
temperature: options?.temperature,
|
|
26
|
+
// P3:run 中止 / 摘要超时都靠这个 signal 取消在飞请求(provider 侧 fetch 已接 signal)
|
|
27
|
+
...(opts?.signal ? { signal: opts.signal } : {}),
|
|
21
28
|
// 源头钳制(评审 F6):三条摘要路径(manual/proactive/overflow)共用本闭包,
|
|
22
29
|
// 硬顶不得越过模型实际输出上限,否则窗口钳制在大窗口下仍会顶穿被 provider 400
|
|
23
30
|
maxTokens: options?.maxOutputTokens && options.maxOutputTokens > 0
|
|
24
31
|
? Math.min(opts?.maxTokens ?? options.maxOutputTokens, options.maxOutputTokens)
|
|
25
32
|
: opts?.maxTokens,
|
|
26
33
|
});
|
|
34
|
+
// usage 回传(2026-11-08 outputTokens 真值):摘要产物体量的真值通道,
|
|
35
|
+
// 供压缩计量以真值替代估算(契约见 SummarizeFunction.onUsage)
|
|
36
|
+
if (response.usage && response.usage.outputTokens > 0) {
|
|
37
|
+
opts?.onUsage?.({ outputTokens: response.usage.outputTokens });
|
|
38
|
+
}
|
|
39
|
+
// 撞 max_tokens 的摘要 = 半截交接书(2026-11-12 评审 MAJ-2)。压缩常在会话最满时
|
|
40
|
+
// 触发,窗口钳制会把 max_tokens 压到刚好放得下,撞顶概率不低;把它当成功会让
|
|
41
|
+
// "## Open items" 段凭空消失且无人知晓——本次带工具面后输出上限进一步被挤压,必须
|
|
42
|
+
// fail loud 交 onSummarizeFail 显式降级,禁止静默半截(capabilities/compact 的既有红线)。
|
|
43
|
+
if (response.finishReason === 'length') {
|
|
44
|
+
throw new Error('summarize_output_truncated: provider stopped at max_tokens');
|
|
45
|
+
}
|
|
46
|
+
// 工具面为重放缓存前缀而完整发送,摘要轮本不该产生 tool_call(指令已明确要求纯文本)。
|
|
47
|
+
// 不带 toolChoice 约束后,本守卫是唯一硬防线:若 provider 仍冒 tool_call,正文就不是
|
|
48
|
+
// 摘要——宁可失败,交 onSummarizeFail 显式降级(禁止静默半截/错型产物)。
|
|
49
|
+
if (response.toolCalls && response.toolCalls.length > 0) {
|
|
50
|
+
throw new Error('summarize_tool_call_emitted: summary turn produced a tool call');
|
|
51
|
+
}
|
|
27
52
|
return response.content;
|
|
28
53
|
};
|
|
29
54
|
}
|
|
@@ -78,6 +78,8 @@ export declare function createDefaultSystemPromptAssembler(options?: {
|
|
|
78
78
|
/** Wisdom 注入条数硬顶 */
|
|
79
79
|
wisdomCoreMaxItems?: number;
|
|
80
80
|
wisdomScenarioMaxItems?: number;
|
|
81
|
+
/** trial 轮转试用名额(wisdom.injection.trialMaxItems;缺省 2——保底曝光打破晋升死锁;0 关闭) */
|
|
82
|
+
wisdomTrialMaxItems?: number;
|
|
81
83
|
/** trial 可见性(wisdom.injection.trialVisible;缺省 true——trial 的意义就是被试用) */
|
|
82
84
|
wisdomIncludeTrial?: boolean;
|
|
83
85
|
cognitionStore?: ConceptGraphStore;
|
|
@@ -86,6 +88,8 @@ export declare function createDefaultSystemPromptAssembler(options?: {
|
|
|
86
88
|
knowledgeGroupByScope?: boolean;
|
|
87
89
|
knowledgeShowProgress?: 'off' | 'bucket' | 'exact';
|
|
88
90
|
cognitionDepth?: number;
|
|
91
|
+
/** 语义种子上限(缺省 4;0 关闭语义种子) */
|
|
92
|
+
cognitionSemanticSeedCount?: number;
|
|
89
93
|
/** 全局宪法;提供则 preamble 固定最前 */
|
|
90
94
|
constitution?: ConstitutionConfig | string | null;
|
|
91
95
|
/** 直接注入宪法正文(覆盖 constitution 配置) */
|
|
@@ -7,6 +7,7 @@ import { DefaultContextAssembler } from './assembler.js';
|
|
|
7
7
|
import { CognitionLayer, KnowledgeLayer, MemoryLayer, PersonaLayer, RuntimeLayer, SkillLayer, WisdomLayer, } from './layers.js';
|
|
8
8
|
import { extractLayerQuery } from './layer-types.js';
|
|
9
9
|
import { loadConstitution } from './constitution/load-constitution.js';
|
|
10
|
+
import { ELISION_MARKER_SYSTEM_NOTE } from '@octopi-agent/core/elision.js';
|
|
10
11
|
import { TOOL_PURPOSE_SYSTEM_NOTE } from '../agent/tool-purpose.js';
|
|
11
12
|
const DEFAULT_SYSTEM_BUDGET_RATIO = 0.22;
|
|
12
13
|
/**
|
|
@@ -26,12 +27,13 @@ export const TURN_SEMANTICS_MD = `## Turns (authoritative)
|
|
|
26
27
|
- If a tool needs old ephemeral detail, re-read the source (session log / files);
|
|
27
28
|
do not assume old picks are still true.`;
|
|
28
29
|
/**
|
|
29
|
-
* 缓存稳定固定块(永远拼进 system):Turn 信封阅读规则置首,purpose
|
|
30
|
+
* 缓存稳定固定块(永远拼进 system):Turn 信封阅读规则置首,purpose 全局说明随后,
|
|
31
|
+
* 降级标记禁令殿后(工具注记区)。
|
|
30
32
|
* purpose 详版+示例在此只写一次(方案 S)——schema 侧只留 TOOL_PURPOSE_PARAM 极短 stub。
|
|
31
33
|
*/
|
|
32
34
|
function withFixedProtocolBlocks(systemPrompt) {
|
|
33
35
|
const body = systemPrompt.trim();
|
|
34
|
-
const head = `${TURN_SEMANTICS_MD}\n\n---\n\n${TOOL_PURPOSE_SYSTEM_NOTE}`;
|
|
36
|
+
const head = `${TURN_SEMANTICS_MD}\n\n---\n\n${TOOL_PURPOSE_SYSTEM_NOTE}\n\n---\n\n${ELISION_MARKER_SYSTEM_NOTE}`;
|
|
35
37
|
return body ? `${head}\n\n---\n\n${body}` : head;
|
|
36
38
|
}
|
|
37
39
|
export function createDefaultSystemPromptAssembler(options) {
|
|
@@ -115,9 +117,10 @@ export function createDefaultSystemPromptAssembler(options) {
|
|
|
115
117
|
text: query,
|
|
116
118
|
coreMaxItems: options?.wisdomCoreMaxItems,
|
|
117
119
|
scenarioMaxItems: options?.wisdomScenarioMaxItems,
|
|
120
|
+
trialMaxItems: options?.wisdomTrialMaxItems,
|
|
118
121
|
includeTrial: options?.wisdomIncludeTrial,
|
|
119
122
|
});
|
|
120
|
-
return picks
|
|
123
|
+
return picks;
|
|
121
124
|
},
|
|
122
125
|
onInjected: (ids) => wisdomStore.touchApplied(ids),
|
|
123
126
|
}));
|
|
@@ -134,6 +137,7 @@ export function createDefaultSystemPromptAssembler(options) {
|
|
|
134
137
|
layers.push(new CognitionLayer({
|
|
135
138
|
store: cognitionStore,
|
|
136
139
|
depth: options?.cognitionDepth,
|
|
140
|
+
semanticSeedCount: options?.cognitionSemanticSeedCount,
|
|
137
141
|
}));
|
|
138
142
|
}
|
|
139
143
|
if (options?.memoryStore) {
|
|
@@ -6,10 +6,14 @@
|
|
|
6
6
|
*/
|
|
7
7
|
/** 通用文本:英文约 4 chars/token,中文约 1 char/token */
|
|
8
8
|
export declare const CHARS_PER_TOKEN = 4;
|
|
9
|
-
/** 工具结果:结构化输出更密集,约
|
|
10
|
-
export declare const TOOL_RESULT_CHARS_PER_TOKEN =
|
|
11
|
-
/** JSON
|
|
12
|
-
export declare const JSON_CHARS_PER_TOKEN =
|
|
9
|
+
/** 工具结果:结构化输出更密集,约 1.7 chars/token(2026-11-08 生产样本拟合:原 2 系统低估 ~40%) */
|
|
10
|
+
export declare const TOOL_RESULT_CHARS_PER_TOKEN = 1.7;
|
|
11
|
+
/** JSON 负载(schema / arguments):结构符号多,约 2 chars/token(2026-11-08 样本拟合调低;与 tr 档共线,取业界密度区间居中,勿硬拟合) */
|
|
12
|
+
export declare const JSON_CHARS_PER_TOKEN = 2;
|
|
13
|
+
/** 中文等 CJK:1 token/字 */
|
|
14
|
+
export declare const TEXT_CJK_CHARS_PER_TOKEN = 1;
|
|
15
|
+
/** 代码 / 标识符:约 3 chars/token */
|
|
16
|
+
export declare const CODE_CHARS_PER_TOKEN = 3;
|
|
13
17
|
/** 消息结构开销(role + 分隔符 + 边界标记) */
|
|
14
18
|
export declare const MESSAGE_OVERHEAD_TOKENS = 12;
|
|
15
19
|
/** 安全余量系数:补偿估算不准 */
|
|
@@ -20,5 +24,3 @@ export declare const IMAGE_TOKEN_ESTIMATE = 1200;
|
|
|
20
24
|
export declare const AUDIO_TOKEN_ESTIMATE = 30;
|
|
21
25
|
/** 视频 token 估算(抽帧处理,假设 10 帧 × 85 token) */
|
|
22
26
|
export declare const VIDEO_TOKEN_ESTIMATE = 850;
|
|
23
|
-
/** 超长文本采样阈值(字符数) */
|
|
24
|
-
export declare const SAMPLE_THRESHOLD = 2000;
|
|
@@ -7,10 +7,14 @@
|
|
|
7
7
|
// ── 字符/token 比率 ──
|
|
8
8
|
/** 通用文本:英文约 4 chars/token,中文约 1 char/token */
|
|
9
9
|
export const CHARS_PER_TOKEN = 4;
|
|
10
|
-
/** 工具结果:结构化输出更密集,约
|
|
11
|
-
export const TOOL_RESULT_CHARS_PER_TOKEN =
|
|
12
|
-
/** JSON
|
|
13
|
-
export const JSON_CHARS_PER_TOKEN =
|
|
10
|
+
/** 工具结果:结构化输出更密集,约 1.7 chars/token(2026-11-08 生产样本拟合:原 2 系统低估 ~40%) */
|
|
11
|
+
export const TOOL_RESULT_CHARS_PER_TOKEN = 1.7;
|
|
12
|
+
/** JSON 负载(schema / arguments):结构符号多,约 2 chars/token(2026-11-08 样本拟合调低;与 tr 档共线,取业界密度区间居中,勿硬拟合) */
|
|
13
|
+
export const JSON_CHARS_PER_TOKEN = 2;
|
|
14
|
+
/** 中文等 CJK:1 token/字 */
|
|
15
|
+
export const TEXT_CJK_CHARS_PER_TOKEN = 1;
|
|
16
|
+
/** 代码 / 标识符:约 3 chars/token */
|
|
17
|
+
export const CODE_CHARS_PER_TOKEN = 3;
|
|
14
18
|
/** 消息结构开销(role + 分隔符 + 边界标记) */
|
|
15
19
|
export const MESSAGE_OVERHEAD_TOKENS = 12;
|
|
16
20
|
/** 安全余量系数:补偿估算不准 */
|
|
@@ -22,6 +26,3 @@ export const IMAGE_TOKEN_ESTIMATE = 1200;
|
|
|
22
26
|
export const AUDIO_TOKEN_ESTIMATE = 30;
|
|
23
27
|
/** 视频 token 估算(抽帧处理,假设 10 帧 × 85 token) */
|
|
24
28
|
export const VIDEO_TOKEN_ESTIMATE = 850;
|
|
25
|
-
// ── 采样策略 ──
|
|
26
|
-
/** 超长文本采样阈值(字符数) */
|
|
27
|
-
export const SAMPLE_THRESHOLD = 2000;
|
|
@@ -1,50 +1,59 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Token
|
|
2
|
+
* Token 估算 adapter(内容形状 → Segment → 唯一计数)
|
|
3
3
|
*
|
|
4
|
-
* 不依赖 Message / LLMMessage
|
|
5
|
-
*
|
|
4
|
+
* 不依赖 Message / LLMMessage 等领域类型,只做「内容形状 → token 数」。
|
|
5
|
+
* 计数逻辑全部委托 {@link estimateSegments}(P1-c:单实现 + 两 adapter);
|
|
6
|
+
* 本模块是「松散形状」adapter,领域/LLM 两入口的形状归一在 `token-estimator.ts`。
|
|
6
7
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* 2. 次选:专用 tokenizer(如 tiktoken)- 未来扩展
|
|
10
|
-
* 3. 兜底:启发式估算,按内容类型使用不同比率
|
|
8
|
+
* R4 红线:不得返回常数兜底——未知内容块按载荷长度计,且**告警**(分类表缺失必须可见,
|
|
9
|
+
* 每 type 限频 1 条/小时,防高频估算路径刷屏)。
|
|
11
10
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
15
|
-
* 每个 CJK 字符计为 CHARS_PER_TOKEN 个等效字符,
|
|
16
|
-
* 这样 `adjustedChars / CHARS_PER_TOKEN` 能得到准确的 token 估算。
|
|
17
|
-
*
|
|
18
|
-
* @param text 原始文本
|
|
19
|
-
* @returns CJK 加权后的等效字符数
|
|
20
|
-
*/
|
|
21
|
-
export declare function estimateAdjustedChars(text: string): number;
|
|
11
|
+
import { JSON_CHARS_PER_TOKEN } from './token-constants.js';
|
|
12
|
+
import { type Segment } from './token-segments.js';
|
|
22
13
|
/**
|
|
23
14
|
* 估算单段文本的 token 数
|
|
24
15
|
*
|
|
25
|
-
* 使用 CJK 感知的等效字符数 + 采样策略。
|
|
26
|
-
*
|
|
27
16
|
* @param text 原始文本
|
|
28
17
|
* @returns 估算 token 数
|
|
29
18
|
*/
|
|
30
19
|
export declare function estimateTextTokens(text: string): number;
|
|
31
20
|
/**
|
|
32
|
-
*
|
|
21
|
+
* 内容块 → Segment(分类归一;media 固定成本继承旧常量)
|
|
33
22
|
*
|
|
34
|
-
*
|
|
23
|
+
* 图像接受三种同价形态:原生 `image` 与转换后的 `image_url` / `input_image`
|
|
24
|
+
* (convertMessage 产出 image_url——旧版漏认导致图像按 0 计)。
|
|
25
|
+
* 未知块类型按实际载荷长度计(R4:无常数兜底):正文走 CJK 分类文本口径;
|
|
26
|
+
* 无正文按指针字段(name/url/src/path/filename)长度计——不序列化内嵌 base64
|
|
27
|
+
* 载荷(会虚高),无载荷即 0(不猜)。
|
|
35
28
|
*
|
|
36
29
|
* @param block ContentBlock(至少含 type,text 块需含 text)
|
|
37
|
-
* @returns
|
|
30
|
+
* @returns 归一化片段
|
|
38
31
|
*/
|
|
39
|
-
export declare function
|
|
32
|
+
export declare function contentBlockSegment(block: {
|
|
40
33
|
type?: string;
|
|
41
34
|
text?: string;
|
|
42
|
-
|
|
35
|
+
name?: string;
|
|
36
|
+
url?: string;
|
|
37
|
+
src?: string;
|
|
38
|
+
path?: string;
|
|
39
|
+
filename?: string;
|
|
40
|
+
mimeType?: string;
|
|
41
|
+
}): Segment;
|
|
43
42
|
/**
|
|
44
|
-
*
|
|
43
|
+
* 估算单个 content block 的 token 数
|
|
44
|
+
*
|
|
45
|
+
* @param block ContentBlock(至少含 type;text 块需含 text;未知块可带指针字段)
|
|
46
|
+
* @returns 估算 token 数
|
|
47
|
+
*/
|
|
48
|
+
export declare function estimateContentBlock(block: Parameters<typeof contentBlockSegment>[0]): number;
|
|
49
|
+
/**
|
|
50
|
+
* 工具调用 → tool_use 段
|
|
45
51
|
*
|
|
46
|
-
* 工具调用的 JSON 结构符号多,token 密度更高。
|
|
47
52
|
* `arguments` 已是字符串时直接用长度,避免二次 JSON.stringify。
|
|
53
|
+
*/
|
|
54
|
+
export declare function toolCallSegment(name: string | undefined, args: unknown): Segment;
|
|
55
|
+
/**
|
|
56
|
+
* 估算工具调用 JSON 的 token 数
|
|
48
57
|
*
|
|
49
58
|
* @param toolCalls `{ name?, arguments? }` 形态的工具调用列表
|
|
50
59
|
* @returns 估算 token 数
|
|
@@ -57,7 +66,7 @@ export declare function estimateToolCallTokens(toolCalls: Array<{
|
|
|
57
66
|
* 估算松散结构消息列表的 token 数
|
|
58
67
|
*
|
|
59
68
|
* 支持 string 和 ContentBlock[] 两种 content 格式,含消息结构开销。
|
|
60
|
-
* 适用于无强类型 Message / LLMMessage
|
|
69
|
+
* 适用于无强类型 Message / LLMMessage 的调用方(knowledge grounding 等)。
|
|
61
70
|
*
|
|
62
71
|
* @param messages `{ content?, role? }` 形态的消息列表
|
|
63
72
|
* @returns 估算 token 数
|
|
@@ -66,3 +75,4 @@ export declare function estimateTokens(messages: Array<{
|
|
|
66
75
|
content?: string | unknown[] | null;
|
|
67
76
|
role?: string;
|
|
68
77
|
}>): number;
|
|
78
|
+
export { JSON_CHARS_PER_TOKEN };
|
|
@@ -1,115 +1,134 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Token
|
|
2
|
+
* Token 估算 adapter(内容形状 → Segment → 唯一计数)
|
|
3
3
|
*
|
|
4
|
-
* 不依赖 Message / LLMMessage
|
|
5
|
-
*
|
|
4
|
+
* 不依赖 Message / LLMMessage 等领域类型,只做「内容形状 → token 数」。
|
|
5
|
+
* 计数逻辑全部委托 {@link estimateSegments}(P1-c:单实现 + 两 adapter);
|
|
6
|
+
* 本模块是「松散形状」adapter,领域/LLM 两入口的形状归一在 `token-estimator.ts`。
|
|
6
7
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* 2. 次选:专用 tokenizer(如 tiktoken)- 未来扩展
|
|
10
|
-
* 3. 兜底:启发式估算,按内容类型使用不同比率
|
|
8
|
+
* R4 红线:不得返回常数兜底——未知内容块按载荷长度计,且**告警**(分类表缺失必须可见,
|
|
9
|
+
* 每 type 限频 1 条/小时,防高频估算路径刷屏)。
|
|
11
10
|
*/
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
// 非拉丁字符检测,覆盖完整东亚字符范围
|
|
15
|
-
const NON_LATIN_RE = /[\u2E80-\u9FFF\uA000-\uA4FF\uAC00-\uD7AF\uF900-\uFAFF\uFF01-\uFF60\uFFE0-\uFFE6]/gu;
|
|
16
|
-
// CJK 扩展 B+ 的 surrogate pairs(U+20000-U+2FA1F)
|
|
17
|
-
const CJK_SURROGATE_HIGH_RE = /[\uD840-\uD87E][\uDC00-\uDFFF]/g;
|
|
11
|
+
import { JSON_CHARS_PER_TOKEN, IMAGE_TOKEN_ESTIMATE, AUDIO_TOKEN_ESTIMATE, VIDEO_TOKEN_ESTIMATE, } from './token-constants.js';
|
|
12
|
+
import { estimateSegments, textSegment } from './token-segments.js';
|
|
18
13
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* 每个 CJK 字符计为 CHARS_PER_TOKEN 个等效字符,
|
|
22
|
-
* 这样 `adjustedChars / CHARS_PER_TOKEN` 能得到准确的 token 估算。
|
|
14
|
+
* 估算单段文本的 token 数
|
|
23
15
|
*
|
|
24
16
|
* @param text 原始文本
|
|
25
|
-
* @returns
|
|
17
|
+
* @returns 估算 token 数
|
|
26
18
|
*/
|
|
27
|
-
export function
|
|
28
|
-
|
|
29
|
-
return 0;
|
|
30
|
-
const nonLatinCount = (text.match(NON_LATIN_RE) ?? []).length;
|
|
31
|
-
// 修正 surrogate pairs(CJK 扩展 B+ 占 2 个 UTF-16 单元但算 1 个字符)
|
|
32
|
-
const cjkSurrogates = (text.match(CJK_SURROGATE_HIGH_RE) ?? []).length;
|
|
33
|
-
const codePointLength = text.length - cjkSurrogates;
|
|
34
|
-
// CJK 字符本身已计入 codePointLength,额外加 (CHARS_PER_TOKEN - 1) 的权重
|
|
35
|
-
return codePointLength + nonLatinCount * (CHARS_PER_TOKEN - 1);
|
|
19
|
+
export function estimateTextTokens(text) {
|
|
20
|
+
return estimateSegments([textSegment(text)]);
|
|
36
21
|
}
|
|
22
|
+
/** 未知块告警限频:每 type 每小时至多 1 条(估算在热路径高频调用,防刷屏) */
|
|
23
|
+
const UNKNOWN_BLOCK_WARN_INTERVAL_MS = 3_600_000;
|
|
24
|
+
/** 记账上限:超出后新 type 不再告警(64 种未分类形状已足够说明分类表崩了) */
|
|
25
|
+
const UNKNOWN_BLOCK_WARN_MAX_TYPES = 64;
|
|
26
|
+
const unknownBlockWarnedAt = new Map();
|
|
37
27
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
28
|
+
* 未分类内容块告警:分类表缺失必须可见(R4 后半),但计数不阻塞(按载荷长度计)。
|
|
29
|
+
* 覆盖两种形态:真正未知的 type,以及已知 type 的畸形载荷(如 text 缺正文)——
|
|
30
|
+
* 后者同样说明分类表没接住,文案用 unclassified 不断言 type 本身未知。
|
|
41
31
|
*
|
|
42
|
-
* @param
|
|
43
|
-
* @
|
|
32
|
+
* @param type 未被分类表接住的块类型
|
|
33
|
+
* @param chars 该块被计入估算的载荷字符数
|
|
44
34
|
*/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
35
|
+
function warnUnknownBlock(type, chars) {
|
|
36
|
+
const key = type ?? '(undefined)';
|
|
37
|
+
const now = Date.now();
|
|
38
|
+
const last = unknownBlockWarnedAt.get(key);
|
|
39
|
+
if (last !== undefined && now - last < UNKNOWN_BLOCK_WARN_INTERVAL_MS)
|
|
40
|
+
return;
|
|
41
|
+
if (last === undefined && unknownBlockWarnedAt.size >= UNKNOWN_BLOCK_WARN_MAX_TYPES)
|
|
42
|
+
return;
|
|
43
|
+
unknownBlockWarnedAt.set(key, now);
|
|
44
|
+
console.warn(`[token-est] unclassified content block type=${JSON.stringify(key)} counted by payload length (chars=${chars}) — add a category in contentBlockSegment if this is a legitimate shape`);
|
|
53
45
|
}
|
|
54
46
|
/**
|
|
55
|
-
*
|
|
47
|
+
* 内容块 → Segment(分类归一;media 固定成本继承旧常量)
|
|
56
48
|
*
|
|
57
|
-
*
|
|
49
|
+
* 图像接受三种同价形态:原生 `image` 与转换后的 `image_url` / `input_image`
|
|
50
|
+
* (convertMessage 产出 image_url——旧版漏认导致图像按 0 计)。
|
|
51
|
+
* 未知块类型按实际载荷长度计(R4:无常数兜底):正文走 CJK 分类文本口径;
|
|
52
|
+
* 无正文按指针字段(name/url/src/path/filename)长度计——不序列化内嵌 base64
|
|
53
|
+
* 载荷(会虚高),无载荷即 0(不猜)。
|
|
58
54
|
*
|
|
59
55
|
* @param block ContentBlock(至少含 type,text 块需含 text)
|
|
60
|
-
* @returns
|
|
56
|
+
* @returns 归一化片段
|
|
61
57
|
*/
|
|
62
|
-
export function
|
|
58
|
+
export function contentBlockSegment(block) {
|
|
63
59
|
if (block.type === 'text' && typeof block.text === 'string') {
|
|
64
|
-
return
|
|
60
|
+
return textSegment(block.text);
|
|
61
|
+
}
|
|
62
|
+
if (block.type === 'image' || block.type === 'image_url' || block.type === 'input_image') {
|
|
63
|
+
return { kind: 'image', fixed: IMAGE_TOKEN_ESTIMATE };
|
|
65
64
|
}
|
|
66
|
-
if (block.type === 'image')
|
|
67
|
-
return IMAGE_TOKEN_ESTIMATE;
|
|
68
65
|
if (block.type === 'audio' || block.type === 'input_audio')
|
|
69
|
-
return AUDIO_TOKEN_ESTIMATE;
|
|
66
|
+
return { kind: 'audio', fixed: AUDIO_TOKEN_ESTIMATE };
|
|
70
67
|
if (block.type === 'video')
|
|
71
|
-
return VIDEO_TOKEN_ESTIMATE;
|
|
72
|
-
|
|
68
|
+
return { kind: 'video', fixed: VIDEO_TOKEN_ESTIMATE };
|
|
69
|
+
if (block.type === 'file') {
|
|
70
|
+
// FileBlock(core 声明类型):按指针字段计数——`data` 是 base64 内嵌载荷,
|
|
71
|
+
// 不序列化(会虚高);`sizeBytes` 不作估算输入(保持"长度计"单一口径)
|
|
72
|
+
return textSegment([block.name, block.url, block.mimeType].filter((v) => typeof v === 'string').join(' '));
|
|
73
|
+
}
|
|
74
|
+
const body = typeof block.text === 'string'
|
|
75
|
+
? block.text
|
|
76
|
+
: [block.name, block.url, block.src, block.path, block.filename]
|
|
77
|
+
.filter((v) => typeof v === 'string')
|
|
78
|
+
.join(' ');
|
|
79
|
+
warnUnknownBlock(block.type, body.length);
|
|
80
|
+
return textSegment(body);
|
|
73
81
|
}
|
|
74
82
|
/**
|
|
75
|
-
*
|
|
83
|
+
* 估算单个 content block 的 token 数
|
|
84
|
+
*
|
|
85
|
+
* @param block ContentBlock(至少含 type;text 块需含 text;未知块可带指针字段)
|
|
86
|
+
* @returns 估算 token 数
|
|
87
|
+
*/
|
|
88
|
+
export function estimateContentBlock(block) {
|
|
89
|
+
return estimateSegments([contentBlockSegment(block)]);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 工具调用 → tool_use 段
|
|
76
93
|
*
|
|
77
|
-
* 工具调用的 JSON 结构符号多,token 密度更高。
|
|
78
94
|
* `arguments` 已是字符串时直接用长度,避免二次 JSON.stringify。
|
|
95
|
+
*/
|
|
96
|
+
export function toolCallSegment(name, args) {
|
|
97
|
+
const argStr = typeof args === 'string' ? args : JSON.stringify(args ?? {});
|
|
98
|
+
return { kind: 'tool_use', nameChars: (name ?? '').length, argChars: argStr.length };
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* 估算工具调用 JSON 的 token 数
|
|
79
102
|
*
|
|
80
103
|
* @param toolCalls `{ name?, arguments? }` 形态的工具调用列表
|
|
81
104
|
* @returns 估算 token 数
|
|
82
105
|
*/
|
|
83
106
|
export function estimateToolCallTokens(toolCalls) {
|
|
84
|
-
|
|
85
|
-
for (const tc of toolCalls) {
|
|
86
|
-
total += estimateTextTokens(tc.name ?? '');
|
|
87
|
-
const argsStr = typeof tc.arguments === 'string' ? tc.arguments : JSON.stringify(tc.arguments ?? {});
|
|
88
|
-
total += Math.ceil(argsStr.length / JSON_CHARS_PER_TOKEN);
|
|
89
|
-
}
|
|
90
|
-
return total;
|
|
107
|
+
return estimateSegments(toolCalls.map((tc) => toolCallSegment(tc.name, tc.arguments)));
|
|
91
108
|
}
|
|
92
109
|
/**
|
|
93
110
|
* 估算松散结构消息列表的 token 数
|
|
94
111
|
*
|
|
95
112
|
* 支持 string 和 ContentBlock[] 两种 content 格式,含消息结构开销。
|
|
96
|
-
* 适用于无强类型 Message / LLMMessage
|
|
113
|
+
* 适用于无强类型 Message / LLMMessage 的调用方(knowledge grounding 等)。
|
|
97
114
|
*
|
|
98
115
|
* @param messages `{ content?, role? }` 形态的消息列表
|
|
99
116
|
* @returns 估算 token 数
|
|
100
117
|
*/
|
|
101
118
|
export function estimateTokens(messages) {
|
|
102
|
-
|
|
119
|
+
const segments = [];
|
|
103
120
|
for (const msg of messages) {
|
|
104
|
-
|
|
105
|
-
if (typeof msg.content === 'string'
|
|
106
|
-
|
|
121
|
+
segments.push({ kind: 'message_overhead', count: 1 });
|
|
122
|
+
if (typeof msg.content === 'string') {
|
|
123
|
+
segments.push(textSegment(msg.content));
|
|
107
124
|
}
|
|
108
125
|
else if (Array.isArray(msg.content)) {
|
|
109
126
|
for (const block of msg.content) {
|
|
110
|
-
|
|
127
|
+
segments.push(contentBlockSegment(block));
|
|
111
128
|
}
|
|
112
129
|
}
|
|
113
130
|
}
|
|
114
|
-
return
|
|
131
|
+
return estimateSegments(segments);
|
|
115
132
|
}
|
|
133
|
+
// JSON_CHARS_PER_TOKEN 仍由本层公开导出(工具 schema 等外部调用方使用);计数已走分类查表。
|
|
134
|
+
export { JSON_CHARS_PER_TOKEN };
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* HeuristicTokenEstimator — 启发式 Token 估算器
|
|
3
3
|
*
|
|
4
|
-
* 实现本域 `TokenEstimator` 契约(`./types.ts
|
|
5
|
-
*
|
|
4
|
+
* 实现本域 `TokenEstimator` 契约(`./types.ts`)。**计数逻辑唯一**在
|
|
5
|
+
* `token-segments.ts`(P1-c「单实现 + 两 adapter」);本文件是两个形状 adapter:
|
|
6
|
+
* - {@link fromDomainMessages}:领域 `Message[]`(toolCalls / toolResults 字段)
|
|
7
|
+
* - {@link fromLlmMessages}:provider `LLMMessage[]`(tool_calls / tool_use /
|
|
8
|
+
* tool_result 块 / role:'tool' 消息)
|
|
9
|
+
*
|
|
10
|
+
* 同内容两入口读数一致是测试红线(差 < 1%)。两处旧口径缺陷已随统一修正:
|
|
11
|
+
* 1. LLM 口径把 tool_use / tool_result 块落「常数 10」兜底(R4 违约)→ 分类计数;
|
|
12
|
+
* 2. 领域口径的 CJK 膨胀 hack 挪到 2 比率上高倍(全 CJK 工具结果 2 token/字)→ 分类查表。
|
|
6
13
|
*
|
|
7
14
|
* 三层策略:
|
|
8
15
|
* 1. 优先使用 LLM 返回的实际 token 数(usage reported prompt)- 外部回写
|
|
@@ -12,10 +19,23 @@
|
|
|
12
19
|
import type { Message } from '@octopi-agent/core/types.js';
|
|
13
20
|
import type { TokenEstimator } from './types.js';
|
|
14
21
|
import type { LLMMessage, LLMToolDefinition } from '@octopi-agent/core/interfaces/model-provider.js';
|
|
22
|
+
import { type Segment } from './token-segments.js';
|
|
15
23
|
export { estimateTextTokens } from './token-estimate-fns.js';
|
|
24
|
+
/** 领域消息列表 → Segment[](adapter 公开入口) */
|
|
25
|
+
export declare function fromDomainMessages(messages: Message[]): Segment[];
|
|
26
|
+
/** provider LLM 消息 → Segment[](adapter 公开入口) */
|
|
27
|
+
export declare function fromLlmMessages(messages: LLMMessage[]): Segment[];
|
|
16
28
|
/**
|
|
17
|
-
*
|
|
29
|
+
* 估算 LLM 消息列表的 token 数
|
|
30
|
+
*
|
|
31
|
+
* 适配 provider 侧 `LLMMessage`(OpenAI 风格 `tool_calls` / Anthropic 风格
|
|
32
|
+
* `tool_use` / `tool_result` 内容块 / `role:'tool'` 消息)。
|
|
33
|
+
*
|
|
34
|
+
* @param messages LLM 消息列表
|
|
35
|
+
* @returns 估算 token 数
|
|
18
36
|
*/
|
|
37
|
+
export declare function estimateLLMMessages(messages: LLMMessage[]): number;
|
|
38
|
+
/** 启发式 Token 估算器实现 */
|
|
19
39
|
export declare class HeuristicTokenEstimator implements TokenEstimator {
|
|
20
40
|
/**
|
|
21
41
|
* 估算单条消息的 token 数
|
|
@@ -30,10 +50,14 @@ export declare class HeuristicTokenEstimator implements TokenEstimator {
|
|
|
30
50
|
/**
|
|
31
51
|
* 估算多条消息的总 token 数
|
|
32
52
|
*
|
|
53
|
+
* 总量一次取整(评审 #3):逐消息取整会让读数随消息切分形态漂移。
|
|
54
|
+
*
|
|
33
55
|
* @param messages 领域消息列表
|
|
34
56
|
* @returns 估算 token 数
|
|
35
57
|
*/
|
|
36
58
|
estimateMessages(messages: Message[]): number;
|
|
59
|
+
/** 单条消息的浮点估算(WeakMap 缓存存未取整值,取整只发生在对外读数上) */
|
|
60
|
+
private rawEstimateMessage;
|
|
37
61
|
/**
|
|
38
62
|
* 估算文本的 token 数
|
|
39
63
|
*
|
|
@@ -52,12 +76,3 @@ export declare class HeuristicTokenEstimator implements TokenEstimator {
|
|
|
52
76
|
*/
|
|
53
77
|
estimateTools(tools: LLMToolDefinition[]): number;
|
|
54
78
|
}
|
|
55
|
-
/**
|
|
56
|
-
* 估算 LLM 消息列表的 token 数
|
|
57
|
-
*
|
|
58
|
-
* 适配 provider 侧 `LLMMessage`(OpenAI 风格 `tool_calls` / `input_audio`)。
|
|
59
|
-
*
|
|
60
|
-
* @param messages LLM 消息列表
|
|
61
|
-
* @returns 估算 token 数
|
|
62
|
-
*/
|
|
63
|
-
export declare function estimateLLMMessages(messages: LLMMessage[]): number;
|