Skip to content

Commit 9961140

Browse files
Fix hydration of events with dates and of step functions (#380)
1 parent 73b6c68 commit 9961140

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

.changeset/eight-clowns-own.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@workflow/web-shared": patch
3+
"@workflow/core": patch
4+
---
5+
6+
Fix hydration of eventData for sleep calls

packages/core/src/observability.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const streamPrintRevivers: Record<string, (value: any) => any> = {
4141
ReadableStream: streamToStreamId,
4242
WritableStream: streamToStreamId,
4343
TransformStream: streamToStreamId,
44+
StepFunction: (value) => `<fn:${value}>`,
4445
};
4546

4647
const hydrateStepIO = <
@@ -101,22 +102,24 @@ const hydrateEventData = <
101102
if (!event.eventData) {
102103
return event;
103104
}
105+
const eventData = { ...event.eventData };
106+
// Events can have various eventData with non-devalued keys.
107+
// So far, only eventData.result is devalued (though this may change),
108+
// so we need to hydrate it specifically.
109+
try {
110+
if ('result' in eventData && typeof eventData.result === 'object') {
111+
eventData.result = hydrateStepReturnValue(
112+
eventData.result,
113+
globalThis,
114+
streamPrintRevivers
115+
);
116+
}
117+
} catch (error) {
118+
console.error('Error hydrating event data', error);
119+
}
104120
return {
105121
...event,
106-
// Events have various top-level non-devalued keys, so we need to
107-
// hydrate each value individually.
108-
eventData: Object.fromEntries(
109-
Object.entries(event.eventData).map(([key, value]) => [
110-
key,
111-
hydrateStepArguments(
112-
value as any,
113-
[],
114-
event.runId as string,
115-
globalThis,
116-
streamPrintRevivers
117-
),
118-
])
119-
),
122+
eventData,
120123
};
121124
};
122125

packages/web-shared/src/api/workflow-server-actions.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,15 @@ function getUserFacingMessage(error: Error): string {
134134
return error.message || 'An unexpected error occurred';
135135
}
136136

137+
const toJSONCompatible = <T>(data: T): T => {
138+
if (data && typeof data === 'object') {
139+
return JSON.parse(JSON.stringify(data)) as T;
140+
}
141+
return data;
142+
};
143+
137144
const hydrate = <T>(data: T): T => {
145+
data = toJSONCompatible(data);
138146
try {
139147
return hydrateResourceIO(data as any) as T;
140148
} catch (error) {
@@ -148,16 +156,7 @@ const hydrate = <T>(data: T): T => {
148156
* @returns ServerActionResult with success=true and the data
149157
*/
150158
function createResponse<T>(data: T): ServerActionResult<T> {
151-
if (data && typeof data === 'object') {
152-
// We can't pass non-serializable objects from client to server. To ensure
153-
// we don't accidentally do this, we convert the object to JSON.
154-
try {
155-
data = JSON.parse(JSON.stringify(data)) as T;
156-
} catch {
157-
// If we can't serialize the data, we'll leave it to the API layer to
158-
// throw an error if the data isn't transportable.
159-
}
160-
}
159+
data = toJSONCompatible(data);
161160
return {
162161
success: true,
163162
data,

workbench/nitro-v2/server/api/trigger.post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineEventHandler, getRequestURL, readRawBody } from 'h3';
22
import { start } from 'workflow/api';
33
import { hydrateWorkflowArguments } from 'workflow/internal/serialization';
4-
import { allWorkflows } from '../_workflows.js';
4+
import { allWorkflows } from '../../_workflows.js';
55

66
export default defineEventHandler(async (event) => {
77
const url = getRequestURL(event);

0 commit comments

Comments
 (0)