@locusgraph/loop 0.1.0 → 0.2.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.
Files changed (115) hide show
  1. package/README.md +2 -0
  2. package/dist/agent/pulse/gradient-label.js +36 -15
  3. package/dist/agent/pulse/marks/bars-mark.js +24 -10
  4. package/dist/agent/pulse/marks/glyph-mark.js +17 -11
  5. package/dist/agent/pulse/marks/locus-mark.js +39 -28
  6. package/dist/agent/pulse/marks/orb-mark.js +21 -8
  7. package/dist/agent/pulse/marks/spinner-mark.js +20 -1
  8. package/dist/agent/pulse/pulse.js +63 -35
  9. package/dist/agent/pulse/use-loop-animation.js +14 -16
  10. package/dist/agent/steps/step-icon.js +40 -24
  11. package/dist/agent/steps/steps.js +72 -25
  12. package/dist/agent/thinking/thinking.js +203 -23
  13. package/dist/agent/thinking/thought-duration.js +7 -9
  14. package/dist/agent/tool/json-block.js +18 -11
  15. package/dist/agent/tool/tool-status.js +28 -18
  16. package/dist/agent/tool/tool.js +184 -6
  17. package/dist/approval/inbox/inbox-row.d.ts +3 -2
  18. package/dist/approval/inbox/inbox-row.js +96 -7
  19. package/dist/approval/inbox/inbox-selection.js +7 -13
  20. package/dist/approval/inbox/inbox.d.ts +2 -1
  21. package/dist/approval/inbox/inbox.js +99 -18
  22. package/dist/approval/inbox/virtual-inbox-list.d.ts +13 -0
  23. package/dist/approval/inbox/virtual-inbox-list.js +53 -0
  24. package/dist/approval/memory/memory-choices.js +27 -22
  25. package/dist/approval/memory/memory-review.js +108 -17
  26. package/dist/approval/memory/memory-row.js +80 -19
  27. package/dist/approval/plan/plan-review.js +129 -28
  28. package/dist/approval/plan/plan-step-row.js +91 -44
  29. package/dist/approval/plan/plan-steps.js +24 -23
  30. package/dist/approval/plan/question-choices.js +73 -40
  31. package/dist/approval/plan/question.d.ts +1 -1
  32. package/dist/approval/plan/question.js +108 -27
  33. package/dist/approval/run/run-bar.js +94 -22
  34. package/dist/approval/shared/decision-line.js +1 -1
  35. package/dist/approval/shared/risk-badge.js +32 -16
  36. package/dist/approval/shared/time.js +5 -8
  37. package/dist/approval/shared/use-focus-on-mount.js +7 -6
  38. package/dist/approval/spend/raise-cap-form.js +64 -19
  39. package/dist/approval/spend/spend-gate.js +124 -21
  40. package/dist/approval/spend/spend-math.js +7 -8
  41. package/dist/approval/spend/spend-meter.js +136 -35
  42. package/dist/approval/tool/approval.js +113 -27
  43. package/dist/approval/tool/args/args-editor.js +104 -26
  44. package/dist/approval/tool/args/args-field.js +79 -22
  45. package/dist/approval/tool/args/args-validation.js +38 -47
  46. package/dist/approval/tool/deny-form.js +43 -10
  47. package/dist/approval/tool/tool-inputs.js +62 -38
  48. package/dist/chat/chat/chat-item-spacing.d.ts +3 -0
  49. package/dist/chat/chat/chat-item-spacing.js +19 -0
  50. package/dist/chat/chat/chat.d.ts +2 -1
  51. package/dist/chat/chat/chat.js +181 -5
  52. package/dist/chat/chat/use-stick-to-bottom.js +71 -48
  53. package/dist/chat/chat/virtual-chat-items.d.ts +8 -0
  54. package/dist/chat/chat/virtual-chat-items.js +60 -0
  55. package/dist/chat/composer/composer-actions.js +69 -17
  56. package/dist/chat/composer/composer.js +330 -32
  57. package/dist/chat/composer/use-auto-grow.js +9 -9
  58. package/dist/chat/files/file-meta.js +19 -19
  59. package/dist/chat/files/files.js +92 -29
  60. package/dist/chat/reply/fenced-code.js +23 -21
  61. package/dist/chat/reply/markdown-components.js +137 -17
  62. package/dist/chat/reply/reply.js +30 -10
  63. package/dist/chat/turn/group-parts.js +43 -29
  64. package/dist/chat/turn/turn.js +87 -30
  65. package/dist/cost/context.js +104 -23
  66. package/dist/cost/cost-steps.js +67 -14
  67. package/dist/cost/cost.js +128 -23
  68. package/dist/cost/model-picker.js +51 -6
  69. package/dist/cost/shared/context-breakdown.js +83 -10
  70. package/dist/cost/shared/meter-marks.js +49 -9
  71. package/dist/cost/shared/model-option-row.js +56 -22
  72. package/dist/cost/shared/step-cost-row.js +65 -22
  73. package/dist/cost/shared/usage-totals.js +22 -18
  74. package/dist/memory/cite.js +62 -12
  75. package/dist/memory/memorized.js +65 -11
  76. package/dist/memory/recall.js +101 -17
  77. package/dist/memory/shared/host-label.js +19 -22
  78. package/dist/memory/shared/source-mark.js +11 -3
  79. package/dist/memory/sources.js +78 -14
  80. package/dist/shared/decision-summary.js +51 -14
  81. package/dist/shared/panel-empty.js +20 -3
  82. package/dist/shared/panel.js +69 -13
  83. package/dist/shared/status-dot.js +16 -8
  84. package/dist/shared/virtual/use-list-virtualizer.d.ts +24 -0
  85. package/dist/shared/virtual/use-list-virtualizer.js +68 -0
  86. package/dist/thread/actions/action-button.js +22 -3
  87. package/dist/thread/actions/actions.js +184 -13
  88. package/dist/thread/actions/branch-switcher.js +28 -5
  89. package/dist/thread/canvas/canvas.js +205 -5
  90. package/dist/thread/canvas/version-switcher.js +54 -8
  91. package/dist/thread/code/code.js +186 -24
  92. package/dist/thread/code/copy-code-button.js +30 -14
  93. package/dist/thread/code/highlight.js +30 -29
  94. package/dist/thread/history/history-rows.d.ts +16 -0
  95. package/dist/thread/history/history-rows.js +23 -0
  96. package/dist/thread/history/history.d.ts +3 -2
  97. package/dist/thread/history/history.js +174 -5
  98. package/dist/thread/history/{history-row.d.ts → row/history-row.d.ts} +3 -2
  99. package/dist/thread/history/row/history-row.js +58 -0
  100. package/dist/thread/history/row/thread-item-menu.js +31 -0
  101. package/dist/thread/history/row/thread-title-editor.js +38 -0
  102. package/dist/thread/history/thread-groups.d.ts +1 -1
  103. package/dist/thread/history/thread-groups.js +40 -34
  104. package/dist/thread/history/virtual-history-list.d.ts +11 -0
  105. package/dist/thread/history/virtual-history-list.js +71 -0
  106. package/dist/thread/index.d.ts +1 -1
  107. package/dist/thread/starters/starters.js +33 -8
  108. package/dist/thread/use-copied.js +18 -16
  109. package/dist/types.js +1 -1
  110. package/package.json +6 -4
  111. package/dist/thread/history/history-row.js +0 -17
  112. package/dist/thread/history/thread-item-menu.js +0 -6
  113. package/dist/thread/history/thread-title-editor.js +0 -25
  114. /package/dist/thread/history/{thread-item-menu.d.ts → row/thread-item-menu.d.ts} +0 -0
  115. /package/dist/thread/history/{thread-title-editor.d.ts → row/thread-title-editor.d.ts} +0 -0
