forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

capability.rs101 lines, 4.2 KiBHistoryRawDownload
1//! The table of capabilities the **backend** serves — the operations a
2//! service calls over the bridge's client-initiated `CapabilityRequest` shape
3//! (`hab/2`'s third request kind), and each one's scope.
4//!
5//! There is one table per capability family, and this module is the walk over
6//! them: [`crate::ai::insight`] serves ZEKA's storage surface,
7//! [`crate::ai::podcast`] serves the podcast job's state reports. Both answer
8//! the same frame, both are dispatched from the same place — the bridge's
9//! capability dispatcher in `ai::server` — and neither owns the other's
10//! names: a name neither table holds is `unknown_capability`, with no prefix
11//! match and no fallback, exactly as the two tables' own doc comments say.
12//!
13//! The scope decides one thing only: whether the bridge resolves the frame's
14//! school before the operation runs. A [`Scope::School`] operation runs against
15//! the named school's own database — the frame's school is the *only* tenancy
16//! input, and a school id that matches nothing is `unknown_school`, never a
17//! fallback to another school or to the control database.
18
19use serde_json::Value;
20
21use crate::ai::podcast;
22use crate::constant::AI_PODCAST_REPORT_CAPABILITY;
23use crate::database::Database;
24use crate::tenant::ResolvedTenant;
25
26pub use crate::ai::insight::Scope;
27
28/// The refusal shape every backend-served capability answers with: a flat,
29/// stable code and a message that names the field or reason. Same alias as
30/// [`crate::ai::insight::Refusal`] — one vocabulary across families.
31pub type Refusal = (&'static str, String);
32
33/// Every capability the podcast family serves, and its scope. One entry today;
34/// a table rather than a match arm so the walk in [`scope_of`] and the
35/// dispatch in [`serve`] are structurally the same decision, and a second
36/// podcast operation is one added row.
37pub const PODCAST_SERVED: &[(&str, Scope)] = &[(AI_PODCAST_REPORT_CAPABILITY, Scope::School)];
38
39/// The scope of one backend-served capability, or `None` if no family serves
40/// it. Matched exactly, family by family, in a fixed order.
41pub fn scope_of(capability: &str) -> Option<Scope> {
42 crate::ai::insight::scope_of(capability).or_else(|| {
43 PODCAST_SERVED
44 .iter()
45 .find(|(name, _)| *name == capability)
46 .map(|(_, scope)| *scope)
47 })
48}
49
50/// Run one backend-served capability. The caller (the bridge) resolves the
51/// school *before* this is called, from [`scope_of`]'s answer — a
52/// [`Scope::Deployment`] operation gets `None`, and a [`Scope::School`] one
53/// always gets the tenant.
54pub async fn serve(
55 capability: &str,
56 school: Option<&ResolvedTenant>,
57 control: &Database,
58 payload: &Value,
59) -> Result<Value, Refusal> {
60 if crate::ai::insight::scope_of(capability).is_some() {
61 return crate::ai::insight::serve(capability, school, control, payload).await;
62 }
63 match capability {
64 AI_PODCAST_REPORT_CAPABILITY => {
65 let Some(tenant) = school else {
66 return Err((
67 "internal",
68 format!("`{AI_PODCAST_REPORT_CAPABILITY}` is school-scoped but no school was resolved"),
69 ));
70 };
71 // The frame's payload is cloned once per report call, and a report
72 // is a handful of short fields — this is the price of one dispatch
73 // table across two families that decode differently.
74 podcast::report(&tenant.db, payload.clone()).await
75 }
76 _ => Err((
77 "unknown_capability",
78 format!("the backend serves no `{capability}` operation"),
79 )),
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn every_family_name_resolves_and_a_stranger_does_not() {
89 assert_eq!(scope_of(AI_PODCAST_REPORT_CAPABILITY), Some(Scope::School));
90 assert_eq!(scope_of("podcast.submit"), None, "worker-only, not served");
91 assert_eq!(scope_of("podcast.status"), None, "deleted: the row answers");
92 assert_eq!(scope_of("podcast.nothing"), None);
93 assert_eq!(scope_of(""), None);
94 }
95
96 #[test]
97 fn the_podcast_table_is_exactly_its_documented_vocabulary() {
98 assert_eq!(PODCAST_SERVED.len(), 1);
99 assert!(PODCAST_SERVED.iter().all(|(_, scope)| *scope == Scope::School));
100 }
101}