|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useRealtimeStream } from "@trigger.dev/react-hooks"; |
| 4 | +import type { UIMessage, UIMessageChunk } from "ai"; |
| 5 | +import { Streamdown } from "streamdown"; |
| 6 | + |
| 7 | +export function AIChat({ accessToken, runId }: { accessToken: string; runId: string }) { |
| 8 | + const { parts, error } = useRealtimeStream<UIMessageChunk>(runId, "chat", { |
| 9 | + accessToken, |
| 10 | + baseURL: process.env.NEXT_PUBLIC_TRIGGER_API_URL, |
| 11 | + timeoutInSeconds: 600, |
| 12 | + }); |
| 13 | + |
| 14 | + if (error) return <div className="text-red-600 font-semibold">Error: {error.message}</div>; |
| 15 | + |
| 16 | + if (!parts) return <div className="text-gray-600">Loading...</div>; |
| 17 | + |
| 18 | + // Compute derived state directly from parts |
| 19 | + let accumulatedText = ""; |
| 20 | + let currentId: string | null = null; |
| 21 | + let isComplete = false; |
| 22 | + |
| 23 | + for (const chunk of parts) { |
| 24 | + switch (chunk.type) { |
| 25 | + case "text-start": |
| 26 | + if (!currentId) { |
| 27 | + currentId = chunk.id; |
| 28 | + } |
| 29 | + break; |
| 30 | + case "text-delta": |
| 31 | + accumulatedText += chunk.delta; |
| 32 | + break; |
| 33 | + case "text-end": |
| 34 | + isComplete = true; |
| 35 | + break; |
| 36 | + case "error": |
| 37 | + console.error("Stream error:", chunk.errorText); |
| 38 | + break; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Determine what to render |
| 43 | + const messages: UIMessage[] = []; |
| 44 | + let currentText = ""; |
| 45 | + let currentMessageId: string | null = null; |
| 46 | + let currentRole: "assistant" | null = null; |
| 47 | + |
| 48 | + if (isComplete && currentId && accumulatedText) { |
| 49 | + // Streaming is complete, show as completed message |
| 50 | + messages.push({ |
| 51 | + id: currentId, |
| 52 | + role: "assistant", |
| 53 | + parts: [{ type: "text", text: accumulatedText }], |
| 54 | + }); |
| 55 | + } else if (currentId) { |
| 56 | + // Still streaming |
| 57 | + currentText = accumulatedText; |
| 58 | + currentMessageId = currentId; |
| 59 | + currentRole = "assistant"; |
| 60 | + } |
| 61 | + |
| 62 | + return ( |
| 63 | + <div className="space-y-6"> |
| 64 | + <div className="text-sm font-medium text-gray-700 mb-4"> |
| 65 | + <span className="font-semibold">Run:</span> {runId} |
| 66 | + </div> |
| 67 | + |
| 68 | + {/* Render completed messages */} |
| 69 | + {messages.map((message) => ( |
| 70 | + <div key={message.id} className="p-4 rounded-lg bg-gray-50 border-l-4 border-purple-500"> |
| 71 | + <div className="text-xs font-semibold text-gray-500 uppercase mb-2">{message.role}</div> |
| 72 | + <div className="prose prose-sm max-w-none text-gray-900"> |
| 73 | + {message.parts.map((part, idx) => |
| 74 | + part.type === "text" ? ( |
| 75 | + <Streamdown key={idx} isAnimating={false}> |
| 76 | + {part.text} |
| 77 | + </Streamdown> |
| 78 | + ) : null |
| 79 | + )} |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + ))} |
| 83 | + |
| 84 | + {/* Render current streaming message */} |
| 85 | + {currentMessageId && currentRole && ( |
| 86 | + <div className="p-4 rounded-lg bg-gray-50 border-l-4 border-purple-500"> |
| 87 | + <div className="text-xs font-semibold text-gray-500 uppercase mb-2"> |
| 88 | + {currentRole} <span className="text-purple-600">(streaming...)</span> |
| 89 | + </div> |
| 90 | + <div className="prose prose-sm max-w-none text-gray-900"> |
| 91 | + <Streamdown isAnimating={true}>{currentText}</Streamdown> |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + )} |
| 95 | + </div> |
| 96 | + ); |
| 97 | +} |
0 commit comments