@ateam-ai-mobile/core 0.1.31 → 0.1.33

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.mjs CHANGED
@@ -1347,6 +1347,106 @@ function focusPlugin(pluginId, props) {
1347
1347
  pluginCommandEmitter.emit("focus-plugin", { pluginId, props });
1348
1348
  }
1349
1349
 
1350
+ // src/storage/types.ts
1351
+ var MAX_ATTACHMENT_BYTES = 90 * 1024 * 1024;
1352
+ var MAX_INLINE_ATTACHMENT_BYTES = 7 * 1024 * 1024;
1353
+
1354
+ // src/chat/attachments.ts
1355
+ var mb = (bytes) => Math.round(bytes / 1024 / 1024);
1356
+ var base64Length = (bytes) => Math.ceil(bytes / 3) * 4;
1357
+ var MAX_INLINE_CHARS = base64Length(MAX_INLINE_ATTACHMENT_BYTES);
1358
+ function tooLarge(name) {
1359
+ return new Error(`"${name}" is too large (max ${mb(MAX_ATTACHMENT_BYTES)} MB)`);
1360
+ }
1361
+ function errorFrom(status, body, name) {
1362
+ let message = "";
1363
+ try {
1364
+ message = String(JSON.parse(body)?.error || "");
1365
+ } catch {
1366
+ }
1367
+ if (status === 413) {
1368
+ return new Error(message || `This server can't take files this big yet ("${name}")`);
1369
+ }
1370
+ if (status === 429) return new Error(message || "Too many uploads: wait a moment and try again");
1371
+ return new Error(message || `The upload of "${name}" failed (${status})`);
1372
+ }
1373
+ async function sendRaw(uploader, att, onProgress) {
1374
+ const query = new URLSearchParams({ mimeType: att.mimeType || "application/octet-stream", name: att.name || "upload" });
1375
+ const url = `${getCoreApiUrl()}/api/artifacts?${query.toString()}`;
1376
+ const attempt = () => {
1377
+ const token = getToken();
1378
+ return uploader.upload({
1379
+ url,
1380
+ uri: att.uri,
1381
+ headers: {
1382
+ "Content-Type": "application/octet-stream",
1383
+ ...token ? { Authorization: `Bearer ${token}` } : {}
1384
+ },
1385
+ onProgress
1386
+ });
1387
+ };
1388
+ let res = await attempt();
1389
+ if (res.status === 401 && getToken()) {
1390
+ const outcome = await refreshToken();
1391
+ if (outcome.ok === "ok") {
1392
+ res = await attempt();
1393
+ if (res.status === 401 || res.status === 403) {
1394
+ notifyAuthFailure();
1395
+ throw new Error("Session expired: sign in again to upload this file");
1396
+ }
1397
+ } else if (outcome.ok === "auth_failed") {
1398
+ notifyAuthFailure();
1399
+ throw new Error("Session expired: sign in again to upload this file");
1400
+ } else {
1401
+ throw new Error("Could not reach the server to refresh your sign-in: check your connection and try again");
1402
+ }
1403
+ }
1404
+ return res;
1405
+ }
1406
+ async function uploadOne(att, onProgress, budget) {
1407
+ const name = att.name || "attachment";
1408
+ if (att.data !== void 0) return { data: att.data, mimeType: att.mimeType, name: att.name };
1409
+ if (!att.uri) throw new Error(`The attachment "${name}" has no file to send`);
1410
+ if (att.size !== void 0 && att.size > MAX_ATTACHMENT_BYTES) throw tooLarge(name);
1411
+ const uploader = getConfig().fileUploader;
1412
+ if (!uploader) throw new Error("This app cannot upload files yet: initAdas needs a fileUploader");
1413
+ const res = await sendRaw(uploader, att, onProgress);
1414
+ if (res.status >= 200 && res.status < 300) {
1415
+ let parsed = null;
1416
+ try {
1417
+ parsed = JSON.parse(res.body);
1418
+ } catch {
1419
+ }
1420
+ if (parsed?.ok && typeof parsed.hash === "string") {
1421
+ onProgress(1);
1422
+ return { hash: parsed.hash, name: att.name, mimeType: parsed.mimeType || att.mimeType };
1423
+ }
1424
+ throw errorFrom(res.status, res.body, name);
1425
+ }
1426
+ if (res.status === 400) {
1427
+ const size = att.size !== void 0 ? att.size : await uploader.fileSize?.(att.uri);
1428
+ if (size === void 0) {
1429
+ throw new Error(`Cannot tell how big "${name}" is, and this server cannot take large files yet`);
1430
+ }
1431
+ if (base64Length(size) > budget.chars) {
1432
+ throw new Error(`"${name}" is too big for this server, which cannot take more than ${mb(MAX_INLINE_ATTACHMENT_BYTES)} MB of files in one message yet`);
1433
+ }
1434
+ const data = await uploader.readBase64(att.uri);
1435
+ budget.chars -= data.length;
1436
+ onProgress(1);
1437
+ return { data, mimeType: att.mimeType, name: att.name };
1438
+ }
1439
+ throw errorFrom(res.status, res.body, name);
1440
+ }
1441
+ async function prepareAttachments(attachments, onProgress) {
1442
+ const budget = { chars: MAX_INLINE_CHARS - attachments.reduce((n, a) => n + (a.data !== void 0 ? a.data.length : 0), 0) };
1443
+ const out = [];
1444
+ for (let i = 0; i < attachments.length; i++) {
1445
+ out.push(await uploadOne(attachments[i], (f) => onProgress?.((i + f) / attachments.length), budget));
1446
+ }
1447
+ return out;
1448
+ }
1449
+
1350
1450
  // src/chat/store.ts
