mirror of
https://github.com/openai/codex.git
synced 2026-05-24 21:14:51 +00:00
## Why `codex-tools` is meant to hold reusable tool primitives, but `ToolsConfig` had become a second copy of core runtime decisions instead of a small shared contract. It carried provider capabilities, auth/model gates, permission and environment state, web/search/image feature gates, multi-agent settings, and goal availability from core into `codex-tools` ([definition](22dd9ad392/codex-rs/tools/src/tool_config.rs (L97)), [stored on each `TurnContext`](22dd9ad392/codex-rs/core/src/session/turn_context.rs (L87))). Every session/context variant then had to build and mutate that snapshot before assembling tools. This PR removes that master object instead of renaming it. Tool planning now reads the live `TurnContext`, where `codex-core` already owns those decisions, while `codex-tools` keeps only reusable primitives and a generic `ToolSetBuilder`/`ToolSet` accumulator. ## What Changed - Removed `ToolsConfig` / `ToolsConfigParams` from `codex-tools`; the crate keeps the shared helpers that still belong there, including request-user-input mode selection, shell backend/type resolution, `UnifiedExecShellMode`, and `ToolEnvironmentMode`. - Replaced config-snapshot planning with `ToolRouter::from_turn_context` and a `spec_plan` pipeline over `CoreToolPlanContext`, deriving provider capabilities, auth gates, model support, feature gates, environment count, goal support, multi-agent options, web search, and image generation from the authoritative turn state. - Added generic `codex_tools::ToolSetBuilder` / `ToolSet`, plus the small core adapter needed to accumulate `CoreToolRuntime` values and hosted model specs. - Added the `tool_family::shell` registration module and moved shell/unified-exec/memory accounting call sites to read the narrow per-turn fields directly. - Narrowed `TurnContext` to the remaining explicit per-turn fields needed by planning: `available_models`, `unified_exec_shell_mode`, and `goal_tools_supported`. - Reworked MCP exposure and tool-search setup so deferred/direct MCP behavior is driven by the current turn rather than a precomputed config snapshot. - Replaced the large expected-spec fixture tests with focused behavior-level coverage for shell tools, environments, goal and agent-job gates, MCP direct/deferred exposure, tool search, request-plugin-install, code mode, multi-agent mode, hosted tools, and extension executor dispatch. ## Verification - `cargo check -p codex-tools` - `cargo check -p codex-core --lib` - `cargo test -p codex-tools` - `cargo test -p codex-core spec_plan --lib` - `cargo test -p codex-core router --lib`
81 lines
3.0 KiB
Rust
81 lines
3.0 KiB
Rust
use crate::tools::context::ToolInvocation;
|
|
use crate::tools::context::ToolPayload;
|
|
use crate::tools::flat_tool_name;
|
|
use crate::tools::handlers::unified_exec::ExecCommandArgs;
|
|
use codex_memories_read::usage::MEMORIES_USAGE_METRIC;
|
|
use codex_memories_read::usage::memories_usage_kinds_from_command;
|
|
use codex_protocol::models::ShellCommandToolCallParams;
|
|
use std::path::PathBuf;
|
|
|
|
pub(crate) async fn emit_metric_for_tool_read(invocation: &ToolInvocation, success: bool) {
|
|
let Some((command, _)) = shell_command_for_invocation(invocation) else {
|
|
return;
|
|
};
|
|
let kinds = memories_usage_kinds_from_command(&command);
|
|
if kinds.is_empty() {
|
|
return;
|
|
}
|
|
|
|
let success = if success { "true" } else { "false" };
|
|
let tool_name = flat_tool_name(&invocation.tool_name);
|
|
for kind in kinds {
|
|
invocation.turn.session_telemetry.counter(
|
|
MEMORIES_USAGE_METRIC,
|
|
/*inc*/ 1,
|
|
&[
|
|
("kind", kind.as_tag()),
|
|
("tool", tool_name.as_ref()),
|
|
("success", success),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
fn shell_command_for_invocation(invocation: &ToolInvocation) -> Option<(Vec<String>, PathBuf)> {
|
|
let ToolPayload::Function { arguments } = &invocation.payload else {
|
|
return None;
|
|
};
|
|
|
|
match (
|
|
invocation.tool_name.namespace.as_deref(),
|
|
invocation.tool_name.name.as_str(),
|
|
) {
|
|
(None, "shell_command") => serde_json::from_str::<ShellCommandToolCallParams>(arguments)
|
|
.ok()
|
|
.map(|params| {
|
|
if !invocation.turn.config.permissions.allow_login_shell
|
|
&& params.login == Some(true)
|
|
{
|
|
#[allow(deprecated)]
|
|
let cwd = invocation.turn.resolve_path(params.workdir).to_path_buf();
|
|
return (Vec::new(), cwd);
|
|
}
|
|
let use_login_shell = params
|
|
.login
|
|
.unwrap_or(invocation.turn.config.permissions.allow_login_shell);
|
|
let command = invocation
|
|
.session
|
|
.user_shell()
|
|
.derive_exec_args(¶ms.command, use_login_shell);
|
|
#[allow(deprecated)]
|
|
let cwd = invocation.turn.resolve_path(params.workdir).to_path_buf();
|
|
(command, cwd)
|
|
}),
|
|
(None, "exec_command") => serde_json::from_str::<ExecCommandArgs>(arguments)
|
|
.ok()
|
|
.and_then(|params| {
|
|
let command = crate::tools::handlers::unified_exec::get_command(
|
|
¶ms,
|
|
invocation.session.user_shell(),
|
|
&invocation.turn.unified_exec_shell_mode,
|
|
invocation.turn.config.permissions.allow_login_shell,
|
|
)
|
|
.ok()?;
|
|
#[allow(deprecated)]
|
|
let cwd = invocation.turn.resolve_path(params.workdir).to_path_buf();
|
|
Some((command.command, cwd))
|
|
}),
|
|
(Some(_), _) | (None, _) => None,
|
|
}
|
|
}
|