Skip to main content

dekopon_model/
stream.rs

1use std::ops::ControlFlow;
2
3use crate::error::InferenceError;
4
5#[derive(Clone, Debug, Default, Eq, PartialEq)]
6pub struct ModelText(String);
7
8impl ModelText {
9    pub(crate) fn from_model(text: String) -> Self {
10        Self(text)
11    }
12
13    #[must_use]
14    pub fn as_str(&self) -> &str {
15        &self.0
16    }
17
18    #[must_use]
19    pub fn len(&self) -> usize {
20        self.0.len()
21    }
22
23    #[must_use]
24    pub fn is_empty(&self) -> bool {
25        self.0.is_empty()
26    }
27
28    pub fn push(&mut self, delta: &Self) {
29        self.0.push_str(&delta.0);
30    }
31
32    #[must_use]
33    pub fn truncated(&self, max_chars: usize) -> Self {
34        match self.0.char_indices().nth(max_chars) {
35            Some((end, _)) => Self(self.0[..end].to_owned()),
36            None => self.clone(),
37        }
38    }
39}
40
41#[derive(Clone, Debug)]
42pub enum TurnEvent {
43    TextDelta(ModelText),
44}
45
46#[derive(Clone, Copy, Debug)]
47pub enum TranscriptDialect {
48    ChatCompletions,
49    Responses,
50}
51
52pub fn events_from_transcript(
53    body: &str,
54    dialect: TranscriptDialect,
55) -> Result<Vec<TurnEvent>, InferenceError> {
56    let mut events = Vec::new();
57    let mut collect = |event: TurnEvent| -> ControlFlow<()> {
58        events.push(event);
59        ControlFlow::Continue(())
60    };
61    match dialect {
62        TranscriptDialect::Responses => {
63            crate::codex::replay_transcript(body, &mut collect)?;
64        }
65        TranscriptDialect::ChatCompletions => {
66            crate::openai::replay_transcript(
67                crate::diagnostic::DiagnosticSecrets::default(),
68                body,
69                &mut collect,
70            )?;
71        }
72    }
73    Ok(events)
74}
75
76#[cfg(test)]
77mod tests {
78    use super::{ModelText, TranscriptDialect, TurnEvent, events_from_transcript};
79
80    #[test]
81    fn cumulative_text_is_the_concatenation_of_its_deltas() {
82        let mut cumulative = ModelText::default();
83        assert!(cumulative.is_empty());
84
85        for delta in ["Look ", "at ", "that."] {
86            cumulative.push(&ModelText::from_model(delta.to_owned()));
87        }
88
89        assert_eq!(cumulative.as_str(), "Look at that.");
90        assert_eq!(cumulative.len(), 13);
91        assert!(!cumulative.is_empty());
92    }
93
94    fn recorded(events: &[TurnEvent]) -> Vec<String> {
95        events
96            .iter()
97            .map(|event| match event {
98                TurnEvent::TextDelta(text) => format!("text:{}", text.as_str()),
99            })
100            .collect()
101    }
102
103    #[test]
104    fn a_recorded_transcript_replays_through_whichever_parser_wrote_it() {
105        let chat = concat!(
106            "data: {\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":null}}]}\n\n",
107            "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Echoed \"}}]}\n\n",
108            "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hello.\"},\"finish_reason\":\"stop\"}]}\n\n",
109            "data: [DONE]\n\n"
110        );
111        let responses = concat!(
112            "data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"reasoning\",\"id\":\"rs_1\"}}\n\n",
113            "data: {\"type\":\"response.output_text.delta\",\"delta\":\"Echoed \"}\n\n",
114            "data: {\"type\":\"response.output_text.delta\",\"delta\":\"hello.\"}\n\n",
115            "data: {\"type\":\"response.completed\",\"response\":{\"status\":\"completed\"}}\n\n"
116        );
117
118        for (backend, transcript, dialect) in [
119            ("chat completions", chat, TranscriptDialect::ChatCompletions),
120            ("responses", responses, TranscriptDialect::Responses),
121        ] {
122            let events = events_from_transcript(transcript, dialect)
123                .unwrap_or_else(|error| panic!("{backend}: {error}"));
124
125            assert_eq!(
126                recorded(&events),
127                vec!["text:Echoed ", "text:hello."],
128                "{backend}"
129            );
130        }
131    }
132
133    #[test]
134    fn responses_replay_accepts_a_multiline_first_data_field() {
135        let body = concat!(
136            "data: {\"type\":\"response.output_text.delta\",\n",
137            "data: \"delta\":\"hello\"}\n\n",
138            "data: {\"type\":\"response.completed\"}\n\n"
139        );
140        let events = events_from_transcript(body, TranscriptDialect::Responses).unwrap();
141        assert_eq!(recorded(&events), ["text:hello"]);
142    }
143
144    #[test]
145    fn a_transcript_that_is_not_a_stream_says_so_rather_than_replaying_nothing() {
146        let error = events_from_transcript(
147            "data: {\"choices\": [\n\n",
148            TranscriptDialect::ChatCompletions,
149        )
150        .expect_err("a truncated JSON payload is not a stream");
151
152        assert!(matches!(
153            error,
154            crate::error::InferenceError::Protocol(crate::error::ProtocolFailure::Decode(source))
155                if source.is_eof()
156        ));
157    }
158
159    #[test]
160    fn truncation_cuts_on_a_character_boundary_rather_than_a_byte_one() {
161        let text = ModelText::from_model("aπ😀b".to_owned());
162        assert_eq!(text.len(), 8, "the byte length is not the character count");
163
164        assert_eq!(text.truncated(0).as_str(), "");
165        assert_eq!(text.truncated(1).as_str(), "a");
166        assert_eq!(text.truncated(2).as_str(), "aπ");
167        assert_eq!(
168            text.truncated(2).len(),
169            3,
170            "two characters are three bytes here, so a byte bound would have cut elsewhere"
171        );
172        assert_eq!(text.truncated(3).as_str(), "aπ😀");
173        assert_eq!(text.truncated(4).as_str(), "aπ😀b");
174        assert_eq!(
175            text.truncated(4_000).as_str(),
176            "aπ😀b",
177            "a bound larger than the text keeps all of it"
178        );
179    }
180}