@arbidocs/sdk 0.5.75 → 0.5.77

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
@@ -5181,6 +5181,39 @@ function buildChunkLookup(tools) {
5181
5181
  return lookup;
5182
5182
  }
5183
5183
 
5184
+ // src/operations/support.ts
5185
+ var support_exports = {};
5186
+ __export(support_exports, {
5187
+ createSupportTicket: () => createSupportTicket
5188
+ });
5189
+
5190
+ // src/operations/mail-encoding.ts
5191
+ function bytesToBase64(bytes) {
5192
+ let binary = "";
5193
+ for (let i2 = 0; i2 < bytes.length; i2 += 32768) {
5194
+ binary += String.fromCharCode(...bytes.subarray(i2, i2 + 32768));
5195
+ }
5196
+ return btoa(binary);
5197
+ }
5198
+
5199
+ // src/operations/support.ts
5200
+ async function createSupportTicket(arbi, input) {
5201
+ if (!input.description.trim()) throw new ArbiError("Describe what you need help with.");
5202
+ if (input.screenshot && (input.screenshot.type !== "image/png" || input.screenshot.size > 2 * 1024 * 1024))
5203
+ throw new ArbiError("The screenshot must be a PNG image no larger than 2 MB.");
5204
+ const screenshot = input.screenshot ? bytesToBase64(new Uint8Array(await input.screenshot.arrayBuffer())) : void 0;
5205
+ return requireData(
5206
+ await arbi.fetch.POST("/v1/support/tickets", {
5207
+ body: {
5208
+ description: input.description.trim(),
5209
+ idempotency_key: input.idempotencyKey ?? crypto.randomUUID(),
5210
+ ...screenshot ? { screenshot_png: screenshot } : {}
5211
+ }
5212
+ }),
5213
+ "Could not send your support request."
5214
+ );
5215
+ }
5216
+
5184
5217
  // src/diagnostics.ts
5185
5218
  async function diagnoseSetup(options) {
5186
5219
  const checks = [];
@@ -6735,15 +6768,6 @@ __export(secure_mail_exports, {
6735
6768
  prepareSecureMail: () => prepareSecureMail,
6736
6769
  secureMime: () => secureMime
6737
6770
  });
6738
-
6739
- // src/operations/mail-encoding.ts
6740
- function bytesToBase64(bytes) {
6741
- let binary = "";
6742
- for (let i2 = 0; i2 < bytes.length; i2 += 32768) {
6743
- binary += String.fromCharCode(...bytes.subarray(i2, i2 + 32768));
6744
- }
6745
- return btoa(binary);
6746
- }
6747
6771
  var MAX_MESSAGE_BYTES = 25 * 1024 * 1024;
6748
6772
  var utf8 = new TextEncoder();
6749
6773
  var encodedWord = (value) => `=?UTF-8?B?${bytesToBase64(utf8.encode(value))}?=`;
@@ -10526,8 +10550,10 @@ __export(mail_exports, {
10526
10550
  listFolders: () => listFolders,
10527
10551
  listMailAccounts: () => listMailAccounts,
10528
10552
  listMessages: () => listMessages,
10553
+ openDraftEditor: () => openDraftEditor,
10529
10554
  saveDraft: () => saveDraft,
10530
10555
  sendMessage: () => sendMessage,
10556
+ unlockMailCredentials: () => unlockMailCredentials,
10531
10557
  unlockNativeMail: () => unlockNativeMail,
10532
10558
  updateMessage: () => updateMessage
10533
10559
  });
@@ -10624,6 +10650,47 @@ async function unlockNativeMail(mail, keys) {
10624
10650
  secretKey.fill(0);
10625
10651
  }
10626
10652
  }
