|
| 1 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 2 | +import { AppError } from '@agent-device/kernel/errors'; |
| 3 | +import type { |
| 4 | + Interactor, |
| 5 | + RunnerContext, |
| 6 | + SnapshotOptions, |
| 7 | + SnapshotResult, |
| 8 | +} from './interactor-types.ts'; |
| 9 | + |
| 10 | +export type { SnapshotResult } from './interactor-types.ts'; |
| 11 | + |
| 12 | +/** Runner metadata needed by the selected snapshot implementation, without request-owned state. */ |
| 13 | +export type SnapshotRuntimeExecution = Readonly<Omit<RunnerContext, 'appBundleId' | 'signal'>>; |
| 14 | + |
| 15 | +/** Neutral snapshot intent. The request binding supplies cancellation and exact-owner authority. */ |
| 16 | +export type CaptureSnapshotInput = Readonly<{ |
| 17 | + options?: Readonly<Omit<SnapshotOptions, 'signal'>>; |
| 18 | + execution?: SnapshotRuntimeExecution; |
| 19 | +}>; |
| 20 | + |
| 21 | +export type SnapshotRuntimeOperations = Readonly<{ |
| 22 | + captureSnapshot(input: CaptureSnapshotInput): Promise<SnapshotResult>; |
| 23 | +}>; |
| 24 | + |
| 25 | +/** Existing desktop-surface mechanics injected without exposing daemon/session ownership. */ |
| 26 | +export type SnapshotRuntimeHost = Readonly<{ |
| 27 | + apple: Readonly<{ |
| 28 | + captureSurface( |
| 29 | + device: DeviceInfo, |
| 30 | + options: CaptureSnapshotInput['options'], |
| 31 | + signal: AbortSignal, |
| 32 | + ): Promise<SnapshotResult>; |
| 33 | + }>; |
| 34 | + linux: Readonly<{ |
| 35 | + captureSurface( |
| 36 | + device: DeviceInfo, |
| 37 | + options: CaptureSnapshotInput['options'], |
| 38 | + signal: AbortSignal, |
| 39 | + ): Promise<SnapshotResult>; |
| 40 | + }>; |
| 41 | +}>; |
| 42 | + |
| 43 | +export type LocalSnapshotInteractorResolver = ( |
| 44 | + device: DeviceInfo, |
| 45 | + runner: RunnerContext, |
| 46 | +) => Promise<Interactor>; |
| 47 | + |
| 48 | +export type ProviderSnapshotInteractorResolver = (runner: RunnerContext) => Interactor | undefined; |
| 49 | + |
| 50 | +type SnapshotInteractorBindingParams = |
| 51 | + | Readonly<{ |
| 52 | + device: DeviceInfo; |
| 53 | + signal: AbortSignal; |
| 54 | + ownership: 'local'; |
| 55 | + resolveInteractor: LocalSnapshotInteractorResolver; |
| 56 | + }> |
| 57 | + | Readonly<{ |
| 58 | + device: DeviceInfo; |
| 59 | + signal: AbortSignal; |
| 60 | + ownership: 'provider'; |
| 61 | + resolveInteractor: ProviderSnapshotInteractorResolver; |
| 62 | + }>; |
| 63 | + |
| 64 | +/** Captures one selected owner's interactor authority for the lifetime of a request binding. */ |
| 65 | +function bindSnapshotInteractor( |
| 66 | + params: SnapshotInteractorBindingParams, |
| 67 | +): SnapshotRuntimeOperations { |
| 68 | + return Object.freeze({ |
| 69 | + captureSnapshot: async (input) => { |
| 70 | + const runner: RunnerContext = { |
| 71 | + ...input.execution, |
| 72 | + appBundleId: input.options?.appBundleId, |
| 73 | + signal: params.signal, |
| 74 | + }; |
| 75 | + const interactor = |
| 76 | + params.ownership === 'local' |
| 77 | + ? await params.resolveInteractor(params.device, runner) |
| 78 | + : params.resolveInteractor(runner); |
| 79 | + if (!interactor) { |
| 80 | + throw new AppError( |
| 81 | + 'UNSUPPORTED_OPERATION', |
| 82 | + 'Provider-owned snapshot operation has no bound provider interactor.', |
| 83 | + { reason: 'provider-runtime-interactor-missing', deviceId: params.device.id }, |
| 84 | + ); |
| 85 | + } |
| 86 | + return await interactor.snapshot({ ...input.options, signal: params.signal }); |
| 87 | + }, |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +export function bindLocalSnapshotInteractor( |
| 92 | + params: Readonly<{ |
| 93 | + device: DeviceInfo; |
| 94 | + signal: AbortSignal; |
| 95 | + resolveInteractor: LocalSnapshotInteractorResolver; |
| 96 | + }>, |
| 97 | +): SnapshotRuntimeOperations { |
| 98 | + return bindSnapshotInteractor({ ...params, ownership: 'local' }); |
| 99 | +} |
| 100 | + |
| 101 | +/** Provider bindings fail closed when their exact owner no longer exposes its interactor. */ |
| 102 | +export function bindProviderSnapshotInteractor( |
| 103 | + params: Readonly<{ |
| 104 | + device: DeviceInfo; |
| 105 | + signal: AbortSignal; |
| 106 | + resolveInteractor: ProviderSnapshotInteractorResolver; |
| 107 | + }>, |
| 108 | +): SnapshotRuntimeOperations { |
| 109 | + return bindSnapshotInteractor({ ...params, ownership: 'provider' }); |
| 110 | +} |
0 commit comments