@superblocksteam/vite-plugin-file-sync 2.0.42-next.5 → 2.0.42-next.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-service/agent/prompts/build-base-system-prompt.d.ts +2 -1
- package/dist/ai-service/agent/prompts/build-base-system-prompt.d.ts.map +1 -1
- package/dist/ai-service/agent/prompts/build-base-system-prompt.js +31 -2
- package/dist/ai-service/agent/prompts/build-base-system-prompt.js.map +1 -1
- package/dist/ai-service/agent/subagents/apis/example-selector.d.ts +24 -0
- package/dist/ai-service/agent/subagents/apis/example-selector.d.ts.map +1 -0
- package/dist/ai-service/agent/subagents/apis/example-selector.js +104 -0
- package/dist/ai-service/agent/subagents/apis/example-selector.js.map +1 -0
- package/dist/ai-service/agent/subagents/apis/examples.d.ts +55 -0
- package/dist/ai-service/agent/subagents/apis/examples.d.ts.map +1 -0
- package/dist/ai-service/agent/subagents/apis/examples.js +1229 -0
- package/dist/ai-service/agent/subagents/apis/examples.js.map +1 -0
- package/dist/ai-service/agent/subagents/apis/generate-api-source.d.ts +83 -0
- package/dist/ai-service/agent/subagents/apis/generate-api-source.d.ts.map +1 -0
- package/dist/ai-service/agent/{apis.js → subagents/apis/generate-api-source.js} +131 -99
- package/dist/ai-service/agent/subagents/apis/generate-api-source.js.map +1 -0
- package/dist/ai-service/agent/subagents/apis/prompt-builder.d.ts +39 -0
- package/dist/ai-service/agent/subagents/apis/prompt-builder.d.ts.map +1 -0
- package/dist/ai-service/agent/subagents/apis/prompt-builder.js +115 -0
- package/dist/ai-service/agent/subagents/apis/prompt-builder.js.map +1 -0
- package/dist/ai-service/agent/{apis-static-analysis.d.ts → subagents/apis/static-analysis.d.ts} +1 -1
- package/dist/ai-service/agent/subagents/apis/static-analysis.d.ts.map +1 -0
- package/dist/ai-service/agent/{apis-static-analysis.js → subagents/apis/static-analysis.js} +3 -2
- package/dist/ai-service/agent/subagents/apis/static-analysis.js.map +1 -0
- package/dist/ai-service/agent/subagents/apis/system-prompt.d.ts +3 -0
- package/dist/ai-service/agent/subagents/apis/system-prompt.d.ts.map +1 -0
- package/dist/ai-service/agent/{apis-system-prompt.js → subagents/apis/system-prompt.js} +67 -31
- package/dist/ai-service/agent/{apis-system-prompt.js.map → subagents/apis/system-prompt.js.map} +1 -1
- package/dist/ai-service/agent/tool-message-utils.d.ts +6 -15
- package/dist/ai-service/agent/tool-message-utils.d.ts.map +1 -1
- package/dist/ai-service/agent/tool-message-utils.js +75 -248
- package/dist/ai-service/agent/tool-message-utils.js.map +1 -1
- package/dist/ai-service/agent/tools/build-add-event.d.ts +2 -4
- package/dist/ai-service/agent/tools/build-add-event.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-add-event.js +9 -8
- package/dist/ai-service/agent/tools/build-add-event.js.map +1 -1
- package/dist/ai-service/agent/tools/build-add-state-var.d.ts +2 -4
- package/dist/ai-service/agent/tools/build-add-state-var.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-add-state-var.js +6 -5
- package/dist/ai-service/agent/tools/build-add-state-var.js.map +1 -1
- package/dist/ai-service/agent/tools/build-add-timer.d.ts +2 -4
- package/dist/ai-service/agent/tools/build-add-timer.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-add-timer.js +7 -6
- package/dist/ai-service/agent/tools/build-add-timer.js.map +1 -1
- package/dist/ai-service/agent/tools/build-create-page.d.ts +2 -3
- package/dist/ai-service/agent/tools/build-create-page.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-create-page.js +11 -10
- package/dist/ai-service/agent/tools/build-create-page.js.map +1 -1
- package/dist/ai-service/agent/tools/build-debug.d.ts +1 -3
- package/dist/ai-service/agent/tools/build-debug.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-debug.js +7 -5
- package/dist/ai-service/agent/tools/build-debug.js.map +1 -1
- package/dist/ai-service/agent/tools/build-edit-file.d.ts +2 -4
- package/dist/ai-service/agent/tools/build-edit-file.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-edit-file.js +10 -7
- package/dist/ai-service/agent/tools/build-edit-file.js.map +1 -1
- package/dist/ai-service/agent/tools/build-finalize.d.ts +2 -4
- package/dist/ai-service/agent/tools/build-finalize.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-finalize.js +7 -6
- package/dist/ai-service/agent/tools/build-finalize.js.map +1 -1
- package/dist/ai-service/agent/tools/build-install-packages.d.ts +2 -4
- package/dist/ai-service/agent/tools/build-install-packages.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-install-packages.js +7 -6
- package/dist/ai-service/agent/tools/build-install-packages.js.map +1 -1
- package/dist/ai-service/agent/tools/build-list-available-components.d.ts +3 -8
- package/dist/ai-service/agent/tools/build-list-available-components.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-list-available-components.js +30 -9
- package/dist/ai-service/agent/tools/build-list-available-components.js.map +1 -1
- package/dist/ai-service/agent/tools/build-list-files.d.ts +2 -3
- package/dist/ai-service/agent/tools/build-list-files.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-list-files.js +10 -8
- package/dist/ai-service/agent/tools/build-list-files.js.map +1 -1
- package/dist/ai-service/agent/tools/build-manage-checklist.d.ts +3 -3
- package/dist/ai-service/agent/tools/build-manage-checklist.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-manage-checklist.js +9 -8
- package/dist/ai-service/agent/tools/build-manage-checklist.js.map +1 -1
- package/dist/ai-service/agent/tools/build-multi-edit-file.d.ts +2 -4
- package/dist/ai-service/agent/tools/build-multi-edit-file.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-multi-edit-file.js +9 -6
- package/dist/ai-service/agent/tools/build-multi-edit-file.js.map +1 -1
- package/dist/ai-service/agent/tools/build-read-files.d.ts +2 -4
- package/dist/ai-service/agent/tools/build-read-files.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-read-files.js +10 -8
- package/dist/ai-service/agent/tools/build-read-files.js.map +1 -1
- package/dist/ai-service/agent/tools/build-register-component-name.d.ts +2 -4
- package/dist/ai-service/agent/tools/build-register-component-name.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-register-component-name.js +9 -8
- package/dist/ai-service/agent/tools/build-register-component-name.js.map +1 -1
- package/dist/ai-service/agent/tools/build-rename-page.d.ts +2 -3
- package/dist/ai-service/agent/tools/build-rename-page.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-rename-page.js +6 -5
- package/dist/ai-service/agent/tools/build-rename-page.js.map +1 -1
- package/dist/ai-service/agent/tools/build-set-api-triggers.d.ts +2 -4
- package/dist/ai-service/agent/tools/build-set-api-triggers.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-set-api-triggers.js +6 -5
- package/dist/ai-service/agent/tools/build-set-api-triggers.js.map +1 -1
- package/dist/ai-service/agent/tools/build-update-state-var.d.ts +2 -3
- package/dist/ai-service/agent/tools/build-update-state-var.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-update-state-var.js +6 -5
- package/dist/ai-service/agent/tools/build-update-state-var.js.map +1 -1
- package/dist/ai-service/agent/tools/build-validate-icons.d.ts +2 -3
- package/dist/ai-service/agent/tools/build-validate-icons.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-validate-icons.js +8 -6
- package/dist/ai-service/agent/tools/build-validate-icons.js.map +1 -1
- package/dist/ai-service/agent/tools/build-write-file.d.ts +2 -4
- package/dist/ai-service/agent/tools/build-write-file.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/build-write-file.js +6 -5
- package/dist/ai-service/agent/tools/build-write-file.js.map +1 -1
- package/dist/ai-service/agent/tools/debug-cache.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/debug-cache.js +3 -0
- package/dist/ai-service/agent/tools/debug-cache.js.map +1 -1
- package/dist/ai-service/agent/tools/index.d.ts +23 -20
- package/dist/ai-service/agent/tools/index.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/index.js +23 -20
- package/dist/ai-service/agent/tools/index.js.map +1 -1
- package/dist/ai-service/agent/tools/integrations/execute-request.d.ts +25 -0
- package/dist/ai-service/agent/tools/integrations/execute-request.d.ts.map +1 -0
- package/dist/ai-service/agent/tools/integrations/execute-request.js +213 -0
- package/dist/ai-service/agent/tools/integrations/execute-request.js.map +1 -0
- package/dist/ai-service/agent/tools/integrations/index.d.ts +4 -0
- package/dist/ai-service/agent/tools/integrations/index.d.ts.map +1 -0
- package/dist/ai-service/agent/tools/integrations/index.js +4 -0
- package/dist/ai-service/agent/tools/integrations/index.js.map +1 -0
- package/dist/ai-service/agent/tools/integrations/internal.d.ts +3 -0
- package/dist/ai-service/agent/tools/integrations/internal.d.ts.map +1 -0
- package/dist/ai-service/agent/tools/integrations/internal.js +11 -0
- package/dist/ai-service/agent/tools/integrations/internal.js.map +1 -0
- package/dist/ai-service/agent/tools/integrations/metadata.d.ts +9 -0
- package/dist/ai-service/agent/tools/integrations/metadata.d.ts.map +1 -0
- package/dist/ai-service/agent/tools/integrations/metadata.js +57 -0
- package/dist/ai-service/agent/tools/integrations/metadata.js.map +1 -0
- package/dist/ai-service/agent/tools/integrations/run-code.d.ts +5 -0
- package/dist/ai-service/agent/tools/integrations/run-code.d.ts.map +1 -0
- package/dist/ai-service/agent/tools/integrations/run-code.js +32 -0
- package/dist/ai-service/agent/tools/integrations/run-code.js.map +1 -0
- package/dist/ai-service/agent/tools/study-current-app-state.d.ts +2 -4
- package/dist/ai-service/agent/tools/study-current-app-state.d.ts.map +1 -1
- package/dist/ai-service/agent/tools/study-current-app-state.js +10 -8
- package/dist/ai-service/agent/tools/study-current-app-state.js.map +1 -1
- package/dist/ai-service/agent/tools.d.ts +3 -280
- package/dist/ai-service/agent/tools.d.ts.map +1 -1
- package/dist/ai-service/agent/tools.js +68 -55
- package/dist/ai-service/agent/tools.js.map +1 -1
- package/dist/ai-service/agent/tools2/access-control.d.ts +119 -0
- package/dist/ai-service/agent/tools2/access-control.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/access-control.js +216 -0
- package/dist/ai-service/agent/tools2/access-control.js.map +1 -0
- package/dist/ai-service/agent/tools2/example.d.ts +13 -0
- package/dist/ai-service/agent/tools2/example.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/example.js +143 -0
- package/dist/ai-service/agent/tools2/example.js.map +1 -0
- package/dist/ai-service/agent/tools2/index.d.ts +16 -0
- package/dist/ai-service/agent/tools2/index.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/index.js +18 -0
- package/dist/ai-service/agent/tools2/index.js.map +1 -0
- package/dist/ai-service/agent/tools2/registry.d.ts +167 -0
- package/dist/ai-service/agent/tools2/registry.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/registry.js +305 -0
- package/dist/ai-service/agent/tools2/registry.js.map +1 -0
- package/dist/ai-service/agent/tools2/tools/ask-multi-choice.d.ts +12 -0
- package/dist/ai-service/agent/tools2/tools/ask-multi-choice.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/tools/ask-multi-choice.js +73 -0
- package/dist/ai-service/agent/tools2/tools/ask-multi-choice.js.map +1 -0
- package/dist/ai-service/agent/tools2/tools/bash.d.ts +34 -0
- package/dist/ai-service/agent/tools2/tools/bash.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/tools/bash.js +76 -0
- package/dist/ai-service/agent/tools2/tools/bash.js.map +1 -0
- package/dist/ai-service/agent/tools2/tools/edit.d.ts +14 -0
- package/dist/ai-service/agent/tools2/tools/edit.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/tools/edit.js +60 -0
- package/dist/ai-service/agent/tools2/tools/edit.js.map +1 -0
- package/dist/ai-service/agent/tools2/tools/exit-plan-mode.d.ts +10 -0
- package/dist/ai-service/agent/tools2/tools/exit-plan-mode.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/tools/exit-plan-mode.js +66 -0
- package/dist/ai-service/agent/tools2/tools/exit-plan-mode.js.map +1 -0
- package/dist/ai-service/agent/tools2/tools/glob.d.ts +11 -0
- package/dist/ai-service/agent/tools2/tools/glob.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/tools/glob.js +44 -0
- package/dist/ai-service/agent/tools2/tools/glob.js.map +1 -0
- package/dist/ai-service/agent/tools2/tools/grep.d.ts +21 -0
- package/dist/ai-service/agent/tools2/tools/grep.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/tools/grep.js +100 -0
- package/dist/ai-service/agent/tools2/tools/grep.js.map +1 -0
- package/dist/ai-service/agent/tools2/tools/index.d.ts +13 -0
- package/dist/ai-service/agent/tools2/tools/index.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/tools/index.js +13 -0
- package/dist/ai-service/agent/tools2/tools/index.js.map +1 -0
- package/dist/ai-service/agent/tools2/tools/read.d.ts +9 -0
- package/dist/ai-service/agent/tools2/tools/read.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/tools/read.js +43 -0
- package/dist/ai-service/agent/tools2/tools/read.js.map +1 -0
- package/dist/ai-service/agent/tools2/tools/write.d.ts +11 -0
- package/dist/ai-service/agent/tools2/tools/write.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/tools/write.js +28 -0
- package/dist/ai-service/agent/tools2/tools/write.js.map +1 -0
- package/dist/ai-service/agent/tools2/types.d.ts +148 -0
- package/dist/ai-service/agent/tools2/types.d.ts.map +1 -0
- package/dist/ai-service/agent/tools2/types.js +55 -0
- package/dist/ai-service/agent/tools2/types.js.map +1 -0
- package/dist/ai-service/agent/utils.d.ts +2 -3
- package/dist/ai-service/agent/utils.d.ts.map +1 -1
- package/dist/ai-service/agent/utils.js +14 -13
- package/dist/ai-service/agent/utils.js.map +1 -1
- package/dist/ai-service/chat/chat-session-store.d.ts +5 -0
- package/dist/ai-service/chat/chat-session-store.d.ts.map +1 -1
- package/dist/ai-service/chat/chat-session-store.js +484 -345
- package/dist/ai-service/chat/chat-session-store.js.map +1 -1
- package/dist/ai-service/chat/extract-history.d.ts +1 -0
- package/dist/ai-service/chat/extract-history.d.ts.map +1 -1
- package/dist/ai-service/chat/extract-history.js +12 -1
- package/dist/ai-service/chat/extract-history.js.map +1 -1
- package/dist/ai-service/const.d.ts +2 -1
- package/dist/ai-service/const.d.ts.map +1 -1
- package/dist/ai-service/const.js +7 -1
- package/dist/ai-service/const.js.map +1 -1
- package/dist/ai-service/index.d.ts +9 -8
- package/dist/ai-service/index.d.ts.map +1 -1
- package/dist/ai-service/index.js +7 -0
- package/dist/ai-service/index.js.map +1 -1
- package/dist/ai-service/integrations/store.d.ts +24 -1
- package/dist/ai-service/integrations/store.d.ts.map +1 -1
- package/dist/ai-service/integrations/store.js +86 -2
- package/dist/ai-service/integrations/store.js.map +1 -1
- package/dist/ai-service/llm/context/caching/constants.d.ts +27 -0
- package/dist/ai-service/llm/context/caching/constants.d.ts.map +1 -0
- package/dist/ai-service/llm/context/caching/constants.js +20 -0
- package/dist/ai-service/llm/context/caching/constants.js.map +1 -0
- package/dist/ai-service/llm/context/caching/factory.d.ts +12 -0
- package/dist/ai-service/llm/context/caching/factory.d.ts.map +1 -0
- package/dist/ai-service/llm/context/caching/factory.js +28 -0
- package/dist/ai-service/llm/context/caching/factory.js.map +1 -0
- package/dist/ai-service/llm/context/caching/index.d.ts +10 -0
- package/dist/ai-service/llm/context/caching/index.d.ts.map +1 -0
- package/dist/ai-service/llm/context/caching/index.js +9 -0
- package/dist/ai-service/llm/context/caching/index.js.map +1 -0
- package/dist/ai-service/llm/context/caching/quantized-strategy.d.ts +22 -0
- package/dist/ai-service/llm/context/caching/quantized-strategy.d.ts.map +1 -0
- package/dist/ai-service/llm/context/caching/quantized-strategy.js +48 -0
- package/dist/ai-service/llm/context/caching/quantized-strategy.js.map +1 -0
- package/dist/ai-service/llm/context/caching/strategy.d.ts +20 -0
- package/dist/ai-service/llm/context/caching/strategy.d.ts.map +1 -0
- package/dist/ai-service/llm/context/caching/strategy.js +5 -0
- package/dist/ai-service/llm/context/caching/strategy.js.map +1 -0
- package/dist/ai-service/llm/context/caching/types.d.ts +34 -0
- package/dist/ai-service/llm/context/caching/types.d.ts.map +1 -0
- package/dist/ai-service/llm/context/caching/types.js +8 -0
- package/dist/ai-service/llm/context/caching/types.js.map +1 -0
- package/dist/ai-service/llm/context/config.d.ts +51 -0
- package/dist/ai-service/llm/context/config.d.ts.map +1 -0
- package/dist/ai-service/llm/context/config.js +58 -0
- package/dist/ai-service/llm/context/config.js.map +1 -0
- package/dist/ai-service/llm/context/constants.d.ts +106 -0
- package/dist/ai-service/llm/context/constants.d.ts.map +1 -0
- package/dist/ai-service/llm/context/constants.js +106 -0
- package/dist/ai-service/llm/context/constants.js.map +1 -0
- package/dist/ai-service/llm/context/context.d.ts +207 -0
- package/dist/ai-service/llm/context/context.d.ts.map +1 -0
- package/dist/ai-service/llm/context/context.js +742 -0
- package/dist/ai-service/llm/context/context.js.map +1 -0
- package/dist/ai-service/llm/context/errors.d.ts +45 -0
- package/dist/ai-service/llm/context/errors.d.ts.map +1 -0
- package/dist/ai-service/llm/context/errors.js +56 -0
- package/dist/ai-service/llm/context/errors.js.map +1 -0
- package/dist/ai-service/llm/context/index.d.ts +13 -0
- package/dist/ai-service/llm/context/index.d.ts.map +1 -0
- package/dist/ai-service/llm/context/index.js +17 -0
- package/dist/ai-service/llm/context/index.js.map +1 -0
- package/dist/ai-service/llm/context/internal-types.d.ts +52 -0
- package/dist/ai-service/llm/context/internal-types.d.ts.map +1 -0
- package/dist/ai-service/llm/context/internal-types.js +28 -0
- package/dist/ai-service/llm/context/internal-types.js.map +1 -0
- package/dist/ai-service/llm/context/levels/index.d.ts +12 -0
- package/dist/ai-service/llm/context/levels/index.d.ts.map +1 -0
- package/dist/ai-service/llm/context/levels/index.js +12 -0
- package/dist/ai-service/llm/context/levels/index.js.map +1 -0
- package/dist/ai-service/llm/context/levels/l1.d.ts +83 -0
- package/dist/ai-service/llm/context/levels/l1.d.ts.map +1 -0
- package/dist/ai-service/llm/context/levels/l1.js +143 -0
- package/dist/ai-service/llm/context/levels/l1.js.map +1 -0
- package/dist/ai-service/llm/context/levels/l2.d.ts +60 -0
- package/dist/ai-service/llm/context/levels/l2.d.ts.map +1 -0
- package/dist/ai-service/llm/context/levels/l2.js +96 -0
- package/dist/ai-service/llm/context/levels/l2.js.map +1 -0
- package/dist/ai-service/llm/context/levels/l3.d.ts +46 -0
- package/dist/ai-service/llm/context/levels/l3.d.ts.map +1 -0
- package/dist/ai-service/llm/context/levels/l3.js +70 -0
- package/dist/ai-service/llm/context/levels/l3.js.map +1 -0
- package/dist/ai-service/llm/context/logger.d.ts +17 -0
- package/dist/ai-service/llm/context/logger.d.ts.map +1 -0
- package/dist/ai-service/llm/context/logger.js +26 -0
- package/dist/ai-service/llm/context/logger.js.map +1 -0
- package/dist/ai-service/llm/context/manager.d.ts +79 -0
- package/dist/ai-service/llm/context/manager.d.ts.map +1 -0
- package/dist/ai-service/llm/context/manager.js +136 -0
- package/dist/ai-service/llm/context/manager.js.map +1 -0
- package/dist/ai-service/llm/context/options.d.ts +43 -0
- package/dist/ai-service/llm/context/options.d.ts.map +1 -0
- package/dist/ai-service/llm/context/options.js +81 -0
- package/dist/ai-service/llm/context/options.js.map +1 -0
- package/dist/ai-service/llm/context/serialization.d.ts +56 -0
- package/dist/ai-service/llm/context/serialization.d.ts.map +1 -0
- package/dist/ai-service/llm/context/serialization.js +9 -0
- package/dist/ai-service/llm/context/serialization.js.map +1 -0
- package/dist/ai-service/llm/context/storage/index.d.ts +10 -0
- package/dist/ai-service/llm/context/storage/index.d.ts.map +1 -0
- package/dist/ai-service/llm/context/storage/index.js +2 -0
- package/dist/ai-service/llm/context/storage/index.js.map +1 -0
- package/dist/ai-service/llm/context/storage/local.d.ts +22 -0
- package/dist/ai-service/llm/context/storage/local.d.ts.map +1 -0
- package/dist/ai-service/llm/context/storage/local.js +42 -0
- package/dist/ai-service/llm/context/storage/local.js.map +1 -0
- package/dist/ai-service/llm/context/types.d.ts +125 -0
- package/dist/ai-service/llm/context/types.d.ts.map +1 -0
- package/dist/ai-service/llm/context/types.js +5 -0
- package/dist/ai-service/llm/context/types.js.map +1 -0
- package/dist/ai-service/llm/context/utils/content-compaction.d.ts +26 -0
- package/dist/ai-service/llm/context/utils/content-compaction.d.ts.map +1 -0
- package/dist/ai-service/llm/context/utils/content-compaction.js +52 -0
- package/dist/ai-service/llm/context/utils/content-compaction.js.map +1 -0
- package/dist/ai-service/llm/context/utils/index.d.ts +10 -0
- package/dist/ai-service/llm/context/utils/index.d.ts.map +1 -0
- package/dist/ai-service/llm/context/utils/index.js +10 -0
- package/dist/ai-service/llm/context/utils/index.js.map +1 -0
- package/dist/ai-service/llm/context/utils/message-utils.d.ts +34 -0
- package/dist/ai-service/llm/context/utils/message-utils.d.ts.map +1 -0
- package/dist/ai-service/llm/context/utils/message-utils.js +68 -0
- package/dist/ai-service/llm/context/utils/message-utils.js.map +1 -0
- package/dist/ai-service/llm/context/utils/token-estimation.d.ts +31 -0
- package/dist/ai-service/llm/context/utils/token-estimation.d.ts.map +1 -0
- package/dist/ai-service/llm/context/utils/token-estimation.js +52 -0
- package/dist/ai-service/llm/context/utils/token-estimation.js.map +1 -0
- package/dist/ai-service/llm/context/utils/visualization.d.ts +40 -0
- package/dist/ai-service/llm/context/utils/visualization.d.ts.map +1 -0
- package/dist/ai-service/llm/context/utils/visualization.js +516 -0
- package/dist/ai-service/llm/context/utils/visualization.js.map +1 -0
- package/dist/ai-service/llm/impl/anthropic.js +2 -2
- package/dist/ai-service/llm/impl/anthropic.js.map +1 -1
- package/dist/ai-service/llmobs/helpers.d.ts.map +1 -1
- package/dist/ai-service/llmobs/helpers.js +10 -19
- package/dist/ai-service/llmobs/helpers.js.map +1 -1
- package/dist/ai-service/llmobs/middleware/stream-text.js +4 -1
- package/dist/ai-service/llmobs/middleware/stream-text.js.map +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/ButtonPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/CheckboxPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/ColumnPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/ContainerPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/DatePickerPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/DropdownPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/IconPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/ImagePropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/InputPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/ModalPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/PagePropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/SectionPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/SlideoutPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/SwitchPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/TablePropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-components/TextPropsDocs.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-typedefs/Dim.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-typedefs/EventFlow.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/library-typedefs/TextStyleWithVariant.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/full-examples.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-api.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-components-rules.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-custom-components.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-data-filtering.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-event-flow.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-forms.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-layouts.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-page.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-rbac.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-routes.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-state.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/superblocks-theming-chakra-new.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/system-base.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/system-incremental.js +1 -1
- package/dist/ai-service/prompt-builder-service/static-fragments/platform-parts/system-specific-edit.js +1 -1
- package/dist/ai-service/state-machine/clark-fsm.d.ts +9 -5
- package/dist/ai-service/state-machine/clark-fsm.d.ts.map +1 -1
- package/dist/ai-service/state-machine/clark-fsm.js.map +1 -1
- package/dist/ai-service/state-machine/handlers/agent-planning.d.ts.map +1 -1
- package/dist/ai-service/state-machine/handlers/agent-planning.js +36 -9
- package/dist/ai-service/state-machine/handlers/agent-planning.js.map +1 -1
- package/dist/ai-service/state-machine/handlers/llm-generating.d.ts.map +1 -1
- package/dist/ai-service/state-machine/handlers/llm-generating.js +154 -104
- package/dist/ai-service/state-machine/handlers/llm-generating.js.map +1 -1
- package/dist/ai-service/state-machine/helpers/context-id.d.ts +4 -0
- package/dist/ai-service/state-machine/helpers/context-id.d.ts.map +1 -0
- package/dist/ai-service/state-machine/helpers/context-id.js +16 -0
- package/dist/ai-service/state-machine/helpers/context-id.js.map +1 -0
- package/dist/ai-service/state-machine/helpers/peer.d.ts.map +1 -1
- package/dist/ai-service/state-machine/helpers/peer.js +7 -4
- package/dist/ai-service/state-machine/helpers/peer.js.map +1 -1
- package/dist/ai-service/state-machine/mocks.d.ts +2 -3
- package/dist/ai-service/state-machine/mocks.d.ts.map +1 -1
- package/dist/ai-service/state-machine/mocks.js.map +1 -1
- package/dist/ai-service/types.d.ts +3 -0
- package/dist/ai-service/types.d.ts.map +1 -1
- package/dist/ai-service/types.js.map +1 -1
- package/dist/ai-service/util/parse-jwt.d.ts +12 -0
- package/dist/ai-service/util/parse-jwt.d.ts.map +1 -0
- package/dist/ai-service/util/parse-jwt.js +30 -0
- package/dist/ai-service/util/parse-jwt.js.map +1 -0
- package/dist/ai-service/util/safe-stringify.d.ts +1 -1
- package/dist/ai-service/util/safe-stringify.d.ts.map +1 -1
- package/dist/ai-service/util/safe-stringify.js +3 -3
- package/dist/ai-service/util/safe-stringify.js.map +1 -1
- package/dist/binding-extraction/extract-js-identifiers.d.ts.map +1 -1
- package/dist/binding-extraction/extract-js-identifiers.js +1 -1
- package/dist/binding-extraction/extract-js-identifiers.js.map +1 -1
- package/dist/component-docs-service/index.d.ts.map +1 -1
- package/dist/component-docs-service/index.js +0 -2
- package/dist/component-docs-service/index.js.map +1 -1
- package/package.json +8 -8
- package/dist/ai-service/agent/apis-static-analysis.d.ts.map +0 -1
- package/dist/ai-service/agent/apis-static-analysis.js.map +0 -1
- package/dist/ai-service/agent/apis-system-prompt.d.ts +0 -3
- package/dist/ai-service/agent/apis-system-prompt.d.ts.map +0 -1
- package/dist/ai-service/agent/apis.d.ts +0 -55
- package/dist/ai-service/agent/apis.d.ts.map +0 -1
- package/dist/ai-service/agent/apis.js.map +0 -1
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main Context class for managing LLM conversation context.
|
|
3
|
+
*
|
|
4
|
+
* Responsibilities:
|
|
5
|
+
* - Maintain conversation turns across three compression levels
|
|
6
|
+
* - Automatically compact context when token limits are approached
|
|
7
|
+
* - Track and refine character-to-token estimation
|
|
8
|
+
* - Persist context state for resumption
|
|
9
|
+
*
|
|
10
|
+
* The three-level compaction strategy:
|
|
11
|
+
* - L1 (50%): Recent turns with full detail
|
|
12
|
+
* - L2 (35%): Older turns with moderate compression
|
|
13
|
+
* - L3 (15%): Oldest turns with maximum compression
|
|
14
|
+
*/
|
|
15
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
16
|
+
var useValue = arguments.length > 2;
|
|
17
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
18
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
19
|
+
}
|
|
20
|
+
return useValue ? value : void 0;
|
|
21
|
+
};
|
|
22
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
23
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
24
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
25
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
26
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
27
|
+
var _, done = false;
|
|
28
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
29
|
+
var context = {};
|
|
30
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
31
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
32
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
33
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
34
|
+
if (kind === "accessor") {
|
|
35
|
+
if (result === void 0) continue;
|
|
36
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
37
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
38
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
39
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
40
|
+
}
|
|
41
|
+
else if (_ = accept(result)) {
|
|
42
|
+
if (kind === "field") initializers.unshift(_);
|
|
43
|
+
else descriptor[key] = _;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
47
|
+
done = true;
|
|
48
|
+
};
|
|
49
|
+
import llmobs from "../../llmobs/index.js";
|
|
50
|
+
import { safeJsonStringify } from "../../util/safe-stringify.js";
|
|
51
|
+
import { CACHE_BREAKPOINT_PROVIDER_OPTIONS, } from "./caching/index.js";
|
|
52
|
+
import { UserPromptTooLargeError, ContextLimitExceededError, } from "./errors.js";
|
|
53
|
+
import { L1Turn, L2Turn, L3Turn } from "./levels/index.js";
|
|
54
|
+
import { createPrefixedLogger } from "./logger.js";
|
|
55
|
+
import { withContextDefaults, withLoggingDefaults, } from "./options.js";
|
|
56
|
+
import { estimateTokenCount, countChars, normalizeToolResultOutput, } from "./utils/index.js";
|
|
57
|
+
import { prependReasoningBlockIfNeeded } from "./utils/message-utils.js";
|
|
58
|
+
import { renderContext, } from "./utils/visualization.js";
|
|
59
|
+
/**
|
|
60
|
+
* Context manages conversation history with automatic compaction.
|
|
61
|
+
* Note: you generally want to use the ContextManager to obtain a Context instance.
|
|
62
|
+
*/
|
|
63
|
+
let Context = (() => {
|
|
64
|
+
let _instanceExtraInitializers = [];
|
|
65
|
+
let _compact_decorators;
|
|
66
|
+
return class Context {
|
|
67
|
+
static {
|
|
68
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
69
|
+
_compact_decorators = [llmobs.decorate({ name: "Context.compact", kind: "task" })];
|
|
70
|
+
__esDecorate(this, null, _compact_decorators, { kind: "method", name: "compact", static: false, private: false, access: { has: obj => "compact" in obj, get: obj => obj.compact }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
71
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
72
|
+
}
|
|
73
|
+
id = __runInitializers(this, _instanceExtraInitializers);
|
|
74
|
+
options;
|
|
75
|
+
logging;
|
|
76
|
+
systemPrompt;
|
|
77
|
+
l3Turns = [];
|
|
78
|
+
l2Turns = [];
|
|
79
|
+
l1Turns = [];
|
|
80
|
+
onPersist;
|
|
81
|
+
// num responses received in active turn
|
|
82
|
+
// need to track this to dedup responses from streamText.onStepFinish
|
|
83
|
+
activeTurnNumResponses = 0;
|
|
84
|
+
// links split turns
|
|
85
|
+
continuations = new WeakMap();
|
|
86
|
+
// start time of current turn
|
|
87
|
+
// so we can show turn durations in viz
|
|
88
|
+
turnStartMs = 0;
|
|
89
|
+
// cached logger with prefix and verbose gating
|
|
90
|
+
_logger = null;
|
|
91
|
+
// cache strategy for breakpoint placement
|
|
92
|
+
cacheStrategy;
|
|
93
|
+
constructor(params) {
|
|
94
|
+
const { id, options, logging, onPersist, cacheStrategy } = params;
|
|
95
|
+
this.id = id;
|
|
96
|
+
this.options = withContextDefaults(options);
|
|
97
|
+
this.logging = withLoggingDefaults(logging);
|
|
98
|
+
this.onPersist = onPersist;
|
|
99
|
+
this.cacheStrategy = cacheStrategy;
|
|
100
|
+
}
|
|
101
|
+
get charsPerToken() {
|
|
102
|
+
return this.options.charsPerToken;
|
|
103
|
+
}
|
|
104
|
+
logPrefix() {
|
|
105
|
+
const name = this.id?.name;
|
|
106
|
+
return name ? `[context:${name}]` : "[context]";
|
|
107
|
+
}
|
|
108
|
+
get logger() {
|
|
109
|
+
if (!this._logger) {
|
|
110
|
+
this._logger = createPrefixedLogger(this.logPrefix(), this.logging.verbose);
|
|
111
|
+
}
|
|
112
|
+
return this._logger;
|
|
113
|
+
}
|
|
114
|
+
async persist() {
|
|
115
|
+
if (this.onPersist) {
|
|
116
|
+
await this.onPersist(this.toRecord());
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Seed empty context with a summary message.
|
|
121
|
+
*/
|
|
122
|
+
seedIfEmpty(summary) {
|
|
123
|
+
if (this.turnMessages.length > 0) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
this.l3Turns.push(new L3Turn(this.withTokenCount(summary)));
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Returns all messages in order (system + L3 + L2 + L1).
|
|
130
|
+
*
|
|
131
|
+
* Messages are returned without token counts (clean ModelMessage format).
|
|
132
|
+
*/
|
|
133
|
+
getMessages() {
|
|
134
|
+
const breakPoints = this.cacheStrategy.findBreakpoints({
|
|
135
|
+
messages: this.allMessages,
|
|
136
|
+
tokens: {
|
|
137
|
+
total: this.tokenCount,
|
|
138
|
+
system: this.systemPromptTokenCount,
|
|
139
|
+
},
|
|
140
|
+
options: this.options.caching,
|
|
141
|
+
});
|
|
142
|
+
this.logger.debug(`Cache: ${breakPoints.length} breakpoints`);
|
|
143
|
+
const messages = this.allMessages.map((msg, index, msgs) => {
|
|
144
|
+
if (breakPoints.includes(index)) {
|
|
145
|
+
msg.hasBreakPoint = true;
|
|
146
|
+
}
|
|
147
|
+
else if (msg.hasBreakPoint) {
|
|
148
|
+
delete msg.hasBreakPoint;
|
|
149
|
+
}
|
|
150
|
+
return this.toModelMessage(msg, index, msgs);
|
|
151
|
+
});
|
|
152
|
+
for (const idx of breakPoints) {
|
|
153
|
+
messages[idx].providerOptions = CACHE_BREAKPOINT_PROVIDER_OPTIONS;
|
|
154
|
+
}
|
|
155
|
+
return messages;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Total token count across all context (system + all turns).
|
|
159
|
+
*/
|
|
160
|
+
get tokenCount() {
|
|
161
|
+
let total = 0;
|
|
162
|
+
total += this.systemPrompt?.tokenCount ?? 0;
|
|
163
|
+
for (const turn of this.l3Turns) {
|
|
164
|
+
total += turn.tokenCount;
|
|
165
|
+
}
|
|
166
|
+
for (const turn of this.l2Turns) {
|
|
167
|
+
total += turn.tokenCount;
|
|
168
|
+
}
|
|
169
|
+
for (const turn of this.l1Turns) {
|
|
170
|
+
total += turn.tokenCount;
|
|
171
|
+
}
|
|
172
|
+
return total;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Token count for system prompt only.
|
|
176
|
+
*/
|
|
177
|
+
get systemPromptTokenCount() {
|
|
178
|
+
if (!this.systemPrompt)
|
|
179
|
+
return 0;
|
|
180
|
+
return this.systemPrompt.tokenCount;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Currently active turn (last L1 turn if not ended).
|
|
184
|
+
*/
|
|
185
|
+
get activeTurn() {
|
|
186
|
+
return this.l1Turns.at(-1);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Currently active step within active turn.
|
|
190
|
+
*/
|
|
191
|
+
get activeStep() {
|
|
192
|
+
return this.activeTurn?.steps.at(-1);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* All turn messages (L3 + L2 + L1), excluding continuation markers.
|
|
196
|
+
*/
|
|
197
|
+
get turnMessages() {
|
|
198
|
+
const messages = [];
|
|
199
|
+
messages.push(...this.l3Turns.flatMap((turn) => turn.messages));
|
|
200
|
+
messages.push(...this.l2Turns.flatMap((turn) => turn.messages));
|
|
201
|
+
messages.push(...this.l1Turns.flatMap((turn) => turn.messages));
|
|
202
|
+
// filter out continuation marker messages
|
|
203
|
+
return messages.filter((msg) => msg.role !== "user" ||
|
|
204
|
+
msg.originalUserMessage == undefined);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* All messages including system prompt.
|
|
208
|
+
*/
|
|
209
|
+
get allMessages() {
|
|
210
|
+
return [
|
|
211
|
+
...(this.systemPrompt ? [this.systemPrompt] : []),
|
|
212
|
+
...this.turnMessages,
|
|
213
|
+
];
|
|
214
|
+
}
|
|
215
|
+
getLevelBudgets() {
|
|
216
|
+
return this.calculateLevelBudgets(this.options.compaction.target);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Sets the system prompt.
|
|
220
|
+
*/
|
|
221
|
+
setSystemPrompt(message) {
|
|
222
|
+
this.systemPrompt = {
|
|
223
|
+
...message,
|
|
224
|
+
tokenCount: this.estimateTokenCount(message),
|
|
225
|
+
};
|
|
226
|
+
this.logger.debug(`Set system prompt with ${this.systemPromptTokenCount} tokens`);
|
|
227
|
+
this.compact(this.options.maxTokens);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Retrieves the current system prompt (if any).
|
|
231
|
+
*/
|
|
232
|
+
getSystemPrompt() {
|
|
233
|
+
if (!this.systemPrompt)
|
|
234
|
+
return undefined;
|
|
235
|
+
const { tokenCount, hasBreakPoint, ...msg } = this.systemPrompt;
|
|
236
|
+
return msg;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Starts a new conversation turn with a user message.
|
|
240
|
+
*
|
|
241
|
+
* @throws {UserPromptTooLargeError} If user + system exceeds maxTokens and no options.allowOverflow is false
|
|
242
|
+
*/
|
|
243
|
+
startTurn(user) {
|
|
244
|
+
this.turnStartMs = Date.now();
|
|
245
|
+
const tokenizedUser = this.withTokenCount(user);
|
|
246
|
+
// Validate that user messages + system prompt doesn't exceed maxTokens
|
|
247
|
+
const userAndSystemTokens = tokenizedUser.tokenCount + this.systemPromptTokenCount;
|
|
248
|
+
if (userAndSystemTokens > this.options.maxTokens) {
|
|
249
|
+
this.logger.warn(`User prompt too large: ${tokenizedUser.tokenCount} + ${this.systemPromptTokenCount} (system prompt) > ${this.options.maxTokens}.`);
|
|
250
|
+
if (!this.options.allowOverflow) {
|
|
251
|
+
const info = {
|
|
252
|
+
userTokens: tokenizedUser.tokenCount,
|
|
253
|
+
systemTokens: this.systemPromptTokenCount,
|
|
254
|
+
maxTokens: this.options.maxTokens,
|
|
255
|
+
combined: userAndSystemTokens,
|
|
256
|
+
};
|
|
257
|
+
throw new UserPromptTooLargeError(info);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
const newTurn = new L1Turn(tokenizedUser);
|
|
261
|
+
this.l1Turns.push(newTurn);
|
|
262
|
+
this.compact(this.options.maxTokens);
|
|
263
|
+
void this.persist();
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Starts a new step within the active turn.
|
|
267
|
+
*/
|
|
268
|
+
startStep() {
|
|
269
|
+
if (!this.activeTurn) {
|
|
270
|
+
throw new Error("No active turn to start a step in.");
|
|
271
|
+
}
|
|
272
|
+
this.logger.info(`Starting new step in active turn`);
|
|
273
|
+
if (this.tokenCount > this.options.maxTokens) {
|
|
274
|
+
this.onContextLimitExceeded();
|
|
275
|
+
}
|
|
276
|
+
this.activeTurn.steps.push([]);
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Ends the current step, adding response messages.
|
|
280
|
+
*
|
|
281
|
+
* @param responses - Assistant and/or tool messages, accumulated over the entire active turn (this is how streamText.onStepFinish provides them)
|
|
282
|
+
* @param usage - Optional token usage info for refining estimates
|
|
283
|
+
*/
|
|
284
|
+
endStep(responses, usage) {
|
|
285
|
+
if (!this.activeStep) {
|
|
286
|
+
this.logger.warn(`endStep called without an active step - ignoring responses`);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const newResponses = responses.slice(this.activeTurnNumResponses);
|
|
290
|
+
this.activeTurnNumResponses = responses.length;
|
|
291
|
+
this.logger.debug(`Ending step with ${newResponses.length} responses`);
|
|
292
|
+
if (usage) {
|
|
293
|
+
this.updateCharsPerTokenEstimate(usage);
|
|
294
|
+
}
|
|
295
|
+
this.activeStep.push(...newResponses.map((msg) => this.withTokenCount(msg)));
|
|
296
|
+
// Split old steps from active turn if needed (keep recent steps active)
|
|
297
|
+
this.splitActiveTurn();
|
|
298
|
+
this.compact(this.options.maxTokens);
|
|
299
|
+
this.printState({ message: "endStep", usage });
|
|
300
|
+
void this.persist();
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Ends the active turn.
|
|
304
|
+
*/
|
|
305
|
+
endTurn(usage) {
|
|
306
|
+
if (!this.activeTurn) {
|
|
307
|
+
this.logger.warn(`endTurn called without an active turn`);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const durationMs = Date.now() - this.turnStartMs;
|
|
311
|
+
this.activeTurnNumResponses = 0;
|
|
312
|
+
this.logger.debug(`Ending active turn`);
|
|
313
|
+
this.compact(this.options.maxTokens, false);
|
|
314
|
+
this.printState({ message: "endTurn", usage, durationMs });
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Updates the character-per-token estimate based on actual LLM usage.
|
|
318
|
+
*/
|
|
319
|
+
updateCharsPerTokenEstimate(usage) {
|
|
320
|
+
const inputChars = this.allMessages.reduce((sum, msg) => sum + countChars(msg), 0);
|
|
321
|
+
const actualInputTokens = (usage.inputTokens ?? 0) + (usage.cachedInputTokens ?? 0);
|
|
322
|
+
if (actualInputTokens >= this.options.tokenEstimation.minTokensForUpdate &&
|
|
323
|
+
actualInputTokens !== this.tokenCount) {
|
|
324
|
+
const newEstimate = inputChars / actualInputTokens;
|
|
325
|
+
if (newEstimate >= this.options.tokenEstimation.minCharsPerToken &&
|
|
326
|
+
newEstimate <= this.options.tokenEstimation.maxCharsPerToken) {
|
|
327
|
+
this.logger.debug(`Step input: ${inputChars} characters, estimated tokens: ${this.tokenCount}, actual tokens: ${usage.inputTokens ?? 0} (${JSON.stringify(usage)})`);
|
|
328
|
+
this.options.charsPerToken = newEstimate;
|
|
329
|
+
this.logger.info(`Updated charactersPerToken estimate: ${this.options.charsPerToken.toFixed(2)}`);
|
|
330
|
+
this.recalculateTokenCounts();
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
this.logger.warn(`Ignoring unreasonable charactersPerToken estimate: ${newEstimate.toFixed(2)}`);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Recalculates all token counts with current estimate.
|
|
339
|
+
*/
|
|
340
|
+
recalculateTokenCounts() {
|
|
341
|
+
for (const msg of this.allMessages) {
|
|
342
|
+
msg.tokenCount = estimateTokenCount(msg, this.options.charsPerToken);
|
|
343
|
+
}
|
|
344
|
+
this.logger.debug(`Recalculated context token count: ${this.tokenCount} tokens`);
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Main compaction algorithm.
|
|
348
|
+
*
|
|
349
|
+
* Triggers when context exceeds compactionTrigger threshold.
|
|
350
|
+
* Demotes/drops turns to reach compactionTarget.
|
|
351
|
+
* TODO(matt): add final AI summary level
|
|
352
|
+
*
|
|
353
|
+
* @param maxTokens - Hard token limit
|
|
354
|
+
* @param lastTurnIsActive - Whether last L1 turn is active (protected from demotion)
|
|
355
|
+
*/
|
|
356
|
+
compact(maxTokens, lastTurnIsActive = true) {
|
|
357
|
+
const effectiveTrigger = Math.min(this.options.compaction.trigger, maxTokens);
|
|
358
|
+
const effectiveTarget = Math.min(this.options.compaction.target, maxTokens);
|
|
359
|
+
// Skip compaction only if under trigger
|
|
360
|
+
if (this.tokenCount < effectiveTrigger) {
|
|
361
|
+
this.logger.debug(`Skipping compaction: ${this.tokenCount} < ${effectiveTrigger} tokens`);
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
const initialTokenCount = this.tokenCount;
|
|
365
|
+
const l1Count = this.l1Turns.length;
|
|
366
|
+
const l2Count = this.l2Turns.length;
|
|
367
|
+
const l3Count = this.l3Turns.length;
|
|
368
|
+
this.logger.info(`Compaction triggered: ${this.tokenCount} tokens (trigger: ${effectiveTrigger}, target: ${effectiveTarget}) | L1=${l1Count} L2=${l2Count} L3=${l3Count}`);
|
|
369
|
+
const { l1, l2, l3 } = this.options.compaction.budgets;
|
|
370
|
+
const budgets = this.calculateLevelBudgets(effectiveTarget);
|
|
371
|
+
this.logger.debug(`Level targets - L1: ${budgets.l1} (${(l1 * 100).toPrecision(2)}%), L2: ${budgets.l2} (${(l2 * 100).toPrecision(2)}%), L3: ${budgets.l3} (${(l3 * 100).toPrecision(2)}%)`);
|
|
372
|
+
// Compact each level to its target
|
|
373
|
+
this.compactL1ToTarget(budgets.l1, lastTurnIsActive);
|
|
374
|
+
this.compactL2ToTarget(budgets.l2);
|
|
375
|
+
this.compactL3ToTarget(budgets.l3);
|
|
376
|
+
// Aggressive compaction if still over max tokens
|
|
377
|
+
this.performAggressiveCompaction(maxTokens);
|
|
378
|
+
const finalTokenCount = this.tokenCount;
|
|
379
|
+
const finalL1 = this.l1Turns.length;
|
|
380
|
+
const finalL2 = this.l2Turns.length;
|
|
381
|
+
const finalL3 = this.l3Turns.length;
|
|
382
|
+
const reducedBy = initialTokenCount - finalTokenCount;
|
|
383
|
+
const reducedByPercent = (reducedBy / initialTokenCount) * 100;
|
|
384
|
+
this.logger.info(`Compaction complete: ${initialTokenCount}→${finalTokenCount} tokens (-${reducedBy}, -${reducedByPercent.toFixed(1)}%) | L1=${l1Count}→${finalL1}, L2=${l2Count}→${finalL2}, L3=${l3Count}→${finalL3}`);
|
|
385
|
+
// Log if compaction occurred
|
|
386
|
+
const compactionOccurred = initialTokenCount !== finalTokenCount ||
|
|
387
|
+
l1Count + l2Count + l3Count !== finalL1 + finalL2 + finalL3;
|
|
388
|
+
if (compactionOccurred) {
|
|
389
|
+
this.logger.debug("Compaction occurred");
|
|
390
|
+
}
|
|
391
|
+
// Check if we're still over maxTokens after all compaction
|
|
392
|
+
if (this.tokenCount > maxTokens) {
|
|
393
|
+
this.logger.warn(`Still exceeds max after compaction: ${this.tokenCount} > ${maxTokens} - consider increasing maxTokens`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
onContextLimitExceeded() {
|
|
397
|
+
this.logger.warn(`Context limit exceeded: ${this.tokenCount} > ${this.options.maxTokens}`);
|
|
398
|
+
if (this.options.allowOverflow) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
const activeStepTokens = this.activeStep?.reduce((sum, msg) => sum + msg.tokenCount, 0) ?? 0;
|
|
402
|
+
const userTokens = this.activeTurn?.user.tokenCount ?? 0;
|
|
403
|
+
const historyTokens = this.l3Turns.reduce((sum, turn) => sum + turn.tokenCount, 0) +
|
|
404
|
+
this.l2Turns.reduce((sum, turn) => sum + turn.tokenCount, 0) +
|
|
405
|
+
this.l1Turns.slice(0, -1).reduce((sum, turn) => sum + turn.tokenCount, 0);
|
|
406
|
+
const info = {
|
|
407
|
+
currentTokens: this.tokenCount,
|
|
408
|
+
maxTokens: this.options.maxTokens,
|
|
409
|
+
breakdown: {
|
|
410
|
+
system: this.systemPromptTokenCount,
|
|
411
|
+
user: userTokens,
|
|
412
|
+
activeStep: activeStepTokens,
|
|
413
|
+
history: historyTokens,
|
|
414
|
+
},
|
|
415
|
+
};
|
|
416
|
+
throw new ContextLimitExceededError(info);
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Calculates budget allocations for each compression level.
|
|
420
|
+
*/
|
|
421
|
+
calculateLevelBudgets(target) {
|
|
422
|
+
const availableForLevels = Math.max(0, target - this.systemPromptTokenCount);
|
|
423
|
+
const { l1, l2, l3 } = this.options.compaction.budgets;
|
|
424
|
+
return {
|
|
425
|
+
l1: Math.floor(availableForLevels * l1),
|
|
426
|
+
l2: Math.floor(availableForLevels * l2),
|
|
427
|
+
l3: Math.floor(availableForLevels * l3),
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Compacts L1 turns to fit within target budget.
|
|
432
|
+
*/
|
|
433
|
+
compactL1ToTarget(target, lastTurnIsActive = true) {
|
|
434
|
+
const getL1Tokens = () => this.l1Turns.reduce((sum, turn) => sum + turn.tokenCount, 0);
|
|
435
|
+
const l1Before = getL1Tokens();
|
|
436
|
+
let demoted = 0;
|
|
437
|
+
// Splits old steps out from active turn so they can be demoted if needed
|
|
438
|
+
this.splitActiveTurn();
|
|
439
|
+
while (getL1Tokens() > target &&
|
|
440
|
+
this.l1Turns.length > (lastTurnIsActive ? 1 : 0)) {
|
|
441
|
+
const turn = this.l1Turns.shift();
|
|
442
|
+
const compacted = turn.compact(this.options.charsPerToken);
|
|
443
|
+
// If this is a continuation user message, try to merge with previous L2 turn
|
|
444
|
+
if (compacted.user.originalUserMessage && this.l2Turns.length > 0) {
|
|
445
|
+
const lastL2 = this.l2Turns.at(-1);
|
|
446
|
+
lastL2.responses.push(...compacted.responses);
|
|
447
|
+
lastL2.user.originalUserMessage = undefined; // Clear originalUserMessage since it's now merged
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
compacted.user = compacted.user.originalUserMessage ?? compacted.user;
|
|
451
|
+
this.l2Turns.push(compacted);
|
|
452
|
+
}
|
|
453
|
+
demoted++;
|
|
454
|
+
}
|
|
455
|
+
if (demoted > 0) {
|
|
456
|
+
this.logger.debug(`L1 compaction: ${demoted} turns demoted, ${l1Before}→${getL1Tokens()} tokens`);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Compacts L2 turns to fit within target budget.
|
|
461
|
+
*/
|
|
462
|
+
compactL2ToTarget(target) {
|
|
463
|
+
const getL2Tokens = () => this.l2Turns.reduce((sum, turn) => sum + turn.tokenCount, 0);
|
|
464
|
+
const l2Before = getL2Tokens();
|
|
465
|
+
let demoted = 0;
|
|
466
|
+
while (getL2Tokens() > target && this.l2Turns.length > 0) {
|
|
467
|
+
const turn = this.l2Turns.shift();
|
|
468
|
+
const compacted = turn.compact();
|
|
469
|
+
this.l3Turns.push(compacted);
|
|
470
|
+
demoted++;
|
|
471
|
+
}
|
|
472
|
+
if (demoted > 0) {
|
|
473
|
+
this.logger.debug(`L2 compaction: ${demoted} turns demoted, ${l2Before}→${getL2Tokens()} tokens`);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Compacts L3 turns to fit within target budget by dropping oldest.
|
|
478
|
+
*/
|
|
479
|
+
compactL3ToTarget(target) {
|
|
480
|
+
const getL3Tokens = () => this.l3Turns.reduce((sum, turn) => sum + turn.tokenCount, 0);
|
|
481
|
+
const l3Before = getL3Tokens();
|
|
482
|
+
let dropped = 0;
|
|
483
|
+
while (getL3Tokens() > target && this.l3Turns.length > 0) {
|
|
484
|
+
const turn = this.l3Turns.shift();
|
|
485
|
+
this.patchContinuationBeforeDrop(turn.user);
|
|
486
|
+
dropped++;
|
|
487
|
+
}
|
|
488
|
+
if (dropped > 0) {
|
|
489
|
+
this.logger.debug(`L3 compaction: ${dropped} turns dropped, ${l3Before}→${getL3Tokens()} tokens`);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
/*
|
|
493
|
+
* Restore original user message to next continuation before dropping turn.
|
|
494
|
+
*/
|
|
495
|
+
patchContinuationBeforeDrop(user) {
|
|
496
|
+
if (!this.continuations.has(user)) {
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
const next = this.continuations.get(user);
|
|
500
|
+
next.content = next.originalUserMessage.content;
|
|
501
|
+
next.tokenCount = this.estimateTokenCount(next);
|
|
502
|
+
this.continuations.delete(user);
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Performs aggressive compaction when still over target after level-based compaction.
|
|
506
|
+
*/
|
|
507
|
+
performAggressiveCompaction(target) {
|
|
508
|
+
if (this.tokenCount <= target)
|
|
509
|
+
return;
|
|
510
|
+
this.logger.debug(`Performing aggressive compaction: ${this.tokenCount} > ${target} tokens`);
|
|
511
|
+
let aggressiveDemotions = 0;
|
|
512
|
+
const l1Count = this.l1Turns.length;
|
|
513
|
+
const l2Count = this.l2Turns.length;
|
|
514
|
+
const l3Count = this.l3Turns.length;
|
|
515
|
+
// split all but two most recent steps out from active turn
|
|
516
|
+
this.splitActiveTurn(2);
|
|
517
|
+
while (this.tokenCount > target) {
|
|
518
|
+
if (this.l1Turns.length > 1) {
|
|
519
|
+
const turn = this.l1Turns.shift();
|
|
520
|
+
this.l2Turns.push(turn.compact(this.options.charsPerToken));
|
|
521
|
+
aggressiveDemotions++;
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
if (this.l2Turns.length) {
|
|
525
|
+
const turn = this.l2Turns.shift();
|
|
526
|
+
this.l3Turns.push(turn.compact());
|
|
527
|
+
aggressiveDemotions++;
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
if (this.l3Turns.length) {
|
|
531
|
+
const turn = this.l3Turns.shift();
|
|
532
|
+
this.patchContinuationBeforeDrop(turn.user);
|
|
533
|
+
aggressiveDemotions++;
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
const finalL1 = this.l1Turns.length;
|
|
539
|
+
const finalL2 = this.l2Turns.length;
|
|
540
|
+
const finalL3 = this.l3Turns.length;
|
|
541
|
+
if (aggressiveDemotions > 0) {
|
|
542
|
+
this.logger.debug(`Aggressive compaction: ${aggressiveDemotions} additional turns processed | | L1=${l1Count}→${finalL1}, L2=${l2Count}→${finalL2}, L3=${l3Count}→${finalL3}`);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Splits old steps from active turn into a completed L1 turn when appropriate.
|
|
547
|
+
*
|
|
548
|
+
* Strategy:
|
|
549
|
+
* - Split when turn has more than `prioritizedSteps` steps
|
|
550
|
+
* - Merge with previous split turn if it exists (via continuations map)
|
|
551
|
+
*
|
|
552
|
+
* This keeps recent steps in the active turn while moving older steps
|
|
553
|
+
* to a completed turn that can be cached and potentially compacted.
|
|
554
|
+
*/
|
|
555
|
+
splitActiveTurn(stepsToKeep) {
|
|
556
|
+
if (!this.activeTurn)
|
|
557
|
+
return;
|
|
558
|
+
const minSteps = stepsToKeep ?? this.options.compaction.prioritizedSteps;
|
|
559
|
+
// Need more than minSteps to split
|
|
560
|
+
if (this.activeTurn.steps.length <= minSteps) {
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
const stepsToSplit = this.activeTurn.steps.length - minSteps;
|
|
564
|
+
this.performSplit(stepsToSplit);
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Performs the actual split operation.
|
|
568
|
+
*
|
|
569
|
+
* If a previous split turn exists (linked via continuations), merges steps into it.
|
|
570
|
+
* Otherwise creates a new completed turn.
|
|
571
|
+
*/
|
|
572
|
+
performSplit(stepsToCacheable) {
|
|
573
|
+
if (!this.activeTurn)
|
|
574
|
+
return;
|
|
575
|
+
const initialStepCount = this.activeTurn.steps.length;
|
|
576
|
+
const initialTokens = this.activeTurn.tokenCount;
|
|
577
|
+
// Extract old steps
|
|
578
|
+
const oldSteps = this.activeTurn.steps.splice(0, stepsToCacheable);
|
|
579
|
+
// Check if previous turn is a continuation from same user message
|
|
580
|
+
const prevTurn = this.l1Turns[this.l1Turns.length - 2]; // -2 because active is at -1
|
|
581
|
+
if (prevTurn &&
|
|
582
|
+
this.continuations.get(prevTurn.user) === this.activeTurn.user) {
|
|
583
|
+
// Merge: append steps to existing split turn
|
|
584
|
+
prevTurn.steps.push(...oldSteps);
|
|
585
|
+
this.logger.info(`Merged ${stepsToCacheable} steps into existing split turn (${initialStepCount}→${this.activeTurn.steps.length} steps in active turn, ${initialTokens}→${this.activeTurn.tokenCount} tokens)`);
|
|
586
|
+
}
|
|
587
|
+
else {
|
|
588
|
+
// Create new split turn
|
|
589
|
+
const completedTurn = new L1Turn(this.activeTurn.user);
|
|
590
|
+
completedTurn.steps = oldSteps;
|
|
591
|
+
// Insert the completed turn before the active turn
|
|
592
|
+
this.l1Turns.splice(this.l1Turns.length - 1, 0, completedTurn);
|
|
593
|
+
// Replace active turn's user message with continuation marker (if not already)
|
|
594
|
+
if (!this.activeTurn.user.originalUserMessage) {
|
|
595
|
+
const continuationUser = {
|
|
596
|
+
role: "user",
|
|
597
|
+
content: "[Continuing previous interaction...]",
|
|
598
|
+
originalUserMessage: this.activeTurn.user,
|
|
599
|
+
tokenCount: 0,
|
|
600
|
+
};
|
|
601
|
+
continuationUser.tokenCount = this.estimateTokenCount(continuationUser);
|
|
602
|
+
this.activeTurn.user = continuationUser;
|
|
603
|
+
}
|
|
604
|
+
this.continuations.set(completedTurn.user, this.activeTurn.user);
|
|
605
|
+
this.logger.info(`Split ${stepsToCacheable} old steps into new L1 turn (${initialStepCount}→${this.activeTurn.steps.length} steps in active turn, ${initialTokens}→${this.activeTurn.tokenCount} tokens)`);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
withTokenCount(message) {
|
|
609
|
+
return {
|
|
610
|
+
...message,
|
|
611
|
+
tokenCount: this.estimateTokenCount(message),
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
estimateTokenCount(message) {
|
|
615
|
+
return estimateTokenCount(message, this.options.charsPerToken);
|
|
616
|
+
}
|
|
617
|
+
toModelMessage(msg, index, messages) {
|
|
618
|
+
const { tokenCount, ...message } = msg;
|
|
619
|
+
if (message.role === "system") {
|
|
620
|
+
return { ...message, role: "system" };
|
|
621
|
+
}
|
|
622
|
+
if (message.role === "assistant") {
|
|
623
|
+
// repair any broken tool-uses
|
|
624
|
+
this.repairBrokenToolCalls(message, index, messages);
|
|
625
|
+
return {
|
|
626
|
+
...prependReasoningBlockIfNeeded(message),
|
|
627
|
+
role: "assistant",
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
if (message.role === "tool") {
|
|
631
|
+
const content = Array.isArray(message.content)
|
|
632
|
+
? message.content.map((part) => {
|
|
633
|
+
if (part.type === "tool-result" &&
|
|
634
|
+
typeof part.output === "object" &&
|
|
635
|
+
part.output !== null) {
|
|
636
|
+
const serialized = safeJsonStringify(part.output);
|
|
637
|
+
const parsed = JSON.parse(serialized);
|
|
638
|
+
const normalized = normalizeToolResultOutput(parsed);
|
|
639
|
+
return { ...part, output: normalized };
|
|
640
|
+
}
|
|
641
|
+
return part;
|
|
642
|
+
})
|
|
643
|
+
: message.content;
|
|
644
|
+
return { ...message, content, role: "tool" };
|
|
645
|
+
}
|
|
646
|
+
return { ...message, role: "user" };
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Removes tool-call parts from assistant messages if their results aren't present later in the context.
|
|
650
|
+
* This can happen if a tool call was made but the tool result message was never added (e.g. due to an error).
|
|
651
|
+
* Such orphaned tool calls trigger an error in some LLMs.
|
|
652
|
+
* Example error from Vertex:
|
|
653
|
+
*{ "type": "invalid_request_error", "message": "messages.2: `tool_use` ids were found without `tool_result` blocks immediately after: toolu_vrtx_014CKoGtt5B94Q4MwZZHZ8LU. Each `tool_use` block must have a corresponding `tool_result` block in the next message."}
|
|
654
|
+
*/
|
|
655
|
+
repairBrokenToolCalls(assistant, index, messages) {
|
|
656
|
+
if (typeof assistant.content === "string") {
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
const toolResultIds = new Set();
|
|
660
|
+
const toolCallIds = assistant.content.flatMap((part) => {
|
|
661
|
+
if (part.type === "tool-call") {
|
|
662
|
+
return part.toolCallId;
|
|
663
|
+
}
|
|
664
|
+
else if (part.type === "tool-result") {
|
|
665
|
+
toolResultIds.add(part.toolCallId);
|
|
666
|
+
}
|
|
667
|
+
return [];
|
|
668
|
+
});
|
|
669
|
+
if (toolCallIds.length === 0) {
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
for (let i = index + 1; i < messages.length; i++) {
|
|
673
|
+
const msg = messages[i];
|
|
674
|
+
if (!msg || !(msg.role === "assistant" || msg.role === "tool")) {
|
|
675
|
+
break;
|
|
676
|
+
}
|
|
677
|
+
if (typeof msg.content === "string") {
|
|
678
|
+
break;
|
|
679
|
+
}
|
|
680
|
+
msg.content.forEach((part) => {
|
|
681
|
+
if (part.type === "tool-result") {
|
|
682
|
+
toolResultIds.add(part.toolCallId);
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
assistant.content = assistant.content.filter((part) => {
|
|
687
|
+
if (part.type === "tool-call") {
|
|
688
|
+
return toolResultIds.has(part.toolCallId);
|
|
689
|
+
}
|
|
690
|
+
return true;
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Serializes context for persistence.
|
|
695
|
+
*/
|
|
696
|
+
toRecord() {
|
|
697
|
+
return {
|
|
698
|
+
charsPerToken: this.options.charsPerToken,
|
|
699
|
+
l1Turns: this.l1Turns.map((turn) => turn.toRecord()),
|
|
700
|
+
l2Turns: this.l2Turns.map((turn) => turn.toRecord()),
|
|
701
|
+
l3Turns: this.l3Turns.map((turn) => turn.toRecord()),
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
printState(params = {}) {
|
|
705
|
+
if (this.logging.visualization.mode === "off")
|
|
706
|
+
return;
|
|
707
|
+
console.log(this.render(params));
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* Returns an ASCII representation of the context.
|
|
711
|
+
*/
|
|
712
|
+
render(params = {}) {
|
|
713
|
+
return renderContext(this, params);
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* Deserializes a context from a record.
|
|
717
|
+
*
|
|
718
|
+
* @param record - Serialized context data
|
|
719
|
+
* @param params - Parameters for the context constructor
|
|
720
|
+
*/
|
|
721
|
+
static fromRecord(record, params) {
|
|
722
|
+
const { id, options = {}, logging, onPersist, cacheStrategy } = params;
|
|
723
|
+
const context = new Context({
|
|
724
|
+
id,
|
|
725
|
+
options: {
|
|
726
|
+
...options,
|
|
727
|
+
// restore persisted charsPerToken estimate
|
|
728
|
+
charsPerToken: record.charsPerToken,
|
|
729
|
+
},
|
|
730
|
+
logging,
|
|
731
|
+
onPersist,
|
|
732
|
+
cacheStrategy,
|
|
733
|
+
});
|
|
734
|
+
context.l1Turns = record.l1Turns.map((turn) => L1Turn.fromRecord(turn));
|
|
735
|
+
context.l2Turns = record.l2Turns.map((turn) => L2Turn.fromRecord(turn));
|
|
736
|
+
context.l3Turns = record.l3Turns.map((turn) => L3Turn.fromRecord(turn));
|
|
737
|
+
return context;
|
|
738
|
+
}
|
|
739
|
+
};
|
|
740
|
+
})();
|
|
741
|
+
export { Context };
|
|
742
|
+
//# sourceMappingURL=context.js.map
|