pi-chrome 0.15.47 → 0.15.49

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.
@@ -64,6 +64,7 @@ const DEFAULT_PORT = Number(process.env.PI_CHROME_BRIDGE_PORT ?? "17318");
64
64
  const DEFAULT_TIMEOUT_MS = 30_000;
65
65
  const MAX_TEXT_CHARS = 30_000;
66
66
  const MAX_ELEMENTS = 80;
67
+ const BACKGROUND_PARAM_DESCRIPTION = "If true, avoid explicit Chrome focus/tab activation for this call. /chrome background on (default) enforces this for every call and ignores false. Ask the user to run /chrome background off to allow foreground work.";
67
68
 
68
69
  function truncateText(text: string, maxChars = MAX_TEXT_CHARS): string {
69
70
  if (text.length <= maxChars) return text;
@@ -685,7 +686,7 @@ export default function (pi: ExtensionAPI): void {
685
686
  globalState[PI_CHROME_GLOBAL_KEY] = { version: PI_CHROME_VERSION, root: currentRoot, token: instanceToken };
686
687
 
687
688
  const bridge = new ChromeProfileBridge(DEFAULT_HOST, DEFAULT_PORT);
688
- let backgroundDefault = true;
689
+ let backgroundEnabled = true;
689
690
  let chromeAuthorizedUntil: number | "indefinite" | undefined;
690
691
  // Restore an authorization that survived a /reload. Drop it if it already expired.
691
692
  const persistedAuth = globalState[PI_CHROME_AUTH_KEY];
@@ -881,14 +882,21 @@ export default function (pi: ExtensionAPI): void {
881
882
  }, Math.max(0, until - Date.now()));
882
883
  };
883
884
 
