Skip to main content

formal_ai/protocol/
content.rs

1use super::MessageContent;
2
3impl MessageContent {
4    #[must_use]
5    pub fn plain_text(&self) -> String {
6        match self {
7            Self::Text(text) => text.clone(),
8            Self::Parts(parts) => parts
9                .iter()
10                .filter_map(|part| part.text.as_deref())
11                .collect::<Vec<_>>()
12                .join("\n"),
13        }
14    }
15
16    /// User-authored request text with client-injected startup metadata removed.
17    ///
18    /// Qwen Code places `<system-reminder>` blocks in a `user` content part.
19    /// Those blocks describe the client and its deferred tools; treating them as
20    /// the task lets their keywords override the actual request that follows.
21    #[must_use]
22    pub fn user_request_text(&self) -> String {
23        strip_system_reminders(&self.plain_text())
24    }
25}
26
27fn strip_system_reminders(text: &str) -> String {
28    const OPEN: &str = "<system-reminder>";
29    const CLOSE: &str = "</system-reminder>";
30    let mut remaining = text;
31    let mut request = String::new();
32    while let Some(start) = remaining.find(OPEN) {
33        request.push_str(&remaining[..start]);
34        let after_open = &remaining[start + OPEN.len()..];
35        let Some(end) = after_open.find(CLOSE) else {
36            remaining = "";
37            break;
38        };
39        remaining = &after_open[end + CLOSE.len()..];
40    }
41    request.push_str(remaining);
42    request.trim().to_owned()
43}