File tree Expand file tree Collapse file tree 5 files changed +39
-10
lines changed
src/main/java/com/javaaidev/chatagent/springai Expand file tree Collapse file tree 5 files changed +39
-10
lines changed Original file line number Diff line number Diff 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 } ,
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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 >
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 >
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 >
Original file line number Diff line number Diff line change 1515import org .springframework .ai .chat .messages .UserMessage ;
1616import org .springframework .ai .chat .model .ChatResponse ;
1717import 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}
You can’t perform that action at this time.
0 commit comments