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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
// Import at the top
// import { formatDistanceToNow } from 'date-fns';
import {
  FileText,
  FileCode,
  FileImage,
  FileJson,
  File,
  FolderOpen,
  FileType,
  FileVideo,
  FileAudio,
  FileArchive,
  Table,
} from 'lucide-react';
import { parseXmlToolCalls, isNewXmlFormat } from './xml-parser';
import { parseToolResult, ParsedToolResult } from './tool-result-parser';

// Helper function to format timestamp
export function formatTimestamp(isoString?: string): string {
  if (!isoString) return '';
  try {
    const date = new Date(isoString);
    return isNaN(date.getTime()) ? 'Invalid date' : date.toLocaleString();
  } catch (e) {
    return 'Invalid date';
  }
}

// Get standardized tool title
export function getToolTitle(toolName: string): string {
  // Normalize tool name
  const normalizedName = toolName.toLowerCase();

  // Map of tool names to their display titles
  const toolTitles: Record<string, string> = {
    'execute-command': 'Execute Command',
    'check-command-output': 'Check Command Output',
    'str-replace': 'String Replace',
    'create-file': 'Create File',
    'full-file-rewrite': 'Rewrite File',
    'delete-file': 'Delete File',
    'web-search': 'Web Search',
    'crawl-webpage': 'Web Crawl',
    'scrape-webpage': 'Web Scrape',
    'browser-navigate': 'Browser Navigate',
    'browser-click': 'Browser Click',
    'browser-extract': 'Browser Extract',
    'browser-fill': 'Browser Fill',
    'browser-wait': 'Browser Wait',
    'see-image': 'View Image',
    'ask': 'Ask',
    'complete': 'Task Complete',
    'execute-data-provider-call': 'Data Provider Call',
    'get-data-provider-endpoints': 'Data Endpoints',
    'deploy': 'Deploy',

    'generic-tool': 'Tool',
    'default': 'Tool',
  };

  // Return the mapped title or a formatted version of the name
  if (toolTitles[normalizedName]) {
    return toolTitles[normalizedName];
  }

  // For browser tools not explicitly mapped
  if (normalizedName.startsWith('browser-')) {
    const operation = normalizedName.replace('browser-', '').replace(/-/g, ' ');
    return 'Browser ' + operation.charAt(0).toUpperCase() + operation.slice(1);
  }

  // Format any other tool name
  return toolName
    .split('-')
    .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ');
}

// Helper to extract command from execute-command content
export function extractCommand(content: string | object | undefined | null): string | null {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return null;
  
  // First try to extract from XML tags (with or without attributes)
  const commandMatch = contentStr.match(
    /<execute-command[^>]*>([\s\S]*?)<\/execute-command>/,
  );
  if (commandMatch) {
    return commandMatch[1].trim();
  }
  
  // Try to find command in JSON structure (for native tool calls)
  try {
    const parsed = JSON.parse(contentStr);
    if (parsed.tool_calls && Array.isArray(parsed.tool_calls)) {
      const execCommand = parsed.tool_calls.find((tc: any) => 
        tc.function?.name === 'execute-command' || 
        tc.function?.name === 'execute_command'
      );
      if (execCommand && execCommand.function?.arguments) {
        try {
          const args = typeof execCommand.function.arguments === 'string' 
            ? JSON.parse(execCommand.function.arguments)
            : execCommand.function.arguments;
          if (args.command) return args.command;
        } catch (e) {
          // If arguments parsing fails, continue
        }
      }
    }
  } catch (e) {
    // Not JSON, continue with other checks
  }
  
  // If no XML tags found, check if the content itself is the command
  // This handles cases where the command is passed directly
  if (!contentStr.includes('<execute-command') && !contentStr.includes('</execute-command>')) {
    // Check if it looks like a command (not JSON, not XML)
    if (!contentStr.startsWith('{') && !contentStr.startsWith('<')) {
      // Don't return content that looks like a tool result or error message
      if (!contentStr.includes('ToolResult') && !contentStr.includes('No command')) {
        return contentStr.trim();
      }
    }
  }
  
  console.log('extractCommand: Could not extract command from content:', contentStr.substring(0, 200));
  return null;
}

// Helper to extract session name from check-command-output content
export function extractSessionName(content: string | object | undefined | null): string | null {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return null;
  
  // First try to extract from XML tags (with or without attributes)
  const sessionMatch = contentStr.match(
    /<check-command-output[^>]*session_name=["']([^"']+)["']/,
  );
  if (sessionMatch) {
    return sessionMatch[1].trim();
  }
  
  // Try to find session_name in JSON structure (for native tool calls)
  try {
    const parsed = JSON.parse(contentStr);
    if (parsed.tool_calls && Array.isArray(parsed.tool_calls)) {
      const checkCommand = parsed.tool_calls.find((tc: any) => 
        tc.function?.name === 'check-command-output' || 
        tc.function?.name === 'check_command_output'
      );
      if (checkCommand && checkCommand.function?.arguments) {
        try {
          const args = typeof checkCommand.function.arguments === 'string' 
            ? JSON.parse(checkCommand.function.arguments)
            : checkCommand.function.arguments;
          if (args.session_name) return args.session_name;
        } catch (e) {
          // If arguments parsing fails, continue
        }
      }
    }
  } catch (e) {
    // Not JSON, continue with other checks
  }
  
  // Look for session_name attribute in the content
  const sessionNameMatch = contentStr.match(/session_name=["']([^"']+)["']/);
  if (sessionNameMatch) {
    return sessionNameMatch[1].trim();
  }
  
  return null;
}

