summaryrefslogtreecommitdiff
path: root/packages/cli/src/tools/edit.tool.ts
diff options
context:
space:
mode:
authorTaylor Mullen <[email protected]>2025-04-21 10:53:11 -0400
committerN. Taylor Mullen <[email protected]>2025-04-21 11:07:09 -0400
commit81f0f618f7ecc439e67447bf98065d87e22483c0 (patch)
tree82851f180c8ba5263427e3586bc738590fc49153 /packages/cli/src/tools/edit.tool.ts
parente351baf10f06d2a1d1872bf2a6d7e9e709effed9 (diff)
Fix Gemini Code's (GC) smarts.
- The tl;dr; is that GC couldn't see what the user was saying when tool call events happened in response. The rason why this was happening was because we were instantly invoking tools that the model told us to invoke and then instantly re-requesting. This resulted in the bug because the genai APIs can't update the chat history before a full response has been completed (doesn't know how to update if it's incomplete). - To address the above issue I had to do quite the large refactor. The gist is that now turns truly drive everything on the server (vs. a server client split). This ensured that when we got tool invocations we could control when/how re-requesting would happen and then also ensure that history was updated. This change also meant that the server would act as an event publisher to enable the client to react to events rather than try and weave in complex logic between the events. - A BIG change that this changeset incudes is the removal of all of the CLI tools in favor of the server tools. - Removed some dead code as part of this - **NOTE: Confirmations are still broken (they were broken prior to this); however, I've set them up to be able to work in the future, I'll dot hat in a follow up to be less breaking to others.** Fixes https://b.corp.google.com/issues/412320087
Diffstat (limited to 'packages/cli/src/tools/edit.tool.ts')
-rw-r--r--packages/cli/src/tools/edit.tool.ts146
1 files changed, 0 insertions, 146 deletions
diff --git a/packages/cli/src/tools/edit.tool.ts b/packages/cli/src/tools/edit.tool.ts
deleted file mode 100644
index 75bb59a8..00000000
--- a/packages/cli/src/tools/edit.tool.ts
+++ /dev/null
@@ -1,146 +0,0 @@
-/**
- * @license
- * Copyright 2025 Google LLC
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import fs from 'fs';
-import path from 'path';
-import {
- EditLogic,
- EditToolParams,
- ToolResult,
- makeRelative,
- shortenPath,
- isNodeError,
-} from '@gemini-code/server';
-import { BaseTool } from './tools.js';
-import {
- ToolCallConfirmationDetails,
- ToolConfirmationOutcome,
- ToolEditConfirmationDetails,
-} from '../ui/types.js';
-import * as Diff from 'diff';
-
-/**
- * CLI wrapper for the Edit tool.
- * Handles confirmation prompts and potentially UI-specific state like 'Always Edit'.
- */
-export class EditTool extends BaseTool<EditToolParams, ToolResult> {
- static readonly Name: string = EditLogic.Name;
- private coreLogic: EditLogic;
- private shouldAlwaysEdit = false;
-
- /**
- * Creates a new instance of the EditTool CLI wrapper
- * @param rootDirectory Root directory to ground this tool in.
- */
- constructor(rootDirectory: string) {
- const coreLogicInstance = new EditLogic(rootDirectory);
- super(
- EditTool.Name,
- 'Edit',
- `Replaces a SINGLE, UNIQUE occurrence of text within a file. Requires providing significant context around the change to ensure uniqueness. For moving/renaming files, use the Bash tool with \`mv\`. For replacing entire file contents or creating new files use the WriteFile tool. Always use the ReadFile tool to examine the file before using this tool.`,
- (coreLogicInstance.schema.parameters as Record<string, unknown>) ?? {},
- );
- this.coreLogic = coreLogicInstance;
- }
-
- /**
- * Delegates validation to the core logic
- */
- validateToolParams(params: EditToolParams): string | null {
- return this.coreLogic.validateParams(params);
- }
-
- /**
- * Delegates getting description to the core logic
- */
- getDescription(params: EditToolParams): string {
- return this.coreLogic.getDescription(params);
- }
-
- /**
- * Handles the confirmation prompt for the Edit tool in the CLI.
- * It needs to calculate the diff to show the user.
- */
- async shouldConfirmExecute(
- params: EditToolParams,
- ): Promise<ToolCallConfirmationDetails | false> {
- if (this.shouldAlwaysEdit) {
- return false;
- }
- const validationError = this.validateToolParams(params);
- if (validationError) {
- console.error(
- `[EditTool Wrapper] Attempted confirmation with invalid parameters: ${validationError}`,
- );
- return false;
- }
- let currentContent: string | null = null;
- let fileExists = false;
- let newContent = '';
- try {
- currentContent = fs.readFileSync(params.file_path, 'utf8');
- fileExists = true;
- } catch (err: unknown) {
- if (isNodeError(err) && err.code === 'ENOENT') {
- fileExists = false;
- } else {
- console.error(`Error reading file for confirmation diff: ${err}`);
- return false;
- }
- }
- if (params.old_string === '' && !fileExists) {
- newContent = params.new_string;
- } else if (!fileExists) {
- return false;
- } else if (currentContent !== null) {
- const occurrences = this.coreLogic['countOccurrences'](
- currentContent,
- params.old_string,
- );
- const expectedReplacements =
- params.expected_replacements === undefined
- ? 1
- : params.expected_replacements;
- if (occurrences === 0 || occurrences !== expectedReplacements) {
- return false;
- }
- newContent = this.coreLogic['replaceAll'](
- currentContent,
- params.old_string,
- params.new_string,
- );
- } else {
- return false;
- }
- const fileName = path.basename(params.file_path);
- const fileDiff = Diff.createPatch(
- fileName,
- currentContent ?? '',
- newContent,
- 'Current',
- 'Proposed',
- { context: 3 },
- );
- const confirmationDetails: ToolEditConfirmationDetails = {
- title: `Confirm Edit: ${shortenPath(makeRelative(params.file_path, this.coreLogic['rootDirectory']))}`,
- fileName,
- fileDiff,
- onConfirm: async (outcome: ToolConfirmationOutcome) => {
- if (outcome === ToolConfirmationOutcome.ProceedAlways) {
- this.shouldAlwaysEdit = true;
- }
- },
- };
- return confirmationDetails;
- }
-
- /**
- * Delegates execution to the core logic
- */
- async execute(params: EditToolParams): Promise<ToolResult> {
- return this.coreLogic.execute(params);
- }
-}