Expand description
§CAI Core - Shared types, traits, and utilities
This crate provides the foundational data structures and shared utilities used throughout the CAI (Coding Agent Insights) workspace.
§Overview
cai-core defines the core domain model for representing AI coding interactions.
All other crates in the workspace depend on these types to ensure consistency
across the system.
§Core Types
Entry- Represents a single AI coding interaction with all associated dataSource- Identifies the origin system (Claude, Codex, Git, etc.)Metadata- Extensible metadata for additional contextError- Unified error type for CAI operationsResult<T>- Type alias forResult<T, Error>
§Usage
Creating a basic entry:
use cai_core::{Entry, Source, Metadata};
use chrono::Utc;
let entry = Entry {
id: "test-entry-123".to_string(),
source: Source::Claude,
timestamp: Utc::now(),
prompt: "Help me refactor this function".to_string(),
response: "Here's the improved version...".to_string(),
metadata: Metadata::default(),
};Working with metadata:
use cai_core::Metadata;
use std::collections::HashMap;
let mut metadata = Metadata {
file_path: Some("src/main.rs".to_string()),
repo_url: Some("https://github.com/user/repo".to_string()),
commit_hash: Some("abc123def".to_string()),
language: Some("Rust".to_string()),
extra: HashMap::new(),
};
// Add custom fields
metadata.extra.insert("complexity".to_string(), "high".to_string());
metadata.extra.insert("reviewed".to_string(), "true".to_string());Error handling:
use cai_core::{Error, Result, Entry};
fn process_entry(entry: &Entry) -> Result<()> {
if entry.prompt.is_empty() {
return Err(Error::Message("Prompt cannot be empty".to_string()));
}
// Process the entry...
Ok(())
}§Design Principles
- Minimal dependencies: Only essential dependencies (serde, chrono)
- Serialization: All public types implement
SerializeandDeserialize - Extensibility:
Metadata.extraallows custom fields without breaking changes - Type safety: Enums use
#[non_exhaustive]for future-proofing
§Testing
# Run all tests
cargo test -p cai-core
# Run with coverage
cargo llvm-cov -p cai-coreStructs§
- Entry
- Core entry representing a single AI coding interaction
- Metadata
- Metadata associated with an entry
Enums§
Type Aliases§
- Result
- Result type for CAI operations