Skip to content

Commit bddcfef

Browse files
committed
update streaming response
1 parent b5e9bcc commit bddcfef

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

chat-agent-ui/components/AgentRuntimeProvider.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,21 @@ const AgentModelAdapter: ChatModelAdapter = {
3838
let text = "";
3939

4040
for (;;) {
41-
const {done, value} = await eventStream.read()
41+
const {done, value} = await eventStream.read();
4242
if (!done) {
43-
let data = value.data || "";
44-
if (data.length > 2) {
45-
data = data.substring(1, data.length - 1);
43+
if (!value.data) {
44+
continue;
4645
}
47-
text += data;
46+
const { content } = JSON.parse(value.data);
47+
if (!content || content.length == 0 || content[0].type !== "text" || !content[0].text) {
48+
continue;
49+
}
50+
text += content[0].text;
4851
yield {
4952
content: [{ type: "text", text }],
5053
};
51-
continue
5254
} else {
53-
break
55+
break;
5456
}
5557
}
5658
},

chat-agent-ui/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.javaaidev.chatagentui</groupId>
88
<artifactId>chat-agent-ui-parent</artifactId>
9-
<version>0.9.0</version>
9+
<version>0.10.0</version>
1010
</parent>
1111

1212
<artifactId>chat-agent-ui</artifactId>

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.chatagentui</groupId>
77
<artifactId>chat-agent-ui-parent</artifactId>
8-
<version>0.9.0</version>
8+
<version>0.10.0</version>
99
<packaging>pom</packaging>
1010
<name>Chat Agent UI :: Parent</name>
1111
<description>Chat Agent UI</description>

spring-ai-adapter/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.javaaidev.chatagentui</groupId>
88
<artifactId>chat-agent-ui-parent</artifactId>
9-
<version>0.9.0</version>
9+
<version>0.10.0</version>
1010
</parent>
1111

1212
<artifactId>spring-ai-adapter</artifactId>
@@ -19,6 +19,7 @@
1919
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2020
<java.version>17</java.version>
2121
<spring-ai.version>1.0.0-M5</spring-ai.version>
22+
<spring.version>6.2.3</spring.version>
2223
</properties>
2324

2425
<dependencyManagement>
@@ -38,6 +39,11 @@
3839
<groupId>org.springframework.ai</groupId>
3940
<artifactId>spring-ai-core</artifactId>
4041
</dependency>
42+
<dependency>
43+
<groupId>org.springframework</groupId>
44+
<artifactId>spring-web</artifactId>
45+
<version>${spring.version}</version>
46+
</dependency>
4147
<dependency>
4248
<groupId>com.javaaidev.chatagentui</groupId>
4349
<artifactId>chat-agent-ui</artifactId>

spring-ai-adapter/src/main/java/com/javaaidev/chatagent/springai/ModelAdapter.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.springframework.ai.chat.messages.UserMessage;
1616
import org.springframework.ai.chat.model.ChatResponse;
1717
import org.springframework.ai.chat.model.Generation;
18+
import org.springframework.http.codec.ServerSentEvent;
19+
import reactor.core.publisher.Flux;
1820

1921
/**
2022
* Convert models between chat agent and Spring AI
@@ -64,4 +66,23 @@ public static ChatAgentResponse toResponse(ChatResponse chatResponse) {
6466
}
6567
return new ChatAgentResponse(content);
6668
}
69+
70+
/**
71+
* Convert a stream of Spring AI {@linkplain ChatResponse} to a stream of
72+
* {@linkplain ChatAgentResponse} using Server-sent Events
73+
*
74+
* @param chatResponse Stream of {@linkplain ChatResponse}
75+
* @return Stream of {@linkplain ChatAgentResponse}
76+
*/
77+
public static Flux<ServerSentEvent<ChatAgentResponse>> toStreamingResponse(
78+
Flux<ChatResponse> chatResponse) {
79+
return chatResponse.concatMap(
80+
response -> Flux.fromIterable(response.getResults()))
81+
.filter(generation -> Objects.nonNull(generation.getOutput().getText()))
82+
.map(generation -> generation.getOutput().getText())
83+
.map(text -> ServerSentEvent.<ChatAgentResponse>builder()
84+
.data(new ChatAgentResponse(
85+
List.of(new TextContentPart(text))))
86+
.build());
87+
}
6788
}

0 commit comments

Comments
 (0)