@@ -1,59 +1,50 @@
1
1
  export function draftsFrom(fields, initialValues) {
2
- return Object.fromEntries(fields.map((field) => {
3
- const initial = initialValues[field.name];
4
- if (field.type === "boolean")
5
- return [field.name, initial === true];
6
- return [field.name, initial === undefined ? "" : String(initial)];
7
- }));
2
+ return Object.fromEntries(fields.map(field => {
3
+ const initial = initialValues[field.name];
4
+ if (field.type === "boolean") return [field.name, initial === true];
5
+ return [field.name, initial === undefined ? "" : String(initial)];
6
+ }));
8
7
  }
9
8
  function numberError(field, draft) {
10
- const value = Number(draft);
11
- if (!Number.isFinite(value))
12
- return `${field.label} must be a number.`;
13
- if (field.min !== undefined && value < field.min) {
14
- return `${field.label} must be at least ${field.min}.`;
15
- }
16
- if (field.max !== undefined && value > field.max) {
17
- return `${field.label} must be at most ${field.max}.`;
18
- }
19
- return undefined;
9
+ const value = Number(draft);
10
+ if (!Number.isFinite(value)) return `${field.label} must be a number.`;
11
+ if (field.min !== undefined && value < field.min) {
12
+ return `${field.label} must be at least ${field.min}.`;
13
+ }
14
+ if (field.max !== undefined && value > field.max) {
15
+ return `${field.label} must be at most ${field.max}.`;
16
+ }
17
+ return undefined;
20
18
  }
