Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions packages/contracts/src/client-gesture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import type { ClickButton } from './click-button.ts';
import type { GesturePointerCount } from './gesture-plan-types.ts';
import type { GesturePayload } from './gesture-input.ts';
import type {
DragGesturePayload,
GesturePayload,
SwipeGesturePayload,
TapGesturePayload,
} from './gesture-input.ts';
import type {
ScrollDirection,
ScrollInputDirection,
SwipePattern,
SwipePreset,
TransformGestureParams,
} from './scroll-gesture.ts';
import type { SelectorSnapshotCommandOptions } from './client-capture.ts';
Expand Down Expand Up @@ -90,13 +94,7 @@ export type PanOptions = DeviceCommandBaseOptions & {
durationMs?: number;
};

export type DragOptions = DeviceCommandBaseOptions & {
source: string;
destination: string;
sourceHoldMs?: number;
moveMs?: number;
destinationHoldMs?: number;
};
export type DragOptions = DeviceCommandBaseOptions & Omit<DragGesturePayload, 'kind'>;

export type FlingOptions = DeviceCommandBaseOptions & {
direction: ScrollDirection;
Expand All @@ -105,9 +103,7 @@ export type FlingOptions = DeviceCommandBaseOptions & {
distance?: number;
};

export type SwipeGestureOptions = DeviceCommandBaseOptions & {
preset: SwipePreset;
};
export type SwipeGestureOptions = DeviceCommandBaseOptions & Omit<SwipeGesturePayload, 'kind'>;

export type FocusOptions = DeviceCommandBaseOptions & {
x: number;
Expand Down Expand Up @@ -147,12 +143,7 @@ export type RotateGestureOptions = DeviceCommandBaseOptions & {
export type TransformGestureOptions = DeviceCommandBaseOptions & TransformGestureParams;

/** Touches 2 to 5 points and holds them for `holdMs`; `count` repeats the same contacts. */
export type TapGestureOptions = DeviceCommandBaseOptions & {
points: { x: number; y: number }[];
holdMs?: number;
count?: number;
pauseMs?: number;
};
export type TapGestureOptions = DeviceCommandBaseOptions & Omit<TapGesturePayload, 'kind'>;

export type ScrollOptions = DeviceCommandBaseOptions &
SettleCommandOptions & {
Expand Down
58 changes: 14 additions & 44 deletions src/agent-device-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ import type {
CaptureSnapshotResult,
DaemonBinding,
DaemonLifecycle,
DragOptions,
FlingOptions,
MaterializationReleaseOptions,
PanOptions,
PinchOptions,
RotateGestureOptions,
SessionSaveScriptOptions,
SessionSaveScriptResult,
SwipeGestureOptions,
TapGestureOptions,
TransformGestureOptions,
} from '@agent-device/contracts/client';
import type { AgentArtifactsResult } from '@agent-device/contracts/observability';
Expand Down Expand Up @@ -458,11 +455,12 @@ export function createAgentDeviceClient(
longPress: async (options) => await executeCommand('longpress', options),
hover: async (options) => await executeCommand('hover', options),
swipe: async (options) => await executeCommand('swipe', options),
pan: async (options) => await executeCommand('gesture', panGestureInput(options)),
drag: async (options) => await executeCommand('gesture', dragGestureInput(options)),
pan: async (options) =>
await executeCommand('gesture', translatedGestureInput('pan', options)),
drag: async (options) => await executeCommand('gesture', { ...options, kind: 'drag' }),
fling: async (options) => await executeCommand('gesture', flingGestureInput(options)),
swipeGesture: async (options) =>
await executeCommand('gesture', swipePresetGestureInput(options)),
await executeCommand('gesture', { ...options, kind: 'swipe' }),
focus: async (options) => await executeCommand('focus', options),
type: async (options) => await executeCommand('type', options),
fill: async (options) => await executeCommand('fill', options),
Expand All @@ -472,8 +470,8 @@ export function createAgentDeviceClient(
rotateGesture: async (options) =>
await executeCommand('gesture', centeredGestureInput('rotate', options)),
transformGesture: async (options) =>
await executeCommand('gesture', transformGestureInput(options)),
tapGesture: async (options) => await executeCommand('gesture', tapGestureInput(options)),
await executeCommand('gesture', translatedGestureInput('transform', options)),
tapGesture: async (options) => await executeCommand('gesture', { ...options, kind: 'tap' }),
get: async (options) => await executeCommand('get', options),
is: async (options) => await executeCommand('is', options),
find: async (options) => await executeCommand('find', options),
Expand Down Expand Up @@ -512,13 +510,12 @@ export function createAgentDeviceClient(
};
}

function panGestureInput(options: PanOptions): InternalRequestOptions & Record<string, unknown> {
function translatedGestureInput(
kind: 'pan' | 'transform',
options: PanOptions | TransformGestureOptions,
): InternalRequestOptions & Record<string, unknown> {
const { x, y, dx, dy, ...common } = options;
return { ...common, kind: 'pan', origin: { x, y }, delta: { x: dx, y: dy } };
}

function dragGestureInput(options: DragOptions): InternalRequestOptions & Record<string, unknown> {
return { ...options, kind: 'drag' };
return { ...common, kind, origin: { x, y }, delta: { x: dx, y: dy } };
}

function flingGestureInput(
Expand All @@ -528,48 +525,21 @@ function flingGestureInput(
return { ...common, kind: 'fling', origin: { x, y } };
}

function swipePresetGestureInput(
options: SwipeGestureOptions,
): InternalRequestOptions & Record<string, unknown> {
return { ...options, kind: 'swipe' };
}

function centeredGestureInput(
kind: 'pinch' | 'rotate',
options: PinchOptions | RotateGestureOptions,
): InternalRequestOptions & Record<string, unknown> {
const { x, y, ...common } = options;
assertCompleteGestureCenter(x, y, kind);
if ((x === undefined) !== (y === undefined)) {
throw new AppError('INVALID_ARGS', `gesture ${kind} center requires both x and y`);
}
return {
...common,
kind,
...(x === undefined && y === undefined ? {} : { origin: { x, y } }),
};
}

function transformGestureInput(
options: TransformGestureOptions,
): InternalRequestOptions & Record<string, unknown> {
const { x, y, dx, dy, ...common } = options;
return { ...common, kind: 'transform', origin: { x, y }, delta: { x: dx, y: dy } };
}

function tapGestureInput(
options: TapGestureOptions,
): InternalRequestOptions & Record<string, unknown> {
return { ...options, kind: 'tap' };
}

function assertCompleteGestureCenter(
x: number | undefined,
y: number | undefined,
gesture: 'pinch' | 'rotate',
): void {
if ((x === undefined) !== (y === undefined)) {
throw new AppError('INVALID_ARGS', `gesture ${gesture} center requires both x and y`);
}
}

function normalizeSnapshotResult(
data: Record<string, unknown>,
session: string | undefined,
Expand Down
Loading