Skip to main content

openai_tools/conversations/
response.rs

1//! OpenAI Conversations API Response Types
2//!
3//! This module defines the response structures for the OpenAI Conversations API.
4//! The Conversations API allows you to create and manage long-running conversations
5//! with the Responses API.
6
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9
10/// Represents an OpenAI Conversation object.
11///
12/// A conversation stores items (messages, tool calls, tool outputs, etc.)
13/// and can be used with the Responses API for multi-turn interactions.
14///
15/// # Example
16///
17/// ```rust
18/// use openai_tools::conversations::response::Conversation;
19///
20/// // Example conversation from API response
21/// let json = r#"{
22///     "id": "conv_abc123",
23///     "object": "conversation",
24///     "created_at": 1741900000,
25///     "metadata": {"topic": "demo"}
26/// }"#;
27///
28/// let conversation: Conversation = serde_json::from_str(json).unwrap();
29/// assert_eq!(conversation.id, "conv_abc123");
30/// ```
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct Conversation {
33    /// The unique ID of the conversation (e.g., "conv_abc123")
34    pub id: String,
35    /// Object type, always "conversation"
36    pub object: String,
37    /// Unix timestamp (in seconds) when the conversation was created
38    pub created_at: i64,
39    /// Set of key-value pairs for storing additional information
40    ///
41    /// Keys are strings with a maximum length of 64 characters.
42    /// Values are strings with a maximum length of 512 characters.
43    #[serde(default)]
44    pub metadata: Option<HashMap<String, String>>,
45}
46
47/// Response structure for listing conversations.
48///
49/// Contains a list of conversation objects with pagination information.
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct ConversationListResponse {
52    /// Object type, always "list"
53    pub object: String,
54    /// Array of conversation objects
55    pub data: Vec<Conversation>,
56    /// The ID of the first object in the list
57    #[serde(default)]
58    pub first_id: Option<String>,
59    /// The ID of the last object in the list
60    #[serde(default)]
61    pub last_id: Option<String>,
62    /// Whether there are more objects available
63    #[serde(default)]
64    pub has_more: bool,
65}
66
67/// Represents a conversation item.
68///
69/// Items can be messages, tool calls, tool outputs, reasoning, or other types
70/// that form the conversation history.
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct ConversationItem {
73    /// The unique ID of the item
74    pub id: String,
75    /// Object type (may not be present in API responses)
76    #[serde(default)]
77    pub object: Option<String>,
78    /// The type of item (e.g., "message", "tool_call", "tool_output")
79    #[serde(rename = "type")]
80    pub item_type: String,
81    /// The role of the item (e.g., "user", "assistant", "system")
82    #[serde(default)]
83    pub role: Option<String>,
84    /// The content of the item (structure varies by type)
85    #[serde(default)]
86    pub content: Option<serde_json::Value>,
87    /// The status of the item
88    #[serde(default)]
89    pub status: Option<String>,
90}
91
92/// Response structure for listing conversation items.
93///
94/// Contains a list of conversation item objects with pagination information.
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct ConversationItemListResponse {
97    /// Object type, always "list"
98    pub object: String,
99    /// Array of conversation item objects
100    pub data: Vec<ConversationItem>,
101    /// The ID of the first item in the list
102    #[serde(default)]
103    pub first_id: Option<String>,
104    /// The ID of the last item in the list
105    #[serde(default)]
106    pub last_id: Option<String>,
107    /// Whether there are more items available
108    #[serde(default)]
109    pub has_more: bool,
110}
111
112/// Response structure for conversation deletion.
113///
114/// Returned when a conversation is successfully deleted.
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct DeleteConversationResponse {
117    /// The conversation ID that was deleted
118    pub id: String,
119    /// Object type, always "conversation.deleted"
120    pub object: String,
121    /// Whether the conversation was successfully deleted
122    pub deleted: bool,
123}
124
125/// Input item for creating conversation items.
126///
127/// Used when adding new items to a conversation.
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct InputItem {
130    /// The type of item (e.g., "message")
131    #[serde(rename = "type")]
132    pub item_type: String,
133    /// The role of the item (e.g., "user", "assistant")
134    #[serde(default, skip_serializing_if = "Option::is_none")]
135    pub role: Option<String>,
136    /// The content of the item (can be a string or structured content)
137    #[serde(default, skip_serializing_if = "Option::is_none")]
138    pub content: Option<serde_json::Value>,
139}
140
141impl InputItem {
142    /// Creates a new message item.
143    ///
144    /// # Arguments
145    ///
146    /// * `role` - The role of the message (e.g., "user", "assistant")
147    /// * `content` - The text content of the message
148    ///
149    /// # Example
150    ///
151    /// ```rust
152    /// use openai_tools::conversations::response::InputItem;
153    ///
154    /// let item = InputItem::message("user", "Hello, how are you?");
155    /// assert_eq!(item.item_type, "message");
156    /// assert_eq!(item.role, Some("user".to_string()));
157    /// ```
158    pub fn message(role: &str, content: &str) -> Self {
159        Self { item_type: "message".to_string(), role: Some(role.to_string()), content: Some(serde_json::Value::String(content.to_string())) }
160    }
161
162    /// Creates a new user message item.
163    ///
164    /// # Arguments
165    ///
166    /// * `content` - The text content of the message
167    ///
168    /// # Example
169    ///
170    /// ```rust
171    /// use openai_tools::conversations::response::InputItem;
172    ///
173    /// let item = InputItem::user_message("Hello!");
174    /// assert_eq!(item.role, Some("user".to_string()));
175    /// ```
176    pub fn user_message(content: &str) -> Self {
177        Self::message("user", content)
178    }
179
180    /// Creates a new assistant message item.
181    ///
182    /// # Arguments
183    ///
184    /// * `content` - The text content of the message
185    ///
186    /// # Example
187    ///
188    /// ```rust
189    /// use openai_tools::conversations::response::InputItem;
190    ///
191    /// let item = InputItem::assistant_message("Hi there!");
192    /// assert_eq!(item.role, Some("assistant".to_string()));
193    /// ```
194    pub fn assistant_message(content: &str) -> Self {
195        Self::message("assistant", content)
196    }
197}