Skip to main content

agent_runtime_http_api/
openai.rs

1//! OpenAI Responses API wire types for the compatibility adapter.
2
3use std::collections::BTreeMap;
4
5use runtime_types::{ConversationId, RuntimeInstanceId, WorkspaceId};
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8
9#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
10pub struct OpenAiResponseRequest {
11    pub model: String,
12    pub input: Value,
13    #[serde(default)]
14    pub instructions: Option<Value>,
15    #[serde(default)]
16    pub max_output_tokens: Option<u32>,
17    #[serde(default)]
18    pub background: bool,
19    #[serde(default)]
20    pub stream: bool,
21    #[serde(default)]
22    pub metadata: BTreeMap<String, String>,
23    #[serde(default)]
24    pub tools: Vec<Value>,
25    #[serde(default)]
26    pub tool_choice: Option<Value>,
27    #[serde(default)]
28    pub previous_response_id: Option<String>,
29    #[serde(default)]
30    pub conversation: Option<Value>,
31    #[serde(default)]
32    pub reasoning: Option<Value>,
33    #[serde(default)]
34    pub temperature: Option<f64>,
35    #[serde(default)]
36    pub top_p: Option<f64>,
37    #[serde(default)]
38    pub store: Option<bool>,
39    #[serde(default)]
40    pub include: Vec<String>,
41    #[serde(default)]
42    pub text: Option<Value>,
43    #[serde(default)]
44    pub truncation: Option<String>,
45    #[serde(default)]
46    pub safety_identifier: Option<String>,
47    #[serde(default)]
48    pub prompt_cache_key: Option<String>,
49    /// Runtime extension. By default `model` is the Runtime ID. Supplying
50    /// `runtimeId` lets an API gateway expose a different public model alias.
51    #[serde(default)]
52    pub agent: Option<OpenAiAgentOptions>,
53    /// Captures newly added provider fields so the adapter can reject them
54    /// explicitly instead of silently discarding semantics it cannot preserve.
55    #[serde(default, flatten)]
56    pub extra: BTreeMap<String, Value>,
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
60#[serde(rename_all = "camelCase", deny_unknown_fields)]
61pub struct OpenAiAgentOptions {
62    #[serde(default)]
63    pub runtime_id: Option<RuntimeInstanceId>,
64    #[serde(default)]
65    pub workspace_id: Option<WorkspaceId>,
66    #[serde(default)]
67    pub conversation_id: Option<ConversationId>,
68    #[serde(default)]
69    pub downstream_model: Option<String>,
70    #[serde(default)]
71    pub deadline_seconds: Option<u64>,
72    #[serde(default)]
73    pub max_model_turns: Option<usize>,
74    #[serde(default)]
75    pub max_tool_calls: Option<usize>,
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
79pub struct OpenAiResponse {
80    pub id: String,
81    pub object: String,
82    pub created_at: i64,
83    pub status: String,
84    pub completed_at: Option<i64>,
85    pub error: Option<OpenAiResponseError>,
86    pub incomplete_details: Option<Value>,
87    pub instructions: Option<Value>,
88    pub max_output_tokens: Option<u32>,
89    pub model: String,
90    pub output: Vec<OpenAiOutputItem>,
91    pub parallel_tool_calls: bool,
92    pub previous_response_id: Option<String>,
93    pub tool_choice: Value,
94    pub tools: Vec<Value>,
95    pub metadata: BTreeMap<String, String>,
96    pub usage: Option<OpenAiUsage>,
97}
98
99#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
100pub struct OpenAiOutputItem {
101    pub id: String,
102    #[serde(rename = "type")]
103    pub item_type: String,
104    pub status: String,
105    pub role: String,
106    pub content: Vec<OpenAiOutputContent>,
107}
108
109#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
110pub struct OpenAiOutputContent {
111    #[serde(rename = "type")]
112    pub content_type: String,
113    pub text: String,
114    pub annotations: Vec<Value>,
115}
116
117#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
118pub struct OpenAiUsage {
119    pub input_tokens: u64,
120    pub input_tokens_details: OpenAiInputTokensDetails,
121    pub output_tokens: u64,
122    pub output_tokens_details: OpenAiOutputTokensDetails,
123    pub total_tokens: u64,
124}
125
126#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
127pub struct OpenAiInputTokensDetails {
128    pub cached_tokens: u64,
129}
130
131#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
132pub struct OpenAiOutputTokensDetails {
133    pub reasoning_tokens: u64,
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
137pub struct OpenAiResponseError {
138    pub code: String,
139    pub message: String,
140}
141
142#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
143pub struct OpenAiErrorEnvelope {
144    pub error: OpenAiError,
145}
146
147#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
148pub struct OpenAiError {
149    pub message: String,
150    #[serde(rename = "type")]
151    pub error_type: String,
152    pub param: Option<String>,
153    pub code: Option<String>,
154}