libretto 0.5.0 → 0.5.2

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 (122) hide show
  1. package/README.md +109 -35
  2. package/dist/cli/cli.js +22 -97
  3. package/dist/cli/commands/browser.js +86 -59
  4. package/dist/cli/commands/execution.js +199 -86
  5. package/dist/cli/commands/init.js +34 -29
  6. package/dist/cli/commands/logs.js +4 -5
  7. package/dist/cli/commands/shared.js +30 -29
  8. package/dist/cli/commands/snapshot.js +26 -39
  9. package/dist/cli/core/ai-config.js +21 -4
  10. package/dist/cli/core/api-snapshot-analyzer.js +15 -5
  11. package/dist/cli/core/browser.js +207 -37
  12. package/dist/cli/core/context.js +4 -1
  13. package/dist/cli/core/session-telemetry.js +434 -174
  14. package/dist/cli/core/session.js +21 -8
  15. package/dist/cli/core/snapshot-analyzer.js +14 -31
  16. package/dist/cli/core/snapshot-api-config.js +2 -6
  17. package/dist/cli/core/telemetry.js +20 -4
  18. package/dist/cli/framework/simple-cli.js +45 -25
  19. package/dist/cli/router.js +14 -21
  20. package/dist/cli/workers/run-integration-runtime.js +24 -5
  21. package/dist/cli/workers/run-integration-worker-protocol.js +3 -1
  22. package/dist/cli/workers/run-integration-worker.js +1 -4
  23. package/dist/index.d.ts +1 -2
  24. package/dist/index.js +7 -10
  25. package/dist/runtime/download/download.js +5 -1
  26. package/dist/runtime/extract/extract.js +11 -2
  27. package/dist/runtime/network/network.js +8 -1
  28. package/dist/runtime/recovery/agent.js +6 -2
  29. package/dist/runtime/recovery/errors.js +3 -1
  30. package/dist/runtime/recovery/recovery.js +3 -1
  31. package/dist/shared/condense-dom/condense-dom.js +17 -69
  32. package/dist/shared/config/config.d.ts +1 -9
  33. package/dist/shared/config/config.js +0 -18
  34. package/dist/shared/config/index.d.ts +2 -1
  35. package/dist/shared/config/index.js +0 -10
  36. package/dist/shared/debug/pause.js +9 -3
  37. package/dist/shared/dom-semantics.d.ts +8 -0
  38. package/dist/shared/dom-semantics.js +69 -0
  39. package/dist/shared/instrumentation/instrument.js +101 -5
  40. package/dist/shared/llm/ai-sdk-adapter.js +3 -1
  41. package/dist/shared/llm/client.js +3 -1
  42. package/dist/shared/logger/index.js +4 -1
  43. package/dist/shared/run/api.js +3 -1
  44. package/dist/shared/run/browser.js +47 -3
  45. package/dist/shared/state/session-state.d.ts +2 -1
  46. package/dist/shared/state/session-state.js +5 -2
  47. package/dist/shared/visualization/ghost-cursor.js +36 -14
  48. package/dist/shared/visualization/highlight.js +9 -6
  49. package/dist/shared/workflow/workflow.d.ts +4 -5
  50. package/dist/shared/workflow/workflow.js +3 -5
  51. package/package.json +6 -2
  52. package/scripts/check-skills-sync.mjs +25 -0
  53. package/scripts/compare-eval-summary.mjs +47 -0
  54. package/scripts/postinstall.mjs +15 -15
  55. package/scripts/prepare-release.sh +97 -0
  56. package/scripts/skills-libretto.mjs +103 -0
  57. package/scripts/summarize-evals.mjs +135 -0
  58. package/scripts/sync-skills.mjs +12 -0
  59. package/skills/libretto/SKILL.md +132 -54
  60. package/skills/libretto/references/action-logs.md +101 -0
  61. package/skills/libretto/references/auth-profiles.md +1 -2
  62. package/skills/libretto/references/code-generation-rules.md +210 -0
  63. package/skills/libretto/references/configuration-file-reference.md +53 -0
  64. package/skills/libretto/references/pages-and-page-targeting.md +1 -1
  65. package/skills/libretto/references/site-security-review.md +143 -0
  66. package/src/cli/cli.ts +23 -110
  67. package/src/cli/commands/browser.ts +94 -70
  68. package/src/cli/commands/execution.ts +233 -102
  69. package/src/cli/commands/init.ts +37 -33
  70. package/src/cli/commands/logs.ts +7 -7
  71. package/src/cli/commands/shared.ts +36 -37
  72. package/src/cli/commands/snapshot.ts +44 -59
  73. package/src/cli/core/ai-config.ts +24 -4
  74. package/src/cli/core/api-snapshot-analyzer.ts +17 -6
  75. package/src/cli/core/browser.ts +260 -49
  76. package/src/cli/core/context.ts +7 -2
  77. package/src/cli/core/session-telemetry.ts +449 -197
  78. package/src/cli/core/session.ts +21 -7
  79. package/src/cli/core/snapshot-analyzer.ts +26 -46
  80. package/src/cli/core/snapshot-api-config.ts +170 -175
  81. package/src/cli/core/telemetry.ts +39 -4
  82. package/src/cli/framework/simple-cli.ts +144 -77
  83. package/src/cli/router.ts +13 -21
  84. package/src/cli/workers/run-integration-runtime.ts +36 -9
  85. package/src/cli/workers/run-integration-worker-protocol.ts +2 -0
  86. package/src/cli/workers/run-integration-worker.ts +1 -4
  87. package/src/index.ts +73 -66
  88. package/src/runtime/download/download.ts +62 -58
  89. package/src/runtime/download/index.ts +5 -5
  90. package/src/runtime/extract/extract.ts +71 -61
  91. package/src/runtime/network/index.ts +3 -3
  92. package/src/runtime/network/network.ts +99 -93
  93. package/src/runtime/recovery/agent.ts +217 -212
  94. package/src/runtime/recovery/errors.ts +107 -104
  95. package/src/runtime/recovery/index.ts +3 -3
  96. package/src/runtime/recovery/recovery.ts +38 -35
  97. package/src/shared/condense-dom/condense-dom.ts +27 -82
  98. package/src/shared/config/config.ts +0 -19
  99. package/src/shared/config/index.ts +0 -5
  100. package/src/shared/debug/pause.ts +57 -51
  101. package/src/shared/dom-semantics.ts +68 -0
  102. package/src/shared/instrumentation/errors.ts +64 -62
  103. package/src/shared/instrumentation/index.ts +5 -5
  104. package/src/shared/instrumentation/instrument.ts +339 -209
  105. package/src/shared/llm/ai-sdk-adapter.ts +58 -55
  106. package/src/shared/llm/client.ts +181 -174
  107. package/src/shared/llm/types.ts +39 -39
  108. package/src/shared/logger/index.ts +11 -4
  109. package/src/shared/logger/logger.ts +312 -306
  110. package/src/shared/logger/sinks.ts +118 -114
  111. package/src/shared/paths/paths.ts +50 -49
  112. package/src/shared/paths/repo-root.ts +17 -17
  113. package/src/shared/run/api.ts +5 -1
  114. package/src/shared/run/browser.ts +65 -3
  115. package/src/shared/state/index.ts +9 -9
  116. package/src/shared/state/session-state.ts +46 -43
  117. package/src/shared/visualization/ghost-cursor.ts +180 -149
  118. package/src/shared/visualization/highlight.ts +89 -86
  119. package/src/shared/visualization/index.ts +13 -13
  120. package/src/shared/workflow/workflow.ts +19 -25
  121. package/skills/libretto/references/reverse-engineering-network-requests.md +0 -39
  122. package/skills/libretto/references/user-action-log.md +0 -31
