openrouter-rs 0.14.0

A type-safe OpenRouter Rust SDK
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
587
588
589
590
591
592
593
//! # Tool and Function Call Types
//!
//! This module contains types for defining and working with tools (function calls)
//! in OpenRouter API requests. Tools allow LLMs to call external functions and
//! use their results in generating responses.
//!
//! ## Tool Definition
//!
//! Tools are defined using the [`Tool`] struct which follows OpenRouter's API format:
//!
//! ```rust
//! use openrouter_rs::types::tool::Tool;
//! use serde_json::json;
//!
//! let tool = Tool::builder()
//!     .name("get_weather")
//!     .description("Get the current weather for a location")
//!     .parameters(json!({
//!         "type": "object",
//!         "properties": {
//!             "location": {
//!                 "type": "string",
//!                 "description": "The city and state, e.g. San Francisco, CA"
//!             }
//!         },
//!         "required": ["location"]
//!     }))
//!     .build()?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## Tool Choice Control
//!
//! Control how the model uses tools with [`ToolChoice`]:
//!
//! ```rust
//! use openrouter_rs::types::tool::ToolChoice;
//!
//! // Model chooses whether to use tools
//! let auto_choice = ToolChoice::auto();
//!
//! // Force model to use tools
//! let required_choice = ToolChoice::required();
//!
//! // Force specific tool
//! let specific_choice = ToolChoice::force_tool("get_weather");
//! ```

use std::collections::HashMap;

use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::error::OpenRouterError;

/// Tool definition for function calling
///
/// Represents a tool that can be called by the LLM. Tools follow OpenRouter's
/// standardized format and are automatically converted to the appropriate
/// format for different model providers.
///
/// # Examples
///
/// ```rust
/// use openrouter_rs::types::tool::Tool;
/// use serde_json::json;
///
/// let weather_tool = Tool::builder()
///     .name("get_weather")
///     .description("Get current weather for a location")
///     .parameters(json!({
///         "type": "object",
///         "properties": {
///             "location": {"type": "string", "description": "City and state"}
///         },
///         "required": ["location"]
///     }))
///     .build()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Serialize, Deserialize, Debug, Clone)]
#[non_exhaustive]
pub struct Tool {
    /// Type of tool (always "function" for now)
    #[serde(rename = "type")]
    pub tool_type: String,

    /// Function definition
    pub function: FunctionDefinition,

    /// Optional cache-control directive for provider-side prompt caching.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<Value>,
}

impl Tool {
    /// Create a new tool builder
    pub fn builder() -> ToolBuilder {
        ToolBuilder::default()
    }

    /// Create a simple tool with name, description, and parameters
    pub fn new(name: &str, description: &str, parameters: Value) -> Self {
        Self {
            tool_type: "function".to_string(),
            function: FunctionDefinition {
                name: name.to_string(),
                description: description.to_string(),
                parameters,
                strict: None,
            },
            cache_control: None,
        }
    }
}

#[derive(Debug, Default, Clone)]
pub struct ToolBuilder {
    tool_type: Option<String>,
    name: Option<String>,
    description: Option<String>,
    parameters: Option<Value>,
    strict: Option<bool>,
    cache_control: Option<Value>,
}

impl ToolBuilder {
    /// Override the tool type. Defaults to `"function"`.
    pub fn tool_type(&mut self, tool_type: impl Into<String>) -> &mut Self {
        self.tool_type = Some(tool_type.into());
        self
    }

    /// Set the full function definition at once.
    pub fn function(&mut self, function: FunctionDefinition) -> &mut Self {
        self.name = Some(function.name);
        self.description = Some(function.description);
        self.parameters = Some(function.parameters);
        self.strict = function.strict;
        self
    }

    /// Build the tool, validating that the function name is present.
    pub fn build(&self) -> Result<Tool, OpenRouterError> {
        let name = self
            .name
            .clone()
            .ok_or_else(|| OpenRouterError::ConfigError("Tool name is required".to_string()))?;

        Ok(Tool {
            tool_type: self
                .tool_type
                .clone()
                .unwrap_or_else(|| "function".to_string()),
            function: FunctionDefinition {
                name,
                description: self.description.clone().unwrap_or_default(),
                parameters: self.parameters.clone().unwrap_or(Value::Null),
                strict: self.strict,
            },
            cache_control: self.cache_control.clone(),
        })
    }
}

