xai-openapi 0.1.1

Rust types for the xAI API (Grok models)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! Tool and function call types for xAI API.

use serde::{Deserialize, Serialize};

use crate::prelude::*;

use crate::common::Annotation;

/// Definition of one tool that the model can call.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Tool {
    /// A function tool that the model can call.
    Function {
        /// Definition of tool call available to the model.
        function: FunctionDefinition,
    },
    /// Live search tool.
    LiveSearch {
        /// Search sources.
        sources: Vec<crate::search::SearchSource>,
    },
}

impl Default for Tool {
    fn default() -> Self {
        Tool::Function {
            function: FunctionDefinition::default(),
        }
    }
}

/// Definition of the tool call made available to the model.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct FunctionDefinition {
    /// The name of the function.
    pub name: String,

    /// A JSON schema describing the function parameters.
    pub parameters: serde_json::Value,

    /// A description of the function to indicate to the model when to call it.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Not supported. Only maintained for compatibility reasons.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub strict: Option<bool>,
}

/// A tool call made by the model.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ToolCall {
    /// A unique ID of the tool call generated by xAI.
    pub id: String,

    /// Function to call for the tool call.
    pub function: Function,

    /// Index of the tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub index: Option<i32>,

    /// Type of tool call.
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub tool_type: Option<String>,
}

/// Function call details.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Function {
    /// Name of the function.
    pub name: String,

    /// Arguments available to the function.
    pub arguments: String,
}

/// Function name for tool choice.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct FunctionChoice {
    /// Name of the function.
    pub name: String,
}

/// Parameter to control how model chooses the tools.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ToolChoice {
    /// Controls tool access by the model.
    /// `"none"` makes model ignore tools, `"auto"` lets the model automatically decide,
    /// `"required"` forces model to pick a tool to call.
    Mode(String),
    /// Specific function to use.
    Specific {
        /// Type is always `"function"`.
        #[serde(rename = "type")]
        tool_type: String,
        /// Name of the function to use.
        #[serde(skip_serializing_if = "Option::is_none")]
        function: Option<FunctionChoice>,
    },
}

impl Default for ToolChoice {
    fn default() -> Self {
        ToolChoice::Mode("auto".to_string())
    }
}

/// A tool call to run a function (for Responses API).
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct FunctionToolCall {
    /// The type of the function tool call.
    #[serde(rename = "type")]
    pub call_type: String,

    /// The unique ID of the function tool call generated by the model.
    pub call_id: String,

    /// The name of the function.
    pub name: String,

    /// The arguments to pass to the function, as a JSON string.
    pub arguments: String,

    /// The unique ID of the function tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// Status of the item. One of `completed`, `in_progress` or `incomplete`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

/// The output of a function tool call.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct FunctionToolCallOutput {
    /// The type of the function tool call, which is always `function_call_output`.
    #[serde(rename = "type")]
    pub output_type: String,

    /// The unique ID of the function tool call generated by the model.
    pub call_id: String,

    /// The output of the function tool call, as a JSON string.
    pub output: String,
}

/// The output of a web search tool call.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct WebSearchCall {
    /// The type of the web search tool call. Always `web_search_call`.
    #[serde(rename = "type")]
    pub call_type: String,

    /// An object describing the specific action taken in this web search call.
    pub action: WebSearchAction,

    /// The unique ID of the web search tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// The status of the web search tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

/// Web search action.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum WebSearchAction {
    /// Action type "search" - Performs a web search query.
    Search {
        /// The search query.
        query: String,
        /// The sources used in the search.
        #[serde(skip_serializing_if = "Option::is_none")]
        sources: Option<Vec<WebSearchSource>>,
    },
    /// Action type `open_page` - Opens a specific URL from search results.
    OpenPage {
        /// The URL of the page to open.
        url: String,
    },
    /// Action type "find" - Searches for a pattern within a loaded page.
    Find {
        /// The source of the page to search in.
        source: WebSearchSource,
        /// The pattern or text to search for within the page.
        pattern: String,
    },
}

impl Default for WebSearchAction {
    fn default() -> Self {
        WebSearchAction::Search {
            query: String::new(),
            sources: None,
        }
    }
}

/// Web search source.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct WebSearchSource {
    /// The type of source.
    #[serde(rename = "type")]
    pub source_type: String,

    /// The URL of the source.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
}

/// Web search options.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct WebSearchOptions {
    /// This field included for compatibility reason with `OpenAI`'s API.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_context_size: Option<String>,

    /// Only included for compatibility.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filters: Option<serde_json::Value>,

    /// Only included for compatibility.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_location: Option<serde_json::Value>,
}

