libretto 0.3.0 → 0.3.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 (162) hide show
  1. package/README.md +51 -125
  2. package/dist/cli/cli.js +298 -0
  3. package/dist/cli/commands/ai.js +21 -0
  4. package/dist/cli/commands/browser.js +103 -0
  5. package/dist/cli/commands/execution.js +490 -0
  6. package/dist/cli/commands/init.js +166 -0
  7. package/dist/cli/commands/logs.js +93 -0
  8. package/dist/cli/commands/snapshot.js +217 -0
  9. package/dist/cli/core/ai-config.js +156 -0
  10. package/dist/cli/core/browser.js +669 -0
  11. package/dist/cli/core/context.js +117 -0
  12. package/dist/cli/core/pause-signals.js +29 -0
  13. package/dist/cli/core/session-telemetry.js +491 -0
  14. package/dist/cli/core/session.js +183 -0
  15. package/dist/cli/core/snapshot-analyzer.js +570 -0
  16. package/dist/cli/core/telemetry.js +362 -0
  17. package/dist/cli/index.js +14 -0
  18. package/dist/cli/workers/run-integration-runtime.js +234 -0
  19. package/dist/cli/workers/run-integration-worker-protocol.js +12 -0
  20. package/dist/cli/workers/run-integration-worker.js +67 -0
  21. package/dist/index.cjs +144 -0
  22. package/dist/index.d.cts +21 -0
  23. package/dist/index.d.ts +21 -0
  24. package/dist/index.js +114 -0
  25. package/dist/runtime/download/download.cjs +70 -0
  26. package/dist/runtime/download/download.d.cts +35 -0
  27. package/dist/runtime/download/download.d.ts +35 -0
  28. package/dist/runtime/download/download.js +45 -0
  29. package/dist/runtime/download/index.cjs +30 -0
  30. package/dist/runtime/download/index.d.cts +3 -0
  31. package/dist/runtime/download/index.d.ts +3 -0
  32. package/dist/runtime/download/index.js +8 -0
  33. package/dist/runtime/extract/extract.cjs +88 -0
  34. package/dist/runtime/extract/extract.d.cts +23 -0
  35. package/dist/runtime/extract/extract.d.ts +23 -0
  36. package/dist/runtime/extract/extract.js +64 -0
  37. package/dist/runtime/extract/index.cjs +28 -0
  38. package/dist/runtime/extract/index.d.cts +5 -0
  39. package/dist/runtime/extract/index.d.ts +5 -0
  40. package/dist/runtime/extract/index.js +4 -0
  41. package/dist/runtime/network/index.cjs +28 -0
  42. package/dist/runtime/network/index.d.cts +4 -0
  43. package/dist/runtime/network/index.d.ts +4 -0
  44. package/dist/runtime/network/index.js +6 -0
  45. package/dist/runtime/network/network.cjs +91 -0
  46. package/dist/runtime/network/network.d.cts +28 -0
  47. package/dist/runtime/network/network.d.ts +28 -0
  48. package/dist/runtime/network/network.js +67 -0
  49. package/dist/runtime/recovery/agent.cjs +223 -0
  50. package/dist/runtime/recovery/agent.d.cts +13 -0
  51. package/dist/runtime/recovery/agent.d.ts +13 -0
  52. package/dist/runtime/recovery/agent.js +199 -0
  53. package/dist/runtime/recovery/errors.cjs +124 -0
  54. package/dist/runtime/recovery/errors.d.cts +31 -0
  55. package/dist/runtime/recovery/errors.d.ts +31 -0
  56. package/dist/runtime/recovery/errors.js +100 -0
  57. package/dist/runtime/recovery/index.cjs +34 -0
  58. package/dist/runtime/recovery/index.d.cts +7 -0
  59. package/dist/runtime/recovery/index.d.ts +7 -0
  60. package/dist/runtime/recovery/index.js +10 -0
  61. package/dist/runtime/recovery/recovery.cjs +55 -0
  62. package/dist/runtime/recovery/recovery.d.cts +12 -0
  63. package/dist/runtime/recovery/recovery.d.ts +12 -0
  64. package/dist/runtime/recovery/recovery.js +31 -0
  65. package/dist/shared/config/config.cjs +44 -0
  66. package/dist/shared/config/config.d.cts +10 -0
  67. package/dist/shared/config/config.d.ts +10 -0
  68. package/dist/shared/config/config.js +18 -0
  69. package/dist/shared/config/index.cjs +32 -0
  70. package/dist/shared/config/index.d.cts +1 -0
  71. package/dist/shared/config/index.d.ts +1 -0
  72. package/dist/shared/config/index.js +10 -0
  73. package/dist/shared/debug/index.cjs +30 -0
  74. package/dist/shared/debug/index.d.cts +1 -0
  75. package/dist/shared/debug/index.d.ts +1 -0
  76. package/dist/shared/debug/index.js +5 -0
  77. package/dist/shared/debug/pause.cjs +90 -0
  78. package/dist/shared/debug/pause.d.cts +16 -0
  79. package/dist/shared/debug/pause.d.ts +16 -0
  80. package/dist/shared/debug/pause.js +55 -0
  81. package/dist/shared/instrumentation/errors.cjs +81 -0
  82. package/dist/shared/instrumentation/errors.d.cts +12 -0
  83. package/dist/shared/instrumentation/errors.d.ts +12 -0
  84. package/dist/shared/instrumentation/errors.js +57 -0
  85. package/dist/shared/instrumentation/index.cjs +35 -0
  86. package/dist/shared/instrumentation/index.d.cts +6 -0
  87. package/dist/shared/instrumentation/index.d.ts +6 -0
  88. package/dist/shared/instrumentation/index.js +12 -0
  89. package/dist/shared/instrumentation/instrument.cjs +206 -0
  90. package/dist/shared/instrumentation/instrument.d.cts +32 -0
  91. package/dist/shared/instrumentation/instrument.d.ts +32 -0
  92. package/dist/shared/instrumentation/instrument.js +190 -0
  93. package/dist/shared/llm/ai-sdk-adapter.cjs +67 -0
  94. package/dist/shared/llm/ai-sdk-adapter.d.cts +22 -0
  95. package/dist/shared/llm/ai-sdk-adapter.d.ts +22 -0
  96. package/dist/shared/llm/ai-sdk-adapter.js +43 -0
  97. package/dist/shared/llm/client.cjs +139 -0
  98. package/dist/shared/llm/client.d.cts +6 -0
  99. package/dist/shared/llm/client.d.ts +6 -0
  100. package/dist/shared/llm/client.js +115 -0
  101. package/dist/shared/llm/index.cjs +31 -0
  102. package/dist/shared/llm/index.d.cts +5 -0
  103. package/dist/shared/llm/index.d.ts +5 -0
  104. package/dist/shared/llm/index.js +6 -0
  105. package/dist/shared/llm/types.cjs +16 -0
  106. package/dist/shared/llm/types.d.cts +66 -0
  107. package/dist/shared/llm/types.d.ts +66 -0
  108. package/dist/shared/llm/types.js +0 -0
  109. package/dist/shared/logger/index.cjs +37 -0
  110. package/dist/shared/logger/index.d.cts +2 -0
  111. package/dist/shared/logger/index.d.ts +2 -0
  112. package/dist/shared/logger/index.js +13 -0
  113. package/dist/shared/logger/logger.cjs +232 -0
  114. package/dist/shared/logger/logger.d.cts +86 -0
  115. package/dist/shared/logger/logger.d.ts +86 -0
  116. package/dist/shared/logger/logger.js +207 -0
  117. package/dist/shared/logger/sinks.cjs +160 -0
  118. package/dist/shared/logger/sinks.d.cts +9 -0
  119. package/dist/shared/logger/sinks.d.ts +9 -0
  120. package/dist/shared/logger/sinks.js +124 -0
  121. package/dist/shared/paths/paths.cjs +104 -0
  122. package/dist/shared/paths/paths.d.cts +10 -0
  123. package/dist/shared/paths/paths.d.ts +10 -0
  124. package/dist/shared/paths/paths.js +73 -0
  125. package/dist/shared/run/api.cjs +28 -0
  126. package/dist/shared/run/api.d.cts +2 -0
  127. package/dist/shared/run/api.d.ts +2 -0
  128. package/dist/shared/run/api.js +4 -0
  129. package/dist/shared/run/browser.cjs +98 -0
  130. package/dist/shared/run/browser.d.cts +22 -0
  131. package/dist/shared/run/browser.d.ts +22 -0
  132. package/dist/shared/run/browser.js +74 -0
  133. package/dist/shared/state/index.cjs +38 -0
  134. package/dist/shared/state/index.d.cts +2 -0
  135. package/dist/shared/state/index.d.ts +2 -0
  136. package/dist/shared/state/index.js +16 -0
  137. package/dist/shared/state/session-state.cjs +92 -0
  138. package/dist/shared/state/session-state.d.cts +40 -0
  139. package/dist/shared/state/session-state.d.ts +40 -0
  140. package/dist/shared/state/session-state.js +62 -0
  141. package/dist/shared/visualization/ghost-cursor.cjs +174 -0
  142. package/dist/shared/visualization/ghost-cursor.d.cts +37 -0
  143. package/dist/shared/visualization/ghost-cursor.d.ts +37 -0
  144. package/dist/shared/visualization/ghost-cursor.js +145 -0
  145. package/dist/shared/visualization/highlight.cjs +134 -0
  146. package/dist/shared/visualization/highlight.d.cts +22 -0
  147. package/dist/shared/visualization/highlight.d.ts +22 -0
  148. package/dist/shared/visualization/highlight.js +108 -0
  149. package/dist/shared/visualization/index.cjs +45 -0
  150. package/dist/shared/visualization/index.d.cts +3 -0
  151. package/dist/shared/visualization/index.d.ts +3 -0
  152. package/dist/shared/visualization/index.js +24 -0
  153. package/dist/shared/workflow/workflow.cjs +47 -0
  154. package/dist/shared/workflow/workflow.d.cts +21 -0
  155. package/dist/shared/workflow/workflow.d.ts +21 -0
  156. package/dist/shared/workflow/workflow.js +21 -0
  157. package/package.json +37 -95
  158. package/bin/libretto.mjs +0 -18
  159. package/scripts/postinstall.mjs +0 -48
  160. /package/{skill → .agents/skills/libretto}/SKILL.md +0 -0
  161. /package/{skill → .agents/skills/libretto}/code-generation-rules.md +0 -0
  162. /package/{skill → .agents/skills/libretto}/integration-approach-selection.md +0 -0
