@cheerlyai/agentlib-react 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -71,6 +71,13 @@ unsigned fact is worth no more than one typed into the chat box.
71
71
  | `placeholder?`, `welcomeTitle?`, `welcomeSubtitle?` | copy overrides |
72
72
  | `accentColor?` | user bubble / send button color |
73
73
  | `persistConversation?` | resume after reload (default `true`) |
74
+ | `attachments?` | let people attach files — paperclip, drag and drop, pasted screenshots (default `true`) |
75
+ | `accept?` | attachable types, `<input accept>` syntax (default: PNG/JPEG/WebP/GIF, PDF, text, Markdown, CSV, JSON) |
76
+
77
+ Attachments are checked in the browser against the API's caps (8 per message,
78
+ 10 MB each) before sending. Whether the agent can *read* a file is up to its
79
+ model: every catalog model takes images and PDFs, and text formats work
80
+ everywhere.
74
81
 
75
82
  Styles are self-contained (injected on mount, `alw-` prefixed, Shadow-DOM
76
83
  compatible) — no CSS import required.
package/dist/index.cjs CHANGED
@@ -445,12 +445,6 @@ var AGENT_CHAT_CSS = `
445
445
  margin: 8px 0 0;
446
446
  }
447
447
  .alw-powered a { color: inherit; text-decoration: none; }
448
- `;
449
- var STYLE_ATTR = "data-agentlib-styles";
450
- function injectStyles(root) {
451
- const target = root instanceof ShadowRoot ? root : (root?.ownerDocument ?? document).head;
452
- const holder = target;
453
- if (holder.querySelector?.(`style[${STYLE_ATTR}]
454
448
  .alw-sources {
455
449
  margin-top: 10px;
456
450
  padding: 8px 11px;
@@ -478,7 +472,88 @@ function injectStyles(root) {
478
472
  .alw-sources ol {
479
473
  padding-left: 18px;
480
474
  }
481
- `)) return;
475
+
476
+ /* For the drop overlay. Zero specificity, so a host that positions the root
477
+ * itself (fixed, absolute) keeps its layout whatever order the sheets load. */
478
+ :where(.alw-root) { position: relative; }
479
+ .alw-dragging::after {
480
+ content: "Drop files to attach";
481
+ position: absolute;
482
+ inset: 0;
483
+ display: flex;
484
+ align-items: center;
485
+ justify-content: center;
486
+ background: rgba(255, 255, 255, 0.9);
487
+ border: 2px dashed var(--alw-accent);
488
+ border-radius: inherit;
489
+ font-weight: 500;
490
+ pointer-events: none;
491
+ }
492
+
493
+ .alw-msg-files { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
494
+ .alw-msg-files:last-child { margin-bottom: 0; }
495
+ .alw-msg-image {
496
+ display: block;
497
+ max-width: 100%;
498
+ max-height: 180px;
499
+ border-radius: 10px;
500
+ object-fit: cover;
501
+ }
502
+ .alw-msg-file, .alw-pending-file {
503
+ display: inline-flex;
504
+ align-items: center;
505
+ gap: 6px;
506
+ max-width: 100%;
507
+ border-radius: 8px;
508
+ font-size: 12.5px;
509
+ }
510
+ .alw-msg-file { padding: 4px 8px; background: rgba(255, 255, 255, 0.18); }
511
+ .alw-file-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
512
+
513
+ .alw-pending { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 2px 8px; }
514
+ .alw-pending-file {
515
+ padding: 3px 4px 3px 6px;
516
+ max-width: 220px;
517
+ border: 1px solid var(--alw-border);
518
+ background: #fafafa;
519
+ color: var(--alw-text);
520
+ }
521
+ .alw-pending-thumb { width: 24px; height: 24px; border-radius: 4px; object-fit: cover; flex: none; }
522
+ .alw-pending-remove {
523
+ flex: none;
524
+ width: 20px; height: 20px;
525
+ border: 0;
526
+ border-radius: 50%;
527
+ background: none;
528
+ color: var(--alw-muted);
529
+ font: inherit;
530
+ font-size: 15px;
531
+ line-height: 1;
532
+ cursor: pointer;
533
+ }
534
+ .alw-pending-remove:hover { background: #ececec; color: var(--alw-text); }
535
+ .alw-attach-error { color: #dc2626; font-size: 12.5px; margin: 0 4px 8px; }
536
+
537
+ .alw-inputrow.alw-with-attach { padding-left: 6px; }
538
+ .alw-attach {
539
+ flex: none;
540
+ width: 32px; height: 32px;
541
+ border: 0;
542
+ border-radius: 50%;
543
+ background: none;
544
+ color: var(--alw-muted);
545
+ cursor: pointer;
546
+ display: inline-flex;
547
+ align-items: center;
548
+ justify-content: center;
549
+ }
550
+ .alw-attach:hover { background: #f5f5f5; color: var(--alw-text); }
551
+ `;
552
+ var STYLE_ATTR = "data-agentlib-styles";
553
+ function injectStyles(root) {
554
+ const target = root instanceof ShadowRoot ? root : (root?.ownerDocument ?? document).head;
555
+ const holder = target;
556
+ if (holder.querySelector?.(`style[${STYLE_ATTR}]`)) return;
482
557
  const style = document.createElement("style");
483
558
  style.setAttribute(STYLE_ATTR, "");
484
559
  style.textContent = AGENT_CHAT_CSS;
@@ -487,6 +562,20 @@ function injectStyles(root) {
487
562
 
488
563
  // src/AgentChat.tsx
489
564
  var import_jsx_runtime2 = require("react/jsx-runtime");
565
+ function hasFiles(e) {
566
+ return Array.from(e.dataTransfer.types).includes("Files");
567
+ }
568
+ function isPreviewableImage(part) {
569
+ return part.mediaType.startsWith("image/") && (part.url.startsWith("data:") || part.url.startsWith("https:"));
570
+ }
571
+ function errorText(error) {
572
+ try {
573
+ const detail = JSON.parse(error.message)?.detail;
574
+ if (typeof detail === "string" && detail) return detail;
575
+ } catch {
576
+ }
577
+ return error.message || "Something went wrong.";
578
+ }
490
579
  function readStored(key) {
491
580
  try {
492
581
  return localStorage.getItem(key);
@@ -504,6 +593,20 @@ function writeStored(key, value) {
504
593
  function messageText(message) {
505
594
  return message.parts.map((p) => p.type === "text" ? p.text : "").join("");
506
595
  }
596
+ var LABEL_BY_SUBTYPE = {
597
+ pdf: "PDF document",
598
+ markdown: "Markdown file",
599
+ csv: "CSV file",
600
+ plain: "Text file",
601
+ json: "JSON file"
602
+ };
603
+ function fileLabel(part) {
604
+ if (part.filename) return part.filename;
605
+ return LABEL_BY_SUBTYPE[part.mediaType.split("/")[1] ?? ""] ?? "Attachment";
606
+ }
607
+ function messageFiles(message) {
608
+ return message.parts.filter((p) => p.type === "file");
609
+ }
507
610
  function ThinkingDots() {
508
611
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "alw-msg alw-msg-assistant", children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "alw-bubble alw-thinking", "aria-label": "Thinking", children: [
509
612
  /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", {}),
@@ -511,6 +614,46 @@ function ThinkingDots() {
511
614
  /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", {})
512
615
  ] }) });
513
616
  }
617
+ function PaperclipIcon() {
618
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("svg", { width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", "aria-hidden": true, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
619
+ "path",
620
+ {
621
+ d: "m21 11.5-8.6 8.6a5.5 5.5 0 0 1-7.8-7.8l8.6-8.6a3.7 3.7 0 0 1 5.2 5.2l-8.6 8.6a1.8 1.8 0 0 1-2.6-2.6l8-8",
622
+ stroke: "currentColor",
623
+ strokeWidth: "2",
624
+ strokeLinecap: "round",
625
+ strokeLinejoin: "round"
626
+ }
627
+ ) });
628
+ }
629
+ function FileIcon() {
630
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("svg", { width: "14", height: "14", viewBox: "0 0 24 24", fill: "none", "aria-hidden": true, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
631
+ "path",
632
+ {
633
+ d: "M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8l-5-5Zm0 0v5h5",
634
+ stroke: "currentColor",
635
+ strokeWidth: "2",
636
+ strokeLinecap: "round",
637
+ strokeLinejoin: "round"
638
+ }
639
+ ) });
640
+ }
641
+ function MessageFiles({ files }) {
642
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "alw-msg-files", children: files.map(
643
+ (f, i) => isPreviewableImage(f) ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
644
+ "img",
645
+ {
646
+ className: "alw-msg-image",
647
+ src: f.url,
648
+ alt: f.filename ?? "Attached image"
649
+ },
650
+ i
651
+ ) : /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { className: "alw-msg-file", children: [
652
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(FileIcon, {}),
653
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "alw-file-name", children: fileLabel(f) })
654
+ ] }, i)
655
+ ) });
656
+ }
514
657
  function SendIcon() {
515
658
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", "aria-hidden": true, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
516
659
  "path",
@@ -541,6 +684,8 @@ function AgentChat({
541
684
  welcomeSubtitle = "Replies stream in as the agent works.",
542
685
  accentColor,
543
686
  persistConversation = true,
687
+ attachments = true,
688
+ accept = import_agentlib_protocol2.ATTACHMENT_ACCEPT,
544
689
  className
545
690
  }) {
546
691
  const api = `${baseUrl.replace(/\/$/, "")}/v1/embed`;
@@ -552,12 +697,18 @@ function AgentChat({
552
697
  const rootRef = (0, import_react3.useRef)(null);
553
698
  const scrollRef = (0, import_react3.useRef)(null);
554
699
  const inputRef = (0, import_react3.useRef)(null);
700
+ const fileInputRef = (0, import_react3.useRef)(null);
555
701
  const stickToBottomRef = (0, import_react3.useRef)(true);
702
+ const dragDepthRef = (0, import_react3.useRef)(0);
556
703
  const conversationIdRef = (0, import_react3.useRef)(
557
704
  persistConversation ? readStored(storageKey) : null
558
705
  );
559
706
  const [prompt, setPrompt] = (0, import_react3.useState)("");
560
707
  const [agentName, setAgentName] = (0, import_react3.useState)(null);
708
+ const [pending, setPending] = (0, import_react3.useState)([]);
709
+ const [reading, setReading] = (0, import_react3.useState)(0);
710
+ const [attachError, setAttachError] = (0, import_react3.useState)(null);
711
+ const [dragging, setDragging] = (0, import_react3.useState)(false);
561
712
  const { messages, sendMessage, status, error, setMessages, clearError, stop } = (0, import_react2.useChat)({
562
713
  transport: new import_ai.DefaultChatTransport({
563
714
  api: `${api}/agents/${encodeURIComponent(agentId)}/chat`,
@@ -624,17 +775,88 @@ function AgentChat({
624
775
  el.style.height = "auto";
625
776
  el.style.height = `${Math.min(el.scrollHeight, 140)}px`;
626
777
  }
778
+ const canSend = (!!prompt.trim() || pending.length > 0) && !busy && reading === 0;
627
779
  function send() {
628
780
  const p = prompt.trim();
629
- if (!p || busy) return;
781
+ if (!canSend) return;
782
+ const files = pending;
630
783
  setPrompt("");
784
+ setPending([]);
785
+ setAttachError(null);
631
786
  requestAnimationFrame(autoGrow);
632
- void sendMessage({ text: p });
787
+ void sendMessage(
788
+ files.length === 0 ? { text: p } : p ? { text: p, files } : { files }
789
+ );
790
+ }
791
+ async function addFiles(files) {
792
+ if (!attachments || files.length === 0) return;
793
+ const problems = [];
794
+ const accepted = [];
795
+ let room = import_agentlib_protocol2.MAX_ATTACHMENTS - pending.length - reading;
796
+ for (const file of files) {
797
+ const problem = (0, import_agentlib_protocol2.attachmentProblem)(file, accept);
798
+ if (problem) {
799
+ problems.push(problem);
800
+ } else if (room <= 0) {
801
+ problems.push(`You can attach up to ${import_agentlib_protocol2.MAX_ATTACHMENTS} files per message.`);
802
+ break;
803
+ } else {
804
+ accepted.push(file);
805
+ room -= 1;
806
+ }
807
+ }
808
+ setAttachError(problems.length ? problems.join(" ") : null);
809
+ if (accepted.length === 0) return;
810
+ setReading((n) => n + accepted.length);
811
+ const read = await Promise.allSettled(accepted.map(import_agentlib_protocol2.toAttachmentPart));
812
+ setReading((n) => n - accepted.length);
813
+ const parts = [];
814
+ read.forEach((result, i) => {
815
+ if (result.status === "fulfilled") parts.push(result.value);
816
+ else problems.push(`Couldn't read ${accepted[i].name}.`);
817
+ });
818
+ setPending((prev) => [...prev, ...parts]);
819
+ if (problems.length) setAttachError(problems.join(" "));
633
820
  }
821
+ function removePending(index) {
822
+ setPending((prev) => prev.filter((_, i) => i !== index));
823
+ setAttachError(null);
824
+ }
825
+ function onPaste(e) {
826
+ if (!attachments) return;
827
+ const files = Array.from(e.clipboardData.files);
828
+ if (files.length === 0) return;
829
+ if (!e.clipboardData.getData("text/plain")) e.preventDefault();
830
+ void addFiles(files);
831
+ }
832
+ const dropHandlers = attachments ? {
833
+ onDragEnter: (e) => {
834
+ if (!hasFiles(e)) return;
835
+ dragDepthRef.current += 1;
836
+ setDragging(true);
837
+ },
838
+ onDragOver: (e) => {
839
+ if (hasFiles(e)) e.preventDefault();
840
+ },
841
+ onDragLeave: (e) => {
842
+ if (!hasFiles(e)) return;
843
+ dragDepthRef.current = Math.max(0, dragDepthRef.current - 1);
844
+ if (dragDepthRef.current === 0) setDragging(false);
845
+ },
846
+ onDrop: (e) => {
847
+ if (!hasFiles(e)) return;
848
+ e.preventDefault();
849
+ dragDepthRef.current = 0;
850
+ setDragging(false);
851
+ void addFiles(Array.from(e.dataTransfer.files));
852
+ }
853
+ } : {};
634
854
  function reset() {
635
855
  stop();
636
856
  setMessages([]);
637
857
  clearError();
858
+ setPending([]);
859
+ setAttachError(null);
638
860
  conversationIdRef.current = null;
639
861
  writeStored(storageKey, null);
640
862
  inputRef.current?.focus();
@@ -643,8 +865,9 @@ function AgentChat({
643
865
  "div",
644
866
  {
645
867
  ref: rootRef,
646
- className: className ? `alw-root ${className}` : "alw-root",
868
+ className: ["alw-root", dragging && "alw-dragging", className].filter(Boolean).join(" "),
647
869
  style: accentColor ? { "--alw-accent": accentColor } : void 0,
870
+ ...dropHandlers,
648
871
  children: [
649
872
  /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "alw-header", children: [
650
873
  /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: "alw-title", children: title ?? agentName ?? "Chat" }),
@@ -666,6 +889,7 @@ function AgentChat({
666
889
  messages.map((m) => {
667
890
  const text = messageText(m);
668
891
  if (!text && m.role === "assistant") return null;
892
+ const files = m.role === "user" ? messageFiles(m) : [];
669
893
  const { body, sources } = m.role === "assistant" ? (0, import_agentlib_protocol2.splitReply)(text) : { body: text, sources: null };
670
894
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
671
895
  "div",
@@ -697,19 +921,64 @@ function AgentChat({
697
921
  }
698
922
  )
699
923
  ] })
700
- ] }) : text })
924
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
925
+ files.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(MessageFiles, { files }),
926
+ text
927
+ ] }) })
701
928
  },
