Repository navigation
Expand file tree
/
Copy pathtext-input.ts
More file actions
492 lines (463 loc) · 17.7 KB
/
Copy pathtext-input.ts
File metadata and controls
492 lines (463 loc) · 17.7 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/**
* How text reaches the focused Android field: the routing between provider-native injection, the
* bundled test IME, and the adb-shell fallback, plus the shell writer all three fall back to.
* Pointer, key, and gesture actions stay in `input-actions.ts`; what the field ended up holding is
* `fill-verification.ts`.
*/
import type { FillUnconfirmedVerification } from '@agent-device/contracts/fill-evidence';
import type { DeviceInfo } from '@agent-device/kernel/device';
import { AppError } from '@agent-device/kernel/errors';
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
import {
resolveAndroidAdbExecutor,
resolveAndroidAdbProvider,
resolveAndroidTextInjector,
type AndroidTextInputAction,
} from './adb-executor.ts';
import { runAndroidShell, sleep } from './adb.ts';
import type { AndroidAdbExecutor } from './adb-transport.ts';
import { getAndroidKeyboardState, type AndroidKeyboardState } from './device-input-state.ts';
import {
buildAndroidFillUnconfirmedVerification,
completeAndroidFillVerification,
readAndroidFillTargetBeforeMutation,
verifyAndroidFilledText,
type AndroidFillVerification,
} from './fill-verification.ts';
import {
ANDROID_IME_HELPER_SERVICE_COMPONENT,
clearAndroidImeHelperText,
getAndroidImeHelperDeviceKey,
isAndroidImeHelperPackage,
rebindAndroidImeHelper,
selectAndroidImeHelperArtifact,
sendAndroidImeHelperText,
} from './ime-helper.ts';
import { isAndroidTestImeActive } from './ime-lifecycle.ts';
import { readAndroidDefaultInputMethod } from './ime-settings-record.ts';
import { activeTestImeDevices } from './ime-state.ts';
import { focusAndroid } from './input-actions.ts';
import type { AndroidHelperSessionOptions } from './snapshot-helper-types.ts';
/**
* `input text` truncates long strings in some app/IME states (#531), so ASCII is written in short
* chunks rather than one command. Do not raise this to buy back spawns — the IME helper's batch
* broadcast is the lever for that, and it does not truncate.
*/
const ANDROID_INPUT_TEXT_CHUNK_SIZE = 8;
export async function typeAndroid(device: DeviceInfo, text: string, delayMs = 0): Promise<void> {
const providerText = resolveAndroidTextInjector(device);
if (providerText) {
await providerText({ action: 'type', text, delayMs });
emitAndroidTextDiagnostic('type', 'provider-native', text);
return;
}
const channel = await admitAndroidTextChannel(device, 'type', text);
if (channel.backend === 'test-ime') {
await typeAndroidImeHelper(device, channel.packageName, text, delayMs);
return;
}
if (delayMs > 0 && Array.from(text).length > 1) {
await typeAndroidShell(device, { action: 'type', text, chunkSize: 1, delayMs });
return;
}
await typeAndroidShell(device, {
action: 'type',
text,
chunkSize: ANDROID_INPUT_TEXT_CHUNK_SIZE,
delayMs: 0,
});
}
export async function fillAndroid(
device: DeviceInfo,
x: number,
y: number,
text: string,
delayMs = 0,
helper: AndroidHelperSessionOptions = {},
): Promise<FillUnconfirmedVerification | void> {
const beforeTarget = await readAndroidFillTargetBeforeMutation(device, x, y, helper);
const providerText = resolveAndroidTextInjector(device);
if (providerText) {
await providerText({ action: 'fill', target: { x, y }, text, delayMs });
emitAndroidTextDiagnostic('fill', 'provider-native', text);
const verification = await verifyAndroidFilledText(device, x, y, text, helper);
return completeAndroidFillVerification(text, beforeTarget, verification);
}
let lastVerification: AndroidFillVerification | null = null;
for (const attempt of buildAndroidShellFillAttempts(delayMs)) {
await focusAndroid(device, x, y);
const channel = await admitAndroidTextChannel(device, 'fill', text);
if (channel.backend === 'test-ime') {
const verification = await fillAndroidImeHelper(
device,
channel.packageName,
x,
y,
text,
beforeTarget,
helper,
);
return completeAndroidFillVerification(text, beforeTarget, verification);
}
const verification = await runAndroidShellFillAttempt(
device,
{ x, y, text, beforeTarget, attempt },
helper,
);
lastVerification = verification;
if (verification.ok) return;
if (verification.reason === 'ime_capture') {
return completeAndroidFillVerification(text, beforeTarget, verification);
}
const unconfirmed = buildAndroidFillUnconfirmedVerification(text, beforeTarget, verification);
if (unconfirmed) return unconfirmed;
}
return completeAndroidFillVerification(text, beforeTarget, lastVerification);
}
type AndroidShellFillAttempt = {
clearPadding: number;
minClear: number;
maxClear: number;
chunkSize: number;
inputDelayMs: number;
};
function buildAndroidShellFillAttempts(delayMs: number): AndroidShellFillAttempt[] {
return [
{
clearPadding: 12,
minClear: 8,
maxClear: 48,
chunkSize: delayMs > 0 ? 1 : ANDROID_INPUT_TEXT_CHUNK_SIZE,
inputDelayMs: delayMs,
},
{
clearPadding: 24,
minClear: 16,
maxClear: 96,
chunkSize: delayMs > 0 ? 1 : 4,
inputDelayMs: delayMs > 0 ? delayMs : 15,
},
];
}
/** One clear-then-type pass on the adb-shell channel, verified against the requested text. */
async function runAndroidShellFillAttempt(
device: DeviceInfo,
input: {
x: number;
y: number;
text: string;
beforeTarget: AndroidFillVerification['targetInput'];
attempt: AndroidShellFillAttempt;
},
helper: AndroidHelperSessionOptions,
): Promise<AndroidFillVerification> {
const { x, y, text, beforeTarget, attempt } = input;
await clearFocusedText(device, androidShellClearCount(text, beforeTarget, attempt));
await typeAndroidShell(device, {
action: 'fill',
text,
chunkSize: attempt.chunkSize,
delayMs: attempt.inputDelayMs,
});
return verifyAndroidFilledText(device, x, y, text, helper);
}
/**
* The delete burst must cover the OLD value. Sizing the empty clear (#2063) from the incoming
* text would send the minimum burst and leave residue in any longer field, so it sizes from the
* pre-mutation read instead — and assumes the attempt's worst case when that read could not see
* the field.
*/
function androidShellClearCount(
text: string,
beforeTarget: AndroidFillVerification['targetInput'],
attempt: AndroidShellFillAttempt,
): number {
const textCodePointLength = Array.from(text).length;
const clearBase =
textCodePointLength > 0
? textCodePointLength + attempt.clearPadding
: beforeTarget?.text
? Array.from(beforeTarget.text).length + attempt.clearPadding
: attempt.maxClear;
return clampCount(clearBase, attempt.minClear, attempt.maxClear);
}
/**
* Which channel writes this text on this device, already admitted: the helper's package for the
* broadcast, or the adb-shell channel once this text and the focused input pass its asserts. The
* helper IME serves whenever it is the device's active input method — because this process
* switched to it (the activation cache answers without a device read, off the packaged artifact's
* manifest), or because a previous run left it active and the input-method read observes it. That
* read precedes the shell asserts by construction: the ASCII limit belongs to the shell channel
* alone, and the helper's batch broadcast carries any Unicode in one spawn instead of ceil(n/8)
* `input text` chunks.
*/
async function admitAndroidTextChannel(
device: DeviceInfo,
action: AndroidTextInputAction,
text: string,
): Promise<{ backend: 'test-ime'; packageName: string } | { backend: 'adb-shell' }> {
if (isAndroidTestImeActive(device)) {
const artifact = await selectAndroidImeHelperArtifact(resolveAndroidAdbProvider(device));
return { backend: 'test-ime', packageName: artifact.manifest.packageName };
}
const inputState = await readAndroidShellTextInputState(device, action);
const packageName = inputState?.inputMethodPackage;
if (isAndroidImeHelperPackage(packageName)) {
return { backend: 'test-ime', packageName };
}
assertAndroidShellTextSupported(text);
assertAndroidShellInputIsAppOwned(inputState, action);
return { backend: 'adb-shell' };
}
async function typeAndroidImeHelper(
device: DeviceInfo,
packageName: string,
text: string,
delayMs: number,
): Promise<void> {
const adb = resolveAndroidAdbExecutor(device);
const parts = text.split('\n');
for (const [partIndex, part] of parts.entries()) {
const chunks = delayMs > 0 ? chunkAndroidInputText(part, 1) : [part];
for (const [chunkIndex, chunk] of chunks.entries()) {
if (chunk) await sendAndroidImeHelperText(adb, packageName, chunk);
if (delayMs > 0 && (chunkIndex + 1 < chunks.length || partIndex + 1 < parts.length)) {
await sleep(delayMs);
}
}
if (partIndex + 1 < parts.length) {
await runAndroidShell(device, ['input', 'keyevent', 'ENTER']);
}
}
emitAndroidTextDiagnostic('type', 'test-ime', text);
}
async function fillAndroidImeHelper(
device: DeviceInfo,
packageName: string,
x: number,
y: number,
text: string,
beforeTarget: AndroidFillVerification['targetInput'],
helper: AndroidHelperSessionOptions,
): Promise<AndroidFillVerification> {
const adb = resolveAndroidAdbExecutor(device);
let lastVerification: AndroidFillVerification | null = null;
// The caller focused the target while resolving the channel; the retry re-focuses because it
// covers the rare not-yet-bound InputConnection right after focus. A commit none of which reached
// the field may also have gone to a stale input session, which only a rebind of the IME replaces.
for (let attempt = 0; attempt < 2; attempt += 1) {
if (
lastVerification &&
!(await prepareAndroidImeHelperRetry(device, adb, x, y, lastVerification, beforeTarget))
) {
break;
}
await clearAndroidImeHelperText(adb, packageName);
if (text) await sendAndroidImeHelperText(adb, packageName, text);
const verification = await verifyAndroidFilledText(device, x, y, text, helper);
lastVerification = verification;
if (verification.ok) break;
if (buildAndroidFillUnconfirmedVerification(text, beforeTarget, verification)) break;
}
emitAndroidTextDiagnostic('fill', 'test-ime', text);
return lastVerification as AndroidFillVerification;
}
/**
* Readies the field for the helper's retry: rebinds the IME when none of the last commit reached the
* field, then re-focuses it. Answers `false` when the rebind left another IME selected, since a retry
* would broadcast to an IME that holds no session.
*/
async function prepareAndroidImeHelperRetry(
device: DeviceInfo,
adb: AndroidAdbExecutor,
x: number,
y: number,
lastVerification: AndroidFillVerification,
beforeTarget: AndroidFillVerification['targetInput'],
): Promise<boolean> {
if (isAndroidImeCommitDropped(lastVerification, beforeTarget)) {
if (!(await rebindAndroidImeHelperChecked(device, adb))) return false;
}
await focusAndroid(device, x, y);
return true;
}
/** Whether none of a helper commit reached the field: it shows its hint, or the value it held before. */
function isAndroidImeCommitDropped(
verification: AndroidFillVerification,
beforeTarget: AndroidFillVerification['targetInput'],
): boolean {
if (verification.actualInput?.hintShowing === true) return true;
return beforeTarget?.text != null && verification.actual === beforeTarget.text;
}
/**
* Rebinds the test IME and confirms it is still the selected IME. A rebind that left another IME
* selected takes the device off the helper route, so the next text entry activates the helper again
* through the checked activation path instead of broadcasting to an IME that holds no session.
*/
async function rebindAndroidImeHelperChecked(
device: DeviceInfo,
adb: AndroidAdbExecutor,
): Promise<boolean> {
emitDiagnostic({ level: 'warn', phase: 'android_test_ime_rebind', data: { device: device.id } });
await rebindAndroidImeHelper(adb);
if ((await readAndroidDefaultInputMethod(adb)) === ANDROID_IME_HELPER_SERVICE_COMPONENT)
return true;
activeTestImeDevices.delete(getAndroidImeHelperDeviceKey(device));
emitDiagnostic({
level: 'warn',
phase: 'android_test_ime_rebind_failed',
data: { device: device.id },
});
return false;
}
async function typeAndroidShell(
device: DeviceInfo,
options: { action: AndroidTextInputAction; text: string; chunkSize: number; delayMs: number },
): Promise<void> {
const parts = options.text.split('\n');
for (const [partIndex, part] of parts.entries()) {
const chunks = chunkAndroidInputText(part, options.chunkSize);
for (const [chunkIndex, chunk] of chunks.entries()) {
await typeAndroidShellChunk(device, chunk);
if (options.delayMs > 0 && (chunkIndex + 1 < chunks.length || partIndex + 1 < parts.length)) {
await sleep(options.delayMs);
}
}
if (partIndex + 1 < parts.length) {
await runAndroidShell(device, ['input', 'keyevent', 'ENTER']);
}
}
emitAndroidTextDiagnostic(options.action, 'adb-shell', options.text);
}
async function typeAndroidShellChunk(device: DeviceInfo, text: string): Promise<void> {
if (!text) return;
try {
await runAndroidShell(device, ['input', 'text', encodeAndroidInputText(text)]);
} catch (error) {
if (isAndroidInputTextUnsupported(error)) {
throw unsupportedAndroidShellTextError(text, error);
}
throw error;
}
}
async function clearFocusedText(device: DeviceInfo, count: number): Promise<void> {
const deletes = Math.max(0, count);
await runAndroidShell(device, ['input', 'keyevent', 'KEYCODE_MOVE_END'], {
allowFailure: true,
});
const batchSize = 24;
for (let i = 0; i < deletes; i += batchSize) {
const size = Math.min(batchSize, deletes - i);
await runAndroidShell(device, ['input', 'keyevent', ...Array(size).fill('KEYCODE_DEL')], {
allowFailure: true,
});
}
}
/**
* Reads the device's input-method state for a text-entry decision, or `null` when the probe itself
* fails. A failed probe is not a refusal: the shell path has always continued without this evidence
* rather than blocking text entry on a diagnostic read.
*/
async function readAndroidShellTextInputState(
device: DeviceInfo,
action: AndroidTextInputAction,
): Promise<AndroidKeyboardState | null> {
try {
return await getAndroidKeyboardState(device);
} catch (error) {
emitDiagnostic({
level: 'warn',
phase: 'android_input_ownership_probe_failed',
data: {
action,
error: error instanceof Error ? error.message : String(error),
},
});
return null;
}
}
function assertAndroidShellInputIsAppOwned(
state: AndroidKeyboardState | null,
action: AndroidTextInputAction,
): void {
if (!state || state.inputOwner !== 'ime') return;
throw new AppError(
'COMMAND_FAILED',
'KEYBOARD_OVERLAY_BLOCKING: Android text input is blocked because the focused input belongs to the active keyboard/IME.',
{
failureReason: 'ime_capture',
action,
inputOwner: state.inputOwner,
inputType: state.inputType,
type: state.type,
inputMethodPackage: state.inputMethodPackage,
focusedPackage: state.focusedPackage,
focusedResourceId: state.focusedResourceId,
nextAction:
'Focused input appears to be owned by the keyboard/IME; dismiss or change the IME before retrying text entry.',
},
);
}
function assertAndroidShellTextSupported(text: string): void {
if (isAndroidShellTextSupported(text)) return;
throw unsupportedAndroidShellTextError(text);
}
function isAndroidShellTextSupported(text: string): boolean {
for (const char of text) {
const code = char.codePointAt(0);
if (code === undefined) continue;
if (char === '\n') continue;
if (code < 0x20 || code > 0x7e) {
return false;
}
}
return true;
}
function encodeAndroidInputText(text: string): string {
// Android shell input uses `%s` as the escaped token for spaces.
return text.replaceAll(' ', '%s');
}
function isAndroidInputTextUnsupported(error: unknown): boolean {
if (!(error instanceof AppError)) return false;
if (error.code !== 'COMMAND_FAILED') return false;
const rawStderr = error.details?.stderr;
const stderr = (typeof rawStderr === 'string' ? rawStderr : '').toLowerCase();
if (stderr.includes("exception occurred while executing 'text'")) return true;
if (stderr.includes('nullpointerexception') && stderr.includes('inputshellcommand.sendtext'))
return true;
return false;
}
function unsupportedAndroidShellTextError(text: string, cause?: unknown): AppError {
return new AppError(
'COMMAND_FAILED',
'Android text input requires provider-native text injection or the bundled test IME helper for non-ASCII/control characters; the adb-shell fallback supports ASCII text only. On emulators the test IME activates automatically; on real devices pass `open --test-ime` to enable it (see `agent-device doctor` for the current IME state).',
{
backend: 'adb-shell',
textLength: Array.from(text).length,
textPreview: text.slice(0, 32),
},
cause instanceof Error ? cause : undefined,
);
}
function chunkAndroidInputText(text: string, chunkSize: number): string[] {
const size = Math.max(1, Math.floor(chunkSize));
const chunks: string[] = [];
const chars = Array.from(text);
for (let i = 0; i < chars.length; i += size) {
chunks.push(chars.slice(i, i + size).join(''));
}
return chunks.length > 0 ? chunks : [''];
}
function emitAndroidTextDiagnostic(
action: AndroidTextInputAction,
backend: 'provider-native' | 'adb-shell' | 'test-ime',
text: string,
): void {
emitDiagnostic({
phase: 'android_text_injection',
data: { action, backend, textLength: Array.from(text).length },
});
}
function clampCount(value: number, min: number, max: number): number {
return Math.max(min, Math.min(max, value));
}