Skip to main content

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 external_agents;
113pub mod handlers;
114pub mod logging;
115pub mod message_hooks;
116pub mod metrics_service;
117pub mod model_config_helper;
118pub mod prompt_defaults;
119pub mod reloadable_provider;
120pub mod request_hooks;
121pub mod routes;
122pub mod schedule_app;
123pub mod schedules;
124pub mod server;
125pub mod server_tools;
126pub mod services;
127pub mod session_app;
128pub mod spawn_scheduler;
129pub mod subagent_profiles;
130pub mod tools;
131pub mod workflow;
132
133#[cfg(test)]
134pub(crate) mod test_support {
135    use std::sync::{Mutex, MutexGuard, OnceLock};
136
137    pub(crate) fn env_cache_lock() -> &'static Mutex<()> {
138        static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
139        LOCK.get_or_init(|| Mutex::new(()))
140    }
141
142    pub(crate) fn env_cache_lock_acquire() -> MutexGuard<'static, ()> {
143        env_cache_lock()
144            .lock()
145            .unwrap_or_else(|poisoned| poisoned.into_inner())
146    }
147}
148
149// Re-export commonly used types
150pub use app_state::{AgentRunner, AgentStatus, AppState};
151pub use config::{build_cors, build_security_headers};
152pub use error::AppError;
153pub use routes::{
154    agent_routes, anthropic_routes, bamboo_v1_routes, configure_routes,
155    configure_routes_with_rate_limiting, gemini_routes, openai_prefixed_routes,
156};
157pub use server::{run, run_with_bind, run_with_bind_and_static, WebService};