use chrono::Local;
use futures::StreamExt;
use openai_dive::v1::{
    api::Client,
    endpoints::chat::RoleTrackingStream,
    resources::{
        chat::{
            ChatCompletionParameters,
            ChatMessage,
            ChatMessageContent,
            DeltaChatMessage,
        },
        shared::Usage,
    },
};
use tokio::sync::mpsc::Sender;
//use tokio::time::{sleep, Duration};
use tracing::{event, Level};

/// info: 记录所有用户的信息
/// error: 定义的错误类型，用于错误传递
use crate::{
    info::{
        insert_message, // 将指定message插入到指定uuid的messages中
        DataType, // 存储问答信息的数据
        get_messages_num, // 获取指定uuid的messages总数
        update_token,
    },
    api::handlers::chat::MainData,
    error::MyError,
};

const THOUGHT_START: &[&str] = &["<think>", "<thought>", "<thinking>", "<reasoning>"];
const THOUGHT_END: &[&str] = &["</think>", "</thought>", "</thinking>", "</reasoning>"];


/// stream接收答案，通过管道将接受的答案传输出去，并将完整答案记录到该uuid中
/// uuid: 当前对话的uuid
/// sender: 管道发送stream答案
/// client: 创建的openai客户端
/// parameters: 提问的参数
/// model: 模型名称
pub async fn use_stream(
    uuid: String,
    sender: Sender<Vec<u8>>,
    client: Client,
    parameters: ChatCompletionParameters,
    model: &str,
    show_thought: bool,
) -> Result<(), MyError> {
    let mut whole_answer = "".to_string(); // 存储完整答案
    let mut msg_token = None;
    let mut role: u8 = 3; // 1表示User，2表示System，3表示Assistant，4表示Developer
    let mut think = false; // 是否属于think思维链部分
    let mut start_stop_think: u8 = 0; // 显示思维链
    let tmp_time = Local::now().format("%Y-%m-%d %H:%M:%S").to_string(); // 回答的当前时间，例如：2024-10-21 16:35:47
    let messages_num = get_messages_num(&uuid); // 流式输出传输答案时，答案还未插入到服务端记录中，因此这里获取总消息数不需要减1
    // 创建stream
    let stream = client.chat().create_stream(parameters).await.map_err(|e| MyError::ApiError{uuid: uuid.clone(), error: e})?;
    let mut tracked_stream = RoleTrackingStream::new(stream);
    // 遍历接受stream信息
    'outer: while let Some(response) = tracked_stream.next().await {
        //let chat_response = response.map_err(|e| MyError::ApiError{uuid: uuid.clone(), error: e})?;
        let chat_response = match response { // 这里遇到错误不能直接返回，否则服务端与前端id差一个，后面代码insert_message没有执行，下个问题会显示在这个未回答完的答案末尾
            Ok(r) => r,
            Err(e) => { // 可能api服务端停止发送答案
                event!(Level::ERROR, "{} {:?}", uuid, e);
                break;
            },
        };
        for choice in chat_response.choices { // 这里要用`for`，不能用`for_each()`，`choice`不是async报错
            match &choice.delta {
                DeltaChatMessage::User{content: ChatMessageContent::Text(c), ..} => {
                    whole_answer += &c;
                    role = 1;
                    //print!("{}", c);
                    //if let Err(e) = sender.send(format!("data: {}\n\n", c).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                    //if let Err(e) = sender.send(format!("data: {}\n\n", c.replace("\n", "srxtzn")).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                    if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, c.replace("\n", "srxtzn"), true, false, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                        //println!("channel send error: {:?}", e);
                        event!(Level::WARN, "channel send error: {:?}", e);
                        break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                    }
                    //sleep(Duration::from_millis(10)).await; // 这里设置间隔10ms，否则输出太快，客户端一大段一大段的输出，不流畅。不是获取stream的问题，确实是每个字符流式输出，只是太快了
                },
                DeltaChatMessage::System{content: ChatMessageContent::Text(c), ..} => {
                    whole_answer += &c;
                    role = 2;
                    //print!("{}", c);
                    //if let Err(e) = sender.send(format!("data: {}\n\n", c).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                    //if let Err(e) = sender.send(format!("data: {}\n\n", c.replace("\n", "srxtzn")).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                    if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, c.replace("\n", "srxtzn"), true, false, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                        //println!("channel send error: {:?}", e);
                        event!(Level::WARN, "channel send error: {:?}", e);
                        break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                    }
                    //sleep(Duration::from_millis(10)).await; // 这里设置间隔10ms，否则输出太快，客户端一大段一大段的输出，不流畅。不是获取stream的问题，确实是每个字符流式输出，只是太快了
                },
                /*
                DeltaChatMessage::Assistant{content: Some(ChatMessageContent::Text(c)), ..} => {
                    whole_answer += &c;
                    role = 3;
                    //print!("{}", c);
                    //if let Err(e) = sender.send(format!("data: {}\n\n", c).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                    //if let Err(e) = sender.send(format!("data: {}\n\n", c.replace("\n", "srxtzn")).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                    if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, c.replace("\n", "srxtzn"), true, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                        //println!("channel send error: {:?}", e);
                        event!(Level::WARN, "channel send error: {:?}", e);
                        break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                    }
                    //sleep(Duration::from_millis(10)).await; // 这里设置间隔10ms，否则输出太快，客户端一大段一大段的输出，不流畅。不是获取stream的问题，确实是每个字符流式输出，只是太快了
                },
                */
                DeltaChatMessage::Assistant{content: tmp_content, reasoning_content: tmp_reasoning_content, ..} => {
                    let c = match (tmp_content, tmp_reasoning_content) {
                        (Some(ChatMessageContent::Text(c)), None) => { // 答案
                            if start_stop_think == 0 && THOUGHT_START.contains(&c.as_ref()) {
                                think = true;
                                start_stop_think = 1; // 开始思维链
                                None
                            } else if start_stop_think == 7 {
                                start_stop_think = 4; // 结束思维链
                                whole_answer += &c;
                                Some(c)
                            } else if start_stop_think == 8 && THOUGHT_END.contains(&c.as_ref()) {
                                start_stop_think = 4; // 结束思维链
                                None
                            } else if start_stop_think == 1 {
                                start_stop_think = 8;
                                whole_answer += &c;
                                Some(c)
                            } else {
                                whole_answer += &c;
                                Some(c)
                            }
                        },
                        (None, Some(c)) => { // 思考过程
                            if start_stop_think == 0 {
                                think = true;
                                start_stop_think = 2; // 开始思维链
                            } else if start_stop_think == 2 {
                                start_stop_think = 7;
                            }
                            Some(c)
                        },
                        _ => continue,
                    };
                    if think && !show_thought { // 不显示思考过程则不传递，仅记录在whole_answer中
                        continue
                    }
                    if think {
                        if start_stop_think == 1 || start_stop_think == 2 {
                            //if let Err(e) = sender.send("data: thinking ...<br>\n\n".as_bytes().to_vec()).await { // 传递数据以`data: `起始，以`\n\n`终止
                            if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, "<div class='think'>thinking ...<br>".to_string(), true, false, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                                event!(Level::WARN, "channel send error: {:?}", e);
                                break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                            }
                        } else if start_stop_think == 4 {
                            //if let Err(e) = sender.send("data: <br>\n\n".as_bytes().to_vec()).await { // 传递数据以`data: `起始，以`\n\n`终止
                            if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, "srxtzn</div>srxtznsrxtzn---srxtznsrxtzn".to_string(), true, false, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                                event!(Level::WARN, "channel send error: {:?}", e);
                                break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                            }
                            think = false;
                        }
                    }
                    role = 3;
                    if let Some(c_str) = c {
                        //print!("{}", c);
                        //if let Err(e) = sender.send(format!("data: {}\n\n", c).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                        //if let Err(e) = sender.send(format!("data: {}\n\n", c.replace("\n", "srxtzn")).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                        if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, c_str.replace("\n", "srxtzn"), true, false, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                            //println!("channel send error: {:?}", e);
                            event!(Level::WARN, "channel send error: {:?}", e);
                            break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                        }
                        //sleep(Duration::from_millis(10)).await; // 这里设置间隔10ms，否则输出太快，客户端一大段一大段的输出，不流畅。不是获取stream的问题，确实是每个字符流式输出，只是太快了
                    }
                },
                DeltaChatMessage::Developer{content: ChatMessageContent::Text(c), ..} => {
                    whole_answer += &c;
                    role = 4;
                    //print!("{}", c);
                    //if let Err(e) = sender.send(format!("data: {}\n\n", c).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                    //if let Err(e) = sender.send(format!("data: {}\n\n", c.replace("\n", "srxtzn")).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                    if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, c.replace("\n", "srxtzn"), true, false, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                        //println!("channel send error: {:?}", e);
                        event!(Level::WARN, "channel send error: {:?}", e);
                        break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                    }
                    //sleep(Duration::from_millis(10)).await; // 这里设置间隔10ms，否则输出太快，客户端一大段一大段的输出，不流畅。不是获取stream的问题，确实是每个字符流式输出，只是太快了
                },
                /*
                DeltaChatMessage::Untagged{content: Some(ChatMessageContent::Text(c)), ..} => { // llama.cpp的llama-server的api传输的答案会在这里
                    whole_answer += &c;
                    role = 3;
                    //print!("{}", c);
                    //if let Err(e) = sender.send(format!("data: {}\n\n", c).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                    //if let Err(e) = sender.send(format!("data: {}\n\n", c.replace("\n", "srxtzn")).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                    if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, c.replace("\n", "srxtzn"), true, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                        //println!("channel send error: {:?}", e);
                        event!(Level::WARN, "channel send error: {:?}", e);
                        break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                    }
                    //sleep(Duration::from_millis(10)).await; // 这里设置间隔10ms，否则输出太快，客户端一大段一大段的输出，不流畅。不是获取stream的问题，确实是每个字符流式输出，只是太快了
                    if think {
                        if whole_answer == "<think>" {
                            whole_answer += "thinking ...<br>"; // 开始think时在最开始显示“thinking ...”
                            //if let Err(e) = sender.send("data: thinking ...<br>\n\n".as_bytes().to_vec()).await { // 传递数据以`data: `起始，以`\n\n`终止
                            if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, "thinking ...<br>".to_string(), true, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                                event!(Level::WARN, "channel send error: {:?}", e);
                                break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                            }
                        } else if c == "</think>" {
                            whole_answer += "<br>"; // think结束后换行
                            //if let Err(e) = sender.send("data: <br>\n\n".as_bytes().to_vec()).await { // 传递数据以`data: `起始，以`\n\n`终止
                            if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, "<br>".to_string(), true, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                                event!(Level::WARN, "channel send error: {:?}", e);
                                break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                            }
                            think = false;
                        }
                    }
                },
                */
                DeltaChatMessage::Untagged{content: tmp_content, reasoning_content: tmp_reasoning_content, ..} => { // llama.cpp的llama-server的api传输的答案会在这里
                    let c = match (tmp_content, tmp_reasoning_content) {
                        (Some(ChatMessageContent::Text(c)), None) => { // 答案
                            if start_stop_think == 0 && THOUGHT_START.contains(&c.as_ref()) {
                                think = true;
                                start_stop_think = 1; // 开始思维链
                                None
                            } else if start_stop_think == 7 {
                                start_stop_think = 4; // 结束思维链
                                whole_answer += &c;
                                Some(c)
                            } else if start_stop_think == 8 && THOUGHT_END.contains(&c.as_ref()) {
                                start_stop_think = 4; // 结束思维链
                                None
                            } else if start_stop_think == 1 {
                                start_stop_think = 8;
                                whole_answer += &c;
                                Some(c)
                            } else {
                                whole_answer += &c;
                                Some(c)
                            }
                        },
                        (None, Some(c)) => { // 思考过程
                            if start_stop_think == 0 {
                                think = true;
                                start_stop_think = 2; // 开始思维链
                            } else if start_stop_think == 2 {
                                start_stop_think = 7;
                            }
                            Some(c)
                        },
                        _ => continue,
                    };
                    if think && !show_thought { // 不显示思考过程则不传递，仅记录在whole_answer中
                        continue
                    }
                    if think {
                        if start_stop_think == 1 || start_stop_think == 2 {
                            //if let Err(e) = sender.send("data: thinking ...<br>\n\n".as_bytes().to_vec()).await { // 传递数据以`data: `起始，以`\n\n`终止
                            if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, "<div class='think'>thinking ...<br>".to_string(), true, false, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                                event!(Level::WARN, "channel send error: {:?}", e);
                                break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                            }
                        } else if start_stop_think == 4 {
                            //if let Err(e) = sender.send("data: <br>\n\n".as_bytes().to_vec()).await { // 传递数据以`data: `起始，以`\n\n`终止
                            if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, "srxtzn</div>srxtznsrxtzn---srxtznsrxtzn".to_string(), true, false, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                                event!(Level::WARN, "channel send error: {:?}", e);
                                break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                            }
                            think = false;
                        }
                    }
                    role = 3;
                    if let Some(c_str) = c {
                        //print!("{}", c);
                        //if let Err(e) = sender.send(format!("data: {}\n\n", c).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                        //if let Err(e) = sender.send(format!("data: {}\n\n", c.replace("\n", "srxtzn")).into_bytes()).await { // 传递数据以`data: `起始，以`\n\n`终止
                        if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, c_str.replace("\n", "srxtzn"), true, false, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
                            //println!("channel send error: {:?}", e);
                            event!(Level::WARN, "channel send error: {:?}", e);
                            break 'outer; // 可能客户端停止接收答案，这里也要停止，否则服务端依然接收答案，计费没停止
                        }
                        //sleep(Duration::from_millis(10)).await; // 这里设置间隔10ms，否则输出太快，客户端一大段一大段的输出，不流畅。不是获取stream的问题，确实是每个字符流式输出，只是太快了
                    }
                },
                _ => (),
            }
        }
        // get token usage from last response
        if let Some(usage) = chat_response.usage {
            msg_token = get_print_token(usage, &uuid);
        }
    }
    // if whole_answer is empty, send `no response result` to client
    if whole_answer.is_empty() {
        if let Err(e) = sender.send(MainData::prepare_sse(&uuid, messages_num, "no response result".to_string(), true, false, false, false, false, None, Some(0), None, false)?).await { // 传递数据以`data: `起始，以`\n\n`终止
            //println!("channel send error: {:?}", e);
            event!(Level::WARN, "channel send error: {:?}", e);
        } else {
            event!(Level::WARN, "{} no response result", uuid);
        }
    }
    // 记录答案
    let message = match role {
        1 => ChatMessage::User{
            content: ChatMessageContent::Text(whole_answer.clone()),
            name: None,
        },
        2 => ChatMessage::System{
            content: ChatMessageContent::Text(whole_answer.clone()),
            name: None,
        },
        3 => ChatMessage::Assistant{
            content: Some(ChatMessageContent::Text(whole_answer.clone())),
            reasoning_content: None,
            refusal: None,
            name: None,
            audio: None,
            tool_calls: None,
        },
        4 => ChatMessage::Developer{
            content: ChatMessageContent::Text(whole_answer.clone()),
            name: None,
        },
        _ => ChatMessage::User{
            content: ChatMessageContent::Text(whole_answer.clone()),
            name: None,
        },
    };
    // 将回答加到问答记录中
    insert_message(&uuid, message, msg_token, tmp_time, false, DataType::Normal, None, model, None);
    Ok(())
}

