Skip to content

Commit ed0d1a0

Browse files
authored
Get around the initial empty response from gemini-2.5-flash. (#10508)
1 parent cf7debb commit ed0d1a0

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

packages/core/src/core/client.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,37 @@ export class GeminiClient {
225225
async startChat(extraHistory?: Content[]): Promise<GeminiChat> {
226226
this.forceFullIdeContext = true;
227227
this.hasFailedCompressionAttempt = false;
228-
const envParts = await getEnvironmentContext(this.config);
229228

230229
const toolRegistry = this.config.getToolRegistry();
231230
const toolDeclarations = toolRegistry.getFunctionDeclarations();
232231
const tools: Tool[] = [{ functionDeclarations: toolDeclarations }];
233232

233+
// 1. Get the environment context parts as an array
234+
const envParts = await getEnvironmentContext(this.config);
235+
236+
// 2. Convert the array of parts into a single string
237+
const envContextString = envParts
238+
.map((part) => part.text || '')
239+
.join('\n\n');
240+
241+
// 3. Combine the dynamic context with the static handshake instruction
242+
const allSetupText = `
243+
${envContextString}
244+
245+
Reminder: Do not return an empty response when a tool call is required.
246+
247+
My setup is complete. I will provide my first command in the next turn.
248+
`.trim();
249+
250+
// 4. Create the history with a single, comprehensive user turn
234251
const history: Content[] = [
235252
{
236253
role: 'user',
237-
parts: envParts,
238-
},
239-
{
240-
role: 'model',
241-
parts: [{ text: 'Got it. Thanks for the context!' }],
254+
parts: [{ text: allSetupText }],
242255
},
243256
...(extraHistory ?? []),
244257
];
258+
245259
try {
246260
const userMemory = this.config.getUserMemory();
247261
const systemInstruction = getCoreSystemPrompt(this.config, userMemory);

packages/core/src/core/turn.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ describe('Turn', () => {
251251
expect(reportError).toHaveBeenCalledWith(
252252
error,
253253
'Error when talking to Gemini API',
254-
[...historyContent, reqParts],
254+
[...historyContent, { role: 'user', parts: reqParts }],
255255
'Turn.run-sendMessageStream',
256256
);
257257
});

packages/core/src/core/turn.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from '../utils/errors.js';
2929
import type { GeminiChat } from './geminiChat.js';
3030
import { parseThought, type ThoughtSummary } from '../utils/thoughtUtils.js';
31+
import { createUserContent } from '@google/genai';
3132

3233
// Define a structure for tools passed to the server
3334
export interface ServerTool {
@@ -306,7 +307,10 @@ export class Turn {
306307
throw error;
307308
}
308309

309-
const contextForReport = [...this.chat.getHistory(/*curated*/ true), req];
310+
const contextForReport = [
311+
...this.chat.getHistory(/*curated*/ true),
312+
createUserContent(req),
313+
];
310314
await reportError(
311315
error,
312316
'Error when talking to Gemini API',

0 commit comments

Comments
 (0)