use serde::{Deserialize, Serialize};
use crate::events::LearningEvent;
use crate::util::epoch_millis;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DependencyGraphRecord {
pub prompt: String,
pub response: String,
pub available_actions: Vec<String>,
pub discover_order: Vec<String>,
pub not_discover_order: Vec<String>,
pub timestamp_ms: u64,
pub model: String,
pub endpoint: String,
pub lora: Option<String>,
pub latency_ms: u64,
pub error: Option<String>,
}
impl DependencyGraphRecord {
pub fn new(model: impl Into<String>) -> Self {
Self {
prompt: String::new(),
response: String::new(),
available_actions: Vec::new(),
discover_order: Vec::new(),
not_discover_order: Vec::new(),
timestamp_ms: epoch_millis(),
model: model.into(),
endpoint: String::new(),
lora: None,
latency_ms: 0,
error: None,
}
}
pub fn prompt(mut self, prompt: impl Into<String>) -> Self {
self.prompt = prompt.into();
self
}
pub fn response(mut self, response: impl Into<String>) -> Self {
self.response = response.into();
self
}
pub fn available_actions(mut self, actions: Vec<String>) -> Self {
self.available_actions = actions;
self
}
pub fn discover_order(mut self, order: Vec<String>) -> Self {
self.discover_order = order;
self
}
pub fn not_discover_order(mut self, order: Vec<String>) -> Self {
self.not_discover_order = order;
self
}
pub fn endpoint(mut self, endpoint: impl Into<String>) -> Self {
self.endpoint = endpoint.into();
self
}
pub fn lora(mut self, lora: impl Into<String>) -> Self {
self.lora = Some(lora.into());
self
}
pub fn latency_ms(mut self, latency: u64) -> Self {
self.latency_ms = latency;
self
}
pub fn error(mut self, error: impl Into<String>) -> Self {
self.error = Some(error.into());
self
}
pub fn is_success(&self) -> bool {
self.error.is_none() && !self.discover_order.is_empty()
}
}
impl From<&LearningEvent> for DependencyGraphRecord {
fn from(event: &LearningEvent) -> Self {
match event {
LearningEvent::DependencyGraphInference {
timestamp_ms,
prompt,
response,
available_actions,
discover_order,
not_discover_order,
model,
endpoint,
lora,
latency_ms,
error,
..
} => Self {
prompt: prompt.clone(),
response: response.clone(),
available_actions: available_actions.clone(),
discover_order: discover_order.clone(),
not_discover_order: not_discover_order.clone(),
timestamp_ms: *timestamp_ms,
model: model.clone(),
endpoint: endpoint.clone(),
lora: lora.clone(),
latency_ms: *latency_ms,
error: error.clone(),
},
_ => Self::new("unknown"),
}
}
}