Skip to content

Commit 7f3adaf

Browse files
Here's my plan to address the issue:
Fix: Ensure full chat history context via comprehensive DTOs/Mappers This commit aims to resolve issues of reduced chat history context (previously observed as "history size: 2" being sent to the AI) by ensuring all relevant SDK Part types are correctly handled during serialization and deserialization between the ViewModel and the ScreenCaptureService. Key changes included in this state: 1. **Comprehensive DTOs (`PhotoReasoningDtos.kt`):** * DTOs (`TextPartDto`, `ImagePartDto`, `BlobPartDto`, `FunctionCallPartDto`, `FunctionResponsePartDto`) are defined to represent all common SDK `com.google.ai.client.generativeai.type.Part` subtypes. * `ImagePartDto` uses file paths to handle image data, avoiding `TransactionTooLargeException`. 2. **Updated Mappers (`PhotoReasoningMappers.kt`):** * Mapper functions (`toDto` and `toSdk`) now fully support conversion for Text, Image (via files), Blob (ByteArray), FunctionCall, and FunctionResponse (JSONObject-as-string) parts. 3. **ViewModel & Service Logic:** * `PhotoReasoningViewModel` uses these mappers to convert the full `chat.history` and `inputContent` to DTOs before serializing to JSON and sending to `ScreenCaptureService`. It also now sends temporary image file paths for cleanup. * `ScreenCaptureService` deserializes to these DTOs, maps them back to SDK types (reloading images from files), executes the AI call, and cleans up temporary image files. * The service also attempts to run in the foreground during AI calls to improve network reliability. 4. **Previous Fixes Incorporated:** * Includes fix for `TransactionTooLargeException` (using files for images). * Includes fix for `BroadcastReceiver` registration security exception on Android 13+. * Includes fixes for `FunctionCallPart` DTO mapping compilation errors. This set of changes should ensure that the complete and accurate chat history, with all its diverse content parts, is provided to the AI model, restoring its contextual understanding and performance. Some diagnostic logging remains in place from previous debugging steps.
1 parent 8aee09f commit 7f3adaf

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

app/src/main/kotlin/com/google/ai/sample/ScreenCaptureService.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import com.google.ai.client.generativeai.GenerativeModel
2929
import com.google.ai.client.generativeai.type.Content
3030
import com.google.ai.client.generativeai.type.ImagePart // For instance check
3131
import com.google.ai.client.generativeai.type.FunctionCallPart // For logging AI response
32+
import com.google.ai.client.generativeai.type.FunctionResponsePart // For logging AI response
33+
import com.google.ai.client.generativeai.type.BlobPart // For logging AI response
3234
import com.google.ai.sample.feature.multimodal.dtos.ContentDto
3335
import com.google.ai.sample.feature.multimodal.dtos.toSdk
3436
import kotlinx.coroutines.CoroutineScope
@@ -249,11 +251,16 @@ class ScreenCaptureService : Service() {
249251
val tempChat = generativeModel.startChat(history = chatHistory) // Use the mapped SDK history
250252
Log.d(TAG, "Executing AI sendMessage with history size: ${chatHistory.size}")
251253
val aiResponse = tempChat.sendMessage(inputContent) // Use the mapped SDK inputContent
252-
Log.d(TAG, "Service received AI Response. Parts: ${aiResponse.parts.joinToString { it.javaClass.simpleName }}")
253-
// If a part is FunctionCallPart, log its name and args
254-
aiResponse.parts.filterIsInstance<com.google.ai.client.generativeai.type.FunctionCallPart>().forEach { fcp ->
254+
Log.d(TAG, "Service received AI Response. Parts: ${aiResponse.parts.joinToString { part -> part.javaClass.simpleName }}")
255+
aiResponse.parts.filterIsInstance<com.google.ai.client.generativeai.type.FunctionCallPart>().forEach { fcp: com.google.ai.client.generativeai.type.FunctionCallPart ->
255256
Log.d(TAG, " AI sent FunctionCallPart: name='${fcp.name}', args='${fcp.args}'")
256257
}
258+
aiResponse.parts.filterIsInstance<com.google.ai.client.generativeai.type.FunctionResponsePart>().forEach { frp: com.google.ai.client.generativeai.type.FunctionResponsePart ->
259+
Log.d(TAG, " AI sent FunctionResponsePart: name='${frp.name}', response='${frp.response.toString().take(100)}...'")
260+
}
261+
aiResponse.parts.filterIsInstance<com.google.ai.client.generativeai.type.BlobPart>().forEach { bp: com.google.ai.client.generativeai.type.BlobPart ->
262+
Log.d(TAG, " AI sent BlobPart: mimeType='${bp.mimeType}', dataSize=${bp.blob.size}")
263+
}
257264
responseText = aiResponse.text
258265
Log.d(TAG, "AI call successful. Response text available: ${responseText != null}")
259266

0 commit comments

Comments
 (0)