hypha-debugger 0.1.11 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hypha-debugger.js +3795 -328
- package/dist/hypha-debugger.js.map +1 -1
- package/dist/hypha-debugger.min.js +21 -12
- package/dist/hypha-debugger.min.js.map +1 -1
- package/dist/hypha-debugger.mjs +81 -37
- package/dist/hypha-debugger.mjs.map +1 -1
- package/package.json +8 -8
package/dist/hypha-debugger.mjs
CHANGED
|
@@ -1972,6 +1972,23 @@ async function toJpeg(node, options = {}) {
|
|
|
1972
1972
|
* Images are downscaled before being returned so agents don't receive
|
|
1973
1973
|
* multi-megabyte base64 payloads that can crash their context window.
|
|
1974
1974
|
*/
|
|
1975
|
+
/** Extract a useful string from an unknown error value. */
|
|
1976
|
+
function errorMessage(err) {
|
|
1977
|
+
if (!err)
|
|
1978
|
+
return "Unknown error";
|
|
1979
|
+
if (typeof err === "string")
|
|
1980
|
+
return err;
|
|
1981
|
+
if (err.message)
|
|
1982
|
+
return err.message;
|
|
1983
|
+
if (err instanceof Event)
|
|
1984
|
+
return `Event: ${err.type}`;
|
|
1985
|
+
try {
|
|
1986
|
+
return JSON.stringify(err);
|
|
1987
|
+
}
|
|
1988
|
+
catch {
|
|
1989
|
+
return String(err);
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1975
1992
|
/**
|
|
1976
1993
|
* Resize an image data URL via a canvas. Returns a new data URL at the
|
|
1977
1994
|
* requested format/quality. Maintains aspect ratio: fits within
|
|
@@ -1981,31 +1998,34 @@ async function resizeDataUrl(dataUrl, maxWidth, maxHeight, format, quality) {
|
|
|
1981
1998
|
return new Promise((resolve, reject) => {
|
|
1982
1999
|
const img = new Image();
|
|
1983
2000
|
img.onload = () => {
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
2001
|
+
try {
|
|
2002
|
+
const srcW = img.naturalWidth;
|
|
2003
|
+
const srcH = img.naturalHeight;
|
|
2004
|
+
const scale = Math.min(maxWidth / srcW, maxHeight / srcH, 1);
|
|
2005
|
+
const dstW = Math.max(1, Math.round(srcW * scale));
|
|
2006
|
+
const dstH = Math.max(1, Math.round(srcH * scale));
|
|
2007
|
+
const canvas = document.createElement("canvas");
|
|
2008
|
+
canvas.width = dstW;
|
|
2009
|
+
canvas.height = dstH;
|
|
2010
|
+
const ctx = canvas.getContext("2d");
|
|
2011
|
+
if (!ctx) {
|
|
2012
|
+
reject(new Error("Could not get 2D canvas context"));
|
|
2013
|
+
return;
|
|
2014
|
+
}
|
|
2015
|
+
if (format === "jpeg") {
|
|
2016
|
+
ctx.fillStyle = "#ffffff";
|
|
2017
|
+
ctx.fillRect(0, 0, dstW, dstH);
|
|
2018
|
+
}
|
|
2019
|
+
ctx.drawImage(img, 0, 0, dstW, dstH);
|
|
2020
|
+
const mime = format === "jpeg" ? "image/jpeg" : "image/png";
|
|
2021
|
+
const out = canvas.toDataURL(mime, quality);
|
|
2022
|
+
resolve({ dataUrl: out, width: dstW, height: dstH });
|
|
1997
2023
|
}
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
ctx.fillStyle = "#ffffff";
|
|
2001
|
-
ctx.fillRect(0, 0, dstW, dstH);
|
|
2024
|
+
catch (drawErr) {
|
|
2025
|
+
reject(new Error(`Canvas resize failed: ${errorMessage(drawErr)}`));
|
|
2002
2026
|
}
|
|
2003
|
-
ctx.drawImage(img, 0, 0, dstW, dstH);
|
|
2004
|
-
const mime = format === "jpeg" ? "image/jpeg" : "image/png";
|
|
2005
|
-
const out = canvas.toDataURL(mime, quality);
|
|
2006
|
-
resolve({ dataUrl: out, width: dstW, height: dstH });
|
|
2007
2027
|
};
|
|
2008
|
-
img.onerror = () => reject(new Error(
|
|
2028
|
+
img.onerror = (ev) => reject(new Error(`Failed to load captured image for resizing${ev instanceof Event ? ` (${ev.type})` : ""}`));
|
|
2009
2029
|
img.src = dataUrl;
|
|
2010
2030
|
});
|
|
2011
2031
|
}
|
|
@@ -2058,25 +2078,49 @@ async function takeScreenshot(selector, format, quality, max_width, max_height,
|
|
|
2058
2078
|
captureOptions.height = viewportH;
|
|
2059
2079
|
}
|
|
2060
2080
|
let dataUrl;
|
|
2061
|
-
|
|
2062
|
-
|
|
2081
|
+
try {
|
|
2082
|
+
if (fmt === "jpeg") {
|
|
2083
|
+
dataUrl = await toJpeg(node, captureOptions);
|
|
2084
|
+
}
|
|
2085
|
+
else {
|
|
2086
|
+
dataUrl = await toPng(node, captureOptions);
|
|
2087
|
+
}
|
|
2063
2088
|
}
|
|
2064
|
-
|
|
2065
|
-
|
|
2089
|
+
catch (captureErr) {
|
|
2090
|
+
return {
|
|
2091
|
+
error: `Capture failed (html-to-image): ${errorMessage(captureErr)}`,
|
|
2092
|
+
};
|
|
2093
|
+
}
|
|
2094
|
+
// Resize down to fit within (maxW × maxH) and re-encode. If resize
|
|
2095
|
+
// fails (e.g. data URL too large to load back into an Image), fall
|
|
2096
|
+
// back to returning the original capture so the caller still gets
|
|
2097
|
+
// something useful.
|
|
2098
|
+
try {
|
|
2099
|
+
const resized = await resizeDataUrl(dataUrl, maxW, maxH, fmt, qual);
|
|
2100
|
+
const sizeKb = Math.round((resized.dataUrl.length * 0.75) / 1024);
|
|
2101
|
+
return {
|
|
2102
|
+
data: resized.dataUrl,
|
|
2103
|
+
format: fmt,
|
|
2104
|
+
width: resized.width,
|
|
2105
|
+
height: resized.height,
|
|
2106
|
+
size_kb: sizeKb,
|
|
2107
|
+
};
|
|
2108
|
+
}
|
|
2109
|
+
catch (resizeErr) {
|
|
2110
|
+
const rect = node.getBoundingClientRect();
|
|
2111
|
+
const sizeKb = Math.round((dataUrl.length * 0.75) / 1024);
|
|
2112
|
+
return {
|
|
2113
|
+
data: dataUrl,
|
|
2114
|
+
format: fmt,
|
|
2115
|
+
width: Math.round(rect.width),
|
|
2116
|
+
height: Math.round(rect.height),
|
|
2117
|
+
size_kb: sizeKb,
|
|
2118
|
+
warning: `Resize failed, returning original: ${errorMessage(resizeErr)}`,
|
|
2119
|
+
};
|
|
2066
2120
|
}
|
|
2067
|
-
// Resize down to fit within (maxW × maxH) and re-encode
|
|
2068
|
-
const resized = await resizeDataUrl(dataUrl, maxW, maxH, fmt, qual);
|
|
2069
|
-
const sizeKb = Math.round((resized.dataUrl.length * 0.75) / 1024); // rough base64 → bytes
|
|
2070
|
-
return {
|
|
2071
|
-
data: resized.dataUrl,
|
|
2072
|
-
format: fmt,
|
|
2073
|
-
width: resized.width,
|
|
2074
|
-
height: resized.height,
|
|
2075
|
-
size_kb: sizeKb,
|
|
2076
|
-
};
|
|
2077
2121
|
}
|
|
2078
2122
|
catch (err) {
|
|
2079
|
-
return { error: `Screenshot failed: ${err
|
|
2123
|
+
return { error: `Screenshot failed: ${errorMessage(err)}` };
|
|
2080
2124
|
}
|
|
2081
2125
|
}
|
|
2082
2126
|
takeScreenshot.__schema__ = {
|