Changesets · Branches · Tags · Compare
| Changeset | 35e8565cc9bde796170bbb6cfc10eb4a512515f7946954ef6be3b7790b91c262 |
|---|---|
| Author | WaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-09-02 14:26 +08:00 |
| Committer | GitHub <noreply@github.com> · 2026-09-02 14:26 +08:00 |
| Parents | 812efb07501a |
1 file changed
modified crates/utopia-server/src/api/chat.rs +161 −4
| @@ -115,6 +115,70 @@ | ||
| 115 | 115 | tools |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | +/// 执行之前先看这次调用说清楚了没有。 | |
| 119 | +/// | |
| 120 | +/// **两种「没说清」以前都会安静地变成一次正常调用。** | |
| 121 | +/// | |
| 122 | +/// 一是参数解析不出来。模型的输出撞上 token 上限时,`arguments` 会在半路断掉, | |
| 123 | +/// 那串 JSON 不完整。从前这里是 `unwrap_or_else(|_| json!({}))`——空对象, | |
| 124 | +/// 接着 `search_chunks` 里 `args["query"].as_str().unwrap_or(&query)` 回落到 | |
| 125 | +/// **用户那句原话**,于是一次被截断的调用变成「拿用户的原问题去检索」, | |
| 126 | +/// 而轨迹上显示的是一条完全正常的 `search · 6 sources`。 | |
| 127 | +/// | |
| 128 | +/// 二是必填参数干脆没给。同一个回落,同一个结果。 | |
| 129 | +/// | |
| 130 | +/// 两种都不该猜。**回落产出的是一个看起来没问题的错误答案**,那比报错坏得多—— | |
| 131 | +/// 报错模型会重试,猜出来的答案没有人会去核。 | |
| 132 | +/// | |
| 133 | +/// 判据直接取自工具表里的 `required`:加一个必填参数,这里自动跟上, | |
| 134 | +/// 不必记得来改第二处。 | |
| 135 | +fn check_call( | |
| 136 | + tools: &serde_json::Value, | |
| 137 | + name: &str, | |
| 138 | + raw_args: &str, | |
| 139 | +) -> Result<serde_json::Value, (String, serde_json::Value)> { | |
| 140 | + let refuse = |detail: &str, message: String| { | |
| 141 | + ( | |
| 142 | + message, | |
| 143 | + json!({ "kind": "tool", "label": name, "detail": detail }), | |
| 144 | + ) | |
| 145 | + }; | |
| 146 | + let Ok(args) = serde_json::from_str::<serde_json::Value>(raw_args) else { | |
| 147 | + return Err(refuse( | |
| 148 | + "bad arguments", | |
| 149 | + format!( | |
| 150 | + "The arguments for {name} were not valid JSON, so the call was not run. \ | |
| 151 | + They were probably cut off. Call it again with complete arguments." | |
| 152 | + ), | |
| 153 | + )); | |
| 154 | + }; | |
| 155 | + let required = tools | |
| 156 | + .as_array() | |
| 157 | + .into_iter() | |
| 158 | + .flatten() | |
| 159 | + .find(|t| t["function"]["name"] == name) | |
| 160 | + .and_then(|t| t["function"]["parameters"]["required"].as_array()); | |
| 161 | + for key in required.into_iter().flatten().filter_map(|k| k.as_str()) { | |
| 162 | + // 空串与 null 都算没给:`{"query": ""}` 检索出来的东西与问题无关, | |
| 163 | + // 而它同样会显示成一条正常的轨迹 | |
| 164 | + let missing = match args.get(key) { | |
| 165 | + None | Some(serde_json::Value::Null) => true, | |
| 166 | + Some(serde_json::Value::String(s)) => s.trim().is_empty(), | |
| 167 | + Some(_) => false, | |
| 168 | + }; | |
| 169 | + if missing { | |
| 170 | + return Err(refuse( | |
| 171 | + &format!("missing {key}"), | |
| 172 | + format!( | |
| 173 | + "{name} needs `{key}`, and it was missing or empty, so the call was not \ | |
| 174 | + run. Call it again with `{key}` set." | |
| 175 | + ), | |
| 176 | + )); | |
| 177 | + } | |
| 178 | + } | |
| 179 | + Ok(args) | |
| 180 | +} | |
| 181 | + | |
| 118 | 182 | const MEMORY_PROMPT: &str = "\ |
| 119 | 183 | Memory: you can persist knowledge with the remember tool. Use it when the user says \ |
| 120 | 184 | \"remember/record this\" or states a decision meant to last. Confirm in your reply what \ |
| @@ -572,11 +636,20 @@ | ||
| 572 | 636 | |
| 573 | 637 | msgs.push(turn.to_message()); |
| 574 | 638 | for call in &turn.tool_calls { |
| 575 | - let args: serde_json::Value = | |
| 576 | - serde_json::from_str(&call.arguments).unwrap_or_else(|_| json!({})); | |
| 639 | + // **说不清自己要做什么的调用不执行。** 把话回给模型,让它重来 | |
| 640 | + let args = match check_call(&tools, &call.name, &call.arguments) { | |
| 641 | + Ok(args) => args, | |
| 642 | + Err((message, step)) => { | |
| 643 | + steps_acc.push(step.clone()); | |
| 644 | + yield Frame::new("step", serde_json::to_string(&step).unwrap_or_default()); | |
| 645 | + msgs.push(tool_result_message(&call.id, &message)); | |
| 646 | + continue; | |
| 647 | + } | |
| 648 | + }; | |
| 577 | 649 | let (result, step) = match call.name.as_str() { |
| 578 | 650 | "search_chunks" => { |
| 579 | - let q = args["query"].as_str().unwrap_or(&query).to_string(); | |
| 651 | + // check_call 已经保证 query 在且非空——这里不再有回落 | |
| 652 | + let q = args["query"].as_str().unwrap_or_default().to_string(); | |
| 580 | 653 | let chunks = retrieval::hybrid(&state, kb_id, workspace_id, &q, SEARCH_TOP_K) |
| 581 | 654 | .await |
| 582 | 655 | .unwrap_or_default(); |
| @@ -606,7 +679,8 @@ | ||
| 606 | 679 | (text, json!({ "kind": "search", "label": q, "detail": format!("{} sources", chunks.len()) })) |
| 607 | 680 | } |
| 608 | 681 | "search_docs" => { |
| 609 | - let q = args["query"].as_str().unwrap_or(&query).to_string(); | |
| 682 | + // 同上:check_call 已经拦过,这里不再回落到用户原话 | |
| 683 | + let q = args["query"].as_str().unwrap_or_default().to_string(); | |
| 610 | 684 | let hits = state.docs.search(&q, 4).unwrap_or_default(); |
| 611 | 685 | let mut lines = Vec::new(); |
| 612 | 686 | for h in &hits { |
| @@ -1353,4 +1427,87 @@ | ||
| 1353 | 1427 | "{line}" |
| 1354 | 1428 | ); |
| 1355 | 1429 | } |
| 1430 | + | |
| 1431 | + // --- check_call --------------------------------------------------------- | |
| 1432 | + | |
| 1433 | + /// 参数在半路断掉——模型撞上 token 上限时就长这样。 | |
| 1434 | + /// | |
| 1435 | + /// **从前这里回落成空对象,然后 `search_chunks` 拿用户那句原话去检索。** | |
| 1436 | + /// 得到的是一条看起来完全正常的 `search · 6 sources`,和一个基于错误输入 | |
| 1437 | + /// 的答案。没人会去核一个看起来正常的答案,所以这一条必须是拒绝 | |
| 1438 | + #[test] | |
| 1439 | + fn arguments_cut_off_mid_json_do_not_become_a_search() { | |
| 1440 | + let tools = tools_schema(false, &[]); | |
| 1441 | + let err = check_call(&tools, "search_chunks", "{\"query\": \"Acme reven") | |
| 1442 | + .expect_err("残缺 JSON 必须拒绝,而不是回落"); | |
| 1443 | + assert_eq!(err.1["kind"], "tool", "轨迹上要显示成一次没做成的调用"); | |
| 1444 | + assert_eq!(err.1["detail"], "bad arguments"); | |
| 1445 | + assert!( | |
| 1446 | + err.0.contains("not valid JSON") && err.0.contains("again"), | |
| 1447 | + "回给模型的话要说清没执行、并让它重来:{}", | |
| 1448 | + err.0 | |
| 1449 | + ); | |
| 1450 | + } | |
| 1451 | + | |
| 1452 | + /// 空串与缺字段是同一件事:`{"query": ""}` 检索回来的东西与问题无关, | |
| 1453 | + /// 而它同样会显示成一条正常轨迹 | |
| 1454 | + #[test] | |
| 1455 | + fn an_empty_required_argument_counts_as_missing() { | |
| 1456 | + let tools = tools_schema(false, &[]); | |
| 1457 | + for raw in [ | |
| 1458 | + "{}", | |
| 1459 | + "{\"query\": \"\"}", | |
| 1460 | + "{\"query\": \" \"}", | |
| 1461 | + "{\"query\": null}", | |
| 1462 | + ] { | |
| 1463 | + let Err(err) = check_call(&tools, "search_chunks", raw) else { | |
| 1464 | + panic!("{raw} 应当被拒"); | |
| 1465 | + }; | |
| 1466 | + assert_eq!(err.1["detail"], "missing query", "{raw}"); | |
| 1467 | + } | |
| 1468 | + } | |
| 1469 | + | |
| 1470 | + /// **判据取自工具表本身。** 这条守的是「加了必填参数却忘了改校验」—— | |
| 1471 | + /// query_data 只在挂了数据源时才出现在表里,它的两个必填参数 | |
| 1472 | + /// 从没在别处被单独写过一遍 | |
| 1473 | + #[test] | |
| 1474 | + fn the_schema_is_the_only_place_required_is_written_down() { | |
| 1475 | + let tools = tools_schema(false, &["warehouse".into()]); | |
| 1476 | + let err = check_call(&tools, "query_data", "{\"data_source\": \"warehouse\"}") | |
| 1477 | + .expect_err("缺 sql 必须拒绝"); | |
| 1478 | + assert_eq!(err.1["detail"], "missing sql"); | |
| 1479 | + check_call( | |
| 1480 | + &tools, | |
| 1481 | + "query_data", | |
| 1482 | + "{\"data_source\": \"warehouse\", \"sql\": \"SELECT 1\"}", | |
| 1483 | + ) | |
| 1484 | + .expect("两个都给了就该放行"); | |
| 1485 | + } | |
| 1486 | + | |
| 1487 | + /// 合规的调用原样通过,**可选参数一个不少**。 | |
| 1488 | + /// | |
| 1489 | + /// 这一关只判「说清了没有」,不做过滤——把 args 重新组装一遍的话, | |
| 1490 | + /// 加一个可选参数就得记得来这里加一次,而忘记的后果是它安静地失效 | |
| 1491 | + #[test] | |
| 1492 | + fn a_well_formed_call_passes_through_untouched() { | |
| 1493 | + let tools = tools_schema(false, &[]); | |
| 1494 | + let args = check_call( | |
| 1495 | + &tools, | |
| 1496 | + "entity_facts", | |
| 1497 | + "{\"entity_id\": \"1f8ac10b-58cc-4372-a567-0e02b2c3d479\", \"at\": \"2026-03-15\"}", | |
| 1498 | + ) | |
| 1499 | + .expect("必填给了就该放行"); | |
| 1500 | + assert_eq!(args["at"], "2026-03-15", "可选参数不能在这一关被吃掉"); | |
| 1501 | + } | |
| 1502 | + | |
| 1503 | + /// `changes` 早就在自己那一支里拒绝缺失的 `since`——**它是唯一做对的一个**。 | |
| 1504 | + /// 这条钉住两件事:新的统一关卡与它一致,而它自己那道对日期格式的检查 | |
| 1505 | + /// (`2026-13-45` 这种)仍然要留着,因为 check_call 只看有没有、不看对不对 | |
| 1506 | + #[test] | |
| 1507 | + fn the_one_tool_that_already_refused_still_refuses() { | |
| 1508 | + let tools = tools_schema(false, &[]); | |
| 1509 | + assert!(check_call(&tools, "changes", "{}").is_err()); | |
| 1510 | + check_call(&tools, "changes", "{\"since\": \"2026-13-45\"}") | |
| 1511 | + .expect("格式错的日期不归这一关管,交给 changes_window"); | |
| 1512 | + } | |
| 1356 | 1513 | } |