diff --git a/.changeset/new-crabs-hope.md b/.changeset/new-crabs-hope.md new file mode 100644 index 000000000..3ab8e5828 --- /dev/null +++ b/.changeset/new-crabs-hope.md @@ -0,0 +1,5 @@ +--- +"@workflow/core": patch +--- + +Make `resumeHook()` accept a `Hook` object or string diff --git a/packages/core/src/runtime/resume-hook.ts b/packages/core/src/runtime/resume-hook.ts index cf0db83cf..b2021ec05 100644 --- a/packages/core/src/runtime/resume-hook.ts +++ b/packages/core/src/runtime/resume-hook.ts @@ -33,7 +33,7 @@ export async function getHookByToken(token: string): Promise { * This function is called externally (e.g., from an API route or server action) * to send data to a hook and resume the associated workflow run. * - * @param token - The unique token identifying the hook + * @param tokenOrHook - The unique token identifying the hook, or the hook object itself * @param payload - The data payload to send to the hook * @returns Promise resolving to the hook * @throws Error if the hook is not found or if there's an error during the process @@ -57,7 +57,7 @@ export async function getHookByToken(token: string): Promise { * ``` */ export async function resumeHook( - token: string, + tokenOrHook: string | Hook, payload: T ): Promise { return await waitedUntil(() => { @@ -65,10 +65,13 @@ export async function resumeHook( const world = getWorld(); try { - const hook = await getHookByToken(token); + const hook = + typeof tokenOrHook === 'string' + ? await getHookByToken(tokenOrHook) + : tokenOrHook; span?.setAttributes({ - ...Attribute.HookToken(token), + ...Attribute.HookToken(hook.token), ...Attribute.HookId(hook.hookId), ...Attribute.WorkflowRunId(hook.runId), }); @@ -129,7 +132,9 @@ export async function resumeHook( return hook; } catch (err) { span?.setAttributes({ - ...Attribute.HookToken(token), + ...Attribute.HookToken( + typeof tokenOrHook === 'string' ? tokenOrHook : tokenOrHook.token + ), ...Attribute.HookFound(false), }); throw err; @@ -206,7 +211,7 @@ export async function resumeWebhook( response = new Response(null, { status: 202 }); } - await resumeHook(hook.token, request); + await resumeHook(hook, request); if (responseReadable) { // Wait for the readable stream to emit one chunk,