MCPcopy Create free account
hub / github.com/Noumena-Network/code / executeFunctionHook

Function executeFunctionHook

src/utils/hooks.ts:4744–4842  ·  view source on GitHub ↗
({
  hook,
  messages,
  hookName,
  toolUseID,
  hookEvent,
  timeoutMs,
  signal,
}: {
  hook: FunctionHook
  messages: Message[]
  hookName: string
  toolUseID: string
  hookEvent: HookEvent
  timeoutMs: number
  signal?: AbortSignal
})

Source from the content-addressed store, hash-verified

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

Callers 1

executeHooksFunction · 0.85

Calls 6

resolveMethod · 0.80
cleanupFunction · 0.70
resolveFunction · 0.70
logErrorFunction · 0.70
createAttachmentMessageFunction · 0.70

Tested by

no test coverage detected