/// Web search filters.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct WebSearchFilters {
    /// List of website domains to restrict search results to.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_domains: Option<Vec<String>>,

    /// List of website domains to exclude from search results.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub excluded_domains: Option<Vec<String>>,
}

/// The output of a file search tool call.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct FileSearchCall {
    /// The type of the file search tool call. Always `file_search_call`.
    #[serde(rename = "type")]
    pub call_type: String,

    /// The queries used to search for files.
    pub queries: Vec<String>,

    /// The results of the file search tool call.
    pub results: Vec<FileSearchResult>,

    /// The unique ID of the file search tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// The status of the file search tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

/// A file search result.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct FileSearchResult {
    /// The file ID of the file search result.
    pub file_id: String,

    /// The filename of the file search result.
    pub filename: String,

    /// The score of the file search result.
    pub score: f64,

    /// The text of the file search result.
    pub text: String,
}

/// The output of a code interpreter tool call.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct CodeInterpreterCall {
    /// The type of the code interpreter tool call. Always `code_interpreter_call`.
    #[serde(rename = "type")]
    pub call_type: String,

    /// The outputs of the code interpreter tool call.
    pub outputs: Vec<CodeInterpreterOutput>,

    /// The code of the code interpreter tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,

    /// The unique ID of the code interpreter tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// The status of the code interpreter tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

/// Output of a code interpreter tool call.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum CodeInterpreterOutput {
    /// Log output from code interpreter.
    Logs {
        /// The output of the code interpreter tool call.
        logs: String,
    },
    /// Image output from code interpreter.
    Image {
        /// The URL of the generated image.
        url: String,
    },
}

impl Default for CodeInterpreterOutput {
    fn default() -> Self {
        CodeInterpreterOutput::Logs {
            logs: String::new(),
        }
    }
}

/// The output of a MCP tool call.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct McpCall {
    /// The type of the MCP tool call. Always `mcp_call`.
    #[serde(rename = "type")]
    pub call_type: String,

    /// The name of the tool that was run.
    pub name: String,

    /// The label of the MCP server running the tool.
    pub server_label: String,

    /// A JSON string of the arguments passed to the tool.
    pub arguments: String,

    /// The output of the MCP tool call.
    pub output: String,

    /// The unique ID of the MCP tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// The error message of the MCP tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,

    /// The status of the MCP tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

/// The output of a custom tool call.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct CustomToolCall {
    /// The type of the custom tool call.
    #[serde(rename = "type")]
    pub call_type: String,

    /// The unique ID of the function tool call generated by the model.
    pub call_id: String,

    /// An identifier used to map this custom tool call to a tool call output.
    pub name: String,

    /// The status of the custom tool call.
    pub id: String,

    /// The unique ID of the custom tool call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input: Option<String>,

    /// Status of the item. One of `completed`, `in_progress` or `incomplete`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
}

