| 45 | } |
| 46 | |
| 47 | export const createFileEditInsertTool: ToolFactory = (config: ToolConfiguration) => { |
| 48 | return tool({ |
| 49 | description: TOOL_DEFINITIONS.file_edit_insert.description, |
| 50 | inputSchema: TOOL_DEFINITIONS.file_edit_insert.schema, |
| 51 | execute: async ( |
| 52 | { path, content, insert_before, insert_after }: FileEditInsertToolArgs, |
| 53 | { abortSignal } |
| 54 | ): Promise<FileEditInsertToolResult> => { |
| 55 | try { |
| 56 | const { |
| 57 | correctedPath, |
| 58 | warning: pathWarning, |
| 59 | resolvedPath, |
| 60 | } = resolvePathWithinCwd(path, config.cwd, config.runtime); |
| 61 | path = correctedPath; |
| 62 | |
| 63 | // Validate plan mode access restrictions |
| 64 | const planModeError = await validatePlanModeAccess(path, config); |
| 65 | if (planModeError) { |
| 66 | return planModeError; |
| 67 | } |
| 68 | |
| 69 | const exists = await fileExists(config.runtime, resolvedPath, abortSignal); |
| 70 | |
| 71 | if (!exists) { |
| 72 | try { |
| 73 | await writeFileString(config.runtime, resolvedPath, content, abortSignal); |
| 74 | } catch (err) { |
| 75 | if (err instanceof RuntimeError) { |
| 76 | return { |
| 77 | success: false, |
| 78 | error: err.message, |
| 79 | }; |
| 80 | } |
| 81 | throw err; |
| 82 | } |
| 83 | |
| 84 | // Record file state for post-compaction attachment tracking |
| 85 | if (config.recordFileState) { |
| 86 | try { |
| 87 | const newStat = await config.runtime.stat(resolvedPath, abortSignal); |
| 88 | await config.recordFileState(resolvedPath, { |
| 89 | content, |
| 90 | timestamp: newStat.modifiedTime.getTime(), |
| 91 | }); |
| 92 | } catch { |
| 93 | // File stat failed, skip recording |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | const diff = generateDiff(resolvedPath, "", content); |
| 98 | return { |
| 99 | success: true, |
| 100 | diff: FILE_EDIT_DIFF_OMITTED_MESSAGE, |
| 101 | ui_only: { |
| 102 | file_edit: { |
| 103 | diff, |
| 104 | }, |