@openclaw/lobster 2026.3.13 → 2026.5.2-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/index.ts +22 -16
- package/openclaw.plugin.json +6 -0
- package/package.json +27 -3
- package/runtime-api.ts +12 -0
- package/src/lobster-ajv-cache.ts +142 -0
- package/src/lobster-core.d.ts +60 -0
- package/src/lobster-runner.test.ts +572 -0
- package/src/lobster-runner.ts +395 -0
- package/src/lobster-taskflow.test.ts +227 -0
- package/src/lobster-taskflow.ts +279 -0
- package/src/lobster-tool.test.ts +250 -208
- package/src/lobster-tool.ts +245 -191
- package/src/taskflow-test-helpers.ts +48 -0
- package/tsconfig.json +16 -0
- package/src/test-helpers.ts +0 -43
- package/src/windows-spawn.test.ts +0 -118
- package/src/windows-spawn.ts +0 -36
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import type { OpenClawPluginApi } from "../runtime-api.js";
|
|
2
|
+
import type { LobsterEnvelope, LobsterRunner, LobsterRunnerParams } from "./lobster-runner.js";
|
|
3
|
+
|
|
4
|
+
type JsonLike =
|
|
5
|
+
| null
|
|
6
|
+
| boolean
|
|
7
|
+
| number
|
|
8
|
+
| string
|
|
9
|
+
| JsonLike[]
|
|
10
|
+
| {
|
|
11
|
+
[key: string]: JsonLike;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type BoundTaskFlow = ReturnType<
|
|
15
|
+
NonNullable<OpenClawPluginApi["runtime"]>["tasks"]["managedFlows"]["bindSession"]
|
|
16
|
+
>;
|
|
17
|
+
|
|
18
|
+
type FlowRecord = ReturnType<BoundTaskFlow["createManaged"]>;
|
|
19
|
+
type MutationResult = ReturnType<BoundTaskFlow["setWaiting"]>;
|
|
20
|
+
|
|
21
|
+
type LobsterApprovalWaitState = {
|
|
22
|
+
kind: "lobster_approval";
|
|
23
|
+
prompt: string;
|
|
24
|
+
items: JsonLike[];
|
|
25
|
+
resumeToken?: string;
|
|
26
|
+
approvalId?: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type RunManagedLobsterFlowParams = {
|
|
30
|
+
taskFlow: BoundTaskFlow;
|
|
31
|
+
runner: LobsterRunner;
|
|
32
|
+
runnerParams: LobsterRunnerParams;
|
|
33
|
+
controllerId: string;
|
|
34
|
+
goal: string;
|
|
35
|
+
stateJson?: JsonLike;
|
|
36
|
+
currentStep?: string;
|
|
37
|
+
waitingStep?: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
type ResumeManagedLobsterFlowParams = {
|
|
41
|
+
taskFlow: BoundTaskFlow;
|
|
42
|
+
runner: LobsterRunner;
|
|
43
|
+
runnerParams: LobsterRunnerParams & {
|
|
44
|
+
action: "resume";
|
|
45
|
+
approve: boolean;
|
|
46
|
+
} & ({ token: string } | { approvalId: string });
|
|
47
|
+
flowId: string;
|
|
48
|
+
expectedRevision: number;
|
|
49
|
+
currentStep?: string;
|
|
50
|
+
waitingStep?: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type ManagedLobsterFlowResult =
|
|
54
|
+
| {
|
|
55
|
+
ok: true;
|
|
56
|
+
envelope: LobsterEnvelope;
|
|
57
|
+
flow: FlowRecord;
|
|
58
|
+
mutation: MutationResult;
|
|
59
|
+
}
|
|
60
|
+
| {
|
|
61
|
+
ok: false;
|
|
62
|
+
flow?: FlowRecord;
|
|
63
|
+
mutation?: MutationResult;
|
|
64
|
+
error: Error;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
function toJsonLike(value: unknown, seen = new WeakSet<object>()): JsonLike {
|
|
68
|
+
if (value === null) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
switch (typeof value) {
|
|
72
|
+
case "boolean":
|
|
73
|
+
case "string":
|
|
74
|
+
return value;
|
|
75
|
+
case "number":
|
|
76
|
+
return Number.isFinite(value) ? value : String(value);
|
|
77
|
+
case "bigint":
|
|
78
|
+
return value.toString();
|
|
79
|
+
case "undefined":
|
|
80
|
+
case "function":
|
|
81
|
+
case "symbol":
|
|
82
|
+
return null;
|
|
83
|
+
case "object": {
|
|
84
|
+
if (value instanceof Date) {
|
|
85
|
+
return value.toISOString();
|
|
86
|
+
}
|
|
87
|
+
if (Array.isArray(value)) {
|
|
88
|
+
return value.map((item) => toJsonLike(item, seen));
|
|
89
|
+
}
|
|
90
|
+
if (seen.has(value)) {
|
|
91
|
+
return "[Circular]";
|
|
92
|
+
}
|
|
93
|
+
seen.add(value);
|
|
94
|
+
const jsonObject: Record<string, JsonLike> = {};
|
|
95
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
96
|
+
if (entry === undefined || typeof entry === "function" || typeof entry === "symbol") {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
jsonObject[key] = toJsonLike(entry, seen);
|
|
100
|
+
}
|
|
101
|
+
seen.delete(value);
|
|
102
|
+
return jsonObject;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function buildApprovalWaitState(envelope: Extract<LobsterEnvelope, { ok: true }>): JsonLike {
|
|
109
|
+
if (!envelope.requiresApproval) {
|
|
110
|
+
return {
|
|
111
|
+
kind: "lobster_approval",
|
|
112
|
+
prompt: "",
|
|
113
|
+
items: [],
|
|
114
|
+
} satisfies LobsterApprovalWaitState;
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
kind: "lobster_approval",
|
|
118
|
+
prompt: envelope.requiresApproval.prompt,
|
|
119
|
+
items: envelope.requiresApproval.items.map((item) => toJsonLike(item)),
|
|
120
|
+
...(envelope.requiresApproval.resumeToken
|
|
121
|
+
? { resumeToken: envelope.requiresApproval.resumeToken }
|
|
122
|
+
: {}),
|
|
123
|
+
...(envelope.requiresApproval.approvalId
|
|
124
|
+
? { approvalId: envelope.requiresApproval.approvalId }
|
|
125
|
+
: {}),
|
|
126
|
+
} satisfies LobsterApprovalWaitState;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function applyEnvelopeToFlow(params: {
|
|
130
|
+
taskFlow: BoundTaskFlow;
|
|
131
|
+
flow: FlowRecord;
|
|
132
|
+
envelope: LobsterEnvelope;
|
|
133
|
+
waitingStep: string;
|
|
134
|
+
}): MutationResult {
|
|
135
|
+
const { taskFlow, flow, envelope, waitingStep } = params;
|
|
136
|
+
|
|
137
|
+
if (!envelope.ok) {
|
|
138
|
+
return taskFlow.fail({
|
|
139
|
+
flowId: flow.flowId,
|
|
140
|
+
expectedRevision: flow.revision,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (envelope.status === "needs_approval") {
|
|
145
|
+
return taskFlow.setWaiting({
|
|
146
|
+
flowId: flow.flowId,
|
|
147
|
+
expectedRevision: flow.revision,
|
|
148
|
+
currentStep: waitingStep,
|
|
149
|
+
waitJson: buildApprovalWaitState(envelope),
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return taskFlow.finish({
|
|
154
|
+
flowId: flow.flowId,
|
|
155
|
+
expectedRevision: flow.revision,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function buildEnvelopeError(envelope: Extract<LobsterEnvelope, { ok: false }>) {
|
|
160
|
+
return new Error(envelope.error.message);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export async function runManagedLobsterFlow(
|
|
164
|
+
params: RunManagedLobsterFlowParams,
|
|
165
|
+
): Promise<ManagedLobsterFlowResult> {
|
|
166
|
+
const flow = params.taskFlow.createManaged({
|
|
167
|
+
controllerId: params.controllerId,
|
|
168
|
+
goal: params.goal,
|
|
169
|
+
currentStep: params.currentStep ?? "run_lobster",
|
|
170
|
+
...(params.stateJson !== undefined ? { stateJson: params.stateJson } : {}),
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
const envelope = await params.runner.run(params.runnerParams);
|
|
175
|
+
const mutation = applyEnvelopeToFlow({
|
|
176
|
+
taskFlow: params.taskFlow,
|
|
177
|
+
flow,
|
|
178
|
+
envelope,
|
|
179
|
+
waitingStep: params.waitingStep ?? "await_lobster_approval",
|
|
180
|
+
});
|
|
181
|
+
if (!envelope.ok) {
|
|
182
|
+
return {
|
|
183
|
+
ok: false,
|
|
184
|
+
flow,
|
|
185
|
+
mutation,
|
|
186
|
+
error: buildEnvelopeError(envelope),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
ok: true,
|
|
191
|
+
envelope,
|
|
192
|
+
flow,
|
|
193
|
+
mutation,
|
|
194
|
+
};
|
|
195
|
+
} catch (error) {
|
|
196
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
197
|
+
try {
|
|
198
|
+
const mutation = params.taskFlow.fail({
|
|
199
|
+
flowId: flow.flowId,
|
|
200
|
+
expectedRevision: flow.revision,
|
|
201
|
+
});
|
|
202
|
+
return {
|
|
203
|
+
ok: false,
|
|
204
|
+
flow,
|
|
205
|
+
mutation,
|
|
206
|
+
error: err,
|
|
207
|
+
};
|
|
208
|
+
} catch {
|
|
209
|
+
return {
|
|
210
|
+
ok: false,
|
|
211
|
+
flow,
|
|
212
|
+
error: err,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export async function resumeManagedLobsterFlow(
|
|
219
|
+
params: ResumeManagedLobsterFlowParams,
|
|
220
|
+
): Promise<ManagedLobsterFlowResult> {
|
|
221
|
+
const resumed = params.taskFlow.resume({
|
|
222
|
+
flowId: params.flowId,
|
|
223
|
+
expectedRevision: params.expectedRevision,
|
|
224
|
+
status: "running",
|
|
225
|
+
currentStep: params.currentStep ?? "resume_lobster",
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
if (!resumed.applied) {
|
|
229
|
+
return {
|
|
230
|
+
ok: false,
|
|
231
|
+
mutation: resumed,
|
|
232
|
+
error: new Error(`TaskFlow resume failed: ${resumed.code}`),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
try {
|
|
237
|
+
const envelope = await params.runner.run(params.runnerParams);
|
|
238
|
+
const mutation = applyEnvelopeToFlow({
|
|
239
|
+
taskFlow: params.taskFlow,
|
|
240
|
+
flow: resumed.flow,
|
|
241
|
+
envelope,
|
|
242
|
+
waitingStep: params.waitingStep ?? "await_lobster_approval",
|
|
243
|
+
});
|
|
244
|
+
if (!envelope.ok) {
|
|
245
|
+
return {
|
|
246
|
+
ok: false,
|
|
247
|
+
flow: resumed.flow,
|
|
248
|
+
mutation,
|
|
249
|
+
error: buildEnvelopeError(envelope),
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
ok: true,
|
|
254
|
+
envelope,
|
|
255
|
+
flow: resumed.flow,
|
|
256
|
+
mutation,
|
|
257
|
+
};
|
|
258
|
+
} catch (error) {
|
|
259
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
260
|
+
try {
|
|
261
|
+
const mutation = params.taskFlow.fail({
|
|
262
|
+
flowId: params.flowId,
|
|
263
|
+
expectedRevision: resumed.flow.revision,
|
|
264
|
+
});
|
|
265
|
+
return {
|
|
266
|
+
ok: false,
|
|
267
|
+
flow: resumed.flow,
|
|
268
|
+
mutation,
|
|
269
|
+
error: err,
|
|
270
|
+
};
|
|
271
|
+
} catch {
|
|
272
|
+
return {
|
|
273
|
+
ok: false,
|
|
274
|
+
flow: resumed.flow,
|
|
275
|
+
error: err,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|