884
- const authorizedBridgeSend = (action: string, params: Record<string, unknown>, timeoutMs = DEFAULT_TIMEOUT_MS, signal?: AbortSignal): Promise<unknown> => {
885
+ const authorizedBridgeSend = async (action: string, params: Record<string, unknown>, timeoutMs = DEFAULT_TIMEOUT_MS, signal?: AbortSignal): Promise<unknown> => {
885
886
  requireChromeControlAuthorized();
886
- // Scope the service worker's dedicated automation tab/window to this session. Forwarded on
887
- // every action so tab resolution, navigation, and cleanup all agree on which target is ours.
887
+ // Background on is a session policy, not a default that tool arguments can override.
888
+ // Apply it here so tab.new, chrome_launch(url), and tools without a background parameter
889
+ // cannot bypass it. Background off still permits per-call background:true.
890
+ const typed = params as { background?: boolean; foreground?: boolean };
891
+ const requestedBackground = typed.background ?? (typed.foreground !== undefined ? !typed.foreground : false);
892
+ const background = backgroundEnabled || requestedBackground;
893
+ if (action === "tab.activate" && background) {
894
+ throw new Error("Tab activation is blocked by background mode. Ask the user to run /chrome background off to allow foreground work.");
895
+ }
896
+ // Scope every action to this session's dedicated automation target and tab group.
888
897
  const sessionKey = sessionKeyFor(sessionCtx);
889
- let wireParams: Record<string, unknown> = sessionKey !== undefined && params.sessionKey === undefined
890
- ? { ...params, sessionKey }
891
- : params;
898
+ let wireParams: Record<string, unknown> = { ...params, background, foreground: !background };
899
+ if (sessionKey !== undefined && params.sessionKey === undefined) wireParams.sessionKey = sessionKey;
892
900
  const sessionTitle = sessionCtx !== undefined ? sessionGroupTitle(sessionCtx) : undefined;
893
901
  // Any tab Pi opens through tab.new/tab.group must use THIS session's group, even if a caller
894
902
  // passes group:false or a custom groupTitle. This central guard covers chrome_tab plus internal
@@ -904,21 +912,21 @@ export default function (pi: ExtensionAPI): void {
904
912
  if (shouldJoinGroup) {
905
913
  wireParams = { ...wireParams, sessionGroupTitle: sessionTitle, joinSessionGroup: true };
906
914
  }
907
- return bridge.send(action, wireParams, timeoutMs, signal);
908
- };
909
-
910
- // Translate the public `background` parameter (default on = silent/background) into the
911
- // service worker's wire-level `foreground` flag, accepting legacy `foreground` as a fallback.
912
- const withBackground = <T extends Record<string, unknown>>(params: T): T => {
913
- const typed = params as { background?: boolean; foreground?: boolean };
914
- const explicit =
915
- typed.background !== undefined
916
- ? typed.background
917
- : typed.foreground !== undefined
918
- ? !typed.foreground
919
- : undefined;
920
- const background = explicit ?? backgroundDefault;
921
- return { ...params, foreground: !background } as T;
915
+ // Older companions ignore background for tab creation and activate tabs for screenshots.
916
+ // Dedicated wire actions make them fail closed, with no probe/action race or extra round trip.
917
+ // These are internal protocol aliases, not new tools or /chrome commands.
918
+ const wireAction = background && (action === "tab.new" || action === "page.screenshot")
919
+ ? `${action}.background`
920
+ : action;
921
+ try {
922
+ return await bridge.send(wireAction, wireParams, timeoutMs, signal);
923
+ } catch (error) {
924
+ const message = error instanceof Error ? error.message : String(error);
925
+ if (wireAction !== action && message.includes(`Unknown action: ${wireAction}`)) {
926
+ throw new Error("Hard background requires an updated Chrome companion extension. Reload Pi Chrome Connector at chrome://extensions, then retry.");
927
+ }
928
+ throw error;
929
+ }
922
930
  };
923
931
 
924
932
  pi.on("session_start", async (_event, ctx) => {
@@ -974,7 +982,7 @@ Usage rules:
974
982
  3. \`includeSnapshot=true\` on click/type/fill/key to verify in one round trip.
975
983
  4. If \`chrome_evaluate\` returns null when you expected a value, the expression evaluated to null/undefined in the page; surface the value via \`JSON.stringify\` to confirm.
976
984
  5. \`chrome_navigate\` supports an optional \`initScript\` that runs at document_start in MAIN world for the next navigation (good for seeding localStorage or stubbing Date.now).
977
- 6. By default chrome_* tools run in the background without focusing Chrome; pass \`background=false\` or run /chrome background off when the user wants to watch Chrome work.
985
+ 6. /chrome background on (default) is a hard policy: per-call \`background=false\` cannot override it, new tabs stay inactive, and \`chrome_tab activate\` is blocked. Ask the user to run /chrome background off when they want foreground/watch mode. With background off, per-call \`background=true\` still avoids explicit focus/tab activation. Screenshots use CDP without activating background tabs; debugger failures never fall back to tab activation. Page scripts, trusted input, native prompts, and Chrome/OS behavior can still affect focus.
978
986
  7. If you hit a native file-picker or privileged browser prompt gate, tell the user; generic clicks/typing/CSP gates are handled by Chrome input.
979
987
  8. Run /chrome doctor when in doubt about connectivity or capabilities.
980
988
  </chrome-profile-bridge>`;
@@ -1045,30 +1053,30 @@ Usage rules:
1045
1053
  ctx.ui.notify(lines.join("\n"), "info");
1046
1054
  };
1047
1055
 
1048
- // Run-in-background (Chrome focus) handler. No args = toggle. Explicit on/off/status.
1056
+ // Existing background setting is the hard policy. No args = toggle; no separate lock mode.
1049
1057
  const BACKGROUND_DESC: Record<string, string> = {
1050
- on: "pi-chrome runs in the background; Chrome won't pop up or steal focus.",
1051
- off: "Chrome pops to the front and switches tabs so you can watch what pi-chrome is doing.",
1058
+ on: "Hard background: pi-chrome will not explicitly focus windows or activate tabs; per-call foreground overrides and tab activation are blocked. Page/Chrome behavior can still affect focus.",
1059
+ off: "Foreground/watch mode: Chrome may come forward and switch tabs. Per-call background:true still avoids explicit focus/tab activation.",
1052
1060
  };
1053
1061
 
1054
1062
  const backgroundHandler = async (ctx: ExtensionContext, args: string) => {
1055
1063
  const arg = (args || "").trim().toLowerCase();
1056
- const currentLabel = backgroundDefault ? "on" : "off";
1064
+ const currentLabel = backgroundEnabled ? "on" : "off";
1057
1065
 
1058
1066
  if (arg === "status") {
1059
1067
  ctx.ui.notify(`Run in background is ${currentLabel}. ${BACKGROUND_DESC[currentLabel]}`, "info");
1060
1068
  return;
1061
1069
  }
1062
1070
 
1063
- if (arg === "on" || arg === "true" || arg === "1") backgroundDefault = true;
1064
- else if (arg === "off" || arg === "false" || arg === "0") backgroundDefault = false;
1065
- else if (arg === "toggle" || arg === "") backgroundDefault = !backgroundDefault;
1071
+ if (arg === "on" || arg === "true" || arg === "1") backgroundEnabled = true;
1072
+ else if (arg === "off" || arg === "false" || arg === "0") backgroundEnabled = false;
1073
+ else if (arg === "toggle" || arg === "") backgroundEnabled = !backgroundEnabled;
1066
1074
  else {
1067
1075
  ctx.ui.notify(`Unknown background setting '${arg}'. Pick one of: on | off | toggle | status.`, "warning");
1068
1076
  return;
1069
1077
  }
1070
1078
 
1071
- const nextLabel = backgroundDefault ? "on" : "off";
1079
+ const nextLabel = backgroundEnabled ? "on" : "off";
1072
1080
  ctx.ui.notify(`Run in background → ${nextLabel}. ${BACKGROUND_DESC[nextLabel]}`, "info");
1073
1081
  };
1074
1082
 
@@ -1151,7 +1159,7 @@ Usage rules:
1151
1159
  parts.push(`✗ Chrome not responding`);
1152
1160
  }
1153
1161
  parts.push(`auth: ${authSummary()}`);
1154
- parts.push(`background: ${backgroundDefault ? "on" : "off"}`);
1162
+ parts.push(`background: ${backgroundEnabled ? "on (hard)" : "off"}`);
1155
1163
  return parts.join(" · ");
1156
1164
  };
1157
1165
 
@@ -1217,7 +1225,7 @@ Usage rules:
1217
1225
 
1218
1226
  pi.registerCommand("chrome", {
1219
1227
  description:
1220
- "All pi-chrome controls in one place.\n /chrome authorize [15m|30m|<minutes>|indefinite] — allow this Pi session to use chrome_* tools.\n /chrome revoke — lock Chrome control.\n /chrome status — one-line snapshot of connection, auth, and background setting.\n /chrome doctor — full health check.\n /chrome onboard — install the Chrome companion extension.\n /chrome background [on|off|status|toggle] — whether pi-chrome runs without focusing Chrome.\nRun with no arguments for an interactive picker that shows current state.",
1228
+ "All pi-chrome controls in one place.\n /chrome authorize [15m|30m|<minutes>|indefinite] — allow this Pi session to use chrome_* tools.\n /chrome revoke — lock Chrome control.\n /chrome status — one-line snapshot of connection, auth, and background setting.\n /chrome doctor — full health check.\n /chrome onboard — install the Chrome companion extension.\n /chrome background [on|off|status|toggle] — enforce no explicit focus/tab activation, or allow foreground/watch mode.\nRun with no arguments for an interactive picker that shows current state.",
1221
1229
  getArgumentCompletions: (prefix) => {
1222
1230
  const raw = prefix;
1223
1231
  const trimmedRight = raw.replace(/\s+$/, "");
@@ -1239,7 +1247,7 @@ Usage rules:
1239
1247
  { fullValue: "status", label: "status", description: "One-line summary: connection, auth, and background setting." },
1240
1248
  { fullValue: "doctor", label: "doctor", description: "Full health check. Tells you if Chrome is connected and what's wrong if it isn't." },
1241
1249
  { fullValue: "onboard", label: "onboard", description: "Install the Chrome companion extension (first-time setup)." },
1242
- { fullValue: "background", label: "background", description: "Run pi-chrome in the background without focusing Chrome?" },
1250
+ { fullValue: "background", label: "background", description: "Enforce hard background or allow foreground/watch mode." },
1243
1251
  ];
1244
1252
  } else if (path[0] === "authorize" && path.length === 1) {
1245
1253
  candidates = [
@@ -1249,7 +1257,7 @@ Usage rules:
1249
1257
  ];
1250
1258
  } else if (path[0] === "background" && path.length === 1) {
1251
1259
  candidates = [
1252
- { fullValue: "background on", label: "on", description: "Run in background. Chrome stays in the background. Your editor keeps focus. (default)" },
1260
+ { fullValue: "background on", label: "on", description: "Hard background: block explicit focus/tab activation and per-call foreground overrides. (default)" },
1253
1261
  { fullValue: "background off", label: "off", description: "Bring Chrome to the front so you can watch." },
1254
1262
  { fullValue: "background toggle", label: "toggle", description: "Flip whichever way it's currently set." },
1255
1263
  { fullValue: "background status", label: "status", description: "Show the current setting." },
@@ -1333,7 +1341,7 @@ Usage rules:
1333
1341
  pi.registerTool({
1334
1342
  name: "chrome_tab",
1335
1343
  label: "Chrome Tab",
1336
- description: "List, create, activate, close, group, ungroup, or inspect tabs in the user's existing Chrome profile via the companion extension. New/grouped tabs always use this session's Pi tab group. activate/close/group/ungroup require a target (targetId/urlIncludes/titleIncludes); with no target they act on this session's pi-chrome automation tab if one exists, and otherwise error rather than touching the user's active tab.",
1344
+ description: "List, create, activate, close, group, ungroup, or inspect tabs in the user's existing Chrome profile via the companion extension. New/grouped tabs always use this session's Pi tab group. Background mode keeps new tabs inactive and blocks activate; ask the user to run /chrome background off for foreground/watch mode. activate/close/group/ungroup require a target (targetId/urlIncludes/titleIncludes); with no target they act on this session's pi-chrome automation tab if one exists, and otherwise error rather than touching the user's active tab.",
1337
1345
  promptSnippet: "List/open/activate/close/group existing Chrome tabs through the companion extension.",
1338
1346
  parameters: Type.Object({
1339
1347
  action: StringEnum(tabActionValues),
@@ -1369,7 +1377,7 @@ Usage rules:
1369
1377
  name: "chrome_snapshot",
1370
1378
  label: "Chrome Snapshot",
1371
1379
  description:
1372
- "Inspect a page in the user's existing Chrome profile. Default output is a concise, agent-friendly observation with structural layout/context, stable uids, visible actions, form fields, page hints, and changes since the previous snapshot. Use mode/query/nearUid to zoom instead of dumping the whole page. Runs in the background by default; pass background=false to bring Chrome to the foreground so the user can watch.",
1380
+ "Inspect a page in the user's existing Chrome profile. Default output is a concise, agent-friendly observation with structural layout/context, stable uids, visible actions, form fields, page hints, and changes since the previous snapshot. Use mode/query/nearUid to zoom instead of dumping the whole page. Background mode (default) blocks explicit focus/tab activation even with background=false. Ask the user to run /chrome background off for foreground/watch mode.",
1373
1381
  promptSnippet: "Observe the current Chrome page: concise summary, structural layout, visible actions, forms, page map, query matches, and stable uids.",
1374
1382
  parameters: Type.Object({
1375
1383
  targetId: Type.Optional(Type.String()),
@@ -1382,16 +1390,14 @@ Usage rules:
1382
1390
  containingText: Type.Optional(Type.String({ description: "Only return elements whose label/text contains this string (case-insensitive). Useful when the page has many controls." })),
1383
1391
  roleFilter: Type.Optional(Type.String({ description: "Only return elements matching this ARIA role or tag name (case-insensitive). e.g. 'button', 'link', 'textbox'." })),
1384
1392
  nearUid: Type.Optional(Type.String({ description: "Sort elements by proximity to this snapshot uid. Useful for finding controls near a known anchor." })),
1385
- background: Type.Optional(
1386
- Type.Boolean({ description: "If true (the default), run silently in the background without focusing Chrome; pass false so Chrome focuses + the tab activates and the user can watch." }),
1387
- ),
1393
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1388
1394
  host: Type.Optional(Type.String()),
1389
1395
  port: Type.Optional(Type.Number()),
1390
1396
  }),
1391
1397
  async execute(_id, params, signal): Promise<ToolTextResult> {
1392
1398
  const snapshot = await authorizedBridgeSend(
1393
1399
  "page.snapshot",
1394
- withBackground({ ...params, maxElements: params.maxElements ?? MAX_ELEMENTS }),
1400
+ { ...params, maxElements: params.maxElements ?? MAX_ELEMENTS },
1395
1401
  DEFAULT_TIMEOUT_MS,
1396
1402
  signal,
1397
1403
  );
@@ -1412,16 +1418,14 @@ Usage rules:
1412
1418
  targetId: Type.Optional(Type.String()),
1413
1419
  urlIncludes: Type.Optional(Type.String()),
1414
1420
  titleIncludes: Type.Optional(Type.String()),
1415
- background: Type.Optional(
1416
- Type.Boolean({ description: "If true (the default), run silently in the background without focusing Chrome; pass false so Chrome focuses + the tab activates and the user can watch." }),
1417
- ),
1421
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1418
1422
  host: Type.Optional(Type.String()),
1419
1423
  port: Type.Optional(Type.Number()),
1420
1424
  }),
1421
1425
  async execute(_id, params, signal): Promise<ToolTextResult> {
1422
1426
  const snapshot = await authorizedBridgeSend(
1423
1427
  "page.snapshot",
1424
- withBackground({ ...params, mode: params.mode || "auto", maxElements: params.maxElements ?? MAX_ELEMENTS }),
1428
+ { ...params, mode: params.mode || "auto", maxElements: params.maxElements ?? MAX_ELEMENTS },
1425
1429
  DEFAULT_TIMEOUT_MS,
1426
1430
  signal,
1427
1431
  );
@@ -1442,15 +1446,13 @@ Usage rules:
1442
1446
  targetId: Type.Optional(Type.String()),
1443
1447
  urlIncludes: Type.Optional(Type.String()),
1444
1448
  titleIncludes: Type.Optional(Type.String()),
1445
- background: Type.Optional(
1446
- Type.Boolean({ description: "If true (the default), run silently in the background without focusing Chrome; pass false so Chrome focuses + the tab activates and the user can watch." }),
1447
- ),
1449
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1448
1450
  host: Type.Optional(Type.String()),
1449
1451
  port: Type.Optional(Type.Number()),
1450
1452
  }),
1451
1453
  async execute(_id, params, signal): Promise<ToolTextResult> {
1452
1454
  try {
1453
- const inspect = await authorizedBridgeSend("page.inspect", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1455
+ const inspect = await authorizedBridgeSend("page.inspect", params, DEFAULT_TIMEOUT_MS, signal);
1454
1456
  return { content: [{ type: "text", text: formatChromeInspect(inspect) }], details: { inspect } };
1455
1457
  } catch (error) {
1456
1458
  const message = error instanceof Error ? error.message : String(error);
@@ -1460,13 +1462,13 @@ Usage rules:
1460
1462
  // but still gives useful nearby candidates instead of failing the workflow.
1461
1463
  const snapshot = await authorizedBridgeSend(
1462
1464
  "page.snapshot",
1463
- withBackground({
1465
+ {
1464
1466
  ...params,
1465
1467
  mode: "interactive",
1466
1468
  maxElements: MAX_ELEMENTS,
1467
1469
  nearUid: params.uid,
1468
1470
  query: params.selector,
1469
- }),
1471
+ },
1470
1472
  DEFAULT_TIMEOUT_MS,
1471
1473
  signal,
1472
1474
  );
@@ -1480,7 +1482,7 @@ Usage rules:
1480
1482
  name: "chrome_navigate",
1481
1483
  label: "Chrome Navigate",
1482
1484
  description:
1483
- "Navigate a Chrome tab to a URL via the companion extension. With no target, navigation goes to pi-chrome's own dedicated automation window/tab — it never replaces the user's active tab. Pass targetId/urlIncludes/titleIncludes only to act on a specific existing tab. Runs in the background by default; pass background=false to focus Chrome and activate the tab so the user can watch. Optionally waits for load completion.",
1485
+ "Navigate a Chrome tab to a URL via the companion extension. With no target, navigation goes to pi-chrome's own dedicated automation window/tab — it never replaces the user's active tab. Pass targetId/urlIncludes/titleIncludes only to act on a specific existing tab. Background mode (default) blocks explicit focus/tab activation even with background=false; /chrome background off allows foreground/watch mode. Optionally waits for load completion.",
1484
1486
  promptSnippet: "Navigate a Chrome tab in the user's existing profile.",
1485
1487
  parameters: Type.Object({
1486
1488
  url: Type.String(),
@@ -1490,14 +1492,12 @@ Usage rules:
1490
1492
  waitUntilLoad: Type.Optional(Type.Boolean({ default: true })),
1491
1493
  timeoutMs: Type.Optional(Type.Number({ default: 15_000 })),
1492
1494
  initScript: Type.Optional(Type.String({ description: "Optional JavaScript source to run in MAIN world at document_start of the next navigation. Useful for seeding localStorage, stubbing Date.now(), or defining navigator.webdriver=undefined. Requires the companion extension's webNavigation permission." })),
1493
- background: Type.Optional(
1494
- Type.Boolean({ description: "If true, navigate silently without focusing Chrome. Defaults to on (the session background setting); pass false to focus Chrome so the user can watch." }),
1495
- ),
1495
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1496
1496
  host: Type.Optional(Type.String()),
1497
1497
  port: Type.Optional(Type.Number()),
1498
1498
  }),
1499
1499
  async execute(_id, params, signal): Promise<ToolTextResult> {
1500
- const result = await authorizedBridgeSend("page.navigate", withBackground(params), (params.timeoutMs ?? 15_000) + 2_000, signal);
1500
+ const result = await authorizedBridgeSend("page.navigate", params, (params.timeoutMs ?? 15_000) + 2_000, signal);
1501
1501
  return { content: [{ type: "text", text: `Navigated to ${params.url}${params.initScript ? " (with initScript)" : ""}` }], details: { result: result as Json } };
1502
1502
  },
1503
1503
  });
@@ -1506,7 +1506,7 @@ Usage rules:
1506
1506
  name: "chrome_evaluate",
1507
1507
  label: "Chrome Evaluate",
1508
1508
  description:
1509
- "Evaluate JavaScript in an existing Chrome tab through the companion extension. Runs in the page context and returns JSON-serializable values when possible. Runs in the background by default; pass background=false to focus Chrome and activate the tab.",
1509
+ "Evaluate JavaScript in an existing Chrome tab through the companion extension. Runs in the page context and returns JSON-serializable values when possible. Background mode (default) blocks explicit focus/tab activation even with background=false; /chrome background off allows foreground/watch mode.",
1510
1510
  promptSnippet: "Evaluate JavaScript in the active Chrome tab through the companion extension.",
1511
1511
  parameters: Type.Object({
1512
1512
  expression: Type.String(),
@@ -1514,14 +1514,12 @@ Usage rules:
1514
1514
  targetId: Type.Optional(Type.String()),
1515
1515
  urlIncludes: Type.Optional(Type.String()),
1516
1516
  titleIncludes: Type.Optional(Type.String()),
1517
- background: Type.Optional(
1518
- Type.Boolean({ description: "If true, evaluate silently without focusing Chrome. Defaults to on (the session background setting); pass false to focus Chrome so the user can watch." }),
1519
- ),
1517
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1520
1518
  host: Type.Optional(Type.String()),
1521
1519
  port: Type.Optional(Type.Number()),
1522
1520
  }),
1523
1521
  async execute(_id, params, signal): Promise<ToolTextResult> {
1524
- const value = await authorizedBridgeSend("page.evaluate", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1522
+ const value = await authorizedBridgeSend("page.evaluate", params, DEFAULT_TIMEOUT_MS, signal);
1525
1523
  const text = value === undefined
1526
1524
  ? "undefined"
1527
1525
  : typeof value === "string"
@@ -1548,14 +1546,12 @@ Usage rules:
1548
1546
  targetId: Type.Optional(Type.String()),
1549
1547
  urlIncludes: Type.Optional(Type.String()),
1550
1548
  titleIncludes: Type.Optional(Type.String()),
1551
- background: Type.Optional(
1552
- Type.Boolean({ description: "If true, click silently without focusing Chrome. Defaults to on (the session background setting); pass false to focus Chrome so the user can watch." }),
1553
- ),
1549
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1554
1550
  host: Type.Optional(Type.String()),
1555
1551
  port: Type.Optional(Type.Number()),
1556
1552
  }),
1557
1553
  async execute(_id, params, signal): Promise<ToolTextResult> {
1558
- const raw = await authorizedBridgeSend("page.click", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1554
+ const raw = await authorizedBridgeSend("page.click", params, DEFAULT_TIMEOUT_MS, signal);
1559
1555
  const result = (params.includeSnapshot ? (raw as { result: unknown }).result : raw) as Json;
1560
1556
  const summary = summarizeActionResult(result);
1561
1557
  const target = params.uid ?? params.selector ?? `${params.x},${params.y}`;
@@ -1568,26 +1564,25 @@ Usage rules:
1568
1564
  name: "chrome_type",
1569
1565
  label: "Chrome Type",
1570
1566
  description:
1571
- "Focus an optional snapshot uid or CSS selector, then type text using Chrome's real keyboard input. Pass includeSnapshot=true to return a fresh snapshot after typing.",
1567
+ "Focus an optional snapshot uid or CSS selector, then type using Chrome's real input. Contenteditables use one native text insertion; other fields use key events. Set perCharacter=true for editors needing individual keydown events. Pass includeSnapshot=true to verify after typing.",
1572
1568
  promptSnippet: "Type text into Chrome, optionally focusing a snapshot uid or selector first.",
1573
1569
  parameters: Type.Object({
1574
1570
  text: Type.String(),
1575
1571
  uid: Type.Optional(Type.String({ description: "Stable element uid from chrome_snapshot." })),
1576
1572
  selector: Type.Optional(Type.String({ description: "CSS selector to focus before typing." })),
1573
+ perCharacter: Type.Optional(Type.Boolean({ default: false, description: "Send individual key events even in contenteditables. Default: one native text insertion for contenteditables; key events for other fields." })),
1577
1574
  includeSnapshot: Type.Optional(Type.Boolean({ description: "If true, include a fresh chrome_snapshot result after typing." })),
1578
1575
  maxElements: Type.Optional(Type.Number({ default: MAX_ELEMENTS, description: "Max elements in the included snapshot." })),
1579
1576
  pressEnter: Type.Optional(Type.Boolean()),
1580
1577
  targetId: Type.Optional(Type.String()),
1581
1578
  urlIncludes: Type.Optional(Type.String()),
1582
1579
  titleIncludes: Type.Optional(Type.String()),
1583
- background: Type.Optional(
1584
- Type.Boolean({ description: "If true, type silently without focusing Chrome. Defaults to on (the session background setting); pass false to focus Chrome so the user can watch." }),
1585
- ),
1580
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1586
1581
  host: Type.Optional(Type.String()),
1587
1582
  port: Type.Optional(Type.Number()),
1588
1583
  }),
1589
1584
  async execute(_id, params, signal): Promise<ToolTextResult> {
1590
- const raw = await authorizedBridgeSend("page.type", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1585
+ const raw = await authorizedBridgeSend("page.type", params, DEFAULT_TIMEOUT_MS, signal);
1591
1586
  const result = (params.includeSnapshot ? (raw as { result: unknown }).result : raw) as Json;
1592
1587
  const summary = summarizeActionResult(result);
1593
1588
  const into = params.uid || params.selector ? ` into ${params.uid ?? params.selector}` : "";
@@ -1601,12 +1596,13 @@ Usage rules:
1601
1596
  name: "chrome_fill",
1602
1597
  label: "Chrome Fill",
1603
1598
  description:
1604
- "Set the full value of a text input, textarea, or contenteditable element using Chrome click/select/delete/type input. Accepts a snapshot uid or CSS selector. Pass includeSnapshot=true to verify after filling.",
1599
+ "Set the full value of a text input, textarea, or contenteditable using Chrome click/select/delete/type input. Contenteditables use one native text insertion; perCharacter=true retains individual keydown events. Accepts a snapshot uid or CSS selector. Pass includeSnapshot=true to verify after filling.",
1605
1600
  promptSnippet: "Fill a Chrome form field by snapshot uid or selector, optionally returning a fresh snapshot.",
1606
1601
  parameters: Type.Object({
1607
1602
  text: Type.String(),
1608
1603
  uid: Type.Optional(Type.String({ description: "Stable element uid from chrome_snapshot." })),
1609
1604
  selector: Type.Optional(Type.String({ description: "CSS selector to fill if uid is omitted." })),
1605
+ perCharacter: Type.Optional(Type.Boolean({ default: false, description: "Send individual key events even in contenteditables. Default: one native text insertion for contenteditables; key events for other fields." })),
1610
1606
  submit: Type.Optional(Type.Boolean({ description: "If true, press Enter after filling." })),
1611
1607
  domFallback: Type.Optional(Type.Boolean({ description: "If true (default), fall back to DOM value-setting if Chrome's CDP input path is blocked by another extension overlay or debugger failure." })),
1612
1608
  includeSnapshot: Type.Optional(Type.Boolean({ description: "If true, include a fresh chrome_snapshot result after filling." })),
@@ -1614,14 +1610,12 @@ Usage rules:
1614
1610
  targetId: Type.Optional(Type.String()),
1615
1611
  urlIncludes: Type.Optional(Type.String()),
1616
1612
  titleIncludes: Type.Optional(Type.String()),
1617
- background: Type.Optional(
1618
- Type.Boolean({ description: "If true, fill silently without focusing Chrome. Defaults to on (the session background setting); pass false to focus Chrome so the user can watch." }),
1619
- ),
1613
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1620
1614
  host: Type.Optional(Type.String()),
1621
1615
  port: Type.Optional(Type.Number()),
1622
1616
  }),
1623
1617
  async execute(_id, params, signal): Promise<ToolTextResult> {
1624
- const raw = await authorizedBridgeSend("page.fill", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1618
+ const raw = await authorizedBridgeSend("page.fill", params, DEFAULT_TIMEOUT_MS, signal);
1625
1619
  const result = (params.includeSnapshot ? (raw as { result: unknown }).result : raw) as Json;
1626
1620
  const summary = summarizeActionResult(result);
1627
1621
  const into = params.uid || params.selector ? ` into ${params.uid ?? params.selector}` : "";
@@ -1635,7 +1629,7 @@ Usage rules:
1635
1629
  name: "chrome_key",
1636
1630
  label: "Chrome Key",
1637
1631
  description:
1638
- "Send a keyboard key to an existing Chrome tab (Enter, Escape, Tab, Backspace, Delete, ArrowUp/Down/Left/Right, or one character). Runs in the background by default; pass background=false to focus Chrome and activate the tab so the user can watch. Pass includeSnapshot=true to verify after the keypress.",
1632
+ "Send a keyboard key to an existing Chrome tab (Enter, Escape, Tab, Backspace, Delete, ArrowUp/Down/Left/Right, or one character). Background mode (default) blocks explicit focus/tab activation even with background=false; /chrome background off allows foreground/watch mode. Pass includeSnapshot=true to verify after the keypress.",
1639
1633
  promptSnippet: "Press keys in Chrome through the companion extension.",
1640
1634
  parameters: Type.Object({
1641
1635
  key: Type.String(),
@@ -1644,20 +1638,18 @@ Usage rules:
1644
1638
  ctrlKey: Type.Optional(Type.Boolean()),
1645
1639
  altKey: Type.Optional(Type.Boolean()),
1646
1640
  metaKey: Type.Optional(Type.Boolean()),
1647
- }, { description: "Modifier keys to hold while pressing the key (chord)." })),
1641
+ }, { description: "Modifier keys to hold while pressing the key. Shift alone types the shifted US-layout character (a → A, 1 → !); Ctrl/Meta/Alt chords do not insert literal text." })),
1648
1642
  includeSnapshot: Type.Optional(Type.Boolean({ description: "If true, include a fresh chrome_snapshot result after the keypress." })),
1649
1643
  maxElements: Type.Optional(Type.Number({ default: MAX_ELEMENTS, description: "Max elements in the included snapshot." })),
1650
1644
  targetId: Type.Optional(Type.String()),
1651
1645
  urlIncludes: Type.Optional(Type.String()),
1652
1646
  titleIncludes: Type.Optional(Type.String()),
1653
- background: Type.Optional(
1654
- Type.Boolean({ description: "If true, send the key silently without focusing Chrome. Defaults to on (the session background setting); pass false to focus Chrome so the user can watch." }),
1655
- ),
1647
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1656
1648
  host: Type.Optional(Type.String()),
1657
1649
  port: Type.Optional(Type.Number()),
1658
1650
  }),
1659
1651
  async execute(_id, params, signal): Promise<ToolTextResult> {
1660
- const raw = await authorizedBridgeSend("page.key", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1652
+ const raw = await authorizedBridgeSend("page.key", params, DEFAULT_TIMEOUT_MS, signal);
1661
1653
  const result = (params.includeSnapshot ? (raw as { result: unknown }).result : raw) as Json;
1662
1654
  const summary = summarizeActionResult(result);
1663
1655
  const base = `Pressed ${params.key}.`;
@@ -1699,12 +1691,12 @@ Usage rules:
1699
1691
  targetId: Type.Optional(Type.String()),
1700
1692
  urlIncludes: Type.Optional(Type.String()),
1701
1693
  titleIncludes: Type.Optional(Type.String()),
1702
- background: Type.Optional(Type.Boolean({ description: "If true, run silently without focusing Chrome. Defaults to on (the session background setting); pass false to focus Chrome so the user can watch." })),
1694
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1703
1695
  host: Type.Optional(Type.String()),
1704
1696
  port: Type.Optional(Type.Number()),
1705
1697
  }),
1706
1698
  async execute(_id, params, signal): Promise<ToolTextResult> {
1707
- const result = await authorizedBridgeSend("page.console.list", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1699
+ const result = await authorizedBridgeSend("page.console.list", params, DEFAULT_TIMEOUT_MS, signal);
1708
1700
  return { content: [{ type: "text", text: truncateText(safeJson(result)) }], details: { result: result as Json } };
1709
1701
  },
1710
1702
  });
@@ -1721,12 +1713,12 @@ Usage rules:
1721
1713
  targetId: Type.Optional(Type.String()),
1722
1714
  urlIncludes: Type.Optional(Type.String()),
1723
1715
  titleIncludes: Type.Optional(Type.String()),
1724
- background: Type.Optional(Type.Boolean({ description: "If true, run silently without focusing Chrome. Defaults to on (the session background setting); pass false to focus Chrome so the user can watch." })),
1716
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1725
1717
  host: Type.Optional(Type.String()),
1726
1718
  port: Type.Optional(Type.Number()),
1727
1719
  }),
1728
1720
  async execute(_id, params, signal): Promise<ToolTextResult> {
1729
- const result = await authorizedBridgeSend("page.network.list", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1721
+ const result = await authorizedBridgeSend("page.network.list", params, DEFAULT_TIMEOUT_MS, signal);
1730
1722
  return { content: [{ type: "text", text: truncateText(safeJson(result)) }], details: { result: result as Json } };
1731
1723
  },
1732
1724
  });
@@ -1741,12 +1733,12 @@ Usage rules:
1741
1733
  targetId: Type.Optional(Type.String()),
1742
1734
  urlIncludes: Type.Optional(Type.String()),
1743
1735
  titleIncludes: Type.Optional(Type.String()),
1744
- background: Type.Optional(Type.Boolean({ description: "If true, run silently without focusing Chrome. Defaults to on (the session background setting); pass false to focus Chrome so the user can watch." })),
1736
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1745
1737
  host: Type.Optional(Type.String()),
1746
1738
  port: Type.Optional(Type.Number()),
1747
1739
  }),
1748
1740
  async execute(_id, params, signal): Promise<ToolTextResult> {
1749
- const result = await authorizedBridgeSend("page.network.get", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1741
+ const result = await authorizedBridgeSend("page.network.get", params, DEFAULT_TIMEOUT_MS, signal);
1750
1742
  return { content: [{ type: "text", text: truncateText(safeJson(result)) }], details: { result: result as Json } };
1751
1743
  },
1752
1744
  });
