|
1 | 1 | import { execFileSync } from "child_process"; |
2 | | -import { existsSync, mkdirSync, appendFileSync, readFileSync } from "fs"; |
| 2 | +import { existsSync, mkdirSync, appendFileSync, readFileSync, openSync, fstatSync, readSync, closeSync } from "fs"; |
3 | 3 | import { join, relative } from "path"; |
4 | 4 |
|
5 | 5 | export interface Range { |
@@ -94,30 +94,187 @@ export function normalizeModelId(model?: string): string | undefined { |
94 | 94 | return model; |
95 | 95 | } |
96 | 96 |
|
| 97 | +/** |
| 98 | + * Extracts the model identifier from a Claude Code transcript file. |
| 99 | + * |
| 100 | + * Claude Code stores conversation transcripts as JSONL files where each line |
| 101 | + * represents a message exchange. The model identifier is stored at `entry.message.model`. |
| 102 | + * This function reads only the tail of the file to efficiently get the most recent model, |
| 103 | + * which handles cases where the model may have changed during a session. |
| 104 | + * |
| 105 | + * @param transcriptPath - Absolute path to the Claude Code transcript JSONL file |
| 106 | + * @returns The model identifier (e.g., "claude-opus-4-5-20251101") or undefined if not found |
| 107 | + * |
| 108 | + * @example |
| 109 | + * ```typescript |
| 110 | + * const model = extractModelFromTranscript("/path/to/transcript.jsonl"); |
| 111 | + * // Returns: "claude-opus-4-5-20251101" |
| 112 | + * ``` |
| 113 | + */ |
| 114 | +export function extractModelFromTranscript(transcriptPath: string): string | undefined { |
| 115 | + try { |
| 116 | + const fd = openSync(transcriptPath, "r"); |
| 117 | + const stats = fstatSync(fd); |
| 118 | + |
| 119 | + // Start with 1KB, expand if needed (handles varying line sizes) |
| 120 | + let readSize = Math.min(stats.size, 1024); |
| 121 | + |
| 122 | + while (readSize <= stats.size) { |
| 123 | + const buffer = Buffer.alloc(readSize); |
| 124 | + readSync(fd, buffer, 0, readSize, stats.size - readSize); |
| 125 | + |
| 126 | + const content = buffer.toString("utf-8"); |
| 127 | + const lines = content.split("\n"); |
| 128 | + |
| 129 | + // Iterate from end to get the most recent model |
| 130 | + for (let i = lines.length - 1; i >= 0; i--) { |
| 131 | + const line = lines[i].trim(); |
| 132 | + if (!line) continue; |
| 133 | + |
| 134 | + try { |
| 135 | + const entry = JSON.parse(line); |
| 136 | + if (entry.message?.model) { |
| 137 | + closeSync(fd); |
| 138 | + return entry.message.model; |
| 139 | + } |
| 140 | + } catch { |
| 141 | + // Skip malformed/partial JSON lines |
| 142 | + continue; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // No model found, try larger chunk |
| 147 | + if (readSize >= stats.size) break; |
| 148 | + readSize = Math.min(stats.size, readSize * 2); |
| 149 | + } |
| 150 | + |
| 151 | + closeSync(fd); |
| 152 | + return undefined; |
| 153 | + } catch { |
| 154 | + // File doesn't exist or isn't readable |
| 155 | + return undefined; |
| 156 | + } |
| 157 | +} |
| 158 | + |
97 | 159 | export interface RangePosition { |
98 | 160 | start_line: number; |
99 | 161 | end_line: number; |
100 | 162 | } |
101 | 163 |
|
| 164 | +/** |
| 165 | + * Computes which lines in `newStr` are actually new or modified compared to `oldStr`. |
| 166 | + * |
| 167 | + * This function performs a simple line-by-line diff to distinguish between: |
| 168 | + * - Context lines: Lines that exist in both old and new strings (not attributed) |
| 169 | + * - Changed lines: Lines that are new or modified (attributed to AI) |
| 170 | + * |
| 171 | + * This is necessary because some tools (like Claude Code's Edit tool) include |
| 172 | + * surrounding context lines in both `old_string` and `new_string`. Without this |
| 173 | + * diff, we would incorrectly attribute unchanged context lines to the AI. |
| 174 | + * |
| 175 | + * @param oldStr - The original string before the edit |
| 176 | + * @param newStr - The new string after the edit |
| 177 | + * @returns Array of 0-indexed line offsets within `newStr` that are new or modified |
| 178 | + * |
| 179 | + * @example |
| 180 | + * ```typescript |
| 181 | + * // old: "line1\nline2\nline3" |
| 182 | + * // new: "line1\nNEW LINE\nline3" |
| 183 | + * diffToFindChangedLines(old, new); // Returns [1] - only the middle line changed |
| 184 | + * ``` |
| 185 | + */ |
| 186 | +function diffToFindChangedLines(oldStr: string, newStr: string): number[] { |
| 187 | + const oldLines = oldStr.split("\n"); |
| 188 | + const newLines = newStr.split("\n"); |
| 189 | + const changedOffsets: number[] = []; |
| 190 | + |
| 191 | + let oldIdx = 0; |
| 192 | + |
| 193 | + for (let newIdx = 0; newIdx < newLines.length; newIdx++) { |
| 194 | + if (oldIdx < oldLines.length && oldLines[oldIdx] === newLines[newIdx]) { |
| 195 | + // Matching line - this is context, not a change |
| 196 | + oldIdx++; |
| 197 | + } else { |
| 198 | + // Check if this line from newStr exists later in oldStr (handles deletions) |
| 199 | + let foundAhead = false; |
| 200 | + for (let lookAhead = oldIdx; lookAhead < oldLines.length; lookAhead++) { |
| 201 | + if (oldLines[lookAhead] === newLines[newIdx]) { |
| 202 | + oldIdx = lookAhead + 1; |
| 203 | + foundAhead = true; |
| 204 | + break; |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + if (!foundAhead) { |
| 209 | + // Line is genuinely new or modified - attribute to AI |
| 210 | + changedOffsets.push(newIdx); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + return changedOffsets; |
| 216 | +} |
| 217 | + |
102 | 218 | export function computeRangePositions(edits: FileEdit[], fileContent?: string): RangePosition[] { |
103 | 219 | return edits |
104 | 220 | .filter((e) => e.new_string) |
105 | | - .map((edit) => { |
| 221 | + .flatMap((edit) => { |
| 222 | + // Case 1: Has explicit range from tool → use it |
106 | 223 | if (edit.range) { |
107 | | - return { |
| 224 | + return [{ |
108 | 225 | start_line: edit.range.start_line_number, |
109 | 226 | end_line: edit.range.end_line_number, |
110 | | - }; |
| 227 | + }]; |
111 | 228 | } |
| 229 | + |
| 230 | + // Case 2: Has both old_string and new_string → diff them to find actual changes |
| 231 | + if (edit.old_string && edit.new_string && fileContent) { |
| 232 | + const idx = fileContent.indexOf(edit.new_string); |
| 233 | + if (idx !== -1) { |
| 234 | + const startLine = fileContent.substring(0, idx).split("\n").length; |
| 235 | + const changedOffsets = diffToFindChangedLines(edit.old_string, edit.new_string); |
| 236 | + |
| 237 | + if (changedOffsets.length === 0) { |
| 238 | + return []; |
| 239 | + } |
| 240 | + |
| 241 | + // Convert offsets to line ranges, merging adjacent lines |
| 242 | + const ranges: RangePosition[] = []; |
| 243 | + let rangeStart = changedOffsets[0]; |
| 244 | + let rangeEnd = changedOffsets[0]; |
| 245 | + |
| 246 | + for (let i = 1; i < changedOffsets.length; i++) { |
| 247 | + if (changedOffsets[i] === rangeEnd + 1) { |
| 248 | + rangeEnd = changedOffsets[i]; |
| 249 | + } else { |
| 250 | + ranges.push({ |
| 251 | + start_line: startLine + rangeStart, |
| 252 | + end_line: startLine + rangeEnd, |
| 253 | + }); |
| 254 | + rangeStart = changedOffsets[i]; |
| 255 | + rangeEnd = changedOffsets[i]; |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + ranges.push({ |
| 260 | + start_line: startLine + rangeStart, |
| 261 | + end_line: startLine + rangeEnd, |
| 262 | + }); |
| 263 | + |
| 264 | + return ranges; |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + // Case 3: Fallback - attribute entire new_string (original behavior) |
112 | 269 | const lineCount = edit.new_string.split("\n").length; |
113 | 270 | if (fileContent) { |
114 | 271 | const idx = fileContent.indexOf(edit.new_string); |
115 | 272 | if (idx !== -1) { |
116 | 273 | const startLine = fileContent.substring(0, idx).split("\n").length; |
117 | | - return { start_line: startLine, end_line: startLine + lineCount - 1 }; |
| 274 | + return [{ start_line: startLine, end_line: startLine + lineCount - 1 }]; |
118 | 275 | } |
119 | 276 | } |
120 | | - return { start_line: 1, end_line: lineCount }; |
| 277 | + return [{ start_line: 1, end_line: lineCount }]; |
121 | 278 | }); |
122 | 279 | } |
123 | 280 |
|
|
0 commit comments