File size: 6,887 Bytes
5da4770 |
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 215 216 217 218 219 220 221 222 |
export type DiffType = 'unchanged' | 'added' | 'removed';
export interface LineDiff {
type: DiffType;
oldLine: string | null;
newLine: string | null;
lineNumber: number;
}
export interface CharDiffPart {
text: string;
type: DiffType;
}
export interface DiffStats {
additions: number;
deletions: number;
}
export interface ExtractedData {
filePath: string | null;
oldStr: string | null;
newStr: string | null;
success?: boolean;
timestamp?: string;
}
export const extractFromNewFormat = (content: any): ExtractedData => {
if (!content) {
return { filePath: null, oldStr: null, newStr: null };
}
if (typeof content === 'string') {
// Only try to parse if it looks like JSON
const trimmed = content.trim();
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
try {
console.debug('StrReplaceToolView: Attempting to parse JSON string:', content.substring(0, 100) + '...');
const parsed = JSON.parse(content);
console.debug('StrReplaceToolView: Successfully parsed JSON:', parsed);
return extractFromNewFormat(parsed);
} catch (error) {
console.error('StrReplaceToolView: JSON parse error:', error, 'Content:', content.substring(0, 200));
return { filePath: null, oldStr: null, newStr: null };
}
} else {
console.debug('StrReplaceToolView: String content does not look like JSON, skipping parse');
return { filePath: null, oldStr: null, newStr: null };
}
}
if (typeof content !== 'object') {
return { filePath: null, oldStr: null, newStr: null };
}
if ('tool_execution' in content && typeof content.tool_execution === 'object') {
const toolExecution = content.tool_execution;
const args = toolExecution.arguments || {};
console.debug('StrReplaceToolView: Extracted from new format:', {
filePath: args.file_path,
oldStr: args.old_str ? `${args.old_str.substring(0, 50)}...` : null,
newStr: args.new_str ? `${args.new_str.substring(0, 50)}...` : null,
success: toolExecution.result?.success
});
return {
filePath: args.file_path || null,
oldStr: args.old_str || null,
newStr: args.new_str || null,
success: toolExecution.result?.success,
timestamp: toolExecution.execution_details?.timestamp
};
}
if ('role' in content && 'content' in content && typeof content.content === 'string') {
console.debug('StrReplaceToolView: Found role/content structure with string content, parsing...');
return extractFromNewFormat(content.content);
}
if ('role' in content && 'content' in content && typeof content.content === 'object') {
console.debug('StrReplaceToolView: Found role/content structure with object content');
return extractFromNewFormat(content.content);
}
return { filePath: null, oldStr: null, newStr: null };
};
export const extractFromLegacyFormat = (content: any, extractToolData: any, extractFilePath: any, extractStrReplaceContent: any): ExtractedData => {
const assistantToolData = extractToolData(content);
if (assistantToolData.toolResult) {
const args = assistantToolData.arguments || {};
console.debug('StrReplaceToolView: Extracted from legacy format (extractToolData):', {
filePath: assistantToolData.filePath || args.file_path,
oldStr: args.old_str ? `${args.old_str.substring(0, 50)}...` : null,
newStr: args.new_str ? `${args.new_str.substring(0, 50)}...` : null
});
return {
filePath: assistantToolData.filePath || args.file_path || null,
oldStr: args.old_str || null,
newStr: args.new_str || null
};
}
const legacyFilePath = extractFilePath(content);
const strReplaceContent = extractStrReplaceContent(content);
console.debug('StrReplaceToolView: Extracted from legacy format (fallback):', {
filePath: legacyFilePath,
oldStr: strReplaceContent.oldStr ? `${strReplaceContent.oldStr.substring(0, 50)}...` : null,
newStr: strReplaceContent.newStr ? `${strReplaceContent.newStr.substring(0, 50)}...` : null
});
return {
filePath: legacyFilePath,
oldStr: strReplaceContent.oldStr,
newStr: strReplaceContent.newStr
};
};
export const parseNewlines = (text: string): string => {
return text.replace(/\\n/g, '\n');
};
export const generateLineDiff = (oldText: string, newText: string): LineDiff[] => {
const parsedOldText = parseNewlines(oldText);
const parsedNewText = parseNewlines(newText);
const oldLines = parsedOldText.split('\n');
const newLines = parsedNewText.split('\n');
const diffLines: LineDiff[] = [];
const maxLines = Math.max(oldLines.length, newLines.length);
for (let i = 0; i < maxLines; i++) {
const oldLine = i < oldLines.length ? oldLines[i] : null;
const newLine = i < newLines.length ? newLines[i] : null;
if (oldLine === newLine) {
diffLines.push({ type: 'unchanged', oldLine, newLine, lineNumber: i + 1 });
} else {
if (oldLine !== null) {
diffLines.push({ type: 'removed', oldLine, newLine: null, lineNumber: i + 1 });
}
if (newLine !== null) {
diffLines.push({ type: 'added', oldLine: null, newLine, lineNumber: i + 1 });
}
}
}
return diffLines;
};
export const generateCharDiff = (oldText: string, newText: string): CharDiffPart[] => {
const parsedOldText = parseNewlines(oldText);
const parsedNewText = parseNewlines(newText);
let prefixLength = 0;
while (
prefixLength < parsedOldText.length &&
prefixLength < parsedNewText.length &&
parsedOldText[prefixLength] === parsedNewText[prefixLength]
) {
prefixLength++;
}
let oldSuffixStart = parsedOldText.length;
let newSuffixStart = parsedNewText.length;
while (
oldSuffixStart > prefixLength &&
newSuffixStart > prefixLength &&
parsedOldText[oldSuffixStart - 1] === parsedNewText[newSuffixStart - 1]
) {
oldSuffixStart--;
newSuffixStart--;
}
const parts: CharDiffPart[] = [];
if (prefixLength > 0) {
parts.push({
text: parsedOldText.substring(0, prefixLength),
type: 'unchanged',
});
}
if (oldSuffixStart > prefixLength) {
parts.push({
text: parsedOldText.substring(prefixLength, oldSuffixStart),
type: 'removed',
});
}
if (newSuffixStart > prefixLength) {
parts.push({
text: parsedNewText.substring(prefixLength, newSuffixStart),
type: 'added',
});
}
if (oldSuffixStart < parsedOldText.length) {
parts.push({
text: parsedOldText.substring(oldSuffixStart),
type: 'unchanged',
});
}
return parts;
};
export const calculateDiffStats = (lineDiff: LineDiff[]): DiffStats => {
return {
additions: lineDiff.filter(line => line.type === 'added').length,
deletions: lineDiff.filter(line => line.type === 'removed').length
};
}; |