open-agent-sdk 0.6.2

Production-ready Rust SDK for building AI agents with local OpenAI-compatible servers (LMStudio, Ollama, llama.cpp, vLLM). Features streaming, tools, hooks, retry logic, and comprehensive examples.
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
//! # Error Types for the Open Agent SDK
//!
//! This module defines all error types used throughout the SDK, providing comprehensive
//! error handling with detailed context for different failure scenarios.
//!
//! ## Design Philosophy
//!
//! - **Explicit Error Handling**: Uses Rust's `Result<T>` type for all fallible operations
//! - **No Silent Failures**: All errors are propagated explicitly to the caller
//! - **Rich Context**: Each error variant provides specific information about what went wrong
//! - **Easy Conversion**: Automatic conversion from common error types (reqwest, serde_json)
//!
//! ## Usage
//!
//! ```ignore
//! use open_agent::{Error, Result};
//!
//! fn example() -> Result<()> {
//!     // Errors can be created using convenience methods
//!     if some_condition {
//!         return Err(Error::config("Invalid model name"));
//!     }
//!
//!     // Or automatically converted from reqwest/serde_json errors
//!     let response = http_client.get(url).send().await?; // Auto-converts to Error::Http
//!     let json = serde_json::from_str(data)?; // Auto-converts to Error::Json
//!
//!     Ok(())
//! }
//! ```

use thiserror::Error;

// ============================================================================
// TYPE ALIASES
// ============================================================================

/// Type alias for `Result<T, Error>` used throughout the SDK.
///
/// This makes function signatures more concise and ensures consistent error handling
/// across the entire API surface. Instead of writing `std::result::Result<T, Error>`,
/// you can simply write `Result<T>`.
///
/// # Example
///
/// ```rust
/// use open_agent::Result;
///
/// async fn send_request() -> Result<String> {
///     // Function body
///     Ok("Success".to_string())
/// }
/// ```
pub type Result<T> = std::result::Result<T, Error>;

// ============================================================================
// ERROR ENUM
// ============================================================================

/// Comprehensive error type covering all failure modes in the SDK.
///
/// This enum uses the `thiserror` crate to automatically implement `std::error::Error`
/// and provide well-formatted error messages. Each variant represents a different
/// category of failure that can occur during SDK operation.
///
/// ## Error Categories
///
/// - **HTTP**: Network communication failures (connection errors, timeouts, etc.)
/// - **JSON**: Serialization/deserialization failures
/// - **Config**: Invalid configuration parameters
/// - **Api**: Error responses from the model server
/// - **Stream**: Failures during streaming response processing
/// - **Tool**: Tool execution or registration failures
/// - **InvalidInput**: User-provided input validation failures
/// - **Timeout**: Request timeout exceeded
/// - **Other**: Catch-all for miscellaneous errors
///
/// ## Automatic Conversions
///
/// The `#[from]` attribute on `Http` and `Json` variants enables automatic conversion
/// from `reqwest::Error` and `serde_json::Error` using the `?` operator, making
/// error propagation seamless.
#[derive(Error, Debug)]
pub enum Error {
    /// HTTP request failed due to network issues, connection problems, or HTTP errors.
    ///
    /// This variant wraps `reqwest::Error` and is automatically created when using
    /// the `?` operator on reqwest operations. Common causes include:
    /// - Connection refused (server not running)
    /// - DNS resolution failures
    /// - TLS/SSL certificate errors
    /// - HTTP status errors (4xx, 5xx)
    /// - Network timeouts
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let response = client.post(url).send().await?; // Auto-converts reqwest::Error
    /// ```
    #[error("HTTP request failed: {0}")]
    Http(#[from] reqwest::Error),

    /// JSON serialization or deserialization failed.
    ///
    /// This variant wraps `serde_json::Error` and occurs when:
    /// - Parsing invalid JSON from the API
    /// - Serializing request data fails
    /// - JSON structure doesn't match expected schema
    /// - Required fields are missing in JSON
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let value: MyType = serde_json::from_str(json_str)?; // Auto-converts serde_json::Error
    /// ```
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),

    /// Invalid configuration provided when building AgentOptions.
    ///
    /// Occurs during the builder pattern validation phase when required fields
    /// are missing or invalid values are provided. Common causes:
    /// - Missing required fields (model, base_url, system_prompt)
    /// - Invalid URL format in base_url
    /// - Invalid timeout values
    /// - Invalid max_tokens or temperature ranges
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// return Err(Error::config("base_url is required"));
    /// ```
    #[error("Invalid configuration: {0}")]
    Config(String),

