@agent-native/core 0.173.0 → 0.173.1-nightly-20260825123905

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 (78) hide show
  1. package/corpus/README.md +1 -1
  2. package/corpus/templates/analytics/.agents/skills/actions/SKILL.md +1 -0
  3. package/corpus/templates/assets/.agents/skills/actions/SKILL.md +1 -0
  4. package/corpus/templates/brain/.agents/skills/actions/SKILL.md +1 -0
  5. package/corpus/templates/calendar/.agents/skills/actions/SKILL.md +1 -0
  6. package/corpus/templates/chat/.agents/skills/actions/SKILL.md +1 -0
  7. package/corpus/templates/clips/.agents/skills/actions/SKILL.md +1 -0
  8. package/corpus/templates/clips/app/components/player/share-dialog.tsx +36 -2
  9. package/corpus/templates/clips/app/components/player/signed-out-share-actions.tsx +13 -3
  10. package/corpus/templates/clips/app/i18n/en-US.ts +2 -0
  11. package/corpus/templates/clips/app/lib/time-param.ts +40 -0
  12. package/corpus/templates/clips/app/routes/embed.$shareId.tsx +2 -35
  13. package/corpus/templates/clips/app/routes/r.$recordingId.tsx +58 -63
  14. package/corpus/templates/clips/app/routes/share.$shareId.tsx +16 -9
  15. package/corpus/templates/clips/changelog/2026-08-24-chrome-recording-recovers-a-dropped-mic-grant.md +6 -0
  16. package/corpus/templates/clips/chrome-extension/src/background.ts +138 -29
  17. package/corpus/templates/clips/chrome-extension/src/media-permission.ts +163 -0
  18. package/corpus/templates/clips/chrome-extension/src/offscreen.ts +43 -11
  19. package/corpus/templates/clips/chrome-extension/src/options.html +8 -6
  20. package/corpus/templates/clips/chrome-extension/src/permission.ts +4 -17
  21. package/corpus/templates/clips/chrome-extension/src/popup.ts +27 -39
  22. package/corpus/templates/clips/chrome-extension/src/styles.css +23 -0
  23. package/corpus/templates/clips/shared/share-attribution.ts +4 -2
  24. package/corpus/templates/clips/shared/share-dashboard-redirect.ts +1 -1
  25. package/corpus/templates/content/.agents/skills/actions/SKILL.md +1 -0
  26. package/corpus/templates/crm/.agents/skills/actions/SKILL.md +1 -0
  27. package/corpus/templates/design/.agents/skills/actions/SKILL.md +1 -0
  28. package/corpus/templates/design/.generated/bridge/editor-chrome.generated.ts +166 -109
  29. package/corpus/templates/design/app/components/design/CanvasContextMenu.tsx +82 -52
  30. package/corpus/templates/design/app/components/design/LayersPanel.tsx +17 -12
  31. package/corpus/templates/design/app/components/design/bridge/editor-chrome.bridge.ts +242 -137
  32. package/corpus/templates/design/app/components/design/edit-panel/component-section.tsx +6 -1
  33. package/corpus/templates/design/app/components/design/edit-panel/position-layout-properties.tsx +11 -6
  34. package/corpus/templates/design/app/components/design/edit-panel/typography-properties.tsx +7 -2
  35. package/corpus/templates/design/app/components/design/editor/DesignBottomToolbar.tsx +4 -1
  36. package/corpus/templates/design/app/components/design/keyboard-shortcuts.ts +27 -4
  37. package/corpus/templates/design/app/hooks/use-shortcut-label.ts +21 -0
  38. package/corpus/templates/design/app/hooks/useDesignHotkeys.ts +38 -16
  39. package/corpus/templates/design/app/pages/DesignEditor.tsx +17 -10
  40. package/corpus/templates/design/app/pages/design-editor/auto-layout-suggestion.ts +5 -3
  41. package/corpus/templates/design/app/pages/design-editor/commands/group-selection.ts +1 -1
  42. package/corpus/templates/design/app/pages/design-editor/commands/paste-to-replace.ts +28 -1
  43. package/corpus/templates/design/app/pages/design-editor/layout-operations.ts +33 -4
  44. package/corpus/templates/design/changelog/2026-08-24-adding-auto-layout-to-a-stack-of-full-width-rows-now-picks.md +6 -0
  45. package/corpus/templates/design/changelog/2026-08-24-dragging-an-element-no-longer-fades-it-or-changes-its-stacki.md +6 -0
  46. package/corpus/templates/design/changelog/2026-08-24-dropping-an-element-into-a-frame-that-already-has-content.md +6 -0
  47. package/corpus/templates/design/shared/code-layer.ts +35 -4
  48. package/corpus/templates/dispatch/.agents/skills/actions/SKILL.md +1 -0
  49. package/corpus/templates/factory/.agents/skills/actions/SKILL.md +1 -0
  50. package/corpus/templates/forms/.agents/skills/actions/SKILL.md +1 -0
  51. package/corpus/templates/mail/.agents/skills/actions/SKILL.md +1 -0
  52. package/corpus/templates/plan/.agents/skills/actions/SKILL.md +1 -0
  53. package/corpus/templates/slides/.agents/skills/actions/SKILL.md +1 -0
  54. package/corpus/templates/tasks/.agents/skills/actions/SKILL.md +1 -0
  55. package/dist/agent/engine/anthropic-engine.js +13 -3
  56. package/dist/agent/engine/types.d.ts +15 -0
  57. package/dist/collab/awareness.d.ts +2 -2
  58. package/dist/file-upload/actions/upload-image.d.ts +1 -1
  59. package/dist/observability/posthog-ai.d.ts +25 -0
  60. package/dist/observability/posthog-ai.js +122 -0
  61. package/dist/observability/routes.d.ts +3 -3
  62. package/dist/observability/traces.js +30 -5
  63. package/dist/progress/routes.d.ts +1 -1
  64. package/dist/resources/handlers.d.ts +1 -1
  65. package/dist/secrets/routes.d.ts +6 -6
  66. package/dist/server/realtime-token.d.ts +1 -1
  67. package/dist/server/transcribe-voice.d.ts +1 -1
  68. package/dist/templates/chat/.agents/skills/actions/SKILL.md +1 -0
  69. package/dist/templates/default/.agents/skills/actions/SKILL.md +1 -0
  70. package/dist/templates/headless/.agents/skills/actions/SKILL.md +1 -0
  71. package/dist/templates/workspace-core/.agents/skills/actions/SKILL.md +1 -0
  72. package/dist/usage/store.d.ts +14 -2
  73. package/dist/usage/store.js +38 -10
  74. package/package.json +3 -3
  75. package/src/templates/chat/.agents/skills/actions/SKILL.md +1 -0
  76. package/src/templates/default/.agents/skills/actions/SKILL.md +1 -0
  77. package/src/templates/headless/.agents/skills/actions/SKILL.md +1 -0
  78. package/src/templates/workspace-core/.agents/skills/actions/SKILL.md +1 -0
