@arbidocs/react 0.5.125 → 0.5.127

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.
Files changed (36) hide show
  1. package/dist/artifacts/index.cjs +81 -38
  2. package/dist/artifacts/index.cjs.map +1 -1
  3. package/dist/artifacts/index.d.ts +3 -0
  4. package/dist/artifacts/useArtifactSession.js +48 -4
  5. package/dist/artifacts/useArtifactSession.js.map +2 -2
  6. package/dist/artifacts/versions.js +2 -1
  7. package/dist/artifacts/versions.js.map +2 -2
  8. package/dist/calls/livekit/index.cjs +42 -42
  9. package/dist/chat/turn-flush.js +3 -3
  10. package/dist/chat/turn-flush.js.map +2 -2
  11. package/dist/{chunk-WUYSVTKE.cjs → chunk-6RRKO4ZK.cjs} +4 -4
  12. package/dist/{chunk-WUYSVTKE.cjs.map → chunk-6RRKO4ZK.cjs.map} +1 -1
  13. package/dist/{chunk-FAJLJDWS.cjs → chunk-FOBB5U4Y.cjs} +5 -5
  14. package/dist/{chunk-FAJLJDWS.cjs.map → chunk-FOBB5U4Y.cjs.map} +1 -1
  15. package/dist/{chunk-3JAEX57E.cjs → chunk-G5DYDBQN.cjs} +4 -4
  16. package/dist/{chunk-3JAEX57E.cjs.map → chunk-G5DYDBQN.cjs.map} +1 -1
  17. package/dist/{chunk-7L7QRIQ2.cjs → chunk-H3XHBIZ6.cjs} +5 -5
  18. package/dist/{chunk-7L7QRIQ2.cjs.map → chunk-H3XHBIZ6.cjs.map} +1 -1
  19. package/dist/{chunk-NDIKMOBJ.cjs → chunk-KULS4YWP.cjs} +4 -3
  20. package/dist/chunk-KULS4YWP.cjs.map +1 -0
  21. package/dist/{chunk-GKGAPU6T.cjs → chunk-NK5KFHXP.cjs} +17 -2
  22. package/dist/chunk-NK5KFHXP.cjs.map +1 -0
  23. package/dist/{chunk-K6ELFSG6.cjs → chunk-TNJ5T2MV.cjs} +25 -25
  24. package/dist/{chunk-K6ELFSG6.cjs.map → chunk-TNJ5T2MV.cjs.map} +1 -1
  25. package/dist/document-tools/core.cjs +97 -97
  26. package/dist/document-tools/core.d.ts +1 -0
  27. package/dist/document-tools/index.cjs +99 -99
  28. package/dist/index.cjs +809 -806
  29. package/dist/index.cjs.map +1 -1
  30. package/dist/index.d.ts +20 -11
  31. package/dist/remote-control.js +12 -2
  32. package/dist/remote-control.js.map +2 -2
  33. package/dist/shell/index.cjs +38 -38
  34. package/package.json +3 -3
  35. package/dist/chunk-GKGAPU6T.cjs.map +0 -1
  36. package/dist/chunk-NDIKMOBJ.cjs.map +0 -1
@@ -1,11 +1,11 @@
1
1
  'use strict';
2
2
 
3
- var chunkGKGAPU6T_cjs = require('../chunk-GKGAPU6T.cjs');
4
- var chunk7L7QRIQ2_cjs = require('../chunk-7L7QRIQ2.cjs');
3
+ var chunkNK5KFHXP_cjs = require('../chunk-NK5KFHXP.cjs');
4
+ var chunkH3XHBIZ6_cjs = require('../chunk-H3XHBIZ6.cjs');
5
5
  var chunk3AADNIOK_cjs = require('../chunk-3AADNIOK.cjs');
6
6
  var chunkO32T7XRD_cjs = require('../chunk-O32T7XRD.cjs');
7
7
  require('../chunk-N73IMVFF.cjs');
8
- var chunkNDIKMOBJ_cjs = require('../chunk-NDIKMOBJ.cjs');
8
+ var chunkKULS4YWP_cjs = require('../chunk-KULS4YWP.cjs');
9
9
  var chunkVOFNX4P6_cjs = require('../chunk-VOFNX4P6.cjs');
10
10
  var chunkEM6DP62E_cjs = require('../chunk-EM6DP62E.cjs');
11
11
  require('../chunk-5NOZDH3F.cjs');
@@ -275,19 +275,19 @@ function useArtifactText(docId, options) {
275
275
  ...DEFAULT_ARTIFACT_TEXT_RETRY,
276
276
  ...options?.retry
277
277
  };
