[Fix Telemetry for tool calls, PR 1/n] Propagate tool reported errors via ToolCallResponseInfo and ToolResult (#5222)

This commit is contained in:
Silvio Junior
2025-08-01 11:20:08 -04:00
committed by GitHub
parent e126d2fcd9
commit 7748e56153
14 changed files with 224 additions and 25 deletions

View File

@@ -17,6 +17,7 @@ import {
ToolResult,
ToolResultDisplay,
} from './tools.js';
import { ToolErrorType } from './tool-error.js';
import { Type } from '@google/genai';
import { SchemaValidator } from '../utils/schemaValidator.js';
import { makeRelative, shortenPath } from '../utils/paths.js';
@@ -62,7 +63,7 @@ interface CalculatedEdit {
currentContent: string | null;
newContent: string;
occurrences: number;
error?: { display: string; raw: string };
error?: { display: string; raw: string; type: ToolErrorType };
isNewFile: boolean;
}
@@ -191,7 +192,9 @@ Expectation for required parameters:
let finalNewString = params.new_string;
let finalOldString = params.old_string;
let occurrences = 0;
let error: { display: string; raw: string } | undefined = undefined;
let error:
| { display: string; raw: string; type: ToolErrorType }
| undefined = undefined;
try {
currentContent = fs.readFileSync(params.file_path, 'utf8');
@@ -214,6 +217,7 @@ Expectation for required parameters:
error = {
display: `File not found. Cannot apply edit. Use an empty old_string to create a new file.`,
raw: `File not found: ${params.file_path}`,
type: ToolErrorType.FILE_NOT_FOUND,
};
} else if (currentContent !== null) {
// Editing an existing file
@@ -233,11 +237,13 @@ Expectation for required parameters:
error = {
display: `Failed to edit. Attempted to create a file that already exists.`,
raw: `File already exists, cannot create: ${params.file_path}`,
type: ToolErrorType.ATTEMPT_TO_CREATE_EXISTING_FILE,
};
} else if (occurrences === 0) {
error = {
display: `Failed to edit, could not find the string to replace.`,
raw: `Failed to edit, 0 occurrences found for old_string in ${params.file_path}. No edits made. The exact text in old_string was not found. Ensure you're not escaping content incorrectly and check whitespace, indentation, and context. Use ${ReadFileTool.Name} tool to verify.`,
type: ToolErrorType.EDIT_NO_OCCURRENCE_FOUND,
};
} else if (occurrences !== expectedReplacements) {
const occurrenceTerm =
@@ -246,11 +252,13 @@ Expectation for required parameters:
error = {
display: `Failed to edit, expected ${expectedReplacements} ${occurrenceTerm} but found ${occurrences}.`,
raw: `Failed to edit, Expected ${expectedReplacements} ${occurrenceTerm} but found ${occurrences} for old_string in file: ${params.file_path}`,
type: ToolErrorType.EDIT_EXPECTED_OCCURRENCE_MISMATCH,
};
} else if (finalOldString === finalNewString) {
error = {
display: `No changes to apply. The old_string and new_string are identical.`,
raw: `No changes to apply. The old_string and new_string are identical in file: ${params.file_path}`,
type: ToolErrorType.EDIT_NO_CHANGE,
};
}
} else {
@@ -258,6 +266,7 @@ Expectation for required parameters:
error = {
display: `Failed to read content of file.`,
raw: `Failed to read content of existing file: ${params.file_path}`,
type: ToolErrorType.READ_CONTENT_FAILURE,
};
}
@@ -374,6 +383,10 @@ Expectation for required parameters:
return {
llmContent: `Error: Invalid parameters provided. Reason: ${validationError}`,
returnDisplay: `Error: ${validationError}`,
error: {
message: validationError,
type: ToolErrorType.INVALID_TOOL_PARAMS,
},
};
}
@@ -385,6 +398,10 @@ Expectation for required parameters:
return {
llmContent: `Error preparing edit: ${errorMsg}`,
returnDisplay: `Error preparing edit: ${errorMsg}`,
error: {
message: errorMsg,
type: ToolErrorType.EDIT_PREPARATION_FAILURE,
},
};
}
@@ -392,6 +409,10 @@ Expectation for required parameters:
return {
llmContent: editData.error.raw,
returnDisplay: `Error: ${editData.error.display}`,
error: {
message: editData.error.raw,
type: editData.error.type,
},
};
}
@@ -442,6 +463,10 @@ Expectation for required parameters:
return {
llmContent: `Error executing edit: ${errorMsg}`,
returnDisplay: `Error writing file: ${errorMsg}`,
error: {
message: errorMsg,
type: ToolErrorType.FILE_WRITE_FAILURE,
},
};
}
}