( orphanedPermission: OrphanedPermission, tools: Tools, mutableMessages: Message[], processUserInputContext: ProcessUserInputContext, )
| 245 | } |
| 246 | |
| 247 | export async function* handleOrphanedPermission( |
| 248 | orphanedPermission: OrphanedPermission, |
| 249 | tools: Tools, |
| 250 | mutableMessages: Message[], |
| 251 | processUserInputContext: ProcessUserInputContext, |
| 252 | ): AsyncGenerator<SDKMessage, void, unknown> { |
| 253 | const persistSession = !isSessionPersistenceDisabled() |
| 254 | const { permissionResult, assistantMessage } = orphanedPermission |
| 255 | const toolUseID = (permissionResult as { toolUseID?: string }).toolUseID |
| 256 | |
| 257 | if (!toolUseID) { |
| 258 | return |
| 259 | } |
| 260 | |
| 261 | const content = assistantMessage.message.content |
| 262 | let toolUseBlock: ToolUseBlock | undefined |
| 263 | if (Array.isArray(content)) { |
| 264 | for (const block of content) { |
| 265 | if (block.type === 'tool_use' && block.id === toolUseID) { |
| 266 | toolUseBlock = block as ToolUseBlock |
| 267 | break |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (!toolUseBlock) { |
| 273 | return |
| 274 | } |
| 275 | |
| 276 | const toolName = toolUseBlock.name |
| 277 | const toolInput = toolUseBlock.input |
| 278 | |
| 279 | const toolDefinition = findToolByName(tools, toolName) |
| 280 | if (!toolDefinition) { |
| 281 | return |
| 282 | } |
| 283 | |
| 284 | // Create ToolUseBlock with the updated input if permission was allowed |
| 285 | let finalInput = toolInput |
| 286 | if (permissionResult.behavior === 'allow') { |
| 287 | const allowResult = permissionResult as { |
| 288 | behavior: 'allow' |
| 289 | updatedInput?: unknown |
| 290 | } |
| 291 | if (allowResult.updatedInput !== undefined) { |
| 292 | finalInput = allowResult.updatedInput |
| 293 | } else { |
| 294 | logForDebugging( |
| 295 | `Orphaned permission for ${toolName}: updatedInput is undefined, falling back to original tool input`, |
| 296 | { level: 'warn' }, |
| 297 | ) |
| 298 | } |
| 299 | } |
| 300 | const finalToolUseBlock: ToolUseBlock = { |
| 301 | ...toolUseBlock, |
| 302 | input: finalInput, |
| 303 | } |
| 304 |
no test coverage detected