| 1 | use std::sync::OnceLock; |
| 2 | |
| 3 | use codewhale_config::{EntryRef, PluginAuthority}; |
| 4 | use codewhale_plugins::PluginRegistry; |
| 5 | use codewhale_plugins::composition_scope::SelectionRevision; |
| 6 | use codewhale_plugins::types::PluginSkillSnapshot; |
| 7 | use serde::{Deserialize, Serialize}; |
| 8 | |
| 9 | /// Public receipt reuses the existing process and host/owner lifetimes. The |
| 10 | /// host-only owner token never enters prompts or persisted queued messages. |
| 11 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 12 | #[serde(deny_unknown_fields)] |
| 13 | pub struct NativeSkillRef { |
| 14 | pub boot_id: String, |
| 15 | pub host_generation: u64, |
| 16 | pub owner_generation: u64, |
| 17 | pub handle: u64, |
| 18 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 19 | pub selection: Option<SelectionRevision>, |
| 20 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 21 | pub scope: Option<EntryRef>, |
| 22 | } |
| 23 | |
| 24 | pub type NativeSkillRoots = Vec<(Vec<PluginSkillSnapshot>, PluginAuthority, NativeSkillRef)>; |
| 25 | |
| 26 | pub trait SkillHostPort: Send + Sync { |
| 27 | fn check_selection( |
| 28 | &self, |
| 29 | selection: Option<SelectionRevision>, |
| 30 | plugins: Option<&PluginRegistry>, |
| 31 | plugin_id: &str, |
| 32 | hash: &str, |
| 33 | scope: Option<&EntryRef>, |
| 34 | ) -> Result<(), String>; |
| 35 | fn verify_native_skill( |
| 36 | &self, |
| 37 | authority: &PluginAuthority, |
| 38 | reference: &NativeSkillRef, |
| 39 | ) -> Result<(), String>; |
| 40 | fn native_roots(&self, plugins: &PluginRegistry) -> NativeSkillRoots; |
| 41 | } |
| 42 | |
| 43 | static PORT: OnceLock<&'static dyn SkillHostPort> = OnceLock::new(); |
| 44 | |
| 45 | pub fn install_skill_host(port: &'static dyn SkillHostPort) { |
| 46 | let _ = PORT.set(port); |
| 47 | } |
| 48 | |
| 49 | const UNAVAILABLE: &str = "Native skill authority is unavailable; select the skill again"; |
| 50 | |
| 51 | pub fn check_selection( |
| 52 | selection: Option<SelectionRevision>, |
| 53 | plugins: Option<&PluginRegistry>, |
| 54 | plugin_id: &str, |
| 55 | hash: &str, |
| 56 | scope: Option<&EntryRef>, |
| 57 | ) -> Result<(), String> { |
| 58 | match PORT.get() { |
| 59 | Some(port) => port.check_selection(selection, plugins, plugin_id, hash, scope), |
| 60 | None => Err(UNAVAILABLE.into()), |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | pub fn verify_native_skill( |
| 65 | authority: &PluginAuthority, |
| 66 | reference: &NativeSkillRef, |
| 67 | ) -> Result<(), String> { |
| 68 | match PORT.get() { |
| 69 | Some(port) => port.verify_native_skill(authority, reference), |
| 70 | None => Err(UNAVAILABLE.into()), |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | pub fn native_roots(plugins: &PluginRegistry) -> NativeSkillRoots { |
| 75 | PORT.get() |
| 76 | .map_or_else(Vec::new, |port| port.native_roots(plugins)) |
| 77 | } |
| 78 |