notte-sdk 1.10.11 → 1.10.13

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
@@ -35,6 +35,7 @@ __export(index_exports, {
35
35
  CAPTCHA_SOLVE_TIMEOUT_MS: () => CAPTCHA_SOLVE_TIMEOUT_MS,
36
36
  CLUSTER_OVERLOAD_RETRY_DELAY_MS: () => CLUSTER_OVERLOAD_RETRY_DELAY_MS,
37
37
  CREDENTIAL_FIELDS: () => CREDENTIAL_FIELDS,
38
+ CaptchaRejectedError: () => CaptchaRejectedError,
38
39
  DEFAULT_MAX_RETRIES: () => DEFAULT_MAX_RETRIES,
39
40
  DEFAULT_NOTTE_API_URL: () => DEFAULT_NOTTE_API_URL,
40
41
  DEFAULT_REQUEST_TIMEOUT_MS: () => DEFAULT_REQUEST_TIMEOUT_MS,
@@ -46,6 +47,7 @@ __export(index_exports, {
46
47
  FetchResponseDecodeError: () => FetchResponseDecodeError,
47
48
  FileExistsError: () => FileExistsError,
48
49
  FileNotFoundError: () => FileNotFoundError,
50
+ FunctionRunRejectedError: () => FunctionRunRejectedError,
49
51
  InvalidRequestError: () => InvalidRequestError,
50
52
  ManagedAuthError: () => ManagedAuthError,
51
53
  NotteAPIError: () => NotteAPIError,
@@ -247,7 +249,7 @@ __export(index_exports, {
247
249
  module.exports = __toCommonJS(index_exports);
248
250
 
249
251
  // src/version.ts
250
- var SDK_VERSION = true ? "1.10.11" : "dev";
252
+ var SDK_VERSION = true ? "1.10.13" : "dev";
251
253
  var SDK_PACKAGE_NAME = true ? "notte-sdk" : "notte-sdk";
252
254
 
253
255
  // src/version-check.ts
@@ -2161,6 +2163,9 @@ var getSessionScript = (options) => (options.client ?? client).get({
2161
2163
  ...options
2162
2164
  });
2163
2165
 
2166
+ // src/run-statuses.ts
2167
+ var ACCEPT_RUN_STATUSES_HEADER = "x-notte-accept-run-statuses";
2168
+
2164
2169
  // src/managed-auth.ts
2165
2170
  var import_promises = require("timers/promises");
2166
2171
 
@@ -2221,15 +2226,32 @@ var FailedToRunCloudFunctionError = class extends NotteError {
2221
2226
  functionId;
2222
2227
  functionRunId;
2223
2228
  response;
2224
- constructor(functionId, functionRunId, response) {
2229
+ constructor(functionId, functionRunId, response, message) {
2225
2230
  super(
2226
- `Function ${functionId} run ${functionRunId} failed: ${typeof response === "string" ? response : describeBody(response ?? {})}`
2231
+ message ?? `Function ${functionId} run ${functionRunId} failed: ${typeof response === "string" ? response : describeBody(response ?? {})}`
2227
2232
  );
2228
2233
  this.functionId = functionId;
2229
2234
  this.functionRunId = functionRunId;
2230
2235
  this.response = response;
2231
2236
  }
2232
2237
  };
2238
+ var FunctionRunRejectedError = class extends FailedToRunCloudFunctionError {
2239
+ error;
2240
+ code;
2241
+ constructor(functionId, functionRunId, response) {
2242
+ const error = response.error ?? null;
2243
+ const code = typeof error?.code === "string" && error.code ? error.code : null;
2244
+ const detail2 = typeof error?.message === "string" && error.message ? error.message : `result '${typeof response.result === "string" ? response.result : JSON.stringify(response.result)}'`;
2245
+ super(
2246
+ functionId,
2247
+ functionRunId,
2248
+ response,
2249
+ `Function ${functionId} with run_id=${functionRunId} was rejected${code ? ` (${code})` : ""}: ${detail2}`
2250
+ );
2251
+ this.error = error;
2252
+ this.code = code;
2253
+ }
2254
+ };
2233
2255
  var ScrapeFailedError = class extends NotteError {
2234
2256
  structuredError;
2235
2257
  constructor(error) {
@@ -2262,6 +2284,11 @@ var ActionExecutionError = class _ActionExecutionError extends NotteError {
2262
2284
  });
2263
2285
  }
2264
2286
  };
2287
+ var CaptchaRejectedError = class extends ActionExecutionError {
2288
+ constructor(message) {
2289
+ super({ error_type: "CaptchaRejectedError", dev_message: message, user_message: message, agent_message: message });
2290
+ }
2291
+ };
2265
2292
  var EvaluateJsNoDataError = class extends NotteError {
2266
2293
  constructor() {
2267
2294
  super("evaluate_js returned no data. Upgrade the Notte API to a build that returns the evaluated value.");
@@ -2905,6 +2932,9 @@ async function executeWithCaptcha(action, budgetSeconds, request) {
2905
2932
  await pause();
2906
2933
  continue;
2907
2934
  }
2935
+ if (status.state === "failed" && status.failure_reason === "rejected") {
2936
+ return failure(status.message || "CAPTCHA rejected by the site", "captcha_rejected");
2937
+ }
2908
2938
  if (status.state === "failed" || status.state === "cancelled") {
2909
2939
  return failure(status.message || `CAPTCHA ${status.state}`, `captcha_${status.state}`);
2910
2940
  }
@@ -4185,6 +4215,9 @@ var Session = class {
4185
4215
  );
4186
4216
  if (raiseOnFailure && !result.success) {
4187
4217
  console.error(`\u{1F6A8} Execution failed with message: '${result.message}'`);
4218
+ if (result.code === "captcha_rejected") {
4219
+ throw new CaptchaRejectedError(String(result.message ?? "").trim() || "CAPTCHA rejected by the site");
4220
+ }
4188
4221
  if (result.exception_detail) {
4189
4222
  throw new ActionExecutionError(result.exception_detail);
4190
4223
  }
@@ -4439,15 +4472,19 @@ var NotteVault = class {
4439
4472
  _vaultId = null;
4440
4473
  initPromise = null;
4441
4474
  createData = null;
4475
+ // Only a vault this object created is deleted by stop().
4476
+ created;
4442
4477
  constructor(client2, options = {}) {
4443
4478
  this.client = client2;
4444
4479
  if (options && "vault_id" in options && options.vault_id !== void 0) {
4480
+ this.created = false;
4445
4481
  if (options.vault_id.length === 0) {
4446
4482
  throw new InvalidRequestError("Vault ID cannot be empty");
4447
4483
  }
4448
4484
  this.initPromise = this.initExistingVault(options.vault_id);
4449
4485
  this.initPromise.catch(() => void 0);
4450
4486
  } else {
4487
+ this.created = true;
4451
4488
  const { vault_id: _ignored, ...createData } = options ?? {};
4452
4489
  this.createData = createData;
4453
4490
  }
@@ -4503,13 +4540,17 @@ var NotteVault = class {
4503
4540
  start() {
4504
4541
  }
4505
4542
  /**
4506
- * Stop the vault: deletes it together with every credential it holds.
4543
+ * Stop the vault: deletes it together with every credential it holds, if
4544
+ * this object created it. A vault opened by `vault_id` is kept.
4507
4545
  *
4508
4546
  * ```ts
4509
4547
  * await vault.stop();
4510
4548
  * ```
4511
4549
  */
4512
4550
  async stop() {
4551
+ if (!this.created) {
4552
+ return;
4553
+ }
4513
4554
  const vaultId = await this.ensureInitialized();
4514
4555
  console.info(`[Vault] ${vaultId} deleted. All credentials have been deleted.`);
4515
4556
  await this.delete();
@@ -5085,7 +5126,7 @@ var FUNCTION_RUN_ENDPOINTS = {
5085
5126
  start: "/functions/{function_id}/runs/{run_id}"
5086
5127
  };
5087
5128
  var RUN_API_KEY_HEADER = "x-notte-api-key";
5088
- var FUNCTION_RUN_STATUSES = ["closed", "active", "failed"];
5129
+ var FUNCTION_RUN_STATUSES = ["closed", "active", "failed", "rejected"];
5089
5130
  var NotteFunction = class {
5090
5131
  client;
5091
5132
  _functionId = null;
@@ -5404,13 +5445,15 @@ var NotteFunction = class {
5404
5445
  * to execute a record returned by `createRun()`; otherwise a new run record
5405
5446
  * is created first, exactly like Python. Pass `version` to run a saved
5406
5447
  * version instead of the latest one. A failed execution throws
5407
- * `FailedToRunCloudFunctionError` unless `raiseOnFailure` is false, and the
5408
- * wait is aborted with `NotteTimeoutError` after `timeoutMs`.
5448
+ * `FailedToRunCloudFunctionError` and a rejected one (the function raised an
5449
+ * `ExpectedFunctionError`) its subclass `FunctionRunRejectedError`, unless
5450
+ * `raiseOnFailure` is false; the wait is aborted with `NotteTimeoutError` after `timeoutMs`.
5409
5451
  *
5410
5452
  * @param variables - JSON-serializable keyword arguments passed to the Python function's `run` entry point.
5411
5453
  * @param options - Log streaming, failure handling, runtime, run ID, version, and timeout settings.
5412
5454
  * @returns The completed execution response. The function's return value is in `result`. Disabling streaming still waits for completion.
5413
5455
  * @throws FailedToRunCloudFunctionError if execution fails and `raiseOnFailure` is true.
5456
+ * @throws FunctionRunRejectedError if the run is rejected and `raiseOnFailure` is true.
5414
5457
  * @throws NotteTimeoutError if the execution request exceeds `timeoutMs`.
5415
5458
  * @throws InvalidRequestError if the run options are invalid.
5416
5459
  * @example
@@ -5463,6 +5506,9 @@ var NotteFunction = class {
5463
5506
  signal: controller.signal
5464
5507
  });
5465
5508
  const result = stream ? await readFunctionStream(raw, options.onLog ?? console.log, endpointPath, controller.signal) : parseFunctionRunResult(raw, endpointPath);
5509
+ if (result.status === "rejected" && (options.raiseOnFailure ?? true)) {
5510
+ throw new FunctionRunRejectedError(functionId, result.function_run_id, result);
5511
+ }
5466
5512
  if (result.status === "failed" && (options.raiseOnFailure ?? true)) {
5467
5513
  throw new FailedToRunCloudFunctionError(functionId, result.function_run_id, result);
5468
5514
  }
@@ -5615,12 +5661,15 @@ function parseFunctionRunResult(value, path) {
5615
5661
  if (typeof candidate.function_run_id !== "string" || typeof candidate.function_id !== "string" || typeof status !== "string" || !FUNCTION_RUN_STATUSES.includes(status) || !("result" in candidate)) {
5616
5662
  throw new NotteError("Invalid function result: missing function_id, function_run_id, status or result");
5617
5663
  }
5664
+ const error = candidate.error;
5618
5665
  return {
5619
5666
  function_id: candidate.function_id,
5620
5667
  function_run_id: candidate.function_run_id,
5621
5668
  session_id: typeof candidate.session_id === "string" ? candidate.session_id : null,
5622
5669
  result: candidate.result,
5623
- status
5670
+ status,
5671
+ // Kept as the API sent it; absent when the run reported no typed error.
5672
+ ...typeof error === "object" && error !== null && !Array.isArray(error) ? { error } : {}
5624
5673
  };
5625
5674
  }
5626
5675
  async function readFunctionStream(body, onLog, path, signal) {
@@ -6192,6 +6241,12 @@ var EXECUTION_ERROR_CLASS = "NotteApiExecutionError";
6192
6241
  function isRelativeProxyUrl(baseUrl) {
6193
6242
  return /^\/(?!\/)/.test(baseUrl);
6194
6243
  }
6244
+ function isFunctionsRequest(requestUrl, baseUrl) {
6245
+ const url = new URL(requestUrl);
6246
+ const api = new URL(baseUrl, url);
6247
+ const prefix = `${api.pathname.replace(/\/$/, "")}/functions`;
6248
+ return url.origin === api.origin && (url.pathname === prefix || url.pathname.startsWith(`${prefix}/`));
6249
+ }
6195
6250
  var NotteClient3 = class {
6196
6251
  config;
6197
6252
  client = createClient();
@@ -6250,6 +6305,9 @@ var NotteClient3 = class {
6250
6305
  headers.set("x-notte-request-origin", REQUEST_ORIGIN);
6251
6306
  headers.set("x-notte-sdk-version", SDK_VERSION);
6252
6307
  if (this.config.dbPreview) headers.set(DB_PREVIEW_HEADER, this.config.dbPreview);
6308
+ if (!headers.has(ACCEPT_RUN_STATUSES_HEADER) && isFunctionsRequest(request.url, this.config.baseUrl)) {
6309
+ headers.set(ACCEPT_RUN_STATUSES_HEADER, "rejected");
6310
+ }
6253
6311
  const override = headers.get(TIMEOUT_HEADER);
6254
6312
  headers.delete(TIMEOUT_HEADER);
6255
6313
  const retriesOverride = headers.get(MAX_RETRIES_HEADER);
@@ -6709,6 +6767,7 @@ var createClient2 = (config) => {
6709
6767
  CAPTCHA_SOLVE_TIMEOUT_MS,
6710
6768
  CLUSTER_OVERLOAD_RETRY_DELAY_MS,
6711
6769
  CREDENTIAL_FIELDS,
6770
+ CaptchaRejectedError,
6712
6771
  DEFAULT_MAX_RETRIES,
6713
6772
  DEFAULT_NOTTE_API_URL,
6714
6773
  DEFAULT_REQUEST_TIMEOUT_MS,
@@ -6720,6 +6779,7 @@ var createClient2 = (config) => {
6720
6779
  FetchResponseDecodeError,
6721
6780
  FileExistsError,
6722
6781
  FileNotFoundError,
6782
+ FunctionRunRejectedError,
6723
6783
  InvalidRequestError,
6724
6784
  ManagedAuthError,
6725
6785
  NotteAPIError,