open_agent/
lib.rs

1//! # Open Agent SDK - Rust Implementation
2//!
3//! A production-ready, streaming-first Rust SDK for building AI agents with local OpenAI-compatible servers.
4//!
5//! ## Overview
6//!
7//! This SDK provides a clean, ergonomic API for working with local LLM servers such as:
8//! - LM Studio
9//! - Ollama
10//! - llama.cpp
11//! - vLLM
12//!
13//! ## Key Features
14//!
15//! - **Zero API Costs**: Run models on your own hardware
16//! - **Privacy-First**: All data stays local on your machine
17//! - **High Performance**: Native async/await with Tokio runtime
18//! - **Streaming Responses**: Real-time token-by-token streaming
19//! - **Tool Calling**: Define and execute tools with automatic schema generation
20//! - **Lifecycle Hooks**: Intercept and control execution at key points
21//! - **Interrupts**: Gracefully cancel long-running operations
22//! - **Context Management**: Manual token estimation and history truncation
23//! - **Retry Logic**: Exponential backoff with jitter for reliability
24//!
25//! ## Two Interaction Modes
26//!
27//! ### 1. Simple Query Function (`query()`)
28//! For single-turn interactions without conversation state:
29//!
30//! ```rust,no_run
31//! use open_agent::{query, AgentOptions, ContentBlock};
32//! use futures::StreamExt;
33//!
34//! #[tokio::main]
35//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
36//!     // Configure the agent with required settings
37//!     let options = AgentOptions::builder()
38//!         .system_prompt("You are a helpful assistant")
39//!         .model("qwen2.5-32b-instruct")
40//!         .base_url("http://localhost:1234/v1")
41//!         .build()?;
42//!
43//!     // Send a single query and stream the response
44//!     let mut stream = query("What's the capital of France?", &options).await?;
45//!
46//!     // Process each content block as it arrives
47//!     while let Some(block) = stream.next().await {
48//!         match block? {
49//!             ContentBlock::Text(text_block) => {
50//!                 print!("{}", text_block.text);
51//!             }
52//!             ContentBlock::ToolUse(tool_block) => {
53//!                 println!("Tool called: {}", tool_block.name);
54//!             }
55//!             ContentBlock::ToolResult(_) => {
56//!                 // Tool results can be ignored in simple queries
57//!             }
58//!         }
59//!     }
60//!
61//!     Ok(())
62//! }
63//! ```
64//!
65//! ### 2. Client Object (`Client`)
66//! For multi-turn conversations with persistent state:
67//!
68//! ```rust,no_run
69//! use open_agent::{Client, AgentOptions, ContentBlock};
70//! use futures::StreamExt;
71//!
72//! #[tokio::main]
73//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
74//!     let options = AgentOptions::builder()
75//!         .system_prompt("You are a helpful assistant")
76//!         .model("qwen2.5-32b-instruct")
77//!         .base_url("http://localhost:1234/v1")
78//!         .build()?;
79//!
80//!     // Create a stateful client that maintains conversation history
81//!     let mut client = Client::new(options)?;
82//!
83//!     // First turn
84//!     client.send("What's 2+2?").await?;
85//!     while let Some(block) = client.receive().await? {
86//!         if let ContentBlock::Text(text) = block {
87//!             print!("{}", text.text);
88//!         }
89//!     }
90//!
91//!     // Second turn - client remembers previous context
92//!     client.send("What about if we multiply that by 3?").await?;
93//!     while let Some(block) = client.receive().await? {
94//!         if let ContentBlock::Text(text) = block {
95//!             print!("{}", text.text);
96//!         }
97//!     }
98//!
99//!     Ok(())
100//! }
101//! ```
102//!
103//! ## Architecture
104//!
105//! The SDK is organized into several modules, each with a specific responsibility:
106//!
107//! - **client**: Core streaming query engine and multi-turn client
108//! - **types**: Data structures for messages, content blocks, and configuration
109//! - **tools**: Tool definition system with automatic JSON schema generation
110//! - **hooks**: Lifecycle event system for intercepting execution
111//! - **config**: Provider-specific configuration helpers
112//! - **error**: Comprehensive error types and conversions
113//! - **context**: Token estimation and message truncation utilities
114//! - **retry**: Exponential backoff retry logic with jitter
115//! - **utils**: Internal utilities for SSE parsing and tool aggregation
116
117// ============================================================================
118// MODULE DECLARATIONS
119// ============================================================================
120// These modules are private (internal implementation details) unless explicitly
121// re-exported through `pub use` statements below.
122
123/// Core client implementation providing streaming queries and stateful conversations.
124/// Contains the `query()` function for single-turn queries and `Client` struct
125/// for multi-turn conversations with automatic state management.
126mod client;
127
128/// Provider configuration helpers for LM Studio, Ollama, llama.cpp, and vLLM.
129/// Simplifies endpoint and model name resolution with environment variable support.
130mod config;
131
132/// Context window management utilities for token estimation and history truncation.
133/// Provides manual control over conversation memory to prevent context overflow.
134mod context;
135
136/// Error types and conversions for comprehensive error handling throughout the SDK.
137/// Defines the `Error` enum and `Result<T>` type alias used across all public APIs.
138mod error;
139
140/// Lifecycle hooks system for intercepting and controlling execution at key points.
141/// Enables security gates, audit logging, input/output modification, and compliance checks.
142mod hooks;
143
144/// Tool definition and execution system with automatic JSON schema generation.
145/// Allows LLMs to call Rust functions with type-safe parameter handling.
146mod tools;
147
148/// Core type definitions for messages, content blocks, and agent configuration.
149/// Includes builder patterns for ergonomic configuration and OpenAI API serialization.
150mod types;
151
152/// Internal utilities for Server-Sent Events (SSE) parsing and tool call aggregation.
153/// Handles the low-level details of streaming response parsing.
154mod utils;
155
156// ============================================================================
157// PUBLIC EXPORTS
158// ============================================================================
159// These items form the public API of the SDK. Everything else is internal.
160
161/// Retry utilities with exponential backoff and jitter.
162/// Made public as a module so users can access retry configuration and functions
163/// for their own operations that need retry logic.
164pub mod retry;
165
166// --- Core Client API ---
167
168pub use client::{Client, query};
169
170// --- Provider Configuration ---
171
172pub use config::{Provider, get_base_url, get_model};
173
174// --- Context Management ---
175
176pub use context::{estimate_tokens, is_approaching_limit, truncate_messages};
177
178// --- Error Handling ---
179
180pub use error::{Error, Result};
181
182// --- Lifecycle Hooks ---
183
184pub use hooks::{
185    HOOK_POST_TOOL_USE, HOOK_PRE_TOOL_USE, HOOK_USER_PROMPT_SUBMIT, HookDecision, Hooks,
186    PostToolUseEvent, PreToolUseEvent, UserPromptSubmitEvent,
187};
188
189// --- Tool System ---
190
191pub use tools::{Tool, ToolBuilder, tool};
192
193// --- Core Types ---
194
195pub use types::{
196    AgentOptions, AgentOptionsBuilder, BaseUrl, ContentBlock, Message, MessageRole, ModelName,
197    Temperature, TextBlock, ToolResultBlock, ToolUseBlock,
198};
199
200// ============================================================================
201// CONVENIENCE PRELUDE
202// ============================================================================
203
204/// Convenience module containing the most commonly used types and functions.
205/// Import with `use open_agent::prelude::*;` to get everything you need for typical usage.
206///
207/// This includes:
208/// - Configuration: AgentOptions, AgentOptionsBuilder
209/// - Client: Client, query()
210/// - Content: ContentBlock, TextBlock, ToolUseBlock
211/// - Tools: Tool, tool()
212/// - Hooks: Hooks, HookDecision, hook event types
213/// - Errors: Error, Result
214pub mod prelude {
215    pub use crate::{
216        AgentOptions, AgentOptionsBuilder, BaseUrl, Client, ContentBlock, Error, HookDecision,
217        Hooks, ModelName, PostToolUseEvent, PreToolUseEvent, Result, Temperature, TextBlock, Tool,
218        ToolUseBlock, UserPromptSubmitEvent, query, tool,
219    };
220}