summaryrefslogtreecommitdiff
path: root/packages/cli/src/core/gemini-client.ts
blob: 41cabdb7af44667d2c14fa9b8209fc56af4df3ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import {
  GenerateContentConfig,
  GoogleGenAI,
  Part,
  Chat,
  Type,
  SchemaUnion,
  PartListUnion,
  Content,
} from '@google/genai';
import { getApiKey } from '../config/env.js';
import { CoreSystemPrompt } from './prompts.js';
import {
  type ToolCallEvent,
  type ToolCallConfirmationDetails,
  ToolCallStatus,
} from '../ui/types.js';
import process from 'node:process';
import { toolRegistry } from '../tools/tool-registry.js';
import { ToolResult } from '../tools/tools.js';
import { getFolderStructure } from '../utils/getFolderStructure.js';
import { GeminiEventType, GeminiStream } from './gemini-stream.js';

type ToolExecutionOutcome = {
  callId: string;
  name: string;
  args: Record<string, any>;
  result?: ToolResult;
  error?: any;
  confirmationDetails?: ToolCallConfirmationDetails;
};

export class GeminiClient {
  private ai: GoogleGenAI;
  private defaultHyperParameters: GenerateContentConfig = {
    temperature: 0,
    topP: 1,
  };
  private readonly MAX_TURNS = 100;

  constructor() {
    const apiKey = getApiKey();
    this.ai = new GoogleGenAI({ apiKey });
  }

  public async startChat(): Promise<Chat> {
    const tools = toolRegistry.getToolSchemas();

    // --- Get environmental information ---
    const cwd = process.cwd();
    const today = new Date().toLocaleDateString(undefined, {
      // Use locale-aware date formatting
      weekday: 'long',
      year: 'numeric',
      month: 'long',
      day: 'numeric',
    });
    const platform = process.platform;

    // --- Format information into a conversational multi-line string ---
    const folderStructure = await getFolderStructure(cwd);
    // --- End folder structure formatting ---)
    const initialContextText = `
Okay, just setting up the context for our chat.
Today is ${today}.
My operating system is: ${platform}
I'm currently working in the directory: ${cwd}
${folderStructure}
        `.trim();

    const initialContextPart: Part = { text: initialContextText };
    // --- End environmental information formatting ---

    try {
      const chat = this.ai.chats.create({
        model: 'gemini-2.0-flash', //'gemini-2.0-flash',
        config: {
          systemInstruction: CoreSystemPrompt,
          ...this.defaultHyperParameters,
          tools,
        },
        history: [
          // --- Add the context as a single part in the initial user message ---
          {
            role: 'user',
            parts: [initialContextPart], // Pass the single Part object in an array
          },
          // --- Add an empty model response to balance the history ---
          {
            role: 'model',
            parts: [{ text: 'Got it. Thanks for the context!' }], // A slightly more conversational model response
          },
          // --- End history modification ---
        ],
      });
      return chat;
    } catch (error) {
      console.error('Error initializing Gemini chat session:', error);
      const message = error instanceof Error ? error.message : 'Unknown error.';
      throw new Error(`Failed to initialize chat: ${message}`);
    }
  }

  public addMessageToHistory(chat: Chat, message: Content): void {
    const history = chat.getHistory();
    history.push(message);
    this.ai.chats;
    chat;
  }

