Skip to content

Commit 956beef

Browse files
committed
adapt legacy start interceptors
1 parent 8d972ca commit 956beef

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
@@ -529,9 +529,40 @@ export class WorkflowClient extends BaseClient {
529529
assertRequiredWorkflowOptions(options);
530530
const compiledOptions = compileWorkflowOptions(ensureArgs(options));
531531

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

536567
const startWithDetails = composeInterceptors(
537568
adaptedInterceptors,

0 commit comments

Comments
 (0)