Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions desktop-tauri/src-tauri/server/src/server/core/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ pub fn is_token_key(key: &str) -> bool {
key == "maxInputTokens" || key == "maxOutputTokens"
}

/// 条目的工具调用能力(发前预剥的读数):`Some(false)` = 明确不支持;
/// `Some(true)` = 明确支持;`None` = 没声明(缺失 / null / 非 bool)或条目缺失。
///
/// 调用方注意 `None ≠ Some(false)`:没声明的不当成不支持,只走模型名启发。
pub fn tool_call_support(item: &Value) -> Option<bool> {
item.get("supportsToolCall").and_then(Value::as_bool)
}

/// 该键是不是布尔能力键。
pub fn is_bool_key(key: &str) -> bool {
key == "supportsToolCall"
Expand Down
65 changes: 65 additions & 0 deletions desktop-tauri/src-tauri/server/src/server/core/model_rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,41 @@ pub(crate) fn save(rules: &ModelRules) -> bool {

use bindings::set_membership;

/// 一次工具降级的预剥判定(发前闸门用):`Strip` = 现在就剥(不动上游)。
///
/// 只有 `supportsToolCall == Some(false)` 才给 `Strip`:那是目录里**明确声明**
/// 不支持。`None`(没声明 / null / 非 bool)**不是**不支持,走模型名启发
/// (`model_suggests_no_tools`);`Some(true)` 不剥。
pub(crate) enum ToolStrip {
Strip,
Keep,
}

pub(crate) fn tool_strip_for(provider: &str, wire_model: &str) -> ToolStrip {
let item = crate::server::core::providers::catalog::manifest_for_provider(provider)
.into_iter()
.find(|entry| {
crate::server::core::models::model_id(entry).eq_ignore_ascii_case(wire_model.trim())
});
match item.as_ref().map(crate::server::core::capability::tool_call_support) {
Some(Some(false)) => ToolStrip::Strip,
Some(Some(true)) => ToolStrip::Keep,
Some(None) | None => {
if model_suggests_no_tools(wire_model) { ToolStrip::Strip } else { ToolStrip::Keep }
}
}
}

/// 模型名启发:ocr / embedding / reranker 类名字按不支持工具处理。
///
/// 只在能力位**没声明**时用(`None ≠ false`,见 `tool_strip_for`)—— 有明确
/// 声明的一律以声明为准。大小写不敏感;匹配子串而非整词(`text-embedding-3`
/// 这类带前后缀的名字同样命中)。
fn model_suggests_no_tools(model: &str) -> bool {
let lower = model.trim().to_ascii_lowercase();
lower.contains("ocr") || lower.contains("embed") || lower.contains("rerank")
}

/// **上游模型 id / 自生成别名**的字符集口径:字母 / 数字 / `- _ . / :`。
///
/// 只用在两处 —— 手动登记的上游模型 id(`api::model_manage` 的 `/api/models/custom`)
Expand Down Expand Up @@ -761,3 +796,33 @@ pub fn custom_models_for(provider: &str) -> Vec<Value> {
})
.collect()
}

