use crate::error::Result;
use crate::types::{FrameId, MemoryCard};
#[derive(Debug, Clone)]
pub struct EnrichmentContext {
pub frame_id: FrameId,
pub uri: String,
pub text: String,
pub title: Option<String>,
pub timestamp: i64,
pub metadata: Option<String>,
}
impl EnrichmentContext {
#[must_use]
pub fn new(
frame_id: FrameId,
uri: String,
text: String,
title: Option<String>,
timestamp: i64,
metadata: Option<String>,
) -> Self {
Self {
frame_id,
uri,
text,
title,
timestamp,
metadata,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct EnrichmentResult {
pub cards: Vec<MemoryCard>,
pub success: bool,
pub error: Option<String>,
}
impl EnrichmentResult {
#[must_use]
pub fn success(cards: Vec<MemoryCard>) -> Self {
Self {
cards,
success: true,
error: None,
}
}
#[must_use]
pub fn empty() -> Self {
Self {
cards: Vec::new(),
success: true,
error: None,
}
}
#[must_use]
pub fn failed(error: impl Into<String>) -> Self {
Self {
cards: Vec::new(),
success: false,
error: Some(error.into()),
}
}
}
pub trait EnrichmentEngine: Send + Sync {
fn kind(&self) -> &str;
fn version(&self) -> &str;
fn enrich(&self, ctx: &EnrichmentContext) -> EnrichmentResult;
fn init(&mut self) -> Result<()> {
Ok(())
}
fn is_ready(&self) -> bool {
true
}
}
#[cfg(test)]
mod tests {
use super::*;
struct TestEngine;
impl EnrichmentEngine for TestEngine {
fn kind(&self) -> &'static str {
"test"
}
fn version(&self) -> &'static str {
"1.0.0"
}
fn enrich(&self, _ctx: &EnrichmentContext) -> EnrichmentResult {
EnrichmentResult::empty()
}
}
#[test]
fn test_enrichment_context() {
let ctx = EnrichmentContext::new(
42,
"mv2://test/msg-1".to_string(),
"Hello, I work at Anthropic.".to_string(),
Some("Test".to_string()),
1700000000,
None,
);
assert_eq!(ctx.frame_id, 42);
assert_eq!(ctx.uri, "mv2://test/msg-1");
}
#[test]
fn test_enrichment_result() {
let success = EnrichmentResult::success(vec![]);
assert!(success.success);
assert!(success.error.is_none());
let empty = EnrichmentResult::empty();
assert!(empty.success);
assert!(empty.cards.is_empty());
let failed = EnrichmentResult::failed("test error");
assert!(!failed.success);
assert_eq!(failed.error, Some("test error".to_string()));
}
#[test]
fn test_engine_trait() {
let engine = TestEngine;
assert_eq!(engine.kind(), "test");
assert_eq!(engine.version(), "1.0.0");
assert!(engine.is_ready());
}
}