/// 非stream，接收openai的完整答案，并将完整答案记录到该uuid中，最后返回完整答案以及思考内容
/// uuid: 当前对话的uuid
/// client: 创建的openai客户端
/// parameters: 提问的参数
/// model: 模型名称
pub async fn not_use_stream(
    uuid: String,
    client: Client,
    parameters: ChatCompletionParameters,
    model: &str,
    insert_this_message: bool, // insert result to history messages
) -> Result<(String, Option<String>), MyError> {
    //let result = client.chat().create(parameters).await.map_err(|e| MyError::ApiError{uuid: uuid.clone(), error: e})?;
    let mut whole_answer = "".to_string(); // 存储完整答案
    let mut thinking = "".to_string(); // 思考部分
    let mut msg_token = None;
    let mut role: u8 = 3; // 1表示User，2表示System，3表示Assistant，4表示Developer
    match client.chat().create(parameters).await { // 这里遇到错误不能直接返回，否则服务端与前端id差一个，后面代码insert_message没有执行，下个问题会显示在这个未回答完的答案末尾
        Ok(result) => {
            // 获取答案
            match &result.choices[0].message {
                ChatMessage::System{content, ..} => {
                    role = 2;
                    match content {
                        ChatMessageContent::Text(res) => {
                            whole_answer = res.to_string();
                            //println!("{}", res);
                        },
                        ChatMessageContent::ContentPart(res_vec) => event!(Level::INFO, "System ChatMessageContent::ContentPart {:?}", res_vec),
                        ChatMessageContent::None => event!(Level::INFO, "System ChatMessageContent::None"),
                    }
                },
                ChatMessage::User{content, ..} => {
                    role = 1;
                    match content {
                        ChatMessageContent::Text(res) => {
                            whole_answer = res.to_string();
                            //println!("{}", res);
                        },
                        ChatMessageContent::ContentPart(res_vec) => event!(Level::INFO, "System ChatMessageContent::ContentPart {:?}", res_vec),
                        ChatMessageContent::None => event!(Level::INFO, "System ChatMessageContent::None"),
                    }
                },
                /*
                ChatMessage::Assistant{content, ..} => {
                    role = 3;
                    match content {
                        Some(c) => {
                            match c {
                                ChatMessageContent::Text(res) => {
                                    whole_answer = res.to_string();
                                    //println!("{}", res);
                                },
                                ChatMessageContent::ContentPart(res_vec) => event!(Level::INFO, "System ChatMessageContent::ContentPart {:?}", res_vec),
                                ChatMessageContent::None => event!(Level::INFO, "System ChatMessageContent::None"),
                            }
                        },
                        None => event!(Level::INFO, "Assistant: None"), // println!("Assistant: None"),
                    }
                },
                */
                ChatMessage::Assistant{content: tmp_content, reasoning_content: tmp_reasoning_content, ..} => {
                    role = 3;
                    if let Some(c) = tmp_reasoning_content {
                        thinking = format!("<div class='think'>thinking ...<br>{}srxtzn</div>srxtznsrxtzn---srxtznsrxtzn", c);
                    }
                    match tmp_content {
                        Some(c) => {
                            match c {
                                ChatMessageContent::Text(res) => {
                                    // 如果思考部分没有单独方在`reasoning_content`，则尝试从答案主体中获取
                                    if thinking.is_empty() {
                                        for (start, end) in THOUGHT_START.iter().zip(THOUGHT_END.iter()) {
                                            if res.starts_with(start) && res.contains(end) {
                                                let (first, second) = res.split_once(end).unwrap();
                                                thinking = format!("<div class='think'>thinking ...<br>{}srxtzn</div>srxtznsrxtzn---srxtznsrxtzn", first);
                                                whole_answer = second.to_string();
                                                break
                                            }
                                        }
                                        if thinking.is_empty() {
                                            whole_answer = res.to_string();
                                        }
                                    } else {
                                        whole_answer = res.to_string();
                                    }
                                    //println!("{}", res);
                                },
                                ChatMessageContent::ContentPart(res_vec) => event!(Level::INFO, "System ChatMessageContent::ContentPart {:?}", res_vec),
                                ChatMessageContent::None => event!(Level::INFO, "System ChatMessageContent::None"),
                            }
                        },
                        None => event!(Level::INFO, "Assistant: None"), // println!("Assistant: None"),
                    }
                },
                ChatMessage::Developer{content, ..} => {
                    role = 4;
                    match content {
                        ChatMessageContent::Text(res) => {
                            whole_answer = res.to_string();
                            //println!("{}", res);
                        },
                        ChatMessageContent::ContentPart(res_vec) => event!(Level::INFO, "System ChatMessageContent::ContentPart {:?}", res_vec),
                        ChatMessageContent::None => event!(Level::INFO, "System ChatMessageContent::None"),
                    }
                },
                ChatMessage::Tool{content, ..} => event!(Level::INFO, "{}", content), // println!("{}", content),
            }
            if let Some(usage) = result.usage {
                msg_token = get_print_token(usage, &uuid);
            }
        },
        Err(e) => event!(Level::ERROR, "{} {:?}", uuid, e),
    }
    let tmp_time = Local::now().format("%Y-%m-%d %H:%M:%S").to_string(); // 回答的当前时间，例如：2024-10-21 16:35:47

    // 记录答案
    let message = match role {
        1 => ChatMessage::User{
            content: ChatMessageContent::Text(whole_answer.clone()),
            name: None,
        },
        2 => ChatMessage::System{
            content: ChatMessageContent::Text(whole_answer.clone()),
            name: None,
        },
        3 => ChatMessage::Assistant{
            content: Some(ChatMessageContent::Text(whole_answer.clone())),
            reasoning_content: None,
            refusal: None,
            name: None,
            audio: None,
            tool_calls: None,
        },
        4 => ChatMessage::Developer{
            content: ChatMessageContent::Text(whole_answer.clone()),
            name: None,
        },
        _ => ChatMessage::User{
            content: ChatMessageContent::Text(whole_answer.clone()),
            name: None,
        },
    };
    // 将回答加到问答记录中
    if insert_this_message {
        insert_message(&uuid, message, msg_token, tmp_time, false, DataType::Normal, None, model, None);
    } else if let Some(tokens) = msg_token {
        update_token(&uuid, tokens);
    }
    Ok((whole_answer, if thinking.is_empty() { None } else { Some(thinking) }))
}

