libretto 0.6.21 → 0.6.23

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.
Files changed (40) hide show
  1. package/README.md +5 -1
  2. package/README.template.md +5 -1
  3. package/dist/cli/commands/execution.js +8 -1
  4. package/dist/cli/core/browser.js +8 -3
  5. package/dist/cli/core/daemon/daemon.js +8 -6
  6. package/dist/cli/core/providers/kernel.js +107 -29
  7. package/dist/cli/core/providers/steel.js +10 -1
  8. package/dist/index.d.ts +3 -2
  9. package/dist/index.js +15 -1
  10. package/dist/runtime/recovery/agent.d.ts +50 -2
  11. package/dist/runtime/recovery/agent.js +159 -45
  12. package/dist/runtime/recovery/index.d.ts +2 -1
  13. package/dist/runtime/recovery/index.js +16 -2
  14. package/dist/runtime/recovery/page-fallbacks.d.ts +45 -0
  15. package/dist/runtime/recovery/page-fallbacks.js +389 -0
  16. package/dist/shared/state/index.d.ts +1 -1
  17. package/dist/shared/state/session-state.d.ts +4 -1
  18. package/dist/shared/state/session-state.js +2 -1
  19. package/dist/shared/workflow/workflow.d.ts +19 -6
  20. package/dist/shared/workflow/workflow.js +38 -9
  21. package/docs/reference/runtime/page-fallbacks.mdx +85 -0
  22. package/docs/understand-libretto/error-handling-and-recovery.mdx +45 -0
  23. package/package.json +4 -12
  24. package/skills/libretto/SKILL.md +8 -2
  25. package/skills/libretto/references/code-generation-rules.md +23 -6
  26. package/skills/libretto-readonly/SKILL.md +1 -1
  27. package/src/cli/commands/execution.ts +8 -1
  28. package/src/cli/core/browser.ts +7 -2
  29. package/src/cli/core/daemon/daemon.ts +9 -4
  30. package/src/cli/core/daemon/ipc.ts +1 -0
  31. package/src/cli/core/providers/kernel.ts +153 -29
  32. package/src/cli/core/providers/steel.ts +11 -1
  33. package/src/cli/core/providers/types.ts +3 -0
  34. package/src/index.ts +22 -2
  35. package/src/runtime/recovery/agent.ts +227 -50
  36. package/src/runtime/recovery/index.ts +21 -1
  37. package/src/runtime/recovery/page-fallbacks.ts +527 -0
  38. package/src/shared/state/index.ts +1 -0
  39. package/src/shared/state/session-state.ts +2 -0
  40. package/src/shared/workflow/workflow.ts +90 -20
@@ -1,5 +1,9 @@
1
1
  import type { Page } from "playwright";
2
2
  import { z } from "zod";
3
+ import {
4
+ createRecoveryPage,
5
+ type RecoveryAction,
6
+ } from "../../runtime/recovery/page-fallbacks.js";
3
7
 
4
8
  export const LIBRETTO_WORKFLOW_BRAND = Symbol.for("libretto.workflow");
5
9
 
@@ -13,12 +17,23 @@ export type LibrettoWorkflowHandler<Input = unknown, Output = unknown> = (
13
17
  input: Input,
14
18
  ) => Promise<Output>;
15
19
 
16
- export type LibrettoWorkflowSchemas<
17
- InputSchema extends z.ZodType,
18
- OutputSchema extends z.ZodType,
20
+ export type LibrettoWorkflowDefinition<
21
+ InputSchema extends z.ZodType = z.ZodType<unknown>,
22
+ OutputSchema extends z.ZodType = z.ZodType<unknown>,
19
23
  > = {
20
- input: InputSchema;
21
- output: OutputSchema;
24
+ input?: InputSchema;
25
+ output?: OutputSchema;
26
+ recoveryAction?: RecoveryAction;
27
+ };
28
+
29
+ export type LibrettoWorkflowOptions<
30
+ InputSchema extends z.ZodType = z.ZodType<unknown>,
31
+ OutputSchema extends z.ZodType = z.ZodType<unknown>,
32
+ > = LibrettoWorkflowDefinition<InputSchema, OutputSchema> & {
33
+ handler: LibrettoWorkflowHandler<
34
+ z.infer<InputSchema>,
35
+ z.infer<OutputSchema>
36
+ >;
22
37
  };
23
38
 
24
39
  // Thrown when input fails Zod validation. The runner surfaces `.message`
@@ -89,6 +104,7 @@ export class LibrettoWorkflow<
89
104
  // this schema to JSON Schema at build time and exposes it via
90
105
  // /v1/workflows/get so API consumers know the workflow's output shape.
91
106
  public readonly outputSchema?: OutputSchema;
107
+ public readonly recoveryAction?: RecoveryAction;
92
108
  private readonly handler: LibrettoWorkflowHandler<
93
109
  z.infer<InputSchema>,
94
110
  z.infer<OutputSchema>
@@ -96,15 +112,22 @@ export class LibrettoWorkflow<
96
112
 
97
113
  constructor(
98
114
  name: string,
99
- schemas: LibrettoWorkflowSchemas<InputSchema, OutputSchema> | undefined,
115
+ options:
116
+ | {
117
+ inputSchema?: InputSchema;
118
+ outputSchema?: OutputSchema;
119
+ recoveryAction?: RecoveryAction;
120
+ }
121
+ | undefined,
100
122
  handler: LibrettoWorkflowHandler<
101
123
  z.infer<InputSchema>,
102
124
  z.infer<OutputSchema>
103
125
  >,
104
126
  ) {
105
127
  this.name = name;
106
- this.inputSchema = schemas?.input;
107
- this.outputSchema = schemas?.output;
128
+ this.inputSchema = options?.inputSchema;
129
+ this.outputSchema = options?.outputSchema;
130
+ this.recoveryAction = options?.recoveryAction;
108
131
  this.handler = handler;
109
132
  }