    /// Error response received from the model server's API.
    ///
    /// This indicates the HTTP request succeeded, but the API returned an error
    /// response. Common causes:
    /// - Model not found on the server
    /// - Invalid API key or authentication failure
    /// - Rate limiting
    /// - Server-side errors (500, 502, 503)
    /// - Invalid request format
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// return Err(Error::api("Model 'gpt-4' not found on server"));
    /// ```
    #[error("API error: {0}")]
    Api(String),

    /// Error occurred while processing the streaming response.
    ///
    /// This happens during Server-Sent Events (SSE) parsing or stream processing.
    /// Common causes:
    /// - Malformed SSE data
    /// - Connection interrupted mid-stream
    /// - Unexpected end of stream
    /// - Invalid chunk format
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// return Err(Error::stream("Unexpected end of SSE stream"));
    /// ```
    #[error("Streaming error: {0}")]
    Stream(String),

    /// Tool execution or registration failed.
    ///
    /// Occurs when there are problems with tool definitions or execution:
    /// - Tool handler returns an error
    /// - Tool input validation fails
    /// - Tool name collision during registration
    /// - Tool not found when executing
    /// - Invalid tool schema
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// return Err(Error::tool("Tool 'calculator' not found"));
    /// ```
    #[error("Tool execution error: {0}")]
    Tool(String),

    /// Invalid input provided by the user.
    ///
    /// Validation error for user-provided data that doesn't meet requirements:
    /// - Empty prompt string
    /// - Invalid parameter format
    /// - Out of range values
    /// - Malformed input data
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// return Err(Error::invalid_input("Prompt cannot be empty"));
    /// ```
    #[error("Invalid input: {0}")]
    InvalidInput(String),

    /// Request exceeded the configured timeout duration.
    ///
    /// The operation took longer than the timeout specified in AgentOptions.
    /// This is a dedicated variant (no message needed) because the cause is clear.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// return Err(Error::timeout());
    /// ```
    #[error("Request timeout")]
    Timeout,

    /// Miscellaneous error that doesn't fit other categories.
    ///
    /// Catch-all variant for unexpected errors or edge cases that don't fit
    /// into the specific categories above. Should be used sparingly.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// return Err(Error::other("Unexpected condition occurred"));
    /// ```
    #[error("Error: {0}")]
    Other(String),
}

// ============================================================================
// CONVENIENCE CONSTRUCTORS
// ============================================================================

/// Implementation of convenience constructors for creating Error instances.
///
/// These methods provide a more ergonomic API for creating errors compared to
/// directly constructing the enum variants. They accept `impl Into<String>`,
/// allowing callers to pass `&str`, `String`, or any other type that converts to `String`.
impl Error {
    /// Create a new configuration error with a descriptive message.
    ///
    /// Use this when validation fails during `AgentOptions` construction or when
    /// invalid configuration values are detected.
    ///
    /// # Arguments
    ///
    /// * `msg` - Error description explaining what configuration is invalid
    ///
    /// # Example
    ///
    /// ```rust
    /// use open_agent::Error;
    ///
    /// let err = Error::config("base_url must be a valid HTTP or HTTPS URL");
    /// assert_eq!(err.to_string(), "Invalid configuration: base_url must be a valid HTTP or HTTPS URL");
    /// ```
    pub fn config(msg: impl Into<String>) -> Self {
        Error::Config(msg.into())
    }

    /// Create a new API error with the server's error message.
    ///
    /// Use this when the API returns an error response (even if the HTTP request
    /// itself succeeded). This typically happens when the server rejects the request
    /// due to invalid parameters, missing resources, or server-side failures.
    ///
    /// # Arguments
    ///
    /// * `msg` - Error message from the API server
    ///
    /// # Example
    ///
    /// ```rust
    /// use open_agent::Error;
    ///
    /// let err = Error::api("Model 'invalid-model' not found");
    /// assert_eq!(err.to_string(), "API error: Model 'invalid-model' not found");
    /// ```
    pub fn api(msg: impl Into<String>) -> Self {
        Error::Api(msg.into())
    }

    /// Create a new streaming error for SSE parsing or stream processing failures.
    ///
    /// Use this when errors occur during Server-Sent Events stream parsing,
    /// such as malformed data, unexpected stream termination, or invalid chunks.
    ///
    /// # Arguments
    ///
    /// * `msg` - Description of the streaming failure
    ///
    /// # Example
    ///
    /// ```rust
    /// use open_agent::Error;
    ///
    /// let err = Error::stream("Unexpected end of SSE stream");
    /// assert_eq!(err.to_string(), "Streaming error: Unexpected end of SSE stream");
    /// ```
    pub fn stream(msg: impl Into<String>) -> Self {
        Error::Stream(msg.into())
    }

