@liveblocks/react-ui 3.8.0-tiptap1 → 3.8.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/dist/components/AiChat.cjs +52 -30
- package/dist/components/AiChat.cjs.map +1 -1
- package/dist/components/AiChat.js +53 -31
- package/dist/components/AiChat.js.map +1 -1
- package/dist/components/AiTool.cjs.map +1 -1
- package/dist/components/AiTool.js.map +1 -1
- package/dist/components/internal/AiComposer.cjs +2 -2
- package/dist/components/internal/AiComposer.cjs.map +1 -1
- package/dist/components/internal/AiComposer.js +2 -2
- package/dist/components/internal/AiComposer.js.map +1 -1
- package/dist/index.d.cts +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/version.cjs +1 -1
- package/dist/version.cjs.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +4 -4
|
@@ -51,50 +51,45 @@ const AiChatMessages = react.forwardRef(
|
|
|
51
51
|
}
|
|
52
52
|
const trailingSpacer = trailingSpacerRef.current;
|
|
53
53
|
const bottomTrailingMarker = bottomTrailingMarkerRef.current;
|
|
54
|
-
let containerHeight =
|
|
55
|
-
let footerHeight =
|
|
56
|
-
let messagesHeight =
|
|
54
|
+
let containerHeight = void 0;
|
|
55
|
+
let footerHeight = void 0;
|
|
56
|
+
let messagesHeight = void 0;
|
|
57
57
|
const resetTrailingSpace = () => {
|
|
58
58
|
trailingSpacer?.style.removeProperty("height");
|
|
59
59
|
bottomTrailingMarker?.style.removeProperty("top");
|
|
60
60
|
};
|
|
61
|
-
const
|
|
61
|
+
const updateTrailingSpace = (updatedContainerHeight, updatedFooterHeight, updatedMessagesHeight) => {
|
|
62
62
|
if (!trailingSpacer || !bottomTrailingMarker) {
|
|
63
63
|
return;
|
|
64
64
|
}
|
|
65
65
|
const lastMessage = messages2.lastElementChild;
|
|
66
66
|
const penultimateMessage = lastMessage?.previousElementSibling;
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
return;
|
|
67
|
+
if (updatedContainerHeight === void 0) {
|
|
68
|
+
updatedContainerHeight = container.getBoundingClientRect().height;
|
|
70
69
|
}
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
let updatedContainerHeight = containerHeight;
|
|
76
|
-
let updatedFooterHeight = footerHeight;
|
|
77
|
-
let updatedMessagesHeight = messagesHeight;
|
|
78
|
-
for (const entry of entries) {
|
|
79
|
-
const entryHeight = entry.borderBoxSize?.[0]?.blockSize ?? entry.contentRect.height;
|
|
80
|
-
if (entry.target === container) {
|
|
81
|
-
updatedContainerHeight = entryHeight ?? null;
|
|
82
|
-
} else if (entry.target === footer) {
|
|
83
|
-
updatedFooterHeight = entryHeight ?? null;
|
|
84
|
-
} else if (entry.target === messages2) {
|
|
85
|
-
updatedMessagesHeight = entryHeight ?? null;
|
|
86
|
-
}
|
|
70
|
+
if (updatedFooterHeight === void 0) {
|
|
71
|
+
updatedFooterHeight = footer.getBoundingClientRect().height;
|
|
87
72
|
}
|
|
88
|
-
if (
|
|
89
|
-
|
|
90
|
-
return;
|
|
73
|
+
if (updatedMessagesHeight === void 0) {
|
|
74
|
+
updatedMessagesHeight = messages2.getBoundingClientRect().height;
|
|
91
75
|
}
|
|
92
76
|
if (updatedContainerHeight === containerHeight && updatedFooterHeight === footerHeight && updatedMessagesHeight === messagesHeight) {
|
|
77
|
+
if (!lastMessage || !penultimateMessage || container.scrollHeight === container.clientHeight) {
|
|
78
|
+
resetTrailingSpace();
|
|
79
|
+
}
|
|
93
80
|
return;
|
|
94
81
|
}
|
|
95
82
|
containerHeight = updatedContainerHeight;
|
|
96
83
|
footerHeight = updatedFooterHeight;
|
|
97
84
|
messagesHeight = updatedMessagesHeight;
|
|
85
|
+
if (!lastMessage || !penultimateMessage) {
|
|
86
|
+
resetTrailingSpace();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (container.scrollHeight === container.clientHeight) {
|
|
90
|
+
resetTrailingSpace();
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
98
93
|
const penultimateMessageScrollMarginTop = Number.parseFloat(
|
|
99
94
|
getComputedStyle(penultimateMessage).scrollMarginTop
|
|
100
95
|
);
|
|
@@ -105,10 +100,31 @@ const AiChatMessages = react.forwardRef(
|
|
|
105
100
|
const trailingSpace = Math.max(containerHeight - differenceHeight, 0);
|
|
106
101
|
trailingSpacer.style.height = `${trailingSpace}px`;
|
|
107
102
|
bottomTrailingMarker.style.top = `${-trailingSpace}px`;
|
|
103
|
+
};
|
|
104
|
+
const resizeObserver = new ResizeObserver((entries) => {
|
|
105
|
+
let updatedContainerHeight = containerHeight;
|
|
106
|
+
let updatedFooterHeight = footerHeight;
|
|
107
|
+
let updatedMessagesHeight = messagesHeight;
|
|
108
|
+
for (const entry of entries) {
|
|
109
|
+
const entryHeight = entry.borderBoxSize?.[0]?.blockSize ?? entry.contentRect.height;
|
|
110
|
+
if (entry.target === container) {
|
|
111
|
+
updatedContainerHeight = entryHeight;
|
|
112
|
+
} else if (entry.target === footer) {
|
|
113
|
+
updatedFooterHeight = entryHeight;
|
|
114
|
+
} else if (entry.target === messages2) {
|
|
115
|
+
updatedMessagesHeight = entryHeight;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
updateTrailingSpace(
|
|
119
|
+
updatedContainerHeight,
|
|
120
|
+
updatedFooterHeight,
|
|
121
|
+
updatedMessagesHeight
|
|
122
|
+
);
|
|
108
123
|
});
|
|
109
124
|
resizeObserver.observe(container);
|
|
110
125
|
resizeObserver.observe(footer);
|
|
111
126
|
resizeObserver.observe(messages2);
|
|
127
|
+
requestAnimationFrame(() => updateTrailingSpace());
|
|
112
128
|
return () => {
|
|
113
129
|
resizeObserver.disconnect();
|
|
114
130
|
resetTrailingSpace();
|
|
@@ -182,6 +198,7 @@ const AiChat = react.forwardRef(
|
|
|
182
198
|
layout = "inset",
|
|
183
199
|
components,
|
|
184
200
|
className,
|
|
201
|
+
responseTimeout,
|
|
185
202
|
...props
|
|
186
203
|
}, forwardedRef) => {
|
|
187
204
|
const { messages, isLoading, error } = react$1.useAiChatMessages(chatId);
|
|
@@ -208,12 +225,12 @@ const AiChat = react.forwardRef(
|
|
|
208
225
|
const scrollToBottom = _private.useLatest(
|
|
209
226
|
(behavior, includeTrailingSpace = false) => {
|
|
210
227
|
if (includeTrailingSpace) {
|
|
211
|
-
|
|
228
|
+
setTimeout(() => {
|
|
212
229
|
bottomMarkerRef.current?.scrollIntoView({
|
|
213
230
|
behavior,
|
|
214
231
|
block: "end"
|
|
215
232
|
});
|
|
216
|
-
});
|
|
233
|
+
}, 0);
|
|
217
234
|
} else {
|
|
218
235
|
bottomTrailingMarkerRef.current?.scrollIntoView({
|
|
219
236
|
behavior,
|
|
@@ -236,6 +253,10 @@ const AiChat = react.forwardRef(
|
|
|
236
253
|
name,
|
|
237
254
|
tool
|
|
238
255
|
}, name)),
|
|
256
|
+
localKnowledge ? localKnowledge.map((knowledge, index) => /* @__PURE__ */ jsxRuntime.jsx(react$1.RegisterAiKnowledge, {
|
|
257
|
+
chatId,
|
|
258
|
+
...knowledge
|
|
259
|
+
}, `${index}:${knowledge.description}`)) : null,
|
|
239
260
|
/* @__PURE__ */ jsxRuntime.jsx("div", {
|
|
240
261
|
className: "lb-ai-chat-content",
|
|
241
262
|
children: isLoading ? /* @__PURE__ */ jsxRuntime.jsx(Loading, {}) : error !== void 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", {
|
|
@@ -264,7 +285,8 @@ const AiChat = react.forwardRef(
|
|
|
264
285
|
ref: trailingSpacerRef,
|
|
265
286
|
"data-trailing-spacer": "",
|
|
266
287
|
style: {
|
|
267
|
-
pointerEvents: "none"
|
|
288
|
+
pointerEvents: "none",
|
|
289
|
+
height: 0
|
|
268
290
|
},
|
|
269
291
|
"aria-hidden": true
|
|
270
292
|
})
|
|
@@ -297,7 +319,7 @@ const AiChat = react.forwardRef(
|
|
|
297
319
|
copilotId,
|
|
298
320
|
overrides: overrides$1,
|
|
299
321
|
autoFocus,
|
|
300
|
-
|
|
322
|
+
responseTimeout,
|
|
301
323
|
onComposerSubmit,
|
|
302
324
|
onComposerSubmitted: ({ id }) => setLastSentMessageId(id),
|
|
303
325
|
className: cn.cn(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AiChat.cjs","sources":["../../src/components/AiChat.tsx"],"sourcesContent":["import type {\n AiKnowledgeSource,\n AiOpaqueToolDefinition,\n CopilotId,\n MessageId,\n} from \"@liveblocks/core\";\nimport { RegisterAiTool, useAiChatMessages } from \"@liveblocks/react\";\nimport { useLatest } from \"@liveblocks/react/_private\";\nimport {\n type ComponentProps,\n type ComponentType,\n forwardRef,\n type MutableRefObject,\n useEffect,\n useImperativeHandle,\n useRef,\n useState,\n} from \"react\";\n\nimport type { GlobalComponents } from \"../components\";\nimport { ArrowDownIcon } from \"../icons/ArrowDown\";\nimport { SpinnerIcon } from \"../icons/Spinner\";\nimport {\n type AiChatMessageOverrides,\n type AiChatOverrides,\n type AiComposerOverrides,\n type GlobalOverrides,\n useOverrides,\n} from \"../overrides\";\nimport type { MarkdownComponents } from \"../primitives/Markdown\";\nimport { cn } from \"../utils/cn\";\nimport { useIntersectionCallback } from \"../utils/use-visible\";\nimport { AiChatAssistantMessage } from \"./internal/AiChatAssistantMessage\";\nimport { AiChatUserMessage } from \"./internal/AiChatUserMessage\";\nimport { AiComposer, type AiComposerProps } from \"./internal/AiComposer\";\n\n/**\n * The minimum number of pixels from the bottom of the scrollable area\n * before showing the scroll to bottom indicator.\n */\nconst MIN_DISTANCE_BOTTOM_SCROLL_INDICATOR = 60;\n\nexport type AiChatComponentsEmptyProps = {\n /**\n * The chat ID provided to the `AiChat` component.\n */\n chatId: string;\n\n /**\n * The copilot ID provided to the `AiChat` component.\n */\n copilotId?: string;\n};\n\nexport type AiChatComponentsLoadingProps = Record<string, never>;\n\nexport type AiChatComponents = {\n /**\n * The component used to render the empty state of the chat.\n */\n Empty: ComponentType<AiChatComponentsEmptyProps>;\n\n /**\n * The component used to render the loading state of the chat.\n */\n Loading: ComponentType<AiChatComponentsLoadingProps>;\n\n /**\n * The components used to render Markdown content.\n */\n markdown?: Partial<MarkdownComponents>;\n};\n\nexport interface AiChatProps extends ComponentProps<\"div\"> {\n /**\n * The ID of the chat the composer belongs to.\n */\n chatId: string;\n\n /**\n * Whether to focus the chat composer on mount.\n */\n autoFocus?: boolean;\n\n /**\n * The ID of the copilot to use to send the message.\n */\n copilotId?: string;\n\n /**\n * The contextual knowledge to include in the chat. May be used by the\n * assistant when generating responses. In addition to the knowledge passed\n * in via this prop, the AiChat instance will also have access to any\n * globally registered knowledge via <RegisterAiKnowledge />.\n */\n knowledge?: AiKnowledgeSource[];\n\n /**\n * Tool definitions to make available within this chat. May be used by the assistant when generating responses.\n */\n tools?: Record<string, AiOpaqueToolDefinition>;\n\n /**\n * The event handler called when the composer is submitted.\n */\n onComposerSubmit?: AiComposerProps[\"onComposerSubmit\"];\n\n /**\n * The layout of the chat and its composer.\n */\n layout?: \"inset\" | \"compact\";\n\n /**\n * Override the component's strings.\n */\n overrides?: Partial<\n GlobalOverrides &\n AiComposerOverrides &\n AiChatMessageOverrides &\n AiChatOverrides\n >;\n\n /**\n * Override the component's components.\n */\n components?: Partial<GlobalComponents & AiChatComponents>;\n}\n\ninterface AiChatMessagesProps extends ComponentProps<\"div\"> {\n messages: NonNullable<ReturnType<typeof useAiChatMessages>[\"messages\"]>;\n overrides: AiChatProps[\"overrides\"];\n components: AiChatProps[\"components\"];\n lastSentMessageId: MessageId | null;\n scrollToBottom: MutableRefObject<\n (behavior: \"instant\" | \"smooth\", includeTrailingSpace?: boolean) => void\n >;\n onScrollAtBottomChange: MutableRefObject<\n (isScrollAtBottom: boolean | null) => void\n >;\n containerRef: MutableRefObject<HTMLDivElement | null>;\n footerRef: MutableRefObject<HTMLDivElement | null>;\n messagesRef: MutableRefObject<HTMLDivElement | null>;\n bottomTrailingMarkerRef: MutableRefObject<HTMLDivElement | null>;\n trailingSpacerRef: MutableRefObject<HTMLDivElement | null>;\n}\n\nconst defaultComponents: AiChatComponents = {\n Empty: () => null,\n Loading: () => (\n <div className=\"lb-loading lb-ai-chat-loading\">\n <SpinnerIcon />\n </div>\n ),\n};\n\nconst AiChatMessages = forwardRef<HTMLDivElement, AiChatMessagesProps>(\n (\n {\n messages,\n overrides,\n components,\n lastSentMessageId,\n scrollToBottom,\n onScrollAtBottomChange,\n containerRef,\n footerRef,\n messagesRef,\n bottomTrailingMarkerRef,\n trailingSpacerRef,\n className,\n ...props\n },\n forwardedRef\n ) => {\n const hasLastSentMessage = lastSentMessageId !== null;\n\n /**\n * Every time the container, footer, or messages list change size,\n * we calculate the trailing space that would allow the penultimate\n * message to be at the top of the viewport, and apply it.\n *\n * ┌─────────────────────────────────────────┐▲ A = The `scroll-margin-top`\n * │ ┌─────────────────────────┐ │▼▲ value of the penultimate message\n * │ │ The penultimate message │ │ │\n * │ └─────────────────────────┘ │ │ B = The height from the top of\n * │ │ │ the penultimate message to the\n * │ ┌─────────────────────────┐ │ │ bottom of the messages list,\n * │ │ The last message │ │ │ including the messages' heights,\n * │ └─────────────────────────┘ │ │ and any padding, gap, etc\n * │ │ │\n * ├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤▲▼\n * │ ││ The trailing space needed to\n * │ = container height - (A + B + C) ││ allow the penultimate message\n * │ ││ to be at the top of the viewport\n * ├ ┬─────────────────────────────────────┬ ┤▼▲\n * │ │ │ │ │\n * │ │ │ │ │ C = The footer's height,\n * │ │ │ │ │ including any padding\n * │ └─────────────────────────────────────┘ │ │\n * └─────────────────────────────────────────┘ ▼\n */\n useEffect(\n () => {\n if (!hasLastSentMessage) {\n return;\n }\n\n const container = containerRef.current;\n const footer = footerRef.current;\n const messages = messagesRef.current;\n\n if (!container || !footer || !messages) {\n return;\n }\n\n const trailingSpacer = trailingSpacerRef.current;\n const bottomTrailingMarker = bottomTrailingMarkerRef.current;\n\n let containerHeight: number | null = null;\n let footerHeight: number | null = null;\n let messagesHeight: number | null = null;\n\n const resetTrailingSpace = () => {\n trailingSpacer?.style.removeProperty(\"height\");\n bottomTrailingMarker?.style.removeProperty(\"top\");\n };\n\n const resizeObserver = new ResizeObserver((entries) => {\n if (!trailingSpacer || !bottomTrailingMarker) {\n return;\n }\n\n const lastMessage = messages.lastElementChild;\n const penultimateMessage = lastMessage?.previousElementSibling;\n\n // If there's no last pair of messages, there's no need for any trailing space.\n if (!lastMessage || !penultimateMessage) {\n resetTrailingSpace();\n return;\n }\n\n // If the container's height is based on its content, the container isn't scrollable and there's no need for any trailing space.\n if (container.scrollHeight === container.clientHeight) {\n resetTrailingSpace();\n return;\n }\n\n let updatedContainerHeight: number | null = containerHeight;\n let updatedFooterHeight: number | null = footerHeight;\n let updatedMessagesHeight: number | null = messagesHeight;\n\n for (const entry of entries) {\n const entryHeight =\n entry.borderBoxSize?.[0]?.blockSize ?? entry.contentRect.height;\n\n if (entry.target === container) {\n updatedContainerHeight = entryHeight ?? null;\n } else if (entry.target === footer) {\n updatedFooterHeight = entryHeight ?? null;\n } else if (entry.target === messages) {\n updatedMessagesHeight = entryHeight ?? null;\n }\n }\n\n // If we don't have all the heights, we can't compute the trailing space.\n if (\n updatedContainerHeight === null ||\n updatedFooterHeight === null ||\n updatedMessagesHeight === null\n ) {\n resetTrailingSpace();\n return;\n }\n\n // If none of the heights have changed, we don't need to do anything.\n if (\n updatedContainerHeight === containerHeight &&\n updatedFooterHeight === footerHeight &&\n updatedMessagesHeight === messagesHeight\n ) {\n return;\n }\n\n // Now that we have compared them, we can update the heights.\n containerHeight = updatedContainerHeight;\n footerHeight = updatedFooterHeight;\n messagesHeight = updatedMessagesHeight;\n\n // A\n const penultimateMessageScrollMarginTop = Number.parseFloat(\n getComputedStyle(penultimateMessage as HTMLElement).scrollMarginTop\n );\n\n // B\n const messagesRect = messages.getBoundingClientRect();\n const penultimateMessageRect =\n penultimateMessage.getBoundingClientRect();\n const heightFromPenultimateMessageTopToMessagesListBottom =\n messagesRect.bottom - penultimateMessageRect.top;\n\n // A + B + C\n const differenceHeight =\n penultimateMessageScrollMarginTop +\n heightFromPenultimateMessageTopToMessagesListBottom +\n (footerHeight ?? 0);\n\n // = container height - (A + B + C)\n const trailingSpace = Math.max(containerHeight - differenceHeight, 0);\n\n // Update the trailing space.\n trailingSpacer.style.height = `${trailingSpace}px`;\n\n // Offset what \"the bottom\" is to the \"scroll at the bottom\" detection logic,\n // so that it doesn't include the trailing space.\n bottomTrailingMarker.style.top = `${-trailingSpace}px`;\n });\n\n resizeObserver.observe(container);\n resizeObserver.observe(footer);\n resizeObserver.observe(messages);\n\n return () => {\n resizeObserver.disconnect();\n resetTrailingSpace();\n };\n },\n // This effect only uses stable refs.\n [hasLastSentMessage] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n /**\n * Update the \"scroll at bottom\" state when needed.\n */\n useIntersectionCallback(\n bottomTrailingMarkerRef,\n (isIntersecting) => {\n onScrollAtBottomChange.current(isIntersecting);\n },\n { root: containerRef, rootMargin: MIN_DISTANCE_BOTTOM_SCROLL_INDICATOR }\n );\n\n /**\n * Instantly scroll to the bottom for the initial state.\n */\n useEffect(\n () => {\n scrollToBottom.current(\"instant\");\n },\n // `scrollToBottom` is a stable ref containing the callback.\n [] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n /**\n * Scroll to new messages when sending them.\n */\n useEffect(\n () => {\n if (lastSentMessageId) {\n scrollToBottom.current(\"smooth\", true);\n }\n },\n // `scrollToBottom` is a stable ref containing the callback.\n [lastSentMessageId] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n /**\n * Reset the \"scroll at bottom\" state when the component unmounts.\n */\n useEffect(\n () => {\n const onScrollAtBottomChangeCallback = onScrollAtBottomChange.current;\n\n return () => {\n onScrollAtBottomChangeCallback(null);\n };\n },\n // `onScrollAtBottomChange` is a stable ref containing the callback.\n [] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n return (\n <div\n className={cn(\"lb-ai-chat-messages\", className)}\n ref={forwardedRef}\n {...props}\n >\n {messages.map((message) => {\n if (message.role === \"user\") {\n return (\n <AiChatUserMessage\n key={message.id}\n message={message}\n overrides={overrides}\n components={components}\n />\n );\n } else if (message.role === \"assistant\") {\n return (\n <AiChatAssistantMessage\n key={message.id}\n message={message}\n overrides={overrides}\n components={components}\n />\n );\n } else {\n return null;\n }\n })}\n </div>\n );\n }\n);\n\nexport const AiChat = forwardRef<HTMLDivElement, AiChatProps>(\n (\n {\n chatId,\n copilotId,\n autoFocus,\n overrides,\n knowledge: localKnowledge,\n tools = {},\n onComposerSubmit,\n layout = \"inset\",\n components,\n className,\n ...props\n },\n forwardedRef\n ) => {\n const { messages, isLoading, error } = useAiChatMessages(chatId);\n const [lastSentMessageId, setLastSentMessageId] =\n useState<MessageId | null>(null);\n\n const $ = useOverrides(overrides);\n const Empty = components?.Empty ?? defaultComponents.Empty;\n const Loading = components?.Loading ?? defaultComponents.Loading;\n\n const containerRef = useRef<HTMLDivElement | null>(null);\n const messagesRef = useRef<HTMLDivElement | null>(null);\n const footerRef = useRef<HTMLDivElement | null>(null);\n const bottomMarkerRef = useRef<HTMLDivElement | null>(null);\n const bottomTrailingMarkerRef = useRef<HTMLDivElement | null>(null);\n const trailingSpacerRef = useRef<HTMLDivElement | null>(null);\n\n const [isScrollAtBottom, setScrollAtBottom] = useState<boolean | null>(\n null\n );\n // `useState`'s setter is stable but this is for clarity in the places it's used.\n const onScrollAtBottomChange = useLatest(setScrollAtBottom);\n const isScrollIndicatorVisible =\n messages && isScrollAtBottom !== null ? !isScrollAtBottom : false;\n\n useImperativeHandle<HTMLDivElement | null, HTMLDivElement | null>(\n forwardedRef,\n () => containerRef.current,\n []\n );\n\n const scrollToBottom = useLatest(\n (behavior: \"instant\" | \"smooth\", includeTrailingSpace = false) => {\n if (includeTrailingSpace) {\n // Scroll to the bottom marker to include the trailing space,\n // and wait for a frame in case the trailing space hasn't\n // been updated yet. (e.g. when sending a new message)\n requestAnimationFrame(() => {\n bottomMarkerRef.current?.scrollIntoView({\n behavior,\n block: \"end\",\n });\n });\n } else {\n // Scroll to the trailing space marker to only scroll to the\n // bottom of the messages, without including the trailing space.\n bottomTrailingMarkerRef.current?.scrollIntoView({\n behavior,\n block: \"end\",\n });\n }\n }\n );\n\n return (\n <div\n ref={containerRef}\n {...props}\n className={cn(\n \"lb-root lb-ai-chat\",\n `lb-ai-chat:layout-${layout}`,\n className\n )}\n >\n {Object.entries(tools).map(([name, tool]) => (\n <RegisterAiTool key={name} chatId={chatId} name={name} tool={tool} />\n ))}\n\n <div className=\"lb-ai-chat-content\">\n {isLoading ? (\n <Loading />\n ) : error !== undefined ? (\n <div className=\"lb-error lb-ai-chat-error\">\n {$.AI_CHAT_MESSAGES_ERROR(error)}\n </div>\n ) : messages.length === 0 ? (\n <Empty chatId={chatId} copilotId={copilotId} />\n ) : (\n <>\n <AiChatMessages\n ref={messagesRef}\n messages={messages}\n overrides={overrides}\n components={components}\n lastSentMessageId={lastSentMessageId}\n scrollToBottom={scrollToBottom}\n onScrollAtBottomChange={onScrollAtBottomChange}\n containerRef={containerRef}\n footerRef={footerRef}\n messagesRef={messagesRef}\n bottomTrailingMarkerRef={bottomTrailingMarkerRef}\n trailingSpacerRef={trailingSpacerRef}\n />\n\n {/**\n * This trailing spacer is used to extend the scrollable area beyond its actual\n * content, to allow messages to appear at the top of the viewport for example.\n */}\n <div\n ref={trailingSpacerRef}\n data-trailing-spacer=\"\"\n style={{\n pointerEvents: \"none\",\n }}\n aria-hidden\n />\n </>\n )}\n </div>\n\n <div className=\"lb-ai-chat-footer\" ref={footerRef}>\n <div className=\"lb-ai-chat-footer-actions\">\n <div\n className=\"lb-root lb-elevation lb-elevation-moderate lb-ai-chat-scroll-indicator\"\n data-visible={isScrollIndicatorVisible ? \"\" : undefined}\n >\n <button\n className=\"lb-ai-chat-scroll-indicator-button\"\n tabIndex={isScrollIndicatorVisible ? 0 : -1}\n aria-hidden={!isScrollIndicatorVisible}\n onClick={() => scrollToBottom.current(\"smooth\")}\n >\n <span className=\"lb-icon-container\">\n <ArrowDownIcon />\n </span>\n </button>\n </div>\n </div>\n <AiComposer\n key={chatId}\n chatId={chatId}\n copilotId={copilotId as CopilotId}\n overrides={overrides}\n autoFocus={autoFocus}\n knowledge={localKnowledge}\n onComposerSubmit={onComposerSubmit}\n onComposerSubmitted={({ id }) => setLastSentMessageId(id)}\n className={cn(\n \"lb-ai-chat-composer\",\n layout === \"inset\"\n ? \"lb-elevation lb-elevation-moderate\"\n : undefined\n )}\n />\n </div>\n\n {/**\n * This invisible marker is a trick which allows us to use IntersectionObserver to detect when the\n * scrollable area is fully scrolled to the bottom instead of manually tracking the scroll position\n * and having to deal with resizes, etc.\n *\n * It's positioned at the bottom of the scrollable area and reliably only becomes \"visible\" to the\n * IntersectionObserver when the scrollable area is scrolled to the bottom.\n */}\n {messages && messages.length > 0 ? (\n <div\n ref={bottomMarkerRef}\n style={{ position: \"sticky\", height: 0 }}\n aria-hidden\n data-bottom-marker=\"\"\n >\n {/**\n * This inner marker is absolutely offset by the same distance as the trailing space so its\n * visibility means the scrollable area is at the bottom of the messages, not the full bottom.\n */}\n <div\n ref={bottomTrailingMarkerRef}\n style={{\n position: \"absolute\",\n height: 0,\n }}\n data-bottom-trailing-marker=\"\"\n />\n </div>\n ) : null}\n </div>\n );\n }\n);\n"],"names":["jsx","SpinnerIcon","forwardRef","useEffect","messages","useIntersectionCallback","cn","AiChatUserMessage","AiChatAssistantMessage","overrides","useAiChatMessages","useState","useOverrides","useRef","useLatest","useImperativeHandle","jsxs","RegisterAiTool","Fragment","ArrowDownIcon","AiComposer"],"mappings":";;;;;;;;;;;;;;;AAwCA,MAAM,oCAAuC,GAAA,EAAA,CAAA;AA0G7C,MAAM,iBAAsC,GAAA;AAAA,EAC1C,OAAO,MAAM,IAAA;AAAA,EACb,OAAA,EAAS,sBACNA,cAAA,CAAA,KAAA,EAAA;AAAA,IAAI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AbK,MAcP,CAAA;AAAA,WAAA;AAAA,SACF,CAAA;AAAA,QAUC,QAAY,IAAA,QAAA,CAAS,MAAS,GAAA,CAAA,mBAC5BN,cAAA,CAAA,KAAA,EAAA;AAAA,UACC,GAAK,EAAA,eAAA;AAAA,UACL,KAAO,EAAA,EAAE,QAAU,EAAA,QAAA,EAAU,QAAQ,CAAE,EAAA;AAAA,UACvC,aAAW,EAAA,IAAA;AAAA,UACX,oBAAmB,EAAA,EAAA;AAAA,UAMnB,QAAC,kBAAAA,cAAA,CAAA,KAAA,EAAA;AAAA,YACC,GAAK,EAAA,uBAAA;AAAA,YACL,KAAO,EAAA;AAAA,cACL,QAAU,EAAA,UAAA;AAAA,cACV,MAAQ,EAAA,CAAA;AAAA,aACV;AAAA,YACA,6BAA4B,EAAA,EAAA;AAAA,WAC9B,CAAA;AAAA,SACF,CACE,GAAA,IAAA;AAAA,OAAA;AAAA,KACN,CAAA,CAAA;AAAA,GAEJ;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"AiChat.cjs","sources":["../../src/components/AiChat.tsx"],"sourcesContent":["import type {\n AiKnowledgeSource,\n AiOpaqueToolDefinition,\n CopilotId,\n MessageId,\n} from \"@liveblocks/core\";\nimport {\n RegisterAiKnowledge,\n RegisterAiTool,\n useAiChatMessages,\n} from \"@liveblocks/react\";\nimport { useLatest } from \"@liveblocks/react/_private\";\nimport {\n type ComponentProps,\n type ComponentType,\n forwardRef,\n type MutableRefObject,\n useEffect,\n useImperativeHandle,\n useRef,\n useState,\n} from \"react\";\n\nimport type { GlobalComponents } from \"../components\";\nimport { ArrowDownIcon } from \"../icons/ArrowDown\";\nimport { SpinnerIcon } from \"../icons/Spinner\";\nimport {\n type AiChatMessageOverrides,\n type AiChatOverrides,\n type AiComposerOverrides,\n type GlobalOverrides,\n useOverrides,\n} from \"../overrides\";\nimport type { MarkdownComponents } from \"../primitives/Markdown\";\nimport { cn } from \"../utils/cn\";\nimport { useIntersectionCallback } from \"../utils/use-visible\";\nimport { AiChatAssistantMessage } from \"./internal/AiChatAssistantMessage\";\nimport { AiChatUserMessage } from \"./internal/AiChatUserMessage\";\nimport { AiComposer, type AiComposerProps } from \"./internal/AiComposer\";\n\n/**\n * The minimum number of pixels from the bottom of the scrollable area\n * before showing the scroll to bottom indicator.\n */\nconst MIN_DISTANCE_BOTTOM_SCROLL_INDICATOR = 60;\n\nexport type AiChatComponentsEmptyProps = {\n /**\n * The chat ID provided to the `AiChat` component.\n */\n chatId: string;\n\n /**\n * The copilot ID provided to the `AiChat` component.\n */\n copilotId?: string;\n};\n\nexport type AiChatComponentsLoadingProps = Record<string, never>;\n\nexport type AiChatComponents = {\n /**\n * The component used to render the empty state of the chat.\n */\n Empty: ComponentType<AiChatComponentsEmptyProps>;\n\n /**\n * The component used to render the loading state of the chat.\n */\n Loading: ComponentType<AiChatComponentsLoadingProps>;\n\n /**\n * The components used to render Markdown content.\n */\n markdown?: Partial<MarkdownComponents>;\n};\n\nexport interface AiChatProps extends ComponentProps<\"div\"> {\n /**\n * The ID of the chat the composer belongs to.\n */\n chatId: string;\n\n /**\n * Whether to focus the chat composer on mount.\n */\n autoFocus?: boolean;\n\n /**\n * The ID of the copilot to use to send the message.\n */\n copilotId?: string;\n\n /**\n * The contextual knowledge to include in the chat. May be used by the\n * assistant when generating responses. In addition to the knowledge passed\n * in via this prop, the AiChat instance will also have access to any\n * globally registered knowledge via <RegisterAiKnowledge />.\n */\n knowledge?: AiKnowledgeSource[];\n\n /**\n * Tool definitions to make available within this chat. May be used by the assistant when generating responses.\n */\n tools?: Record<string, AiOpaqueToolDefinition>;\n\n /**\n * The event handler called when the composer is submitted.\n */\n onComposerSubmit?: AiComposerProps[\"onComposerSubmit\"];\n\n /**\n * The layout of the chat and its composer.\n */\n layout?: \"inset\" | \"compact\";\n\n /**\n * The time, in milliseconds, before an AI response will timeout.\n */\n responseTimeout?: number;\n\n /**\n * Override the component's strings.\n */\n overrides?: Partial<\n GlobalOverrides &\n AiComposerOverrides &\n AiChatMessageOverrides &\n AiChatOverrides\n >;\n\n /**\n * Override the component's components.\n */\n components?: Partial<GlobalComponents & AiChatComponents>;\n}\n\ninterface AiChatMessagesProps extends ComponentProps<\"div\"> {\n messages: NonNullable<ReturnType<typeof useAiChatMessages>[\"messages\"]>;\n overrides: AiChatProps[\"overrides\"];\n components: AiChatProps[\"components\"];\n lastSentMessageId: MessageId | null;\n scrollToBottom: MutableRefObject<\n (behavior: \"instant\" | \"smooth\", includeTrailingSpace?: boolean) => void\n >;\n onScrollAtBottomChange: MutableRefObject<\n (isScrollAtBottom: boolean | null) => void\n >;\n containerRef: MutableRefObject<HTMLDivElement | null>;\n footerRef: MutableRefObject<HTMLDivElement | null>;\n messagesRef: MutableRefObject<HTMLDivElement | null>;\n bottomTrailingMarkerRef: MutableRefObject<HTMLDivElement | null>;\n trailingSpacerRef: MutableRefObject<HTMLDivElement | null>;\n}\n\nconst defaultComponents: AiChatComponents = {\n Empty: () => null,\n Loading: () => (\n <div className=\"lb-loading lb-ai-chat-loading\">\n <SpinnerIcon />\n </div>\n ),\n};\n\nconst AiChatMessages = forwardRef<HTMLDivElement, AiChatMessagesProps>(\n (\n {\n messages,\n overrides,\n components,\n lastSentMessageId,\n scrollToBottom,\n onScrollAtBottomChange,\n containerRef,\n footerRef,\n messagesRef,\n bottomTrailingMarkerRef,\n trailingSpacerRef,\n className,\n ...props\n },\n forwardedRef\n ) => {\n const hasLastSentMessage = lastSentMessageId !== null;\n\n /**\n * Every time the container, footer, or messages list change size,\n * we calculate the trailing space that would allow the penultimate\n * message to be at the top of the viewport, and apply it.\n *\n * ┌─────────────────────────────────────────┐▲ A = The `scroll-margin-top`\n * │ ┌─────────────────────────┐ │▼▲ value of the penultimate message\n * │ │ The penultimate message │ │ │\n * │ └─────────────────────────┘ │ │ B = The height from the top of\n * │ │ │ the penultimate message to the\n * │ ┌─────────────────────────┐ │ │ bottom of the messages list,\n * │ │ The last message │ │ │ including the messages' heights,\n * │ └─────────────────────────┘ │ │ and any padding, gap, etc\n * │ │ │\n * ├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤▲▼\n * │ ││ The trailing space needed to\n * │ = container height - (A + B + C) ││ allow the penultimate message\n * │ ││ to be at the top of the viewport\n * ├ ┬─────────────────────────────────────┬ ┤▼▲\n * │ │ │ │ │\n * │ │ │ │ │ C = The footer's height,\n * │ │ │ │ │ including any padding\n * │ └─────────────────────────────────────┘ │ │\n * └─────────────────────────────────────────┘ ▼\n */\n useEffect(\n () => {\n if (!hasLastSentMessage) {\n return;\n }\n\n const container = containerRef.current;\n const footer = footerRef.current;\n const messages = messagesRef.current;\n\n if (!container || !footer || !messages) {\n return;\n }\n\n const trailingSpacer = trailingSpacerRef.current;\n const bottomTrailingMarker = bottomTrailingMarkerRef.current;\n\n let containerHeight: number | undefined = undefined;\n let footerHeight: number | undefined = undefined;\n let messagesHeight: number | undefined = undefined;\n\n const resetTrailingSpace = () => {\n trailingSpacer?.style.removeProperty(\"height\");\n bottomTrailingMarker?.style.removeProperty(\"top\");\n };\n\n const updateTrailingSpace = (\n updatedContainerHeight?: number,\n updatedFooterHeight?: number,\n updatedMessagesHeight?: number\n ) => {\n if (!trailingSpacer || !bottomTrailingMarker) {\n return;\n }\n\n const lastMessage = messages.lastElementChild;\n const penultimateMessage = lastMessage?.previousElementSibling;\n\n if (updatedContainerHeight === undefined) {\n updatedContainerHeight = container.getBoundingClientRect().height;\n }\n\n if (updatedFooterHeight === undefined) {\n updatedFooterHeight = footer.getBoundingClientRect().height;\n }\n\n if (updatedMessagesHeight === undefined) {\n updatedMessagesHeight = messages.getBoundingClientRect().height;\n }\n\n // If the heights haven't changed, there's no need to update the trailing space.\n if (\n updatedContainerHeight === containerHeight &&\n updatedFooterHeight === footerHeight &&\n updatedMessagesHeight === messagesHeight\n ) {\n if (\n !lastMessage ||\n !penultimateMessage ||\n container.scrollHeight === container.clientHeight\n ) {\n resetTrailingSpace();\n }\n return;\n }\n\n // Now that we have compared them, we can update the heights.\n containerHeight = updatedContainerHeight;\n footerHeight = updatedFooterHeight;\n messagesHeight = updatedMessagesHeight;\n\n // If there's no last pair of messages, there's no need for any trailing space.\n if (!lastMessage || !penultimateMessage) {\n resetTrailingSpace();\n return;\n }\n\n // If the container isn't scrollable, there's no need for trailing space.\n if (container.scrollHeight === container.clientHeight) {\n resetTrailingSpace();\n return;\n }\n\n // A\n const penultimateMessageScrollMarginTop = Number.parseFloat(\n getComputedStyle(penultimateMessage as HTMLElement).scrollMarginTop\n );\n\n // B\n const messagesRect = messages.getBoundingClientRect();\n const penultimateMessageRect =\n penultimateMessage.getBoundingClientRect();\n const heightFromPenultimateMessageTopToMessagesListBottom =\n messagesRect.bottom - penultimateMessageRect.top;\n\n // A + B + C\n const differenceHeight =\n penultimateMessageScrollMarginTop +\n heightFromPenultimateMessageTopToMessagesListBottom +\n (footerHeight ?? 0);\n\n // = container height - (A + B + C)\n const trailingSpace = Math.max(containerHeight - differenceHeight, 0);\n\n // Update the trailing space.\n trailingSpacer.style.height = `${trailingSpace}px`;\n\n // Offset what \"the bottom\" is to the \"scroll at the bottom\" detection logic,\n // so that it doesn't include the trailing space.\n bottomTrailingMarker.style.top = `${-trailingSpace}px`;\n };\n\n const resizeObserver = new ResizeObserver((entries) => {\n let updatedContainerHeight: number | undefined = containerHeight;\n let updatedFooterHeight: number | undefined = footerHeight;\n let updatedMessagesHeight: number | undefined = messagesHeight;\n\n for (const entry of entries) {\n const entryHeight =\n entry.borderBoxSize?.[0]?.blockSize ?? entry.contentRect.height;\n\n if (entry.target === container) {\n updatedContainerHeight = entryHeight;\n } else if (entry.target === footer) {\n updatedFooterHeight = entryHeight;\n } else if (entry.target === messages) {\n updatedMessagesHeight = entryHeight;\n }\n }\n\n updateTrailingSpace(\n updatedContainerHeight,\n updatedFooterHeight,\n updatedMessagesHeight\n );\n });\n\n resizeObserver.observe(container);\n resizeObserver.observe(footer);\n resizeObserver.observe(messages);\n\n // Initialize before the first resize.\n requestAnimationFrame(() => updateTrailingSpace());\n\n return () => {\n resizeObserver.disconnect();\n resetTrailingSpace();\n };\n },\n // This effect only uses stable refs.\n [hasLastSentMessage] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n /**\n * Update the \"scroll at bottom\" state when needed.\n */\n useIntersectionCallback(\n bottomTrailingMarkerRef,\n (isIntersecting) => {\n onScrollAtBottomChange.current(isIntersecting);\n },\n { root: containerRef, rootMargin: MIN_DISTANCE_BOTTOM_SCROLL_INDICATOR }\n );\n\n /**\n * Instantly scroll to the bottom for the initial state.\n */\n useEffect(\n () => {\n scrollToBottom.current(\"instant\");\n },\n // `scrollToBottom` is a stable ref containing the callback.\n [] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n /**\n * Scroll to new messages when sending them.\n */\n useEffect(\n () => {\n if (lastSentMessageId) {\n scrollToBottom.current(\"smooth\", true);\n }\n },\n // `scrollToBottom` is a stable ref containing the callback.\n [lastSentMessageId] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n /**\n * Reset the \"scroll at bottom\" state when the component unmounts.\n */\n useEffect(\n () => {\n const onScrollAtBottomChangeCallback = onScrollAtBottomChange.current;\n\n return () => {\n onScrollAtBottomChangeCallback(null);\n };\n },\n // `onScrollAtBottomChange` is a stable ref containing the callback.\n [] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n return (\n <div\n className={cn(\"lb-ai-chat-messages\", className)}\n ref={forwardedRef}\n {...props}\n >\n {messages.map((message) => {\n if (message.role === \"user\") {\n return (\n <AiChatUserMessage\n key={message.id}\n message={message}\n overrides={overrides}\n components={components}\n />\n );\n } else if (message.role === \"assistant\") {\n return (\n <AiChatAssistantMessage\n key={message.id}\n message={message}\n overrides={overrides}\n components={components}\n />\n );\n } else {\n return null;\n }\n })}\n </div>\n );\n }\n);\n\nexport const AiChat = forwardRef<HTMLDivElement, AiChatProps>(\n (\n {\n chatId,\n copilotId,\n autoFocus,\n overrides,\n knowledge: localKnowledge,\n tools = {},\n onComposerSubmit,\n layout = \"inset\",\n components,\n className,\n responseTimeout,\n ...props\n },\n forwardedRef\n ) => {\n const { messages, isLoading, error } = useAiChatMessages(chatId);\n const [lastSentMessageId, setLastSentMessageId] =\n useState<MessageId | null>(null);\n\n const $ = useOverrides(overrides);\n const Empty = components?.Empty ?? defaultComponents.Empty;\n const Loading = components?.Loading ?? defaultComponents.Loading;\n\n const containerRef = useRef<HTMLDivElement | null>(null);\n const messagesRef = useRef<HTMLDivElement | null>(null);\n const footerRef = useRef<HTMLDivElement | null>(null);\n const bottomMarkerRef = useRef<HTMLDivElement | null>(null);\n const bottomTrailingMarkerRef = useRef<HTMLDivElement | null>(null);\n const trailingSpacerRef = useRef<HTMLDivElement | null>(null);\n\n const [isScrollAtBottom, setScrollAtBottom] = useState<boolean | null>(\n null\n );\n // `useState`'s setter is stable but this is for clarity in the places it's used.\n const onScrollAtBottomChange = useLatest(setScrollAtBottom);\n const isScrollIndicatorVisible =\n messages && isScrollAtBottom !== null ? !isScrollAtBottom : false;\n\n useImperativeHandle<HTMLDivElement | null, HTMLDivElement | null>(\n forwardedRef,\n () => containerRef.current,\n []\n );\n\n const scrollToBottom = useLatest(\n (behavior: \"instant\" | \"smooth\", includeTrailingSpace = false) => {\n if (includeTrailingSpace) {\n // Scroll to the bottom marker to include the trailing space,\n // but wait for the next tick in case the trailing space hasn't\n // been updated yet. (e.g. when sending a new message)\n setTimeout(() => {\n bottomMarkerRef.current?.scrollIntoView({\n behavior,\n block: \"end\",\n });\n }, 0);\n } else {\n // Scroll to the trailing space marker to only scroll to the\n // bottom of the messages, without including the trailing space.\n bottomTrailingMarkerRef.current?.scrollIntoView({\n behavior,\n block: \"end\",\n });\n }\n }\n );\n\n return (\n <div\n ref={containerRef}\n {...props}\n className={cn(\n \"lb-root lb-ai-chat\",\n `lb-ai-chat:layout-${layout}`,\n className\n )}\n >\n {Object.entries(tools).map(([name, tool]) => (\n <RegisterAiTool key={name} chatId={chatId} name={name} tool={tool} />\n ))}\n\n {localKnowledge\n ? localKnowledge.map((knowledge, index) => (\n <RegisterAiKnowledge\n key={`${index}:${knowledge.description}`}\n chatId={chatId}\n {...knowledge}\n />\n ))\n : null}\n\n <div className=\"lb-ai-chat-content\">\n {isLoading ? (\n <Loading />\n ) : error !== undefined ? (\n <div className=\"lb-error lb-ai-chat-error\">\n {$.AI_CHAT_MESSAGES_ERROR(error)}\n </div>\n ) : messages.length === 0 ? (\n <Empty chatId={chatId} copilotId={copilotId} />\n ) : (\n <>\n <AiChatMessages\n ref={messagesRef}\n messages={messages}\n overrides={overrides}\n components={components}\n lastSentMessageId={lastSentMessageId}\n scrollToBottom={scrollToBottom}\n onScrollAtBottomChange={onScrollAtBottomChange}\n containerRef={containerRef}\n footerRef={footerRef}\n messagesRef={messagesRef}\n bottomTrailingMarkerRef={bottomTrailingMarkerRef}\n trailingSpacerRef={trailingSpacerRef}\n />\n\n {/**\n * This trailing spacer is used to extend the scrollable area beyond its actual\n * content, to allow messages to appear at the top of the viewport for example.\n */}\n <div\n ref={trailingSpacerRef}\n data-trailing-spacer=\"\"\n style={{\n pointerEvents: \"none\",\n height: 0,\n }}\n aria-hidden\n />\n </>\n )}\n </div>\n\n <div className=\"lb-ai-chat-footer\" ref={footerRef}>\n <div className=\"lb-ai-chat-footer-actions\">\n <div\n className=\"lb-root lb-elevation lb-elevation-moderate lb-ai-chat-scroll-indicator\"\n data-visible={isScrollIndicatorVisible ? \"\" : undefined}\n >\n <button\n className=\"lb-ai-chat-scroll-indicator-button\"\n tabIndex={isScrollIndicatorVisible ? 0 : -1}\n aria-hidden={!isScrollIndicatorVisible}\n onClick={() => scrollToBottom.current(\"smooth\")}\n >\n <span className=\"lb-icon-container\">\n <ArrowDownIcon />\n </span>\n </button>\n </div>\n </div>\n <AiComposer\n key={chatId}\n chatId={chatId}\n copilotId={copilotId as CopilotId}\n overrides={overrides}\n autoFocus={autoFocus}\n responseTimeout={responseTimeout}\n onComposerSubmit={onComposerSubmit}\n onComposerSubmitted={({ id }) => setLastSentMessageId(id)}\n className={cn(\n \"lb-ai-chat-composer\",\n layout === \"inset\"\n ? \"lb-elevation lb-elevation-moderate\"\n : undefined\n )}\n />\n </div>\n\n {/**\n * This invisible marker is a trick which allows us to use IntersectionObserver to detect when the\n * scrollable area is fully scrolled to the bottom instead of manually tracking the scroll position\n * and having to deal with resizes, etc.\n *\n * It's positioned at the bottom of the scrollable area and reliably only becomes \"visible\" to the\n * IntersectionObserver when the scrollable area is scrolled to the bottom.\n */}\n {messages && messages.length > 0 ? (\n <div\n ref={bottomMarkerRef}\n style={{ position: \"sticky\", height: 0 }}\n aria-hidden\n data-bottom-marker=\"\"\n >\n {/**\n * This inner marker is absolutely offset by the same distance as the trailing space so its\n * visibility means the scrollable area is at the bottom of the messages, not the full bottom.\n */}\n <div\n ref={bottomTrailingMarkerRef}\n style={{\n position: \"absolute\",\n height: 0,\n }}\n data-bottom-trailing-marker=\"\"\n />\n </div>\n ) : null}\n </div>\n );\n }\n);\n"],"names":["jsx","SpinnerIcon","forwardRef","useEffect","messages","useIntersectionCallback","cn","AiChatUserMessage","AiChatAssistantMessage","overrides","useAiChatMessages","useState","useOverrides","useRef","useLatest","useImperativeHandle","jsxs","RegisterAiTool","RegisterAiKnowledge","Fragment","ArrowDownIcon","AiComposer"],"mappings":";;;;;;;;;;;;;;;AA4CA,MAAM,oCAAuC,GAAA,EAAA,CAAA;AA+G7C,MAAM,iBAAsC,GAAA;AAAA,EAC1C,OAAO,MAAM,IAAA;AAAA,EACb,OAAA,EAAS,sBACNA,cAAA,CAAA,KAAA,EAAA;AAAA,IAAI,SAAU,EAAA,+BAAA;AAAA,IACb,yCAACC,mBAAY,EAAA,EAAA,CAAA;AAAA,GACf,CAAA;AAEJ,CAAA,CAAA;AAEA,MAAM,cAAiB,GAAAC,gBAAA;AAAA,EACrB,CACE;AAAA,IACE,QAAA;AAAA,IACA,SAAA;AAAA,IACA,UAAA;AAAA,IACA,iBAAA;AAAA,IACA,cAA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
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
|
-
import { useAiChatMessages, RegisterAiTool } from '@liveblocks/react';
|
|
2
|
+
import { useAiChatMessages, RegisterAiTool, RegisterAiKnowledge } from '@liveblocks/react';
|
|
3
3
|
import { useLatest } from '@liveblocks/react/_private';
|
|
4
4
|
import { forwardRef, useEffect, useState, useRef, useImperativeHandle } from 'react';
|
|
5
5
|
import { ArrowDownIcon } from '../icons/ArrowDown.js';
|
|
@@ -49,50 +49,45 @@ const AiChatMessages = forwardRef(
|
|
|
49
49
|
}
|
|
50
50
|
const trailingSpacer = trailingSpacerRef.current;
|
|
51
51
|
const bottomTrailingMarker = bottomTrailingMarkerRef.current;
|
|
52
|
-
let containerHeight =
|
|
53
|
-
let footerHeight =
|
|
54
|
-
let messagesHeight =
|
|
52
|
+
let containerHeight = void 0;
|
|
53
|
+
let footerHeight = void 0;
|
|
54
|
+
let messagesHeight = void 0;
|
|
55
55
|
const resetTrailingSpace = () => {
|
|
56
56
|
trailingSpacer?.style.removeProperty("height");
|
|
57
57
|
bottomTrailingMarker?.style.removeProperty("top");
|
|
58
58
|
};
|
|
59
|
-
const
|
|
59
|
+
const updateTrailingSpace = (updatedContainerHeight, updatedFooterHeight, updatedMessagesHeight) => {
|
|
60
60
|
if (!trailingSpacer || !bottomTrailingMarker) {
|
|
61
61
|
return;
|
|
62
62
|
}
|
|
63
63
|
const lastMessage = messages2.lastElementChild;
|
|
64
64
|
const penultimateMessage = lastMessage?.previousElementSibling;
|
|
65
|
-
if (
|
|
66
|
-
|
|
67
|
-
return;
|
|
65
|
+
if (updatedContainerHeight === void 0) {
|
|
66
|
+
updatedContainerHeight = container.getBoundingClientRect().height;
|
|
68
67
|
}
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
let updatedContainerHeight = containerHeight;
|
|
74
|
-
let updatedFooterHeight = footerHeight;
|
|
75
|
-
let updatedMessagesHeight = messagesHeight;
|
|
76
|
-
for (const entry of entries) {
|
|
77
|
-
const entryHeight = entry.borderBoxSize?.[0]?.blockSize ?? entry.contentRect.height;
|
|
78
|
-
if (entry.target === container) {
|
|
79
|
-
updatedContainerHeight = entryHeight ?? null;
|
|
80
|
-
} else if (entry.target === footer) {
|
|
81
|
-
updatedFooterHeight = entryHeight ?? null;
|
|
82
|
-
} else if (entry.target === messages2) {
|
|
83
|
-
updatedMessagesHeight = entryHeight ?? null;
|
|
84
|
-
}
|
|
68
|
+
if (updatedFooterHeight === void 0) {
|
|
69
|
+
updatedFooterHeight = footer.getBoundingClientRect().height;
|
|
85
70
|
}
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
return;
|
|
71
|
+
if (updatedMessagesHeight === void 0) {
|
|
72
|
+
updatedMessagesHeight = messages2.getBoundingClientRect().height;
|
|
89
73
|
}
|
|
90
74
|
if (updatedContainerHeight === containerHeight && updatedFooterHeight === footerHeight && updatedMessagesHeight === messagesHeight) {
|
|
75
|
+
if (!lastMessage || !penultimateMessage || container.scrollHeight === container.clientHeight) {
|
|
76
|
+
resetTrailingSpace();
|
|
77
|
+
}
|
|
91
78
|
return;
|
|
92
79
|
}
|
|
93
80
|
containerHeight = updatedContainerHeight;
|
|
94
81
|
footerHeight = updatedFooterHeight;
|
|
95
82
|
messagesHeight = updatedMessagesHeight;
|
|
83
|
+
if (!lastMessage || !penultimateMessage) {
|
|
84
|
+
resetTrailingSpace();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (container.scrollHeight === container.clientHeight) {
|
|
88
|
+
resetTrailingSpace();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
96
91
|
const penultimateMessageScrollMarginTop = Number.parseFloat(
|
|
97
92
|
getComputedStyle(penultimateMessage).scrollMarginTop
|
|
98
93
|
);
|
|
@@ -103,10 +98,31 @@ const AiChatMessages = forwardRef(
|
|
|
103
98
|
const trailingSpace = Math.max(containerHeight - differenceHeight, 0);
|
|
104
99
|
trailingSpacer.style.height = `${trailingSpace}px`;
|
|
105
100
|
bottomTrailingMarker.style.top = `${-trailingSpace}px`;
|
|
101
|
+
};
|
|
102
|
+
const resizeObserver = new ResizeObserver((entries) => {
|
|
103
|
+
let updatedContainerHeight = containerHeight;
|
|
104
|
+
let updatedFooterHeight = footerHeight;
|
|
105
|
+
let updatedMessagesHeight = messagesHeight;
|
|
106
|
+
for (const entry of entries) {
|
|
107
|
+
const entryHeight = entry.borderBoxSize?.[0]?.blockSize ?? entry.contentRect.height;
|
|
108
|
+
if (entry.target === container) {
|
|
109
|
+
updatedContainerHeight = entryHeight;
|
|
110
|
+
} else if (entry.target === footer) {
|
|
111
|
+
updatedFooterHeight = entryHeight;
|
|
112
|
+
} else if (entry.target === messages2) {
|
|
113
|
+
updatedMessagesHeight = entryHeight;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
updateTrailingSpace(
|
|
117
|
+
updatedContainerHeight,
|
|
118
|
+
updatedFooterHeight,
|
|
119
|
+
updatedMessagesHeight
|
|
120
|
+
);
|
|
106
121
|
});
|
|
107
122
|
resizeObserver.observe(container);
|
|
108
123
|
resizeObserver.observe(footer);
|
|
109
124
|
resizeObserver.observe(messages2);
|
|
125
|
+
requestAnimationFrame(() => updateTrailingSpace());
|
|
110
126
|
return () => {
|
|
111
127
|
resizeObserver.disconnect();
|
|
112
128
|
resetTrailingSpace();
|
|
@@ -180,6 +196,7 @@ const AiChat = forwardRef(
|
|
|
180
196
|
layout = "inset",
|
|
181
197
|
components,
|
|
182
198
|
className,
|
|
199
|
+
responseTimeout,
|
|
183
200
|
...props
|
|
184
201
|
}, forwardedRef) => {
|
|
185
202
|
const { messages, isLoading, error } = useAiChatMessages(chatId);
|
|
@@ -206,12 +223,12 @@ const AiChat = forwardRef(
|
|
|
206
223
|
const scrollToBottom = useLatest(
|
|
207
224
|
(behavior, includeTrailingSpace = false) => {
|
|
208
225
|
if (includeTrailingSpace) {
|
|
209
|
-
|
|
226
|
+
setTimeout(() => {
|
|
210
227
|
bottomMarkerRef.current?.scrollIntoView({
|
|
211
228
|
behavior,
|
|
212
229
|
block: "end"
|
|
213
230
|
});
|
|
214
|
-
});
|
|
231
|
+
}, 0);
|
|
215
232
|
} else {
|
|
216
233
|
bottomTrailingMarkerRef.current?.scrollIntoView({
|
|
217
234
|
behavior,
|
|
@@ -234,6 +251,10 @@ const AiChat = forwardRef(
|
|
|
234
251
|
name,
|
|
235
252
|
tool
|
|
236
253
|
}, name)),
|
|
254
|
+
localKnowledge ? localKnowledge.map((knowledge, index) => /* @__PURE__ */ jsx(RegisterAiKnowledge, {
|
|
255
|
+
chatId,
|
|
256
|
+
...knowledge
|
|
257
|
+
}, `${index}:${knowledge.description}`)) : null,
|
|
237
258
|
/* @__PURE__ */ jsx("div", {
|
|
238
259
|
className: "lb-ai-chat-content",
|
|
239
260
|
children: isLoading ? /* @__PURE__ */ jsx(Loading, {}) : error !== void 0 ? /* @__PURE__ */ jsx("div", {
|
|
@@ -262,7 +283,8 @@ const AiChat = forwardRef(
|
|
|
262
283
|
ref: trailingSpacerRef,
|
|
263
284
|
"data-trailing-spacer": "",
|
|
264
285
|
style: {
|
|
265
|
-
pointerEvents: "none"
|
|
286
|
+
pointerEvents: "none",
|
|
287
|
+
height: 0
|
|
266
288
|
},
|
|
267
289
|
"aria-hidden": true
|
|
268
290
|
})
|
|
@@ -295,7 +317,7 @@ const AiChat = forwardRef(
|
|
|
295
317
|
copilotId,
|
|
296
318
|
overrides,
|
|
297
319
|
autoFocus,
|
|
298
|
-
|
|
320
|
+
responseTimeout,
|
|
299
321
|
onComposerSubmit,
|
|
300
322
|
onComposerSubmitted: ({ id }) => setLastSentMessageId(id),
|
|
301
323
|
className: cn(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AiChat.js","sources":["../../src/components/AiChat.tsx"],"sourcesContent":["import type {\n AiKnowledgeSource,\n AiOpaqueToolDefinition,\n CopilotId,\n MessageId,\n} from \"@liveblocks/core\";\nimport { RegisterAiTool, useAiChatMessages } from \"@liveblocks/react\";\nimport { useLatest } from \"@liveblocks/react/_private\";\nimport {\n type ComponentProps,\n type ComponentType,\n forwardRef,\n type MutableRefObject,\n useEffect,\n useImperativeHandle,\n useRef,\n useState,\n} from \"react\";\n\nimport type { GlobalComponents } from \"../components\";\nimport { ArrowDownIcon } from \"../icons/ArrowDown\";\nimport { SpinnerIcon } from \"../icons/Spinner\";\nimport {\n type AiChatMessageOverrides,\n type AiChatOverrides,\n type AiComposerOverrides,\n type GlobalOverrides,\n useOverrides,\n} from \"../overrides\";\nimport type { MarkdownComponents } from \"../primitives/Markdown\";\nimport { cn } from \"../utils/cn\";\nimport { useIntersectionCallback } from \"../utils/use-visible\";\nimport { AiChatAssistantMessage } from \"./internal/AiChatAssistantMessage\";\nimport { AiChatUserMessage } from \"./internal/AiChatUserMessage\";\nimport { AiComposer, type AiComposerProps } from \"./internal/AiComposer\";\n\n/**\n * The minimum number of pixels from the bottom of the scrollable area\n * before showing the scroll to bottom indicator.\n */\nconst MIN_DISTANCE_BOTTOM_SCROLL_INDICATOR = 60;\n\nexport type AiChatComponentsEmptyProps = {\n /**\n * The chat ID provided to the `AiChat` component.\n */\n chatId: string;\n\n /**\n * The copilot ID provided to the `AiChat` component.\n */\n copilotId?: string;\n};\n\nexport type AiChatComponentsLoadingProps = Record<string, never>;\n\nexport type AiChatComponents = {\n /**\n * The component used to render the empty state of the chat.\n */\n Empty: ComponentType<AiChatComponentsEmptyProps>;\n\n /**\n * The component used to render the loading state of the chat.\n */\n Loading: ComponentType<AiChatComponentsLoadingProps>;\n\n /**\n * The components used to render Markdown content.\n */\n markdown?: Partial<MarkdownComponents>;\n};\n\nexport interface AiChatProps extends ComponentProps<\"div\"> {\n /**\n * The ID of the chat the composer belongs to.\n */\n chatId: string;\n\n /**\n * Whether to focus the chat composer on mount.\n */\n autoFocus?: boolean;\n\n /**\n * The ID of the copilot to use to send the message.\n */\n copilotId?: string;\n\n /**\n * The contextual knowledge to include in the chat. May be used by the\n * assistant when generating responses. In addition to the knowledge passed\n * in via this prop, the AiChat instance will also have access to any\n * globally registered knowledge via <RegisterAiKnowledge />.\n */\n knowledge?: AiKnowledgeSource[];\n\n /**\n * Tool definitions to make available within this chat. May be used by the assistant when generating responses.\n */\n tools?: Record<string, AiOpaqueToolDefinition>;\n\n /**\n * The event handler called when the composer is submitted.\n */\n onComposerSubmit?: AiComposerProps[\"onComposerSubmit\"];\n\n /**\n * The layout of the chat and its composer.\n */\n layout?: \"inset\" | \"compact\";\n\n /**\n * Override the component's strings.\n */\n overrides?: Partial<\n GlobalOverrides &\n AiComposerOverrides &\n AiChatMessageOverrides &\n AiChatOverrides\n >;\n\n /**\n * Override the component's components.\n */\n components?: Partial<GlobalComponents & AiChatComponents>;\n}\n\ninterface AiChatMessagesProps extends ComponentProps<\"div\"> {\n messages: NonNullable<ReturnType<typeof useAiChatMessages>[\"messages\"]>;\n overrides: AiChatProps[\"overrides\"];\n components: AiChatProps[\"components\"];\n lastSentMessageId: MessageId | null;\n scrollToBottom: MutableRefObject<\n (behavior: \"instant\" | \"smooth\", includeTrailingSpace?: boolean) => void\n >;\n onScrollAtBottomChange: MutableRefObject<\n (isScrollAtBottom: boolean | null) => void\n >;\n containerRef: MutableRefObject<HTMLDivElement | null>;\n footerRef: MutableRefObject<HTMLDivElement | null>;\n messagesRef: MutableRefObject<HTMLDivElement | null>;\n bottomTrailingMarkerRef: MutableRefObject<HTMLDivElement | null>;\n trailingSpacerRef: MutableRefObject<HTMLDivElement | null>;\n}\n\nconst defaultComponents: AiChatComponents = {\n Empty: () => null,\n Loading: () => (\n <div className=\"lb-loading lb-ai-chat-loading\">\n <SpinnerIcon />\n </div>\n ),\n};\n\nconst AiChatMessages = forwardRef<HTMLDivElement, AiChatMessagesProps>(\n (\n {\n messages,\n overrides,\n components,\n lastSentMessageId,\n scrollToBottom,\n onScrollAtBottomChange,\n containerRef,\n footerRef,\n messagesRef,\n bottomTrailingMarkerRef,\n trailingSpacerRef,\n className,\n ...props\n },\n forwardedRef\n ) => {\n const hasLastSentMessage = lastSentMessageId !== null;\n\n /**\n * Every time the container, footer, or messages list change size,\n * we calculate the trailing space that would allow the penultimate\n * message to be at the top of the viewport, and apply it.\n *\n * ┌─────────────────────────────────────────┐▲ A = The `scroll-margin-top`\n * │ ┌─────────────────────────┐ │▼▲ value of the penultimate message\n * │ │ The penultimate message │ │ │\n * │ └─────────────────────────┘ │ │ B = The height from the top of\n * │ │ │ the penultimate message to the\n * │ ┌─────────────────────────┐ │ │ bottom of the messages list,\n * │ │ The last message │ │ │ including the messages' heights,\n * │ └─────────────────────────┘ │ │ and any padding, gap, etc\n * │ │ │\n * ├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤▲▼\n * │ ││ The trailing space needed to\n * │ = container height - (A + B + C) ││ allow the penultimate message\n * │ ││ to be at the top of the viewport\n * ├ ┬─────────────────────────────────────┬ ┤▼▲\n * │ │ │ │ │\n * │ │ │ │ │ C = The footer's height,\n * │ │ │ │ │ including any padding\n * │ └─────────────────────────────────────┘ │ │\n * └─────────────────────────────────────────┘ ▼\n */\n useEffect(\n () => {\n if (!hasLastSentMessage) {\n return;\n }\n\n const container = containerRef.current;\n const footer = footerRef.current;\n const messages = messagesRef.current;\n\n if (!container || !footer || !messages) {\n return;\n }\n\n const trailingSpacer = trailingSpacerRef.current;\n const bottomTrailingMarker = bottomTrailingMarkerRef.current;\n\n let containerHeight: number | null = null;\n let footerHeight: number | null = null;\n let messagesHeight: number | null = null;\n\n const resetTrailingSpace = () => {\n trailingSpacer?.style.removeProperty(\"height\");\n bottomTrailingMarker?.style.removeProperty(\"top\");\n };\n\n const resizeObserver = new ResizeObserver((entries) => {\n if (!trailingSpacer || !bottomTrailingMarker) {\n return;\n }\n\n const lastMessage = messages.lastElementChild;\n const penultimateMessage = lastMessage?.previousElementSibling;\n\n // If there's no last pair of messages, there's no need for any trailing space.\n if (!lastMessage || !penultimateMessage) {\n resetTrailingSpace();\n return;\n }\n\n // If the container's height is based on its content, the container isn't scrollable and there's no need for any trailing space.\n if (container.scrollHeight === container.clientHeight) {\n resetTrailingSpace();\n return;\n }\n\n let updatedContainerHeight: number | null = containerHeight;\n let updatedFooterHeight: number | null = footerHeight;\n let updatedMessagesHeight: number | null = messagesHeight;\n\n for (const entry of entries) {\n const entryHeight =\n entry.borderBoxSize?.[0]?.blockSize ?? entry.contentRect.height;\n\n if (entry.target === container) {\n updatedContainerHeight = entryHeight ?? null;\n } else if (entry.target === footer) {\n updatedFooterHeight = entryHeight ?? null;\n } else if (entry.target === messages) {\n updatedMessagesHeight = entryHeight ?? null;\n }\n }\n\n // If we don't have all the heights, we can't compute the trailing space.\n if (\n updatedContainerHeight === null ||\n updatedFooterHeight === null ||\n updatedMessagesHeight === null\n ) {\n resetTrailingSpace();\n return;\n }\n\n // If none of the heights have changed, we don't need to do anything.\n if (\n updatedContainerHeight === containerHeight &&\n updatedFooterHeight === footerHeight &&\n updatedMessagesHeight === messagesHeight\n ) {\n return;\n }\n\n // Now that we have compared them, we can update the heights.\n containerHeight = updatedContainerHeight;\n footerHeight = updatedFooterHeight;\n messagesHeight = updatedMessagesHeight;\n\n // A\n const penultimateMessageScrollMarginTop = Number.parseFloat(\n getComputedStyle(penultimateMessage as HTMLElement).scrollMarginTop\n );\n\n // B\n const messagesRect = messages.getBoundingClientRect();\n const penultimateMessageRect =\n penultimateMessage.getBoundingClientRect();\n const heightFromPenultimateMessageTopToMessagesListBottom =\n messagesRect.bottom - penultimateMessageRect.top;\n\n // A + B + C\n const differenceHeight =\n penultimateMessageScrollMarginTop +\n heightFromPenultimateMessageTopToMessagesListBottom +\n (footerHeight ?? 0);\n\n // = container height - (A + B + C)\n const trailingSpace = Math.max(containerHeight - differenceHeight, 0);\n\n // Update the trailing space.\n trailingSpacer.style.height = `${trailingSpace}px`;\n\n // Offset what \"the bottom\" is to the \"scroll at the bottom\" detection logic,\n // so that it doesn't include the trailing space.\n bottomTrailingMarker.style.top = `${-trailingSpace}px`;\n });\n\n resizeObserver.observe(container);\n resizeObserver.observe(footer);\n resizeObserver.observe(messages);\n\n return () => {\n resizeObserver.disconnect();\n resetTrailingSpace();\n };\n },\n // This effect only uses stable refs.\n [hasLastSentMessage] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n /**\n * Update the \"scroll at bottom\" state when needed.\n */\n useIntersectionCallback(\n bottomTrailingMarkerRef,\n (isIntersecting) => {\n onScrollAtBottomChange.current(isIntersecting);\n },\n { root: containerRef, rootMargin: MIN_DISTANCE_BOTTOM_SCROLL_INDICATOR }\n );\n\n /**\n * Instantly scroll to the bottom for the initial state.\n */\n useEffect(\n () => {\n scrollToBottom.current(\"instant\");\n },\n // `scrollToBottom` is a stable ref containing the callback.\n [] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n /**\n * Scroll to new messages when sending them.\n */\n useEffect(\n () => {\n if (lastSentMessageId) {\n scrollToBottom.current(\"smooth\", true);\n }\n },\n // `scrollToBottom` is a stable ref containing the callback.\n [lastSentMessageId] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n /**\n * Reset the \"scroll at bottom\" state when the component unmounts.\n */\n useEffect(\n () => {\n const onScrollAtBottomChangeCallback = onScrollAtBottomChange.current;\n\n return () => {\n onScrollAtBottomChangeCallback(null);\n };\n },\n // `onScrollAtBottomChange` is a stable ref containing the callback.\n [] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n return (\n <div\n className={cn(\"lb-ai-chat-messages\", className)}\n ref={forwardedRef}\n {...props}\n >\n {messages.map((message) => {\n if (message.role === \"user\") {\n return (\n <AiChatUserMessage\n key={message.id}\n message={message}\n overrides={overrides}\n components={components}\n />\n );\n } else if (message.role === \"assistant\") {\n return (\n <AiChatAssistantMessage\n key={message.id}\n message={message}\n overrides={overrides}\n components={components}\n />\n );\n } else {\n return null;\n }\n })}\n </div>\n );\n }\n);\n\nexport const AiChat = forwardRef<HTMLDivElement, AiChatProps>(\n (\n {\n chatId,\n copilotId,\n autoFocus,\n overrides,\n knowledge: localKnowledge,\n tools = {},\n onComposerSubmit,\n layout = \"inset\",\n components,\n className,\n ...props\n },\n forwardedRef\n ) => {\n const { messages, isLoading, error } = useAiChatMessages(chatId);\n const [lastSentMessageId, setLastSentMessageId] =\n useState<MessageId | null>(null);\n\n const $ = useOverrides(overrides);\n const Empty = components?.Empty ?? defaultComponents.Empty;\n const Loading = components?.Loading ?? defaultComponents.Loading;\n\n const containerRef = useRef<HTMLDivElement | null>(null);\n const messagesRef = useRef<HTMLDivElement | null>(null);\n const footerRef = useRef<HTMLDivElement | null>(null);\n const bottomMarkerRef = useRef<HTMLDivElement | null>(null);\n const bottomTrailingMarkerRef = useRef<HTMLDivElement | null>(null);\n const trailingSpacerRef = useRef<HTMLDivElement | null>(null);\n\n const [isScrollAtBottom, setScrollAtBottom] = useState<boolean | null>(\n null\n );\n // `useState`'s setter is stable but this is for clarity in the places it's used.\n const onScrollAtBottomChange = useLatest(setScrollAtBottom);\n const isScrollIndicatorVisible =\n messages && isScrollAtBottom !== null ? !isScrollAtBottom : false;\n\n useImperativeHandle<HTMLDivElement | null, HTMLDivElement | null>(\n forwardedRef,\n () => containerRef.current,\n []\n );\n\n const scrollToBottom = useLatest(\n (behavior: \"instant\" | \"smooth\", includeTrailingSpace = false) => {\n if (includeTrailingSpace) {\n // Scroll to the bottom marker to include the trailing space,\n // and wait for a frame in case the trailing space hasn't\n // been updated yet. (e.g. when sending a new message)\n requestAnimationFrame(() => {\n bottomMarkerRef.current?.scrollIntoView({\n behavior,\n block: \"end\",\n });\n });\n } else {\n // Scroll to the trailing space marker to only scroll to the\n // bottom of the messages, without including the trailing space.\n bottomTrailingMarkerRef.current?.scrollIntoView({\n behavior,\n block: \"end\",\n });\n }\n }\n );\n\n return (\n <div\n ref={containerRef}\n {...props}\n className={cn(\n \"lb-root lb-ai-chat\",\n `lb-ai-chat:layout-${layout}`,\n className\n )}\n >\n {Object.entries(tools).map(([name, tool]) => (\n <RegisterAiTool key={name} chatId={chatId} name={name} tool={tool} />\n ))}\n\n <div className=\"lb-ai-chat-content\">\n {isLoading ? (\n <Loading />\n ) : error !== undefined ? (\n <div className=\"lb-error lb-ai-chat-error\">\n {$.AI_CHAT_MESSAGES_ERROR(error)}\n </div>\n ) : messages.length === 0 ? (\n <Empty chatId={chatId} copilotId={copilotId} />\n ) : (\n <>\n <AiChatMessages\n ref={messagesRef}\n messages={messages}\n overrides={overrides}\n components={components}\n lastSentMessageId={lastSentMessageId}\n scrollToBottom={scrollToBottom}\n onScrollAtBottomChange={onScrollAtBottomChange}\n containerRef={containerRef}\n footerRef={footerRef}\n messagesRef={messagesRef}\n bottomTrailingMarkerRef={bottomTrailingMarkerRef}\n trailingSpacerRef={trailingSpacerRef}\n />\n\n {/**\n * This trailing spacer is used to extend the scrollable area beyond its actual\n * content, to allow messages to appear at the top of the viewport for example.\n */}\n <div\n ref={trailingSpacerRef}\n data-trailing-spacer=\"\"\n style={{\n pointerEvents: \"none\",\n }}\n aria-hidden\n />\n </>\n )}\n </div>\n\n <div className=\"lb-ai-chat-footer\" ref={footerRef}>\n <div className=\"lb-ai-chat-footer-actions\">\n <div\n className=\"lb-root lb-elevation lb-elevation-moderate lb-ai-chat-scroll-indicator\"\n data-visible={isScrollIndicatorVisible ? \"\" : undefined}\n >\n <button\n className=\"lb-ai-chat-scroll-indicator-button\"\n tabIndex={isScrollIndicatorVisible ? 0 : -1}\n aria-hidden={!isScrollIndicatorVisible}\n onClick={() => scrollToBottom.current(\"smooth\")}\n >\n <span className=\"lb-icon-container\">\n <ArrowDownIcon />\n </span>\n </button>\n </div>\n </div>\n <AiComposer\n key={chatId}\n chatId={chatId}\n copilotId={copilotId as CopilotId}\n overrides={overrides}\n autoFocus={autoFocus}\n knowledge={localKnowledge}\n onComposerSubmit={onComposerSubmit}\n onComposerSubmitted={({ id }) => setLastSentMessageId(id)}\n className={cn(\n \"lb-ai-chat-composer\",\n layout === \"inset\"\n ? \"lb-elevation lb-elevation-moderate\"\n : undefined\n )}\n />\n </div>\n\n {/**\n * This invisible marker is a trick which allows us to use IntersectionObserver to detect when the\n * scrollable area is fully scrolled to the bottom instead of manually tracking the scroll position\n * and having to deal with resizes, etc.\n *\n * It's positioned at the bottom of the scrollable area and reliably only becomes \"visible\" to the\n * IntersectionObserver when the scrollable area is scrolled to the bottom.\n */}\n {messages && messages.length > 0 ? (\n <div\n ref={bottomMarkerRef}\n style={{ position: \"sticky\", height: 0 }}\n aria-hidden\n data-bottom-marker=\"\"\n >\n {/**\n * This inner marker is absolutely offset by the same distance as the trailing space so its\n * visibility means the scrollable area is at the bottom of the messages, not the full bottom.\n */}\n <div\n ref={bottomTrailingMarkerRef}\n style={{\n position: \"absolute\",\n height: 0,\n }}\n data-bottom-trailing-marker=\"\"\n />\n </div>\n ) : null}\n </div>\n );\n }\n);\n"],"names":["messages"],"mappings":";;;;;;;;;;;;;AAwCA,MAAM,oCAAuC,GAAA,EAAA,CAAA;AA0G7C,MAAM,iBAAsC,GAAA;AAAA,EAC1C,OAAO,MAAM,IAAA;AAAA,EACb,OAAA,EAAS,sBACN,GAAA,CAAA,KAAA,EAAA;AAAA,IAAI,SAAU,EAAA,+BAAA;AAAA,IACb,8BAAC,WAAY,EAAA,EAAA,CAAA;AAAA,GACf,CAAA;AAEJ,CAAA,CAAA;AAEA,MAAM,cAAiB,GAAA,UAAA;AAAA,EACrB,CACE;AAAA,IACE,QAAA;AAAA,IACA,SAAA;AAAA,IACA,UAAA;AAAA,IACA,iBAAA;AAAA,IACA,cAAA;AAAA,IACA,sBAAA;AAAA,IACA,YAAA;AAAA,IACA,SAAA;AAAA,IACA,WAAA;AAAA,IACA,uBAAA;AAAA,IACA,iBAAA;AAAA,IACA,SAAA;AAAA,IACG,GAAA,KAAA;AAAA,KAEL,YACG,KAAA;AACH,IAAA,MAAM,qBAAqB,iBAAsB,KAAA,IAAA,CAAA;AA2BjD,IAAA,S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AA,OAAA;AAAA,KACN,CAAA,CAAA;AAAA,GAEJ;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"AiChat.js","sources":["../../src/components/AiChat.tsx"],"sourcesContent":["import type {\n AiKnowledgeSource,\n AiOpaqueToolDefinition,\n CopilotId,\n MessageId,\n} from \"@liveblocks/core\";\nimport {\n RegisterAiKnowledge,\n RegisterAiTool,\n useAiChatMessages,\n} from \"@liveblocks/react\";\nimport { useLatest } from \"@liveblocks/react/_private\";\nimport {\n type ComponentProps,\n type ComponentType,\n forwardRef,\n type MutableRefObject,\n useEffect,\n useImperativeHandle,\n useRef,\n useState,\n} from \"react\";\n\nimport type { GlobalComponents } from \"../components\";\nimport { ArrowDownIcon } from \"../icons/ArrowDown\";\nimport { SpinnerIcon } from \"../icons/Spinner\";\nimport {\n type AiChatMessageOverrides,\n type AiChatOverrides,\n type AiComposerOverrides,\n type GlobalOverrides,\n useOverrides,\n} from \"../overrides\";\nimport type { MarkdownComponents } from \"../primitives/Markdown\";\nimport { cn } from \"../utils/cn\";\nimport { useIntersectionCallback } from \"../utils/use-visible\";\nimport { AiChatAssistantMessage } from \"./internal/AiChatAssistantMessage\";\nimport { AiChatUserMessage } from \"./internal/AiChatUserMessage\";\nimport { AiComposer, type AiComposerProps } from \"./internal/AiComposer\";\n\n/**\n * The minimum number of pixels from the bottom of the scrollable area\n * before showing the scroll to bottom indicator.\n */\nconst MIN_DISTANCE_BOTTOM_SCROLL_INDICATOR = 60;\n\nexport type AiChatComponentsEmptyProps = {\n /**\n * The chat ID provided to the `AiChat` component.\n */\n chatId: string;\n\n /**\n * The copilot ID provided to the `AiChat` component.\n */\n copilotId?: string;\n};\n\nexport type AiChatComponentsLoadingProps = Record<string, never>;\n\nexport type AiChatComponents = {\n /**\n * The component used to render the empty state of the chat.\n */\n Empty: ComponentType<AiChatComponentsEmptyProps>;\n\n /**\n * The component used to render the loading state of the chat.\n */\n Loading: ComponentType<AiChatComponentsLoadingProps>;\n\n /**\n * The components used to render Markdown content.\n */\n markdown?: Partial<MarkdownComponents>;\n};\n\nexport interface AiChatProps extends ComponentProps<\"div\"> {\n /**\n * The ID of the chat the composer belongs to.\n */\n chatId: string;\n\n /**\n * Whether to focus the chat composer on mount.\n */\n autoFocus?: boolean;\n\n /**\n * The ID of the copilot to use to send the message.\n */\n copilotId?: string;\n\n /**\n * The contextual knowledge to include in the chat. May be used by the\n * assistant when generating responses. In addition to the knowledge passed\n * in via this prop, the AiChat instance will also have access to any\n * globally registered knowledge via <RegisterAiKnowledge />.\n */\n knowledge?: AiKnowledgeSource[];\n\n /**\n * Tool definitions to make available within this chat. May be used by the assistant when generating responses.\n */\n tools?: Record<string, AiOpaqueToolDefinition>;\n\n /**\n * The event handler called when the composer is submitted.\n */\n onComposerSubmit?: AiComposerProps[\"onComposerSubmit\"];\n\n /**\n * The layout of the chat and its composer.\n */\n layout?: \"inset\" | \"compact\";\n\n /**\n * The time, in milliseconds, before an AI response will timeout.\n */\n responseTimeout?: number;\n\n /**\n * Override the component's strings.\n */\n overrides?: Partial<\n GlobalOverrides &\n AiComposerOverrides &\n AiChatMessageOverrides &\n AiChatOverrides\n >;\n\n /**\n * Override the component's components.\n */\n components?: Partial<GlobalComponents & AiChatComponents>;\n}\n\ninterface AiChatMessagesProps extends ComponentProps<\"div\"> {\n messages: NonNullable<ReturnType<typeof useAiChatMessages>[\"messages\"]>;\n overrides: AiChatProps[\"overrides\"];\n components: AiChatProps[\"components\"];\n lastSentMessageId: MessageId | null;\n scrollToBottom: MutableRefObject<\n (behavior: \"instant\" | \"smooth\", includeTrailingSpace?: boolean) => void\n >;\n onScrollAtBottomChange: MutableRefObject<\n (isScrollAtBottom: boolean | null) => void\n >;\n containerRef: MutableRefObject<HTMLDivElement | null>;\n footerRef: MutableRefObject<HTMLDivElement | null>;\n messagesRef: MutableRefObject<HTMLDivElement | null>;\n bottomTrailingMarkerRef: MutableRefObject<HTMLDivElement | null>;\n trailingSpacerRef: MutableRefObject<HTMLDivElement | null>;\n}\n\nconst defaultComponents: AiChatComponents = {\n Empty: () => null,\n Loading: () => (\n <div className=\"lb-loading lb-ai-chat-loading\">\n <SpinnerIcon />\n </div>\n ),\n};\n\nconst AiChatMessages = forwardRef<HTMLDivElement, AiChatMessagesProps>(\n (\n {\n messages,\n overrides,\n components,\n lastSentMessageId,\n scrollToBottom,\n onScrollAtBottomChange,\n containerRef,\n footerRef,\n messagesRef,\n bottomTrailingMarkerRef,\n trailingSpacerRef,\n className,\n ...props\n },\n forwardedRef\n ) => {\n const hasLastSentMessage = lastSentMessageId !== null;\n\n /**\n * Every time the container, footer, or messages list change size,\n * we calculate the trailing space that would allow the penultimate\n * message to be at the top of the viewport, and apply it.\n *\n * ┌─────────────────────────────────────────┐▲ A = The `scroll-margin-top`\n * │ ┌─────────────────────────┐ │▼▲ value of the penultimate message\n * │ │ The penultimate message │ │ │\n * │ └─────────────────────────┘ │ │ B = The height from the top of\n * │ │ │ the penultimate message to the\n * │ ┌─────────────────────────┐ │ │ bottom of the messages list,\n * │ │ The last message │ │ │ including the messages' heights,\n * │ └─────────────────────────┘ │ │ and any padding, gap, etc\n * │ │ │\n * ├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤▲▼\n * │ ││ The trailing space needed to\n * │ = container height - (A + B + C) ││ allow the penultimate message\n * │ ││ to be at the top of the viewport\n * ├ ┬─────────────────────────────────────┬ ┤▼▲\n * │ │ │ │ │\n * │ │ │ │ │ C = The footer's height,\n * │ │ │ │ │ including any padding\n * │ └─────────────────────────────────────┘ │ │\n * └─────────────────────────────────────────┘ ▼\n */\n useEffect(\n () => {\n if (!hasLastSentMessage) {\n return;\n }\n\n const container = containerRef.current;\n const footer = footerRef.current;\n const messages = messagesRef.current;\n\n if (!container || !footer || !messages) {\n return;\n }\n\n const trailingSpacer = trailingSpacerRef.current;\n const bottomTrailingMarker = bottomTrailingMarkerRef.current;\n\n let containerHeight: number | undefined = undefined;\n let footerHeight: number | undefined = undefined;\n let messagesHeight: number | undefined = undefined;\n\n const resetTrailingSpace = () => {\n trailingSpacer?.style.removeProperty(\"height\");\n bottomTrailingMarker?.style.removeProperty(\"top\");\n };\n\n const updateTrailingSpace = (\n updatedContainerHeight?: number,\n updatedFooterHeight?: number,\n updatedMessagesHeight?: number\n ) => {\n if (!trailingSpacer || !bottomTrailingMarker) {\n return;\n }\n\n const lastMessage = messages.lastElementChild;\n const penultimateMessage = lastMessage?.previousElementSibling;\n\n if (updatedContainerHeight === undefined) {\n updatedContainerHeight = container.getBoundingClientRect().height;\n }\n\n if (updatedFooterHeight === undefined) {\n updatedFooterHeight = footer.getBoundingClientRect().height;\n }\n\n if (updatedMessagesHeight === undefined) {\n updatedMessagesHeight = messages.getBoundingClientRect().height;\n }\n\n // If the heights haven't changed, there's no need to update the trailing space.\n if (\n updatedContainerHeight === containerHeight &&\n updatedFooterHeight === footerHeight &&\n updatedMessagesHeight === messagesHeight\n ) {\n if (\n !lastMessage ||\n !penultimateMessage ||\n container.scrollHeight === container.clientHeight\n ) {\n resetTrailingSpace();\n }\n return;\n }\n\n // Now that we have compared them, we can update the heights.\n containerHeight = updatedContainerHeight;\n footerHeight = updatedFooterHeight;\n messagesHeight = updatedMessagesHeight;\n\n // If there's no last pair of messages, there's no need for any trailing space.\n if (!lastMessage || !penultimateMessage) {\n resetTrailingSpace();\n return;\n }\n\n // If the container isn't scrollable, there's no need for trailing space.\n if (container.scrollHeight === container.clientHeight) {\n resetTrailingSpace();\n return;\n }\n\n // A\n const penultimateMessageScrollMarginTop = Number.parseFloat(\n getComputedStyle(penultimateMessage as HTMLElement).scrollMarginTop\n );\n\n // B\n const messagesRect = messages.getBoundingClientRect();\n const penultimateMessageRect =\n penultimateMessage.getBoundingClientRect();\n const heightFromPenultimateMessageTopToMessagesListBottom =\n messagesRect.bottom - penultimateMessageRect.top;\n\n // A + B + C\n const differenceHeight =\n penultimateMessageScrollMarginTop +\n heightFromPenultimateMessageTopToMessagesListBottom +\n (footerHeight ?? 0);\n\n // = container height - (A + B + C)\n const trailingSpace = Math.max(containerHeight - differenceHeight, 0);\n\n // Update the trailing space.\n trailingSpacer.style.height = `${trailingSpace}px`;\n\n // Offset what \"the bottom\" is to the \"scroll at the bottom\" detection logic,\n // so that it doesn't include the trailing space.\n bottomTrailingMarker.style.top = `${-trailingSpace}px`;\n };\n\n const resizeObserver = new ResizeObserver((entries) => {\n let updatedContainerHeight: number | undefined = containerHeight;\n let updatedFooterHeight: number | undefined = footerHeight;\n let updatedMessagesHeight: number | undefined = messagesHeight;\n\n for (const entry of entries) {\n const entryHeight =\n entry.borderBoxSize?.[0]?.blockSize ?? entry.contentRect.height;\n\n if (entry.target === container) {\n updatedContainerHeight = entryHeight;\n } else if (entry.target === footer) {\n updatedFooterHeight = entryHeight;\n } else if (entry.target === messages) {\n updatedMessagesHeight = entryHeight;\n }\n }\n\n updateTrailingSpace(\n updatedContainerHeight,\n updatedFooterHeight,\n updatedMessagesHeight\n );\n });\n\n resizeObserver.observe(container);\n resizeObserver.observe(footer);\n resizeObserver.observe(messages);\n\n // Initialize before the first resize.\n requestAnimationFrame(() => updateTrailingSpace());\n\n return () => {\n resizeObserver.disconnect();\n resetTrailingSpace();\n };\n },\n // This effect only uses stable refs.\n [hasLastSentMessage] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n /**\n * Update the \"scroll at bottom\" state when needed.\n */\n useIntersectionCallback(\n bottomTrailingMarkerRef,\n (isIntersecting) => {\n onScrollAtBottomChange.current(isIntersecting);\n },\n { root: containerRef, rootMargin: MIN_DISTANCE_BOTTOM_SCROLL_INDICATOR }\n );\n\n /**\n * Instantly scroll to the bottom for the initial state.\n */\n useEffect(\n () => {\n scrollToBottom.current(\"instant\");\n },\n // `scrollToBottom` is a stable ref containing the callback.\n [] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n /**\n * Scroll to new messages when sending them.\n */\n useEffect(\n () => {\n if (lastSentMessageId) {\n scrollToBottom.current(\"smooth\", true);\n }\n },\n // `scrollToBottom` is a stable ref containing the callback.\n [lastSentMessageId] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n /**\n * Reset the \"scroll at bottom\" state when the component unmounts.\n */\n useEffect(\n () => {\n const onScrollAtBottomChangeCallback = onScrollAtBottomChange.current;\n\n return () => {\n onScrollAtBottomChangeCallback(null);\n };\n },\n // `onScrollAtBottomChange` is a stable ref containing the callback.\n [] // eslint-disable-line react-hooks/exhaustive-deps\n );\n\n return (\n <div\n className={cn(\"lb-ai-chat-messages\", className)}\n ref={forwardedRef}\n {...props}\n >\n {messages.map((message) => {\n if (message.role === \"user\") {\n return (\n <AiChatUserMessage\n key={message.id}\n message={message}\n overrides={overrides}\n components={components}\n />\n );\n } else if (message.role === \"assistant\") {\n return (\n <AiChatAssistantMessage\n key={message.id}\n message={message}\n overrides={overrides}\n components={components}\n />\n );\n } else {\n return null;\n }\n })}\n </div>\n );\n }\n);\n\nexport const AiChat = forwardRef<HTMLDivElement, AiChatProps>(\n (\n {\n chatId,\n copilotId,\n autoFocus,\n overrides,\n knowledge: localKnowledge,\n tools = {},\n onComposerSubmit,\n layout = \"inset\",\n components,\n className,\n responseTimeout,\n ...props\n },\n forwardedRef\n ) => {\n const { messages, isLoading, error } = useAiChatMessages(chatId);\n const [lastSentMessageId, setLastSentMessageId] =\n useState<MessageId | null>(null);\n\n const $ = useOverrides(overrides);\n const Empty = components?.Empty ?? defaultComponents.Empty;\n const Loading = components?.Loading ?? defaultComponents.Loading;\n\n const containerRef = useRef<HTMLDivElement | null>(null);\n const messagesRef = useRef<HTMLDivElement | null>(null);\n const footerRef = useRef<HTMLDivElement | null>(null);\n const bottomMarkerRef = useRef<HTMLDivElement | null>(null);\n const bottomTrailingMarkerRef = useRef<HTMLDivElement | null>(null);\n const trailingSpacerRef = useRef<HTMLDivElement | null>(null);\n\n const [isScrollAtBottom, setScrollAtBottom] = useState<boolean | null>(\n null\n );\n // `useState`'s setter is stable but this is for clarity in the places it's used.\n const onScrollAtBottomChange = useLatest(setScrollAtBottom);\n const isScrollIndicatorVisible =\n messages && isScrollAtBottom !== null ? !isScrollAtBottom : false;\n\n useImperativeHandle<HTMLDivElement | null, HTMLDivElement | null>(\n forwardedRef,\n () => containerRef.current,\n []\n );\n\n const scrollToBottom = useLatest(\n (behavior: \"instant\" | \"smooth\", includeTrailingSpace = false) => {\n if (includeTrailingSpace) {\n // Scroll to the bottom marker to include the trailing space,\n // but wait for the next tick in case the trailing space hasn't\n // been updated yet. (e.g. when sending a new message)\n setTimeout(() => {\n bottomMarkerRef.current?.scrollIntoView({\n behavior,\n block: \"end\",\n });\n }, 0);\n } else {\n // Scroll to the trailing space marker to only scroll to the\n // bottom of the messages, without including the trailing space.\n bottomTrailingMarkerRef.current?.scrollIntoView({\n behavior,\n block: \"end\",\n });\n }\n }\n );\n\n return (\n <div\n ref={containerRef}\n {...props}\n className={cn(\n \"lb-root lb-ai-chat\",\n `lb-ai-chat:layout-${layout}`,\n className\n )}\n >\n {Object.entries(tools).map(([name, tool]) => (\n <RegisterAiTool key={name} chatId={chatId} name={name} tool={tool} />\n ))}\n\n {localKnowledge\n ? localKnowledge.map((knowledge, index) => (\n <RegisterAiKnowledge\n key={`${index}:${knowledge.description}`}\n chatId={chatId}\n {...knowledge}\n />\n ))\n : null}\n\n <div className=\"lb-ai-chat-content\">\n {isLoading ? (\n <Loading />\n ) : error !== undefined ? (\n <div className=\"lb-error lb-ai-chat-error\">\n {$.AI_CHAT_MESSAGES_ERROR(error)}\n </div>\n ) : messages.length === 0 ? (\n <Empty chatId={chatId} copilotId={copilotId} />\n ) : (\n <>\n <AiChatMessages\n ref={messagesRef}\n messages={messages}\n overrides={overrides}\n components={components}\n lastSentMessageId={lastSentMessageId}\n scrollToBottom={scrollToBottom}\n onScrollAtBottomChange={onScrollAtBottomChange}\n containerRef={containerRef}\n footerRef={footerRef}\n messagesRef={messagesRef}\n bottomTrailingMarkerRef={bottomTrailingMarkerRef}\n trailingSpacerRef={trailingSpacerRef}\n />\n\n {/**\n * This trailing spacer is used to extend the scrollable area beyond its actual\n * content, to allow messages to appear at the top of the viewport for example.\n */}\n <div\n ref={trailingSpacerRef}\n data-trailing-spacer=\"\"\n style={{\n pointerEvents: \"none\",\n height: 0,\n }}\n aria-hidden\n />\n </>\n )}\n </div>\n\n <div className=\"lb-ai-chat-footer\" ref={footerRef}>\n <div className=\"lb-ai-chat-footer-actions\">\n <div\n className=\"lb-root lb-elevation lb-elevation-moderate lb-ai-chat-scroll-indicator\"\n data-visible={isScrollIndicatorVisible ? \"\" : undefined}\n >\n <button\n className=\"lb-ai-chat-scroll-indicator-button\"\n tabIndex={isScrollIndicatorVisible ? 0 : -1}\n aria-hidden={!isScrollIndicatorVisible}\n onClick={() => scrollToBottom.current(\"smooth\")}\n >\n <span className=\"lb-icon-container\">\n <ArrowDownIcon />\n </span>\n </button>\n </div>\n </div>\n <AiComposer\n key={chatId}\n chatId={chatId}\n copilotId={copilotId as CopilotId}\n overrides={overrides}\n autoFocus={autoFocus}\n responseTimeout={responseTimeout}\n onComposerSubmit={onComposerSubmit}\n onComposerSubmitted={({ id }) => setLastSentMessageId(id)}\n className={cn(\n \"lb-ai-chat-composer\",\n layout === \"inset\"\n ? \"lb-elevation lb-elevation-moderate\"\n : undefined\n )}\n />\n </div>\n\n {/**\n * This invisible marker is a trick which allows us to use IntersectionObserver to detect when the\n * scrollable area is fully scrolled to the bottom instead of manually tracking the scroll position\n * and having to deal with resizes, etc.\n *\n * It's positioned at the bottom of the scrollable area and reliably only becomes \"visible\" to the\n * IntersectionObserver when the scrollable area is scrolled to the bottom.\n */}\n {messages && messages.length > 0 ? (\n <div\n ref={bottomMarkerRef}\n style={{ position: \"sticky\", height: 0 }}\n aria-hidden\n data-bottom-marker=\"\"\n >\n {/**\n * This inner marker is absolutely offset by the same distance as the trailing space so its\n * visibility means the scrollable area is at the bottom of the messages, not the full bottom.\n */}\n <div\n ref={bottomTrailingMarkerRef}\n style={{\n position: \"absolute\",\n height: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,QAAA,EAAU,QAAQ,CAAE,EAAA;AAAA,UACvC,aAAW,EAAA,IAAA;AAAA,UACX,oBAAmB,EAAA,EAAA;AAAA,UAMnB,QAAC,kBAAA,GAAA,CAAA,KAAA,EAAA;AAAA,YACC,GAAK,EAAA,uBAAA;AAAA,YACL,KAAO,EAAA;AAAA,cACL,QAAU,EAAA,UAAA;AAAA,cACV,MAAQ,EAAA,CAAA;AAAA,aACV;AAAA,YACA,6BAA4B,EAAA,EAAA;AAAA,WAC9B,CAAA;AAAA,SACF,CACE,GAAA,IAAA;AAAA,OAAA;AAAA,KACN,CAAA,CAAA;AAAA,GAEJ;AACF;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AiTool.cjs","sources":["../../src/components/AiTool.tsx"],"sourcesContent":["import type {\n AiToolExecuteCallback,\n AiToolTypePack,\n JsonObject,\n NoInfr,\n} from \"@liveblocks/core\";\nimport { kInternal } from \"@liveblocks/core\";\nimport type { ComponentProps, ReactNode } from \"react\";\nimport { Children, forwardRef, useCallback, useMemo } from \"react\";\n\nimport { Button } from \"../_private\";\nimport {\n CheckCircleFillIcon,\n ChevronRightIcon,\n CrossCircleFillIcon,\n MinusCircleIcon,\n SpinnerIcon,\n} from \"../icons\";\nimport {\n type AiToolConfirmationOverrides,\n type GlobalOverrides,\n useOverrides,\n} from \"../overrides\";\nimport { useAiToolInvocationContext } from \"../primitives/AiMessage/contexts\";\nimport * as Collapsible from \"../primitives/Collapsible\";\nimport { cn } from \"../utils/cn\";\nimport { useSemiControllableState } from \"../utils/use-controllable-state\";\nimport { CodeBlock } from \"./internal/CodeBlock\";\n\nexport interface AiToolProps\n extends Omit<ComponentProps<\"div\">, \"title\" | \"children\"> {\n /**\n * The tool's title.\n *\n * By default, a human-readable version of the tool's name is used:\n * - `\"showTodo\"` → \"Show todo\"\n * - `\"get_weather\"` → \"Get weather\"\n */\n title?: string;\n\n /**\n * An optional icon displayed next to the title.\n */\n icon?: ReactNode;\n\n /**\n * The content shown in the tool.\n */\n children?: ReactNode;\n\n /**\n * The visual appearance of the tool.\n */\n variant?: \"block\" | \"minimal\";\n\n /**\n * Whether the content is currently collapsed.\n * It is not a traditional controlled value, as in if you set it to `true` it would only stay expanded.\n * Instead, it is \"semi-controlled\", meaning that setting it to `true` will expand it, but it\n * can still be collapsed/expanded by clicking on it.\n */\n collapsed?: boolean;\n\n /**\n * The event handler called when the content is collapsed or expanded by clicking on it.\n */\n onCollapsedChange?: (collapsed: boolean) => void;\n\n /**\n * Whether the content can be collapsed/expanded.\n * If set to `false`, clicking on it will have no effect.\n * If there's no content, this prop has no effect.\n */\n collapsible?: boolean;\n}\n\nexport type AiToolIconProps = ComponentProps<\"div\">;\n\nexport type AiToolInspectorProps = ComponentProps<\"div\">;\n\nexport interface AiToolConfirmationProps<\n A extends JsonObject,\n R extends JsonObject,\n> extends ComponentProps<\"div\"> {\n /**\n * The callback invoked when the user clicks the confirm button.\n */\n confirm: AiToolExecuteCallback<A, R>;\n\n /**\n * The callback invoked when the user clicks the cancel button.\n */\n cancel?: AiToolExecuteCallback<A, R>;\n\n /**\n * The visual appearance.\n */\n variant?: \"default\" | \"destructive\";\n\n /**\n * Override the component's strings.\n */\n overrides?: Partial<GlobalOverrides & AiToolConfirmationOverrides>;\n\n /**\n * The tool's result type, to be used with the `types` prop in the `render` method.\n *\n * @example\n * defineAiTool<{ value: number }>()({\n * // ...\n * render: ({ types }) => (\n * <AiTool.Confirmation\n * types={types}\n * confirm={() => {\n * return {\n * // Using `types` makes the result type-safe\n * // based on the tool's definition\n * data: { value: 123 },\n * };\n * }}\n * />\n * ),\n * })\n */\n types?: NoInfr<AiToolTypePack<A, R>>;\n}\n\nfunction AiToolIcon({ className, ...props }: AiToolIconProps) {\n return <div className={cn(\"lb-ai-tool-icon\", className)} {...props} />;\n}\n\nfunction AiToolInspector({ className, ...props }: AiToolInspectorProps) {\n const { args, partialArgs, result } = useAiToolInvocationContext();\n\n return (\n <div className={cn(\"lb-ai-tool-inspector\", className)} {...props}>\n <CodeBlock\n title=\"Arguments\"\n code={JSON.stringify(args ?? partialArgs, null, 2)}\n />\n {result !== undefined ? (\n <CodeBlock title=\"Result\" code={JSON.stringify(result, null, 2)} />\n ) : null}\n </div>\n );\n}\n\nfunction AiToolConfirmation<\n TPack extends AiToolTypePack,\n A extends JsonObject = TPack[\"A\"],\n R extends JsonObject = TPack[\"R\"],\n>({\n children,\n variant = \"default\",\n confirm,\n cancel,\n overrides,\n className,\n ...props\n}: AiToolConfirmationProps<A, R>) {\n const { stage, args, respond, name, invocationId } =\n useAiToolInvocationContext();\n const $ = useOverrides(overrides);\n\n const enabled = stage === \"executing\";\n\n const context = useMemo(() => ({ name, invocationId }), [name, invocationId]);\n\n const onConfirmClick = useCallback(async () => {\n if (enabled) {\n const result = await confirm(args as A, context);\n respond(result ?? undefined);\n }\n }, [enabled, args, confirm, respond, context]);\n\n const onCancelClick = useCallback(async () => {\n if (enabled) {\n if (cancel === undefined) {\n respond({ cancel: true });\n } else {\n const result = await cancel(args as A, context);\n respond(result ?? undefined);\n }\n }\n }, [enabled, args, cancel, respond, context]);\n\n // If there's no content and the tool has been executed (so there's no\n // confirmation UI displayed either), don't render anything.\n if (stage === \"executed\" && !children) {\n return null;\n }\n\n return (\n <div className={cn(\"lb-ai-tool-confirmation\", className)} {...props}>\n {children ? (\n <div className=\"lb-ai-tool-confirmation-content\">{children}</div>\n ) : null}\n {stage !== \"executed\" && (\n <div className=\"lb-ai-tool-confirmation-footer\">\n <div className=\"lb-ai-tool-confirmation-actions\">\n <Button\n disabled={!enabled}\n onClick={onCancelClick}\n variant=\"secondary\"\n >\n {$.AI_TOOL_CONFIRMATION_CANCEL}\n </Button>\n <Button\n disabled={!enabled}\n onClick={onConfirmClick}\n variant={variant === \"destructive\" ? \"destructive\" : \"primary\"}\n >\n {$.AI_TOOL_CONFIRMATION_CONFIRM}\n </Button>\n </div>\n </div>\n )}\n </div>\n );\n}\n\nfunction prettifyString(string: string) {\n return (\n string\n // Convert camelCase to spaces\n .replace(/([a-z])([A-Z])/g, \"$1 $2\")\n // Convert snake_case and kebab-case to spaces\n .replace(/[_-]+/g, \" \")\n // Collapse multiple following spaces\n .replace(/\\s+/g, \" \")\n // Trim leading and trailing spaces\n .trim()\n // Capitalize first word\n .toLowerCase()\n .replace(/^\\w/, (character) => character.toUpperCase())\n );\n}\n\n/**\n * A pre-built component which displays a tool call.\n *\n * By default, a human-readable version of the tool's name is used as a title:\n * - `\"showTodo\"` → \"Show todo\"\n * - `\"get_weather\"` → \"Get weather\"\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: () => (\n * <AiTool />\n * ),\n * })\n *\n * It can be customized in various ways:\n * - adding an icon\n * - customizing the title (even dynamically)\n * - adding custom content inside it\n * - collapsing it conditionally\n * - etc.\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: ({ stage, result }) => (\n * <AiTool\n * icon=\"🔍\"\n *\n * // Override the default title based on the tool's stage\n * title={stage === \"executing\" ? \"Searching…\" : \"Search results\"}\n *\n * // Start open and automatically collapse after it is executed\n * // The user can still expand/collapse it manually at any time\n * collapsed={stage === \"executed\"}\n * >\n * <SearchResults data={result.data} />\n * </AiTool>\n * ),\n * })\n *\n * It also comes with a few built-in sub-components:\n * - `AiTool.Confirmation` to display a human-in-the-loop confirmation step\n * which can be accepted or cancelled by the user.\n * - `AiTool.Inspector` to display the tool's arguments and result which can\n * be useful during development.\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: () => (\n * <AiTool>\n * <AiTool.Confirmation\n * // Use a destructive visual appearance\n * variant=\"destructive\"\n *\n * // The tool's arguments can be directly accessed like in `execute`\n * confirm={({ pageIds }) => {\n * const deletedPageTitles = pages\n * .filter((p) => pageIds.includes(p.id))\n * .map((page) => page.title);\n *\n * deletePages(pageIds);\n *\n * // This result will be available as `result` in the tool's `render` props\n * return { data: { deletedPageTitles } };\n * }}\n *\n * // If needed, `cancel={() => ...}` would work similarly\n * >\n * Do you want to delete these pages?\n * <PagesPreviews />\n * </AiTool.Confirmation>\n * </AiTool>\n * ),\n * })\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: () => (\n * <AiTool>\n * <AiTool.Inspector />\n * </AiTool>\n * ),\n * })\n */\nexport const AiTool = Object.assign(\n forwardRef<HTMLDivElement, AiToolProps>(\n (\n {\n children,\n title,\n icon,\n collapsible,\n collapsed,\n onCollapsedChange,\n variant = \"block\",\n className,\n ...props\n },\n forwardedRef\n ) => {\n const {\n stage,\n result,\n name,\n [kInternal]: { execute, messageStatus },\n } = useAiToolInvocationContext();\n // Only mark the tool as pending visually (e.g. show a spinner, add a shimmer animation, etc.)\n // if it has an `execute` method and it isn't in the \"executed\" stage.\n const isVisuallyPending =\n execute !== undefined &&\n stage !== \"executed\" &&\n // If it's in the \"receiving\" stage, we also check that the outer message is still generating.\n (stage === \"receiving\" ? messageStatus === \"generating\" : true);\n const [semiControlledCollapsed, onSemiControlledCollapsed] =\n useSemiControllableState(collapsed ?? false, onCollapsedChange);\n // TODO: This check won't work for cases like:\n // <AiTool>\n // <ComponentThatRendersNull />\n // <ComponentThatAlsoRendersNull />\n // </AiTool>\n // One solution could be to check the DOM on every render with `useLayoutEffect`\n // to see if there's any actual content.\n // For now we're limiting the visual issues caused by the above by using CSS's\n // `:empty` pseudo-class to make the content 0px high if it's actually empty.\n const hasContent = Children.count(children) > 0;\n // If there's no content, the tool is never collapsible.\n const isCollapsible = hasContent ? (collapsible ?? true) : false;\n const resolvedTitle = useMemo(() => {\n return title ?? prettifyString(name);\n }, [title, name]);\n\n // `AiTool` uses \"collapsed\" instead of \"open\" (like the `Composer` component) because \"open\"\n // makes sense next to something called \"Collapsible\" but less so for something called \"AiTool\".\n const handleCollapsibleOpenChange = useCallback(\n (open: boolean) => {\n onSemiControlledCollapsed(!open);\n },\n [onSemiControlledCollapsed]\n );\n\n return (\n <Collapsible.Root\n ref={forwardedRef}\n className={cn(\n \"lb-collapsible lb-ai-tool\",\n `lb-ai-tool:variant-${variant}`,\n className\n )}\n {...props}\n // Regardless of `semiControlledCollapsed`, the collapsible is closed if there's no content.\n open={hasContent ? !semiControlledCollapsed : false}\n onOpenChange={handleCollapsibleOpenChange}\n disabled={!isCollapsible}\n data-result={result?.type}\n data-stage={stage}\n >\n <Collapsible.Trigger\n className={cn(\n \"lb-collapsible-trigger lb-ai-tool-header\",\n variant === \"minimal\" && isVisuallyPending && \"lb-ai-chat-pending\"\n )}\n >\n {icon ? (\n <div className=\"lb-ai-tool-header-icon-container\">{icon}</div>\n ) : null}\n <span className=\"lb-ai-tool-header-title\">{resolvedTitle}</span>\n {isCollapsible ? (\n <span className=\"lb-collapsible-chevron lb-icon-container\">\n <ChevronRightIcon />\n </span>\n ) : null}\n {variant !== \"minimal\" ? (\n <div className=\"lb-ai-tool-header-status\">\n {stage === \"executed\" ? (\n result.type === \"success\" ? (\n <CheckCircleFillIcon />\n ) : result.type === \"error\" ? (\n <CrossCircleFillIcon />\n ) : result.type === \"cancelled\" ? (\n <MinusCircleIcon />\n ) : null\n ) : isVisuallyPending ? (\n <SpinnerIcon />\n ) : null}\n </div>\n ) : null}\n </Collapsible.Trigger>\n\n {hasContent ? (\n <Collapsible.Content className=\"lb-collapsible-content lb-ai-tool-content-container\">\n <div className=\"lb-ai-tool-content\">{children}</div>\n </Collapsible.Content>\n ) : null}\n </Collapsible.Root>\n );\n }\n ),\n {\n /**\n * Display an icon in a container.\n *\n * @example\n * <AiTool\n * icon={\n * <AiTool.Icon>🔍</AiTool.Icon>\n * }\n * />\n */\n Icon: AiToolIcon,\n\n /**\n * Display the tool's arguments and result, which can be useful during\n * development.\n *\n * @example\n * <AiTool>\n * <AiTool.Inspector />\n * </AiTool>\n */\n Inspector: AiToolInspector,\n\n /**\n * Display a human-in-the-loop confirmation step which can be accepted\n * or cancelled by the user.\n *\n * The `confirm` and `cancel` callbacks work like `execute` in tool definitions: they can\n * perform side-effects, be async if needed, and return a result. The tool call will stay\n * pending until either `confirm` or `cancel` is called.\n *\n * @example\n * <AiTool>\n * <AiTool.Confirmation\n * // Use a destructive visual appearance\n * variant=\"destructive\"\n *\n * // The tool's arguments can be directly accessed like in `execute`\n * confirm={({ pageIds }) => {\n * const deletedPageTitles = pages\n * .filter((p) => pageIds.includes(p.id))\n * .map((page) => page.title);\n *\n * deletePages(pageIds);\n *\n * // This result will be available as `result` in the tool's `render` props\n * return { data: { deletedPageTitles } };\n * }}\n *\n * // If needed, `cancel={() => ...}` would work similarly\n * >\n * Do you want to delete these pages?\n * <PagesPreviews />\n * </AiTool.Confirmation>\n * </AiTool>\n */\n Confirmation: AiToolConfirmation,\n }\n);\n"],"names":["jsx","cn","useAiToolInvocationContext","jsxs","CodeBlock","overrides","useOverrides","useMemo","useCallback","Button","forwardRef","kInternal","useSemiControllableState","Children","Collapsible.Root","Collapsible.Trigger","ChevronRightIcon","CheckCircleFillIcon","CrossCircleFillIcon","MinusCircleIcon","SpinnerIcon","Collapsible.Content"],"mappings":";;;;;;;;;;;;;;;;;;;;AA+HA,SAAS,UAAW,CAAA,EAAE,SAAc,EAAA,GAAA,KAAA,EAA0B,EAAA;AAC5D,EAAA,uBAAQA,cAAA,CAAA,KAAA,EAAA;AAAA,IAAI,SAAA,EAAWC,KAAG,CAAA,iBAAA,EAAmB,SAAS,CAAA;AAAA,IAAI,GAAG,KAAA;AAAA,GAAO,CAAA,CAAA;AACtE,CAAA;AAEA,SAAS,eAAgB,CAAA,EAAE,SAAc,EAAA,GAAA,KAAA,EAA+B,EAAA;AACtE,EAAA,MAAM,EAAE,IAAA,EAAM,WAAa,EAAA,MAAA,KAAWC,mCAA2B,EAAA,CAAA;AAEjE,EAAA,uBACGC,eAAA,CAAA,KAAA,EAAA;AAAA,IAAI,SAAA,EAAWF,KAAG,CAAA,sBAAA,EAAwB,SAAS,CAAA;AAAA,IAAI,GAAG,KAAA;AAAA,IACzD,QAAA,EAAA;AAAA,sBAACD,cAAA,CAAAI,mBAAA,EAAA;AAAA,QACC,KAAM,EAAA,WAAA;AAAA,QACN,M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Tool.cjs","sources":["../../src/components/AiTool.tsx"],"sourcesContent":["import type {\n AiToolExecuteCallback,\n AiToolTypePack,\n JsonObject,\n NoInfr,\n} from \"@liveblocks/core\";\nimport { kInternal } from \"@liveblocks/core\";\nimport type { ComponentProps, ReactNode } from \"react\";\nimport { Children, forwardRef, useCallback, useMemo } from \"react\";\n\nimport { Button } from \"../_private\";\nimport {\n CheckCircleFillIcon,\n ChevronRightIcon,\n CrossCircleFillIcon,\n MinusCircleIcon,\n SpinnerIcon,\n} from \"../icons\";\nimport {\n type AiToolConfirmationOverrides,\n type GlobalOverrides,\n useOverrides,\n} from \"../overrides\";\nimport { useAiToolInvocationContext } from \"../primitives/AiMessage/contexts\";\nimport * as Collapsible from \"../primitives/Collapsible\";\nimport { cn } from \"../utils/cn\";\nimport { useSemiControllableState } from \"../utils/use-controllable-state\";\nimport { CodeBlock } from \"./internal/CodeBlock\";\n\nexport interface AiToolProps\n extends Omit<ComponentProps<\"div\">, \"title\" | \"children\"> {\n /**\n * The tool's title.\n *\n * By default, a human-readable version of the tool's name is used:\n * - `\"showTodo\"` → \"Show todo\"\n * - `\"get_weather\"` → \"Get weather\"\n */\n title?: ReactNode;\n\n /**\n * An optional icon displayed next to the title.\n */\n icon?: ReactNode;\n\n /**\n * The content shown in the tool.\n */\n children?: ReactNode;\n\n /**\n * The visual appearance of the tool.\n */\n variant?: \"block\" | \"minimal\";\n\n /**\n * Whether the content is currently collapsed.\n * It is not a traditional controlled value, as in if you set it to `true` it would only stay expanded.\n * Instead, it is \"semi-controlled\", meaning that setting it to `true` will expand it, but it\n * can still be collapsed/expanded by clicking on it.\n */\n collapsed?: boolean;\n\n /**\n * The event handler called when the content is collapsed or expanded by clicking on it.\n */\n onCollapsedChange?: (collapsed: boolean) => void;\n\n /**\n * Whether the content can be collapsed/expanded.\n * If set to `false`, clicking on it will have no effect.\n * If there's no content, this prop has no effect.\n */\n collapsible?: boolean;\n}\n\nexport type AiToolIconProps = ComponentProps<\"div\">;\n\nexport type AiToolInspectorProps = ComponentProps<\"div\">;\n\nexport interface AiToolConfirmationProps<\n A extends JsonObject,\n R extends JsonObject,\n> extends ComponentProps<\"div\"> {\n /**\n * The callback invoked when the user clicks the confirm button.\n */\n confirm: AiToolExecuteCallback<A, R>;\n\n /**\n * The callback invoked when the user clicks the cancel button.\n */\n cancel?: AiToolExecuteCallback<A, R>;\n\n /**\n * The visual appearance.\n */\n variant?: \"default\" | \"destructive\";\n\n /**\n * Override the component's strings.\n */\n overrides?: Partial<GlobalOverrides & AiToolConfirmationOverrides>;\n\n /**\n * The tool's result type, to be used with the `types` prop in the `render` method.\n *\n * @example\n * defineAiTool<{ value: number }>()({\n * // ...\n * render: ({ types }) => (\n * <AiTool.Confirmation\n * types={types}\n * confirm={() => {\n * return {\n * // Using `types` makes the result type-safe\n * // based on the tool's definition\n * data: { value: 123 },\n * };\n * }}\n * />\n * ),\n * })\n */\n types?: NoInfr<AiToolTypePack<A, R>>;\n}\n\nfunction AiToolIcon({ className, ...props }: AiToolIconProps) {\n return <div className={cn(\"lb-ai-tool-icon\", className)} {...props} />;\n}\n\nfunction AiToolInspector({ className, ...props }: AiToolInspectorProps) {\n const { args, partialArgs, result } = useAiToolInvocationContext();\n\n return (\n <div className={cn(\"lb-ai-tool-inspector\", className)} {...props}>\n <CodeBlock\n title=\"Arguments\"\n code={JSON.stringify(args ?? partialArgs, null, 2)}\n />\n {result !== undefined ? (\n <CodeBlock title=\"Result\" code={JSON.stringify(result, null, 2)} />\n ) : null}\n </div>\n );\n}\n\nfunction AiToolConfirmation<\n TPack extends AiToolTypePack,\n A extends JsonObject = TPack[\"A\"],\n R extends JsonObject = TPack[\"R\"],\n>({\n children,\n variant = \"default\",\n confirm,\n cancel,\n overrides,\n className,\n ...props\n}: AiToolConfirmationProps<A, R>) {\n const { stage, args, respond, name, invocationId } =\n useAiToolInvocationContext();\n const $ = useOverrides(overrides);\n\n const enabled = stage === \"executing\";\n\n const context = useMemo(() => ({ name, invocationId }), [name, invocationId]);\n\n const onConfirmClick = useCallback(async () => {\n if (enabled) {\n const result = await confirm(args as A, context);\n respond(result ?? undefined);\n }\n }, [enabled, args, confirm, respond, context]);\n\n const onCancelClick = useCallback(async () => {\n if (enabled) {\n if (cancel === undefined) {\n respond({ cancel: true });\n } else {\n const result = await cancel(args as A, context);\n respond(result ?? undefined);\n }\n }\n }, [enabled, args, cancel, respond, context]);\n\n // If there's no content and the tool has been executed (so there's no\n // confirmation UI displayed either), don't render anything.\n if (stage === \"executed\" && !children) {\n return null;\n }\n\n return (\n <div className={cn(\"lb-ai-tool-confirmation\", className)} {...props}>\n {children ? (\n <div className=\"lb-ai-tool-confirmation-content\">{children}</div>\n ) : null}\n {stage !== \"executed\" && (\n <div className=\"lb-ai-tool-confirmation-footer\">\n <div className=\"lb-ai-tool-confirmation-actions\">\n <Button\n disabled={!enabled}\n onClick={onCancelClick}\n variant=\"secondary\"\n >\n {$.AI_TOOL_CONFIRMATION_CANCEL}\n </Button>\n <Button\n disabled={!enabled}\n onClick={onConfirmClick}\n variant={variant === \"destructive\" ? \"destructive\" : \"primary\"}\n >\n {$.AI_TOOL_CONFIRMATION_CONFIRM}\n </Button>\n </div>\n </div>\n )}\n </div>\n );\n}\n\nfunction prettifyString(string: string) {\n return (\n string\n // Convert camelCase to spaces\n .replace(/([a-z])([A-Z])/g, \"$1 $2\")\n // Convert snake_case and kebab-case to spaces\n .replace(/[_-]+/g, \" \")\n // Collapse multiple following spaces\n .replace(/\\s+/g, \" \")\n // Trim leading and trailing spaces\n .trim()\n // Capitalize first word\n .toLowerCase()\n .replace(/^\\w/, (character) => character.toUpperCase())\n );\n}\n\n/**\n * A pre-built component which displays a tool call.\n *\n * By default, a human-readable version of the tool's name is used as a title:\n * - `\"showTodo\"` → \"Show todo\"\n * - `\"get_weather\"` → \"Get weather\"\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: () => (\n * <AiTool />\n * ),\n * })\n *\n * It can be customized in various ways:\n * - adding an icon\n * - customizing the title (even dynamically)\n * - adding custom content inside it\n * - collapsing it conditionally\n * - etc.\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: ({ stage, result }) => (\n * <AiTool\n * icon=\"🔍\"\n *\n * // Override the default title based on the tool's stage\n * title={stage === \"executing\" ? \"Searching…\" : \"Search results\"}\n *\n * // Start open and automatically collapse after it is executed\n * // The user can still expand/collapse it manually at any time\n * collapsed={stage === \"executed\"}\n * >\n * <SearchResults data={result.data} />\n * </AiTool>\n * ),\n * })\n *\n * It also comes with a few built-in sub-components:\n * - `AiTool.Confirmation` to display a human-in-the-loop confirmation step\n * which can be accepted or cancelled by the user.\n * - `AiTool.Inspector` to display the tool's arguments and result which can\n * be useful during development.\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: () => (\n * <AiTool>\n * <AiTool.Confirmation\n * // Use a destructive visual appearance\n * variant=\"destructive\"\n *\n * // The tool's arguments can be directly accessed like in `execute`\n * confirm={({ pageIds }) => {\n * const deletedPageTitles = pages\n * .filter((p) => pageIds.includes(p.id))\n * .map((page) => page.title);\n *\n * deletePages(pageIds);\n *\n * // This result will be available as `result` in the tool's `render` props\n * return { data: { deletedPageTitles } };\n * }}\n *\n * // If needed, `cancel={() => ...}` would work similarly\n * >\n * Do you want to delete these pages?\n * <PagesPreviews />\n * </AiTool.Confirmation>\n * </AiTool>\n * ),\n * })\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: () => (\n * <AiTool>\n * <AiTool.Inspector />\n * </AiTool>\n * ),\n * })\n */\nexport const AiTool = Object.assign(\n forwardRef<HTMLDivElement, AiToolProps>(\n (\n {\n children,\n title,\n icon,\n collapsible,\n collapsed,\n onCollapsedChange,\n variant = \"block\",\n className,\n ...props\n },\n forwardedRef\n ) => {\n const {\n stage,\n result,\n name,\n [kInternal]: { execute, messageStatus },\n } = useAiToolInvocationContext();\n // Only mark the tool as pending visually (e.g. show a spinner, add a shimmer animation, etc.)\n // if it has an `execute` method and it isn't in the \"executed\" stage.\n const isVisuallyPending =\n execute !== undefined &&\n stage !== \"executed\" &&\n // If it's in the \"receiving\" stage, we also check that the outer message is still generating.\n (stage === \"receiving\" ? messageStatus === \"generating\" : true);\n const [semiControlledCollapsed, onSemiControlledCollapsed] =\n useSemiControllableState(collapsed ?? false, onCollapsedChange);\n // TODO: This check won't work for cases like:\n // <AiTool>\n // <ComponentThatRendersNull />\n // <ComponentThatAlsoRendersNull />\n // </AiTool>\n // One solution could be to check the DOM on every render with `useLayoutEffect`\n // to see if there's any actual content.\n // For now we're limiting the visual issues caused by the above by using CSS's\n // `:empty` pseudo-class to make the content 0px high if it's actually empty.\n const hasContent = Children.count(children) > 0;\n // If there's no content, the tool is never collapsible.\n const isCollapsible = hasContent ? (collapsible ?? true) : false;\n const resolvedTitle = useMemo(() => {\n return title ?? prettifyString(name);\n }, [title, name]);\n\n // `AiTool` uses \"collapsed\" instead of \"open\" (like the `Composer` component) because \"open\"\n // makes sense next to something called \"Collapsible\" but less so for something called \"AiTool\".\n const handleCollapsibleOpenChange = useCallback(\n (open: boolean) => {\n onSemiControlledCollapsed(!open);\n },\n [onSemiControlledCollapsed]\n );\n\n return (\n <Collapsible.Root\n ref={forwardedRef}\n className={cn(\n \"lb-collapsible lb-ai-tool\",\n `lb-ai-tool:variant-${variant}`,\n className\n )}\n {...props}\n // Regardless of `semiControlledCollapsed`, the collapsible is closed if there's no content.\n open={hasContent ? !semiControlledCollapsed : false}\n onOpenChange={handleCollapsibleOpenChange}\n disabled={!isCollapsible}\n data-result={result?.type}\n data-stage={stage}\n >\n <Collapsible.Trigger\n className={cn(\n \"lb-collapsible-trigger lb-ai-tool-header\",\n variant === \"minimal\" && isVisuallyPending && \"lb-ai-chat-pending\"\n )}\n >\n {icon ? (\n <div className=\"lb-ai-tool-header-icon-container\">{icon}</div>\n ) : null}\n <span className=\"lb-ai-tool-header-title\">{resolvedTitle}</span>\n {isCollapsible ? (\n <span className=\"lb-collapsible-chevron lb-icon-container\">\n <ChevronRightIcon />\n </span>\n ) : null}\n {variant !== \"minimal\" ? (\n <div className=\"lb-ai-tool-header-status\">\n {stage === \"executed\" ? (\n result.type === \"success\" ? (\n <CheckCircleFillIcon />\n ) : result.type === \"error\" ? (\n <CrossCircleFillIcon />\n ) : result.type === \"cancelled\" ? (\n <MinusCircleIcon />\n ) : null\n ) : isVisuallyPending ? (\n <SpinnerIcon />\n ) : null}\n </div>\n ) : null}\n </Collapsible.Trigger>\n\n {hasContent ? (\n <Collapsible.Content className=\"lb-collapsible-content lb-ai-tool-content-container\">\n <div className=\"lb-ai-tool-content\">{children}</div>\n </Collapsible.Content>\n ) : null}\n </Collapsible.Root>\n );\n }\n ),\n {\n /**\n * Display an icon in a container.\n *\n * @example\n * <AiTool\n * icon={\n * <AiTool.Icon>🔍</AiTool.Icon>\n * }\n * />\n */\n Icon: AiToolIcon,\n\n /**\n * Display the tool's arguments and result, which can be useful during\n * development.\n *\n * @example\n * <AiTool>\n * <AiTool.Inspector />\n * </AiTool>\n */\n Inspector: AiToolInspector,\n\n /**\n * Display a human-in-the-loop confirmation step which can be accepted\n * or cancelled by the user.\n *\n * The `confirm` and `cancel` callbacks work like `execute` in tool definitions: they can\n * perform side-effects, be async if needed, and return a result. The tool call will stay\n * pending until either `confirm` or `cancel` is called.\n *\n * @example\n * <AiTool>\n * <AiTool.Confirmation\n * // Use a destructive visual appearance\n * variant=\"destructive\"\n *\n * // The tool's arguments can be directly accessed like in `execute`\n * confirm={({ pageIds }) => {\n * const deletedPageTitles = pages\n * .filter((p) => pageIds.includes(p.id))\n * .map((page) => page.title);\n *\n * deletePages(pageIds);\n *\n * // This result will be available as `result` in the tool's `render` props\n * return { data: { deletedPageTitles } };\n * }}\n *\n * // If needed, `cancel={() => ...}` would work similarly\n * >\n * Do you want to delete these pages?\n * <PagesPreviews />\n * </AiTool.Confirmation>\n * </AiTool>\n */\n Confirmation: AiToolConfirmation,\n }\n);\n"],"names":["jsx","cn","useAiToolInvocationContext","jsxs","CodeBlock","overrides","useOverrides","useMemo","useCallback","Button","forwardRef","kInternal","useSemiControllableState","Children","Collapsible.Root","Collapsible.Trigger","ChevronRightIcon","CheckCircleFillIcon","CrossCircleFillIcon","MinusCircleIcon","SpinnerIcon","Collapsible.Content"],"mappings":";;;;;;;;;;;;;;;;;;;;AA+HA,SAAS,UAAW,CAAA,EAAE,SAAc,EAAA,GAAA,KAAA,EAA0B,EAAA;AAC5D,EAAA,uBAAQA,cAAA,CAAA,KAAA,EAAA;AAAA,IAAI,SAAA,EAAWC,KAAG,CAAA,iBAAA,EAAmB,SAAS,CAAA;AAAA,IAAI,GAAG,KAAA;AAAA,GAAO,CAAA,CAAA;AACtE,CAAA;AAEA,SAAS,eAAgB,CAAA,EAAE,SAAc,EAAA,GAAA,KAAA,EAA+B,EAAA;AACtE,EAAA,MAAM,EAAE,IAAA,EAAM,WAAa,EAAA,MAAA,KAAWC,mCAA2B,EAAA,CAAA;AAEjE,EAAA,uBACGC,eAAA,CAAA,KAAA,EAAA;AAAA,IAAI,SAAA,EAAWF,KAAG,CAAA,sBAAA,EAAwB,SAAS,CAAA;AAAA,IAAI,GAAG,KAAA;AAAA,IACzD,QAAA,EAAA;AAAA,sBAACD,cAAA,CAAAI,mBAAA,EAAA;AAAA,QACC,KAAM,EAAA,WAAA;AAAA,QACN,MAAM,IAAK,CAAA,SAAA,CAAU,IAAQ,IAAA,WAAA,EAAa,MAAM,CAAC,CAAA;AAAA,OACnD,CAAA;AAAA,MACC,MAAA,KAAW,yBACTJ,cAAA,CAAAI,mBAAA,EAAA;AAAA,QAAU,KAAM,EAAA,QAAA;AAAA,QAAS,IAAM,EAAA,IAAA,CAAK,SAAU,CAAA,MAAA,EAAQ,MAAM,CAAC,CAAA;AAAA,OAAG,CAC/D,GAAA,IAAA;AAAA,KAAA;AAAA,GACN,CAAA,CAAA;AAEJ,CAAA;AAEA,SAAS,kBAIP,CAAA;AAAA,EACA,QAAA;AAAA,EACA,OAAU,GAAA,SAAA;AAAA,EACV,OAAA;AAAA,EACA,MAAA;AAAA,a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A,SAAA,kCACbiB,mCAAoB,EAAA,EAAA,CAAA,GACnB,MAAO,CAAA,IAAA,KAAS,OAClB,mBAAAjB,cAAA,CAACkB,uCAAoB,CACnB,GAAA,MAAA,CAAO,IAAS,KAAA,WAAA,mBACjBlB,cAAA,CAAAmB,2BAAA,EAAA,EAAgB,IACf,IACF,GAAA,iBAAA,mBACDnB,cAAA,CAAAoB,mBAAA,EAAA,EAAY,CACX,GAAA,IAAA;AAAA,eACN,CACE,GAAA,IAAA;AAAA,aAAA;AAAA,WACN,CAAA;AAAA,UAEC,UAAA,mBACEpB,cAAA,CAAAqB,aAAA,EAAA;AAAA,YAAoB,SAAU,EAAA,qDAAA;AAAA,YAC7B,QAAC,kBAAArB,cAAA,CAAA,KAAA,EAAA;AAAA,cAAI,SAAU,EAAA,oBAAA;AAAA,cAAsB,QAAA;AAAA,aAAS,CAAA;AAAA,WAChD,CACE,GAAA,IAAA;AAAA,SAAA;AAAA,OACN,CAAA,CAAA;AAAA,KAEJ;AAAA,GACF;AAAA,EACA;AAAA,IAWE,IAAM,EAAA,UAAA;AAAA,IAWN,SAAW,EAAA,eAAA;AAAA,IAmCX,YAAc,EAAA,kBAAA;AAAA,GAChB;AACF;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AiTool.js","sources":["../../src/components/AiTool.tsx"],"sourcesContent":["import type {\n AiToolExecuteCallback,\n AiToolTypePack,\n JsonObject,\n NoInfr,\n} from \"@liveblocks/core\";\nimport { kInternal } from \"@liveblocks/core\";\nimport type { ComponentProps, ReactNode } from \"react\";\nimport { Children, forwardRef, useCallback, useMemo } from \"react\";\n\nimport { Button } from \"../_private\";\nimport {\n CheckCircleFillIcon,\n ChevronRightIcon,\n CrossCircleFillIcon,\n MinusCircleIcon,\n SpinnerIcon,\n} from \"../icons\";\nimport {\n type AiToolConfirmationOverrides,\n type GlobalOverrides,\n useOverrides,\n} from \"../overrides\";\nimport { useAiToolInvocationContext } from \"../primitives/AiMessage/contexts\";\nimport * as Collapsible from \"../primitives/Collapsible\";\nimport { cn } from \"../utils/cn\";\nimport { useSemiControllableState } from \"../utils/use-controllable-state\";\nimport { CodeBlock } from \"./internal/CodeBlock\";\n\nexport interface AiToolProps\n extends Omit<ComponentProps<\"div\">, \"title\" | \"children\"> {\n /**\n * The tool's title.\n *\n * By default, a human-readable version of the tool's name is used:\n * - `\"showTodo\"` → \"Show todo\"\n * - `\"get_weather\"` → \"Get weather\"\n */\n title?: string;\n\n /**\n * An optional icon displayed next to the title.\n */\n icon?: ReactNode;\n\n /**\n * The content shown in the tool.\n */\n children?: ReactNode;\n\n /**\n * The visual appearance of the tool.\n */\n variant?: \"block\" | \"minimal\";\n\n /**\n * Whether the content is currently collapsed.\n * It is not a traditional controlled value, as in if you set it to `true` it would only stay expanded.\n * Instead, it is \"semi-controlled\", meaning that setting it to `true` will expand it, but it\n * can still be collapsed/expanded by clicking on it.\n */\n collapsed?: boolean;\n\n /**\n * The event handler called when the content is collapsed or expanded by clicking on it.\n */\n onCollapsedChange?: (collapsed: boolean) => void;\n\n /**\n * Whether the content can be collapsed/expanded.\n * If set to `false`, clicking on it will have no effect.\n * If there's no content, this prop has no effect.\n */\n collapsible?: boolean;\n}\n\nexport type AiToolIconProps = ComponentProps<\"div\">;\n\nexport type AiToolInspectorProps = ComponentProps<\"div\">;\n\nexport interface AiToolConfirmationProps<\n A extends JsonObject,\n R extends JsonObject,\n> extends ComponentProps<\"div\"> {\n /**\n * The callback invoked when the user clicks the confirm button.\n */\n confirm: AiToolExecuteCallback<A, R>;\n\n /**\n * The callback invoked when the user clicks the cancel button.\n */\n cancel?: AiToolExecuteCallback<A, R>;\n\n /**\n * The visual appearance.\n */\n variant?: \"default\" | \"destructive\";\n\n /**\n * Override the component's strings.\n */\n overrides?: Partial<GlobalOverrides & AiToolConfirmationOverrides>;\n\n /**\n * The tool's result type, to be used with the `types` prop in the `render` method.\n *\n * @example\n * defineAiTool<{ value: number }>()({\n * // ...\n * render: ({ types }) => (\n * <AiTool.Confirmation\n * types={types}\n * confirm={() => {\n * return {\n * // Using `types` makes the result type-safe\n * // based on the tool's definition\n * data: { value: 123 },\n * };\n * }}\n * />\n * ),\n * })\n */\n types?: NoInfr<AiToolTypePack<A, R>>;\n}\n\nfunction AiToolIcon({ className, ...props }: AiToolIconProps) {\n return <div className={cn(\"lb-ai-tool-icon\", className)} {...props} />;\n}\n\nfunction AiToolInspector({ className, ...props }: AiToolInspectorProps) {\n const { args, partialArgs, result } = useAiToolInvocationContext();\n\n return (\n <div className={cn(\"lb-ai-tool-inspector\", className)} {...props}>\n <CodeBlock\n title=\"Arguments\"\n code={JSON.stringify(args ?? partialArgs, null, 2)}\n />\n {result !== undefined ? (\n <CodeBlock title=\"Result\" code={JSON.stringify(result, null, 2)} />\n ) : null}\n </div>\n );\n}\n\nfunction AiToolConfirmation<\n TPack extends AiToolTypePack,\n A extends JsonObject = TPack[\"A\"],\n R extends JsonObject = TPack[\"R\"],\n>({\n children,\n variant = \"default\",\n confirm,\n cancel,\n overrides,\n className,\n ...props\n}: AiToolConfirmationProps<A, R>) {\n const { stage, args, respond, name, invocationId } =\n useAiToolInvocationContext();\n const $ = useOverrides(overrides);\n\n const enabled = stage === \"executing\";\n\n const context = useMemo(() => ({ name, invocationId }), [name, invocationId]);\n\n const onConfirmClick = useCallback(async () => {\n if (enabled) {\n const result = await confirm(args as A, context);\n respond(result ?? undefined);\n }\n }, [enabled, args, confirm, respond, context]);\n\n const onCancelClick = useCallback(async () => {\n if (enabled) {\n if (cancel === undefined) {\n respond({ cancel: true });\n } else {\n const result = await cancel(args as A, context);\n respond(result ?? undefined);\n }\n }\n }, [enabled, args, cancel, respond, context]);\n\n // If there's no content and the tool has been executed (so there's no\n // confirmation UI displayed either), don't render anything.\n if (stage === \"executed\" && !children) {\n return null;\n }\n\n return (\n <div className={cn(\"lb-ai-tool-confirmation\", className)} {...props}>\n {children ? (\n <div className=\"lb-ai-tool-confirmation-content\">{children}</div>\n ) : null}\n {stage !== \"executed\" && (\n <div className=\"lb-ai-tool-confirmation-footer\">\n <div className=\"lb-ai-tool-confirmation-actions\">\n <Button\n disabled={!enabled}\n onClick={onCancelClick}\n variant=\"secondary\"\n >\n {$.AI_TOOL_CONFIRMATION_CANCEL}\n </Button>\n <Button\n disabled={!enabled}\n onClick={onConfirmClick}\n variant={variant === \"destructive\" ? \"destructive\" : \"primary\"}\n >\n {$.AI_TOOL_CONFIRMATION_CONFIRM}\n </Button>\n </div>\n </div>\n )}\n </div>\n );\n}\n\nfunction prettifyString(string: string) {\n return (\n string\n // Convert camelCase to spaces\n .replace(/([a-z])([A-Z])/g, \"$1 $2\")\n // Convert snake_case and kebab-case to spaces\n .replace(/[_-]+/g, \" \")\n // Collapse multiple following spaces\n .replace(/\\s+/g, \" \")\n // Trim leading and trailing spaces\n .trim()\n // Capitalize first word\n .toLowerCase()\n .replace(/^\\w/, (character) => character.toUpperCase())\n );\n}\n\n/**\n * A pre-built component which displays a tool call.\n *\n * By default, a human-readable version of the tool's name is used as a title:\n * - `\"showTodo\"` → \"Show todo\"\n * - `\"get_weather\"` → \"Get weather\"\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: () => (\n * <AiTool />\n * ),\n * })\n *\n * It can be customized in various ways:\n * - adding an icon\n * - customizing the title (even dynamically)\n * - adding custom content inside it\n * - collapsing it conditionally\n * - etc.\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: ({ stage, result }) => (\n * <AiTool\n * icon=\"🔍\"\n *\n * // Override the default title based on the tool's stage\n * title={stage === \"executing\" ? \"Searching…\" : \"Search results\"}\n *\n * // Start open and automatically collapse after it is executed\n * // The user can still expand/collapse it manually at any time\n * collapsed={stage === \"executed\"}\n * >\n * <SearchResults data={result.data} />\n * </AiTool>\n * ),\n * })\n *\n * It also comes with a few built-in sub-components:\n * - `AiTool.Confirmation` to display a human-in-the-loop confirmation step\n * which can be accepted or cancelled by the user.\n * - `AiTool.Inspector` to display the tool's arguments and result which can\n * be useful during development.\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: () => (\n * <AiTool>\n * <AiTool.Confirmation\n * // Use a destructive visual appearance\n * variant=\"destructive\"\n *\n * // The tool's arguments can be directly accessed like in `execute`\n * confirm={({ pageIds }) => {\n * const deletedPageTitles = pages\n * .filter((p) => pageIds.includes(p.id))\n * .map((page) => page.title);\n *\n * deletePages(pageIds);\n *\n * // This result will be available as `result` in the tool's `render` props\n * return { data: { deletedPageTitles } };\n * }}\n *\n * // If needed, `cancel={() => ...}` would work similarly\n * >\n * Do you want to delete these pages?\n * <PagesPreviews />\n * </AiTool.Confirmation>\n * </AiTool>\n * ),\n * })\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: () => (\n * <AiTool>\n * <AiTool.Inspector />\n * </AiTool>\n * ),\n * })\n */\nexport const AiTool = Object.assign(\n forwardRef<HTMLDivElement, AiToolProps>(\n (\n {\n children,\n title,\n icon,\n collapsible,\n collapsed,\n onCollapsedChange,\n variant = \"block\",\n className,\n ...props\n },\n forwardedRef\n ) => {\n const {\n stage,\n result,\n name,\n [kInternal]: { execute, messageStatus },\n } = useAiToolInvocationContext();\n // Only mark the tool as pending visually (e.g. show a spinner, add a shimmer animation, etc.)\n // if it has an `execute` method and it isn't in the \"executed\" stage.\n const isVisuallyPending =\n execute !== undefined &&\n stage !== \"executed\" &&\n // If it's in the \"receiving\" stage, we also check that the outer message is still generating.\n (stage === \"receiving\" ? messageStatus === \"generating\" : true);\n const [semiControlledCollapsed, onSemiControlledCollapsed] =\n useSemiControllableState(collapsed ?? false, onCollapsedChange);\n // TODO: This check won't work for cases like:\n // <AiTool>\n // <ComponentThatRendersNull />\n // <ComponentThatAlsoRendersNull />\n // </AiTool>\n // One solution could be to check the DOM on every render with `useLayoutEffect`\n // to see if there's any actual content.\n // For now we're limiting the visual issues caused by the above by using CSS's\n // `:empty` pseudo-class to make the content 0px high if it's actually empty.\n const hasContent = Children.count(children) > 0;\n // If there's no content, the tool is never collapsible.\n const isCollapsible = hasContent ? (collapsible ?? true) : false;\n const resolvedTitle = useMemo(() => {\n return title ?? prettifyString(name);\n }, [title, name]);\n\n // `AiTool` uses \"collapsed\" instead of \"open\" (like the `Composer` component) because \"open\"\n // makes sense next to something called \"Collapsible\" but less so for something called \"AiTool\".\n const handleCollapsibleOpenChange = useCallback(\n (open: boolean) => {\n onSemiControlledCollapsed(!open);\n },\n [onSemiControlledCollapsed]\n );\n\n return (\n <Collapsible.Root\n ref={forwardedRef}\n className={cn(\n \"lb-collapsible lb-ai-tool\",\n `lb-ai-tool:variant-${variant}`,\n className\n )}\n {...props}\n // Regardless of `semiControlledCollapsed`, the collapsible is closed if there's no content.\n open={hasContent ? !semiControlledCollapsed : false}\n onOpenChange={handleCollapsibleOpenChange}\n disabled={!isCollapsible}\n data-result={result?.type}\n data-stage={stage}\n >\n <Collapsible.Trigger\n className={cn(\n \"lb-collapsible-trigger lb-ai-tool-header\",\n variant === \"minimal\" && isVisuallyPending && \"lb-ai-chat-pending\"\n )}\n >\n {icon ? (\n <div className=\"lb-ai-tool-header-icon-container\">{icon}</div>\n ) : null}\n <span className=\"lb-ai-tool-header-title\">{resolvedTitle}</span>\n {isCollapsible ? (\n <span className=\"lb-collapsible-chevron lb-icon-container\">\n <ChevronRightIcon />\n </span>\n ) : null}\n {variant !== \"minimal\" ? (\n <div className=\"lb-ai-tool-header-status\">\n {stage === \"executed\" ? (\n result.type === \"success\" ? (\n <CheckCircleFillIcon />\n ) : result.type === \"error\" ? (\n <CrossCircleFillIcon />\n ) : result.type === \"cancelled\" ? (\n <MinusCircleIcon />\n ) : null\n ) : isVisuallyPending ? (\n <SpinnerIcon />\n ) : null}\n </div>\n ) : null}\n </Collapsible.Trigger>\n\n {hasContent ? (\n <Collapsible.Content className=\"lb-collapsible-content lb-ai-tool-content-container\">\n <div className=\"lb-ai-tool-content\">{children}</div>\n </Collapsible.Content>\n ) : null}\n </Collapsible.Root>\n );\n }\n ),\n {\n /**\n * Display an icon in a container.\n *\n * @example\n * <AiTool\n * icon={\n * <AiTool.Icon>🔍</AiTool.Icon>\n * }\n * />\n */\n Icon: AiToolIcon,\n\n /**\n * Display the tool's arguments and result, which can be useful during\n * development.\n *\n * @example\n * <AiTool>\n * <AiTool.Inspector />\n * </AiTool>\n */\n Inspector: AiToolInspector,\n\n /**\n * Display a human-in-the-loop confirmation step which can be accepted\n * or cancelled by the user.\n *\n * The `confirm` and `cancel` callbacks work like `execute` in tool definitions: they can\n * perform side-effects, be async if needed, and return a result. The tool call will stay\n * pending until either `confirm` or `cancel` is called.\n *\n * @example\n * <AiTool>\n * <AiTool.Confirmation\n * // Use a destructive visual appearance\n * variant=\"destructive\"\n *\n * // The tool's arguments can be directly accessed like in `execute`\n * confirm={({ pageIds }) => {\n * const deletedPageTitles = pages\n * .filter((p) => pageIds.includes(p.id))\n * .map((page) => page.title);\n *\n * deletePages(pageIds);\n *\n * // This result will be available as `result` in the tool's `render` props\n * return { data: { deletedPageTitles } };\n * }}\n *\n * // If needed, `cancel={() => ...}` would work similarly\n * >\n * Do you want to delete these pages?\n * <PagesPreviews />\n * </AiTool.Confirmation>\n * </AiTool>\n */\n Confirmation: AiToolConfirmation,\n }\n);\n"],"names":["Collapsible.Root","Collapsible.Trigger","Collapsible.Content"],"mappings":";;;;;;;;;;;;;;;;;;AA+HA,SAAS,UAAW,CAAA,EAAE,SAAc,EAAA,GAAA,KAAA,EAA0B,EAAA;AAC5D,EAAA,uBAAQ,GAAA,CAAA,KAAA,EAAA;AAAA,IAAI,SAAA,EAAW,EAAG,CAAA,iBAAA,EAAmB,SAAS,CAAA;AAAA,IAAI,GAAG,KAAA;AAAA,GAAO,CAAA,CAAA;AACtE,CAAA;AAEA,SAAS,eAAgB,CAAA,EAAE,SAAc,EAAA,GAAA,KAAA,EAA+B,EAAA;AACtE,EAAA,MAAM,EAAE,IAAA,EAAM,WAAa,EAAA,MAAA,KAAW,0BAA2B,EAAA,CAAA;AAEjE,EAAA,uBACG,IAAA,CAAA,KAAA,EAAA;AAAA,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AA;AAAA,IAWN,SAAW,EAAA,eAAA;AAAA,IAmCX,YAAc,EAAA,kBAAA;AAAA,GAChB;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"AiTool.js","sources":["../../src/components/AiTool.tsx"],"sourcesContent":["import type {\n AiToolExecuteCallback,\n AiToolTypePack,\n JsonObject,\n NoInfr,\n} from \"@liveblocks/core\";\nimport { kInternal } from \"@liveblocks/core\";\nimport type { ComponentProps, ReactNode } from \"react\";\nimport { Children, forwardRef, useCallback, useMemo } from \"react\";\n\nimport { Button } from \"../_private\";\nimport {\n CheckCircleFillIcon,\n ChevronRightIcon,\n CrossCircleFillIcon,\n MinusCircleIcon,\n SpinnerIcon,\n} from \"../icons\";\nimport {\n type AiToolConfirmationOverrides,\n type GlobalOverrides,\n useOverrides,\n} from \"../overrides\";\nimport { useAiToolInvocationContext } from \"../primitives/AiMessage/contexts\";\nimport * as Collapsible from \"../primitives/Collapsible\";\nimport { cn } from \"../utils/cn\";\nimport { useSemiControllableState } from \"../utils/use-controllable-state\";\nimport { CodeBlock } from \"./internal/CodeBlock\";\n\nexport interface AiToolProps\n extends Omit<ComponentProps<\"div\">, \"title\" | \"children\"> {\n /**\n * The tool's title.\n *\n * By default, a human-readable version of the tool's name is used:\n * - `\"showTodo\"` → \"Show todo\"\n * - `\"get_weather\"` → \"Get weather\"\n */\n title?: ReactNode;\n\n /**\n * An optional icon displayed next to the title.\n */\n icon?: ReactNode;\n\n /**\n * The content shown in the tool.\n */\n children?: ReactNode;\n\n /**\n * The visual appearance of the tool.\n */\n variant?: \"block\" | \"minimal\";\n\n /**\n * Whether the content is currently collapsed.\n * It is not a traditional controlled value, as in if you set it to `true` it would only stay expanded.\n * Instead, it is \"semi-controlled\", meaning that setting it to `true` will expand it, but it\n * can still be collapsed/expanded by clicking on it.\n */\n collapsed?: boolean;\n\n /**\n * The event handler called when the content is collapsed or expanded by clicking on it.\n */\n onCollapsedChange?: (collapsed: boolean) => void;\n\n /**\n * Whether the content can be collapsed/expanded.\n * If set to `false`, clicking on it will have no effect.\n * If there's no content, this prop has no effect.\n */\n collapsible?: boolean;\n}\n\nexport type AiToolIconProps = ComponentProps<\"div\">;\n\nexport type AiToolInspectorProps = ComponentProps<\"div\">;\n\nexport interface AiToolConfirmationProps<\n A extends JsonObject,\n R extends JsonObject,\n> extends ComponentProps<\"div\"> {\n /**\n * The callback invoked when the user clicks the confirm button.\n */\n confirm: AiToolExecuteCallback<A, R>;\n\n /**\n * The callback invoked when the user clicks the cancel button.\n */\n cancel?: AiToolExecuteCallback<A, R>;\n\n /**\n * The visual appearance.\n */\n variant?: \"default\" | \"destructive\";\n\n /**\n * Override the component's strings.\n */\n overrides?: Partial<GlobalOverrides & AiToolConfirmationOverrides>;\n\n /**\n * The tool's result type, to be used with the `types` prop in the `render` method.\n *\n * @example\n * defineAiTool<{ value: number }>()({\n * // ...\n * render: ({ types }) => (\n * <AiTool.Confirmation\n * types={types}\n * confirm={() => {\n * return {\n * // Using `types` makes the result type-safe\n * // based on the tool's definition\n * data: { value: 123 },\n * };\n * }}\n * />\n * ),\n * })\n */\n types?: NoInfr<AiToolTypePack<A, R>>;\n}\n\nfunction AiToolIcon({ className, ...props }: AiToolIconProps) {\n return <div className={cn(\"lb-ai-tool-icon\", className)} {...props} />;\n}\n\nfunction AiToolInspector({ className, ...props }: AiToolInspectorProps) {\n const { args, partialArgs, result } = useAiToolInvocationContext();\n\n return (\n <div className={cn(\"lb-ai-tool-inspector\", className)} {...props}>\n <CodeBlock\n title=\"Arguments\"\n code={JSON.stringify(args ?? partialArgs, null, 2)}\n />\n {result !== undefined ? (\n <CodeBlock title=\"Result\" code={JSON.stringify(result, null, 2)} />\n ) : null}\n </div>\n );\n}\n\nfunction AiToolConfirmation<\n TPack extends AiToolTypePack,\n A extends JsonObject = TPack[\"A\"],\n R extends JsonObject = TPack[\"R\"],\n>({\n children,\n variant = \"default\",\n confirm,\n cancel,\n overrides,\n className,\n ...props\n}: AiToolConfirmationProps<A, R>) {\n const { stage, args, respond, name, invocationId } =\n useAiToolInvocationContext();\n const $ = useOverrides(overrides);\n\n const enabled = stage === \"executing\";\n\n const context = useMemo(() => ({ name, invocationId }), [name, invocationId]);\n\n const onConfirmClick = useCallback(async () => {\n if (enabled) {\n const result = await confirm(args as A, context);\n respond(result ?? undefined);\n }\n }, [enabled, args, confirm, respond, context]);\n\n const onCancelClick = useCallback(async () => {\n if (enabled) {\n if (cancel === undefined) {\n respond({ cancel: true });\n } else {\n const result = await cancel(args as A, context);\n respond(result ?? undefined);\n }\n }\n }, [enabled, args, cancel, respond, context]);\n\n // If there's no content and the tool has been executed (so there's no\n // confirmation UI displayed either), don't render anything.\n if (stage === \"executed\" && !children) {\n return null;\n }\n\n return (\n <div className={cn(\"lb-ai-tool-confirmation\", className)} {...props}>\n {children ? (\n <div className=\"lb-ai-tool-confirmation-content\">{children}</div>\n ) : null}\n {stage !== \"executed\" && (\n <div className=\"lb-ai-tool-confirmation-footer\">\n <div className=\"lb-ai-tool-confirmation-actions\">\n <Button\n disabled={!enabled}\n onClick={onCancelClick}\n variant=\"secondary\"\n >\n {$.AI_TOOL_CONFIRMATION_CANCEL}\n </Button>\n <Button\n disabled={!enabled}\n onClick={onConfirmClick}\n variant={variant === \"destructive\" ? \"destructive\" : \"primary\"}\n >\n {$.AI_TOOL_CONFIRMATION_CONFIRM}\n </Button>\n </div>\n </div>\n )}\n </div>\n );\n}\n\nfunction prettifyString(string: string) {\n return (\n string\n // Convert camelCase to spaces\n .replace(/([a-z])([A-Z])/g, \"$1 $2\")\n // Convert snake_case and kebab-case to spaces\n .replace(/[_-]+/g, \" \")\n // Collapse multiple following spaces\n .replace(/\\s+/g, \" \")\n // Trim leading and trailing spaces\n .trim()\n // Capitalize first word\n .toLowerCase()\n .replace(/^\\w/, (character) => character.toUpperCase())\n );\n}\n\n/**\n * A pre-built component which displays a tool call.\n *\n * By default, a human-readable version of the tool's name is used as a title:\n * - `\"showTodo\"` → \"Show todo\"\n * - `\"get_weather\"` → \"Get weather\"\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: () => (\n * <AiTool />\n * ),\n * })\n *\n * It can be customized in various ways:\n * - adding an icon\n * - customizing the title (even dynamically)\n * - adding custom content inside it\n * - collapsing it conditionally\n * - etc.\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: ({ stage, result }) => (\n * <AiTool\n * icon=\"🔍\"\n *\n * // Override the default title based on the tool's stage\n * title={stage === \"executing\" ? \"Searching…\" : \"Search results\"}\n *\n * // Start open and automatically collapse after it is executed\n * // The user can still expand/collapse it manually at any time\n * collapsed={stage === \"executed\"}\n * >\n * <SearchResults data={result.data} />\n * </AiTool>\n * ),\n * })\n *\n * It also comes with a few built-in sub-components:\n * - `AiTool.Confirmation` to display a human-in-the-loop confirmation step\n * which can be accepted or cancelled by the user.\n * - `AiTool.Inspector` to display the tool's arguments and result which can\n * be useful during development.\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: () => (\n * <AiTool>\n * <AiTool.Confirmation\n * // Use a destructive visual appearance\n * variant=\"destructive\"\n *\n * // The tool's arguments can be directly accessed like in `execute`\n * confirm={({ pageIds }) => {\n * const deletedPageTitles = pages\n * .filter((p) => pageIds.includes(p.id))\n * .map((page) => page.title);\n *\n * deletePages(pageIds);\n *\n * // This result will be available as `result` in the tool's `render` props\n * return { data: { deletedPageTitles } };\n * }}\n *\n * // If needed, `cancel={() => ...}` would work similarly\n * >\n * Do you want to delete these pages?\n * <PagesPreviews />\n * </AiTool.Confirmation>\n * </AiTool>\n * ),\n * })\n *\n * @example\n * defineAiTool()({\n * // ...\n * render: () => (\n * <AiTool>\n * <AiTool.Inspector />\n * </AiTool>\n * ),\n * })\n */\nexport const AiTool = Object.assign(\n forwardRef<HTMLDivElement, AiToolProps>(\n (\n {\n children,\n title,\n icon,\n collapsible,\n collapsed,\n onCollapsedChange,\n variant = \"block\",\n className,\n ...props\n },\n forwardedRef\n ) => {\n const {\n stage,\n result,\n name,\n [kInternal]: { execute, messageStatus },\n } = useAiToolInvocationContext();\n // Only mark the tool as pending visually (e.g. show a spinner, add a shimmer animation, etc.)\n // if it has an `execute` method and it isn't in the \"executed\" stage.\n const isVisuallyPending =\n execute !== undefined &&\n stage !== \"executed\" &&\n // If it's in the \"receiving\" stage, we also check that the outer message is still generating.\n (stage === \"receiving\" ? messageStatus === \"generating\" : true);\n const [semiControlledCollapsed, onSemiControlledCollapsed] =\n useSemiControllableState(collapsed ?? false, onCollapsedChange);\n // TODO: This check won't work for cases like:\n // <AiTool>\n // <ComponentThatRendersNull />\n // <ComponentThatAlsoRendersNull />\n // </AiTool>\n // One solution could be to check the DOM on every render with `useLayoutEffect`\n // to see if there's any actual content.\n // For now we're limiting the visual issues caused by the above by using CSS's\n // `:empty` pseudo-class to make the content 0px high if it's actually empty.\n const hasContent = Children.count(children) > 0;\n // If there's no content, the tool is never collapsible.\n const isCollapsible = hasContent ? (collapsible ?? true) : false;\n const resolvedTitle = useMemo(() => {\n return title ?? prettifyString(name);\n }, [title, name]);\n\n // `AiTool` uses \"collapsed\" instead of \"open\" (like the `Composer` component) because \"open\"\n // makes sense next to something called \"Collapsible\" but less so for something called \"AiTool\".\n const handleCollapsibleOpenChange = useCallback(\n (open: boolean) => {\n onSemiControlledCollapsed(!open);\n },\n [onSemiControlledCollapsed]\n );\n\n return (\n <Collapsible.Root\n ref={forwardedRef}\n className={cn(\n \"lb-collapsible lb-ai-tool\",\n `lb-ai-tool:variant-${variant}`,\n className\n )}\n {...props}\n // Regardless of `semiControlledCollapsed`, the collapsible is closed if there's no content.\n open={hasContent ? !semiControlledCollapsed : false}\n onOpenChange={handleCollapsibleOpenChange}\n disabled={!isCollapsible}\n data-result={result?.type}\n data-stage={stage}\n >\n <Collapsible.Trigger\n className={cn(\n \"lb-collapsible-trigger lb-ai-tool-header\",\n variant === \"minimal\" && isVisuallyPending && \"lb-ai-chat-pending\"\n )}\n >\n {icon ? (\n <div className=\"lb-ai-tool-header-icon-container\">{icon}</div>\n ) : null}\n <span className=\"lb-ai-tool-header-title\">{resolvedTitle}</span>\n {isCollapsible ? (\n <span className=\"lb-collapsible-chevron lb-icon-container\">\n <ChevronRightIcon />\n </span>\n ) : null}\n {variant !== \"minimal\" ? (\n <div className=\"lb-ai-tool-header-status\">\n {stage === \"executed\" ? (\n result.type === \"success\" ? (\n <CheckCircleFillIcon />\n ) : result.type === \"error\" ? (\n <CrossCircleFillIcon />\n ) : result.type === \"cancelled\" ? (\n <MinusCircleIcon />\n ) : null\n ) : isVisuallyPending ? (\n <SpinnerIcon />\n ) : null}\n </div>\n ) : null}\n </Collapsible.Trigger>\n\n {hasContent ? (\n <Collapsible.Content className=\"lb-collapsible-content lb-ai-tool-content-container\">\n <div className=\"lb-ai-tool-content\">{children}</div>\n </Collapsible.Content>\n ) : null}\n </Collapsible.Root>\n );\n }\n ),\n {\n /**\n * Display an icon in a container.\n *\n * @example\n * <AiTool\n * icon={\n * <AiTool.Icon>🔍</AiTool.Icon>\n * }\n * />\n */\n Icon: AiToolIcon,\n\n /**\n * Display the tool's arguments and result, which can be useful during\n * development.\n *\n * @example\n * <AiTool>\n * <AiTool.Inspector />\n * </AiTool>\n */\n Inspector: AiToolInspector,\n\n /**\n * Display a human-in-the-loop confirmation step which can be accepted\n * or cancelled by the user.\n *\n * The `confirm` and `cancel` callbacks work like `execute` in tool definitions: they can\n * perform side-effects, be async if needed, and return a result. The tool call will stay\n * pending until either `confirm` or `cancel` is called.\n *\n * @example\n * <AiTool>\n * <AiTool.Confirmation\n * // Use a destructive visual appearance\n * variant=\"destructive\"\n *\n * // The tool's arguments can be directly accessed like in `execute`\n * confirm={({ pageIds }) => {\n * const deletedPageTitles = pages\n * .filter((p) => pageIds.includes(p.id))\n * .map((page) => page.title);\n *\n * deletePages(pageIds);\n *\n * // This result will be available as `result` in the tool's `render` props\n * return { data: { deletedPageTitles } };\n * }}\n *\n * // If needed, `cancel={() => ...}` would work similarly\n * >\n * Do you want to delete these pages?\n * <PagesPreviews />\n * </AiTool.Confirmation>\n *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uBAAoB,CACnB,GAAA,MAAA,CAAO,IAAS,KAAA,WAAA,mBACjB,GAAA,CAAA,eAAA,EAAA,EAAgB,IACf,IACF,GAAA,iBAAA,mBACD,GAAA,CAAA,WAAA,EAAA,EAAY,CACX,GAAA,IAAA;AAAA,eACN,CACE,GAAA,IAAA;AAAA,aAAA;AAAA,WACN,CAAA;AAAA,UAEC,UAAA,mBACE,GAAA,CAAAC,kBAAA,EAAA;AAAA,YAAoB,SAAU,EAAA,qDAAA;AAAA,YAC7B,QAAC,kBAAA,GAAA,CAAA,KAAA,EAAA;AAAA,cAAI,SAAU,EAAA,oBAAA;AAAA,cAAsB,QAAA;AAAA,aAAS,CAAA;AAAA,WAChD,CACE,GAAA,IAAA;AAAA,SAAA;AAAA,OACN,CAAA,CAAA;AAAA,KAEJ;AAAA,GACF;AAAA,EACA;AAAA,IAWE,IAAM,EAAA,UAAA;AAAA,IAWN,SAAW,EAAA,eAAA;AAAA,IAmCX,YAAc,EAAA,kBAAA;AAAA,GAChB;AACF;;;;"}
|
|
@@ -62,9 +62,9 @@ const AiComposer = react.forwardRef(
|
|
|
62
62
|
overrides: overrides$1,
|
|
63
63
|
className,
|
|
64
64
|
chatId,
|
|
65
|
-
knowledge: localKnowledge,
|
|
66
65
|
branchId,
|
|
67
66
|
copilotId,
|
|
67
|
+
responseTimeout,
|
|
68
68
|
stream = true,
|
|
69
69
|
onComposerSubmitted,
|
|
70
70
|
...props
|
|
@@ -73,7 +73,7 @@ const AiComposer = react.forwardRef(
|
|
|
73
73
|
const sendAiMessage = react$1.useSendAiMessage(chatId, {
|
|
74
74
|
stream,
|
|
75
75
|
copilotId,
|
|
76
|
-
|
|
76
|
+
timeout: responseTimeout
|
|
77
77
|
});
|
|
78
78
|
const handleComposerSubmit = react.useCallback(
|
|
79
79
|
(message, event) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AiComposer.cjs","sources":["../../../src/components/internal/AiComposer.tsx"],"sourcesContent":["import {\n type AiChatMessage,\n type
|
|
1
|
+
{"version":3,"file":"AiComposer.cjs","sources":["../../../src/components/internal/AiComposer.tsx"],"sourcesContent":["import {\n type AiChatMessage,\n type CopilotId,\n type MessageId,\n} from \"@liveblocks/core\";\nimport { useSendAiMessage } from \"@liveblocks/react\";\nimport {\n type ComponentProps,\n type FormEvent,\n forwardRef,\n type SyntheticEvent,\n useCallback,\n} from \"react\";\n\nimport { SendIcon } from \"../../icons/Send\";\nimport { StopIcon } from \"../../icons/Stop\";\nimport {\n type AiComposerOverrides,\n type GlobalOverrides,\n useOverrides,\n} from \"../../overrides\";\nimport * as AiComposerPrimitive from \"../../primitives/AiComposer\";\nimport { useAiComposer } from \"../../primitives/AiComposer/contexts\";\nimport type {\n AiComposerEditorProps,\n AiComposerFormProps,\n AiComposerSubmitMessage,\n} from \"../../primitives/AiComposer/types\";\nimport { cn } from \"../../utils/cn\";\nimport { Button } from \"./Button\";\nimport { ShortcutTooltip, TooltipProvider } from \"./Tooltip\";\n\n/* -------------------------------------------------------------------------------------------------\n * AiComposer\n * -----------------------------------------------------------------------------------------------*/\nexport interface AiComposerProps\n extends Omit<ComponentProps<\"form\">, \"defaultValue\"> {\n /**\n * The composer's initial value.\n */\n defaultValue?: string;\n\n /**\n * The event handler called when the composer is submitted.\n */\n onComposerSubmit?: (\n message: AiComposerSubmitMessage,\n event: FormEvent<HTMLFormElement>\n ) => void;\n\n /**\n * The event handler called after the composer is submitted.\n *\n * @internal This API will change, and is not considered stable. DO NOT RELY on it.\n */\n onComposerSubmitted?: (message: AiChatMessage) => void;\n\n /**\n * Whether the composer is disabled.\n */\n disabled?: AiComposerFormProps[\"disabled\"];\n\n /**\n * Whether to focus the composer on mount.\n */\n autoFocus?: AiComposerEditorProps[\"autoFocus\"];\n\n /**\n * Override the component's strings.\n */\n overrides?: Partial<GlobalOverrides & AiComposerOverrides>;\n\n /**\n * The ID of the chat the composer belongs to.\n */\n chatId: string;\n\n /**\n * The ID of the copilot to use to send the message.\n */\n copilotId?: CopilotId;\n\n /**\n * The time, in milliseconds, before an AI response will timeout.\n */\n responseTimeout?: number;\n\n /**\n * @internal\n */\n branchId?: MessageId;\n\n /**\n * @internal\n */\n stream?: boolean;\n}\n\nfunction AiComposerAction({\n overrides,\n}: {\n overrides?: AiComposerProps[\"overrides\"];\n}) {\n const { canAbort } = useAiComposer();\n const $ = useOverrides(overrides);\n\n const preventDefault = useCallback((event: SyntheticEvent) => {\n event.preventDefault();\n }, []);\n\n const stopPropagation = useCallback((event: SyntheticEvent) => {\n event.stopPropagation();\n }, []);\n\n return canAbort ? (\n <ShortcutTooltip content={$.AI_COMPOSER_ABORT}>\n <AiComposerPrimitive.Abort asChild>\n <Button\n onPointerDown={preventDefault}\n onClick={stopPropagation}\n className=\"lb-ai-composer-action\"\n variant=\"secondary\"\n aria-label={$.AI_COMPOSER_ABORT}\n icon={<StopIcon />}\n />\n </AiComposerPrimitive.Abort>\n </ShortcutTooltip>\n ) : (\n <ShortcutTooltip content={$.AI_COMPOSER_SEND} shortcut=\"Enter\">\n <AiComposerPrimitive.Submit asChild>\n <Button\n onPointerDown={preventDefault}\n onClick={stopPropagation}\n className=\"lb-ai-composer-action\"\n variant=\"primary\"\n aria-label={$.AI_COMPOSER_SEND}\n icon={<SendIcon />}\n />\n </AiComposerPrimitive.Submit>\n </ShortcutTooltip>\n );\n}\n\nexport const AiComposer = forwardRef<HTMLFormElement, AiComposerProps>(\n (\n {\n defaultValue,\n onComposerSubmit,\n disabled,\n autoFocus,\n overrides,\n className,\n chatId,\n branchId,\n copilotId,\n responseTimeout,\n stream = true,\n onComposerSubmitted,\n ...props\n },\n forwardedRef\n ) => {\n const $ = useOverrides(overrides);\n const sendAiMessage = useSendAiMessage(chatId, {\n stream,\n copilotId,\n timeout: responseTimeout,\n });\n\n const handleComposerSubmit = useCallback(\n (message: AiComposerSubmitMessage, event: FormEvent<HTMLFormElement>) => {\n onComposerSubmit?.(message, event);\n\n if (event.isDefaultPrevented()) return;\n\n const newMessage = sendAiMessage(message.text);\n\n onComposerSubmitted?.(newMessage);\n },\n [onComposerSubmit, sendAiMessage, onComposerSubmitted]\n );\n\n return (\n <TooltipProvider>\n <AiComposerPrimitive.Form\n className={cn(\n \"lb-root lb-ai-composer lb-ai-composer-form\",\n className\n )}\n dir={$.dir}\n {...props}\n disabled={disabled}\n ref={forwardedRef}\n onComposerSubmit={handleComposerSubmit}\n chatId={chatId}\n branchId={branchId}\n >\n <div className=\"lb-ai-composer-editor-container\">\n <AiComposerPrimitive.Editor\n autoFocus={autoFocus}\n className=\"lb-ai-composer-editor\"\n placeholder={$.AI_COMPOSER_PLACEHOLDER}\n defaultValue={defaultValue}\n />\n\n <div className=\"lb-ai-composer-footer\">\n <div className=\"lb-ai-composer-editor-actions\">\n {/* No actions for now but it makes sense to keep the DOM structure */}\n </div>\n\n <div className=\"lb-ai-composer-actions\">\n <AiComposerAction overrides={overrides} />\n </div>\n </div>\n </div>\n </AiComposerPrimitive.Form>\n </TooltipProvider>\n );\n }\n);\n"],"names":["overrides","useAiComposer","useOverrides","useCallback","jsx","ShortcutTooltip","AiComposerPrimitive.Abort","Button","StopIcon","AiComposerPrimitive.Submit","SendIcon","forwardRef","useSendAiMessage","TooltipProvider","AiComposerPrimitive.Form","cn","jsxs","AiComposerPrimitive.Editor"],"mappings":";;;;;;;;;;;;;;;AAkGA,SAAS,gBAAiB,CAAA;AAAA,aACxBA,WAAA;AACF,CAEG,EAAA;AACD,EAAM,MAAA,EAAE,QAAS,EAAA,GAAIC,sBAAc,EAAA,CAAA;AACnC,EAAM,MAAA,CAAA,GAAIC,uBAAaF,WAAS,CAAA,CAAA;AAEhC,EAAM,MAAA,cAAA,GAAiBG,iBAAY,CAAA,CAAC,KAA0B,KAAA;AAC5D,IAAA,KAAA,CAAM,cAAe,EAAA,CAAA;AAAA,GACvB,EAAG,EAAE,CAAA,CAAA;AAEL,EAAM,MAAA,eAAA,GAAkBA,iBAAY,CAAA,CAAC,KAA0B,KAAA;AAC7D,IAA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,SAAA;AAAA,cACA,SAAU,EAAA,uBAAA;AAAA,cACV,aAAa,CAAE,CAAA,uBAAA;AAAA,cACf,YAAA;AAAA,aACF,CAAA;AAAA,4BAECD,eAAA,CAAA,KAAA,EAAA;AAAA,cAAI,SAAU,EAAA,uBAAA;AAAA,cACb,QAAA,EAAA;AAAA,gCAACZ,cAAA,CAAA,KAAA,EAAA;AAAA,kBAAI,SAAU,EAAA,+BAAA;AAAA,iBAEf,CAAA;AAAA,gCAECA,cAAA,CAAA,KAAA,EAAA;AAAA,kBAAI,SAAU,EAAA,wBAAA;AAAA,kBACb,QAAC,kBAAAA,cAAA,CAAA,gBAAA,EAAA;AAAA,+BAAiBJ,WAAA;AAAA,mBAAsB,CAAA;AAAA,iBAC1C,CAAA;AAAA,eAAA;AAAA,aACF,CAAA;AAAA,WAAA;AAAA,SACF,CAAA;AAAA,OACF,CAAA;AAAA,KACF,CAAA,CAAA;AAAA,GAEJ;AACF;;;;"}
|
|
@@ -60,9 +60,9 @@ const AiComposer = forwardRef(
|
|
|
60
60
|
overrides,
|
|
61
61
|
className,
|
|
62
62
|
chatId,
|
|
63
|
-
knowledge: localKnowledge,
|
|
64
63
|
branchId,
|
|
65
64
|
copilotId,
|
|
65
|
+
responseTimeout,
|
|
66
66
|
stream = true,
|
|
67
67
|
onComposerSubmitted,
|
|
68
68
|
...props
|
|
@@ -71,7 +71,7 @@ const AiComposer = forwardRef(
|
|
|
71
71
|
const sendAiMessage = useSendAiMessage(chatId, {
|
|
72
72
|
stream,
|
|
73
73
|
copilotId,
|
|
74
|
-
|
|
74
|
+
timeout: responseTimeout
|
|
75
75
|
});
|
|
76
76
|
const handleComposerSubmit = useCallback(
|
|
77
77
|
(message, event) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AiComposer.js","sources":["../../../src/components/internal/AiComposer.tsx"],"sourcesContent":["import {\n type AiChatMessage,\n type
|
|
1
|
+
{"version":3,"file":"AiComposer.js","sources":["../../../src/components/internal/AiComposer.tsx"],"sourcesContent":["import {\n type AiChatMessage,\n type CopilotId,\n type MessageId,\n} from \"@liveblocks/core\";\nimport { useSendAiMessage } from \"@liveblocks/react\";\nimport {\n type ComponentProps,\n type FormEvent,\n forwardRef,\n type SyntheticEvent,\n useCallback,\n} from \"react\";\n\nimport { SendIcon } from \"../../icons/Send\";\nimport { StopIcon } from \"../../icons/Stop\";\nimport {\n type AiComposerOverrides,\n type GlobalOverrides,\n useOverrides,\n} from \"../../overrides\";\nimport * as AiComposerPrimitive from \"../../primitives/AiComposer\";\nimport { useAiComposer } from \"../../primitives/AiComposer/contexts\";\nimport type {\n AiComposerEditorProps,\n AiComposerFormProps,\n AiComposerSubmitMessage,\n} from \"../../primitives/AiComposer/types\";\nimport { cn } from \"../../utils/cn\";\nimport { Button } from \"./Button\";\nimport { ShortcutTooltip, TooltipProvider } from \"./Tooltip\";\n\n/* -------------------------------------------------------------------------------------------------\n * AiComposer\n * -----------------------------------------------------------------------------------------------*/\nexport interface AiComposerProps\n extends Omit<ComponentProps<\"form\">, \"defaultValue\"> {\n /**\n * The composer's initial value.\n */\n defaultValue?: string;\n\n /**\n * The event handler called when the composer is submitted.\n */\n onComposerSubmit?: (\n message: AiComposerSubmitMessage,\n event: FormEvent<HTMLFormElement>\n ) => void;\n\n /**\n * The event handler called after the composer is submitted.\n *\n * @internal This API will change, and is not considered stable. DO NOT RELY on it.\n */\n onComposerSubmitted?: (message: AiChatMessage) => void;\n\n /**\n * Whether the composer is disabled.\n */\n disabled?: AiComposerFormProps[\"disabled\"];\n\n /**\n * Whether to focus the composer on mount.\n */\n autoFocus?: AiComposerEditorProps[\"autoFocus\"];\n\n /**\n * Override the component's strings.\n */\n overrides?: Partial<GlobalOverrides & AiComposerOverrides>;\n\n /**\n * The ID of the chat the composer belongs to.\n */\n chatId: string;\n\n /**\n * The ID of the copilot to use to send the message.\n */\n copilotId?: CopilotId;\n\n /**\n * The time, in milliseconds, before an AI response will timeout.\n */\n responseTimeout?: number;\n\n /**\n * @internal\n */\n branchId?: MessageId;\n\n /**\n * @internal\n */\n stream?: boolean;\n}\n\nfunction AiComposerAction({\n overrides,\n}: {\n overrides?: AiComposerProps[\"overrides\"];\n}) {\n const { canAbort } = useAiComposer();\n const $ = useOverrides(overrides);\n\n const preventDefault = useCallback((event: SyntheticEvent) => {\n event.preventDefault();\n }, []);\n\n const stopPropagation = useCallback((event: SyntheticEvent) => {\n event.stopPropagation();\n }, []);\n\n return canAbort ? (\n <ShortcutTooltip content={$.AI_COMPOSER_ABORT}>\n <AiComposerPrimitive.Abort asChild>\n <Button\n onPointerDown={preventDefault}\n onClick={stopPropagation}\n className=\"lb-ai-composer-action\"\n variant=\"secondary\"\n aria-label={$.AI_COMPOSER_ABORT}\n icon={<StopIcon />}\n />\n </AiComposerPrimitive.Abort>\n </ShortcutTooltip>\n ) : (\n <ShortcutTooltip content={$.AI_COMPOSER_SEND} shortcut=\"Enter\">\n <AiComposerPrimitive.Submit asChild>\n <Button\n onPointerDown={preventDefault}\n onClick={stopPropagation}\n className=\"lb-ai-composer-action\"\n variant=\"primary\"\n aria-label={$.AI_COMPOSER_SEND}\n icon={<SendIcon />}\n />\n </AiComposerPrimitive.Submit>\n </ShortcutTooltip>\n );\n}\n\nexport const AiComposer = forwardRef<HTMLFormElement, AiComposerProps>(\n (\n {\n defaultValue,\n onComposerSubmit,\n disabled,\n autoFocus,\n overrides,\n className,\n chatId,\n branchId,\n copilotId,\n responseTimeout,\n stream = true,\n onComposerSubmitted,\n ...props\n },\n forwardedRef\n ) => {\n const $ = useOverrides(overrides);\n const sendAiMessage = useSendAiMessage(chatId, {\n stream,\n copilotId,\n timeout: responseTimeout,\n });\n\n const handleComposerSubmit = useCallback(\n (message: AiComposerSubmitMessage, event: FormEvent<HTMLFormElement>) => {\n onComposerSubmit?.(message, event);\n\n if (event.isDefaultPrevented()) return;\n\n const newMessage = sendAiMessage(message.text);\n\n onComposerSubmitted?.(newMessage);\n },\n [onComposerSubmit, sendAiMessage, onComposerSubmitted]\n );\n\n return (\n <TooltipProvider>\n <AiComposerPrimitive.Form\n className={cn(\n \"lb-root lb-ai-composer lb-ai-composer-form\",\n className\n )}\n dir={$.dir}\n {...props}\n disabled={disabled}\n ref={forwardedRef}\n onComposerSubmit={handleComposerSubmit}\n chatId={chatId}\n branchId={branchId}\n >\n <div className=\"lb-ai-composer-editor-container\">\n <AiComposerPrimitive.Editor\n autoFocus={autoFocus}\n className=\"lb-ai-composer-editor\"\n placeholder={$.AI_COMPOSER_PLACEHOLDER}\n defaultValue={defaultValue}\n />\n\n <div className=\"lb-ai-composer-footer\">\n <div className=\"lb-ai-composer-editor-actions\">\n {/* No actions for now but it makes sense to keep the DOM structure */}\n </div>\n\n <div className=\"lb-ai-composer-actions\">\n <AiComposerAction overrides={overrides} />\n </div>\n </div>\n </div>\n </AiComposerPrimitive.Form>\n </TooltipProvider>\n );\n }\n);\n"],"names":["AiComposerPrimitive.Abort","AiComposerPrimitive.Submit","AiComposerPrimitive.Form","AiComposerPrimitive.Editor"],"mappings":";;;;;;;;;;;;;AAkGA,SAAS,gBAAiB,CAAA;AAAA,EACxB,SAAA;AACF,CAEG,EAAA;AACD,EAAM,MAAA,EAAE,QAAS,EAAA,GAAI,aAAc,EAAA,CAAA;AACnC,EAAM,MAAA,CAAA,GAAI,aAAa,SAAS,CAAA,CAAA;AAEhC,EAAM,MAAA,cAAA,GAAiB,WAAY,CAAA,CAAC,KAA0B,KAAA;AAC5D,IAAA,KAAA,CAAM,cAAe,EAAA,CAAA;AAAA,GACvB,EAAG,EAAE,CAAA,CAAA;AAEL,EAAM,MAAA,eAAA,GAAkB,WAAY,CAAA,CAAC,KAA0B,KAAA;AAC7D,IAAA,KAAA,CAAM,eAAgB,EAAA,CAAA;AAAA,GACxB,EAAG,EAAE,CAAA,CAAA;AAEL,EAAA,OAAO,2BACJ,GAAA,CAAA,eAAA,EAAA;AAAA,IAAgB,SAAS,CAAE,CAAA,iBAAA;AAAA,IAC1B,QAAA,kB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,CAAA,KAAA,EAAA;AAAA,kBAAI,SAAU,EAAA,+BAAA;AAAA,iBAEf,CAAA;AAAA,gCAEC,GAAA,CAAA,KAAA,EAAA;AAAA,kBAAI,SAAU,EAAA,wBAAA;AAAA,kBACb,QAAC,kBAAA,GAAA,CAAA,gBAAA,EAAA;AAAA,oBAAiB,SAAA;AAAA,mBAAsB,CAAA;AAAA,iBAC1C,CAAA;AAAA,eAAA;AAAA,aACF,CAAA;AAAA,WAAA;AAAA,SACF,CAAA;AAAA,OACF,CAAA;AAAA,KACF,CAAA,CAAA;AAAA,GAEJ;AACF;;;;"}
|
package/dist/index.d.cts
CHANGED
|
@@ -457,6 +457,10 @@ interface AiComposerProps extends Omit<ComponentProps<"form">, "defaultValue"> {
|
|
|
457
457
|
* The ID of the copilot to use to send the message.
|
|
458
458
|
*/
|
|
459
459
|
copilotId?: CopilotId;
|
|
460
|
+
/**
|
|
461
|
+
* The time, in milliseconds, before an AI response will timeout.
|
|
462
|
+
*/
|
|
463
|
+
responseTimeout?: number;
|
|
460
464
|
}
|
|
461
465
|
|
|
462
466
|
type AiChatComponentsEmptyProps = {
|
|
@@ -516,6 +520,10 @@ interface AiChatProps extends ComponentProps<"div"> {
|
|
|
516
520
|
* The layout of the chat and its composer.
|
|
517
521
|
*/
|
|
518
522
|
layout?: "inset" | "compact";
|
|
523
|
+
/**
|
|
524
|
+
* The time, in milliseconds, before an AI response will timeout.
|
|
525
|
+
*/
|
|
526
|
+
responseTimeout?: number;
|
|
519
527
|
/**
|
|
520
528
|
* Override the component's strings.
|
|
521
529
|
*/
|
|
@@ -535,7 +543,7 @@ interface AiToolProps extends Omit<ComponentProps<"div">, "title" | "children">
|
|
|
535
543
|
* - `"showTodo"` → "Show todo"
|
|
536
544
|
* - `"get_weather"` → "Get weather"
|
|
537
545
|
*/
|
|
538
|
-
title?:
|
|
546
|
+
title?: ReactNode;
|
|
539
547
|
/**
|
|
540
548
|
* An optional icon displayed next to the title.
|
|
541
549
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -457,6 +457,10 @@ interface AiComposerProps extends Omit<ComponentProps<"form">, "defaultValue"> {
|
|
|
457
457
|
* The ID of the copilot to use to send the message.
|
|
458
458
|
*/
|
|
459
459
|
copilotId?: CopilotId;
|
|
460
|
+
/**
|
|
461
|
+
* The time, in milliseconds, before an AI response will timeout.
|
|
462
|
+
*/
|
|
463
|
+
responseTimeout?: number;
|
|
460
464
|
}
|
|
461
465
|
|
|
462
466
|
type AiChatComponentsEmptyProps = {
|
|
@@ -516,6 +520,10 @@ interface AiChatProps extends ComponentProps<"div"> {
|
|
|
516
520
|
* The layout of the chat and its composer.
|
|
517
521
|
*/
|
|
518
522
|
layout?: "inset" | "compact";
|
|
523
|
+
/**
|
|
524
|
+
* The time, in milliseconds, before an AI response will timeout.
|
|
525
|
+
*/
|
|
526
|
+
responseTimeout?: number;
|
|
519
527
|
/**
|
|
520
528
|
* Override the component's strings.
|
|
521
529
|
*/
|
|
@@ -535,7 +543,7 @@ interface AiToolProps extends Omit<ComponentProps<"div">, "title" | "children">
|
|
|
535
543
|
* - `"showTodo"` → "Show todo"
|
|
536
544
|
* - `"get_weather"` → "Get weather"
|
|
537
545
|
*/
|
|
538
|
-
title?:
|
|
546
|
+
title?: ReactNode;
|
|
539
547
|
/**
|
|
540
548
|
* An optional icon displayed next to the title.
|
|
541
549
|
*/
|
package/dist/version.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const PKG_NAME = "@liveblocks/react-ui";
|
|
4
|
-
const PKG_VERSION = typeof "3.8.
|
|
4
|
+
const PKG_VERSION = typeof "3.8.1" === "string" && "3.8.1";
|
|
5
5
|
const PKG_FORMAT = typeof "cjs" === "string" && "cjs";
|
|
6
6
|
|
|
7
7
|
exports.PKG_FORMAT = PKG_FORMAT;
|
package/dist/version.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.cjs","sources":["../src/version.ts"],"sourcesContent":["declare const __VERSION__: string;\ndeclare const ROLLUP_FORMAT: string;\n\nexport const PKG_NAME = \"@liveblocks/react-ui\";\nexport const PKG_VERSION = typeof __VERSION__ === \"string\" && __VERSION__;\nexport const PKG_FORMAT = typeof ROLLUP_FORMAT === \"string\" && ROLLUP_FORMAT;\n"],"names":[],"mappings":";;AAGO,MAAM,QAAW,GAAA,uBAAA;AACX,MAAA,WAAA,GAAc,OAAO,
|
|
1
|
+
{"version":3,"file":"version.cjs","sources":["../src/version.ts"],"sourcesContent":["declare const __VERSION__: string;\ndeclare const ROLLUP_FORMAT: string;\n\nexport const PKG_NAME = \"@liveblocks/react-ui\";\nexport const PKG_VERSION = typeof __VERSION__ === \"string\" && __VERSION__;\nexport const PKG_FORMAT = typeof ROLLUP_FORMAT === \"string\" && ROLLUP_FORMAT;\n"],"names":[],"mappings":";;AAGO,MAAM,QAAW,GAAA,uBAAA;AACX,MAAA,WAAA,GAAc,OAAO,OAAA,KAAgB,QAAY,IAAA,QAAA;AACjD,MAAA,UAAA,GAAa,OAAO,KAAA,KAAkB,QAAY,IAAA;;;;;;"}
|
package/dist/version.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const PKG_NAME = "@liveblocks/react-ui";
|
|
2
|
-
const PKG_VERSION = typeof "3.8.
|
|
2
|
+
const PKG_VERSION = typeof "3.8.1" === "string" && "3.8.1";
|
|
3
3
|
const PKG_FORMAT = typeof "esm" === "string" && "esm";
|
|
4
4
|
|
|
5
5
|
export { PKG_FORMAT, PKG_NAME, PKG_VERSION };
|
package/dist/version.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sources":["../src/version.ts"],"sourcesContent":["declare const __VERSION__: string;\ndeclare const ROLLUP_FORMAT: string;\n\nexport const PKG_NAME = \"@liveblocks/react-ui\";\nexport const PKG_VERSION = typeof __VERSION__ === \"string\" && __VERSION__;\nexport const PKG_FORMAT = typeof ROLLUP_FORMAT === \"string\" && ROLLUP_FORMAT;\n"],"names":[],"mappings":"AAGO,MAAM,QAAW,GAAA,uBAAA;AACX,MAAA,WAAA,GAAc,OAAO,
|
|
1
|
+
{"version":3,"file":"version.js","sources":["../src/version.ts"],"sourcesContent":["declare const __VERSION__: string;\ndeclare const ROLLUP_FORMAT: string;\n\nexport const PKG_NAME = \"@liveblocks/react-ui\";\nexport const PKG_VERSION = typeof __VERSION__ === \"string\" && __VERSION__;\nexport const PKG_FORMAT = typeof ROLLUP_FORMAT === \"string\" && ROLLUP_FORMAT;\n"],"names":[],"mappings":"AAGO,MAAM,QAAW,GAAA,uBAAA;AACX,MAAA,WAAA,GAAc,OAAO,OAAA,KAAgB,QAAY,IAAA,QAAA;AACjD,MAAA,UAAA,GAAa,OAAO,KAAA,KAAkB,QAAY,IAAA;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liveblocks/react-ui",
|
|
3
|
-
"version": "3.8.
|
|
3
|
+
"version": "3.8.1",
|
|
4
4
|
"description": "A set of React pre-built components for the Liveblocks products. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -76,9 +76,9 @@
|
|
|
76
76
|
},
|
|
77
77
|
"dependencies": {
|
|
78
78
|
"@floating-ui/react-dom": "^2.1.2",
|
|
79
|
-
"@liveblocks/client": "3.8.
|
|
80
|
-
"@liveblocks/core": "3.8.
|
|
81
|
-
"@liveblocks/react": "3.8.
|
|
79
|
+
"@liveblocks/client": "3.8.1",
|
|
80
|
+
"@liveblocks/core": "3.8.1",
|
|
81
|
+
"@liveblocks/react": "3.8.1",
|
|
82
82
|
"@radix-ui/react-dropdown-menu": "^2.1.2",
|
|
83
83
|
"@radix-ui/react-popover": "^1.1.2",
|
|
84
84
|
"@radix-ui/react-slot": "^1.1.0",
|