diff --git a/src/commands/capture/runtime/index.ts b/src/commands/capture/runtime/index.ts index 14bd7f2fe..a661f3fa6 100644 --- a/src/commands/capture/runtime/index.ts +++ b/src/commands/capture/runtime/index.ts @@ -1,6 +1,5 @@ -import type { AgentDeviceRuntime } from '../../../runtime-contract.ts'; import type { - BoundRuntimeCommand, + BoundOf, DiffSnapshotCommandOptions, RuntimeCommand, ScreenshotCommandOptions, @@ -26,12 +25,7 @@ export type CaptureCommands = { diffSnapshot: RuntimeCommand; }; -export type BoundCaptureCommands = { - screenshot: BoundRuntimeCommand; - diffScreenshot: BoundRuntimeCommand; - snapshot: BoundRuntimeCommand; - diffSnapshot: BoundRuntimeCommand; -}; +export type BoundCaptureCommands = BoundOf; export const captureCommands: CaptureCommands = { screenshot: screenshotCommand, @@ -39,12 +33,3 @@ export const captureCommands: CaptureCommands = { snapshot: snapshotCommand, diffSnapshot: diffSnapshotCommand, }; - -export function bindCaptureCommands(runtime: AgentDeviceRuntime): BoundCaptureCommands { - return { - screenshot: (options) => captureCommands.screenshot(runtime, options), - diffScreenshot: (options) => captureCommands.diffScreenshot(runtime, options), - snapshot: (options) => captureCommands.snapshot(runtime, options), - diffSnapshot: (options) => captureCommands.diffSnapshot(runtime, options), - }; -} diff --git a/src/commands/index.ts b/src/commands/index.ts index 27b4790f1..813f3505b 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -1,6 +1,6 @@ import type { AgentDeviceRuntime } from '../runtime-contract.ts'; +import { bindRuntimeCommands } from './runtime-types.ts'; import { - bindCaptureCommands, captureCommands, type BoundCaptureCommands, type CaptureCommands, @@ -18,7 +18,6 @@ import { import { adminCommands, appCommands, - bindAdminCommands, bindAppCommands, type AdminCommands, type AppCommands, @@ -26,19 +25,16 @@ import { type BoundAppCommands, } from './management/runtime/index.ts'; import { - bindObservabilityCommands, diagnosticsCommands, type BoundObservabilityCommands, type DiagnosticsCommands, } from './observability/runtime/index.ts'; import { - bindRecordingCommands, recordingCommands, type BoundRecordingCommands, type RecordingCommands, } from './recording/runtime/index.ts'; import { - bindSystemCommands, systemCommands, type BoundSystemCommands, type SystemCommands, @@ -84,13 +80,13 @@ export const commands: AgentDeviceCommands = { export function bindCommands(runtime: AgentDeviceRuntime): BoundAgentDeviceCommands { return { - capture: bindCaptureCommands(runtime), + capture: bindRuntimeCommands(captureCommands, runtime), selectors: bindSelectorCommands(runtime), interactions: bindInteractionCommands(runtime), - system: bindSystemCommands(runtime), + system: bindRuntimeCommands(systemCommands, runtime), apps: bindAppCommands(runtime), - admin: bindAdminCommands(runtime), - recording: bindRecordingCommands(runtime), - observability: bindObservabilityCommands(runtime), + admin: bindRuntimeCommands(adminCommands, runtime), + recording: bindRuntimeCommands(recordingCommands, runtime), + observability: bindRuntimeCommands(diagnosticsCommands, runtime), }; } diff --git a/src/commands/management/runtime/index.ts b/src/commands/management/runtime/index.ts index 53e719eb6..40ec7d8ce 100644 --- a/src/commands/management/runtime/index.ts +++ b/src/commands/management/runtime/index.ts @@ -1,5 +1,5 @@ import type { AgentDeviceRuntime } from '../../../runtime-contract.ts'; -import type { BoundRuntimeCommand, RuntimeCommand } from '../../runtime-types.ts'; +import { bindRuntimeCommands, type BoundOf, type RuntimeCommand } from '../../runtime-types.ts'; import { resolveAppsFilter } from '../app-inventory-contract.ts'; import { bootCommand, @@ -61,26 +61,9 @@ export type AdminCommands = { >; }; -export type BoundAppCommands = { - open: BoundRuntimeCommand; - close: (options?: CloseAppCommandOptions) => Promise; - list: (options?: ListAppsCommandOptions) => Promise; - state: BoundRuntimeCommand; - push: BoundRuntimeCommand; - triggerEvent: BoundRuntimeCommand; -}; +export type BoundAppCommands = BoundOf; -export type BoundAdminCommands = { - devices: (options?: AdminDevicesCommandOptions) => Promise; - boot: (options?: AdminBootCommandOptions) => Promise; - shutdown: (options?: AdminShutdownCommandOptions) => Promise; - install: BoundRuntimeCommand; - reinstall: BoundRuntimeCommand; - installFromSource: BoundRuntimeCommand< - AdminInstallFromSourceCommandOptions, - AdminInstallCommandResult - >; -}; +export type BoundAdminCommands = BoundOf; export const appCommands: AppCommands = { open: openAppCommand, @@ -102,26 +85,11 @@ export const adminCommands: AdminCommands = { export function bindAppCommands(runtime: AgentDeviceRuntime): BoundAppCommands { return { - open: (options) => appCommands.open(runtime, options), - close: (options) => appCommands.close(runtime, options), + ...bindRuntimeCommands(appCommands, runtime), list: (options = {}) => appCommands.list(runtime, { ...options, filter: resolveAppsFilter(options.filter), }), - state: (options) => appCommands.state(runtime, options), - push: (options) => appCommands.push(runtime, options), - triggerEvent: (options) => appCommands.triggerEvent(runtime, options), - }; -} - -export function bindAdminCommands(runtime: AgentDeviceRuntime): BoundAdminCommands { - return { - devices: (options) => adminCommands.devices(runtime, options), - boot: (options) => adminCommands.boot(runtime, options), - shutdown: (options) => adminCommands.shutdown(runtime, options), - install: (options) => adminCommands.install(runtime, options), - reinstall: (options) => adminCommands.reinstall(runtime, options), - installFromSource: (options) => adminCommands.installFromSource(runtime, options), }; } diff --git a/src/commands/observability/runtime/index.ts b/src/commands/observability/runtime/index.ts index 150be884b..d2a7c8ea0 100644 --- a/src/commands/observability/runtime/index.ts +++ b/src/commands/observability/runtime/index.ts @@ -1,5 +1,4 @@ -import type { AgentDeviceRuntime } from '../../../runtime-contract.ts'; -import type { RuntimeCommand } from '../../runtime-types.ts'; +import type { BoundOf, RuntimeCommand } from '../../runtime-types.ts'; import { logsCommand, networkCommand, @@ -21,22 +20,10 @@ export type DiagnosticsCommands = { perf: RuntimeCommand; }; -export type BoundObservabilityCommands = { - logs: (options?: DiagnosticsLogsCommandOptions) => Promise; - network: (options?: DiagnosticsNetworkCommandOptions) => Promise; - perf: (options?: DiagnosticsPerfCommandOptions) => Promise; -}; +export type BoundObservabilityCommands = BoundOf; export const diagnosticsCommands: DiagnosticsCommands = { logs: logsCommand, network: networkCommand, perf: perfCommand, }; - -export function bindObservabilityCommands(runtime: AgentDeviceRuntime): BoundObservabilityCommands { - return { - logs: (options) => diagnosticsCommands.logs(runtime, options), - network: (options) => diagnosticsCommands.network(runtime, options), - perf: (options) => diagnosticsCommands.perf(runtime, options), - }; -} diff --git a/src/commands/recording/runtime/index.ts b/src/commands/recording/runtime/index.ts index 81b8240f5..3051b8da5 100644 --- a/src/commands/recording/runtime/index.ts +++ b/src/commands/recording/runtime/index.ts @@ -1,5 +1,4 @@ -import type { AgentDeviceRuntime } from '../../../runtime-contract.ts'; -import type { BoundRuntimeCommand, RuntimeCommand } from '../../runtime-types.ts'; +import type { BoundOf, RuntimeCommand } from '../../runtime-types.ts'; import { recordCommand, traceCommand, @@ -14,19 +13,9 @@ export type RecordingCommands = { trace: RuntimeCommand; }; -export type BoundRecordingCommands = { - record: BoundRuntimeCommand; - trace: BoundRuntimeCommand; -}; +export type BoundRecordingCommands = BoundOf; export const recordingCommands: RecordingCommands = { record: recordCommand, trace: traceCommand, }; - -export function bindRecordingCommands(runtime: AgentDeviceRuntime): BoundRecordingCommands { - return { - record: (options) => recordingCommands.record(runtime, options), - trace: (options) => recordingCommands.trace(runtime, options), - }; -} diff --git a/src/commands/runtime-types.ts b/src/commands/runtime-types.ts index 0f2210292..d8c8f0476 100644 --- a/src/commands/runtime-types.ts +++ b/src/commands/runtime-types.ts @@ -13,6 +13,26 @@ export type BoundRuntimeCommand, TResult = Co options: TOptions, ) => Promise; +export type BoundOf = { + [K in keyof T]: T[K] extends RuntimeCommand + ? undefined extends TOptions + ? (options?: TOptions) => Promise + : BoundRuntimeCommand + : never; +}; + +export function bindRuntimeCommands>>( + commands: T, + runtime: AgentDeviceRuntime, +): BoundOf { + return Object.fromEntries( + Object.entries(commands).map(([name, command]) => [ + name, + (options: unknown) => command(runtime, options), + ]), + ) as BoundOf; +} + export function toBackendResult(result: unknown): Record | undefined { return result && typeof result === 'object' ? (result as Record) : undefined; } diff --git a/src/commands/system/runtime/index.ts b/src/commands/system/runtime/index.ts index be808dc84..5cd1b467b 100644 --- a/src/commands/system/runtime/index.ts +++ b/src/commands/system/runtime/index.ts @@ -1,5 +1,4 @@ -import type { AgentDeviceRuntime } from '../../../runtime-contract.ts'; -import type { BoundRuntimeCommand, RuntimeCommand } from '../../runtime-types.ts'; +import type { BoundOf, RuntimeCommand } from '../../runtime-types.ts'; import { alertCommand, appSwitcherCommand, @@ -45,19 +44,7 @@ export type SystemCommands = { tvRemote: RuntimeCommand; }; -export type BoundSystemCommands = { - back: (options?: SystemBackCommandOptions) => Promise; - home: (options?: SystemHomeCommandOptions) => Promise; - orientation: BoundRuntimeCommand; - keyboard: (options?: SystemKeyboardCommandOptions) => Promise; - clipboard: BoundRuntimeCommand; - settings: (options?: SystemSettingsCommandOptions) => Promise; - alert: (options?: SystemAlertCommandOptions) => Promise; - appSwitcher: ( - options?: SystemAppSwitcherCommandOptions, - ) => Promise; - tvRemote: BoundRuntimeCommand; -}; +export type BoundSystemCommands = BoundOf; export const systemCommands: SystemCommands = { back: backCommand, @@ -70,17 +57,3 @@ export const systemCommands: SystemCommands = { appSwitcher: appSwitcherCommand, tvRemote: tvRemoteCommand, }; - -export function bindSystemCommands(runtime: AgentDeviceRuntime): BoundSystemCommands { - return { - back: (options) => systemCommands.back(runtime, options), - home: (options) => systemCommands.home(runtime, options), - orientation: (options) => systemCommands.orientation(runtime, options), - keyboard: (options) => systemCommands.keyboard(runtime, options), - clipboard: (options) => systemCommands.clipboard(runtime, options), - settings: (options) => systemCommands.settings(runtime, options), - alert: (options) => systemCommands.alert(runtime, options), - appSwitcher: (options) => systemCommands.appSwitcher(runtime, options), - tvRemote: (options) => systemCommands.tvRemote(runtime, options), - }; -}