@@ -85,6 +85,7 @@ import { usePlayerShortcuts } from "@/hooks/use-player-shortcuts";
85
85
  import { useViewTracking } from "@/hooks/use-view-tracking";
86
86
  import { parsePlaybackSpeed } from "@/lib/playback-speed";
87
87
  import { isStorageSetupFailureReason } from "@/lib/storage-failures";
88
+ import { parseTimeParam, resolveStartMs } from "@/lib/time-param";
88
89
 
89
90
  import { getDb, schema } from "../../server/db";
90
91
  import { resolvePlayerThumbnailUrl } from "../../server/lib/player-thumbnail-url";
@@ -386,6 +387,8 @@ export default function ShareRoute() {
386
387
  const { shareId } = useParams<{ shareId: string }>();
387
388
  const navigate = useNavigate();
388
389
  const [searchParams] = useSearchParams();
390
+ const startAt = searchParams.get("at");
391
+ const startMs = useMemo(() => parseTimeParam(startAt), [startAt]);
389
392
 
390
393
  // Viral attribution: read the `ref`/`via` the visitor arrived on (the tagged
391
394
  // share link) so we can fire funnel events and forward attribution into the
@@ -462,7 +465,7 @@ export default function ShareRoute() {
462
465
  } catch {}
463
466
  }, [shareId]);
464
467
  const [pwError, setPwError] = useState<string | null>(null);
465
- const [currentMs, setCurrentMs] = useState(0);
468
+ const [currentMs, setCurrentMs] = useState(startMs);
466
469
  const [commentOpen, setCommentOpen] = useState(false);
467
470
  const [commentAtMs, setCommentAtMs] = useState(0);
468
471
  const [commentDraft, setCommentDraft] = useState("");
@@ -514,9 +517,9 @@ export default function ShareRoute() {
514
517
  const shareReturnTo = useMemo(() => {
515
518
  const path = `/share/${encodeURIComponent(recordingId)}`;
516
519
  if (typeof window === "undefined") return path;
517
- const query = buildShareContinuationQuery(attribution);
520
+ const query = buildShareContinuationQuery(attribution, startAt);
518
521
  return query ? `${path}?${query}` : path;
519
- }, [attribution, recordingId]);
522
+ }, [attribution, recordingId, startAt]);
520
523
  const signInHref = buildSignInReturnHref({ returnTo: shareReturnTo });
521
524
 
522
525
  const submitAccessRequest = useCallback(
@@ -652,6 +655,7 @@ export default function ShareRoute() {
652
655
  });