/// Definition of one tool that the model can call (for Responses API).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ModelTool {
    /// A function that the model can call.
    Function {
        /// The name of the function.
        name: String,
        /// A JSON schema describing the function parameters.
        parameters: serde_json::Value,
        /// A description of the function.
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
        /// Not supported. Only maintained for compatibility reasons.
        #[serde(skip_serializing_if = "Option::is_none")]
        strict: Option<bool>,
    },
    /// Search the web.
    WebSearch {
        /// List of website domains to allow in the search results.
        #[serde(skip_serializing_if = "Option::is_none")]
        allowed_domains: Option<Vec<String>>,
        /// List of website domains to exclude from the search results.
        #[serde(skip_serializing_if = "Option::is_none")]
        excluded_domains: Option<Vec<String>>,
        /// Enable image understanding during web search.
        #[serde(skip_serializing_if = "Option::is_none")]
        enable_image_understanding: Option<bool>,
        /// Control whether the web search tool fetches live content or uses only cached content.
        #[serde(skip_serializing_if = "Option::is_none")]
        external_web_access: Option<bool>,
        /// Filters to apply to the search results.
        #[serde(skip_serializing_if = "Option::is_none")]
        filters: Option<WebSearchFilters>,
        /// High level guidance for the amount of context window space to use.
        #[serde(skip_serializing_if = "Option::is_none")]
        search_context_size: Option<String>,
        /// The user location to use for the search.
        #[serde(skip_serializing_if = "Option::is_none")]
        user_location: Option<serde_json::Value>,
    },
    /// Search X.
    XSearch {
        /// List of X Handles of the users from whom to consider the posts.
        #[serde(skip_serializing_if = "Option::is_none")]
        allowed_x_handles: Option<Vec<String>>,
        /// List of X Handles of the users from whom to exclude the posts.
        #[serde(skip_serializing_if = "Option::is_none")]
        excluded_x_handles: Option<Vec<String>>,
        /// Enable image understanding during X search.
        #[serde(skip_serializing_if = "Option::is_none")]
        enable_image_understanding: Option<bool>,
        /// Enable video understanding during X search.
        #[serde(skip_serializing_if = "Option::is_none")]
        enable_video_understanding: Option<bool>,
        /// Date from which to consider the results.
        #[serde(skip_serializing_if = "Option::is_none")]
        from_date: Option<String>,
        /// Date up to which to consider the results.
        #[serde(skip_serializing_if = "Option::is_none")]
        to_date: Option<String>,
    },
    /// Search the knowledge bases.
    FileSearch {
        /// List of vector store IDs to search within.
        vector_store_ids: Vec<String>,
        /// Maximum number of results.
        #[serde(skip_serializing_if = "Option::is_none")]
        max_num_results: Option<i32>,
        /// A filter to apply.
        #[serde(skip_serializing_if = "Option::is_none")]
        filters: Option<serde_json::Value>,
        /// Ranking options for search.
        #[serde(skip_serializing_if = "Option::is_none")]
        ranking_options: Option<serde_json::Value>,
    },
    /// Execute code.
    CodeInterpreter {
        /// The code interpreter container.
        #[serde(skip_serializing_if = "Option::is_none")]
        container: Option<serde_json::Value>,
    },
    /// A remote MCP server to use.
    Mcp {
        /// The label of the MCP server.
        server_label: String,
        /// The URL of the MCP server.
        server_url: String,
        /// Allowed tools.
        #[serde(skip_serializing_if = "Option::is_none")]
        allowed_tools: Option<Vec<String>>,
        /// Authorization token.
        #[serde(skip_serializing_if = "Option::is_none")]
        authorization: Option<String>,
        /// Connector ID.
        #[serde(skip_serializing_if = "Option::is_none")]
        connector_id: Option<String>,
        /// Additional headers.
        #[serde(skip_serializing_if = "Option::is_none")]
        headers: Option<HashMap<String, String>>,
        /// Whether to require approval.
        #[serde(skip_serializing_if = "Option::is_none")]
        require_approval: Option<String>,
        /// Server description.
        #[serde(skip_serializing_if = "Option::is_none")]
        server_description: Option<String>,
    },
}

impl Default for ModelTool {
    fn default() -> Self {
        ModelTool::Function {
            name: String::new(),
            parameters: serde_json::Value::Null,
            description: None,
            strict: None,
        }
    }
}

/// Parameter to control how model chooses the tools (for Responses API).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ModelToolChoice {
    /// Controls tool access by the model.
    Mode(String),
    /// Specific function to use.
    Specific {
        /// Type is always `"function"`.
        #[serde(rename = "type")]
        tool_type: String,
        /// Name of the function to use.
        name: String,
    },
}

impl Default for ModelToolChoice {
    fn default() -> Self {
        ModelToolChoice::Mode("auto".to_string())
    }
}

/// Text output from the model.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct OutputText {
    /// The type of the output. Always `output_text`.
    #[serde(rename = "type")]
    pub output_type: String,

    /// The text output from the model.
    pub text: String,

    /// Citations.
    pub annotations: Vec<Annotation>,

    /// The log probabilities of each output token returned.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub logprobs: Option<Vec<crate::chat::TokenLogProb>>,
}

/// Refusal output from the model.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct OutputRefusal {
    /// The type of the output. Always `refusal`.
    #[serde(rename = "type")]
    pub output_type: String,

    /// The reason for the refusal.
    pub refusal: String,
}

/// Output message content.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum OutputMessageContent {
    /// Text output.
    OutputText {
        /// The text output from the model.
        text: String,
        /// Citations.
        annotations: Vec<Annotation>,
        /// The log probabilities of each output token.
        #[serde(skip_serializing_if = "Option::is_none")]
        logprobs: Option<Vec<crate::chat::TokenLogProb>>,
    },
    /// Refusal output.
    Refusal {
        /// The reason for the refusal.
        refusal: String,
    },
}

impl Default for OutputMessageContent {
    fn default() -> Self {
        OutputMessageContent::OutputText {
            text: String::new(),
            annotations: Vec::new(),
            logprobs: None,
        }
    }
}