@pyrokine/mcp-chrome 3.0.1 → 3.1.0

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 (144) hide show
  1. package/README.md +678 -341
  2. package/README_zh.md +694 -291
  3. package/dist/cdp/client.d.ts +7 -3
  4. package/dist/cdp/client.d.ts.map +1 -1
  5. package/dist/cdp/client.js +252 -63
  6. package/dist/cdp/client.js.map +1 -1
  7. package/dist/cdp/launcher.d.ts +1 -1
  8. package/dist/cdp/launcher.d.ts.map +1 -1
  9. package/dist/cdp/launcher.js +33 -12
  10. package/dist/cdp/launcher.js.map +1 -1
  11. package/dist/core/auto-wait.d.ts.map +1 -1
  12. package/dist/core/auto-wait.js +88 -52
  13. package/dist/core/auto-wait.js.map +1 -1
  14. package/dist/core/browser-driver.d.ts +72 -15
  15. package/dist/core/browser-driver.d.ts.map +1 -1
  16. package/dist/core/browser-driver.js.map +1 -1
  17. package/dist/core/browser-event-schema.d.ts +287 -0
  18. package/dist/core/browser-event-schema.d.ts.map +1 -0
  19. package/dist/core/browser-event-schema.js +104 -0
  20. package/dist/core/browser-event-schema.js.map +1 -0
  21. package/dist/core/browser-events.d.ts +99 -0
  22. package/dist/core/browser-events.d.ts.map +1 -0
  23. package/dist/core/browser-events.js +1312 -0
  24. package/dist/core/browser-events.js.map +1 -0
  25. package/dist/core/challenge-page.d.ts +39 -0
  26. package/dist/core/challenge-page.d.ts.map +1 -0
  27. package/dist/core/challenge-page.js +226 -0
  28. package/dist/core/challenge-page.js.map +1 -0
  29. package/dist/core/dom-helpers.d.ts +28 -0
  30. package/dist/core/dom-helpers.d.ts.map +1 -0
  31. package/dist/core/dom-helpers.js +311 -0
  32. package/dist/core/dom-helpers.js.map +1 -0
  33. package/dist/core/errors.d.ts +4 -0
  34. package/dist/core/errors.d.ts.map +1 -1
  35. package/dist/core/errors.js +51 -7
  36. package/dist/core/errors.js.map +1 -1
  37. package/dist/core/file-upload.d.ts +22 -0
  38. package/dist/core/file-upload.d.ts.map +1 -0
  39. package/dist/core/file-upload.js +194 -0
  40. package/dist/core/file-upload.js.map +1 -0
  41. package/dist/core/locator.d.ts +10 -2
  42. package/dist/core/locator.d.ts.map +1 -1
  43. package/dist/core/locator.js +121 -200
  44. package/dist/core/locator.js.map +1 -1
  45. package/dist/core/os-mouse.d.ts +11 -0
  46. package/dist/core/os-mouse.d.ts.map +1 -1
  47. package/dist/core/os-mouse.js +102 -29
  48. package/dist/core/os-mouse.js.map +1 -1
  49. package/dist/core/retry.d.ts.map +1 -1
  50. package/dist/core/retry.js +4 -1
  51. package/dist/core/retry.js.map +1 -1
  52. package/dist/core/session.d.ts +61 -12
  53. package/dist/core/session.d.ts.map +1 -1
  54. package/dist/core/session.js +1040 -259
  55. package/dist/core/session.js.map +1 -1
  56. package/dist/core/types.d.ts +5 -1
  57. package/dist/core/types.d.ts.map +1 -1
  58. package/dist/core/types.js +16 -1
  59. package/dist/core/types.js.map +1 -1
  60. package/dist/core/unified-session.d.ts +41 -35
  61. package/dist/core/unified-session.d.ts.map +1 -1
  62. package/dist/core/unified-session.js +872 -263
  63. package/dist/core/unified-session.js.map +1 -1
  64. package/dist/core/utils.d.ts +5 -4
  65. package/dist/core/utils.d.ts.map +1 -1
  66. package/dist/core/utils.js +73 -18
  67. package/dist/core/utils.js.map +1 -1
  68. package/dist/extension/bridge.d.ts +48 -17
  69. package/dist/extension/bridge.d.ts.map +1 -1
  70. package/dist/extension/bridge.js +239 -37
  71. package/dist/extension/bridge.js.map +1 -1
  72. package/dist/extension/http-server.d.ts +7 -2
  73. package/dist/extension/http-server.d.ts.map +1 -1
  74. package/dist/extension/http-server.js +211 -49
  75. package/dist/extension/http-server.js.map +1 -1
  76. package/dist/index.js +14 -1
  77. package/dist/index.js.map +1 -1
  78. package/dist/tools/browse.d.ts.map +1 -1
  79. package/dist/tools/browse.js +56 -28
  80. package/dist/tools/browse.js.map +1 -1
  81. package/dist/tools/browser-event-output.d.ts +7 -0
  82. package/dist/tools/browser-event-output.d.ts.map +1 -0
  83. package/dist/tools/browser-event-output.js +19 -0
  84. package/dist/tools/browser-event-output.js.map +1 -0
  85. package/dist/tools/challenge.d.ts +17 -5
  86. package/dist/tools/challenge.d.ts.map +1 -1
  87. package/dist/tools/challenge.js +110 -189
  88. package/dist/tools/challenge.js.map +1 -1
  89. package/dist/tools/cookies.d.ts.map +1 -1
  90. package/dist/tools/cookies.js +33 -8
  91. package/dist/tools/cookies.js.map +1 -1
  92. package/dist/tools/diagnostics.d.ts.map +1 -1
  93. package/dist/tools/diagnostics.js +14 -0
  94. package/dist/tools/diagnostics.js.map +1 -1
  95. package/dist/tools/evaluate.d.ts +6 -5
  96. package/dist/tools/evaluate.d.ts.map +1 -1
  97. package/dist/tools/evaluate.js +92 -52
  98. package/dist/tools/evaluate.js.map +1 -1
  99. package/dist/tools/extract.d.ts.map +1 -1
  100. package/dist/tools/extract.js +149 -103
  101. package/dist/tools/extract.js.map +1 -1
  102. package/dist/tools/input.d.ts.map +1 -1
  103. package/dist/tools/input.js +555 -229
  104. package/dist/tools/input.js.map +1 -1
  105. package/dist/tools/logs.d.ts.map +1 -1
  106. package/dist/tools/logs.js +50 -11
  107. package/dist/tools/logs.js.map +1 -1
  108. package/dist/tools/manage.d.ts.map +1 -1
  109. package/dist/tools/manage.js +111 -16
  110. package/dist/tools/manage.js.map +1 -1
  111. package/dist/tools/network-sanitizer.d.ts +1 -0
  112. package/dist/tools/network-sanitizer.d.ts.map +1 -1
  113. package/dist/tools/network-sanitizer.js +30 -1
  114. package/dist/tools/network-sanitizer.js.map +1 -1
  115. package/dist/tools/page-context.d.ts +4 -3
  116. package/dist/tools/page-context.d.ts.map +1 -1
  117. package/dist/tools/page-context.js +56 -14
  118. package/dist/tools/page-context.js.map +1 -1
  119. package/dist/tools/post-condition.d.ts +1 -1
  120. package/dist/tools/post-condition.d.ts.map +1 -1
  121. package/dist/tools/post-condition.js +136 -44
  122. package/dist/tools/post-condition.js.map +1 -1
  123. package/dist/tools/schema.d.ts +7 -4
  124. package/dist/tools/schema.d.ts.map +1 -1
  125. package/dist/tools/schema.js +32 -122
  126. package/dist/tools/schema.js.map +1 -1
  127. package/dist/tools/target-diagnostics.d.ts +3 -3
  128. package/dist/tools/target-diagnostics.d.ts.map +1 -1
  129. package/dist/tools/target-diagnostics.js +17 -7
  130. package/dist/tools/target-diagnostics.js.map +1 -1
  131. package/dist/tools/wait.d.ts.map +1 -1
  132. package/dist/tools/wait.js +94 -40
  133. package/dist/tools/wait.js.map +1 -1
  134. package/extension/dist/assets/{content.ts-qe6IB5DK.js → content.ts-BAMOcF3R.js} +1 -1
  135. package/extension/dist/assets/{content.ts-loader-DUWbDHRS.js → content.ts-loader-4F9a1EWZ.js} +1 -1
  136. package/extension/dist/assets/index.ts-B79g2ikq.js +19820 -0
  137. package/extension/dist/assets/{page-scripts-D508WLkW.js → page-scripts-CSxn58h1.js} +257 -366
  138. package/extension/dist/assets/{popup.html-B-eVq48O.js → popup.html-DwwHo00N.js} +2 -2
  139. package/extension/dist/manifest.json +4 -4
  140. package/extension/dist/service-worker-loader.js +1 -1
  141. package/extension/dist/src/popup/popup.html +3 -3
  142. package/package.json +8 -6
  143. package/scripts/start-chrome.ps1 +9 -1
  144. package/extension/dist/assets/index.ts-D7_LCUoV.js +0 -9319
@@ -6,15 +6,18 @@
6
6
  * 2. CDP 模式:通过 Chrome DevTools Protocol 操作(Fallback)
7
7
  */
8
8
  import { AsyncLocalStorage } from 'node:async_hooks';
9
+ import { randomUUID } from 'node:crypto';
10
+ import { eventRefTarget } from './browser-events.js';
11
+ import { setTimeout as delay } from 'node:timers/promises';
12
+ import { Script } from 'node:vm';
9
13
  import { ExtensionBridge } from '../extension/index.js';
10
14
  import { DriverCapabilityError, } from './browser-driver.js';
11
- import { OsMouseError, PagePreconditionError, setErrorPageContext, StructuredToolError, TargetNotFoundError, TimeoutError, } from './errors.js';
15
+ import { isNativeExecutionResultUnknown, getErrorSource, getErrorPageContext, OsMouseError, PagePreconditionError, setErrorPageContext, setErrorSource, StructuredToolError, TargetNotFoundError, TimeoutError, } from './errors.js';
12
16
  import { isExtensionDisconnected } from './extension-errors.js';
13
17
  import { asViewportMetrics, osLeftClick, resolveTurnstileOsClickPoint, } from './os-mouse.js';
14
18
  import { getKeyDefinition, getSession as getCdpSession } from './session.js';
15
- import { DEFAULT_TIMEOUT } from './types.js';
19
+ import { DEFAULT_TIMEOUT, EvaluateResultTooLargeError, extractCdpValue, formatCdpException, isKnownNonSerializableRemoteObject, MODIFIER_KEYS, NonSerializableEvaluateResultError, } from './types.js';
16
20
  import { remainingNavigationTimeout, waitForNavigationQueue, withNavigationBudget } from './utils.js';
