@arbidocs/sdk 0.4.169 → 0.5.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.cjs CHANGED
@@ -3502,6 +3502,24 @@ var ArbiSessionExpiredError = class extends ArbiError {
3502
3502
  this.name = "ArbiSessionExpiredError";
3503
3503
  }
3504
3504
  };
3505
+ var ArbiWorkspaceSupersededError = class extends ArbiError {
3506
+ constructor(workspaceId, supersededBy) {
3507
+ super(`Opening ${workspaceId} was superseded by opening ${supersededBy}`);
3508
+ this.workspaceId = workspaceId;
3509
+ this.supersededBy = supersededBy;
3510
+ this.name = "ArbiWorkspaceSupersededError";
3511
+ }
3512
+ workspaceId;
3513
+ supersededBy;
3514
+ };
3515
+ var ArbiWorkspaceChangedError = class extends ArbiError {
3516
+ constructor(workspaceId) {
3517
+ super(`The workspace changed while reading ${workspaceId}`);
3518
+ this.workspaceId = workspaceId;
3519
+ this.name = "ArbiWorkspaceChangedError";
3520
+ }
3521
+ workspaceId;
3522
+ };
3505
3523
  function detailText(detail) {
3506
3524
  if (typeof detail === "string") return detail || void 0;
3507
3525
  if (Array.isArray(detail)) {
@@ -5057,7 +5075,8 @@ async function* listPaginated(arbi, options = {}) {
5057
5075
  order = "id_asc",
5058
5076
  fields,
5059
5077
  signal,
5060
- lookahead = 1
5078
+ lookahead = 1,
5079
+ workspaceExtId
5061
5080
  } = options;
5062
5081
  const fetchPage = async (pageOffset, limit) => {
5063
5082
  const query2 = {
@@ -5066,6 +5085,7 @@ async function* listPaginated(arbi, options = {}) {
5066
5085
  order
5067
5086
  };
5068
5087
  if (fields) query2.fields = fields;
5088
+ if (workspaceExtId) query2.workspace_ext_id = workspaceExtId;
5069
5089
  const { data, error } = await arbi.fetch.GET("/v1/document/list", {
5070
5090
  params: { query: query2 },
5071
5091
  signal
@@ -5073,7 +5093,11 @@ async function* listPaginated(arbi, options = {}) {
5073
5093
  if (error) {
5074
5094
  throw new Error(typeof error === "string" ? error : JSON.stringify(error));
5075
5095
  }
5076
- return data ?? [];
5096
+ const page = data ?? [];
5097
+ if (workspaceExtId && page.some((doc) => doc.workspace_ext_id !== workspaceExtId)) {
5098
+ throw new ArbiError(`The server returned another workspace's documents for ${workspaceExtId}`);
5099
+ }
5100
+ return page;
5077
5101
  };
5078
5102
  if (signal?.aborted) return;
5079
5103
  const depth = Math.max(1, Math.min(lookahead, MAX_PAGES));
@@ -10398,12 +10422,21 @@ function tokenExpiresWithin(token, seconds) {
10398
10422
  const expiresAt = client.readSessionClaims(token)?.expiresAt;
10399
10423
  return expiresAt != null && expiresAt - Date.now() < seconds * 1e3;
10400
10424
  }
10425
+ var WorkspaceOpenIntent = class {
10426
+ constructor(workspaceId) {
10427
+ this.workspaceId = workspaceId;
10428
+ }
10429
+ workspaceId;
10430
+ result = null;
10431
+ };
10401
10432
  var Arbi = class {
10402
10433
  client = null;
10403
10434
  loginResult = null;
10404
10435
  userProfile = null;
10405
10436
  profileListeners = /* @__PURE__ */ new Set();
10406
10437
  currentWorkspaceId = null;
10438
+ /** The latest `selectWorkspace` asked for: only it may open when its turn comes. */
10439
+ openIntent = null;
10407
10440
  chatCryptoContext = null;
10408
10441
  /** The chat keys I hold, opened once per set of epochs (`chatRingSignature`). */
10409
10442
  chatRings = /* @__PURE__ */ new Map();
@@ -10639,6 +10672,38 @@ var Arbi = class {
10639
10672
  * documents, which are listed page by page). A public workspace has no key and opens without one.
10640
10673
  */
10641
10674
  async selectWorkspace(workspaceId) {
10675
+ const client = this.requireClient();
10676
+ this.requireLogin();
10677
+ const intent = new WorkspaceOpenIntent(workspaceId);
10678
+ this.openIntent = intent;
10679
+ client.session.setSelectedWorkspace(workspaceId);
10680
+ const result = client.workspaceOpens.run(
10681
+ () => workspaceId,
10682
+ async () => {
10683
+ const latest = this.openIntent;
10684
+ if (latest && latest !== intent) return latest;
10685
+ const opened = await this.openOnServer(workspaceId);
10686
+ this.currentWorkspaceId = workspaceId;
10687
+ return opened;
10688
+ }
10689
+ ).then(
10690
+ (outcome) => {
10691
+ if (!(outcome instanceof WorkspaceOpenIntent)) return outcome;
10692
+ if (outcome.workspaceId === workspaceId && outcome.result) return outcome.result;
10693
+ throw new ArbiWorkspaceSupersededError(workspaceId, outcome.workspaceId);
10694
+ },
10695
+ (error) => {
10696
+ if (this.openIntent === intent) {
10697
+ client.session.setSelectedWorkspace(this.currentWorkspaceId);
10698
+ }
10699
+ throw error;
10700
+ }
10701
+ );
10702
+ intent.result = result;
10703
+ return result;
10704
+ }
10705
+ /** `POST /open` with the workspace's key sealed to this session (none for a public workspace). */
10706
+ async openOnServer(workspaceId) {
10642
10707
  const client = this.requireClient();
10643
10708
  const lr2 = this.requireLogin();
10644
10709
  const open = async (workspaceKey) => requireData(
@@ -10651,21 +10716,40 @@ var Arbi = class {
10651
10716
  const listed = await client.workspaceKeys.wrappedKey(workspaceId).catch((error) => {
10652
10717
  throw new ArbiError(error instanceof Error ? error.message : String(error));
10653
10718
  });
10654
- const opened = !listed.wrappedKey ? await open("") : await this.withWrappedKey(
10719
+ if (!listed.wrappedKey) return open("");
10720
+ return this.withWrappedKey(
10655
10721
  workspaceId,
10656
10722
  async (wrappedKey) => open(
10657
- await selectWorkspace(
10723
+ await generateEncryptedWorkspaceKey(
10658
10724
  client,
10659
- workspaceId,
10660
10725
  wrappedKey,
10661
10726
  lr2.serverSessionKey,
10662
10727
  client.crypto.bytesToBase64(lr2.signingPrivateKey)
10663
10728
  )
10664
10729
  )
10665
10730
  );
10666
- client.session.setSelectedWorkspace(workspaceId);
10667
- this.currentWorkspaceId = workspaceId;
10668
- return opened;
10731
+ }
10732
+ /** Resolves once no workspace open is running or waiting (a switch has landed on the server). */
10733
+ workspaceSettled() {
10734
+ return this.client ? this.client.workspaceOpens.idle() : Promise.resolve();
10735
+ }
10736
+ /**
10737
+ * Run `read` — a request answered from the session's open workspace — for `workspaceId`, and
10738
+ * keep its answer only if it can only be that workspace's: once no open is pending, the open
10739
+ * workspace must be `workspaceId` when it is sent and still be when it returns, with no open
10740
+ * started in between. Otherwise it throws `ArbiWorkspaceChangedError` and nothing it returned
10741
+ * is kept, so a list read across a switch can never be cached as another workspace's.
10742
+ */
10743
+ async readInWorkspace(workspaceId, read) {
10744
+ const opens = this.requireClient().workspaceOpens;
10745
+ await opens.idle();
10746
+ const generation = opens.generation;
10747
+ if (this.currentWorkspaceId !== workspaceId) throw new ArbiWorkspaceChangedError(workspaceId);
10748
+ const result = await read();
10749
+ if (opens.generation !== generation || this.currentWorkspaceId !== workspaceId) {
10750
+ throw new ArbiWorkspaceChangedError(workspaceId);
10751
+ }
10752
+ return result;
10669
10753
  }
10670
10754
  /**
10671
10755
  * Subscribe to "the session cannot be recovered". Fires when a request failed with 401 or
@@ -10687,6 +10771,7 @@ var Arbi = class {
10687
10771
  this.loginResult = null;
10688
10772
  this.userProfile = null;
10689
10773
  this.currentWorkspaceId = null;
10774
+ this.openIntent = null;
10690
10775
  this.chatCryptoContext = null;
10691
10776
  this.chatRings.clear();
10692
10777
  for (const listener of [...this.sessionExpiredListeners]) listener();
@@ -10734,6 +10819,7 @@ var Arbi = class {
10734
10819
  this.loginResult = null;
10735
10820
  this.userProfile = null;
10736
10821
  this.currentWorkspaceId = null;
10822
+ this.openIntent = null;
10737
10823
  this.chatCryptoContext = null;
10738
10824
  this.chatRings.clear();
10739
10825
  }
@@ -11128,7 +11214,16 @@ var Arbi = class {
11128
11214
  * }
11129
11215
  * ```
11130
11216
  */
11131
- listPaginated: (options) => listPaginated(this.requireClient(), options),
11217
+ listPaginated: (options) => {
11218
+ const { workspaceId, ...rest } = options ?? {};
11219
+ if (!workspaceId) return listPaginated(this.requireClient(), rest);
11220
+ const client = this.requireClient();
11221
+ const open = () => this.openWorkspace(workspaceId);
11222
+ return (async function* () {
11223
+ await open();
11224
+ yield* listPaginated(client, { ...rest, workspaceExtId: workspaceId });
11225
+ })();
11226
+ },
11132
11227
  /**
11133
11228
  * Collect all workspace documents into a single array using sequential pagination.
11134
11229
  *
@@ -13899,6 +13994,8 @@ exports.Arbi = Arbi;
13899
13994
  exports.ArbiApiError = ArbiApiError;
13900
13995
  exports.ArbiError = ArbiError;
13901
13996
  exports.ArbiSessionExpiredError = ArbiSessionExpiredError;
13997
+ exports.ArbiWorkspaceChangedError = ArbiWorkspaceChangedError;
13998
+ exports.ArbiWorkspaceSupersededError = ArbiWorkspaceSupersededError;
13902
13999
  exports.CallApiError = CallApiError;
13903
14000
  exports.ClaudeOrchestrator = ClaudeOrchestrator;
13904
14001
  exports.DEFAULT_AI_USD_PER_GBP = DEFAULT_AI_USD_PER_GBP;