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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
use nom::Parser;
use nom::bytes::complete::tag;

use crate::{FileInfo, Image};

/// A file or directory attachment included in a chat message.
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq, Eq)]
pub struct Attachment {
    /// The resolved content of the attachment (image, file text, or directory
    /// listing).
    pub content: AttachmentContent,
    /// The original path or URL string used to reference this attachment.
    pub path: String,
}

/// The resolved content of an attachment, discriminated by the type of resource
/// it represents.
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq, Eq)]
pub enum AttachmentContent {
    /// A binary image file encoded for inline display.
    Image(Image),
    /// A text file, optionally restricted to a line range.
    FileContent {
        /// Line-numbered display text shown to the model. May represent only a
        /// slice of the full file when a range was requested.
        content: String,
        /// Metadata about the file read: line positions and full-file content
        /// hash for external-change detection.
        info: FileInfo,
    },
    /// A directory listing showing the immediate children of a directory.
    DirectoryListing {
        /// Entries contained in the directory.
        entries: Vec<DirectoryEntry>,
    },
}

/// A single entry within a directory listing attachment.
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq, Eq)]
pub struct DirectoryEntry {
    /// Path of the entry relative to the listed directory.
    pub path: String,
    /// Whether this entry is itself a directory.
    pub is_dir: bool,
}

impl AttachmentContent {
    pub fn as_image(&self) -> Option<&Image> {
        match self {
            AttachmentContent::Image(image) => Some(image),
            _ => None,
        }
    }

    pub fn contains(&self, text: &str) -> bool {
        match self {
            AttachmentContent::Image(_) => false,
            AttachmentContent::FileContent { content, .. } => content.contains(text),
            AttachmentContent::DirectoryListing { .. } => false,
        }
    }

    pub fn file_content(&self) -> Option<&str> {
        match self {
            AttachmentContent::FileContent { content, .. } => Some(content),
            _ => None,
        }
    }

    pub fn range_info(&self) -> Option<(u64, u64, u64)> {
        match self {
            AttachmentContent::FileContent { info, .. } => {
                Some((info.start_line, info.end_line, info.total_lines))
            }
            _ => None,
        }
    }
}

