@oya-ai/browser 1.0.107 → 1.0.111

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.js CHANGED
@@ -1,3 +1,44 @@
1
+ // src/constants.ts
2
+ var DEFAULT_BASE_URL = "https://oyabrowser.com";
3
+ var DEFAULT_TIMEOUT_MS = 6e4;
4
+ var START_TIMEOUT_MS = 12e4;
5
+ var READY_TIMEOUT_MS = 12e4;
6
+ var READY_POLL_MS = 2e3;
7
+ var NAVIGATE_TIMEOUT_MS = 12e4;
8
+ var CHALLENGE_TIMEOUT_MS = 18e4;
9
+ var AGENT_TIMEOUT_MS = 6e5;
10
+ var PLAYBOOK_TIMEOUT_MS = 12e4;
11
+ var STOP_TIMEOUT_MS = 6e4;
12
+ var WAIT_FOR_DEFAULT_MS = 3e4;
13
+ var WAIT_FOR_GRACE_MS = 5e3;
14
+ var AIMED_SCROLL_AMOUNT = 500;
15
+ var RUN_POLL_MS = 2e3;
16
+ var MAX_RUN_POLL_ERRORS = 5;
17
+ var BYTES_PER_MB = 1048576;
18
+ var MAX_FILE_MB = 10;
19
+ var BASE64_CHUNK_BYTES = 8192;
20
+ var MS_PER_SECOND = 1e3;
21
+ var LATIN1_MAX = 255;
22
+ var SPACE = 32;
23
+ var TAB = 9;
24
+ var DEL = 127;
25
+ var HEX = 16;
26
+ var CODE_POINT_DIGITS = 4;
27
+ var Status = {
28
+ /** The caller passed something unusable, such as a non-numeric element id. */
29
+ BAD_REQUEST: 400,
30
+ /** The server does not know that session. */
31
+ NOT_FOUND: 404,
32
+ /** The browser is in a state that needs attention first. */
33
+ CONFLICT: 409,
34
+ /** A browser command ran and failed. */
35
+ UNPROCESSABLE: 422,
36
+ /** A run failed without saying why. */
37
+ SERVER_ERROR: 500,
38
+ /** A browser did not come up in time. */
39
+ GATEWAY_TIMEOUT: 504
40
+ };
41
+
1
42
  // src/errors.ts
2
43
  var OyaError = class extends Error {
3
44
  /** The HTTP status, or the SDK's own status for errors it raises itself. */
@@ -12,6 +53,10 @@ var OyaError = class extends Error {
12
53
  this.body = body;
13
54
  }
14
55
  };
56
+ function refusal(message, field, suggestion) {
57
+ const body = { error: message, code: "invalid_request", field, ...suggestion ? { suggestion } : {} };
58
+ return new OyaError(message, Status.BAD_REQUEST, body);
59
+ }
15
60
 
16
61
  // src/cli-config.ts
17
62
  var node = () => globalThis.process;
@@ -35,58 +80,26 @@ function savedConfig() {
35
80
  }
36
81
  }
37
82
 
38
- // src/constants.ts
39
- var DEFAULT_BASE_URL = "https://oyabrowser.com";
40
- var DEFAULT_TIMEOUT_MS = 6e4;
41
- var START_TIMEOUT_MS = 12e4;
42
- var READY_TIMEOUT_MS = 12e4;
43
- var READY_POLL_MS = 2e3;
44
- var NAVIGATE_TIMEOUT_MS = 12e4;
45
- var CHALLENGE_TIMEOUT_MS = 18e4;
46
- var AGENT_TIMEOUT_MS = 6e5;
47
- var PLAYBOOK_TIMEOUT_MS = 12e4;
48
- var STOP_TIMEOUT_MS = 6e4;
49
- var WAIT_FOR_DEFAULT_MS = 3e4;
50
- var WAIT_FOR_GRACE_MS = 5e3;
51
- var AIMED_SCROLL_AMOUNT = 500;
52
- var RUN_POLL_MS = 2e3;
53
- var MAX_RUN_POLL_ERRORS = 5;
54
- var BYTES_PER_MB = 1048576;
55
- var MAX_FILE_MB = 10;
56
- var BASE64_CHUNK_BYTES = 8192;
57
- var MS_PER_SECOND = 1e3;
58
- var Status = {
59
- /** The caller passed something unusable, such as a non-numeric element id. */
60
- BAD_REQUEST: 400,
61
- /** The server does not know that session. */
62
- NOT_FOUND: 404,
63
- /** The browser is in a state that needs attention first. */
64
- CONFLICT: 409,
65
- /** A browser command ran and failed. */
66
- UNPROCESSABLE: 422,
67
- /** A run failed without saying why. */
68
- SERVER_ERROR: 500,
69
- /** A browser did not come up in time. */
70
- GATEWAY_TIMEOUT: 504
71
- };
72
-
73
83
  // src/client.ts
