-
Notifications
You must be signed in to change notification settings - Fork 14.3k
standalone websearch extension #23823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bdaf22e
[codex] Add standalone web search extension
sayan-oai 6caca4a
[codex] Preserve standalone search settings
sayan-oai b473489
Refine standalone web search gating
sayan-oai 807fe7a
rm ThreadStore injection now that ToolCall has ConversationHistory
sayan-oai 4a85c0b
reorganize logic + integration test
sayan-oai ad5959c
Refine standalone web search history handling
sayan-oai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| use std::path::Path; | ||
| use std::time::Duration; | ||
|
|
||
| use anyhow::Context; | ||
| use anyhow::Result; | ||
| use app_test_support::ChatGptAuthFixture; | ||
| use app_test_support::McpProcess; | ||
| use app_test_support::to_response; | ||
| use app_test_support::write_chatgpt_auth; | ||
| use codex_app_server_protocol::JSONRPCResponse; | ||
| use codex_app_server_protocol::RequestId; | ||
| use codex_app_server_protocol::ThreadStartParams; | ||
| use codex_app_server_protocol::ThreadStartResponse; | ||
| use codex_app_server_protocol::TurnStartParams; | ||
| use codex_app_server_protocol::TurnStartResponse; | ||
| use codex_app_server_protocol::UserInput as V2UserInput; | ||
| use codex_config::types::AuthCredentialsStoreMode; | ||
| use core_test_support::responses; | ||
| use pretty_assertions::assert_eq; | ||
| use serde_json::Value; | ||
| use serde_json::json; | ||
| use tempfile::TempDir; | ||
| use tokio::time::timeout; | ||
| use wiremock::Mock; | ||
| use wiremock::MockServer; | ||
| use wiremock::ResponseTemplate; | ||
| use wiremock::matchers::method; | ||
| use wiremock::matchers::path; | ||
|
|
||
| // macOS and Windows Bazel CI can spend tens of seconds starting app-server | ||
| // subprocesses or processing test RPCs under load. | ||
| #[cfg(any(target_os = "macos", windows))] | ||
| const DEFAULT_READ_TIMEOUT: Duration = Duration::from_secs(60); | ||
| #[cfg(not(any(target_os = "macos", windows)))] | ||
| const DEFAULT_READ_TIMEOUT: Duration = Duration::from_secs(10); | ||
|
|
||
| #[tokio::test] | ||
| async fn standalone_web_search_round_trips_encrypted_output() -> Result<()> { | ||
| let call_id = "web-run-1"; | ||
| let server = responses::start_mock_server().await; | ||
| mount_search_response(&server).await; | ||
|
|
||
| let response_mock = responses::mount_sse_sequence( | ||
| &server, | ||
| vec![ | ||
| responses::sse(vec![ | ||
| responses::ev_response_created("resp-1"), | ||
| responses::ev_function_call_with_namespace( | ||
| call_id, | ||
| "web", | ||
| "run", | ||
| &json!({ | ||
| "search_query": [{"q": "standalone web search"}], | ||
| }) | ||
| .to_string(), | ||
| ), | ||
| responses::ev_completed("resp-1"), | ||
| ]), | ||
| responses::sse(vec![ | ||
| responses::ev_assistant_message("msg-1", "Done"), | ||
| responses::ev_completed("resp-2"), | ||
| ]), | ||
| ], | ||
| ) | ||
| .await; | ||
|
|
||
| let codex_home = TempDir::new()?; | ||
| create_config_toml(codex_home.path(), &server.uri())?; | ||
| write_chatgpt_auth( | ||
| codex_home.path(), | ||
| ChatGptAuthFixture::new("access-chatgpt"), | ||
| AuthCredentialsStoreMode::File, | ||
| )?; | ||
|
|
||
| let mut mcp = McpProcess::new_with_env(codex_home.path(), &[("OPENAI_API_KEY", None)]).await?; | ||
| timeout(DEFAULT_READ_TIMEOUT, mcp.initialize()).await??; | ||
|
|
||
| let thread_req = mcp | ||
| .send_thread_start_request(ThreadStartParams::default()) | ||
| .await?; | ||
| let thread_resp: JSONRPCResponse = timeout( | ||
| DEFAULT_READ_TIMEOUT, | ||
| mcp.read_stream_until_response_message(RequestId::Integer(thread_req)), | ||
| ) | ||
| .await??; | ||
| let ThreadStartResponse { thread, .. } = to_response::<ThreadStartResponse>(thread_resp)?; | ||
|
|
||
| let turn_req = mcp | ||
| .send_turn_start_request(TurnStartParams { | ||
| thread_id: thread.id, | ||
| input: vec![V2UserInput::Text { | ||
| text: "Search the web".to_string(), | ||
| text_elements: Vec::new(), | ||
| }], | ||
| ..Default::default() | ||
| }) | ||
| .await?; | ||
| let turn_resp: JSONRPCResponse = timeout( | ||
| DEFAULT_READ_TIMEOUT, | ||
| mcp.read_stream_until_response_message(RequestId::Integer(turn_req)), | ||
| ) | ||
| .await??; | ||
| let _turn: TurnStartResponse = to_response::<TurnStartResponse>(turn_resp)?; | ||
|
|
||
| timeout( | ||
| DEFAULT_READ_TIMEOUT, | ||
| mcp.read_stream_until_notification_message("turn/completed"), | ||
| ) | ||
| .await??; | ||
|
|
||
| let requests = response_mock.requests(); | ||
| assert_eq!(requests.len(), 2); | ||
|
|
||
| let first_response = requests[0].body_json(); | ||
| assert!( | ||
| requests[0].tool_by_name("web", "run").is_some(), | ||
| "web.run should be sent to the model" | ||
| ); | ||
| assert!( | ||
| !has_hosted_web_search(&first_response), | ||
| "standalone web search should replace hosted web search" | ||
| ); | ||
|
|
||
| let search_body = search_request_body(&server).await?; | ||
| assert_eq!( | ||
| search_body["commands"], | ||
| json!({ | ||
| "search_query": [{"q": "standalone web search"}], | ||
| }) | ||
| ); | ||
| assert_eq!( | ||
| search_body["input"] | ||
| .as_array() | ||
| .context("search input should be an array")? | ||
| .last(), | ||
| Some(&json!({ | ||
| "type": "message", | ||
| "role": "user", | ||
| "content": [{"type": "input_text", "text": "Search the web"}], | ||
| })) | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| requests[1].function_call_output(call_id), | ||
| json!({ | ||
| "type": "function_call_output", | ||
| "call_id": call_id, | ||
| "output": [{ | ||
| "type": "encrypted_content", | ||
| "encrypted_content": "ciphertext", | ||
| }], | ||
| }) | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| async fn mount_search_response(server: &MockServer) { | ||
| Mock::given(method("POST")) | ||
| .and(path("/api/codex/alpha/search")) | ||
| .respond_with(ResponseTemplate::new(200).set_body_json(json!({ | ||
| "encrypted_output": "ciphertext", | ||
| }))) | ||
| .expect(1) | ||
| .mount(server) | ||
| .await; | ||
| } | ||
|
|
||
| fn has_hosted_web_search(body: &Value) -> bool { | ||
| body.get("tools") | ||
| .and_then(Value::as_array) | ||
| .is_some_and(|tools| { | ||
| tools | ||
| .iter() | ||
| .any(|tool| tool.get("type").and_then(Value::as_str) == Some("web_search")) | ||
| }) | ||
| } | ||
|
|
||
| async fn search_request_body(server: &MockServer) -> Result<Value> { | ||
| server | ||
| .received_requests() | ||
| .await | ||
| .context("failed to fetch received requests")? | ||
| .into_iter() | ||
| .find(|request| request.url.path() == "/api/codex/alpha/search") | ||
| .context("expected standalone search request")? | ||
| .body_json() | ||
| .context("search request body should be JSON") | ||
| } | ||
|
|
||
| fn create_config_toml(codex_home: &Path, server_uri: &str) -> std::io::Result<()> { | ||
| std::fs::write( | ||
| codex_home.join("config.toml"), | ||
| format!( | ||
| r#" | ||
| model = "mock-model" | ||
| approval_policy = "never" | ||
| sandbox_mode = "read-only" | ||
| model_provider = "openai-custom" | ||
| chatgpt_base_url = "{server_uri}" | ||
|
|
||
| [features] | ||
| standalone_web_search = true | ||
|
|
||
| [model_providers.openai-custom] | ||
| name = "OpenAI" | ||
| base_url = "{server_uri}/api/codex" | ||
| wire_api = "responses" | ||
| request_max_retries = 0 | ||
| stream_max_retries = 0 | ||
| supports_websockets = false | ||
| requires_openai_auth = true | ||
| "# | ||
| ), | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.