@xelauvas/xela-cli 0.1.0
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 +200 -0
- package/bin/xela +100 -0
- package/package.json +88 -0
- package/src/QueryEngine.ts +1295 -0
- package/src/Task.ts +125 -0
- package/src/Tool.ts +792 -0
- package/src/_shims/_generated_stubs/_universal_stub.mjs +168 -0
- package/src/_shims/_generated_stubs/text_stub.mjs +1 -0
- package/src/_shims/bun_bundle.js +9 -0
- package/src/_shims/cjs_stub.cjs +23 -0
- package/src/_shims/empty_stub.js +33 -0
- package/src/_shims/loader.js +352 -0
- package/src/_shims/openai_adapter.ts +486 -0
- package/src/_shims/react_compiler_runtime.js +17 -0
- package/src/_shims/register.js +148 -0
- package/src/assistant/sessionHistory.ts +87 -0
- package/src/bootstrap/state.ts +1759 -0
- package/src/bridge/bridgeApi.ts +539 -0
- package/src/bridge/bridgeConfig.ts +48 -0
- package/src/bridge/bridgeDebug.ts +135 -0
- package/src/bridge/bridgeEnabled.ts +202 -0
- package/src/bridge/bridgeMain.ts +2999 -0
- package/src/bridge/bridgeMessaging.ts +461 -0
- package/src/bridge/bridgePermissionCallbacks.ts +43 -0
- package/src/bridge/bridgePointer.ts +210 -0
- package/src/bridge/bridgeStatusUtil.ts +163 -0
- package/src/bridge/bridgeUI.ts +530 -0
- package/src/bridge/capacityWake.ts +56 -0
- package/src/bridge/codeSessionApi.ts +168 -0
- package/src/bridge/createSession.ts +384 -0
- package/src/bridge/debugUtils.ts +141 -0
- package/src/bridge/envLessBridgeConfig.ts +165 -0
- package/src/bridge/flushGate.ts +71 -0
- package/src/bridge/inboundAttachments.ts +175 -0
- package/src/bridge/inboundMessages.ts +80 -0
- package/src/bridge/initReplBridge.ts +569 -0
- package/src/bridge/jwtUtils.ts +256 -0
- package/src/bridge/pollConfig.ts +110 -0
- package/src/bridge/pollConfigDefaults.ts +82 -0
- package/src/bridge/remoteBridgeCore.ts +1008 -0
- package/src/bridge/replBridge.ts +2406 -0
- package/src/bridge/replBridgeHandle.ts +36 -0
- package/src/bridge/replBridgeTransport.ts +370 -0
- package/src/bridge/sessionIdCompat.ts +57 -0
- package/src/bridge/sessionRunner.ts +550 -0
- package/src/bridge/trustedDevice.ts +210 -0
- package/src/bridge/types.ts +262 -0
- package/src/bridge/workSecret.ts +127 -0
- package/src/buddy/CompanionSprite.tsx +371 -0
- package/src/buddy/companion.ts +133 -0
- package/src/buddy/prompt.ts +36 -0
- package/src/buddy/sprites.ts +514 -0
- package/src/buddy/types.ts +148 -0
- package/src/buddy/useBuddyNotification.tsx +98 -0
- package/src/cli/exit.ts +31 -0
- package/src/cli/handlers/agents.ts +70 -0
- package/src/cli/handlers/auth.ts +330 -0
- package/src/cli/handlers/autoMode.ts +170 -0
- package/src/cli/handlers/mcp.tsx +362 -0
- package/src/cli/handlers/plugins.ts +878 -0
- package/src/cli/handlers/util.tsx +110 -0
- package/src/cli/ndjsonSafeStringify.ts +32 -0
- package/src/cli/print.ts +5594 -0
- package/src/cli/remoteIO.ts +255 -0
- package/src/cli/structuredIO.ts +859 -0
- package/src/cli/transports/HybridTransport.ts +282 -0
- package/src/cli/transports/SSETransport.ts +711 -0
- package/src/cli/transports/SerialBatchEventUploader.ts +275 -0
- package/src/cli/transports/WebSocketTransport.ts +800 -0
- package/src/cli/transports/WorkerStateUploader.ts +131 -0
- package/src/cli/transports/ccrClient.ts +998 -0
- package/src/cli/transports/transportUtils.ts +45 -0
- package/src/cli/update.ts +422 -0
- package/src/commands/add-dir/add-dir.tsx +126 -0
- package/src/commands/add-dir/index.ts +11 -0
- package/src/commands/add-dir/validation.ts +110 -0
- package/src/commands/advisor.ts +109 -0
- package/src/commands/agents/agents.tsx +12 -0
- package/src/commands/agents/index.ts +10 -0
- package/src/commands/ant-trace/index.js +1 -0
- package/src/commands/autofix-pr/index.js +1 -0
- package/src/commands/backfill-sessions/index.js +1 -0
- package/src/commands/branch/branch.ts +296 -0
- package/src/commands/branch/index.ts +14 -0
- package/src/commands/break-cache/index.js +1 -0
- package/src/commands/bridge/bridge.tsx +509 -0
- package/src/commands/bridge/index.ts +26 -0
- package/src/commands/bridge-kick.ts +200 -0
- package/src/commands/brief.ts +130 -0
- package/src/commands/btw/btw.tsx +243 -0
- package/src/commands/btw/index.ts +13 -0
- package/src/commands/bughunter/index.js +1 -0
- package/src/commands/chrome/chrome.tsx +285 -0
- package/src/commands/chrome/index.ts +13 -0
- package/src/commands/clear/caches.ts +144 -0
- package/src/commands/clear/clear.ts +7 -0
- package/src/commands/clear/conversation.ts +251 -0
- package/src/commands/clear/index.ts +19 -0
- package/src/commands/color/color.ts +93 -0
- package/src/commands/color/index.ts +16 -0
- package/src/commands/commit-push-pr.ts +158 -0
- package/src/commands/commit.ts +92 -0
- package/src/commands/compact/compact.ts +287 -0
- package/src/commands/compact/index.ts +15 -0
- package/src/commands/config/config.tsx +7 -0
- package/src/commands/config/index.ts +11 -0
- package/src/commands/context/context-noninteractive.ts +325 -0
- package/src/commands/context/context.tsx +64 -0
- package/src/commands/context/index.ts +24 -0
- package/src/commands/copy/copy.tsx +371 -0
- package/src/commands/copy/index.ts +15 -0
- package/src/commands/cost/cost.ts +24 -0
- package/src/commands/cost/index.ts +23 -0
- package/src/commands/createMovedToPluginCommand.ts +65 -0
- package/src/commands/ctx_viz/index.js +1 -0
- package/src/commands/debug-tool-call/index.js +1 -0
- package/src/commands/desktop/desktop.tsx +9 -0
- package/src/commands/desktop/index.ts +26 -0
- package/src/commands/diff/diff.tsx +9 -0
- package/src/commands/diff/index.ts +8 -0
- package/src/commands/doctor/doctor.tsx +7 -0
- package/src/commands/doctor/index.ts +12 -0
- package/src/commands/effort/effort.tsx +183 -0
- package/src/commands/effort/index.ts +13 -0
- package/src/commands/env/index.js +1 -0
- package/src/commands/exit/exit.tsx +33 -0
- package/src/commands/exit/index.ts +12 -0
- package/src/commands/export/export.tsx +91 -0
- package/src/commands/export/index.ts +11 -0
- package/src/commands/extra-usage/extra-usage-core.ts +118 -0
- package/src/commands/extra-usage/extra-usage-noninteractive.ts +16 -0
- package/src/commands/extra-usage/extra-usage.tsx +17 -0
- package/src/commands/extra-usage/index.ts +31 -0
- package/src/commands/fast/fast.tsx +269 -0
- package/src/commands/fast/index.ts +26 -0
- package/src/commands/feedback/feedback.tsx +25 -0
- package/src/commands/feedback/index.ts +26 -0
- package/src/commands/files/files.ts +19 -0
- package/src/commands/files/index.ts +12 -0
- package/src/commands/good-claude/index.js +1 -0
- package/src/commands/heapdump/heapdump.ts +17 -0
- package/src/commands/heapdump/index.ts +12 -0
- package/src/commands/help/help.tsx +11 -0
- package/src/commands/help/index.ts +10 -0
- package/src/commands/hooks/hooks.tsx +13 -0
- package/src/commands/hooks/index.ts +11 -0
- package/src/commands/ide/ide.tsx +646 -0
- package/src/commands/ide/index.ts +11 -0
- package/src/commands/init-verifiers.ts +262 -0
- package/src/commands/init.ts +256 -0
- package/src/commands/insights.ts +3200 -0
- package/src/commands/install-github-app/ApiKeyStep.tsx +231 -0
- package/src/commands/install-github-app/CheckExistingSecretStep.tsx +190 -0
- package/src/commands/install-github-app/CheckGitHubStep.tsx +15 -0
- package/src/commands/install-github-app/ChooseRepoStep.tsx +211 -0
- package/src/commands/install-github-app/CreatingStep.tsx +65 -0
- package/src/commands/install-github-app/ErrorStep.tsx +85 -0
- package/src/commands/install-github-app/ExistingWorkflowStep.tsx +103 -0
- package/src/commands/install-github-app/InstallAppStep.tsx +94 -0
- package/src/commands/install-github-app/OAuthFlowStep.tsx +276 -0
- package/src/commands/install-github-app/SuccessStep.tsx +96 -0
- package/src/commands/install-github-app/WarningsStep.tsx +73 -0
- package/src/commands/install-github-app/index.ts +13 -0
- package/src/commands/install-github-app/install-github-app.tsx +587 -0
- package/src/commands/install-github-app/setupGitHubActions.ts +325 -0
- package/src/commands/install-slack-app/index.ts +12 -0
- package/src/commands/install-slack-app/install-slack-app.ts +30 -0
- package/src/commands/install.tsx +300 -0
- package/src/commands/issue/index.js +1 -0
- package/src/commands/keybindings/index.ts +13 -0
- package/src/commands/keybindings/keybindings.ts +53 -0
- package/src/commands/login/index.ts +14 -0
- package/src/commands/login/login.tsx +104 -0
- package/src/commands/logout/index.ts +10 -0
- package/src/commands/logout/logout.tsx +82 -0
- package/src/commands/mcp/addCommand.ts +280 -0
- package/src/commands/mcp/index.ts +12 -0
- package/src/commands/mcp/mcp.tsx +85 -0
- package/src/commands/mcp/xaaIdpCommand.ts +266 -0
- package/src/commands/memory/index.ts +10 -0
- package/src/commands/memory/memory.tsx +90 -0
- package/src/commands/mobile/index.ts +11 -0
- package/src/commands/mobile/mobile.tsx +274 -0
- package/src/commands/mock-limits/index.js +1 -0
- package/src/commands/model/index.ts +16 -0
- package/src/commands/model/model.tsx +297 -0
- package/src/commands/oauth-refresh/index.js +1 -0
- package/src/commands/onboarding/index.js +1 -0
- package/src/commands/output-style/index.ts +11 -0
- package/src/commands/output-style/output-style.tsx +7 -0
- package/src/commands/passes/index.ts +22 -0
- package/src/commands/passes/passes.tsx +24 -0
- package/src/commands/perf-issue/index.js +1 -0
- package/src/commands/permissions/index.ts +11 -0
- package/src/commands/permissions/permissions.tsx +10 -0
- package/src/commands/plan/index.ts +11 -0
- package/src/commands/plan/plan.tsx +122 -0
- package/src/commands/plugin/AddMarketplace.tsx +162 -0
- package/src/commands/plugin/BrowseMarketplace.tsx +802 -0
- package/src/commands/plugin/DiscoverPlugins.tsx +781 -0
- package/src/commands/plugin/ManageMarketplaces.tsx +838 -0
- package/src/commands/plugin/ManagePlugins.tsx +2215 -0
- package/src/commands/plugin/PluginErrors.tsx +124 -0
- package/src/commands/plugin/PluginOptionsDialog.tsx +357 -0
- package/src/commands/plugin/PluginOptionsFlow.tsx +135 -0
- package/src/commands/plugin/PluginSettings.tsx +1072 -0
- package/src/commands/plugin/PluginTrustWarning.tsx +32 -0
- package/src/commands/plugin/UnifiedInstalledCell.tsx +565 -0
- package/src/commands/plugin/ValidatePlugin.tsx +98 -0
- package/src/commands/plugin/index.tsx +11 -0
- package/src/commands/plugin/parseArgs.ts +103 -0
- package/src/commands/plugin/plugin.tsx +7 -0
- package/src/commands/plugin/pluginDetailsHelpers.tsx +117 -0
- package/src/commands/plugin/usePagination.ts +171 -0
- package/src/commands/pr_comments/index.ts +50 -0
- package/src/commands/privacy-settings/index.ts +14 -0
- package/src/commands/privacy-settings/privacy-settings.tsx +58 -0
- package/src/commands/rate-limit-options/index.ts +19 -0
- package/src/commands/rate-limit-options/rate-limit-options.tsx +210 -0
- package/src/commands/release-notes/index.ts +11 -0
- package/src/commands/release-notes/release-notes.ts +50 -0
- package/src/commands/reload-plugins/index.ts +18 -0
- package/src/commands/reload-plugins/reload-plugins.ts +61 -0
- package/src/commands/remote-env/index.ts +15 -0
- package/src/commands/remote-env/remote-env.tsx +7 -0
- package/src/commands/remote-setup/api.ts +182 -0
- package/src/commands/remote-setup/index.ts +20 -0
- package/src/commands/remote-setup/remote-setup.tsx +187 -0
- package/src/commands/rename/generateSessionName.ts +67 -0
- package/src/commands/rename/index.ts +12 -0
- package/src/commands/rename/rename.ts +87 -0
- package/src/commands/reset-limits/index.js +4 -0
- package/src/commands/resume/index.ts +12 -0
- package/src/commands/resume/resume.tsx +275 -0
- package/src/commands/review/UltrareviewOverageDialog.tsx +96 -0
- package/src/commands/review/reviewRemote.ts +316 -0
- package/src/commands/review/ultrareviewCommand.tsx +58 -0
- package/src/commands/review/ultrareviewEnabled.ts +14 -0
- package/src/commands/review.ts +57 -0
- package/src/commands/rewind/index.ts +13 -0
- package/src/commands/rewind/rewind.ts +13 -0
- package/src/commands/sandbox-toggle/index.ts +50 -0
- package/src/commands/sandbox-toggle/sandbox-toggle.tsx +83 -0
- package/src/commands/security-review.ts +243 -0
- package/src/commands/session/index.ts +16 -0
- package/src/commands/session/session.tsx +140 -0
- package/src/commands/share/index.js +1 -0
- package/src/commands/skills/index.ts +10 -0
- package/src/commands/skills/skills.tsx +8 -0
- package/src/commands/stats/index.ts +10 -0
- package/src/commands/stats/stats.tsx +7 -0
- package/src/commands/status/index.ts +12 -0
- package/src/commands/status/status.tsx +8 -0
- package/src/commands/statusline.tsx +24 -0
- package/src/commands/stickers/index.ts +11 -0
- package/src/commands/stickers/stickers.ts +16 -0
- package/src/commands/summary/index.js +1 -0
- package/src/commands/tag/index.ts +12 -0
- package/src/commands/tag/tag.tsx +215 -0
- package/src/commands/tasks/index.ts +11 -0
- package/src/commands/tasks/tasks.tsx +8 -0
- package/src/commands/teleport/index.js +1 -0
- package/src/commands/terminalSetup/index.ts +23 -0
- package/src/commands/terminalSetup/terminalSetup.tsx +531 -0
- package/src/commands/theme/index.ts +10 -0
- package/src/commands/theme/theme.tsx +57 -0
- package/src/commands/thinkback/index.ts +13 -0
- package/src/commands/thinkback/thinkback.tsx +554 -0
- package/src/commands/thinkback-play/index.ts +17 -0
- package/src/commands/thinkback-play/thinkback-play.ts +43 -0
- package/src/commands/ultraplan.tsx +471 -0
- package/src/commands/upgrade/index.ts +16 -0
- package/src/commands/upgrade/upgrade.tsx +38 -0
- package/src/commands/usage/index.ts +9 -0
- package/src/commands/usage/usage.tsx +7 -0
- package/src/commands/version.ts +22 -0
- package/src/commands/vim/index.ts +11 -0
- package/src/commands/vim/vim.ts +38 -0
- package/src/commands/voice/index.ts +20 -0
- package/src/commands/voice/voice.ts +150 -0
- package/src/commands.ts +754 -0
- package/src/components/AgentProgressLine.tsx +136 -0
- package/src/components/App.tsx +56 -0
- package/src/components/ApproveApiKey.tsx +123 -0
- package/src/components/AutoModeOptInDialog.tsx +142 -0
- package/src/components/AutoUpdater.tsx +198 -0
- package/src/components/AutoUpdaterWrapper.tsx +91 -0
- package/src/components/AwsAuthStatusBox.tsx +82 -0
- package/src/components/BaseTextInput.tsx +136 -0
- package/src/components/BashModeProgress.tsx +56 -0
- package/src/components/BridgeDialog.tsx +401 -0
- package/src/components/BypassPermissionsModeDialog.tsx +87 -0
- package/src/components/ChannelDowngradeDialog.tsx +102 -0
- package/src/components/ClaudeCodeHint/PluginHintMenu.tsx +78 -0
- package/src/components/ClaudeInChromeOnboarding.tsx +121 -0
- package/src/components/ClaudeMdExternalIncludesDialog.tsx +137 -0
- package/src/components/ClickableImageRef.tsx +73 -0
- package/src/components/CompactSummary.tsx +118 -0
- package/src/components/ConfigurableShortcutHint.tsx +57 -0
- package/src/components/ConsoleOAuthFlow.tsx +631 -0
- package/src/components/ContextSuggestions.tsx +47 -0
- package/src/components/ContextVisualization.tsx +489 -0
- package/src/components/CoordinatorAgentStatus.tsx +273 -0
- package/src/components/CostThresholdDialog.tsx +50 -0
- package/src/components/CtrlOToExpand.tsx +51 -0
- package/src/components/CustomSelect/SelectMulti.tsx +213 -0
- package/src/components/CustomSelect/index.ts +3 -0
- package/src/components/CustomSelect/option-map.ts +50 -0
- package/src/components/CustomSelect/select-input-option.tsx +488 -0
- package/src/components/CustomSelect/select-option.tsx +68 -0
- package/src/components/CustomSelect/select.tsx +690 -0
- package/src/components/CustomSelect/use-multi-select-state.ts +414 -0
- package/src/components/CustomSelect/use-select-input.ts +287 -0
- package/src/components/CustomSelect/use-select-navigation.ts +653 -0
- package/src/components/CustomSelect/use-select-state.ts +157 -0
- package/src/components/DesktopHandoff.tsx +193 -0
- package/src/components/DesktopUpsell/DesktopUpsellStartup.tsx +171 -0
- package/src/components/DevBar.tsx +49 -0
- package/src/components/DevChannelsDialog.tsx +105 -0
- package/src/components/DiagnosticsDisplay.tsx +95 -0
- package/src/components/EffortCallout.tsx +265 -0
- package/src/components/EffortIndicator.ts +42 -0
- package/src/components/ExitFlow.tsx +48 -0
- package/src/components/ExportDialog.tsx +128 -0
- package/src/components/FallbackToolUseErrorMessage.tsx +116 -0
- package/src/components/FallbackToolUseRejectedMessage.tsx +16 -0
- package/src/components/FastIcon.tsx +46 -0
- package/src/components/Feedback.tsx +592 -0
- package/src/components/FeedbackSurvey/FeedbackSurvey.tsx +174 -0
- package/src/components/FeedbackSurvey/FeedbackSurveyView.tsx +108 -0
- package/src/components/FeedbackSurvey/TranscriptSharePrompt.tsx +88 -0
- package/src/components/FeedbackSurvey/submitTranscriptShare.ts +112 -0
- package/src/components/FeedbackSurvey/useDebouncedDigitInput.ts +82 -0
- package/src/components/FeedbackSurvey/useFeedbackSurvey.tsx +296 -0
- package/src/components/FeedbackSurvey/useMemorySurvey.tsx +213 -0
- package/src/components/FeedbackSurvey/usePostCompactSurvey.tsx +206 -0
- package/src/components/FeedbackSurvey/useSurveyState.tsx +100 -0
- package/src/components/FileEditToolDiff.tsx +181 -0
- package/src/components/FileEditToolUpdatedMessage.tsx +124 -0
- package/src/components/FileEditToolUseRejectedMessage.tsx +170 -0
- package/src/components/FilePathLink.tsx +43 -0
- package/src/components/FullscreenLayout.tsx +637 -0
- package/src/components/GlobalSearchDialog.tsx +343 -0
- package/src/components/HelpV2/Commands.tsx +82 -0
- package/src/components/HelpV2/General.tsx +23 -0
- package/src/components/HelpV2/HelpV2.tsx +184 -0
- package/src/components/HighlightedCode/Fallback.tsx +193 -0
- package/src/components/HighlightedCode.tsx +190 -0
- package/src/components/HistorySearchDialog.tsx +118 -0
- package/src/components/IdeAutoConnectDialog.tsx +154 -0
- package/src/components/IdeOnboardingDialog.tsx +167 -0
- package/src/components/IdeStatusIndicator.tsx +58 -0
- package/src/components/IdleReturnDialog.tsx +118 -0
- package/src/components/InterruptedByUser.tsx +15 -0
- package/src/components/InvalidConfigDialog.tsx +156 -0
- package/src/components/InvalidSettingsDialog.tsx +89 -0
- package/src/components/KeybindingWarnings.tsx +55 -0
- package/src/components/LanguagePicker.tsx +86 -0
- package/src/components/LogSelector.tsx +1575 -0
- package/src/components/LogoV2/AnimatedAsterisk.tsx +50 -0
- package/src/components/LogoV2/AnimatedClawd.tsx +124 -0
- package/src/components/LogoV2/ChannelsNotice.tsx +266 -0
- package/src/components/LogoV2/Clawd.tsx +240 -0
- package/src/components/LogoV2/CondensedLogo.tsx +161 -0
- package/src/components/LogoV2/EmergencyTip.tsx +58 -0
- package/src/components/LogoV2/Feed.tsx +112 -0
- package/src/components/LogoV2/FeedColumn.tsx +59 -0
- package/src/components/LogoV2/GuestPassesUpsell.tsx +70 -0
- package/src/components/LogoV2/LogoV2.tsx +543 -0
- package/src/components/LogoV2/Opus1mMergeNotice.tsx +55 -0
- package/src/components/LogoV2/OverageCreditUpsell.tsx +166 -0
- package/src/components/LogoV2/VoiceModeNotice.tsx +68 -0
- package/src/components/LogoV2/WelcomeV2.tsx +433 -0
- package/src/components/LogoV2/feedConfigs.tsx +92 -0
- package/src/components/LspRecommendation/LspRecommendationMenu.tsx +88 -0
- package/src/components/MCPServerApprovalDialog.tsx +115 -0
- package/src/components/MCPServerDesktopImportDialog.tsx +203 -0
- package/src/components/MCPServerDialogCopy.tsx +15 -0
- package/src/components/MCPServerMultiselectDialog.tsx +133 -0
- package/src/components/ManagedSettingsSecurityDialog/ManagedSettingsSecurityDialog.tsx +149 -0
- package/src/components/ManagedSettingsSecurityDialog/utils.ts +144 -0
- package/src/components/Markdown.tsx +236 -0
- package/src/components/MarkdownTable.tsx +322 -0
- package/src/components/MemoryUsageIndicator.tsx +37 -0
- package/src/components/Message.tsx +627 -0
- package/src/components/MessageModel.tsx +43 -0
- package/src/components/MessageResponse.tsx +78 -0
- package/src/components/MessageRow.tsx +383 -0
- package/src/components/MessageSelector.tsx +831 -0
- package/src/components/MessageTimestamp.tsx +63 -0
- package/src/components/Messages.tsx +834 -0
- package/src/components/ModelPicker.tsx +448 -0
- package/src/components/NativeAutoUpdater.tsx +193 -0
- package/src/components/NotebookEditToolUseRejectedMessage.tsx +92 -0
- package/src/components/OffscreenFreeze.tsx +44 -0
- package/src/components/Onboarding.tsx +244 -0
- package/src/components/OutputStylePicker.tsx +112 -0
- package/src/components/PackageManagerAutoUpdater.tsx +104 -0
- package/src/components/Passes/Passes.tsx +184 -0
- package/src/components/PrBadge.tsx +97 -0
- package/src/components/PressEnterToContinue.tsx +15 -0
- package/src/components/PromptInput/HistorySearchInput.tsx +51 -0
- package/src/components/PromptInput/IssueFlagBanner.tsx +12 -0
- package/src/components/PromptInput/Notifications.tsx +332 -0
- package/src/components/PromptInput/PromptInput.tsx +2339 -0
- package/src/components/PromptInput/PromptInputFooter.tsx +191 -0
- package/src/components/PromptInput/PromptInputFooterLeftSide.tsx +517 -0
- package/src/components/PromptInput/PromptInputFooterSuggestions.tsx +293 -0
- package/src/components/PromptInput/PromptInputHelpMenu.tsx +358 -0
- package/src/components/PromptInput/PromptInputModeIndicator.tsx +93 -0
- package/src/components/PromptInput/PromptInputQueuedCommands.tsx +117 -0
- package/src/components/PromptInput/PromptInputStashNotice.tsx +25 -0
- package/src/components/PromptInput/SandboxPromptFooterHint.tsx +64 -0
- package/src/components/PromptInput/ShimmeredInput.tsx +143 -0
- package/src/components/PromptInput/VoiceIndicator.tsx +137 -0
- package/src/components/PromptInput/inputModes.ts +33 -0
- package/src/components/PromptInput/inputPaste.ts +90 -0
- package/src/components/PromptInput/useMaybeTruncateInput.ts +58 -0
- package/src/components/PromptInput/usePromptInputPlaceholder.ts +76 -0
- package/src/components/PromptInput/useShowFastIconHint.ts +31 -0
- package/src/components/PromptInput/useSwarmBanner.ts +155 -0
- package/src/components/PromptInput/utils.ts +60 -0
- package/src/components/QuickOpenDialog.tsx +244 -0
- package/src/components/RemoteCallout.tsx +76 -0
- package/src/components/RemoteEnvironmentDialog.tsx +340 -0
- package/src/components/ResumeTask.tsx +268 -0
- package/src/components/SandboxViolationExpandedView.tsx +99 -0
- package/src/components/ScrollKeybindingHandler.tsx +1012 -0
- package/src/components/SearchBox.tsx +72 -0
- package/src/components/SentryErrorBoundary.ts +28 -0
- package/src/components/SessionBackgroundHint.tsx +108 -0
- package/src/components/SessionPreview.tsx +194 -0
- package/src/components/Settings/Config.tsx +1822 -0
- package/src/components/Settings/Settings.tsx +137 -0
- package/src/components/Settings/Status.tsx +241 -0
- package/src/components/Settings/Usage.tsx +377 -0
- package/src/components/ShowInIDEPrompt.tsx +170 -0
- package/src/components/SkillImprovementSurvey.tsx +152 -0
- package/src/components/Spinner/FlashingChar.tsx +61 -0
- package/src/components/Spinner/GlimmerMessage.tsx +328 -0
- package/src/components/Spinner/ShimmerChar.tsx +36 -0
- package/src/components/Spinner/SpinnerAnimationRow.tsx +265 -0
- package/src/components/Spinner/SpinnerGlyph.tsx +80 -0
- package/src/components/Spinner/TeammateSpinnerLine.tsx +233 -0
- package/src/components/Spinner/TeammateSpinnerTree.tsx +272 -0
- package/src/components/Spinner/index.ts +10 -0
- package/src/components/Spinner/teammateSelectHint.ts +1 -0
- package/src/components/Spinner/useShimmerAnimation.ts +31 -0
- package/src/components/Spinner/useStalledAnimation.ts +75 -0
- package/src/components/Spinner/utils.ts +84 -0
- package/src/components/Spinner.tsx +562 -0
- package/src/components/Stats.tsx +1228 -0
- package/src/components/StatusLine.tsx +324 -0
- package/src/components/StatusNotices.tsx +55 -0
- package/src/components/StructuredDiff/Fallback.tsx +487 -0
- package/src/components/StructuredDiff/colorDiff.ts +37 -0
- package/src/components/StructuredDiff.tsx +190 -0
- package/src/components/StructuredDiffList.tsx +30 -0
- package/src/components/TagTabs.tsx +139 -0
- package/src/components/TaskListV2.tsx +378 -0
- package/src/components/TeammateViewHeader.tsx +82 -0
- package/src/components/TeleportError.tsx +189 -0
- package/src/components/TeleportProgress.tsx +140 -0
- package/src/components/TeleportRepoMismatchDialog.tsx +104 -0
- package/src/components/TeleportResumeWrapper.tsx +167 -0
- package/src/components/TeleportStash.tsx +116 -0
- package/src/components/TextInput.tsx +124 -0
- package/src/components/ThemePicker.tsx +333 -0
- package/src/components/ThinkingToggle.tsx +153 -0
- package/src/components/TokenWarning.tsx +179 -0
- package/src/components/ToolUseLoader.tsx +42 -0
- package/src/components/TrustDialog/TrustDialog.tsx +290 -0
- package/src/components/TrustDialog/utils.ts +245 -0
- package/src/components/ValidationErrorsList.tsx +148 -0
- package/src/components/VimTextInput.tsx +140 -0
- package/src/components/VirtualMessageList.tsx +1082 -0
- package/src/components/WorkflowMultiselectDialog.tsx +128 -0
- package/src/components/WorktreeExitDialog.tsx +231 -0
- package/src/components/agents/AgentDetail.tsx +220 -0
- package/src/components/agents/AgentEditor.tsx +178 -0
- package/src/components/agents/AgentNavigationFooter.tsx +26 -0
- package/src/components/agents/AgentsList.tsx +440 -0
- package/src/components/agents/AgentsMenu.tsx +800 -0
- package/src/components/agents/ColorPicker.tsx +112 -0
- package/src/components/agents/ModelSelector.tsx +68 -0
- package/src/components/agents/ToolSelector.tsx +562 -0
- package/src/components/agents/agentFileUtils.ts +272 -0
- package/src/components/agents/generateAgent.ts +197 -0
- package/src/components/agents/new-agent-creation/CreateAgentWizard.tsx +97 -0
- package/src/components/agents/new-agent-creation/wizard-steps/ColorStep.tsx +84 -0
- package/src/components/agents/new-agent-creation/wizard-steps/ConfirmStep.tsx +378 -0
- package/src/components/agents/new-agent-creation/wizard-steps/ConfirmStepWrapper.tsx +74 -0
- package/src/components/agents/new-agent-creation/wizard-steps/DescriptionStep.tsx +123 -0
- package/src/components/agents/new-agent-creation/wizard-steps/GenerateStep.tsx +143 -0
- package/src/components/agents/new-agent-creation/wizard-steps/LocationStep.tsx +80 -0
- package/src/components/agents/new-agent-creation/wizard-steps/MemoryStep.tsx +113 -0
- package/src/components/agents/new-agent-creation/wizard-steps/MethodStep.tsx +80 -0
- package/src/components/agents/new-agent-creation/wizard-steps/ModelStep.tsx +52 -0
- package/src/components/agents/new-agent-creation/wizard-steps/PromptStep.tsx +128 -0
- package/src/components/agents/new-agent-creation/wizard-steps/ToolsStep.tsx +61 -0
- package/src/components/agents/new-agent-creation/wizard-steps/TypeStep.tsx +103 -0
- package/src/components/agents/types.ts +27 -0
- package/src/components/agents/utils.ts +18 -0
- package/src/components/agents/validateAgent.ts +109 -0
- package/src/components/design-system/Byline.tsx +77 -0
- package/src/components/design-system/Dialog.tsx +138 -0
- package/src/components/design-system/Divider.tsx +149 -0
- package/src/components/design-system/FuzzyPicker.tsx +312 -0
- package/src/components/design-system/KeyboardShortcutHint.tsx +81 -0
- package/src/components/design-system/ListItem.tsx +244 -0
- package/src/components/design-system/LoadingState.tsx +94 -0
- package/src/components/design-system/Pane.tsx +77 -0
- package/src/components/design-system/ProgressBar.tsx +86 -0
- package/src/components/design-system/Ratchet.tsx +80 -0
- package/src/components/design-system/StatusIcon.tsx +95 -0
- package/src/components/design-system/Tabs.tsx +340 -0
- package/src/components/design-system/ThemeProvider.tsx +174 -0
- package/src/components/design-system/ThemedBox.tsx +156 -0
- package/src/components/design-system/ThemedText.tsx +124 -0
- package/src/components/design-system/color.ts +30 -0
- package/src/components/diff/DiffDetailView.tsx +281 -0
- package/src/components/diff/DiffDialog.tsx +383 -0
- package/src/components/diff/DiffFileList.tsx +292 -0
- package/src/components/grove/Grove.tsx +463 -0
- package/src/components/hooks/HooksConfigMenu.tsx +578 -0
- package/src/components/hooks/PromptDialog.tsx +90 -0
- package/src/components/hooks/SelectEventMode.tsx +127 -0
- package/src/components/hooks/SelectHookMode.tsx +112 -0
- package/src/components/hooks/SelectMatcherMode.tsx +144 -0
- package/src/components/hooks/ViewHookMode.tsx +199 -0
- package/src/components/mcp/CapabilitiesSection.tsx +61 -0
- package/src/components/mcp/ElicitationDialog.tsx +1169 -0
- package/src/components/mcp/MCPAgentServerMenu.tsx +183 -0
- package/src/components/mcp/MCPListPanel.tsx +504 -0
- package/src/components/mcp/MCPReconnect.tsx +167 -0
- package/src/components/mcp/MCPRemoteServerMenu.tsx +649 -0
- package/src/components/mcp/MCPSettings.tsx +398 -0
- package/src/components/mcp/MCPStdioServerMenu.tsx +177 -0
- package/src/components/mcp/MCPToolDetailView.tsx +212 -0
- package/src/components/mcp/MCPToolListView.tsx +141 -0
- package/src/components/mcp/McpParsingWarnings.tsx +213 -0
- package/src/components/mcp/index.ts +9 -0
- package/src/components/mcp/utils/reconnectHelpers.tsx +49 -0
- package/src/components/memory/MemoryFileSelector.tsx +438 -0
- package/src/components/memory/MemoryUpdateNotification.tsx +45 -0
- package/src/components/messageActions.tsx +450 -0
- package/src/components/messages/AdvisorMessage.tsx +158 -0
- package/src/components/messages/AssistantRedactedThinkingMessage.tsx +31 -0
- package/src/components/messages/AssistantTextMessage.tsx +270 -0
- package/src/components/messages/AssistantThinkingMessage.tsx +86 -0
- package/src/components/messages/AssistantToolUseMessage.tsx +368 -0
- package/src/components/messages/AttachmentMessage.tsx +536 -0
- package/src/components/messages/CollapsedReadSearchContent.tsx +484 -0
- package/src/components/messages/CompactBoundaryMessage.tsx +18 -0
- package/src/components/messages/GroupedToolUseContent.tsx +58 -0
- package/src/components/messages/HighlightedThinkingText.tsx +162 -0
- package/src/components/messages/HookProgressMessage.tsx +116 -0
- package/src/components/messages/PlanApprovalMessage.tsx +222 -0
- package/src/components/messages/RateLimitMessage.tsx +161 -0
- package/src/components/messages/ShutdownMessage.tsx +132 -0
- package/src/components/messages/SystemAPIErrorMessage.tsx +141 -0
- package/src/components/messages/SystemTextMessage.tsx +827 -0
- package/src/components/messages/TaskAssignmentMessage.tsx +76 -0
- package/src/components/messages/UserAgentNotificationMessage.tsx +83 -0
- package/src/components/messages/UserBashInputMessage.tsx +58 -0
- package/src/components/messages/UserBashOutputMessage.tsx +54 -0
- package/src/components/messages/UserChannelMessage.tsx +137 -0
- package/src/components/messages/UserCommandMessage.tsx +108 -0
- package/src/components/messages/UserImageMessage.tsx +59 -0
- package/src/components/messages/UserLocalCommandOutputMessage.tsx +167 -0
- package/src/components/messages/UserMemoryInputMessage.tsx +75 -0
- package/src/components/messages/UserPlanMessage.tsx +42 -0
- package/src/components/messages/UserPromptMessage.tsx +80 -0
- package/src/components/messages/UserResourceUpdateMessage.tsx +121 -0
- package/src/components/messages/UserTeammateMessage.tsx +206 -0
- package/src/components/messages/UserTextMessage.tsx +275 -0
- package/src/components/messages/UserToolResultMessage/RejectedPlanMessage.tsx +31 -0
- package/src/components/messages/UserToolResultMessage/RejectedToolUseMessage.tsx +16 -0
- package/src/components/messages/UserToolResultMessage/UserToolCanceledMessage.tsx +16 -0
- package/src/components/messages/UserToolResultMessage/UserToolErrorMessage.tsx +103 -0
- package/src/components/messages/UserToolResultMessage/UserToolRejectMessage.tsx +95 -0
- package/src/components/messages/UserToolResultMessage/UserToolResultMessage.tsx +106 -0
- package/src/components/messages/UserToolResultMessage/UserToolSuccessMessage.tsx +104 -0
- package/src/components/messages/UserToolResultMessage/utils.tsx +44 -0
- package/src/components/messages/nullRenderingAttachments.ts +70 -0
- package/src/components/messages/teamMemCollapsed.tsx +140 -0
- package/src/components/messages/teamMemSaved.ts +19 -0
- package/src/components/permissions/AskUserQuestionPermissionRequest/AskUserQuestionPermissionRequest.tsx +645 -0
- package/src/components/permissions/AskUserQuestionPermissionRequest/PreviewBox.tsx +229 -0
- package/src/components/permissions/AskUserQuestionPermissionRequest/PreviewQuestionView.tsx +328 -0
- package/src/components/permissions/AskUserQuestionPermissionRequest/QuestionNavigationBar.tsx +178 -0
- package/src/components/permissions/AskUserQuestionPermissionRequest/QuestionView.tsx +465 -0
- package/src/components/permissions/AskUserQuestionPermissionRequest/SubmitQuestionsView.tsx +144 -0
- package/src/components/permissions/AskUserQuestionPermissionRequest/use-multiple-choice-state.ts +179 -0
- package/src/components/permissions/BashPermissionRequest/BashPermissionRequest.tsx +482 -0
- package/src/components/permissions/BashPermissionRequest/bashToolUseOptions.tsx +147 -0
- package/src/components/permissions/ComputerUseApproval/ComputerUseApproval.tsx +441 -0
- package/src/components/permissions/EnterPlanModePermissionRequest/EnterPlanModePermissionRequest.tsx +122 -0
- package/src/components/permissions/ExitPlanModePermissionRequest/ExitPlanModePermissionRequest.tsx +768 -0
- package/src/components/permissions/FallbackPermissionRequest.tsx +333 -0
- package/src/components/permissions/FileEditPermissionRequest/FileEditPermissionRequest.tsx +182 -0
- package/src/components/permissions/FilePermissionDialog/FilePermissionDialog.tsx +204 -0
- package/src/components/permissions/FilePermissionDialog/ideDiffConfig.ts +42 -0
- package/src/components/permissions/FilePermissionDialog/permissionOptions.tsx +177 -0
- package/src/components/permissions/FilePermissionDialog/useFilePermissionDialog.ts +212 -0
- package/src/components/permissions/FilePermissionDialog/usePermissionHandler.ts +185 -0
- package/src/components/permissions/FileWritePermissionRequest/FileWritePermissionRequest.tsx +161 -0
- package/src/components/permissions/FileWritePermissionRequest/FileWriteToolDiff.tsx +89 -0
- package/src/components/permissions/FilesystemPermissionRequest/FilesystemPermissionRequest.tsx +115 -0
- package/src/components/permissions/NotebookEditPermissionRequest/NotebookEditPermissionRequest.tsx +166 -0
- package/src/components/permissions/NotebookEditPermissionRequest/NotebookEditToolDiff.tsx +235 -0
- package/src/components/permissions/PermissionDecisionDebugInfo.tsx +460 -0
- package/src/components/permissions/PermissionDialog.tsx +72 -0
- package/src/components/permissions/PermissionExplanation.tsx +272 -0
- package/src/components/permissions/PermissionPrompt.tsx +336 -0
- package/src/components/permissions/PermissionRequest.tsx +217 -0
- package/src/components/permissions/PermissionRequestTitle.tsx +66 -0
- package/src/components/permissions/PermissionRuleExplanation.tsx +121 -0
- package/src/components/permissions/PowerShellPermissionRequest/PowerShellPermissionRequest.tsx +235 -0
- package/src/components/permissions/PowerShellPermissionRequest/powershellToolUseOptions.tsx +91 -0
- package/src/components/permissions/SandboxPermissionRequest.tsx +163 -0
- package/src/components/permissions/SedEditPermissionRequest/SedEditPermissionRequest.tsx +230 -0
- package/src/components/permissions/SkillPermissionRequest/SkillPermissionRequest.tsx +369 -0
- package/src/components/permissions/WebFetchPermissionRequest/WebFetchPermissionRequest.tsx +258 -0
- package/src/components/permissions/WorkerBadge.tsx +49 -0
- package/src/components/permissions/WorkerPendingPermission.tsx +105 -0
- package/src/components/permissions/hooks.ts +209 -0
- package/src/components/permissions/rules/AddPermissionRules.tsx +180 -0
- package/src/components/permissions/rules/AddWorkspaceDirectory.tsx +340 -0
- package/src/components/permissions/rules/PermissionRuleDescription.tsx +76 -0
- package/src/components/permissions/rules/PermissionRuleInput.tsx +138 -0
- package/src/components/permissions/rules/PermissionRuleList.tsx +1179 -0
- package/src/components/permissions/rules/RecentDenialsTab.tsx +207 -0
- package/src/components/permissions/rules/RemoveWorkspaceDirectory.tsx +110 -0
- package/src/components/permissions/rules/WorkspaceTab.tsx +150 -0
- package/src/components/permissions/shellPermissionHelpers.tsx +164 -0
- package/src/components/permissions/useShellPermissionFeedback.ts +148 -0
- package/src/components/permissions/utils.ts +25 -0
- package/src/components/sandbox/SandboxConfigTab.tsx +45 -0
- package/src/components/sandbox/SandboxDependenciesTab.tsx +120 -0
- package/src/components/sandbox/SandboxDoctorSection.tsx +46 -0
- package/src/components/sandbox/SandboxOverridesTab.tsx +193 -0
- package/src/components/sandbox/SandboxSettings.tsx +296 -0
- package/src/components/shell/ExpandShellOutputContext.tsx +36 -0
- package/src/components/shell/OutputLine.tsx +118 -0
- package/src/components/shell/ShellProgressMessage.tsx +150 -0
- package/src/components/shell/ShellTimeDisplay.tsx +74 -0
- package/src/components/skills/SkillsMenu.tsx +237 -0
- package/src/components/tasks/AsyncAgentDetailDialog.tsx +229 -0
- package/src/components/tasks/BackgroundTask.tsx +345 -0
- package/src/components/tasks/BackgroundTaskStatus.tsx +429 -0
- package/src/components/tasks/BackgroundTasksDialog.tsx +652 -0
- package/src/components/tasks/DreamDetailDialog.tsx +251 -0
- package/src/components/tasks/InProcessTeammateDetailDialog.tsx +266 -0
- package/src/components/tasks/RemoteSessionDetailDialog.tsx +904 -0
- package/src/components/tasks/RemoteSessionProgress.tsx +243 -0
- package/src/components/tasks/ShellDetailDialog.tsx +404 -0
- package/src/components/tasks/ShellProgress.tsx +87 -0
- package/src/components/tasks/renderToolActivity.tsx +33 -0
- package/src/components/tasks/taskStatusUtils.tsx +107 -0
- package/src/components/teams/TeamStatus.tsx +80 -0
- package/src/components/teams/TeamsDialog.tsx +715 -0
- package/src/components/ui/OrderedList.tsx +71 -0
- package/src/components/ui/OrderedListItem.tsx +45 -0
- package/src/components/ui/TreeSelect.tsx +397 -0
- package/src/components/wizard/WizardDialogLayout.tsx +65 -0
- package/src/components/wizard/WizardNavigationFooter.tsx +24 -0
- package/src/components/wizard/WizardProvider.tsx +213 -0
- package/src/components/wizard/index.ts +9 -0
- package/src/components/wizard/useWizard.ts +13 -0
- package/src/constants/apiLimits.ts +94 -0
- package/src/constants/betas.ts +52 -0
- package/src/constants/common.ts +33 -0
- package/src/constants/cyberRiskInstruction.ts +24 -0
- package/src/constants/errorIds.ts +15 -0
- package/src/constants/figures.ts +45 -0
- package/src/constants/files.ts +156 -0
- package/src/constants/github-app.ts +144 -0
- package/src/constants/keys.ts +11 -0
- package/src/constants/messages.ts +1 -0
- package/src/constants/oauth.ts +234 -0
- package/src/constants/outputStyles.ts +216 -0
- package/src/constants/product.ts +76 -0
- package/src/constants/prompts.ts +914 -0
- package/src/constants/spinnerVerbs.ts +204 -0
- package/src/constants/system.ts +95 -0
- package/src/constants/systemPromptSections.ts +68 -0
- package/src/constants/toolLimits.ts +56 -0
- package/src/constants/tools.ts +112 -0
- package/src/constants/turnCompletionVerbs.ts +12 -0
- package/src/constants/xml.ts +86 -0
- package/src/context/QueuedMessageContext.tsx +63 -0
- package/src/context/fpsMetrics.tsx +30 -0
- package/src/context/mailbox.tsx +38 -0
- package/src/context/modalContext.tsx +58 -0
- package/src/context/notifications.tsx +240 -0
- package/src/context/overlayContext.tsx +151 -0
- package/src/context/promptOverlayContext.tsx +125 -0
- package/src/context/stats.tsx +220 -0
- package/src/context/voice.tsx +88 -0
- package/src/context.ts +189 -0
- package/src/coordinator/coordinatorMode.ts +369 -0
- package/src/cost-tracker.ts +323 -0
- package/src/costHook.ts +22 -0
- package/src/dialogLaunchers.tsx +133 -0
- package/src/entrypoints/agentSdkTypes.ts +443 -0
- package/src/entrypoints/cli.tsx +303 -0
- package/src/entrypoints/init.ts +340 -0
- package/src/entrypoints/mcp.ts +196 -0
- package/src/entrypoints/sandboxTypes.ts +156 -0
- package/src/entrypoints/sdk/controlSchemas.ts +663 -0
- package/src/entrypoints/sdk/coreSchemas.ts +1889 -0
- package/src/entrypoints/sdk/coreTypes.ts +62 -0
- package/src/history.ts +464 -0
- package/src/hooks/fileSuggestions.ts +811 -0
- package/src/hooks/notifs/useAutoModeUnavailableNotification.ts +56 -0
- package/src/hooks/notifs/useCanSwitchToExistingSubscription.tsx +60 -0
- package/src/hooks/notifs/useDeprecationWarningNotification.tsx +44 -0
- package/src/hooks/notifs/useFastModeNotification.tsx +162 -0
- package/src/hooks/notifs/useIDEStatusIndicator.tsx +186 -0
- package/src/hooks/notifs/useInstallMessages.tsx +26 -0
- package/src/hooks/notifs/useLspInitializationNotification.tsx +143 -0
- package/src/hooks/notifs/useMcpConnectivityStatus.tsx +88 -0
- package/src/hooks/notifs/useModelMigrationNotifications.tsx +52 -0
- package/src/hooks/notifs/useNpmDeprecationNotification.tsx +25 -0
- package/src/hooks/notifs/usePluginAutoupdateNotification.tsx +83 -0
- package/src/hooks/notifs/usePluginInstallationStatus.tsx +128 -0
- package/src/hooks/notifs/useRateLimitWarningNotification.tsx +114 -0
- package/src/hooks/notifs/useSettingsErrors.tsx +69 -0
- package/src/hooks/notifs/useStartupNotification.ts +41 -0
- package/src/hooks/notifs/useTeammateShutdownNotification.ts +78 -0
- package/src/hooks/renderPlaceholder.ts +51 -0
- package/src/hooks/toolPermission/PermissionContext.ts +388 -0
- package/src/hooks/toolPermission/handlers/coordinatorHandler.ts +65 -0
- package/src/hooks/toolPermission/handlers/interactiveHandler.ts +536 -0
- package/src/hooks/toolPermission/handlers/swarmWorkerHandler.ts +159 -0
- package/src/hooks/toolPermission/permissionLogging.ts +238 -0
- package/src/hooks/unifiedSuggestions.ts +202 -0
- package/src/hooks/useAfterFirstRender.ts +17 -0
- package/src/hooks/useApiKeyVerification.ts +84 -0
- package/src/hooks/useArrowKeyHistory.tsx +229 -0
- package/src/hooks/useAssistantHistory.ts +250 -0
- package/src/hooks/useAwaySummary.ts +125 -0
- package/src/hooks/useBackgroundTaskNavigation.ts +251 -0
- package/src/hooks/useBlink.ts +34 -0
- package/src/hooks/useCanUseTool.tsx +204 -0
- package/src/hooks/useCancelRequest.ts +276 -0
- package/src/hooks/useChromeExtensionNotification.tsx +50 -0
- package/src/hooks/useClaudeCodeHintRecommendation.tsx +129 -0
- package/src/hooks/useClipboardImageHint.ts +77 -0
- package/src/hooks/useCommandKeybindings.tsx +108 -0
- package/src/hooks/useCommandQueue.ts +15 -0
- package/src/hooks/useCopyOnSelect.ts +98 -0
- package/src/hooks/useDeferredHookMessages.ts +46 -0
- package/src/hooks/useDiffData.ts +110 -0
- package/src/hooks/useDiffInIDE.ts +379 -0
- package/src/hooks/useDirectConnect.ts +229 -0
- package/src/hooks/useDoublePress.ts +62 -0
- package/src/hooks/useDynamicConfig.ts +22 -0
- package/src/hooks/useElapsedTime.ts +37 -0
- package/src/hooks/useExitOnCtrlCD.ts +95 -0
- package/src/hooks/useExitOnCtrlCDWithKeybindings.ts +24 -0
- package/src/hooks/useFileHistorySnapshotInit.ts +25 -0
- package/src/hooks/useGlobalKeybindings.tsx +249 -0
- package/src/hooks/useHistorySearch.ts +303 -0
- package/src/hooks/useIDEIntegration.tsx +70 -0
- package/src/hooks/useIdeAtMentioned.ts +76 -0
- package/src/hooks/useIdeConnectionStatus.ts +33 -0
- package/src/hooks/useIdeLogging.ts +41 -0
- package/src/hooks/useIdeSelection.ts +150 -0
- package/src/hooks/useInboxPoller.ts +969 -0
- package/src/hooks/useInputBuffer.ts +132 -0
- package/src/hooks/useIssueFlagBanner.ts +133 -0
- package/src/hooks/useLogMessages.ts +119 -0
- package/src/hooks/useLspPluginRecommendation.tsx +194 -0
- package/src/hooks/useMailboxBridge.ts +21 -0
- package/src/hooks/useMainLoopModel.ts +34 -0
- package/src/hooks/useManagePlugins.ts +304 -0
- package/src/hooks/useMemoryUsage.ts +39 -0
- package/src/hooks/useMergedClients.ts +23 -0
- package/src/hooks/useMergedCommands.ts +15 -0
- package/src/hooks/useMergedTools.ts +44 -0
- package/src/hooks/useMinDisplayTime.ts +35 -0
- package/src/hooks/useNotifyAfterTimeout.ts +65 -0
- package/src/hooks/useOfficialMarketplaceNotification.tsx +48 -0
- package/src/hooks/usePasteHandler.ts +285 -0
- package/src/hooks/usePluginRecommendationBase.tsx +105 -0
- package/src/hooks/usePrStatus.ts +106 -0
- package/src/hooks/usePromptSuggestion.ts +177 -0
- package/src/hooks/usePromptsFromClaudeInChrome.tsx +71 -0
- package/src/hooks/useQueueProcessor.ts +68 -0
- package/src/hooks/useRemoteSession.ts +605 -0
- package/src/hooks/useReplBridge.tsx +723 -0
- package/src/hooks/useSSHSession.ts +241 -0
- package/src/hooks/useScheduledTasks.ts +139 -0
- package/src/hooks/useSearchInput.ts +364 -0
- package/src/hooks/useSessionBackgrounding.ts +158 -0
- package/src/hooks/useSettings.ts +17 -0
- package/src/hooks/useSettingsChange.ts +25 -0
- package/src/hooks/useSkillImprovementSurvey.ts +105 -0
- package/src/hooks/useSkillsChange.ts +62 -0
- package/src/hooks/useSwarmInitialization.ts +81 -0
- package/src/hooks/useSwarmPermissionPoller.ts +330 -0
- package/src/hooks/useTaskListWatcher.ts +221 -0
- package/src/hooks/useTasksV2.ts +250 -0
- package/src/hooks/useTeammateViewAutoExit.ts +63 -0
- package/src/hooks/useTeleportResume.tsx +85 -0
- package/src/hooks/useTerminalSize.ts +15 -0
- package/src/hooks/useTextInput.ts +529 -0
- package/src/hooks/useTimeout.ts +14 -0
- package/src/hooks/useTurnDiffs.ts +213 -0
- package/src/hooks/useTypeahead.tsx +1385 -0
- package/src/hooks/useUpdateNotification.ts +34 -0
- package/src/hooks/useVimInput.ts +316 -0
- package/src/hooks/useVirtualScroll.ts +721 -0
- package/src/hooks/useVoice.ts +1144 -0
- package/src/hooks/useVoiceEnabled.ts +25 -0
- package/src/hooks/useVoiceIntegration.tsx +677 -0
- package/src/ink/Ansi.tsx +292 -0
- package/src/ink/bidi.ts +139 -0
- package/src/ink/clearTerminal.ts +74 -0
- package/src/ink/colorize.ts +231 -0
- package/src/ink/components/AlternateScreen.tsx +80 -0
- package/src/ink/components/App.tsx +659 -0
- package/src/ink/components/AppContext.ts +21 -0
- package/src/ink/components/Box.tsx +214 -0
- package/src/ink/components/Button.tsx +192 -0
- package/src/ink/components/ClockContext.tsx +112 -0
- package/src/ink/components/CursorDeclarationContext.ts +32 -0
- package/src/ink/components/ErrorOverview.tsx +109 -0
- package/src/ink/components/Link.tsx +42 -0
- package/src/ink/components/Newline.tsx +39 -0
- package/src/ink/components/NoSelect.tsx +68 -0
- package/src/ink/components/RawAnsi.tsx +57 -0
- package/src/ink/components/ScrollBox.tsx +237 -0
- package/src/ink/components/Spacer.tsx +20 -0
- package/src/ink/components/StdinContext.ts +49 -0
- package/src/ink/components/TerminalFocusContext.tsx +52 -0
- package/src/ink/components/TerminalSizeContext.tsx +7 -0
- package/src/ink/components/Text.tsx +254 -0
- package/src/ink/constants.ts +2 -0
- package/src/ink/dom.ts +484 -0
- package/src/ink/events/click-event.ts +38 -0
- package/src/ink/events/dispatcher.ts +233 -0
- package/src/ink/events/emitter.ts +39 -0
- package/src/ink/events/event-handlers.ts +73 -0
- package/src/ink/events/event.ts +11 -0
- package/src/ink/events/focus-event.ts +21 -0
- package/src/ink/events/input-event.ts +205 -0
- package/src/ink/events/keyboard-event.ts +51 -0
- package/src/ink/events/terminal-event.ts +107 -0
- package/src/ink/events/terminal-focus-event.ts +19 -0
- package/src/ink/focus.ts +181 -0
- package/src/ink/frame.ts +124 -0
- package/src/ink/get-max-width.ts +27 -0
- package/src/ink/hit-test.ts +130 -0
- package/src/ink/hooks/use-animation-frame.ts +57 -0
- package/src/ink/hooks/use-app.ts +8 -0
- package/src/ink/hooks/use-declared-cursor.ts +73 -0
- package/src/ink/hooks/use-input.ts +92 -0
- package/src/ink/hooks/use-interval.ts +67 -0
- package/src/ink/hooks/use-search-highlight.ts +53 -0
- package/src/ink/hooks/use-selection.ts +104 -0
- package/src/ink/hooks/use-stdin.ts +8 -0
- package/src/ink/hooks/use-tab-status.ts +72 -0
- package/src/ink/hooks/use-terminal-focus.ts +16 -0
- package/src/ink/hooks/use-terminal-title.ts +31 -0
- package/src/ink/hooks/use-terminal-viewport.ts +96 -0
- package/src/ink/ink.tsx +1728 -0
- package/src/ink/instances.ts +10 -0
- package/src/ink/layout/engine.ts +6 -0
- package/src/ink/layout/geometry.ts +97 -0
- package/src/ink/layout/node.ts +152 -0
- package/src/ink/layout/yoga.ts +308 -0
- package/src/ink/line-width-cache.ts +24 -0
- package/src/ink/log-update.ts +773 -0
- package/src/ink/measure-element.ts +23 -0
- package/src/ink/measure-text.ts +47 -0
- package/src/ink/node-cache.ts +54 -0
- package/src/ink/optimizer.ts +93 -0
- package/src/ink/output.ts +797 -0
- package/src/ink/parse-keypress.ts +801 -0
- package/src/ink/reconciler.ts +512 -0
- package/src/ink/render-border.ts +231 -0
- package/src/ink/render-node-to-output.ts +1462 -0
- package/src/ink/render-to-screen.ts +231 -0
- package/src/ink/renderer.ts +178 -0
- package/src/ink/root.ts +184 -0
- package/src/ink/screen.ts +1486 -0
- package/src/ink/searchHighlight.ts +93 -0
- package/src/ink/selection.ts +917 -0
- package/src/ink/squash-text-nodes.ts +92 -0
- package/src/ink/stringWidth.ts +222 -0
- package/src/ink/styles.ts +771 -0
- package/src/ink/supports-hyperlinks.ts +57 -0
- package/src/ink/tabstops.ts +46 -0
- package/src/ink/terminal-focus-state.ts +47 -0
- package/src/ink/terminal-querier.ts +212 -0
- package/src/ink/terminal.ts +248 -0
- package/src/ink/termio/ansi.ts +75 -0
- package/src/ink/termio/csi.ts +319 -0
- package/src/ink/termio/dec.ts +60 -0
- package/src/ink/termio/esc.ts +67 -0
- package/src/ink/termio/osc.ts +493 -0
- package/src/ink/termio/parser.ts +394 -0
- package/src/ink/termio/sgr.ts +308 -0
- package/src/ink/termio/tokenize.ts +319 -0
- package/src/ink/termio/types.ts +236 -0
- package/src/ink/termio.ts +42 -0
- package/src/ink/useTerminalNotification.ts +126 -0
- package/src/ink/warn.ts +9 -0
- package/src/ink/widest-line.ts +19 -0
- package/src/ink/wrap-text.ts +74 -0
- package/src/ink/wrapAnsi.ts +20 -0
- package/src/ink.ts +85 -0
- package/src/interactiveHelpers.tsx +367 -0
- package/src/keybindings/KeybindingContext.tsx +243 -0
- package/src/keybindings/KeybindingProviderSetup.tsx +308 -0
- package/src/keybindings/defaultBindings.ts +340 -0
- package/src/keybindings/loadUserBindings.ts +472 -0
- package/src/keybindings/match.ts +120 -0
- package/src/keybindings/parser.ts +203 -0
- package/src/keybindings/reservedShortcuts.ts +127 -0
- package/src/keybindings/resolver.ts +244 -0
- package/src/keybindings/schema.ts +236 -0
- package/src/keybindings/shortcutFormat.ts +63 -0
- package/src/keybindings/template.ts +52 -0
- package/src/keybindings/useKeybinding.ts +196 -0
- package/src/keybindings/useShortcutDisplay.ts +59 -0
- package/src/keybindings/validate.ts +498 -0
- package/src/main.tsx +4684 -0
- package/src/memdir/findRelevantMemories.ts +141 -0
- package/src/memdir/memdir.ts +507 -0
- package/src/memdir/memoryAge.ts +53 -0
- package/src/memdir/memoryScan.ts +94 -0
- package/src/memdir/memoryTypes.ts +271 -0
- package/src/memdir/paths.ts +278 -0
- package/src/memdir/teamMemPaths.ts +292 -0
- package/src/memdir/teamMemPrompts.ts +100 -0
- package/src/migrations/migrateAutoUpdatesToSettings.ts +61 -0
- package/src/migrations/migrateBypassPermissionsAcceptedToSettings.ts +40 -0
- package/src/migrations/migrateEnableAllProjectMcpServersToSettings.ts +118 -0
- package/src/migrations/migrateFennecToOpus.ts +45 -0
- package/src/migrations/migrateLegacyOpusToCurrent.ts +57 -0
- package/src/migrations/migrateOpusToOpus1m.ts +43 -0
- package/src/migrations/migrateReplBridgeEnabledToRemoteControlAtStartup.ts +22 -0
- package/src/migrations/migrateSonnet1mToSonnet45.ts +48 -0
- package/src/migrations/migrateSonnet45ToSonnet46.ts +67 -0
- package/src/migrations/resetAutoModeOptInForDefaultOffer.ts +51 -0
- package/src/migrations/resetProToOpusDefault.ts +51 -0
- package/src/moreright/useMoreRight.tsx +26 -0
- package/src/native-ts/color-diff/index.ts +999 -0
- package/src/native-ts/file-index/index.ts +370 -0
- package/src/native-ts/yoga-layout/enums.ts +134 -0
- package/src/native-ts/yoga-layout/index.ts +2578 -0
- package/src/outputStyles/loadOutputStylesDir.ts +98 -0
- package/src/plugins/builtinPlugins.ts +159 -0
- package/src/plugins/bundled/index.ts +23 -0
- package/src/projectOnboardingState.ts +83 -0
- package/src/query/config.ts +46 -0
- package/src/query/deps.ts +40 -0
- package/src/query/stopHooks.ts +473 -0
- package/src/query/tokenBudget.ts +93 -0
- package/src/query.ts +1729 -0
- package/src/remote/RemoteSessionManager.ts +343 -0
- package/src/remote/SessionsWebSocket.ts +404 -0
- package/src/remote/remotePermissionBridge.ts +78 -0
- package/src/remote/sdkMessageAdapter.ts +302 -0
- package/src/replLauncher.tsx +23 -0
- package/src/schemas/hooks.ts +222 -0
- package/src/screens/Doctor.tsx +575 -0
- package/src/screens/REPL.tsx +5006 -0
- package/src/screens/ResumeConversation.tsx +399 -0
- package/src/server/createDirectConnectSession.ts +88 -0
- package/src/server/directConnectManager.ts +213 -0
- package/src/server/types.ts +57 -0
- package/src/services/AgentSummary/agentSummary.ts +179 -0
- package/src/services/MagicDocs/magicDocs.ts +254 -0
- package/src/services/MagicDocs/prompts.ts +127 -0
- package/src/services/PromptSuggestion/promptSuggestion.ts +523 -0
- package/src/services/PromptSuggestion/speculation.ts +991 -0
- package/src/services/SessionMemory/prompts.ts +324 -0
- package/src/services/SessionMemory/sessionMemory.ts +495 -0
- package/src/services/SessionMemory/sessionMemoryUtils.ts +207 -0
- package/src/services/analytics/config.ts +38 -0
- package/src/services/analytics/datadog.ts +307 -0
- package/src/services/analytics/firstPartyEventLogger.ts +449 -0
- package/src/services/analytics/firstPartyEventLoggingExporter.ts +806 -0
- package/src/services/analytics/growthbook.ts +1157 -0
- package/src/services/analytics/index.ts +173 -0
- package/src/services/analytics/metadata.ts +973 -0
- package/src/services/analytics/sink.ts +114 -0
- package/src/services/analytics/sinkKillswitch.ts +25 -0
- package/src/services/api/adminRequests.ts +119 -0
- package/src/services/api/bootstrap.ts +142 -0
- package/src/services/api/claude.ts +3433 -0
- package/src/services/api/client.ts +395 -0
- package/src/services/api/dumpPrompts.ts +226 -0
- package/src/services/api/emptyUsage.ts +22 -0
- package/src/services/api/errorUtils.ts +260 -0
- package/src/services/api/errors.ts +1207 -0
- package/src/services/api/filesApi.ts +748 -0
- package/src/services/api/firstTokenDate.ts +60 -0
- package/src/services/api/grove.ts +357 -0
- package/src/services/api/logging.ts +788 -0
- package/src/services/api/metricsOptOut.ts +159 -0
- package/src/services/api/overageCreditGrant.ts +137 -0
- package/src/services/api/promptCacheBreakDetection.ts +727 -0
- package/src/services/api/referral.ts +281 -0
- package/src/services/api/sessionIngress.ts +514 -0
- package/src/services/api/ultrareviewQuota.ts +38 -0
- package/src/services/api/usage.ts +63 -0
- package/src/services/api/withRetry.ts +826 -0
- package/src/services/autoDream/autoDream.ts +324 -0
- package/src/services/autoDream/config.ts +21 -0
- package/src/services/autoDream/consolidationLock.ts +140 -0
- package/src/services/autoDream/consolidationPrompt.ts +65 -0
- package/src/services/awaySummary.ts +74 -0
- package/src/services/claudeAiLimits.ts +515 -0
- package/src/services/claudeAiLimitsHook.ts +23 -0
- package/src/services/compact/apiMicrocompact.ts +153 -0
- package/src/services/compact/autoCompact.ts +351 -0
- package/src/services/compact/compact.ts +1705 -0
- package/src/services/compact/compactWarningHook.ts +16 -0
- package/src/services/compact/compactWarningState.ts +18 -0
- package/src/services/compact/grouping.ts +63 -0
- package/src/services/compact/microCompact.ts +530 -0
- package/src/services/compact/postCompactCleanup.ts +77 -0
- package/src/services/compact/prompt.ts +374 -0
- package/src/services/compact/sessionMemoryCompact.ts +630 -0
- package/src/services/compact/timeBasedMCConfig.ts +43 -0
- package/src/services/diagnosticTracking.ts +397 -0
- package/src/services/extractMemories/extractMemories.ts +615 -0
- package/src/services/extractMemories/prompts.ts +154 -0
- package/src/services/internalLogging.ts +90 -0
- package/src/services/lsp/LSPClient.ts +447 -0
- package/src/services/lsp/LSPDiagnosticRegistry.ts +386 -0
- package/src/services/lsp/LSPServerInstance.ts +511 -0
- package/src/services/lsp/LSPServerManager.ts +420 -0
- package/src/services/lsp/config.ts +79 -0
- package/src/services/lsp/manager.ts +289 -0
- package/src/services/lsp/passiveFeedback.ts +328 -0
- package/src/services/mcp/InProcessTransport.ts +63 -0
- package/src/services/mcp/MCPConnectionManager.tsx +73 -0
- package/src/services/mcp/SdkControlTransport.ts +136 -0
- package/src/services/mcp/auth.ts +2465 -0
- package/src/services/mcp/channelAllowlist.ts +76 -0
- package/src/services/mcp/channelNotification.ts +316 -0
- package/src/services/mcp/channelPermissions.ts +240 -0
- package/src/services/mcp/claudeai.ts +164 -0
- package/src/services/mcp/client.ts +3348 -0
- package/src/services/mcp/config.ts +1578 -0
- package/src/services/mcp/elicitationHandler.ts +313 -0
- package/src/services/mcp/envExpansion.ts +38 -0
- package/src/services/mcp/headersHelper.ts +138 -0
- package/src/services/mcp/mcpStringUtils.ts +106 -0
- package/src/services/mcp/normalization.ts +23 -0
- package/src/services/mcp/oauthPort.ts +78 -0
- package/src/services/mcp/officialRegistry.ts +72 -0
- package/src/services/mcp/types.ts +258 -0
- package/src/services/mcp/useManageMCPConnections.ts +1141 -0
- package/src/services/mcp/utils.ts +575 -0
- package/src/services/mcp/vscodeSdkMcp.ts +112 -0
- package/src/services/mcp/xaa.ts +511 -0
- package/src/services/mcp/xaaIdpLogin.ts +487 -0
- package/src/services/mcpServerApproval.tsx +41 -0
- package/src/services/mockRateLimits.ts +882 -0
- package/src/services/notifier.ts +156 -0
- package/src/services/oauth/auth-code-listener.ts +211 -0
- package/src/services/oauth/client.ts +566 -0
- package/src/services/oauth/crypto.ts +23 -0
- package/src/services/oauth/getOauthProfile.ts +53 -0
- package/src/services/oauth/index.ts +198 -0
- package/src/services/plugins/PluginInstallationManager.ts +184 -0
- package/src/services/plugins/pluginCliCommands.ts +344 -0
- package/src/services/plugins/pluginOperations.ts +1088 -0
- package/src/services/policyLimits/index.ts +664 -0
- package/src/services/policyLimits/types.ts +27 -0
- package/src/services/preventSleep.ts +165 -0
- package/src/services/rateLimitMessages.ts +344 -0
- package/src/services/rateLimitMocking.ts +144 -0
- package/src/services/remoteManagedSettings/index.ts +639 -0
- package/src/services/remoteManagedSettings/securityCheck.tsx +74 -0
- package/src/services/remoteManagedSettings/syncCache.ts +112 -0
- package/src/services/remoteManagedSettings/syncCacheState.ts +96 -0
- package/src/services/remoteManagedSettings/types.ts +31 -0
- package/src/services/settingsSync/index.ts +581 -0
- package/src/services/settingsSync/types.ts +67 -0
- package/src/services/teamMemorySync/index.ts +1256 -0
- package/src/services/teamMemorySync/secretScanner.ts +324 -0
- package/src/services/teamMemorySync/teamMemSecretGuard.ts +44 -0
- package/src/services/teamMemorySync/types.ts +156 -0
- package/src/services/teamMemorySync/watcher.ts +387 -0
- package/src/services/tips/tipHistory.ts +17 -0
- package/src/services/tips/tipRegistry.ts +686 -0
- package/src/services/tips/tipScheduler.ts +58 -0
- package/src/services/tokenEstimation.ts +495 -0
- package/src/services/toolUseSummary/toolUseSummaryGenerator.ts +112 -0
- package/src/services/tools/StreamingToolExecutor.ts +530 -0
- package/src/services/tools/toolExecution.ts +1745 -0
- package/src/services/tools/toolHooks.ts +650 -0
- package/src/services/tools/toolOrchestration.ts +188 -0
- package/src/services/vcr.ts +406 -0
- package/src/services/voice.ts +525 -0
- package/src/services/voiceKeyterms.ts +106 -0
- package/src/services/voiceStreamSTT.ts +544 -0
- package/src/setup.ts +477 -0
- package/src/skills/bundled/batch.ts +124 -0
- package/src/skills/bundled/claudeApi.ts +196 -0
- package/src/skills/bundled/claudeApiContent.ts +75 -0
- package/src/skills/bundled/claudeInChrome.ts +34 -0
- package/src/skills/bundled/debug.ts +103 -0
- package/src/skills/bundled/index.ts +79 -0
- package/src/skills/bundled/keybindings.ts +339 -0
- package/src/skills/bundled/loop.ts +92 -0
- package/src/skills/bundled/loremIpsum.ts +282 -0
- package/src/skills/bundled/remember.ts +82 -0
- package/src/skills/bundled/scheduleRemoteAgents.ts +447 -0
- package/src/skills/bundled/simplify.ts +69 -0
- package/src/skills/bundled/skillify.ts +197 -0
- package/src/skills/bundled/stuck.ts +79 -0
- package/src/skills/bundled/updateConfig.ts +475 -0
- package/src/skills/bundled/verify.ts +30 -0
- package/src/skills/bundled/verifyContent.ts +13 -0
- package/src/skills/bundledSkills.ts +220 -0
- package/src/skills/loadSkillsDir.ts +1086 -0
- package/src/skills/mcpSkillBuilders.ts +44 -0
- package/src/state/AppState.tsx +200 -0
- package/src/state/AppStateStore.ts +569 -0
- package/src/state/onChangeAppState.ts +171 -0
- package/src/state/selectors.ts +76 -0
- package/src/state/store.ts +34 -0
- package/src/state/teammateViewHelpers.ts +141 -0
- package/src/tasks/DreamTask/DreamTask.ts +157 -0
- package/src/tasks/InProcessTeammateTask/InProcessTeammateTask.tsx +126 -0
- package/src/tasks/InProcessTeammateTask/types.ts +121 -0
- package/src/tasks/LocalAgentTask/LocalAgentTask.tsx +683 -0
- package/src/tasks/LocalMainSessionTask.ts +479 -0
- package/src/tasks/LocalShellTask/LocalShellTask.tsx +523 -0
- package/src/tasks/LocalShellTask/guards.ts +41 -0
- package/src/tasks/LocalShellTask/killShellTasks.ts +76 -0
- package/src/tasks/RemoteAgentTask/RemoteAgentTask.tsx +856 -0
- package/src/tasks/pillLabel.ts +82 -0
- package/src/tasks/stopTask.ts +100 -0
- package/src/tasks/types.ts +46 -0
- package/src/tasks.ts +39 -0
- package/src/tools/AgentTool/AgentTool.tsx +1398 -0
- package/src/tools/AgentTool/UI.tsx +872 -0
- package/src/tools/AgentTool/agentColorManager.ts +66 -0
- package/src/tools/AgentTool/agentDisplay.ts +104 -0
- package/src/tools/AgentTool/agentMemory.ts +177 -0
- package/src/tools/AgentTool/agentMemorySnapshot.ts +197 -0
- package/src/tools/AgentTool/agentToolUtils.ts +686 -0
- package/src/tools/AgentTool/built-in/claudeCodeGuideAgent.ts +205 -0
- package/src/tools/AgentTool/built-in/exploreAgent.ts +83 -0
- package/src/tools/AgentTool/built-in/generalPurposeAgent.ts +34 -0
- package/src/tools/AgentTool/built-in/planAgent.ts +92 -0
- package/src/tools/AgentTool/built-in/statuslineSetup.ts +144 -0
- package/src/tools/AgentTool/built-in/verificationAgent.ts +152 -0
- package/src/tools/AgentTool/builtInAgents.ts +72 -0
- package/src/tools/AgentTool/constants.ts +12 -0
- package/src/tools/AgentTool/forkSubagent.ts +210 -0
- package/src/tools/AgentTool/loadAgentsDir.ts +755 -0
- package/src/tools/AgentTool/prompt.ts +287 -0
- package/src/tools/AgentTool/resumeAgent.ts +265 -0
- package/src/tools/AgentTool/runAgent.ts +973 -0
- package/src/tools/AskUserQuestionTool/AskUserQuestionTool.tsx +266 -0
- package/src/tools/AskUserQuestionTool/prompt.ts +44 -0
- package/src/tools/BashTool/BashTool.tsx +1144 -0
- package/src/tools/BashTool/BashToolResultMessage.tsx +191 -0
- package/src/tools/BashTool/UI.tsx +185 -0
- package/src/tools/BashTool/bashCommandHelpers.ts +265 -0
- package/src/tools/BashTool/bashPermissions.ts +2621 -0
- package/src/tools/BashTool/bashSecurity.ts +2592 -0
- package/src/tools/BashTool/commandSemantics.ts +140 -0
- package/src/tools/BashTool/commentLabel.ts +13 -0
- package/src/tools/BashTool/destructiveCommandWarning.ts +102 -0
- package/src/tools/BashTool/modeValidation.ts +115 -0
- package/src/tools/BashTool/pathValidation.ts +1303 -0
- package/src/tools/BashTool/prompt.ts +369 -0
- package/src/tools/BashTool/readOnlyValidation.ts +1990 -0
- package/src/tools/BashTool/sedEditParser.ts +322 -0
- package/src/tools/BashTool/sedValidation.ts +684 -0
- package/src/tools/BashTool/shouldUseSandbox.ts +153 -0
- package/src/tools/BashTool/toolName.ts +2 -0
- package/src/tools/BashTool/utils.ts +223 -0
- package/src/tools/BriefTool/BriefTool.ts +204 -0
- package/src/tools/BriefTool/UI.tsx +101 -0
- package/src/tools/BriefTool/attachments.ts +110 -0
- package/src/tools/BriefTool/prompt.ts +22 -0
- package/src/tools/BriefTool/upload.ts +174 -0
- package/src/tools/ConfigTool/ConfigTool.ts +467 -0
- package/src/tools/ConfigTool/UI.tsx +38 -0
- package/src/tools/ConfigTool/constants.ts +1 -0
- package/src/tools/ConfigTool/prompt.ts +93 -0
- package/src/tools/ConfigTool/supportedSettings.ts +211 -0
- package/src/tools/EnterPlanModeTool/EnterPlanModeTool.ts +126 -0
- package/src/tools/EnterPlanModeTool/UI.tsx +33 -0
- package/src/tools/EnterPlanModeTool/constants.ts +1 -0
- package/src/tools/EnterPlanModeTool/prompt.ts +170 -0
- package/src/tools/EnterWorktreeTool/EnterWorktreeTool.ts +127 -0
- package/src/tools/EnterWorktreeTool/UI.tsx +20 -0
- package/src/tools/EnterWorktreeTool/constants.ts +1 -0
- package/src/tools/EnterWorktreeTool/prompt.ts +30 -0
- package/src/tools/ExitPlanModeTool/ExitPlanModeV2Tool.ts +493 -0
- package/src/tools/ExitPlanModeTool/UI.tsx +82 -0
- package/src/tools/ExitPlanModeTool/constants.ts +2 -0
- package/src/tools/ExitPlanModeTool/prompt.ts +29 -0
- package/src/tools/ExitWorktreeTool/ExitWorktreeTool.ts +329 -0
- package/src/tools/ExitWorktreeTool/UI.tsx +25 -0
- package/src/tools/ExitWorktreeTool/constants.ts +1 -0
- package/src/tools/ExitWorktreeTool/prompt.ts +32 -0
- package/src/tools/FileEditTool/FileEditTool.ts +625 -0
- package/src/tools/FileEditTool/UI.tsx +289 -0
- package/src/tools/FileEditTool/constants.ts +11 -0
- package/src/tools/FileEditTool/prompt.ts +28 -0
- package/src/tools/FileEditTool/types.ts +85 -0
- package/src/tools/FileEditTool/utils.ts +775 -0
- package/src/tools/FileReadTool/FileReadTool.ts +1183 -0
- package/src/tools/FileReadTool/UI.tsx +185 -0
- package/src/tools/FileReadTool/imageProcessor.ts +94 -0
- package/src/tools/FileReadTool/limits.ts +92 -0
- package/src/tools/FileReadTool/prompt.ts +49 -0
- package/src/tools/FileWriteTool/FileWriteTool.ts +434 -0
- package/src/tools/FileWriteTool/UI.tsx +405 -0
- package/src/tools/FileWriteTool/prompt.ts +18 -0
- package/src/tools/GlobTool/GlobTool.ts +198 -0
- package/src/tools/GlobTool/UI.tsx +63 -0
- package/src/tools/GlobTool/prompt.ts +7 -0
- package/src/tools/GrepTool/GrepTool.ts +577 -0
- package/src/tools/GrepTool/UI.tsx +201 -0
- package/src/tools/GrepTool/prompt.ts +18 -0
- package/src/tools/LSPTool/LSPTool.ts +860 -0
- package/src/tools/LSPTool/UI.tsx +228 -0
- package/src/tools/LSPTool/formatters.ts +592 -0
- package/src/tools/LSPTool/prompt.ts +21 -0
- package/src/tools/LSPTool/schemas.ts +215 -0
- package/src/tools/LSPTool/symbolContext.ts +90 -0
- package/src/tools/ListMcpResourcesTool/ListMcpResourcesTool.ts +123 -0
- package/src/tools/ListMcpResourcesTool/UI.tsx +29 -0
- package/src/tools/ListMcpResourcesTool/prompt.ts +20 -0
- package/src/tools/MCPTool/MCPTool.ts +77 -0
- package/src/tools/MCPTool/UI.tsx +403 -0
- package/src/tools/MCPTool/classifyForCollapse.ts +604 -0
- package/src/tools/MCPTool/prompt.ts +3 -0
- package/src/tools/McpAuthTool/McpAuthTool.ts +215 -0
- package/src/tools/NotebookEditTool/NotebookEditTool.ts +490 -0
- package/src/tools/NotebookEditTool/UI.tsx +93 -0
- package/src/tools/NotebookEditTool/constants.ts +2 -0
- package/src/tools/NotebookEditTool/prompt.ts +3 -0
- package/src/tools/PowerShellTool/PowerShellTool.tsx +1001 -0
- package/src/tools/PowerShellTool/UI.tsx +131 -0
- package/src/tools/PowerShellTool/clmTypes.ts +211 -0
- package/src/tools/PowerShellTool/commandSemantics.ts +142 -0
- package/src/tools/PowerShellTool/commonParameters.ts +30 -0
- package/src/tools/PowerShellTool/destructiveCommandWarning.ts +109 -0
- package/src/tools/PowerShellTool/gitSafety.ts +176 -0
- package/src/tools/PowerShellTool/modeValidation.ts +404 -0
- package/src/tools/PowerShellTool/pathValidation.ts +2049 -0
- package/src/tools/PowerShellTool/powershellPermissions.ts +1648 -0
- package/src/tools/PowerShellTool/powershellSecurity.ts +1090 -0
- package/src/tools/PowerShellTool/prompt.ts +145 -0
- package/src/tools/PowerShellTool/readOnlyValidation.ts +1823 -0
- package/src/tools/PowerShellTool/toolName.ts +2 -0
- package/src/tools/REPLTool/constants.ts +46 -0
- package/src/tools/REPLTool/primitiveTools.ts +39 -0
- package/src/tools/ReadMcpResourceTool/ReadMcpResourceTool.ts +158 -0
- package/src/tools/ReadMcpResourceTool/UI.tsx +37 -0
- package/src/tools/ReadMcpResourceTool/prompt.ts +16 -0
- package/src/tools/RemoteTriggerTool/RemoteTriggerTool.ts +161 -0
- package/src/tools/RemoteTriggerTool/UI.tsx +17 -0
- package/src/tools/RemoteTriggerTool/prompt.ts +15 -0
- package/src/tools/ScheduleCronTool/CronCreateTool.ts +157 -0
- package/src/tools/ScheduleCronTool/CronDeleteTool.ts +95 -0
- package/src/tools/ScheduleCronTool/CronListTool.ts +97 -0
- package/src/tools/ScheduleCronTool/UI.tsx +60 -0
- package/src/tools/ScheduleCronTool/prompt.ts +135 -0
- package/src/tools/SendMessageTool/SendMessageTool.ts +917 -0
- package/src/tools/SendMessageTool/UI.tsx +31 -0
- package/src/tools/SendMessageTool/constants.ts +1 -0
- package/src/tools/SendMessageTool/prompt.ts +49 -0
- package/src/tools/SkillTool/SkillTool.ts +1108 -0
- package/src/tools/SkillTool/UI.tsx +128 -0
- package/src/tools/SkillTool/constants.ts +1 -0
- package/src/tools/SkillTool/prompt.ts +241 -0
- package/src/tools/SleepTool/prompt.ts +17 -0
- package/src/tools/SyntheticOutputTool/SyntheticOutputTool.ts +163 -0
- package/src/tools/TaskCreateTool/TaskCreateTool.ts +138 -0
- package/src/tools/TaskCreateTool/constants.ts +1 -0
- package/src/tools/TaskCreateTool/prompt.ts +56 -0
- package/src/tools/TaskGetTool/TaskGetTool.ts +128 -0
- package/src/tools/TaskGetTool/constants.ts +1 -0
- package/src/tools/TaskGetTool/prompt.ts +24 -0
- package/src/tools/TaskListTool/TaskListTool.ts +116 -0
- package/src/tools/TaskListTool/constants.ts +1 -0
- package/src/tools/TaskListTool/prompt.ts +49 -0
- package/src/tools/TaskOutputTool/TaskOutputTool.tsx +584 -0
- package/src/tools/TaskOutputTool/constants.ts +1 -0
- package/src/tools/TaskStopTool/TaskStopTool.ts +131 -0
- package/src/tools/TaskStopTool/UI.tsx +41 -0
- package/src/tools/TaskStopTool/prompt.ts +8 -0
- package/src/tools/TaskUpdateTool/TaskUpdateTool.ts +406 -0
- package/src/tools/TaskUpdateTool/constants.ts +1 -0
- package/src/tools/TaskUpdateTool/prompt.ts +77 -0
- package/src/tools/TeamCreateTool/TeamCreateTool.ts +240 -0
- package/src/tools/TeamCreateTool/UI.tsx +6 -0
- package/src/tools/TeamCreateTool/constants.ts +1 -0
- package/src/tools/TeamCreateTool/prompt.ts +113 -0
- package/src/tools/TeamDeleteTool/TeamDeleteTool.ts +139 -0
- package/src/tools/TeamDeleteTool/UI.tsx +20 -0
- package/src/tools/TeamDeleteTool/constants.ts +1 -0
- package/src/tools/TeamDeleteTool/prompt.ts +16 -0
- package/src/tools/TodoWriteTool/TodoWriteTool.ts +115 -0
- package/src/tools/TodoWriteTool/constants.ts +1 -0
- package/src/tools/TodoWriteTool/prompt.ts +184 -0
- package/src/tools/ToolSearchTool/ToolSearchTool.ts +471 -0
- package/src/tools/ToolSearchTool/constants.ts +1 -0
- package/src/tools/ToolSearchTool/prompt.ts +121 -0
- package/src/tools/TungstenTool/TungstenTool.js +2 -0
- package/src/tools/TungstenTool/TungstenTool.ts +1 -0
- package/src/tools/WebFetchTool/UI.tsx +72 -0
- package/src/tools/WebFetchTool/WebFetchTool.ts +318 -0
- package/src/tools/WebFetchTool/preapproved.ts +166 -0
- package/src/tools/WebFetchTool/prompt.ts +46 -0
- package/src/tools/WebFetchTool/utils.ts +530 -0
- package/src/tools/WebSearchTool/UI.tsx +101 -0
- package/src/tools/WebSearchTool/WebSearchTool.ts +435 -0
- package/src/tools/WebSearchTool/prompt.ts +34 -0
- package/src/tools/shared/gitOperationTracking.ts +277 -0
- package/src/tools/shared/spawnMultiAgent.ts +1093 -0
- package/src/tools/testing/TestingPermissionTool.tsx +74 -0
- package/src/tools/utils.ts +40 -0
- package/src/tools.ts +389 -0
- package/src/types/command.ts +216 -0
- package/src/types/connectorText.js +5 -0
- package/src/types/connectorText.ts +1 -0
- package/src/types/generated/events_mono/claude_code/v1/claude_code_internal_event.ts +865 -0
- package/src/types/generated/events_mono/common/v1/auth.ts +100 -0
- package/src/types/generated/events_mono/growthbook/v1/growthbook_experiment_event.ts +223 -0
- package/src/types/generated/google/protobuf/timestamp.ts +187 -0
- package/src/types/hooks.ts +290 -0
- package/src/types/ids.ts +44 -0
- package/src/types/logs.ts +330 -0
- package/src/types/permissions.ts +441 -0
- package/src/types/plugin.ts +363 -0
- package/src/types/textInputTypes.ts +387 -0
- package/src/upstreamproxy/relay.ts +455 -0
- package/src/upstreamproxy/upstreamproxy.ts +285 -0
- package/src/utils/CircularBuffer.ts +84 -0
- package/src/utils/Cursor.ts +1530 -0
- package/src/utils/QueryGuard.ts +121 -0
- package/src/utils/Shell.ts +474 -0
- package/src/utils/ShellCommand.ts +465 -0
- package/src/utils/abortController.ts +99 -0
- package/src/utils/activityManager.ts +164 -0
- package/src/utils/advisor.ts +145 -0
- package/src/utils/agentContext.ts +178 -0
- package/src/utils/agentId.ts +99 -0
- package/src/utils/agentSwarmsEnabled.ts +44 -0
- package/src/utils/agenticSessionSearch.ts +307 -0
- package/src/utils/analyzeContext.ts +1382 -0
- package/src/utils/ansiToPng.ts +334 -0
- package/src/utils/ansiToSvg.ts +272 -0
- package/src/utils/api.ts +718 -0
- package/src/utils/apiPreconnect.ts +72 -0
- package/src/utils/appleTerminalBackup.ts +124 -0
- package/src/utils/argumentSubstitution.ts +145 -0
- package/src/utils/array.ts +13 -0
- package/src/utils/asciicast.ts +239 -0
- package/src/utils/attachments.ts +3997 -0
- package/src/utils/attribution.ts +393 -0
- package/src/utils/auth.ts +2007 -0
- package/src/utils/authFileDescriptor.ts +196 -0
- package/src/utils/authPortable.ts +19 -0
- package/src/utils/autoModeDenials.ts +26 -0
- package/src/utils/autoRunIssue.tsx +122 -0
- package/src/utils/autoUpdater.ts +562 -0
- package/src/utils/aws.ts +74 -0
- package/src/utils/awsAuthStatusManager.ts +81 -0
- package/src/utils/background/remote/preconditions.ts +235 -0
- package/src/utils/background/remote/remoteSession.ts +98 -0
- package/src/utils/backgroundHousekeeping.ts +94 -0
- package/src/utils/bash/ParsedCommand.ts +318 -0
- package/src/utils/bash/ShellSnapshot.ts +582 -0
- package/src/utils/bash/ast.ts +2679 -0
- package/src/utils/bash/bashParser.ts +4436 -0
- package/src/utils/bash/bashPipeCommand.ts +294 -0
- package/src/utils/bash/commands.ts +1339 -0
- package/src/utils/bash/heredoc.ts +733 -0
- package/src/utils/bash/parser.ts +230 -0
- package/src/utils/bash/prefix.ts +204 -0
- package/src/utils/bash/registry.ts +53 -0
- package/src/utils/bash/shellCompletion.ts +259 -0
- package/src/utils/bash/shellPrefix.ts +28 -0
- package/src/utils/bash/shellQuote.ts +304 -0
- package/src/utils/bash/shellQuoting.ts +128 -0
- package/src/utils/bash/specs/alias.ts +14 -0
- package/src/utils/bash/specs/index.ts +18 -0
- package/src/utils/bash/specs/nohup.ts +13 -0
- package/src/utils/bash/specs/pyright.ts +91 -0
- package/src/utils/bash/specs/sleep.ts +13 -0
- package/src/utils/bash/specs/srun.ts +31 -0
- package/src/utils/bash/specs/time.ts +13 -0
- package/src/utils/bash/specs/timeout.ts +20 -0
- package/src/utils/bash/treeSitterAnalysis.ts +506 -0
- package/src/utils/betas.ts +438 -0
- package/src/utils/billing.ts +78 -0
- package/src/utils/binaryCheck.ts +53 -0
- package/src/utils/browser.ts +68 -0
- package/src/utils/bufferedWriter.ts +100 -0
- package/src/utils/bundledMode.ts +22 -0
- package/src/utils/caCerts.ts +115 -0
- package/src/utils/caCertsConfig.ts +88 -0
- package/src/utils/cachePaths.ts +38 -0
- package/src/utils/classifierApprovals.ts +88 -0
- package/src/utils/classifierApprovalsHook.ts +17 -0
- package/src/utils/claudeCodeHints.ts +193 -0
- package/src/utils/claudeDesktop.ts +152 -0
- package/src/utils/claudeInChrome/chromeNativeHost.ts +527 -0
- package/src/utils/claudeInChrome/common.ts +540 -0
- package/src/utils/claudeInChrome/mcpServer.ts +293 -0
- package/src/utils/claudeInChrome/prompt.ts +83 -0
- package/src/utils/claudeInChrome/setup.ts +400 -0
- package/src/utils/claudeInChrome/setupPortable.ts +233 -0
- package/src/utils/claudeInChrome/toolRendering.tsx +262 -0
- package/src/utils/claudemd.ts +1479 -0
- package/src/utils/cleanup.ts +602 -0
- package/src/utils/cleanupRegistry.ts +25 -0
- package/src/utils/cliArgs.ts +60 -0
- package/src/utils/cliHighlight.ts +54 -0
- package/src/utils/codeIndexing.ts +206 -0
- package/src/utils/collapseBackgroundBashNotifications.ts +84 -0
- package/src/utils/collapseHookSummaries.ts +59 -0
- package/src/utils/collapseReadSearch.ts +1109 -0
- package/src/utils/collapseTeammateShutdowns.ts +55 -0
- package/src/utils/combinedAbortSignal.ts +47 -0
- package/src/utils/commandLifecycle.ts +21 -0
- package/src/utils/commitAttribution.ts +961 -0
- package/src/utils/completionCache.ts +166 -0
- package/src/utils/computerUse/appNames.ts +196 -0
- package/src/utils/computerUse/cleanup.ts +86 -0
- package/src/utils/computerUse/common.ts +61 -0
- package/src/utils/computerUse/computerUseLock.ts +215 -0
- package/src/utils/computerUse/drainRunLoop.ts +79 -0
- package/src/utils/computerUse/escHotkey.ts +54 -0
- package/src/utils/computerUse/executor.ts +658 -0
- package/src/utils/computerUse/gates.ts +72 -0
- package/src/utils/computerUse/hostAdapter.ts +69 -0
- package/src/utils/computerUse/inputLoader.ts +30 -0
- package/src/utils/computerUse/mcpServer.ts +106 -0
- package/src/utils/computerUse/setup.ts +53 -0
- package/src/utils/computerUse/swiftLoader.ts +23 -0
- package/src/utils/computerUse/toolRendering.tsx +125 -0
- package/src/utils/computerUse/wrapper.tsx +336 -0
- package/src/utils/concurrentSessions.ts +204 -0
- package/src/utils/config.ts +1817 -0
- package/src/utils/configConstants.ts +21 -0
- package/src/utils/contentArray.ts +51 -0
- package/src/utils/context.ts +221 -0
- package/src/utils/contextAnalysis.ts +272 -0
- package/src/utils/contextSuggestions.ts +235 -0
- package/src/utils/controlMessageCompat.ts +32 -0
- package/src/utils/conversationRecovery.ts +597 -0
- package/src/utils/cron.ts +308 -0
- package/src/utils/cronJitterConfig.ts +75 -0
- package/src/utils/cronScheduler.ts +565 -0
- package/src/utils/cronTasks.ts +458 -0
- package/src/utils/cronTasksLock.ts +195 -0
- package/src/utils/crossProjectResume.ts +75 -0
- package/src/utils/crypto.ts +13 -0
- package/src/utils/cwd.ts +32 -0
- package/src/utils/debug.ts +268 -0
- package/src/utils/debugFilter.ts +157 -0
- package/src/utils/deepLink/banner.ts +123 -0
- package/src/utils/deepLink/parseDeepLink.ts +170 -0
- package/src/utils/deepLink/protocolHandler.ts +136 -0
- package/src/utils/deepLink/registerProtocol.ts +348 -0
- package/src/utils/deepLink/terminalLauncher.ts +557 -0
- package/src/utils/deepLink/terminalPreference.ts +54 -0
- package/src/utils/desktopDeepLink.ts +236 -0
- package/src/utils/detectRepository.ts +178 -0
- package/src/utils/diagLogs.ts +94 -0
- package/src/utils/diff.ts +177 -0
- package/src/utils/directMemberMessage.ts +69 -0
- package/src/utils/displayTags.ts +51 -0
- package/src/utils/doctorContextWarnings.ts +265 -0
- package/src/utils/doctorDiagnostic.ts +625 -0
- package/src/utils/dxt/helpers.ts +88 -0
- package/src/utils/dxt/zip.ts +226 -0
- package/src/utils/earlyInput.ts +191 -0
- package/src/utils/editor.ts +183 -0
- package/src/utils/effort.ts +329 -0
- package/src/utils/embeddedTools.ts +29 -0
- package/src/utils/env.ts +347 -0
- package/src/utils/envDynamic.ts +151 -0
- package/src/utils/envUtils.ts +183 -0
- package/src/utils/envValidation.ts +38 -0
- package/src/utils/errorLogSink.ts +235 -0
- package/src/utils/errors.ts +238 -0
- package/src/utils/exampleCommands.ts +184 -0
- package/src/utils/execFileNoThrow.ts +150 -0
- package/src/utils/execFileNoThrowPortable.ts +89 -0
- package/src/utils/execSyncWrapper.ts +38 -0
- package/src/utils/exportRenderer.tsx +98 -0
- package/src/utils/extraUsage.ts +23 -0
- package/src/utils/fastMode.ts +532 -0
- package/src/utils/file.ts +584 -0
- package/src/utils/fileHistory.ts +1115 -0
- package/src/utils/fileOperationAnalytics.ts +71 -0
- package/src/utils/filePersistence/filePersistence.ts +287 -0
- package/src/utils/filePersistence/outputsScanner.ts +126 -0
- package/src/utils/fileRead.ts +102 -0
- package/src/utils/fileReadCache.ts +96 -0
- package/src/utils/fileStateCache.ts +142 -0
- package/src/utils/findExecutable.ts +17 -0
- package/src/utils/fingerprint.ts +76 -0
- package/src/utils/forkedAgent.ts +689 -0
- package/src/utils/format.ts +308 -0
- package/src/utils/formatBriefTimestamp.ts +81 -0
- package/src/utils/fpsTracker.ts +47 -0
- package/src/utils/frontmatterParser.ts +370 -0
- package/src/utils/fsOperations.ts +770 -0
- package/src/utils/fullscreen.ts +202 -0
- package/src/utils/generatedFiles.ts +136 -0
- package/src/utils/generators.ts +88 -0
- package/src/utils/genericProcessUtils.ts +184 -0
- package/src/utils/getWorktreePaths.ts +70 -0
- package/src/utils/getWorktreePathsPortable.ts +27 -0
- package/src/utils/ghPrStatus.ts +106 -0
- package/src/utils/git/gitConfigParser.ts +277 -0
- package/src/utils/git/gitFilesystem.ts +699 -0
- package/src/utils/git/gitignore.ts +99 -0
- package/src/utils/git.ts +926 -0
- package/src/utils/gitDiff.ts +532 -0
- package/src/utils/gitSettings.ts +18 -0
- package/src/utils/github/ghAuthStatus.ts +29 -0
- package/src/utils/githubRepoPathMapping.ts +162 -0
- package/src/utils/glob.ts +130 -0
- package/src/utils/gracefulShutdown.ts +529 -0
- package/src/utils/groupToolUses.ts +182 -0
- package/src/utils/handlePromptSubmit.ts +610 -0
- package/src/utils/hash.ts +46 -0
- package/src/utils/headlessProfiler.ts +178 -0
- package/src/utils/heapDumpService.ts +303 -0
- package/src/utils/heatmap.ts +198 -0
- package/src/utils/highlightMatch.tsx +28 -0
- package/src/utils/hooks/AsyncHookRegistry.ts +309 -0
- package/src/utils/hooks/apiQueryHookHelper.ts +141 -0
- package/src/utils/hooks/execAgentHook.ts +339 -0
- package/src/utils/hooks/execHttpHook.ts +242 -0
- package/src/utils/hooks/execPromptHook.ts +211 -0
- package/src/utils/hooks/fileChangedWatcher.ts +191 -0
- package/src/utils/hooks/hookEvents.ts +192 -0
- package/src/utils/hooks/hookHelpers.ts +83 -0
- package/src/utils/hooks/hooksConfigManager.ts +400 -0
- package/src/utils/hooks/hooksConfigSnapshot.ts +133 -0
- package/src/utils/hooks/hooksSettings.ts +271 -0
- package/src/utils/hooks/postSamplingHooks.ts +70 -0
- package/src/utils/hooks/registerFrontmatterHooks.ts +67 -0
- package/src/utils/hooks/registerSkillHooks.ts +64 -0
- package/src/utils/hooks/sessionHooks.ts +447 -0
- package/src/utils/hooks/skillImprovement.ts +267 -0
- package/src/utils/hooks/ssrfGuard.ts +294 -0
- package/src/utils/hooks.ts +5022 -0
- package/src/utils/horizontalScroll.ts +137 -0
- package/src/utils/http.ts +136 -0
- package/src/utils/hyperlink.ts +39 -0
- package/src/utils/iTermBackup.ts +73 -0
- package/src/utils/ide.ts +1494 -0
- package/src/utils/idePathConversion.ts +90 -0
- package/src/utils/idleTimeout.ts +53 -0
- package/src/utils/imagePaste.ts +416 -0
- package/src/utils/imageResizer.ts +880 -0
- package/src/utils/imageStore.ts +167 -0
- package/src/utils/imageValidation.ts +104 -0
- package/src/utils/immediateCommand.ts +15 -0
- package/src/utils/inProcessTeammateHelpers.ts +102 -0
- package/src/utils/ink.ts +26 -0
- package/src/utils/intl.ts +94 -0
- package/src/utils/jetbrains.ts +191 -0
- package/src/utils/json.ts +277 -0
- package/src/utils/jsonRead.ts +16 -0
- package/src/utils/keyboardShortcuts.ts +14 -0
- package/src/utils/lazySchema.ts +8 -0
- package/src/utils/listSessionsImpl.ts +454 -0
- package/src/utils/localInstaller.ts +162 -0
- package/src/utils/lockfile.ts +43 -0
- package/src/utils/log.ts +362 -0
- package/src/utils/logoV2Utils.ts +350 -0
- package/src/utils/mailbox.ts +73 -0
- package/src/utils/managedEnv.ts +199 -0
- package/src/utils/managedEnvConstants.ts +191 -0
- package/src/utils/markdown.ts +381 -0
- package/src/utils/markdownConfigLoader.ts +600 -0
- package/src/utils/mcp/dateTimeParser.ts +121 -0
- package/src/utils/mcp/elicitationValidation.ts +336 -0
- package/src/utils/mcpInstructionsDelta.ts +130 -0
- package/src/utils/mcpOutputStorage.ts +189 -0
- package/src/utils/mcpValidation.ts +208 -0
- package/src/utils/mcpWebSocketTransport.ts +200 -0
- package/src/utils/memoize.ts +269 -0
- package/src/utils/memory/types.ts +12 -0
- package/src/utils/memory/versions.ts +8 -0
- package/src/utils/memoryFileDetection.ts +289 -0
- package/src/utils/messagePredicates.ts +8 -0
- package/src/utils/messageQueueManager.ts +547 -0
- package/src/utils/messages/mappers.ts +290 -0
- package/src/utils/messages/systemInit.ts +96 -0
- package/src/utils/messages.ts +5512 -0
- package/src/utils/model/agent.ts +157 -0
- package/src/utils/model/aliases.ts +25 -0
- package/src/utils/model/antModels.ts +64 -0
- package/src/utils/model/bedrock.ts +265 -0
- package/src/utils/model/check1mAccess.ts +72 -0
- package/src/utils/model/configs.ts +118 -0
- package/src/utils/model/contextWindowUpgradeCheck.ts +47 -0
- package/src/utils/model/deprecation.ts +101 -0
- package/src/utils/model/model.ts +634 -0
- package/src/utils/model/modelAllowlist.ts +170 -0
- package/src/utils/model/modelCapabilities.ts +118 -0
- package/src/utils/model/modelOptions.ts +540 -0
- package/src/utils/model/modelStrings.ts +166 -0
- package/src/utils/model/modelSupportOverrides.ts +50 -0
- package/src/utils/model/providers.ts +46 -0
- package/src/utils/model/validateModel.ts +159 -0
- package/src/utils/modelCost.ts +235 -0
- package/src/utils/modifiers.ts +36 -0
- package/src/utils/mtls.ts +179 -0
- package/src/utils/nativeInstaller/download.ts +523 -0
- package/src/utils/nativeInstaller/index.ts +18 -0
- package/src/utils/nativeInstaller/installer.ts +1708 -0
- package/src/utils/nativeInstaller/packageManagers.ts +336 -0
- package/src/utils/nativeInstaller/pidLock.ts +433 -0
- package/src/utils/notebook.ts +224 -0
- package/src/utils/objectGroupBy.ts +18 -0
- package/src/utils/pasteStore.ts +104 -0
- package/src/utils/path.ts +155 -0
- package/src/utils/pdf.ts +300 -0
- package/src/utils/pdfUtils.ts +70 -0
- package/src/utils/peerAddress.ts +21 -0
- package/src/utils/permissions/PermissionMode.ts +141 -0
- package/src/utils/permissions/PermissionPromptToolResultSchema.ts +127 -0
- package/src/utils/permissions/PermissionResult.ts +35 -0
- package/src/utils/permissions/PermissionRule.ts +40 -0
- package/src/utils/permissions/PermissionUpdate.ts +389 -0
- package/src/utils/permissions/PermissionUpdateSchema.ts +78 -0
- package/src/utils/permissions/autoModeState.ts +39 -0
- package/src/utils/permissions/bashClassifier.ts +61 -0
- package/src/utils/permissions/bypassPermissionsKillswitch.ts +155 -0
- package/src/utils/permissions/classifierDecision.ts +98 -0
- package/src/utils/permissions/classifierShared.ts +39 -0
- package/src/utils/permissions/dangerousPatterns.ts +80 -0
- package/src/utils/permissions/denialTracking.ts +45 -0
- package/src/utils/permissions/filesystem.ts +1777 -0
- package/src/utils/permissions/getNextPermissionMode.ts +101 -0
- package/src/utils/permissions/pathValidation.ts +485 -0
- package/src/utils/permissions/permissionExplainer.ts +250 -0
- package/src/utils/permissions/permissionRuleParser.ts +198 -0
- package/src/utils/permissions/permissionSetup.ts +1532 -0
- package/src/utils/permissions/permissions.ts +1486 -0
- package/src/utils/permissions/permissionsLoader.ts +296 -0
- package/src/utils/permissions/shadowedRuleDetection.ts +234 -0
- package/src/utils/permissions/shellRuleMatching.ts +228 -0
- package/src/utils/permissions/yoloClassifier.ts +1495 -0
- package/src/utils/planModeV2.ts +95 -0
- package/src/utils/plans.ts +397 -0
- package/src/utils/platform.ts +150 -0
- package/src/utils/plugins/addDirPluginSettings.ts +71 -0
- package/src/utils/plugins/cacheUtils.ts +196 -0
- package/src/utils/plugins/dependencyResolver.ts +305 -0
- package/src/utils/plugins/fetchTelemetry.ts +135 -0
- package/src/utils/plugins/gitAvailability.ts +69 -0
- package/src/utils/plugins/headlessPluginInstall.ts +174 -0
- package/src/utils/plugins/hintRecommendation.ts +164 -0
- package/src/utils/plugins/installCounts.ts +292 -0
- package/src/utils/plugins/installedPluginsManager.ts +1268 -0
- package/src/utils/plugins/loadPluginAgents.ts +348 -0
- package/src/utils/plugins/loadPluginCommands.ts +946 -0
- package/src/utils/plugins/loadPluginHooks.ts +287 -0
- package/src/utils/plugins/loadPluginOutputStyles.ts +178 -0
- package/src/utils/plugins/lspPluginIntegration.ts +387 -0
- package/src/utils/plugins/lspRecommendation.ts +374 -0
- package/src/utils/plugins/managedPlugins.ts +27 -0
- package/src/utils/plugins/marketplaceHelpers.ts +592 -0
- package/src/utils/plugins/marketplaceManager.ts +2643 -0
- package/src/utils/plugins/mcpPluginIntegration.ts +634 -0
- package/src/utils/plugins/mcpbHandler.ts +968 -0
- package/src/utils/plugins/officialMarketplace.ts +25 -0
- package/src/utils/plugins/officialMarketplaceGcs.ts +216 -0
- package/src/utils/plugins/officialMarketplaceStartupCheck.ts +439 -0
- package/src/utils/plugins/orphanedPluginFilter.ts +114 -0
- package/src/utils/plugins/parseMarketplaceInput.ts +162 -0
- package/src/utils/plugins/performStartupChecks.tsx +70 -0
- package/src/utils/plugins/pluginAutoupdate.ts +284 -0
- package/src/utils/plugins/pluginBlocklist.ts +127 -0
- package/src/utils/plugins/pluginDirectories.ts +178 -0
- package/src/utils/plugins/pluginFlagging.ts +208 -0
- package/src/utils/plugins/pluginIdentifier.ts +123 -0
- package/src/utils/plugins/pluginInstallationHelpers.ts +595 -0
- package/src/utils/plugins/pluginLoader.ts +3302 -0
- package/src/utils/plugins/pluginOptionsStorage.ts +400 -0
- package/src/utils/plugins/pluginPolicy.ts +20 -0
- package/src/utils/plugins/pluginStartupCheck.ts +341 -0
- package/src/utils/plugins/pluginVersioning.ts +157 -0
- package/src/utils/plugins/reconciler.ts +265 -0
- package/src/utils/plugins/refresh.ts +215 -0
- package/src/utils/plugins/schemas.ts +1681 -0
- package/src/utils/plugins/validatePlugin.ts +903 -0
- package/src/utils/plugins/walkPluginMarkdown.ts +69 -0
- package/src/utils/plugins/zipCache.ts +406 -0
- package/src/utils/plugins/zipCacheAdapters.ts +164 -0
- package/src/utils/powershell/dangerousCmdlets.ts +185 -0
- package/src/utils/powershell/parser.ts +1804 -0
- package/src/utils/powershell/staticPrefix.ts +316 -0
- package/src/utils/preflightChecks.tsx +151 -0
- package/src/utils/privacyLevel.ts +55 -0
- package/src/utils/process.ts +68 -0
- package/src/utils/processUserInput/processBashCommand.tsx +140 -0
- package/src/utils/processUserInput/processSlashCommand.tsx +922 -0
- package/src/utils/processUserInput/processTextPrompt.ts +100 -0
- package/src/utils/processUserInput/processUserInput.ts +605 -0
- package/src/utils/profilerBase.ts +46 -0
- package/src/utils/promptCategory.ts +49 -0
- package/src/utils/promptEditor.ts +188 -0
- package/src/utils/promptShellExecution.ts +183 -0
- package/src/utils/proxy.ts +426 -0
- package/src/utils/queryContext.ts +179 -0
- package/src/utils/queryHelpers.ts +552 -0
- package/src/utils/queryProfiler.ts +301 -0
- package/src/utils/queueProcessor.ts +95 -0
- package/src/utils/readEditContext.ts +227 -0
- package/src/utils/readFileInRange.ts +383 -0
- package/src/utils/releaseNotes.ts +360 -0
- package/src/utils/renderOptions.ts +77 -0
- package/src/utils/ripgrep.ts +679 -0
- package/src/utils/sandbox/sandbox-adapter.ts +985 -0
- package/src/utils/sandbox/sandbox-ui-utils.ts +12 -0
- package/src/utils/sanitization.ts +91 -0
- package/src/utils/screenshotClipboard.ts +121 -0
- package/src/utils/sdkEventQueue.ts +134 -0
- package/src/utils/secureStorage/fallbackStorage.ts +70 -0
- package/src/utils/secureStorage/index.ts +17 -0
- package/src/utils/secureStorage/keychainPrefetch.ts +116 -0
- package/src/utils/secureStorage/macOsKeychainHelpers.ts +111 -0
- package/src/utils/secureStorage/macOsKeychainStorage.ts +231 -0
- package/src/utils/secureStorage/plainTextStorage.ts +84 -0
- package/src/utils/semanticBoolean.ts +29 -0
- package/src/utils/semanticNumber.ts +36 -0
- package/src/utils/semver.ts +59 -0
- package/src/utils/sequential.ts +56 -0
- package/src/utils/sessionActivity.ts +133 -0
- package/src/utils/sessionEnvVars.ts +22 -0
- package/src/utils/sessionEnvironment.ts +166 -0
- package/src/utils/sessionFileAccessHooks.ts +250 -0
- package/src/utils/sessionIngressAuth.ts +140 -0
- package/src/utils/sessionRestore.ts +551 -0
- package/src/utils/sessionStart.ts +232 -0
- package/src/utils/sessionState.ts +150 -0
- package/src/utils/sessionStorage.ts +5105 -0
- package/src/utils/sessionStoragePortable.ts +793 -0
- package/src/utils/sessionTitle.ts +129 -0
- package/src/utils/sessionUrl.ts +64 -0
- package/src/utils/set.ts +53 -0
- package/src/utils/settings/allErrors.ts +32 -0
- package/src/utils/settings/applySettingsChange.ts +92 -0
- package/src/utils/settings/changeDetector.ts +488 -0
- package/src/utils/settings/constants.ts +202 -0
- package/src/utils/settings/internalWrites.ts +37 -0
- package/src/utils/settings/managedPath.ts +34 -0
- package/src/utils/settings/mdm/constants.ts +81 -0
- package/src/utils/settings/mdm/rawRead.ts +130 -0
- package/src/utils/settings/mdm/settings.ts +316 -0
- package/src/utils/settings/permissionValidation.ts +262 -0
- package/src/utils/settings/pluginOnlyPolicy.ts +60 -0
- package/src/utils/settings/schemaOutput.ts +8 -0
- package/src/utils/settings/settings.ts +1015 -0
- package/src/utils/settings/settingsCache.ts +80 -0
- package/src/utils/settings/toolValidationConfig.ts +103 -0
- package/src/utils/settings/types.ts +1148 -0
- package/src/utils/settings/validateEditTool.ts +45 -0
- package/src/utils/settings/validation.ts +265 -0
- package/src/utils/settings/validationTips.ts +164 -0
- package/src/utils/shell/bashProvider.ts +255 -0
- package/src/utils/shell/outputLimits.ts +14 -0
- package/src/utils/shell/powershellDetection.ts +107 -0
- package/src/utils/shell/powershellProvider.ts +123 -0
- package/src/utils/shell/prefix.ts +367 -0
- package/src/utils/shell/readOnlyCommandValidation.ts +1893 -0
- package/src/utils/shell/resolveDefaultShell.ts +14 -0
- package/src/utils/shell/shellProvider.ts +33 -0
- package/src/utils/shell/shellToolUtils.ts +22 -0
- package/src/utils/shell/specPrefix.ts +241 -0
- package/src/utils/shellConfig.ts +167 -0
- package/src/utils/sideQuery.ts +222 -0
- package/src/utils/sideQuestion.ts +155 -0
- package/src/utils/signal.ts +43 -0
- package/src/utils/sinks.ts +16 -0
- package/src/utils/skills/skillChangeDetector.ts +311 -0
- package/src/utils/slashCommandParsing.ts +60 -0
- package/src/utils/sleep.ts +84 -0
- package/src/utils/sliceAnsi.ts +91 -0
- package/src/utils/slowOperations.ts +286 -0
- package/src/utils/standaloneAgent.ts +23 -0
- package/src/utils/startupProfiler.ts +194 -0
- package/src/utils/staticRender.tsx +116 -0
- package/src/utils/stats.ts +1061 -0
- package/src/utils/statsCache.ts +434 -0
- package/src/utils/status.tsx +362 -0
- package/src/utils/statusNoticeDefinitions.tsx +198 -0
- package/src/utils/statusNoticeHelpers.ts +20 -0
- package/src/utils/stream.ts +76 -0
- package/src/utils/streamJsonStdoutGuard.ts +123 -0
- package/src/utils/streamlinedTransform.ts +201 -0
- package/src/utils/stringUtils.ts +235 -0
- package/src/utils/subprocessEnv.ts +99 -0
- package/src/utils/suggestions/commandSuggestions.ts +567 -0
- package/src/utils/suggestions/directoryCompletion.ts +263 -0
- package/src/utils/suggestions/shellHistoryCompletion.ts +119 -0
- package/src/utils/suggestions/skillUsageTracking.ts +55 -0
- package/src/utils/suggestions/slackChannelSuggestions.ts +209 -0
- package/src/utils/swarm/It2SetupPrompt.tsx +380 -0
- package/src/utils/swarm/backends/ITermBackend.ts +370 -0
- package/src/utils/swarm/backends/InProcessBackend.ts +339 -0
- package/src/utils/swarm/backends/PaneBackendExecutor.ts +354 -0
- package/src/utils/swarm/backends/TmuxBackend.ts +764 -0
- package/src/utils/swarm/backends/detection.ts +128 -0
- package/src/utils/swarm/backends/it2Setup.ts +245 -0
- package/src/utils/swarm/backends/registry.ts +464 -0
- package/src/utils/swarm/backends/teammateModeSnapshot.ts +87 -0
- package/src/utils/swarm/backends/types.ts +311 -0
- package/src/utils/swarm/constants.ts +33 -0
- package/src/utils/swarm/inProcessRunner.ts +1552 -0
- package/src/utils/swarm/leaderPermissionBridge.ts +54 -0
- package/src/utils/swarm/permissionSync.ts +928 -0
- package/src/utils/swarm/reconnection.ts +119 -0
- package/src/utils/swarm/spawnInProcess.ts +328 -0
- package/src/utils/swarm/spawnUtils.ts +146 -0
- package/src/utils/swarm/teamHelpers.ts +683 -0
- package/src/utils/swarm/teammateInit.ts +129 -0
- package/src/utils/swarm/teammateLayoutManager.ts +107 -0
- package/src/utils/swarm/teammateModel.ts +10 -0
- package/src/utils/swarm/teammatePromptAddendum.ts +18 -0
- package/src/utils/systemDirectories.ts +74 -0
- package/src/utils/systemPrompt.ts +123 -0
- package/src/utils/systemPromptType.ts +14 -0
- package/src/utils/systemTheme.ts +119 -0
- package/src/utils/taggedId.ts +54 -0
- package/src/utils/task/TaskOutput.ts +390 -0
- package/src/utils/task/diskOutput.ts +451 -0
- package/src/utils/task/framework.ts +308 -0
- package/src/utils/task/outputFormatting.ts +38 -0
- package/src/utils/task/sdkProgress.ts +36 -0
- package/src/utils/tasks.ts +862 -0
- package/src/utils/teamDiscovery.ts +81 -0
- package/src/utils/teamMemoryOps.ts +88 -0
- package/src/utils/teammate.ts +292 -0
- package/src/utils/teammateContext.ts +96 -0
- package/src/utils/teammateMailbox.ts +1183 -0
- package/src/utils/telemetry/betaSessionTracing.ts +491 -0
- package/src/utils/telemetry/bigqueryExporter.ts +252 -0
- package/src/utils/telemetry/events.ts +75 -0
- package/src/utils/telemetry/instrumentation.ts +825 -0
- package/src/utils/telemetry/logger.ts +26 -0
- package/src/utils/telemetry/perfettoTracing.ts +1120 -0
- package/src/utils/telemetry/pluginTelemetry.ts +289 -0
- package/src/utils/telemetry/sessionTracing.ts +927 -0
- package/src/utils/telemetry/skillLoadedEvent.ts +39 -0
- package/src/utils/telemetryAttributes.ts +71 -0
- package/src/utils/teleport/api.ts +466 -0
- package/src/utils/teleport/environmentSelection.ts +77 -0
- package/src/utils/teleport/environments.ts +120 -0
- package/src/utils/teleport/gitBundle.ts +292 -0
- package/src/utils/teleport.tsx +1226 -0
- package/src/utils/tempfile.ts +31 -0
- package/src/utils/terminal.ts +131 -0
- package/src/utils/terminalPanel.ts +191 -0
- package/src/utils/textHighlighting.ts +166 -0
- package/src/utils/theme.ts +639 -0
- package/src/utils/thinking.ts +162 -0
- package/src/utils/timeouts.ts +39 -0
- package/src/utils/tmuxSocket.ts +427 -0
- package/src/utils/todo/types.ts +18 -0
- package/src/utils/tokenBudget.ts +73 -0
- package/src/utils/tokens.ts +261 -0
- package/src/utils/toolErrors.ts +132 -0
- package/src/utils/toolPool.ts +79 -0
- package/src/utils/toolResultStorage.ts +1040 -0
- package/src/utils/toolSchemaCache.ts +26 -0
- package/src/utils/toolSearch.ts +756 -0
- package/src/utils/transcriptSearch.ts +202 -0
- package/src/utils/treeify.ts +170 -0
- package/src/utils/truncate.ts +179 -0
- package/src/utils/ultraplan/ccrSession.ts +349 -0
- package/src/utils/ultraplan/keyword.ts +127 -0
- package/src/utils/ultraplan/prompt.txt +1 -0
- package/src/utils/unaryLogging.ts +39 -0
- package/src/utils/undercover.ts +89 -0
- package/src/utils/user.ts +194 -0
- package/src/utils/userAgent.ts +10 -0
- package/src/utils/userPromptKeywords.ts +27 -0
- package/src/utils/uuid.ts +27 -0
- package/src/utils/warningHandler.ts +121 -0
- package/src/utils/which.ts +82 -0
- package/src/utils/windowsPaths.ts +173 -0
- package/src/utils/withResolvers.ts +13 -0
- package/src/utils/words.ts +800 -0
- package/src/utils/workloadContext.ts +57 -0
- package/src/utils/worktree.ts +1519 -0
- package/src/utils/worktreeModeEnabled.ts +11 -0
- package/src/utils/xdg.ts +65 -0
- package/src/utils/xml.ts +16 -0
- package/src/utils/yaml.ts +15 -0
- package/src/utils/zodToJsonSchema.ts +23 -0
- package/src/vim/motions.ts +82 -0
- package/src/vim/operators.ts +556 -0
- package/src/vim/textObjects.ts +186 -0
- package/src/vim/transitions.ts +490 -0
- package/src/vim/types.ts +199 -0
- package/src/voice/voiceModeEnabled.ts +54 -0
- package/start.js +1 -0
|
@@ -0,0 +1,3433 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BetaContentBlock,
|
|
3
|
+
BetaContentBlockParam,
|
|
4
|
+
BetaImageBlockParam,
|
|
5
|
+
BetaJSONOutputFormat,
|
|
6
|
+
BetaMessage,
|
|
7
|
+
BetaMessageDeltaUsage,
|
|
8
|
+
BetaMessageStreamParams,
|
|
9
|
+
BetaOutputConfig,
|
|
10
|
+
BetaRawMessageStreamEvent,
|
|
11
|
+
BetaRequestDocumentBlock,
|
|
12
|
+
BetaStopReason,
|
|
13
|
+
BetaToolChoiceAuto,
|
|
14
|
+
BetaToolChoiceTool,
|
|
15
|
+
BetaToolResultBlockParam,
|
|
16
|
+
BetaToolUnion,
|
|
17
|
+
BetaUsage,
|
|
18
|
+
BetaMessageParam as MessageParam,
|
|
19
|
+
} from '@anthropic-ai/sdk/resources/beta/messages/messages.mjs'
|
|
20
|
+
import type { TextBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'
|
|
21
|
+
import type { Stream } from '@anthropic-ai/sdk/streaming.mjs'
|
|
22
|
+
import { randomUUID } from 'crypto'
|
|
23
|
+
import {
|
|
24
|
+
getAPIProvider,
|
|
25
|
+
isFirstPartyAnthropicBaseUrl,
|
|
26
|
+
} from 'src/utils/model/providers.js'
|
|
27
|
+
import {
|
|
28
|
+
getAttributionHeader,
|
|
29
|
+
getCLISyspromptPrefix,
|
|
30
|
+
} from '../../constants/system.js'
|
|
31
|
+
import {
|
|
32
|
+
getEmptyToolPermissionContext,
|
|
33
|
+
type QueryChainTracking,
|
|
34
|
+
type Tool,
|
|
35
|
+
type ToolPermissionContext,
|
|
36
|
+
type Tools,
|
|
37
|
+
toolMatchesName,
|
|
38
|
+
} from '../../Tool.js'
|
|
39
|
+
import type { AgentDefinition } from '../../tools/AgentTool/loadAgentsDir.js'
|
|
40
|
+
import {
|
|
41
|
+
type ConnectorTextBlock,
|
|
42
|
+
type ConnectorTextDelta,
|
|
43
|
+
isConnectorTextBlock,
|
|
44
|
+
} from '../../types/connectorText.js'
|
|
45
|
+
import type {
|
|
46
|
+
AssistantMessage,
|
|
47
|
+
Message,
|
|
48
|
+
StreamEvent,
|
|
49
|
+
SystemAPIErrorMessage,
|
|
50
|
+
UserMessage,
|
|
51
|
+
} from '../../types/message.js'
|
|
52
|
+
import {
|
|
53
|
+
type CacheScope,
|
|
54
|
+
logAPIPrefix,
|
|
55
|
+
splitSysPromptPrefix,
|
|
56
|
+
toolToAPISchema,
|
|
57
|
+
} from '../../utils/api.js'
|
|
58
|
+
import { getOauthAccountInfo } from '../../utils/auth.js'
|
|
59
|
+
import {
|
|
60
|
+
getBedrockExtraBodyParamsBetas,
|
|
61
|
+
getMergedBetas,
|
|
62
|
+
getModelBetas,
|
|
63
|
+
} from '../../utils/betas.js'
|
|
64
|
+
import { getOrCreateUserID } from '../../utils/config.js'
|
|
65
|
+
import {
|
|
66
|
+
CAPPED_DEFAULT_MAX_TOKENS,
|
|
67
|
+
getModelMaxOutputTokens,
|
|
68
|
+
getSonnet1mExpTreatmentEnabled,
|
|
69
|
+
} from '../../utils/context.js'
|
|
70
|
+
import { resolveAppliedEffort } from '../../utils/effort.js'
|
|
71
|
+
import { isEnvTruthy } from '../../utils/envUtils.js'
|
|
72
|
+
import { errorMessage } from '../../utils/errors.js'
|
|
73
|
+
import { computeFingerprintFromMessages } from '../../utils/fingerprint.js'
|
|
74
|
+
import { captureAPIRequest, logError } from '../../utils/log.js'
|
|
75
|
+
import {
|
|
76
|
+
createAssistantAPIErrorMessage,
|
|
77
|
+
createUserMessage,
|
|
78
|
+
ensureToolResultPairing,
|
|
79
|
+
normalizeContentFromAPI,
|
|
80
|
+
normalizeMessagesForAPI,
|
|
81
|
+
stripAdvisorBlocks,
|
|
82
|
+
stripCallerFieldFromAssistantMessage,
|
|
83
|
+
stripToolReferenceBlocksFromUserMessage,
|
|
84
|
+
} from '../../utils/messages.js'
|
|
85
|
+
import {
|
|
86
|
+
getDefaultOpusModel,
|
|
87
|
+
getDefaultSonnetModel,
|
|
88
|
+
getSmallFastModel,
|
|
89
|
+
isNonCustomOpusModel,
|
|
90
|
+
} from '../../utils/model/model.js'
|
|
91
|
+
import {
|
|
92
|
+
asSystemPrompt,
|
|
93
|
+
type SystemPrompt,
|
|
94
|
+
} from '../../utils/systemPromptType.js'
|
|
95
|
+
import { tokenCountFromLastAPIResponse } from '../../utils/tokens.js'
|
|
96
|
+
import { getDynamicConfig_BLOCKS_ON_INIT } from '../analytics/growthbook.js'
|
|
97
|
+
import {
|
|
98
|
+
currentLimits,
|
|
99
|
+
extractQuotaStatusFromError,
|
|
100
|
+
extractQuotaStatusFromHeaders,
|
|
101
|
+
} from '../claudeAiLimits.js'
|
|
102
|
+
import { getAPIContextManagement } from '../compact/apiMicrocompact.js'
|
|
103
|
+
|
|
104
|
+
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
105
|
+
const autoModeStateModule = feature('TRANSCRIPT_CLASSIFIER')
|
|
106
|
+
? (require('../../utils/permissions/autoModeState.js') as typeof import('../../utils/permissions/autoModeState.js'))
|
|
107
|
+
: null
|
|
108
|
+
|
|
109
|
+
import { feature } from 'bun:bundle'
|
|
110
|
+
import type { ClientOptions } from '@anthropic-ai/sdk'
|
|
111
|
+
import {
|
|
112
|
+
APIConnectionTimeoutError,
|
|
113
|
+
APIError,
|
|
114
|
+
APIUserAbortError,
|
|
115
|
+
} from '@anthropic-ai/sdk/error'
|
|
116
|
+
import {
|
|
117
|
+
getAfkModeHeaderLatched,
|
|
118
|
+
getCacheEditingHeaderLatched,
|
|
119
|
+
getFastModeHeaderLatched,
|
|
120
|
+
getLastApiCompletionTimestamp,
|
|
121
|
+
getPromptCache1hAllowlist,
|
|
122
|
+
getPromptCache1hEligible,
|
|
123
|
+
getSessionId,
|
|
124
|
+
getThinkingClearLatched,
|
|
125
|
+
setAfkModeHeaderLatched,
|
|
126
|
+
setCacheEditingHeaderLatched,
|
|
127
|
+
setFastModeHeaderLatched,
|
|
128
|
+
setLastMainRequestId,
|
|
129
|
+
setPromptCache1hAllowlist,
|
|
130
|
+
setPromptCache1hEligible,
|
|
131
|
+
setThinkingClearLatched,
|
|
132
|
+
} from 'src/bootstrap/state.js'
|
|
133
|
+
import {
|
|
134
|
+
AFK_MODE_BETA_HEADER,
|
|
135
|
+
CONTEXT_1M_BETA_HEADER,
|
|
136
|
+
CONTEXT_MANAGEMENT_BETA_HEADER,
|
|
137
|
+
EFFORT_BETA_HEADER,
|
|
138
|
+
FAST_MODE_BETA_HEADER,
|
|
139
|
+
PROMPT_CACHING_SCOPE_BETA_HEADER,
|
|
140
|
+
REDACT_THINKING_BETA_HEADER,
|
|
141
|
+
STRUCTURED_OUTPUTS_BETA_HEADER,
|
|
142
|
+
TASK_BUDGETS_BETA_HEADER,
|
|
143
|
+
} from 'src/constants/betas.js'
|
|
144
|
+
import type { QuerySource } from 'src/constants/querySource.js'
|
|
145
|
+
import type { Notification } from 'src/context/notifications.js'
|
|
146
|
+
import { addToTotalSessionCost } from 'src/cost-tracker.js'
|
|
147
|
+
import { getFeatureValue_CACHED_MAY_BE_STALE } from 'src/services/analytics/growthbook.js'
|
|
148
|
+
import type { AgentId } from 'src/types/ids.js'
|
|
149
|
+
import {
|
|
150
|
+
ADVISOR_TOOL_INSTRUCTIONS,
|
|
151
|
+
getExperimentAdvisorModels,
|
|
152
|
+
isAdvisorEnabled,
|
|
153
|
+
isValidAdvisorModel,
|
|
154
|
+
modelSupportsAdvisor,
|
|
155
|
+
} from 'src/utils/advisor.js'
|
|
156
|
+
import { getAgentContext } from 'src/utils/agentContext.js'
|
|
157
|
+
import { isClaudeAISubscriber } from 'src/utils/auth.js'
|
|
158
|
+
import {
|
|
159
|
+
getToolSearchBetaHeader,
|
|
160
|
+
modelSupportsStructuredOutputs,
|
|
161
|
+
shouldIncludeFirstPartyOnlyBetas,
|
|
162
|
+
shouldUseGlobalCacheScope,
|
|
163
|
+
} from 'src/utils/betas.js'
|
|
164
|
+
import { CLAUDE_IN_CHROME_MCP_SERVER_NAME } from 'src/utils/claudeInChrome/common.js'
|
|
165
|
+
import { CHROME_TOOL_SEARCH_INSTRUCTIONS } from 'src/utils/claudeInChrome/prompt.js'
|
|
166
|
+
import { getMaxThinkingTokensForModel } from 'src/utils/context.js'
|
|
167
|
+
import { logForDebugging } from 'src/utils/debug.js'
|
|
168
|
+
import { logForDiagnosticsNoPII } from 'src/utils/diagLogs.js'
|
|
169
|
+
import { type EffortValue, modelSupportsEffort } from 'src/utils/effort.js'
|
|
170
|
+
import {
|
|
171
|
+
isFastModeAvailable,
|
|
172
|
+
isFastModeCooldown,
|
|
173
|
+
isFastModeEnabled,
|
|
174
|
+
isFastModeSupportedByModel,
|
|
175
|
+
} from 'src/utils/fastMode.js'
|
|
176
|
+
import { returnValue } from 'src/utils/generators.js'
|
|
177
|
+
import { headlessProfilerCheckpoint } from 'src/utils/headlessProfiler.js'
|
|
178
|
+
import { isMcpInstructionsDeltaEnabled } from 'src/utils/mcpInstructionsDelta.js'
|
|
179
|
+
import { calculateUSDCost } from 'src/utils/modelCost.js'
|
|
180
|
+
import { endQueryProfile, queryCheckpoint } from 'src/utils/queryProfiler.js'
|
|
181
|
+
import {
|
|
182
|
+
modelSupportsAdaptiveThinking,
|
|
183
|
+
modelSupportsThinking,
|
|
184
|
+
type ThinkingConfig,
|
|
185
|
+
} from 'src/utils/thinking.js'
|
|
186
|
+
import {
|
|
187
|
+
extractDiscoveredToolNames,
|
|
188
|
+
isDeferredToolsDeltaEnabled,
|
|
189
|
+
isToolSearchEnabled,
|
|
190
|
+
} from 'src/utils/toolSearch.js'
|
|
191
|
+
import { API_MAX_MEDIA_PER_REQUEST } from '../../constants/apiLimits.js'
|
|
192
|
+
import { ADVISOR_BETA_HEADER } from '../../constants/betas.js'
|
|
193
|
+
import {
|
|
194
|
+
formatDeferredToolLine,
|
|
195
|
+
isDeferredTool,
|
|
196
|
+
TOOL_SEARCH_TOOL_NAME,
|
|
197
|
+
} from '../../tools/ToolSearchTool/prompt.js'
|
|
198
|
+
import { count } from '../../utils/array.js'
|
|
199
|
+
import { insertBlockAfterToolResults } from '../../utils/contentArray.js'
|
|
200
|
+
import { validateBoundedIntEnvVar } from '../../utils/envValidation.js'
|
|
201
|
+
import { safeParseJSON } from '../../utils/json.js'
|
|
202
|
+
import { getInferenceProfileBackingModel } from '../../utils/model/bedrock.js'
|
|
203
|
+
import {
|
|
204
|
+
normalizeModelStringForAPI,
|
|
205
|
+
parseUserSpecifiedModel,
|
|
206
|
+
} from '../../utils/model/model.js'
|
|
207
|
+
import {
|
|
208
|
+
startSessionActivity,
|
|
209
|
+
stopSessionActivity,
|
|
210
|
+
} from '../../utils/sessionActivity.js'
|
|
211
|
+
import { jsonStringify } from '../../utils/slowOperations.js'
|
|
212
|
+
import {
|
|
213
|
+
isBetaTracingEnabled,
|
|
214
|
+
type LLMRequestNewContext,
|
|
215
|
+
startLLMRequestSpan,
|
|
216
|
+
} from '../../utils/telemetry/sessionTracing.js'
|
|
217
|
+
/* eslint-enable @typescript-eslint/no-require-imports */
|
|
218
|
+
import {
|
|
219
|
+
type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
220
|
+
logEvent,
|
|
221
|
+
} from '../analytics/index.js'
|
|
222
|
+
import {
|
|
223
|
+
consumePendingCacheEdits,
|
|
224
|
+
getPinnedCacheEdits,
|
|
225
|
+
markToolsSentToAPIState,
|
|
226
|
+
pinCacheEdits,
|
|
227
|
+
} from '../compact/microCompact.js'
|
|
228
|
+
import { getInitializationStatus } from '../lsp/manager.js'
|
|
229
|
+
import { isToolFromMcpServer } from '../mcp/utils.js'
|
|
230
|
+
import { withStreamingVCR, withVCR } from '../vcr.js'
|
|
231
|
+
import { CLIENT_REQUEST_ID_HEADER, getAnthropicClient } from './client.js'
|
|
232
|
+
import {
|
|
233
|
+
API_ERROR_MESSAGE_PREFIX,
|
|
234
|
+
CUSTOM_OFF_SWITCH_MESSAGE,
|
|
235
|
+
getAssistantMessageFromError,
|
|
236
|
+
getErrorMessageIfRefusal,
|
|
237
|
+
} from './errors.js'
|
|
238
|
+
import {
|
|
239
|
+
EMPTY_USAGE,
|
|
240
|
+
type GlobalCacheStrategy,
|
|
241
|
+
logAPIError,
|
|
242
|
+
logAPIQuery,
|
|
243
|
+
logAPISuccessAndDuration,
|
|
244
|
+
type NonNullableUsage,
|
|
245
|
+
} from './logging.js'
|
|
246
|
+
import {
|
|
247
|
+
CACHE_TTL_1HOUR_MS,
|
|
248
|
+
checkResponseForCacheBreak,
|
|
249
|
+
recordPromptState,
|
|
250
|
+
} from './promptCacheBreakDetection.js'
|
|
251
|
+
import {
|
|
252
|
+
CannotRetryError,
|
|
253
|
+
FallbackTriggeredError,
|
|
254
|
+
is529Error,
|
|
255
|
+
type RetryContext,
|
|
256
|
+
withRetry,
|
|
257
|
+
} from './withRetry.js'
|
|
258
|
+
|
|
259
|
+
// Define a type that represents valid JSON values
|
|
260
|
+
type JsonValue = string | number | boolean | null | JsonObject | JsonArray
|
|
261
|
+
type JsonObject = { [key: string]: JsonValue }
|
|
262
|
+
type JsonArray = JsonValue[]
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Assemble the extra body parameters for the API request, based on the
|
|
266
|
+
* CLAUDE_CODE_EXTRA_BODY environment variable if present and on any beta
|
|
267
|
+
* headers (primarily for Bedrock requests).
|
|
268
|
+
*
|
|
269
|
+
* @param betaHeaders - An array of beta headers to include in the request.
|
|
270
|
+
* @returns A JSON object representing the extra body parameters.
|
|
271
|
+
*/
|
|
272
|
+
export function getExtraBodyParams(betaHeaders?: string[]): JsonObject {
|
|
273
|
+
// Skip Anthropic-specific extra body params when using OpenAI backend
|
|
274
|
+
if (process.env.OPENAI_API_KEY) {
|
|
275
|
+
return {}
|
|
276
|
+
}
|
|
277
|
+
// Parse user's extra body parameters first
|
|
278
|
+
const extraBodyStr = process.env.CLAUDE_CODE_EXTRA_BODY
|
|
279
|
+
let result: JsonObject = {}
|
|
280
|
+
|
|
281
|
+
if (extraBodyStr) {
|
|
282
|
+
try {
|
|
283
|
+
// Parse as JSON, which can be null, boolean, number, string, array or object
|
|
284
|
+
const parsed = safeParseJSON(extraBodyStr)
|
|
285
|
+
// We expect an object with key-value pairs to spread into API parameters
|
|
286
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
287
|
+
// Shallow clone — safeParseJSON is LRU-cached and returns the same
|
|
288
|
+
// object reference for the same string. Mutating `result` below
|
|
289
|
+
// would poison the cache, causing stale values to persist.
|
|
290
|
+
result = { ...(parsed as JsonObject) }
|
|
291
|
+
} else {
|
|
292
|
+
logForDebugging(
|
|
293
|
+
`CLAUDE_CODE_EXTRA_BODY env var must be a JSON object, but was given ${extraBodyStr}`,
|
|
294
|
+
{ level: 'error' },
|
|
295
|
+
)
|
|
296
|
+
}
|
|
297
|
+
} catch (error) {
|
|
298
|
+
logForDebugging(
|
|
299
|
+
`Error parsing CLAUDE_CODE_EXTRA_BODY: ${errorMessage(error)}`,
|
|
300
|
+
{ level: 'error' },
|
|
301
|
+
)
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Anti-distillation: send fake_tools opt-in for 1P CLI only
|
|
306
|
+
if (
|
|
307
|
+
feature('ANTI_DISTILLATION_CC')
|
|
308
|
+
? process.env.CLAUDE_CODE_ENTRYPOINT === 'cli' &&
|
|
309
|
+
shouldIncludeFirstPartyOnlyBetas() &&
|
|
310
|
+
getFeatureValue_CACHED_MAY_BE_STALE(
|
|
311
|
+
'tengu_anti_distill_fake_tool_injection',
|
|
312
|
+
false,
|
|
313
|
+
)
|
|
314
|
+
: false
|
|
315
|
+
) {
|
|
316
|
+
result.anti_distillation = ['fake_tools']
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Handle beta headers if provided
|
|
320
|
+
if (betaHeaders && betaHeaders.length > 0) {
|
|
321
|
+
if (result.anthropic_beta && Array.isArray(result.anthropic_beta)) {
|
|
322
|
+
// Add to existing array, avoiding duplicates
|
|
323
|
+
const existingHeaders = result.anthropic_beta as string[]
|
|
324
|
+
const newHeaders = betaHeaders.filter(
|
|
325
|
+
header => !existingHeaders.includes(header),
|
|
326
|
+
)
|
|
327
|
+
result.anthropic_beta = [...existingHeaders, ...newHeaders]
|
|
328
|
+
} else {
|
|
329
|
+
// Create new array with the beta headers
|
|
330
|
+
result.anthropic_beta = betaHeaders
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return result
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export function getPromptCachingEnabled(model: string): boolean {
|
|
338
|
+
// Global disable takes precedence
|
|
339
|
+
if (isEnvTruthy(process.env.DISABLE_PROMPT_CACHING)) return false
|
|
340
|
+
|
|
341
|
+
// Check if we should disable for small/fast model
|
|
342
|
+
if (isEnvTruthy(process.env.DISABLE_PROMPT_CACHING_HAIKU)) {
|
|
343
|
+
const smallFastModel = getSmallFastModel()
|
|
344
|
+
if (model === smallFastModel) return false
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Check if we should disable for default Sonnet
|
|
348
|
+
if (isEnvTruthy(process.env.DISABLE_PROMPT_CACHING_SONNET)) {
|
|
349
|
+
const defaultSonnet = getDefaultSonnetModel()
|
|
350
|
+
if (model === defaultSonnet) return false
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Check if we should disable for default Opus
|
|
354
|
+
if (isEnvTruthy(process.env.DISABLE_PROMPT_CACHING_OPUS)) {
|
|
355
|
+
const defaultOpus = getDefaultOpusModel()
|
|
356
|
+
if (model === defaultOpus) return false
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return true
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export function getCacheControl({
|
|
363
|
+
scope,
|
|
364
|
+
querySource,
|
|
365
|
+
}: {
|
|
366
|
+
scope?: CacheScope
|
|
367
|
+
querySource?: QuerySource
|
|
368
|
+
} = {}): {
|
|
369
|
+
type: 'ephemeral'
|
|
370
|
+
ttl?: '1h'
|
|
371
|
+
scope?: CacheScope
|
|
372
|
+
} {
|
|
373
|
+
return {
|
|
374
|
+
type: 'ephemeral',
|
|
375
|
+
...(should1hCacheTTL(querySource) && { ttl: '1h' }),
|
|
376
|
+
...(scope === 'global' && { scope }),
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Determines if 1h TTL should be used for prompt caching.
|
|
382
|
+
*
|
|
383
|
+
* Only applied when:
|
|
384
|
+
* 1. User is eligible (ant or subscriber within rate limits)
|
|
385
|
+
* 2. The query source matches a pattern in the GrowthBook allowlist
|
|
386
|
+
*
|
|
387
|
+
* GrowthBook config shape: { allowlist: string[] }
|
|
388
|
+
* Patterns support trailing '*' for prefix matching.
|
|
389
|
+
* Examples:
|
|
390
|
+
* - { allowlist: ["repl_main_thread*", "sdk"] } — main thread + SDK only
|
|
391
|
+
* - { allowlist: ["repl_main_thread*", "sdk", "agent:*"] } — also subagents
|
|
392
|
+
* - { allowlist: ["*"] } — all sources
|
|
393
|
+
*
|
|
394
|
+
* The allowlist is cached in STATE for session stability — prevents mixed
|
|
395
|
+
* TTLs when GrowthBook's disk cache updates mid-request.
|
|
396
|
+
*/
|
|
397
|
+
function should1hCacheTTL(querySource?: QuerySource): boolean {
|
|
398
|
+
// 3P Bedrock users get 1h TTL when opted in via env var — they manage their own billing
|
|
399
|
+
// No GrowthBook gating needed since 3P users don't have GrowthBook configured
|
|
400
|
+
if (
|
|
401
|
+
getAPIProvider() === 'bedrock' &&
|
|
402
|
+
isEnvTruthy(process.env.ENABLE_PROMPT_CACHING_1H_BEDROCK)
|
|
403
|
+
) {
|
|
404
|
+
return true
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// Latch eligibility in bootstrap state for session stability — prevents
|
|
408
|
+
// mid-session overage flips from changing the cache_control TTL, which
|
|
409
|
+
// would bust the server-side prompt cache (~20K tokens per flip).
|
|
410
|
+
let userEligible = getPromptCache1hEligible()
|
|
411
|
+
if (userEligible === null) {
|
|
412
|
+
userEligible =
|
|
413
|
+
process.env.USER_TYPE === 'ant' ||
|
|
414
|
+
(isClaudeAISubscriber() && !currentLimits.isUsingOverage)
|
|
415
|
+
setPromptCache1hEligible(userEligible)
|
|
416
|
+
}
|
|
417
|
+
if (!userEligible) return false
|
|
418
|
+
|
|
419
|
+
// Cache allowlist in bootstrap state for session stability — prevents mixed
|
|
420
|
+
// TTLs when GrowthBook's disk cache updates mid-request
|
|
421
|
+
let allowlist = getPromptCache1hAllowlist()
|
|
422
|
+
if (allowlist === null) {
|
|
423
|
+
const config = getFeatureValue_CACHED_MAY_BE_STALE<{
|
|
424
|
+
allowlist?: string[]
|
|
425
|
+
}>('tengu_prompt_cache_1h_config', {})
|
|
426
|
+
allowlist = config.allowlist ?? []
|
|
427
|
+
setPromptCache1hAllowlist(allowlist)
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
return (
|
|
431
|
+
querySource !== undefined &&
|
|
432
|
+
allowlist.some(pattern =>
|
|
433
|
+
pattern.endsWith('*')
|
|
434
|
+
? querySource.startsWith(pattern.slice(0, -1))
|
|
435
|
+
: querySource === pattern,
|
|
436
|
+
)
|
|
437
|
+
)
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Configure effort parameters for API request.
|
|
442
|
+
*
|
|
443
|
+
*/
|
|
444
|
+
function configureEffortParams(
|
|
445
|
+
effortValue: EffortValue | undefined,
|
|
446
|
+
outputConfig: BetaOutputConfig,
|
|
447
|
+
extraBodyParams: Record<string, unknown>,
|
|
448
|
+
betas: string[],
|
|
449
|
+
model: string,
|
|
450
|
+
): void {
|
|
451
|
+
if (!modelSupportsEffort(model) || 'effort' in outputConfig) {
|
|
452
|
+
return
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (effortValue === undefined) {
|
|
456
|
+
betas.push(EFFORT_BETA_HEADER)
|
|
457
|
+
} else if (typeof effortValue === 'string') {
|
|
458
|
+
// Send string effort level as is
|
|
459
|
+
outputConfig.effort = effortValue
|
|
460
|
+
betas.push(EFFORT_BETA_HEADER)
|
|
461
|
+
} else if (process.env.USER_TYPE === 'ant') {
|
|
462
|
+
// Numeric effort override - ant-only (uses anthropic_internal)
|
|
463
|
+
const existingInternal =
|
|
464
|
+
(extraBodyParams.anthropic_internal as Record<string, unknown>) || {}
|
|
465
|
+
extraBodyParams.anthropic_internal = {
|
|
466
|
+
...existingInternal,
|
|
467
|
+
effort_override: effortValue,
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// output_config.task_budget — API-side token budget awareness for the model.
|
|
473
|
+
// Stainless SDK types don't yet include task_budget on BetaOutputConfig, so we
|
|
474
|
+
// define the wire shape locally and cast. The API validates on receipt; see
|
|
475
|
+
// api/api/schemas/messages/request/output_config.py:12-39 in the monorepo.
|
|
476
|
+
// Beta: task-budgets-2026-03-13 (EAP, claude-strudel-eap only as of Mar 2026).
|
|
477
|
+
type TaskBudgetParam = {
|
|
478
|
+
type: 'tokens'
|
|
479
|
+
total: number
|
|
480
|
+
remaining?: number
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
export function configureTaskBudgetParams(
|
|
484
|
+
taskBudget: Options['taskBudget'],
|
|
485
|
+
outputConfig: BetaOutputConfig & { task_budget?: TaskBudgetParam },
|
|
486
|
+
betas: string[],
|
|
487
|
+
): void {
|
|
488
|
+
if (
|
|
489
|
+
!taskBudget ||
|
|
490
|
+
'task_budget' in outputConfig ||
|
|
491
|
+
!shouldIncludeFirstPartyOnlyBetas()
|
|
492
|
+
) {
|
|
493
|
+
return
|
|
494
|
+
}
|
|
495
|
+
outputConfig.task_budget = {
|
|
496
|
+
type: 'tokens',
|
|
497
|
+
total: taskBudget.total,
|
|
498
|
+
...(taskBudget.remaining !== undefined && {
|
|
499
|
+
remaining: taskBudget.remaining,
|
|
500
|
+
}),
|
|
501
|
+
}
|
|
502
|
+
if (!betas.includes(TASK_BUDGETS_BETA_HEADER)) {
|
|
503
|
+
betas.push(TASK_BUDGETS_BETA_HEADER)
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
export function getAPIMetadata() {
|
|
508
|
+
// Skip Anthropic-specific metadata when using OpenAI backend
|
|
509
|
+
if (process.env.OPENAI_API_KEY) {
|
|
510
|
+
return { user_id: '' }
|
|
511
|
+
}
|
|
512
|
+
// https://docs.google.com/document/d/1dURO9ycXXQCBS0V4Vhl4poDBRgkelFc5t2BNPoEgH5Q/edit?tab=t.0#heading=h.5g7nec5b09w5
|
|
513
|
+
let extra: JsonObject = {}
|
|
514
|
+
const extraStr = process.env.CLAUDE_CODE_EXTRA_METADATA
|
|
515
|
+
if (extraStr) {
|
|
516
|
+
const parsed = safeParseJSON(extraStr, false)
|
|
517
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
518
|
+
extra = parsed as JsonObject
|
|
519
|
+
} else {
|
|
520
|
+
logForDebugging(
|
|
521
|
+
`CLAUDE_CODE_EXTRA_METADATA env var must be a JSON object, but was given ${extraStr}`,
|
|
522
|
+
{ level: 'error' },
|
|
523
|
+
)
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
return {
|
|
528
|
+
user_id: jsonStringify({
|
|
529
|
+
...extra,
|
|
530
|
+
device_id: getOrCreateUserID(),
|
|
531
|
+
// Only include OAuth account UUID when actively using OAuth authentication
|
|
532
|
+
account_uuid: getOauthAccountInfo()?.accountUuid ?? '',
|
|
533
|
+
session_id: getSessionId(),
|
|
534
|
+
}),
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
export async function verifyApiKey(
|
|
539
|
+
apiKey: string,
|
|
540
|
+
isNonInteractiveSession: boolean,
|
|
541
|
+
): Promise<boolean> {
|
|
542
|
+
// Skip API verification when using OpenAI backend
|
|
543
|
+
if (process.env.OPENAI_API_KEY) {
|
|
544
|
+
return true
|
|
545
|
+
}
|
|
546
|
+
// Skip API verification if running in print mode (isNonInteractiveSession)
|
|
547
|
+
if (isNonInteractiveSession) {
|
|
548
|
+
return true
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
try {
|
|
552
|
+
// WARNING: if you change this to use a non-Haiku model, this request will fail in 1P unless it uses getCLISyspromptPrefix.
|
|
553
|
+
const model = getSmallFastModel()
|
|
554
|
+
const betas = getModelBetas(model)
|
|
555
|
+
return await returnValue(
|
|
556
|
+
withRetry(
|
|
557
|
+
() =>
|
|
558
|
+
getAnthropicClient({
|
|
559
|
+
apiKey,
|
|
560
|
+
maxRetries: 3,
|
|
561
|
+
model,
|
|
562
|
+
source: 'verify_api_key',
|
|
563
|
+
}),
|
|
564
|
+
async anthropic => {
|
|
565
|
+
const messages: MessageParam[] = [{ role: 'user', content: 'test' }]
|
|
566
|
+
// biome-ignore lint/plugin: API key verification is intentionally a minimal direct call
|
|
567
|
+
await anthropic.beta.messages.create({
|
|
568
|
+
model,
|
|
569
|
+
max_tokens: 1,
|
|
570
|
+
messages,
|
|
571
|
+
temperature: 1,
|
|
572
|
+
...(betas.length > 0 && { betas }),
|
|
573
|
+
metadata: getAPIMetadata(),
|
|
574
|
+
...getExtraBodyParams(),
|
|
575
|
+
})
|
|
576
|
+
return true
|
|
577
|
+
},
|
|
578
|
+
{ maxRetries: 2, model, thinkingConfig: { type: 'disabled' } }, // Use fewer retries for API key verification
|
|
579
|
+
),
|
|
580
|
+
)
|
|
581
|
+
} catch (errorFromRetry) {
|
|
582
|
+
let error = errorFromRetry
|
|
583
|
+
if (errorFromRetry instanceof CannotRetryError) {
|
|
584
|
+
error = errorFromRetry.originalError
|
|
585
|
+
}
|
|
586
|
+
logError(error)
|
|
587
|
+
// Check for authentication error
|
|
588
|
+
if (
|
|
589
|
+
error instanceof Error &&
|
|
590
|
+
error.message.includes(
|
|
591
|
+
'{"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"}}',
|
|
592
|
+
)
|
|
593
|
+
) {
|
|
594
|
+
return false
|
|
595
|
+
}
|
|
596
|
+
throw error
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
export function userMessageToMessageParam(
|
|
601
|
+
message: UserMessage,
|
|
602
|
+
addCache = false,
|
|
603
|
+
enablePromptCaching: boolean,
|
|
604
|
+
querySource?: QuerySource,
|
|
605
|
+
): MessageParam {
|
|
606
|
+
if (addCache) {
|
|
607
|
+
if (typeof message.message.content === 'string') {
|
|
608
|
+
return {
|
|
609
|
+
role: 'user',
|
|
610
|
+
content: [
|
|
611
|
+
{
|
|
612
|
+
type: 'text',
|
|
613
|
+
text: message.message.content,
|
|
614
|
+
...(enablePromptCaching && {
|
|
615
|
+
cache_control: getCacheControl({ querySource }),
|
|
616
|
+
}),
|
|
617
|
+
},
|
|
618
|
+
],
|
|
619
|
+
}
|
|
620
|
+
} else {
|
|
621
|
+
return {
|
|
622
|
+
role: 'user',
|
|
623
|
+
content: message.message.content.map((_, i) => ({
|
|
624
|
+
..._,
|
|
625
|
+
...(i === message.message.content.length - 1
|
|
626
|
+
? enablePromptCaching
|
|
627
|
+
? { cache_control: getCacheControl({ querySource }) }
|
|
628
|
+
: {}
|
|
629
|
+
: {}),
|
|
630
|
+
})),
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
// Clone array content to prevent in-place mutations (e.g., insertCacheEditsBlock's
|
|
635
|
+
// splice) from contaminating the original message. Without cloning, multiple calls
|
|
636
|
+
// to addCacheBreakpoints share the same array and each splices in duplicate cache_edits.
|
|
637
|
+
return {
|
|
638
|
+
role: 'user',
|
|
639
|
+
content: Array.isArray(message.message.content)
|
|
640
|
+
? [...message.message.content]
|
|
641
|
+
: message.message.content,
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
export function assistantMessageToMessageParam(
|
|
646
|
+
message: AssistantMessage,
|
|
647
|
+
addCache = false,
|
|
648
|
+
enablePromptCaching: boolean,
|
|
649
|
+
querySource?: QuerySource,
|
|
650
|
+
): MessageParam {
|
|
651
|
+
if (addCache) {
|
|
652
|
+
if (typeof message.message.content === 'string') {
|
|
653
|
+
return {
|
|
654
|
+
role: 'assistant',
|
|
655
|
+
content: [
|
|
656
|
+
{
|
|
657
|
+
type: 'text',
|
|
658
|
+
text: message.message.content,
|
|
659
|
+
...(enablePromptCaching && {
|
|
660
|
+
cache_control: getCacheControl({ querySource }),
|
|
661
|
+
}),
|
|
662
|
+
},
|
|
663
|
+
],
|
|
664
|
+
}
|
|
665
|
+
} else {
|
|
666
|
+
return {
|
|
667
|
+
role: 'assistant',
|
|
668
|
+
content: message.message.content.map((_, i) => ({
|
|
669
|
+
..._,
|
|
670
|
+
...(i === message.message.content.length - 1 &&
|
|
671
|
+
_.type !== 'thinking' &&
|
|
672
|
+
_.type !== 'redacted_thinking' &&
|
|
673
|
+
(feature('CONNECTOR_TEXT') ? !isConnectorTextBlock(_) : true)
|
|
674
|
+
? enablePromptCaching
|
|
675
|
+
? { cache_control: getCacheControl({ querySource }) }
|
|
676
|
+
: {}
|
|
677
|
+
: {}),
|
|
678
|
+
})),
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
return {
|
|
683
|
+
role: 'assistant',
|
|
684
|
+
content: message.message.content,
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
export type Options = {
|
|
689
|
+
getToolPermissionContext: () => Promise<ToolPermissionContext>
|
|
690
|
+
model: string
|
|
691
|
+
toolChoice?: BetaToolChoiceTool | BetaToolChoiceAuto | undefined
|
|
692
|
+
isNonInteractiveSession: boolean
|
|
693
|
+
extraToolSchemas?: BetaToolUnion[]
|
|
694
|
+
maxOutputTokensOverride?: number
|
|
695
|
+
fallbackModel?: string
|
|
696
|
+
onStreamingFallback?: () => void
|
|
697
|
+
querySource: QuerySource
|
|
698
|
+
agents: AgentDefinition[]
|
|
699
|
+
allowedAgentTypes?: string[]
|
|
700
|
+
hasAppendSystemPrompt: boolean
|
|
701
|
+
fetchOverride?: ClientOptions['fetch']
|
|
702
|
+
enablePromptCaching?: boolean
|
|
703
|
+
skipCacheWrite?: boolean
|
|
704
|
+
temperatureOverride?: number
|
|
705
|
+
effortValue?: EffortValue
|
|
706
|
+
mcpTools: Tools
|
|
707
|
+
hasPendingMcpServers?: boolean
|
|
708
|
+
queryTracking?: QueryChainTracking
|
|
709
|
+
agentId?: AgentId // Only set for subagents
|
|
710
|
+
outputFormat?: BetaJSONOutputFormat
|
|
711
|
+
fastMode?: boolean
|
|
712
|
+
advisorModel?: string
|
|
713
|
+
addNotification?: (notif: Notification) => void
|
|
714
|
+
// API-side task budget (output_config.task_budget). Distinct from the
|
|
715
|
+
// tokenBudget.ts +500k auto-continue feature — this one is sent to the API
|
|
716
|
+
// so the model can pace itself. `remaining` is computed by the caller
|
|
717
|
+
// (query.ts decrements across the agentic loop).
|
|
718
|
+
taskBudget?: { total: number; remaining?: number }
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
export async function queryModelWithoutStreaming({
|
|
722
|
+
messages,
|
|
723
|
+
systemPrompt,
|
|
724
|
+
thinkingConfig,
|
|
725
|
+
tools,
|
|
726
|
+
signal,
|
|
727
|
+
options,
|
|
728
|
+
}: {
|
|
729
|
+
messages: Message[]
|
|
730
|
+
systemPrompt: SystemPrompt
|
|
731
|
+
thinkingConfig: ThinkingConfig
|
|
732
|
+
tools: Tools
|
|
733
|
+
signal: AbortSignal
|
|
734
|
+
options: Options
|
|
735
|
+
}): Promise<AssistantMessage> {
|
|
736
|
+
// Store the assistant message but continue consuming the generator to ensure
|
|
737
|
+
// logAPISuccessAndDuration gets called (which happens after all yields)
|
|
738
|
+
let assistantMessage: AssistantMessage | undefined
|
|
739
|
+
for await (const message of withStreamingVCR(messages, async function* () {
|
|
740
|
+
yield* queryModel(
|
|
741
|
+
messages,
|
|
742
|
+
systemPrompt,
|
|
743
|
+
thinkingConfig,
|
|
744
|
+
tools,
|
|
745
|
+
signal,
|
|
746
|
+
options,
|
|
747
|
+
)
|
|
748
|
+
})) {
|
|
749
|
+
if (message.type === 'assistant') {
|
|
750
|
+
assistantMessage = message
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
if (!assistantMessage) {
|
|
754
|
+
// If the signal was aborted, throw APIUserAbortError instead of a generic error
|
|
755
|
+
// This allows callers to handle abort scenarios gracefully
|
|
756
|
+
if (signal.aborted) {
|
|
757
|
+
throw new APIUserAbortError()
|
|
758
|
+
}
|
|
759
|
+
throw new Error('No assistant message found')
|
|
760
|
+
}
|
|
761
|
+
return assistantMessage
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
export async function* queryModelWithStreaming({
|
|
765
|
+
messages,
|
|
766
|
+
systemPrompt,
|
|
767
|
+
thinkingConfig,
|
|
768
|
+
tools,
|
|
769
|
+
signal,
|
|
770
|
+
options,
|
|
771
|
+
}: {
|
|
772
|
+
messages: Message[]
|
|
773
|
+
systemPrompt: SystemPrompt
|
|
774
|
+
thinkingConfig: ThinkingConfig
|
|
775
|
+
tools: Tools
|
|
776
|
+
signal: AbortSignal
|
|
777
|
+
options: Options
|
|
778
|
+
}): AsyncGenerator<
|
|
779
|
+
StreamEvent | AssistantMessage | SystemAPIErrorMessage,
|
|
780
|
+
void
|
|
781
|
+
> {
|
|
782
|
+
return yield* withStreamingVCR(messages, async function* () {
|
|
783
|
+
yield* queryModel(
|
|
784
|
+
messages,
|
|
785
|
+
systemPrompt,
|
|
786
|
+
thinkingConfig,
|
|
787
|
+
tools,
|
|
788
|
+
signal,
|
|
789
|
+
options,
|
|
790
|
+
)
|
|
791
|
+
})
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/**
|
|
795
|
+
* Determines if an LSP tool should be deferred (tool appears with defer_loading: true)
|
|
796
|
+
* because LSP initialization is not yet complete.
|
|
797
|
+
*/
|
|
798
|
+
function shouldDeferLspTool(tool: Tool): boolean {
|
|
799
|
+
if (!('isLsp' in tool) || !tool.isLsp) {
|
|
800
|
+
return false
|
|
801
|
+
}
|
|
802
|
+
const status = getInitializationStatus()
|
|
803
|
+
// Defer when pending or not started
|
|
804
|
+
return status.status === 'pending' || status.status === 'not-started'
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* Per-attempt timeout for non-streaming fallback requests, in milliseconds.
|
|
809
|
+
* Reads API_TIMEOUT_MS when set so slow backends and the streaming path
|
|
810
|
+
* share the same ceiling.
|
|
811
|
+
*
|
|
812
|
+
* Remote sessions default to 120s to stay under CCR's container idle-kill
|
|
813
|
+
* (~5min) so a hung fallback to a wedged backend surfaces a clean
|
|
814
|
+
* APIConnectionTimeoutError instead of stalling past SIGKILL.
|
|
815
|
+
*
|
|
816
|
+
* Otherwise defaults to 300s — long enough for slow backends without
|
|
817
|
+
* approaching the API's 10-minute non-streaming boundary.
|
|
818
|
+
*/
|
|
819
|
+
function getNonstreamingFallbackTimeoutMs(): number {
|
|
820
|
+
const override = parseInt(process.env.API_TIMEOUT_MS || '', 10)
|
|
821
|
+
if (override) return override
|
|
822
|
+
return isEnvTruthy(process.env.CLAUDE_CODE_REMOTE) ? 120_000 : 300_000
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* Helper generator for non-streaming API requests.
|
|
827
|
+
* Encapsulates the common pattern of creating a withRetry generator,
|
|
828
|
+
* iterating to yield system messages, and returning the final BetaMessage.
|
|
829
|
+
*/
|
|
830
|
+
export async function* executeNonStreamingRequest(
|
|
831
|
+
clientOptions: {
|
|
832
|
+
model: string
|
|
833
|
+
fetchOverride?: Options['fetchOverride']
|
|
834
|
+
source: string
|
|
835
|
+
},
|
|
836
|
+
retryOptions: {
|
|
837
|
+
model: string
|
|
838
|
+
fallbackModel?: string
|
|
839
|
+
thinkingConfig: ThinkingConfig
|
|
840
|
+
fastMode?: boolean
|
|
841
|
+
signal: AbortSignal
|
|
842
|
+
initialConsecutive529Errors?: number
|
|
843
|
+
querySource?: QuerySource
|
|
844
|
+
},
|
|
845
|
+
paramsFromContext: (context: RetryContext) => BetaMessageStreamParams,
|
|
846
|
+
onAttempt: (attempt: number, start: number, maxOutputTokens: number) => void,
|
|
847
|
+
captureRequest: (params: BetaMessageStreamParams) => void,
|
|
848
|
+
/**
|
|
849
|
+
* Request ID of the failed streaming attempt this fallback is recovering
|
|
850
|
+
* from. Emitted in tengu_nonstreaming_fallback_error for funnel correlation.
|
|
851
|
+
*/
|
|
852
|
+
originatingRequestId?: string | null,
|
|
853
|
+
): AsyncGenerator<SystemAPIErrorMessage, BetaMessage> {
|
|
854
|
+
const fallbackTimeoutMs = getNonstreamingFallbackTimeoutMs()
|
|
855
|
+
const generator = withRetry(
|
|
856
|
+
() =>
|
|
857
|
+
getAnthropicClient({
|
|
858
|
+
maxRetries: 0,
|
|
859
|
+
model: clientOptions.model,
|
|
860
|
+
fetchOverride: clientOptions.fetchOverride,
|
|
861
|
+
source: clientOptions.source,
|
|
862
|
+
}),
|
|
863
|
+
async (anthropic, attempt, context) => {
|
|
864
|
+
const start = Date.now()
|
|
865
|
+
const retryParams = paramsFromContext(context)
|
|
866
|
+
captureRequest(retryParams)
|
|
867
|
+
onAttempt(attempt, start, retryParams.max_tokens)
|
|
868
|
+
|
|
869
|
+
const adjustedParams = adjustParamsForNonStreaming(
|
|
870
|
+
retryParams,
|
|
871
|
+
MAX_NON_STREAMING_TOKENS,
|
|
872
|
+
)
|
|
873
|
+
|
|
874
|
+
try {
|
|
875
|
+
// biome-ignore lint/plugin: non-streaming API call
|
|
876
|
+
return await anthropic.beta.messages.create(
|
|
877
|
+
{
|
|
878
|
+
...adjustedParams,
|
|
879
|
+
model: normalizeModelStringForAPI(adjustedParams.model),
|
|
880
|
+
},
|
|
881
|
+
{
|
|
882
|
+
signal: retryOptions.signal,
|
|
883
|
+
timeout: fallbackTimeoutMs,
|
|
884
|
+
},
|
|
885
|
+
)
|
|
886
|
+
} catch (err) {
|
|
887
|
+
// User aborts are not errors — re-throw immediately without logging
|
|
888
|
+
if (err instanceof APIUserAbortError) throw err
|
|
889
|
+
|
|
890
|
+
// Instrumentation: record when the non-streaming request errors (including
|
|
891
|
+
// timeouts). Lets us distinguish "fallback hung past container kill"
|
|
892
|
+
// (no event) from "fallback hit the bounded timeout" (this event).
|
|
893
|
+
logForDiagnosticsNoPII('error', 'cli_nonstreaming_fallback_error')
|
|
894
|
+
logEvent('tengu_nonstreaming_fallback_error', {
|
|
895
|
+
model:
|
|
896
|
+
clientOptions.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
897
|
+
error:
|
|
898
|
+
err instanceof Error
|
|
899
|
+
? (err.name as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
|
|
900
|
+
: ('unknown' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS),
|
|
901
|
+
attempt,
|
|
902
|
+
timeout_ms: fallbackTimeoutMs,
|
|
903
|
+
request_id: (originatingRequestId ??
|
|
904
|
+
'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
905
|
+
})
|
|
906
|
+
throw err
|
|
907
|
+
}
|
|
908
|
+
},
|
|
909
|
+
{
|
|
910
|
+
model: retryOptions.model,
|
|
911
|
+
fallbackModel: retryOptions.fallbackModel,
|
|
912
|
+
thinkingConfig: retryOptions.thinkingConfig,
|
|
913
|
+
...(isFastModeEnabled() && { fastMode: retryOptions.fastMode }),
|
|
914
|
+
signal: retryOptions.signal,
|
|
915
|
+
initialConsecutive529Errors: retryOptions.initialConsecutive529Errors,
|
|
916
|
+
querySource: retryOptions.querySource,
|
|
917
|
+
},
|
|
918
|
+
)
|
|
919
|
+
|
|
920
|
+
let e
|
|
921
|
+
do {
|
|
922
|
+
e = await generator.next()
|
|
923
|
+
if (!e.done && e.value.type === 'system') {
|
|
924
|
+
yield e.value
|
|
925
|
+
}
|
|
926
|
+
} while (!e.done)
|
|
927
|
+
|
|
928
|
+
return e.value as BetaMessage
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
/**
|
|
932
|
+
* Extracts the request ID from the most recent assistant message in the
|
|
933
|
+
* conversation. Used to link consecutive API requests in analytics so we can
|
|
934
|
+
* join them for cache-hit-rate analysis and incremental token tracking.
|
|
935
|
+
*
|
|
936
|
+
* Deriving this from the message array (rather than global state) ensures each
|
|
937
|
+
* query chain (main thread, subagent, teammate) tracks its own request chain
|
|
938
|
+
* independently, and rollback/undo naturally updates the value.
|
|
939
|
+
*/
|
|
940
|
+
function getPreviousRequestIdFromMessages(
|
|
941
|
+
messages: Message[],
|
|
942
|
+
): string | undefined {
|
|
943
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
944
|
+
const msg = messages[i]!
|
|
945
|
+
if (msg.type === 'assistant' && msg.requestId) {
|
|
946
|
+
return msg.requestId
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
return undefined
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
function isMedia(
|
|
953
|
+
block: BetaContentBlockParam,
|
|
954
|
+
): block is BetaImageBlockParam | BetaRequestDocumentBlock {
|
|
955
|
+
return block.type === 'image' || block.type === 'document'
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
function isToolResult(
|
|
959
|
+
block: BetaContentBlockParam,
|
|
960
|
+
): block is BetaToolResultBlockParam {
|
|
961
|
+
return block.type === 'tool_result'
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* Ensures messages contain at most `limit` media items (images + documents).
|
|
966
|
+
* Strips oldest media first to preserve the most recent.
|
|
967
|
+
*/
|
|
968
|
+
export function stripExcessMediaItems(
|
|
969
|
+
messages: (UserMessage | AssistantMessage)[],
|
|
970
|
+
limit: number,
|
|
971
|
+
): (UserMessage | AssistantMessage)[] {
|
|
972
|
+
let toRemove = 0
|
|
973
|
+
for (const msg of messages) {
|
|
974
|
+
if (!Array.isArray(msg.message.content)) continue
|
|
975
|
+
for (const block of msg.message.content) {
|
|
976
|
+
if (isMedia(block)) toRemove++
|
|
977
|
+
if (isToolResult(block) && Array.isArray(block.content)) {
|
|
978
|
+
for (const nested of block.content) {
|
|
979
|
+
if (isMedia(nested)) toRemove++
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
toRemove -= limit
|
|
985
|
+
if (toRemove <= 0) return messages
|
|
986
|
+
|
|
987
|
+
return messages.map(msg => {
|
|
988
|
+
if (toRemove <= 0) return msg
|
|
989
|
+
const content = msg.message.content
|
|
990
|
+
if (!Array.isArray(content)) return msg
|
|
991
|
+
|
|
992
|
+
const before = toRemove
|
|
993
|
+
const stripped = content
|
|
994
|
+
.map(block => {
|
|
995
|
+
if (
|
|
996
|
+
toRemove <= 0 ||
|
|
997
|
+
!isToolResult(block) ||
|
|
998
|
+
!Array.isArray(block.content)
|
|
999
|
+
)
|
|
1000
|
+
return block
|
|
1001
|
+
const filtered = block.content.filter(n => {
|
|
1002
|
+
if (toRemove > 0 && isMedia(n)) {
|
|
1003
|
+
toRemove--
|
|
1004
|
+
return false
|
|
1005
|
+
}
|
|
1006
|
+
return true
|
|
1007
|
+
})
|
|
1008
|
+
return filtered.length === block.content.length
|
|
1009
|
+
? block
|
|
1010
|
+
: { ...block, content: filtered }
|
|
1011
|
+
})
|
|
1012
|
+
.filter(block => {
|
|
1013
|
+
if (toRemove > 0 && isMedia(block)) {
|
|
1014
|
+
toRemove--
|
|
1015
|
+
return false
|
|
1016
|
+
}
|
|
1017
|
+
return true
|
|
1018
|
+
})
|
|
1019
|
+
|
|
1020
|
+
return before === toRemove
|
|
1021
|
+
? msg
|
|
1022
|
+
: {
|
|
1023
|
+
...msg,
|
|
1024
|
+
message: { ...msg.message, content: stripped },
|
|
1025
|
+
}
|
|
1026
|
+
}) as (UserMessage | AssistantMessage)[]
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
async function* queryModel(
|
|
1030
|
+
messages: Message[],
|
|
1031
|
+
systemPrompt: SystemPrompt,
|
|
1032
|
+
thinkingConfig: ThinkingConfig,
|
|
1033
|
+
tools: Tools,
|
|
1034
|
+
signal: AbortSignal,
|
|
1035
|
+
options: Options,
|
|
1036
|
+
): AsyncGenerator<
|
|
1037
|
+
StreamEvent | AssistantMessage | SystemAPIErrorMessage,
|
|
1038
|
+
void
|
|
1039
|
+
> {
|
|
1040
|
+
// Check cheap conditions first — the off-switch await blocks on GrowthBook
|
|
1041
|
+
// init (~10ms). For non-Opus models (haiku, sonnet) this skips the await
|
|
1042
|
+
// entirely. Subscribers don't hit this path at all.
|
|
1043
|
+
// Skip entirely for OpenAI backend — no Anthropic off-switch applies.
|
|
1044
|
+
if (
|
|
1045
|
+
!process.env.OPENAI_API_KEY &&
|
|
1046
|
+
!isClaudeAISubscriber() &&
|
|
1047
|
+
isNonCustomOpusModel(options.model) &&
|
|
1048
|
+
(
|
|
1049
|
+
await getDynamicConfig_BLOCKS_ON_INIT<{ activated: boolean }>(
|
|
1050
|
+
'tengu-off-switch',
|
|
1051
|
+
{
|
|
1052
|
+
activated: false,
|
|
1053
|
+
},
|
|
1054
|
+
)
|
|
1055
|
+
).activated
|
|
1056
|
+
) {
|
|
1057
|
+
logEvent('tengu_off_switch_query', {})
|
|
1058
|
+
yield getAssistantMessageFromError(
|
|
1059
|
+
new Error(CUSTOM_OFF_SWITCH_MESSAGE),
|
|
1060
|
+
options.model,
|
|
1061
|
+
)
|
|
1062
|
+
return
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
// Derive previous request ID from the last assistant message in this query chain.
|
|
1066
|
+
// This is scoped per message array (main thread, subagent, teammate each have their own),
|
|
1067
|
+
// so concurrent agents don't clobber each other's request chain tracking.
|
|
1068
|
+
// Also naturally handles rollback/undo since removed messages won't be in the array.
|
|
1069
|
+
const previousRequestId = getPreviousRequestIdFromMessages(messages)
|
|
1070
|
+
|
|
1071
|
+
const resolvedModel =
|
|
1072
|
+
getAPIProvider() === 'bedrock' &&
|
|
1073
|
+
options.model.includes('application-inference-profile')
|
|
1074
|
+
? ((await getInferenceProfileBackingModel(options.model)) ??
|
|
1075
|
+
options.model)
|
|
1076
|
+
: options.model
|
|
1077
|
+
|
|
1078
|
+
queryCheckpoint('query_tool_schema_build_start')
|
|
1079
|
+
const isAgenticQuery =
|
|
1080
|
+
options.querySource.startsWith('repl_main_thread') ||
|
|
1081
|
+
options.querySource.startsWith('agent:') ||
|
|
1082
|
+
options.querySource === 'sdk' ||
|
|
1083
|
+
options.querySource === 'hook_agent' ||
|
|
1084
|
+
options.querySource === 'verification_agent'
|
|
1085
|
+
const betas = getMergedBetas(options.model, { isAgenticQuery })
|
|
1086
|
+
|
|
1087
|
+
// Always send the advisor beta header when advisor is enabled, so
|
|
1088
|
+
// non-agentic queries (compact, side_question, extract_memories, etc.)
|
|
1089
|
+
// can parse advisor server_tool_use blocks already in the conversation history.
|
|
1090
|
+
if (isAdvisorEnabled()) {
|
|
1091
|
+
betas.push(ADVISOR_BETA_HEADER)
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
let advisorModel: string | undefined
|
|
1095
|
+
if (isAgenticQuery && isAdvisorEnabled()) {
|
|
1096
|
+
let advisorOption = options.advisorModel
|
|
1097
|
+
|
|
1098
|
+
const advisorExperiment = getExperimentAdvisorModels()
|
|
1099
|
+
if (advisorExperiment !== undefined) {
|
|
1100
|
+
if (
|
|
1101
|
+
normalizeModelStringForAPI(advisorExperiment.baseModel) ===
|
|
1102
|
+
normalizeModelStringForAPI(options.model)
|
|
1103
|
+
) {
|
|
1104
|
+
// Override the advisor model if the base model matches. We
|
|
1105
|
+
// should only have experiment models if the user cannot
|
|
1106
|
+
// configure it themselves.
|
|
1107
|
+
advisorOption = advisorExperiment.advisorModel
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
if (advisorOption) {
|
|
1112
|
+
const normalizedAdvisorModel = normalizeModelStringForAPI(
|
|
1113
|
+
parseUserSpecifiedModel(advisorOption),
|
|
1114
|
+
)
|
|
1115
|
+
if (!modelSupportsAdvisor(options.model)) {
|
|
1116
|
+
logForDebugging(
|
|
1117
|
+
`[AdvisorTool] Skipping advisor - base model ${options.model} does not support advisor`,
|
|
1118
|
+
)
|
|
1119
|
+
} else if (!isValidAdvisorModel(normalizedAdvisorModel)) {
|
|
1120
|
+
logForDebugging(
|
|
1121
|
+
`[AdvisorTool] Skipping advisor - ${normalizedAdvisorModel} is not a valid advisor model`,
|
|
1122
|
+
)
|
|
1123
|
+
} else {
|
|
1124
|
+
advisorModel = normalizedAdvisorModel
|
|
1125
|
+
logForDebugging(
|
|
1126
|
+
`[AdvisorTool] Server-side tool enabled with ${advisorModel} as the advisor model`,
|
|
1127
|
+
)
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
// Check if tool search is enabled (checks mode, model support, and threshold for auto mode)
|
|
1133
|
+
// This is async because it may need to calculate MCP tool description sizes for TstAuto mode
|
|
1134
|
+
let useToolSearch = await isToolSearchEnabled(
|
|
1135
|
+
options.model,
|
|
1136
|
+
tools,
|
|
1137
|
+
options.getToolPermissionContext,
|
|
1138
|
+
options.agents,
|
|
1139
|
+
'query',
|
|
1140
|
+
)
|
|
1141
|
+
|
|
1142
|
+
// Precompute once — isDeferredTool does 2 GrowthBook lookups per call
|
|
1143
|
+
const deferredToolNames = new Set<string>()
|
|
1144
|
+
if (useToolSearch) {
|
|
1145
|
+
for (const t of tools) {
|
|
1146
|
+
if (isDeferredTool(t)) deferredToolNames.add(t.name)
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
// Even if tool search mode is enabled, skip if there are no deferred tools
|
|
1151
|
+
// AND no MCP servers are still connecting. When servers are pending, keep
|
|
1152
|
+
// ToolSearch available so the model can discover tools after they connect.
|
|
1153
|
+
if (
|
|
1154
|
+
useToolSearch &&
|
|
1155
|
+
deferredToolNames.size === 0 &&
|
|
1156
|
+
!options.hasPendingMcpServers
|
|
1157
|
+
) {
|
|
1158
|
+
logForDebugging(
|
|
1159
|
+
'Tool search disabled: no deferred tools available to search',
|
|
1160
|
+
)
|
|
1161
|
+
useToolSearch = false
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
// Filter out ToolSearchTool if tool search is not enabled for this model
|
|
1165
|
+
// ToolSearchTool returns tool_reference blocks which unsupported models can't handle
|
|
1166
|
+
let filteredTools: Tools
|
|
1167
|
+
|
|
1168
|
+
if (useToolSearch) {
|
|
1169
|
+
// Dynamic tool loading: Only include deferred tools that have been discovered
|
|
1170
|
+
// via tool_reference blocks in the message history. This eliminates the need
|
|
1171
|
+
// to predeclare all deferred tools upfront and removes limits on tool quantity.
|
|
1172
|
+
const discoveredToolNames = extractDiscoveredToolNames(messages)
|
|
1173
|
+
|
|
1174
|
+
filteredTools = tools.filter(tool => {
|
|
1175
|
+
// Always include non-deferred tools
|
|
1176
|
+
if (!deferredToolNames.has(tool.name)) return true
|
|
1177
|
+
// Always include ToolSearchTool (so it can discover more tools)
|
|
1178
|
+
if (toolMatchesName(tool, TOOL_SEARCH_TOOL_NAME)) return true
|
|
1179
|
+
// Only include deferred tools that have been discovered
|
|
1180
|
+
return discoveredToolNames.has(tool.name)
|
|
1181
|
+
})
|
|
1182
|
+
} else {
|
|
1183
|
+
filteredTools = tools.filter(
|
|
1184
|
+
t => !toolMatchesName(t, TOOL_SEARCH_TOOL_NAME),
|
|
1185
|
+
)
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
// Add tool search beta header if enabled - required for defer_loading to be accepted
|
|
1189
|
+
// Header differs by provider: 1P/Foundry use advanced-tool-use, Vertex/Bedrock use tool-search-tool
|
|
1190
|
+
// For Bedrock, this header must go in extraBodyParams, not the betas array
|
|
1191
|
+
const toolSearchHeader = useToolSearch ? getToolSearchBetaHeader() : null
|
|
1192
|
+
if (toolSearchHeader && getAPIProvider() !== 'bedrock') {
|
|
1193
|
+
if (!betas.includes(toolSearchHeader)) {
|
|
1194
|
+
betas.push(toolSearchHeader)
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
// Determine if cached microcompact is enabled for this model.
|
|
1199
|
+
// Computed once here (in async context) and captured by paramsFromContext.
|
|
1200
|
+
// The beta header is also captured here to avoid a top-level import of the
|
|
1201
|
+
// ant-only CACHE_EDITING_BETA_HEADER constant.
|
|
1202
|
+
let cachedMCEnabled = false
|
|
1203
|
+
let cacheEditingBetaHeader = ''
|
|
1204
|
+
if (feature('CACHED_MICROCOMPACT')) {
|
|
1205
|
+
const {
|
|
1206
|
+
isCachedMicrocompactEnabled,
|
|
1207
|
+
isModelSupportedForCacheEditing,
|
|
1208
|
+
getCachedMCConfig,
|
|
1209
|
+
} = await import('../compact/cachedMicrocompact.js')
|
|
1210
|
+
const betas = await import('src/constants/betas.js')
|
|
1211
|
+
cacheEditingBetaHeader = betas.CACHE_EDITING_BETA_HEADER
|
|
1212
|
+
const featureEnabled = isCachedMicrocompactEnabled()
|
|
1213
|
+
const modelSupported = isModelSupportedForCacheEditing(options.model)
|
|
1214
|
+
cachedMCEnabled = featureEnabled && modelSupported
|
|
1215
|
+
const config = getCachedMCConfig()
|
|
1216
|
+
logForDebugging(
|
|
1217
|
+
`Cached MC gate: enabled=${featureEnabled} modelSupported=${modelSupported} model=${options.model} supportedModels=${jsonStringify(config.supportedModels)}`,
|
|
1218
|
+
)
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
const useGlobalCacheFeature = shouldUseGlobalCacheScope()
|
|
1222
|
+
const willDefer = (t: Tool) =>
|
|
1223
|
+
useToolSearch && (deferredToolNames.has(t.name) || shouldDeferLspTool(t))
|
|
1224
|
+
// MCP tools are per-user → dynamic tool section → can't globally cache.
|
|
1225
|
+
// Only gate when an MCP tool will actually render (not defer_loading).
|
|
1226
|
+
const needsToolBasedCacheMarker =
|
|
1227
|
+
useGlobalCacheFeature &&
|
|
1228
|
+
filteredTools.some(t => t.isMcp === true && !willDefer(t))
|
|
1229
|
+
|
|
1230
|
+
// Ensure prompt_caching_scope beta header is present when global cache is enabled.
|
|
1231
|
+
if (
|
|
1232
|
+
useGlobalCacheFeature &&
|
|
1233
|
+
!betas.includes(PROMPT_CACHING_SCOPE_BETA_HEADER)
|
|
1234
|
+
) {
|
|
1235
|
+
betas.push(PROMPT_CACHING_SCOPE_BETA_HEADER)
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
// Determine global cache strategy for logging
|
|
1239
|
+
const globalCacheStrategy: GlobalCacheStrategy = useGlobalCacheFeature
|
|
1240
|
+
? needsToolBasedCacheMarker
|
|
1241
|
+
? 'none'
|
|
1242
|
+
: 'system_prompt'
|
|
1243
|
+
: 'none'
|
|
1244
|
+
|
|
1245
|
+
// Build tool schemas, adding defer_loading for MCP tools when tool search is enabled
|
|
1246
|
+
// Note: We pass the full `tools` list (not filteredTools) to toolToAPISchema so that
|
|
1247
|
+
// ToolSearchTool's prompt can list ALL available MCP tools. The filtering only affects
|
|
1248
|
+
// which tools are actually sent to the API, not what the model sees in tool descriptions.
|
|
1249
|
+
const toolSchemas = await Promise.all(
|
|
1250
|
+
filteredTools.map(tool =>
|
|
1251
|
+
toolToAPISchema(tool, {
|
|
1252
|
+
getToolPermissionContext: options.getToolPermissionContext,
|
|
1253
|
+
tools,
|
|
1254
|
+
agents: options.agents,
|
|
1255
|
+
allowedAgentTypes: options.allowedAgentTypes,
|
|
1256
|
+
model: options.model,
|
|
1257
|
+
deferLoading: willDefer(tool),
|
|
1258
|
+
}),
|
|
1259
|
+
),
|
|
1260
|
+
)
|
|
1261
|
+
|
|
1262
|
+
if (useToolSearch) {
|
|
1263
|
+
const includedDeferredTools = count(filteredTools, t =>
|
|
1264
|
+
deferredToolNames.has(t.name),
|
|
1265
|
+
)
|
|
1266
|
+
logForDebugging(
|
|
1267
|
+
`Dynamic tool loading: ${includedDeferredTools}/${deferredToolNames.size} deferred tools included`,
|
|
1268
|
+
)
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
queryCheckpoint('query_tool_schema_build_end')
|
|
1272
|
+
|
|
1273
|
+
// Normalize messages before building system prompt (needed for fingerprinting)
|
|
1274
|
+
// Instrumentation: Track message count before normalization
|
|
1275
|
+
logEvent('tengu_api_before_normalize', {
|
|
1276
|
+
preNormalizedMessageCount: messages.length,
|
|
1277
|
+
})
|
|
1278
|
+
|
|
1279
|
+
queryCheckpoint('query_message_normalization_start')
|
|
1280
|
+
let messagesForAPI = normalizeMessagesForAPI(messages, filteredTools)
|
|
1281
|
+
queryCheckpoint('query_message_normalization_end')
|
|
1282
|
+
|
|
1283
|
+
// Model-specific post-processing: strip tool-search-specific fields if the
|
|
1284
|
+
// selected model doesn't support tool search.
|
|
1285
|
+
//
|
|
1286
|
+
// Why is this needed in addition to normalizeMessagesForAPI?
|
|
1287
|
+
// - normalizeMessagesForAPI uses isToolSearchEnabledNoModelCheck() because it's
|
|
1288
|
+
// called from ~20 places (analytics, feedback, sharing, etc.), many of which
|
|
1289
|
+
// don't have model context. Adding model to its signature would be a large refactor.
|
|
1290
|
+
// - This post-processing uses the model-aware isToolSearchEnabled() check
|
|
1291
|
+
// - This handles mid-conversation model switching (e.g., Sonnet → Haiku) where
|
|
1292
|
+
// stale tool-search fields from the previous model would cause 400 errors
|
|
1293
|
+
//
|
|
1294
|
+
// Note: For assistant messages, normalizeMessagesForAPI already normalized the
|
|
1295
|
+
// tool inputs, so stripCallerFieldFromAssistantMessage only needs to remove the
|
|
1296
|
+
// 'caller' field (not re-normalize inputs).
|
|
1297
|
+
if (!useToolSearch) {
|
|
1298
|
+
messagesForAPI = messagesForAPI.map(msg => {
|
|
1299
|
+
switch (msg.type) {
|
|
1300
|
+
case 'user':
|
|
1301
|
+
// Strip tool_reference blocks from tool_result content
|
|
1302
|
+
return stripToolReferenceBlocksFromUserMessage(msg)
|
|
1303
|
+
case 'assistant':
|
|
1304
|
+
// Strip 'caller' field from tool_use blocks
|
|
1305
|
+
return stripCallerFieldFromAssistantMessage(msg)
|
|
1306
|
+
default:
|
|
1307
|
+
return msg
|
|
1308
|
+
}
|
|
1309
|
+
})
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
// Repair tool_use/tool_result pairing mismatches that can occur when resuming
|
|
1313
|
+
// remote/teleport sessions. Inserts synthetic error tool_results for orphaned
|
|
1314
|
+
// tool_uses and strips orphaned tool_results referencing non-existent tool_uses.
|
|
1315
|
+
messagesForAPI = ensureToolResultPairing(messagesForAPI)
|
|
1316
|
+
|
|
1317
|
+
// Strip advisor blocks — the API rejects them without the beta header.
|
|
1318
|
+
if (!betas.includes(ADVISOR_BETA_HEADER)) {
|
|
1319
|
+
messagesForAPI = stripAdvisorBlocks(messagesForAPI)
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
// Strip excess media items before making the API call.
|
|
1323
|
+
// The API rejects requests with >100 media items but returns a confusing error.
|
|
1324
|
+
// Rather than erroring (which is hard to recover from in Cowork/CCD), we
|
|
1325
|
+
// silently drop the oldest media items to stay within the limit.
|
|
1326
|
+
messagesForAPI = stripExcessMediaItems(
|
|
1327
|
+
messagesForAPI,
|
|
1328
|
+
API_MAX_MEDIA_PER_REQUEST,
|
|
1329
|
+
)
|
|
1330
|
+
|
|
1331
|
+
// Instrumentation: Track message count after normalization
|
|
1332
|
+
logEvent('tengu_api_after_normalize', {
|
|
1333
|
+
postNormalizedMessageCount: messagesForAPI.length,
|
|
1334
|
+
})
|
|
1335
|
+
|
|
1336
|
+
// Compute fingerprint from first user message for attribution.
|
|
1337
|
+
// Must run BEFORE injecting synthetic messages (e.g. deferred tool names)
|
|
1338
|
+
// so the fingerprint reflects the actual user input.
|
|
1339
|
+
const fingerprint = computeFingerprintFromMessages(messagesForAPI)
|
|
1340
|
+
|
|
1341
|
+
// When the delta attachment is enabled, deferred tools are announced
|
|
1342
|
+
// via persisted deferred_tools_delta attachments instead of this
|
|
1343
|
+
// ephemeral prepend (which busts cache whenever the pool changes).
|
|
1344
|
+
if (useToolSearch && !isDeferredToolsDeltaEnabled()) {
|
|
1345
|
+
const deferredToolList = tools
|
|
1346
|
+
.filter(t => deferredToolNames.has(t.name))
|
|
1347
|
+
.map(formatDeferredToolLine)
|
|
1348
|
+
.sort()
|
|
1349
|
+
.join('\n')
|
|
1350
|
+
if (deferredToolList) {
|
|
1351
|
+
messagesForAPI = [
|
|
1352
|
+
createUserMessage({
|
|
1353
|
+
content: `<available-deferred-tools>\n${deferredToolList}\n</available-deferred-tools>`,
|
|
1354
|
+
isMeta: true,
|
|
1355
|
+
}),
|
|
1356
|
+
...messagesForAPI,
|
|
1357
|
+
]
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
// Chrome tool-search instructions: when the delta attachment is enabled,
|
|
1362
|
+
// these are carried as a client-side block in mcp_instructions_delta
|
|
1363
|
+
// (attachments.ts) instead of here. This per-request sys-prompt append
|
|
1364
|
+
// busts the prompt cache when chrome connects late.
|
|
1365
|
+
const hasChromeTools = filteredTools.some(t =>
|
|
1366
|
+
isToolFromMcpServer(t.name, CLAUDE_IN_CHROME_MCP_SERVER_NAME),
|
|
1367
|
+
)
|
|
1368
|
+
const injectChromeHere =
|
|
1369
|
+
useToolSearch && hasChromeTools && !isMcpInstructionsDeltaEnabled()
|
|
1370
|
+
|
|
1371
|
+
// filter(Boolean) works by converting each element to a boolean - empty strings become false and are filtered out.
|
|
1372
|
+
systemPrompt = asSystemPrompt(
|
|
1373
|
+
[
|
|
1374
|
+
getAttributionHeader(fingerprint),
|
|
1375
|
+
getCLISyspromptPrefix({
|
|
1376
|
+
isNonInteractive: options.isNonInteractiveSession,
|
|
1377
|
+
hasAppendSystemPrompt: options.hasAppendSystemPrompt,
|
|
1378
|
+
}),
|
|
1379
|
+
...systemPrompt,
|
|
1380
|
+
...(advisorModel ? [ADVISOR_TOOL_INSTRUCTIONS] : []),
|
|
1381
|
+
...(injectChromeHere ? [CHROME_TOOL_SEARCH_INSTRUCTIONS] : []),
|
|
1382
|
+
].filter(Boolean),
|
|
1383
|
+
)
|
|
1384
|
+
|
|
1385
|
+
// Prepend system prompt block for easy API identification
|
|
1386
|
+
logAPIPrefix(systemPrompt)
|
|
1387
|
+
|
|
1388
|
+
const enablePromptCaching =
|
|
1389
|
+
options.enablePromptCaching ?? getPromptCachingEnabled(options.model)
|
|
1390
|
+
const system = buildSystemPromptBlocks(systemPrompt, enablePromptCaching, {
|
|
1391
|
+
skipGlobalCacheForSystemPrompt: needsToolBasedCacheMarker,
|
|
1392
|
+
querySource: options.querySource,
|
|
1393
|
+
})
|
|
1394
|
+
const useBetas = betas.length > 0
|
|
1395
|
+
|
|
1396
|
+
// Build minimal context for detailed tracing (when beta tracing is enabled)
|
|
1397
|
+
// Note: The actual new_context message extraction is done in sessionTracing.ts using
|
|
1398
|
+
// hash-based tracking per querySource (agent) from the messagesForAPI array
|
|
1399
|
+
const extraToolSchemas = [...(options.extraToolSchemas ?? [])]
|
|
1400
|
+
if (advisorModel) {
|
|
1401
|
+
// Server tools must be in the tools array by API contract. Appended after
|
|
1402
|
+
// toolSchemas (which carries the cache_control marker) so toggling /advisor
|
|
1403
|
+
// only churns the small suffix, not the cached prefix.
|
|
1404
|
+
extraToolSchemas.push({
|
|
1405
|
+
type: 'advisor_20260301',
|
|
1406
|
+
name: 'advisor',
|
|
1407
|
+
model: advisorModel,
|
|
1408
|
+
} as unknown as BetaToolUnion)
|
|
1409
|
+
}
|
|
1410
|
+
const allTools = [...toolSchemas, ...extraToolSchemas]
|
|
1411
|
+
|
|
1412
|
+
const isFastMode =
|
|
1413
|
+
isFastModeEnabled() &&
|
|
1414
|
+
isFastModeAvailable() &&
|
|
1415
|
+
!isFastModeCooldown() &&
|
|
1416
|
+
isFastModeSupportedByModel(options.model) &&
|
|
1417
|
+
!!options.fastMode
|
|
1418
|
+
|
|
1419
|
+
// Sticky-on latches for dynamic beta headers. Each header, once first
|
|
1420
|
+
// sent, keeps being sent for the rest of the session so mid-session
|
|
1421
|
+
// toggles don't change the server-side cache key and bust ~50-70K tokens.
|
|
1422
|
+
// Latches are cleared on /clear and /compact via clearBetaHeaderLatches().
|
|
1423
|
+
// Per-call gates (isAgenticQuery, querySource===repl_main_thread) stay
|
|
1424
|
+
// per-call so non-agentic queries keep their own stable header set.
|
|
1425
|
+
|
|
1426
|
+
let afkHeaderLatched = getAfkModeHeaderLatched() === true
|
|
1427
|
+
if (feature('TRANSCRIPT_CLASSIFIER')) {
|
|
1428
|
+
if (
|
|
1429
|
+
!afkHeaderLatched &&
|
|
1430
|
+
isAgenticQuery &&
|
|
1431
|
+
shouldIncludeFirstPartyOnlyBetas() &&
|
|
1432
|
+
(autoModeStateModule?.isAutoModeActive() ?? false)
|
|
1433
|
+
) {
|
|
1434
|
+
afkHeaderLatched = true
|
|
1435
|
+
setAfkModeHeaderLatched(true)
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
let fastModeHeaderLatched = getFastModeHeaderLatched() === true
|
|
1440
|
+
if (!fastModeHeaderLatched && isFastMode) {
|
|
1441
|
+
fastModeHeaderLatched = true
|
|
1442
|
+
setFastModeHeaderLatched(true)
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
let cacheEditingHeaderLatched = getCacheEditingHeaderLatched() === true
|
|
1446
|
+
if (feature('CACHED_MICROCOMPACT')) {
|
|
1447
|
+
if (
|
|
1448
|
+
!cacheEditingHeaderLatched &&
|
|
1449
|
+
cachedMCEnabled &&
|
|
1450
|
+
getAPIProvider() === 'firstParty' &&
|
|
1451
|
+
options.querySource === 'repl_main_thread'
|
|
1452
|
+
) {
|
|
1453
|
+
cacheEditingHeaderLatched = true
|
|
1454
|
+
setCacheEditingHeaderLatched(true)
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
// Only latch from agentic queries so a classifier call doesn't flip the
|
|
1459
|
+
// main thread's context_management mid-turn.
|
|
1460
|
+
let thinkingClearLatched = getThinkingClearLatched() === true
|
|
1461
|
+
if (!thinkingClearLatched && isAgenticQuery) {
|
|
1462
|
+
const lastCompletion = getLastApiCompletionTimestamp()
|
|
1463
|
+
if (
|
|
1464
|
+
lastCompletion !== null &&
|
|
1465
|
+
Date.now() - lastCompletion > CACHE_TTL_1HOUR_MS
|
|
1466
|
+
) {
|
|
1467
|
+
thinkingClearLatched = true
|
|
1468
|
+
setThinkingClearLatched(true)
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
const effort = resolveAppliedEffort(options.model, options.effortValue)
|
|
1473
|
+
|
|
1474
|
+
if (feature('PROMPT_CACHE_BREAK_DETECTION')) {
|
|
1475
|
+
// Exclude defer_loading tools from the hash -- the API strips them from the
|
|
1476
|
+
// prompt, so they never affect the actual cache key. Including them creates
|
|
1477
|
+
// false-positive "tool schemas changed" breaks when tools are discovered or
|
|
1478
|
+
// MCP servers reconnect.
|
|
1479
|
+
const toolsForCacheDetection = allTools.filter(
|
|
1480
|
+
t => !('defer_loading' in t && t.defer_loading),
|
|
1481
|
+
)
|
|
1482
|
+
// Capture everything that could affect the server-side cache key.
|
|
1483
|
+
// Pass latched header values (not live state) so break detection
|
|
1484
|
+
// reflects what we actually send, not what the user toggled.
|
|
1485
|
+
recordPromptState({
|
|
1486
|
+
system,
|
|
1487
|
+
toolSchemas: toolsForCacheDetection,
|
|
1488
|
+
querySource: options.querySource,
|
|
1489
|
+
model: options.model,
|
|
1490
|
+
agentId: options.agentId,
|
|
1491
|
+
fastMode: fastModeHeaderLatched,
|
|
1492
|
+
globalCacheStrategy,
|
|
1493
|
+
betas,
|
|
1494
|
+
autoModeActive: afkHeaderLatched,
|
|
1495
|
+
isUsingOverage: currentLimits.isUsingOverage ?? false,
|
|
1496
|
+
cachedMCEnabled: cacheEditingHeaderLatched,
|
|
1497
|
+
effortValue: effort,
|
|
1498
|
+
extraBodyParams: getExtraBodyParams(),
|
|
1499
|
+
})
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1502
|
+
const newContext: LLMRequestNewContext | undefined = isBetaTracingEnabled()
|
|
1503
|
+
? {
|
|
1504
|
+
systemPrompt: systemPrompt.join('\n\n'),
|
|
1505
|
+
querySource: options.querySource,
|
|
1506
|
+
tools: jsonStringify(allTools),
|
|
1507
|
+
}
|
|
1508
|
+
: undefined
|
|
1509
|
+
|
|
1510
|
+
// Capture the span so we can pass it to endLLMRequestSpan later
|
|
1511
|
+
// This ensures responses are matched to the correct request when multiple requests run in parallel
|
|
1512
|
+
const llmSpan = startLLMRequestSpan(
|
|
1513
|
+
options.model,
|
|
1514
|
+
newContext,
|
|
1515
|
+
messagesForAPI,
|
|
1516
|
+
isFastMode,
|
|
1517
|
+
)
|
|
1518
|
+
|
|
1519
|
+
const startIncludingRetries = Date.now()
|
|
1520
|
+
let start = Date.now()
|
|
1521
|
+
let attemptNumber = 0
|
|
1522
|
+
const attemptStartTimes: number[] = []
|
|
1523
|
+
let stream: Stream<BetaRawMessageStreamEvent> | undefined = undefined
|
|
1524
|
+
let streamRequestId: string | null | undefined = undefined
|
|
1525
|
+
let clientRequestId: string | undefined = undefined
|
|
1526
|
+
// eslint-disable-next-line eslint-plugin-n/no-unsupported-features/node-builtins -- Response is available in Node 18+ and is used by the SDK
|
|
1527
|
+
let streamResponse: Response | undefined = undefined
|
|
1528
|
+
|
|
1529
|
+
// Release all stream resources to prevent native memory leaks.
|
|
1530
|
+
// The Response object holds native TLS/socket buffers that live outside the
|
|
1531
|
+
// V8 heap (observed on the Node.js/npm path; see GH #32920), so we must
|
|
1532
|
+
// explicitly cancel and release it regardless of how the generator exits.
|
|
1533
|
+
function releaseStreamResources(): void {
|
|
1534
|
+
cleanupStream(stream)
|
|
1535
|
+
stream = undefined
|
|
1536
|
+
if (streamResponse) {
|
|
1537
|
+
streamResponse.body?.cancel().catch(() => {})
|
|
1538
|
+
streamResponse = undefined
|
|
1539
|
+
}
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
// Consume pending cache edits ONCE before paramsFromContext is defined.
|
|
1543
|
+
// paramsFromContext is called multiple times (logging, retries), so consuming
|
|
1544
|
+
// inside it would cause the first call to steal edits from subsequent calls.
|
|
1545
|
+
const consumedCacheEdits = cachedMCEnabled ? consumePendingCacheEdits() : null
|
|
1546
|
+
const consumedPinnedEdits = cachedMCEnabled ? getPinnedCacheEdits() : []
|
|
1547
|
+
|
|
1548
|
+
// Capture the betas sent in the last API request, including the ones that
|
|
1549
|
+
// were dynamically added, so we can log and send it to telemetry.
|
|
1550
|
+
let lastRequestBetas: string[] | undefined
|
|
1551
|
+
|
|
1552
|
+
const paramsFromContext = (retryContext: RetryContext) => {
|
|
1553
|
+
const betasParams = [...betas]
|
|
1554
|
+
|
|
1555
|
+
// Append 1M beta dynamically for the Sonnet 1M experiment.
|
|
1556
|
+
if (
|
|
1557
|
+
!betasParams.includes(CONTEXT_1M_BETA_HEADER) &&
|
|
1558
|
+
getSonnet1mExpTreatmentEnabled(retryContext.model)
|
|
1559
|
+
) {
|
|
1560
|
+
betasParams.push(CONTEXT_1M_BETA_HEADER)
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1563
|
+
// For Bedrock, include both model-based betas and dynamically-added tool search header
|
|
1564
|
+
const bedrockBetas =
|
|
1565
|
+
getAPIProvider() === 'bedrock'
|
|
1566
|
+
? [
|
|
1567
|
+
...getBedrockExtraBodyParamsBetas(retryContext.model),
|
|
1568
|
+
...(toolSearchHeader ? [toolSearchHeader] : []),
|
|
1569
|
+
]
|
|
1570
|
+
: []
|
|
1571
|
+
const extraBodyParams = getExtraBodyParams(bedrockBetas)
|
|
1572
|
+
|
|
1573
|
+
const outputConfig: BetaOutputConfig = {
|
|
1574
|
+
...((extraBodyParams.output_config as BetaOutputConfig) ?? {}),
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
configureEffortParams(
|
|
1578
|
+
effort,
|
|
1579
|
+
outputConfig,
|
|
1580
|
+
extraBodyParams,
|
|
1581
|
+
betasParams,
|
|
1582
|
+
options.model,
|
|
1583
|
+
)
|
|
1584
|
+
|
|
1585
|
+
configureTaskBudgetParams(
|
|
1586
|
+
options.taskBudget,
|
|
1587
|
+
outputConfig as BetaOutputConfig & { task_budget?: TaskBudgetParam },
|
|
1588
|
+
betasParams,
|
|
1589
|
+
)
|
|
1590
|
+
|
|
1591
|
+
// Merge outputFormat into extraBodyParams.output_config alongside effort
|
|
1592
|
+
// Requires structured-outputs beta header per SDK (see parse() in messages.mjs)
|
|
1593
|
+
if (options.outputFormat && !('format' in outputConfig)) {
|
|
1594
|
+
outputConfig.format = options.outputFormat as BetaJSONOutputFormat
|
|
1595
|
+
// Add beta header if not already present and provider supports it
|
|
1596
|
+
if (
|
|
1597
|
+
modelSupportsStructuredOutputs(options.model) &&
|
|
1598
|
+
!betasParams.includes(STRUCTURED_OUTPUTS_BETA_HEADER)
|
|
1599
|
+
) {
|
|
1600
|
+
betasParams.push(STRUCTURED_OUTPUTS_BETA_HEADER)
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
// Retry context gets preference because it tries to course correct if we exceed the context window limit
|
|
1605
|
+
const maxOutputTokens =
|
|
1606
|
+
retryContext?.maxTokensOverride ||
|
|
1607
|
+
options.maxOutputTokensOverride ||
|
|
1608
|
+
getMaxOutputTokensForModel(options.model)
|
|
1609
|
+
|
|
1610
|
+
const hasThinking =
|
|
1611
|
+
thinkingConfig.type !== 'disabled' &&
|
|
1612
|
+
!isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_THINKING)
|
|
1613
|
+
let thinking: BetaMessageStreamParams['thinking'] | undefined = undefined
|
|
1614
|
+
|
|
1615
|
+
// IMPORTANT: Do not change the adaptive-vs-budget thinking selection below
|
|
1616
|
+
// without notifying the model launch DRI and research. This is a sensitive
|
|
1617
|
+
// setting that can greatly affect model quality and bashing.
|
|
1618
|
+
if (hasThinking && modelSupportsThinking(options.model)) {
|
|
1619
|
+
if (
|
|
1620
|
+
!isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING) &&
|
|
1621
|
+
modelSupportsAdaptiveThinking(options.model)
|
|
1622
|
+
) {
|
|
1623
|
+
// For models that support adaptive thinking, always use adaptive
|
|
1624
|
+
// thinking without a budget.
|
|
1625
|
+
thinking = {
|
|
1626
|
+
type: 'adaptive',
|
|
1627
|
+
} satisfies BetaMessageStreamParams['thinking']
|
|
1628
|
+
} else {
|
|
1629
|
+
// For models that do not support adaptive thinking, use the default
|
|
1630
|
+
// thinking budget unless explicitly specified.
|
|
1631
|
+
let thinkingBudget = getMaxThinkingTokensForModel(options.model)
|
|
1632
|
+
if (
|
|
1633
|
+
thinkingConfig.type === 'enabled' &&
|
|
1634
|
+
thinkingConfig.budgetTokens !== undefined
|
|
1635
|
+
) {
|
|
1636
|
+
thinkingBudget = thinkingConfig.budgetTokens
|
|
1637
|
+
}
|
|
1638
|
+
thinkingBudget = Math.min(maxOutputTokens - 1, thinkingBudget)
|
|
1639
|
+
thinking = {
|
|
1640
|
+
budget_tokens: thinkingBudget,
|
|
1641
|
+
type: 'enabled',
|
|
1642
|
+
} satisfies BetaMessageStreamParams['thinking']
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
// Get API context management strategies if enabled
|
|
1647
|
+
const contextManagement = getAPIContextManagement({
|
|
1648
|
+
hasThinking,
|
|
1649
|
+
isRedactThinkingActive: betasParams.includes(REDACT_THINKING_BETA_HEADER),
|
|
1650
|
+
clearAllThinking: thinkingClearLatched,
|
|
1651
|
+
})
|
|
1652
|
+
|
|
1653
|
+
const enablePromptCaching =
|
|
1654
|
+
options.enablePromptCaching ?? getPromptCachingEnabled(retryContext.model)
|
|
1655
|
+
|
|
1656
|
+
// Fast mode: header is latched session-stable (cache-safe), but
|
|
1657
|
+
// `speed='fast'` stays dynamic so cooldown still suppresses the actual
|
|
1658
|
+
// fast-mode request without changing the cache key.
|
|
1659
|
+
let speed: BetaMessageStreamParams['speed']
|
|
1660
|
+
const isFastModeForRetry =
|
|
1661
|
+
isFastModeEnabled() &&
|
|
1662
|
+
isFastModeAvailable() &&
|
|
1663
|
+
!isFastModeCooldown() &&
|
|
1664
|
+
isFastModeSupportedByModel(options.model) &&
|
|
1665
|
+
!!retryContext.fastMode
|
|
1666
|
+
if (isFastModeForRetry) {
|
|
1667
|
+
speed = 'fast'
|
|
1668
|
+
}
|
|
1669
|
+
if (fastModeHeaderLatched && !betasParams.includes(FAST_MODE_BETA_HEADER)) {
|
|
1670
|
+
betasParams.push(FAST_MODE_BETA_HEADER)
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1673
|
+
// AFK mode beta: latched once auto mode is first activated. Still gated
|
|
1674
|
+
// by isAgenticQuery per-call so classifiers/compaction don't get it.
|
|
1675
|
+
if (feature('TRANSCRIPT_CLASSIFIER')) {
|
|
1676
|
+
if (
|
|
1677
|
+
afkHeaderLatched &&
|
|
1678
|
+
shouldIncludeFirstPartyOnlyBetas() &&
|
|
1679
|
+
isAgenticQuery &&
|
|
1680
|
+
!betasParams.includes(AFK_MODE_BETA_HEADER)
|
|
1681
|
+
) {
|
|
1682
|
+
betasParams.push(AFK_MODE_BETA_HEADER)
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
// Cache editing beta: header is latched session-stable; useCachedMC
|
|
1687
|
+
// (controls cache_edits body behavior) stays live so edits stop when
|
|
1688
|
+
// the feature disables but the header doesn't flip.
|
|
1689
|
+
const useCachedMC =
|
|
1690
|
+
cachedMCEnabled &&
|
|
1691
|
+
getAPIProvider() === 'firstParty' &&
|
|
1692
|
+
options.querySource === 'repl_main_thread'
|
|
1693
|
+
if (
|
|
1694
|
+
cacheEditingHeaderLatched &&
|
|
1695
|
+
getAPIProvider() === 'firstParty' &&
|
|
1696
|
+
options.querySource === 'repl_main_thread' &&
|
|
1697
|
+
!betasParams.includes(cacheEditingBetaHeader)
|
|
1698
|
+
) {
|
|
1699
|
+
betasParams.push(cacheEditingBetaHeader)
|
|
1700
|
+
logForDebugging(
|
|
1701
|
+
'Cache editing beta header enabled for cached microcompact',
|
|
1702
|
+
)
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1705
|
+
// Only send temperature when thinking is disabled — the API requires
|
|
1706
|
+
// temperature: 1 when thinking is enabled, which is already the default.
|
|
1707
|
+
const temperature = !hasThinking
|
|
1708
|
+
? (options.temperatureOverride ?? 1)
|
|
1709
|
+
: undefined
|
|
1710
|
+
|
|
1711
|
+
lastRequestBetas = betasParams
|
|
1712
|
+
|
|
1713
|
+
return {
|
|
1714
|
+
model: normalizeModelStringForAPI(options.model),
|
|
1715
|
+
messages: addCacheBreakpoints(
|
|
1716
|
+
messagesForAPI,
|
|
1717
|
+
enablePromptCaching,
|
|
1718
|
+
options.querySource,
|
|
1719
|
+
useCachedMC,
|
|
1720
|
+
consumedCacheEdits,
|
|
1721
|
+
consumedPinnedEdits,
|
|
1722
|
+
options.skipCacheWrite,
|
|
1723
|
+
),
|
|
1724
|
+
system,
|
|
1725
|
+
tools: allTools,
|
|
1726
|
+
tool_choice: options.toolChoice,
|
|
1727
|
+
...(useBetas && { betas: betasParams }),
|
|
1728
|
+
metadata: getAPIMetadata(),
|
|
1729
|
+
max_tokens: maxOutputTokens,
|
|
1730
|
+
thinking,
|
|
1731
|
+
...(temperature !== undefined && { temperature }),
|
|
1732
|
+
...(contextManagement &&
|
|
1733
|
+
useBetas &&
|
|
1734
|
+
betasParams.includes(CONTEXT_MANAGEMENT_BETA_HEADER) && {
|
|
1735
|
+
context_management: contextManagement,
|
|
1736
|
+
}),
|
|
1737
|
+
...extraBodyParams,
|
|
1738
|
+
...(Object.keys(outputConfig).length > 0 && {
|
|
1739
|
+
output_config: outputConfig,
|
|
1740
|
+
}),
|
|
1741
|
+
...(speed !== undefined && { speed }),
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
// Compute log scalars synchronously so the fire-and-forget .then() closure
|
|
1746
|
+
// captures only primitives instead of paramsFromContext's full closure scope
|
|
1747
|
+
// (messagesForAPI, system, allTools, betas — the entire request-building
|
|
1748
|
+
// context), which would otherwise be pinned until the promise resolves.
|
|
1749
|
+
{
|
|
1750
|
+
const queryParams = paramsFromContext({
|
|
1751
|
+
model: options.model,
|
|
1752
|
+
thinkingConfig,
|
|
1753
|
+
})
|
|
1754
|
+
const logMessagesLength = queryParams.messages.length
|
|
1755
|
+
const logBetas = useBetas ? (queryParams.betas ?? []) : []
|
|
1756
|
+
const logThinkingType = queryParams.thinking?.type ?? 'disabled'
|
|
1757
|
+
const logEffortValue = queryParams.output_config?.effort
|
|
1758
|
+
void options.getToolPermissionContext().then(permissionContext => {
|
|
1759
|
+
logAPIQuery({
|
|
1760
|
+
model: options.model,
|
|
1761
|
+
messagesLength: logMessagesLength,
|
|
1762
|
+
temperature: options.temperatureOverride ?? 1,
|
|
1763
|
+
betas: logBetas,
|
|
1764
|
+
permissionMode: permissionContext.mode,
|
|
1765
|
+
querySource: options.querySource,
|
|
1766
|
+
queryTracking: options.queryTracking,
|
|
1767
|
+
thinkingType: logThinkingType,
|
|
1768
|
+
effortValue: logEffortValue,
|
|
1769
|
+
fastMode: isFastMode,
|
|
1770
|
+
previousRequestId,
|
|
1771
|
+
})
|
|
1772
|
+
})
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
const newMessages: AssistantMessage[] = []
|
|
1776
|
+
let ttftMs = 0
|
|
1777
|
+
let partialMessage: BetaMessage | undefined = undefined
|
|
1778
|
+
const contentBlocks: (BetaContentBlock | ConnectorTextBlock)[] = []
|
|
1779
|
+
let usage: NonNullableUsage = EMPTY_USAGE
|
|
1780
|
+
let costUSD = 0
|
|
1781
|
+
let stopReason: BetaStopReason | null = null
|
|
1782
|
+
let didFallBackToNonStreaming = false
|
|
1783
|
+
let fallbackMessage: AssistantMessage | undefined
|
|
1784
|
+
let maxOutputTokens = 0
|
|
1785
|
+
let responseHeaders: globalThis.Headers | undefined = undefined
|
|
1786
|
+
let research: unknown = undefined
|
|
1787
|
+
let isFastModeRequest = isFastMode // Keep separate state as it may change if falling back
|
|
1788
|
+
let isAdvisorInProgress = false
|
|
1789
|
+
|
|
1790
|
+
try {
|
|
1791
|
+
queryCheckpoint('query_client_creation_start')
|
|
1792
|
+
const generator = withRetry(
|
|
1793
|
+
() =>
|
|
1794
|
+
getAnthropicClient({
|
|
1795
|
+
maxRetries: 0, // Disabled auto-retry in favor of manual implementation
|
|
1796
|
+
model: options.model,
|
|
1797
|
+
fetchOverride: options.fetchOverride,
|
|
1798
|
+
source: options.querySource,
|
|
1799
|
+
}),
|
|
1800
|
+
async (anthropic, attempt, context) => {
|
|
1801
|
+
attemptNumber = attempt
|
|
1802
|
+
isFastModeRequest = context.fastMode ?? false
|
|
1803
|
+
start = Date.now()
|
|
1804
|
+
attemptStartTimes.push(start)
|
|
1805
|
+
// Client has been created by withRetry's getClient() call. This fires
|
|
1806
|
+
// once per attempt; on retries the client is usually cached (withRetry
|
|
1807
|
+
// only calls getClient() again after auth errors), so the delta from
|
|
1808
|
+
// client_creation_start is meaningful on attempt 1.
|
|
1809
|
+
queryCheckpoint('query_client_creation_end')
|
|
1810
|
+
|
|
1811
|
+
const params = paramsFromContext(context)
|
|
1812
|
+
captureAPIRequest(params, options.querySource) // Capture for bug reports
|
|
1813
|
+
|
|
1814
|
+
maxOutputTokens = params.max_tokens
|
|
1815
|
+
|
|
1816
|
+
// Fire immediately before the fetch is dispatched. .withResponse() below
|
|
1817
|
+
// awaits until response headers arrive, so this MUST be before the await
|
|
1818
|
+
// or the "Network TTFB" phase measurement is wrong.
|
|
1819
|
+
queryCheckpoint('query_api_request_sent')
|
|
1820
|
+
if (!options.agentId) {
|
|
1821
|
+
headlessProfilerCheckpoint('api_request_sent')
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
// Generate and track client request ID so timeouts (which return no
|
|
1825
|
+
// server request ID) can still be correlated with server logs.
|
|
1826
|
+
// First-party only — 3P providers don't log it (inc-4029 class).
|
|
1827
|
+
clientRequestId =
|
|
1828
|
+
getAPIProvider() === 'firstParty' && isFirstPartyAnthropicBaseUrl()
|
|
1829
|
+
? randomUUID()
|
|
1830
|
+
: undefined
|
|
1831
|
+
|
|
1832
|
+
// Use raw stream instead of BetaMessageStream to avoid O(n²) partial JSON parsing
|
|
1833
|
+
// BetaMessageStream calls partialParse() on every input_json_delta, which we don't need
|
|
1834
|
+
// since we handle tool input accumulation ourselves
|
|
1835
|
+
// biome-ignore lint/plugin: main conversation loop handles attribution separately
|
|
1836
|
+
const result = await anthropic.beta.messages
|
|
1837
|
+
.create(
|
|
1838
|
+
{ ...params, stream: true },
|
|
1839
|
+
{
|
|
1840
|
+
signal,
|
|
1841
|
+
...(clientRequestId && {
|
|
1842
|
+
headers: { [CLIENT_REQUEST_ID_HEADER]: clientRequestId },
|
|
1843
|
+
}),
|
|
1844
|
+
},
|
|
1845
|
+
)
|
|
1846
|
+
.withResponse()
|
|
1847
|
+
queryCheckpoint('query_response_headers_received')
|
|
1848
|
+
streamRequestId = result.request_id
|
|
1849
|
+
streamResponse = result.response
|
|
1850
|
+
return result.data
|
|
1851
|
+
},
|
|
1852
|
+
{
|
|
1853
|
+
model: options.model,
|
|
1854
|
+
fallbackModel: options.fallbackModel,
|
|
1855
|
+
thinkingConfig,
|
|
1856
|
+
...(isFastModeEnabled() ? { fastMode: isFastMode } : false),
|
|
1857
|
+
signal,
|
|
1858
|
+
querySource: options.querySource,
|
|
1859
|
+
},
|
|
1860
|
+
)
|
|
1861
|
+
|
|
1862
|
+
let e
|
|
1863
|
+
do {
|
|
1864
|
+
e = await generator.next()
|
|
1865
|
+
|
|
1866
|
+
// yield API error messages (the stream has a 'controller' property, error messages don't)
|
|
1867
|
+
if (!('controller' in e.value)) {
|
|
1868
|
+
yield e.value
|
|
1869
|
+
}
|
|
1870
|
+
} while (!e.done)
|
|
1871
|
+
stream = e.value as Stream<BetaRawMessageStreamEvent>
|
|
1872
|
+
|
|
1873
|
+
// reset state
|
|
1874
|
+
newMessages.length = 0
|
|
1875
|
+
ttftMs = 0
|
|
1876
|
+
partialMessage = undefined
|
|
1877
|
+
contentBlocks.length = 0
|
|
1878
|
+
usage = EMPTY_USAGE
|
|
1879
|
+
stopReason = null
|
|
1880
|
+
isAdvisorInProgress = false
|
|
1881
|
+
|
|
1882
|
+
// Streaming idle timeout watchdog: abort the stream if no chunks arrive
|
|
1883
|
+
// for STREAM_IDLE_TIMEOUT_MS. Unlike the stall detection below (which only
|
|
1884
|
+
// fires when the *next* chunk arrives), this uses setTimeout to actively
|
|
1885
|
+
// kill hung streams. Without this, a silently dropped connection can hang
|
|
1886
|
+
// the session indefinitely since the SDK's request timeout only covers the
|
|
1887
|
+
// initial fetch(), not the streaming body.
|
|
1888
|
+
const streamWatchdogEnabled = isEnvTruthy(
|
|
1889
|
+
process.env.CLAUDE_ENABLE_STREAM_WATCHDOG,
|
|
1890
|
+
)
|
|
1891
|
+
const STREAM_IDLE_TIMEOUT_MS =
|
|
1892
|
+
parseInt(process.env.CLAUDE_STREAM_IDLE_TIMEOUT_MS || '', 10) || 90_000
|
|
1893
|
+
const STREAM_IDLE_WARNING_MS = STREAM_IDLE_TIMEOUT_MS / 2
|
|
1894
|
+
let streamIdleAborted = false
|
|
1895
|
+
// performance.now() snapshot when watchdog fires, for measuring abort propagation delay
|
|
1896
|
+
let streamWatchdogFiredAt: number | null = null
|
|
1897
|
+
let streamIdleWarningTimer: ReturnType<typeof setTimeout> | null = null
|
|
1898
|
+
let streamIdleTimer: ReturnType<typeof setTimeout> | null = null
|
|
1899
|
+
function clearStreamIdleTimers(): void {
|
|
1900
|
+
if (streamIdleWarningTimer !== null) {
|
|
1901
|
+
clearTimeout(streamIdleWarningTimer)
|
|
1902
|
+
streamIdleWarningTimer = null
|
|
1903
|
+
}
|
|
1904
|
+
if (streamIdleTimer !== null) {
|
|
1905
|
+
clearTimeout(streamIdleTimer)
|
|
1906
|
+
streamIdleTimer = null
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1909
|
+
function resetStreamIdleTimer(): void {
|
|
1910
|
+
clearStreamIdleTimers()
|
|
1911
|
+
if (!streamWatchdogEnabled) {
|
|
1912
|
+
return
|
|
1913
|
+
}
|
|
1914
|
+
streamIdleWarningTimer = setTimeout(
|
|
1915
|
+
warnMs => {
|
|
1916
|
+
logForDebugging(
|
|
1917
|
+
`Streaming idle warning: no chunks received for ${warnMs / 1000}s`,
|
|
1918
|
+
{ level: 'warn' },
|
|
1919
|
+
)
|
|
1920
|
+
logForDiagnosticsNoPII('warn', 'cli_streaming_idle_warning')
|
|
1921
|
+
},
|
|
1922
|
+
STREAM_IDLE_WARNING_MS,
|
|
1923
|
+
STREAM_IDLE_WARNING_MS,
|
|
1924
|
+
)
|
|
1925
|
+
streamIdleTimer = setTimeout(() => {
|
|
1926
|
+
streamIdleAborted = true
|
|
1927
|
+
streamWatchdogFiredAt = performance.now()
|
|
1928
|
+
logForDebugging(
|
|
1929
|
+
`Streaming idle timeout: no chunks received for ${STREAM_IDLE_TIMEOUT_MS / 1000}s, aborting stream`,
|
|
1930
|
+
{ level: 'error' },
|
|
1931
|
+
)
|
|
1932
|
+
logForDiagnosticsNoPII('error', 'cli_streaming_idle_timeout')
|
|
1933
|
+
logEvent('tengu_streaming_idle_timeout', {
|
|
1934
|
+
model:
|
|
1935
|
+
options.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
1936
|
+
request_id: (streamRequestId ??
|
|
1937
|
+
'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
1938
|
+
timeout_ms: STREAM_IDLE_TIMEOUT_MS,
|
|
1939
|
+
})
|
|
1940
|
+
releaseStreamResources()
|
|
1941
|
+
}, STREAM_IDLE_TIMEOUT_MS)
|
|
1942
|
+
}
|
|
1943
|
+
resetStreamIdleTimer()
|
|
1944
|
+
|
|
1945
|
+
startSessionActivity('api_call')
|
|
1946
|
+
try {
|
|
1947
|
+
// stream in and accumulate state
|
|
1948
|
+
let isFirstChunk = true
|
|
1949
|
+
let lastEventTime: number | null = null // Set after first chunk to avoid measuring TTFB as a stall
|
|
1950
|
+
const STALL_THRESHOLD_MS = 30_000 // 30 seconds
|
|
1951
|
+
let totalStallTime = 0
|
|
1952
|
+
let stallCount = 0
|
|
1953
|
+
|
|
1954
|
+
for await (const part of stream) {
|
|
1955
|
+
resetStreamIdleTimer()
|
|
1956
|
+
const now = Date.now()
|
|
1957
|
+
|
|
1958
|
+
// Detect and log streaming stalls (only after first event to avoid counting TTFB)
|
|
1959
|
+
if (lastEventTime !== null) {
|
|
1960
|
+
const timeSinceLastEvent = now - lastEventTime
|
|
1961
|
+
if (timeSinceLastEvent > STALL_THRESHOLD_MS) {
|
|
1962
|
+
stallCount++
|
|
1963
|
+
totalStallTime += timeSinceLastEvent
|
|
1964
|
+
logForDebugging(
|
|
1965
|
+
`Streaming stall detected: ${(timeSinceLastEvent / 1000).toFixed(1)}s gap between events (stall #${stallCount})`,
|
|
1966
|
+
{ level: 'warn' },
|
|
1967
|
+
)
|
|
1968
|
+
logEvent('tengu_streaming_stall', {
|
|
1969
|
+
stall_duration_ms: timeSinceLastEvent,
|
|
1970
|
+
stall_count: stallCount,
|
|
1971
|
+
total_stall_time_ms: totalStallTime,
|
|
1972
|
+
event_type:
|
|
1973
|
+
part.type as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
1974
|
+
model:
|
|
1975
|
+
options.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
1976
|
+
request_id: (streamRequestId ??
|
|
1977
|
+
'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
1978
|
+
})
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1981
|
+
lastEventTime = now
|
|
1982
|
+
|
|
1983
|
+
if (isFirstChunk) {
|
|
1984
|
+
logForDebugging('Stream started - received first chunk')
|
|
1985
|
+
queryCheckpoint('query_first_chunk_received')
|
|
1986
|
+
if (!options.agentId) {
|
|
1987
|
+
headlessProfilerCheckpoint('first_chunk')
|
|
1988
|
+
}
|
|
1989
|
+
endQueryProfile()
|
|
1990
|
+
isFirstChunk = false
|
|
1991
|
+
}
|
|
1992
|
+
|
|
1993
|
+
switch (part.type) {
|
|
1994
|
+
case 'message_start': {
|
|
1995
|
+
partialMessage = part.message
|
|
1996
|
+
ttftMs = Date.now() - start
|
|
1997
|
+
usage = updateUsage(usage, part.message?.usage)
|
|
1998
|
+
// Capture research from message_start if available (internal only).
|
|
1999
|
+
// Always overwrite with the latest value.
|
|
2000
|
+
if (
|
|
2001
|
+
process.env.USER_TYPE === 'ant' &&
|
|
2002
|
+
'research' in (part.message as unknown as Record<string, unknown>)
|
|
2003
|
+
) {
|
|
2004
|
+
research = (part.message as unknown as Record<string, unknown>)
|
|
2005
|
+
.research
|
|
2006
|
+
}
|
|
2007
|
+
break
|
|
2008
|
+
}
|
|
2009
|
+
case 'content_block_start':
|
|
2010
|
+
switch (part.content_block.type) {
|
|
2011
|
+
case 'tool_use':
|
|
2012
|
+
contentBlocks[part.index] = {
|
|
2013
|
+
...part.content_block,
|
|
2014
|
+
input: '',
|
|
2015
|
+
}
|
|
2016
|
+
break
|
|
2017
|
+
case 'server_tool_use':
|
|
2018
|
+
contentBlocks[part.index] = {
|
|
2019
|
+
...part.content_block,
|
|
2020
|
+
input: '' as unknown as { [key: string]: unknown },
|
|
2021
|
+
}
|
|
2022
|
+
if ((part.content_block.name as string) === 'advisor') {
|
|
2023
|
+
isAdvisorInProgress = true
|
|
2024
|
+
logForDebugging(`[AdvisorTool] Advisor tool called`)
|
|
2025
|
+
logEvent('tengu_advisor_tool_call', {
|
|
2026
|
+
model:
|
|
2027
|
+
options.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2028
|
+
advisor_model: (advisorModel ??
|
|
2029
|
+
'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2030
|
+
})
|
|
2031
|
+
}
|
|
2032
|
+
break
|
|
2033
|
+
case 'text':
|
|
2034
|
+
contentBlocks[part.index] = {
|
|
2035
|
+
...part.content_block,
|
|
2036
|
+
// awkwardly, the sdk sometimes returns text as part of a
|
|
2037
|
+
// content_block_start message, then returns the same text
|
|
2038
|
+
// again in a content_block_delta message. we ignore it here
|
|
2039
|
+
// since there doesn't seem to be a way to detect when a
|
|
2040
|
+
// content_block_delta message duplicates the text.
|
|
2041
|
+
text: '',
|
|
2042
|
+
}
|
|
2043
|
+
break
|
|
2044
|
+
case 'thinking':
|
|
2045
|
+
contentBlocks[part.index] = {
|
|
2046
|
+
...part.content_block,
|
|
2047
|
+
// also awkward
|
|
2048
|
+
thinking: '',
|
|
2049
|
+
// initialize signature to ensure field exists even if signature_delta never arrives
|
|
2050
|
+
signature: '',
|
|
2051
|
+
}
|
|
2052
|
+
break
|
|
2053
|
+
default:
|
|
2054
|
+
// even more awkwardly, the sdk mutates the contents of text blocks
|
|
2055
|
+
// as it works. we want the blocks to be immutable, so that we can
|
|
2056
|
+
// accumulate state ourselves.
|
|
2057
|
+
contentBlocks[part.index] = { ...part.content_block }
|
|
2058
|
+
if (
|
|
2059
|
+
(part.content_block.type as string) === 'advisor_tool_result'
|
|
2060
|
+
) {
|
|
2061
|
+
isAdvisorInProgress = false
|
|
2062
|
+
logForDebugging(`[AdvisorTool] Advisor tool result received`)
|
|
2063
|
+
}
|
|
2064
|
+
break
|
|
2065
|
+
}
|
|
2066
|
+
break
|
|
2067
|
+
case 'content_block_delta': {
|
|
2068
|
+
const contentBlock = contentBlocks[part.index]
|
|
2069
|
+
const delta = part.delta as typeof part.delta | ConnectorTextDelta
|
|
2070
|
+
if (!contentBlock) {
|
|
2071
|
+
logEvent('tengu_streaming_error', {
|
|
2072
|
+
error_type:
|
|
2073
|
+
'content_block_not_found_delta' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2074
|
+
part_type:
|
|
2075
|
+
part.type as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2076
|
+
part_index: part.index,
|
|
2077
|
+
})
|
|
2078
|
+
throw new RangeError('Content block not found')
|
|
2079
|
+
}
|
|
2080
|
+
if (
|
|
2081
|
+
feature('CONNECTOR_TEXT') &&
|
|
2082
|
+
delta.type === 'connector_text_delta'
|
|
2083
|
+
) {
|
|
2084
|
+
if (contentBlock.type !== 'connector_text') {
|
|
2085
|
+
logEvent('tengu_streaming_error', {
|
|
2086
|
+
error_type:
|
|
2087
|
+
'content_block_type_mismatch_connector_text' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2088
|
+
expected_type:
|
|
2089
|
+
'connector_text' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2090
|
+
actual_type:
|
|
2091
|
+
contentBlock.type as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2092
|
+
})
|
|
2093
|
+
throw new Error('Content block is not a connector_text block')
|
|
2094
|
+
}
|
|
2095
|
+
contentBlock.connector_text += delta.connector_text
|
|
2096
|
+
} else {
|
|
2097
|
+
switch (delta.type) {
|
|
2098
|
+
case 'citations_delta':
|
|
2099
|
+
// TODO: handle citations
|
|
2100
|
+
break
|
|
2101
|
+
case 'input_json_delta':
|
|
2102
|
+
if (
|
|
2103
|
+
contentBlock.type !== 'tool_use' &&
|
|
2104
|
+
contentBlock.type !== 'server_tool_use'
|
|
2105
|
+
) {
|
|
2106
|
+
logEvent('tengu_streaming_error', {
|
|
2107
|
+
error_type:
|
|
2108
|
+
'content_block_type_mismatch_input_json' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2109
|
+
expected_type:
|
|
2110
|
+
'tool_use' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2111
|
+
actual_type:
|
|
2112
|
+
contentBlock.type as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2113
|
+
})
|
|
2114
|
+
throw new Error('Content block is not a input_json block')
|
|
2115
|
+
}
|
|
2116
|
+
if (typeof contentBlock.input !== 'string') {
|
|
2117
|
+
logEvent('tengu_streaming_error', {
|
|
2118
|
+
error_type:
|
|
2119
|
+
'content_block_input_not_string' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2120
|
+
input_type:
|
|
2121
|
+
typeof contentBlock.input as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2122
|
+
})
|
|
2123
|
+
throw new Error('Content block input is not a string')
|
|
2124
|
+
}
|
|
2125
|
+
contentBlock.input += delta.partial_json
|
|
2126
|
+
break
|
|
2127
|
+
case 'text_delta':
|
|
2128
|
+
if (contentBlock.type !== 'text') {
|
|
2129
|
+
logEvent('tengu_streaming_error', {
|
|
2130
|
+
error_type:
|
|
2131
|
+
'content_block_type_mismatch_text' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2132
|
+
expected_type:
|
|
2133
|
+
'text' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2134
|
+
actual_type:
|
|
2135
|
+
contentBlock.type as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2136
|
+
})
|
|
2137
|
+
throw new Error('Content block is not a text block')
|
|
2138
|
+
}
|
|
2139
|
+
contentBlock.text += delta.text
|
|
2140
|
+
break
|
|
2141
|
+
case 'signature_delta':
|
|
2142
|
+
if (
|
|
2143
|
+
feature('CONNECTOR_TEXT') &&
|
|
2144
|
+
contentBlock.type === 'connector_text'
|
|
2145
|
+
) {
|
|
2146
|
+
contentBlock.signature = delta.signature
|
|
2147
|
+
break
|
|
2148
|
+
}
|
|
2149
|
+
if (contentBlock.type !== 'thinking') {
|
|
2150
|
+
logEvent('tengu_streaming_error', {
|
|
2151
|
+
error_type:
|
|
2152
|
+
'content_block_type_mismatch_thinking_signature' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2153
|
+
expected_type:
|
|
2154
|
+
'thinking' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2155
|
+
actual_type:
|
|
2156
|
+
contentBlock.type as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2157
|
+
})
|
|
2158
|
+
throw new Error('Content block is not a thinking block')
|
|
2159
|
+
}
|
|
2160
|
+
contentBlock.signature = delta.signature
|
|
2161
|
+
break
|
|
2162
|
+
case 'thinking_delta':
|
|
2163
|
+
if (contentBlock.type !== 'thinking') {
|
|
2164
|
+
logEvent('tengu_streaming_error', {
|
|
2165
|
+
error_type:
|
|
2166
|
+
'content_block_type_mismatch_thinking_delta' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2167
|
+
expected_type:
|
|
2168
|
+
'thinking' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2169
|
+
actual_type:
|
|
2170
|
+
contentBlock.type as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2171
|
+
})
|
|
2172
|
+
throw new Error('Content block is not a thinking block')
|
|
2173
|
+
}
|
|
2174
|
+
contentBlock.thinking += delta.thinking
|
|
2175
|
+
break
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
// Capture research from content_block_delta if available (internal only).
|
|
2179
|
+
// Always overwrite with the latest value.
|
|
2180
|
+
if (process.env.USER_TYPE === 'ant' && 'research' in part) {
|
|
2181
|
+
research = (part as { research: unknown }).research
|
|
2182
|
+
}
|
|
2183
|
+
break
|
|
2184
|
+
}
|
|
2185
|
+
case 'content_block_stop': {
|
|
2186
|
+
const contentBlock = contentBlocks[part.index]
|
|
2187
|
+
if (!contentBlock) {
|
|
2188
|
+
logEvent('tengu_streaming_error', {
|
|
2189
|
+
error_type:
|
|
2190
|
+
'content_block_not_found_stop' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2191
|
+
part_type:
|
|
2192
|
+
part.type as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2193
|
+
part_index: part.index,
|
|
2194
|
+
})
|
|
2195
|
+
throw new RangeError('Content block not found')
|
|
2196
|
+
}
|
|
2197
|
+
if (!partialMessage) {
|
|
2198
|
+
logEvent('tengu_streaming_error', {
|
|
2199
|
+
error_type:
|
|
2200
|
+
'partial_message_not_found' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2201
|
+
part_type:
|
|
2202
|
+
part.type as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2203
|
+
})
|
|
2204
|
+
throw new Error('Message not found')
|
|
2205
|
+
}
|
|
2206
|
+
const m: AssistantMessage = {
|
|
2207
|
+
message: {
|
|
2208
|
+
...partialMessage,
|
|
2209
|
+
content: normalizeContentFromAPI(
|
|
2210
|
+
[contentBlock] as BetaContentBlock[],
|
|
2211
|
+
tools,
|
|
2212
|
+
options.agentId,
|
|
2213
|
+
),
|
|
2214
|
+
},
|
|
2215
|
+
requestId: streamRequestId ?? undefined,
|
|
2216
|
+
type: 'assistant',
|
|
2217
|
+
uuid: randomUUID(),
|
|
2218
|
+
timestamp: new Date().toISOString(),
|
|
2219
|
+
...(process.env.USER_TYPE === 'ant' &&
|
|
2220
|
+
research !== undefined && { research }),
|
|
2221
|
+
...(advisorModel && { advisorModel }),
|
|
2222
|
+
}
|
|
2223
|
+
newMessages.push(m)
|
|
2224
|
+
yield m
|
|
2225
|
+
break
|
|
2226
|
+
}
|
|
2227
|
+
case 'message_delta': {
|
|
2228
|
+
usage = updateUsage(usage, part.usage)
|
|
2229
|
+
// Capture research from message_delta if available (internal only).
|
|
2230
|
+
// Always overwrite with the latest value. Also write back to
|
|
2231
|
+
// already-yielded messages since message_delta arrives after
|
|
2232
|
+
// content_block_stop.
|
|
2233
|
+
if (
|
|
2234
|
+
process.env.USER_TYPE === 'ant' &&
|
|
2235
|
+
'research' in (part as unknown as Record<string, unknown>)
|
|
2236
|
+
) {
|
|
2237
|
+
research = (part as unknown as Record<string, unknown>).research
|
|
2238
|
+
for (const msg of newMessages) {
|
|
2239
|
+
msg.research = research
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
|
|
2243
|
+
// Write final usage and stop_reason back to the last yielded
|
|
2244
|
+
// message. Messages are created at content_block_stop from
|
|
2245
|
+
// partialMessage, which was set at message_start before any tokens
|
|
2246
|
+
// were generated (output_tokens: 0, stop_reason: null).
|
|
2247
|
+
// message_delta arrives after content_block_stop with the real
|
|
2248
|
+
// values.
|
|
2249
|
+
//
|
|
2250
|
+
// IMPORTANT: Use direct property mutation, not object replacement.
|
|
2251
|
+
// The transcript write queue holds a reference to message.message
|
|
2252
|
+
// and serializes it lazily (100ms flush interval). Object
|
|
2253
|
+
// replacement ({ ...lastMsg.message, usage }) would disconnect
|
|
2254
|
+
// the queued reference; direct mutation ensures the transcript
|
|
2255
|
+
// captures the final values.
|
|
2256
|
+
stopReason = part.delta.stop_reason
|
|
2257
|
+
|
|
2258
|
+
const lastMsg = newMessages.at(-1)
|
|
2259
|
+
if (lastMsg) {
|
|
2260
|
+
lastMsg.message.usage = usage
|
|
2261
|
+
lastMsg.message.stop_reason = stopReason
|
|
2262
|
+
}
|
|
2263
|
+
|
|
2264
|
+
// Update cost
|
|
2265
|
+
const costUSDForPart = calculateUSDCost(resolvedModel, usage)
|
|
2266
|
+
costUSD += addToTotalSessionCost(
|
|
2267
|
+
costUSDForPart,
|
|
2268
|
+
usage,
|
|
2269
|
+
options.model,
|
|
2270
|
+
)
|
|
2271
|
+
|
|
2272
|
+
const refusalMessage = getErrorMessageIfRefusal(
|
|
2273
|
+
part.delta.stop_reason,
|
|
2274
|
+
options.model,
|
|
2275
|
+
)
|
|
2276
|
+
if (refusalMessage) {
|
|
2277
|
+
yield refusalMessage
|
|
2278
|
+
}
|
|
2279
|
+
|
|
2280
|
+
if (stopReason === 'max_tokens') {
|
|
2281
|
+
logEvent('tengu_max_tokens_reached', {
|
|
2282
|
+
max_tokens: maxOutputTokens,
|
|
2283
|
+
})
|
|
2284
|
+
yield createAssistantAPIErrorMessage({
|
|
2285
|
+
content: `${API_ERROR_MESSAGE_PREFIX}: Claude's response exceeded the ${
|
|
2286
|
+
maxOutputTokens
|
|
2287
|
+
} output token maximum. To configure this behavior, set the CLAUDE_CODE_MAX_OUTPUT_TOKENS environment variable.`,
|
|
2288
|
+
apiError: 'max_output_tokens',
|
|
2289
|
+
error: 'max_output_tokens',
|
|
2290
|
+
})
|
|
2291
|
+
}
|
|
2292
|
+
|
|
2293
|
+
if (stopReason === 'model_context_window_exceeded') {
|
|
2294
|
+
logEvent('tengu_context_window_exceeded', {
|
|
2295
|
+
max_tokens: maxOutputTokens,
|
|
2296
|
+
output_tokens: usage.output_tokens,
|
|
2297
|
+
})
|
|
2298
|
+
// Reuse the max_output_tokens recovery path — from the model's
|
|
2299
|
+
// perspective, both mean "response was cut off, continue from
|
|
2300
|
+
// where you left off."
|
|
2301
|
+
yield createAssistantAPIErrorMessage({
|
|
2302
|
+
content: `${API_ERROR_MESSAGE_PREFIX}: The model has reached its context window limit.`,
|
|
2303
|
+
apiError: 'max_output_tokens',
|
|
2304
|
+
error: 'max_output_tokens',
|
|
2305
|
+
})
|
|
2306
|
+
}
|
|
2307
|
+
break
|
|
2308
|
+
}
|
|
2309
|
+
case 'message_stop':
|
|
2310
|
+
break
|
|
2311
|
+
}
|
|
2312
|
+
|
|
2313
|
+
yield {
|
|
2314
|
+
type: 'stream_event',
|
|
2315
|
+
event: part,
|
|
2316
|
+
...(part.type === 'message_start' ? { ttftMs } : undefined),
|
|
2317
|
+
}
|
|
2318
|
+
}
|
|
2319
|
+
// Clear the idle timeout watchdog now that the stream loop has exited
|
|
2320
|
+
clearStreamIdleTimers()
|
|
2321
|
+
|
|
2322
|
+
// If the stream was aborted by our idle timeout watchdog, fall back to
|
|
2323
|
+
// non-streaming retry rather than treating it as a completed stream.
|
|
2324
|
+
if (streamIdleAborted) {
|
|
2325
|
+
// Instrumentation: proves the for-await exited after the watchdog fired
|
|
2326
|
+
// (vs. hung forever). exit_delay_ms measures abort propagation latency:
|
|
2327
|
+
// 0-10ms = abort worked; >>1000ms = something else woke the loop.
|
|
2328
|
+
const exitDelayMs =
|
|
2329
|
+
streamWatchdogFiredAt !== null
|
|
2330
|
+
? Math.round(performance.now() - streamWatchdogFiredAt)
|
|
2331
|
+
: -1
|
|
2332
|
+
logForDiagnosticsNoPII(
|
|
2333
|
+
'info',
|
|
2334
|
+
'cli_stream_loop_exited_after_watchdog_clean',
|
|
2335
|
+
)
|
|
2336
|
+
logEvent('tengu_stream_loop_exited_after_watchdog', {
|
|
2337
|
+
request_id: (streamRequestId ??
|
|
2338
|
+
'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2339
|
+
exit_delay_ms: exitDelayMs,
|
|
2340
|
+
exit_path:
|
|
2341
|
+
'clean' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2342
|
+
model:
|
|
2343
|
+
options.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2344
|
+
})
|
|
2345
|
+
// Prevent double-emit: this throw lands in the catch block below,
|
|
2346
|
+
// whose exit_path='error' probe guards on streamWatchdogFiredAt.
|
|
2347
|
+
streamWatchdogFiredAt = null
|
|
2348
|
+
throw new Error('Stream idle timeout - no chunks received')
|
|
2349
|
+
}
|
|
2350
|
+
|
|
2351
|
+
// Detect when the stream completed without producing any assistant messages.
|
|
2352
|
+
// This covers two proxy failure modes:
|
|
2353
|
+
// 1. No events at all (!partialMessage): proxy returned 200 with non-SSE body
|
|
2354
|
+
// 2. Partial events (partialMessage set but no content blocks completed AND
|
|
2355
|
+
// no stop_reason received): proxy returned message_start but stream ended
|
|
2356
|
+
// before content_block_stop and before message_delta with stop_reason
|
|
2357
|
+
// BetaMessageStream had the first check in _endRequest() but the raw Stream
|
|
2358
|
+
// does not - without it the generator silently returns no assistant messages,
|
|
2359
|
+
// causing "Execution error" in -p mode.
|
|
2360
|
+
// Note: We must check stopReason to avoid false positives. For example, with
|
|
2361
|
+
// structured output (--json-schema), the model calls a StructuredOutput tool
|
|
2362
|
+
// on turn 1, then on turn 2 responds with end_turn and no content blocks.
|
|
2363
|
+
// That's a legitimate empty response, not an incomplete stream.
|
|
2364
|
+
if (!partialMessage || (newMessages.length === 0 && !stopReason)) {
|
|
2365
|
+
logForDebugging(
|
|
2366
|
+
!partialMessage
|
|
2367
|
+
? 'Stream completed without receiving message_start event - triggering non-streaming fallback'
|
|
2368
|
+
: 'Stream completed with message_start but no content blocks completed - triggering non-streaming fallback',
|
|
2369
|
+
{ level: 'error' },
|
|
2370
|
+
)
|
|
2371
|
+
logEvent('tengu_stream_no_events', {
|
|
2372
|
+
model:
|
|
2373
|
+
options.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2374
|
+
request_id: (streamRequestId ??
|
|
2375
|
+
'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2376
|
+
})
|
|
2377
|
+
throw new Error('Stream ended without receiving any events')
|
|
2378
|
+
}
|
|
2379
|
+
|
|
2380
|
+
// Log summary if any stalls occurred during streaming
|
|
2381
|
+
if (stallCount > 0) {
|
|
2382
|
+
logForDebugging(
|
|
2383
|
+
`Streaming completed with ${stallCount} stall(s), total stall time: ${(totalStallTime / 1000).toFixed(1)}s`,
|
|
2384
|
+
{ level: 'warn' },
|
|
2385
|
+
)
|
|
2386
|
+
logEvent('tengu_streaming_stall_summary', {
|
|
2387
|
+
stall_count: stallCount,
|
|
2388
|
+
total_stall_time_ms: totalStallTime,
|
|
2389
|
+
model:
|
|
2390
|
+
options.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2391
|
+
request_id: (streamRequestId ??
|
|
2392
|
+
'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2393
|
+
})
|
|
2394
|
+
}
|
|
2395
|
+
|
|
2396
|
+
// Check if the cache actually broke based on response tokens
|
|
2397
|
+
if (feature('PROMPT_CACHE_BREAK_DETECTION')) {
|
|
2398
|
+
void checkResponseForCacheBreak(
|
|
2399
|
+
options.querySource,
|
|
2400
|
+
usage.cache_read_input_tokens,
|
|
2401
|
+
usage.cache_creation_input_tokens,
|
|
2402
|
+
messages,
|
|
2403
|
+
options.agentId,
|
|
2404
|
+
streamRequestId,
|
|
2405
|
+
)
|
|
2406
|
+
}
|
|
2407
|
+
|
|
2408
|
+
// Process fallback percentage header and quota status if available
|
|
2409
|
+
// streamResponse is set when the stream is created in the withRetry callback above
|
|
2410
|
+
// TypeScript's control flow analysis can't track that streamResponse is set in the callback
|
|
2411
|
+
// eslint-disable-next-line eslint-plugin-n/no-unsupported-features/node-builtins
|
|
2412
|
+
const resp = streamResponse as unknown as Response | undefined
|
|
2413
|
+
if (resp) {
|
|
2414
|
+
extractQuotaStatusFromHeaders(resp.headers)
|
|
2415
|
+
// Store headers for gateway detection
|
|
2416
|
+
responseHeaders = resp.headers
|
|
2417
|
+
}
|
|
2418
|
+
} catch (streamingError) {
|
|
2419
|
+
// Clear the idle timeout watchdog on error path too
|
|
2420
|
+
clearStreamIdleTimers()
|
|
2421
|
+
|
|
2422
|
+
// Instrumentation: if the watchdog had already fired and the for-await
|
|
2423
|
+
// threw (rather than exiting cleanly), record that the loop DID exit and
|
|
2424
|
+
// how long after the watchdog. Distinguishes true hangs from error exits.
|
|
2425
|
+
if (streamIdleAborted && streamWatchdogFiredAt !== null) {
|
|
2426
|
+
const exitDelayMs = Math.round(
|
|
2427
|
+
performance.now() - streamWatchdogFiredAt,
|
|
2428
|
+
)
|
|
2429
|
+
logForDiagnosticsNoPII(
|
|
2430
|
+
'info',
|
|
2431
|
+
'cli_stream_loop_exited_after_watchdog_error',
|
|
2432
|
+
)
|
|
2433
|
+
logEvent('tengu_stream_loop_exited_after_watchdog', {
|
|
2434
|
+
request_id: (streamRequestId ??
|
|
2435
|
+
'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2436
|
+
exit_delay_ms: exitDelayMs,
|
|
2437
|
+
exit_path:
|
|
2438
|
+
'error' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2439
|
+
error_name:
|
|
2440
|
+
streamingError instanceof Error
|
|
2441
|
+
? (streamingError.name as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
|
|
2442
|
+
: ('unknown' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS),
|
|
2443
|
+
model:
|
|
2444
|
+
options.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2445
|
+
})
|
|
2446
|
+
}
|
|
2447
|
+
|
|
2448
|
+
if (streamingError instanceof APIUserAbortError) {
|
|
2449
|
+
// Check if the abort signal was triggered by the user (ESC key)
|
|
2450
|
+
// If the signal is aborted, it's a user-initiated abort
|
|
2451
|
+
// If not, it's likely a timeout from the SDK
|
|
2452
|
+
if (signal.aborted) {
|
|
2453
|
+
// This is a real user abort (ESC key was pressed)
|
|
2454
|
+
logForDebugging(
|
|
2455
|
+
`Streaming aborted by user: ${errorMessage(streamingError)}`,
|
|
2456
|
+
)
|
|
2457
|
+
if (isAdvisorInProgress) {
|
|
2458
|
+
logEvent('tengu_advisor_tool_interrupted', {
|
|
2459
|
+
model:
|
|
2460
|
+
options.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2461
|
+
advisor_model: (advisorModel ??
|
|
2462
|
+
'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2463
|
+
})
|
|
2464
|
+
}
|
|
2465
|
+
throw streamingError
|
|
2466
|
+
} else {
|
|
2467
|
+
// The SDK threw APIUserAbortError but our signal wasn't aborted
|
|
2468
|
+
// This means it's a timeout from the SDK's internal timeout
|
|
2469
|
+
logForDebugging(
|
|
2470
|
+
`Streaming timeout (SDK abort): ${streamingError.message}`,
|
|
2471
|
+
{ level: 'error' },
|
|
2472
|
+
)
|
|
2473
|
+
// Throw a more specific error for timeout
|
|
2474
|
+
throw new APIConnectionTimeoutError({ message: 'Request timed out' })
|
|
2475
|
+
}
|
|
2476
|
+
}
|
|
2477
|
+
|
|
2478
|
+
// When the flag is enabled, skip the non-streaming fallback and let the
|
|
2479
|
+
// error propagate to withRetry. The mid-stream fallback causes double tool
|
|
2480
|
+
// execution when streaming tool execution is active: the partial stream
|
|
2481
|
+
// starts a tool, then the non-streaming retry produces the same tool_use
|
|
2482
|
+
// and runs it again. See inc-4258.
|
|
2483
|
+
const disableFallback =
|
|
2484
|
+
isEnvTruthy(process.env.CLAUDE_CODE_DISABLE_NONSTREAMING_FALLBACK) ||
|
|
2485
|
+
getFeatureValue_CACHED_MAY_BE_STALE(
|
|
2486
|
+
'tengu_disable_streaming_to_non_streaming_fallback',
|
|
2487
|
+
false,
|
|
2488
|
+
)
|
|
2489
|
+
|
|
2490
|
+
if (disableFallback) {
|
|
2491
|
+
logForDebugging(
|
|
2492
|
+
`Error streaming (non-streaming fallback disabled): ${errorMessage(streamingError)}`,
|
|
2493
|
+
{ level: 'error' },
|
|
2494
|
+
)
|
|
2495
|
+
logEvent('tengu_streaming_fallback_to_non_streaming', {
|
|
2496
|
+
model:
|
|
2497
|
+
options.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2498
|
+
error:
|
|
2499
|
+
streamingError instanceof Error
|
|
2500
|
+
? (streamingError.name as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
|
|
2501
|
+
: (String(
|
|
2502
|
+
streamingError,
|
|
2503
|
+
) as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS),
|
|
2504
|
+
attemptNumber,
|
|
2505
|
+
maxOutputTokens,
|
|
2506
|
+
thinkingType:
|
|
2507
|
+
thinkingConfig.type as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2508
|
+
fallback_disabled: true,
|
|
2509
|
+
request_id: (streamRequestId ??
|
|
2510
|
+
'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2511
|
+
fallback_cause: (streamIdleAborted
|
|
2512
|
+
? 'watchdog'
|
|
2513
|
+
: 'other') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2514
|
+
})
|
|
2515
|
+
throw streamingError
|
|
2516
|
+
}
|
|
2517
|
+
|
|
2518
|
+
logForDebugging(
|
|
2519
|
+
`Error streaming, falling back to non-streaming mode: ${errorMessage(streamingError)}`,
|
|
2520
|
+
{ level: 'error' },
|
|
2521
|
+
)
|
|
2522
|
+
didFallBackToNonStreaming = true
|
|
2523
|
+
if (options.onStreamingFallback) {
|
|
2524
|
+
options.onStreamingFallback()
|
|
2525
|
+
}
|
|
2526
|
+
|
|
2527
|
+
logEvent('tengu_streaming_fallback_to_non_streaming', {
|
|
2528
|
+
model:
|
|
2529
|
+
options.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2530
|
+
error:
|
|
2531
|
+
streamingError instanceof Error
|
|
2532
|
+
? (streamingError.name as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
|
|
2533
|
+
: (String(
|
|
2534
|
+
streamingError,
|
|
2535
|
+
) as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS),
|
|
2536
|
+
attemptNumber,
|
|
2537
|
+
maxOutputTokens,
|
|
2538
|
+
thinkingType:
|
|
2539
|
+
thinkingConfig.type as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2540
|
+
fallback_disabled: false,
|
|
2541
|
+
request_id: (streamRequestId ??
|
|
2542
|
+
'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2543
|
+
fallback_cause: (streamIdleAborted
|
|
2544
|
+
? 'watchdog'
|
|
2545
|
+
: 'other') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2546
|
+
})
|
|
2547
|
+
|
|
2548
|
+
// Fall back to non-streaming mode with retries.
|
|
2549
|
+
// If the streaming failure was itself a 529, count it toward the
|
|
2550
|
+
// consecutive-529 budget so total 529s-before-model-fallback is the
|
|
2551
|
+
// same whether the overload was hit in streaming or non-streaming mode.
|
|
2552
|
+
// This is a speculative fix for https://github.com/anthropics/claude-code/issues/1513
|
|
2553
|
+
// Instrumentation: proves executeNonStreamingRequest was entered (vs. the
|
|
2554
|
+
// fallback event firing but the call itself hanging at dispatch).
|
|
2555
|
+
logForDiagnosticsNoPII('info', 'cli_nonstreaming_fallback_started')
|
|
2556
|
+
logEvent('tengu_nonstreaming_fallback_started', {
|
|
2557
|
+
request_id: (streamRequestId ??
|
|
2558
|
+
'unknown') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2559
|
+
model:
|
|
2560
|
+
options.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2561
|
+
fallback_cause: (streamIdleAborted
|
|
2562
|
+
? 'watchdog'
|
|
2563
|
+
: 'other') as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2564
|
+
})
|
|
2565
|
+
const result = yield* executeNonStreamingRequest(
|
|
2566
|
+
{ model: options.model, source: options.querySource },
|
|
2567
|
+
{
|
|
2568
|
+
model: options.model,
|
|
2569
|
+
fallbackModel: options.fallbackModel,
|
|
2570
|
+
thinkingConfig,
|
|
2571
|
+
...(isFastModeEnabled() && { fastMode: isFastMode }),
|
|
2572
|
+
signal,
|
|
2573
|
+
initialConsecutive529Errors: is529Error(streamingError) ? 1 : 0,
|
|
2574
|
+
querySource: options.querySource,
|
|
2575
|
+
},
|
|
2576
|
+
paramsFromContext,
|
|
2577
|
+
(attempt, _startTime, tokens) => {
|
|
2578
|
+
attemptNumber = attempt
|
|
2579
|
+
maxOutputTokens = tokens
|
|
2580
|
+
},
|
|
2581
|
+
params => captureAPIRequest(params, options.querySource),
|
|
2582
|
+
streamRequestId,
|
|
2583
|
+
)
|
|
2584
|
+
|
|
2585
|
+
const m: AssistantMessage = {
|
|
2586
|
+
message: {
|
|
2587
|
+
...result,
|
|
2588
|
+
content: normalizeContentFromAPI(
|
|
2589
|
+
result.content,
|
|
2590
|
+
tools,
|
|
2591
|
+
options.agentId,
|
|
2592
|
+
),
|
|
2593
|
+
},
|
|
2594
|
+
requestId: streamRequestId ?? undefined,
|
|
2595
|
+
type: 'assistant',
|
|
2596
|
+
uuid: randomUUID(),
|
|
2597
|
+
timestamp: new Date().toISOString(),
|
|
2598
|
+
...(process.env.USER_TYPE === 'ant' &&
|
|
2599
|
+
research !== undefined && {
|
|
2600
|
+
research,
|
|
2601
|
+
}),
|
|
2602
|
+
...(advisorModel && {
|
|
2603
|
+
advisorModel,
|
|
2604
|
+
}),
|
|
2605
|
+
}
|
|
2606
|
+
newMessages.push(m)
|
|
2607
|
+
fallbackMessage = m
|
|
2608
|
+
yield m
|
|
2609
|
+
} finally {
|
|
2610
|
+
clearStreamIdleTimers()
|
|
2611
|
+
}
|
|
2612
|
+
} catch (errorFromRetry) {
|
|
2613
|
+
// FallbackTriggeredError must propagate to query.ts, which performs the
|
|
2614
|
+
// actual model switch. Swallowing it here would turn the fallback into a
|
|
2615
|
+
// no-op — the user would just see "Model fallback triggered: X -> Y" as
|
|
2616
|
+
// an error message with no actual retry on the fallback model.
|
|
2617
|
+
if (errorFromRetry instanceof FallbackTriggeredError) {
|
|
2618
|
+
throw errorFromRetry
|
|
2619
|
+
}
|
|
2620
|
+
|
|
2621
|
+
// Check if this is a 404 error during stream creation that should trigger
|
|
2622
|
+
// non-streaming fallback. This handles gateways that return 404 for streaming
|
|
2623
|
+
// endpoints but work fine with non-streaming. Before v2.1.8, BetaMessageStream
|
|
2624
|
+
// threw 404s during iteration (caught by inner catch with fallback), but now
|
|
2625
|
+
// with raw streams, 404s are thrown during creation (caught here).
|
|
2626
|
+
const is404StreamCreationError =
|
|
2627
|
+
!didFallBackToNonStreaming &&
|
|
2628
|
+
errorFromRetry instanceof CannotRetryError &&
|
|
2629
|
+
errorFromRetry.originalError instanceof APIError &&
|
|
2630
|
+
errorFromRetry.originalError.status === 404
|
|
2631
|
+
|
|
2632
|
+
if (is404StreamCreationError) {
|
|
2633
|
+
// 404 is thrown at .withResponse() before streamRequestId is assigned,
|
|
2634
|
+
// and CannotRetryError means every retry failed — so grab the failed
|
|
2635
|
+
// request's ID from the error header instead.
|
|
2636
|
+
const failedRequestId =
|
|
2637
|
+
(errorFromRetry.originalError as APIError).requestID ?? 'unknown'
|
|
2638
|
+
logForDebugging(
|
|
2639
|
+
'Streaming endpoint returned 404, falling back to non-streaming mode',
|
|
2640
|
+
{ level: 'warn' },
|
|
2641
|
+
)
|
|
2642
|
+
didFallBackToNonStreaming = true
|
|
2643
|
+
if (options.onStreamingFallback) {
|
|
2644
|
+
options.onStreamingFallback()
|
|
2645
|
+
}
|
|
2646
|
+
|
|
2647
|
+
logEvent('tengu_streaming_fallback_to_non_streaming', {
|
|
2648
|
+
model:
|
|
2649
|
+
options.model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2650
|
+
error:
|
|
2651
|
+
'404_stream_creation' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2652
|
+
attemptNumber,
|
|
2653
|
+
maxOutputTokens,
|
|
2654
|
+
thinkingType:
|
|
2655
|
+
thinkingConfig.type as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2656
|
+
request_id:
|
|
2657
|
+
failedRequestId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2658
|
+
fallback_cause:
|
|
2659
|
+
'404_stream_creation' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
2660
|
+
})
|
|
2661
|
+
|
|
2662
|
+
try {
|
|
2663
|
+
// Fall back to non-streaming mode
|
|
2664
|
+
const result = yield* executeNonStreamingRequest(
|
|
2665
|
+
{ model: options.model, source: options.querySource },
|
|
2666
|
+
{
|
|
2667
|
+
model: options.model,
|
|
2668
|
+
fallbackModel: options.fallbackModel,
|
|
2669
|
+
thinkingConfig,
|
|
2670
|
+
...(isFastModeEnabled() && { fastMode: isFastMode }),
|
|
2671
|
+
signal,
|
|
2672
|
+
},
|
|
2673
|
+
paramsFromContext,
|
|
2674
|
+
(attempt, _startTime, tokens) => {
|
|
2675
|
+
attemptNumber = attempt
|
|
2676
|
+
maxOutputTokens = tokens
|
|
2677
|
+
},
|
|
2678
|
+
params => captureAPIRequest(params, options.querySource),
|
|
2679
|
+
failedRequestId,
|
|
2680
|
+
)
|
|
2681
|
+
|
|
2682
|
+
const m: AssistantMessage = {
|
|
2683
|
+
message: {
|
|
2684
|
+
...result,
|
|
2685
|
+
content: normalizeContentFromAPI(
|
|
2686
|
+
result.content,
|
|
2687
|
+
tools,
|
|
2688
|
+
options.agentId,
|
|
2689
|
+
),
|
|
2690
|
+
},
|
|
2691
|
+
requestId: streamRequestId ?? undefined,
|
|
2692
|
+
type: 'assistant',
|
|
2693
|
+
uuid: randomUUID(),
|
|
2694
|
+
timestamp: new Date().toISOString(),
|
|
2695
|
+
...(process.env.USER_TYPE === 'ant' &&
|
|
2696
|
+
research !== undefined && { research }),
|
|
2697
|
+
...(advisorModel && { advisorModel }),
|
|
2698
|
+
}
|
|
2699
|
+
newMessages.push(m)
|
|
2700
|
+
fallbackMessage = m
|
|
2701
|
+
yield m
|
|
2702
|
+
|
|
2703
|
+
// Continue to success logging below
|
|
2704
|
+
} catch (fallbackError) {
|
|
2705
|
+
// Propagate model-fallback signal to query.ts (see comment above).
|
|
2706
|
+
if (fallbackError instanceof FallbackTriggeredError) {
|
|
2707
|
+
throw fallbackError
|
|
2708
|
+
}
|
|
2709
|
+
|
|
2710
|
+
// Fallback also failed, handle as normal error
|
|
2711
|
+
logForDebugging(
|
|
2712
|
+
`Non-streaming fallback also failed: ${errorMessage(fallbackError)}`,
|
|
2713
|
+
{ level: 'error' },
|
|
2714
|
+
)
|
|
2715
|
+
|
|
2716
|
+
let error = fallbackError
|
|
2717
|
+
let errorModel = options.model
|
|
2718
|
+
if (fallbackError instanceof CannotRetryError) {
|
|
2719
|
+
error = fallbackError.originalError
|
|
2720
|
+
errorModel = fallbackError.retryContext.model
|
|
2721
|
+
}
|
|
2722
|
+
|
|
2723
|
+
if (error instanceof APIError) {
|
|
2724
|
+
extractQuotaStatusFromError(error)
|
|
2725
|
+
}
|
|
2726
|
+
|
|
2727
|
+
const requestId =
|
|
2728
|
+
streamRequestId ||
|
|
2729
|
+
(error instanceof APIError ? error.requestID : undefined) ||
|
|
2730
|
+
(error instanceof APIError
|
|
2731
|
+
? (error.error as { request_id?: string })?.request_id
|
|
2732
|
+
: undefined)
|
|
2733
|
+
|
|
2734
|
+
logAPIError({
|
|
2735
|
+
error,
|
|
2736
|
+
model: errorModel,
|
|
2737
|
+
messageCount: messagesForAPI.length,
|
|
2738
|
+
messageTokens: tokenCountFromLastAPIResponse(messagesForAPI),
|
|
2739
|
+
durationMs: Date.now() - start,
|
|
2740
|
+
durationMsIncludingRetries: Date.now() - startIncludingRetries,
|
|
2741
|
+
attempt: attemptNumber,
|
|
2742
|
+
requestId,
|
|
2743
|
+
clientRequestId,
|
|
2744
|
+
didFallBackToNonStreaming,
|
|
2745
|
+
queryTracking: options.queryTracking,
|
|
2746
|
+
querySource: options.querySource,
|
|
2747
|
+
llmSpan,
|
|
2748
|
+
fastMode: isFastModeRequest,
|
|
2749
|
+
previousRequestId,
|
|
2750
|
+
})
|
|
2751
|
+
|
|
2752
|
+
if (error instanceof APIUserAbortError) {
|
|
2753
|
+
releaseStreamResources()
|
|
2754
|
+
return
|
|
2755
|
+
}
|
|
2756
|
+
|
|
2757
|
+
yield getAssistantMessageFromError(error, errorModel, {
|
|
2758
|
+
messages,
|
|
2759
|
+
messagesForAPI,
|
|
2760
|
+
})
|
|
2761
|
+
releaseStreamResources()
|
|
2762
|
+
return
|
|
2763
|
+
}
|
|
2764
|
+
} else {
|
|
2765
|
+
// Original error handling for non-404 errors
|
|
2766
|
+
logForDebugging(`Error in API request: ${errorMessage(errorFromRetry)}`, {
|
|
2767
|
+
level: 'error',
|
|
2768
|
+
})
|
|
2769
|
+
|
|
2770
|
+
let error = errorFromRetry
|
|
2771
|
+
let errorModel = options.model
|
|
2772
|
+
if (errorFromRetry instanceof CannotRetryError) {
|
|
2773
|
+
error = errorFromRetry.originalError
|
|
2774
|
+
errorModel = errorFromRetry.retryContext.model
|
|
2775
|
+
}
|
|
2776
|
+
|
|
2777
|
+
// Extract quota status from error headers if it's a rate limit error
|
|
2778
|
+
if (error instanceof APIError) {
|
|
2779
|
+
extractQuotaStatusFromError(error)
|
|
2780
|
+
}
|
|
2781
|
+
|
|
2782
|
+
// Extract requestId from stream, error header, or error body
|
|
2783
|
+
const requestId =
|
|
2784
|
+
streamRequestId ||
|
|
2785
|
+
(error instanceof APIError ? error.requestID : undefined) ||
|
|
2786
|
+
(error instanceof APIError
|
|
2787
|
+
? (error.error as { request_id?: string })?.request_id
|
|
2788
|
+
: undefined)
|
|
2789
|
+
|
|
2790
|
+
logAPIError({
|
|
2791
|
+
error,
|
|
2792
|
+
model: errorModel,
|
|
2793
|
+
messageCount: messagesForAPI.length,
|
|
2794
|
+
messageTokens: tokenCountFromLastAPIResponse(messagesForAPI),
|
|
2795
|
+
durationMs: Date.now() - start,
|
|
2796
|
+
durationMsIncludingRetries: Date.now() - startIncludingRetries,
|
|
2797
|
+
attempt: attemptNumber,
|
|
2798
|
+
requestId,
|
|
2799
|
+
clientRequestId,
|
|
2800
|
+
didFallBackToNonStreaming,
|
|
2801
|
+
queryTracking: options.queryTracking,
|
|
2802
|
+
querySource: options.querySource,
|
|
2803
|
+
llmSpan,
|
|
2804
|
+
fastMode: isFastModeRequest,
|
|
2805
|
+
previousRequestId,
|
|
2806
|
+
})
|
|
2807
|
+
|
|
2808
|
+
// Don't yield an assistant error message for user aborts
|
|
2809
|
+
// The interruption message is handled in query.ts
|
|
2810
|
+
if (error instanceof APIUserAbortError) {
|
|
2811
|
+
releaseStreamResources()
|
|
2812
|
+
return
|
|
2813
|
+
}
|
|
2814
|
+
|
|
2815
|
+
yield getAssistantMessageFromError(error, errorModel, {
|
|
2816
|
+
messages,
|
|
2817
|
+
messagesForAPI,
|
|
2818
|
+
})
|
|
2819
|
+
releaseStreamResources()
|
|
2820
|
+
return
|
|
2821
|
+
}
|
|
2822
|
+
} finally {
|
|
2823
|
+
stopSessionActivity('api_call')
|
|
2824
|
+
// Must be in the finally block: if the generator is terminated early
|
|
2825
|
+
// via .return() (e.g. consumer breaks out of for-await-of, or query.ts
|
|
2826
|
+
// encounters an abort), code after the try/finally never executes.
|
|
2827
|
+
// Without this, the Response object's native TLS/socket buffers leak
|
|
2828
|
+
// until the generator itself is GC'd (see GH #32920).
|
|
2829
|
+
releaseStreamResources()
|
|
2830
|
+
|
|
2831
|
+
// Non-streaming fallback cost: the streaming path tracks cost in the
|
|
2832
|
+
// message_delta handler before any yield. Fallback pushes to newMessages
|
|
2833
|
+
// then yields, so tracking must be here to survive .return() at the yield.
|
|
2834
|
+
if (fallbackMessage) {
|
|
2835
|
+
const fallbackUsage = fallbackMessage.message.usage
|
|
2836
|
+
usage = updateUsage(EMPTY_USAGE, fallbackUsage)
|
|
2837
|
+
stopReason = fallbackMessage.message.stop_reason
|
|
2838
|
+
const fallbackCost = calculateUSDCost(resolvedModel, fallbackUsage)
|
|
2839
|
+
costUSD += addToTotalSessionCost(
|
|
2840
|
+
fallbackCost,
|
|
2841
|
+
fallbackUsage,
|
|
2842
|
+
options.model,
|
|
2843
|
+
)
|
|
2844
|
+
}
|
|
2845
|
+
}
|
|
2846
|
+
|
|
2847
|
+
// Mark all registered tools as sent to API so they become eligible for deletion
|
|
2848
|
+
if (feature('CACHED_MICROCOMPACT') && cachedMCEnabled) {
|
|
2849
|
+
markToolsSentToAPIState()
|
|
2850
|
+
}
|
|
2851
|
+
|
|
2852
|
+
// Track the last requestId for the main conversation chain so shutdown
|
|
2853
|
+
// can send a cache eviction hint to inference. Exclude backgrounded
|
|
2854
|
+
// sessions (Ctrl+B) which share the repl_main_thread querySource but
|
|
2855
|
+
// run inside an agent context — they are independent conversation chains
|
|
2856
|
+
// whose cache should not be evicted when the foreground session clears.
|
|
2857
|
+
if (
|
|
2858
|
+
streamRequestId &&
|
|
2859
|
+
!getAgentContext() &&
|
|
2860
|
+
(options.querySource.startsWith('repl_main_thread') ||
|
|
2861
|
+
options.querySource === 'sdk')
|
|
2862
|
+
) {
|
|
2863
|
+
setLastMainRequestId(streamRequestId)
|
|
2864
|
+
}
|
|
2865
|
+
|
|
2866
|
+
// Precompute scalars so the fire-and-forget .then() closure doesn't pin the
|
|
2867
|
+
// full messagesForAPI array (the entire conversation up to the context window
|
|
2868
|
+
// limit) until getToolPermissionContext() resolves.
|
|
2869
|
+
const logMessageCount = messagesForAPI.length
|
|
2870
|
+
const logMessageTokens = tokenCountFromLastAPIResponse(messagesForAPI)
|
|
2871
|
+
void options.getToolPermissionContext().then(permissionContext => {
|
|
2872
|
+
logAPISuccessAndDuration({
|
|
2873
|
+
model:
|
|
2874
|
+
newMessages[0]?.message.model ?? partialMessage?.model ?? options.model,
|
|
2875
|
+
preNormalizedModel: options.model,
|
|
2876
|
+
usage,
|
|
2877
|
+
start,
|
|
2878
|
+
startIncludingRetries,
|
|
2879
|
+
attempt: attemptNumber,
|
|
2880
|
+
messageCount: logMessageCount,
|
|
2881
|
+
messageTokens: logMessageTokens,
|
|
2882
|
+
requestId: streamRequestId ?? null,
|
|
2883
|
+
stopReason,
|
|
2884
|
+
ttftMs,
|
|
2885
|
+
didFallBackToNonStreaming,
|
|
2886
|
+
querySource: options.querySource,
|
|
2887
|
+
headers: responseHeaders,
|
|
2888
|
+
costUSD,
|
|
2889
|
+
queryTracking: options.queryTracking,
|
|
2890
|
+
permissionMode: permissionContext.mode,
|
|
2891
|
+
// Pass newMessages for beta tracing - extraction happens in logging.ts
|
|
2892
|
+
// only when beta tracing is enabled
|
|
2893
|
+
newMessages,
|
|
2894
|
+
llmSpan,
|
|
2895
|
+
globalCacheStrategy,
|
|
2896
|
+
requestSetupMs: start - startIncludingRetries,
|
|
2897
|
+
attemptStartTimes,
|
|
2898
|
+
fastMode: isFastModeRequest,
|
|
2899
|
+
previousRequestId,
|
|
2900
|
+
betas: lastRequestBetas,
|
|
2901
|
+
})
|
|
2902
|
+
})
|
|
2903
|
+
|
|
2904
|
+
// Defensive: also release on normal completion (no-op if finally already ran).
|
|
2905
|
+
releaseStreamResources()
|
|
2906
|
+
}
|
|
2907
|
+
|
|
2908
|
+
/**
|
|
2909
|
+
* Cleans up stream resources to prevent memory leaks.
|
|
2910
|
+
* @internal Exported for testing
|
|
2911
|
+
*/
|
|
2912
|
+
export function cleanupStream(
|
|
2913
|
+
stream: Stream<BetaRawMessageStreamEvent> | undefined,
|
|
2914
|
+
): void {
|
|
2915
|
+
if (!stream) {
|
|
2916
|
+
return
|
|
2917
|
+
}
|
|
2918
|
+
try {
|
|
2919
|
+
// Abort the stream via its controller if not already aborted
|
|
2920
|
+
if (!stream.controller.signal.aborted) {
|
|
2921
|
+
stream.controller.abort()
|
|
2922
|
+
}
|
|
2923
|
+
} catch {
|
|
2924
|
+
// Ignore - stream may already be closed
|
|
2925
|
+
}
|
|
2926
|
+
}
|
|
2927
|
+
|
|
2928
|
+
/**
|
|
2929
|
+
* Updates usage statistics with new values from streaming API events.
|
|
2930
|
+
* Note: Anthropic's streaming API provides cumulative usage totals, not incremental deltas.
|
|
2931
|
+
* Each event contains the complete usage up to that point in the stream.
|
|
2932
|
+
*
|
|
2933
|
+
* Input-related tokens (input_tokens, cache_creation_input_tokens, cache_read_input_tokens)
|
|
2934
|
+
* are typically set in message_start and remain constant. message_delta events may send
|
|
2935
|
+
* explicit 0 values for these fields, which should not overwrite the values from message_start.
|
|
2936
|
+
* We only update these fields if they have a non-null, non-zero value.
|
|
2937
|
+
*/
|
|
2938
|
+
export function updateUsage(
|
|
2939
|
+
usage: Readonly<NonNullableUsage>,
|
|
2940
|
+
partUsage: BetaMessageDeltaUsage | undefined,
|
|
2941
|
+
): NonNullableUsage {
|
|
2942
|
+
if (!partUsage) {
|
|
2943
|
+
return { ...usage }
|
|
2944
|
+
}
|
|
2945
|
+
return {
|
|
2946
|
+
input_tokens:
|
|
2947
|
+
partUsage.input_tokens !== null && partUsage.input_tokens > 0
|
|
2948
|
+
? partUsage.input_tokens
|
|
2949
|
+
: usage.input_tokens,
|
|
2950
|
+
cache_creation_input_tokens:
|
|
2951
|
+
partUsage.cache_creation_input_tokens !== null &&
|
|
2952
|
+
partUsage.cache_creation_input_tokens > 0
|
|
2953
|
+
? partUsage.cache_creation_input_tokens
|
|
2954
|
+
: usage.cache_creation_input_tokens,
|
|
2955
|
+
cache_read_input_tokens:
|
|
2956
|
+
partUsage.cache_read_input_tokens !== null &&
|
|
2957
|
+
partUsage.cache_read_input_tokens > 0
|
|
2958
|
+
? partUsage.cache_read_input_tokens
|
|
2959
|
+
: usage.cache_read_input_tokens,
|
|
2960
|
+
output_tokens: partUsage.output_tokens ?? usage.output_tokens,
|
|
2961
|
+
server_tool_use: {
|
|
2962
|
+
web_search_requests:
|
|
2963
|
+
partUsage.server_tool_use?.web_search_requests ??
|
|
2964
|
+
usage.server_tool_use.web_search_requests,
|
|
2965
|
+
web_fetch_requests:
|
|
2966
|
+
partUsage.server_tool_use?.web_fetch_requests ??
|
|
2967
|
+
usage.server_tool_use.web_fetch_requests,
|
|
2968
|
+
},
|
|
2969
|
+
service_tier: usage.service_tier,
|
|
2970
|
+
cache_creation: {
|
|
2971
|
+
// SDK type BetaMessageDeltaUsage is missing cache_creation, but it's real!
|
|
2972
|
+
ephemeral_1h_input_tokens:
|
|
2973
|
+
(partUsage as BetaUsage).cache_creation?.ephemeral_1h_input_tokens ??
|
|
2974
|
+
usage.cache_creation.ephemeral_1h_input_tokens,
|
|
2975
|
+
ephemeral_5m_input_tokens:
|
|
2976
|
+
(partUsage as BetaUsage).cache_creation?.ephemeral_5m_input_tokens ??
|
|
2977
|
+
usage.cache_creation.ephemeral_5m_input_tokens,
|
|
2978
|
+
},
|
|
2979
|
+
// cache_deleted_input_tokens: returned by the API when cache editing
|
|
2980
|
+
// deletes KV cache content, but not in SDK types. Kept off NonNullableUsage
|
|
2981
|
+
// so the string is eliminated from external builds by dead code elimination.
|
|
2982
|
+
// Uses the same > 0 guard as other token fields to prevent message_delta
|
|
2983
|
+
// from overwriting the real value with 0.
|
|
2984
|
+
...(feature('CACHED_MICROCOMPACT')
|
|
2985
|
+
? {
|
|
2986
|
+
cache_deleted_input_tokens:
|
|
2987
|
+
(partUsage as unknown as { cache_deleted_input_tokens?: number })
|
|
2988
|
+
.cache_deleted_input_tokens != null &&
|
|
2989
|
+
(partUsage as unknown as { cache_deleted_input_tokens: number })
|
|
2990
|
+
.cache_deleted_input_tokens > 0
|
|
2991
|
+
? (partUsage as unknown as { cache_deleted_input_tokens: number })
|
|
2992
|
+
.cache_deleted_input_tokens
|
|
2993
|
+
: ((usage as unknown as { cache_deleted_input_tokens?: number })
|
|
2994
|
+
.cache_deleted_input_tokens ?? 0),
|
|
2995
|
+
}
|
|
2996
|
+
: {}),
|
|
2997
|
+
inference_geo: usage.inference_geo,
|
|
2998
|
+
iterations: partUsage.iterations ?? usage.iterations,
|
|
2999
|
+
speed: (partUsage as BetaUsage).speed ?? usage.speed,
|
|
3000
|
+
}
|
|
3001
|
+
}
|
|
3002
|
+
|
|
3003
|
+
/**
|
|
3004
|
+
* Accumulates usage from one message into a total usage object.
|
|
3005
|
+
* Used to track cumulative usage across multiple assistant turns.
|
|
3006
|
+
*/
|
|
3007
|
+
export function accumulateUsage(
|
|
3008
|
+
totalUsage: Readonly<NonNullableUsage>,
|
|
3009
|
+
messageUsage: Readonly<NonNullableUsage>,
|
|
3010
|
+
): NonNullableUsage {
|
|
3011
|
+
return {
|
|
3012
|
+
input_tokens: totalUsage.input_tokens + messageUsage.input_tokens,
|
|
3013
|
+
cache_creation_input_tokens:
|
|
3014
|
+
totalUsage.cache_creation_input_tokens +
|
|
3015
|
+
messageUsage.cache_creation_input_tokens,
|
|
3016
|
+
cache_read_input_tokens:
|
|
3017
|
+
totalUsage.cache_read_input_tokens + messageUsage.cache_read_input_tokens,
|
|
3018
|
+
output_tokens: totalUsage.output_tokens + messageUsage.output_tokens,
|
|
3019
|
+
server_tool_use: {
|
|
3020
|
+
web_search_requests:
|
|
3021
|
+
totalUsage.server_tool_use.web_search_requests +
|
|
3022
|
+
messageUsage.server_tool_use.web_search_requests,
|
|
3023
|
+
web_fetch_requests:
|
|
3024
|
+
totalUsage.server_tool_use.web_fetch_requests +
|
|
3025
|
+
messageUsage.server_tool_use.web_fetch_requests,
|
|
3026
|
+
},
|
|
3027
|
+
service_tier: messageUsage.service_tier, // Use the most recent service tier
|
|
3028
|
+
cache_creation: {
|
|
3029
|
+
ephemeral_1h_input_tokens:
|
|
3030
|
+
totalUsage.cache_creation.ephemeral_1h_input_tokens +
|
|
3031
|
+
messageUsage.cache_creation.ephemeral_1h_input_tokens,
|
|
3032
|
+
ephemeral_5m_input_tokens:
|
|
3033
|
+
totalUsage.cache_creation.ephemeral_5m_input_tokens +
|
|
3034
|
+
messageUsage.cache_creation.ephemeral_5m_input_tokens,
|
|
3035
|
+
},
|
|
3036
|
+
// See comment in updateUsage — field is not on NonNullableUsage to keep
|
|
3037
|
+
// the string out of external builds.
|
|
3038
|
+
...(feature('CACHED_MICROCOMPACT')
|
|
3039
|
+
? {
|
|
3040
|
+
cache_deleted_input_tokens:
|
|
3041
|
+
((totalUsage as unknown as { cache_deleted_input_tokens?: number })
|
|
3042
|
+
.cache_deleted_input_tokens ?? 0) +
|
|
3043
|
+
((
|
|
3044
|
+
messageUsage as unknown as { cache_deleted_input_tokens?: number }
|
|
3045
|
+
).cache_deleted_input_tokens ?? 0),
|
|
3046
|
+
}
|
|
3047
|
+
: {}),
|
|
3048
|
+
inference_geo: messageUsage.inference_geo, // Use the most recent
|
|
3049
|
+
iterations: messageUsage.iterations, // Use the most recent
|
|
3050
|
+
speed: messageUsage.speed, // Use the most recent
|
|
3051
|
+
}
|
|
3052
|
+
}
|
|
3053
|
+
|
|
3054
|
+
function isToolResultBlock(
|
|
3055
|
+
block: unknown,
|
|
3056
|
+
): block is { type: 'tool_result'; tool_use_id: string } {
|
|
3057
|
+
return (
|
|
3058
|
+
block !== null &&
|
|
3059
|
+
typeof block === 'object' &&
|
|
3060
|
+
'type' in block &&
|
|
3061
|
+
(block as { type: string }).type === 'tool_result' &&
|
|
3062
|
+
'tool_use_id' in block
|
|
3063
|
+
)
|
|
3064
|
+
}
|
|
3065
|
+
|
|
3066
|
+
type CachedMCEditsBlock = {
|
|
3067
|
+
type: 'cache_edits'
|
|
3068
|
+
edits: { type: 'delete'; cache_reference: string }[]
|
|
3069
|
+
}
|
|
3070
|
+
|
|
3071
|
+
type CachedMCPinnedEdits = {
|
|
3072
|
+
userMessageIndex: number
|
|
3073
|
+
block: CachedMCEditsBlock
|
|
3074
|
+
}
|
|
3075
|
+
|
|
3076
|
+
// Exported for testing cache_reference placement constraints
|
|
3077
|
+
export function addCacheBreakpoints(
|
|
3078
|
+
messages: (UserMessage | AssistantMessage)[],
|
|
3079
|
+
enablePromptCaching: boolean,
|
|
3080
|
+
querySource?: QuerySource,
|
|
3081
|
+
useCachedMC = false,
|
|
3082
|
+
newCacheEdits?: CachedMCEditsBlock | null,
|
|
3083
|
+
pinnedEdits?: CachedMCPinnedEdits[],
|
|
3084
|
+
skipCacheWrite = false,
|
|
3085
|
+
): MessageParam[] {
|
|
3086
|
+
logEvent('tengu_api_cache_breakpoints', {
|
|
3087
|
+
totalMessageCount: messages.length,
|
|
3088
|
+
cachingEnabled: enablePromptCaching,
|
|
3089
|
+
skipCacheWrite,
|
|
3090
|
+
})
|
|
3091
|
+
|
|
3092
|
+
// Exactly one message-level cache_control marker per request. Mycro's
|
|
3093
|
+
// turn-to-turn eviction (page_manager/index.rs: Index::insert) frees
|
|
3094
|
+
// local-attention KV pages at any cached prefix position NOT in
|
|
3095
|
+
// cache_store_int_token_boundaries. With two markers the second-to-last
|
|
3096
|
+
// position is protected and its locals survive an extra turn even though
|
|
3097
|
+
// nothing will ever resume from there — with one marker they're freed
|
|
3098
|
+
// immediately. For fire-and-forget forks (skipCacheWrite) we shift the
|
|
3099
|
+
// marker to the second-to-last message: that's the last shared-prefix
|
|
3100
|
+
// point, so the write is a no-op merge on mycro (entry already exists)
|
|
3101
|
+
// and the fork doesn't leave its own tail in the KVCC. Dense pages are
|
|
3102
|
+
// refcounted and survive via the new hash either way.
|
|
3103
|
+
const markerIndex = skipCacheWrite ? messages.length - 2 : messages.length - 1
|
|
3104
|
+
const result = messages.map((msg, index) => {
|
|
3105
|
+
const addCache = index === markerIndex
|
|
3106
|
+
if (msg.type === 'user') {
|
|
3107
|
+
return userMessageToMessageParam(
|
|
3108
|
+
msg,
|
|
3109
|
+
addCache,
|
|
3110
|
+
enablePromptCaching,
|
|
3111
|
+
querySource,
|
|
3112
|
+
)
|
|
3113
|
+
}
|
|
3114
|
+
return assistantMessageToMessageParam(
|
|
3115
|
+
msg,
|
|
3116
|
+
addCache,
|
|
3117
|
+
enablePromptCaching,
|
|
3118
|
+
querySource,
|
|
3119
|
+
)
|
|
3120
|
+
})
|
|
3121
|
+
|
|
3122
|
+
if (!useCachedMC) {
|
|
3123
|
+
return result
|
|
3124
|
+
}
|
|
3125
|
+
|
|
3126
|
+
// Track all cache_references being deleted to prevent duplicates across blocks.
|
|
3127
|
+
const seenDeleteRefs = new Set<string>()
|
|
3128
|
+
|
|
3129
|
+
// Helper to deduplicate a cache_edits block against already-seen deletions
|
|
3130
|
+
const deduplicateEdits = (block: CachedMCEditsBlock): CachedMCEditsBlock => {
|
|
3131
|
+
const uniqueEdits = block.edits.filter(edit => {
|
|
3132
|
+
if (seenDeleteRefs.has(edit.cache_reference)) {
|
|
3133
|
+
return false
|
|
3134
|
+
}
|
|
3135
|
+
seenDeleteRefs.add(edit.cache_reference)
|
|
3136
|
+
return true
|
|
3137
|
+
})
|
|
3138
|
+
return { ...block, edits: uniqueEdits }
|
|
3139
|
+
}
|
|
3140
|
+
|
|
3141
|
+
// Re-insert all previously-pinned cache_edits at their original positions
|
|
3142
|
+
for (const pinned of pinnedEdits ?? []) {
|
|
3143
|
+
const msg = result[pinned.userMessageIndex]
|
|
3144
|
+
if (msg && msg.role === 'user') {
|
|
3145
|
+
if (!Array.isArray(msg.content)) {
|
|
3146
|
+
msg.content = [{ type: 'text', text: msg.content as string }]
|
|
3147
|
+
}
|
|
3148
|
+
const dedupedBlock = deduplicateEdits(pinned.block)
|
|
3149
|
+
if (dedupedBlock.edits.length > 0) {
|
|
3150
|
+
insertBlockAfterToolResults(msg.content, dedupedBlock)
|
|
3151
|
+
}
|
|
3152
|
+
}
|
|
3153
|
+
}
|
|
3154
|
+
|
|
3155
|
+
// Insert new cache_edits into the last user message and pin them
|
|
3156
|
+
if (newCacheEdits && result.length > 0) {
|
|
3157
|
+
const dedupedNewEdits = deduplicateEdits(newCacheEdits)
|
|
3158
|
+
if (dedupedNewEdits.edits.length > 0) {
|
|
3159
|
+
for (let i = result.length - 1; i >= 0; i--) {
|
|
3160
|
+
const msg = result[i]
|
|
3161
|
+
if (msg && msg.role === 'user') {
|
|
3162
|
+
if (!Array.isArray(msg.content)) {
|
|
3163
|
+
msg.content = [{ type: 'text', text: msg.content as string }]
|
|
3164
|
+
}
|
|
3165
|
+
insertBlockAfterToolResults(msg.content, dedupedNewEdits)
|
|
3166
|
+
// Pin so this block is re-sent at the same position in future calls
|
|
3167
|
+
pinCacheEdits(i, newCacheEdits)
|
|
3168
|
+
|
|
3169
|
+
logForDebugging(
|
|
3170
|
+
`Added cache_edits block with ${dedupedNewEdits.edits.length} deletion(s) to message[${i}]: ${dedupedNewEdits.edits.map(e => e.cache_reference).join(', ')}`,
|
|
3171
|
+
)
|
|
3172
|
+
break
|
|
3173
|
+
}
|
|
3174
|
+
}
|
|
3175
|
+
}
|
|
3176
|
+
}
|
|
3177
|
+
|
|
3178
|
+
// Add cache_reference to tool_result blocks that are within the cached prefix.
|
|
3179
|
+
// Must be done AFTER cache_edits insertion since that modifies content arrays.
|
|
3180
|
+
if (enablePromptCaching) {
|
|
3181
|
+
// Find the last message containing a cache_control marker
|
|
3182
|
+
let lastCCMsg = -1
|
|
3183
|
+
for (let i = 0; i < result.length; i++) {
|
|
3184
|
+
const msg = result[i]!
|
|
3185
|
+
if (Array.isArray(msg.content)) {
|
|
3186
|
+
for (const block of msg.content) {
|
|
3187
|
+
if (block && typeof block === 'object' && 'cache_control' in block) {
|
|
3188
|
+
lastCCMsg = i
|
|
3189
|
+
}
|
|
3190
|
+
}
|
|
3191
|
+
}
|
|
3192
|
+
}
|
|
3193
|
+
|
|
3194
|
+
// Add cache_reference to tool_result blocks that are strictly before
|
|
3195
|
+
// the last cache_control marker. The API requires cache_reference to
|
|
3196
|
+
// appear "before or on" the last cache_control — we use strict "before"
|
|
3197
|
+
// to avoid edge cases where cache_edits splicing shifts block indices.
|
|
3198
|
+
//
|
|
3199
|
+
// Create new objects instead of mutating in-place to avoid contaminating
|
|
3200
|
+
// blocks reused by secondary queries that use models without cache_editing support.
|
|
3201
|
+
if (lastCCMsg >= 0) {
|
|
3202
|
+
for (let i = 0; i < lastCCMsg; i++) {
|
|
3203
|
+
const msg = result[i]!
|
|
3204
|
+
if (msg.role !== 'user' || !Array.isArray(msg.content)) {
|
|
3205
|
+
continue
|
|
3206
|
+
}
|
|
3207
|
+
let cloned = false
|
|
3208
|
+
for (let j = 0; j < msg.content.length; j++) {
|
|
3209
|
+
const block = msg.content[j]
|
|
3210
|
+
if (block && isToolResultBlock(block)) {
|
|
3211
|
+
if (!cloned) {
|
|
3212
|
+
msg.content = [...msg.content]
|
|
3213
|
+
cloned = true
|
|
3214
|
+
}
|
|
3215
|
+
msg.content[j] = Object.assign({}, block, {
|
|
3216
|
+
cache_reference: block.tool_use_id,
|
|
3217
|
+
})
|
|
3218
|
+
}
|
|
3219
|
+
}
|
|
3220
|
+
}
|
|
3221
|
+
}
|
|
3222
|
+
}
|
|
3223
|
+
|
|
3224
|
+
return result
|
|
3225
|
+
}
|
|
3226
|
+
|
|
3227
|
+
export function buildSystemPromptBlocks(
|
|
3228
|
+
systemPrompt: SystemPrompt,
|
|
3229
|
+
enablePromptCaching: boolean,
|
|
3230
|
+
options?: {
|
|
3231
|
+
skipGlobalCacheForSystemPrompt?: boolean
|
|
3232
|
+
querySource?: QuerySource
|
|
3233
|
+
},
|
|
3234
|
+
): TextBlockParam[] {
|
|
3235
|
+
// IMPORTANT: Do not add any more blocks for caching or you will get a 400
|
|
3236
|
+
return splitSysPromptPrefix(systemPrompt, {
|
|
3237
|
+
skipGlobalCacheForSystemPrompt: options?.skipGlobalCacheForSystemPrompt,
|
|
3238
|
+
}).map(block => {
|
|
3239
|
+
return {
|
|
3240
|
+
type: 'text' as const,
|
|
3241
|
+
text: block.text,
|
|
3242
|
+
...(enablePromptCaching &&
|
|
3243
|
+
block.cacheScope !== null && {
|
|
3244
|
+
cache_control: getCacheControl({
|
|
3245
|
+
scope: block.cacheScope,
|
|
3246
|
+
querySource: options?.querySource,
|
|
3247
|
+
}),
|
|
3248
|
+
}),
|
|
3249
|
+
}
|
|
3250
|
+
})
|
|
3251
|
+
}
|
|
3252
|
+
|
|
3253
|
+
type HaikuOptions = Omit<Options, 'model' | 'getToolPermissionContext'>
|
|
3254
|
+
|
|
3255
|
+
export async function queryHaiku({
|
|
3256
|
+
systemPrompt = asSystemPrompt([]),
|
|
3257
|
+
userPrompt,
|
|
3258
|
+
outputFormat,
|
|
3259
|
+
signal,
|
|
3260
|
+
options,
|
|
3261
|
+
}: {
|
|
3262
|
+
systemPrompt: SystemPrompt
|
|
3263
|
+
userPrompt: string
|
|
3264
|
+
outputFormat?: BetaJSONOutputFormat
|
|
3265
|
+
signal: AbortSignal
|
|
3266
|
+
options: HaikuOptions
|
|
3267
|
+
}): Promise<AssistantMessage> {
|
|
3268
|
+
const result = await withVCR(
|
|
3269
|
+
[
|
|
3270
|
+
createUserMessage({
|
|
3271
|
+
content: systemPrompt.map(text => ({ type: 'text', text })),
|
|
3272
|
+
}),
|
|
3273
|
+
createUserMessage({
|
|
3274
|
+
content: userPrompt,
|
|
3275
|
+
}),
|
|
3276
|
+
],
|
|
3277
|
+
async () => {
|
|
3278
|
+
const messages = [
|
|
3279
|
+
createUserMessage({
|
|
3280
|
+
content: userPrompt,
|
|
3281
|
+
}),
|
|
3282
|
+
]
|
|
3283
|
+
|
|
3284
|
+
const result = await queryModelWithoutStreaming({
|
|
3285
|
+
messages,
|
|
3286
|
+
systemPrompt,
|
|
3287
|
+
thinkingConfig: { type: 'disabled' },
|
|
3288
|
+
tools: [],
|
|
3289
|
+
signal,
|
|
3290
|
+
options: {
|
|
3291
|
+
...options,
|
|
3292
|
+
model: getSmallFastModel(),
|
|
3293
|
+
enablePromptCaching: options.enablePromptCaching ?? false,
|
|
3294
|
+
outputFormat,
|
|
3295
|
+
async getToolPermissionContext() {
|
|
3296
|
+
return getEmptyToolPermissionContext()
|
|
3297
|
+
},
|
|
3298
|
+
},
|
|
3299
|
+
})
|
|
3300
|
+
return [result]
|
|
3301
|
+
},
|
|
3302
|
+
)
|
|
3303
|
+
// We don't use streaming for Haiku so this is safe
|
|
3304
|
+
return result[0]! as AssistantMessage
|
|
3305
|
+
}
|
|
3306
|
+
|
|
3307
|
+
type QueryWithModelOptions = Omit<Options, 'getToolPermissionContext'>
|
|
3308
|
+
|
|
3309
|
+
/**
|
|
3310
|
+
* Query a specific model through the Claude Code infrastructure.
|
|
3311
|
+
* This goes through the full query pipeline including proper authentication,
|
|
3312
|
+
* betas, and headers - unlike direct API calls.
|
|
3313
|
+
*/
|
|
3314
|
+
export async function queryWithModel({
|
|
3315
|
+
systemPrompt = asSystemPrompt([]),
|
|
3316
|
+
userPrompt,
|
|
3317
|
+
outputFormat,
|
|
3318
|
+
signal,
|
|
3319
|
+
options,
|
|
3320
|
+
}: {
|
|
3321
|
+
systemPrompt: SystemPrompt
|
|
3322
|
+
userPrompt: string
|
|
3323
|
+
outputFormat?: BetaJSONOutputFormat
|
|
3324
|
+
signal: AbortSignal
|
|
3325
|
+
options: QueryWithModelOptions
|
|
3326
|
+
}): Promise<AssistantMessage> {
|
|
3327
|
+
const result = await withVCR(
|
|
3328
|
+
[
|
|
3329
|
+
createUserMessage({
|
|
3330
|
+
content: systemPrompt.map(text => ({ type: 'text', text })),
|
|
3331
|
+
}),
|
|
3332
|
+
createUserMessage({
|
|
3333
|
+
content: userPrompt,
|
|
3334
|
+
}),
|
|
3335
|
+
],
|
|
3336
|
+
async () => {
|
|
3337
|
+
const messages = [
|
|
3338
|
+
createUserMessage({
|
|
3339
|
+
content: userPrompt,
|
|
3340
|
+
}),
|
|
3341
|
+
]
|
|
3342
|
+
|
|
3343
|
+
const result = await queryModelWithoutStreaming({
|
|
3344
|
+
messages,
|
|
3345
|
+
systemPrompt,
|
|
3346
|
+
thinkingConfig: { type: 'disabled' },
|
|
3347
|
+
tools: [],
|
|
3348
|
+
signal,
|
|
3349
|
+
options: {
|
|
3350
|
+
...options,
|
|
3351
|
+
enablePromptCaching: options.enablePromptCaching ?? false,
|
|
3352
|
+
outputFormat,
|
|
3353
|
+
async getToolPermissionContext() {
|
|
3354
|
+
return getEmptyToolPermissionContext()
|
|
3355
|
+
},
|
|
3356
|
+
},
|
|
3357
|
+
})
|
|
3358
|
+
return [result]
|
|
3359
|
+
},
|
|
3360
|
+
)
|
|
3361
|
+
return result[0]! as AssistantMessage
|
|
3362
|
+
}
|
|
3363
|
+
|
|
3364
|
+
// Non-streaming requests have a 10min max per the docs:
|
|
3365
|
+
// https://platform.claude.com/docs/en/api/errors#long-requests
|
|
3366
|
+
// The SDK's 21333-token cap is derived from 10min × 128k tokens/hour, but we
|
|
3367
|
+
// bypass it by setting a client-level timeout, so we can cap higher.
|
|
3368
|
+
export const MAX_NON_STREAMING_TOKENS = 64_000
|
|
3369
|
+
|
|
3370
|
+
/**
|
|
3371
|
+
* Adjusts thinking budget when max_tokens is capped for non-streaming fallback.
|
|
3372
|
+
* Ensures the API constraint: max_tokens > thinking.budget_tokens
|
|
3373
|
+
*
|
|
3374
|
+
* @param params - The parameters that will be sent to the API
|
|
3375
|
+
* @param maxTokensCap - The maximum allowed tokens (MAX_NON_STREAMING_TOKENS)
|
|
3376
|
+
* @returns Adjusted parameters with thinking budget capped if needed
|
|
3377
|
+
*/
|
|
3378
|
+
export function adjustParamsForNonStreaming<
|
|
3379
|
+
T extends {
|
|
3380
|
+
max_tokens: number
|
|
3381
|
+
thinking?: BetaMessageStreamParams['thinking']
|
|
3382
|
+
},
|
|
3383
|
+
>(params: T, maxTokensCap: number): T {
|
|
3384
|
+
const cappedMaxTokens = Math.min(params.max_tokens, maxTokensCap)
|
|
3385
|
+
|
|
3386
|
+
// Adjust thinking budget if it would exceed capped max_tokens
|
|
3387
|
+
// to maintain the constraint: max_tokens > thinking.budget_tokens
|
|
3388
|
+
const adjustedParams = { ...params }
|
|
3389
|
+
if (
|
|
3390
|
+
adjustedParams.thinking?.type === 'enabled' &&
|
|
3391
|
+
adjustedParams.thinking.budget_tokens
|
|
3392
|
+
) {
|
|
3393
|
+
adjustedParams.thinking = {
|
|
3394
|
+
...adjustedParams.thinking,
|
|
3395
|
+
budget_tokens: Math.min(
|
|
3396
|
+
adjustedParams.thinking.budget_tokens,
|
|
3397
|
+
cappedMaxTokens - 1, // Must be at least 1 less than max_tokens
|
|
3398
|
+
),
|
|
3399
|
+
}
|
|
3400
|
+
}
|
|
3401
|
+
|
|
3402
|
+
return {
|
|
3403
|
+
...adjustedParams,
|
|
3404
|
+
max_tokens: cappedMaxTokens,
|
|
3405
|
+
}
|
|
3406
|
+
}
|
|
3407
|
+
|
|
3408
|
+
function isMaxTokensCapEnabled(): boolean {
|
|
3409
|
+
// 3P default: false (not validated on Bedrock/Vertex)
|
|
3410
|
+
return getFeatureValue_CACHED_MAY_BE_STALE('tengu_otk_slot_v1', false)
|
|
3411
|
+
}
|
|
3412
|
+
|
|
3413
|
+
export function getMaxOutputTokensForModel(model: string): number {
|
|
3414
|
+
const maxOutputTokens = getModelMaxOutputTokens(model)
|
|
3415
|
+
|
|
3416
|
+
// Slot-reservation cap: drop default to 8k for all models. BQ p99 output
|
|
3417
|
+
// = 4,911 tokens; 32k/64k defaults over-reserve 8-16× slot capacity.
|
|
3418
|
+
// Requests hitting the cap get one clean retry at 64k (query.ts
|
|
3419
|
+
// max_output_tokens_escalate). Math.min keeps models with lower native
|
|
3420
|
+
// defaults (e.g. claude-3-opus at 4k) at their native value. Applied
|
|
3421
|
+
// before the env-var override so CLAUDE_CODE_MAX_OUTPUT_TOKENS still wins.
|
|
3422
|
+
const defaultTokens = isMaxTokensCapEnabled()
|
|
3423
|
+
? Math.min(maxOutputTokens.default, CAPPED_DEFAULT_MAX_TOKENS)
|
|
3424
|
+
: maxOutputTokens.default
|
|
3425
|
+
|
|
3426
|
+
const result = validateBoundedIntEnvVar(
|
|
3427
|
+
'CLAUDE_CODE_MAX_OUTPUT_TOKENS',
|
|
3428
|
+
process.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS,
|
|
3429
|
+
defaultTokens,
|
|
3430
|
+
maxOutputTokens.upperLimit,
|
|
3431
|
+
)
|
|
3432
|
+
return result.effective
|
|
3433
|
+
}
|