@arbidocs/sdk 0.4.48 → 0.4.50

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
@@ -5795,19 +5795,29 @@ async function generateDocTags(arbi, tagIds, docIds, options) {
5795
5795
  // src/operations/dm.ts
5796
5796
  var dm_exports = {};
5797
5797
  __export(dm_exports, {
5798
+ DM_ATTACHMENT_MAX_BYTES: () => DM_ATTACHMENT_MAX_BYTES,
5799
+ DM_ATTACHMENT_MAX_FILES: () => DM_ATTACHMENT_MAX_FILES,
5800
+ checkDmAttachmentLimits: () => checkDmAttachmentLimits,
5798
5801
  createDmCryptoContext: () => createDmCryptoContext,
5802
+ decryptDmAttachments: () => decryptDmAttachments,
5799
5803
  decryptDmBatch: () => decryptDmBatch,
5800
5804
  decryptDmContent: () => decryptDmContent,
5801
5805
  deleteDMs: () => deleteDMs,
5806
+ downloadDmAttachment: () => downloadDmAttachment,
5807
+ encryptDmAttachmentManifest: () => encryptDmAttachmentManifest,
5802
5808
  encryptDmContent: () => encryptDmContent,
5803
5809
  listActivity: () => listActivity,
5804
5810
  listDMs: () => listDMs,
5805
5811
  listDecryptedDMs: () => listDecryptedDMs,
5806
5812
  markRead: () => markRead,
5813
+ parseDmAttachmentManifest: () => parseDmAttachmentManifest,
5807
5814
  requestWorkspaceAccess: () => requestWorkspaceAccess,
5808
5815
  resolveRecipient: () => resolveRecipient,
5809
5816
  sendDM: () => sendDM,
5810
- sendEncryptedDM: () => sendEncryptedDM
5817
+ sendEncryptedDM: () => sendEncryptedDM,
5818
+ serializeDmAttachmentManifest: () => serializeDmAttachmentManifest,
5819
+ uploadDmAttachment: () => uploadDmAttachment,
5820
+ uploadDmAttachments: () => uploadDmAttachments
5811
5821
  });
5812
5822
 
5813
5823
  // src/operations/agents.ts
@@ -5914,6 +5924,138 @@ async function stageAgentFile(auth, agentId, file, name) {
5914
5924
  });
5915
5925
  return response.json();
5916
5926
  }
