Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import type { MaybePromise, PiHookNext } from './types'; |
| 2 | |
| 3 | export const runHook = async <TResult, TContext>( |
| 4 | hook: ((ctx: TContext, next: PiHookNext<TResult>) => MaybePromise<unknown>) | undefined, |
| 5 | context: TContext, |
| 6 | getDefaultResult: () => MaybePromise<TResult> |
| 7 | ): Promise<TResult | unknown> => { |
| 8 | if (!hook) return getDefaultResult(); |
| 9 | |
| 10 | const state: { nextResult?: Promise<TResult> } = {}; |
| 11 | const next = async (): Promise<TResult> => { |
| 12 | state.nextResult ??= Promise.resolve(getDefaultResult()); |
| 13 | return state.nextResult; |
| 14 | }; |
| 15 | |
| 16 | const hookValue = await hook(context, next); |
| 17 | if (hookValue !== undefined) return hookValue; |
| 18 | return state.nextResult ?? next(); |
| 19 | }; |