diff --git a/packages/examples/package.json b/packages/examples/package.json index e186fd40..0fe5a819 100644 --- a/packages/examples/package.json +++ b/packages/examples/package.json @@ -7,7 +7,9 @@ "./*": "./src/*.tsx" }, "dependencies": { + "@ai-sdk/react": "^3.0.61", "@icons-pack/react-simple-icons": "^13.8.0", + "@loremllm/transport": "^0.6.1", "@repo/elements": "workspace:*", "@xyflow/react": "^12.10.0", "ai": "^6.0.39", diff --git a/packages/examples/src/demo-chat-data.tsx b/packages/examples/src/demo-chat-data.tsx new file mode 100644 index 00000000..b1c403ac --- /dev/null +++ b/packages/examples/src/demo-chat-data.tsx @@ -0,0 +1,227 @@ +import type { + ReasoningUIPart, + SourceUrlUIPart, + TextUIPart, + ToolUIPart, + UIMessage, +} from "ai"; +import { isReasoningUIPart, isStaticToolUIPart, isTextUIPart } from "ai"; +import type { LucideIcon } from "lucide-react"; +import { + BarChartIcon, + BoxIcon, + CodeSquareIcon, + GraduationCapIcon, + NotepadTextIcon, +} from "lucide-react"; +import { nanoid } from "nanoid"; + +// Mock messages as Map of user message text -> assistant message parts +export const mockMessages = new Map([ + [ + "Can you explain how to use React hooks effectively?", + [ + { + type: "source-url", + sourceId: nanoid(), + url: "https://react.dev/reference/react", + title: "React Documentation", + }, + { + type: "source-url", + sourceId: nanoid(), + url: "https://react.dev/reference/react-dom", + title: "React DOM Documentation", + }, + { + type: "text", + text: `# React Hooks Best Practices + +React hooks are a powerful feature that let you use state and other React features without writing classes. Here are some tips for using them effectively: + +## Rules of Hooks + +1. **Only call hooks at the top level** of your component or custom hooks +2. **Don't call hooks inside loops, conditions, or nested functions** + +## Common Hooks + +- **useState**: For local component state +- **useEffect**: For side effects like data fetching +- **useContext**: For consuming context +- **useReducer**: For complex state logic +- **useCallback**: For memoizing functions +- **useMemo**: For memoizing values + +## Example of useState and useEffect + +\`\`\`jsx +function ProfilePage({ userId }) { + const [user, setUser] = useState(null); + + useEffect(() => { + // This runs after render and when userId changes + fetchUser(userId).then(userData => { + setUser(userData); + }); + }, [userId]); + + return user ? : ; +} +\`\`\` + +Would you like me to explain any specific hook in more detail?`, + }, + ], + ], + [ + "Yes, could you explain useCallback and useMemo in more detail? When should I use one over the other?", + [ + { + type: "reasoning", + text: `The user is asking for a detailed explanation of useCallback and useMemo. I should provide a clear and concise explanation of each hook's purpose and how they differ. + +The useCallback hook is used to memoize functions to prevent unnecessary re-renders of child components that receive functions as props. + +The useMemo hook is used to memoize values to avoid expensive recalculations on every render. + +Both hooks help with performance optimization, but they serve different purposes.`, + }, + { + type: "text", + text: `## useCallback vs useMemo + +Both hooks help with *performance optimization*, but they serve different purposes: + +### useCallback + +\`useCallback\` memoizes **functions** to prevent unnecessary re-renders of child components that receive functions as props. + +\`\`\`jsx +// Without useCallback - a new function is created on every render +const handleClick = () => { + console.log(count); +}; + +// With useCallback - the function is only recreated when dependencies change +const handleClick = useCallback(() => { + console.log(count); +}, [count]); +\`\`\` + +### useMemo + +\`useMemo\` memoizes **values** to avoid expensive recalculations on every render. + +\`\`\`jsx +// Without useMemo - expensive calculation runs on every render +const sortedList = expensiveSort(items); + +// With useMemo - calculation only runs when items change +const sortedList = useMemo(() => expensiveSort(items), [items]); +\`\`\` + +### When to use which? + +- Use **useCallback** when: + - Passing callbacks to optimized child components that rely on reference equality + - Working with event handlers that you pass to child components + +- Use **useMemo** when: + - You have computationally expensive calculations + - You want to avoid recreating objects that are used as dependencies for other hooks + +### Performance Note + +Don't overuse these hooks! They come with their own overhead. Only use them when you have identified a genuine performance issue. + +### ~~Deprecated Methods~~ + +Note that ~~class-based lifecycle methods~~ like \`componentDidMount\` are now replaced by the \`useEffect\` hook in modern React development.`, + }, + ], + ], +]); + +// Ordered sequence of user messages for auto-play demo. +// useDemoChat sends the first message on mount, then auto-sends +// each subsequent message after the previous response completes. +export const scriptedUserMessages = [...mockMessages.keys()]; + +// Map of message text -> all versions (for MessageBranch UI) +const messageVersionsMap = new Map([ + [ + "Yes, could you explain useCallback and useMemo in more detail? When should I use one over the other?", + [ + "Yes, could you explain useCallback and useMemo in more detail? When should I use one over the other?", + "I'm particularly interested in understanding when to use useCallback vs useMemo. Can you provide some practical examples?", + "Thanks for the overview! Could you dive deeper into the performance optimization hooks? I want to understand the tradeoffs.", + ], + ], +]); + +// Get alternative versions for a user message text +export function getMessageVersions(text: string): string[] | null { + return messageVersionsMap.get(text) ?? null; +} + +export type Suggestion = { + icon: LucideIcon | null; + text: string; + color?: string; +}; + +export const suggestions: readonly Suggestion[] = [ + { icon: BarChartIcon, text: "Analyze data", color: "#76d0eb" }, + { icon: BoxIcon, text: "Surprise me", color: "#76d0eb" }, + { icon: NotepadTextIcon, text: "Summarize text", color: "#ea8444" }, + { icon: CodeSquareIcon, text: "Code", color: "#6c71ff" }, + { icon: GraduationCapIcon, text: "Get advice", color: "#76d0eb" }, + { icon: null, text: "More" }, +]; + +export const mockResponses: readonly string[] = [ + "That's a great question! Let me help you understand this concept better. The key thing to remember is that proper implementation requires careful consideration of the underlying principles and best practices in the field.", + "I'd be happy to explain this topic in detail. From my understanding, there are several important factors to consider when approaching this problem. Let me break it down step by step for you.", + "This is an interesting topic that comes up frequently. The solution typically involves understanding the core concepts and applying them in the right context. Here's what I recommend...", + "Great choice of topic! This is something that many developers encounter. The approach I'd suggest is to start with the fundamentals and then build up to more complex scenarios.", + "That's definitely worth exploring. From what I can see, the best way to handle this is to consider both the theoretical aspects and practical implementation details.", +]; + +export function getLastUserMessageText(messages: UIMessage[]): string { + const lastUserMessage = messages.findLast((msg) => msg.role === "user"); + const textPart = lastUserMessage?.parts.find(isTextUIPart); + return textPart?.text ?? ""; +} + +// Categorized message parts with proper types +export type CategorizedParts = { + sources: SourceUrlUIPart[]; + reasoning: ReasoningUIPart[]; + tools: ToolUIPart[]; + text: TextUIPart[]; +}; + +// Categorization of message parts. We need this because of branching messages. +export function categorizeMessageParts( + parts: UIMessage["parts"], +): CategorizedParts { + const sources: SourceUrlUIPart[] = []; + const reasoning: ReasoningUIPart[] = []; + const tools: ToolUIPart[] = []; + const text: TextUIPart[] = []; + + for (const p of parts) { + if (isTextUIPart(p)) { + text.push(p); + } else if (isReasoningUIPart(p)) { + reasoning.push(p); + } else if (isStaticToolUIPart(p)) { + tools.push(p); + } else if (p.type === "source-url") { + sources.push(p); + } + } + + return { sources, reasoning, tools, text }; +} diff --git a/packages/examples/src/demo-chat-shared.tsx b/packages/examples/src/demo-chat-shared.tsx new file mode 100644 index 00000000..e58b30bf --- /dev/null +++ b/packages/examples/src/demo-chat-shared.tsx @@ -0,0 +1,81 @@ +"use client"; + +import { useChat } from "@ai-sdk/react"; +import { StaticChatTransport } from "@loremllm/transport"; +import { useEffect, useRef } from "react"; + +import { + getLastUserMessageText, + mockMessages, + mockResponses, + scriptedUserMessages, +} from "./demo-chat-data"; + +// Shared transport for all demos +export const demoTransport = new StaticChatTransport({ + chunkDelayMs: [20, 50], + async *mockResponse({ messages }) { + const lastUserMessageText = getLastUserMessageText(messages); + + // Check for predefined response + const assistantParts = mockMessages.get(lastUserMessageText); + if (assistantParts) { + for (const part of assistantParts) yield part; + return; + } + + // Fallback to random response + const randomResponse = + mockResponses[Math.floor(Math.random() * mockResponses.length)]; + + if (Math.random() > 0.5) { + yield { + type: "reasoning", + text: "Let me think about this question carefully. I need to provide a comprehensive and helpful response that addresses the user's needs while being clear and concise.", + }; + } + + yield { + type: "text", + text: randomResponse, + }; + }, +}); + +// Hook for demo chat with auto-initialization and scripted flow +export function useDemoChat(chatId: string) { + const { messages, sendMessage, setMessages, status } = useChat({ + id: chatId, + transport: demoTransport, + onFinish: ({ messages }) => { + // Auto-send next scripted message only if current was part of script + const lastText = getLastUserMessageText(messages); + const currentIndex = scriptedUserMessages.indexOf(lastText); + + // Don't auto-continue for non-scripted messages (e.g., suggestions) + if (currentIndex === -1) return; + + const nextIndex = currentIndex + 1; + if (nextIndex < scriptedUserMessages.length) { + sendMessage({ text: scriptedUserMessages[nextIndex] }); + } + }, + }); + + // Track initialization to avoid double-firing in strict mode + const initialized = useRef(false); + + useEffect(() => { + if (initialized.current) return; + initialized.current = true; + + demoTransport.clearCache(chatId); + setMessages([]); + if (scriptedUserMessages.length > 0) { + sendMessage({ text: scriptedUserMessages[0] }); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + return { messages, sendMessage, status }; +} diff --git a/packages/examples/src/demo-chatgpt.tsx b/packages/examples/src/demo-chatgpt.tsx index 02bb0ad4..eebc7c25 100644 --- a/packages/examples/src/demo-chatgpt.tsx +++ b/packages/examples/src/demo-chatgpt.tsx @@ -36,6 +36,13 @@ import { SourcesTrigger, } from "@repo/elements/sources"; import { Suggestion, Suggestions } from "@repo/elements/suggestion"; +import { + Tool, + ToolContent, + ToolHeader, + ToolInput, + ToolOutput, +} from "@repo/elements/tool"; import { DropdownMenu, DropdownMenuContent, @@ -43,503 +50,25 @@ import { DropdownMenuTrigger, } from "@repo/shadcn-ui/components/ui/dropdown-menu"; import { cn } from "@repo/shadcn-ui/lib/utils"; -import type { ToolUIPart } from "ai"; import { AudioWaveformIcon, - BarChartIcon, - BoxIcon, CameraIcon, - CodeSquareIcon, FileIcon, GlobeIcon, - GraduationCapIcon, ImageIcon, - NotepadTextIcon, PaperclipIcon, ScreenShareIcon, } from "lucide-react"; -import { nanoid } from "nanoid"; -import { useCallback, useEffect, useState } from "react"; +import { useState } from "react"; import { toast } from "sonner"; -interface MessageType { - key: string; - from: "user" | "assistant"; - sources?: { href: string; title: string }[]; - versions: { - id: string; - content: string; - }[]; - reasoning?: { - content: string; - duration: number; - }; - tools?: { - name: string; - description: string; - status: ToolUIPart["state"]; - parameters: Record; - result: string | undefined; - error: string | undefined; - }[]; - isReasoningComplete?: boolean; - isContentComplete?: boolean; - isReasoningStreaming?: boolean; -} - -const mockMessages: MessageType[] = [ - { - key: nanoid(), - from: "user", - versions: [ - { - id: nanoid(), - content: "Can you explain how to use React hooks effectively?", - }, - ], - }, - { - key: nanoid(), - from: "assistant", - sources: [ - { - href: "https://react.dev/reference/react", - title: "React Documentation", - }, - { - href: "https://react.dev/reference/react-dom", - title: "React DOM Documentation", - }, - ], - tools: [ - { - name: "mcp", - description: "Searching React documentation", - status: "input-available", - parameters: { - query: "React hooks best practices", - source: "react.dev", - }, - result: `{ - "query": "React hooks best practices", - "results": [ - { - "title": "Rules of Hooks", - "url": "https://react.dev/warnings/invalid-hook-call-warning", - "snippet": "Hooks must be called at the top level of your React function components or custom hooks. Don't call hooks inside loops, conditions, or nested functions." - }, - { - "title": "useState Hook", - "url": "https://react.dev/reference/react/useState", - "snippet": "useState is a React Hook that lets you add state to your function components. It returns an array with two values: the current state and a function to update it." - }, - { - "title": "useEffect Hook", - "url": "https://react.dev/reference/react/useEffect", - "snippet": "useEffect lets you synchronize a component with external systems. It runs after render and can be used to perform side effects like data fetching." - } - ] -}`, - error: undefined, - }, - ], - versions: [ - { - id: nanoid(), - content: `# React Hooks Best Practices - -React hooks are a powerful feature that let you use state and other React features without writing classes. Here are some tips for using them effectively: - -## Rules of Hooks - -1. **Only call hooks at the top level** of your component or custom hooks -2. **Don't call hooks inside loops, conditions, or nested functions** - -## Common Hooks - -- **useState**: For local component state -- **useEffect**: For side effects like data fetching -- **useContext**: For consuming context -- **useReducer**: For complex state logic -- **useCallback**: For memoizing functions -- **useMemo**: For memoizing values - -## Example of useState and useEffect - -\`\`\`jsx -function ProfilePage({ userId }) { - const [user, setUser] = useState(null); - - useEffect(() => { - // This runs after render and when userId changes - fetchUser(userId).then(userData => { - setUser(userData); - }); - }, [userId]); - - return user ? : ; -} -\`\`\` - -Would you like me to explain any specific hook in more detail?`, - }, - ], - }, - { - key: nanoid(), - from: "user", - versions: [ - { - id: nanoid(), - content: - "Yes, could you explain useCallback and useMemo in more detail? When should I use one over the other?", - }, - { - id: nanoid(), - content: - "I'm particularly interested in understanding the performance implications of useCallback and useMemo. Could you break down when each is most appropriate?", - }, - { - id: nanoid(), - content: - "Thanks for the overview! Could you dive deeper into the specific use cases where useCallback and useMemo make the biggest difference in React applications?", - }, - ], - }, - { - key: nanoid(), - from: "assistant", - reasoning: { - content: `The user is asking for a detailed explanation of useCallback and useMemo. I should provide a clear and concise explanation of each hook's purpose and how they differ. - -The useCallback hook is used to memoize functions to prevent unnecessary re-renders of child components that receive functions as props. - -The useMemo hook is used to memoize values to avoid expensive recalculations on every render. - -Both hooks help with performance optimization, but they serve different purposes.`, - duration: 10, - }, - versions: [ - { - id: nanoid(), - content: `## useCallback vs useMemo - -Both hooks help with *performance optimization*, but they serve different purposes: - -### useCallback - -\`useCallback\` memoizes **functions** to prevent unnecessary re-renders of child components that receive functions as props. - -\`\`\`jsx -// Without useCallback - a new function is created on every render -const handleClick = () => { - console.log(count); -}; - -// With useCallback - the function is only recreated when dependencies change -const handleClick = useCallback(() => { - console.log(count); -}, [count]); -\`\`\` - -### useMemo - -\`useMemo\` memoizes **values** to avoid expensive recalculations on every render. - -\`\`\`jsx -// Without useMemo - expensive calculation runs on every render -const sortedList = expensiveSort(items); - -// With useMemo - calculation only runs when items change -const sortedList = useMemo(() => expensiveSort(items), [items]); -\`\`\` - -### When to use which? - -- Use **useCallback** when: - - Passing callbacks to optimized child components that rely on reference equality - - Working with event handlers that you pass to child components - -- Use **useMemo** when: - - You have computationally expensive calculations - - You want to avoid recreating objects that are used as dependencies for other hooks - -### Performance Note - -Don't overuse these hooks! They come with their own overhead. Only use them when you have identified a genuine performance issue. - -### ~~Deprecated Methods~~ - -Note that ~~class-based lifecycle methods~~ like \`componentDidMount\` are now replaced by the \`useEffect\` hook in modern React development.`, - }, - ], - }, -]; - -const suggestions = [ - { icon: BarChartIcon, text: "Analyze data", color: "#76d0eb" }, - { icon: BoxIcon, text: "Surprise me", color: "#76d0eb" }, - { icon: NotepadTextIcon, text: "Summarize text", color: "#ea8444" }, - { icon: CodeSquareIcon, text: "Code", color: "#6c71ff" }, - { icon: GraduationCapIcon, text: "Get advice", color: "#76d0eb" }, - { icon: null, text: "More" }, -]; - -const mockMessageResponses = [ - "That's a great question! Let me help you understand this concept better. The key thing to remember is that proper implementation requires careful consideration of the underlying principles and best practices in the field.", - "I'd be happy to explain this topic in detail. From my understanding, there are several important factors to consider when approaching this problem. Let me break it down step by step for you.", - "This is an interesting topic that comes up frequently. The solution typically involves understanding the core concepts and applying them in the right context. Here's what I recommend...", - "Great choice of topic! This is something that many developers encounter. The approach I'd suggest is to start with the fundamentals and then build up to more complex scenarios.", - "That's definitely worth exploring. From what I can see, the best way to handle this is to consider both the theoretical aspects and practical implementation details.", -]; +import { categorizeMessageParts, getMessageVersions, suggestions } from "./demo-chat-data"; +import { useDemoChat } from "./demo-chat-shared"; const Example = () => { - const [text, setText] = useState(""); - const [useWebSearch, setUseWebSearch] = useState(false); - const [useMicrophone, setUseMicrophone] = useState(false); - const [_status, setStatus] = useState< - "submitted" | "streaming" | "ready" | "error" - >("ready"); - const [messages, setMessages] = useState([]); - const [_streamingMessageId, setStreamingMessageId] = useState( - null - ); - - const streamReasoning = async ( - messageKey: string, - _versionId: string, - reasoningContent: string - ) => { - const words = reasoningContent.split(" "); - let currentContent = ""; - - for (let i = 0; i < words.length; i++) { - currentContent += (i > 0 ? " " : "") + words[i]; - - setMessages((prev) => - prev.map((msg) => { - if (msg.key === messageKey) { - return { - ...msg, - reasoning: msg.reasoning - ? { ...msg.reasoning, content: currentContent } - : undefined, - }; - } - return msg; - }) - ); - - await new Promise((resolve) => - setTimeout(resolve, Math.random() * 30 + 20) - ); - } - - // Mark reasoning as complete - setMessages((prev) => - prev.map((msg) => { - if (msg.key === messageKey) { - return { - ...msg, - isReasoningComplete: true, - isReasoningStreaming: false, - }; - } - return msg; - }) - ); - }; - - const streamContent = async ( - messageKey: string, - versionId: string, - content: string - ) => { - const words = content.split(" "); - let currentContent = ""; - - for (let i = 0; i < words.length; i++) { - currentContent += (i > 0 ? " " : "") + words[i]; - - setMessages((prev) => - prev.map((msg) => { - if (msg.key === messageKey) { - return { - ...msg, - versions: msg.versions.map((v) => - v.id === versionId ? { ...v, content: currentContent } : v - ), - }; - } - return msg; - }) - ); - - await new Promise((resolve) => - setTimeout(resolve, Math.random() * 50 + 25) - ); - } - - // Mark content as complete - setMessages((prev) => - prev.map((msg) => { - if (msg.key === messageKey) { - return { ...msg, isContentComplete: true }; - } - return msg; - }) - ); - }; + const [text, setText] = useState(""); - // biome-ignore lint/correctness/useExhaustiveDependencies: streamContent and streamReasoning only use stable setMessages - const streamMessageResponse = useCallback( - async ( - messageKey: string, - versionId: string, - content: string, - reasoning?: { content: string; duration: number } - ) => { - setStatus("streaming"); - setStreamingMessageId(versionId); - - // First stream the reasoning if it exists - if (reasoning) { - await streamReasoning(messageKey, versionId, reasoning.content); - await new Promise((resolve) => setTimeout(resolve, 500)); // Pause between reasoning and content - } - - // Then stream the content - await streamContent(messageKey, versionId, content); - - setStatus("ready"); - setStreamingMessageId(null); - }, - [] - ); - - const streamMessage = useCallback( - async (message: MessageType) => { - if (message.from === "user") { - setMessages((prev) => [...prev, message]); - return; - } - - // Add empty assistant message with reasoning structure - const newMessage = { - ...message, - versions: message.versions.map((v) => ({ ...v, content: "" })), - reasoning: message.reasoning - ? { ...message.reasoning, content: "" } - : undefined, - isReasoningComplete: false, - isContentComplete: false, - isReasoningStreaming: !!message.reasoning, - }; - - setMessages((prev) => [...prev, newMessage]); - - // Get the first version for streaming - const firstVersion = message.versions[0]; - if (!firstVersion) { - return; - } - - // Stream the response - await streamMessageResponse( - newMessage.key, - firstVersion.id, - firstVersion.content, - message.reasoning - ); - }, - [streamMessageResponse] - ); - - const addUserMessage = useCallback( - (content: string) => { - const userMessage: MessageType = { - key: `user-${Date.now()}`, - from: "user", - versions: [ - { - id: `user-${Date.now()}`, - content, - }, - ], - }; - - setMessages((prev) => [...prev, userMessage]); - - setTimeout(() => { - const assistantMessageKey = `assistant-${Date.now()}`; - const assistantMessageId = `version-${Date.now()}`; - const randomMessageResponse = - mockMessageResponses[ - Math.floor(Math.random() * mockMessageResponses.length) - ]; - - // Create reasoning for some responses - const shouldHaveReasoning = Math.random() > 0.5; - const reasoning = shouldHaveReasoning - ? { - content: - "Let me think about this question carefully. I need to provide a comprehensive and helpful response that addresses the user's needs while being clear and concise.", - duration: 3, - } - : undefined; - - const assistantMessage: MessageType = { - key: assistantMessageKey, - from: "assistant", - versions: [ - { - id: assistantMessageId, - content: "", - }, - ], - reasoning: reasoning ? { ...reasoning, content: "" } : undefined, - isReasoningComplete: false, - isContentComplete: false, - isReasoningStreaming: !!reasoning, - }; - - setMessages((prev) => [...prev, assistantMessage]); - streamMessageResponse( - assistantMessageKey, - assistantMessageId, - randomMessageResponse, - reasoning - ); - }, 500); - }, - [streamMessageResponse] - ); - - useEffect(() => { - // Reset state on mount to ensure fresh component - setMessages([]); - - const processMessages = async () => { - for (let i = 0; i < mockMessages.length; i++) { - await streamMessage(mockMessages[i]); - - if (i < mockMessages.length - 1) { - await new Promise((resolve) => setTimeout(resolve, 1000)); - } - } - }; - - // Small delay to ensure state is reset before starting - const timer = setTimeout(() => { - processMessages(); - }, 100); - - // Cleanup function to cancel any ongoing operations - return () => { - clearTimeout(timer); - setMessages([]); - }; - }, [streamMessage]); + const { messages, sendMessage } = useDemoChat("demo-chatgpt"); const handleSubmit = (message: PromptInputMessage) => { const hasText = Boolean(message.text); @@ -549,8 +78,7 @@ const Example = () => { return; } - setStatus("submitted"); - addUserMessage(message.text || "Sent with attachments"); + sendMessage({ text: message.text || "Sent with attachments" }); setText(""); }; @@ -561,73 +89,101 @@ const Example = () => { }; const handleSuggestionClick = (suggestion: string) => { - setStatus("submitted"); - addUserMessage(suggestion); + sendMessage({ text: suggestion }); }; return (
- {messages.map(({ versions, ...message }) => ( - - - {versions.map((version) => ( - -
- {message.sources?.length && ( - - - - {message.sources.map((source) => ( - - ))} - - - )} - {message.reasoning && ( - - - - {message.reasoning.content} - - - )} - {(message.from === "user" || - message.isReasoningComplete || - !message.reasoning) && ( - - {version.content} - + {messages.map((message) => { + const { sources, reasoning, tools, text } = categorizeMessageParts(message.parts); + const messageText = text[0]?.text ?? ""; + + // Check if user message has alternative versions + if (message.role === "user") { + const versions = getMessageVersions(messageText); + if (versions) { + return ( + + + {versions.map((version, i) => ( + + + {version} + + + ))} + + + + + + + + ); + } + } + + return ( + +
+ {sources.length > 0 && ( + + + + {sources.map((source, i) => ( + + ))} + + + )} + {tools.map((tool, i) => ( + + + + + + + + ))} + {reasoning.map((part, i) => ( + + + {part.text} + + ))} + {text.map((part, i) => ( + - - ))} - - {versions.length > 1 && ( - - - - - - )} - - ))} + key={`${message.id}-text-${i}`} + > + {part.text} + + ))} +
+
+ ); + })} @@ -683,7 +239,6 @@ const Example = () => { setUseWebSearch(!useWebSearch)} variant="outline" > @@ -692,7 +247,6 @@ const Example = () => { setUseMicrophone(!useMicrophone)} variant="secondary" > diff --git a/packages/examples/src/demo-claude.tsx b/packages/examples/src/demo-claude.tsx index de3b1f41..e6961cf6 100644 --- a/packages/examples/src/demo-claude.tsx +++ b/packages/examples/src/demo-claude.tsx @@ -49,6 +49,13 @@ import { SourcesContent, SourcesTrigger, } from "@repo/elements/sources"; +import { + Tool, + ToolContent, + ToolHeader, + ToolInput, + ToolOutput, +} from "@repo/elements/tool"; import { DropdownMenu, DropdownMenuContent, @@ -56,7 +63,6 @@ import { DropdownMenuTrigger, } from "@repo/shadcn-ui/components/ui/dropdown-menu"; import { cn } from "@repo/shadcn-ui/lib/utils"; -import type { ToolUIPart } from "ai"; import { ArrowUpIcon, CameraIcon, @@ -67,225 +73,11 @@ import { ScreenShareIcon, Settings2Icon, } from "lucide-react"; -import { nanoid } from "nanoid"; -import { useCallback, useEffect, useState } from "react"; +import { useState } from "react"; import { toast } from "sonner"; -interface MessageType { - key: string; - from: "user" | "assistant"; - sources?: { href: string; title: string }[]; - versions: { - id: string; - content: string; - }[]; - reasoning?: { - content: string; - duration: number; - }; - tools?: { - name: string; - description: string; - status: ToolUIPart["state"]; - parameters: Record; - result: string | undefined; - error: string | undefined; - }[]; - isReasoningComplete?: boolean; - isContentComplete?: boolean; - isReasoningStreaming?: boolean; -} - -const mockMessages: MessageType[] = [ - { - key: nanoid(), - from: "user", - versions: [ - { - id: nanoid(), - content: "Can you explain how to use React hooks effectively?", - }, - ], - }, - { - key: nanoid(), - from: "assistant", - sources: [ - { - href: "https://react.dev/reference/react", - title: "React Documentation", - }, - { - href: "https://react.dev/reference/react-dom", - title: "React DOM Documentation", - }, - ], - tools: [ - { - name: "mcp", - description: "Searching React documentation", - status: "input-available", - parameters: { - query: "React hooks best practices", - source: "react.dev", - }, - result: `{ - "query": "React hooks best practices", - "results": [ - { - "title": "Rules of Hooks", - "url": "https://react.dev/warnings/invalid-hook-call-warning", - "snippet": "Hooks must be called at the top level of your React function components or custom hooks. Don't call hooks inside loops, conditions, or nested functions." - }, - { - "title": "useState Hook", - "url": "https://react.dev/reference/react/useState", - "snippet": "useState is a React Hook that lets you add state to your function components. It returns an array with two values: the current state and a function to update it." - }, - { - "title": "useEffect Hook", - "url": "https://react.dev/reference/react/useEffect", - "snippet": "useEffect lets you synchronize a component with external systems. It runs after render and can be used to perform side effects like data fetching." - } - ] -}`, - error: undefined, - }, - ], - versions: [ - { - id: nanoid(), - content: `# React Hooks Best Practices - -React hooks are a powerful feature that let you use state and other React features without writing classes. Here are some tips for using them effectively: - -## Rules of Hooks - -1. **Only call hooks at the top level** of your component or custom hooks -2. **Don't call hooks inside loops, conditions, or nested functions** - -## Common Hooks - -- **useState**: For local component state -- **useEffect**: For side effects like data fetching -- **useContext**: For consuming context -- **useReducer**: For complex state logic -- **useCallback**: For memoizing functions -- **useMemo**: For memoizing values - -## Example of useState and useEffect - -\`\`\`jsx -function ProfilePage({ userId }) { - const [user, setUser] = useState(null); - - useEffect(() => { - // This runs after render and when userId changes - fetchUser(userId).then(userData => { - setUser(userData); - }); - }, [userId]); - - return user ? : ; -} -\`\`\` - -Would you like me to explain any specific hook in more detail?`, - }, - ], - }, - { - key: nanoid(), - from: "user", - versions: [ - { - id: nanoid(), - content: - "Yes, could you explain useCallback and useMemo in more detail? When should I use one over the other?", - }, - { - id: nanoid(), - content: - "I'm particularly interested in understanding the performance implications of useCallback and useMemo. Could you break down when each is most appropriate?", - }, - { - id: nanoid(), - content: - "Thanks for the overview! Could you dive deeper into the specific use cases where useCallback and useMemo make the biggest difference in React applications?", - }, - ], - }, - { - key: nanoid(), - from: "assistant", - reasoning: { - content: `The user is asking for a detailed explanation of useCallback and useMemo. I should provide a clear and concise explanation of each hook's purpose and how they differ. - -The useCallback hook is used to memoize functions to prevent unnecessary re-renders of child components that receive functions as props. - -The useMemo hook is used to memoize values to avoid expensive recalculations on every render. - -Both hooks help with performance optimization, but they serve different purposes.`, - duration: 10, - }, - versions: [ - { - id: nanoid(), - content: `## useCallback vs useMemo - -Both hooks help with _performance optimization_, but they serve different purposes: - -### useCallback - -\`useCallback\` memoizes **functions** to prevent unnecessary re-renders of child components that receive functions as props. - -\`\`\`jsx -// Without useCallback - a new function is created on every render -const handleClick = () => { - console.log(count); -}; - -// With useCallback - the function is only recreated when dependencies change -const handleClick = useCallback(() => { - console.log(count); -}, [count]); -\`\`\` - -### useMemo - -\`useMemo\` memoizes __values__ to avoid expensive recalculations on every render. - -\`\`\`jsx -// Without useMemo - expensive calculation runs on every render -const sortedList = expensiveSort(items); - -// With useMemo - calculation only runs when items change -const sortedList = useMemo(() => expensiveSort(items), [items]); -\`\`\` - -### When to use which? - -- Use **useCallback** when: - - Passing callbacks to optimized child components that rely on reference equality - - Working with event handlers that you pass to child components - -- Use **useMemo** when: - - You have computationally expensive calculations - - You want to avoid recreating objects that are used as dependencies for other hooks - -### Performance Note - -Don't overuse these hooks! They come with their own overhead. Only use them when you have identified a genuine performance issue. - -### ~~Common Mistakes~~ - -Avoid these ~~anti-patterns~~ when using hooks: -- ~~Calling hooks conditionally~~ - Always call hooks at the top level -- Using \`useEffect\` without proper dependency arrays`, - }, - ], - }, -]; +import { categorizeMessageParts, getMessageVersions } from "./demo-chat-data"; +import { useDemoChat } from "./demo-chat-shared"; const models = [ { @@ -311,266 +103,14 @@ const models = [ }, ]; -const mockMessageResponses = [ - "That's a great question! Let me help you understand this concept better. The key thing to remember is that proper implementation requires careful consideration of the underlying principles and best practices in the field.", - "I'd be happy to explain this topic in detail. From my understanding, there are several important factors to consider when approaching this problem. Let me break it down step by step for you.", - "This is an interesting topic that comes up frequently. The solution typically involves understanding the core concepts and applying them in the right context. Here's what I recommend...", - "Great choice of topic! This is something that many developers encounter. The approach I'd suggest is to start with the fundamentals and then build up to more complex scenarios.", - "That's definitely worth exploring. From what I can see, the best way to handle this is to consider both the theoretical aspects and practical implementation details.", -]; - const Example = () => { - const [model, setModel] = useState(models[0].id); + const [model, setModel] = useState(models[0].id); const [modelSelectorOpen, setModelSelectorOpen] = useState(false); - const [text, setText] = useState(""); - const [_useWebSearch, _setUseWebSearch] = useState(false); - const [_useMicrophone, _setUseMicrophone] = useState(false); - const [status, setStatus] = useState< - "submitted" | "streaming" | "ready" | "error" - >("ready"); - const [messages, setMessages] = useState([]); - const [_streamingMessageId, setStreamingMessageId] = useState( - null - ); + const [text, setText] = useState(""); + const { messages, sendMessage, status } = useDemoChat("demo-claude"); const selectedModelData = models.find((m) => m.id === model); - const streamReasoning = async ( - messageKey: string, - _versionId: string, - reasoningContent: string - ) => { - const words = reasoningContent.split(" "); - let currentContent = ""; - - for (let i = 0; i < words.length; i++) { - currentContent += (i > 0 ? " " : "") + words[i]; - - setMessages((prev) => - prev.map((msg) => { - if (msg.key === messageKey) { - return { - ...msg, - reasoning: msg.reasoning - ? { ...msg.reasoning, content: currentContent } - : undefined, - }; - } - return msg; - }) - ); - - await new Promise((resolve) => - setTimeout(resolve, Math.random() * 30 + 20) - ); - } - - // Mark reasoning as complete - setMessages((prev) => - prev.map((msg) => { - if (msg.key === messageKey) { - return { - ...msg, - isReasoningComplete: true, - isReasoningStreaming: false, - }; - } - return msg; - }) - ); - }; - - const streamContent = async ( - messageKey: string, - versionId: string, - content: string - ) => { - const words = content.split(" "); - let currentContent = ""; - - for (let i = 0; i < words.length; i++) { - currentContent += (i > 0 ? " " : "") + words[i]; - - setMessages((prev) => - prev.map((msg) => { - if (msg.key === messageKey) { - return { - ...msg, - versions: msg.versions.map((v) => - v.id === versionId ? { ...v, content: currentContent } : v - ), - }; - } - return msg; - }) - ); - - await new Promise((resolve) => - setTimeout(resolve, Math.random() * 50 + 25) - ); - } - - // Mark content as complete - setMessages((prev) => - prev.map((msg) => { - if (msg.key === messageKey) { - return { ...msg, isContentComplete: true }; - } - return msg; - }) - ); - }; - - // biome-ignore lint/correctness/useExhaustiveDependencies: streamContent and streamReasoning only use stable setMessages - const streamMessageResponse = useCallback( - async ( - messageKey: string, - versionId: string, - content: string, - reasoning?: { content: string; duration: number } - ) => { - setStatus("streaming"); - setStreamingMessageId(versionId); - - // First stream the reasoning if it exists - if (reasoning) { - await streamReasoning(messageKey, versionId, reasoning.content); - await new Promise((resolve) => setTimeout(resolve, 500)); // Pause between reasoning and content - } - - // Then stream the content - await streamContent(messageKey, versionId, content); - - setStatus("ready"); - setStreamingMessageId(null); - }, - [] - ); - - const streamMessage = useCallback( - async (message: MessageType) => { - if (message.from === "user") { - setMessages((prev) => [...prev, message]); - return; - } - - // Add empty assistant message with reasoning structure - const newMessage = { - ...message, - versions: message.versions.map((v) => ({ ...v, content: "" })), - reasoning: message.reasoning - ? { ...message.reasoning, content: "" } - : undefined, - isReasoningComplete: false, - isContentComplete: false, - isReasoningStreaming: !!message.reasoning, - }; - - setMessages((prev) => [...prev, newMessage]); - - // Get the first version for streaming - const firstVersion = message.versions[0]; - if (!firstVersion) { - return; - } - - // Stream the response - await streamMessageResponse( - newMessage.key, - firstVersion.id, - firstVersion.content, - message.reasoning - ); - }, - [streamMessageResponse] - ); - - const addUserMessage = useCallback( - (content: string) => { - const userMessage: MessageType = { - key: `user-${Date.now()}`, - from: "user", - versions: [ - { - id: `user-${Date.now()}`, - content, - }, - ], - }; - - setMessages((prev) => [...prev, userMessage]); - - setTimeout(() => { - const assistantMessageKey = `assistant-${Date.now()}`; - const assistantMessageId = `version-${Date.now()}`; - const randomMessageResponse = - mockMessageResponses[ - Math.floor(Math.random() * mockMessageResponses.length) - ]; - - // Create reasoning for some responses - const shouldHaveReasoning = Math.random() > 0.5; - const reasoning = shouldHaveReasoning - ? { - content: - "Let me think about this question carefully. I need to provide a comprehensive and helpful response that addresses the user's needs while being clear and concise.", - duration: 3, - } - : undefined; - - const assistantMessage: MessageType = { - key: assistantMessageKey, - from: "assistant", - versions: [ - { - id: assistantMessageId, - content: "", - }, - ], - reasoning: reasoning ? { ...reasoning, content: "" } : undefined, - isReasoningComplete: false, - isContentComplete: false, - isReasoningStreaming: !!reasoning, - }; - - setMessages((prev) => [...prev, assistantMessage]); - streamMessageResponse( - assistantMessageKey, - assistantMessageId, - randomMessageResponse, - reasoning - ); - }, 500); - }, - [streamMessageResponse] - ); - - useEffect(() => { - // Reset state on mount to ensure fresh component - setMessages([]); - - const processMessages = async () => { - for (let i = 0; i < mockMessages.length; i++) { - await streamMessage(mockMessages[i]); - - if (i < mockMessages.length - 1) { - await new Promise((resolve) => setTimeout(resolve, 1000)); - } - } - }; - - // Small delay to ensure state is reset before starting - const timer = setTimeout(() => { - processMessages(); - }, 100); - - // Cleanup function to cancel any ongoing operations - return () => { - clearTimeout(timer); - setMessages([]); - }; - }, [streamMessage]); - const handleSubmit = (message: PromptInputMessage) => { const hasText = Boolean(message.text); const hasAttachments = Boolean(message.files?.length); @@ -579,8 +119,7 @@ const Example = () => { return; } - setStatus("submitted"); - addUserMessage(message.text || "Sent with attachments"); + sendMessage({ text: message.text || "Sent with attachments" }); setText(""); }; @@ -590,75 +129,106 @@ const Example = () => { }); }; - const _handleSuggestionClick = (suggestion: string) => { - setStatus("submitted"); - addUserMessage(suggestion); - }; - return (
- {messages.map(({ versions, ...message }) => ( - - - {versions.map((version) => ( - -
- {message.sources?.length && ( - - - - {message.sources.map((source) => ( - - ))} - - - )} - {message.reasoning && ( - { + const { sources, reasoning, tools, text } = categorizeMessageParts(message.parts); + const messageText = text[0]?.text ?? ""; + + // Check if user message has alternative versions + if (message.role === "user") { + const versions = getMessageVersions(messageText); + if (versions) { + return ( + + + {versions.map((version, i) => ( + - - - {message.reasoning.content} - - - )} - {(message.from === "user" || - message.isReasoningComplete || - !message.reasoning) && ( - - {version.content} - + + {version} + + + ))} + + + + + + + + ); + } + } + + return ( + +
+ {sources.length > 0 && ( + + + + {sources.map((source, i) => ( + + ))} + + + )} + {tools.map((tool, i) => ( + + + + + + + + ))} + {reasoning.map((part, i) => ( + + + {part.text} + + ))} + {text.map((part, i) => ( + - - ))} - - {versions.length > 1 && ( - - - - - - )} - - ))} + key={`${message.id}-text-${i}`} + > + {part.text} + + ))} +
+
+ ); + })} diff --git a/packages/examples/src/demo-grok.tsx b/packages/examples/src/demo-grok.tsx index aad1b8bc..503c3961 100644 --- a/packages/examples/src/demo-grok.tsx +++ b/packages/examples/src/demo-grok.tsx @@ -48,6 +48,13 @@ import { SourcesContent, SourcesTrigger, } from "@repo/elements/sources"; +import { + Tool, + ToolContent, + ToolHeader, + ToolInput, + ToolOutput, +} from "@repo/elements/tool"; import { DropdownMenu, DropdownMenuContent, @@ -55,7 +62,6 @@ import { DropdownMenuTrigger, } from "@repo/shadcn-ui/components/ui/dropdown-menu"; import { cn } from "@repo/shadcn-ui/lib/utils"; -import type { ToolUIPart } from "ai"; import { AudioWaveformIcon, CameraIcon, @@ -68,34 +74,11 @@ import { ScreenShareIcon, SearchIcon, } from "lucide-react"; -import { nanoid } from "nanoid"; -import { useCallback, useEffect, useState } from "react"; +import { useState } from "react"; import { toast } from "sonner"; -interface MessageType { - key: string; - from: "user" | "assistant"; - sources?: { href: string; title: string }[]; - versions: { - id: string; - content: string; - }[]; - reasoning?: { - content: string; - duration: number; - }; - tools?: { - name: string; - description: string; - status: ToolUIPart["state"]; - parameters: Record; - result: string | undefined; - error: string | undefined; - }[]; - isReasoningComplete?: boolean; - isContentComplete?: boolean; - isReasoningStreaming?: boolean; -} +import { categorizeMessageParts, getMessageVersions } from "./demo-chat-data"; +import { useDemoChat } from "./demo-chat-shared"; const models = [ { @@ -114,457 +97,14 @@ const models = [ }, ]; -const mockMessages: MessageType[] = [ - { - key: nanoid(), - from: "user", - versions: [ - { - id: nanoid(), - content: "Can you explain how to use React hooks effectively?", - }, - ], - }, - { - key: nanoid(), - from: "assistant", - sources: [ - { - href: "https://react.dev/reference/react", - title: "React Documentation", - }, - { - href: "https://react.dev/reference/react-dom", - title: "React DOM Documentation", - }, - ], - tools: [ - { - name: "mcp", - description: "Searching React documentation", - status: "input-available", - parameters: { - query: "React hooks best practices", - source: "react.dev", - }, - result: `{ - "query": "React hooks best practices", - "results": [ - { - "title": "Rules of Hooks", - "url": "https://react.dev/warnings/invalid-hook-call-warning", - "snippet": "Hooks must be called at the top level of your React function components or custom hooks. Don't call hooks inside loops, conditions, or nested functions." - }, - { - "title": "useState Hook", - "url": "https://react.dev/reference/react/useState", - "snippet": "useState is a React Hook that lets you add state to your function components. It returns an array with two values: the current state and a function to update it." - }, - { - "title": "useEffect Hook", - "url": "https://react.dev/reference/react/useEffect", - "snippet": "useEffect lets you synchronize a component with external systems. It runs after render and can be used to perform side effects like data fetching." - } - ] -}`, - error: undefined, - }, - ], - versions: [ - { - id: nanoid(), - content: `# React Hooks Best Practices - -React hooks are a powerful feature that let you use state and other React features without writing classes. Here are some tips for using them effectively: - -## Rules of Hooks - -1. **Only call hooks at the top level** of your component or custom hooks -2. **Don't call hooks inside loops, conditions, or nested functions** - -## Common Hooks - -- **useState**: For local component state -- **useEffect**: For side effects like data fetching -- **useContext**: For consuming context -- **useReducer**: For complex state logic -- **useCallback**: For memoizing functions -- **useMemo**: For memoizing values - -## Example of useState and useEffect - -\`\`\`jsx -function ProfilePage({ userId }) { - const [user, setUser] = useState(null); - - useEffect(() => { - // This runs after render and when userId changes - fetchUser(userId).then(userData => { - setUser(userData); - }); - }, [userId]); - - return user ? : ; -} -\`\`\` - -Would you like me to explain any specific hook in more detail?`, - }, - ], - }, - { - key: nanoid(), - from: "user", - versions: [ - { - id: nanoid(), - content: - "Yes, could you explain useCallback and useMemo in more detail? When should I use one over the other?", - }, - { - id: nanoid(), - content: - "I'm particularly interested in understanding the performance implications of useCallback and useMemo. Could you break down when each is most appropriate?", - }, - { - id: nanoid(), - content: - "Thanks for the overview! Could you dive deeper into the specific use cases where useCallback and useMemo make the biggest difference in React applications?", - }, - ], - }, - { - key: nanoid(), - from: "assistant", - reasoning: { - content: `The user is asking for a detailed explanation of useCallback and useMemo. I should provide a clear and concise explanation of each hook's purpose and how they differ. - -The useCallback hook is used to memoize functions to prevent unnecessary re-renders of child components that receive functions as props. - -The useMemo hook is used to memoize values to avoid expensive recalculations on every render. - -Both hooks help with performance optimization, but they serve different purposes.`, - duration: 10, - }, - versions: [ - { - id: nanoid(), - content: `## useCallback vs useMemo - -Both hooks help with **performance optimization**, but they serve _different purposes_: - -### useCallback - -\`useCallback\` memoizes __functions__ to prevent unnecessary re-renders of child components that receive functions as props. - -\`\`\`jsx -// Without useCallback - a new function is created on every render -const handleClick = () => { - console.log(count); -}; - -// With useCallback - the function is only recreated when dependencies change -const handleClick = useCallback(() => { - console.log(count); -}, [count]); -\`\`\` - -### useMemo - -\`useMemo\` memoizes *values* to avoid expensive recalculations on every render. - -\`\`\`jsx -// Without useMemo - expensive calculation runs on every render -const sortedList = expensiveSort(items); - -// With useMemo - calculation only runs when items change -const sortedList = useMemo(() => expensiveSort(items), [items]); -\`\`\` - -### When to use which? - -- Use **useCallback** when: - - Passing callbacks to optimized child components that rely on reference equality - - Working with event handlers that you pass to child components - -- Use **useMemo** when: - - You have computationally expensive calculations - - You want to avoid recreating objects that are used as dependencies for other hooks - -### Performance Note - -Don't overuse these hooks! They come with their own overhead. Only use them when you have identified a genuine performance issue. - -### ~~Legacy Patterns~~ - -Remember that these ~~outdated approaches~~ should be avoided: -- ~~Class components for simple state~~ - Use \`useState\` instead -- ~~Manual event listener cleanup~~ - Let \`useEffect\` handle it`, - }, - ], - }, -]; - -const mockMessageResponses = [ - "That's a great question! Let me help you understand this concept better. The key thing to remember is that proper implementation requires careful consideration of the underlying principles and best practices in the field.", - "I'd be happy to explain this topic in detail. From my understanding, there are several important factors to consider when approaching this problem. Let me break it down step by step for you.", - "This is an interesting topic that comes up frequently. The solution typically involves understanding the core concepts and applying them in the right context. Here's what I recommend...", - "Great choice of topic! This is something that many developers encounter. The approach I'd suggest is to start with the fundamentals and then build up to more complex scenarios.", - "That's definitely worth exploring. From what I can see, the best way to handle this is to consider both the theoretical aspects and practical implementation details.", -]; - const Example = () => { - const [model, setModel] = useState(models[0].id); + const [model, setModel] = useState(models[0].id); const [modelSelectorOpen, setModelSelectorOpen] = useState(false); - const [text, setText] = useState(""); - const [useWebSearch, setUseWebSearch] = useState(false); - const [useMicrophone, setUseMicrophone] = useState(false); - const [_status, setStatus] = useState< - "submitted" | "streaming" | "ready" | "error" - >("ready"); - const [messages, setMessages] = useState([]); - const [_streamingMessageId, setStreamingMessageId] = useState( - null - ); + const [text, setText] = useState(""); + const { messages, sendMessage } = useDemoChat("demo-grok"); const selectedModelData = models.find((m) => m.id === model); - const streamReasoning = async ( - messageKey: string, - _versionId: string, - reasoningContent: string - ) => { - const words = reasoningContent.split(" "); - let currentContent = ""; - - for (let i = 0; i < words.length; i++) { - currentContent += (i > 0 ? " " : "") + words[i]; - - setMessages((prev) => - prev.map((msg) => { - if (msg.key === messageKey) { - return { - ...msg, - reasoning: msg.reasoning - ? { ...msg.reasoning, content: currentContent } - : undefined, - }; - } - return msg; - }) - ); - - await new Promise((resolve) => - setTimeout(resolve, Math.random() * 30 + 20) - ); - } - - // Mark reasoning as complete - setMessages((prev) => - prev.map((msg) => { - if (msg.key === messageKey) { - return { - ...msg, - isReasoningComplete: true, - isReasoningStreaming: false, - }; - } - return msg; - }) - ); - }; - - const streamContent = async ( - messageKey: string, - versionId: string, - content: string - ) => { - const words = content.split(" "); - let currentContent = ""; - - for (let i = 0; i < words.length; i++) { - currentContent += (i > 0 ? " " : "") + words[i]; - - setMessages((prev) => - prev.map((msg) => { - if (msg.key === messageKey) { - return { - ...msg, - versions: msg.versions.map((v) => - v.id === versionId ? { ...v, content: currentContent } : v - ), - }; - } - return msg; - }) - ); - - await new Promise((resolve) => - setTimeout(resolve, Math.random() * 50 + 25) - ); - } - - // Mark content as complete - setMessages((prev) => - prev.map((msg) => { - if (msg.key === messageKey) { - return { ...msg, isContentComplete: true }; - } - return msg; - }) - ); - }; - - // biome-ignore lint/correctness/useExhaustiveDependencies: streamContent and streamReasoning only use stable setMessages - const streamMessageResponse = useCallback( - async ( - messageKey: string, - versionId: string, - content: string, - reasoning?: { content: string; duration: number } - ) => { - setStatus("streaming"); - setStreamingMessageId(versionId); - - // First stream the reasoning if it exists - if (reasoning) { - await streamReasoning(messageKey, versionId, reasoning.content); - await new Promise((resolve) => setTimeout(resolve, 500)); // Pause between reasoning and content - } - - // Then stream the content - await streamContent(messageKey, versionId, content); - - setStatus("ready"); - setStreamingMessageId(null); - }, - [] - ); - - const streamMessage = useCallback( - async (message: MessageType) => { - if (message.from === "user") { - setMessages((prev) => [...prev, message]); - return; - } - - // Add empty assistant message with reasoning structure - const newMessage = { - ...message, - versions: message.versions.map((v) => ({ ...v, content: "" })), - reasoning: message.reasoning - ? { ...message.reasoning, content: "" } - : undefined, - isReasoningComplete: false, - isContentComplete: false, - isReasoningStreaming: !!message.reasoning, - }; - - setMessages((prev) => [...prev, newMessage]); - - // Get the first version for streaming - const firstVersion = message.versions[0]; - if (!firstVersion) { - return; - } - - // Stream the response - await streamMessageResponse( - newMessage.key, - firstVersion.id, - firstVersion.content, - message.reasoning - ); - }, - [streamMessageResponse] - ); - - const addUserMessage = useCallback( - (content: string) => { - const userMessage: MessageType = { - key: `user-${Date.now()}`, - from: "user", - versions: [ - { - id: `user-${Date.now()}`, - content, - }, - ], - }; - - setMessages((prev) => [...prev, userMessage]); - - setTimeout(() => { - const assistantMessageKey = `assistant-${Date.now()}`; - const assistantMessageId = `version-${Date.now()}`; - const randomMessageResponse = - mockMessageResponses[ - Math.floor(Math.random() * mockMessageResponses.length) - ]; - - // Create reasoning for some responses - const shouldHaveReasoning = Math.random() > 0.5; - const reasoning = shouldHaveReasoning - ? { - content: - "Let me think about this question carefully. I need to provide a comprehensive and helpful response that addresses the user's needs while being clear and concise.", - duration: 3, - } - : undefined; - - const assistantMessage: MessageType = { - key: assistantMessageKey, - from: "assistant", - versions: [ - { - id: assistantMessageId, - content: "", - }, - ], - reasoning: reasoning ? { ...reasoning, content: "" } : undefined, - isReasoningComplete: false, - isContentComplete: false, - isReasoningStreaming: !!reasoning, - }; - - setMessages((prev) => [...prev, assistantMessage]); - streamMessageResponse( - assistantMessageKey, - assistantMessageId, - randomMessageResponse, - reasoning - ); - }, 500); - }, - [streamMessageResponse] - ); - - useEffect(() => { - // Reset state on mount to ensure fresh component - setMessages([]); - - const processMessages = async () => { - for (let i = 0; i < mockMessages.length; i++) { - await streamMessage(mockMessages[i]); - - if (i < mockMessages.length - 1) { - await new Promise((resolve) => setTimeout(resolve, 1000)); - } - } - }; - - // Small delay to ensure state is reset before starting - const timer = setTimeout(() => { - processMessages(); - }, 100); - - // Cleanup function to cancel any ongoing operations - return () => { - clearTimeout(timer); - setMessages([]); - }; - }, [streamMessage]); - const handleSubmit = (message: PromptInputMessage) => { const hasText = Boolean(message.text); const hasAttachments = Boolean(message.files?.length); @@ -573,8 +113,7 @@ const Example = () => { return; } - setStatus("submitted"); - addUserMessage(message.text || "Sent with attachments"); + sendMessage({ text: message.text || "Sent with attachments" }); setText(""); }; @@ -584,74 +123,98 @@ const Example = () => { }); }; - const _handleSuggestionClick = (suggestion: string) => { - setStatus("submitted"); - addUserMessage(suggestion); - }; - return (
- {messages.map(({ versions, ...message }) => ( - - - {versions.map((version) => ( - -
- {message.sources?.length && ( - - - - {message.sources.map((source) => ( - - ))} - - - )} - {message.reasoning && ( - - - - {message.reasoning.content} - - - )} - {(message.from === "user" || - message.isReasoningComplete || - !message.reasoning) && ( - - {version.content} - + {messages.map((message) => { + const { sources, reasoning, tools, text } = categorizeMessageParts(message.parts); + const messageText = text[0]?.text ?? ""; + + // Check if user message has alternative versions + if (message.role === "user") { + const versions = getMessageVersions(messageText); + if (versions) { + return ( + + + {versions.map((version, i) => ( + + + {version} + + + ))} + + + + + + + + ); + } + } + + return ( + +
+ {sources.length > 0 && ( + + + + {sources.map((source, i) => ( + + ))} + + + )} + {tools.map((tool, i) => ( + + + + + + + + ))} + {reasoning.map((part, i) => ( + + + {part.text} + + ))} + {text.map((part, i) => ( + - - ))} - - {versions.length > 1 && ( - - - - - - )} - - ))} + key={`${message.id}-text-${i}`} + > + {part.text} + + ))} +
+
+ ); + })} @@ -708,7 +271,6 @@ const Example = () => {
setUseWebSearch(!useWebSearch)} variant="ghost" > @@ -787,7 +349,6 @@ const Example = () => { setUseMicrophone(!useMicrophone)} variant="default" > diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e94cb966..9d0db9a7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -300,9 +300,15 @@ importers: packages/examples: dependencies: + '@ai-sdk/react': + specifier: ^3.0.61 + version: 3.0.61(react@19.2.3)(zod@4.3.5) '@icons-pack/react-simple-icons': specifier: ^13.8.0 version: 13.8.0(react@19.2.3) + '@loremllm/transport': + specifier: ^0.6.1 + version: 0.6.1(ai@6.0.39(zod@4.3.5)) '@repo/elements': specifier: workspace:* version: link:../elements @@ -449,6 +455,18 @@ packages: peerDependencies: zod: ^3.25.76 || ^4.1.8 + '@ai-sdk/gateway@3.0.27': + resolution: {integrity: sha512-Pr+ApS9k6/jcR3kNltJNxo60OdYvnVU4DeRhzVtxUAYTXCHx4qO+qTMG9nNRn+El1acJnNRA//Su47srjXkT/w==} + engines: {node: '>=18'} + peerDependencies: + zod: ^3.25.76 || ^4.1.8 + + '@ai-sdk/provider-utils@4.0.10': + resolution: {integrity: sha512-VeDAiCH+ZK8Xs4hb9Cw7pHlujWNL52RKe8TExOkrw6Ir1AmfajBZTb9XUdKOZO08RwQElIKA8+Ltm+Gqfo8djQ==} + engines: {node: '>=18'} + peerDependencies: + zod: ^3.25.76 || ^4.1.8 + '@ai-sdk/provider-utils@4.0.8': resolution: {integrity: sha512-ns9gN7MmpI8vTRandzgz+KK/zNMLzhrriiKECMt4euLtQFSBgNfydtagPOX4j4pS1/3KvHF6RivhT3gNQgBZsg==} engines: {node: '>=18'} @@ -459,12 +477,22 @@ packages: resolution: {integrity: sha512-5KXyBOSEX+l67elrEa+wqo/LSsSTtrPj9Uoh3zMbe/ceQX4ucHI3b9nUEfNkGF3Ry1svv90widAt+aiKdIJasQ==} engines: {node: '>=18'} + '@ai-sdk/provider@3.0.5': + resolution: {integrity: sha512-2Xmoq6DBJqmSl80U6V9z5jJSJP7ehaJJQMy2iFUqTay06wdCqTnPVBBQbtEL8RCChenL+q5DC5H5WzU3vV3v8w==} + engines: {node: '>=18'} + '@ai-sdk/react@3.0.41': resolution: {integrity: sha512-mTyfkM+WVUIlaqIpOPSgHlKL1sRQ9df+hdhs7EmDi804m/Ouw+Cq++HCJ9GFAnpnLizcO8huBoTNIrgdjewjzQ==} engines: {node: '>=18'} peerDependencies: react: ^18 || ~19.0.1 || ~19.1.2 || ^19.2.1 + '@ai-sdk/react@3.0.61': + resolution: {integrity: sha512-vCjZBnY2+TawFBXamSKt6elAt9n1MXMfcjSd9DSgT9peCJN27qNGVSXgaGNh/B3cUgeOktFfhB2GVmIqOjvmLQ==} + engines: {node: '>=18'} + peerDependencies: + react: ^18 || ~19.0.1 || ~19.1.2 || ^19.2.1 + '@alloc/quick-lru@5.2.0': resolution: {integrity: sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw==} engines: {node: '>=10'} @@ -1349,6 +1377,11 @@ packages: '@jridgewell/trace-mapping@0.3.31': resolution: {integrity: sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==} + '@loremllm/transport@0.6.1': + resolution: {integrity: sha512-ox2IX9sa5rmCE5WrUCKAI6qWmlHkL7GHuj2seSIqvkdt5tRLtgGFRWVz99gv4sdqGmfqNrVNJI+ZRbNWjf/KTA==} + peerDependencies: + ai: ^6.x.x + '@manypkg/find-root@1.1.0': resolution: {integrity: sha512-mki5uBvhHzO8kYYix/WRy2WX8S3B5wdVSc9D6KcU5lQNglP2yt58/VfLuAK49glRXChosY8ap2oJ1qgma3GUVA==} @@ -2452,9 +2485,6 @@ packages: resolution: {integrity: sha512-tlqY9xq5ukxTUZBmoOp+m61cqwQD5pHJtFY3Mn8CA8ps6yghLH/Hw8UPdqg4OLmFW3IFlcXnQNmo/dh8HzXYIQ==} engines: {node: '>=18'} - '@standard-schema/spec@1.0.0': - resolution: {integrity: sha512-m2bOd0f2RT9k8QJx1JN85cZYyH1RqFBdlwtkSlf4tBDYLCiiZnv1fIIwacK6cqwXavOydf0NPToMQgpKq+dVlA==} - '@standard-schema/spec@1.1.0': resolution: {integrity: sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==} @@ -2994,6 +3024,12 @@ packages: peerDependencies: zod: ^3.25.76 || ^4.1.8 + ai@6.0.59: + resolution: {integrity: sha512-9SfCvcr4kVk4t8ZzIuyHpuL1hFYKsYMQfBSbBq3dipXPa+MphARvI8wHEjNaRqYl3JOsJbWxEBIMqHL0L92mUA==} + engines: {node: '>=18'} + peerDependencies: + zod: ^3.25.76 || ^4.1.8 + ajv@6.12.6: resolution: {integrity: sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==} @@ -6433,6 +6469,20 @@ snapshots: '@vercel/oidc': 3.1.0 zod: 4.3.5 + '@ai-sdk/gateway@3.0.27(zod@4.3.5)': + dependencies: + '@ai-sdk/provider': 3.0.5 + '@ai-sdk/provider-utils': 4.0.10(zod@4.3.5) + '@vercel/oidc': 3.1.0 + zod: 4.3.5 + + '@ai-sdk/provider-utils@4.0.10(zod@4.3.5)': + dependencies: + '@ai-sdk/provider': 3.0.5 + '@standard-schema/spec': 1.1.0 + eventsource-parser: 3.0.6 + zod: 4.3.5 + '@ai-sdk/provider-utils@4.0.8(zod@3.25.76)': dependencies: '@ai-sdk/provider': 3.0.4 @@ -6451,6 +6501,10 @@ snapshots: dependencies: json-schema: 0.4.0 + '@ai-sdk/provider@3.0.5': + dependencies: + json-schema: 0.4.0 + '@ai-sdk/react@3.0.41(react@19.2.3)(zod@3.25.76)': dependencies: '@ai-sdk/provider-utils': 4.0.8(zod@3.25.76) @@ -6471,6 +6525,16 @@ snapshots: transitivePeerDependencies: - zod + '@ai-sdk/react@3.0.61(react@19.2.3)(zod@4.3.5)': + dependencies: + '@ai-sdk/provider-utils': 4.0.10(zod@4.3.5) + ai: 6.0.59(zod@4.3.5) + react: 19.2.3 + swr: 2.3.8(react@19.2.3) + throttleit: 2.1.0 + transitivePeerDependencies: + - zod + '@alloc/quick-lru@5.2.0': {} '@antfu/install-pkg@1.1.0': @@ -7309,6 +7373,10 @@ snapshots: '@jridgewell/resolve-uri': 3.1.2 '@jridgewell/sourcemap-codec': 1.5.5 + '@loremllm/transport@0.6.1(ai@6.0.39(zod@4.3.5))': + dependencies: + ai: 6.0.39(zod@4.3.5) + '@manypkg/find-root@1.1.0': dependencies: '@babel/runtime': 7.28.4 @@ -8514,8 +8582,6 @@ snapshots: '@sindresorhus/merge-streams@4.0.0': {} - '@standard-schema/spec@1.0.0': {} - '@standard-schema/spec@1.1.0': {} '@standard-schema/utils@0.3.0': {} @@ -9023,7 +9089,7 @@ snapshots: '@vitest/expect@4.0.17': dependencies: - '@standard-schema/spec': 1.0.0 + '@standard-schema/spec': 1.1.0 '@types/chai': 5.2.2 '@vitest/spy': 4.0.17 '@vitest/utils': 4.0.17 @@ -9113,6 +9179,14 @@ snapshots: '@opentelemetry/api': 1.9.0 zod: 4.3.5 + ai@6.0.59(zod@4.3.5): + dependencies: + '@ai-sdk/gateway': 3.0.27(zod@4.3.5) + '@ai-sdk/provider': 3.0.5 + '@ai-sdk/provider-utils': 4.0.10(zod@4.3.5) + '@opentelemetry/api': 1.9.0 + zod: 4.3.5 + ajv@6.12.6: dependencies: fast-deep-equal: 3.1.3