Skip to main content

sqz_engine/
types.rs

1use std::path::PathBuf;
2use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4
5// Type aliases
6pub type SessionId = String;
7pub type AgentId = String;
8pub type CorrectionId = String;
9pub type ToolId = String;
10
11// --- Enums ---
12
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14pub enum ImageFormat {
15    Png,
16    Jpeg,
17    Gif,
18    WebP,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
22#[serde(rename_all = "lowercase")]
23pub enum Role {
24    User,
25    Assistant,
26    System,
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30pub enum ContentType {
31    Json,
32    CliOutput { command: String },
33    Code { language: String, path: PathBuf },
34    PlainText,
35    Image { format: ImageFormat },
36}
37
38#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
39pub enum ModelFamily {
40    AnthropicClaude,
41    OpenAiGpt,
42    GoogleGemini,
43    Local(String),
44}
45
46#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
47pub enum Provider {
48    /// 90% cache discount
49    Anthropic,
50    /// 50% cache discount
51    OpenAI,
52    /// No cache boundary
53    Google,
54}
55
56#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
57pub enum TaskClassification {
58    Simple,
59    Complex,
60}
61
62// --- Content types ---
63
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct ContentMetadata {
66    pub source: Option<String>,
67    pub path: Option<PathBuf>,
68    pub language: Option<String>,
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct Content {
73    pub raw: String,
74    pub content_type: ContentType,
75    pub metadata: ContentMetadata,
76    pub tokens_original: u32,
77}
78
79/// Source provenance for a compressed segment — enables reversibility and trust.
80#[derive(Debug, Clone, Serialize, Deserialize, Default)]
81pub struct Provenance {
82    /// File path the content originated from, if known.
83    pub file: Option<PathBuf>,
84    /// Line range within the file (start inclusive, end exclusive).
85    pub line_range: Option<std::ops::Range<usize>>,
86    /// SHA-256 hex digest of the original content.
87    pub content_hash: Option<String>,
88    /// Tool call ID that produced this content, if applicable.
89    pub tool_call_id: Option<String>,
90    /// Human-readable source label (e.g. "git diff", "cargo test output").
91    pub label: Option<String>,
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct CompressedContent {
96    pub data: String,
97    pub tokens_compressed: u32,
98    pub tokens_original: u32,
99    pub stages_applied: Vec<String>,
100    pub compression_ratio: f64,
101    /// Source provenance — where this content came from.
102    #[serde(default)]
103    pub provenance: Provenance,
104    /// Verifier result — None if verify pass was not run.
105    #[serde(default)]
106    pub verify: Option<VerifyResult>,
107}
108
109// --- Session types ---
110
111#[derive(Debug, Clone, Serialize, Deserialize)]
112pub struct Turn {
113    pub role: Role,
114    pub content: String,
115    pub tokens: u32,
116    pub pinned: bool,
117    pub timestamp: DateTime<Utc>,
118}
119
120pub type ConversationTurn = Turn;
121
122#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct PinnedSegment {
124    pub turn_index: usize,
125    pub reason: String,
126    pub tokens: u32,
127}
128
129pub type PinEntry = PinnedSegment;
130
131#[derive(Debug, Clone, Serialize, Deserialize)]
132pub struct KvFact {
133    pub key: String,
134    pub value: String,
135    pub source_turn: usize,
136}
137
138pub type Learning = KvFact;
139
140#[derive(Debug, Clone, Serialize, Deserialize)]
141pub struct WindowUsage {
142    pub window_size: u32,
143    pub consumed: u32,
144    pub pinned: u32,
145    pub model_family: ModelFamily,
146}
147
148pub type BudgetState = WindowUsage;
149
150#[derive(Debug, Clone, Serialize, Deserialize)]
151pub struct ToolCall {
152    pub tool_name: String,
153    pub tokens_input: u32,
154    pub tokens_output: u32,
155    pub cost_usd: f64,
156    pub timestamp: DateTime<Utc>,
157}
158
159pub type ToolUsageRecord = ToolCall;
160
161#[derive(Debug, Clone, Serialize, Deserialize)]
162pub struct EditRecord {
163    pub id: CorrectionId,
164    pub timestamp: DateTime<Utc>,
165    pub original: String,
166    pub correction: String,
167    pub context: String,
168}
169
170pub type CorrectionEntry = EditRecord;
171
172#[derive(Debug, Clone, Serialize, Deserialize, Default)]
173pub struct EditHistory {
174    pub entries: Vec<EditRecord>,
175}
176
177pub type CorrectionLog = EditHistory;
178
179/// Active compression session — tracks conversation, budget, and tool usage.
180#[derive(Debug, Clone, Serialize, Deserialize)]
181pub struct Session {
182    pub id: SessionId,
183    pub project_dir: PathBuf,
184    pub conversation: Vec<Turn>,
185    pub corrections: EditHistory,
186    pub pins: Vec<PinnedSegment>,
187    pub learnings: Vec<KvFact>,
188    pub compressed_summary: String,
189    pub budget: WindowUsage,
190    pub tool_usage: Vec<ToolCall>,
191    pub created_at: DateTime<Utc>,
192    pub updated_at: DateTime<Utc>,
193}
194
195pub type SessionState = Session;
196
197/// Configuration for a single compression stage
198#[derive(Debug, Clone, Serialize, Deserialize, Default)]
199pub struct StageConfig {
200    pub enabled: bool,
201    #[serde(default)]
202    pub options: serde_json::Value,
203}
204
205/// Result of the two-pass compression verifier.
206#[derive(Debug, Clone, Serialize, Deserialize)]
207pub struct VerifyResult {
208    /// Overall pass/fail.
209    pub passed: bool,
210    /// Confidence score 0.0–1.0 (1.0 = fully verified).
211    pub confidence: f64,
212    /// Which checks passed.
213    pub checks_passed: Vec<String>,
214    /// Which checks failed with reason.
215    pub checks_failed: Vec<(String, String)>,
216    /// Whether the pipeline fell back to a safer preset.
217    pub fallback_triggered: bool,
218}