/// Function definition within a tool
///
/// Defines the function that can be called, including its name,
/// description, and parameter schema.
#[derive(Serialize, Deserialize, Debug, Clone, Builder)]
#[builder(build_fn(error = "OpenRouterError"))]
#[non_exhaustive]
pub struct FunctionDefinition {
    /// Name of the function
    #[builder(setter(into))]
    pub name: String,

    /// Description of what the function does
    #[builder(setter(into))]
    pub description: String,

    /// JSON Schema defining the function parameters
    #[builder(setter(custom))]
    pub parameters: Value,

    /// Whether the model must strictly adhere to the parameter schema.
    #[builder(setter(strip_option), default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub strict: Option<bool>,
}

impl FunctionDefinition {
    /// Create a new function definition builder
    pub fn builder() -> FunctionDefinitionBuilder {
        FunctionDefinitionBuilder::default()
    }
}

impl ToolBuilder {
    /// Set the function name
    pub fn name(&mut self, name: &str) -> &mut Self {
        self.name = Some(name.to_string());
        self
    }

    /// Set the function description
    pub fn description(&mut self, description: &str) -> &mut Self {
        self.description = Some(description.to_string());
        self
    }

    /// Set the parameters as a JSON Value
    pub fn parameters(&mut self, parameters: Value) -> &mut Self {
        self.parameters = Some(parameters);
        self
    }

    /// Set parameters from a serializable struct
    pub fn parameters_from<T: Serialize>(
        &mut self,
        params: &T,
    ) -> Result<&mut Self, OpenRouterError> {
        let value = serde_json::to_value(params).map_err(OpenRouterError::Serialization)?;
        Ok(self.parameters(value))
    }

    /// Set parameters from a JSON string
    pub fn parameters_json(&mut self, json: &str) -> Result<&mut Self, OpenRouterError> {
        let value: Value = serde_json::from_str(json).map_err(OpenRouterError::Serialization)?;
        Ok(self.parameters(value))
    }

    /// Set the function strict-schema flag.
    pub fn strict(&mut self, strict: bool) -> &mut Self {
        self.strict = Some(strict);
        self
    }

    /// Set the top-level tool cache-control payload.
    pub fn cache_control(&mut self, cache_control: impl Into<Value>) -> &mut Self {
        self.cache_control = Some(cache_control.into());
        self
    }
}

impl FunctionDefinitionBuilder {
    /// Set parameters from a JSON Value
    pub fn parameters(&mut self, parameters: Value) -> &mut Self {
        self.parameters = Some(parameters);
        self
    }

    /// Set parameters from a serializable struct
    pub fn parameters_from<T: Serialize>(
        &mut self,
        params: &T,
    ) -> Result<&mut Self, OpenRouterError> {
        let value = serde_json::to_value(params).map_err(OpenRouterError::Serialization)?;
        self.parameters = Some(value);
        Ok(self)
    }

    /// Set parameters from a JSON string
    pub fn parameters_json(&mut self, json: &str) -> Result<&mut Self, OpenRouterError> {
        let value: Value = serde_json::from_str(json).map_err(OpenRouterError::Serialization)?;
        self.parameters = Some(value);
        Ok(self)
    }
}

/// OpenRouter built-in server tool definition.
///
/// Server tools are OpenRouter-hosted capabilities such as web search,
/// datetime lookup, files, bash, and model search. They share a common wire
/// shape: a `type`, optional `parameters`, and optional tool-specific
/// top-level fields.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[non_exhaustive]
pub struct ServerTool {
    #[serde(rename = "type")]
    pub tool_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parameters: Option<Value>,
    #[serde(flatten)]
    pub extra: HashMap<String, Value>,
}