17
- import { EvaluateResultTooLargeError, extractCdpValue, formatCdpException, isKnownNonSerializableRemoteObject, MODIFIER_KEYS, NonSerializableEvaluateResultError, } from './types.js';
18
21
  class UnifiedSessionManager {
19
22
  static instance;
20
23
  static CONNECTION_COOLDOWN = 30000; // 连接失败后 30 秒内不重试
@@ -25,6 +28,7 @@ class UnifiedSessionManager {
25
28
  extensionBridge = null;
26
29
  inputMode = 'precise'; // 默认使用 precise 模式,可绕过 CSP 限制
27
30
  currentMousePosition = { x: 0, y: 0 }; // 跟踪鼠标位置
31
+ mouseFrameContext;
28
32
  /** 当前按下的修饰键位掩码 */
29
33
  modifiers = 0;
30
34
  /** 当前按下的所有键(用于 Puppeteer 风格的 rawKeyDown/autoRepeat 长按重复) */
@@ -40,7 +44,9 @@ class UnifiedSessionManager {
40
44
  currentFrameOffset = null; // iframe 在主页面的偏移量(withFrame 期间有效)
41
45
  lastEvaluationMetadata;
42
46
  pageMetadata = new Map();
43
- constructor() { }
47
+ constructor() {
48
+ getCdpSession().requestContext = () => this.tabLockContext.getStore();
49
+ }
44
50
  static getInstance() {
45
51
  if (!UnifiedSessionManager.instance) {
46
52
  UnifiedSessionManager.instance = new UnifiedSessionManager();
@@ -54,7 +60,7 @@ class UnifiedSessionManager {
54
60
  if (this.extensionBridge) {
55
61
  return; // 已经启动
56
62
  }
57
- this.extensionBridge = new ExtensionBridge();
63
+ this.extensionBridge = new ExtensionBridge({ requestContext: () => this.tabLockContext.getStore() });
58
64
  try {
59
65
  await this.extensionBridge.start();
60
66
  console.error(`[MCP] Extension HTTP server listening on port ${this.extensionBridge.getPort()}`);
@@ -114,6 +120,7 @@ class UnifiedSessionManager {
114
120
  }
115
121
  getPageMetadata(targetId) {
116
122
  return (this.pageMetadata.get(this.metadataKey(targetId)) ?? {
123
+ origin: 'unknown',
117
124
  createdBy: 'unknown',
118
125
  createdAt: null,
119
126
  lastUsedAt: null,
@@ -121,6 +128,15 @@ class UnifiedSessionManager {
121
128
  }
122
129
  async setPageMetadata(targetId, updates) {
123
130
  await this.getPageSnapshot(targetId);
131
+ if (this.getMode() === 'extension') {
132
+ const driver = await this.getDriver();
133
+ if (!driver.setPageMetadata) {
134
+ throw new DriverCapabilityError('Extension 不支持持久页面备注,请成套更新 Server 和 Extension');
135
+ }
136
+ const metadata = await driver.setPageMetadata(targetId, updates);
137
+ this.pageMetadata.set(this.metadataKey(targetId), metadata);
138
+ return metadata;
139
+ }
124
140
  const metadata = { ...this.getPageMetadata(targetId) };
125
141
  if (updates.taskHint !== undefined) {
126
142
  if (updates.taskHint === null) {
@@ -190,7 +206,19 @@ class UnifiedSessionManager {
190
206
  * 启动浏览器(CDP 模式)或等待 Extension 连接
191
207
  */
192
208
  async launch(options = {}) {
193
- const extensionReady = this.cdpExplicitlySelected ? false : await this.ensureExtensionConnected(options.timeout);
209
+ this.assertRequestActive();
210
+ const explicitProcess = [
211
+ options.port,
212
+ options.executablePath,
213
+ options.headless,
214
+ options.userDataDir,
215
+ options.incognito,
216
+ ].some((value) => value !== undefined);
217
+ if (explicitProcess && getCdpSession().isConnected()) {
218
+ throw new DriverCapabilityError('显式启动参数要求新的独立浏览器;当前已有 CDP 连接,未关闭或替换现有浏览器');
219
+ }
220
+ const extensionReady = explicitProcess || this.cdpExplicitlySelected ? false : await this.ensureExtensionConnected(options.timeout);
221
+ this.assertRequestActive();
194
222
  // 优先检查 Extension 是否已连接,如果断开则等待重连(受 timeout 约束)
195
223
  if (extensionReady) {
196
224
  // newPage 会设置 currentTabId,需要加锁
@@ -200,6 +228,9 @@ class UnifiedSessionManager {
200
228
  this.activeMode = 'extension';
201
229
  this.cdpExplicitlySelected = false;
202
230
  this.recordPageUse(result.targetId, true);
231
+ if (result.metadata) {
232
+ this.rememberPageMetadata(result.targetId, result.metadata);
233
+ }
203
234
  return {
204
235
  targetId: result.targetId,
205
236
  type: result.type ?? 'page',
@@ -212,7 +243,7 @@ class UnifiedSessionManager {
212
243
  // Fallback 到 CDP 模式
213
244
  const target = await getCdpSession().launch(options);
214
245
  this.activeMode = 'cdp';
215
- this.cdpExplicitlySelected = false;
246
+ this.cdpExplicitlySelected = explicitProcess;
216
247
  this.recordPageUse(target.targetId, target.targetCreated ?? target.reused !== true);
217
248
  return {
218
249
  ...target,
@@ -222,20 +253,25 @@ class UnifiedSessionManager {
222
253
  /**
223
254
  * 列出所有页面
224
255
  */
225
- async listTargets(timeout) {
256
+ async listTargets(timeout, options) {
226
257
  const mode = this.getMode();
227
258
  if (mode === 'none') {
228
259
  return [];
229
260
  }
230
261
  const deadline = timeout === undefined ? undefined : Date.now() + timeout;
231
262
  const driver = await this.getDriver(timeout);
232
- const targets = await driver.listTargets(deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
263
+ const targets = await driver.listTargets(deadline === undefined ? undefined : remainingNavigationTimeout(deadline), options);
233
264
  const liveKeys = new Set(targets.map((t) => `${mode}:${t.targetId ?? String(t.id)}`));
234
265
  for (const key of this.pageMetadata.keys()) {
235
266
  if (key.startsWith(`${mode}:`) && !liveKeys.has(key)) {
236
267
  this.pageMetadata.delete(key);
237
268
  }
238
269
  }
270
+ for (const target of targets) {
271
+ if (target.metadata) {
272
+ this.rememberPageMetadata(target.targetId ?? String(target.id), target.metadata);
273
+ }
274
+ }
239
275
  return targets.map(({ id, ...target }) => ({
240
276
  ...target,
241
277
  targetId: target.targetId ?? String(id),
@@ -245,7 +281,7 @@ class UnifiedSessionManager {
245
281
  metadata: this.getPageMetadata(target.targetId ?? String(id)),
246
282
  }));
247
283
  }
248
- async listTopology(timeout) {
284
+ async listTopology(timeout, options) {
249
285
  const mode = this.getMode();
250
286
  if (mode === 'none') {
251
287
  return null;
@@ -255,7 +291,13 @@ class UnifiedSessionManager {
255
291
  if (!driver.listWindows) {
256
292
  return null;
257
293
  }
258
- return driver.listWindows(deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
294
+ const topology = await driver.listWindows(deadline === undefined ? undefined : remainingNavigationTimeout(deadline), options);
295
+ for (const tab of topology.windows.flatMap((window) => window.tabs)) {
296
+ if (tab.metadata) {
297
+ this.rememberPageMetadata(tab.targetId ?? String(tab.id), tab.metadata);
298
+ }
299
+ }
300
+ return topology;
259
301
  }
260
302
  async setReadAccess(targetId, allow) {
261
303
  const driver = await this.getDriver();
@@ -440,7 +482,7 @@ class UnifiedSessionManager {
440
482
  */
441
483
  async click(refId) {
442
484
  const driver = await this.getDriver();
443
- await driver.click(refId);
485
+ await driver.click(refId, undefined, this.modifiers);
444
486
  }
445
487
  /**
446
488
  * 带 actionability 检查的点击(Extension 模式)
@@ -451,21 +493,43 @@ class UnifiedSessionManager {
451
493
  const driver = await this.getDriver();
452
494
  if (point) {
453
495
  this.currentMousePosition = { x: point.x, y: point.y };
496
+ this.rememberMouseFrame();
454
497
  }
455
- if (button === 'left' && clickCount === 1) {
456
- return driver.actionableClick(refId, force);
498
+ if (this.inputMode === 'stealth' || force) {
499
+ if (button === 'left' && clickCount === 1) {
500
+ return driver.actionableClick(refId, force, undefined, this.modifiers);
501
+ }
457
502
  }
458
503
  if (!force) {
459
504
  const result = await driver.checkActionability(refId);
460
- if (!result.success)
505
+ if (!result.success) {
461
506
  return result;
507
+ }
508
+ if (result.clickPoint) {
509
+ this.currentMousePosition = { ...result.clickPoint };
510
+ this.rememberMouseFrame();
511
+ }
462
512
  }
463
513
  if (force) {
464
514
  const { x, y } = this.currentMousePosition;
465
- await driver.stealthClick(x, y, button, clickCount, refId);
515
+ await driver.stealthClick(x, y, button, clickCount, refId, undefined, this.modifiers);
466
516
  }
467
517
  else {
468
- await this.mouseClick(button, clickCount, refId);
518
+ try {
519
+ await this.mouseClick(button, clickCount, refId);
520
+ }
521
+ catch (error) {
522
+ const code = error?.code;
523
+ if (code !== 'NATIVE_INPUT_UNAVAILABLE' ||
524
+ !this.isExtensionConnected() ||
525
+ button !== 'left' ||
526
+ clickCount !== 1 ||
527
+ this.modifiers !== 0) {
528
+ throw error;
529
+ }
530
+ const result = await driver.actionableClick(refId, false, undefined, this.modifiers);
531
+ return { ...result, warning: '后台元素单击使用合成事件,isTrusted=false,未激活标签页' };
532
+ }
469
533
  }
470
534
  return { success: true };
471
535
  }
@@ -524,7 +588,7 @@ class UnifiedSessionManager {
524
588
  * @param _retried 内部重试标记,外部不应传入
525
589
  */
526
590
  async evaluate(code, mode, timeout, args, options = {}, _retried = false) {
527
- const entryStart = Date.now();
591
+ this.assertRequestActive();
528
592
  this.lastEvaluationMetadata = undefined;
529
593
  const effectiveMode = mode ?? this.inputMode;
530
594
  const hasArgs = args !== undefined;
@@ -535,53 +599,34 @@ class UnifiedSessionManager {
535
599
  expression = `(${code})(${argsStr})`;
536
600
  }
537
601
  const cdpScript = hasArgs ? code : expression;
538
- try {
539
- if (timeout !== undefined) {
540
- // 轮询上下文:快速失败,端到端预算受控
541
- if (!this.isExtensionConnected()) {
542
- this.assertCdpFallbackAllowed();
543
- return this.evaluateViaCdp(cdpScript, args, timeout);
544
- }
602
+ if (!_retried && !hasArgs && /\breturn\b/.test(code)) {
603
+ try {
604
+ new Script(code);
545
605
  }
546
- else {
547
- // 非轮询上下文:允许等待重连;连接失败时回退 CDP
548
- if (!(await this.ensureExtensionConnected())) {
549
- return this.evaluateViaCdp(cdpScript, args, timeout);
606
+ catch (error) {
607
+ if (error instanceof SyntaxError && /Illegal return statement/.test(error.message)) {
608
+ return this.evaluate(`(() => { ${code} })()`, mode, timeout, undefined, options, true);
550
609
  }
551
610
  }
552
- // Extension 路径
553
- if (effectiveMode === 'precise') {
554
- return this.evaluateViaExtensionPrecise(code, expression, args, timeout, options);
555
- }
556
- return this.evaluateViaExtensionStealth(expression, timeout);
557
611
  }
558
- catch (err) {
559
- const msg = err instanceof Error ? err.message : String(err);
560
- // 计算重试时剩余预算(避免重试用满预算导致总耗时超额)
561
- const remainingTimeout = timeout !== undefined ? Math.max(0, timeout - (Date.now() - entryStart)) : undefined;
562
- // Extension 断连时等待重连后重试一次
563
- if (!_retried && (isExtensionDisconnected(err) || msg.includes('not connected'))) {
564
- if (remainingTimeout !== undefined && remainingTimeout <= 0) {
565
- throw err;
566
- }
567
- await new Promise((r) => setTimeout(r, 2000));
568
- if (this.isExtensionConnected()) {
569
- return this.evaluate(code, mode, remainingTimeout, args, options, true);
570
- }
571
- // Extension 重连失败,尝试 CDP fallback
572
- if (!this.requireExtension) {
573
- return this.evaluateViaCdp(cdpScript, args, remainingTimeout);
574
- }
612
+ if (timeout !== undefined) {
613
+ // 轮询上下文:快速失败,端到端预算受控
614
+ if (!this.isExtensionConnected()) {
615
+ this.assertCdpFallbackAllowed();
616
+ return await this.evaluateViaCdp(cdpScript, args, timeout, options);
575
617
  }
576
- // 裸 return 语句导致语法错误时,自动包裹 IIFE 重试(仅一次)
577
- if (!_retried && !hasArgs) {
578
- if (/Illegal return statement|'return' not inside function|Unexpected token 'return'/.test(msg)) {
579
- const wrapped = `(() => { ${code} })()`;
580
- return this.evaluate(wrapped, mode, timeout, undefined, options, true);
581
- }
618
+ }
619
+ else {
620
+ // 非轮询上下文:允许等待重连;连接失败时回退 CDP
621
+ if (!(await this.ensureExtensionConnected())) {
622
+ return await this.evaluateViaCdp(cdpScript, args, timeout, options);
582
623
  }
583
- throw err;
584
624
  }
625
+ // Extension 路径
626
+ if (effectiveMode === 'precise') {
627
+ return await this.evaluateViaExtensionPrecise(code, expression, args, timeout, options);
628
+ }
629
+ return await this.evaluateViaExtensionStealth(expression, timeout, options.userAction);
585
630
  }
586
631
  /**
587
632
  * 获取页面文本
@@ -677,22 +722,26 @@ class UnifiedSessionManager {
677
722
  * @param text 文本内容
678
723
  * @param xpath XPath 表达式
679
724
  * @param timeout 端到端预算(毫秒),包含连接等待和请求超时,传给 bridge.find → sendCommand
725
+ * @param semanticOrMatchIndex 语义名称条件,或兼容旧调用的匹配序号
726
+ * @param matchIndex 匹配序号,从 0 开始,指定时只返回该位置
680
727
  */
681
- async find(selector, text, xpath, timeout, matchIndex) {
682
- if (matchIndex !== undefined) {
683
- return this.extensionReadBridge().find(selector, text, xpath, timeout, undefined, matchIndex);
684
- }
728
+ async find(selector, text, xpath, timeout, semanticOrMatchIndex, matchIndex) {
729
+ const semantic = typeof semanticOrMatchIndex === 'object' ? semanticOrMatchIndex : undefined;
730
+ matchIndex = typeof semanticOrMatchIndex === 'number' ? semanticOrMatchIndex : matchIndex;
731
+ this.assertRequestActive();
685
732
  if (timeout !== undefined) {
686
733
  // 轮询上下文:快速失败,端到端预算受控
687
734
  if (this.isExtensionConnected()) {
688
- return this.extensionBridge.find(selector, text, xpath, timeout);
735
+ return this.extensionBridge.find(selector, text, xpath, timeout, semantic, matchIndex);
689
736
  }
690
737
  this.assertCdpFallbackAllowed();
691
- return getCdpSession().find(selector, text, xpath, timeout);
738
+ return getCdpSession().find(selector, text, xpath, timeout, semantic, matchIndex);
692
739
  }
693
740
  // 非轮询上下文:允许等待重连,断连时 fallback CDP
694
741
  const driver = await this.getDriver();
695
- return this.withExtensionRetry(() => driver.find(selector, text, xpath), () => getCdpSession().find(selector, text, xpath));
742
+ return this.withExtensionRetry(() => driver === this.extensionBridge
743
+ ? this.extensionBridge.find(selector, text, xpath, undefined, semantic, matchIndex)
744
+ : getCdpSession().find(selector, text, xpath, undefined, semantic, matchIndex), () => getCdpSession().find(selector, text, xpath, undefined, semantic, matchIndex));
696
745
  }
697
746
  /**
698
747
  * 获取元素属性
@@ -718,8 +767,8 @@ class UnifiedSessionManager {
718
767
  const driver = await this.getDriver();
719
768
  await driver.setCookie(params);
720
769
  }
721
- async deleteCookie(url, name) {
722
- return (await this.getDriver()).deleteCookie(url, name);
770
+ async deleteCookie(url, name, scope) {
771
+ return (await this.getDriver()).deleteCookie(url, name, scope);
723
772
  }
724
773
  async clearCookies(filter) {
725
774
  if (!filter || (!filter.url && !filter.domain)) {
@@ -736,6 +785,12 @@ class UnifiedSessionManager {
736
785
  const op = async () => {
737
786
  const result = await driver.newPage(url, timeout, options);
738
787
  this.recordPageUse(result.targetId, true);
788
+ if (result.metadata) {
789
+ this.rememberPageMetadata(result.targetId, result.metadata);
790
+ }
791
+ else if (!isExt && (options?.taskHint !== undefined || options?.note !== undefined)) {
792
+ await this.setPageMetadata(result.targetId, options);
793
+ }
739
794
  return {
740
795
  ...result,
741
796
  targetId: result.targetId,
@@ -802,8 +857,258 @@ class UnifiedSessionManager {
802
857
  *
803
858
  * 同一请求内的嵌套调用可重入,恢复状态只保存在当前请求的 AsyncLocalStorage 中
804
859
  */
805
- async withTabLock(fn, deadline) {
860
+ assertRequestActive(scope = this.tabLockContext.getStore()) {
861
+ if (scope?.budgetError) {
862
+ throw scope.budgetError;
863
+ }
864
+ scope?.signal?.throwIfAborted();
865
+ if (scope?.deadline !== undefined && Date.now() >= scope.deadline) {
866
+ throw setErrorSource(new StructuredToolError('TIMEOUT', 'Request timeout before dispatch', '请检查页面状态后再提交操作', {
867
+ actionExecuted: false,
868
+ actionStatus: 'not_started',
869
+ retryable: false,
870
+ }), 'budget');
871
+ }
872
+ }
873
+ async withUserAction(kind, fn, restoreTargetAfterPreparationFailure = false) {
874
+ return this.withTabLock(async () => {
875
+ const scope = this.tabLockContext.getStore();
876
+ const previous = scope.userAction;
877
+ const action = {
878
+ kind,
879
+ dispatched: false,
880
+ phase: 'preparation',
881
+ };
882
+ scope.userAction = action;
883
+ try {
884
+ return await fn();
885
+ }
886
+ catch (error) {
887
+ if (restoreTargetAfterPreparationFailure && !action.dispatched && action.phase === 'preparation') {
888
+ scope.restoreTargetAfterPreparationFailure = true;
889
+ }
890
+ throw this.annotateUserActionError(error);
891
+ }
892
+ finally {
893
+ if (previous?.kind === kind && action.dispatched) {
894
+ previous.dispatched = true;
895
+ }
896
+ scope.userAction = previous;
897
+ }
898
+ });
899
+ }
900
+ annotateUserActionError(error) {
901
+ const scope = this.tabLockContext.getStore();
902
+ if (scope && error instanceof Error && getErrorSource(error) === 'budget') {
903
+ scope.budgetError ??= error;
904
+ }
905
+ const action = scope?.userAction;
906
+ const uncertain = getErrorSource(error) !== 'page' && isNativeExecutionResultUnknown(error);
907
+ const outputFailure = getErrorSource(error) !== 'page' &&
908
+ error instanceof StructuredToolError &&
909
+ error.code === 'NON_SERIALIZABLE_EVALUATE_RESULT' &&
910
+ getErrorPageContext(error)?.contextPhase === 'preparation';
911
+ const preparing = action &&
912
+ !action.dispatched &&
913
+ (uncertain ||
914
+ outputFailure ||
915
+ getErrorPageContext(error)?.keyboardFocusPreparation === true ||
916
+ getErrorPageContext(error)?.frameResolutionPreparation === true ||
917
+ action.phase === 'precondition' ||
918
+ error instanceof PagePreconditionError);
919
+ return setErrorPageContext(error, {
920
+ contextPhase: preparing
921
+ ? error instanceof PagePreconditionError
922
+ ? 'precondition'
923
+ : action.phase
924
+ : 'action',
925
+ });
926
+ }
927
+ async prepareKeyboardFocus(timeout) {
928
+ if (this.getMode() !== 'extension' || this.inputMode !== 'precise') {
929
+ return undefined;
930
+ }
931
+ const scope = this.tabLockContext.getStore();
932
+ if (scope.keyboardFocus) {
933
+ return undefined;
934
+ }
935
+ const bridge = this.extensionBridge;
936
+ const targetId = bridge.getCurrentTargetId();
937
+ if (!targetId) {
938
+ throw new TargetNotFoundError('没有当前原生按键目标');
939
+ }
940
+ const focus = { id: randomUUID(), targetId };
941
+ scope.keyboardFocus = focus;
942
+ try {
943
+ this.assertRequestActive();
944
+ const result = await bridge.acquireKeyboardFocus(focus.id, timeout);
945
+ if (result.changed) {
946
+ focus.warning = '原生按键期间文档临时报告 visible;操作结束后恢复原设置,实际标签页与窗口焦点保持';
947
+ }
948
+ this.assertRequestActive();
949
+ return focus.warning;
950
+ }
951
+ catch (error) {
952
+ throw setErrorPageContext(error, { keyboardFocusPreparation: true });
953
+ }
954
+ }
955
+ async withInputCleanup(fn) {
956
+ const scope = this.tabLockContext.getStore();
957
+ const keysBefore = new Set(this.pressedKeys);
958
+ const driver = this.getMode() === 'extension' ? this.extensionBridge : getCdpSession();
959
+ const targetId = driver?.getCurrentTargetId();
960
+ const frameId = driver?.getCurrentFrameId?.();
961
+ const documentId = this.extensionBridge === driver ? this.extensionBridge?.getCurrentFrameDocumentId?.() : undefined;
962
+ const keys = new Set();
963
+ scope.inputKeys = keys;
964
+ const cleanup = async (releaseUnknown = false) => {
965
+ if (!releaseUnknown &&
966
+ !scope.signal?.aborted &&
967
+ !scope.budgetError &&
968
+ (scope.deadline === undefined || Date.now() < scope.deadline)) {
969
+ return;
970
+ }
971
+ scope.inputCleanup = undefined;
972
+ if (![...keys].some((key) => !keysBefore.has(key))) {
973
+ return;
974
+ }
975
+ const cleanupDeadline = Date.now() + 1000;
976
+ try {
977
+ await this.tabLockContext.run({
978
+ ...scope,
979
+ signal: undefined,
980
+ deadline: cleanupDeadline,
981
+ inputCleanup: undefined,
982
+ userAction: undefined,
983
+ budgetError: undefined,
984
+ pendingPageCondition: null,
985
+ }, async () => {
986
+ const bridge = driver === this.extensionBridge ? this.extensionBridge : null;
987
+ const previousTarget = driver?.getCurrentTargetId();
988
+ const previousFrame = driver?.getCurrentFrameId?.();
989
+ const previousDocument = bridge?.getCurrentFrameDocumentId?.();
990
+ const inputContext = !bridge && targetId && (targetId !== previousTarget || frameId !== previousFrame)
991
+ ? { targetId, frameId }
992
+ : undefined;
993
+ try {
994
+ if (bridge && targetId) {
995
+ bridge.setCurrentTargetId(targetId);
996
+ if (frameId !== undefined) {
997
+ bridge.setCurrentFrameId(frameId, documentId);
998
+ }
999
+ }
1000
+ for (const key of keys) {
1001
+ if (keysBefore.has(key)) {
1002
+ continue;
1003
+ }
1004
+ try {
1005
+ await this.keyUp(key, remainingNavigationTimeout(cleanupDeadline), inputContext);
1006
+ }
1007
+ catch (error) {
1008
+ console.warn('[MCP] Cancelled input key release failed:', key, error);
1009
+ }
1010
+ }
1011
+ }
1012
+ finally {
1013
+ if (bridge && previousTarget !== undefined) {
1014
+ bridge.setCurrentTargetId(previousTarget);
1015
+ if (previousFrame !== undefined) {
1016
+ bridge.setCurrentFrameId(previousFrame, previousDocument);
1017
+ }
1018
+ }
1019
+ }
1020
+ });
1021
+ }
1022
+ catch (error) {
1023
+ console.warn('[MCP] Cancelled input scope cleanup failed:', error);
1024
+ }
1025
+ };
1026
+ scope.inputCleanup = cleanup;
1027
+ let failed = false;
1028
+ let primaryError;
1029
+ let cleanupError;
1030
+ let value;
1031
+ try {
1032
+ value = await fn();
1033
+ }
1034
+ catch (error) {
1035
+ failed = true;
1036
+ primaryError = error;
1037
+ if (scope.keyboardFocus?.warning) {
1038
+ setErrorPageContext(error, { keyboardFocusWarning: scope.keyboardFocus.warning });
1039
+ }
1040
+ throw error;
1041
+ }
1042
+ finally {
1043
+ await cleanup(failed && isNativeExecutionResultUnknown(primaryError));
1044
+ const focus = scope.keyboardFocus;
1045
+ if (focus && driver === this.extensionBridge) {
1046
+ scope.inputCleanup = undefined;
1047
+ const releaseKeys = failed ||
1048
+ Boolean(scope.signal?.aborted || scope.budgetError) ||
1049
+ (scope.deadline !== undefined && Date.now() >= scope.deadline);
1050
+ let result;
1051
+ try {
1052
+ result = await this.tabLockContext.run({
1053
+ ...scope,
1054
+ signal: undefined,
1055
+ deadline: Date.now() + 4000,
1056
+ budgetError: undefined,
1057
+ userAction: undefined,
1058
+ pendingPageCondition: null,
1059
+ keyboardFocus: undefined,
1060
+ inputCleanup: undefined,
1061
+ }, () => this.extensionBridge.releaseKeyboardFocus(focus, releaseKeys));
1062
+ if (releaseKeys && result.restored) {
1063
+ for (const key of keys) {
1064
+ if (!keysBefore.has(key)) {
1065
+ this.pressedKeys.delete(key);
1066
+ this.modifiers &= ~(MODIFIER_KEYS[key] ?? 0);
1067
+ }
1068
+ }
1069
+ }
1070
+ }
1071
+ catch (error) {
1072
+ result = { restored: false, errors: [error instanceof Error ? error.message : String(error)] };
1073
+ }
1074
+ finally {
1075
+ scope.keyboardFocus = undefined;
1076
+ }
1077
+ if (!result.restored) {
1078
+ if (failed) {
1079
+ setErrorPageContext(primaryError, { keyboardFocusCleanup: result });
1080
+ }
1081
+ else {
1082
+ cleanupError = new StructuredToolError('KEYBOARD_FOCUS_CLEANUP_FAILED', '原生按键已执行,焦点状态恢复未完成', '请读取当前页面与 Extension 日志', {
1083
+ failureStage: 'cleanup',
1084
+ retryable: false,
1085
+ keyboardFocusCleanup: result,
1086
+ });
1087
+ }
1088
+ }
1089
+ }
1090
+ }
1091
+ if (cleanupError) {
1092
+ throw cleanupError;
1093
+ }
1094
+ return value;
1095
+ }
1096
+ async wait(ms) {
1097
+ this.assertRequestActive();
1098
+ await delay(ms, undefined, { signal: this.tabLockContext.getStore()?.signal });
1099
+ this.assertRequestActive();
1100
+ }
1101
+ async withMetadataContext(fn, deadline, signal) {
1102
+ const mode = this.getMode();
1103
+ return this.tabLockContext.run({ session: this, deadline, signal, mode: mode === 'none' ? undefined : mode }, async () => {
1104
+ this.assertRequestActive();
1105
+ return fn();
1106
+ });
1107
+ }
1108
+ async withTabLock(fn, deadline, signal) {
1109
+ signal?.throwIfAborted();
806
1110
  if (this.tabLockContext.getStore()?.session === this) {
1111
+ this.assertRequestActive();
807
1112
  if (deadline !== undefined) {
808
1113
  remainingNavigationTimeout(deadline);
809
1114
  }
@@ -816,9 +1121,28 @@ class UnifiedSessionManager {
816
1121
  });
817
1122
  let acquired = false;
818
1123
  try {
819
- await waitForNavigationQueue(previousLock, deadline);
1124
+ await waitForNavigationQueue(previousLock, deadline, signal);
820
1125
  acquired = true;
821
- return await this.tabLockContext.run({ session: this }, fn);
1126
+ signal?.throwIfAborted();
1127
+ return await this.tabLockContext.run({ session: this, deadline, signal }, async () => {
1128
+ const scope = this.tabLockContext.getStore();
1129
+ try {
1130
+ return await fn();
1131
+ }
1132
+ finally {
1133
+ try {
1134
+ if (scope.signal?.aborted ||
1135
+ scope.budgetError ||
1136
+ (scope.deadline !== undefined && Date.now() >= scope.deadline)) {
1137
+ await scope.inputCleanup?.();
1138
+ }
1139
+ }
1140
+ finally {
1141
+ scope.inputKeys = undefined;
1142
+ scope.inputCleanup = undefined;
1143
+ }
1144
+ }
1145
+ });
822
1146
  }
823
1147
  finally {
824
1148
  if (acquired) {
@@ -904,49 +1228,79 @@ class UnifiedSessionManager {
904
1228
  temporaryTargetId: targetId,
905
1229
  currentTargetId: null,
906
1230
  };
907
- let clearTarget = true;
908
- try {
909
- if (previousId) {
910
- const targets = await driver.listTargets(remaining());
911
- if (targets.some((target) => (target.targetId ?? String(target.id)) === previousId)) {
912
- await driver.selectPage(previousId, remaining());
913
- clearTarget = false;
914
- }
915
- else {
916
- restoration.status = 'previous_target_closed';
917
- }
1231
+ const independentCleanup = (scope.restoreTargetAfterPreparationFailure === true ||
1232
+ scope.restoreTargetAfterLookup === true) &&
1233
+ (scope.signal?.aborted === true ||
1234
+ scope.budgetError !== undefined ||
1235
+ (scope.deadline !== undefined && Date.now() >= scope.deadline));
1236
+ scope.restoreTargetAfterPreparationFailure = undefined;
1237
+ scope.restoreTargetAfterLookup = undefined;
1238
+ const cleanupDeadline = independentCleanup ? Date.now() + 1000 : deadline;
1239
+ const cleanupRemaining = () => cleanupDeadline === undefined ? undefined : remainingNavigationTimeout(cleanupDeadline);
1240
+ await this.tabLockContext.run(independentCleanup
1241
+ ? {
1242
+ ...scope,
1243
+ signal: undefined,
1244
+ deadline: cleanupDeadline,
1245
+ budgetError: undefined,
1246
+ userAction: undefined,
1247
+ pendingPageCondition: null,
1248
+ keyboardFocus: undefined,
1249
+ inputCleanup: undefined,
918
1250
  }
919
- }
920
- catch (error) {
921
- restoration.status = 'failed';
922
- restoration.error = error instanceof Error ? error.message : String(error);
923
- }
924
- if (clearTarget) {
1251
+ : scope, async () => {
1252
+ let clearTarget = true;
925
1253
  try {
926
- if (mode === 'extension') {
927
- driver.setCurrentTargetId(null);
928
- }
929
- else {
930
- await getCdpSession().deselectPage(deadline === undefined ? undefined : Math.max(0, deadline - Date.now()));
1254
+ if (previousId) {
1255
+ const targets = await driver.listTargets(cleanupRemaining());
1256
+ if (targets.some((target) => (target.targetId ?? String(target.id)) === previousId)) {
1257
+ await driver.selectPage(previousId, cleanupRemaining());
1258
+ clearTarget = false;
1259
+ }
1260
+ else {
1261
+ restoration.status = 'previous_target_closed';
1262
+ }
931
1263
  }
932
1264
  }
933
1265
  catch (error) {
934
- const message = error instanceof Error ? error.message : String(error);
935
- if (restoration.error) {
936
- restoration.cleanupError = message;
1266
+ restoration.status = 'failed';
1267
+ restoration.error = error instanceof Error ? error.message : String(error);
1268
+ }
1269
+ if (clearTarget) {
1270
+ try {
1271
+ if (mode === 'extension') {
1272
+ driver.setCurrentTargetId(null);
1273
+ }
1274
+ else {
1275
+ await getCdpSession().deselectPage(cleanupDeadline === undefined
1276
+ ? undefined
1277
+ : Math.max(0, cleanupDeadline - Date.now()));
1278
+ }
937
1279
  }
938
- else {
939
- restoration.status = 'failed';
940
- restoration.error = message;
1280
+ catch (error) {
1281
+ const message = error instanceof Error ? error.message : String(error);
1282
+ if (restoration.error) {
1283
+ restoration.cleanupError = message;
1284
+ }
1285
+ else {
1286
+ restoration.status = 'failed';
1287
+ restoration.error = message;
1288
+ }
941
1289
  }
942
1290
  }
943
- }
1291
+ });
944
1292
  restoration.currentTargetId = driver.getCurrentTargetId();
945
- this.tabLockContext.getStore().targetRestoration = restoration;
1293
+ scope.targetRestoration = restoration;
946
1294
  }
947
1295
  }
948
1296
  }, deadline);
949
1297
  }
1298
+ preserveTargetAfterLookup() {
1299
+ const scope = this.tabLockContext.getStore();
1300
+ if (scope) {
1301
+ scope.restoreTargetAfterLookup = true;
1302
+ }
1303
+ }
950
1304
  getTargetRestoration() {
951
1305
  return this.tabLockContext.getStore()?.targetRestoration;
952
1306
  }
@@ -963,57 +1317,81 @@ class UnifiedSessionManager {
963
1317
  ? await this.extensionBridge.getPageSnapshot(id, timeout)
964
1318
  : await getCdpSession().getPageSnapshot(id, timeout);
965
1319
  this.recordPageUse(id);
1320
+ const nativeMetadata = snapshot.metadata;
1321
+ if (nativeMetadata) {
1322
+ this.rememberPageMetadata(id, nativeMetadata);
1323
+ }
966
1324
  return snapshot;
967
1325
  }
968
1326
  async withPageCondition(condition, fn, deadline) {
969
- if (condition.expectedUrl === undefined && condition.expectedRevision === undefined) {
970
- return fn();
971
- }
972
- return this.withTabLock(async () => {
973
- const mode = this.getMode();
974
- const targetId = condition.targetId ?? this.getCurrentTargetId();
975
- if (mode === 'none' || !targetId) {
976
- throw new DriverCapabilityError('没有操作目标,请先选择页面');
977
- }
978
- const driver = mode === 'extension' ? this.extensionBridge : getCdpSession();
979
- const previousRequireExtension = this.requireExtension;
980
- const previousCdpSelection = this.cdpExplicitlySelected;
981
- const previousMode = this.activeMode;
982
- if (mode === 'extension') {
983
- this.requireExtension = true;
984
- }
985
- else {
986
- this.cdpExplicitlySelected = true;
1327
+ let contextPhase = 'action';
1328
+ try {
1329
+ if (condition.expectedUrl === undefined && condition.expectedRevision === undefined) {
1330
+ return await fn();
987
1331
  }
988
- try {
989
- const snapshot = await driver.getPageSnapshot(targetId, deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
990
- if (snapshot.revision === null) {
991
- throw new StructuredToolError('DRIVER_CAPABILITY_ERROR', '当前 Extension 不支持原子页面条件,未执行操作', '更新 Extension 后再使用 expectedUrl 或 expectedRevision', { targetId, actionExecuted: false, actionStatus: 'not_started', failureStage: 'precondition' });
1332
+ return await this.withTabLock(async () => {
1333
+ contextPhase = 'precondition';
1334
+ const userAction = this.tabLockContext.getStore()?.userAction;
1335
+ if (userAction) {
1336
+ userAction.phase = 'precondition';
1337
+ }
1338
+ const mode = this.getMode();
1339
+ const targetId = condition.targetId ?? this.getCurrentTargetId();
1340
+ if (mode === 'none' || !targetId) {
1341
+ throw new DriverCapabilityError('没有操作目标,请先选择页面');
1342
+ }
1343
+ const driver = mode === 'extension' ? this.extensionBridge : getCdpSession();
1344
+ const previousRequireExtension = this.requireExtension;
1345
+ const previousCdpSelection = this.cdpExplicitlySelected;
1346
+ const previousMode = this.activeMode;
1347
+ if (mode === 'extension') {
1348
+ this.requireExtension = true;
1349
+ }
1350
+ else {
1351
+ this.cdpExplicitlySelected = true;
992
1352
  }
993
- if ((condition.expectedUrl !== undefined && snapshot.url !== condition.expectedUrl) ||
994
- (condition.expectedRevision !== undefined && snapshot.revision !== condition.expectedRevision)) {
995
- throw new PagePreconditionError({
1353
+ try {
1354
+ const snapshot = await driver.getPageSnapshot(targetId, deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
1355
+ if (snapshot.revision === null) {
1356
+ throw new StructuredToolError('DRIVER_CAPABILITY_ERROR', '当前 Extension 不支持原子页面条件,未执行操作', '更新 Extension 后再使用 expectedUrl 或 expectedRevision', {
1357
+ targetId,
1358
+ actionExecuted: false,
1359
+ actionStatus: 'not_started',
1360
+ failureStage: 'precondition',
1361
+ });
1362
+ }
1363
+ if ((condition.expectedUrl !== undefined && snapshot.url !== condition.expectedUrl) ||
1364
+ (condition.expectedRevision !== undefined && snapshot.revision !== condition.expectedRevision)) {
1365
+ throw new PagePreconditionError({
1366
+ targetId,
1367
+ expectedUrl: condition.expectedUrl ?? null,
1368
+ expectedRevision: condition.expectedRevision ?? null,
1369
+ actualUrl: snapshot.url,
1370
+ actualRevision: snapshot.revision,
1371
+ });
1372
+ }
1373
+ driver.setPageCondition({
996
1374
  targetId,
997
- expectedUrl: condition.expectedUrl ?? null,
998
- expectedRevision: condition.expectedRevision ?? null,
999
- actualUrl: snapshot.url,
1000
- actualRevision: snapshot.revision,
1375
+ expectedUrl: condition.expectedUrl,
1376
+ expectedRevision: condition.expectedRevision,
1001
1377
  });
1378
+ contextPhase = 'action';
1379
+ if (userAction) {
1380
+ userAction.phase = 'preparation';
1381
+ }
1382
+ return await fn();
1002
1383
  }
1003
- driver.setPageCondition({
1004
- targetId,
1005
- expectedUrl: condition.expectedUrl,
1006
- expectedRevision: condition.expectedRevision,
1007
- });
1008
- return await fn();
1009
- }
1010
- finally {
1011
- driver.setPageCondition(null);
1012
- this.requireExtension = previousRequireExtension;
1013
- this.cdpExplicitlySelected = previousCdpSelection;
1014
- this.activeMode = previousMode;
1015
- }
1016
- }, deadline);
1384
+ finally {
1385
+ driver.setPageCondition(null);
1386
+ this.requireExtension = previousRequireExtension;
1387
+ this.cdpExplicitlySelected = previousCdpSelection;
1388
+ this.activeMode = previousMode;
1389
+ }
1390
+ }, deadline);
1391
+ }
1392
+ catch (error) {
1393
+ throw setErrorPageContext(error, { contextPhase });
1394
+ }
1017
1395
  }
1018
1396
  /**
1019
1397
  * 临时切换操作目标 iframe,执行完后恢复
@@ -1028,35 +1406,88 @@ class UnifiedSessionManager {
1028
1406
  if (frame === undefined) {
1029
1407
  return fn();
1030
1408
  }
1031
- if (this.getMode() !== 'extension' || !this.extensionBridge?.isConnected()) {
1032
- throw new DriverCapabilityError('iframe 穿透需要 Extension 模式,当前连接未执行 frame 解析或操作');
1033
- }
1034
- const driver = this.extensionBridge;
1035
- const { frameId, offset, frameContext } = await driver.resolveFrame(frame, deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
1409
+ const scope = this.tabLockContext.getStore();
1410
+ deadline = deadline === undefined ? scope?.deadline : Math.min(deadline, scope?.deadline ?? deadline);
1411
+ const driver = await this.getDriver(deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
1412
+ const { frameId, offset, frameContext } = await driver
1413
+ .resolveFrame(frame, deadline === undefined ? undefined : remainingNavigationTimeout(deadline))
1414
+ .catch((error) => {
1415
+ if (!scope?.userAction?.dispatched) {
1416
+ if (scope) {
1417
+ scope.restoreTargetAfterPreparationFailure = true;
1418
+ }
1419
+ throw setErrorPageContext(error, { contextPhase: 'preparation', frameResolutionPreparation: true });
1420
+ }
1421
+ throw error;
1422
+ });
1036
1423
  const previousFrameId = driver.getCurrentFrameId();
1037
- const previousFrameOffset = this.currentFrameOffset;
1038
- const previousRequireExtension = this.requireExtension;
1039
- const previousActiveMode = this.activeMode;
1040
- driver.setCurrentFrameId(frameId);
1424
+ const extension = driver === this.extensionBridge ? this.extensionBridge : undefined;
1425
+ const previousDocument = extension?.getCurrentFrameDocumentId?.();
1426
+ const previousOffset = this.currentFrameOffset;
1427
+ if (extension) {
1428
+ extension.setCurrentFrameId(frameId, typeof frameContext?.documentId === 'string' ? frameContext.documentId : undefined);
1429
+ }
1430
+ else {
1431
+ driver.setCurrentFrameId(frameId);
1432
+ }
1041
1433
  this.currentFrameOffset = offset;
1042
- this.requireExtension = true;
1043
- this.activeMode = 'extension';
1044
1434
  try {
1045
1435
  return await fn();
1046
1436
  }
1047
1437
  catch (error) {
1048
- throw setErrorPageContext(error, {
1049
- frameContext: { selection: frame, frameId, ...frameContext },
1050
- });
1438
+ throw setErrorPageContext(error, { frameContext: { selection: frame, frameId, ...frameContext } });
1051
1439
  }
1052
1440
  finally {
1053
- // 字段赋值不会 throw,driver.setCurrentFrameId 是同步函数也不会 throw,无需嵌套 try
1054
- this.requireExtension = previousRequireExtension;
1055
- this.activeMode = previousActiveMode;
1056
- this.currentFrameOffset = previousFrameOffset;
1057
- driver.setCurrentFrameId(previousFrameId);
1441
+ this.currentFrameOffset = previousOffset;
1442
+ if (extension) {
1443
+ extension.setCurrentFrameId(previousFrameId, previousDocument);
1444
+ }
1445
+ else {
1446
+ driver.setCurrentFrameId(previousFrameId);
1447
+ }
1058
1448
  }
1059
1449
  }
1450
+ hasBrowserEventObservation(targetId) {
1451
+ return this.getMode() === 'cdp' || this.extensionBridge?.hasBrowserEventObservation(targetId) === true;
1452
+ }
1453
+ async browserEvents(action, command, targetId, timeout = DEFAULT_TIMEOUT, signal, deadline = Date.now() + timeout) {
1454
+ const refTarget = command.ref ? eventRefTarget(command.ref) : undefined;
1455
+ const mode = refTarget?.backend ?? this.getMode();
1456
+ if (targetId && refTarget && targetId !== refTarget.targetId) {
1457
+ throw new DriverCapabilityError('Browser event ref target mismatch');
1458
+ }
1459
+ const driver = mode === 'extension' ? this.extensionBridge : mode === 'cdp' ? getCdpSession() : null;
1460
+ if (!driver) {
1461
+ throw new DriverCapabilityError('Browser event backend is not connected');
1462
+ }
1463
+ const id = refTarget?.targetId ?? targetId ?? driver.getCurrentTargetId() ?? undefined;
1464
+ const scope = this.tabLockContext.getStore();
1465
+ return this.tabLockContext.run({
1466
+ session: this,
1467
+ signal: signal ?? scope?.signal,
1468
+ deadline: Math.min(scope?.deadline ?? Infinity, deadline),
1469
+ }, () => driver.browserEvents(action, command, id, timeout));
1470
+ }
1471
+ async browserFetchCommand(method, params, targetId, signal) {
1472
+ const mode = this.getMode();
1473
+ const driver = mode === 'extension' ? this.extensionBridge : mode === 'cdp' ? getCdpSession() : null;
1474
+ if (!driver?.browserFetchCommand) {
1475
+ throw new DriverCapabilityError('Fetch control backend unavailable');
1476
+ }
1477
+ const scope = this.tabLockContext.getStore();
1478
+ return this.tabLockContext.run({
1479
+ ...scope,
1480
+ session: this,
1481
+ signal: signal ?? scope?.signal,
1482
+ deadline: Math.min(scope?.deadline ?? Infinity, Date.now() + DEFAULT_TIMEOUT),
1483
+ }, () => driver.browserFetchCommand(method, params, targetId));
1484
+ }
1485
+ async suspendBrowserEventsForChallenge() {
1486
+ const driver = await this.getDriver();
1487
+ return driver.suspendBrowserEventsForChallenge
1488
+ ? driver.suspendBrowserEventsForChallenge()
1489
+ : { suspended: false, reason: 'backend capability unavailable' };
1490
+ }
1060
1491
  /**
1061
1492
  * 获取当前状态
1062
1493
  */
@@ -1067,15 +1498,15 @@ class UnifiedSessionManager {
1067
1498
  }
1068
1499
  return getCdpSession().getState();
1069
1500
  }
1070
- async getLiveState() {
1501
+ async getLiveState(timeout) {
1071
1502
  if (this.getMode() !== 'extension') {
1072
- return getCdpSession().getLiveState();
1503
+ return getCdpSession().getLiveState(timeout);
1073
1504
  }
1074
1505
  const targetId = this.extensionBridge.getCurrentTargetId();
1075
1506
  if (!targetId) {
1076
1507
  return null;
1077
1508
  }
1078
- const targets = await this.extensionBridge.listTargets();
1509
+ const targets = await this.extensionBridge.listTargets(timeout);
1079
1510
  const target = targets.find((item) => item.targetId === targetId);
1080
1511
  if (!target) {
1081
1512
  return null;
@@ -1109,6 +1540,7 @@ class UnifiedSessionManager {
1109
1540
  throw new Error('commands 参数不支持 stealth 输入模式,请先调用 manage action=inputMode inputMode=precise 切换后重试');
1110
1541
  }
1111
1542
  // Puppeteer 风格:连续 keyDown 同 key → rawKeyDown + autoRepeat(长按重复)
1543
+ this.tabLockContext.getStore()?.inputKeys?.add(key);
1112
1544
  const isRepeat = this.pressedKeys.has(key);
1113
1545
  const nextModifiers = MODIFIER_KEYS[key] ? this.modifiers | MODIFIER_KEYS[key] : this.modifiers;
1114
1546
  if (!isExt) {
@@ -1139,14 +1571,19 @@ class UnifiedSessionManager {
1139
1571
  /**
1140
1572
  * 释放键盘按键
1141
1573
  */
1142
- async keyUp(key, timeout) {
1574
+ async keyUp(key, timeout, inputContext) {
1143
1575
  const deadline = timeout === undefined ? undefined : Date.now() + timeout;
1144
1576
  const driver = await this.getDriver(timeout);
1145
1577
  const remaining = () => (deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
1146
1578
  const isExt = this.isExtensionConnected();
1147
1579
  const nextModifiers = MODIFIER_KEYS[key] ? this.modifiers & ~MODIFIER_KEYS[key] : this.modifiers;
1148
1580
  if (!isExt) {
1149
- await getCdpSession().keyUp(key, remaining());
1581
+ if (inputContext) {
1582
+ await getCdpSession().keyUp(key, remaining(), { ...inputContext, modifiers: nextModifiers });
1583
+ }
1584
+ else {
1585
+ await getCdpSession().keyUp(key, remaining());
1586
+ }
1150
1587
  }
1151
1588
  else if (this.inputMode === 'stealth') {
1152
1589
  await driver.stealthKey(key, 'up', this.getModifierNames(nextModifiers), remaining());
@@ -1162,6 +1599,7 @@ class UnifiedSessionManager {
1162
1599
  }
1163
1600
  this.modifiers = nextModifiers;
1164
1601
  this.pressedKeys.delete(key);
1602
+ this.tabLockContext.getStore()?.inputKeys?.delete(key);
1165
1603
  }
1166
1604
  /**
1167
1605
  * 输入文本
@@ -1188,50 +1626,59 @@ class UnifiedSessionManager {
1188
1626
  if (remaining !== undefined && remaining <= 0) {
1189
1627
  throw new TimeoutError('input timeout before mouse movement');
1190
1628
  }
1191
- this.currentMousePosition = { x, y }; // 更新位置
1192
1629
  const isExt = this.isExtensionConnected();
1630
+ if (!isExt) {
1631
+ ;
1632
+ ({ x, y } = await getCdpSession().mapFramePoint({ x, y }, remaining));
1633
+ }
1193
1634
  if (isExt && this.inputMode === 'stealth') {
1194
- await driver.stealthMouse('mousemove', x, y, undefined, remaining);
1635
+ await driver.stealthMouse('mousemove', x, y, undefined, remaining, this.modifiers);
1195
1636
  }
1196
1637
  else {
1197
1638
  await driver.inputMouse('mouseMoved', x, y, { modifiers: this.modifiers }, remaining);
1198
1639
  }
1640
+ this.currentMousePosition = { x, y };
1641
+ this.rememberMouseFrame();
1199
1642
  }
1200
1643
  /**
1201
1644
  * 鼠标按下
1202
1645
  */
1203
1646
  async mouseDown(button = 'left', clickCount = 1) {
1204
- const { x, y } = this.currentMousePosition;
1205
- const driver = await this.getDriver();
1206
- const isExt = this.isExtensionConnected();
1207
- if (isExt && this.inputMode === 'stealth') {
1208
- await driver.stealthMouse('mousedown', x, y, button);
1209
- }
1210
- else {
1211
- await driver.inputMouse('mousePressed', x, y, {
1212
- button,
1213
- clickCount,
1214
- modifiers: this.modifiers,
1215
- });
1216
- }
1647
+ return this.withMouseFrame(async () => {
1648
+ const { x, y } = this.currentMousePosition;
1649
+ const driver = await this.getDriver();
1650
+ const isExt = this.isExtensionConnected();
1651
+ if (isExt && this.inputMode === 'stealth') {
1652
+ await driver.stealthMouse('mousedown', x, y, button, undefined, this.modifiers);
1653
+ }
1654
+ else {
1655
+ await driver.inputMouse('mousePressed', x, y, {
1656
+ button,
1657
+ clickCount,
1658
+ modifiers: this.modifiers,
1659
+ });
1660
+ }
1661
+ });
1217
1662
  }
1218
1663
  /**
1219
1664
  * 鼠标释放
1220
1665
  */
1221
1666
  async mouseUp(button = 'left', clickCount = 1) {
1222
- const { x, y } = this.currentMousePosition;
1223
- const driver = await this.getDriver();
1224
- const isExt = this.isExtensionConnected();
1225
- if (isExt && this.inputMode === 'stealth') {
1226
- await driver.stealthMouse('mouseup', x, y, button);
1227
- }
1228
- else {
1229
- await driver.inputMouse('mouseReleased', x, y, {
1230
- button,
1231
- clickCount,
1232
- modifiers: this.modifiers,
1233
- });
1234
- }
1667
+ return this.withMouseFrame(async () => {
1668
+ const { x, y } = this.currentMousePosition;
1669
+ const driver = await this.getDriver();
1670
+ const isExt = this.isExtensionConnected();
1671
+ if (isExt && this.inputMode === 'stealth') {
1672
+ await driver.stealthMouse('mouseup', x, y, button, undefined, this.modifiers);
1673
+ }
1674
+ else {
1675
+ await driver.inputMouse('mouseReleased', x, y, {
1676
+ button,
1677
+ clickCount,
1678
+ modifiers: this.modifiers,
1679
+ });
1680
+ }
1681
+ });
1235
1682
  }
1236
1683
  /**
1237
1684
  * 鼠标点击(mousedown + mouseup + click 三合一)
@@ -1240,50 +1687,71 @@ class UnifiedSessionManager {
1240
1687
  * precise / CDP 模式:mouseDown + mouseUp,浏览器自动合成原生 click 事件
1241
1688
  */
1242
1689
  async mouseClick(button = 'left', clickCount = 1, refId) {
1243
- const driver = await this.getDriver();
1244
- const isExt = this.isExtensionConnected();
1245
- if (isExt && (this.inputMode === 'stealth' || button === 'right')) {
1246
- const { x, y } = this.currentMousePosition;
1247
- // stealth 通过单次脚本注入完成所有 mouse/click/dblclick/contextmenu 事件
1248
- // refId 透传:嵌套 iframe overlay 场景下绕过 elementFromPoint 命中外层 IFRAME 的问题
1249
- await driver.stealthClick(x, y, button, clickCount, refId);
1250
- return;
1251
- }
1252
- // CDP 每次 mousePressed/mouseReleased 递增 clickCount,让浏览器合成 dblclick/tripleclick
1253
- for (let i = 1; i <= clickCount; i++) {
1254
- await this.mouseDown(button, i);
1255
- await this.mouseUp(button, i);
1256
- }
1690
+ return this.withMouseFrame(async () => {
1691
+ const driver = await this.getDriver();
1692
+ const isExt = this.isExtensionConnected();
1693
+ if (isExt && (this.inputMode === 'stealth' || button === 'right')) {
1694
+ const { x, y } = this.currentMousePosition;
1695
+ // stealth 通过单次脚本注入完成所有 mouse/click/dblclick/contextmenu 事件
1696
+ // refId 透传:嵌套 iframe overlay 场景下绕过 elementFromPoint 命中外层 IFRAME 的问题
1697
+ await driver.stealthClick(x, y, button, clickCount, refId, undefined, this.modifiers);
1698
+ return;
1699
+ }
1700
+ // CDP 每次 mousePressed/mouseReleased 递增 clickCount,让浏览器合成 dblclick/tripleclick
1701
+ for (let i = 1; i <= clickCount; i++) {
1702
+ let pressRejected = false;
1703
+ try {
1704
+ await this.mouseDown(button, i);
1705
+ }
1706
+ catch (error) {
1707
+ pressRejected = error instanceof StructuredToolError && error.context.actionExecuted === false;
1708
+ throw error;
1709
+ }
1710
+ finally {
1711
+ if (!pressRejected) {
1712
+ const { x, y } = this.currentMousePosition;
1713
+ await driver.inputMouse('mouseReleased', x, y, {
1714
+ button,
1715
+ clickCount: i,
1716
+ modifiers: this.modifiers,
1717
+ });
1718
+ }
1719
+ }
1720
+ }
1721
+ });
1257
1722
  }
1258
1723
  /**
1259
1724
  * Turnstile 点击:同页控件走页面鼠标;Cloudflare iframe 只在已聚焦窗口的活动受控 tab 上使用系统鼠标
1260
1725
  */
1261
1726
  // noinspection JSUnusedGlobalSymbols — resolveChallenge 通过 ChallengeHost 结构类型调用
1262
- async clickTurnstile(snapshot, timeout) {
1263
- const deadline = timeout === undefined ? undefined : Date.now() + timeout;
1264
- const remaining = () => (deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
1727
+ async clickTurnstile(snapshot, timeout, signal, refreshSnapshot) {
1728
+ const scope = this.tabLockContext.getStore();
1729
+ signal ??= scope?.signal;
1730
+ signal?.throwIfAborted();
1731
+ const deadline = timeout === undefined ? scope?.deadline : Math.min(Date.now() + timeout, scope?.deadline ?? Infinity);
1732
+ const remaining = () => {
1733
+ signal?.throwIfAborted();
1734
+ return deadline === undefined ? undefined : remainingNavigationTimeout(deadline);
1735
+ };
1265
1736
  if (snapshot.clickSource !== 'iframe') {
1266
1737
  const point = snapshot.clickPoint;
1267
1738
  if (!point) {
1268
1739
  throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '页面未返回可点击的 Turnstile 坐标', '重新检查页面状态后再重试;交互式验证码无法自动完成');
1269
1740
  }
1270
- const driver = await withNavigationBudget(() => this.getDriver(remaining()), deadline);
1741
+ const driver = await withNavigationBudget(() => this.getDriver(remaining()), deadline, signal);
1271
1742
  if (this.isExtensionConnected()) {
1272
- await withNavigationBudget(() => driver.stealthClick(point.x, point.y, 'left', 1, undefined, remaining()), deadline);
1743
+ await withNavigationBudget(() => driver.stealthClick(point.x, point.y, 'left', 1, undefined, remaining()), deadline, signal);
1273
1744
  return;
1274
1745
  }
1275
- const frameOffset = this.currentFrameOffset;
1276
- const clickPoint = frameOffset && this.inputMode !== 'stealth'
1277
- ? { x: point.x + frameOffset.x, y: point.y + frameOffset.y }
1278
- : point;
1746
+ const clickPoint = await withNavigationBudget(() => getCdpSession().mapFramePoint(point, remaining()), deadline, signal);
1279
1747
  this.currentMousePosition = clickPoint;
1280
- await withNavigationBudget(() => driver.inputMouse('mouseMoved', clickPoint.x, clickPoint.y, { modifiers: this.modifiers }), deadline);
1748
+ await withNavigationBudget(() => driver.inputMouse('mouseMoved', clickPoint.x, clickPoint.y, { modifiers: this.modifiers }), deadline, signal);
1281
1749
  await withNavigationBudget(() => getCdpSession().inputMouse('mousePressed', clickPoint.x, clickPoint.y, {
1282
1750
  button: 'left',
1283
1751
  clickCount: 1,
1284
1752
  modifiers: this.modifiers,
1285
1753
  releaseAfterPress: true,
1286
- }, remaining()), deadline);
1754
+ }, remaining()), deadline, signal);
1287
1755
  return;
1288
1756
  }
1289
1757
  if (this.getMode() !== 'extension' || !this.extensionBridge) {
@@ -1293,7 +1761,7 @@ class UnifiedSessionManager {
1293
1761
  if (!targetId) {
1294
1762
  throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '没有当前受控页面,无法使用系统鼠标点击 Turnstile', '先 browse attach 到受控测试页,把该页放到已经聚焦的窗口并保持为活动 tab 后重试');
1295
1763
  }
1296
- const topology = await this.listTopology(remaining());
1764
+ const topology = await withNavigationBudget(() => this.listTopology(remaining()), deadline, signal);
1297
1765
  const windowInfo = topology?.windows.find((item) => item.tabs.some((tab) => String(tab.id) === targetId || tab.targetId === targetId));
1298
1766
  const tab = windowInfo?.tabs.find((item) => String(item.id) === targetId || item.targetId === targetId);
1299
1767
  if (!windowInfo || !tab) {
@@ -1309,29 +1777,96 @@ class UnifiedSessionManager {
1309
1777
  if (tab.active !== true && (activeTabId === undefined || String(activeTabId) !== targetId)) {
1310
1778
  throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '受控测试页不是当前窗口的活动 tab,拒绝系统鼠标点击', '先把受控测试页设为该窗口的活动 tab,且窗口已经聚焦后再重试');
1311
1779
  }
1312
- await withNavigationBudget(() => this.extensionBridge.debuggerDetach(undefined, remaining()), deadline);
1313
- const viewport = await withNavigationBudget(() => this.readViewportMetrics(0), deadline);
1314
- const refreshedTopology = await this.listTopology(remaining());
1315
- const refreshedWindow = refreshedTopology?.windows.find((item) => item.tabs.some((candidate) => String(candidate.id) === targetId || candidate.targetId === targetId));
1316
- const refreshedTab = refreshedWindow?.tabs.find((candidate) => String(candidate.id) === targetId || candidate.targetId === targetId);
1317
- const refreshedActiveTabId = refreshedWindow?.activeTabId;
1318
- if (!refreshedWindow?.focused ||
1319
- refreshedTab?.managed !== true ||
1320
- (refreshedTab.active !== true &&
1321
- (refreshedActiveTabId === undefined || String(refreshedActiveTabId) !== targetId))) {
1322
- throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '系统鼠标点击前,受控测试页的窗口或活动 tab 已变化', '重新聚焦受控测试页并保持为活动 tab 后重试');
1323
- }
1324
- const frameOffset = this.currentFrameOffset;
1325
- const clickPoint = snapshot.clickPoint && frameOffset
1326
- ? { x: snapshot.clickPoint.x + frameOffset.x, y: snapshot.clickPoint.y + frameOffset.y }
1327
- : snapshot.clickPoint;
1328
- const screen = await withNavigationBudget(() => resolveTurnstileOsClickPoint({
1329
- ...snapshot,
1330
- clickPoint,
1331
- screenPoint: null,
1332
- viewport,
1333
- }, { deadline }), deadline);
1334
- await withNavigationBudget(() => osLeftClick(screen.x, screen.y, { deadline }), deadline);
1780
+ await withNavigationBudget(() => this.extensionBridge.debuggerDetach(undefined, remaining()), deadline, signal);
1781
+ const pointerId = process.platform === 'linux' ? randomUUID() : undefined;
1782
+ let pointerDocumentId;
1783
+ const resolvePoint = async (refresh, probe) => {
1784
+ const current = refresh && refreshSnapshot
1785
+ ? await withNavigationBudget(() => refreshSnapshot(remaining()), deadline, signal)
1786
+ : snapshot;
1787
+ if (!current.clickPoint ||
1788
+ current.clickSource !== 'iframe' ||
1789
+ current.clickTargetKind !== snapshot.clickTargetKind ||
1790
+ current.url !== snapshot.url) {
1791
+ throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '系统鼠标按下前,页面或验证控件已变化', '重新检查页面验证状态后再重试');
1792
+ }
1793
+ const viewport = await withNavigationBudget(() => this.readViewportMetrics(0), deadline, signal);
1794
+ const refreshedTopology = await withNavigationBudget(() => this.listTopology(remaining()), deadline, signal);
1795
+ const refreshedWindow = refreshedTopology?.windows.find((item) => item.tabs.some((candidate) => String(candidate.id) === targetId || candidate.targetId === targetId));
1796
+ const refreshedTab = refreshedWindow?.tabs.find((candidate) => String(candidate.id) === targetId || candidate.targetId === targetId);
1797
+ const refreshedActiveTabId = refreshedWindow?.activeTabId;
1798
+ if (!refreshedWindow?.focused ||
1799
+ refreshedTab?.managed !== true ||
1800
+ (refreshedTab.active !== true &&
1801
+ (refreshedActiveTabId === undefined || String(refreshedActiveTabId) !== targetId))) {
1802
+ throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '系统鼠标点击前,受控测试页的窗口或活动 tab 已变化', '重新聚焦受控测试页并保持为活动 tab 后重试');
1803
+ }
1804
+ const frameOffset = this.currentFrameOffset;
1805
+ const clickPoint = frameOffset
1806
+ ? { x: current.clickPoint.x + frameOffset.x, y: current.clickPoint.y + frameOffset.y }
1807
+ : current.clickPoint;
1808
+ const estimate = await withNavigationBudget(() => resolveTurnstileOsClickPoint({ ...current, clickPoint, screenPoint: null, viewport }, { deadline, signal }), deadline, signal);
1809
+ if (!pointerId || !probe) {
1810
+ return estimate;
1811
+ }
1812
+ const request = await probe();
1813
+ const origin = (await withNavigationBudget(() => this.extensionBridge.getViewportMetrics(0, { operation: 'read', id: pointerId, documentId: pointerDocumentId, ...request }, remaining()), deadline, signal));
1814
+ if (typeof origin.x !== 'number' ||
1815
+ !Number.isFinite(origin.x) ||
1816
+ typeof origin.y !== 'number' ||
1817
+ !Number.isFinite(origin.y)) {
1818
+ throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '未取得可信原生指针原点', '重新检查当前受控页面');
1819
+ }
1820
+ return {
1821
+ x: Math.round(origin.x + clickPoint.x * viewport.devicePixelRatio),
1822
+ y: Math.round(origin.y + clickPoint.y * viewport.devicePixelRatio),
1823
+ };
1824
+ };
1825
+ let clickError;
1826
+ try {
1827
+ if (pointerId) {
1828
+ const started = (await withNavigationBudget(() => this.extensionBridge.getViewportMetrics(0, { operation: 'start', id: pointerId }, remaining()), deadline, signal));
1829
+ pointerDocumentId = started.documentId;
1830
+ if (!pointerDocumentId) {
1831
+ throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '原生指针观察器未取得文档身份', '重新检查当前受控页面');
1832
+ }
1833
+ }
1834
+ const screen = await resolvePoint(false);
1835
+ // 请求取消后仍须等待必要的鼠标释放完成,再结束 tab 锁
1836
+ await osLeftClick(screen.x, screen.y, {
1837
+ deadline,
1838
+ signal,
1839
+ refreshPoint: (probe) => resolvePoint(true, probe),
1840
+ });
1841
+ }
1842
+ catch (error) {
1843
+ clickError = { error };
1844
+ }
1845
+ if (pointerId) {
1846
+ try {
1847
+ await this.tabLockContext.run({
1848
+ ...scope,
1849
+ session: this,
1850
+ signal: undefined,
1851
+ deadline: Date.now() + 5000,
1852
+ budgetError: undefined,
1853
+ userAction: undefined,
1854
+ pendingPageCondition: null,
1855
+ keyboardFocus: undefined,
1856
+ inputCleanup: undefined,
1857
+ }, () => this.extensionBridge.getViewportMetrics(0, { operation: 'stop', id: pointerId }, 5000));
1858
+ }
1859
+ catch (error) {
1860
+ console.warn('[MCP] Native pointer observer cleanup failed:', error);
1861
+ if (clickError?.error instanceof Error) {
1862
+ Object.assign(clickError.error, { pointerCleanupError: error });
1863
+ }
1864
+ clickError ??= { error };
1865
+ }
1866
+ }
1867
+ if (clickError) {
1868
+ throw clickError.error;
1869
+ }
1335
1870
  }
1336
1871
  // noinspection JSUnusedGlobalSymbols — resolveChallenge 通过 ChallengeHost 结构类型调用
1337
1872
  async inspectChallenge(options, timeout) {
@@ -1360,10 +1895,20 @@ class UnifiedSessionManager {
1360
1895
  /**
1361
1896
  * 鼠标滚轮
1362
1897
  */
1363
- async mouseWheel(deltaX, deltaY) {
1364
- const { x, y } = this.currentMousePosition;
1365
- const driver = await this.getDriver();
1366
- await driver.inputMouse('mouseWheel', x, y, { deltaX, deltaY, modifiers: this.modifiers });
1898
+ async mouseWheel(deltaX, deltaY, timeout, point) {
1899
+ this.assertRequestActive();
1900
+ const deadline = timeout === undefined ? undefined : Date.now() + timeout;
1901
+ const remaining = () => (deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
1902
+ const driver = await this.getDriver(remaining());
1903
+ this.assertRequestActive();
1904
+ const { x, y } = point ?? this.currentMousePosition;
1905
+ const dispatch = () => driver.inputMouse('mouseWheel', x, y, { deltaX, deltaY, modifiers: this.modifiers }, remaining());
1906
+ if (point) {
1907
+ await dispatch();
1908
+ }
1909
+ else {
1910
+ await this.withMouseFrame(dispatch);
1911
+ }
1367
1912
  }
1368
1913
  /**
1369
1914
  * 注入反检测脚本
@@ -1465,17 +2010,29 @@ class UnifiedSessionManager {
1465
2010
  metadataKey(targetId) {
1466
2011
  return `${this.getMode()}:${targetId}`;
1467
2012
  }
2013
+ rememberPageMetadata(targetId, metadata) {
2014
+ const lastUsedAt = this.getPageMetadata(targetId).lastUsedAt;
2015
+ this.pageMetadata.set(this.metadataKey(targetId), {
2016
+ ...metadata,
2017
+ lastUsedAt: lastUsedAt && lastUsedAt > (metadata.lastUsedAt ?? '') ? lastUsedAt : metadata.lastUsedAt,
2018
+ });
2019
+ }
1468
2020
  recordPageUse(targetId, created = false) {
1469
2021
  const metadata = this.getPageMetadata(targetId);
1470
2022
  this.pageMetadata.set(this.metadataKey(targetId), {
1471
2023
  ...metadata,
2024
+ origin: created ? 'created' : metadata.origin,
1472
2025
  createdBy: created ? 'mcp-chrome' : metadata.createdBy,
1473
2026
  createdAt: created ? new Date().toISOString() : metadata.createdAt,
1474
2027
  lastUsedAt: new Date().toISOString(),
1475
2028
  });
1476
2029
  }
1477
- evaluateViaCdp(script, args, timeout) {
1478
- return getCdpSession().evaluate(script, args, timeout);
2030
+ evaluateViaCdp(script, args, timeout, options) {
2031
+ const session = getCdpSession();
2032
+ if (session.getCurrentFrameId() !== 0 && options.staleContextRetry === 'readOnly') {
2033
+ return Promise.reject(new StructuredToolError('DRIVER_CAPABILITY_ERROR', 'CDP iframe 暂不支持 readOnly context 重试,脚本未执行', '使用 Extension precise 执行无副作用脚本,或显式使用 never 并独立核对结果', { actionExecuted: false, actionStatus: 'not_started', failureStage: 'context', retryable: false }));
2034
+ }
2035
+ return session.evaluate(script, args, timeout, options.userAction);
1479
2036
  }
1480
2037
  async evaluateViaExtensionPrecise(code, expression, args, timeout, options = {}) {
1481
2038
  const hasArgs = args !== undefined;
@@ -1492,7 +2049,7 @@ class UnifiedSessionManager {
1492
2049
  const argsStr = args.map((a) => JSON.stringify(a)).join(', ');
1493
2050
  iframeExpression = `(${code})(${argsStr})`;
1494
2051
  }
1495
- const result = (await this.extensionBridge.evaluateInFrame(currentFrameId, iframeExpression, timeout, staleContextRetry));
2052
+ const result = (await this.extensionBridge.evaluateInFrame(currentFrameId, iframeExpression, timeout, staleContextRetry, options.userAction));
1496
2053
  if (result.frameContext) {
1497
2054
  this.lastEvaluationMetadata = {
1498
2055
  retryAttempted: result.retryAttempted === true,
@@ -1511,14 +2068,14 @@ class UnifiedSessionManager {
1511
2068
  if (timeout !== undefined) {
1512
2069
  params.timeout = timeout;
1513
2070
  }
1514
- const result = (await this.extensionBridge.debuggerSend('Runtime.evaluate', params, undefined, timeout));
2071
+ const result = (await this.extensionBridge.debuggerSend('Runtime.evaluate', params, undefined, timeout, options.userAction));
1515
2072
  if (result.exceptionDetails) {
1516
- throw new Error(formatCdpException(result.exceptionDetails));
2073
+ throw setErrorSource(new Error(formatCdpException(result.exceptionDetails)), 'page');
1517
2074
  }
1518
2075
  return this.materializeCdpResult(result.result, timeout, options.fileOutput);
1519
2076
  }
1520
- async evaluateViaExtensionStealth(expression, timeout) {
1521
- return (await this.extensionBridge.evaluate(expression, undefined, timeout));
2077
+ async evaluateViaExtensionStealth(expression, timeout, userAction) {
2078
+ return (await this.extensionBridge.evaluate(expression, undefined, timeout, userAction));
1522
2079
  }
1523
2080
  async readViewportMetrics(frameId) {
1524
2081
  let raw;
@@ -1547,7 +2104,8 @@ class UnifiedSessionManager {
1547
2104
  }
1548
2105
  catch (err) {
1549
2106
  const msg = err instanceof Error ? err.message : String(err);
1550
- if (isExtensionDisconnected(err) || msg.includes('not connected')) {
2107
+ if (!isNativeExecutionResultUnknown(err) &&
2108
+ (isExtensionDisconnected(err) || msg.includes('not connected'))) {
1551
2109
  // 等待 Extension 重连
1552
2110
  await new Promise((r) => setTimeout(r, 2000));
1553
2111
  if (this.isExtensionConnected()) {
@@ -1584,7 +2142,7 @@ class UnifiedSessionManager {
1584
2142
  */
1585
2143
  checkCdpResult(result) {
1586
2144
  if (result.exceptionDetails) {
1587
- throw new Error(formatCdpException(result.exceptionDetails));
2145
+ throw setErrorSource(new Error(formatCdpException(result.exceptionDetails)), 'page');
1588
2146
  }
1589
2147
  return extractCdpValue(result.result);
1590
2148
  }
@@ -1623,13 +2181,27 @@ class UnifiedSessionManager {
1623
2181
  generatePreview: false,
1624
2182
  }, undefined, timeout));
1625
2183
  if (properties.exceptionDetails) {
1626
- throw new Error(formatCdpException(properties.exceptionDetails));
2184
+ throw setErrorSource(new Error(formatCdpException(properties.exceptionDetails)), 'page');
1627
2185
  }
1628
2186
  const descriptors = properties.result ?? [];
1629
2187
  state.nodes += descriptors.length;
1630
2188
  this.assertMaterializeBudget(state, depth, result);
1631
2189
  if (result.subtype === 'array') {
1632
- const array = [];
2190
+ const length = descriptors.find((descriptor) => descriptor.name === 'length')?.value?.value;
2191
+ if (typeof length !== 'number' ||
2192
+ !Number.isInteger(length) ||
2193
+ length < 0 ||
2194
+ length > UnifiedSessionManager.CDP_MATERIALIZE_MAX_NODES) {
2195
+ this.throwMaterializeLimit(state, depth, result);
2196
+ }
2197
+ const populated = descriptors.filter((descriptor) => /^\d+$/.test(descriptor.name) &&
2198
+ Number(descriptor.name) < length &&
2199
+ descriptor.value).length;
2200
+ const holes = length - populated;
2201
+ state.nodes += holes;
2202
+ state.chars += holes * 5;
2203
+ this.assertMaterializeBudget(state, depth, result);
2204
+ const array = new Array(length);
1633
2205
  for (const descriptor of descriptors) {
1634
2206
  if (!descriptor.value || !/^\d+$/.test(descriptor.name)) {
1635
2207
  continue;
@@ -1649,7 +2221,12 @@ class UnifiedSessionManager {
1649
2221
  continue;
1650
2222
  }
1651
2223
  this.trackMaterializedPropertyName(descriptor.name, state, depth, result);
1652
- object[descriptor.name] = await this.materializePropertyValue(descriptor.value, timeout, state, depth + 1);
2224
+ Object.defineProperty(object, descriptor.name, {
2225
+ value: await this.materializePropertyValue(descriptor.value, timeout, state, depth + 1),
2226
+ enumerable: true,
2227
+ configurable: true,
2228
+ writable: true,
2229
+ });
1653
2230
  }
1654
2231
  return object;
1655
2232
  }
@@ -1748,6 +2325,7 @@ class UnifiedSessionManager {
1748
2325
  * 避免工具 timeout 被连接等待吞掉,不传则使用默认 30s
1749
2326
  */
1750
2327
  async getDriver(timeout) {
2328
+ this.assertRequestActive();
1751
2329
  const scopedMode = this.tabLockContext.getStore()?.mode;
1752
2330
  if (scopedMode === 'extension') {
1753
2331
  if (!this.extensionBridge?.isConnected()) {
@@ -1775,6 +2353,7 @@ class UnifiedSessionManager {
1775
2353
  return getCdpSession();
1776
2354
  }
1777
2355
  async ensureExtensionConnected(maxWait) {
2356
+ this.assertRequestActive();
1778
2357
  if (!this.extensionBridge) {
1779
2358
  return this.assertCdpFallbackAllowed();
1780
2359
  }
@@ -1803,7 +2382,9 @@ class UnifiedSessionManager {
1803
2382
  }
1804
2383
  console.error(`[MCP] Waiting for Chrome Extension connection (${waitTimeout}ms timeout)...`);
1805
2384
  console.error('[MCP] Please ensure Chrome is running with MCP Chrome extension installed.');
1806
- const connected = await this.extensionBridge.waitForConnection(waitTimeout);
2385
+ const scope = this.tabLockContext.getStore();
2386
+ const connected = await withNavigationBudget(() => this.extensionBridge.waitForConnection(waitTimeout), scope?.deadline, scope?.signal);
2387
+ this.assertRequestActive();
1807
2388
  if (connected) {
1808
2389
  console.error('[MCP] Chrome Extension connected successfully');
1809
2390
  this.lastConnectionFailure = 0;
@@ -1843,9 +2424,9 @@ class UnifiedSessionManager {
1843
2424
  if (timeout !== undefined) {
1844
2425
  params.timeout = timeout;
1845
2426
  }
1846
- const result = (await this.extensionBridge.debuggerSend('Runtime.callFunctionOn', params, undefined, timeout));
2427
+ const result = (await this.extensionBridge.debuggerSend('Runtime.callFunctionOn', params, undefined, timeout, options.userAction));
1847
2428
  if (result.exceptionDetails) {
1848
- throw new Error(formatCdpException(result.exceptionDetails));
2429
+ throw setErrorSource(new Error(formatCdpException(result.exceptionDetails)), 'page');
1849
2430
  }
1850
2431
  return await this.materializeCdpResult(result.result, timeout, options.fileOutput);
1851
2432
  }
@@ -1855,6 +2436,34 @@ class UnifiedSessionManager {
1855
2436
  }).catch(() => { });
1856
2437
  }
1857
2438
  }
2439
+ async withMouseFrame(fn) {
2440
+ const bridge = this.isExtensionConnected() ? this.extensionBridge : null;
2441
+ const original = this.mouseFrameContext?.targetId === this.getCurrentTargetId() ? this.mouseFrameContext : undefined;
2442
+ if (!bridge || !original) {
2443
+ return fn();
2444
+ }
2445
+ const frameId = bridge.getCurrentFrameId();
2446
+ const documentId = bridge.getCurrentFrameDocumentId();
2447
+ bridge.setCurrentFrameId(original.frameId, original.documentId);
2448
+ try {
2449
+ return await fn();
2450
+ }
2451
+ finally {
2452
+ bridge.setCurrentFrameId(frameId, documentId);
2453
+ }
2454
+ }
2455
+ rememberMouseFrame() {
2456
+ const bridge = this.isExtensionConnected() ? this.extensionBridge : null;
2457
+ const targetId = bridge?.getCurrentTargetId();
2458
+ this.mouseFrameContext =
2459
+ bridge && targetId
2460
+ ? {
2461
+ targetId,
2462
+ frameId: bridge.getCurrentFrameId(),
2463
+ documentId: bridge.getCurrentFrameDocumentId(),
2464
+ }
2465
+ : undefined;
2466
+ }
1858
2467
  }
1859
2468
  /**
1860
2469
  * 获取统一会话管理器实例