zai-rs 0.1.15

一个 Rust SDK, 用于调用 智普AI API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! JSON parsing helpers for LLM tool_calls responses.
//!
//! These utilities help extract tool call requests from LLM responses that
//! follow OpenAI/Zhipu-style schemas where tool calls are returned under
//! `choices[*].message.tool_calls`.

use std::borrow::Cow;

use serde_json::Value;

/// A parsed tool call request from an LLM response with zero-copy optimization.
#[derive(Debug, Clone, PartialEq)]
pub struct LlmToolCall<'a> {
    pub id: Cow<'a, str>,
    pub name: Cow<'a, str>,
    /// Raw string form of arguments if the provider returned it as a JSON
    /// string. Useful for diagnostics; may be None if provider already
    /// returned an object.
    pub arguments_raw: Option<Cow<'a, str>>,
    /// Parsed JSON arguments. For providers that return a string, we attempt to
    /// parse it. If parsing fails, we return the raw string in this field.
    pub arguments: Value,
}

/// Normalize JSON arguments for better consistency
pub fn normalize_arguments(args: &Value) -> Value {
    match args {
        Value::String(s) => {
            // Try to parse string as JSON
            serde_json::from_str(s).unwrap_or_else(|_| Value::String(s.clone()))
        },
        Value::Object(obj) => {
            // Normalize object keys: trim whitespace only, preserve case
            let mut normalized = serde_json::Map::new();
            for (k, v) in obj {
                let normalized_key = k.trim().to_string();
                normalized.insert(normalized_key, normalize_arguments(v));
            }
            Value::Object(normalized)
        },
        _ => args.clone(),
    }
}

/// Parse tool calls with better error recovery and zero-copy optimization
pub fn parse_tool_calls_robust(response: &Value) -> Vec<LlmToolCall<'_>> {
    let mut results = Vec::new();

    // Try multiple parsing strategies
    if let Some(calls) = response.get("tool_calls").and_then(|v| v.as_array()) {
        // Direct tool_calls
        results.extend(parse_tool_calls_array(calls));
    } else if let Some(choices) = response.get("choices").and_then(|v| v.as_array()) {
        // Choices format
        for choice in choices {
            if let Some(msg) = choice.get("message")
                && let Some(calls) = msg.get("tool_calls").and_then(|v| v.as_array())
            {
                results.extend(parse_tool_calls_array(calls));
            }
        }
    } else if let Some(function_call) = response.get("function_call") {
        // Legacy function_call format
        if let Some(call) = parse_legacy_function_call(function_call) {
            results.push(call);
        }
    }

    results
}

/// Parse legacy function_call format
fn parse_legacy_function_call(function_call: &Value) -> Option<LlmToolCall<'static>> {
    let name = function_call.get("name")?.as_str()?;

    let arguments = function_call
        .get("arguments")
        .cloned()
        .unwrap_or(Value::Null);

    let (arguments_raw, parsed_args) = match arguments {
        Value::String(s) => {
            let parsed = match serde_json::from_str::<Value>(&s) {
                Ok(v) => v,
                Err(_) => Value::String(s.clone()),
            };
            (Some(Cow::Owned(s)), parsed)
        },
        other => (None, other),
    };

    Some(LlmToolCall {
        id: Cow::Borrowed("legacy"),
        name: Cow::Owned(name.to_string()),
        arguments_raw,
        arguments: parsed_args,
    })
}

