khive-pack-session 0.20.0

Session verb pack - store/list/resume/export over the notes substrate
Documentation
//! `SessionPack` self-registration factory and `PackRuntime` dispatch impl.

use async_trait::async_trait;
use serde_json::Value;
use std::sync::Arc;

use khive_runtime::pack::PackRuntime;
use khive_runtime::{KhiveRuntime, KindHook, NamespaceToken, RuntimeError, VerbRegistry};
use khive_types::{HandlerDef, Pack, PackColumnAddition, PackSchemaPlan};

use crate::{handlers, vocab::SESSION_HANDLERS};

pub struct SessionPack {
    runtime: KhiveRuntime,
}

impl SessionPack {
    pub fn new(runtime: KhiveRuntime) -> Self {
        Self { runtime }
    }

    pub(crate) fn runtime(&self) -> &KhiveRuntime {
        &self.runtime
    }
}

impl Pack for SessionPack {
    const NAME: &'static str = "session";
    const NOTE_KINDS: &'static [&'static str] = &["session"];
    const ENTITY_KINDS: &'static [&'static str] = &[];
    const HANDLERS: &'static [HandlerDef] = &SESSION_HANDLERS;
    const REQUIRES: &'static [&'static str] = &["kg"];
    const SCHEMA_PLAN: Option<PackSchemaPlan> = Some(PackSchemaPlan {
        pack: "session",
        statements: &crate::vocab::SESSION_SCHEMA_PLAN_STMTS,
    });
    const SCHEMA_COLUMN_ADDITIONS: &'static [PackColumnAddition] =
        &crate::vocab::SESSION_SCHEMA_COLUMN_ADDITIONS;
}

struct SessionPackFactory;

impl khive_runtime::PackFactory for SessionPackFactory {
    khive_runtime::pack_factory_metadata!(SessionPack);

    fn create(&self, runtime: KhiveRuntime) -> Box<dyn khive_runtime::PackRuntime> {
        Box::new(SessionPack::new(runtime))
    }
}

inventory::submit! { khive_runtime::PackRegistration(&SessionPackFactory) }

#[async_trait]
impl PackRuntime for SessionPack {
    khive_runtime::pack_runtime_metadata!();

    fn kind_hook(&self, kind: &str) -> Option<Arc<dyn KindHook>> {
        (kind == "session").then(|| Arc::new(crate::hook::SessionKindHook) as Arc<dyn KindHook>)
    }

    async fn warm(&self) {
        // Mirror services ingest external sessions and therefore write notes.
        // A snapshot-inspection runtime may expose session read verbs, but it
        // must never launch a background mirror against its read-only backend.
        if self.runtime.is_read_only() {
            return;
        }
        let config = crate::mirror::MirrorConfig::from_env();
        if !config.enabled
            && !config.codex_enabled
            && !config.chatgpt_enabled
            && !config.claude_ai_enabled
        {
            return;
        }
        let runtime = self.runtime().clone();
        tokio::spawn(async move {
            crate::mirror::run_mirror_service(runtime, config).await;
        });
    }

    async fn dispatch(
        &self,
        verb: &str,
        params: Value,
        _registry: &VerbRegistry,
        token: &NamespaceToken,
    ) -> Result<Value, RuntimeError> {
        let runtime = self.runtime();
        match verb {
            "session.store" => handlers::store::handle_store(runtime, token, params).await,
            "session.list" => handlers::list::handle_list(runtime, token, params).await,
            "session.resume" => handlers::resume::handle_resume(runtime, token, params).await,
            "session.export" => handlers::export::handle_export(runtime, token, params).await,
            "session.search" => handlers::search::handle_search(runtime, token, params).await,
            "session.stats" => handlers::maintenance::handle_stats(runtime, params).await,
            "session.vacuum" => handlers::maintenance::handle_vacuum(runtime, params).await,
            _ => Err(RuntimeError::InvalidInput(format!(
                "session pack does not handle verb {verb:?}; valid verbs: \
                 session.store, session.list, session.resume, session.export, session.search, \
                 session.stats, session.vacuum"
            ))),
        }
    }
}