libretto 0.2.2 → 0.2.4

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 CHANGED
@@ -1,231 +1,125 @@
1
1
  # Libretto
2
2
 
3
- A TypeScript library for browser automation with AI-powered recovery and data extraction. Built on Playwright.
4
-
5
- ## Features
6
-
7
- - **AI-powered recovery** — Vision-based agent that automatically detects and dismisses popups or obstacles using an LLM
8
- - **Structured data extraction** — Extract typed data from web pages using AI vision + Zod schemas
9
- - **Error detection** — Classify form/submission errors against known patterns
10
- - **In-browser network requests** — Execute authenticated fetch calls inside the page context with optional Zod validation
11
- - **File downloads** — Trigger and intercept file downloads via click, with optional save-to-disk
12
- - **Dry-run mode** — Skip mutations in development without side effects
13
- - **Pluggable LLM** — Bring your own LLM provider (Claude, GPT, etc.) via a simple interface
14
- - **Pluggable logging** — All runtime functions accept an optional logger; defaults to console output
3
+ AI-powered browser automation library and CLI built on Playwright.
15
4
 
16
5
  ## Installation
17
6
 
18
7
  ```bash
19
8
  pnpm add libretto playwright zod
9
+ npx libretto init
20
10
  ```
21
11
 
22
- `playwright` and `zod` are peer dependencies.
12
+ > **pnpm users:** add `onlyBuiltDependencies` to your `package.json` to allow
13
+ > Playwright's postinstall script to run:
14
+ >
15
+ > ```jsonc
16
+ > // package.json
17
+ > {
18
+ > "pnpm": {
19
+ > "onlyBuiltDependencies": ["playwright"]
20
+ > }
21
+ > }
22
+ > ```
23
23
 
24
24
  ## Quick Start
25
25
 
26
- ```typescript
27
- import { chromium } from "playwright";
28
- import { extractFromPage, attemptWithRecovery } from "libretto";
29
-
30
- const browser = await chromium.launch();
31
- const page = await browser.newPage();
32
-
33
- await page.goto("https://example.com/login");
34
- await page.fill("#email", "user@example.com");
35
- await page.fill("#password", "secret");
36
-
37
- // Automatically retry with AI popup recovery on failure
38
- await attemptWithRecovery(page, () => page.click('button[type="submit"]'));
39
-
40
- await browser.close();
41
- ```
42
-
43
- ## Runtime Functions
26
+ ### 1. Configure your LLM
44
27
 
45
- ### Recovery
46
-
47
- #### `attemptWithRecovery(page, fn, logger?, llmClient?)`
48
-
49
- Executes a function and, if it fails, uses AI vision to detect and dismiss popups before retrying once.
28
+ The easiest way is to use the built-in Vercel AI SDK adapter with any compatible provider:
50
29
 
51
30
  ```typescript
52
- import { attemptWithRecovery } from "libretto";
31
+ import { createLLMClientFromModel } from "libretto/llm";
32
+ import { openai } from "@ai-sdk/openai";
53
33
 
54
- await attemptWithRecovery(page, async () => {
55
- await page.click('button[type="submit"]');
56
- }, undefined, llmClient);
34
+ const llmClient = createLLMClientFromModel(openai("gpt-4o"));
57
35
  ```
58
36
 
59
- #### `executeRecoveryAgent(page, instruction, logger?, llmClient?)`
60
-
61
- Runs a multi-step vision-based recovery agent that takes screenshots and executes browser actions (click, type, scroll, etc.) to resolve obstacles.
37
+ Or use any other provider:
62
38
 
63
39
  ```typescript
64
- import { executeRecoveryAgent } from "libretto";
65
-
66
- await executeRecoveryAgent(
67
- page,
68
- "Close the cookie consent banner",
69
- undefined,
70
- llmClient,
71
- );
72
- ```
40
+ import { createLLMClientFromModel } from "libretto";
41
+ import { anthropic } from "@ai-sdk/anthropic";
73
42
 
74
- #### `detectSubmissionError(page, error, logContext, llmClient, knownErrors?, logger?)`
75
-
76
- Uses a screenshot + LLM vision to detect if an error occurred during a form submission. Matches against provided known error patterns.
77
-
78
- ```typescript
79
- import { detectSubmissionError } from "libretto";
80
-
81
- try {
82
- await page.click("#submit");
83
- } catch (error) {
84
- const result = await detectSubmissionError(page, error, "checkout", llmClient, [
85
- { id: "duplicate", errorPatterns: ["already exists"], userMessage: "Duplicate entry" },
86
- ]);
87
- console.log(result.errorId, result.message);
88
- }
43
+ const llmClient = createLLMClientFromModel(anthropic("claude-sonnet-4-20250514"));
89
44
  ```