@@ -1755,19 +1747,17 @@ Usage rules:
1755
1747
  name: "chrome_screenshot",
1756
1748
  label: "Chrome Screenshot",
1757
1749
  description:
1758
- "Capture a screenshot of an existing Chrome tab via the companion extension and save it to disk. Chrome's extension screenshot API requires the target tab to be the active tab in its window. Runs in the background by default (the tab is briefly activated within its window for the capture, then the previous active tab is restored); pass background=false to focus Chrome so the user can watch.",
1750
+ "Capture a screenshot of a Chrome tab via CDP and save it to disk without activating background tabs. Requires debugger access; failures never fall back to activating a tab. Background mode (default) ignores background=false; /chrome background off allows foreground/watch mode.",
1759
1751
  promptSnippet: "Capture Chrome screenshots and save them under .pi/chrome-screenshots by default.",
1760
1752
  parameters: Type.Object({
1761
1753
  path: Type.Optional(Type.String({ description: "Output path. Defaults to .pi/chrome-screenshots/<timestamp>.<format>." })),
1762
1754
  format: Type.Optional(StringEnum(imageFormatValues)),
1763
- quality: Type.Optional(Type.Number({ description: "JPEG quality 0-100." })),
1764
- fullPage: Type.Optional(Type.Boolean({ description: "Not supported by the extension bridge yet; viewport screenshots are captured." })),
1755
+ quality: Type.Optional(Type.Number({ minimum: 0, maximum: 100, description: "JPEG quality 0-100." })),
1756
+ fullPage: Type.Optional(Type.Boolean({ description: "Capture full-page tiles plus a JSON manifest. Temporarily scrolls the target page; does not activate background tabs." })),
1765
1757
  targetId: Type.Optional(Type.String()),
1766
1758
  urlIncludes: Type.Optional(Type.String()),
1767
1759
  titleIncludes: Type.Optional(Type.String()),
1768
- background: Type.Optional(
1769
- Type.Boolean({ description: "If true (the default), capture silently without focusing the Chrome window (the target tab is briefly activated within its window for the capture, then restored); pass false to focus Chrome." }),
1770
- ),
1760
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1771
1761
  host: Type.Optional(Type.String()),
1772
1762
  port: Type.Optional(Type.Number()),
1773
1763
  }),