impl ServerTool {
    pub fn new(tool_type: impl Into<String>) -> Self {
        Self {
            tool_type: tool_type.into(),
            parameters: None,
            extra: HashMap::new(),
        }
    }

    pub fn with_parameters(tool_type: impl Into<String>, parameters: impl Into<Value>) -> Self {
        Self::new(tool_type).parameters(parameters)
    }

    pub fn parameters(mut self, parameters: impl Into<Value>) -> Self {
        self.parameters = Some(parameters.into());
        self
    }

    pub fn parameters_from<T: Serialize>(mut self, params: &T) -> Result<Self, OpenRouterError> {
        self.parameters =
            Some(serde_json::to_value(params).map_err(OpenRouterError::Serialization)?);
        Ok(self)
    }

    pub fn option(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
        self.extra.insert(key.into(), value.into());
        self
    }

    pub fn web_search() -> Self {
        Self::new("openrouter:web_search")
    }

    pub fn web_search_with_parameters(parameters: impl Into<Value>) -> Self {
        Self::with_parameters("openrouter:web_search", parameters)
    }

    pub fn web_search_preview() -> Self {
        Self::new("web_search_preview")
    }

    pub fn datetime() -> Self {
        Self::new("openrouter:datetime")
    }

    pub fn datetime_with_timezone(timezone: impl Into<String>) -> Self {
        Self::with_parameters(
            "openrouter:datetime",
            serde_json::json!({ "timezone": timezone.into() }),
        )
    }

    pub fn files() -> Self {
        Self::new("openrouter:files")
    }

    pub fn bash() -> Self {
        Self::new("openrouter:bash")
    }

    pub fn web_fetch() -> Self {
        Self::new("openrouter:web_fetch")
    }

    pub fn advisor() -> Self {
        Self::new("openrouter:advisor")
    }

    pub fn subagent() -> Self {
        Self::new("openrouter:subagent")
    }

    pub fn image_generation() -> Self {
        Self::new("openrouter:image_generation")
    }

    pub fn search_models() -> Self {
        Self::new("openrouter:experimental__search_models")
    }

    pub fn apply_patch() -> Self {
        Self::new("openrouter:apply_patch")
    }

    pub(crate) fn is_server_tool_type(tool_type: &str) -> bool {
        tool_type.starts_with("openrouter:")
            || matches!(
                tool_type,
                "web_search"
                    | "web_search_2025_08_26"
                    | "web_search_preview"
                    | "web_search_preview_2025_03_11"
                    | "apply_patch"
                    | "shell"
                    | "namespace"
            )
    }

    pub(crate) fn is_files_tool_type(tool_type: &str) -> bool {
        matches!(tool_type, "openrouter:files" | "files")
    }

    pub(crate) fn is_files_tool(&self) -> bool {
        Self::is_files_tool_type(&self.tool_type)
    }

    pub(crate) fn is_server_tool_value(value: &Value) -> bool {
        value
            .get("type")
            .and_then(Value::as_str)
            .is_some_and(Self::is_server_tool_type)
    }

    pub(crate) fn is_files_tool_value(value: &Value) -> bool {
        value
            .get("type")
            .and_then(Value::as_str)
            .is_some_and(Self::is_files_tool_type)
    }
}

impl From<ServerTool> for Value {
    fn from(tool: ServerTool) -> Self {
        serde_json::to_value(tool).expect("server tool serialization should not fail")
    }
}

/// Control how the model chooses to use tools
///
/// Specifies whether the model should use tools, and if so, how it should
/// choose which tools to call.
///
/// # Examples
///
/// ```rust
/// use openrouter_rs::types::tool::ToolChoice;
///
/// // Let model decide
/// let auto = ToolChoice::auto();
///
/// // Prevent tool use
/// let none = ToolChoice::none();
///
/// // Require tool use
/// let required = ToolChoice::required();
///
/// // Force specific tool
/// let specific = ToolChoice::force_tool("get_weather");
/// ```
#[derive(Serialize, Deserialize, Debug, Clone)]
#[non_exhaustive]
#[serde(untagged)]
pub enum ToolChoice {
    /// Simple string choices: "none", "auto", "required"
    String(String),
    /// Force a specific tool to be called
    Specific(SpecificToolChoice),
    /// Force a specific OpenRouter server tool to be called
    Server(ServerToolChoice),
}