#[cfg(test)]
mod tool_strip_tests {
use super::model_suggests_no_tools;
use crate::server::core::capability::tool_call_support;

/// 模型名启发:ocr / embedding / reranker 命中,其余不命中。
#[test]
fn name_heuristic_hits_ocr_embedding_reranker() {
for name in ["ocr-pro", "text-embedding-3", "reranker-v2", "OCR"] {
assert!(model_suggests_no_tools(name), "{name} 应当启发为不支持工具");
}
for name in ["gpt-4o", "deepseek-v4", ""] {
assert!(!model_suggests_no_tools(name), "{name} 不该被启发");
}
}

/// 能力位读数:Some(false) 明确不支持;None(缺失/null/非 bool)不是不支持。
#[test]
fn tool_call_support_distinguishes_none_from_false() {
let no = serde_json::json!({"id": "x", "supportsToolCall": false});
assert_eq!(tool_call_support(&no), Some(false));
let missing = serde_json::json!({"id": "x"});
assert_eq!(tool_call_support(&missing), None);
let null = serde_json::json!({"id": "x", "supportsToolCall": null});
assert_eq!(tool_call_support(&null), None);
let yes = serde_json::json!({"id": "x", "supportsToolCall": true});
assert_eq!(tool_call_support(&yes), Some(true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub mod native_tool;
pub mod responses;
pub mod responses_outbound;
mod responses_phase;
pub mod tool_args;
pub mod tool_plan;
pub mod usage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,51 @@ pub fn strip_cross_protocol(body: &Value, target: &str) -> Option<(Value, Downgr
})
}

/// 不支持工具的上游:整份 `tools` 全剥 + `tool_choice` 能降级就降。
///
/// 与 [`downgrade`] 的区别:那道闸门只剔原生声明、保留 `type:"function"`;
/// 这里是「这家**什么工具都不接**」—— function 留着同样整轮 400。
/// `tool_choice` 只降 `auto`(字符串 `"auto"` 或 `{type:"auto"}` 这类无点名形态),
/// 点名具体工具的一律撤掉(工具都没了,点名必是「指定的工具不存在」)。
pub fn strip_all_tools(body: &Value) -> Option<(Value, Downgrade)> {
let mut next = body.clone();
let mut dropped: Vec<Value> = Vec::new();
if let Some(tools) = body.get("tools").and_then(Value::as_array) {
if !tools.is_empty() {
dropped = tools.clone();
if let Some(object) = next.as_object_mut() {
object.remove("tools");
}
}
}
let mut choice: Option<String> = None;
if let Some(current) = next.get("tool_choice").cloned() {
if super::string_field(&current, "type").eq_ignore_ascii_case("auto")
|| current.as_str().is_some_and(|text| text.trim().eq_ignore_ascii_case("auto"))
{
if let Some(object) = next.as_object_mut() {
object.insert("tool_choice".to_string(), Value::String("auto".to_string()));
}
choice = Some("不支持工具的上游,tool_choice 降为 auto".to_string());
} else if let Some(reason) = choice_conflict(&current, &dropped) {
if let Some(object) = next.as_object_mut() {
object.remove("tool_choice");
}
choice = Some(reason);
} else if next.get("tool_choice").is_some() {
// 非 auto 又没点中被剔工具:工具都没了,留着它同样整轮 400。
if let Some(object) = next.as_object_mut() {
object.remove("tool_choice");
}
choice = Some("tools 已整份剥离,tool_choice 一并撤掉".to_string());
}
}
if dropped.is_empty() && choice.is_none() {
return None;
}
Some((next, Downgrade { tools: dropped, choice }))
}

/// 剔除与 `tool_choice` 撤回的共同实现(两个口径只差 `should_drop` 判据)。
fn strip_with<F: Fn(&Value) -> bool>(body: &Value, should_drop: F) -> Option<(Value, Downgrade)> {
let mut next = body.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use serde_json::{json, Map, Value};

use super::{
content_parts, content_text, event_frame, freeform, is_truthy, json_text, native_tool,
random_id, responses_phase, string_field, string_value, tool_plan, SseLineBuffer, TOOL_IMAGE_PLACEHOLDER,
random_id, responses_phase, string_field, string_value, tool_args, tool_plan, SseLineBuffer, TOOL_IMAGE_PLACEHOLDER,
FIELD_ENCRYPTED_CONTENT,
};
use crate::server::logging;
Expand Down Expand Up @@ -1046,12 +1046,19 @@ pub fn responses_from_chat(
output.push(item);
}
if let Some(calls) = tool_calls {
// 引号数字归一化(`server-shim` v2.7 的干净版):回程 arguments 原样
// 透传时,flash 级别模型发的 `{"limit":"10"}` 会在客户端侧严格反序列化
// 失败。映射表只认本次请求的 tools 声明(见 `tool_args` 模块头),
// 修不了的保持原样 —— 最坏只是不生效,永远不坏流。
let numerics = tool_args::NumericFields::build_numeric_map(request);
for call in calls {
let name = call.pointer("/function/name").map(string_value).unwrap_or_default();
let arguments = call
.pointer("/function/arguments")
.map(json_text)
.unwrap_or_else(|| "{}".to_string());
let arguments = tool_args::fix_args_str(&numerics, &name, &arguments)
.unwrap_or(arguments);
let call_id = {
let raw = string_field(call, "id");
if raw.is_empty() { random_id("call") } else { raw }
Expand Down Expand Up @@ -1554,6 +1561,14 @@ impl ResponsesStream {

let is_custom = self.plan.is_custom(&name);
if announced_now {
// 引号数字归一化(`server-shim` v2.7 的干净版):宣告前攒下的
// 参数是完整 JSON(名字与参数同一帧到达时),单 call 可解析即修;
// 多 call / 不完整保持原样 —— 只丢修复不坏流(见 `tool_args`)。
let numerics = tool_args::NumericFields::build_numeric_map(&self.request);
let buffered = (!is_custom)
.then(|| tool_args::fix_args_str(&numerics, &name, &buffered))
.flatten()
.unwrap_or(buffered);
// 自由文本工具的「参数」是裸文本 input,item 形态与 function 不同
// —— 见官方 custom_tool_call 项的定义
let item = if is_custom {
Expand Down Expand Up @@ -1820,6 +1835,18 @@ impl ResponsesStream {
// 但也不能丢 —— 补一个占位名,否则客户端少一次工具调用
let name = if tool.name.is_empty() { "unknown".to_string() } else { tool.name.clone() };
let arguments = if tool.arguments.is_empty() { "{}".to_string() } else { tool.arguments.clone() };
// 流式收尾同一归一化:`close_tools` 拿到的是完整 arguments,
// 单 call 可解析即修,多 call / 残缺保持原样(见 `tool_args`)。
let arguments = (!self.plan.is_custom(&name))
.then(|| {
tool_args::fix_args_str(
&tool_args::NumericFields::build_numeric_map(&self.request),
&name,
&arguments,
)
})
.flatten()
.unwrap_or(arguments);
// 用统一口径构造 item:custom 命中时要输出 custom_tool_call 与
// 裸文本 input,类型给错客户端就按 JSON 解析,工具照样跑不起来
let item = {
Expand Down Expand Up @@ -2042,6 +2069,14 @@ impl ResponsesCollector {
let name = if tool.name.is_empty() { "unknown".to_string() } else { tool.name };
let call_id = if tool.call_id.is_empty() { random_id("call") } else { tool.call_id };
let arguments = if tool.arguments.is_empty() { "{}".to_string() } else { tool.arguments };
// 聚合出口同一归一化:聚合的是完整 JSON,单 call 可解析即修,
// 修不了保持原样(见 `tool_args` 模块头)。
let arguments = tool_args::fix_args_str(
&tool_args::NumericFields::build_numeric_map(request),
&name,
&arguments,
)
.unwrap_or(arguments);
// 与另外两个回程出口共用口径:custom 命中时输出 custom_tool_call
// (item id 的前缀由 tool_call_item 内部按类型选,不必在这里管)
output.push(tool_call_item(&name, &call_id, &arguments, &plan));
Expand Down
Loading
Loading