@duskmoon-dev/components 0.5.5 → 0.5.6

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.
@@ -2,23 +2,25 @@
2
2
 
3
3
  // src/components/chat/Chat.tsx
4
4
  import {
5
- forwardRef as forwardRef4,
5
+ forwardRef as forwardRef6,
6
6
  useId as useId3,
7
- useLayoutEffect as useLayoutEffect2,
8
- useRef as useRef3,
7
+ useLayoutEffect as useLayoutEffect3,
8
+ useRef as useRef5,
9
9
  useState as useState3
10
10
  } from "react";
11
11
 
12
12
  // src/components/chat/ChatComposer.tsx
13
13
  import {
14
- forwardRef as forwardRef2,
15
- useEffect,
14
+ forwardRef as forwardRef4,
16
15
  useId,
17
- useLayoutEffect,
18
- useRef as useRef2,
16
+ useLayoutEffect as useLayoutEffect2,
17
+ useRef as useRef4,
19
18
  useState as useState2
20
19
  } from "react";
21
20
 
21
+ // src/components/chat/composer/ComposerShell.tsx
22
+ import { forwardRef, useEffect, useRef } from "react";
23
+
22
24
  // src/components/button/Button.tsx
23
25
  import React from "react";
24
26
  import { createPortal } from "react-dom";
@@ -371,11 +373,231 @@ function Button({
371
373
  ]
372
374
  });
373
375
  }
376
+ // src/components/chat/composer/ComposerShell.tsx
377
+ import { jsx as jsx2, jsxs as jsxs2, Fragment as Fragment2 } from "react/jsx-runtime";
378
+ function AttachmentPreview({ file }) {
379
+ const imageRef = useRef(null);
380
+ useEffect(() => {
381
+ const image = imageRef.current;
382
+ if (!image || !file.type.startsWith("image/") || !URL.createObjectURL)
383
+ return;
384
+ const objectUrl = URL.createObjectURL(file);
385
+ image.src = objectUrl;
386
+ image.hidden = false;
387
+ return () => {
388
+ image.hidden = true;
389
+ image.removeAttribute("src");
390
+ URL.revokeObjectURL(objectUrl);
391
+ };
392
+ }, [file]);
393
+ return file.type.startsWith("image/") ? /* @__PURE__ */ jsx2("img", {
394
+ ref: imageRef,
395
+ alt: file.name,
396
+ hidden: true
397
+ }) : null;
398
+ }
399
+ function AttachmentContent({
400
+ file,
401
+ renderAttachment,
402
+ context
403
+ }) {
404
+ return renderAttachment(file, context);
405
+ }
406
+ function ExtraActions({
407
+ children,
408
+ context
409
+ }) {
410
+ return typeof children === "function" ? children(context) : children;
411
+ }
412
+ var ComposerShell = forwardRef(({
413
+ editor,
414
+ composerId,
415
+ entries,
416
+ renderAttachment,
417
+ removeAttachment,
418
+ addAttachments,
419
+ onAttachmentsChange,
420
+ pickerRef,
421
+ accept,
422
+ multiple,
423
+ disabled,
424
+ busy,
425
+ submitting,
426
+ generating,
427
+ eligible,
428
+ completion,
429
+ candidates,
430
+ active,
431
+ showCompletion,
432
+ selectCompletion,
433
+ visibleError,
434
+ hasError,
435
+ attachLabel,
436
+ sendLabel,
437
+ stopLabel,
438
+ openAttachments,
439
+ submit,
440
+ onStop,
441
+ children,
442
+ className,
443
+ ...props
444
+ }, ref) => /* @__PURE__ */ jsxs2("div", {
445
+ ...props,
446
+ ref,
447
+ className: cn("chat-composer", className),
448
+ children: [
449
+ entries.length > 0 ? /* @__PURE__ */ jsx2("ul", {
450
+ role: "list",
451
+ className: "chat-composer-attachments",
452
+ "aria-label": "Attachments",
453
+ children: entries.map(({ file, id }) => /* @__PURE__ */ jsx2("li", {
454
+ className: "chat-composer-attachment",
455
+ children: renderAttachment ? /* @__PURE__ */ jsx2(AttachmentContent, {
456
+ file,
457
+ renderAttachment,
458
+ context: {
459
+ remove: () => removeAttachment(id),
460
+ disabled: disabled || !onAttachmentsChange
461
+ }
462
+ }) : /* @__PURE__ */ jsxs2(Fragment2, {
463
+ children: [
464
+ /* @__PURE__ */ jsx2(AttachmentPreview, {
465
+ file
466
+ }),
467
+ /* @__PURE__ */ jsx2("span", {
468
+ className: "chat-composer-filename",
469
+ title: file.name,
470
+ children: file.name
471
+ }),
472
+ onAttachmentsChange ? /* @__PURE__ */ jsx2(Button, {
473
+ type: "button",
474
+ size: "sm",
475
+ color: "base",
476
+ appearance: "ghost",
477
+ disabled,
478
+ "aria-label": `Remove ${file.name}`,
479
+ onClick: () => removeAttachment(id),
480
+ children: "Remove"
481
+ }) : null
482
+ ]
483
+ })
484
+ }, id))
485
+ }) : null,
486
+ editor,
487
+ showCompletion ? /* @__PURE__ */ jsx2("ul", {
488
+ id: `${composerId}-completions`,
489
+ role: "listbox",
490
+ "aria-label": completion?.label ?? "Suggestions",
491
+ className: "chat-composer-completions",
492
+ children: candidates.map((item, index) => /* @__PURE__ */ jsxs2("li", {
493
+ id: `${composerId}-option-${index}`,
494
+ role: "option",
495
+ "aria-selected": item.id === active?.id,
496
+ "aria-disabled": item.disabled || undefined,
497
+ onMouseDown: (event) => event.preventDefault(),
498
+ onClick: () => selectCompletion(item),
499
+ onKeyDown: (event) => {
500
+ if (event.key === "Enter" || event.key === " ") {
501
+ event.preventDefault();
502
+ selectCompletion(item);
503
+ }
504
+ },
505
+ children: [
506
+ /* @__PURE__ */ jsx2("span", {
507
+ children: item.label
508
+ }),
509
+ item.description ? /* @__PURE__ */ jsx2("span", {
510
+ className: "chat-composer-description",
511
+ children: item.description
512
+ }) : null
513
+ ]
514
+ }, item.id))
515
+ }) : null,
516
+ hasError ? /* @__PURE__ */ jsx2("div", {
517
+ id: `${composerId}-error`,
518
+ role: "alert",
519
+ className: "chat-composer-error chat-composer-status",
520
+ "data-state": "error",
521
+ children: visibleError
522
+ }) : null,
523
+ /* @__PURE__ */ jsxs2("div", {
524
+ className: "chat-composer-actions",
525
+ children: [
526
+ onAttachmentsChange ? /* @__PURE__ */ jsxs2(Fragment2, {
527
+ children: [
528
+ /* @__PURE__ */ jsx2("input", {
529
+ ref: pickerRef,
530
+ type: "file",
531
+ accept,
532
+ multiple,
533
+ disabled,
534
+ hidden: true,
535
+ "aria-label": attachLabel,
536
+ onChange: (event) => {
537
+ const files = Array.from(event.currentTarget.files ?? []);
538
+ event.currentTarget.value = "";
539
+ if (files.length > 0)
540
+ addAttachments(files);
541
+ }
542
+ }),
543
+ /* @__PURE__ */ jsx2(Button, {
544
+ type: "button",
545
+ color: "base",
546
+ appearance: "ghost",
547
+ disabled,
548
+ onClick: openAttachments,
549
+ children: attachLabel
550
+ })
551
+ ]
552
+ }) : null,
553
+ /* @__PURE__ */ jsx2(ExtraActions, {
554
+ context: {
555
+ submit: () => {
556
+ submit();
557
+ },
558
+ submitting,
559
+ canSubmit: eligible,
560
+ disabled,
561
+ generating,
562
+ openAttachments
563
+ },
564
+ children
565
+ }),
566
+ generating && onStop ? /* @__PURE__ */ jsx2(Button, {
567
+ type: "button",
568
+ disabled,
569
+ onClick: onStop,
570
+ children: stopLabel
571
+ }) : null,
572
+ /* @__PURE__ */ jsx2(Button, {
573
+ type: "button",
574
+ color: "primary",
575
+ disabled: !eligible,
576
+ "aria-busy": busy || submitting || undefined,
577
+ onClick: () => {
578
+ submit();
579
+ },
580
+ children: sendLabel
581
+ })
582
+ ]
583
+ })
584
+ ]
585
+ }));
586
+ ComposerShell.displayName = "ComposerShell";
587
+
588
+ // src/components/chat/composer/PlainEditor.tsx
589
+ import {
590
+ forwardRef as forwardRef3,
591
+ useImperativeHandle as useImperativeHandle2,
592
+ useLayoutEffect,
593
+ useRef as useRef3
594
+ } from "react";
595
+
374
596
  // src/components/input/Input.tsx
375
597
  import {
376
- forwardRef,
598
+ forwardRef as forwardRef2,
377
599
  useImperativeHandle,
378
- useRef,
600
+ useRef as useRef2,
379
601
  useState
380
602
  } from "react";
381
603
 
@@ -465,14 +687,14 @@ function getTextAreaCountClasses({ exceeded }) {
465
687
  }
466
688
 
467
689
  // src/components/input/Input.tsx