/// Parse tool calls array with zero-copy optimization
fn parse_tool_calls_array(calls: &[Value]) -> Vec<LlmToolCall<'_>> {
    let mut out = Vec::new();

    for tc in calls {
        let Some(id) = tc.get("id").and_then(|v| v.as_str()) else {
            continue;
        };
        let Some(func) = tc.get("function").and_then(|v| v.as_object()) else {
            continue;
        };
        let Some(name) = func.get("name").and_then(|v| v.as_str()) else {
            continue;
        };

        let (arguments_raw, arguments) = match func.get("arguments") {
            Some(Value::String(s)) => {
                // Provider returned stringified JSON; try to parse.
                match serde_json::from_str::<Value>(s) {
                    Ok(v) => (Some(Cow::Borrowed(s.as_str())), v),
                    Err(_) => (Some(Cow::Borrowed(s.as_str())), Value::String(s.clone())),
                }
            },
            Some(v @ Value::Object(_)) | Some(v @ Value::Array(_)) => (None, v.clone()),
            Some(v) => {
                // Unexpected primitive; keep as-is for robustness.
                (None, v.clone())
            },
            None => (None, Value::Null),
        };

        out.push(LlmToolCall {
            id: Cow::Borrowed(id),
            name: Cow::Owned(name.to_string()),
            arguments_raw,
            arguments,
        });
    }

    out
}

/// Parse all tool calls from a single assistant message object with zero-copy
/// optimization.
pub fn parse_tool_calls_from_message(message: &Value) -> Vec<LlmToolCall<'_>> {
    let mut out = Vec::new();
    let Some(calls) = message.get("tool_calls").and_then(|v| v.as_array()) else {
        return out;
    };

    for tc in calls {
        let Some(id) = tc.get("id").and_then(|v| v.as_str()) else {
            continue;
        };
        let Some(func) = tc.get("function").and_then(|v| v.as_object()) else {
            continue;
        };
        let Some(name) = func.get("name").and_then(|v| v.as_str()) else {
            continue;
        };

        let (arguments_raw, arguments) = match func.get("arguments") {
            Some(Value::String(s)) => {
                // Provider returned stringified JSON; try to parse.
                match serde_json::from_str::<Value>(s) {
                    Ok(v) => (Some(Cow::Borrowed(s.as_str())), v),
                    Err(_) => (Some(Cow::Borrowed(s.as_str())), Value::String(s.clone())),
                }
            },
            Some(v @ Value::Object(_)) | Some(v @ Value::Array(_)) => (None, v.clone()),
            Some(v) => {
                // Unexpected primitive; keep as-is for robustness.
                (None, v.clone())
            },
            None => (None, Value::Null),
        };

        out.push(LlmToolCall {
            id: Cow::Borrowed(id),
            name: Cow::Owned(name.to_string()),
            arguments_raw,
            arguments,
        });
    }

    out
}

/// Parse tool calls from a full LLM response object with zero-copy
/// optimization. This aggregates tool calls from all choices' messages.
pub fn parse_tool_calls(response: &Value) -> Vec<LlmToolCall<'_>> {
    let mut all = Vec::new();
    if let Some(choices) = response.get("choices").and_then(|v| v.as_array()) {
        for ch in choices {
            if let Some(msg) = ch.get("message") {
                all.extend(parse_tool_calls_from_message(msg));
            }
        }
    }
    all
}

