libretto 0.4.2 → 0.5.0

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 (161) hide show
  1. package/dist/cli/cli.js +20 -19
  2. package/dist/cli/commands/ai.js +1 -1
  3. package/dist/cli/commands/browser.js +3 -3
  4. package/dist/cli/commands/execution.js +3 -3
  5. package/dist/cli/commands/init.js +30 -21
  6. package/dist/cli/commands/logs.js +1 -1
  7. package/dist/cli/commands/snapshot.js +6 -1
  8. package/dist/cli/core/ai-config.js +62 -12
  9. package/dist/cli/core/browser.js +11 -6
  10. package/dist/cli/core/context.js +4 -18
  11. package/dist/cli/core/session.js +2 -2
  12. package/dist/cli/core/snapshot-analyzer.js +2 -2
  13. package/dist/cli/core/snapshot-api-config.js +46 -5
  14. package/dist/cli/router.js +1 -1
  15. package/dist/cli/workers/run-integration-runtime.js +2 -4
  16. package/dist/shared/debug/index.d.ts +1 -1
  17. package/dist/shared/debug/index.js +2 -3
  18. package/dist/shared/debug/pause.d.ts +2 -6
  19. package/dist/shared/debug/pause.js +27 -20
  20. package/dist/shared/llm/client.js +5 -5
  21. package/dist/shared/paths/paths.js +2 -1
  22. package/dist/shared/paths/repo-root.d.ts +3 -0
  23. package/dist/shared/paths/repo-root.js +24 -0
  24. package/package.json +8 -8
  25. package/scripts/postinstall.mjs +50 -0
  26. package/skills/libretto/SKILL.md +93 -404
  27. package/skills/libretto/references/auth-profiles.md +30 -0
  28. package/skills/libretto/references/pages-and-page-targeting.md +29 -0
  29. package/skills/libretto/references/reverse-engineering-network-requests.md +39 -0
  30. package/skills/libretto/references/user-action-log.md +31 -0
  31. package/src/cli/cli.ts +173 -0
  32. package/src/cli/commands/ai.ts +35 -0
  33. package/src/cli/commands/browser.ts +165 -0
  34. package/src/cli/commands/execution.ts +691 -0
  35. package/src/cli/commands/init.ts +327 -0
  36. package/src/cli/commands/logs.ts +128 -0
  37. package/src/cli/commands/shared.ts +70 -0
  38. package/src/cli/commands/snapshot.ts +327 -0
  39. package/src/cli/core/ai-config.ts +255 -0
  40. package/src/cli/core/api-snapshot-analyzer.ts +97 -0
  41. package/src/cli/core/browser.ts +839 -0
  42. package/src/cli/core/context.ts +122 -0
  43. package/src/cli/core/pause-signals.ts +35 -0
  44. package/src/cli/core/session-telemetry.ts +553 -0
  45. package/src/cli/core/session.ts +209 -0
  46. package/src/cli/core/snapshot-analyzer.ts +875 -0
  47. package/src/cli/core/snapshot-api-config.ts +236 -0
  48. package/src/cli/core/telemetry.ts +446 -0
  49. package/src/cli/framework/simple-cli.ts +1273 -0
  50. package/src/cli/index.ts +13 -0
  51. package/src/cli/router.ts +28 -0
  52. package/src/cli/workers/run-integration-runtime.ts +311 -0
  53. package/src/cli/workers/run-integration-worker-protocol.ts +14 -0
  54. package/src/cli/workers/run-integration-worker.ts +75 -0
  55. package/src/index.ts +120 -0
  56. package/src/runtime/download/download.ts +100 -0
  57. package/src/runtime/download/index.ts +7 -0
  58. package/src/runtime/extract/extract.ts +92 -0
  59. package/src/runtime/extract/index.ts +1 -0
  60. package/src/runtime/network/index.ts +5 -0
  61. package/src/runtime/network/network.ts +113 -0
  62. package/src/runtime/recovery/agent.ts +256 -0
  63. package/src/runtime/recovery/errors.ts +152 -0
  64. package/src/runtime/recovery/index.ts +7 -0
  65. package/src/runtime/recovery/recovery.ts +50 -0
  66. package/{dist/shared/condense-dom/condense-dom.cjs → src/shared/condense-dom/condense-dom.ts} +243 -115
  67. package/src/shared/config/config.ts +22 -0
  68. package/src/shared/config/index.ts +5 -0
  69. package/src/shared/debug/index.ts +1 -0
  70. package/src/shared/debug/pause.ts +85 -0
  71. package/src/shared/instrumentation/errors.ts +82 -0
  72. package/src/shared/instrumentation/index.ts +9 -0
  73. package/src/shared/instrumentation/instrument.ts +276 -0
  74. package/src/shared/llm/ai-sdk-adapter.ts +78 -0
  75. package/src/shared/llm/client.ts +217 -0
  76. package/src/shared/llm/index.ts +3 -0
  77. package/src/shared/llm/types.ts +63 -0
  78. package/src/shared/logger/index.ts +6 -0
  79. package/src/shared/logger/logger.ts +352 -0
  80. package/src/shared/logger/sinks.ts +144 -0
  81. package/src/shared/paths/paths.ts +109 -0
  82. package/src/shared/paths/repo-root.ts +27 -0
  83. package/src/shared/run/api.ts +2 -0
  84. package/src/shared/run/browser.ts +98 -0
  85. package/src/shared/state/index.ts +11 -0
  86. package/src/shared/state/session-state.ts +74 -0
  87. package/src/shared/visualization/ghost-cursor.ts +200 -0
  88. package/src/shared/visualization/highlight.ts +146 -0
  89. package/src/shared/visualization/index.ts +18 -0
  90. package/src/shared/workflow/workflow.ts +42 -0
  91. package/dist/index.cjs +0 -144
  92. package/dist/index.d.cts +0 -21
  93. package/dist/runtime/download/download.cjs +0 -70
  94. package/dist/runtime/download/download.d.cts +0 -35
  95. package/dist/runtime/download/index.cjs +0 -30
  96. package/dist/runtime/download/index.d.cts +0 -3
  97. package/dist/runtime/extract/extract.cjs +0 -88
  98. package/dist/runtime/extract/extract.d.cts +0 -23
  99. package/dist/runtime/extract/index.cjs +0 -28
  100. package/dist/runtime/extract/index.d.cts +0 -5
  101. package/dist/runtime/network/index.cjs +0 -28
  102. package/dist/runtime/network/index.d.cts +0 -4
  103. package/dist/runtime/network/network.cjs +0 -91
  104. package/dist/runtime/network/network.d.cts +0 -28
  105. package/dist/runtime/recovery/agent.cjs +0 -223
  106. package/dist/runtime/recovery/agent.d.cts +0 -13
  107. package/dist/runtime/recovery/errors.cjs +0 -124
  108. package/dist/runtime/recovery/errors.d.cts +0 -31
  109. package/dist/runtime/recovery/index.cjs +0 -34
  110. package/dist/runtime/recovery/index.d.cts +0 -7
  111. package/dist/runtime/recovery/recovery.cjs +0 -55
  112. package/dist/runtime/recovery/recovery.d.cts +0 -12
  113. package/dist/shared/condense-dom/condense-dom.d.cts +0 -34
  114. package/dist/shared/config/config.cjs +0 -44
  115. package/dist/shared/config/config.d.cts +0 -10
  116. package/dist/shared/config/index.cjs +0 -32
  117. package/dist/shared/config/index.d.cts +0 -1
  118. package/dist/shared/debug/index.cjs +0 -30
  119. package/dist/shared/debug/index.d.cts +0 -1
  120. package/dist/shared/debug/pause.cjs +0 -90
  121. package/dist/shared/debug/pause.d.cts +0 -16
  122. package/dist/shared/instrumentation/errors.cjs +0 -81
  123. package/dist/shared/instrumentation/errors.d.cts +0 -12
  124. package/dist/shared/instrumentation/index.cjs +0 -35
  125. package/dist/shared/instrumentation/index.d.cts +0 -6
  126. package/dist/shared/instrumentation/instrument.cjs +0 -206
  127. package/dist/shared/instrumentation/instrument.d.cts +0 -32
  128. package/dist/shared/llm/ai-sdk-adapter.cjs +0 -71
  129. package/dist/shared/llm/ai-sdk-adapter.d.cts +0 -22
  130. package/dist/shared/llm/client.cjs +0 -218
  131. package/dist/shared/llm/client.d.cts +0 -13
  132. package/dist/shared/llm/index.cjs +0 -31
  133. package/dist/shared/llm/index.d.cts +0 -5
  134. package/dist/shared/llm/types.cjs +0 -16
  135. package/dist/shared/llm/types.d.cts +0 -67
  136. package/dist/shared/logger/index.cjs +0 -37
  137. package/dist/shared/logger/index.d.cts +0 -2
  138. package/dist/shared/logger/logger.cjs +0 -232
  139. package/dist/shared/logger/logger.d.cts +0 -86
  140. package/dist/shared/logger/sinks.cjs +0 -160
  141. package/dist/shared/logger/sinks.d.cts +0 -9
  142. package/dist/shared/paths/paths.cjs +0 -104
  143. package/dist/shared/paths/paths.d.cts +0 -10
  144. package/dist/shared/run/api.cjs +0 -28
  145. package/dist/shared/run/api.d.cts +0 -2
  146. package/dist/shared/run/browser.cjs +0 -98
  147. package/dist/shared/run/browser.d.cts +0 -22
  148. package/dist/shared/state/index.cjs +0 -38
  149. package/dist/shared/state/index.d.cts +0 -2
  150. package/dist/shared/state/session-state.cjs +0 -92
  151. package/dist/shared/state/session-state.d.cts +0 -40
  152. package/dist/shared/visualization/ghost-cursor.cjs +0 -174
  153. package/dist/shared/visualization/ghost-cursor.d.cts +0 -37
  154. package/dist/shared/visualization/highlight.cjs +0 -134
  155. package/dist/shared/visualization/highlight.d.cts +0 -22
  156. package/dist/shared/visualization/index.cjs +0 -45
  157. package/dist/shared/visualization/index.d.cts +0 -3
  158. package/dist/shared/workflow/workflow.cjs +0 -47
  159. package/dist/shared/workflow/workflow.d.cts +0 -21
  160. package/skills/libretto/code-generation-rules.md +0 -223
  161. package/skills/libretto/integration-approach-selection.md +0 -174
