mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Add ranked tool discovery to JavaScript code mode (#51209)
## What changed
- Add the default-disabled `code_mode_tool_search` feature to expose `await tools.tool_search({query: "...", limit: 8})` in code mode when the model supports tool search.
- Return BM25-ranked deferred tools as `{tools: [{name, description}]}`, including callable names and TypeScript declarations for use in later code cells.
- Filter results through the current step's nested-tool policy before applying the limit, preserving namespace restrictions, normalized identities, description prefixes, and schema budgets.
- Add ranked-search guidance while retaining `ALL_TOOLS` catalog discovery and description overrides.
## Testing
Add coverage for feature and model-capability gating, namespace restrictions, argument validation, schema budgets, and discovery followed by deferred MCP and dynamic tool calls. Extend the scenario snapshot to cover search, invocation, yielding, and completion through `wait`.
GitOrigin-RevId: 092a29dd5f0773c518f53a001b9cafb917422f13
This commit is contained in:
committed by
copyberry
parent
5ad6891696
commit
4d15794336
@@ -8,6 +8,7 @@ use codex_prompts::ResolvedModelMessages;
|
||||
use codex_tools::IndirectNamespacePrefixes;
|
||||
use codex_tools::ToolName;
|
||||
use codex_tools::ToolSpec;
|
||||
use std::borrow::Cow;
|
||||
use std::sync::Arc;
|
||||
|
||||
use super::ExecContext;
|
||||
@@ -15,6 +16,7 @@ use super::PUBLIC_TOOL_NAME;
|
||||
use super::handle_runtime_response;
|
||||
use super::is_exec_tool_name;
|
||||
use super::output::CodeModeToolOutput;
|
||||
use super::prepare_code_mode_tool_definitions;
|
||||
use super::telemetry::CodeModeToolCallGuard;
|
||||
use super::telemetry::trace_id;
|
||||
|
||||
@@ -55,22 +57,15 @@ impl CodeModeExecuteHandler {
|
||||
.tool_input_schema_max_bytes;
|
||||
let mut enabled_tools = Vec::with_capacity(self.nested_tool_specs.len());
|
||||
for (spec, cached_runtime) in &self.nested_tool_specs {
|
||||
if let Some(cached_definitions) = cached_runtime.as_ref().and_then(|runtime| {
|
||||
runtime.cached_code_mode_definitions(code_mode_input_schema_max_bytes)
|
||||
}) {
|
||||
enabled_tools.extend_from_slice(cached_definitions);
|
||||
continue;
|
||||
}
|
||||
|
||||
let definitions = codex_tools::collect_code_mode_tool_definitions(
|
||||
std::iter::once(spec.as_ref()),
|
||||
let definitions = prepare_code_mode_tool_definitions(
|
||||
cached_runtime.as_deref(),
|
||||
|| Cow::Borrowed(spec.as_ref()),
|
||||
code_mode_input_schema_max_bytes,
|
||||
);
|
||||
enabled_tools.extend(definitions.into_iter().map(|mut definition| {
|
||||
definition.input_schema = None;
|
||||
definition.output_schema = None;
|
||||
definition
|
||||
}));
|
||||
match definitions {
|
||||
Cow::Borrowed(definitions) => enabled_tools.extend_from_slice(definitions),
|
||||
Cow::Owned(definitions) => enabled_tools.extend(definitions),
|
||||
}
|
||||
}
|
||||
enabled_tools.sort_by(|left, right| left.name.cmp(&right.name));
|
||||
enabled_tools.dedup_by(|left, right| left.name == right.name);
|
||||
|
||||
@@ -1,20 +1,8 @@
|
||||
use codex_code_mode::ImageDetailVisibility;
|
||||
use codex_code_mode::ToolDefinition as CodeModeToolDefinition;
|
||||
use codex_protocol::openai_models::CodeModeToolMessages;
|
||||
use codex_tools::FreeformTool;
|
||||
use codex_tools::FreeformToolFormat;
|
||||
use codex_tools::ToolSpec;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
pub(crate) fn create_code_mode_tool(
|
||||
enabled_tools: &[CodeModeToolDefinition],
|
||||
deferred_tools: &[CodeModeToolDefinition],
|
||||
namespace_descriptions: &BTreeMap<String, codex_code_mode::ToolNamespaceDescription>,
|
||||
default_exec_yield_time_ms: u64,
|
||||
code_mode_only: bool,
|
||||
image_detail_visibility: ImageDetailVisibility,
|
||||
messages: Option<&CodeModeToolMessages>,
|
||||
) -> ToolSpec {
|
||||
pub(crate) fn create_code_mode_tool(description: String) -> ToolSpec {
|
||||
const CODE_MODE_FREEFORM_GRAMMAR: &str = r#"
|
||||
start: pragma_source | plain_source
|
||||
pragma_source: PRAGMA_LINE NEWLINE SOURCE
|
||||
@@ -27,15 +15,7 @@ SOURCE: /[\s\S]+/
|
||||
|
||||
ToolSpec::Freeform(FreeformTool {
|
||||
name: codex_code_mode::PUBLIC_TOOL_NAME.to_string(),
|
||||
description: codex_code_mode::build_exec_tool_description(
|
||||
enabled_tools,
|
||||
deferred_tools,
|
||||
namespace_descriptions,
|
||||
default_exec_yield_time_ms,
|
||||
code_mode_only,
|
||||
image_detail_visibility,
|
||||
messages,
|
||||
),
|
||||
description,
|
||||
defer_loading: None,
|
||||
format: FreeformToolFormat {
|
||||
r#type: "grammar".to_string(),
|
||||
@@ -48,42 +28,15 @@ SOURCE: /[\s\S]+/
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use codex_tools::ToolName;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn create_code_mode_tool_matches_expected_spec() {
|
||||
let enabled_tools = vec![codex_code_mode::ToolDefinition {
|
||||
name: "update_plan".to_string(),
|
||||
tool_name: ToolName::plain("update_plan"),
|
||||
description: "Update the plan".to_string(),
|
||||
kind: codex_code_mode::CodeModeToolKind::Function,
|
||||
input_schema: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: None,
|
||||
}];
|
||||
|
||||
assert_eq!(
|
||||
create_code_mode_tool(
|
||||
&enabled_tools,
|
||||
&[],
|
||||
&BTreeMap::new(),
|
||||
codex_code_mode::DEFAULT_EXEC_YIELD_TIME_MS,
|
||||
/*code_mode_only*/ true,
|
||||
ImageDetailVisibility::Visible,
|
||||
/*messages*/ None,
|
||||
),
|
||||
create_code_mode_tool("Run JavaScript code.".to_string()),
|
||||
ToolSpec::Freeform(FreeformTool {
|
||||
name: codex_code_mode::PUBLIC_TOOL_NAME.to_string(),
|
||||
description: codex_code_mode::build_exec_tool_description(
|
||||
&enabled_tools,
|
||||
&[],
|
||||
&BTreeMap::new(),
|
||||
codex_code_mode::DEFAULT_EXEC_YIELD_TIME_MS,
|
||||
/*code_mode_only*/ true,
|
||||
ImageDetailVisibility::Visible,
|
||||
/*messages*/ None,
|
||||
),
|
||||
description: "Run JavaScript code.".to_string(),
|
||||
defer_loading: None,
|
||||
format: FreeformToolFormat {
|
||||
r#type: "grammar".to_string(),
|
||||
|
||||
@@ -4,6 +4,7 @@ pub(crate) mod execute_spec;
|
||||
mod output;
|
||||
mod response_adapter;
|
||||
mod telemetry;
|
||||
mod tool_definitions;
|
||||
mod wait_handler;
|
||||
pub(crate) mod wait_spec;
|
||||
|
||||
@@ -54,6 +55,7 @@ use delegate::CodeModeDispatchWorker;
|
||||
pub(crate) use execute_handler::CodeModeExecuteHandler;
|
||||
use output::CodeModeToolOutput;
|
||||
use response_adapter::into_function_call_output_content_items;
|
||||
pub(crate) use tool_definitions::prepare_code_mode_tool_definitions;
|
||||
pub(crate) use wait_handler::CodeModeWaitHandler;
|
||||
|
||||
pub(crate) const PUBLIC_TOOL_NAME: &str = codex_code_mode::PUBLIC_TOOL_NAME;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
//! Prepares nested tool definitions for execution and search.
|
||||
//! Cached definitions stay borrowed; rendered schemas are dropped after augmentation.
|
||||
|
||||
use crate::tools::registry::CoreToolRuntime;
|
||||
use codex_code_mode::ToolDefinition;
|
||||
use codex_tools::ToolSpec;
|
||||
use std::borrow::Cow;
|
||||
|
||||
pub(crate) fn prepare_code_mode_tool_definitions<'a>(
|
||||
runtime: Option<&'a dyn CoreToolRuntime>,
|
||||
spec: impl FnOnce() -> Cow<'a, ToolSpec>,
|
||||
code_mode_input_schema_max_bytes: Option<usize>,
|
||||
) -> Cow<'a, [ToolDefinition]> {
|
||||
if let Some(definitions) = runtime
|
||||
.and_then(|runtime| runtime.cached_code_mode_definitions(code_mode_input_schema_max_bytes))
|
||||
{
|
||||
return Cow::Borrowed(definitions);
|
||||
}
|
||||
|
||||
let spec = spec();
|
||||
let mut definitions = codex_tools::collect_code_mode_tool_definitions(
|
||||
std::iter::once(spec.as_ref()),
|
||||
code_mode_input_schema_max_bytes,
|
||||
);
|
||||
for definition in &mut definitions {
|
||||
definition.input_schema = None;
|
||||
definition.output_schema = None;
|
||||
}
|
||||
Cow::Owned(definitions)
|
||||
}
|
||||
Reference in New Issue
Block a user