@@ -61,12 +61,15 @@ import {
6161 WorkflowStartUpdateOutput ,
6262 WorkflowStartUpdateWithStartInput ,
6363 WorkflowStartUpdateWithStartOutput ,
64+ WorkflowStartOutput ,
6465} from './interceptors' ;
6566import {
6667 CountWorkflowExecution ,
6768 DescribeWorkflowExecutionResponse ,
6869 encodeQueryRejectCondition ,
6970 GetWorkflowExecutionHistoryRequest ,
71+ InternalConnectionLike ,
72+ InternalConnectionLikeSymbol ,
7073 QueryRejectCondition ,
7174 RequestCancelWorkflowExecutionResponse ,
7275 StartWorkflowExecutionRequest ,
@@ -94,6 +97,7 @@ import {
9497import { mapAsyncIterable } from './iterators-utils' ;
9598import { WorkflowUpdateStage , encodeWorkflowUpdateStage } from './workflow-update-stage' ;
9699import { InternalWorkflowStartOptionsSymbol , InternalWorkflowStartOptions } from './internal' ;
100+ import { adaptWorkflowClientInterceptor } from './interceptor-adapters' ;
97101
98102const UpdateWorkflowExecutionLifecycleStage = temporal . api . enums . v1 . UpdateWorkflowExecutionLifecycleStage ;
99103
@@ -263,6 +267,15 @@ export interface WorkflowHandleWithFirstExecutionRunId<T extends Workflow = Work
263267 readonly firstExecutionRunId : string ;
264268}
265269
270+ /**
271+ * This interface is exactly the same as {@link WorkflowHandleWithFirstExecutionRunId} except it
272+ * includes the `eagerlyStarted` returned from {@link WorkflowClient.start}.
273+ */
274+ export interface WorkflowHandleWithStartDetails < T extends Workflow = Workflow >
275+ extends WorkflowHandleWithFirstExecutionRunId < T > {
276+ readonly eagerlyStarted : boolean ;
277+ }
278+
266279/**
267280 * This interface is exactly the same as {@link WorkflowHandle} except it
268281 * includes the `signaledRunId` returned from `signalWithStart`.
@@ -514,14 +527,19 @@ export class WorkflowClient extends BaseClient {
514527 workflowTypeOrFunc : string | T ,
515528 options : WorkflowStartOptions < T > ,
516529 interceptors : WorkflowClientInterceptor [ ]
517- ) : Promise < string > {
530+ ) : Promise < WorkflowStartOutput > {
518531 const workflowType = extractWorkflowType ( workflowTypeOrFunc ) ;
519532 assertRequiredWorkflowOptions ( options ) ;
520533 const compiledOptions = compileWorkflowOptions ( ensureArgs ( options ) ) ;
534+ const adaptedInterceptors = interceptors . map ( ( i ) => adaptWorkflowClientInterceptor ( i ) ) ;
521535
522- const start = composeInterceptors ( interceptors , 'start' , this . _startWorkflowHandler . bind ( this ) ) ;
536+ const startWithDetails = composeInterceptors (
537+ adaptedInterceptors ,
538+ 'startWithDetails' ,
539+ this . _startWorkflowHandler . bind ( this )
540+ ) ;
523541
524- return start ( {
542+ return startWithDetails ( {
525543 options : compiledOptions ,
526544 headers : { } ,
527545 workflowType,
@@ -537,7 +555,6 @@ export class WorkflowClient extends BaseClient {
537555 const { signal, signalArgs, ...rest } = options ;
538556 assertRequiredWorkflowOptions ( rest ) ;
539557 const compiledOptions = compileWorkflowOptions ( ensureArgs ( rest ) ) ;
540-
541558 const signalWithStart = composeInterceptors (
542559 interceptors ,
543560 'signalWithStart' ,
@@ -561,22 +578,25 @@ export class WorkflowClient extends BaseClient {
561578 public async start < T extends Workflow > (
562579 workflowTypeOrFunc : string | T ,
563580 options : WorkflowStartOptions < T >
564- ) : Promise < WorkflowHandleWithFirstExecutionRunId < T > > {
581+ ) : Promise < WorkflowHandleWithStartDetails < T > > {
565582 const { workflowId } = options ;
566583 const interceptors = this . getOrMakeInterceptors ( workflowId ) ;
567- const runId = await this . _start ( workflowTypeOrFunc , { ...options , workflowId } , interceptors ) ;
584+ const wfStartOutput = await this . _start ( workflowTypeOrFunc , { ...options , workflowId } , interceptors ) ;
568585 // runId is not used in handles created with `start*` calls because these
569586 // handles should allow interacting with the workflow if it continues as new.
570- const handle = this . _createWorkflowHandle ( {
587+ const baseHandle = this . _createWorkflowHandle ( {
571588 workflowId,
572589 runId : undefined ,
573- firstExecutionRunId : runId ,
574- runIdForResult : runId ,
590+ firstExecutionRunId : wfStartOutput . runId ,
591+ runIdForResult : wfStartOutput . runId ,
575592 interceptors,
576593 followRuns : options . followRuns ?? true ,
577- } ) as WorkflowHandleWithFirstExecutionRunId < T > ; // Cast is safe because we know we add the firstExecutionRunId below
578- ( handle as any ) /* readonly */ . firstExecutionRunId = runId ;
579- return handle ;
594+ } ) ;
595+ return {
596+ ...baseHandle ,
597+ firstExecutionRunId : wfStartOutput . runId ,
598+ eagerlyStarted : wfStartOutput . eagerlyStarted ,
599+ } ;
580600 }
581601
582602 /**
@@ -1246,7 +1266,7 @@ export class WorkflowClient extends BaseClient {
12461266 *
12471267 * Used as the final function of the start interceptor chain
12481268 */
1249- protected async _startWorkflowHandler ( input : WorkflowStartInput ) : Promise < string > {
1269+ protected async _startWorkflowHandler ( input : WorkflowStartInput ) : Promise < WorkflowStartOutput > {
12501270 const req = await this . createStartWorkflowRequest ( input ) ;
12511271 const { options : opts , workflowType } = input ;
12521272 const internalOptions = ( opts as InternalWorkflowStartOptions ) [ InternalWorkflowStartOptionsSymbol ] ;
@@ -1255,7 +1275,10 @@ export class WorkflowClient extends BaseClient {
12551275 if ( internalOptions != null ) {
12561276 internalOptions . backLink = response . link ?? undefined ;
12571277 }
1258- return response . runId ;
1278+ return {
1279+ runId : response . runId ,
1280+ eagerlyStarted : response . eagerWorkflowTask != null ,
1281+ } ;
12591282 } catch ( err : any ) {
12601283 if ( err . code === grpcStatus . ALREADY_EXISTS ) {
12611284 throw new WorkflowExecutionAlreadyStartedError (
@@ -1272,6 +1295,15 @@ export class WorkflowClient extends BaseClient {
12721295 const { options : opts , workflowType, headers } = input ;
12731296 const { identity, namespace } = this . options ;
12741297 const internalOptions = ( opts as InternalWorkflowStartOptions ) [ InternalWorkflowStartOptionsSymbol ] ;
1298+ const supportsEagerStart = ( this . connection as InternalConnectionLike ) ?. [ InternalConnectionLikeSymbol ]
1299+ ?. supportsEagerStart ;
1300+
1301+ if ( opts . requestEagerStart && ! supportsEagerStart ) {
1302+ throw new Error (
1303+ 'Eager workflow start requires a NativeConnection shared between client and worker. ' +
1304+ 'Pass a NativeConnection via ClientOptions.connection, or disable requestEagerStart.'
1305+ ) ;
1306+ }
12751307
12761308 return {
12771309 namespace,
@@ -1303,6 +1335,7 @@ export class WorkflowClient extends BaseClient {
13031335 userMetadata : await encodeUserMetadata ( this . dataConverter , opts . staticSummary , opts . staticDetails ) ,
13041336 priority : opts . priority ? compilePriority ( opts . priority ) : undefined ,
13051337 versioningOverride : opts . versioningOverride ?? undefined ,
1338+ requestEagerExecution : opts . requestEagerStart ,
13061339 ...filterNullAndUndefined ( internalOptions ?? { } ) ,
13071340 } ;
13081341 }
0 commit comments