@within-7/minto 0.2.0 → 0.3.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/dist/commands/agents/AgentsCommand.js +22 -24
- package/dist/commands/agents/AgentsCommand.js.map +2 -2
- package/dist/commands/context.js +2 -1
- package/dist/commands/context.js.map +2 -2
- package/dist/commands/export.js +2 -1
- package/dist/commands/export.js.map +2 -2
- package/dist/commands/mcp-interactive.js +7 -6
- package/dist/commands/mcp-interactive.js.map +2 -2
- package/dist/commands/model.js +3 -2
- package/dist/commands/model.js.map +2 -2
- package/dist/commands/permissions.js +4 -3
- package/dist/commands/permissions.js.map +2 -2
- package/dist/commands/plugin/AddMarketplaceForm.js +3 -2
- package/dist/commands/plugin/AddMarketplaceForm.js.map +2 -2
- package/dist/commands/plugin/ConfirmDialog.js +2 -1
- package/dist/commands/plugin/ConfirmDialog.js.map +2 -2
- package/dist/commands/plugin/ErrorView.js +2 -1
- package/dist/commands/plugin/ErrorView.js.map +2 -2
- package/dist/commands/plugin/InstalledPluginsByMarketplace.js +5 -4
- package/dist/commands/plugin/InstalledPluginsByMarketplace.js.map +2 -2
- package/dist/commands/plugin/InstalledPluginsManager.js +5 -4
- package/dist/commands/plugin/InstalledPluginsManager.js.map +2 -2
- package/dist/commands/plugin/MainMenu.js +2 -1
- package/dist/commands/plugin/MainMenu.js.map +2 -2
- package/dist/commands/plugin/MarketplaceManager.js +5 -4
- package/dist/commands/plugin/MarketplaceManager.js.map +2 -2
- package/dist/commands/plugin/MarketplaceSelector.js +4 -3
- package/dist/commands/plugin/MarketplaceSelector.js.map +2 -2
- package/dist/commands/plugin/PlaceholderScreen.js +3 -2
- package/dist/commands/plugin/PlaceholderScreen.js.map +2 -2
- package/dist/commands/plugin/PluginBrowser.js +6 -5
- package/dist/commands/plugin/PluginBrowser.js.map +2 -2
- package/dist/commands/plugin/PluginDetailsInstall.js +5 -4
- package/dist/commands/plugin/PluginDetailsInstall.js.map +2 -2
- package/dist/commands/plugin/PluginDetailsManage.js +4 -3
- package/dist/commands/plugin/PluginDetailsManage.js.map +2 -2
- package/dist/commands/plugin.js +16 -15
- package/dist/commands/plugin.js.map +2 -2
- package/dist/commands/sandbox.js +4 -3
- package/dist/commands/sandbox.js.map +2 -2
- package/dist/commands/setup.js +2 -1
- package/dist/commands/setup.js.map +2 -2
- package/dist/commands/status.js +2 -1
- package/dist/commands/status.js.map +2 -2
- package/dist/commands/undo.js +245 -0
- package/dist/commands/undo.js.map +7 -0
- package/dist/commands.js +2 -0
- package/dist/commands.js.map +2 -2
- package/dist/components/AgentThinkingBlock.js +1 -1
- package/dist/components/AgentThinkingBlock.js.map +2 -2
- package/dist/components/AsciiLogo.js +7 -8
- package/dist/components/AsciiLogo.js.map +2 -2
- package/dist/components/AskUserQuestionDialog/AskUserQuestionDialog.js +3 -2
- package/dist/components/AskUserQuestionDialog/AskUserQuestionDialog.js.map +2 -2
- package/dist/components/AskUserQuestionDialog/QuestionView.js +2 -1
- package/dist/components/AskUserQuestionDialog/QuestionView.js.map +2 -2
- package/dist/components/CollapsibleHint.js +2 -1
- package/dist/components/CollapsibleHint.js.map +2 -2
- package/dist/components/Config.js +3 -2
- package/dist/components/Config.js.map +2 -2
- package/dist/components/ConsoleOAuthFlow.js +2 -1
- package/dist/components/ConsoleOAuthFlow.js.map +2 -2
- package/dist/components/Cost.js +2 -1
- package/dist/components/Cost.js.map +2 -2
- package/dist/components/HeaderBar.js +13 -8
- package/dist/components/HeaderBar.js.map +2 -2
- package/dist/components/HistorySearchOverlay.js +4 -3
- package/dist/components/HistorySearchOverlay.js.map +2 -2
- package/dist/components/HotkeyHelpPanel.js +8 -11
- package/dist/components/HotkeyHelpPanel.js.map +2 -2
- package/dist/components/InvalidConfigDialog.js +2 -1
- package/dist/components/InvalidConfigDialog.js.map +2 -2
- package/dist/components/Logo.js +23 -67
- package/dist/components/Logo.js.map +2 -2
- package/dist/components/MCPServerApprovalDialog.js +2 -1
- package/dist/components/MCPServerApprovalDialog.js.map +2 -2
- package/dist/components/MCPServerDialogCopy.js +2 -1
- package/dist/components/MCPServerDialogCopy.js.map +2 -2
- package/dist/components/MCPServerMultiselectDialog.js +2 -1
- package/dist/components/MCPServerMultiselectDialog.js.map +2 -2
- package/dist/components/MessageSelector.js +4 -3
- package/dist/components/MessageSelector.js.map +2 -2
- package/dist/components/ModeIndicator.js +2 -1
- package/dist/components/ModeIndicator.js.map +2 -2
- package/dist/components/ModelConfig.js +4 -3
- package/dist/components/ModelConfig.js.map +2 -2
- package/dist/components/ModelListManager.js +4 -3
- package/dist/components/ModelListManager.js.map +2 -2
- package/dist/components/ModelSelector/ModelSelector.js +26 -13
- package/dist/components/ModelSelector/ModelSelector.js.map +2 -2
- package/dist/components/Onboarding.js +3 -2
- package/dist/components/Onboarding.js.map +2 -2
- package/dist/components/OperationSummary.js +130 -0
- package/dist/components/OperationSummary.js.map +7 -0
- package/dist/components/PromptInput.js +88 -75
- package/dist/components/PromptInput.js.map +2 -2
- package/dist/components/SensitiveFileWarning.js +31 -0
- package/dist/components/SensitiveFileWarning.js.map +7 -0
- package/dist/components/Spinner.js +71 -22
- package/dist/components/Spinner.js.map +2 -2
- package/dist/components/StructuredDiff.js +6 -8
- package/dist/components/StructuredDiff.js.map +2 -2
- package/dist/components/SubagentBlock.js +4 -2
- package/dist/components/SubagentBlock.js.map +2 -2
- package/dist/components/SubagentProgress.js +7 -4
- package/dist/components/SubagentProgress.js.map +2 -2
- package/dist/components/TaskCard.js +14 -11
- package/dist/components/TaskCard.js.map +2 -2
- package/dist/components/TextInput.js +9 -1
- package/dist/components/TextInput.js.map +2 -2
- package/dist/components/TodoPanel.js +44 -26
- package/dist/components/TodoPanel.js.map +2 -2
- package/dist/components/ToolUseLoader.js +2 -2
- package/dist/components/ToolUseLoader.js.map +2 -2
- package/dist/components/TreeConnector.js +4 -3
- package/dist/components/TreeConnector.js.map +2 -2
- package/dist/components/TrustDialog.js +2 -1
- package/dist/components/TrustDialog.js.map +2 -2
- package/dist/components/binary-feedback/BinaryFeedbackView.js +2 -1
- package/dist/components/binary-feedback/BinaryFeedbackView.js.map +2 -2
- package/dist/components/messages/AssistantTextMessage.js +17 -9
- package/dist/components/messages/AssistantTextMessage.js.map +2 -2
- package/dist/components/messages/AssistantToolUseMessage.js +8 -4
- package/dist/components/messages/AssistantToolUseMessage.js.map +2 -2
- package/dist/components/messages/GroupRenderer.js +2 -1
- package/dist/components/messages/GroupRenderer.js.map +2 -2
- package/dist/components/messages/NestedTasksPreview.js +13 -1
- package/dist/components/messages/NestedTasksPreview.js.map +2 -2
- package/dist/components/messages/ParallelTasksGroupView.js +4 -3
- package/dist/components/messages/ParallelTasksGroupView.js.map +2 -2
- package/dist/components/messages/TaskInModuleView.js +35 -15
- package/dist/components/messages/TaskInModuleView.js.map +2 -2
- package/dist/components/messages/TaskOutputContent.js +9 -6
- package/dist/components/messages/TaskOutputContent.js.map +2 -2
- package/dist/components/messages/UserPromptMessage.js +2 -2
- package/dist/components/messages/UserPromptMessage.js.map +2 -2
- package/dist/constants/colors.js +90 -72
- package/dist/constants/colors.js.map +2 -2
- package/dist/constants/toolInputExamples.js +84 -0
- package/dist/constants/toolInputExamples.js.map +7 -0
- package/dist/core/backupManager.js +321 -0
- package/dist/core/backupManager.js.map +7 -0
- package/dist/core/costTracker.js +9 -18
- package/dist/core/costTracker.js.map +2 -2
- package/dist/core/gitAutoCommit.js +287 -0
- package/dist/core/gitAutoCommit.js.map +7 -0
- package/dist/core/index.js +3 -0
- package/dist/core/index.js.map +2 -2
- package/dist/core/operationTracker.js +212 -0
- package/dist/core/operationTracker.js.map +7 -0
- package/dist/core/permissions/rules/allowedToolsRule.js +1 -1
- package/dist/core/permissions/rules/allowedToolsRule.js.map +2 -2
- package/dist/core/permissions/rules/autoEscalationRule.js +5 -0
- package/dist/core/permissions/rules/autoEscalationRule.js.map +2 -2
- package/dist/core/permissions/rules/projectBoundaryRule.js +5 -0
- package/dist/core/permissions/rules/projectBoundaryRule.js.map +2 -2
- package/dist/core/permissions/rules/sensitivePathsRule.js +5 -0
- package/dist/core/permissions/rules/sensitivePathsRule.js.map +2 -2
- package/dist/core/tokenStats.js +9 -0
- package/dist/core/tokenStats.js.map +7 -0
- package/dist/core/tokenStatsManager.js +331 -0
- package/dist/core/tokenStatsManager.js.map +7 -0
- package/dist/entrypoints/cli.js +115 -87
- package/dist/entrypoints/cli.js.map +2 -2
- package/dist/hooks/useAgentTokenStats.js +72 -0
- package/dist/hooks/useAgentTokenStats.js.map +7 -0
- package/dist/hooks/useAgentTranscripts.js +30 -6
- package/dist/hooks/useAgentTranscripts.js.map +2 -2
- package/dist/hooks/useLogMessages.js +12 -1
- package/dist/hooks/useLogMessages.js.map +2 -2
- package/dist/i18n/locales/en.js +6 -5
- package/dist/i18n/locales/en.js.map +2 -2
- package/dist/i18n/locales/zh-CN.js +6 -5
- package/dist/i18n/locales/zh-CN.js.map +2 -2
- package/dist/i18n/types.js.map +1 -1
- package/dist/permissions.js +28 -1
- package/dist/permissions.js.map +2 -2
- package/dist/query.js +78 -4
- package/dist/query.js.map +3 -3
- package/dist/screens/REPL.js +23 -3
- package/dist/screens/REPL.js.map +2 -2
- package/dist/services/claude.js +54 -3
- package/dist/services/claude.js.map +2 -2
- package/dist/services/intelligentCompactor.js +1 -1
- package/dist/services/intelligentCompactor.js.map +2 -2
- package/dist/services/mcpClient.js +81 -25
- package/dist/services/mcpClient.js.map +2 -2
- package/dist/services/sandbox/filesystemBoundary.js +58 -17
- package/dist/services/sandbox/filesystemBoundary.js.map +2 -2
- package/dist/tools/AskExpertModelTool/AskExpertModelTool.js +3 -2
- package/dist/tools/AskExpertModelTool/AskExpertModelTool.js.map +2 -2
- package/dist/tools/AskUserQuestionTool/AskUserQuestionTool.js +2 -1
- package/dist/tools/AskUserQuestionTool/AskUserQuestionTool.js.map +2 -2
- package/dist/tools/BashTool/BashTool.js +22 -3
- package/dist/tools/BashTool/BashTool.js.map +2 -2
- package/dist/tools/BashTool/prompt.js +178 -34
- package/dist/tools/BashTool/prompt.js.map +2 -2
- package/dist/tools/FileEditTool/prompt.js +6 -3
- package/dist/tools/FileEditTool/prompt.js.map +2 -2
- package/dist/tools/FileWriteTool/prompt.js +4 -2
- package/dist/tools/FileWriteTool/prompt.js.map +2 -2
- package/dist/tools/MultiEditTool/prompt.js +5 -3
- package/dist/tools/MultiEditTool/prompt.js.map +2 -2
- package/dist/tools/NotebookEditTool/NotebookEditTool.js +2 -1
- package/dist/tools/NotebookEditTool/NotebookEditTool.js.map +2 -2
- package/dist/tools/PlanModeTool/EnterPlanModeTool.js +3 -2
- package/dist/tools/PlanModeTool/EnterPlanModeTool.js.map +2 -2
- package/dist/tools/PlanModeTool/ExitPlanModeTool.js +3 -2
- package/dist/tools/PlanModeTool/ExitPlanModeTool.js.map +2 -2
- package/dist/tools/PlanModeTool/prompt.js +1 -1
- package/dist/tools/PlanModeTool/prompt.js.map +1 -1
- package/dist/tools/SkillTool/SkillTool.js +4 -3
- package/dist/tools/SkillTool/SkillTool.js.map +2 -2
- package/dist/tools/SkillTool/prompt.js +1 -1
- package/dist/tools/SkillTool/prompt.js.map +1 -1
- package/dist/tools/TaskOutputTool/TaskOutputTool.js +3 -2
- package/dist/tools/TaskOutputTool/TaskOutputTool.js.map +2 -2
- package/dist/tools/TaskTool/TaskTool.js +8 -0
- package/dist/tools/TaskTool/TaskTool.js.map +2 -2
- package/dist/utils/CircuitBreaker.js +242 -0
- package/dist/utils/CircuitBreaker.js.map +7 -0
- package/dist/utils/ask.js +2 -0
- package/dist/utils/ask.js.map +2 -2
- package/dist/utils/config.js +47 -5
- package/dist/utils/config.js.map +2 -2
- package/dist/utils/credentials/CredentialStore.js +1 -0
- package/dist/utils/credentials/CredentialStore.js.map +7 -0
- package/dist/utils/credentials/EncryptedFileStore.js +157 -0
- package/dist/utils/credentials/EncryptedFileStore.js.map +7 -0
- package/dist/utils/credentials/index.js +37 -0
- package/dist/utils/credentials/index.js.map +7 -0
- package/dist/utils/credentials/migration.js +82 -0
- package/dist/utils/credentials/migration.js.map +7 -0
- package/dist/utils/markdown.js +13 -1
- package/dist/utils/markdown.js.map +2 -2
- package/dist/utils/permissions/filesystem.js +5 -1
- package/dist/utils/permissions/filesystem.js.map +2 -2
- package/dist/utils/safePath.js +132 -0
- package/dist/utils/safePath.js.map +7 -0
- package/dist/utils/sensitiveFiles.js +125 -0
- package/dist/utils/sensitiveFiles.js.map +7 -0
- package/dist/utils/taskDisplayUtils.js +9 -9
- package/dist/utils/taskDisplayUtils.js.map +2 -2
- package/dist/utils/theme.js +6 -6
- package/dist/utils/theme.js.map +1 -1
- package/dist/utils/toolRiskClassification.js +207 -0
- package/dist/utils/toolRiskClassification.js.map +7 -0
- package/dist/utils/tooling/safeRender.js +5 -4
- package/dist/utils/tooling/safeRender.js.map +2 -2
- package/dist/version.js +2 -2
- package/dist/version.js.map +1 -1
- package/package.json +9 -7
- package/dist/hooks/useCancelRequest.js +0 -31
- package/dist/hooks/useCancelRequest.js.map +0 -7
package/dist/permissions.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/permissions.ts"],
|
|
4
|
-
"sourcesContent": ["import type { CanUseToolFn } from './hooks/useCanUseTool'\nimport { Tool, ToolUseContext } from './Tool'\nimport { BashTool, inputSchema } from './tools/BashTool/BashTool'\nimport { FileEditTool } from './tools/FileEditTool/FileEditTool'\nimport { FileWriteTool } from './tools/FileWriteTool/FileWriteTool'\nimport { NotebookEditTool } from './tools/NotebookEditTool/NotebookEditTool'\nimport { getCommandSubcommandPrefix, splitCommand } from './utils/commands'\nimport {\n getCurrentProjectConfig,\n saveCurrentProjectConfig,\n} from '@utils/config'\nimport { AbortError } from './utils/errors'\nimport { logError } from './utils/log'\nimport { grantWritePermissionForOriginalDir } from './utils/permissions/filesystem'\nimport { getCwd } from './utils/state'\nimport { PRODUCT_NAME } from './constants/product'\n\n// Commands that are known to be safe for execution\nconst SAFE_COMMANDS = new Set([\n 'git status',\n 'git diff',\n 'git log',\n 'git branch',\n 'pwd',\n 'tree',\n 'date',\n 'which',\n])\n\nexport const bashToolCommandHasExactMatchPermission = (\n tool: Tool,\n command: string,\n allowedTools: string[],\n): boolean => {\n if (SAFE_COMMANDS.has(command)) {\n return true\n }\n // Check exact match first\n if (allowedTools.includes(getPermissionKey(tool, { command }, null))) {\n return true\n }\n // Check if command is an exact match with an approved prefix\n if (allowedTools.includes(getPermissionKey(tool, { command }, command))) {\n return true\n }\n return false\n}\n\nexport const bashToolCommandHasPermission = (\n tool: Tool,\n command: string,\n prefix: string | null,\n allowedTools: string[],\n): boolean => {\n // Check exact match first\n if (bashToolCommandHasExactMatchPermission(tool, command, allowedTools)) {\n return true\n }\n return allowedTools.includes(getPermissionKey(tool, { command }, prefix))\n}\n\nexport const bashToolHasPermission = async (\n tool: Tool,\n command: string,\n context: ToolUseContext,\n allowedTools: string[],\n getCommandSubcommandPrefixFn = getCommandSubcommandPrefix,\n): Promise<PermissionResult> => {\n if (bashToolCommandHasExactMatchPermission(tool, command, allowedTools)) {\n // This is an exact match for a command that is allowed, so we can skip the prefix check\n return { result: true }\n }\n\n const subCommands = splitCommand(command).filter(_ => {\n // Denim likes to add this, we strip it out so we don't need to prompt the user each time\n if (_ === `cd ${getCwd()}`) {\n return false\n }\n return true\n })\n const commandSubcommandPrefix = await getCommandSubcommandPrefixFn(\n command,\n context.abortController.signal,\n )\n if (context.abortController.signal.aborted) {\n throw new AbortError()\n }\n\n if (commandSubcommandPrefix === null) {\n // Fail closed and ask for user approval if the command prefix query failed (e.g. due to network error)\n // This is NOT the same as `fullCommandPrefix.commandPrefix === null`, which means no prefix was detected\n return {\n result: false,\n message: `${PRODUCT_NAME} requested permissions to use ${tool.name}, but you haven't granted it yet.`,\n }\n }\n\n if (commandSubcommandPrefix.commandInjectionDetected) {\n // Only allow exact matches for potential command injections\n if (bashToolCommandHasExactMatchPermission(tool, command, allowedTools)) {\n return { result: true }\n } else {\n return {\n result: false,\n message: `${PRODUCT_NAME} requested permissions to use ${tool.name}, but you haven't granted it yet.`,\n }\n }\n }\n\n // After the commandInjectionDetected check above, TypeScript still sees the union type.\n // We know commandInjectionDetected is false here, so commandPrefix exists.\n // Use 'in' check for proper type narrowing\n const commandPrefix =\n 'commandPrefix' in commandSubcommandPrefix\n ? commandSubcommandPrefix.commandPrefix\n : null\n\n // If there is only one command, no need to process subCommands\n if (subCommands.length < 2) {\n if (\n bashToolCommandHasPermission(tool, command, commandPrefix, allowedTools)\n ) {\n return { result: true }\n } else {\n return {\n result: false,\n message: `${PRODUCT_NAME} requested permissions to use ${tool.name}, but you haven't granted it yet.`,\n }\n }\n }\n if (\n subCommands.every(subCommand => {\n const prefixResult =\n commandSubcommandPrefix.subcommandPrefixes.get(subCommand)\n if (prefixResult === undefined || prefixResult.commandInjectionDetected) {\n // If prefix result is missing or command injection is detected, always ask for permission\n return false\n }\n // After the check above, we know commandInjectionDetected is false\n // Use 'in' check for proper type narrowing\n const subCommandPrefix =\n 'commandPrefix' in prefixResult ? prefixResult.commandPrefix : null\n const hasPermission = bashToolCommandHasPermission(\n tool,\n subCommand,\n subCommandPrefix,\n allowedTools,\n )\n return hasPermission\n })\n ) {\n return { result: true }\n }\n return {\n result: false,\n message: `${PRODUCT_NAME} requested permissions to use ${tool.name}, but you haven't granted it yet.`,\n }\n}\n\ntype PermissionResult = { result: true } | { result: false; message: string }\n\nexport const hasPermissionsToUseTool: CanUseToolFn = async (\n tool,\n input,\n context,\n _assistantMessage,\n): Promise<PermissionResult> => {\n // If safe mode is not enabled, allow all tools (permissive by default)\n if (!context.options.safeMode) {\n return { result: true }\n }\n\n if (context.abortController.signal.aborted) {\n throw new AbortError()\n }\n\n // Check if the tool needs permissions\n try {\n if (!tool.needsPermissions(input as never)) {\n return { result: true }\n }\n } catch (e) {\n logError(`Error checking permissions: ${e}`)\n return { result: false, message: 'Error checking permissions' }\n }\n\n const projectConfig = getCurrentProjectConfig()\n const allowedTools = projectConfig.allowedTools ?? []\n // Special case for BashTool to allow blanket commands without exposing them in the UI\n if (tool === BashTool && allowedTools.includes(BashTool.name)) {\n return { result: true }\n }\n\n // TODO: Move this into tool definitions (done for read tools!)\n switch (tool) {\n // For bash tool, check each sub-command's permissions separately\n case BashTool: {\n // The types have already been validated by the tool,\n // so we can safely parse the input (as opposed to safeParse).\n const { command } = inputSchema.parse(input)\n return await bashToolHasPermission(tool, command, context, allowedTools)\n }\n // For file editing tools, check session-only permissions\n case FileEditTool:\n case FileWriteTool:\n case NotebookEditTool: {\n // The types have already been validated by the tool,\n // so we can safely pass this in\n if (!tool.needsPermissions(input)) {\n return { result: true }\n }\n return {\n result: false,\n message: `${PRODUCT_NAME} requested permissions to use ${tool.name}, but you haven't granted it yet.`,\n }\n }\n // For other tools, check persistent permissions\n default: {\n const permissionKey = getPermissionKey(tool, input, null)\n if (allowedTools.includes(permissionKey)) {\n return { result: true }\n }\n\n return {\n result: false,\n message: `${PRODUCT_NAME} requested permissions to use ${tool.name}, but you haven't granted it yet.`,\n }\n }\n }\n}\n\nexport async function savePermission(\n tool: Tool,\n input: { [k: string]: unknown },\n prefix: string | null,\n): Promise<void> {\n const key = getPermissionKey(tool, input, prefix)\n\n // For file editing tools, store write permissions only in memory\n if (\n tool === FileEditTool ||\n tool === FileWriteTool ||\n tool === NotebookEditTool\n ) {\n grantWritePermissionForOriginalDir()\n return\n }\n\n // For other tools, store permissions on disk\n const projectConfig = getCurrentProjectConfig()\n if (projectConfig.allowedTools.includes(key)) {\n return\n }\n\n projectConfig.allowedTools.push(key)\n projectConfig.allowedTools.sort()\n\n saveCurrentProjectConfig(projectConfig)\n}\n\nfunction getPermissionKey(\n tool: Tool,\n input: { [k: string]: unknown },\n prefix: string | null,\n): string {\n switch (tool) {\n case BashTool:\n if (prefix) {\n return `${BashTool.name}(${prefix}:*)`\n }\n return `${BashTool.name}(${BashTool.renderToolUseMessage(input as never)})`\n default:\n return tool.name\n }\n}\n"],
|
|
5
|
-
"mappings": "AAEA,SAAS,UAAU,mBAAmB;AACtC,SAAS,oBAAoB;AAC7B,SAAS,qBAAqB;AAC9B,SAAS,wBAAwB;AACjC,SAAS,4BAA4B,oBAAoB;AACzD;AAAA,EACE;AAAA,EACA;AAAA,
|
|
4
|
+
"sourcesContent": ["import type { CanUseToolFn } from './hooks/useCanUseTool'\nimport { Tool, ToolUseContext } from './Tool'\nimport { BashTool, inputSchema } from './tools/BashTool/BashTool'\nimport { FileEditTool } from './tools/FileEditTool/FileEditTool'\nimport { FileWriteTool } from './tools/FileWriteTool/FileWriteTool'\nimport { NotebookEditTool } from './tools/NotebookEditTool/NotebookEditTool'\nimport { getCommandSubcommandPrefix, splitCommand } from './utils/commands'\nimport {\n getCurrentProjectConfig,\n saveCurrentProjectConfig,\n type SafetyMode,\n} from '@utils/config'\nimport { AbortError } from './utils/errors'\nimport { logError } from './utils/log'\nimport { grantWritePermissionForOriginalDir } from './utils/permissions/filesystem'\nimport { getCwd } from './utils/state'\nimport { PRODUCT_NAME } from './constants/product'\nimport {\n getToolRiskLevel,\n type RiskLevel,\n} from './utils/toolRiskClassification'\n\n// Commands that are known to be safe for execution\nconst SAFE_COMMANDS = new Set([\n 'git status',\n 'git diff',\n 'git log',\n 'git branch',\n 'pwd',\n 'tree',\n 'date',\n 'which',\n])\n\nexport const bashToolCommandHasExactMatchPermission = (\n tool: Tool,\n command: string,\n allowedTools: string[],\n): boolean => {\n if (SAFE_COMMANDS.has(command)) {\n return true\n }\n // Check exact match first\n if (allowedTools.includes(getPermissionKey(tool, { command }, null))) {\n return true\n }\n // Check if command is an exact match with an approved prefix\n if (allowedTools.includes(getPermissionKey(tool, { command }, command))) {\n return true\n }\n return false\n}\n\nexport const bashToolCommandHasPermission = (\n tool: Tool,\n command: string,\n prefix: string | null,\n allowedTools: string[],\n): boolean => {\n // Check exact match first\n if (bashToolCommandHasExactMatchPermission(tool, command, allowedTools)) {\n return true\n }\n return allowedTools.includes(getPermissionKey(tool, { command }, prefix))\n}\n\nexport const bashToolHasPermission = async (\n tool: Tool,\n command: string,\n context: ToolUseContext,\n allowedTools: string[],\n getCommandSubcommandPrefixFn = getCommandSubcommandPrefix,\n): Promise<PermissionResult> => {\n if (bashToolCommandHasExactMatchPermission(tool, command, allowedTools)) {\n // This is an exact match for a command that is allowed, so we can skip the prefix check\n return { result: true }\n }\n\n const subCommands = splitCommand(command).filter(_ => {\n // Denim likes to add this, we strip it out so we don't need to prompt the user each time\n if (_ === `cd ${getCwd()}`) {\n return false\n }\n return true\n })\n const commandSubcommandPrefix = await getCommandSubcommandPrefixFn(\n command,\n context.abortController.signal,\n )\n if (context.abortController.signal.aborted) {\n throw new AbortError()\n }\n\n if (commandSubcommandPrefix === null) {\n // Fail closed and ask for user approval if the command prefix query failed (e.g. due to network error)\n // This is NOT the same as `fullCommandPrefix.commandPrefix === null`, which means no prefix was detected\n return {\n result: false,\n message: `${PRODUCT_NAME} requested permissions to use ${tool.name}, but you haven't granted it yet.`,\n }\n }\n\n if (commandSubcommandPrefix.commandInjectionDetected) {\n // Only allow exact matches for potential command injections\n if (bashToolCommandHasExactMatchPermission(tool, command, allowedTools)) {\n return { result: true }\n } else {\n return {\n result: false,\n message: `${PRODUCT_NAME} requested permissions to use ${tool.name}, but you haven't granted it yet.`,\n }\n }\n }\n\n // After the commandInjectionDetected check above, TypeScript still sees the union type.\n // We know commandInjectionDetected is false here, so commandPrefix exists.\n // Use 'in' check for proper type narrowing\n const commandPrefix =\n 'commandPrefix' in commandSubcommandPrefix\n ? commandSubcommandPrefix.commandPrefix\n : null\n\n // If there is only one command, no need to process subCommands\n if (subCommands.length < 2) {\n if (\n bashToolCommandHasPermission(tool, command, commandPrefix, allowedTools)\n ) {\n return { result: true }\n } else {\n return {\n result: false,\n message: `${PRODUCT_NAME} requested permissions to use ${tool.name}, but you haven't granted it yet.`,\n }\n }\n }\n if (\n subCommands.every(subCommand => {\n const prefixResult =\n commandSubcommandPrefix.subcommandPrefixes.get(subCommand)\n if (prefixResult === undefined || prefixResult.commandInjectionDetected) {\n // If prefix result is missing or command injection is detected, always ask for permission\n return false\n }\n // After the check above, we know commandInjectionDetected is false\n // Use 'in' check for proper type narrowing\n const subCommandPrefix =\n 'commandPrefix' in prefixResult ? prefixResult.commandPrefix : null\n const hasPermission = bashToolCommandHasPermission(\n tool,\n subCommand,\n subCommandPrefix,\n allowedTools,\n )\n return hasPermission\n })\n ) {\n return { result: true }\n }\n return {\n result: false,\n message: `${PRODUCT_NAME} requested permissions to use ${tool.name}, but you haven't granted it yet.`,\n }\n}\n\ntype PermissionResult = { result: true } | { result: false; message: string }\n\n/**\n * Get effective safety mode from context options\n * Handles backward compatibility with legacy safeMode boolean\n */\nfunction getEffectiveSafetyMode(options: {\n safeMode?: boolean\n safetyMode?: SafetyMode\n}): SafetyMode {\n // New safetyMode takes precedence\n if (options.safetyMode) {\n return options.safetyMode\n }\n // Backward compatibility: convert legacy safeMode boolean\n // safeMode: true \u2192 'strict' (old behavior)\n // safeMode: false \u2192 'yolo' (permissive)\n if (options.safeMode === true) {\n return 'strict'\n }\n // Default to 'yolo' for non-technical users (zero interruption)\n return 'yolo'\n}\n\n/**\n * Check if a tool should be allowed based on safety mode and risk level\n *\n * Safety Mode Matrix:\n * | Mode | Safe Tools | Monitored Tools | Dangerous Tools |\n * |--------|------------|-----------------|-----------------|\n * | yolo | \u2713 allow | \u2713 allow | \u2713 allow |\n * | smart | \u2713 allow | \u2713 allow | \u26A0 ask |\n * | strict | \u2713 allow | \u26A0 ask | \u26A0 ask |\n */\nfunction shouldAllowByRiskLevel(\n safetyMode: SafetyMode,\n riskLevel: RiskLevel,\n): boolean {\n switch (safetyMode) {\n case 'yolo':\n // YOLO mode: allow everything\n return true\n case 'smart':\n // Smart mode: allow safe and monitored, ask for dangerous\n return riskLevel === 'safe' || riskLevel === 'monitored'\n case 'strict':\n // Strict mode: only allow safe tools\n return riskLevel === 'safe'\n }\n}\n\nexport const hasPermissionsToUseTool: CanUseToolFn = async (\n tool,\n input,\n context,\n _assistantMessage,\n): Promise<PermissionResult> => {\n const safetyMode = getEffectiveSafetyMode(context.options)\n\n // Get risk level for this tool (with command-specific classification for Bash)\n const riskLevel = getToolRiskLevel(\n tool.name,\n input as { command?: string } | undefined,\n )\n\n // Check if this tool should be auto-allowed based on safety mode and risk level\n if (shouldAllowByRiskLevel(safetyMode, riskLevel)) {\n return { result: true }\n }\n\n if (context.abortController.signal.aborted) {\n throw new AbortError()\n }\n\n // Check if the tool needs permissions\n try {\n if (!tool.needsPermissions(input as never)) {\n return { result: true }\n }\n } catch (e) {\n logError(`Error checking permissions: ${e}`)\n return { result: false, message: 'Error checking permissions' }\n }\n\n const projectConfig = getCurrentProjectConfig()\n const allowedTools = projectConfig.allowedTools ?? []\n // Special case for BashTool to allow blanket commands without exposing them in the UI\n if (tool === BashTool && allowedTools.includes(BashTool.name)) {\n return { result: true }\n }\n\n // TODO: Move this into tool definitions (done for read tools!)\n switch (tool) {\n // For bash tool, check each sub-command's permissions separately\n case BashTool: {\n // The types have already been validated by the tool,\n // so we can safely parse the input (as opposed to safeParse).\n const { command } = inputSchema.parse(input)\n return await bashToolHasPermission(tool, command, context, allowedTools)\n }\n // For file editing tools, check session-only permissions\n case FileEditTool:\n case FileWriteTool:\n case NotebookEditTool: {\n // The types have already been validated by the tool,\n // so we can safely pass this in\n if (!tool.needsPermissions(input)) {\n return { result: true }\n }\n return {\n result: false,\n message: `${PRODUCT_NAME} requested permissions to use ${tool.name}, but you haven't granted it yet.`,\n }\n }\n // For other tools, check persistent permissions\n default: {\n const permissionKey = getPermissionKey(tool, input, null)\n if (allowedTools.includes(permissionKey)) {\n return { result: true }\n }\n\n return {\n result: false,\n message: `${PRODUCT_NAME} requested permissions to use ${tool.name}, but you haven't granted it yet.`,\n }\n }\n }\n}\n\nexport async function savePermission(\n tool: Tool,\n input: { [k: string]: unknown },\n prefix: string | null,\n): Promise<void> {\n const key = getPermissionKey(tool, input, prefix)\n\n // For file editing tools, store write permissions only in memory\n if (\n tool === FileEditTool ||\n tool === FileWriteTool ||\n tool === NotebookEditTool\n ) {\n grantWritePermissionForOriginalDir()\n return\n }\n\n // For other tools, store permissions on disk\n const projectConfig = getCurrentProjectConfig()\n if (projectConfig.allowedTools.includes(key)) {\n return\n }\n\n projectConfig.allowedTools.push(key)\n projectConfig.allowedTools.sort()\n\n saveCurrentProjectConfig(projectConfig)\n}\n\nfunction getPermissionKey(\n tool: Tool,\n input: { [k: string]: unknown },\n prefix: string | null,\n): string {\n switch (tool) {\n case BashTool:\n if (prefix) {\n return `${BashTool.name}(${prefix}:*)`\n }\n return `${BashTool.name}(${BashTool.renderToolUseMessage(input as never)})`\n default:\n return tool.name\n }\n}\n"],
|
|
5
|
+
"mappings": "AAEA,SAAS,UAAU,mBAAmB;AACtC,SAAS,oBAAoB;AAC7B,SAAS,qBAAqB;AAC9B,SAAS,wBAAwB;AACjC,SAAS,4BAA4B,oBAAoB;AACzD;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAS,kBAAkB;AAC3B,SAAS,gBAAgB;AACzB,SAAS,0CAA0C;AACnD,SAAS,cAAc;AACvB,SAAS,oBAAoB;AAC7B;AAAA,EACE;AAAA,OAEK;AAGP,MAAM,gBAAgB,oBAAI,IAAI;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAEM,MAAM,yCAAyC,CACpD,MACA,SACA,iBACY;AACZ,MAAI,cAAc,IAAI,OAAO,GAAG;AAC9B,WAAO;AAAA,EACT;AAEA,MAAI,aAAa,SAAS,iBAAiB,MAAM,EAAE,QAAQ,GAAG,IAAI,CAAC,GAAG;AACpE,WAAO;AAAA,EACT;AAEA,MAAI,aAAa,SAAS,iBAAiB,MAAM,EAAE,QAAQ,GAAG,OAAO,CAAC,GAAG;AACvE,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAEO,MAAM,+BAA+B,CAC1C,MAC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AAc,aAAa,KAAK;AAEhC,2BAAyB,aAAa;AACxC;AAEA,SAAS,iBACP,MACA,OACA,QACQ;AACR,UAAQ,MAAM;AAAA,IACZ,KAAK;AACH,UAAI,QAAQ;AACV,eAAO,GAAG,SAAS,IAAI,IAAI,MAAM;AAAA,MACnC;AACA,aAAO,GAAG,SAAS,IAAI,IAAI,SAAS,qBAAqB,KAAc,CAAC;AAAA,IAC1E;AACE,aAAO,KAAK;AAAA,EAChB;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/query.js
CHANGED
|
@@ -7,6 +7,11 @@ import {
|
|
|
7
7
|
queryLLM
|
|
8
8
|
} from "./services/claude.js";
|
|
9
9
|
import { emitReminderEvent } from "./services/systemReminder.js";
|
|
10
|
+
import {
|
|
11
|
+
TOOL_INPUT_EXAMPLES,
|
|
12
|
+
TOOL_DESCRIPTIONS,
|
|
13
|
+
PARAMETER_DESCRIPTIONS
|
|
14
|
+
} from "./constants/toolInputExamples.js";
|
|
10
15
|
import { all } from "./utils/generators.js";
|
|
11
16
|
import { logError } from "./utils/log.js";
|
|
12
17
|
import {
|
|
@@ -455,13 +460,82 @@ function normalizeToolInput(tool, input) {
|
|
|
455
460
|
return input;
|
|
456
461
|
}
|
|
457
462
|
}
|
|
463
|
+
function buildToolInputErrorMessage(tool, input, validationError) {
|
|
464
|
+
const parseError = input;
|
|
465
|
+
if (parseError.__parse_error__) {
|
|
466
|
+
let message2 = [
|
|
467
|
+
`Error: Failed to parse tool parameters for "${tool.name}".`,
|
|
468
|
+
``,
|
|
469
|
+
`The model generated invalid JSON. This is typically a temporary issue.`,
|
|
470
|
+
``,
|
|
471
|
+
`Expected format: ${TOOL_INPUT_EXAMPLES[tool.name] || "{...}"}`
|
|
472
|
+
];
|
|
473
|
+
if (parseError.__error_message__) {
|
|
474
|
+
message2.push(`Parse error: ${parseError.__error_message__}`);
|
|
475
|
+
}
|
|
476
|
+
if (parseError.__raw_json_preview__) {
|
|
477
|
+
message2.push(``, `Raw JSON preview:`, parseError.__raw_json_preview__);
|
|
478
|
+
}
|
|
479
|
+
message2.push(
|
|
480
|
+
``,
|
|
481
|
+
`Suggestions:`,
|
|
482
|
+
`\u2022 Retry your request - the model may succeed on the next attempt`,
|
|
483
|
+
`\u2022 Try simplifying your request with fewer parameters`,
|
|
484
|
+
`\u2022 Check if special characters in values need escaping`
|
|
485
|
+
);
|
|
486
|
+
return message2.join("\n");
|
|
487
|
+
}
|
|
488
|
+
const inputKeys = Object.keys(input);
|
|
489
|
+
const isEmptyInput = inputKeys.length === 0;
|
|
490
|
+
const example = TOOL_INPUT_EXAMPLES[tool.name] || "{...required parameters...}";
|
|
491
|
+
const description = TOOL_DESCRIPTIONS[tool.name];
|
|
492
|
+
const paramDescriptions = PARAMETER_DESCRIPTIONS[tool.name];
|
|
493
|
+
let message = [
|
|
494
|
+
`Error: Invalid parameters for "${tool.name}" tool.`,
|
|
495
|
+
``,
|
|
496
|
+
`Expected format:`,
|
|
497
|
+
`${example}`
|
|
498
|
+
];
|
|
499
|
+
if (description) {
|
|
500
|
+
message.push(``, `Description: ${description}`);
|
|
501
|
+
}
|
|
502
|
+
if (isEmptyInput) {
|
|
503
|
+
message.push(
|
|
504
|
+
``,
|
|
505
|
+
`Issue: The tool was called without any parameters.`,
|
|
506
|
+
`This usually indicates a JSON parsing error in the model's response.`
|
|
507
|
+
);
|
|
508
|
+
} else {
|
|
509
|
+
message.push(
|
|
510
|
+
``,
|
|
511
|
+
`Issue: ${validationError.message}`,
|
|
512
|
+
`Provided parameters: ${inputKeys.length > 0 ? inputKeys.join(", ") : "(none)"}`
|
|
513
|
+
);
|
|
514
|
+
if (paramDescriptions) {
|
|
515
|
+
message.push(``, `Parameter guide:`);
|
|
516
|
+
for (const [param, desc] of Object.entries(paramDescriptions)) {
|
|
517
|
+
message.push(`\u2022 ${param}: ${desc}`);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
message.push(
|
|
522
|
+
``,
|
|
523
|
+
`Suggestions:`,
|
|
524
|
+
`\u2022 Ensure all required parameters are provided`,
|
|
525
|
+
`\u2022 Check that parameters have the correct type (string, number, boolean)`,
|
|
526
|
+
`\u2022 Verify file paths are absolute (start with /)`,
|
|
527
|
+
`\u2022 Retry your request - the model often succeeds on retry`
|
|
528
|
+
);
|
|
529
|
+
return message.join("\n");
|
|
530
|
+
}
|
|
458
531
|
async function* checkPermissionsAndCallTool(tool, toolUseID, siblingToolUseIDs, input, context, canUseTool, assistantMessage, shouldSkipPermissionCheck) {
|
|
459
532
|
const isValidInput = tool.inputSchema.safeParse(input);
|
|
460
533
|
if (!isValidInput.success) {
|
|
461
|
-
let errorMessage =
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
534
|
+
let errorMessage = buildToolInputErrorMessage(
|
|
535
|
+
tool,
|
|
536
|
+
input,
|
|
537
|
+
isValidInput.error
|
|
538
|
+
);
|
|
465
539
|
yield createUserMessage([
|
|
466
540
|
{
|
|
467
541
|
type: "tool_result",
|
package/dist/query.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/query.ts"],
|
|
4
|
-
"sourcesContent": ["import {\n Message as APIAssistantMessage,\n MessageParam,\n ToolUseBlock,\n} from '@anthropic-ai/sdk/resources/index.mjs'\nimport type { UUID } from './types/common'\nimport type { ThinkingMetadata } from './types/thinking'\nimport type { Tool, ToolUseContext, AskUserFn } from './Tool'\nimport {\n messagePairValidForBinaryFeedback,\n shouldUseBinaryFeedback,\n} from '@components/binary-feedback/utils'\nimport { CanUseToolFn } from './hooks/useCanUseTool'\nimport {\n formatSystemPromptWithContext,\n queryLLM,\n queryModel,\n} from '@services/claude'\nimport { emitReminderEvent } from '@services/systemReminder'\nimport { all } from '@utils/generators'\nimport { logError } from '@utils/log'\nimport {\n debug,\n markPhase,\n getCurrentRequest,\n logUserFriendly,\n} from './utils/debugLogger'\nimport { getModelManager } from '@utils/model'\nimport {\n createAssistantMessage,\n createAssistantAPIErrorMessage,\n createProgressMessage,\n createToolResultStopMessage,\n createUserMessage,\n FullToolUseResult,\n INTERRUPT_MESSAGE,\n INTERRUPT_MESSAGE_FOR_TOOL_USE,\n NormalizedMessage,\n normalizeMessagesForAPI,\n} from '@utils/messages'\nimport { createToolExecutionController } from '@utils/toolExecutionController'\nimport { withTimeout, ToolTimeoutError } from '@utils/toolTimeout'\nimport { setStreamingState, resetStreamingState } from '@components/Spinner'\nimport { BashTool } from '@tools/BashTool/BashTool'\n\n/**\n * Normalize tool result content for API submission.\n * Ensures content is always a string regardless of input type.\n */\nfunction normalizeToolResultContent(content: unknown): string {\n if (content === null || content === undefined) {\n return '(no content)'\n }\n if (typeof content === 'string') {\n return content || '(no content)'\n }\n if (Array.isArray(content)) {\n // Handle array of content blocks (e.g., TextBlock[])\n return content\n .map(item => {\n if (typeof item === 'string') return item\n if (item && typeof item === 'object' && 'text' in item)\n return String(item.text)\n return JSON.stringify(item)\n })\n .join('\\n')\n }\n if (typeof content === 'object') {\n // Handle objects - try to extract text or stringify\n if ('text' in content) return String((content as { text: unknown }).text)\n return JSON.stringify(content)\n }\n return String(content)\n}\nimport { getCwd } from './utils/state'\nimport { checkAutoCompact } from './utils/autoCompactCore'\nimport { getHookManager } from '@utils/hookManager'\n\n// Extended ToolUseContext for query functions\ninterface ExtendedToolUseContext extends ToolUseContext {\n abortController: AbortController\n options: {\n commands: any[]\n forkNumber: number\n messageLogName: string\n tools: Tool[]\n verbose: boolean\n safeMode: boolean\n maxThinkingTokens: number\n isKodingRequest?: boolean\n model?: string | import('./utils/config').ModelPointerType\n }\n readFileTimestamps: { [filename: string]: number }\n setToolJSX: (jsx: any) => void\n askUser?: AskUserFn\n requestId?: string\n}\n\nexport type Response = { costUSD: number; response: string }\nexport type UserMessage = {\n message: MessageParam\n type: 'user'\n uuid: UUID\n toolUseResult?: FullToolUseResult\n options?: {\n isKodingRequest?: boolean\n kodingContext?: string\n isCustomCommand?: boolean\n commandName?: string\n commandArgs?: string\n }\n}\n\nexport type AssistantMessage = {\n costUSD: number\n durationMs: number\n message: APIAssistantMessage\n type: 'assistant'\n uuid: UUID\n isApiErrorMessage?: boolean\n responseId?: string // For GPT-5 Responses API state management\n /**\n * Original message UUID before normalization.\n * When a message with multiple content blocks is split into individual messages,\n * each resulting message will have a unique `uuid` but share the same `originalUuid`.\n * This is used to identify parallel tool calls from the same assistant message.\n */\n originalUuid?: UUID\n /**\n * Metadata for thinking blocks if present.\n * Used to display duration and character count in verbose mode.\n */\n thinkingMetadata?: ThinkingMetadata\n}\n\nexport type BinaryFeedbackResult =\n | { message: AssistantMessage | null; shouldSkipPermissionCheck: false }\n | { message: AssistantMessage; shouldSkipPermissionCheck: true }\n\n/**\n * Streaming output content for tools like BashTool\n */\nexport type StreamingProgressContent = {\n type: 'streaming'\n toolName: string\n stdout: string\n stderr: string\n isStreaming: boolean\n}\n\nexport type ProgressMessage = {\n content: AssistantMessage | StreamingProgressContent\n normalizedMessages: NormalizedMessage[]\n siblingToolUseIDs: Set<string>\n tools: Tool[]\n toolUseID: string\n type: 'progress'\n uuid: UUID\n}\n\n// Each array item is either a single message or a message-and-response pair\nexport type Message = UserMessage | AssistantMessage | ProgressMessage\n\nconst MAX_TOOL_USE_CONCURRENCY = 10\n\n/**\n * Maximum recursion depth for query calls.\n * Prevents stack overflow from infinite tool use loops.\n */\nconst MAX_QUERY_DEPTH = 50\n\n/**\n * Error thrown when query depth limit is exceeded\n */\nexport class QueryDepthExceededError extends Error {\n constructor(depth: number) {\n super(\n `Query depth limit exceeded (${depth}/${MAX_QUERY_DEPTH}). ` +\n `This usually indicates an infinite loop in tool usage.`,\n )\n this.name = 'QueryDepthExceededError'\n }\n}\n\n// Returns a message if we got one, or `null` if the user cancelled\nasync function queryWithBinaryFeedback(\n toolUseContext: ExtendedToolUseContext,\n getAssistantResponse: () => Promise<AssistantMessage>,\n getBinaryFeedbackResponse?: (\n m1: AssistantMessage,\n m2: AssistantMessage,\n ) => Promise<BinaryFeedbackResult>,\n): Promise<BinaryFeedbackResult> {\n if (\n process.env.USER_TYPE !== 'ant' ||\n !getBinaryFeedbackResponse ||\n !(await shouldUseBinaryFeedback())\n ) {\n const assistantMessage = await getAssistantResponse()\n if (toolUseContext.abortController.signal.aborted) {\n return { message: null, shouldSkipPermissionCheck: false }\n }\n return { message: assistantMessage, shouldSkipPermissionCheck: false }\n }\n const [m1, m2] = await Promise.all([\n getAssistantResponse(),\n getAssistantResponse(),\n ])\n if (toolUseContext.abortController.signal.aborted) {\n return { message: null, shouldSkipPermissionCheck: false }\n }\n if (m2.isApiErrorMessage) {\n // If m2 is an error, we might as well return m1, even if it's also an error --\n // the UI will display it as an error as it would in the non-feedback path.\n return { message: m1, shouldSkipPermissionCheck: false }\n }\n if (m1.isApiErrorMessage) {\n return { message: m2, shouldSkipPermissionCheck: false }\n }\n if (!messagePairValidForBinaryFeedback(m1, m2)) {\n return { message: m1, shouldSkipPermissionCheck: false }\n }\n return await getBinaryFeedbackResponse(m1, m2)\n}\n\n/**\n * The rules of thinking are lengthy and fortuitous. They require plenty of thinking\n * of most long duration and deep meditation for a wizard to wrap one's noggin around.\n *\n * The rules follow:\n * 1. A message that contains a thinking or redacted_thinking block must be part of a query whose max_thinking_length > 0\n * 2. A thinking block may not be the last message in a block\n * 3. Thinking blocks must be preserved for the duration of an assistant trajectory (a single turn, or if that turn includes a tool_use block then also its subsequent tool_result and the following assistant message)\n *\n * Heed these rules well, young wizard. For they are the rules of thinking, and\n * the rules of thinking are the rules of the universe. If ye does not heed these\n * rules, ye will be punished with an entire day of debugging and hair pulling.\n */\nexport async function* query(\n messages: Message[],\n systemPrompt: string[],\n context: { [k: string]: string },\n canUseTool: CanUseToolFn,\n toolUseContext: ExtendedToolUseContext,\n getBinaryFeedbackResponse?: (\n m1: AssistantMessage,\n m2: AssistantMessage,\n ) => Promise<BinaryFeedbackResult>,\n _depth: number = 0, // Internal: tracks recursion depth\n): AsyncGenerator<Message, void> {\n // Check recursion depth to prevent stack overflow\n if (_depth > MAX_QUERY_DEPTH) {\n const error = new QueryDepthExceededError(_depth)\n logError(error)\n yield createAssistantAPIErrorMessage(error.message)\n return\n }\n const currentRequest = getCurrentRequest()\n\n markPhase('QUERY_INIT')\n\n // Auto-compact check\n // \u2705 P2 FIX: Show compaction status in UI\n setStreamingState({ phase: 'compacting' })\n\n const { messages: processedMessages, wasCompacted } = await checkAutoCompact(\n messages,\n toolUseContext,\n )\n if (wasCompacted) {\n messages = processedMessages\n debug.flow('CONTEXT_COMPACTED', {\n originalCount: messages.length,\n compactedCount: processedMessages.length,\n })\n }\n\n markPhase('SYSTEM_PROMPT_BUILD')\n\n const { systemPrompt: fullSystemPrompt, reminders } =\n formatSystemPromptWithContext(systemPrompt, context, toolUseContext.agentId)\n\n // Emit session startup event\n emitReminderEvent('session:startup', {\n agentId: toolUseContext.agentId,\n messages: messages.length,\n timestamp: Date.now(),\n })\n\n // Inject reminders into the latest user message\n if (reminders && messages.length > 0) {\n // Find the last user message\n for (let i = messages.length - 1; i >= 0; i--) {\n const msg = messages[i]\n if (msg?.type === 'user') {\n const lastUserMessage = msg as UserMessage\n messages[i] = {\n ...lastUserMessage,\n message: {\n ...lastUserMessage.message,\n content:\n typeof lastUserMessage.message.content === 'string'\n ? reminders + lastUserMessage.message.content\n : [\n ...(Array.isArray(lastUserMessage.message.content)\n ? lastUserMessage.message.content\n : []),\n { type: 'text', text: reminders },\n ],\n },\n }\n break\n }\n }\n }\n\n markPhase('LLM_PREPARATION')\n\n // Update streaming state for UI feedback\n setStreamingState({ phase: 'waiting' })\n\n function getAssistantResponse() {\n return queryLLM(\n normalizeMessagesForAPI(messages),\n fullSystemPrompt,\n toolUseContext.options.maxThinkingTokens,\n toolUseContext.options.tools,\n toolUseContext.abortController.signal,\n {\n safeMode: toolUseContext.options.safeMode ?? false,\n model: toolUseContext.options.model || 'main',\n prependCLISysprompt: true,\n toolUseContext: toolUseContext,\n },\n )\n }\n\n const result = await queryWithBinaryFeedback(\n toolUseContext,\n getAssistantResponse,\n getBinaryFeedbackResponse,\n )\n\n // If request was cancelled, return immediately with interrupt message\n if (toolUseContext.abortController.signal.aborted) {\n yield createAssistantMessage(INTERRUPT_MESSAGE)\n return\n }\n\n if (result.message === null) {\n yield createAssistantMessage(INTERRUPT_MESSAGE)\n return\n }\n\n const assistantMessage = result.message\n const shouldSkipPermissionCheck = result.shouldSkipPermissionCheck\n\n // Update streaming state - we got a response\n setStreamingState({ phase: 'generating' })\n\n yield assistantMessage\n\n // @see https://docs.anthropic.com/en/docs/build-with-claude/tool-use\n // Note: stop_reason === 'tool_use' is unreliable -- it's not always set correctly\n const toolUseMessages = assistantMessage.message.content.filter(\n _ => _.type === 'tool_use',\n )\n\n // If there's no more tool use, we're done\n if (!toolUseMessages.length) {\n resetStreamingState()\n return\n }\n\n const toolResults: UserMessage[] = []\n\n // Smart concurrency: separate tools into serial and concurrent groups\n // This allows mixing TodoWrite (serial) with multiple Task calls (concurrent)\n const serialTools: ToolUseBlock[] = []\n const concurrentTools: ToolUseBlock[] = []\n\n for (const msg of toolUseMessages) {\n const tool = toolUseContext.options.tools.find(t => t.name === msg.name)\n const isSafe = tool?.isConcurrencySafe?.() ?? tool?.isReadOnly?.() ?? false\n if (isSafe) {\n concurrentTools.push(msg)\n } else {\n serialTools.push(msg)\n }\n }\n\n // Debug: Log concurrency decision\n debug.flow('TOOL_CONCURRENCY_CHECK', {\n toolCount: toolUseMessages.length,\n serialTools: serialTools.map(msg => msg.name),\n concurrentTools: concurrentTools.map(msg => msg.name),\n concurrencyDetails: toolUseMessages.map(msg => {\n const tool = toolUseContext.options.tools.find(t => t.name === msg.name)\n return {\n name: msg.name,\n isConcurrencySafe: tool?.isConcurrencySafe?.() ?? 'undefined',\n isReadOnly: tool?.isReadOnly?.() ?? 'undefined',\n }\n }),\n })\n\n // Step 1: Execute serial tools first (e.g., TodoWrite)\n if (serialTools.length > 0) {\n debug.flow('SERIAL_TOOLS_START', {\n count: serialTools.length,\n tools: serialTools.map(t => t.name),\n })\n for await (const message of runToolsSerially(\n serialTools,\n assistantMessage,\n canUseTool,\n toolUseContext,\n shouldSkipPermissionCheck,\n )) {\n yield message\n if (message.type === 'user') {\n toolResults.push(message)\n }\n }\n debug.flow('SERIAL_TOOLS_COMPLETE', {\n count: serialTools.length,\n })\n }\n\n // Step 2: Execute concurrent tools in parallel (e.g., multiple Task calls)\n if (concurrentTools.length > 0) {\n debug.flow('CONCURRENT_TOOLS_START', {\n count: concurrentTools.length,\n tools: concurrentTools.map(t => t.name),\n maxConcurrency: MAX_TOOL_USE_CONCURRENCY,\n })\n\n // \u2705 P2 FIX: Update UI to show concurrent task count\n setStreamingState({\n phase: 'concurrent',\n concurrentCount: concurrentTools.length,\n })\n\n const concurrentStartTime = Date.now()\n for await (const message of runToolsConcurrently(\n concurrentTools,\n assistantMessage,\n canUseTool,\n toolUseContext,\n shouldSkipPermissionCheck,\n )) {\n yield message\n if (message.type === 'user') {\n toolResults.push(message)\n }\n }\n debug.flow('CONCURRENT_TOOLS_COMPLETE', {\n count: concurrentTools.length,\n durationMs: Date.now() - concurrentStartTime,\n })\n }\n\n if (toolUseContext.abortController.signal.aborted) {\n yield createAssistantMessage(INTERRUPT_MESSAGE_FOR_TOOL_USE)\n return\n }\n\n // Sort toolResults to match the order of toolUseMessages\n const orderedToolResults = toolResults.sort((a, b) => {\n const aIndex = toolUseMessages.findIndex(\n tu => tu.id === (a.message.content[0] as ToolUseBlock).id,\n )\n const bIndex = toolUseMessages.findIndex(\n tu => tu.id === (b.message.content[0] as ToolUseBlock).id,\n )\n return aIndex - bIndex\n })\n\n // Process contextModifier and newMessages from tool results\n // These are used by SlashCommandTool to modify context and inject messages\n let modifiedContext = toolUseContext\n const additionalMessages: Message[] = []\n\n for (const result of orderedToolResults) {\n if (result.toolUseResult?.contextModifier) {\n // Apply context modifier (e.g., allowed tools, model switching)\n modifiedContext =\n result.toolUseResult.contextModifier.modifyContext(modifiedContext)\n debug.flow('CONTEXT_MODIFIER_APPLIED', {\n toolResultId: (result.message.content[0] as ToolUseBlock)?.id,\n })\n }\n\n if (\n result.toolUseResult?.newMessages &&\n Array.isArray(result.toolUseResult.newMessages)\n ) {\n // Add new messages from tool execution (e.g., skill-generated prompts)\n for (const msg of result.toolUseResult.newMessages) {\n if (msg && typeof msg === 'object' && 'type' in msg) {\n additionalMessages.push(msg as Message)\n }\n }\n debug.flow('NEW_MESSAGES_ADDED', {\n count: result.toolUseResult.newMessages.length,\n })\n }\n }\n\n // Recursive query with depth tracking\n\n try {\n yield* await query(\n [\n ...messages,\n assistantMessage,\n ...orderedToolResults,\n ...additionalMessages,\n ],\n systemPrompt,\n context,\n canUseTool,\n modifiedContext, // Use modified context if contextModifier was applied\n getBinaryFeedbackResponse,\n _depth + 1, // Increment depth for recursion tracking\n )\n } catch (error) {\n // \uD83D\uDD27 CRITICAL FIX: Yield error message instead of just re-throwing\n // This ensures the UI receives proper feedback when recursive query fails\n const errorMessage = error instanceof Error ? error.message : String(error)\n logError(`Recursive query error: ${errorMessage}`)\n\n // Yield an error message so the UI can display it\n yield createAssistantAPIErrorMessage(`Query failed: ${errorMessage}`)\n\n // Re-throw to propagate to caller for additional handling if needed\n throw error\n }\n}\n\nasync function* runToolsConcurrently(\n toolUseMessages: ToolUseBlock[],\n assistantMessage: AssistantMessage,\n canUseTool: CanUseToolFn,\n toolUseContext: ExtendedToolUseContext,\n shouldSkipPermissionCheck?: boolean,\n): AsyncGenerator<Message, void> {\n yield* all(\n toolUseMessages.map(toolUse =>\n runToolUse(\n toolUse,\n new Set(toolUseMessages.map(_ => _.id)),\n assistantMessage,\n canUseTool,\n toolUseContext,\n shouldSkipPermissionCheck,\n ),\n ),\n MAX_TOOL_USE_CONCURRENCY,\n toolUseContext.abortController.signal, // Pass abort signal for proper cancellation\n )\n}\n\nasync function* runToolsSerially(\n toolUseMessages: ToolUseBlock[],\n assistantMessage: AssistantMessage,\n canUseTool: CanUseToolFn,\n toolUseContext: ExtendedToolUseContext,\n shouldSkipPermissionCheck?: boolean,\n): AsyncGenerator<Message, void> {\n for (const toolUse of toolUseMessages) {\n yield* runToolUse(\n toolUse,\n new Set(toolUseMessages.map(_ => _.id)),\n assistantMessage,\n canUseTool,\n toolUseContext,\n shouldSkipPermissionCheck,\n )\n }\n}\n\nexport async function* runToolUse(\n toolUse: ToolUseBlock,\n siblingToolUseIDs: Set<string>,\n assistantMessage: AssistantMessage,\n canUseTool: CanUseToolFn,\n toolUseContext: ExtendedToolUseContext,\n shouldSkipPermissionCheck?: boolean,\n): AsyncGenerator<Message, void> {\n const currentRequest = getCurrentRequest()\n\n // \uD83D\uDD0D Debug: \u5DE5\u5177\u8C03\u7528\u5F00\u59CB\n debug.flow('TOOL_USE_START', {\n toolName: toolUse.name,\n toolUseID: toolUse.id,\n inputSize: JSON.stringify(toolUse.input).length,\n siblingToolCount: siblingToolUseIDs.size,\n shouldSkipPermissionCheck: !!shouldSkipPermissionCheck,\n requestId: currentRequest?.id,\n })\n\n logUserFriendly(\n 'TOOL_EXECUTION',\n {\n toolName: toolUse.name,\n action: 'Starting',\n target: toolUse.input ? Object.keys(toolUse.input).join(', ') : '',\n },\n currentRequest?.id,\n )\n\n const toolName = toolUse.name\n const tool = toolUseContext.options.tools.find(t => t.name === toolName)\n\n // Update streaming state to show tool execution\n setStreamingState({ phase: 'tool_use', toolName })\n\n // Check if the tool exists\n if (!tool) {\n debug.error('TOOL_NOT_FOUND', {\n requestedTool: toolName,\n availableTools: toolUseContext.options.tools.map(t => t.name),\n toolUseID: toolUse.id,\n requestId: currentRequest?.id,\n })\n\n yield createUserMessage([\n {\n type: 'tool_result',\n content: `Error: No such tool available: ${toolName}`,\n is_error: true,\n tool_use_id: toolUse.id,\n },\n ])\n return\n }\n\n const toolInput = toolUse.input as { [key: string]: string }\n\n debug.flow('TOOL_VALIDATION_START', {\n toolName: tool.name,\n toolUseID: toolUse.id,\n inputKeys: Object.keys(toolInput),\n requestId: currentRequest?.id,\n })\n\n try {\n // \uD83D\uDD27 Check for cancellation before starting tool execution\n if (toolUseContext.abortController.signal.aborted) {\n debug.flow('TOOL_USE_CANCELLED_BEFORE_START', {\n toolName: tool.name,\n toolUseID: toolUse.id,\n abortReason: 'AbortController signal',\n requestId: currentRequest?.id,\n })\n\n const message = createUserMessage([\n createToolResultStopMessage(toolUse.id),\n ])\n yield message\n return\n }\n\n // Track if any progress messages were yielded\n let hasProgressMessages = false\n\n for await (const message of checkPermissionsAndCallTool(\n tool,\n toolUse.id,\n siblingToolUseIDs,\n toolInput,\n toolUseContext,\n canUseTool,\n assistantMessage,\n shouldSkipPermissionCheck,\n )) {\n // \uD83D\uDD27 Check for cancellation during tool execution\n if (toolUseContext.abortController.signal.aborted) {\n debug.flow('TOOL_USE_CANCELLED_DURING_EXECUTION', {\n toolName: tool.name,\n toolUseID: toolUse.id,\n hasProgressMessages,\n abortReason: 'AbortController signal during execution',\n requestId: currentRequest?.id,\n })\n\n // If we yielded progress messages but got cancelled, yield a cancellation result\n if (hasProgressMessages && message.type === 'progress') {\n yield message // yield the last progress message first\n }\n\n // Always yield a tool result message for cancellation to clear UI state\n const cancelMessage = createUserMessage([\n createToolResultStopMessage(toolUse.id),\n ])\n yield cancelMessage\n return\n }\n\n if (message.type === 'progress') {\n hasProgressMessages = true\n }\n\n yield message\n }\n } catch (e) {\n logError(e)\n\n // \uD83D\uDD27 Even on error, ensure we yield a tool result to clear UI state\n const errorMessage = createUserMessage([\n {\n type: 'tool_result',\n content: `Tool execution failed: ${e instanceof Error ? e.message : String(e)}`,\n is_error: true,\n tool_use_id: toolUse.id,\n },\n ])\n yield errorMessage\n }\n}\n\n// TODO: Generalize this to all tools\nexport function normalizeToolInput(\n tool: Tool,\n input: { [key: string]: boolean | string | number },\n): { [key: string]: boolean | string | number } {\n switch (tool) {\n case BashTool: {\n const { command, timeout } = BashTool.inputSchema.parse(input) // already validated upstream, won't throw\n return {\n command: command.replace(`cd ${getCwd()} && `, ''),\n ...(timeout ? { timeout } : {}),\n }\n }\n default:\n return input\n }\n}\n\nasync function* checkPermissionsAndCallTool(\n tool: Tool,\n toolUseID: string,\n siblingToolUseIDs: Set<string>,\n input: { [key: string]: boolean | string | number },\n context: ToolUseContext,\n canUseTool: CanUseToolFn,\n assistantMessage: AssistantMessage,\n shouldSkipPermissionCheck?: boolean,\n): AsyncGenerator<UserMessage | ProgressMessage, void> {\n // Validate input types with zod\n // (surprisingly, the model is not great at generating valid input)\n const isValidInput = tool.inputSchema.safeParse(input)\n if (!isValidInput.success) {\n // Create a more helpful error message for common cases\n let errorMessage = `InputValidationError: ${isValidInput.error.message}`\n\n // Special handling for the \"View\" tool (FileReadTool) being called with empty parameters\n if (tool.name === 'View' && Object.keys(input).length === 0) {\n errorMessage = `Error: The View tool requires a 'file_path' parameter to specify which file to read. Please provide the absolute path to the file you want to view. For example: {\"file_path\": \"/path/to/file.txt\"}`\n }\n\n yield createUserMessage([\n {\n type: 'tool_result',\n content: errorMessage,\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n return\n }\n\n const normalizedInput = normalizeToolInput(tool, input)\n\n // Validate input values. Each tool has its own validation logic\n const isValidCall = await tool.validateInput?.(\n normalizedInput as never,\n context,\n )\n if (isValidCall?.result === false) {\n yield createUserMessage([\n {\n type: 'tool_result',\n content: isValidCall!.message,\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n return\n }\n\n // Check whether we have permission to use the tool,\n // and ask the user for permission if we don't\n const permissionResult = shouldSkipPermissionCheck\n ? ({ result: true } as const)\n : await canUseTool(tool, normalizedInput, context, assistantMessage)\n if (permissionResult.result === false) {\n yield createUserMessage([\n {\n type: 'tool_result',\n content: permissionResult.message,\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n return\n }\n\n // \u2705 P1 FIX: Show permission waiting state when permission check is pending\n // Note: This is handled by the PermissionRequest dialog in the UI\n // But we update streaming state to indicate the phase\n if (!shouldSkipPermissionCheck && !permissionResult.result) {\n setStreamingState({ phase: 'permission' })\n }\n\n // PreToolUse hooks\n const hookManager = getHookManager()\n if (hookManager) {\n try {\n const decision = await hookManager.executePreToolUse(\n tool.name,\n normalizedInput as Record<string, unknown>,\n )\n\n if (!decision.shouldContinue) {\n if (decision.shouldAskUser) {\n // Ask user for approval via permission system\n const approved = await canUseTool(\n tool,\n normalizedInput,\n context,\n assistantMessage,\n )\n if (approved.result === false) {\n yield createUserMessage([\n {\n type: 'tool_result',\n content:\n decision.reason || 'Hook requested approval but user denied',\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n return\n }\n } else {\n // Hook blocked the tool\n yield createUserMessage([\n {\n type: 'tool_result',\n content: decision.reason || 'Tool execution blocked by hook',\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n return\n }\n }\n } catch (error) {\n debug.error('PreToolUse hook failed', { error })\n // Continue on error (fail-safe)\n }\n }\n\n // Call the tool with timeout protection\n let toolOutput: FullToolUseResult | null = null\n try {\n // Add toolUseId to context for tracking concurrent executions\n const contextWithToolUseId = {\n ...context,\n toolUseId: toolUseID,\n }\n const rawGenerator = tool.call(\n normalizedInput as never,\n contextWithToolUseId,\n )\n // Wrap tool execution with timeout to prevent infinite hangs\n const generator = withTimeout(rawGenerator, tool.name)\n\n for await (const result of generator) {\n switch (result.type) {\n case 'result':\n // Normalize content to ensure API compatibility\n const normalizedContent = normalizeToolResultContent(\n result.resultForAssistant ?? result.data,\n )\n toolOutput = {\n data: result.data,\n resultForAssistant: normalizedContent,\n // Preserve contextModifier and newMessages for caller to process\n newMessages: result.newMessages,\n contextModifier: result.contextModifier,\n }\n\n yield createUserMessage(\n [\n {\n type: 'tool_result',\n content: normalizedContent,\n tool_use_id: toolUseID,\n },\n ],\n toolOutput,\n )\n\n // PostToolUse hooks (fire-and-forget)\n if (hookManager && toolOutput) {\n hookManager\n .executePostToolUse(\n tool.name,\n normalizedInput as Record<string, unknown>,\n toolOutput as any,\n )\n .catch(err => {\n debug.error('PostToolUse hook failed', { error: err })\n })\n }\n\n return\n case 'progress':\n yield createProgressMessage(\n toolUseID,\n siblingToolUseIDs,\n result.content,\n result.normalizedMessages || [],\n result.tools || [],\n )\n break\n }\n }\n } catch (error) {\n // Special handling for timeout errors\n if (error instanceof ToolTimeoutError) {\n debug.error('TOOL_TIMEOUT', {\n toolName: tool.name,\n toolUseID,\n timeoutMs: error.timeoutMs,\n })\n\n yield createUserMessage([\n {\n type: 'tool_result',\n content: `Error: ${error.message}. The tool execution was cancelled. Consider breaking this operation into smaller steps or checking if there's an infinite loop.`,\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n return\n }\n\n const content = formatError(error)\n logError(error)\n\n yield createUserMessage([\n {\n type: 'tool_result',\n content,\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n }\n}\n\nfunction formatError(error: unknown): string {\n if (!(error instanceof Error)) {\n return String(error)\n }\n const parts = [error.message]\n if ('stderr' in error && typeof error.stderr === 'string') {\n parts.push(error.stderr)\n }\n if ('stdout' in error && typeof error.stdout === 'string') {\n parts.push(error.stdout)\n }\n const fullMessage = parts.filter(Boolean).join('\\n')\n if (fullMessage.length <= 10000) {\n return fullMessage\n }\n const halfLength = 5000\n const start = fullMessage.slice(0, halfLength)\n const end = fullMessage.slice(-halfLength)\n return `${start}\\n\\n... [${fullMessage.length - 10000} characters truncated] ...\\n\\n${end}`\n}\n"],
|
|
5
|
-
"mappings": "AAQA;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAS,yBAAyB;AAClC,SAAS,WAAW;AACpB,SAAS,gBAAgB;AACzB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,EAEA;AAAA,OACK;AAEP,SAAS,aAAa,wBAAwB;AAC9C,SAAS,mBAAmB,2BAA2B;AACvD,SAAS,gBA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AK,MAAM,MAAM;AAAA,EACzB;AACA,MAAI,YAAY,SAAS,OAAO,MAAM,WAAW,UAAU;AACzD,UAAM,KAAK,MAAM,MAAM;AAAA,EACzB;AACA,QAAM,cAAc,MAAM,OAAO,OAAO,EAAE,KAAK,IAAI;AACnD,MAAI,YAAY,UAAU,KAAO;AAC/B,WAAO;AAAA,EACT;AACA,QAAM,aAAa;AACnB,QAAM,QAAQ,YAAY,MAAM,GAAG,UAAU;AAC7C,QAAM,MAAM,YAAY,MAAM,CAAC,UAAU;AACzC,SAAO,GAAG,KAAK;AAAA;AAAA,OAAY,YAAY,SAAS,GAAK;AAAA;AAAA,EAAiC,GAAG;AAC3F;",
|
|
6
|
-
"names": ["result"]
|
|
4
|
+
"sourcesContent": ["import {\n Message as APIAssistantMessage,\n MessageParam,\n ToolUseBlock,\n} from '@anthropic-ai/sdk/resources/index.mjs'\nimport type { UUID } from './types/common'\nimport type { ThinkingMetadata } from './types/thinking'\nimport type { Tool, ToolUseContext, AskUserFn } from './Tool'\nimport {\n messagePairValidForBinaryFeedback,\n shouldUseBinaryFeedback,\n} from '@components/binary-feedback/utils'\nimport { CanUseToolFn } from './hooks/useCanUseTool'\nimport {\n formatSystemPromptWithContext,\n queryLLM,\n queryModel,\n} from '@services/claude'\nimport { emitReminderEvent } from '@services/systemReminder'\nimport {\n TOOL_INPUT_EXAMPLES,\n TOOL_DESCRIPTIONS,\n PARAMETER_DESCRIPTIONS,\n} from '@constants/toolInputExamples'\nimport { all } from '@utils/generators'\nimport { logError } from '@utils/log'\nimport {\n debug,\n markPhase,\n getCurrentRequest,\n logUserFriendly,\n} from './utils/debugLogger'\nimport { getModelManager } from '@utils/model'\nimport {\n createAssistantMessage,\n createAssistantAPIErrorMessage,\n createProgressMessage,\n createToolResultStopMessage,\n createUserMessage,\n FullToolUseResult,\n INTERRUPT_MESSAGE,\n INTERRUPT_MESSAGE_FOR_TOOL_USE,\n NormalizedMessage,\n normalizeMessagesForAPI,\n} from '@utils/messages'\nimport { createToolExecutionController } from '@utils/toolExecutionController'\nimport { withTimeout, ToolTimeoutError } from '@utils/toolTimeout'\nimport { setStreamingState, resetStreamingState } from '@components/Spinner'\nimport { BashTool } from '@tools/BashTool/BashTool'\n\n/**\n * Normalize tool result content for API submission.\n * Ensures content is always a string regardless of input type.\n */\nfunction normalizeToolResultContent(content: unknown): string {\n if (content === null || content === undefined) {\n return '(no content)'\n }\n if (typeof content === 'string') {\n return content || '(no content)'\n }\n if (Array.isArray(content)) {\n // Handle array of content blocks (e.g., TextBlock[])\n return content\n .map(item => {\n if (typeof item === 'string') return item\n if (item && typeof item === 'object' && 'text' in item)\n return String(item.text)\n return JSON.stringify(item)\n })\n .join('\\n')\n }\n if (typeof content === 'object') {\n // Handle objects - try to extract text or stringify\n if ('text' in content) return String((content as { text: unknown }).text)\n return JSON.stringify(content)\n }\n return String(content)\n}\nimport { getCwd } from './utils/state'\nimport { checkAutoCompact } from './utils/autoCompactCore'\nimport { getHookManager } from '@utils/hookManager'\n\n// Extended ToolUseContext for query functions\ninterface ExtendedToolUseContext extends ToolUseContext {\n abortController: AbortController\n options: {\n commands: any[]\n forkNumber: number\n messageLogName: string\n tools: Tool[]\n verbose: boolean\n /** @deprecated Use safetyMode instead */\n safeMode?: boolean\n /** Safety mode: 'yolo' | 'smart' | 'strict' */\n safetyMode?: import('./utils/config').SafetyMode\n maxThinkingTokens: number\n isKodingRequest?: boolean\n model?: string | import('./utils/config').ModelPointerType\n }\n readFileTimestamps: { [filename: string]: number }\n setToolJSX: (jsx: any) => void\n askUser?: AskUserFn\n requestId?: string\n}\n\nexport type Response = { costUSD: number; response: string }\nexport type UserMessage = {\n message: MessageParam\n type: 'user'\n uuid: UUID\n toolUseResult?: FullToolUseResult\n options?: {\n isKodingRequest?: boolean\n kodingContext?: string\n isCustomCommand?: boolean\n commandName?: string\n commandArgs?: string\n }\n}\n\nexport type AssistantMessage = {\n costUSD: number\n durationMs: number\n message: APIAssistantMessage\n type: 'assistant'\n uuid: UUID\n isApiErrorMessage?: boolean\n responseId?: string // For GPT-5 Responses API state management\n /**\n * Original message UUID before normalization.\n * When a message with multiple content blocks is split into individual messages,\n * each resulting message will have a unique `uuid` but share the same `originalUuid`.\n * This is used to identify parallel tool calls from the same assistant message.\n */\n originalUuid?: UUID\n /**\n * Metadata for thinking blocks if present.\n * Used to display duration and character count in verbose mode.\n */\n thinkingMetadata?: ThinkingMetadata\n}\n\nexport type BinaryFeedbackResult =\n | { message: AssistantMessage | null; shouldSkipPermissionCheck: false }\n | { message: AssistantMessage; shouldSkipPermissionCheck: true }\n\n/**\n * Streaming output content for tools like BashTool\n */\nexport type StreamingProgressContent = {\n type: 'streaming'\n toolName: string\n stdout: string\n stderr: string\n isStreaming: boolean\n}\n\nexport type ProgressMessage = {\n content: AssistantMessage | StreamingProgressContent\n normalizedMessages: NormalizedMessage[]\n siblingToolUseIDs: Set<string>\n tools: Tool[]\n toolUseID: string\n type: 'progress'\n uuid: UUID\n}\n\n// Each array item is either a single message or a message-and-response pair\nexport type Message = UserMessage | AssistantMessage | ProgressMessage\n\nconst MAX_TOOL_USE_CONCURRENCY = 10\n\n/**\n * Maximum recursion depth for query calls.\n * Prevents stack overflow from infinite tool use loops.\n */\nconst MAX_QUERY_DEPTH = 50\n\n/**\n * Error thrown when query depth limit is exceeded\n */\nexport class QueryDepthExceededError extends Error {\n constructor(depth: number) {\n super(\n `Query depth limit exceeded (${depth}/${MAX_QUERY_DEPTH}). ` +\n `This usually indicates an infinite loop in tool usage.`,\n )\n this.name = 'QueryDepthExceededError'\n }\n}\n\n// Returns a message if we got one, or `null` if the user cancelled\nasync function queryWithBinaryFeedback(\n toolUseContext: ExtendedToolUseContext,\n getAssistantResponse: () => Promise<AssistantMessage>,\n getBinaryFeedbackResponse?: (\n m1: AssistantMessage,\n m2: AssistantMessage,\n ) => Promise<BinaryFeedbackResult>,\n): Promise<BinaryFeedbackResult> {\n if (\n process.env.USER_TYPE !== 'ant' ||\n !getBinaryFeedbackResponse ||\n !(await shouldUseBinaryFeedback())\n ) {\n const assistantMessage = await getAssistantResponse()\n if (toolUseContext.abortController.signal.aborted) {\n return { message: null, shouldSkipPermissionCheck: false }\n }\n return { message: assistantMessage, shouldSkipPermissionCheck: false }\n }\n const [m1, m2] = await Promise.all([\n getAssistantResponse(),\n getAssistantResponse(),\n ])\n if (toolUseContext.abortController.signal.aborted) {\n return { message: null, shouldSkipPermissionCheck: false }\n }\n if (m2.isApiErrorMessage) {\n // If m2 is an error, we might as well return m1, even if it's also an error --\n // the UI will display it as an error as it would in the non-feedback path.\n return { message: m1, shouldSkipPermissionCheck: false }\n }\n if (m1.isApiErrorMessage) {\n return { message: m2, shouldSkipPermissionCheck: false }\n }\n if (!messagePairValidForBinaryFeedback(m1, m2)) {\n return { message: m1, shouldSkipPermissionCheck: false }\n }\n return await getBinaryFeedbackResponse(m1, m2)\n}\n\n/**\n * The rules of thinking are lengthy and fortuitous. They require plenty of thinking\n * of most long duration and deep meditation for a wizard to wrap one's noggin around.\n *\n * The rules follow:\n * 1. A message that contains a thinking or redacted_thinking block must be part of a query whose max_thinking_length > 0\n * 2. A thinking block may not be the last message in a block\n * 3. Thinking blocks must be preserved for the duration of an assistant trajectory (a single turn, or if that turn includes a tool_use block then also its subsequent tool_result and the following assistant message)\n *\n * Heed these rules well, young wizard. For they are the rules of thinking, and\n * the rules of thinking are the rules of the universe. If ye does not heed these\n * rules, ye will be punished with an entire day of debugging and hair pulling.\n */\nexport async function* query(\n messages: Message[],\n systemPrompt: string[],\n context: { [k: string]: string },\n canUseTool: CanUseToolFn,\n toolUseContext: ExtendedToolUseContext,\n getBinaryFeedbackResponse?: (\n m1: AssistantMessage,\n m2: AssistantMessage,\n ) => Promise<BinaryFeedbackResult>,\n _depth: number = 0, // Internal: tracks recursion depth\n): AsyncGenerator<Message, void> {\n // Check recursion depth to prevent stack overflow\n if (_depth > MAX_QUERY_DEPTH) {\n const error = new QueryDepthExceededError(_depth)\n logError(error)\n yield createAssistantAPIErrorMessage(error.message)\n return\n }\n const currentRequest = getCurrentRequest()\n\n markPhase('QUERY_INIT')\n\n // Auto-compact check\n // \u2705 P2 FIX: Show compaction status in UI\n setStreamingState({ phase: 'compacting' })\n\n const { messages: processedMessages, wasCompacted } = await checkAutoCompact(\n messages,\n toolUseContext,\n )\n if (wasCompacted) {\n messages = processedMessages\n debug.flow('CONTEXT_COMPACTED', {\n originalCount: messages.length,\n compactedCount: processedMessages.length,\n })\n }\n\n markPhase('SYSTEM_PROMPT_BUILD')\n\n const { systemPrompt: fullSystemPrompt, reminders } =\n formatSystemPromptWithContext(systemPrompt, context, toolUseContext.agentId)\n\n // Emit session startup event\n emitReminderEvent('session:startup', {\n agentId: toolUseContext.agentId,\n messages: messages.length,\n timestamp: Date.now(),\n })\n\n // Inject reminders into the latest user message\n if (reminders && messages.length > 0) {\n // Find the last user message\n for (let i = messages.length - 1; i >= 0; i--) {\n const msg = messages[i]\n if (msg?.type === 'user') {\n const lastUserMessage = msg as UserMessage\n messages[i] = {\n ...lastUserMessage,\n message: {\n ...lastUserMessage.message,\n content:\n typeof lastUserMessage.message.content === 'string'\n ? reminders + lastUserMessage.message.content\n : [\n ...(Array.isArray(lastUserMessage.message.content)\n ? lastUserMessage.message.content\n : []),\n { type: 'text', text: reminders },\n ],\n },\n }\n break\n }\n }\n }\n\n markPhase('LLM_PREPARATION')\n\n // Update streaming state for UI feedback\n setStreamingState({ phase: 'waiting' })\n\n function getAssistantResponse() {\n return queryLLM(\n normalizeMessagesForAPI(messages),\n fullSystemPrompt,\n toolUseContext.options.maxThinkingTokens,\n toolUseContext.options.tools,\n toolUseContext.abortController.signal,\n {\n safeMode: toolUseContext.options.safeMode ?? false,\n model: toolUseContext.options.model || 'main',\n prependCLISysprompt: true,\n toolUseContext: toolUseContext,\n },\n )\n }\n\n const result = await queryWithBinaryFeedback(\n toolUseContext,\n getAssistantResponse,\n getBinaryFeedbackResponse,\n )\n\n // If request was cancelled, return immediately with interrupt message\n if (toolUseContext.abortController.signal.aborted) {\n yield createAssistantMessage(INTERRUPT_MESSAGE)\n return\n }\n\n if (result.message === null) {\n yield createAssistantMessage(INTERRUPT_MESSAGE)\n return\n }\n\n const assistantMessage = result.message\n const shouldSkipPermissionCheck = result.shouldSkipPermissionCheck\n\n // Update streaming state - we got a response\n setStreamingState({ phase: 'generating' })\n\n yield assistantMessage\n\n // @see https://docs.anthropic.com/en/docs/build-with-claude/tool-use\n // Note: stop_reason === 'tool_use' is unreliable -- it's not always set correctly\n const toolUseMessages = assistantMessage.message.content.filter(\n _ => _.type === 'tool_use',\n )\n\n // If there's no more tool use, we're done\n if (!toolUseMessages.length) {\n resetStreamingState()\n return\n }\n\n const toolResults: UserMessage[] = []\n\n // Smart concurrency: separate tools into serial and concurrent groups\n // This allows mixing TodoWrite (serial) with multiple Task calls (concurrent)\n const serialTools: ToolUseBlock[] = []\n const concurrentTools: ToolUseBlock[] = []\n\n for (const msg of toolUseMessages) {\n const tool = toolUseContext.options.tools.find(t => t.name === msg.name)\n const isSafe = tool?.isConcurrencySafe?.() ?? tool?.isReadOnly?.() ?? false\n if (isSafe) {\n concurrentTools.push(msg)\n } else {\n serialTools.push(msg)\n }\n }\n\n // Debug: Log concurrency decision\n debug.flow('TOOL_CONCURRENCY_CHECK', {\n toolCount: toolUseMessages.length,\n serialTools: serialTools.map(msg => msg.name),\n concurrentTools: concurrentTools.map(msg => msg.name),\n concurrencyDetails: toolUseMessages.map(msg => {\n const tool = toolUseContext.options.tools.find(t => t.name === msg.name)\n return {\n name: msg.name,\n isConcurrencySafe: tool?.isConcurrencySafe?.() ?? 'undefined',\n isReadOnly: tool?.isReadOnly?.() ?? 'undefined',\n }\n }),\n })\n\n // Step 1: Execute serial tools first (e.g., TodoWrite)\n if (serialTools.length > 0) {\n debug.flow('SERIAL_TOOLS_START', {\n count: serialTools.length,\n tools: serialTools.map(t => t.name),\n })\n for await (const message of runToolsSerially(\n serialTools,\n assistantMessage,\n canUseTool,\n toolUseContext,\n shouldSkipPermissionCheck,\n )) {\n yield message\n if (message.type === 'user') {\n toolResults.push(message)\n }\n }\n debug.flow('SERIAL_TOOLS_COMPLETE', {\n count: serialTools.length,\n })\n }\n\n // Step 2: Execute concurrent tools in parallel (e.g., multiple Task calls)\n if (concurrentTools.length > 0) {\n debug.flow('CONCURRENT_TOOLS_START', {\n count: concurrentTools.length,\n tools: concurrentTools.map(t => t.name),\n maxConcurrency: MAX_TOOL_USE_CONCURRENCY,\n })\n\n // \u2705 P2 FIX: Update UI to show concurrent task count\n setStreamingState({\n phase: 'concurrent',\n concurrentCount: concurrentTools.length,\n })\n\n const concurrentStartTime = Date.now()\n for await (const message of runToolsConcurrently(\n concurrentTools,\n assistantMessage,\n canUseTool,\n toolUseContext,\n shouldSkipPermissionCheck,\n )) {\n yield message\n if (message.type === 'user') {\n toolResults.push(message)\n }\n }\n debug.flow('CONCURRENT_TOOLS_COMPLETE', {\n count: concurrentTools.length,\n durationMs: Date.now() - concurrentStartTime,\n })\n }\n\n if (toolUseContext.abortController.signal.aborted) {\n yield createAssistantMessage(INTERRUPT_MESSAGE_FOR_TOOL_USE)\n return\n }\n\n // Sort toolResults to match the order of toolUseMessages\n const orderedToolResults = toolResults.sort((a, b) => {\n const aIndex = toolUseMessages.findIndex(\n tu => tu.id === (a.message.content[0] as ToolUseBlock).id,\n )\n const bIndex = toolUseMessages.findIndex(\n tu => tu.id === (b.message.content[0] as ToolUseBlock).id,\n )\n return aIndex - bIndex\n })\n\n // Process contextModifier and newMessages from tool results\n // These are used by SlashCommandTool to modify context and inject messages\n let modifiedContext = toolUseContext\n const additionalMessages: Message[] = []\n\n for (const result of orderedToolResults) {\n if (result.toolUseResult?.contextModifier) {\n // Apply context modifier (e.g., allowed tools, model switching)\n modifiedContext =\n result.toolUseResult.contextModifier.modifyContext(modifiedContext)\n debug.flow('CONTEXT_MODIFIER_APPLIED', {\n toolResultId: (result.message.content[0] as ToolUseBlock)?.id,\n })\n }\n\n if (\n result.toolUseResult?.newMessages &&\n Array.isArray(result.toolUseResult.newMessages)\n ) {\n // Add new messages from tool execution (e.g., skill-generated prompts)\n for (const msg of result.toolUseResult.newMessages) {\n if (msg && typeof msg === 'object' && 'type' in msg) {\n additionalMessages.push(msg as Message)\n }\n }\n debug.flow('NEW_MESSAGES_ADDED', {\n count: result.toolUseResult.newMessages.length,\n })\n }\n }\n\n // Recursive query with depth tracking\n\n try {\n yield* await query(\n [\n ...messages,\n assistantMessage,\n ...orderedToolResults,\n ...additionalMessages,\n ],\n systemPrompt,\n context,\n canUseTool,\n modifiedContext, // Use modified context if contextModifier was applied\n getBinaryFeedbackResponse,\n _depth + 1, // Increment depth for recursion tracking\n )\n } catch (error) {\n // \uD83D\uDD27 CRITICAL FIX: Yield error message instead of just re-throwing\n // This ensures the UI receives proper feedback when recursive query fails\n const errorMessage = error instanceof Error ? error.message : String(error)\n logError(`Recursive query error: ${errorMessage}`)\n\n // Yield an error message so the UI can display it\n yield createAssistantAPIErrorMessage(`Query failed: ${errorMessage}`)\n\n // Re-throw to propagate to caller for additional handling if needed\n throw error\n }\n}\n\nasync function* runToolsConcurrently(\n toolUseMessages: ToolUseBlock[],\n assistantMessage: AssistantMessage,\n canUseTool: CanUseToolFn,\n toolUseContext: ExtendedToolUseContext,\n shouldSkipPermissionCheck?: boolean,\n): AsyncGenerator<Message, void> {\n yield* all(\n toolUseMessages.map(toolUse =>\n runToolUse(\n toolUse,\n new Set(toolUseMessages.map(_ => _.id)),\n assistantMessage,\n canUseTool,\n toolUseContext,\n shouldSkipPermissionCheck,\n ),\n ),\n MAX_TOOL_USE_CONCURRENCY,\n toolUseContext.abortController.signal, // Pass abort signal for proper cancellation\n )\n}\n\nasync function* runToolsSerially(\n toolUseMessages: ToolUseBlock[],\n assistantMessage: AssistantMessage,\n canUseTool: CanUseToolFn,\n toolUseContext: ExtendedToolUseContext,\n shouldSkipPermissionCheck?: boolean,\n): AsyncGenerator<Message, void> {\n for (const toolUse of toolUseMessages) {\n yield* runToolUse(\n toolUse,\n new Set(toolUseMessages.map(_ => _.id)),\n assistantMessage,\n canUseTool,\n toolUseContext,\n shouldSkipPermissionCheck,\n )\n }\n}\n\nexport async function* runToolUse(\n toolUse: ToolUseBlock,\n siblingToolUseIDs: Set<string>,\n assistantMessage: AssistantMessage,\n canUseTool: CanUseToolFn,\n toolUseContext: ExtendedToolUseContext,\n shouldSkipPermissionCheck?: boolean,\n): AsyncGenerator<Message, void> {\n const currentRequest = getCurrentRequest()\n\n // \uD83D\uDD0D Debug: \u5DE5\u5177\u8C03\u7528\u5F00\u59CB\n debug.flow('TOOL_USE_START', {\n toolName: toolUse.name,\n toolUseID: toolUse.id,\n inputSize: JSON.stringify(toolUse.input).length,\n siblingToolCount: siblingToolUseIDs.size,\n shouldSkipPermissionCheck: !!shouldSkipPermissionCheck,\n requestId: currentRequest?.id,\n })\n\n logUserFriendly(\n 'TOOL_EXECUTION',\n {\n toolName: toolUse.name,\n action: 'Starting',\n target: toolUse.input ? Object.keys(toolUse.input).join(', ') : '',\n },\n currentRequest?.id,\n )\n\n const toolName = toolUse.name\n const tool = toolUseContext.options.tools.find(t => t.name === toolName)\n\n // Update streaming state to show tool execution\n setStreamingState({ phase: 'tool_use', toolName })\n\n // Check if the tool exists\n if (!tool) {\n debug.error('TOOL_NOT_FOUND', {\n requestedTool: toolName,\n availableTools: toolUseContext.options.tools.map(t => t.name),\n toolUseID: toolUse.id,\n requestId: currentRequest?.id,\n })\n\n yield createUserMessage([\n {\n type: 'tool_result',\n content: `Error: No such tool available: ${toolName}`,\n is_error: true,\n tool_use_id: toolUse.id,\n },\n ])\n return\n }\n\n const toolInput = toolUse.input as { [key: string]: string }\n\n debug.flow('TOOL_VALIDATION_START', {\n toolName: tool.name,\n toolUseID: toolUse.id,\n inputKeys: Object.keys(toolInput),\n requestId: currentRequest?.id,\n })\n\n try {\n // \uD83D\uDD27 Check for cancellation before starting tool execution\n if (toolUseContext.abortController.signal.aborted) {\n debug.flow('TOOL_USE_CANCELLED_BEFORE_START', {\n toolName: tool.name,\n toolUseID: toolUse.id,\n abortReason: 'AbortController signal',\n requestId: currentRequest?.id,\n })\n\n const message = createUserMessage([\n createToolResultStopMessage(toolUse.id),\n ])\n yield message\n return\n }\n\n // Track if any progress messages were yielded\n let hasProgressMessages = false\n\n for await (const message of checkPermissionsAndCallTool(\n tool,\n toolUse.id,\n siblingToolUseIDs,\n toolInput,\n toolUseContext,\n canUseTool,\n assistantMessage,\n shouldSkipPermissionCheck,\n )) {\n // \uD83D\uDD27 Check for cancellation during tool execution\n if (toolUseContext.abortController.signal.aborted) {\n debug.flow('TOOL_USE_CANCELLED_DURING_EXECUTION', {\n toolName: tool.name,\n toolUseID: toolUse.id,\n hasProgressMessages,\n abortReason: 'AbortController signal during execution',\n requestId: currentRequest?.id,\n })\n\n // If we yielded progress messages but got cancelled, yield a cancellation result\n if (hasProgressMessages && message.type === 'progress') {\n yield message // yield the last progress message first\n }\n\n // Always yield a tool result message for cancellation to clear UI state\n const cancelMessage = createUserMessage([\n createToolResultStopMessage(toolUse.id),\n ])\n yield cancelMessage\n return\n }\n\n if (message.type === 'progress') {\n hasProgressMessages = true\n }\n\n yield message\n }\n } catch (e) {\n logError(e)\n\n // \uD83D\uDD27 Even on error, ensure we yield a tool result to clear UI state\n const errorMessage = createUserMessage([\n {\n type: 'tool_result',\n content: `Tool execution failed: ${e instanceof Error ? e.message : String(e)}`,\n is_error: true,\n tool_use_id: toolUse.id,\n },\n ])\n yield errorMessage\n }\n}\n\n// TODO: Generalize this to all tools\nexport function normalizeToolInput(\n tool: Tool,\n input: { [key: string]: boolean | string | number },\n): { [key: string]: boolean | string | number } {\n switch (tool) {\n case BashTool: {\n const { command, timeout } = BashTool.inputSchema.parse(input) // already validated upstream, won't throw\n return {\n command: command.replace(`cd ${getCwd()} && `, ''),\n ...(timeout ? { timeout } : {}),\n }\n }\n default:\n return input\n }\n}\n\n/**\n * Build a user-friendly error message for tool input validation failures.\n * Provides clear guidance on expected format, examples, and how to fix the issue.\n */\nfunction buildToolInputErrorMessage(\n tool: Tool,\n input: { [key: string]: boolean | string | number },\n validationError: any,\n): string {\n // Check if this is a JSON parse error from upstream (claude.ts)\n const parseError = input as {\n __parse_error__?: boolean\n __error_message__?: string\n __raw_json_preview__?: string\n }\n\n if (parseError.__parse_error__) {\n // JSON parse error - model generated malformed JSON\n let message = [\n `Error: Failed to parse tool parameters for \"${tool.name}\".`,\n ``,\n `The model generated invalid JSON. This is typically a temporary issue.`,\n ``,\n `Expected format: ${TOOL_INPUT_EXAMPLES[tool.name] || '{...}'}`,\n ]\n\n if (parseError.__error_message__) {\n message.push(`Parse error: ${parseError.__error_message__}`)\n }\n\n if (parseError.__raw_json_preview__) {\n message.push(``, `Raw JSON preview:`, parseError.__raw_json_preview__)\n }\n\n message.push(\n ``,\n `Suggestions:`,\n `\u2022 Retry your request - the model may succeed on the next attempt`,\n `\u2022 Try simplifying your request with fewer parameters`,\n `\u2022 Check if special characters in values need escaping`,\n )\n\n return message.join('\\n')\n }\n\n // Validation error - parameters are syntactically correct but semantically wrong\n const inputKeys = Object.keys(input)\n const isEmptyInput = inputKeys.length === 0\n\n const example =\n TOOL_INPUT_EXAMPLES[tool.name] || '{...required parameters...}'\n const description = TOOL_DESCRIPTIONS[tool.name]\n const paramDescriptions = PARAMETER_DESCRIPTIONS[tool.name]\n\n let message = [\n `Error: Invalid parameters for \"${tool.name}\" tool.`,\n ``,\n `Expected format:`,\n `${example}`,\n ]\n\n if (description) {\n message.push(``, `Description: ${description}`)\n }\n\n if (isEmptyInput) {\n message.push(\n ``,\n `Issue: The tool was called without any parameters.`,\n `This usually indicates a JSON parsing error in the model's response.`,\n )\n } else {\n // Show which parameters were provided vs missing\n message.push(\n ``,\n `Issue: ${validationError.message}`,\n `Provided parameters: ${inputKeys.length > 0 ? inputKeys.join(', ') : '(none)'}`,\n )\n\n // Show parameter-specific help if available\n if (paramDescriptions) {\n message.push(``, `Parameter guide:`)\n for (const [param, desc] of Object.entries(paramDescriptions)) {\n message.push(`\u2022 ${param}: ${desc}`)\n }\n }\n }\n\n message.push(\n ``,\n `Suggestions:`,\n `\u2022 Ensure all required parameters are provided`,\n `\u2022 Check that parameters have the correct type (string, number, boolean)`,\n `\u2022 Verify file paths are absolute (start with /)`,\n `\u2022 Retry your request - the model often succeeds on retry`,\n )\n\n return message.join('\\n')\n}\n\nasync function* checkPermissionsAndCallTool(\n tool: Tool,\n toolUseID: string,\n siblingToolUseIDs: Set<string>,\n input: { [key: string]: boolean | string | number },\n context: ToolUseContext,\n canUseTool: CanUseToolFn,\n assistantMessage: AssistantMessage,\n shouldSkipPermissionCheck?: boolean,\n): AsyncGenerator<UserMessage | ProgressMessage, void> {\n // Validate input types with zod\n // (surprisingly, the model is not great at generating valid input)\n const isValidInput = tool.inputSchema.safeParse(input)\n if (!isValidInput.success) {\n let errorMessage = buildToolInputErrorMessage(\n tool,\n input,\n isValidInput.error,\n )\n\n yield createUserMessage([\n {\n type: 'tool_result',\n content: errorMessage,\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n return\n }\n\n const normalizedInput = normalizeToolInput(tool, input)\n\n // Validate input values. Each tool has its own validation logic\n const isValidCall = await tool.validateInput?.(\n normalizedInput as never,\n context,\n )\n if (isValidCall?.result === false) {\n yield createUserMessage([\n {\n type: 'tool_result',\n content: isValidCall!.message,\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n return\n }\n\n // Check whether we have permission to use the tool,\n // and ask the user for permission if we don't\n const permissionResult = shouldSkipPermissionCheck\n ? ({ result: true } as const)\n : await canUseTool(tool, normalizedInput, context, assistantMessage)\n if (permissionResult.result === false) {\n yield createUserMessage([\n {\n type: 'tool_result',\n content: permissionResult.message,\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n return\n }\n\n // \u2705 P1 FIX: Show permission waiting state when permission check is pending\n // Note: This is handled by the PermissionRequest dialog in the UI\n // But we update streaming state to indicate the phase\n if (!shouldSkipPermissionCheck && !permissionResult.result) {\n setStreamingState({ phase: 'permission' })\n }\n\n // PreToolUse hooks\n const hookManager = getHookManager()\n if (hookManager) {\n try {\n const decision = await hookManager.executePreToolUse(\n tool.name,\n normalizedInput as Record<string, unknown>,\n )\n\n if (!decision.shouldContinue) {\n if (decision.shouldAskUser) {\n // Ask user for approval via permission system\n const approved = await canUseTool(\n tool,\n normalizedInput,\n context,\n assistantMessage,\n )\n if (approved.result === false) {\n yield createUserMessage([\n {\n type: 'tool_result',\n content:\n decision.reason || 'Hook requested approval but user denied',\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n return\n }\n } else {\n // Hook blocked the tool\n yield createUserMessage([\n {\n type: 'tool_result',\n content: decision.reason || 'Tool execution blocked by hook',\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n return\n }\n }\n } catch (error) {\n debug.error('PreToolUse hook failed', { error })\n // Continue on error (fail-safe)\n }\n }\n\n // Call the tool with timeout protection\n let toolOutput: FullToolUseResult | null = null\n try {\n // Add toolUseId to context for tracking concurrent executions\n const contextWithToolUseId = {\n ...context,\n toolUseId: toolUseID,\n }\n const rawGenerator = tool.call(\n normalizedInput as never,\n contextWithToolUseId,\n )\n // Wrap tool execution with timeout to prevent infinite hangs\n const generator = withTimeout(rawGenerator, tool.name)\n\n for await (const result of generator) {\n switch (result.type) {\n case 'result':\n // Normalize content to ensure API compatibility\n const normalizedContent = normalizeToolResultContent(\n result.resultForAssistant ?? result.data,\n )\n toolOutput = {\n data: result.data,\n resultForAssistant: normalizedContent,\n // Preserve contextModifier and newMessages for caller to process\n newMessages: result.newMessages,\n contextModifier: result.contextModifier,\n }\n\n yield createUserMessage(\n [\n {\n type: 'tool_result',\n content: normalizedContent,\n tool_use_id: toolUseID,\n },\n ],\n toolOutput,\n )\n\n // PostToolUse hooks (fire-and-forget)\n if (hookManager && toolOutput) {\n hookManager\n .executePostToolUse(\n tool.name,\n normalizedInput as Record<string, unknown>,\n toolOutput as any,\n )\n .catch(err => {\n debug.error('PostToolUse hook failed', { error: err })\n })\n }\n\n return\n case 'progress':\n yield createProgressMessage(\n toolUseID,\n siblingToolUseIDs,\n result.content,\n result.normalizedMessages || [],\n result.tools || [],\n )\n break\n }\n }\n } catch (error) {\n // Special handling for timeout errors\n if (error instanceof ToolTimeoutError) {\n debug.error('TOOL_TIMEOUT', {\n toolName: tool.name,\n toolUseID,\n timeoutMs: error.timeoutMs,\n })\n\n yield createUserMessage([\n {\n type: 'tool_result',\n content: `Error: ${error.message}. The tool execution was cancelled. Consider breaking this operation into smaller steps or checking if there's an infinite loop.`,\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n return\n }\n\n const content = formatError(error)\n logError(error)\n\n yield createUserMessage([\n {\n type: 'tool_result',\n content,\n is_error: true,\n tool_use_id: toolUseID,\n },\n ])\n }\n}\n\nfunction formatError(error: unknown): string {\n if (!(error instanceof Error)) {\n return String(error)\n }\n const parts = [error.message]\n if ('stderr' in error && typeof error.stderr === 'string') {\n parts.push(error.stderr)\n }\n if ('stdout' in error && typeof error.stdout === 'string') {\n parts.push(error.stdout)\n }\n const fullMessage = parts.filter(Boolean).join('\\n')\n if (fullMessage.length <= 10000) {\n return fullMessage\n }\n const halfLength = 5000\n const start = fullMessage.slice(0, halfLength)\n const end = fullMessage.slice(-halfLength)\n return `${start}\\n\\n... [${fullMessage.length - 10000} characters truncated] ...\\n\\n${end}`\n}\n"],
|
|
5
|
+
"mappings": "AAQA;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAS,yBAAyB;AAClC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,WAAW;AACpB,SAAS,gBAAgB;AACzB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,EAEA;AAAA,OACK;AAEP,SAAS,aAAa,wBAAwB;AAC9C,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,IACF,CAAC;AAAA,EACH;AACF;AAEA,SAAS,YAAY,OAAwB;AAC3C,MAAI,EAAE,iBAAiB,QAAQ;AAC7B,WAAO,OAAO,KAAK;AAAA,EACrB;AACA,QAAM,QAAQ,CAAC,MAAM,OAAO;AAC5B,MAAI,YAAY,SAAS,OAAO,MAAM,WAAW,UAAU;AACzD,UAAM,KAAK,MAAM,MAAM;AAAA,EACzB;AACA,MAAI,YAAY,SAAS,OAAO,MAAM,WAAW,UAAU;AACzD,UAAM,KAAK,MAAM,MAAM;AAAA,EACzB;AACA,QAAM,cAAc,MAAM,OAAO,OAAO,EAAE,KAAK,IAAI;AACnD,MAAI,YAAY,UAAU,KAAO;AAC/B,WAAO;AAAA,EACT;AACA,QAAM,aAAa;AACnB,QAAM,QAAQ,YAAY,MAAM,GAAG,UAAU;AAC7C,QAAM,MAAM,YAAY,MAAM,CAAC,UAAU;AACzC,SAAO,GAAG,KAAK;AAAA;AAAA,OAAY,YAAY,SAAS,GAAK;AAAA;AAAA,EAAiC,GAAG;AAC3F;",
|
|
6
|
+
"names": ["result", "message"]
|
|
7
7
|
}
|
package/dist/screens/REPL.js
CHANGED
|
@@ -22,7 +22,8 @@ import { useLogStartupTime } from "../hooks/useLogStartupTime.js";
|
|
|
22
22
|
import { addToHistory } from "../history.js";
|
|
23
23
|
import { useApiKeyVerification } from "../hooks/useApiKeyVerification.js";
|
|
24
24
|
import useCanUseTool from "../hooks/useCanUseTool.js";
|
|
25
|
-
import { useLogMessages } from "../hooks/useLogMessages.js";
|
|
25
|
+
import { useLogMessages, serializeMessageForLog } from "../hooks/useLogMessages.js";
|
|
26
|
+
import { overwriteLog } from "../utils/log.js";
|
|
26
27
|
import { PermissionProvider } from "../context/PermissionContext.js";
|
|
27
28
|
import { useFullscreenExitCallback } from "../hooks/useTerminalSize.js";
|
|
28
29
|
import { ModeIndicator } from "../components/ModeIndicator.js";
|
|
@@ -84,10 +85,12 @@ import useAskUser from "../hooks/useAskUser.js";
|
|
|
84
85
|
import { useMessageGroups } from "../hooks/useMessageGroups.js";
|
|
85
86
|
import { useAgentTranscripts } from "../hooks/useAgentTranscripts.js";
|
|
86
87
|
import { GroupRenderer } from "../components/messages/GroupRenderer.js";
|
|
88
|
+
import { SEMANTIC_COLORS } from "../constants/colors.js";
|
|
87
89
|
const RESUME_VISIBLE_MESSAGE_COUNT = 10;
|
|
88
90
|
function REPL({
|
|
89
91
|
commands,
|
|
90
92
|
safeMode,
|
|
93
|
+
safetyMode = "yolo",
|
|
91
94
|
debug = false,
|
|
92
95
|
initialForkNumber = 0,
|
|
93
96
|
initialPrompt,
|
|
@@ -128,6 +131,7 @@ function REPL({
|
|
|
128
131
|
null
|
|
129
132
|
);
|
|
130
133
|
const [messages, setMessages] = useState(initialMessages ?? []);
|
|
134
|
+
const messagesRef = useRef(initialMessages ?? []);
|
|
131
135
|
const [messageHistory, setMessageHistory] = useState([]);
|
|
132
136
|
const [inputValue, setInputValue] = useState("");
|
|
133
137
|
const [inputMode, setInputMode] = useState(
|
|
@@ -209,6 +213,14 @@ function REPL({
|
|
|
209
213
|
if (!isLoading) {
|
|
210
214
|
return;
|
|
211
215
|
}
|
|
216
|
+
const currentMessages = messagesRef.current;
|
|
217
|
+
if (currentMessages.length > 0) {
|
|
218
|
+
const serializedMessages = currentMessages.map(serializeMessageForLog);
|
|
219
|
+
overwriteLog(
|
|
220
|
+
getMessagesPath(messageLogName, forkNumber, 0),
|
|
221
|
+
serializedMessages
|
|
222
|
+
);
|
|
223
|
+
}
|
|
212
224
|
setToolJSX(null);
|
|
213
225
|
setToolUseConfirm(null);
|
|
214
226
|
setBinaryFeedbackContext(null);
|
|
@@ -297,6 +309,7 @@ function REPL({
|
|
|
297
309
|
tools,
|
|
298
310
|
verbose,
|
|
299
311
|
safeMode,
|
|
312
|
+
safetyMode,
|
|
300
313
|
maxThinkingTokens
|
|
301
314
|
},
|
|
302
315
|
messageId: getLastAssistantMessageId([...messages, ...newMessages]),
|
|
@@ -381,6 +394,7 @@ function REPL({
|
|
|
381
394
|
tools,
|
|
382
395
|
verbose,
|
|
383
396
|
safeMode,
|
|
397
|
+
safetyMode,
|
|
384
398
|
maxThinkingTokens,
|
|
385
399
|
// If this came from Koding mode, pass that along
|
|
386
400
|
isKodingRequest: isKodingRequest || void 0
|
|
@@ -448,6 +462,9 @@ function REPL({
|
|
|
448
462
|
setForkNumber((prev) => prev + 1);
|
|
449
463
|
});
|
|
450
464
|
}, []);
|
|
465
|
+
useEffect(() => {
|
|
466
|
+
messagesRef.current = messages;
|
|
467
|
+
}, [messages]);
|
|
451
468
|
useLogMessages(messages, messageLogName, forkNumber);
|
|
452
469
|
useLogStartupTime();
|
|
453
470
|
useEffect(() => {
|
|
@@ -543,7 +560,7 @@ function REPL({
|
|
|
543
560
|
const result = [
|
|
544
561
|
{
|
|
545
562
|
type: "static",
|
|
546
|
-
jsx: /* @__PURE__ */ React.createElement(Box, { flexDirection: "column", key: `logo${forkNumber}` }, /* @__PURE__ */ React.createElement(
|
|
563
|
+
jsx: /* @__PURE__ */ React.createElement(Box, { flexDirection: "column", width: "100%", key: `logo${forkNumber}` }, /* @__PURE__ */ React.createElement(
|
|
547
564
|
Logo,
|
|
548
565
|
{
|
|
549
566
|
mcpClients,
|
|
@@ -557,7 +574,7 @@ function REPL({
|
|
|
557
574
|
if (hiddenMessageCount > 0) {
|
|
558
575
|
result.push({
|
|
559
576
|
type: "static",
|
|
560
|
-
jsx: /* @__PURE__ */ React.createElement(Box, { key: "collapsed-history", paddingLeft: 2, marginBottom: 1 }, /* @__PURE__ */ React.createElement(Text, {
|
|
577
|
+
jsx: /* @__PURE__ */ React.createElement(Box, { key: "collapsed-history", paddingLeft: 2, marginBottom: 1 }, /* @__PURE__ */ React.createElement(Text, { color: SEMANTIC_COLORS.dim }, "\xB7\xB7\xB7 ", hiddenMessageCount, " earlier messages hidden \xB7\xB7\xB7"))
|
|
561
578
|
});
|
|
562
579
|
}
|
|
563
580
|
const renderedGroupIds = /* @__PURE__ */ new Set();
|
|
@@ -784,9 +801,12 @@ function REPL({
|
|
|
784
801
|
const message = `${getVerboseLabel(newVerbose)} (Ctrl+O)`;
|
|
785
802
|
setVerboseToggleMessage(message);
|
|
786
803
|
setTimeout(() => setVerboseToggleMessage(null), 2e3);
|
|
804
|
+
setIsResizeClearing(true);
|
|
805
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
787
806
|
await clearTerminal();
|
|
788
807
|
setVerbose(newVerbose);
|
|
789
808
|
setForkNumber((prev) => prev + 1);
|
|
809
|
+
setIsResizeClearing(false);
|
|
790
810
|
}, []);
|
|
791
811
|
const handleQueueAwareQuery = useCallback(
|
|
792
812
|
async (newMessages, abortController2) => {
|