@@ -1,43 +1,47 @@
1
1
  ---
2
2
  name: libretto
3
- description: "Browser automation CLI for inspecting live pages, prototyping interactions, and running browser workflows."
3
+ description: "Browser automation CLI for building, maintaining, and running browser automation workflows by inspecting live pages and prototyping interactions."
4
4
  license: MIT
5
5
  metadata:
6
6
  author: saffron-health
7
- version: "0.4.0"
7
+ version: "0.4.2"
8
8
  ---
9
9
 
10
- # Libretto
10
+ ## How Libretto Works
11
11
 
12
- Use `npx libretto` to inspect live browser state, prototype interactions, and run existing browser workflows.
12
+ - Libretto is a CLI for exploring live websites and building or debugging reusable browser automation scripts.
13
+ - Use Libretto commands to inspect the site and open pages, observe state, inspect requests, and prototype interactions.
14
+ - Libretto work must end in script changes. Create or edit the workflow file instead of stopping at interactive exploration.
13
15
 
14
- ## Intro
16
+ ## Default Integration Approach
15
17
 
16
- - Use this skill when the truth is on the page.
17
- - Prefer Libretto when you need to see what the browser is doing, not when you only need to edit source files.
18
- - Treat Libretto as a session-based workflow: open a page, inspect it, try a focused action, then turn what you learned into code outside the CLI.
19
- - When building a new integration, prefer reverse-engineering network requests first. Fall back to browser automation when the request path is unclear, too fragile, or blocked by anti-bot systems.
18
+ - Prefer network requests first for new integrations unless the user explicitly asks for Playwright or UI automation, then do not use the site's internal API.
19
+ - Read `references/site-security-review.md` before committing to a network-first approach on a new site.
20
+ - Fall back to passive interception or Playwright-driven UI automation when the security review rules network requests out, the request path is not workable, or the user explicitly asks for Playwright.
20
21
 
21
22
  ## Setup
22
23
 
23
- - Ask the user to set up snapshot analysis before relying on `snapshot` for page understanding.
24
- - Use `npx libretto init` for first-time setup.
25
- - If they already have credentials, `npx libretto ai configure openai|anthropic|gemini|vertex` is enough.
24
+ - Use `npx libretto init` for first-time workspace setup (sets up config file and snapshot command).
25
+ - If credentials are already available, `npx libretto ai configure openai|anthropic|gemini|vertex` is usually enough.
26
26
 
