diff --git a/packages/core/src/core/client.ts b/packages/core/src/core/client.ts index 90f86df26f1..6760428d464 100644 --- a/packages/core/src/core/client.ts +++ b/packages/core/src/core/client.ts @@ -429,7 +429,8 @@ export class GeminiClient { const text = message.parts ?.filter( - (part): part is { text: string } => typeof part.text === 'string', + (part): part is { text: string } => + typeof part.text === 'string' && !part.thought, ) .map((part) => part.text) .join('') ?? ''; diff --git a/packages/core/src/core/geminiChat.test.ts b/packages/core/src/core/geminiChat.test.ts index 7e20b94ed5d..b1254f636b8 100644 --- a/packages/core/src/core/geminiChat.test.ts +++ b/packages/core/src/core/geminiChat.test.ts @@ -4136,6 +4136,27 @@ describe('GeminiChat', async () => { expect(chat.getLastModelMessageText()).toBe('new answer'); expect(structuredCloneSpy).not.toHaveBeenCalled(); }); + + it('filters out thought parts from the last model message', () => { + chat.addHistory({ + role: 'model', + parts: [ + { text: 'internal reasoning...', thought: true }, + { text: 'visible response' }, + ], + }); + + expect(chat.getLastModelMessageText()).toBe('visible response'); + }); + + it('returns undefined when all text parts are thoughts', () => { + chat.addHistory({ + role: 'model', + parts: [{ text: 'only thinking', thought: true }], + }); + + expect(chat.getLastModelMessageText()).toBeUndefined(); + }); }); describe('sendMessageStream with retries', () => { diff --git a/packages/core/src/core/geminiChat.ts b/packages/core/src/core/geminiChat.ts index 6ade570acdd..158d87ec3e0 100644 --- a/packages/core/src/core/geminiChat.ts +++ b/packages/core/src/core/geminiChat.ts @@ -2776,7 +2776,8 @@ export class GeminiChat { const text = message.parts ?.filter( - (part): part is { text: string } => typeof part.text === 'string', + (part): part is { text: string } => + typeof part.text === 'string' && !part.thought, ) .map((part) => part.text) .join('') ?? '';