  public async *sendMessageStream(
    chat: Chat,
    request: PartListUnion,
    signal?: AbortSignal,
  ): GeminiStream {
    let currentMessageToSend: PartListUnion = request;
    let turns = 0;

    try {
      while (turns < this.MAX_TURNS) {
        turns++;
        const resultStream = await chat.sendMessageStream({
          message: currentMessageToSend,
        });
        let functionResponseParts: Part[] = [];
        let pendingToolCalls: Array<{
          callId: string;
          name: string;
          args: Record<string, any>;
        }> = [];
        let yieldedTextInTurn = false;
        const chunksForDebug = [];

        for await (const chunk of resultStream) {
          chunksForDebug.push(chunk);
          if (signal?.aborted) {
            const abortError = new Error(
              'Request cancelled by user during stream.',
            );
            abortError.name = 'AbortError';
            throw abortError;
          }

          const functionCalls = chunk.functionCalls;
          if (functionCalls && functionCalls.length > 0) {
            for (const call of functionCalls) {
              const callId =
                call.id ??
                `${call.name}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
              const name = call.name || 'undefined_tool_name';
              const args = (call.args || {}) as Record<string, any>;

              pendingToolCalls.push({ callId, name, args });
              const evtValue: ToolCallEvent = {
                type: 'tool_call',
                status: ToolCallStatus.Pending,
                callId,
                name,
                args,
                resultDisplay: undefined,
                confirmationDetails: undefined,
              };
              yield {
                type: GeminiEventType.ToolCallInfo,
                value: evtValue,
              };
            }
          } else {
            const text = chunk.text;
            if (text) {
              yieldedTextInTurn = true;
              yield {
                type: GeminiEventType.Content,
                value: text,
              };
            }
          }
        }

        if (pendingToolCalls.length > 0) {
          const toolPromises: Promise<ToolExecutionOutcome>[] =
            pendingToolCalls.map(async (pendingToolCall) => {
              const tool = toolRegistry.getTool(pendingToolCall.name);

              if (!tool) {
                // Directly return error outcome if tool not found
                return {
                  ...pendingToolCall,
                  error: new Error(
                    `Tool "${pendingToolCall.name}" not found or is not registered.`,
                  ),
                };
              }

              try {
                const confirmation = await tool.shouldConfirmExecute(
                  pendingToolCall.args,
                );
                if (confirmation) {
                  return {
                    ...pendingToolCall,
                    confirmationDetails: confirmation,
                  };
                }
              } catch (error) {
                return {
                  ...pendingToolCall,
                  error: new Error(
                    `Tool failed to check tool confirmation: ${error}`,
                  ),
                };
              }

              try {
                const result = await tool.execute(pendingToolCall.args);
                return { ...pendingToolCall, result };
              } catch (error) {
                return {
                  ...pendingToolCall,
                  error: new Error(`Tool failed to execute: ${error}`),
                };
              }
            });
          const toolExecutionOutcomes: ToolExecutionOutcome[] =
            await Promise.all(toolPromises);

          for (const executedTool of toolExecutionOutcomes) {
            const { callId, name, args, result, error, confirmationDetails } =
              executedTool;

            if (error) {
              const errorMessage = error?.message || String(error);
              yield {
                type: GeminiEventType.Content,
                value: `[Error invoking tool ${name}: ${errorMessage}]`,
              };
            } else if (
              result &&
              typeof result === 'object' &&
              result !== null &&
              'error' in result
            ) {
              const errorMessage = String(result.error);
              yield {
                type: GeminiEventType.Content,
                value: `[Error executing tool ${name}: ${errorMessage}]`,
              };
            } else {
              const status = confirmationDetails
                ? ToolCallStatus.Confirming
                : ToolCallStatus.Invoked;
              const evtValue: ToolCallEvent = {
                type: 'tool_call',
                status,
                callId,
                name,
                args,
                resultDisplay: result?.returnDisplay,
                confirmationDetails,
              };
              yield {
                type: GeminiEventType.ToolCallInfo,
                value: evtValue,
              };
            }
          }

          pendingToolCalls = [];

          const waitingOnConfirmations =
            toolExecutionOutcomes.filter(
              (outcome) => outcome.confirmationDetails,
            ).length > 0;
          if (waitingOnConfirmations) {
            // Stop processing content, wait for user.
            // TODO: Kill token processing once API supports signals.
            break;
          }

          functionResponseParts = toolExecutionOutcomes.map(
            (executedTool: ToolExecutionOutcome): Part => {
              const { name, result, error } = executedTool;
              const output = { output: result?.llmContent };
              let toolOutcomePayload: any;

              if (error) {
                const errorMessage = error?.message || String(error);
                toolOutcomePayload = {
                  error: `Invocation failed: ${errorMessage}`,
                };
                console.error(
                  `[Turn ${turns}] Critical error invoking tool ${name}:`,
                  error,
                );
              } else if (
                result &&
                typeof result === 'object' &&
                result !== null &&
                'error' in result
              ) {
                toolOutcomePayload = output;
                console.warn(
                  `[Turn ${turns}] Tool ${name} returned an error structure:`,
                  result.error,
                );
              } else {
                toolOutcomePayload = output;
              }

              return {
                functionResponse: {
                  name: name,
                  id: executedTool.callId,
                  response: toolOutcomePayload,
                },
              };
            },
          );
          currentMessageToSend = functionResponseParts;
        } else if (yieldedTextInTurn) {
          const history = chat.getHistory();
          const checkPrompt = `Analyze *only* the content and structure of your immediately preceding response (your last turn in the conversation history). Based *strictly* on that response, determine who should logically speak next: the 'user' or the 'model' (you).

**Decision Rules (apply in order):**

1.  **Model Continues:** If your last response explicitly states an immediate next action *you* intend to take (e.g., "Next, I will...", "Now I'll process...", "Moving on to analyze...", indicates an intended tool call that didn't execute), OR if the response seems clearly incomplete (cut off mid-thought without a natural conclusion), then the **'model'** should speak next.
2.  **Question to User:** If your last response ends with a direct question specifically addressed *to the user*, then the **'user'** should speak next.
3.  **Waiting for User:** If your last response completed a thought, statement, or task *and* does not meet the criteria for Rule 1 (Model Continues) or Rule 2 (Question to User), it implies a pause expecting user input or reaction. In this case, the **'user'** should speak next.

**Output Format:**

Respond *only* in JSON format according to the following schema. Do not include any text outside the JSON structure.

\`\`\`json
{
  "type": "object",
  "properties": {
    "reasoning": {
        "type": "string",
        "description": "Brief explanation justifying the 'next_speaker' choice based *strictly* on the applicable rule and the content/structure of the preceding turn."
    },
    "next_speaker": {
      "type": "string",
      "enum": ["user", "model"],
      "description": "Who should speak next based *only* on the preceding turn and the decision rules."
    }
  },
  "required": ["next_speaker", "reasoning"]
\`\`\`
}`;

          // Schema Idea
          const responseSchema: SchemaUnion = {
            type: Type.OBJECT,
            properties: {
              reasoning: {
                type: Type.STRING,
                description:
                  "Brief explanation justifying the 'next_speaker' choice based *strictly* on the applicable rule and the content/structure of the preceding turn.",
              },
              next_speaker: {
                type: Type.STRING,
                enum: ['user', 'model'], // Enforce the choices
                description:
                  'Who should speak next based *only* on the preceding turn and the decision rules',
              },
            },
            required: ['reasoning', 'next_speaker'],
          };

          try {
            // Use the new generateJson method, passing the history and the check prompt
            const parsedResponse = await this.generateJson(
              [
                ...history,
                {
                  role: 'user',
                  parts: [{ text: checkPrompt }],
                },
              ],
              responseSchema,
            );

            // Safely extract the next speaker value
            const nextSpeaker: string | undefined =
              typeof parsedResponse?.next_speaker === 'string'
                ? parsedResponse.next_speaker
                : undefined;

            if (nextSpeaker === 'model') {
              currentMessageToSend = { text: 'alright' }; // Or potentially a more meaningful continuation prompt
            } else {
              // 'user' should speak next, or value is missing/invalid. End the turn.
              break;
            }
          } catch (error) {
            console.error(
              `[Turn ${turns}] Failed to get or parse next speaker check:`,
              error,
            );
            // If the check fails, assume user should speak next to avoid infinite loops
            break;
          }
        } else {
          console.warn(
            `[Turn ${turns}] No text or function calls received from Gemini. Ending interaction.`,
          );
          break;
        }
      }

      if (turns >= this.MAX_TURNS) {
        console.warn(
          'sendMessageStream: Reached maximum tool call turns limit.',
        );
        yield {
          type: GeminiEventType.Content,
          value:
            '\n\n[System Notice: Maximum interaction turns reached. The conversation may be incomplete.]',
        };
      }
    } catch (error: unknown) {
      if (error instanceof Error && error.name === 'AbortError') {
        console.log('Gemini stream request aborted by user.');
        throw error;
      } else {
        console.error(`Error during Gemini stream or tool interaction:`, error);
        const message = error instanceof Error ? error.message : String(error);
        yield {
          type: GeminiEventType.Content,
          value: `\n\n[Error: An unexpected error occurred during the chat: ${message}]`,
        };
        throw error;
      }
    }
  }

  /**
   * Generates structured JSON content based on conversational history and a schema.
   * @param contents The conversational history (Content array) to provide context.
   * @param schema The SchemaUnion defining the desired JSON structure.
   * @returns A promise that resolves to the parsed JSON object matching the schema.
   * @throws Throws an error if the API call fails or the response is not valid JSON.
   */
  public async generateJson(
    contents: Content[],
    schema: SchemaUnion,
  ): Promise<any> {
    try {
      const result = await this.ai.models.generateContent({
        model: 'gemini-2.0-flash', // Using flash for potentially faster structured output
        config: {
          ...this.defaultHyperParameters,
          systemInstruction: CoreSystemPrompt,
          responseSchema: schema,
          responseMimeType: 'application/json',
        },
        contents: contents, // Pass the full Content array
      });

      const responseText = result.text;
      if (!responseText) {
        throw new Error('API returned an empty response.');
      }

      try {
        const parsedJson = JSON.parse(responseText);
        // TODO: Add schema validation if needed
        return parsedJson;
      } catch (parseError) {
        console.error('Failed to parse JSON response:', responseText);
        throw new Error(
          `Failed to parse API response as JSON: ${parseError instanceof Error ? parseError.message : String(parseError)}`,
        );
      }
    } catch (error) {
      console.error('Error generating JSON content:', error);
      const message =
        error instanceof Error ? error.message : 'Unknown API error.';
      throw new Error(`Failed to generate JSON content: ${message}`);
    }
  }
}