* Execute PermissionRequest hooks and return a decision if one is made. * Returns undefined if no hook made a decision.
( toolName: string, toolUseID: string, input: Record<string, unknown>, toolUseContext: ToolUseContext, suggestions: PermissionUpdate[] | undefined, )
| 777 | * Returns undefined if no hook made a decision. |
| 778 | */ |
| 779 | async function executePermissionRequestHooksForSDK( |
| 780 | toolName: string, |
| 781 | toolUseID: string, |
| 782 | input: Record<string, unknown>, |
| 783 | toolUseContext: ToolUseContext, |
| 784 | suggestions: PermissionUpdate[] | undefined, |
| 785 | ): Promise<PermissionDecision | undefined> { |
| 786 | const appState = toolUseContext.getAppState() |
| 787 | const permissionMode = appState.toolPermissionContext.mode |
| 788 | |
| 789 | // Iterate directly over the generator instead of using `all` |
| 790 | const hookGenerator = executePermissionRequestHooks( |
| 791 | toolName, |
| 792 | toolUseID, |
| 793 | input, |
| 794 | toolUseContext, |
| 795 | permissionMode, |
| 796 | suggestions, |
| 797 | toolUseContext.abortController.signal, |
| 798 | ) |
| 799 | |
| 800 | for await (const hookResult of hookGenerator) { |
| 801 | if ( |
| 802 | hookResult.permissionRequestResult && |
| 803 | (hookResult.permissionRequestResult.behavior === 'allow' || |
| 804 | hookResult.permissionRequestResult.behavior === 'deny') |
| 805 | ) { |
| 806 | const decision = hookResult.permissionRequestResult |
| 807 | if (decision.behavior === 'allow') { |
| 808 | const finalInput = decision.updatedInput || input |
| 809 | |
| 810 | // Apply permission updates if provided by hook ("always allow") |
| 811 | const permissionUpdates = decision.updatedPermissions ?? [] |
| 812 | if (permissionUpdates.length > 0) { |
| 813 | persistPermissionUpdates(permissionUpdates) |
| 814 | const currentAppState = toolUseContext.getAppState() |
| 815 | const updatedContext = applyPermissionUpdates( |
| 816 | currentAppState.toolPermissionContext, |
| 817 | permissionUpdates, |
| 818 | ) |
| 819 | // Update permission context via setAppState |
| 820 | toolUseContext.setAppState(prev => { |
| 821 | if (prev.toolPermissionContext === updatedContext) return prev |
| 822 | return { ...prev, toolPermissionContext: updatedContext } |
| 823 | }) |
| 824 | } |
| 825 | |
| 826 | return { |
| 827 | behavior: 'allow', |
| 828 | updatedInput: finalInput, |
| 829 | userModified: false, |
| 830 | decisionReason: { |
| 831 | type: 'hook', |
| 832 | hookName: 'PermissionRequest', |
| 833 | }, |
| 834 | } |
| 835 | } else { |
| 836 | // Hook denied the permission |
no test coverage detected