// Helper to extract command output from tool result content
export function extractCommandOutput(
  content: string | object | undefined | null,
): string | null {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return null;

  try {
    // First try to parse the JSON content
    const parsedContent = JSON.parse(contentStr);
    
    // Handle check-command-output specific format
    if (parsedContent.output && typeof parsedContent.output === 'string') {
      return parsedContent.output;
    }
    
    if (parsedContent.content && typeof parsedContent.content === 'string') {
      // Look for a tool_result tag
      const toolResultMatch = parsedContent.content.match(
        /<tool_result>\s*<(?:execute-command|check-command-output)>([\s\S]*?)<\/(?:execute-command|check-command-output)>\s*<\/tool_result>/,
      );
      if (toolResultMatch) {
        return toolResultMatch[1].trim();
      }

      // Look for output field in a ToolResult pattern
      const outputMatch = parsedContent.content.match(
        /ToolResult\(.*?output='([\s\S]*?)'.*?\)/,
      );
      if (outputMatch) {
        return outputMatch[1];
      }

      // Return the content itself as a fallback
      return parsedContent.content;
    }
    
    // If parsedContent is the actual output (new format)
    if (typeof parsedContent === 'string') {
      return parsedContent;
    }
  } catch (e) {
    // If JSON parsing fails, try regex directly
    const toolResultMatch = contentStr.match(
      /<tool_result>\s*<(?:execute-command|check-command-output)>([\s\S]*?)<\/(?:execute-command|check-command-output)>\s*<\/tool_result>/,
    );
    if (toolResultMatch) {
      return toolResultMatch[1].trim();
    }

    const outputMatch = contentStr.match(
      /ToolResult\(.*?output='([\s\S]*?)'.*?\)/,
    );
    if (outputMatch) {
      return outputMatch[1];
    }
    
    // If no special format is found, return the content as-is
    // This handles cases where the output is stored directly
    if (!contentStr.startsWith('<') && !contentStr.includes('ToolResult')) {
      return contentStr;
    }
  }

  return contentStr;
}

// Helper to extract the exit code from tool result
export function extractExitCode(content: string | object | undefined | null): number | null {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return null;

  try {
    const exitCodeMatch = contentStr.match(/exit_code=(\d+)/);
    if (exitCodeMatch && exitCodeMatch[1]) {
      return parseInt(exitCodeMatch[1], 10);
    }
    return 0; // Assume success if no exit code found but command completed
  } catch (e) {
    return null;
  }
}