27
- ## Rules
27
+ ## Working Rules
28
28
 
29
29
  - Announce which session you are using and what page you are on.
30
30
  - Ask instead of guessing when it is unclear what to click, type, or submit.
31
- - Use `snapshot` to understand unknown page state before trying multiple selectors.
31
+ - Do not treat visibility as interactivity. If an element will not act, inspect blockers before retrying.
32
+ - Defer repo/code review until you begin generating code, unless the user explicitly asks for it earlier.
33
+ - Read and follow guidelines in `references/code-generation-rules.md` before generating or editing production workflow code.
34
+ - Validation requires a successful clean `run --headless` with confirmation of the actual returned output, not just process success. If the user wants to watch the finished workflow, do a final headed `run` after headless validation succeeds.
35
+ - Treat exploration sessions as disposable unless the user explicitly wants one kept open.
32
36
  - Get explicit user confirmation before mutating actions or replaying network requests that may have side effects.
33
37
  - Never run multiple `exec` commands at the same time.
34
- - Keep the browser session open until the user says the session is done.
35
38
 
36
39
  ## Commands
37
40
 
38
41
  ### `open`
39
42
 
40
43
  - Open a page before using `exec` or `snapshot`.
44
+ - Use `open` at the start of script authoring when you need live page state to decide how the workflow should work.
41
45
  - Use headed mode when the user needs to log in or watch the workflow.
42
46
 
43
47
  ```bash
@@ -45,47 +49,133 @@ npx libretto open https://example.com --headed
45
49
  npx libretto open https://example.com --headless --session debug-example
46
50
  ```
47
51
 
48
- ### `exec`
52
+ ### `connect`
49
53
 
50
- - Use `exec` for focused inspection and short-lived interaction experiments.
51
- - Let failures throw. Do not hide `exec` failures with `try/catch`.
54
+ - Use `connect` to attach to any existing Chrome DevTools Protocol (CDP) endpoint — a browser started with `--remote-debugging-port`, an Electron app, or any other CDP-compatible target.
55
+ - After connecting, `exec`, `snapshot`, `pages`, and all other session commands work normally.
56
+ - Libretto does not manage the connected process's lifecycle. `close` clears the session but does not terminate the remote process.
52
57
 
53
58
  ```bash
54
- npx libretto exec "return await page.url()"
55
- npx libretto exec "return await page.locator('button').count()"
56
- npx libretto exec --visualize "await page.locator('button:has-text(\"Continue\")').click()"
59
+ npx libretto connect http://127.0.0.1:9222 --session my-session
60
+ npx libretto connect http://127.0.0.1:9223 --session another-session
57
61
  ```
58
62
 
59
63
  ### `snapshot`
60
64
 
61
65
  - Use `snapshot` as the primary page observation tool.
62
- - When you want analysis, provide both `--objective` and `--context`.
63
- - If you only need the PNG and HTML files, omit `--objective`. That runs capture-only mode and skips AI analysis.
64
- - When using `--objective`, expect analysis to take time. Use a timeout of at least 2 minutes for shell-wrapped calls.
66
+ - Always provide both `--objective` and `--context`.
67
+ - A single snapshot objective can include multiple questions or analysis tasks.
68
+ - Use it before guessing at selectors, after workflow failures, and whenever the visible page state is unclear.
69
+ - When analysis is involved, expect it to take time. Use a timeout of at least 2 minutes for shell-wrapped calls.
65
70
 
66
71
  ```bash
67
- npx libretto snapshot
68
72
  npx libretto snapshot \
69
73
  --objective "Find the sign-in form and submit button" \
70
74
  --context "I just opened the login page and need the email field, password field, and submit button."
71
75
  npx libretto snapshot \
76
+ --session debug-example \
77
+ --page <page-id> \
72
78
  --objective "Explain why the table is empty" \
73
- --context "I opened the referrals page and expected rows after applying filters."
79
+ --context "I opened the referrals page, applied filters, and expected rows to appear."
80
+ ```
81
+
82
+ ### `exec`
83
+
84
+ - Use `exec` for focused inspection and short-lived interaction experiments.
85
+ - Use `exec` to validate selectors, inspect data, or prototype a step before you encode it in the workflow file.
86
+ - Available globals: `page`, `context`, `browser`, `state`, `fetch`, `Buffer`.
87
+ - Let failures throw. Do not hide `exec` failures with `try/catch` or `.catch()`.
88
+ - Do not run multiple `exec` commands in parallel.
89
+
90
+ ```bash
91
+ npx libretto exec "return await page.url()"
92
+ npx libretto exec "return await page.locator('button').count()"
93
+ npx libretto exec "await page.locator('button:has-text(\"Continue\")').click()"
94
+ ```
95
+
96
+ ### `pages`
97
+
98
+ - Use `pages` when a popup, new tab, or second page appears.
99
+ - If `exec`, `snapshot`, `network`, or `actions` complains about multiple pages, list page ids first and then pass `--page`.
100
+
101
+ ```bash
102
+ npx libretto pages --session debug-example
103
+ npx libretto exec --session debug-example --page <page-id> "return await page.url()"
74
104
  ```
