| 1 | import { Position } from "../.."; |
| 2 | import { |
| 3 | INSTINCT_CONTEXT_FILE_TOKEN, |
| 4 | INSTINCT_EDITABLE_REGION_END_TOKEN, |
| 5 | INSTINCT_EDITABLE_REGION_START_TOKEN, |
| 6 | INSTINCT_SNIPPET_TOKEN, |
| 7 | INSTINCT_USER_CURSOR_IS_HERE_TOKEN, |
| 8 | } from "../constants"; |
| 9 | import { insertCursorToken } from "./utils"; |
| 10 | |
| 11 | /** |
| 12 | * @param contextSnippets Codestral style snippet with +++++ filename\ncontent or an empty string. |
| 13 | */ |
| 14 | export function contextSnippetsBlock(contextSnippets: string) { |
| 15 | const headerRegex = /^(\+\+\+\+\+ )(.*)/; |
| 16 | const lines = contextSnippets.split("\n"); |
| 17 | |
| 18 | return lines |
| 19 | .reduce<string[]>((acc, line) => { |
| 20 | const matches = line.match(headerRegex); |
| 21 | if (matches) { |
| 22 | const filename = matches[2]; |
| 23 | acc.push(`${INSTINCT_CONTEXT_FILE_TOKEN}: ${filename}`); |
| 24 | } else { |
| 25 | if ( |
| 26 | acc.length > 0 && |
| 27 | acc[acc.length - 1].startsWith(INSTINCT_CONTEXT_FILE_TOKEN) // if header was added just before |
| 28 | ) { |
| 29 | acc.push(`${INSTINCT_SNIPPET_TOKEN}`); |
| 30 | } |
| 31 | acc.push(line); |
| 32 | } |
| 33 | |
| 34 | return acc; |
| 35 | }, []) |
| 36 | .join("\n"); |
| 37 | } |
| 38 | |
| 39 | export function currentFileContentBlock( |
| 40 | currentFileContent: string, |
| 41 | windowStart: number, |
| 42 | windowEnd: number, |
| 43 | editableRegionStartLine: number, |
| 44 | editableRegionEndLine: number, |
| 45 | cursorPosition: Position, |
| 46 | ) { |
| 47 | const currentFileContentLines = currentFileContent.split("\n"); |
| 48 | |
| 49 | const insertedCursorLines = insertCursorToken( |
| 50 | currentFileContentLines, |
| 51 | cursorPosition, |
| 52 | INSTINCT_USER_CURSOR_IS_HERE_TOKEN, |
| 53 | ); |
| 54 | |
| 55 | const instrumentedLines = [ |
| 56 | ...insertedCursorLines.slice(windowStart, editableRegionStartLine), |
| 57 | INSTINCT_EDITABLE_REGION_START_TOKEN, |
| 58 | ...insertedCursorLines.slice( |
| 59 | editableRegionStartLine, |
| 60 | editableRegionEndLine + 1, |
| 61 | ), |
| 62 | INSTINCT_EDITABLE_REGION_END_TOKEN, |
| 63 | ...insertedCursorLines.slice(editableRegionEndLine + 1, windowEnd + 1), |
| 64 | ]; |
| 65 | |
| 66 | return instrumentedLines.join("\n"); |
| 67 | } |
| 68 | |
| 69 | export function editHistoryBlock( |
| 70 | editDiffHistories: string[], // array of unified diffs with Index headers |
| 71 | ) { |
| 72 | if (!editDiffHistories.length) { |
| 73 | return ""; |
| 74 | } |
| 75 | |
| 76 | const blocks: string[] = []; |
| 77 | |
| 78 | for (const editDiffHistory of editDiffHistories) { |
| 79 | if (!editDiffHistory.trim()) { |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | // Split on Index: lines to get the unified diff. |
| 84 | const diffSections = editDiffHistory |
| 85 | .split(/^Index: /m) |
| 86 | .filter((section) => section.trim()); |
| 87 | |
| 88 | for (const section of diffSections) { |
| 89 | const lines = section.split("\n"); |
| 90 | |
| 91 | // Extract filename from the first line (after "Index: " was split off). |
| 92 | const filename = lines[0]; |
| 93 | |
| 94 | // Find the start of the actual diff content (skip ---, +++, and === lines). |
| 95 | const diffLines = lines |
| 96 | .filter( |
| 97 | (line) => |
| 98 | !line.startsWith("---") && |
| 99 | !line.startsWith("+++") && |
| 100 | !line.startsWith("===") && |
| 101 | line.trim() !== "", // remove empty lines from header section |
| 102 | ) |
| 103 | .slice(1); // remove the filename line |
| 104 | |
| 105 | // Only include lines that are actual diff content (@@, +, -, or context lines). |
| 106 | const actualDiffContent = diffLines.filter( |
| 107 | (line) => |
| 108 | line.startsWith("@@") || |
| 109 | line.startsWith("+") || |
| 110 | line.startsWith("-") || |
| 111 | line.startsWith(" "), // context lines |
| 112 | ); |
| 113 | |
| 114 | if (actualDiffContent.length === 0) continue; |
| 115 | |
| 116 | const diffBlock = [ |
| 117 | `User edited file "${filename}"`, |
| 118 | "", |
| 119 | "```diff", |
| 120 | actualDiffContent.join("\n"), |
| 121 | "```", |
| 122 | ].join("\n"); |
| 123 | |
| 124 | blocks.push(diffBlock); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return blocks.join("\n"); |
| 129 | } |