BlueGit

ict rusty-teams

Files

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

mod.rs

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