@arbidocs/sdk 0.4.94 → 0.4.96

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
@@ -4088,6 +4088,12 @@ async function getRawWorkspaceKey(arbi, workspaceId, signingPrivateKeyBase64) {
4088
4088
  });
4089
4089
  return client.sealedBoxDecrypt(ws.wrapped_key, encryptionKeyPair.secretKey);
4090
4090
  }
4091
+ var StaleSessionError = class extends ArbiError {
4092
+ constructor(message) {
4093
+ super(message);
4094
+ this.name = "StaleSessionError";
4095
+ }
4096
+ };
4091
4097
  async function openWorkspaceEntry(arbi, ws, serverSessionKey, signingPrivateKeyBase64) {
4092
4098
  if (ws.wrapped_key) {
4093
4099
  const sealedKey = await selectWorkspace(
@@ -4097,12 +4103,13 @@ async function openWorkspaceEntry(arbi, ws, serverSessionKey, signingPrivateKeyB
4097
4103
  serverSessionKey,
4098
4104
  signingPrivateKeyBase64
4099
4105
  );
4100
- const { error: error2 } = await arbi.fetch.POST("/v1/workspace/{workspace_ext_id}/open", {
4106
+ const { error: error2, response } = await arbi.fetch.POST("/v1/workspace/{workspace_ext_id}/open", {
4101
4107
  params: { path: { workspace_ext_id: ws.external_id } },
4102
4108
  body: { workspace_key: sealedKey }
4103
4109
  });
4104
4110
  if (error2) {
4105
- throw new ArbiError(`Workspace open failed for ${ws.external_id}: ${JSON.stringify(error2)}`);
4111
+ const message = `Workspace open failed for ${ws.external_id}: ${JSON.stringify(error2)}`;
4112
+ throw response.status === 401 ? new StaleSessionError(message) : new ArbiError(message);
4106
4113
  }
4107
4114
  return sealedKey;
4108
4115
  }
@@ -4161,6 +4168,28 @@ async function resolveAuth(store) {
4161
4168
  const { arbi, loginResult } = await createAuthenticatedClient(config, creds, store);
4162
4169
  return { arbi, loginResult, config };
4163
4170
  }
4171
+ async function openOnCachedSession(store, config, creds, workspaceId) {
4172
+ const { arbi, loginResult } = await buildAuthFromCache(config, creds, store);
4173
+ arbi.session.setSelectedWorkspace(workspaceId);
4174
+ await selectWorkspaceById(
4175
+ arbi,
4176
+ workspaceId,
4177
+ loginResult.serverSessionKey,
4178
+ creds.signingPrivateKeyBase64
4179
+ );
4180
+ const nextOpened = Array.from(/* @__PURE__ */ new Set([...creds.openedWorkspaces ?? [], workspaceId]));
4181
+ store.saveCredentials({
4182
+ ...store.requireCredentials(),
4183
+ openedWorkspaces: nextOpened
4184
+ });
4185
+ return {
4186
+ arbi,
4187
+ loginResult,
4188
+ config,
4189
+ workspaceId,
4190
+ accessToken: creds.accessToken
4191
+ };
4192
+ }
4164
4193
  async function resolveWorkspace(store, workspaceOpt) {
4165
4194
  const config = store.requireConfig();
4166
4195
  const creds = store.requireCredentials();
@@ -4169,26 +4198,11 @@ async function resolveWorkspace(store, workspaceOpt) {
4169
4198
  throw new ArbiError("No workspace selected. Run: arbi workspace select <id>");
4170
4199
  }
4171
4200
  if (isSessionAlive(creds)) {
4172
- const { arbi: arbi2, loginResult: loginResult2 } = await buildAuthFromCache(config, creds, store);
4173
- arbi2.session.setSelectedWorkspace(workspaceId);
4174
- await selectWorkspaceById(
4175
- arbi2,
4176
- workspaceId,
4177
- loginResult2.serverSessionKey,
4178
- creds.signingPrivateKeyBase64
4179
- );
4180
- const nextOpened = Array.from(/* @__PURE__ */ new Set([...creds.openedWorkspaces ?? [], workspaceId]));
4181
- store.saveCredentials({
4182
- ...store.requireCredentials(),
4183
- openedWorkspaces: nextOpened
4184
- });
4185
- return {
4186
- arbi: arbi2,
4187
- loginResult: loginResult2,
4188
- config,
4189
- workspaceId,
4190
- accessToken: creds.accessToken
4191
- };
4201
+ try {
4202
+ return await openOnCachedSession(store, config, creds, workspaceId);
4203
+ } catch (err) {
4204
+ if (!(err instanceof StaleSessionError)) throw err;
4205
+ }
4192
4206
  }
4193
4207
  const { arbi, loginResult } = await createAuthenticatedClient(config, creds, store);
4194
4208
  await selectWorkspaceById(
@@ -6589,8 +6603,12 @@ function currentSessionId(accessToken) {
6589
6603
  // src/operations/profile.ts
6590
6604
  var profile_exports = {};
6591
6605
  __export(profile_exports, {
6606
+ getCurrentUser: () => getCurrentUser,
6592
6607
  updateProfile: () => updateProfile
6593
6608
  });
6609
+ async function getCurrentUser(arbi) {
6610
+ return requireData(await arbi.fetch.GET("/v1/user/me"), "Failed to fetch the current user");
6611
+ }
6594
6612
  async function updateProfile(arbi, body) {
6595
6613
  return requireData(
6596
6614
  await arbi.fetch.PATCH("/v1/user/profile", { body }),
@@ -8171,7 +8189,12 @@ var Arbi = class {
8171
8189
  return () => this.profileListeners.delete(listener);
8172
8190
  },
8173
8191
  /** Today's deployment-wide credit spend for the current user. */
8174
- usageToday: () => getUserUsageToday(this.requireClient())
8192
+ usageToday: () => getUserUsageToday(this.requireClient()),
8193
+ /**
8194
+ * The signed-in user from the server, with `can_create_projects`: false on deployments where
8195
+ * only firm members may create projects and workspaces and this user is not one (yet).
8196
+ */
8197
+ me: () => getCurrentUser(this.requireClient())
8175
8198
  };
8176
8199
  // ── Profile ───────────────────────────────────────────────────────────
8177
8200
  profile = {