|
| 1 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 2 | +import { AppError } from '@agent-device/kernel/errors'; |
| 3 | +import type { Point } from '@agent-device/kernel/snapshot'; |
| 4 | +import type { Interactor, RunnerContext } from './interactor-types.ts'; |
| 5 | +import type { RuntimeOperationFact } from './platform-runtime.ts'; |
| 6 | +import type { SessionSurface } from './session-surface.ts'; |
| 7 | +import type { SnapshotRuntimeExecution } from './snapshot-runtime.ts'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Neutral intent for one point-addressed focus. The point is already resolved — from the |
| 11 | + * positionals a caller parsed, or from the node a selector matched — so the operation names no |
| 12 | + * command, request, session, or CLI flag. |
| 13 | + */ |
| 14 | +export type FocusPointInput = Readonly<{ |
| 15 | + point: Point; |
| 16 | + options?: Readonly<{ appBundleId?: string; surface?: SessionSurface }>; |
| 17 | + /** Same runner metadata a capture needs; reuses that type rather than restating it. */ |
| 18 | + execution?: SnapshotRuntimeExecution; |
| 19 | +}>; |
| 20 | + |
| 21 | +/** |
| 22 | + * Focus returns nothing. The legacy leaf discarded whatever the interactor answered and reported |
| 23 | + * only the point it addressed, so a result type here would be a surface the command never had. |
| 24 | + */ |
| 25 | +export type FocusRuntimeOperations = Readonly<{ |
| 26 | + focusPoint(input: FocusPointInput): Promise<void>; |
| 27 | +}>; |
| 28 | + |
| 29 | +export type FocusRuntimeOperationFacts = Readonly<{ |
| 30 | + focusPoint: RuntimeOperationFact; |
| 31 | +}>; |
| 32 | + |
| 33 | +export function focusRuntimeOperationFacts( |
| 34 | + input: Readonly<{ focus: RuntimeOperationFact }>, |
| 35 | +): FocusRuntimeOperationFacts { |
| 36 | + return Object.freeze({ focusPoint: input.focus }); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Captures one selected owner's interactor authority for the lifetime of a request binding. The |
| 41 | + * owner is already chosen by the time a binder is called, so each entry point supplies its own |
| 42 | + * resolution and this holds only what both share: the runner context and the focus itself. |
| 43 | + */ |
| 44 | +function bindFocusPoint( |
| 45 | + signal: AbortSignal, |
| 46 | + resolveInteractor: (runner: RunnerContext) => Promise<Interactor>, |
| 47 | +): FocusRuntimeOperations { |
| 48 | + return Object.freeze({ |
| 49 | + focusPoint: async (input: FocusPointInput) => { |
| 50 | + signal.throwIfAborted(); |
| 51 | + const interactor = await resolveInteractor({ |
| 52 | + ...input.execution, |
| 53 | + appBundleId: input.options?.appBundleId, |
| 54 | + signal, |
| 55 | + }); |
| 56 | + await interactor.focus(input.point.x, input.point.y); |
| 57 | + }, |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +export type LocalFocusInteractorResolver = ( |
| 62 | + device: DeviceInfo, |
| 63 | + runner: RunnerContext, |
| 64 | +) => Promise<Interactor>; |
| 65 | + |
| 66 | +export function bindLocalFocusInteractor( |
| 67 | + params: Readonly<{ |
| 68 | + device: DeviceInfo; |
| 69 | + signal: AbortSignal; |
| 70 | + resolveInteractor: LocalFocusInteractorResolver; |
| 71 | + }>, |
| 72 | +): FocusRuntimeOperations { |
| 73 | + return bindFocusPoint( |
| 74 | + params.signal, |
| 75 | + async (runner) => await params.resolveInteractor(params.device, runner), |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +export type ProviderFocusInteractorResolver = (runner: RunnerContext) => Interactor | undefined; |
| 80 | + |
| 81 | +/** Provider bindings fail closed when their exact owner no longer exposes its interactor. */ |
| 82 | +export function bindProviderFocusInteractor( |
| 83 | + params: Readonly<{ |
| 84 | + device: DeviceInfo; |
| 85 | + signal: AbortSignal; |
| 86 | + resolveInteractor: ProviderFocusInteractorResolver; |
| 87 | + }>, |
| 88 | +): FocusRuntimeOperations { |
| 89 | + return bindFocusPoint(params.signal, async (runner) => { |
| 90 | + const interactor = params.resolveInteractor(runner); |
| 91 | + if (interactor) return interactor; |
| 92 | + throw new AppError( |
| 93 | + 'UNSUPPORTED_OPERATION', |
| 94 | + 'Provider-owned focus operation has no bound provider interactor.', |
| 95 | + { reason: 'provider-runtime-interactor-missing', deviceId: params.device.id }, |
| 96 | + ); |
| 97 | + }); |
| 98 | +} |
0 commit comments