@shareai-lab/kode 1.0.9

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.
Files changed (286) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +426 -0
  3. package/README.zh-CN.md +326 -0
  4. package/cli.js +79 -0
  5. package/package.json +119 -0
  6. package/scripts/postinstall.js +18 -0
  7. package/src/ProjectOnboarding.tsx +198 -0
  8. package/src/Tool.ts +82 -0
  9. package/src/commands/agents.tsx +3410 -0
  10. package/src/commands/approvedTools.ts +53 -0
  11. package/src/commands/bug.tsx +20 -0
  12. package/src/commands/clear.ts +43 -0
  13. package/src/commands/compact.ts +120 -0
  14. package/src/commands/config.tsx +19 -0
  15. package/src/commands/cost.ts +18 -0
  16. package/src/commands/ctx_viz.ts +209 -0
  17. package/src/commands/doctor.ts +24 -0
  18. package/src/commands/help.tsx +19 -0
  19. package/src/commands/init.ts +37 -0
  20. package/src/commands/listen.ts +42 -0
  21. package/src/commands/login.tsx +51 -0
  22. package/src/commands/logout.tsx +40 -0
  23. package/src/commands/mcp.ts +41 -0
  24. package/src/commands/model.tsx +40 -0
  25. package/src/commands/modelstatus.tsx +20 -0
  26. package/src/commands/onboarding.tsx +34 -0
  27. package/src/commands/pr_comments.ts +59 -0
  28. package/src/commands/refreshCommands.ts +54 -0
  29. package/src/commands/release-notes.ts +34 -0
  30. package/src/commands/resume.tsx +31 -0
  31. package/src/commands/review.ts +49 -0
  32. package/src/commands/terminalSetup.ts +221 -0
  33. package/src/commands.ts +139 -0
  34. package/src/components/ApproveApiKey.tsx +93 -0
  35. package/src/components/AsciiLogo.tsx +13 -0
  36. package/src/components/AutoUpdater.tsx +148 -0
  37. package/src/components/Bug.tsx +367 -0
  38. package/src/components/Config.tsx +293 -0
  39. package/src/components/ConsoleOAuthFlow.tsx +327 -0
  40. package/src/components/Cost.tsx +23 -0
  41. package/src/components/CostThresholdDialog.tsx +46 -0
  42. package/src/components/CustomSelect/option-map.ts +42 -0
  43. package/src/components/CustomSelect/select-option.tsx +78 -0
  44. package/src/components/CustomSelect/select.tsx +152 -0
  45. package/src/components/CustomSelect/theme.ts +45 -0
  46. package/src/components/CustomSelect/use-select-state.ts +414 -0
  47. package/src/components/CustomSelect/use-select.ts +35 -0
  48. package/src/components/FallbackToolUseRejectedMessage.tsx +15 -0
  49. package/src/components/FileEditToolUpdatedMessage.tsx +66 -0
  50. package/src/components/Help.tsx +215 -0
  51. package/src/components/HighlightedCode.tsx +33 -0
  52. package/src/components/InvalidConfigDialog.tsx +113 -0
  53. package/src/components/Link.tsx +32 -0
  54. package/src/components/LogSelector.tsx +86 -0
  55. package/src/components/Logo.tsx +170 -0
  56. package/src/components/MCPServerApprovalDialog.tsx +100 -0
  57. package/src/components/MCPServerDialogCopy.tsx +25 -0
  58. package/src/components/MCPServerMultiselectDialog.tsx +109 -0
  59. package/src/components/Message.tsx +221 -0
  60. package/src/components/MessageResponse.tsx +15 -0
  61. package/src/components/MessageSelector.tsx +211 -0
  62. package/src/components/ModeIndicator.tsx +88 -0
  63. package/src/components/ModelConfig.tsx +301 -0
  64. package/src/components/ModelListManager.tsx +227 -0
  65. package/src/components/ModelSelector.tsx +3387 -0
  66. package/src/components/ModelStatusDisplay.tsx +230 -0
  67. package/src/components/Onboarding.tsx +274 -0
  68. package/src/components/PressEnterToContinue.tsx +11 -0
  69. package/src/components/PromptInput.tsx +760 -0
  70. package/src/components/SentryErrorBoundary.ts +39 -0
  71. package/src/components/Spinner.tsx +129 -0
  72. package/src/components/StickerRequestForm.tsx +16 -0
  73. package/src/components/StructuredDiff.tsx +191 -0
  74. package/src/components/TextInput.tsx +259 -0
  75. package/src/components/TodoItem.tsx +47 -0
  76. package/src/components/TokenWarning.tsx +31 -0
  77. package/src/components/ToolUseLoader.tsx +40 -0
  78. package/src/components/TrustDialog.tsx +106 -0
  79. package/src/components/binary-feedback/BinaryFeedback.tsx +63 -0
  80. package/src/components/binary-feedback/BinaryFeedbackOption.tsx +111 -0
  81. package/src/components/binary-feedback/BinaryFeedbackView.tsx +172 -0
  82. package/src/components/binary-feedback/utils.ts +220 -0
  83. package/src/components/messages/AssistantBashOutputMessage.tsx +22 -0
  84. package/src/components/messages/AssistantLocalCommandOutputMessage.tsx +49 -0
  85. package/src/components/messages/AssistantRedactedThinkingMessage.tsx +19 -0
  86. package/src/components/messages/AssistantTextMessage.tsx +144 -0
  87. package/src/components/messages/AssistantThinkingMessage.tsx +40 -0
  88. package/src/components/messages/AssistantToolUseMessage.tsx +133 -0
  89. package/src/components/messages/TaskProgressMessage.tsx +32 -0
  90. package/src/components/messages/TaskToolMessage.tsx +58 -0
  91. package/src/components/messages/UserBashInputMessage.tsx +28 -0
  92. package/src/components/messages/UserCommandMessage.tsx +30 -0
  93. package/src/components/messages/UserKodingInputMessage.tsx +28 -0
  94. package/src/components/messages/UserPromptMessage.tsx +35 -0
  95. package/src/components/messages/UserTextMessage.tsx +39 -0
  96. package/src/components/messages/UserToolResultMessage/UserToolCanceledMessage.tsx +12 -0
  97. package/src/components/messages/UserToolResultMessage/UserToolErrorMessage.tsx +36 -0
  98. package/src/components/messages/UserToolResultMessage/UserToolRejectMessage.tsx +31 -0
  99. package/src/components/messages/UserToolResultMessage/UserToolResultMessage.tsx +57 -0
  100. package/src/components/messages/UserToolResultMessage/UserToolSuccessMessage.tsx +35 -0
  101. package/src/components/messages/UserToolResultMessage/utils.tsx +56 -0
  102. package/src/components/permissions/BashPermissionRequest/BashPermissionRequest.tsx +121 -0
  103. package/src/components/permissions/FallbackPermissionRequest.tsx +153 -0
  104. package/src/components/permissions/FileEditPermissionRequest/FileEditPermissionRequest.tsx +182 -0
  105. package/src/components/permissions/FileEditPermissionRequest/FileEditToolDiff.tsx +77 -0
  106. package/src/components/permissions/FileWritePermissionRequest/FileWritePermissionRequest.tsx +164 -0
  107. package/src/components/permissions/FileWritePermissionRequest/FileWriteToolDiff.tsx +83 -0
  108. package/src/components/permissions/FilesystemPermissionRequest/FilesystemPermissionRequest.tsx +240 -0
  109. package/src/components/permissions/PermissionRequest.tsx +101 -0
  110. package/src/components/permissions/PermissionRequestTitle.tsx +69 -0
  111. package/src/components/permissions/hooks.ts +44 -0
  112. package/src/components/permissions/toolUseOptions.ts +59 -0
  113. package/src/components/permissions/utils.ts +23 -0
  114. package/src/constants/betas.ts +5 -0
  115. package/src/constants/claude-asterisk-ascii-art.tsx +238 -0
  116. package/src/constants/figures.ts +4 -0
  117. package/src/constants/keys.ts +3 -0
  118. package/src/constants/macros.ts +8 -0
  119. package/src/constants/modelCapabilities.ts +179 -0
  120. package/src/constants/models.ts +1025 -0
  121. package/src/constants/oauth.ts +18 -0
  122. package/src/constants/product.ts +17 -0
  123. package/src/constants/prompts.ts +168 -0
  124. package/src/constants/releaseNotes.ts +7 -0
  125. package/src/context/PermissionContext.tsx +149 -0
  126. package/src/context.ts +278 -0
  127. package/src/cost-tracker.ts +84 -0
  128. package/src/entrypoints/cli.tsx +1542 -0
  129. package/src/entrypoints/mcp.ts +176 -0
  130. package/src/history.ts +25 -0
  131. package/src/hooks/useApiKeyVerification.ts +59 -0
  132. package/src/hooks/useArrowKeyHistory.ts +55 -0
  133. package/src/hooks/useCanUseTool.ts +138 -0
  134. package/src/hooks/useCancelRequest.ts +39 -0
  135. package/src/hooks/useDoublePress.ts +42 -0
  136. package/src/hooks/useExitOnCtrlCD.ts +31 -0
  137. package/src/hooks/useInterval.ts +25 -0
  138. package/src/hooks/useLogMessages.ts +16 -0
  139. package/src/hooks/useLogStartupTime.ts +12 -0
  140. package/src/hooks/useNotifyAfterTimeout.ts +65 -0
  141. package/src/hooks/usePermissionRequestLogging.ts +44 -0
  142. package/src/hooks/useTerminalSize.ts +49 -0
  143. package/src/hooks/useTextInput.ts +318 -0
  144. package/src/hooks/useUnifiedCompletion.ts +1405 -0
  145. package/src/messages.ts +38 -0
  146. package/src/permissions.ts +268 -0
  147. package/src/query.ts +715 -0
  148. package/src/screens/ConfigureNpmPrefix.tsx +197 -0
  149. package/src/screens/Doctor.tsx +219 -0
  150. package/src/screens/LogList.tsx +68 -0
  151. package/src/screens/REPL.tsx +809 -0
  152. package/src/screens/ResumeConversation.tsx +68 -0
  153. package/src/services/adapters/base.ts +38 -0
  154. package/src/services/adapters/chatCompletions.ts +90 -0
  155. package/src/services/adapters/responsesAPI.ts +170 -0
  156. package/src/services/browserMocks.ts +66 -0
  157. package/src/services/claude.ts +2197 -0
  158. package/src/services/customCommands.ts +704 -0
  159. package/src/services/fileFreshness.ts +377 -0
  160. package/src/services/gpt5ConnectionTest.ts +340 -0
  161. package/src/services/mcpClient.ts +564 -0
  162. package/src/services/mcpServerApproval.tsx +50 -0
  163. package/src/services/mentionProcessor.ts +273 -0
  164. package/src/services/modelAdapterFactory.ts +69 -0
  165. package/src/services/notifier.ts +40 -0
  166. package/src/services/oauth.ts +357 -0
  167. package/src/services/openai.ts +1338 -0
  168. package/src/services/responseStateManager.ts +90 -0
  169. package/src/services/sentry.ts +3 -0
  170. package/src/services/statsig.ts +172 -0
  171. package/src/services/statsigStorage.ts +86 -0
  172. package/src/services/systemReminder.ts +507 -0
  173. package/src/services/vcr.ts +161 -0
  174. package/src/test/testAdapters.ts +96 -0
  175. package/src/tools/ArchitectTool/ArchitectTool.tsx +122 -0
  176. package/src/tools/ArchitectTool/prompt.ts +15 -0
  177. package/src/tools/AskExpertModelTool/AskExpertModelTool.tsx +569 -0
  178. package/src/tools/BashTool/BashTool.tsx +243 -0
  179. package/src/tools/BashTool/BashToolResultMessage.tsx +38 -0
  180. package/src/tools/BashTool/OutputLine.tsx +49 -0
  181. package/src/tools/BashTool/prompt.ts +174 -0
  182. package/src/tools/BashTool/utils.ts +56 -0
  183. package/src/tools/FileEditTool/FileEditTool.tsx +315 -0
  184. package/src/tools/FileEditTool/prompt.ts +51 -0
  185. package/src/tools/FileEditTool/utils.ts +58 -0
  186. package/src/tools/FileReadTool/FileReadTool.tsx +404 -0
  187. package/src/tools/FileReadTool/prompt.ts +7 -0
  188. package/src/tools/FileWriteTool/FileWriteTool.tsx +297 -0
  189. package/src/tools/FileWriteTool/prompt.ts +10 -0
  190. package/src/tools/GlobTool/GlobTool.tsx +119 -0
  191. package/src/tools/GlobTool/prompt.ts +8 -0
  192. package/src/tools/GrepTool/GrepTool.tsx +147 -0
  193. package/src/tools/GrepTool/prompt.ts +11 -0
  194. package/src/tools/MCPTool/MCPTool.tsx +107 -0
  195. package/src/tools/MCPTool/prompt.ts +3 -0
  196. package/src/tools/MemoryReadTool/MemoryReadTool.tsx +127 -0
  197. package/src/tools/MemoryReadTool/prompt.ts +3 -0
  198. package/src/tools/MemoryWriteTool/MemoryWriteTool.tsx +89 -0
  199. package/src/tools/MemoryWriteTool/prompt.ts +3 -0
  200. package/src/tools/MultiEditTool/MultiEditTool.tsx +366 -0
  201. package/src/tools/MultiEditTool/prompt.ts +45 -0
  202. package/src/tools/NotebookEditTool/NotebookEditTool.tsx +298 -0
  203. package/src/tools/NotebookEditTool/prompt.ts +3 -0
  204. package/src/tools/NotebookReadTool/NotebookReadTool.tsx +258 -0
  205. package/src/tools/NotebookReadTool/prompt.ts +3 -0
  206. package/src/tools/StickerRequestTool/StickerRequestTool.tsx +93 -0
  207. package/src/tools/StickerRequestTool/prompt.ts +19 -0
  208. package/src/tools/TaskTool/TaskTool.tsx +466 -0
  209. package/src/tools/TaskTool/constants.ts +1 -0
  210. package/src/tools/TaskTool/prompt.ts +92 -0
  211. package/src/tools/ThinkTool/ThinkTool.tsx +54 -0
  212. package/src/tools/ThinkTool/prompt.ts +12 -0
  213. package/src/tools/TodoWriteTool/TodoWriteTool.tsx +313 -0
  214. package/src/tools/TodoWriteTool/prompt.ts +63 -0
  215. package/src/tools/URLFetcherTool/URLFetcherTool.tsx +178 -0
  216. package/src/tools/URLFetcherTool/cache.ts +55 -0
  217. package/src/tools/URLFetcherTool/htmlToMarkdown.ts +55 -0
  218. package/src/tools/URLFetcherTool/prompt.ts +17 -0
  219. package/src/tools/WebSearchTool/WebSearchTool.tsx +103 -0
  220. package/src/tools/WebSearchTool/prompt.ts +13 -0
  221. package/src/tools/WebSearchTool/searchProviders.ts +66 -0
  222. package/src/tools/lsTool/lsTool.tsx +272 -0
  223. package/src/tools/lsTool/prompt.ts +2 -0
  224. package/src/tools.ts +67 -0
  225. package/src/types/PermissionMode.ts +120 -0
  226. package/src/types/RequestContext.ts +72 -0
  227. package/src/types/conversation.ts +51 -0
  228. package/src/types/logs.ts +58 -0
  229. package/src/types/modelCapabilities.ts +64 -0
  230. package/src/types/notebook.ts +87 -0
  231. package/src/utils/Cursor.ts +436 -0
  232. package/src/utils/PersistentShell.ts +552 -0
  233. package/src/utils/advancedFuzzyMatcher.ts +290 -0
  234. package/src/utils/agentLoader.ts +278 -0
  235. package/src/utils/agentStorage.ts +97 -0
  236. package/src/utils/array.ts +3 -0
  237. package/src/utils/ask.tsx +99 -0
  238. package/src/utils/auth.ts +13 -0
  239. package/src/utils/autoCompactCore.ts +223 -0
  240. package/src/utils/autoUpdater.ts +458 -0
  241. package/src/utils/betas.ts +20 -0
  242. package/src/utils/browser.ts +14 -0
  243. package/src/utils/cleanup.ts +72 -0
  244. package/src/utils/commands.ts +261 -0
  245. package/src/utils/commonUnixCommands.ts +161 -0
  246. package/src/utils/config.ts +945 -0
  247. package/src/utils/conversationRecovery.ts +55 -0
  248. package/src/utils/debugLogger.ts +1235 -0
  249. package/src/utils/diff.ts +42 -0
  250. package/src/utils/env.ts +57 -0
  251. package/src/utils/errors.ts +21 -0
  252. package/src/utils/exampleCommands.ts +109 -0
  253. package/src/utils/execFileNoThrow.ts +51 -0
  254. package/src/utils/expertChatStorage.ts +136 -0
  255. package/src/utils/file.ts +405 -0
  256. package/src/utils/fileRecoveryCore.ts +71 -0
  257. package/src/utils/format.tsx +44 -0
  258. package/src/utils/fuzzyMatcher.ts +328 -0
  259. package/src/utils/generators.ts +62 -0
  260. package/src/utils/git.ts +92 -0
  261. package/src/utils/globalLogger.ts +77 -0
  262. package/src/utils/http.ts +10 -0
  263. package/src/utils/imagePaste.ts +38 -0
  264. package/src/utils/json.ts +13 -0
  265. package/src/utils/log.ts +382 -0
  266. package/src/utils/markdown.ts +213 -0
  267. package/src/utils/messageContextManager.ts +289 -0
  268. package/src/utils/messages.tsx +939 -0
  269. package/src/utils/model.ts +914 -0
  270. package/src/utils/permissions/filesystem.ts +127 -0
  271. package/src/utils/responseState.ts +23 -0
  272. package/src/utils/ripgrep.ts +167 -0
  273. package/src/utils/secureFile.ts +564 -0
  274. package/src/utils/sessionState.ts +49 -0
  275. package/src/utils/state.ts +25 -0
  276. package/src/utils/style.ts +29 -0
  277. package/src/utils/terminal.ts +50 -0
  278. package/src/utils/theme.ts +127 -0
  279. package/src/utils/thinking.ts +144 -0
  280. package/src/utils/todoStorage.ts +431 -0
  281. package/src/utils/tokens.ts +43 -0
  282. package/src/utils/toolExecutionController.ts +163 -0
  283. package/src/utils/unaryLogging.ts +26 -0
  284. package/src/utils/user.ts +37 -0
  285. package/src/utils/validate.ts +165 -0
  286. package/yoga.wasm +0 -0
