@@ -11,7 +11,10 @@ import type {
1111 WorkflowsWorkflow ,
1212} from "../generated" ;
1313import type { zWorkflowsListInstancesData } from "../generated/zod.gen" ;
14- import type { RestartFromStep } from "@cloudflare/workflows-shared/src/binding" ;
14+ import type {
15+ RestartFromStep ,
16+ WorkflowInstanceTerminateOptions ,
17+ } from "@cloudflare/workflows-shared/src/binding" ;
1518import type { z } from "zod" ;
1619
1720// ============================================================================
@@ -35,11 +38,17 @@ interface WorkflowHandle {
3538 pause ( ) : Promise < void > ;
3639 resume ( ) : Promise < void > ;
3740 restart ( options ?: { from ?: RestartFromStep } ) : Promise < void > ;
38- terminate ( ) : Promise < void > ;
41+ terminate ( options ?: WorkflowInstanceTerminateOptions ) : Promise < void > ;
3942 sendEvent ( args : { payload : unknown ; type : string } ) : Promise < void > ;
4043 status ( ) : Promise < { status : string ; output ?: unknown ; error ?: unknown } > ;
4144}
4245
46+ /** Local Workflow proxy binding. Current workers-types may lag local features. */
47+ interface LocalWorkflowBinding {
48+ create ( options ?: { id ?: string ; params ?: unknown } ) : Promise < { id : string } > ;
49+ get ( id : string ) : Promise < WorkflowHandle > ;
50+ }
51+
4352/** RPC methods exposed by the Engine Durable Object. */
4453interface EngineStub {
4554 getInstanceMetadata ( ) : Promise < {
@@ -173,12 +182,15 @@ function getEngineNamespace(
173182 * Get the Workflow proxy binding for a workflow.
174183 * Used for operations that go through the standard Workflow interface (create).
175184 */
176- function getWorkflowBinding ( env : Env , workflowName : string ) : Workflow | null {
185+ function getWorkflowBinding (
186+ env : Env ,
187+ workflowName : string
188+ ) : LocalWorkflowBinding | null {
177189 const info = env . LOCAL_EXPLORER_BINDING_MAP . workflows [ workflowName ] ;
178190 if ( ! info ) {
179191 return null ;
180192 }
181- return env [ info . binding ] as Workflow ;
193+ return env [ info . binding ] as unknown as LocalWorkflowBinding ;
182194}
183195
184196function getLocalWorkflows ( env : Env ) : WorkflowsWorkflow [ ] {
@@ -886,6 +898,14 @@ export async function changeWorkflowInstanceStatus(
886898 ) ;
887899 }
888900
901+ if ( body . rollback !== undefined && action !== "terminate" ) {
902+ return errorResponse (
903+ 400 ,
904+ 10001 ,
905+ "'rollback' is only valid when terminating."
906+ ) ;
907+ }
908+
889909 const handle = await workflow . get ( instanceId ) ;
890910
891911 switch ( action ) {
@@ -904,12 +924,13 @@ export async function changeWorkflowInstanceStatus(
904924 ) ;
905925 }
906926 const opts = body . from ? { from : body . from } : undefined ;
907- // TODO(vaish): remove cast once @cloudflare/workers-types ships restart options
908- await ( handle as unknown as WorkflowHandle ) . restart ( opts ) ;
927+ await handle . restart ( opts ) ;
909928 break ;
910929 }
911930 case "terminate" :
912- await handle . terminate ( ) ;
931+ await handle . terminate (
932+ body . rollback === true ? { rollback : true } : undefined
933+ ) ;
913934 break ;
914935 }
915936
@@ -1044,9 +1065,7 @@ export async function sendWorkflowInstanceEvent(
10441065 // Empty body is fine — payload is optional
10451066 }
10461067
1047- const handle = ( await workflow . get (
1048- instanceId
1049- ) ) as unknown as WorkflowHandle ;
1068+ const handle = await workflow . get ( instanceId ) ;
10501069 await handle . sendEvent ( { payload, type : eventType } ) ;
10511070
10521071 return c . json ( wrapResponse ( { success : true } ) ) ;
0 commit comments