pub mod store;
pub mod tokens;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::path::PathBuf;
pub use store::{ChunkStore, Snapshot};
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ChunkId(pub [u8; 32]);
impl ChunkId {
pub fn hex(&self) -> String {
hex::encode(self.0)
}
pub fn short(&self) -> String {
hex::encode(&self.0[..4])
}
pub fn parse(s: &str) -> Option<Self> {
let v = hex::decode(s.trim()).ok()?;
if v.len() != 32 {
return None;
}
let mut a = [0u8; 32];
a.copy_from_slice(&v);
Some(Self(a))
}
}
impl fmt::Display for ChunkId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.short())
}
}
impl fmt::Debug for ChunkId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ChunkId({})", self.short())
}
}
impl Serialize for ChunkId {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&self.hex())
}
}
impl<'de> Deserialize<'de> for ChunkId {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
let s = String::deserialize(d)?;
ChunkId::parse(&s).ok_or_else(|| serde::de::Error::custom("bad chunk id"))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Access {
Read,
Write,
}
impl fmt::Display for Access {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Access::Read => "read",
Access::Write => "write",
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Visibility {
Hide,
Short,
Long,
Full,
}
impl Visibility {
pub fn parse(s: &str) -> Option<Self> {
match s.trim().to_ascii_lowercase().as_str() {
"hide" => Some(Self::Hide),
"short" => Some(Self::Short),
"long" => Some(Self::Long),
"full" => Some(Self::Full),
_ => None,
}
}
pub fn as_str(&self) -> &'static str {
match self {
Self::Hide => "hide",
Self::Short => "short",
Self::Long => "long",
Self::Full => "full",
}
}
}
impl fmt::Display for Visibility {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Sensitivity {
Open,
Standard,
Restricted,
Custom,
}
impl fmt::Display for Sensitivity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Open => "open",
Self::Standard => "standard",
Self::Restricted => "restricted",
Self::Custom => "custom",
})
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Kind {
UserTurn,
Assistant,
Reasoning,
ToolCall { tool: String, args: serde_json::Value, intent: String },
ToolOutput { call: ChunkId, tool: String, access: Access, ok: bool },
File { path: PathBuf, rev: String },
Diff { path: PathBuf },
Instruction { condition: String, pinned: bool },
Summary { of: ChunkId, level: Visibility },
SubagentResult { goal: String, model: String },
}
impl Kind {
pub fn tag(&self) -> &'static str {
match self {
Kind::UserTurn => "user",
Kind::Assistant => "assistant",
Kind::Reasoning => "reasoning",
Kind::ToolCall { .. } => "tool_call",
Kind::ToolOutput { .. } => "tool_output",
Kind::File { .. } => "file",
Kind::Diff { .. } => "diff",
Kind::Instruction { .. } => "instruction",
Kind::Summary { .. } => "summary",
Kind::SubagentResult { .. } => "subagent",
}
}
fn turn_sensitive(&self) -> bool {
!matches!(self, Kind::Instruction { .. } | Kind::File { .. } | Kind::Summary { .. })
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Chunk {
pub id: ChunkId,
pub kind: Kind,
pub body: String,
pub tokens: u32,
pub turn: u32,
pub session: String,
pub sensitivity: Sensitivity,
pub paths: Vec<PathBuf>,
pub created: i64,
}
impl Chunk {
pub fn new(kind: Kind, body: impl Into<String>, turn: u32, session: &str) -> Self {
let body = body.into();
let mut h = blake3::Hasher::new();
h.update(kind.tag().as_bytes());
h.update(b"\0");
if kind.turn_sensitive() {
h.update(&turn.to_le_bytes());
h.update(session.as_bytes());
}
if let Kind::Summary { of, level } = &kind {
h.update(&of.0);
h.update(level.as_str().as_bytes());
}
if let Kind::Instruction { condition, .. } = &kind {
h.update(condition.as_bytes());
}
h.update(b"\0");
h.update(body.as_bytes());
let tokens = tokens::count(&body);
Self {
id: ChunkId(*h.finalize().as_bytes()),
kind,
body,
tokens,
turn,
session: session.to_string(),
sensitivity: Sensitivity::Standard,
paths: Vec::new(),
created: chrono::Utc::now().timestamp(),
}
}
pub fn with_paths(mut self, paths: Vec<PathBuf>) -> Self {
self.paths = paths;
self
}
pub fn with_sensitivity(mut self, s: Sensitivity) -> Self {
self.sensitivity = s;
self
}
pub fn pinned(&self) -> bool {
matches!(self.kind, Kind::Instruction { pinned: true, .. })
}
pub fn preview(&self, n: usize) -> String {
let mut out = String::with_capacity(n.min(self.body.len()) + 1);
let mut last_ws = false;
for ch in self.body.chars() {
if out.len() >= n {
out.push('…');
break;
}
if ch.is_whitespace() {
if !last_ws {
out.push(' ');
}
last_ws = true;
} else {
out.push(ch);
last_ws = false;
}
}
out
}
pub fn label(&self) -> String {
let what = match &self.kind {
Kind::UserTurn => "user turn".to_string(),
Kind::Assistant => "assistant".to_string(),
Kind::Reasoning => "reasoning".to_string(),
Kind::ToolCall { tool, .. } => format!("call {tool}"),
Kind::ToolOutput { tool, access, .. } => format!("{tool} output · {access}"),
Kind::File { path, .. } => format!("file {}", path.display()),
Kind::Diff { path } => format!("diff {}", path.display()),
Kind::Instruction { condition, .. } => format!("instruction [{condition}]"),
Kind::Summary { of, level } => format!("{level} summary of {of}"),
Kind::SubagentResult { goal, .. } => format!("sub-agent: {}", truncate(goal, 40)),
};
format!("{what} · turn {}", self.turn)
}
}
pub fn truncate(s: &str, n: usize) -> String {
if s.chars().count() <= n {
s.to_string()
} else {
let mut t: String = s.chars().take(n.saturating_sub(1)).collect();
t.push('…');
t
}
}