1use crate::error::CoreError;
2use crate::event::Usage;
3use crate::ids::RunId;
4use crate::message::Message;
5use async_trait::async_trait;
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8use std::collections::BTreeMap;
9
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
14pub struct Checkpoint {
15 pub run_id: RunId,
16 pub step: u64,
17 pub state: Value,
18}
19
20#[async_trait]
24pub trait CheckpointStore: Send + Sync {
25 async fn save(&self, cp: Checkpoint) -> Result<(), CoreError>;
26 async fn latest(&self, run_id: RunId) -> Result<Option<Checkpoint>, CoreError>;
27
28 async fn history(&self, run_id: RunId) -> Result<Vec<Checkpoint>, CoreError> {
32 Ok(self.latest(run_id).await?.into_iter().collect())
33 }
34
35 async fn prune(&self, run_id: RunId, keep_last: u64) -> Result<u64, CoreError> {
40 let _ = run_id;
41 if keep_last == 0 {
42 return Err(CoreError::Store("prune keep_last must be at least 1".into()));
43 }
44 Ok(0)
45 }
46}
47
48#[async_trait]
52pub trait MemoryLog: Send + Sync {
53 async fn append(&self, log: &str, expected_len: u64, entries: Vec<Message>) -> Result<u64, CoreError>;
54 async fn read(&self, log: &str) -> Result<Vec<Message>, CoreError>;
55}
56
57#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
61pub struct VectorRecord {
62 pub id: String,
63 pub vector: Vec<f32>,
64 pub text: String,
65 pub meta: BTreeMap<String, String>,
66}
67
68#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
72pub struct Scored {
73 pub id: String,
74 pub score: f32,
75 pub text: String,
76 pub meta: BTreeMap<String, String>,
77}
78
79#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
83pub struct Filter(pub BTreeMap<String, String>);
84
85impl Filter {
86 pub fn matches(&self, meta: &BTreeMap<String, String>) -> bool {
90 self.0.iter().all(|(k, v)| meta.get(k) == Some(v))
91 }
92}
93
94#[async_trait]
98pub trait VectorStore: Send + Sync {
99 fn dims(&self) -> usize;
100 async fn upsert(&self, records: Vec<VectorRecord>) -> Result<(), CoreError>;
101 async fn search(&self, query: &[f32], k: usize, filter: &Filter) -> Result<Vec<Scored>, CoreError>;
102}
103
104#[async_trait]
109pub trait KeywordStore: Send + Sync {
110 async fn search_text(&self, query: &str, k: usize, filter: &Filter) -> Result<Vec<Scored>, CoreError>;
111}
112
113#[async_trait]
117pub trait BlobStore: Send + Sync {
118 async fn put(&self, path: &str, bytes: Vec<u8>) -> Result<(), CoreError>;
119 async fn get(&self, path: &str) -> Result<Vec<u8>, CoreError>;
120}
121
122pub fn cosine(a: &[f32], b: &[f32]) -> f32 {
126 assert_eq!(a.len(), b.len(), "cosine of unequal lengths");
127 let dot: f32 = a.iter().zip(b).map(|(x, y)| x * y).sum();
128 let na = a.iter().map(|x| x * x).sum::<f32>().sqrt();
129 let nb = b.iter().map(|x| x * x).sum::<f32>().sqrt();
130 if na == 0.0 || nb == 0.0 {
131 return 0.0;
132 }
133 dot / (na * nb)
134}
135
136#[derive(Debug, Clone, PartialEq, Eq)]
140pub struct Lease {
141 pub run_id: RunId,
142 pub token: u64,
143 pub attempts: u32,
144}
145
146#[derive(Debug, Clone, PartialEq, Eq)]
150pub enum JobState {
151 Pending {
152 due_ms: i64,
153 },
154 Leased {
155 token: u64,
156 until_ms: i64,
157 next_due_ms: Option<i64>,
158 },
159 Done,
160 Failed {
161 error: String,
162 },
163}
164
165#[async_trait]
169pub trait JobQueue: Send + Sync {
170 async fn enqueue(&self, run_id: RunId, due_ms: i64) -> Result<(), CoreError>;
171 async fn claim_due(&self, limit: usize, lease_ms: i64) -> Result<Vec<Lease>, CoreError>;
172 async fn complete(&self, lease: &Lease) -> Result<(), CoreError>;
173 async fn fail(&self, lease: &Lease, error: &str) -> Result<(), CoreError>;
174 async fn reschedule(&self, lease: &Lease, due_ms: i64) -> Result<(), CoreError>;
175}
176
177#[async_trait]
181pub trait UsageLedger: Send + Sync {
182 async fn period_total(&self, key: &str) -> Result<u64, CoreError>;
183 async fn record(&self, key: &str, usage: Usage) -> Result<(), CoreError>;
184}