702
929
  m.id
703
930
  );
704
931
  }),
705
932
  status === "submitted" && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(ThinkingDots, {}),
706
933
  error && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("p", { className: "alw-error", children: [
707
- error.message || "Something went wrong.",
934
+ errorText(error),
708
935
  /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("button", { type: "button", onClick: clearError, children: "Dismiss" })
709
936
  ] })
710
937
  ] }) }),
711
938
  /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "alw-composer", children: [
712
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "alw-inputrow", children: [
939
+ pending.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "alw-pending", children: pending.map((f, i) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { className: "alw-pending-file", children: [
940
+ isPreviewableImage(f) ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("img", { className: "alw-pending-thumb", src: f.url, alt: "" }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(FileIcon, {}),
941
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "alw-file-name", children: f.filename }),
942
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
943
+ "button",
944
+ {
945
+ type: "button",
946
+ className: "alw-pending-remove",
947
+ onClick: () => removePending(i),
948
+ "aria-label": `Remove ${f.filename ?? "file"}`,
949
+ children: "\xD7"
950
+ }
951
+ )
952
+ ] }, i)) }),
953
+ attachError && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { className: "alw-attach-error", role: "alert", children: attachError }),
954
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: attachments ? "alw-inputrow alw-with-attach" : "alw-inputrow", children: [
955
+ attachments && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(import_jsx_runtime2.Fragment, { children: [
956
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
957
+ "input",
958
+ {
959
+ ref: fileInputRef,
960
+ type: "file",
961
+ multiple: true,
962
+ accept,
963
+ hidden: true,
964
+ onChange: (e) => {
965
+ void addFiles(Array.from(e.target.files ?? []));
966
+ e.target.value = "";
967
+ }
968
+ }
969
+ ),
970
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
971
+ "button",
972
+ {
973
+ type: "button",
974
+ className: "alw-attach",
975
+ onClick: () => fileInputRef.current?.click(),
976
+ "aria-label": "Attach files",
977
+ title: "Attach files",
978
+ children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(PaperclipIcon, {})
979
+ }
980
+ )
981
+ ] }),
713
982
  /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
714
983
  "textarea",
715
984
  {
@@ -727,7 +996,8 @@ function AgentChat({
727
996
  e.preventDefault();
728
997
  send();
729
998
  }
730
- }
999
+ },
1000
+ onPaste
731
1001
  }
732
1002
  ),
733
1003
  /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
@@ -736,7 +1006,7 @@ function AgentChat({
736
1006
  type: "button",
737
1007
  className: "alw-send",
738
1008
  onClick: send,
739
- disabled: !prompt.trim() || busy,
1009
+ disabled: !canSend,
740
1010
  "aria-label": "Send",
741
1011
  children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(SendIcon, {})
742
1012
  }