@@ -1,34 +0,0 @@
1
- /**
2
- * DOM condensation — reduces serialized HTML for LLM consumption.
3
- *
4
- * All rules run unconditionally (no tiers). The function operates on
5
- * already-serialized HTML strings (the output of `page.content()`),
6
- * not a browser-side DOM walk or parsed DOM tree.
7
- *
8
- * Rules applied in order:
9
- * 1. Noscript blocks — remove entirely
10
- * 2. HTML comments — remove entirely
11
- * 3. Script contents — hollow out, keep tags + useful attributes
12
- * 4. Style contents — hollow out, keep tags + useful attributes
13
- * 5. Embedded binary data — replace base64 data URIs
14
- * 6. Attribute allowlist — keep trusted attrs, special-case class/style/URLs
15
- * 7. SVG elements — collapse to single tag, extract title/desc
16
- * 8. Inline style properties — keep only layout-relevant props
17
- * 9. Non-semantic class names — filter or delete class values
18
- * 10. (Cross-reference IDs — preserved, no action needed)
19
- * 11. Framework-internal and SVG visual attributes — remove
20
- * 12. Whitespace — collapse (preserve <pre> content)
21
- */
22
- type CondenseDomResult = {
23
- /** The condensed HTML string. Valid, parseable HTML. */
24
- html: string;
25
- /** Character count of the input. */
26
- originalLength: number;
27
- /** Character count of the output. */
28
- condensedLength: number;
29
- /** Characters removed, keyed by rule name. */
30
- reductions: Record<string, number>;
31
- };
32
- declare function condenseDom(html: string): CondenseDomResult;
33
-
34
- export { type CondenseDomResult, condenseDom };
@@ -1,44 +0,0 @@
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 config_exports = {};
20
- __export(config_exports, {
21
- isDebugMode: () => isDebugMode,
22
- isDryRun: () => isDryRun,
23
- shouldPauseBeforeMutation: () => shouldPauseBeforeMutation
24
- });
25
- module.exports = __toCommonJS(config_exports);
26
- function isDebugMode() {
27
- return process.env.LIBRETTO_DEBUG === "true";
28
- }
29
- function isDryRun() {
30
- const explicit = process.env.LIBRETTO_DRY_RUN;
31
- if (explicit !== void 0) {
32
- return explicit === "true";
33
- }
34
- return process.env.NODE_ENV === "development";
35
- }
36
- function shouldPauseBeforeMutation() {
37
- return isDryRun() && isDebugMode();
38
- }
39
- // Annotate the CommonJS export names for ESM import in node:
40
- 0 && (module.exports = {
41
- isDebugMode,
42
- isDryRun,
43
- shouldPauseBeforeMutation
44
- });
@@ -1,10 +0,0 @@
1
- /**
2
- * Runtime configuration for libretto.
3
- *
4
- * Values are derived from environment variables only.
5
- */
6
- declare function isDebugMode(): boolean;
7
- declare function isDryRun(): boolean;
8
- declare function shouldPauseBeforeMutation(): boolean;
9
-
10
- export { isDebugMode, isDryRun, shouldPauseBeforeMutation };
@@ -1,32 +0,0 @@
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 config_exports = {};
20
- __export(config_exports, {
21
- isDebugMode: () => import_config.isDebugMode,
22
- isDryRun: () => import_config.isDryRun,
23
- shouldPauseBeforeMutation: () => import_config.shouldPauseBeforeMutation
24
- });
25
- module.exports = __toCommonJS(config_exports);
26
- var import_config = require("./config.js");
27
- // Annotate the CommonJS export names for ESM import in node:
28
- 0 && (module.exports = {
29
- isDebugMode,
30
- isDryRun,
31
- shouldPauseBeforeMutation
32
- });
@@ -1 +0,0 @@
1
- export { isDebugMode, isDryRun, shouldPauseBeforeMutation } from './config.cjs';
@@ -1,30 +0,0 @@
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 debug_exports = {};
20
- __export(debug_exports, {
21
- pause: () => import_pause.pause,
22
- setSessionForPause: () => import_pause.setSessionForPause
23
- });
24
- module.exports = __toCommonJS(debug_exports);
25
- var import_pause = require("./pause.js");
26
- // Annotate the CommonJS export names for ESM import in node:
27
- 0 && (module.exports = {
28
- pause,
29
- setSessionForPause
30
- });
@@ -1 +0,0 @@
1
- export { pause, setSessionForPause } from './pause.cjs';
@@ -1,90 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var pause_exports = {};
30
- __export(pause_exports, {
31
- pause: () => pause,
32
- setSessionForPause: () => setSessionForPause
33
- });
34
- module.exports = __toCommonJS(pause_exports);
35
- var import_node_fs = require("node:fs");
36
- var import_promises = require("node:fs/promises");
37
- let _sessionName;
38
- function setSessionForPause(session) {
39
- _sessionName = session;
40
- }
41
- function getSessionFromProcessArgs() {
42
- const rawPayload = process.argv[2];
43
- if (!rawPayload) return void 0;
44
- try {
45
- const parsed = JSON.parse(rawPayload);
46
- return typeof parsed.session === "string" ? parsed.session : void 0;
47
- } catch {
48
- return void 0;
49
- }
50
- }
51
- function resolveSession() {
52
- return _sessionName ?? getSessionFromProcessArgs();
53
- }
54
- async function pause() {
55
- if (process.env.NODE_ENV === "production") {
56
- return;
57
- }
58
- const session = resolveSession();
59
- if (!session) {
60
- return;
61
- }
62
- const { getPauseSignalPaths, removeSignalIfExists } = await import("../../cli/core/pause-signals.js");
63
- const { getSessionDir } = await import("../../cli/core/context.js");
64
- const signalPaths = getPauseSignalPaths(session);
65
- const { pausedSignalPath, resumeSignalPath } = signalPaths;
66
- await (0, import_promises.mkdir)(getSessionDir(session), { recursive: true });
67
- await removeSignalIfExists(resumeSignalPath);
68
- const details = {
69
- sessionName: session,
70
- pausedAt: (/* @__PURE__ */ new Date()).toISOString(),
71
- url: "unknown"
72
- };
73
- await (0, import_promises.writeFile)(pausedSignalPath, JSON.stringify(details, null, 2), "utf8");
74
- console.log(`[pause] Paused (session: ${session})`);
75
- console.log("[pause] Waiting for resume signal...");
76
- const RESUME_POLL_INTERVAL_MS = 250;
77
- while (!(0, import_node_fs.existsSync)(resumeSignalPath)) {
78
- await new Promise(
79
- (resolve) => setTimeout(resolve, RESUME_POLL_INTERVAL_MS)
80
- );
81
- }
82
- await removeSignalIfExists(resumeSignalPath);
83
- await removeSignalIfExists(pausedSignalPath);
84
- console.log("[pause] Resume signal received. Continuing workflow...");
85
- }
86
- // Annotate the CommonJS export names for ESM import in node:
87
- 0 && (module.exports = {
88
- pause,
89
- setSessionForPause
90
- });
@@ -1,16 +0,0 @@
1
- /**
2
- * Called by the CLI runtime to make the session name available to `pause()`.
3
- */
4
- declare function setSessionForPause(session: string): void;
5
- /**
6
- * Standalone pause function.
7
- *
8
- * - In production (`NODE_ENV === "production"`), returns immediately (no-op).
9
- * - Otherwise, writes a `.paused` signal file and polls for a `.resume` signal,
10
- * using the same file-based mechanism as the CLI runner.
11
- *
12
- * Import directly: `import { pause } from "libretto";`
13
- */
14
- declare function pause(): Promise<void>;
15
-
16
- export { pause, setSessionForPause };
@@ -1,81 +0,0 @@
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 errors_exports = {};
20
- __export(errors_exports, {
21
- enrichTimeoutError: () => enrichTimeoutError
22
- });
23
- module.exports = __toCommonJS(errors_exports);
24
- async function enrichTimeoutError(err, locator, page) {
25
- const reasons = [];
26
- try {
27
- const visible = await locator.isVisible().catch(() => null);
28
- if (visible === false) {
29
- reasons.push("Element is not visible");
30
- }
31
- if (typeof locator.isInViewport === "function") {
32
- const inViewport = await locator.isInViewport().catch(() => null);
33
- if (inViewport === false) {
34
- reasons.push("Element is outside of the viewport");
35
- }
36
- }
37
- const enabled = await locator.isEnabled().catch(() => null);
38
- if (enabled === false) {
39
- reasons.push("Element is not enabled (disabled)");
40
- }
41
- if (reasons.length === 0) {
42
- const box = await locator.boundingBox().catch(() => null);
43
- if (box) {
44
- const centerX = box.x + box.width / 2;
45
- const centerY = box.y + box.height / 2;
46
- const interceptInfo = await page.evaluate(
47
- ({ x, y }) => {
48
- const els = document.elementsFromPoint(x, y);
49
- if (!els || els.length < 2) return null;
50
- const topEl = els[0];
51
- if (!topEl) return null;
52
- const tag = topEl.tagName.toLowerCase();
53
- const id = topEl.id ? `#${topEl.id}` : "";
54
- const cls = topEl.className ? `.${String(topEl.className).split(/\s+/).slice(0, 2).join(".")}` : "";
55
- const text = (topEl.textContent || "").trim().slice(0, 40);
56
- return {
57
- tag,
58
- preview: `<${tag}${id}${cls}>${text ? ` "${text}"` : ""}`
59
- };
60
- },
61
- { x: centerX, y: centerY }
62
- ).catch(() => null);
63
- if (interceptInfo) {
64
- reasons.push(
65
- `Element may be intercepted by ${interceptInfo.preview}`
66
- );
67
- }
68
- }
69
- }
70
- } catch {
71
- }
72
- if (reasons.length > 0) {
73
- const enrichment = `
74
- [libretto diagnostics] ${reasons.join("; ")}`;
75
- err.message = (err.message || "") + enrichment;
76
- }
77
- }
78
- // Annotate the CommonJS export names for ESM import in node:
79
- 0 && (module.exports = {
80
- enrichTimeoutError
81
- });
@@ -1,12 +0,0 @@
1
- import { Locator, Page } from 'playwright';
2
-
3
- /**
4
- * Enrich a timeout error from a pointer action (click/dblclick/hover) with
5
- * diagnostic information about why the action may have failed.
6
- *
7
- * Mutates err.message in-place to append the enrichment.
8
- * Best-effort: if any probe fails, we skip that check silently.
9
- */
10
- declare function enrichTimeoutError(err: any, locator: Locator, page: Page): Promise<void>;
11
-
12
- export { enrichTimeoutError };
@@ -1,35 +0,0 @@
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 instrumentation_exports = {};
20
- __export(instrumentation_exports, {
21
- enrichTimeoutError: () => import_errors.enrichTimeoutError,
22
- installInstrumentation: () => import_instrument.installInstrumentation,
23
- instrumentContext: () => import_instrument.instrumentContext,
24
- instrumentPage: () => import_instrument.instrumentPage
25
- });
26
- module.exports = __toCommonJS(instrumentation_exports);
27
- var import_instrument = require("./instrument.js");
28
- var import_errors = require("./errors.js");
29
- // Annotate the CommonJS export names for ESM import in node:
30
- 0 && (module.exports = {
31
- enrichTimeoutError,
32
- installInstrumentation,
33
- instrumentContext,
34
- instrumentPage
35
- });
@@ -1,6 +0,0 @@
1
- export { InstrumentationOptions, InstrumentedPage, installInstrumentation, instrumentContext, instrumentPage } from './instrument.cjs';
2
- export { enrichTimeoutError } from './errors.cjs';
3
- import 'playwright';
4
- import '../logger/logger.cjs';
5
- import '../visualization/ghost-cursor.cjs';
6
- import '../visualization/highlight.cjs';
@@ -1,206 +0,0 @@
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 instrument_exports = {};
20
- __export(instrument_exports, {
21
- installInstrumentation: () => installInstrumentation,
22
- instrumentContext: () => instrumentContext,
23
- instrumentPage: () => instrumentPage
24
- });
25
- module.exports = __toCommonJS(instrument_exports);
26
- var import_ghost_cursor = require("../visualization/ghost-cursor.js");
27
- var import_highlight = require("../visualization/highlight.js");
28
- var import_errors = require("./errors.js");
29
- const LOCATOR_ACTIONS = [
30
- "click",
31
- "dblclick",
32
- "hover",
33
- "fill",
34
- "type",
35
- "press",
36
- "check",
37
- "uncheck",
38
- "selectOption",
39
- "focus"
40
- ];
41
- const NAV_ACTIONS = ["goto", "reload", "goBack", "goForward"];
42
- const POINTER_ACTIONS = /* @__PURE__ */ new Set(["click", "dblclick", "hover"]);
43
- const pageQueues = /* @__PURE__ */ new WeakMap();
44
- function enqueue(page, fn) {
45
- const prev = pageQueues.get(page) ?? Promise.resolve();
46
- const next = prev.then(fn, fn);
47
- pageQueues.set(page, next);
48
- return next;
49
- }
50
- async function visualizeBeforeAction(page, box, actionName, highlightMs) {
51
- if (!box) return;
52
- await (0, import_ghost_cursor.ensureGhostCursor)(page);
53
- await (0, import_highlight.ensureHighlightLayer)(page);
54
- const centerX = box.x + box.width / 2;
55
- const centerY = box.y + box.height / 2;
56
- await (0, import_highlight.showHighlight)(page, {
57
- box,
58
- durationMs: highlightMs + 200
59
- // keep visible a bit past the cursor arrival
60
- });
61
- const cursorPos = await (0, import_ghost_cursor.getGhostCursorPosition)(page);
62
- if (cursorPos) {
63
- await (0, import_ghost_cursor.moveGhostCursorWithDistance)(page, cursorPos, {
64
- x: centerX,
65
- y: centerY
66
- });
67
- } else {
68
- await (0, import_ghost_cursor.moveGhostCursor)(page, { x: centerX, y: centerY, durationMs: 200 });
69
- }
70
- if (actionName === "click" || actionName === "dblclick") {
71
- await (0, import_ghost_cursor.ghostClick)(page, { x: centerX, y: centerY });
72
- }
73
- }
74
- async function visualizeAfterAction(page) {
75
- await (0, import_highlight.clearHighlights)(page);
76
- }
77
- function wrapLocatorActions(locator, page, opts) {
78
- for (const method of LOCATOR_ACTIONS) {
79
- if (typeof locator[method] !== "function") continue;
80
- const orig = locator[method].bind(locator);
81
- locator[method] = async (...args) => {
82
- if (opts.visualize) {
83
- await enqueue(page, async () => {
84
- try {
85
- const box = await locator.boundingBox();
86
- await visualizeBeforeAction(
87
- page,
88
- box,
89
- method,
90
- opts.highlightBeforeActionMs
91
- );
92
- } catch {
93
- }
94
- });
95
- }
96
- try {
97
- const result = await orig(...args);
98
- if (opts.visualize) {
99
- enqueue(page, () => visualizeAfterAction(page));
100
- }
101
- return result;
102
- } catch (err) {
103
- if (opts.visualize) {
104
- enqueue(page, () => visualizeAfterAction(page));
105
- }
106
- if (POINTER_ACTIONS.has(method) && isTimeoutError(err)) {
107
- await (0, import_errors.enrichTimeoutError)(err, locator, page);
108
- }
109
- throw err;
110
- }
111
- };
112
- }
113
- }
114
- function isTimeoutError(err) {
115
- if (!err || typeof err.message !== "string") return false;
116
- return err.message.includes("Timeout") || err.message.includes("timeout") || err.name === "TimeoutError";
117
- }
118
- const LOCATOR_FACTORIES = [
119
- "locator",
120
- "getByRole",
121
- "getByText",
122
- "getByLabel",
123
- "getByPlaceholder",
124
- "getByAltText",
125
- "getByTitle",
126
- "getByTestId"
127
- ];
128
- async function installInstrumentation(page, options) {
129
- if (page.__librettoInstrumented) return;
130
- page.__librettoInstrumented = true;
131
- const visualize = options?.visualize ?? false;
132
- const highlightBeforeActionMs = options?.highlightBeforeActionMs ?? 350;
133
- const mergedOpts = { ...options, visualize, highlightBeforeActionMs };
134
- if (visualize) {
135
- await (0, import_ghost_cursor.ensureGhostCursor)(page, options?.ghostCursor);
136
- await (0, import_highlight.ensureHighlightLayer)(page, options?.highlight);
137
- }
138
- for (const method of LOCATOR_ACTIONS) {
139
- if (typeof page[method] !== "function") continue;
140
- const orig = page[method].bind(page);
141
- page[method] = async (...args) => {
142
- if (visualize && typeof args[0] === "string") {
143
- await enqueue(page, async () => {
144
- try {
145
- const loc = page.locator(args[0]);
146
- const box = await loc.boundingBox();
147
- await visualizeBeforeAction(page, box, method, highlightBeforeActionMs);
148
- } catch {
149
- }
150
- });
151
- }
152
- try {
153
- const result = await orig(...args);
154
- if (visualize) {
155
- enqueue(page, () => visualizeAfterAction(page));
156
- }
157
- return result;
158
- } catch (err) {
159
- if (visualize) {
160
- enqueue(page, () => visualizeAfterAction(page));
161
- }
162
- if (POINTER_ACTIONS.has(method) && isTimeoutError(err) && typeof args[0] === "string") {
163
- await (0, import_errors.enrichTimeoutError)(err, page.locator(args[0]), page);
164
- }
165
- throw err;
166
- }
167
- };
168
- }
169
- for (const method of NAV_ACTIONS) {
170
- if (typeof page[method] !== "function") continue;
171
- const orig = page[method].bind(page);
172
- page[method] = async (...args) => {
173
- options?.logger?.info(`instrumentation:${method}`, {
174
- url: typeof args[0] === "string" ? args[0] : page.url()
175
- });
176
- return orig(...args);
177
- };
178
- }
179
- for (const factory of LOCATOR_FACTORIES) {
180
- if (typeof page[factory] !== "function") continue;
181
- const origFactory = page[factory].bind(page);
182
- page[factory] = (...factoryArgs) => {
183
- const locator = origFactory(...factoryArgs);
184
- wrapLocatorActions(locator, page, mergedOpts);
185
- return locator;
186
- };
187
- }
188
- }
189
- async function instrumentPage(page, options) {
190
- await installInstrumentation(page, options);
191
- return page;
192
- }
193
- async function instrumentContext(context, options) {
194
- for (const page of context.pages()) {
195
- await installInstrumentation(page, options);
196
- }
197
- context.on("page", async (page) => {
198
- await installInstrumentation(page, options);
199
- });
200
- }
201
- // Annotate the CommonJS export names for ESM import in node:
202
- 0 && (module.exports = {
203
- installInstrumentation,
204
- instrumentContext,
205
- instrumentPage
206
- });
@@ -1,32 +0,0 @@
1
- import { Page, BrowserContext } from 'playwright';
2
- import { MinimalLogger } from '../logger/logger.cjs';
3
- import { GhostCursorOptions } from '../visualization/ghost-cursor.cjs';
4
- import { HighlightOptions } from '../visualization/highlight.cjs';
5
-
6
- type InstrumentationOptions = {
7
- visualize?: boolean;
8
- logger?: MinimalLogger;
9
- highlightBeforeActionMs?: number;
10
- ghostCursor?: GhostCursorOptions;
11
- highlight?: HighlightOptions;
12
- };
13
- type InstrumentedPage = Page & {
14
- __librettoInstrumented: true;
15
- };
16
- /**
17
- * In-place patching of a Page object to add visualization and error enrichment.
18
- * Modifies the page directly (does not return a new object).
19
- */
20
- declare function installInstrumentation(page: Page, options?: InstrumentationOptions): Promise<void>;
21
- /**
22
- * Returns a new object that proxies to the page with instrumentation applied.
23
- * The original page is not modified.
24
- */
25
- declare function instrumentPage(page: Page, options?: InstrumentationOptions): Promise<InstrumentedPage>;
26
- /**
27
- * Install overlays on a page and auto-install on all future pages in the context.
28
- * Useful when connecting to an existing browser via CDP.
29
- */
30
- declare function instrumentContext(context: BrowserContext, options?: InstrumentationOptions): Promise<void>;
31
-
32
- export { type InstrumentationOptions, type InstrumentedPage, installInstrumentation, instrumentContext, instrumentPage };