@humain/humain-code 0.4.12 → 0.4.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -1
- package/dist/bun/sandbox-env-setup.js +1 -1
- package/dist/bun/sandbox-env-setup.js.map +1 -1
- package/dist/bundle/agent-monitor.js +1 -1
- package/dist/bundle/chunks/anthropic-messages-YSAHA7QM.js +67 -0
- package/dist/bundle/chunks/azure-openai-responses-Z5XYDPHS.js +15 -0
- package/dist/bundle/chunks/bedrock-converse-stream.js +393 -314
- package/dist/bundle/chunks/{chunk-EDKZRYWJ.js → chunk-2TTB6GIB.js} +2 -2
- package/dist/bundle/chunks/{chunk-HSDTSUZE.js → chunk-36Q5UVQL.js} +47 -89
- package/dist/bundle/chunks/chunk-4DXXZHY2.js +540 -0
- package/dist/bundle/chunks/{chunk-BKJU56SM.js → chunk-A7QKOI62.js} +142 -126
- package/dist/bundle/chunks/{chunk-UW4GMZL3.js → chunk-AIXI6RIE.js} +264 -248
- package/dist/bundle/chunks/{chunk-V5ZF7Q7L.js → chunk-BVHXWQLD.js} +52 -47
- package/dist/bundle/chunks/{chunk-I2ZWZAIV.js → chunk-COF5P635.js} +222 -148
- package/dist/bundle/chunks/{chunk-SCIGZFN5.js → chunk-DNGJMRJW.js} +11 -11
- package/dist/bundle/chunks/{chunk-XGO43DKL.js → chunk-DUSJT2W4.js} +21 -25
- package/dist/bundle/chunks/chunk-EYIYBQAR.js +7 -0
- package/dist/bundle/chunks/chunk-FRUFMISJ.js +505 -0
- package/dist/bundle/chunks/{chunk-R5VIAWGA.js → chunk-H2FY3PEV.js} +9 -8
- package/dist/bundle/chunks/{chunk-45ZBHMQG.js → chunk-HDA2IGKA.js} +4 -4
- package/dist/bundle/chunks/{chunk-KGI4EX6R.js → chunk-IUKCPYKD.js} +442 -442
- package/dist/bundle/chunks/{chunk-NLTW6A7G.js → chunk-KA7K6ECX.js} +3730 -3353
- package/dist/bundle/chunks/{chunk-AS2Y2MO6.js → chunk-KK3KUUMM.js} +643 -624
- package/dist/bundle/chunks/{chunk-6KAMTNN3.js → chunk-L3J4P5JR.js} +1 -1
- package/dist/bundle/chunks/chunk-L6Z6JKKC.js +3 -0
- package/dist/bundle/chunks/{chunk-DZ5C66IU.js → chunk-LUL775V7.js} +33 -33
- package/dist/bundle/chunks/{chunk-BRXQUQSC.js → chunk-MF2NEHP4.js} +778 -775
- package/dist/bundle/chunks/chunk-MG5PX4KU.js +31 -0
- package/dist/bundle/chunks/{chunk-LCOHFEZZ.js → chunk-NNKA2PAP.js} +1 -1
- package/dist/bundle/chunks/{chunk-HMWAH7ZY.js → chunk-OX2IBCCA.js} +53 -49
- package/dist/bundle/chunks/{chunk-WBJN6PEI.js → chunk-QYCOHSI7.js} +1 -1
- package/dist/bundle/chunks/{chunk-77EWF2NW.js → chunk-TJG52LZB.js} +1 -1
- package/dist/bundle/chunks/{chunk-5XD3DHYL.js → chunk-UDVC7A5S.js} +1 -1
- package/dist/bundle/chunks/chunk-UGXYG7HN.js +22 -0
- package/dist/bundle/chunks/{chunk-VYIIPF57.js → chunk-UHLW4KJ7.js} +419 -413
- package/dist/bundle/chunks/{chunk-FF26YENK.js → chunk-W2QRCQBX.js} +4 -3
- package/dist/bundle/chunks/chunk-XQRYOCHA.js +26 -0
- package/dist/bundle/chunks/{chunk-7Z5MWPYE.js → chunk-Y3RFGBG2.js} +1 -1
- package/dist/bundle/chunks/chunk-YJ3NQWES.js +9 -0
- package/dist/bundle/chunks/{chunk-YJBE3IGR.js → chunk-YRCLNWFV.js} +20 -20
- package/dist/bundle/chunks/chunk-YZMQXAJM.js +8 -0
- package/dist/bundle/chunks/{chunk-BHM75BJJ.js → chunk-Z74ZYQPX.js} +207 -79
- package/dist/bundle/chunks/{cli-5ACW3WND.js → cli-SRI4UOSL.js} +6 -6
- package/dist/bundle/chunks/{cloudflare-workers-ai-system-one-FZ3UQCZ7.js → cloudflare-workers-ai-system-one-JGN7NZCV.js} +1 -1
- package/dist/bundle/chunks/codemode-worker.js +27 -14
- package/dist/bundle/chunks/{compat-KXWWOFHQ.js → compat-OQMUE3QD.js} +6 -6
- package/dist/bundle/chunks/{dist-6SWVHFCS.js → dist-65YQEF26.js} +277 -72
- package/dist/bundle/chunks/dist-MNSGVI4C.js +7 -0
- package/dist/bundle/chunks/{dist-cjs-RL2OCAP2.js → dist-cjs-4BMVQ74V.js} +1 -1
- package/dist/bundle/chunks/{easter-egg-3d-3L3IKAT6.js → easter-egg-3d-ZJ2JUFOA.js} +1 -1
- package/dist/bundle/chunks/es-YEFEOGR3.js +1905 -0
- package/dist/bundle/chunks/{esm-763X2IA7.js → esm-NCUV2DF7.js} +1 -1
- package/dist/bundle/chunks/{event-streams-R5OECII4.js → event-streams-NXFTZDSF.js} +1 -1
- package/dist/bundle/chunks/execute-LRTXH4YJ.js +553 -0
- package/dist/bundle/chunks/{extract-DLZ425VI.js → extract-OZW5JHG6.js} +140 -120
- package/dist/bundle/chunks/{google-generative-ai-JD2FVOIX.js → google-generative-ai-YXXFSTX7.js} +2 -2
- package/dist/bundle/chunks/{google-vertex-PFBMDCBW.js → google-vertex-3IVWLYJ5.js} +2 -2
- package/dist/bundle/chunks/host-2RMBOGA7.js +2 -0
- package/dist/bundle/chunks/{mistral-conversations-D74NZ65O.js → mistral-conversations-Y7GWO3QB.js} +14 -13
- package/dist/bundle/chunks/{node-W47VUEGE.js → node-ELWNZC34.js} +328 -337
- package/dist/bundle/chunks/{node-4GGYDW5Q.js → node-SPBTZUUS.js} +26 -25
- package/dist/bundle/chunks/{openai-codex-responses-JAAA7MSS.js → openai-codex-responses-NSXWHIDT.js} +3 -4
- package/dist/bundle/chunks/{openai-completions-PGPMGQPU.js → openai-completions-WGBQHTYA.js} +9 -8
- package/dist/bundle/chunks/{openai-decisions-QQ4MEABI.js → openai-decisions-QWEISDVO.js} +1 -1
- package/dist/bundle/chunks/{openai-responses-FG2R2M4L.js → openai-responses-OKGCSIEY.js} +6 -6
- package/dist/bundle/chunks/{openrouter-images-NTBP3UUE.js → openrouter-images-BMVMAROU.js} +1 -1
- package/dist/bundle/chunks/{pi-messages-JD4O7KIP.js → pi-messages-XNEG23NU.js} +1 -1
- package/dist/bundle/chunks/runtime-VGSK3QCM.js +2 -0
- package/dist/bundle/chunks/{typesafe-system-one-TTC7YCO3.js → typesafe-system-one-62YHYXZV.js} +1 -1
- package/dist/bundle/chunks/{undici-EEOUTE5F.js → undici-7EW63V4X.js} +1 -1
- package/dist/bundle/chunks/virtual-modules-5KW36RPG.js +5 -0
- package/dist/bundle/cli-runtime.js +3 -3
- package/dist/bundle/flow/package.json +5 -6
- package/dist/bundle/index.js +6 -6
- package/dist/bundle/rpc-entry.js +2 -2
- package/dist/coordination/adapter/delivery.d.ts.map +1 -1
- package/dist/coordination/adapter/delivery.js +1 -0
- package/dist/coordination/adapter/delivery.js.map +1 -1
- package/dist/coordination/adapter/index.d.ts.map +1 -1
- package/dist/coordination/adapter/index.js +3 -1
- package/dist/coordination/adapter/index.js.map +1 -1
- package/dist/coordination/adapter/receipts.d.ts +2 -1
- package/dist/coordination/adapter/receipts.d.ts.map +1 -1
- package/dist/coordination/adapter/receipts.js +7 -4
- package/dist/coordination/adapter/receipts.js.map +1 -1
- package/dist/coordination/adapter/supervisor.d.ts.map +1 -1
- package/dist/coordination/adapter/supervisor.js +8 -0
- package/dist/coordination/adapter/supervisor.js.map +1 -1
- package/dist/coordination/broker/broker.d.ts +11 -0
- package/dist/coordination/broker/broker.d.ts.map +1 -1
- package/dist/coordination/broker/broker.js +195 -18
- package/dist/coordination/broker/broker.js.map +1 -1
- package/dist/coordination/client/client.d.ts +5 -0
- package/dist/coordination/client/client.d.ts.map +1 -1
- package/dist/coordination/client/client.js +23 -9
- package/dist/coordination/client/client.js.map +1 -1
- package/dist/coordination/transport/protocol.d.ts.map +1 -1
- package/dist/coordination/transport/protocol.js +8 -1
- package/dist/coordination/transport/protocol.js.map +1 -1
- package/dist/coordination/transport/types.d.ts +6 -0
- package/dist/coordination/transport/types.d.ts.map +1 -1
- package/dist/coordination/transport/types.js +2 -1
- package/dist/coordination/transport/types.js.map +1 -1
- package/dist/core/agent-session.d.ts +44 -2
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +305 -124
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-storage.d.ts.map +1 -1
- package/dist/core/auth-storage.js +20 -13
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/auto-effort.d.ts +16 -0
- package/dist/core/auto-effort.d.ts.map +1 -0
- package/dist/core/auto-effort.js +69 -0
- package/dist/core/auto-effort.js.map +1 -0
- package/dist/core/auto-session-namer.d.ts.map +1 -1
- package/dist/core/auto-session-namer.js +2 -12
- package/dist/core/auto-session-namer.js.map +1 -1
- package/dist/core/bug-report.d.ts +11 -1
- package/dist/core/bug-report.d.ts.map +1 -1
- package/dist/core/bug-report.js +2 -2
- package/dist/core/bug-report.js.map +1 -1
- package/dist/core/compaction/branch-summarization.d.ts +3 -2
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
- package/dist/core/compaction/branch-summarization.js +1 -1
- package/dist/core/compaction/branch-summarization.js.map +1 -1
- package/dist/core/compaction/compaction.d.ts +13 -4
- package/dist/core/compaction/compaction.d.ts.map +1 -1
- package/dist/core/compaction/compaction.js +94 -5
- package/dist/core/compaction/compaction.js.map +1 -1
- package/dist/core/compaction/utils.d.ts +6 -0
- package/dist/core/compaction/utils.d.ts.map +1 -1
- package/dist/core/compaction/utils.js +7 -8
- package/dist/core/compaction/utils.js.map +1 -1
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +4 -2
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +6 -3
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +33 -7
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/key-id-schema.d.ts +6 -0
- package/dist/core/key-id-schema.d.ts.map +1 -0
- package/dist/core/key-id-schema.js +16 -0
- package/dist/core/key-id-schema.js.map +1 -0
- package/dist/core/keybindings-schema.d.ts +4 -0
- package/dist/core/keybindings-schema.d.ts.map +1 -0
- package/dist/core/keybindings-schema.js +16 -0
- package/dist/core/keybindings-schema.js.map +1 -0
- package/dist/core/keybindings.d.ts +198 -63
- package/dist/core/keybindings.d.ts.map +1 -1
- package/dist/core/keybindings.js +26 -23
- package/dist/core/keybindings.js.map +1 -1
- package/dist/core/model-config.d.ts +695 -74
- package/dist/core/model-config.d.ts.map +1 -1
- package/dist/core/model-config.js +16 -166
- package/dist/core/model-config.js.map +1 -1
- package/dist/core/model-runtime.d.ts.map +1 -1
- package/dist/core/model-runtime.js +39 -4
- package/dist/core/model-runtime.js.map +1 -1
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +3 -3
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/project-trust.d.ts.map +1 -1
- package/dist/core/project-trust.js +2 -1
- package/dist/core/project-trust.js.map +1 -1
- package/dist/core/remote-catalog-provider.d.ts +13 -1
- package/dist/core/remote-catalog-provider.d.ts.map +1 -1
- package/dist/core/remote-catalog-provider.js +110 -27
- package/dist/core/remote-catalog-provider.js.map +1 -1
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +5 -1
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/session-export.d.ts.map +1 -1
- package/dist/core/session-export.js +2 -2
- package/dist/core/session-export.js.map +1 -1
- package/dist/core/session-manager.d.ts +36 -5
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +164 -40
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/settings-defaults.d.ts +65 -0
- package/dist/core/settings-defaults.d.ts.map +1 -0
- package/dist/core/settings-defaults.js +66 -0
- package/dist/core/settings-defaults.js.map +1 -0
- package/dist/core/settings-manager.d.ts +10 -175
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +64 -44
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/settings-schema.d.ts +257 -0
- package/dist/core/settings-schema.d.ts.map +1 -0
- package/dist/core/settings-schema.js +311 -0
- package/dist/core/settings-schema.js.map +1 -0
- package/dist/core/tools/edit-diff.d.ts +4 -1
- package/dist/core/tools/edit-diff.d.ts.map +1 -1
- package/dist/core/tools/edit-diff.js +480 -29
- package/dist/core/tools/edit-diff.js.map +1 -1
- package/dist/core/tools/render-utils.d.ts +18 -0
- package/dist/core/tools/render-utils.d.ts.map +1 -1
- package/dist/core/tools/render-utils.js +46 -0
- package/dist/core/tools/render-utils.js.map +1 -1
- package/dist/core/tools/renderers/bash.d.ts.map +1 -1
- package/dist/core/tools/renderers/bash.js +17 -46
- package/dist/core/tools/renderers/bash.js.map +1 -1
- package/dist/core/tools/renderers/edit.d.ts +19 -1
- package/dist/core/tools/renderers/edit.d.ts.map +1 -1
- package/dist/core/tools/renderers/edit.js +38 -7
- package/dist/core/tools/renderers/edit.js.map +1 -1
- package/dist/core/tools/renderers/find.d.ts.map +1 -1
- package/dist/core/tools/renderers/find.js +3 -0
- package/dist/core/tools/renderers/find.js.map +1 -1
- package/dist/core/tools/renderers/grep.d.ts.map +1 -1
- package/dist/core/tools/renderers/grep.js +3 -0
- package/dist/core/tools/renderers/grep.js.map +1 -1
- package/dist/core/tools/renderers/highlighting.d.ts +13 -0
- package/dist/core/tools/renderers/highlighting.d.ts.map +1 -0
- package/dist/core/tools/renderers/highlighting.js +42 -0
- package/dist/core/tools/renderers/highlighting.js.map +1 -0
- package/dist/core/tools/renderers/ls.d.ts.map +1 -1
- package/dist/core/tools/renderers/ls.js +3 -0
- package/dist/core/tools/renderers/ls.js.map +1 -1
- package/dist/core/tools/renderers/read.d.ts.map +1 -1
- package/dist/core/tools/renderers/read.js +3 -0
- package/dist/core/tools/renderers/read.js.map +1 -1
- package/dist/core/tools/renderers/write.d.ts.map +1 -1
- package/dist/core/tools/renderers/write.js +3 -1
- package/dist/core/tools/renderers/write.js.map +1 -1
- package/dist/core/tools/subagent.d.ts +4 -0
- package/dist/core/tools/subagent.d.ts.map +1 -1
- package/dist/core/tools/subagent.js +85 -8
- package/dist/core/tools/subagent.js.map +1 -1
- package/dist/core/tools/truncate.d.ts.map +1 -1
- package/dist/core/tools/truncate.js +92 -44
- package/dist/core/tools/truncate.js.map +1 -1
- package/dist/core/usage-totals.d.ts +8 -1
- package/dist/core/usage-totals.d.ts.map +1 -1
- package/dist/core/usage-totals.js +106 -10
- package/dist/core/usage-totals.js.map +1 -1
- package/dist/extensions/codemode/execute.d.ts.map +1 -1
- package/dist/extensions/codemode/execute.js +64 -45
- package/dist/extensions/codemode/execute.js.map +1 -1
- package/dist/extensions/codemode/index.d.ts.map +1 -1
- package/dist/extensions/codemode/index.js +2 -1
- package/dist/extensions/codemode/index.js.map +1 -1
- package/dist/extensions/codemode/renderer.d.ts +2 -2
- package/dist/extensions/codemode/renderer.d.ts.map +1 -1
- package/dist/extensions/codemode/renderer.js +9 -3
- package/dist/extensions/codemode/renderer.js.map +1 -1
- package/dist/extensions/codemode/tool.d.ts +6 -12
- package/dist/extensions/codemode/tool.d.ts.map +1 -1
- package/dist/extensions/codemode/tool.js +44 -32
- package/dist/extensions/codemode/tool.js.map +1 -1
- package/dist/extensions/mcp/cli.js +5 -8
- package/dist/extensions/mcp/cli.js.map +1 -1
- package/dist/extensions/mcp/index.d.ts.map +1 -1
- package/dist/extensions/mcp/index.js +26 -23
- package/dist/extensions/mcp/index.js.map +1 -1
- package/dist/extensions/mcp/resources.d.ts +0 -6
- package/dist/extensions/mcp/resources.d.ts.map +1 -1
- package/dist/extensions/mcp/resources.js +1 -1
- package/dist/extensions/mcp/resources.js.map +1 -1
- package/dist/extensions/mcp/runtime.d.ts +3 -6
- package/dist/extensions/mcp/runtime.d.ts.map +1 -1
- package/dist/extensions/mcp/runtime.js +4 -39
- package/dist/extensions/mcp/runtime.js.map +1 -1
- package/dist/extensions/mcp/tools.d.ts +2 -3
- package/dist/extensions/mcp/tools.d.ts.map +1 -1
- package/dist/extensions/mcp/tools.js +62 -6
- package/dist/extensions/mcp/tools.js.map +1 -1
- package/dist/extensions/tool-search/tool.d.ts +0 -2
- package/dist/extensions/tool-search/tool.d.ts.map +1 -1
- package/dist/extensions/tool-search/tool.js +2 -4
- package/dist/extensions/tool-search/tool.js.map +1 -1
- package/dist/humain/adhd/index.d.ts +3 -2
- package/dist/humain/adhd/index.d.ts.map +1 -1
- package/dist/humain/adhd/index.js +22 -10
- package/dist/humain/adhd/index.js.map +1 -1
- package/dist/humain/bedrock-provider/vendor/index.bundle.mjs +306 -20
- package/dist/humain/caveman/index.d.ts +4 -2
- package/dist/humain/caveman/index.d.ts.map +1 -1
- package/dist/humain/caveman/index.js +51 -16
- package/dist/humain/caveman/index.js.map +1 -1
- package/dist/humain/ponytail/index.d.ts +2 -0
- package/dist/humain/ponytail/index.d.ts.map +1 -1
- package/dist/humain/ponytail/index.js +2 -0
- package/dist/humain/ponytail/index.js.map +1 -1
- package/dist/humain/ponytail/vendor/hooks/ponytail-config.d.ts.map +1 -1
- package/dist/humain/ponytail/vendor/hooks/ponytail-config.js +39 -9
- package/dist/humain/ponytail/vendor/hooks/ponytail-config.js.map +1 -1
- package/dist/humain/ponytail/vendor/hooks/ponytail-instructions.js +1 -1
- package/dist/humain/ponytail/vendor/hooks/ponytail-instructions.js.map +1 -1
- package/dist/humain/ponytail/vendor/pi-extension/index.d.ts +4 -0
- package/dist/humain/ponytail/vendor/pi-extension/index.d.ts.map +1 -1
- package/dist/humain/ponytail/vendor/pi-extension/index.js +8 -0
- package/dist/humain/ponytail/vendor/pi-extension/index.js.map +1 -1
- package/dist/humain/web-access/vendor/baizhi.js.map +1 -1
- package/dist/humain/web-access/vendor/brave.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/brave.js +19 -2
- package/dist/humain/web-access/vendor/brave.js.map +1 -1
- package/dist/humain/web-access/vendor/ceramic.d.ts +4 -0
- package/dist/humain/web-access/vendor/ceramic.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/ceramic.js +212 -0
- package/dist/humain/web-access/vendor/ceramic.js.map +1 -0
- package/dist/humain/web-access/vendor/content-find.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/content-find.js +2 -1
- package/dist/humain/web-access/vendor/content-find.js.map +1 -1
- package/dist/humain/web-access/vendor/curator-page.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/curator-page.js +3 -1
- package/dist/humain/web-access/vendor/curator-page.js.map +1 -1
- package/dist/humain/web-access/vendor/curator-server.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/curator-server.js +2 -0
- package/dist/humain/web-access/vendor/curator-server.js.map +1 -1
- package/dist/humain/web-access/vendor/duckduckgo.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/duckduckgo.js +5 -1
- package/dist/humain/web-access/vendor/duckduckgo.js.map +1 -1
- package/dist/humain/web-access/vendor/endpoint-pdf-extract.d.ts +23 -0
- package/dist/humain/web-access/vendor/endpoint-pdf-extract.d.ts.map +1 -0
- package/dist/humain/web-access/vendor/endpoint-pdf-extract.js +193 -0
- package/dist/humain/web-access/vendor/endpoint-pdf-extract.js.map +1 -0
- package/dist/humain/web-access/vendor/extract.d.ts +5 -1
- package/dist/humain/web-access/vendor/extract.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/extract.js +49 -12
- package/dist/humain/web-access/vendor/extract.js.map +1 -1
- package/dist/humain/web-access/vendor/fetch-params.d.ts +2 -0
- package/dist/humain/web-access/vendor/fetch-params.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/fetch-params.js +9 -0
- package/dist/humain/web-access/vendor/fetch-params.js.map +1 -1
- package/dist/humain/web-access/vendor/gemini-api.d.ts +5 -0
- package/dist/humain/web-access/vendor/gemini-api.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/gemini-api.js +16 -0
- package/dist/humain/web-access/vendor/gemini-api.js.map +1 -1
- package/dist/humain/web-access/vendor/gemini-search.d.ts +3 -3
- package/dist/humain/web-access/vendor/gemini-search.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/gemini-search.js +48 -172
- package/dist/humain/web-access/vendor/gemini-search.js.map +1 -1
- package/dist/humain/web-access/vendor/github-extract.d.ts +7 -0
- package/dist/humain/web-access/vendor/github-extract.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/github-extract.js +31 -2
- package/dist/humain/web-access/vendor/github-extract.js.map +1 -1
- package/dist/humain/web-access/vendor/github-issue-pr.d.ts +2 -0
- package/dist/humain/web-access/vendor/github-issue-pr.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/github-issue-pr.js +86 -74
- package/dist/humain/web-access/vendor/github-issue-pr.js.map +1 -1
- package/dist/humain/web-access/vendor/index.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/index.js +68 -27
- package/dist/humain/web-access/vendor/index.js.map +1 -1
- package/dist/humain/web-access/vendor/openai-search.js +3 -3
- package/dist/humain/web-access/vendor/openai-search.js.map +1 -1
- package/dist/humain/web-access/vendor/page-query.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/page-query.js +6 -9
- package/dist/humain/web-access/vendor/page-query.js.map +1 -1
- package/dist/humain/web-access/vendor/parallel-mcp.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/parallel-mcp.js +2 -0
- package/dist/humain/web-access/vendor/parallel-mcp.js.map +1 -1
- package/dist/humain/web-access/vendor/pdf-extract.d.ts +1 -1
- package/dist/humain/web-access/vendor/pdf-extract.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/pdf-extract.js +31 -1
- package/dist/humain/web-access/vendor/pdf-extract.js.map +1 -1
- package/dist/humain/web-access/vendor/perplexity.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/perplexity.js +21 -2
- package/dist/humain/web-access/vendor/perplexity.js.map +1 -1
- package/dist/humain/web-access/vendor/query-rewrite.js +1 -1
- package/dist/humain/web-access/vendor/query-rewrite.js.map +1 -1
- package/dist/humain/web-access/vendor/ssrf-protection.d.ts +4 -0
- package/dist/humain/web-access/vendor/ssrf-protection.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/ssrf-protection.js +19 -4
- package/dist/humain/web-access/vendor/ssrf-protection.js.map +1 -1
- package/dist/humain/web-access/vendor/storage.d.ts +14 -0
- package/dist/humain/web-access/vendor/storage.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/storage.js +48 -0
- package/dist/humain/web-access/vendor/storage.js.map +1 -1
- package/dist/humain/web-access/vendor/summary-review.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/summary-review.js +18 -6
- package/dist/humain/web-access/vendor/summary-review.js.map +1 -1
- package/dist/humain/web-access/vendor/tool-arguments.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/tool-arguments.js +5 -2
- package/dist/humain/web-access/vendor/tool-arguments.js.map +1 -1
- package/dist/humain/web-access/vendor/utils.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/utils.js +21 -6
- package/dist/humain/web-access/vendor/utils.js.map +1 -1
- package/dist/humain/web-access/vendor/video-extract.js +6 -2
- package/dist/humain/web-access/vendor/video-extract.js.map +1 -1
- package/dist/humain/web-access/vendor/web-tool-core.d.ts +2 -1
- package/dist/humain/web-access/vendor/web-tool-core.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/web-tool-core.js +56 -13
- package/dist/humain/web-access/vendor/web-tool-core.js.map +1 -1
- package/dist/humain/web-access/vendor/zai.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/main.js +3 -3
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/assistant-message.d.ts +21 -1
- package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/assistant-message.js +79 -10
- package/dist/modes/interactive/components/assistant-message.js.map +1 -1
- package/dist/modes/interactive/components/footer.d.ts +8 -3
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +156 -133
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/mermaid.js +1 -1
- package/dist/modes/interactive/components/mermaid.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector.d.ts +10 -1
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector.js +83 -7
- package/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/dist/modes/interactive/components/status-indicator.d.ts +1 -1
- package/dist/modes/interactive/components/status-indicator.d.ts.map +1 -1
- package/dist/modes/interactive/components/status-indicator.js +2 -2
- package/dist/modes/interactive/components/status-indicator.js.map +1 -1
- package/dist/modes/interactive/components/themed-text.d.ts.map +1 -1
- package/dist/modes/interactive/components/themed-text.js +3 -1
- package/dist/modes/interactive/components/themed-text.js.map +1 -1
- package/dist/modes/interactive/components/tool-execution.d.ts +7 -1
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +36 -19
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/components/transcript-container.d.ts +12 -0
- package/dist/modes/interactive/components/transcript-container.d.ts.map +1 -0
- package/dist/modes/interactive/components/transcript-container.js +31 -0
- package/dist/modes/interactive/components/transcript-container.js.map +1 -0
- package/dist/modes/interactive/components/user-message.d.ts +6 -1
- package/dist/modes/interactive/components/user-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/user-message.js +20 -5
- package/dist/modes/interactive/components/user-message.js.map +1 -1
- package/dist/modes/interactive/components/visual-truncate.d.ts +20 -5
- package/dist/modes/interactive/components/visual-truncate.d.ts.map +1 -1
- package/dist/modes/interactive/components/visual-truncate.js +42 -3
- package/dist/modes/interactive/components/visual-truncate.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +2 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +125 -19
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/theme/theme-schema.d.ts +28 -0
- package/dist/modes/interactive/theme/theme-schema.d.ts.map +1 -0
- package/dist/modes/interactive/theme/theme-schema.js +105 -0
- package/dist/modes/interactive/theme/theme-schema.js.map +1 -0
- package/dist/modes/interactive/theme/theme-tokens.d.ts +257 -0
- package/dist/modes/interactive/theme/theme-tokens.d.ts.map +1 -0
- package/dist/modes/interactive/theme/theme-tokens.js +94 -0
- package/dist/modes/interactive/theme/theme-tokens.js.map +1 -0
- package/dist/modes/interactive/theme/theme.d.ts +15 -16
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme.js +26 -29
- package/dist/modes/interactive/theme/theme.js.map +1 -1
- package/dist/modes/print-mode.d.ts.map +1 -1
- package/dist/modes/print-mode.js +4 -0
- package/dist/modes/print-mode.js.map +1 -1
- package/dist/utils/child-process.d.ts +0 -7
- package/dist/utils/child-process.d.ts.map +1 -1
- package/dist/utils/child-process.js +12 -3
- package/dist/utils/child-process.js.map +1 -1
- package/dist/utils/clipboard.d.ts.map +1 -1
- package/dist/utils/clipboard.js +6 -1
- package/dist/utils/clipboard.js.map +1 -1
- package/dist/utils/image-convert.d.ts +7 -0
- package/dist/utils/image-convert.d.ts.map +1 -1
- package/dist/utils/image-convert.js +35 -3
- package/dist/utils/image-convert.js.map +1 -1
- package/dist/utils/image-resize.d.ts.map +1 -1
- package/dist/utils/image-resize.js +2 -2
- package/dist/utils/image-resize.js.map +1 -1
- package/dist/utils/syntax-highlight.d.ts +7 -0
- package/dist/utils/syntax-highlight.d.ts.map +1 -1
- package/dist/utils/syntax-highlight.js +54 -3
- package/dist/utils/syntax-highlight.js.map +1 -1
- package/dist/utils/windows-path.js +1 -1
- package/dist/utils/windows-path.js.map +1 -1
- package/dist/utils/zip.d.ts.map +1 -1
- package/dist/utils/zip.js +3 -1
- package/dist/utils/zip.js.map +1 -1
- package/docs/auto-session-namer.md +4 -0
- package/docs/cli-integration.md +1 -1
- package/docs/cli.md +1 -1
- package/docs/codemode.md +1 -1
- package/docs/compaction.md +9 -5
- package/docs/coordination.md +7 -0
- package/docs/environment-variables.md +1 -0
- package/docs/extensions.md +7 -4
- package/docs/keybindings.md +1 -0
- package/docs/mcp.md +1 -1
- package/docs/models.md +1 -0
- package/docs/providers.md +20 -0
- package/docs/session-format.md +16 -2
- package/docs/settings.md +16 -1
- package/docs/themes.md +4 -2
- package/docs/tui.md +1 -1
- package/docs/usage.md +6 -2
- package/examples/extensions/custom-provider-anthropic/package-lock.json +6 -6
- package/examples/extensions/custom-provider-anthropic/package.json +2 -2
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/dynamic-resources/dynamic.json +1 -1
- package/examples/extensions/gondolin/package-lock.json +20 -20
- package/examples/extensions/gondolin/package.json +1 -1
- package/examples/extensions/overlay-test.ts +2 -2
- package/examples/extensions/plan-mode/index.ts +6 -2
- package/examples/extensions/sandbox/package-lock.json +8 -8
- package/examples/extensions/sandbox/package.json +2 -2
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/examples/plugins/pi-example-plugin/package.json +2 -2
- package/examples/plugins/pi-example-plugin/src/contract.ts +1 -1
- package/examples/plugins/pi-example-plugin/src/session.ts +1 -1
- package/examples/plugins/pi-example-plugin/src/tui.ts +1 -1
- package/npm-shrinkwrap.json +174 -190
- package/package.json +24 -23
- package/schemas/keybindings.schema.json +397 -0
- package/schemas/models.schema.json +1157 -0
- package/schemas/settings.schema.json +875 -0
- package/schemas/theme.schema.json +365 -0
- package/src/coordination/UPSTREAM.md +36 -3
- package/src/coordination/upstream.json +3 -2
- package/src/humain/adhd/UPSTREAM.md +21 -3
- package/src/humain/adhd/upstream.json +1 -1
- package/src/humain/caveman/UPSTREAM.md +24 -3
- package/src/humain/ponytail/UPSTREAM.md +51 -4
- package/src/humain/ponytail/upstream.json +12 -12
- package/src/humain/ponytail/vendor/skills/ponytail/SKILL.md +1 -1
- package/src/humain/ponytail/vendor/skills/ponytail-audit/SKILL.md +1 -1
- package/src/humain/ponytail/vendor/skills/ponytail-debt/SKILL.md +13 -10
- package/src/humain/ponytail/vendor/skills/ponytail-help/SKILL.md +1 -1
- package/src/humain/ponytail/vendor/skills/ponytail-review/SKILL.md +1 -1
- package/src/humain/web-access/UPSTREAM.md +55 -1
- package/src/humain/web-access/upstream.json +4 -4
- package/dist/bundle/chunks/anthropic-messages-UNHXXOD4.js +0 -70
- package/dist/bundle/chunks/azure-openai-responses-4QRYJCC2.js +0 -14
- package/dist/bundle/chunks/chunk-GVVRXZHN.js +0 -17
- package/dist/bundle/chunks/chunk-QMUIATLD.js +0 -209
- package/dist/bundle/chunks/chunk-QSPDLT2R.js +0 -160
- package/dist/bundle/chunks/chunk-SZRZXVVZ.js +0 -125
- package/dist/bundle/chunks/chunk-XMHLSV3I.js +0 -48
- package/dist/bundle/chunks/chunk-YKZGL4MB.js +0 -86
- package/dist/bundle/chunks/dist-VNSMKBZ6.js +0 -7
- package/dist/bundle/chunks/es-N6YFJOD7.js +0 -1901
- package/dist/bundle/chunks/execute-4X6G65NZ.js +0 -536
- package/dist/bundle/chunks/host-4J7H7KDS.js +0 -2
- package/dist/bundle/chunks/runtime-SI76JZNE.js +0 -2
- package/dist/bundle/chunks/virtual-modules-GZY3Y4GX.js +0 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"truncate.d.ts","sourceRoot":"","sources":["../../../src/core/tools/truncate.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"truncate.d.ts","sourceRoot":"","sources":["../../../src/core/tools/truncate.ts"],"names":[],"mappings":"AACA;;;;;;;;GAQG;AAEH,eAAO,MAAM,iBAAiB,OAAO,CAAC;AACtC,eAAO,MAAM,iBAAiB,QAAY,CAAC;AAC3C,eAAO,MAAM,oBAAo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M,CAAC;IAChB,SAAS,EAAE,OAAO,CAAC;IACnB,2BAA2B;IAC3B,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;CACnB;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,sBAAsB,CA0BxF"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// Derived from Pi-Bolt (MIT, Copyright (c) 2026 OpenSec (Pi-Bolt)), commit b93895d2; modified for HUMAIN Code CLI.
|
|
1
2
|
/**
|
|
2
3
|
* Shared truncation utilities for tool outputs.
|
|
3
4
|
*
|
|
@@ -10,15 +11,35 @@
|
|
|
10
11
|
export const DEFAULT_MAX_LINES = 2000;
|
|
11
12
|
export const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB
|
|
12
13
|
export const GREP_MAX_LINE_LENGTH = 500; // Max chars per grep match line
|
|
13
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Number of lines in `content`: one per "\n"-separated line, not counting the empty one after a trailing "\n".
|
|
16
|
+
* Counted without splitting: callers truncate output that can be many megabytes to a few kilobytes, and splitting
|
|
17
|
+
* would allocate a string per line of all of it.
|
|
18
|
+
*/
|
|
19
|
+
function countLines(content) {
|
|
14
20
|
if (content.length === 0) {
|
|
15
|
-
return
|
|
21
|
+
return 0;
|
|
16
22
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
lines
|
|
23
|
+
let lines = 1;
|
|
24
|
+
for (let i = content.indexOf("\n"); i !== -1; i = content.indexOf("\n", i + 1)) {
|
|
25
|
+
lines++;
|
|
20
26
|
}
|
|
21
|
-
return lines;
|
|
27
|
+
return content.endsWith("\n") ? lines - 1 : lines;
|
|
28
|
+
}
|
|
29
|
+
const NON_ASCII = /[\u0080-\uffff]/g;
|
|
30
|
+
const SURROGATE = /[\ud800-\udfff]/;
|
|
31
|
+
const SURROGATE_PAIRS = /[\ud800-\udbff][\udc00-\udfff]/g;
|
|
32
|
+
/** Index of the first non-ASCII character of `content` at or after `from`, or its length if there is none. */
|
|
33
|
+
function findNonAscii(content, from) {
|
|
34
|
+
NON_ASCII.lastIndex = from;
|
|
35
|
+
return NON_ASCII.exec(content)?.index ?? content.length;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A copy of `text` that shares no memory with the string it was cut from, so a short result does not keep a large
|
|
39
|
+
* input alive. UTF-16 code units round-trip unchanged, lone surrogates included.
|
|
40
|
+
*/
|
|
41
|
+
function copyString(text) {
|
|
42
|
+
return Buffer.from(text, "utf16le").toString("utf16le");
|
|
22
43
|
}
|
|
23
44
|
/**
|
|
24
45
|
* Format bytes as human-readable size.
|
|
@@ -45,8 +66,7 @@ export function truncateHead(content, options = {}) {
|
|
|
45
66
|
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
|
|
46
67
|
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
47
68
|
const totalBytes = Buffer.byteLength(content, "utf-8");
|
|
48
|
-
const
|
|
49
|
-
const totalLines = lines.length;
|
|
69
|
+
const totalLines = countLines(content);
|
|
50
70
|
// Check if no truncation needed
|
|
51
71
|
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
|
52
72
|
return {
|
|
@@ -64,7 +84,12 @@ export function truncateHead(content, options = {}) {
|
|
|
64
84
|
};
|
|
65
85
|
}
|
|
66
86
|
// Check if first line alone exceeds byte limit
|
|
67
|
-
|
|
87
|
+
// Every non-ASCII character takes more UTF-8 bytes than UTF-16 units, so equal lengths mean the content is ASCII and
|
|
88
|
+
// a line's byte length is its length.
|
|
89
|
+
const ascii = totalBytes === content.length;
|
|
90
|
+
const firstNewline = content.indexOf("\n");
|
|
91
|
+
const firstLineEnd = firstNewline === -1 ? content.length : firstNewline;
|
|
92
|
+
const firstLineBytes = ascii ? firstLineEnd : Buffer.byteLength(content.slice(0, firstLineEnd), "utf-8");
|
|
68
93
|
if (firstLineBytes > maxBytes) {
|
|
69
94
|
return {
|
|
70
95
|
content: "",
|
|
@@ -80,34 +105,48 @@ export function truncateHead(content, options = {}) {
|
|
|
80
105
|
maxBytes,
|
|
81
106
|
};
|
|
82
107
|
}
|
|
83
|
-
//
|
|
84
|
-
|
|
108
|
+
// Count the complete lines that fit. They are a prefix of `content`, which is cut once instead of slicing every
|
|
109
|
+
// line and joining them again. Only lines that contain a non-ASCII character are measured with Buffer.byteLength.
|
|
110
|
+
let outputLines = 0;
|
|
85
111
|
let outputBytesCount = 0;
|
|
112
|
+
let outputEnd = 0;
|
|
86
113
|
let truncatedBy = "lines";
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const
|
|
114
|
+
let nextNonAscii = ascii ? content.length : findNonAscii(content, 0);
|
|
115
|
+
for (let start = 0; outputLines < totalLines && outputLines < maxLines;) {
|
|
116
|
+
const newline = content.indexOf("\n", start);
|
|
117
|
+
const end = newline === -1 ? content.length : newline;
|
|
118
|
+
let lineBytes;
|
|
119
|
+
if (end <= nextNonAscii) {
|
|
120
|
+
lineBytes = end - start;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
lineBytes = Buffer.byteLength(content.slice(start, end), "utf-8");
|
|
124
|
+
nextNonAscii = findNonAscii(content, end);
|
|
125
|
+
}
|
|
126
|
+
if (outputLines > 0)
|
|
127
|
+
lineBytes++; // +1 for newline
|
|
90
128
|
if (outputBytesCount + lineBytes > maxBytes) {
|
|
91
129
|
truncatedBy = "bytes";
|
|
92
130
|
break;
|
|
93
131
|
}
|
|
94
|
-
|
|
132
|
+
outputLines++;
|
|
95
133
|
outputBytesCount += lineBytes;
|
|
134
|
+
outputEnd = end;
|
|
135
|
+
start = end + 1;
|
|
96
136
|
}
|
|
97
137
|
// If we exited due to line limit
|
|
98
|
-
if (
|
|
138
|
+
if (outputLines >= maxLines && outputBytesCount <= maxBytes) {
|
|
99
139
|
truncatedBy = "lines";
|
|
100
140
|
}
|
|
101
|
-
const outputContent = outputLinesArr.join("\n");
|
|
102
|
-
const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8");
|
|
103
141
|
return {
|
|
104
|
-
content:
|
|
142
|
+
content: copyString(content.slice(0, outputEnd)),
|
|
105
143
|
truncated: true,
|
|
106
144
|
truncatedBy,
|
|
107
145
|
totalLines,
|
|
108
146
|
totalBytes,
|
|
109
|
-
outputLines
|
|
110
|
-
|
|
147
|
+
outputLines,
|
|
148
|
+
// A "\n" never splits a surrogate pair, so the lines' byte lengths add up to the output's.
|
|
149
|
+
outputBytes: outputBytesCount,
|
|
111
150
|
lastLinePartial: false,
|
|
112
151
|
firstLineExceedsLimit: false,
|
|
113
152
|
maxLines,
|
|
@@ -124,8 +163,7 @@ export function truncateTail(content, options = {}) {
|
|
|
124
163
|
const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
|
|
125
164
|
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
126
165
|
const totalBytes = Buffer.byteLength(content, "utf-8");
|
|
127
|
-
const
|
|
128
|
-
const totalLines = lines.length;
|
|
166
|
+
const totalLines = countLines(content);
|
|
129
167
|
// Check if no truncation needed
|
|
130
168
|
if (totalLines <= maxLines && totalBytes <= maxBytes) {
|
|
131
169
|
return {
|
|
@@ -142,44 +180,49 @@ export function truncateTail(content, options = {}) {
|
|
|
142
180
|
maxBytes,
|
|
143
181
|
};
|
|
144
182
|
}
|
|
145
|
-
// Work backwards from the end
|
|
146
|
-
|
|
183
|
+
// Work backwards from the end. A trailing "\n" ends the last line. The kept lines are a suffix of the content up to
|
|
184
|
+
// that "\n", which is cut once instead of slicing every line and joining them again.
|
|
185
|
+
const ascii = totalBytes === content.length;
|
|
186
|
+
const contentEnd = content.endsWith("\n") ? content.length - 1 : content.length;
|
|
187
|
+
let outputLines = 0;
|
|
147
188
|
let outputBytesCount = 0;
|
|
189
|
+
let outputStart = contentEnd;
|
|
190
|
+
let partialLine;
|
|
148
191
|
let truncatedBy = "lines";
|
|
149
|
-
let
|
|
150
|
-
for (let i =
|
|
151
|
-
const
|
|
152
|
-
const lineBytes = Buffer.byteLength(
|
|
192
|
+
let end = contentEnd;
|
|
193
|
+
for (let i = totalLines - 1; i >= 0 && outputLines < maxLines; i--) {
|
|
194
|
+
const start = i === 0 ? 0 : content.lastIndexOf("\n", end - 1) + 1;
|
|
195
|
+
const lineBytes = (ascii ? end - start : Buffer.byteLength(content.slice(start, end), "utf-8")) + (outputLines > 0 ? 1 : 0); // +1 for newline
|
|
153
196
|
if (outputBytesCount + lineBytes > maxBytes) {
|
|
154
197
|
truncatedBy = "bytes";
|
|
155
198
|
// Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,
|
|
156
199
|
// take the end of the line (partial)
|
|
157
|
-
if (
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
outputBytesCount = Buffer.byteLength(
|
|
161
|
-
lastLinePartial = true;
|
|
200
|
+
if (outputLines === 0) {
|
|
201
|
+
partialLine = truncateStringToBytesFromEnd(content.slice(start, end), maxBytes);
|
|
202
|
+
outputLines = 1;
|
|
203
|
+
outputBytesCount = Buffer.byteLength(partialLine, "utf-8");
|
|
162
204
|
}
|
|
163
205
|
break;
|
|
164
206
|
}
|
|
165
|
-
|
|
207
|
+
outputLines++;
|
|
166
208
|
outputBytesCount += lineBytes;
|
|
209
|
+
outputStart = start;
|
|
210
|
+
end = start - 1;
|
|
167
211
|
}
|
|
168
212
|
// If we exited due to line limit
|
|
169
|
-
if (
|
|
213
|
+
if (outputLines >= maxLines && outputBytesCount <= maxBytes) {
|
|
170
214
|
truncatedBy = "lines";
|
|
171
215
|
}
|
|
172
|
-
const outputContent = outputLinesArr.join("\n");
|
|
173
|
-
const finalOutputBytes = Buffer.byteLength(outputContent, "utf-8");
|
|
174
216
|
return {
|
|
175
|
-
content:
|
|
217
|
+
content: partialLine ?? copyString(content.slice(outputStart, contentEnd)),
|
|
176
218
|
truncated: true,
|
|
177
219
|
truncatedBy,
|
|
178
220
|
totalLines,
|
|
179
221
|
totalBytes,
|
|
180
|
-
outputLines
|
|
181
|
-
|
|
182
|
-
|
|
222
|
+
outputLines,
|
|
223
|
+
// A "\n" never splits a surrogate pair, so the lines' byte lengths add up to the output's.
|
|
224
|
+
outputBytes: outputBytesCount,
|
|
225
|
+
lastLinePartial: partialLine !== undefined,
|
|
183
226
|
firstLineExceedsLimit: false,
|
|
184
227
|
maxLines,
|
|
185
228
|
maxBytes,
|
|
@@ -218,7 +261,7 @@ export function truncateLine(line, maxChars = GREP_MAX_LINE_LENGTH) {
|
|
|
218
261
|
*/
|
|
219
262
|
export function truncateMiddle(content, maxBytes) {
|
|
220
263
|
const buf = Buffer.from(content, "utf-8");
|
|
221
|
-
const totalLines =
|
|
264
|
+
const totalLines = countLines(content);
|
|
222
265
|
if (buf.length <= maxBytes) {
|
|
223
266
|
return { content, truncated: false, removedChars: 0, totalBytes: buf.length, totalLines };
|
|
224
267
|
}
|
|
@@ -232,7 +275,12 @@ export function truncateMiddle(content, maxBytes) {
|
|
|
232
275
|
tailStart++;
|
|
233
276
|
const head = buf.subarray(0, headEnd).toString("utf-8");
|
|
234
277
|
const tail = buf.subarray(tailStart).toString("utf-8");
|
|
235
|
-
|
|
278
|
+
// Characters are code points: UTF-16 units less one per surrogate pair. Counted with native string searches
|
|
279
|
+
// instead of building an array of every removed character, which can be megabytes of them.
|
|
280
|
+
const removed = buf.subarray(headEnd, tailStart).toString("utf-8");
|
|
281
|
+
let removedChars = removed.length;
|
|
282
|
+
if (SURROGATE.test(removed))
|
|
283
|
+
removedChars -= removed.match(SURROGATE_PAIRS)?.length ?? 0;
|
|
236
284
|
return {
|
|
237
285
|
content: `${head}…${removedChars} chars truncated…${tail}`,
|
|
238
286
|
truncated: true,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"truncate.js","sourceRoot":"","sources":["../../../src/core/tools/truncate.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAA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G,CAAC,CAAC,CAAC,CAAC;IACnE,OAAO,SAAS,GAAG,GAAG,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC;QAAE,SAAS,EAAE,CAAC;IACrE,MAAM,IAAI,GAAG,GAAG,CAAC,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACxD,MAAM,IAAI,GAAG,GAAG,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACvD,MAAM,YAAY,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC;IAC3F,OAAO;QACN,OAAO,EAAE,GAAG,IAAI,IAAI,YAAY,oBAAoB,IAAI,EAAE;QAC1D,SAAS,EAAE,IAAI;QACf,YAAY;QACZ,UAAU,EAAE,GAAG,CAAC,MAAM;QACtB,UAAU;KACV,CAAC;AACH,CAAC","sourcesContent":["/**\n * Shared truncation utilities for tool outputs.\n *\n * Truncation is based on two independent limits - whichever is hit first wins:\n * - Line limit (default: 2000 lines)\n * - Byte limit (default: 50KB)\n *\n * Never returns partial lines (except bash tail truncation edge case).\n */\n\nexport const DEFAULT_MAX_LINES = 2000;\nexport const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB\nexport const GREP_MAX_LINE_LENGTH = 500; // Max chars per grep match line\n\nexport interface TruncationResult {\n\t/** The truncated content */\n\tcontent: string;\n\t/** Whether truncation occurred */\n\ttruncated: boolean;\n\t/** Which limit was hit: \"lines\", \"bytes\", or null if not truncated */\n\ttruncatedBy: \"lines\" | \"bytes\" | null;\n\t/** Total number of lines in the original content */\n\ttotalLines: number;\n\t/** Total number of bytes in the original content */\n\ttotalBytes: number;\n\t/** Number of complete lines in the truncated output */\n\toutputLines: number;\n\t/** Number of bytes in the truncated output */\n\toutputBytes: number;\n\t/** Whether the last line was partially truncated (only for tail truncation edge case) */\n\tlastLinePartial: boolean;\n\t/** Whether the first line exceeded the byte limit (for head truncation) */\n\tfirstLineExceedsLimit: boolean;\n\t/** The max lines limit that was applied */\n\tmaxLines: number;\n\t/** The max bytes limit that was applied */\n\tmaxBytes: number;\n}\n\nexport interface TruncationOptions {\n\t/** Maximum number of lines (default: 2000) */\n\tmaxLines?: number;\n\t/** Maximum number of bytes (default: 50KB) */\n\tmaxBytes?: number;\n}\n\nfunction splitLinesForCounting(content: string): string[] {\n\tif (content.length === 0) {\n\t\treturn [];\n\t}\n\tconst lines = content.split(\"\\n\");\n\tif (content.endsWith(\"\\n\")) {\n\t\tlines.pop();\n\t}\n\treturn lines;\n}\n\n/**\n * Format bytes as human-readable size.\n */\nexport function formatSize(bytes: number): string {\n\tif (bytes < 1024) {\n\t\treturn `${bytes}B`;\n\t} else if (bytes < 1024 * 1024) {\n\t\treturn `${(bytes / 1024).toFixed(1)}KB`;\n\t} else {\n\t\treturn `${(bytes / (1024 * 1024)).toFixed(1)}MB`;\n\t}\n}\n\n/**\n * Truncate content from the head (keep first N lines/bytes).\n * Suitable for file reads where you want to see the beginning.\n *\n * Never returns partial lines. If first line exceeds byte limit,\n * returns empty content with firstLineExceedsLimit=true.\n */\nexport function truncateHead(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = Buffer.byteLength(content, \"utf-8\");\n\tconst lines = splitLinesForCounting(content);\n\tconst totalLines = lines.length;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Check if first line alone exceeds byte limit\n\tconst firstLineBytes = Buffer.byteLength(lines[0], \"utf-8\");\n\tif (firstLineBytes > maxBytes) {\n\t\treturn {\n\t\t\tcontent: \"\",\n\t\t\ttruncated: true,\n\t\t\ttruncatedBy: \"bytes\",\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: 0,\n\t\t\toutputBytes: 0,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: true,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Collect complete lines that fit\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\n\tfor (let i = 0; i < lines.length && i < maxLines; i++) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = Buffer.byteLength(line, \"utf-8\") + (i > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.push(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = Buffer.byteLength(outputContent, \"utf-8\");\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial: false,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n\n/**\n * Truncate content from the tail (keep last N lines/bytes).\n * Suitable for bash output where you want to see the end (errors, final results).\n *\n * May return partial first line if the last line of original content exceeds byte limit.\n */\nexport function truncateTail(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = Buffer.byteLength(content, \"utf-8\");\n\tconst lines = splitLinesForCounting(content);\n\tconst totalLines = lines.length;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Work backwards from the end\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\tlet lastLinePartial = false;\n\n\tfor (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = Buffer.byteLength(line, \"utf-8\") + (outputLinesArr.length > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\t// Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,\n\t\t\t// take the end of the line (partial)\n\t\t\tif (outputLinesArr.length === 0) {\n\t\t\t\tconst truncatedLine = truncateStringToBytesFromEnd(line, maxBytes);\n\t\t\t\toutputLinesArr.unshift(truncatedLine);\n\t\t\t\toutputBytesCount = Buffer.byteLength(truncatedLine, \"utf-8\");\n\t\t\t\tlastLinePartial = true;\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.unshift(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = Buffer.byteLength(outputContent, \"utf-8\");\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n\n/**\n * Truncate a string to fit within a byte limit (from the end).\n * Handles multi-byte UTF-8 characters correctly.\n */\nfunction truncateStringToBytesFromEnd(str: string, maxBytes: number): string {\n\tconst buf = Buffer.from(str, \"utf-8\");\n\tif (buf.length <= maxBytes) {\n\t\treturn str;\n\t}\n\n\t// Start from the end, skip maxBytes back\n\tlet start = buf.length - maxBytes;\n\n\t// Find a valid UTF-8 boundary (start of a character)\n\twhile (start < buf.length && (buf[start] & 0xc0) === 0x80) {\n\t\tstart++;\n\t}\n\n\treturn buf.slice(start).toString(\"utf-8\");\n}\n\n/**\n * Truncate a single line to max characters, adding [truncated] suffix.\n * Used for grep match lines.\n */\nexport function truncateLine(\n\tline: string,\n\tmaxChars: number = GREP_MAX_LINE_LENGTH,\n): { text: string; wasTruncated: boolean } {\n\tif (line.length <= maxChars) {\n\t\treturn { text: line, wasTruncated: false };\n\t}\n\treturn { text: `${line.slice(0, maxChars)}... [truncated]`, wasTruncated: true };\n}\n\nexport interface MiddleTruncationResult {\n\t/** The start and end of the content with a `…N chars truncated…` marker between them. */\n\tcontent: string;\n\ttruncated: boolean;\n\t/** Characters left out. */\n\tremovedChars: number;\n\ttotalBytes: number;\n\ttotalLines: number;\n}\n\n/**\n * Keep the start and the end of `content`, half of `maxBytes` each, and replace the middle with a\n * `…N chars truncated…` marker, like Codex does for tool output. Cuts only at character boundaries.\n */\nexport function truncateMiddle(content: string, maxBytes: number): MiddleTruncationResult {\n\tconst buf = Buffer.from(content, \"utf-8\");\n\tconst totalLines = splitLinesForCounting(content).length;\n\tif (buf.length <= maxBytes) {\n\t\treturn { content, truncated: false, removedChars: 0, totalBytes: buf.length, totalLines };\n\t}\n\t// Continuation bytes (10xxxxxx) are not character starts.\n\tconst isBoundary = (index: number) => index >= buf.length || (buf[index] & 0xc0) !== 0x80;\n\tlet headEnd = Math.floor(maxBytes / 2);\n\twhile (headEnd > 0 && !isBoundary(headEnd)) headEnd--;\n\tlet tailStart = buf.length - (maxBytes - Math.floor(maxBytes / 2));\n\twhile (tailStart < buf.length && !isBoundary(tailStart)) tailStart++;\n\tconst head = buf.subarray(0, headEnd).toString(\"utf-8\");\n\tconst tail = buf.subarray(tailStart).toString(\"utf-8\");\n\tconst removedChars = Array.from(buf.subarray(headEnd, tailStart).toString(\"utf-8\")).length;\n\treturn {\n\t\tcontent: `${head}…${removedChars} chars truncated…${tail}`,\n\t\ttruncated: true,\n\t\tremovedChars,\n\t\ttotalBytes: buf.length,\n\t\ttotalLines,\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"truncate.js","sourceRoot":"","sources":["../../../src/core/tools/truncate.ts"],"names":[],"mappings":"AAAA,mHAAmH;AACnH;;;;;;;;GAQG;AAEH,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,CAAC;AACtC,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,OAAO;AACnD,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAG,CAAC,CAAC,gCAAgC;AAkCzE;;;;GAIG;AACH,SAAS,UAAU,CAAC,OAAe;IAClC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,CAAC,CAAC;IACV,CAAC;IACD,IAAI,KAAK,G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,CAAC,CAAC;IACvC,OAAO,OAAO,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC;QAAE,OAAO,EAAE,CAAC;IACtD,IAAI,SAAS,GAAG,GAAG,CAAC,MAAM,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC;IACnE,OAAO,SAAS,GAAG,GAAG,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC;QAAE,SAAS,EAAE,CAAC;IACrE,MAAM,IAAI,GAAG,GAAG,CAAC,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACxD,MAAM,IAAI,GAAG,GAAG,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACvD,4GAA4G;IAC5G,2FAA2F;IAC3F,MAAM,OAAO,GAAG,GAAG,CAAC,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACnE,IAAI,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC;IAClC,IAAI,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC;QAAE,YAAY,IAAI,OAAO,CAAC,KAAK,CAAC,eAAe,CAAC,EAAE,MAAM,IAAI,CAAC,CAAC;IACzF,OAAO;QACN,OAAO,EAAE,GAAG,IAAI,IAAI,YAAY,oBAAoB,IAAI,EAAE;QAC1D,SAAS,EAAE,IAAI;QACf,YAAY;QACZ,UAAU,EAAE,GAAG,CAAC,MAAM;QACtB,UAAU;KACV,CAAC;AACH,CAAC","sourcesContent":["// Derived from Pi-Bolt (MIT, Copyright (c) 2026 OpenSec (Pi-Bolt)), commit b93895d2; modified for HUMAIN Code CLI.\n/**\n * Shared truncation utilities for tool outputs.\n *\n * Truncation is based on two independent limits - whichever is hit first wins:\n * - Line limit (default: 2000 lines)\n * - Byte limit (default: 50KB)\n *\n * Never returns partial lines (except bash tail truncation edge case).\n */\n\nexport const DEFAULT_MAX_LINES = 2000;\nexport const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB\nexport const GREP_MAX_LINE_LENGTH = 500; // Max chars per grep match line\n\nexport interface TruncationResult {\n\t/** The truncated content */\n\tcontent: string;\n\t/** Whether truncation occurred */\n\ttruncated: boolean;\n\t/** Which limit was hit: \"lines\", \"bytes\", or null if not truncated */\n\ttruncatedBy: \"lines\" | \"bytes\" | null;\n\t/** Total number of lines in the original content */\n\ttotalLines: number;\n\t/** Total number of bytes in the original content */\n\ttotalBytes: number;\n\t/** Number of complete lines in the truncated output */\n\toutputLines: number;\n\t/** Number of bytes in the truncated output */\n\toutputBytes: number;\n\t/** Whether the last line was partially truncated (only for tail truncation edge case) */\n\tlastLinePartial: boolean;\n\t/** Whether the first line exceeded the byte limit (for head truncation) */\n\tfirstLineExceedsLimit: boolean;\n\t/** The max lines limit that was applied */\n\tmaxLines: number;\n\t/** The max bytes limit that was applied */\n\tmaxBytes: number;\n}\n\nexport interface TruncationOptions {\n\t/** Maximum number of lines (default: 2000) */\n\tmaxLines?: number;\n\t/** Maximum number of bytes (default: 50KB) */\n\tmaxBytes?: number;\n}\n\n/**\n * Number of lines in `content`: one per \"\\n\"-separated line, not counting the empty one after a trailing \"\\n\".\n * Counted without splitting: callers truncate output that can be many megabytes to a few kilobytes, and splitting\n * would allocate a string per line of all of it.\n */\nfunction countLines(content: string): number {\n\tif (content.length === 0) {\n\t\treturn 0;\n\t}\n\tlet lines = 1;\n\tfor (let i = content.indexOf(\"\\n\"); i !== -1; i = content.indexOf(\"\\n\", i + 1)) {\n\t\tlines++;\n\t}\n\treturn content.endsWith(\"\\n\") ? lines - 1 : lines;\n}\n\nconst NON_ASCII = /[\\u0080-\\uffff]/g;\nconst SURROGATE = /[\\ud800-\\udfff]/;\nconst SURROGATE_PAIRS = /[\\ud800-\\udbff][\\udc00-\\udfff]/g;\n\n/** Index of the first non-ASCII character of `content` at or after `from`, or its length if there is none. */\nfunction findNonAscii(content: string, from: number): number {\n\tNON_ASCII.lastIndex = from;\n\treturn NON_ASCII.exec(content)?.index ?? content.length;\n}\n\n/**\n * A copy of `text` that shares no memory with the string it was cut from, so a short result does not keep a large\n * input alive. UTF-16 code units round-trip unchanged, lone surrogates included.\n */\nfunction copyString(text: string): string {\n\treturn Buffer.from(text, \"utf16le\").toString(\"utf16le\");\n}\n\n/**\n * Format bytes as human-readable size.\n */\nexport function formatSize(bytes: number): string {\n\tif (bytes < 1024) {\n\t\treturn `${bytes}B`;\n\t} else if (bytes < 1024 * 1024) {\n\t\treturn `${(bytes / 1024).toFixed(1)}KB`;\n\t} else {\n\t\treturn `${(bytes / (1024 * 1024)).toFixed(1)}MB`;\n\t}\n}\n\n/**\n * Truncate content from the head (keep first N lines/bytes).\n * Suitable for file reads where you want to see the beginning.\n *\n * Never returns partial lines. If first line exceeds byte limit,\n * returns empty content with firstLineExceedsLimit=true.\n */\nexport function truncateHead(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = Buffer.byteLength(content, \"utf-8\");\n\tconst totalLines = countLines(content);\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Check if first line alone exceeds byte limit\n\t// Every non-ASCII character takes more UTF-8 bytes than UTF-16 units, so equal lengths mean the content is ASCII and\n\t// a line's byte length is its length.\n\tconst ascii = totalBytes === content.length;\n\tconst firstNewline = content.indexOf(\"\\n\");\n\tconst firstLineEnd = firstNewline === -1 ? content.length : firstNewline;\n\tconst firstLineBytes = ascii ? firstLineEnd : Buffer.byteLength(content.slice(0, firstLineEnd), \"utf-8\");\n\tif (firstLineBytes > maxBytes) {\n\t\treturn {\n\t\t\tcontent: \"\",\n\t\t\ttruncated: true,\n\t\t\ttruncatedBy: \"bytes\",\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: 0,\n\t\t\toutputBytes: 0,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: true,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Count the complete lines that fit. They are a prefix of `content`, which is cut once instead of slicing every\n\t// line and joining them again. Only lines that contain a non-ASCII character are measured with Buffer.byteLength.\n\tlet outputLines = 0;\n\tlet outputBytesCount = 0;\n\tlet outputEnd = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\tlet nextNonAscii = ascii ? content.length : findNonAscii(content, 0);\n\n\tfor (let start = 0; outputLines < totalLines && outputLines < maxLines; ) {\n\t\tconst newline = content.indexOf(\"\\n\", start);\n\t\tconst end = newline === -1 ? content.length : newline;\n\t\tlet lineBytes: number;\n\t\tif (end <= nextNonAscii) {\n\t\t\tlineBytes = end - start;\n\t\t} else {\n\t\t\tlineBytes = Buffer.byteLength(content.slice(start, end), \"utf-8\");\n\t\t\tnextNonAscii = findNonAscii(content, end);\n\t\t}\n\t\tif (outputLines > 0) lineBytes++; // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLines++;\n\t\toutputBytesCount += lineBytes;\n\t\toutputEnd = end;\n\t\tstart = end + 1;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLines >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\treturn {\n\t\tcontent: copyString(content.slice(0, outputEnd)),\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines,\n\t\t// A \"\\n\" never splits a surrogate pair, so the lines' byte lengths add up to the output's.\n\t\toutputBytes: outputBytesCount,\n\t\tlastLinePartial: false,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n\n/**\n * Truncate content from the tail (keep last N lines/bytes).\n * Suitable for bash output where you want to see the end (errors, final results).\n *\n * May return partial first line if the last line of original content exceeds byte limit.\n */\nexport function truncateTail(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = Buffer.byteLength(content, \"utf-8\");\n\tconst totalLines = countLines(content);\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Work backwards from the end. A trailing \"\\n\" ends the last line. The kept lines are a suffix of the content up to\n\t// that \"\\n\", which is cut once instead of slicing every line and joining them again.\n\tconst ascii = totalBytes === content.length;\n\tconst contentEnd = content.endsWith(\"\\n\") ? content.length - 1 : content.length;\n\tlet outputLines = 0;\n\tlet outputBytesCount = 0;\n\tlet outputStart = contentEnd;\n\tlet partialLine: string | undefined;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\n\tlet end = contentEnd;\n\tfor (let i = totalLines - 1; i >= 0 && outputLines < maxLines; i--) {\n\t\tconst start = i === 0 ? 0 : content.lastIndexOf(\"\\n\", end - 1) + 1;\n\t\tconst lineBytes =\n\t\t\t(ascii ? end - start : Buffer.byteLength(content.slice(start, end), \"utf-8\")) + (outputLines > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\t// Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,\n\t\t\t// take the end of the line (partial)\n\t\t\tif (outputLines === 0) {\n\t\t\t\tpartialLine = truncateStringToBytesFromEnd(content.slice(start, end), maxBytes);\n\t\t\t\toutputLines = 1;\n\t\t\t\toutputBytesCount = Buffer.byteLength(partialLine, \"utf-8\");\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLines++;\n\t\toutputBytesCount += lineBytes;\n\t\toutputStart = start;\n\t\tend = start - 1;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLines >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\treturn {\n\t\tcontent: partialLine ?? copyString(content.slice(outputStart, contentEnd)),\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines,\n\t\t// A \"\\n\" never splits a surrogate pair, so the lines' byte lengths add up to the output's.\n\t\toutputBytes: outputBytesCount,\n\t\tlastLinePartial: partialLine !== undefined,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n\n/**\n * Truncate a string to fit within a byte limit (from the end).\n * Handles multi-byte UTF-8 characters correctly.\n */\nfunction truncateStringToBytesFromEnd(str: string, maxBytes: number): string {\n\tconst buf = Buffer.from(str, \"utf-8\");\n\tif (buf.length <= maxBytes) {\n\t\treturn str;\n\t}\n\n\t// Start from the end, skip maxBytes back\n\tlet start = buf.length - maxBytes;\n\n\t// Find a valid UTF-8 boundary (start of a character)\n\twhile (start < buf.length && (buf[start] & 0xc0) === 0x80) {\n\t\tstart++;\n\t}\n\n\treturn buf.slice(start).toString(\"utf-8\");\n}\n\n/**\n * Truncate a single line to max characters, adding [truncated] suffix.\n * Used for grep match lines.\n */\nexport function truncateLine(\n\tline: string,\n\tmaxChars: number = GREP_MAX_LINE_LENGTH,\n): { text: string; wasTruncated: boolean } {\n\tif (line.length <= maxChars) {\n\t\treturn { text: line, wasTruncated: false };\n\t}\n\treturn { text: `${line.slice(0, maxChars)}... [truncated]`, wasTruncated: true };\n}\n\nexport interface MiddleTruncationResult {\n\t/** The start and end of the content with a `…N chars truncated…` marker between them. */\n\tcontent: string;\n\ttruncated: boolean;\n\t/** Characters left out. */\n\tremovedChars: number;\n\ttotalBytes: number;\n\ttotalLines: number;\n}\n\n/**\n * Keep the start and the end of `content`, half of `maxBytes` each, and replace the middle with a\n * `…N chars truncated…` marker, like Codex does for tool output. Cuts only at character boundaries.\n */\nexport function truncateMiddle(content: string, maxBytes: number): MiddleTruncationResult {\n\tconst buf = Buffer.from(content, \"utf-8\");\n\tconst totalLines = countLines(content);\n\tif (buf.length <= maxBytes) {\n\t\treturn { content, truncated: false, removedChars: 0, totalBytes: buf.length, totalLines };\n\t}\n\t// Continuation bytes (10xxxxxx) are not character starts.\n\tconst isBoundary = (index: number) => index >= buf.length || (buf[index] & 0xc0) !== 0x80;\n\tlet headEnd = Math.floor(maxBytes / 2);\n\twhile (headEnd > 0 && !isBoundary(headEnd)) headEnd--;\n\tlet tailStart = buf.length - (maxBytes - Math.floor(maxBytes / 2));\n\twhile (tailStart < buf.length && !isBoundary(tailStart)) tailStart++;\n\tconst head = buf.subarray(0, headEnd).toString(\"utf-8\");\n\tconst tail = buf.subarray(tailStart).toString(\"utf-8\");\n\t// Characters are code points: UTF-16 units less one per surrogate pair. Counted with native string searches\n\t// instead of building an array of every removed character, which can be megabytes of them.\n\tconst removed = buf.subarray(headEnd, tailStart).toString(\"utf-8\");\n\tlet removedChars = removed.length;\n\tif (SURROGATE.test(removed)) removedChars -= removed.match(SURROGATE_PAIRS)?.length ?? 0;\n\treturn {\n\t\tcontent: `${head}…${removedChars} chars truncated…${tail}`,\n\t\ttruncated: true,\n\t\tremovedChars,\n\t\ttotalBytes: buf.length,\n\t\ttotalLines,\n\t};\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Usage } from "@earendil-works/pi-ai/compat";
|
|
2
|
-
import type { SessionEntry } from "./session-manager.ts";
|
|
2
|
+
import type { SessionEntry, UsageEntry } from "./session-manager.ts";
|
|
3
3
|
export interface UsageTotals {
|
|
4
4
|
usageIncomplete?: boolean;
|
|
5
5
|
input: number;
|
|
@@ -12,6 +12,13 @@ export declare function createUsageTotals(): UsageTotals;
|
|
|
12
12
|
export declare function addUsageToTotals(totals: UsageTotals, usage: Usage): void;
|
|
13
13
|
/** Sum of two usages, keeping the optional token splits when either side reports them. */
|
|
14
14
|
export declare function combineUsage(first: Usage, second: Usage): Usage;
|
|
15
|
+
/** Linked summaries contribute only evidence not already accounted for by their distinct owners. */
|
|
16
|
+
export declare function getSummaryUsage(usage: Partial<Usage> | undefined, ids: unknown, kind: string, owners: readonly UsageEntry[]): Usage | undefined;
|
|
17
|
+
/** Accounting projection shared by session statistics, cost breakdown, and footer. */
|
|
18
|
+
export declare function getSessionUsage(entries: SessionEntry[]): {
|
|
19
|
+
entry: SessionEntry;
|
|
20
|
+
usage: Usage;
|
|
21
|
+
}[];
|
|
15
22
|
export interface UsageCostBreakdownEntry {
|
|
16
23
|
usageIncomplete?: boolean;
|
|
17
24
|
key: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usage-totals.d.ts","sourceRoot":"","sources":["../../src/core/usage-totals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"usage-totals.d.ts","sourceRoot":"","s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 -40,27 +40,123 @@ export function combineUsage(first, second) {
|
|
|
40
40
|
},
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
|
+
function normalizeUsage(usage) {
|
|
44
|
+
const result = {
|
|
45
|
+
input: 0,
|
|
46
|
+
output: 0,
|
|
47
|
+
cacheRead: 0,
|
|
48
|
+
cacheWrite: 0,
|
|
49
|
+
totalTokens: 0,
|
|
50
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
51
|
+
...(usage?.usageIncomplete ? { usageIncomplete: true } : {}),
|
|
52
|
+
};
|
|
53
|
+
for (const key of ["input", "output", "cacheRead", "cacheWrite"]) {
|
|
54
|
+
const count = usage?.[key];
|
|
55
|
+
if (typeof count === "number" && Number.isSafeInteger(count) && count >= 0)
|
|
56
|
+
result[key] = count;
|
|
57
|
+
else
|
|
58
|
+
result.usageIncomplete = true;
|
|
59
|
+
const cost = usage?.cost?.[key];
|
|
60
|
+
if (typeof cost === "number" && Number.isFinite(cost) && cost >= 0)
|
|
61
|
+
result.cost[key] = cost;
|
|
62
|
+
else
|
|
63
|
+
result.usageIncomplete = true;
|
|
64
|
+
}
|
|
65
|
+
const totalTokens = usage?.totalTokens;
|
|
66
|
+
if (typeof totalTokens !== "number" || !Number.isSafeInteger(totalTokens) || totalTokens < 0)
|
|
67
|
+
result.usageIncomplete = true;
|
|
68
|
+
result.totalTokens = result.input + result.output + result.cacheRead + result.cacheWrite;
|
|
69
|
+
const cost = usage?.cost?.total;
|
|
70
|
+
if (typeof cost === "number" && Number.isFinite(cost) && cost >= 0)
|
|
71
|
+
result.cost.total = cost;
|
|
72
|
+
else
|
|
73
|
+
result.usageIncomplete = true;
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
/** Linked summaries contribute only evidence not already accounted for by their distinct owners. */
|
|
77
|
+
export function getSummaryUsage(usage, ids, kind, owners) {
|
|
78
|
+
if (ids === undefined || (Array.isArray(ids) && ids.length === 0))
|
|
79
|
+
return usage;
|
|
80
|
+
const remaining = normalizeUsage(usage);
|
|
81
|
+
const ownerIds = Array.isArray(ids) ? [...new Set(ids)] : [];
|
|
82
|
+
let covered = ownerIds.length > 0;
|
|
83
|
+
const observed = createUsageTotals();
|
|
84
|
+
for (const id of ownerIds) {
|
|
85
|
+
const owner = typeof id === "string" && id.length > 0 ? owners.find((entry) => entry.id === id) : undefined;
|
|
86
|
+
if (!owner || owner.kind !== kind) {
|
|
87
|
+
covered = false;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const value = normalizeUsage(owner.usage);
|
|
91
|
+
if (typeof owner.provider !== "string" ||
|
|
92
|
+
owner.provider.length === 0 ||
|
|
93
|
+
typeof owner.model !== "string" ||
|
|
94
|
+
owner.model.length === 0 ||
|
|
95
|
+
value.usageIncomplete)
|
|
96
|
+
covered = false;
|
|
97
|
+
addUsageToTotals(observed, value);
|
|
98
|
+
}
|
|
99
|
+
if (covered && !usage)
|
|
100
|
+
return undefined;
|
|
101
|
+
if (covered && !usage?.usageIncomplete)
|
|
102
|
+
delete remaining.usageIncomplete;
|
|
103
|
+
const residual = remaining.cost.total - observed.cost;
|
|
104
|
+
// Sum order changes floating-point USD; drift is not another charge.
|
|
105
|
+
const drift = Number.EPSILON * Math.max(remaining.cost.total, observed.cost) * (ownerIds.length + 1);
|
|
106
|
+
remaining.cost.total = residual > drift ? residual : 0;
|
|
107
|
+
for (const key of ["input", "output", "cacheRead", "cacheWrite"])
|
|
108
|
+
remaining[key] = Math.max(0, remaining[key] - observed[key]);
|
|
109
|
+
remaining.totalTokens = remaining.input + remaining.output + remaining.cacheRead + remaining.cacheWrite;
|
|
110
|
+
if (!covered || remaining.cost.total > 0 || remaining.totalTokens > 0)
|
|
111
|
+
remaining.usageIncomplete = true;
|
|
112
|
+
return remaining.usageIncomplete || remaining.cost.total > 0 || remaining.totalTokens > 0 ? remaining : undefined;
|
|
113
|
+
}
|
|
114
|
+
/** Accounting projection shared by session statistics, cost breakdown, and footer. */
|
|
115
|
+
export function getSessionUsage(entries) {
|
|
116
|
+
const owners = entries.filter((entry) => entry.type === "usage");
|
|
117
|
+
const seen = new Set();
|
|
118
|
+
const summaries = new Map();
|
|
119
|
+
const result = [];
|
|
120
|
+
for (const entry of entries) {
|
|
121
|
+
let usage;
|
|
122
|
+
if (entry.type === "usage") {
|
|
123
|
+
if (seen.has(entry.id))
|
|
124
|
+
continue;
|
|
125
|
+
seen.add(entry.id);
|
|
126
|
+
usage = normalizeUsage(entry.usage);
|
|
127
|
+
}
|
|
128
|
+
else if (entry.type === "message" &&
|
|
129
|
+
(entry.message.role === "assistant" || entry.message.role === "toolResult")) {
|
|
130
|
+
usage = entry.message.usage;
|
|
131
|
+
}
|
|
132
|
+
else if (entry.type === "compaction" || entry.type === "branch_summary") {
|
|
133
|
+
usage = getSummaryUsage(entry.usage, entry.usageEntryIds, entry.type === "compaction" ? "compaction" : "branchSummary", owners);
|
|
134
|
+
if (Array.isArray(entry.usageEntryIds) && entry.usageEntryIds.length > 0) {
|
|
135
|
+
const key = JSON.stringify([entry.type, [...new Set(entry.usageEntryIds)].sort()]);
|
|
136
|
+
const previous = summaries.get(key);
|
|
137
|
+
if (usage && (!previous || usage.cost.total >= previous.usage.cost.total))
|
|
138
|
+
summaries.set(key, { entry, usage });
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (usage)
|
|
143
|
+
result.push({ entry, usage });
|
|
144
|
+
}
|
|
145
|
+
return [...result, ...summaries.values()];
|
|
146
|
+
}
|
|
43
147
|
/** Group model-attributed usage by model and all other usage into a separate bucket. */
|
|
44
148
|
export function getUsageCostBreakdown(entries) {
|
|
45
149
|
const totalsByKey = new Map();
|
|
46
|
-
for (const entry of entries) {
|
|
150
|
+
for (const { entry, usage } of getSessionUsage(entries)) {
|
|
47
151
|
let key;
|
|
48
|
-
let usage;
|
|
49
152
|
if (entry.type === "message" && entry.message.role === "assistant") {
|
|
50
153
|
key = `${entry.message.provider}/${entry.message.responseModel ?? entry.message.model}`;
|
|
51
|
-
usage = entry.message.usage;
|
|
52
154
|
}
|
|
53
155
|
else if (entry.type === "usage") {
|
|
54
156
|
key = `${entry.provider}/${entry.model}`;
|
|
55
|
-
usage = entry.usage;
|
|
56
|
-
}
|
|
57
|
-
else if (entry.type === "message" && entry.message.role === "toolResult" && entry.message.usage) {
|
|
58
|
-
key = "Tools/summaries";
|
|
59
|
-
usage = entry.message.usage;
|
|
60
157
|
}
|
|
61
|
-
else
|
|
158
|
+
else {
|
|
62
159
|
key = "Tools/summaries";
|
|
63
|
-
usage = entry.usage;
|
|
64
160
|
}
|
|
65
161
|
if (!key || !usage)
|
|
66
162
|
continue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usage-totals.js","sourceRoot":"","sources":["../../src/core/usage-totals.ts"],"names":[],"mappings":"AAYA,MAAM,UAAU,iBAAiB;IAChC,OAAO;QACN,KAAK,EAAE,CAAC;QACR,MAAM,EAAE,CAAC;QACT,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,CAAC;QACb,IAAI,EAAE,CAAC;KACP,CAAC;AACH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,MAAmB,EAAE,KAAY;IACjE,IAAI,KAAK,CAAC,eAAe;QAAE,MAAM,CAAC,eAAe,GAAG,IAAI,CAAC;IACzD,MAAM,CAAC,KAAK,IAAI,KAAK,CAAC,KAAK,CAAC;IAC5B,MAAM,CAAC,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC;IAC9B,MAAM,CAAC,SAAS,IAAI,KAAK,CAAC,SAAS,CAAC;IACpC,MAAM,CAAC,UAAU,IAAI,KAAK,CAAC,UAAU,CAAC;IACtC,MAAM,CAAC,IAAI,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;AACjC,CAAC;AAED,0FAA0F;AAC1F,MAAM,UAAU,YAAY,CAAC,KAAY,EAAE,MAAa;IACvD,OAAO;QACN,GAAG,CAAC,KAAK,CAAC,eAA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iB,CAAC;YACxB,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;QACrB,CAAC;QACD,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK;YAAE,SAAS;QAE7B,IAAI,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAClC,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,MAAM,GAAG,iBAAiB,EAAE,CAAC;YAC7B,WAAW,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QAC9B,CAAC;QACD,gBAAgB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC;QAClD,GAAG;QACH,GAAG,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5D,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,MAAM,EAAE,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,UAAU;KAC3E,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,eAAe,IAAI,KAAK,CAAC,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;SAC9E,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC","sourcesContent":["import type { Usage } from \"@earendil-works/pi-ai/compat\";\nimport type { SessionEntry } from \"./session-manager.ts\";\n\nexport interface UsageTotals {\n\tusageIncomplete?: boolean;\n\tinput: number;\n\toutput: number;\n\tcacheRead: number;\n\tcacheWrite: number;\n\tcost: number;\n}\n\nexport function createUsageTotals(): UsageTotals {\n\treturn {\n\t\tinput: 0,\n\t\toutput: 0,\n\t\tcacheRead: 0,\n\t\tcacheWrite: 0,\n\t\tcost: 0,\n\t};\n}\n\nexport function addUsageToTotals(totals: UsageTotals, usage: Usage): void {\n\tif (usage.usageIncomplete) totals.usageIncomplete = true;\n\ttotals.input += usage.input;\n\ttotals.output += usage.output;\n\ttotals.cacheRead += usage.cacheRead;\n\ttotals.cacheWrite += usage.cacheWrite;\n\ttotals.cost += usage.cost.total;\n}\n\n/** Sum of two usages, keeping the optional token splits when either side reports them. */\nexport function combineUsage(first: Usage, second: Usage): Usage {\n\treturn {\n\t\t...(first.usageIncomplete || second.usageIncomplete ? { usageIncomplete: true } : {}),\n\t\tinput: first.input + second.input,\n\t\toutput: first.output + second.output,\n\t\tcacheRead: first.cacheRead + second.cacheRead,\n\t\tcacheWrite: first.cacheWrite + second.cacheWrite,\n\t\t...(first.cacheWrite1h !== undefined || second.cacheWrite1h !== undefined\n\t\t\t? { cacheWrite1h: (first.cacheWrite1h ?? 0) + (second.cacheWrite1h ?? 0) }\n\t\t\t: {}),\n\t\t...(first.reasoning !== undefined || second.reasoning !== undefined\n\t\t\t? { reasoning: (first.reasoning ?? 0) + (second.reasoning ?? 0) }\n\t\t\t: {}),\n\t\ttotalTokens: first.totalTokens + second.totalTokens,\n\t\tcost: {\n\t\t\tinput: first.cost.input + second.cost.input,\n\t\t\toutput: first.cost.output + second.cost.output,\n\t\t\tcacheRead: first.cost.cacheRead + second.cost.cacheRead,\n\t\t\tcacheWrite: first.cost.cacheWrite + second.cost.cacheWrite,\n\t\t\ttotal: first.cost.total + second.cost.total,\n\t\t},\n\t};\n}\n\nexport interface UsageCostBreakdownEntry {\n\tusageIncomplete?: boolean;\n\tkey: string;\n\tcost: number;\n\ttokens: number;\n}\n\n/** Group model-attributed usage by model and all other usage into a separate bucket. */\nexport function getUsageCostBreakdown(entries: SessionEntry[]): UsageCostBreakdownEntry[] {\n\tconst totalsByKey = new Map<string, UsageTotals>();\n\n\tfor (const entry of entries) {\n\t\tlet key: string | undefined;\n\t\tlet usage: Usage | undefined;\n\t\tif (entry.type === \"message\" && entry.message.role === \"assistant\") {\n\t\t\tkey = `${entry.message.provider}/${entry.message.responseModel ?? entry.message.model}`;\n\t\t\tusage = entry.message.usage;\n\t\t} else if (entry.type === \"usage\") {\n\t\t\tkey = `${entry.provider}/${entry.model}`;\n\t\t\tusage = entry.usage;\n\t\t} else if (entry.type === \"message\" && entry.message.role === \"toolResult\" && entry.message.usage) {\n\t\t\tkey = \"Tools/summaries\";\n\t\t\tusage = entry.message.usage;\n\t\t} else if ((entry.type === \"branch_summary\" || entry.type === \"compaction\") && entry.usage) {\n\t\t\tkey = \"Tools/summaries\";\n\t\t\tusage = entry.usage;\n\t\t}\n\t\tif (!key || !usage) continue;\n\n\t\tlet totals = totalsByKey.get(key);\n\t\tif (!totals) {\n\t\t\ttotals = createUsageTotals();\n\t\t\ttotalsByKey.set(key, totals);\n\t\t}\n\t\taddUsageToTotals(totals, usage);\n\t}\n\n\treturn Array.from(totalsByKey, ([key, totals]) => ({\n\t\tkey,\n\t\t...(totals.usageIncomplete ? { usageIncomplete: true } : {}),\n\t\tcost: totals.cost,\n\t\ttokens: totals.input + totals.output + totals.cacheRead + totals.cacheWrite,\n\t}))\n\t\t.filter((entry) => entry.usageIncomplete || entry.cost > 0 || entry.tokens > 0)\n\t\t.sort((a, b) => b.cost - a.cost);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"usage-totals.js","sourceRoot":"","sources":["../../src/core/usage-totals.ts"],"names":[],"mappings":"AAYA,MAAM,UAAU,iBAAiB;IAChC,OAAO;QACN,KAAK,EAAE,CAAC;QACR,MAAM,EAAE,CAAC;QACT,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,CAAC;QACb,IAAI,EAAE,CAAC;KACP,CAAC;AACH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,MAAmB,EAAE,KAAY;IACjE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AAG,GAAG,KAAK,CAAC,QAAQ,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;QAC1C,CAAC;aAAM,CAAC;YACP,GAAG,GAAG,iBAAiB,CAAC;QACzB,CAAC;QACD,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK;YAAE,SAAS;QAE7B,IAAI,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAClC,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,MAAM,GAAG,iBAAiB,EAAE,CAAC;YAC7B,WAAW,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QAC9B,CAAC;QACD,gBAAgB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC;QAClD,GAAG;QACH,GAAG,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5D,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,MAAM,EAAE,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,UAAU;KAC3E,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,eAAe,IAAI,KAAK,CAAC,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;SAC9E,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC","sourcesContent":["import type { Usage } from \"@earendil-works/pi-ai/compat\";\nimport type { SessionEntry, UsageEntry } from \"./session-manager.ts\";\n\nexport interface UsageTotals {\n\tusageIncomplete?: boolean;\n\tinput: number;\n\toutput: number;\n\tcacheRead: number;\n\tcacheWrite: number;\n\tcost: number;\n}\n\nexport function createUsageTotals(): UsageTotals {\n\treturn {\n\t\tinput: 0,\n\t\toutput: 0,\n\t\tcacheRead: 0,\n\t\tcacheWrite: 0,\n\t\tcost: 0,\n\t};\n}\n\nexport function addUsageToTotals(totals: UsageTotals, usage: Usage): void {\n\tif (usage.usageIncomplete) totals.usageIncomplete = true;\n\ttotals.input += usage.input;\n\ttotals.output += usage.output;\n\ttotals.cacheRead += usage.cacheRead;\n\ttotals.cacheWrite += usage.cacheWrite;\n\ttotals.cost += usage.cost.total;\n}\n\n/** Sum of two usages, keeping the optional token splits when either side reports them. */\nexport function combineUsage(first: Usage, second: Usage): Usage {\n\treturn {\n\t\t...(first.usageIncomplete || second.usageIncomplete ? { usageIncomplete: true } : {}),\n\t\tinput: first.input + second.input,\n\t\toutput: first.output + second.output,\n\t\tcacheRead: first.cacheRead + second.cacheRead,\n\t\tcacheWrite: first.cacheWrite + second.cacheWrite,\n\t\t...(first.cacheWrite1h !== undefined || second.cacheWrite1h !== undefined\n\t\t\t? { cacheWrite1h: (first.cacheWrite1h ?? 0) + (second.cacheWrite1h ?? 0) }\n\t\t\t: {}),\n\t\t...(first.reasoning !== undefined || second.reasoning !== undefined\n\t\t\t? { reasoning: (first.reasoning ?? 0) + (second.reasoning ?? 0) }\n\t\t\t: {}),\n\t\ttotalTokens: first.totalTokens + second.totalTokens,\n\t\tcost: {\n\t\t\tinput: first.cost.input + second.cost.input,\n\t\t\toutput: first.cost.output + second.cost.output,\n\t\t\tcacheRead: first.cost.cacheRead + second.cost.cacheRead,\n\t\t\tcacheWrite: first.cost.cacheWrite + second.cost.cacheWrite,\n\t\t\ttotal: first.cost.total + second.cost.total,\n\t\t},\n\t};\n}\n\nfunction normalizeUsage(usage: Partial<Usage> | undefined): Usage {\n\tconst result: Usage = {\n\t\tinput: 0,\n\t\toutput: 0,\n\t\tcacheRead: 0,\n\t\tcacheWrite: 0,\n\t\ttotalTokens: 0,\n\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t...(usage?.usageIncomplete ? { usageIncomplete: true } : {}),\n\t};\n\tfor (const key of [\"input\", \"output\", \"cacheRead\", \"cacheWrite\"] as const) {\n\t\tconst count = usage?.[key];\n\t\tif (typeof count === \"number\" && Number.isSafeInteger(count) && count >= 0) result[key] = count;\n\t\telse result.usageIncomplete = true;\n\t\tconst cost = usage?.cost?.[key];\n\t\tif (typeof cost === \"number\" && Number.isFinite(cost) && cost >= 0) result.cost[key] = cost;\n\t\telse result.usageIncomplete = true;\n\t}\n\tconst totalTokens = usage?.totalTokens;\n\tif (typeof totalTokens !== \"number\" || !Number.isSafeInteger(totalTokens) || totalTokens < 0)\n\t\tresult.usageIncomplete = true;\n\tresult.totalTokens = result.input + result.output + result.cacheRead + result.cacheWrite;\n\tconst cost = usage?.cost?.total;\n\tif (typeof cost === \"number\" && Number.isFinite(cost) && cost >= 0) result.cost.total = cost;\n\telse result.usageIncomplete = true;\n\treturn result;\n}\n\n/** Linked summaries contribute only evidence not already accounted for by their distinct owners. */\nexport function getSummaryUsage(\n\tusage: Partial<Usage> | undefined,\n\tids: unknown,\n\tkind: string,\n\towners: readonly UsageEntry[],\n): Usage | undefined {\n\tif (ids === undefined || (Array.isArray(ids) && ids.length === 0)) return usage as Usage | undefined;\n\tconst remaining = normalizeUsage(usage);\n\tconst ownerIds = Array.isArray(ids) ? [...new Set<unknown>(ids)] : [];\n\tlet covered = ownerIds.length > 0;\n\tconst observed = createUsageTotals();\n\tfor (const id of ownerIds) {\n\t\tconst owner = typeof id === \"string\" && id.length > 0 ? owners.find((entry) => entry.id === id) : undefined;\n\t\tif (!owner || owner.kind !== kind) {\n\t\t\tcovered = false;\n\t\t\tcontinue;\n\t\t}\n\t\tconst value = normalizeUsage(owner.usage);\n\t\tif (\n\t\t\ttypeof owner.provider !== \"string\" ||\n\t\t\towner.provider.length === 0 ||\n\t\t\ttypeof owner.model !== \"string\" ||\n\t\t\towner.model.length === 0 ||\n\t\t\tvalue.usageIncomplete\n\t\t)\n\t\t\tcovered = false;\n\t\taddUsageToTotals(observed, value);\n\t}\n\tif (covered && !usage) return undefined;\n\tif (covered && !usage?.usageIncomplete) delete remaining.usageIncomplete;\n\tconst residual = remaining.cost.total - observed.cost;\n\t// Sum order changes floating-point USD; drift is not another charge.\n\tconst drift = Number.EPSILON * Math.max(remaining.cost.total, observed.cost) * (ownerIds.length + 1);\n\tremaining.cost.total = residual > drift ? residual : 0;\n\tfor (const key of [\"input\", \"output\", \"cacheRead\", \"cacheWrite\"] as const)\n\t\tremaining[key] = Math.max(0, remaining[key] - observed[key]);\n\tremaining.totalTokens = remaining.input + remaining.output + remaining.cacheRead + remaining.cacheWrite;\n\tif (!covered || remaining.cost.total > 0 || remaining.totalTokens > 0) remaining.usageIncomplete = true;\n\treturn remaining.usageIncomplete || remaining.cost.total > 0 || remaining.totalTokens > 0 ? remaining : undefined;\n}\n\n/** Accounting projection shared by session statistics, cost breakdown, and footer. */\nexport function getSessionUsage(entries: SessionEntry[]): { entry: SessionEntry; usage: Usage }[] {\n\tconst owners = entries.filter((entry): entry is UsageEntry => entry.type === \"usage\");\n\tconst seen = new Set<string>();\n\tconst summaries = new Map<string, { entry: SessionEntry; usage: Usage }>();\n\tconst result: { entry: SessionEntry; usage: Usage }[] = [];\n\tfor (const entry of entries) {\n\t\tlet usage: Usage | undefined;\n\t\tif (entry.type === \"usage\") {\n\t\t\tif (seen.has(entry.id)) continue;\n\t\t\tseen.add(entry.id);\n\t\t\tusage = normalizeUsage(entry.usage);\n\t\t} else if (\n\t\t\tentry.type === \"message\" &&\n\t\t\t(entry.message.role === \"assistant\" || entry.message.role === \"toolResult\")\n\t\t) {\n\t\t\tusage = entry.message.usage;\n\t\t} else if (entry.type === \"compaction\" || entry.type === \"branch_summary\") {\n\t\t\tusage = getSummaryUsage(\n\t\t\t\tentry.usage,\n\t\t\t\tentry.usageEntryIds,\n\t\t\t\tentry.type === \"compaction\" ? \"compaction\" : \"branchSummary\",\n\t\t\t\towners,\n\t\t\t);\n\t\t\tif (Array.isArray(entry.usageEntryIds) && entry.usageEntryIds.length > 0) {\n\t\t\t\tconst key = JSON.stringify([entry.type, [...new Set(entry.usageEntryIds)].sort()]);\n\t\t\t\tconst previous = summaries.get(key);\n\t\t\t\tif (usage && (!previous || usage.cost.total >= previous.usage.cost.total))\n\t\t\t\t\tsummaries.set(key, { entry, usage });\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t\tif (usage) result.push({ entry, usage });\n\t}\n\treturn [...result, ...summaries.values()];\n}\n\nexport interface UsageCostBreakdownEntry {\n\tusageIncomplete?: boolean;\n\tkey: string;\n\tcost: number;\n\ttokens: number;\n}\n\n/** Group model-attributed usage by model and all other usage into a separate bucket. */\nexport function getUsageCostBreakdown(entries: SessionEntry[]): UsageCostBreakdownEntry[] {\n\tconst totalsByKey = new Map<string, UsageTotals>();\n\n\tfor (const { entry, usage } of getSessionUsage(entries)) {\n\t\tlet key: string | undefined;\n\t\tif (entry.type === \"message\" && entry.message.role === \"assistant\") {\n\t\t\tkey = `${entry.message.provider}/${entry.message.responseModel ?? entry.message.model}`;\n\t\t} else if (entry.type === \"usage\") {\n\t\t\tkey = `${entry.provider}/${entry.model}`;\n\t\t} else {\n\t\t\tkey = \"Tools/summaries\";\n\t\t}\n\t\tif (!key || !usage) continue;\n\n\t\tlet totals = totalsByKey.get(key);\n\t\tif (!totals) {\n\t\t\ttotals = createUsageTotals();\n\t\t\ttotalsByKey.set(key, totals);\n\t\t}\n\t\taddUsageToTotals(totals, usage);\n\t}\n\n\treturn Array.from(totalsByKey, ([key, totals]) => ({\n\t\tkey,\n\t\t...(totals.usageIncomplete ? { usageIncomplete: true } : {}),\n\t\tcost: totals.cost,\n\t\ttokens: totals.input + totals.output + totals.cacheRead + totals.cacheWrite,\n\t}))\n\t\t.filter((entry) => entry.usageIncomplete || entry.cost > 0 || entry.tokens > 0)\n\t\t.sort((a, b) => b.cost - a.cost);\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"execute.d.ts","sourceRoot":"","sources":["../../../src/extensions/codemode/execute.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAmC,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAsBtG,OAAO,KAAK,EAAE,oBAAoB,EAAiB,MAAM,gCAAgC,CAAC;AAC1F,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAKlE,OAAO,EAMN,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,EAGxB,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"execute.d.ts","sourceRoot":"","sources":["../../../src/extensions/codemode/execute.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAmC,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAsBtG,OAAO,KAAK,EAAE,oBAAoB,EAAiB,MAAM,gCAAgC,CAAC;AAC1F,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAKlE,OAAO,EAMN,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,EAGxB,MAAM,WAAW,CAAC;AAgMnB,6FAA6F;AAC7F,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,SAAS,YAAY,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAU1F;AAyLD;;;GAGG;AACH,wBAAsB,eAAe,CACpC,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,iBAAiB,EACxB,MAAM,EAAE,WAAW,GAAG,SAAS,EAC/B,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,eAAe,CAAC,mBAAmB,CAAC,KAAK,IAAI,CAAC,GAAG,SAAS,EAC9E,GAAG,EAAE,oBAAoB,GAAG,SAAS,EACrC,OAAO,GAAE,mBAAwB,GAC/B,OAAO,CAAC,eAAe,CAAC,mBAAmB,CAAC,CAAC,CAoH/C"}
|