refactor: use shared streaming helpers in openai and embedded providers

Agent: carmack

openai.rs:
- Use make_text_chunk() for streaming text content
- Use make_final_chunk() for final completion chunk
- Simplify tool_calls conversion logic

embedded.rs:
- Use make_text_chunk() for all 4 streaming text chunks
- Use make_final_chunk() for final completion chunk
- Remove unused CompletionChunk import

Net reduction: 35 lines removed
All tests pass. Behavior unchanged.
This commit is contained in:
Dhanji R. Prasanna
2026-01-07 13:01:03 +11:00
parent 2bf475960c
commit 3776ed847e
2 changed files with 15 additions and 50 deletions

View File

@@ -12,6 +12,7 @@ use tracing::{debug, error};
use crate::{
CompletionChunk, CompletionRequest, CompletionResponse, CompletionStream, LLMProvider, Message,
MessageRole, Tool, ToolCall, Usage,
streaming::{make_text_chunk, make_final_chunk},
};
#[derive(Clone)]
@@ -171,12 +172,7 @@ impl OpenAIProvider {
if let Some(content) = &choice.delta.content {
accumulated_content.push_str(content);
let chunk = CompletionChunk {
content: content.clone(),
finished: false,
tool_calls: None,
usage: None,
};
let chunk = make_text_chunk(content.clone());
if tx.send(Ok(chunk)).await.is_err() {
debug!("Receiver dropped, stopping stream");
return accumulated_usage;
@@ -242,22 +238,15 @@ impl OpenAIProvider {
// Send final chunk if we haven't already
let tool_calls = if current_tool_calls.is_empty() {
None
vec![]
} else {
Some(
current_tool_calls
.iter()
.filter_map(|tc| tc.to_tool_call())
.collect(),
)
current_tool_calls
.iter()
.filter_map(|tc| tc.to_tool_call())
.collect()
};
let final_chunk = CompletionChunk {
content: String::new(),
finished: true,
tool_calls,
usage: accumulated_usage.clone(),
};
let final_chunk = make_final_chunk(tool_calls, accumulated_usage.clone());
let _ = tx.send(Ok(final_chunk)).await;
accumulated_usage