-
Notifications
You must be signed in to change notification settings - Fork 161
Make resumeHook() accept a Hook object or string
#773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@workflow/core": patch | ||
| --- | ||
|
|
||
| Make `resumeHook()` accept a `Hook` object or string |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -33,7 +33,7 @@ export async function getHookByToken(token: string): Promise<Hook> { | |||||||||||||||||||||||||||||||
| * 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,18 +57,21 @@ export async function getHookByToken(token: string): Promise<Hook> { | |||||||||||||||||||||||||||||||
| * ``` | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| export async function resumeHook<T = any>( | ||||||||||||||||||||||||||||||||
| token: string, | ||||||||||||||||||||||||||||||||
| tokenOrHook: string | Hook, | ||||||||||||||||||||||||||||||||
| payload: T | ||||||||||||||||||||||||||||||||
| ): Promise<Hook> { | ||||||||||||||||||||||||||||||||
| return await waitedUntil(() => { | ||||||||||||||||||||||||||||||||
| return trace('HOOK.resume', async (span) => { | ||||||||||||||||||||||||||||||||
| const world = getWorld(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||
| const hook = await getHookByToken(token); | ||||||||||||||||||||||||||||||||
| const hook = | ||||||||||||||||||||||||||||||||
| typeof tokenOrHook === 'string' | ||||||||||||||||||||||||||||||||
| ? await getHookByToken(tokenOrHook) | ||||||||||||||||||||||||||||||||
| : tokenOrHook; | ||||||||||||||||||||||||||||||||
|
Comment on lines
59
to
+71
|
||||||||||||||||||||||||||||||||
| const hook = | |
| typeof tokenOrHook === 'string' | |
| ? await getHookByToken(tokenOrHook) | |
| : tokenOrHook; | |
| let hook: Hook; | |
| if (typeof tokenOrHook === 'string') { | |
| hook = await getHookByToken(tokenOrHook); | |
| } else { | |
| hook = tokenOrHook; | |
| // Hydrate the metadata if it was set from within the workflow run | |
| // (same as getHookByToken does for consistency) | |
| if (typeof hook.metadata !== 'undefined') { | |
| hook.metadata = hydrateStepArguments(hook.metadata as any, [], hook.runId); | |
| } | |
| } |
When a Hook object is passed directly to resumeHook(), its metadata is not hydrated via hydrateStepArguments() like it is when a token string is passed, causing inconsistent behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a Hook object is passed directly to resumeHook, it bypasses the metadata hydration logic that exists in getHookByToken(). The getHookByToken function hydrates the metadata property using hydrateStepArguments if it was set from within the workflow run. This could lead to bugs when the hook's metadata is used elsewhere in the workflow execution, as it would remain in its serialized/dehydrated form rather than being properly deserialized. Consider either: 1) documenting that callers must ensure the Hook object has already been processed through getHookByToken, or 2) adding metadata hydration logic here to handle Hook objects that haven't been hydrated yet.