@phone-use/sdk 0.2.0 → 0.3.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.
package/dist/index.d.mts CHANGED
@@ -477,6 +477,77 @@ declare function executeAction(core: DeviceCore, action: Action, opts?: ActOptio
477
477
  */
478
478
  declare function createAgentDeviceBackend(config?: DeviceConfig): DeviceBackend;
479
479
  //#endregion
480
+ //#region src/backends/cloud-sandbox.d.ts
481
+ type SandboxRpcMethod = 'snapshot' | 'screenshot' | 'press' | 'longPress' | 'fill' | 'typeText' | 'pressKey' | 'scroll' | 'pan' | 'installApp' | 'waitForText' | 'systemAlert' | 'home' | 'back' | 'openApp' | 'listApps' | 'closeSession';
482
+ type SandboxRpcRequest = {
483
+ method: SandboxRpcMethod;
484
+ args: unknown[];
485
+ };
486
+ type SandboxRpcSuccess = {
487
+ ok: true;
488
+ result: unknown;
489
+ };
490
+ type SandboxRpcFailure = {
491
+ ok: false;
492
+ error: {
493
+ message: string;
494
+ code?: string | undefined;
495
+ retryable?: boolean | undefined;
496
+ };
497
+ };
498
+ type SandboxRpcResponse = SandboxRpcSuccess | SandboxRpcFailure;
499
+ type CloudSandboxBackendOptions = {
500
+ endpoint: string;
501
+ token: string;
502
+ capabilities?: Capability[] | undefined;
503
+ fetch?: typeof globalThis.fetch | undefined;
504
+ };
505
+ declare class CloudSandboxBackend extends BaseDeviceBackend {
506
+ private readonly endpoint;
507
+ private readonly token;
508
+ private readonly fetchImpl;
509
+ constructor(opts: CloudSandboxBackendOptions);
510
+ private rpc;
511
+ snapshot(opts?: {
512
+ interactiveOnly?: boolean;
513
+ depth?: number;
514
+ }): Promise<Snapshot>;
515
+ screenshot(opts: {
516
+ path: string;
517
+ overlayRefs?: boolean;
518
+ }): Promise<{
519
+ path: string;
520
+ }>;
521
+ press(target: PressTarget): Promise<void>;
522
+ longPress(ref: string, durationMs?: number): Promise<void>;
523
+ fill(ref: string, text: string): Promise<void>;
524
+ typeText(text: string): Promise<void>;
525
+ pressKey(key: 'return'): Promise<void>;
526
+ scroll(direction: ScrollDirection): Promise<void>;
527
+ pan(x: number, y: number, dx: number, dy: number, durationMs?: number): Promise<void>;
528
+ waitForText(text: string, timeoutMs?: number): Promise<void>;
529
+ systemAlert(action: AlertAction): Promise<BackendAlertResult>;
530
+ home(): Promise<void>;
531
+ back(): Promise<void>;
532
+ openApp(opts: {
533
+ app?: string;
534
+ url?: string;
535
+ relaunch?: boolean;
536
+ }): Promise<OpenAppResult>;
537
+ listApps(): Promise<string[]>;
538
+ closeSession(): Promise<void>;
539
+ /** Upload a zipped .app bundle (base64) and install it on the sandbox device. */
540
+ installApp(base64Zip: string): Promise<{
541
+ installed?: string;
542
+ }>;
543
+ }
544
+ /**
545
+ * Build a CloudSandboxBackend from explicit options or the environment:
546
+ * PHONE_USE_SANDBOX_URL + PHONE_USE_SANDBOX_TOKEN (printed by
547
+ * `phone-use sandbox env <id>`).
548
+ */
549
+ declare function createCloudSandboxBackend(config?: Partial<CloudSandboxBackendOptions>): CloudSandboxBackend;
550
+ //#endregion
480
551
  //#region src/backends/device-runner.d.ts
481
552
  /**
482
553
  * A device-runner is addressed purely by URL, so it takes no DeviceConfig
@@ -743,7 +814,7 @@ declare const ios: {
743
814
  * deliberately not re-exported here.
744
815
  */
745
816
  /** The published package version (kept in sync with package.json by the release flow). */
746
- declare const VERSION = "0.2.0";
817
+ declare const VERSION = "0.3.0";
747
818
  //#endregion
748
- export { ALL_CAPABILITIES, AbortedError, type ActOptions, type Action, type ActionEvidence, ActionFailedError, type ActionResult, type ActionVerb, type AlertAction, type AlertOutcome, type AndroidDeviceConfig, type BackendAlertResult, type BackendFactory, BaseDeviceBackend, type Capability, type CommonDeviceConfig, type CompiledSkill, type CreateDeviceHandleOptions, type Device, type DeviceBackend, type DeviceConfig, DeviceCore, DeviceInUseError, DeviceNotFoundError, type DevicePlatform, DeviceRunnerBackend, type DeviceStatus, type ElementQuery, type IosConnectOptions, type IosDeviceConfig, type IosLaunchOptions, type Observation, type ObserveResult, type ObservedElement, type OpenAppResult, PhoneUseError, type PhoneUseErrorCode, type PhoneUseErrorDetails, type PressTarget, type Rect, type RenderState, type Resolution, type ResolveOpts, type ScrollDirection, SecretStore, SessionNotFoundError, type Snapshot, type SnapshotNode, TimeoutError, type UiElement, UnsupportedCapabilityError, VERSION, buildObserveResult, createAgentDeviceBackend, createDeviceHandle, createDeviceRunnerBackend, describeError, executeAction, getBackendFactory, ios, labelMatches, listBackends, matchInElements, registerBackend, toActions, toPhoneUseError };
819
+ export { ALL_CAPABILITIES, AbortedError, type ActOptions, type Action, type ActionEvidence, ActionFailedError, type ActionResult, type ActionVerb, type AlertAction, type AlertOutcome, type AndroidDeviceConfig, type BackendAlertResult, type BackendFactory, BaseDeviceBackend, type Capability, CloudSandboxBackend, type CloudSandboxBackendOptions, type CommonDeviceConfig, type CompiledSkill, type CreateDeviceHandleOptions, type Device, type DeviceBackend, type DeviceConfig, DeviceCore, DeviceInUseError, DeviceNotFoundError, type DevicePlatform, DeviceRunnerBackend, type DeviceStatus, type ElementQuery, type IosConnectOptions, type IosDeviceConfig, type IosLaunchOptions, type Observation, type ObserveResult, type ObservedElement, type OpenAppResult, PhoneUseError, type PhoneUseErrorCode, type PhoneUseErrorDetails, type PressTarget, type Rect, type RenderState, type Resolution, type ResolveOpts, type SandboxRpcMethod, type SandboxRpcRequest, type SandboxRpcResponse, type ScrollDirection, SecretStore, SessionNotFoundError, type Snapshot, type SnapshotNode, TimeoutError, type UiElement, UnsupportedCapabilityError, VERSION, buildObserveResult, createAgentDeviceBackend, createCloudSandboxBackend, createDeviceHandle, createDeviceRunnerBackend, describeError, executeAction, getBackendFactory, ios, labelMatches, listBackends, matchInElements, registerBackend, toActions, toPhoneUseError };
749
820
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import { a as registerBackend, c as DeviceInUseError, d as SessionNotFoundError, f as TimeoutError, i as listBackends, l as DeviceNotFoundError, m as toPhoneUseError, n as BaseDeviceBackend, o as AbortedError, p as UnsupportedCapabilityError, r as getBackendFactory, s as ActionFailedError, t as ALL_CAPABILITIES, u as PhoneUseError } from "./device-Xsy_LPUF.mjs";
2
2
  import { createAgentDeviceClient } from "agent-device";
3
+ import { writeFile } from "node:fs/promises";
3
4
  import { execFile } from "node:child_process";
4
5
  import { promisify } from "node:util";
5
6
  //#region src/observe.ts
@@ -1453,6 +1454,127 @@ function createAgentDeviceBackend(config) {
1453
1454
  return new AgentDeviceBackend(config);
1454
1455
  }
1455
1456
  //#endregion
1457
+ //#region src/backends/cloud-sandbox.ts
1458
+ var CloudSandboxBackend = class extends BaseDeviceBackend {
1459
+ endpoint;
1460
+ token;
1461
+ fetchImpl;
1462
+ constructor(opts) {
1463
+ super("phone-use-cloud", opts.capabilities ?? ALL_CAPABILITIES);
1464
+ this.endpoint = opts.endpoint.replace(/\/+$/, "");
1465
+ this.token = opts.token;
1466
+ this.fetchImpl = opts.fetch ?? globalThis.fetch;
1467
+ }
1468
+ async rpc(method, ...args) {
1469
+ const response = await this.fetchImpl(`${this.endpoint}/rpc`, {
1470
+ method: "POST",
1471
+ headers: {
1472
+ authorization: `Bearer ${this.token}`,
1473
+ "content-type": "application/json"
1474
+ },
1475
+ body: JSON.stringify({
1476
+ method,
1477
+ args
1478
+ })
1479
+ });
1480
+ const body = await response.json().catch(() => ({}));
1481
+ if (!response.ok || !("ok" in body)) throw new PhoneUseError(("error" in body && typeof body.error === "string" ? body.error : void 0) ?? ("message" in body ? body.message : void 0) ?? `HTTP ${response.status}`, {
1482
+ code: "UNKNOWN",
1483
+ retryable: false
1484
+ });
1485
+ if (!body.ok) throw new PhoneUseError(body.error.message, {
1486
+ code: normalizeErrorCode(body.error.code),
1487
+ retryable: body.error.retryable ?? false
1488
+ });
1489
+ return body.result;
1490
+ }
1491
+ snapshot(opts) {
1492
+ return this.rpc("snapshot", opts);
1493
+ }
1494
+ async screenshot(opts) {
1495
+ const result = await this.rpc("screenshot", { overlayRefs: opts.overlayRefs });
1496
+ await writeFile(opts.path, Buffer.from(result.base64, "base64"));
1497
+ return { path: opts.path };
1498
+ }
1499
+ press(target) {
1500
+ return this.rpc("press", target);
1501
+ }
1502
+ longPress(ref, durationMs) {
1503
+ return this.rpc("longPress", ref, durationMs);
1504
+ }
1505
+ fill(ref, text) {
1506
+ return this.rpc("fill", ref, text);
1507
+ }
1508
+ typeText(text) {
1509
+ return this.rpc("typeText", text);
1510
+ }
1511
+ pressKey(key) {
1512
+ return this.rpc("pressKey", key);
1513
+ }
1514
+ scroll(direction) {
1515
+ return this.rpc("scroll", direction);
1516
+ }
1517
+ pan(x, y, dx, dy, durationMs) {
1518
+ return this.rpc("pan", x, y, dx, dy, durationMs);
1519
+ }
1520
+ waitForText(text, timeoutMs) {
1521
+ return this.rpc("waitForText", text, timeoutMs);
1522
+ }
1523
+ systemAlert(action) {
1524
+ return this.rpc("systemAlert", action);
1525
+ }
1526
+ home() {
1527
+ return this.rpc("home");
1528
+ }
1529
+ back() {
1530
+ return this.rpc("back");
1531
+ }
1532
+ openApp(opts) {
1533
+ return this.rpc("openApp", opts);
1534
+ }
1535
+ listApps() {
1536
+ return this.rpc("listApps");
1537
+ }
1538
+ closeSession() {
1539
+ return this.rpc("closeSession");
1540
+ }
1541
+ /** Upload a zipped .app bundle (base64) and install it on the sandbox device. */
1542
+ installApp(base64Zip) {
1543
+ return this.rpc("installApp", base64Zip);
1544
+ }
1545
+ };
1546
+ /**
1547
+ * Build a CloudSandboxBackend from explicit options or the environment:
1548
+ * PHONE_USE_SANDBOX_URL + PHONE_USE_SANDBOX_TOKEN (printed by
1549
+ * `phone-use sandbox env <id>`).
1550
+ */
1551
+ function createCloudSandboxBackend(config) {
1552
+ const endpoint = config?.endpoint ?? process.env.PHONE_USE_SANDBOX_URL;
1553
+ const token = config?.token ?? process.env.PHONE_USE_SANDBOX_TOKEN;
1554
+ if (!endpoint || !token) throw new PhoneUseError("cloud sandbox backend needs PHONE_USE_SANDBOX_URL and PHONE_USE_SANDBOX_TOKEN (see `phone-use sandbox env <id>`)", {
1555
+ code: "BACKEND_NOT_FOUND",
1556
+ retryable: false
1557
+ });
1558
+ return new CloudSandboxBackend({
1559
+ ...config,
1560
+ endpoint,
1561
+ token
1562
+ });
1563
+ }
1564
+ function normalizeErrorCode(code) {
1565
+ return (/* @__PURE__ */ new Set([
1566
+ "DEVICE_NOT_FOUND",
1567
+ "DEVICE_IN_USE",
1568
+ "SESSION_NOT_FOUND",
1569
+ "TIMEOUT",
1570
+ "ACTION_FAILED",
1571
+ "UNSUPPORTED_CAPABILITY",
1572
+ "BACKEND_NOT_FOUND",
1573
+ "ABORTED",
1574
+ "UNKNOWN"
1575
+ ])).has(code ?? "") ? code : "UNKNOWN";
1576
+ }
1577
+ //#endregion
1456
1578
  //#region src/backends/device-runner.ts
1457
1579
  const RUNNER_CAPABILITIES = [
1458
1580
  "snapshot",
@@ -1994,9 +2116,9 @@ const ios = {
1994
2116
  * deliberately not re-exported here.
1995
2117
  */
1996
2118
  /** The published package version (kept in sync with package.json by the release flow). */
1997
- const VERSION = "0.2.0";
2119
+ const VERSION = "0.3.0";
1998
2120
  registerBackend("agent-device", createAgentDeviceBackend);
1999
2121
  //#endregion
2000
- export { ALL_CAPABILITIES, AbortedError, ActionFailedError, BaseDeviceBackend, DeviceCore, DeviceInUseError, DeviceNotFoundError, DeviceRunnerBackend, PhoneUseError, SecretStore, SessionNotFoundError, TimeoutError, UnsupportedCapabilityError, VERSION, buildObserveResult, createAgentDeviceBackend, createDeviceHandle, createDeviceRunnerBackend, describeError, executeAction, getBackendFactory, ios, labelMatches, listBackends, matchInElements, registerBackend, toActions, toPhoneUseError };
2122
+ export { ALL_CAPABILITIES, AbortedError, ActionFailedError, BaseDeviceBackend, CloudSandboxBackend, DeviceCore, DeviceInUseError, DeviceNotFoundError, DeviceRunnerBackend, PhoneUseError, SecretStore, SessionNotFoundError, TimeoutError, UnsupportedCapabilityError, VERSION, buildObserveResult, createAgentDeviceBackend, createCloudSandboxBackend, createDeviceHandle, createDeviceRunnerBackend, describeError, executeAction, getBackendFactory, ios, labelMatches, listBackends, matchInElements, registerBackend, toActions, toPhoneUseError };
2001
2123
 
2002
2124
  //# sourceMappingURL=index.mjs.map