    /// Create a new tool execution error.
    ///
    /// Use this when tool registration, lookup, or execution fails. This includes
    /// tool handler errors, missing tools, and invalid tool inputs.
    ///
    /// # Arguments
    ///
    /// * `msg` - Description of the tool failure
    ///
    /// # Example
    ///
    /// ```rust
    /// use open_agent::Error;
    ///
    /// let err = Error::tool("Calculator tool failed: division by zero");
    /// assert_eq!(err.to_string(), "Tool execution error: Calculator tool failed: division by zero");
    /// ```
    pub fn tool(msg: impl Into<String>) -> Self {
        Error::Tool(msg.into())
    }

    /// Create a new invalid input error for user input validation failures.
    ///
    /// Use this when user-provided data doesn't meet requirements, such as
    /// empty strings, out-of-range values, or malformed data.
    ///
    /// # Arguments
    ///
    /// * `msg` - Description of why the input is invalid
    ///
    /// # Example
    ///
    /// ```rust
    /// use open_agent::Error;
    ///
    /// let err = Error::invalid_input("Prompt cannot be empty");
    /// assert_eq!(err.to_string(), "Invalid input: Prompt cannot be empty");
    /// ```
    pub fn invalid_input(msg: impl Into<String>) -> Self {
        Error::InvalidInput(msg.into())
    }

    /// Create a new miscellaneous error for cases that don't fit other categories.
    ///
    /// Use this sparingly for unexpected conditions that don't fit into the
    /// more specific error variants.
    ///
    /// # Arguments
    ///
    /// * `msg` - Description of the error
    ///
    /// # Example
    ///
    /// ```rust
    /// use open_agent::Error;
    ///
    /// let err = Error::other("Unexpected internal state");
    /// assert_eq!(err.to_string(), "Error: Unexpected internal state");
    /// ```
    pub fn other(msg: impl Into<String>) -> Self {
        Error::Other(msg.into())
    }

    /// Create a timeout error indicating the operation exceeded the time limit.
    ///
    /// Use this when the request or operation takes longer than the configured
    /// timeout duration. No message is needed since the cause is self-explanatory.
    ///
    /// # Example
    ///
    /// ```rust
    /// use open_agent::Error;
    ///
    /// let err = Error::timeout();
    /// assert_eq!(err.to_string(), "Request timeout");
    /// ```
    pub fn timeout() -> Self {
        Error::Timeout
    }
}

// ============================================================================
// TESTS
// ============================================================================

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

    #[test]
    fn test_error_config() {
        let err = Error::config("Invalid model");
        assert!(matches!(err, Error::Config(_)));
        assert_eq!(err.to_string(), "Invalid configuration: Invalid model");
    }

    #[test]
    fn test_error_api() {
        let err = Error::api("500 Internal Server Error");
        assert!(matches!(err, Error::Api(_)));
        assert_eq!(err.to_string(), "API error: 500 Internal Server Error");
    }

    #[test]
    fn test_error_stream() {
        let err = Error::stream("Connection lost");
        assert!(matches!(err, Error::Stream(_)));
        assert_eq!(err.to_string(), "Streaming error: Connection lost");
    }

    #[test]
    fn test_error_tool() {
        let err = Error::tool("Tool not found");
        assert!(matches!(err, Error::Tool(_)));
        assert_eq!(err.to_string(), "Tool execution error: Tool not found");
    }

    #[test]
    fn test_error_invalid_input() {
        let err = Error::invalid_input("Missing parameter");
        assert!(matches!(err, Error::InvalidInput(_)));
        assert_eq!(err.to_string(), "Invalid input: Missing parameter");
    }

    #[test]
    fn test_error_timeout() {
        let err = Error::timeout();
        assert!(matches!(err, Error::Timeout));
        assert_eq!(err.to_string(), "Request timeout");
    }

    #[test]
    fn test_error_other() {
        let err = Error::other("Something went wrong");
        assert!(matches!(err, Error::Other(_)));
        assert_eq!(err.to_string(), "Error: Something went wrong");
    }

    #[test]
    fn test_error_from_reqwest() {
        // Test that reqwest::Error can be converted
        // This is mostly for compile-time checking
        fn _test_conversion(_e: reqwest::Error) -> Error {
            // This function just needs to compile
            Error::Http(_e)
        }
    }

    #[test]
    fn test_error_from_serde_json() {
        // Test that serde_json::Error can be converted
        let json_err = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
        let err: Error = json_err.into();
        assert!(matches!(err, Error::Json(_)));
    }

    #[test]
    fn test_result_type_alias() {
        // Test that our Result type alias works correctly
        fn _returns_result() -> Result<i32> {
            Ok(42)
        }

        fn _returns_error() -> Result<i32> {
            Err(Error::timeout())
        }
    }
}