|
| 1 | +/** |
| 2 | + * File Diff Streaming Helpers |
| 3 | + * |
| 4 | + * Utilities for streaming file changes through the execution pipeline. |
| 5 | + * Integrates with the core streaming system to show code diffs in real-time. |
| 6 | + * |
| 7 | + * @package @bitcode/api/streams |
| 8 | + */ |
| 9 | + |
| 10 | +import type { StreamMessage, FileDiff, FileTreeChange } from './streams'; |
| 11 | +import { writeStreamMessage } from './streams'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Write a single file diff to the stream |
| 15 | + * |
| 16 | + * Use this when a file is created, modified, or deleted during execution. |
| 17 | + * The UI will render an expandable diff viewer inline with the log. |
| 18 | + */ |
| 19 | +export async function writeFileDiff( |
| 20 | + dataStream: any, |
| 21 | + fileDiff: FileDiff, |
| 22 | + options?: { |
| 23 | + phase?: string; |
| 24 | + agent?: string; |
| 25 | + step?: string; |
| 26 | + message?: string; |
| 27 | + } |
| 28 | +): Promise<void> { |
| 29 | + const message: StreamMessage = { |
| 30 | + type: 'file-diff', |
| 31 | + message: options?.message || `File ${fileDiff.action}: ${fileDiff.path}`, |
| 32 | + fileDiff, |
| 33 | + executionState: options ? { |
| 34 | + phase: options.phase as any, |
| 35 | + agent: options.agent, |
| 36 | + step: options.step as any, |
| 37 | + } : undefined, |
| 38 | + progress: fileDiff.action === 'created' ? 'success' : fileDiff.action === 'deleted' ? 'warning' : 'in-progress', |
| 39 | + }; |
| 40 | + |
| 41 | + await writeStreamMessage(dataStream, message); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Write complete file tree changes to the stream |
| 46 | + * |
| 47 | + * Use this at the end of implementation phase to show all file changes |
| 48 | + * in a single, comprehensive view with statistics. |
| 49 | + */ |
| 50 | +export async function writeFileTreeChanges( |
| 51 | + dataStream: any, |
| 52 | + fileTree: FileTreeChange, |
| 53 | + options?: { |
| 54 | + phase?: string; |
| 55 | + agent?: string; |
| 56 | + step?: string; |
| 57 | + message?: string; |
| 58 | + } |
| 59 | +): Promise<void> { |
| 60 | + const message: StreamMessage = { |
| 61 | + type: 'file-diff', |
| 62 | + message: options?.message || 'File tree changes completed', |
| 63 | + fileTree, |
| 64 | + executionState: options ? { |
| 65 | + phase: options.phase as any, |
| 66 | + agent: options.agent, |
| 67 | + step: options.step as any, |
| 68 | + } : undefined, |
| 69 | + progress: 'success', |
| 70 | + }; |
| 71 | + |
| 72 | + await writeStreamMessage(dataStream, message); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Calculate file tree statistics from an array of file diffs |
| 77 | + */ |
| 78 | +export function calculateFileTreeStats(files: FileDiff[]): FileTreeChange { |
| 79 | + const stats = files.reduce((acc, file) => { |
| 80 | + if (file.action === 'created') acc.filesCreated++; |
| 81 | + if (file.action === 'modified') acc.filesModified++; |
| 82 | + if (file.action === 'deleted') acc.filesDeleted++; |
| 83 | + acc.totalLinesAdded += file.linesAdded || 0; |
| 84 | + acc.totalLinesRemoved += file.linesRemoved || 0; |
| 85 | + return acc; |
| 86 | + }, { |
| 87 | + filesCreated: 0, |
| 88 | + filesModified: 0, |
| 89 | + filesDeleted: 0, |
| 90 | + totalLinesAdded: 0, |
| 91 | + totalLinesRemoved: 0, |
| 92 | + files: files, |
| 93 | + }); |
| 94 | + |
| 95 | + return stats; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Extract file diffs from tool results |
| 100 | + * |
| 101 | + * Parse tool execution results (Write, Edit, Delete tools) and convert |
| 102 | + * to FileDiff objects for streaming. |
| 103 | + */ |
| 104 | +export function extractFileDiffsFromToolResults(toolResults: any[]): FileDiff[] { |
| 105 | + const diffs: FileDiff[] = []; |
| 106 | + |
| 107 | + for (const result of toolResults) { |
| 108 | + if (result.toolName === 'Write' && result.result) { |
| 109 | + // File created or overwritten |
| 110 | + diffs.push({ |
| 111 | + path: result.args?.file_path || result.args?.path, |
| 112 | + action: 'created', |
| 113 | + newContent: result.args?.content, |
| 114 | + linesAdded: result.args?.content?.split('\n').length || 0, |
| 115 | + linesRemoved: 0, |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + if (result.toolName === 'Edit' && result.result) { |
| 120 | + // File modified |
| 121 | + diffs.push({ |
| 122 | + path: result.args?.file_path || result.args?.path, |
| 123 | + action: 'modified', |
| 124 | + oldContent: result.metadata?.originalContent, |
| 125 | + newContent: result.metadata?.newContent, |
| 126 | + linesAdded: result.metadata?.linesAdded || 0, |
| 127 | + linesRemoved: result.metadata?.linesRemoved || 0, |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + if (result.toolName === 'Bash' && result.args?.command?.includes('rm ')) { |
| 132 | + // File deleted (simple detection) |
| 133 | + const match = result.args.command.match(/rm\s+([^\s]+)/); |
| 134 | + if (match) { |
| 135 | + diffs.push({ |
| 136 | + path: match[1], |
| 137 | + action: 'deleted', |
| 138 | + linesAdded: 0, |
| 139 | + linesRemoved: result.metadata?.linesRemoved || 0, |
| 140 | + }); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return diffs; |
| 146 | +} |
0 commit comments