codewhale_core/
context_reference.rs1use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14pub struct ContextReference {
15 pub kind: ContextReferenceKind,
16 pub source: ContextReferenceSource,
17 pub badge: String,
19 pub label: String,
21 pub target: String,
23 pub included: bool,
24 pub expanded: bool,
25 #[serde(default, skip_serializing_if = "Option::is_none")]
26 pub detail: Option<String>,
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
30#[serde(rename_all = "snake_case")]
31pub enum ContextReferenceKind {
32 File,
33 Directory,
34 Missing,
35 Unsupported,
36 MediaMention,
37 MediaAttachment,
38 GitContext,
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
43#[serde(rename_all = "snake_case")]
44pub enum ContextReferenceSource {
45 AtMention,
46 Attachment,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct MediaAttachmentReference {
51 pub kind: String,
52 pub path: String,
53 pub start_byte: usize,
54 pub end_byte: usize,
55}
56
57#[must_use]
59pub fn media_attachment_references(input: &str) -> Vec<MediaAttachmentReference> {
60 let mut out = Vec::new();
61 let mut offset = 0usize;
62 for line in input.split_inclusive('\n') {
63 let start_byte = offset;
64 let end_byte = offset + line.len();
65 offset = end_byte;
66 let trimmed = line.trim();
67 let Some(body) = trimmed
68 .strip_prefix("[Attached ")
69 .and_then(|value| value.strip_suffix(']'))
70 else {
71 continue;
72 };
73 let Some((kind, rest)) = body.split_once(": ") else {
74 continue;
75 };
76 let path = rest
77 .rsplit_once(" at ")
78 .map_or(rest, |(_, path)| path)
79 .trim();
80 if !path.is_empty() {
81 out.push(MediaAttachmentReference {
82 kind: kind.trim().to_string(),
83 path: path.to_string(),
84 start_byte,
85 end_byte,
86 });
87 }
88 }
89 out
90}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95
96 #[test]
97 fn serialization_roundtrip() {
98 let reference = ContextReference {
99 kind: ContextReferenceKind::File,
100 source: ContextReferenceSource::AtMention,
101 badge: "file".to_string(),
102 label: "test.rs".to_string(),
103 target: "/path/to/test.rs".to_string(),
104 included: true,
105 expanded: false,
106 detail: Some("included".to_string()),
107 };
108 let json = serde_json::to_string(&reference).unwrap();
109 let deserialized: ContextReference = serde_json::from_str(&json).unwrap();
110 assert_eq!(reference, deserialized);
111 }
112
113 #[test]
114 fn parses_media_attachments() {
115 let input = "Here is the screenshot:\n[Attached image: 100x100 at /tmp/shot.png]\nPlease analyze it.";
116 let refs = media_attachment_references(input);
117 assert_eq!(refs.len(), 1);
118 assert_eq!(refs[0].kind, "image");
119 assert_eq!(refs[0].path, "/tmp/shot.png");
120 }
121}