Skip to main content

vtcode_llm/open_responses/
request.rs

1//! Request object for Open Responses.
2//!
3//! The Request is the top-level object sent to the API,
4//! containing input items, tool definitions, and model parameters.
5
6use serde::{Deserialize, Deserializer, Serialize};
7use serde_json::Value;
8
9use super::{MessageRole, OutputItem};
10use crate::provider::ToolDefinition;
11
12/// The main request object per the Open Responses specification.
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
14pub struct Request {
15    /// The model to use for the request.
16    model: String,
17
18    /// The input items that form the context for the model.
19    /// Per the spec, these are polymorphic items (messages, tool outputs, etc.).
20    input: Vec<OutputItem>,
21
22    /// Tools available to the model.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub(crate) tools: Option<Vec<ToolDefinition>>,
25
26    /// Tool choice parameter.
27    #[serde(skip_serializing_if = "Option::is_none")]
28    tool_choice: Option<ToolChoice>,
29
30    /// Whether to stream the response.
31    #[serde(default)]
32    pub stream: bool,
33
34    /// Sampling temperature.
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub temperature: Option<f64>,
37
38    /// Nucleus sampling parameter.
39    #[serde(skip_serializing_if = "Option::is_none")]
40    top_p: Option<f64>,
41
42    /// Truncation configuration.
43    #[serde(skip_serializing_if = "Option::is_none")]
44    truncation: Option<Box<TruncationConfig>>,
45
46    /// Maximum output tokens allowed.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub(crate) max_output_tokens: Option<u64>,
49
50    /// Maximum tool calls allowed in a single request.
51    #[serde(skip_serializing_if = "Option::is_none")]
52    max_tool_calls: Option<u64>,
53
54    /// Stop sequences for the model.
55    #[serde(skip_serializing_if = "Option::is_none")]
56    stop: Option<Vec<String>>,
57
58    /// Presence penalty.
59    #[serde(skip_serializing_if = "Option::is_none")]
60    presence_penalty: Option<f64>,
61
62    /// Frequency penalty.
63    #[serde(skip_serializing_if = "Option::is_none")]
64    frequency_penalty: Option<f64>,
65
66    /// Logit bias for token sampling.
67    #[serde(skip_serializing_if = "Option::is_none")]
68    logit_bias: Option<hashbrown::HashMap<String, f64>>,
69
70    /// Whether to return log probabilities.
71    #[serde(skip_serializing_if = "Option::is_none")]
72    logprobs: Option<bool>,
73
74    /// Number of top log probabilities to return.
75    #[serde(skip_serializing_if = "Option::is_none")]
76    top_logprobs: Option<u32>,
77
78    /// User ID for tracking and rate limiting.
79    #[serde(skip_serializing_if = "Option::is_none")]
80    user: Option<String>,
81
82    /// Service tier requested.
83    #[serde(skip_serializing_if = "Option::is_none")]
84    service_tier: Option<String>,
85
86    /// Reasoning configuration.
87    #[serde(
88        default,
89        skip_serializing_if = "Option::is_none",
90        deserialize_with = "deserialize_boxed_reasoning_config_opt"
91    )]
92    reasoning: Option<Box<ReasoningConfig>>,
93
94    /// Whether to store the request/response.
95    #[serde(skip_serializing_if = "Option::is_none")]
96    pub(crate) store: Option<bool>,
97
98    /// Optional ID of the previous response for server-side continuity.
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub(crate) previous_response_id: Option<String>,
101
102    /// Optional response fields to include.
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub(crate) include: Option<Vec<String>>,
105
106    /// Metadata for the request.
107    #[serde(skip_serializing_if = "Option::is_none")]
108    metadata: Option<Value>,
109}
110
111/// Reasoning configuration for the request.
112#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
113pub struct ReasoningConfig {
114    /// Reasoning effort level (low, medium, high).
115    #[serde(skip_serializing_if = "Option::is_none")]
116    pub effort: Option<String>,
117}
118
119impl ReasoningConfig {
120    fn is_empty(&self) -> bool {
121        self.effort.is_none()
122    }
123
124    fn into_boxed_if_non_empty(self) -> Option<Box<Self>> {
125        (!self.is_empty()).then_some(Box::new(self))
126    }
127}
128
129/// Truncation configuration for the request.
130#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
131pub struct TruncationConfig {
132    /// The truncation strategy to use.
133    pub strategy: String,
134    /// The number of tokens to keep.
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub max_prompt_tokens: Option<u64>,
137}
138
139impl Request {
140    /// Creates a new request with the given model and input.
141    pub(crate) fn new(model: impl Into<String>, input: Vec<OutputItem>) -> Self {
142        Self {
143            model: model.into(),
144            input,
145            tools: None,
146            tool_choice: None,
147            stream: false,
148            temperature: None,
149            top_p: None,
150            truncation: None,
151            max_output_tokens: None,
152            max_tool_calls: None,
153            stop: None,
154            presence_penalty: None,
155            frequency_penalty: None,
156            logit_bias: None,
157            logprobs: None,
158            top_logprobs: None,
159            user: None,
160            service_tier: None,
161            reasoning: None,
162            store: None,
163            previous_response_id: None,
164            include: None,
165            metadata: None,
166        }
167    }
168
169    /// Convenience method to create a request from a single user message.
170    pub fn from_message(model: impl Into<String>, text: impl Into<String>) -> Self {
171        let item =
172            OutputItem::completed_message("msg_init", MessageRole::User, vec![super::ContentPart::input_text(text)]);
173        Self::new(model, vec![item])
174    }
175}
176
177fn deserialize_boxed_reasoning_config_opt<'de, D>(deserializer: D) -> Result<Option<Box<ReasoningConfig>>, D::Error>
178where
179    D: Deserializer<'de>,
180{
181    Option::<ReasoningConfig>::deserialize(deserializer)
182        .map(|value| value.and_then(ReasoningConfig::into_boxed_if_non_empty))
183}
184
185/// Tool choice options.
186#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
187#[serde(untagged)]
188pub enum ToolChoice {
189    /// Standard tool choice mode (auto, none, required).
190    Mode(ToolChoiceMode),
191    /// Specific tool to call.
192    Tool(SpecificToolChoice),
193}
194
195/// Standard tool choice modes.
196#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
197#[serde(rename_all = "lowercase")]
198pub enum ToolChoiceMode {
199    /// Model decides whether to call a tool.
200    Auto,
201    /// Model MUST NOT call any tools.
202    None,
203    /// Model MUST call at least one tool.
204    Required,
205}
206
207/// Specific tool choice.
208#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
209pub struct SpecificToolChoice {
210    /// The type of the tool, always "function".
211    #[serde(rename = "type")]
212    tool_type: String,
213    /// The name of the function to call.
214    function: FunctionName,
215}
216
217/// Function name wrapper for tool choice.
218#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
219pub struct FunctionName {
220    /// Name of the function.
221    pub name: String,
222}
223
224#[cfg(test)]
225mod tests {
226    use super::*;
227
228    #[test]
229    fn test_request_serialization() {
230        let req = Request::from_message("gpt-5", "Hello");
231        let json = serde_json::to_string(&req).unwrap();
232        assert!(json.contains("\"model\":\"gpt-5\""));
233        assert!(json.contains("\"input\":["));
234        assert!(json.contains("\"type\":\"message\""));
235    }
236
237    #[test]
238    fn empty_reasoning_config_deserializes_to_none() {
239        let req: Request = serde_json::from_str(
240            r#"{
241                "model": "gpt-5",
242                "input": [],
243                "reasoning": {}
244            }"#,
245        )
246        .unwrap();
247
248        assert!(req.reasoning.is_none());
249    }
250
251    #[test]
252    fn boxed_reasoning_config_is_smaller_than_inline_option() {
253        use std::mem::size_of;
254
255        assert!(size_of::<Option<Box<ReasoningConfig>>>() < size_of::<Option<ReasoningConfig>>());
256    }
257}