volition_core/models/
chat.rs

1// volition-agent-core/src/models/chat.rs
2
3//! Structures related to chat messages and API responses for OpenAI-compatible endpoints.
4
5use super::tools::ToolCall;
6use serde::{Deserialize, Serialize};
7
8/// Represents a message in the chat history sequence sent to/from the AI.
9///
10/// This struct is used for all roles (`system`, `user`, `assistant`, `tool`)
11/// within the conversation history managed by the [`Agent`](crate::Agent).
12/// Optional fields are handled by `serde` during serialization/deserialization.
13#[derive(Serialize, Deserialize, Debug, Clone, Default)]
14pub struct ChatMessage {
15    /// The role of the message sender (e.g., "system", "user", "assistant", "tool").
16    pub role: String,
17    /// The text content of the message.
18    /// Present for `system`, `user`, and final `assistant` messages, and `tool` messages.
19    #[serde(skip_serializing_if = "Option::is_none", default)]
20    pub content: Option<String>,
21    /// A list of tool calls requested by the assistant.
22    /// Only present for `assistant` messages that request tool execution.
23    #[serde(skip_serializing_if = "Option::is_none", default)]
24    pub tool_calls: Option<Vec<ToolCall>>,
25    /// The ID of the tool call this message is responding to.
26    /// Only present for `tool` messages.
27    #[serde(skip_serializing_if = "Option::is_none", default)]
28    pub tool_call_id: Option<String>,
29}
30
31/// Represents one of the possible responses provided by the AI model.
32#[derive(Serialize, Deserialize, Debug, Clone)]
33pub struct Choice {
34    /// The index of this choice in the list of choices.
35    pub index: u32,
36    /// The message generated by the AI for this choice.
37    pub message: ChatMessage,
38    /// The reason the AI model stopped generating tokens (e.g., "stop", "tool_calls").
39    pub finish_reason: String,
40}
41
42/// Represents the overall structure of a successful response from the AI chat completion API.
43#[derive(Serialize, Deserialize, Debug, Clone)]
44pub struct ApiResponse {
45    /// A unique identifier for the API response.
46    pub id: String,
47    /// The generated text content.
48    pub content: String,
49    /// The reason the AI model stopped generating tokens (e.g., "stop", "tool_calls").
50    pub finish_reason: String,
51    /// Number of tokens used in the prompt.
52    pub prompt_tokens: u32,
53    /// Number of tokens used in the completion.
54    pub completion_tokens: u32,
55    /// Total number of tokens used.
56    pub total_tokens: u32,
57    /// A list of completion choices. Currently, only the first choice is used by the [`Agent`](crate::Agent).
58    pub choices: Vec<Choice>,
59}