claude_codes/lib.rs
1//! A tightly typed Rust interface for the Claude Code JSON protocol
2//!
3//! This crate provides type-safe bindings for interacting with the Claude CLI
4//! through its JSON Lines protocol. It handles the complexity of message serialization,
5//! deserialization, and streaming communication with Claude.
6//!
7//! # Quick Start
8//!
9//! Add this crate to your project:
10//! ```bash
11//! cargo add claude-codes
12//! ```
13//!
14//! ## Using the Async Client (Recommended)
15//!
16//! ```ignore
17//! use claude_codes::AsyncClient;
18//!
19//! #[tokio::main]
20//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
21//! // Create a client with automatic version checking
22//! let mut client = AsyncClient::with_defaults().await?;
23//!
24//! // Send a query and stream responses
25//! let mut stream = client.query_stream("What is 2 + 2?").await?;
26//!
27//! while let Some(response) = stream.next().await {
28//! match response {
29//! Ok(output) => {
30//! println!("Received: {}", output.message_type());
31//! // Handle different message types
32//! }
33//! Err(e) => eprintln!("Error: {}", e),
34//! }
35//! }
36//!
37//! Ok(())
38//! }
39//! ```
40//!
41//! ## Using the Sync Client
42//!
43//! ```ignore
44//! use claude_codes::{SyncClient, ClaudeInput};
45//!
46//! fn main() -> Result<(), Box<dyn std::error::Error>> {
47//! // Create a synchronous client
48//! let mut client = SyncClient::with_defaults()?;
49//!
50//! // Build a structured input message
51//! let input = ClaudeInput::user_message("What is 2 + 2?", uuid::Uuid::new_v4());
52//!
53//! // Send and collect all responses
54//! let responses = client.query(input)?;
55//!
56//! for response in responses {
57//! println!("Received: {}", response.message_type());
58//! }
59//!
60//! Ok(())
61//! }
62//! ```
63//!
64//! # Architecture
65//!
66//! The crate is organized into several key modules:
67//!
68//! - [`client`] - High-level async and sync clients for easy interaction
69//! - [`protocol`] - Core JSON Lines protocol implementation
70//! - [`io`] - Top-level message types (`ClaudeInput`, `ClaudeOutput`)
71//! - [`messages`] - Detailed message structures for requests and responses
72//! - [`cli`] - Builder for configuring Claude CLI invocation
73//! - [`error`] - Error types and result aliases
74//! - [`version`] - Version compatibility checking
75//!
76//! # Version Compatibility
77//!
78//! ⚠️ **Important**: The Claude CLI protocol is unstable and evolving. This crate
79//! automatically checks your Claude CLI version and warns if it's newer than tested.
80//!
81//! Current tested version: **2.1.3**
82//!
83//! Report compatibility issues at: <https://github.com/meawoppl/rust-claude-codes/pulls>
84//!
85//! # Message Types
86//!
87//! The protocol uses several message types:
88//!
89//! - **System** - Initialization and metadata messages
90//! - **User** - Input messages from the user
91//! - **Assistant** - Claude's responses
92//! - **Result** - Session completion with timing and cost info
93//!
94//! # Examples
95//!
96//! See the `examples/` directory for complete working examples:
97//! - `async_client.rs` - Simple async client usage
98//! - `sync_client.rs` - Synchronous client usage
99//! - `basic_repl.rs` - Interactive REPL implementation
100
101// Core modules always available
102pub mod error;
103pub mod io;
104pub mod messages;
105pub mod protocol;
106pub mod tool_inputs;
107pub mod types;
108
109// Client modules
110#[cfg(feature = "async-client")]
111pub mod client_async;
112#[cfg(feature = "sync-client")]
113pub mod client_sync;
114
115// Client-related modules
116#[cfg(any(feature = "sync-client", feature = "async-client"))]
117pub mod cli;
118#[cfg(any(feature = "sync-client", feature = "async-client"))]
119pub mod version;
120
121// Core exports always available
122pub use error::{Error, Result};
123pub use io::{
124 AnthropicError, AnthropicErrorDetails, ApiErrorType, AssistantMessageContent, ClaudeInput,
125 ClaudeOutput, ParseError,
126};
127pub use messages::*;
128pub use protocol::{MessageEnvelope, Protocol};
129pub use types::*;
130
131// Content block types for message parsing
132pub use io::{
133 CodeExecutionToolResultBlock, ContainerUploadBlock, ContentBlock, ImageBlock, ImageSource,
134 ImageSourceType, McpToolResultBlock, McpToolUseBlock, MediaType, ServerToolUseBlock, TextBlock,
135 ThinkingBlock, ToolResultBlock, ToolResultContent, WebSearchToolResultBlock,
136};
137
138// Control protocol types for tool permission handling
139pub use io::{
140 AskUserQuestionResponseError, ControlRequest, ControlRequestMessage, ControlRequestPayload,
141 ControlResponse, ControlResponseMessage, ControlResponsePayload, HookCallbackRequest,
142 InitializeRequest, McpMessageRequest, Permission, PermissionBehavior, PermissionDenial,
143 PermissionDestination, PermissionModeName, PermissionResult, PermissionRule,
144 PermissionSuggestion, PermissionType, SDKControlInterruptRequest, ToolPermissionRequest,
145 ToolUseBlock,
146};
147
148// System message and assistant message types
149pub use io::{
150 ApiKeySource, CompactBoundaryMessage, CompactMetadata, CompactionTrigger, InitMessage,
151 InitPermissionMode, MessageRole, OutputStyle, PluginInfo, StatusMessage, StatusMessageStatus,
152 StopReason, SystemMessage, SystemSubtype, TaskNotificationMessage, TaskProgressMessage,
153 TaskStartedMessage, TaskStatus, TaskType, TaskUsage,
154};
155
156// Rate limit types
157pub use io::{
158 OverageDisabledReason, OverageStatus, RateLimitEvent, RateLimitInfo, RateLimitStatus,
159 RateLimitWindow,
160};
161
162// Usage types
163pub use io::{AssistantUsage, CacheCreationDetails};
164
165// Typed tool input types
166pub use tool_inputs::{
167 AllowedPrompt, AskUserQuestionInput, BashInput, EditInput, EnterPlanModeInput,
168 ExitPlanModeInput, GlobInput, GrepInput, GrepOutputMode, KillShellInput, LsInput,
169 MultiEditInput, MultiEditOperation, NotebookCellType, NotebookEditInput, NotebookEditMode,
170 NotebookReadInput, Question, QuestionMetadata, QuestionOption, ReadInput, ScheduleWakeupInput,
171 SkillInput, SubagentType, TaskInput, TaskOutputInput, TodoItem, TodoStatus, TodoWriteInput,
172 ToolInput, ToolSearchInput, WebFetchInput, WebSearchInput, WriteInput,
173};
174
175// Client exports
176#[cfg(feature = "async-client")]
177pub use client_async::{AsyncClient, AsyncStreamProcessor};
178#[cfg(feature = "sync-client")]
179pub use client_sync::{StreamProcessor, SyncClient};
180
181// Client-related exports
182#[cfg(any(feature = "sync-client", feature = "async-client"))]
183pub use cli::{ClaudeCliBuilder, CliFlag, InputFormat, OutputFormat, PermissionMode};
184
185#[cfg(test)]
186mod tests {
187 #[test]
188 fn it_works() {
189 assert_eq!(2 + 2, 4);
190 }
191}