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::{KhiveRuntime, KindHook, NamespaceToken, RuntimeError, VerbRegistry};
9use khive_types::{HandlerDef, Pack, PackColumnAddition, PackSchemaPlan};
10
11use crate::{handlers, vocab::SESSION_HANDLERS};
12
13pub struct SessionPack {
14    runtime: KhiveRuntime,
15}
16
17impl SessionPack {
18    pub fn new(runtime: KhiveRuntime) -> Self {
19        Self { runtime }
20    }
21
22    pub(crate) fn runtime(&self) -> &KhiveRuntime {
23        &self.runtime
24    }
25}
26
27impl Pack for SessionPack {
28    const NAME: &'static str = "session";
29    const NOTE_KINDS: &'static [&'static str] = &["session"];
30    const ENTITY_KINDS: &'static [&'static str] = &[];
31    const HANDLERS: &'static [HandlerDef] = &SESSION_HANDLERS;
32    const REQUIRES: &'static [&'static str] = &["kg"];
33    const SCHEMA_PLAN: Option<PackSchemaPlan> = Some(PackSchemaPlan {
34        pack: "session",
35        statements: &crate::vocab::SESSION_SCHEMA_PLAN_STMTS,
36    });
37    const SCHEMA_COLUMN_ADDITIONS: &'static [PackColumnAddition] =
38        &crate::vocab::SESSION_SCHEMA_COLUMN_ADDITIONS;
39}
40
41struct SessionPackFactory;
42
43impl khive_runtime::PackFactory for SessionPackFactory {
44    khive_runtime::pack_factory_metadata!(SessionPack);
45
46    fn create(&self, runtime: KhiveRuntime) -> Box<dyn khive_runtime::PackRuntime> {
47        Box::new(SessionPack::new(runtime))
48    }
49}
50
51inventory::submit! { khive_runtime::PackRegistration(&SessionPackFactory) }
52
53#[async_trait]
54impl PackRuntime for SessionPack {
55    khive_runtime::pack_runtime_metadata!();
56
57    fn kind_hook(&self, kind: &str) -> Option<Arc<dyn KindHook>> {
58        (kind == "session").then(|| Arc::new(crate::hook::SessionKindHook) as Arc<dyn KindHook>)
59    }
60
61    async fn warm(&self) {
62        // Mirror services ingest external sessions and therefore write notes.
63        // A snapshot-inspection runtime may expose session read verbs, but it
64        // must never launch a background mirror against its read-only backend.
65        if self.runtime.is_read_only() {
66            return;
67        }
68        let config = crate::mirror::MirrorConfig::from_env();
69        if !config.enabled
70            && !config.codex_enabled
71            && !config.chatgpt_enabled
72            && !config.claude_ai_enabled
73        {
74            return;
75        }
76        let runtime = self.runtime().clone();
77        tokio::spawn(async move {
78            crate::mirror::run_mirror_service(runtime, config).await;
79        });
80    }
81
82    async fn dispatch(
83        &self,
84        verb: &str,
85        params: Value,
86        _registry: &VerbRegistry,
87        token: &NamespaceToken,
88    ) -> Result<Value, RuntimeError> {
89        let runtime = self.runtime();
90        match verb {
91            "session.store" => handlers::store::handle_store(runtime, token, params).await,
92            "session.list" => handlers::list::handle_list(runtime, token, params).await,
93            "session.resume" => handlers::resume::handle_resume(runtime, token, params).await,
94            "session.export" => handlers::export::handle_export(runtime, token, params).await,
95            "session.search" => handlers::search::handle_search(runtime, token, params).await,
96            "session.stats" => handlers::maintenance::handle_stats(runtime, params).await,
97            "session.vacuum" => handlers::maintenance::handle_vacuum(runtime, params).await,
98            _ => Err(RuntimeError::InvalidInput(format!(
99                "session pack does not handle verb {verb:?}; valid verbs: \
100                 session.store, session.list, session.resume, session.export, session.search, \
101                 session.stats, session.vacuum"
102            ))),
103        }
104    }
105}