mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
3a2712ea14
## Summary - Add `web_search = "indexed"` alongside `disabled`, `cached`, and `live`. - Use that same resolved mode for both hosted and standalone web search. - For hosted search, send `index_gated_web_access: true` with external web access enabled only when `indexed` is selected. - For standalone search, preserve the existing boolean wire values for existing modes (`cached` maps to `false` and `live` to `true`) and send `"indexed"` only for `indexed`; `disabled` keeps the tool unavailable. - Carry the mode through managed configuration requirements and generated schemas. ## Why Indexed search provides a middle ground between cached-only search and unrestricted live page fetching. Search queries can remain live while direct page fetches are limited to URLs admitted by the server. The existing `web_search` setting remains the single source of truth, so hosted and standalone executors cannot drift into different access modes. Without an explicit `indexed` selection, the existing model-visible tool and request shapes are unchanged. ```toml web_search = "indexed" [features] standalone_web_search = true ``` ## Validation - `just fmt` - `just test -p codex-api` (`126 passed`) - `just test -p codex-web-search-extension` (`7 passed`) - `just test -p codex-core code_mode_can_call_indexed_standalone_web_search` (`1 passed`) - Focused configuration, hosted request, standalone request, and managed-requirement coverage is included in the PR; remaining suites run in CI. The full workspace test suite was not run locally.
136 lines
4.7 KiB
Rust
136 lines
4.7 KiB
Rust
use crate::FreeformTool;
|
|
use crate::JsonSchema;
|
|
use crate::LoadableToolSpec;
|
|
use crate::ResponsesApiNamespace;
|
|
use crate::ResponsesApiTool;
|
|
use codex_protocol::config_types::WebSearchContextSize;
|
|
use codex_protocol::config_types::WebSearchFilters as ConfigWebSearchFilters;
|
|
use codex_protocol::config_types::WebSearchUserLocation as ConfigWebSearchUserLocation;
|
|
use codex_protocol::config_types::WebSearchUserLocationType;
|
|
use serde::Serialize;
|
|
use serde_json::Value;
|
|
|
|
/// When serialized as JSON, this produces a valid "Tool" in the OpenAI
|
|
/// Responses API.
|
|
#[derive(Debug, Clone, Serialize, PartialEq)]
|
|
#[serde(tag = "type")]
|
|
pub enum ToolSpec {
|
|
#[serde(rename = "function")]
|
|
Function(ResponsesApiTool),
|
|
#[serde(rename = "namespace")]
|
|
Namespace(ResponsesApiNamespace),
|
|
#[serde(rename = "tool_search")]
|
|
ToolSearch {
|
|
execution: String,
|
|
description: String,
|
|
parameters: JsonSchema,
|
|
},
|
|
#[serde(rename = "image_generation")]
|
|
ImageGeneration { output_format: String },
|
|
// TODO: Understand why we get an error on web_search although the API docs
|
|
// say it's supported.
|
|
// https://platform.openai.com/docs/guides/tools-web-search?api-mode=responses#:~:text=%7B%20type%3A%20%22web_search%22%20%7D%2C
|
|
// The `external_web_access` field determines whether the web search is over
|
|
// cached or live content.
|
|
// https://platform.openai.com/docs/guides/tools-web-search#live-internet-access
|
|
#[serde(rename = "web_search")]
|
|
WebSearch {
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
external_web_access: Option<bool>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
index_gated_web_access: Option<bool>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
filters: Option<ResponsesApiWebSearchFilters>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
user_location: Option<ResponsesApiWebSearchUserLocation>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
search_context_size: Option<WebSearchContextSize>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
search_content_types: Option<Vec<String>>,
|
|
},
|
|
#[serde(rename = "custom")]
|
|
Freeform(FreeformTool),
|
|
}
|
|
|
|
impl ToolSpec {
|
|
pub fn name(&self) -> &str {
|
|
match self {
|
|
ToolSpec::Function(tool) => tool.name.as_str(),
|
|
ToolSpec::Namespace(namespace) => namespace.name.as_str(),
|
|
ToolSpec::ToolSearch { .. } => "tool_search",
|
|
ToolSpec::ImageGeneration { .. } => "image_generation",
|
|
ToolSpec::WebSearch { .. } => "web_search",
|
|
ToolSpec::Freeform(tool) => tool.name.as_str(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<LoadableToolSpec> for ToolSpec {
|
|
fn from(value: LoadableToolSpec) -> Self {
|
|
match value {
|
|
LoadableToolSpec::Function(tool) => ToolSpec::Function(tool),
|
|
LoadableToolSpec::Namespace(namespace) => ToolSpec::Namespace(namespace),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Returns JSON values that are compatible with Function Calling in the
|
|
/// Responses API:
|
|
/// https://platform.openai.com/docs/guides/function-calling?api-mode=responses
|
|
pub fn create_tools_json_for_responses_api(
|
|
tools: &[ToolSpec],
|
|
) -> Result<Vec<Value>, serde_json::Error> {
|
|
let mut tools_json = Vec::new();
|
|
|
|
for tool in tools {
|
|
let json = serde_json::to_value(tool)?;
|
|
tools_json.push(json);
|
|
}
|
|
|
|
Ok(tools_json)
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, PartialEq)]
|
|
pub struct ResponsesApiWebSearchFilters {
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub allowed_domains: Option<Vec<String>>,
|
|
}
|
|
|
|
impl From<ConfigWebSearchFilters> for ResponsesApiWebSearchFilters {
|
|
fn from(filters: ConfigWebSearchFilters) -> Self {
|
|
Self {
|
|
allowed_domains: filters.allowed_domains,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, PartialEq)]
|
|
pub struct ResponsesApiWebSearchUserLocation {
|
|
#[serde(rename = "type")]
|
|
pub r#type: WebSearchUserLocationType,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub country: Option<String>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub region: Option<String>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub city: Option<String>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub timezone: Option<String>,
|
|
}
|
|
|
|
impl From<ConfigWebSearchUserLocation> for ResponsesApiWebSearchUserLocation {
|
|
fn from(user_location: ConfigWebSearchUserLocation) -> Self {
|
|
Self {
|
|
r#type: user_location.r#type,
|
|
country: user_location.country,
|
|
region: user_location.region,
|
|
city: user_location.city,
|
|
timezone: user_location.timezone,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "tool_spec_tests.rs"]
|
|
mod tests;
|