summaryrefslogtreecommitdiff
path: root/packages/server/src/tools/write-file.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/server/src/tools/write-file.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/server/src/tools/write-file.ts')
-rw-r--r--packages/server/src/tools/write-file.ts68
1 files changed, 63 insertions, 5 deletions
diff --git a/packages/server/src/tools/write-file.ts b/packages/server/src/tools/write-file.ts
index ce723061..814efa86 100644
--- a/packages/server/src/tools/write-file.ts
+++ b/packages/server/src/tools/write-file.ts
@@ -7,7 +7,14 @@
import fs from 'fs';
import path from 'path';
import * as Diff from 'diff'; // Keep for result generation
-import { BaseTool, ToolResult, FileDiff } from './tools.js'; // Updated import (Removed ToolResultDisplay)
+import {
+ BaseTool,
+ ToolResult,
+ FileDiff,
+ ToolEditConfirmationDetails,
+ ToolConfirmationOutcome,
+ ToolCallConfirmationDetails,
+} from './tools.js'; // Updated import (Removed ToolResultDisplay)
import { SchemaValidator } from '../utils/schemaValidator.js'; // Updated import
import { makeRelative, shortenPath } from '../utils/paths.js'; // Updated import
import { isNodeError } from '../utils/errors.js'; // Import isNodeError
@@ -30,16 +37,17 @@ export interface WriteFileToolParams {
/**
* Implementation of the WriteFile tool logic (moved from CLI)
*/
-export class WriteFileLogic extends BaseTool<WriteFileToolParams, ToolResult> {
+export class WriteFileTool extends BaseTool<WriteFileToolParams, ToolResult> {
static readonly Name: string = 'write_file';
+ private shouldAlwaysWrite = false;
private readonly rootDirectory: string;
constructor(rootDirectory: string) {
super(
- WriteFileLogic.Name,
- '', // Display name handled by CLI wrapper
- '', // Description handled by CLI wrapper
+ WriteFileTool.Name,
+ 'WriteFile',
+ 'Writes content to a specified file in the local filesystem.',
{
properties: {
file_path: {
@@ -98,6 +106,56 @@ export class WriteFileLogic extends BaseTool<WriteFileToolParams, ToolResult> {
return `Writing to ${shortenPath(relativePath)}`;
}
+ /**
+ * Handles the confirmation prompt for the WriteFile tool in the CLI.
+ */
+ async shouldConfirmExecute(
+ params: WriteFileToolParams,
+ ): Promise<ToolCallConfirmationDetails | false> {
+ if (this.shouldAlwaysWrite) {
+ return false;
+ }
+
+ const validationError = this.validateToolParams(params);
+ if (validationError) {
+ console.error(
+ `[WriteFile Wrapper] Attempted confirmation with invalid parameters: ${validationError}`,
+ );
+ return false;
+ }
+
+ const relativePath = makeRelative(params.file_path, this.rootDirectory);
+ const fileName = path.basename(params.file_path);
+
+ let currentContent = '';
+ try {
+ currentContent = fs.readFileSync(params.file_path, 'utf8');
+ } catch {
+ // File might not exist, that's okay for write/create
+ }
+
+ const fileDiff = Diff.createPatch(
+ fileName,
+ currentContent,
+ params.content,
+ 'Current',
+ 'Proposed',
+ { context: 3 },
+ );
+
+ const confirmationDetails: ToolEditConfirmationDetails = {
+ title: `Confirm Write: ${shortenPath(relativePath)}`,
+ fileName,
+ fileDiff,
+ onConfirm: async (outcome: ToolConfirmationOutcome) => {
+ if (outcome === ToolConfirmationOutcome.ProceedAlways) {
+ this.shouldAlwaysWrite = true;
+ }
+ },
+ };
+ return confirmationDetails;
+ }
+
async execute(params: WriteFileToolParams): Promise<ToolResult> {
const validationError = this.validateParams(params);
if (validationError) {