use std::sync::Arc;
use serde_json::{json, Value};
use crate::client::{parse_response, InnerClient};
use crate::error::CencoriError;
use crate::types::{
ChatResponse, EmbeddingInput, EmbeddingResponse, EmbeddingUsage, GenerateObjectResponse,
GeneratedImage, ImageGenerationResponse, Message, RagResponse, RagSource, RagStreamChunk,
ResponsesResponse, StreamChunk, ToolCall, Usage,
};
#[derive(Debug, Clone)]
pub struct AIModule {
pub(crate) inner: Arc<InnerClient>,
}
impl AIModule {
pub(crate) fn new(inner: Arc<InnerClient>) -> Self {
Self { inner }
}
fn build_chat_payload(
messages: &[Message],
model: &str,
temperature: Option<f64>,
max_tokens: Option<u32>,
user_id: Option<&str>,
stream: bool,
) -> Value {
let mut payload = json!({
"messages": messages,
"model": model,
"stream": stream,
});
if let Some(t) = temperature {
payload["temperature"] = json!(t);
}
if let Some(mt) = max_tokens {
payload["maxTokens"] = json!(mt);
}
if let Some(uid) = user_id {
payload["userId"] = json!(uid);
}
payload
}
fn parse_chat_response(data: &Value, model: &str) -> ChatResponse {
let choice = data["choices"]
.as_array()
.and_then(|choices| choices.first());
let content = choice
.and_then(|c| c["message"]["content"].as_str())
.unwrap_or(data["content"].as_str().unwrap_or(""))
.to_string();
let raw_tool_calls = data["toolCalls"]
.as_array()
.or_else(|| data["tool_calls"].as_array())
.or_else(|| choice.and_then(|c| c["message"]["tool_calls"].as_array()));
let tool_calls = raw_tool_calls.and_then(|tc| {
serde_json::from_value::<Vec<ToolCall>>(serde_json::Value::Array(tc.clone())).ok()
});
let finish_reason = choice
.and_then(|c| c["finish_reason"].as_str())
.or_else(|| data["finish_reason"].as_str())
.map(String::from);
let usage = Usage {
prompt_tokens: data["usage"]["prompt_tokens"].as_u64().unwrap_or(0),
completion_tokens: data["usage"]["completion_tokens"].as_u64().unwrap_or(0),
total_tokens: data["usage"]["total_tokens"].as_u64().unwrap_or(0),
};
let cost_usd = data["cost_usd"].as_f64().unwrap_or(0.0);
let provider = data["provider"].as_str().unwrap_or("").to_string();
ChatResponse {
id: data["id"].as_str().unwrap_or("").to_string(),
model: data["model"].as_str().unwrap_or(model).to_string(),
content,
tool_calls,
finish_reason,
usage,
cost_usd,
provider,
}
}
pub fn chat(
&self,
messages: Vec<Message>,
model: Option<&str>,
temperature: Option<f64>,
max_tokens: Option<u32>,
user_id: Option<&str>,
) -> Result<ChatResponse, CencoriError> {
let model = model.unwrap_or("gemini-2.5-flash");
let payload =
Self::build_chat_payload(&messages, model, temperature, max_tokens, user_id, false);
let url = self.inner.url("/api/ai/chat");
let response = self
.inner
.blocking_client
.post(&url)
.json(&payload)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
Ok(Self::parse_chat_response(&data, model))
}
pub fn chat_stream(
&self,
messages: Vec<Message>,
model: Option<&str>,
temperature: Option<f64>,
max_tokens: Option<u32>,
user_id: Option<&str>,
) -> Result<ChatStreamIter, CencoriError> {
let payload = Self::build_chat_payload(
&messages,
model.unwrap_or("gemini-2.5-flash"),
temperature,
max_tokens,
user_id,
true,
);
let url = self.inner.url("/api/ai/chat");
let response = self
.inner
.blocking_client
.post(&url)
.json(&payload)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
if status == 401 {
return Err(CencoriError::Authentication);
}
if status == 429 {
return Err(CencoriError::RateLimit);
}
if !status.is_success() {
return Err(CencoriError::api(
status.as_u16(),
format!("Request failed with status {status}"),
));
}
Ok(ChatStreamIter { reader: response })
}
pub fn completions(
&self,
prompt: &str,
model: Option<&str>,
temperature: Option<f64>,
max_tokens: Option<u32>,
) -> Result<ChatResponse, CencoriError> {
self.chat(
vec![Message::user(prompt)],
model,
temperature,
max_tokens,
None,
)
}
pub fn embeddings(
&self,
input: EmbeddingInput,
model: Option<&str>,
) -> Result<EmbeddingResponse, CencoriError> {
let payload = json!({
"input": input,
"model": model.unwrap_or("text-embedding-3-small"),
});
let url = self.inner.url("/api/ai/embeddings");
let response = self
.inner
.blocking_client
.post(&url)
.json(&payload)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
let embeddings: Vec<Vec<f64>> = data["data"]
.as_array()
.map(|arr| {
arr.iter()
.filter_map(|item| {
item["embedding"]
.as_array()
.map(|e| e.iter().filter_map(|v| v.as_f64()).collect())
})
.collect()
})
.unwrap_or_default();
Ok(EmbeddingResponse {
model: data
.get("model")
.and_then(|m| m.as_str())
.unwrap_or(model.unwrap_or("text-embedding-3-small"))
.to_string(),
embeddings,
usage: EmbeddingUsage {
total_tokens: data["usage"]["total_tokens"].as_u64().unwrap_or(0),
},
})
}
pub fn generate_object<T: serde::de::DeserializeOwned>(
&self,
model: &str,
prompt: Option<&str>,
schema: Value,
schema_name: Option<&str>,
schema_description: Option<&str>,
temperature: Option<f64>,
max_tokens: Option<u32>,
) -> Result<GenerateObjectResponse<T>, CencoriError> {
let messages = vec![Message::user(prompt.unwrap_or(""))];
let name = schema_name.unwrap_or("generate_object");
let mut payload = json!({
"model": model,
"messages": messages,
"stream": false,
"tools": [{
"type": "function",
"function": {
"name": name,
"description": schema_description.unwrap_or("Generate a structured object matching the schema"),
"parameters": schema,
}
}],
"toolChoice": {
"type": "function",
"function": { "name": name },
},
});
if let Some(t) = temperature {
payload["temperature"] = json!(t);
}
if let Some(mt) = max_tokens {
payload["maxTokens"] = json!(mt);
}
let url = self.inner.url("/api/ai/chat");
let response = self
.inner
.blocking_client
.post(&url)
.json(&payload)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
let tool_call = data["toolCalls"]
.as_array()
.and_then(|tc| tc.first())
.or_else(|| data["tool_calls"].as_array().and_then(|tc| tc.first()))
.or_else(|| {
data["choices"]
.as_array()
.and_then(|choices| choices.first())
.and_then(|c| c["message"]["tool_calls"].as_array())
.and_then(|tc| tc.first())
});
let arguments = tool_call
.and_then(|tc| tc["function"]["arguments"].as_str())
.ok_or_else(|| {
CencoriError::Json(serde::de::Error::custom(
"Model did not return structured output",
))
})?;
let object: T = serde_json::from_str(arguments).map_err(|e| {
CencoriError::Json(serde::de::Error::custom(format!(
"Failed to parse structured output: {e}"
)))
})?;
Ok(GenerateObjectResponse {
object,
usage: Usage {
prompt_tokens: data["usage"]["prompt_tokens"].as_u64().unwrap_or(0),
completion_tokens: data["usage"]["completion_tokens"].as_u64().unwrap_or(0),
total_tokens: data["usage"]["total_tokens"].as_u64().unwrap_or(0),
},
})
}
pub fn generate_image(
&self,
prompt: &str,
model: Option<&str>,
n: Option<u32>,
size: Option<&str>,
quality: Option<&str>,
style: Option<&str>,
response_format: Option<&str>,
) -> Result<ImageGenerationResponse, CencoriError> {
let mut payload = json!({
"prompt": prompt,
"model": model.unwrap_or("dall-e-3"),
});
if let Some(v) = n {
payload["n"] = json!(v);
}
if let Some(v) = size {
payload["size"] = json!(v);
}
if let Some(v) = quality {
payload["quality"] = json!(v);
}
if let Some(v) = style {
payload["style"] = json!(v);
}
if let Some(v) = response_format {
payload["responseFormat"] = json!(v);
}
let url = self.inner.url("/api/ai/images/generate");
let response = self
.inner
.blocking_client
.post(&url)
.json(&payload)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
let images: Vec<GeneratedImage> = data["images"]
.as_array()
.map(|arr| {
arr.iter()
.map(|img| GeneratedImage {
url: img["url"].as_str().map(String::from),
b64_json: img["b64_json"].as_str().map(String::from),
revised_prompt: img["revisedPrompt"].as_str().map(String::from),
})
.collect()
})
.unwrap_or_default();
Ok(ImageGenerationResponse {
images,
model: data["model"].as_str().unwrap_or("").to_string(),
provider: data["provider"].as_str().unwrap_or("").to_string(),
})
}
pub fn rag(
&self,
model: &str,
messages: Vec<Message>,
namespace: &str,
temperature: Option<f64>,
max_tokens: Option<u32>,
limit: Option<u32>,
threshold: Option<f64>,
include_sources: Option<bool>,
) -> Result<RagResponse, CencoriError> {
let mut payload = json!({
"model": model,
"messages": messages,
"namespace": namespace,
"stream": false,
});
if let Some(t) = temperature {
payload["temperature"] = json!(t);
}
if let Some(mt) = max_tokens {
payload["maxTokens"] = json!(mt);
}
payload["limit"] = json!(limit.unwrap_or(5));
payload["threshold"] = json!(threshold.unwrap_or(0.5));
payload["include_sources"] = json!(include_sources.unwrap_or(true));
let url = self.inner.url("/api/ai/rag");
let response = self
.inner
.blocking_client
.post(&url)
.json(&payload)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
Self::parse_rag_response(&data)
}
fn parse_rag_response(data: &Value) -> Result<RagResponse, CencoriError> {
let sources = data["sources"].as_array().map(|arr| {
arr.iter()
.map(|s| RagSource {
content: s["content"].as_str().unwrap_or_default().to_string(),
metadata: s["metadata"]
.as_object()
.map(|m| m.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
.unwrap_or_default(),
similarity: s["similarity"].as_f64().unwrap_or(0.0),
})
.collect()
});
Ok(RagResponse {
message: Message {
role: "assistant".to_string(),
content: data["message"]["content"]
.as_str()
.unwrap_or_default()
.to_string(),
},
model: data["model"].as_str().unwrap_or_default().to_string(),
provider: data["provider"].as_str().unwrap_or_default().to_string(),
usage: Usage {
prompt_tokens: data["usage"]["prompt_tokens"].as_u64().unwrap_or(0),
completion_tokens: data["usage"]["completion_tokens"].as_u64().unwrap_or(0),
total_tokens: data["usage"]["total_tokens"].as_u64().unwrap_or(0),
},
sources,
latency_ms: data["latency_ms"].as_u64().unwrap_or(0),
})
}
pub fn responses(
&self,
model: &str,
input: Value,
instructions: Option<&str>,
tools: Option<Value>,
tool_choice: Option<Value>,
temperature: Option<f64>,
max_output_tokens: Option<u32>,
top_p: Option<f64>,
store: Option<bool>,
metadata: Option<Value>,
previous_response_id: Option<&str>,
parallel_tool_calls: Option<bool>,
truncation: Option<&str>,
response_format: Option<Value>,
include: Option<Vec<String>>,
user: Option<&str>,
) -> Result<ResponsesResponse, CencoriError> {
let mut payload = json!({
"model": model,
"input": input,
"stream": false,
});
if let Some(v) = instructions {
payload["instructions"] = json!(v);
}
if let Some(v) = tools {
payload["tools"] = v;
}
if let Some(v) = tool_choice {
payload["tool_choice"] = v;
}
if let Some(t) = temperature {
payload["temperature"] = json!(t);
}
if let Some(mt) = max_output_tokens {
payload["max_output_tokens"] = json!(mt);
}
if let Some(tp) = top_p {
payload["top_p"] = json!(tp);
}
if let Some(s) = store {
payload["store"] = json!(s);
}
if let Some(m) = metadata {
payload["metadata"] = m;
}
if let Some(pr) = previous_response_id {
payload["previous_response_id"] = json!(pr);
}
if let Some(pt) = parallel_tool_calls {
payload["parallel_tool_calls"] = json!(pt);
}
if let Some(t) = truncation {
payload["truncation"] = json!(t);
}
if let Some(rf) = response_format {
payload["response_format"] = rf;
}
if let Some(inc) = include {
payload["include"] = json!(inc);
}
if let Some(u) = user {
payload["user"] = json!(u);
}
let url = self.inner.url("/v1/responses");
let response = self
.inner
.blocking_client
.post(&url)
.json(&payload)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub async fn async_chat(
&self,
messages: Vec<Message>,
model: Option<&str>,
temperature: Option<f64>,
max_tokens: Option<u32>,
user_id: Option<&str>,
) -> Result<ChatResponse, CencoriError> {
let model = model.unwrap_or("gemini-2.5-flash");
let payload =
Self::build_chat_payload(&messages, model, temperature, max_tokens, user_id, false);
let url = self.inner.url("/api/ai/chat");
let response = self
.inner
.http_client
.post(&url)
.json(&payload)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().await.map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
Ok(Self::parse_chat_response(&data, model))
}
pub async fn async_completions(
&self,
prompt: &str,
model: Option<&str>,
temperature: Option<f64>,
max_tokens: Option<u32>,
) -> Result<ChatResponse, CencoriError> {
self.async_chat(
vec![Message::user(prompt)],
model,
temperature,
max_tokens,
None,
)
.await
}
pub async fn async_embeddings(
&self,
input: EmbeddingInput,
model: Option<&str>,
) -> Result<EmbeddingResponse, CencoriError> {
let payload = json!({
"input": input,
"model": model.unwrap_or("text-embedding-3-small"),
});
let url = self.inner.url("/api/ai/embeddings");
let response = self
.inner
.http_client
.post(&url)
.json(&payload)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().await.map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
let embeddings: Vec<Vec<f64>> = data["data"]
.as_array()
.map(|arr| {
arr.iter()
.filter_map(|item| {
item["embedding"]
.as_array()
.map(|e| e.iter().filter_map(|v| v.as_f64()).collect())
})
.collect()
})
.unwrap_or_default();
Ok(EmbeddingResponse {
model: data
.get("model")
.and_then(|m| m.as_str())
.unwrap_or(model.unwrap_or("text-embedding-3-small"))
.to_string(),
embeddings,
usage: EmbeddingUsage {
total_tokens: data["usage"]["total_tokens"].as_u64().unwrap_or(0),
},
})
}
pub async fn async_generate_object<T: serde::de::DeserializeOwned>(
&self,
model: &str,
prompt: Option<&str>,
schema: Value,
schema_name: Option<&str>,
schema_description: Option<&str>,
temperature: Option<f64>,
max_tokens: Option<u32>,
) -> Result<GenerateObjectResponse<T>, CencoriError> {
let messages = vec![Message::user(prompt.unwrap_or(""))];
let name = schema_name.unwrap_or("generate_object");
let mut payload = json!({
"model": model,
"messages": messages,
"stream": false,
"tools": [{
"type": "function",
"function": {
"name": name,
"description": schema_description.unwrap_or("Generate a structured object matching the schema"),
"parameters": schema,
}
}],
"toolChoice": {
"type": "function",
"function": { "name": name },
},
});
if let Some(t) = temperature {
payload["temperature"] = json!(t);
}
if let Some(mt) = max_tokens {
payload["maxTokens"] = json!(mt);
}
let url = self.inner.url("/api/ai/chat");
let response = self
.inner
.http_client
.post(&url)
.json(&payload)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().await.map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
let tool_call = data["toolCalls"]
.as_array()
.and_then(|tc| tc.first())
.or_else(|| data["tool_calls"].as_array().and_then(|tc| tc.first()))
.or_else(|| {
data["choices"]
.as_array()
.and_then(|choices| choices.first())
.and_then(|c| c["message"]["tool_calls"].as_array())
.and_then(|tc| tc.first())
});
let arguments = tool_call
.and_then(|tc| tc["function"]["arguments"].as_str())
.ok_or_else(|| {
CencoriError::Json(serde::de::Error::custom(
"Model did not return structured output",
))
})?;
let object: T = serde_json::from_str(arguments).map_err(|e| {
CencoriError::Json(serde::de::Error::custom(format!(
"Failed to parse structured output: {e}"
)))
})?;
Ok(GenerateObjectResponse {
object,
usage: Usage {
prompt_tokens: data["usage"]["prompt_tokens"].as_u64().unwrap_or(0),
completion_tokens: data["usage"]["completion_tokens"].as_u64().unwrap_or(0),
total_tokens: data["usage"]["total_tokens"].as_u64().unwrap_or(0),
},
})
}
pub async fn async_generate_image(
&self,
prompt: &str,
model: Option<&str>,
n: Option<u32>,
size: Option<&str>,
quality: Option<&str>,
style: Option<&str>,
response_format: Option<&str>,
) -> Result<ImageGenerationResponse, CencoriError> {
let mut payload = json!({
"prompt": prompt,
"model": model.unwrap_or("dall-e-3"),
});
if let Some(v) = n {
payload["n"] = json!(v);
}
if let Some(v) = size {
payload["size"] = json!(v);
}
if let Some(v) = quality {
payload["quality"] = json!(v);
}
if let Some(v) = style {
payload["style"] = json!(v);
}
if let Some(v) = response_format {
payload["responseFormat"] = json!(v);
}
let url = self.inner.url("/api/ai/images/generate");
let response = self
.inner
.http_client
.post(&url)
.json(&payload)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().await.map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
let images: Vec<GeneratedImage> = data["images"]
.as_array()
.map(|arr| {
arr.iter()
.map(|img| GeneratedImage {
url: img["url"].as_str().map(String::from),
b64_json: img["b64_json"].as_str().map(String::from),
revised_prompt: img["revisedPrompt"].as_str().map(String::from),
})
.collect()
})
.unwrap_or_default();
Ok(ImageGenerationResponse {
images,
model: data["model"].as_str().unwrap_or("").to_string(),
provider: data["provider"].as_str().unwrap_or("").to_string(),
})
}
pub async fn async_rag(
&self,
model: &str,
messages: Vec<Message>,
namespace: &str,
temperature: Option<f64>,
max_tokens: Option<u32>,
limit: Option<u32>,
threshold: Option<f64>,
include_sources: Option<bool>,
) -> Result<RagResponse, CencoriError> {
let mut payload = json!({
"model": model,
"messages": messages,
"namespace": namespace,
"stream": false,
});
if let Some(t) = temperature {
payload["temperature"] = json!(t);
}
if let Some(mt) = max_tokens {
payload["maxTokens"] = json!(mt);
}
payload["limit"] = json!(limit.unwrap_or(5));
payload["threshold"] = json!(threshold.unwrap_or(0.5));
payload["include_sources"] = json!(include_sources.unwrap_or(true));
let url = self.inner.url("/api/ai/rag");
let response = self
.inner
.http_client
.post(&url)
.json(&payload)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().await.map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
Self::parse_rag_response(&data)
}
pub async fn async_responses(
&self,
model: &str,
input: Value,
instructions: Option<&str>,
tools: Option<Value>,
tool_choice: Option<Value>,
temperature: Option<f64>,
max_output_tokens: Option<u32>,
top_p: Option<f64>,
store: Option<bool>,
metadata: Option<Value>,
previous_response_id: Option<&str>,
parallel_tool_calls: Option<bool>,
truncation: Option<&str>,
response_format: Option<Value>,
include: Option<Vec<String>>,
user: Option<&str>,
) -> Result<ResponsesResponse, CencoriError> {
let mut payload = json!({
"model": model,
"input": input,
"stream": false,
});
if let Some(v) = instructions {
payload["instructions"] = json!(v);
}
if let Some(v) = tools {
payload["tools"] = v;
}
if let Some(v) = tool_choice {
payload["tool_choice"] = v;
}
if let Some(t) = temperature {
payload["temperature"] = json!(t);
}
if let Some(mt) = max_output_tokens {
payload["max_output_tokens"] = json!(mt);
}
if let Some(tp) = top_p {
payload["top_p"] = json!(tp);
}
if let Some(s) = store {
payload["store"] = json!(s);
}
if let Some(m) = metadata {
payload["metadata"] = m;
}
if let Some(pr) = previous_response_id {
payload["previous_response_id"] = json!(pr);
}
if let Some(pt) = parallel_tool_calls {
payload["parallel_tool_calls"] = json!(pt);
}
if let Some(t) = truncation {
payload["truncation"] = json!(t);
}
if let Some(rf) = response_format {
payload["response_format"] = rf;
}
if let Some(inc) = include {
payload["include"] = json!(inc);
}
if let Some(u) = user {
payload["user"] = json!(u);
}
let url = self.inner.url("/v1/responses");
let response = self
.inner
.http_client
.post(&url)
.json(&payload)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().await.map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub async fn async_rag_stream(
&self,
model: &str,
messages: Vec<Message>,
namespace: &str,
temperature: Option<f64>,
max_tokens: Option<u32>,
limit: Option<u32>,
threshold: Option<f64>,
include_sources: Option<bool>,
) -> Result<RagAsyncStream, CencoriError> {
let mut payload = json!({
"model": model,
"messages": messages,
"namespace": namespace,
"stream": true,
});
if let Some(t) = temperature {
payload["temperature"] = json!(t);
}
if let Some(mt) = max_tokens {
payload["maxTokens"] = json!(mt);
}
payload["limit"] = json!(limit.unwrap_or(5));
payload["threshold"] = json!(threshold.unwrap_or(0.5));
payload["include_sources"] = json!(include_sources.unwrap_or(true));
let url = self.inner.url("/api/ai/rag");
let response = self
.inner
.http_client
.post(&url)
.json(&payload)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
if status == 401 {
return Err(CencoriError::Authentication);
}
if status == 429 {
return Err(CencoriError::RateLimit);
}
if !status.is_success() {
return Err(CencoriError::api(
status.as_u16(),
format!("RAG stream failed with status {status}"),
));
}
Ok(RagAsyncStream {
reader: response,
buffer: String::new(),
})
}
}
pub struct ChatStreamIter {
reader: reqwest::blocking::Response,
}
impl Iterator for ChatStreamIter {
type Item = Result<StreamChunk, CencoriError>;
fn next(&mut self) -> Option<Self::Item> {
use std::io::{BufRead, BufReader};
let mut buffer = String::new();
let mut reader = BufReader::new(&mut self.reader);
loop {
buffer.clear();
match reader.read_line(&mut buffer) {
Ok(0) => return None, Ok(_) => {
let trimmed = buffer.trim();
if trimmed.is_empty() {
continue;
}
if let Some(chunk) = parse_sse_line(trimmed) {
if chunk.error.is_some() {
return None;
}
return Some(Ok(chunk));
}
}
Err(e) => return Some(Err(CencoriError::Stream(format!("Read error: {e}")))),
}
}
}
}
pub struct RagAsyncStream {
reader: reqwest::Response,
buffer: String,
}
impl RagAsyncStream {
pub async fn next(&mut self) -> Option<Result<RagStreamChunk, CencoriError>> {
loop {
if let Some(idx) = self.buffer.find('\n') {
let line = self.buffer[..idx].to_string();
self.buffer = self.buffer[idx + 1..].to_string();
let trimmed = line.trim().to_string();
if trimmed.is_empty() {
continue;
}
if let Some(chunk) = parse_rag_sse_line(&trimmed) {
return Some(Ok(chunk));
}
continue;
}
match self.reader.chunk().await {
Ok(Some(bytes)) => {
let text = String::from_utf8_lossy(&bytes).to_string();
self.buffer.push_str(&text);
continue;
}
Ok(None) => {
if !self.buffer.is_empty() {
let remaining = std::mem::take(&mut self.buffer);
if let Some(chunk) = parse_rag_sse_line(remaining.trim()) {
return Some(Ok(chunk));
}
}
return None;
}
Err(e) => {
return Some(Err(CencoriError::Stream(format!("Read error: {e}"))));
}
}
}
}
}
fn parse_sse_line(line: &str) -> Option<StreamChunk> {
let line = line.trim();
if !line.starts_with("data: ") {
return None;
}
let data_str = &line[6..];
if data_str == "[DONE]" {
return Some(StreamChunk {
delta: String::new(),
finish_reason: None,
tool_calls: None,
error: None,
});
}
let data: Value = serde_json::from_str(data_str).ok()?;
if let Some(err) = data.get("error").and_then(|e| e.as_str()) {
return Some(StreamChunk {
delta: String::new(),
finish_reason: None,
tool_calls: None,
error: Some(err.to_string()),
});
}
let tool_calls = serde_json::from_value::<Vec<ToolCall>>(data["toolCalls"].clone())
.ok()
.or_else(|| serde_json::from_value::<Vec<ToolCall>>(data["tool_calls"].clone()).ok());
Some(StreamChunk {
delta: data
.get("delta")
.and_then(|d| d.as_str())
.unwrap_or("")
.to_string(),
finish_reason: data
.get("finish_reason")
.and_then(|f| f.as_str())
.map(String::from),
tool_calls,
error: None,
})
}
fn parse_rag_sse_line(line: &str) -> Option<RagStreamChunk> {
let line = line.trim();
if !line.starts_with("data: ") {
return None;
}
let data_str = &line[6..];
if data_str == "[DONE]" {
return None;
}
let data: Value = serde_json::from_str(data_str).ok()?;
Some(RagStreamChunk {
type_field: data["type"].as_str().unwrap_or("content").to_string(),
delta: data["delta"].as_str().map(String::from),
finish_reason: data["finish_reason"].as_str().map(String::from),
sources: data["sources"].as_array().map(|arr| {
arr.iter()
.map(|s| RagSource {
content: s["content"].as_str().unwrap_or_default().to_string(),
metadata: s["metadata"]
.as_object()
.map(|m| m.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
.unwrap_or_default(),
similarity: s["similarity"].as_f64().unwrap_or(0.0),
})
.collect()
}),
})
}