21
19
  export function fieldError(field, draft) {
22
- if (field.type === "boolean")
23
- return undefined;
24
- const text = typeof draft === "string" ? draft.trim() : "";
25
- if (text === "")
26
- return field.required ? `${field.label} is required.` : undefined;
27
- if (field.type === "number")
28
- return numberError(field, text);
29
- if (field.type === "select" && !field.options?.some((option) => option.value === text)) {
30
- return `Choose one of the listed ${field.label.toLowerCase()} options.`;
31
- }
32
- return undefined;
20
+ if (field.type === "boolean") return undefined;
21
+ const text = typeof draft === "string" ? draft.trim() : "";
22
+ if (text === "") return field.required ? `${field.label} is required.` : undefined;
23
+ if (field.type === "number") return numberError(field, text);
24
+ if (field.type === "select" && !field.options?.some(option => option.value === text)) {
25
+ return `Choose one of the listed ${field.label.toLowerCase()} options.`;
26
+ }
27
+ return undefined;
33
28
  }
34
29
  export function validateDrafts(fields, drafts) {
35
- const errors = {};
36
- for (const field of fields) {
37
- const error = fieldError(field, drafts[field.name]);
38
- if (error)
39
- errors[field.name] = error;
40
- }
41
- return errors;
30
+ const errors = {};
31
+ for (const field of fields) {
32
+ const error = fieldError(field, drafts[field.name]);
33
+ if (error) errors[field.name] = error;
34
+ }
35
+ return errors;
42
36
  }
43
37
  function valueFromDraft(field, draft) {
44
- if (field.type === "boolean")
45
- return draft === true;
46
- const text = typeof draft === "string" ? draft.trim() : "";
47
- if (text === "")
48
- return undefined;
49
- return field.type === "number" ? Number(text) : text;
38
+ if (field.type === "boolean") return draft === true;
39
+ const text = typeof draft === "string" ? draft.trim() : "";
40
+ if (text === "") return undefined;
41
+ return field.type === "number" ? Number(text) : text;
50
42
  }
51
43
  export function valuesFromDrafts(fields, drafts) {
52
- const values = {};
53
- for (const field of fields) {
54
- const value = valueFromDraft(field, drafts[field.name]);
55
- if (value !== undefined)
56
- values[field.name] = value;
57
- }
58
- return values;
44
+ const values = {};
45
+ for (const field of fields) {
46
+ const value = valueFromDraft(field, drafts[field.name]);
47
+ if (value !== undefined) values[field.name] = value;
48
+ }
49
+ return values;
59
50
  }
@@ -1,15 +1,48 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
1
  import { Button, Label, Textarea } from "@locusgraph/components";
3
2
  import { useId, useState } from "react";
4
3
  import { PanelBody, PanelFooter } from "../../shared/panel.js";
5
4
  import { useFocusOnMount } from "../shared/use-focus-on-mount.js";