impl Attachment {
    /// Parses a string and extracts all file paths in the format
    /// @[path/to/file]. File paths can contain spaces and are considered to
    /// extend until the closing bracket. If the closing bracket is missing,
    /// consider everything until the end of the string as the path.
    pub fn parse_all<T: ToString>(text: T) -> Vec<FileTag> {
        let input = text.to_string();
        let mut remaining = input.as_str();
        let mut tags = Vec::new();

        while !remaining.is_empty() {
            // Find the next "@[" pattern
            if let Some(start_pos) = remaining.find("@[") {
                // Move to the position where "@[" starts
                remaining = remaining.get(start_pos..).unwrap_or("");
                match FileTag::parse(remaining) {
                    Ok((next_remaining, file_tag)) => {
                        tags.push(file_tag);
                        remaining = next_remaining;
                    }
                    Err(_e) => {
                        // Skip the "@[" since we couldn't parse it
                        remaining = remaining.get(2..).unwrap_or("");
                    }
                }
            } else {
                // No more "@[" patterns found
                break;
            }
        }

        let mut seen = std::collections::HashSet::new();
        tags.retain(|tag| seen.insert((tag.path.clone(), tag.loc.clone(), tag.symbol.clone())));

        tags
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Location {
    pub start: Option<u64>,
    pub end: Option<u64>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FileTag {
    pub path: String,
    pub loc: Option<Location>,
    pub symbol: Option<String>,
}

impl FileTag {
    pub fn parse(input: &str) -> nom::IResult<&str, FileTag> {
        use nom::bytes::complete::take_while1;
        use nom::character::complete::{char, digit1};
        use nom::combinator::{map_res, opt};
        use nom::sequence::{delimited, preceded};

        let parse_u64 = || map_res(digit1, str::parse::<u64>);
        let parse_symbol = preceded(char('#'), take_while1(|c: char| c != ']'));

        let parse_location_full = (
            preceded(char(':'), parse_u64()),
            preceded(char(':'), parse_u64()),
        );
        let parse_location_start_only = preceded(char(':'), parse_u64());

        let parse_location = nom::branch::alt((
            nom::combinator::map(parse_location_full, |(start, end)| (Some(start), Some(end))),
            nom::combinator::map(parse_location_start_only, |start| (Some(start), None)),
        ));

        let parse_path = nom::branch::alt((
            // Try Windows drive path first (letter:path)
            nom::combinator::recognize((
                nom::character::complete::satisfy(|c| c.is_ascii_alphabetic()),
                nom::character::complete::char(':'),
                take_while1(|c: char| c != ':' && c != '#' && c != ']'),
            )),
            // Fall back to regular path parsing
            take_while1(|c: char| c != ':' && c != '#' && c != ']'),
        ));
        let mut parser = delimited(
            tag("@["),
            (parse_path, opt(parse_location), opt(parse_symbol)),
            char(']'),
        );

        let (remaining, (path, location, symbol)) = parser.parse(input)?;
        let loc = location.map(|(start, end)| Location { start, end });
        Ok((
            remaining,
            FileTag {
                path: path.to_string(),
                loc,
                symbol: symbol.map(|s| s.to_string()),
            },
        ))
    }
}

impl AsRef<std::path::Path> for FileTag {
    fn as_ref(&self) -> &std::path::Path {
        std::path::Path::new(&self.path)
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn test_attachment_parse_all_empty() {
        let text = String::from("No attachments here");
        let attachments = Attachment::parse_all(text);
        assert!(attachments.is_empty());
    }

    #[test]
    fn test_attachment_parse_all_simple() {
        let text = String::from("Check this file @[/path/to/file.txt]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let path_found = paths.first().unwrap();
        assert_eq!(path_found.path, "/path/to/file.txt");
    }

    #[test]
    fn test_attachment_parse_all_with_spaces() {
        let text = String::from("Check this file @[/path/with spaces/file.txt]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let path_found = paths.first().unwrap();
        assert_eq!(path_found.path, "/path/with spaces/file.txt");
    }

    #[test]
    fn test_attachment_parse_all_multiple() {
        let text = String::from(
            "Check @[/file1.txt] and also @[/path/with spaces/file2.txt] and @[/file3.txt]",
        );
        let paths = Attachment::parse_all(text);
        let paths = paths
            .iter()
            .map(|tag| tag.path.as_str())
            .collect::<Vec<_>>();
        assert_eq!(paths.len(), 3);

        assert!(paths.contains(&"/file1.txt"));
        assert!(paths.contains(&"/path/with spaces/file2.txt"));
        assert!(paths.contains(&"/file3.txt"));
    }

    #[test]
    fn test_attachment_parse_all_at_end() {
        let text = String::from("Check this file @[");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 0);
    }

    #[test]
    fn test_attachment_parse_all_unclosed_bracket() {
        let text = String::from("Check this file @[/path/with spaces/unclosed");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 0);
    }

    #[test]
    fn test_attachment_parse_all_with_multibyte_chars() {
        let text = String::from(
            "Check this file @[🚀/path/with spaces/file.txt🔥] and also @[🌟simple_path]",
        );
        let paths = Attachment::parse_all(text);
        let paths = paths
            .iter()
            .map(|tag| tag.path.as_str())
            .collect::<Vec<_>>();
        assert_eq!(paths.len(), 2);

        assert!(paths.contains(&"🚀/path/with spaces/file.txt🔥"));
        assert!(paths.contains(&"🌟simple_path"));
    }

    #[test]
    fn test_attachment_parse_with_location() {
        let text = String::from("Check line @[/path/to/file.txt:10:20]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "/path/to/file.txt".to_string(),
            loc: Some(Location { start: Some(10), end: Some(20) }),
            symbol: None,
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_with_symbol() {
        let text = String::from("Check function @[/path/to/file.rs#my_function]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "/path/to/file.rs".to_string(),
            loc: None,
            symbol: Some("my_function".to_string()),
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_with_location_and_symbol() {
        let text = String::from("Check @[/src/main.rs:5:15#main_function]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "/src/main.rs".to_string(),
            loc: Some(Location { start: Some(5), end: Some(15) }),
            symbol: Some("main_function".to_string()),
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_multiple_with_mixed_features() {
        let text = String::from(
            "Check @[/file1.txt] and @[/file2.rs:10:20] and @[/file3.py#function] and @[/file4.js:1:5#init]",
        );
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 4);

        let expected = vec![
            FileTag { path: "/file1.txt".to_string(), loc: None, symbol: None },
            FileTag {
                path: "/file2.rs".to_string(),
                loc: Some(Location { start: Some(10), end: Some(20) }),
                symbol: None,
            },
            FileTag {
                path: "/file3.py".to_string(),
                loc: None,
                symbol: Some("function".to_string()),
            },
            FileTag {
                path: "/file4.js".to_string(),
                loc: Some(Location { start: Some(1), end: Some(5) }),
                symbol: Some("init".to_string()),
            },
        ];

        for expected_tag in expected {
            assert!(paths.contains(&expected_tag));
        }
    }

    #[test]
    fn test_attachment_parse_symbol_with_special_chars() {
        let text = String::from("Check @[/file.rs#function_with_underscore_123]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "/file.rs".to_string(),
            loc: None,
            symbol: Some("function_with_underscore_123".to_string()),
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_location_edge_cases() {
        let text = String::from("Check @[/file.txt:0:999999]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "/file.txt".to_string(),
            loc: Some(Location { start: Some(0), end: Some(999999) }),
            symbol: None,
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_location_with_start() {
        let text = String::from("Check @[/file.txt:12#main()]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "/file.txt".to_string(),
            loc: Some(Location { start: Some(12), end: None }),
            symbol: Some("main()".to_string()),
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_location_duplicate_entries() {
        let text = String::from("Check @[/file.txt:12#main()] and @[/file.txt:12#main()]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "/file.txt".to_string(),
            loc: Some(Location { start: Some(12), end: None }),
            symbol: Some("main()".to_string()),
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_windows_drive_path() {
        let text = String::from("Check @[C:\\Users\\test\\file.txt:10:20]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "C:\\Users\\test\\file.txt".to_string(),
            loc: Some(Location { start: Some(10), end: Some(20) }),
            symbol: None,
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_windows_drive_simple() {
        let text = String::from("Check @[D:\\file.txt]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag { path: "D:\\file.txt".to_string(), loc: None, symbol: None };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_windows_drive_with_symbol() {
        let text = String::from("Check @[E:\\src\\main.rs#function_name]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "E:\\src\\main.rs".to_string(),
            loc: None,
            symbol: Some("function_name".to_string()),
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_windows_drive_with_line_start_only() {
        let text = String::from("Check @[F:\\project\\lib.rs:42]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "F:\\project\\lib.rs".to_string(),
            loc: Some(Location { start: Some(42), end: None }),
            symbol: None,
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_windows_drive_with_line_range_and_symbol() {
        let text = String::from("Check @[G:\\code\\test.rs:5:15#test_function]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "G:\\code\\test.rs".to_string(),
            loc: Some(Location { start: Some(5), end: Some(15) }),
            symbol: Some("test_function".to_string()),
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_linux_path_with_line_numbers() {
        let text = String::from("Check @[/home/user/project/file.rs:25:30]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "/home/user/project/file.rs".to_string(),
            loc: Some(Location { start: Some(25), end: Some(30) }),
            symbol: None,
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_linux_path_with_line_start_only() {
        let text = String::from("Check @[/var/log/app.log:100]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "/var/log/app.log".to_string(),
            loc: Some(Location { start: Some(100), end: None }),
            symbol: None,
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_unix_path_simple() {
        let text = String::from("Check @[/usr/local/bin/app]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "/usr/local/bin/app".to_string(),
            loc: None,
            symbol: None,
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_unix_path_with_symbol() {
        let text = String::from("Check @[/opt/project/src/main.c#main]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "/opt/project/src/main.c".to_string(),
            loc: None,
            symbol: Some("main".to_string()),
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_unix_path_with_line_and_symbol() {
        let text = String::from("Check @[/tmp/script.sh:10#setup_function]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 1);

        let expected = FileTag {
            path: "/tmp/script.sh".to_string(),
            loc: Some(Location { start: Some(10), end: None }),
            symbol: Some("setup_function".to_string()),
        };
        let actual = paths.first().unwrap();
        assert_eq!(actual, &expected);
    }

    #[test]
    fn test_attachment_parse_mixed_unix_and_windows() {
        let text = String::from("Check @[/unix/path.txt] and @[C:\\windows\\path.txt]");
        let paths = Attachment::parse_all(text);
        assert_eq!(paths.len(), 2);

        let expected_unix = FileTag { path: "/unix/path.txt".to_string(), loc: None, symbol: None };
        let expected_windows = FileTag {
            path: "C:\\windows\\path.txt".to_string(),
            loc: None,
            symbol: None,
        };

        assert!(paths.contains(&expected_unix));
        assert!(paths.contains(&expected_windows));
    }
}