@yourgpt/copilot-sdk 1.4.1 → 1.4.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.
@@ -10,12 +10,99 @@ var DEFAULT_OPTIONS = {
10
10
  maxHeight: 1080,
11
11
  includeCursor: false
12
12
  };
13
- var html2canvasPromise = null;
14
- async function getHtml2Canvas() {
15
- if (!html2canvasPromise) {
16
- html2canvasPromise = import('html2canvas').then((mod) => mod.default);
13
+ var htmlToImagePromise = null;
14
+ async function getHtmlToImage() {
15
+ if (!htmlToImagePromise) {
16
+ htmlToImagePromise = import('html-to-image');
17
+ }
18
+ return htmlToImagePromise;
19
+ }
20
+ async function captureWithHtmlToImage(element, opts) {
21
+ const htmlToImage = await getHtmlToImage();
22
+ const rect = element.getBoundingClientRect();
23
+ const width = rect.width || window.innerWidth;
24
+ const height = rect.height || window.innerHeight;
25
+ const scale = Math.min(opts.maxWidth / width, opts.maxHeight / height, 1);
26
+ let dataUrl;
27
+ if (opts.format === "jpeg") {
28
+ dataUrl = await htmlToImage.toJpeg(element, {
29
+ quality: opts.quality,
30
+ pixelRatio: scale,
31
+ skipAutoScale: true,
32
+ cacheBust: true
33
+ });
34
+ } else {
35
+ dataUrl = await htmlToImage.toPng(element, {
36
+ pixelRatio: scale,
37
+ skipAutoScale: true,
38
+ cacheBust: true
39
+ });
40
+ }
41
+ const img = new Image();
42
+ await new Promise((resolve, reject) => {
43
+ img.onload = () => resolve();
44
+ img.onerror = () => reject(new Error("Failed to load captured image"));
45
+ img.src = dataUrl;
46
+ });
47
+ return {
48
+ data: dataUrl,
49
+ format: opts.format,
50
+ width: img.width,
51
+ height: img.height,
52
+ timestamp: Date.now()
53
+ };
54
+ }
55
+ async function captureWithDisplayMedia(opts) {
56
+ const stream = await navigator.mediaDevices.getDisplayMedia({
57
+ video: {
58
+ displaySurface: "browser"
59
+ },
60
+ audio: false,
61
+ // @ts-expect-error - preferCurrentTab is a newer API
62
+ preferCurrentTab: true
63
+ });
64
+ try {
65
+ const track = stream.getVideoTracks()[0];
66
+ const settings = track.getSettings();
67
+ const video = document.createElement("video");
68
+ video.srcObject = stream;
69
+ video.muted = true;
70
+ await new Promise((resolve, reject) => {
71
+ video.onloadedmetadata = () => {
72
+ video.play().then(() => resolve()).catch(reject);
73
+ };
74
+ video.onerror = () => reject(new Error("Failed to load video stream"));
75
+ });
76
+ await new Promise((resolve) => setTimeout(resolve, 100));
77
+ const videoWidth = settings.width || video.videoWidth || window.innerWidth;
78
+ const videoHeight = settings.height || video.videoHeight || window.innerHeight;
79
+ const scale = Math.min(
80
+ opts.maxWidth / videoWidth,
81
+ opts.maxHeight / videoHeight,
82
+ 1
83
+ );
84
+ const width = Math.round(videoWidth * scale);
85
+ const height = Math.round(videoHeight * scale);
86
+ const canvas = document.createElement("canvas");
87
+ canvas.width = width;
88
+ canvas.height = height;
89
+ const ctx = canvas.getContext("2d");
90
+ if (!ctx) {
91
+ throw new Error("Failed to create canvas context");
92
+ }
93
+ ctx.drawImage(video, 0, 0, width, height);
94
+ const mimeType = `image/${opts.format === "jpeg" ? "jpeg" : opts.format}`;
95
+ const data = canvas.toDataURL(mimeType, opts.quality);
96
+ return {
97
+ data,
98
+ format: opts.format,
99
+ width,
100
+ height,
101
+ timestamp: Date.now()
102
+ };
103
+ } finally {
104
+ stream.getTracks().forEach((track) => track.stop());
17
105
  }
18
- return html2canvasPromise;
19
106
  }
20
107
  async function captureScreenshot(options = {}) {
21
108
  if (!isBrowser) {
@@ -25,77 +112,45 @@ async function captureScreenshot(options = {}) {
25
112
  }
26
113
  const opts = { ...DEFAULT_OPTIONS, ...options };
27
114
  const element = opts.element || document.body;
115
+ try {
116
+ return await captureWithHtmlToImage(element, opts);
117
+ } catch (error) {
118
+ console.warn(
119
+ "[Copilot SDK] html-to-image capture failed, trying native API",
120
+ error
121
+ );
122
+ }
123
+ const hasDisplayMedia = typeof navigator !== "undefined" && typeof navigator.mediaDevices !== "undefined" && typeof navigator.mediaDevices.getDisplayMedia === "function";
124
+ if (hasDisplayMedia) {
125
+ try {
126
+ return await captureWithDisplayMedia(opts);
127
+ } catch (error) {
128
+ console.warn(
129
+ "[Copilot SDK] Screen capture cancelled or not supported",
130
+ error
131
+ );
132
+ }
133
+ }
28
134
  const rect = element.getBoundingClientRect();
29
135
  let width = rect.width || window.innerWidth;
30
136
  let height = rect.height || window.innerHeight;
31
137
  const scale = Math.min(opts.maxWidth / width, opts.maxHeight / height, 1);
32
138
  width = Math.round(width * scale);
33
139
  height = Math.round(height * scale);
34
- let canvas;
35
- try {
36
- const html2canvas = await getHtml2Canvas();
37
- canvas = await html2canvas(element, {
38
- scale,
39
- useCORS: true,
40
- // Enable cross-origin images
41
- allowTaint: false,
42
- // Don't allow tainted canvas
43
- backgroundColor: null,
44
- // Transparent background (uses element's bg)
45
- logging: false,
46
- // Disable internal logging
47
- width: rect.width,
48
- height: rect.height,
49
- windowWidth: window.innerWidth,
50
- windowHeight: window.innerHeight,
51
- scrollX: 0,
52
- scrollY: 0,
53
- x: rect.left + window.scrollX,
54
- y: rect.top + window.scrollY
55
- });
56
- } catch (error) {
57
- canvas = document.createElement("canvas");
58
- canvas.width = width;
59
- canvas.height = height;
60
- const ctx = canvas.getContext("2d");
61
- if (ctx) {
62
- createPlaceholder(ctx, width, height, element, String(error));
63
- }
140
+ const canvas = document.createElement("canvas");
141
+ canvas.width = width;
142
+ canvas.height = height;
143
+ const ctx = canvas.getContext("2d");
144
+ if (ctx) {
145
+ createPlaceholder(ctx, width, height, element, "Screenshot capture failed");
64
146
  }
65
147
  const mimeType = `image/${opts.format === "jpeg" ? "jpeg" : opts.format}`;
66
- let data;
67
- try {
68
- data = canvas.toDataURL(mimeType, opts.quality);
69
- } catch (e) {
70
- if (e instanceof DOMException && e.name === "SecurityError") {
71
- console.warn(
72
- "[Copilot SDK] Canvas tainted by cross-origin content. Creating placeholder."
73
- );
74
- const cleanCanvas = document.createElement("canvas");
75
- cleanCanvas.width = width;
76
- cleanCanvas.height = height;
77
- const cleanCtx = cleanCanvas.getContext("2d");
78
- if (cleanCtx) {
79
- createPlaceholder(
80
- cleanCtx,
81
- width,
82
- height,
83
- element,
84
- "Cross-origin content blocked"
85
- );
86
- data = cleanCanvas.toDataURL(mimeType, opts.quality);
87
- } else {
88
- throw new Error("Failed to create placeholder canvas");
89
- }
90
- } else {
91
- throw e;
92
- }
93
- }
148
+ const data = canvas.toDataURL(mimeType, opts.quality);
94
149
  return {
95
150
  data,
96
151
  format: opts.format,
97
- width: canvas.width,
98
- height: canvas.height,
152
+ width,
153
+ height,
99
154
  timestamp: Date.now()
100
155
  };
101
156
  }
@@ -122,7 +177,7 @@ function createPlaceholder(ctx, width, height, element, errorMessage) {
122
177
  width / 2,
123
178
  height / 2
124
179
  );
125
- if (errorMessage) {
180
+ {
126
181
  ctx.fillStyle = "#999";
127
182
  ctx.font = "12px system-ui, sans-serif";
128
183
  ctx.fillText(
@@ -1134,6 +1189,10 @@ function createScreenshotTool(options) {
1134
1189
  }
1135
1190
 
1136
1191
  // src/core/tools/builtin/console.ts
1192
+ var isBrowser4 = typeof window !== "undefined" && typeof console !== "undefined";
1193
+ if (isBrowser4 && !isConsoleCaptureActive()) {
1194
+ startConsoleCapture();
1195
+ }
1137
1196
  var consoleLogsTool = tool({
1138
1197
  description: "Get recent console logs from the browser. Use this when debugging JavaScript errors, checking for warnings, or understanding what's happening in the application.",
1139
1198
  location: "client",
@@ -2783,5 +2842,5 @@ exports.toolToAnthropicFormat = toolToAnthropicFormat;
2783
2842
  exports.toolToOpenAIFormat = toolToOpenAIFormat;
2784
2843
  exports.zodObjectToInputSchema = zodObjectToInputSchema;
2785
2844
  exports.zodToJsonSchema = zodToJsonSchema;
2786
- //# sourceMappingURL=chunk-I623CRO4.cjs.map
2787
- //# sourceMappingURL=chunk-I623CRO4.cjs.map
2845
+ //# sourceMappingURL=chunk-CVKN4H62.cjs.map
2846
+ //# sourceMappingURL=chunk-CVKN4H62.cjs.map