Skip to main content

spider_agent/
lib.rs

1//! # Spider Agent
2//!
3//! A concurrent-safe multimodal agent for web automation and research.
4//!
5//! ## Features
6//!
7//! - **Concurrent-safe**: Designed to be wrapped in `Arc` for multi-task access
8//! - **Feature-gated**: Only include dependencies you need
9//! - **Multiple LLM providers**: OpenAI, OpenAI-compatible APIs
10//! - **Multiple search providers**: Serper, Brave, Bing, Tavily
11//! - **Browser automation**: Chrome support via chromiumoxide
12//!
13//! ## Quick Start
14//!
15//! ```rust,ignore
16//! use spider_agent::{Agent, AgentConfig};
17//! use std::sync::Arc;
18//!
19//! #[tokio::main]
20//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
21//!     let agent = Arc::new(Agent::builder()
22//!         .with_openai("sk-...", "gpt-4o-mini")
23//!         .with_search_serper("serper-key")
24//!         .build()?);
25//!
26//!     // Search
27//!     let results = agent.search("rust web frameworks").await?;
28//!     println!("Found {} results", results.len());
29//!
30//!     // Extract from first result
31//!     let html = agent.fetch(&results.results[0].url).await?.html;
32//!     let data = agent.extract(&html, "Extract framework name and features").await?;
33//!     println!("{}", serde_json::to_string_pretty(&data)?);
34//!
35//!     Ok(())
36//! }
37//! ```
38//!
39//! ## Concurrent Execution
40//!
41//! ```rust,ignore
42//! use spider_agent::Agent;
43//! use std::sync::Arc;
44//!
45//! let agent = Arc::new(Agent::builder()
46//!     .with_openai("sk-...", "gpt-4o")
47//!     .with_search_serper("serper-key")
48//!     .with_max_concurrent_llm_calls(10)
49//!     .build()?);
50//!
51//! // Execute multiple searches concurrently
52//! let queries = vec!["rust async", "rust web frameworks", "rust databases"];
53//! let mut handles = Vec::new();
54//!
55//! for query in queries {
56//!     let agent = agent.clone();
57//!     let query = query.to_string();
58//!     handles.push(tokio::spawn(async move {
59//!         agent.search(&query).await
60//!     }));
61//! }
62//!
63//! // Collect results
64//! for handle in handles {
65//!     let result = handle.await??;
66//!     println!("Found {} results", result.results.len());
67//! }
68//! ```
69//!
70//! ## Feature Flags
71//!
72//! - `openai` - OpenAI/OpenAI-compatible LLM provider
73//! - `chrome` - Browser automation via chromiumoxide
74//! - `search` - Base search functionality
75//! - `search_serper` - Serper.dev search provider
76//! - `search_brave` - Brave Search provider
77//! - `search_bing` - Bing Search provider
78//! - `search_tavily` - Tavily AI Search provider
79//! - `full` - All features
80
81#![warn(missing_docs)]
82
83mod agent;
84pub mod automation;
85mod config;
86mod error;
87mod llm;
88mod memory;
89pub mod tools;
90
91#[cfg(feature = "search")]
92pub mod search;
93
94#[cfg(feature = "chrome")]
95pub mod browser;
96
97#[cfg(feature = "webdriver")]
98pub mod webdriver;
99
100#[cfg(feature = "fs")]
101pub mod temp;
102
103// Re-exports
104pub use agent::{Agent, AgentBuilder, FetchResult, PageExtraction};
105pub use config::{AgentConfig, HtmlCleaningMode, LimitType, ResearchOptions, RetryConfig, SearchOptions, TimeRange, UsageLimits, UsageSnapshot, UsageStats};
106pub use error::{AgentError, AgentResult, SearchError};
107pub use llm::{CompletionOptions, CompletionResponse, LLMProvider, Message, MessageContent, TokenUsage};
108pub use memory::AgentMemory;
109pub use tools::{AuthConfig, CustomTool, CustomToolRegistry, CustomToolResult, HttpMethod};
110
111// Automation re-exports - core types
112pub use automation::{
113    ActionRecord, ActionResult, ActionType, ActResult, AutomationConfig, AutomationResult, AutomationUsage,
114    CaptureProfile, ChainBuilder, ChainCondition, ChainContext, ChainResult, ChainStep,
115    ChainStepResult, CleaningIntent, ClipViewport, ContentAnalysis, CostTier, ExtractionSchema,
116    FormField, FormInfo, HtmlCleaningProfile, InteractiveElement, ModelEndpoint, ModelPolicy,
117    NavigationOption, PageObservation, PromptUrlGate, RecoveryStrategy, RetryPolicy,
118    SelectorCache, SelectorCacheEntry, StructuredOutputConfig, VisionRouteMode,
119};
120
121// Automation re-exports - engine and configuration
122pub use automation::{
123    RemoteMultimodalConfig, RemoteMultimodalConfigs, RemoteMultimodalEngine,
124};
125
126// Automation re-exports - engine error types
127pub use automation::{EngineError, EngineResult};
128
129// Automation re-exports - helper functions
130pub use automation::{
131    best_effort_parse_json_object, extract_assistant_content, extract_last_code_block,
132    extract_last_json_array, extract_last_json_boundaries, extract_last_json_object, extract_usage,
133    fnv1a64, truncate_utf8_tail,
134};
135
136// Automation re-exports - HTML cleaning
137pub use automation::{
138    clean_html, clean_html_base, clean_html_full, clean_html_raw, clean_html_slim,
139    clean_html_with_profile, clean_html_with_profile_and_intent, smart_clean_html,
140};
141
142// Automation re-exports - map result types
143pub use automation::{categories, DiscoveredUrl, MapResult};
144
145// Automation re-exports - memory operations
146pub use automation::{AutomationMemory, MemoryOperation};
147
148// Automation re-exports - system prompts
149pub use automation::{
150    ACT_SYSTEM_PROMPT, CONFIGURATION_SYSTEM_PROMPT, DEFAULT_SYSTEM_PROMPT,
151    EXTRACT_SYSTEM_PROMPT, MAP_SYSTEM_PROMPT, OBSERVE_SYSTEM_PROMPT,
152};
153
154// Automation re-exports - concurrent chain types
155pub use automation::{
156    ConcurrentChainConfig, ConcurrentChainResult, DependencyGraph, DependentStep, StepResult,
157};
158
159// Automation re-exports - confidence types
160pub use automation::{
161    Alternative, ConfidenceRetryStrategy, ConfidenceSummary, ConfidenceTracker, ConfidentStep,
162    Verification, VerificationType,
163};
164
165// Automation re-exports - tool calling types
166pub use automation::{
167    ActionToolSchemas, FunctionCall, FunctionDefinition, ToolCall, ToolCallingMode, ToolDefinition,
168};
169
170// Automation re-exports - HTML diff types
171pub use automation::{
172    ChangeType, DiffStats, ElementChange, HtmlDiffMode, HtmlDiffResult, PageStateDiff,
173};
174
175// Automation re-exports - planning types
176pub use automation::{
177    Checkpoint, CheckpointResult, CheckpointType, ExecutionPlan, PageState, PlanExecutionState,
178    PlanningModeConfig, PlannedStep, ReplanContext,
179};
180
181// Automation re-exports - self-healing types
182pub use automation::{
183    HealedSelectorCache, HealingDiagnosis, HealingRequest, HealingResult, HealingStats,
184    SelectorIssueType, SelfHealingConfig,
185};
186
187// Automation re-exports - synthesis types
188pub use automation::{
189    MultiPageContext, PageContext, PageContribution, SynthesisConfig, SynthesisResult,
190};
191
192// Automation re-exports - schema generation types
193pub use automation::{
194    build_schema_generation_prompt, generate_schema, infer_schema, infer_schema_from_examples,
195    refine_schema, GeneratedSchema, SchemaCache, SchemaGenerationRequest,
196};
197
198// Automation re-exports - self-healing helper functions
199pub use automation::extract_html_context;
200
201// Automation re-exports - tool calling helper functions
202pub use automation::{parse_tool_calls, tool_calls_to_steps};
203
204// Automation re-exports - concurrent chain execution
205pub use automation::execute_graph;
206
207// Performance re-exports
208pub use automation::cache::{CacheStats, CacheValue, SmartCache};
209pub use automation::executor::{BatchExecutor, ChainExecutor, PrefetchManager};
210pub use automation::router::{ModelRouter, RoutingDecision, TaskAnalysis, TaskCategory};
211
212#[cfg(feature = "search")]
213pub use agent::ResearchResult;
214
215#[cfg(feature = "search")]
216pub use search::{SearchProvider, SearchResult, SearchResults};
217
218#[cfg(feature = "openai")]
219pub use llm::OpenAIProvider;
220
221#[cfg(feature = "search_serper")]
222pub use search::SerperProvider;
223
224#[cfg(feature = "search_brave")]
225pub use search::BraveProvider;
226
227#[cfg(feature = "search_bing")]
228pub use search::BingProvider;
229
230#[cfg(feature = "search_tavily")]
231pub use search::TavilyProvider;
232
233#[cfg(feature = "memvid")]
234pub use automation::{
235    ExperienceMemory, ExperienceMemoryConfig, ExperienceOutcome, ExperienceRecord,
236    MemoryStats as ExperienceMemoryStats, RecalledExperience,
237};
238
239#[cfg(feature = "chrome")]
240pub use browser::BrowserContext;
241
242#[cfg(feature = "chrome")]
243pub use automation::{
244    run_remote_multimodal_with_page, run_spawn_pages_concurrent, run_spawn_pages_with_factory,
245    run_spawn_pages_with_options, PageFactory, PageSetupFn, SpawnPageOptions, SpawnedPageResult,
246};
247
248#[cfg(feature = "webdriver")]
249pub use webdriver::WebDriverContext;
250
251#[cfg(feature = "fs")]
252pub use temp::{TempStorage, TempFile};