@ebowwa/coder 0.2.1 → 0.7.64
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/README.md +31 -32
- 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 +32 -2
- 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 +167 -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/index.d.ts +480 -0
- package/dist/native/index.d.ts.map +1 -0
- package/dist/native/index.js +1625 -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/README.md +5 -5
- package/native/index.darwin-arm64.node +0 -0
- package/native/index.node +0 -0
- package/native/package.json +4 -4
- package/package.json +33 -16
- package/packages/src/core/__tests__/permissions.test.ts +1091 -0
- package/packages/src/core/agent-loop/__tests__/compaction.test.ts +280 -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 +88 -0
- package/packages/src/core/agent-loop/formatters.ts +50 -0
- package/packages/src/core/agent-loop/index.ts +135 -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 +222 -0
- package/packages/src/core/agent-loop/types.ts +148 -0
- package/packages/src/core/agent-loop.ts +18 -0
- package/packages/src/core/api-client-impl.ts +619 -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 +590 -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-loader.ts +324 -0
- package/packages/src/core/context-compaction.ts +578 -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 +430 -0
- package/packages/src/core/normalizers/todo +4 -0
- package/packages/src/core/permissions.ts +431 -0
- package/packages/src/core/retry.ts +170 -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 +1877 -0
- package/packages/src/index.ts +120 -0
- package/packages/src/interfaces/mcp/client.ts +389 -0
- package/packages/src/interfaces/ui/Screenshot 2026-03-02 at 9.23.10/342/200/257PM.png +0 -0
- package/packages/src/interfaces/ui/Screenshot 2026-03-03 at 10.55.11/342/200/257AM.png +0 -0
- package/packages/src/interfaces/ui/index.ts +161 -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/cli/index.ts +228 -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 +71 -0
- package/packages/src/interfaces/ui/terminal/shared/loading-state.ts +322 -0
- package/packages/src/interfaces/ui/terminal/shared/query.ts +146 -0
- package/packages/src/interfaces/ui/terminal/shared/setup.ts +295 -0
- package/packages/src/interfaces/ui/terminal/shared/status-line.ts +358 -0
- package/packages/src/interfaces/ui/terminal/shared/system-prompt.ts +146 -0
- package/packages/src/interfaces/ui/terminal/tui/HelpPanel.tsx +262 -0
- package/packages/src/interfaces/ui/terminal/tui/InputContext.tsx +232 -0
- package/packages/src/interfaces/ui/terminal/tui/InputField.tsx +62 -0
- package/packages/src/interfaces/ui/terminal/tui/InteractiveTUI.tsx +537 -0
- package/packages/src/interfaces/ui/terminal/tui/MessageArea.tsx +107 -0
- package/packages/src/interfaces/ui/terminal/tui/MessageStore.tsx +240 -0
- package/packages/src/interfaces/ui/terminal/tui/StatusBar.tsx +54 -0
- package/packages/src/interfaces/ui/terminal/tui/commands.ts +438 -0
- package/packages/src/interfaces/ui/terminal/tui/components/InteractiveElements.tsx +584 -0
- package/packages/src/interfaces/ui/terminal/tui/components/MultilineInput.tsx +614 -0
- package/packages/src/interfaces/ui/terminal/tui/components/PaneManager.tsx +333 -0
- package/packages/src/interfaces/ui/terminal/tui/components/Sidebar.tsx +604 -0
- package/packages/src/interfaces/ui/terminal/tui/components/index.ts +118 -0
- package/packages/src/interfaces/ui/terminal/tui/console.ts +49 -0
- package/packages/src/interfaces/ui/terminal/tui/index.ts +90 -0
- package/packages/src/interfaces/ui/terminal/tui/run.tsx +42 -0
- package/packages/src/interfaces/ui/terminal/tui/spinner.ts +69 -0
- package/packages/src/interfaces/ui/terminal/tui/tui-app.tsx +390 -0
- package/packages/src/interfaces/ui/terminal/tui/tui-footer.ts +422 -0
- package/packages/src/interfaces/ui/terminal/tui/types.ts +186 -0
- package/packages/src/interfaces/ui/terminal/tui/useInputHandler.ts +104 -0
- package/packages/src/interfaces/ui/terminal/tui/useNativeInput.ts +239 -0
- package/packages/src/lmdb.db +0 -0
- package/packages/src/lmdb.db-lock +0 -0
- package/packages/src/native/index.ts +2345 -0
- package/packages/src/teammates/index.ts +982 -0
- package/packages/src/types/index.ts +722 -0
- package/dist/cli.js +0 -148
- package/dist/index-0pkak453.js +0 -136
- package/dist/index-0qd0x8b4.js +0 -110
- package/dist/index-0x3kprq6.js +0 -240
- package/dist/index-1eawy937.js +0 -308
- package/dist/index-24m2aygy.js +0 -240
- package/dist/index-29xcjnne.js +0 -280
- package/dist/index-2avyytn5.js +0 -349
- package/dist/index-4ms367ey.js +0 -136
- package/dist/index-4w2t3b0m.js +0 -240
- package/dist/index-4xfgd8nz.js +0 -261
- package/dist/index-5acjp9gc.js +0 -157
- package/dist/index-5s15hr56.js +0 -136
- package/dist/index-6e4wf341.js +0 -349
- package/dist/index-6fvnkedw.js +0 -240
- package/dist/index-6rqpmd4g.js +0 -128
- package/dist/index-77ckwnbm.js +0 -280
- package/dist/index-9knxy49k.js +0 -128
- package/dist/index-9zrnw4zx.js +0 -128
- package/dist/index-bk21w99v.js +0 -280
- package/dist/index-c41n76fv.js +0 -240
- package/dist/index-cb4ppjdt.js +0 -255
- package/dist/index-cfb2edt6.js +0 -240
- package/dist/index-cmfa38hh.js +0 -308
- package/dist/index-datjz8q1.js +0 -257
- package/dist/index-eadf4wvn.js +0 -240
- package/dist/index-em5k0m3z.js +0 -345
- package/dist/index-gh8r333a.js +0 -110
- package/dist/index-gkx6k2tr.js +0 -261
- package/dist/index-h5cabfks.js +0 -155
- package/dist/index-hcrpwyy3.js +0 -261
- package/dist/index-hk7fwwa8.js +0 -257
- package/dist/index-jb8cw7f8.js +0 -136
- package/dist/index-kbyw4th1.js +0 -347
- package/dist/index-kgj5gqnm.js +0 -345
- package/dist/index-mdf6xp1z.js +0 -255
- package/dist/index-mrhv8kvc.js +0 -280
- package/dist/index-mt4743dd.js +0 -161
- package/dist/index-qnwsg97q.js +0 -240
- package/dist/index-qwdy6x44.js +0 -261
- package/dist/index-rmj77261.js +0 -157
- package/dist/index-sbbw1a61.js +0 -349
- package/dist/index-svy5bcpn.js +0 -345
- package/dist/index-tvmy7tm9.js +0 -261
- package/dist/index-tzz4vzkj.js +0 -312
- package/dist/index-vz80zmhe.js +0 -110
- package/dist/index-wed2fk67.js +0 -240
- package/dist/index-wksgzz8e.js +0 -280
- package/dist/index-wn2m4wma.js +0 -240
- package/dist/index-xha05vjc.js +0 -257
- package/dist/index-yc6eh8p8.js +0 -136
- package/dist/index-ycjxx9ft.js +0 -240
- package/dist/index-z0gzd0fc.js +0 -110
- package/dist/index-z8cwtf8j.js +0 -240
- package/dist/index-zy5mtt00.js +0 -128
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API Client - SSE streaming for LLM APIs
|
|
3
|
+
*/
|
|
4
|
+
import { DEFAULT_CACHE_CONFIG, calculateBudgetTokens, supportsExtendedThinking as supportsThinkingType, EFFORT_TO_BUDGET, } from "../types/index.js";
|
|
5
|
+
import { withRetry, parseRetryAfter } from "./retry.js";
|
|
6
|
+
import { calculateCost as calculateModelCost, DEFAULT_MODEL, supportsExtendedThinking, } from "./models.js";
|
|
7
|
+
/**
|
|
8
|
+
* Calculate cost for API usage including cache metrics
|
|
9
|
+
* Delegates to models.ts for centralized pricing
|
|
10
|
+
*/
|
|
11
|
+
export function calculateCost(model, usage) {
|
|
12
|
+
return calculateModelCost(model, usage);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Build system prompt with cache control
|
|
16
|
+
*/
|
|
17
|
+
export function buildSystemPrompt(systemPrompt, cacheConfig) {
|
|
18
|
+
if (!systemPrompt || !cacheConfig.enabled || !cacheConfig.cacheSystemPrompt) {
|
|
19
|
+
return typeof systemPrompt === "string" ? systemPrompt : undefined;
|
|
20
|
+
}
|
|
21
|
+
// If already in block format, add cache_control to the last block
|
|
22
|
+
if (Array.isArray(systemPrompt)) {
|
|
23
|
+
const blocks = [...systemPrompt];
|
|
24
|
+
if (blocks.length > 0) {
|
|
25
|
+
const lastBlock = blocks[blocks.length - 1];
|
|
26
|
+
if (lastBlock && lastBlock.type === "text") {
|
|
27
|
+
blocks[blocks.length - 1] = {
|
|
28
|
+
type: "text",
|
|
29
|
+
text: lastBlock.text,
|
|
30
|
+
cache_control: { type: "ephemeral", ttl: cacheConfig.ttl },
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return blocks;
|
|
35
|
+
}
|
|
36
|
+
// Convert string to block format with cache_control
|
|
37
|
+
return [
|
|
38
|
+
{
|
|
39
|
+
type: "text",
|
|
40
|
+
text: systemPrompt,
|
|
41
|
+
cache_control: { type: "ephemeral", ttl: cacheConfig.ttl },
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Build messages with cache control for long context blocks
|
|
47
|
+
*/
|
|
48
|
+
export function buildCachedMessages(messages, cacheConfig) {
|
|
49
|
+
if (!cacheConfig.enabled) {
|
|
50
|
+
return messages;
|
|
51
|
+
}
|
|
52
|
+
const result = [];
|
|
53
|
+
for (let i = 0; i < messages.length; i++) {
|
|
54
|
+
const msg = messages[i];
|
|
55
|
+
// Convert string content to content blocks array
|
|
56
|
+
const contentBlocks = typeof msg.content === "string"
|
|
57
|
+
? [{ type: "text", text: msg.content }]
|
|
58
|
+
: msg.content;
|
|
59
|
+
const cachedContent = [];
|
|
60
|
+
for (let j = 0; j < contentBlocks.length; j++) {
|
|
61
|
+
const block = contentBlocks[j];
|
|
62
|
+
const isLastBlock = j === contentBlocks.length - 1;
|
|
63
|
+
const isLastMessage = i === messages.length - 1;
|
|
64
|
+
// Add cache_control to large text blocks (especially in user messages)
|
|
65
|
+
if (block.type === "text" &&
|
|
66
|
+
block.text.length >= cacheConfig.minTokensForCache * 4 && // Approximate chars per token
|
|
67
|
+
!block.cache_control &&
|
|
68
|
+
(isLastBlock || isLastMessage)) {
|
|
69
|
+
cachedContent.push({
|
|
70
|
+
...block,
|
|
71
|
+
cache_control: { type: "ephemeral", ttl: cacheConfig.ttl },
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
cachedContent.push(block);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
result.push({ ...msg, content: cachedContent });
|
|
79
|
+
}
|
|
80
|
+
// Ensure the last message has cache_control on its last content block
|
|
81
|
+
if (result.length > 0 && cacheConfig.enabled) {
|
|
82
|
+
const lastMsg = result[result.length - 1];
|
|
83
|
+
const lastBlock = lastMsg.content[lastMsg.content.length - 1];
|
|
84
|
+
if (lastBlock && !("cache_control" in lastBlock)) {
|
|
85
|
+
lastMsg.content[lastMsg.content.length - 1] = {
|
|
86
|
+
...lastBlock,
|
|
87
|
+
cache_control: { type: "ephemeral", ttl: cacheConfig.ttl },
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Calculate cache metrics from usage
|
|
95
|
+
*/
|
|
96
|
+
export function calculateCacheMetrics(usage) {
|
|
97
|
+
const cacheReadTokens = usage.cache_read_input_tokens ?? 0;
|
|
98
|
+
const cacheWriteTokens = usage.cache_creation_input_tokens ?? 0;
|
|
99
|
+
const totalCacheTokens = cacheReadTokens + cacheWriteTokens;
|
|
100
|
+
const cacheHits = cacheReadTokens > 0 ? 1 : 0;
|
|
101
|
+
const cacheMisses = cacheWriteTokens > 0 ? 1 : 0;
|
|
102
|
+
const total = cacheHits + cacheMisses;
|
|
103
|
+
return {
|
|
104
|
+
cacheHits,
|
|
105
|
+
cacheMisses,
|
|
106
|
+
totalCacheReadTokens: cacheReadTokens,
|
|
107
|
+
totalCacheWriteTokens: cacheWriteTokens,
|
|
108
|
+
cacheHitRate: total > 0 ? cacheHits / total : 0,
|
|
109
|
+
estimatedSavingsUSD: 0, // Will be calculated after pricing lookup
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Create a streaming message request to Anthropic API
|
|
114
|
+
*/
|
|
115
|
+
export async function createMessageStream(messages, options) {
|
|
116
|
+
const { apiKey, model = "claude-sonnet-4-6", maxTokens = 4096, tools, systemPrompt, cacheConfig = DEFAULT_CACHE_CONFIG, thinking, extendedThinking, onToken, onThinking, onRedactedThinking, onToolUse, signal, } = options;
|
|
117
|
+
const startTime = Date.now();
|
|
118
|
+
let ttft = 0;
|
|
119
|
+
let firstToken = true;
|
|
120
|
+
let totalThinkingTokens = 0;
|
|
121
|
+
// Build cached messages
|
|
122
|
+
const cachedMessages = buildCachedMessages(messages, cacheConfig);
|
|
123
|
+
// Build system prompt with cache control
|
|
124
|
+
const cachedSystemPrompt = buildSystemPrompt(systemPrompt, cacheConfig);
|
|
125
|
+
// Build request
|
|
126
|
+
const request = {
|
|
127
|
+
model,
|
|
128
|
+
max_tokens: maxTokens,
|
|
129
|
+
messages: cachedMessages.map((m) => ({
|
|
130
|
+
role: m.role,
|
|
131
|
+
content: m.content,
|
|
132
|
+
})),
|
|
133
|
+
stream: true,
|
|
134
|
+
};
|
|
135
|
+
// Add system prompt if provided
|
|
136
|
+
if (cachedSystemPrompt) {
|
|
137
|
+
request.system = cachedSystemPrompt;
|
|
138
|
+
}
|
|
139
|
+
// Add tools if provided (with optional caching)
|
|
140
|
+
if (tools && tools.length > 0) {
|
|
141
|
+
request.tools = tools;
|
|
142
|
+
}
|
|
143
|
+
// Determine API endpoint (support custom base URL for GLM, etc.)
|
|
144
|
+
const baseUrl = process.env.ANTHROPIC_BASE_URL || "https://api.anthropic.com";
|
|
145
|
+
const apiEndpoint = `${baseUrl}/v1/messages`;
|
|
146
|
+
// Build headers
|
|
147
|
+
const headers = {
|
|
148
|
+
"Content-Type": "application/json",
|
|
149
|
+
"x-api-key": apiKey,
|
|
150
|
+
"anthropic-version": "2023-06-01",
|
|
151
|
+
};
|
|
152
|
+
// Determine thinking configuration
|
|
153
|
+
const shouldUseExtendedThinking = (extendedThinking?.enabled ?? false) ||
|
|
154
|
+
(thinking && thinking.type !== "disabled");
|
|
155
|
+
if (shouldUseExtendedThinking && supportsExtendedThinking(model)) {
|
|
156
|
+
// Calculate budget tokens
|
|
157
|
+
let budgetTokens;
|
|
158
|
+
if (extendedThinking?.budgetTokens) {
|
|
159
|
+
budgetTokens = extendedThinking.budgetTokens;
|
|
160
|
+
}
|
|
161
|
+
else if (thinking?.type === "enabled") {
|
|
162
|
+
budgetTokens = thinking.budget_tokens;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
// Use effort level to determine budget
|
|
166
|
+
const effort = extendedThinking?.effort || "medium";
|
|
167
|
+
budgetTokens = calculateBudgetTokens({
|
|
168
|
+
enabled: true,
|
|
169
|
+
effort,
|
|
170
|
+
modelMultiplier: model.includes("opus") ? 2 : 1,
|
|
171
|
+
}, model);
|
|
172
|
+
}
|
|
173
|
+
// Clamp budget to valid range
|
|
174
|
+
budgetTokens = Math.max(1024, Math.min(budgetTokens, 100000));
|
|
175
|
+
request.thinking = {
|
|
176
|
+
type: "enabled",
|
|
177
|
+
budget_tokens: budgetTokens,
|
|
178
|
+
};
|
|
179
|
+
// Add beta headers for extended thinking features
|
|
180
|
+
const betaFeatures = ["extended-thinking-2025-01-24"];
|
|
181
|
+
// Add interleaved thinking support if enabled
|
|
182
|
+
if (extendedThinking?.interleaved !== false) {
|
|
183
|
+
betaFeatures.push("interleaved-thinking-2025-01-24");
|
|
184
|
+
}
|
|
185
|
+
headers["anthropic-beta"] = betaFeatures.join(",");
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
// Default beta header
|
|
189
|
+
headers["anthropic-beta"] = "max-tokens-3-5-sonnet-2024-07-15";
|
|
190
|
+
}
|
|
191
|
+
// Make API request with retry logic
|
|
192
|
+
const retryOptions = {
|
|
193
|
+
maxRetries: 3,
|
|
194
|
+
baseDelayMs: 1000,
|
|
195
|
+
maxDelayMs: 30000,
|
|
196
|
+
retryableStatusCodes: [429, 500, 502, 503, 504, 529],
|
|
197
|
+
onRetry: (attempt, error, delayMs) => {
|
|
198
|
+
console.log(`\x1b[33mAPI retry ${attempt}/3 after ${delayMs}ms: ${error.message}\x1b[0m`);
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
const response = await withRetry(async () => {
|
|
202
|
+
const res = await fetch(apiEndpoint, {
|
|
203
|
+
method: "POST",
|
|
204
|
+
headers,
|
|
205
|
+
body: JSON.stringify(request),
|
|
206
|
+
signal,
|
|
207
|
+
});
|
|
208
|
+
// Throw for retryable status codes so withRetry can handle them
|
|
209
|
+
if (!res.ok && retryOptions.retryableStatusCodes?.includes(res.status)) {
|
|
210
|
+
const errorText = await res.text();
|
|
211
|
+
throw new Error(`API error: ${res.status} - ${errorText}`);
|
|
212
|
+
}
|
|
213
|
+
return res;
|
|
214
|
+
}, retryOptions);
|
|
215
|
+
if (!response.ok) {
|
|
216
|
+
const error = await response.text();
|
|
217
|
+
throw new Error(`API error: ${response.status} - ${error}`);
|
|
218
|
+
}
|
|
219
|
+
if (!response.body) {
|
|
220
|
+
throw new Error("No response body");
|
|
221
|
+
}
|
|
222
|
+
// Parse SSE stream
|
|
223
|
+
const reader = response.body.getReader();
|
|
224
|
+
const decoder = new TextDecoder();
|
|
225
|
+
let message = null;
|
|
226
|
+
let currentContent = [];
|
|
227
|
+
let usage = { input_tokens: 0, output_tokens: 0 };
|
|
228
|
+
let currentTextBlock = null;
|
|
229
|
+
let currentThinkingBlock = null;
|
|
230
|
+
let currentRedactedThinkingBlock = null;
|
|
231
|
+
let currentToolUseBlock = null;
|
|
232
|
+
let toolUseInput = "";
|
|
233
|
+
const buffer = "";
|
|
234
|
+
try {
|
|
235
|
+
let buffer = "";
|
|
236
|
+
while (true) {
|
|
237
|
+
const { done, value } = await reader.read();
|
|
238
|
+
if (done)
|
|
239
|
+
break;
|
|
240
|
+
buffer += decoder.decode(value, { stream: true });
|
|
241
|
+
const lines = buffer.split("\n");
|
|
242
|
+
buffer = lines.pop() || "";
|
|
243
|
+
for (const line of lines) {
|
|
244
|
+
if (!line.startsWith("data: "))
|
|
245
|
+
continue;
|
|
246
|
+
const data = line.slice(6);
|
|
247
|
+
if (!data)
|
|
248
|
+
continue;
|
|
249
|
+
// Debug: Log all SSE data when debug enabled
|
|
250
|
+
if (process.env.DEBUG_API === '1') {
|
|
251
|
+
console.log('\x1b[90m[DEBUG] SSE data:\x1b[0m', data.substring(0, 200));
|
|
252
|
+
}
|
|
253
|
+
try {
|
|
254
|
+
const event = JSON.parse(data);
|
|
255
|
+
// Debug: Log event types
|
|
256
|
+
if (process.env.DEBUG_API === '1' && event.type) {
|
|
257
|
+
console.log('\x1b[90m[DEBUG] SSE event type:\x1b[0m', event.type);
|
|
258
|
+
}
|
|
259
|
+
switch (event.type) {
|
|
260
|
+
case "error": {
|
|
261
|
+
// API returned an error - surface it
|
|
262
|
+
const errorEvent = event;
|
|
263
|
+
const errorMsg = errorEvent.error?.message || errorEvent.error?.type || "Unknown API error";
|
|
264
|
+
console.error(`API error: ${errorMsg}`);
|
|
265
|
+
// Include more details in debug mode
|
|
266
|
+
if (process.env.DEBUG_API === '1') {
|
|
267
|
+
console.log('\x1b[91m[DEBUG] API error event:', JSON.stringify(errorEvent, null, 255));
|
|
268
|
+
}
|
|
269
|
+
throw new Error(`API error: ${errorMsg}`);
|
|
270
|
+
}
|
|
271
|
+
case "message_start": {
|
|
272
|
+
const msg = event.message;
|
|
273
|
+
message = msg;
|
|
274
|
+
usage = msg.usage;
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
case "content_block_start": {
|
|
278
|
+
const block = event.content_block;
|
|
279
|
+
if (block.type === "text") {
|
|
280
|
+
currentTextBlock = { type: "text", text: "" };
|
|
281
|
+
}
|
|
282
|
+
else if (block.type === "thinking") {
|
|
283
|
+
currentThinkingBlock = { type: "thinking", thinking: "" };
|
|
284
|
+
}
|
|
285
|
+
else if (block.type === "redacted_thinking") {
|
|
286
|
+
currentRedactedThinkingBlock = { type: "redacted_thinking", data: "" };
|
|
287
|
+
}
|
|
288
|
+
else if (block.type === "tool_use") {
|
|
289
|
+
currentToolUseBlock = {
|
|
290
|
+
type: "tool_use",
|
|
291
|
+
id: block.id,
|
|
292
|
+
name: block.name,
|
|
293
|
+
input: {},
|
|
294
|
+
};
|
|
295
|
+
toolUseInput = "";
|
|
296
|
+
}
|
|
297
|
+
break;
|
|
298
|
+
}
|
|
299
|
+
case "content_block_delta": {
|
|
300
|
+
const delta = event.delta;
|
|
301
|
+
if (delta.type === "text_delta" && currentTextBlock) {
|
|
302
|
+
const text = delta.text;
|
|
303
|
+
currentTextBlock.text += text;
|
|
304
|
+
onToken?.(text);
|
|
305
|
+
if (firstToken) {
|
|
306
|
+
ttft = Date.now() - startTime;
|
|
307
|
+
firstToken = false;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
else if (delta.type === "thinking_delta" && currentThinkingBlock) {
|
|
311
|
+
const thinking = delta.thinking;
|
|
312
|
+
currentThinkingBlock.thinking += thinking;
|
|
313
|
+
onThinking?.(thinking);
|
|
314
|
+
totalThinkingTokens += Math.ceil(thinking.length / 4); // Rough estimate
|
|
315
|
+
}
|
|
316
|
+
else if (delta.type === "redacted_thinking_delta" && currentRedactedThinkingBlock) {
|
|
317
|
+
// Handle redacted thinking deltas
|
|
318
|
+
const redactedData = delta.data;
|
|
319
|
+
currentRedactedThinkingBlock.data += redactedData;
|
|
320
|
+
onRedactedThinking?.(redactedData);
|
|
321
|
+
totalThinkingTokens += Math.ceil(redactedData.length / 4); // Rough estimate
|
|
322
|
+
}
|
|
323
|
+
else if (delta.type === "input_json_delta" && currentToolUseBlock) {
|
|
324
|
+
toolUseInput += delta.partial_json;
|
|
325
|
+
}
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
case "content_block_stop": {
|
|
329
|
+
// content_block_stop event has { index: number }, not the block itself
|
|
330
|
+
// We need to check which current block is active and push it
|
|
331
|
+
if (currentTextBlock !== null) {
|
|
332
|
+
currentContent.push(currentTextBlock);
|
|
333
|
+
currentTextBlock = null;
|
|
334
|
+
}
|
|
335
|
+
else if (currentThinkingBlock !== null) {
|
|
336
|
+
currentContent.push(currentThinkingBlock);
|
|
337
|
+
currentThinkingBlock = null;
|
|
338
|
+
}
|
|
339
|
+
else if (currentRedactedThinkingBlock !== null) {
|
|
340
|
+
currentContent.push(currentRedactedThinkingBlock);
|
|
341
|
+
onRedactedThinking?.(currentRedactedThinkingBlock.data);
|
|
342
|
+
currentRedactedThinkingBlock = null;
|
|
343
|
+
}
|
|
344
|
+
else if (currentToolUseBlock !== null) {
|
|
345
|
+
try {
|
|
346
|
+
currentToolUseBlock.input = JSON.parse(toolUseInput);
|
|
347
|
+
}
|
|
348
|
+
catch {
|
|
349
|
+
currentToolUseBlock.input = {};
|
|
350
|
+
}
|
|
351
|
+
currentContent.push(currentToolUseBlock);
|
|
352
|
+
onToolUse?.({
|
|
353
|
+
id: currentToolUseBlock.id,
|
|
354
|
+
name: currentToolUseBlock.name,
|
|
355
|
+
input: currentToolUseBlock.input,
|
|
356
|
+
});
|
|
357
|
+
currentToolUseBlock = null;
|
|
358
|
+
toolUseInput = "";
|
|
359
|
+
}
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
case "message_delta": {
|
|
363
|
+
const evt = event;
|
|
364
|
+
if (evt.usage) {
|
|
365
|
+
usage.output_tokens = evt.usage.output_tokens;
|
|
366
|
+
}
|
|
367
|
+
if (message && evt.delta?.stop_reason) {
|
|
368
|
+
message.stop_reason = evt.delta.stop_reason;
|
|
369
|
+
}
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
case "message_stop":
|
|
373
|
+
// Message complete
|
|
374
|
+
break;
|
|
375
|
+
// OpenAI/Z.AI compatible format (for GLM-5, etc.)
|
|
376
|
+
// OpenAI streaming sends chunks with choices array
|
|
377
|
+
default: {
|
|
378
|
+
// Check for OpenAI format: { choices: [{ delta: { content: "..." } }], usage: {...} }
|
|
379
|
+
if (event.choices && Array.isArray(event.choices)) {
|
|
380
|
+
const choice = event.choices[0];
|
|
381
|
+
if (choice?.delta?.content) {
|
|
382
|
+
const text = choice.delta.content;
|
|
383
|
+
if (currentTextBlock) {
|
|
384
|
+
currentTextBlock.text += text;
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
currentTextBlock = { type: "text", text };
|
|
388
|
+
}
|
|
389
|
+
onToken?.(text);
|
|
390
|
+
if (firstToken) {
|
|
391
|
+
ttft = Date.now() - startTime;
|
|
392
|
+
firstToken = false;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
// Check for finish
|
|
396
|
+
if (choice?.finish_reason) {
|
|
397
|
+
if (currentTextBlock) {
|
|
398
|
+
currentContent.push(currentTextBlock);
|
|
399
|
+
currentTextBlock = null;
|
|
400
|
+
}
|
|
401
|
+
if (!message) {
|
|
402
|
+
message = {
|
|
403
|
+
id: `msg-${Date.now()}`,
|
|
404
|
+
type: "message",
|
|
405
|
+
role: "assistant",
|
|
406
|
+
content: currentContent,
|
|
407
|
+
model: model,
|
|
408
|
+
stop_reason: (choice.finish_reason === "stop" ? "end_turn" : choice.finish_reason === "length" ? "max_tokens" : "end_turn"),
|
|
409
|
+
stop_sequence: null,
|
|
410
|
+
usage: { input_tokens: 0, output_tokens: 0 },
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
message.stop_reason = (choice.finish_reason === "stop" ? "end_turn" : choice.finish_reason === "length" ? "max_tokens" : "end_turn");
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
// OpenAI usage format (often in final chunk)
|
|
419
|
+
if (event.usage) {
|
|
420
|
+
const openaiUsage = event.usage;
|
|
421
|
+
usage.input_tokens = openaiUsage.prompt_tokens || 0;
|
|
422
|
+
usage.output_tokens = openaiUsage.completion_tokens || 0;
|
|
423
|
+
}
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
catch (err) {
|
|
429
|
+
// Log the parse error with more detail
|
|
430
|
+
if (process.env.DEBUG_API === '1') {
|
|
431
|
+
console.error('\x1b[91m[DEBUG] JSON parse error:', err);
|
|
432
|
+
console.error('\x1b[91m[DEBUG] Error parsing SSE data:', data.substring(0, 200));
|
|
433
|
+
console.error('\x1b[91m[DEBUG] Original buffer:', buffer.substring(0, 500));
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
finally {
|
|
440
|
+
reader.releaseLock();
|
|
441
|
+
}
|
|
442
|
+
if (!message) {
|
|
443
|
+
// If we received content via OpenAI format but no message_start, create a message
|
|
444
|
+
if (currentContent.length > 0) {
|
|
445
|
+
message = {
|
|
446
|
+
id: `msg-${Date.now()}`,
|
|
447
|
+
type: "message",
|
|
448
|
+
role: "assistant",
|
|
449
|
+
content: currentContent,
|
|
450
|
+
model: model,
|
|
451
|
+
stop_reason: "end_turn",
|
|
452
|
+
stop_sequence: null,
|
|
453
|
+
usage: { input_tokens: 0, output_tokens: 0 },
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
// Debug: Log what we did receive
|
|
458
|
+
if (process.env.DEBUG_API === '1') {
|
|
459
|
+
console.log('\x1b[91m[DEBUG] No message_start event received. Buffer:\x1b[0m', buffer.substring(0, 500));
|
|
460
|
+
}
|
|
461
|
+
throw new Error("No message received from API");
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
message.content = currentContent;
|
|
465
|
+
// Calculate cost and cache metrics
|
|
466
|
+
const { costUSD, estimatedSavingsUSD } = calculateCost(model, usage);
|
|
467
|
+
const cacheMetrics = calculateCacheMetrics(usage);
|
|
468
|
+
cacheMetrics.estimatedSavingsUSD = estimatedSavingsUSD;
|
|
469
|
+
const durationMs = Date.now() - startTime;
|
|
470
|
+
return {
|
|
471
|
+
message,
|
|
472
|
+
usage,
|
|
473
|
+
cacheMetrics,
|
|
474
|
+
costUSD,
|
|
475
|
+
durationMs,
|
|
476
|
+
ttftMs: ttft || durationMs,
|
|
477
|
+
thinkingTokens: totalThinkingTokens,
|
|
478
|
+
};
|
|
479
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-client.d.ts","sourceRoot":"","sources":["../../packages/src/core/api-client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,sBAAsB,CAAC;AACrC,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checkpoint Manager - Save and restore conversation states
|
|
3
|
+
* Captures both chat context AND code/file changes
|
|
4
|
+
*/
|
|
5
|
+
import type { Message } from "../types/index.js";
|
|
6
|
+
export interface FileSnapshot {
|
|
7
|
+
path: string;
|
|
8
|
+
content: string;
|
|
9
|
+
hash: string;
|
|
10
|
+
}
|
|
11
|
+
export interface GitState {
|
|
12
|
+
branch: string;
|
|
13
|
+
ahead: number;
|
|
14
|
+
behind: number;
|
|
15
|
+
staged: string[];
|
|
16
|
+
unstaged: string[];
|
|
17
|
+
untracked: string[];
|
|
18
|
+
stash?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface Checkpoint {
|
|
21
|
+
id: string;
|
|
22
|
+
sessionId: string;
|
|
23
|
+
timestamp: number;
|
|
24
|
+
label: string;
|
|
25
|
+
description?: string;
|
|
26
|
+
messages: Message[];
|
|
27
|
+
files: FileSnapshot[];
|
|
28
|
+
gitState?: GitState;
|
|
29
|
+
metadata: {
|
|
30
|
+
model?: string;
|
|
31
|
+
workingDirectory?: string;
|
|
32
|
+
totalCost: number;
|
|
33
|
+
messageCount: number;
|
|
34
|
+
fileCount: number;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export interface CheckpointStore {
|
|
38
|
+
checkpoints: Map<string, Checkpoint>;
|
|
39
|
+
sessionFile: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Load checkpoints for a session
|
|
43
|
+
*/
|
|
44
|
+
export declare function loadCheckpoints(sessionId: string): Promise<Map<string, Checkpoint>>;
|
|
45
|
+
/**
|
|
46
|
+
* Save checkpoints for a session
|
|
47
|
+
*/
|
|
48
|
+
export declare function saveCheckpoints(sessionId: string, checkpoints: Map<string, Checkpoint>): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Create a new checkpoint with file snapshots
|
|
51
|
+
*/
|
|
52
|
+
export declare function createCheckpoint(sessionId: string, messages: Message[], options?: {
|
|
53
|
+
label?: string;
|
|
54
|
+
description?: string;
|
|
55
|
+
model?: string;
|
|
56
|
+
workingDirectory?: string;
|
|
57
|
+
totalCost?: number;
|
|
58
|
+
trackFiles?: boolean;
|
|
59
|
+
}): Promise<Checkpoint>;
|
|
60
|
+
/**
|
|
61
|
+
* Restore a checkpoint (returns the checkpoint, doesn't apply it)
|
|
62
|
+
*/
|
|
63
|
+
export declare function restoreCheckpoint(sessionId: string, checkpointId: string): Promise<Checkpoint | null>;
|
|
64
|
+
/**
|
|
65
|
+
* Apply a checkpoint - restore files and return messages
|
|
66
|
+
*/
|
|
67
|
+
export declare function applyCheckpoint(checkpoint: Checkpoint, options?: {
|
|
68
|
+
restoreFiles?: boolean;
|
|
69
|
+
restoreMessages?: boolean;
|
|
70
|
+
workingDirectory?: string;
|
|
71
|
+
}): Promise<{
|
|
72
|
+
messages: Message[];
|
|
73
|
+
filesRestored: number;
|
|
74
|
+
filesFailed: number;
|
|
75
|
+
}>;
|
|
76
|
+
/**
|
|
77
|
+
* Delete a checkpoint
|
|
78
|
+
*/
|
|
79
|
+
export declare function deleteCheckpoint(sessionId: string, checkpointId: string): Promise<boolean>;
|
|
80
|
+
/**
|
|
81
|
+
* List all checkpoints for a session
|
|
82
|
+
*/
|
|
83
|
+
export declare function listCheckpoints(sessionId: string): Promise<Checkpoint[]>;
|
|
84
|
+
/**
|
|
85
|
+
* Format checkpoint for display
|
|
86
|
+
*/
|
|
87
|
+
export declare function formatCheckpoint(checkpoint: Checkpoint, verbose?: boolean): string;
|
|
88
|
+
/**
|
|
89
|
+
* Print checkpoints list
|
|
90
|
+
*/
|
|
91
|
+
export declare function printCheckpointsList(checkpoints: Checkpoint[]): void;
|
|
92
|
+
/**
|
|
93
|
+
* Clear all checkpoints for a session
|
|
94
|
+
*/
|
|
95
|
+
export declare function clearCheckpoints(sessionId: string): Promise<number>;
|
|
96
|
+
/**
|
|
97
|
+
* Get checkpoint summary for status display
|
|
98
|
+
*/
|
|
99
|
+
export declare function getCheckpointSummary(checkpoint: Checkpoint): string;
|
|
100
|
+
/**
|
|
101
|
+
* Register a new checkpoint (called after createCheckpoint)
|
|
102
|
+
*/
|
|
103
|
+
export declare function registerCheckpoint(sessionId: string, checkpointId: string): Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Undo - go back to previous checkpoint
|
|
106
|
+
*/
|
|
107
|
+
export declare function undoCheckpoint(sessionId: string): Promise<{
|
|
108
|
+
checkpoint: Checkpoint | null;
|
|
109
|
+
canRedo: boolean;
|
|
110
|
+
}>;
|
|
111
|
+
/**
|
|
112
|
+
* Redo - go forward to next checkpoint
|
|
113
|
+
*/
|
|
114
|
+
export declare function redoCheckpoint(sessionId: string): Promise<{
|
|
115
|
+
checkpoint: Checkpoint | null;
|
|
116
|
+
canRedo: boolean;
|
|
117
|
+
}>;
|
|
118
|
+
/**
|
|
119
|
+
* Get navigation status
|
|
120
|
+
*/
|
|
121
|
+
export declare function getNavigationStatus(sessionId: string): Promise<{
|
|
122
|
+
total: number;
|
|
123
|
+
current: number;
|
|
124
|
+
canUndo: boolean;
|
|
125
|
+
canRedo: boolean;
|
|
126
|
+
currentId?: string;
|
|
127
|
+
}>;
|
|
128
|
+
//# sourceMappingURL=checkpoints.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkpoints.d.ts","sourceRoot":"","sources":["../../packages/src/core/checkpoints.ts"],"names":[],"mappings":"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"}
|