libretto 0.6.23 → 0.6.24
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.
|
@@ -88,16 +88,23 @@ function readPngDimensions(buffer) {
|
|
|
88
88
|
height: buffer.readUInt32BE(20)
|
|
89
89
|
};
|
|
90
90
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
91
|
+
function toPositiveNumber(value) {
|
|
92
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : void 0;
|
|
93
|
+
}
|
|
94
|
+
async function getViewportFromPage(page) {
|
|
95
|
+
const metrics = await page.evaluate(() => ({
|
|
96
|
+
visualViewportWidth: window.visualViewport?.width,
|
|
97
|
+
visualViewportHeight: window.visualViewport?.height,
|
|
98
|
+
innerWidth: window.innerWidth,
|
|
99
|
+
innerHeight: window.innerHeight,
|
|
100
|
+
documentElementClientWidth: document.documentElement?.clientWidth,
|
|
101
|
+
documentElementClientHeight: document.documentElement?.clientHeight
|
|
102
|
+
}));
|
|
103
|
+
const width = toPositiveNumber(metrics.visualViewportWidth) ?? toPositiveNumber(metrics.innerWidth) ?? toPositiveNumber(metrics.documentElementClientWidth);
|
|
104
|
+
const height = toPositiveNumber(metrics.visualViewportHeight) ?? toPositiveNumber(metrics.innerHeight) ?? toPositiveNumber(metrics.documentElementClientHeight);
|
|
105
|
+
return width && height ? { width, height } : null;
|
|
106
|
+
}
|
|
107
|
+
function screenshotState(screenshot, viewport) {
|
|
101
108
|
const dimensions = readPngDimensions(screenshot);
|
|
102
109
|
return {
|
|
103
110
|
screenshot,
|
|
@@ -110,6 +117,18 @@ async function takeViewportScreenshot(page) {
|
|
|
110
117
|
}
|
|
111
118
|
};
|
|
112
119
|
}
|
|
120
|
+
async function takeViewportScreenshot(page) {
|
|
121
|
+
const viewport = page.viewportSize() ?? await getViewportFromPage(page).catch(() => null);
|
|
122
|
+
if (!viewport) {
|
|
123
|
+
throw new Error("Viewport size not found");
|
|
124
|
+
}
|
|
125
|
+
const screenshot = await page.screenshot({
|
|
126
|
+
fullPage: false,
|
|
127
|
+
scale: "css",
|
|
128
|
+
timeout: 1e4
|
|
129
|
+
});
|
|
130
|
+
return screenshotState(screenshot, viewport);
|
|
131
|
+
}
|
|
113
132
|
async function executeBrowserAction(page, action, logger = defaultLogger) {
|
|
114
133
|
switch (action.type) {
|
|
115
134
|
case "click": {
|
|
@@ -247,9 +266,9 @@ async function executeRecoveryAgent(page, instruction, logger, model, maxSteps =
|
|
|
247
266
|
}
|
|
248
267
|
const log = logger ?? defaultLogger;
|
|
249
268
|
log.info("Executing vision-based recovery agent", { instruction });
|
|
250
|
-
let
|
|
269
|
+
let screenshotState2;
|
|
251
270
|
try {
|
|
252
|
-
|
|
271
|
+
screenshotState2 = await takeViewportScreenshot(page);
|
|
253
272
|
} catch (screenshotError) {
|
|
254
273
|
log.warn("Failed to take screenshot for recovery agent, skipping", {
|
|
255
274
|
screenshotError: screenshotError instanceof Error ? screenshotError.message : String(screenshotError)
|
|
@@ -258,7 +277,7 @@ async function executeRecoveryAgent(page, instruction, logger, model, maxSteps =
|
|
|
258
277
|
}
|
|
259
278
|
const steps = [];
|
|
260
279
|
for (let step = 1; step <= maxSteps; step++) {
|
|
261
|
-
const { screenshot, dimensions, scale } =
|
|
280
|
+
const { screenshot, dimensions, scale } = screenshotState2;
|
|
262
281
|
const { object: result } = await generateObject({
|
|
263
282
|
model,
|
|
264
283
|
schema: recoveryActionSchema,
|
|
@@ -305,7 +324,7 @@ Analyze the screenshot and decide what action to take. If the task is complete o
|
|
|
305
324
|
await executeBrowserAction(page, action, log);
|
|
306
325
|
await delay(2e3);
|
|
307
326
|
if (step < maxSteps) {
|
|
308
|
-
|
|
327
|
+
screenshotState2 = await takeViewportScreenshot(page);
|
|
309
328
|
}
|
|
310
329
|
}
|
|
311
330
|
log.info("Recovery agent execution completed");
|
package/package.json
CHANGED
package/skills/libretto/SKILL.md
CHANGED
|
@@ -153,21 +153,41 @@ function readPngDimensions(buffer: Buffer): ImageDimensions {
|
|
|
153
153
|
};
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
|
|
156
|
+
function toPositiveNumber(value: unknown): number | undefined {
|
|
157
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0
|
|
158
|
+
? value
|
|
159
|
+
: undefined;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function getViewportFromPage(page: Page): Promise<ImageDimensions | null> {
|
|
163
|
+
const metrics = await page.evaluate(() => ({
|
|
164
|
+
visualViewportWidth: window.visualViewport?.width,
|
|
165
|
+
visualViewportHeight: window.visualViewport?.height,
|
|
166
|
+
innerWidth: window.innerWidth,
|
|
167
|
+
innerHeight: window.innerHeight,
|
|
168
|
+
documentElementClientWidth: document.documentElement?.clientWidth,
|
|
169
|
+
documentElementClientHeight: document.documentElement?.clientHeight,
|
|
170
|
+
}));
|
|
171
|
+
const width =
|
|
172
|
+
toPositiveNumber(metrics.visualViewportWidth) ??
|
|
173
|
+
toPositiveNumber(metrics.innerWidth) ??
|
|
174
|
+
toPositiveNumber(metrics.documentElementClientWidth);
|
|
175
|
+
const height =
|
|
176
|
+
toPositiveNumber(metrics.visualViewportHeight) ??
|
|
177
|
+
toPositiveNumber(metrics.innerHeight) ??
|
|
178
|
+
toPositiveNumber(metrics.documentElementClientHeight);
|
|
179
|
+
|
|
180
|
+
return width && height ? { width, height } : null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function screenshotState(
|
|
184
|
+
screenshot: Buffer,
|
|
185
|
+
viewport: ImageDimensions,
|
|
186
|
+
): {
|
|
157
187
|
screenshot: Buffer;
|
|
158
188
|
dimensions: ImageDimensions;
|
|
159
189
|
scale: CoordinateScale;
|
|
160
|
-
}
|
|
161
|
-
const viewport = page.viewportSize();
|
|
162
|
-
if (!viewport) {
|
|
163
|
-
throw new Error("Viewport size not found");
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const screenshot = await page.screenshot({
|
|
167
|
-
fullPage: false,
|
|
168
|
-
scale: "css",
|
|
169
|
-
timeout: 10000,
|
|
170
|
-
});
|
|
190
|
+
} {
|
|
171
191
|
const dimensions = readPngDimensions(screenshot);
|
|
172
192
|
return {
|
|
173
193
|
screenshot,
|
|
@@ -181,6 +201,25 @@ async function takeViewportScreenshot(page: Page): Promise<{
|
|
|
181
201
|
};
|
|
182
202
|
}
|
|
183
203
|
|
|
204
|
+
async function takeViewportScreenshot(page: Page): Promise<{
|
|
205
|
+
screenshot: Buffer;
|
|
206
|
+
dimensions: ImageDimensions;
|
|
207
|
+
scale: CoordinateScale;
|
|
208
|
+
}> {
|
|
209
|
+
const viewport =
|
|
210
|
+
page.viewportSize() ?? (await getViewportFromPage(page).catch(() => null));
|
|
211
|
+
if (!viewport) {
|
|
212
|
+
throw new Error("Viewport size not found");
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const screenshot = await page.screenshot({
|
|
216
|
+
fullPage: false,
|
|
217
|
+
scale: "css",
|
|
218
|
+
timeout: 10000,
|
|
219
|
+
});
|
|
220
|
+
return screenshotState(screenshot, viewport);
|
|
221
|
+
}
|
|
222
|
+
|
|
184
223
|
async function executeBrowserAction(
|
|
185
224
|
page: Page,
|
|
186
225
|
action: BrowserAction,
|