@@ -1776,8 +1766,9 @@ Usage rules:
1776
1766
  const cwd = workspaceCwd(ctx);
1777
1767
  const defaultPath = join(cwd, ".pi", "chrome-screenshots", `${new Date().toISOString().replace(/[:.]/g, "-")}.${format}`);
1778
1768
  const outputPath = params.path ? resolve(cwd, params.path) : defaultPath;
1779
- const result = (await authorizedBridgeSend("page.screenshot", withBackground(params), params.fullPage ? 120_000 : DEFAULT_TIMEOUT_MS, signal)) as {
1769
+ const result = (await authorizedBridgeSend("page.screenshot", params, params.fullPage ? 120_000 : DEFAULT_TIMEOUT_MS, signal)) as {
1780
1770
  dataUrl?: string;
1771
+ method?: string;
1781
1772
  tab?: unknown;
1782
1773
  fullPage?: boolean;
1783
1774
  dimensions?: { width: number; height: number; viewportHeight: number; dpr: number };
@@ -1800,13 +1791,13 @@ Usage rules:
1800
1791
  await writeFile(outputPath + ".json", JSON.stringify({ width, height, viewportHeight, dpr, tiles: manifest }, null, 2));
1801
1792
  return {
1802
1793
  content: [{ type: "text", text: `Saved ${result.tiles.length} full-page tile(s) for ${width}×${height}px page. Manifest: ${outputPath}.json` }],
1803
- details: { manifest: outputPath + ".json", tiles: manifest, dimensions: result.dimensions, tab: result.tab } as unknown as Record<string, unknown>,
1794
+ details: { manifest: outputPath + ".json", tiles: manifest, dimensions: result.dimensions, tab: result.tab, method: result.method } as unknown as Record<string, unknown>,
1804
1795
  };
1805
1796
  }
1806
1797
  if (!result.dataUrl) throw new Error("Screenshot returned no dataUrl");
1807
1798
  const base64 = result.dataUrl.replace(/^data:image\/(?:png|jpeg);base64,/, "");
1808
1799
  await writeFile(outputPath, Buffer.from(base64, "base64"));
1809
- return { content: [{ type: "text", text: `Saved Chrome screenshot to ${outputPath}` }], details: { path: outputPath, format, tab: result.tab } };
1800
+ return { content: [{ type: "text", text: `Saved Chrome screenshot to ${outputPath}` }], details: { path: outputPath, format, tab: result.tab, method: result.method } };
1810
1801
  },
