Skip to main content

sessionwiki/
model.rs

1use chrono::{DateTime, Utc};
2use serde::Serialize;
3use std::path::PathBuf;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
6#[serde(rename_all = "lowercase")]
7pub enum Role {
8    User,
9    Assistant,
10    Tool,
11}
12
13impl Role {
14    pub fn label(&self) -> &'static str {
15        match self {
16            Role::User => "user",
17            Role::Assistant => "assistant",
18            Role::Tool => "tool",
19        }
20    }
21}
22
23#[derive(Debug, Serialize)]
24pub struct Message {
25    pub role: Role,
26    pub text: String,
27    pub ts: Option<DateTime<Utc>>,
28}
29
30/// The write tool behind one file edit, normalized across the variants a tool
31/// exposes - the evidence of *what kind* of change a session made to a file.
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
33#[serde(rename_all = "lowercase")]
34pub enum EditKind {
35    Edit,
36    Write,
37    MultiEdit,
38    NotebookEdit,
39}
40
41impl EditKind {
42    /// Stable lowercase token stored in the index (matches the Serialize form).
43    pub fn as_str(&self) -> &'static str {
44        match self {
45            EditKind::Edit => "edit",
46            EditKind::Write => "write",
47            EditKind::MultiEdit => "multiedit",
48            EditKind::NotebookEdit => "notebookedit",
49        }
50    }
51}
52
53/// One concrete edit a session made to one file - the evidence chain's atom.
54/// `snippet` is a bounded excerpt of the change (the resulting code, or the
55/// created content) so a later reader can see WHAT changed without re-opening
56/// the original session.
57#[derive(Debug, Serialize)]
58pub struct EditEvent {
59    pub path: String,
60    pub kind: EditKind,
61    pub snippet: String,
62    pub ts: Option<DateTime<Utc>>,
63}
64
65#[derive(Debug, Serialize)]
66pub struct Session {
67    /// Short stable id derived from the file path (FNV-1a hash, hex).
68    pub id: String,
69    pub tool: &'static str,
70    pub path: PathBuf,
71    pub project: String,
72    pub started: Option<DateTime<Utc>>,
73    pub ended: Option<DateTime<Utc>>,
74    pub title: String,
75    /// True for subagent transcripts spawned inside a parent session.
76    pub subagent: bool,
77    pub messages: Vec<Message>,
78    /// Files the session edited or created, extracted from its tool calls
79    /// (Claude's Edit/Write, Codex's apply_patch). This is the link between a
80    /// session and the code it produced - the basis for `files` and `blame`.
81    pub touched: Vec<String>,
82    /// The concrete edits behind `touched`, per file - the evidence layer: what
83    /// kind of change and a bounded snippet of it. Empty for adapters that do
84    /// not yet extract structured edits (they still populate `touched`).
85    pub edits: Vec<EditEvent>,
86}
87
88/// One discovered session store on disk (for `scan`).
89pub struct StoreReport {
90    pub tool: &'static str,
91    pub root: PathBuf,
92    pub files: usize,
93    pub bytes: u64,
94    pub oldest: Option<DateTime<Utc>>,
95    pub newest: Option<DateTime<Utc>>,
96}