90
45
 
91
- ### Data Extraction
92
-
93
- #### `extractFromPage(options)`
94
-
95
- Extract structured data from a page using AI vision + a Zod schema.
96
-
97
- ```typescript
98
- import { extractFromPage } from "libretto";
99
- import { z } from "zod";
100
-
101
- const result = await extractFromPage({
102
- page,
103
- llmClient,
104
- instruction: "Extract the product name and price",
105
- schema: z.object({
106
- name: z.string(),
107
- price: z.number(),
108
- }),
109
- selector: ".product-card", // optional — scopes to a specific element
110
- });
111
- // result is typed as { name: string; price: number }
112
- ```
113
-
114
- ### Network
115
-
116
- #### `pageRequest(page, config, options?)`
117
-
118
- Executes a fetch call inside the browser context via `page.evaluate()`, inheriting the page's cookies and auth state. Supports optional Zod validation.
119
-
120
- ```typescript
121
- import { pageRequest } from "libretto";
122
- import { z } from "zod";
123
-
124
- const data = await pageRequest(
125
- page,
126
- {
127
- url: "https://example.com/api/profile",
128
- method: "GET",
129
- responseType: "json",
130
- },
131
- {
132
- schema: z.object({ name: z.string(), email: z.string() }),
133
- },
134
- );
135
- ```
136
-
137
- ### Downloads
138
-
139
- #### `downloadViaClick(page, selector, options?)`
140
-
141
- Triggers a file download by clicking a DOM element and intercepts the result.
142
-
143
- ```typescript
144
- import { downloadViaClick } from "libretto";
145
-
146
- const { buffer, filename } = await downloadViaClick(page, "#download-btn");
147
- ```
148
-
149
- #### `downloadAndSave(page, selector, options?)`
150
-
151
- Same as `downloadViaClick` but also writes the file to disk.
152
-
153
- ```typescript
154
- import { downloadAndSave } from "libretto";
155
-
156
- const { savedTo } = await downloadAndSave(page, "#export-csv", {
157
- savePath: "./exports/report.csv",
158
- });
159
- ```
160
-
161
- ## LLM Client Interface
162
-
163
- Provide your own implementation backed by any LLM provider:
46
+ You can also implement the `LLMClient` interface directly for full control:
164
47
 
165
48
  ```typescript
166
49
  import type { LLMClient } from "libretto";
167
50
 
168
- const myLLMClient: LLMClient = {
51
+ const llmClient: LLMClient = {
169
52
  async generateObject({ prompt, schema, temperature }) {
170
53
  // Call your LLM, return parsed + validated result
171
54
  },
172
55
  async generateObjectFromMessages({ messages, schema, temperature }) {
173
- // Call your LLM with message history, return parsed + validated result
56
+ // Call your LLM with message history (may include images)
174
57
  },
175
58
  };
176
59
  ```
177
60
 
178
- ## Logging
179
-
180
- All runtime functions accept an optional `logger` parameter. When omitted, output goes to `console.log` with `[INFO]`, `[WARN]`, `[ERROR]` prefixes.
181
-
182
- For structured logging, use the built-in `Logger` class:
61
+ ### 2. Write a workflow
183
62
 
184
63
  ```typescript
185
- import { Logger, createFileLogSink, prettyConsoleSink } from "libretto";
64
+ import { workflow } from "libretto";
65
+ import { z } from "zod";
186
66
 
187
- const logger = new Logger()
188
- .withSink(createFileLogSink({ filePath: "./app.log" }))
189
- .withSink(prettyConsoleSink);
67
+ export default workflow({
68
+ name: "extract-product",
69
+ schema: z.object({ url: z.string() }),
70
+ handler: async (ctx) => {
71
+ const page = ctx.page;
72
+ await page.goto(ctx.params.url);
190
73
 
191
- const scoped = logger.withScope("auth");
192
- scoped.info("login attempt", { user: "alice" });
193
- scoped.error("login failed", { reason: "bad password" });
194
- ```
74
+ const data = await ctx.extract({
75
+ instruction: "Extract the product name and price",
76
+ schema: z.object({ name: z.string(), price: z.number() }),
77
+ });
195
78
 