75
105
 
76
106
  ### `run`
77
107
 
78
- - Use `run` to execute an existing Libretto workflow.
108
+ - Use `run` to verify a workflow file after creating it or editing it, preferring `run --headless` for the normal fix/verify loop.
109
+ - Plain `run` defaults to headed mode.
79
110
  - If the workflow fails, Libretto keeps the browser open. Inspect the failed state with `snapshot` and `exec` before editing code.
111
+ - Insert `await pause(session)` statements in the workflow file when you need to stop at specific states for interactive debugging, like breakpoints in the browser flow.
80
112
  - If the workflow pauses, resume it with `npx libretto resume --session <name>`.
81
113
  - Re-run the same workflow after each fix to verify the browser behavior end to end.
82
114
 
83
115
  ```bash
84
- npx libretto run ./integration.ts main
85
- npx libretto run ./integration.ts main --params '{"status":"open"}'
86
- npx libretto run ./integration.ts main --auth-profile app.example.com --headed
116
+ npx libretto run ./integration.ts main --headless --params '{"status":"open"}'
117
+ npx libretto run ./integration.ts main --auth-profile app.example.com
118
+ ```
119
+
120
+ ### `resume`
121
+
122
+ - Workflows pause by calling `await pause("session-name")` in the workflow file. Import `pause` from `"libretto"`.
123
+ - `pause(session)` is a no-op when `NODE_ENV === "production"`.
124
+ - Use `resume` when a workflow hit a `pause()` call.
125
+ - Keep resuming the same session until the workflow completes or pauses again.
126
+
127
+ ```bash
128
+ npx libretto resume --session debug-example
129
+ ```
130
+
131
+ ### `save`
132
+
133
+ - Use `save` only when the user explicitly asks to save or reuse authenticated browser state.
134
+
135
+ ```bash
136
+ npx libretto save app.example.com
87
137
  ```
88
138
 
139
+ ### `close`
140
+
141
+ - Use `close` when the user is done with the session or an exploration session is no longer helping progress (unless the user asked to keep watching that browser).
142
+ - `close --all` is available for workspace cleanup.
143
+
144
+ ```bash
145
+ npx libretto close --session debug-example
146
+ npx libretto close --all
147
+ ```
148
+
149
+ ## Session Logs
150
+
151
+ Session state is stored in `.libretto/sessions/<session>/state.json`.
152
+
153
+ Session logs are JSONL files at `.libretto/sessions/<session>/`:
154
+
155
+ - CLI logs are in `.libretto/sessions/<session>/logs.jsonl`.
156
+ - Action logs are in `.libretto/sessions/<session>/actions.jsonl`.
157
+ - Network logs are in `.libretto/sessions/<session>/network.jsonl`.
158
+
159
+ Use `jq` to query jsonl logs directly — for any filtering, slicing, or inspection task.
160
+
161
+ ```bash
162
+ # Last 20 action entries
163
+ tail -n 20 .libretto/sessions/<session>/actions.jsonl | jq .
164
+
165
+ # POST requests only
166
+ jq 'select(.method == "POST")' .libretto/sessions/<session>/network.jsonl
167
+ ```
168
+
169
+ ### Action log (`actions.jsonl`)
170
+
171
+ Key fields: `ts` (ISO timestamp), `source` (`user` or `agent`), `action` (`click`, `fill`, `goto`, etc.), `selector` (locator used by the agent), `bestSemanticSelector` (canonical selector for user DOM events), `success` (boolean), `url` (navigation target), `value` (typed or submitted value), `error` (message on failure).
172
+
173
+ Read `references/action-logs.md` for full field descriptions and user-vs-agent entry semantics.
174
+
175
+ ### Network log (`network.jsonl`)
176
+
177
+ Key fields: `ts` (ISO timestamp), `method` (HTTP method, e.g. `GET`, `POST`), `url` (request URL), `status` (HTTP status code), `contentType` (response content type), `responseBody` (response body string, may be null).
178
+
89
179
  ## Examples
90
180
 
91
181
  ### Building new browser automation workflows
@@ -95,31 +185,16 @@ npx libretto run ./integration.ts main --auth-profile app.example.com --headed
95
185
  ```text
96
186
  <example>
97
187
  [Context: The user wants to build a new browser workflow and does not yet know the page structure]
98
- Assistant: Let me open the target page in headed mode so we can inspect the real workflow.
188
+ Assistant: I'll inspect the real site first if needed, but before I finish I'll create `target-workflow.ts` so the task produces reusable automation code.
99
189
  Assistant: [Runs `npx libretto open https://target.example.com --headed`]
