notte-sdk 1.10.12 → 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.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.13" : "dev";
3
3
  var SDK_PACKAGE_NAME = true ? "notte-sdk" : "notte-sdk";
4
4
 
5
5
  // src/version-check.ts
@@ -1913,6 +1913,9 @@ var getSessionScript = (options) => (options.client ?? client).get({
1913
1913
  ...options
1914
1914
  });
1915
1915
 
1916
+ // src/run-statuses.ts
1917
+ var ACCEPT_RUN_STATUSES_HEADER = "x-notte-accept-run-statuses";
1918
+
1916
1919
  // src/managed-auth.ts
1917
1920
  import { setTimeout as delay } from "timers/promises";
1918
1921
 
@@ -1973,15 +1976,32 @@ var FailedToRunCloudFunctionError = class extends NotteError {
1973
1976
  functionId;
1974
1977
  functionRunId;
1975
1978
  response;
1976
- constructor(functionId, functionRunId, response) {
1979
+ constructor(functionId, functionRunId, response, message) {
1977
1980
  super(
1978
- `Function ${functionId} run ${functionRunId} failed: ${typeof response === "string" ? response : describeBody(response ?? {})}`
1981
+ message ?? `Function ${functionId} run ${functionRunId} failed: ${typeof response === "string" ? response : describeBody(response ?? {})}`
1979
1982
  );
1980
1983
  this.functionId = functionId;
1981
1984
  this.functionRunId = functionRunId;
1982
1985
  this.response = response;
1983
1986
  }
1984
1987
  };
1988
+ var FunctionRunRejectedError = class extends FailedToRunCloudFunctionError {
1989
+ error;
1990
+ code;
1991
+ constructor(functionId, functionRunId, response) {
1992
+ const error = response.error ?? null;
1993
+ const code = typeof error?.code === "string" && error.code ? error.code : null;
1994
+ const detail2 = typeof error?.message === "string" && error.message ? error.message : `result '${typeof response.result === "string" ? response.result : JSON.stringify(response.result)}'`;
1995
+ super(
1996
+ functionId,
1997
+ functionRunId,
1998
+ response,
1999
+ `Function ${functionId} with run_id=${functionRunId} was rejected${code ? ` (${code})` : ""}: ${detail2}`
2000
+ );
2001
+ this.error = error;
2002
+ this.code = code;
2003
+ }
2004
+ };
1985
2005
  var ScrapeFailedError = class extends NotteError {
1986
2006
  structuredError;
1987
2007
  constructor(error) {
@@ -2014,6 +2034,11 @@ var ActionExecutionError = class _ActionExecutionError extends NotteError {
2014
2034
  });
2015
2035
  }
2016
2036
  };