5927
+ var DM_ATTACHMENT_MAX_BYTES = 15 * 1024 * 1024;
5928
+ var DM_ATTACHMENT_MAX_FILES = 10;
5929
+ var ATTACHMENT_ID = /^att-[a-zA-Z0-9]{8}$/;
5930
+ var MANIFEST_VERSION = 1;
5931
+ var MB = 1024 * 1024;
5932
+ function checkDmAttachmentLimits(files) {
5933
+ if (files.length > DM_ATTACHMENT_MAX_FILES) {
5934
+ throw new Error(
5935
+ `You can attach at most ${DM_ATTACHMENT_MAX_FILES} files to a message (this one has ${files.length}).`
5936
+ );
5937
+ }
5938
+ for (const file of files) {
5939
+ if (file.size > DM_ATTACHMENT_MAX_BYTES) {
5940
+ throw new Error(
5941
+ `${file.name} is too large (${(file.size / MB).toFixed(1)} MB). Files can be at most ${DM_ATTACHMENT_MAX_BYTES / MB} MB.`
5942
+ );
5943
+ }
5944
+ }
5945
+ }
5946
+ async function uploadDmAttachment(arbi, input) {
5947
+ checkDmAttachmentLimits([{ name: input.name, size: input.bytes.length }]);
5948
+ await client.initSodium();
5949
+ const key = globalThis.crypto.getRandomValues(new Uint8Array(32));
5950
+ try {
5951
+ const ciphertext = client.encryptFile(input.bytes, key);
5952
+ const form = new FormData();
5953
+ form.append(
5954
+ "file",
5955
+ new Blob([ciphertext], { type: "application/octet-stream" }),
5956
+ "attachment.bin"
5957
+ );
5958
+ const result = await arbi.fetch.POST("/v1/notifications/attachments", {
5959
+ body: { file: "" },
5960
+ bodySerializer: () => form
5961
+ });
5962
+ if (result.response?.status === 413) {
5963
+ throw new ArbiApiError(
5964
+ `${input.name} is too large. Files can be at most ${DM_ATTACHMENT_MAX_BYTES / MB} MB.`,
5965
+ result.error,
5966
+ 413
5967
+ );
5968
+ }
5969
+ const uploaded = requireData(result, `Failed to upload ${input.name}`);
5970
+ return {
5971
+ id: uploaded.external_id,
5972
+ name: input.name,
5973
+ type: input.type || "application/octet-stream",
5974
+ size: input.bytes.length,
5975
+ key: client.bytesToBase64(key)
5976
+ };
5977
+ } finally {
5978
+ key.fill(0);
5979
+ }
5980
+ }
5981
+ async function uploadDmAttachments(arbi, inputs) {
5982
+ checkDmAttachmentLimits(inputs.map((i2) => ({ name: i2.name, size: i2.bytes.length })));
5983
+ const files = [];
5984
+ for (const input of inputs) files.push(await uploadDmAttachment(arbi, input));
5985
+ return files;
5986
+ }
5987
+ function serializeDmAttachmentManifest(files) {
5988
+ return JSON.stringify({
5989
+ v: MANIFEST_VERSION,
5990
+ files: files.map(({ id, name, type, size, key }) => ({ id, name, type, size, key }))
5991
+ });
5992
+ }
5993
+ function parseDmAttachmentManifest(json2) {
5994
+ const manifest = JSON.parse(json2);
5995
+ if (manifest?.v !== MANIFEST_VERSION || !Array.isArray(manifest.files)) {
5996
+ throw new Error("Unsupported attachment manifest");
5997
+ }
5998
+ return manifest.files.map((entry) => {
5999
+ const f2 = entry;
6000
+ if (typeof f2.id !== "string" || !ATTACHMENT_ID.test(f2.id) || typeof f2.name !== "string" || typeof f2.size !== "number" || !Number.isInteger(f2.size) || f2.size < 0 || typeof f2.key !== "string") {
6001
+ throw new Error("Malformed attachment manifest");
6002
+ }
6003
+ return {
6004
+ id: f2.id,
6005
+ name: f2.name,
6006
+ type: typeof f2.type === "string" && f2.type ? f2.type : "application/octet-stream",
6007
+ size: f2.size,
6008
+ key: f2.key
6009
+ };
6010
+ });
6011
+ }
6012
+ async function encryptDmAttachmentManifest(arbi, files, recipientEncryptionPubKeyBase64, senderEncryptionSecretKey) {
6013
+ return arbi.crypto.encryptMessage(
6014
+ serializeDmAttachmentManifest(files),
6015
+ recipientEncryptionPubKeyBase64,
6016
+ senderEncryptionSecretKey
6017
+ );
6018
+ }
6019
+ async function decryptDmAttachments(arbi, notification, crypto2) {
6020
+ if (!notification.attachments) return [];
6021
+ const isSent = notification.sender.external_id === crypto2.userExtId;
6022
+ const otherParty = isSent ? notification.recipient : notification.sender;
6023
+ const json2 = await arbi.crypto.decryptMessage(
6024
+ notification.attachments,
6025
+ otherParty.encryption_public_key,
6026
+ crypto2.encryptionKeypair.secretKey
6027
+ );
6028
+ return parseDmAttachmentManifest(json2);
6029
+ }
6030
+ async function downloadDmAttachment(arbi, file) {
6031
+ if (!ATTACHMENT_ID.test(file.id)) throw new Error(`Invalid attachment id: ${file.id}`);
6032
+ const result = await arbi.fetch.GET("/v1/notifications/attachments/{external_id}", {
6033
+ params: { path: { external_id: file.id } },
6034
+ parseAs: "arrayBuffer"
6035
+ });
6036
+ if (result.response?.status === 404) {
6037
+ throw new ArbiApiError(`${file.name} is no longer available`, result.error, 404);
6038
+ }
6039
+ const ciphertext = requireData(
6040
+ result,
6041
+ `Failed to download ${file.name}`
6042
+ );
6043
+ await client.initSodium();
6044
+ const key = client.base64ToBytes(file.key);
6045
+ let plain;
6046
+ try {
6047
+ if (key.length !== 32) throw new Error("bad key");
6048
+ plain = client.decryptFile(new Uint8Array(ciphertext), key);
6049
+ } catch {
6050
+ throw new Error(`Could not decrypt ${file.name}: the file was altered or the key is wrong`);
6051
+ } finally {
6052
+ key.fill(0);
6053
+ }
6054
+ if (plain.length !== file.size) {
6055
+ throw new Error(`${file.name} is ${plain.length} bytes, expected ${file.size} bytes`);
6056
+ }
6057
+ return plain;
6058
+ }
5917
6059
 