1351
1451
  function coreHeaders() {
1352
1452
  const token = getToken();
@@ -1681,8 +1781,11 @@ var useChatStore = create4((set, get) => ({
1681
1781
  attachments: attachments.map((a) => ({
1682
1782
  mimeType: a.mimeType,
1683
1783
  name: a.name,
1684
- previewUri: a.mimeType.startsWith("image/") ? `data:${a.mimeType};base64,${a.data}` : void 0
1685
- }))
1784
+ // An image on the phone previews from its own file; an inline one from its base64.
1785
+ previewUri: !a.mimeType.startsWith("image/") ? void 0 : a.data !== void 0 ? `data:${a.mimeType};base64,${a.data}` : a.uri
1786
+ })),
1787
+ // A file on the phone is uploaded before the message is posted: the bubble shows how far along it is.
1788
+ ...attachments.some((a) => a.uri && a.data === void 0) ? { uploadProgress: 0 } : {}
1686
1789
  } : {}
1687
1790
  };
1688
1791
  set((s) => ({ messages: [...s.messages, userMsg], loading: true, error: null }));
@@ -1699,7 +1802,15 @@ var useChatStore = create4((set, get) => ({
1699
1802
  }
1700
1803
  };
1701
1804
  if (skillSlug) body.skillSlug = skillSlug;
1702
- if (attachments?.length) body.attachments = attachments;
1805
+ if (attachments?.length) {
1806
+ let shown = 0;
1807
+ body.attachments = await prepareAttachments(attachments, (f) => {
1808
+ if (f - shown < 0.01 && f < 1) return;
1809
+ shown = f;
1810
+ set((s) => ({ messages: s.messages.map((m) => m.id === userMsg.id && m.uploadProgress !== void 0 ? { ...m, uploadProgress: f } : m) }));
1811
+ });
1812
+ set((s) => ({ messages: s.messages.map((m) => m.id === userMsg.id ? { ...m, uploadProgress: void 0 } : m) }));
1813
+ }
1703
1814
  const res = await fetch(`${getCoreApiUrl()}/api/chat`, {
1704
1815
  method: "POST",
1705
1816
  headers: coreHeaders(),
@@ -1766,7 +1877,7 @@ var useChatStore = create4((set, get) => ({
1766
1877
  streamJob(jobId, getTenant(), getToken());
1767
1878
  } catch (err) {
1768
1879
  set((s) => ({
1769
- messages: [...s.messages, {
1880
+ messages: [...s.messages.map((m) => m.id === userMsg.id && m.uploadProgress !== void 0 ? { ...m, uploadProgress: void 0 } : m), {
1770
1881
  id: `error-${Date.now()}`,
1771
1882
  role: "system",
1772
1883
  content: `Error: ${err.message}`,
@@ -2223,6 +2334,8 @@ var borderRadius = {
2223
2334
  full: 9999
2224
2335
  };
2225
2336
  export {
2337
+ MAX_ATTACHMENT_BYTES,
2338
+ MAX_INLINE_ATTACHMENT_BYTES,
2226
2339
  PluginCommandEmitter,
2227
2340
  TABLE_BLOCK_CLOSE,
2228
2341
  TABLE_BLOCK_OPEN,
@@ -2283,6 +2396,7 @@ export {
2283
2396
  patchHomeAction,
2284
2397
  pluginCommandEmitter,
2285
2398
  postCommandResult,
2399
+ prepareAttachments,
2286
2400
  previewInvite,
2287
2401
  readDevMode,
2288
2402
  recentDispatches as recentNotifDispatches,