This commit is contained in:
Tae Hyung Kim 2025-05-07 22:58:41 -07:00 committed by N. Taylor Mullen
parent 6989032414
commit 448a24746c
1 changed files with 15 additions and 6 deletions

View File

@ -180,12 +180,16 @@ export const useGeminiStream = (
if (signal.aborted) break; if (signal.aborted) break;
if (event.type === ServerGeminiEventType.Content) { if (event.type === ServerGeminiEventType.Content) {
if (pendingHistoryItemRef.current?.type !== 'gemini') { if (
pendingHistoryItemRef.current?.type !== 'gemini' &&
pendingHistoryItemRef.current?.type !== 'gemini_content'
) {
// Flush out existing pending history item. // Flush out existing pending history item.
if (pendingHistoryItemRef.current) { if (pendingHistoryItemRef.current) {
addItem(pendingHistoryItemRef.current, userMessageTimestamp); addItem(pendingHistoryItemRef.current, userMessageTimestamp);
} }
setPendingHistoryItem({ setPendingHistoryItem({
// Use the 'gemini' type for the initial history item.
type: 'gemini', type: 'gemini',
text: '', text: '',
}); });
@ -199,10 +203,10 @@ export const useGeminiStream = (
const splitPoint = findLastSafeSplitPoint(geminiMessageBuffer); const splitPoint = findLastSafeSplitPoint(geminiMessageBuffer);
if (splitPoint === geminiMessageBuffer.length) { if (splitPoint === geminiMessageBuffer.length) {
// Update the existing message with accumulated content // Update the existing message with accumulated content
setPendingHistoryItem({ setPendingHistoryItem((item) => ({
type: 'gemini', type: item?.type as 'gemini' | 'gemini_content',
text: geminiMessageBuffer, text: geminiMessageBuffer,
}); }));
} else { } else {
// This indicates that we need to split up this Gemini Message. // This indicates that we need to split up this Gemini Message.
// Splitting a message is primarily a performance consideration. There is a // Splitting a message is primarily a performance consideration. There is a
@ -216,11 +220,16 @@ export const useGeminiStream = (
const afterText = geminiMessageBuffer.substring(splitPoint); const afterText = geminiMessageBuffer.substring(splitPoint);
geminiMessageBuffer = afterText; // Continue accumulating from split point geminiMessageBuffer = afterText; // Continue accumulating from split point
addItem( addItem(
{ type: 'gemini', text: beforeText }, {
type: pendingHistoryItemRef.current?.type as
| 'gemini'
| 'gemini_content',
text: beforeText,
},
userMessageTimestamp, userMessageTimestamp,
); );
setPendingHistoryItem({ setPendingHistoryItem({
type: 'gemini', type: 'gemini_content',
text: afterText, text: afterText,
}); });
} }