@@ -17,7 +17,9 @@ export type BoundTaskFlow = ReturnType<
1717> ;
1818
1919type FlowRecord = NonNullable < ReturnType < BoundTaskFlow [ "tryCreateManaged" ] > > ;
20- type MutationResult = ReturnType < BoundTaskFlow [ "setWaiting" ] > ;
20+ type MutationResult =
21+ | ReturnType < BoundTaskFlow [ "setWaiting" ] >
22+ | Awaited < ReturnType < BoundTaskFlow [ "cancel" ] > > ;
2123
2224type LobsterApprovalWaitState = {
2325 kind : "lobster_approval" ;
@@ -29,6 +31,7 @@ type LobsterApprovalWaitState = {
2931
3032type RunManagedLobsterFlowParams = {
3133 taskFlow : BoundTaskFlow ;
34+ config : OpenClawPluginApi [ "config" ] ;
3235 runner : LobsterRunner ;
3336 runnerParams : LobsterRunnerParams ;
3437 controllerId : string ;
@@ -40,6 +43,7 @@ type RunManagedLobsterFlowParams = {
4043
4144type ResumeManagedLobsterFlowParams = {
4245 taskFlow : BoundTaskFlow ;
46+ config : OpenClawPluginApi [ "config" ] ;
4347 runner : LobsterRunner ;
4448 runnerParams : LobsterRunnerParams & {
4549 action : "resume" ;
@@ -117,46 +121,45 @@ function buildApprovalWaitState(envelope: Extract<LobsterEnvelope, { ok: true }>
117121 } satisfies LobsterApprovalWaitState ;
118122}
119123
120- function applyEnvelopeToFlow ( params : {
121- taskFlow : BoundTaskFlow ;
122- flow : FlowRecord ;
123- envelope : LobsterEnvelope ;
124- waitingStep : string ;
125- } ) : MutationResult {
126- const { taskFlow, flow, envelope, waitingStep } = params ;
127- const flowMutation = { flowId : flow . flowId , expectedRevision : flow . revision } ;
128-
129- if ( ! envelope . ok ) {
130- return taskFlow . fail ( flowMutation ) ;
131- }
132-
133- if ( envelope . status === "needs_approval" ) {
134- return taskFlow . setWaiting ( {
135- ...flowMutation ,
136- currentStep : waitingStep ,
137- waitJson : buildApprovalWaitState ( envelope ) ,
138- } ) ;
139- }
140-
141- return taskFlow . finish ( flowMutation ) ;
142- }
143-
144124async function executeManagedLobsterFlow (
145- params : Pick < RunManagedLobsterFlowParams , "taskFlow" | "runner" | "runnerParams" | "waitingStep" > ,
125+ params : Pick <
126+ RunManagedLobsterFlowParams ,
127+ "taskFlow" | "config" | "runner" | "runnerParams" | "waitingStep"
128+ > ,
146129 flow : FlowRecord ,
147130 failureFlowId = flow . flowId ,
148131) : Promise < ManagedLobsterFlowResult > {
149132 try {
150133 const envelope = await params . runner . run ( params . runnerParams ) ;
151- const mutation = applyEnvelopeToFlow ( {
152- taskFlow : params . taskFlow ,
153- flow,
154- envelope,
155- waitingStep : params . waitingStep ?? "await_lobster_approval" ,
156- } ) ;
134+ if ( envelope . ok && envelope . status === "cancelled" ) {
135+ try {
136+ const mutation = await params . taskFlow . cancel ( { flowId : flow . flowId , cfg : params . config } ) ;
137+ return mutation . cancelled
138+ ? { ok : true , envelope, flow, mutation }
139+ : {
140+ ok : false ,
141+ flow,
142+ mutation,
143+ error : new Error ( `TaskFlow cancellation failed: ${ mutation . reason ?? "unknown" } ` ) ,
144+ } ;
145+ } catch ( error ) {
146+ const err = error instanceof Error ? error : new Error ( String ( error ) ) ;
147+ return { ok : false , flow, error : err } ;
148+ }
149+ }
150+ const flowMutation = { flowId : flow . flowId , expectedRevision : flow . revision } ;
157151 if ( ! envelope . ok ) {
152+ const mutation = params . taskFlow . fail ( flowMutation ) ;
158153 return { ok : false , flow, mutation, error : new Error ( envelope . error . message ) } ;
159154 }
155+ const mutation =
156+ envelope . status === "needs_approval"
157+ ? params . taskFlow . setWaiting ( {
158+ ...flowMutation ,
159+ currentStep : params . waitingStep ?? "await_lobster_approval" ,
160+ waitJson : buildApprovalWaitState ( envelope ) ,
161+ } )
162+ : params . taskFlow . finish ( flowMutation ) ;
160163 return { ok : true , envelope, flow, mutation } ;
161164 } catch ( error ) {
162165 const err = error instanceof Error ? error : new Error ( String ( error ) ) ;
0 commit comments