File tree Expand file tree Collapse file tree 1 file changed +17
-1
lines changed
Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -2,8 +2,10 @@ import {
22 AIMessage ,
33 AIMessageChunk ,
44 BaseMessage ,
5+ BaseMessageChunk ,
56 HumanMessage ,
67 isAIMessage ,
8+ isAIMessageChunk ,
79 isSystemMessage ,
810 SystemMessage ,
911 ToolMessage ,
@@ -155,7 +157,12 @@ export class BaseModelProvider {
155157 await callbacks . onStreamChunk ( aiMessage ) ;
156158 }
157159
158- fullMessages . push ( aiMessage ) ;
160+ // don't push empty messages at the end of the stream
161+ // FIXME: this is duplicated code. The end of stream also happens somewhere below
162+ if ( aiMessage . text . trim ( ) . length > 0 ) {
163+ fullMessages . push ( aiMessage ) ;
164+ }
165+
159166 return fullMessages ;
160167 }
161168
@@ -213,6 +220,15 @@ export class BaseModelProvider {
213220 callbacks . onError ?.( error ) ;
214221 }
215222
223+ const lastMessage = fullMessages [ fullMessages . length - 1 ] ;
224+ if ( isAIMessage ( lastMessage ) || isAIMessageChunk ( lastMessage as BaseMessageChunk ) ) {
225+ if ( lastMessage . text . trim ( ) . length === 0 ) {
226+ // don't push empty messages at the end of the stream
227+ // FIXME: this is duplicated code. The end of stream also happens somewhere above
228+ fullMessages . pop ( ) ;
229+ }
230+ }
231+
216232 return fullMessages ;
217233 }
218234
You can’t perform that action at this time.
0 commit comments