1811
1802
  });
1812
1803
 
@@ -1823,10 +1814,10 @@ Usage rules:
1823
1814
  targetId: Type.Optional(Type.String()),
1824
1815
  urlIncludes: Type.Optional(Type.String()),
1825
1816
  titleIncludes: Type.Optional(Type.String()),
1826
- background: Type.Optional(Type.Boolean()),
1817
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1827
1818
  }),
1828
1819
  async execute(_id, params, signal): Promise<ToolTextResult> {
1829
- const result = await authorizedBridgeSend("page.hover", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1820
+ const result = await authorizedBridgeSend("page.hover", params, DEFAULT_TIMEOUT_MS, signal);
1830
1821
  return { content: [{ type: "text", text: `Hovered ${params.uid ?? params.selector ?? `${params.x},${params.y}`}` }], details: { result: result as Json } };
1831
1822
  },
1832
1823
  });
@@ -1849,10 +1840,10 @@ Usage rules:
1849
1840
  targetId: Type.Optional(Type.String()),
1850
1841
  urlIncludes: Type.Optional(Type.String()),
1851
1842
  titleIncludes: Type.Optional(Type.String()),
1852
- background: Type.Optional(Type.Boolean()),
1843
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1853
1844
  }),
1854
1845
  async execute(_id, params, signal): Promise<ToolTextResult> {
1855
- const result = await authorizedBridgeSend("page.drag", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1846
+ const result = await authorizedBridgeSend("page.drag", params, DEFAULT_TIMEOUT_MS, signal);
1856
1847
  return { content: [{ type: "text", text: `Dragged from ${params.fromUid ?? params.fromSelector} to ${params.toUid ?? params.toSelector}` }], details: { result: result as Json } };
1857
1848
  },
1858
1849
  });
