11import { attachRefs , type RawSnapshotNode } from '@agent-device/kernel/snapshot' ;
22import type { SnapshotOptions , SnapshotResult } from '@agent-device/contracts/interaction' ;
3+ import type { CaptureSnapshotInput , SnapshotRuntimeHost } from '@agent-device/contracts/platform' ;
4+ import type { DeviceInfo } from '@agent-device/kernel/device' ;
35import { findNodeByLabel } from './snapshot-node-label.ts' ;
46
7+ type SnapshotSurfaceOptions = NonNullable < CaptureSnapshotInput [ 'options' ] > ;
8+
9+ export function createSnapshotRuntimeHost ( ) : SnapshotRuntimeHost {
10+ return Object . freeze ( { captureSurface } ) ;
11+ }
12+
13+ export async function captureLinuxSurfaceSnapshot (
14+ options : CaptureSnapshotInput [ 'options' ] ,
15+ signal ?: AbortSignal ,
16+ ) {
17+ const { snapshotLinux } = await import ( '../platforms/linux/snapshot.ts' ) ;
18+ const result = await snapshotLinux ( options ?. surface , signal ) ;
19+ return shapeDesktopSurfaceSnapshot (
20+ { nodes : result . nodes , truncated : result . truncated , backend : 'linux-atspi' } ,
21+ options ?? { } ,
22+ ) ;
23+ }
24+
25+ export async function captureMacOsSurfaceSnapshot (
26+ options : SnapshotSurfaceOptions ,
27+ signal ?: AbortSignal ,
28+ ) {
29+ const surface = options . surface ;
30+ if ( ! surface || surface === 'app' ) {
31+ throw new TypeError ( 'Apple surface capture requires a non-app macOS surface' ) ;
32+ }
33+ const { runMacOsSnapshotAction } = await import ( '../platforms/apple/os/macos/helper.ts' ) ;
34+ const result = await runMacOsSnapshotAction ( surface , {
35+ bundleId : surface === 'menubar' ? options . appBundleId : undefined ,
36+ signal,
37+ } ) ;
38+ return shapeDesktopSurfaceSnapshot ( result , options ) ;
39+ }
40+
41+ const captureSurface : SnapshotRuntimeHost [ 'captureSurface' ] = async ( device , options , signal ) => {
42+ if ( device . platform === 'linux' ) {
43+ return await captureLinuxSurfaceSnapshot ( options , signal ) ;
44+ }
45+ requireMacOsSurfaceDevice ( device ) ;
46+ return await captureMacOsSurfaceSnapshot ( options ?? { } , signal ) ;
47+ } ;
48+
49+ function requireMacOsSurfaceDevice ( device : DeviceInfo ) : void {
50+ if ( device . platform !== 'apple' || device . appleOs !== 'macos' ) {
51+ throw new TypeError ( 'Apple surface capture requires a non-app macOS surface' ) ;
52+ }
53+ }
54+
555const INTERACTIVE_ROLE_TOKENS = [
656 'button' ,
757 'menu' ,
@@ -13,7 +63,7 @@ const INTERACTIVE_ROLE_TOKENS = [
1363] as const ;
1464
1565/** Applies the legacy desktop-surface projection once for both runtime hosts and legacy capture. */
16- export function shapeDesktopSurfaceSnapshot (
66+ function shapeDesktopSurfaceSnapshot (
1767 data : SnapshotResult ,
1868 options : Pick < SnapshotOptions , 'depth' | 'interactiveOnly' | 'scope' > ,
1969) : SnapshotResult {
0 commit comments