volition-core 0.2.0

Core library for the Volition AI agent
Documentation
// volition-agent-core/src/models/chat.rs

//! Structures related to chat messages and API responses for OpenAI-compatible endpoints.

use super::tools::ToolCall;
use serde::{Deserialize, Serialize};

/// Represents a message in the chat history sequence sent to/from the AI.
///
/// This struct is used for all roles (`system`, `user`, `assistant`, `tool`)
/// within the conversation history managed by the [`Agent`](crate::Agent).
/// Optional fields are handled by `serde` during serialization/deserialization.
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct ChatMessage {
    /// The role of the message sender (e.g., "system", "user", "assistant", "tool").
    pub role: String,
    /// The text content of the message.
    /// Present for `system`, `user`, and final `assistant` messages, and `tool` messages.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub content: Option<String>,
    /// A list of tool calls requested by the assistant.
    /// Only present for `assistant` messages that request tool execution.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub tool_calls: Option<Vec<ToolCall>>,
    /// The ID of the tool call this message is responding to.
    /// Only present for `tool` messages.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub tool_call_id: Option<String>,
}

/// Represents one of the possible responses provided by the AI model.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Choice {
    /// The index of this choice in the list of choices.
    pub index: u32,
    /// The message generated by the AI for this choice.
    pub message: ChatMessage,
    /// The reason the AI model stopped generating tokens (e.g., "stop", "tool_calls").
    pub finish_reason: String,
}

/// Represents the overall structure of a successful response from the AI chat completion API.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ApiResponse {
    /// A unique identifier for the API response.
    pub id: String,
    /// The generated text content.
    pub content: String,
    /// The reason the AI model stopped generating tokens (e.g., "stop", "tool_calls").
    pub finish_reason: String,
    /// Number of tokens used in the prompt.
    pub prompt_tokens: u32,
    /// Number of tokens used in the completion.
    pub completion_tokens: u32,
    /// Total number of tokens used.
    pub total_tokens: u32,
    /// A list of completion choices. Currently, only the first choice is used by the [`Agent`](crate::Agent).
    pub choices: Vec<Choice>,
}