196
- ## Module Exports
79
+ return data;
80
+ },
81
+ });
82
+ ```
197
83
 
198
- Libretto provides granular imports:
84
+ ### 3. Run it
199
85
 
200
- | Import | Contents |
201
- | ------------------------ | --------------------------------------------------------- |
202
- | `libretto` | Everything |
203
- | `libretto/logger` | `Logger`, `defaultLogger`, sinks |
204
- | `libretto/recovery` | `attemptWithRecovery`, `executeRecoveryAgent`, `detectSubmissionError` |
205
- | `libretto/extract` | `extractFromPage` |
206
- | `libretto/network` | `pageRequest` |
207
- | `libretto/download` | `downloadViaClick`, `downloadAndSave` |
208
- | `libretto/debug` | `debugPause` |
209
- | `libretto/config` | `isDryRun`, `isDebugMode`, `shouldPauseBeforeMutation` |
210
- | `libretto/instrumentation` | `instrumentPage`, `installInstrumentation` |
211
- | `libretto/visualization` | Ghost cursor and highlight helpers |
212
- | `libretto/run` | `launchBrowser` |
213
- | `libretto/state` | Session state serialization and parsing |
214
- | `libretto/llm` | `LLMClient` type |
86
+ ```bash
87
+ npx libretto run ./workflows/extract-product.ts extractProduct \
88
+ --params '{"url": "https://example.com/product"}'
89
+ ```
215
90
 
216
- ## Configuration
91
+ ## CLI Commands
217
92
 
218
- Runtime flags via environment variables:
93
+ ```
94
+ npx libretto init # Copy skills, install Playwright Chromium
95
+ npx libretto open <url> # Launch browser and open URL
96
+ npx libretto run <file> <export> # Run a workflow
97
+ npx libretto ai configure <preset> # Configure AI runtime (codex, claude, gemini)
98
+ npx libretto snapshot # Capture page screenshot + HTML
99
+ npx libretto exec <code> # Execute Playwright code
100
+ ```
219
101
 
220
- | Env Variable | Effect |
221
- | --------------------- | --------------------------------------------------- |
222
- | `LIBRETTO_DEBUG` | Enable debug mode |
223
- | `LIBRETTO_DRY_RUN` | Enable dry-run mode (defaults to `true` in development) |
102
+ Run `npx libretto help` for the full list.
224
103
 
225
- ## Development
104
+ ## Module Exports
226
105
 
227
- ```bash
228
- pnpm install
229
- pnpm build # compile to dist/
230
- pnpm type-check # typecheck without emitting
231
- ```
106
+ | Import | Contents |
107
+ | -------------------------- | ------------------------------------------------------------- |
108
+ | `libretto` | Everything |
109
+ | `libretto/llm` | `LLMClient` type, `createLLMClient`, `createLLMClientFromModel` |
110
+ | `libretto/recovery` | `attemptWithRecovery`, `executeRecoveryAgent`, `detectSubmissionError` |
111
+ | `libretto/extract` | `extractFromPage` |
112
+ | `libretto/network` | `pageRequest` |
113
+ | `libretto/download` | `downloadViaClick`, `downloadAndSave` |
114
+ | `libretto/logger` | `Logger`, `defaultLogger`, sinks |
115
+ | `libretto/debug` | `debugPause` |
116
+ | `libretto/config` | `isDryRun`, `isDebugMode`, `shouldPauseBeforeMutation` |
117
+ | `libretto/instrumentation` | `instrumentPage`, `installInstrumentation` |
118
+ | `libretto/visualization` | Ghost cursor and highlight helpers |
119
+ | `libretto/run` | `launchBrowser` |
120
+ | `libretto/state` | Session state serialization and parsing |
121
+
122
+ ## Links
123
+
124
+ - [GitHub](https://github.com/saffron-health/libretto)
125
+ - [Issues](https://github.com/saffron-health/libretto/issues)
package/dist/cli/cli.js CHANGED
@@ -4,6 +4,7 @@ import { registerAICommands } from "./commands/ai.js";
4
4
  import { registerBrowserCommands } from "./commands/browser.js";
5
5
  import { registerExecutionCommands } from "./commands/execution.js";
6
6
  import { registerLogCommands } from "./commands/logs.js";
7
+ import { registerInitCommand } from "./commands/init.js";
7
8
  import { registerSnapshotCommands } from "./commands/snapshot.js";
8
9
  import {
9
10
  closeLogger,
@@ -26,6 +27,7 @@ const CLI_COMMANDS = /* @__PURE__ */ new Set([
26
27
  "pages",
27
28
  "resume",
28
29
  "close",
30
+ "init",
29
31
  "--help",
30
32
  "-h",
31
33
  "help"
@@ -34,6 +36,7 @@ function printUsage() {
34
36
  console.log(`Usage: libretto-cli <command> [--session <name>]
35
37
 
36
38
  Commands:
39
+ init [--skip-browsers] Initialize libretto (copy skills, install browsers)
37
40
  open <url> [--headless] Launch browser and open URL (headed by default)
38
41
  Automatically loads saved profile if available
39
42
  run <integrationFile> <integrationExport> [--params <json> | --params-file <path>] [--headed|--headless] [--debug] Run an exported Libretto workflow from a file; pass --debug to enable pause-on-failure debugging (or --no-debug to disable)
