| 1 | import { BuiltInToolNames } from "core/tools/builtIn"; |
| 2 | import { resolveRelativePathInDir } from "core/util/ideUtils"; |
| 3 | import { getUriPathBasename } from "core/util/uri"; |
| 4 | import { IIdeMessenger } from "../../context/IdeMessenger"; |
| 5 | import { setToolCallArgs } from "../slices/sessionSlice"; |
| 6 | import { AppThunkDispatch } from "../store"; |
| 7 | |
| 8 | export async function enhanceParsedArgs( |
| 9 | ideMessenger: IIdeMessenger, |
| 10 | dispatch: AppThunkDispatch, |
| 11 | toolName: string | undefined, |
| 12 | toolCallId: string, |
| 13 | currentArgs: undefined | Record<string, any>, |
| 14 | ) { |
| 15 | // Add file content to parsedArgs for find/replace tools |
| 16 | let enhancedArgs = { ...currentArgs }; |
| 17 | if ( |
| 18 | (toolName === BuiltInToolNames.SingleFindAndReplace || |
| 19 | toolName === BuiltInToolNames.MultiEdit) && |
| 20 | currentArgs?.filepath && |
| 21 | !currentArgs?.editingFileContents |
| 22 | ) { |
| 23 | try { |
| 24 | const fileUri = await resolveRelativePathInDir( |
| 25 | currentArgs.filepath, |
| 26 | ideMessenger.ide, |
| 27 | ); |
| 28 | if (!fileUri) { |
| 29 | throw new Error(`File ${currentArgs.filepath} not found`); |
| 30 | } |
| 31 | const baseName = getUriPathBasename(fileUri); |
| 32 | const fileContent = await ideMessenger.ide.readFile(fileUri); |
| 33 | enhancedArgs = { |
| 34 | ...currentArgs, |
| 35 | fileUri, |
| 36 | baseName, |
| 37 | editingFileContents: fileContent, |
| 38 | }; |
| 39 | dispatch( |
| 40 | setToolCallArgs({ |
| 41 | toolCallId, |
| 42 | newArgs: enhancedArgs, |
| 43 | }), |
| 44 | ); |
| 45 | } catch (error) { |
| 46 | // If we can't read the file, let the tool handle the error |
| 47 | console.warn( |
| 48 | `Failed to enhance args: failed to read file ${currentArgs?.filepath}`, |
| 49 | ); |
| 50 | } |
| 51 | } |
| 52 | } |