mirror of
https://github.com/openai/codex.git
synced 2026-12-05 01:00:54 +00:00
Expose TypeScript signatures in Code Mode's ALL_TOOLS (#64939)
## Why Code Mode embeds TypeScript declarations in tool descriptions, so scripts that want to inspect a tool's argument and return types have to extract the declaration from the description. ## What changed Enable `features.code_mode_all_tools_schema` to expose callable names, descriptions, and TypeScript signatures as separate fields in `ALL_TOOLS`. Update the `exec` guidance to match the selected format, including when resuming a session. The flag is off by default. Carry signatures over gRPC and stdio. Reject execution requests with signatures when the host does not advertise support for them. ## Testing Add coverage for both catalog formats, `exec` guidance on resume, and signature preservation over gRPC and stdio. GitOrigin-RevId: dedaeec4a2563c23d432c324187ef20f6a0e5ea3
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
//! Selects how Code Mode presents tool metadata in ALL_TOOLS and its prompt.
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
||||
pub enum ToolCatalogFormat {
|
||||
#[default]
|
||||
Legacy,
|
||||
Structured,
|
||||
}
|
||||
@@ -21,12 +21,13 @@ Tool availability can change between calls."#;
|
||||
pub const TOOL_SEARCH_GUIDANCE: &str = r#"Use `await tools.tool_search({query: "...", limit: 8})` for BM25-ranked discovery of deferred tools (up to `limit` matches; 8 by default). It returns `{tools: [{name, description}]}` with callable names and full TypeScript declarations. Print results with `text()` and inspect unfamiliar declarations before calling `tools[result.name](arguments)` in a later execution."#;
|
||||
const LEGACY_IMAGE_HELPER_DESCRIPTION: &str = r#"`image(imageUrlOrItem: string | { image_url: string; detail?: "auto" | "low" | "high" | "original" | null } | ImageContent, detail?: "auto" | "low" | "high" | "original" | null)`: Appends an image item. `image_url` should be a base64-encoded `data:` URL. To forward an MCP tool image, pass an individual `ImageContent` block from `result.content`, for example `image(result.content[0])`. MCP image blocks may request detail with `_meta: { "codex/imageDetail": "original" }`. When provided, the second `detail` argument overrides any detail embedded in the first argument."#;
|
||||
const UNIFIED_IMAGE_HELPER_DESCRIPTION: &str = r#"`image(imageUrlOrItem: string | { image_url: string } | ImageContent)`: Appends an image item. `image_url` should be a base64-encoded `data:` URL. To forward an MCP tool image, pass an individual `ImageContent` block from `result.content`, for example `image(result.content[0])`."#;
|
||||
const EXEC_DESCRIPTION_TEMPLATE: &str = r#"Run JavaScript code to orchestrate/compose tool calls
|
||||
const EXEC_DESCRIPTION_INTRO: &str = r#"Run JavaScript code to orchestrate/compose tool calls
|
||||
- Evaluates the provided JavaScript code in a fresh V8 isolate as an async module.
|
||||
- All nested tools are available on the global `tools` object, for example `await tools.exec_command(...)`. Tool names are exposed as normalized JavaScript identifiers, for example `await tools.mcp__ologs__get_profile(...)`.
|
||||
- Nested tool methods take either a string or an object as their input argument.
|
||||
- Nested tools return either an object or a string, based on the description.
|
||||
- Runs raw JavaScript -- no Node, no file system, no network access, no console.
|
||||
- All nested tools are available on the global `tools` object, for example `await tools.exec_command(...)`. Tool names are exposed as normalized JavaScript identifiers, for example `await tools.mcp__ologs__get_profile(...)`."#;
|
||||
const LEGACY_NESTED_TOOL_TYPES: &str = r#"- Nested tool methods take either a string or an object as their input argument.
|
||||
- Nested tools return either an object or a string, based on the description."#;
|
||||
const SIGNATURE_NESTED_TOOL_TYPES: &str = "- Nested tools take a string or an object. Each tool’s signature shows its argument and return types.";
|
||||
const EXEC_DESCRIPTION_BODY_TEMPLATE: &str = r#"- Runs raw JavaScript -- no Node, no file system, no network access, no console.
|
||||
- Accepts raw JavaScript source text, not JSON, quoted strings, or markdown code fences.
|
||||
- You may optionally start the tool input with a first-line pragma like `// @exec: {"yield_time_ms": 10000, "max_output_tokens": 1000}`.
|
||||
- `yield_time_ms` asks `exec` to yield early if the script is still running. Defaults to {{ default_exec_yield_time_ms }} ms.
|
||||
@@ -42,9 +43,11 @@ const EXEC_HELPERS_TEMPLATE: &str = r#"- Global helpers:
|
||||
- `load(key: string)`: returns the stored value for a string key, or `undefined` if it is missing.
|
||||
- `notify(value: string | number | boolean | undefined | null)`: immediately injects an extra `custom_tool_call_output` for the current `exec` call. Values are stringified like `text(...)`.
|
||||
- `setTimeout(callback: () => void, delayMs?: number)`: schedules a callback to run later and returns a timeout id. Pending timeouts do not keep `exec` alive by themselves; await an explicit promise if you need to wait for one.
|
||||
- `clearTimeout(timeoutId?: number)`: cancels a timeout created by `setTimeout`.
|
||||
- `ALL_TOOLS`: metadata for the enabled nested tools as `{ name, description }` entries.
|
||||
- `yield_control()`: yields the accumulated output to the model immediately while the script keeps running."#;
|
||||
- `clearTimeout(timeoutId?: number)`: cancels a timeout created by `setTimeout`."#;
|
||||
const LEGACY_ALL_TOOLS: &str =
|
||||
"- `ALL_TOOLS`: metadata for the enabled nested tools as `{ name, description }` entries.";
|
||||
const SIGNATURE_ALL_TOOLS: &str = "- `ALL_TOOLS`: metadata for the enabled nested tools as `{ name, description, signature }` entries.";
|
||||
const EXEC_YIELD_CONTROL_GUIDANCE: &str = "- `yield_control()`: yields the accumulated output to the model immediately while the script keeps running.";
|
||||
const WAIT_DESCRIPTION_TEMPLATE: &str = r#"- Use `wait` only after `exec` returns `Script running with cell ID ...`.
|
||||
- `cell_id` identifies the running `exec` cell to resume.
|
||||
- `yield_time_ms` controls how long to wait for more output before yielding again. Defaults to 10000 ms.
|
||||
@@ -146,6 +149,9 @@ pub struct ToolDefinition {
|
||||
pub tool_name: ToolName,
|
||||
pub description: String,
|
||||
pub kind: CodeModeToolKind,
|
||||
/// When present, ALL_TOOLS exposes this TypeScript signature separately from the description.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub signature: Option<String>,
|
||||
pub input_schema: Option<JsonValue>,
|
||||
/// Internal budget used while rendering the input declaration, before sending it to the host.
|
||||
#[serde(skip)]
|
||||
@@ -171,7 +177,7 @@ pub struct ExecDefinition {
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ExecDefinitions {
|
||||
pub entries: Vec<ExecDefinition>,
|
||||
/// Newline-separated signatures of the global helpers inserted by the renderer.
|
||||
/// Newline-separated helper signatures and catalog format markers used to refresh exec.
|
||||
pub helper_signatures: String,
|
||||
}
|
||||
|
||||
@@ -302,6 +308,7 @@ pub fn build_exec_tool_description_and_definitions(
|
||||
image_detail_visibility: ImageDetailVisibility,
|
||||
deferred_tool_discovery: DeferredToolDiscovery,
|
||||
incremental_exec_enabled: bool,
|
||||
catalog_format: crate::ToolCatalogFormat,
|
||||
messages: Option<&CodeModeToolMessages>,
|
||||
) -> (String, ExecDefinitions) {
|
||||
let mut sections = Vec::new();
|
||||
@@ -309,6 +316,10 @@ pub fn build_exec_tool_description_and_definitions(
|
||||
ImageDetailVisibility::Visible => LEGACY_IMAGE_HELPER_DESCRIPTION,
|
||||
ImageDetailVisibility::Hidden => UNIFIED_IMAGE_HELPER_DESCRIPTION,
|
||||
};
|
||||
let (nested_tool_types, all_tools) = match catalog_format {
|
||||
crate::ToolCatalogFormat::Legacy => (LEGACY_NESTED_TOOL_TYPES, LEGACY_ALL_TOOLS),
|
||||
crate::ToolCatalogFormat::Structured => (SIGNATURE_NESTED_TOOL_TYPES, SIGNATURE_ALL_TOOLS),
|
||||
};
|
||||
const IMAGE_HELPER_PLACEHOLDER: &str = "{{ image_helper }}";
|
||||
let yield_time = default_exec_yield_time_ms.to_string();
|
||||
let render = |template: &str| {
|
||||
@@ -325,14 +336,21 @@ pub fn build_exec_tool_description_and_definitions(
|
||||
if !description.is_empty() {
|
||||
sections.push(description);
|
||||
}
|
||||
if catalog_format == crate::ToolCatalogFormat::Structured {
|
||||
sections.push(all_tools.to_string());
|
||||
}
|
||||
if incremental_exec_enabled && template.contains(IMAGE_HELPER_PLACEHOLDER) {
|
||||
definitions.helper_signatures = helper_signature(image_helper)
|
||||
.unwrap_or_default()
|
||||
.to_owned();
|
||||
}
|
||||
} else {
|
||||
sections.push(render(EXEC_DESCRIPTION_TEMPLATE));
|
||||
let body = render(EXEC_DESCRIPTION_BODY_TEMPLATE);
|
||||
sections.push(format!(
|
||||
"{EXEC_DESCRIPTION_INTRO}\n{nested_tool_types}\n{body}"
|
||||
));
|
||||
let helpers = render(EXEC_HELPERS_TEMPLATE);
|
||||
let helpers = format!("{helpers}\n{all_tools}\n{EXEC_YIELD_CONTROL_GUIDANCE}");
|
||||
if incremental_exec_enabled {
|
||||
definitions.helper_signatures = helpers
|
||||
.lines()
|
||||
@@ -342,6 +360,16 @@ pub fn build_exec_tool_description_and_definitions(
|
||||
}
|
||||
sections.push(helpers);
|
||||
}
|
||||
// The helper name is ALL_TOOLS in both formats. Only tag the new shape so
|
||||
// existing legacy snapshots keep their original refresh key on resume.
|
||||
if incremental_exec_enabled && catalog_format == crate::ToolCatalogFormat::Structured {
|
||||
if !definitions.helper_signatures.is_empty() {
|
||||
definitions.helper_signatures.push('\n');
|
||||
}
|
||||
definitions
|
||||
.helper_signatures
|
||||
.push_str("ALL_TOOLS:signature");
|
||||
}
|
||||
let default_guidance = match deferred_tool_discovery {
|
||||
// In Code Mode Only, the enabled search tool's description is rendered below.
|
||||
DeferredToolDiscovery::RankedSearch if !code_mode_only => {
|
||||
@@ -485,11 +513,14 @@ pub fn enabled_tool_metadata(definition: &ToolDefinition) -> EnabledToolMetadata
|
||||
global_name: normalize_code_mode_identifier(&definition.name),
|
||||
description: definition.description.clone(),
|
||||
kind: definition.kind,
|
||||
signature: definition.signature.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
|
||||
pub struct EnabledToolMetadata {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub signature: Option<String>,
|
||||
pub tool_name: ToolName,
|
||||
pub global_name: String,
|
||||
pub description: String,
|
||||
@@ -503,14 +534,22 @@ pub fn render_code_mode_sample(
|
||||
input_type: String,
|
||||
output_type: String,
|
||||
) -> String {
|
||||
let declaration = format!(
|
||||
"declare const tools: {{ {} }};",
|
||||
render_code_mode_tool_declaration(tool_name, input_name, &input_type, &output_type)
|
||||
);
|
||||
format!("{description}\n\nexec tool declaration:\n```ts\n{declaration}\n```")
|
||||
let signature = render_method_signature(tool_name, input_name, &input_type, &output_type);
|
||||
render_description_with_signature(description, &signature)
|
||||
}
|
||||
|
||||
fn render_code_mode_sample_for_definition(definition: &ToolDefinition) -> String {
|
||||
let signature = render_code_mode_signature(definition);
|
||||
render_description_with_signature(&definition.description, &signature)
|
||||
}
|
||||
|
||||
fn render_description_with_signature(description: &str, signature: &str) -> String {
|
||||
format!(
|
||||
"{description}\n\nexec tool declaration:\n```ts\ndeclare const tools: {{ {signature} }};\n```"
|
||||
)
|
||||
}
|
||||
|
||||
pub fn render_code_mode_signature(definition: &ToolDefinition) -> String {
|
||||
let input_name = match definition.kind {
|
||||
CodeModeToolKind::Function => "args",
|
||||
CodeModeToolKind::Freeform => "input",
|
||||
@@ -542,16 +581,10 @@ fn render_code_mode_sample_for_definition(definition: &ToolDefinition) -> String
|
||||
.map(render_json_schema_to_typescript)
|
||||
.unwrap_or_else(|| "unknown".to_string())
|
||||
};
|
||||
render_code_mode_sample(
|
||||
&definition.description,
|
||||
&definition.name,
|
||||
input_name,
|
||||
input_type,
|
||||
output_type,
|
||||
)
|
||||
render_method_signature(&definition.name, input_name, &input_type, &output_type)
|
||||
}
|
||||
|
||||
fn render_code_mode_tool_declaration(
|
||||
fn render_method_signature(
|
||||
tool_name: &str,
|
||||
input_name: &str,
|
||||
input_type: &str,
|
||||
@@ -700,6 +733,7 @@ mod tests {
|
||||
"required": ["city"],
|
||||
"additionalProperties": false
|
||||
})),
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: Some(json!({
|
||||
"type": "object",
|
||||
@@ -741,6 +775,7 @@ mod tests {
|
||||
},
|
||||
"required": ["weather"]
|
||||
})),
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: Some(json!({
|
||||
"type": "object",
|
||||
@@ -778,6 +813,7 @@ mod tests {
|
||||
"properties": {},
|
||||
"additionalProperties": false
|
||||
})),
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: Some(mcp_call_tool_result_schema(json!({
|
||||
"type": "object",
|
||||
@@ -818,6 +854,7 @@ mod tests {
|
||||
description: "bar".to_string(),
|
||||
kind: CodeModeToolKind::Function,
|
||||
input_schema: None,
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: None,
|
||||
}],
|
||||
@@ -828,6 +865,7 @@ mod tests {
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
/*messages*/ None,
|
||||
);
|
||||
assert!(definitions.entries.is_empty());
|
||||
@@ -849,6 +887,7 @@ bar"
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
/*messages*/ None,
|
||||
);
|
||||
assert!(description.contains("`audio(audioUrlOrItem:"));
|
||||
@@ -877,6 +916,7 @@ bar"
|
||||
"required": ["city"],
|
||||
"additionalProperties": false
|
||||
})),
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: Some(mcp_call_tool_result_schema(json!({
|
||||
"type": "object",
|
||||
@@ -895,6 +935,7 @@ bar"
|
||||
"properties": {},
|
||||
"additionalProperties": false
|
||||
})),
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: Some(mcp_call_tool_result_schema(json!({
|
||||
"type": "object",
|
||||
@@ -912,6 +953,7 @@ bar"
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ true,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
/*messages*/ None,
|
||||
);
|
||||
assert_eq!(description.matches("## mcp__sample").count(), 1);
|
||||
@@ -963,6 +1005,7 @@ bar"
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ true,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
/*messages*/ None,
|
||||
);
|
||||
assert_eq!(description.matches("## mcp__sample").count(), 2);
|
||||
@@ -996,6 +1039,7 @@ bar"
|
||||
"properties": {},
|
||||
"additionalProperties": false
|
||||
})),
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: Some(mcp_call_tool_result_schema(json!({
|
||||
"type": "object",
|
||||
@@ -1010,6 +1054,7 @@ bar"
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
/*messages*/ None,
|
||||
);
|
||||
|
||||
@@ -1029,6 +1074,7 @@ bar"
|
||||
"properties": {},
|
||||
"additionalProperties": false
|
||||
})),
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: Some(json!({
|
||||
"type": "object",
|
||||
@@ -1064,6 +1110,7 @@ bar"
|
||||
"properties": {},
|
||||
"additionalProperties": false
|
||||
})),
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: Some(json!({
|
||||
"type": "object",
|
||||
@@ -1098,6 +1145,7 @@ bar"
|
||||
description: "First tool".to_string(),
|
||||
kind: first_tool.kind,
|
||||
input_schema: first_tool.input_schema,
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: first_tool.output_schema,
|
||||
},
|
||||
@@ -1107,6 +1155,7 @@ bar"
|
||||
description: "Second tool".to_string(),
|
||||
kind: second_tool.kind,
|
||||
input_schema: second_tool.input_schema,
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: second_tool.output_schema,
|
||||
},
|
||||
@@ -1118,6 +1167,7 @@ bar"
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
/*messages*/ None,
|
||||
);
|
||||
|
||||
@@ -1142,6 +1192,7 @@ bar"
|
||||
"properties": {},
|
||||
"additionalProperties": false
|
||||
})),
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: Some(mcp_call_tool_result_schema(json!({
|
||||
"type": "object",
|
||||
@@ -1159,6 +1210,7 @@ bar"
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
/*messages*/ None,
|
||||
);
|
||||
|
||||
@@ -1175,6 +1227,7 @@ bar"
|
||||
description: "Deferred tool".to_string(),
|
||||
kind: CodeModeToolKind::Function,
|
||||
input_schema: None,
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: None,
|
||||
}];
|
||||
@@ -1184,6 +1237,7 @@ bar"
|
||||
description: super::TOOL_SEARCH_GUIDANCE.to_string(),
|
||||
kind: CodeModeToolKind::Function,
|
||||
input_schema: None,
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: None,
|
||||
};
|
||||
@@ -1205,6 +1259,7 @@ bar"
|
||||
ImageDetailVisibility::Visible,
|
||||
discovery,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
/*messages*/ None,
|
||||
);
|
||||
assert!(description.contains("Some deferred nested tools may be omitted"));
|
||||
@@ -1223,6 +1278,7 @@ bar"
|
||||
ImageDetailVisibility::Visible,
|
||||
discovery,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
/*messages*/ None,
|
||||
);
|
||||
assert_eq!(description, empty_catalog_description);
|
||||
|
||||
@@ -38,6 +38,7 @@ fn exec_override_renders_only_known_literal_placeholders() {
|
||||
image_detail_visibility,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ true,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
Some(&CodeModeToolMessages {
|
||||
exec: Some(ToolMessage {
|
||||
description: Some(" \nDefaults to 10000 ms. {{ default_exec_yield_time_ms }} ms.\n{{ image_helper }}\n{{ unknown }} {{default_exec_yield_time_ms}}\t ".into()),
|
||||
@@ -70,6 +71,7 @@ fn exec_override_preserves_empty_and_whitespace_only_text() {
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
Some(&CodeModeToolMessages {
|
||||
exec: Some(ToolMessage {
|
||||
description: Some(description_override.into()),
|
||||
@@ -102,6 +104,7 @@ fn exec_override_preserves_runtime_sections() {
|
||||
description: "First tool".to_string(),
|
||||
kind: CodeModeToolKind::Function,
|
||||
input_schema: None,
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: None,
|
||||
}];
|
||||
@@ -111,6 +114,7 @@ fn exec_override_preserves_runtime_sections() {
|
||||
description: "Deferred tool".to_string(),
|
||||
kind: CodeModeToolKind::Function,
|
||||
input_schema: None,
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: Some(json!({
|
||||
"type": "object",
|
||||
@@ -161,6 +165,7 @@ fn exec_override_preserves_runtime_sections() {
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
Some(&CodeModeToolMessages {
|
||||
exec: Some(ToolMessage {
|
||||
description: Some("".into()),
|
||||
@@ -185,6 +190,7 @@ fn ranked_search_guidance_honors_overrides() {
|
||||
description: "Deferred tool".to_string(),
|
||||
kind: CodeModeToolKind::Function,
|
||||
input_schema: None,
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: None,
|
||||
}];
|
||||
@@ -210,6 +216,7 @@ fn ranked_search_guidance_honors_overrides() {
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::RankedSearch,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
Some(&CodeModeToolMessages {
|
||||
exec: Some(ToolMessage {
|
||||
description: Some("".into()),
|
||||
@@ -233,6 +240,7 @@ fn mcp_types_stay_stable_when_a_deferred_tool_changes_its_output_schema() {
|
||||
description: "Deferred tool".to_string(),
|
||||
kind: CodeModeToolKind::Function,
|
||||
input_schema: None,
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: None,
|
||||
};
|
||||
@@ -246,6 +254,7 @@ fn mcp_types_stay_stable_when_a_deferred_tool_changes_its_output_schema() {
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
Some(&CodeModeToolMessages {
|
||||
mcp_typescript_preamble: preamble.map(Into::into),
|
||||
..Default::default()
|
||||
@@ -278,6 +287,7 @@ fn nested_guidance_survives_catalog_changes_and_respects_overrides() {
|
||||
description: "Echo a value.".to_string(),
|
||||
kind: CodeModeToolKind::Function,
|
||||
input_schema: None,
|
||||
signature: None,
|
||||
input_schema_max_bytes: None,
|
||||
output_schema: None,
|
||||
}];
|
||||
@@ -291,6 +301,7 @@ fn nested_guidance_survives_catalog_changes_and_respects_overrides() {
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Legacy,
|
||||
Some(&CodeModeToolMessages {
|
||||
exec: Some(ToolMessage {
|
||||
description: Some("".into()),
|
||||
@@ -316,3 +327,103 @@ fn nested_guidance_survives_catalog_changes_and_respects_overrides() {
|
||||
}
|
||||
assert!(DEFERRED_NESTED_TOOLS_GUIDANCE.contains("Tool availability can change between calls"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exec_catalog_guidance_matches_the_selected_format() {
|
||||
let catalog_guidance = |format| {
|
||||
let (description, _) = build_exec_tool_description_and_definitions(
|
||||
&[],
|
||||
&[],
|
||||
&BTreeMap::new(),
|
||||
crate::DEFAULT_EXEC_YIELD_TIME_MS,
|
||||
/*code_mode_only*/ false,
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
format,
|
||||
/*messages*/ None,
|
||||
);
|
||||
description
|
||||
.lines()
|
||||
.filter(|line| {
|
||||
line.starts_with("- Nested tool")
|
||||
|| line.starts_with("- `ALL_TOOLS`")
|
||||
|| line.starts_with("To find one,")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
};
|
||||
|
||||
insta::assert_snapshot!(catalog_guidance(crate::ToolCatalogFormat::Legacy), @r"
|
||||
- Nested tool methods take either a string or an object as their input argument.
|
||||
- Nested tools return either an object or a string, based on the description.
|
||||
- `ALL_TOOLS`: metadata for the enabled nested tools as `{ name, description }` entries.
|
||||
To find one, filter `ALL_TOOLS` by `name` and `description`.
|
||||
");
|
||||
insta::assert_snapshot!(catalog_guidance(crate::ToolCatalogFormat::Structured), @r"
|
||||
- Nested tools take a string or an object. Each tool’s signature shows its argument and return types.
|
||||
- `ALL_TOOLS`: metadata for the enabled nested tools as `{ name, description, signature }` entries.
|
||||
To find one, filter `ALL_TOOLS` by `name` and `description`.
|
||||
");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structured_catalog_guidance_accompanies_custom_exec_descriptions() {
|
||||
let (description, _) = build_exec_tool_description_and_definitions(
|
||||
&[],
|
||||
&[],
|
||||
&BTreeMap::new(),
|
||||
crate::DEFAULT_EXEC_YIELD_TIME_MS,
|
||||
/*code_mode_only*/ false,
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ false,
|
||||
crate::ToolCatalogFormat::Structured,
|
||||
Some(&CodeModeToolMessages {
|
||||
exec: Some(ToolMessage {
|
||||
description: Some("Custom execution guidance.".into()),
|
||||
..Default::default()
|
||||
}),
|
||||
..Default::default()
|
||||
}),
|
||||
);
|
||||
assert!(description.starts_with("Custom execution guidance.\n\n"));
|
||||
assert!(description.contains("{ name, description, signature }"));
|
||||
assert!(description.ends_with(DEFERRED_NESTED_TOOLS_GUIDANCE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn catalog_format_refreshes_custom_exec_descriptions() {
|
||||
let image_signature = super::helper_signature(LEGACY_IMAGE_HELPER_DESCRIPTION).unwrap();
|
||||
for (template, original) in [
|
||||
("Run JavaScript.", ""),
|
||||
("Run JavaScript. {{ image_helper }}", image_signature),
|
||||
] {
|
||||
let messages = CodeModeToolMessages {
|
||||
exec: Some(ToolMessage {
|
||||
description: Some(template.into()),
|
||||
..Default::default()
|
||||
}),
|
||||
..Default::default()
|
||||
};
|
||||
let render = |format| {
|
||||
build_exec_tool_description_and_definitions(
|
||||
&[],
|
||||
&[],
|
||||
&BTreeMap::new(),
|
||||
crate::DEFAULT_EXEC_YIELD_TIME_MS,
|
||||
/*code_mode_only*/ true,
|
||||
ImageDetailVisibility::Visible,
|
||||
DeferredToolDiscovery::Catalog,
|
||||
/*incremental_exec_enabled*/ true,
|
||||
format,
|
||||
Some(&messages),
|
||||
)
|
||||
};
|
||||
let (_, legacy) = render(crate::ToolCatalogFormat::Legacy);
|
||||
let (description, structured) = render(crate::ToolCatalogFormat::Structured);
|
||||
assert_eq!(legacy.helper_signatures, original);
|
||||
assert_ne!(structured.helper_signatures, legacy.helper_signatures);
|
||||
assert!(!description.contains("ALL_TOOLS:signature"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,6 +97,8 @@ message SessionOpened {
|
||||
optional OutputTruncation output_truncation = 2;
|
||||
// Clients may send ExecuteRequest.catalog_revision when advertised.
|
||||
bool supports_catalog_revision = 3;
|
||||
// Whether the host exposes a separate signature on ALL_TOOLS entries.
|
||||
bool supports_tool_catalog_signature = 4;
|
||||
}
|
||||
|
||||
message CloseSessionRequest {
|
||||
@@ -299,6 +301,8 @@ message ToolDefinition {
|
||||
ToolKind kind = 4;
|
||||
optional bytes input_schema_json = 5;
|
||||
optional bytes output_schema_json = 6;
|
||||
reserved 7;
|
||||
optional string signature = 8;
|
||||
}
|
||||
|
||||
message ToolName {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
mod catalog;
|
||||
mod description;
|
||||
pub mod grpc;
|
||||
pub mod host;
|
||||
@@ -8,6 +9,7 @@ mod runtime;
|
||||
mod session;
|
||||
mod shared_catalog;
|
||||
|
||||
pub use catalog::ToolCatalogFormat;
|
||||
pub use description::CODE_MODE_PRAGMA_PREFIX;
|
||||
pub use description::CodeModeToolKind;
|
||||
pub use description::DeferredToolDiscovery;
|
||||
@@ -26,6 +28,7 @@ pub use description::is_code_mode_nested_tool;
|
||||
pub use description::normalize_code_mode_identifier;
|
||||
pub use description::parse_exec_source;
|
||||
pub use description::render_code_mode_sample;
|
||||
pub use description::render_code_mode_signature;
|
||||
pub use json_schema_types::DEFAULT_INPUT_SCHEMA_MAX_BYTES;
|
||||
pub use json_schema_types::render_json_schema_to_typescript;
|
||||
pub use process_metrics::CodeModeProcessMetrics;
|
||||
|
||||
Reference in New Issue
Block a user