use serde::{Deserialize, Serialize}; use serde_json::from_str; use crate::v1::{chat, common, constants, error, tool}; // ----------------------------------------------------------------------------- // Response #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ChatStreamChunk { pub id: String, pub object: String, /// Unix timestamp (in seconds). pub created: u64, pub model: constants::Model, pub choices: Vec, #[serde(skip_serializing_if = "Option::is_none")] pub usage: Option, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ChatStreamChunkChoice { pub index: u32, pub delta: ChatStreamChunkChoiceDelta, pub finish_reason: Option, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ChatStreamChunkChoiceDelta { pub role: Option, #[serde(default)] pub content: Option, #[serde(skip_serializing_if = "Option::is_none")] pub tool_calls: Option>, } /// Extracts serialized chunks from a stream message. pub fn get_chunk_from_stream_message_line( line: &str, ) -> Result>, error::ApiError> { if line.trim() == "data: [DONE]" { return Ok(None); } let chunk_as_json = line.trim_start_matches("data: ").trim(); if chunk_as_json.is_empty() { return Ok(Some(vec![])); } match from_str::(chunk_as_json) { Ok(chunk) => Ok(Some(vec![chunk])), Err(e) => Err(error::ApiError { message: e.to_string(), }), } }