6
- export function DenyForm({ onDeny, onBack }) {
7
- const [reason, setReason] = useState("");
8
- const reasonId = useId();
9
- const reasonRef = useFocusOnMount(true);
10
- const trimmed = reason.trim();
11
- return (_jsxs("form", { "data-slot": "deny-form", onSubmit: (event) => {
12
- event.preventDefault();
13
- onDeny(trimmed === "" ? undefined : trimmed);
14
- }, children: [_jsxs(PanelBody, { className: "gap-2 pt-0", children: [_jsx(Label, { htmlFor: reasonId, className: "text-xs font-normal text-muted-foreground", children: "Reason for the agent (optional)" }), _jsx(Textarea, { id: reasonId, ref: reasonRef, value: reason, placeholder: "Tell the agent what to do instead", onChange: (event) => setReason(event.target.value) })] }), _jsxs(PanelFooter, { children: [_jsx(Button, { type: "button", variant: "ghost", size: "sm", onClick: onBack, children: "Back" }), _jsx(Button, { type: "submit", variant: "destructive", size: "sm", children: "Send denial" })] })] }));
5
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
6
+ export function DenyForm({
7
+ onDeny,
8
+ onBack
9
+ }) {
10
+ const [reason, setReason] = useState("");
11
+ const reasonId = useId();
12
+ const reasonRef = useFocusOnMount(true);
13
+ const trimmed = reason.trim();
14
+ return /*#__PURE__*/_jsxs("form", {
15
+ "data-slot": "deny-form",
16
+ onSubmit: event => {
17
+ event.preventDefault();
18
+ onDeny(trimmed === "" ? undefined : trimmed);
19
+ },
20
+ children: [/*#__PURE__*/_jsxs(PanelBody, {
21
+ className: "gap-2 pt-0",
22
+ children: [/*#__PURE__*/_jsx(Label, {
23
+ htmlFor: reasonId,
24
+ className: "text-xs font-normal text-muted-foreground",
25
+ children: "Reason for the agent (optional)"
26
+ }), /*#__PURE__*/_jsx(Textarea, {
27
+ id: reasonId,
28
+ ref: reasonRef,
29
+ value: reason,
30
+ placeholder: "Tell the agent what to do instead",
31
+ onChange: event => setReason(event.target.value)
32
+ })]
33
+ }), /*#__PURE__*/_jsxs(PanelFooter, {
34
+ children: [/*#__PURE__*/_jsx(Button, {
35
+ type: "button",
36
+ variant: "ghost",
37
+ size: "sm",
38
+ onClick: onBack,
39
+ children: "Back"
40
+ }), /*#__PURE__*/_jsx(Button, {
41
+ type: "submit",
42
+ variant: "destructive",
43
+ size: "sm",
44
+ children: "Send denial"
45
+ })]
46
+ })]
47
+ });
15
48
  }
@@ -1,53 +1,77 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
1
  import { cn } from "@locusgraph/components";
3
2
  import { useId, useState } from "react";
3
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
4
4
  function isPlainRecord(value) {
5
- return typeof value === "object" && value !== null && !Array.isArray(value);
5
+ return typeof value === "object" && value !== null && !Array.isArray(value);
6
6
  }
7
7
  function isPrimitive(value) {
8
- return value === null || ["string", "number", "boolean"].includes(typeof value);
8
+ return value === null || ["string", "number", "boolean"].includes(typeof value);
9
9
  }
10
10
  export function readableValue(value) {
11
- if (value === null || value === undefined)
12
- return "None";
13
- if (typeof value === "boolean")
14
- return value ? "Yes" : "No";
15
- if (typeof value === "string" || typeof value === "number")
16
- return String(value);
17
- if (Array.isArray(value) && value.every(isPrimitive)) {
18
- return value.length === 0 ? "None" : value.map(readableValue).join(", ");
19
- }
20
- return JSON.stringify(value);
11
+ if (value === null || value === undefined) return "None";
12
+ if (typeof value === "boolean") return value ? "Yes" : "No";
13
+ if (typeof value === "string" || typeof value === "number") return String(value);
14
+ if (Array.isArray(value) && value.every(isPrimitive)) {
15
+ return value.length === 0 ? "None" : value.map(readableValue).join(", ");
16
+ }
17
+ return JSON.stringify(value);
21
18
  }
22
19
  const acronyms = {
23
- api: "API",
24
- csv: "CSV",
25
- id: "ID",
26
- ids: "IDs",
27
- json: "JSON",
28
- pdf: "PDF",
29
- sql: "SQL",
30
- url: "URL",
31
- urls: "URLs",
32
- usd: "USD",
20
+ api: "API",
21
+ csv: "CSV",
22
+ id: "ID",
23
+ ids: "IDs",
24
+ json: "JSON",
25
+ pdf: "PDF",
26
+ sql: "SQL",
27
+ url: "URL",
28
+ urls: "URLs",
29
+ usd: "USD"
33
30
  };
34
31
  export function humanizeKey(key) {
35
- const words = key
36
- .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
37
- .replace(/[_-]+/g, " ")
38
- .trim()
39
- .toLowerCase()
40
- .split(/\s+/)
41
- .map((word) => acronyms[word] ?? word);
42
- const sentence = words.join(" ");
43
- return sentence.charAt(0).toUpperCase() + sentence.slice(1);
32
+ const words = key.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[_-]+/g, " ").trim().toLowerCase().split(/\s+/).map(word => acronyms[word] ?? word);
33
+ const sentence = words.join(" ");
34
+ return sentence.charAt(0).toUpperCase() + sentence.slice(1);
44
35
  }
45
36
  function rawJson(input) {
46
- return JSON.stringify(input, null, 2) ?? "undefined";
37
+ return JSON.stringify(input, null, 2) ?? "undefined";
47
38
  }
