-
-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathmetadata.ts
More file actions
293 lines (267 loc) · 10.6 KB
/
Copy pathmetadata.ts
File metadata and controls
293 lines (267 loc) · 10.6 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { defineCommandMetadata } from '../command-contract.ts';
import {
booleanField,
elementTargetField,
enumField,
fieldsInputSchema,
integerField,
interactionTargetField,
numberField,
pointField,
readCommonInput,
readFieldInput,
readInputRecord,
repeatedFields,
requiredField,
selectorSnapshotFields,
stringField,
type CommandFieldMap,
type CommonCommandInput,
type InferCommandInput,
} from '../command-input.ts';
import { defineFieldCommandMetadata } from '../field-command-contract.ts';
import { CLICK_BUTTONS } from '../../core/click-button.ts';
import { SCROLL_DURATION_MAX_MS } from '../../contracts/scroll-command.ts';
import {
SCROLL_DIRECTIONS,
SWIPE_PAUSE_MAX_MS,
SWIPE_PATTERNS,
SWIPE_PRESETS,
SWIPE_REPETITION_MAX,
} from '../../contracts/scroll-gesture.ts';
import { SCROLL_INPUT_DIRECTIONS } from './runtime/gestures.ts';
import { FIND_LOCATORS } from '../../selectors/find.ts';
import {
commandSupportsSettleObservation,
commandSupportsVerifyEvidence,
} from '../../core/command-descriptor/registry.ts';
import type { PostActionObservationSupportFor } from '../../core/command-descriptor/post-action-observation.ts';
import {
GESTURE_KINDS,
readGesturePayload,
type FlingGesturePayload,
type PanGesturePayload,
type PinchGesturePayload,
type RotateGesturePayload,
type SwipeGesturePayload,
type TransformGesturePayload,
} from '../../contracts/gesture-input.ts';
const FIND_ACTION_VALUES = [
'click',
'focus',
'exists',
'getText',
'getAttrs',
'wait',
'fill',
'type',
] as const;
const interactionCommandDescriptions = {
click: 'Click or tap a semantic UI target by ref, selector, or point.',
press: 'Press a semantic UI target by ref, selector, or point.',
fill: 'Replace text in a semantic UI target by ref, selector, or point.',
longpress: 'Long press by ref, selector, or point.',
swipe: 'Swipe between two points.',
focus: 'Focus input at coordinates.',
type: 'Append text to the focused field.',
scroll: 'Scroll in a direction or to an edge.',
get: 'Get element text or attributes.',
is: 'Assert UI state.',
find: 'Find an element and optionally act on it.',
gesture: 'Run a structured gesture.',
} as const;
type InteractionCommandName = keyof typeof interactionCommandDescriptions;
const verifyField = () =>
booleanField(
'Capture cheap post-action evidence (AX digest, node counts, changedFromBefore) instead of a follow-up snapshot.',
);
const settleFields = () => ({
settle: booleanField(
'After the action, wait for the UI to go quiet and return the settled diff vs the pre-action tree in the same response. Best-effort; never fails the action.',
),
settleQuietMs: integerField('Settle: quiet window in milliseconds (default 500).', { min: 0 }),
timeoutMs: integerField('Settle: wait deadline in milliseconds (default 10000).', { min: 1 }),
});
type VerifyFieldMap = { verify: ReturnType<typeof verifyField> };
type SettleFieldMap = ReturnType<typeof settleFields>;
type PostActionObservationFields<TName extends string> =
PostActionObservationSupportFor<TName> extends 'settle-and-verify'
? VerifyFieldMap & SettleFieldMap
: PostActionObservationSupportFor<TName> extends 'settle'
? SettleFieldMap
: {};
function postActionObservationFields<const TName extends InteractionCommandName>(
command: TName,
): PostActionObservationFields<TName> {
return {
...(commandSupportsVerifyEvidence(command) ? { verify: verifyField() } : {}),
...(commandSupportsSettleObservation(command) ? settleFields() : {}),
} as PostActionObservationFields<TName>;
}
const clickFields = {
target: requiredField(interactionTargetField()),
button: enumField(CLICK_BUTTONS, 'Pointer button for platforms that support mouse buttons.'),
...selectorSnapshotFields(),
...repeatedFields(),
...postActionObservationFields('click'),
};
const pressFields = {
target: requiredField(interactionTargetField()),
...selectorSnapshotFields(),
...repeatedFields(),
...postActionObservationFields('press'),
};
const fillFields = {
target: requiredField(interactionTargetField()),
text: requiredField(stringField('Text to enter into the target.')),
delayMs: integerField('Delay between typed characters.', { min: 0 }),
recordAs: stringField(
'When script recording is armed, send text to the live app but publish it as ${VAR}. Use an uppercase replay variable name such as PASSWORD.',
),
...selectorSnapshotFields(),
...postActionObservationFields('fill'),
};
const longPressFields = {
target: requiredField(interactionTargetField()),
durationMs: integerField('Long press duration in milliseconds.', { min: 0 }),
...selectorSnapshotFields(),
...postActionObservationFields('longpress'),
};
const swipeFields = {
from: requiredField(pointField('Swipe start point.')),
to: requiredField(pointField('Swipe end point.')),
count: integerField('Number of swipe repetitions.', { min: 1, max: SWIPE_REPETITION_MAX }),
pauseMs: integerField('Pause between repeated swipes.', { min: 0, max: SWIPE_PAUSE_MAX_MS }),
pattern: enumField(SWIPE_PATTERNS),
};
const focusFields = {
x: requiredField(numberField('X coordinate.')),
y: requiredField(numberField('Y coordinate.')),
};
const typeFields = {
text: requiredField(stringField('Text to type.')),
delayMs: integerField('Delay between typed characters.', { min: 0 }),
};
const scrollFields = {
direction: requiredField(enumField(SCROLL_INPUT_DIRECTIONS)),
amount: numberField('Platform scroll amount.'),
pixels: integerField('Pixel scroll amount.', { min: 0 }),
durationMs: integerField('Scroll duration in milliseconds when the backend supports pacing.', {
min: 0,
max: SCROLL_DURATION_MAX_MS,
}),
};
// #1271 stage 2 (ADR 0012 amendment): `get`/`is`/`find` are observation-only,
// so a repair-armed heal excludes an OUT-OF-BAND one by default. Both flags
// are exposed here so the Node SDK's typed options and the MCP tool schema can
// set them, mirroring `--no-record`/`--record` on the CLI. On `find`, `record`
// is valid only for a read-only action — a mutating `find … click|fill|focus|
// type` is refused as INVALID_ARGS by the daemon (`handleFindCommands`), since
// the observe-vs-mutate split is a positional the schema cannot see.
const recordControlFields = () => ({
noRecord: booleanField('Do not record this action.'),
record: booleanField(
'Force-record this out-of-band observation into a repair-armed heal (mutually exclusive with noRecord). Authored replay steps are recorded automatically and never need this. On find, valid only for a read-only action.',
),
});
const getFields = {
format: requiredField(enumField(['text', 'attrs'] as const)),
target: requiredField(elementTargetField()),
...selectorSnapshotFields(),
...recordControlFields(),
};
const isFields = {
predicate: requiredField(
enumField(['visible', 'hidden', 'exists', 'editable', 'selected', 'focused', 'text'] as const),
),
selector: requiredField(stringField()),
value: stringField(),
...selectorSnapshotFields(),
...recordControlFields(),
};
const findFields = {
locator: enumField(FIND_LOCATORS),
query: requiredField(stringField()),
action: enumField(FIND_ACTION_VALUES),
value: stringField(),
timeoutMs: integerField(),
first: booleanField(),
last: booleanField(),
depth: integerField(),
raw: booleanField(),
...recordControlFields(),
};
const gestureFields = {
kind: requiredField(enumField(GESTURE_KINDS, 'Gesture variant.')),
direction: enumField(SCROLL_DIRECTIONS, 'Fling direction.'),
preset: enumField(SWIPE_PRESETS, 'Swipe preset.'),
origin: pointField('Gesture origin point.'),
delta: pointField('Movement delta for pan or transform gestures.'),
distance: integerField('Fling distance.', { min: 0 }),
scale: numberField('Pinch or transform scale.'),
degrees: numberField('Rotation in degrees.'),
durationMs: integerField('Pan/transform duration.', { min: 16, max: 10_000 }),
pointerCount: integerField('Pan touch pointer count (1 or 2).', { min: 1, max: 2 }),
};
export type ClickInput = InferCommandInput<typeof clickFields>;
export type PressInput = InferCommandInput<typeof pressFields>;
export type FillInput = InferCommandInput<typeof fillFields>;
export type LongPressInput = InferCommandInput<typeof longPressFields>;
export type GetInput = InferCommandInput<typeof getFields>;
export type PanInput = CommonCommandInput & PanGesturePayload;
export type FlingInput = CommonCommandInput & FlingGesturePayload;
export type SwipeGestureInput = CommonCommandInput & SwipeGesturePayload;
export type PinchInput = CommonCommandInput & PinchGesturePayload;
export type RotateInput = CommonCommandInput & RotateGesturePayload;
export type TransformInput = CommonCommandInput & TransformGesturePayload;
export type GestureInput =
| PanInput
| FlingInput
| SwipeGestureInput
| PinchInput
| RotateInput
| TransformInput;
export const interactionCommandMetadata = [
defineCommandMetadata({
name: 'click',
description: interactionCommandDescriptions.click,
inputSchema: fieldsInputSchema(clickFields),
readInput: (input) => readFieldInput(input, clickFields),
}),
defineCommandMetadata({
name: 'press',
description: interactionCommandDescriptions.press,
inputSchema: fieldsInputSchema(pressFields),
readInput: (input) => readFieldInput(input, pressFields),
}),
defineCommandMetadata({
name: 'fill',
description: interactionCommandDescriptions.fill,
inputSchema: fieldsInputSchema(fillFields),
readInput: (input) => readFieldInput(input, fillFields),
}),
defineInteractionCommandMetadata('longpress', longPressFields),
defineInteractionCommandMetadata('swipe', swipeFields),
defineInteractionCommandMetadata('focus', focusFields),
defineInteractionCommandMetadata('type', typeFields),
defineInteractionCommandMetadata('scroll', scrollFields),
defineInteractionCommandMetadata('get', getFields),
defineInteractionCommandMetadata('is', isFields),
defineInteractionCommandMetadata('find', findFields),
defineCommandMetadata({
name: 'gesture',
description: interactionCommandDescriptions.gesture,
inputSchema: fieldsInputSchema(gestureFields),
readInput: readGestureInput,
}),
] as const;
export function readGestureInput(input: unknown): GestureInput {
const record = readInputRecord(input);
return { ...readCommonInput(record), ...readGesturePayload(record) } as GestureInput;
}
function defineInteractionCommandMetadata<
const TName extends InteractionCommandName,
const TFields extends CommandFieldMap,
>(name: TName, fields: TFields) {
return defineFieldCommandMetadata(name, interactionCommandDescriptions[name], fields);
}