|
| 1 | +use anyhow::Result; |
| 2 | +use app_test_support::McpProcess; |
| 3 | +use app_test_support::to_response; |
| 4 | +use codex_app_server_protocol::AddConversationListenerParams; |
| 5 | +use codex_app_server_protocol::InputItem; |
| 6 | +use codex_app_server_protocol::JSONRPCResponse; |
| 7 | +use codex_app_server_protocol::NewConversationParams; |
| 8 | +use codex_app_server_protocol::NewConversationResponse; |
| 9 | +use codex_app_server_protocol::RequestId; |
| 10 | +use codex_app_server_protocol::SendUserTurnParams; |
| 11 | +use codex_app_server_protocol::SendUserTurnResponse; |
| 12 | +use codex_core::protocol::AskForApproval; |
| 13 | +use codex_core::protocol::SandboxPolicy; |
| 14 | +use codex_protocol::config_types::ReasoningSummary; |
| 15 | +use codex_protocol::openai_models::ReasoningEffort; |
| 16 | +use core_test_support::responses; |
| 17 | +use core_test_support::skip_if_no_network; |
| 18 | +use pretty_assertions::assert_eq; |
| 19 | +use std::path::Path; |
| 20 | +use tempfile::TempDir; |
| 21 | +use tokio::time::timeout; |
| 22 | + |
| 23 | +const DEFAULT_READ_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); |
| 24 | + |
| 25 | +#[tokio::test] |
| 26 | +async fn send_user_turn_accepts_output_schema_v1() -> Result<()> { |
| 27 | + skip_if_no_network!(Ok(())); |
| 28 | + |
| 29 | + let server = responses::start_mock_server().await; |
| 30 | + let body = responses::sse(vec![ |
| 31 | + responses::ev_response_created("resp-1"), |
| 32 | + responses::ev_assistant_message("msg-1", "Done"), |
| 33 | + responses::ev_completed("resp-1"), |
| 34 | + ]); |
| 35 | + let response_mock = responses::mount_sse_once(&server, body).await; |
| 36 | + |
| 37 | + let codex_home = TempDir::new()?; |
| 38 | + create_config_toml(codex_home.path(), &server.uri())?; |
| 39 | + |
| 40 | + let mut mcp = McpProcess::new(codex_home.path()).await?; |
| 41 | + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; |
| 42 | + |
| 43 | + let new_conv_id = mcp |
| 44 | + .send_new_conversation_request(NewConversationParams { |
| 45 | + ..Default::default() |
| 46 | + }) |
| 47 | + .await?; |
| 48 | + let new_conv_resp: JSONRPCResponse = timeout( |
| 49 | + DEFAULT_READ_TIMEOUT, |
| 50 | + mcp.read_stream_until_response_message(RequestId::Integer(new_conv_id)), |
| 51 | + ) |
| 52 | + .await??; |
| 53 | + let NewConversationResponse { |
| 54 | + conversation_id, .. |
| 55 | + } = to_response::<NewConversationResponse>(new_conv_resp)?; |
| 56 | + |
| 57 | + let listener_id = mcp |
| 58 | + .send_add_conversation_listener_request(AddConversationListenerParams { |
| 59 | + conversation_id, |
| 60 | + experimental_raw_events: false, |
| 61 | + }) |
| 62 | + .await?; |
| 63 | + timeout( |
| 64 | + DEFAULT_READ_TIMEOUT, |
| 65 | + mcp.read_stream_until_response_message(RequestId::Integer(listener_id)), |
| 66 | + ) |
| 67 | + .await??; |
| 68 | + |
| 69 | + let output_schema = serde_json::json!({ |
| 70 | + "type": "object", |
| 71 | + "properties": { |
| 72 | + "answer": { "type": "string" } |
| 73 | + }, |
| 74 | + "required": ["answer"], |
| 75 | + "additionalProperties": false |
| 76 | + }); |
| 77 | + |
| 78 | + let send_turn_id = mcp |
| 79 | + .send_send_user_turn_request(SendUserTurnParams { |
| 80 | + conversation_id, |
| 81 | + items: vec![InputItem::Text { |
| 82 | + text: "Hello".to_string(), |
| 83 | + }], |
| 84 | + cwd: codex_home.path().to_path_buf(), |
| 85 | + approval_policy: AskForApproval::Never, |
| 86 | + sandbox_policy: SandboxPolicy::new_read_only_policy(), |
| 87 | + model: "mock-model".to_string(), |
| 88 | + effort: Some(ReasoningEffort::Medium), |
| 89 | + summary: ReasoningSummary::Auto, |
| 90 | + output_schema: Some(output_schema.clone()), |
| 91 | + }) |
| 92 | + .await?; |
| 93 | + let _send_turn_resp: SendUserTurnResponse = to_response::<SendUserTurnResponse>( |
| 94 | + timeout( |
| 95 | + DEFAULT_READ_TIMEOUT, |
| 96 | + mcp.read_stream_until_response_message(RequestId::Integer(send_turn_id)), |
| 97 | + ) |
| 98 | + .await??, |
| 99 | + )?; |
| 100 | + |
| 101 | + timeout( |
| 102 | + DEFAULT_READ_TIMEOUT, |
| 103 | + mcp.read_stream_until_notification_message("codex/event/task_complete"), |
| 104 | + ) |
| 105 | + .await??; |
| 106 | + |
| 107 | + let request = response_mock.single_request(); |
| 108 | + let payload = request.body_json(); |
| 109 | + let text = payload.get("text").expect("request missing text field"); |
| 110 | + let format = text |
| 111 | + .get("format") |
| 112 | + .expect("request missing text.format field"); |
| 113 | + assert_eq!( |
| 114 | + format, |
| 115 | + &serde_json::json!({ |
| 116 | + "name": "codex_output_schema", |
| 117 | + "type": "json_schema", |
| 118 | + "strict": true, |
| 119 | + "schema": output_schema, |
| 120 | + }) |
| 121 | + ); |
| 122 | + |
| 123 | + Ok(()) |
| 124 | +} |
| 125 | + |
| 126 | +#[tokio::test] |
| 127 | +async fn send_user_turn_output_schema_is_per_turn_v1() -> Result<()> { |
| 128 | + skip_if_no_network!(Ok(())); |
| 129 | + |
| 130 | + let server = responses::start_mock_server().await; |
| 131 | + let body1 = responses::sse(vec![ |
| 132 | + responses::ev_response_created("resp-1"), |
| 133 | + responses::ev_assistant_message("msg-1", "Done"), |
| 134 | + responses::ev_completed("resp-1"), |
| 135 | + ]); |
| 136 | + let response_mock1 = responses::mount_sse_once(&server, body1).await; |
| 137 | + |
| 138 | + let codex_home = TempDir::new()?; |
| 139 | + create_config_toml(codex_home.path(), &server.uri())?; |
| 140 | + |
| 141 | + let mut mcp = McpProcess::new(codex_home.path()).await?; |
| 142 | + timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; |
| 143 | + |
| 144 | + let new_conv_id = mcp |
| 145 | + .send_new_conversation_request(NewConversationParams { |
| 146 | + ..Default::default() |
| 147 | + }) |
| 148 | + .await?; |
| 149 | + let new_conv_resp: JSONRPCResponse = timeout( |
| 150 | + DEFAULT_READ_TIMEOUT, |
| 151 | + mcp.read_stream_until_response_message(RequestId::Integer(new_conv_id)), |
| 152 | + ) |
| 153 | + .await??; |
| 154 | + let NewConversationResponse { |
| 155 | + conversation_id, .. |
| 156 | + } = to_response::<NewConversationResponse>(new_conv_resp)?; |
| 157 | + |
| 158 | + let listener_id = mcp |
| 159 | + .send_add_conversation_listener_request(AddConversationListenerParams { |
| 160 | + conversation_id, |
| 161 | + experimental_raw_events: false, |
| 162 | + }) |
| 163 | + .await?; |
| 164 | + timeout( |
| 165 | + DEFAULT_READ_TIMEOUT, |
| 166 | + mcp.read_stream_until_response_message(RequestId::Integer(listener_id)), |
| 167 | + ) |
| 168 | + .await??; |
| 169 | + |
| 170 | + let output_schema = serde_json::json!({ |
| 171 | + "type": "object", |
| 172 | + "properties": { |
| 173 | + "answer": { "type": "string" } |
| 174 | + }, |
| 175 | + "required": ["answer"], |
| 176 | + "additionalProperties": false |
| 177 | + }); |
| 178 | + |
| 179 | + let send_turn_id = mcp |
| 180 | + .send_send_user_turn_request(SendUserTurnParams { |
| 181 | + conversation_id, |
| 182 | + items: vec![InputItem::Text { |
| 183 | + text: "Hello".to_string(), |
| 184 | + }], |
| 185 | + cwd: codex_home.path().to_path_buf(), |
| 186 | + approval_policy: AskForApproval::Never, |
| 187 | + sandbox_policy: SandboxPolicy::new_read_only_policy(), |
| 188 | + model: "mock-model".to_string(), |
| 189 | + effort: Some(ReasoningEffort::Medium), |
| 190 | + summary: ReasoningSummary::Auto, |
| 191 | + output_schema: Some(output_schema.clone()), |
| 192 | + }) |
| 193 | + .await?; |
| 194 | + let _send_turn_resp: SendUserTurnResponse = to_response::<SendUserTurnResponse>( |
| 195 | + timeout( |
| 196 | + DEFAULT_READ_TIMEOUT, |
| 197 | + mcp.read_stream_until_response_message(RequestId::Integer(send_turn_id)), |
| 198 | + ) |
| 199 | + .await??, |
| 200 | + )?; |
| 201 | + |
| 202 | + timeout( |
| 203 | + DEFAULT_READ_TIMEOUT, |
| 204 | + mcp.read_stream_until_notification_message("codex/event/task_complete"), |
| 205 | + ) |
| 206 | + .await??; |
| 207 | + |
| 208 | + let payload1 = response_mock1.single_request().body_json(); |
| 209 | + assert_eq!( |
| 210 | + payload1.pointer("/text/format"), |
| 211 | + Some(&serde_json::json!({ |
| 212 | + "name": "codex_output_schema", |
| 213 | + "type": "json_schema", |
| 214 | + "strict": true, |
| 215 | + "schema": output_schema, |
| 216 | + })) |
| 217 | + ); |
| 218 | + |
| 219 | + let body2 = responses::sse(vec![ |
| 220 | + responses::ev_response_created("resp-2"), |
| 221 | + responses::ev_assistant_message("msg-2", "Done"), |
| 222 | + responses::ev_completed("resp-2"), |
| 223 | + ]); |
| 224 | + let response_mock2 = responses::mount_sse_once(&server, body2).await; |
| 225 | + |
| 226 | + let send_turn_id_2 = mcp |
| 227 | + .send_send_user_turn_request(SendUserTurnParams { |
| 228 | + conversation_id, |
| 229 | + items: vec![InputItem::Text { |
| 230 | + text: "Hello again".to_string(), |
| 231 | + }], |
| 232 | + cwd: codex_home.path().to_path_buf(), |
| 233 | + approval_policy: AskForApproval::Never, |
| 234 | + sandbox_policy: SandboxPolicy::new_read_only_policy(), |
| 235 | + model: "mock-model".to_string(), |
| 236 | + effort: Some(ReasoningEffort::Medium), |
| 237 | + summary: ReasoningSummary::Auto, |
| 238 | + output_schema: None, |
| 239 | + }) |
| 240 | + .await?; |
| 241 | + let _send_turn_resp_2: SendUserTurnResponse = to_response::<SendUserTurnResponse>( |
| 242 | + timeout( |
| 243 | + DEFAULT_READ_TIMEOUT, |
| 244 | + mcp.read_stream_until_response_message(RequestId::Integer(send_turn_id_2)), |
| 245 | + ) |
| 246 | + .await??, |
| 247 | + )?; |
| 248 | + |
| 249 | + timeout( |
| 250 | + DEFAULT_READ_TIMEOUT, |
| 251 | + mcp.read_stream_until_notification_message("codex/event/task_complete"), |
| 252 | + ) |
| 253 | + .await??; |
| 254 | + |
| 255 | + let payload2 = response_mock2.single_request().body_json(); |
| 256 | + assert_eq!(payload2.pointer("/text/format"), None); |
| 257 | + |
| 258 | + Ok(()) |
| 259 | +} |
| 260 | + |
| 261 | +fn create_config_toml(codex_home: &Path, server_uri: &str) -> std::io::Result<()> { |
| 262 | + let config_toml = codex_home.join("config.toml"); |
| 263 | + std::fs::write( |
| 264 | + config_toml, |
| 265 | + format!( |
| 266 | + r#" |
| 267 | +model = "mock-model" |
| 268 | +approval_policy = "never" |
| 269 | +sandbox_mode = "read-only" |
| 270 | +
|
| 271 | +model_provider = "mock_provider" |
| 272 | +
|
| 273 | +[model_providers.mock_provider] |
| 274 | +name = "Mock provider for test" |
| 275 | +base_url = "{server_uri}/v1" |
| 276 | +wire_api = "responses" |
| 277 | +request_max_retries = 0 |
| 278 | +stream_max_retries = 0 |
| 279 | +"# |
| 280 | + ), |
| 281 | + ) |
| 282 | +} |
0 commit comments