use crate::{GraphrtError, Runtime};
#[derive(Clone, Copy, Debug, PartialEq, Eq, uniffi::Enum)]
pub enum LessonKind {
Preference,
Fact,
Procedure,
Correction,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, uniffi::Enum)]
pub enum LessonStatus {
Proposed,
Approved,
Rejected,
Retired,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, uniffi::Enum)]
pub enum LessonDecision {
Approve,
Reject,
Retire,
}
#[derive(Clone, Debug, PartialEq, Eq, uniffi::Record)]
pub struct LessonInfo {
pub id: String,
pub agent: String,
pub user: Option<String>,
pub kind: LessonKind,
pub text: String,
pub status: LessonStatus,
pub high_risk: bool,
pub origin: String,
pub created_at_ms: u64,
}
#[derive(Clone, Debug, PartialEq, Eq, uniffi::Record)]
pub struct LessonProposal {
pub agent: String,
#[uniffi(default)]
pub user: Option<String>,
pub kind: LessonKind,
pub text: String,
#[uniffi(default)]
pub run_id: Option<String>,
#[uniffi(default)]
pub author: Option<String>,
#[uniffi(default)]
pub author_authenticated: bool,
#[uniffi(default)]
pub evidence: Option<String>,
}
#[cfg(feature = "learning")]
mod imp {
use std::sync::Arc;
use graphrt_learn::{Decision, LessonFilter, LessonStore, NewLesson, Proposal, Risk, Signal, UserFilter};
use super::{LessonDecision, LessonInfo, LessonKind, LessonProposal, LessonStatus};
use crate::GraphrtError;
pub(crate) type Store = Arc<LessonStore>;
const ACTOR: &str = "app";
fn failed(e: graphrt_learn::Error) -> GraphrtError {
GraphrtError::input(e.to_string())
}
pub(crate) fn install(dir: &str, services: &mut graphrt_core::Services) -> Store {
let store = Arc::new(LessonStore::open(dir));
services.insert(Arc::clone(&store).into_service());
store
}
fn kind_of(kind: LessonKind) -> graphrt_learn::LessonKind {
match kind {
LessonKind::Preference => graphrt_learn::LessonKind::Preference,
LessonKind::Fact => graphrt_learn::LessonKind::Fact,
LessonKind::Procedure => graphrt_learn::LessonKind::Procedure,
LessonKind::Correction => graphrt_learn::LessonKind::Correction,
}
}
fn status_of(status: LessonStatus) -> graphrt_learn::LessonStatus {
match status {
LessonStatus::Proposed => graphrt_learn::LessonStatus::Proposed,
LessonStatus::Approved => graphrt_learn::LessonStatus::Approved,
LessonStatus::Rejected => graphrt_learn::LessonStatus::Rejected,
LessonStatus::Retired => graphrt_learn::LessonStatus::Retired,
}
}
fn info(lesson: graphrt_learn::Lesson) -> LessonInfo {
let origin = lesson.origin_sentence();
LessonInfo {
kind: match lesson.kind {
graphrt_learn::LessonKind::Preference => LessonKind::Preference,
graphrt_learn::LessonKind::Fact => LessonKind::Fact,
graphrt_learn::LessonKind::Procedure => LessonKind::Procedure,
graphrt_learn::LessonKind::Correction => LessonKind::Correction,
},
status: match lesson.status {
graphrt_learn::LessonStatus::Proposed => LessonStatus::Proposed,
graphrt_learn::LessonStatus::Approved => LessonStatus::Approved,
graphrt_learn::LessonStatus::Rejected => LessonStatus::Rejected,
graphrt_learn::LessonStatus::Retired => LessonStatus::Retired,
},
high_risk: lesson.risk == Risk::High,
origin,
id: lesson.id,
agent: lesson.agent,
user: lesson.user,
text: lesson.text,
created_at_ms: lesson.created_at_ms,
}
}
pub(crate) fn list(
store: &LessonStore,
agent: &str,
status: Option<LessonStatus>,
) -> Result<Vec<LessonInfo>, GraphrtError> {
let filter = LessonFilter { user: UserFilter::Any, status: status.map(status_of) };
Ok(store.list(agent, &filter).map_err(failed)?.into_iter().map(info).collect())
}
pub(crate) fn add(
store: &LessonStore,
agent: String,
user: Option<String>,
kind: LessonKind,
text: String,
) -> Result<LessonInfo, GraphrtError> {
let new = NewLesson { agent, user, kind: kind_of(kind), text };
store.add(new, ACTOR).map(info).map_err(failed)
}
pub(crate) fn propose(store: &LessonStore, proposal: LessonProposal) -> Result<LessonInfo, GraphrtError> {
let proposal = Proposal {
agent: proposal.agent,
subject_user: proposal.user,
kind: kind_of(proposal.kind),
text: proposal.text,
signal: Signal::Feedback,
run_id: proposal.run_id,
author: proposal.author,
author_authenticated: proposal.author_authenticated,
evidence: proposal.evidence,
};
store.propose(proposal).map(info).map_err(failed)
}
pub(crate) fn decide(
store: &LessonStore,
agent: &str,
id: &str,
decision: LessonDecision,
) -> Result<LessonInfo, GraphrtError> {
let decision = match decision {
LessonDecision::Approve => Decision::Approve,
LessonDecision::Reject => Decision::Reject,
LessonDecision::Retire => Decision::Retire,
};
store.decide(agent, id, decision, ACTOR).map(info).map_err(failed)
}
pub(crate) fn delete(store: &LessonStore, agent: &str, id: &str) -> Result<(), GraphrtError> {
store.delete(agent, id, ACTOR).map_err(failed)
}
}
#[cfg(feature = "learning")]
pub(crate) use imp::{Store, install};
#[cfg(not(feature = "learning"))]
pub(crate) type Store = ();
impl Runtime {
#[cfg(feature = "learning")]
fn lesson_store(&self) -> Result<&Store, GraphrtError> {
self.lessons.as_ref().ok_or_else(|| GraphrtError::not_configured("learning", "learning_dir"))
}
}
#[cfg(not(feature = "learning"))]
macro_rules! without_learning {
($($unused:expr),*) => {{
let _ = ($($unused,)*);
Err(GraphrtError::unsupported("learning", "learning"))
}};
}
#[uniffi::export]
impl Runtime {
pub fn list_lessons(&self, agent: String, status: Option<LessonStatus>) -> Result<Vec<LessonInfo>, GraphrtError> {
#[cfg(feature = "learning")]
{
imp::list(self.lesson_store()?, &agent, status)
}
#[cfg(not(feature = "learning"))]
without_learning!(agent, status)
}
pub fn add_lesson(
&self,
agent: String,
user: Option<String>,
kind: LessonKind,
text: String,
) -> Result<LessonInfo, GraphrtError> {
#[cfg(feature = "learning")]
{
imp::add(self.lesson_store()?, agent, user, kind, text)
}
#[cfg(not(feature = "learning"))]
without_learning!(agent, user, kind, text)
}
pub fn propose_lesson(&self, proposal: LessonProposal) -> Result<LessonInfo, GraphrtError> {
#[cfg(feature = "learning")]
{
imp::propose(self.lesson_store()?, proposal)
}
#[cfg(not(feature = "learning"))]
without_learning!(proposal)
}
pub fn decide_lesson(
&self,
agent: String,
id: String,
decision: LessonDecision,
) -> Result<LessonInfo, GraphrtError> {
#[cfg(feature = "learning")]
{
imp::decide(self.lesson_store()?, &agent, &id, decision)
}
#[cfg(not(feature = "learning"))]
without_learning!(agent, id, decision)
}
pub fn delete_lesson(&self, agent: String, id: String) -> Result<(), GraphrtError> {
#[cfg(feature = "learning")]
{
imp::delete(self.lesson_store()?, &agent, &id)
}
#[cfg(not(feature = "learning"))]
without_learning!(agent, id)
}
}