({
hook,
messages,
hookName,
toolUseID,
hookEvent,
timeoutMs,
signal,
}: {
hook: FunctionHook
messages: Message[]
hookName: string
toolUseID: string
hookEvent: HookEvent
timeoutMs: number
signal?: AbortSignal
})
| 4906 | } |
| 4907 | |
| 4908 | async function executeFunctionHook({ |
| 4909 | hook, |
| 4910 | messages, |
| 4911 | hookName, |
| 4912 | toolUseID, |
| 4913 | hookEvent, |
| 4914 | timeoutMs, |
| 4915 | signal, |
| 4916 | }: { |
| 4917 | hook: FunctionHook |
| 4918 | messages: Message[] |
| 4919 | hookName: string |
| 4920 | toolUseID: string |
| 4921 | hookEvent: HookEvent |
| 4922 | timeoutMs: number |
| 4923 | signal?: AbortSignal |
| 4924 | }): Promise<HookResult> { |
| 4925 | const callbackTimeoutMs = hook.timeout ?? timeoutMs |
| 4926 | const { signal: abortSignal, cleanup } = createCombinedAbortSignal(signal, { |
| 4927 | timeoutMs: callbackTimeoutMs, |
| 4928 | }) |
| 4929 | |
| 4930 | try { |
| 4931 | // Check if already aborted |
| 4932 | if (abortSignal.aborted) { |
| 4933 | cleanup() |
| 4934 | return { |
| 4935 | outcome: 'cancelled', |
| 4936 | hook, |
| 4937 | } |
| 4938 | } |
| 4939 | |
| 4940 | // Execute callback with abort signal |
| 4941 | const passed = await new Promise<boolean>((resolve, reject) => { |
| 4942 | // Handle abort signal |
| 4943 | const onAbort = () => reject(new Error('Function hook cancelled')) |
| 4944 | abortSignal.addEventListener('abort', onAbort) |
| 4945 | |
| 4946 | // Execute callback |
| 4947 | Promise.resolve(hook.callback(messages, abortSignal)) |
| 4948 | .then(result => { |
| 4949 | abortSignal.removeEventListener('abort', onAbort) |
| 4950 | resolve(result) |
| 4951 | }) |
| 4952 | .catch(error => { |
| 4953 | abortSignal.removeEventListener('abort', onAbort) |
| 4954 | reject(error) |
| 4955 | }) |
| 4956 | }) |
| 4957 | |
| 4958 | cleanup() |
| 4959 | |
| 4960 | if (passed) { |
| 4961 | return { |
| 4962 | outcome: 'success', |
| 4963 | hook, |
| 4964 | } |
| 4965 | } |
no test coverage detected