84
+ var OPTIONS_ORIGIN = { apiKeyFrom: "the apiKey option", baseUrlFrom: "the baseUrl option", savedKey: false };
74
85
  var Http = class {
75
- /** Stores where to call, with which key, how long to wait and which fetch to use. */
76
- constructor(baseUrl, apiKey, timeoutMs, fetchImpl) {
86
+ /** Stores where to call, with which key, how long to wait, which fetch to use, and where the key and address came from. */
87
+ constructor(baseUrl, apiKey, timeoutMs, fetchImpl, origin = OPTIONS_ORIGIN) {
77
88
  this.baseUrl = baseUrl;
78
89
  this.apiKey = apiKey;
79
90
  this.timeoutMs = timeoutMs;
80
91
  this.fetchImpl = fetchImpl;
92
+ this.origin = origin;
81
93
  }
82
94
  baseUrl;
83
95
  apiKey;
84
96
  timeoutMs;
85
97
  fetchImpl;
98
+ origin;
86
99
  /** Sends one request and returns the parsed answer, or throws an OyaError when it failed. */
87
100
  async request(method, path, body, timeoutMs = this.timeoutMs, headers = {}) {
88
101
  const res = await this.send(`${this.baseUrl}${path}`, this.init(method, body, timeoutMs, headers), timeoutMs);
89
- return readAnswer(res, `${method} ${path}`, this.baseUrl);
102
+ return readAnswer(res, `${method} ${path}`, this);
90
103
  }
91
104
  /** The fetch options for one call, with this client's key on them. */
92
105
  init(method, body, timeoutMs, headers) {
@@ -101,23 +114,34 @@ var Http = class {
101
114
  try {
102
115
  return await this.fetchImpl(url, init);
103
116
  } catch (err) {
104
- throw new OyaError(unreachable(this.baseUrl, timeoutMs, err), 0, { error: String(err?.message) });
117
+ throw noAnswer(this, timeoutMs, err);
105
118
  }
106
119
  }
107
120
  };
108
- function unreachable(baseUrl, timeoutMs, err) {
109
- const cause = err?.name === "TimeoutError" || /abort/i.test(String(err?.message));
110
- if (cause) return `No answer from ${baseUrl} within ${timeoutMs}ms. Is it reachable, and is the call this slow?`;
111
- return `Could not reach ${baseUrl}. Is the server running, and is OYA_BASE_URL right?`;
121
+ var timedOut = (err) => err?.name === "TimeoutError" || /abort/i.test(String(err?.message));
122
+ function causeOf(err) {
123
+ const cause = err?.cause;
124
+ return cause?.code ?? cause?.errors?.[0]?.code;
125
+ }
126
+ function noAnswer(http, timeoutMs, err) {
127
+ const code = timedOut(err) ? "timeout" : "unreachable";
128
+ const body = { error: String(err?.message), code, ...causeOf(err) ? { cause: causeOf(err) } : {} };
129
+ return new OyaError(unreachable(http, timeoutMs, code), 0, body);
130
+ }
131
+ function unreachable({ baseUrl, origin }, timeoutMs, code) {
132
+ if (code === "timeout")
133
+ return `No answer from ${baseUrl} within ${timeoutMs}ms. Is it reachable, and is the call this slow?`;
134
+ if (origin.baseUrlFrom === "default") return `Could not reach ${baseUrl}. Check your connection.`;
135
+ return `Could not reach ${baseUrl}. Is the server running, and is ${origin.baseUrlFrom} right?`;
112
136
  }
113
137
  function requestInit(method, body, timeoutMs, headers) {
114
138
  const json = body === void 0 ? {} : { "Content-Type": "application/json" };
115
139
  const payload = body === void 0 ? void 0 : JSON.stringify(body);
116
140
  return { method, headers: { ...headers, ...json }, body: payload, signal: AbortSignal.timeout(timeoutMs) };
117
141
  }
118
- async function readAnswer(res, call2, baseUrl) {
142
+ async function readAnswer(res, call2, http) {
119
143
  const payload = parseBody(await res.text());
120
- if (!res.ok) throw failure(call2, res.status, payload, baseUrl);
144
+ if (!res.ok) throw failure(call2, res.status, payload, http);
121
145
  return payload;
122
146
  }
123
147
  function parseBody(text) {
@@ -127,22 +151,74 @@ function parseBody(text) {
127
151
  return text;
128
152
  }
129
153
  }
130
- function failure(call2, status, payload, baseUrl) {
131
- let message = payload?.error || `${call2} failed (${status})`;
132
- if (message === "Invalid API key") {
133
- message += ` for ${baseUrl}. Check OYA_API_KEY: a value exported in your shell beats .env.`;
134
- }
135
- return new OyaError(message, status, payload);
154
+ function failure(call2, status, payload, http) {
155
+ const message = payload?.error || `${call2} failed (${status})`;
156
+ return new OyaError(message === "Invalid API key" ? invalidKey(http) : message, status, payload);
157
+ }
158
+ function invalidKey({ baseUrl, origin }) {
159
+ const overrides = origin.apiKeyFrom === "OYA_API_KEY" && origin.savedKey;
160
+ const rest = overrides ? ", which overrides the one saved in ~/.oya/config.json" : "";
161
+ return `Invalid API key for ${baseUrl}. The key came from ${origin.apiKeyFrom}${rest}.`;
136
162
  }
137
163
  var env = (name) => globalThis.process?.env?.[name];
164
+ function firstOf(option, name, saved, optionName) {
165
+ if (option) return { value: option, from: `the ${optionName} option` };
166
+ if (env(name)) return { value: env(name), from: name };
167
+ return { value: saved, from: "~/.oya/config.json" };
168
+ }
169
+ function addressOf(options, saved) {
170
+ const found = firstOf(options.baseUrl, "OYA_BASE_URL", saved, "baseUrl");
171
+ return found.value ? found : { value: DEFAULT_BASE_URL, from: "default" };
172
+ }
173
+ function keyOf(options, saved) {
174
+ const found = firstOf(options.apiKey, "OYA_API_KEY", saved, "apiKey");
175
+ if (!found.value) throw new Error("No API key. Pass { apiKey }, set OYA_API_KEY, or run `npx @oya-ai/cli login`.");
176
+ return { ...found, value: headerSafe(found.value, "apiKey", "Copy the key again.") };
177
+ }
138
178
  function createHttp(options) {
139
179
  const saved = savedConfig();
140
- const apiKey = options.apiKey || env("OYA_API_KEY") || saved.apiKey;
141
- if (!apiKey) throw new Error("No API key. Pass { apiKey }, set OYA_API_KEY, or run `npx @oya-ai/cli login`.");
142
- const baseUrl = (options.baseUrl || env("OYA_BASE_URL") || saved.baseUrl || DEFAULT_BASE_URL).replace(/\/+$/, "");
180
+ const key = keyOf(options, saved.apiKey);
181
+ const address = addressOf(options, saved.baseUrl);
143
182
  const fetchImpl = options.fetch || globalThis.fetch;
144
183
  if (!fetchImpl) throw new Error("No fetch available, pass { fetch } or use Node 18+.");
145
- return new Http(baseUrl, apiKey, options.timeoutMs ?? DEFAULT_TIMEOUT_MS, fetchImpl.bind(globalThis));
184
+ const origin = { apiKeyFrom: key.from, baseUrlFrom: address.from, savedKey: !!saved.apiKey };
185
+ const timeout = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
186
+ return new Http(webAddress(address), key.value, timeout, fetchImpl.bind(globalThis), origin);
187
+ }
188
+ var LOCAL_HOST = /^(localhost|127\.)/i;
189
+ function webAddress({ value, from }) {
190
+ const url = value.trim().replace(/\/+$/, "");
191
+ if (/^https?:\/\//i.test(url)) return url;
192
+ const name = from.startsWith("the ") ? "baseUrl" : from === "OYA_BASE_URL" ? from : `baseUrl in ${from}`;
193
+ const suggestion = suggestionFor(url);
194
+ const tryIt = suggestion ? ` Try ${suggestion}.` : "";
195
+ throw refusal(`${name} must start with http:// or https://, not "${url}".${tryIt}`, "baseUrl", suggestion);
196
+ }
197
+ function suggestionFor(url) {
198
+ if (/^[a-z][a-z0-9+.-]*:/i.test(url) && !/^[^:/]+:\d+(\/|$)/.test(url)) return void 0;
199
+ return `${LOCAL_HOST.test(url) ? "http" : "https"}://${url}`;
200
+ }
201
+ function notInAHeader(ch) {
202
+ const code = ch.codePointAt(0);
203
+ return code < SPACE && code !== TAB || code === DEL || code > LATIN1_MAX;
204
+ }
205
+ function headerSafe(value, field, ending) {
206
+ const trimmed = value.trim();
207
+ const chars = Array.from(trimmed);
208
+ const at = chars.findIndex(notInAHeader);
209
+ if (at < 0) return trimmed;
210
+ const leading = Array.from(value).length - Array.from(value.trimStart()).length;
211
+ const where = `position ${leading + at + 1} (${codePoint(chars[at])})`;
212
+ throw refusal(`${field} has a character HTTP headers cannot carry at ${where}. ${ending}`, field);
213
+ }
214
+ var codePoint = (ch) => `U+${ch.codePointAt(0).toString(HEX).toUpperCase().padStart(CODE_POINT_DIGITS, "0")}`;
215
+ var shown = (value) => typeof value === "string" ? JSON.stringify(value) : String(value);
216
+ function segment(value, field = "id") {
217
+ if (typeof value !== "string" || !value)
218
+ throw refusal(`${field} must be a non-empty string, not ${shown(value)}`, field);
219
+ if (value.includes("/") || value === "." || value === "..")
220
+ throw refusal(`${field} must be a single path segment (no "/" and not "." or ".."), not ${shown(value)}`, field);
221
+ return encodeURIComponent(value);
146
222
  }
147
223
 
148
224
  // src/run-watch.ts
@@ -381,7 +457,7 @@ var Browser = class {
381
457
  * Play `'<name>:draft'` to try a draft before promoting it.
382
458
  */
383
459
  async play(name, data = {}, { autoHeal = true } = {}) {
384
- const path = `/api/browsers/${this.id}/playbooks/${encodeURIComponent(name)}/play`;
460
+ const path = `/api/browsers/${this.id}/playbooks/${segment(name, "name")}/play`;
385
461
  return agentAnswer(
386
462
  await this.http.request("POST", path, { variables: data, autoHeal }, AGENT_TIMEOUT_MS)
387
463
  );
@@ -432,7 +508,7 @@ var Browser = class {
432
508
  }
433
509
  /** Revoke a link from `shareUrl()` before it expires, by the id it returned. */
434
510
  async revokeShare(id) {
435
- await this.http.request("DELETE", `/api/control/credentials/${encodeURIComponent(id)}`);
511
+ await this.http.request("DELETE", `/api/control/credentials/${segment(id)}`);
436
512
  }
437
513
  /** Counters, health and the last 50 things this browser did. */
438
514
  status() {
@@ -488,8 +564,9 @@ function startBody(options) {
488
564
  const { profile, persona, provider, wsUrl, name, queueMs, priority, budgetUsd, governed, policy } = options;
489
565
  return { profile: profile || persona, provider, wsUrl, name, queueMs, priority, budgetUsd, governed, policy };
490
566
  }
567
+ var idempotencyKeyOf = ({ idempotencyKey }) => idempotencyKey ? headerSafe(idempotencyKey, "idempotencyKey", 'Use letters, digits, "-" and "_".') : globalThis.crypto.randomUUID();
491
568
  async function start(http, wait, options) {
492
- const headers = { "Idempotency-Key": options.idempotencyKey || globalThis.crypto.randomUUID() };
569
+ const headers = { "Idempotency-Key": idempotencyKeyOf(options) };
493
570
  const path = "/api/browsers/start";
494
571
  const started = await http().request("POST", path, startBody(options), START_TIMEOUT_MS, headers);
495
572
  if (started.status === "starting") {
@@ -498,7 +575,7 @@ async function start(http, wait, options) {
498
575
  }
499
576
  return new Browser(http(), started, options.captcha === "auto");
500
577
  }
501
- var fetchBrowser = (http, id) => http().request("GET", `/api/browsers/${encodeURIComponent(id)}`);
578
+ var fetchBrowser = (http, id) => http().request("GET", `/api/browsers/${segment(id)}`);
502
579
  async function reattach(http, id) {
503
580
  const found = await fetchBrowser(http, id);
504
581
  return new Browser(http(), asStarted(found), false);
@@ -510,69 +587,79 @@ function asStarted(found) {
510
587
  var stopBrowsers = (http, ids) => http().request("POST", "/api/browsers/stop", ids === "all" ? { all: true } : { ids }, START_TIMEOUT_MS);
511
588
  var browserApi = (http, wait) => ({
512
589
  /** Start a browser and wait until it can take commands. */
513
- start: (options = {}) => start(http, wait, options),
590
+ start: async (options = {}) => start(http, wait, options),
514
591
  /** Reattach to a browser that is already running. */
515
- get: (id) => reattach(http, id),
592
+ get: async (id) => reattach(http, id),
516
593
  /** Every browser on this key. */
517
- list: () => http().request("GET", "/api/browsers"),
594
+ list: async () => http().request("GET", "/api/browsers"),
518
595
  /** Stop some (`ids`) or every browser on this key. Each reports separately. */
519
- stop: (ids) => stopBrowsers(http, ids),
596
+ stop: async (ids) => stopBrowsers(http, ids),
520
597
  /** Stop every browser on this key; returns how many stopped. */
521
598
  stopAll: async () => (await stopBrowsers(http, "all")).stopped
522
599
  });
523
600
 
524
601
  // src/api/control.ts
525
- var session = (id, action = "") => `/api/control/sessions/${encodeURIComponent(id)}${action}`;
526
- var item = (kind, id) => `/api/control/${kind}/${encodeURIComponent(id)}`;
602
+ var session = (id, action = "") => `/api/control/sessions/${segment(id)}${action}`;
603
+ var item = (kind, id) => `/api/control/${kind}/${segment(id)}`;
527
604
  var sessionCalls = (http) => ({
528
605
  /** Settings, sessions and recent events at a glance. */
529
- overview: () => http().request("GET", "/api/control"),
606
+ overview: async () => http().request("GET", "/api/control"),
530
607
  /** Every session, including disconnected and cleanup-pending ones. */
531
- sessions: () => http().request("GET", "/api/control/sessions"),
608
+ sessions: async () => http().request("GET", "/api/control/sessions"),
532
609
  /** One session. */
533
- session: (id) => http().request("GET", session(id)),
610
+ session: async (id) => http().request("GET", session(id)),
534
611
  /** Update limits, rate cards and retention. */
535
- settings: (changes) => http().request("PATCH", "/api/control/project", changes)
612
+ settings: async (changes) => http().request("PATCH", "/api/control/project", changes)
536
613
  });
537
614
  var lifecycleCalls = (http) => ({
538
- /** Cancel queued or provisioning work. */
539
- cancel: (id) => http().request("POST", session(id, "/cancel"), {}),
615
+ /** Stop a session in any state; answers its final state. */
616
+ cancel: async (id) => http().request("POST", session(id, "/cancel"), {}),
540
617
  /** Stop a session; `force` stops despite a profile-save error, or reconciles. */
541
- stop: (id, force = false) => http().request("POST", session(id, "/stop"), { force }),
618
+ stop: async (id, force = false) => http().request("POST", session(id, "/stop"), { force }),
542
619
  /** Acquire or release human control, or acknowledge the agent's resume. */
543
- takeover: (id, action) => http().request("POST", session(id, "/control"), { action }),
620
+ takeover: async (id, action) => http().request("POST", session(id, "/control"), { action }),
544
621
  /** Send one input as the human holding the control lease. */
545
- input: (id, action, params) => http().request("POST", session(id, "/input"), { action, params })
622
+ input: async (id, action, params) => http().request("POST", session(id, "/input"), { action, params })
546
623
  });
624
+ function recoverBody(options) {
625
+ const { replace = false, wsUrl } = typeof options === "boolean" ? { replace: options } : options ?? {};
626
+ if (wsUrl && !replace)
627
+ throw refusal("wsUrl is only used with replace: true; recovering in place keeps the endpoint it had.", "wsUrl");
628
+ return wsUrl ? { replace, wsUrl } : { replace };
629
+ }
547
630
  var recoveryCalls = (http) => ({
548
- /** Explicitly recover a session, or replace it with a fresh one. */
549
- recover: (id, replace = false) => http().request("POST", session(id, "/recover"), { replace }),
631
+ /**
632
+ * Explicitly recover a session, or replace it with a fresh one. A cdp
633
+ * session's replacement needs the Chrome it runs on: `{ replace: true, wsUrl }`.
634
+ * `recover(id, true)` still means replace.
635
+ */
636
+ recover: async (id, options = {}) => http().request("POST", session(id, "/recover"), recoverBody(options)),
550
637
  /** A single-use ticket for the live stream. */
551
- ticket: (id) => http().request("POST", session(id, "/ticket"), {}),
638
+ ticket: async (id) => http().request("POST", session(id, "/ticket"), {}),
552
639
  /** Durable lifecycle events after a cursor. */
553
- events: (after = 0) => http().request("GET", `/api/control/events?after=${after}`)
640
+ events: async (after = 0) => http().request("GET", `/api/control/events?after=${after}`)
554
641
  });
555
642
  var credentialCalls = (http) => ({
556
643
  /** Mint a service credential. Its token is returned this once. */
557
- createCredential: (options) => http().request("POST", "/api/control/credentials", options),
644
+ createCredential: async (options) => http().request("POST", "/api/control/credentials", options),
558
645
  /** Revoke a service credential. */
559
- revokeCredential: (id) => http().request("DELETE", item("credentials", id))
646
+ revokeCredential: async (id) => http().request("DELETE", item("credentials", id))
560
647
  });
561
648
  var memberCalls = (http) => ({
562
649
  /** The owner and every member. */
563
- members: () => http().request("GET", "/api/control/members"),
650
+ members: async () => http().request("GET", "/api/control/members"),
564
651
  /** An invitation code for a new member. */
565
- inviteMember: (role = "operator") => http().request("POST", "/api/control/members/invite", { role }),
652
+ inviteMember: async (role = "operator") => http().request("POST", "/api/control/members/invite", { role }),
566
653
  /** Remove a member. */
567
- removeMember: (userId) => http().request("DELETE", item("members", userId))
654
+ removeMember: async (userId) => http().request("DELETE", item("members", userId))
568
655
  });
569
656
  var webhookCalls = (http) => ({
570
657
  /** Register a webhook; `types` limits which events it receives. */
571
- createWebhook: (url, types = []) => http().request("POST", "/api/control/webhooks", { url, types }),
658
+ createWebhook: async (url, types = []) => http().request("POST", "/api/control/webhooks", { url, types }),
572
659
  /** Disable a webhook. */
573
- removeWebhook: (id) => http().request("DELETE", item("webhooks", id)),
660
+ removeWebhook: async (id) => http().request("DELETE", item("webhooks", id)),
574
661
  /** Send a delivery again. */
575
- replayDelivery: (id) => http().request("POST", `${item("deliveries", id)}/replay`, {})
662
+ replayDelivery: async (id) => http().request("POST", `${item("deliveries", id)}/replay`, {})
576
663
  });
577
664
  var controlApi = (http) => ({
578
665
  ...sessionCalls(http),
@@ -584,7 +671,7 @@ var controlApi = (http) => ({
584
671
  });
585
672
 
586
673
  // src/api/playbooks.ts
587
- var playbook = (name) => `/api/playbooks/${encodeURIComponent(name)}`;
674
+ var playbook = (name) => `/api/playbooks/${segment(name, "name")}`;
588
675
  var playbookApi = (http) => ({
589
676
  /** Every saved playbook, with any draft waiting on it. */
590
677
  list: async () => (await http().request("GET", "/api/playbooks")).playbooks,
@@ -593,7 +680,7 @@ var playbookApi = (http) => ({
593
680
  await http().request("DELETE", playbook(name));
594
681
  },
595
682
  /** Replace a playbook with the draft a healed replay saved. Try it first with `browser.play('<name>:draft')`. */
596
- promote: (name) => http().request("POST", `${playbook(name)}/promote`, {})
683
+ promote: async (name) => http().request("POST", `${playbook(name)}/promote`, {})
597
684
  });
598
685
 
599
686
  // src/api/proxies.ts
@@ -601,10 +688,10 @@ var proxyApi = (http) => ({
601
688
  /** Every proxy this key can use, shared ones included. */
602
689
  list: async () => (await http().request("GET", "/api/proxies")).proxies,
603
690
  /** Add a proxy. Its credentials are never read back. */
604
- create: (proxy) => http().request("POST", "/api/proxies", proxy),
691
+ create: async (proxy) => http().request("POST", "/api/proxies", proxy),
605
692
  /** Remove one of this key's proxies. */
606
693
  remove: async (id) => {
607
- await http().request("DELETE", `/api/proxies/${encodeURIComponent(id)}`);
694
+ await http().request("DELETE", `/api/proxies/${segment(id)}`);
608
695
  },
609
696
  /** Dial each proxy and learn its real exit IP. Failing ones cool down and are skipped. */
610
697
  check: async () => (await http().request("POST", "/api/proxies/check", {})).results
@@ -615,35 +702,38 @@ var identityCalls = (http) => ({
615
702
  /** Every persona on this key. */
616
703
  list: async () => (await http().request("GET", "/api/personas")).personas,
617
704
  /** One persona. */
618
- get: (id) => http().request("GET", `/api/personas/${id}`),
705
+ get: async (id) => http().request("GET", `/api/personas/${segment(id)}`),
619
706
  /**
620
707
  * Create an identity. The device, platform, timezone, locale, is chosen
621
708
  * here and fixed for its life; `preview()` shows what a choice produces.
622
709
  */
623
- create: (options = {}) => http().request("POST", "/api/personas", options),
710
+ create: async (options = {}) => http().request("POST", "/api/personas", options),
624
711
  /** Name, concurrency cap and proxy hint. Never the device, clone for that. */
625
- update: (id, changes) => http().request("PUT", `/api/personas/${id}`, changes),
712
+ update: async (id, changes) => http().request("PUT", `/api/personas/${segment(id)}`, changes),
626
713
  /** A new persona of the same kind of device: same choices, fresh identity, empty jar. */
627
- clone: (id, options = {}) => http().request("POST", `/api/personas/${id}/clone`, options)
714
+ clone: async (id, options = {}) => http().request("POST", `/api/personas/${segment(id)}/clone`, options)
628
715
  });
629
716
  var deviceCalls = (http) => ({
630
717
  /** The fingerprint these choices would produce. Persists nothing. */
631
718
  preview: async (prefs = {}) => (await http().request("POST", "/api/personas/preview", { prefs })).fingerprint,
632
719
  /** Platforms, and the timezones and locales each may coherently claim. */
633
- options: () => http().request("GET", "/api/personas/options"),
720
+ options: async () => http().request("GET", "/api/personas/options"),
634
721
  /** Pin the persona to one of your proxies, or `null` to let assignment happen at connect. */
635
- pinProxy: (id, proxyId) => http().request("PUT", `/api/personas/${id}/proxy`, { proxyId }),
722
+ pinProxy: async (id, proxyId) => http().request("PUT", `/api/personas/${segment(id)}/proxy`, { proxyId }),
636
723
  /** Delete a persona. */
637
724
  remove: async (id) => {
638
- await http().request("DELETE", `/api/personas/${id}`);
725
+ await http().request("DELETE", `/api/personas/${segment(id)}`);
639
726
  }
640
727
  });
641
728
  var mfaCalls = (http) => ({
642
729
  /** Store the second factor for this identity. Sealed at rest, never read back. */
643
- setMfa: (id, config) => http().request("PUT", `/api/personas/${id}/mfa`, config),
730
+ setMfa: async (id, config) => http().request("PUT", `/api/personas/${segment(id)}/mfa`, config),
644
731
  /** Remove the persona-wide factor, or the one filed against `domain`. */
645
732
  clearMfa: async (id, domain) => {
646
- await http().request("DELETE", `/api/personas/${id}/mfa${domain ? `?domain=${encodeURIComponent(domain)}` : ""}`);
733
+ await http().request(
734
+ "DELETE",
735
+ `/api/personas/${segment(id)}/mfa${domain ? `?domain=${encodeURIComponent(domain)}` : ""}`
736
+ );
647
737
  }
648
738
  });
649
739
  var loginCalls = (http) => ({
@@ -654,19 +744,36 @@ var loginCalls = (http) => ({
654
744
  * better path. This is for portals that expire a session server-side
655
745
  * between runs, where an unattended run has nothing else to recover with.
656
746
  */
657
- setCredentials: (id, config) => http().request("PUT", `/api/personas/${id}/credentials`, config),
747
+ setCredentials: async (id, config) => http().request("PUT", `/api/personas/${segment(id)}/credentials`, config),
658
748
  /** Which sites this identity can sign in to. Usernames only. */
659
- credentials: (id) => http().request("GET", `/api/personas/${id}/credentials`),
749
+ credentials: async (id) => http().request("GET", `/api/personas/${segment(id)}/credentials`),
660
750
  /** Remove the login stored for one site. */
661
751
  clearCredentials: async (id, domain) => {
662
- await http().request("DELETE", `/api/personas/${id}/credentials?domain=${encodeURIComponent(domain)}`);
752
+ await http().request("DELETE", `/api/personas/${segment(id)}/credentials?domain=${encodeURIComponent(domain)}`);
753
+ }
754
+ });
755
+ var jarPath = (id) => `/api/pool/cookies?persona=${segment(id)}`;
756
+ var jarCalls = (http) => ({
757
+ /** Every cookie in the jar; `format: 'playwright'` is ready for `context.addCookies()`. */
758
+ cookies: async (id, format = "json") => (await http().request("GET", `${jarPath(id)}&format=${encodeURIComponent(format)}`)).cookies,
759
+ /** Merge cookies into the jar. The persona's browsers pick them up on their next visit to each site. */
760
+ importCookies: async (id, list) => http().request("PUT", jarPath(id), { cookies: list })
761
+ });
762
+ var cookieCalls = (http) => ({
763
+ ...jarCalls(http),
764
+ /** Copy every login of persona `from` into persona `to`, which keeps its own device. */
765
+ copyCookies: async (from, to) => jarCalls(http).importCookies(to, await jarCalls(http).cookies(from)),
766
+ /** Forget every cookie in the jar: signs the persona out everywhere. */
767
+ clearCookies: async (id) => {
768
+ await http().request("DELETE", jarPath(id));
663
769
  }
664
770
  });
665
771
  var personaApi = (http) => ({
666
772
  ...identityCalls(http),
667
773
  ...deviceCalls(http),
668
774
  ...mfaCalls(http),
669
- ...loginCalls(http)
775
+ ...loginCalls(http),
776
+ ...cookieCalls(http)
670
777
  });
671
778
 
672
779
  // src/api/config.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oya-ai/browser",
3
- "version": "1.0.107",
3
+ "version": "1.0.111",
4
4
  "description": "Rotate thousands of browsers behind one API, personas, proxies, stealth, CAPTCHA and MFA.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://oyabrowser.com",