({
hook,
messages,
hookName,
toolUseID,
hookEvent,
timeoutMs,
signal,
}: {
hook: FunctionHook
messages: Message[]
hookName: string
toolUseID: string
hookEvent: HookEvent
timeoutMs: number
signal?: AbortSignal
})
| 4738 | } |
| 4739 | |
| 4740 | async function executeFunctionHook({ |
| 4741 | hook, |
| 4742 | messages, |
| 4743 | hookName, |
| 4744 | toolUseID, |
| 4745 | hookEvent, |
| 4746 | timeoutMs, |
| 4747 | signal, |
| 4748 | }: { |
| 4749 | hook: FunctionHook |
| 4750 | messages: Message[] |
| 4751 | hookName: string |
| 4752 | toolUseID: string |
| 4753 | hookEvent: HookEvent |
| 4754 | timeoutMs: number |
| 4755 | signal?: AbortSignal |
| 4756 | }): Promise<HookResult> { |
| 4757 | const callbackTimeoutMs = hook.timeout ?? timeoutMs |
| 4758 | const { signal: abortSignal, cleanup } = createCombinedAbortSignal(signal, { |
| 4759 | timeoutMs: callbackTimeoutMs, |
| 4760 | }) |
| 4761 | |
| 4762 | try { |
| 4763 | // Check if already aborted |
| 4764 | if (abortSignal.aborted) { |
| 4765 | cleanup() |
| 4766 | return { |
| 4767 | outcome: 'cancelled', |
| 4768 | hook, |
| 4769 | } |
| 4770 | } |
| 4771 | |
| 4772 | // Execute callback with abort signal |
| 4773 | const passed = await new Promise<boolean>((resolve, reject) => { |
| 4774 | // Handle abort signal |
| 4775 | const onAbort = () => reject(new Error('Function hook cancelled')) |
| 4776 | abortSignal.addEventListener('abort', onAbort) |
| 4777 | |
| 4778 | // Execute callback |
| 4779 | Promise.resolve(hook.callback(messages, abortSignal)) |
| 4780 | .then(result => { |
| 4781 | abortSignal.removeEventListener('abort', onAbort) |
| 4782 | resolve(result) |
| 4783 | }) |
| 4784 | .catch(error => { |
| 4785 | abortSignal.removeEventListener('abort', onAbort) |
| 4786 | reject(error) |
| 4787 | }) |
| 4788 | }) |
| 4789 | |
| 4790 | cleanup() |
| 4791 | |
| 4792 | if (passed) { |
| 4793 | return { |
| 4794 | outcome: 'success', |
| 4795 | hook, |
| 4796 | } |
| 4797 | } |
no test coverage detected