claude-session-types 0.1.0

Type definitions for Claude Code session event parsing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Complete event type hierarchy for Claude Code sessions
//!
//! This module provides a comprehensive type map for ALL event types found in
//! Claude Code JSONL session files, based on detailed analysis in
//! `research/TYPE_HIERARCHY.md`.
//!
//! # Design Philosophy
//!
//! These types serve as a **complete data map** - not for immediate parsing of
//! everything, but to know where data lives and have typed access when needed.
//!
//! # Module Structure
//!
//! - `root` - Top-level event types (7 root types)
//! - `message` - Message content types (5 content types)
//! - `progress` - Progress event types (5+ progress types)
//! - `attachment` - Attachment types (10+ types)
//! - `tool_result` - Tool result types (5+ types)
//! - `system` - System event subtypes (2+ types)
//! - `metadata` - Common metadata structures
//!
//! # Type Hierarchy Overview
//!
//! ```text
//! Level 1: Root Events (.type)
//! ├── progress (82,200)      → progress::ProgressEvent
//! ├── assistant (49,426)     → root::AssistantEvent
//! ├── user (29,913)          → root::UserEvent
//! ├── system (6)             → system::SystemEvent
//! ├── file-history-snapshot  → root::FileHistorySnapshot
//! ├── queue-operation        → root::QueueOperation
//! └── summary                → root::SessionSummary
//!
//! Level 2: Message Content (.message.content[].type)
//! ├── text (18,557)          → message::TextBlock
//! ├── tool_use (30,782)      → message::ToolUseBlock
//! ├── tool_result (29,848)   → message::ToolResultBlock
//! ├── image (5)              → message::ImageBlock
//! └── attachment             → attachment::AttachmentBlock
//!
//! Level 3: Progress Data (.data.type when root .type == 'progress')
//! ├── bash_progress (49,591)  → progress::BashProgressData
//! ├── hook_progress (31,668)  → progress::HookProgressData
//! ├── agent_progress (1,371)  → progress::AgentProgressData
//! ├── query_update            → progress::QueryUpdateData
//! └── search_results_received → progress::SearchResultsData
//!
//! Level 4: Attachment Types (.attachment.type)
//! ├── hook_success (31,648)            → attachment::HookSuccess
//! ├── todo_reminder (2,914)            → attachment::TodoReminder
//! ├── critical_system_reminder (1,096) → attachment::CriticalReminder
//! ├── edited_text_file (302)           → attachment::EditedTextFile
//! ├── edited_notebook_cell             → attachment::EditedNotebookCell
//! ├── file_snapshot                    → attachment::FileSnapshot
//! └── ... (more types)
//!
//! Level 5: Tool Result Types (.toolUseResult.type)
//! ├── text (28,313)  → tool_result::TextResult
//! ├── create (1,895) → tool_result::CreateResult
//! ├── update (155)   → tool_result::UpdateResult
//! ├── delete         → tool_result::DeleteResult
//! ├── read           → tool_result::ReadResult
//! └── error          → tool_result::ErrorResult
//!
//! Level 6: System Subtypes (.subtype when .type == 'system')
//! ├── compact_boundary       → system::CompactBoundary
//! ├── microcompact_boundary  → system::MicrocompactBoundary
//! └── ... (more types)
//! ```
//!
//! # Usage Examples
//!
//! ## Parsing Root Events
//!
//! ```rust
//! use claude_session_types::events::{SessionEvent, ProgressData};
//!
//! let line = r#"{"type": "user", "uuid": "test-uuid", "sessionId": "session-1", "timestamp": "2024-01-01T00:00:00Z", "isSidechain": false, "message": {"role": "user", "content": "hello"}}"#;
//! let event: SessionEvent = serde_json::from_str(line)?;
//!
//! match event {
//!     SessionEvent::User(user) => {
//!         // Access user message content
//!         for _block in &user.message.content {
//!             // Process content blocks
//!         }
//!     }
//!     SessionEvent::Progress(progress) => {
//!         // Access progress data
//!         match &progress.data {
//!             ProgressData::BashProgress(bash) => {
//!                 println!("Output: {}", bash.full_output);
//!             }
//!             ProgressData::AgentProgress(agent) => {
//!                 println!("Agent: {}", agent.agent_id);
//!             }
//!             _ => {}
//!         }
//!     }
//!     _ => {}
//! }
//! # Ok::<(), serde_json::Error>(())
//! ```
//!
//! ## Accessing Nested Content
//!
//! ```rust
//! use claude_session_types::events::{SessionEvent, ContentBlock, ToolUseResult};
//!
//! # let event: SessionEvent = serde_json::from_str(r#"{"type": "user", "uuid": "test", "timestamp": "2024-01-01T00:00:00Z", "sessionId": "test", "parentUuid": null, "isSidechain": false, "cwd": "/test", "message": {"role": "user", "content": []}}"#)?;
//! if let SessionEvent::User(user) = event {
//!     for block in &user.message.content {
//!         match block {
//!             ContentBlock::Text(text) => {
//!                 println!("Text: {}", text.text);
//!             }
//!             ContentBlock::ToolResult(result) => {
//!                 println!("Tool result ID: {}", result.tool_use_id);
//!
//!                 // Access nested tool use result
//!                 if let Some(tool_result) = &result.tool_use_result {
//!                     match tool_result {
//!                         ToolUseResult::Create(create) => {
//!                             println!("Created: {}", create.file_path);
//!                         }
//!                         _ => {}
//!                     }
//!                 }
//!             }
//!             _ => {}
//!         }
//!     }
//! }
//! # Ok::<(), serde_json::Error>(())
//! ```
//!
//! ## Working with Progress Events
//!
//! Progress events contain the full conversation context in
//! `.data.normalized_messages[]`, which recursively contains all event types:
//!
//! ```rust
//! use claude_session_types::events::SessionEvent;
//! use claude_session_types::events::progress::NormalizedMessage;
//!
//! # let event: SessionEvent = serde_json::from_str(r#"{"type": "progress", "uuid": "test", "timestamp": "2024-01-01T00:00:00Z", "sessionId": "test", "parentUuid": null, "isSidechain": false, "cwd": "/test", "data": {"type": "bash_progress", "output": "", "fullOutput": "", "elapsedTimeSeconds": 0, "totalLines": 0, "message": {}, "normalizedMessages": []}}"#)?;
//! if let SessionEvent::Progress(progress) = event {
//!     // Access normalized messages (conversation replay)
//!     if let Some(normalized) = progress.data.normalized_messages() {
//!         for msg in normalized {
//!             match msg {
//!                 NormalizedMessage::User(_user) => {
//!                     println!("User turn");
//!                 }
//!                 NormalizedMessage::Assistant(_assistant) => {
//!                     println!("Assistant turn");
//!                 }
//!                 NormalizedMessage::Attachment(attachment) => {
//!                     println!("Attachment type: {:?}", attachment.attachment_type);
//!                 }
//!                 _ => {}
//!             }
//!         }
//!     }
//! }
//! # Ok::<(), serde_json::Error>(())
//! ```

