Skip to main content

ccboard_core/models/
session.rs

1//! Session models for JSONL session files
2
3use chrono::{DateTime, Utc};
4use rusqlite::types::{FromSql, FromSqlError, ToSql, ToSqlOutput, ValueRef};
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7use std::borrow::{Borrow, Cow};
8use std::fmt;
9use std::ops::{Deref, Index, Range, RangeFrom, RangeFull, RangeTo};
10use std::path::PathBuf;
11
12/// Newtype for Session ID - zero-cost type safety
13#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
14#[serde(transparent)]
15pub struct SessionId(String);
16
17impl SessionId {
18    /// Create a new SessionId
19    pub fn new(id: String) -> Self {
20        Self(id)
21    }
22
23    /// Get reference to inner string
24    pub fn as_str(&self) -> &str {
25        &self.0
26    }
27
28    /// Extract inner String, consuming self
29    pub fn into_inner(self) -> String {
30        self.0
31    }
32
33    /// Check if the session ID is empty
34    pub fn is_empty(&self) -> bool {
35        self.0.is_empty()
36    }
37
38    /// Get an iterator over the characters
39    pub fn chars(&self) -> std::str::Chars<'_> {
40        self.0.chars()
41    }
42
43    /// Check if the session ID starts with a given pattern
44    pub fn starts_with(&self, pattern: &str) -> bool {
45        self.0.starts_with(pattern)
46    }
47
48    /// Get the length of the session ID
49    pub fn len(&self) -> usize {
50        self.0.len()
51    }
52}
53
54impl From<String> for SessionId {
55    fn from(s: String) -> Self {
56        Self(s)
57    }
58}
59
60impl From<&str> for SessionId {
61    fn from(s: &str) -> Self {
62        Self(s.to_string())
63    }
64}
65
66impl fmt::Display for SessionId {
67    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68        write!(f, "{}", self.0)
69    }
70}
71
72impl AsRef<str> for SessionId {
73    fn as_ref(&self) -> &str {
74        &self.0
75    }
76}
77
78impl PartialEq<str> for SessionId {
79    fn eq(&self, other: &str) -> bool {
80        self.0 == other
81    }
82}
83
84impl PartialEq<&str> for SessionId {
85    fn eq(&self, other: &&str) -> bool {
86        self.0 == *other
87    }
88}
89
90impl PartialEq<String> for SessionId {
91    fn eq(&self, other: &String) -> bool {
92        &self.0 == other
93    }
94}
95
96impl ToSql for SessionId {
97    fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
98        Ok(ToSqlOutput::from(self.0.as_str()))
99    }
100}
101
102impl FromSql for SessionId {
103    fn column_result(value: ValueRef<'_>) -> Result<Self, FromSqlError> {
104        value.as_str().map(|s| SessionId::from(s))
105    }
106}
107
108impl Borrow<str> for SessionId {
109    fn borrow(&self) -> &str {
110        &self.0
111    }
112}
113
114impl Deref for SessionId {
115    type Target = str;
116
117    fn deref(&self) -> &Self::Target {
118        &self.0
119    }
120}
121
122impl Index<RangeFull> for SessionId {
123    type Output = str;
124
125    fn index(&self, _index: RangeFull) -> &Self::Output {
126        &self.0
127    }
128}
129
130impl Index<Range<usize>> for SessionId {
131    type Output = str;
132
133    fn index(&self, index: Range<usize>) -> &Self::Output {
134        &self.0[index]
135    }
136}
137
138impl Index<RangeFrom<usize>> for SessionId {
139    type Output = str;
140
141    fn index(&self, index: RangeFrom<usize>) -> &Self::Output {
142        &self.0[index]
143    }
144}
145
146impl Index<RangeTo<usize>> for SessionId {
147    type Output = str;
148
149    fn index(&self, index: RangeTo<usize>) -> &Self::Output {
150        &self.0[index]
151    }
152}
153
154impl<'a> From<&'a SessionId> for Cow<'a, str> {
155    fn from(id: &'a SessionId) -> Self {
156        Cow::Borrowed(id.as_str())
157    }
158}
159
160/// Newtype for Project ID - zero-cost type safety
161#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
162#[serde(transparent)]
163pub struct ProjectId(String);
164
165impl ProjectId {
166    /// Create a new ProjectId
167    pub fn new(id: String) -> Self {
168        Self(id)
169    }
170
171    /// Get reference to inner string
172    pub fn as_str(&self) -> &str {
173        &self.0
174    }
175
176    /// Extract inner String, consuming self
177    pub fn into_inner(self) -> String {
178        self.0
179    }
180
181    /// Get the length of the project ID
182    pub fn len(&self) -> usize {
183        self.0.len()
184    }
185
186    /// Check if the project ID is empty
187    #[allow(dead_code)]
188    pub fn is_empty(&self) -> bool {
189        self.0.is_empty()
190    }
191
192    /// Convert to lowercase
193    pub fn to_lowercase(&self) -> String {
194        self.0.to_lowercase()
195    }
196}
197
198impl From<String> for ProjectId {
199    fn from(s: String) -> Self {
200        Self(s)
201    }
202}
203
204impl From<&str> for ProjectId {
205    fn from(s: &str) -> Self {
206        Self(s.to_string())
207    }
208}
209
210impl fmt::Display for ProjectId {
211    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
212        write!(f, "{}", self.0)
213    }
214}
215
216impl AsRef<str> for ProjectId {
217    fn as_ref(&self) -> &str {
218        &self.0
219    }
220}
221
222impl ToSql for ProjectId {
223    fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
224        Ok(ToSqlOutput::from(self.0.as_str()))
225    }
226}
227
228impl FromSql for ProjectId {
229    fn column_result(value: ValueRef<'_>) -> Result<Self, FromSqlError> {
230        value.as_str().map(|s| ProjectId::from(s))
231    }
232}
233
234impl Deref for ProjectId {
235    type Target = str;
236
237    fn deref(&self) -> &Self::Target {
238        &self.0
239    }
240}
241
242impl<'a> From<&'a ProjectId> for Cow<'a, str> {
243    fn from(id: &'a ProjectId) -> Self {
244        Cow::Borrowed(id.as_str())
245    }
246}
247
248/// A single line from a session JSONL file
249#[derive(Debug, Clone, Default, Serialize, Deserialize)]
250#[serde(rename_all = "camelCase")]
251pub struct SessionLine {
252    /// Session ID
253    #[serde(default)]
254    pub session_id: Option<String>,
255
256    /// Event type: "user", "assistant", "file-history-snapshot", "session_end", etc.
257    #[serde(rename = "type")]
258    pub line_type: String,
259
260    /// Timestamp of the event
261    #[serde(default)]
262    pub timestamp: Option<DateTime<Utc>>,
263
264    /// Current working directory
265    #[serde(default)]
266    pub cwd: Option<String>,
267
268    /// Git branch (if available)
269    #[serde(default)]
270    pub git_branch: Option<String>,
271
272    /// Message content (for user/assistant types)
273    #[serde(default)]
274    pub message: Option<SessionMessage>,
275
276    /// Model used (for assistant messages)
277    #[serde(default)]
278    pub model: Option<String>,
279
280    /// Token usage for this message
281    #[serde(default)]
282    pub usage: Option<TokenUsage>,
283
284    /// Summary data (for session_end type)
285    #[serde(default)]
286    pub summary: Option<SessionSummary>,
287
288    /// Parent session ID (for subagents)
289    #[serde(default)]
290    pub parent_session_id: Option<String>,
291}
292
293/// Message content in a session
294#[derive(Debug, Clone, Serialize, Deserialize)]
295#[serde(rename_all = "camelCase")]
296pub struct SessionMessage {
297    /// Role: "user" or "assistant"
298    #[serde(default)]
299    pub role: Option<String>,
300
301    /// Text content (can be String or Array of content blocks in newer Claude Code versions)
302    #[serde(default)]
303    pub content: Option<Value>,
304
305    /// Tool calls made
306    #[serde(default)]
307    pub tool_calls: Option<Vec<serde_json::Value>>,
308
309    /// Tool results
310    #[serde(default)]
311    pub tool_results: Option<Vec<serde_json::Value>>,
312
313    /// Token usage (for assistant messages)
314    #[serde(default)]
315    pub usage: Option<TokenUsage>,
316}
317
318/// Token usage for a message
319#[derive(Debug, Clone, Default, Serialize, Deserialize)]
320pub struct TokenUsage {
321    #[serde(default)]
322    pub input_tokens: u64,
323
324    #[serde(default)]
325    pub output_tokens: u64,
326
327    /// Cache read tokens (from cache_read_input_tokens in JSONL)
328    #[serde(default, alias = "cache_read_input_tokens")]
329    pub cache_read_tokens: u64,
330
331    /// Cache creation tokens (from cache_creation_input_tokens in JSONL)
332    #[serde(default, alias = "cache_creation_input_tokens")]
333    pub cache_write_tokens: u64,
334}
335
336impl TokenUsage {
337    /// Total tokens including cache reads and writes
338    ///
339    /// This is the sum of all token types:
340    /// - input_tokens: Regular input tokens (not cached)
341    /// - output_tokens: Generated tokens
342    /// - cache_read_tokens: Tokens read from cache (cache hits)
343    /// - cache_write_tokens: Tokens written to cache (cache creation)
344    pub fn total(&self) -> u64 {
345        self.input_tokens + self.output_tokens + self.cache_read_tokens + self.cache_write_tokens
346    }
347}
348
349/// Summary at session end
350#[derive(Debug, Clone, Default, Serialize, Deserialize)]
351#[serde(rename_all = "camelCase")]
352pub struct SessionSummary {
353    #[serde(default)]
354    pub total_tokens: u64,
355    #[serde(default)]
356    pub total_input_tokens: u64,
357    #[serde(default)]
358    pub total_output_tokens: u64,
359    #[serde(default)]
360    pub total_cache_read_tokens: u64,
361    #[serde(default)]
362    pub total_cache_write_tokens: u64,
363    #[serde(default)]
364    pub message_count: u64,
365    #[serde(default)]
366    pub duration_seconds: Option<u64>,
367    #[serde(default)]
368    pub models_used: Option<Vec<String>>,
369}
370
371/// Metadata extracted from a session without full parse
372///
373/// Created by streaming the JSONL until session_end event.
374#[derive(Debug, Clone, Serialize, Deserialize)]
375pub struct SessionMetadata {
376    /// Session ID (from filename or content)
377    pub id: SessionId,
378
379    /// Full path to the JSONL file
380    pub file_path: PathBuf,
381
382    /// Project path (extracted from directory structure)
383    pub project_path: ProjectId,
384
385    /// First timestamp in session
386    pub first_timestamp: Option<DateTime<Utc>>,
387
388    /// Last timestamp in session
389    pub last_timestamp: Option<DateTime<Utc>>,
390
391    /// Total message count (from summary or counted)
392    pub message_count: u64,
393
394    /// Total tokens (from summary)
395    pub total_tokens: u64,
396
397    /// Token breakdown for precise pricing
398    pub input_tokens: u64,
399    pub output_tokens: u64,
400    pub cache_creation_tokens: u64,
401    pub cache_read_tokens: u64,
402
403    /// Models used in this session
404    pub models_used: Vec<String>,
405
406    /// File size in bytes
407    pub file_size_bytes: u64,
408
409    /// Preview of first user message (truncated to 200 chars)
410    pub first_user_message: Option<String>,
411
412    /// Whether this session spawned subagents
413    pub has_subagents: bool,
414
415    /// Duration in seconds (from summary)
416    pub duration_seconds: Option<u64>,
417
418    /// Git branch name (normalized, extracted from first gitBranch in session)
419    pub branch: Option<String>,
420
421    /// Tool usage statistics: tool name -> call count
422    /// Extracted from tool_calls in assistant messages during session scan
423    pub tool_usage: std::collections::HashMap<String, usize>,
424}
425
426impl SessionMetadata {
427    /// Create a minimal metadata from just file path
428    pub fn from_path(path: PathBuf, project_path: ProjectId) -> Self {
429        let id = SessionId::new(
430            path.file_stem()
431                .and_then(|s| s.to_str())
432                .unwrap_or("unknown")
433                .to_string(),
434        );
435
436        let file_size_bytes = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
437
438        Self {
439            id,
440            file_path: path,
441            project_path,
442            first_timestamp: None,
443            last_timestamp: None,
444            message_count: 0,
445            total_tokens: 0,
446            input_tokens: 0,
447            output_tokens: 0,
448            cache_creation_tokens: 0,
449            cache_read_tokens: 0,
450            models_used: Vec::new(),
451            file_size_bytes,
452            first_user_message: None,
453            has_subagents: false,
454            duration_seconds: None,
455            branch: None,
456            tool_usage: std::collections::HashMap::new(),
457        }
458    }
459
460    /// Human-readable duration
461    pub fn duration_display(&self) -> String {
462        match self.duration_seconds {
463            Some(s) if s >= 3600 => format!("{}h {}m", s / 3600, (s % 3600) / 60),
464            Some(s) if s >= 60 => format!("{}m {}s", s / 60, s % 60),
465            Some(s) => format!("{}s", s),
466            None => "unknown".to_string(),
467        }
468    }
469
470    /// Human-readable file size
471    pub fn size_display(&self) -> String {
472        let bytes = self.file_size_bytes;
473        if bytes >= 1_000_000 {
474            format!("{:.1} MB", bytes as f64 / 1_000_000.0)
475        } else if bytes >= 1_000 {
476            format!("{:.1} KB", bytes as f64 / 1_000.0)
477        } else {
478            format!("{} B", bytes)
479        }
480    }
481}
482
483#[cfg(test)]
484mod tests {
485    use super::*;
486
487    #[test]
488    fn test_token_usage_total() {
489        let usage = TokenUsage {
490            input_tokens: 100,
491            output_tokens: 50,
492            ..Default::default()
493        };
494        assert_eq!(usage.total(), 150);
495    }
496
497    #[test]
498    fn test_session_metadata_duration_display() {
499        let mut meta =
500            SessionMetadata::from_path(PathBuf::from("/test.jsonl"), ProjectId::from("test"));
501
502        meta.duration_seconds = Some(90);
503        assert_eq!(meta.duration_display(), "1m 30s");
504
505        meta.duration_seconds = Some(3665);
506        assert_eq!(meta.duration_display(), "1h 1m");
507
508        meta.duration_seconds = Some(45);
509        assert_eq!(meta.duration_display(), "45s");
510    }
511
512    #[test]
513    fn test_session_metadata_size_display() {
514        let mut meta =
515            SessionMetadata::from_path(PathBuf::from("/test.jsonl"), ProjectId::from("test"));
516
517        meta.file_size_bytes = 500;
518        assert_eq!(meta.size_display(), "500 B");
519
520        meta.file_size_bytes = 5_000;
521        assert_eq!(meta.size_display(), "5.0 KB");
522
523        meta.file_size_bytes = 2_500_000;
524        assert_eq!(meta.size_display(), "2.5 MB");
525    }
526}
527
528#[cfg(test)]
529mod token_tests {
530    use super::*;
531
532    #[test]
533    fn test_real_claude_token_format_deserialization() {
534        // CRITICAL: Real format from Claude Code v2.1.29+
535        let json = r#"{
536            "input_tokens": 10,
537            "cache_creation_input_tokens": 64100,
538            "cache_read_input_tokens": 19275,
539            "cache_creation": {
540                "ephemeral_5m_input_tokens": 0,
541                "ephemeral_1h_input_tokens": 64100
542            },
543            "output_tokens": 1,
544            "service_tier": "standard"
545        }"#;
546
547        let result: Result<TokenUsage, _> = serde_json::from_str(json);
548
549        assert!(
550            result.is_ok(),
551            "Deserialization MUST succeed for real Claude format. Error: {:?}",
552            result.err()
553        );
554
555        let usage = result.unwrap();
556        assert_eq!(usage.input_tokens, 10);
557        assert_eq!(usage.output_tokens, 1);
558        assert_eq!(usage.cache_read_tokens, 19275);
559        assert_eq!(usage.cache_write_tokens, 64100);
560
561        let total = usage.total();
562        assert_eq!(total, 83386, "Total should be 10+1+19275+64100 = 83386");
563    }
564}