// print and update total token usage
fn get_print_token(usage: Usage, uuid: &str) -> Option<(u32, u32, u32)> {
    // 缓存命中的token数
    let cached_tokens = match usage.prompt_tokens_details {
        Some(details) => details.cached_tokens.unwrap_or(0),
        None => 0,
    };
    // 推理部分的token数
    let reasoning_tokens = match usage.completion_tokens_details {
        Some(details) => details.reasoning_tokens.unwrap_or(0),
        None => 0,
    };
    match (usage.prompt_tokens, usage.completion_tokens) {
        (Some(prompt_tokens), Some(completion_tokens)) => {
            event!(Level::INFO, "{} {{prompt_tokens: {} (cached_tokens: {}), completion_tokens: {} (reasoning_tokens: {}), total_tokens: {}}}", uuid, prompt_tokens, cached_tokens, completion_tokens, reasoning_tokens, usage.total_tokens);
            Some((prompt_tokens, completion_tokens, usage.total_tokens))
        },
        (Some(prompt_tokens), None) => {
            event!(Level::INFO, "{} {{prompt_tokens: {} (cached_tokens: {}), completion_tokens: None, total_tokens: {}}}", uuid, cached_tokens, prompt_tokens, usage.total_tokens);
            Some((prompt_tokens, 0, usage.total_tokens))
        },
        (None, Some(completion_tokens)) => {
            event!(Level::INFO, "{} {{prompt_tokens: None, completion_tokens: {} (reasoning_tokens: {}), total_tokens: {}}}", uuid, completion_tokens, reasoning_tokens, usage.total_tokens);
            Some((0, completion_tokens, usage.total_tokens))
        },
        (None, None) => {
            event!(Level::INFO, "{} {{prompt_tokens: None, completion_tokens: None, total_tokens: {}}}", uuid, usage.total_tokens);
            Some((0, 0, usage.total_tokens))
        },
    }
}