653
656
 
654
657
  const recording = dataQ.data?.data?.recording;
658
+ const playbackMs = resolveStartMs(currentMs, recording?.durationMs);
655
659
  const verificationPending = recording?.verificationPending === true;
656
660
  const comments = dataQ.data?.data?.comments ?? [];
657
661
  const reactions = dataQ.data?.data?.reactions ?? [];
@@ -1168,6 +1172,7 @@ export default function ShareRoute() {
1168
1172
  {session ? null : (
1169
1173
  <SignedOutShareActions
1170
1174
  recordingId={recording.id}
1175
+ startAt={startAt}
1171
1176
  onCtaClick={fireShareCtaClick}
1172
1177
  />
1173
1178
  )}
@@ -1181,6 +1186,7 @@ export default function ShareRoute() {
1181
1186
  thumbnailUrl={recording.thumbnailUrl}
1182
1187
  animatedThumbnailUrl={recording.animatedThumbnailUrl}
1183
1188
  isLoomRecording={isLoomEmbedBacked}
1189
+ currentMs={playbackMs}
1184
1190
  hasPassword={Boolean(recording.hasPassword)}
1185
1191
  viewerReshareOnly={viewerReshareOnly}
1186
1192
  >
@@ -1248,6 +1254,7 @@ export default function ShareRoute() {
1248
1254
  videoFormat={recording.videoFormat}
1249
1255
  embedProvider={isLoomEmbedBacked ? "loom" : null}
1250
1256
  durationMs={recording.durationMs}
1257
+ startMs={resolveStartMs(startMs, recording.durationMs)}
1251
1258
  persistPlaybackPosition={Boolean(session)}
1252
1259
  editsJson={recording.editsJson}
1253
1260
  thumbnailUrl={recording.thumbnailUrl}
@@ -1285,7 +1292,7 @@ export default function ShareRoute() {
1285
1292
  liveCt >= 0 &&
1286
1293
  liveCt < 1e7
1287
1294
  ? Math.floor(liveCt * 1000)
1288
- : currentMs;
1295
+ : playbackMs;
1289
1296
  setCommentAtMs(liveMs);
1290
1297
  setCommentOpen(true);
1291
1298
  }
@@ -1311,7 +1318,7 @@ export default function ShareRoute() {
1311
1318
  liveCt >= 0 &&
1312
1319
  liveCt < 1e7
1313
1320
  ? Math.floor(liveCt * 1000)
1314
- : currentMs;
1321
+ : playbackMs;
1315
1322
  return fetch(
1316
1323
  agentNativePath(
1317
1324
  "/_agent-native/actions/react-to-recording",
@@ -1389,7 +1396,7 @@ export default function ShareRoute() {
1389
1396
  return;
1390
1397
  }
1391
1398
  const liveMs =
1392
- playerRef.current?.getCurrentOriginalMs() ?? currentMs;
1399
+ playerRef.current?.getCurrentOriginalMs() ?? playbackMs;
1393
1400
  setCommentAtMs(liveMs);
1394
1401
  setCommentOpen(true);
1395
1402
  }}
@@ -1406,7 +1413,7 @@ export default function ShareRoute() {
1406
1413
  }
1407
1414
  tracking.reportReaction(emoji);
1408
1415
  const liveMs =
1409
- playerRef.current?.getCurrentOriginalMs() ?? currentMs;
1416
+ playerRef.current?.getCurrentOriginalMs() ?? playbackMs;
1410
1417
  return fetch(
1411
1418
  agentNativePath(
1412
1419
  "/_agent-native/actions/react-to-recording",
@@ -1517,7 +1524,7 @@ export default function ShareRoute() {
1517
1524
  segments={transcriptSegments}
1518
1525
  fullText={transcriptFullText}
1519
1526
  durationMs={recording.durationMs}
1520
- currentMs={currentMs}
1527
+ currentMs={playbackMs}
1521
1528
  onSeek={(ms) => playerRef.current?.seek(ms)}
1522
1529
  status={transcriptStatus}
1523
1530
  failureReason={transcriptFailureReason}
@@ -1531,7 +1538,7 @@ export default function ShareRoute() {
1531
1538
  <CommentsPanel
1532
1539
  recordingId={recording.id}
1533
1540
  comments={comments}
1534
- currentMs={currentMs}
1541
+ currentMs={playbackMs}
1535
1542
  currentUserEmail={session?.email}
1536
1543
  enableComments={recording.enableComments}
1537
1544
  canComment={viewerCanComment}
@@ -0,0 +1,6 @@
1
+ ---
2
+ type: fixed
3
+ date: 2026-08-24
4
+ ---
5
+
6
+ Recording from the Chrome extension no longer dead-ends on "Permission dismissed" when Chrome has quietly dropped the extension's microphone or camera access: Clips now reopens the permission page and starts the recording once you allow it again.
@@ -1,5 +1,12 @@
1
1
  import { buildRecordingShareUrl } from "@shared/recording-link";
2
2
 
3
+ import {
4
+ MediaPermissionRequiredError,
5
+ mediaPermissionErrorFromResponse,
6
+ mediaPermissionRequirements,
7
+ permissionPageUrl,
8
+ writeCachedMediaPermission,
9
+ } from "./media-permission";
3
10
  import {
4
11
  restartUploadModeFromResponse,
5
12
  restartUploadResetBody,
@@ -1060,23 +1067,58 @@ async function ensureOffscreenDocument(): Promise<void> {
1060
1067
  });
1061
1068
  }
1062
1069
 
1070
+ type OffscreenErrorResponse = {
1071
+ error?: string;
1072
+ errorCode?: string;
1073
+ errorDevice?: string;
1074
+ };
1075
+
1076
+ // The offscreen document can only answer with plain data, so rebuild the typed
1077
+ // error here — callers branch on the class, not on a message string.
1078
+ function offscreenError(response: OffscreenErrorResponse): Error {
1079
+ return (
1080
+ mediaPermissionErrorFromResponse(response) ?? new Error(response.error)
1081
+ );
1082
+ }
1083
+
1063
1084
  function sendOffscreenMessage<T>(message: Record<string, unknown>): Promise<T> {
1064
1085
  return new Promise((resolve, reject) => {
1065
- chrome.runtime.sendMessage(message, (response: T & { error?: string }) => {
1066
- const error = chromeLastError();
1067
- if (error) {
1068
- reject(error);
1069
- return;
1070
- }
1071
- if (response && typeof response.error === "string") {
1072
- reject(new Error(response.error));
1073
- return;
1074
- }
1075
- resolve(response);
1076
- });
1086
+ chrome.runtime.sendMessage(
1087
+ message,
1088
+ (response: T & OffscreenErrorResponse) => {
1089
+ const error = chromeLastError();
1090
+ if (error) {
1091
+ reject(error);
1092
+ return;
1093
+ }
1094
+ if (response && typeof response.error === "string") {
1095
+ reject(offscreenError(response));
1096
+ return;
1097
+ }
1098
+ resolve(response);
1099
+ },
1100
+ );
1077
1101
  });
1078
1102
  }
1079
1103
 
1104
+ // MV3 suspends an idle worker after ~30 seconds. The native picker routinely
1105
+ // stays open longer than that, and a worker that dies mid-acquire takes the
1106
+ // popup's pending sendResponse channel with it ("the message channel closed
1107
+ // before a response was received"). Any extension API call resets the idle
1108
+ // timer, so ping while the user is still choosing.
1109
+ const WORKER_KEEPALIVE_INTERVAL_MS = 20_000;
1110
+
1111
+ async function withWorkerKeepAlive<T>(run: () => Promise<T>): Promise<T> {
1112
+ const timer = setInterval(() => {
1113
+ void chrome.runtime.getPlatformInfo().catch(() => undefined);
1114
+ }, WORKER_KEEPALIVE_INTERVAL_MS);
1115
+ try {
1116
+ return await run();
1117
+ } finally {
1118
+ clearInterval(timer);
1119
+ }
1120
+ }
1121
+
1080
1122
  // The author's own dashboard. Fine to OPEN for the person who just recorded;
1081
1123
  // never the thing to hand someone else — see recordingShareUrl below.
1082
1124
  function recordingUrl(
@@ -1172,8 +1214,13 @@ async function handlePopupStart(message: PopupStartMessage) {
1172
1214
  async function startRecordingFromTab(args: {
1173
1215
  tab: ChromeTab;
1174
1216
  settings: ExtensionSettings;
1217
+ // False for the start that the permission page itself triggered: sending that
1218
+ // one back to a fresh permission tab on failure would open a new tab per
1219
+ // round, so it reports the failure instead.
1220
+ allowPermissionRecovery?: boolean;
1175
1221
  }) {
1176
1222
  const { tab, settings } = args;
1223
+ const allowPermissionRecovery = args.allowPermissionRecovery !== false;
1177
1224
  await reconcilePersistedNativeRecording();
1178
1225
  // The persisted value is authoritative (survives a worker suspension during
1179
1226
  // arming); the in-memory var is only a same-tick fast path on top of it.
@@ -1201,7 +1248,12 @@ async function startRecordingFromTab(args: {
1201
1248
  await setArmingGuard(sessionId);
1202
1249
  try {
1203
1250
  await storageSet(settings);
1204
- return await armRecording({ sessionId, tab, settings });
1251
+ return await armRecording({
1252
+ sessionId,
1253
+ tab,
1254
+ settings,
1255
+ allowPermissionRecovery,
1256
+ });
1205
1257
  } finally {
1206
1258
  if (armingNativeRecordingSessionId === sessionId) {
1207
1259
  await setArmingGuard(null);
@@ -1243,7 +1295,52 @@ async function handlePermissionStartAfterGrant() {
1243
1295
  return { ok: false, error: "The original tab is no longer available." };
1244
1296
  }
1245
1297
  await activateTab(tab);
1246
- return startRecordingFromTab({ tab, settings: pending.settings });
1298
+ return startRecordingFromTab({
1299
+ tab,
1300
+ settings: pending.settings,
1301
+ allowPermissionRecovery: false,
1302
+ });
1303
+ }
1304
+
1305
+ // The popup's gate let this start because the cached grant said the device was
1306
+ // allowed, and Chrome then refused a prompt the offscreen recorder could not
1307
+ // show. Drop the stale cache entry so the gate stops trusting it, and send the
1308
+ // user through the permission page — the only surface Chrome will prompt from —
1309
+ // with the recording queued to resume once the grant lands.
1310
+ async function recoverMissingMediaPermission(args: {
1311
+ error: MediaPermissionRequiredError;
1312
+ tab: ChromeTab;
1313
+ settings: ExtensionSettings;
1314
+ openPermissionPage: boolean;
1315
+ }): Promise<{ ok: false; error: string }> {
1316
+ const { error, tab, settings, openPermissionPage } = args;
1317
+ captureExtensionError(error, {
1318
+ tags: {
1319
+ surface: "background",
1320
+ recordingStep: "offscreen-acquire",
1321
+ permission: error.device,
1322
+ },
1323
+ });
1324
+ await writeCachedMediaPermission({ [error.device]: false });
1325
+ if (!openPermissionPage) {
1326
+ return {
1327
+ ok: false,
1328
+ error: `Chrome still has not granted Clips ${error.device} access. Allow it from Chrome's address-bar icon, then start the recording again.`,
1329
+ };
1330
+ }
1331
+ if (typeof tab.id === "number") {
1332
+ await writePendingPermissionStart({
1333
+ settings,
1334
+ targetTabId: tab.id,
1335
+ createdAtMs: Date.now(),
1336
+ });
1337
+ }
1338
+ await createTab(
1339
+ permissionPageUrl(mediaPermissionRequirements(settings), {
1340
+ startAfterGrant: typeof tab.id === "number",
1341
+ }),
1342
+ ).catch(() => undefined);
1343
+ return { ok: false, error: error.message };
1247
1344
  }
1248
1345
 
1249
1346
  // Arm a Loom-style in-page recording: show the native picker, create the row,
@@ -1254,8 +1351,9 @@ async function armRecording(args: {
1254
1351
  sessionId: string;
1255
1352
  tab: ChromeTab;
1256
1353
  settings: ExtensionSettings;
1354
+ allowPermissionRecovery: boolean;
1257
1355
  }) {
1258
- const { sessionId, tab, settings } = args;
1356
+ const { sessionId, tab, settings, allowPermissionRecovery } = args;
1259
1357
  const mode: CaptureMode =
1260
1358
  settings.captureSurface === "camera" ? "camera" : "screen";
1261
1359
  const surface: "browser" | "window" | "monitor" =
@@ -1268,20 +1366,31 @@ async function armRecording(args: {
1268
1366
 
1269
1367
  // 1) Native "Choose what to share" picker. Do this before any network round
1270
1368
  // trip so the picker stays tied to the user gesture. Throws if cancelled.
1271
- const acq = await sendOffscreenMessage<{
1272
- ok: boolean;
1273
- width: number;
1274
- height: number;
1275
- }>({
1276
- type: "CLIPS_OFFSCREEN_ACQUIRE",
1277
- sessionId,
1278
- mode,
1279
- surface,
1280
- includeMicrophone: settings.includeMicrophone,
1281
- includeCamera: settings.includeCamera,
1282
- videoDeviceId: settings.videoDeviceId,
1283
- audioDeviceId: settings.audioDeviceId,
1284
- });
1369
+ let acq: { ok: boolean; width: number; height: number };
1370
+ try {
1371
+ acq = await withWorkerKeepAlive(() =>
1372
+ sendOffscreenMessage<{ ok: boolean; width: number; height: number }>({
1373
+ type: "CLIPS_OFFSCREEN_ACQUIRE",
1374
+ sessionId,
1375
+ mode,
1376
+ surface,
1377
+ includeMicrophone: settings.includeMicrophone,
1378
+ includeCamera: settings.includeCamera,
1379
+ videoDeviceId: settings.videoDeviceId,
1380
+ audioDeviceId: settings.audioDeviceId,
1381
+ }),
1382
+ );
1383
+ } catch (err) {
1384
+ if (err instanceof MediaPermissionRequiredError) {
1385
+ return recoverMissingMediaPermission({
1386
+ error: err,
1387
+ tab,
1388
+ settings,
1389
+ openPermissionPage: allowPermissionRecovery,
1390
+ });
1391
+ }
1392
+ throw err;
1393
+ }
1285
1394
  console.log("[clips-bg] arm: acquired stream", acq);
1286
1395
 
1287
1396
  // 2) Show the countdown overlay IMMEDIATELY — before the network round-trip —
@@ -0,0 +1,163 @@
1
+ // Camera/microphone grants for the chrome-extension:// origin, and the one
2
+ // failure they produce that does not mean "the user cancelled".
3
+ //
4
+ // Chrome only shows the permission prompt for a real extension page, so the
5
+ // headless offscreen recorder can never answer one: an ungranted getUserMedia()
6
+ // there rejects immediately with NotAllowedError "Permission dismissed". The
7
+ // cached grant below is what lets a recording get that far — it can outlive the
8
+ // real grant (Chrome revokes permissions for origins it considers unused, and
9
+ // clearing site data drops them), and nothing but reinstalling the extension
10
+ // used to clear it. So a dismissal from a headless context is a missing grant to
11
+ // recover from, never a cancellation to report back as-is.
12
+
13
+ const CACHE_KEY = "clipsMediaPermission";
14
+
15
+ export const MEDIA_PERMISSION_REQUIRED_CODE = "CLIPS_MEDIA_PERMISSION_REQUIRED";
16
+
17
+ export const MEDIA_PERMISSION_DEVICES = ["camera", "microphone"] as const;
18
+
19
+ export type MediaPermissionDevice = (typeof MEDIA_PERMISSION_DEVICES)[number];
20
+
21
+ export type MediaPermissionRequirements = Record<
22
+ MediaPermissionDevice,
23
+ boolean
24
+ >;
25
+
26
+ export type CachedMediaPermission = Partial<MediaPermissionRequirements>;
27
+
28
+ export type MediaPermissionSettings = {
29
+ captureSurface: "browser" | "window" | "monitor" | "camera";
30
+ includeCamera: boolean;
31
+ includeMicrophone: boolean;
32
+ };
33
+
34
+ export function mediaPermissionRequirements(
35
+ settings: MediaPermissionSettings,
36
+ ): MediaPermissionRequirements {
37
+ return {
38
+ camera: settings.captureSurface === "camera" || settings.includeCamera,
39
+ microphone: settings.includeMicrophone,
40
+ };
41
+ }
42
+
43
+ export function mediaPermissionRequiredMessage(
44
+ device: MediaPermissionDevice,
45
+ ): string {
46
+ return `Chrome has not granted Clips ${device} access. Allow it in the tab that just opened, then start the recording again.`;
47
+ }
48
+
49
+ export function toMediaPermissionDevice(
50
+ value: unknown,
51
+ ): MediaPermissionDevice | null {
52
+ return value === "camera" || value === "microphone" ? value : null;
53
+ }
54
+
55
+ export class MediaPermissionRequiredError extends Error {
56
+ readonly code = MEDIA_PERMISSION_REQUIRED_CODE;
57
+ readonly device: MediaPermissionDevice;
58
+
59
+ constructor(device: MediaPermissionDevice, options?: { cause?: unknown }) {
60
+ super(mediaPermissionRequiredMessage(device));
61
+ this.name = "MediaPermissionRequiredError";
62
+ this.device = device;
63
+ // `cause` is set here rather than through the Error options bag: the
64
+ // extension's TS lib target predates it, and the original NotAllowedError
65
+ // is what makes the Sentry report diagnosable.
66
+ if (options && "cause" in options) {
67
+ (this as { cause?: unknown }).cause = options.cause;
68
+ }
69
+ }
70
+ }
71
+
72
+ // True only for the "Chrome would have to ask, and nobody can answer" family. A
73
+ // system-level denial (macOS/Windows privacy settings) reads the same to the
74
+ // page but cannot be fixed from the permission page, so it stays a raw failure.
75
+ export function isMediaPermissionDeniedError(error: unknown): boolean {
76
+ const text =
77
+ error instanceof Error
78
+ ? `${error.name}: ${error.message}`
79
+ : String(error ?? "");
80
+ if (/\bdenied by system\b/i.test(text)) return false;
81
+ return (
82
+ /\bNotAllowedError\b/.test(text) ||
83
+ /\bPermission (?:denied|dismissed)\b/i.test(text)
84
+ );
85
+ }
86
+
87
+ // Wraps a getUserMedia() call made where Chrome cannot prompt.
88
+ export async function requireMediaPermission<T>(
89
+ device: MediaPermissionDevice,
90
+ request: () => Promise<T>,
91
+ ): Promise<T> {
92
+ try {
93
+ return await request();
94
+ } catch (error) {
95
+ if (!isMediaPermissionDeniedError(error)) throw error;
96
+ throw new MediaPermissionRequiredError(device, { cause: error });
97
+ }
98
+ }
99
+
100
+ // Rebuilds the typed error on the receiving side of a message reply, so a
101
+ // missing grant stays branchable instead of decaying into a message string.
102
+ export function mediaPermissionErrorFromResponse(response: {
103
+ errorCode?: string;
104
+ errorDevice?: string;
105
+ }): MediaPermissionRequiredError | null {
106
+ const device = toMediaPermissionDevice(response.errorDevice);
107
+ if (response.errorCode !== MEDIA_PERMISSION_REQUIRED_CODE || !device) {
108
+ return null;
109
+ }
110
+ return new MediaPermissionRequiredError(device);
111
+ }
112
+
113
+ export function permissionPageUrl(
114
+ requirements: MediaPermissionRequirements,
115
+ options?: { startAfterGrant?: boolean },
116
+ ): string {
117
+ const url = new URL(chrome.runtime.getURL("src/permission.html"));
118
+ if (options?.startAfterGrant) url.searchParams.set("startAfterGrant", "1");
119
+ url.searchParams.set("needsCamera", String(requirements.camera));
120
+ url.searchParams.set("needsMicrophone", String(requirements.microphone));
121
+ return url.toString();
122
+ }
123
+
124
+ export function readCachedMediaPermission(): Promise<CachedMediaPermission> {
125
+ return new Promise((resolve) => {
126
+ chrome.storage.local.get(CACHE_KEY, (value) => {
127
+ const cached = value?.[CACHE_KEY];
128
+ resolve(
129
+ cached && typeof cached === "object"
130
+ ? (cached as CachedMediaPermission)
131
+ : {},
132
+ );
133
+ });
134
+ });
135
+ }
136
+
137
+ export async function writeCachedMediaPermission(
138
+ patch: CachedMediaPermission,
139
+ ): Promise<void> {
140
+ const current = await readCachedMediaPermission();
141
+ await new Promise<void>((resolve) => {
142
+ chrome.storage.local.set({ [CACHE_KEY]: { ...current, ...patch } }, () =>
143
+ resolve(),
144
+ );
145
+ });
146
+ }
147
+
148
+ // Device labels are exposed only while the origin holds a live grant, so this
149
+ // separates a cached grant Chrome still honors from one it revoked — without the
150
+ // prompt that neither the popup nor the offscreen document can show.
151
+ export async function hasGrantedDeviceLabels(
152
+ device: MediaPermissionDevice,
153
+ ): Promise<boolean> {
154
+ const kind = device === "camera" ? "videoinput" : "audioinput";
155
+ try {
156
+ const devices = await navigator.mediaDevices.enumerateDevices();
157
+ return devices.some((entry) => entry.kind === kind && Boolean(entry.label));
158
+ } catch {
159
+ // coercion-ok: unreadable is not "revoked", but the only caller treats both
160
+ // the same — it routes through the permission page either way, which recovers.
161
+ return false;
162
+ }
163
+ }
@@ -36,6 +36,10 @@ import {
36
36
  import { MAX_UPLOAD_BYTES } from "@shared/upload-limits";
37
37
 
38
38
  import { waitForAcceptedRecordingAfterFinalizeError } from "./finalize-recovery";
39
+ import {
40
+ MediaPermissionRequiredError,
41
+ requireMediaPermission,
42
+ } from "./media-permission";
39
43
  import {
40
44
  createNativeTranscriptionCapture,
41
45
  type NativeTranscriptionCapture,
@@ -1123,12 +1127,24 @@ async function acquire(message: AcquireMessage): Promise<{
1123
1127
  audio: message.audioDeviceId,
1124
1128
  });
1125
1129
 
1130
+ // Chrome cannot prompt for camera/mic in this headless document, so an
1131
+ // ungranted device rejects as a dismissal. requireMediaPermission types that
1132
+ // apart from a real cancellation; the worker sends the user to the permission
1133
+ // page instead of surfacing Chrome's "Permission dismissed" as a dead end.
1134
+ const acquireMicStream = async (): Promise<MediaStream> => {
1135
+ const audioLabel = await lookupAudioDeviceLabel(devices.audio);
1136
+ return requireMediaPermission("microphone", () =>
1137
+ getMicStream(devices.audio, audioLabel),
1138
+ );
1139
+ };
1140
+
1126
1141
  try {
1127
1142
  if (message.mode === "camera") {
1128
- cameraStream = await getCameraStream(devices.video);
1143
+ cameraStream = await requireMediaPermission("camera", () =>
1144
+ getCameraStream(devices.video),
1145
+ );
1129
1146
  if (message.includeMicrophone) {
1130
- const audioLabel = await lookupAudioDeviceLabel(devices.audio);
1131
- micStream = await getMicStream(devices.audio, audioLabel);
1147
+ micStream = await acquireMicStream();
1132
1148
  }
1133
1149
  } else {
1134
1150
  // Native "Choose what to share" picker. This is the screenshot Steve showed.
@@ -1136,8 +1152,7 @@ async function acquire(message: AcquireMessage): Promise<{
1136
1152
  displayConstraints(message.surface, message.includeMicrophone),
1137
1153
  );
1138
1154
  if (message.includeMicrophone) {
1139
- const audioLabel = await lookupAudioDeviceLabel(devices.audio);
1140
- micStream = await getMicStream(devices.audio, audioLabel);
1155
+ micStream = await acquireMicStream();
1141
1156
  }
1142
1157
  // The screen+camera face comes from the on-page bubble (captured in the
1143
1158
  // display pixels), NOT composited here: canvas/requestAnimationFrame does
@@ -1876,11 +1891,28 @@ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
1876
1891
  return false;
1877
1892
  }
1878
1893
 
1879
- void task.then(sendResponse).catch((err) =>
1880
- sendResponse({
1881
- ok: false,
1882
- error: err instanceof Error ? err.message : "Recording failed.",
1883
- }),
1884
- );
1894
+ void task.then(sendResponse).catch((err) => sendResponse(errorResponse(err)));
1885
1895
  return true;
1886
1896
  });
1897
+
1898
+ // Errors cross the message boundary as plain data, so a missing grant has to
1899
+ // carry its code with it — the worker cannot recover from a bare message.
1900
+ export function errorResponse(error: unknown): {
1901
+ ok: false;
1902
+ error: string;
1903
+ errorCode?: string;
1904
+ errorDevice?: string;
1905
+ } {
1906
+ if (error instanceof MediaPermissionRequiredError) {
1907
+ return {
1908
+ ok: false,
1909
+ error: error.message,
1910
+ errorCode: error.code,
1911
+ errorDevice: error.device,
1912
+ };
1913
+ }
1914
+ return {
1915
+ ok: false,
1916
+ error: error instanceof Error ? error.message : "Recording failed.",
1917
+ };
1918
+ }
@@ -38,12 +38,14 @@
38
38
 
39
39
  <label class="field">
40
40
  <span class="field-label">Default capture</span>
41
- <select id="capture-surface">
42
- <option value="browser">Current tab</option>
43
- <option value="window">Window</option>
44
- <option value="monitor">Screen</option>
45
- <option value="camera">Camera only</option>
46
- </select>
41
+ <span class="capture-surface-control">
42
+ <select id="capture-surface">
43
+ <option value="browser">Current tab</option>
44
+ <option value="window">Window</option>
45
+ <option value="monitor">Screen</option>
46
+ <option value="camera">Camera only</option>
47
+ </select>
48
+ </span>
47
49
  </label>
48
50
 
49
51
  <label class="toggle-row" for="include-camera">
@@ -5,6 +5,7 @@
5
5
  // the grant for the whole chrome-extension:// origin — so the offscreen recorder
6
6
  // (mic) and the camera-bubble iframe both work afterward.
7
7
 
8
+ import { writeCachedMediaPermission } from "./media-permission";
8
9
  import { captureExtensionError, initExtensionSentry } from "./sentry";
9
10
 
10
11
  initExtensionSentry("permission");
@@ -96,23 +97,9 @@ function requiredPermissionsReady(camOk: boolean, micOk: boolean): boolean {
96
97
  }
97
98
 
98
99
  function writePermissionCache(camOk: boolean, micOk: boolean): Promise<void> {
99
- return new Promise((resolve) => {
100
- chrome.storage.local.get("clipsMediaPermission", (value) => {
101
- const current =
102
- value.clipsMediaPermission &&
103
- typeof value.clipsMediaPermission === "object"
104
- ? (value.clipsMediaPermission as {
105
- camera?: boolean;
106
- microphone?: boolean;
107
- })
108
- : {};
109
- const next = {
110
- ...current,
111
- ...(shouldRequestCamera ? { camera: camOk } : {}),
112
- ...(shouldRequestMicrophone ? { microphone: micOk } : {}),
113
- };
114
- chrome.storage.local.set({ clipsMediaPermission: next }, () => resolve());
115
- });
100
+ return writeCachedMediaPermission({
101
+ ...(shouldRequestCamera ? { camera: camOk } : {}),
102
+ ...(shouldRequestMicrophone ? { microphone: micOk } : {}),
116
103
  });
117
104
  }
118
105