use uuid::Uuid;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Query {
Remember {
agent_id: Uuid,
content: String,
memory_type: String,
metadata: Value,
tags: Vec<String>,
},
Search {
agent_id: Uuid,
text: String,
semantic: bool,
top_k: usize,
filter: Option<Value>,
alpha: f32,
},
Recall {
agent_id: Uuid,
memory_ids: Vec<Uuid>,
},
Branch {
agent_id: Uuid,
parent: String,
name: String,
description: Option<String>,
},
Merge {
agent_id: Uuid,
source: String,
target: String,
strategy: String,
},
Diff {
agent_id: Uuid,
branch_a: String,
branch_b: String,
},
Sync {
agent_id: Uuid,
},
Reflect {
agent_id: Uuid,
job_type: String,
dry_run: bool,
},
Raw {
sql: String,
params: Vec<Value>,
entity_type: String,
},
}
impl Query {
pub fn target_component(&self) -> &'static str {
match self {
Self::Remember { .. } | Self::Recall { .. } => "core",
Self::Search { semantic: true, .. } => "vector",
Self::Search { semantic: false, .. } => "core",
Self::Branch { .. } | Self::Merge { .. } | Self::Diff { .. } => "branch",
Self::Sync { .. } => "sync",
Self::Reflect { .. } => "reflect",
Self::Raw { .. } => "core",
}
}
pub fn is_write(&self) -> bool {
matches!(
self,
Self::Remember { .. }
| Self::Branch { .. }
| Self::Merge { .. }
| Self::Sync { .. }
| Self::Reflect { .. }
| Self::Raw { .. }
)
}
pub fn agent_id(&self) -> Uuid {
match self {
Self::Remember { agent_id, .. }
| Self::Search { agent_id, .. }
| Self::Recall { agent_id, .. }
| Self::Branch { agent_id, .. }
| Self::Merge { agent_id, .. }
| Self::Diff { agent_id, .. }
| Self::Sync { agent_id, .. }
| Self::Reflect { agent_id, .. } => *agent_id,
Self::Raw { .. } => Uuid::nil(),
}
}
}
#[derive(Debug, Clone)]
pub struct QueryPlan {
pub id: Uuid,
pub query: Query,
pub steps: Vec<QueryStep>,
pub estimated_ms: Option<u64>,
pub guard_applied: bool,
}
#[derive(Debug, Clone)]
pub enum QueryStep {
CoreRead { sql: String },
CoreWrite { sql: String },
VectorSearch { collection: String, top_k: usize },
VectorUpsert { collection: String },
SyncPush,
SyncPull,
BranchOp { branch_name: String },
ReflectJob { job_type: String },
GuardCheck { action: String },
Parallel(Vec<QueryStep>),
Sequential(Vec<QueryStep>),
}
#[derive(Debug, Clone)]
pub struct QueryResult {
pub query_id: Uuid,
pub data: QueryResultData,
pub latency_ms: u64,
pub guard_applied: bool,
pub from_cache: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum QueryResultData {
MemoryEntries(Vec<Value>),
SearchResults(Vec<Value>),
BranchInfo(Value),
SyncResult(Value),
ReflectResult(Value),
Unit,
Error(String),
}