BlueGit

ict rusty-teams

Files

rusty-teams / crates / agents / src / parse / mod.rs

mod.rs

mod.rs 1.8 KiB Download Raw
1mod claude;
2mod copilot;
3mod text;
4
5use std::time::Duration;
6
7use crate::profile::OutputFormat;
8
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub enum RunEvent {
11 Step(String),
12 Text(String),
13 Final(String),
14 SessionId(String),
15 Failed(String),
16 ApiRetry {
17 message: String,
18 retry_in: Option<Duration>,
19 },
20}
21
22#[derive(Debug)]
23pub struct OutputParser {
24 format: OutputFormat,
25 last_text: Option<String>,
26 all_output: String,
27}
28
29impl OutputParser {
30 pub fn new(format: OutputFormat) -> Self {
31 Self {
32 format,
33 last_text: None,
34 all_output: String::new(),
35 }
36 }
37
38 pub fn feed(&mut self, line: &str) -> Vec<RunEvent> {
39 match self.format {
40 OutputFormat::ClaudeStreamJson => claude::parse_line(line),
41 OutputFormat::CopilotJsonl => copilot::parse_line(line, &mut self.last_text),
42 OutputFormat::Text => text::parse_line(line, &mut self.all_output),
43 }
44 }
45
46 pub fn finish(&mut self) -> Vec<RunEvent> {
47 match self.format {
48 OutputFormat::Text => text::finish(&self.all_output),
49 OutputFormat::ClaudeStreamJson | OutputFormat::CopilotJsonl => Vec::new(),
50 }
51 }
52}
53
54pub fn parse_line(format: OutputFormat, line: &str) -> Vec<RunEvent> {
55 OutputParser::new(format).feed(line)
56}
57
58pub(crate) fn truncate(text: &str, max_chars: usize) -> String {
59 match text.char_indices().nth(max_chars) {
60 Some((byte_index, _)) => format!("{}...", &text[..byte_index]),
61 None => text.to_string(),
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn truncate_respects_char_boundaries() {
71 assert_eq!(truncate("abc", 3), "abc");
72 assert_eq!(truncate("abcd", 3), "abc...");
73 assert_eq!(truncate("äöüß", 2), "äö...");
74 }
75}