110
133
 
@@ -113,7 +136,16 @@ export class LibrettoWorkflow<
113
136
  input: unknown,
114
137
  ): Promise<z.infer<OutputSchema>> {
115
138
  const parsed = parseWorkflowInput(this.name, this.inputSchema, input);
116
- return this.handler(ctx, parsed);
139
+ const workflowContext =
140
+ !this.recoveryAction
141
+ ? ctx
142
+ : {
143
+ ...ctx,
144
+ page: createRecoveryPage(ctx.page, {
145
+ recoveryAction: this.recoveryAction,
146
+ }),
147
+ };
148
+ return this.handler(workflowContext, parsed);
117
149
  }
118
150
  }
119
151
 
@@ -122,6 +154,7 @@ export type ExportedLibrettoWorkflow = {
122
154
  readonly name: string;
123
155
  readonly inputSchema?: z.ZodType;
124
156
  readonly outputSchema?: z.ZodType;
157
+ readonly recoveryAction?: RecoveryAction;
125
158
  run: (ctx: LibrettoWorkflowContext, input: unknown) => Promise<unknown>;
126
159
  };
127
160
 
@@ -212,22 +245,47 @@ export function getWorkflowFromModuleExports(
212
245
  return null;
213
246
  }
214
247
 
215
- // Recommended 3-arg form: pass Zod schemas so input is validated at run time
216
- // and the hosted platform can expose typed I/O metadata via /v1/workflows/get.
217
- export function workflow<
248
+ function getWorkflowConstructorOptions<
218
249
  InputSchema extends z.ZodType,
219
250
  OutputSchema extends z.ZodType,
251
+ >(
252
+ options:
253
+ | LibrettoWorkflowDefinition<InputSchema, OutputSchema>
254
+ | LibrettoWorkflowOptions<InputSchema, OutputSchema>,
255
+ ): {
256
+ inputSchema?: InputSchema;
257
+ outputSchema?: OutputSchema;
258
+ recoveryAction?: RecoveryAction;
259
+ } {
260
+ return {
261
+ inputSchema: options.input,
262
+ outputSchema: options.output,
263
+ recoveryAction: options.recoveryAction,
264
+ };
265
+ }
266
+
267
+ export function workflow<
268
+ InputSchema extends z.ZodType = z.ZodType<unknown>,
269
+ OutputSchema extends z.ZodType = z.ZodType<unknown>,
220
270
  >(
221
271
  name: string,
222
- schemas: LibrettoWorkflowSchemas<InputSchema, OutputSchema>,
272
+ definition: LibrettoWorkflowDefinition<InputSchema, OutputSchema>,
223
273
  handler: LibrettoWorkflowHandler<
224
274
  z.infer<InputSchema>,
225
275
  z.infer<OutputSchema>
226
276
  >,
227
277
  ): LibrettoWorkflow<InputSchema, OutputSchema>;
228
278
 
279
+ export function workflow<
280
+ InputSchema extends z.ZodType = z.ZodType<unknown>,
281
+ OutputSchema extends z.ZodType = z.ZodType<unknown>,
282
+ >(
283
+ name: string,
284
+ options: LibrettoWorkflowOptions<InputSchema, OutputSchema>,
285
+ ): LibrettoWorkflow<InputSchema, OutputSchema>;
286
+
229
287
  // Legacy 2-arg form kept so deployments built before Zod schemas existed
230
- // continue to load. New code should always pass schemas.
288
+ // continue to load. New code should pass input/output schemas when possible.
231
289
  export function workflow<Input = unknown, Output = unknown>(
232
290
  name: string,
233
291
  handler: LibrettoWorkflowHandler<Input, Output>,
@@ -235,18 +293,30 @@ export function workflow<Input = unknown, Output = unknown>(
235
293
 
236
294
  export function workflow(
237
295
  name: string,
238
- schemasOrHandler:
239
- | LibrettoWorkflowSchemas<z.ZodType, z.ZodType>
296
+ definitionOrHandler:
297
+ | LibrettoWorkflowDefinition<z.ZodType, z.ZodType>
298
+ | LibrettoWorkflowOptions
240
299
  | LibrettoWorkflowHandler,
241
300
  maybeHandler?: LibrettoWorkflowHandler,
242
301
  ): LibrettoWorkflow {
243
- if (typeof schemasOrHandler === "function") {
244
- return new LibrettoWorkflow(name, undefined, schemasOrHandler);
302
+ if (typeof definitionOrHandler === "function") {
303
+ return new LibrettoWorkflow(name, undefined, definitionOrHandler);
304
+ }
305
+ if ("handler" in definitionOrHandler) {
306
+ return new LibrettoWorkflow(
307
+ name,
308
+ getWorkflowConstructorOptions(definitionOrHandler),
309
+ definitionOrHandler.handler,
310
+ );
245
311
  }
246
312
  if (!maybeHandler) {
247
313
  throw new Error(
248
- `workflow("${name}") called with schemas but no handler. Pass the handler as the third argument.`,
314
+ `workflow("${name}") called without a handler. Pass the handler as the third argument or in the options object.`,
249
315
  );
250
316
  }
251
- return new LibrettoWorkflow(name, schemasOrHandler, maybeHandler);
317
+ return new LibrettoWorkflow(
318
+ name,
319
+ getWorkflowConstructorOptions(definitionOrHandler),
320
+ maybeHandler,
321
+ );
252
322
  }