( toolName: string, runtime: ReplWrapperRuntime, )
| 149 | } |
| 150 | |
| 151 | export function createToolWrapper( |
| 152 | toolName: string, |
| 153 | runtime: ReplWrapperRuntime, |
| 154 | ): (args: Record<string, unknown>) => Promise<unknown> { |
| 155 | return async (rawArgs: Record<string, unknown>) => { |
| 156 | const toolInput = ensureRecord(rawArgs) |
| 157 | const innerToolUseID = randomUUID() |
| 158 | |
| 159 | const toolUse: ToolUseBlock = { |
| 160 | type: 'tool_use', |
| 161 | id: innerToolUseID, |
| 162 | name: toolName, |
| 163 | input: toolInput, |
| 164 | } |
| 165 | |
| 166 | const assistantMessage = createAssistantMessage({ |
| 167 | content: [toolUse], |
| 168 | isVirtual: true, |
| 169 | }) as AssistantMessage |
| 170 | |
| 171 | runtime.pushMessage(assistantMessage) |
| 172 | runtime.onProgress?.({ |
| 173 | toolUseID: runtime.outerToolUseID, |
| 174 | data: { |
| 175 | type: 'repl_tool_call', |
| 176 | phase: 'start', |
| 177 | toolName, |
| 178 | toolInput, |
| 179 | }, |
| 180 | }) |
| 181 | |
| 182 | const innerContext: ToolUseContext = { |
| 183 | ...runtime.toolUseContext, |
| 184 | getAppState: createNestedGetAppState( |
| 185 | runtime.toolUseContext.getAppState, |
| 186 | runtime.availableTools, |
| 187 | ), |
| 188 | options: { |
| 189 | ...runtime.toolUseContext.options, |
| 190 | tools: runtime.availableTools, |
| 191 | }, |
| 192 | } |
| 193 | |
| 194 | let sawToolResult = false |
| 195 | let toolResult: unknown |
| 196 | let toolError: string | undefined |
| 197 | |
| 198 | for await (const update of runToolUse( |
| 199 | toolUse, |
| 200 | assistantMessage, |
| 201 | runtime.canUseTool, |
| 202 | innerContext, |
| 203 | )) { |
| 204 | if (update.contextModifier) { |
| 205 | runtime.pushContextModifier(update.contextModifier.modifyContext) |
| 206 | } |
| 207 | if (update.message.type === 'progress') { |
| 208 | continue |
no test coverage detected