@xortex/xcode 3.0.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/LICENSE +21 -0
- package/README.md +171 -0
- package/bin/xcode +127 -0
- package/bin/xcode-test +84 -0
- package/bin/xcode.cmd +31 -0
- package/constants/apiLimits.ts +94 -0
- package/constants/betas.ts +52 -0
- package/constants/common.ts +33 -0
- package/constants/cyberRiskInstruction.ts +24 -0
- package/constants/errorIds.ts +15 -0
- package/constants/figures.ts +45 -0
- package/constants/files.ts +156 -0
- package/constants/github-app.ts +144 -0
- package/constants/keys.ts +11 -0
- package/constants/messages.ts +1 -0
- package/constants/oauth.ts +234 -0
- package/constants/outputStyles.ts +216 -0
- package/constants/product.ts +76 -0
- package/constants/prompts.ts +939 -0
- package/constants/spinnerVerbs.ts +204 -0
- package/constants/system.ts +95 -0
- package/constants/systemPromptSections.ts +68 -0
- package/constants/toolLimits.ts +56 -0
- package/constants/tools.ts +112 -0
- package/constants/turnCompletionVerbs.ts +12 -0
- package/constants/xml.ts +86 -0
- package/entrypoints/agentSdkTypes.ts +443 -0
- package/entrypoints/cli.tsx +307 -0
- package/entrypoints/init.ts +340 -0
- package/entrypoints/mcp.ts +196 -0
- package/entrypoints/sandboxTypes.ts +156 -0
- package/entrypoints/sdk/controlSchemas.ts +663 -0
- package/entrypoints/sdk/coreSchemas.ts +1889 -0
- package/entrypoints/sdk/coreTypes.generated.ts +2 -0
- package/entrypoints/sdk/coreTypes.ts +62 -0
- package/entrypoints/sdk/runtimeTypes.ts +140 -0
- package/entrypoints/sdk/sdkUtilityTypes.ts +3 -0
- package/entrypoints/sdk/toolTypes.ts +90 -0
- package/main.tsx +4686 -0
- package/package.json +120 -0
- package/services/AgentSummary/agentSummary.ts +179 -0
- package/services/MagicDocs/magicDocs.ts +254 -0
- package/services/MagicDocs/prompts.ts +127 -0
- package/services/PromptSuggestion/promptSuggestion.ts +523 -0
- package/services/PromptSuggestion/speculation.ts +991 -0
- package/services/SessionMemory/prompts.ts +324 -0
- package/services/SessionMemory/sessionMemory.ts +495 -0
- package/services/SessionMemory/sessionMemoryUtils.ts +207 -0
- package/services/analytics/config.ts +38 -0
- package/services/analytics/datadog.ts +307 -0
- package/services/analytics/firstPartyEventLogger.ts +449 -0
- package/services/analytics/firstPartyEventLoggingExporter.ts +806 -0
- package/services/analytics/growthbook.ts +1155 -0
- package/services/analytics/index.ts +173 -0
- package/services/analytics/metadata.ts +973 -0
- package/services/analytics/sink.ts +114 -0
- package/services/analytics/sinkKillswitch.ts +25 -0
- package/services/api/adminRequests.ts +119 -0
- package/services/api/bootstrap.ts +141 -0
- package/services/api/claude.ts +3422 -0
- package/services/api/client.ts +406 -0
- package/services/api/dumpPrompts.ts +226 -0
- package/services/api/emptyUsage.ts +22 -0
- package/services/api/errorUtils.ts +260 -0
- package/services/api/errors.ts +1207 -0
- package/services/api/filesApi.ts +748 -0
- package/services/api/firstTokenDate.ts +60 -0
- package/services/api/gemini.ts +359 -0
- package/services/api/geminiAdapter.ts +123 -0
- package/services/api/geminiClient.ts +291 -0
- package/services/api/grove.ts +357 -0
- package/services/api/logging.ts +788 -0
- package/services/api/metricsOptOut.ts +159 -0
- package/services/api/openRouterClient.ts +453 -0
- package/services/api/overageCreditGrant.ts +137 -0
- package/services/api/promptCacheBreakDetection.ts +727 -0
- package/services/api/referral.ts +281 -0
- package/services/api/sessionIngress.ts +514 -0
- package/services/api/ultrareviewQuota.ts +38 -0
- package/services/api/usage.ts +63 -0
- package/services/api/withRetry.ts +822 -0
- package/services/autoDream/autoDream.ts +324 -0
- package/services/autoDream/config.ts +21 -0
- package/services/autoDream/consolidationLock.ts +140 -0
- package/services/autoDream/consolidationPrompt.ts +65 -0
- package/services/awaySummary.ts +74 -0
- package/services/claudeAiLimits.ts +515 -0
- package/services/claudeAiLimitsHook.ts +23 -0
- package/services/compact/apiMicrocompact.ts +153 -0
- package/services/compact/autoCompact.ts +351 -0
- package/services/compact/compact.ts +1705 -0
- package/services/compact/compactWarningHook.ts +16 -0
- package/services/compact/compactWarningState.ts +18 -0
- package/services/compact/grouping.ts +63 -0
- package/services/compact/microCompact.ts +530 -0
- package/services/compact/postCompactCleanup.ts +77 -0
- package/services/compact/prompt.ts +374 -0
- package/services/compact/sessionMemoryCompact.ts +630 -0
- package/services/compact/timeBasedMCConfig.ts +43 -0
- package/services/diagnosticTracking.ts +397 -0
- package/services/extractMemories/extractMemories.ts +517 -0
- package/services/extractMemories/prompts.ts +154 -0
- package/services/internalLogging.ts +90 -0
- package/services/lsp/LSPClient.ts +447 -0
- package/services/lsp/LSPDiagnosticRegistry.ts +386 -0
- package/services/lsp/LSPServerInstance.ts +511 -0
- package/services/lsp/LSPServerManager.ts +420 -0
- package/services/lsp/config.ts +79 -0
- package/services/lsp/manager.ts +289 -0
- package/services/lsp/passiveFeedback.ts +328 -0
- package/services/mcp/InProcessTransport.ts +63 -0
- package/services/mcp/MCPConnectionManager.tsx +73 -0
- package/services/mcp/SdkControlTransport.ts +136 -0
- package/services/mcp/auth.ts +2465 -0
- package/services/mcp/channelAllowlist.ts +76 -0
- package/services/mcp/channelNotification.ts +316 -0
- package/services/mcp/channelPermissions.ts +240 -0
- package/services/mcp/claudeai.ts +164 -0
- package/services/mcp/client.ts +3348 -0
- package/services/mcp/config.ts +1578 -0
- package/services/mcp/elicitationHandler.ts +313 -0
- package/services/mcp/envExpansion.ts +38 -0
- package/services/mcp/headersHelper.ts +138 -0
- package/services/mcp/mcpStringUtils.ts +106 -0
- package/services/mcp/normalization.ts +23 -0
- package/services/mcp/oauthPort.ts +78 -0
- package/services/mcp/officialRegistry.ts +72 -0
- package/services/mcp/types.ts +258 -0
- package/services/mcp/useManageMCPConnections.ts +1141 -0
- package/services/mcp/utils.ts +575 -0
- package/services/mcp/vscodeSdkMcp.ts +112 -0
- package/services/mcp/xaa.ts +511 -0
- package/services/mcp/xaaIdpLogin.ts +487 -0
- package/services/mcpServerApproval.tsx +41 -0
- package/services/mockRateLimits.ts +882 -0
- package/services/notifier.ts +156 -0
- package/services/oauth/auth-code-listener.ts +211 -0
- package/services/oauth/client.ts +566 -0
- package/services/oauth/crypto.ts +23 -0
- package/services/oauth/getOauthProfile.ts +53 -0
- package/services/oauth/index.ts +198 -0
- package/services/plugins/PluginInstallationManager.ts +184 -0
- package/services/plugins/pluginCliCommands.ts +344 -0
- package/services/plugins/pluginOperations.ts +1088 -0
- package/services/policyLimits/index.ts +663 -0
- package/services/policyLimits/types.ts +27 -0
- package/services/preventSleep.ts +165 -0
- package/services/rateLimitMessages.ts +344 -0
- package/services/rateLimitMocking.ts +144 -0
- package/services/remoteManagedSettings/index.ts +638 -0
- package/services/remoteManagedSettings/securityCheck.tsx +74 -0
- package/services/remoteManagedSettings/syncCache.ts +112 -0
- package/services/remoteManagedSettings/syncCacheState.ts +96 -0
- package/services/remoteManagedSettings/types.ts +31 -0
- package/services/settingsSync/index.ts +581 -0
- package/services/settingsSync/types.ts +67 -0
- package/services/teamMemorySync/index.ts +1256 -0
- package/services/teamMemorySync/secretScanner.ts +324 -0
- package/services/teamMemorySync/teamMemSecretGuard.ts +44 -0
- package/services/teamMemorySync/types.ts +156 -0
- package/services/teamMemorySync/watcher.ts +387 -0
- package/services/tips/tipHistory.ts +17 -0
- package/services/tips/tipRegistry.ts +686 -0
- package/services/tips/tipScheduler.ts +58 -0
- package/services/tokenEstimation.ts +495 -0
- package/services/toolUseSummary/toolUseSummaryGenerator.ts +112 -0
- package/services/tools/StreamingToolExecutor.ts +530 -0
- package/services/tools/toolExecution.ts +1745 -0
- package/services/tools/toolHooks.ts +650 -0
- package/services/tools/toolOrchestration.ts +188 -0
- package/services/vcr.ts +406 -0
- package/services/voice.ts +525 -0
- package/services/voiceKeyterms.ts +106 -0
- package/services/voiceStreamSTT.ts +544 -0
- package/tools/AgentTool/AgentTool.tsx +1398 -0
- package/tools/AgentTool/UI.tsx +872 -0
- package/tools/AgentTool/agentColorManager.ts +66 -0
- package/tools/AgentTool/agentDisplay.ts +104 -0
- package/tools/AgentTool/agentMemory.ts +177 -0
- package/tools/AgentTool/agentMemorySnapshot.ts +197 -0
- package/tools/AgentTool/agentToolUtils.ts +686 -0
- package/tools/AgentTool/built-in/claudeCodeGuideAgent.ts +205 -0
- package/tools/AgentTool/built-in/exploreAgent.ts +83 -0
- package/tools/AgentTool/built-in/generalPurposeAgent.ts +34 -0
- package/tools/AgentTool/built-in/planAgent.ts +92 -0
- package/tools/AgentTool/built-in/statuslineSetup.ts +144 -0
- package/tools/AgentTool/built-in/verificationAgent.ts +152 -0
- package/tools/AgentTool/builtInAgents.ts +72 -0
- package/tools/AgentTool/constants.ts +12 -0
- package/tools/AgentTool/forkSubagent.ts +210 -0
- package/tools/AgentTool/loadAgentsDir.ts +755 -0
- package/tools/AgentTool/prompt.ts +287 -0
- package/tools/AgentTool/resumeAgent.ts +265 -0
- package/tools/AgentTool/runAgent.ts +973 -0
- package/tools/AskUserQuestionTool/AskUserQuestionTool.tsx +266 -0
- package/tools/AskUserQuestionTool/prompt.ts +44 -0
- package/tools/BashTool/BashTool.tsx +1144 -0
- package/tools/BashTool/BashToolResultMessage.tsx +191 -0
- package/tools/BashTool/UI.tsx +185 -0
- package/tools/BashTool/bashCommandHelpers.ts +265 -0
- package/tools/BashTool/bashPermissions.ts +2621 -0
- package/tools/BashTool/bashSecurity.ts +2592 -0
- package/tools/BashTool/commandSemantics.ts +140 -0
- package/tools/BashTool/commentLabel.ts +13 -0
- package/tools/BashTool/destructiveCommandWarning.ts +102 -0
- package/tools/BashTool/modeValidation.ts +115 -0
- package/tools/BashTool/pathValidation.ts +1303 -0
- package/tools/BashTool/prompt.ts +369 -0
- package/tools/BashTool/readOnlyValidation.ts +1990 -0
- package/tools/BashTool/sedEditParser.ts +322 -0
- package/tools/BashTool/sedValidation.ts +684 -0
- package/tools/BashTool/shouldUseSandbox.ts +153 -0
- package/tools/BashTool/toolName.ts +2 -0
- package/tools/BashTool/utils.ts +223 -0
- package/tools/BriefTool/BriefTool.ts +204 -0
- package/tools/BriefTool/UI.tsx +101 -0
- package/tools/BriefTool/attachments.ts +110 -0
- package/tools/BriefTool/prompt.ts +22 -0
- package/tools/BriefTool/upload.ts +174 -0
- package/tools/ConfigTool/ConfigTool.ts +467 -0
- package/tools/ConfigTool/UI.tsx +38 -0
- package/tools/ConfigTool/constants.ts +1 -0
- package/tools/ConfigTool/prompt.ts +93 -0
- package/tools/ConfigTool/supportedSettings.ts +211 -0
- package/tools/EnterPlanModeTool/EnterPlanModeTool.ts +126 -0
- package/tools/EnterPlanModeTool/UI.tsx +33 -0
- package/tools/EnterPlanModeTool/constants.ts +1 -0
- package/tools/EnterPlanModeTool/prompt.ts +170 -0
- package/tools/EnterWorktreeTool/EnterWorktreeTool.ts +127 -0
- package/tools/EnterWorktreeTool/UI.tsx +20 -0
- package/tools/EnterWorktreeTool/constants.ts +1 -0
- package/tools/EnterWorktreeTool/prompt.ts +30 -0
- package/tools/ExitPlanModeTool/ExitPlanModeV2Tool.ts +493 -0
- package/tools/ExitPlanModeTool/UI.tsx +82 -0
- package/tools/ExitPlanModeTool/constants.ts +2 -0
- package/tools/ExitPlanModeTool/prompt.ts +29 -0
- package/tools/ExitWorktreeTool/ExitWorktreeTool.ts +329 -0
- package/tools/ExitWorktreeTool/UI.tsx +25 -0
- package/tools/ExitWorktreeTool/constants.ts +1 -0
- package/tools/ExitWorktreeTool/prompt.ts +32 -0
- package/tools/FileEditTool/FileEditTool.ts +625 -0
- package/tools/FileEditTool/UI.tsx +289 -0
- package/tools/FileEditTool/constants.ts +11 -0
- package/tools/FileEditTool/prompt.ts +28 -0
- package/tools/FileEditTool/types.ts +85 -0
- package/tools/FileEditTool/utils.ts +775 -0
- package/tools/FileReadTool/FileReadTool.ts +1183 -0
- package/tools/FileReadTool/UI.tsx +185 -0
- package/tools/FileReadTool/imageProcessor.ts +94 -0
- package/tools/FileReadTool/limits.ts +92 -0
- package/tools/FileReadTool/prompt.ts +49 -0
- package/tools/FileWriteTool/FileWriteTool.ts +434 -0
- package/tools/FileWriteTool/UI.tsx +405 -0
- package/tools/FileWriteTool/prompt.ts +18 -0
- package/tools/GlobTool/GlobTool.ts +198 -0
- package/tools/GlobTool/UI.tsx +63 -0
- package/tools/GlobTool/prompt.ts +7 -0
- package/tools/GrepTool/GrepTool.ts +577 -0
- package/tools/GrepTool/UI.tsx +201 -0
- package/tools/GrepTool/prompt.ts +18 -0
- package/tools/LSPTool/LSPTool.ts +860 -0
- package/tools/LSPTool/UI.tsx +228 -0
- package/tools/LSPTool/formatters.ts +592 -0
- package/tools/LSPTool/prompt.ts +21 -0
- package/tools/LSPTool/schemas.ts +215 -0
- package/tools/LSPTool/symbolContext.ts +90 -0
- package/tools/ListMcpResourcesTool/ListMcpResourcesTool.ts +123 -0
- package/tools/ListMcpResourcesTool/UI.tsx +29 -0
- package/tools/ListMcpResourcesTool/prompt.ts +20 -0
- package/tools/MCPTool/MCPTool.ts +77 -0
- package/tools/MCPTool/UI.tsx +403 -0
- package/tools/MCPTool/classifyForCollapse.ts +604 -0
- package/tools/MCPTool/prompt.ts +3 -0
- package/tools/McpAuthTool/McpAuthTool.ts +215 -0
- package/tools/NotebookEditTool/NotebookEditTool.ts +490 -0
- package/tools/NotebookEditTool/UI.tsx +93 -0
- package/tools/NotebookEditTool/constants.ts +2 -0
- package/tools/NotebookEditTool/prompt.ts +3 -0
- package/tools/PowerShellTool/PowerShellTool.tsx +1001 -0
- package/tools/PowerShellTool/UI.tsx +131 -0
- package/tools/PowerShellTool/clmTypes.ts +211 -0
- package/tools/PowerShellTool/commandSemantics.ts +142 -0
- package/tools/PowerShellTool/commonParameters.ts +30 -0
- package/tools/PowerShellTool/destructiveCommandWarning.ts +109 -0
- package/tools/PowerShellTool/gitSafety.ts +176 -0
- package/tools/PowerShellTool/modeValidation.ts +404 -0
- package/tools/PowerShellTool/pathValidation.ts +2049 -0
- package/tools/PowerShellTool/powershellPermissions.ts +1648 -0
- package/tools/PowerShellTool/powershellSecurity.ts +1090 -0
- package/tools/PowerShellTool/prompt.ts +145 -0
- package/tools/PowerShellTool/readOnlyValidation.ts +1823 -0
- package/tools/PowerShellTool/toolName.ts +2 -0
- package/tools/REPLTool/constants.ts +46 -0
- package/tools/REPLTool/primitiveTools.ts +39 -0
- package/tools/ReadMcpResourceTool/ReadMcpResourceTool.ts +158 -0
- package/tools/ReadMcpResourceTool/UI.tsx +37 -0
- package/tools/ReadMcpResourceTool/prompt.ts +16 -0
- package/tools/RemoteTriggerTool/RemoteTriggerTool.ts +161 -0
- package/tools/RemoteTriggerTool/UI.tsx +17 -0
- package/tools/RemoteTriggerTool/prompt.ts +15 -0
- package/tools/ScheduleCronTool/CronCreateTool.ts +157 -0
- package/tools/ScheduleCronTool/CronDeleteTool.ts +95 -0
- package/tools/ScheduleCronTool/CronListTool.ts +97 -0
- package/tools/ScheduleCronTool/UI.tsx +60 -0
- package/tools/ScheduleCronTool/prompt.ts +135 -0
- package/tools/SendMessageTool/SendMessageTool.ts +917 -0
- package/tools/SendMessageTool/UI.tsx +31 -0
- package/tools/SendMessageTool/constants.ts +1 -0
- package/tools/SendMessageTool/prompt.ts +49 -0
- package/tools/SkillTool/SkillTool.ts +1108 -0
- package/tools/SkillTool/UI.tsx +128 -0
- package/tools/SkillTool/constants.ts +1 -0
- package/tools/SkillTool/prompt.ts +241 -0
- package/tools/SleepTool/prompt.ts +17 -0
- package/tools/SyntheticOutputTool/SyntheticOutputTool.ts +163 -0
- package/tools/TaskCreateTool/TaskCreateTool.ts +138 -0
- package/tools/TaskCreateTool/constants.ts +1 -0
- package/tools/TaskCreateTool/prompt.ts +56 -0
- package/tools/TaskGetTool/TaskGetTool.ts +128 -0
- package/tools/TaskGetTool/constants.ts +1 -0
- package/tools/TaskGetTool/prompt.ts +24 -0
- package/tools/TaskListTool/TaskListTool.ts +116 -0
- package/tools/TaskListTool/constants.ts +1 -0
- package/tools/TaskListTool/prompt.ts +49 -0
- package/tools/TaskOutputTool/TaskOutputTool.tsx +584 -0
- package/tools/TaskOutputTool/constants.ts +1 -0
- package/tools/TaskStopTool/TaskStopTool.ts +131 -0
- package/tools/TaskStopTool/UI.tsx +41 -0
- package/tools/TaskStopTool/prompt.ts +8 -0
- package/tools/TaskUpdateTool/TaskUpdateTool.ts +406 -0
- package/tools/TaskUpdateTool/constants.ts +1 -0
- package/tools/TaskUpdateTool/prompt.ts +77 -0
- package/tools/TeamCreateTool/TeamCreateTool.ts +240 -0
- package/tools/TeamCreateTool/UI.tsx +6 -0
- package/tools/TeamCreateTool/constants.ts +1 -0
- package/tools/TeamCreateTool/prompt.ts +113 -0
- package/tools/TeamDeleteTool/TeamDeleteTool.ts +139 -0
- package/tools/TeamDeleteTool/UI.tsx +20 -0
- package/tools/TeamDeleteTool/constants.ts +1 -0
- package/tools/TeamDeleteTool/prompt.ts +16 -0
- package/tools/TodoWriteTool/TodoWriteTool.ts +115 -0
- package/tools/TodoWriteTool/constants.ts +1 -0
- package/tools/TodoWriteTool/prompt.ts +184 -0
- package/tools/ToolSearchTool/ToolSearchTool.ts +471 -0
- package/tools/ToolSearchTool/constants.ts +1 -0
- package/tools/ToolSearchTool/prompt.ts +121 -0
- package/tools/TungstenTool/TungstenTool.ts +4 -0
- package/tools/WebFetchTool/UI.tsx +72 -0
- package/tools/WebFetchTool/WebFetchTool.ts +318 -0
- package/tools/WebFetchTool/preapproved.ts +166 -0
- package/tools/WebFetchTool/prompt.ts +46 -0
- package/tools/WebFetchTool/utils.ts +530 -0
- package/tools/WebSearchTool/UI.tsx +101 -0
- package/tools/WebSearchTool/WebSearchTool.ts +435 -0
- package/tools/WebSearchTool/prompt.ts +34 -0
- package/tools/WorkflowTool/constants.ts +2 -0
- package/tools/XMemIngestTool/XMemIngestTool.ts +140 -0
- package/tools/XMemIngestTool/prompt.ts +13 -0
- package/tools/XMemRetrieveTool/XMemRetrieveTool.ts +177 -0
- package/tools/XMemRetrieveTool/prompt.ts +16 -0
- package/tools/XMemSearchTool/XMemSearchTool.ts +172 -0
- package/tools/XMemSearchTool/prompt.ts +11 -0
- package/tools/shared/gitOperationTracking.ts +277 -0
- package/tools/shared/spawnMultiAgent.ts +1093 -0
- package/tools/testing/TestingPermissionTool.tsx +74 -0
- package/tools/utils.ts +40 -0
- package/utils/CircularBuffer.ts +84 -0
- package/utils/Cursor.ts +1530 -0
- package/utils/QueryGuard.ts +121 -0
- package/utils/Shell.ts +474 -0
- package/utils/ShellCommand.ts +465 -0
- package/utils/abortController.ts +99 -0
- package/utils/activityManager.ts +164 -0
- package/utils/advisor.ts +145 -0
- package/utils/agentContext.ts +178 -0
- package/utils/agentId.ts +99 -0
- package/utils/agentSwarmsEnabled.ts +44 -0
- package/utils/agenticSessionSearch.ts +307 -0
- package/utils/analyzeContext.ts +1382 -0
- package/utils/ansiToPng.ts +334 -0
- package/utils/ansiToSvg.ts +272 -0
- package/utils/api.ts +718 -0
- package/utils/apiPreconnect.ts +71 -0
- package/utils/appleTerminalBackup.ts +124 -0
- package/utils/argumentSubstitution.ts +145 -0
- package/utils/array.ts +13 -0
- package/utils/asciicast.ts +239 -0
- package/utils/attachments.ts +4091 -0
- package/utils/attribution.ts +393 -0
- package/utils/auth.ts +2002 -0
- package/utils/authFileDescriptor.ts +196 -0
- package/utils/authPortable.ts +19 -0
- package/utils/autoModeDenials.ts +26 -0
- package/utils/autoRunIssue.tsx +122 -0
- package/utils/autoUpdater.ts +561 -0
- package/utils/aws.ts +74 -0
- package/utils/awsAuthStatusManager.ts +81 -0
- package/utils/axios.ts +8 -0
- package/utils/background/remote/preconditions.ts +235 -0
- package/utils/background/remote/remoteSession.ts +98 -0
- package/utils/backgroundHousekeeping.ts +94 -0
- package/utils/bash/ParsedCommand.ts +318 -0
- package/utils/bash/ShellSnapshot.ts +582 -0
- package/utils/bash/ast.ts +2679 -0
- package/utils/bash/bashParser.ts +4436 -0
- package/utils/bash/bashPipeCommand.ts +294 -0
- package/utils/bash/commands.ts +1339 -0
- package/utils/bash/heredoc.ts +733 -0
- package/utils/bash/parser.ts +230 -0
- package/utils/bash/prefix.ts +204 -0
- package/utils/bash/registry.ts +53 -0
- package/utils/bash/shellCompletion.ts +259 -0
- package/utils/bash/shellPrefix.ts +28 -0
- package/utils/bash/shellQuote.ts +304 -0
- package/utils/bash/shellQuoting.ts +128 -0
- package/utils/bash/specs/alias.ts +14 -0
- package/utils/bash/specs/index.ts +18 -0
- package/utils/bash/specs/nohup.ts +13 -0
- package/utils/bash/specs/pyright.ts +91 -0
- package/utils/bash/specs/sleep.ts +13 -0
- package/utils/bash/specs/srun.ts +31 -0
- package/utils/bash/specs/time.ts +13 -0
- package/utils/bash/specs/timeout.ts +20 -0
- package/utils/bash/treeSitterAnalysis.ts +506 -0
- package/utils/betas.ts +434 -0
- package/utils/billing.ts +78 -0
- package/utils/binaryCheck.ts +53 -0
- package/utils/browser.ts +68 -0
- package/utils/bufferedWriter.ts +100 -0
- package/utils/bundledMode.ts +22 -0
- package/utils/caCerts.ts +115 -0
- package/utils/caCertsConfig.ts +88 -0
- package/utils/cachePaths.ts +38 -0
- package/utils/classifierApprovals.ts +88 -0
- package/utils/classifierApprovalsHook.ts +17 -0
- package/utils/claudeCodeHints.ts +193 -0
- package/utils/claudeDesktop.ts +152 -0
- package/utils/claudeInChrome/chromeNativeHost.ts +527 -0
- package/utils/claudeInChrome/common.ts +540 -0
- package/utils/claudeInChrome/mcpServer.ts +292 -0
- package/utils/claudeInChrome/prompt.ts +83 -0
- package/utils/claudeInChrome/setup.ts +400 -0
- package/utils/claudeInChrome/setupPortable.ts +233 -0
- package/utils/claudeInChrome/toolRendering.tsx +262 -0
- package/utils/claudemd.ts +1479 -0
- package/utils/cleanup.ts +602 -0
- package/utils/cleanupRegistry.ts +25 -0
- package/utils/cliArgs.ts +60 -0
- package/utils/cliHighlight.ts +54 -0
- package/utils/codeIndexing.ts +206 -0
- package/utils/collapseBackgroundBashNotifications.ts +84 -0
- package/utils/collapseHookSummaries.ts +59 -0
- package/utils/collapseReadSearch.ts +1109 -0
- package/utils/collapseTeammateShutdowns.ts +55 -0
- package/utils/color-diff-mock.ts +27 -0
- package/utils/combinedAbortSignal.ts +47 -0
- package/utils/commandLifecycle.ts +21 -0
- package/utils/commitAttribution.ts +961 -0
- package/utils/completionCache.ts +166 -0
- package/utils/computerUse/appNames.ts +196 -0
- package/utils/computerUse/cleanup.ts +86 -0
- package/utils/computerUse/common.ts +61 -0
- package/utils/computerUse/computerUseLock.ts +215 -0
- package/utils/computerUse/drainRunLoop.ts +79 -0
- package/utils/computerUse/escHotkey.ts +54 -0
- package/utils/computerUse/executor.ts +658 -0
- package/utils/computerUse/gates.ts +72 -0
- package/utils/computerUse/hostAdapter.ts +69 -0
- package/utils/computerUse/inputLoader.ts +30 -0
- package/utils/computerUse/mcpServer.ts +106 -0
- package/utils/computerUse/setup.ts +53 -0
- package/utils/computerUse/swiftLoader.ts +23 -0
- package/utils/computerUse/toolRendering.tsx +125 -0
- package/utils/computerUse/wrapper.tsx +336 -0
- package/utils/concurrentSessions.ts +204 -0
- package/utils/config.ts +1817 -0
- package/utils/configConstants.ts +21 -0
- package/utils/contentArray.ts +51 -0
- package/utils/context.ts +221 -0
- package/utils/contextAnalysis.ts +272 -0
- package/utils/contextSuggestions.ts +235 -0
- package/utils/controlMessageCompat.ts +32 -0
- package/utils/conversationRecovery.ts +597 -0
- package/utils/cron.ts +308 -0
- package/utils/cronJitterConfig.ts +75 -0
- package/utils/cronScheduler.ts +565 -0
- package/utils/cronTasks.ts +458 -0
- package/utils/cronTasksLock.ts +195 -0
- package/utils/crossProjectResume.ts +75 -0
- package/utils/crypto.ts +13 -0
- package/utils/cwd.ts +32 -0
- package/utils/debug.ts +268 -0
- package/utils/debugFilter.ts +157 -0
- package/utils/deepLink/banner.ts +123 -0
- package/utils/deepLink/parseDeepLink.ts +170 -0
- package/utils/deepLink/protocolHandler.ts +136 -0
- package/utils/deepLink/registerProtocol.ts +348 -0
- package/utils/deepLink/terminalLauncher.ts +557 -0
- package/utils/deepLink/terminalPreference.ts +54 -0
- package/utils/desktopDeepLink.ts +236 -0
- package/utils/detectRepository.ts +178 -0
- package/utils/diagLogs.ts +94 -0
- package/utils/diff.ts +177 -0
- package/utils/directMemberMessage.ts +69 -0
- package/utils/displayTags.ts +51 -0
- package/utils/doctorContextWarnings.ts +265 -0
- package/utils/doctorDiagnostic.ts +625 -0
- package/utils/dxt/helpers.ts +88 -0
- package/utils/dxt/zip.ts +226 -0
- package/utils/earlyInput.ts +191 -0
- package/utils/editor.ts +183 -0
- package/utils/effort.ts +329 -0
- package/utils/embeddedTools.ts +29 -0
- package/utils/env.ts +347 -0
- package/utils/envDynamic.ts +151 -0
- package/utils/envUtils.ts +183 -0
- package/utils/envValidation.ts +38 -0
- package/utils/errorLogSink.ts +235 -0
- package/utils/errors.ts +238 -0
- package/utils/exampleCommands.ts +184 -0
- package/utils/execFileNoThrow.ts +150 -0
- package/utils/execFileNoThrowPortable.ts +89 -0
- package/utils/execSyncWrapper.ts +38 -0
- package/utils/exportRenderer.tsx +98 -0
- package/utils/extraUsage.ts +23 -0
- package/utils/fastMode.ts +532 -0
- package/utils/file.ts +584 -0
- package/utils/fileHistory.ts +1115 -0
- package/utils/fileOperationAnalytics.ts +71 -0
- package/utils/filePersistence/filePersistence.ts +287 -0
- package/utils/filePersistence/outputsScanner.ts +126 -0
- package/utils/fileRead.ts +102 -0
- package/utils/fileReadCache.ts +96 -0
- package/utils/fileStateCache.ts +142 -0
- package/utils/findExecutable.ts +17 -0
- package/utils/fingerprint.ts +76 -0
- package/utils/forkedAgent.ts +689 -0
- package/utils/format.ts +308 -0
- package/utils/formatBriefTimestamp.ts +81 -0
- package/utils/fpsTracker.ts +47 -0
- package/utils/frontmatterParser.ts +370 -0
- package/utils/fsOperations.ts +770 -0
- package/utils/fullscreen.ts +202 -0
- package/utils/generatedFiles.ts +136 -0
- package/utils/generators.ts +88 -0
- package/utils/genericProcessUtils.ts +184 -0
- package/utils/getWorktreePaths.ts +70 -0
- package/utils/getWorktreePathsPortable.ts +27 -0
- package/utils/ghPrStatus.ts +106 -0
- package/utils/git/gitConfigParser.ts +277 -0
- package/utils/git/gitFilesystem.ts +699 -0
- package/utils/git/gitignore.ts +99 -0
- package/utils/git.ts +926 -0
- package/utils/gitDiff.ts +532 -0
- package/utils/gitSettings.ts +18 -0
- package/utils/github/ghAuthStatus.ts +29 -0
- package/utils/githubRepoPathMapping.ts +162 -0
- package/utils/glob.ts +130 -0
- package/utils/gracefulShutdown.ts +529 -0
- package/utils/groupToolUses.ts +182 -0
- package/utils/handlePromptSubmit.ts +610 -0
- package/utils/hash.ts +46 -0
- package/utils/headlessProfiler.ts +178 -0
- package/utils/heapDumpService.ts +303 -0
- package/utils/heatmap.ts +198 -0
- package/utils/highlightMatch.tsx +28 -0
- package/utils/hooks/AsyncHookRegistry.ts +309 -0
- package/utils/hooks/apiQueryHookHelper.ts +141 -0
- package/utils/hooks/execAgentHook.ts +339 -0
- package/utils/hooks/execHttpHook.ts +242 -0
- package/utils/hooks/execPromptHook.ts +211 -0
- package/utils/hooks/fileChangedWatcher.ts +191 -0
- package/utils/hooks/hookEvents.ts +192 -0
- package/utils/hooks/hookHelpers.ts +83 -0
- package/utils/hooks/hooksConfigManager.ts +400 -0
- package/utils/hooks/hooksConfigSnapshot.ts +133 -0
- package/utils/hooks/hooksSettings.ts +271 -0
- package/utils/hooks/postSamplingHooks.ts +70 -0
- package/utils/hooks/registerFrontmatterHooks.ts +67 -0
- package/utils/hooks/registerSkillHooks.ts +64 -0
- package/utils/hooks/sessionHooks.ts +447 -0
- package/utils/hooks/skillImprovement.ts +267 -0
- package/utils/hooks/ssrfGuard.ts +294 -0
- package/utils/hooks.ts +5022 -0
- package/utils/horizontalScroll.ts +137 -0
- package/utils/http.ts +136 -0
- package/utils/hyperlink.ts +39 -0
- package/utils/iTermBackup.ts +73 -0
- package/utils/ide.ts +1494 -0
- package/utils/idePathConversion.ts +90 -0
- package/utils/idleTimeout.ts +53 -0
- package/utils/imagePaste.ts +416 -0
- package/utils/imageResizer.ts +880 -0
- package/utils/imageStore.ts +167 -0
- package/utils/imageValidation.ts +104 -0
- package/utils/immediateCommand.ts +15 -0
- package/utils/inProcessTeammateHelpers.ts +102 -0
- package/utils/ink.ts +26 -0
- package/utils/intl.ts +94 -0
- package/utils/jetbrains.ts +191 -0
- package/utils/json.ts +277 -0
- package/utils/jsonRead.ts +16 -0
- package/utils/keyboardShortcuts.ts +14 -0
- package/utils/lazySchema.ts +8 -0
- package/utils/listSessionsImpl.ts +454 -0
- package/utils/localInstaller.ts +162 -0
- package/utils/lockfile.ts +43 -0
- package/utils/log.ts +362 -0
- package/utils/logoV2Utils.ts +347 -0
- package/utils/mailbox.ts +73 -0
- package/utils/managedEnv.ts +199 -0
- package/utils/managedEnvConstants.ts +191 -0
- package/utils/markdown.ts +381 -0
- package/utils/markdownConfigLoader.ts +600 -0
- package/utils/mcp/dateTimeParser.ts +121 -0
- package/utils/mcp/elicitationValidation.ts +336 -0
- package/utils/mcpInstructionsDelta.ts +130 -0
- package/utils/mcpOutputStorage.ts +189 -0
- package/utils/mcpValidation.ts +208 -0
- package/utils/mcpWebSocketTransport.ts +200 -0
- package/utils/memoize.ts +269 -0
- package/utils/memory/types.ts +12 -0
- package/utils/memory/versions.ts +8 -0
- package/utils/memoryFileDetection.ts +289 -0
- package/utils/messagePredicates.ts +8 -0
- package/utils/messageQueueManager.ts +547 -0
- package/utils/messages/mappers.ts +290 -0
- package/utils/messages/systemInit.ts +96 -0
- package/utils/messages.ts +5520 -0
- package/utils/model/agent.ts +157 -0
- package/utils/model/aliases.ts +35 -0
- package/utils/model/antModels.ts +64 -0
- package/utils/model/bedrock.ts +265 -0
- package/utils/model/check1mAccess.ts +72 -0
- package/utils/model/configs.ts +158 -0
- package/utils/model/contextWindowUpgradeCheck.ts +47 -0
- package/utils/model/deprecation.ts +101 -0
- package/utils/model/model.ts +654 -0
- package/utils/model/modelAllowlist.ts +170 -0
- package/utils/model/modelCapabilities.ts +118 -0
- package/utils/model/modelOptions.ts +589 -0
- package/utils/model/modelStrings.ts +170 -0
- package/utils/model/modelSupportOverrides.ts +50 -0
- package/utils/model/providers.ts +42 -0
- package/utils/model/validateModel.ts +159 -0
- package/utils/modelCost.ts +231 -0
- package/utils/modifiers.ts +36 -0
- package/utils/mtls.ts +179 -0
- package/utils/nativeInstaller/download.ts +523 -0
- package/utils/nativeInstaller/index.ts +18 -0
- package/utils/nativeInstaller/installer.ts +1708 -0
- package/utils/nativeInstaller/packageManagers.ts +336 -0
- package/utils/nativeInstaller/pidLock.ts +433 -0
- package/utils/notebook.ts +224 -0
- package/utils/objectGroupBy.ts +18 -0
- package/utils/pasteStore.ts +104 -0
- package/utils/path.ts +155 -0
- package/utils/pdf.ts +300 -0
- package/utils/pdfUtils.ts +70 -0
- package/utils/peerAddress.ts +21 -0
- package/utils/permissions/PermissionMode.ts +141 -0
- package/utils/permissions/PermissionPromptToolResultSchema.ts +127 -0
- package/utils/permissions/PermissionResult.ts +35 -0
- package/utils/permissions/PermissionRule.ts +40 -0
- package/utils/permissions/PermissionUpdate.ts +389 -0
- package/utils/permissions/PermissionUpdateSchema.ts +78 -0
- package/utils/permissions/autoModeState.ts +39 -0
- package/utils/permissions/bashClassifier.ts +61 -0
- package/utils/permissions/bypassPermissionsKillswitch.ts +155 -0
- package/utils/permissions/classifierDecision.ts +98 -0
- package/utils/permissions/classifierShared.ts +39 -0
- package/utils/permissions/dangerousPatterns.ts +80 -0
- package/utils/permissions/denialTracking.ts +45 -0
- package/utils/permissions/filesystem.ts +1777 -0
- package/utils/permissions/getNextPermissionMode.ts +101 -0
- package/utils/permissions/pathValidation.ts +485 -0
- package/utils/permissions/permissionExplainer.ts +250 -0
- package/utils/permissions/permissionRuleParser.ts +198 -0
- package/utils/permissions/permissionSetup.ts +1532 -0
- package/utils/permissions/permissions.ts +1486 -0
- package/utils/permissions/permissionsLoader.ts +296 -0
- package/utils/permissions/shadowedRuleDetection.ts +234 -0
- package/utils/permissions/shellRuleMatching.ts +228 -0
- package/utils/permissions/yoloClassifier.ts +1495 -0
- package/utils/planModeV2.ts +95 -0
- package/utils/plans.ts +397 -0
- package/utils/platform.ts +150 -0
- package/utils/plugins/addDirPluginSettings.ts +71 -0
- package/utils/plugins/cacheUtils.ts +196 -0
- package/utils/plugins/dependencyResolver.ts +305 -0
- package/utils/plugins/fetchTelemetry.ts +135 -0
- package/utils/plugins/gitAvailability.ts +69 -0
- package/utils/plugins/headlessPluginInstall.ts +174 -0
- package/utils/plugins/hintRecommendation.ts +164 -0
- package/utils/plugins/installCounts.ts +292 -0
- package/utils/plugins/installedPluginsManager.ts +1268 -0
- package/utils/plugins/loadPluginAgents.ts +348 -0
- package/utils/plugins/loadPluginCommands.ts +946 -0
- package/utils/plugins/loadPluginHooks.ts +287 -0
- package/utils/plugins/loadPluginOutputStyles.ts +178 -0
- package/utils/plugins/lspPluginIntegration.ts +387 -0
- package/utils/plugins/lspRecommendation.ts +374 -0
- package/utils/plugins/managedPlugins.ts +27 -0
- package/utils/plugins/marketplaceHelpers.ts +592 -0
- package/utils/plugins/marketplaceManager.ts +2643 -0
- package/utils/plugins/mcpPluginIntegration.ts +634 -0
- package/utils/plugins/mcpbHandler.ts +968 -0
- package/utils/plugins/officialMarketplace.ts +25 -0
- package/utils/plugins/officialMarketplaceGcs.ts +216 -0
- package/utils/plugins/officialMarketplaceStartupCheck.ts +439 -0
- package/utils/plugins/orphanedPluginFilter.ts +114 -0
- package/utils/plugins/parseMarketplaceInput.ts +162 -0
- package/utils/plugins/performStartupChecks.tsx +70 -0
- package/utils/plugins/pluginAutoupdate.ts +284 -0
- package/utils/plugins/pluginBlocklist.ts +127 -0
- package/utils/plugins/pluginDirectories.ts +178 -0
- package/utils/plugins/pluginFlagging.ts +208 -0
- package/utils/plugins/pluginIdentifier.ts +123 -0
- package/utils/plugins/pluginInstallationHelpers.ts +595 -0
- package/utils/plugins/pluginLoader.ts +3302 -0
- package/utils/plugins/pluginOptionsStorage.ts +400 -0
- package/utils/plugins/pluginPolicy.ts +20 -0
- package/utils/plugins/pluginStartupCheck.ts +341 -0
- package/utils/plugins/pluginVersioning.ts +157 -0
- package/utils/plugins/reconciler.ts +265 -0
- package/utils/plugins/refresh.ts +215 -0
- package/utils/plugins/schemas.ts +1681 -0
- package/utils/plugins/validatePlugin.ts +903 -0
- package/utils/plugins/walkPluginMarkdown.ts +69 -0
- package/utils/plugins/zipCache.ts +406 -0
- package/utils/plugins/zipCacheAdapters.ts +164 -0
- package/utils/powershell/dangerousCmdlets.ts +185 -0
- package/utils/powershell/parser.ts +1804 -0
- package/utils/powershell/staticPrefix.ts +316 -0
- package/utils/preflightChecks.tsx +151 -0
- package/utils/privacyLevel.ts +55 -0
- package/utils/process.ts +68 -0
- package/utils/processUserInput/processBashCommand.tsx +140 -0
- package/utils/processUserInput/processSlashCommand.tsx +922 -0
- package/utils/processUserInput/processTextPrompt.ts +100 -0
- package/utils/processUserInput/processUserInput.ts +605 -0
- package/utils/profilerBase.ts +46 -0
- package/utils/promptCategory.ts +49 -0
- package/utils/promptEditor.ts +188 -0
- package/utils/promptShellExecution.ts +183 -0
- package/utils/proxy.ts +426 -0
- package/utils/queryContext.ts +179 -0
- package/utils/queryHelpers.ts +552 -0
- package/utils/queryProfiler.ts +301 -0
- package/utils/queueProcessor.ts +95 -0
- package/utils/readEditContext.ts +227 -0
- package/utils/readFileInRange.ts +383 -0
- package/utils/releaseNotes.ts +360 -0
- package/utils/renderOptions.ts +113 -0
- package/utils/ripgrep.ts +679 -0
- package/utils/sandbox/sandbox-adapter.ts +985 -0
- package/utils/sandbox/sandbox-ui-utils.ts +12 -0
- package/utils/sanitization.ts +91 -0
- package/utils/screenshotClipboard.ts +121 -0
- package/utils/sdkEventQueue.ts +134 -0
- package/utils/secureStorage/fallbackStorage.ts +70 -0
- package/utils/secureStorage/index.ts +17 -0
- package/utils/secureStorage/keychainPrefetch.ts +116 -0
- package/utils/secureStorage/macOsKeychainHelpers.ts +111 -0
- package/utils/secureStorage/macOsKeychainStorage.ts +231 -0
- package/utils/secureStorage/plainTextStorage.ts +84 -0
- package/utils/semanticBoolean.ts +29 -0
- package/utils/semanticNumber.ts +36 -0
- package/utils/semver.ts +59 -0
- package/utils/sequential.ts +56 -0
- package/utils/sessionActivity.ts +133 -0
- package/utils/sessionEnvVars.ts +22 -0
- package/utils/sessionEnvironment.ts +166 -0
- package/utils/sessionFileAccessHooks.ts +250 -0
- package/utils/sessionIngressAuth.ts +140 -0
- package/utils/sessionRestore.ts +551 -0
- package/utils/sessionStart.ts +232 -0
- package/utils/sessionState.ts +150 -0
- package/utils/sessionStorage.ts +5105 -0
- package/utils/sessionStoragePortable.ts +793 -0
- package/utils/sessionTitle.ts +129 -0
- package/utils/sessionUrl.ts +64 -0
- package/utils/set.ts +53 -0
- package/utils/settings/allErrors.ts +32 -0
- package/utils/settings/applySettingsChange.ts +92 -0
- package/utils/settings/changeDetector.ts +488 -0
- package/utils/settings/constants.ts +202 -0
- package/utils/settings/internalWrites.ts +37 -0
- package/utils/settings/managedPath.ts +34 -0
- package/utils/settings/mdm/constants.ts +81 -0
- package/utils/settings/mdm/rawRead.ts +130 -0
- package/utils/settings/mdm/settings.ts +316 -0
- package/utils/settings/permissionValidation.ts +262 -0
- package/utils/settings/pluginOnlyPolicy.ts +60 -0
- package/utils/settings/schemaOutput.ts +8 -0
- package/utils/settings/settings.ts +1015 -0
- package/utils/settings/settingsCache.ts +80 -0
- package/utils/settings/toolValidationConfig.ts +103 -0
- package/utils/settings/types.ts +1149 -0
- package/utils/settings/validateEditTool.ts +45 -0
- package/utils/settings/validation.ts +265 -0
- package/utils/settings/validationTips.ts +164 -0
- package/utils/shell/bashProvider.ts +255 -0
- package/utils/shell/outputLimits.ts +14 -0
- package/utils/shell/powershellDetection.ts +107 -0
- package/utils/shell/powershellProvider.ts +123 -0
- package/utils/shell/prefix.ts +367 -0
- package/utils/shell/readOnlyCommandValidation.ts +1893 -0
- package/utils/shell/resolveDefaultShell.ts +14 -0
- package/utils/shell/shellProvider.ts +33 -0
- package/utils/shell/shellToolUtils.ts +22 -0
- package/utils/shell/specPrefix.ts +241 -0
- package/utils/shellConfig.ts +167 -0
- package/utils/sideQuery.ts +222 -0
- package/utils/sideQuestion.ts +155 -0
- package/utils/signal.ts +43 -0
- package/utils/sinks.ts +16 -0
- package/utils/skills/skillChangeDetector.ts +311 -0
- package/utils/slashCommandParsing.ts +60 -0
- package/utils/sleep.ts +84 -0
- package/utils/sliceAnsi.ts +91 -0
- package/utils/slowOperations.ts +286 -0
- package/utils/standaloneAgent.ts +23 -0
- package/utils/startupProfiler.ts +194 -0
- package/utils/staticRender.tsx +116 -0
- package/utils/stats.ts +1061 -0
- package/utils/statsCache.ts +434 -0
- package/utils/status.tsx +362 -0
- package/utils/statusNoticeDefinitions.tsx +198 -0
- package/utils/statusNoticeHelpers.ts +20 -0
- package/utils/stream.ts +76 -0
- package/utils/streamJsonStdoutGuard.ts +123 -0
- package/utils/streamlinedTransform.ts +201 -0
- package/utils/stringUtils.ts +235 -0
- package/utils/subprocessEnv.ts +99 -0
- package/utils/suggestions/commandSuggestions.ts +567 -0
- package/utils/suggestions/directoryCompletion.ts +263 -0
- package/utils/suggestions/shellHistoryCompletion.ts +119 -0
- package/utils/suggestions/skillUsageTracking.ts +55 -0
- package/utils/suggestions/slackChannelSuggestions.ts +209 -0
- package/utils/swarm/It2SetupPrompt.tsx +380 -0
- package/utils/swarm/backends/ITermBackend.ts +370 -0
- package/utils/swarm/backends/InProcessBackend.ts +339 -0
- package/utils/swarm/backends/PaneBackendExecutor.ts +354 -0
- package/utils/swarm/backends/TmuxBackend.ts +764 -0
- package/utils/swarm/backends/detection.ts +128 -0
- package/utils/swarm/backends/it2Setup.ts +245 -0
- package/utils/swarm/backends/registry.ts +464 -0
- package/utils/swarm/backends/teammateModeSnapshot.ts +87 -0
- package/utils/swarm/backends/types.ts +311 -0
- package/utils/swarm/constants.ts +33 -0
- package/utils/swarm/inProcessRunner.ts +1552 -0
- package/utils/swarm/leaderPermissionBridge.ts +54 -0
- package/utils/swarm/permissionSync.ts +928 -0
- package/utils/swarm/reconnection.ts +119 -0
- package/utils/swarm/spawnInProcess.ts +328 -0
- package/utils/swarm/spawnUtils.ts +146 -0
- package/utils/swarm/teamHelpers.ts +683 -0
- package/utils/swarm/teammateInit.ts +129 -0
- package/utils/swarm/teammateLayoutManager.ts +107 -0
- package/utils/swarm/teammateModel.ts +10 -0
- package/utils/swarm/teammatePromptAddendum.ts +18 -0
- package/utils/systemDirectories.ts +74 -0
- package/utils/systemPrompt.ts +123 -0
- package/utils/systemPromptType.ts +14 -0
- package/utils/systemTheme.ts +119 -0
- package/utils/taggedId.ts +54 -0
- package/utils/task/TaskOutput.ts +390 -0
- package/utils/task/diskOutput.ts +451 -0
- package/utils/task/framework.ts +308 -0
- package/utils/task/outputFormatting.ts +38 -0
- package/utils/task/sdkProgress.ts +36 -0
- package/utils/tasks.ts +862 -0
- package/utils/teamDiscovery.ts +81 -0
- package/utils/teamMemoryOps.ts +88 -0
- package/utils/teammate.ts +292 -0
- package/utils/teammateContext.ts +96 -0
- package/utils/teammateMailbox.ts +1183 -0
- package/utils/telemetry/betaSessionTracing.ts +491 -0
- package/utils/telemetry/bigqueryExporter.ts +252 -0
- package/utils/telemetry/events.ts +75 -0
- package/utils/telemetry/instrumentation.ts +825 -0
- package/utils/telemetry/logger.ts +26 -0
- package/utils/telemetry/perfettoTracing.ts +1120 -0
- package/utils/telemetry/pluginTelemetry.ts +289 -0
- package/utils/telemetry/sessionTracing.ts +927 -0
- package/utils/telemetry/skillLoadedEvent.ts +39 -0
- package/utils/telemetryAttributes.ts +71 -0
- package/utils/teleport/api.ts +466 -0
- package/utils/teleport/environmentSelection.ts +77 -0
- package/utils/teleport/environments.ts +120 -0
- package/utils/teleport/gitBundle.ts +292 -0
- package/utils/teleport.tsx +1226 -0
- package/utils/tempfile.ts +31 -0
- package/utils/terminal.ts +131 -0
- package/utils/terminalPanel.ts +191 -0
- package/utils/textHighlighting.ts +166 -0
- package/utils/theme.ts +639 -0
- package/utils/thinking.ts +162 -0
- package/utils/timeouts.ts +39 -0
- package/utils/tmuxSocket.ts +427 -0
- package/utils/todo/types.ts +18 -0
- package/utils/tokenBudget.ts +73 -0
- package/utils/tokens.ts +261 -0
- package/utils/toolErrors.ts +132 -0
- package/utils/toolPool.ts +79 -0
- package/utils/toolResultStorage.ts +1040 -0
- package/utils/toolSchemaCache.ts +26 -0
- package/utils/toolSearch.ts +756 -0
- package/utils/transcriptSearch.ts +202 -0
- package/utils/treeify.ts +170 -0
- package/utils/truncate.ts +179 -0
- package/utils/ultraplan/ccrSession.ts +349 -0
- package/utils/ultraplan/keyword.ts +127 -0
- package/utils/ultraplan/prompt.txt +1 -0
- package/utils/unaryLogging.ts +39 -0
- package/utils/undercover.ts +89 -0
- package/utils/user.ts +194 -0
- package/utils/userAgent.ts +10 -0
- package/utils/userPromptKeywords.ts +27 -0
- package/utils/uuid.ts +27 -0
- package/utils/warningHandler.ts +121 -0
- package/utils/which.ts +82 -0
- package/utils/windowsPaths.ts +173 -0
- package/utils/withResolvers.ts +13 -0
- package/utils/words.ts +800 -0
- package/utils/workloadContext.ts +57 -0
- package/utils/worktree.ts +1519 -0
- package/utils/worktreeModeEnabled.ts +11 -0
- package/utils/xdg.ts +65 -0
- package/utils/xmem.ts +6 -0
- package/utils/xml.ts +16 -0
- package/utils/yaml.ts +15 -0
- package/utils/zodToJsonSchema.ts +23 -0
|
@@ -0,0 +1,1382 @@
|
|
|
1
|
+
import { feature } from 'bun:bundle'
|
|
2
|
+
import type { Anthropic } from '@anthropic-ai/sdk'
|
|
3
|
+
import {
|
|
4
|
+
getSystemPrompt,
|
|
5
|
+
SYSTEM_PROMPT_DYNAMIC_BOUNDARY,
|
|
6
|
+
} from 'src/constants/prompts.js'
|
|
7
|
+
import { microcompactMessages } from 'src/services/compact/microCompact.js'
|
|
8
|
+
import { getSdkBetas } from '../bootstrap/state.js'
|
|
9
|
+
import { getCommandName } from '../commands.js'
|
|
10
|
+
import { getSystemContext } from '../context.js'
|
|
11
|
+
import { getFeatureValue_CACHED_MAY_BE_STALE } from '../services/analytics/growthbook.js'
|
|
12
|
+
import {
|
|
13
|
+
AUTOCOMPACT_BUFFER_TOKENS,
|
|
14
|
+
getEffectiveContextWindowSize,
|
|
15
|
+
isAutoCompactEnabled,
|
|
16
|
+
MANUAL_COMPACT_BUFFER_TOKENS,
|
|
17
|
+
} from '../services/compact/autoCompact.js'
|
|
18
|
+
import {
|
|
19
|
+
countMessagesTokensWithAPI,
|
|
20
|
+
countTokensViaHaikuFallback,
|
|
21
|
+
roughTokenCountEstimation,
|
|
22
|
+
} from '../services/tokenEstimation.js'
|
|
23
|
+
import { estimateSkillFrontmatterTokens } from '../skills/loadSkillsDir.js'
|
|
24
|
+
import {
|
|
25
|
+
findToolByName,
|
|
26
|
+
type Tool,
|
|
27
|
+
type ToolPermissionContext,
|
|
28
|
+
type Tools,
|
|
29
|
+
type ToolUseContext,
|
|
30
|
+
toolMatchesName,
|
|
31
|
+
} from '../Tool.js'
|
|
32
|
+
import type {
|
|
33
|
+
AgentDefinition,
|
|
34
|
+
AgentDefinitionsResult,
|
|
35
|
+
} from '../tools/AgentTool/loadAgentsDir.js'
|
|
36
|
+
import { SKILL_TOOL_NAME } from '../tools/SkillTool/constants.js'
|
|
37
|
+
import {
|
|
38
|
+
getLimitedSkillToolCommands,
|
|
39
|
+
getSkillToolInfo as getSlashCommandInfo,
|
|
40
|
+
} from '../tools/SkillTool/prompt.js'
|
|
41
|
+
import type {
|
|
42
|
+
AssistantMessage,
|
|
43
|
+
AttachmentMessage,
|
|
44
|
+
Message,
|
|
45
|
+
NormalizedAssistantMessage,
|
|
46
|
+
NormalizedUserMessage,
|
|
47
|
+
UserMessage,
|
|
48
|
+
} from '../types/message.js'
|
|
49
|
+
import { toolToAPISchema } from './api.js'
|
|
50
|
+
import { filterInjectedMemoryFiles, getMemoryFiles } from './claudemd.js'
|
|
51
|
+
import { getContextWindowForModel } from './context.js'
|
|
52
|
+
import { getCwd } from './cwd.js'
|
|
53
|
+
import { logForDebugging } from './debug.js'
|
|
54
|
+
import { isEnvTruthy } from './envUtils.js'
|
|
55
|
+
import { errorMessage, toError } from './errors.js'
|
|
56
|
+
import { logError } from './log.js'
|
|
57
|
+
import { normalizeMessagesForAPI } from './messages.js'
|
|
58
|
+
import { getRuntimeMainLoopModel } from './model/model.js'
|
|
59
|
+
import type { SettingSource } from './settings/constants.js'
|
|
60
|
+
import { jsonStringify } from './slowOperations.js'
|
|
61
|
+
import { buildEffectiveSystemPrompt } from './systemPrompt.js'
|
|
62
|
+
import type { Theme } from './theme.js'
|
|
63
|
+
import { getCurrentUsage } from './tokens.js'
|
|
64
|
+
|
|
65
|
+
const RESERVED_CATEGORY_NAME = 'Autocompact buffer'
|
|
66
|
+
const MANUAL_COMPACT_BUFFER_NAME = 'Compact buffer'
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Fixed token overhead added by the API when tools are present.
|
|
70
|
+
* The API adds a tool prompt preamble (~500 tokens) once per API call when tools are present.
|
|
71
|
+
* When we count tools individually via the token counting API, each call includes this overhead,
|
|
72
|
+
* leading to N × overhead instead of 1 × overhead for N tools.
|
|
73
|
+
* We subtract this overhead from per-tool counts to show accurate tool content sizes.
|
|
74
|
+
*/
|
|
75
|
+
export const TOOL_TOKEN_COUNT_OVERHEAD = 500
|
|
76
|
+
|
|
77
|
+
async function countTokensWithFallback(
|
|
78
|
+
messages: Anthropic.Beta.Messages.BetaMessageParam[],
|
|
79
|
+
tools: Anthropic.Beta.Messages.BetaToolUnion[],
|
|
80
|
+
): Promise<number | null> {
|
|
81
|
+
try {
|
|
82
|
+
const result = await countMessagesTokensWithAPI(messages, tools)
|
|
83
|
+
if (result !== null) {
|
|
84
|
+
return result
|
|
85
|
+
}
|
|
86
|
+
logForDebugging(
|
|
87
|
+
`countTokensWithFallback: API returned null, trying haiku fallback (${tools.length} tools)`,
|
|
88
|
+
)
|
|
89
|
+
} catch (err) {
|
|
90
|
+
logForDebugging(`countTokensWithFallback: API failed: ${errorMessage(err)}`)
|
|
91
|
+
logError(err)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
const fallbackResult = await countTokensViaHaikuFallback(messages, tools)
|
|
96
|
+
if (fallbackResult === null) {
|
|
97
|
+
logForDebugging(
|
|
98
|
+
`countTokensWithFallback: haiku fallback also returned null (${tools.length} tools)`,
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
return fallbackResult
|
|
102
|
+
} catch (err) {
|
|
103
|
+
logForDebugging(
|
|
104
|
+
`countTokensWithFallback: haiku fallback failed: ${errorMessage(err)}`,
|
|
105
|
+
)
|
|
106
|
+
logError(err)
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
interface ContextCategory {
|
|
112
|
+
name: string
|
|
113
|
+
tokens: number
|
|
114
|
+
color: keyof Theme
|
|
115
|
+
/** When true, these tokens are deferred and don't count toward context usage */
|
|
116
|
+
isDeferred?: boolean
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
interface GridSquare {
|
|
120
|
+
color: keyof Theme
|
|
121
|
+
isFilled: boolean
|
|
122
|
+
categoryName: string
|
|
123
|
+
tokens: number
|
|
124
|
+
percentage: number
|
|
125
|
+
squareFullness: number // 0-1 representing how full this individual square is
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
interface MemoryFile {
|
|
129
|
+
path: string
|
|
130
|
+
type: string
|
|
131
|
+
tokens: number
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
interface McpTool {
|
|
135
|
+
name: string
|
|
136
|
+
serverName: string
|
|
137
|
+
tokens: number
|
|
138
|
+
isLoaded?: boolean
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface DeferredBuiltinTool {
|
|
142
|
+
name: string
|
|
143
|
+
tokens: number
|
|
144
|
+
isLoaded: boolean
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export interface SystemToolDetail {
|
|
148
|
+
name: string
|
|
149
|
+
tokens: number
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface SystemPromptSectionDetail {
|
|
153
|
+
name: string
|
|
154
|
+
tokens: number
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
interface Agent {
|
|
158
|
+
agentType: string
|
|
159
|
+
source: SettingSource | 'built-in' | 'plugin'
|
|
160
|
+
tokens: number
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
interface SlashCommandInfo {
|
|
164
|
+
readonly totalCommands: number
|
|
165
|
+
readonly includedCommands: number
|
|
166
|
+
readonly tokens: number
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Individual skill detail for context display */
|
|
170
|
+
interface SkillFrontmatter {
|
|
171
|
+
name: string
|
|
172
|
+
source: SettingSource | 'plugin'
|
|
173
|
+
tokens: number
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Information about skills included in the context window.
|
|
178
|
+
*/
|
|
179
|
+
interface SkillInfo {
|
|
180
|
+
/** Total number of available skills */
|
|
181
|
+
readonly totalSkills: number
|
|
182
|
+
/** Number of skills included within token budget */
|
|
183
|
+
readonly includedSkills: number
|
|
184
|
+
/** Total tokens consumed by skills */
|
|
185
|
+
readonly tokens: number
|
|
186
|
+
/** Individual skill details */
|
|
187
|
+
readonly skillFrontmatter: SkillFrontmatter[]
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export interface ContextData {
|
|
191
|
+
readonly categories: ContextCategory[]
|
|
192
|
+
readonly totalTokens: number
|
|
193
|
+
readonly maxTokens: number
|
|
194
|
+
readonly rawMaxTokens: number
|
|
195
|
+
readonly percentage: number
|
|
196
|
+
readonly gridRows: GridSquare[][]
|
|
197
|
+
readonly model: string
|
|
198
|
+
readonly memoryFiles: MemoryFile[]
|
|
199
|
+
readonly mcpTools: McpTool[]
|
|
200
|
+
/** Ant-only: per-tool breakdown of deferred built-in tools */
|
|
201
|
+
readonly deferredBuiltinTools?: DeferredBuiltinTool[]
|
|
202
|
+
/** Ant-only: per-tool breakdown of always-loaded built-in tools */
|
|
203
|
+
readonly systemTools?: SystemToolDetail[]
|
|
204
|
+
/** Ant-only: per-section breakdown of system prompt */
|
|
205
|
+
readonly systemPromptSections?: SystemPromptSectionDetail[]
|
|
206
|
+
readonly agents: Agent[]
|
|
207
|
+
readonly slashCommands?: SlashCommandInfo
|
|
208
|
+
/** Skill statistics */
|
|
209
|
+
readonly skills?: SkillInfo
|
|
210
|
+
readonly autoCompactThreshold?: number
|
|
211
|
+
readonly isAutoCompactEnabled: boolean
|
|
212
|
+
messageBreakdown?: {
|
|
213
|
+
toolCallTokens: number
|
|
214
|
+
toolResultTokens: number
|
|
215
|
+
attachmentTokens: number
|
|
216
|
+
assistantMessageTokens: number
|
|
217
|
+
userMessageTokens: number
|
|
218
|
+
toolCallsByType: Array<{
|
|
219
|
+
name: string
|
|
220
|
+
callTokens: number
|
|
221
|
+
resultTokens: number
|
|
222
|
+
}>
|
|
223
|
+
attachmentsByType: Array<{ name: string; tokens: number }>
|
|
224
|
+
}
|
|
225
|
+
/** Actual token usage from last API response (if available) */
|
|
226
|
+
readonly apiUsage: {
|
|
227
|
+
input_tokens: number
|
|
228
|
+
output_tokens: number
|
|
229
|
+
cache_creation_input_tokens: number
|
|
230
|
+
cache_read_input_tokens: number
|
|
231
|
+
} | null
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export async function countToolDefinitionTokens(
|
|
235
|
+
tools: Tools,
|
|
236
|
+
getToolPermissionContext: () => Promise<ToolPermissionContext>,
|
|
237
|
+
agentInfo: AgentDefinitionsResult | null,
|
|
238
|
+
model?: string,
|
|
239
|
+
): Promise<number> {
|
|
240
|
+
const toolSchemas = await Promise.all(
|
|
241
|
+
tools.map(tool =>
|
|
242
|
+
toolToAPISchema(tool, {
|
|
243
|
+
getToolPermissionContext,
|
|
244
|
+
tools,
|
|
245
|
+
agents: agentInfo?.activeAgents ?? [],
|
|
246
|
+
model,
|
|
247
|
+
}),
|
|
248
|
+
),
|
|
249
|
+
)
|
|
250
|
+
const result = await countTokensWithFallback([], toolSchemas)
|
|
251
|
+
if (result === null || result === 0) {
|
|
252
|
+
const toolNames = tools.map(t => t.name).join(', ')
|
|
253
|
+
logForDebugging(
|
|
254
|
+
`countToolDefinitionTokens returned ${result} for ${tools.length} tools: ${toolNames.slice(0, 100)}${toolNames.length > 100 ? '...' : ''}`,
|
|
255
|
+
)
|
|
256
|
+
}
|
|
257
|
+
return result ?? 0
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Extract a human-readable name from a system prompt section's content */
|
|
261
|
+
function extractSectionName(content: string): string {
|
|
262
|
+
// Try to find first markdown heading
|
|
263
|
+
const headingMatch = content.match(/^#+\s+(.+)$/m)
|
|
264
|
+
if (headingMatch) {
|
|
265
|
+
return headingMatch[1]!.trim()
|
|
266
|
+
}
|
|
267
|
+
// Fall back to a truncated preview of the first non-empty line
|
|
268
|
+
const firstLine = content.split('\n').find(l => l.trim().length > 0) ?? ''
|
|
269
|
+
return firstLine.length > 40 ? firstLine.slice(0, 40) + '…' : firstLine
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async function countSystemTokens(
|
|
273
|
+
effectiveSystemPrompt: readonly string[],
|
|
274
|
+
): Promise<{
|
|
275
|
+
systemPromptTokens: number
|
|
276
|
+
systemPromptSections: SystemPromptSectionDetail[]
|
|
277
|
+
}> {
|
|
278
|
+
// Get system context (gitStatus, etc.) which is always included
|
|
279
|
+
const systemContext = await getSystemContext()
|
|
280
|
+
|
|
281
|
+
// Build named entries: system prompt parts + system context values
|
|
282
|
+
// Skip empty strings and the global-cache boundary marker
|
|
283
|
+
const namedEntries: Array<{ name: string; content: string }> = [
|
|
284
|
+
...effectiveSystemPrompt
|
|
285
|
+
.filter(
|
|
286
|
+
content =>
|
|
287
|
+
content.length > 0 && content !== SYSTEM_PROMPT_DYNAMIC_BOUNDARY,
|
|
288
|
+
)
|
|
289
|
+
.map(content => ({ name: extractSectionName(content), content })),
|
|
290
|
+
...Object.entries(systemContext)
|
|
291
|
+
.filter(([, content]) => content.length > 0)
|
|
292
|
+
.map(([name, content]) => ({ name, content })),
|
|
293
|
+
]
|
|
294
|
+
|
|
295
|
+
if (namedEntries.length < 1) {
|
|
296
|
+
return { systemPromptTokens: 0, systemPromptSections: [] }
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const systemTokenCounts = await Promise.all(
|
|
300
|
+
namedEntries.map(({ content }) =>
|
|
301
|
+
countTokensWithFallback([{ role: 'user', content }], []),
|
|
302
|
+
),
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
const systemPromptSections: SystemPromptSectionDetail[] = namedEntries.map(
|
|
306
|
+
(entry, i) => ({
|
|
307
|
+
name: entry.name,
|
|
308
|
+
tokens: systemTokenCounts[i] || 0,
|
|
309
|
+
}),
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
const systemPromptTokens = systemTokenCounts.reduce(
|
|
313
|
+
(sum: number, tokens) => sum + (tokens || 0),
|
|
314
|
+
0,
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
return { systemPromptTokens, systemPromptSections }
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
async function countMemoryFileTokens(): Promise<{
|
|
321
|
+
memoryFileDetails: MemoryFile[]
|
|
322
|
+
claudeMdTokens: number
|
|
323
|
+
}> {
|
|
324
|
+
// Simple mode disables CLAUDE.md loading, so don't report tokens for them
|
|
325
|
+
if (isEnvTruthy(process.env.CLAUDE_CODE_SIMPLE)) {
|
|
326
|
+
return { memoryFileDetails: [], claudeMdTokens: 0 }
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const memoryFilesData = filterInjectedMemoryFiles(await getMemoryFiles())
|
|
330
|
+
const memoryFileDetails: MemoryFile[] = []
|
|
331
|
+
let claudeMdTokens = 0
|
|
332
|
+
|
|
333
|
+
if (memoryFilesData.length < 1) {
|
|
334
|
+
return {
|
|
335
|
+
memoryFileDetails: [],
|
|
336
|
+
claudeMdTokens: 0,
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const claudeMdTokenCounts = await Promise.all(
|
|
341
|
+
memoryFilesData.map(async file => {
|
|
342
|
+
const tokens = await countTokensWithFallback(
|
|
343
|
+
[{ role: 'user', content: file.content }],
|
|
344
|
+
[],
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
return { file, tokens: tokens || 0 }
|
|
348
|
+
}),
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
for (const { file, tokens } of claudeMdTokenCounts) {
|
|
352
|
+
claudeMdTokens += tokens
|
|
353
|
+
memoryFileDetails.push({
|
|
354
|
+
path: file.path,
|
|
355
|
+
type: file.type,
|
|
356
|
+
tokens,
|
|
357
|
+
})
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return { claudeMdTokens, memoryFileDetails }
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async function countBuiltInToolTokens(
|
|
364
|
+
tools: Tools,
|
|
365
|
+
getToolPermissionContext: () => Promise<ToolPermissionContext>,
|
|
366
|
+
agentInfo: AgentDefinitionsResult | null,
|
|
367
|
+
model?: string,
|
|
368
|
+
messages?: Message[],
|
|
369
|
+
): Promise<{
|
|
370
|
+
builtInToolTokens: number
|
|
371
|
+
deferredBuiltinDetails: DeferredBuiltinTool[]
|
|
372
|
+
deferredBuiltinTokens: number
|
|
373
|
+
systemToolDetails: SystemToolDetail[]
|
|
374
|
+
}> {
|
|
375
|
+
const builtInTools = tools.filter(tool => !tool.isMcp)
|
|
376
|
+
if (builtInTools.length < 1) {
|
|
377
|
+
return {
|
|
378
|
+
builtInToolTokens: 0,
|
|
379
|
+
deferredBuiltinDetails: [],
|
|
380
|
+
deferredBuiltinTokens: 0,
|
|
381
|
+
systemToolDetails: [],
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// Check if tool search is enabled
|
|
386
|
+
const { isToolSearchEnabled } = await import('./toolSearch.js')
|
|
387
|
+
const { isDeferredTool } = await import('../tools/ToolSearchTool/prompt.js')
|
|
388
|
+
const isDeferred = await isToolSearchEnabled(
|
|
389
|
+
model ?? '',
|
|
390
|
+
tools,
|
|
391
|
+
getToolPermissionContext,
|
|
392
|
+
agentInfo?.activeAgents ?? [],
|
|
393
|
+
'analyzeBuiltIn',
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
// Separate always-loaded and deferred builtin tools using dynamic isDeferredTool check
|
|
397
|
+
const alwaysLoadedTools = builtInTools.filter(t => !isDeferredTool(t))
|
|
398
|
+
const deferredBuiltinTools = builtInTools.filter(t => isDeferredTool(t))
|
|
399
|
+
|
|
400
|
+
// Count always-loaded tools
|
|
401
|
+
const alwaysLoadedTokens =
|
|
402
|
+
alwaysLoadedTools.length > 0
|
|
403
|
+
? await countToolDefinitionTokens(
|
|
404
|
+
alwaysLoadedTools,
|
|
405
|
+
getToolPermissionContext,
|
|
406
|
+
agentInfo,
|
|
407
|
+
model,
|
|
408
|
+
)
|
|
409
|
+
: 0
|
|
410
|
+
|
|
411
|
+
// Build per-tool breakdown for always-loaded tools (ant-only, proportional
|
|
412
|
+
// split of the bulk count based on rough schema size estimation). Excludes
|
|
413
|
+
// SkillTool since its tokens are shown in the separate Skills category.
|
|
414
|
+
let systemToolDetails: SystemToolDetail[] = []
|
|
415
|
+
if (process.env.USER_TYPE === 'ant') {
|
|
416
|
+
const toolsForBreakdown = alwaysLoadedTools.filter(
|
|
417
|
+
t => !toolMatchesName(t, SKILL_TOOL_NAME),
|
|
418
|
+
)
|
|
419
|
+
if (toolsForBreakdown.length > 0) {
|
|
420
|
+
const estimates = toolsForBreakdown.map(t =>
|
|
421
|
+
roughTokenCountEstimation(jsonStringify(t.inputSchema ?? {})),
|
|
422
|
+
)
|
|
423
|
+
const estimateTotal = estimates.reduce((s, e) => s + e, 0) || 1
|
|
424
|
+
const distributable = Math.max(
|
|
425
|
+
0,
|
|
426
|
+
alwaysLoadedTokens - TOOL_TOKEN_COUNT_OVERHEAD,
|
|
427
|
+
)
|
|
428
|
+
systemToolDetails = toolsForBreakdown
|
|
429
|
+
.map((t, i) => ({
|
|
430
|
+
name: t.name,
|
|
431
|
+
tokens: Math.round((estimates[i]! / estimateTotal) * distributable),
|
|
432
|
+
}))
|
|
433
|
+
.sort((a, b) => b.tokens - a.tokens)
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Count deferred builtin tools individually for details
|
|
438
|
+
const deferredBuiltinDetails: DeferredBuiltinTool[] = []
|
|
439
|
+
let loadedDeferredTokens = 0
|
|
440
|
+
let totalDeferredTokens = 0
|
|
441
|
+
|
|
442
|
+
if (deferredBuiltinTools.length > 0 && isDeferred) {
|
|
443
|
+
// Find which deferred tools have been used in messages
|
|
444
|
+
const loadedToolNames = new Set<string>()
|
|
445
|
+
if (messages) {
|
|
446
|
+
const deferredToolNameSet = new Set(deferredBuiltinTools.map(t => t.name))
|
|
447
|
+
for (const msg of messages) {
|
|
448
|
+
if (msg.type === 'assistant') {
|
|
449
|
+
for (const block of msg.message.content) {
|
|
450
|
+
if (
|
|
451
|
+
'type' in block &&
|
|
452
|
+
block.type === 'tool_use' &&
|
|
453
|
+
'name' in block &&
|
|
454
|
+
typeof block.name === 'string' &&
|
|
455
|
+
deferredToolNameSet.has(block.name)
|
|
456
|
+
) {
|
|
457
|
+
loadedToolNames.add(block.name)
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// Count each deferred tool
|
|
465
|
+
const tokensByTool = await Promise.all(
|
|
466
|
+
deferredBuiltinTools.map(t =>
|
|
467
|
+
countToolDefinitionTokens(
|
|
468
|
+
[t],
|
|
469
|
+
getToolPermissionContext,
|
|
470
|
+
agentInfo,
|
|
471
|
+
model,
|
|
472
|
+
),
|
|
473
|
+
),
|
|
474
|
+
)
|
|
475
|
+
|
|
476
|
+
for (const [i, tool] of deferredBuiltinTools.entries()) {
|
|
477
|
+
const tokens = Math.max(
|
|
478
|
+
0,
|
|
479
|
+
(tokensByTool[i] || 0) - TOOL_TOKEN_COUNT_OVERHEAD,
|
|
480
|
+
)
|
|
481
|
+
const isLoaded = loadedToolNames.has(tool.name)
|
|
482
|
+
deferredBuiltinDetails.push({
|
|
483
|
+
name: tool.name,
|
|
484
|
+
tokens,
|
|
485
|
+
isLoaded,
|
|
486
|
+
})
|
|
487
|
+
totalDeferredTokens += tokens
|
|
488
|
+
if (isLoaded) {
|
|
489
|
+
loadedDeferredTokens += tokens
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
} else if (deferredBuiltinTools.length > 0) {
|
|
493
|
+
// Tool search not enabled - count deferred tools as regular
|
|
494
|
+
const deferredTokens = await countToolDefinitionTokens(
|
|
495
|
+
deferredBuiltinTools,
|
|
496
|
+
getToolPermissionContext,
|
|
497
|
+
agentInfo,
|
|
498
|
+
model,
|
|
499
|
+
)
|
|
500
|
+
return {
|
|
501
|
+
builtInToolTokens: alwaysLoadedTokens + deferredTokens,
|
|
502
|
+
deferredBuiltinDetails: [],
|
|
503
|
+
deferredBuiltinTokens: 0,
|
|
504
|
+
systemToolDetails,
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
return {
|
|
509
|
+
// When deferred, only count always-loaded tools + any loaded deferred tools
|
|
510
|
+
builtInToolTokens: alwaysLoadedTokens + loadedDeferredTokens,
|
|
511
|
+
deferredBuiltinDetails,
|
|
512
|
+
deferredBuiltinTokens: totalDeferredTokens - loadedDeferredTokens,
|
|
513
|
+
systemToolDetails,
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function findSkillTool(tools: Tools): Tool | undefined {
|
|
518
|
+
return findToolByName(tools, SKILL_TOOL_NAME)
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
async function countSlashCommandTokens(
|
|
522
|
+
tools: Tools,
|
|
523
|
+
getToolPermissionContext: () => Promise<ToolPermissionContext>,
|
|
524
|
+
agentInfo: AgentDefinitionsResult | null,
|
|
525
|
+
): Promise<{
|
|
526
|
+
slashCommandTokens: number
|
|
527
|
+
commandInfo: { totalCommands: number; includedCommands: number }
|
|
528
|
+
}> {
|
|
529
|
+
const info = await getSlashCommandInfo(getCwd())
|
|
530
|
+
|
|
531
|
+
const slashCommandTool = findSkillTool(tools)
|
|
532
|
+
if (!slashCommandTool) {
|
|
533
|
+
return {
|
|
534
|
+
slashCommandTokens: 0,
|
|
535
|
+
commandInfo: { totalCommands: 0, includedCommands: 0 },
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const slashCommandTokens = await countToolDefinitionTokens(
|
|
540
|
+
[slashCommandTool],
|
|
541
|
+
getToolPermissionContext,
|
|
542
|
+
agentInfo,
|
|
543
|
+
)
|
|
544
|
+
|
|
545
|
+
return {
|
|
546
|
+
slashCommandTokens,
|
|
547
|
+
commandInfo: {
|
|
548
|
+
totalCommands: info.totalCommands,
|
|
549
|
+
includedCommands: info.includedCommands,
|
|
550
|
+
},
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
async function countSkillTokens(
|
|
555
|
+
tools: Tools,
|
|
556
|
+
getToolPermissionContext: () => Promise<ToolPermissionContext>,
|
|
557
|
+
agentInfo: AgentDefinitionsResult | null,
|
|
558
|
+
): Promise<{
|
|
559
|
+
skillTokens: number
|
|
560
|
+
skillInfo: {
|
|
561
|
+
totalSkills: number
|
|
562
|
+
includedSkills: number
|
|
563
|
+
skillFrontmatter: SkillFrontmatter[]
|
|
564
|
+
}
|
|
565
|
+
}> {
|
|
566
|
+
try {
|
|
567
|
+
const skills = await getLimitedSkillToolCommands(getCwd())
|
|
568
|
+
|
|
569
|
+
const slashCommandTool = findSkillTool(tools)
|
|
570
|
+
if (!slashCommandTool) {
|
|
571
|
+
return {
|
|
572
|
+
skillTokens: 0,
|
|
573
|
+
skillInfo: { totalSkills: 0, includedSkills: 0, skillFrontmatter: [] },
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// NOTE: This counts the entire SlashCommandTool (which includes both commands AND skills).
|
|
578
|
+
// This is the same tool counted by countSlashCommandTokens(), but we track it separately
|
|
579
|
+
// here for display purposes. These tokens should NOT be added to context categories
|
|
580
|
+
// to avoid double-counting.
|
|
581
|
+
const skillTokens = await countToolDefinitionTokens(
|
|
582
|
+
[slashCommandTool],
|
|
583
|
+
getToolPermissionContext,
|
|
584
|
+
agentInfo,
|
|
585
|
+
)
|
|
586
|
+
|
|
587
|
+
// Calculate per-skill token estimates based on frontmatter only
|
|
588
|
+
// (name, description, whenToUse) since full content is only loaded on invocation
|
|
589
|
+
const skillFrontmatter: SkillFrontmatter[] = skills.map(skill => ({
|
|
590
|
+
name: getCommandName(skill),
|
|
591
|
+
source: (skill.type === 'prompt' ? skill.source : 'plugin') as
|
|
592
|
+
| SettingSource
|
|
593
|
+
| 'plugin',
|
|
594
|
+
tokens: estimateSkillFrontmatterTokens(skill),
|
|
595
|
+
}))
|
|
596
|
+
|
|
597
|
+
return {
|
|
598
|
+
skillTokens,
|
|
599
|
+
skillInfo: {
|
|
600
|
+
totalSkills: skills.length,
|
|
601
|
+
includedSkills: skills.length,
|
|
602
|
+
skillFrontmatter,
|
|
603
|
+
},
|
|
604
|
+
}
|
|
605
|
+
} catch (error) {
|
|
606
|
+
logError(toError(error))
|
|
607
|
+
|
|
608
|
+
// Return zero values rather than failing the entire context analysis
|
|
609
|
+
return {
|
|
610
|
+
skillTokens: 0,
|
|
611
|
+
skillInfo: { totalSkills: 0, includedSkills: 0, skillFrontmatter: [] },
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
export async function countMcpToolTokens(
|
|
617
|
+
tools: Tools,
|
|
618
|
+
getToolPermissionContext: () => Promise<ToolPermissionContext>,
|
|
619
|
+
agentInfo: AgentDefinitionsResult | null,
|
|
620
|
+
model: string,
|
|
621
|
+
messages?: Message[],
|
|
622
|
+
): Promise<{
|
|
623
|
+
mcpToolTokens: number
|
|
624
|
+
mcpToolDetails: McpTool[]
|
|
625
|
+
deferredToolTokens: number
|
|
626
|
+
loadedMcpToolNames: Set<string>
|
|
627
|
+
}> {
|
|
628
|
+
const mcpTools = tools.filter(tool => tool.isMcp)
|
|
629
|
+
const mcpToolDetails: McpTool[] = []
|
|
630
|
+
// Single bulk API call for all MCP tools (instead of N individual calls)
|
|
631
|
+
const totalTokensRaw = await countToolDefinitionTokens(
|
|
632
|
+
mcpTools,
|
|
633
|
+
getToolPermissionContext,
|
|
634
|
+
agentInfo,
|
|
635
|
+
model,
|
|
636
|
+
)
|
|
637
|
+
// Subtract the single overhead since we made one bulk call
|
|
638
|
+
const totalTokens = Math.max(
|
|
639
|
+
0,
|
|
640
|
+
(totalTokensRaw || 0) - TOOL_TOKEN_COUNT_OVERHEAD,
|
|
641
|
+
)
|
|
642
|
+
|
|
643
|
+
// Estimate per-tool proportions for display using local estimation.
|
|
644
|
+
// Include name + description + input schema to match what toolToAPISchema
|
|
645
|
+
// sends — otherwise tools with similar schemas but different descriptions
|
|
646
|
+
// get identical counts (MCP tools share the same base Zod inputSchema).
|
|
647
|
+
const estimates = await Promise.all(
|
|
648
|
+
mcpTools.map(async t =>
|
|
649
|
+
roughTokenCountEstimation(
|
|
650
|
+
jsonStringify({
|
|
651
|
+
name: t.name,
|
|
652
|
+
description: await t.prompt({
|
|
653
|
+
getToolPermissionContext,
|
|
654
|
+
tools,
|
|
655
|
+
agents: agentInfo?.activeAgents ?? [],
|
|
656
|
+
}),
|
|
657
|
+
input_schema: t.inputJSONSchema ?? {},
|
|
658
|
+
}),
|
|
659
|
+
),
|
|
660
|
+
),
|
|
661
|
+
)
|
|
662
|
+
const estimateTotal = estimates.reduce((s, e) => s + e, 0) || 1
|
|
663
|
+
const mcpToolTokensByTool = estimates.map(e =>
|
|
664
|
+
Math.round((e / estimateTotal) * totalTokens),
|
|
665
|
+
)
|
|
666
|
+
|
|
667
|
+
// Check if tool search is enabled - if so, MCP tools are deferred
|
|
668
|
+
// isToolSearchEnabled handles threshold calculation internally for TstAuto mode
|
|
669
|
+
const { isToolSearchEnabled } = await import('./toolSearch.js')
|
|
670
|
+
const { isDeferredTool } = await import('../tools/ToolSearchTool/prompt.js')
|
|
671
|
+
|
|
672
|
+
const isDeferred = await isToolSearchEnabled(
|
|
673
|
+
model,
|
|
674
|
+
tools,
|
|
675
|
+
getToolPermissionContext,
|
|
676
|
+
agentInfo?.activeAgents ?? [],
|
|
677
|
+
'analyzeMcp',
|
|
678
|
+
)
|
|
679
|
+
|
|
680
|
+
// Find MCP tools that have been used in messages (loaded via ToolSearchTool)
|
|
681
|
+
const loadedMcpToolNames = new Set<string>()
|
|
682
|
+
if (isDeferred && messages) {
|
|
683
|
+
const mcpToolNameSet = new Set(mcpTools.map(t => t.name))
|
|
684
|
+
for (const msg of messages) {
|
|
685
|
+
if (msg.type === 'assistant') {
|
|
686
|
+
for (const block of msg.message.content) {
|
|
687
|
+
if (
|
|
688
|
+
'type' in block &&
|
|
689
|
+
block.type === 'tool_use' &&
|
|
690
|
+
'name' in block &&
|
|
691
|
+
typeof block.name === 'string' &&
|
|
692
|
+
mcpToolNameSet.has(block.name)
|
|
693
|
+
) {
|
|
694
|
+
loadedMcpToolNames.add(block.name)
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// Build tool details with isLoaded flag
|
|
702
|
+
for (const [i, tool] of mcpTools.entries()) {
|
|
703
|
+
mcpToolDetails.push({
|
|
704
|
+
name: tool.name,
|
|
705
|
+
serverName: tool.name.split('__')[1] || 'unknown',
|
|
706
|
+
tokens: mcpToolTokensByTool[i]!,
|
|
707
|
+
isLoaded: loadedMcpToolNames.has(tool.name) || !isDeferredTool(tool),
|
|
708
|
+
})
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// Calculate loaded vs deferred tokens
|
|
712
|
+
let loadedTokens = 0
|
|
713
|
+
let deferredTokens = 0
|
|
714
|
+
for (const detail of mcpToolDetails) {
|
|
715
|
+
if (detail.isLoaded) {
|
|
716
|
+
loadedTokens += detail.tokens
|
|
717
|
+
} else if (isDeferred) {
|
|
718
|
+
deferredTokens += detail.tokens
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
return {
|
|
723
|
+
// When deferred but some tools are loaded, count loaded tokens
|
|
724
|
+
mcpToolTokens: isDeferred ? loadedTokens : totalTokens,
|
|
725
|
+
mcpToolDetails,
|
|
726
|
+
// Track deferred tokens separately for display
|
|
727
|
+
deferredToolTokens: deferredTokens,
|
|
728
|
+
loadedMcpToolNames,
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
async function countCustomAgentTokens(agentDefinitions: {
|
|
733
|
+
activeAgents: AgentDefinition[]
|
|
734
|
+
}): Promise<{
|
|
735
|
+
agentTokens: number
|
|
736
|
+
agentDetails: Agent[]
|
|
737
|
+
}> {
|
|
738
|
+
const customAgents = agentDefinitions.activeAgents.filter(
|
|
739
|
+
a => a.source !== 'built-in',
|
|
740
|
+
)
|
|
741
|
+
const agentDetails: Agent[] = []
|
|
742
|
+
let agentTokens = 0
|
|
743
|
+
|
|
744
|
+
const tokenCounts = await Promise.all(
|
|
745
|
+
customAgents.map(agent =>
|
|
746
|
+
countTokensWithFallback(
|
|
747
|
+
[
|
|
748
|
+
{
|
|
749
|
+
role: 'user',
|
|
750
|
+
content: [agent.agentType, agent.whenToUse].join(' '),
|
|
751
|
+
},
|
|
752
|
+
],
|
|
753
|
+
[],
|
|
754
|
+
),
|
|
755
|
+
),
|
|
756
|
+
)
|
|
757
|
+
|
|
758
|
+
for (const [i, agent] of customAgents.entries()) {
|
|
759
|
+
const tokens = tokenCounts[i] || 0
|
|
760
|
+
agentTokens += tokens || 0
|
|
761
|
+
agentDetails.push({
|
|
762
|
+
agentType: agent.agentType,
|
|
763
|
+
source: agent.source,
|
|
764
|
+
tokens: tokens || 0,
|
|
765
|
+
})
|
|
766
|
+
}
|
|
767
|
+
return { agentTokens, agentDetails }
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
type MessageBreakdown = {
|
|
771
|
+
totalTokens: number
|
|
772
|
+
toolCallTokens: number
|
|
773
|
+
toolResultTokens: number
|
|
774
|
+
attachmentTokens: number
|
|
775
|
+
assistantMessageTokens: number
|
|
776
|
+
userMessageTokens: number
|
|
777
|
+
toolCallsByType: Map<string, number>
|
|
778
|
+
toolResultsByType: Map<string, number>
|
|
779
|
+
attachmentsByType: Map<string, number>
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
function processAssistantMessage(
|
|
783
|
+
msg: AssistantMessage | NormalizedAssistantMessage,
|
|
784
|
+
breakdown: MessageBreakdown,
|
|
785
|
+
): void {
|
|
786
|
+
// Process each content block individually
|
|
787
|
+
for (const block of msg.message.content) {
|
|
788
|
+
const blockStr = jsonStringify(block)
|
|
789
|
+
const blockTokens = roughTokenCountEstimation(blockStr)
|
|
790
|
+
|
|
791
|
+
if ('type' in block && block.type === 'tool_use') {
|
|
792
|
+
breakdown.toolCallTokens += blockTokens
|
|
793
|
+
const toolName = ('name' in block ? block.name : undefined) || 'unknown'
|
|
794
|
+
breakdown.toolCallsByType.set(
|
|
795
|
+
toolName,
|
|
796
|
+
(breakdown.toolCallsByType.get(toolName) || 0) + blockTokens,
|
|
797
|
+
)
|
|
798
|
+
} else {
|
|
799
|
+
// Text blocks or other non-tool content
|
|
800
|
+
breakdown.assistantMessageTokens += blockTokens
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
function processUserMessage(
|
|
806
|
+
msg: UserMessage | NormalizedUserMessage,
|
|
807
|
+
breakdown: MessageBreakdown,
|
|
808
|
+
toolUseIdToName: Map<string, string>,
|
|
809
|
+
): void {
|
|
810
|
+
// Handle both string and array content
|
|
811
|
+
if (typeof msg.message.content === 'string') {
|
|
812
|
+
// Simple string content
|
|
813
|
+
const tokens = roughTokenCountEstimation(msg.message.content)
|
|
814
|
+
breakdown.userMessageTokens += tokens
|
|
815
|
+
return
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// Process each content block individually
|
|
819
|
+
for (const block of msg.message.content) {
|
|
820
|
+
const blockStr = jsonStringify(block)
|
|
821
|
+
const blockTokens = roughTokenCountEstimation(blockStr)
|
|
822
|
+
|
|
823
|
+
if ('type' in block && block.type === 'tool_result') {
|
|
824
|
+
breakdown.toolResultTokens += blockTokens
|
|
825
|
+
const toolUseId = 'tool_use_id' in block ? block.tool_use_id : undefined
|
|
826
|
+
const toolName =
|
|
827
|
+
(toolUseId ? toolUseIdToName.get(toolUseId) : undefined) || 'unknown'
|
|
828
|
+
breakdown.toolResultsByType.set(
|
|
829
|
+
toolName,
|
|
830
|
+
(breakdown.toolResultsByType.get(toolName) || 0) + blockTokens,
|
|
831
|
+
)
|
|
832
|
+
} else {
|
|
833
|
+
// Text blocks or other non-tool content
|
|
834
|
+
breakdown.userMessageTokens += blockTokens
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
function processAttachment(
|
|
840
|
+
msg: AttachmentMessage,
|
|
841
|
+
breakdown: MessageBreakdown,
|
|
842
|
+
): void {
|
|
843
|
+
const contentStr = jsonStringify(msg.attachment)
|
|
844
|
+
const tokens = roughTokenCountEstimation(contentStr)
|
|
845
|
+
breakdown.attachmentTokens += tokens
|
|
846
|
+
const attachType = msg.attachment.type || 'unknown'
|
|
847
|
+
breakdown.attachmentsByType.set(
|
|
848
|
+
attachType,
|
|
849
|
+
(breakdown.attachmentsByType.get(attachType) || 0) + tokens,
|
|
850
|
+
)
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
async function approximateMessageTokens(
|
|
854
|
+
messages: Message[],
|
|
855
|
+
): Promise<MessageBreakdown> {
|
|
856
|
+
const microcompactResult = await microcompactMessages(messages)
|
|
857
|
+
|
|
858
|
+
// Initialize tracking
|
|
859
|
+
const breakdown: MessageBreakdown = {
|
|
860
|
+
totalTokens: 0,
|
|
861
|
+
toolCallTokens: 0,
|
|
862
|
+
toolResultTokens: 0,
|
|
863
|
+
attachmentTokens: 0,
|
|
864
|
+
assistantMessageTokens: 0,
|
|
865
|
+
userMessageTokens: 0,
|
|
866
|
+
toolCallsByType: new Map<string, number>(),
|
|
867
|
+
toolResultsByType: new Map<string, number>(),
|
|
868
|
+
attachmentsByType: new Map<string, number>(),
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
// Build a map of tool_use_id to tool_name for easier lookup
|
|
872
|
+
const toolUseIdToName = new Map<string, string>()
|
|
873
|
+
for (const msg of microcompactResult.messages) {
|
|
874
|
+
if (msg.type === 'assistant') {
|
|
875
|
+
for (const block of msg.message.content) {
|
|
876
|
+
if ('type' in block && block.type === 'tool_use') {
|
|
877
|
+
const toolUseId = 'id' in block ? block.id : undefined
|
|
878
|
+
const toolName =
|
|
879
|
+
('name' in block ? block.name : undefined) || 'unknown'
|
|
880
|
+
if (toolUseId) {
|
|
881
|
+
toolUseIdToName.set(toolUseId, toolName)
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// Process each message for detailed breakdown
|
|
889
|
+
for (const msg of microcompactResult.messages) {
|
|
890
|
+
if (msg.type === 'assistant') {
|
|
891
|
+
processAssistantMessage(msg, breakdown)
|
|
892
|
+
} else if (msg.type === 'user') {
|
|
893
|
+
processUserMessage(msg, breakdown, toolUseIdToName)
|
|
894
|
+
} else if (msg.type === 'attachment') {
|
|
895
|
+
processAttachment(msg, breakdown)
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
// Calculate total tokens using the API for accuracy
|
|
900
|
+
const approximateMessageTokens = await countTokensWithFallback(
|
|
901
|
+
normalizeMessagesForAPI(microcompactResult.messages).map(_ => {
|
|
902
|
+
if (_.type === 'assistant') {
|
|
903
|
+
return {
|
|
904
|
+
// Important: strip out fields like id, etc. -- the counting API errors if they're present
|
|
905
|
+
role: 'assistant',
|
|
906
|
+
content: _.message.content,
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
return _.message
|
|
910
|
+
}),
|
|
911
|
+
[],
|
|
912
|
+
)
|
|
913
|
+
|
|
914
|
+
breakdown.totalTokens = approximateMessageTokens ?? 0
|
|
915
|
+
return breakdown
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
export async function analyzeContextUsage(
|
|
919
|
+
messages: Message[],
|
|
920
|
+
model: string,
|
|
921
|
+
getToolPermissionContext: () => Promise<ToolPermissionContext>,
|
|
922
|
+
tools: Tools,
|
|
923
|
+
agentDefinitions: AgentDefinitionsResult,
|
|
924
|
+
terminalWidth?: number,
|
|
925
|
+
toolUseContext?: Pick<ToolUseContext, 'options'>,
|
|
926
|
+
mainThreadAgentDefinition?: AgentDefinition,
|
|
927
|
+
/** Original messages before microcompact, used to extract API usage */
|
|
928
|
+
originalMessages?: Message[],
|
|
929
|
+
): Promise<ContextData> {
|
|
930
|
+
const runtimeModel = getRuntimeMainLoopModel({
|
|
931
|
+
permissionMode: (await getToolPermissionContext()).mode,
|
|
932
|
+
mainLoopModel: model,
|
|
933
|
+
})
|
|
934
|
+
// Get context window size
|
|
935
|
+
const contextWindow = getContextWindowForModel(runtimeModel, getSdkBetas())
|
|
936
|
+
|
|
937
|
+
// Build the effective system prompt using the shared utility
|
|
938
|
+
const defaultSystemPrompt = await getSystemPrompt(tools, runtimeModel)
|
|
939
|
+
const effectiveSystemPrompt = buildEffectiveSystemPrompt({
|
|
940
|
+
mainThreadAgentDefinition,
|
|
941
|
+
toolUseContext: toolUseContext ?? {
|
|
942
|
+
options: {} as ToolUseContext['options'],
|
|
943
|
+
},
|
|
944
|
+
customSystemPrompt: toolUseContext?.options.customSystemPrompt,
|
|
945
|
+
defaultSystemPrompt,
|
|
946
|
+
appendSystemPrompt: toolUseContext?.options.appendSystemPrompt,
|
|
947
|
+
})
|
|
948
|
+
|
|
949
|
+
// Critical operations that should not fail due to skills
|
|
950
|
+
const [
|
|
951
|
+
{ systemPromptTokens, systemPromptSections },
|
|
952
|
+
{ claudeMdTokens, memoryFileDetails },
|
|
953
|
+
{
|
|
954
|
+
builtInToolTokens,
|
|
955
|
+
deferredBuiltinDetails,
|
|
956
|
+
deferredBuiltinTokens,
|
|
957
|
+
systemToolDetails,
|
|
958
|
+
},
|
|
959
|
+
{ mcpToolTokens, mcpToolDetails, deferredToolTokens },
|
|
960
|
+
{ agentTokens, agentDetails },
|
|
961
|
+
{ slashCommandTokens, commandInfo },
|
|
962
|
+
messageBreakdown,
|
|
963
|
+
] = await Promise.all([
|
|
964
|
+
countSystemTokens(effectiveSystemPrompt),
|
|
965
|
+
countMemoryFileTokens(),
|
|
966
|
+
countBuiltInToolTokens(
|
|
967
|
+
tools,
|
|
968
|
+
getToolPermissionContext,
|
|
969
|
+
agentDefinitions,
|
|
970
|
+
runtimeModel,
|
|
971
|
+
messages,
|
|
972
|
+
),
|
|
973
|
+
countMcpToolTokens(
|
|
974
|
+
tools,
|
|
975
|
+
getToolPermissionContext,
|
|
976
|
+
agentDefinitions,
|
|
977
|
+
runtimeModel,
|
|
978
|
+
messages,
|
|
979
|
+
),
|
|
980
|
+
countCustomAgentTokens(agentDefinitions),
|
|
981
|
+
countSlashCommandTokens(tools, getToolPermissionContext, agentDefinitions),
|
|
982
|
+
approximateMessageTokens(messages),
|
|
983
|
+
])
|
|
984
|
+
|
|
985
|
+
// Count skills separately with error isolation
|
|
986
|
+
const skillResult = await countSkillTokens(
|
|
987
|
+
tools,
|
|
988
|
+
getToolPermissionContext,
|
|
989
|
+
agentDefinitions,
|
|
990
|
+
)
|
|
991
|
+
const skillInfo = skillResult.skillInfo
|
|
992
|
+
// Use sum of individual skill token estimates (matches what's shown in details)
|
|
993
|
+
// rather than skillResult.skillTokens which includes tool schema overhead
|
|
994
|
+
const skillFrontmatterTokens = skillInfo.skillFrontmatter.reduce(
|
|
995
|
+
(sum, skill) => sum + skill.tokens,
|
|
996
|
+
0,
|
|
997
|
+
)
|
|
998
|
+
|
|
999
|
+
const messageTokens = messageBreakdown.totalTokens
|
|
1000
|
+
|
|
1001
|
+
// Check if autocompact is enabled and calculate threshold
|
|
1002
|
+
const isAutoCompact = isAutoCompactEnabled()
|
|
1003
|
+
const autoCompactThreshold = isAutoCompact
|
|
1004
|
+
? getEffectiveContextWindowSize(model) - AUTOCOMPACT_BUFFER_TOKENS
|
|
1005
|
+
: undefined
|
|
1006
|
+
|
|
1007
|
+
// Create categories
|
|
1008
|
+
const cats: ContextCategory[] = []
|
|
1009
|
+
|
|
1010
|
+
// System prompt is always shown first (fixed overhead)
|
|
1011
|
+
if (systemPromptTokens > 0) {
|
|
1012
|
+
cats.push({
|
|
1013
|
+
name: 'System prompt',
|
|
1014
|
+
tokens: systemPromptTokens,
|
|
1015
|
+
color: 'promptBorder',
|
|
1016
|
+
})
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
// Built-in tools right after system prompt (skills shown separately below)
|
|
1020
|
+
// Ant users get a per-tool breakdown via systemToolDetails
|
|
1021
|
+
const systemToolsTokens = builtInToolTokens - skillFrontmatterTokens
|
|
1022
|
+
if (systemToolsTokens > 0) {
|
|
1023
|
+
cats.push({
|
|
1024
|
+
name:
|
|
1025
|
+
process.env.USER_TYPE === 'ant'
|
|
1026
|
+
? '[ANT-ONLY] System tools'
|
|
1027
|
+
: 'System tools',
|
|
1028
|
+
tokens: systemToolsTokens,
|
|
1029
|
+
color: 'inactive',
|
|
1030
|
+
})
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
// MCP tools after system tools
|
|
1034
|
+
if (mcpToolTokens > 0) {
|
|
1035
|
+
cats.push({
|
|
1036
|
+
name: 'MCP tools',
|
|
1037
|
+
tokens: mcpToolTokens,
|
|
1038
|
+
color: 'cyan_FOR_SUBAGENTS_ONLY',
|
|
1039
|
+
})
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
// Show deferred MCP tools (when tool search is enabled)
|
|
1043
|
+
// These don't count toward context usage but we show them for visibility
|
|
1044
|
+
if (deferredToolTokens > 0) {
|
|
1045
|
+
cats.push({
|
|
1046
|
+
name: 'MCP tools (deferred)',
|
|
1047
|
+
tokens: deferredToolTokens,
|
|
1048
|
+
color: 'inactive',
|
|
1049
|
+
isDeferred: true,
|
|
1050
|
+
})
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
// Show deferred builtin tools (when tool search is enabled)
|
|
1054
|
+
if (deferredBuiltinTokens > 0) {
|
|
1055
|
+
cats.push({
|
|
1056
|
+
name: 'System tools (deferred)',
|
|
1057
|
+
tokens: deferredBuiltinTokens,
|
|
1058
|
+
color: 'inactive',
|
|
1059
|
+
isDeferred: true,
|
|
1060
|
+
})
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
// Custom agents after MCP tools
|
|
1064
|
+
if (agentTokens > 0) {
|
|
1065
|
+
cats.push({
|
|
1066
|
+
name: 'Custom agents',
|
|
1067
|
+
tokens: agentTokens,
|
|
1068
|
+
color: 'permission',
|
|
1069
|
+
})
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
// Memory files after custom agents
|
|
1073
|
+
if (claudeMdTokens > 0) {
|
|
1074
|
+
cats.push({
|
|
1075
|
+
name: 'Memory files',
|
|
1076
|
+
tokens: claudeMdTokens,
|
|
1077
|
+
color: 'claude',
|
|
1078
|
+
})
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
// Skills after memory files
|
|
1082
|
+
if (skillFrontmatterTokens > 0) {
|
|
1083
|
+
cats.push({
|
|
1084
|
+
name: 'Skills',
|
|
1085
|
+
tokens: skillFrontmatterTokens,
|
|
1086
|
+
color: 'warning',
|
|
1087
|
+
})
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
if (messageTokens !== null && messageTokens > 0) {
|
|
1091
|
+
cats.push({
|
|
1092
|
+
name: 'Messages',
|
|
1093
|
+
tokens: messageTokens,
|
|
1094
|
+
color: 'purple_FOR_SUBAGENTS_ONLY',
|
|
1095
|
+
})
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
// Calculate actual content usage (before adding reserved buffers)
|
|
1099
|
+
// Exclude deferred categories from the usage calculation
|
|
1100
|
+
const actualUsage = cats.reduce(
|
|
1101
|
+
(sum, cat) => sum + (cat.isDeferred ? 0 : cat.tokens),
|
|
1102
|
+
0,
|
|
1103
|
+
)
|
|
1104
|
+
|
|
1105
|
+
// Reserved space after messages (not counted in actualUsage shown to user).
|
|
1106
|
+
// Under reactive-only mode (cobalt_raccoon), proactive autocompact never
|
|
1107
|
+
// fires and the reserved buffer is a lie — skip it entirely and let Free
|
|
1108
|
+
// space fill the grid. feature() guard keeps the flag string out of
|
|
1109
|
+
// external builds. Same for context-collapse (marble_origami) — collapse
|
|
1110
|
+
// owns the threshold ladder and autocompact is suppressed in
|
|
1111
|
+
// shouldAutoCompact, so the 33k buffer shown here would be a lie too.
|
|
1112
|
+
let reservedTokens = 0
|
|
1113
|
+
let skipReservedBuffer = false
|
|
1114
|
+
if (feature('REACTIVE_COMPACT')) {
|
|
1115
|
+
if (getFeatureValue_CACHED_MAY_BE_STALE('tengu_cobalt_raccoon', false)) {
|
|
1116
|
+
skipReservedBuffer = true
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
if (feature('CONTEXT_COLLAPSE')) {
|
|
1120
|
+
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
1121
|
+
const { isContextCollapseEnabled } =
|
|
1122
|
+
require('../services/contextCollapse/index.js') as typeof import('../services/contextCollapse/index.js')
|
|
1123
|
+
/* eslint-enable @typescript-eslint/no-require-imports */
|
|
1124
|
+
if (isContextCollapseEnabled()) {
|
|
1125
|
+
skipReservedBuffer = true
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
if (skipReservedBuffer) {
|
|
1129
|
+
// No buffer category pushed — reactive compaction is transparent and
|
|
1130
|
+
// doesn't need a visible reservation in the grid.
|
|
1131
|
+
} else if (isAutoCompact && autoCompactThreshold !== undefined) {
|
|
1132
|
+
// Autocompact buffer (from effective context)
|
|
1133
|
+
reservedTokens = contextWindow - autoCompactThreshold
|
|
1134
|
+
cats.push({
|
|
1135
|
+
name: RESERVED_CATEGORY_NAME,
|
|
1136
|
+
tokens: reservedTokens,
|
|
1137
|
+
color: 'inactive',
|
|
1138
|
+
})
|
|
1139
|
+
} else if (!isAutoCompact) {
|
|
1140
|
+
// Compact buffer reserve (3k from actual context limit)
|
|
1141
|
+
reservedTokens = MANUAL_COMPACT_BUFFER_TOKENS
|
|
1142
|
+
cats.push({
|
|
1143
|
+
name: MANUAL_COMPACT_BUFFER_NAME,
|
|
1144
|
+
tokens: reservedTokens,
|
|
1145
|
+
color: 'inactive',
|
|
1146
|
+
})
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
// Calculate free space (subtract both actual usage and reserved buffer)
|
|
1150
|
+
const freeTokens = Math.max(0, contextWindow - actualUsage - reservedTokens)
|
|
1151
|
+
|
|
1152
|
+
cats.push({
|
|
1153
|
+
name: 'Free space',
|
|
1154
|
+
tokens: freeTokens,
|
|
1155
|
+
color: 'promptBorder',
|
|
1156
|
+
})
|
|
1157
|
+
|
|
1158
|
+
// Total for display (everything except free space)
|
|
1159
|
+
const totalIncludingReserved = actualUsage
|
|
1160
|
+
|
|
1161
|
+
// Extract API usage from original messages (if provided) to match status line
|
|
1162
|
+
// This uses the same source of truth as the status line for consistency
|
|
1163
|
+
const apiUsage = getCurrentUsage(originalMessages ?? messages)
|
|
1164
|
+
|
|
1165
|
+
// When API usage is available, use it for total to match status line calculation
|
|
1166
|
+
// Status line uses: input_tokens + cache_creation_input_tokens + cache_read_input_tokens
|
|
1167
|
+
const totalFromAPI = apiUsage
|
|
1168
|
+
? apiUsage.input_tokens +
|
|
1169
|
+
apiUsage.cache_creation_input_tokens +
|
|
1170
|
+
apiUsage.cache_read_input_tokens
|
|
1171
|
+
: null
|
|
1172
|
+
|
|
1173
|
+
// Use API total if available, otherwise fall back to estimated total
|
|
1174
|
+
const finalTotalTokens = totalFromAPI ?? totalIncludingReserved
|
|
1175
|
+
|
|
1176
|
+
// Pre-calculate grid based on model context window and terminal width
|
|
1177
|
+
// For narrow screens (< 80 cols), use 5x5 for 200k models, 5x10 for 1M+ models
|
|
1178
|
+
// For normal screens, use 10x10 for 200k models, 20x10 for 1M+ models
|
|
1179
|
+
const isNarrowScreen = terminalWidth && terminalWidth < 80
|
|
1180
|
+
const GRID_WIDTH =
|
|
1181
|
+
contextWindow >= 1000000
|
|
1182
|
+
? isNarrowScreen
|
|
1183
|
+
? 5
|
|
1184
|
+
: 20
|
|
1185
|
+
: isNarrowScreen
|
|
1186
|
+
? 5
|
|
1187
|
+
: 10
|
|
1188
|
+
const GRID_HEIGHT = contextWindow >= 1000000 ? 10 : isNarrowScreen ? 5 : 10
|
|
1189
|
+
const TOTAL_SQUARES = GRID_WIDTH * GRID_HEIGHT
|
|
1190
|
+
|
|
1191
|
+
// Filter out deferred categories - they don't take up actual context space
|
|
1192
|
+
// (e.g., MCP tools when tool search is enabled)
|
|
1193
|
+
const nonDeferredCats = cats.filter(cat => !cat.isDeferred)
|
|
1194
|
+
|
|
1195
|
+
// Calculate squares per category (use rawEffectiveMax for visualization to show full context)
|
|
1196
|
+
const categorySquares = nonDeferredCats.map(cat => ({
|
|
1197
|
+
...cat,
|
|
1198
|
+
squares:
|
|
1199
|
+
cat.name === 'Free space'
|
|
1200
|
+
? Math.round((cat.tokens / contextWindow) * TOTAL_SQUARES)
|
|
1201
|
+
: Math.max(1, Math.round((cat.tokens / contextWindow) * TOTAL_SQUARES)),
|
|
1202
|
+
percentageOfTotal: Math.round((cat.tokens / contextWindow) * 100),
|
|
1203
|
+
}))
|
|
1204
|
+
|
|
1205
|
+
// Helper function to create grid squares for a category
|
|
1206
|
+
function createCategorySquares(
|
|
1207
|
+
category: (typeof categorySquares)[0],
|
|
1208
|
+
): GridSquare[] {
|
|
1209
|
+
const squares: GridSquare[] = []
|
|
1210
|
+
const exactSquares = (category.tokens / contextWindow) * TOTAL_SQUARES
|
|
1211
|
+
const wholeSquares = Math.floor(exactSquares)
|
|
1212
|
+
const fractionalPart = exactSquares - wholeSquares
|
|
1213
|
+
|
|
1214
|
+
for (let i = 0; i < category.squares; i++) {
|
|
1215
|
+
// Determine fullness: full squares get 1.0, partial square gets fractional amount
|
|
1216
|
+
let squareFullness = 1.0
|
|
1217
|
+
if (i === wholeSquares && fractionalPart > 0) {
|
|
1218
|
+
// This is the partial square
|
|
1219
|
+
squareFullness = fractionalPart
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
squares.push({
|
|
1223
|
+
color: category.color,
|
|
1224
|
+
isFilled: true,
|
|
1225
|
+
categoryName: category.name,
|
|
1226
|
+
tokens: category.tokens,
|
|
1227
|
+
percentage: category.percentageOfTotal,
|
|
1228
|
+
squareFullness,
|
|
1229
|
+
})
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
return squares
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
// Build the grid as an array of squares with full metadata
|
|
1236
|
+
const gridSquares: GridSquare[] = []
|
|
1237
|
+
|
|
1238
|
+
// Separate reserved category for end placement (either autocompact or manual compact buffer)
|
|
1239
|
+
const reservedCategory = categorySquares.find(
|
|
1240
|
+
cat =>
|
|
1241
|
+
cat.name === RESERVED_CATEGORY_NAME ||
|
|
1242
|
+
cat.name === MANUAL_COMPACT_BUFFER_NAME,
|
|
1243
|
+
)
|
|
1244
|
+
const nonReservedCategories = categorySquares.filter(
|
|
1245
|
+
cat =>
|
|
1246
|
+
cat.name !== RESERVED_CATEGORY_NAME &&
|
|
1247
|
+
cat.name !== MANUAL_COMPACT_BUFFER_NAME &&
|
|
1248
|
+
cat.name !== 'Free space',
|
|
1249
|
+
)
|
|
1250
|
+
|
|
1251
|
+
// Add all non-reserved, non-free-space squares first
|
|
1252
|
+
for (const cat of nonReservedCategories) {
|
|
1253
|
+
const squares = createCategorySquares(cat)
|
|
1254
|
+
for (const square of squares) {
|
|
1255
|
+
if (gridSquares.length < TOTAL_SQUARES) {
|
|
1256
|
+
gridSquares.push(square)
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
// Calculate how many squares are needed for reserved
|
|
1262
|
+
const reservedSquareCount = reservedCategory ? reservedCategory.squares : 0
|
|
1263
|
+
|
|
1264
|
+
// Fill with free space, leaving room for reserved at the end
|
|
1265
|
+
const freeSpaceCat = cats.find(c => c.name === 'Free space')
|
|
1266
|
+
const freeSpaceTarget = TOTAL_SQUARES - reservedSquareCount
|
|
1267
|
+
|
|
1268
|
+
while (gridSquares.length < freeSpaceTarget) {
|
|
1269
|
+
gridSquares.push({
|
|
1270
|
+
color: 'promptBorder',
|
|
1271
|
+
isFilled: true,
|
|
1272
|
+
categoryName: 'Free space',
|
|
1273
|
+
tokens: freeSpaceCat?.tokens || 0,
|
|
1274
|
+
percentage: freeSpaceCat
|
|
1275
|
+
? Math.round((freeSpaceCat.tokens / contextWindow) * 100)
|
|
1276
|
+
: 0,
|
|
1277
|
+
squareFullness: 1.0, // Free space is always "full"
|
|
1278
|
+
})
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
// Add reserved squares at the end
|
|
1282
|
+
if (reservedCategory) {
|
|
1283
|
+
const squares = createCategorySquares(reservedCategory)
|
|
1284
|
+
for (const square of squares) {
|
|
1285
|
+
if (gridSquares.length < TOTAL_SQUARES) {
|
|
1286
|
+
gridSquares.push(square)
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
// Convert to rows for rendering
|
|
1292
|
+
const gridRows: GridSquare[][] = []
|
|
1293
|
+
for (let i = 0; i < GRID_HEIGHT; i++) {
|
|
1294
|
+
gridRows.push(gridSquares.slice(i * GRID_WIDTH, (i + 1) * GRID_WIDTH))
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
// Format message breakdown (used by context suggestions for all users)
|
|
1298
|
+
// Combine tool calls and results, then get top 5
|
|
1299
|
+
const toolsMap = new Map<
|
|
1300
|
+
string,
|
|
1301
|
+
{ callTokens: number; resultTokens: number }
|
|
1302
|
+
>()
|
|
1303
|
+
|
|
1304
|
+
// Add call tokens
|
|
1305
|
+
for (const [name, tokens] of messageBreakdown.toolCallsByType.entries()) {
|
|
1306
|
+
const existing = toolsMap.get(name) || { callTokens: 0, resultTokens: 0 }
|
|
1307
|
+
toolsMap.set(name, { ...existing, callTokens: tokens })
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
// Add result tokens
|
|
1311
|
+
for (const [name, tokens] of messageBreakdown.toolResultsByType.entries()) {
|
|
1312
|
+
const existing = toolsMap.get(name) || { callTokens: 0, resultTokens: 0 }
|
|
1313
|
+
toolsMap.set(name, { ...existing, resultTokens: tokens })
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
// Convert to array and sort by total tokens (calls + results)
|
|
1317
|
+
const toolsByTypeArray = Array.from(toolsMap.entries())
|
|
1318
|
+
.map(([name, { callTokens, resultTokens }]) => ({
|
|
1319
|
+
name,
|
|
1320
|
+
callTokens,
|
|
1321
|
+
resultTokens,
|
|
1322
|
+
}))
|
|
1323
|
+
.sort(
|
|
1324
|
+
(a, b) => b.callTokens + b.resultTokens - (a.callTokens + a.resultTokens),
|
|
1325
|
+
)
|
|
1326
|
+
|
|
1327
|
+
const attachmentsByTypeArray = Array.from(
|
|
1328
|
+
messageBreakdown.attachmentsByType.entries(),
|
|
1329
|
+
)
|
|
1330
|
+
.map(([name, tokens]) => ({ name, tokens }))
|
|
1331
|
+
.sort((a, b) => b.tokens - a.tokens)
|
|
1332
|
+
|
|
1333
|
+
const formattedMessageBreakdown = {
|
|
1334
|
+
toolCallTokens: messageBreakdown.toolCallTokens,
|
|
1335
|
+
toolResultTokens: messageBreakdown.toolResultTokens,
|
|
1336
|
+
attachmentTokens: messageBreakdown.attachmentTokens,
|
|
1337
|
+
assistantMessageTokens: messageBreakdown.assistantMessageTokens,
|
|
1338
|
+
userMessageTokens: messageBreakdown.userMessageTokens,
|
|
1339
|
+
toolCallsByType: toolsByTypeArray,
|
|
1340
|
+
attachmentsByType: attachmentsByTypeArray,
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
return {
|
|
1344
|
+
categories: cats,
|
|
1345
|
+
totalTokens: finalTotalTokens,
|
|
1346
|
+
maxTokens: contextWindow,
|
|
1347
|
+
rawMaxTokens: contextWindow,
|
|
1348
|
+
percentage: Math.round((finalTotalTokens / contextWindow) * 100),
|
|
1349
|
+
gridRows,
|
|
1350
|
+
model: runtimeModel,
|
|
1351
|
+
memoryFiles: memoryFileDetails,
|
|
1352
|
+
mcpTools: mcpToolDetails,
|
|
1353
|
+
deferredBuiltinTools:
|
|
1354
|
+
process.env.USER_TYPE === 'ant' ? deferredBuiltinDetails : undefined,
|
|
1355
|
+
systemTools:
|
|
1356
|
+
process.env.USER_TYPE === 'ant' ? systemToolDetails : undefined,
|
|
1357
|
+
systemPromptSections:
|
|
1358
|
+
process.env.USER_TYPE === 'ant' ? systemPromptSections : undefined,
|
|
1359
|
+
agents: agentDetails,
|
|
1360
|
+
slashCommands:
|
|
1361
|
+
slashCommandTokens > 0
|
|
1362
|
+
? {
|
|
1363
|
+
totalCommands: commandInfo.totalCommands,
|
|
1364
|
+
includedCommands: commandInfo.includedCommands,
|
|
1365
|
+
tokens: slashCommandTokens,
|
|
1366
|
+
}
|
|
1367
|
+
: undefined,
|
|
1368
|
+
skills:
|
|
1369
|
+
skillFrontmatterTokens > 0
|
|
1370
|
+
? {
|
|
1371
|
+
totalSkills: skillInfo.totalSkills,
|
|
1372
|
+
includedSkills: skillInfo.includedSkills,
|
|
1373
|
+
tokens: skillFrontmatterTokens,
|
|
1374
|
+
skillFrontmatter: skillInfo.skillFrontmatter,
|
|
1375
|
+
}
|
|
1376
|
+
: undefined,
|
|
1377
|
+
autoCompactThreshold,
|
|
1378
|
+
isAutoCompactEnabled: isAutoCompact,
|
|
1379
|
+
messageBreakdown: formattedMessageBreakdown,
|
|
1380
|
+
apiUsage,
|
|
1381
|
+
}
|
|
1382
|
+
}
|