48
- export function ToolInputs({ input, muted = false }) {
49
- const [showRaw, setShowRaw] = useState(false);
50
- const rawId = useId();
51
- const entries = isPlainRecord(input) ? Object.entries(input) : null;
52
- return (_jsxs("div", { "data-slot": "tool-inputs", className: "flex flex-col gap-2", children: [entries && !showRaw && (_jsxs("dl", { className: cn("-mx-5 flex flex-col divide-y border-y text-sm", muted && "text-muted-foreground"), children: [entries.map(([key, value]) => (_jsxs("div", { className: "grid grid-cols-[minmax(0,8.5rem)_minmax(0,1fr)] items-baseline gap-3 px-5 py-2.5", children: [_jsx("dt", { className: "text-xs text-muted-foreground", children: humanizeKey(key) }), _jsx("dd", { className: "font-mono text-sm break-words", children: readableValue(value) })] }, key))), entries.length === 0 && (_jsx("div", { className: "px-5 py-2.5 text-xs text-muted-foreground", children: "No inputs." }))] })), (showRaw || !entries) && (_jsx("pre", { id: rawId, className: "overflow-x-auto bg-muted/40 p-3 font-mono text-xs", children: rawJson(input) })), entries && (_jsx("button", { type: "button", className: "eyebrow self-start outline-none hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/70 motion-safe:transition-colors", "aria-expanded": showRaw, "aria-controls": showRaw ? rawId : undefined, onClick: () => setShowRaw((current) => !current), children: showRaw ? "Show fields" : "Show raw" }))] }));
39
+ export function ToolInputs({
40
+ input,
41
+ muted = false
42
+ }) {
43
+ const [showRaw, setShowRaw] = useState(false);
44
+ const rawId = useId();
45
+ const entries = isPlainRecord(input) ? Object.entries(input) : null;
46
+ return /*#__PURE__*/_jsxs("div", {
47
+ "data-slot": "tool-inputs",
48
+ className: "flex flex-col gap-2",
49
+ children: [entries && !showRaw && /*#__PURE__*/_jsxs("dl", {
50
+ className: cn("-mx-5 flex flex-col divide-y border-y text-sm", muted && "text-muted-foreground"),
51
+ children: [entries.map(([key, value]) => /*#__PURE__*/_jsxs("div", {
52
+ className: "grid grid-cols-[minmax(0,8.5rem)_minmax(0,1fr)] items-baseline gap-3 px-5 py-2.5",
53
+ children: [/*#__PURE__*/_jsx("dt", {
54
+ className: "text-xs text-muted-foreground",
55
+ children: humanizeKey(key)
56
+ }), /*#__PURE__*/_jsx("dd", {
57
+ className: "font-mono text-sm break-words",
58
+ children: readableValue(value)
59
+ })]
60
+ }, key)), entries.length === 0 && /*#__PURE__*/_jsx("div", {
61
+ className: "px-5 py-2.5 text-xs text-muted-foreground",
62
+ children: "No inputs."
63
+ })]
64
+ }), (showRaw || !entries) && /*#__PURE__*/_jsx("pre", {
65
+ id: rawId,
66
+ className: "overflow-x-auto bg-muted/40 p-3 font-mono text-xs",
67
+ children: rawJson(input)
68
+ }), entries && /*#__PURE__*/_jsx("button", {
69
+ type: "button",
70
+ className: "eyebrow self-start outline-none hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/70 motion-safe:transition-colors",
71
+ "aria-expanded": showRaw,
72
+ "aria-controls": showRaw ? rawId : undefined,
73
+ onClick: () => setShowRaw(current => !current),
74
+ children: showRaw ? "Show fields" : "Show raw"
75
+ })]
76
+ });
53
77
  }
@@ -0,0 +1,3 @@
1
+ import { type ReactNode } from "react";
2
+ export declare function gapBeforeItem(items: ReactNode[], index: number): number;
3
+ export declare function isPositionable(node: ReactNode): node is React.ReactElement;
@@ -0,0 +1,19 @@
1
+ import { Fragment, isValidElement } from "react";
2
+ const turnGapPx = 32;
3
+ const answerAfterQuestionGapPx = 16;
4
+ function roleOf(node) {
5
+ if (!isValidElement(node)) {
6
+ return undefined;
7
+ }
8
+ return node.props["data-role"] ?? node.props.message?.role;
9
+ }
10
+ export function gapBeforeItem(items, index) {
11
+ if (index === 0) {
12
+ return 0;
13
+ }
14
+ const answersQuestion = roleOf(items[index - 1]) === "user" && roleOf(items[index]) === "assistant";
15
+ return answersQuestion ? answerAfterQuestionGapPx : turnGapPx;
16
+ }
17
+ export function isPositionable(node) {
18
+ return isValidElement(node) && node.type !== Fragment && typeof node.type !== "string";
19
+ }
@@ -3,5 +3,6 @@ export type ChatProps = React.ComponentProps<"div"> & {
3
3
  empty?: React.ReactNode;
4
4
  label?: string;
5
5
  jumpLabel?: string;
6
+ virtualize?: boolean;
6
7
  };
7
- export declare function Chat({ children, empty, label, jumpLabel, className, ...props }: ChatProps): React.JSX.Element;
8
+ export declare function Chat({ children, empty, label, jumpLabel, virtualize, className, ...props }: ChatProps): React.JSX.Element;
@@ -1,11 +1,187 @@
1
1
  "use client";
2
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+
3
+ import { c as _c } from "react/compiler-runtime";
3
4
  import { ArrowDown as ArrowDownIcon } from "@carbon/icons-react";
4
5
  import { Button, cn } from "@locusgraph/components";
5
6
  import * as React from "react";
7
+ import { shouldVirtualize } from "../../shared/virtual/use-list-virtualizer.js";
6
8
  import { useStickToBottom } from "./use-stick-to-bottom.js";
7
- export function Chat({ children, empty, label = "Chat", jumpLabel = "Jump to latest", className, ...props }) {
8
- const { viewportRef, contentRef, isAtBottom, handleScroll, scrollToLatest } = useStickToBottom();
9
- const isEmpty = React.Children.toArray(children).length === 0;
10
- return (_jsxs("div", { "data-slot": "chat", "data-empty": isEmpty || undefined, className: cn("relative flex min-h-0 flex-1 flex-col overflow-hidden", className), ...props, children: [_jsx("div", { ref: viewportRef, "data-slot": "chat-viewport", role: "log", "aria-label": label, tabIndex: 0, onScroll: handleScroll, className: "min-h-0 flex-1 overflow-y-auto overscroll-contain outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-inset", children: _jsx("div", { ref: contentRef, "data-slot": "chat-content", className: "mx-auto flex min-h-full w-full max-w-3xl flex-col gap-8 px-4 py-6 [&>[data-role=user]+[data-role=assistant]]:-mt-4", children: isEmpty ? (_jsx("div", { "data-slot": "chat-empty", className: "flex flex-1 flex-col items-center justify-center text-center", children: empty })) : (children) }) }), !isEmpty && !isAtBottom && (_jsxs(Button, { type: "button", variant: "outline", size: "sm", "data-slot": "chat-jump", onClick: scrollToLatest, className: "absolute bottom-3 left-1/2 h-7 -translate-x-1/2 bg-card px-3 text-xs text-muted-foreground shadow-none hover:text-foreground motion-safe:animate-in motion-safe:fade-in-0 dark:bg-card", children: [_jsx(ArrowDownIcon, { "aria-hidden": "true", className: "size-3.5" }), jumpLabel] }))] }));
9
+ import { VirtualChatItems } from "./virtual-chat-items.js";
10
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
11
+ export function Chat(t0) {
12
+ const $ = _c(43);
13
+ let children;
14
+ let className;
15
+ let empty;
16
+ let props;
17
+ let t1;
18
+ let t2;
19
+ let t3;
20
+ if ($[0] !== t0) {
21
+ ({
22
+ children,
23
+ empty,
24
+ label: t1,
25
+ jumpLabel: t2,
26
+ virtualize: t3,
27
+ className,
28
+ ...props
29
+ } = t0);
30
+ $[0] = t0;
31
+ $[1] = children;
32
+ $[2] = className;
33
+ $[3] = empty;
34
+ $[4] = props;
35
+ $[5] = t1;
36
+ $[6] = t2;
37
+ $[7] = t3;
38
+ } else {
39
+ children = $[1];
40
+ className = $[2];
41
+ empty = $[3];
42
+ props = $[4];
43
+ t1 = $[5];
44
+ t2 = $[6];
45
+ t3 = $[7];
46
+ }
47
+ const label = t1 === undefined ? "Chat" : t1;
48
+ const jumpLabel = t2 === undefined ? "Jump to latest" : t2;
49
+ const virtualize = t3 === undefined ? true : t3;
50
+ const {
51
+ viewportRef,
52
+ contentRef,
53
+ isAtBottom,
54
+ handleScroll,
55
+ scrollToLatest
56
+ } = useStickToBottom();
57
+ const [scrolling, setScrolling] = React.useState(false);
58
+ let isEmpty;
59
+ let items;
60
+ let t4;
61
+ if ($[8] !== children || $[9] !== virtualize) {
62
+ items = React.Children.toArray(children);
63
+ isEmpty = items.length === 0;
64
+ t4 = shouldVirtualize(items.length, virtualize);
65
+ $[8] = children;
66
+ $[9] = virtualize;
67
+ $[10] = isEmpty;
68
+ $[11] = items;
69
+ $[12] = t4;
70
+ } else {
71
+ isEmpty = $[10];
72
+ items = $[11];
73
+ t4 = $[12];
74
+ }
75
+ const isVirtual = t4;
76
+ const t5 = isEmpty || undefined;
77
+ const t6 = isVirtual || undefined;
78
+ let t7;
79
+ if ($[13] !== className) {
80
+ t7 = cn("relative flex min-h-0 flex-1 flex-col overflow-hidden", className);
81
+ $[13] = className;
82
+ $[14] = t7;
83
+ } else {
84
+ t7 = $[14];
85
+ }
86
+ const t8 = isVirtual && scrolling || undefined;
87
+ let t9;
88
+ if ($[15] !== children || $[16] !== empty || $[17] !== isEmpty || $[18] !== isVirtual || $[19] !== items || $[20] !== viewportRef) {
89
+ t9 = isEmpty ? /*#__PURE__*/_jsx("div", {
90
+ "data-slot": "chat-empty",
91
+ className: "flex flex-1 flex-col items-center justify-center text-center",
92
+ children: empty
93
+ }) : isVirtual ? /*#__PURE__*/_jsx(VirtualChatItems, {
94
+ items: items,
95
+ viewportRef: viewportRef,
96
+ onScrollingChange: setScrolling
97
+ }) : children;
98
+ $[15] = children;
99
+ $[16] = empty;
100
+ $[17] = isEmpty;
101
+ $[18] = isVirtual;
102
+ $[19] = items;
103
+ $[20] = viewportRef;
104
+ $[21] = t9;
105
+ } else {
106
+ t9 = $[21];
107
+ }
108
+ let t10;
109
+ if ($[22] !== contentRef || $[23] !== t9) {
110
+ t10 = /*#__PURE__*/_jsx("div", {
111
+ ref: contentRef,
112
+ "data-slot": "chat-content",
113
+ className: "mx-auto flex min-h-full w-full max-w-3xl flex-col gap-8 px-4 py-6 [&>[data-role=user]+[data-role=assistant]]:-mt-4",
114
+ children: t9
115
+ });
116
+ $[22] = contentRef;
117
+ $[23] = t9;
118
+ $[24] = t10;
119
+ } else {
120
+ t10 = $[24];
121
+ }
122
+ let t11;
123
+ if ($[25] !== handleScroll || $[26] !== label || $[27] !== t10 || $[28] !== t8 || $[29] !== viewportRef) {
124
+ t11 = /*#__PURE__*/_jsx("div", {
125
+ ref: viewportRef,
126
+ "data-slot": "chat-viewport",
127
+ role: "log",
128
+ "aria-label": label,
129
+ "aria-busy": t8,
130
+ tabIndex: 0,
131
+ onScroll: handleScroll,
132
+ className: "min-h-0 flex-1 overflow-y-auto overscroll-contain outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-inset",
133
+ children: t10
134
+ });
135
+ $[25] = handleScroll;
136
+ $[26] = label;
137
+ $[27] = t10;
138
+ $[28] = t8;
139
+ $[29] = viewportRef;
140
+ $[30] = t11;
141
+ } else {
142
+ t11 = $[30];
143
+ }
144
+ let t12;
145
+ if ($[31] !== isAtBottom || $[32] !== isEmpty || $[33] !== jumpLabel || $[34] !== scrollToLatest) {
146
+ t12 = !isEmpty && !isAtBottom && /*#__PURE__*/_jsxs(Button, {
147
+ type: "button",
148
+ variant: "outline",
149
+ size: "sm",
150
+ "data-slot": "chat-jump",
151
+ onClick: scrollToLatest,
152
+ className: "absolute bottom-3 left-1/2 h-7 -translate-x-1/2 bg-card px-3 text-xs text-muted-foreground shadow-none hover:text-foreground motion-safe:animate-in motion-safe:fade-in-0 dark:bg-card",
153
+ children: [/*#__PURE__*/_jsx(ArrowDownIcon, {
154
+ "aria-hidden": "true",
155
+ className: "size-3.5"
156
+ }), jumpLabel]
157
+ });
158
+ $[31] = isAtBottom;
159
+ $[32] = isEmpty;
160
+ $[33] = jumpLabel;
161
+ $[34] = scrollToLatest;
162
+ $[35] = t12;
163
+ } else {
164
+ t12 = $[35];
165
+ }
166
+ let t13;
167
+ if ($[36] !== props || $[37] !== t11 || $[38] !== t12 || $[39] !== t5 || $[40] !== t6 || $[41] !== t7) {
168
+ t13 = /*#__PURE__*/_jsxs("div", {
169
+ "data-slot": "chat",
170
+ "data-empty": t5,
171
+ "data-virtual": t6,
172
+ className: t7,
173
+ ...props,
174
+ children: [t11, t12]
175
+ });
176
+ $[36] = props;
177
+ $[37] = t11;
178
+ $[38] = t12;
179
+ $[39] = t5;
180
+ $[40] = t6;
181
+ $[41] = t7;
182
+ $[42] = t13;
183
+ } else {
184
+ t13 = $[42];
185
+ }
186
+ return t13;
11
187
  }
@@ -1,57 +1,80 @@
1
1
  import { useCallback, useEffect, useRef, useState } from "react";
2
2
  const bottomThresholdPx = 48;
3
3
  function distanceFromBottom(element) {
4
- return element.scrollHeight - element.scrollTop - element.clientHeight;
4
+ return element.scrollHeight - element.scrollTop - element.clientHeight;
5
5
  }
6
6
  function prefersReducedMotion() {
7
- return (typeof window !== "undefined" &&
8
- window.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true);
7
+ return typeof window !== "undefined" && window.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true;
8
+ }
9
+ function scrolledUpByReader(viewport, previous) {
10
+ return viewport.scrollTop < previous.top && viewport.scrollHeight === previous.height;
9
11
  }
10
12
  export function useStickToBottom() {
11
- const viewportRef = useRef(null);
12
- const contentRef = useRef(null);
13
- const stuckRef = useRef(true);
14
- const [isAtBottom, setIsAtBottom] = useState(true);
15
- const pinToBottom = useCallback(() => {
16
- const viewport = viewportRef.current;
17
- if (viewport) {
18
- viewport.scrollTop = viewport.scrollHeight;
19
- }
20
- }, []);
21
- const handleScroll = useCallback(() => {
22
- const viewport = viewportRef.current;
23
- if (!viewport) {
24
- return;
25
- }
26
- const atBottom = distanceFromBottom(viewport) <= bottomThresholdPx;
27
- stuckRef.current = atBottom;
28
- setIsAtBottom(atBottom);
29
- }, []);
30
- const scrollToLatest = useCallback(() => {
31
- const viewport = viewportRef.current;
32
- if (!viewport) {
33
- return;
34
- }
35
- stuckRef.current = true;
36
- setIsAtBottom(true);
37
- viewport.scrollTo({
38
- top: viewport.scrollHeight,
39
- behavior: prefersReducedMotion() ? "auto" : "smooth",
40
- });
41
- }, []);
42
- useEffect(() => {
43
- const content = contentRef.current;
44
- if (!content || typeof ResizeObserver === "undefined") {
45
- return;
46
- }
13
+ const viewportRef = useRef(null);
14
+ const contentRef = useRef(null);
15
+ const stuckRef = useRef(true);
16
+ const lastScrollRef = useRef({
17
+ top: 0,
18
+ height: 0
19
+ });
20
+ const [isAtBottom, setIsAtBottom] = useState(true);
21
+ const pinToBottom = useCallback(() => {
22
+ const viewport = viewportRef.current;
23
+ if (viewport) {
24
+ viewport.scrollTop = viewport.scrollHeight;
25
+ }
26
+ }, []);
27
+ const handleScroll = useCallback(() => {
28
+ const viewport = viewportRef.current;
29
+ if (!viewport) {
30
+ return;
31
+ }
32
+ const atBottom = distanceFromBottom(viewport) <= bottomThresholdPx;
33
+ const readerScrolledUp = scrolledUpByReader(viewport, lastScrollRef.current);
34
+ lastScrollRef.current = {
35
+ top: viewport.scrollTop,
36
+ height: viewport.scrollHeight
37
+ };
38
+ if (atBottom) {
39
+ stuckRef.current = true;
40
+ } else if (readerScrolledUp) {
41
+ stuckRef.current = false;
42
+ } else if (stuckRef.current) {
43
+ pinToBottom();
44
+ }
45
+ setIsAtBottom(stuckRef.current);
46
+ }, [pinToBottom]);
47
+ const scrollToLatest = useCallback(() => {
48
+ const viewport = viewportRef.current;
49
+ if (!viewport) {
50
+ return;
51
+ }
52
+ stuckRef.current = true;
53
+ setIsAtBottom(true);
54
+ viewport.scrollTo({
55
+ top: viewport.scrollHeight,
56
+ behavior: prefersReducedMotion() ? "auto" : "smooth"
57
+ });
58
+ }, []);
59
+ useEffect(() => {
60
+ const content = contentRef.current;
61
+ if (!content || typeof ResizeObserver === "undefined") {
62
+ return;
63
+ }
64
+ pinToBottom();
65
+ const observer = new ResizeObserver(() => {
66
+ if (stuckRef.current) {
47
67
  pinToBottom();
48
- const observer = new ResizeObserver(() => {
49
- if (stuckRef.current) {
50
- pinToBottom();
51
- }
52
- });
53
- observer.observe(content);
54
- return () => observer.disconnect();
55
- }, [pinToBottom]);
56
- return { viewportRef, contentRef, isAtBottom, handleScroll, scrollToLatest };
68
+ }
69
+ });
70
+ observer.observe(content);
71
+ return () => observer.disconnect();
72
+ }, [pinToBottom]);
73
+ return {
74
+ viewportRef,
75
+ contentRef,
76
+ isAtBottom,
77
+ handleScroll,
78
+ scrollToLatest
79
+ };
57
80
  }
@@ -0,0 +1,8 @@
1
+ import { type ReactNode, type RefObject } from "react";
2
+ type VirtualChatItemsProps = {
3
+ items: ReactNode[];
4
+ viewportRef: RefObject<HTMLDivElement | null>;
5
+ onScrollingChange: (scrolling: boolean) => void;
6
+ };
7
+ export declare function VirtualChatItems({ items, viewportRef, onScrollingChange }: VirtualChatItemsProps): import("react").JSX.Element;
8
+ export {};