( orphanedPermission: OrphanedPermission, tools: Tools, mutableMessages: Message[], processUserInputContext: ProcessUserInputContext, )
| 222 | } |
| 223 | |
| 224 | export async function* handleOrphanedPermission( |
| 225 | orphanedPermission: OrphanedPermission, |
| 226 | tools: Tools, |
| 227 | mutableMessages: Message[], |
| 228 | processUserInputContext: ProcessUserInputContext, |
| 229 | ): AsyncGenerator<SDKMessage, void, unknown> { |
| 230 | const persistSession = !isSessionPersistenceDisabled() |
| 231 | const { permissionResult, assistantMessage } = orphanedPermission |
| 232 | const { toolUseID } = permissionResult |
| 233 | |
| 234 | if (!toolUseID) { |
| 235 | return |
| 236 | } |
| 237 | |
| 238 | const content = assistantMessage.message.content |
| 239 | let toolUseBlock: ToolUseBlock | undefined |
| 240 | if (Array.isArray(content)) { |
| 241 | for (const block of content) { |
| 242 | if (block.type === 'tool_use' && block.id === toolUseID) { |
| 243 | toolUseBlock = block as ToolUseBlock |
| 244 | break |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if (!toolUseBlock) { |
| 250 | return |
| 251 | } |
| 252 | |
| 253 | const toolName = toolUseBlock.name |
| 254 | const toolInput = toolUseBlock.input |
| 255 | |
| 256 | const toolDefinition = findToolByName(tools, toolName) |
| 257 | if (!toolDefinition) { |
| 258 | return |
| 259 | } |
| 260 | |
| 261 | // Create ToolUseBlock with the updated input if permission was allowed |
| 262 | let finalInput = toolInput |
| 263 | if (permissionResult.behavior === 'allow') { |
| 264 | if (permissionResult.updatedInput !== undefined) { |
| 265 | finalInput = permissionResult.updatedInput |
| 266 | } else { |
| 267 | logForDebugging( |
| 268 | `Orphaned permission for ${toolName}: updatedInput is undefined, falling back to original tool input`, |
| 269 | { level: 'warn' }, |
| 270 | ) |
| 271 | } |
| 272 | } |
| 273 | const finalToolUseBlock: ToolUseBlock = { |
| 274 | ...toolUseBlock, |
| 275 | input: finalInput, |
| 276 | } |
| 277 | |
| 278 | const canUseTool: CanUseToolFn = async () => ({ |
| 279 | ...permissionResult, |
| 280 | decisionReason: { |
| 281 | type: 'mode', |
no test coverage detected