Skip to content

Commit 64b970e

Browse files
committed
add streaming support
1 parent e79159e commit 64b970e

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

components/AgentRuntimeProvider.tsx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import {
66
useLocalRuntime,
77
type ChatModelAdapter,
88
} from "@assistant-ui/react";
9+
import {EventSourceParserStream} from 'eventsource-parser/stream'
910

1011
const AgentModelAdapter: ChatModelAdapter = {
11-
async run({ messages, abortSignal }) {
12-
const result = await fetch("/chat", {
12+
async *run({ messages, abortSignal }) {
13+
const response = await fetch("/chat", {
1314
method: "POST",
1415
headers: {
1516
"Content-Type": "application/json",
@@ -20,7 +21,30 @@ const AgentModelAdapter: ChatModelAdapter = {
2021
signal: abortSignal,
2122
});
2223

23-
return await result.json();
24+
if (!response.ok) {
25+
const error = await response.text();
26+
throw new Error(`Failed (${response.status}): ${error}`);
27+
}
28+
29+
if (!response.body) {
30+
throw new Error(`No response`);
31+
}
32+
33+
const eventStream = response.body
34+
.pipeThrough(new TextDecoderStream())
35+
.pipeThrough(new EventSourceParserStream());
36+
37+
let text = "";
38+
for await (const content of eventStream) {
39+
let data = content.data || "";
40+
if (data.length > 2) {
41+
data = data.substring(1, data.length - 1);
42+
}
43+
text += data;
44+
yield {
45+
content: [{ type: "text", text }],
46+
};
47+
}
2448
},
2549
};
2650

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@radix-ui/react-tooltip": "^1.1.8",
1616
"class-variance-authority": "^0.7.1",
1717
"clsx": "^2.1.1",
18+
"eventsource-parser": "^3.0.1",
1819
"lucide-react": "^0.483.0",
1920
"next": "15.2.0",
2021
"react": "^19.0.0",

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.javaaidev</groupId>
77
<artifactId>chat-agent-ui</artifactId>
8-
<version>0.6.0</version>
8+
<version>0.7.0</version>
99
<name>Chat Agent UI</name>
1010
<description>Chat Agent UI</description>
1111

0 commit comments

Comments
 (0)