use crate::error::Result;
use crate::llm::gateway::StreamChunk;
use crate::llm::models::LlmToolCall;
use crate::llm::stream_events::LineBuffer;
use futures::stream::{Stream, StreamExt};
use serde_json::Value;
use std::collections::HashMap;
use tracing::warn;
pub(crate) enum LegacyLine {
Done,
Frame(Value),
}
pub(crate) fn read_legacy_line(line: &str) -> Option<LegacyLine> {
let data = line.trim().strip_prefix("data: ")?;
if data == "[DONE]" {
return Some(LegacyLine::Done);
}
match serde_json::from_str::<Value>(data) {
Ok(frame) => Some(LegacyLine::Frame(frame)),
Err(e) => {
warn!("Failed to parse streaming chunk: {}", e);
None
}
}
}
#[derive(Default)]
pub(crate) struct OpenAiLegacyParser {
tool_calls: HashMap<usize, ToolCallAccumulator>,
}
impl OpenAiLegacyParser {
pub(crate) fn parse_line(&mut self, line: &str) -> Vec<StreamChunk> {
match read_legacy_line(line) {
Some(LegacyLine::Done) => self.done(),
Some(LegacyLine::Frame(frame)) => self.frame(&frame),
None => Vec::new(),
}
}
pub(crate) fn done(&self) -> Vec<StreamChunk> {
self.complete_tool_calls().into_iter().collect()
}
pub(crate) fn frame(&mut self, frame: &Value) -> Vec<StreamChunk> {
let Some(choice) = frame["choices"].as_array().and_then(|choices| choices.first()) else {
return Vec::new();
};
let delta = &choice["delta"];
let mut chunks = Vec::new();
if let Some(content) = delta["content"].as_str() {
if !content.is_empty() {
chunks.push(StreamChunk::Content(content.to_string()));
}
}
if let Some(tool_calls) = delta["tool_calls"].as_array() {
for tc in tool_calls {
self.accumulate(tc);
}
}
if choice["finish_reason"].as_str() == Some("tool_calls") && !self.tool_calls.is_empty() {
chunks.extend(self.complete_tool_calls());
self.tool_calls.clear();
}
chunks
}
fn accumulate(&mut self, tc: &Value) {
let Some(index) = tc["index"].as_u64() else {
return;
};
let acc = self.tool_calls.entry(index as usize).or_default();
if let Some(id) = tc["id"].as_str() {
acc.id = Some(id.to_string());
}
if let Some(name) = tc["function"]["name"].as_str() {
acc.name = Some(name.to_string());
}
if let Some(args) = tc["function"]["arguments"].as_str() {
acc.arguments.push_str(args);
}
}
fn complete_tool_calls(&self) -> Option<StreamChunk> {
let calls = build_complete_tool_calls(&self.tool_calls);
(!calls.is_empty()).then_some(StreamChunk::ToolCalls(calls))
}
}
pub(crate) fn legacy_body_stream<'a, S, B, F>(
bytes: S,
mut parse_line: F,
) -> impl Stream<Item = Result<StreamChunk>> + Send + 'a
where
S: Stream<Item = std::result::Result<B, reqwest::Error>> + Send + 'a,
B: AsRef<[u8]> + Send + 'a,
F: FnMut(&str) -> Vec<StreamChunk> + Send + 'a,
{
async_stream::stream! {
let mut bytes = Box::pin(bytes);
let mut lines = LineBuffer::default();
while let Some(chunk_result) = bytes.next().await {
match chunk_result {
Ok(chunk) => {
for line in lines.push(chunk.as_ref()) {
match std::str::from_utf8(&line) {
Ok(text) => {
for chunk in parse_line(text) {
yield Ok(chunk);
}
}
Err(e) => warn!("Skipping a streaming line that is not UTF-8: {}", e),
}
}
}
Err(e) => {
yield Err(e.into());
return;
}
}
}
}
}
#[derive(Default)]
struct ToolCallAccumulator {
id: Option<String>,
name: Option<String>,
arguments: String,
}
fn build_complete_tool_calls(
accumulators: &HashMap<usize, ToolCallAccumulator>,
) -> Vec<LlmToolCall> {
let mut indices: Vec<_> = accumulators.keys().collect();
indices.sort();
indices
.iter()
.filter_map(|&&index| {
let acc = accumulators.get(&index)?;
let name = acc.name.clone()?;
let arguments: HashMap<String, Value> =
serde_json::from_str(&acc.arguments).unwrap_or_default();
Some(LlmToolCall {
id: acc.id.clone(),
name,
arguments,
})
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(lines: &[&str]) -> Vec<StreamChunk> {
let mut parser = OpenAiLegacyParser::default();
lines.iter().flat_map(|line| parser.parse_line(line)).collect()
}
#[test]
fn test_build_complete_tool_calls() {
let mut accumulators = HashMap::new();
accumulators.insert(
0,
ToolCallAccumulator {
id: Some("call_123".to_string()),
name: Some("get_weather".to_string()),
arguments: r#"{"location": "NYC"}"#.to_string(),
},
);
accumulators.insert(
1,
ToolCallAccumulator {
id: Some("call_456".to_string()),
name: Some("search".to_string()),
arguments: r#"{"query": "test"}"#.to_string(),
},
);
let result = build_complete_tool_calls(&accumulators);
assert_eq!(result.len(), 2);
assert_eq!(result[0].id, Some("call_123".to_string()));
assert_eq!(result[0].name, "get_weather");
assert_eq!(result[1].id, Some("call_456".to_string()));
assert_eq!(result[1].name, "search");
}
#[test]
fn test_build_complete_tool_calls_missing_name() {
let mut accumulators = HashMap::new();
accumulators.insert(
0,
ToolCallAccumulator {
id: Some("call_123".to_string()),
name: None,
arguments: r#"{}"#.to_string(),
},
);
let result = build_complete_tool_calls(&accumulators);
assert!(result.is_empty());
}
#[tokio::test]
async fn the_body_stream_decodes_a_character_split_across_chunks() {
use futures::stream::StreamExt;
let line = "data: {\"choices\":[{\"delta\":{\"content\":\"\u{e9}\"}}]}\n".as_bytes();
let split = line.iter().position(|byte| *byte >= 0x80).expect("multi-byte") + 1;
let chunks: Vec<std::result::Result<Vec<u8>, reqwest::Error>> =
vec![Ok(line[..split].to_vec()), Ok(line[split..].to_vec())];
let mut parser = OpenAiLegacyParser::default();
let items: Vec<_> =
legacy_body_stream(futures::stream::iter(chunks), move |l: &str| parser.parse_line(l))
.collect()
.await;
assert!(
matches!(items.as_slice(), [Ok(StreamChunk::Content(text))] if text == "\u{e9}"),
"{items:?}"
);
}
#[test]
fn lines_without_data_carry_nothing() {
assert!(parse(&["", ": keep-alive", "event: message", "data: {not json"]).is_empty());
}
#[test]
fn a_complete_tool_call_in_one_delta_is_yielded_at_the_finish_reason() {
let chunks = parse(&[
r#"data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"f","arguments":"{\"x\": 1}"}}]}}]}"#,
r#"data: {"choices":[{"delta":{},"finish_reason":"tool_calls"}]}"#,
"data: [DONE]",
]);
let [StreamChunk::ToolCalls(calls)] = chunks.as_slice() else {
panic!("expected one tool-calls chunk, got {chunks:?}");
};
assert_eq!(calls[0].name, "f");
assert_eq!(calls[0].arguments["x"], 1);
}
}