pub mod attachment;
pub mod message;
pub mod metadata;
pub mod progress;
pub mod root;
pub mod system;
pub mod tool_result;

// Re-export main types for convenience
pub use attachment::{AttachmentBlock, AttachmentType};
pub use message::{ContentBlock, MessageContent};
pub use metadata::EventMetadata;
pub use progress::{ProgressData, ProgressEvent};
pub use root::{
    AgentNameEvent, AiTitleEvent, AssistantMessage, AtisLatchEvent, BridgeSessionEvent,
    CacheCreation, CustomTitleEvent, FileHistoryDeltaEvent, FileHistorySnapshot, LastPromptEvent,
    ModeEvent, OriginInfo, PermissionModeEvent, QueueOperation, RootAttachmentEvent, SessionEvent,
    SessionSummary, Snapshot, TokenUsage, UserTurnKind,
};
pub use system::{CompactMetadata, SystemEvent};
pub use tool_result::ToolUseResult;

// Re-export for backward compatibility with existing code
pub use root::QueueOperation as QueueOperationEvent;
pub use root::{AssistantEvent as AssistantMessageEvent, UserEvent as UserMessageEvent};

// ============================================================================
// Legacy Implementations for Backward Compatibility
// ============================================================================

impl SessionEvent {
    /// Extract tags for indexing (legacy method)
    ///
    /// Tags enable fast filtering of events without parsing full content.
    pub fn extract_tags(&self) -> Vec<String> {
        match self {
            Self::User(e) => {
                let mut tags = vec!["user".to_string()];

                if e.metadata.user_type.as_deref() == Some("external") {
                    tags.push("prompt".to_string());
                }

                if e.tool_use_result.is_some() {
                    tags.push("tool_result".to_string());
                }

                tags
            }

            Self::Assistant(e) => {
                let mut tags = vec!["assistant".to_string()];
                tags.push(e.message.model.clone());

                for block in &e.message.content {
                    match block {
                        ContentBlock::Text(_) if !tags.contains(&"text".to_string()) => {
                            tags.push("text".to_string());
                        }
                        ContentBlock::Text(_) => {}
                        ContentBlock::ToolUse(tool) => {
                            tags.push("tool_use".to_string());
                            tags.push(tool.name.clone());
                        }
                        _ => {}
                    }
                }

                tags
            }

            Self::Progress(e) => {
                let mut tags = vec!["progress".to_string()];

                match &e.data {
                    ProgressData::BashProgress(_) => {
                        tags.push("bash_progress".to_string());
                    }
                    ProgressData::AgentProgress(agent) => {
                        tags.push("agent_progress".to_string());
                        tags.push(agent.agent_id.clone());
                        if let Some(slug) = &e.metadata.slug {
                            tags.push(slug.clone());
                        }
                    }
                    ProgressData::HookProgress(hook) => {
                        tags.push("hook_progress".to_string());
                        tags.push(hook.hook_name.clone());
                    }
                    ProgressData::QueryUpdate(_) => {
                        tags.push("query_update".to_string());
                    }
                    ProgressData::SearchResultsReceived(_) => {
                        tags.push("search_results".to_string());
                    }
                    ProgressData::WaitingForTask(task) => {
                        tags.push("waiting_for_task".to_string());
                        tags.push(task.task_type.clone());
                    }
                    ProgressData::Unknown => {
                        tags.push("unknown_progress".to_string());
                    }
                }

                tags
            }

            Self::System(e) => {
                let mut tags = vec!["system".to_string()];

                if let Some(subtype) = &e.subtype {
                    tags.push(subtype.clone());

                    if e.is_compact_boundary() {
                        if let Some(meta) = &e.compact_metadata {
                            tags.push(meta.trigger.clone());
                        }
                    }
                }

                tags
            }

            Self::FileSnapshot(_) => vec!["file_snapshot".to_string()],

            Self::QueueOperation(e) => {
                vec!["queue_operation".to_string(), e.operation.clone()]
            }

            Self::Summary(_) => vec!["summary".to_string()],

            Self::Attachment(e) => {
                vec!["attachment".to_string(), e.attachment.type_name().to_string()]
            }
            Self::CustomTitle(_) => vec!["custom_title".to_string()],
            Self::AiTitle(_) => vec!["ai_title".to_string()],
            Self::LastPrompt(_) => vec!["last_prompt".to_string()],
            Self::BridgeSession(_) => vec!["bridge_session".to_string()],
            Self::AtisLatch(_) => vec!["atis_latch".to_string()],
            Self::Mode(e) => vec!["mode".to_string(), e.mode.clone()],
            Self::PermissionMode(e) => {
                vec!["permission_mode".to_string(), e.permission_mode.clone()]
            }
            Self::AgentName(_) => vec!["agent_name".to_string()],
            Self::FileHistoryDelta(_) => vec!["file_history_delta".to_string()],

            Self::Unknown => vec!["unknown".to_string()],
        }
    }

