bamboo_server/lib.rs
1//! Unified HTTP server consolidating web_service and agent/server
2//!
3//! This module provides a single, consolidated server implementation that:
4//! - Eliminates the proxy pattern from web_service
5//! - Provides direct provider access without HTTP callbacks
6//! - Unifies state management across all endpoints
7//! - Supports all API routes (agent, OpenAI, Anthropic, Gemini)
8//!
9//! # Architecture
10//!
11//! The server is organized into several key components:
12//! - **app_state**: Unified state management with direct provider access
13//! - **config**: CORS and security header configuration
14//! - **metrics**: Unified metrics infrastructure
15//! - **handlers**: Agent API handlers (chat, execute, events, etc.)
16//! - **controllers**: Multi-provider API controllers (OpenAI, Anthropic, Gemini)
17//! - **services**: Business logic services
18//! - **routes**: Route configuration for all API endpoints
19//! - **server**: Entry points for running the server
20//!
21//! # Quick Start
22//!
23//! ```no_run
24//! use std::path::PathBuf;
25//! use bamboo_server::run;
26//!
27//! #[tokio::main]
28//! async fn main() -> Result<(), String> {
29//! let app_data_dir = PathBuf::from("/path/to/bamboo-data-dir");
30//! run(app_data_dir, 3456).await
31//! }
32//! ```
33//!
34//! # Server Modes
35//!
36//! The server supports three modes:
37//!
38//! ## 1. Desktop Mode (Default)
39//!
40//! Binds to localhost only, No rate limiting. Perfect for local development.
41//!
42//! ```no_run
43//! use std::path::PathBuf;
44//! use bamboo_server::run;
45//!
46//! #[tokio::main]
47//! async fn main() -> Result<(), String> {
48//! let data_dir = PathBuf::from("./.bamboo");
49//! run(data_dir, 9562).await
50//! }
51//! ```
52//!
53//! ## 2. Docker Mode
54//!
55//! Custom bind address with rate limiting. For containerized deployments.
56//!
57//! ```no_run
58//! use std::path::PathBuf;
59//! use bamboo_server::run_with_bind;
60//!
61//! #[tokio::main]
62//! async fn main() -> Result<(), String> {
63//! let data_dir = PathBuf::from("./.bamboo");
64//! run_with_bind(data_dir, 9562, "0.0.0.0").await
65//! }
66//! ```
67//!
68//! ## 3. Production Mode with Frontend
69//!
70//! Serves static files alongside API. Full production setup.
71//!
72//! ```no_run
73//! use std::path::PathBuf;
74//! use bamboo_server::run_with_bind_and_static;
75//!
76//! #[tokio::main]
77//! async fn main() -> Result<(), String> {
78//! let data_dir = PathBuf::from("./.bamboo");
79//! run_with_bind_and_static(
80//! data_dir,
81//! 9562,
82//! "0.0.0.0",
83//! Some(PathBuf::from("./dist")),
84//! )
85//! .await
86//! }
87//! ```
88//!
89//! # Route Organization
90//!
91//! ## Agent Routes (/api/v1/*)
92//!
93//! Core agent functionality: chat, execute, events, metrics, MCP.
94//!
95//! ## OpenAI Routes (/v1/*)
96//!
97//! OpenAI-compatible API for tool integration.
98//!
99//! ## Anthropic Routes (/anthropic/v1/*)
100//!
101//! Anthropic Claude API compatible endpoints.
102//!
103//! ## Gemini Routes (/gemini/v1beta/*)
104//!
105//! Google Gemini API compatible endpoints.
106
107pub mod app_state;
108pub mod claude_runner;
109pub mod config;
110pub mod config_manager;
111pub mod error;
112pub mod handlers;
113pub mod logging;
114pub mod message_hooks;
115pub mod metrics_service;
116pub mod model_config_helper;
117pub mod prompt_defaults;
118pub mod reloadable_provider;
119pub mod request_hooks;
120pub mod routes;
121pub mod schedule_app;
122pub mod schedules;
123pub mod server;
124pub mod server_tools;
125pub mod services;
126pub mod session_app;
127pub mod spawn_scheduler;
128pub mod tools;
129pub mod workflow;
130
131#[cfg(test)]
132pub(crate) mod test_support {
133 use std::sync::{Mutex, MutexGuard, OnceLock};
134
135 pub(crate) fn env_cache_lock() -> &'static Mutex<()> {
136 static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
137 LOCK.get_or_init(|| Mutex::new(()))
138 }
139
140 pub(crate) fn env_cache_lock_acquire() -> MutexGuard<'static, ()> {
141 env_cache_lock()
142 .lock()
143 .unwrap_or_else(|poisoned| poisoned.into_inner())
144 }
145}
146
147// Re-export commonly used types
148pub use app_state::{AgentRunner, AgentStatus, AppState};
149pub use config::{build_cors, build_security_headers};
150pub use error::AppError;
151pub use routes::{
152 agent_routes, anthropic_routes, bamboo_v1_routes, configure_routes,
153 configure_routes_with_rate_limiting, gemini_routes, openai_prefixed_routes,
154};
155pub use server::{run, run_with_bind, run_with_bind_and_static, WebService};