Repository navigation
Expand file tree
/
Copy pathinteractions.ts
More file actions
252 lines (243 loc) · 8.94 KB
/
Copy pathinteractions.ts
File metadata and controls
252 lines (243 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import type {
ElementTarget,
FillOptions,
InteractionTarget,
LongPressOptions,
TypeTextOptions,
} from '@agent-device/contracts/client';
import {
assertNoRemovedSwipeInput,
swipePayloadFromPositionals,
} from '@agent-device/contracts/gesture-normalization';
import { AppError } from '@agent-device/kernel/errors';
import { PUBLIC_COMMANDS } from '@agent-device/command-registry/catalog';
import {
readFillTargetFromPositionals,
readInteractionTargetFromPositionals,
} from '@agent-device/selectors/interaction-positionals';
import {
commonInputFromFlags,
direct,
elementTargetPositionals,
interactionTargetPositionals,
isFiniteNumberString,
observationRecordInputFromFlags,
optionalCliNumber,
optionalNumber,
readElementTargetFromPositionals,
readGetFormat,
repeatedInputFromFlags,
request,
requiredDaemonString,
selectorSnapshotInputFromFlags,
targetInputFromClientTarget,
} from '../cli-grammar/common.ts';
import type { CliReader, DaemonWriter } from '../cli-grammar/types.ts';
import { assertNoRetiredReadinessTimeout } from '../common-input-fields.ts';
import type { ScrollInputDirection } from '@agent-device/contracts/scroll-gesture';
export const interactionCliReaders = {
click: (positionals, flags) => ({
...commonInputFromFlags(flags),
...selectorSnapshotInputFromFlags(flags),
...repeatedInputFromFlags(flags),
target: targetInputFromClientTarget(readInteractionTargetFromPositionals(positionals)),
button: flags.clickButton,
verify: flags.verify,
}),
press: (positionals, flags) => ({
...commonInputFromFlags(flags),
...selectorSnapshotInputFromFlags(flags),
...repeatedInputFromFlags(flags),
target: targetInputFromClientTarget(readInteractionTargetFromPositionals(positionals)),
verify: flags.verify,
}),
longpress: (positionals, flags) => {
const decoded = readLongPressTargetFromPositionals(positionals);
return {
...commonInputFromFlags(flags),
...selectorSnapshotInputFromFlags(flags),
target: targetInputFromClientTarget(decoded),
durationMs: decoded.durationMs,
};
},
hover: (positionals, flags) => ({
...commonInputFromFlags(flags),
...selectorSnapshotInputFromFlags(flags),
target: targetInputFromClientTarget(readInteractionTargetFromPositionals(positionals)),
}),
swipe: (positionals, flags) => ({
...commonInputFromFlags(flags),
...swipePayloadFromPositionals(positionals, {
count: flags.count,
pauseMs: flags.pauseMs,
pattern: flags.pattern,
}),
}),
focus: (positionals, flags) => ({
...commonInputFromFlags(flags),
x: Number(positionals[0]),
y: Number(positionals[1]),
}),
type: (positionals, flags) => ({
...commonInputFromFlags(flags),
text: positionals.join(' '),
delayMs: flags.delayMs,
}),
fill: (positionals, flags) => {
const decoded = flags.textStdin
? readStdinFillTarget(positionals)
: readFillTargetFromPositionals(positionals);
return {
...commonInputFromFlags(flags),
...selectorSnapshotInputFromFlags(flags),
target: targetInputFromClientTarget(decoded.target),
text: decoded.text,
delayMs: flags.delayMs,
recordAs: flags.recordAs,
textStdin: flags.textStdin,
verify: flags.verify,
};
},
scroll: (positionals, flags) => ({
...commonInputFromFlags(flags),
direction: readScrollDirection(positionals[0]),
amount: optionalCliNumber(positionals[1]),
pixels: flags.pixels,
durationMs: flags.durationMs,
until: flags.until,
}),
// The one observation-only reader in this file: `get` can be excluded from a
// repair-armed heal by default, so it also takes the `--record` opt-in
// (#1271 stage 2). Every other reader here is a mutation and takes only
// `--no-record`.
get: (positionals, flags) => ({
...commonInputFromFlags(flags),
...observationRecordInputFromFlags(flags),
...selectorSnapshotInputFromFlags(flags),
format: readGetFormat(positionals[0]),
target: targetInputFromClientTarget(readElementTargetFromPositionals(positionals.slice(1))),
}),
} satisfies Record<string, CliReader>;
export const interactionDaemonWriters = {
click: (input) => {
assertNoRetiredReadinessTimeout(input);
return request(
PUBLIC_COMMANDS.click,
interactionTargetPositionals(input as InteractionTarget),
{ ...input, clickButton: input.button },
);
},
press: direct(PUBLIC_COMMANDS.press, (input) => {
assertNoRetiredReadinessTimeout(input);
return interactionTargetPositionals(input as InteractionTarget);
}),
longpress: direct(PUBLIC_COMMANDS.longPress, (input) => {
assertNoRetiredReadinessTimeout(input);
return longPressPositionals(input as LongPressOptions);
}),
hover: direct(PUBLIC_COMMANDS.hover, (input) =>
interactionTargetPositionals(input as InteractionTarget),
),
swipe: (input) => {
assertNoRemovedSwipeInput(input);
return request(PUBLIC_COMMANDS.swipe, [], input, {
from: input.from,
to: input.to,
count: input.count,
pauseMs: input.pauseMs,
pattern: input.pattern,
});
},
focus: direct(PUBLIC_COMMANDS.focus, (input) => [String(input.x), String(input.y)]),
type: direct(PUBLIC_COMMANDS.type, (input) => typePositionals(input as TypeTextOptions)),
fill: direct(PUBLIC_COMMANDS.fill, (input) => fillPositionals(input as FillOptions)),
scroll: direct(PUBLIC_COMMANDS.scroll, (input) => [
requiredDaemonString(input.direction, 'scroll requires direction'),
...optionalNumber(input.amount),
]),
get: direct(PUBLIC_COMMANDS.get, (input) => [
requiredDaemonString(input.format, 'get requires format'),
...elementTargetPositionals(input as ElementTarget),
]),
} satisfies Record<string, DaemonWriter>;
/**
* Reads only the target for `fill --text-stdin`. A stray positional may be the secret itself
* (`fill --text-stdin "$PASSWORD"`), so neither a parse failure nor the conflict echoes any of them.
*/
function readStdinFillTarget(
positionals: string[],
): ReturnType<typeof readFillTargetFromPositionals> {
let decoded: ReturnType<typeof readFillTargetFromPositionals>;
try {
decoded = readFillTargetFromPositionals(positionals);
} catch (error) {
if (!(error instanceof AppError)) throw error;
throw new AppError('INVALID_ARGS', 'fill --text-stdin could not read the target.', {
reason: 'fill_text_stdin_target_invalid',
hint: 'Pass only the target: an @ref, a quoted selector, or x y coordinates. The text itself goes to stdin.',
});
}
if (decoded.text !== undefined) {
throw new AppError(
'INVALID_ARGS',
'fill --text-stdin reads the text from stdin, so it cannot also take a text argument.',
{
reason: 'fill_text_source_conflict',
hint: 'Pass only the target, for example: printf %s "$PASSWORD" | agent-device fill @e3 --text-stdin. Quote a multi-word selector so no part of it is read as text.',
},
);
}
return decoded;
}
function readLongPressTargetFromPositionals(positionals: string[]): LongPressOptions {
const targetPositionals = readLongPressTargetPositionals(positionals);
return {
...readInteractionTargetFromPositionals(targetPositionals.target),
...(targetPositionals.durationMs !== undefined
? { durationMs: targetPositionals.durationMs }
: {}),
};
}
function longPressPositionals(input: LongPressOptions): string[] {
return [...interactionTargetPositionals(input), ...optionalNumber(input.durationMs)];
}
function typePositionals(input: TypeTextOptions): string[] {
return [input.text];
}
function fillPositionals(input: FillOptions): string[] {
return [...interactionTargetPositionals(input), input.text];
}
function readScrollDirection(value: string | undefined): ScrollInputDirection {
if (
value === 'up' ||
value === 'down' ||
value === 'left' ||
value === 'right' ||
value === 'top' ||
value === 'bottom'
) {
return value;
}
// #1366: agents recovering from an off-screen target often mis-shape this as
// `scroll @ref down` or `scroll down @ref`. scroll takes no target — name the
// grammar so the retry lands instead of cycling through arg orders.
throw new AppError('INVALID_ARGS', `Unknown direction: ${String(value)}`, {
hint: 'scroll takes a direction first, then an optional amount: scroll <up|down|left|right|top|bottom> [amount]. It takes no @ref or selector — scroll the whole viewport, then retry the target with a selector.',
});
}
function readLongPressTargetPositionals(positionals: string[]): {
target: string[];
durationMs?: number;
} {
if (isFiniteNumberString(positionals[0]) && isFiniteNumberString(positionals[1])) {
return {
target: positionals.slice(0, 2),
...(positionals[2] !== undefined ? { durationMs: Number(positionals[2]) } : {}),
};
}
const last = positionals.at(-1);
if (positionals.length > 1 && isFiniteNumberString(last)) {
return { target: positionals.slice(0, -1), durationMs: Number(last) };
}
return { target: positionals };
}