/// Convenience: parse the first tool call found in the response.
pub fn parse_first_tool_call(response: &Value) -> Option<LlmToolCall<'_>> {
    parse_tool_calls(response).into_iter().next()
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    #[test]
    fn test_normalize_arguments_string() {
        let args = json!(r#"{"city": "Shenzhen"}"#);
        let normalized = normalize_arguments(&args);
        assert_eq!(normalized, json!({"city": "Shenzhen"}));
    }

    #[test]
    fn test_normalize_arguments_invalid_string() {
        let args = json!("invalid json");
        let normalized = normalize_arguments(&args);
        assert_eq!(normalized, json!("invalid json"));
    }

    #[test]
    fn test_normalize_arguments_object() {
        let args = json!({"CITY": "Shenzhen", "count": 5});
        let normalized = normalize_arguments(&args);
        assert_eq!(normalized, json!({"CITY": "Shenzhen", "count": 5}));
    }

    #[test]
    fn test_normalize_arguments_nested() {
        let args = json!({
            "data": {
                "CITY": "Shenzhen",
                "Count": 5
            }
        });
        let normalized = normalize_arguments(&args);
        assert_eq!(
            normalized,
            json!({
                "data": {
                    "CITY": "Shenzhen",
                    "Count": 5
                }
            })
        );
    }

    #[test]
    fn test_normalize_arguments_preserves_case() {
        let args = json!({"cityName": "Shenzhen", "UserID": 42});
        let normalized = normalize_arguments(&args);
        assert_eq!(normalized, json!({"cityName": "Shenzhen", "UserID": 42}));
    }

    #[test]
    fn test_parse_tool_calls_from_message() {
        let message = json!({
            "tool_calls": [
                {
                    "id": "call_123",
                    "type": "function",
                    "function": {
                        "name": "get_weather",
                        "arguments": "{\"city\": \"Shenzhen\"}"
                    }
                }
            ]
        });

        let calls = parse_tool_calls_from_message(&message);
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].id, "call_123");
        assert_eq!(calls[0].name, "get_weather");
    }

    #[test]
    fn test_parse_tool_calls_from_message_object_args() {
        let message = json!({
            "tool_calls": [
                {
                    "id": "call_456",
                    "type": "function",
                    "function": {
                        "name": "calc",
                        "arguments": {"op": "add", "a": 1, "b": 2}
                    }
                }
            ]
        });

        let calls = parse_tool_calls_from_message(&message);
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "calc");
        assert_eq!(calls[0].arguments, json!({"op": "add", "a": 1, "b": 2}));
    }

    #[test]
    fn test_parse_tool_calls_from_response() {
        let response = json!({
            "choices": [
                {
                    "message": {
                        "tool_calls": [
                            {
                                "id": "call_789",
                                "function": {
                                    "name": "test_tool",
                                    "arguments": "{\"input\": \"test\"}"
                                }
                            }
                        ]
                    }
                }
            ]
        });

        let calls = parse_tool_calls(&response);
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "test_tool");
    }

    #[test]
    fn test_parse_first_tool_call() {
        let response = json!({
            "choices": [
                {
                    "message": {
                        "tool_calls": [
                            {
                                "id": "call_first",
                                "function": {
                                    "name": "first_tool",
                                    "arguments": "{}"
                                }
                            },
                            {
                                "id": "call_second",
                                "function": {
                                    "name": "second_tool",
                                    "arguments": "{}"
                                }
                            }
                        ]
                    }
                }
            ]
        });

        let first = parse_first_tool_call(&response);
        assert!(first.is_some());
        assert_eq!(first.unwrap().name, "first_tool");
    }

    #[test]
    fn test_parse_tool_calls_robust_direct() {
        let response = json!({
            "tool_calls": [
                {
                    "id": "call_direct",
                    "function": {
                        "name": "direct_tool",
                        "arguments": "{}"
                    }
                }
            ]
        });

        let calls = parse_tool_calls_robust(&response);
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "direct_tool");
    }

    #[test]
    fn test_parse_legacy_function_call() {
        let response = json!({
            "function_call": {
                "name": "legacy_tool",
                "arguments": "{\"param\": \"value\"}"
            }
        });

        let calls = parse_tool_calls_robust(&response);
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "legacy_tool");
        assert_eq!(calls[0].id, "legacy");
    }

    #[test]
    fn test_parse_multiple_tool_calls() {
        let message = json!({
            "tool_calls": [
                {
                    "id": "call_1",
                    "function": {
                        "name": "tool_a",
                        "arguments": "{\"x\": 1}"
                    }
                },
                {
                    "id": "call_2",
                    "function": {
                        "name": "tool_b",
                        "arguments": "{\"y\": 2}"
                    }
                }
            ]
        });

        let calls = parse_tool_calls_from_message(&message);
        assert_eq!(calls.len(), 2);
        assert_eq!(calls[0].name, "tool_a");
        assert_eq!(calls[1].name, "tool_b");
    }
}