@@ -0,0 +1,570 @@
1
+ import {
2
+ existsSync,
3
+ mkdtempSync,
4
+ readFileSync,
5
+ rmSync
6
+ } from "node:fs";
7
+ import { extname, isAbsolute, join, resolve } from "node:path";
8
+ import { spawn } from "node:child_process";
9
+ import { tmpdir } from "node:os";
10
+ import { z } from "zod";
11
+ import {
12
+ formatCommandPrefix,
13
+ readAiConfig
14
+ } from "./ai-config.js";
15
+ import {
16
+ getLLMClientFactory
17
+ } from "./context.js";
18
+ const InterpretResultSchema = z.object({
19
+ answer: z.string(),
20
+ selectors: z.array(
21
+ z.object({
22
+ label: z.string(),
23
+ selector: z.string(),
24
+ rationale: z.string()
25
+ })
26
+ ).default([]),
27
+ notes: z.string().optional().default("")
28
+ });
29
+ class UserCodingAgent {
30
+ constructor(config) {
31
+ this.config = config;
32
+ }
33
+ static resolveFromConfig(config) {
34
+ switch (config.preset) {
35
+ case "codex":
36
+ return new CodexUserCodingAgent(config);
37
+ case "claude":
38
+ return new ClaudeUserCodingAgent(config);
39
+ case "gemini":
40
+ return new GeminiUserCodingAgent(config);
41
+ }
42
+ }
43
+ static readConfiguredConfig() {
44
+ return readAiConfig();
45
+ }
46
+ static getConfigured() {
47
+ const config = this.readConfiguredConfig();
48
+ return config ? this.resolveFromConfig(config) : null;
49
+ }
50
+ get snapshotAnalyzerConfig() {
51
+ return this.config;
52
+ }
53
+ get command() {
54
+ const command = this.config.commandPrefix[0];
55
+ if (!command) {
56
+ throw new Error("AI config is invalid: command prefix is empty.");
57
+ }
58
+ return command;
59
+ }
60
+ get baseArgs() {
61
+ return this.config.commandPrefix.slice(1);
62
+ }
63
+ screenshotHint(pngPath) {
64
+ return `
65
+
66
+ Screenshot file path: ${pngPath}
67
+ Use the screenshot alongside the HTML snapshot context above.`;
68
+ }
69
+ async runAnalyzer(args, logger, stdinText) {
70
+ const result = await runExternalCommand(this.command, args, logger, stdinText);
71
+ if (result.exitCode !== 0) {
72
+ throw new Error(
73
+ `Analyzer command failed (${formatCommandPrefix([this.command, ...args])}).
74
+ ${stripAnsi(result.stderr).trim() || stripAnsi(result.stdout).trim() || "No error output."}`
75
+ );
76
+ }
77
+ return result;
78
+ }
79
+ async runAndParse(args, logger, stdinText) {
80
+ const result = await this.runAnalyzer(args, logger, stdinText);
81
+ return parseInterpretResultFromText(result.stdout);
82
+ }
83
+ }
84
+ class CodexUserCodingAgent extends UserCodingAgent {
85
+ async analyzeSnapshot(prompt, pngPath, logger) {
86
+ const tempDir = mkdtempSync(join(tmpdir(), "libretto-cli-analyzer-"));
87
+ const outputPath = join(
88
+ tempDir,
89
+ `snapshot-analyzer-${Date.now()}-${Math.random().toString(36).slice(2)}.json`
90
+ );
91
+ const args = [
92
+ ...this.baseArgs,
93
+ "--output-last-message",
94
+ outputPath,
95
+ "-i",
96
+ pngPath,
97
+ "-"
98
+ ];
99
+ logger.info("interpret-analyzer-codex-start", {
100
+ outputPath,
101
+ pngPath,
102
+ promptChars: prompt.length,
103
+ args
104
+ });
105
+ const result = await this.runAnalyzer(args, logger, prompt);
106
+ let outputText = result.stdout;
107
+ try {
108
+ logger.info("interpret-analyzer-codex-finish", {
109
+ outputPath,
110
+ outputFileExists: existsSync(outputPath),
111
+ stdoutChars: result.stdout.length,
112
+ stderrChars: result.stderr.length
113
+ });
114
+ if (existsSync(outputPath)) {
115
+ outputText = readFileSync(outputPath, "utf-8");
116
+ }
117
+ return parseInterpretResultFromText(outputText);
118
+ } finally {
119
+ rmSync(tempDir, { recursive: true, force: true });
120
+ }
121
+ }
122
+ }
123
+ class ClaudeUserCodingAgent extends UserCodingAgent {
124
+ async analyzeSnapshot(prompt, pngPath, logger) {
125
+ return await this.runAndParse(
126
+ [...this.baseArgs],
127
+ logger,
128
+ `${prompt}${this.screenshotHint(pngPath)}`
129
+ );
130
+ }
131
+ }
132
+ class GeminiUserCodingAgent extends UserCodingAgent {
133
+ async analyzeSnapshot(prompt, pngPath, logger) {
134
+ return await this.runAndParse(
135
+ [...this.baseArgs],
136
+ logger,
137
+ `${prompt}${this.screenshotHint(pngPath)}`
138
+ );
139
+ }
140
+ }
141
+ async function runExternalCommand(command, args, logger, stdinText) {
142
+ return await new Promise((resolve2, reject) => {
143
+ const startedAt = Date.now();
144
+ logger.info("interpret-analyzer-spawn-start", {
145
+ command,
146
+ args,
147
+ stdinChars: stdinText?.length ?? 0
148
+ });
149
+ const child = spawn(command, args, {
150
+ stdio: ["pipe", "pipe", "pipe"]
151
+ });
152
+ let stdout = "";
153
+ let stderr = "";
154
+ let stdinError = null;
155
+ child.stdout.on("data", (chunk) => {
156
+ stdout += chunk.toString();
157
+ });
158
+ child.stderr.on("data", (chunk) => {
159
+ stderr += chunk.toString();
160
+ });
161
+ child.stdin.on("error", (err) => {
162
+ stdinError = err;
163
+ logger.warn("interpret-analyzer-stdin-pipe-error", {
164
+ command,
165
+ args,
166
+ code: stdinError.code ?? null,
167
+ message: stdinError.message,
168
+ hint: stdinError.code === "EPIPE" ? "Child process exited before consuming all stdin data" : "Unexpected stdin write error"
169
+ });
170
+ });
171
+ child.on("error", (err) => {
172
+ logger.error("interpret-analyzer-spawn-error", {
173
+ command,
174
+ args,
175
+ error: err
176
+ });
177
+ const error = err;
178
+ if (error.code === "ENOENT") {
179
+ reject(
180
+ new Error(
181
+ `Command not found: ${command}. Configure AI with 'libretto-cli ai configure'.`
182
+ )
183
+ );
184
+ return;
185
+ }
186
+ reject(err);
187
+ });
188
+ child.on("close", (code) => {
189
+ const stdinNote = formatStdinError(stderr, stdinError);
190
+ const combinedStderr = `${stderr}${stdinNote}`;
191
+ logger.info("interpret-analyzer-spawn-close", {
192
+ command,
193
+ args,
194
+ exitCode: code ?? 1,
195
+ durationMs: Date.now() - startedAt,
196
+ stdoutChars: stdout.length,
197
+ stderrChars: combinedStderr.length,
198
+ stdinErrorCode: stdinError?.code ?? null,
199
+ stdoutPreview: summarizeForLog(stdout),
200
+ stderrPreview: summarizeForLog(combinedStderr)
201
+ });
202
+ resolve2({
203
+ exitCode: code ?? 1,
204
+ stdout,
205
+ stderr: combinedStderr
206
+ });
207
+ });
208
+ try {
209
+ if (stdinText !== void 0) {
210
+ child.stdin.end(stdinText);
211
+ } else {
212
+ child.stdin.end();
213
+ }
214
+ } catch (err) {
215
+ stdinError = err;
216
+ logger.warn("interpret-analyzer-stdin-write-error", {
217
+ command,
218
+ args,
219
+ code: stdinError.code ?? null,
220
+ message: stdinError.message,
221
+ hint: stdinError.code === "EPIPE" ? "Child process exited before consuming all stdin data" : "Unexpected stdin write error"
222
+ });
223
+ }
224
+ });
225
+ }
226
+ function stripAnsi(value) {
227
+ return value.replace(
228
+ /\u001b\[[0-9;]*[A-Za-z]|\u001b\][^\u0007]*(?:\u0007|\u001b\\)/g,
229
+ ""
230
+ );
231
+ }
232
+ function summarizeForLog(value, maxChars = 800) {
233
+ const cleaned = stripAnsi(value).trim();
234
+ if (!cleaned) return "";
235
+ if (cleaned.length <= maxChars) return cleaned;
236
+ return `${cleaned.slice(0, maxChars)}\u2026 [truncated ${cleaned.length - maxChars} chars]`;
237
+ }
238
+ function formatStdinError(stderr, error) {
239
+ if (!error) return "";
240
+ const detail = error.code === "EPIPE" ? "Analyzer closed stdin before Libretto finished sending the snapshot prompt." : `Analyzer stdin error: ${error.message}`;
241
+ if (stderr.includes(detail)) return "";
242
+ return `${stderr.endsWith("\n") || stderr.length === 0 ? "" : "\n"}${detail}
243
+ `;
244
+ }
245
+ function extractJsonObjectCandidates(text) {
246
+ const candidates = [];
247
+ const seen = /* @__PURE__ */ new Set();
248
+ const add = (value) => {
249
+ const trimmed = value.trim();
250
+ if (!trimmed || seen.has(trimmed)) return;
251
+ seen.add(trimmed);
252
+ candidates.push(trimmed);
253
+ };
254
+ try {
255
+ const direct = text.trim();
256
+ if (direct.startsWith("{") && direct.endsWith("}")) {
257
+ add(direct);
258
+ }
259
+ } catch {
260
+ }
261
+ const codeBlockRegex = /```(?:json)?\s*([\s\S]*?)```/gi;
262
+ let codeBlockMatch;
263
+ while ((codeBlockMatch = codeBlockRegex.exec(text)) !== null) {
264
+ const body = codeBlockMatch[1]?.trim();
265
+ if (body && body.startsWith("{") && body.endsWith("}")) {
266
+ add(body);
267
+ }
268
+ }
269
+ const lines = text.split("\n");
270
+ for (const line of lines) {
271
+ const trimmed = line.trim();
272
+ if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
273
+ add(trimmed);
274
+ }
275
+ }
276
+ let depth = 0;
277
+ let start = -1;
278
+ let inString = false;
279
+ let escaped = false;
280
+ for (let i = 0; i < text.length; i++) {
281
+ const char = text[i];
282
+ if (inString) {
283
+ if (escaped) {
284
+ escaped = false;
285
+ continue;
286
+ }
287
+ if (char === "\\") {
288
+ escaped = true;
289
+ continue;
290
+ }
291
+ if (char === '"') {
292
+ inString = false;
293
+ }
294
+ continue;
295
+ }
296
+ if (char === '"') {
297
+ inString = true;
298
+ continue;
299
+ }
300
+ if (char === "{") {
301
+ if (depth === 0) start = i;
302
+ depth += 1;
303
+ continue;
304
+ }
305
+ if (char === "}") {
306
+ if (depth > 0) depth -= 1;
307
+ if (depth === 0 && start >= 0) {
308
+ add(text.slice(start, i + 1));
309
+ start = -1;
310
+ }
311
+ }
312
+ }
313
+ return candidates;
314
+ }
315
+ function collectStringLeaves(value, out, depth = 0) {
316
+ if (depth > 6 || value == null) return;
317
+ if (typeof value === "string") {
318
+ out.push(value);
319
+ return;
320
+ }
321
+ if (Array.isArray(value)) {
322
+ for (const item of value) {
323
+ collectStringLeaves(item, out, depth + 1);
324
+ }
325
+ return;
326
+ }
327
+ if (typeof value === "object") {
328
+ for (const nested of Object.values(value)) {
329
+ collectStringLeaves(nested, out, depth + 1);
330
+ }
331
+ }
332
+ }
333
+ function parseInterpretResultFromText(text) {
334
+ const cleaned = stripAnsi(text).trim();
335
+ const candidates = extractJsonObjectCandidates(cleaned);
336
+ if (candidates.length === 0) {
337
+ throw new Error(
338
+ "Analyzer output did not include a JSON object matching the interpret schema."
339
+ );
340
+ }
341
+ for (const candidate of candidates) {
342
+ try {
343
+ const parsed = JSON.parse(candidate);
344
+ const valid = InterpretResultSchema.safeParse(parsed);
345
+ if (valid.success) {
346
+ return valid.data;
347
+ }
348
+ const nestedStrings = [];
349
+ collectStringLeaves(parsed, nestedStrings);
350
+ for (const nestedText of nestedStrings) {
351
+ const nestedCandidates = extractJsonObjectCandidates(nestedText);
352
+ for (const nestedCandidate of nestedCandidates) {
353
+ try {
354
+ const nestedParsed = JSON.parse(nestedCandidate);
355
+ const nestedValid = InterpretResultSchema.safeParse(nestedParsed);
356
+ if (nestedValid.success) {
357
+ return nestedValid.data;
358
+ }
359
+ } catch {
360
+ }
361
+ }
362
+ }
363
+ } catch {
364
+ }
365
+ }
366
+ throw new Error(
367
+ "Analyzer output could not be parsed as valid interpret JSON. Ensure the configured command returns only the requested JSON object."
368
+ );
369
+ }
370
+ function resolvePath(filePath) {
371
+ return isAbsolute(filePath) ? filePath : resolve(process.cwd(), filePath);
372
+ }
373
+ function getMimeType(filePath) {
374
+ const ext = extname(filePath).toLowerCase();
375
+ if (ext === ".png") return "image/png";
376
+ if (ext === ".jpg" || ext === ".jpeg") return "image/jpeg";
377
+ if (ext === ".webp") return "image/webp";
378
+ if (ext === ".gif") return "image/gif";
379
+ return "application/octet-stream";
380
+ }
381
+ function readFileAsBase64(filePath) {
382
+ return readFileSync(filePath).toString("base64");
383
+ }
384
+ function truncateText(text, maxChars) {
385
+ if (text.length <= maxChars) {
386
+ return { text, truncated: false };
387
+ }
388
+ const head = text.slice(0, Math.floor(maxChars * 0.6));
389
+ const tail = text.slice(-Math.floor(maxChars * 0.4));
390
+ return {
391
+ text: `${head}
392
+
393
+ ... [truncated] ...
394
+
395
+ ${tail}`,
396
+ truncated: true
397
+ };
398
+ }
399
+ function collectSelectorHints(html, limit = 120) {
400
+ const candidates = [];
401
+ const seen = /* @__PURE__ */ new Set();
402
+ const add = (value) => {
403
+ if (candidates.length >= limit || seen.has(value)) return;
404
+ seen.add(value);
405
+ candidates.push(value);
406
+ };
407
+ const selectors = [
408
+ { attr: "data-testid", format: (value) => `[data-testid="${value}"]` },
409
+ { attr: "data-test", format: (value) => `[data-test="${value}"]` },
410
+ { attr: "data-qa", format: (value) => `[data-qa="${value}"]` },
411
+ { attr: "aria-label", format: (value) => `[aria-label="${value}"]` },
412
+ { attr: "role", format: (value) => `[role="${value}"]` },
413
+ { attr: "name", format: (value) => `[name="${value}"]` },
414
+ { attr: "placeholder", format: (value) => `[placeholder="${value}"]` },
415
+ { attr: "id", format: (value) => `#${value}` }
416
+ ];
417
+ for (const selector of selectors) {
418
+ const regex = new RegExp(`${selector.attr}\\s*=\\s*["']([^"']+)["']`, "gi");
419
+ let match;
420
+ while ((match = regex.exec(html)) !== null) {
421
+ const value = match[1]?.trim();
422
+ if (!value) continue;
423
+ add(selector.format(value));
424
+ if (candidates.length >= limit) break;
425
+ }
426
+ if (candidates.length >= limit) break;
427
+ }
428
+ return candidates;
429
+ }
430
+ async function runInterpret(args, logger) {
431
+ logger.info("interpret-start", {
432
+ objective: args.objective,
433
+ pngPath: args.pngPath,
434
+ htmlPath: args.htmlPath
435
+ });
436
+ process.env.NODE_ENV = "development";
437
+ const pngPath = resolvePath(args.pngPath);
438
+ const htmlPath = resolvePath(args.htmlPath);
439
+ if (!existsSync(pngPath)) {
440
+ throw new Error(`PNG file not found: ${pngPath}`);
441
+ }
442
+ if (!existsSync(htmlPath)) {
443
+ throw new Error(`HTML file not found: ${htmlPath}`);
444
+ }
445
+ const htmlContent = readFileSync(htmlPath, "utf-8");
446
+ const htmlCharLimit = 5e5;
447
+ const { text: trimmedHtml, truncated } = truncateText(
448
+ htmlContent,
449
+ htmlCharLimit
450
+ );
451
+ const selectorHints = collectSelectorHints(htmlContent, 120);
452
+ let prompt = `# Objective
453
+ ${args.objective}
454
+
455
+ `;
456
+ prompt += `# Context
457
+ ${args.context}
458
+
459
+ `;
460
+ prompt += `# Instructions
461
+ `;
462
+ prompt += `You are analyzing a screenshot and HTML snapshot of the same web page on behalf of an automation agent.
463
+ `;
464
+ prompt += `The agent needs to interact with this page programmatically using Playwright.
465
+
466
+ `;
467
+ prompt += `Based on the objective and context above:
468
+ `;
469
+ prompt += `1. Answer the objective concisely
470
+ `;
471
+ prompt += `2. Identify ALL interactive elements relevant to the objective and provide Playwright-ready CSS selectors
472
+ `;
473
+ prompt += `3. Note any relevant page state (loading indicators, error messages, disabled elements, modals/overlays)
474
+ `;
475
+ prompt += `4. If elements are inside iframes, identify the iframe selector and the element selector within it
476
+
477
+ `;
478
+ prompt += `Output JSON with this shape:
479
+ `;
480
+ prompt += `{"answer": string, "selectors": [{"label": string, "selector": string, "rationale": string}], "notes": string}
481
+
482
+ `;
483
+ prompt += `Selectors should prefer robust attributes: data-testid, data-test, aria-label, name, id, role. Avoid fragile class-based or positional selectors.
484
+ `;
485
+ prompt += `Only include selectors that exist in the HTML snapshot.
486
+
487
+ `;
488
+ if (selectorHints.length > 0) {
489
+ prompt += `Selector hints from HTML attributes (use if relevant):
490
+ `;
491
+ prompt += selectorHints.map((hint) => `- ${hint}`).join("\n");
492
+ prompt += "\n\n";
493
+ }
494
+ if (truncated) {
495
+ prompt += `HTML content is truncated to fit token limits.
496
+
497
+ `;
498
+ }
499
+ prompt += `HTML snapshot:
500
+
501
+ ${trimmedHtml}`;
502
+ prompt += "\n\nReturn only a JSON object. Do not include markdown code fences or extra commentary.";
503
+ let parsed;
504
+ const configuredAgent = UserCodingAgent.getConfigured();
505
+ if (configuredAgent) {
506
+ const configuredAnalyzer = configuredAgent.snapshotAnalyzerConfig;
507
+ logger.info("interpret-analyzer-config", {
508
+ preset: configuredAnalyzer.preset,
509
+ commandPrefix: configuredAnalyzer.commandPrefix
510
+ });
511
+ parsed = await configuredAgent.analyzeSnapshot(prompt, pngPath, logger);
512
+ } else {
513
+ const llmClientFactory = getLLMClientFactory();
514
+ if (!llmClientFactory) {
515
+ throw new Error(
516
+ "No AI config set. Run 'libretto-cli ai configure codex' (or claude/gemini). Library integrations can still set a factory via setLLMClientFactory()."
517
+ );
518
+ }
519
+ logger.info("interpret-analyzer-factory-fallback", {});
520
+ const imageBase64 = readFileAsBase64(pngPath);
521
+ const client = await llmClientFactory(logger, "google/gemini-3-flash-preview");
522
+ const result = await client.generateObjectFromMessages({
523
+ schema: InterpretResultSchema,
524
+ messages: [
525
+ {
526
+ role: "user",
527
+ content: [
528
+ { type: "text", text: prompt },
529
+ {
530
+ type: "image",
531
+ image: `data:${getMimeType(pngPath)};base64,${imageBase64}`
532
+ }
533
+ ]
534
+ }
535
+ ],
536
+ temperature: 0.1
537
+ });
538
+ parsed = InterpretResultSchema.parse(result);
539
+ }
540
+ logger.info("interpret-success", {
541
+ selectorCount: parsed.selectors.length,
542
+ answer: parsed.answer.slice(0, 200)
543
+ });
544
+ const outputLines = [];
545
+ outputLines.push("Interpretation:");
546
+ outputLines.push(`Answer: ${parsed.answer}`);
547
+ outputLines.push("");
548
+ if (parsed.selectors.length === 0) {
549
+ outputLines.push("Selectors: none found.");
550
+ } else {
551
+ outputLines.push("Selectors:");
552
+ parsed.selectors.forEach((selector, index) => {
553
+ outputLines.push(` ${index + 1}. ${selector.label}`);
554
+ outputLines.push(` selector: ${selector.selector}`);
555
+ outputLines.push(` rationale: ${selector.rationale}`);
556
+ });
557
+ }
558
+ if (parsed.notes.trim()) {
559
+ outputLines.push("");
560
+ outputLines.push(`Notes: ${parsed.notes.trim()}`);
561
+ }
562
+ console.log(outputLines.join("\n"));
563
+ }
564
+ function canAnalyzeSnapshots() {
565
+ return UserCodingAgent.getConfigured() !== null || getLLMClientFactory() !== null;
566
+ }
567
+ export {
568
+ canAnalyzeSnapshots,
569
+ runInterpret
570
+ };