278
- const enabled = (options?.enabled ?? true) && chunkNDIKMOBJ_cjs.isDocumentId(docId) && arbi.hasWorkspace;
278
+ const enabled = (options?.enabled ?? true) && chunkKULS4YWP_cjs.isDocumentId(docId) && arbi.hasWorkspace;
279
279
  const query = reactQuery.useQuery({
280
280
  // Under the shared `parsedDoc` key, so a document's invalidation reaches it too.
281
281
  queryKey: [...chunkO32T7XRD_cjs.arbiQueryKeys.parsedDoc(docId ?? "", "marker"), "artifact-text"],
282
282
  queryFn: async () => {
283
- const { docId: plainId, version } = chunkNDIKMOBJ_cjs.parseVersionRef(docId);
283
+ const { docId: plainId, version } = chunkKULS4YWP_cjs.parseVersionRef(docId);
284
284
  const parsed = await arbi.documents.getParsedContent(
285
285
  plainId,
286
286
  "marker",
287
287
  version ?? void 0
288
288
  );
289
289
  if (!parsed.chunks?.some((c) => c.content?.trim())) throw new NotParsedYetError();
290
- return chunkNDIKMOBJ_cjs.flattenChunks(parsed.chunks, { unescape: true });
290
+ return chunkKULS4YWP_cjs.flattenChunks(parsed.chunks, { unescape: true });
291
291
  },
292
292
  enabled,
293
293
  retry: (failureCount, error) => !isRefusal(error) && failureCount < attempts,
@@ -325,17 +325,17 @@ function useArtifactText(docId, options) {
325
325
 
326
326
  // src/artifacts/useArtifactSession.ts
327
327
  function useArtifactVersions(workspaceId, docId) {
328
- const plainId = docId ? chunkNDIKMOBJ_cjs.parseVersionRef(docId).docId : null;
329
- const { data: documents } = chunkGKGAPU6T_cjs.useDocuments(workspaceId);
328
+ const plainId = docId ? chunkKULS4YWP_cjs.parseVersionRef(docId).docId : null;
329
+ const { data: documents } = chunkNK5KFHXP_cjs.useDocuments(workspaceId);
330
330
  const listed = react.useMemo(
331
331
  () => (documents ?? []).find((d) => d.external_id === plainId),
332
332
  [documents, plainId]
333
333
  );
334
- const { data: history } = chunk7L7QRIQ2_cjs.useDocumentVersionList(plainId, {
334
+ const { data: history } = chunkH3XHBIZ6_cjs.useDocumentVersionList(plainId, {
335
335
  enabled: !!plainId && documents !== void 0 && !listed
336
336
  });
337
337
  return react.useMemo(
338
- () => listed ? chunkNDIKMOBJ_cjs.documentVersions(listed) : chunkNDIKMOBJ_cjs.toArtifactVersions(plainId, history),
338
+ () => listed ? chunkKULS4YWP_cjs.documentVersions(listed) : chunkKULS4YWP_cjs.toArtifactVersions(plainId, history),
339
339
  [listed, history, plainId]
340
340
  );
341
341
  }
@@ -343,19 +343,20 @@ function useSaveArtifactVersion() {
343
343
  const arbi = chunkO32T7XRD_cjs.useArbi();
344
344
  const queryClient = reactQuery.useQueryClient();
345
345
  return reactQuery.useMutation({
346
- mutationFn: async ({ title, content, versionOf, configId, wpType }) => {
346
+ mutationFn: async ({ title, content, versionOf, configId, wpType, autosave }) => {
347
347
  const fileName = /\.md$/i.test(title) ? title : `${title}.md`;
348
348
  const result = await arbi.documents.uploadFile(
349
349
  new Blob([content], { type: "text/markdown" }),
350
350
  fileName,
351
351
  {
352
352
  wpType: wpType ?? "artifact",
353
- versionOf: versionOf ? chunkNDIKMOBJ_cjs.parseVersionRef(versionOf).docId : void 0,
353
+ autosave,
354
+ versionOf: versionOf ? chunkKULS4YWP_cjs.parseVersionRef(versionOf).docId : void 0,
354
355
  configExtId: configId ?? void 0
355
356
  }
356
357
  );
357
358
  const docId = result.doc_ext_ids?.[0];
358
- if (docId) await chunkGKGAPU6T_cjs.refreshDocuments(queryClient, arbi, [docId]);
359
+ if (docId) await chunkNK5KFHXP_cjs.refreshDocuments(queryClient, arbi, [docId]);
359
360
  await queryClient.invalidateQueries({ queryKey: chunkO32T7XRD_cjs.arbiQueryKeys.allDocumentVersions() });
360
361
  if (docId)
361
362
  await queryClient.invalidateQueries({ queryKey: chunkO32T7XRD_cjs.arbiQueryKeys.parsedDocument(docId) });
@@ -378,7 +379,7 @@ function useArtifactSession(options) {
378
379
  const [saved, setSaved] = react.useState(null);
379
380
  const adopted = saved && saved.forId === hostDocId ? saved : null;
380
381
  const docId = adopted?.id ?? hostDocId;
381
- const isLocal = !chunkNDIKMOBJ_cjs.isDocumentId(docId);
382
+ const isLocal = !chunkKULS4YWP_cjs.isDocumentId(docId);
382
383
  const [ownCompareId, setOwnCompareId] = react.useState(null);
383
384
  const activeCompareId = compareId === void 0 ? ownCompareId : compareId;
384
385
  const changeCompare = react.useCallback(
@@ -394,7 +395,8 @@ function useArtifactSession(options) {
394
395
  enabled: !!activeCompareId
395
396
  });
396
397
  const saveVersion = useSaveArtifactVersion();
397
- const current = chunkNDIKMOBJ_cjs.findVersion(versions, docId);
398
+ const autosaveVersion = saveVersion.mutateAsync;
399
+ const current = chunkKULS4YWP_cjs.findVersion(versions, docId);
398
400
  const save = react.useCallback(
399
401
  async (content) => {
400
402
  const result = await saveVersion.mutateAsync({
@@ -410,7 +412,7 @@ function useArtifactSession(options) {
410
412
  );
411
413
  const handleSaved = react.useCallback(
412
414
  (result, content) => {
413
- if (!chunkNDIKMOBJ_cjs.isDocumentId(hostDocId) && result.docId)
415
+ if (!chunkKULS4YWP_cjs.isDocumentId(hostDocId) && result.docId)
414
416
  setSaved({ forId: hostDocId, id: result.docId, text: content });
415
417
  onSaved?.(result, content);
416
418
  },
@@ -430,6 +432,36 @@ function useArtifactSession(options) {
430
432
  save,
431
433
  onSaved: handleSaved
432
434
  });
435
+ react.useEffect(() => {
436
+ if (isLocal || !session.hasChanges || session.isSaving) return;
437
+ const timer = setTimeout(() => {
438
+ void autosaveVersion({
439
+ title: title ?? current?.name ?? "Untitled",
440
+ content: session.content,
441
+ versionOf: docId,
442
+ configId,
443
+ autosave: true
444
+ }).catch(() => void 0);
445
+ }, 2500);
446
+ return () => clearTimeout(timer);
447
+ }, [
448
+ isLocal,
449
+ session.content,
450
+ session.hasChanges,
451
+ session.isSaving,
452
+ docId,
453
+ title,
454
+ current?.name,
455
+ configId,
456
+ autosaveVersion
457
+ ]);
458
+ react.useEffect(() => {
459
+ if (!docId) return;
460
+ return chunkNK5KFHXP_cjs.registerDocumentTurnFlush(docId, async () => {
461
+ if (session.hasChanges && !await session.save())
462
+ throw new Error("Save the document before sending your message.");
463
+ });
464
+ }, [docId, session]);
433
465
  const { redline } = session;
434
466
  return react.useMemo(
435
467
  () => ({
@@ -439,11 +471,11 @@ function useArtifactSession(options) {
439
471
  isLoading,
440
472
  status,
441
473
  versions,
442
- labelOf: (id) => chunkNDIKMOBJ_cjs.versionLabel(versions, id),
474
+ labelOf: (id) => chunkKULS4YWP_cjs.versionLabel(versions, id),
443
475
  view: {
444
476
  content: session.baseline,
445
477
  isEditing: session.isEditing,
446
- hasChanges: session.hasChanges,
478
+ hasChanges: session.hasChanges || !!current?.autosave,
447
479
  isSaving: session.isSaving,
448
480
  canCompare: session.canCompare,
449
481
  comparing: !!session.compareId,
@@ -471,7 +503,18 @@ function useArtifactSession(options) {
471
503
  onRevert: (regionId) => void session.revertRegion(regionId)
472
504
  }
473
505
  }),
474
- [session, redline, isLoading, status, retry, isLocal, versions, docId]
506
+ [
507
+ session,
508
+ redline,
509
+ isLoading,
510
+ status,
511
+ retry,
512
+ isLocal,
513
+ versions,
514
+ docId,
515
+ current?.id,
516
+ current?.autosave
517
+ ]
475
518
  );
476
519
  }
477
520
  function useArtifactSource(documentId, enabled = true) {
@@ -504,7 +547,7 @@ function useStreamedArtifact({
504
547
  const started = react.useRef(null);
505
548
  const content = artifact?.content ?? "";
506
549
  const title = artifact?.title ?? "Draft";
507
- const passThrough = !!artifact && chunkNDIKMOBJ_cjs.isDocumentId(artifact.doc_ext_id);
550
+ const passThrough = !!artifact && chunkKULS4YWP_cjs.isDocumentId(artifact.doc_ext_id);
508
551
  const needsSave = settled && !!artifact && !passThrough && !!content.trim() && !!workspaceId;
509
552
  react.useEffect(() => {
510
553
  const key = `${attempt}:${content}`;
@@ -514,7 +557,7 @@ function useStreamedArtifact({
514
557
  let stored;
515
558
  if (clean) stored = clean(content);
516
559
  else {
517
- const report = chunkNDIKMOBJ_cjs.cleanDraftWithReport(content);
560
+ const report = chunkKULS4YWP_cjs.cleanDraftWithReport(content);
518
561
  stored = report.text;
519
562
  setCleaned(report);
520
563
  onCleaned?.(report);
@@ -582,71 +625,71 @@ Object.defineProperty(exports, "revertRedlineRegion", {
582
625
  });
583
626
  Object.defineProperty(exports, "cleanDraftMarkdown", {
584
627
  enumerable: true,
585
- get: function () { return chunkNDIKMOBJ_cjs.cleanDraftMarkdown; }
628
+ get: function () { return chunkKULS4YWP_cjs.cleanDraftMarkdown; }
586
629
  });
587
630
  Object.defineProperty(exports, "cleanDraftWithReport", {
588
631
  enumerable: true,
589
- get: function () { return chunkNDIKMOBJ_cjs.cleanDraftWithReport; }
632
+ get: function () { return chunkKULS4YWP_cjs.cleanDraftWithReport; }
590
633
  });
591
634
  Object.defineProperty(exports, "documentVersions", {
592
635
  enumerable: true,
593
- get: function () { return chunkNDIKMOBJ_cjs.documentVersions; }
636
+ get: function () { return chunkKULS4YWP_cjs.documentVersions; }
594
637
  });
595
638
  Object.defineProperty(exports, "findVersion", {
596
639
  enumerable: true,
597
- get: function () { return chunkNDIKMOBJ_cjs.findVersion; }
640
+ get: function () { return chunkKULS4YWP_cjs.findVersion; }
598
641
  });
599
642
  Object.defineProperty(exports, "flattenChunks", {
600
643
  enumerable: true,
601
- get: function () { return chunkNDIKMOBJ_cjs.flattenChunks; }
644
+ get: function () { return chunkKULS4YWP_cjs.flattenChunks; }
602
645
  });
603
646
  Object.defineProperty(exports, "isDocumentId", {
604
647
  enumerable: true,
605
- get: function () { return chunkNDIKMOBJ_cjs.isDocumentId; }
648
+ get: function () { return chunkKULS4YWP_cjs.isDocumentId; }
606
649
  });
607
650
  Object.defineProperty(exports, "parseVersionRef", {
608
651
  enumerable: true,
609
- get: function () { return chunkNDIKMOBJ_cjs.parseVersionRef; }
652
+ get: function () { return chunkKULS4YWP_cjs.parseVersionRef; }
610
653
  });
611
654
  Object.defineProperty(exports, "promoteBoldTitle", {
612
655
  enumerable: true,
613
- get: function () { return chunkNDIKMOBJ_cjs.promoteBoldTitle; }
656
+ get: function () { return chunkKULS4YWP_cjs.promoteBoldTitle; }
614
657
  });
615
658
  Object.defineProperty(exports, "repairParsedStructure", {
616
659
  enumerable: true,
617
- get: function () { return chunkNDIKMOBJ_cjs.repairParsedStructure; }
660
+ get: function () { return chunkKULS4YWP_cjs.repairParsedStructure; }
618
661
  });
619
662
  Object.defineProperty(exports, "repairParsedTitle", {
620
663
  enumerable: true,
621
- get: function () { return chunkNDIKMOBJ_cjs.repairParsedTitle; }
664
+ get: function () { return chunkKULS4YWP_cjs.repairParsedTitle; }
622
665
  });
623
666
  Object.defineProperty(exports, "savedFromId", {
624
667
  enumerable: true,
625
- get: function () { return chunkNDIKMOBJ_cjs.savedFromId; }
668
+ get: function () { return chunkKULS4YWP_cjs.savedFromId; }
626
669
  });
627
670
  Object.defineProperty(exports, "stripAgentPreamble", {
628
671
  enumerable: true,
629
- get: function () { return chunkNDIKMOBJ_cjs.stripAgentPreamble; }
672
+ get: function () { return chunkKULS4YWP_cjs.stripAgentPreamble; }
630
673
  });
631
674
  Object.defineProperty(exports, "toArtifactVersions", {
632
675
  enumerable: true,
633
- get: function () { return chunkNDIKMOBJ_cjs.toArtifactVersions; }
676
+ get: function () { return chunkKULS4YWP_cjs.toArtifactVersions; }
634
677
  });
635
678
  Object.defineProperty(exports, "unescapeMarkdown", {
636
679
  enumerable: true,
637
- get: function () { return chunkNDIKMOBJ_cjs.unescapeMarkdown; }
680
+ get: function () { return chunkKULS4YWP_cjs.unescapeMarkdown; }
638
681
  });
639
682
  Object.defineProperty(exports, "unwrapCitationLinks", {
640
683
  enumerable: true,
641
- get: function () { return chunkNDIKMOBJ_cjs.unwrapCitationLinks; }
684
+ get: function () { return chunkKULS4YWP_cjs.unwrapCitationLinks; }
642
685
  });
643
686
  Object.defineProperty(exports, "versionLabel", {
644
687
  enumerable: true,
645
- get: function () { return chunkNDIKMOBJ_cjs.versionLabel; }
688
+ get: function () { return chunkKULS4YWP_cjs.versionLabel; }
646
689
  });
647
690
  Object.defineProperty(exports, "versionRef", {
648
691
  enumerable: true,
649
- get: function () { return chunkNDIKMOBJ_cjs.versionRef; }
692
+ get: function () { return chunkKULS4YWP_cjs.versionRef; }
650
693
  });
651
694
  Object.defineProperty(exports, "normalizeMarkdown", {
652
695
  enumerable: true,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/artifacts/redline-engine.ts","../../src/artifacts/useRedline.ts","../../src/artifacts/useArtifactEditSession.ts","../../src/artifacts/useArtifactText.ts","../../src/artifacts/useArtifactSession.ts","../../src/artifacts/useArtifactSource.ts","../../src/artifacts/renderArtifactMarkdown.tsx","../../src/artifacts/useStreamedArtifact.ts","../../src/artifacts/artifact-kind.ts"],"names":["buildHybridRedlineMarkdown","buildHybridRedlineWithRegions","revertRedlineRegion","useState","useRef","useEffect","normalizeMarkdown","useCallback","useMemo","savedFromId","content","useArbi","isDocumentId","useQuery","arbiQueryKeys","parseVersionRef","flattenChunks","useDocuments","useDocumentVersionList","documentVersions","toArtifactVersions","useQueryClient","useMutation","refreshDocuments","findVersion","versionLabel","jsx","MarkdownRenderer","cleanDraftWithReport"],"mappings":";;;;;;;;;;;;;;;;;AAsBO,IAAM,uBAAA,GAAyC;AAAA,EACpD,YAAA,EAAcA,4CAAA;AAAA,EACd,uBAAA,EAAyBC,+CAAA;AAAA,EACzB,YAAA,EAAcC;AAChB;ACQO,SAAS,UAAA,CAAW;AAAA,EACzB,QAAA;AAAA,EACA,WAAA;AAAA,EACA,SAAA,GAAY,KAAA;AAAA,EACZ,MAAA,GAAS;AACX,CAAA,EAAwC;AACtC,EAAA,MAAM,CAAC,OAAO,QAAQ,CAAA,GAAIC,eAAuB,EAAE,IAAA,EAAM,QAAQ,CAAA;AACjE,EAAA,MAAM,YAAA,GAAeC,aAAO,CAAC,CAAA;AAE7B,EAAAC,eAAA,CAAU,MAAM;AACd,IAAA,IAAI,QAAA,KAAa,MAAA,IAAa,WAAA,KAAgB,MAAA,EAAW;AACvD,MAAA,QAAA,CAAS,EAAE,IAAA,EAAM,MAAA,EAAQ,CAAA;AACzB,MAAA;AAAA,IACF;AACA,IAAA,IAAI,SAAA,EAAW;AACb,MAAA,QAAA,CAAS,EAAE,IAAA,EAAM,WAAA,EAAa,CAAA;AAC9B,MAAA;AAAA,IACF;AACA,IAAA,IAAI,QAAA,KAAa,IAAA,IAAQ,W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CAAK,MAAA,KAAW,OAAA,IAAW,IAAA,CAAK,MAAM,KAAA,GAAQ,OAAA;AAAA,OAAA,IAC9C,KAAK,MAAA,KAAW,OAAA,IAAW,IAAA,CAAK,MAAA,KAAW,SAAS,KAAA,GAAQ,OAAA;AAAA,OAChE,KAAA,GAAQ,SAAA;AAEb,EAAA,MAAM,KAAA,GACJ,UAAU,OAAA,GACN,IAAA,GACC,aAAa,IAAA,CAAK,KAAA,IAAS,IAAI,KAAA,CAAM,kCAAkC,CAAA;AAC9E,EAAA,OAAO,EAAE,KAAA,EAAO,QAAA,EAAU,KAAA,EAAO,KAAA,EAAO,SAAS,KAAA,EAAM;AACzD;;;AC7IO,SAAS,yBAAyB,QAAA,EAAsD;AAC7F,EAAA,MAAM,GAAA,GAAA,CAAO,YAAY,EAAA,EAAI,WAAA,GAAc,KAAA,CAAM,cAAc,IAAI,CAAC,CAAA;AACpE,EAAA,IAAI,GAAA,KAAQ,OAAA,IAAW,GAAA,KAAQ,MAAA,EAAQ,OAAO,MAAA;AAC9C,EAAA,IAAI,GAAA,KAAQ,QAAQ,OAAO,OAAA;AAC3B,EAAA,OAAO,IAAA;AACT;AAEO,SAAS,qBAAqB,IAAA,EAAkD;AACrF,EAAA,OAAO,IAAA,KAAS,MAAA,IAAU,IAAA,KAAS,OAAA,GAAU,IAAA,GAAO,IAAA;AACtD;AAOO,SAAS,yBACd,IAAA,EACuC;AACvC,EAAA,IAAI,IAAA,KAAS,SAAS,OAAO,KAAA;AAC7B,EAAA,IAAI,IAAA,KAAS,gBAAgB,OAAO,QAAA;AACpC,EAAA,OAAO,oBAAA,CAAqB,IAAI,CAAA,GAAI,SAAA,GAAY,MAAA;AAClD","file":"index.cjs","sourcesContent":["import {\n buildHybridRedlineMarkdown,\n buildHybridRedlineWithRegions,\n revertRedlineRegion,\n type RedlineWithRegions,\n} from './redline-diff'\n\n/**\n * Where a redline is computed. The default runs on the calling thread; a host with large\n * documents passes an engine backed by a Web Worker (for example a Comlink proxy exposing the same\n * three functions) so typing never waits on a diff.\n */\nexport interface RedlineEngine {\n buildRedline(baseText: string, compareText: string): string | Promise<string>\n buildRedlineWithRegions(\n baseText: string,\n compareText: string\n ): RedlineWithRegions | Promise<RedlineWithRegions>\n revertRegion(baseText: string, compareText: string, regionId: string): string | Promise<string>\n}\n\n/** Main-thread engine: the pure functions of `redline-diff`. */\nexport const mainThreadRedlineEngine: RedlineEngine = {\n buildRedline: buildHybridRedlineMarkdown,\n buildRedlineWithRegions: buildHybridRedlineWithRegions,\n revertRegion: revertRedlineRegion,\n}\n","import { useCallback, useEffect, useMemo, useRef, useState } from 'react'\nimport type { RedlineRegion } from './redline-diff'\nimport { normalizeMarkdown } from './normalize-markdown'\nimport { mainThreadRedlineEngine, type RedlineEngine } from './redline-engine'\n\n/** What a redline view can show. Mirrors the status parts of `@arbidocs/blocks/artifacts`. */\nexport type RedlineState =\n | { kind: 'idle' }\n | { kind: 'computing' }\n | { kind: 'ready'; markdown: string; regions: RedlineRegion[] }\n | { kind: 'identical' }\n | { kind: 'unavailable' }\n\nexport interface UseRedlineOptions {\n /** The older side. `null` means the text could not be loaded; `undefined` means not chosen. */\n baseText: string | null | undefined\n /** The newer side (a loaded version, or the live edit buffer). */\n compareText: string | null | undefined\n /** Either text is still being fetched. */\n isLoading?: boolean\n /** Defaults to the main thread; pass a worker-backed engine for big documents. */\n engine?: RedlineEngine\n}\n\nexport interface UseRedlineResult {\n state: RedlineState\n /** Undo one change: resolves with the compare text rebuilt without it (null if nothing changed). */\n revertRegion: (regionId: string) => Promise<string | null>\n}\n\n/**\n * Track-changes between two texts. Stale results are dropped when the inputs change mid-compute,\n * and a failed diff reports `unavailable` instead of throwing into the render.\n */\nexport function useRedline({\n baseText,\n compareText,\n isLoading = false,\n engine = mainThreadRedlineEngine,\n}: UseRedlineOptions): UseRedlineResult {\n const [state, setState] = useState<RedlineState>({ kind: 'idle' })\n const requestIdRef = useRef(0)\n\n useEffect(() => {\n if (baseText === undefined || compareText === undefined) {\n setState({ kind: 'idle' })\n return\n }\n if (isLoading) {\n setState({ kind: 'computing' })\n return\n }\n if (baseText === null || compareText === null) {\n setState({ kind: 'unavailable' })\n return\n }\n if (\n baseText === compareText ||\n normalizeMarkdown(baseText) === normalizeMarkdown(compareText)\n ) {\n setState({ kind: 'identical' })\n return\n }\n\n const id = ++requestIdRef.current\n let cancelled = false\n setState({ kind: 'computing' })\n // Every change carries a stable id, so a view can offer per-change undo.\n Promise.resolve(engine.buildRedlineWithRegions(baseText, compareText))\n .then((result) => {\n if (cancelled || id !== requestIdRef.current) return\n setState({ kind: 'ready', markdown: result.markdown, regions: result.regions })\n })\n .catch(() => {\n if (cancelled || id !== requestIdRef.current) return\n setState({ kind: 'unavailable' })\n })\n return () => {\n cancelled = true\n }\n }, [baseText, compareText, isLoading, engine])\n\n // The latest texts, so `revertRegion` stays referentially stable while the user types.\n const latest = useRef({ baseText, compareText })\n useEffect(() => {\n latest.current = { baseText, compareText }\n }, [baseText, compareText])\n\n const revertRegion = useCallback(\n async (regionId: string): Promise<string | null> => {\n const { baseText: base, compareText: compare } = latest.current\n if (!base || !compare) return null\n try {\n const next = await engine.revertRegion(base, compare, regionId)\n return next !== normalizeMarkdown(compare) ? next : null\n } catch {\n return null\n }\n },\n [engine]\n )\n\n return useMemo(() => ({ state, revertRegion }), [state, revertRegion])\n}\n","import { useCallback, useEffect, useMemo, useRef, useState } from 'react'\nimport { normalizeMarkdown } from './normalize-markdown'\nimport { useRedline, type UseRedlineResult } from './useRedline'\nimport type { RedlineEngine } from './redline-engine'\n\n/** What a save reports back. `docId` is the new version when the backend already knows it. */\nexport interface ArtifactSaveResult {\n docId?: string\n /** 1-based number of the version that was just saved (1 for the first save of a draft). */\n version?: number\n}\n\nexport interface ArtifactEditSessionOptions {\n /** The artifact being edited. Changing it discards the buffer and any comparison. */\n docId: string | null | undefined\n /**\n * The saved text of the open version. `undefined` while it loads; whenever it changes the\n * baseline follows it (a version arriving from the realtime channel, a finished stream).\n */\n baseline: string | undefined\n /**\n * A new document the user types from scratch: open straight in the editor, seeded with\n * `initialContent`, instead of showing a read-only view. Read when `docId` changes.\n */\n startEditing?: boolean\n initialContent?: string\n /** The version this one was saved from. Offering \"compare with previous\" needs it. */\n savedFromId?: string | null\n /** Controlled comparison target. Omit for an uncontrolled one the session owns. */\n compareId?: string | null\n onCompareChange?: (id: string | null) => void\n /** The saved text of the compare target (the older side of the diff). */\n compareText?: string | null\n compareLoading?: boolean\n /** Starting a comparison opens the editor with track changes on (default true). */\n compareOpensEditor?: boolean\n /** Persist the buffer as a NEW version. Rejecting keeps the buffer so nothing is lost. */\n save: (content: string) => Promise<ArtifactSaveResult | void>\n onSaved?: (result: ArtifactSaveResult, content: string) => void\n onSaveError?: (error: unknown) => void\n /** Every buffer change (a host keeps a draft store in sync with it). */\n onContentChange?: (content: string) => void\n /** The unsaved-changes flag, for a host that warns before a panel is closed. */\n onDirtyChange?: (dirty: boolean) => void\n /** Where the diff runs. Defaults to the main thread. */\n redlineEngine?: RedlineEngine\n /** Typing is never debounced; the diff trails the buffer by this long. Default 300ms. */\n diffDelayMs?: number\n}\n\nexport interface ArtifactEditSession {\n isEditing: boolean\n /** The saved text of the open version. */\n baseline: string\n /** The text in the editor (the baseline until the first edit). */\n content: string\n hasChanges: boolean\n isSaving: boolean\n canCompare: boolean\n compareId: string | null\n showTrackChanges: boolean\n /** Track changes between the compare target and the (debounced) buffer. */\n redline: UseRedlineResult\n startEdit: () => void\n /** Discard the buffer and any comparison; back to the rendered view. */\n cancel: () => void\n /** Save the buffer as a new version. Resolves false when it failed (the buffer is kept). */\n save: () => Promise<boolean>\n /** The editor's `onChange`; also replaces the buffer (for example after an undo-this-change). */\n change: (markdown: string) => void\n /** The editor's `onNormalized`: adopt its canonical form as the baseline so Save does not fire. */\n normalized: (markdown: string) => void\n /** Compare with the previous version (when no id is given). */\n compare: (id?: string) => void\n stopCompare: () => void\n setShowTrackChanges: (show: boolean) => void\n /** Undo one tracked change in the buffer. */\n revertRegion: (regionId: string) => Promise<void>\n}\n\n/**\n * The state machine of an artifact edit session, with no SDK and no network: the edit buffer and\n * its baseline, the dirty flag, immutable-version saves and a track-changes comparison. A host\n * supplies the saved text and a `save` function (see `useArtifactSession` for the SDK binding).\n */\nexport function useArtifactEditSession(options: ArtifactEditSessionOptions): ArtifactEditSession {\n const {\n docId,\n baseline: loadedBaseline,\n startEditing = false,\n initialContent,\n savedFromId,\n compareId: controlledCompareId,\n onCompareChange,\n compareText,\n compareLoading = false,\n compareOpensEditor = true,\n save: persist,\n onSaved,\n onSaveError,\n onContentChange,\n onDirtyChange,\n redlineEngine,\n diffDelayMs = 300,\n } = options\n\n const [isEditing, setIsEditing] = useState(false)\n const [baseline, setBaseline] = useState('')\n const [buffer, setBuffer] = useState('')\n const [isSaving, setIsSaving] = useState(false)\n const [showTrackChanges, setShowTrackChanges] = useState(false)\n const [ownCompareId, setOwnCompareId] = useState<string | null>(null)\n const controlled = controlledCompareId !== undefined\n const compareId = controlled ? controlledCompareId : ownCompareId\n\n const setCompareId = useCallback(\n (id: string | null) => {\n if (!controlled) setOwnCompareId(id)\n onCompareChange?.(id)\n },\n [controlled, onCompareChange]\n )\n\n // The latest values for callbacks that must not change identity while the user types.\n const latest = useRef({ buffer, baseline, isEditing, initialContent })\n latest.current = { buffer, baseline, isEditing, initialContent }\n\n // The baseline follows the loaded text. A draft has none and seeds itself below.\n useEffect(() => {\n if (loadedBaseline !== undefined) setBaseline(loadedBaseline)\n }, [loadedBaseline])\n\n // Another artifact: drop stale edits and comparison. A new typed document starts in the editor.\n useEffect(() => {\n if (startEditing) {\n const seed = latest.current.initialContent ?? ''\n setIsEditing(true)\n setBaseline(seed)\n setBuffer(seed)\n } else {\n setIsEditing(false)\n setBuffer('')\n }\n setCompareId(null)\n setShowTrackChanges(false)\n // Only the identity of the artifact resets the session.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [docId])\n\n const startEdit = useCallback(() => {\n setBuffer(latest.current.baseline)\n setIsEditing(true)\n }, [])\n\n const stopCompare = useCallback(() => {\n setCompareId(null)\n setShowTrackChanges(false)\n }, [setCompareId])\n\n const cancel = useCallback(() => {\n setIsEditing(false)\n setBuffer('')\n stopCompare()\n }, [stopCompare])\n\n const compare = useCallback(\n (id?: string) => {\n const target = id ?? savedFromId\n if (target) setCompareId(target)\n },\n [savedFromId, setCompareId]\n )\n\n // Comparing and editing are one surface: a comparison opens the editor with the diff showing,\n // like Word's Compare yields tracked changes you can keep typing through.\n const hasBaseline = baseline !== ''\n useEffect(() => {\n if (!compareId || !compareOpensEditor || !hasBaseline) return\n if (!latest.current.isEditing) startEdit()\n setShowTrackChanges(true)\n }, [compareId, compareOpensEditor, hasBaseline, startEdit])\n\n const save = useCallback(async (): Promise<boolean> => {\n // Persist the canonical spelling so the editor's gratuitous escapes never reach a version.\n const raw = latest.current.buffer\n const normalised = normalizeMarkdown(raw)\n const content = raw.endsWith('\\n') ? normalised : normalised.replace(/\\n$/, '')\n setIsSaving(true)\n try {\n const result = (await persist(content)) ?? {}\n setBaseline(content)\n setIsEditing(false)\n stopCompare()\n onSaved?.(result, content)\n return true\n } catch (error) {\n onSaveError?.(error)\n return false\n } finally {\n setIsSaving(false)\n }\n }, [persist, stopCompare, onSaved, onSaveError])\n\n const content = buffer || baseline\n const hasChanges = isEditing && !!buffer && buffer !== baseline\n useEffect(() => {\n onDirtyChange?.(hasChanges)\n return () => onDirtyChange?.(false)\n }, [hasChanges, onDirtyChange])\n\n const setContent = useCallback(\n (markdown: string) => {\n setBuffer(markdown)\n onContentChange?.(markdown)\n },\n [onContentChange]\n )\n const normalized = useCallback((markdown: string) => {\n setBaseline(markdown)\n setBuffer(markdown)\n }, [])\n\n // The diff trails the buffer so it runs on a settled text.\n const [diffContent, setDiffContent] = useState(content)\n useEffect(() => {\n const id = setTimeout(() => setDiffContent(content), diffDelayMs)\n return () => clearTimeout(id)\n }, [content, diffDelayMs])\n\n const redline = useRedline({\n baseText: compareId ? compareText : undefined,\n compareText: compareId ? diffContent || baseline : undefined,\n isLoading: compareLoading,\n engine: redlineEngine,\n })\n const { revertRegion: revertInRedline } = redline\n const revertRegion = useCallback(\n async (regionId: string): Promise<void> => {\n const next = await revertInRedline(regionId)\n if (next !== null) setContent(next)\n },\n [revertInRedline, setContent]\n )\n\n return useMemo(\n () => ({\n isEditing,\n baseline,\n content,\n hasChanges,\n isSaving,\n canCompare: !!savedFromId && !compareId,\n compareId,\n showTrackChanges,\n redline,\n startEdit,\n cancel,\n save,\n change: setContent,\n normalized,\n compare,\n stopCompare,\n setShowTrackChanges,\n revertRegion,\n }),\n [\n isEditing,\n baseline,\n content,\n hasChanges,\n isSaving,\n savedFromId,\n compareId,\n showTrackChanges,\n redline,\n startEdit,\n cancel,\n save,\n setContent,\n normalized,\n compare,\n stopCompare,\n revertRegion,\n ]\n )\n}\n","import { useCallback, useMemo } from 'react'\nimport { useQuery } from '@tanstack/react-query'\nimport { useArbi } from '../useArbi'\nimport { arbiQueryKeys } from '../hooks'\nimport { flattenChunks, isDocumentId, parseVersionRef } from './versions'\n\n/**\n * Where the text of a stored artifact is:\n * - `loading`: the first request is in flight (or there is no document yet);\n * - `parsing`: the backend has not finished parsing the upload (the endpoint answers 404, 422 or\n * 500, or an empty parse, for a few seconds after an upload) and the hook is retrying;\n * - `ready`: the text is here (an empty document is `ready` with `text: null`);\n * - `error`: the retries ran out, or the request was refused (401/403).\n */\nexport type ArtifactTextStatus = 'loading' | 'parsing' | 'ready' | 'error'\n\n/** How long `useArtifactText` keeps asking while a document is still being parsed. */\nexport interface ArtifactTextRetry {\n /** Retries after the first request. Default 12. */\n attempts?: number\n /** Delay before the first retry; it grows by 1.6x each time. Default 1000 ms. */\n baseDelayMs?: number\n /** Cap for one delay. Default 5000 ms (12 retries are about 50 s in all). */\n maxDelayMs?: number\n}\n\nexport const DEFAULT_ARTIFACT_TEXT_RETRY: Required<ArtifactTextRetry> = {\n attempts: 12,\n baseDelayMs: 1000,\n maxDelayMs: 5000,\n}\n\n/** Thrown inside the query when the parse has no text yet, so that it is retried like a 500. */\nclass NotParsedYetError extends Error {\n constructor() {\n super('The document has no parsed text yet')\n }\n}\n\nconst isRefusal = (error: unknown): boolean => {\n const status = error as { status?: number; response?: { status?: number } } | null\n const code = status?.status ?? status?.response?.status\n return code === 401 || code === 403\n}\n\nexport interface ArtifactText {\n /**\n * The markdown, clean: `undefined` while `loading` or `parsing`, `null` when there is none (an\n * empty document, or `error`).\n */\n text: string | null | undefined\n /** `status` is `loading` or `parsing`. */\n isLoading: boolean\n status: ArtifactTextStatus\n /** The last request error while `parsing` or after `error`. */\n error: Error | null\n /** Ask again now (after an `error`). */\n retry: () => void\n}\n\n/**\n * The saved markdown of an artifact version (its parsed chunks, joined and unescaped).\n *\n * A freshly uploaded document is parsed asynchronously, and for roughly 3 to 20 seconds the parsed\n * content endpoint answers 404, 422 or 500 (or an empty parse). The hook therefore retries with a\n * growing delay (see `ArtifactTextRetry`) and reports `status: 'parsing'` meanwhile, so a host can\n * show \"Preparing editor...\" instead of opening an editor on an empty baseline. After the last\n * retry the status is `error`; `retry()` starts a new round.\n *\n * The text is run through `unescapeMarkdown`: an artifact is always stored from markdown, and the\n * backend's parse of a `.md` escapes its punctuation.\n */\nexport function useArtifactText(\n docId: string | null | undefined,\n options?: { enabled?: boolean; retry?: ArtifactTextRetry }\n): ArtifactText {\n const arbi = useArbi()\n const { attempts, baseDelayMs, maxDelayMs } = {\n ...DEFAULT_ARTIFACT_TEXT_RETRY,\n ...options?.retry,\n }\n // A local draft (placeholder id) has no backend document: asking for its text would be a 422.\n const enabled = (options?.enabled ?? true) && isDocumentId(docId) && arbi.hasWorkspace\n const query = useQuery({\n // Under the shared `parsedDoc` key, so a document's invalidation reaches it too.\n queryKey: [...arbiQueryKeys.parsedDoc(docId ?? '', 'marker'), 'artifact-text'] as const,\n queryFn: async () => {\n // A version reference (`doc-…@v2`) reads that version; a plain id the current one.\n const { docId: plainId, version } = parseVersionRef(docId)\n const parsed = (await arbi.documents.getParsedContent(\n plainId,\n 'marker',\n version ?? undefined\n )) as {\n chunks?: { content?: string | null }[]\n }\n if (!parsed.chunks?.some((c) => c.content?.trim())) throw new NotParsedYetError()\n return flattenChunks(parsed.chunks, { unescape: true })\n },\n enabled,\n retry: (failureCount, error) => !isRefusal(error) && failureCount < attempts,\n retryDelay: (failureCount) => Math.min(baseDelayMs * 1.6 ** failureCount, maxDelayMs),\n })\n\n const { refetch } = query\n const retry = useCallback(() => void refetch(), [refetch])\n\n return useMemo<ArtifactText>(() => {\n const error = query.error\n const neverParsed = error instanceof NotParsedYetError\n let status: ArtifactTextStatus = 'loading'\n if (!enabled) status = 'loading'\n else if (query.isSuccess || (query.isError && neverParsed)) status = 'ready'\n else if (query.isError) status = 'error'\n else if (query.failureCount > 0) status = 'parsing'\n const text = !enabled\n ? undefined\n : query.isSuccess\n ? query.data\n : status === 'ready' || status === 'error'\n ? null\n : undefined\n return {\n text,\n isLoading: enabled && (status === 'loading' || status === 'parsing'),\n status,\n error: status === 'ready' ? null : (error ?? query.failureReason ?? null),\n retry,\n }\n }, [\n enabled,\n query.isSuccess,\n query.isError,\n query.data,\n query.error,\n query.failureCount,\n query.failureReason,\n retry,\n ])\n}\n","import { useCallback, useMemo, useState } from 'react'\nimport { useMutation, useQueryClient } from '@tanstack/react-query'\nimport { useArbi } from '../useArbi'\nimport { arbiQueryKeys, useDocumentVersionList } from '../hooks'\nimport { refreshDocuments, useDocuments } from '../documents/documents-data'\nimport { useArtifactText, type ArtifactTextStatus } from './useArtifactText'\nimport {\n documentVersions,\n findVersion,\n isDocumentId,\n parseVersionRef,\n toArtifactVersions,\n versionLabel,\n type ArtifactVersion,\n} from './versions'\nimport {\n useArtifactEditSession,\n type ArtifactEditSession,\n type ArtifactEditSessionOptions,\n type ArtifactSaveResult,\n} from './useArtifactEditSession'\n\n/**\n * The versions of an artifact, oldest first. `docId` is a document id or a version reference\n * (`doc-…@v2`). Every listed document carries its versions; a document that is not in the\n * workspace list asks for its version list.\n */\nexport function useArtifactVersions(\n workspaceId: string | undefined,\n docId: string | null | undefined\n): ArtifactVersion[] {\n const plainId = docId ? parseVersionRef(docId).docId : null\n const { data: documents } = useDocuments(workspaceId)\n const listed = useMemo(\n () => (documents ?? []).find((d) => d.external_id === plainId),\n [documents, plainId]\n )\n // Asked only once the list has loaded and the document is not in it.\n const { data: history } = useDocumentVersionList(plainId, {\n enabled: !!plainId && documents !== undefined && !listed,\n })\n return useMemo(\n () => (listed ? documentVersions(listed) : toArtifactVersions(plainId, history)),\n [listed, history, plainId]\n )\n}\n\nexport interface SaveArtifactVersionInput {\n /** File name of the new version (`.md` is added when missing). */\n title: string\n content: string\n /** Save as the next version of this document (it keeps its id); omit for a new document. */\n versionOf?: string | null\n /** The processing config (auto-title, parsing) to run the upload with. */\n configId?: string | null\n /** `artifact` (default) for AI-style drafts; `source` for a document the user typed. */\n wpType?: 'artifact' | 'source'\n}\n\n/**\n * Save markdown as a NEW immutable version (the next version of `versionOf`'s document, which keeps\n * its id) and refresh the workspace document list, version lists and the document's text.\n */\nexport function useSaveArtifactVersion() {\n const arbi = useArbi()\n const queryClient = useQueryClient()\n return useMutation<ArtifactSaveResult, Error, SaveArtifactVersionInput>({\n mutationFn: async ({ title, content, versionOf, configId, wpType }) => {\n // The backend classifies uploads by extension: a title without one is silently dropped.\n const fileName = /\\.md$/i.test(title) ? title : `${title}.md`\n const result = await arbi.documents.uploadFile(\n new Blob([content], { type: 'text/markdown' }),\n fileName,\n {\n wpType: wpType ?? 'artifact',\n versionOf: versionOf ? parseVersionRef(versionOf).docId : undefined,\n configExtId: configId ?? undefined,\n }\n )\n const docId = result.doc_ext_ids?.[0]\n // The document's row moves to the new version (its version lists re-read with it).\n if (docId) await refreshDocuments(queryClient, arbi, [docId])\n await queryClient.invalidateQueries({ queryKey: arbiQueryKeys.allDocumentVersions() })\n // The document's current text is the new version's now.\n if (docId)\n await queryClient.invalidateQueries({ queryKey: arbiQueryKeys.parsedDocument(docId) })\n return { docId, version: result.version ?? undefined }\n },\n })\n}\n\nexport interface ArtifactSessionOptions extends Pick<\n ArtifactEditSessionOptions,\n | 'onSaved'\n | 'onSaveError'\n | 'onCompareChange'\n | 'compareId'\n | 'compareOpensEditor'\n | 'redlineEngine'\n | 'onDirtyChange'\n> {\n workspaceId: string | undefined\n /**\n * The open document (its current version), a version reference `doc-…@v{n}`, or the placeholder\n * id of an unsaved local draft (anything that is not a backend document id, i.e. does not start\n * with `doc`).\n */\n docId: string | null | undefined\n /**\n * The text of a local draft (nothing is fetched for it). The FIRST save uploads it as a real\n * document with no parent and the session switches to the new id; later saves chain from it.\n */\n draftContent?: string\n /** Name for saved versions. Defaults to the open version's file name, else \"Untitled\". */\n title?: string\n /** Processing config for saved versions. */\n configId?: string | null\n}\n\n/** Everything `ArtifactEditor` (`@arbidocs/blocks/artifacts`) takes from a session, flat. */\nexport interface ArtifactSessionView {\n content: string\n isEditing: boolean\n hasChanges: boolean\n isSaving: boolean\n canCompare: boolean\n comparing: boolean\n compareId: string | null\n showTrackChanges: boolean\n versions: ArtifactVersion[]\n currentVersionId: string\n onStartEdit: () => void\n onCancel: () => void\n onSave: () => void\n onCompare: () => void\n onStopCompare: () => void\n onToggleTrackChanges: () => void\n /**\n * Whether the text is ready. `ArtifactEditor` renders \"Preparing editor...\" while it is\n * `loading` or `parsing` (a fresh upload is parsed for a few seconds) and an error with Retry.\n */\n status: ArtifactTextStatus\n /** Ask for the text again after an `error`. */\n onRetry: () => void\n}\n\nexport interface ArtifactSession extends ArtifactEditSession {\n /** The open document: the new real id once a local draft has been saved. */\n docId: string | null | undefined\n /** The open version is an unsaved local draft (no backend document yet). */\n isLocal: boolean\n isLoading: boolean\n /** See `ArtifactTextStatus`. */\n status: ArtifactTextStatus\n versions: ArtifactVersion[]\n /** \"v2\" for any version (reference) of the document. */\n labelOf: (id: string | null | undefined) => string | null\n /** Props for `<ArtifactEditor>`. */\n view: ArtifactSessionView\n /** Props for `<ArtifactMarkdownEditor>` (`@arbidocs/blocks/artifacts/editor`). */\n editorProps: {\n markdown: string\n onChange: (markdown: string) => void\n onNormalized: (markdown: string) => void\n }\n /** Props for `<ArtifactRedlineView>`. */\n redlineProps: {\n state: ArtifactEditSession['redline']['state']\n onRevert: (regionId: string) => void\n }\n}\n\n/**\n * Edit an artifact through the SDK: load it, edit it, save it as a new version, list its versions\n * and compare any two with track changes. Needs an `<ArbiHost>` with an `arbi` and a selected workspace.\n *\n * ```tsx\n * const session = useArtifactSession({ workspaceId, docId, onSaved: ({ docId }) => docId && open(docId) })\n * <ArtifactEditor {...session.view} onOpenVersion={open}\n * editor={<ArtifactMarkdownEditor {...session.editorProps} />}\n * redline={<ArtifactRedlineView {...session.redlineProps} />} />\n * ```\n */\nexport function useArtifactSession(options: ArtifactSessionOptions): ArtifactSession {\n const {\n workspaceId,\n docId: hostDocId,\n draftContent,\n title,\n configId,\n compareId,\n onCompareChange,\n onSaved,\n ...passthrough\n } = options\n // A saved local draft becomes this real document; another host docId resets it.\n const [saved, setSaved] = useState<{\n forId: string | null | undefined\n id: string\n text: string\n } | null>(null)\n const adopted = saved && saved.forId === hostDocId ? saved : null\n const docId = adopted?.id ?? hostDocId\n const isLocal = !isDocumentId(docId)\n\n // Owned here (not in the edit session) because the compare target's text must be loaded first.\n const [ownCompareId, setOwnCompareId] = useState<string | null>(null)\n const activeCompareId = compareId === undefined ? ownCompareId : compareId\n const changeCompare = useCallback(\n (id: string | null) => {\n setOwnCompareId(id)\n onCompareChange?.(id)\n },\n [onCompareChange]\n )\n\n const { text, isLoading, status, retry } = useArtifactText(docId)\n const versions = useArtifactVersions(workspaceId, isLocal ? null : docId)\n const { text: compareText, isLoading: compareLoading } = useArtifactText(activeCompareId, {\n enabled: !!activeCompareId,\n })\n const saveVersion = useSaveArtifactVersion()\n const current = findVersion(versions, docId)\n\n const save = useCallback(\n async (content: string) => {\n const result = await saveVersion.mutateAsync({\n title: title ?? current?.name ?? 'Untitled',\n content,\n versionOf: isLocal ? null : docId,\n configId,\n })\n const latest = versions.reduce((n, v) => Math.max(n, v.number), 0)\n return { ...result, version: result.version ?? (isLocal ? 1 : latest + 1) }\n },\n [saveVersion, title, current?.name, isLocal, docId, configId, versions]\n )\n\n const handleSaved = useCallback(\n (result: ArtifactSaveResult, content: string) => {\n // A host that keeps passing the local draft's placeholder id: follow every save to the real\n // document it created (the first save creates version 1, later ones chain from it).\n if (!isDocumentId(hostDocId) && result.docId)\n setSaved({ forId: hostDocId, id: result.docId, text: content })\n onSaved?.(result, content)\n },\n [hostDocId, onSaved]\n )\n\n const session = useArtifactEditSession({\n ...passthrough,\n docId,\n // A local draft has only its own text; a just-saved one shows what was saved until the parsed\n // text of the new document arrives.\n baseline: text ?? (isLocal ? draftContent : adopted?.text),\n savedFromId: current?.savedFromId,\n compareId: activeCompareId,\n onCompareChange: changeCompare,\n compareText,\n compareLoading,\n save,\n onSaved: handleSaved,\n })\n\n const { redline } = session\n return useMemo<ArtifactSession>(\n () => ({\n ...session,\n docId,\n isLocal,\n isLoading,\n status,\n versions,\n labelOf: (id) => versionLabel(versions, id),\n view: {\n content: session.baseline,\n isEditing: session.isEditing,\n hasChanges: session.hasChanges,\n isSaving: session.isSaving,\n canCompare: session.canCompare,\n comparing: !!session.compareId,\n compareId: session.compareId,\n showTrackChanges: session.showTrackChanges,\n versions,\n currentVersionId: current?.id ?? docId ?? '',\n onStartEdit: session.startEdit,\n onCancel: session.cancel,\n onSave: () => void session.save(),\n onCompare: () => session.compare(),\n onStopCompare: session.stopCompare,\n onToggleTrackChanges: () => session.setShowTrackChanges(!session.showTrackChanges),\n // A local draft has its text already: it is ready.\n status: isLocal ? 'ready' : status,\n onRetry: retry,\n },\n editorProps: {\n markdown: session.content,\n onChange: session.change,\n onNormalized: session.normalized,\n },\n redlineProps: {\n state: redline.state,\n onRevert: (regionId) => void session.revertRegion(regionId),\n },\n }),\n [session, redline, isLoading, status, retry, isLocal, versions, docId]\n )\n}\n","import { useQuery } from '@tanstack/react-query'\nimport { useArbi } from '../useArbi'\nimport { arbiQueryKeys } from '../query-keys'\n\n/**\n * The stored source of a live artifact (the `.html` / `.jsx` file itself; the parsed text strips\n * it). `undefined` until it has loaded.\n */\nexport function useArtifactSource(documentId: string | null | undefined, enabled = true) {\n const arbi = useArbi()\n return useQuery({\n queryKey: arbiQueryKeys.artifactSource(documentId ?? ''),\n queryFn: async () => (await arbi.documents.download(documentId as string)).text(),\n enabled: enabled && !!documentId && arbi.isLoggedIn,\n staleTime: 5 * 60 * 1000,\n })\n}\n","import type { ComponentPropsWithoutRef, FC, ReactNode } from 'react'\nimport { MarkdownRenderer } from '../ai/MarkdownRenderer'\n\n/** The `<del>` / `<ins>` renderers `ArtifactRedlineView` hands to a markdown renderer. */\nexport interface ArtifactRedlineComponents {\n del: FC<ComponentPropsWithoutRef<'del'> & { 'data-cid'?: string }>\n ins: FC<ComponentPropsWithoutRef<'ins'> & { 'data-cid'?: string }>\n}\n\n/**\n * The kit's markdown renderer as the `renderMarkdown` slot of `ArtifactEditor` and\n * `ArtifactRedlineView`: full markdown (tables, lists, code) plus the raw `<del>` / `<ins>` tags\n * of a redline.\n */\nexport function renderArtifactMarkdown(\n markdown: string,\n components?: ArtifactRedlineComponents\n): ReactNode {\n return <MarkdownRenderer components={components}>{markdown}</MarkdownRenderer>\n}\n","import { useCallback, useEffect, useRef, useState } from 'react'\nimport type { ArtifactEvent } from '../ai/engine'\nimport { cleanDraftWithReport, type DraftCleanReport } from '../ai/stored-markdown'\nimport { useSaveArtifactVersion } from './useArtifactSession'\nimport { isDocumentId } from './versions'\nimport { useArtifactText, type ArtifactTextRetry } from './useArtifactText'\n\n/**\n * Where a drafted artifact is in its STREAM -> PERSIST -> EDIT life:\n * - `streaming`: the agent is still working (or has produced nothing final): show the live canvas;\n * - `persisting`: the finished draft is being stored as version 1 of the artifact;\n * - `parsing`: stored; the backend is still parsing it, so there is no editable text yet;\n * - `ready`: the text is readable: mount the editor on `docId`;\n * - `error`: storing failed or the parse never produced text (`retry()` starts over).\n */\nexport type StreamedArtifactPhase = 'streaming' | 'persisting' | 'parsing' | 'ready' | 'error'\n\nexport interface UseStreamedArtifactOptions {\n /** The artifacts of the agent run (`useConnectedAiTask().artifacts`). */\n artifacts: ArtifactEvent[]\n workspaceId: string | undefined\n /**\n * The run has stopped: not busy and not failed. Nothing is stored before this, so a draft that\n * is still streaming is never persisted half-written.\n */\n settled: boolean\n /**\n * Make the stored markdown from the streamed content. Default `cleanDraftWithReport`, which is\n * guarded: it stores the raw streamed text (or links-only text) rather than a cleanup that would\n * lose content (see `DraftCleanReport`).\n */\n clean?: (streamed: string) => string\n /** Called once per stored draft with what the cleanup did (`stage` is not `cleaned` if guarded). */\n onCleaned?: (report: DraftCleanReport) => void\n /** Processing config for the stored version. */\n configId?: string | null\n /** How long to wait for the parse (see `useArtifactText`). */\n parseRetry?: ArtifactTextRetry\n}\n\nexport interface StreamedArtifact {\n phase: StreamedArtifactPhase\n /** The finished streamed artifact (its `title` names the stored versions). */\n artifact: ArtifactEvent | undefined\n /** The backend document to edit, once there is one (while `parsing` and `ready`). */\n docId: string | undefined\n error: Error | null\n /** What the cleanup did to the stored draft (`null` until it was stored, or with a custom `clean`). */\n cleaned: DraftCleanReport | null\n /** After an `error`: store again or ask for the parse again. */\n retry: () => void\n}\n\n/**\n * The lifecycle of an agent-drafted artifact, so a host passes the stream and renders the phase.\n *\n * While the agent streams, the draft lives only on the canvas. Once the run is `settled` and the\n * draft is complete, a draft that is not yet a backend document is cleaned (`cleanDraftMarkdown`)\n * and stored ONCE as the artifact's first version (`wpType: 'artifact'`); a draft that already is a\n * backend document passes through. The hook then waits for the parse (`useArtifactText`, which\n * retries) and reports `ready` with the `docId` to open in an editor.\n *\n * Needs an `<ArbiHost>` with an `arbi` and a workspace. `@arbidocs/blocks/connect` has the whole thing as a\n * component (`StreamedArtifact`).\n */\nexport function useStreamedArtifact({\n artifacts,\n workspaceId,\n settled,\n clean,\n onCleaned,\n configId,\n parseRetry,\n}: UseStreamedArtifactOptions): StreamedArtifact {\n const artifact = artifacts.find((a) => a.is_complete)\n const { mutateAsync } = useSaveArtifactVersion()\n const [saved, setSaved] = useState<{ content: string; docId: string } | null>(null)\n const [saveError, setSaveError] = useState<Error | null>(null)\n const [attempt, setAttempt] = useState(0)\n const [cleaned, setCleaned] = useState<DraftCleanReport | null>(null)\n const started = useRef<string | null>(null)\n\n const content = artifact?.content ?? ''\n const title = artifact?.title ?? 'Draft'\n const passThrough = !!artifact && isDocumentId(artifact.doc_ext_id)\n const needsSave = settled && !!artifact && !passThrough && !!content.trim() && !!workspaceId\n\n useEffect(() => {\n const key = `${attempt}:${content}`\n if (!needsSave || started.current === key) return\n started.current = key\n setSaveError(null)\n let stored: string\n if (clean) stored = clean(content)\n else {\n const report = cleanDraftWithReport(content)\n stored = report.text\n setCleaned(report)\n onCleaned?.(report)\n if (report.guarded) {\n console.warn(\n `[useStreamedArtifact] the draft cleanup would have lost content; stored the ${report.stage} text instead (kept ${Math.round(report.ratio * 100)}%, changed by: ${report.changedBy.join(', ') || 'nothing'})`\n )\n }\n }\n mutateAsync({\n title,\n content: stored,\n configId,\n wpType: 'artifact',\n })\n .then((r) => {\n if (r.docId) setSaved({ content, docId: r.docId })\n else setSaveError(new Error('The draft was stored but the server returned no document id'))\n })\n .catch((e: unknown) => setSaveError(e instanceof Error ? e : new Error(String(e))))\n // `clean` and `onCleaned` are deliberately not dependencies: a new function identity must not\n // store twice.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [needsSave, content, title, workspaceId, configId, attempt, mutateAsync])\n\n const docId = !artifact\n ? undefined\n : passThrough\n ? artifact.doc_ext_id\n : saved?.content === content\n ? saved.docId\n : undefined\n\n const text = useArtifactText(docId, { enabled: settled, retry: parseRetry })\n const { retry: retryText } = text\n\n const retry = useCallback(() => {\n if (saveError) setAttempt((n) => n + 1)\n else retryText()\n }, [saveError, retryText])\n\n let phase: StreamedArtifactPhase\n if (!settled || !artifact) phase = 'streaming'\n else if (saveError) phase = 'error'\n else if (!docId) phase = needsSave ? 'persisting' : 'streaming'\n else if (text.status === 'ready' && text.text) phase = 'ready'\n else if (text.status === 'error' || text.status === 'ready') phase = 'error'\n else phase = 'parsing'\n\n const error =\n phase !== 'error'\n ? null\n : (saveError ?? text.error ?? new Error('The document has no text to edit'))\n return { phase, artifact, docId, error, cleaned, retry }\n}\n","/**\n * Which live renderer, if any, an artifact maps to. Kept free of heavy imports (no compiler) so the\n * document viewer can use it without pulling the preview code into its bundle.\n *\n * The backend persists an artifact's render mode in the saved file's extension: .html / .jsx are live\n * artifacts; document drafts are stored as .docx and open in the Word editor.\n */\nexport type ArtifactPreviewKind = 'html' | 'react'\n\nexport function artifactKindFromFileName(fileName?: string | null): ArtifactPreviewKind | null {\n const ext = (fileName ?? '').toLowerCase().match(/\\.[a-z0-9]+$/)?.[0]\n if (ext === '.html' || ext === '.htm') return 'html'\n if (ext === '.jsx') return 'react'\n return null\n}\n\nexport function artifactKindFromMode(mode?: string | null): ArtifactPreviewKind | null {\n return mode === 'html' || mode === 'react' ? mode : null\n}\n\n/**\n * The viewer tab an artifact opens on: a generated image on its media view ('pdf'), an html/react\n * artifact on its rendered Preview, a slide deck ARBI saved (a .pptx) in the slides editor, and a\n * document draft (stored as .docx) in the Word editor.\n */\nexport function viewerTabForArtifactMode(\n mode?: string | null\n): 'pdf' | 'preview' | 'edit' | 'slides' {\n if (mode === 'image') return 'pdf'\n if (mode === 'presentation') return 'slides'\n return artifactKindFromMode(mode) ? 'preview' : 'edit'\n}\n"]}
1
+ {"version":3,"sources":["../../src/artifacts/redline-engine.ts","../../src/artifacts/useRedline.ts","../../src/artifacts/useArtifactEditSession.ts","../../src/artifacts/useArtifactText.ts","../../src/artifacts/useArtifactSession.ts","../../src/artifacts/useArtifactSource.ts","../../src/artifacts/renderArtifactMarkdown.tsx","../../src/artifacts/useStreamedArtifact.ts","../../src/artifacts/artifact-kind.ts"],"names":["buildHybridRedlineMarkdown","buildHybridRedlineWithRegions","revertRedlineRegion","useState","useRef","useEffect","normalizeMarkdown","useCallback","useMemo","savedFromId","content","useArbi","isDocumentId","useQuery","arbiQueryKeys","parseVersionRef","flattenChunks","useDocuments","useDocumentVersionList","documentVersions","toArtifactVersions","useQueryClient","useMutation","refreshDocuments","findVersion","registerDocumentTurnFlush","versionLabel","jsx","MarkdownRenderer","cleanDraftWithReport"],"mappings":";;;;;;;;;;;;;;;;;AAsBO,IAAM,uBAAA,GAAyC;AAAA,EACpD,YAAA,EAAcA,4CAAA;AAAA,EACd,uBAAA,EAAyBC,+CAAA;AAAA,EACzB,YAAA,EAAcC;AAChB;ACQO,SAAS,UAAA,CAAW;AAAA,EACzB,QAAA;AAAA,EACA,WAAA;AAAA,EACA,SAAA,GAAY,KAAA;AAAA,EACZ,MAAA,GAAS;AACX,CAAA,EAAwC;AACtC,EAAA,MAAM,CAAC,OAAO,QAAQ,CAAA,GAAIC,eAAuB,EAAE,IAAA,EAAM,QAAQ,CAAA;AACjE,EAAA,MAAM,YAAA,GAAeC,aAAO,CAAC,CAAA;AAE7B,EAAAC,eAAA,CAAU,MAAM;AACd,IAAA,IAAI,QAAA,KAAa,MAAA,IAAa,WAAA,KAAgB,MAAA,EAAW;AACvD,MAAA,QAAA,CAAS,EAAE,IAAA,EAAM,MAAA,EAAQ,CAAA;AACzB,MAAA;AAAA,IACF;AACA,IAAA,IAAI,SAAA,EAAW;AACb,MAAA,QAAA,CAAS,EAAE,IAAA,EAAM,WAAA,EAAa,CAAA;AAC9B,MAAA;AAAA,IACF;AACA,IAAA,IAAI,QAAA,KAAa,IAAA,IAAQ,WAAA,KAAgB,IAAA,EAAM;AAC7C,MAAA,QAAA,CAAS,EAAE,IAAA,EAAM,aAAA,EAAe,CAAA;AAChC,MAAA;AAAA,IACF;AACA,IAAA,IACE,aAAa,WAAA,IACbC,mCAAA,CAAkB,QAAQ,CAAA,KAAMA,mCAAA,CAAkB,WAAW,CAAA,EAC7D;AACA,MAAA,QAAA,CAAS,EAAE,IAAA,EAAM,WAAA,EA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J,UAAU,OAAA,GACN,IAAA,GACC,aAAa,IAAA,CAAK,KAAA,IAAS,IAAI,KAAA,CAAM,kCAAkC,CAAA;AAC9E,EAAA,OAAO,EAAE,KAAA,EAAO,QAAA,EAAU,KAAA,EAAO,KAAA,EAAO,SAAS,KAAA,EAAM;AACzD;;;AC7IO,SAAS,yBAAyB,QAAA,EAAsD;AAC7F,EAAA,MAAM,GAAA,GAAA,CAAO,YAAY,EAAA,EAAI,WAAA,GAAc,KAAA,CAAM,cAAc,IAAI,CAAC,CAAA;AACpE,EAAA,IAAI,GAAA,KAAQ,OAAA,IAAW,GAAA,KAAQ,MAAA,EAAQ,OAAO,MAAA;AAC9C,EAAA,IAAI,GAAA,KAAQ,QAAQ,OAAO,OAAA;AAC3B,EAAA,OAAO,IAAA;AACT;AAEO,SAAS,qBAAqB,IAAA,EAAkD;AACrF,EAAA,OAAO,IAAA,KAAS,MAAA,IAAU,IAAA,KAAS,OAAA,GAAU,IAAA,GAAO,IAAA;AACtD;AAOO,SAAS,yBACd,IAAA,EACuC;AACvC,EAAA,IAAI,IAAA,KAAS,SAAS,OAAO,KAAA;AAC7B,EAAA,IAAI,IAAA,KAAS,gBAAgB,OAAO,QAAA;AACpC,EAAA,OAAO,oBAAA,CAAqB,IAAI,CAAA,GAAI,SAAA,GAAY,MAAA;AAClD","file":"index.cjs","sourcesContent":["import {\n buildHybridRedlineMarkdown,\n buildHybridRedlineWithRegions,\n revertRedlineRegion,\n type RedlineWithRegions,\n} from './redline-diff'\n\n/**\n * Where a redline is computed. The default runs on the calling thread; a host with large\n * documents passes an engine backed by a Web Worker (for example a Comlink proxy exposing the same\n * three functions) so typing never waits on a diff.\n */\nexport interface RedlineEngine {\n buildRedline(baseText: string, compareText: string): string | Promise<string>\n buildRedlineWithRegions(\n baseText: string,\n compareText: string\n ): RedlineWithRegions | Promise<RedlineWithRegions>\n revertRegion(baseText: string, compareText: string, regionId: string): string | Promise<string>\n}\n\n/** Main-thread engine: the pure functions of `redline-diff`. */\nexport const mainThreadRedlineEngine: RedlineEngine = {\n buildRedline: buildHybridRedlineMarkdown,\n buildRedlineWithRegions: buildHybridRedlineWithRegions,\n revertRegion: revertRedlineRegion,\n}\n","import { useCallback, useEffect, useMemo, useRef, useState } from 'react'\nimport type { RedlineRegion } from './redline-diff'\nimport { normalizeMarkdown } from './normalize-markdown'\nimport { mainThreadRedlineEngine, type RedlineEngine } from './redline-engine'\n\n/** What a redline view can show. Mirrors the status parts of `@arbidocs/blocks/artifacts`. */\nexport type RedlineState =\n | { kind: 'idle' }\n | { kind: 'computing' }\n | { kind: 'ready'; markdown: string; regions: RedlineRegion[] }\n | { kind: 'identical' }\n | { kind: 'unavailable' }\n\nexport interface UseRedlineOptions {\n /** The older side. `null` means the text could not be loaded; `undefined` means not chosen. */\n baseText: string | null | undefined\n /** The newer side (a loaded version, or the live edit buffer). */\n compareText: string | null | undefined\n /** Either text is still being fetched. */\n isLoading?: boolean\n /** Defaults to the main thread; pass a worker-backed engine for big documents. */\n engine?: RedlineEngine\n}\n\nexport interface UseRedlineResult {\n state: RedlineState\n /** Undo one change: resolves with the compare text rebuilt without it (null if nothing changed). */\n revertRegion: (regionId: string) => Promise<string | null>\n}\n\n/**\n * Track-changes between two texts. Stale results are dropped when the inputs change mid-compute,\n * and a failed diff reports `unavailable` instead of throwing into the render.\n */\nexport function useRedline({\n baseText,\n compareText,\n isLoading = false,\n engine = mainThreadRedlineEngine,\n}: UseRedlineOptions): UseRedlineResult {\n const [state, setState] = useState<RedlineState>({ kind: 'idle' })\n const requestIdRef = useRef(0)\n\n useEffect(() => {\n if (baseText === undefined || compareText === undefined) {\n setState({ kind: 'idle' })\n return\n }\n if (isLoading) {\n setState({ kind: 'computing' })\n return\n }\n if (baseText === null || compareText === null) {\n setState({ kind: 'unavailable' })\n return\n }\n if (\n baseText === compareText ||\n normalizeMarkdown(baseText) === normalizeMarkdown(compareText)\n ) {\n setState({ kind: 'identical' })\n return\n }\n\n const id = ++requestIdRef.current\n let cancelled = false\n setState({ kind: 'computing' })\n // Every change carries a stable id, so a view can offer per-change undo.\n Promise.resolve(engine.buildRedlineWithRegions(baseText, compareText))\n .then((result) => {\n if (cancelled || id !== requestIdRef.current) return\n setState({ kind: 'ready', markdown: result.markdown, regions: result.regions })\n })\n .catch(() => {\n if (cancelled || id !== requestIdRef.current) return\n setState({ kind: 'unavailable' })\n })\n return () => {\n cancelled = true\n }\n }, [baseText, compareText, isLoading, engine])\n\n // The latest texts, so `revertRegion` stays referentially stable while the user types.\n const latest = useRef({ baseText, compareText })\n useEffect(() => {\n latest.current = { baseText, compareText }\n }, [baseText, compareText])\n\n const revertRegion = useCallback(\n async (regionId: string): Promise<string | null> => {\n const { baseText: base, compareText: compare } = latest.current\n if (!base || !compare) return null\n try {\n const next = await engine.revertRegion(base, compare, regionId)\n return next !== normalizeMarkdown(compare) ? next : null\n } catch {\n return null\n }\n },\n [engine]\n )\n\n return useMemo(() => ({ state, revertRegion }), [state, revertRegion])\n}\n","import { useCallback, useEffect, useMemo, useRef, useState } from 'react'\nimport { normalizeMarkdown } from './normalize-markdown'\nimport { useRedline, type UseRedlineResult } from './useRedline'\nimport type { RedlineEngine } from './redline-engine'\n\n/** What a save reports back. `docId` is the new version when the backend already knows it. */\nexport interface ArtifactSaveResult {\n docId?: string\n /** 1-based number of the version that was just saved (1 for the first save of a draft). */\n version?: number\n}\n\nexport interface ArtifactEditSessionOptions {\n /** The artifact being edited. Changing it discards the buffer and any comparison. */\n docId: string | null | undefined\n /**\n * The saved text of the open version. `undefined` while it loads; whenever it changes the\n * baseline follows it (a version arriving from the realtime channel, a finished stream).\n */\n baseline: string | undefined\n /**\n * A new document the user types from scratch: open straight in the editor, seeded with\n * `initialContent`, instead of showing a read-only view. Read when `docId` changes.\n */\n startEditing?: boolean\n initialContent?: string\n /** The version this one was saved from. Offering \"compare with previous\" needs it. */\n savedFromId?: string | null\n /** Controlled comparison target. Omit for an uncontrolled one the session owns. */\n compareId?: string | null\n onCompareChange?: (id: string | null) => void\n /** The saved text of the compare target (the older side of the diff). */\n compareText?: string | null\n compareLoading?: boolean\n /** Starting a comparison opens the editor with track changes on (default true). */\n compareOpensEditor?: boolean\n /** Persist the buffer as a NEW version. Rejecting keeps the buffer so nothing is lost. */\n save: (content: string) => Promise<ArtifactSaveResult | void>\n onSaved?: (result: ArtifactSaveResult, content: string) => void\n onSaveError?: (error: unknown) => void\n /** Every buffer change (a host keeps a draft store in sync with it). */\n onContentChange?: (content: string) => void\n /** The unsaved-changes flag, for a host that warns before a panel is closed. */\n onDirtyChange?: (dirty: boolean) => void\n /** Where the diff runs. Defaults to the main thread. */\n redlineEngine?: RedlineEngine\n /** Typing is never debounced; the diff trails the buffer by this long. Default 300ms. */\n diffDelayMs?: number\n}\n\nexport interface ArtifactEditSession {\n isEditing: boolean\n /** The saved text of the open version. */\n baseline: string\n /** The text in the editor (the baseline until the first edit). */\n content: string\n hasChanges: boolean\n isSaving: boolean\n canCompare: boolean\n compareId: string | null\n showTrackChanges: boolean\n /** Track changes between the compare target and the (debounced) buffer. */\n redline: UseRedlineResult\n startEdit: () => void\n /** Discard the buffer and any comparison; back to the rendered view. */\n cancel: () => void\n /** Save the buffer as a new version. Resolves false when it failed (the buffer is kept). */\n save: () => Promise<boolean>\n /** The editor's `onChange`; also replaces the buffer (for example after an undo-this-change). */\n change: (markdown: string) => void\n /** The editor's `onNormalized`: adopt its canonical form as the baseline so Save does not fire. */\n normalized: (markdown: string) => void\n /** Compare with the previous version (when no id is given). */\n compare: (id?: string) => void\n stopCompare: () => void\n setShowTrackChanges: (show: boolean) => void\n /** Undo one tracked change in the buffer. */\n revertRegion: (regionId: string) => Promise<void>\n}\n\n/**\n * The state machine of an artifact edit session, with no SDK and no network: the edit buffer and\n * its baseline, the dirty flag, immutable-version saves and a track-changes comparison. A host\n * supplies the saved text and a `save` function (see `useArtifactSession` for the SDK binding).\n */\nexport function useArtifactEditSession(options: ArtifactEditSessionOptions): ArtifactEditSession {\n const {\n docId,\n baseline: loadedBaseline,\n startEditing = false,\n initialContent,\n savedFromId,\n compareId: controlledCompareId,\n onCompareChange,\n compareText,\n compareLoading = false,\n compareOpensEditor = true,\n save: persist,\n onSaved,\n onSaveError,\n onContentChange,\n onDirtyChange,\n redlineEngine,\n diffDelayMs = 300,\n } = options\n\n const [isEditing, setIsEditing] = useState(false)\n const [baseline, setBaseline] = useState('')\n const [buffer, setBuffer] = useState('')\n const [isSaving, setIsSaving] = useState(false)\n const [showTrackChanges, setShowTrackChanges] = useState(false)\n const [ownCompareId, setOwnCompareId] = useState<string | null>(null)\n const controlled = controlledCompareId !== undefined\n const compareId = controlled ? controlledCompareId : ownCompareId\n\n const setCompareId = useCallback(\n (id: string | null) => {\n if (!controlled) setOwnCompareId(id)\n onCompareChange?.(id)\n },\n [controlled, onCompareChange]\n )\n\n // The latest values for callbacks that must not change identity while the user types.\n const latest = useRef({ buffer, baseline, isEditing, initialContent })\n latest.current = { buffer, baseline, isEditing, initialContent }\n\n // The baseline follows the loaded text. A draft has none and seeds itself below.\n useEffect(() => {\n if (loadedBaseline !== undefined) setBaseline(loadedBaseline)\n }, [loadedBaseline])\n\n // Another artifact: drop stale edits and comparison. A new typed document starts in the editor.\n useEffect(() => {\n if (startEditing) {\n const seed = latest.current.initialContent ?? ''\n setIsEditing(true)\n setBaseline(seed)\n setBuffer(seed)\n } else {\n setIsEditing(false)\n setBuffer('')\n }\n setCompareId(null)\n setShowTrackChanges(false)\n // Only the identity of the artifact resets the session.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [docId])\n\n const startEdit = useCallback(() => {\n setBuffer(latest.current.baseline)\n setIsEditing(true)\n }, [])\n\n const stopCompare = useCallback(() => {\n setCompareId(null)\n setShowTrackChanges(false)\n }, [setCompareId])\n\n const cancel = useCallback(() => {\n setIsEditing(false)\n setBuffer('')\n stopCompare()\n }, [stopCompare])\n\n const compare = useCallback(\n (id?: string) => {\n const target = id ?? savedFromId\n if (target) setCompareId(target)\n },\n [savedFromId, setCompareId]\n )\n\n // Comparing and editing are one surface: a comparison opens the editor with the diff showing,\n // like Word's Compare yields tracked changes you can keep typing through.\n const hasBaseline = baseline !== ''\n useEffect(() => {\n if (!compareId || !compareOpensEditor || !hasBaseline) return\n if (!latest.current.isEditing) startEdit()\n setShowTrackChanges(true)\n }, [compareId, compareOpensEditor, hasBaseline, startEdit])\n\n const save = useCallback(async (): Promise<boolean> => {\n // Persist the canonical spelling so the editor's gratuitous escapes never reach a version.\n const raw = latest.current.buffer\n const normalised = normalizeMarkdown(raw)\n const content = raw.endsWith('\\n') ? normalised : normalised.replace(/\\n$/, '')\n setIsSaving(true)\n try {\n const result = (await persist(content)) ?? {}\n setBaseline(content)\n setIsEditing(false)\n stopCompare()\n onSaved?.(result, content)\n return true\n } catch (error) {\n onSaveError?.(error)\n return false\n } finally {\n setIsSaving(false)\n }\n }, [persist, stopCompare, onSaved, onSaveError])\n\n const content = buffer || baseline\n const hasChanges = isEditing && !!buffer && buffer !== baseline\n useEffect(() => {\n onDirtyChange?.(hasChanges)\n return () => onDirtyChange?.(false)\n }, [hasChanges, onDirtyChange])\n\n const setContent = useCallback(\n (markdown: string) => {\n setBuffer(markdown)\n onContentChange?.(markdown)\n },\n [onContentChange]\n )\n const normalized = useCallback((markdown: string) => {\n setBaseline(markdown)\n setBuffer(markdown)\n }, [])\n\n // The diff trails the buffer so it runs on a settled text.\n const [diffContent, setDiffContent] = useState(content)\n useEffect(() => {\n const id = setTimeout(() => setDiffContent(content), diffDelayMs)\n return () => clearTimeout(id)\n }, [content, diffDelayMs])\n\n const redline = useRedline({\n baseText: compareId ? compareText : undefined,\n compareText: compareId ? diffContent || baseline : undefined,\n isLoading: compareLoading,\n engine: redlineEngine,\n })\n const { revertRegion: revertInRedline } = redline\n const revertRegion = useCallback(\n async (regionId: string): Promise<void> => {\n const next = await revertInRedline(regionId)\n if (next !== null) setContent(next)\n },\n [revertInRedline, setContent]\n )\n\n return useMemo(\n () => ({\n isEditing,\n baseline,\n content,\n hasChanges,\n isSaving,\n canCompare: !!savedFromId && !compareId,\n compareId,\n showTrackChanges,\n redline,\n startEdit,\n cancel,\n save,\n change: setContent,\n normalized,\n compare,\n stopCompare,\n setShowTrackChanges,\n revertRegion,\n }),\n [\n isEditing,\n baseline,\n content,\n hasChanges,\n isSaving,\n savedFromId,\n compareId,\n showTrackChanges,\n redline,\n startEdit,\n cancel,\n save,\n setContent,\n normalized,\n compare,\n stopCompare,\n revertRegion,\n ]\n )\n}\n","import { useCallback, useMemo } from 'react'\nimport { useQuery } from '@tanstack/react-query'\nimport { useArbi } from '../useArbi'\nimport { arbiQueryKeys } from '../hooks'\nimport { flattenChunks, isDocumentId, parseVersionRef } from './versions'\n\n/**\n * Where the text of a stored artifact is:\n * - `loading`: the first request is in flight (or there is no document yet);\n * - `parsing`: the backend has not finished parsing the upload (the endpoint answers 404, 422 or\n * 500, or an empty parse, for a few seconds after an upload) and the hook is retrying;\n * - `ready`: the text is here (an empty document is `ready` with `text: null`);\n * - `error`: the retries ran out, or the request was refused (401/403).\n */\nexport type ArtifactTextStatus = 'loading' | 'parsing' | 'ready' | 'error'\n\n/** How long `useArtifactText` keeps asking while a document is still being parsed. */\nexport interface ArtifactTextRetry {\n /** Retries after the first request. Default 12. */\n attempts?: number\n /** Delay before the first retry; it grows by 1.6x each time. Default 1000 ms. */\n baseDelayMs?: number\n /** Cap for one delay. Default 5000 ms (12 retries are about 50 s in all). */\n maxDelayMs?: number\n}\n\nexport const DEFAULT_ARTIFACT_TEXT_RETRY: Required<ArtifactTextRetry> = {\n attempts: 12,\n baseDelayMs: 1000,\n maxDelayMs: 5000,\n}\n\n/** Thrown inside the query when the parse has no text yet, so that it is retried like a 500. */\nclass NotParsedYetError extends Error {\n constructor() {\n super('The document has no parsed text yet')\n }\n}\n\nconst isRefusal = (error: unknown): boolean => {\n const status = error as { status?: number; response?: { status?: number } } | null\n const code = status?.status ?? status?.response?.status\n return code === 401 || code === 403\n}\n\nexport interface ArtifactText {\n /**\n * The markdown, clean: `undefined` while `loading` or `parsing`, `null` when there is none (an\n * empty document, or `error`).\n */\n text: string | null | undefined\n /** `status` is `loading` or `parsing`. */\n isLoading: boolean\n status: ArtifactTextStatus\n /** The last request error while `parsing` or after `error`. */\n error: Error | null\n /** Ask again now (after an `error`). */\n retry: () => void\n}\n\n/**\n * The saved markdown of an artifact version (its parsed chunks, joined and unescaped).\n *\n * A freshly uploaded document is parsed asynchronously, and for roughly 3 to 20 seconds the parsed\n * content endpoint answers 404, 422 or 500 (or an empty parse). The hook therefore retries with a\n * growing delay (see `ArtifactTextRetry`) and reports `status: 'parsing'` meanwhile, so a host can\n * show \"Preparing editor...\" instead of opening an editor on an empty baseline. After the last\n * retry the status is `error`; `retry()` starts a new round.\n *\n * The text is run through `unescapeMarkdown`: an artifact is always stored from markdown, and the\n * backend's parse of a `.md` escapes its punctuation.\n */\nexport function useArtifactText(\n docId: string | null | undefined,\n options?: { enabled?: boolean; retry?: ArtifactTextRetry }\n): ArtifactText {\n const arbi = useArbi()\n const { attempts, baseDelayMs, maxDelayMs } = {\n ...DEFAULT_ARTIFACT_TEXT_RETRY,\n ...options?.retry,\n }\n // A local draft (placeholder id) has no backend document: asking for its text would be a 422.\n const enabled = (options?.enabled ?? true) && isDocumentId(docId) && arbi.hasWorkspace\n const query = useQuery({\n // Under the shared `parsedDoc` key, so a document's invalidation reaches it too.\n queryKey: [...arbiQueryKeys.parsedDoc(docId ?? '', 'marker'), 'artifact-text'] as const,\n queryFn: async () => {\n // A version reference (`doc-…@v2`) reads that version; a plain id the current one.\n const { docId: plainId, version } = parseVersionRef(docId)\n const parsed = (await arbi.documents.getParsedContent(\n plainId,\n 'marker',\n version ?? undefined\n )) as {\n chunks?: { content?: string | null }[]\n }\n if (!parsed.chunks?.some((c) => c.content?.trim())) throw new NotParsedYetError()\n return flattenChunks(parsed.chunks, { unescape: true })\n },\n enabled,\n retry: (failureCount, error) => !isRefusal(error) && failureCount < attempts,\n retryDelay: (failureCount) => Math.min(baseDelayMs * 1.6 ** failureCount, maxDelayMs),\n })\n\n const { refetch } = query\n const retry = useCallback(() => void refetch(), [refetch])\n\n return useMemo<ArtifactText>(() => {\n const error = query.error\n const neverParsed = error instanceof NotParsedYetError\n let status: ArtifactTextStatus = 'loading'\n if (!enabled) status = 'loading'\n else if (query.isSuccess || (query.isError && neverParsed)) status = 'ready'\n else if (query.isError) status = 'error'\n else if (query.failureCount > 0) status = 'parsing'\n const text = !enabled\n ? undefined\n : query.isSuccess\n ? query.data\n : status === 'ready' || status === 'error'\n ? null\n : undefined\n return {\n text,\n isLoading: enabled && (status === 'loading' || status === 'parsing'),\n status,\n error: status === 'ready' ? null : (error ?? query.failureReason ?? null),\n retry,\n }\n }, [\n enabled,\n query.isSuccess,\n query.isError,\n query.data,\n query.error,\n query.failureCount,\n query.failureReason,\n retry,\n ])\n}\n","import { registerDocumentTurnFlush } from '../chat/turn-flush'\nimport { useCallback, useEffect, useMemo, useState } from 'react'\nimport { useMutation, useQueryClient } from '@tanstack/react-query'\nimport { useArbi } from '../useArbi'\nimport { arbiQueryKeys, useDocumentVersionList } from '../hooks'\nimport { refreshDocuments, useDocuments } from '../documents/documents-data'\nimport { useArtifactText, type ArtifactTextStatus } from './useArtifactText'\nimport {\n documentVersions,\n findVersion,\n isDocumentId,\n parseVersionRef,\n toArtifactVersions,\n versionLabel,\n type ArtifactVersion,\n} from './versions'\nimport {\n useArtifactEditSession,\n type ArtifactEditSession,\n type ArtifactEditSessionOptions,\n type ArtifactSaveResult,\n} from './useArtifactEditSession'\n\n/**\n * The versions of an artifact, oldest first. `docId` is a document id or a version reference\n * (`doc-…@v2`). Every listed document carries its versions; a document that is not in the\n * workspace list asks for its version list.\n */\nexport function useArtifactVersions(\n workspaceId: string | undefined,\n docId: string | null | undefined\n): ArtifactVersion[] {\n const plainId = docId ? parseVersionRef(docId).docId : null\n const { data: documents } = useDocuments(workspaceId)\n const listed = useMemo(\n () => (documents ?? []).find((d) => d.external_id === plainId),\n [documents, plainId]\n )\n // Asked only once the list has loaded and the document is not in it.\n const { data: history } = useDocumentVersionList(plainId, {\n enabled: !!plainId && documents !== undefined && !listed,\n })\n return useMemo(\n () => (listed ? documentVersions(listed) : toArtifactVersions(plainId, history)),\n [listed, history, plainId]\n )\n}\n\nexport interface SaveArtifactVersionInput {\n /** File name of the new version (`.md` is added when missing). */\n title: string\n content: string\n /** Save as the next version of this document (it keeps its id); omit for a new document. */\n autosave?: boolean\n versionOf?: string | null\n /** The processing config (auto-title, parsing) to run the upload with. */\n configId?: string | null\n /** `artifact` (default) for AI-style drafts; `source` for a document the user typed. */\n wpType?: 'artifact' | 'source'\n}\n\n/**\n * Save markdown as a NEW immutable version (the next version of `versionOf`'s document, which keeps\n * its id) and refresh the workspace document list, version lists and the document's text.\n */\nexport function useSaveArtifactVersion() {\n const arbi = useArbi()\n const queryClient = useQueryClient()\n return useMutation<ArtifactSaveResult, Error, SaveArtifactVersionInput>({\n mutationFn: async ({ title, content, versionOf, configId, wpType, autosave }) => {\n // The backend classifies uploads by extension: a title without one is silently dropped.\n const fileName = /\\.md$/i.test(title) ? title : `${title}.md`\n const result = await arbi.documents.uploadFile(\n new Blob([content], { type: 'text/markdown' }),\n fileName,\n {\n wpType: wpType ?? 'artifact',\n autosave,\n versionOf: versionOf ? parseVersionRef(versionOf).docId : undefined,\n configExtId: configId ?? undefined,\n }\n )\n const docId = result.doc_ext_ids?.[0]\n // The document's row moves to the new version (its version lists re-read with it).\n if (docId) await refreshDocuments(queryClient, arbi, [docId])\n await queryClient.invalidateQueries({ queryKey: arbiQueryKeys.allDocumentVersions() })\n // The document's current text is the new version's now.\n if (docId)\n await queryClient.invalidateQueries({ queryKey: arbiQueryKeys.parsedDocument(docId) })\n return { docId, version: result.version ?? undefined }\n },\n })\n}\n\nexport interface ArtifactSessionOptions extends Pick<\n ArtifactEditSessionOptions,\n | 'onSaved'\n | 'onSaveError'\n | 'onCompareChange'\n | 'compareId'\n | 'compareOpensEditor'\n | 'redlineEngine'\n | 'onDirtyChange'\n> {\n workspaceId: string | undefined\n /**\n * The open document (its current version), a version reference `doc-…@v{n}`, or the placeholder\n * id of an unsaved local draft (anything that is not a backend document id, i.e. does not start\n * with `doc`).\n */\n docId: string | null | undefined\n /**\n * The text of a local draft (nothing is fetched for it). The FIRST save uploads it as a real\n * document with no parent and the session switches to the new id; later saves chain from it.\n */\n draftContent?: string\n /** Name for saved versions. Defaults to the open version's file name, else \"Untitled\". */\n title?: string\n /** Processing config for saved versions. */\n configId?: string | null\n}\n\n/** Everything `ArtifactEditor` (`@arbidocs/blocks/artifacts`) takes from a session, flat. */\nexport interface ArtifactSessionView {\n content: string\n isEditing: boolean\n hasChanges: boolean\n isSaving: boolean\n canCompare: boolean\n comparing: boolean\n compareId: string | null\n showTrackChanges: boolean\n versions: ArtifactVersion[]\n currentVersionId: string\n onStartEdit: () => void\n onCancel: () => void\n onSave: () => void\n onCompare: () => void\n onStopCompare: () => void\n onToggleTrackChanges: () => void\n /**\n * Whether the text is ready. `ArtifactEditor` renders \"Preparing editor...\" while it is\n * `loading` or `parsing` (a fresh upload is parsed for a few seconds) and an error with Retry.\n */\n status: ArtifactTextStatus\n /** Ask for the text again after an `error`. */\n onRetry: () => void\n}\n\nexport interface ArtifactSession extends ArtifactEditSession {\n /** The open document: the new real id once a local draft has been saved. */\n docId: string | null | undefined\n /** The open version is an unsaved local draft (no backend document yet). */\n isLocal: boolean\n isLoading: boolean\n /** See `ArtifactTextStatus`. */\n status: ArtifactTextStatus\n versions: ArtifactVersion[]\n /** \"v2\" for any version (reference) of the document. */\n labelOf: (id: string | null | undefined) => string | null\n /** Props for `<ArtifactEditor>`. */\n view: ArtifactSessionView\n /** Props for `<ArtifactMarkdownEditor>` (`@arbidocs/blocks/artifacts/editor`). */\n editorProps: {\n markdown: string\n onChange: (markdown: string) => void\n onNormalized: (markdown: string) => void\n }\n /** Props for `<ArtifactRedlineView>`. */\n redlineProps: {\n state: ArtifactEditSession['redline']['state']\n onRevert: (regionId: string) => void\n }\n}\n\n/**\n * Edit an artifact through the SDK: load it, edit it, save it as a new version, list its versions\n * and compare any two with track changes. Needs an `<ArbiHost>` with an `arbi` and a selected workspace.\n *\n * ```tsx\n * const session = useArtifactSession({ workspaceId, docId, onSaved: ({ docId }) => docId && open(docId) })\n * <ArtifactEditor {...session.view} onOpenVersion={open}\n * editor={<ArtifactMarkdownEditor {...session.editorProps} />}\n * redline={<ArtifactRedlineView {...session.redlineProps} />} />\n * ```\n */\nexport function useArtifactSession(options: ArtifactSessionOptions): ArtifactSession {\n const {\n workspaceId,\n docId: hostDocId,\n draftContent,\n title,\n configId,\n compareId,\n onCompareChange,\n onSaved,\n ...passthrough\n } = options\n // A saved local draft becomes this real document; another host docId resets it.\n const [saved, setSaved] = useState<{\n forId: string | null | undefined\n id: string\n text: string\n } | null>(null)\n const adopted = saved && saved.forId === hostDocId ? saved : null\n const docId = adopted?.id ?? hostDocId\n const isLocal = !isDocumentId(docId)\n\n // Owned here (not in the edit session) because the compare target's text must be loaded first.\n const [ownCompareId, setOwnCompareId] = useState<string | null>(null)\n const activeCompareId = compareId === undefined ? ownCompareId : compareId\n const changeCompare = useCallback(\n (id: string | null) => {\n setOwnCompareId(id)\n onCompareChange?.(id)\n },\n [onCompareChange]\n )\n\n const { text, isLoading, status, retry } = useArtifactText(docId)\n const versions = useArtifactVersions(workspaceId, isLocal ? null : docId)\n const { text: compareText, isLoading: compareLoading } = useArtifactText(activeCompareId, {\n enabled: !!activeCompareId,\n })\n const saveVersion = useSaveArtifactVersion()\n const autosaveVersion = saveVersion.mutateAsync\n const current = findVersion(versions, docId)\n\n const save = useCallback(\n async (content: string) => {\n const result = await saveVersion.mutateAsync({\n title: title ?? current?.name ?? 'Untitled',\n content,\n versionOf: isLocal ? null : docId,\n configId,\n })\n const latest = versions.reduce((n, v) => Math.max(n, v.number), 0)\n return { ...result, version: result.version ?? (isLocal ? 1 : latest + 1) }\n },\n [saveVersion, title, current?.name, isLocal, docId, configId, versions]\n )\n\n const handleSaved = useCallback(\n (result: ArtifactSaveResult, content: string) => {\n // A host that keeps passing the local draft's placeholder id: follow every save to the real\n // document it created (the first save creates version 1, later ones chain from it).\n if (!isDocumentId(hostDocId) && result.docId)\n setSaved({ forId: hostDocId, id: result.docId, text: content })\n onSaved?.(result, content)\n },\n [hostDocId, onSaved]\n )\n\n const session = useArtifactEditSession({\n ...passthrough,\n docId,\n // A local draft has only its own text; a just-saved one shows what was saved until the parsed\n // text of the new document arrives.\n baseline: text ?? (isLocal ? draftContent : adopted?.text),\n savedFromId: current?.savedFromId,\n compareId: activeCompareId,\n onCompareChange: changeCompare,\n compareText,\n compareLoading,\n save,\n onSaved: handleSaved,\n })\n\n useEffect(() => {\n if (isLocal || !session.hasChanges || session.isSaving) return\n const timer = setTimeout(() => {\n void autosaveVersion({\n title: title ?? current?.name ?? 'Untitled',\n content: session.content,\n versionOf: docId,\n configId,\n autosave: true,\n }).catch(() => undefined)\n }, 2500)\n return () => clearTimeout(timer)\n }, [\n isLocal,\n session.content,\n session.hasChanges,\n session.isSaving,\n docId,\n title,\n current?.name,\n configId,\n autosaveVersion,\n ])\n\n useEffect(() => {\n if (!docId) return\n return registerDocumentTurnFlush(docId, async () => {\n if (session.hasChanges && !(await session.save()))\n throw new Error('Save the document before sending your message.')\n })\n }, [docId, session])\n\n const { redline } = session\n return useMemo<ArtifactSession>(\n () => ({\n ...session,\n docId,\n isLocal,\n isLoading,\n status,\n versions,\n labelOf: (id) => versionLabel(versions, id),\n view: {\n content: session.baseline,\n isEditing: session.isEditing,\n hasChanges: session.hasChanges || !!current?.autosave,\n isSaving: session.isSaving,\n canCompare: session.canCompare,\n comparing: !!session.compareId,\n compareId: session.compareId,\n showTrackChanges: session.showTrackChanges,\n versions,\n currentVersionId: current?.id ?? docId ?? '',\n onStartEdit: session.startEdit,\n onCancel: session.cancel,\n onSave: () => void session.save(),\n onCompare: () => session.compare(),\n onStopCompare: session.stopCompare,\n onToggleTrackChanges: () => session.setShowTrackChanges(!session.showTrackChanges),\n // A local draft has its text already: it is ready.\n status: isLocal ? 'ready' : status,\n onRetry: retry,\n },\n editorProps: {\n markdown: session.content,\n onChange: session.change,\n onNormalized: session.normalized,\n },\n redlineProps: {\n state: redline.state,\n onRevert: (regionId) => void session.revertRegion(regionId),\n },\n }),\n [\n session,\n redline,\n isLoading,\n status,\n retry,\n isLocal,\n versions,\n docId,\n current?.id,\n current?.autosave,\n ]\n )\n}\n","import { useQuery } from '@tanstack/react-query'\nimport { useArbi } from '../useArbi'\nimport { arbiQueryKeys } from '../query-keys'\n\n/**\n * The stored source of a live artifact (the `.html` / `.jsx` file itself; the parsed text strips\n * it). `undefined` until it has loaded.\n */\nexport function useArtifactSource(documentId: string | null | undefined, enabled = true) {\n const arbi = useArbi()\n return useQuery({\n queryKey: arbiQueryKeys.artifactSource(documentId ?? ''),\n queryFn: async () => (await arbi.documents.download(documentId as string)).text(),\n enabled: enabled && !!documentId && arbi.isLoggedIn,\n staleTime: 5 * 60 * 1000,\n })\n}\n","import type { ComponentPropsWithoutRef, FC, ReactNode } from 'react'\nimport { MarkdownRenderer } from '../ai/MarkdownRenderer'\n\n/** The `<del>` / `<ins>` renderers `ArtifactRedlineView` hands to a markdown renderer. */\nexport interface ArtifactRedlineComponents {\n del: FC<ComponentPropsWithoutRef<'del'> & { 'data-cid'?: string }>\n ins: FC<ComponentPropsWithoutRef<'ins'> & { 'data-cid'?: string }>\n}\n\n/**\n * The kit's markdown renderer as the `renderMarkdown` slot of `ArtifactEditor` and\n * `ArtifactRedlineView`: full markdown (tables, lists, code) plus the raw `<del>` / `<ins>` tags\n * of a redline.\n */\nexport function renderArtifactMarkdown(\n markdown: string,\n components?: ArtifactRedlineComponents\n): ReactNode {\n return <MarkdownRenderer components={components}>{markdown}</MarkdownRenderer>\n}\n","import { useCallback, useEffect, useRef, useState } from 'react'\nimport type { ArtifactEvent } from '../ai/engine'\nimport { cleanDraftWithReport, type DraftCleanReport } from '../ai/stored-markdown'\nimport { useSaveArtifactVersion } from './useArtifactSession'\nimport { isDocumentId } from './versions'\nimport { useArtifactText, type ArtifactTextRetry } from './useArtifactText'\n\n/**\n * Where a drafted artifact is in its STREAM -> PERSIST -> EDIT life:\n * - `streaming`: the agent is still working (or has produced nothing final): show the live canvas;\n * - `persisting`: the finished draft is being stored as version 1 of the artifact;\n * - `parsing`: stored; the backend is still parsing it, so there is no editable text yet;\n * - `ready`: the text is readable: mount the editor on `docId`;\n * - `error`: storing failed or the parse never produced text (`retry()` starts over).\n */\nexport type StreamedArtifactPhase = 'streaming' | 'persisting' | 'parsing' | 'ready' | 'error'\n\nexport interface UseStreamedArtifactOptions {\n /** The artifacts of the agent run (`useConnectedAiTask().artifacts`). */\n artifacts: ArtifactEvent[]\n workspaceId: string | undefined\n /**\n * The run has stopped: not busy and not failed. Nothing is stored before this, so a draft that\n * is still streaming is never persisted half-written.\n */\n settled: boolean\n /**\n * Make the stored markdown from the streamed content. Default `cleanDraftWithReport`, which is\n * guarded: it stores the raw streamed text (or links-only text) rather than a cleanup that would\n * lose content (see `DraftCleanReport`).\n */\n clean?: (streamed: string) => string\n /** Called once per stored draft with what the cleanup did (`stage` is not `cleaned` if guarded). */\n onCleaned?: (report: DraftCleanReport) => void\n /** Processing config for the stored version. */\n configId?: string | null\n /** How long to wait for the parse (see `useArtifactText`). */\n parseRetry?: ArtifactTextRetry\n}\n\nexport interface StreamedArtifact {\n phase: StreamedArtifactPhase\n /** The finished streamed artifact (its `title` names the stored versions). */\n artifact: ArtifactEvent | undefined\n /** The backend document to edit, once there is one (while `parsing` and `ready`). */\n docId: string | undefined\n error: Error | null\n /** What the cleanup did to the stored draft (`null` until it was stored, or with a custom `clean`). */\n cleaned: DraftCleanReport | null\n /** After an `error`: store again or ask for the parse again. */\n retry: () => void\n}\n\n/**\n * The lifecycle of an agent-drafted artifact, so a host passes the stream and renders the phase.\n *\n * While the agent streams, the draft lives only on the canvas. Once the run is `settled` and the\n * draft is complete, a draft that is not yet a backend document is cleaned (`cleanDraftMarkdown`)\n * and stored ONCE as the artifact's first version (`wpType: 'artifact'`); a draft that already is a\n * backend document passes through. The hook then waits for the parse (`useArtifactText`, which\n * retries) and reports `ready` with the `docId` to open in an editor.\n *\n * Needs an `<ArbiHost>` with an `arbi` and a workspace. `@arbidocs/blocks/connect` has the whole thing as a\n * component (`StreamedArtifact`).\n */\nexport function useStreamedArtifact({\n artifacts,\n workspaceId,\n settled,\n clean,\n onCleaned,\n configId,\n parseRetry,\n}: UseStreamedArtifactOptions): StreamedArtifact {\n const artifact = artifacts.find((a) => a.is_complete)\n const { mutateAsync } = useSaveArtifactVersion()\n const [saved, setSaved] = useState<{ content: string; docId: string } | null>(null)\n const [saveError, setSaveError] = useState<Error | null>(null)\n const [attempt, setAttempt] = useState(0)\n const [cleaned, setCleaned] = useState<DraftCleanReport | null>(null)\n const started = useRef<string | null>(null)\n\n const content = artifact?.content ?? ''\n const title = artifact?.title ?? 'Draft'\n const passThrough = !!artifact && isDocumentId(artifact.doc_ext_id)\n const needsSave = settled && !!artifact && !passThrough && !!content.trim() && !!workspaceId\n\n useEffect(() => {\n const key = `${attempt}:${content}`\n if (!needsSave || started.current === key) return\n started.current = key\n setSaveError(null)\n let stored: string\n if (clean) stored = clean(content)\n else {\n const report = cleanDraftWithReport(content)\n stored = report.text\n setCleaned(report)\n onCleaned?.(report)\n if (report.guarded) {\n console.warn(\n `[useStreamedArtifact] the draft cleanup would have lost content; stored the ${report.stage} text instead (kept ${Math.round(report.ratio * 100)}%, changed by: ${report.changedBy.join(', ') || 'nothing'})`\n )\n }\n }\n mutateAsync({\n title,\n content: stored,\n configId,\n wpType: 'artifact',\n })\n .then((r) => {\n if (r.docId) setSaved({ content, docId: r.docId })\n else setSaveError(new Error('The draft was stored but the server returned no document id'))\n })\n .catch((e: unknown) => setSaveError(e instanceof Error ? e : new Error(String(e))))\n // `clean` and `onCleaned` are deliberately not dependencies: a new function identity must not\n // store twice.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [needsSave, content, title, workspaceId, configId, attempt, mutateAsync])\n\n const docId = !artifact\n ? undefined\n : passThrough\n ? artifact.doc_ext_id\n : saved?.content === content\n ? saved.docId\n : undefined\n\n const text = useArtifactText(docId, { enabled: settled, retry: parseRetry })\n const { retry: retryText } = text\n\n const retry = useCallback(() => {\n if (saveError) setAttempt((n) => n + 1)\n else retryText()\n }, [saveError, retryText])\n\n let phase: StreamedArtifactPhase\n if (!settled || !artifact) phase = 'streaming'\n else if (saveError) phase = 'error'\n else if (!docId) phase = needsSave ? 'persisting' : 'streaming'\n else if (text.status === 'ready' && text.text) phase = 'ready'\n else if (text.status === 'error' || text.status === 'ready') phase = 'error'\n else phase = 'parsing'\n\n const error =\n phase !== 'error'\n ? null\n : (saveError ?? text.error ?? new Error('The document has no text to edit'))\n return { phase, artifact, docId, error, cleaned, retry }\n}\n","/**\n * Which live renderer, if any, an artifact maps to. Kept free of heavy imports (no compiler) so the\n * document viewer can use it without pulling the preview code into its bundle.\n *\n * The backend persists an artifact's render mode in the saved file's extension: .html / .jsx are live\n * artifacts; document drafts are stored as .docx and open in the Word editor.\n */\nexport type ArtifactPreviewKind = 'html' | 'react'\n\nexport function artifactKindFromFileName(fileName?: string | null): ArtifactPreviewKind | null {\n const ext = (fileName ?? '').toLowerCase().match(/\\.[a-z0-9]+$/)?.[0]\n if (ext === '.html' || ext === '.htm') return 'html'\n if (ext === '.jsx') return 'react'\n return null\n}\n\nexport function artifactKindFromMode(mode?: string | null): ArtifactPreviewKind | null {\n return mode === 'html' || mode === 'react' ? mode : null\n}\n\n/**\n * The viewer tab an artifact opens on: a generated image on its media view ('pdf'), an html/react\n * artifact on its rendered Preview, a slide deck ARBI saved (a .pptx) in the slides editor, and a\n * document draft (stored as .docx) in the Word editor.\n */\nexport function viewerTabForArtifactMode(\n mode?: string | null\n): 'pdf' | 'preview' | 'edit' | 'slides' {\n if (mode === 'image') return 'pdf'\n if (mode === 'presentation') return 'slides'\n return artifactKindFromMode(mode) ? 'preview' : 'edit'\n}\n"]}
@@ -63,6 +63,7 @@ declare function useRedline({ baseText, compareText, isLoading, engine, }: UseRe
63
63
  */
64
64
  /** One version entry as the API returns it (a subset of `DocVersion`). */
65
65
  interface DocVersionEntry {
66
+ autosave?: boolean;
66
67
  n: number;
67
68
  file_name?: string | null;
68
69
  created_at?: string | null;
@@ -98,6 +99,7 @@ interface ArtifactVersion {
98
99
  reason: string;
99
100
  isCurrent: boolean;
100
101
  /** The version this one was saved from (an older one than the previous version when restored). */
102
+ autosave?: boolean;
101
103
  savedFromId?: string | null;
102
104
  /** The thread (and its message) whose turn made this version; none for a person's. */
103
105
  thread?: {
@@ -285,6 +287,7 @@ interface SaveArtifactVersionInput {
285
287
  title: string;
286
288
  content: string;
287
289
  /** Save as the next version of this document (it keeps its id); omit for a new document. */
290
+ autosave?: boolean;
288
291
  versionOf?: string | null;
289
292
  /** The processing config (auto-title, parsing) to run the upload with. */
290
293
  configId?: string | null;