summaryrefslogtreecommitdiff
path: root/packages/server/src/core/geminiChat.ts
diff options
context:
space:
mode:
authorTaylor Mullen <[email protected]>2025-05-29 23:53:35 -0700
committerN. Taylor Mullen <[email protected]>2025-05-30 10:43:48 -0700
commit9537ff476219486574fb6a50e54389a78beefe8e (patch)
tree95fe901641dec941f81b3a9f8d3dff610bd2e3d9 /packages/server/src/core/geminiChat.ts
parent7c4a5464f68db32bb0069927f65deb2a61bd094f (diff)
feat(server): consolidate adjacent model content in chat history
- Consolidates consecutive model messages into a single message in the chat history. - This prevents multiple model messages from being displayed in a row, improving readability. - This may also address some instances of 500 errors that could have been caused by multiple, rapidly succeeding model messages. - Adds tests for the new consolidation logic. Fixes https://b.corp.google.com/issues/421010429
Diffstat (limited to 'packages/server/src/core/geminiChat.ts')
-rw-r--r--packages/server/src/core/geminiChat.ts40
1 files changed, 39 insertions, 1 deletions
diff --git a/packages/server/src/core/geminiChat.ts b/packages/server/src/core/geminiChat.ts
index 5ba8ce2d..877d0825 100644
--- a/packages/server/src/core/geminiChat.ts
+++ b/packages/server/src/core/geminiChat.ts
@@ -313,6 +313,44 @@ export class GeminiChat {
} else {
this.history.push(userInput);
}
- this.history.push(...outputContents);
+
+ // Consolidate adjacent model roles in outputContents
+ const consolidatedOutputContents: Content[] = [];
+ for (const content of outputContents) {
+ const lastContent =
+ consolidatedOutputContents[consolidatedOutputContents.length - 1];
+ if (
+ lastContent &&
+ lastContent.role === 'model' &&
+ content.role === 'model' &&
+ lastContent.parts
+ ) {
+ lastContent.parts.push(...(content.parts || []));
+ } else {
+ consolidatedOutputContents.push(content);
+ }
+ }
+
+ if (consolidatedOutputContents.length > 0) {
+ const lastHistoryEntry = this.history[this.history.length - 1];
+ // Only merge if AFC history was NOT just added, to prevent merging with last AFC model turn.
+ const canMergeWithLastHistory =
+ !automaticFunctionCallingHistory ||
+ automaticFunctionCallingHistory.length === 0;
+
+ if (
+ canMergeWithLastHistory &&
+ lastHistoryEntry &&
+ lastHistoryEntry.role === 'model' &&
+ lastHistoryEntry.parts &&
+ consolidatedOutputContents[0].role === 'model'
+ ) {
+ lastHistoryEntry.parts.push(
+ ...(consolidatedOutputContents[0].parts || []),
+ );
+ consolidatedOutputContents.shift(); // Remove the first element as it's merged
+ }
+ this.history.push(...consolidatedOutputContents);
+ }
}
}