@@ -160,6 +163,7 @@ function createParser(logger) {
160
163
  parser = registerLogCommands(parser);
161
164
  parser = registerAICommands(parser);
162
165
  parser = registerSnapshotCommands(parser, logger);
166
+ parser = registerInitCommand(parser);
163
167
  parser = parser.command("help", "Show usage", () => {
164
168
  }, () => {
165
169
  printUsage();
@@ -0,0 +1,95 @@
1
+ import { existsSync, mkdirSync, cpSync, readdirSync } from "node:fs";
2
+ import { join, dirname } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { spawnSync } from "node:child_process";
5
+ import { REPO_ROOT } from "../core/context.js";
6
+ function getSkillSourceDir() {
7
+ const thisDir = dirname(fileURLToPath(import.meta.url));
8
+ const pkgRoot = join(thisDir, "..", "..", "..");
9
+ const skillDir = join(pkgRoot, "skill");
10
+ if (existsSync(skillDir)) return skillDir;
11
+ const skillsDir = join(pkgRoot, "skills");
12
+ if (existsSync(skillsDir)) return skillsDir;
13
+ throw new Error(
14
+ "Could not find skill/ or skills/ directory in the libretto package."
15
+ );
16
+ }
17
+ function copySkills() {
18
+ const src = getSkillSourceDir();
19
+ const files = readdirSync(src);
20
+ if (files.length === 0) {
21
+ console.log(" No skill files found to copy.");
22
+ return;
23
+ }
24
+ const targets = [
25
+ join(REPO_ROOT, ".agents", "skills", "libretto"),
26
+ join(REPO_ROOT, ".claude", "skills", "libretto")
27
+ ];
28
+ for (const target of targets) {
29
+ mkdirSync(target, { recursive: true });
30
+ cpSync(src, target, { recursive: true });
31
+ console.log(` \u2713 Copied skill files to ${target}`);
32
+ }
33
+ }
34
+ function installBrowsers() {
35
+ console.log("\nInstalling Playwright Chromium...");
36
+ const result = spawnSync("npx", ["playwright", "install", "chromium"], {
37
+ stdio: "inherit",
38
+ shell: true
39
+ });
40
+ if (result.status === 0) {
41
+ console.log(" \u2713 Playwright Chromium installed");
42
+ } else {
43
+ console.error(
44
+ " \u2717 Failed to install Playwright Chromium. Run manually: npx playwright install chromium"
45
+ );
46
+ }
47
+ }
48
+ function checkSnapshotLLM() {
49
+ const hasAnyCreds = process.env.GOOGLE_CLOUD_PROJECT || process.env.GCLOUD_PROJECT || process.env.ANTHROPIC_API_KEY || process.env.OPENAI_API_KEY;
50
+ console.log("\nSnapshot LLM configuration:");
51
+ if (hasAnyCreds) {
52
+ console.log(" \u2713 LLM credentials detected");
53
+ } else {
54
+ console.log(" \u2717 No LLM credentials found.");
55
+ console.log(" Set one of the following environment variables:");
56
+ console.log(" GOOGLE_CLOUD_PROJECT (for Vertex AI / Gemini)");
57
+ console.log(" ANTHROPIC_API_KEY (for Claude)");
58
+ console.log(" OPENAI_API_KEY (for GPT)");
59
+ console.log(
60
+ " Then configure via: npx libretto ai configure <preset>"
61
+ );
62
+ }
63
+ }
64
+ function registerInitCommand(yargs) {
65
+ return yargs.command(
66
+ "init",
67
+ "Initialize libretto in the current project",
68
+ (cmd) => cmd.option("skip-browsers", {
69
+ type: "boolean",
70
+ default: false,
71
+ describe: "Skip Playwright Chromium installation"
72
+ }),
73
+ (argv) => {
74
+ console.log("Initializing libretto...\n");
75
+ console.log("Copying skill files...");
76
+ try {
77
+ copySkills();
78
+ } catch (err) {
79
+ console.error(
80
+ ` \u2717 ${err instanceof Error ? err.message : String(err)}`
81
+ );
82
+ }
83
+ if (!argv["skip-browsers"]) {
84
+ installBrowsers();
85
+ } else {
86
+ console.log("\nSkipping browser installation (--skip-browsers)");
87
+ }
88
+ checkSnapshotLLM();
89
+ console.log("\n\u2713 libretto init complete");
90
+ }
91
+ );
92
+ }
93
+ export {
94
+ registerInitCommand
95
+ };
@@ -53,6 +53,11 @@ function ensureLibrettoSetup() {
53
53
  if (!existsSync(LIBRETTO_GITIGNORE_PATH)) {
54
54
  writeFileSync(LIBRETTO_GITIGNORE_PATH, LIBRETTO_GITIGNORE_CONTENT, "utf-8");
55
55
  }
56
+ const agentsSkillsDir = join(REPO_ROOT, ".agents", "skills", "libretto");
57
+ const claudeSkillsDir = join(REPO_ROOT, ".claude", "skills", "libretto");
58
+ if (!existsSync(agentsSkillsDir) && !existsSync(claudeSkillsDir)) {
59
+ console.log("[libretto] Skills not installed. Run 'npx libretto init' to complete setup.");
60
+ }
56
61
  }
57
62
  function createLoggerForSession(session) {
58
63
  validateSessionName(session);
package/dist/index.cjs CHANGED
@@ -29,6 +29,7 @@ __export(index_exports, {
29
29
  attemptWithRecovery: () => import_recovery.attemptWithRecovery,
30
30
  clearHighlights: () => import_highlight.clearHighlights,
31
31
  createFileLogSink: () => import_sinks.createFileLogSink,
32
+ createLLMClientFromModel: () => import_ai_sdk_adapter.createLLMClientFromModel,
32
33
  debugPause: () => import_pause.debugPause,
33
34
  defaultLogger: () => import_logger.defaultLogger,
34
35
  detectSubmissionError: () => import_errors.detectSubmissionError,
@@ -63,6 +64,7 @@ __export(index_exports, {
63
64
  module.exports = __toCommonJS(index_exports);
64
65
  var import_logger = require("./shared/logger/logger.js");
65
66
  var import_sinks = require("./shared/logger/sinks.js");
67
+ var import_ai_sdk_adapter = require("./shared/llm/ai-sdk-adapter.js");
66
68
  var import_state = require("./shared/state/index.js");
67
69
  var import_agent = require("./runtime/recovery/agent.js");
68
70
  var import_recovery = require("./runtime/recovery/recovery.js");
@@ -90,6 +92,7 @@ var import_workflow = require("./shared/workflow/workflow.js");
90
92
  attemptWithRecovery,
91
93
  clearHighlights,
92
94
  createFileLogSink,
95
+ createLLMClientFromModel,
93
96
  debugPause,
94
97
  defaultLogger,
95
98
  detectSubmissionError,
package/dist/index.d.cts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { LogOptions, Logger, LoggerApi, LoggerSink, MinimalLogger, defaultLogger } from './shared/logger/logger.cjs';
2
2
  export { createFileLogSink, jsonlConsoleSink, prettyConsoleSink } from './shared/logger/sinks.cjs';
3
3
  export { LLMClient, Message, MessageContentPart } from './shared/llm/types.cjs';
4
+ export { createLLMClientFromModel } from './shared/llm/ai-sdk-adapter.cjs';
4
5
  export { SESSION_STATE_VERSION, SessionState, SessionStateFile, SessionStateFileSchema, SessionStatus, SessionStatusSchema, parseSessionStateContent, parseSessionStateData, serializeSessionState } from './shared/state/session-state.cjs';
5
6
  export { executeRecoveryAgent } from './runtime/recovery/agent.cjs';
6
7
  export { attemptWithRecovery } from './runtime/recovery/recovery.cjs';
@@ -16,4 +17,5 @@ export { HighlightOptions, clearHighlights, ensureHighlightLayer, showHighlight
16
17
  export { BrowserSession, LaunchBrowserArgs, launchBrowser } from './shared/run/browser.cjs';
17
18
  export { LIBRETTO_WORKFLOW_BRAND, LibrettoAuthProfile, LibrettoWorkflow, LibrettoWorkflowContext, LibrettoWorkflowHandler, LibrettoWorkflowMetadata, workflow } from './shared/workflow/workflow.cjs';
18
19
  import 'zod';
20
+ import 'ai';
19
21
  import 'playwright';
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { LogOptions, Logger, LoggerApi, LoggerSink, MinimalLogger, defaultLogger } from './shared/logger/logger.js';
2
2
  export { createFileLogSink, jsonlConsoleSink, prettyConsoleSink } from './shared/logger/sinks.js';
3
3
  export { LLMClient, Message, MessageContentPart } from './shared/llm/types.js';
4
+ export { createLLMClientFromModel } from './shared/llm/ai-sdk-adapter.js';
4
5
  export { SESSION_STATE_VERSION, SessionState, SessionStateFile, SessionStateFileSchema, SessionStatus, SessionStatusSchema, parseSessionStateContent, parseSessionStateData, serializeSessionState } from './shared/state/session-state.js';
5
6
  export { executeRecoveryAgent } from './runtime/recovery/agent.js';
6
7
  export { attemptWithRecovery } from './runtime/recovery/recovery.js';
@@ -16,4 +17,5 @@ export { HighlightOptions, clearHighlights, ensureHighlightLayer, showHighlight
16
17
  export { BrowserSession, LaunchBrowserArgs, launchBrowser } from './shared/run/browser.js';
17
18
  export { LIBRETTO_WORKFLOW_BRAND, LibrettoAuthProfile, LibrettoWorkflow, LibrettoWorkflowContext, LibrettoWorkflowHandler, LibrettoWorkflowMetadata, workflow } from './shared/workflow/workflow.js';
18
19
  import 'zod';
20
+ import 'ai';
19
21
  import 'playwright';
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import {
4
4
  prettyConsoleSink,
5
5
  jsonlConsoleSink
6
6
  } from "./shared/logger/sinks.js";
7
+ import { createLLMClientFromModel } from "./shared/llm/ai-sdk-adapter.js";
7
8
  import {
8
9
  SESSION_STATE_VERSION,
9
10
  SessionStatusSchema,
@@ -74,6 +75,7 @@ export {
74
75
  attemptWithRecovery,
75
76
  clearHighlights,
76
77
  createFileLogSink,
78
+ createLLMClientFromModel,
77
79
  debugPause,
78
80
  defaultLogger,
79
81
  detectSubmissionError,
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var ai_sdk_adapter_exports = {};
20
+ __export(ai_sdk_adapter_exports, {
21
+ createLLMClientFromModel: () => createLLMClientFromModel
22
+ });
23
+ module.exports = __toCommonJS(ai_sdk_adapter_exports);
24
+ var import_ai = require("ai");
25
+ function createLLMClientFromModel(model) {
26
+ return {
27
+ async generateObject(opts) {
28
+ const { object } = await (0, import_ai.generateObject)({
29
+ model,
30
+ schema: opts.schema,
31
+ prompt: opts.prompt,
32
+ temperature: opts.temperature ?? 0
33
+ });
34
+ return object;
35
+ },
36
+ async generateObjectFromMessages(opts) {
37
+ const messages = opts.messages.map((msg) => {
38
+ if (typeof msg.content === "string") {
39
+ return { role: msg.role, content: msg.content };
40
+ }
41
+ return {
42
+ role: msg.role,
43
+ content: msg.content.map(
44
+ (part) => part.type === "text" ? { type: "text", text: part.text } : { type: "image", image: part.image }
45
+ )
46
+ };
47
+ });
48
+ const { object } = await (0, import_ai.generateObject)({
49
+ model,
50
+ schema: opts.schema,
51
+ messages,
52
+ temperature: opts.temperature ?? 0
53
+ });
54
+ return object;
55
+ }
56
+ };
57
+ }
58
+ // Annotate the CommonJS export names for ESM import in node:
59
+ 0 && (module.exports = {
60
+ createLLMClientFromModel
61
+ });
@@ -0,0 +1,22 @@
1
+ import { LanguageModel } from 'ai';
2
+ import { LLMClient } from './types.cjs';
3
+ import 'zod';
4
+
5
+ /**
6
+ * Creates a libretto LLMClient from a Vercel AI SDK LanguageModel.
7
+ *
8
+ * This eliminates the need for consumers to write their own adapter
9
+ * when using @ai-sdk/openai, @ai-sdk/anthropic, @ai-sdk/google-vertex,
10
+ * or any other Vercel AI SDK-compatible provider.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { createLLMClientFromModel } from "libretto/llm";
15
+ * import { openai } from "@ai-sdk/openai";
16
+ *
17
+ * const llmClient = createLLMClientFromModel(openai("gpt-4o"));
18
+ * ```
19
+ */
20
+ declare function createLLMClientFromModel(model: LanguageModel): LLMClient;
21
+
22
+ export { createLLMClientFromModel };
@@ -0,0 +1,22 @@
1
+ import { LanguageModel } from 'ai';
2
+ import { LLMClient } from './types.js';
3
+ import 'zod';
4
+
5
+ /**
6
+ * Creates a libretto LLMClient from a Vercel AI SDK LanguageModel.
7
+ *
8
+ * This eliminates the need for consumers to write their own adapter
9
+ * when using @ai-sdk/openai, @ai-sdk/anthropic, @ai-sdk/google-vertex,
10
+ * or any other Vercel AI SDK-compatible provider.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { createLLMClientFromModel } from "libretto/llm";
15
+ * import { openai } from "@ai-sdk/openai";
16
+ *
17
+ * const llmClient = createLLMClientFromModel(openai("gpt-4o"));
18
+ * ```
19
+ */
20
+ declare function createLLMClientFromModel(model: LanguageModel): LLMClient;
21
+
22
+ export { createLLMClientFromModel };
@@ -0,0 +1,37 @@
1
+ import { generateObject } from "ai";
2
+ function createLLMClientFromModel(model) {
3
+ return {
4
+ async generateObject(opts) {
5
+ const { object } = await generateObject({
6
+ model,
7
+ schema: opts.schema,
8
+ prompt: opts.prompt,
9
+ temperature: opts.temperature ?? 0
10
+ });
11
+ return object;
12
+ },
13
+ async generateObjectFromMessages(opts) {
14
+ const messages = opts.messages.map((msg) => {
15
+ if (typeof msg.content === "string") {
16
+ return { role: msg.role, content: msg.content };
17
+ }
18
+ return {
19
+ role: msg.role,
20
+ content: msg.content.map(
21
+ (part) => part.type === "text" ? { type: "text", text: part.text } : { type: "image", image: part.image }
22
+ )
23
+ };
24
+ });
25
+ const { object } = await generateObject({
26
+ model,
27
+ schema: opts.schema,
28
+ messages,
29
+ temperature: opts.temperature ?? 0
30
+ });
31
+ return object;
32
+ }
33
+ };
34
+ }
35
+ export {
36
+ createLLMClientFromModel
37
+ };
@@ -18,11 +18,14 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var llm_exports = {};
20
20
  __export(llm_exports, {
21
- createLLMClient: () => import_client.createLLMClient
21
+ createLLMClient: () => import_client.createLLMClient,
22
+ createLLMClientFromModel: () => import_ai_sdk_adapter.createLLMClientFromModel
22
23
  });
23
24
  module.exports = __toCommonJS(llm_exports);
24
25
  var import_client = require("./client.js");
26
+ var import_ai_sdk_adapter = require("./ai-sdk-adapter.js");
25
27
  // Annotate the CommonJS export names for ESM import in node:
26
28
  0 && (module.exports = {
27
- createLLMClient
29
+ createLLMClient,
30
+ createLLMClientFromModel
28
31
  });
@@ -1,3 +1,5 @@
1
1
  export { LLMClient, Message, MessageContentPart } from './types.cjs';
2
2
  export { createLLMClient } from './client.cjs';
3
+ export { createLLMClientFromModel } from './ai-sdk-adapter.cjs';
3
4
  import 'zod';
5
+ import 'ai';
@@ -1,3 +1,5 @@
1
1
  export { LLMClient, Message, MessageContentPart } from './types.js';
2
2
  export { createLLMClient } from './client.js';
3
+ export { createLLMClientFromModel } from './ai-sdk-adapter.js';
3
4
  import 'zod';
5
+ import 'ai';
@@ -1,4 +1,6 @@
1
1
  import { createLLMClient } from "./client.js";
2
+ import { createLLMClientFromModel } from "./ai-sdk-adapter.js";
2
3
  export {
3
- createLLMClient
4
+ createLLMClient,
5
+ createLLMClientFromModel
4
6
  };
@@ -17,13 +17,45 @@ type Message = {
17
17
  * Users provide their own implementation backed by any LLM provider
18
18
  * (OpenAI, Anthropic, etc.). Libretto uses this interface for AI extraction,
19
19
  * recovery agents, and error detection.
20
+ *
21
+ * **Error handling:** implementations should throw on failure rather than
22
+ * returning sentinel values (e.g. `null` or `undefined`). Libretto relies
23
+ * on exceptions to trigger retry/recovery logic.
24
+ *
25
+ * A ready-made adapter for the Vercel AI SDK is available via
26
+ * {@link createLLMClientFromModel} in `libretto/llm`.
20
27
  */
21
28
  interface LLMClient {
29
+ /**
30
+ * Generate a structured object from a single text prompt.
31
+ *
32
+ * The underlying model **must** support structured / JSON output so that
33
+ * the response can be parsed and validated against the provided Zod schema.
34
+ *
35
+ * @param opts.prompt - The text prompt sent to the model.
36
+ * @param opts.schema - A Zod schema describing the expected response shape.
37
+ * @param opts.temperature - Sampling temperature (default chosen by implementation, typically 0).
38
+ * @returns The parsed object matching the schema.
39
+ * @throws On LLM or parsing failure.
40
+ */
22
41
  generateObject<T extends z.ZodType>(opts: {
23
42
  prompt: string;
24
43
  schema: T;
25
44
  temperature?: number;
26
45
  }): Promise<z.infer<T>>;
46
+ /**
47
+ * Generate a structured object from a conversation-style message array.
48
+ *
49
+ * Messages may contain **image content** (base64 data URIs via
50
+ * {@link MessageContentPart}), so the backing model must support
51
+ * vision / multimodal input when images are present.
52
+ *
53
+ * @param opts.messages - Ordered list of user/assistant messages, potentially multimodal.
54
+ * @param opts.schema - A Zod schema describing the expected response shape.
55
+ * @param opts.temperature - Sampling temperature (default chosen by implementation, typically 0).
56
+ * @returns The parsed object matching the schema.
57
+ * @throws On LLM or parsing failure.
58
+ */
27
59
  generateObjectFromMessages<T extends z.ZodType>(opts: {
28
60
  messages: Message[];
29
61
  schema: T;
@@ -17,13 +17,45 @@ type Message = {
17
17
  * Users provide their own implementation backed by any LLM provider
18
18
  * (OpenAI, Anthropic, etc.). Libretto uses this interface for AI extraction,
19
19
  * recovery agents, and error detection.
20
+ *
21
+ * **Error handling:** implementations should throw on failure rather than
22
+ * returning sentinel values (e.g. `null` or `undefined`). Libretto relies
23
+ * on exceptions to trigger retry/recovery logic.
24
+ *
25
+ * A ready-made adapter for the Vercel AI SDK is available via
26
+ * {@link createLLMClientFromModel} in `libretto/llm`.
20
27
  */
21
28
  interface LLMClient {
29
+ /**
30
+ * Generate a structured object from a single text prompt.
31
+ *
32
+ * The underlying model **must** support structured / JSON output so that
33
+ * the response can be parsed and validated against the provided Zod schema.
34
+ *
35
+ * @param opts.prompt - The text prompt sent to the model.
36
+ * @param opts.schema - A Zod schema describing the expected response shape.
37
+ * @param opts.temperature - Sampling temperature (default chosen by implementation, typically 0).
38
+ * @returns The parsed object matching the schema.
39
+ * @throws On LLM or parsing failure.
40
+ */
22
41
  generateObject<T extends z.ZodType>(opts: {
23
42
  prompt: string;
24
43
  schema: T;
25
44
  temperature?: number;
26
45
  }): Promise<z.infer<T>>;
46
+ /**
47
+ * Generate a structured object from a conversation-style message array.
48
+ *
49
+ * Messages may contain **image content** (base64 data URIs via
50
+ * {@link MessageContentPart}), so the backing model must support
51
+ * vision / multimodal input when images are present.
52
+ *
53
+ * @param opts.messages - Ordered list of user/assistant messages, potentially multimodal.
54
+ * @param opts.schema - A Zod schema describing the expected response shape.
55
+ * @param opts.temperature - Sampling temperature (default chosen by implementation, typically 0).
56
+ * @returns The parsed object matching the schema.
57
+ * @throws On LLM or parsing failure.
58
+ */
27
59
  generateObjectFromMessages<T extends z.ZodType>(opts: {
28
60
  messages: Message[];
29
61
  schema: T;
@@ -1,4 +1,5 @@
1
1
  import { Page, BrowserContext, Browser } from 'playwright';
2
+ import { MinimalLogger } from '../logger/logger.cjs';
2
3
 
3
4
  declare const LIBRETTO_WORKFLOW_BRAND: unique symbol;
4
5
  type LibrettoAuthProfile = {
@@ -9,7 +10,7 @@ type LibrettoWorkflowMetadata = {
9
10
  authProfile?: LibrettoAuthProfile;
10
11
  };
11
12
  type LibrettoWorkflowContext = {
12
- logger: unknown;
13
+ logger: MinimalLogger;
13
14
  page: Page;
14
15
  context: BrowserContext;
15
16
  browser: Browser;
@@ -1,4 +1,5 @@
1
1
  import { Page, BrowserContext, Browser } from 'playwright';
2
+ import { MinimalLogger } from '../logger/logger.js';
2
3
 
3
4
  declare const LIBRETTO_WORKFLOW_BRAND: unique symbol;
4
5
  type LibrettoAuthProfile = {
@@ -9,7 +10,7 @@ type LibrettoWorkflowMetadata = {
9
10
  authProfile?: LibrettoAuthProfile;
10
11
  };
11
12
  type LibrettoWorkflowContext = {
12
- logger: unknown;
13
+ logger: MinimalLogger;
13
14
  page: Page;
14
15
  context: BrowserContext;
15
16
  browser: Browser;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libretto",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "AI-powered browser automation library and CLI built on Playwright",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -105,15 +105,9 @@
105
105
  "zod": ">=3.0.0"
106
106
  },
107
107
  "peerDependenciesMeta": {
108
- "@ai-sdk/anthropic": {
109
- "optional": true
110
- },
111
- "@ai-sdk/google-vertex": {
112
- "optional": true
113
- },
114
- "@ai-sdk/openai": {
115
- "optional": true
116
- }
108
+ "@ai-sdk/anthropic": { "optional": true },
109
+ "@ai-sdk/google-vertex": { "optional": true },
110
+ "@ai-sdk/openai": { "optional": true }
117
111
  },
118
112
  "devDependencies": {
119
113
  "@ai-sdk/anthropic": "^3.0.53",