@@ -61,6 +61,7 @@ import {
6161 WorkflowStartUpdateOutput ,
6262 WorkflowStartUpdateWithStartInput ,
6363 WorkflowStartUpdateWithStartOutput ,
64+ WorkflowStartOutput ,
6465} from './interceptors' ;
6566import {
6667 CountWorkflowExecution ,
@@ -263,6 +264,15 @@ export interface WorkflowHandleWithFirstExecutionRunId<T extends Workflow = Work
263264 readonly firstExecutionRunId : string ;
264265}
265266
267+ /**
268+ * This interface is exactly the same as {@link WorkflowHandleWithFirstExecutionRunId} except it
269+ * includes the `eagerlyStarted` returned from {@link WorkflowClient.start}.
270+ */
271+ export interface WorkflowHandleWithStartDetails < T extends Workflow = Workflow >
272+ extends WorkflowHandleWithFirstExecutionRunId < T > {
273+ readonly eagerlyStarted : boolean ;
274+ }
275+
266276/**
267277 * This interface is exactly the same as {@link WorkflowHandle} except it
268278 * includes the `signaledRunId` returned from `signalWithStart`.
@@ -514,14 +524,22 @@ export class WorkflowClient extends BaseClient {
514524 workflowTypeOrFunc : string | T ,
515525 options : WorkflowStartOptions < T > ,
516526 interceptors : WorkflowClientInterceptor [ ]
517- ) : Promise < string > {
527+ ) : Promise < WorkflowStartOutput > {
518528 const workflowType = extractWorkflowType ( workflowTypeOrFunc ) ;
519529 assertRequiredWorkflowOptions ( options ) ;
520530 const compiledOptions = compileWorkflowOptions ( ensureArgs ( options ) ) ;
521531
522- const start = composeInterceptors ( interceptors , 'start' , this . _startWorkflowHandler . bind ( this ) ) ;
532+ const adaptedInterceptors : WorkflowClientInterceptor [ ] = interceptors . map ( ( i ) =>
533+ i . startWithDetails ? i : { ...i , startWithDetails : ( input , next ) => next ( input ) }
534+ ) ;
535+
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 (
@@ -1273,6 +1296,13 @@ export class WorkflowClient extends BaseClient {
12731296 const { identity, namespace } = this . options ;
12741297 const internalOptions = ( opts as InternalWorkflowStartOptions ) [ InternalWorkflowStartOptionsSymbol ] ;
12751298
1299+ if ( opts . requestEagerStart && ! ( 'supportsEagerStart' in this . connection && this . connection . supportsEagerStart ) ) {
1300+ throw new Error (
1301+ 'Eager workflow start requires a NativeConnection shared between client and worker. ' +
1302+ 'Pass a NativeConnection via ClientOptions.connection, or disable requestEagerStart.'
1303+ ) ;
1304+ }
1305+
12761306 return {
12771307 namespace,
12781308 identity,
@@ -1303,6 +1333,7 @@ export class WorkflowClient extends BaseClient {
13031333 userMetadata : await encodeUserMetadata ( this . dataConverter , opts . staticSummary , opts . staticDetails ) ,
13041334 priority : opts . priority ? compilePriority ( opts . priority ) : undefined ,
13051335 versioningOverride : opts . versioningOverride ?? undefined ,
1336+ requestEagerExecution : opts . requestEagerStart ,
13061337 ...filterNullAndUndefined ( internalOptions ?? { } ) ,
13071338 } ;
13081339 }
0 commit comments