({
hook,
messages,
hookName,
toolUseID,
hookEvent,
timeoutMs,
signal,
}: {
hook: FunctionHook
messages: Message[]
hookName: string
toolUseID: string
hookEvent: HookEvent
timeoutMs: number
signal?: AbortSignal
})
| 4837 | } |
| 4838 | |
| 4839 | async function executeFunctionHook({ |
| 4840 | hook, |
| 4841 | messages, |
| 4842 | hookName, |
| 4843 | toolUseID, |
| 4844 | hookEvent, |
| 4845 | timeoutMs, |
| 4846 | signal, |
| 4847 | }: { |
| 4848 | hook: FunctionHook |
| 4849 | messages: Message[] |
| 4850 | hookName: string |
| 4851 | toolUseID: string |
| 4852 | hookEvent: HookEvent |
| 4853 | timeoutMs: number |
| 4854 | signal?: AbortSignal |
| 4855 | }): Promise<HookResult> { |
| 4856 | const callbackTimeoutMs = hook.timeout ?? timeoutMs |
| 4857 | const { signal: abortSignal, cleanup } = createCombinedAbortSignal(signal, { |
| 4858 | timeoutMs: callbackTimeoutMs, |
| 4859 | }) |
| 4860 | |
| 4861 | try { |
| 4862 | // Check if already aborted |
| 4863 | if (abortSignal.aborted) { |
| 4864 | cleanup() |
| 4865 | return { |
| 4866 | outcome: 'cancelled', |
| 4867 | hook, |
| 4868 | } |
| 4869 | } |
| 4870 | |
| 4871 | // Execute callback with abort signal |
| 4872 | const passed = await new Promise<boolean>((resolve, reject) => { |
| 4873 | // Handle abort signal |
| 4874 | const onAbort = () => reject(new Error('Function hook cancelled')) |
| 4875 | abortSignal.addEventListener('abort', onAbort) |
| 4876 | |
| 4877 | // Execute callback |
| 4878 | Promise.resolve(hook.callback(messages, abortSignal)) |
| 4879 | .then(result => { |
| 4880 | abortSignal.removeEventListener('abort', onAbort) |
| 4881 | resolve(result) |
| 4882 | }) |
| 4883 | .catch(error => { |
| 4884 | abortSignal.removeEventListener('abort', onAbort) |
| 4885 | reject(error) |
| 4886 | }) |
| 4887 | }) |
| 4888 | |
| 4889 | cleanup() |
| 4890 | |
| 4891 | if (passed) { |
| 4892 | return { |
| 4893 | outcome: 'success', |
| 4894 | hook, |
| 4895 | } |
| 4896 | } |
no test coverage detected