| 18 | * stays plain (no markdown injection from user input). |
| 19 | */ |
| 20 | export function ChatMessage({ |
| 21 | message, |
| 22 | runStillActive = false, |
| 23 | }: { |
| 24 | message: UIMessage; |
| 25 | /** |
| 26 | * True only for the message currently being generated by the agent. |
| 27 | * Used to decide whether a reasoning block without an explicit |
| 28 | * "complete" state signal should keep its streaming UI. Anything in |
| 29 | * a prior message is always considered done. |
| 30 | */ |
| 31 | runStillActive?: boolean; |
| 32 | }) { |
| 33 | const isUser = message.role === 'user'; |
| 34 | |
| 35 | // Index tool results by callId so we can attach them to their call. |
| 36 | const resultByCallId = new Map<string, { result?: unknown; status: string }>(); |
| 37 | for (const part of message.parts) { |
| 38 | if (part.type === 'tool-result') { |
| 39 | resultByCallId.set(part.callId, { |
| 40 | result: part.result, |
| 41 | status: part.status, |
| 42 | }); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | const lastIdx = message.parts.length - 1; |
| 47 | |
| 48 | return ( |
| 49 | <div |
| 50 | className={cn( |
| 51 | 'flex flex-col gap-2', |
| 52 | isUser ? 'items-end' : 'items-start', |
| 53 | )} |
| 54 | > |
| 55 | {message.parts.map((part, idx) => { |
| 56 | switch (part.type) { |
| 57 | case 'text': |
| 58 | if (isUser) { |
| 59 | return ( |
| 60 | <div |
| 61 | key={`${message.localId}-text-${idx}`} |
| 62 | className="rounded-lg px-3 py-2 text leading-[1.5] whitespace-pre-wrap break-words max-w-[90%] bg-primary text-primary-foreground" |
| 63 | > |
| 64 | {part.text} |
| 65 | </div> |
| 66 | ); |
| 67 | } |
| 68 | return ( |
| 69 | <div |
| 70 | key={`${message.localId}-text-${idx}`} |
| 71 | className="max-w-full w-full" |
| 72 | > |
| 73 | <ChatMarkdown text={part.text} /> |
| 74 | </div> |
| 75 | ); |
| 76 | |
| 77 | case 'reasoning': { |