5918
6060
  // src/operations/dm.ts
5919
6061
  function createDmCryptoContext(arbi, signingPrivateKeyBytes, userExtId) {
@@ -5944,16 +6086,25 @@ async function decryptDmContent(arbi, notification, crypto2) {
5944
6086
  async function decryptDmBatch(arbi, notifications, crypto2) {
5945
6087
  const results = [];
5946
6088
  for (const n2 of notifications) {
5947
- if (n2.type !== "user_message" || !n2.content) {
6089
+ if (n2.type !== "user_message" || !n2.content && !n2.attachments) {
5948
6090
  results.push(n2);
5949
6091
  continue;
5950
6092
  }
6093
+ let row;
5951
6094
  try {
5952
- const plaintext = await decryptDmContent(arbi, n2, crypto2);
5953
- results.push({ ...n2, content: plaintext });
6095
+ row = { ...n2, content: await decryptDmContent(arbi, n2, crypto2) };
5954
6096
  } catch {
5955
- results.push({ ...n2, content: "[Decryption failed]" });
6097
+ row = { ...n2, content: "[Decryption failed]" };
5956
6098
  }
6099
+ if (n2.attachments) {
6100
+ try {
6101
+ row.attachmentFiles = await decryptDmAttachments(arbi, n2, crypto2);
6102
+ } catch {
6103
+ row.attachmentFiles = [];
6104
+ row.attachmentsError = "The attachments could not be decrypted";
6105
+ }
6106
+ }
6107
+ results.push(row);
5957
6108
  }
5958
6109
  return results;
5959
6110
  }
@@ -6009,7 +6160,9 @@ async function resolveRecipient(arbi, ref) {
6009
6160
  }
6010
6161
  return null;
6011
6162
  }
6012
- async function sendEncryptedDM(arbi, messages, crypto2) {
6163
+ async function sendEncryptedDM(arbi, messages, crypto2, options) {
6164
+ const attachments = options?.attachments ?? [];
6165
+ checkDmAttachmentLimits(attachments);
6013
6166
  const plaintextFor = new Map(messages.map((m2) => [m2.recipient_ext_id, m2.content]));
6014
6167
  const encrypted = await Promise.all(
6015
6168
  messages.map(async (msg) => ({
@@ -6019,18 +6172,31 @@ async function sendEncryptedDM(arbi, messages, crypto2) {
6019
6172
  msg.content,
6020
6173
  msg.recipient_encryption_public_key,
6021
6174
  crypto2.encryptionKeypair.secretKey
6022
- )
6175
+ ),
6176
+ ...attachments.length ? {
6177
+ attachments: await encryptDmAttachmentManifest(
6178
+ arbi,
6179
+ attachments,
6180
+ msg.recipient_encryption_public_key,
6181
+ crypto2.encryptionKeypair.secretKey
6182
+ )
6183
+ } : {}
6023
6184
  }))
6024
6185
  );
6025
6186
  const created = requireData(
6026
6187
  await arbi.fetch.POST("/v1/notifications/", {
6027
- body: { messages: encrypted, context_only: false }
6188
+ body: {
6189
+ messages: encrypted,
6190
+ context_only: false,
6191
+ ...attachments.length ? { attachment_ext_ids: attachments.map((f2) => f2.id) } : {}
6192
+ }
6028
6193
  }),
6029
6194
  "Failed to send message"
6030
6195
  );
6031
6196
  return created.map((n2) => ({
6032
6197
  ...n2,
6033
- content: plaintextFor.get(n2.recipient.external_id) ?? n2.content
6198
+ content: plaintextFor.get(n2.recipient.external_id) ?? n2.content,
6199
+ ...attachments.length ? { attachmentFiles: attachments } : {}
6034
6200
  }));
6035
6201
  }
6036
6202
  async function requestWorkspaceAccess(arbi, params, crypto2) {
@@ -8254,10 +8420,10 @@ function attachmentFromBytes(name, contentType, bytes) {
8254
8420
  name,
8255
8421
  content_type: contentType,
8256
8422
  size: bytes.length,
8257
- content_base64: bytesToBase64(bytes)
8423
+ content_base64: bytesToBase642(bytes)
8258
8424
  };
8259
8425
  }
8260
- function bytesToBase64(bytes) {
8426
+ function bytesToBase642(bytes) {
8261
8427
  let binary = "";
8262
8428
  for (let i2 = 0; i2 < bytes.length; i2 += 32768) {
8263
8429
  binary += String.fromCharCode(...bytes.subarray(i2, i2 + 32768));