@wrongstack/core 0.313.1 → 0.316.1
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/chronicle/index.js +31 -31
- package/dist/chronicle/legacy-journal-import.d.ts +0 -6
- package/dist/chronicle/metrics-ingest.d.ts +14 -0
- package/dist/chronicle/metrics-schema.d.ts +10 -1
- package/dist/chronicle/metrics-store.d.ts +20 -2
- package/dist/chronicle/project-server.js +39 -31
- package/dist/chronicle/sink.d.ts +13 -0
- package/dist/chronicle/sqlite-journal.d.ts +20 -2
- package/dist/{chunk-WOEZV3Q4.js → chunk-25LSLC3G.js} +2 -1
- package/dist/{chunk-AI4LLQ2U.js → chunk-27QIURLN.js} +229 -12
- package/dist/{chunk-I45KAW2N.js → chunk-3AGWVSS7.js} +38 -450
- package/dist/chunk-3BDV7RL3.js +211 -0
- package/dist/{chunk-NFAYHPRH.js → chunk-3ECENVHL.js} +10 -8
- package/dist/{chunk-FW5LDDMJ.js → chunk-45WI72ZB.js} +1576 -1156
- package/dist/{chunk-AT4V4XGI.js → chunk-4BMVPOHO.js} +539 -313
- package/dist/{chunk-YBUJCZ57.js → chunk-4I7HGVVA.js} +17 -5
- package/dist/{chunk-JVGPR3TS.js → chunk-4LCZVJVF.js} +7 -55
- package/dist/{chunk-4ZHTKJHM.js → chunk-4SJNS7OB.js} +36 -2
- package/dist/{chunk-AD7ZFZCS.js → chunk-5BMIE7QG.js} +3 -3
- package/dist/{chunk-IQ35HMQL.js → chunk-5YPSXQXM.js} +71 -24
- package/dist/chunk-6FHWXLPT.js +10 -0
- package/dist/{chunk-FIV4IJL7.js → chunk-6H5GEOET.js} +163 -20
- package/dist/{chunk-VB3H5WWB.js → chunk-6NWEVFEL.js} +2 -2
- package/dist/{chunk-E6I5RXVK.js → chunk-AYW5YINX.js} +2 -2
- package/dist/{chunk-DEIQMBD2.js → chunk-B7F4WVP4.js} +2 -2
- package/dist/{chunk-XQ2XGIBF.js → chunk-BPBKKBUK.js} +6 -7
- package/dist/{chunk-K4YUNKU7.js → chunk-BYD345OX.js} +2 -2
- package/dist/{chunk-LZDDLNCZ.js → chunk-C27KDLOX.js} +4 -4
- package/dist/{chunk-6AKOTGLM.js → chunk-CU546ESW.js} +7 -7
- package/dist/{chunk-ZZ4V3WIZ.js → chunk-DLHHXUAM.js} +10 -1
- package/dist/{chunk-BAKVJLEV.js → chunk-DOTHQSJ2.js} +28 -16
- package/dist/{chunk-KBZGE6LN.js → chunk-DYLPMV4Q.js} +4 -3
- package/dist/{chunk-52RTVH4M.js → chunk-F3JWNST7.js} +2 -2
- package/dist/{chunk-WEBMSQP2.js → chunk-FC2ZGRF3.js} +21 -4
- package/dist/{chunk-FHAMQS7Q.js → chunk-FCBTO76Z.js} +76 -146
- package/dist/{chunk-4LJJYBAU.js → chunk-FHZVFYSL.js} +9 -1
- package/dist/{chunk-W4A6QA3D.js → chunk-FWNLEZYG.js} +4 -3
- package/dist/{chunk-OJZEX73F.js → chunk-GDQ7CZRQ.js} +5 -1
- package/dist/{chunk-QVOVMXJR.js → chunk-GJJGJB2R.js} +32 -9
- package/dist/{chunk-4XV6IR37.js → chunk-HI3JGZR2.js} +7 -6
- package/dist/{chunk-FSWCWSTS.js → chunk-HMLIGAU7.js} +26 -26
- package/dist/{chunk-PFXFQQRT.js → chunk-HXKZ7OBQ.js} +7 -43
- package/dist/{chunk-2FQL4PDC.js → chunk-I7DALG7J.js} +5 -5
- package/dist/{chunk-GM54GJBU.js → chunk-IRUTEHCE.js} +3 -3
- package/dist/{chunk-TYW2GI5I.js → chunk-J2WRWR37.js} +40 -13
- package/dist/{chunk-ZEHUCJHY.js → chunk-JFPJ6LLQ.js} +2 -2
- package/dist/chunk-JMULFFLN.js +9 -0
- package/dist/{chunk-RD6DEE36.js → chunk-JO4MGB6Z.js} +94 -8
- package/dist/{chunk-5DSMEVA4.js → chunk-K6BXGITA.js} +7 -7
- package/dist/{chunk-O74MJHXU.js → chunk-KZMSZV32.js} +2 -2
- package/dist/{chunk-GFJOMSHG.js → chunk-LA7CLDRW.js} +6 -2
- package/dist/{chunk-M2WF6AXG.js → chunk-LSMMOS2Z.js} +7 -3
- package/dist/{chunk-ASX6AW36.js → chunk-LXWCMQNJ.js} +3 -3
- package/dist/{chunk-WZO23AWO.js → chunk-M27LIEKV.js} +72 -22
- package/dist/{chunk-JTHKOYWU.js → chunk-MJPHJRAL.js} +15 -15
- package/dist/{chunk-G5AAKEEZ.js → chunk-MTL4O5YR.js} +5 -5
- package/dist/chunk-NHJ75HXY.js +1 -0
- package/dist/{chunk-ZYU2YOJ6.js → chunk-NIOAGFPB.js} +29 -15
- package/dist/{chunk-Y2GE5TSI.js → chunk-NS52ISWS.js} +2 -2
- package/dist/{chunk-UIWONWVG.js → chunk-NSOFPKGT.js} +541 -148
- package/dist/{chunk-RCQYT2I4.js → chunk-NVIZG63R.js} +2 -2
- package/dist/{chunk-EPC3GZD4.js → chunk-OOYZPOXM.js} +2 -2
- package/dist/{chunk-7ZHZE5EO.js → chunk-OSGLYW5J.js} +2 -2
- package/dist/{chunk-CE4ZOCOI.js → chunk-PFCMSSUF.js} +9 -9
- package/dist/{chunk-GFXZTHNI.js → chunk-PGRVXIMY.js} +5 -5
- package/dist/{chunk-JJPMWPB6.js → chunk-PISBFBJN.js} +6 -6
- package/dist/chunk-POOOIWE5.js +9 -0
- package/dist/{chunk-5RP3QTRL.js → chunk-PQNS3U7Z.js} +20 -3
- package/dist/{chunk-3E244UDA.js → chunk-QSTSJOCK.js} +52 -6
- package/dist/{chunk-YUPMXOGP.js → chunk-RFQMOKWK.js} +3 -3
- package/dist/chunk-RTOTNK3P.js +484 -0
- package/dist/{chunk-SN2DQRL6.js → chunk-RXKSDUAU.js} +7 -7
- package/dist/{chunk-T3KGZBU5.js → chunk-S45JPKQN.js} +189 -43
- package/dist/{chunk-YNCVSFGF.js → chunk-SAFHACFA.js} +4 -1
- package/dist/{chunk-KI4L2DUL.js → chunk-TCCHINEF.js} +114 -19
- package/dist/{chunk-AWNQ6ED6.js → chunk-THEGTRAG.js} +7 -2
- package/dist/{chunk-2KKODIQU.js → chunk-TR4LQE7L.js} +15 -15
- package/dist/{chunk-GPQ3NJPR.js → chunk-TWJMGXCI.js} +3 -3
- package/dist/{chunk-H72H4XDU.js → chunk-U45AXT5D.js} +9 -9
- package/dist/{chunk-62V2AFVG.js → chunk-U4FWBSO3.js} +160 -73
- package/dist/{chunk-A455ATZB.js → chunk-UBGDX6FI.js} +8 -1
- package/dist/chunk-UJILKYES.js +75 -0
- package/dist/{chunk-KSJ5UTP7.js → chunk-ULMAQV3T.js} +84 -4
- package/dist/{chunk-CB2HFKTP.js → chunk-VVBP6KGT.js} +1 -1
- package/dist/{chunk-RPOA7IAE.js → chunk-W5VITDM2.js} +43 -25
- package/dist/{chunk-QRTG4XTI.js → chunk-WAHQVNWT.js} +7 -1
- package/dist/{chunk-SQP64RCS.js → chunk-WGGYRBXH.js} +3019 -2976
- package/dist/{chunk-K3G6JBUC.js → chunk-WLYKU45P.js} +679 -141
- package/dist/chunk-WP7BVANJ.js +53 -0
- package/dist/{chunk-GAZEWQEZ.js → chunk-XF4VUMKW.js} +2 -2
- package/dist/{chunk-PAAWLLZX.js → chunk-Y5YXDM7D.js} +2 -2
- package/dist/{chunk-65RDIOR2.js → chunk-YOPAUVU4.js} +162 -44
- package/dist/{chunk-FFSX2QDQ.js → chunk-YPT4A46F.js} +3 -3
- package/dist/chunk-YWXT3JNM.js +425 -0
- package/dist/{chunk-EFEHDQGY.js → chunk-ZHZ2Y2DH.js} +64 -10
- package/dist/{chunk-CQQL4ZOQ.js → chunk-ZT7JNIGX.js} +4 -3
- package/dist/coordination/agent-monitor.d.ts +9 -1
- package/dist/coordination/agent-subagent-runner.d.ts +1 -1
- package/dist/coordination/agents/agent-prompts.d.ts +8 -0
- package/dist/coordination/agents/index.d.ts +33 -1
- package/dist/coordination/agents/index.js +52 -22
- package/dist/coordination/agents/phase3-wave1-platform.d.ts +1 -3
- package/dist/coordination/agents/phase3-wave2-meta.d.ts +1 -3
- package/dist/coordination/agents/phase8-wave3-products.d.ts +1 -3
- package/dist/coordination/agents/phase9-wave4-platform-meta.d.ts +1 -3
- package/dist/coordination/agents/project-agent-identity.d.ts +11 -11
- package/dist/coordination/agents/types.d.ts +13 -2
- package/dist/coordination/brain-monitor.d.ts +30 -7
- package/dist/coordination/collab-debug.d.ts +1 -1
- package/dist/coordination/collab-pause.d.ts +1 -1
- package/dist/coordination/delegate-tool.d.ts +7 -5
- package/dist/coordination/director-kanban-queue-helpers.d.ts +1 -0
- package/dist/coordination/director-options.d.ts +7 -0
- package/dist/coordination/director-spawn-model.d.ts +17 -0
- package/dist/coordination/director-tools.d.ts +2 -2
- package/dist/coordination/director.d.ts +12 -0
- package/dist/coordination/dispatcher.d.ts +8 -0
- package/dist/coordination/fleet-bus.d.ts +35 -4
- package/dist/coordination/fleet-supervisor.d.ts +10 -2
- package/dist/coordination/fleet.d.ts +16 -7
- package/dist/coordination/index.d.ts +4 -0
- package/dist/coordination/index.js +86 -49
- package/dist/coordination/kanban-dispatch-port.d.ts +2 -2
- package/dist/coordination/mailbox-http-router.d.ts +4 -3
- package/dist/coordination/mailbox-http-validation.d.ts +1 -1
- package/dist/coordination/mailbox-project-server.js +15 -15
- package/dist/coordination/mailbox-types.d.ts +24 -6
- package/dist/coordination/model-tier-leader.d.ts +122 -0
- package/dist/coordination/model-tier.d.ts +122 -0
- package/dist/coordination/multi-agent-coordinator.d.ts +24 -0
- package/dist/coordination/origin-session.d.ts +16 -0
- package/dist/coordination/provider-status-tracker.d.ts +37 -5
- package/dist/coordination/session-note-hub.d.ts +44 -0
- package/dist/coordination/session-note-tool.d.ts +10 -0
- package/dist/core/agent.d.ts +1 -1
- package/dist/core/context.d.ts +22 -3
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +130 -38
- package/dist/core/request-conversation-binding.d.ts +37 -0
- package/dist/core/session-notes.d.ts +25 -0
- package/dist/core/system-prompt-builder.d.ts +11 -2
- package/dist/core/system-prompt-variants.d.ts +78 -0
- package/dist/design/index.js +3 -3
- package/dist/execution/auto-compaction-middleware.d.ts +11 -0
- package/dist/execution/autonomy-brain.d.ts +2 -1
- package/dist/execution/autonomy-prompt-contributor.d.ts +7 -2
- package/dist/execution/compaction-core.d.ts +4 -4
- package/dist/execution/eternal-autonomy.d.ts +1 -1
- package/dist/execution/index.js +138 -129
- package/dist/execution/model-runtime.d.ts +0 -7
- package/dist/execution/parallel-eternal-engine.d.ts +7 -7
- package/dist/extension/index.js +3 -3
- package/dist/goal/index.js +6 -6
- package/dist/hooks/index.js +3 -3
- package/dist/hq/index.js +14 -12
- package/dist/hq/persistence/event-log.d.ts +0 -1
- package/dist/hq/persistence.d.ts +4 -4
- package/dist/hq/publisher.d.ts +2 -0
- package/dist/index.d.ts +14 -11
- package/dist/index.js +728 -635
- package/dist/infrastructure/index.d.ts +4 -4
- package/dist/infrastructure/index.js +281 -260
- package/dist/infrastructure/token-counter.d.ts +9 -0
- package/dist/kernel/events/agent-events.d.ts +77 -3
- package/dist/kernel/events/brain-events.d.ts +12 -0
- package/dist/kernel/events/provider-events.d.ts +7 -1
- package/dist/kernel/events/session-events.d.ts +14 -0
- package/dist/kernel/events/tool-events.d.ts +13 -0
- package/dist/kernel/index.js +4 -4
- package/dist/mailbox-attach.d.ts +1 -1
- package/dist/models/index.js +10 -9
- package/dist/observability/event-bridge.d.ts +25 -5
- package/dist/observability/index.d.ts +9 -8
- package/dist/observability/index.js +459 -351
- package/dist/observability/tool-usage-source.d.ts +65 -0
- package/dist/plugin/index.d.ts +5 -0
- package/dist/plugin/index.js +66 -50
- package/dist/plugins/auto-review-plugin.d.ts +3 -2
- package/dist/plugins/review-finding-commands.d.ts +0 -8
- package/dist/{review-finding-store-DKAV3AS6.js → plugins/review-finding-store.js} +7 -6
- package/dist/plugins/review-finding-types.js +19 -0
- package/dist/plugins/review-report-integration.js +21 -0
- package/dist/{review-report-store-OWY3EC3N.js → plugins/review-report-store.js} +7 -6
- package/dist/plugins/review-report-types.js +13 -0
- package/dist/prompts/index.js +5 -5
- package/dist/registry/index.js +3 -3
- package/dist/registry/tool-registry.d.ts +59 -9
- package/dist/{registry-R23UBE7H.js → registry-7N4O3E7W.js} +5 -5
- package/dist/security/capabilities.d.ts +6 -0
- package/dist/security/file-permissions.d.ts +1 -1
- package/dist/security/index.js +47 -33
- package/dist/security/permission-policy.d.ts +12 -1
- package/dist/security/secret-scrubber.d.ts +15 -0
- package/dist/session-catalog/index.d.ts +1 -0
- package/dist/session-catalog/index.js +19 -14
- package/dist/session-catalog/project-server.js +30 -13
- package/dist/session-catalog/protocol.d.ts +32 -1
- package/dist/session-catalog/registry.d.ts +17 -1
- package/dist/session-catalog/session-agents.d.ts +56 -0
- package/dist/session-catalog/store-rebuild.d.ts +14 -0
- package/dist/session-catalog/store-schema.d.ts +26 -0
- package/dist/session-catalog/store.d.ts +27 -1
- package/dist/session-note-attach.d.ts +9 -0
- package/dist/skills/index.js +5 -5
- package/dist/statusline/index.d.ts +43 -0
- package/dist/statusline/index.js +102 -0
- package/dist/storage/board-store-port.d.ts +2 -2
- package/dist/storage/config-loader/in-project-policy.d.ts +29 -0
- package/dist/storage/config-loader.d.ts +3 -2
- package/dist/storage/file-session-writer.d.ts +4 -2
- package/dist/storage/goal-coordination.d.ts +6 -1
- package/dist/storage/goal-kanban.d.ts +4 -1
- package/dist/storage/index.d.ts +12 -4
- package/dist/storage/index.js +76 -61
- package/dist/storage/input-history-store.d.ts +2 -4
- package/dist/storage/memory-curator.d.ts +133 -0
- package/dist/storage/session-agent-attribution.d.ts +39 -0
- package/dist/storage/session-doctor.d.ts +109 -0
- package/dist/storage/session-event-bridge.d.ts +5 -7
- package/dist/storage/session-outcome.d.ts +42 -0
- package/dist/storage/session-recovery.d.ts +55 -0
- package/dist/storage/session-store/load-session-data.d.ts +11 -0
- package/dist/storage/session-store/prune-helpers.d.ts +1 -1
- package/dist/storage/session-store/resume-session.d.ts +0 -2
- package/dist/storage/session-store/types.d.ts +8 -0
- package/dist/storage/session-store.d.ts +3 -3
- package/dist/storage/session-summary-tracker.d.ts +8 -1
- package/dist/storage/session-write-buffer.d.ts +18 -0
- package/dist/storage/session-writer-truncate.d.ts +2 -0
- package/dist/tasking/index.js +2 -2
- package/dist/tools/fallback-manage-tools.d.ts +6 -4
- package/dist/tools/index.js +21 -21
- package/dist/tools/model-tier-set-tool.d.ts +66 -0
- package/dist/types/agent-bridge.d.ts +2 -1
- package/dist/types/config/mcp-features.d.ts +7 -0
- package/dist/types/config/model-tiers.d.ts +148 -0
- package/dist/types/config/root.d.ts +9 -0
- package/dist/types/config/tools.d.ts +73 -0
- package/dist/types/config.d.ts +1 -0
- package/dist/types/context.d.ts +2 -0
- package/dist/types/default-config.d.ts +6 -0
- package/dist/types/errors.d.ts +2 -1
- package/dist/types/index.d.ts +11 -9
- package/dist/types/index.js +25 -15
- package/dist/types/multi-agent.d.ts +43 -0
- package/dist/types/plugin.d.ts +1 -1
- package/dist/types/provider.d.ts +15 -0
- package/dist/types/runtime-capability-manifest.d.ts +1 -1
- package/dist/types/secret-scrubber.d.ts +15 -0
- package/dist/types/session-markers.d.ts +37 -10
- package/dist/types/session-markers.js +1 -1
- package/dist/types/session-timeline.d.ts +197 -0
- package/dist/types/session-timeline.js +12 -0
- package/dist/types/session.d.ts +172 -11
- package/dist/types/system-prompt.d.ts +27 -0
- package/dist/utils/config-backup.d.ts +9 -1
- package/dist/utils/heap-watchdog.js +2 -2
- package/dist/utils/index.js +50 -48
- package/dist/utils/message-invariants.d.ts +4 -1
- package/dist/utils/regex-guard.d.ts +1 -1
- package/dist/utils/socket-path.d.ts +1 -1
- package/dist/utils/token-estimate.d.ts +6 -2
- package/dist/utils/wstack-paths.d.ts +2 -0
- package/dist/wiring/proxy-rewrite.d.ts +28 -0
- package/dist/wiring/proxy-rewrite.js +7 -1
- package/dist/worktree/index.js +2 -2
- package/instructions/agents/explore-companion.md +45 -18
- package/instructions/agents/memory-curator.md +34 -22
- package/instructions/coordination/director-preamble.md +12 -7
- package/instructions/coordination/subagent-baseline.md +26 -5
- package/instructions/leader-after-task.md +4 -1
- package/instructions/llm/chimera-review.md +30 -9
- package/instructions/llm/memory-curator.md +31 -0
- package/instructions/sections/tool/delegation-compact.md +2 -0
- package/instructions/sections/tool/delegation-full.md +5 -1
- package/instructions/sections/tool/mailbox-compact.md +5 -1
- package/instructions/sections/tool/mailbox-full.md +3 -0
- package/instructions/sections/tool/session-note-compact.md +3 -0
- package/instructions/sections/tool/session-note-full.md +15 -0
- package/instructions/system-lite.md +9 -4
- package/instructions/system-pro.md +66 -143
- package/instructions/system.md +73 -175
- package/package.json +32 -4
- package/dist/chunk-2BUUIMQB.js +0 -225
- package/dist/chunk-FAYRX2AD.js +0 -13
- package/dist/chunk-H5E77KUJ.js +0 -15
- package/dist/chunk-M7BS2K6U.js +0 -1
- package/dist/coordination/agents.d.ts +0 -8
- package/dist/coordination/transport.d.ts +0 -2
- package/dist/storage/session-writer/session-writer-summary-tracker.d.ts +0 -28
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
export { assertProjectRootOutsideStateDir, type BootConfigOptions, type BootConfigResult, bootConfig, flagsToConfigPatch, } from '../boot.js';
|
|
2
|
+
export { CONTEXT_MANAGER_TOOL_NAME, type ContextManagerAction, type ContextManagerInput, type ContextManagerResult, type ContextManagerToolOptions, contextManagerTool, createContextManagerTool, } from './context-manager.js';
|
|
1
3
|
export { DefaultLogger, type DefaultLoggerOptions, type LogFormat } from './logger.js';
|
|
4
|
+
export { allServers, awsServer, blockServer, braveSearchServer, context7Server, everArtServer, filesystemServer, githubServer, googleMapsServer, miniMaxVisionServer, playwrightServer, sentinelServer, slackServer, sshManagerServer, zaiVisionServer, } from './mcp-servers.js';
|
|
2
5
|
export { DefaultPathResolver } from './path-resolver.js';
|
|
3
|
-
export { DefaultTokenCounter } from './token-counter.js';
|
|
4
6
|
export { ProviderCacheLedger } from './provider-cache-ledger.js';
|
|
5
|
-
export {
|
|
6
|
-
export { contextManagerTool, createContextManagerTool, type ContextManagerInput, type ContextManagerResult, type ContextManagerAction, type ContextManagerToolOptions, } from './context-manager.js';
|
|
7
|
-
export { assertProjectRootOutsideStateDir, bootConfig, flagsToConfigPatch, type BootConfigOptions, type BootConfigResult, } from '../boot.js';
|
|
7
|
+
export { DefaultTokenCounter } from './token-counter.js';
|
|
8
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -4,9 +4,9 @@ import {
|
|
|
4
4
|
assertProjectRootOutsideStateDir,
|
|
5
5
|
bootConfig,
|
|
6
6
|
flagsToConfigPatch
|
|
7
|
-
} from "../chunk-
|
|
8
|
-
import "../chunk-
|
|
9
|
-
import "../chunk-
|
|
7
|
+
} from "../chunk-MJPHJRAL.js";
|
|
8
|
+
import "../chunk-5YPSXQXM.js";
|
|
9
|
+
import "../chunk-CU546ESW.js";
|
|
10
10
|
import {
|
|
11
11
|
allServers,
|
|
12
12
|
awsServer,
|
|
@@ -28,272 +28,30 @@ import "../chunk-YY6APYUM.js";
|
|
|
28
28
|
import {
|
|
29
29
|
DefaultLogger
|
|
30
30
|
} from "../chunk-WBPVKEEE.js";
|
|
31
|
-
import "../chunk-
|
|
32
|
-
import "../chunk-
|
|
33
|
-
import
|
|
34
|
-
|
|
35
|
-
} from "../chunk-NFAYHPRH.js";
|
|
36
|
-
import "../chunk-CQQL4ZOQ.js";
|
|
37
|
-
import "../chunk-RPOA7IAE.js";
|
|
38
|
-
import "../chunk-DEIQMBD2.js";
|
|
31
|
+
import "../chunk-ZT7JNIGX.js";
|
|
32
|
+
import "../chunk-W5VITDM2.js";
|
|
33
|
+
import "../chunk-B7F4WVP4.js";
|
|
34
|
+
import "../chunk-6FHWXLPT.js";
|
|
39
35
|
import {
|
|
40
36
|
repairToolUseAdjacency
|
|
41
|
-
} from "../chunk-
|
|
42
|
-
import "../chunk-4XV6IR37.js";
|
|
37
|
+
} from "../chunk-LSMMOS2Z.js";
|
|
43
38
|
import "../chunk-4VWO3ZXL.js";
|
|
39
|
+
import "../chunk-HI3JGZR2.js";
|
|
44
40
|
import {
|
|
45
41
|
estimateMessageTokens,
|
|
46
42
|
estimateRequestTokens
|
|
47
|
-
} from "../chunk-
|
|
48
|
-
import "../chunk-
|
|
49
|
-
import "../chunk-
|
|
43
|
+
} from "../chunk-HMLIGAU7.js";
|
|
44
|
+
import "../chunk-OSGLYW5J.js";
|
|
45
|
+
import "../chunk-SAFHACFA.js";
|
|
46
|
+
import "../chunk-DLHHXUAM.js";
|
|
47
|
+
import {
|
|
48
|
+
promptCacheHitRatio
|
|
49
|
+
} from "../chunk-3ECENVHL.js";
|
|
50
50
|
import "../chunk-KJ2ZTQSL.js";
|
|
51
51
|
import "../chunk-B4AMLZPB.js";
|
|
52
|
-
import "../chunk-WOEZV3Q4.js";
|
|
53
|
-
import "../chunk-TBRL2RCC.js";
|
|
54
52
|
import "../chunk-PFZO3C6H.js";
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
var PRICE_CACHE_MAX_SIZE = 100;
|
|
58
|
-
var DefaultTokenCounter = class {
|
|
59
|
-
input = 0;
|
|
60
|
-
output = 0;
|
|
61
|
-
cacheRead = 0;
|
|
62
|
-
cacheWrite = 0;
|
|
63
|
-
cacheWrite5m = 0;
|
|
64
|
-
cacheWrite1h = 0;
|
|
65
|
-
costInput = 0;
|
|
66
|
-
costOutput = 0;
|
|
67
|
-
cacheSaved = 0;
|
|
68
|
-
cacheByProvider = /* @__PURE__ */ new Map();
|
|
69
|
-
registry;
|
|
70
|
-
providerId;
|
|
71
|
-
events;
|
|
72
|
-
sessionId;
|
|
73
|
-
priceCache = /* @__PURE__ */ new Map();
|
|
74
|
-
/** Most recently accounted request's tokens. Used for per-request context pressure. */
|
|
75
|
-
lastInput = 0;
|
|
76
|
-
lastCacheRead = 0;
|
|
77
|
-
lastCacheWrite = 0;
|
|
78
|
-
constructor(opts = {}) {
|
|
79
|
-
this.registry = opts.registry;
|
|
80
|
-
this.providerId = opts.providerId;
|
|
81
|
-
this.events = opts.events;
|
|
82
|
-
this.sessionId = opts.sessionId;
|
|
83
|
-
}
|
|
84
|
-
setSessionId(sessionId) {
|
|
85
|
-
this.sessionId = sessionId;
|
|
86
|
-
}
|
|
87
|
-
account(usage, model, providerId = this.currentProviderId()) {
|
|
88
|
-
const eventSessionId = this.currentSessionId();
|
|
89
|
-
this.input += usage.input;
|
|
90
|
-
this.output += usage.output;
|
|
91
|
-
this.cacheRead += usage.cacheRead ?? 0;
|
|
92
|
-
const ttlAggregate = usage.cacheWrite5m !== void 0 || usage.cacheWrite1h !== void 0 ? (usage.cacheWrite5m ?? 0) + (usage.cacheWrite1h ?? 0) : 0;
|
|
93
|
-
const cacheWriteTotal = usage.cacheWrite ?? ttlAggregate;
|
|
94
|
-
this.cacheWrite += cacheWriteTotal;
|
|
95
|
-
this.cacheWrite5m += usage.cacheWrite5m ?? 0;
|
|
96
|
-
this.cacheWrite1h += usage.cacheWrite1h ?? 0;
|
|
97
|
-
this.recordProviderCache(usage, providerId, cacheWriteTotal);
|
|
98
|
-
this.lastInput = usage.input;
|
|
99
|
-
this.lastCacheRead = usage.cacheRead ?? 0;
|
|
100
|
-
this.lastCacheWrite = cacheWriteTotal;
|
|
101
|
-
const priceKey = providerId && model ? `${providerId}\0${model}` : void 0;
|
|
102
|
-
const price = priceKey ? this.priceCache.get(priceKey) : void 0;
|
|
103
|
-
if (price) {
|
|
104
|
-
this.applyPrice(usage, price);
|
|
105
|
-
this.emitAccounted(eventSessionId, model, providerId, usage);
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
if (this.registry && providerId && model) {
|
|
109
|
-
if (this.priceCache.size >= PRICE_CACHE_MAX_SIZE) {
|
|
110
|
-
const keys = [...this.priceCache.keys()];
|
|
111
|
-
this.priceCache.delete(keys[0] ?? "");
|
|
112
|
-
}
|
|
113
|
-
void this.registry.getModel(providerId, model).then((m) => {
|
|
114
|
-
if (m) {
|
|
115
|
-
const p = priceFromModel(m);
|
|
116
|
-
this.priceCache.set(priceKey, p);
|
|
117
|
-
this.applyPrice(usage, p);
|
|
118
|
-
}
|
|
119
|
-
this.emitAccounted(eventSessionId, model, providerId, usage);
|
|
120
|
-
}).catch(() => {
|
|
121
|
-
this.events?.emit("token.cost_estimate_unavailable", {
|
|
122
|
-
...eventSessionId ? { sessionId: eventSessionId } : {},
|
|
123
|
-
model: model ?? "<unknown>"
|
|
124
|
-
});
|
|
125
|
-
this.emitAccounted(eventSessionId, model, providerId, usage);
|
|
126
|
-
return void 0;
|
|
127
|
-
});
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
this.emitAccounted(eventSessionId, model, providerId, usage);
|
|
131
|
-
}
|
|
132
|
-
/** Synchronous variant for code paths that have already resolved the model. */
|
|
133
|
-
accountWithModel(usage, resolved) {
|
|
134
|
-
const eventSessionId = this.currentSessionId();
|
|
135
|
-
this.input += usage.input;
|
|
136
|
-
this.output += usage.output;
|
|
137
|
-
this.cacheRead += usage.cacheRead ?? 0;
|
|
138
|
-
const ttlAggregate = usage.cacheWrite5m !== void 0 || usage.cacheWrite1h !== void 0 ? (usage.cacheWrite5m ?? 0) + (usage.cacheWrite1h ?? 0) : 0;
|
|
139
|
-
const cacheWriteTotal = usage.cacheWrite ?? ttlAggregate;
|
|
140
|
-
this.cacheWrite += cacheWriteTotal;
|
|
141
|
-
this.cacheWrite5m += usage.cacheWrite5m ?? 0;
|
|
142
|
-
this.cacheWrite1h += usage.cacheWrite1h ?? 0;
|
|
143
|
-
this.recordProviderCache(usage, resolved.providerId, cacheWriteTotal);
|
|
144
|
-
this.lastInput = usage.input;
|
|
145
|
-
this.lastCacheRead = usage.cacheRead ?? 0;
|
|
146
|
-
this.lastCacheWrite = cacheWriteTotal;
|
|
147
|
-
const price = priceFromModel(resolved);
|
|
148
|
-
if (this.priceCache.size >= PRICE_CACHE_MAX_SIZE) {
|
|
149
|
-
const keys = [...this.priceCache.keys()];
|
|
150
|
-
this.priceCache.delete(keys[0] ?? "");
|
|
151
|
-
}
|
|
152
|
-
this.priceCache.set(`${resolved.providerId}\0${resolved.modelId}`, price);
|
|
153
|
-
this.applyPrice(usage, price);
|
|
154
|
-
this.emitAccounted(eventSessionId, resolved.modelId, resolved.providerId, usage);
|
|
155
|
-
}
|
|
156
|
-
total() {
|
|
157
|
-
return {
|
|
158
|
-
input: this.input,
|
|
159
|
-
output: this.output,
|
|
160
|
-
cacheRead: this.cacheRead,
|
|
161
|
-
cacheWrite: this.cacheWrite
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
currentRequestTokens() {
|
|
165
|
-
return {
|
|
166
|
-
input: this.lastInput,
|
|
167
|
-
cacheRead: this.lastCacheRead,
|
|
168
|
-
cacheWrite: this.lastCacheWrite
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
setCurrentRequestTokens(input, cacheRead, cacheWrite) {
|
|
172
|
-
this.lastInput = input;
|
|
173
|
-
this.lastCacheRead = cacheRead ?? 0;
|
|
174
|
-
this.lastCacheWrite = cacheWrite ?? 0;
|
|
175
|
-
}
|
|
176
|
-
estimateCost() {
|
|
177
|
-
return {
|
|
178
|
-
input: round4(this.costInput),
|
|
179
|
-
output: round4(this.costOutput),
|
|
180
|
-
total: round4(this.costInput + this.costOutput),
|
|
181
|
-
currency: "USD"
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
cacheStats() {
|
|
185
|
-
const exposeTtlSplit = this.cacheWrite5m > 0 || this.cacheWrite1h > 0;
|
|
186
|
-
return {
|
|
187
|
-
readTokens: this.cacheRead,
|
|
188
|
-
writeTokens: this.cacheWrite,
|
|
189
|
-
...exposeTtlSplit ? { cacheWrite5m: this.cacheWrite5m, cacheWrite1h: this.cacheWrite1h } : {},
|
|
190
|
-
hitRatio: promptCacheHitRatio({
|
|
191
|
-
input: this.input,
|
|
192
|
-
output: this.output,
|
|
193
|
-
cacheRead: this.cacheRead,
|
|
194
|
-
cacheWrite: this.cacheWrite
|
|
195
|
-
}),
|
|
196
|
-
savedUsd: round4(this.cacheSaved),
|
|
197
|
-
providers: [...this.cacheByProvider.entries()].map(([provider, usage]) => ({
|
|
198
|
-
provider,
|
|
199
|
-
input: usage.input,
|
|
200
|
-
cacheRead: usage.cacheRead,
|
|
201
|
-
cacheWrite: usage.cacheWrite,
|
|
202
|
-
...usage.cacheWrite5m > 0 || usage.cacheWrite1h > 0 ? { cacheWrite5m: usage.cacheWrite5m, cacheWrite1h: usage.cacheWrite1h } : {},
|
|
203
|
-
hitRatio: promptCacheHitRatio({ ...usage, output: 0 })
|
|
204
|
-
})).sort((a, b) => b.cacheRead - a.cacheRead)
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
/** Invalidate cached prices so the next account() call fetches fresh data. */
|
|
208
|
-
invalidateCache() {
|
|
209
|
-
this.priceCache.clear();
|
|
210
|
-
}
|
|
211
|
-
emitAccounted(sessionId = this.currentSessionId(), model, providerId = this.currentProviderId(), deltaUsage) {
|
|
212
|
-
this.events?.emit("token.accounted", {
|
|
213
|
-
...sessionId ? { sessionId } : {},
|
|
214
|
-
usage: this.total(),
|
|
215
|
-
...deltaUsage ? { deltaUsage: { ...deltaUsage } } : {},
|
|
216
|
-
cost: {
|
|
217
|
-
input: this.costInput,
|
|
218
|
-
output: this.costOutput,
|
|
219
|
-
total: this.costInput + this.costOutput
|
|
220
|
-
},
|
|
221
|
-
...providerId ? { provider: providerId } : {},
|
|
222
|
-
...model ? { model } : {}
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
currentSessionId() {
|
|
226
|
-
const value = typeof this.sessionId === "function" ? this.sessionId() : this.sessionId;
|
|
227
|
-
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
228
|
-
}
|
|
229
|
-
currentProviderId() {
|
|
230
|
-
const value = typeof this.providerId === "function" ? this.providerId() : this.providerId;
|
|
231
|
-
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
232
|
-
}
|
|
233
|
-
reset() {
|
|
234
|
-
this.input = 0;
|
|
235
|
-
this.output = 0;
|
|
236
|
-
this.cacheRead = 0;
|
|
237
|
-
this.cacheWrite = 0;
|
|
238
|
-
this.cacheWrite5m = 0;
|
|
239
|
-
this.cacheWrite1h = 0;
|
|
240
|
-
this.costInput = 0;
|
|
241
|
-
this.costOutput = 0;
|
|
242
|
-
this.cacheSaved = 0;
|
|
243
|
-
this.cacheByProvider.clear();
|
|
244
|
-
this.lastInput = 0;
|
|
245
|
-
this.lastCacheRead = 0;
|
|
246
|
-
this.lastCacheWrite = 0;
|
|
247
|
-
this.emitAccounted(void 0, void 0, void 0, {
|
|
248
|
-
input: 0,
|
|
249
|
-
output: 0,
|
|
250
|
-
cacheRead: 0,
|
|
251
|
-
cacheWrite: 0
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
recordProviderCache(usage, providerId, derivedCacheWrite) {
|
|
255
|
-
const key = providerId ?? "unknown";
|
|
256
|
-
const current = this.cacheByProvider.get(key) ?? { input: 0, cacheRead: 0, cacheWrite: 0, cacheWrite5m: 0, cacheWrite1h: 0 };
|
|
257
|
-
current.input += usage.input;
|
|
258
|
-
current.cacheRead += usage.cacheRead ?? 0;
|
|
259
|
-
current.cacheWrite += derivedCacheWrite;
|
|
260
|
-
current.cacheWrite5m += usage.cacheWrite5m ?? 0;
|
|
261
|
-
current.cacheWrite1h += usage.cacheWrite1h ?? 0;
|
|
262
|
-
this.cacheByProvider.set(key, current);
|
|
263
|
-
}
|
|
264
|
-
applyPrice(usage, price) {
|
|
265
|
-
if (price.input) this.costInput += usage.input / 1e6 * price.input;
|
|
266
|
-
if (price.output) this.costOutput += usage.output / 1e6 * price.output;
|
|
267
|
-
if (usage.cacheRead && price.cacheRead) {
|
|
268
|
-
this.costInput += usage.cacheRead / 1e6 * price.cacheRead;
|
|
269
|
-
}
|
|
270
|
-
if (usage.cacheRead && price.input !== void 0) {
|
|
271
|
-
this.cacheSaved += usage.cacheRead / 1e6 * (price.input - (price.cacheRead ?? 0));
|
|
272
|
-
}
|
|
273
|
-
const hasCacheWriteSplit = usage.cacheWrite5m !== void 0 || usage.cacheWrite1h !== void 0;
|
|
274
|
-
const cacheWrite5m = usage.cacheWrite5m ?? (hasCacheWriteSplit ? 0 : usage.cacheWrite);
|
|
275
|
-
const cacheWrite1h = usage.cacheWrite1h ?? 0;
|
|
276
|
-
if (cacheWrite5m && (price.cacheWrite5m ?? price.cacheWrite)) {
|
|
277
|
-
this.costInput += cacheWrite5m / 1e6 * (price.cacheWrite5m ?? price.cacheWrite ?? 0);
|
|
278
|
-
}
|
|
279
|
-
if (cacheWrite1h && (price.cacheWrite1h ?? price.cacheWrite)) {
|
|
280
|
-
this.costInput += cacheWrite1h / 1e6 * (price.cacheWrite1h ?? price.cacheWrite ?? 0);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
};
|
|
284
|
-
function priceFromModel(m) {
|
|
285
|
-
return {
|
|
286
|
-
input: m.cost?.input,
|
|
287
|
-
output: m.cost?.output,
|
|
288
|
-
cacheRead: m.cost?.cache_read,
|
|
289
|
-
cacheWrite: m.cost?.cache_write,
|
|
290
|
-
cacheWrite5m: m.cost?.cache_write_5m ?? m.cost?.cache_write,
|
|
291
|
-
cacheWrite1h: m.cost?.cache_write_1h ?? (m.cost?.input !== void 0 ? m.cost.input * 2 : void 0)
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
function round4(n) {
|
|
295
|
-
return Math.round(n * 1e4) / 1e4;
|
|
296
|
-
}
|
|
53
|
+
import "../chunk-25LSLC3G.js";
|
|
54
|
+
import "../chunk-TBRL2RCC.js";
|
|
297
55
|
|
|
298
56
|
// src/infrastructure/context-manager.ts
|
|
299
57
|
var CONTEXT_MANAGER_TOOL_NAME = "context_manager";
|
|
@@ -554,7 +312,270 @@ function createContextManagerTool(opts = {}) {
|
|
|
554
312
|
};
|
|
555
313
|
}
|
|
556
314
|
var contextManagerTool = createContextManagerTool();
|
|
315
|
+
|
|
316
|
+
// src/infrastructure/token-counter.ts
|
|
317
|
+
var PRICE_CACHE_MAX_SIZE = 100;
|
|
318
|
+
var DefaultTokenCounter = class {
|
|
319
|
+
input = 0;
|
|
320
|
+
output = 0;
|
|
321
|
+
cacheRead = 0;
|
|
322
|
+
cacheWrite = 0;
|
|
323
|
+
cacheWrite5m = 0;
|
|
324
|
+
cacheWrite1h = 0;
|
|
325
|
+
costInput = 0;
|
|
326
|
+
costOutput = 0;
|
|
327
|
+
cacheSaved = 0;
|
|
328
|
+
cacheByProvider = /* @__PURE__ */ new Map();
|
|
329
|
+
registry;
|
|
330
|
+
providerId;
|
|
331
|
+
events;
|
|
332
|
+
sessionId;
|
|
333
|
+
/**
|
|
334
|
+
* Agent this counter belongs to, stamped onto every `token.accounted`.
|
|
335
|
+
* Undefined for the leader. Subagent counters are constructed with the HOST
|
|
336
|
+
* session id (so live cost UIs stay on one row), which makes this the only
|
|
337
|
+
* thing separating a subagent's tokens from the leader's downstream.
|
|
338
|
+
*/
|
|
339
|
+
agentId;
|
|
340
|
+
priceCache = /* @__PURE__ */ new Map();
|
|
341
|
+
/** Most recently accounted request's tokens. Used for per-request context pressure. */
|
|
342
|
+
lastInput = 0;
|
|
343
|
+
lastCacheRead = 0;
|
|
344
|
+
lastCacheWrite = 0;
|
|
345
|
+
constructor(opts = {}) {
|
|
346
|
+
this.registry = opts.registry;
|
|
347
|
+
this.providerId = opts.providerId;
|
|
348
|
+
this.events = opts.events;
|
|
349
|
+
this.sessionId = opts.sessionId;
|
|
350
|
+
this.agentId = opts.agentId;
|
|
351
|
+
}
|
|
352
|
+
setSessionId(sessionId) {
|
|
353
|
+
this.sessionId = sessionId;
|
|
354
|
+
}
|
|
355
|
+
account(usage, model, providerId = this.currentProviderId()) {
|
|
356
|
+
const eventSessionId = this.currentSessionId();
|
|
357
|
+
this.input += usage.input;
|
|
358
|
+
this.output += usage.output;
|
|
359
|
+
this.cacheRead += usage.cacheRead ?? 0;
|
|
360
|
+
const ttlAggregate = usage.cacheWrite5m !== void 0 || usage.cacheWrite1h !== void 0 ? (usage.cacheWrite5m ?? 0) + (usage.cacheWrite1h ?? 0) : 0;
|
|
361
|
+
const cacheWriteTotal = usage.cacheWrite ?? ttlAggregate;
|
|
362
|
+
this.cacheWrite += cacheWriteTotal;
|
|
363
|
+
this.cacheWrite5m += usage.cacheWrite5m ?? 0;
|
|
364
|
+
this.cacheWrite1h += usage.cacheWrite1h ?? 0;
|
|
365
|
+
this.recordProviderCache(usage, providerId, cacheWriteTotal);
|
|
366
|
+
this.lastInput = usage.input;
|
|
367
|
+
this.lastCacheRead = usage.cacheRead ?? 0;
|
|
368
|
+
this.lastCacheWrite = cacheWriteTotal;
|
|
369
|
+
const priceKey = providerId && model ? `${providerId}\0${model}` : void 0;
|
|
370
|
+
const price = priceKey ? this.priceCache.get(priceKey) : void 0;
|
|
371
|
+
if (price) {
|
|
372
|
+
this.applyPrice(usage, price);
|
|
373
|
+
this.emitAccounted(eventSessionId, model, providerId, usage);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
if (this.registry && providerId && model) {
|
|
377
|
+
if (this.priceCache.size >= PRICE_CACHE_MAX_SIZE) {
|
|
378
|
+
const keys = [...this.priceCache.keys()];
|
|
379
|
+
this.priceCache.delete(keys[0] ?? "");
|
|
380
|
+
}
|
|
381
|
+
void this.registry.getModel(providerId, model).then((m) => {
|
|
382
|
+
if (m) {
|
|
383
|
+
const p = priceFromModel(m);
|
|
384
|
+
this.priceCache.set(priceKey, p);
|
|
385
|
+
this.applyPrice(usage, p);
|
|
386
|
+
}
|
|
387
|
+
this.emitAccounted(eventSessionId, model, providerId, usage);
|
|
388
|
+
}).catch(() => {
|
|
389
|
+
this.events?.emit("token.cost_estimate_unavailable", {
|
|
390
|
+
...eventSessionId ? { sessionId: eventSessionId } : {},
|
|
391
|
+
model: model ?? "<unknown>"
|
|
392
|
+
});
|
|
393
|
+
this.emitAccounted(eventSessionId, model, providerId, usage);
|
|
394
|
+
return void 0;
|
|
395
|
+
});
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
this.emitAccounted(eventSessionId, model, providerId, usage);
|
|
399
|
+
}
|
|
400
|
+
/** Synchronous variant for code paths that have already resolved the model. */
|
|
401
|
+
accountWithModel(usage, resolved) {
|
|
402
|
+
const eventSessionId = this.currentSessionId();
|
|
403
|
+
this.input += usage.input;
|
|
404
|
+
this.output += usage.output;
|
|
405
|
+
this.cacheRead += usage.cacheRead ?? 0;
|
|
406
|
+
const ttlAggregate = usage.cacheWrite5m !== void 0 || usage.cacheWrite1h !== void 0 ? (usage.cacheWrite5m ?? 0) + (usage.cacheWrite1h ?? 0) : 0;
|
|
407
|
+
const cacheWriteTotal = usage.cacheWrite ?? ttlAggregate;
|
|
408
|
+
this.cacheWrite += cacheWriteTotal;
|
|
409
|
+
this.cacheWrite5m += usage.cacheWrite5m ?? 0;
|
|
410
|
+
this.cacheWrite1h += usage.cacheWrite1h ?? 0;
|
|
411
|
+
this.recordProviderCache(usage, resolved.providerId, cacheWriteTotal);
|
|
412
|
+
this.lastInput = usage.input;
|
|
413
|
+
this.lastCacheRead = usage.cacheRead ?? 0;
|
|
414
|
+
this.lastCacheWrite = cacheWriteTotal;
|
|
415
|
+
const price = priceFromModel(resolved);
|
|
416
|
+
if (this.priceCache.size >= PRICE_CACHE_MAX_SIZE) {
|
|
417
|
+
const keys = [...this.priceCache.keys()];
|
|
418
|
+
this.priceCache.delete(keys[0] ?? "");
|
|
419
|
+
}
|
|
420
|
+
this.priceCache.set(`${resolved.providerId}\0${resolved.modelId}`, price);
|
|
421
|
+
this.applyPrice(usage, price);
|
|
422
|
+
this.emitAccounted(eventSessionId, resolved.modelId, resolved.providerId, usage);
|
|
423
|
+
}
|
|
424
|
+
total() {
|
|
425
|
+
return {
|
|
426
|
+
input: this.input,
|
|
427
|
+
output: this.output,
|
|
428
|
+
cacheRead: this.cacheRead,
|
|
429
|
+
cacheWrite: this.cacheWrite
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
currentRequestTokens() {
|
|
433
|
+
return {
|
|
434
|
+
input: this.lastInput,
|
|
435
|
+
cacheRead: this.lastCacheRead,
|
|
436
|
+
cacheWrite: this.lastCacheWrite
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
setCurrentRequestTokens(input, cacheRead, cacheWrite) {
|
|
440
|
+
this.lastInput = input;
|
|
441
|
+
this.lastCacheRead = cacheRead ?? 0;
|
|
442
|
+
this.lastCacheWrite = cacheWrite ?? 0;
|
|
443
|
+
}
|
|
444
|
+
estimateCost() {
|
|
445
|
+
return {
|
|
446
|
+
input: round4(this.costInput),
|
|
447
|
+
output: round4(this.costOutput),
|
|
448
|
+
total: round4(this.costInput + this.costOutput),
|
|
449
|
+
currency: "USD"
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
cacheStats() {
|
|
453
|
+
const exposeTtlSplit = this.cacheWrite5m > 0 || this.cacheWrite1h > 0;
|
|
454
|
+
return {
|
|
455
|
+
readTokens: this.cacheRead,
|
|
456
|
+
writeTokens: this.cacheWrite,
|
|
457
|
+
...exposeTtlSplit ? { cacheWrite5m: this.cacheWrite5m, cacheWrite1h: this.cacheWrite1h } : {},
|
|
458
|
+
hitRatio: promptCacheHitRatio({
|
|
459
|
+
input: this.input,
|
|
460
|
+
output: this.output,
|
|
461
|
+
cacheRead: this.cacheRead,
|
|
462
|
+
cacheWrite: this.cacheWrite
|
|
463
|
+
}),
|
|
464
|
+
savedUsd: round4(this.cacheSaved),
|
|
465
|
+
providers: [...this.cacheByProvider.entries()].map(([provider, usage]) => ({
|
|
466
|
+
provider,
|
|
467
|
+
input: usage.input,
|
|
468
|
+
cacheRead: usage.cacheRead,
|
|
469
|
+
cacheWrite: usage.cacheWrite,
|
|
470
|
+
...usage.cacheWrite5m > 0 || usage.cacheWrite1h > 0 ? { cacheWrite5m: usage.cacheWrite5m, cacheWrite1h: usage.cacheWrite1h } : {},
|
|
471
|
+
hitRatio: promptCacheHitRatio({ ...usage, output: 0 })
|
|
472
|
+
})).sort((a, b) => b.cacheRead - a.cacheRead)
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
/** Invalidate cached prices so the next account() call fetches fresh data. */
|
|
476
|
+
invalidateCache() {
|
|
477
|
+
this.priceCache.clear();
|
|
478
|
+
}
|
|
479
|
+
emitAccounted(sessionId = this.currentSessionId(), model, providerId = this.currentProviderId(), deltaUsage) {
|
|
480
|
+
const agentId = this.currentAgentId();
|
|
481
|
+
this.events?.emit("token.accounted", {
|
|
482
|
+
...sessionId ? { sessionId } : {},
|
|
483
|
+
...agentId ? { agentId } : {},
|
|
484
|
+
usage: this.total(),
|
|
485
|
+
...deltaUsage ? { deltaUsage: { ...deltaUsage } } : {},
|
|
486
|
+
cost: {
|
|
487
|
+
input: this.costInput,
|
|
488
|
+
output: this.costOutput,
|
|
489
|
+
total: this.costInput + this.costOutput
|
|
490
|
+
},
|
|
491
|
+
...providerId ? { provider: providerId } : {},
|
|
492
|
+
...model ? { model } : {}
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
currentSessionId() {
|
|
496
|
+
const value = typeof this.sessionId === "function" ? this.sessionId() : this.sessionId;
|
|
497
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
498
|
+
}
|
|
499
|
+
currentProviderId() {
|
|
500
|
+
const value = typeof this.providerId === "function" ? this.providerId() : this.providerId;
|
|
501
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
502
|
+
}
|
|
503
|
+
currentAgentId() {
|
|
504
|
+
const value = typeof this.agentId === "function" ? this.agentId() : this.agentId;
|
|
505
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
506
|
+
}
|
|
507
|
+
reset() {
|
|
508
|
+
this.input = 0;
|
|
509
|
+
this.output = 0;
|
|
510
|
+
this.cacheRead = 0;
|
|
511
|
+
this.cacheWrite = 0;
|
|
512
|
+
this.cacheWrite5m = 0;
|
|
513
|
+
this.cacheWrite1h = 0;
|
|
514
|
+
this.costInput = 0;
|
|
515
|
+
this.costOutput = 0;
|
|
516
|
+
this.cacheSaved = 0;
|
|
517
|
+
this.cacheByProvider.clear();
|
|
518
|
+
this.lastInput = 0;
|
|
519
|
+
this.lastCacheRead = 0;
|
|
520
|
+
this.lastCacheWrite = 0;
|
|
521
|
+
this.emitAccounted(void 0, void 0, void 0, {
|
|
522
|
+
input: 0,
|
|
523
|
+
output: 0,
|
|
524
|
+
cacheRead: 0,
|
|
525
|
+
cacheWrite: 0
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
recordProviderCache(usage, providerId, derivedCacheWrite) {
|
|
529
|
+
const key = providerId ?? "unknown";
|
|
530
|
+
const current = this.cacheByProvider.get(key) ?? {
|
|
531
|
+
input: 0,
|
|
532
|
+
cacheRead: 0,
|
|
533
|
+
cacheWrite: 0,
|
|
534
|
+
cacheWrite5m: 0,
|
|
535
|
+
cacheWrite1h: 0
|
|
536
|
+
};
|
|
537
|
+
current.input += usage.input;
|
|
538
|
+
current.cacheRead += usage.cacheRead ?? 0;
|
|
539
|
+
current.cacheWrite += derivedCacheWrite;
|
|
540
|
+
current.cacheWrite5m += usage.cacheWrite5m ?? 0;
|
|
541
|
+
current.cacheWrite1h += usage.cacheWrite1h ?? 0;
|
|
542
|
+
this.cacheByProvider.set(key, current);
|
|
543
|
+
}
|
|
544
|
+
applyPrice(usage, price) {
|
|
545
|
+
if (price.input) this.costInput += usage.input / 1e6 * price.input;
|
|
546
|
+
if (price.output) this.costOutput += usage.output / 1e6 * price.output;
|
|
547
|
+
if (usage.cacheRead && price.cacheRead) {
|
|
548
|
+
this.costInput += usage.cacheRead / 1e6 * price.cacheRead;
|
|
549
|
+
}
|
|
550
|
+
if (usage.cacheRead && price.input !== void 0) {
|
|
551
|
+
this.cacheSaved += usage.cacheRead / 1e6 * (price.input - (price.cacheRead ?? 0));
|
|
552
|
+
}
|
|
553
|
+
const hasCacheWriteSplit = usage.cacheWrite5m !== void 0 || usage.cacheWrite1h !== void 0;
|
|
554
|
+
const cacheWrite5m = usage.cacheWrite5m ?? (hasCacheWriteSplit ? 0 : usage.cacheWrite);
|
|
555
|
+
const cacheWrite1h = usage.cacheWrite1h ?? 0;
|
|
556
|
+
if (cacheWrite5m && (price.cacheWrite5m ?? price.cacheWrite)) {
|
|
557
|
+
this.costInput += cacheWrite5m / 1e6 * (price.cacheWrite5m ?? price.cacheWrite ?? 0);
|
|
558
|
+
}
|
|
559
|
+
if (cacheWrite1h && (price.cacheWrite1h ?? price.cacheWrite)) {
|
|
560
|
+
this.costInput += cacheWrite1h / 1e6 * (price.cacheWrite1h ?? price.cacheWrite ?? 0);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
function priceFromModel(m) {
|
|
565
|
+
return {
|
|
566
|
+
input: m.cost?.input,
|
|
567
|
+
output: m.cost?.output,
|
|
568
|
+
cacheRead: m.cost?.cache_read,
|
|
569
|
+
cacheWrite: m.cost?.cache_write,
|
|
570
|
+
cacheWrite5m: m.cost?.cache_write_5m ?? m.cost?.cache_write,
|
|
571
|
+
cacheWrite1h: m.cost?.cache_write_1h ?? (m.cost?.input !== void 0 ? m.cost.input * 2 : void 0)
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
function round4(n) {
|
|
575
|
+
return Math.round(n * 1e4) / 1e4;
|
|
576
|
+
}
|
|
557
577
|
export {
|
|
578
|
+
CONTEXT_MANAGER_TOOL_NAME,
|
|
558
579
|
DefaultLogger,
|
|
559
580
|
DefaultPathResolver,
|
|
560
581
|
DefaultTokenCounter,
|
|
@@ -22,6 +22,13 @@ export declare class DefaultTokenCounter implements TokenCounter {
|
|
|
22
22
|
private readonly providerId?;
|
|
23
23
|
private readonly events?;
|
|
24
24
|
private sessionId?;
|
|
25
|
+
/**
|
|
26
|
+
* Agent this counter belongs to, stamped onto every `token.accounted`.
|
|
27
|
+
* Undefined for the leader. Subagent counters are constructed with the HOST
|
|
28
|
+
* session id (so live cost UIs stay on one row), which makes this the only
|
|
29
|
+
* thing separating a subagent's tokens from the leader's downstream.
|
|
30
|
+
*/
|
|
31
|
+
private readonly agentId?;
|
|
25
32
|
private priceCache;
|
|
26
33
|
/** Most recently accounted request's tokens. Used for per-request context pressure. */
|
|
27
34
|
private lastInput;
|
|
@@ -32,6 +39,7 @@ export declare class DefaultTokenCounter implements TokenCounter {
|
|
|
32
39
|
providerId?: string | (() => string | undefined) | undefined;
|
|
33
40
|
events?: EventBus | undefined;
|
|
34
41
|
sessionId?: string | (() => string | undefined) | undefined;
|
|
42
|
+
agentId?: string | (() => string | undefined) | undefined;
|
|
35
43
|
});
|
|
36
44
|
setSessionId(sessionId: string | (() => string | undefined) | undefined): void;
|
|
37
45
|
account(usage: Usage, model?: string, providerId?: string | undefined): void;
|
|
@@ -56,6 +64,7 @@ export declare class DefaultTokenCounter implements TokenCounter {
|
|
|
56
64
|
private emitAccounted;
|
|
57
65
|
private currentSessionId;
|
|
58
66
|
private currentProviderId;
|
|
67
|
+
private currentAgentId;
|
|
59
68
|
reset(): void;
|
|
60
69
|
private recordProviderCache;
|
|
61
70
|
private applyPrice;
|