@haex-space/vault-sdk 4.7.0 → 4.8.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.
package/dist/index.mjs CHANGED
@@ -2483,22 +2483,45 @@ var ModelsAPI = class {
2483
2483
  * `cancelled` with the error. Errors of the start itself (an invalid job, a refused model or
2484
2484
  * consent, no fitting model) reject. The end can arrive before the start answers; it is kept
2485
2485
  * until then.
2486
+ *
2487
+ * Aborting `signal` asks to cancel the job, which then ends as `cancelled`; a job that had
2488
+ * already ended keeps its actual end. A signal that is already aborted rejects with its reason
2489
+ * and starts no job. If the cancel request fails, the run rejects with that error and the job
2490
+ * keeps running on the host; if it reports an unknown job, the run rejects with an error.
2486
2491
  */
2487
- async runAsync(job) {
2492
+ async runAsync(job, options = {}) {
2493
+ const { signal } = options;
2494
+ if (signal?.aborted) throw signal.reason;
2488
2495
  const ended = /* @__PURE__ */ new Map();
2489
2496
  let waiting = null;
2490
2497
  const unsubscribe = this.onCompleted((completion) => {
2491
2498
  if (waiting && completion.jobId === waiting.jobId) waiting.resolve(completion);
2492
2499
  else ended.set(completion.jobId, completion);
2493
2500
  });
2501
+ let detach = () => {
2502
+ };
2494
2503
  try {
2495
2504
  const { jobId } = await this.startAsync(job);
2496
- return await new Promise((resolve) => {
2505
+ return await new Promise((resolve, reject) => {
2497
2506
  const early = ended.get(jobId);
2498
- if (early) resolve(early);
2499
- else waiting = { jobId, resolve };
2507
+ if (early) return resolve(early);
2508
+ waiting = { jobId, resolve };
2509
+ if (!signal) return;
2510
+ const cancel = async () => {
2511
+ try {
2512
+ if (!await this.cancelAsync(jobId)) {
2513
+ reject(new Error("The model job could not be cancelled"));
2514
+ }
2515
+ } catch (error) {
2516
+ reject(error);
2517
+ }
2518
+ };
2519
+ if (signal.aborted) return cancel();
2520
+ signal.addEventListener("abort", cancel, { once: true });
2521
+ detach = () => signal.removeEventListener("abort", cancel);
2500
2522
  });
2501
2523
  } finally {
2524
+ detach();
2502
2525
  unsubscribe();
2503
2526
  }
2504
2527
  }
@@ -3531,6 +3554,11 @@ var PermissionWaiterRegistry = class {
3531
3554
  for (const callback of [...set]) callback(decision);
3532
3555
  }
3533
3556
  };
3557
+ function promptedKey(error) {
3558
+ const details = error.details ?? {};
3559
+ const field = (name) => typeof details[name] === "string" ? details[name] : error[name];
3560
+ return permissionKey(field("resourceType"), field("action"), field("target"));
3561
+ }
3534
3562
  function toDeniedError(error) {
3535
3563
  return { ...error, code: 1002 /* DENIED */ };
3536
3564
  }
@@ -3543,7 +3571,7 @@ async function withPermissionRetry(send, registry, log, timeoutMs = PERMISSION_D
3543
3571
  if (!isPermissionPromptError(error) || attempt >= MAX_PERMISSION_PROMPTS) {
3544
3572
  throw error;
3545
3573
  }
3546
- const key = permissionKey(error.resourceType, error.action, error.target);
3574
+ const key = promptedKey(error);
3547
3575
  const times = asked.get(key) ?? 0;
3548
3576
  if (times >= MAX_PERMISSION_RETRIES) throw error;
3549
3577
  asked.set(key, times + 1);