// Helper to extract file path from commands
export function extractFilePath(content: string | object | undefined | null): string | null {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return null;

  try {
    // Try to parse content as JSON first
    const parsedContent = JSON.parse(contentStr);
    if (parsedContent.content) {
      // Check if it's the new XML format
      if (isNewXmlFormat(parsedContent.content)) {
        const toolCalls = parseXmlToolCalls(parsedContent.content);
        if (toolCalls.length > 0 && toolCalls[0].parameters.file_path) {
          return cleanFilePath(toolCalls[0].parameters.file_path);
        }
      }
      
      // Fall back to old format
      const oldFormatMatch = parsedContent.content.match(
        /file_path=["']([^"']+)["']/,
      );
      if (oldFormatMatch) {
        return cleanFilePath(oldFormatMatch[1]);
      }
    }
  } catch (e) {
    // Fall back to direct regex search if JSON parsing fails
  }

  // Check if it's the new XML format in raw content
  if (isNewXmlFormat(contentStr)) {
    const toolCalls = parseXmlToolCalls(contentStr);
    if (toolCalls.length > 0 && toolCalls[0].parameters.file_path) {
      return cleanFilePath(toolCalls[0].parameters.file_path);
    }
  }

  // Direct regex search in the content string (old format)
  const directMatch = contentStr.match(/file_path=["']([^"']+)["']/);
  if (directMatch) {
    return cleanFilePath(directMatch[1]);
  }

  // Handle double-escaped JSON (old format)
  if (typeof content === 'string' && content.startsWith('"{') && content.endsWith('}"')) {
    try {
      // First parse to get the inner JSON string
      const innerString = JSON.parse(content);
      // Then parse the inner string to get the actual object
      const parsed = JSON.parse(innerString);
      if (parsed && typeof parsed === 'object') {
        if (parsed.file_path) {
          return cleanFilePath(parsed.file_path);
        }
        if (parsed.arguments && parsed.arguments.file_path) {
          return cleanFilePath(parsed.arguments.file_path);
        }
      }
    } catch (e) {
      // Continue with normal extraction
    }
  }

  // First, check if content is already a parsed object (new format after double-escape fix)
  if (typeof content === 'object' && content !== null) {
    try {
      // Check if it's a direct object with content field
      if ('content' in content && typeof content.content === 'string') {
        // Look for XML tags in the content string
        const xmlFilePathMatch =
          content.content.match(/<(?:create-file|delete-file|full-file-rewrite|str-replace)[^>]*\s+file_path=["']([\s\S]*?)["']/i) ||
          content.content.match(/<delete[^>]*\s+file_path=["']([\s\S]*?)["']/i) ||
          content.content.match(/<delete-file[^>]*>([^<]+)<\/delete-file>/i) ||
          content.content.match(/<(?:create-file|delete-file|full-file-rewrite)\s+file_path=["']([^"']+)/i);
        if (xmlFilePathMatch) {
          return cleanFilePath(xmlFilePathMatch[1]);
        }
      }
      
      // Check for direct file_path property
      if ('file_path' in content) {
        return cleanFilePath(content.file_path as string);
      }
      
      // Check for arguments.file_path
      if ('arguments' in content && content.arguments && typeof content.arguments === 'object') {
        const args = content.arguments as any;
        if (args.file_path) {
          return cleanFilePath(args.file_path);
        }
      }
    } catch (e) {
      // Continue with string parsing if object parsing fails
    }
  }

  // Try parsing as JSON string (old format)
  try {
    const parsedContent = JSON.parse(contentStr);
    if (parsedContent.file_path) {
      return cleanFilePath(parsedContent.file_path);
    }
    if (parsedContent.arguments && parsedContent.arguments.file_path) {
      return cleanFilePath(parsedContent.arguments.file_path);
    }
  } catch (e) {
    // Continue with original content if parsing fails
  }

  // Look for file_path in different formats
  const filePathMatch =
    contentStr.match(/file_path=["']([\s\S]*?)["']/i) ||
    contentStr.match(/target_file=["']([\s\S]*?)["']/i) ||
    contentStr.match(/path=["']([\s\S]*?)["']/i);
  if (filePathMatch) {
    const path = filePathMatch[1].trim();
    // Handle newlines and return first line if multiple lines
    return cleanFilePath(path);
  }

  // Look for file_path in XML-like tags (including incomplete ones for streaming)
  const xmlFilePathMatch =
    contentStr.match(/<(?:create-file|delete-file|full-file-rewrite|str-replace)[^>]*\s+file_path=["']([\s\S]*?)["']/i) ||
    contentStr.match(/<delete[^>]*\s+file_path=["']([\s\S]*?)["']/i) ||
    contentStr.match(/<delete-file[^>]*>([^<]+)<\/delete-file>/i) ||
    // Handle incomplete tags during streaming
    contentStr.match(/<(?:create-file|delete-file|full-file-rewrite)\s+file_path=["']([^"']+)/i);
  if (xmlFilePathMatch) {
    return cleanFilePath(xmlFilePathMatch[1]);
  }

  // Look for file paths in delete operations in particular
  if (
    contentStr.toLowerCase().includes('delete') ||
    contentStr.includes('delete-file')
  ) {
    // Look for patterns like "Deleting file: path/to/file.txt"
    const deletePathMatch = contentStr.match(
      /(?:delete|remove|deleting)\s+(?:file|the file)?:?\s+["']?([\w\-./\\]+\.\w+)["']?/i,
    );
    if (deletePathMatch) return cleanFilePath(deletePathMatch[1]);

    // Look for isolated file paths with extensions
    const fileMatch = contentStr.match(/["']?([\w\-./\\]+\.\w+)["']?/);
    if (fileMatch) return cleanFilePath(fileMatch[1]);
  }

  return null;
}

// Helper to clean and process a file path string, handling escaped chars
function cleanFilePath(path: string): string {
  if (!path) return path;

  // Handle escaped newlines and other escaped characters
  return path
    .replace(/\\n/g, '\n') // Replace \n with actual newlines
    .replace(/\\t/g, '\t') // Replace \t with actual tabs
    .replace(/\\r/g, '') // Remove \r
    .replace(/\\\\/g, '\\') // Replace \\ with \
    .replace(/\\"/g, '"') // Replace \" with "
    .replace(/\\'/g, "'") // Replace \' with '
    .split('\n')[0] // Take only the first line if multiline
    .trim(); // Trim whitespace
}

// Helper to extract str-replace old and new strings
export function extractStrReplaceContent(content: string | object | undefined | null): {
  oldStr: string | null;
  newStr: string | null;
} {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return { oldStr: null, newStr: null };

  // First try to extract from a str-replace tag with attributes
  const strReplaceMatch = contentStr.match(/<str-replace[^>]*>([\s\S]*?)<\/str-replace>/);
  if (strReplaceMatch) {
    const innerContent = strReplaceMatch[1];
    const oldMatch = innerContent.match(/<old_str>([\s\S]*?)<\/old_str>/);
    const newMatch = innerContent.match(/<new_str>([\s\S]*?)<\/new_str>/);
    
    return {
      oldStr: oldMatch ? oldMatch[1] : null,
      newStr: newMatch ? newMatch[1] : null,
    };
  }

  // Fall back to direct search for old_str and new_str tags
  const oldMatch = contentStr.match(/<old_str>([\s\S]*?)<\/old_str>/);
  const newMatch = contentStr.match(/<new_str>([\s\S]*?)<\/new_str>/);

  return {
    oldStr: oldMatch ? oldMatch[1] : null,
    newStr: newMatch ? newMatch[1] : null,
  };
}

// Helper to extract file content from create-file or file-rewrite
export function extractFileContent(
  content: string | object | undefined | null,
  toolType: 'create-file' | 'full-file-rewrite',
): string | null {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return null;

  try {
    // Try to parse content as JSON first
    const parsedContent = JSON.parse(contentStr);
    if (parsedContent.content) {
      // Check if it's the new XML format
      if (isNewXmlFormat(parsedContent.content)) {
        const toolCalls = parseXmlToolCalls(parsedContent.content);
        if (toolCalls.length > 0 && toolCalls[0].parameters.file_contents) {
          return processFileContent(toolCalls[0].parameters.file_contents);
        }
      }
      
      // Fall back to old format
      const tagName = toolType === 'create-file' ? 'create-file' : 'full-file-rewrite';
      const fileContentMatch = parsedContent.content.match(
        new RegExp(`<${tagName}[^>]*>([\\s\\S]*?)<\\/${tagName}>`, 'i'),
      );
      if (fileContentMatch) {
        return processFileContent(fileContentMatch[1]);
      }
    }
  } catch (e) {
    // Fall back to direct regex search if JSON parsing fails
  }

  // Check if it's the new XML format in raw content
  if (isNewXmlFormat(contentStr)) {
    const toolCalls = parseXmlToolCalls(contentStr);
    if (toolCalls.length > 0 && toolCalls[0].parameters.file_contents) {
      return processFileContent(toolCalls[0].parameters.file_contents);
    }
  }

  // Direct regex search in the content string (old format)
  const tagName = toolType === 'create-file' ? 'create-file' : 'full-file-rewrite';
  const fileContentMatch = contentStr.match(
    new RegExp(`<${tagName}[^>]*>([\\s\\S]*?)<\\/${tagName}>`, 'i'),
  );
  if (fileContentMatch) {
    return processFileContent(fileContentMatch[1]);
  }

  return null;
}

function processFileContent(content: string | object): string {
  if (!content) return '';
  if (typeof content === 'object') {
    return JSON.stringify(content, null, 2);
  }

  const trimmedContent = content.trim();
  const isLikelyJson = (trimmedContent.startsWith('{') && trimmedContent.endsWith('}')) ||
                       (trimmedContent.startsWith('[') && trimmedContent.endsWith(']'));
  
  if (isLikelyJson) {
    try {
      const parsed = JSON.parse(content);
      return JSON.stringify(parsed, null, 2);
    } catch (e) {
    }
  }
  return content
    .replace(/\\n/g, '\n')
    .replace(/\\t/g, '\t')
    .replace(/\\r/g, '')
    .replace(/\\\\/g, '\\')
    .replace(/\\"/g, '"')
    .replace(/\\'/g, "'");
}

// Helper to determine file type (for syntax highlighting)
export function getFileType(filePath: string): string {
  const extension = filePath.split('.').pop()?.toLowerCase() || '';

  switch (extension) {
    case 'js':
      return 'JavaScript';
    case 'ts':
      return 'TypeScript';
    case 'jsx':
    case 'tsx':
      return 'React';
    case 'py':
      return 'Python';
    case 'html':
      return 'HTML';
    case 'css':
      return 'CSS';
    case 'json':
      return 'JSON';
    case 'md':
      return 'Markdown';
    default:
      return extension.toUpperCase() || 'Text';
  }
}

// Helper to extract URL from browser navigate operations
export function extractBrowserUrl(content: string | object | undefined | null): string | null {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return null;
  
  const urlMatch = contentStr.match(/url=["'](https?:\/\/[^"']+)["']/);
  return urlMatch ? urlMatch[1] : null;
}

// Helper to extract browser operation type
export function extractBrowserOperation(toolName: string | undefined): string {
  if (!toolName) return 'Browser Operation';

  const operation = toolName.replace('browser-', '').replace(/-/g, ' ');
  return operation.charAt(0).toUpperCase() + operation.slice(1);
}

// Helper to extract search query
export function extractSearchQuery(content: string | object | undefined | null): string | null {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return null;

  // First, look for ToolResult pattern in the content string
  const toolResultMatch = contentStr.match(
    /ToolResult\(.*?output='([\s\S]*?)'.*?\)/,
  );
  
  if (toolResultMatch) {
    try {
      // Parse the output JSON from ToolResult
      const outputJson = JSON.parse(toolResultMatch[1]);
      
      // Check if this is the new Tavily response format with query field
      if (outputJson.query && typeof outputJson.query === 'string') {
        return outputJson.query;
      }
    } catch (e) {
      // Continue with other extraction methods
    }
  }

  let contentToSearch = contentStr; // Start with the normalized content

  // Try parsing as JSON first
  try {
    const parsedContent = JSON.parse(contentStr);
    
    // Check if it's the new Tavily response format
    if (parsedContent.query && typeof parsedContent.query === 'string') {
      return parsedContent.query;
    }
    
    // Continue with existing logic for backward compatibility
    if (typeof parsedContent.content === 'string') {
      // If the outer content is JSON and has a 'content' string field,
      // use that inner content for searching the query.
      contentToSearch = parsedContent.content;

      // Also check common JSON structures within the outer parsed object itself
      if (typeof parsedContent.query === 'string') {
        return parsedContent.query;
      }
      if (
        typeof parsedContent.arguments === 'object' &&
        parsedContent.arguments !== null &&
        typeof parsedContent.arguments.query === 'string'
      ) {
        return parsedContent.arguments.query;
      }
      if (
        Array.isArray(parsedContent.tool_calls) &&
        parsedContent.tool_calls.length > 0
      ) {
        const toolCall = parsedContent.tool_calls[0];
        if (
          typeof toolCall.arguments === 'object' &&
          toolCall.arguments !== null &&
          typeof toolCall.arguments.query === 'string'
        ) {
          return toolCall.arguments.query;
        }
        if (typeof toolCall.arguments === 'string') {
          try {
            const argsParsed = JSON.parse(toolCall.arguments);
            if (typeof argsParsed.query === 'string') {
              return argsParsed.query;
            }
          } catch {}
        }
      }
    }
  } catch (e) {
    // If parsing fails, continue with the original content string
  }

  // Now search within contentToSearch (either original or nested content)

  // 1. Try regex for attribute within <web-search ...> tag (with or without attributes)
  const xmlQueryMatch = contentToSearch.match(
    /<web-search[^>]*\s+query=[\"']([^\"']*)["'][^>]*>/i,
  );
  if (xmlQueryMatch && xmlQueryMatch[1]) {
    return xmlQueryMatch[1].trim();
  }

  // 2. Try simple attribute regex (fallback, less specific)
  const simpleAttrMatch = contentToSearch.match(/query=[\"']([\s\S]*?)["']/i);
  if (simpleAttrMatch && simpleAttrMatch[1]) {
    return simpleAttrMatch[1].split(/[\"']/)[0].trim();
  }

  // 4. If nothing found after checking original/nested content and JSON structure, return null
  return null;
}

// Helper to extract URLs and titles with regex
export function extractUrlsAndTitles(
  content: string,
): Array<{ title: string; url: string; snippet?: string }> {
  const results: Array<{ title: string; url: string; snippet?: string }> = [];

  // Try to parse as JSON first to extract proper results
  try {
    const parsed = JSON.parse(content);
    if (Array.isArray(parsed)) {
      return parsed.map(result => ({
        title: result.title || '',
        url: result.url || '',
        snippet: result.content || result.snippet || '',
      }));
    }
    if (parsed.results && Array.isArray(parsed.results)) {
      return parsed.results.map((result: any) => ({
        title: result.title || '',
        url: result.url || '',
        snippet: result.content || '',
      }));
    }
  } catch (e) {
    // Not valid JSON, continue with regex extraction
  }

  // Look for properly formatted JSON objects with title and url
  const jsonObjectPattern = /\{\s*"title"\s*:\s*"([^"]+)"\s*,\s*"url"\s*:\s*"(https?:\/\/[^"]+)"\s*(?:,\s*"content"\s*:\s*"([^"]*)")?\s*\}/g;
  let objectMatch;

  while ((objectMatch = jsonObjectPattern.exec(content)) !== null) {
    const title = objectMatch[1];
    const url = objectMatch[2];
    const snippet = objectMatch[3] || '';

    if (url && title && !results.some((r) => r.url === url)) {
      results.push({ title, url, snippet });
    }
  }

  // If we didn't find any results with the JSON fragment approach, fall back to standard URL extraction
  if (results.length === 0) {
    // Regex to find URLs, attempting to exclude common trailing unwanted characters/tags
    const urlRegex = /https?:\/\/[^\s"<]+/g;
    let match;

    while ((match = urlRegex.exec(content)) !== null) {
      let url = match[0];

      // --- Start: New Truncation Logic ---
      // Find the first occurrence of potential garbage separators like /n or \n after the protocol.
      const protocolEndIndex = url.indexOf('://');
      const searchStartIndex =
        protocolEndIndex !== -1 ? protocolEndIndex + 3 : 0;

      const newlineIndexN = url.indexOf('/n', searchStartIndex);
      const newlineIndexSlashN = url.indexOf('\\n', searchStartIndex);

      let firstNewlineIndex = -1;
      if (newlineIndexN !== -1 && newlineIndexSlashN !== -1) {
        firstNewlineIndex = Math.min(newlineIndexN, newlineIndexSlashN);
      } else if (newlineIndexN !== -1) {
        firstNewlineIndex = newlineIndexN;
      } else if (newlineIndexSlashN !== -1) {
        firstNewlineIndex = newlineIndexSlashN;
      }

      // If a newline indicator is found, truncate the URL there.
      if (firstNewlineIndex !== -1) {
        url = url.substring(0, firstNewlineIndex);
      }
      // --- End: New Truncation Logic ---

      // Basic cleaning: remove common tags or artifacts if they are directly appended
      url = url
        .replace(/<\/?url>$/, '')
        .replace(/<\/?content>$/, '')
        .replace(/%3C$/, ''); // Remove trailing %3C (less than sign)

      // Aggressive trailing character removal (common issues)
      // Apply this *after* potential truncation
      while (/[);.,\/]$/.test(url)) {
        url = url.slice(0, -1);
      }

      // Decode URI components to handle % sequences, but catch errors
      try {
        // Decode multiple times? Sometimes needed for double encoding
        url = decodeURIComponent(decodeURIComponent(url));
      } catch (e) {
        try {
          // Try decoding once if double decoding failed
          url = decodeURIComponent(url);
        } catch (e2) {
          console.warn('Failed to decode URL component:', url, e2);
        }
      }

      // Final cleaning for specific problematic sequences like ellipsis or remaining tags
      url = url.replace(/\u2026$/, ''); // Remove trailing ellipsis (…)
      url = url.replace(/<\/?url>$/, '').replace(/<\/?content>$/, ''); // Re-apply tag removal after decode

      // Try to find a title near this URL
      const urlIndex = match.index;
      const surroundingText = content.substring(
        Math.max(0, urlIndex - 100),
        urlIndex + url.length + 200,
      );

      // Look for title patterns more robustly
      const titleMatch =
        surroundingText.match(/title"?\s*:\s*"([^"]+)"/i) ||
        surroundingText.match(/Title[:\s]+([^\n<]+)/i) ||
        surroundingText.match(/\"(.*?)\"[\s\n]*?https?:\/\//);

      let title = cleanUrl(url); // Default to cleaned URL hostname/path
      if (titleMatch && titleMatch[1].trim()) {
        title = titleMatch[1].trim();
      }

      // Avoid adding duplicates if the cleaning resulted in the same URL
      if (url && !results.some((r) => r.url === url)) {
        results.push({
          title: title,
          url: url,
        });
      }
    }
  }

  return results;
}

// Helper to clean URL for display
export function cleanUrl(url: string): string {
  try {
    const urlObj = new URL(url);
    return (
      urlObj.hostname.replace('www.', '') +
      (urlObj.pathname !== '/' ? urlObj.pathname : '')
    );
  } catch (e) {
    return url;
  }
}

// Helper to extract URL for webpage crawling/scraping
export function extractCrawlUrl(content: string | object | undefined | null): string | null {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return null;

  try {
    // Try to parse content as JSON first (for the new format)
    const parsedContent = JSON.parse(contentStr);
    if (parsedContent.content) {
      // Look for URL in the content string (with or without attributes)
      const urlMatch = parsedContent.content.match(
        /<(?:crawl|scrape)-webpage[^>]*\s+url=["'](https?:\/\/[^"']+)["']/i,
      );
      if (urlMatch) return urlMatch[1];
    }
  } catch (e) {
    // Fall back to direct regex search if JSON parsing fails
  }

  // Direct regex search in the content string (updated to handle attributes)
  const urlMatch =
    contentStr.match(
      /<(?:crawl|scrape)-webpage[^>]*\s+url=["'](https?:\/\/[^"']+)["']/i,
    ) || contentStr.match(/url=["'](https?:\/\/[^"']+)["']/i);

  return urlMatch ? urlMatch[1] : null;
}

// Helper to extract webpage content from crawl/scrape result
export function extractWebpageContent(
  content: string | object | undefined | null,
): { title: string; text: string } | null {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return null;

  try {
    // Try to parse the JSON content
    const parsedContent = JSON.parse(contentStr);

    // Handle case where content is in parsedContent.content field
    if (parsedContent.content && typeof parsedContent.content === 'string') {
      // Look for tool_result tag (with attributes)
      const toolResultMatch = parsedContent.content.match(
        /<tool_result[^>]*>\s*<(?:crawl|scrape)-webpage[^>]*>([\s\S]*?)<\/(?:crawl|scrape)-webpage>\s*<\/tool_result>/,
      );
      if (toolResultMatch) {
        try {
          // Try to parse the content inside the tags
          const rawData = toolResultMatch[1];

          // Look for ToolResult pattern in the raw data
          const toolResultOutputMatch = rawData.match(
            /ToolResult\(.*?output='([\s\S]*?)'.*?\)/,
          );
          if (toolResultOutputMatch) {
            try {
              // If ToolResult pattern found, try to parse its output which may be a stringified JSON
              const outputJson = JSON.parse(
                toolResultOutputMatch[1]
                  .replace(/\\\\n/g, '\\n')
                  .replace(/\\\\u/g, '\\u'),
              );

              // Handle array format (first item)
              if (Array.isArray(outputJson) && outputJson.length > 0) {
                const item = outputJson[0];
                return {
                  title: item.Title || item.title || '',
                  text: item.Text || item.text || item.content || '',
                };
              }

              // Handle direct object format
              return {
                title: outputJson.Title || outputJson.title || '',
                text:
                  outputJson.Text ||
                  outputJson.text ||
                  outputJson.content ||
                  '',
              };
            } catch (e) {
              // If parsing fails, use the raw output
              return {
                title: 'Webpage Content',
                text: toolResultOutputMatch[1],
              };
            }
          }

          // Try to parse as direct JSON if no ToolResult pattern
          const crawlData = JSON.parse(rawData);

          // Handle array format
          if (Array.isArray(crawlData) && crawlData.length > 0) {
            const item = crawlData[0];
            return {
              title: item.Title || item.title || '',
              text: item.Text || item.text || item.content || '',
            };
          }

          // Handle direct object format
          return {
            title: crawlData.Title || crawlData.title || '',
            text: crawlData.Text || crawlData.text || crawlData.content || '',
          };
        } catch (e) {
          // Fallback to basic text extraction
          return {
            title: 'Webpage Content',
            text: toolResultMatch[1],
          };
        }
      }

      // Handle ToolResult pattern in the content directly
      const toolResultOutputMatch = parsedContent.content.match(
        /ToolResult\(.*?output='([\s\S]*?)'.*?\)/,
      );
      if (toolResultOutputMatch) {
        try {
          // Parse the output which might be a stringified JSON
          const outputJson = JSON.parse(
            toolResultOutputMatch[1]
              .replace(/\\\\n/g, '\\n')
              .replace(/\\\\u/g, '\\u'),
          );

          // Handle array format
          if (Array.isArray(outputJson) && outputJson.length > 0) {
            const item = outputJson[0];
            return {
              title: item.Title || item.title || '',
              text: item.Text || item.text || item.content || '',
            };
          }

          // Handle direct object format
          return {
            title: outputJson.Title || outputJson.title || '',
            text:
              outputJson.Text || outputJson.text || outputJson.content || '',
          };
        } catch (e) {
          // If parsing fails, use the raw output
          return {
            title: 'Webpage Content',
            text: toolResultOutputMatch[1],
          };
        }
      }
    }

    // Direct handling of <crawl-webpage> or <scrape-webpage> format outside of content field (with attributes)
    const webpageMatch = contentStr.match(
      /<(?:crawl|scrape)-webpage[^>]*>([\s\S]*?)<\/(?:crawl|scrape)-webpage>/,
    );
    if (webpageMatch) {
      const rawData = webpageMatch[1];

      // Look for ToolResult pattern
      const toolResultOutputMatch = rawData.match(
        /ToolResult\(.*?output='([\s\S]*?)'.*?\)/,
      );
      if (toolResultOutputMatch) {
        try {
          // Parse the output which might be a stringified JSON
          const outputString = toolResultOutputMatch[1]
            .replace(/\\\\n/g, '\\n')
            .replace(/\\\\u/g, '\\u');
          const outputJson = JSON.parse(outputString);

          // Handle array format
          if (Array.isArray(outputJson) && outputJson.length > 0) {
            const item = outputJson[0];
            return {
              title:
                item.Title ||
                item.title ||
                (item.URL ? new URL(item.URL).hostname : ''),
              text: item.Text || item.text || item.content || '',
            };
          }

          // Handle direct object format
          return {
            title: outputJson.Title || outputJson.title || '',
            text:
              outputJson.Text || outputJson.text || outputJson.content || '',
          };
        } catch (e) {
          // If parsing fails, use the raw output
          return {
            title: 'Webpage Content',
            text: toolResultOutputMatch[1],
          };
        }
      }
    }

    // Direct content extraction from parsed JSON if it's an array
    if (Array.isArray(parsedContent) && parsedContent.length > 0) {
      const item = parsedContent[0];
      return {
        title: item.Title || item.title || '',
        text: item.Text || item.text || item.content || '',
      };
    }

    // Direct content extraction from parsed JSON as object
    if (typeof parsedContent === 'object' && parsedContent !== null) {
      // Check if it's already the webpage data (new format after double-escape fix)
      if ('Title' in parsedContent || 'title' in parsedContent || 'Text' in parsedContent || 'text' in parsedContent) {
      return {
        title: parsedContent.Title || parsedContent.title || 'Webpage Content',
        text:
          parsedContent.Text ||
          parsedContent.text ||
          parsedContent.content ||
            '',
        };
      }
      
      // Otherwise, try to stringify it
      return {
        title: 'Webpage Content',
        text: JSON.stringify(parsedContent),
      };
    }
  } catch (e) {
    // Last resort, try to match the ToolResult pattern directly in the raw content
    const toolResultMatch = contentStr.match(
      /ToolResult\(.*?output='([\s\S]*?)'.*?\)/,
    );
    if (toolResultMatch) {
      try {
        // Try to parse the output which might be a stringified JSON
        const outputJson = JSON.parse(
          toolResultMatch[1].replace(/\\\\n/g, '\\n').replace(/\\\\u/g, '\\u'),
        );

        // Handle array format
        if (Array.isArray(outputJson) && outputJson.length > 0) {
          const item = outputJson[0];
          return {
            title: item.Title || item.title || '',
            text: item.Text || item.text || item.content || '',
          };
        }

        // Handle direct object format
        return {
          title: outputJson.Title || outputJson.title || '',
          text: outputJson.Text || outputJson.text || outputJson.content || '',
        };
      } catch (e) {
        // If parsing fails, use the raw output
        return {
          title: 'Webpage Content',
          text: toolResultMatch[1],
        };
      }
    }

    // If all else fails, return the content as-is
    if (contentStr) {
      return {
        title: 'Webpage Content',
        text: contentStr,
      };
    }
  }

  return null;
}

// Helper to extract search results from tool response
export function extractSearchResults(
  content: string | object | undefined | null,
): Array<{ title: string; url: string; snippet?: string }> {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return [];

    try {
    // Instead of trying to parse the complex ToolResult JSON, 
    // let's look for the results array pattern directly in the content
    
    // Look for the results array pattern within the content
    const resultsPattern = /"results":\s*\[([^\]]*(?:\[[^\]]*\][^\]]*)*)\]/;
    const resultsMatch = contentStr.match(resultsPattern);
    
    if (resultsMatch) {
      try {
        // Extract just the results array and parse it
        const resultsArrayStr = '[' + resultsMatch[1] + ']';
        const results = JSON.parse(resultsArrayStr);
        
        if (Array.isArray(results)) {
          return results.map(result => ({
            title: result.title || '',
            url: result.url || '',
            snippet: result.content || '',
          }));
        }
      } catch (e) {
        console.warn('Failed to parse results array:', e);
      }
    }
    
    // Fallback: Look for individual result objects
    const resultObjectPattern = /\{\s*"url":\s*"([^"]+)"\s*,\s*"title":\s*"([^"]+)"\s*,\s*"content":\s*"([^"]*)"[^}]*\}/g;
    const results = [];
    let match;
    
    while ((match = resultObjectPattern.exec(contentStr)) !== null) {
      results.push({
        url: match[1],
        title: match[2],
        snippet: match[3],
      });
    }
    
    if (results.length > 0) {
      return results;
    }

    // Try parsing the entire content as JSON (for direct Tavily responses)
    const parsedContent = JSON.parse(contentStr);
    
    // Check if this is the new Tavily response format
    if (parsedContent.results && Array.isArray(parsedContent.results)) {
      return parsedContent.results.map((result: any) => ({
        title: result.title || '',
        url: result.url || '',
        snippet: result.content || '',
      }));
    }
    
    // Continue with existing logic for backward compatibility
    if (parsedContent.content && typeof parsedContent.content === 'string') {
      // Look for a tool_result tag (with attributes)
      const toolResultTagMatch = parsedContent.content.match(
        /<tool_result[^>]*>\s*<web-search[^>]*>([\s\S]*?)<\/web-search>\s*<\/tool_result>/,
      );
      if (toolResultTagMatch) {
        // Try to parse the results array
        try {
          return JSON.parse(toolResultTagMatch[1]);
        } catch (e) {
          // Fallback to regex extraction of URLs and titles
          return extractUrlsAndTitles(toolResultTagMatch[1]);
        }
      }

      // Try to find JSON array in the content
      const jsonArrayMatch = parsedContent.content.match(/\[\s*{[\s\S]*}\s*\]/);
      if (jsonArrayMatch) {
        try {
          return JSON.parse(jsonArrayMatch[0]);
        } catch (e) {
          return extractUrlsAndTitles(parsedContent.content);
        }
      }

      // If none of the above worked, try the whole content
      return extractUrlsAndTitles(parsedContent.content);
    }
  } catch (e) {
    // If JSON parsing fails, extract directly from the content
    return extractUrlsAndTitles(contentStr);
  }

  // Last resort fallback
  return extractUrlsAndTitles(contentStr);
}

// Function to determine which tool component to render based on the tool name
export function getToolComponent(toolName: string): string {
  if (!toolName) return 'GenericToolView';

  const normalizedName = toolName.toLowerCase();

  // Map specific tool names to their respective components
  switch (normalizedName) {
    // Browser tools
    case 'browser-navigate':
    case 'browser-click':
    case 'browser-extract':
    case 'browser-fill':
    case 'browser-wait':
    case 'browser-screenshot':
      return 'BrowserToolView';

    // Command execution
    case 'execute-command':
      return 'CommandToolView';

    // File operations
    case 'create-file':
    case 'delete-file':
    case 'full-file-rewrite':
    case 'read-file':
      return 'FileOperationToolView';

    // String operations
    case 'str-replace':
      return 'StrReplaceToolView';

    // Web operations
    case 'web-search':
      return 'WebSearchToolView';
    case 'crawl-webpage':
      return 'WebCrawlToolView';
    case 'scrape-webpage':
      return 'WebScrapeToolView';

    // Data provider operations
    case 'execute-data-provider-call':
    case 'get-data-provider-endpoints':
      return 'DataProviderToolView';


    //Deploy
    case 'deploy':
      return 'DeployToolView';

    // Default
    default:
      return 'GenericToolView';
  }
}

// Helper function to normalize content to string
export function normalizeContentToString(content: string | object | undefined | null): string | null {
  if (!content) return null;
  
  if (typeof content === 'string') {
    // Check if it's a double-escaped JSON string (old format)
    if (content.startsWith('"{') && content.endsWith('}"')) {
      try {
        // First parse to get the inner JSON string
        const innerString = JSON.parse(content);
        // Then parse the inner string to get the actual object
        const parsed = JSON.parse(innerString);
        // Return the content field if it exists
        if (parsed && typeof parsed === 'object' && 'content' in parsed) {
          return parsed.content;
        }
        // Otherwise return the stringified object
        return JSON.stringify(parsed);
      } catch (e) {
        // If parsing fails, return as is
      }
    }
    return content;
  }
  
  if (typeof content === 'object' && content !== null) {
    try {
      // Handle case where content is a parsed object with content field (new format)
      if ('content' in content && typeof content.content === 'string') {
        return content.content;
      } 
      // Handle case where content is a parsed object with content field that's also an object
      else if ('content' in content && typeof content.content === 'object' && content.content !== null) {
        // Check if the nested content has a content field
        if ('content' in content.content && typeof content.content.content === 'string') {
          return content.content.content;
        }
        // Try to stringify nested content object
        return JSON.stringify(content.content);
      }
      // Handle message format {role: 'tool', content: '...'}
      else if ('role' in content && 'content' in content && typeof content.content === 'string') {
        return content.content;
      } 
      // Handle nested message format {role: 'assistant', content: {role: 'assistant', content: '...'}}
      else if ('role' in content && 'content' in content && typeof content.content === 'object' && content.content !== null) {
        if ('content' in content.content && typeof content.content.content === 'string') {
          return content.content.content;
        }
        // Try to stringify nested content object
        return JSON.stringify(content.content);
      } 
      // Handle direct object that might be the content itself (new format)
      else {
        // If it looks like it might contain XML or structured content, stringify it
        const stringified = JSON.stringify(content);
        // Check if the stringified version contains XML tags or other structured content
        if (stringified.includes('<') || stringified.includes('file_path') || stringified.includes('command')) {
          return stringified;
        }
        // Otherwise, try to extract meaningful content
        return stringified;
      }
    } catch (e) {
      console.error('Error in normalizeContentToString:', e, 'Content:', content);
      return null;
    }
  }
  
  return null;
}

// Helper function to extract file content for streaming (handles incomplete XML)
export function extractStreamingFileContent(
  content: string | object | undefined | null,
  toolType: 'create-file' | 'full-file-rewrite',
): string | null {
  const contentStr = normalizeContentToString(content);
  if (!contentStr) return null;

  const tagName = toolType === 'create-file' ? 'create-file' : 'full-file-rewrite';
  
  // First check if content is already a parsed object (new format)
  if (typeof content === 'object' && content !== null) {
    try {
      if ('content' in content && typeof content.content === 'string') {
        // Look for the opening tag
        const openTagMatch = content.content.match(new RegExp(`<${tagName}[^>]*>`, 'i'));
        if (openTagMatch) {
          // Find where the tag ends
          const tagEndIndex = content.content.indexOf(openTagMatch[0]) + openTagMatch[0].length;
          // Extract everything after the opening tag
          const afterTag = content.content.substring(tagEndIndex);
          
          // Check if there's a closing tag
          const closeTagMatch = afterTag.match(new RegExp(`<\\/${tagName}>`, 'i'));
          if (closeTagMatch) {
            // Return content between tags
            return processFileContent(afterTag.substring(0, closeTagMatch.index));
          } else {
            // No closing tag yet (streaming), return what we have
            return processFileContent(afterTag);
          }
        }
      }
    } catch (e) {
      // Continue with string parsing
    }
  }

  // Fallback to string-based extraction
  // Look for the opening tag
  const openTagMatch = contentStr.match(new RegExp(`<${tagName}[^>]*>`, 'i'));
  if (openTagMatch) {
    // Find where the tag ends
    const tagEndIndex = contentStr.indexOf(openTagMatch[0]) + openTagMatch[0].length;
    // Extract everything after the opening tag
    const afterTag = contentStr.substring(tagEndIndex);
    
    // Check if there's a closing tag
    const closeTagMatch = afterTag.match(new RegExp(`<\\/${tagName}>`, 'i'));
    if (closeTagMatch) {
      // Return content between tags
      return processFileContent(afterTag.substring(0, closeTagMatch.index));
    } else {
      // No closing tag yet (streaming), return what we have
      return processFileContent(afterTag);
    }
  }

  return null;
}

export const getFileIconAndColor = (filename: string) => {
  const ext = filename.split('.').pop()?.toLowerCase();
  
  switch (ext) {
    case 'js':
    case 'jsx':
    case 'ts':
    case 'tsx':
      return { 
        icon: FileCode, 
        color: 'text-yellow-500 dark:text-yellow-400', 
        bgColor: 'bg-gradient-to-br from-yellow-500/20 to-yellow-600/10 border border-yellow-500/20' 
      };
    case 'py':
      return { 
        icon: FileCode, 
        color: 'text-blue-500 dark:text-blue-400', 
        bgColor: 'bg-gradient-to-br from-blue-500/20 to-blue-600/10 border border-blue-500/20' 
      };
    case 'html':
    case 'css':
    case 'scss':
      return { 
        icon: FileCode, 
        color: 'text-orange-500 dark:text-orange-400', 
        bgColor: 'bg-gradient-to-br from-orange-500/20 to-orange-600/10 border border-orange-500/20' 
      };
    
    // Data files
    case 'json':
      return { 
        icon: FileJson, 
        color: 'text-green-500 dark:text-green-400', 
        bgColor: 'bg-gradient-to-br from-green-500/20 to-green-600/10 border border-green-500/20' 
      };
    case 'csv':
      return { 
        icon: Table, 
        color: 'text-emerald-500 dark:text-emerald-400', 
        bgColor: 'bg-gradient-to-br from-emerald-500/20 to-emerald-600/10 border border-emerald-500/20' 
      };
    case 'xml':
    case 'yaml':
    case 'yml':
      return { 
        icon: FileCode, 
        color: 'text-purple-500 dark:text-purple-400', 
        bgColor: 'bg-gradient-to-br from-purple-500/20 to-purple-600/10 border border-purple-500/20' 
      };
    
    // Image files
    case 'jpg':
    case 'jpeg':
    case 'png':
    case 'gif':
    case 'svg':
    case 'webp':
      return { 
        icon: FileImage, 
        color: 'text-pink-500 dark:text-pink-400', 
        bgColor: 'bg-gradient-to-br from-pink-500/20 to-pink-600/10 border border-pink-500/20' 
      };
    
    // Document files
    case 'md':
    case 'mdx':
      return { 
        icon: FileText, 
        color: 'text-slate-500 dark:text-slate-400', 
        bgColor: 'bg-gradient-to-br from-slate-500/20 to-slate-600/10 border border-slate-500/20' 
      };
    case 'txt':
      return { 
        icon: FileText, 
        color: 'text-zinc-500 dark:text-zinc-400', 
        bgColor: 'bg-gradient-to-br from-zinc-500/20 to-zinc-600/10 border border-zinc-500/20' 
      };
    case 'pdf':
      return { 
        icon: FileType, 
        color: 'text-red-500 dark:text-red-400', 
        bgColor: 'bg-gradient-to-br from-red-500/20 to-red-600/10 border border-red-500/20' 
      };
    
    // Media files
    case 'mp4':
    case 'avi':
    case 'mov':
      return { 
        icon: FileVideo, 
        color: 'text-indigo-500 dark:text-indigo-400', 
        bgColor: 'bg-gradient-to-br from-indigo-500/20 to-indigo-600/10 border border-indigo-500/20' 
      };
    case 'mp3':
    case 'wav':
    case 'ogg':
      return { 
        icon: FileAudio, 
        color: 'text-teal-500 dark:text-teal-400', 
        bgColor: 'bg-gradient-to-br from-teal-500/20 to-teal-600/10 border border-teal-500/20' 
      };
    
    // Archive files
    case 'zip':
    case 'tar':
    case 'gz':
    case 'rar':
      return { 
        icon: FileArchive, 
        color: 'text-amber-500 dark:text-amber-400', 
        bgColor: 'bg-gradient-to-br from-amber-500/20 to-amber-600/10 border border-amber-500/20' 
      };
    
    // Default
    default:
      if (!ext || filename.includes('/')) {
        return { 
          icon: FolderOpen, 
          color: 'text-blue-500 dark:text-blue-400', 
          bgColor: 'bg-gradient-to-br from-blue-500/20 to-blue-600/10 border border-blue-500/20' 
        };
      }
      return { 
        icon: File, 
        color: 'text-zinc-500 dark:text-zinc-400', 
        bgColor: 'bg-gradient-to-br from-zinc-500/20 to-zinc-600/10 border border-zinc-500/20' 
      };
  }
};

/**
 * Extract tool data from content using the new parser with backwards compatibility
 */
export function extractToolData(content: any): {
  toolResult: ParsedToolResult | null;
  arguments: Record<string, any>;
  filePath: string | null;
  fileContent: string | null;
  command: string | null;
  url: string | null;
  query: string | null;
} {
  const toolResult = parseToolResult(content);
  
  if (toolResult) {
    const args = toolResult.arguments || {};
    return {
      toolResult,
      arguments: args,
      filePath: args.file_path || args.path || null,
      fileContent: args.file_contents || args.content || null,
      command: args.command || null,
      url: args.url || null,
      query: args.query || null,
    };
  }

  // Fallback to legacy parsing if new format not detected
  return {
    toolResult: null,
    arguments: {},
    filePath: null,
    fileContent: null,
    command: null,
    url: null,
    query: null,
  };
}