(
params: {
path: string
initialContentPromise: Promise<string | null>
newContent: string
logger: Logger
},
)
| 31 | * - `{ aborted: false, value: WriteFileResult }` on success or recoverable error |
| 32 | */ |
| 33 | export async function processFileBlock( |
| 34 | params: { |
| 35 | path: string |
| 36 | initialContentPromise: Promise<string | null> |
| 37 | newContent: string |
| 38 | logger: Logger |
| 39 | }, |
| 40 | ): Promise<PromptResult<WriteFileResult>> { |
| 41 | const { |
| 42 | path, |
| 43 | initialContentPromise, |
| 44 | newContent, |
| 45 | logger, |
| 46 | } = params |
| 47 | const initialContent = await initialContentPromise |
| 48 | |
| 49 | if (initialContent === null) { |
| 50 | const cleanContent = cleanMarkdownCodeBlock(newContent) |
| 51 | |
| 52 | logger.debug( |
| 53 | { path, cleanContent }, |
| 54 | `processFileBlock: Created new file ${path}`, |
| 55 | ) |
| 56 | return promptSuccess({ |
| 57 | tool: 'write_file' as const, |
| 58 | path, |
| 59 | content: cleanContent, |
| 60 | patch: undefined, |
| 61 | messages: [`Created new file ${path}`], |
| 62 | }) |
| 63 | } |
| 64 | |
| 65 | if (newContent === initialContent) { |
| 66 | logger.info( |
| 67 | { newContent }, |
| 68 | `processFileBlock: New was same as old, skipping ${path}`, |
| 69 | ) |
| 70 | return promptSuccess({ |
| 71 | tool: 'write_file' as const, |
| 72 | path, |
| 73 | error: 'The new content was the same as the old content, skipping.', |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | const lineEnding = initialContent.includes('\r\n') ? '\r\n' : '\n' |
| 78 | const normalizeLineEndings = (str: string) => str.replace(/\r\n/g, '\n') |
| 79 | const normalizedInitialContent = normalizeLineEndings(initialContent) |
| 80 | const normalizedNewContent = normalizeLineEndings(newContent) |
| 81 | |
| 82 | let patch = createPatch(path, normalizedInitialContent, normalizedNewContent) |
| 83 | const lines = patch.split('\n') |
| 84 | const hunkStartIndex = lines.findIndex((line) => line.startsWith('@@')) |
| 85 | if (hunkStartIndex !== -1) { |
| 86 | patch = lines.slice(hunkStartIndex).join('\n') |
| 87 | } else { |
| 88 | logger.debug( |
| 89 | { |
| 90 | path, |
no test coverage detected