@arbidocs/sdk 0.5.136 → 0.5.138

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
@@ -5078,6 +5078,15 @@ function createDocumentWaiter(options) {
5078
5078
  function isNotification(msg) {
5079
5079
  return "sender" in msg && "recipient" in msg;
5080
5080
  }
5081
+ function uploadSetTotals(content) {
5082
+ let meta = {};
5083
+ try {
5084
+ meta = JSON.parse(content || "{}") ?? {};
5085
+ } catch {
5086
+ }
5087
+ const count = (key) => typeof meta[key] === "number" ? meta[key] : 0;
5088
+ return { total: count("total"), completed: count("completed"), failed: count("failed") };
5089
+ }
5081
5090
  function formatWsMessage(msg) {
5082
5091
  if (client.isMessageType(msg, "task_update")) {
5083
5092
  const progress = msg.progress > 0 ? ` (${Math.round(msg.progress)}%)` : "";
@@ -5090,7 +5099,8 @@ function formatWsMessage(msg) {
5090
5099
  const count = msg.doc_ext_ids.length;
5091
5100
  return {
5092
5101
  text: `Batch complete: ${msg.batch_type} \u2014 ${count} doc${count !== 1 ? "s" : ""}`,
5093
- level: "success"
5102
+ // A batch of an upload set is progress: the set's one notification says when it is done.
5103
+ level: msg.upload_set_id ? "info" : "success"
5094
5104
  };
5095
5105
  }
5096
5106
  if (client.isMessageType(msg, "error")) {
@@ -5109,6 +5119,11 @@ function formatWsMessage(msg) {
5109
5119
  if (msg.type === "chat.message") {
5110
5120
  return { text: `New message in ${msg.chat_ext_id}`, level: "info" };
5111
5121
  }
5122
+ if (isNotification(msg) && msg.type === "upload_set_complete") {
5123
+ const totals = uploadSetTotals(msg.content);
5124
+ const text2 = totals.completed === totals.total ? `Upload done: ${totals.completed} doc${totals.completed !== 1 ? "s" : ""} indexed` : `Upload done: ${totals.completed} of ${totals.total} docs indexed` + (totals.failed ? `, ${totals.failed} failed` : "");
5125
+ return { text: text2, level: totals.failed || totals.completed < totals.total ? "warning" : "success" };
5126
+ }
5112
5127
  if (isNotification(msg)) {
5113
5128
  const sender = msg.sender?.email || "someone";
5114
5129
  const content = msg.content ?? msg.type;
@@ -6364,6 +6379,7 @@ __export(documents_exports, {
6364
6379
  listPaginated: () => listPaginated,
6365
6380
  listVersions: () => listVersions,
6366
6381
  mimeFromName: () => mimeFromName,
6382
+ newUploadSet: () => newUploadSet,
6367
6383
  presignedPut: () => presignedPut,
6368
6384
  restoreVersion: () => restoreVersion,
6369
6385
  sanitizeFolderPath: () => sanitizeFolderPath,
@@ -6373,6 +6389,7 @@ __export(documents_exports, {
6373
6389
  uploadDocumentsDirect: () => uploadDocumentsDirect,
6374
6390
  uploadFile: () => uploadFile,
6375
6391
  uploadFiles: () => uploadFiles,
6392
+ uploadSetQuery: () => uploadSetQuery,
6376
6393
  uploadUrl: () => uploadUrl
6377
6394
  });
6378
6395
  var MAX_PAGES = 400;
@@ -6422,6 +6439,17 @@ var SUPPORTED_EXTENSIONS = /* @__PURE__ */ new Set([
6422
6439
  function sanitizeFolderPath(folderPath) {
6423
6440
  return folderPath.replace(/[\x00-\x1f\x7f]/g, "").replace(/\\/g, "/").replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "").trim();
6424
6441
  }
6442
+ function newUploadSet(size) {
6443
+ const crypto2 = globalThis.crypto;
6444
+ const id = typeof crypto2?.randomUUID === "function" ? crypto2.randomUUID() : Array.from(
6445
+ crypto2.getRandomValues(new Uint8Array(16)),
6446
+ (b2) => b2.toString(16).padStart(2, "0")
6447
+ ).join("");
6448
+ return { id, size: Math.max(1, Math.floor(size)) };
6449
+ }
6450
+ function uploadSetQuery(set) {
6451
+ return set ? { upload_set_id: set.id, upload_set_size: set.size } : {};
6452
+ }
6425
6453
  async function listDocuments(arbi, workspaceExtId) {
6426
6454
  if (!workspaceExtId) {
6427
6455
  return requireData(await arbi.fetch.GET("/v1/document/list"), "Failed to fetch documents");
@@ -6604,6 +6632,7 @@ async function uploadDocuments(arbi, files, options = {}) {
6604
6632
  query2.derived_from = options.derivedFrom.docId;
6605
6633
  query2.derived_role = options.derivedFrom.role;
6606
6634
  }
6635
+ Object.assign(query2, uploadSetQuery(options.uploadSet));
6607
6636
  const result = await arbi.fetch.POST("/v1/document/upload", {
6608
6637
  params: { query: query2 },
6609
6638
  ...options.onProgress ? { fetch: progressFetch(options.onProgress, arbi.http) } : {},
@@ -6664,6 +6693,8 @@ async function uploadFile(auth, fileData, fileName, options) {
6664
6693
  params.set("derived_from", options.derivedFrom.docId);
6665
6694
  params.set("derived_role", options.derivedFrom.role);
6666
6695
  }
6696
+ for (const [key, value] of Object.entries(uploadSetQuery(options?.uploadSet)))
6697
+ params.set(key, String(value));
6667
6698
  const qs = params.toString();
6668
6699
  const res = await authenticatedFetch({
6669
6700
  ...auth,
@@ -6686,6 +6717,8 @@ async function uploadFiles(auth, files, options) {
6686
6717
  params.set("derived_from", options.derivedFrom.docId);
6687
6718
  params.set("derived_role", options.derivedFrom.role);
6688
6719
  }
6720
+ for (const [key, value] of Object.entries(uploadSetQuery(options?.uploadSet)))
6721
+ params.set(key, String(value));
6689
6722
  const qs = params.toString();
6690
6723
  const res = await authenticatedFetch({
6691
6724
  ...auth,
@@ -6797,7 +6830,7 @@ async function uploadDocumentsDirect(arbi, workspaceKey, files, options) {
6797
6830
  folder: options.folder ? sanitizeFolderPath(options.folder) : null,
6798
6831
  presign_expires_in: options.presignExpiresIn ?? 3600
6799
6832
  };
6800
- const query2 = { workspace_ext_id: options.workspaceExtId };
6833
+ const query2 = { workspace_ext_id: options.workspaceExtId, ...uploadSetQuery(options.uploadSet) };
6801
6834
  const initResult = requireUploadData(
6802
6835
  await arbi.fetch.POST("/v1/document/upload-init", { params: { query: query2 }, body: initBody }),
6803
6836
  "Failed to initialize direct upload"
@@ -14786,6 +14819,7 @@ async function runGroupedDirectUpload(arbi, workspaceKey, groups, options) {
14786
14819
  const folders = /* @__PURE__ */ new Map();
14787
14820
  const totalFiles = groups.reduce((s2, g2) => s2 + g2.files.length, 0);
14788
14821
  const totalBatches = groups.length;
14822
+ const uploadSet = newUploadSet(totalFiles);
14789
14823
  let currentBatch = 0;
14790
14824
  let filesUploaded = 0;
14791
14825
  for (const group of groups) {
@@ -14802,7 +14836,8 @@ async function runGroupedDirectUpload(arbi, workspaceKey, groups, options) {
14802
14836
  const result = await uploadDocumentsDirect(arbi, workspaceKey, group.files, {
14803
14837
  folder: group.folder || void 0,
14804
14838
  configExtId: options.configExtId,
14805
- workspaceExtId: options.workspaceExtId
14839
+ workspaceExtId: options.workspaceExtId,
14840
+ uploadSet
14806
14841
  });
14807
14842
  const skipped = result.skipped ?? [];
14808
14843
  allDocIds.push(...result.doc_ext_ids);
@@ -14950,6 +14985,9 @@ async function uploadDirectory(auth, dirPath, options) {
14950
14985
  }
14951
14986
  let currentBatch = 0;
14952
14987
  let filesUploaded = 0;
14988
+ const uploadSet = newUploadSet(
14989
+ [...groups.values()].flat().filter((f2) => f2.size <= MAX_FILE_SIZE_BYTES).length
14990
+ );
14953
14991
  for (const [folder, batches] of folderBatches) {
14954
14992
  const folderDocIds = [];
14955
14993
  const folderSkipped = [];
@@ -14989,7 +15027,8 @@ async function uploadDirectory(auth, dirPath, options) {
14989
15027
  if (uploadable.length > 0) {
14990
15028
  result = await uploadFiles(auth, uploadable, {
14991
15029
  folder,
14992
- configExtId: options?.configExtId
15030
+ configExtId: options?.configExtId,
15031
+ uploadSet
14993
15032
  });
14994
15033
  }
14995
15034
  const skipped = [...batchSkipped, ...result.skipped ?? []];
@@ -15057,6 +15096,7 @@ async function uploadZip(auth, zipPath, options) {
15057
15096
  }
15058
15097
  let currentBatch = 0;
15059
15098
  let filesUploaded = 0;
15099
+ const uploadSet = newUploadSet(entries.length);
15060
15100
  for (const [folder, batches] of folderBatches) {
15061
15101
  const folderDocIds = [];
15062
15102
  const folderSkipped = [];
@@ -15079,7 +15119,8 @@ async function uploadZip(auth, zipPath, options) {
15079
15119
  }));
15080
15120
  const result = await uploadFiles(auth, blobs, {
15081
15121
  folder,
15082
- configExtId: options?.configExtId
15122
+ configExtId: options?.configExtId,
15123
+ uploadSet
15083
15124
  });
15084
15125
  const skipped = result.skipped ?? [];
15085
15126
  folderDocIds.push(...result.doc_ext_ids ?? []);
@@ -15245,6 +15286,7 @@ async function uploadManifest(auth, paths, options) {
15245
15286
  groups.get(pf.folder).push(pf);
15246
15287
  }
15247
15288
  const totalBytes = prepared.reduce((s2, p2) => s2 + p2.size, 0);
15289
+ const uploadSet = newUploadSet(prepared.length);
15248
15290
  let done = outcomes.length;
15249
15291
  let bytesDone = 0;
15250
15292
  for (const [folder, files] of groups) {
@@ -15298,7 +15340,7 @@ async function uploadManifest(auth, paths, options) {
15298
15340
  result = await uploadFiles(
15299
15341
  auth,
15300
15342
  readable.map((r2) => ({ data: r2.blob, name: r2.f.basename })),
15301
- { folder, configExtId: options?.configExtId }
15343
+ { folder, configExtId: options?.configExtId, uploadSet }
15302
15344
  );
15303
15345
  lastErr = void 0;
15304
15346
  break;