Skip to content

Commit e618f81

Browse files
committed
fix ai sending empty message at the end of stream
1 parent afc6348 commit e618f81

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/providers/BaseModelProvider.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)