Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Fix qwen parser (#806)

Aleksandr Golokozdc855efe1aad
diff --git a/crates/legacy/cli/src/server/chat_tool_calls.rs b/crates/legacy/cli/src/server/chat_tool_calls.rs
index f573a549e..8467cded6 100644
--- a/crates/legacy/cli/src/server/chat_tool_calls.rs
+++ b/crates/legacy/cli/src/server/chat_tool_calls.rs
@@ -355,6 +355,7 @@ pub struct ToolCallStreamer {
name_announced: bool,
key_order: Vec<String>,
open_text: String,
+ final_candidate: Option<String>,
}
enum FramedParam {
@@ -519,6 +520,7 @@ impl ToolCallStreamer {
name_announced: false,
key_order: Vec::new(),
open_text: String::new(),
+ final_candidate: None,
}
}
@@ -534,20 +536,22 @@ impl ToolCallStreamer {
let call = parse_framed_call(raw);
name = call.name.filter(|name| !name.is_empty());
self.key_order = call.params.iter().map(|(key, _)| key.clone()).collect();
- if let Some(open_text) = open_arguments_text(&call.params, name.as_deref(), types)
- && open_text.len() > self.open_text.len()
- && open_text.starts_with(&self.open_text)
- {
- deltas.push(OaiToolCall {
- index: Some(index),
- id: String::new(),
- kind: String::new(),
- function: OaiFunctionCall {
- name: String::new(),
- arguments: open_text[self.open_text.len()..].to_string(),
- },
- });
- self.open_text = open_text;
+ if let Some(open_text) = open_arguments_text(&call.params, name.as_deref(), types) {
+ if call.params.iter().all(|(_, param)| matches!(param, FramedParam::Complete(_))) {
+ self.final_candidate = Some(format!("{open_text}}}"));
+ }
+ if open_text.len() > self.open_text.len() && open_text.starts_with(&self.open_text) {
+ deltas.push(OaiToolCall {
+ index: Some(index),
+ id: String::new(),
+ kind: String::new(),
+ function: OaiFunctionCall {
+ name: String::new(),
+ arguments: open_text[self.open_text.len()..].to_string(),
+ },
+ });
+ self.open_text = open_text;
+ }
}
} else {
name = json_candidate_name(raw);
@@ -625,6 +629,15 @@ impl ToolCallStreamer {
match final_text.strip_prefix(&self.open_text) {
Some(suffix) => suffix.to_string(),
None => {
+ if let Some(candidate) = self.final_candidate.as_deref()
+ && serde_json::from_str::<serde_json::Value>(candidate).is_ok()
+ && let Some(suffix) = candidate.strip_prefix(&self.open_text)
+ {
+ eprintln!(
+ "[server] tool call stream diverged from the final call; using the structurally parsed candidate"
+ );
+ return suffix.to_string();
+ }
eprintln!(
"[server] tool call stream diverged from the final call; the client may reject the assembled arguments"
);
diff --git a/crates/legacy/cli/unit/server/chat_tool_calls_test.rs b/crates/legacy/cli/unit/server/chat_tool_calls_test.rs
index 332d838d0..f6589cf15 100644
--- a/crates/legacy/cli/unit/server/chat_tool_calls_test.rs
+++ b/crates/legacy/cli/unit/server/chat_tool_calls_test.rs
@@ -322,6 +322,28 @@ fn framed_tool_call_streams_multibyte_content_without_panicking() {
assert_eq!(parsed, serde_json::json!({ "content": "London — a city" }));
}
+#[test]
+fn framed_tool_call_uses_valid_candidate_when_final_call_diverges() {
+ let markup = "<function=write_file>\n<parameter=path>\nrepro.rs\n</parameter>\n<parameter=content>\nlet x = \"hello\";\n</parameter>\n</function>";
+ let corrupted_call = ToolCall {
+ identifier: None,
+ name: "write_file".to_string(),
+ arguments: Value {
+ json: r#"{"path":"repro.rs","content":"let x = ","hello\";\"":""}"#.to_string(),
+ },
+ };
+
+ let mut streamer = ToolCallStreamer::new();
+ let mut fragments = String::new();
+ for delta in streamer.update(0, markup, &ToolParameterTypes::default()) {
+ fragments.push_str(&delta.function.arguments);
+ }
+ fragments.push_str(&streamer.finish(0, &corrupted_call).function.arguments);
+
+ let parsed: serde_json::Value = serde_json::from_str(&fragments).expect("assembled arguments parse");
+ assert_eq!(parsed, serde_json::json!({"path": "repro.rs", "content": "let x = \"hello\";"}));
+}
+
#[test]
fn framed_tool_call_with_array_parameter_assembles_exactly() {
// the edit-call shape: a parameter whose value is a JSON array must never be
diff --git a/crates/legacy/hanashi/configs/parsing/qwen3.5.json b/crates/legacy/hanashi/configs/parsing/qwen3.5.json
index cf3980016..d551de45f 100644
--- a/crates/legacy/hanashi/configs/parsing/qwen3.5.json
+++ b/crates/legacy/hanashi/configs/parsing/qwen3.5.json
@@ -70,35 +70,9 @@
],
"arguments": [
{
- "type": "regex_find_all",
- "pattern": "<parameter=\\w+>[\\s\\S]*?</parameter>"
- },
- {
- "type": "each",
- "apply": [
- {
- "type": "regex_replace",
- "pattern": "<parameter=(\\w+)>\\n([{\\[][\\s\\S]*?[}\\]])\\n</parameter>",
- "template": "\"$1\": $2"
- },
- {
- "type": "regex_replace",
- "pattern": "<parameter=(\\w+)>\\n([\\s\\S]*?)\\n</parameter>",
- "template": "\"$1\": \"$2\""
- }
- ]
- },
- {
- "type": "join",
- "separator": ", "
- },
- {
- "type": "format",
- "template": "{ {} }"
- },
- {
- "type": "parse_json",
- "repair": true
+ "type": "regex_capture_object",
+ "pattern": "<parameter=(\\w+)>\\n([\\s\\S]*?)\\n</parameter>",
+ "parse_json_values": true
}
]
}
diff --git a/crates/legacy/hanashi/configs/parsing/qwen3.6.json b/crates/legacy/hanashi/configs/parsing/qwen3.6.json
index cf3980016..d551de45f 100644
--- a/crates/legacy/hanashi/configs/parsing/qwen3.6.json
+++ b/crates/legacy/hanashi/configs/parsing/qwen3.6.json
@@ -70,35 +70,9 @@
],
"arguments": [
{
- "type": "regex_find_all",
- "pattern": "<parameter=\\w+>[\\s\\S]*?</parameter>"
- },
- {
- "type": "each",
- "apply": [
- {
- "type": "regex_replace",
- "pattern": "<parameter=(\\w+)>\\n([{\\[][\\s\\S]*?[}\\]])\\n</parameter>",
- "template": "\"$1\": $2"
- },
- {
- "type": "regex_replace",
- "pattern": "<parameter=(\\w+)>\\n([\\s\\S]*?)\\n</parameter>",
- "template": "\"$1\": \"$2\""
- }
- ]
- },
- {
- "type": "join",
- "separator": ", "
- },
- {
- "type": "format",
- "template": "{ {} }"
- },
- {
- "type": "parse_json",
- "repair": true
+ "type": "regex_capture_object",
+ "pattern": "<parameter=(\\w+)>\\n([\\s\\S]*?)\\n</parameter>",
+ "parse_json_values": true
}
]
}
diff --git a/crates/legacy/json-transform/src/execution/operation.rs b/crates/legacy/json-transform/src/execution/operation.rs
index 1dc440828..85fabfbfb 100644
--- a/crates/legacy/json-transform/src/execution/operation.rs
+++ b/crates/legacy/json-transform/src/execution/operation.rs
@@ -150,6 +150,17 @@ pub enum Operation {
#[serde(default)]
regex_engine: RegexEngine,
},
+ /// Build an object from regex matches. Capture group 1 is the key and
+ /// capture group 2 is the value. Values are strings unless `parse_json_values`
+ /// is enabled and the captured text is a JSON object or array.
+ /// String → Object
+ RegexCaptureObject {
+ pattern: String,
+ #[serde(default)]
+ parse_json_values: bool,
+ #[serde(default)]
+ regex_engine: RegexEngine,
+ },
/// Split a string at separators outside quoted strings and nested structures.
/// String → Array<String>
SplitTopLevel {
@@ -243,6 +254,9 @@ impl fmt::Display for Operation {
Self::RegexFindAll {
..
} => write!(formatter, "regex_find_all"),
+ Self::RegexCaptureObject {
+ ..
+ } => write!(formatter, "regex_capture_object"),
Self::SplitTopLevel {
separator,
} => write!(formatter, "split_top_level({separator})"),
@@ -381,6 +395,11 @@ impl Operation {
pattern,
regex_engine,
} => string::execute_regex_find_all(pattern, regex_engine, input),
+ Self::RegexCaptureObject {
+ pattern,
+ parse_json_values,
+ regex_engine,
+ } => string::execute_regex_capture_object(pattern, *parse_json_values, regex_engine, input),
Self::SplitTopLevel {
separator,
} => string::execute_split_top_level(*separator, input),
diff --git a/crates/legacy/json-transform/src/execution/string.rs b/crates/legacy/json-transform/src/execution/string.rs
index 573dbbbac..5a6f72880 100644
--- a/crates/legacy/json-transform/src/execution/string.rs
+++ b/crates/legacy/json-transform/src/execution/string.rs
@@ -56,6 +56,37 @@ pub fn execute_regex_find_all(
Ok(Value::Array(matches))
}
+/// Extracts repeated key/value captures directly into a JSON object. Building
+/// the object structurally is important when values contain quotes, newlines,
+/// or backslashes: interpolating those captures into JSON text loses their
+/// boundaries before a repair pass can recover them.
+#[tracing::instrument(skip_all)]
+pub fn execute_regex_capture_object(
+ pattern: &str,
+ parse_json_values: bool,
+ regex_engine: &RegexEngine,
+ input: Value,
+) -> Result<Value, TransformError> {
+ let Value::String(text) = input else {
+ return Ok(Value::Null);
+ };
+ let regex = Regex::new(pattern, regex_engine)?;
+ let mut object = serde_json::Map::new();
+ for captures in regex.captures_iter(&text) {
+ let (Some(key), Some(value)) = (captures.get(1), captures.get(2)) else {
+ continue;
+ };
+ let trimmed = value.text.trim_start();
+ let value = if parse_json_values && (trimmed.starts_with('{') || trimmed.starts_with('[')) {
+ execute_parse_json(true, Value::String(value.text.clone()))?
+ } else {
+ Value::String(value.text.clone())
+ };
+ object.insert(key.text.clone(), value);
+ }
+ Ok(Value::Object(object))
+}
+
#[tracing::instrument(skip(input))]
pub fn execute_split_top_level(
separator: char,
diff --git a/crates/legacy/json-transform/tests/test_json_transform_string.rs b/crates/legacy/json-transform/tests/test_json_transform_string.rs
index c1c776e60..e002737c1 100644
--- a/crates/legacy/json-transform/tests/test_json_transform_string.rs
+++ b/crates/legacy/json-transform/tests/test_json_transform_string.rs
@@ -196,6 +196,32 @@ fn test_regex_find_all_then_each_parse() {
assert_eq!(result, json!([{"a": 1}, {"b": 2}]));
}
+#[test]
+fn test_regex_capture_object_preserves_string_boundaries() {
+ let result = execute_root(
+ vec![Operation::RegexCaptureObject {
+ pattern: r"<parameter=(\w+)>\n([\s\S]*?)\n</parameter>".to_string(),
+ parse_json_values: true,
+ regex_engine: RegexEngine::Standard,
+ }],
+ json!(
+ "<parameter=path>\nrepro.rs\n</parameter>\n\
+ <parameter=content>\nlet x = \"hello\";\nC:\\tmp\n</parameter>\n\
+ <parameter=options>\n{\"enabled\":true}\n</parameter>"
+ ),
+ )
+ .unwrap();
+
+ assert_eq!(
+ result,
+ json!({
+ "path": "repro.rs",
+ "content": "let x = \"hello\";\nC:\\tmp",
+ "options": {"enabled": true}
+ })
+ );
+}
+
#[test]
fn test_regex_find_all_with_each_parse_json() {
let result = execute_root(