1use crate::{GraphrtError, Runtime};
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq, uniffi::Enum)]
14pub enum LessonKind {
15 Preference,
17 Fact,
19 Procedure,
21 Correction,
23}
24
25#[derive(Clone, Copy, Debug, PartialEq, Eq, uniffi::Enum)]
27pub enum LessonStatus {
28 Proposed,
29 Approved,
30 Rejected,
31 Retired,
32}
33
34#[derive(Clone, Copy, Debug, PartialEq, Eq, uniffi::Enum)]
36pub enum LessonDecision {
37 Approve,
39 Reject,
41 Retire,
43}
44
45#[derive(Clone, Debug, PartialEq, Eq, uniffi::Record)]
47pub struct LessonInfo {
48 pub id: String,
49 pub agent: String,
50 pub user: Option<String>,
52 pub kind: LessonKind,
53 pub text: String,
54 pub status: LessonStatus,
55 pub high_risk: bool,
58 pub origin: String,
60 pub created_at_ms: u64,
61}
62
63#[derive(Clone, Debug, PartialEq, Eq, uniffi::Record)]
65pub struct LessonProposal {
66 pub agent: String,
68 #[uniffi(default)]
70 pub user: Option<String>,
71 pub kind: LessonKind,
72 pub text: String,
73 #[uniffi(default)]
75 pub run_id: Option<String>,
76 #[uniffi(default)]
78 pub author: Option<String>,
79 #[uniffi(default)]
80 pub author_authenticated: bool,
81 #[uniffi(default)]
83 pub evidence: Option<String>,
84}
85
86#[cfg(feature = "learning")]
87mod imp {
88 use std::sync::Arc;
89
90 use graphrt_learn::{Decision, LessonFilter, LessonStore, NewLesson, Proposal, Risk, Signal, UserFilter};
91
92 use super::{LessonDecision, LessonInfo, LessonKind, LessonProposal, LessonStatus};
93 use crate::GraphrtError;
94
95 pub(crate) type Store = Arc<LessonStore>;
96
97 const ACTOR: &str = "app";
99
100 fn failed(e: graphrt_learn::Error) -> GraphrtError {
101 GraphrtError::input(e.to_string())
102 }
103
104 pub(crate) fn install(dir: &str, services: &mut graphrt_core::Services) -> Store {
105 let store = Arc::new(LessonStore::open(dir));
106 services.insert(Arc::clone(&store).into_service());
107 store
108 }
109
110 fn kind_of(kind: LessonKind) -> graphrt_learn::LessonKind {
111 match kind {
112 LessonKind::Preference => graphrt_learn::LessonKind::Preference,
113 LessonKind::Fact => graphrt_learn::LessonKind::Fact,
114 LessonKind::Procedure => graphrt_learn::LessonKind::Procedure,
115 LessonKind::Correction => graphrt_learn::LessonKind::Correction,
116 }
117 }
118
119 fn status_of(status: LessonStatus) -> graphrt_learn::LessonStatus {
120 match status {
121 LessonStatus::Proposed => graphrt_learn::LessonStatus::Proposed,
122 LessonStatus::Approved => graphrt_learn::LessonStatus::Approved,
123 LessonStatus::Rejected => graphrt_learn::LessonStatus::Rejected,
124 LessonStatus::Retired => graphrt_learn::LessonStatus::Retired,
125 }
126 }
127
128 fn info(lesson: graphrt_learn::Lesson) -> LessonInfo {
129 let origin = lesson.origin_sentence();
130 LessonInfo {
131 kind: match lesson.kind {
132 graphrt_learn::LessonKind::Preference => LessonKind::Preference,
133 graphrt_learn::LessonKind::Fact => LessonKind::Fact,
134 graphrt_learn::LessonKind::Procedure => LessonKind::Procedure,
135 graphrt_learn::LessonKind::Correction => LessonKind::Correction,
136 },
137 status: match lesson.status {
138 graphrt_learn::LessonStatus::Proposed => LessonStatus::Proposed,
139 graphrt_learn::LessonStatus::Approved => LessonStatus::Approved,
140 graphrt_learn::LessonStatus::Rejected => LessonStatus::Rejected,
141 graphrt_learn::LessonStatus::Retired => LessonStatus::Retired,
142 },
143 high_risk: lesson.risk == Risk::High,
144 origin,
145 id: lesson.id,
146 agent: lesson.agent,
147 user: lesson.user,
148 text: lesson.text,
149 created_at_ms: lesson.created_at_ms,
150 }
151 }
152
153 pub(crate) fn list(
154 store: &LessonStore,
155 agent: &str,
156 status: Option<LessonStatus>,
157 ) -> Result<Vec<LessonInfo>, GraphrtError> {
158 let filter = LessonFilter { user: UserFilter::Any, status: status.map(status_of) };
159 Ok(store.list(agent, &filter).map_err(failed)?.into_iter().map(info).collect())
160 }
161
162 pub(crate) fn add(
163 store: &LessonStore,
164 agent: String,
165 user: Option<String>,
166 kind: LessonKind,
167 text: String,
168 ) -> Result<LessonInfo, GraphrtError> {
169 let new = NewLesson { agent, user, kind: kind_of(kind), text };
170 store.add(new, ACTOR).map(info).map_err(failed)
171 }
172
173 pub(crate) fn propose(store: &LessonStore, proposal: LessonProposal) -> Result<LessonInfo, GraphrtError> {
174 let proposal = Proposal {
175 agent: proposal.agent,
176 subject_user: proposal.user,
177 kind: kind_of(proposal.kind),
178 text: proposal.text,
179 signal: Signal::Feedback,
180 run_id: proposal.run_id,
181 author: proposal.author,
182 author_authenticated: proposal.author_authenticated,
183 evidence: proposal.evidence,
184 };
185 store.propose(proposal).map(info).map_err(failed)
186 }
187
188 pub(crate) fn decide(
189 store: &LessonStore,
190 agent: &str,
191 id: &str,
192 decision: LessonDecision,
193 ) -> Result<LessonInfo, GraphrtError> {
194 let decision = match decision {
195 LessonDecision::Approve => Decision::Approve,
196 LessonDecision::Reject => Decision::Reject,
197 LessonDecision::Retire => Decision::Retire,
198 };
199 store.decide(agent, id, decision, ACTOR).map(info).map_err(failed)
200 }
201
202 pub(crate) fn delete(store: &LessonStore, agent: &str, id: &str) -> Result<(), GraphrtError> {
203 store.delete(agent, id, ACTOR).map_err(failed)
204 }
205}
206
207#[cfg(feature = "learning")]
208pub(crate) use imp::{Store, install};
209
210#[cfg(not(feature = "learning"))]
211pub(crate) type Store = ();
212
213impl Runtime {
214 #[cfg(feature = "learning")]
215 fn lesson_store(&self) -> Result<&Store, GraphrtError> {
216 self.lessons.as_ref().ok_or_else(|| GraphrtError::not_configured("learning", "learning_dir"))
217 }
218}
219
220#[cfg(not(feature = "learning"))]
221macro_rules! without_learning {
222 ($($unused:expr),*) => {{
223 let _ = ($($unused,)*);
224 Err(GraphrtError::unsupported("learning", "learning"))
225 }};
226}
227
228#[uniffi::export]
229impl Runtime {
230 pub fn list_lessons(&self, agent: String, status: Option<LessonStatus>) -> Result<Vec<LessonInfo>, GraphrtError> {
232 #[cfg(feature = "learning")]
233 {
234 imp::list(self.lesson_store()?, &agent, status)
235 }
236 #[cfg(not(feature = "learning"))]
237 without_learning!(agent, status)
238 }
239
240 pub fn add_lesson(
242 &self,
243 agent: String,
244 user: Option<String>,
245 kind: LessonKind,
246 text: String,
247 ) -> Result<LessonInfo, GraphrtError> {
248 #[cfg(feature = "learning")]
249 {
250 imp::add(self.lesson_store()?, agent, user, kind, text)
251 }
252 #[cfg(not(feature = "learning"))]
253 without_learning!(agent, user, kind, text)
254 }
255
256 pub fn propose_lesson(&self, proposal: LessonProposal) -> Result<LessonInfo, GraphrtError> {
259 #[cfg(feature = "learning")]
260 {
261 imp::propose(self.lesson_store()?, proposal)
262 }
263 #[cfg(not(feature = "learning"))]
264 without_learning!(proposal)
265 }
266
267 pub fn decide_lesson(
269 &self,
270 agent: String,
271 id: String,
272 decision: LessonDecision,
273 ) -> Result<LessonInfo, GraphrtError> {
274 #[cfg(feature = "learning")]
275 {
276 imp::decide(self.lesson_store()?, &agent, &id, decision)
277 }
278 #[cfg(not(feature = "learning"))]
279 without_learning!(agent, id, decision)
280 }
281
282 pub fn delete_lesson(&self, agent: String, id: String) -> Result<(), GraphrtError> {
284 #[cfg(feature = "learning")]
285 {
286 imp::delete(self.lesson_store()?, &agent, &id)
287 }
288 #[cfg(not(feature = "learning"))]
289 without_learning!(agent, id)
290 }
291}