@sandboxrouter/sdk 0.12.0 → 0.13.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.
@@ -35,7 +35,6 @@ export class MobilerunProvider {
35
35
  configured() {
36
36
  return !!this.apiKey;
37
37
  }
38
- /** @internal */
39
38
  async sdk() {
40
39
  if (!this.apiKey)
41
40
  throw new ProviderError(ID, "MOBILERUN_CLOUD_API_KEY is not set");
@@ -58,12 +57,12 @@ export class MobilerunProvider {
58
57
  if (mine) {
59
58
  if (mine.state === "stopped")
60
59
  await guard(client.devices.resume(mine.id));
61
- return new MobilerunComputer(this, await waitReady(client, mine.id));
60
+ return new MobilerunComputer(client, await waitReady(client, mine.id));
62
61
  }
63
62
  }
64
63
  const created = await guard(client.devices.create({ deviceType: "android_cloud_phone", billing: "auto", ...(name ? { name } : {}) }));
65
64
  const ready = created.state === "ready" ? created : await waitReady(client, created.id);
66
- return new MobilerunComputer(this, ready);
65
+ return new MobilerunComputer(client, ready);
67
66
  }
68
67
  /** Re-attach by device id. A parked (stopped) phone is resumed and waited on. */
69
68
  async connectComputer(nativeId) {
@@ -73,7 +72,7 @@ export class MobilerunProvider {
73
72
  throw new ProviderError(ID, `device ${nativeId} is terminated`, 404);
74
73
  if (d.state === "stopped")
75
74
  await guard(client.devices.resume(nativeId));
76
- return new MobilerunComputer(this, d.state === "ready" ? d : await waitReady(client, nativeId));
75
+ return new MobilerunComputer(client, d.state === "ready" ? d : await waitReady(client, nativeId));
77
76
  }
78
77
  }
79
78
  /**
@@ -203,19 +202,16 @@ async function bytesOf(p) {
203
202
  }
204
203
  /** Gestures take the canonical frame (the last screenshot), which is the phone's native pixels. */
205
204
  class MobilerunComputer {
206
- p;
205
+ client;
207
206
  nativeId;
208
207
  frame = 0;
209
208
  size;
210
- constructor(p, device) {
211
- this.p = p;
209
+ constructor(client, device) {
210
+ this.client = client;
212
211
  this.nativeId = device.id;
213
212
  }
214
- sdk() {
215
- return this.p.sdk();
216
- }
217
213
  async info() {
218
- const d = await guard((await this.sdk()).devices.retrieve(this.nativeId));
214
+ const d = await guard(this.client.devices.retrieve(this.nativeId));
219
215
  const status = d.state === "ready" ? "ready" : d.state === "stopped" ? "sleeping" : d.state === "terminated" ? "deleted" : d.state;
220
216
  const info = {
221
217
  id: d.id,
@@ -250,8 +246,7 @@ class MobilerunComputer {
250
246
  return out;
251
247
  }
252
248
  async capture() {
253
- const client = await this.sdk();
254
- const png = await bytesOf(client.devices.screenshots.capture(this.nativeId, {}, { headers: { "Accept-Encoding": "identity" } }));
249
+ const png = await bytesOf(this.client.devices.screenshots.capture(this.nativeId, {}, { headers: { "Accept-Encoding": "identity" } }));
255
250
  if (!png.length)
256
251
  throw new Error("screenshot came back empty");
257
252
  const s = pngSize(png);
@@ -271,7 +266,6 @@ class MobilerunComputer {
271
266
  return this.size;
272
267
  }
273
268
  async perform(a) {
274
- const client = await this.sdk();
275
269
  const id = this.nativeId;
276
270
  switch (a.action) {
277
271
  case "screenshot":
@@ -282,7 +276,7 @@ class MobilerunComputer {
282
276
  if (a.modifier)
283
277
  throw new Error("mobilerun taps can't hold modifier keys");
284
278
  const [x, y] = a.coordinate;
285
- await client.devices.actions.tap(id, { x: Math.round(x), y: Math.round(y) });
279
+ await this.client.devices.actions.tap(id, { x: Math.round(x), y: Math.round(y) });
286
280
  return {};
287
281
  }
288
282
  case "left_click_drag": {
@@ -290,7 +284,7 @@ class MobilerunComputer {
290
284
  throw new Error("left_click_drag needs startCoordinate and coordinate (a phone has no cursor)");
291
285
  const [sx, sy] = a.startCoordinate;
292
286
  const [ex, ey] = a.coordinate;
293
- await client.devices.actions.swipe(id, { startX: Math.round(sx), startY: Math.round(sy), endX: Math.round(ex), endY: Math.round(ey), duration: 600 });
287
+ await this.client.devices.actions.swipe(id, { startX: Math.round(sx), startY: Math.round(sy), endX: Math.round(ex), endY: Math.round(ey), duration: 600 });
294
288
  return {};
295
289
  }
296
290
  case "scroll": {
@@ -308,13 +302,13 @@ class MobilerunComputer {
308
302
  const [sx, sy, ex, ey] = vertical
309
303
  ? [cx, cy + (sign * span) / 2, cx, cy - (sign * span) / 2]
310
304
  : [cx + (sign * span) / 2, cy, cx - (sign * span) / 2, cy];
311
- await client.devices.actions.swipe(id, { startX: clampX(sx), startY: clampY(sy), endX: clampX(ex), endY: clampY(ey), duration: 400 });
305
+ await this.client.devices.actions.swipe(id, { startX: clampX(sx), startY: clampY(sy), endX: clampX(ex), endY: clampY(ey), duration: 400 });
312
306
  return {};
313
307
  }
314
308
  case "type":
315
309
  if (!a.text)
316
310
  throw new Error("type needs text");
317
- await client.devices.keyboard.write(id, { text: a.text });
311
+ await this.client.devices.keyboard.write(id, { text: a.text });
318
312
  return {};
319
313
  case "key": {
320
314
  if (!a.text)
@@ -323,7 +317,7 @@ class MobilerunComputer {
323
317
  const codes = a.text.trim().split(/\s+/).map(mobilerunKey);
324
318
  for (let n = 0; n < Math.max(1, a.repeat ?? 1); n++) {
325
319
  for (const key of codes)
326
- await client.devices.keyboard.key(id, { key });
320
+ await this.client.devices.keyboard.key(id, { key });
327
321
  }
328
322
  return {};
329
323
  }
@@ -362,9 +356,8 @@ class MobilerunComputer {
362
356
  }
363
357
  /** Pulls a file from a device path, e.g. /sdcard/Download/report.pdf. */
364
358
  async readFile(path) {
365
- const client = await this.sdk();
366
359
  try {
367
- return await bytesOf(client.devices.files.download(this.nativeId, { path }, { headers: { "Accept-Encoding": "identity" } }));
360
+ return await bytesOf(this.client.devices.files.download(this.nativeId, { path }, { headers: { "Accept-Encoding": "identity" } }));
368
361
  }
369
362
  catch (err) {
370
363
  throw toProviderError(err);
@@ -375,10 +368,9 @@ class MobilerunComputer {
375
368
  const name = path.slice(path.lastIndexOf("/") + 1);
376
369
  if (!name)
377
370
  throw new ProviderError(ID, `writeFile needs a file path, got directory "${path}"`);
378
- const client = await this.sdk();
379
371
  const { toFile } = await optionalImport("@mobilerun/sdk", "mobilerun");
380
372
  const file = await toFile(Buffer.from(toBytes(content)), name);
381
- await guard(client.devices.files.upload(this.nativeId, { path: dirname(path), file }));
373
+ await guard(this.client.devices.files.upload(this.nativeId, { path: dirname(path), file }));
382
374
  }
383
375
  /**
384
376
  * The device's WebRTC stream: `url` is the device's streamUrl and `token` its device-scoped
@@ -387,7 +379,7 @@ class MobilerunComputer {
387
379
  * (DeviceStream hasControl={false}). Mobilerun does not publish an expiry, so expiresAt is null.
388
380
  */
389
381
  async viewer(mode = "watch", _opts = {}) {
390
- const d = await guard((await this.sdk()).devices.retrieve(this.nativeId));
382
+ const d = await guard(this.client.devices.retrieve(this.nativeId));
391
383
  if (!d.streamUrl || !d.streamToken)
392
384
  throw new ProviderError(ID, `no live stream for device ${this.nativeId} (state: ${d.state})`);
393
385
  return {
@@ -400,11 +392,11 @@ class MobilerunComputer {
400
392
  }
401
393
  /** Park: apps, data and identity kept, no minutes billed. connectComputer resumes it. 404 where capabilities.stop is false. */
402
394
  async sleep() {
403
- await guard((await this.sdk()).devices.stop(this.nativeId));
395
+ await guard(this.client.devices.stop(this.nativeId));
404
396
  }
405
397
  /** Terminate and release. Subscription-billed (dedicated) phones refuse this with 401. */
406
398
  async delete() {
407
- await guard((await this.sdk()).devices.terminate(this.nativeId));
399
+ await guard(this.client.devices.terminate(this.nativeId));
408
400
  }
409
401
  }
410
402
  function seconds(n) {
@@ -1,4 +1,4 @@
1
- import { DEFAULT_MEMORY_GB, DEFAULT_VCPU } from "../catalog.js";
1
+ import { vercelSandboxSize } from "../catalog.js";
2
2
  import { ProviderError } from "../types.js";
3
3
  import { DEFAULT_EXEC_TIMEOUT_S, killedByTimeout, optionalImport, timeoutError, toBytes, wrapCommand } from "../util.js";
4
4
  /**
@@ -19,7 +19,7 @@ export class VercelProvider {
19
19
  }
20
20
  async create(spec) {
21
21
  const { Sandbox } = await optionalImport("@vercel/sandbox", "vercel");
22
- const vcpus = Math.min(8, Math.max(spec.vcpu ?? DEFAULT_VCPU, Math.ceil((spec.memoryGb ?? DEFAULT_MEMORY_GB) / 2)));
22
+ const { vcpu: vcpus } = vercelSandboxSize(spec);
23
23
  const sbx = await Sandbox.create({
24
24
  ...this.creds(),
25
25
  runtime: spec.image?.vercel ?? "node22",