@autifyhq/aximo-cli 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/README.md +1 -1
  2. package/dist/commands/device/connect.js +19 -1
  3. package/dist/commands/device/doctor.d.ts +2 -0
  4. package/dist/commands/device/doctor.js +37 -10
  5. package/dist/commands/device/doctor.test.js +26 -1
  6. package/dist/commands/mcp/server.test.js +3 -1
  7. package/dist/commands/mcp/tools.js +28 -0
  8. package/dist/commands/mcp/tools.test.js +90 -0
  9. package/dist/commands/mobile/connect-core.d.ts +2 -2
  10. package/dist/commands/mobile/connect-core.js +35 -2
  11. package/dist/commands/mobile/signing-consent.js +5 -11
  12. package/dist/desktop-server/preflight.d.ts +43 -0
  13. package/dist/desktop-server/preflight.js +78 -0
  14. package/dist/desktop-server/preflight.test.d.ts +1 -0
  15. package/dist/desktop-server/preflight.test.js +115 -0
  16. package/dist/mobile-control/appium-controller.d.ts +24 -0
  17. package/dist/mobile-control/appium-controller.js +218 -127
  18. package/dist/mobile-control/appium-controller.test.js +102 -0
  19. package/dist/mobile-control/frame-sender.d.ts +34 -0
  20. package/dist/mobile-control/frame-sender.js +96 -0
  21. package/dist/mobile-control/frame-sender.test.d.ts +1 -0
  22. package/dist/mobile-control/frame-sender.test.js +108 -0
  23. package/dist/mobile-control/mjpeg-stream.d.ts +43 -0
  24. package/dist/mobile-control/mjpeg-stream.js +160 -0
  25. package/dist/mobile-control/mjpeg-stream.test.d.ts +1 -0
  26. package/dist/mobile-control/mjpeg-stream.test.js +93 -0
  27. package/dist/mobile-control/tunnel.d.ts +10 -1
  28. package/dist/mobile-control/tunnel.js +20 -2
  29. package/dist/mobile-control/tunnel.test.js +33 -0
  30. package/dist/mobile-control/webdriver.d.ts +2 -0
  31. package/dist/mobile-control/webdriver.js +6 -0
  32. package/dist/prompt.d.ts +11 -0
  33. package/dist/prompt.js +19 -0
  34. package/dist/types.d.ts +17 -0
  35. package/package.json +1 -1
