Skip to content

Commit c92aab3

Browse files
committed
adapt legacy start interceptors
1 parent c92cbb1 commit c92aab3

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

packages/client/src/workflow-client.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,40 @@ export class WorkflowClient extends BaseClient {
526526
assertRequiredWorkflowOptions(options);
527527
const compiledOptions = compileWorkflowOptions(ensureArgs(options));
528528

529-
const adaptedInterceptors: WorkflowClientInterceptor[] = interceptors.map((i) =>
530-
i.startWithDetails ? i : { ...i, startWithDetails: (input, next) => next(input) }
531-
);
529+
// Adapt legacy `start` interceptors to the new `startWithDetails` interface.
530+
const adaptedInterceptors: WorkflowClientInterceptor[] = interceptors.map((i) => {
531+
// If it already has the new method, or doesn't have the legacy one, no adaptation is needed.
532+
if (i.startWithDetails || !i.start) {
533+
return i;
534+
}
535+
536+
// This interceptor has a legacy `start` but not `startWithDetails`. We'll adapt it.
537+
return {
538+
...i,
539+
startWithDetails: async (input, next): Promise<WorkflowStartOutput> => {
540+
let downstreamOut: WorkflowStartOutput | undefined;
541+
542+
// Patched `next` for legacy `start` interceptors.
543+
// Captures the full `WorkflowStartOutput` while returning `runId` as a string.
544+
const patchedNext = async (patchedInput: WorkflowStartInput): Promise<string> => {
545+
downstreamOut = await next(patchedInput);
546+
return downstreamOut.runId;
547+
};
548+
549+
const runIdFromLegacyInterceptor = await i.start!(input, patchedNext);
550+
551+
// If the interceptor short-circuited (didn't call `next`), `downstreamOut` will be undefined.
552+
// In that case, we can't have an eager start.
553+
if (downstreamOut === undefined) {
554+
return { runId: runIdFromLegacyInterceptor, eagerlyStarted: false };
555+
}
556+
557+
// If `next` was called, honor the `runId` from the legacy interceptor but preserve
558+
// the `eagerlyStarted` status from the actual downstream call.
559+
return { ...downstreamOut, runId: runIdFromLegacyInterceptor };
560+
},
561+
};
562+
});
532563

533564
const startWithDetails = composeInterceptors(
534565
adaptedInterceptors,

0 commit comments

Comments
 (0)