100
- Assistant: I'll first determine whether this flow can be built from captured network requests instead of UI-only automation.
101
- Assistant: [Runs `npx libretto snapshot --objective "Find the next required action" --context "We are starting the workflow from the landing page."`]
102
- Assistant: If the page exposes a clear and stable request path, I'll prioritize that. If the request path is unclear, fragile, or heavily defended, I'll fall back to browser automation.
103
- Assistant: [Uses `network`, `snapshot`, and `exec` as needed to prove the approach]
190
+ Assistant: [Reads `references/site-security-review.md` before choosing between passive network inspection, direct browser fetch calls, and Playwright-first automation]
191
+ Assistant: [Runs `npx libretto snapshot --objective "Find the next required action" --context "We are starting the workflow from the landing page and need the first meaningful step."`]
192
+ Assistant: [Uses `network`, `snapshot`, and `exec` as needed to understand the site and decide the implementation path]
193
+ Assistant: [Reads `references/code-generation-rules.md` before writing production workflow code]
104
194
  Assistant: I found the working path. I'll now update the workflow file outside Libretto and verify it with `npx libretto run ...`.
105
195
  </example>
106
196
  ```
107
197
 
108
- #### Network requests
109
-
110
- ```text
111
- <example>
112
- [Context: The user wants to build an integration using network requests]
113
- Assistant: [Reads `references/reverse-engineering-network-requests.md`]
114
- Assistant: Let me open the page in headed mode. Perform the workflow and I'll use the network log to recreate it.
115
- Assistant: [Runs `npx libretto open https://target.example.com --headed`]
116
- [User performs workflow]
117
- User: I've completed the workflow
118
- Assistant: [Runs `npx libretto network --method POST --last 20`]
119
- Assistant: I found the relevant requests. I'll recreate the workflow from those requests, then test the resulting script with `npx libretto run ...`.
120
- </example>
121
- ```
122
-
123
198
  ### Debugging existing workflows
124
199
 
125
200
  ```text
