@ai4b-team/fsaos-gateway-sdk 5.5.0 → 5.5.2

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.d.cts CHANGED
@@ -1273,6 +1273,10 @@ interface ChannelMessage {
1273
1273
  principal_id?: string;
1274
1274
  /** The sender's member item id, when the kernel recorded one (agent replies carry it). */
1275
1275
  member_id?: string;
1276
+ /** Saved author identity; survives the sender leaving the current chat roster. */
1277
+ sender_display_name?: string;
1278
+ sender_is_ai?: boolean;
1279
+ sender_role_display_name?: string;
1276
1280
  /** Replies under this message. Above zero, a top-level message is a thread (a side conversation inside the chat). */
1277
1281
  reply_count: number;
1278
1282
  /**
@@ -3329,6 +3333,8 @@ interface FileRef {
3329
3333
  size_bytes: number;
3330
3334
  version: number;
3331
3335
  etag?: string;
3336
+ /** SHA-256 verified by storage, when the upload used an integrity contract. */
3337
+ content_sha256?: string;
3332
3338
  }
3333
3339
  interface UploadOptions {
3334
3340
  /** VFS path where the file item will be created (required). */
package/dist/index.d.ts CHANGED
@@ -1273,6 +1273,10 @@ interface ChannelMessage {
1273
1273
  principal_id?: string;
1274
1274
  /** The sender's member item id, when the kernel recorded one (agent replies carry it). */
1275
1275
  member_id?: string;
1276
+ /** Saved author identity; survives the sender leaving the current chat roster. */
1277
+ sender_display_name?: string;
1278
+ sender_is_ai?: boolean;
1279
+ sender_role_display_name?: string;
1276
1280
  /** Replies under this message. Above zero, a top-level message is a thread (a side conversation inside the chat). */
1277
1281
  reply_count: number;
1278
1282
  /**
@@ -3329,6 +3333,8 @@ interface FileRef {
3329
3333
  size_bytes: number;
3330
3334
  version: number;
3331
3335
  etag?: string;
3336
+ /** SHA-256 verified by storage, when the upload used an integrity contract. */
3337
+ content_sha256?: string;
3332
3338
  }
3333
3339
  interface UploadOptions {
3334
3340
  /** VFS path where the file item will be created (required). */
package/dist/index.js CHANGED
@@ -2201,6 +2201,9 @@ function parseChannelMessages(rawItems) {
2201
2201
  seq: td.seq || 0,
2202
2202
  principal_id: td.principal_id,
2203
2203
  member_id: typeof td.member_id === "string" ? td.member_id : void 0,
2204
+ sender_display_name: typeof td.sender_display_name === "string" ? td.sender_display_name : void 0,
2205
+ sender_is_ai: typeof td.sender_is_ai === "boolean" ? td.sender_is_ai : void 0,
2206
+ sender_role_display_name: typeof td.sender_role_display_name === "string" ? td.sender_role_display_name : void 0,
2204
2207
  reply_count: td.reply_count || 0,
2205
2208
  member_count: td.member_count || 0,
2206
2209
  parent_message_id: td.parent_message_id,
@@ -6236,6 +6239,32 @@ function getRequiredFormFields(typeDef) {
6236
6239
  (f) => required.has(f.key) || f.default_value === void 0
6237
6240
  );
6238
6241
  }
6242
+
6243
+ // src/upload-integrity.ts
6244
+ var MAX_BUFFERED_HASH_BYTES = 16 * 1024 * 1024;
6245
+ var hashing = Promise.resolve();
6246
+ function checkUploadCancelled(signal2) {
6247
+ if (signal2?.aborted) throw new Error("Upload cancelled");
6248
+ }
6249
+ async function prepareUploadIntegrity(file, signal2) {
6250
+ checkUploadCancelled(signal2);
6251
+ if (file.size > MAX_BUFFERED_HASH_BYTES || !globalThis.crypto?.subtle) return {};
6252
+ const operation = hashing.then(async () => {
6253
+ checkUploadCancelled(signal2);
6254
+ const bytes = await file.arrayBuffer();
6255
+ checkUploadCancelled(signal2);
6256
+ const digest = await globalThis.crypto.subtle.digest("SHA-256", bytes);
6257
+ checkUploadCancelled(signal2);
6258
+ return {
6259
+ expected_sha256: Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join(""),
6260
+ expected_size_bytes: file.size
6261
+ };
6262
+ });
6263
+ hashing = operation.catch(() => void 0);
6264
+ return operation;
6265
+ }
6266
+
6267
+ // src/upload.ts
6239
6268
  function generateUploadId() {
6240
6269
  if (globalThis.crypto?.randomUUID) return globalThis.crypto.randomUUID();
6241
6270
  const bytes = new Uint8Array(16);
@@ -6257,6 +6286,12 @@ async function uploadFile(file, options, onProgress, signal2) {
6257
6286
  const itemType = options.itemType || "file";
6258
6287
  const collisionPolicy = options.collisionPolicy || "keep_both";
6259
6288
  const uploadId = collisionPolicy === "keep_both" ? generateUploadId() : void 0;
6289
+ const scopeVersion = getScopeVersion(), sessionGeneration2 = getSessionGeneration();
6290
+ const integrity = await prepareUploadIntegrity(file, signal2);
6291
+ checkUploadCancelled(signal2);
6292
+ if (scopeVersion !== getScopeVersion() || sessionGeneration2 !== getSessionGeneration()) {
6293
+ throw new Error("The account or Space changed while preparing the upload. Start it again.");
6294
+ }
6260
6295
  const createParams = {
6261
6296
  parent_path: options.parentPath,
6262
6297
  item_type: itemType,
@@ -6267,7 +6302,8 @@ async function uploadFile(file, options, onProgress, signal2) {
6267
6302
  type: "upload",
6268
6303
  content_type: contentType,
6269
6304
  collision_policy: collisionPolicy,
6270
- ...uploadId ? { upload_id: uploadId } : {}
6305
+ ...uploadId ? { upload_id: uploadId } : {},
6306
+ ...integrity
6271
6307
  }
6272
6308
  }
6273
6309
  };
@@ -6275,6 +6311,7 @@ async function uploadFile(file, options, onProgress, signal2) {
6275
6311
  createParams.card_parent_path = options.cardParentPath;
6276
6312
  }
6277
6313
  const createResult = await gatewayCall("create", createParams);
6314
+ checkUploadCancelled(signal2);
6278
6315
  const result = createResult;
6279
6316
  if (!result.id) {
6280
6317
  throw new Error(result.error || result.message || "Failed to create file item");
@@ -6291,7 +6328,8 @@ async function uploadFile(file, options, onProgress, signal2) {
6291
6328
  throw new Error("Gateway returned no storage_key \u2014 file_source may not be supported for this item type");
6292
6329
  }
6293
6330
  await uploadBinaryXHR(storageWorkerUrl, storageKey, uploadToken, file, contentType, onProgress, signal2);
6294
- const fileRef = await confirmUpload(itemId, storageKey, contentType);
6331
+ checkUploadCancelled(signal2);
6332
+ const fileRef = await confirmUpload(itemId, storageKey, contentType, uploadToken, integrity.expected_sha256, file.size);
6295
6333
  return { itemId, itemPath, fileRef };
6296
6334
  }
6297
6335
  function uploadBinaryXHR(storageWorkerUrl, storageKey, uploadToken, body, contentType, onProgress, signal2) {
@@ -6317,6 +6355,7 @@ function uploadBinaryXHR(storageWorkerUrl, storageKey, uploadToken, body, conten
6317
6355
  };
6318
6356
  xhr.onerror = () => reject(new Error("Storage upload network error"));
6319
6357
  xhr.ontimeout = () => reject(new Error("Storage upload timed out"));
6358
+ xhr.onabort = () => reject(new Error("Upload cancelled"));
6320
6359
  if (signal2) {
6321
6360
  if (signal2.aborted) {
6322
6361
  reject(new Error("Upload cancelled"));
@@ -6330,7 +6369,7 @@ function uploadBinaryXHR(storageWorkerUrl, storageKey, uploadToken, body, conten
6330
6369
  xhr.send(body);
6331
6370
  });
6332
6371
  }
6333
- async function confirmUpload(itemId, storageKey, contentType) {
6372
+ async function confirmUpload(itemId, storageKey, contentType, uploadToken, contentSha256, sizeBytes) {
6334
6373
  const token = await getAccessToken();
6335
6374
  const response = await fetch(`${GATEWAY_URL}/storage/upload-complete`, {
6336
6375
  method: "POST",
@@ -6341,7 +6380,9 @@ async function confirmUpload(itemId, storageKey, contentType) {
6341
6380
  body: JSON.stringify({
6342
6381
  item_id: itemId,
6343
6382
  storage_key: storageKey,
6344
- content_type: contentType
6383
+ content_type: contentType,
6384
+ ...uploadToken ? { upload_token: uploadToken } : {},
6385
+ ...contentSha256 ? { hash: contentSha256 } : {}
6345
6386
  })
6346
6387
  });
6347
6388
  if (!response.ok) {
@@ -6352,6 +6393,9 @@ async function confirmUpload(itemId, storageKey, contentType) {
6352
6393
  if (!result.success) {
6353
6394
  throw new Error(result.error || result.message || "Upload confirmation rejected");
6354
6395
  }
6396
+ if (contentSha256 && (result.file_ref?.content_sha256 !== contentSha256 || result.file_ref?.size_bytes !== sizeBytes || result.file_ref?.content_type !== contentType)) {
6397
+ throw new Error("Stored file did not confirm the expected checksum, size and type");
6398
+ }
6355
6399
  return result.file_ref;
6356
6400
  }
6357
6401
  function useFileUpload() {
@@ -6359,6 +6403,7 @@ function useFileUpload() {
6359
6403
  const qc = useQueryClient();
6360
6404
  const uploadsRef = useRef(uploads);
6361
6405
  uploadsRef.current = uploads;
6406
+ const attempts = useRef(/* @__PURE__ */ new Map());
6362
6407
  const updateUpload = useCallback((id, patch) => {
6363
6408
  setUploads((prev) => prev.map((u) => u.id === id ? { ...u, ...patch } : u));
6364
6409
  }, []);
@@ -6375,9 +6420,20 @@ function useFileUpload() {
6375
6420
  let storageKey = existingState?.storageKey || null;
6376
6421
  let uploadToken = existingState?.uploadToken || null;
6377
6422
  let storageWorkerUrl = existingState?.storageWorkerUrl || null;
6423
+ let contentSha256 = existingState?.contentSha256 || void 0;
6424
+ const controller = new AbortController();
6425
+ const scopeVersion = getScopeVersion(), sessionGeneration2 = getSessionGeneration();
6426
+ attempts.current.get(uploadId)?.abort();
6427
+ attempts.current.set(uploadId, controller);
6378
6428
  try {
6379
6429
  if (!resumeFrom || resumeFrom === "create") {
6380
6430
  updateUpload(uploadId, { status: "creating", error: null, _failedAt: null });
6431
+ const integrity = await prepareUploadIntegrity(file, controller.signal);
6432
+ checkUploadCancelled(controller.signal);
6433
+ if (scopeVersion !== getScopeVersion() || sessionGeneration2 !== getSessionGeneration()) {
6434
+ throw new Error("The account or Space changed while preparing the upload. Start it again.");
6435
+ }
6436
+ contentSha256 = integrity.expected_sha256;
6381
6437
  const createResult = await gatewayCall("create", {
6382
6438
  parent_path: options.parentPath,
6383
6439
  item_type: itemType,
@@ -6388,10 +6444,12 @@ function useFileUpload() {
6388
6444
  type: "upload",
6389
6445
  content_type: contentType,
6390
6446
  collision_policy: collisionPolicy,
6391
- ...collisionPolicy === "keep_both" ? { upload_id: uploadId } : {}
6447
+ ...collisionPolicy === "keep_both" ? { upload_id: uploadId } : {},
6448
+ ...integrity
6392
6449
  }
6393
6450
  }
6394
6451
  });
6452
+ checkUploadCancelled(controller.signal);
6395
6453
  const result = createResult;
6396
6454
  if (!result.id) {
6397
6455
  throw Object.assign(
@@ -6417,7 +6475,8 @@ function useFileUpload() {
6417
6475
  updateUpload(uploadId, {
6418
6476
  _storageKey: storageKey,
6419
6477
  _uploadToken: uploadToken,
6420
- _storageWorkerUrl: storageWorkerUrl
6478
+ _storageWorkerUrl: storageWorkerUrl,
6479
+ _contentSha256: contentSha256 || null
6421
6480
  });
6422
6481
  if (!storageKey) {
6423
6482
  throw Object.assign(
@@ -6460,19 +6519,27 @@ function useFileUpload() {
6460
6519
  new Error("Storage upload timed out"),
6461
6520
  { _step: "upload" }
6462
6521
  ));
6522
+ xhr.onabort = () => reject(Object.assign(new Error("Upload cancelled"), { _step: "upload" }));
6523
+ controller.signal.addEventListener("abort", () => xhr.abort(), { once: true });
6524
+ checkUploadCancelled(controller.signal);
6463
6525
  xhr.send(file);
6464
6526
  });
6465
6527
  await xhrPromise;
6528
+ checkUploadCancelled(controller.signal);
6466
6529
  updateUpload(uploadId, { progress: 100, _xhr: null });
6467
6530
  }
6468
6531
  updateUpload(uploadId, { status: "confirming", error: null, _failedAt: null });
6469
- const fileRef = await confirmUpload(itemId, storageKey, contentType);
6532
+ const fileRef = await confirmUpload(itemId, storageKey, contentType, uploadToken || "", contentSha256, file.size).catch((error) => {
6533
+ throw Object.assign(error, { _step: "confirm" });
6534
+ });
6535
+ checkUploadCancelled(controller.signal);
6470
6536
  updateUpload(uploadId, {
6471
6537
  status: "complete",
6472
6538
  fileRef
6473
6539
  });
6474
6540
  qc.invalidateQueries({ queryKey: vfsKeys.children(options.parentPath) });
6475
6541
  } catch (err) {
6542
+ if (attempts.current.get(uploadId) !== controller) return;
6476
6543
  const message = err?.message || String(err);
6477
6544
  const failedAt = err?._step || "create";
6478
6545
  updateUpload(uploadId, {
@@ -6481,6 +6548,8 @@ function useFileUpload() {
6481
6548
  _failedAt: failedAt,
6482
6549
  _xhr: null
6483
6550
  });
6551
+ } finally {
6552
+ if (attempts.current.get(uploadId) === controller) attempts.current.delete(uploadId);
6484
6553
  }
6485
6554
  }, [updateUpload, qc]);
6486
6555
  const upload = useCallback((file, options) => {
@@ -6498,6 +6567,7 @@ function useFileUpload() {
6498
6567
  _storageKey: null,
6499
6568
  _uploadToken: null,
6500
6569
  _storageWorkerUrl: null,
6570
+ _contentSha256: null,
6501
6571
  _xhr: null,
6502
6572
  _failedAt: null
6503
6573
  };
@@ -6518,11 +6588,13 @@ function useFileUpload() {
6518
6588
  uploadToken: item._uploadToken || "",
6519
6589
  storageWorkerUrl: item._storageWorkerUrl || "https://fsaos-storage.radns.workers.dev",
6520
6590
  itemId: item.itemId,
6521
- itemPath: item.itemPath || ""
6591
+ itemPath: item.itemPath || "",
6592
+ contentSha256: item._contentSha256
6522
6593
  } : void 0;
6523
6594
  executeUpload(uploadId, item.file, item.options, resumeFrom, existingState);
6524
6595
  }, [executeUpload]);
6525
6596
  const cancelUpload = useCallback((uploadId) => {
6597
+ attempts.current.get(uploadId)?.abort();
6526
6598
  const item = uploadsRef.current.find((u) => u.id === uploadId);
6527
6599
  if (!item) return;
6528
6600
  if (item._xhr) {
@@ -6536,6 +6608,8 @@ function useFileUpload() {
6536
6608
  });
6537
6609
  }, [updateUpload]);
6538
6610
  const removeUpload = useCallback((uploadId) => {
6611
+ attempts.current.get(uploadId)?.abort();
6612
+ attempts.current.delete(uploadId);
6539
6613
  setUploads((prev) => prev.filter((u) => u.id !== uploadId));
6540
6614
  }, []);
6541
6615
  const clearCompleted = useCallback(() => {
@@ -6544,7 +6618,7 @@ function useFileUpload() {
6544
6618
  const isUploading = uploads.some(
6545
6619
  (u) => u.status === "creating" || u.status === "uploading" || u.status === "confirming"
6546
6620
  );
6547
- const publicUploads = uploads.map(({ _storageKey, _uploadToken, _storageWorkerUrl, _xhr, _failedAt, ...rest }) => rest);
6621
+ const publicUploads = uploads.map(({ _storageKey, _uploadToken, _storageWorkerUrl, _contentSha256, _xhr, _failedAt, ...rest }) => rest);
6548
6622
  return {
6549
6623
  upload,
6550
6624
  uploadMultiple,