notte-sdk 1.10.12 → 1.10.14

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.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var SDK_VERSION = true ? "1.10.12" : "dev";
2
+ var SDK_VERSION = true ? "1.10.14" : "dev";
3
3
  var SDK_PACKAGE_NAME = true ? "notte-sdk" : "notte-sdk";
4
4
 
5
5
  // src/version-check.ts
@@ -961,6 +961,11 @@ var sessionStatus = (options) => (options.client ?? client).get({
961
961
  url: "/sessions/{session_id}",
962
962
  ...options
963
963
  });
964
+ var sessionSteps = (options) => (options.client ?? client).get({
965
+ security: [{ scheme: "bearer", type: "http" }],
966
+ url: "/sessions/{session_id}/steps",
967
+ ...options
968
+ });
964
969
  var sessionProfilePreview = (options) => (options.client ?? client).get({
965
970
  security: [{ scheme: "bearer", type: "http" }],
966
971
  url: "/sessions/{session_id}/profile-preview",
@@ -1466,49 +1471,6 @@ var profileList = (options) => (options?.client ?? client).get({
1466
1471
  url: "/profiles",
1467
1472
  ...options
1468
1473
  });
1469
- var personaCreate = (options) => (options.client ?? client).post({
1470
- security: [{ scheme: "bearer", type: "http" }],
1471
- url: "/personas/create",
1472
- ...options,
1473
- headers: {
1474
- "Content-Type": "application/json",
1475
- ...options.headers
1476
- }
1477
- });
1478
- var personaDelete = (options) => (options.client ?? client).delete({
1479
- security: [{ scheme: "bearer", type: "http" }],
1480
- url: "/personas/{persona_id}",
1481
- ...options
1482
- });
1483
- var personaGet = (options) => (options.client ?? client).get({
1484
- security: [{ scheme: "bearer", type: "http" }],
1485
- url: "/personas/{persona_id}",
1486
- ...options
1487
- });
1488
- var personaUpdate = (options) => (options.client ?? client).patch({
1489
- security: [{ scheme: "bearer", type: "http" }],
1490
- url: "/personas/{persona_id}",
1491
- ...options,
1492
- headers: {
1493
- "Content-Type": "application/json",
1494
- ...options.headers
1495
- }
1496
- });
1497
- var personaEmailsList = (options) => (options.client ?? client).get({
1498
- security: [{ scheme: "bearer", type: "http" }],
1499
- url: "/personas/{persona_id}/emails",
1500
- ...options
1501
- });
1502
- var listPersonas = (options) => (options?.client ?? client).get({
1503
- security: [{ scheme: "bearer", type: "http" }],
1504
- url: "/personas",
1505
- ...options
1506
- });
1507
- var personaSmsList = (options) => (options.client ?? client).get({
1508
- security: [{ scheme: "bearer", type: "http" }],
1509
- url: "/personas/{persona_id}/sms",
1510
- ...options
1511
- });
1512
1474
  var mailboxConnectToken = (options) => (options.client ?? client).post({
1513
1475
  security: [{ scheme: "bearer", type: "http" }],
1514
1476
  url: "/mailboxes/connect-token",
@@ -1620,11 +1582,6 @@ var deleteSecret = (options) => (options.client ?? client).delete({
1620
1582
  url: "/secrets/{secret_id}",
1621
1583
  ...options
1622
1584
  });
1623
- var listSecretProxyHostPresets = (options) => (options?.client ?? client).get({
1624
- security: [{ scheme: "bearer", type: "http" }],
1625
- url: "/secret-proxy/host-presets",
1626
- ...options
1627
- });
1628
1585
  var healthCheck = (options) => (options?.client ?? client).get({ url: "/health", ...options });
1629
1586
  var readyCheck = (options) => (options?.client ?? client).get({ url: "/ready", ...options });
1630
1587
  var scrapeWebpage = (options) => (options.client ?? client).post({
@@ -1868,6 +1825,54 @@ var listAuditEvents = (options) => (options?.client ?? client).get({
1868
1825
  url: "/audit-events",
1869
1826
  ...options
1870
1827
  });
1828
+ var listPersonas = (options) => (options?.client ?? client).get({
1829
+ security: [{ scheme: "bearer", type: "http" }],
1830
+ url: "/personas",
1831
+ ...options
1832
+ });
1833
+ var personaCreate = (options) => (options.client ?? client).post({
1834
+ security: [{ scheme: "bearer", type: "http" }],
1835
+ url: "/personas/create",
1836
+ ...options,
1837
+ headers: {
1838
+ "Content-Type": "application/json",
1839
+ ...options.headers
1840
+ }
1841
+ });
1842
+ var personaDelete = (options) => (options.client ?? client).delete({
1843
+ security: [{ scheme: "bearer", type: "http" }],
1844
+ url: "/personas/{persona_id}",
1845
+ ...options
1846
+ });
1847
+ var personaGet = (options) => (options.client ?? client).get({
1848
+ security: [{ scheme: "bearer", type: "http" }],
1849
+ url: "/personas/{persona_id}",
1850
+ ...options
1851
+ });
1852
+ var personaUpdate = (options) => (options.client ?? client).patch({
1853
+ security: [{ scheme: "bearer", type: "http" }],
1854
+ url: "/personas/{persona_id}",
1855
+ ...options,
1856
+ headers: {
1857
+ "Content-Type": "application/json",
1858
+ ...options.headers
1859
+ }
1860
+ });
1861
+ var personaEmailsList = (options) => (options.client ?? client).get({
1862
+ security: [{ scheme: "bearer", type: "http" }],
1863
+ url: "/personas/{persona_id}/emails",
1864
+ ...options
1865
+ });
1866
+ var personaSmsList = (options) => (options.client ?? client).get({
1867
+ security: [{ scheme: "bearer", type: "http" }],
1868
+ url: "/personas/{persona_id}/sms",
1869
+ ...options
1870
+ });
1871
+ var listSecretProxyHostPresets = (options) => (options?.client ?? client).get({
1872
+ security: [{ scheme: "bearer", type: "http" }],
1873
+ url: "/secret-proxy/host-presets",
1874
+ ...options
1875
+ });
1871
1876
  var getUsage = (options) => (options?.client ?? client).get({
1872
1877
  security: [{ scheme: "bearer", type: "http" }],
1873
1878
  url: "/usage",
@@ -1913,6 +1918,9 @@ var getSessionScript = (options) => (options.client ?? client).get({
1913
1918
  ...options
1914
1919
  });
1915
1920
 
1921
+ // src/run-statuses.ts
1922
+ var ACCEPT_RUN_STATUSES_HEADER = "x-notte-accept-run-statuses";
1923
+
1916
1924
  // src/managed-auth.ts
1917
1925
  import { setTimeout as delay } from "timers/promises";
1918
1926
 
@@ -1973,15 +1981,32 @@ var FailedToRunCloudFunctionError = class extends NotteError {
1973
1981
  functionId;
1974
1982
  functionRunId;
1975
1983
  response;
1976
- constructor(functionId, functionRunId, response) {
1984
+ constructor(functionId, functionRunId, response, message) {
1977
1985
  super(
1978
- `Function ${functionId} run ${functionRunId} failed: ${typeof response === "string" ? response : describeBody(response ?? {})}`
1986
+ message ?? `Function ${functionId} run ${functionRunId} failed: ${typeof response === "string" ? response : describeBody(response ?? {})}`
1979
1987
  );
1980
1988
  this.functionId = functionId;
1981
1989
  this.functionRunId = functionRunId;
1982
1990
  this.response = response;
1983
1991
  }
1984
1992
  };
1993
+ var FunctionRunRejectedError = class extends FailedToRunCloudFunctionError {
1994
+ error;
1995
+ code;
1996
+ constructor(functionId, functionRunId, response) {
1997
+ const error = response.error ?? null;
1998
+ const code = typeof error?.code === "string" && error.code ? error.code : null;
1999
+ const detail2 = typeof error?.message === "string" && error.message ? error.message : `result '${typeof response.result === "string" ? response.result : JSON.stringify(response.result)}'`;
2000
+ super(
2001
+ functionId,
2002
+ functionRunId,
2003
+ response,
2004
+ `Function ${functionId} with run_id=${functionRunId} was rejected${code ? ` (${code})` : ""}: ${detail2}`
2005
+ );
2006
+ this.error = error;
2007
+ this.code = code;
2008
+ }
2009
+ };
1985
2010
  var ScrapeFailedError = class extends NotteError {
1986
2011
  structuredError;
1987
2012
  constructor(error) {
@@ -2014,6 +2039,11 @@ var ActionExecutionError = class _ActionExecutionError extends NotteError {
2014
2039
  });
2015
2040
  }
2016
2041
  };
2042
+ var CaptchaRejectedError = class extends ActionExecutionError {
2043
+ constructor(message) {
2044
+ super({ error_type: "CaptchaRejectedError", dev_message: message, user_message: message, agent_message: message });
2045
+ }
2046
+ };
2017
2047
  var EvaluateJsNoDataError = class extends NotteError {
2018
2048
  constructor() {
2019
2049
  super("evaluate_js returned no data. Upgrade the Notte API to a build that returns the evaluated value.");
@@ -2422,6 +2452,7 @@ function createResources(client2, getApiKey) {
2422
2452
  },
2423
2453
  /** functionRunStart: /functions/{function_id}/runs/start. Returns the API response body. */
2424
2454
  runStart: async (functionId, body, requestOptions = {}) => {
2455
+ if (typeof functionId !== "string" || !functionId.trim()) throw new TypeError("runStart requires a non-empty functionId.");
2425
2456
  const response = await functionRunStart({ client: client2, throwOnError: true, signal: requestOptions.signal, headers: { ...requestOptions.headers, "x-notte-api-key": getApiKey() }, path: { "function_id": functionId }, body });
2426
2457
  return response.data;
2427
2458
  },
@@ -2657,6 +2688,9 @@ async function executeWithCaptcha(action, budgetSeconds, request) {
2657
2688
  await pause();
2658
2689
  continue;
2659
2690
  }
2691
+ if (status.state === "failed" && status.failure_reason === "rejected") {
2692
+ return failure(status.message || "CAPTCHA rejected by the site", "captcha_rejected");
2693
+ }
2660
2694
  if (status.state === "failed" || status.state === "cancelled") {
2661
2695
  return failure(status.message || `CAPTCHA ${status.state}`, `captcha_${status.state}`);
2662
2696
  }
@@ -3268,6 +3302,25 @@ function abortableSleep(ms, signal) {
3268
3302
  });
3269
3303
  }
3270
3304
 
3305
+ // src/session-steps.ts
3306
+ async function getSessionSteps(client2, sessionId, options = {}) {
3307
+ const response = await client2.getClient().get({
3308
+ url: `/sessions/${encodeURIComponent(sessionId)}/steps`,
3309
+ query: { cursor: options.cursor, limit: options.limit },
3310
+ throwOnError: true
3311
+ });
3312
+ return { steps: response.data.steps ?? [], next_cursor: response.data.next_cursor ?? null };
3313
+ }
3314
+ async function* iterSessionSteps(client2, sessionId, options = {}) {
3315
+ let cursor;
3316
+ while (true) {
3317
+ const page = await getSessionSteps(client2, sessionId, { cursor, limit: options.limit });
3318
+ yield* page.steps;
3319
+ if (page.next_cursor === null) return;
3320
+ cursor = page.next_cursor;
3321
+ }
3322
+ }
3323
+
3271
3324
  // src/session.ts
3272
3325
  var SESSION_START_TRIES = 3;
3273
3326
  var CLUSTER_OVERLOAD_RETRY_DELAY_MS = 3e4;
@@ -3778,6 +3831,36 @@ var Session = class {
3778
3831
  });
3779
3832
  return response.data;
3780
3833
  }
3834
+ /**
3835
+ * Get one page of the session's steps. Each step has the shape of an item of
3836
+ * `status().steps`. Pass the page's `next_cursor` back as `cursor`, as is, to
3837
+ * get the next page; it is `null` on the last page. Works after `stop()`.
3838
+ *
3839
+ * @throws NotteAPIError with status 400 when `cursor` is no longer valid: restart from the first page (no `cursor`).
3840
+ * @example
3841
+ * let page = await session.steps({ limit: 50 });
3842
+ * while (page.next_cursor !== null) {
3843
+ * page = await session.steps({ cursor: page.next_cursor, limit: 50 });
3844
+ * }
3845
+ */
3846
+ async steps(options = {}) {
3847
+ return getSessionSteps(this.client, this.lastSessionId(), options);
3848
+ }
3849
+ /**
3850
+ * Iterate over all of the session's steps, following `next_cursor` page by page.
3851
+ * A cursor that is no longer valid stops the iteration with a `NotteAPIError`
3852
+ * with status 400, after the steps already yielded; it is not restarted here,
3853
+ * since that would yield those steps again. To read the steps again, start a
3854
+ * new iteration. Works after `stop()`.
3855
+ *
3856
+ * @example
3857
+ * for await (const step of session.iterSteps({ limit: 50 })) {
3858
+ * console.log(step);
3859
+ * }
3860
+ */
3861
+ async *iterSteps(options = {}) {
3862
+ yield* iterSessionSteps(this.client, this.lastSessionId(), options);
3863
+ }
3781
3864
  // #######################################################################
3782
3865
  // ############################# Debug / CDP #############################
3783
3866
  // #######################################################################
@@ -3937,6 +4020,9 @@ var Session = class {
3937
4020
  );
3938
4021
  if (raiseOnFailure && !result.success) {
3939
4022
  console.error(`\u{1F6A8} Execution failed with message: '${result.message}'`);
4023
+ if (result.code === "captcha_rejected") {
4024
+ throw new CaptchaRejectedError(String(result.message ?? "").trim() || "CAPTCHA rejected by the site");
4025
+ }
3940
4026
  if (result.exception_detail) {
3941
4027
  throw new ActionExecutionError(result.exception_detail);
3942
4028
  }
@@ -4191,15 +4277,19 @@ var NotteVault = class {
4191
4277
  _vaultId = null;
4192
4278
  initPromise = null;
4193
4279
  createData = null;
4280
+ // Only a vault this object created is deleted by stop().
4281
+ created;
4194
4282
  constructor(client2, options = {}) {
4195
4283
  this.client = client2;
4196
4284
  if (options && "vault_id" in options && options.vault_id !== void 0) {
4285
+ this.created = false;
4197
4286
  if (options.vault_id.length === 0) {
4198
4287
  throw new InvalidRequestError("Vault ID cannot be empty");
4199
4288
  }
4200
4289
  this.initPromise = this.initExistingVault(options.vault_id);
4201
4290
  this.initPromise.catch(() => void 0);
4202
4291
  } else {
4292
+ this.created = true;
4203
4293
  const { vault_id: _ignored, ...createData } = options ?? {};
4204
4294
  this.createData = createData;
4205
4295
  }
@@ -4255,13 +4345,17 @@ var NotteVault = class {
4255
4345
  start() {
4256
4346
  }
4257
4347
  /**
4258
- * Stop the vault: deletes it together with every credential it holds.
4348
+ * Stop the vault: deletes it together with every credential it holds, if
4349
+ * this object created it. A vault opened by `vault_id` is kept.
4259
4350
  *
4260
4351
  * ```ts
4261
4352
  * await vault.stop();
4262
4353
  * ```
4263
4354
  */
4264
4355
  async stop() {
4356
+ if (!this.created) {
4357
+ return;
4358
+ }
4265
4359
  const vaultId = await this.ensureInitialized();
4266
4360
  console.info(`[Vault] ${vaultId} deleted. All credentials have been deleted.`);
4267
4361
  await this.delete();
@@ -4837,7 +4931,7 @@ var FUNCTION_RUN_ENDPOINTS = {
4837
4931
  start: "/functions/{function_id}/runs/{run_id}"
4838
4932
  };
4839
4933
  var RUN_API_KEY_HEADER = "x-notte-api-key";
4840
- var FUNCTION_RUN_STATUSES = ["closed", "active", "failed"];
4934
+ var FUNCTION_RUN_STATUSES = ["closed", "active", "failed", "rejected"];
4841
4935
  var NotteFunction = class {
4842
4936
  client;
4843
4937
  _functionId = null;
@@ -5156,13 +5250,15 @@ var NotteFunction = class {
5156
5250
  * to execute a record returned by `createRun()`; otherwise a new run record
5157
5251
  * is created first, exactly like Python. Pass `version` to run a saved
5158
5252
  * version instead of the latest one. A failed execution throws
5159
- * `FailedToRunCloudFunctionError` unless `raiseOnFailure` is false, and the
5160
- * wait is aborted with `NotteTimeoutError` after `timeoutMs`.
5253
+ * `FailedToRunCloudFunctionError` and a rejected one (the function raised an
5254
+ * `ExpectedFunctionError`) its subclass `FunctionRunRejectedError`, unless
5255
+ * `raiseOnFailure` is false; the wait is aborted with `NotteTimeoutError` after `timeoutMs`.
5161
5256
  *
5162
5257
  * @param variables - JSON-serializable keyword arguments passed to the Python function's `run` entry point.
5163
5258
  * @param options - Log streaming, failure handling, runtime, run ID, version, and timeout settings.
5164
5259
  * @returns The completed execution response. The function's return value is in `result`. Disabling streaming still waits for completion.
5165
5260
  * @throws FailedToRunCloudFunctionError if execution fails and `raiseOnFailure` is true.
5261
+ * @throws FunctionRunRejectedError if the run is rejected and `raiseOnFailure` is true.
5166
5262
  * @throws NotteTimeoutError if the execution request exceeds `timeoutMs`.
5167
5263
  * @throws InvalidRequestError if the run options are invalid.
5168
5264
  * @example
@@ -5215,6 +5311,9 @@ var NotteFunction = class {
5215
5311
  signal: controller.signal
5216
5312
  });
5217
5313
  const result = stream ? await readFunctionStream(raw, options.onLog ?? console.log, endpointPath, controller.signal) : parseFunctionRunResult(raw, endpointPath);
5314
+ if (result.status === "rejected" && (options.raiseOnFailure ?? true)) {
5315
+ throw new FunctionRunRejectedError(functionId, result.function_run_id, result);
5316
+ }
5218
5317
  if (result.status === "failed" && (options.raiseOnFailure ?? true)) {
5219
5318
  throw new FailedToRunCloudFunctionError(functionId, result.function_run_id, result);
5220
5319
  }
@@ -5367,12 +5466,15 @@ function parseFunctionRunResult(value, path) {
5367
5466
  if (typeof candidate.function_run_id !== "string" || typeof candidate.function_id !== "string" || typeof status !== "string" || !FUNCTION_RUN_STATUSES.includes(status) || !("result" in candidate)) {
5368
5467
  throw new NotteError("Invalid function result: missing function_id, function_run_id, status or result");
5369
5468
  }
5469
+ const error = candidate.error;
5370
5470
  return {
5371
5471
  function_id: candidate.function_id,
5372
5472
  function_run_id: candidate.function_run_id,
5373
5473
  session_id: typeof candidate.session_id === "string" ? candidate.session_id : null,
5374
5474
  result: candidate.result,
5375
- status
5475
+ status,
5476
+ // Kept as the API sent it; absent when the run reported no typed error.
5477
+ ...typeof error === "object" && error !== null && !Array.isArray(error) ? { error } : {}
5376
5478
  };
5377
5479
  }
5378
5480
  async function readFunctionStream(body, onLog, path, signal) {
@@ -5944,6 +6046,12 @@ var EXECUTION_ERROR_CLASS = "NotteApiExecutionError";
5944
6046
  function isRelativeProxyUrl(baseUrl) {
5945
6047
  return /^\/(?!\/)/.test(baseUrl);
5946
6048
  }
6049
+ function isFunctionsRequest(requestUrl, baseUrl) {
6050
+ const url = new URL(requestUrl);
6051
+ const api = new URL(baseUrl, url);
6052
+ const prefix = `${api.pathname.replace(/\/$/, "")}/functions`;
6053
+ return url.origin === api.origin && (url.pathname === prefix || url.pathname.startsWith(`${prefix}/`));
6054
+ }
5947
6055
  var NotteClient3 = class {
5948
6056
  config;
5949
6057
  client = createClient();
@@ -6002,6 +6110,9 @@ var NotteClient3 = class {
6002
6110
  headers.set("x-notte-request-origin", REQUEST_ORIGIN);
6003
6111
  headers.set("x-notte-sdk-version", SDK_VERSION);
6004
6112
  if (this.config.dbPreview) headers.set(DB_PREVIEW_HEADER, this.config.dbPreview);
6113
+ if (!headers.has(ACCEPT_RUN_STATUSES_HEADER) && isFunctionsRequest(request.url, this.config.baseUrl)) {
6114
+ headers.set(ACCEPT_RUN_STATUSES_HEADER, "rejected");
6115
+ }
6005
6116
  const override = headers.get(TIMEOUT_HEADER);
6006
6117
  headers.delete(TIMEOUT_HEADER);
6007
6118
  const retriesOverride = headers.get(MAX_RETRIES_HEADER);
@@ -6217,7 +6328,27 @@ var NotteClient3 = class {
6217
6328
  list: async (options = {}) => {
6218
6329
  const response = await listSessions({ client: this.getClient(), query: options });
6219
6330
  return response.data?.items ?? [];
6220
- }
6331
+ },
6332
+ /**
6333
+ * One page of the session's steps (`{ steps, next_cursor }`), from
6334
+ * `GET /sessions/{session_id}/steps`. Each step has the shape of an item of
6335
+ * `status().steps`. Pass `next_cursor` back as `cursor`, as is, for the next
6336
+ * page; it is `null` on the last page. A `NotteAPIError` with status 400 means
6337
+ * the cursor is no longer valid: restart from the first page (no `cursor`).
6338
+ */
6339
+ steps: (sessionId, options = {}) => getSessionSteps(this, sessionId, options),
6340
+ /**
6341
+ * Every step of the session, following `next_cursor` page by page. A cursor
6342
+ * that is no longer valid stops the iteration with the 400 `NotteAPIError`,
6343
+ * after the steps already yielded; start a new iteration to read them again.
6344
+ *
6345
+ * ```ts
6346
+ * for await (const step of client.sessions.iterSteps(sessionId, { limit: 50 })) {
6347
+ * console.log(step);
6348
+ * }
6349
+ * ```
6350
+ */
6351
+ iterSteps: (sessionId, options = {}) => iterSessionSteps(this, sessionId, options)
6221
6352
  };
6222
6353
  }
6223
6354
  /** Client-bound vault API operations. `list()` retains its array return value. */
@@ -6460,6 +6591,7 @@ export {
6460
6591
  CAPTCHA_SOLVE_TIMEOUT_MS,
6461
6592
  CLUSTER_OVERLOAD_RETRY_DELAY_MS,
6462
6593
  CREDENTIAL_FIELDS,
6594
+ CaptchaRejectedError,
6463
6595
  DEFAULT_MAX_RETRIES,
6464
6596
  DEFAULT_NOTTE_API_URL,
6465
6597
  DEFAULT_REQUEST_TIMEOUT_MS,
@@ -6471,6 +6603,7 @@ export {
6471
6603
  FetchResponseDecodeError,
6472
6604
  FileExistsError,
6473
6605
  FileNotFoundError,
6606
+ FunctionRunRejectedError,
6474
6607
  InvalidRequestError,
6475
6608
  ManagedAuthError,
6476
6609
  NotteAPIError,
@@ -6647,6 +6780,7 @@ export {
6647
6780
  sessionResume,
6648
6781
  sessionStart,
6649
6782
  sessionStatus,
6783
+ sessionSteps,
6650
6784
  sessionStop,
6651
6785
  startVersionCheck,
6652
6786
  storeSecret,