mirror of
https://github.com/openai/codex.git
synced 2026-11-23 23:10:47 +00:00
## Why Namespace-wide guidance can repeat on every entry in flat Code Mode listings. The existing prefix limit covers only reachable `functions` tools, leaving indirect namespace guidance outside the budget. ## What changed - Add `builtin_tool_namespaces` to put guidance on namespace headers in Code Mode prompts and tool search responses, including headers shared with client tools. The selector applies to namespaces with a registered built-in tool and is omitted from flat listings. Overlapping selectors must agree after trimming. - Validate all four prefix maps together against the selected model's `description_prefix_budget_bytes`, which defaults to 512. Count each trimmed value once in UTF-8 bytes, including unused selectors. Exclude keys, paragraph separators, and rendered copies. ## Testing Cover shared headers, flat listings, and conflicting selectors. Exercise the default budget boundary with multibyte text and verify that a model can raise the limit. GitOrigin-RevId: 1570c23ea1dbad6041cc8e48d0dc2b77f92596c7
74 lines
2.8 KiB
Rust
74 lines
2.8 KiB
Rust
//! Prepares function descriptions before direct, Code Mode, or search rendering.
|
|
//! Only the `functions` namespace is affected; runtime-owned specs stay unchanged.
|
|
|
|
use crate::ResponsesApiNamespaceTool;
|
|
use crate::ToolName;
|
|
use crate::ToolSpec;
|
|
use crate::indirect_namespace_prefixes::prepend;
|
|
use std::collections::BTreeMap;
|
|
|
|
/// Trimmed description prefixes for the available `functions.*` tools.
|
|
#[derive(Default)]
|
|
pub struct FunctionsNamespaceFunctionPrefixes(BTreeMap<String, String>);
|
|
|
|
impl FunctionsNamespaceFunctionPrefixes {
|
|
pub fn new(
|
|
prefixes: Option<&BTreeMap<String, String>>,
|
|
available_tools: impl IntoIterator<Item = ToolName>,
|
|
) -> Self {
|
|
let Some(prefixes) = prefixes.filter(|prefixes| !prefixes.is_empty()) else {
|
|
return Self::default();
|
|
};
|
|
let mut normalized = BTreeMap::new();
|
|
for tool in available_tools {
|
|
if !tool.is_default_namespace() || normalized.contains_key(&tool.name) {
|
|
continue;
|
|
}
|
|
let Some(prefix) = prefixes.get(&tool.name) else {
|
|
continue;
|
|
};
|
|
let prefix = prefix.trim();
|
|
if prefix.is_empty() {
|
|
continue;
|
|
}
|
|
normalized.insert(tool.name, prefix.to_owned());
|
|
}
|
|
Self(normalized)
|
|
}
|
|
|
|
/// Returns an owned replacement only for a configured function. Unaffected tools
|
|
/// keep their original specs and derived caches, including large MCP schemas.
|
|
pub fn prepare(&self, name: &ToolName, spec: impl FnOnce() -> ToolSpec) -> Option<ToolSpec> {
|
|
if !name.is_default_namespace() || !self.0.contains_key(&name.name) {
|
|
return None;
|
|
}
|
|
let mut spec = spec();
|
|
match &mut spec {
|
|
ToolSpec::Function(tool) => self.prepend(&tool.name, &mut tool.description),
|
|
ToolSpec::Freeform(tool) => self.prepend(&tool.name, &mut tool.description),
|
|
ToolSpec::Namespace(namespace) if namespace.name == "functions" => {
|
|
for tool in &mut namespace.tools {
|
|
match tool {
|
|
ResponsesApiNamespaceTool::Function(tool) => {
|
|
self.prepend(&tool.name, &mut tool.description);
|
|
}
|
|
ResponsesApiNamespaceTool::Custom(tool) => {
|
|
self.prepend(&tool.name, &mut tool.description);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ToolSpec::Namespace(_) | ToolSpec::ToolSearch { .. } | ToolSpec::WebSearch { .. } => {
|
|
return None;
|
|
}
|
|
}
|
|
Some(spec)
|
|
}
|
|
|
|
fn prepend(&self, name: &str, description: &mut String) {
|
|
if let Some(prefix) = self.0.get(name) {
|
|
prepend(prefix, description);
|
|
}
|
|
}
|
|
}
|