@@ -3,8 +3,11 @@ import type { FileEditInsertToolArgs, FileEditInsertToolResult } from "@/types/t
33import { EDIT_FAILED_NOTE_PREFIX , NOTE_READ_FILE_RETRY } from "@/types/tools" ;
44import type { ToolConfiguration , ToolFactory } from "@/utils/tools/tools" ;
55import { TOOL_DEFINITIONS } from "@/utils/tools/toolDefinitions" ;
6- import { validateAndCorrectPath , validatePathInCwd } from "./fileCommon" ;
6+ import { generateDiff , validateAndCorrectPath , validatePathInCwd } from "./fileCommon" ;
77import { executeFileEditOperation } from "./file_edit_operation" ;
8+ import { fileExists } from "@/utils/runtime/fileExists" ;
9+ import { writeFileString } from "@/utils/runtime/helpers" ;
10+ import { RuntimeError } from "@/runtime/Runtime" ;
811
912const READ_AND_RETRY_NOTE = `${ EDIT_FAILED_NOTE_PREFIX } ${ NOTE_READ_FILE_RETRY } ` ;
1013
@@ -45,11 +48,15 @@ export const createFileEditInsertTool: ToolFactory = (config: ToolConfiguration)
4548 description : TOOL_DEFINITIONS . file_edit_insert . description ,
4649 inputSchema : TOOL_DEFINITIONS . file_edit_insert . schema ,
4750 execute : async (
48- { file_path, content, before, after } : FileEditInsertToolArgs ,
51+ { file_path, content, before, after, create } : FileEditInsertToolArgs ,
4952 { abortSignal }
5053 ) : Promise < FileEditInsertToolResult > => {
5154 try {
52- const { correctedPath } = validateAndCorrectPath ( file_path , config . cwd , config . runtime ) ;
55+ const { correctedPath, warning : pathWarning } = validateAndCorrectPath (
56+ file_path ,
57+ config . cwd ,
58+ config . runtime
59+ ) ;
5360 file_path = correctedPath ;
5461
5562 const pathValidation = validatePathInCwd ( file_path , config . cwd , config . runtime ) ;
@@ -60,6 +67,38 @@ export const createFileEditInsertTool: ToolFactory = (config: ToolConfiguration)
6067 } ;
6168 }
6269
70+ const resolvedPath = config . runtime . normalizePath ( file_path , config . cwd ) ;
71+ const exists = await fileExists ( config . runtime , resolvedPath , abortSignal ) ;
72+
73+ if ( ! exists ) {
74+ if ( ! create ) {
75+ return {
76+ success : false ,
77+ error : `File not found: ${ file_path } . Set create: true to create it.` ,
78+ note : `${ EDIT_FAILED_NOTE_PREFIX } File does not exist. Set create: true to create it, or check the file path.` ,
79+ } ;
80+ }
81+
82+ try {
83+ await writeFileString ( config . runtime , resolvedPath , content , abortSignal ) ;
84+ } catch ( err ) {
85+ if ( err instanceof RuntimeError ) {
86+ return {
87+ success : false ,
88+ error : err . message ,
89+ } ;
90+ }
91+ throw err ;
92+ }
93+
94+ const diff = generateDiff ( resolvedPath , "" , content ) ;
95+ return {
96+ success : true ,
97+ diff,
98+ ...( pathWarning && { warning : pathWarning } ) ,
99+ } ;
100+ }
101+
63102 return executeFileEditOperation ( {
64103 config,
65104 filePath : file_path ,
0 commit comments