diff options
Diffstat (limited to 'packages/cli/src/core')
| -rw-r--r-- | packages/cli/src/core/gemini-client.test.ts | 165 | ||||
| -rw-r--r-- | packages/cli/src/core/gemini-client.ts | 282 | ||||
| -rw-r--r-- | packages/cli/src/core/gemini-stream.ts | 183 | ||||
| -rw-r--r-- | packages/cli/src/core/history-updater.ts | 4 | ||||
| -rw-r--r-- | packages/cli/src/core/prompts.ts | 100 | ||||
| -rw-r--r-- | packages/cli/src/core/turn.ts | 233 |
6 files changed, 11 insertions, 956 deletions
diff --git a/packages/cli/src/core/gemini-client.test.ts b/packages/cli/src/core/gemini-client.test.ts deleted file mode 100644 index 4651529e..00000000 --- a/packages/cli/src/core/gemini-client.test.ts +++ /dev/null @@ -1,165 +0,0 @@ -import { describe, it, expect, vi, beforeEach, Mock } from 'vitest'; -import { GoogleGenAI, Type, Content } from '@google/genai'; -import { GeminiClient } from './gemini-client.js'; -import { Config } from '../config/config.js'; - -// Mock the entire @google/genai module -vi.mock('@google/genai'); - -// Mock the Config class and its methods -vi.mock('../config/config.js', () => { - // The mock constructor should accept the arguments but not explicitly return an object. - // vi.fn() will create a mock instance that inherits from the prototype. - const MockConfig = vi.fn(); - // Methods are mocked on the prototype, so instances will inherit them. - MockConfig.prototype.getApiKey = vi.fn(() => 'mock-api-key'); - MockConfig.prototype.getModel = vi.fn(() => 'mock-model'); - MockConfig.prototype.getTargetDir = vi.fn(() => 'mock-target-dir'); - return { Config: MockConfig }; -}); - -// Define a type for the mocked GoogleGenAI instance structure -type MockGoogleGenAIType = { - models: { - generateContent: Mock; - }; - chats: { - create: Mock; - }; -}; - -describe('GeminiClient', () => { - // Use the specific types defined above - let mockGenerateContent: MockGoogleGenAIType['models']['generateContent']; - let mockGoogleGenAIInstance: MockGoogleGenAIType; - let config: Config; - let client: GeminiClient; - - beforeEach(() => { - vi.clearAllMocks(); - - // Mock the generateContent method specifically - mockGenerateContent = vi.fn(); - - // Mock the chainable structure ai.models.generateContent - mockGoogleGenAIInstance = { - models: { - generateContent: mockGenerateContent, - }, - chats: { - create: vi.fn(), // Mock create as well - }, - }; - - // Configure the mocked GoogleGenAI constructor to return our mock instance - (GoogleGenAI as Mock).mockImplementation(() => mockGoogleGenAIInstance); - - config = new Config('mock-api-key-arg', 'mock-model-arg', 'mock-dir-arg'); - client = new GeminiClient(config); - }); - - describe('generateJson', () => { - it('should call ai.models.generateContent with correct parameters', async () => { - const mockContents: Content[] = [ - { role: 'user', parts: [{ text: 'test prompt' }] }, - ]; - const mockSchema = { - type: Type.OBJECT, - properties: { key: { type: Type.STRING } }, - }; - const mockApiResponse = { text: JSON.stringify({ key: 'value' }) }; - - mockGenerateContent.mockResolvedValue(mockApiResponse); - await client.generateJson(mockContents, mockSchema); - - expect(mockGenerateContent).toHaveBeenCalledTimes(1); - - // Use expect.objectContaining for the config assertion - const expectedConfigMatcher = expect.objectContaining({ - temperature: 0, - topP: 1, - systemInstruction: expect.any(String), - responseSchema: mockSchema, - responseMimeType: 'application/json', - }); - expect(mockGenerateContent).toHaveBeenCalledWith({ - model: 'mock-model', - config: expectedConfigMatcher, - contents: mockContents, - }); - }); - - it('should return the parsed JSON response', async () => { - const mockContents: Content[] = [ - { role: 'user', parts: [{ text: 'test prompt' }] }, - ]; - const mockSchema = { - type: Type.OBJECT, - properties: { key: { type: Type.STRING } }, - }; - const expectedJson = { key: 'value' }; - const mockApiResponse = { text: JSON.stringify(expectedJson) }; - - mockGenerateContent.mockResolvedValue(mockApiResponse); - - const result = await client.generateJson(mockContents, mockSchema); - - expect(result).toEqual(expectedJson); - }); - - it('should throw an error if API returns empty response', async () => { - const mockContents: Content[] = [ - { role: 'user', parts: [{ text: 'test prompt' }] }, - ]; - const mockSchema = { - type: Type.OBJECT, - properties: { key: { type: Type.STRING } }, - }; - const mockApiResponse = { text: '' }; // Empty response - - mockGenerateContent.mockResolvedValue(mockApiResponse); - - await expect( - client.generateJson(mockContents, mockSchema), - ).rejects.toThrow( - 'Failed to generate JSON content: API returned an empty response.', - ); - }); - - it('should throw an error if API response is not valid JSON', async () => { - const mockContents: Content[] = [ - { role: 'user', parts: [{ text: 'test prompt' }] }, - ]; - const mockSchema = { - type: Type.OBJECT, - properties: { key: { type: Type.STRING } }, - }; - const mockApiResponse = { text: 'invalid json' }; // Invalid JSON - - mockGenerateContent.mockResolvedValue(mockApiResponse); - - await expect( - client.generateJson(mockContents, mockSchema), - ).rejects.toThrow('Failed to parse API response as JSON:'); - }); - - it('should throw an error if generateContent rejects', async () => { - const mockContents: Content[] = [ - { role: 'user', parts: [{ text: 'test prompt' }] }, - ]; - const mockSchema = { - type: Type.OBJECT, - properties: { key: { type: Type.STRING } }, - }; - const apiError = new Error('API call failed'); - - mockGenerateContent.mockRejectedValue(apiError); - - await expect( - client.generateJson(mockContents, mockSchema), - ).rejects.toThrow(`Failed to generate JSON content: ${apiError.message}`); - }); - }); - - // TODO: Add tests for startChat and sendMessageStream later -}); diff --git a/packages/cli/src/core/gemini-client.ts b/packages/cli/src/core/gemini-client.ts deleted file mode 100644 index 19dba40f..00000000 --- a/packages/cli/src/core/gemini-client.ts +++ /dev/null @@ -1,282 +0,0 @@ -/** - * @license - * Copyright 2025 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import { - GenerateContentConfig, - GoogleGenAI, - Part, - Chat, - Type, - SchemaUnion, - PartListUnion, - Content, -} from '@google/genai'; -import { CoreSystemPrompt } from './prompts.js'; -import process from 'node:process'; -import { toolRegistry } from '../tools/tool-registry.js'; -import { getFolderStructure } from '../utils/getFolderStructure.js'; -import { GeminiEventType, GeminiStream } from './gemini-stream.js'; -import { Config } from '../config/config.js'; -import { Turn } from './turn.js'; - -export class GeminiClient { - private config: Config; - private ai: GoogleGenAI; - private generateContentConfig: GenerateContentConfig = { - temperature: 0, - topP: 1, - }; - private readonly MAX_TURNS = 100; - - constructor(config: Config) { - this.config = config; - this.ai = new GoogleGenAI({ apiKey: config.getApiKey() }); - } - - private async getEnvironment(): Promise<Part> { - const cwd = process.cwd(); - const today = new Date().toLocaleDateString(undefined, { - weekday: 'long', - year: 'numeric', - month: 'long', - day: 'numeric', - }); - const platform = process.platform; - - const folderStructure = await getFolderStructure(cwd); - - const context = ` - 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(); - - return { text: context }; - } - - async startChat(): Promise<Chat> { - const envPart = await this.getEnvironment(); - const model = this.config.getModel(); - const tools = toolRegistry.getToolSchemas(); - - try { - const chat = this.ai.chats.create({ - model, - config: { - systemInstruction: CoreSystemPrompt, - ...this.generateContentConfig, - tools, - }, - history: [ - // --- Add the context as a single part in the initial user message --- - { - role: 'user', - parts: [envPart], // 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}`); - } - } - - async *sendMessageStream( - chat: Chat, - request: PartListUnion, - signal?: AbortSignal, - ): GeminiStream { - let turns = 0; - - try { - while (turns < this.MAX_TURNS) { - turns++; - // A turn either yields a text response or returns - // function responses to be used in the next turn. - // This callsite is responsible to handle the buffered - // function responses and use it on the next turn. - const turn = new Turn(chat); - const resultStream = turn.run(request, signal); - - for await (const event of resultStream) { - yield event; - } - const fnResponses = turn.getFunctionResponses(); - if (fnResponses.length > 0) { - request = fnResponses; - continue; // use the responses in the next turn - } - - 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') { - request = { 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; - } - } - - 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) { - // TODO(jbd): There is so much of packing/unpacking of error types. - // Figure out a way to remove the redundant work. - 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. - */ - async generateJson( - contents: Content[], - schema: SchemaUnion, - ): Promise<Record<string, unknown>> { - const model = this.config.getModel(); - try { - const result = await this.ai.models.generateContent({ - model, - config: { - ...this.generateContentConfig, - systemInstruction: CoreSystemPrompt, - responseSchema: schema, - responseMimeType: 'application/json', - }, - 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}`); - } - } -} diff --git a/packages/cli/src/core/gemini-stream.ts b/packages/cli/src/core/gemini-stream.ts index 182291f7..d41c50d7 100644 --- a/packages/cli/src/core/gemini-stream.ts +++ b/packages/cli/src/core/gemini-stream.ts @@ -4,181 +4,16 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { ToolCallEvent, HistoryItem } from '../ui/types.js'; -import { Part } from '@google/genai'; -import { - handleToolCallChunk, - addErrorMessageToHistory, -} from './history-updater.js'; - -export enum GeminiEventType { - Content, - ToolCallInfo, -} - -export interface GeminiContentEvent { - type: GeminiEventType.Content; - value: string; -} - -export interface GeminiToolCallInfoEvent { - type: GeminiEventType.ToolCallInfo; - value: ToolCallEvent; -} - -export type GeminiEvent = GeminiContentEvent | GeminiToolCallInfoEvent; - -export type GeminiStream = AsyncIterable<GeminiEvent>; - +// Only defining the state enum needed by the UI export enum StreamingState { - Idle, - Responding, + Idle = 'idle', + Responding = 'responding', + WaitingForConfirmation = 'waiting_for_confirmation', } -interface StreamProcessorParams { - stream: GeminiStream; - signal: AbortSignal; - setHistory: React.Dispatch<React.SetStateAction<HistoryItem[]>>; - submitQuery: (query: Part) => Promise<void>; - getNextMessageId: () => number; - addHistoryItem: (itemData: Omit<HistoryItem, 'id'>, id: number) => void; - currentToolGroupIdRef: React.MutableRefObject<number | null>; +// Copied from server/src/core/turn.ts for CLI usage +export enum GeminiEventType { + Content = 'content', + ToolCallRequest = 'tool_call_request', + // Add other event types if the UI hook needs to handle them } - -/** - * Processes the Gemini stream, managing text buffering, adaptive rendering, - * and delegating history updates for tool calls and errors. - */ -export const processGeminiStream = async ({ - // Renamed function for clarity - stream, - signal, - setHistory, - submitQuery, - getNextMessageId, - addHistoryItem, - currentToolGroupIdRef, -}: StreamProcessorParams): Promise<void> => { - // --- State specific to this stream processing invocation --- - let textBuffer = ''; - let renderTimeoutId: NodeJS.Timeout | null = null; - let isStreamComplete = false; - let currentGeminiMessageId: number | null = null; - - const render = (content: string) => { - if (currentGeminiMessageId === null) { - return; - } - setHistory((prev) => - prev.map((item) => - item.id === currentGeminiMessageId && item.type === 'gemini' - ? { ...item, text: (item.text ?? '') + content } - : item, - ), - ); - }; - // --- Adaptive Rendering Logic (nested) --- - const renderBufferedText = () => { - if (signal.aborted) { - if (renderTimeoutId) clearTimeout(renderTimeoutId); - renderTimeoutId = null; - return; - } - - const bufferLength = textBuffer.length; - let chunkSize = 0; - let delay = 50; - - if (bufferLength > 150) { - chunkSize = Math.min(bufferLength, 30); - delay = 5; - } else if (bufferLength > 30) { - chunkSize = Math.min(bufferLength, 10); - delay = 10; - } else if (bufferLength > 0) { - chunkSize = 2; - delay = 20; - } - - if (chunkSize > 0) { - const chunkToRender = textBuffer.substring(0, chunkSize); - textBuffer = textBuffer.substring(chunkSize); - render(chunkToRender); - - renderTimeoutId = setTimeout(renderBufferedText, delay); - } else { - renderTimeoutId = null; // Clear timeout ID if nothing to render - if (!isStreamComplete) { - // Buffer empty, but stream might still send data, check again later - renderTimeoutId = setTimeout(renderBufferedText, 50); - } - } - }; - - const scheduleRender = () => { - if (renderTimeoutId === null) { - renderTimeoutId = setTimeout(renderBufferedText, 0); - } - }; - - // --- Stream Processing Loop --- - try { - for await (const chunk of stream) { - if (signal.aborted) break; - - if (chunk.type === GeminiEventType.Content) { - currentToolGroupIdRef.current = null; // Reset tool group on text - - if (currentGeminiMessageId === null) { - currentGeminiMessageId = getNextMessageId(); - addHistoryItem({ type: 'gemini', text: '' }, currentGeminiMessageId); - textBuffer = ''; - } - textBuffer += chunk.value; - scheduleRender(); - } else if (chunk.type === GeminiEventType.ToolCallInfo) { - if (renderTimeoutId) { - // Stop rendering loop - clearTimeout(renderTimeoutId); - renderTimeoutId = null; - } - // Flush any text buffer content. - render(textBuffer); - currentGeminiMessageId = null; // End text message context - textBuffer = ''; // Clear buffer - - // Delegate history update for tool call - handleToolCallChunk( - chunk.value, - setHistory, - submitQuery, - getNextMessageId, - currentToolGroupIdRef, - ); - } - } - if (signal.aborted) { - throw new Error('Request cancelled by user'); - } - } catch (error: unknown) { - if (renderTimeoutId) { - // Ensure render loop stops on error - clearTimeout(renderTimeoutId); - renderTimeoutId = null; - } - // Delegate history update for error message - addErrorMessageToHistory( - error as Error | DOMException, - setHistory, - getNextMessageId, - ); - } finally { - isStreamComplete = true; // Signal stream end for render loop completion - if (renderTimeoutId) { - clearTimeout(renderTimeoutId); - renderTimeoutId = null; - } - - renderBufferedText(); // Force final render - } -}; diff --git a/packages/cli/src/core/history-updater.ts b/packages/cli/src/core/history-updater.ts index 5363bfce..f56e76ca 100644 --- a/packages/cli/src/core/history-updater.ts +++ b/packages/cli/src/core/history-updater.ts @@ -54,9 +54,9 @@ export const handleToolCallChunk = ( handleToolCallChunk( { ...chunk, - status: ToolCallStatus.Canceled, + status: ToolCallStatus.Error, confirmationDetails: undefined, - resultDisplay, + resultDisplay: resultDisplay ?? 'Canceled by user.', }, setHistory, submitQuery, diff --git a/packages/cli/src/core/prompts.ts b/packages/cli/src/core/prompts.ts deleted file mode 100644 index 09df9f59..00000000 --- a/packages/cli/src/core/prompts.ts +++ /dev/null @@ -1,100 +0,0 @@ -/** - * @license - * Copyright 2025 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import { ReadFileTool } from '../tools/read-file.tool.js'; -import { TerminalTool } from '../tools/terminal.tool.js'; - -const MEMORY_FILE_NAME = 'GEMINI.md'; - -const contactEmail = '[email protected]'; -export const CoreSystemPrompt = ` -You are an interactive CLI tool assistant specializing in software engineering tasks. Your primary goal is to help users safely and efficiently, adhering strictly to the following instructions and utilizing your available tools. - -# Core Directives & Safety Rules -1. **Explain Critical Commands:** Before executing any command (especially using \`${TerminalTool.Name}\`) that modifies the file system, codebase, or system state, you *must* provide a brief explanation of the command's purpose and potential impact. Prioritize user understanding and safety. -2. **NEVER Commit Changes:** Unless explicitly instructed by the user to do so, you MUST NOT commit changes to version control (e.g., git commit). This is critical for user control over their repository. -3. **Security First:** Always apply security best practices. Never introduce code that exposes, logs, or commits secrets, API keys, or other sensitive information. - -# Primary Workflow: Software Engineering Tasks -When requested to perform tasks like fixing bugs, adding features, refactoring, or explaining code, follow this sequence: -1. **Understand:** Analyze the user's request and the relevant codebase context. Check for project-specific information in \`${MEMORY_FILE_NAME}\` if it exists. Use search tools extensively (in parallel if independent) to understand file structures, existing code patterns, and conventions. -2. **Implement:** Use the available tools (e.g., file editing, \`${TerminalTool.Name}\`) to construct the solution, strictly adhering to the project's established conventions (see 'Following Conventions' below). - - If creating a new project rely on scaffolding commands do lay out the initial project structure (i.e. npm init ...) -3. **Verify (Tests):** If applicable and feasible, verify the changes using the project's testing procedures. Identify the correct test commands and frameworks by examining \`README\` files, \`${MEMORY_FILE_NAME}\`, build/package configuration (e.g., \`package.json\`), or existing test execution patterns. NEVER assume standard test commands. -4. **Verify (Standards):** VERY IMPORTANT: After making code changes, execute the project-specific linting and type-checking commands (e.g., \`npm run lint\`, \`ruff check .\`, \`tsc\`) that you have identified for this project (or obtained from the user). This ensures code quality and adherence to standards. If unsure about these commands, ask the user and propose adding them to \`${MEMORY_FILE_NAME}\` for future reference. - -# Key Operating Principles - -## Following Conventions -Rigorously adhere to existing project conventions when reading or modifying code. Analyze surrounding code and configuration first. -- **Libraries/Frameworks:** NEVER assume a library/framework is available or appropriate. Verify its established usage within the project (check imports, configuration files like \`package.json\`, \`Cargo.toml\`, \`requirements.txt\`, \`build.gradle\`, etc., or observe neighboring files) before employing it. -- **Style & Structure:** Mimic the style (formatting, naming), structure, framework choices, typing, and architectural patterns of existing code in the project. -- **Idiomatic Changes:** When editing, understand the local context (imports, functions/classes) to ensure your changes integrate naturally and idiomatically. -- **Comments:** Add code comments sparingly. Focus on *why* something is done, especially for complex logic, rather than *what* is done. Only add comments if necessary for clarity or if requested by the user. - -## Memory (${MEMORY_FILE_NAME}) -Utilize the \`${MEMORY_FILE_NAME}\` file in the current working directory for project-specific context: -- Reference stored commands, style preferences, and codebase notes when performing tasks. -- When you discover frequently used commands (build, test, lint, typecheck) or learn about specific project conventions or style preferences, proactively propose adding them to \`${MEMORY_FILE_NAME}\` for future sessions. - -## Tone and Style (CLI Interaction) -- **Concise & Direct:** Adopt a professional, direct, and concise tone suitable for a CLI environment. -- **Minimal Output:** Aim for fewer than 4 lines of text output (excluding tool use/code generation) per response whenever practical. Focus strictly on the user's query. -- **Clarity over Brevity (When Needed):** While conciseness is key, prioritize clarity for essential explanations (like pre-command warnings) or when seeking necessary clarification if a request is ambiguous. -- **No Chitchat:** Avoid conversational filler, preambles ("Okay, I will now..."), or postambles ("I have finished the changes..."). Get straight to the action or answer. -- **Formatting:** Use GitHub-flavored Markdown. Responses will be rendered in monospace. -- **Tools vs. Text:** Use tools for actions, text output *only* for communication. Do not add explanatory comments within tool calls or code blocks unless specifically part of the required code/command itself. -- **Handling Inability:** If unable/unwilling to fulfill a request, state so briefly (1-2 sentences) without excessive justification. Offer alternatives if appropriate. - -## Proactiveness -- **Act within Scope:** Fulfill the user's request thoroughly, including reasonable, directly implied follow-up actions. -- **Confirm Ambiguity/Expansion:** Do not take significant actions beyond the clear scope of the request without confirming with the user. If asked *how* to do something, explain first, don't just do it. -- **Stop After Action:** After completing a code modification or file operation, simply stop. Do not provide summaries unless asked. - -# Tool Usage -- **Search:** Prefer the Agent tool for file searching to optimize context usage. -- **Parallelism:** Execute multiple independent tool calls in parallel when feasible. -- **Command Execution:** Use the \`${TerminalTool.Name}\` tool for running shell commands, remembering the safety rule to explain modifying commands first. - -# Interaction Details -- **Help Command:** Use \`/help\` to display Gemini Code help. To get specific command/flag info, execute \`gemini -h\` via \`${TerminalTool.Name}\` and show the output. -- **Synthetic Messages:** Ignore system messages like \`++Request Cancelled++\`. Do not generate them. -- **Feedback:** Direct feedback to ${contactEmail}. - -# Examples (Illustrating Tone and Workflow) -<example> -user: 1 + 2 -assistant: 3 -</example> - -<example> -user: is 13 a prime number? -assistant: true -</example> - -<example> -user: List files here. -assistant: [tool_call: execute_bash_command for 'ls -la']))] -</example> - -<example> -user: Refactor the auth logic in src/auth.py to use the 'requests' library. -assistant: Okay, I see src/auth.py currently uses 'urllib'. Before changing it, I need to check if 'requests' is already a project dependency. [tool_call: ${TerminalTool.Name} for grep 'requests', 'requirements.txt'] -(After confirming dependency or asking user to add it) -Okay, 'requests' is available. I will now refactor src/auth.py. -[tool_call: Uses read, edit tools following conventions] -(After editing) -[tool_call: Runs project-specific lint/typecheck commands found previously, e.g., ${TerminalTool.Name} for 'ruff', 'check', 'src/auth.py'] -</example> - -<example> -user: Delete the temp directory. -assistant: I can run \`rm -rf ./temp\`. This will permanently delete the directory and all its contents. Is it okay to proceed? -</example> - -# Final Reminder -Your core function is efficient and safe assistance. Balance extreme conciseness with the crucial need for clarity, especially regarding safety and potential system modifications. Always prioritize user control and project conventions. Never make assumptions on the contents of files; instead use the ${ReadFileTool.Name} to ensure you aren't making too broad of assumptions. -`; diff --git a/packages/cli/src/core/turn.ts b/packages/cli/src/core/turn.ts deleted file mode 100644 index e8c4ef78..00000000 --- a/packages/cli/src/core/turn.ts +++ /dev/null @@ -1,233 +0,0 @@ -import { - Part, - Chat, - PartListUnion, - GenerateContentResponse, - FunctionCall, -} from '@google/genai'; -import { - type ToolCallConfirmationDetails, - ToolCallStatus, - ToolCallEvent, -} from '../ui/types.js'; -import { ToolResult } from '../tools/tools.js'; -import { toolRegistry } from '../tools/tool-registry.js'; -import { GeminiEventType, GeminiStream } from './gemini-stream.js'; - -export type ToolExecutionOutcome = { - callId: string; - name: string; - args: Record<string, never>; - result?: ToolResult; - error?: Error; - confirmationDetails?: ToolCallConfirmationDetails; -}; - -// TODO(jbd): Move ToolExecutionOutcome to somewhere else? - -// A turn manages the agentic loop turn. -// Turn.run emits throught the turn events that could be used -// as immediate feedback to the user. -export class Turn { - private readonly chat: Chat; - private pendingToolCalls: Array<{ - callId: string; - name: string; - args: Record<string, never>; - }>; - private fnResponses: Part[]; - private debugResponses: GenerateContentResponse[]; - - constructor(chat: Chat) { - this.chat = chat; - this.pendingToolCalls = []; - this.fnResponses = []; - this.debugResponses = []; - } - - async *run(req: PartListUnion, signal?: AbortSignal): GeminiStream { - const responseStream = await this.chat.sendMessageStream({ - message: req, - }); - for await (const resp of responseStream) { - this.debugResponses.push(resp); - if (signal?.aborted) { - throw this.abortError(); - } - if (resp.text) { - yield { - type: GeminiEventType.Content, - value: resp.text, - }; - continue; - } - if (!resp.functionCalls) { - continue; - } - for (const fnCall of resp.functionCalls) { - for await (const event of this.handlePendingFunctionCall(fnCall)) { - yield event; - } - } - - // Create promises to be able to wait for executions to complete. - const toolPromises = this.pendingToolCalls.map( - async (pendingToolCall) => { - const tool = toolRegistry.getTool(pendingToolCall.name); - if (!tool) { - return { - ...pendingToolCall, - error: new Error( - `Tool "${pendingToolCall.name}" not found or is not registered.`, - ), - }; - } - const shouldConfirm = await tool.shouldConfirmExecute( - pendingToolCall.args, - ); - if (shouldConfirm) { - return { - // TODO(jbd): Should confirm isn't confirmation details. - ...pendingToolCall, - confirmationDetails: shouldConfirm, - }; - } - const result = await tool.execute(pendingToolCall.args); - return { ...pendingToolCall, result }; - }, - ); - const outcomes = await Promise.all(toolPromises); - for await (const event of this.handleToolOutcomes(outcomes)) { - yield event; - } - this.pendingToolCalls = []; - - // TODO(jbd): Make it harder for the caller to ignore the - // buffered function responses. - this.fnResponses = this.buildFunctionResponses(outcomes); - } - } - - private async *handlePendingFunctionCall(fnCall: FunctionCall): GeminiStream { - const callId = - fnCall.id ?? - `${fnCall.name}-${Date.now()}-${Math.random().toString(16).slice(2)}`; - // TODO(jbd): replace with uuid. - const name = fnCall.name || 'undefined_tool_name'; - const args = (fnCall.args || {}) as Record<string, never>; - - this.pendingToolCalls.push({ callId, name, args }); - const value: ToolCallEvent = { - type: 'tool_call', - status: ToolCallStatus.Pending, - callId, - name, - args, - resultDisplay: undefined, - confirmationDetails: undefined, - }; - yield { - type: GeminiEventType.ToolCallInfo, - value, - }; - } - - private async *handleToolOutcomes( - outcomes: ToolExecutionOutcome[], - ): GeminiStream { - for (const outcome of outcomes) { - const { callId, name, args, result, error, confirmationDetails } = - outcome; - if (error) { - // TODO(jbd): Error handling needs a cleanup. - const errorMessage = error?.message || String(error); - yield { - type: GeminiEventType.Content, - value: `[Error invoking tool ${name}: ${errorMessage}]`, - }; - return; - } - 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}]`, - }; - return; - } - const status = confirmationDetails - ? ToolCallStatus.Confirming - : ToolCallStatus.Invoked; - const value: ToolCallEvent = { - type: 'tool_call', - status, - callId, - name, - args, - resultDisplay: result?.returnDisplay, - confirmationDetails, - }; - yield { - type: GeminiEventType.ToolCallInfo, - value, - }; - } - } - - private buildFunctionResponses(outcomes: ToolExecutionOutcome[]): Part[] { - return outcomes.map((outcome: ToolExecutionOutcome): Part => { - const { name, result, error } = outcome; - const output = { output: result?.llmContent }; - let fnResponse: Record<string, unknown>; - - if (error) { - const errorMessage = error?.message || String(error); - fnResponse = { - error: `Invocation failed: ${errorMessage}`, - }; - console.error(`[Turn] Critical error invoking tool ${name}:`, error); - } else if ( - result && - typeof result === 'object' && - result !== null && - 'error' in result - ) { - fnResponse = output; - console.warn( - `[Turn] Tool ${name} returned an error structure:`, - result.error, - ); - } else { - fnResponse = output; - } - - return { - functionResponse: { - name, - id: outcome.callId, - response: fnResponse, - }, - }; - }); - } - - private abortError(): Error { - // TODO(jbd): Move it out of this class. - const error = new Error('Request cancelled by user during stream.'); - error.name = 'AbortError'; - throw error; - } - - getFunctionResponses(): Part[] { - return this.fnResponses; - } - - getDebugResponses(): GenerateContentResponse[] { - return this.debugResponses; - } -} |
