serdes_ai_agent/lib.rs
1//! Agent implementation for serdes-ai.
2//!
3//! The agent is the core abstraction for building AI applications. It provides:
4//!
5//! - Model orchestration
6//! - Tool registration and execution
7//! - Structured output parsing
8//! - Retry logic and error handling
9//! - Usage tracking and limits
10//!
11//! # Example
12//!
13//! ```rust,ignore
14//! use serdes_ai_agent::{agent, EndStrategy};
15//! use serdes_ai_models::openai::OpenAIChatModel;
16//!
17//! // Create a simple agent
18//! let model = OpenAIChatModel::new("gpt-4o", "sk-...");
19//! let agent = agent(model)
20//! .system_prompt("You are a helpful assistant.")
21//! .temperature(0.7)
22//! .build();
23//!
24//! // Run the agent
25//! let result = agent.run("Hello!", ()).await?;
26//! println!("Response: {}", result.output());
27//! ```
28//!
29//! # With Tools
30//!
31//! ```rust,ignore
32//! use serdes_ai_agent::agent;
33//! use serdes_ai_tools::ToolReturn;
34//!
35//! let agent = agent(model)
36//! .system_prompt("You can search the web.")
37//! .tool_fn(
38//! "search",
39//! "Search the web for information",
40//! serde_json::json!({
41//! "type": "object",
42//! "properties": {
43//! "query": {"type": "string"}
44//! },
45//! "required": ["query"]
46//! }),
47//! |ctx, args: serde_json::Value| {
48//! let query = args["query"].as_str().unwrap();
49//! Ok(ToolReturn::text(format!("Results for: {}", query)))
50//! },
51//! )
52//! .build();
53//! ```
54//!
55//! # Structured Output
56//!
57//! ```rust,ignore
58//! use serde::Deserialize;
59//!
60//! #[derive(Debug, Deserialize)]
61//! struct Analysis {
62//! sentiment: String,
63//! score: f64,
64//! }
65//!
66//! let agent = agent(model)
67//! .output_type::<Analysis>()
68//! .build();
69//!
70//! let result = agent.run("Analyze: I love Rust!", ()).await?;
71//! println!("Sentiment: {} ({})", result.output.sentiment, result.output.score);
72//! ```
73
74pub mod agent;
75pub mod builder;
76pub mod context;
77pub mod errors;
78pub mod history;
79pub mod instructions;
80pub mod output;
81pub mod run;
82pub mod stream;
83
84// Re-exports
85pub use agent::{Agent, EndStrategy, InstrumentationSettings, RegisteredTool, ToolExecutor};
86pub use builder::{agent, agent_with_deps, AgentBuilder, ModelConfig};
87pub use context::{generate_run_id, RunContext, RunUsage, UsageLimits};
88pub use errors::{
89 AgentBuildError, AgentRunError, OutputParseError, OutputValidationError, UsageLimitError,
90};
91pub use history::{
92 ChainedProcessor, FilterHistory, FnProcessor, HistoryProcessor, SummarizeHistory,
93 TruncateByTokens, TruncateHistory,
94};
95pub use instructions::{
96 AsyncInstructionFn, AsyncSystemPromptFn, DateTimeInstruction, InstructionBuilder,
97 InstructionFn, StaticInstruction, StaticSystemPrompt, SyncInstructionFn, SyncSystemPromptFn,
98 SystemPromptFn,
99};
100pub use output::{
101 AsyncValidator, ChainedValidator, DefaultOutputSchema, JsonOutputSchema, LengthValidator,
102 NonEmptyValidator, OutputMode, OutputSchema, OutputValidator, SyncValidator, TextOutputSchema,
103 ToolOutputSchema,
104};
105pub use run::{
106 AgentRun, AgentRunResult, CompressionStrategy, ContextCompression, RunOptions, StepResult,
107};
108pub use stream::{AgentStream, AgentStreamEvent};
109
110// Re-export CancellationToken for convenience
111pub use tokio_util::sync::CancellationToken;
112
113/// Prelude for common imports.
114pub mod prelude {
115 pub use crate::{
116 agent, agent_with_deps, Agent, AgentBuilder, AgentRun, AgentRunError, AgentRunResult,
117 AgentStream, AgentStreamEvent, CancellationToken, CompressionStrategy, ContextCompression,
118 EndStrategy, OutputMode, OutputSchema, OutputValidator, RunContext, RunOptions, RunUsage,
119 StepResult, UsageLimits,
120 };
121}
122
123#[cfg(test)]
124mod tests {
125
126 #[test]
127 fn test_prelude_imports() {
128 // Just verify the prelude compiles
129 use crate::prelude::*;
130 let _ = EndStrategy::Early;
131 let _ = OutputMode::Text;
132 }
133}