perf: skip file I/O in updateMessagesFromHistory when no tool calls match

Build the partsMap before touching the conversation file. If no
function responses are found in the history, return immediately
without reading or writing the session JSON.
This commit is contained in:
Sandy Tao
2026-03-06 18:33:43 -08:00
parent e062f0d09a
commit 651c1a28b8
@@ -694,22 +694,16 @@ export class ChatRecordingService {
if (!this.conversationFile) return;
try {
this.updateConversation((conversation) => {
// Create a map of tool results from the API history for quick lookup by call ID.
// We store the full list of parts associated with each tool call ID to preserve
// multi-modal data and proper trajectory structure.
// Build the parts map from the API history BEFORE touching the file.
const partsMap = new Map<string, Part[]>();
for (const content of history) {
if (content.role === 'user' && content.parts) {
// Find all unique call IDs in this message
const callIds = content.parts
.map((p) => p.functionResponse?.id)
.filter((id): id is string => !!id);
if (callIds.length === 0) continue;
// Use the first ID as a seed to capture any "leading" non-ID parts
// in this specific content block.
let currentCallId = callIds[0];
for (const part of content.parts) {
if (part.functionResponse?.id) {
@@ -724,7 +718,10 @@ export class ChatRecordingService {
}
}
// Update the conversation records tool results if they've changed.
// Nothing to sync — skip the read/write entirely.
if (partsMap.size === 0) return;
this.updateConversation((conversation) => {
for (const message of conversation.messages) {
if (message.type === 'gemini' && message.toolCalls) {
for (const toolCall of message.toolCalls) {