@ebowwa/coder 0.7.63 → 0.7.65
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/core/__tests__/permissions.test.d.ts +12 -0
- package/dist/core/__tests__/permissions.test.d.ts.map +1 -0
- package/dist/core/__tests__/permissions.test.js +851 -0
- package/dist/core/agent-loop/__tests__/compaction.test.d.ts +5 -0
- package/dist/core/agent-loop/__tests__/compaction.test.d.ts.map +1 -0
- package/dist/core/agent-loop/__tests__/compaction.test.js +209 -0
- package/dist/core/agent-loop/__tests__/formatters.test.d.ts +5 -0
- package/dist/core/agent-loop/__tests__/formatters.test.d.ts.map +1 -0
- package/dist/core/agent-loop/__tests__/formatters.test.js +195 -0
- package/dist/core/agent-loop/__tests__/index.test.d.ts +5 -0
- package/dist/core/agent-loop/__tests__/index.test.d.ts.map +1 -0
- package/dist/core/agent-loop/__tests__/index.test.js +121 -0
- package/dist/core/agent-loop/__tests__/loop-state.test.d.ts +5 -0
- package/dist/core/agent-loop/__tests__/loop-state.test.d.ts.map +1 -0
- package/dist/core/agent-loop/__tests__/loop-state.test.js +340 -0
- package/dist/core/agent-loop/__tests__/message-builder.test.d.ts +5 -0
- package/dist/core/agent-loop/__tests__/message-builder.test.d.ts.map +1 -0
- package/dist/core/agent-loop/__tests__/message-builder.test.js +178 -0
- package/dist/core/agent-loop/__tests__/tool-executor.test.d.ts +5 -0
- package/dist/core/agent-loop/__tests__/tool-executor.test.d.ts.map +1 -0
- package/dist/core/agent-loop/__tests__/tool-executor.test.js +331 -0
- package/dist/core/agent-loop/compaction.d.ts +39 -0
- package/dist/core/agent-loop/compaction.d.ts.map +1 -0
- package/dist/core/agent-loop/compaction.js +51 -0
- package/dist/core/agent-loop/formatters.d.ts +21 -0
- package/dist/core/agent-loop/formatters.d.ts.map +1 -0
- package/dist/core/agent-loop/formatters.js +42 -0
- package/dist/core/agent-loop/index.d.ts +25 -0
- package/dist/core/agent-loop/index.d.ts.map +1 -0
- package/dist/core/agent-loop/index.js +83 -0
- package/dist/core/agent-loop/loop-state.d.ts +74 -0
- package/dist/core/agent-loop/loop-state.d.ts.map +1 -0
- package/dist/core/agent-loop/loop-state.js +147 -0
- package/dist/core/agent-loop/message-builder.d.ts +13 -0
- package/dist/core/agent-loop/message-builder.d.ts.map +1 -0
- package/dist/core/agent-loop/message-builder.js +49 -0
- package/dist/core/agent-loop/tool-executor.d.ts +23 -0
- package/dist/core/agent-loop/tool-executor.d.ts.map +1 -0
- package/dist/core/agent-loop/tool-executor.js +152 -0
- package/dist/core/agent-loop/turn-executor.d.ts +57 -0
- package/dist/core/agent-loop/turn-executor.d.ts.map +1 -0
- package/dist/core/agent-loop/turn-executor.js +124 -0
- package/dist/core/agent-loop/types.d.ts +141 -0
- package/dist/core/agent-loop/types.d.ts.map +1 -0
- package/dist/core/agent-loop/types.js +4 -0
- package/dist/core/agent-loop.d.ts +17 -0
- package/dist/core/agent-loop.d.ts.map +1 -0
- package/dist/core/agent-loop.js +16 -0
- package/dist/core/api-client-impl.d.ts +62 -0
- package/dist/core/api-client-impl.d.ts.map +1 -0
- package/dist/core/api-client-impl.js +479 -0
- package/dist/core/api-client.d.ts +6 -0
- package/dist/core/api-client.d.ts.map +1 -0
- package/dist/core/api-client.js +5 -0
- package/dist/core/checkpoints.d.ts +128 -0
- package/dist/core/checkpoints.d.ts.map +1 -0
- package/dist/core/checkpoints.js +438 -0
- package/dist/core/claude-md.d.ts +71 -0
- package/dist/core/claude-md.d.ts.map +1 -0
- package/dist/core/claude-md.js +198 -0
- package/dist/core/cognitive-security/hooks.d.ts +138 -0
- package/dist/core/cognitive-security/hooks.d.ts.map +1 -0
- package/dist/core/cognitive-security/hooks.js +389 -0
- package/dist/core/cognitive-security/index.d.ts +751 -0
- package/dist/core/cognitive-security/index.d.ts.map +1 -0
- package/dist/core/cognitive-security/index.js +1123 -0
- package/dist/core/cognitive-security/middleware.d.ts +136 -0
- package/dist/core/cognitive-security/middleware.d.ts.map +1 -0
- package/dist/core/cognitive-security/middleware.js +376 -0
- package/dist/core/config-loader.d.ts +127 -0
- package/dist/core/config-loader.d.ts.map +1 -0
- package/dist/core/config-loader.js +219 -0
- package/dist/core/context-compaction.d.ts +87 -0
- package/dist/core/context-compaction.d.ts.map +1 -0
- package/dist/core/context-compaction.js +428 -0
- package/dist/core/git-status.d.ts +25 -0
- package/dist/core/git-status.d.ts.map +1 -0
- package/dist/core/git-status.js +204 -0
- package/dist/core/image.d.ts +69 -0
- package/dist/core/image.d.ts.map +1 -0
- package/dist/core/image.js +290 -0
- package/dist/core/image.test.d.ts +2 -0
- package/dist/core/image.test.d.ts.map +1 -0
- package/dist/core/image.test.js +149 -0
- package/dist/core/models.d.ts +123 -0
- package/dist/core/models.d.ts.map +1 -0
- package/dist/core/models.js +325 -0
- package/dist/core/permissions.d.ts +81 -0
- package/dist/core/permissions.d.ts.map +1 -0
- package/dist/core/permissions.js +327 -0
- package/dist/core/retry.d.ts +25 -0
- package/dist/core/retry.d.ts.map +1 -0
- package/dist/core/retry.js +121 -0
- package/dist/core/session-store.d.ts +9 -0
- package/dist/core/session-store.d.ts.map +1 -0
- package/dist/core/session-store.js +10 -0
- package/dist/core/sessions/export.d.ts +47 -0
- package/dist/core/sessions/export.d.ts.map +1 -0
- package/dist/core/sessions/export.js +256 -0
- package/dist/core/sessions/index.d.ts +132 -0
- package/dist/core/sessions/index.d.ts.map +1 -0
- package/dist/core/sessions/index.js +442 -0
- package/dist/core/sessions/metadata.d.ts +77 -0
- package/dist/core/sessions/metadata.d.ts.map +1 -0
- package/dist/core/sessions/metadata.js +233 -0
- package/dist/core/sessions/persistence.d.ts +72 -0
- package/dist/core/sessions/persistence.d.ts.map +1 -0
- package/dist/core/sessions/persistence.js +201 -0
- package/dist/core/sessions/types.d.ts +110 -0
- package/dist/core/sessions/types.d.ts.map +1 -0
- package/dist/core/sessions/types.js +4 -0
- package/dist/core/stream-highlighter.d.ts +18 -0
- package/dist/core/stream-highlighter.d.ts.map +1 -0
- package/dist/core/stream-highlighter.js +916 -0
- package/dist/core/system-reminders.d.ts +89 -0
- package/dist/core/system-reminders.d.ts.map +1 -0
- package/dist/core/system-reminders.js +285 -0
- package/dist/ecosystem/hooks/__tests__/index.test.d.ts +5 -0
- package/dist/ecosystem/hooks/__tests__/index.test.d.ts.map +1 -0
- package/dist/ecosystem/hooks/__tests__/index.test.js +458 -0
- package/dist/ecosystem/hooks/index.d.ts +59 -0
- package/dist/ecosystem/hooks/index.d.ts.map +1 -0
- package/dist/ecosystem/hooks/index.js +294 -0
- package/dist/ecosystem/hooks/prompt-evaluator.d.ts +32 -0
- package/dist/ecosystem/hooks/prompt-evaluator.d.ts.map +1 -0
- package/dist/ecosystem/hooks/prompt-evaluator.js +229 -0
- package/dist/ecosystem/skills/index.d.ts +55 -0
- package/dist/ecosystem/skills/index.d.ts.map +1 -0
- package/dist/ecosystem/skills/index.js +258 -0
- package/dist/ecosystem/tools/__tests__/index.test.d.ts +7 -0
- package/dist/ecosystem/tools/__tests__/index.test.d.ts.map +1 -0
- package/dist/ecosystem/tools/__tests__/index.test.js +856 -0
- package/dist/ecosystem/tools/index.d.ts +24 -0
- package/dist/ecosystem/tools/index.d.ts.map +1 -0
- package/dist/ecosystem/tools/index.js +1709 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33688 -49712
- package/dist/interfaces/mcp/client.d.ts +40 -0
- package/dist/interfaces/mcp/client.d.ts.map +1 -0
- package/dist/interfaces/mcp/client.js +309 -0
- package/dist/interfaces/ui/index.d.ts +36 -0
- package/dist/interfaces/ui/index.d.ts.map +1 -0
- package/dist/interfaces/ui/index.js +61 -0
- package/dist/interfaces/ui/spinner.d.ts +140 -0
- package/dist/interfaces/ui/spinner.d.ts.map +1 -0
- package/dist/interfaces/ui/spinner.js +342 -0
- package/dist/interfaces/ui/terminal/cli/index.d.ts +12 -0
- package/dist/interfaces/ui/terminal/cli/index.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/cli/index.js +32012 -50526
- package/dist/interfaces/ui/terminal/native/README.md +53 -0
- package/dist/interfaces/ui/terminal/native/claude_code_native.darwin-x64.node +0 -0
- package/dist/interfaces/ui/terminal/native/claude_code_native.dylib +0 -0
- package/dist/interfaces/ui/terminal/native/index.d.ts +0 -0
- package/dist/interfaces/ui/terminal/native/index.darwin-arm64.node +0 -0
- package/dist/interfaces/ui/terminal/native/index.js +43 -0
- package/dist/interfaces/ui/terminal/native/index.node +0 -0
- package/dist/interfaces/ui/terminal/native/package.json +34 -0
- package/dist/interfaces/ui/terminal/shared/args.d.ts +39 -0
- package/dist/interfaces/ui/terminal/shared/args.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/shared/args.js +176 -0
- package/dist/interfaces/ui/terminal/shared/index.d.ts +11 -0
- package/dist/interfaces/ui/terminal/shared/index.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/shared/index.js +16 -0
- package/dist/interfaces/ui/terminal/shared/loading-state.d.ts +124 -0
- package/dist/interfaces/ui/terminal/shared/loading-state.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/shared/loading-state.js +246 -0
- package/dist/interfaces/ui/terminal/shared/query.d.ts +22 -0
- package/dist/interfaces/ui/terminal/shared/query.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/shared/query.js +100 -0
- package/dist/interfaces/ui/terminal/shared/setup.d.ts +33 -0
- package/dist/interfaces/ui/terminal/shared/setup.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/shared/setup.js +226 -0
- package/dist/interfaces/ui/terminal/shared/status-line.d.ts +117 -0
- package/dist/interfaces/ui/terminal/shared/status-line.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/shared/status-line.js +267 -0
- package/dist/interfaces/ui/terminal/shared/system-prompt.d.ts +38 -0
- package/dist/interfaces/ui/terminal/shared/system-prompt.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/shared/system-prompt.js +102 -0
- package/dist/interfaces/ui/terminal/tui/HelpPanel.d.ts +39 -0
- package/dist/interfaces/ui/terminal/tui/HelpPanel.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/HelpPanel.js +215 -0
- package/dist/interfaces/ui/terminal/tui/InputContext.d.ts +91 -0
- package/dist/interfaces/ui/terminal/tui/InputContext.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/InputContext.js +154 -0
- package/dist/interfaces/ui/terminal/tui/InputField.d.ts +18 -0
- package/dist/interfaces/ui/terminal/tui/InputField.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/InputField.js +41 -0
- package/dist/interfaces/ui/terminal/tui/InteractiveTUI.d.ts +16 -0
- package/dist/interfaces/ui/terminal/tui/InteractiveTUI.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/InteractiveTUI.js +451 -0
- package/dist/interfaces/ui/terminal/tui/MessageArea.d.ts +10 -0
- package/dist/interfaces/ui/terminal/tui/MessageArea.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/MessageArea.js +91 -0
- package/dist/interfaces/ui/terminal/tui/MessageStore.d.ts +48 -0
- package/dist/interfaces/ui/terminal/tui/MessageStore.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/MessageStore.js +151 -0
- package/dist/interfaces/ui/terminal/tui/StatusBar.d.ts +9 -0
- package/dist/interfaces/ui/terminal/tui/StatusBar.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/StatusBar.js +36 -0
- package/dist/interfaces/ui/terminal/tui/commands.d.ts +21 -0
- package/dist/interfaces/ui/terminal/tui/commands.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/commands.js +359 -0
- package/dist/interfaces/ui/terminal/tui/components/InteractiveElements.d.ts +115 -0
- package/dist/interfaces/ui/terminal/tui/components/InteractiveElements.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/components/InteractiveElements.js +306 -0
- package/dist/interfaces/ui/terminal/tui/components/MultilineInput.d.ts +92 -0
- package/dist/interfaces/ui/terminal/tui/components/MultilineInput.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/components/MultilineInput.js +399 -0
- package/dist/interfaces/ui/terminal/tui/components/PaneManager.d.ts +59 -0
- package/dist/interfaces/ui/terminal/tui/components/PaneManager.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/components/PaneManager.js +139 -0
- package/dist/interfaces/ui/terminal/tui/components/Sidebar.d.ts +68 -0
- package/dist/interfaces/ui/terminal/tui/components/Sidebar.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/components/Sidebar.js +340 -0
- package/dist/interfaces/ui/terminal/tui/components/index.d.ts +23 -0
- package/dist/interfaces/ui/terminal/tui/components/index.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/components/index.js +51 -0
- package/dist/interfaces/ui/terminal/tui/console.d.ts +20 -0
- package/dist/interfaces/ui/terminal/tui/console.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/console.js +46 -0
- package/dist/interfaces/ui/terminal/tui/index.d.ts +20 -0
- package/dist/interfaces/ui/terminal/tui/index.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/index.js +28 -0
- package/dist/interfaces/ui/terminal/tui/run.d.ts +13 -0
- package/dist/interfaces/ui/terminal/tui/run.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/run.js +31 -0
- package/dist/interfaces/ui/terminal/tui/spinner.d.ts +44 -0
- package/dist/interfaces/ui/terminal/tui/spinner.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/spinner.js +59 -0
- package/dist/interfaces/ui/terminal/tui/tui-app.d.ts +39 -0
- package/dist/interfaces/ui/terminal/tui/tui-app.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/tui-app.js +198 -0
- package/dist/interfaces/ui/terminal/tui/tui-footer.d.ts +167 -0
- package/dist/interfaces/ui/terminal/tui/tui-footer.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/tui-footer.js +330 -0
- package/dist/interfaces/ui/terminal/tui/types.d.ts +165 -0
- package/dist/interfaces/ui/terminal/tui/types.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/types.js +5 -0
- package/dist/interfaces/ui/terminal/tui/useInputHandler.d.ts +23 -0
- package/dist/interfaces/ui/terminal/tui/useInputHandler.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/useInputHandler.js +72 -0
- package/dist/interfaces/ui/terminal/tui/useNativeInput.d.ts +90 -0
- package/dist/interfaces/ui/terminal/tui/useNativeInput.d.ts.map +1 -0
- package/dist/interfaces/ui/terminal/tui/useNativeInput.js +188 -0
- package/dist/native/README.md +53 -0
- package/dist/native/claude_code_native.darwin-x64.node +0 -0
- package/dist/native/claude_code_native.dylib +0 -0
- package/dist/native/index.d.ts +0 -0
- package/dist/native/index.d.ts.map +1 -0
- package/dist/native/index.darwin-arm64.node +0 -0
- package/dist/native/index.js +43 -0
- package/dist/native/index.node +0 -0
- package/dist/native/package.json +34 -0
- package/dist/teammates/index.d.ts +161 -0
- package/dist/teammates/index.d.ts.map +1 -0
- package/dist/teammates/index.js +827 -0
- package/dist/types/index.d.ts +482 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +52 -0
- package/native/index.darwin-arm64.node +0 -0
- package/native/index.js +33 -19
- package/package.json +6 -3
- package/packages/src/core/__tests__/permissions.test.ts +1091 -0
- package/packages/src/core/agent-loop/__tests__/compaction.test.ts +283 -0
- package/packages/src/core/agent-loop/__tests__/formatters.test.ts +234 -0
- package/packages/src/core/agent-loop/__tests__/index.test.ts +162 -0
- package/packages/src/core/agent-loop/__tests__/loop-state.test.ts +413 -0
- package/packages/src/core/agent-loop/__tests__/message-builder.test.ts +229 -0
- package/packages/src/core/agent-loop/__tests__/tool-executor.test.ts +457 -0
- package/packages/src/core/agent-loop/compaction.ts +92 -0
- package/packages/src/core/agent-loop/formatters.ts +50 -0
- package/packages/src/core/agent-loop/index.ts +137 -0
- package/packages/src/core/agent-loop/loop-state.ts +187 -0
- package/packages/src/core/agent-loop/message-builder.ts +62 -0
- package/packages/src/core/agent-loop/tool-executor.ts +211 -0
- package/packages/src/core/agent-loop/turn-executor.ts +226 -0
- package/packages/src/core/agent-loop/types.ts +152 -0
- package/packages/src/core/agent-loop.ts +18 -0
- package/packages/src/core/api-client-impl.ts +729 -0
- package/packages/src/core/api-client.ts +6 -0
- package/packages/src/core/checkpoints.ts +606 -0
- package/packages/src/core/claude-md.ts +272 -0
- package/packages/src/core/cognitive-security/hooks.ts +591 -0
- package/packages/src/core/cognitive-security/index.ts +2041 -0
- package/packages/src/core/cognitive-security/middleware.ts +536 -0
- package/packages/src/core/config/todo +7 -0
- package/packages/src/core/config-loader.ts +324 -0
- package/packages/src/core/context/__tests__/integration.test.ts +334 -0
- package/packages/src/core/context/compaction.ts +170 -0
- package/packages/src/core/context/constants.ts +58 -0
- package/packages/src/core/context/extraction.ts +85 -0
- package/packages/src/core/context/index.ts +66 -0
- package/packages/src/core/context/summarization.ts +251 -0
- package/packages/src/core/context/token-estimation.ts +98 -0
- package/packages/src/core/context/types.ts +59 -0
- package/packages/src/core/git-status.ts +262 -0
- package/packages/src/core/image.test.ts +180 -0
- package/packages/src/core/image.ts +350 -0
- package/packages/src/core/lmdb.db +0 -0
- package/packages/src/core/lmdb.db-lock +0 -0
- package/packages/src/core/models.ts +507 -0
- package/packages/src/core/normalizers/todo +8 -0
- package/packages/src/core/permissions.ts +431 -0
- package/packages/src/core/providers/README.md +230 -0
- package/packages/src/core/providers/__tests__/providers.test.ts +135 -0
- package/packages/src/core/providers/index.ts +419 -0
- package/packages/src/core/providers/types.ts +132 -0
- package/packages/src/core/retry.ts +180 -0
- package/packages/src/core/session-store.ts +36 -0
- package/packages/src/core/sessions/export.ts +329 -0
- package/packages/src/core/sessions/index.ts +587 -0
- package/packages/src/core/sessions/metadata.ts +309 -0
- package/packages/src/core/sessions/persistence.ts +244 -0
- package/packages/src/core/sessions/types.ts +169 -0
- package/packages/src/core/stream-highlighter.ts +1123 -0
- package/packages/src/core/system-reminders.ts +402 -0
- package/packages/src/core/todo +8 -0
- package/packages/src/ecosystem/hooks/__tests__/index.test.ts +561 -0
- package/packages/src/ecosystem/hooks/index.ts +341 -0
- package/packages/src/ecosystem/hooks/prompt-evaluator.ts +300 -0
- package/packages/src/ecosystem/skills/index.ts +295 -0
- package/packages/src/ecosystem/tools/__tests__/index.test.ts +1335 -0
- package/packages/src/ecosystem/tools/index.ts +2051 -0
- package/packages/src/index.ts +141 -0
- package/packages/src/interfaces/mcp/client.ts +389 -0
- package/packages/src/interfaces/ui/index.ts +158 -0
- package/packages/src/interfaces/ui/lmdb.db +0 -0
- package/packages/src/interfaces/ui/lmdb.db-lock +0 -0
- package/packages/src/interfaces/ui/spinner.ts +451 -0
- package/packages/src/interfaces/ui/terminal/bridge/index.ts +370 -0
- package/packages/src/interfaces/ui/terminal/bridge/ipc.ts +829 -0
- package/packages/src/interfaces/ui/terminal/bridge/screen-export.ts +968 -0
- package/packages/src/interfaces/ui/terminal/bridge/types.ts +226 -0
- package/packages/src/interfaces/ui/terminal/bridge/useBridge.ts +210 -0
- package/packages/src/interfaces/ui/terminal/cli/bootstrap.ts +132 -0
- package/packages/src/interfaces/ui/terminal/cli/index.ts +415 -0
- package/packages/src/interfaces/ui/terminal/cli/interactive/index.ts +110 -0
- package/packages/src/interfaces/ui/terminal/cli/interactive/input-handler.ts +393 -0
- package/packages/src/interfaces/ui/terminal/cli/interactive/interactive-runner.ts +820 -0
- package/packages/src/interfaces/ui/terminal/cli/interactive/message-store.ts +299 -0
- package/packages/src/interfaces/ui/terminal/cli/interactive/types.ts +274 -0
- package/packages/src/interfaces/ui/terminal/lmdb.db +0 -0
- package/packages/src/interfaces/ui/terminal/lmdb.db-lock +0 -0
- package/packages/src/interfaces/ui/terminal/shared/args.ts +222 -0
- package/packages/src/interfaces/ui/terminal/shared/index.ts +84 -0
- package/packages/src/interfaces/ui/terminal/shared/loading-state.ts +322 -0
- package/packages/src/interfaces/ui/terminal/shared/query.ts +152 -0
- package/packages/src/interfaces/ui/terminal/shared/setup.ts +299 -0
- package/packages/src/interfaces/ui/terminal/shared/spinner-frames.ts +73 -0
- package/packages/src/interfaces/ui/terminal/shared/status-line.ts +366 -0
- package/packages/src/interfaces/ui/terminal/shared/system-prompt.ts +146 -0
- package/packages/src/lmdb.db +0 -0
- package/packages/src/lmdb.db-lock +0 -0
- package/packages/src/native/index.ts +2722 -0
- package/packages/src/native/tui_v2_types.ts +39 -0
- package/packages/src/teammates/coordination.test.ts +279 -0
- package/packages/src/teammates/coordination.ts +646 -0
- package/packages/src/teammates/index.ts +1052 -0
- package/packages/src/teammates/integration.test.ts +272 -0
- package/packages/src/teammates/runner.test.ts +235 -0
- package/packages/src/teammates/runner.ts +750 -0
- package/packages/src/teammates/schemas.ts +673 -0
- package/packages/src/types/index.ts +723 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compaction - Reduce message context size while preserving important information
|
|
3
|
+
*
|
|
4
|
+
* Strategy:
|
|
5
|
+
* 1. Always keep the first N messages (original query)
|
|
6
|
+
* 2. Always keep the last M messages (recent context)
|
|
7
|
+
* 3. Summarize middle messages into a single "context summary" user message
|
|
8
|
+
* 4. Preserve tool_use/tool_result pairs when possible
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Message, ContentBlock } from "../../types/index.js";
|
|
12
|
+
import type { CompactionOptions, CompactionResult, CompactionStats } from "./types.js";
|
|
13
|
+
import {
|
|
14
|
+
DEFAULT_KEEP_FIRST,
|
|
15
|
+
DEFAULT_KEEP_LAST,
|
|
16
|
+
DEFAULT_COMPACTION_THRESHOLD,
|
|
17
|
+
MIN_MESSAGES_FOR_COMPACTION,
|
|
18
|
+
} from "./constants.js";
|
|
19
|
+
import { estimateMessagesTokens } from "./token-estimation.js";
|
|
20
|
+
import { extractToolPairs } from "./extraction.js";
|
|
21
|
+
import { summarizeMessages, summarizeWithLLM } from "./summarization.js";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Compact messages to fit within a token limit.
|
|
25
|
+
*/
|
|
26
|
+
export async function compactMessages(
|
|
27
|
+
messages: Message[],
|
|
28
|
+
maxTokens: number,
|
|
29
|
+
options: CompactionOptions = {}
|
|
30
|
+
): Promise<CompactionResult> {
|
|
31
|
+
const {
|
|
32
|
+
keepFirst = DEFAULT_KEEP_FIRST,
|
|
33
|
+
keepLast = DEFAULT_KEEP_LAST,
|
|
34
|
+
preserveToolPairs = true,
|
|
35
|
+
useLLMSummarization = true,
|
|
36
|
+
apiKey,
|
|
37
|
+
baseUrl,
|
|
38
|
+
} = options;
|
|
39
|
+
|
|
40
|
+
const tokensBefore = estimateMessagesTokens(messages);
|
|
41
|
+
|
|
42
|
+
// If already under limit, no compaction needed
|
|
43
|
+
if (tokensBefore <= maxTokens) {
|
|
44
|
+
return {
|
|
45
|
+
messages,
|
|
46
|
+
messagesRemoved: 0,
|
|
47
|
+
tokensBefore,
|
|
48
|
+
tokensAfter: tokensBefore,
|
|
49
|
+
didCompact: false,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Not enough messages to compact - silent return
|
|
54
|
+
if (messages.length <= keepFirst + keepLast) {
|
|
55
|
+
return {
|
|
56
|
+
messages,
|
|
57
|
+
messagesRemoved: 0,
|
|
58
|
+
tokensBefore,
|
|
59
|
+
tokensAfter: tokensBefore,
|
|
60
|
+
didCompact: false,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Extract segments
|
|
65
|
+
const firstMessages = messages.slice(0, keepFirst);
|
|
66
|
+
const middleMessages = messages.slice(keepFirst, -keepLast);
|
|
67
|
+
const lastMessages = messages.slice(-keepLast);
|
|
68
|
+
|
|
69
|
+
// Create summary of middle messages (use LLM if available, fallback to simple)
|
|
70
|
+
const summary = useLLMSummarization
|
|
71
|
+
? await summarizeWithLLM(middleMessages, { apiKey, baseUrl })
|
|
72
|
+
: await summarizeMessages(middleMessages);
|
|
73
|
+
|
|
74
|
+
// Build summary message
|
|
75
|
+
const summaryMessage: Message = {
|
|
76
|
+
role: "user",
|
|
77
|
+
content: [{
|
|
78
|
+
type: "text",
|
|
79
|
+
text: `[Previous context has been compacted for continuity]\n\n${summary}`,
|
|
80
|
+
}],
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// Optionally preserve important tool pairs
|
|
84
|
+
let preservedBlocks: ContentBlock[] = [];
|
|
85
|
+
if (preserveToolPairs && middleMessages.length > 0) {
|
|
86
|
+
const toolPairs = extractToolPairs(middleMessages);
|
|
87
|
+
|
|
88
|
+
// Keep the most recent tool use/result pairs (up to 3)
|
|
89
|
+
const recentPairs = Array.from(toolPairs.values())
|
|
90
|
+
.slice(-3)
|
|
91
|
+
.filter(pair => pair.result && !pair.result.is_error);
|
|
92
|
+
|
|
93
|
+
for (const pair of recentPairs) {
|
|
94
|
+
preservedBlocks.push(pair.use as ContentBlock);
|
|
95
|
+
if (pair.result) {
|
|
96
|
+
preservedBlocks.push(pair.result as ContentBlock);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Build compacted message list
|
|
102
|
+
const compacted: Message[] = [
|
|
103
|
+
...firstMessages,
|
|
104
|
+
summaryMessage,
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
// Add preserved tool results if any
|
|
108
|
+
if (preservedBlocks.length > 0) {
|
|
109
|
+
compacted.push({
|
|
110
|
+
role: "assistant",
|
|
111
|
+
content: preservedBlocks.filter(b => b.type === "tool_use"),
|
|
112
|
+
});
|
|
113
|
+
compacted.push({
|
|
114
|
+
role: "user",
|
|
115
|
+
content: preservedBlocks.filter(b => b.type === "tool_result"),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Add recent messages
|
|
120
|
+
compacted.push(...lastMessages);
|
|
121
|
+
|
|
122
|
+
const tokensAfter = estimateMessagesTokens(compacted);
|
|
123
|
+
const messagesRemoved = messages.length - compacted.length;
|
|
124
|
+
|
|
125
|
+
console.log(`Context compaction: ${messages.length} -> ${compacted.length} messages, ${tokensBefore} -> ${tokensAfter} tokens`);
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
messages: compacted,
|
|
129
|
+
messagesRemoved,
|
|
130
|
+
tokensBefore,
|
|
131
|
+
tokensAfter,
|
|
132
|
+
didCompact: true,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Check if compaction is needed proactively.
|
|
138
|
+
* Returns true if current token usage exceeds the threshold AND there are enough messages to compact.
|
|
139
|
+
*/
|
|
140
|
+
export function needsCompaction(
|
|
141
|
+
messages: Message[],
|
|
142
|
+
maxTokens: number,
|
|
143
|
+
threshold: number = DEFAULT_COMPACTION_THRESHOLD
|
|
144
|
+
): boolean {
|
|
145
|
+
// Not enough messages to meaningfully compact
|
|
146
|
+
if (messages.length < MIN_MESSAGES_FOR_COMPACTION) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const currentTokens = estimateMessagesTokens(messages);
|
|
151
|
+
const thresholdTokens = Math.floor(maxTokens * threshold);
|
|
152
|
+
return currentTokens >= thresholdTokens;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Get compaction statistics for logging/metrics
|
|
157
|
+
*/
|
|
158
|
+
export function getCompactionStats(result: CompactionResult): CompactionStats {
|
|
159
|
+
if (!result.didCompact) {
|
|
160
|
+
return { reductionPercent: 0, tokensSaved: 0 };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const tokensSaved = result.tokensBefore - result.tokensAfter;
|
|
164
|
+
const reductionPercent = (tokensSaved / result.tokensBefore) * 100;
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
reductionPercent: Math.round(reductionPercent * 100) / 100,
|
|
168
|
+
tokensSaved,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context Constants - Configuration values for context compaction
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/** Approximate characters per token (rough estimate for Claude models) */
|
|
6
|
+
export const CHARS_PER_TOKEN = 4;
|
|
7
|
+
|
|
8
|
+
/** Default number of recent messages to keep during compaction */
|
|
9
|
+
export const DEFAULT_KEEP_LAST = 5;
|
|
10
|
+
|
|
11
|
+
/** Default number of initial messages to keep (usually just the first user query) */
|
|
12
|
+
export const DEFAULT_KEEP_FIRST = 1;
|
|
13
|
+
|
|
14
|
+
/** Minimum messages required before compaction is possible */
|
|
15
|
+
export const MIN_MESSAGES_FOR_COMPACTION = 8;
|
|
16
|
+
|
|
17
|
+
/** Default threshold for proactive compaction (90% of max tokens) */
|
|
18
|
+
export const DEFAULT_COMPACTION_THRESHOLD = 0.9;
|
|
19
|
+
|
|
20
|
+
/** Maximum length for summary text before truncation */
|
|
21
|
+
export const MAX_SUMMARY_LENGTH = 8000;
|
|
22
|
+
|
|
23
|
+
/** Maximum tokens for summary output */
|
|
24
|
+
export const SUMMARY_MAX_TOKENS = 2000;
|
|
25
|
+
|
|
26
|
+
/** System prompt for summarization */
|
|
27
|
+
export const SUMMARIZATION_SYSTEM_PROMPT = `You are a context summarizer. Your job is to create concise, information-dense summaries of conversation history.
|
|
28
|
+
|
|
29
|
+
Guidelines:
|
|
30
|
+
- Preserve all important decisions, file changes, and key information
|
|
31
|
+
- Keep track of what tools were used and their outcomes
|
|
32
|
+
- Note any errors encountered and how they were resolved
|
|
33
|
+
- Maintain chronological flow
|
|
34
|
+
- Be extremely concise - use bullet points and short sentences
|
|
35
|
+
- Focus on information that would be needed to continue the conversation
|
|
36
|
+
- Do not include pleasantries or filler text
|
|
37
|
+
|
|
38
|
+
Format your summary as:
|
|
39
|
+
## Summary
|
|
40
|
+
[Brief overview of what was discussed]
|
|
41
|
+
|
|
42
|
+
## Key Actions
|
|
43
|
+
- [Action 1]
|
|
44
|
+
- [Action 2]
|
|
45
|
+
|
|
46
|
+
## Files Modified
|
|
47
|
+
- [file]: [what changed]
|
|
48
|
+
|
|
49
|
+
## Important Context
|
|
50
|
+
[Any critical information needed going forward]`;
|
|
51
|
+
|
|
52
|
+
/** User prompt template for summarization */
|
|
53
|
+
export const SUMMARIZATION_PROMPT = `Summarize the following conversation messages for context compaction. Preserve all important information in a concise format.
|
|
54
|
+
|
|
55
|
+
Messages to summarize:
|
|
56
|
+
{{MESSAGES}}
|
|
57
|
+
|
|
58
|
+
Provide a dense, information-rich summary that captures everything needed to continue this conversation.`;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content Extraction - Extract and organize message content
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { Message, ContentBlock } from "../../types/index.js";
|
|
6
|
+
import type { ToolPair } from "./types.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Extract text content from a message for summarization
|
|
10
|
+
*/
|
|
11
|
+
export function extractTextFromMessage(message: Message): string {
|
|
12
|
+
const parts: string[] = [];
|
|
13
|
+
|
|
14
|
+
for (const block of message.content) {
|
|
15
|
+
switch (block.type) {
|
|
16
|
+
case "text":
|
|
17
|
+
parts.push(block.text);
|
|
18
|
+
break;
|
|
19
|
+
case "tool_use":
|
|
20
|
+
parts.push(`[Tool: ${block.name}(${JSON.stringify(block.input)})]`);
|
|
21
|
+
break;
|
|
22
|
+
case "tool_result":
|
|
23
|
+
const content = typeof block.content === "string"
|
|
24
|
+
? block.content
|
|
25
|
+
: block.content.map(b => b.type === "text" ? b.text : "[content]").join("");
|
|
26
|
+
parts.push(`[Result: ${content.slice(0, 500)}${content.length > 500 ? "..." : ""}]`);
|
|
27
|
+
break;
|
|
28
|
+
case "thinking":
|
|
29
|
+
parts.push(`[Thinking: ${block.thinking.slice(0, 200)}...]`);
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return parts.join("\n");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Extract tool use/result pairs from messages for preservation
|
|
39
|
+
*/
|
|
40
|
+
export function extractToolPairs(messages: Message[]): Map<string, ToolPair> {
|
|
41
|
+
const toolPairs = new Map<string, ToolPair>();
|
|
42
|
+
|
|
43
|
+
// First pass: collect all tool uses
|
|
44
|
+
for (const message of messages) {
|
|
45
|
+
for (const block of message.content) {
|
|
46
|
+
if (block.type === "tool_use") {
|
|
47
|
+
toolPairs.set(block.id, {
|
|
48
|
+
use: { type: "tool_use", id: block.id, name: block.name, input: block.input }
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Second pass: match results to uses
|
|
55
|
+
for (const message of messages) {
|
|
56
|
+
for (const block of message.content) {
|
|
57
|
+
if (block.type === "tool_result") {
|
|
58
|
+
const pair = toolPairs.get(block.tool_use_id);
|
|
59
|
+
if (pair) {
|
|
60
|
+
pair.result = {
|
|
61
|
+
type: "tool_result",
|
|
62
|
+
tool_use_id: block.tool_use_id,
|
|
63
|
+
content: block.content,
|
|
64
|
+
is_error: block.is_error
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return toolPairs;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Extract tool names from a message
|
|
76
|
+
*/
|
|
77
|
+
export function extractToolNames(message: Message): string[] {
|
|
78
|
+
const names: string[] = [];
|
|
79
|
+
for (const block of message.content) {
|
|
80
|
+
if (block.type === "tool_use") {
|
|
81
|
+
names.push(block.name);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return names;
|
|
85
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context Module - Context compaction for conversation management
|
|
3
|
+
*
|
|
4
|
+
* When the context window fills up, we need to compact messages to continue.
|
|
5
|
+
* This module provides token estimation, summarization, and compaction utilities.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { compactMessages, needsCompaction, estimateMessagesTokens } from "./context/index.js";
|
|
10
|
+
*
|
|
11
|
+
* // Check if compaction is needed
|
|
12
|
+
* if (needsCompaction(messages, maxTokens)) {
|
|
13
|
+
* const result = await compactMessages(messages, maxTokens);
|
|
14
|
+
* console.log(`Saved ${result.tokensBefore - result.tokensAfter} tokens`);
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// Types
|
|
20
|
+
export type {
|
|
21
|
+
LLMSummarizationOptions,
|
|
22
|
+
CompactionOptions,
|
|
23
|
+
CompactionResult,
|
|
24
|
+
CompactionStats,
|
|
25
|
+
ToolPair,
|
|
26
|
+
} from "./types.js";
|
|
27
|
+
|
|
28
|
+
// Constants
|
|
29
|
+
export {
|
|
30
|
+
CHARS_PER_TOKEN,
|
|
31
|
+
DEFAULT_KEEP_LAST,
|
|
32
|
+
DEFAULT_KEEP_FIRST,
|
|
33
|
+
MIN_MESSAGES_FOR_COMPACTION,
|
|
34
|
+
DEFAULT_COMPACTION_THRESHOLD,
|
|
35
|
+
MAX_SUMMARY_LENGTH,
|
|
36
|
+
SUMMARY_MAX_TOKENS,
|
|
37
|
+
} from "./constants.js";
|
|
38
|
+
|
|
39
|
+
// Token Estimation
|
|
40
|
+
export {
|
|
41
|
+
estimateTokens,
|
|
42
|
+
estimateBlockTokens,
|
|
43
|
+
estimateMessageTokens,
|
|
44
|
+
estimateMessagesTokens,
|
|
45
|
+
} from "./token-estimation.js";
|
|
46
|
+
|
|
47
|
+
// Extraction
|
|
48
|
+
export {
|
|
49
|
+
extractTextFromMessage,
|
|
50
|
+
extractToolPairs,
|
|
51
|
+
extractToolNames,
|
|
52
|
+
} from "./extraction.js";
|
|
53
|
+
|
|
54
|
+
// Summarization
|
|
55
|
+
export {
|
|
56
|
+
summarizeMessages,
|
|
57
|
+
summarizeWithLLM,
|
|
58
|
+
compactContentNative,
|
|
59
|
+
} from "./summarization.js";
|
|
60
|
+
|
|
61
|
+
// Compaction
|
|
62
|
+
export {
|
|
63
|
+
compactMessages,
|
|
64
|
+
needsCompaction,
|
|
65
|
+
getCompactionStats,
|
|
66
|
+
} from "./compaction.js";
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Summarization - Create summaries of conversation messages
|
|
3
|
+
*
|
|
4
|
+
* Strategies:
|
|
5
|
+
* 1. Native compact_content (Rust) - Fast preprocessing
|
|
6
|
+
* 2. Simple concatenation + truncation - Fallback
|
|
7
|
+
* 3. LLM-based summarization - Best quality when API available
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Message, ToolResultBlock } from "../../types/index.js";
|
|
11
|
+
import { SUMMARIZATION_MODEL } from "../models.js";
|
|
12
|
+
import type { LLMSummarizationOptions } from "./types.js";
|
|
13
|
+
import {
|
|
14
|
+
MAX_SUMMARY_LENGTH,
|
|
15
|
+
SUMMARY_MAX_TOKENS,
|
|
16
|
+
SUMMARIZATION_SYSTEM_PROMPT,
|
|
17
|
+
SUMMARIZATION_PROMPT,
|
|
18
|
+
} from "./constants.js";
|
|
19
|
+
import { extractTextFromMessage, extractToolNames } from "./extraction.js";
|
|
20
|
+
|
|
21
|
+
// Lazy-load native module
|
|
22
|
+
let _native: {
|
|
23
|
+
compact_content?: (
|
|
24
|
+
content: string,
|
|
25
|
+
maxTokens: number,
|
|
26
|
+
strategy?: "truncate" | "summarize" | "extract"
|
|
27
|
+
) => string;
|
|
28
|
+
} | null = null;
|
|
29
|
+
let _nativeLoadAttempted = false;
|
|
30
|
+
|
|
31
|
+
function getNative(): typeof _native {
|
|
32
|
+
if (_nativeLoadAttempted) return _native;
|
|
33
|
+
_nativeLoadAttempted = true;
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
const nativePath = require.resolve("../../../native/index.js");
|
|
37
|
+
const nativeModule = require(nativePath);
|
|
38
|
+
_native = nativeModule.getNative?.() ?? null;
|
|
39
|
+
} catch {
|
|
40
|
+
_native = null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return _native;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Preprocess content using native compact_content if available.
|
|
48
|
+
* Used to reduce large content before LLM summarization.
|
|
49
|
+
*/
|
|
50
|
+
function preprocessContent(content: string, maxTokens: number): string {
|
|
51
|
+
const native = getNative();
|
|
52
|
+
|
|
53
|
+
if (native?.compact_content) {
|
|
54
|
+
// Use native "extract" strategy for better preservation of structure
|
|
55
|
+
return native.compact_content(content, maxTokens, "extract");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Fallback: simple truncation
|
|
59
|
+
const maxChars = maxTokens * 4;
|
|
60
|
+
if (content.length <= maxChars) return content;
|
|
61
|
+
return content.slice(0, maxChars) + "\n\n...[content truncated]";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Simple summarization that concatenates and truncates.
|
|
66
|
+
* Can be enhanced later with LLM-based summarization.
|
|
67
|
+
*/
|
|
68
|
+
export async function summarizeMessages(messages: Message[]): Promise<string> {
|
|
69
|
+
if (!messages || messages.length === 0) {
|
|
70
|
+
return "";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const summaryParts: string[] = [];
|
|
74
|
+
summaryParts.push(`[Context Summary: ${messages.length} messages compacted]\n`);
|
|
75
|
+
|
|
76
|
+
// Track tool operations for a cleaner summary
|
|
77
|
+
const toolOperations: string[] = [];
|
|
78
|
+
|
|
79
|
+
for (let i = 0; i < messages.length; i++) {
|
|
80
|
+
const message = messages[i];
|
|
81
|
+
if (!message) continue;
|
|
82
|
+
|
|
83
|
+
const role = message.role.toUpperCase();
|
|
84
|
+
const text = extractTextFromMessage(message);
|
|
85
|
+
|
|
86
|
+
// Track tool operations
|
|
87
|
+
toolOperations.push(...extractToolNames(message));
|
|
88
|
+
|
|
89
|
+
// Add truncated message content
|
|
90
|
+
const truncated = text.length > 300 ? `${text.slice(0, 300)}...` : text;
|
|
91
|
+
summaryParts.push(`${role}: ${truncated}\n`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Add tool summary
|
|
95
|
+
if (toolOperations.length > 0) {
|
|
96
|
+
const toolCounts = toolOperations.reduce((acc, tool) => {
|
|
97
|
+
acc[tool] = (acc[tool] || 0) + 1;
|
|
98
|
+
return acc;
|
|
99
|
+
}, {} as Record<string, number>);
|
|
100
|
+
|
|
101
|
+
const toolSummary = Object.entries(toolCounts)
|
|
102
|
+
.map(([name, count]) => `${name}(${count})`)
|
|
103
|
+
.join(", ");
|
|
104
|
+
summaryParts.push(`\nTools used: ${toolSummary}\n`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let summary = summaryParts.join("");
|
|
108
|
+
|
|
109
|
+
// Truncate if too long
|
|
110
|
+
if (summary.length > MAX_SUMMARY_LENGTH) {
|
|
111
|
+
summary = summary.slice(0, MAX_SUMMARY_LENGTH) + "\n...[truncated]";
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return summary;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Summarize messages using an LLM for better context preservation.
|
|
119
|
+
* Falls back to simple truncation if LLM fails or no API key provided.
|
|
120
|
+
*/
|
|
121
|
+
export async function summarizeWithLLM(
|
|
122
|
+
messages: Message[],
|
|
123
|
+
options: LLMSummarizationOptions = {}
|
|
124
|
+
): Promise<string> {
|
|
125
|
+
const {
|
|
126
|
+
apiKey = process.env.ANTHROPIC_AUTH_TOKEN || process.env.ANTHROPIC_API_KEY || process.env.CLAUDE_API_KEY,
|
|
127
|
+
model = SUMMARIZATION_MODEL,
|
|
128
|
+
baseUrl = process.env.ANTHROPIC_BASE_URL || "https://api.anthropic.com",
|
|
129
|
+
timeout = 30000,
|
|
130
|
+
} = options;
|
|
131
|
+
|
|
132
|
+
// No API key - fall back to simple summarization
|
|
133
|
+
if (!apiKey) {
|
|
134
|
+
return summarizeMessages(messages);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
// Build the conversation text for summarization
|
|
139
|
+
let conversationText = messages.map((msg) => {
|
|
140
|
+
const role = msg.role.toUpperCase();
|
|
141
|
+
const text = extractTextFromMessage(msg);
|
|
142
|
+
|
|
143
|
+
// Extract tool info
|
|
144
|
+
const tools: string[] = [];
|
|
145
|
+
for (const block of msg.content) {
|
|
146
|
+
if (block.type === "tool_use") {
|
|
147
|
+
tools.push(`[TOOL_USE: ${block.name}]`);
|
|
148
|
+
} else if (block.type === "tool_result") {
|
|
149
|
+
const resultBlock = block as ToolResultBlock;
|
|
150
|
+
const preview = typeof resultBlock.content === "string"
|
|
151
|
+
? resultBlock.content.slice(0, 200)
|
|
152
|
+
: "[complex result]";
|
|
153
|
+
tools.push(`[TOOL_RESULT: ${resultBlock.is_error ? "ERROR" : "OK"}] ${preview}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const toolsStr = tools.length > 0 ? `\n${tools.join("\n")}` : "";
|
|
158
|
+
return `${role}:\n${text.slice(0, 2000)}${toolsStr}`;
|
|
159
|
+
}).join("\n\n---\n\n");
|
|
160
|
+
|
|
161
|
+
// Preprocess with native if content is very large (reduce API costs)
|
|
162
|
+
const estimatedInputTokens = Math.ceil(conversationText.length / 4);
|
|
163
|
+
if (estimatedInputTokens > 8000) {
|
|
164
|
+
// Use native preprocessing to reduce size before LLM
|
|
165
|
+
conversationText = preprocessContent(conversationText, 6000);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Build request
|
|
169
|
+
const requestBody = {
|
|
170
|
+
model,
|
|
171
|
+
max_tokens: SUMMARY_MAX_TOKENS,
|
|
172
|
+
system: SUMMARIZATION_SYSTEM_PROMPT,
|
|
173
|
+
messages: [{
|
|
174
|
+
role: "user" as const,
|
|
175
|
+
content: SUMMARIZATION_PROMPT.replace("{{MESSAGES}}", conversationText),
|
|
176
|
+
}],
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// Make API call with timeout
|
|
180
|
+
const controller = new AbortController();
|
|
181
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
const response = await fetch(`${baseUrl}/v1/messages`, {
|
|
185
|
+
method: "POST",
|
|
186
|
+
headers: {
|
|
187
|
+
"Content-Type": "application/json",
|
|
188
|
+
"x-api-key": apiKey,
|
|
189
|
+
"anthropic-version": "2023-06-01",
|
|
190
|
+
},
|
|
191
|
+
body: JSON.stringify(requestBody),
|
|
192
|
+
signal: controller.signal,
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
clearTimeout(timeoutId);
|
|
196
|
+
|
|
197
|
+
if (!response.ok) {
|
|
198
|
+
const errorText = await response.text();
|
|
199
|
+
console.error(`\x1b[33m[Compaction] LLM summarization failed: ${response.status} - ${errorText}\x1b[0m`);
|
|
200
|
+
return summarizeMessages(messages);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const data = await response.json() as {
|
|
204
|
+
content?: Array<{ type: string; text?: string }>;
|
|
205
|
+
usage?: { input_tokens: number; output_tokens: number };
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// Extract text from response
|
|
209
|
+
const summaryText = data.content
|
|
210
|
+
?.filter((block) => block.type === "text")
|
|
211
|
+
.map((block) => block.text || "")
|
|
212
|
+
.join("\n") || "";
|
|
213
|
+
|
|
214
|
+
if (!summaryText) {
|
|
215
|
+
console.error("\x1b[33m[Compaction] LLM returned empty summary\x1b[0m");
|
|
216
|
+
return summarizeMessages(messages);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Log usage for debugging
|
|
220
|
+
if (data.usage) {
|
|
221
|
+
console.log(`\x1b[90m[Compaction] LLM summary: ${data.usage.input_tokens} in, ${data.usage.output_tokens} out\x1b[0m`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return `[LLM Summary of ${messages.length} messages]\n\n${summaryText}`;
|
|
225
|
+
|
|
226
|
+
} finally {
|
|
227
|
+
clearTimeout(timeoutId);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
} catch (error) {
|
|
231
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
232
|
+
console.error(`\x1b[33m[Compaction] LLM summarization error: ${errorMsg}\x1b[0m`);
|
|
233
|
+
// Fall back to simple summarization
|
|
234
|
+
return summarizeMessages(messages);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Compact content using native module (exposed for direct use)
|
|
240
|
+
*/
|
|
241
|
+
export function compactContentNative(
|
|
242
|
+
content: string,
|
|
243
|
+
maxTokens: number,
|
|
244
|
+
strategy: "truncate" | "summarize" | "extract" = "extract"
|
|
245
|
+
): string | null {
|
|
246
|
+
const native = getNative();
|
|
247
|
+
if (native?.compact_content) {
|
|
248
|
+
return native.compact_content(content, maxTokens, strategy);
|
|
249
|
+
}
|
|
250
|
+
return null;
|
|
251
|
+
}
|