@aiyiran/myclaw 1.1.146 → 1.1.150

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.
@@ -19,7 +19,7 @@
19
19
 
20
20
  window.__OPENCLAW_DEBUG_PROBE_INSTALLED__ = true;
21
21
 
22
- const PROBE_VERSION = "probe_001";
22
+ const PROBE_VERSION = "probe_003";
23
23
  const MAX_TEXT_LENGTH = 1000;
24
24
  const MAX_STACK_LENGTH = 3000;
25
25
  const MAX_ELEMENT_TEXT_LENGTH = 80;
@@ -135,6 +135,13 @@
135
135
  };
136
136
  }
137
137
 
138
+ const _isInIframe = (function () {
139
+ try { return window.parent !== window; } catch (e) { return false; }
140
+ })();
141
+
142
+ // 保存原始 console.log,防止 probe 覆盖后造成递归
143
+ const _nativeLog = console.log.bind(console);
144
+
138
145
  function sendEvent(type, payload) {
139
146
  const event = {
140
147
  type,
@@ -145,7 +152,7 @@
145
152
  };
146
153
 
147
154
  try {
148
- if (window.parent && window.parent !== window) {
155
+ if (_isInIframe) {
149
156
  window.parent.postMessage(
150
157
  {
151
158
  source: "OPENCLAW_DEBUG_PROBE",
@@ -154,6 +161,9 @@
154
161
  },
155
162
  "*"
156
163
  );
164
+ } else {
165
+ // 独立页面模式:直接打到控制台,方便本地调试
166
+ _nativeLog("[PROBE]", type, payload);
157
167
  }
158
168
  } catch (error) {
159
169
  // 不让 probe 影响学生作品运行
@@ -227,6 +237,66 @@
227
237
  });
228
238
  }
229
239
 
240
+ // 去重:避免 setTimeout 拦截 和 window.error 事件 重复上报同一个错误
241
+ const _reportedErrorKeys = new Set();
242
+
243
+ function reportRuntimeError(message, filename, lineno, colno, stack) {
244
+ const key = truncate(message, 200);
245
+ if (_reportedErrorKeys.has(key)) return;
246
+ _reportedErrorKeys.add(key);
247
+ sendEvent("runtime-error", {
248
+ message: truncate(message || "Unknown runtime error", MAX_TEXT_LENGTH),
249
+ filename: filename || "",
250
+ lineno: lineno || null,
251
+ colno: colno || null,
252
+ stack: truncate(stack || "", MAX_STACK_LENGTH)
253
+ });
254
+ }
255
+
256
+ // 拦截 setTimeout / setInterval,捕获回调里的同步异常
257
+ function setupAsyncErrorCapture() {
258
+ const _origSetTimeout = window.setTimeout;
259
+ const _origSetInterval = window.setInterval;
260
+
261
+ window.setTimeout = function (fn, delay) {
262
+ const args = Array.prototype.slice.call(arguments, 2);
263
+ if (typeof fn !== "function") return _origSetTimeout.apply(window, arguments);
264
+ return _origSetTimeout.call(window, function () {
265
+ try {
266
+ fn.apply(this, args);
267
+ } catch (e) {
268
+ reportRuntimeError(
269
+ e && e.message ? e.message : String(e),
270
+ e && e.fileName ? e.fileName : "",
271
+ e && e.lineNumber ? e.lineNumber : null,
272
+ e && e.columnNumber ? e.columnNumber : null,
273
+ e && e.stack ? e.stack : ""
274
+ );
275
+ throw e;
276
+ }
277
+ }, delay);
278
+ };
279
+
280
+ window.setInterval = function (fn, delay) {
281
+ const args = Array.prototype.slice.call(arguments, 2);
282
+ if (typeof fn !== "function") return _origSetInterval.apply(window, arguments);
283
+ return _origSetInterval.call(window, function () {
284
+ try {
285
+ fn.apply(this, args);
286
+ } catch (e) {
287
+ reportRuntimeError(
288
+ e && e.message ? e.message : String(e),
289
+ e && e.fileName ? e.fileName : "",
290
+ e && e.lineNumber ? e.lineNumber : null,
291
+ e && e.columnNumber ? e.columnNumber : null,
292
+ e && e.stack ? e.stack : ""
293
+ );
294
+ throw e;
295
+ }
296
+ }, delay);
297
+ };
298
+ }
299
+
230
300
  function setupRuntimeErrorListener() {
231
301
  window.addEventListener(
232
302
  "error",
@@ -249,19 +319,14 @@
249
319
  return;
250
320
  }
251
321
 
252
- // JS 运行时报错
253
- sendEvent("runtime-error", {
254
- message: truncate(event.message || "Unknown runtime error", MAX_TEXT_LENGTH),
255
- filename: event.filename || "",
256
- lineno: event.lineno || null,
257
- colno: event.colno || null,
258
- stack: truncate(
259
- event.error && event.error.stack
260
- ? event.error.stack
261
- : "",
262
- MAX_STACK_LENGTH
263
- )
264
- });
322
+ // JS 运行时报错(兜底,可能已被 setTimeout 拦截器上报)
323
+ reportRuntimeError(
324
+ event.message,
325
+ event.filename,
326
+ event.lineno,
327
+ event.colno,
328
+ event.error && event.error.stack ? event.error.stack : ""
329
+ );
265
330
  },
266
331
  true
267
332
  );
@@ -289,6 +354,7 @@
289
354
  }
290
355
 
291
356
  function init() {
357
+ setupAsyncErrorCapture();
292
358
  setupConsoleListener();
293
359
  setupRuntimeErrorListener();
294
360
  setupUnhandledRejectionListener();
@@ -558,9 +558,6 @@
558
558
  } else if (rec.workspace) {
559
559
  lines.push("调试记录:" + rec.workspace + "/debug/debug-record.html");
560
560
  }
561
- if (rec.entryFile) {
562
- lines.push("正在调试的文件:" + rec.entryFile);
563
- }
564
561
  lines.push("");
565
562
  }
566
563
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiyiran/myclaw",
3
- "version": "1.1.146",
3
+ "version": "1.1.150",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {