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
//! Advanced integration tests
//!
//! Tests that combine multiple features together to verify they work correctly
//! in realistic scenarios.

use open_agent::{AgentOptions, Client, HookDecision, Hooks, tool};
use serde_json::json;
use std::sync::{
    Arc, Mutex,
    atomic::{AtomicUsize, Ordering},
};

/// Test: Auto-execution + Hooks together
///
/// Verifies that auto-execution and hooks work correctly when combined,
/// with hooks intercepting and modifying tool execution during auto-execution.
#[tokio::test]
async fn test_auto_execution_with_hooks_integration() {
    let execution_count = Arc::new(AtomicUsize::new(0));
    let count_clone = execution_count.clone();

    // Tool that can be blocked or modified by hooks
    let calculator = tool("divide", "Divide numbers")
        .param("a", "number")
        .param("b", "number")
        .build(move |args| {
            let count = count_clone.clone();
            async move {
                count.fetch_add(1, Ordering::SeqCst);
                let a = args["a"].as_f64().unwrap_or(0.0);
                let b = args["b"].as_f64().unwrap_or(0.0);

                if b == 0.0 {
                    return Err(open_agent::Error::tool("Division by zero"));
                }

                Ok(json!({"result": a / b}))
            }
        });

    let blocked_count = Arc::new(AtomicUsize::new(0));
    let blocked_clone = blocked_count.clone();

    let hooks = Hooks::new()
        .add_pre_tool_use(move |event| {
            let blocked = blocked_clone.clone();
            async move {
                // Block division by zero before execution
                if let Some(b) = event.tool_input.get("b").and_then(|v| v.as_f64()) {
                    if b == 0.0 {
                        blocked.fetch_add(1, Ordering::SeqCst);
                        return Some(HookDecision::block("Division by zero prevented"));
                    }
                }
                Some(HookDecision::continue_())
            }
        })
        .add_post_tool_use(|event| async move {
            // Add precision info to result
            if event.tool_result.get("result").is_some() {
                let mut enhanced = event.tool_result.clone();
                enhanced["precision"] = json!("high");
                return Some(HookDecision::modify_input(enhanced, "Added precision"));
            }
            None
        });

    let options = AgentOptions::builder()
        .system_prompt("Calculator")
        .model("test")
        .base_url("http://localhost:11434/v1")
        .tool(calculator)
        .hooks(hooks)
        .auto_execute_tools(true)
        .build()
        .unwrap();

    let _client = Client::new(options).expect("Failed to create client");

    // Verify structure is correct
    assert!(execution_count.load(Ordering::SeqCst) == 0);
    assert!(blocked_count.load(Ordering::SeqCst) == 0);
}

/// Test: Auto-execution + Interrupt
///
/// Verifies that interrupt functionality works correctly during auto-execution,
/// allowing long-running auto-execution loops to be stopped gracefully.
#[tokio::test]
async fn test_auto_execution_with_interrupt_integration() {
    let slow_tool = tool("slow_operation", "Slow operation")
        .param("duration_ms", "number")
        .build(|args| async move {
            let duration = args["duration_ms"].as_u64().unwrap_or(100);
            tokio::time::sleep(tokio::time::Duration::from_millis(duration)).await;
            Ok(json!({"completed": true}))
        });

    let options = AgentOptions::builder()
        .system_prompt("Test")
        .model("test")
        .base_url("http://localhost:11434/v1")
        .tool(slow_tool)
        .auto_execute_tools(true)
        .max_tool_iterations(10)
        .build()
        .unwrap();

    let client = Client::new(options).expect("Failed to create client");

    // Interrupt can be used to stop auto-execution
    // The interrupt() method sets the interrupt flag
    client.interrupt();

    // Structure validates interrupt capability during auto-execution
}

/// Test: Auto-execution + Context Management
///
/// Verifies that context management utilities work correctly with auto-execution,
/// allowing proper handling of conversation history and token limits.
#[tokio::test]
async fn test_auto_execution_with_context_management() {
    use open_agent::{estimate_tokens, truncate_messages};

    let echo_tool = tool("echo", "Echo")
        .param("msg", "string")
        .build(|args| async move { Ok(args["msg"].clone()) });

    let options = AgentOptions::builder()
        .system_prompt("Test assistant")
        .model("test")
        .base_url("http://localhost:11434/v1")
        .tool(echo_tool)
        .auto_execute_tools(true)
        .build()
        .unwrap();

    let mut client = Client::new(options).expect("Failed to create client");

    // After auto-execution, check token count
    let initial_tokens = estimate_tokens(client.history());

    // Add a message to history
    client
        .history_mut()
        .push(open_agent::Message::user("Hello"));

    let tokens = estimate_tokens(client.history());
    assert!(tokens > initial_tokens);

    // Truncate if needed
    if tokens > 100 {
        let truncated = truncate_messages(client.history(), 2, true);
        *client.history_mut() = truncated;

        let new_tokens = estimate_tokens(client.history());
        assert!(new_tokens < tokens);
    }
}

/// Test: Multiple hooks with auto-execution
///
/// Verifies that multiple hooks of the same type work correctly together
/// during auto-execution, with proper first-match-wins semantics.
#[tokio::test]
async fn test_multiple_hooks_with_auto_execution() {
    let log = Arc::new(Mutex::new(Vec::new()));
    let log_clone1 = log.clone();
    let log_clone2 = log.clone();

    let test_tool = tool("test_op", "Test operation")
        .param("value", "number")
        .build(|args| async move { Ok(json!({"result": args["value"]})) });

    let hooks = Hooks::new()
        .add_pre_tool_use(move |event| {
            let log = log_clone1.clone();
            async move {
                log.lock()
                    .unwrap()
                    .push(format!("Pre1: {}", event.tool_name));

                // First hook: block if value > 100
                if let Some(value) = event.tool_input.get("value").and_then(|v| v.as_f64()) {
                    if value > 100.0 {
                        return Some(HookDecision::block("Value too large"));
                    }
                }
                None // Continue to next hook
            }
        })
        .add_pre_tool_use(move |event| {
            let log = log_clone2.clone();
            async move {
                log.lock()
                    .unwrap()
                    .push(format!("Pre2: {}", event.tool_name));

                // Second hook: modify if value > 50
                if let Some(value) = event.tool_input.get("value").and_then(|v| v.as_f64()) {
                    if value > 50.0 {
                        return Some(HookDecision::modify_input(
                            json!({"value": 50.0}),
                            "Clamped to 50",
                        ));
                    }
                }
                Some(HookDecision::continue_())
            }
        });

    let options = AgentOptions::builder()
        .system_prompt("Test")
        .model("test")
        .base_url("http://localhost:11434/v1")
        .tool(test_tool)
        .hooks(hooks)
        .auto_execute_tools(true)
        .build()
        .unwrap();

    let _client = Client::new(options).expect("Failed to create client");

    // Multiple hooks should execute in sequence
    // First hook that returns a decision stops the chain
}

/// Test: Auto-execution preserves manual mode compatibility
///
/// Verifies that when auto_execute_tools is false, manual mode still works
/// correctly and hooks behave as expected.
#[tokio::test]
async fn test_manual_mode_still_works_after_auto_execution_implementation() {
    let manual_tool = tool("manual_op", "Manual operation")
        .param("x", "number")
        .build(
            |args| async move { Ok(json!({"result": args["x"].as_f64().unwrap_or(0.0) * 2.0})) },
        );

    let options = AgentOptions::builder()
        .system_prompt("Test")
        .model("test")
        .base_url("http://localhost:11434/v1")
        .tool(manual_tool)
        .auto_execute_tools(false) // ← Manual mode!
        .build()
        .unwrap();

    let client = Client::new(options).expect("Failed to create client");

    // In manual mode, receive() should yield ToolUse blocks
    // User manually calls get_tool() and executes
    assert!(!client.options().auto_execute_tools());

    // Manual execution should still work
    if let Some(tool) = client.get_tool("manual_op") {
        let result = tool.execute(json!({"x": 21.0})).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap()["result"], 42.0);
    } else {
        panic!("Tool not found");
    }
}

/// Test: History management with auto-execution and hooks
///
/// Verifies that conversation history is properly maintained when
/// auto-execution and hooks are used together.
#[tokio::test]
async fn test_history_management_with_auto_execution_and_hooks() {
    let simple_tool = tool("echo", "Echo input")
        .param("msg", "string")
        .build(|args| async move { Ok(args["msg"].clone()) });

    let hooks = Hooks::new().add_post_tool_use(|event| async move {
        // Log but don't modify
        println!("Executed: {}", event.tool_name);
        None
    });

    let options = AgentOptions::builder()
        .system_prompt("Test")
        .model("test")
        .base_url("http://localhost:11434/v1")
        .tool(simple_tool)
        .hooks(hooks)
        .auto_execute_tools(true)
        .build()
        .unwrap();

    let client = Client::new(options).expect("Failed to create client");

    let initial_history_len = client.history().len();

    // After auto-execution with hooks, history should contain:
    // 1. System message (already present)
    // Additional messages would be added during actual auto-execution

    // Verify history structure
    assert!(client.history().len() >= initial_history_len);
}

/// Test: Error handling in auto-execution with hooks
///
/// Verifies that errors are handled gracefully when tools fail
/// during auto-execution, with hooks potentially modifying error results.
#[tokio::test]
async fn test_error_handling_auto_execution_with_hooks() {
    let failing_tool = tool("failing_op", "Operation that fails")
        .param("should_fail", "boolean")
        .build(|args| async move {
            if args["should_fail"].as_bool().unwrap_or(false) {
                return Err(open_agent::Error::tool("Intentional failure"));
            }
            Ok(json!({"success": true}))
        });

    let hooks = Hooks::new().add_post_tool_use(|event| async move {
        // Even if tool failed, we can modify the error result
        if event.tool_result.get("error").is_some() {
            let mut modified = event.tool_result.clone();
            modified["handled_by_hook"] = json!(true);
            return Some(HookDecision::modify_input(modified, "Added error handling"));
        }
        None
    });

    let options = AgentOptions::builder()
        .system_prompt("Test")
        .model("test")
        .base_url("http://localhost:11434/v1")
        .tool(failing_tool)
        .hooks(hooks)
        .auto_execute_tools(true)
        .build()
        .unwrap();

    let _client = Client::new(options).expect("Failed to create client");

    // Tool failures should not crash auto-execution
    // Hooks can modify error results
}

/// Test: Concurrent features don't interfere
///
/// Verifies that features like retry, interrupt, and hooks don't
/// interfere with each other when used together.
#[tokio::test]
async fn test_feature_isolation_and_compatibility() {
    let test_tool = tool("test", "Test")
        .param("x", "number")
        .build(|args| async move { Ok(args["x"].clone()) });

    // All features enabled
    let hooks = Hooks::new()
        .add_pre_tool_use(|_| async { Some(HookDecision::continue_()) })
        .add_post_tool_use(|_| async { None });

    let options = AgentOptions::builder()
        .system_prompt("Test")
        .model("test")
        .base_url("http://localhost:11434/v1")
        .tool(test_tool)
        .hooks(hooks)
        .auto_execute_tools(true)
        .max_tool_iterations(5)
        .build()
        .unwrap();

    let _client = Client::new(options).expect("Failed to create client");

    // All features should coexist without conflicts
}

/// Test: Complex workflow integration
///
/// Realistic scenario combining auto-execution, multiple hooks,
/// context management, and error handling.
#[tokio::test]
async fn test_complex_workflow_integration() {
    let execution_log = Arc::new(Mutex::new(Vec::new()));
    let log_clone = execution_log.clone();

    // Multiple tools
    let calc_tool = tool("calculate", "Calculate")
        .param("a", "number")
        .param("b", "number")
        .param("op", "string")
        .build(|args| async move {
            let a = args["a"].as_f64().unwrap_or(0.0);
            let b = args["b"].as_f64().unwrap_or(0.0);
            let op = args["op"].as_str().unwrap_or("add");

            let result = match op {
                "add" => a + b,
                "multiply" => a * b,
                "divide" => {
                    if b == 0.0 {
                        return Err(open_agent::Error::tool("Division by zero"));
                    }
                    a / b
                }
                _ => a + b,
            };

            Ok(json!({"result": result}))
        });

    let format_tool = tool("format", "Format result")
        .param("value", "number")
        .build(|args| async move {
            let value = args["value"].as_f64().unwrap_or(0.0);
            Ok(json!({"formatted": format!("{:.2}", value)}))
        });

    // Comprehensive hooks
    let hooks = Hooks::new()
        .add_pre_tool_use(|event| async move {
            // Safety check
            if event.tool_name == "calculate" {
                if let Some(op) = event.tool_input.get("op").and_then(|v| v.as_str()) {
                    if op == "divide" {
                        if let Some(b) = event.tool_input.get("b").and_then(|v| v.as_f64()) {
                            if b == 0.0 {
                                return Some(HookDecision::block("Division by zero prevented"));
                            }
                        }
                    }
                }
            }
            Some(HookDecision::continue_())
        })
        .add_post_tool_use(move |event| {
            let log = log_clone.clone();
            async move {
                // Log all executions
                log.lock().unwrap().push(format!(
                    "{}: {:?}",
                    event.tool_name,
                    event.tool_result.get("result")
                ));
                None
            }
        });

    let options = AgentOptions::builder()
        .system_prompt("Advanced calculator")
        .model("test")
        .base_url("http://localhost:11434/v1")
        .tool(calc_tool)
        .tool(format_tool)
        .hooks(hooks)
        .auto_execute_tools(true)
        .max_tool_iterations(10)
        .build()
        .unwrap();

    let _client = Client::new(options).expect("Failed to create client");

    // Complex workflow should work seamlessly
    // - Multiple tools
    // - Safety hooks
    // - Logging hooks
    // - Auto-execution
    // - Error handling
}