2037
+ var CaptchaRejectedError = class extends ActionExecutionError {
2038
+ constructor(message) {
2039
+ super({ error_type: "CaptchaRejectedError", dev_message: message, user_message: message, agent_message: message });
2040
+ }
2041
+ };
2017
2042
  var EvaluateJsNoDataError = class extends NotteError {
2018
2043
  constructor() {
2019
2044
  super("evaluate_js returned no data. Upgrade the Notte API to a build that returns the evaluated value.");
@@ -2657,6 +2682,9 @@ async function executeWithCaptcha(action, budgetSeconds, request) {
2657
2682
  await pause();
2658
2683
  continue;
2659
2684
  }
2685
+ if (status.state === "failed" && status.failure_reason === "rejected") {
2686
+ return failure(status.message || "CAPTCHA rejected by the site", "captcha_rejected");
2687
+ }
2660
2688
  if (status.state === "failed" || status.state === "cancelled") {
2661
2689
  return failure(status.message || `CAPTCHA ${status.state}`, `captcha_${status.state}`);
2662
2690
  }
@@ -3937,6 +3965,9 @@ var Session = class {
3937
3965
  );
3938
3966
  if (raiseOnFailure && !result.success) {
3939
3967
  console.error(`\u{1F6A8} Execution failed with message: '${result.message}'`);
3968
+ if (result.code === "captcha_rejected") {
3969
+ throw new CaptchaRejectedError(String(result.message ?? "").trim() || "CAPTCHA rejected by the site");
3970
+ }
3940
3971
  if (result.exception_detail) {
3941
3972
  throw new ActionExecutionError(result.exception_detail);
3942
3973
  }
@@ -4191,15 +4222,19 @@ var NotteVault = class {
4191
4222
  _vaultId = null;
4192
4223
  initPromise = null;
4193
4224
  createData = null;
4225
+ // Only a vault this object created is deleted by stop().
4226
+ created;
4194
4227
  constructor(client2, options = {}) {
4195
4228
  this.client = client2;
4196
4229
  if (options && "vault_id" in options && options.vault_id !== void 0) {
4230
+ this.created = false;
4197
4231
  if (options.vault_id.length === 0) {
4198
4232
  throw new InvalidRequestError("Vault ID cannot be empty");
4199
4233
  }
4200
4234
  this.initPromise = this.initExistingVault(options.vault_id);
4201
4235
  this.initPromise.catch(() => void 0);
4202
4236
  } else {
4237
+ this.created = true;
4203
4238
  const { vault_id: _ignored, ...createData } = options ?? {};
4204
4239
  this.createData = createData;
4205
4240
  }
@@ -4255,13 +4290,17 @@ var NotteVault = class {
4255
4290
  start() {
4256
4291
  }
4257
4292
  /**
4258
- * Stop the vault: deletes it together with every credential it holds.
4293
+ * Stop the vault: deletes it together with every credential it holds, if
4294
+ * this object created it. A vault opened by `vault_id` is kept.
4259
4295
  *
4260
4296
  * ```ts
4261
4297
  * await vault.stop();
4262
4298
  * ```
4263
4299
  */
4264
4300
  async stop() {
4301
+ if (!this.created) {
4302
+ return;
4303
+ }
4265
4304
  const vaultId = await this.ensureInitialized();
4266
4305
  console.info(`[Vault] ${vaultId} deleted. All credentials have been deleted.`);
4267
4306
  await this.delete();
@@ -4837,7 +4876,7 @@ var FUNCTION_RUN_ENDPOINTS = {
4837
4876
  start: "/functions/{function_id}/runs/{run_id}"
4838
4877
  };
4839
4878
  var RUN_API_KEY_HEADER = "x-notte-api-key";
4840
- var FUNCTION_RUN_STATUSES = ["closed", "active", "failed"];
4879
+ var FUNCTION_RUN_STATUSES = ["closed", "active", "failed", "rejected"];
4841
4880
  var NotteFunction = class {
4842
4881
  client;
4843
4882
  _functionId = null;
@@ -5156,13 +5195,15 @@ var NotteFunction = class {
5156
5195
  * to execute a record returned by `createRun()`; otherwise a new run record
5157
5196
  * is created first, exactly like Python. Pass `version` to run a saved
5158
5197
  * 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`.
5198
+ * `FailedToRunCloudFunctionError` and a rejected one (the function raised an
5199
+ * `ExpectedFunctionError`) its subclass `FunctionRunRejectedError`, unless
5200
+ * `raiseOnFailure` is false; the wait is aborted with `NotteTimeoutError` after `timeoutMs`.
5161
5201
  *
5162
5202
  * @param variables - JSON-serializable keyword arguments passed to the Python function's `run` entry point.
5163
5203
  * @param options - Log streaming, failure handling, runtime, run ID, version, and timeout settings.
5164
5204
  * @returns The completed execution response. The function's return value is in `result`. Disabling streaming still waits for completion.
5165
5205
  * @throws FailedToRunCloudFunctionError if execution fails and `raiseOnFailure` is true.
5206
+ * @throws FunctionRunRejectedError if the run is rejected and `raiseOnFailure` is true.
5166
5207
  * @throws NotteTimeoutError if the execution request exceeds `timeoutMs`.
5167
5208
  * @throws InvalidRequestError if the run options are invalid.
5168
5209
  * @example
@@ -5215,6 +5256,9 @@ var NotteFunction = class {
5215
5256
  signal: controller.signal
5216
5257
  });
5217
5258
  const result = stream ? await readFunctionStream(raw, options.onLog ?? console.log, endpointPath, controller.signal) : parseFunctionRunResult(raw, endpointPath);
5259
+ if (result.status === "rejected" && (options.raiseOnFailure ?? true)) {
5260
+ throw new FunctionRunRejectedError(functionId, result.function_run_id, result);
5261
+ }
5218
5262
  if (result.status === "failed" && (options.raiseOnFailure ?? true)) {
5219
5263
  throw new FailedToRunCloudFunctionError(functionId, result.function_run_id, result);
5220
5264
  }
@@ -5367,12 +5411,15 @@ function parseFunctionRunResult(value, path) {
5367
5411
  if (typeof candidate.function_run_id !== "string" || typeof candidate.function_id !== "string" || typeof status !== "string" || !FUNCTION_RUN_STATUSES.includes(status) || !("result" in candidate)) {
5368
5412
  throw new NotteError("Invalid function result: missing function_id, function_run_id, status or result");
5369
5413
  }
5414
+ const error = candidate.error;
5370
5415
  return {
5371
5416
  function_id: candidate.function_id,
5372
5417
  function_run_id: candidate.function_run_id,
5373
5418
  session_id: typeof candidate.session_id === "string" ? candidate.session_id : null,
5374
5419
  result: candidate.result,
5375
- status
5420
+ status,
5421
+ // Kept as the API sent it; absent when the run reported no typed error.
5422
+ ...typeof error === "object" && error !== null && !Array.isArray(error) ? { error } : {}
5376
5423
  };
5377
5424
  }
5378
5425
  async function readFunctionStream(body, onLog, path, signal) {
@@ -5944,6 +5991,12 @@ var EXECUTION_ERROR_CLASS = "NotteApiExecutionError";
5944
5991
  function isRelativeProxyUrl(baseUrl) {
5945
5992
  return /^\/(?!\/)/.test(baseUrl);
5946
5993
  }
5994
+ function isFunctionsRequest(requestUrl, baseUrl) {
5995
+ const url = new URL(requestUrl);
5996
+ const api = new URL(baseUrl, url);
5997
+ const prefix = `${api.pathname.replace(/\/$/, "")}/functions`;
5998
+ return url.origin === api.origin && (url.pathname === prefix || url.pathname.startsWith(`${prefix}/`));
5999
+ }
5947
6000
  var NotteClient3 = class {
5948
6001
  config;
5949
6002
  client = createClient();
@@ -6002,6 +6055,9 @@ var NotteClient3 = class {
6002
6055
  headers.set("x-notte-request-origin", REQUEST_ORIGIN);
6003
6056
  headers.set("x-notte-sdk-version", SDK_VERSION);
6004
6057
  if (this.config.dbPreview) headers.set(DB_PREVIEW_HEADER, this.config.dbPreview);
6058
+ if (!headers.has(ACCEPT_RUN_STATUSES_HEADER) && isFunctionsRequest(request.url, this.config.baseUrl)) {
6059
+ headers.set(ACCEPT_RUN_STATUSES_HEADER, "rejected");
6060
+ }
6005
6061
  const override = headers.get(TIMEOUT_HEADER);
6006
6062
  headers.delete(TIMEOUT_HEADER);
6007
6063
  const retriesOverride = headers.get(MAX_RETRIES_HEADER);
@@ -6460,6 +6516,7 @@ export {
6460
6516
  CAPTCHA_SOLVE_TIMEOUT_MS,
6461
6517
  CLUSTER_OVERLOAD_RETRY_DELAY_MS,
6462
6518
  CREDENTIAL_FIELDS,
6519
+ CaptchaRejectedError,
6463
6520
  DEFAULT_MAX_RETRIES,
6464
6521
  DEFAULT_NOTTE_API_URL,
6465
6522
  DEFAULT_REQUEST_TIMEOUT_MS,
@@ -6471,6 +6528,7 @@ export {
6471
6528
  FetchResponseDecodeError,
6472
6529
  FileExistsError,
6473
6530
  FileNotFoundError,
6531
+ FunctionRunRejectedError,
6474
6532
  InvalidRequestError,
6475
6533
  ManagedAuthError,
6476
6534
  NotteAPIError,