10653
+ async function unlockMailCredentials(mail, accountId, keys) {
10654
+ await client.initSodium();
10655
+ const secretKey = client.encryptionKeypairFromSigningKey(keys.signingPrivateKey).secretKey;
10656
+ const { sealed_credentials } = await json(
10657
+ mail(`${accountPath(accountId)}/sealed-credentials`)
10658
+ );
10659
+ const bytes = client.sealedBoxDecrypt(sealed_credentials, secretKey);
10660
+ try {
10661
+ const access = await client.sealKeyForSession(bytes, keys.serverSessionKey);
10662
+ await mail(`${accountPath(accountId)}/session-credentials`, {
10663
+ method: "POST",
10664
+ body: JSON.stringify({ access })
10665
+ });
10666
+ } finally {
10667
+ bytes.fill(0);
10668
+ secretKey.fill(0);
10669
+ }
10670
+ }
10671
+ async function openDraftEditor(mail, body, keys) {
10672
+ const open = (extra = {}) => json(
10673
+ mail("/drafts/editor", { method: "POST", body: JSON.stringify({ ...body, ...extra }) })
10674
+ );
10675
+ try {
10676
+ return await open();
10677
+ } catch (error) {
10678
+ const { status, apiError } = error;
10679
+ const locked = apiError?.detail;
10680
+ if (status !== 423 || !locked?.sealed_key || !locked.room_id) throw error;
10681
+ const { signingPrivateKey, serverSessionKey } = await keys();
10682
+ await client.initSodium();
10683
+ const secretKey = client.encryptionKeypairFromSigningKey(signingPrivateKey).secretKey;
10684
+ const key = client.sealedBoxDecrypt(locked.sealed_key, secretKey);
10685
+ try {
10686
+ const sealed = await client.sealKeyForSession(key, serverSessionKey);
10687
+ return await open({ room_id: locked.room_id, key: sealed });
10688
+ } finally {
10689
+ key.fill(0);
10690
+ secretKey.fill(0);
10691
+ }
10692
+ }
10693
+ }
10627
10694
  function attachmentFromBytes(name, contentType, bytes) {
10628
10695
  return {
10629
10696
  id: "",
@@ -12892,34 +12959,84 @@ var Arbi = class {
12892
12959
  * recovery. Pass `fetch` to the `mail` operations, e.g. `mail.listFolders(arbi.mail.fetch)`.
12893
12960
  */
12894
12961
  mail = {
12895
- /** Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`). */
12896
- fetch: (path5, init = {}) => this.recovering(async () => {
12897
- const { baseUrl, accessToken, fetch: send = globalThis.fetch } = this.getAuthHeaders();
12898
- const headers = new Headers(init.headers);
12899
- headers.set("Authorization", `Bearer ${accessToken}`);
12900
- if (init.body && !headers.has("Content-Type"))
12901
- headers.set("Content-Type", "application/json");
12902
- const response = await send(`${baseUrl}/v1/mail${path5}`, { ...init, headers });
12903
- if (!response.ok) {
12904
- const body = await response.json().catch(() => null);
12905
- throw new ArbiApiError(
12906
- typeof body?.detail === "string" ? body.detail : "The mail request could not be completed",
12907
- body,
12908
- response.status
12909
- );
12910
- }
12911
- return response;
12962
+ /**
12963
+ * Fetch a path under `/v1/mail`; rejects a non-2xx response with an `ArbiApiError` (`status`).
12964
+ * A read of the native inbox unlocks it first (`unlockNative`), so mail that arrived since is
12965
+ * readable too; `nativeUnlocked` skips that when the caller already unlocked it for this pass.
12966
+ * With no token yet (a new tab) the session is restored first; a 401 re-logs in and is sent
12967
+ * again once.
12968
+ */
12969
+ fetch: (path5, init = {}, { nativeUnlocked = false } = {}) => this.recovering(async () => {
12970
+ const nativeRead = path5.startsWith("/accounts/native/") && (init.method ?? "GET").toUpperCase() !== "POST";
12971
+ if (nativeRead && !nativeUnlocked) await this.mail.unlockNative();
12972
+ return this.mailRequest(path5, init);
12912
12973
  }),
12913
- /** Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`). */
12914
- unlockNative: () => {
12915
- const login = this.loginResult;
12916
- if (!login) return Promise.reject(new ArbiError("Sign in to unlock your inbox"));
12917
- return unlockNativeMail(this.mail.fetch, {
12918
- signingPrivateKey: login.signingPrivateKey,
12919
- serverSessionKey: login.serverSessionKey
12920
- });
12921
- }
12974
+ /**
12975
+ * Unlock the signed-in user's native inbox for this server session (see `unlockNativeMail`).
12976
+ * Concurrent calls share one unlock.
12977
+ */
12978
+ unlockNative: () => this.nativeUnlock ??= this.nativeMailKeys().then((keys) => unlockNativeMail(this.mail.fetch, keys)).finally(() => {
12979
+ this.nativeUnlock = null;
12980
+ }),
12981
+ /**
12982
+ * Hand this session what the server needs to read `account`: the native inbox's keys, a
12983
+ * Microsoft or Gmail account's connector tokens, an IMAP account's sealed credentials.
12984
+ */
12985
+ unlockAccount: async (account) => {
12986
+ if (account.provider === "native") return this.mail.unlockNative();
12987
+ if (account.provider === "microsoft" || account.provider === "gmail") {
12988
+ await this.connectors.bridgeTokens({ vendor: account.provider });
12989
+ return;
12990
+ }
12991
+ await unlockMailCredentials(this.mail.fetch, account.id, await this.nativeMailKeys());
12992
+ },
12993
+ /**
12994
+ * Open a native draft's body in the Word editor, unlocking its room for this session with the
12995
+ * user's key when the server asks (see `openDraftEditor`).
12996
+ */
12997
+ openDraftEditor: (body) => openDraftEditor(this.mail.fetch, body, () => this.nativeMailKeys())
12922
12998
  };
12999
+ /** The native inbox unlock in flight (`mail.unlockNative`). */
13000
+ nativeUnlock = null;
13001
+ /** One request under `/v1/mail` on the session's token (see `mail.fetch`). */
13002
+ async mailRequest(path5, init) {
13003
+ const client = this.requireClient();
13004
+ let token = client.session.getState().accessToken;
13005
+ if (!token) token = await client.auth.relogin();
13006
+ if (!token) throw new ArbiError("Sign in again to restore your session");
13007
+ const send = client.http?.fetch ?? globalThis.fetch;
13008
+ const request = (bearer) => {
13009
+ const headers = new Headers(init.headers);
13010
+ headers.set("Authorization", `Bearer ${bearer}`);
13011
+ if (init.body && !headers.has("Content-Type")) headers.set("Content-Type", "application/json");
13012
+ return send(`${this.options.url}/v1/mail${path5}`, { ...init, headers });
13013
+ };
13014
+ let response = await request(token);
13015
+ if (response.status === 401) {
13016
+ const refreshed = await client.auth.relogin();
13017
+ if (refreshed) response = await request(refreshed);
13018
+ }
13019
+ if (!response.ok) {
13020
+ const body = await response.json().catch(() => null);
13021
+ throw new ArbiApiError(
13022
+ typeof body?.detail === "string" ? body.detail : "The mail request could not be completed",
13023
+ body,
13024
+ response.status
13025
+ );
13026
+ }
13027
+ return response;
13028
+ }
13029
+ /**
13030
+ * The keys that unlock the user's mail for this server session: the session's key (current after
13031
+ * a re-login) and the identity key (the login's, or the one kept on this device).
13032
+ */
13033
+ async nativeMailKeys() {
13034
+ const client = this.requireClient();
13035
+ const serverSessionKey = client.session.getState().serverSessionKey ?? this.loginResult?.serverSessionKey ?? null;
13036
+ const signingPrivateKey = this.loginResult?.signingPrivateKey ?? (await client.storage.getSession().catch(() => null))?.signingPrivateKey;
13037
+ if (!serverSessionKey || !signingPrivateKey) throw new ArbiError("Sign in to unlock your inbox");
13038
+ return { signingPrivateKey, serverSessionKey };
13039
+ }
12923
13040
  // ── Secure mail ─────────────────────────────────────────────────────
12924
13041
  /**
12925
13042
  * Mail to anyone, sealed in this browser under a key that travels only in the link's fragment
@@ -13854,6 +13971,12 @@ var Arbi = class {
13854
13971
  getModels: () => getModels(this.requireClient())
13855
13972
  };
13856
13973
  // ── Health ────────────────────────────────────────────────────────────
13974
+ support = {
13975
+ create: (input) => {
13976
+ const request = { ...input, idempotencyKey: input.idempotencyKey ?? crypto.randomUUID() };
13977
+ return this.recovering(() => createSupportTicket(this.requireClient(), request));
13978
+ }
13979
+ };
13857
13980
  health = {
13858
13981
  check: () => getHealth(this.requireClient()),
13859
13982
  models: () => getHealthModels(this.requireClient()),
@@ -16037,6 +16160,7 @@ exports.streamSSE = streamSSE;
16037
16160
  exports.stripCitationMarkdown = stripCitationMarkdown;
16038
16161
  exports.subagentJobLabel = subagentJobLabel;
16039
16162
  exports.summarizeCitations = summarizeCitations;
16163
+ exports.support = support_exports;
16040
16164
  exports.tagUseCount = tagUseCount;
16041
16165
  exports.tags = tags_exports;
16042
16166
  exports.tagsForScope = tagsForScope;