@@ -1871,10 +1862,10 @@ Usage rules:
1871
1862
  targetId: Type.Optional(Type.String()),
1872
1863
  urlIncludes: Type.Optional(Type.String()),
1873
1864
  titleIncludes: Type.Optional(Type.String()),
1874
- background: Type.Optional(Type.Boolean()),
1865
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1875
1866
  }),
1876
1867
  async execute(_id, params, signal): Promise<ToolTextResult> {
1877
- const result = await authorizedBridgeSend("page.tap", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1868
+ const result = await authorizedBridgeSend("page.tap", params, DEFAULT_TIMEOUT_MS, signal);
1878
1869
  const target = params.uid ?? params.selector ?? `${params.x},${params.y}`;
1879
1870
  return { content: [{ type: "text", text: `Tapped ${target} (touch)` }], details: { result: result as Json } };
1880
1871
  },
@@ -1894,10 +1885,10 @@ Usage rules:
1894
1885
  targetId: Type.Optional(Type.String()),
1895
1886
  urlIncludes: Type.Optional(Type.String()),
1896
1887
  titleIncludes: Type.Optional(Type.String()),
1897
- background: Type.Optional(Type.Boolean()),
1888
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1898
1889
  }),
1899
1890
  async execute(_id, params, signal): Promise<ToolTextResult> {
1900
- const result = await authorizedBridgeSend("page.scroll", withBackground(params), DEFAULT_TIMEOUT_MS, signal);
1891
+ const result = await authorizedBridgeSend("page.scroll", params, DEFAULT_TIMEOUT_MS, signal);
1901
1892
  return { content: [{ type: "text", text: `Scrolled dy=${params.deltaY ?? 0} dx=${params.deltaX ?? 0}` }], details: { result: result as Json } };
1902
1893
  },
