pub enum ClaudeOutput {
System(SystemMessage),
User(UserMessage),
Assistant(AssistantMessage),
Result(ResultMessage),
ControlRequest(ControlRequest),
ControlResponse(ControlResponse),
Error(AnthropicError),
RateLimitEvent(RateLimitEvent),
}Expand description
Top-level enum for all possible Claude output messages
Variants§
System(SystemMessage)
System initialization message
User(UserMessage)
User message echoed back
Assistant(AssistantMessage)
Assistant response
Result(ResultMessage)
Result message (completion of a query)
ControlRequest(ControlRequest)
Control request from CLI (tool permissions, hooks, etc.)
ControlResponse(ControlResponse)
Control response from CLI (ack for initialization, etc.)
Error(AnthropicError)
API error from Anthropic (500, 529 overloaded, etc.)
RateLimitEvent(RateLimitEvent)
Rate limit status event
Implementations§
Source§impl ClaudeOutput
impl ClaudeOutput
Sourcepub fn message_type(&self) -> String
pub fn message_type(&self) -> String
Get the message type as a string
Sourcepub fn is_control_request(&self) -> bool
pub fn is_control_request(&self) -> bool
Check if this is a control request (tool permission request)
Sourcepub fn is_control_response(&self) -> bool
pub fn is_control_response(&self) -> bool
Check if this is a control response
Sourcepub fn is_api_error(&self) -> bool
pub fn is_api_error(&self) -> bool
Check if this is an Anthropic API error
Sourcepub fn as_control_request(&self) -> Option<&ControlRequest>
pub fn as_control_request(&self) -> Option<&ControlRequest>
Get the control request if this is one
Sourcepub fn as_anthropic_error(&self) -> Option<&AnthropicError>
pub fn as_anthropic_error(&self) -> Option<&AnthropicError>
Get the Anthropic error if this is one
§Example
use claude_codes::ClaudeOutput;
let json = r#"{"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}"#;
let output: ClaudeOutput = serde_json::from_str(json).unwrap();
if let Some(err) = output.as_anthropic_error() {
if err.is_overloaded() {
println!("API is overloaded, retrying...");
}
}Sourcepub fn is_rate_limit_event(&self) -> bool
pub fn is_rate_limit_event(&self) -> bool
Check if this is a rate limit event
Sourcepub fn as_rate_limit_event(&self) -> Option<&RateLimitEvent>
pub fn as_rate_limit_event(&self) -> Option<&RateLimitEvent>
Get the rate limit event if this is one
Sourcepub fn is_assistant_message(&self) -> bool
pub fn is_assistant_message(&self) -> bool
Check if this is an assistant message
Sourcepub fn is_system_message(&self) -> bool
pub fn is_system_message(&self) -> bool
Check if this is a system message
Sourcepub fn is_system_init(&self) -> bool
pub fn is_system_init(&self) -> bool
Check if this is a system init message
§Example
use claude_codes::ClaudeOutput;
let json = r#"{"type":"system","subtype":"init","session_id":"abc"}"#;
let output: ClaudeOutput = serde_json::from_str(json).unwrap();
assert!(output.is_system_init());Sourcepub fn session_id(&self) -> Option<&str>
pub fn session_id(&self) -> Option<&str>
Get the session ID from any message type that has one.
Returns the session ID from System, Assistant, or Result messages.
Returns None for User, ControlRequest, and ControlResponse messages.
§Example
use claude_codes::ClaudeOutput;
let json = r#"{"type":"result","subtype":"success","is_error":false,
"duration_ms":100,"duration_api_ms":200,"num_turns":1,
"session_id":"my-session","total_cost_usd":0.01}"#;
let output: ClaudeOutput = serde_json::from_str(json).unwrap();
assert_eq!(output.session_id(), Some("my-session"));Sourcepub fn as_tool_use(&self, tool_name: &str) -> Option<&ToolUseBlock>
pub fn as_tool_use(&self, tool_name: &str) -> Option<&ToolUseBlock>
Get a specific tool use by name from an assistant message.
Returns the first ToolUseBlock with the given name, or None if this
is not an assistant message or doesn’t contain the specified tool.
§Example
use claude_codes::ClaudeOutput;
let json = r#"{"type":"assistant","message":{"id":"msg_1","role":"assistant",
"model":"claude-3","content":[{"type":"tool_use","id":"tu_1",
"name":"Bash","input":{"command":"ls"}}]},"session_id":"abc"}"#;
let output: ClaudeOutput = serde_json::from_str(json).unwrap();
if let Some(bash) = output.as_tool_use("Bash") {
assert_eq!(bash.name, "Bash");
}Sourcepub fn tool_uses(&self) -> impl Iterator<Item = &ToolUseBlock>
pub fn tool_uses(&self) -> impl Iterator<Item = &ToolUseBlock>
Get all tool uses from an assistant message.
Returns an iterator over all ToolUseBlocks in the message, or an empty
iterator if this is not an assistant message.
§Example
use claude_codes::ClaudeOutput;
let json = r#"{"type":"assistant","message":{"id":"msg_1","role":"assistant",
"model":"claude-3","content":[
{"type":"tool_use","id":"tu_1","name":"Read","input":{"file_path":"/tmp/a"}},
{"type":"tool_use","id":"tu_2","name":"Write","input":{"file_path":"/tmp/b","content":"x"}}
]},"session_id":"abc"}"#;
let output: ClaudeOutput = serde_json::from_str(json).unwrap();
let tools: Vec<_> = output.tool_uses().collect();
assert_eq!(tools.len(), 2);Sourcepub fn text_content(&self) -> Option<String>
pub fn text_content(&self) -> Option<String>
Get text content from an assistant message.
Returns the concatenated text from all text blocks in the message,
or None if this is not an assistant message or has no text content.
§Example
use claude_codes::ClaudeOutput;
let json = r#"{"type":"assistant","message":{"id":"msg_1","role":"assistant",
"model":"claude-3","content":[{"type":"text","text":"Hello, world!"}]},
"session_id":"abc"}"#;
let output: ClaudeOutput = serde_json::from_str(json).unwrap();
assert_eq!(output.text_content(), Some("Hello, world!".to_string()));Sourcepub fn as_assistant(&self) -> Option<&AssistantMessage>
pub fn as_assistant(&self) -> Option<&AssistantMessage>
Get the assistant message if this is one.
§Example
use claude_codes::ClaudeOutput;
let json = r#"{"type":"assistant","message":{"id":"msg_1","role":"assistant",
"model":"claude-3","content":[]},"session_id":"abc"}"#;
let output: ClaudeOutput = serde_json::from_str(json).unwrap();
if let Some(assistant) = output.as_assistant() {
assert_eq!(assistant.message.model, "claude-3");
}Sourcepub fn as_result(&self) -> Option<&ResultMessage>
pub fn as_result(&self) -> Option<&ResultMessage>
Get the result message if this is one.
§Example
use claude_codes::ClaudeOutput;
let json = r#"{"type":"result","subtype":"success","is_error":false,
"duration_ms":100,"duration_api_ms":200,"num_turns":1,
"session_id":"abc","total_cost_usd":0.01}"#;
let output: ClaudeOutput = serde_json::from_str(json).unwrap();
if let Some(result) = output.as_result() {
assert!(!result.is_error);
}Sourcepub fn as_system(&self) -> Option<&SystemMessage>
pub fn as_system(&self) -> Option<&SystemMessage>
Get the system message if this is one.
Sourcepub fn parse_json_tolerant(s: &str) -> Result<ClaudeOutput, ParseError>
pub fn parse_json_tolerant(s: &str) -> Result<ClaudeOutput, ParseError>
Parse a JSON string, handling potential ANSI escape codes and other prefixes This method will:
- First try to parse as-is
- If that fails, trim until it finds a ‘{’ and try again
Sourcepub fn parse_json(s: &str) -> Result<ClaudeOutput, ParseError>
pub fn parse_json(s: &str) -> Result<ClaudeOutput, ParseError>
Parse a JSON string, returning ParseError with raw JSON if it doesn’t match our types
Trait Implementations§
Source§impl Clone for ClaudeOutput
impl Clone for ClaudeOutput
Source§fn clone(&self) -> ClaudeOutput
fn clone(&self) -> ClaudeOutput
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more