use std::collections::HashMap;
use std::time::Duration;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use tokio_stream::StreamExt;
use crate::{
SecretString,
provider::{
ChatMessage, ChatMessageResponse, Message, Role, ToolCall, ToolCallFunction, ToolDefinition,
},
};
pub const DEFAULT_REQUEST_TIMEOUT_SECS: u64 = 30;
pub struct OpenAiCompatInner {
client: Client,
base_url: String,
model: Option<String>,
api_key: Option<SecretString>,
timeout_secs: u64,
max_retries: u32,
}
impl OpenAiCompatInner {
pub fn new(base_url: String) -> Self {
Self::with_api_key(base_url, None)
}
pub fn with_api_key(base_url: String, api_key: Option<SecretString>) -> Self {
Self::with_options(base_url, api_key, DEFAULT_REQUEST_TIMEOUT_SECS, 0)
}
pub fn with_options(
base_url: String,
api_key: Option<SecretString>,
timeout_secs: u64,
max_retries: u32,
) -> Self {
let client = Client::builder()
.connect_timeout(Duration::from_secs(15))
.read_timeout(Duration::from_secs(timeout_secs + 60))
.build()
.unwrap_or_else(|_| Client::new());
OpenAiCompatInner {
client,
base_url,
model: None,
api_key,
timeout_secs,
max_retries,
}
}
pub fn timeout_secs(&self) -> u64 {
self.timeout_secs
}
pub fn max_retries(&self) -> u32 {
self.max_retries
}
pub fn set_timeout(&mut self, timeout_secs: u64) {
self.timeout_secs = timeout_secs;
self.client = Client::builder()
.connect_timeout(Duration::from_secs(15))
.read_timeout(Duration::from_secs(timeout_secs + 60))
.build()
.unwrap_or_else(|_| Client::new());
}
pub fn set_retries(&mut self, max_retries: u32) {
self.max_retries = max_retries;
}
pub async fn health_check(&self) -> Result<(), String> {
let url = format!("{}/health", self.base_url.trim_end_matches('/'));
let mut req = self.client.get(&url);
if let Some(key) = &self.api_key {
req = req.bearer_auth(key.expose_secret());
}
match req.send().await {
Ok(resp) if resp.status().is_success() => Ok(()),
Ok(resp) if resp.status() == reqwest::StatusCode::NOT_FOUND => {
match self.fetch_models_raw().await {
Ok(models) if !models.is_empty() => Ok(()),
Ok(_) => Err(
"Server has no /health endpoint and /v1/models returned an empty list"
.to_string(),
),
Err(e) => Err(format!(
"Server has no /health endpoint and /v1/models failed: {e}"
)),
}
}
Ok(resp) => Err(format!(
"Server returned {}: {}",
resp.status().as_u16(),
resp.text().await.unwrap_or_default()
)),
Err(e) => Err(format!("Cannot reach {}: {}", url, e)),
}
}
pub fn select_model(&mut self, name: String) {
self.model = Some(name);
}
pub fn current_model(&self) -> Option<String> {
self.model.clone()
}
pub fn chat_url(&self) -> String {
format!(
"{}/v1/chat/completions",
self.base_url.trim_end_matches('/').trim_end_matches("/v1")
)
}
pub fn models_url(&self) -> String {
format!(
"{}/v1/models",
self.base_url.trim_end_matches('/').trim_end_matches("/v1")
)
}
pub async fn fetch_models_raw(&self) -> Result<Vec<String>, String> {
let url = self.models_url();
let mut req = self.client.get(&url);
if let Some(key) = &self.api_key {
req = req.bearer_auth(key.expose_secret());
}
let resp = match req.send().await {
Ok(resp) => resp,
Err(e) => return Err(format!("cannot reach {url}: {e}")),
};
if !resp.status().is_success() {
return Err(format!("server returned {}", resp.status().as_u16()));
}
match resp.json::<ModelListResponse>().await {
Ok(list) => Ok(list.data.into_iter().map(|m| m.id).collect()),
Err(e) => Err(format!("invalid model list response: {e}")),
}
}
pub async fn fetch_model_list(&self) -> Vec<String> {
match self.fetch_models_raw().await {
Ok(models) => models,
Err(_) => self.model.clone().into_iter().collect(),
}
}
pub async fn chat(
&self,
messages: Vec<Message>,
tools: Vec<ToolDefinition>,
) -> Result<tokio::sync::mpsc::Receiver<ChatMessageResponse>, String> {
let (send, recv) =
tokio::sync::mpsc::channel::<ChatMessageResponse>(super::STREAM_CHANNEL_CAPACITY);
let model = self.model.clone().unwrap_or_default();
let openai_messages = messages.into_iter().map(to_openai_message).collect();
let openai_tools = tools.into_iter().map(to_openai_tool).collect();
let client = self.client.clone();
let chat_url = self.chat_url();
let api_key = self.api_key.clone();
let timeout_secs = self.timeout_secs;
let max_retries = self.max_retries;
let body = ChatRequest {
model,
messages: openai_messages,
stream: true,
stream_options: Some(StreamOptions {
include_usage: true,
}),
tools: openai_tools,
};
tokio::spawn(async move {
let _usage = chat_with_retries(
&client,
&chat_url,
&body,
api_key.as_ref(),
timeout_secs,
max_retries,
&send,
)
.await;
});
Ok(recv)
}
}
async fn chat_with_retries(
client: &reqwest::Client,
url: &str,
body: &ChatRequest,
api_key: Option<&SecretString>,
timeout_secs: u64,
max_retries: u32,
send: &tokio::sync::mpsc::Sender<ChatMessageResponse>,
) -> Option<crate::provider::TokenUsage> {
let max_attempts = max_retries.max(1);
for attempt in 1..=max_attempts {
let result = tokio::time::timeout(
Duration::from_secs(timeout_secs),
stream_chat_completions(client, url, body, api_key, send),
)
.await;
match result {
Ok(AttemptOutcome::Final(usage)) => return usage,
Ok(AttemptOutcome::Retryable(e)) => {
if attempt >= max_attempts {
send_error_chunk(send, &format!("Error after {attempt} attempt(s): {e}")).await;
return None;
}
}
Err(_) => {
if attempt >= max_attempts {
send_error_chunk(
send,
&format!(
"Error: Request timed out after {timeout_secs}s ({attempt} attempt(s))"
),
)
.await;
return None;
}
}
}
let backoff = Duration::from_secs(1 << (attempt - 1));
tokio::time::sleep(backoff).await;
}
None
}
#[derive(Serialize)]
pub struct ChatRequest {
pub model: String,
pub messages: Vec<OpenAIMessage>,
pub stream: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub stream_options: Option<StreamOptions>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub tools: Vec<OpenAITool>,
}
#[derive(Serialize)]
pub struct StreamOptions {
pub include_usage: bool,
}
#[derive(Serialize)]
pub struct OpenAIMessage {
pub role: String,
pub content: serde_json::Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<OpenAIToolCall>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_call_id: Option<String>,
}
#[derive(Serialize)]
pub struct OpenAITool {
#[serde(rename = "type")]
pub tool_type: String,
pub function: OpenAIToolFunction,
}
#[derive(Serialize)]
pub struct OpenAIToolFunction {
pub name: String,
pub description: String,
pub parameters: serde_json::Value,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct OpenAIToolCall {
#[serde(default)]
pub index: usize,
#[serde(default)]
pub id: String,
#[serde(rename = "type", default)]
pub call_type: String,
#[serde(default)]
pub function: OpenAIToolCallFunction,
}
#[derive(Serialize, Deserialize, Clone, Default)]
pub struct OpenAIToolCallFunction {
#[serde(default)]
pub name: String,
#[serde(default)]
pub arguments: String,
}
#[derive(Deserialize)]
pub struct ChunkChoice {
pub delta: Delta,
#[serde(default, rename = "finish_reason")]
pub _finish_reason: Option<String>,
}
#[derive(Deserialize, Default)]
pub struct Delta {
#[serde(default)]
pub content: Option<String>,
#[serde(default)]
pub tool_calls: Option<Vec<OpenAIToolCall>>,
#[serde(default)]
pub reasoning: Option<String>,
#[serde(default)]
pub reasoning_content: Option<String>,
#[serde(default)]
pub reasoning_details: Option<Vec<ReasoningDetail>>,
}
#[derive(Deserialize, Clone, Debug, Default)]
pub struct ReasoningDetail {
#[serde(default)]
pub text: Option<String>,
#[serde(default)]
pub summary: Option<serde_json::Value>,
}
impl ReasoningDetail {
fn display_text(&self) -> String {
let mut out = String::new();
if let Some(text) = &self.text {
out.push_str(text);
}
if let Some(summary) = &self.summary {
match summary {
serde_json::Value::String(s) => out.push_str(s),
serde_json::Value::Array(parts) => {
for part in parts {
if let Some(s) = part.get("text").and_then(|t| t.as_str()) {
out.push_str(s);
}
}
}
_ => {}
}
}
out
}
}
fn delta_reasoning(delta: &Delta) -> Option<String> {
let mut out = String::new();
for text in [&delta.reasoning, &delta.reasoning_content]
.into_iter()
.flatten()
{
out.push_str(text);
}
if out.is_empty()
&& let Some(details) = &delta.reasoning_details
{
for detail in details {
out.push_str(&detail.display_text());
}
}
if out.is_empty() { None } else { Some(out) }
}
#[derive(Deserialize)]
pub struct StreamChunk {
#[serde(default)]
pub choices: Vec<ChunkChoice>,
#[serde(default)]
pub usage: Option<OpenAIUsage>,
}
#[derive(Deserialize, Clone)]
pub struct OpenAIUsage {
#[serde(default)]
pub prompt_tokens: u32,
#[serde(default)]
pub completion_tokens: u32,
#[serde(default)]
pub total_tokens: u32,
}
#[derive(Deserialize)]
pub struct ModelListResponse {
pub data: Vec<ModelEntry>,
}
#[derive(Deserialize)]
pub struct ModelEntry {
pub id: String,
}
pub fn to_openai_message(msg: Message) -> OpenAIMessage {
fn build_content(msg: &Message) -> serde_json::Value {
if msg.images.is_empty() {
serde_json::Value::String(msg.content.clone())
} else {
let mut parts: Vec<serde_json::Value> = Vec::new();
if !msg.content.is_empty() {
parts.push(serde_json::json!({
"type": "text",
"text": msg.content
}));
}
for img in &msg.images {
parts.push(serde_json::json!({
"type": "image_url",
"image_url": {
"url": img.data_uri()
}
}));
}
serde_json::Value::Array(parts)
}
}
match msg.role {
Role::System => OpenAIMessage {
role: "system".to_string(),
content: serde_json::Value::String(msg.content),
tool_calls: None,
tool_call_id: None,
},
Role::User => OpenAIMessage {
role: "user".to_string(),
content: build_content(&msg),
tool_calls: None,
tool_call_id: None,
},
Role::Assistant => {
if msg.tool_calls.is_empty() {
OpenAIMessage {
role: "assistant".to_string(),
content: serde_json::Value::String(msg.content),
tool_calls: None,
tool_call_id: None,
}
} else {
let tool_calls: Vec<OpenAIToolCall> = msg
.tool_calls
.into_iter()
.enumerate()
.map(|(i, tc)| {
let id = tc
.id
.filter(|s| !s.is_empty())
.unwrap_or_else(|| format!("call_{}", i));
let args_str = if tc.function.arguments.is_null() {
"{}".to_string()
} else {
tc.function.arguments.to_string()
};
OpenAIToolCall {
index: i,
id,
call_type: "function".to_string(),
function: OpenAIToolCallFunction {
name: tc.function.name,
arguments: args_str,
},
}
})
.collect();
OpenAIMessage {
role: "assistant".to_string(),
content: if msg.content.is_empty() {
serde_json::Value::Null
} else {
serde_json::Value::String(msg.content)
},
tool_calls: Some(tool_calls),
tool_call_id: None,
}
}
}
Role::Tool => {
let tool_call_id = msg.tool_call_id.filter(|s| !s.is_empty());
OpenAIMessage {
role: "tool".to_string(),
content: serde_json::Value::String(msg.content),
tool_calls: None,
tool_call_id,
}
}
}
}
pub fn to_openai_tool(ti: ToolDefinition) -> OpenAITool {
OpenAITool {
tool_type: "function".to_string(),
function: OpenAIToolFunction {
name: ti.name,
description: ti.description,
parameters: serde_json::to_value(ti.parameters).unwrap_or_default(),
},
}
}
enum AttemptOutcome {
Retryable(String),
Final(Option<crate::provider::TokenUsage>),
}
fn is_retryable_status(status: reqwest::StatusCode) -> bool {
status.is_server_error() || status == reqwest::StatusCode::TOO_MANY_REQUESTS
}
async fn stream_chat_completions(
client: &reqwest::Client,
url: &str,
body: &ChatRequest,
api_key: Option<&SecretString>,
send: &tokio::sync::mpsc::Sender<ChatMessageResponse>,
) -> AttemptOutcome {
let mut request = client.post(url).json(body);
if let Some(key) = api_key {
request = request.bearer_auth(key.expose_secret());
}
let response = match request.send().await {
Ok(r) => r,
Err(e) => return AttemptOutcome::Retryable(format!("Error: {}", e)),
};
let status = response.status();
if !status.is_success() {
let body = response.text().await.unwrap_or_default();
let msg = format!("Error: HTTP {} — {}", status.as_u16(), body);
if is_retryable_status(status) {
return AttemptOutcome::Retryable(msg);
}
send_error_chunk(send, &msg).await;
return AttemptOutcome::Final(None);
}
let mut stream = response.bytes_stream();
let mut buf = String::new();
let mut acc_tool_calls: HashMap<usize, OpenAIToolCall> = HashMap::new();
let mut response_content = String::new();
let mut response_thinking = String::new();
let mut token_usage: Option<crate::provider::TokenUsage> = None;
while let Some(chunk_result) = stream.next().await {
let chunk = match chunk_result {
Ok(c) => c,
Err(e) => {
send_error_chunk(send, &format!("\n\nStream error: {}", e)).await;
return AttemptOutcome::Final(token_usage);
}
};
buf.push_str(&String::from_utf8_lossy(&chunk));
loop {
match buf.find('\n') {
None => break,
Some(pos) => {
let line = buf[..pos].trim().to_string();
buf = buf[pos + 1..].to_string();
if line.is_empty() || line == "data: [DONE]" {
continue;
}
if let Some(data) = line.strip_prefix("data: ")
&& let Ok(chunk) = serde_json::from_str::<StreamChunk>(data)
{
if let Some(usage) = &chunk.usage {
token_usage = Some(crate::provider::TokenUsage {
prompt_tokens: usage.prompt_tokens,
completion_tokens: usage.completion_tokens,
total_tokens: usage.total_tokens,
});
}
for choice in chunk.choices {
if let Some(content) = &choice.delta.content {
response_content.push_str(content);
}
if let Some(reasoning) = delta_reasoning(&choice.delta) {
response_thinking.push_str(&reasoning);
}
if let Some(tool_calls) = &choice.delta.tool_calls {
for tc in tool_calls {
let entry =
acc_tool_calls.entry(tc.index).or_insert(OpenAIToolCall {
index: tc.index,
id: String::new(),
call_type: "function".to_string(),
function: OpenAIToolCallFunction::default(),
});
if !tc.id.is_empty() {
entry.id = tc.id.clone();
}
if !tc.function.name.is_empty() {
entry.function.name = tc.function.name.clone();
}
entry.function.arguments.push_str(&tc.function.arguments);
}
}
}
}
}
}
}
if !response_thinking.is_empty() {
let thinking = std::mem::take(&mut response_thinking);
if send
.send(ChatMessageResponse {
message: ChatMessage {
content: String::new(),
tool_calls: vec![],
thinking: Some(thinking),
},
done: false,
is_error: false,
usage: None,
})
.await
.is_err()
{
return AttemptOutcome::Final(token_usage);
}
}
if !response_content.is_empty() {
if send
.send(ChatMessageResponse {
message: ChatMessage {
content: response_content.clone(),
tool_calls: vec![],
thinking: None,
},
done: false,
is_error: false,
usage: None,
})
.await
.is_err()
{
return AttemptOutcome::Final(token_usage);
}
response_content.clear();
}
}
if !response_thinking.is_empty() {
let thinking = std::mem::take(&mut response_thinking);
if send
.send(ChatMessageResponse {
message: ChatMessage {
content: String::new(),
tool_calls: vec![],
thinking: Some(thinking),
},
done: false,
is_error: false,
usage: None,
})
.await
.is_err()
{
return AttemptOutcome::Final(token_usage);
}
}
let tool_calls: Vec<ToolCall> = if !acc_tool_calls.is_empty() {
acc_tool_calls
.into_values()
.map(|tc| {
let args: serde_json::Value =
serde_json::from_str(&tc.function.arguments).unwrap_or(serde_json::Value::Null);
ToolCall {
id: if tc.id.is_empty() { None } else { Some(tc.id) },
function: ToolCallFunction {
name: tc.function.name,
arguments: args,
thought_signature: None,
},
}
})
.collect()
} else {
vec![]
};
let _ = send
.send(ChatMessageResponse {
message: ChatMessage {
content: String::new(),
tool_calls,
thinking: None,
},
done: true,
is_error: false,
usage: token_usage.clone(),
})
.await;
AttemptOutcome::Final(token_usage)
}
async fn send_error_chunk(send: &tokio::sync::mpsc::Sender<ChatMessageResponse>, message: &str) {
let _ = send
.send(ChatMessageResponse {
message: ChatMessage {
content: message.to_string(),
tool_calls: vec![],
thinking: None,
},
done: true,
is_error: true,
usage: None,
})
.await;
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::{Arc, Mutex};
fn reason_phrase(status: u16) -> &'static str {
match status {
200 => "OK",
404 => "Not Found",
401 => "Unauthorized",
500 => "Internal Server Error",
503 => "Service Unavailable",
_ => "Status",
}
}
async fn spawn_mock_server(
routes: Vec<(&'static str, u16, &'static str)>,
) -> (String, Arc<Mutex<Vec<String>>>, tokio::task::JoinHandle<()>) {
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
.await
.expect("mock server bind");
let addr = listener.local_addr().expect("mock server addr");
let log: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![]));
let routes = Arc::new(routes);
let log_in = Arc::clone(&log);
let handle = tokio::spawn(async move {
while let Ok((mut stream, _)) = listener.accept().await {
let routes = Arc::clone(&routes);
let log = Arc::clone(&log_in);
tokio::spawn(async move {
let mut buf = [0u8; 4096];
let n = match stream.read(&mut buf).await {
Ok(0) | Err(_) => return,
Ok(n) => n,
};
let head = String::from_utf8_lossy(&buf[..n]);
let path = head
.split_whitespace()
.nth(1)
.unwrap_or_default()
.to_string();
log.lock().unwrap().push(path.clone());
let (status, body) = routes
.iter()
.find(|(p, _, _)| *p == path)
.map(|(_, s, b)| (*s, *b))
.unwrap_or((404, "{\"error\":\"not found\"}"));
let resp = format!(
"HTTP/1.1 {status} {reason}\r\ncontent-type: application/json\r\n\
content-length: {len}\r\nconnection: close\r\n\r\n{body}",
reason = reason_phrase(status),
len = body.len(),
);
let _ = stream.write_all(resp.as_bytes()).await;
let _ = stream.shutdown().await;
});
}
});
(format!("http://{addr}"), log, handle)
}
#[test]
fn retryable_status_codes() {
assert!(is_retryable_status(reqwest::StatusCode::BAD_GATEWAY));
assert!(is_retryable_status(
reqwest::StatusCode::SERVICE_UNAVAILABLE
));
assert!(is_retryable_status(
reqwest::StatusCode::INTERNAL_SERVER_ERROR
));
assert!(is_retryable_status(reqwest::StatusCode::TOO_MANY_REQUESTS));
}
#[test]
fn non_retryable_status_codes() {
assert!(!is_retryable_status(reqwest::StatusCode::UNAUTHORIZED));
assert!(!is_retryable_status(reqwest::StatusCode::NOT_FOUND));
assert!(!is_retryable_status(reqwest::StatusCode::BAD_REQUEST));
assert!(!is_retryable_status(
reqwest::StatusCode::UNPROCESSABLE_ENTITY
));
}
#[test]
fn defaults_timeout_and_retries() {
let inner = OpenAiCompatInner::new("http://localhost:8080".to_string());
assert_eq!(inner.timeout_secs(), 30);
assert_eq!(inner.max_retries(), 0);
}
#[test]
fn with_options_stores_configuration() {
let inner =
OpenAiCompatInner::with_options("http://localhost:8080".to_string(), None, 120, 5);
assert_eq!(inner.timeout_secs(), 120);
assert_eq!(inner.max_retries(), 5);
}
#[test]
fn set_timeout_updates_value() {
let mut inner = OpenAiCompatInner::new("http://localhost:8080".to_string());
inner.set_timeout(60);
assert_eq!(inner.timeout_secs(), 60);
}
#[test]
fn set_retries_updates_value() {
let mut inner = OpenAiCompatInner::new("http://localhost:8080".to_string());
inner.set_retries(7);
assert_eq!(inner.max_retries(), 7);
}
#[test]
fn models_url_normalises_trailing_v1() {
let plain = OpenAiCompatInner::new("http://example.test:1234".to_string());
assert_eq!(plain.models_url(), "http://example.test:1234/v1/models");
let with_v1 = OpenAiCompatInner::new("http://example.test:1234/v1".to_string());
assert_eq!(with_v1.models_url(), "http://example.test:1234/v1/models");
}
#[tokio::test]
async fn health_check_uses_health_endpoint_when_present() {
let (base, log, _server) =
spawn_mock_server(vec![("/health", 200, "{\"status\":\"ok\"}")]).await;
let inner = OpenAiCompatInner::new(base);
assert!(inner.health_check().await.is_ok());
assert_eq!(log.lock().unwrap().as_slice(), ["/health".to_string()]);
}
#[tokio::test]
async fn health_check_falls_back_to_models_endpoint() {
let (base, log, _server) = spawn_mock_server(vec![(
"/v1/models",
200,
"{\"data\":[{\"id\":\"gpt-4o-mini\"}]}",
)])
.await;
let inner = OpenAiCompatInner::new(base);
assert!(inner.health_check().await.is_ok());
assert_eq!(
log.lock().unwrap().as_slice(),
["/health".to_string(), "/v1/models".to_string()]
);
}
#[tokio::test]
async fn health_check_fallback_rejects_empty_model_list() {
let (base, _log, _server) =
spawn_mock_server(vec![("/v1/models", 200, "{\"data\":[]}")]).await;
let inner = OpenAiCompatInner::new(base);
let err = inner.health_check().await.expect_err("should fail");
assert!(err.contains("/health"), "unexpected error: {err}");
assert!(err.contains("empty list"), "unexpected error: {err}");
}
#[tokio::test]
async fn health_check_fallback_reports_models_failure() {
let (base, _log, _server) =
spawn_mock_server(vec![("/v1/models", 500, "{\"error\":\"boom\"}")]).await;
let inner = OpenAiCompatInner::new(base);
let err = inner.health_check().await.expect_err("should fail");
assert!(err.contains("/health"), "unexpected error: {err}");
assert!(err.contains("500"), "unexpected error: {err}");
}
#[tokio::test]
async fn health_check_does_not_mask_non_404_errors() {
let (base, log, _server) =
spawn_mock_server(vec![("/health", 503, "{\"status\":\"unhealthy\"}")]).await;
let inner = OpenAiCompatInner::new(base);
let err = inner.health_check().await.expect_err("should fail");
assert!(err.contains("503"), "unexpected error: {err}");
assert_eq!(log.lock().unwrap().as_slice(), ["/health".to_string()]);
}
#[tokio::test]
async fn fetch_models_raw_propagates_errors() {
let (base, _log, _server) = spawn_mock_server(vec![]).await;
let inner = OpenAiCompatInner::new(base);
assert!(inner.fetch_models_raw().await.is_err());
let (base, _log, _server) = spawn_mock_server(vec![(
"/v1/models",
200,
"{\"data\":[{\"id\":\"alpha\"},{\"id\":\"beta\"}]}",
)])
.await;
let inner = OpenAiCompatInner::new(base);
assert_eq!(
inner.fetch_models_raw().await.expect("ok"),
vec!["alpha".to_string(), "beta".to_string()]
);
}
#[tokio::test]
async fn fetch_model_list_falls_back_to_selected_model() {
let (base, _log, _server) = spawn_mock_server(vec![]).await;
let mut inner = OpenAiCompatInner::new(base);
inner.select_model("chosen-model".to_string());
assert_eq!(
inner.fetch_model_list().await,
vec!["chosen-model".to_string()]
);
}
fn parse_chunk(json: &str) -> StreamChunk {
serde_json::from_str(json).unwrap_or_else(|e| panic!("invalid chunk {json}: {e}"))
}
fn reasoning_of(json: &str) -> Option<String> {
let chunk = parse_chunk(json);
chunk
.choices
.first()
.and_then(|c| delta_reasoning(&c.delta))
}
#[test]
fn delta_reasoning_reads_openrouter_field() {
assert_eq!(
reasoning_of(r#"{"choices":[{"delta":{"reasoning":"hmm"}}]}"#).as_deref(),
Some("hmm")
);
}
#[test]
fn delta_reasoning_reads_vllm_field() {
assert_eq!(
reasoning_of(r#"{"choices":[{"delta":{"reasoning_content":"hmm"}}]}"#).as_deref(),
Some("hmm")
);
}
#[test]
fn delta_reasoning_reads_structured_details() {
assert_eq!(
reasoning_of(
r#"{"choices":[{"delta":{"reasoning_details":[{"type":"text","text":"a"},{"type":"text","text":"b"}]}}]}"#
)
.as_deref(),
Some("ab")
);
}
#[test]
fn delta_reasoning_reads_summary_parts() {
assert_eq!(
reasoning_of(
r#"{"choices":[{"delta":{"reasoning_details":[{"type":"summary_text","summary":[{"text":"x"}]}]}}]}"#
)
.as_deref(),
Some("x")
);
}
#[test]
fn delta_reasoning_ignores_opaque_details() {
assert_eq!(
reasoning_of(
r#"{"choices":[{"delta":{"reasoning_details":[{"type":"reasoning.encrypted","data":"gAAAAA"}]}}]}"#
),
None
);
}
#[test]
fn delta_reasoning_does_not_duplicate_plain_and_structured() {
assert_eq!(
reasoning_of(
r#"{"choices":[{"delta":{"reasoning":"same","reasoning_details":[{"type":"text","text":"same"}]}}]}"#
)
.as_deref(),
Some("same")
);
}
#[test]
fn delta_reasoning_is_none_without_reasoning_fields() {
assert_eq!(
reasoning_of(r#"{"choices":[{"delta":{"content":"hello"}}]}"#),
None
);
}
const REASONING_SSE: &str = concat!(
"data: {\"choices\":[{\"delta\":{\"reasoning\":\"think \"}}]}\n\n",
"data: {\"choices\":[{\"delta\":{\"reasoning\":\"more\"}}]}\n\n",
"data: {\"choices\":[{\"delta\":{\"content\":\"Hel\"}}]}\n\n",
"data: {\"choices\":[{\"delta\":{\"content\":\"lo\"}}]}\n\n",
"data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":2,\"total_tokens\":3}}\n\n",
"data: [DONE]\n\n",
);
#[tokio::test]
async fn stream_forwards_reasoning_as_thinking_chunks() {
let (base, _log, _server) =
spawn_mock_server(vec![("/v1/chat/completions", 200, REASONING_SSE)]).await;
let mut inner = OpenAiCompatInner::new(base);
inner.select_model("test-model".to_string());
let mut recv = inner
.chat(vec![Message::simple(Role::User, "hi")], vec![])
.await
.expect("chat started");
let mut thinking = String::new();
let mut content = String::new();
let mut done = false;
while let Some(resp) = recv.recv().await {
if let Some(t) = &resp.message.thinking {
thinking.push_str(t);
}
content.push_str(&resp.message.content);
if resp.done {
done = true;
break;
}
}
assert!(done, "stream should terminate with a done chunk");
assert_eq!(thinking, "think more");
assert_eq!(content, "Hello");
}
#[tokio::test]
async fn stream_without_reasoning_leaves_thinking_unset() {
let (base, _log, _server) = spawn_mock_server(vec![(
"/v1/chat/completions",
200,
"data: {\"choices\":[{\"delta\":{\"content\":\"plain\"}}]}\n\ndata: [DONE]\n\n",
)])
.await;
let mut inner = OpenAiCompatInner::new(base);
inner.select_model("test-model".to_string());
let mut recv = inner
.chat(vec![Message::simple(Role::User, "hi")], vec![])
.await
.expect("chat started");
while let Some(resp) = recv.recv().await {
assert!(resp.message.thinking.is_none());
if resp.done {
break;
}
}
}
}