fix: sanitize provider tool names
This commit is contained in:
28
third_party/zeroclaw/src/providers/compatible.rs
vendored
28
third_party/zeroclaw/src/providers/compatible.rs
vendored
@@ -405,10 +405,11 @@ impl OpenAiCompatibleProvider {
|
||||
tools
|
||||
.iter()
|
||||
.map(|tool| {
|
||||
let provider_name = crate::tools::provider_safe_tool_name(&tool.name);
|
||||
serde_json::json!({
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": tool.name,
|
||||
"name": provider_name,
|
||||
"description": tool.description,
|
||||
"parameters": tool.parameters
|
||||
}
|
||||
@@ -1321,10 +1322,11 @@ impl OpenAiCompatibleProvider {
|
||||
items
|
||||
.iter()
|
||||
.map(|tool| {
|
||||
let provider_name = crate::tools::provider_safe_tool_name(&tool.name);
|
||||
serde_json::json!({
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": tool.name,
|
||||
"name": provider_name,
|
||||
"description": tool.description,
|
||||
"parameters": tool.parameters,
|
||||
}
|
||||
@@ -1387,7 +1389,7 @@ impl OpenAiCompatibleProvider {
|
||||
id: Some(tc.id),
|
||||
kind: Some("function".to_string()),
|
||||
function: Some(Function {
|
||||
name: Some(tc.name),
|
||||
name: Some(crate::tools::provider_safe_tool_name(&tc.name)),
|
||||
arguments: Some(tc.arguments),
|
||||
}),
|
||||
name: None,
|
||||
@@ -3233,6 +3235,26 @@ mod tests {
|
||||
assert_eq!(tools[0]["function"]["parameters"]["required"][0], "command");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_specs_convert_invalid_function_names_to_provider_safe_names() {
|
||||
let specs = vec![crate::tools::ToolSpec {
|
||||
name: "zhihu-hotlist.extract_hotlist".to_string(),
|
||||
description: "Extract Zhihu hotlist rows".to_string(),
|
||||
parameters: serde_json::json!({
|
||||
"type": "object",
|
||||
"properties": {"top_n": {"type": "string"}},
|
||||
"required": ["top_n"]
|
||||
}),
|
||||
}];
|
||||
|
||||
let tools = OpenAiCompatibleProvider::tool_specs_to_openai_format(&specs);
|
||||
assert_eq!(tools.len(), 1);
|
||||
assert_eq!(
|
||||
tools[0]["function"]["name"],
|
||||
"zhihu-hotlist_extract_hotlist"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn request_serializes_with_tools() {
|
||||
let tools = vec![serde_json::json!({
|
||||
|
||||
Reference in New Issue
Block a user