@@ -0,0 +1,115 @@
1
+ import { mkdtempSync, writeFileSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { beforeAll, describe, expect, it, vi } from "vitest";
5
+ import { confirmProceed, formatPreflightLines, hasBlockingChecks, runLocalPreflight, } from "./preflight.js";
6
+ const REPORT = {
7
+ agent: { platform: "Darwin" },
8
+ checks: [
9
+ {
10
+ name: "capture target",
11
+ status: "info",
12
+ message: "capturing the primary monitor (2560x1440)",
13
+ },
14
+ {
15
+ name: "accessibility permission",
16
+ status: "fail",
17
+ message: "not granted",
18
+ hint: "System Settings → Privacy & Security → Accessibility",
19
+ },
20
+ ],
21
+ };
22
+ const HEALTHY = {
23
+ checks: [{ name: "input source", status: "pass", message: "com.apple.keylayout.ABC" }],
24
+ };
25
+ let pythonPath;
26
+ let scriptPath;
27
+ beforeAll(() => {
28
+ const dir = mkdtempSync(join(tmpdir(), "aximo-preflight-"));
29
+ pythonPath = join(dir, "python3");
30
+ scriptPath = join(dir, "remote.py");
31
+ writeFileSync(pythonPath, "");
32
+ writeFileSync(scriptPath, "");
33
+ });
34
+ function spawnReturning(overrides) {
35
+ return vi.fn(() => ({
36
+ status: 0,
37
+ signal: null,
38
+ pid: 1,
39
+ output: [],
40
+ stdout: "",
41
+ stderr: "",
42
+ ...overrides,
43
+ }));
44
+ }
45
+ describe("runLocalPreflight", () => {
46
+ it("returns the report the agent printed", () => {
47
+ const spawn = spawnReturning({ stdout: JSON.stringify(REPORT) });
48
+ expect(runLocalPreflight({ pythonPath, scriptPath, spawn })).toEqual({
49
+ kind: "ok",
50
+ report: REPORT,
51
+ });
52
+ expect(spawn).toHaveBeenCalledWith(pythonPath, [scriptPath, "--preflight"], expect.objectContaining({ encoding: "utf-8" }));
53
+ });
54
+ // A server still serving an agent script without --preflight exits on the
55
+ // missing GATEWAY_URL rather than printing a report.
56
+ it("reports not-installed when the agent script predates --preflight", () => {
57
+ const spawn = spawnReturning({ status: 1, stderr: "GATEWAY_URL is required" });
58
+ expect(runLocalPreflight({ pythonPath, scriptPath, spawn })).toEqual({
59
+ kind: "not-installed",
60
+ });
61
+ });
62
+ // A crash is not the same as "not set up yet", and doctor must not conflate them.
63
+ it("reports the failure when the agent runs but cannot answer", () => {
64
+ expect(runLocalPreflight({
65
+ pythonPath,
66
+ scriptPath,
67
+ spawn: spawnReturning({ status: 1, stderr: "ImportError: no module named mss" }),
68
+ })).toEqual({ kind: "error", detail: "ImportError: no module named mss" });
69
+ });
70
+ it("reports an error when stdout is not a report", () => {
71
+ for (const stdout of ["boom", "[]", JSON.stringify({ checks: [{ name: "x" }] })]) {
72
+ expect(runLocalPreflight({ pythonPath, scriptPath, spawn: spawnReturning({ stdout }) })).toEqual({ kind: "error", detail: expect.stringContaining("readable report") });
73
+ }
74
+ });
75
+ it("reports not-installed without spawning when python is missing", () => {
76
+ const spawn = spawnReturning({ stdout: JSON.stringify(REPORT) });
77
+ expect(runLocalPreflight({ pythonPath: join(tmpdir(), "no-such-python"), scriptPath, spawn })).toEqual({ kind: "not-installed" });
78
+ expect(spawn).not.toHaveBeenCalled();
79
+ });
80
+ });
81
+ describe("formatPreflightLines", () => {
82
+ it("marks each check and indents its hint", () => {
83
+ const lines = formatPreflightLines(REPORT);
84
+ expect(lines[0]).toBe("Desktop environment:");
85
+ expect(lines[1]).toContain("· capture target: capturing the primary monitor");
86
+ expect(lines[2]).toContain("✘ accessibility permission: not granted");
87
+ expect(lines[2]).toContain("→ System Settings");
88
+ });
89
+ });
90
+ describe("hasBlockingChecks", () => {
91
+ it("is true only when something warns or fails", () => {
92
+ expect(hasBlockingChecks(REPORT)).toBe(true);
93
+ expect(hasBlockingChecks(HEALTHY)).toBe(false);
94
+ });
95
+ });
96
+ describe("confirmProceed", () => {
97
+ it("does not ask when every check is fine", async () => {
98
+ const ask = vi.fn();
99
+ expect(await confirmProceed(HEALTHY, { interactive: true, ask })).toBe(true);
100
+ expect(ask).not.toHaveBeenCalled();
101
+ });
102
+ it("proceeds on an empty answer", async () => {
103
+ expect(await confirmProceed(REPORT, { interactive: true, ask: async () => "" })).toBe(true);
104
+ });
105
+ it("stops on a declined prompt", async () => {
106
+ expect(await confirmProceed(REPORT, { interactive: true, ask: async () => "n" })).toBe(false);
107
+ expect(await confirmProceed(REPORT, { interactive: true, ask: async () => "No" })).toBe(false);
108
+ });
109
+ // Headless/CI: nobody can answer, so a prompt would read as a hang.
110
+ it("never asks without a terminal", async () => {
111
+ const ask = vi.fn();
112
+ expect(await confirmProceed(REPORT, { interactive: false, ask })).toBe(true);
113
+ expect(ask).not.toHaveBeenCalled();
114
+ });
115
+ });
@@ -12,6 +12,12 @@ export interface AppiumControllerOptions {
12
12
  * isn't picked long before it is bound.
13
13
  */
14
14
  wdaLocalPort?: number | (() => Promise<number>);
15
+ /**
16
+ * Local port the driver forwards to the on-device MJPEG server (WDA / the
17
+ * UiAutomator2 server). When set, the session is created with the MJPEG
18
+ * server enabled and conservative capture settings applied.
19
+ */
20
+ mjpegServerPort?: number | (() => Promise<number>);
15
21
  transport?: WebDriverTransport;
16
22
  }
17
23
  export declare class AppiumController implements MobileController {
@@ -20,12 +26,28 @@ export declare class AppiumController implements MobileController {
20
26
  private readonly target;
21
27
  private readonly iosSigning;
22
28
  private readonly wdaLocalPort;
29
+ private readonly mjpegServerPort;
23
30
  private resolvedWdaPort;
31
+ private resolvedMjpegPort;
24
32
  private sessionReady;
25
33
  private scaling;
26
34
  constructor(opts: AppiumControllerOptions);
27
35
  private capabilities;
28
36
  private ensureSession;
37
+ private static readonly MJPEG_SETTINGS;
38
+ private applyMjpegSettings;
39
+ private resetSession;
40
+ /**
41
+ * Run a command against a live session, rebuilding the session once if the
42
+ * cached one has died (WDA killed for CPU, device rebooted, adb dropped).
43
+ * Without this, one on-device crash permanently wedges every later command —
44
+ * including the screenshot fallback — until the CLI is restarted.
45
+ */
46
+ private withSession;
47
+ /** Bootstrap (or rebuild) the device session without running a command. */
48
+ ensureReady(): Promise<void>;
49
+ /** Local port of the driver-forwarded MJPEG server, once the session is up. */
50
+ getMjpegPort(): number | undefined;
29
51
  private scale;
30
52
  private toPoint;
31
53
  private pointerAction;
@@ -35,11 +57,13 @@ export declare class AppiumController implements MobileController {
35
57
  longPress(x: number, y: number, durationMs: number): Promise<void>;
36
58
  swipe(x1: number, y1: number, x2: number, y2: number, durationMs: number): Promise<void>;
37
59
  pinchZoom(x: number, y: number, scale: number, durationMs: number): Promise<void>;
60
+ private pinchZoomOnce;
38
61
  private performPointer;
39
62
  typeText(text: string): Promise<ActionResult>;
40
63
  private activeElementOrNull;
41
64
  clearText(): Promise<ActionResult>;
42
65
  pressKey(key: string): Promise<ActionResult>;
66
+ private pressKeyOnce;
43
67
  launchApp(app: string): Promise<ActionResult>;
44
68
  findElement(text: string): Promise<ActionResult>;
45
69
  private locate;
@@ -24,13 +24,22 @@ const W3C_KEYS = {
24
24
  escape: "\uE00C",
25
25
  space: " ",
26
26
  };
27
+ // A dead-but-cached Appium session (WDA killed for CPU, phone rebooted, adb
28
+ // dropped) answers every command with one of these. Distinct from command
29
+ // failures on a live session, which must not trigger a session rebuild.
30
+ const SESSION_DEAD_PATTERN = /invalid session id|terminated or not started|session deleted|session not found|has already finished/i;
31
+ function isSessionDeadError(err) {
32
+ return err instanceof Error && SESSION_DEAD_PATTERN.test(err.message);
33
+ }
27
34
  export class AppiumController {
28
35
  session;
29
36
  platform;
30
37
  target;
31
38
  iosSigning;
32
39
  wdaLocalPort;
40
+ mjpegServerPort;
33
41
  resolvedWdaPort;
42
+ resolvedMjpegPort;
34
43
  sessionReady = null;
35
44
  scaling = null;
36
45
  constructor(opts) {
@@ -38,10 +47,14 @@ export class AppiumController {
38
47
  this.target = opts.target;
39
48
  this.iosSigning = opts.iosSigning;
40
49
  this.wdaLocalPort = opts.wdaLocalPort;
50
+ this.mjpegServerPort = opts.mjpegServerPort;
41
51
  const transport = opts.transport ?? fetchTransport(opts.endpoint);
42
52
  this.session = new WebDriverSession(transport);
43
53
  }
44
54
  capabilities() {
55
+ const mjpeg = this.resolvedMjpegPort
56
+ ? { "appium:mjpegServerPort": this.resolvedMjpegPort }
57
+ : {};
45
58
  if (this.platform === "android") {
46
59
  return {
47
60
  platformName: "Android",
@@ -50,6 +63,7 @@ export class AppiumController {
50
63
  "appium:noReset": true,
51
64
  "appium:autoLaunch": false,
52
65
  "appium:newCommandTimeout": 0,
66
+ ...mjpeg,
53
67
  };
54
68
  }
55
69
  return {
@@ -59,6 +73,7 @@ export class AppiumController {
59
73
  "appium:newCommandTimeout": 0,
60
74
  // Unique local->device WDA port so a second connected phone doesn't collide on 8100.
61
75
  ...(this.resolvedWdaPort ? { "appium:wdaLocalPort": this.resolvedWdaPort } : {}),
76
+ ...mjpeg,
62
77
  // WDA code-signing caps for physical iPhones (empty unless a team id is set).
63
78
  ...(this.iosSigning ? iosSigningCapabilities(this.iosSigning) : {}),
64
79
  };
@@ -68,11 +83,14 @@ export class AppiumController {
68
83
  this.sessionReady = (async () => {
69
84
  this.resolvedWdaPort =
70
85
  typeof this.wdaLocalPort === "function" ? await this.wdaLocalPort() : this.wdaLocalPort;
86
+ this.resolvedMjpegPort =
87
+ typeof this.mjpegServerPort === "function"
88
+ ? await this.mjpegServerPort()
89
+ : this.mjpegServerPort;
71
90
  await this.session.createSession(this.capabilities());
91
+ await this.applyMjpegSettings();
72
92
  })().catch(async (err) => {
73
- this.sessionReady = null;
74
- // A retry must re-pick a free port — the one resolved here may be taken by then.
75
- this.resolvedWdaPort = undefined;
93
+ this.resetSession();
76
94
  // XCUITest reports session-start failures generically ("xcodebuild failed
77
95
  // with code 65"); enrich with the real reason from the WDA .xcresult and
78
96
  // an actionable hint so the user isn't left with an opaque error.
@@ -84,6 +102,61 @@ export class AppiumController {
84
102
  }
85
103
  return this.sessionReady;
86
104
  }
105
+ // Conservative stream shape until runtime adaptation lands: iOS defaults to
106
+ // scalingFactor 100 (full-res frames) which can drive WDA CPU high enough for
107
+ // the OS to kill it, so both platforms are pinned to the same explicit values.
108
+ static MJPEG_SETTINGS = {
109
+ mjpegServerFramerate: 10,
110
+ mjpegScalingFactor: 50,
111
+ mjpegServerScreenshotQuality: 40,
112
+ };
113
+ async applyMjpegSettings() {
114
+ if (!this.resolvedMjpegPort)
115
+ return;
116
+ try {
117
+ await this.session.updateSettings(AppiumController.MJPEG_SETTINGS);
118
+ }
119
+ catch (err) {
120
+ console.error(`MJPEG capture settings were not applied (streaming continues with driver defaults): ${err instanceof Error ? err.message : String(err)}`);
121
+ }
122
+ }
123
+ resetSession() {
124
+ this.sessionReady = null;
125
+ this.scaling = null;
126
+ // A retry must re-pick free ports — the ones resolved here may be taken by then.
127
+ this.resolvedWdaPort = undefined;
128
+ this.resolvedMjpegPort = undefined;
129
+ }
130
+ /**
131
+ * Run a command against a live session, rebuilding the session once if the
132
+ * cached one has died (WDA killed for CPU, device rebooted, adb dropped).
133
+ * Without this, one on-device crash permanently wedges every later command —
134
+ * including the screenshot fallback — until the CLI is restarted.
135
+ */
136
+ async withSession(fn) {
137
+ const ready = this.ensureSession();
138
+ await ready;
139
+ try {
140
+ return await fn();
141
+ }
142
+ catch (err) {
143
+ if (!isSessionDeadError(err))
144
+ throw err;
145
+ // Only reset if no concurrent command already rebuilt the session.
146
+ if (this.sessionReady === ready)
147
+ this.resetSession();
148
+ await this.ensureSession();
149
+ return await fn();
150
+ }
151
+ }
152
+ /** Bootstrap (or rebuild) the device session without running a command. */
153
+ async ensureReady() {
154
+ await this.ensureSession();
155
+ }
156
+ /** Local port of the driver-forwarded MJPEG server, once the session is up. */
157
+ getMjpegPort() {
158
+ return this.resolvedMjpegPort;
159
+ }
87
160
  // Incoming pixel coords scale to logical points: iOS divides by DPR, Android ~= 1.
88
161
  async scale() {
89
162
  if (this.scaling)
@@ -109,57 +182,62 @@ export class AppiumController {
109
182
  ];
110
183
  }
111
184
  async screenshot() {
112
- await this.ensureSession();
113
- return this.session.screenshot();
185
+ return this.withSession(() => this.session.screenshot());
114
186
  }
115
187
  async tap(x, y) {
116
- await this.ensureSession();
117
- const [px, py] = await this.toPoint(x, y);
118
- await this.performPointer([
119
- { type: "pointerMove", duration: 0, x: px, y: py },
120
- { type: "pointerDown", button: 0 },
121
- { type: "pause", duration: 50 },
122
- { type: "pointerUp", button: 0 },
123
- ]);
188
+ await this.withSession(async () => {
189
+ const [px, py] = await this.toPoint(x, y);
190
+ await this.performPointer([
191
+ { type: "pointerMove", duration: 0, x: px, y: py },
192
+ { type: "pointerDown", button: 0 },
193
+ { type: "pause", duration: 50 },
194
+ { type: "pointerUp", button: 0 },
195
+ ]);
196
+ });
124
197
  }
125
198
  async doubleTap(x, y) {
126
- await this.ensureSession();
127
- const [px, py] = await this.toPoint(x, y);
128
- await this.performPointer([
129
- { type: "pointerMove", duration: 0, x: px, y: py },
130
- { type: "pointerDown", button: 0 },
131
- { type: "pause", duration: 50 },
132
- { type: "pointerUp", button: 0 },
133
- { type: "pause", duration: 50 },
134
- { type: "pointerDown", button: 0 },
135
- { type: "pause", duration: 50 },
136
- { type: "pointerUp", button: 0 },
137
- ]);
199
+ await this.withSession(async () => {
200
+ const [px, py] = await this.toPoint(x, y);
201
+ await this.performPointer([
202
+ { type: "pointerMove", duration: 0, x: px, y: py },
203
+ { type: "pointerDown", button: 0 },
204
+ { type: "pause", duration: 50 },
205
+ { type: "pointerUp", button: 0 },
206
+ { type: "pause", duration: 50 },
207
+ { type: "pointerDown", button: 0 },
208
+ { type: "pause", duration: 50 },
209
+ { type: "pointerUp", button: 0 },
210
+ ]);
211
+ });
138
212
  }
139
213
  async longPress(x, y, durationMs) {
140
- await this.ensureSession();
141
- const [px, py] = await this.toPoint(x, y);
142
- await this.performPointer([
143
- { type: "pointerMove", duration: 0, x: px, y: py },
144
- { type: "pointerDown", button: 0 },
145
- { type: "pause", duration: Math.max(0, Math.round(durationMs)) },
146
- { type: "pointerUp", button: 0 },
147
- ]);
214
+ await this.withSession(async () => {
215
+ const [px, py] = await this.toPoint(x, y);
216
+ await this.performPointer([
217
+ { type: "pointerMove", duration: 0, x: px, y: py },
218
+ { type: "pointerDown", button: 0 },
219
+ { type: "pause", duration: Math.max(0, Math.round(durationMs)) },
220
+ { type: "pointerUp", button: 0 },
221
+ ]);
222
+ });
148
223
  }
149
224
  async swipe(x1, y1, x2, y2, durationMs) {
150
- await this.ensureSession();
151
- const [px1, py1] = await this.toPoint(x1, y1);
152
- const [px2, py2] = await this.toPoint(x2, y2);
153
- await this.performPointer([
154
- { type: "pointerMove", duration: 0, x: px1, y: py1 },
155
- { type: "pointerDown", button: 0 },
156
- { type: "pointerMove", duration: Math.max(0, Math.round(durationMs)), x: px2, y: py2 },
157
- { type: "pointerUp", button: 0 },
158
- ]);
225
+ await this.withSession(async () => {
226
+ const [px1, py1] = await this.toPoint(x1, y1);
227
+ const [px2, py2] = await this.toPoint(x2, y2);
228
+ await this.performPointer([
229
+ { type: "pointerMove", duration: 0, x: px1, y: py1 },
230
+ { type: "pointerDown", button: 0 },
231
+ { type: "pointerMove", duration: Math.max(0, Math.round(durationMs)), x: px2, y: py2 },
232
+ { type: "pointerUp", button: 0 },
233
+ ]);
234
+ });
159
235
  }
160
236
  // Two-finger vertical pinch: scale < 1 pinches in (zoom out), > 1 spreads (zoom in).
161
237
  async pinchZoom(x, y, scale, durationMs) {
162
- await this.ensureSession();
238
+ await this.withSession(() => this.pinchZoomOnce(x, y, scale, durationMs));
239
+ }
240
+ async pinchZoomOnce(x, y, scale, durationMs) {
163
241
  const [cx, cy] = await this.toPoint(x, y);
164
242
  const startOffset = 100;
165
243
  const endOffset = startOffset * scale;
@@ -187,31 +265,32 @@ export class AppiumController {
187
265
  await this.session.releaseActions();
188
266
  }
189
267
  async typeText(text) {
190
- await this.ensureSession();
191
268
  try {
192
- if (this.platform === "android") {
193
- if (ANDROID_INPUT_TEXT_UNSAFE.test(text)) {
194
- const active = await this.activeElementOrNull();
195
- if (!active) {
196
- return {
197
- ok: false,
198
- error: "Cannot type special or non-ASCII characters: no input field is focused. Tap the field first.",
199
- };
269
+ return await this.withSession(async () => {
270
+ if (this.platform === "android") {
271
+ if (ANDROID_INPUT_TEXT_UNSAFE.test(text)) {
272
+ const active = await this.activeElementOrNull();
273
+ if (!active) {
274
+ return {
275
+ ok: false,
276
+ error: "Cannot type special or non-ASCII characters: no input field is focused. Tap the field first.",
277
+ };
278
+ }
279
+ await this.session.elementSendKeys(active, text);
280
+ return { ok: true };
200
281
  }
201
- await this.session.elementSendKeys(active, text);
282
+ await this.session.execute("mobile: type", [{ text }]);
202
283
  return { ok: true };
203
284
  }
204
- await this.session.execute("mobile: type", [{ text }]);
285
+ // XCUITest: per-char W3C key actions; no focused element required.
286
+ const actions = [...text].flatMap((char) => [
287
+ { type: "keyDown", value: char },
288
+ { type: "keyUp", value: char },
289
+ ]);
290
+ await this.session.performActions([{ type: "key", id: "keyboard", actions }]);
291
+ await this.session.releaseActions();
205
292
  return { ok: true };
206
- }
207
- // XCUITest: per-char W3C key actions; no focused element required.
208
- const actions = [...text].flatMap((char) => [
209
- { type: "keyDown", value: char },
210
- { type: "keyUp", value: char },
211
- ]);
212
- await this.session.performActions([{ type: "key", id: "keyboard", actions }]);
213
- await this.session.releaseActions();
214
- return { ok: true };
293
+ });
215
294
  }
216
295
  catch (error) {
217
296
  return fail(error);
@@ -227,37 +306,52 @@ export class AppiumController {
227
306
  }
228
307
  }
229
308
  async clearText() {
230
- await this.ensureSession();
231
309
  try {
232
- const active = await this.session.getActiveElement();
233
- if (active) {
234
- await this.session.elementClear(active);
235
- return { ok: true };
236
- }
310
+ return await this.withSession(async () => {
311
+ try {
312
+ const active = await this.session.getActiveElement();
313
+ if (active) {
314
+ await this.session.elementClear(active);
315
+ return { ok: true };
316
+ }
317
+ }
318
+ catch (error) {
319
+ // Re-throw a dead session so withSession can rebuild it; "no focused
320
+ // element" style rejections fall through to the fallback path.
321
+ if (isSessionDeadError(error))
322
+ throw error;
323
+ }
324
+ if (this.platform === "android") {
325
+ await this.session.execute("mobile: pressKey", [
326
+ { keycode: ANDROID_KEYCODE_A, metastate: ANDROID_META_CTRL_ON },
327
+ ]);
328
+ await this.session.execute("mobile: pressKey", [
329
+ { keycode: ANDROID_KEY_CODES["delete"] },
330
+ ]);
331
+ return { ok: true };
332
+ }
333
+ return {
334
+ ok: false,
335
+ error: "Could not clear the field: no focused input element was found. " +
336
+ "Tap the field to focus it before calling clear_text.",
337
+ };
338
+ });
237
339
  }
238
- catch { }
239
- if (this.platform === "android") {
240
- try {
241
- await this.session.execute("mobile: pressKey", [
242
- { keycode: ANDROID_KEYCODE_A, metastate: ANDROID_META_CTRL_ON },
243
- ]);
244
- await this.session.execute("mobile: pressKey", [{ keycode: ANDROID_KEY_CODES["delete"] }]);
245
- return { ok: true };
246
- }
247
- catch (error) {
248
- return fail(error);
249
- }
340
+ catch (error) {
341
+ return fail(error);
250
342
  }
251
- return {
252
- ok: false,
253
- error: "Could not clear the field: no focused input element was found. " +
254
- "Tap the field to focus it before calling clear_text.",
255
- };
256
343
  }
257
344
  async pressKey(key) {
258
- await this.ensureSession();
259
345
  const normalized = key.toLowerCase().trim();
260
346
  try {
347
+ return await this.withSession(() => this.pressKeyOnce(key, normalized));
348
+ }
349
+ catch (error) {
350
+ return fail(error);
351
+ }
352
+ }
353
+ async pressKeyOnce(key, normalized) {
354
+ {
261
355
  if (this.platform === "android") {
262
356
  const code = ANDROID_KEY_CODES[normalized];
263
357
  if (code === undefined)
@@ -293,18 +387,16 @@ export class AppiumController {
293
387
  }
294
388
  return { ok: false, error: `Unsupported key: ${key}` };
295
389
  }
296
- catch (error) {
297
- return fail(error);
298
- }
299
390
  }
300
391
  async launchApp(app) {
301
392
  if (!app)
302
393
  return { ok: false, error: "launch_app requires an app id" };
303
- await this.ensureSession();
304
394
  try {
305
- const param = this.platform === "android" ? { appId: app } : { bundleId: app };
306
- await this.session.execute("mobile: activateApp", [param]);
307
- return { ok: true };
395
+ return await this.withSession(async () => {
396
+ const param = this.platform === "android" ? { appId: app } : { bundleId: app };
397
+ await this.session.execute("mobile: activateApp", [param]);
398
+ return { ok: true };
399
+ });
308
400
  }
309
401
  catch (error) {
310
402
  return fail(error);
@@ -313,13 +405,14 @@ export class AppiumController {
313
405
  async findElement(text) {
314
406
  if (!text)
315
407
  return { ok: false, error: "find_element requires text" };
316
- await this.ensureSession();
317
408
  try {
318
- const ref = await this.locate(text);
319
- if (!ref)
320
- return { ok: false, error: `Element with text "${text}" not found` };
321
- await this.session.elementClick(ref);
322
- return { ok: true };
409
+ return await this.withSession(async () => {
410
+ const ref = await this.locate(text);
411
+ if (!ref)
412
+ return { ok: false, error: `Element with text "${text}" not found` };
413
+ await this.session.elementClick(ref);
414
+ return { ok: true };
415
+ });
323
416
  }
324
417
  catch (error) {
325
418
  return fail(error);
@@ -339,53 +432,51 @@ export class AppiumController {
339
432
  return null;
340
433
  }
341
434
  async hideKeyboard() {
342
- await this.ensureSession();
343
435
  try {
344
- await this.session.execute("mobile: hideKeyboard", []);
345
- return { ok: true };
346
- }
347
- catch {
348
- return { ok: true };
436
+ await this.withSession(() => this.session.execute("mobile: hideKeyboard", []));
349
437
  }
438
+ catch { }
439
+ return { ok: true };
350
440
  }
351
441
  async rotateDevice(orientation) {
352
- await this.ensureSession();
353
442
  try {
354
- const wantLandscape = orientation === "landscape";
355
- await this.session.setOrientation(wantLandscape ? "LANDSCAPE" : "PORTRAIT");
356
- // Rotation changes the window rect, so the cached pixel->point scale is stale.
357
- this.scaling = null;
358
- // Poll up to 2s until the OS reports the new dimensions; rotation speed varies.
359
- const deadline = Date.now() + 2000;
360
- while (Date.now() < deadline) {
361
- try {
362
- const rect = await this.session.windowRect();
363
- if (rect.width > rect.height === wantLandscape)
364
- break;
443
+ return await this.withSession(async () => {
444
+ const wantLandscape = orientation === "landscape";
445
+ await this.session.setOrientation(wantLandscape ? "LANDSCAPE" : "PORTRAIT");
446
+ // Rotation changes the window rect, so the cached pixel->point scale is stale.
447
+ this.scaling = null;
448
+ // Poll up to 2s until the OS reports the new dimensions; rotation speed varies.
449
+ const deadline = Date.now() + 2000;
450
+ while (Date.now() < deadline) {
451
+ try {
452
+ const rect = await this.session.windowRect();
453
+ if (rect.width > rect.height === wantLandscape)
454
+ break;
455
+ }
456
+ catch { }
457
+ await new Promise((resolve) => setTimeout(resolve, 100));
365
458
  }
366
- catch { }
367
- await new Promise((resolve) => setTimeout(resolve, 100));
368
- }
369
- return { ok: true };
459
+ return { ok: true };
460
+ });
370
461
  }
371
462
  catch (error) {
372
463
  return fail(error);
373
464
  }
374
465
  }
375
466
  async setLocation(latitude, longitude, altitude) {
376
- await this.ensureSession();
377
467
  try {
378
- await this.session.setGeoLocation({ latitude, longitude, altitude });
379
- return { ok: true };
468
+ return await this.withSession(async () => {
469
+ await this.session.setGeoLocation({ latitude, longitude, altitude });
470
+ return { ok: true };
471
+ });
380
472
  }
381
473
  catch (error) {
382
474
  return fail(error);
383
475
  }
384
476
  }
385
477
  async getDeviceTime() {
386
- await this.ensureSession();
387
478
  try {
388
- const raw = await this.session.execute("mobile: getDeviceTime", []);
479
+ const raw = await this.withSession(() => this.session.execute("mobile: getDeviceTime", []));
389
480
  if (typeof raw !== "string" || raw.trim().length === 0)
390
481
  return null;
391
482
  const parsed = new Date(raw);