From c976db70632983317047ee7820995530b701293d Mon Sep 17 00:00:00 2001 From: LucasDLTG Date: Mon, 11 May 2026 19:04:14 +0200 Subject: [PATCH] feat: return conversation id --- src/dto/api.rs | 2 ++ src/providers/ollama/mapper.rs | 2 ++ src/routes/v1/chat.rs | 16 +++++++++------- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/dto/api.rs b/src/dto/api.rs index 007d2ff..f841fd5 100644 --- a/src/dto/api.rs +++ b/src/dto/api.rs @@ -155,6 +155,8 @@ pub struct ChatCompletionResponse { pub model: String, pub choices: Vec, pub usage: Option, // optional (Ollama may not always provide) + + pub conversation_id: Option, } #[derive(Debug, Serialize, Deserialize, ToSchema)] diff --git a/src/providers/ollama/mapper.rs b/src/providers/ollama/mapper.rs index b3863df..18a4ae0 100644 --- a/src/providers/ollama/mapper.rs +++ b/src/providers/ollama/mapper.rs @@ -81,6 +81,8 @@ impl From for api::ChatCompletionResponse { completion_tokens, total_tokens: prompt_tokens + completion_tokens, }), + + conversation_id: None, } } } diff --git a/src/routes/v1/chat.rs b/src/routes/v1/chat.rs index b81ad7c..ca568c8 100644 --- a/src/routes/v1/chat.rs +++ b/src/routes/v1/chat.rs @@ -126,7 +126,7 @@ pub async fn chat_completions( ) -> Result { tracing::debug!("Received /chat/completion with body {:?}", body); - if matches!(&auth, Auth::Jwt(_)) { + let conversation_id = if matches!(&auth, Auth::Jwt(_)) { tracing::debug!( "Is conversation_id existing: {:?}", body.conversation_id.is_some() @@ -137,7 +137,7 @@ pub async fn chat_completions( .await .map_err(crate::databases::postgres::errors::into_http_response)?; - let conversation_id = match &conversation_state { + let id = match &conversation_state { ConversationState::Existing(uuid) => *uuid, ConversationState::Created(uuid) => { let conversation_id = *uuid; @@ -161,8 +161,11 @@ pub async fn chat_completions( } }; - tracing::debug!("Using conversation_id: {:?}", conversation_id); - } + tracing::debug!("Using conversation_id: {:?}", id); + Some(id) + } else { + None + }; if body.base.stream { let stream = state @@ -170,17 +173,16 @@ pub async fn chat_completions( .chat_completions_stream(&body) .await .map_err(into_http_response)?; - Ok(Sse::new(stream) .keep_alive(KeepAlive::default()) .into_response()) } else { - let response = state + let mut response = state .ollama .chat_completions(&body) .await .map_err(into_http_response)?; - + response.conversation_id = conversation_id; Ok(Json(response).into_response()) } }