1903
1894
  });
@@ -1914,12 +1905,12 @@ Usage rules:
1914
1905
  targetId: Type.Optional(Type.String()),
1915
1906
  urlIncludes: Type.Optional(Type.String()),
1916
1907
  titleIncludes: Type.Optional(Type.String()),
1917
- background: Type.Optional(Type.Boolean()),
1908
+ background: Type.Optional(Type.Boolean({ description: BACKGROUND_PARAM_DESCRIPTION })),
1918
1909
  }),
1919
1910
  async execute(_id, params, signal, _onUpdate, ctx): Promise<ToolTextResult> {
1920
1911
  const cwd = workspaceCwd(ctx);
1921
1912
  const paths = params.paths.map((p) => resolve(cwd, p));
1922
- const result = await authorizedBridgeSend("page.upload", withBackground({ ...params, paths }), DEFAULT_TIMEOUT_MS, signal);
1913
+ const result = await authorizedBridgeSend("page.upload", { ...params, paths }, DEFAULT_TIMEOUT_MS, signal);
1923
1914
  return { content: [{ type: "text", text: `Uploaded ${paths.length} file(s) to ${params.uid ?? params.selector}` }], details: { result: result as Json } };
1924
1915
  },
1925
1916
  });
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "pi-chrome",
3
- "version": "0.15.47",
3
+ "version": "0.15.49",
4
4
  "scripts": {
5
- "test": "node test-suite/unit/csp-eval.test.mjs && node test-suite/unit/automation-target.test.mjs && node test-suite/unit/session-cleanup.test.mjs",
5
+ "test": "node test-suite/unit/csp-eval.test.mjs && node test-suite/unit/automation-target.test.mjs && node test-suite/unit/session-cleanup.test.mjs && node test-suite/unit/background-policy.test.mjs && node test-suite/unit/input-reliability.test.mjs",
6
6
  "version": "node scripts/sync-manifest-version.js",
7
7
  "prepublishOnly": "node scripts/sync-manifest-version.js"
8
8
  },