Skip to main content

khive_pack_session/
pack.rs

1//! `SessionPack` self-registration factory and `PackRuntime` dispatch impl.
2
3use async_trait::async_trait;
4use serde_json::Value;
5use std::sync::Arc;
6
7use khive_runtime::pack::PackRuntime;
8use khive_runtime::{
9    KhiveRuntime, KindHook, NamespaceToken, RuntimeError, SchemaPlan, VerbRegistry,
10};
11use khive_types::{EdgeEndpointRule, HandlerDef, Pack, PackSchemaPlan};
12
13use crate::{handlers, vocab::SESSION_HANDLERS};
14
15pub struct SessionPack {
16    runtime: KhiveRuntime,
17}
18
19impl SessionPack {
20    pub fn new(runtime: KhiveRuntime) -> Self {
21        Self { runtime }
22    }
23
24    pub(crate) fn runtime(&self) -> &KhiveRuntime {
25        &self.runtime
26    }
27}
28
29impl Pack for SessionPack {
30    const NAME: &'static str = "session";
31    const NOTE_KINDS: &'static [&'static str] = &["session"];
32    const ENTITY_KINDS: &'static [&'static str] = &[];
33    const HANDLERS: &'static [HandlerDef] = &SESSION_HANDLERS;
34    const REQUIRES: &'static [&'static str] = &["kg"];
35    const SCHEMA_PLAN: Option<PackSchemaPlan> = Some(PackSchemaPlan {
36        pack: "session",
37        statements: &crate::vocab::SESSION_SCHEMA_PLAN_STMTS,
38    });
39}
40
41struct SessionPackFactory;
42
43impl khive_runtime::PackFactory for SessionPackFactory {
44    fn name(&self) -> &'static str {
45        "session"
46    }
47
48    fn requires(&self) -> &'static [&'static str] {
49        &["kg"]
50    }
51
52    fn create(&self, runtime: KhiveRuntime) -> Box<dyn khive_runtime::PackRuntime> {
53        Box::new(SessionPack::new(runtime))
54    }
55}
56
57inventory::submit! { khive_runtime::PackRegistration(&SessionPackFactory) }
58
59#[async_trait]
60impl PackRuntime for SessionPack {
61    fn name(&self) -> &str {
62        <SessionPack as Pack>::NAME
63    }
64
65    fn note_kinds(&self) -> &'static [&'static str] {
66        <SessionPack as Pack>::NOTE_KINDS
67    }
68
69    fn kind_hook(&self, kind: &str) -> Option<Arc<dyn KindHook>> {
70        (kind == "session").then(|| Arc::new(crate::hook::SessionKindHook) as Arc<dyn KindHook>)
71    }
72
73    fn entity_kinds(&self) -> &'static [&'static str] {
74        <SessionPack as Pack>::ENTITY_KINDS
75    }
76
77    fn handlers(&self) -> &'static [HandlerDef] {
78        <SessionPack as Pack>::HANDLERS
79    }
80
81    fn edge_rules(&self) -> &'static [EdgeEndpointRule] {
82        &[]
83    }
84
85    fn requires(&self) -> &'static [&'static str] {
86        <SessionPack as Pack>::REQUIRES
87    }
88
89    fn schema_plan(&self) -> SchemaPlan {
90        SchemaPlan {
91            pack: "session",
92            statements: &crate::vocab::SESSION_SCHEMA_PLAN_STMTS,
93        }
94    }
95
96    async fn warm(&self) {
97        // Mirror services ingest external sessions and therefore write notes.
98        // A snapshot-inspection runtime may expose session read verbs, but it
99        // must never launch a background mirror against its read-only backend.
100        if self.runtime.is_read_only() {
101            return;
102        }
103        let config = crate::mirror::MirrorConfig::from_env();
104        if !config.enabled
105            && !config.codex_enabled
106            && !config.chatgpt_enabled
107            && !config.claude_ai_enabled
108        {
109            return;
110        }
111        let runtime = self.runtime().clone();
112        tokio::spawn(async move {
113            crate::mirror::run_mirror_service(runtime, config).await;
114        });
115    }
116
117    async fn dispatch(
118        &self,
119        verb: &str,
120        params: Value,
121        _registry: &VerbRegistry,
122        token: &NamespaceToken,
123    ) -> Result<Value, RuntimeError> {
124        let runtime = self.runtime();
125        match verb {
126            "session.store" => handlers::store::handle_store(runtime, token, params).await,
127            "session.list" => handlers::list::handle_list(runtime, token, params).await,
128            "session.resume" => handlers::resume::handle_resume(runtime, token, params).await,
129            "session.export" => handlers::export::handle_export(runtime, token, params).await,
130            "session.search" => handlers::search::handle_search(runtime, token, params).await,
131            _ => Err(RuntimeError::InvalidInput(format!(
132                "session pack does not handle verb {verb:?}; valid verbs: \
133                 session.store, session.list, session.resume, session.export, session.search"
134            ))),
135        }
136    }
137}