Skip to content

Commit 589f037

Browse files
Get around the initial empty response from gemini-2.5-flash. (#10508) (#10660)
Co-authored-by: Victor May <mayvic@google.com>
1 parent 11f7a6a commit 589f037

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
@@ -221,23 +221,37 @@ export class GeminiClient {
221221
async startChat(extraHistory?: Content[]): Promise<GeminiChat> {
222222
this.forceFullIdeContext = true;
223223
this.hasFailedCompressionAttempt = false;
224-
const envParts = await getEnvironmentContext(this.config);
225224

226225
const toolRegistry = this.config.getToolRegistry();
227226
const toolDeclarations = toolRegistry.getFunctionDeclarations();
228227
const tools: Tool[] = [{ functionDeclarations: toolDeclarations }];
229228

229+
// 1. Get the environment context parts as an array
230+
const envParts = await getEnvironmentContext(this.config);
231+
232+
// 2. Convert the array of parts into a single string
233+
const envContextString = envParts
234+
.map((part) => part.text || '')
235+
.join('\n\n');
236+
237+
// 3. Combine the dynamic context with the static handshake instruction
238+
const allSetupText = `
239+
${envContextString}
240+
241+
Reminder: Do not return an empty response when a tool call is required.
242+
243+
My setup is complete. I will provide my first command in the next turn.
244+
`.trim();
245+
246+
// 4. Create the history with a single, comprehensive user turn
230247
const history: Content[] = [
231248
{
232249
role: 'user',
233-
parts: envParts,
234-
},
235-
{
236-
role: 'model',
237-
parts: [{ text: 'Got it. Thanks for the context!' }],
250+
parts: [{ text: allSetupText }],
238251
},
239252
...(extraHistory ?? []),
240253
];
254+
241255
try {
242256
const userMemory = this.config.getUserMemory();
243257
const systemInstruction = getCoreSystemPrompt(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)