impl ToolChoice {
    /// Model will not call any tools
    pub fn none() -> Self {
        Self::String("none".to_string())
    }

    /// Model can choose whether to call tools
    pub fn auto() -> Self {
        Self::String("auto".to_string())
    }

    /// Model must call at least one tool
    pub fn required() -> Self {
        Self::String("required".to_string())
    }

    /// Force the model to call a specific tool
    pub fn force_tool(tool_name: &str) -> Self {
        Self::Specific(SpecificToolChoice {
            tool_type: "function".to_string(),
            function: SpecificToolFunction {
                name: tool_name.to_string(),
            },
        })
    }

    /// Force the model to call a specific OpenRouter server tool.
    pub fn force_server_tool(tool_type: impl Into<String>) -> Self {
        Self::Server(ServerToolChoice {
            tool_type: tool_type.into(),
        })
    }
}

/// Specific tool choice for forcing a particular tool
#[derive(Serialize, Deserialize, Debug, Clone)]
#[non_exhaustive]
pub struct SpecificToolChoice {
    #[serde(rename = "type")]
    pub tool_type: String,
    pub function: SpecificToolFunction,
}

/// Function specification for specific tool choice
#[derive(Serialize, Deserialize, Debug, Clone)]
#[non_exhaustive]
pub struct SpecificToolFunction {
    pub name: String,
}

/// Specific server-tool choice for forcing an OpenRouter built-in tool.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[non_exhaustive]
pub struct ServerToolChoice {
    #[serde(rename = "type")]
    pub tool_type: String,
}

/// Helper function to create a tool with common parameter structure
///
/// Creates a tool with an object-type parameter schema and the specified properties.
///
/// # Examples
///
/// ```rust
/// use openrouter_rs::types::tool::create_tool;
/// use serde_json::json;
///
/// let tool = create_tool(
///     "calculator",
///     "Perform basic arithmetic operations",
///     json!({
///         "operation": {"type": "string", "enum": ["add", "subtract", "multiply", "divide"]},
///         "a": {"type": "number"},
///         "b": {"type": "number"}
///     }),
///     &["operation", "a", "b"]
/// );
/// ```
pub fn create_tool(name: &str, description: &str, properties: Value, required: &[&str]) -> Tool {
    let parameters = serde_json::json!({
        "type": "object",
        "properties": properties,
        "required": required
    });

    Tool::new(name, description, parameters)
}

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

    #[test]
    fn test_tool_creation() {
        let tool = Tool::builder()
            .name("test_function")
            .description("A test function")
            .parameters(json!({"type": "object"}))
            .build()
            .unwrap();

        assert_eq!(tool.tool_type, "function");
        assert_eq!(tool.function.name, "test_function");
        assert_eq!(tool.function.description, "A test function");
    }

    #[test]
    fn test_tool_choice_variants() {
        let auto = ToolChoice::auto();
        let none = ToolChoice::none();
        let required = ToolChoice::required();
        let specific = ToolChoice::force_tool("my_function");

        // Test serialization
        assert_eq!(serde_json::to_string(&auto).unwrap(), r#""auto""#);
        assert_eq!(serde_json::to_string(&none).unwrap(), r#""none""#);
        assert_eq!(serde_json::to_string(&required).unwrap(), r#""required""#);

        if let ToolChoice::Specific(spec) = specific {
            assert_eq!(spec.function.name, "my_function");
        } else {
            panic!("Expected specific tool choice");
        }
    }

    #[test]
    fn test_create_tool_helper() {
        let tool = create_tool(
            "weather",
            "Get weather",
            json!({"location": {"type": "string"}}),
            &["location"],
        );

        assert_eq!(tool.function.name, "weather");
        assert_eq!(tool.function.description, "Get weather");

        let params = &tool.function.parameters;
        assert_eq!(params["type"], "object");
        assert_eq!(params["required"], json!(["location"]));
    }
}