forked from Handit-AI/handit-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-diff.js
More file actions
214 lines (186 loc) · 6.05 KB
/
test-diff.js
File metadata and controls
214 lines (186 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const { IterativeCodeGenerator } = require('./src/generator/iterativeGenerator');
// Mock data to test the diff system
const mockNode = {
name: 'process-document',
file: 'server.js',
line: 64
};
const originalCode = `app.post('/process-document', upload.single('image'), async (req, res) => {
if (!req.file) {
return res.status(400).json({ error: 'No image file provided' });
}
console.log(\`Processing file: \${req.file.filename}\`);
// Process the document
const result = await processDocument(req.file.path);
// Clean up the uploaded file
fs.unlinkSync(req.file.path);
res.json({
success: true,
data: result
});
}
// Additional context after the function
console.log('Server started on port 3000');
app.listen(3000, () => {
console.log('Document processing service ready');
});`;
const structuredChanges = {
additions: [
{
line: 1,
content: "import { startTracing, trackNode, endTracing } from '@handit.ai/node';"
},
{
line: 1,
content: "app.post('/process-document', upload.single('image'), async (req, res) => {"
},
{
line: 6,
content: " console.log(`Processing file: ${req.file.filename}`);"
},
{
line: 9,
content: " const result = await processDocument(req.file.path, executionId);"
},
{
line: 16,
content: " await trackNode({"
},
{
line: 17,
content: " input: req.file.path,"
},
{
line: 18,
content: " output: result,"
},
{
line: 19,
content: " nodeName: 'process-document',"
},
{
line: 20,
content: " agentName: 'my-agent',"
},
{
line: 21,
content: " nodeType: 'tool',"
},
{
line: 22,
content: " executionId"
},
{
line: 23,
content: " });"
},
{
line: 33,
content: " } finally {"
},
{
line: 34,
content: " await endTracing({ executionId, agentName: 'my-agent' });"
},
{
line: 35,
content: " }"
}
],
removals: [
{
line: 1,
content: "app.post('/process-document', upload.single('image'), async (req, res) => {"
},
{
line: 9,
content: " const result = await processDocument(req.file.path);"
},
{
line: 33,
content: "}"
}
]
};
async function testDiff() {
// Create a mock generator without OpenAI dependency
const mockGenerator = {
showStructuredDiff: function(node, originalCode, structuredChanges) {
const chalk = require('chalk');
// Dark theme styling - similar to the image
console.log(chalk.bgBlack.white('─'.repeat(80)));
console.log(chalk.bgBlack.cyan.bold(`🔍 Code Changes for ${node.name}`));
console.log(chalk.bgBlack.white('─'.repeat(80)));
const originalLines = originalCode.split('\n');
// Summary section with dark background
console.log(chalk.bgBlack.yellow.bold('📊 Summary:'));
console.log(chalk.bgBlack.green(` + ${structuredChanges.additions.length} lines added`));
console.log(chalk.bgBlack.red(` - ${structuredChanges.removals.length} lines removed\n`));
// Create unified changes
const changes = [];
// Process additions
structuredChanges.additions.forEach(addition => {
changes.push({
type: 'addition',
line: addition.line,
content: addition.content
});
});
// Process removals
structuredChanges.removals.forEach(removal => {
changes.push({
type: 'removal',
line: removal.line,
content: removal.content
});
});
// Sort by line number
const unifiedChanges = changes.sort((a, b) => a.line - b.line);
if (unifiedChanges.length === 0) return;
console.log(chalk.bgBlack.yellow.bold('\n📋 Changes:'));
console.log(chalk.bgBlack.white('─'.repeat(80)));
const contextLines = 4;
let lastContextEnd = -1;
unifiedChanges.forEach((change, index) => {
const lineNumber = change.line;
const contextStart = Math.max(0, lineNumber - contextLines - 1);
const contextEnd = Math.min(originalLines.length - 1, lineNumber + contextLines - 1);
// Add separator if there's a gap
if (lastContextEnd >= 0 && contextStart > lastContextEnd + 1) {
console.log(chalk.bgBlack.gray(' ...'));
}
// Show context before change
const contextBeforeStart = Math.max(lastContextEnd + 1, contextStart);
if (contextBeforeStart < lineNumber) {
for (let i = contextBeforeStart; i < lineNumber; i++) {
const lineContent = originalLines[i] || '';
console.log(chalk.bgBlack.white(`${(i + 1).toString().padStart(3)}: ${lineContent}`));
}
}
// Show the change
if (change.type === 'addition') {
console.log(chalk.bgGreen.black(`${lineNumber.toString().padStart(3)}: + ${change.content}`));
} else if (change.type === 'removal') {
console.log(chalk.bgRed.white(`${lineNumber.toString().padStart(3)}: - ${change.content}`));
}
// Show context after change
const contextAfterStart = lineNumber;
const contextAfterEnd = Math.min(originalLines.length, contextEnd + 1);
if (contextAfterStart < contextAfterEnd) {
for (let i = contextAfterStart; i < contextAfterEnd; i++) {
const lineContent = originalLines[i] || '';
console.log(chalk.bgBlack.white(`${(i + 1).toString().padStart(3)}: ${lineContent}`));
}
lastContextEnd = contextAfterEnd - 1;
} else {
lastContextEnd = lineNumber - 1;
}
});
console.log(chalk.bgBlack.white('─'.repeat(80)));
}
};
console.log('🔍 Testing Improved Diff System');
console.log('─'.repeat(80));
mockGenerator.showStructuredDiff(mockNode, originalCode, structuredChanges);
}
testDiff().catch(console.error);