package/src/query.ts ADDED
@@ -0,0 +1,715 @@
1
+ import {
2
+ Message as APIAssistantMessage,
3
+ MessageParam,
4
+ ToolUseBlock,
5
+ } from '@anthropic-ai/sdk/resources/index.mjs'
6
+ import { UUID } from 'crypto'
7
+ import type { Tool, ToolUseContext } from './Tool'
8
+ import {
9
+ messagePairValidForBinaryFeedback,
10
+ shouldUseBinaryFeedback,
11
+ } from './components/binary-feedback/utils.js'
12
+ import { CanUseToolFn } from './hooks/useCanUseTool'
13
+ import {
14
+ formatSystemPromptWithContext,
15
+ queryLLM,
16
+ queryModel,
17
+ } from './services/claude.js'
18
+ import { emitReminderEvent } from './services/systemReminder'
19
+ import { logEvent } from './services/statsig'
20
+ import { all } from './utils/generators'
21
+ import { logError } from './utils/log'
22
+ import {
23
+ debug as debugLogger,
24
+ markPhase,
25
+ getCurrentRequest,
26
+ logUserFriendly,
27
+ } from './utils/debugLogger'
28
+ import { getModelManager } from './utils/model.js'
29
+ import {
30
+ createAssistantMessage,
31
+ createProgressMessage,
32
+ createToolResultStopMessage,
33
+ createUserMessage,
34
+ FullToolUseResult,
35
+ INTERRUPT_MESSAGE,
36
+ INTERRUPT_MESSAGE_FOR_TOOL_USE,
37
+ NormalizedMessage,
38
+ normalizeMessagesForAPI,
39
+ } from './utils/messages.js'
40
+ import { createToolExecutionController } from './utils/toolExecutionController'
41
+ import { BashTool } from './tools/BashTool/BashTool'
42
+ import { getCwd } from './utils/state'
43
+ import { checkAutoCompact } from './utils/autoCompactCore'
44
+
45
+ // Extended ToolUseContext for query functions
46
+ interface ExtendedToolUseContext extends ToolUseContext {
47
+ abortController: AbortController
48
+ options: {
49
+ commands: any[]
50
+ forkNumber: number
51
+ messageLogName: string
52
+ tools: Tool[]
53
+ verbose: boolean
54
+ safeMode: boolean
55
+ maxThinkingTokens: number
56
+ isKodingRequest?: boolean
57
+ model?: string | import('./utils/config').ModelPointerType
58
+ }
59
+ readFileTimestamps: { [filename: string]: number }
60
+ setToolJSX: (jsx: any) => void
61
+ requestId?: string
62
+ }
63
+
64
+ export type Response = { costUSD: number; response: string }
65
+ export type UserMessage = {
66
+ message: MessageParam
67
+ type: 'user'
68
+ uuid: UUID
69
+ toolUseResult?: FullToolUseResult
70
+ options?: {
71
+ isKodingRequest?: boolean
72
+ kodingContext?: string
73
+ }
74
+ }
75
+
76
+ export type AssistantMessage = {
77
+ costUSD: number
78
+ durationMs: number
79
+ message: APIAssistantMessage
80
+ type: 'assistant'
81
+ uuid: UUID
82
+ isApiErrorMessage?: boolean
83
+ responseId?: string // For GPT-5 Responses API state management
84
+ }
85
+
86
+ export type BinaryFeedbackResult =
87
+ | { message: AssistantMessage | null; shouldSkipPermissionCheck: false }
88
+ | { message: AssistantMessage; shouldSkipPermissionCheck: true }
89
+
90
+ export type ProgressMessage = {
91
+ content: AssistantMessage
92
+ normalizedMessages: NormalizedMessage[]
93
+ siblingToolUseIDs: Set<string>
94
+ tools: Tool[]
95
+ toolUseID: string
96
+ type: 'progress'
97
+ uuid: UUID
98
+ }
99
+
100
+ // Each array item is either a single message or a message-and-response pair
101
+ export type Message = UserMessage | AssistantMessage | ProgressMessage
102
+
103
+ const MAX_TOOL_USE_CONCURRENCY = 10
104
+
105
+ // Returns a message if we got one, or `null` if the user cancelled
106
+ async function queryWithBinaryFeedback(
107
+ toolUseContext: ExtendedToolUseContext,
108
+ getAssistantResponse: () => Promise<AssistantMessage>,
109
+ getBinaryFeedbackResponse?: (
110
+ m1: AssistantMessage,
111
+ m2: AssistantMessage,
112
+ ) => Promise<BinaryFeedbackResult>,
113
+ ): Promise<BinaryFeedbackResult> {
114
+ if (
115
+ process.env.USER_TYPE !== 'ant' ||
116
+ !getBinaryFeedbackResponse ||
117
+ !(await shouldUseBinaryFeedback())
118
+ ) {
119
+ const assistantMessage = await getAssistantResponse()
120
+ if (toolUseContext.abortController.signal.aborted) {
121
+ return { message: null, shouldSkipPermissionCheck: false }
122
+ }
123
+ return { message: assistantMessage, shouldSkipPermissionCheck: false }
124
+ }
125
+ const [m1, m2] = await Promise.all([
126
+ getAssistantResponse(),
127
+ getAssistantResponse(),
128
+ ])
129
+ if (toolUseContext.abortController.signal.aborted) {
130
+ return { message: null, shouldSkipPermissionCheck: false }
131
+ }
132
+ if (m2.isApiErrorMessage) {
133
+ // If m2 is an error, we might as well return m1, even if it's also an error --
134
+ // the UI will display it as an error as it would in the non-feedback path.
135
+ return { message: m1, shouldSkipPermissionCheck: false }
136
+ }
137
+ if (m1.isApiErrorMessage) {
138
+ return { message: m2, shouldSkipPermissionCheck: false }
139
+ }
140
+ if (!messagePairValidForBinaryFeedback(m1, m2)) {
141
+ return { message: m1, shouldSkipPermissionCheck: false }
142
+ }
143
+ return await getBinaryFeedbackResponse(m1, m2)
144
+ }
145
+
146
+ /**
147
+ * The rules of thinking are lengthy and fortuitous. They require plenty of thinking
148
+ * of most long duration and deep meditation for a wizard to wrap one's noggin around.
149
+ *
150
+ * The rules follow:
151
+ * 1. A message that contains a thinking or redacted_thinking block must be part of a query whose max_thinking_length > 0
152
+ * 2. A thinking block may not be the last message in a block
153
+ * 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)
154
+ *
155
+ * Heed these rules well, young wizard. For they are the rules of thinking, and
156
+ * the rules of thinking are the rules of the universe. If ye does not heed these
157
+ * rules, ye will be punished with an entire day of debugging and hair pulling.
158
+ */
159
+ export async function* query(
160
+ messages: Message[],
161
+ systemPrompt: string[],
162
+ context: { [k: string]: string },
163
+ canUseTool: CanUseToolFn,
164
+ toolUseContext: ExtendedToolUseContext,
165
+ getBinaryFeedbackResponse?: (
166
+ m1: AssistantMessage,
167
+ m2: AssistantMessage,
168
+ ) => Promise<BinaryFeedbackResult>,
169
+ ): AsyncGenerator<Message, void> {
170
+ const currentRequest = getCurrentRequest()
171
+
172
+ markPhase('QUERY_INIT')
173
+
174
+ // Auto-compact check
175
+ const { messages: processedMessages, wasCompacted } = await checkAutoCompact(
176
+ messages,
177
+ toolUseContext,
178
+ )
179
+ if (wasCompacted) {
180
+ messages = processedMessages
181
+ }
182
+
183
+ markPhase('SYSTEM_PROMPT_BUILD')
184
+
185
+ const { systemPrompt: fullSystemPrompt, reminders } =
186
+ formatSystemPromptWithContext(systemPrompt, context, toolUseContext.agentId)
187
+
188
+ // Emit session startup event
189
+ emitReminderEvent('session:startup', {
190
+ agentId: toolUseContext.agentId,
191
+ messages: messages.length,
192
+ timestamp: Date.now(),
193
+ })
194
+
195
+ // Inject reminders into the latest user message
196
+ if (reminders && messages.length > 0) {
197
+ // Find the last user message
198
+ for (let i = messages.length - 1; i >= 0; i--) {
199
+ if (messages[i]?.type === 'user') {
200
+ const lastUserMessage = messages[i]
201
+ messages[i] = {
202
+ ...lastUserMessage,
203
+ message: {
204
+ ...lastUserMessage.message,
205
+ content:
206
+ typeof lastUserMessage.message.content === 'string'
207
+ ? reminders + lastUserMessage.message.content
208
+ : [
209
+ ...(Array.isArray(lastUserMessage.message.content)
210
+ ? lastUserMessage.message.content
211
+ : []),
212
+ { type: 'text', text: reminders },
213
+ ],
214
+ },
215
+ }
216
+ break
217
+ }
218
+ }
219
+ }
220
+
221
+ markPhase('LLM_PREPARATION')
222
+
223
+ function getAssistantResponse() {
224
+ return queryLLM(
225
+ normalizeMessagesForAPI(messages),
226
+ fullSystemPrompt,
227
+ toolUseContext.options.maxThinkingTokens,
228
+ toolUseContext.options.tools,
229
+ toolUseContext.abortController.signal,
230
+ {
231
+ safeMode: toolUseContext.options.safeMode ?? false,
232
+ model: toolUseContext.options.model || 'main',
233
+ prependCLISysprompt: true,
234
+ toolUseContext: toolUseContext,
235
+ },
236
+ )
237
+ }
238
+
239
+ const result = await queryWithBinaryFeedback(
240
+ toolUseContext,
241
+ getAssistantResponse,
242
+ getBinaryFeedbackResponse,
243
+ )
244
+
245
+ // If request was cancelled, return immediately with interrupt message
246
+ if (toolUseContext.abortController.signal.aborted) {
247
+ yield createAssistantMessage(INTERRUPT_MESSAGE)
248
+ return
249
+ }
250
+
251
+ if (result.message === null) {
252
+ yield createAssistantMessage(INTERRUPT_MESSAGE)
253
+ return
254
+ }
255
+
256
+ const assistantMessage = result.message
257
+ const shouldSkipPermissionCheck = result.shouldSkipPermissionCheck
258
+
259
+ yield assistantMessage
260
+
261
+ // @see https://docs.anthropic.com/en/docs/build-with-claude/tool-use
262
+ // Note: stop_reason === 'tool_use' is unreliable -- it's not always set correctly
263
+ const toolUseMessages = assistantMessage.message.content.filter(
264
+ _ => _.type === 'tool_use',
265
+ )
266
+
267
+ // If there's no more tool use, we're done
268
+ if (!toolUseMessages.length) {
269
+ return
270
+ }
271
+
272
+ const toolResults: UserMessage[] = []
273
+
274
+ // Simple concurrency check like original system
275
+ const canRunConcurrently = toolUseMessages.every(msg =>
276
+ toolUseContext.options.tools.find(t => t.name === msg.name)?.isReadOnly(),
277
+ )
278
+
279
+ if (canRunConcurrently) {
280
+ for await (const message of runToolsConcurrently(
281
+ toolUseMessages,
282
+ assistantMessage,
283
+ canUseTool,
284
+ toolUseContext,
285
+ shouldSkipPermissionCheck,
286
+ )) {
287
+ yield message
288
+ // progress messages are not sent to the server, so don't need to be accumulated for the next turn
289
+ if (message.type === 'user') {
290
+ toolResults.push(message)
291
+ }
292
+ }
293
+ } else {
294
+ for await (const message of runToolsSerially(
295
+ toolUseMessages,
296
+ assistantMessage,
297
+ canUseTool,
298
+ toolUseContext,
299
+ shouldSkipPermissionCheck,
300
+ )) {
301
+ yield message
302
+ // progress messages are not sent to the server, so don't need to be accumulated for the next turn
303
+ if (message.type === 'user') {
304
+ toolResults.push(message)
305
+ }
306
+ }
307
+ }
308
+
309
+ if (toolUseContext.abortController.signal.aborted) {
310
+ yield createAssistantMessage(INTERRUPT_MESSAGE_FOR_TOOL_USE)
311
+ return
312
+ }
313
+
314
+ // Sort toolResults to match the order of toolUseMessages
315
+ const orderedToolResults = toolResults.sort((a, b) => {
316
+ const aIndex = toolUseMessages.findIndex(
317
+ tu => tu.id === (a.message.content[0] as ToolUseBlock).id,
318
+ )
319
+ const bIndex = toolUseMessages.findIndex(
320
+ tu => tu.id === (b.message.content[0] as ToolUseBlock).id,
321
+ )
322
+ return aIndex - bIndex
323
+ })
324
+
325
+ // Recursive query
326
+
327
+ try {
328
+ yield* await query(
329
+ [...messages, assistantMessage, ...orderedToolResults],
330
+ systemPrompt,
331
+ context,
332
+ canUseTool,
333
+ toolUseContext,
334
+ getBinaryFeedbackResponse,
335
+ )
336
+ } catch (error) {
337
+ // Re-throw the error to maintain the original behavior
338
+ throw error
339
+ }
340
+ }
341
+
342
+ async function* runToolsConcurrently(
343
+ toolUseMessages: ToolUseBlock[],
344
+ assistantMessage: AssistantMessage,
345
+ canUseTool: CanUseToolFn,
346
+ toolUseContext: ExtendedToolUseContext,
347
+ shouldSkipPermissionCheck?: boolean,
348
+ ): AsyncGenerator<Message, void> {
349
+ yield* all(
350
+ toolUseMessages.map(toolUse =>
351
+ runToolUse(
352
+ toolUse,
353
+ new Set(toolUseMessages.map(_ => _.id)),
354
+ assistantMessage,
355
+ canUseTool,
356
+ toolUseContext,
357
+ shouldSkipPermissionCheck,
358
+ ),
359
+ ),
360
+ MAX_TOOL_USE_CONCURRENCY,
361
+ )
362
+ }
363
+
364
+ async function* runToolsSerially(
365
+ toolUseMessages: ToolUseBlock[],
366
+ assistantMessage: AssistantMessage,
367
+ canUseTool: CanUseToolFn,
368
+ toolUseContext: ExtendedToolUseContext,
369
+ shouldSkipPermissionCheck?: boolean,
370
+ ): AsyncGenerator<Message, void> {
371
+ for (const toolUse of toolUseMessages) {
372
+ yield* runToolUse(
373
+ toolUse,
374
+ new Set(toolUseMessages.map(_ => _.id)),
375
+ assistantMessage,
376
+ canUseTool,
377
+ toolUseContext,
378
+ shouldSkipPermissionCheck,
379
+ )
380
+ }
381
+ }
382
+
383
+ export async function* runToolUse(
384
+ toolUse: ToolUseBlock,
385
+ siblingToolUseIDs: Set<string>,
386
+ assistantMessage: AssistantMessage,
387
+ canUseTool: CanUseToolFn,
388
+ toolUseContext: ExtendedToolUseContext,
389
+ shouldSkipPermissionCheck?: boolean,
390
+ ): AsyncGenerator<Message, void> {
391
+ const currentRequest = getCurrentRequest()
392
+
393
+ // 🔍 Debug: 工具调用开始
394
+ debugLogger.flow('TOOL_USE_START', {
395
+ toolName: toolUse.name,
396
+ toolUseID: toolUse.id,
397
+ inputSize: JSON.stringify(toolUse.input).length,
398
+ siblingToolCount: siblingToolUseIDs.size,
399
+ shouldSkipPermissionCheck: !!shouldSkipPermissionCheck,
400
+ requestId: currentRequest?.id,
401
+ })
402
+
403
+ logUserFriendly(
404
+ 'TOOL_EXECUTION',
405
+ {
406
+ toolName: toolUse.name,
407
+ action: 'Starting',
408
+ target: toolUse.input ? Object.keys(toolUse.input).join(', ') : '',
409
+ },
410
+ currentRequest?.id,
411
+ )
412
+
413
+
414
+ logEvent('tengu_tool_use_start', {
415
+ toolName: toolUse.name,
416
+ toolUseID: toolUse.id,
417
+ })
418
+
419
+ const toolName = toolUse.name
420
+ const tool = toolUseContext.options.tools.find(t => t.name === toolName)
421
+
422
+ // Check if the tool exists
423
+ if (!tool) {
424
+ debugLogger.error('TOOL_NOT_FOUND', {
425
+ requestedTool: toolName,
426
+ availableTools: toolUseContext.options.tools.map(t => t.name),
427
+ toolUseID: toolUse.id,
428
+ requestId: currentRequest?.id,
429
+ })
430
+
431
+ logEvent('tengu_tool_use_error', {
432
+ error: `No such tool available: ${toolName}`,
433
+ messageID: assistantMessage.message.id,
434
+ toolName,
435
+ toolUseID: toolUse.id,
436
+ })
437
+
438
+ yield createUserMessage([
439
+ {
440
+ type: 'tool_result',
441
+ content: `Error: No such tool available: ${toolName}`,
442
+ is_error: true,
443
+ tool_use_id: toolUse.id,
444
+ },
445
+ ])
446
+ return
447
+ }
448
+
449
+ const toolInput = toolUse.input as { [key: string]: string }
450
+
451
+ debugLogger.flow('TOOL_VALIDATION_START', {
452
+ toolName: tool.name,
453
+ toolUseID: toolUse.id,
454
+ inputKeys: Object.keys(toolInput),
455
+ requestId: currentRequest?.id,
456
+ })
457
+
458
+ try {
459
+ // 🔧 Check for cancellation before starting tool execution
460
+ if (toolUseContext.abortController.signal.aborted) {
461
+ debugLogger.flow('TOOL_USE_CANCELLED_BEFORE_START', {
462
+ toolName: tool.name,
463
+ toolUseID: toolUse.id,
464
+ abortReason: 'AbortController signal',
465
+ requestId: currentRequest?.id,
466
+ })
467
+
468
+ logEvent('tengu_tool_use_cancelled', {
469
+ toolName: tool.name,
470
+ toolUseID: toolUse.id,
471
+ })
472
+
473
+ const message = createUserMessage([
474
+ createToolResultStopMessage(toolUse.id),
475
+ ])
476
+ yield message
477
+ return
478
+ }
479
+
480
+ // Track if any progress messages were yielded
481
+ let hasProgressMessages = false
482
+
483
+ for await (const message of checkPermissionsAndCallTool(
484
+ tool,
485
+ toolUse.id,
486
+ siblingToolUseIDs,
487
+ toolInput,
488
+ toolUseContext,
489
+ canUseTool,
490
+ assistantMessage,
491
+ shouldSkipPermissionCheck,
492
+ )) {
493
+ // 🔧 Check for cancellation during tool execution
494
+ if (toolUseContext.abortController.signal.aborted) {
495
+ debugLogger.flow('TOOL_USE_CANCELLED_DURING_EXECUTION', {
496
+ toolName: tool.name,
497
+ toolUseID: toolUse.id,
498
+ hasProgressMessages,
499
+ abortReason: 'AbortController signal during execution',
500
+ requestId: currentRequest?.id,
501
+ })
502
+
503
+ // If we yielded progress messages but got cancelled, yield a cancellation result
504
+ if (hasProgressMessages && message.type === 'progress') {
505
+ yield message // yield the last progress message first
506
+ }
507
+
508
+ // Always yield a tool result message for cancellation to clear UI state
509
+ const cancelMessage = createUserMessage([
510
+ createToolResultStopMessage(toolUse.id),
511
+ ])
512
+ yield cancelMessage
513
+ return
514
+ }
515
+
516
+ if (message.type === 'progress') {
517
+ hasProgressMessages = true
518
+ }
519
+
520
+ yield message
521
+ }
522
+ } catch (e) {
523
+ logError(e)
524
+
525
+ // 🔧 Even on error, ensure we yield a tool result to clear UI state
526
+ const errorMessage = createUserMessage([
527
+ {
528
+ type: 'tool_result',
529
+ content: `Tool execution failed: ${e instanceof Error ? e.message : String(e)}`,
530
+ is_error: true,
531
+ tool_use_id: toolUse.id,
532
+ },
533
+ ])
534
+ yield errorMessage
535
+ }
536
+ }
537
+
538
+ // TODO: Generalize this to all tools
539
+ export function normalizeToolInput(
540
+ tool: Tool,
541
+ input: { [key: string]: boolean | string | number },
542
+ ): { [key: string]: boolean | string | number } {
543
+ switch (tool) {
544
+ case BashTool: {
545
+ const { command, timeout } = BashTool.inputSchema.parse(input) // already validated upstream, won't throw
546
+ return {
547
+ command: command.replace(`cd ${getCwd()} && `, ''),
548
+ ...(timeout ? { timeout } : {}),
549
+ }
550
+ }
551
+ default:
552
+ return input
553
+ }
554
+ }
555
+
556
+ async function* checkPermissionsAndCallTool(
557
+ tool: Tool,
558
+ toolUseID: string,
559
+ siblingToolUseIDs: Set<string>,
560
+ input: { [key: string]: boolean | string | number },
561
+ context: ToolUseContext,
562
+ canUseTool: CanUseToolFn,
563
+ assistantMessage: AssistantMessage,
564
+ shouldSkipPermissionCheck?: boolean,
565
+ ): AsyncGenerator<UserMessage | ProgressMessage, void> {
566
+ // Validate input types with zod
567
+ // (surprisingly, the model is not great at generating valid input)
568
+ const isValidInput = tool.inputSchema.safeParse(input)
569
+ if (!isValidInput.success) {
570
+ // Create a more helpful error message for common cases
571
+ let errorMessage = `InputValidationError: ${isValidInput.error.message}`
572
+
573
+ // Special handling for the "View" tool (FileReadTool) being called with empty parameters
574
+ if (tool.name === 'View' && Object.keys(input).length === 0) {
575
+ 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"}`
576
+ }
577
+
578
+ logEvent('tengu_tool_use_error', {
579
+ error: errorMessage,
580
+ messageID: assistantMessage.message.id,
581
+ toolName: tool.name,
582
+ toolInput: JSON.stringify(input).slice(0, 200),
583
+ })
584
+ yield createUserMessage([
585
+ {
586
+ type: 'tool_result',
587
+ content: errorMessage,
588
+ is_error: true,
589
+ tool_use_id: toolUseID,
590
+ },
591
+ ])
592
+ return
593
+ }
594
+
595
+ const normalizedInput = normalizeToolInput(tool, input)
596
+
597
+ // Validate input values. Each tool has its own validation logic
598
+ const isValidCall = await tool.validateInput?.(
599
+ normalizedInput as never,
600
+ context,
601
+ )
602
+ if (isValidCall?.result === false) {
603
+ logEvent('tengu_tool_use_error', {
604
+ error: isValidCall?.message.slice(0, 2000),
605
+ messageID: assistantMessage.message.id,
606
+ toolName: tool.name,
607
+ toolInput: JSON.stringify(input).slice(0, 200),
608
+ ...(isValidCall?.meta ?? {}),
609
+ })
610
+ yield createUserMessage([
611
+ {
612
+ type: 'tool_result',
613
+ content: isValidCall!.message,
614
+ is_error: true,
615
+ tool_use_id: toolUseID,
616
+ },
617
+ ])
618
+ return
619
+ }
620
+
621
+ // Check whether we have permission to use the tool,
622
+ // and ask the user for permission if we don't
623
+ const permissionResult = shouldSkipPermissionCheck
624
+ ? ({ result: true } as const)
625
+ : await canUseTool(tool, normalizedInput, context, assistantMessage)
626
+ if (permissionResult.result === false) {
627
+ yield createUserMessage([
628
+ {
629
+ type: 'tool_result',
630
+ content: permissionResult.message,
631
+ is_error: true,
632
+ tool_use_id: toolUseID,
633
+ },
634
+ ])
635
+ return
636
+ }
637
+
638
+ // Call the tool
639
+ try {
640
+ const generator = tool.call(normalizedInput as never, context, canUseTool)
641
+ for await (const result of generator) {
642
+ switch (result.type) {
643
+ case 'result':
644
+ logEvent('tengu_tool_use_success', {
645
+ messageID: assistantMessage.message.id,
646
+ toolName: tool.name,
647
+ })
648
+ yield createUserMessage(
649
+ [
650
+ {
651
+ type: 'tool_result',
652
+ content: result.resultForAssistant,
653
+ tool_use_id: toolUseID,
654
+ },
655
+ ],
656
+ {
657
+ data: result.data,
658
+ resultForAssistant: result.resultForAssistant,
659
+ },
660
+ )
661
+ return
662
+ case 'progress':
663
+ logEvent('tengu_tool_use_progress', {
664
+ messageID: assistantMessage.message.id,
665
+ toolName: tool.name,
666
+ })
667
+ yield createProgressMessage(
668
+ toolUseID,
669
+ siblingToolUseIDs,
670
+ result.content,
671
+ result.normalizedMessages,
672
+ result.tools,
673
+ )
674
+ }
675
+ }
676
+ } catch (error) {
677
+ const content = formatError(error)
678
+ logError(error)
679
+ logEvent('tengu_tool_use_error', {
680
+ error: content.slice(0, 2000),
681
+ messageID: assistantMessage.message.id,
682
+ toolName: tool.name,
683
+ toolInput: JSON.stringify(input).slice(0, 1000),
684
+ })
685
+ yield createUserMessage([
686
+ {
687
+ type: 'tool_result',
688
+ content,
689
+ is_error: true,
690
+ tool_use_id: toolUseID,
691
+ },
692
+ ])
693
+ }
694
+ }
695
+
696
+ function formatError(error: unknown): string {
697
+ if (!(error instanceof Error)) {
698
+ return String(error)
699
+ }
700
+ const parts = [error.message]
701
+ if ('stderr' in error && typeof error.stderr === 'string') {
702
+ parts.push(error.stderr)
703
+ }
704
+ if ('stdout' in error && typeof error.stdout === 'string') {
705
+ parts.push(error.stdout)
706
+ }
707
+ const fullMessage = parts.filter(Boolean).join('\n')
708
+ if (fullMessage.length <= 10000) {
709
+ return fullMessage
710
+ }
711
+ const halfLength = 5000
712
+ const start = fullMessage.slice(0, halfLength)
713
+ const end = fullMessage.slice(-halfLength)
714
+ return `${start}\n\n... [${fullMessage.length - 10000} characters truncated] ...\n\n${end}`
715
+ }