468
- import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
690
+ import { jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
469
691
  function toTextValue(value) {
470
692
  if (Array.isArray(value)) {
471
693
  return value.join(",");
472
694
  }
473
695
  return value == null ? "" : String(value);
474
696
  }
475
- var InputRoot = forwardRef(({
697
+ var InputRoot = forwardRef2(({
476
698
  allowClear,
477
699
  className,
478
700
  defaultValue,
@@ -487,7 +709,7 @@ var InputRoot = forwardRef(({
487
709
  variant = "outlined",
488
710
  ...props
489
711
  }, ref) => {
490
- const inputRef = useRef(null);
712
+ const inputRef = useRef2(null);
491
713
  const [internalValue, setInternalValue] = useState(() => toTextValue(defaultValue));
492
714
  const isControlled = value !== undefined;
493
715
  const currentValue = isControlled ? toTextValue(value) : internalValue;
@@ -515,7 +737,7 @@ var InputRoot = forwardRef(({
515
737
  });
516
738
  input.focus();
517
739
  }
518
- const control = /* @__PURE__ */ jsx2("input", {
740
+ const control = /* @__PURE__ */ jsx3("input", {
519
741
  ...props,
520
742
  ref: inputRef,
521
743
  type,
@@ -528,22 +750,22 @@ var InputRoot = forwardRef(({
528
750
  return control;
529
751
  }
530
752
  const showClear = allowClear && !disabled && currentValue.length > 0;
531
- return /* @__PURE__ */ jsxs2("span", {
753
+ return /* @__PURE__ */ jsxs3("span", {
532
754
  className: getInputWrapperClasses({ affixed: hasAffix }),
533
755
  children: [
534
- prefix != null ? /* @__PURE__ */ jsx2("span", {
756
+ prefix != null ? /* @__PURE__ */ jsx3("span", {
535
757
  className: inputPrefixClass,
536
758
  children: prefix
537
759
  }) : null,
538
760
  control,
539
- showClear ? /* @__PURE__ */ jsx2("button", {
761
+ showClear ? /* @__PURE__ */ jsx3("button", {
540
762
  type: "button",
541
763
  className: inputClearClass,
542
764
  "aria-label": "Clear input",
543
765
  onClick: handleClear,
544
766
  children: "x"
545
767
  }) : null,
546
- suffix != null ? /* @__PURE__ */ jsx2("span", {
768
+ suffix != null ? /* @__PURE__ */ jsx3("span", {
547
769
  className: inputSuffixClass,
548
770
  children: suffix
549
771
  }) : null
@@ -551,7 +773,7 @@ var InputRoot = forwardRef(({
551
773
  });
552
774
  });
553
775
  InputRoot.displayName = "Input";
554
- var Search = forwardRef(({
776
+ var Search = forwardRef2(({
555
777
  className,
556
778
  disabled,
557
779
  enterButton,
@@ -560,7 +782,7 @@ var Search = forwardRef(({
560
782
  onSearch,
561
783
  ...props
562
784
  }, ref) => {
563
- const inputRef = useRef(null);
785
+ const inputRef = useRef2(null);
564
786
  useImperativeHandle(ref, () => inputRef.current);
565
787
  function emitSearch(event) {
566
788
  onSearch?.(inputRef.current?.value ?? "", event);
@@ -572,16 +794,16 @@ var Search = forwardRef(({
572
794
  }
573
795
  }
574
796
  const buttonContent = enterButton === true ? "Search" : enterButton ?? "Search";
575
- return /* @__PURE__ */ jsxs2("span", {
797
+ return /* @__PURE__ */ jsxs3("span", {
576
798
  className: getSearchWrapperClasses({ className }),
577
799
  children: [
578
- /* @__PURE__ */ jsx2(InputRoot, {
800
+ /* @__PURE__ */ jsx3(InputRoot, {
579
801
  ...props,
580
802
  ref: inputRef,
581
803
  disabled,
582
804
  onKeyDown: handleKeyDown
583
805
  }),
584
- enterButton || loading ? /* @__PURE__ */ jsx2("button", {
806
+ enterButton || loading ? /* @__PURE__ */ jsx3("button", {
585
807
  type: "button",
586
808
  className: inputSearchButtonClass,
587
809
  disabled: disabled || loading,
@@ -593,10 +815,10 @@ var Search = forwardRef(({
593
815
  });
594
816
  });
595
817
  Search.displayName = "Input.Search";
596
- var Password = forwardRef(({ className, disabled, visibilityToggle = true, ...props }, ref) => {
818
+ var Password = forwardRef2(({ className, disabled, visibilityToggle = true, ...props }, ref) => {
597
819
  const [visible, setVisible] = useState(false);
598
820
  const type = visible ? "text" : "password";
599
- const toggle = visibilityToggle ? /* @__PURE__ */ jsx2("button", {
821
+ const toggle = visibilityToggle ? /* @__PURE__ */ jsx3("button", {
600
822
  type: "button",
601
823
  className: inputPasswordToggleClass,
602
824
  disabled,
@@ -606,9 +828,9 @@ var Password = forwardRef(({ className, disabled, visibilityToggle = true, ...pr
606
828
  },
607
829
  children: visible ? "Hide" : "Show"
608
830
  }) : undefined;
609
- return /* @__PURE__ */ jsx2("span", {
831
+ return /* @__PURE__ */ jsx3("span", {
610
832
  className: getPasswordWrapperClasses({ className }),
611
- children: /* @__PURE__ */ jsx2(InputRoot, {
833
+ children: /* @__PURE__ */ jsx3(InputRoot, {
612
834
  ...props,
613
835
  ref,
614
836
  disabled,
@@ -618,7 +840,7 @@ var Password = forwardRef(({ className, disabled, visibilityToggle = true, ...pr
618
840
  });
619
841
  });
620
842
  Password.displayName = "Input.Password";
621
- var TextArea = forwardRef(({
843
+ var TextArea = forwardRef2(({
622
844
  autoSize,
623
845
  className,
624
846
  defaultValue,
@@ -643,7 +865,7 @@ var TextArea = forwardRef(({
643
865
  }
644
866
  onChange?.(event);
645
867
  }
646
- const control = /* @__PURE__ */ jsx2("textarea", {
868
+ const control = /* @__PURE__ */ jsx3("textarea", {
647
869
  ...props,
648
870
  ref,
649
871
  className: getTextAreaClasses({
@@ -661,11 +883,11 @@ var TextArea = forwardRef(({
661
883
  if (!showCount) {
662
884
  return control;
663
885
  }
664
- return /* @__PURE__ */ jsxs2("span", {
886
+ return /* @__PURE__ */ jsxs3("span", {
665
887
  className: textAreaWrapperClass,
666
888
  children: [
667
889
  control,
668
- /* @__PURE__ */ jsxs2("span", {
890
+ /* @__PURE__ */ jsxs3("span", {
669
891
  className: getTextAreaCountClasses({ exceeded }),
670
892
  children: [
671
893
  count,
@@ -676,7 +898,7 @@ var TextArea = forwardRef(({
676
898
  });
677
899
  });
678
900
  TextArea.displayName = "Input.TextArea";
679
- var Group = forwardRef(({ className, children, ...props }, ref) => /* @__PURE__ */ jsx2("div", {
901
+ var Group = forwardRef2(({ className, children, ...props }, ref) => /* @__PURE__ */ jsx3("div", {
680
902
  ...props,
681
903
  ref,
682
904
  className: cn(inputGroupClass, className),
@@ -689,46 +911,152 @@ var Input = Object.assign(InputRoot, {
689
911
  TextArea,
690
912
  Group
691
913
  });
692
- // src/components/chat/ChatComposer.tsx
693
- import { jsx as jsx3, jsxs as jsxs3, Fragment as Fragment2 } from "react/jsx-runtime";
694
- function AttachmentPreview({ file }) {
695
- const imageRef = useRef2(null);
696
- useEffect(() => {
697
- const image = imageRef.current;
698
- if (!image || !file.type.startsWith("image/") || !URL.createObjectURL)
914
+ // src/components/chat/composer/PlainEditor.tsx
915
+ import { jsx as jsx4 } from "react/jsx-runtime";
916
+ var PlainEditor = forwardRef3(({
917
+ value,
918
+ disabled,
919
+ minRows,
920
+ maxRows,
921
+ generation,
922
+ textareaProps,
923
+ composerId,
924
+ hasError,
925
+ hasCompletion,
926
+ showCompletion,
927
+ activeIndex,
928
+ onChange,
929
+ onKeyDown
930
+ }, ref) => {
931
+ const editorRef = useRef3(null);
932
+ const composingRef = useRef3(false);
933
+ const minimumRows = Math.max(1, Math.floor(minRows));
934
+ const maximumRows = Math.max(minimumRows, Math.floor(maxRows));
935
+ useImperativeHandle2(ref, () => ({
936
+ focus: () => editorRef.current?.focus(),
937
+ selection: (currentValue) => ({
938
+ value: currentValue,
939
+ selectionStart: editorRef.current?.selectionStart ?? currentValue.length,
940
+ selectionEnd: editorRef.current?.selectionEnd ?? currentValue.length
941
+ })
942
+ }), []);
943
+ useLayoutEffect(() => {
944
+ composingRef.current = false;
945
+ }, [generation]);
946
+ useLayoutEffect(() => {
947
+ const editor = editorRef.current;
948
+ if (!editor)
699
949
  return;
700
- const objectUrl = URL.createObjectURL(file);
701
- image.src = objectUrl;
702
- image.hidden = false;
950
+ function measure() {
951
+ if (!editor)
952
+ return;
953
+ const style = getComputedStyle(editor);
954
+ const lineHeight = parseFloat(style.lineHeight) || 24;
955
+ const padding = (parseFloat(style.paddingTop) || 0) + (parseFloat(style.paddingBottom) || 0);
956
+ const borders = (parseFloat(style.borderTopWidth) || 0) + (parseFloat(style.borderBottomWidth) || 0);
957
+ const adjustment = style.boxSizing === "border-box" ? padding + borders : 0;
958
+ editor.style.height = "auto";
959
+ const minimum = minimumRows * lineHeight + adjustment;
960
+ const maximum = maximumRows * lineHeight + adjustment;
961
+ const content = editor.scrollHeight - padding + adjustment;
962
+ editor.style.height = `${Math.min(maximum, Math.max(minimum, content))}px`;
963
+ editor.style.overflowY = content > maximum ? "auto" : "hidden";
964
+ }
965
+ measure();
966
+ const observer = typeof ResizeObserver === "undefined" ? undefined : new ResizeObserver(measure);
967
+ observer?.observe(editor);
968
+ window.addEventListener("resize", measure);
703
969
  return () => {
704
- image.hidden = true;
705
- image.removeAttribute("src");
706
- URL.revokeObjectURL(objectUrl);
970
+ observer?.disconnect();
971
+ window.removeEventListener("resize", measure);
707
972
  };
708
- }, [file]);
709
- return file.type.startsWith("image/") ? /* @__PURE__ */ jsx3("img", {
710
- ref: imageRef,
711
- alt: file.name,
712
- hidden: true
713
- }) : null;
973
+ }, [value, minimumRows, maximumRows, textareaProps?.style]);
974
+ return /* @__PURE__ */ jsx4(Input.TextArea, {
975
+ ...textareaProps,
976
+ ref: editorRef,
977
+ className: cn("chat-composer-editor", textareaProps?.className),
978
+ value,
979
+ disabled,
980
+ rows: minimumRows,
981
+ resize: "none",
982
+ "aria-label": textareaProps?.["aria-label"] ?? (textareaProps?.["aria-labelledby"] ? undefined : "Message"),
983
+ "aria-describedby": cn(textareaProps?.["aria-describedby"], hasError && `${composerId}-error`) || undefined,
984
+ "aria-invalid": hasError ? true : textareaProps?.["aria-invalid"],
985
+ "aria-controls": showCompletion ? `${composerId}-completions` : undefined,
986
+ "aria-autocomplete": hasCompletion ? "list" : undefined,
987
+ "aria-haspopup": hasCompletion ? "listbox" : undefined,
988
+ "aria-activedescendant": showCompletion && activeIndex >= 0 ? `${composerId}-option-${activeIndex}` : undefined,
989
+ onChange: (event) => onChange(event.currentTarget.value),
990
+ onCompositionStart: (event) => {
991
+ composingRef.current = true;
992
+ textareaProps?.onCompositionStart?.(event);
993
+ },
994
+ onCompositionEnd: (event) => {
995
+ composingRef.current = false;
996
+ textareaProps?.onCompositionEnd?.(event);
997
+ },
998
+ onKeyDown: (event) => {
999
+ textareaProps?.onKeyDown?.(event);
1000
+ if (event.defaultPrevented || composingRef.current || event.nativeEvent.isComposing || event.keyCode === 229)
1001
+ return;
1002
+ onKeyDown(event);
1003
+ }
1004
+ });
1005
+ });
1006
+ PlainEditor.displayName = "PlainEditor";
1007
+
1008
+ // src/components/chat/composer/legacy.ts
1009
+ function reconcileAttachments(previous, files, nextId) {
1010
+ const remaining = [...previous];
1011
+ const entries = files.map((file) => {
1012
+ const index = remaining.findIndex((entry) => entry.file === file);
1013
+ return index < 0 ? { file, id: nextId++ } : remaining.splice(index, 1)[0];
1014
+ });
1015
+ return { entries, nextId };
714
1016
  }
715
- function AttachmentContent({
716
- file,
717
- renderAttachment,
718
- context
1017
+ function legacyEligibility({
1018
+ value,
1019
+ attachments = [],
1020
+ allowAttachmentOnly = false,
1021
+ canSubmit = true,
1022
+ disabled = false,
1023
+ busy = false,
1024
+ submitting
719
1025
  }) {
720
- return renderAttachment(file, context);
1026
+ return canSubmit && !disabled && !busy && !submitting && (value.trim().length > 0 || allowAttachmentOnly && attachments.length > 0);
721
1027
  }
722
- function ExtraActions({
723
- children,
724
- context
725
- }) {
726
- return typeof children === "function" ? children(context) : children;
1028
+ function legacyCompletion(candidates, activeId) {
1029
+ const enabled = candidates.filter((item) => !item.disabled);
1030
+ return {
1031
+ enabled,
1032
+ active: enabled.find((item) => item.id === activeId) ?? enabled[0]
1033
+ };
727
1034
  }
728
- var ChatComposer = forwardRef2(({
1035
+ function moveLegacyCompletion(enabled, activeId, direction) {
1036
+ if (enabled.length === 0)
1037
+ return;
1038
+ const position = enabled.findIndex((item) => item.id === activeId);
1039
+ return enabled[(position + direction + enabled.length) % enabled.length].id;
1040
+ }
1041
+ function legacySnapshot(value, files, revision, entries) {
1042
+ const submission = { value, files: [...files] };
1043
+ return {
1044
+ submission,
1045
+ revision,
1046
+ entryIds: new Set(entries.map((entry) => entry.id))
1047
+ };
1048
+ }
1049
+ function retainUnsubmittedAttachments(entries, submittedIds) {
1050
+ return entries.filter((entry) => !submittedIds.has(entry.id));
1051
+ }
1052
+
1053
+ // src/components/chat/ChatComposer.tsx
1054
+ import { jsx as jsx5 } from "react/jsx-runtime";
1055
+ var ChatComposer = forwardRef4(({
729
1056
  value,
730
1057
  onValueChange,
731
1058
  onSubmit,
1059
+ scope,
732
1060
  attachments = [],
733
1061
  onAttachmentsChange,
734
1062
  accept,
@@ -755,20 +1083,24 @@ var ChatComposer = forwardRef2(({
755
1083
  ...props
756
1084
  }, ref) => {
757
1085
  const id = useId();
758
- const editorRef = useRef2(null);
759
- const pickerRef = useRef2(null);
760
- const mountedRef = useRef2(false);
761
- const composingRef = useRef2(false);
762
- const submittingRef = useRef2(false);
763
- const revisionRef = useRef2(0);
764
- const observedValueRef = useRef2(value);
765
- const entriesRef = useRef2([]);
766
- const nextEntryRef = useRef2(0);
1086
+ const editorRef = useRef4(null);
1087
+ const pickerRef = useRef4(null);
1088
+ const mountedRef = useRef4(false);
1089
+ const submittingRef = useRef4(false);
1090
+ const revisionRef = useRef4(0);
1091
+ const observedValueRef = useRef4(value);
1092
+ const entriesRef = useRef4([]);
1093
+ const nextEntryRef = useRef4(0);
1094
+ const scopeRef = useRef4({
1095
+ contextKey: scope?.contextKey,
1096
+ conversationId: scope?.conversationId
1097
+ });
1098
+ const generationRef = useRef4(0);
767
1099
  const [submitting, setSubmitting] = useState2(false);
768
1100
  const [submitError, setSubmitError] = useState2();
769
1101
  const [activeId, setActiveId] = useState2();
770
1102
  const [dismissed, setDismissed] = useState2(false);
771
- const latestRef = useRef2({
1103
+ const latestRef = useRef4({
772
1104
  value,
773
1105
  onValueChange,
774
1106
  attachments,
@@ -776,27 +1108,37 @@ var ChatComposer = forwardRef2(({
776
1108
  onSubmitError,
777
1109
  clearOnSubmit
778
1110
  });
779
- function reconcileEntries(files) {
780
- const previous = [...entriesRef.current];
781
- entriesRef.current = files.map((file) => {
782
- const index = previous.findIndex((entry) => entry.file === file);
783
- if (index < 0)
784
- return { file, id: nextEntryRef.current++ };
785
- return previous.splice(index, 1)[0];
786
- });
787
- }
788
- useLayoutEffect(() => {
1111
+ const scopeChanged = scopeRef.current.contextKey !== scope?.contextKey || scopeRef.current.conversationId !== scope?.conversationId;
1112
+ const generation = generationRef.current + (scopeChanged ? 1 : 0);
1113
+ const occurrences = reconcileAttachments(scopeChanged ? [] : entriesRef.current, attachments, nextEntryRef.current);
1114
+ useLayoutEffect2(() => {
789
1115
  mountedRef.current = true;
790
1116
  return () => {
791
1117
  mountedRef.current = false;
792
1118
  };
793
1119
  }, []);
794
- useLayoutEffect(() => {
1120
+ useLayoutEffect2(() => {
1121
+ if (scopeChanged) {
1122
+ scopeRef.current = {
1123
+ contextKey: scope?.contextKey,
1124
+ conversationId: scope?.conversationId
1125
+ };
1126
+ generationRef.current = generation;
1127
+ submittingRef.current = false;
1128
+ setSubmitting(false);
1129
+ setSubmitError(undefined);
1130
+ setActiveId(undefined);
1131
+ setDismissed(false);
1132
+ revisionRef.current++;
1133
+ }
1134
+ }, [scopeChanged, scope?.contextKey, scope?.conversationId, generation]);
1135
+ useLayoutEffect2(() => {
795
1136
  if (observedValueRef.current !== value) {
796
1137
  revisionRef.current++;
797
1138
  observedValueRef.current = value;
798
1139
  }
799
- reconcileEntries(attachments);
1140
+ entriesRef.current = occurrences.entries;
1141
+ nextEntryRef.current = occurrences.nextId;
800
1142
  latestRef.current = {
801
1143
  value,
802
1144
  onValueChange,
@@ -806,40 +1148,17 @@ var ChatComposer = forwardRef2(({
806
1148
  clearOnSubmit
807
1149
  };
808
1150
  });
809
- const minimumRows = Math.max(1, Math.floor(minRows));
810
- const maximumRows = Math.max(minimumRows, Math.floor(maxRows));
811
- useLayoutEffect(() => {
812
- const editor = editorRef.current;
813
- if (!editor)
814
- return;
815
- function measure() {
816
- if (!editor)
817
- return;
818
- const style = getComputedStyle(editor);
819
- const lineHeight = parseFloat(style.lineHeight) || 24;
820
- const padding = (parseFloat(style.paddingTop) || 0) + (parseFloat(style.paddingBottom) || 0);
821
- const borders = (parseFloat(style.borderTopWidth) || 0) + (parseFloat(style.borderBottomWidth) || 0);
822
- const adjustment = style.boxSizing === "border-box" ? padding + borders : 0;
823
- editor.style.height = "auto";
824
- const minimum = minimumRows * lineHeight + adjustment;
825
- const maximum = maximumRows * lineHeight + adjustment;
826
- const content = editor.scrollHeight - padding + adjustment;
827
- editor.style.height = `${Math.min(maximum, Math.max(minimum, content))}px`;
828
- editor.style.overflowY = content > maximum ? "auto" : "hidden";
829
- }
830
- measure();
831
- const observer = typeof ResizeObserver === "undefined" ? undefined : new ResizeObserver(measure);
832
- observer?.observe(editor);
833
- window.addEventListener("resize", measure);
834
- return () => {
835
- observer?.disconnect();
836
- window.removeEventListener("resize", measure);
837
- };
838
- }, [value, minimumRows, maximumRows, textareaProps?.style]);
839
- const eligible = canSubmit && !disabled && !busy && !submitting && (value.trim().length > 0 || allowAttachmentOnly && attachments.length > 0);
1151
+ const eligible = legacyEligibility({
1152
+ value,
1153
+ attachments,
1154
+ allowAttachmentOnly,
1155
+ canSubmit,
1156
+ disabled,
1157
+ busy,
1158
+ submitting
1159
+ });
840
1160
  const candidates = completion?.items ?? [];
841
- const enabledCandidates = candidates.filter((item) => !item.disabled);
842
- const active = enabledCandidates.find((item) => item.id === activeId) ?? enabledCandidates[0];
1161
+ const { enabled, active } = legacyCompletion(candidates, activeId);
843
1162
  const showCompletion = !disabled && !dismissed && candidates.length > 0;
844
1163
  const visibleError = error ?? submitError;
845
1164
  const hasError = visibleError !== undefined && visibleError !== null && visibleError !== false;
@@ -848,27 +1167,31 @@ var ChatComposer = forwardRef2(({
848
1167
  pickerRef.current?.click();
849
1168
  }
850
1169
  function changeAttachments(files) {
851
- reconcileEntries(files);
1170
+ const next = reconcileAttachments(entriesRef.current, files, nextEntryRef.current);
1171
+ entriesRef.current = next.entries;
1172
+ nextEntryRef.current = next.nextId;
852
1173
  latestRef.current.attachments = files;
853
1174
  onAttachmentsChange?.(files);
854
1175
  }
855
- function removeAttachment(index) {
1176
+ function removeAttachment(entryId) {
856
1177
  if (disabled || !onAttachmentsChange)
857
1178
  return;
1179
+ const index = entriesRef.current.findIndex((entry) => entry.id === entryId);
1180
+ if (index < 0)
1181
+ return;
858
1182
  entriesRef.current.splice(index, 1);
859
1183
  changeAttachments(attachments.filter((_, fileIndex) => fileIndex !== index));
860
1184
  }
861
1185
  function selectCompletion(item) {
862
1186
  if (!completion || item.disabled || disabled)
863
1187
  return;
864
- const editor = editorRef.current;
865
- completion.onSelect(item, {
1188
+ completion.onSelect(item, editorRef.current?.selection(value) ?? {
866
1189
  value,
867
- selectionStart: editor?.selectionStart ?? value.length,
868
- selectionEnd: editor?.selectionEnd ?? value.length
1190
+ selectionStart: value.length,
1191
+ selectionEnd: value.length
869
1192
  });
870
1193
  setDismissed(true);
871
- editor?.focus();
1194
+ editorRef.current?.focus();
872
1195
  }
873
1196
  async function submit() {
874
1197
  if (!eligible || submittingRef.current)
@@ -876,237 +1199,120 @@ var ChatComposer = forwardRef2(({
876
1199
  submittingRef.current = true;
877
1200
  setSubmitting(true);
878
1201
  setSubmitError(undefined);
879
- const snapshot = { value, files: [...attachments] };
880
- const submittedRevision = revisionRef.current;
881
- const submittedEntries = new Set(entriesRef.current.map((entry) => entry.id));
1202
+ const snapshot = legacySnapshot(value, attachments, revisionRef.current, entriesRef.current);
1203
+ const submittedGeneration = generationRef.current;
1204
+ const isCurrent = () => mountedRef.current && generationRef.current === submittedGeneration;
882
1205
  try {
883
- const accepted = await onSubmit(snapshot);
884
- if (!mountedRef.current || accepted === false)
1206
+ const accepted = await onSubmit(snapshot.submission);
1207
+ if (!isCurrent() || accepted === false)
885
1208
  return;
886
1209
  const current = latestRef.current;
887
1210
  if (current.clearOnSubmit) {
888
- if (revisionRef.current === submittedRevision)
1211
+ if (revisionRef.current === snapshot.revision)
889
1212
  current.onValueChange("");
890
- const retained = entriesRef.current.filter((entry) => !submittedEntries.has(entry.id));
1213
+ if (!isCurrent())
1214
+ return;
1215
+ const retained = retainUnsubmittedAttachments(entriesRef.current, snapshot.entryIds);
891
1216
  if (retained.length !== entriesRef.current.length) {
892
1217
  entriesRef.current = retained;
893
1218
  current.onAttachmentsChange?.(retained.map((entry) => entry.file));
894
1219
  }
895
1220
  }
896
1221
  } catch (cause) {
897
- if (!mountedRef.current)
1222
+ if (!isCurrent())
898
1223
  return;
899
1224
  setSubmitError("Unable to send message. Please try again.");
900
1225
  latestRef.current.onSubmitError?.(cause);
901
1226
  } finally {
902
- submittingRef.current = false;
903
- if (mountedRef.current)
1227
+ if (isCurrent()) {
1228
+ submittingRef.current = false;
904
1229
  setSubmitting(false);
1230
+ }
905
1231
  }
906
1232
  }
907
- return /* @__PURE__ */ jsxs3("div", {
1233
+ return /* @__PURE__ */ jsx5(ComposerShell, {
908
1234
  ...props,
909
1235
  ref,
910
- className: cn("chat-composer", className),
911
- children: [
912
- attachments.length > 0 ? /* @__PURE__ */ jsx3("ul", {
913
- className: "chat-composer-attachments",
914
- "aria-label": "Attachments",
915
- children: attachments.map((file, index) => /* @__PURE__ */ jsx3("li", {
916
- className: "chat-composer-attachment",
917
- children: renderAttachment ? /* @__PURE__ */ jsx3(AttachmentContent, {
918
- file,
919
- renderAttachment,
920
- context: {
921
- remove: () => removeAttachment(index),
922
- disabled: disabled || !onAttachmentsChange
923
- }
924
- }) : /* @__PURE__ */ jsxs3(Fragment2, {
925
- children: [
926
- /* @__PURE__ */ jsx3(AttachmentPreview, {
927
- file
928
- }),
929
- /* @__PURE__ */ jsx3("span", {
930
- className: "chat-composer-filename",
931
- children: file.name
932
- }),
933
- onAttachmentsChange ? /* @__PURE__ */ jsx3(Button, {
934
- type: "button",
935
- size: "sm",
936
- color: "base",
937
- appearance: "ghost",
938
- disabled,
939
- "aria-label": `Remove ${file.name}`,
940
- onClick: () => removeAttachment(index),
941
- children: "Remove"
942
- }) : null
943
- ]
944
- })
945
- }, index))
946
- }) : null,
947
- /* @__PURE__ */ jsx3(Input.TextArea, {
948
- ...textareaProps,
949
- ref: editorRef,
950
- className: cn("chat-composer-editor", textareaProps?.className),
951
- value,
952
- disabled,
953
- rows: minimumRows,
954
- resize: "none",
955
- "aria-label": textareaProps?.["aria-label"] ?? (textareaProps?.["aria-labelledby"] ? undefined : "Message"),
956
- "aria-describedby": cn(textareaProps?.["aria-describedby"], hasError && `${id}-error`) || undefined,
957
- "aria-invalid": hasError ? true : textareaProps?.["aria-invalid"],
958
- "aria-controls": showCompletion ? `${id}-completions` : undefined,
959
- "aria-autocomplete": completion ? "list" : undefined,
960
- "aria-haspopup": completion ? "listbox" : undefined,
961
- "aria-activedescendant": showCompletion && active ? `${id}-option-${candidates.indexOf(active)}` : undefined,
962
- onChange: (event) => {
963
- revisionRef.current++;
964
- observedValueRef.current = event.currentTarget.value;
965
- setSubmitError(undefined);
966
- setDismissed(false);
967
- setActiveId(undefined);
968
- onValueChange(event.currentTarget.value);
969
- },
970
- onCompositionStart: (event) => {
971
- composingRef.current = true;
972
- textareaProps?.onCompositionStart?.(event);
973
- },
974
- onCompositionEnd: (event) => {
975
- composingRef.current = false;
976
- textareaProps?.onCompositionEnd?.(event);
977
- },
978
- onKeyDown: (event) => {
979
- textareaProps?.onKeyDown?.(event);
980
- if (event.defaultPrevented || composingRef.current || event.nativeEvent.isComposing || event.keyCode === 229)
1236
+ className,
1237
+ composerId: id,
1238
+ entries: occurrences.entries,
1239
+ renderAttachment,
1240
+ removeAttachment,
1241
+ addAttachments: (files) => changeAttachments([...attachments, ...files]),
1242
+ onAttachmentsChange,
1243
+ pickerRef,
1244
+ accept,
1245
+ multiple,
1246
+ disabled,
1247
+ busy,
1248
+ submitting,
1249
+ generating,
1250
+ eligible,
1251
+ completion,
1252
+ candidates,
1253
+ active,
1254
+ showCompletion,
1255
+ selectCompletion,
1256
+ visibleError,
1257
+ hasError,
1258
+ attachLabel,
1259
+ sendLabel,
1260
+ stopLabel,
1261
+ openAttachments,
1262
+ submit,
1263
+ onStop,
1264
+ editor: /* @__PURE__ */ jsx5(PlainEditor, {
1265
+ ref: editorRef,
1266
+ value,
1267
+ disabled,
1268
+ minRows,
1269
+ maxRows,
1270
+ generation,
1271
+ textareaProps,
1272
+ composerId: id,
1273
+ hasError,
1274
+ hasCompletion: !!completion,
1275
+ showCompletion,
1276
+ activeIndex: active ? candidates.indexOf(active) : -1,
1277
+ onChange: (nextValue) => {
1278
+ revisionRef.current++;
1279
+ observedValueRef.current = nextValue;
1280
+ setSubmitError(undefined);
1281
+ setDismissed(false);
1282
+ setActiveId(undefined);
1283
+ onValueChange(nextValue);
1284
+ },
1285
+ onKeyDown: (event) => {
1286
+ if (showCompletion) {
1287
+ if (event.key === "Escape") {
1288
+ event.preventDefault();
1289
+ setDismissed(true);
981
1290
  return;
982
- if (showCompletion) {
983
- if (event.key === "Escape") {
984
- event.preventDefault();
985
- setDismissed(true);
986
- return;
987
- }
988
- if (enabledCandidates.length > 0 && (event.key === "ArrowDown" || event.key === "ArrowUp")) {
989
- event.preventDefault();
990
- const position = enabledCandidates.findIndex((item) => item.id === active?.id);
991
- const direction = event.key === "ArrowDown" ? 1 : -1;
992
- setActiveId(enabledCandidates[(position + direction + enabledCandidates.length) % enabledCandidates.length].id);
993
- return;
994
- }
995
- if (active && !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey && (event.key === "Enter" || event.key === "Tab")) {
996
- event.preventDefault();
997
- selectCompletion(active);
998
- return;
999
- }
1000
1291
  }
1001
- if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
1292
+ if (enabled.length > 0 && (event.key === "ArrowDown" || event.key === "ArrowUp")) {
1002
1293
  event.preventDefault();
1003
- submit();
1294
+ setActiveId(moveLegacyCompletion(enabled, active?.id, event.key === "ArrowDown" ? 1 : -1));
1295
+ return;
1296
+ }
1297
+ if (active && !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey && (event.key === "Enter" || event.key === "Tab")) {
1298
+ event.preventDefault();
1299
+ selectCompletion(active);
1300
+ return;
1004
1301
  }
1005
1302
  }
1006
- }),
1007
- showCompletion ? /* @__PURE__ */ jsx3("ul", {
1008
- id: `${id}-completions`,
1009
- role: "listbox",
1010
- "aria-label": completion?.label ?? "Suggestions",
1011
- className: "chat-composer-completions",
1012
- children: candidates.map((item, index) => /* @__PURE__ */ jsxs3("li", {
1013
- id: `${id}-option-${index}`,
1014
- role: "option",
1015
- "aria-selected": item.id === active?.id,
1016
- "aria-disabled": item.disabled || undefined,
1017
- onMouseDown: (event) => event.preventDefault(),
1018
- onClick: () => selectCompletion(item),
1019
- onKeyDown: (event) => {
1020
- if (event.key === "Enter" || event.key === " ") {
1021
- event.preventDefault();
1022
- selectCompletion(item);
1023
- }
1024
- },
1025
- children: [
1026
- /* @__PURE__ */ jsx3("span", {
1027
- children: item.label
1028
- }),
1029
- item.description ? /* @__PURE__ */ jsx3("span", {
1030
- className: "chat-composer-description",
1031
- children: item.description
1032
- }) : null
1033
- ]
1034
- }, item.id))
1035
- }) : null,
1036
- hasError ? /* @__PURE__ */ jsx3("div", {
1037
- id: `${id}-error`,
1038
- role: "alert",
1039
- className: "chat-composer-error",
1040
- children: visibleError
1041
- }) : null,
1042
- /* @__PURE__ */ jsxs3("div", {
1043
- className: "chat-composer-actions",
1044
- children: [
1045
- onAttachmentsChange ? /* @__PURE__ */ jsxs3(Fragment2, {
1046
- children: [
1047
- /* @__PURE__ */ jsx3("input", {
1048
- ref: pickerRef,
1049
- type: "file",
1050
- accept,
1051
- multiple,
1052
- disabled,
1053
- hidden: true,
1054
- "aria-label": attachLabel,
1055
- onChange: (event) => {
1056
- const files = Array.from(event.currentTarget.files ?? []);
1057
- event.currentTarget.value = "";
1058
- if (files.length > 0)
1059
- changeAttachments([...attachments, ...files]);
1060
- }
1061
- }),
1062
- /* @__PURE__ */ jsx3(Button, {
1063
- type: "button",
1064
- color: "base",
1065
- appearance: "ghost",
1066
- disabled,
1067
- onClick: openAttachments,
1068
- children: attachLabel
1069
- })
1070
- ]
1071
- }) : null,
1072
- /* @__PURE__ */ jsx3(ExtraActions, {
1073
- context: {
1074
- submit: () => {
1075
- submit();
1076
- },
1077
- submitting,
1078
- canSubmit: eligible,
1079
- disabled,
1080
- generating,
1081
- openAttachments
1082
- },
1083
- children
1084
- }),
1085
- generating && onStop ? /* @__PURE__ */ jsx3(Button, {
1086
- type: "button",
1087
- disabled,
1088
- onClick: onStop,
1089
- children: stopLabel
1090
- }) : null,
1091
- /* @__PURE__ */ jsx3(Button, {
1092
- type: "button",
1093
- color: "primary",
1094
- disabled: !eligible,
1095
- "aria-busy": busy || submitting || undefined,
1096
- onClick: () => {
1097
- submit();
1098
- },
1099
- children: sendLabel
1100
- })
1101
- ]
1102
- })
1103
- ]
1303
+ if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
1304
+ event.preventDefault();
1305
+ submit();
1306
+ }
1307
+ }
1308
+ }),
1309
+ children
1104
1310
  });
1105
1311
  });
1106
1312
  ChatComposer.displayName = "Chat.Composer";
1107
1313
 
1108
1314
  // src/components/chat/ChatPrimitives.tsx
1109
- import { forwardRef as forwardRef3, useId as useId2 } from "react";
1315
+ import { forwardRef as forwardRef5, useId as useId2 } from "react";
1110
1316
 
1111
1317
  // src/classes/chat.ts
1112
1318
  var chatBaseClass = "chat";
@@ -1183,69 +1389,69 @@ function getChatToolClasses({
1183
1389
  }
1184
1390
 
1185
1391
  // src/components/chat/ChatPrimitives.tsx
1186
- import { jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
1187
- var ChatAgent = forwardRef3(({ timeline, className, ...props }, ref) => /* @__PURE__ */ jsx4("div", {
1392
+ import { jsx as jsx6, jsxs as jsxs4 } from "react/jsx-runtime";
1393
+ var ChatAgent = forwardRef5(({ timeline, className, ...props }, ref) => /* @__PURE__ */ jsx6("div", {
1188
1394
  ...props,
1189
1395
  ref,
1190
1396
  "data-chat-tl": timeline ?? props["data-chat-tl"],
1191
1397
  className: getChatClasses({ className: cn("chat-agent", className) })
1192
1398
  }));
1193
1399
  ChatAgent.displayName = "Chat.Agent";
1194
- var ChatContent = forwardRef3(({ streaming, className, ...props }, ref) => /* @__PURE__ */ jsx4("div", {
1400
+ var ChatContent = forwardRef5(({ streaming, className, ...props }, ref) => /* @__PURE__ */ jsx6("div", {
1195
1401
  ...props,
1196
1402
  ref,
1197
1403
  className: cn("chat-content", streaming && "chat-streaming", className)
1198
1404
  }));
1199
1405
  ChatContent.displayName = "Chat.Content";
1200
- var ChatWork = forwardRef3(({ className, ...props }, ref) => /* @__PURE__ */ jsx4("details", {
1406
+ var ChatWork = forwardRef5(({ className, ...props }, ref) => /* @__PURE__ */ jsx6("details", {
1201
1407
  ...props,
1202
1408
  ref,
1203
1409
  className: cn("chat-work", className)
1204
1410
  }));
1205
1411
  ChatWork.displayName = "Chat.Work";
1206
- var ChatWorkSummary = forwardRef3(({ className, ...props }, ref) => /* @__PURE__ */ jsx4("summary", {
1412
+ var ChatWorkSummary = forwardRef5(({ className, ...props }, ref) => /* @__PURE__ */ jsx6("summary", {
1207
1413
  ...props,
1208
1414
  ref,
1209
1415
  className: cn("chat-work-summary", className)
1210
1416
  }));
1211
1417
  ChatWorkSummary.displayName = "Chat.WorkSummary";
1212
- var ChatWorkBody = forwardRef3(({ className, ...props }, ref) => /* @__PURE__ */ jsx4("div", {
1418
+ var ChatWorkBody = forwardRef5(({ className, ...props }, ref) => /* @__PURE__ */ jsx6("div", {
1213
1419
  ...props,
1214
1420
  ref,
1215
1421
  className: cn("chat-work-body", className)
1216
1422
  }));
1217
1423
  ChatWorkBody.displayName = "Chat.WorkBody";
1218
- var ChatInputArea = forwardRef3(({ className, ...props }, ref) => /* @__PURE__ */ jsx4("div", {
1424
+ var ChatInputArea = forwardRef5(({ className, ...props }, ref) => /* @__PURE__ */ jsx6("div", {
1219
1425
  ...props,
1220
1426
  ref,
1221
1427
  className: cn("chat-input-area", className)
1222
1428
  }));
1223
1429
  ChatInputArea.displayName = "Chat.InputArea";
1224
- var ChatComposerForm = forwardRef3(({ color, ghost, className, ...props }, ref) => /* @__PURE__ */ jsx4("form", {
1430
+ var ChatComposerForm = forwardRef5(({ color, ghost, className, ...props }, ref) => /* @__PURE__ */ jsx6("form", {
1225
1431
  ...props,
1226
1432
  ref,
1227
1433
  className: cn("chat-composer", color && `chat-composer-${color}`, ghost && "chat-composer-ghost", className)
1228
1434
  }));
1229
1435
  ChatComposerForm.displayName = "Chat.ComposerForm";
1230
- var ChatComposerInput = forwardRef3(({ className, ...props }, ref) => /* @__PURE__ */ jsx4("textarea", {
1436
+ var ChatComposerInput = forwardRef5(({ className, ...props }, ref) => /* @__PURE__ */ jsx6("textarea", {
1231
1437
  ...props,
1232
1438
  ref,
1233
1439
  className: cn("chat-composer-input", className)
1234
1440
  }));
1235
1441
  ChatComposerInput.displayName = "Chat.ComposerInput";
1236
- var ChatComposerActions = forwardRef3(({ className, ...props }, ref) => /* @__PURE__ */ jsx4("div", {
1442
+ var ChatComposerActions = forwardRef5(({ className, ...props }, ref) => /* @__PURE__ */ jsx6("div", {
1237
1443
  ...props,
1238
1444
  ref,
1239
1445
  className: cn("chat-composer-actions", className)
1240
1446
  }));
1241
1447
  ChatComposerActions.displayName = "Chat.ComposerActions";
1242
- var ChatComposerHint = forwardRef3(({ className, ...props }, ref) => /* @__PURE__ */ jsx4("kbd", {
1448
+ var ChatComposerHint = forwardRef5(({ className, ...props }, ref) => /* @__PURE__ */ jsx6("kbd", {
1243
1449
  ...props,
1244
1450
  ref,
1245
1451
  className: cn("chat-composer-hint", className)
1246
1452
  }));
1247
1453
  ChatComposerHint.displayName = "Chat.ComposerHint";
1248
- var ChatQueue = forwardRef3(({
1454
+ var ChatQueue = forwardRef5(({
1249
1455
  items,
1250
1456
  activeId = items[0]?.id,
1251
1457
  pendingIds = [],
@@ -1268,7 +1474,7 @@ var ChatQueue = forwardRef3(({
1268
1474
  /* @__PURE__ */ jsxs4("div", {
1269
1475
  className: "chat-queue-header sr-only",
1270
1476
  children: [
1271
- /* @__PURE__ */ jsx4("h3", {
1477
+ /* @__PURE__ */ jsx6("h3", {
1272
1478
  className: "chat-queue-title",
1273
1479
  id: titleId,
1274
1480
  children: label
@@ -1282,7 +1488,7 @@ var ChatQueue = forwardRef3(({
1282
1488
  })
1283
1489
  ]
1284
1490
  }),
1285
- /* @__PURE__ */ jsx4("ol", {
1491
+ /* @__PURE__ */ jsx6("ol", {
1286
1492
  className: "chat-queue-list",
1287
1493
  role: "list",
1288
1494
  tabIndex: 0,
@@ -1293,14 +1499,14 @@ var ChatQueue = forwardRef3(({
1293
1499
  className: cn("chat-queue-item", item.id === activeId && "chat-queue-item-active"),
1294
1500
  "aria-busy": pending || undefined,
1295
1501
  children: [
1296
- /* @__PURE__ */ jsx4("svg", {
1502
+ /* @__PURE__ */ jsx6("svg", {
1297
1503
  className: "chat-queue-icon",
1298
1504
  viewBox: "0 0 24 24",
1299
1505
  fill: "none",
1300
1506
  stroke: "currentColor",
1301
1507
  "aria-hidden": "true",
1302
1508
  focusable: "false",
1303
- children: /* @__PURE__ */ jsx4("path", {
1509
+ children: /* @__PURE__ */ jsx6("path", {
1304
1510
  d: "m8 6 4 4-4 4M12 10H5m10-4h4m-4 6h4m-4 6h4"
1305
1511
  })
1306
1512
  }),
@@ -1311,12 +1517,12 @@ var ChatQueue = forwardRef3(({
1311
1517
  index + 1
1312
1518
  ]
1313
1519
  }),
1314
- item.attachment ? /* @__PURE__ */ jsx4("img", {
1520
+ item.attachment ? /* @__PURE__ */ jsx6("img", {
1315
1521
  className: "chat-queue-attachment",
1316
1522
  src: item.attachment.src,
1317
1523
  alt: item.attachment.alt
1318
1524
  }) : null,
1319
- /* @__PURE__ */ jsx4("div", {
1525
+ /* @__PURE__ */ jsx6("div", {
1320
1526
  className: "chat-queue-content",
1321
1527
  title: item.text,
1322
1528
  children: item.text
@@ -1331,7 +1537,7 @@ var ChatQueue = forwardRef3(({
1331
1537
  "aria-label": `Steer waiting message ${index + 1}`,
1332
1538
  onClick: () => onSteer(item.id),
1333
1539
  children: [
1334
- /* @__PURE__ */ jsx4("svg", {
1540
+ /* @__PURE__ */ jsx6("svg", {
1335
1541
  width: "16",
1336
1542
  height: "16",
1337
1543
  viewBox: "0 0 24 24",
@@ -1339,20 +1545,20 @@ var ChatQueue = forwardRef3(({
1339
1545
  stroke: "currentColor",
1340
1546
  "aria-hidden": "true",
1341
1547
  focusable: "false",
1342
- children: /* @__PURE__ */ jsx4("path", {
1548
+ children: /* @__PURE__ */ jsx6("path", {
1343
1549
  d: "m15 4 5 5-5 5M20 9h-9a6 6 0 0 0-6 6v4"
1344
1550
  })
1345
1551
  }),
1346
1552
  "Steer"
1347
1553
  ]
1348
1554
  }),
1349
- /* @__PURE__ */ jsx4("button", {
1555
+ /* @__PURE__ */ jsx6("button", {
1350
1556
  className: "btn btn-ghost btn-sm btn-square chat-queue-remove",
1351
1557
  type: "button",
1352
1558
  disabled: pending,
1353
1559
  "aria-label": `Remove waiting message ${index + 1}`,
1354
1560
  onClick: () => onRemove(item.id),
1355
- children: /* @__PURE__ */ jsx4("svg", {
1561
+ children: /* @__PURE__ */ jsx6("svg", {
1356
1562
  width: "16",
1357
1563
  height: "16",
1358
1564
  viewBox: "0 0 24 24",
@@ -1360,7 +1566,7 @@ var ChatQueue = forwardRef3(({
1360
1566
  stroke: "currentColor",
1361
1567
  "aria-hidden": "true",
1362
1568
  focusable: "false",
1363
- children: /* @__PURE__ */ jsx4("path", {
1569
+ children: /* @__PURE__ */ jsx6("path", {
1364
1570
  d: "M3 6h18M9 6V3h6v3M5 6l1 15h12l1-15M10 10v7m4-7v7"
1365
1571
  })
1366
1572
  })
@@ -1377,20 +1583,20 @@ var ChatQueue = forwardRef3(({
1377
1583
  ChatQueue.displayName = "Chat.Queue";
1378
1584
 
1379
1585
  // src/components/chat/Chat.tsx
1380
- import { jsx as jsx5, jsxs as jsxs5, Fragment as Fragment3 } from "react/jsx-runtime";
1381
- var ChatAvatar = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("div", {
1586
+ import { jsx as jsx7, jsxs as jsxs5, Fragment as Fragment3 } from "react/jsx-runtime";
1587
+ var ChatAvatar = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("div", {
1382
1588
  ...props,
1383
1589
  ref,
1384
1590
  className: cn(chatAvatarClass, className)
1385
1591
  }));
1386
1592
  ChatAvatar.displayName = "Chat.Avatar";
1387
- var ChatHeader = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("div", {
1593
+ var ChatHeader = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("div", {
1388
1594
  ...props,
1389
1595
  ref,
1390
1596
  className: cn(chatHeaderClass, className)
1391
1597
  }));
1392
1598
  ChatHeader.displayName = "Chat.Header";
1393
- var ChatBubble = forwardRef4(({
1599
+ var ChatBubble = forwardRef6(({
1394
1600
  color,
1395
1601
  size = "md",
1396
1602
  filled,
@@ -1404,14 +1610,14 @@ var ChatBubble = forwardRef4(({
1404
1610
  ...props
1405
1611
  }, ref) => {
1406
1612
  const contentId = `${useId3()}-content`;
1407
- const contentRef = useRef3(null);
1408
- const toggleRef = useRef3(null);
1409
- const collapseRequestedRef = useRef3(false);
1613
+ const contentRef = useRef5(null);
1614
+ const toggleRef = useRef5(null);
1615
+ const collapseRequestedRef = useRef5(false);
1410
1616
  const [overflows, setOverflows] = useState3(false);
1411
1617
  const [internalExpanded, setInternalExpanded] = useState3(defaultExpanded);
1412
1618
  const requestedExpanded = expanded ?? internalExpanded;
1413
1619
  const isExpanded = overflows && requestedExpanded;
1414
- useLayoutEffect2(() => {
1620
+ useLayoutEffect3(() => {
1415
1621
  const content = contentRef.current;
1416
1622
  if (!collapsible || !content) {
1417
1623
  collapseRequestedRef.current = false;
@@ -1456,13 +1662,13 @@ var ChatBubble = forwardRef4(({
1456
1662
  const body = /* @__PURE__ */ jsxs5(Fragment3, {
1457
1663
  children: [
1458
1664
  children,
1459
- streaming ? /* @__PURE__ */ jsx5("span", {
1665
+ streaming ? /* @__PURE__ */ jsx7("span", {
1460
1666
  className: "chat-bubble-content chat-bubble-streaming",
1461
1667
  "aria-hidden": "true"
1462
1668
  }) : null
1463
1669
  ]
1464
1670
  });
1465
- return /* @__PURE__ */ jsx5("div", {
1671
+ return /* @__PURE__ */ jsx7("div", {
1466
1672
  ...props,
1467
1673
  ref,
1468
1674
  className: getChatBubbleClasses({
@@ -1474,14 +1680,14 @@ var ChatBubble = forwardRef4(({
1474
1680
  }),
1475
1681
  children: collapsible ? /* @__PURE__ */ jsxs5(Fragment3, {
1476
1682
  children: [
1477
- /* @__PURE__ */ jsx5("div", {
1683
+ /* @__PURE__ */ jsx7("div", {
1478
1684
  ref: contentRef,
1479
1685
  className: "chat-bubble-content",
1480
1686
  id: contentId,
1481
1687
  tabIndex: -1,
1482
1688
  children: body
1483
1689
  }),
1484
- /* @__PURE__ */ jsx5("button", {
1690
+ /* @__PURE__ */ jsx7("button", {
1485
1691
  ref: toggleRef,
1486
1692
  type: "button",
1487
1693
  className: "btn btn-ghost btn-xs chat-bubble-toggle",
@@ -1500,55 +1706,55 @@ var ChatBubble = forwardRef4(({
1500
1706
  });
1501
1707
  });
1502
1708
  ChatBubble.displayName = "Chat.Bubble";
1503
- var ChatFooter = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("div", {
1709
+ var ChatFooter = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("div", {
1504
1710
  ...props,
1505
1711
  ref,
1506
1712
  className: cn(chatFooterClass, className)
1507
1713
  }));
1508
1714
  ChatFooter.displayName = "Chat.Footer";
1509
- var ChatStatus = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("div", {
1715
+ var ChatStatus = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("div", {
1510
1716
  ...props,
1511
1717
  ref,
1512
1718
  className: cn(chatStatusClass, className)
1513
1719
  }));
1514
1720
  ChatStatus.displayName = "Chat.Status";
1515
- var ChatStatusItem = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("span", {
1721
+ var ChatStatusItem = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("span", {
1516
1722
  ...props,
1517
1723
  ref,
1518
1724
  className: cn(chatStatusItemClass, className)
1519
1725
  }));
1520
1726
  ChatStatusItem.displayName = "Chat.StatusItem";
1521
- var ChatStatusValue = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("span", {
1727
+ var ChatStatusValue = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("span", {
1522
1728
  ...props,
1523
1729
  ref,
1524
1730
  className: cn(chatStatusValueClass, className)
1525
1731
  }));
1526
1732
  ChatStatusValue.displayName = "Chat.StatusValue";
1527
- var ChatActions = forwardRef4(({ hover, className, ...props }, ref) => /* @__PURE__ */ jsx5("div", {
1733
+ var ChatActions = forwardRef6(({ hover, className, ...props }, ref) => /* @__PURE__ */ jsx7("div", {
1528
1734
  ...props,
1529
1735
  ref,
1530
1736
  className: cn(chatActionsClass, hover && chatActionsHoverClass, className)
1531
1737
  }));
1532
1738
  ChatActions.displayName = "Chat.Actions";
1533
- var ChatScroll = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("div", {
1739
+ var ChatScroll = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("div", {
1534
1740
  ...props,
1535
1741
  ref,
1536
1742
  className: cn(chatScrollClass, className)
1537
1743
  }));
1538
1744
  ChatScroll.displayName = "Chat.Scroll";
1539
- var ChatScrollBody = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("div", {
1745
+ var ChatScrollBody = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("div", {
1540
1746
  ...props,
1541
1747
  ref,
1542
1748
  className: cn(chatScrollBodyClass, className)
1543
1749
  }));
1544
1750
  ChatScrollBody.displayName = "Chat.ScrollBody";
1545
- var ChatScrollTrack = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("div", {
1751
+ var ChatScrollTrack = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("div", {
1546
1752
  ...props,
1547
1753
  ref,
1548
1754
  className: cn(chatScrollTrackClass, className)
1549
1755
  }));
1550
1756
  ChatScrollTrack.displayName = "Chat.ScrollTrack";
1551
- var ChatScrollIndicator = forwardRef4(({ targetId, timeline, type = "button", className, onClick, ...props }, ref) => /* @__PURE__ */ jsx5("button", {
1757
+ var ChatScrollIndicator = forwardRef6(({ targetId, timeline, type = "button", className, onClick, ...props }, ref) => /* @__PURE__ */ jsx7("button", {
1552
1758
  ...props,
1553
1759
  ref,
1554
1760
  type,
@@ -1570,59 +1776,59 @@ var ChatScrollIndicator = forwardRef4(({ targetId, timeline, type = "button", cl
1570
1776
  }
1571
1777
  }));
1572
1778
  ChatScrollIndicator.displayName = "Chat.ScrollIndicator";
1573
- var ChatReasoning = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("details", {
1779
+ var ChatReasoning = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("details", {
1574
1780
  ...props,
1575
1781
  ref,
1576
1782
  className: cn(chatReasoningClass, className)
1577
1783
  }));
1578
1784
  ChatReasoning.displayName = "Chat.Reasoning";
1579
- var ChatTool = forwardRef4(({ status, className, ...props }, ref) => /* @__PURE__ */ jsx5("details", {
1785
+ var ChatTool = forwardRef6(({ status, className, ...props }, ref) => /* @__PURE__ */ jsx7("details", {
1580
1786
  ...props,
1581
1787
  ref,
1582
1788
  className: getChatToolClasses({ status, className })
1583
1789
  }));
1584
1790
  ChatTool.displayName = "Chat.Tool";
1585
- var ChatToolHeader = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("summary", {
1791
+ var ChatToolHeader = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("summary", {
1586
1792
  ...props,
1587
1793
  ref,
1588
1794
  className: cn(chatToolHeaderClass, className)
1589
1795
  }));
1590
1796
  ChatToolHeader.displayName = "Chat.ToolHeader";
1591
- var ChatToolStatus = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("span", {
1797
+ var ChatToolStatus = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("span", {
1592
1798
  ...props,
1593
1799
  ref,
1594
1800
  className: cn(chatToolStatusClass, className)
1595
1801
  }));
1596
1802
  ChatToolStatus.displayName = "Chat.ToolStatus";
1597
- var ChatToolCall = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("div", {
1803
+ var ChatToolCall = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("div", {
1598
1804
  ...props,
1599
1805
  ref,
1600
1806
  className: cn(chatToolCallClass, className)
1601
1807
  }));
1602
1808
  ChatToolCall.displayName = "Chat.ToolCall";
1603
- var ChatToolResult = forwardRef4(({ className, ...props }, ref) => /* @__PURE__ */ jsx5("div", {
1809
+ var ChatToolResult = forwardRef6(({ className, ...props }, ref) => /* @__PURE__ */ jsx7("div", {
1604
1810
  ...props,
1605
1811
  ref,
1606
1812
  className: cn(chatToolResultClass, className)
1607
1813
  }));
1608
1814
  ChatToolResult.displayName = "Chat.ToolResult";
1609
- var ChatTyping = forwardRef4(({
1815
+ var ChatTyping = forwardRef6(({
1610
1816
  className,
1611
1817
  role = "status",
1612
1818
  "aria-label": ariaLabel = "Assistant is typing",
1613
1819
  ...props
1614
- }, ref) => /* @__PURE__ */ jsx5("span", {
1820
+ }, ref) => /* @__PURE__ */ jsx7("span", {
1615
1821
  ...props,
1616
1822
  ref,
1617
1823
  role,
1618
1824
  "aria-label": ariaLabel,
1619
1825
  className: cn(chatTypingClass, className),
1620
- children: /* @__PURE__ */ jsx5("span", {
1826
+ children: /* @__PURE__ */ jsx7("span", {
1621
1827
  "aria-hidden": "true"
1622
1828
  })
1623
1829
  }));
1624
1830
  ChatTyping.displayName = "Chat.Typing";
1625
- var ChatRoot = forwardRef4(({ placement = "start", timeline, className, ...props }, ref) => /* @__PURE__ */ jsx5("div", {
1831
+ var ChatRoot = forwardRef6(({ placement = "start", timeline, className, ...props }, ref) => /* @__PURE__ */ jsx7("div", {
1626
1832
  ...props,
1627
1833
  ref,
1628
1834
  "data-chat-tl": timeline ?? props["data-chat-tl"],
@@ -1662,6 +1868,238 @@ var Chat = Object.assign(ChatRoot, {
1662
1868
  ToolResult: ChatToolResult,
1663
1869
  Typing: ChatTyping
1664
1870
  });
1871
+ // src/components/chat/composer/document.ts
1872
+ function object(value) {
1873
+ return value !== null && typeof value === "object" && !Array.isArray(value);
1874
+ }
1875
+ function keys(value, required, optional = []) {
1876
+ return required.every((key) => Object.hasOwn(value, key)) && Object.keys(value).every((key) => required.includes(key) || optional.includes(key));
1877
+ }
1878
+ function identity(value) {
1879
+ return typeof value === "string" && value.length > 0;
1880
+ }
1881
+ function revision(value) {
1882
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
1883
+ }
1884
+ function copyJson(value, ancestors = new Set) {
1885
+ if (value === null || typeof value === "string" || typeof value === "boolean")
1886
+ return value;
1887
+ if (typeof value === "number" && Number.isFinite(value))
1888
+ return value;
1889
+ if (typeof value !== "object" || value === null)
1890
+ throw new TypeError("Expected JSON data");
1891
+ if (ancestors.has(value))
1892
+ throw new TypeError("Cyclic data is not portable JSON");
1893
+ const prototype = Object.getPrototypeOf(value);
1894
+ if (Array.isArray(value) && prototype !== Array.prototype) {
1895
+ throw new TypeError("Only plain JSON arrays are supported");
1896
+ }
1897
+ if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null) {
1898
+ throw new TypeError("Only plain JSON objects are supported");
1899
+ }
1900
+ ancestors.add(value);
1901
+ try {
1902
+ const descriptors = Object.getOwnPropertyDescriptors(value);
1903
+ if (Reflect.ownKeys(value).some((key) => typeof key !== "string"))
1904
+ throw new TypeError("Symbol fields are not portable JSON");
1905
+ for (const [key, descriptor] of Object.entries(descriptors)) {
1906
+ if (Array.isArray(value) && key === "length")
1907
+ continue;
1908
+ if (!descriptor.enumerable || !("value" in descriptor))
1909
+ throw new TypeError("Non-JSON property descriptor");
1910
+ }
1911
+ if (Array.isArray(value)) {
1912
+ if (Object.keys(value).length !== value.length || Object.keys(value).some((key, index) => key !== String(index))) {
1913
+ throw new TypeError("Sparse arrays or array properties are not portable JSON");
1914
+ }
1915
+ const result = [];
1916
+ for (let index = 0;index < value.length; index++) {
1917
+ result.push(copyJson(descriptors[String(index)].value, ancestors));
1918
+ }
1919
+ return Object.freeze(result);
1920
+ }
1921
+ const result = {};
1922
+ for (const [key, descriptor] of Object.entries(descriptors)) {
1923
+ Object.defineProperty(result, key, {
1924
+ value: copyJson(descriptor.value, ancestors),
1925
+ enumerable: true
1926
+ });
1927
+ }
1928
+ return Object.freeze(result);
1929
+ } finally {
1930
+ ancestors.delete(value);
1931
+ }
1932
+ }
1933
+ function validScope(value) {
1934
+ return value !== undefined && object(value) && keys(value, ["contextKey", "conversationId"]) && identity(value.contextKey) && identity(value.conversationId);
1935
+ }
1936
+ function validCommand(value) {
1937
+ return value !== undefined && object(value) && keys(value, ["providerId", "commandId"]) && identity(value.providerId) && identity(value.commandId);
1938
+ }
1939
+ function validDocument(value) {
1940
+ if (!object(value) || !keys(value, ["schemaVersion", "content"], ["command"]) || value.schemaVersion !== 1 || !Array.isArray(value.content))
1941
+ return false;
1942
+ if (Object.hasOwn(value, "command") && !validCommand(value.command))
1943
+ return false;
1944
+ const ids = new Set;
1945
+ return value.content.every((node) => {
1946
+ if (!object(node))
1947
+ return false;
1948
+ if (node.type === "text")
1949
+ return keys(node, ["type", "text"]) && typeof node.text === "string";
1950
+ if (!identity(node.nodeId) || ids.has(node.nodeId))
1951
+ return false;
1952
+ ids.add(node.nodeId);
1953
+ if (node.type === "reference") {
1954
+ return keys(node, [
1955
+ "type",
1956
+ "nodeId",
1957
+ "providerId",
1958
+ "refId",
1959
+ "label",
1960
+ "plainText"
1961
+ ]) && identity(node.providerId) && identity(node.refId) && typeof node.label === "string" && typeof node.plainText === "string";
1962
+ }
1963
+ if (node.type === "quote") {
1964
+ if (!keys(node, ["type", "nodeId", "text"], ["source"]) || typeof node.text !== "string")
1965
+ return false;
1966
+ if (!Object.hasOwn(node, "source"))
1967
+ return true;
1968
+ return node.source !== undefined && object(node.source) && keys(node.source, [], ["providerId", "refId", "label", "messageId", "conversationId"]) && Object.values(node.source).every((field) => typeof field === "string");
1969
+ }
1970
+ return false;
1971
+ });
1972
+ }
1973
+ function validMetadata(value) {
1974
+ return value !== undefined && object(value) && keys(value, ["name", "mediaType", "size"], ["lastModified"]) && typeof value.name === "string" && typeof value.mediaType === "string" && revision(value.size) && (!Object.hasOwn(value, "lastModified") || revision(value.lastModified));
1975
+ }
1976
+ function validHandle(value) {
1977
+ return value !== undefined && object(value) && keys(value, ["providerId", "reference"]) && identity(value.providerId);
1978
+ }
1979
+ function validAttachments(value, draft) {
1980
+ if (!Array.isArray(value))
1981
+ return false;
1982
+ const ids = new Set;
1983
+ return value.every((entry) => {
1984
+ if (!object(entry) || !identity(entry.entryId) || ids.has(entry.entryId) || !validMetadata(entry.metadata))
1985
+ return false;
1986
+ ids.add(entry.entryId);
1987
+ if (!draft)
1988
+ return keys(entry, ["entryId", "metadata", "handle"]) && validHandle(entry.handle);
1989
+ return entry.status === "reattach" ? keys(entry, ["entryId", "metadata", "status"]) : entry.status === "ready" && keys(entry, ["entryId", "metadata", "status", "handle"]) && validHandle(entry.handle);
1990
+ });
1991
+ }
1992
+ function read(input, validate, reason) {
1993
+ try {
1994
+ const value = copyJson(input);
1995
+ if (!validate(value))
1996
+ return { status: "incompatible", input, reason };
1997
+ return { status: "compatible", value, migrated: false };
1998
+ } catch (error) {
1999
+ return {
2000
+ status: "incompatible",
2001
+ input,
2002
+ reason: error instanceof Error ? error.message : reason
2003
+ };
2004
+ }
2005
+ }
2006
+ function migrateLegacyComposerDocument(text) {
2007
+ return Object.freeze({
2008
+ schemaVersion: 1,
2009
+ content: Object.freeze([Object.freeze({ type: "text", text })])
2010
+ });
2011
+ }
2012
+ function readComposerDocument(input) {
2013
+ if (typeof input === "string")
2014
+ return {
2015
+ status: "compatible",
2016
+ value: migrateLegacyComposerDocument(input),
2017
+ migrated: true
2018
+ };
2019
+ return read(input, validDocument, "Unsupported or invalid ComposerDocument v1");
2020
+ }
2021
+ function snapshotComposerDocument(document2) {
2022
+ const result = read(document2, validDocument, "Unsupported or invalid ComposerDocument v1");
2023
+ if (result.status === "incompatible")
2024
+ throw new TypeError(result.reason);
2025
+ return result.value;
2026
+ }
2027
+ function readComposerDraft(input, legacy) {
2028
+ const migrated = typeof input === "string";
2029
+ const candidate = migrated ? {
2030
+ draftVersion: 1,
2031
+ scope: legacy.scope,
2032
+ draftKey: legacy.draftKey,
2033
+ revision: 0,
2034
+ document: migrateLegacyComposerDocument(input),
2035
+ attachments: []
2036
+ } : input;
2037
+ const result = read(candidate, (value) => object(value) && keys(value, [
2038
+ "draftVersion",
2039
+ "scope",
2040
+ "draftKey",
2041
+ "revision",
2042
+ "document",
2043
+ "attachments"
2044
+ ]) && value.draftVersion === 1 && validScope(value.scope) && identity(value.draftKey) && revision(value.revision) && value.document !== undefined && validDocument(value.document) && validAttachments(value.attachments, true), "Unsupported or invalid ComposerDraft v1");
2045
+ return result.status === "compatible" ? { ...result, migrated } : { ...result, input };
2046
+ }
2047
+ function snapshotComposerSubmission(submission) {
2048
+ const result = read(submission, (value) => {
2049
+ if (!object(value) || !keys(value, [
2050
+ "protocolVersion",
2051
+ "clientSubmissionId",
2052
+ "scope",
2053
+ "draftKey",
2054
+ "draftRevision",
2055
+ "operation",
2056
+ "document",
2057
+ "attachments",
2058
+ "options"
2059
+ ], ["intent", "targetTurnId"]) || value.protocolVersion !== 1 || !identity(value.clientSubmissionId) || !validScope(value.scope) || !identity(value.draftKey) || !revision(value.draftRevision) || value.document === undefined || !validDocument(value.document) || !validAttachments(value.attachments, false) || value.options === undefined || !object(value.options))
2060
+ return false;
2061
+ if (value.operation === "command")
2062
+ return object(value.document) && validCommand(value.document.command) && !Object.hasOwn(value, "intent") && !Object.hasOwn(value, "targetTurnId");
2063
+ if (value.operation !== "prompt")
2064
+ return false;
2065
+ return value.intent === "steer" ? identity(value.targetTurnId) : (value.intent === "send" || value.intent === "follow_up") && !Object.hasOwn(value, "targetTurnId");
2066
+ }, "Unsupported or invalid ComposerSubmission v1");
2067
+ if (result.status === "incompatible")
2068
+ throw new TypeError(result.reason);
2069
+ return result.value;
2070
+ }
2071
+ function exportComposerText(document2) {
2072
+ const snapshot = snapshotComposerDocument(document2);
2073
+ let output = "";
2074
+ let afterQuote = false;
2075
+ for (const node of snapshot.content) {
2076
+ const text = node.type === "reference" ? node.plainText : node.text;
2077
+ if (node.type === "quote") {
2078
+ if (output && !output.endsWith(`
2079
+ `))
2080
+ output += `
2081
+ `;
2082
+ output += text.split(/\r\n|\r|\n/).map((line) => `> ${line}`).join(`
2083
+ `);
2084
+ if (node.source?.label) {
2085
+ output += `
2086
+ ` + node.source.label.split(/\r\n|\r|\n/).map((line, index) => `> ${index === 0 ? "— " : ""}${line}`).join(`
2087
+ `);
2088
+ }
2089
+ afterQuote = true;
2090
+ } else {
2091
+ if (afterQuote && text && !text.startsWith(`
2092
+ `) && !text.startsWith("\r"))
2093
+ output += `
2094
+ `;
2095
+ output += text;
2096
+ if (text)
2097
+ afterQuote = false;
2098
+ }
2099
+ }
2100
+ return output;
2101
+ }
2102
+ var exportComposerMarkdown = exportComposerText;
1665
2103
  export {
1666
2104
  Chat,
1667
2105
  ChatActions,
@@ -1694,5 +2132,11 @@ export {
1694
2132
  ChatTyping,
1695
2133
  ChatWork,
1696
2134
  ChatWorkBody,
1697
- ChatWorkSummary
2135
+ ChatWorkSummary,
2136
+ exportComposerMarkdown,
2137
+ exportComposerText,
2138
+ readComposerDocument,
2139
+ readComposerDraft,
2140
+ snapshotComposerDocument,
2141
+ snapshotComposerSubmission
1698
2142
  };