    /// Check if this event is important for context extraction
    pub fn is_context_relevant(&self) -> bool {
        match self {
            Self::Progress(e) => matches!(e.data, ProgressData::AgentProgress(_)),
            Self::User(e) => e.metadata.user_type.as_deref() == Some("external"),
            Self::Assistant(e) => e
                .message
                .content
                .iter()
                .any(|block| matches!(block, ContentBlock::Text(_))),
            Self::System(e) => e.is_compact_boundary(),
            _ => false,
        }
    }
}

impl root::TokenUsage {
    /// Total tokens (input + output)
    pub fn total(&self) -> u64 {
        self.input_tokens + self.output_tokens
    }

    /// Total input including cache operations
    pub fn total_input(&self) -> u64 {
        self.input_tokens + self.cache_creation_input_tokens + self.cache_read_input_tokens
    }

    /// Effective input tokens with cache discount (90% for reads)
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn effective_input(&self) -> f64 {
        (self.input_tokens + self.cache_creation_input_tokens) as f64
            + (self.cache_read_input_tokens as f64 * 0.1)
    }

    /// Calculate cost in USD based on model pricing
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn calculate_cost(&self, model: &str) -> Option<f64> {
        let (input_cost, output_cost) = match normalize_model_name(model) {
            "haiku" => (1.0 / 1_000_000.0, 5.0 / 1_000_000.0),
            "sonnet" => (3.0 / 1_000_000.0, 15.0 / 1_000_000.0),
            "opus" => (15.0 / 1_000_000.0, 75.0 / 1_000_000.0),
            _ => return None,
        };

        let cache_write_cost = input_cost * 1.25;
        let cache_read_cost = input_cost * 0.1;

        Some(
            (self.input_tokens as f64 * input_cost)
                + (self.output_tokens as f64 * output_cost)
                + (self.cache_creation_input_tokens as f64 * cache_write_cost)
                + (self.cache_read_input_tokens as f64 * cache_read_cost),
        )
    }

    /// Format cost as human-readable string
    #[must_use]
    pub fn format_cost(&self, model: &str) -> String {
        match self.calculate_cost(model) {
            Some(cost) => {
                if cost < 0.01 {
                    format!("${cost:.4}")
                } else {
                    format!("${cost:.2}")
                }
            }
            None => "Unknown model".to_string(),
        }
    }

    /// Calculate approximate cost (deprecated)
    #[deprecated(since = "0.1.0", note = "Use calculate_cost(model) instead")]
    pub fn estimated_cost(&self) -> f64 {
        self.calculate_cost("sonnet").unwrap_or(0.0)
    }
}

/// Normalize model name to "haiku", "sonnet", or "opus"
fn normalize_model_name(model: &str) -> &str {
    let lower = model.to_lowercase();
    if lower.contains("haiku") {
        "haiku"
    } else if lower.contains("sonnet") {
        "sonnet"
    } else if lower.contains("opus") {
        "opus"
    } else {
        "unknown"
    }
}