Skip to content

Commit ef3e9af

Browse files
committed
[AI] Chat history must store all the parts (streaming)
The chat history in streaming mode reconstructs the parts from their contents, rather than storing the parts themselves. This causes non-visible elements, like `thoughtSignature` to get lost.
1 parent 9cc0669 commit ef3e9af

File tree

1 file changed

+17
-9
lines changed
  • firebase-ai/src/main/kotlin/com/google/firebase/ai

1 file changed

+17
-9
lines changed

firebase-ai/src/main/kotlin/com/google/firebase/ai/Chat.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.google.firebase.ai.type.GenerateContentResponse
2222
import com.google.firebase.ai.type.ImagePart
2323
import com.google.firebase.ai.type.InlineDataPart
2424
import com.google.firebase.ai.type.InvalidStateException
25+
import com.google.firebase.ai.type.Part
2526
import com.google.firebase.ai.type.TextPart
2627
import com.google.firebase.ai.type.content
2728
import java.util.LinkedList
@@ -133,6 +134,7 @@ public class Chat(
133134
val bitmaps = LinkedList<Bitmap>()
134135
val inlineDataParts = LinkedList<InlineDataPart>()
135136
val text = StringBuilder()
137+
val parts = mutableListOf<Part>()
136138

137139
/**
138140
* TODO: revisit when images and inline data are returned. This will cause issues with how
@@ -147,22 +149,28 @@ public class Chat(
147149
is ImagePart -> bitmaps.add(part.image)
148150
is InlineDataPart -> inlineDataParts.add(part)
149151
}
152+
parts.add(part)
150153
}
151154
}
152155
.onCompletion {
153156
lock.release()
154157
if (it == null) {
155158
val content =
156159
content("model") {
157-
for (bitmap in bitmaps) {
158-
image(bitmap)
159-
}
160-
for (inlineDataPart in inlineDataParts) {
161-
inlineData(inlineDataPart.inlineData, inlineDataPart.mimeType)
162-
}
163-
if (text.isNotBlank()) {
164-
text(text.toString())
165-
}
160+
setParts(
161+
parts
162+
.filterNot { part -> part is TextPart && part.text.isNotEmpty() }
163+
.toMutableList()
164+
)
165+
// for (bitmap in bitmaps) {
166+
// image(bitmap)
167+
// }
168+
// for (inlineDataPart in inlineDataParts) {
169+
// inlineData(inlineDataPart.inlineData, inlineDataPart.mimeType)
170+
// }
171+
// if (text.isNotBlank()) {
172+
// text(text.toString())
173+
// }
166174
}
167175

168176
history.add(prompt)

0 commit comments

Comments
 (0)