mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
initial
This commit is contained in:
@@ -36,6 +36,8 @@ use crate::client_common::Prompt;
|
||||
use crate::client_common::ResponseEvent;
|
||||
use crate::client_common::ResponseStream;
|
||||
use crate::client_common::ResponsesApiRequest;
|
||||
use crate::client_common::ToolChoiceMode;
|
||||
use crate::client_common::ToolChoicePayload;
|
||||
use crate::client_common::create_reasoning_param_for_request;
|
||||
use crate::client_common::create_text_param_for_request;
|
||||
use crate::config::Config;
|
||||
@@ -245,12 +247,33 @@ impl ModelClient {
|
||||
// For Azure, we send `store: true` and preserve reasoning item IDs.
|
||||
let azure_workaround = self.provider.is_azure_responses_endpoint();
|
||||
|
||||
let allowed_tool_values = prompt.allowed_tools.as_ref().map(|allowed_names| {
|
||||
use std::collections::HashSet;
|
||||
let allowed: HashSet<&str> = allowed_names.iter().map(String::as_str).collect();
|
||||
prompt
|
||||
.tools
|
||||
.iter()
|
||||
.filter(|spec| allowed.contains(spec.name()))
|
||||
.map(|spec| spec.to_allowed_tool_entry())
|
||||
.collect::<Vec<serde_json::Value>>()
|
||||
});
|
||||
|
||||
let tool_choice = match allowed_tool_values {
|
||||
Some(tools) if !tools.is_empty() => Some(ToolChoicePayload::AllowedTools {
|
||||
mode: ToolChoiceMode::Auto,
|
||||
tools,
|
||||
}),
|
||||
_ => Some(ToolChoicePayload::Auto),
|
||||
};
|
||||
|
||||
trace!(tool_choice = ?tool_choice, "resolved tool choice for request");
|
||||
|
||||
let payload = ResponsesApiRequest {
|
||||
model: &self.config.model,
|
||||
instructions: &full_instructions,
|
||||
input: &input_with_instructions,
|
||||
tools: &tools_json,
|
||||
tool_choice: "auto",
|
||||
tool_choice,
|
||||
parallel_tool_calls: prompt.parallel_tool_calls,
|
||||
reasoning,
|
||||
store: azure_workaround,
|
||||
|
||||
@@ -11,6 +11,7 @@ use codex_protocol::models::ResponseItem;
|
||||
use futures::Stream;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use serde::ser::Serializer;
|
||||
use serde_json::Value;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashSet;
|
||||
@@ -41,6 +42,9 @@ pub struct Prompt {
|
||||
|
||||
/// Optional the output schema for the model's response.
|
||||
pub output_schema: Option<Value>,
|
||||
|
||||
/// Optional list of tool identifiers that should remain enabled for this turn.
|
||||
pub allowed_tools: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
impl Prompt {
|
||||
@@ -268,7 +272,8 @@ pub(crate) struct ResponsesApiRequest<'a> {
|
||||
// separate enum for serialization.
|
||||
pub(crate) input: &'a Vec<ResponseItem>,
|
||||
pub(crate) tools: &'a [serde_json::Value],
|
||||
pub(crate) tool_choice: &'static str,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub(crate) tool_choice: Option<ToolChoicePayload>,
|
||||
pub(crate) parallel_tool_calls: bool,
|
||||
pub(crate) reasoning: Option<Reasoning>,
|
||||
pub(crate) store: bool,
|
||||
@@ -280,10 +285,54 @@ pub(crate) struct ResponsesApiRequest<'a> {
|
||||
pub(crate) text: Option<TextControls>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum ToolChoiceMode {
|
||||
Auto,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ToolChoicePayload {
|
||||
Auto,
|
||||
AllowedTools {
|
||||
mode: ToolChoiceMode,
|
||||
tools: Vec<serde_json::Value>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Serialize for ToolChoicePayload {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match self {
|
||||
ToolChoicePayload::Auto => serializer.serialize_str("auto"),
|
||||
ToolChoicePayload::AllowedTools { mode, tools } => {
|
||||
#[derive(Serialize)]
|
||||
struct AllowedToolsPayload<'a> {
|
||||
#[serde(rename = "type")]
|
||||
r#type: &'static str,
|
||||
mode: ToolChoiceMode,
|
||||
tools: &'a [serde_json::Value],
|
||||
}
|
||||
|
||||
let payload = AllowedToolsPayload {
|
||||
r#type: "allowed_tools",
|
||||
mode: *mode,
|
||||
tools: tools.as_slice(),
|
||||
};
|
||||
payload.serialize(serializer)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod tools {
|
||||
use crate::tools::spec::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
use serde_json::json;
|
||||
|
||||
/// When serialized as JSON, this produces a valid "Tool" in the OpenAI
|
||||
/// Responses API.
|
||||
@@ -311,6 +360,21 @@ pub(crate) mod tools {
|
||||
ToolSpec::Freeform(tool) => tool.name.as_str(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn to_allowed_tool_entry(&self) -> Value {
|
||||
match self {
|
||||
ToolSpec::Function(tool) => json!({
|
||||
"type": "function",
|
||||
"name": tool.name,
|
||||
}),
|
||||
ToolSpec::LocalShell {} => json!({ "type": "local_shell" }),
|
||||
ToolSpec::WebSearch {} => json!({ "type": "web_search" }),
|
||||
ToolSpec::Freeform(tool) => json!({
|
||||
"type": "custom",
|
||||
"name": tool.name,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
@@ -457,7 +521,7 @@ mod tests {
|
||||
instructions: "i",
|
||||
input: &input,
|
||||
tools: &tools,
|
||||
tool_choice: "auto",
|
||||
tool_choice: Some(ToolChoicePayload::Auto),
|
||||
parallel_tool_calls: true,
|
||||
reasoning: None,
|
||||
store: false,
|
||||
@@ -498,7 +562,7 @@ mod tests {
|
||||
instructions: "i",
|
||||
input: &input,
|
||||
tools: &tools,
|
||||
tool_choice: "auto",
|
||||
tool_choice: Some(ToolChoicePayload::Auto),
|
||||
parallel_tool_calls: true,
|
||||
reasoning: None,
|
||||
store: false,
|
||||
@@ -534,7 +598,7 @@ mod tests {
|
||||
instructions: "i",
|
||||
input: &input,
|
||||
tools: &tools,
|
||||
tool_choice: "auto",
|
||||
tool_choice: Some(ToolChoicePayload::Auto),
|
||||
parallel_tool_calls: true,
|
||||
reasoning: None,
|
||||
store: false,
|
||||
@@ -547,4 +611,65 @@ mod tests {
|
||||
let v = serde_json::to_value(&req).expect("json");
|
||||
assert!(v.get("text").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serializes_auto_tool_choice_as_string() {
|
||||
let input: Vec<ResponseItem> = vec![];
|
||||
let tools: Vec<serde_json::Value> = vec![];
|
||||
let req = ResponsesApiRequest {
|
||||
model: "gpt-5",
|
||||
instructions: "i",
|
||||
input: &input,
|
||||
tools: &tools,
|
||||
tool_choice: Some(ToolChoicePayload::Auto),
|
||||
parallel_tool_calls: true,
|
||||
reasoning: None,
|
||||
store: false,
|
||||
stream: true,
|
||||
include: vec![],
|
||||
prompt_cache_key: None,
|
||||
text: None,
|
||||
};
|
||||
|
||||
let v = serde_json::to_value(&req).expect("json");
|
||||
assert_eq!(
|
||||
v.get("tool_choice"),
|
||||
Some(&serde_json::Value::String("auto".to_string()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serializes_allowed_tools_choice() {
|
||||
let input: Vec<ResponseItem> = vec![];
|
||||
let tools: Vec<serde_json::Value> = vec![];
|
||||
let allowed_tools = vec![
|
||||
serde_json::json!({"type": "function", "name": "shell"}),
|
||||
serde_json::json!({"type": "local_shell"}),
|
||||
];
|
||||
let req = ResponsesApiRequest {
|
||||
model: "gpt-5",
|
||||
instructions: "i",
|
||||
input: &input,
|
||||
tools: &tools,
|
||||
tool_choice: Some(ToolChoicePayload::AllowedTools {
|
||||
mode: ToolChoiceMode::Auto,
|
||||
tools: allowed_tools.clone(),
|
||||
}),
|
||||
parallel_tool_calls: true,
|
||||
reasoning: None,
|
||||
store: false,
|
||||
stream: true,
|
||||
include: vec![],
|
||||
prompt_cache_key: None,
|
||||
text: None,
|
||||
};
|
||||
|
||||
let v = serde_json::to_value(&req).expect("json");
|
||||
let expected = serde_json::json!({
|
||||
"type": "allowed_tools",
|
||||
"mode": "auto",
|
||||
"tools": allowed_tools,
|
||||
});
|
||||
assert_eq!(v.get("tool_choice"), Some(&expected));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -348,6 +348,17 @@ impl SessionConfiguration {
|
||||
if let Some(cwd) = updates.cwd.clone() {
|
||||
next_configuration.cwd = cwd;
|
||||
}
|
||||
if let Some(web_search_request) = updates.web_search_request {
|
||||
if web_search_request {
|
||||
next_configuration
|
||||
.features
|
||||
.enable(crate::features::Feature::WebSearchRequest);
|
||||
} else {
|
||||
next_configuration
|
||||
.features
|
||||
.disable(crate::features::Feature::WebSearchRequest);
|
||||
}
|
||||
}
|
||||
next_configuration
|
||||
}
|
||||
}
|
||||
@@ -361,6 +372,7 @@ pub(crate) struct SessionSettingsUpdate {
|
||||
pub(crate) reasoning_effort: Option<Option<ReasoningEffortConfig>>,
|
||||
pub(crate) reasoning_summary: Option<ReasoningSummaryConfig>,
|
||||
pub(crate) final_output_json_schema: Option<Option<Value>>,
|
||||
pub(crate) web_search_request: Option<bool>,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
@@ -376,6 +388,7 @@ impl Session {
|
||||
let model_family = find_family_for_model(&session_configuration.model)
|
||||
.unwrap_or_else(|| config.model_family.clone());
|
||||
let mut per_turn_config = (*config).clone();
|
||||
let features = session_configuration.features.clone();
|
||||
per_turn_config.model = session_configuration.model.clone();
|
||||
per_turn_config.model_family = model_family.clone();
|
||||
per_turn_config.model_reasoning_effort = session_configuration.model_reasoning_effort;
|
||||
@@ -384,6 +397,16 @@ impl Session {
|
||||
per_turn_config.model_context_window = Some(model_info.context_window);
|
||||
}
|
||||
|
||||
per_turn_config.features = features.clone();
|
||||
per_turn_config.include_apply_patch_tool = features.enabled(Feature::ApplyPatchFreeform);
|
||||
per_turn_config.include_view_image_tool = features.enabled(Feature::ViewImageTool);
|
||||
per_turn_config.tools_web_search_request = features.enabled(Feature::WebSearchRequest);
|
||||
per_turn_config.experimental_sandbox_command_assessment =
|
||||
features.enabled(Feature::SandboxCommandAssessment);
|
||||
per_turn_config.use_experimental_streamable_shell_tool =
|
||||
features.enabled(Feature::StreamableShell);
|
||||
per_turn_config.use_experimental_unified_exec_tool = features.enabled(Feature::UnifiedExec);
|
||||
|
||||
let otel_event_manager = otel_event_manager.clone().with_model(
|
||||
session_configuration.model.as_str(),
|
||||
session_configuration.model.as_str(),
|
||||
@@ -401,7 +424,7 @@ impl Session {
|
||||
|
||||
let tools_config = ToolsConfig::new(&ToolsConfigParams {
|
||||
model_family: &model_family,
|
||||
features: &config.features,
|
||||
features: &features,
|
||||
});
|
||||
|
||||
TurnContext {
|
||||
@@ -1254,6 +1277,7 @@ async fn submission_loop(sess: Arc<Session>, config: Arc<Config>, rx_sub: Receiv
|
||||
model,
|
||||
effort,
|
||||
summary,
|
||||
include_web_search_request,
|
||||
} => {
|
||||
let updates = SessionSettingsUpdate {
|
||||
cwd,
|
||||
@@ -1262,6 +1286,7 @@ async fn submission_loop(sess: Arc<Session>, config: Arc<Config>, rx_sub: Receiv
|
||||
model,
|
||||
reasoning_effort: effort,
|
||||
reasoning_summary: summary,
|
||||
web_search_request: include_web_search_request,
|
||||
..Default::default()
|
||||
};
|
||||
sess.update_settings(updates).await;
|
||||
@@ -1288,6 +1313,7 @@ async fn submission_loop(sess: Arc<Session>, config: Arc<Config>, rx_sub: Receiv
|
||||
reasoning_effort: Some(effort),
|
||||
reasoning_summary: Some(summary),
|
||||
final_output_json_schema: Some(final_output_json_schema),
|
||||
web_search_request: None,
|
||||
},
|
||||
),
|
||||
Op::UserInput { items } => (items, SessionSettingsUpdate::default()),
|
||||
@@ -1848,12 +1874,17 @@ async fn run_turn(
|
||||
.get_model_family()
|
||||
.supports_parallel_tool_calls;
|
||||
let parallel_tool_calls = model_supports_parallel;
|
||||
let tool_specs = router.specs();
|
||||
let allowed_tools = turn_context
|
||||
.tools_config
|
||||
.allowed_tool_names(tool_specs.as_slice());
|
||||
let prompt = Prompt {
|
||||
input: filter_model_visible_history(input),
|
||||
tools: router.specs(),
|
||||
tools: tool_specs,
|
||||
parallel_tool_calls,
|
||||
base_instructions_override: turn_context.base_instructions.clone(),
|
||||
output_schema: turn_context.final_output_json_schema.clone(),
|
||||
allowed_tools,
|
||||
};
|
||||
|
||||
let mut retries = 0;
|
||||
|
||||
@@ -1780,6 +1780,48 @@ trust_level = "trusted"
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn web_search_override_keeps_config_and_features_in_sync() -> std::io::Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
let cfg = ConfigToml::default();
|
||||
|
||||
let enabled = Config::load_from_base_config_with_overrides(
|
||||
cfg.clone(),
|
||||
ConfigOverrides {
|
||||
tools_web_search_request: Some(true),
|
||||
..Default::default()
|
||||
},
|
||||
codex_home.path().to_path_buf(),
|
||||
)?;
|
||||
assert!(
|
||||
enabled.features.enabled(Feature::WebSearchRequest),
|
||||
"feature flag should be enabled when override is true"
|
||||
);
|
||||
assert!(
|
||||
enabled.tools_web_search_request,
|
||||
"config mirror flag should reflect enabled state"
|
||||
);
|
||||
|
||||
let disabled = Config::load_from_base_config_with_overrides(
|
||||
cfg,
|
||||
ConfigOverrides {
|
||||
tools_web_search_request: Some(false),
|
||||
..Default::default()
|
||||
},
|
||||
codex_home.path().to_path_buf(),
|
||||
)?;
|
||||
assert!(
|
||||
!disabled.features.enabled(Feature::WebSearchRequest),
|
||||
"feature flag should be disabled when override is false"
|
||||
);
|
||||
assert!(
|
||||
!disabled.tools_web_search_request,
|
||||
"config mirror flag should reflect disabled state"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn profile_sandbox_mode_overrides_base() -> std::io::Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
|
||||
@@ -93,6 +93,14 @@ fn apply_toml_edit_override_segments(
|
||||
current[last] = value;
|
||||
}
|
||||
|
||||
fn scalar_from_str(value: &str) -> toml_edit::Item {
|
||||
match value {
|
||||
"true" => toml_edit::value(true),
|
||||
"false" => toml_edit::value(false),
|
||||
_ => toml_edit::value(value),
|
||||
}
|
||||
}
|
||||
|
||||
async fn persist_overrides_with_behavior(
|
||||
codex_home: &Path,
|
||||
profile: Option<&str>,
|
||||
@@ -161,7 +169,7 @@ async fn persist_overrides_with_behavior(
|
||||
|
||||
match value {
|
||||
Some(v) => {
|
||||
let item_value = toml_edit::value(v);
|
||||
let item_value = scalar_from_str(v);
|
||||
apply_toml_edit_override_segments(&mut doc, segments_to_apply, item_value);
|
||||
mutated = true;
|
||||
}
|
||||
|
||||
@@ -356,6 +356,7 @@ impl ConversationHistory {
|
||||
call_id: call_id.clone(),
|
||||
output: FunctionCallOutputPayload {
|
||||
content: truncated,
|
||||
content_items: output.content_items.clone(),
|
||||
success: output.success,
|
||||
},
|
||||
}
|
||||
@@ -653,6 +654,7 @@ mod tests {
|
||||
call_id: "call-100".to_string(),
|
||||
output: FunctionCallOutputPayload {
|
||||
content: long_output.clone(),
|
||||
content_items: None,
|
||||
success: Some(true),
|
||||
},
|
||||
};
|
||||
|
||||
@@ -128,6 +128,7 @@ pub(crate) async fn assess_command(
|
||||
parallel_tool_calls: false,
|
||||
base_instructions_override: Some(system_prompt),
|
||||
output_schema: Some(sandbox_assessment_schema()),
|
||||
allowed_tools: None,
|
||||
};
|
||||
|
||||
let child_otel =
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::client_common::tools::ToolSpec;
|
||||
use crate::codex::Session;
|
||||
@@ -153,6 +154,24 @@ impl ToolRouter {
|
||||
payload,
|
||||
};
|
||||
|
||||
if !invocation
|
||||
.turn
|
||||
.tools_config
|
||||
.is_tool_allowed(invocation.tool_name.as_str())
|
||||
{
|
||||
let message = format!("tool {} is disabled for this session", invocation.tool_name);
|
||||
let otel = invocation.turn.client.get_otel_event_manager();
|
||||
otel.tool_result(
|
||||
invocation.tool_name.as_str(),
|
||||
&invocation.call_id,
|
||||
invocation.payload.log_payload().as_ref(),
|
||||
Duration::ZERO,
|
||||
false,
|
||||
&message,
|
||||
);
|
||||
return Err(FunctionCallError::RespondToModel(message));
|
||||
}
|
||||
|
||||
match self.registry.dispatch(invocation).await {
|
||||
Ok(response) => Ok(response),
|
||||
Err(FunctionCallError::Fatal(message)) => Err(FunctionCallError::Fatal(message)),
|
||||
|
||||
@@ -78,6 +78,37 @@ impl ToolsConfig {
|
||||
experimental_supported_tools: model_family.experimental_supported_tools.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn allowed_tool_names(&self, tool_specs: &[ToolSpec]) -> Option<Vec<String>> {
|
||||
if tool_specs.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut removed_web_search = false;
|
||||
let mut allowed: Vec<String> = Vec::with_capacity(tool_specs.len());
|
||||
|
||||
for spec in tool_specs {
|
||||
let name = spec.name();
|
||||
if name == "web_search" && !self.web_search_request {
|
||||
removed_web_search = true;
|
||||
continue;
|
||||
}
|
||||
allowed.push(name.to_string());
|
||||
}
|
||||
|
||||
if removed_web_search {
|
||||
Some(allowed)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_tool_allowed(&self, name: &str) -> bool {
|
||||
if name == "web_search" {
|
||||
return self.web_search_request;
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// Generic JSON‑Schema subset needed for our tool definitions
|
||||
@@ -971,9 +1002,7 @@ pub(crate) fn build_specs(
|
||||
builder.register_handler("test_sync_tool", test_sync_handler);
|
||||
}
|
||||
|
||||
if config.web_search_request {
|
||||
builder.push_spec(ToolSpec::WebSearch {});
|
||||
}
|
||||
builder.push_spec(ToolSpec::WebSearch {});
|
||||
|
||||
if config.include_view_image_tool {
|
||||
builder.push_spec_with_parallel_support(create_view_image_tool(), true);
|
||||
@@ -1205,6 +1234,64 @@ mod tests {
|
||||
assert_contains_tool_names(&tools, &subset);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_web_search_spec_present_even_when_disabled() {
|
||||
let model_family = find_family_for_model("o3").expect("o3 should be a valid model family");
|
||||
let mut features = Features::with_defaults();
|
||||
features.disable(Feature::WebSearchRequest);
|
||||
let config = ToolsConfig::new(&ToolsConfigParams {
|
||||
model_family: &model_family,
|
||||
features: &features,
|
||||
});
|
||||
let (tools, _) = build_specs(&config, None).build();
|
||||
assert!(
|
||||
tools
|
||||
.iter()
|
||||
.any(|tool| tool_name(&tool.spec) == "web_search"),
|
||||
"web_search spec should be present even when feature disabled"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_allowed_tool_names_filters_disabled_web_search() {
|
||||
let model_family = find_family_for_model("o3").expect("o3 should be a valid model family");
|
||||
let mut features = Features::with_defaults();
|
||||
features.disable(Feature::WebSearchRequest);
|
||||
let config = ToolsConfig::new(&ToolsConfigParams {
|
||||
model_family: &model_family,
|
||||
features: &features,
|
||||
});
|
||||
let (tools, _) = build_specs(&config, None).build();
|
||||
let specs: Vec<ToolSpec> = tools.iter().map(|t| t.spec.clone()).collect();
|
||||
let allowed = config
|
||||
.allowed_tool_names(specs.as_slice())
|
||||
.expect("expected allow list when web_search disabled");
|
||||
assert!(
|
||||
!allowed.iter().any(|name| name == "web_search"),
|
||||
"allowed tool list should exclude web_search"
|
||||
);
|
||||
assert!(
|
||||
!allowed.is_empty(),
|
||||
"allowed tool list should retain other tool names"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_allowed_tool_names_none_when_enabled() {
|
||||
let model_family = find_family_for_model("o3").expect("o3 should be a valid model family");
|
||||
let features = Features::with_defaults();
|
||||
let config = ToolsConfig::new(&ToolsConfigParams {
|
||||
model_family: &model_family,
|
||||
features: &features,
|
||||
});
|
||||
let (tools, _) = build_specs(&config, None).build();
|
||||
let specs: Vec<ToolSpec> = tools.iter().map(|t| t.spec.clone()).collect();
|
||||
assert!(
|
||||
config.allowed_tool_names(specs.as_slice()).is_none(),
|
||||
"allowed tool list should be None when web_search enabled"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_parallel_support_flags() {
|
||||
|
||||
@@ -1,15 +1,79 @@
|
||||
use codex_core::CodexAuth;
|
||||
use codex_core::ConversationManager;
|
||||
use codex_core::ModelProviderInfo;
|
||||
use codex_core::built_in_model_providers;
|
||||
use codex_core::features::Feature;
|
||||
use codex_core::model_family::find_family_for_model;
|
||||
use codex_core::protocol::EventMsg;
|
||||
use codex_core::protocol::Op;
|
||||
use codex_core::protocol_config_types::ReasoningEffort;
|
||||
use codex_protocol::user_input::UserInput;
|
||||
use core_test_support::load_default_config_for_test;
|
||||
use core_test_support::load_sse_fixture_with_id;
|
||||
use core_test_support::wait_for_event;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::Value;
|
||||
use tempfile::TempDir;
|
||||
use wiremock::Mock;
|
||||
use wiremock::MockServer;
|
||||
use wiremock::ResponseTemplate;
|
||||
use wiremock::matchers::method;
|
||||
use wiremock::matchers::path;
|
||||
|
||||
const CONFIG_TOML: &str = "config.toml";
|
||||
|
||||
fn sse_completed(id: &str) -> String {
|
||||
load_sse_fixture_with_id("tests/fixtures/completed_template.json", id)
|
||||
}
|
||||
|
||||
fn tool_identifiers(body: &Value) -> Vec<String> {
|
||||
let tools = match body["tools"].as_array() {
|
||||
Some(array) => array,
|
||||
None => panic!("tool list missing in Responses payload: {body:?}"),
|
||||
};
|
||||
|
||||
tools
|
||||
.iter()
|
||||
.map(|tool| {
|
||||
if let Some(name) = tool.get("name").and_then(Value::as_str) {
|
||||
name.to_string()
|
||||
} else if let Some(kind) = tool.get("type").and_then(Value::as_str) {
|
||||
kind.to_string()
|
||||
} else {
|
||||
panic!("tool entry missing identifiers: {tool:?}");
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn allowed_tool_specs(body: &Value) -> Option<Vec<Value>> {
|
||||
match body.get("tool_choice") {
|
||||
None => panic!("tool_choice missing in Responses payload: {body:?}"),
|
||||
Some(Value::String(name)) => {
|
||||
assert_eq!(name, "auto", "unexpected tool_choice string: {name}");
|
||||
None
|
||||
}
|
||||
Some(Value::Object(obj)) => {
|
||||
let ty = obj
|
||||
.get("type")
|
||||
.and_then(Value::as_str)
|
||||
.expect("tool_choice.type field");
|
||||
assert_eq!(ty, "allowed_tools", "unexpected tool_choice type: {ty}");
|
||||
let mode = obj
|
||||
.get("mode")
|
||||
.and_then(Value::as_str)
|
||||
.expect("tool_choice.mode field");
|
||||
assert_eq!(mode, "auto", "unexpected tool_choice mode: {mode}");
|
||||
let tools = obj
|
||||
.get("tools")
|
||||
.and_then(Value::as_array)
|
||||
.expect("tool_choice.tools field");
|
||||
Some(tools.to_vec())
|
||||
}
|
||||
Some(other) => panic!("unexpected tool_choice payload: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn override_turn_context_does_not_persist_when_config_exists() {
|
||||
let codex_home = TempDir::new().unwrap();
|
||||
@@ -38,6 +102,7 @@ async fn override_turn_context_does_not_persist_when_config_exists() {
|
||||
model: Some("o3".to_string()),
|
||||
effort: Some(Some(ReasoningEffort::High)),
|
||||
summary: None,
|
||||
include_web_search_request: None,
|
||||
})
|
||||
.await
|
||||
.expect("submit override");
|
||||
@@ -78,6 +143,7 @@ async fn override_turn_context_does_not_create_config_file() {
|
||||
model: Some("o3".to_string()),
|
||||
effort: Some(Some(ReasoningEffort::Medium)),
|
||||
summary: None,
|
||||
include_web_search_request: None,
|
||||
})
|
||||
.await
|
||||
.expect("submit override");
|
||||
@@ -90,3 +156,141 @@ async fn override_turn_context_does_not_create_config_file() {
|
||||
"override should not create config.toml"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn override_turn_context_toggles_web_search_tool() {
|
||||
let server = MockServer::start().await;
|
||||
|
||||
let sse = sse_completed("toggle-web-search");
|
||||
let template = ResponseTemplate::new(200)
|
||||
.insert_header("content-type", "text/event-stream")
|
||||
.set_body_raw(sse, "text/event-stream");
|
||||
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/v1/responses"))
|
||||
.respond_with(template.clone())
|
||||
.expect(2)
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let model_provider = ModelProviderInfo {
|
||||
base_url: Some(format!("{}/v1", server.uri())),
|
||||
..built_in_model_providers()["openai"].clone()
|
||||
};
|
||||
|
||||
let cwd = TempDir::new().unwrap();
|
||||
let codex_home = TempDir::new().unwrap();
|
||||
let mut config = load_default_config_for_test(&codex_home);
|
||||
config.cwd = cwd.path().to_path_buf();
|
||||
config.model = "gpt-5-codex".to_string();
|
||||
config.model_family = find_family_for_model("gpt-5-codex").expect("model family");
|
||||
config.model_provider = model_provider;
|
||||
config.model_provider_id = "openai".to_string();
|
||||
config.features.disable(Feature::WebSearchRequest);
|
||||
config.features.disable(Feature::ViewImageTool);
|
||||
config.features.disable(Feature::ApplyPatchFreeform);
|
||||
config.features.disable(Feature::StreamableShell);
|
||||
config.features.disable(Feature::UnifiedExec);
|
||||
config.features.disable(Feature::SandboxCommandAssessment);
|
||||
config.tools_web_search_request = config.features.enabled(Feature::WebSearchRequest);
|
||||
|
||||
let conversation_manager =
|
||||
ConversationManager::with_auth(CodexAuth::from_api_key("Test API Key"));
|
||||
let codex = conversation_manager
|
||||
.new_conversation(config)
|
||||
.await
|
||||
.expect("create conversation")
|
||||
.conversation;
|
||||
|
||||
codex
|
||||
.submit(Op::OverrideTurnContext {
|
||||
cwd: None,
|
||||
approval_policy: None,
|
||||
sandbox_policy: None,
|
||||
model: None,
|
||||
effort: None,
|
||||
summary: None,
|
||||
include_web_search_request: Some(true),
|
||||
})
|
||||
.await
|
||||
.expect("enable web search");
|
||||
|
||||
codex
|
||||
.submit(Op::UserInput {
|
||||
items: vec![UserInput::Text {
|
||||
text: "first turn".to_string(),
|
||||
}],
|
||||
})
|
||||
.await
|
||||
.expect("submit first input");
|
||||
wait_for_event(&codex, |ev| matches!(ev, EventMsg::TaskComplete(_))).await;
|
||||
|
||||
codex
|
||||
.submit(Op::OverrideTurnContext {
|
||||
cwd: None,
|
||||
approval_policy: None,
|
||||
sandbox_policy: None,
|
||||
model: None,
|
||||
effort: None,
|
||||
summary: None,
|
||||
include_web_search_request: Some(false),
|
||||
})
|
||||
.await
|
||||
.expect("disable web search");
|
||||
|
||||
codex
|
||||
.submit(Op::UserInput {
|
||||
items: vec![UserInput::Text {
|
||||
text: "second turn".to_string(),
|
||||
}],
|
||||
})
|
||||
.await
|
||||
.expect("submit second input");
|
||||
wait_for_event(&codex, |ev| matches!(ev, EventMsg::TaskComplete(_))).await;
|
||||
|
||||
let requests = server
|
||||
.received_requests()
|
||||
.await
|
||||
.unwrap_or_else(|| Vec::new());
|
||||
assert_eq!(requests.len(), 2, "expected two requests for two turns");
|
||||
|
||||
let first = match requests[0].body_json::<Value>() {
|
||||
Ok(json) => json,
|
||||
Err(err) => panic!("first request body should be JSON: {err}"),
|
||||
};
|
||||
let first_tools = tool_identifiers(&first);
|
||||
assert!(
|
||||
first_tools.iter().any(|tool| tool == "web_search"),
|
||||
"expected web_search tool after enabling; got {first_tools:?}"
|
||||
);
|
||||
assert!(
|
||||
allowed_tool_specs(&first).is_none(),
|
||||
"expected auto tool choice when web_search enabled"
|
||||
);
|
||||
|
||||
let second = match requests[1].body_json::<Value>() {
|
||||
Ok(json) => json,
|
||||
Err(err) => panic!("second request body should be JSON: {err}"),
|
||||
};
|
||||
let second_tools = tool_identifiers(&second);
|
||||
assert!(
|
||||
second_tools.iter().any(|tool| tool == "web_search"),
|
||||
"expected web_search tool spec to remain present; got {second_tools:?}"
|
||||
);
|
||||
let second_allowed = allowed_tool_specs(&second).expect("expected allowed tools payload");
|
||||
assert!(
|
||||
second_allowed.iter().all(|tool| tool
|
||||
.get("type")
|
||||
.and_then(Value::as_str)
|
||||
.map(|ty| ty != "web_search")
|
||||
.unwrap_or(true)),
|
||||
"expected web_search not to be allowed after disabling; got {second_allowed:?}"
|
||||
);
|
||||
assert!(
|
||||
second_allowed.iter().any(|tool| {
|
||||
tool.get("type") == Some(&Value::String("function".to_string()))
|
||||
&& tool.get("name") == Some(&Value::String("shell".to_string()))
|
||||
}),
|
||||
"expected shell function to remain allowed; got {second_allowed:?}"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -460,6 +460,7 @@ async fn overrides_turn_context_but_keeps_cached_prefix_and_key_constant() {
|
||||
model: Some("o3".to_string()),
|
||||
effort: Some(Some(ReasoningEffort::High)),
|
||||
summary: Some(ReasoningSummary::Detailed),
|
||||
include_web_search_request: None,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Reference in New Issue
Block a user