@@ -130,13 +205,16 @@ Assistant: [Runs `npx libretto run ./integration.ts main --session debug-flow --
130
205
  Assistant: The workflow failed and Libretto kept the browser open. I'll inspect the page state before changing code.
131
206
  Assistant: [Runs `npx libretto snapshot --session debug-flow --objective "Find the blocking error or broken selector target" --context "The workflow just failed after trying to continue from the review step, and I need to identify the visible blocker on the current page."`]
132
207
  Assistant: [Runs `npx libretto exec --session debug-flow "...focused inspection or prototype..."`]
208
+ Assistant: [Reads `references/code-generation-rules.md` before patching the workflow file]
133
209
  Assistant: I found the issue. I'll patch the workflow code, then rerun `npx libretto run ...` to verify the fix.
134
210
  </example>
135
211
  ```
136
212
 
137
213
  ## References
138
214
 
139
- - For reverse-engineering captured requests, read `references/reverse-engineering-network-requests.md`.
140
- - For incorporating manual browser steps the user performed, read `references/user-action-log.md`.
141
- - For saving and reusing login state, read `references/auth-profiles.md`.
142
- - For multiple open pages and page targeting, read `references/pages-and-page-targeting.md`.
215
+ - Read `references/configuration-file-reference.md` when you need to inspect or change `.libretto/config.json` for snapshot model selection or viewport defaults.
216
+ - Read `references/site-security-review.md` before reviewing the site's security posture and deciding whether to lead with network requests, passive interception, or Playwright DOM automation on a new site.
217
+ - Read `references/code-generation-rules.md` before writing or editing production workflow files.
218
+ - Read `references/auth-profiles.md` when auth-profile behavior is relevant.
219
+ - Read `references/pages-and-page-targeting.md` when a session has multiple open pages or you need `--page`.
220
+ - Read `references/action-logs.md` for full action log field descriptions and user-vs-agent event semantics.
@@ -0,0 +1,101 @@
1
+ # Action Logs
2
+
3
+ - Stored at `.libretto/sessions/<session>/actions.jsonl`.
4
+ - One JSON object per line.
5
+ - Query the file directly with `jq`, for example `tail -n 20 .libretto/sessions/<session>/actions.jsonl | jq .`.
6
+ - This is an orientation log, not a replay trace.
7
+
8
+ ## User vs Agent
9
+
10
+ - `agent` entries log the Playwright action Libretto observed, usually as `action` plus `selector`, and sometimes `value`, `url`, `duration`, or `error`.
11
+ - `user` entries log the browser DOM event Libretto captured, so they can include `bestSemanticSelector`, `targetSelector`, `ancestorSelectors`, `nearbyText`, `composedPath`, and `coordinates`.
12
+ - This is why agent entries usually describe what Playwright tried to do, while user entries can describe what element was actually interacted with in the page.
13
+
14
+ ## Fields
15
+
16
+ - `ts`
17
+ ISO timestamp.
18
+
19
+ - `pageId`
20
+ Playwright target id for the page that produced the entry.
21
+
22
+ - `action`
23
+ Logged action name, such as `click`, `dblclick`, `fill`, `goto`, or `reload`.
24
+
25
+ - `source`
26
+ `user` for captured DOM events, `agent` for logged Playwright calls.
27
+
28
+ - `success`
29
+ `true` if the action completed, `false` if Libretto logged a failure.
30
+
31
+ - `selector`
32
+ Selector or locator hint for agent entries.
33
+
34
+ - `bestSemanticSelector`
35
+ Canonical selector for user DOM events.
36
+
37
+ - `targetSelector`
38
+ Selector for the raw DOM event target. Usually only present for user DOM events.
39
+
40
+ - `ancestorSelectors`
41
+ Meaningful ancestor selector candidates for a user DOM event. Ordered from closest meaningful ancestor to farthest meaningful ancestor.
42
+
43
+ - `nearbyText`
44
+ Short visible text near the event target, used as human context.
45
+
46
+ - `composedPath`
47
+ Compact event-path summaries. Ordered from the raw event target to farthest ancestor.
48
+
49
+ - `coordinates`
50
+ Rounded `clientX` and `clientY` for pointer-style events:
51
+
52
+ ```json
53
+ { "x": 42, "y": 24 }
54
+ ```
55
+
56
+ - `value`
57
+ Typed, selected, or submitted value when the action had one.
58
+
59
+ - `url`
60
+ Navigation target or recorded page URL for navigation-style actions.
61
+
62
+ - `duration`
63
+ Elapsed time in milliseconds when Libretto recorded it.
64
+
65
+ - `error`
66
+ Error message when the action failed.
67
+
68
+ ## User Example
69
+
70
+ ```json
71
+ {
72
+ "ts": "2026-03-20T22:34:56.123Z",
73
+ "pageId": "A1B2C3D4E5F6",
74
+ "action": "dblclick",
75
+ "source": "user",
76
+ "bestSemanticSelector": "button#saveBtn",
77
+ "targetSelector": "span",
78
+ "ancestorSelectors": ["button#saveBtn", "form[action=\"/save\"]"],
79
+ "nearbyText": "Save",
80
+ "composedPath": ["span [text=\"Save\"]", "button#saveBtn [text=\"Save\"]"],
81
+ "coordinates": {
82
+ "x": 42,
83
+ "y": 24
84
+ },
85
+ "success": true
86
+ }
87
+ ```
88
+
89
+ ## Agent Example
90
+
91
+ ```json
92
+ {
93
+ "ts": "2026-03-20T22:35:10.456Z",
94
+ "pageId": "A1B2C3D4E5F6",
95
+ "action": "click",
96
+ "source": "agent",
97
+ "selector": "page.getByRole(\"button\", {\"name\":\"Save\"})",
98
+ "duration": 187,
99
+ "success": true
100
+ }
101
+ ```
@@ -1,12 +1,11 @@
1
1
  # Auth Profiles
2
2
 
3
- Use this reference when the target site requires login and the user wants to reuse local authenticated browser state.
3
+ Use this reference only when the user explicitly asks to save or reuse local authenticated browser state.
4
4
 
5
5
  ## When to Use This
6
6
 
7
7
  - The site requires manual login.
8
8
  - The user is running workflows locally.
9
- - Reusing a saved session is simpler than building credential-handling logic into the workflow.
10
9
 
11
10
  ## Workflow
12
11
 
@@ -0,0 +1,210 @@
1
+ # Code Generation Rules
2
+
3
+ These rules apply when generating production TypeScript files from interactive browser sessions. Read this file before writing any production code.
4
+
5
+ Follow the user's existing codebase conventions, abstractions, and patterns whenever possible. Do not introduce a new style unless the codebase does not already have a suitable one.
6
+
7
+ ## Workflow File Structure
8
+
9
+ Generated files must export a `workflow()` instance so they can be run via `npx libretto run <file> <exportName>`. Import `workflow` and its types from `"libretto"`:
10
+
11
+ ```typescript
12
+ import { workflow, pause, type LibrettoWorkflowContext } from "libretto";
13
+
14
+ type Input = {
15
+ // Define the expected input shape — passed via --params JSON
16
+ query: string;
17
+ maxResults?: number;
18
+ };
19
+
20
+ type Output = {
21
+ // Define what the workflow returns
22
+ results: Array<{ name: string; value: string }>;
23
+ };
24
+
25
+ export const myWorkflow = workflow<Input, Output>(
26
+ async (ctx, input): Promise<Output> => {
27
+ const { session, page, logger } = ctx;
28
+
29
+ logger.info("workflow-start", { session, query: input.query });
30
+ await page.goto("https://example.com");
31
+ await pause(session);
32
+
33
+ return { results: [] };
34
+ },
35
+ );
36
+ ```
37
+
38
+ Key points:
39
+
40
+ - The named export (e.g., `myWorkflow`) is what you pass as the second arg to `npx libretto run ./file.ts myWorkflow`
41
+ - `workflow(handler)` returns a branded workflow object with a `.run(ctx, input)` method. The CLI expects that contract.
42
+ - `ctx` provides `session`, `page`, `logger`, and `services` (generic, default `{}`)
43
+ - `input` comes from `--params '{"query":"foo"}'` or `--params-file params.json` on the CLI
44
+ - Use `await pause(ctx.session)` (or `await pause(session)`) to pause the workflow for debugging. It is a no-op in production.
45
+ - After validation is complete and the workflow is confirmed working end to end, remove all `pause()` calls and pause-only workflow params unless the user explicitly says to keep them.
46
+ - The browser is launched and closed automatically by the CLI. Do not launch or close it in the handler.
47
+
48
+ ## Passing Application Dependencies via Services
49
+
50
+ Use the third generic on `workflow<Input, Output, Services>` to inject
51
+ dependencies that exist in your application but not in libretto's runtime
52
+ (DB transactions, API clients, caches, etc.):
53
+
54
+ ```typescript
55
+ import { type Transaction } from "./db";
56
+
57
+ type MyServices = { tx?: Transaction };
58
+
59
+ export const myWorkflow = workflow<Input, Output, MyServices>(
60
+ async (ctx, input) => {
61
+ if (ctx.services.tx) {
62
+ await ctx.services.tx.insert(/* ... */);
63
+ } else {
64
+ ctx.logger.info("No DB transaction — skipping write");
65
+ }
66
+ // ... browser automation ...
67
+ },
68
+ );
69
+ ```
70
+
71
+ In production, the caller passes services when invoking `.run()`:
72
+
73
+ ```typescript
74
+ await myWorkflow.run(
75
+ { session: "debug-flow", page, logger, services: { tx } },
76
+ input,
77
+ );
78
+ ```
79
+
80
+ When running standalone via `npx libretto run`, services defaults to `{}`,
81
+ so mark fields optional for anything unavailable in that context.
82
+
83
+ ## Playwright DOM Interaction Rules
84
+
85
+ Generated code must use Playwright locator APIs for all DOM interactions. Do not use `page.evaluate()` with `document.querySelector`, `querySelectorAll`, `textContent`, `click()`, or other DOM APIs when a Playwright locator can do the same thing.
86
+
87
+ During the interactive `exec` phase, `page.evaluate` is fine for quick prototyping. In generated production code, translate those patterns into Playwright locators.
88
+
89
+ Before extracting data (for example text, rows, or field values), wait for the target content itself to be ready, not just its container.
90
+
91
+ ### Anti-Patterns
92
+
93
+ These patterns come up frequently during interactive sessions and should not carry over into production code:
94
+
95
+ ```typescript
96
+ // DON'T — batch-read via evaluate string
97
+ const data = await page.evaluate(`(() => {
98
+ const posts = document.querySelectorAll('.post');
99
+ return Array.from(posts).map(p => ({
100
+ name: p.querySelector('.name')?.textContent,
101
+ content: p.querySelector('.content')?.textContent,
102
+ }));
103
+ })()`);
104
+
105
+ // DO — Playwright locators with a loop
106
+ const posts = await page.locator(".post").all();
107
+ for (const post of posts) {
108
+ const name = await post.locator(".name").textContent();
109
+ const content = await post.locator(".content").textContent();
110
+ }
111
+ ```
112
+
113
+ ```typescript
114
+ // DON'T — evaluate to count elements
115
+ const count = await el.evaluate(`(el) => el.querySelectorAll('.item').length`);
116
+
117
+ // DO
118
+ const count = await el.locator(".item").count();
119
+ ```
120
+
121
+ ```typescript
122
+ // DON'T — evaluate to read scoped text
123
+ const text = await post.evaluate(
124
+ `(el) => el.querySelector('[data-view-name="foo"]')?.textContent`,
125
+ );
126
+
127
+ // DO
128
+ const text = await post.locator('[data-view-name="foo"]').textContent();
129
+ ```
130
+
131
+ ### When `page.evaluate()` Is Acceptable
132
+
133
+ Use `page.evaluate()` only for operations that have no Playwright locator equivalent:
134
+
135
+ 1. Browser-native APIs: `getComputedStyle()`, `window.*` globals, `document.cookie`, scroll position
136
+ 2. In-browser `fetch()` calls: making HTTP requests from the browser context
137
+ 3. Parsing operations: using `DOMParser` to parse HTML/XML strings inside the browser
138
+
139
+ A quick test: if the evaluate body contains `querySelector`, `querySelectorAll`, `textContent`, `click()`, `getAttribute()`, or iterates DOM elements, it should be rewritten with Playwright locators.
140
+
141
+ When `page.evaluate()` is used for the acceptable cases above, keep the logic self-contained and return JSON-serializable values:
142
+
143
+ ```typescript
144
+ const data = (await page.evaluate(`(() => {
145
+ const style = getComputedStyle(document.documentElement);
146
+ return style.getPropertyValue('--brand-color');
147
+ })()`)) as string;
148
+ ```
149
+
150
+ Do not rely on broad DOM querying inside `page.evaluate()` for production flows when Playwright locators can express the same interaction.
151
+
152
+ ## Network Request Methods
153
+
154
+ When codifying network-based data extraction or form submissions, wrap `page.evaluate(() => fetch(...))` calls in typed methods on a shared API client class:
155
+
156
+ ```typescript
157
+ class ApiClient {
158
+ constructor(private page: Page) {}
159
+
160
+ private async apiFetch(
161
+ url: string,
162
+ options?: { method?: string; body?: string },
163
+ ): Promise<string> {
164
+ return await this.page.evaluate(
165
+ async ({ url, method, body }) => {
166
+ const init: RequestInit = { method: method ?? "GET" };
167
+ if (body) {
168
+ init.headers = {
169
+ "Content-Type": "application/x-www-form-urlencoded",
170
+ };
171
+ init.body = body;
172
+ }
173
+ const response = await fetch(url, init);
174
+ if (!response.ok) throw new Error(`${response.status} for ${url}`);
175
+ return await response.text();
176
+ },
177
+ { url, method: options?.method, body: options?.body },
178
+ );
179
+ }
180
+
181
+ async fetchReferralList(status: string): Promise<Referral[]> {
182
+ const raw = await this.apiFetch(`/api/referrals?status=${status}`);
183
+ // parse and return typed data
184
+ }
185
+ }
186
+ ```
187
+
188
+ One method per endpoint. No try/catch in API methods. Let errors propagate to the orchestrator. Parse XML/HTML inside `page.evaluate()` with `DOMParser`. Use string expressions for `page.evaluate()` to avoid DOM type errors.
189
+
190
+ ## Comments
191
+
192
+ Add comments throughout generated code to explain what each logical block is doing. Comments should describe intent, not restate the code. Group related actions under a single comment rather than commenting every line.
193
+
194
+ ```typescript
195
+ // Log in with credentials
196
+ await page.locator("#username").fill(user);
197
+ await page.locator("#password").fill(pass);
198
+ await page.locator("#login").click();
199
+
200
+ // Extract author and content from each feed post
201
+ const posts = await page.locator(".post").all();
202
+ for (const post of posts) {
203
+ const name = await post.locator(".name").textContent();
204
+ const content = await post.locator(".content").textContent();
205
+ }
206
+ ```
207
+
208
+ ## Type Checking
209
+
210
+ The generated file must pass `npx tsc --noEmit` before it's considered done. If there are type errors around DOM access, prefer locator APIs first, then use focused `page.evaluate()` only for browser-native APIs.
@@ -0,0 +1,53 @@
1
+ # Configuration File Reference
2
+
3
+ Use this reference when you need to inspect or change the workspace configuration that powers `snapshot` analysis and default viewport behavior.
4
+
5
+ ## When to Use This
6
+
7
+ - You want to confirm which AI model `snapshot` will use.
8
+ - You want to understand where Libretto stores workspace-level settings.
9
+ - You want a persistent default viewport for `open` or `run`.
10
+
11
+ ## File Location
12
+
13
+ Libretto reads workspace config from `.libretto/config.json`.
14
+
15
+ - The file is usually created or updated by `npx libretto ai configure ...`.
16
+ - API credentials still come from your shell environment or `.env`. The config file stores the selected model, not the secret itself.
17
+ - For first-time setup instructions, follow the main `SKILL.md` flow instead of expanding this reference.
18
+
19
+ ## Supported Settings
20
+
21
+ - `ai.model` selects the configured analysis model for `snapshot`.
22
+ - `viewport` is an optional top-level setting used by `open` and `run` when you do not pass `--viewport`.
23
+ - Viewport precedence is: CLI `--viewport`, then `.libretto/config.json`, then the default `1366x768`.
24
+
25
+ Example:
26
+
27
+ ```json
28
+ {
29
+ "version": 1,
30
+ "ai": {
31
+ "model": "openai/gpt-5.4",
32
+ "updatedAt": "2026-01-01T00:00:00.000Z"
33
+ },
34
+ "viewport": {
35
+ "width": 1280,
36
+ "height": 800
37
+ }
38
+ }
39
+ ```
40
+
41
+ ## Common Commands
42
+
43
+ ```bash
44
+ npx libretto init
45
+ npx libretto ai configure openai
46
+ npx libretto open https://app.example.com --viewport 1440x900
47
+ npx libretto run ./integration.ts main --viewport 1440x900
48
+ ```
49
+
50
+ ## Notes
51
+
52
+ - If you want a persistent default viewport for the workspace, add `viewport` to `.libretto/config.json` instead of repeating `--viewport` on every command.
53
+ - If `snapshot` analysis is not configured yet, return to the setup steps in the main `SKILL.md` flow.
@@ -26,4 +26,4 @@ npx libretto snapshot --session debug-flow --page <page-id> --objective "Find th
26
26
 
27
27
  - A session can contain more than one page.
28
28
  - When multiple pages are open, think about page targeting first before debugging selectors.
29
- - Use `pages` to resolve the correct page id, then pass `--page` to `exec`, `snapshot`, `network`, or `actions` when needed.
29
+ - Use `pages` to resolve the correct page id, then pass `--page` to `exec` or `snapshot` when needed.