ralph_workflow/json_parser/codex/
parser.rs1pub struct CodexParser {
3 colors: Colors,
4 verbosity: Verbosity,
5 log_path: Option<PathBuf>,
7 display_name: String,
8 streaming_session: Rc<RefCell<StreamingSession>>,
10 reasoning_accumulator: Rc<RefCell<super::types::DeltaAccumulator>>,
13 turn_counter: Rc<RefCell<u64>>,
15 terminal_mode: RefCell<TerminalMode>,
17 show_streaming_metrics: bool,
19 printer: SharedPrinter,
21 last_rendered_content: RefCell<std::collections::HashMap<String, String>>,
28}
29
30impl CodexParser {
31 pub(crate) fn new(colors: Colors, verbosity: Verbosity) -> Self {
32 Self::with_printer(colors, verbosity, super::printer::shared_stdout())
33 }
34
35 pub(crate) fn with_printer(
37 colors: Colors,
38 verbosity: Verbosity,
39 printer: SharedPrinter,
40 ) -> Self {
41 let verbose_warnings = matches!(verbosity, Verbosity::Debug);
42 let streaming_session = StreamingSession::new().with_verbose_warnings(verbose_warnings);
43
44 let _printer_is_terminal = printer.borrow().is_terminal();
46
47 Self {
48 colors,
49 verbosity,
50 log_path: None,
51 display_name: "Codex".to_string(),
52 streaming_session: Rc::new(RefCell::new(streaming_session)),
53 reasoning_accumulator: Rc::new(RefCell::new(super::types::DeltaAccumulator::new())),
54 turn_counter: Rc::new(RefCell::new(0)),
55 terminal_mode: RefCell::new(TerminalMode::detect()),
56 show_streaming_metrics: false,
57 printer,
58 last_rendered_content: RefCell::new(std::collections::HashMap::new()),
59 }
60 }
61
62 pub(crate) const fn with_show_streaming_metrics(mut self, show: bool) -> Self {
63 self.show_streaming_metrics = show;
64 self
65 }
66
67 pub(crate) fn with_display_name(mut self, display_name: &str) -> Self {
68 self.display_name = display_name.to_string();
69 self
70 }
71
72 pub(crate) fn with_log_file(mut self, path: &str) -> Self {
76 self.log_path = Some(PathBuf::from(path));
77 self
78 }
79
80 #[cfg(any(test, feature = "test-utils"))]
81 pub fn with_terminal_mode(self, mode: TerminalMode) -> Self {
82 *self.terminal_mode.borrow_mut() = mode;
83 self
84 }
85
86 #[cfg(any(test, feature = "test-utils"))]
95 pub fn with_printer_for_test(
96 colors: Colors,
97 verbosity: Verbosity,
98 printer: SharedPrinter,
99 ) -> Self {
100 Self::with_printer(colors, verbosity, printer)
101 }
102
103 #[cfg(any(test, feature = "test-utils"))]
108 pub fn with_log_file_for_test(mut self, path: &str) -> Self {
109 self.log_path = Some(PathBuf::from(path));
110 self
111 }
112
113 #[cfg(any(test, feature = "test-utils"))]
118 pub fn with_display_name_for_test(mut self, display_name: &str) -> Self {
119 self.display_name = display_name.to_string();
120 self
121 }
122
123 #[cfg(any(test, feature = "test-utils"))]
128 pub fn parse_stream_for_test<R: std::io::BufRead>(
129 &self,
130 reader: R,
131 workspace: &dyn Workspace,
132 ) -> std::io::Result<()> {
133 self.parse_stream(reader, workspace)
134 }
135
136 #[cfg(any(test, feature = "test-utils"))]
142 pub fn printer(&self) -> SharedPrinter {
143 Rc::clone(&self.printer)
144 }
145
146 #[cfg(any(test, feature = "test-utils"))]
151 pub fn streaming_metrics(&self) -> StreamingQualityMetrics {
152 self.streaming_session
153 .borrow()
154 .get_streaming_quality_metrics()
155 }
156
157 #[inline]
159 fn optional_output(output: String) -> Option<String> {
160 if output.is_empty() {
161 None
162 } else {
163 Some(output)
164 }
165 }
166}