Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

feat(web): render streamed chat markdown via `streamdown` (#2493)

Colton Padden65397de4d46b
diff --git a/apps/frameworks/next/app/_chat/App.tsx b/apps/frameworks/next/app/_chat/App.tsx
index 489e4601f..b5a1847ce 100644
--- a/apps/frameworks/next/app/_chat/App.tsx
+++ b/apps/frameworks/next/app/_chat/App.tsx
@@ -2,15 +2,29 @@
import { useEveAgent } from "eve/react";
import { type FormEvent, type JSX, useEffect, useMemo, useRef, useState } from "react";
+import { type Components, Streamdown } from "streamdown";
import { traceReducer } from "./trace-reducer";
-import { resolveTurnFailureMessage, shouldRenderAssistantTurn } from "./turn-content";
+import {
+ resolveTurnAssistantMessage,
+ resolveTurnFailureMessage,
+ shouldRenderAssistantTurn,
+} from "./turn-content";
import type { TraceTurn } from "./types";
+const streamdownComponents = {
+ img: () => null,
+} satisfies Components;
+
function ConversationSection(props: {
readonly isSending: boolean;
readonly turns: readonly TraceTurn[];
}) {
+ const activeTurnId = props.isSending ? props.turns.at(-1)?.turnId : undefined;
+ const hasStreamingAssistantText = props.turns.some(
+ (turn) => turn.turnId === activeTurnId && resolveTurnAssistantMessage(turn) !== undefined,
+ );
+
return (
<ul className="chat-feed">
{props.turns.flatMap((turn) => {
@@ -30,21 +44,37 @@ function ConversationSection(props: {
return rendered;
}
- const assistantText = turn.assistantMessage ?? resolveTurnFailureMessage(turn) ?? "";
+ const assistantText =
+ resolveTurnAssistantMessage(turn) ?? resolveTurnFailureMessage(turn) ?? "";
+ const isStreaming = turn.turnId === activeTurnId;
rendered.push(
<li
className={`chat-row role-assistant${turn.status === "failed" ? " variant-error" : ""}`}
key={`${turn.turnId}:assistant`}
>
<div className="chat-bubble-stack">
- <div className="chat-bubble">{assistantText}</div>
+ <div className="chat-bubble">
+ {turn.status === "failed" ? (
+ assistantText
+ ) : (
+ <Streamdown
+ animated
+ components={streamdownComponents}
+ isAnimating={isStreaming}
+ mode={isStreaming ? "streaming" : "static"}
+ skipHtml
+ >
+ {assistantText}
+ </Streamdown>
+ )}
+ </div>
</div>
</li>,
);
return rendered;
})}
- {props.isSending ? (
+ {props.isSending && !hasStreamingAssistantText ? (
<li className="chat-row role-assistant pending">
<div className="chat-bubble">Thinking…</div>
</li>
diff --git a/apps/frameworks/next/app/_chat/turn-content.ts b/apps/frameworks/next/app/_chat/turn-content.ts
index 470f182f3..8aa8c18fe 100644
--- a/apps/frameworks/next/app/_chat/turn-content.ts
+++ b/apps/frameworks/next/app/_chat/turn-content.ts
@@ -72,6 +72,25 @@ function readMessageFromCompletedEvent(event: { readonly data?: unknown }): stri
return readString(event.data.message);
}
+/**
+ * Resolves the latest assistant text, including a response that is still
+ * streaming and has not emitted `message.completed` yet.
+ */
+export function resolveTurnAssistantMessage(turn: TraceTurn): string | undefined {
+ if (typeof turn.assistantMessage === "string" && turn.assistantMessage.length > 0) {
+ return turn.assistantMessage;
+ }
+
+ for (let index = turn.steps.length - 1; index >= 0; index -= 1) {
+ const responseText = turn.steps[index]?.responseText;
+ if (typeof responseText === "string" && responseText.length > 0) {
+ return responseText;
+ }
+ }
+
+ return undefined;
+}
+
/**
* Resolves the most useful assistant-facing failure text for a turn when no
* normal assistant message was completed.
@@ -102,7 +121,7 @@ export function resolveTurnFailureMessage(turn: TraceTurn): string | undefined {
* for the provided turn.
*/
export function shouldRenderAssistantTurn(turn: TraceTurn): boolean {
- if (typeof turn.assistantMessage === "string" && turn.assistantMessage.length > 0) {
+ if (resolveTurnAssistantMessage(turn) !== undefined) {
return true;
}
diff --git a/apps/frameworks/next/app/globals.css b/apps/frameworks/next/app/globals.css
index a2dc41ece..1400ca84a 100644
--- a/apps/frameworks/next/app/globals.css
+++ b/apps/frameworks/next/app/globals.css
@@ -1,4 +1,5 @@
@import "tailwindcss";
+@source "../node_modules/streamdown/dist/*.js";
:root {
--background: #ffffff;
@@ -7,7 +8,13 @@
@theme inline {
--color-background: var(--background);
+ --color-border: var(--border);
--color-foreground: var(--foreground);
+ --color-muted: var(--bg-hover);
+ --color-muted-foreground: var(--fg-muted);
+ --color-primary: var(--fg);
+ --color-primary-foreground: var(--fg-on-inverted);
+ --color-sidebar: var(--bg-subtle);
--font-sans: var(--font-geist-sans);
--font-mono: var(--font-geist-mono);
}
diff --git a/apps/frameworks/next/app/layout.tsx b/apps/frameworks/next/app/layout.tsx
index 38565cbed..10e345bc0 100644
--- a/apps/frameworks/next/app/layout.tsx
+++ b/apps/frameworks/next/app/layout.tsx
@@ -1,5 +1,6 @@
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
+import "streamdown/styles.css";
import "./globals.css";
const geistSans = Geist({
diff --git a/apps/frameworks/next/package.json b/apps/frameworks/next/package.json
index 2be2ee547..bdfe3110b 100644
--- a/apps/frameworks/next/package.json
+++ b/apps/frameworks/next/package.json
@@ -19,6 +19,7 @@
"next": "catalog:",
"react": "catalog:",
"react-dom": "catalog:",
+ "streamdown": "catalog:",
"zod": "catalog:"
},
"devDependencies": {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 95cc41a09..3e21549a3 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -441,6 +441,9 @@ importers:
react-dom:
specifier: 'catalog:'
version: 19.2.6([email protected])
+ streamdown:
+ specifier: 'catalog:'
zod:
specifier: 'catalog:'
version: 4.4.3