2025-08-05 19:27:52 -07:00
|
|
|
|
use serde::Deserialize;
|
2025-05-30 14:07:03 -07:00
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
use std::collections::BTreeMap;
|
2025-08-05 19:27:52 -07:00
|
|
|
|
use std::collections::HashMap;
|
2025-05-30 14:07:03 -07:00
|
|
|
|
|
2025-08-04 23:50:03 -07:00
|
|
|
|
use crate::model_family::ModelFamily;
|
2025-07-29 11:22:02 -07:00
|
|
|
|
use crate::plan_tool::PLAN_TOOL;
|
2025-05-30 14:07:03 -07:00
|
|
|
|
|
2025-08-05 19:27:52 -07:00
|
|
|
|
#[derive(Debug, Clone, Serialize, PartialEq)]
|
|
|
|
|
|
pub struct ResponsesApiTool {
|
|
|
|
|
|
pub(crate) name: String,
|
|
|
|
|
|
pub(crate) description: String,
|
|
|
|
|
|
/// TODO: Validation. When strict is set to true, the JSON schema,
|
|
|
|
|
|
/// `required` and `additional_properties` must be present. All fields in
|
|
|
|
|
|
/// `properties` must be present in `required`.
|
2025-07-29 11:22:02 -07:00
|
|
|
|
pub(crate) strict: bool,
|
|
|
|
|
|
pub(crate) parameters: JsonSchema,
|
2025-05-30 14:07:03 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// When serialized as JSON, this produces a valid "Tool" in the OpenAI
|
|
|
|
|
|
/// Responses API.
|
2025-08-05 19:27:52 -07:00
|
|
|
|
#[derive(Debug, Clone, Serialize, PartialEq)]
|
2025-05-30 14:07:03 -07:00
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
|
|
pub(crate) enum OpenAiTool {
|
|
|
|
|
|
#[serde(rename = "function")]
|
|
|
|
|
|
Function(ResponsesApiTool),
|
|
|
|
|
|
#[serde(rename = "local_shell")]
|
|
|
|
|
|
LocalShell {},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-05 19:27:52 -07:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
|
pub enum ConfigShellToolType {
|
|
|
|
|
|
DefaultShell,
|
|
|
|
|
|
LocalShell,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
|
pub struct ToolsConfig {
|
|
|
|
|
|
pub shell_type: ConfigShellToolType,
|
|
|
|
|
|
pub plan_tool: bool,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ToolsConfig {
|
|
|
|
|
|
pub fn new(model_family: &ModelFamily, include_plan_tool: bool) -> Self {
|
|
|
|
|
|
let shell_type = if model_family.uses_local_shell_tool {
|
|
|
|
|
|
ConfigShellToolType::LocalShell
|
|
|
|
|
|
} else {
|
|
|
|
|
|
ConfigShellToolType::DefaultShell
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
|
shell_type,
|
|
|
|
|
|
plan_tool: include_plan_tool,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-30 14:07:03 -07:00
|
|
|
|
/// Generic JSON‑Schema subset needed for our tool definitions
|
2025-08-05 19:27:52 -07:00
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
2025-05-30 14:07:03 -07:00
|
|
|
|
#[serde(tag = "type", rename_all = "lowercase")]
|
|
|
|
|
|
pub(crate) enum JsonSchema {
|
|
|
|
|
|
String,
|
|
|
|
|
|
Number,
|
|
|
|
|
|
Array {
|
|
|
|
|
|
items: Box<JsonSchema>,
|
|
|
|
|
|
},
|
|
|
|
|
|
Object {
|
|
|
|
|
|
properties: BTreeMap<String, JsonSchema>,
|
2025-08-05 19:27:52 -07:00
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
|
required: Option<Vec<String>>,
|
|
|
|
|
|
#[serde(
|
|
|
|
|
|
rename = "additionalProperties",
|
|
|
|
|
|
skip_serializing_if = "Option::is_none"
|
|
|
|
|
|
)]
|
|
|
|
|
|
additional_properties: Option<bool>,
|
2025-05-30 14:07:03 -07:00
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-05 19:27:52 -07:00
|
|
|
|
pub(crate) fn create_shell_tool() -> OpenAiTool {
|
2025-05-30 14:07:03 -07:00
|
|
|
|
let mut properties = BTreeMap::new();
|
|
|
|
|
|
properties.insert(
|
|
|
|
|
|
"command".to_string(),
|
|
|
|
|
|
JsonSchema::Array {
|
|
|
|
|
|
items: Box::new(JsonSchema::String),
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
properties.insert("workdir".to_string(), JsonSchema::String);
|
|
|
|
|
|
properties.insert("timeout".to_string(), JsonSchema::Number);
|
|
|
|
|
|
|
2025-08-04 23:50:03 -07:00
|
|
|
|
OpenAiTool::Function(ResponsesApiTool {
|
2025-08-05 19:27:52 -07:00
|
|
|
|
name: "shell".to_string(),
|
|
|
|
|
|
description: "Runs a shell command and returns its output".to_string(),
|
2025-05-30 14:07:03 -07:00
|
|
|
|
strict: false,
|
|
|
|
|
|
parameters: JsonSchema::Object {
|
|
|
|
|
|
properties,
|
2025-08-05 19:27:52 -07:00
|
|
|
|
required: Some(vec!["command".to_string()]),
|
|
|
|
|
|
additional_properties: Some(false),
|
2025-05-30 14:07:03 -07:00
|
|
|
|
},
|
2025-08-04 23:50:03 -07:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-05-30 14:07:03 -07:00
|
|
|
|
|
|
|
|
|
|
/// 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(crate) fn create_tools_json_for_responses_api(
|
2025-08-05 19:27:52 -07:00
|
|
|
|
tools: &Vec<OpenAiTool>,
|
2025-05-30 14:07:03 -07:00
|
|
|
|
) -> crate::error::Result<Vec<serde_json::Value>> {
|
2025-08-05 19:27:52 -07:00
|
|
|
|
let mut tools_json = Vec::new();
|
2025-08-04 23:50:03 -07:00
|
|
|
|
|
2025-08-05 19:27:52 -07:00
|
|
|
|
for tool in tools {
|
2025-08-04 23:50:03 -07:00
|
|
|
|
tools_json.push(serde_json::to_value(tool)?);
|
2025-05-30 14:07:03 -07:00
|
|
|
|
}
|
2025-07-29 11:22:02 -07:00
|
|
|
|
|
2025-05-30 14:07:03 -07:00
|
|
|
|
Ok(tools_json)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns JSON values that are compatible with Function Calling in the
|
|
|
|
|
|
/// Chat Completions API:
|
|
|
|
|
|
/// https://platform.openai.com/docs/guides/function-calling?api-mode=chat
|
|
|
|
|
|
pub(crate) fn create_tools_json_for_chat_completions_api(
|
2025-08-05 19:27:52 -07:00
|
|
|
|
tools: &Vec<OpenAiTool>,
|
2025-05-30 14:07:03 -07:00
|
|
|
|
) -> crate::error::Result<Vec<serde_json::Value>> {
|
|
|
|
|
|
// We start with the JSON for the Responses API and than rewrite it to match
|
|
|
|
|
|
// the chat completions tool call format.
|
2025-08-05 19:27:52 -07:00
|
|
|
|
let responses_api_tools_json = create_tools_json_for_responses_api(tools)?;
|
2025-05-30 14:07:03 -07:00
|
|
|
|
let tools_json = responses_api_tools_json
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
.filter_map(|mut tool| {
|
|
|
|
|
|
if tool.get("type") != Some(&serde_json::Value::String("function".to_string())) {
|
|
|
|
|
|
return None;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(map) = tool.as_object_mut() {
|
|
|
|
|
|
// Remove "type" field as it is not needed in chat completions.
|
|
|
|
|
|
map.remove("type");
|
|
|
|
|
|
Some(json!({
|
|
|
|
|
|
"type": "function",
|
|
|
|
|
|
"function": map,
|
|
|
|
|
|
}))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
None
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.collect::<Vec<serde_json::Value>>();
|
|
|
|
|
|
Ok(tools_json)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-05 19:27:52 -07:00
|
|
|
|
pub(crate) fn mcp_tool_to_openai_tool(
|
2025-05-30 14:07:03 -07:00
|
|
|
|
fully_qualified_name: String,
|
|
|
|
|
|
tool: mcp_types::Tool,
|
2025-08-05 19:27:52 -07:00
|
|
|
|
) -> Result<ResponsesApiTool, serde_json::Error> {
|
2025-05-30 14:07:03 -07:00
|
|
|
|
let mcp_types::Tool {
|
|
|
|
|
|
description,
|
|
|
|
|
|
mut input_schema,
|
|
|
|
|
|
..
|
|
|
|
|
|
} = tool;
|
|
|
|
|
|
|
|
|
|
|
|
// OpenAI models mandate the "properties" field in the schema. The Agents
|
|
|
|
|
|
// SDK fixed this by inserting an empty object for "properties" if it is not
|
|
|
|
|
|
// already present https://github.com/openai/openai-agents-python/issues/449
|
|
|
|
|
|
// so here we do the same.
|
|
|
|
|
|
if input_schema.properties.is_none() {
|
|
|
|
|
|
input_schema.properties = Some(serde_json::Value::Object(serde_json::Map::new()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-05 19:27:52 -07:00
|
|
|
|
let serialized_input_schema = serde_json::to_value(input_schema)?;
|
|
|
|
|
|
let input_schema = serde_json::from_value::<JsonSchema>(serialized_input_schema)?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(ResponsesApiTool {
|
|
|
|
|
|
name: fully_qualified_name,
|
|
|
|
|
|
description: description.unwrap_or_default(),
|
|
|
|
|
|
strict: false,
|
|
|
|
|
|
parameters: input_schema,
|
2025-05-30 14:07:03 -07:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2025-08-05 19:27:52 -07:00
|
|
|
|
|
|
|
|
|
|
/// Returns a list of OpenAiTools based on the provided config and MCP tools.
|
|
|
|
|
|
/// Note that the keys of mcp_tools should be fully qualified names. See
|
|
|
|
|
|
/// [`McpConnectionManager`] for more details.
|
|
|
|
|
|
pub(crate) fn get_openai_tools(
|
|
|
|
|
|
config: &ToolsConfig,
|
|
|
|
|
|
mcp_tools: Option<HashMap<String, mcp_types::Tool>>,
|
|
|
|
|
|
) -> Vec<OpenAiTool> {
|
|
|
|
|
|
let mut tools: Vec<OpenAiTool> = Vec::new();
|
|
|
|
|
|
|
|
|
|
|
|
match config.shell_type {
|
|
|
|
|
|
ConfigShellToolType::DefaultShell => {
|
|
|
|
|
|
tools.push(create_shell_tool());
|
|
|
|
|
|
}
|
|
|
|
|
|
ConfigShellToolType::LocalShell => {
|
|
|
|
|
|
tools.push(OpenAiTool::LocalShell {});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if config.plan_tool {
|
|
|
|
|
|
tools.push(PLAN_TOOL.clone());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(mcp_tools) = mcp_tools {
|
|
|
|
|
|
for (name, tool) in mcp_tools {
|
|
|
|
|
|
match mcp_tool_to_openai_tool(name.clone(), tool.clone()) {
|
|
|
|
|
|
Ok(converted_tool) => tools.push(OpenAiTool::Function(converted_tool)),
|
|
|
|
|
|
Err(e) => {
|
|
|
|
|
|
tracing::error!("Failed to convert {name:?} MCP tool to OpenAI tool: {e:?}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tools
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
#[allow(clippy::expect_used)]
|
|
|
|
|
|
mod tests {
|
|
|
|
|
|
use crate::model_family::find_family_for_model;
|
|
|
|
|
|
use mcp_types::ToolInputSchema;
|
|
|
|
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
fn assert_eq_tool_names(tools: &[OpenAiTool], expected_names: &[&str]) {
|
|
|
|
|
|
let tool_names = tools
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.map(|tool| match tool {
|
|
|
|
|
|
OpenAiTool::Function(ResponsesApiTool { name, .. }) => name,
|
|
|
|
|
|
OpenAiTool::LocalShell {} => "local_shell",
|
|
|
|
|
|
})
|
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
|
tool_names.len(),
|
|
|
|
|
|
expected_names.len(),
|
|
|
|
|
|
"tool_name mismatch, {tool_names:?}, {expected_names:?}",
|
|
|
|
|
|
);
|
|
|
|
|
|
for (name, expected_name) in tool_names.iter().zip(expected_names.iter()) {
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
|
name, expected_name,
|
|
|
|
|
|
"tool_name mismatch, {name:?}, {expected_name:?}"
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_get_openai_tools() {
|
|
|
|
|
|
let model_family = find_family_for_model("codex-mini-latest")
|
|
|
|
|
|
.expect("codex-mini-latest should be a valid model family");
|
|
|
|
|
|
let config = ToolsConfig::new(&model_family, true);
|
|
|
|
|
|
let tools = get_openai_tools(&config, Some(HashMap::new()));
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq_tool_names(&tools, &["local_shell", "update_plan"]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_get_openai_tools_default_shell() {
|
|
|
|
|
|
let model_family = find_family_for_model("o3").expect("o3 should be a valid model family");
|
|
|
|
|
|
let config = ToolsConfig::new(&model_family, true);
|
|
|
|
|
|
let tools = get_openai_tools(&config, Some(HashMap::new()));
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq_tool_names(&tools, &["shell", "update_plan"]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_get_openai_tools_mcp_tools() {
|
|
|
|
|
|
let model_family = find_family_for_model("o3").expect("o3 should be a valid model family");
|
|
|
|
|
|
let config = ToolsConfig::new(&model_family, false);
|
|
|
|
|
|
let tools = get_openai_tools(
|
|
|
|
|
|
&config,
|
|
|
|
|
|
Some(HashMap::from([(
|
|
|
|
|
|
"test_server/do_something_cool".to_string(),
|
|
|
|
|
|
mcp_types::Tool {
|
|
|
|
|
|
name: "do_something_cool".to_string(),
|
|
|
|
|
|
input_schema: ToolInputSchema {
|
|
|
|
|
|
properties: Some(serde_json::json!({
|
|
|
|
|
|
"string_argument": {
|
|
|
|
|
|
"type": "string",
|
|
|
|
|
|
},
|
|
|
|
|
|
"number_argument": {
|
|
|
|
|
|
"type": "number",
|
|
|
|
|
|
},
|
|
|
|
|
|
"object_argument": {
|
|
|
|
|
|
"type": "object",
|
|
|
|
|
|
"properties": {
|
|
|
|
|
|
"string_property": { "type": "string" },
|
|
|
|
|
|
"number_property": { "type": "number" },
|
|
|
|
|
|
},
|
|
|
|
|
|
"required": [
|
|
|
|
|
|
"string_property",
|
|
|
|
|
|
"number_property"
|
|
|
|
|
|
],
|
|
|
|
|
|
"additionalProperties": Some(false),
|
|
|
|
|
|
},
|
|
|
|
|
|
})),
|
|
|
|
|
|
required: None,
|
|
|
|
|
|
r#type: "object".to_string(),
|
|
|
|
|
|
},
|
|
|
|
|
|
output_schema: None,
|
|
|
|
|
|
title: None,
|
|
|
|
|
|
annotations: None,
|
|
|
|
|
|
description: Some("Do something cool".to_string()),
|
|
|
|
|
|
},
|
|
|
|
|
|
)])),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq_tool_names(&tools, &["shell", "test_server/do_something_cool"]);
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
|
tools[1],
|
|
|
|
|
|
OpenAiTool::Function(ResponsesApiTool {
|
|
|
|
|
|
name: "test_server/do_something_cool".to_string(),
|
|
|
|
|
|
parameters: JsonSchema::Object {
|
|
|
|
|
|
properties: BTreeMap::from([
|
|
|
|
|
|
("string_argument".to_string(), JsonSchema::String),
|
|
|
|
|
|
("number_argument".to_string(), JsonSchema::Number),
|
|
|
|
|
|
(
|
|
|
|
|
|
"object_argument".to_string(),
|
|
|
|
|
|
JsonSchema::Object {
|
|
|
|
|
|
properties: BTreeMap::from([
|
|
|
|
|
|
("string_property".to_string(), JsonSchema::String),
|
|
|
|
|
|
("number_property".to_string(), JsonSchema::Number),
|
|
|
|
|
|
]),
|
|
|
|
|
|
required: Some(vec![
|
|
|
|
|
|
"string_property".to_string(),
|
|
|
|
|
|
"number_property".to_string(),
|
|
|
|
|
|
]),
|
|
|
|
|
|
additional_properties: Some(false),
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
]),
|
|
|
|
|
|
required: None,
|
|
|
|
|
|
additional_properties: None,
|
|
|
|
|
|
},
|
|
|
|
|
|
description: "Do something cool".to_string(),
|
|
|
|
|
|
strict: false,
|
|
|
|
|
|
})
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|