@@ -74,20 +74,26 @@ fn handle_single_token_tool_calls(input: &str, start_token: &str) -> String {
74
74
continue ;
75
75
}
76
76
// Only consider segments that start like JSON
77
- if s. starts_with ( '{' ) || s . starts_with ( '[' ) {
77
+ if s. starts_with ( '{' ) {
78
78
// Trim trailing non-JSON by cutting at the last closing brace/bracket
79
- if let Some ( pos) = s. rfind ( [ '}' , ']' ] ) {
80
- let candidate = & s[ ..=pos] ;
79
+ if let Some ( pos) = s. rfind ( '}' ) {
80
+ let candidate = & s[ ..=pos] . trim ( ) ;
81
81
// Keep only valid JSON candidates
82
82
if serde_json:: from_str :: < serde_json:: Value > ( candidate) . is_ok ( ) {
83
83
items. push ( candidate. to_string ( ) ) ;
84
84
}
85
85
}
86
86
}
87
87
}
88
-
89
88
if items. is_empty ( ) {
90
- return input. to_string ( ) ;
89
+ // Remove everything up to and including the first occurrence of the start token
90
+ if let Some ( idx) = input. find ( start_token) {
91
+ let rest = & input[ idx + start_token. len ( ) ..] ;
92
+ return rest. trim_start ( ) . to_string ( ) ;
93
+ } else {
94
+ // Shouldn't happen because we checked contains() above, but be defensive
95
+ return input. to_string ( ) ;
96
+ }
91
97
}
92
98
format ! ( "[{}]" , items. join( "," ) )
93
99
}
@@ -167,7 +173,7 @@ pub fn try_tool_call_parse_json(
167
173
} ;
168
174
}
169
175
170
- // Convert json to &str if it's a String, otherwise keep as &str
176
+ // Convert json ( String) to &str
171
177
let json = json. as_str ( ) ;
172
178
173
179
// Anonymous function to attempt deserialization into a known representation
0 commit comments