File size: 4,187 Bytes
4efde5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
  DiffType,
  LineDiff,
  CharDiffPart,
  DiffStats,
  generateLineDiff,
  generateCharDiff,
  calculateDiffStats,
} from '../file-operation/_utils';

export type { DiffType, LineDiff, CharDiffPart, DiffStats };
export { generateLineDiff, generateCharDiff, calculateDiffStats };

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
  };
};