claude_session_parser/lib.rs
1//! Streaming parser for [Claude Code](https://claude.com/claude-code) session JSONL files.
2//!
3//! Three modules, all `pub`:
4//!
5//! - [`usage`] — per-assistant-turn token/cost/tool entries (`UsageEntry`), session
6//! summaries (`SessionUsage`), hook executions (`HookExecution`), pricing tables,
7//! and project-path encoding compatible with Claude Code's `~/.claude/projects/`
8//! directory naming.
9//! - [`chat`] — flat, timestamp-ordered `ChatEvent` stream for rendering the
10//! conversation (user / assistant / thinking / tool-use / tool-result blocks).
11//! - [`activity`] — deterministic classifier that tags each assistant turn with one
12//! of 13 categories (Coding, Debugging, Refactor, Testing, Exploration, …).
13//!
14//! Parsing is streaming (`BufReader::lines()`), tolerates malformed lines, and
15//! caps inputs at [`MAX_JSONL_BYTES`] (200MB) so a corrupted or runaway file
16//! cannot block a caller.
17//!
18//! See the crate README for the trust-surface description, the JSONL format
19//! notes (split content blocks, `<synthetic>` skip, hook formats), and a usage
20//! example.
21
22pub mod activity;
23pub mod chat;
24pub mod usage;
25
26/// Hard cap on JSONL file size accepted by [`usage::parse_jsonl_entries`] and
27/// [`chat::parse_chat_events`].
28///
29/// Parsing is streaming (`BufReader::lines()`), so memory is O(1) per line —
30/// this cap only exists to keep a runaway / corrupted file from blocking the
31/// caller indefinitely. Real agent sessions (Task-tool sub-agents) frequently
32/// exceed 30MB, so the cap is set generously at 200MB.
33pub const MAX_JSONL_BYTES: u64 = 200 * 1024 * 1024;
34
35pub use activity::{classify, normalize_user_text, Activity};
36pub use chat::{parse_chat_events, ChatEvent};
37pub use usage::{
38 context_window_for_model, encode_project_path, find_current_session_jsonl,
39 parse_jsonl_entries, pricing_for_entry, pricing_for_model, reduce_session, HookExecution,
40 ModelPricing, ParsedJsonl, SessionUsage, UsageEntry,
41};