Skip to main content

qualia_client_core/api/
daemon.rs

1//! Daemon port + app session tokens
2
3#![allow(non_snake_case)]
4
5use super::*;
6
7use crate::qapp_paths::qapps_dir;
8use std::sync::atomic::{AtomicU16, Ordering};
9
10static ACTIVE_DAEMON_PORT: AtomicU16 = AtomicU16::new(0);
11
12/// Records the loopback port chosen when the graph daemon last started.
13pub fn set_active_daemon_port(port: u16) {
14    ACTIVE_DAEMON_PORT.store(port, Ordering::SeqCst);
15}
16
17/// Returns the active daemon port when known, otherwise the configured default.
18pub fn get_active_daemon_port() -> u16 {
19    let active = ACTIVE_DAEMON_PORT.load(Ordering::SeqCst);
20    if active != 0 {
21        return active;
22    }
23    crate::state::APP_STATE
24        .get()
25        .map(|state| state.config.lock().unwrap().daemon_port)
26        .unwrap_or(4242)
27}
28
29/// Issues a signed semantic app token scoped to the installed app's manifest shapes.
30pub fn build_anatomy_graph_context_json(
31    qapp_name: String,
32    user_prompt: String,
33    agent_reply: String,
34) -> Result<String, String> {
35    crate::anatomy_context::build_anatomy_graph_context_json(qapp_name, user_prompt, agent_reply)
36}
37
38/// Builds anatomy-graph-context-json-with-dicom.
39pub fn build_anatomy_graph_context_json_with_dicom(
40    qapp_name: String,
41    user_prompt: String,
42    agent_reply: String,
43    dicom_file_path: Option<String>,
44) -> Result<String, String> {
45    crate::anatomy_context::build_anatomy_graph_context_json_with_dicom(
46        qapp_name,
47        user_prompt,
48        agent_reply,
49        dicom_file_path,
50    )
51}
52
53/// Parses dicom-metadata-json.
54pub fn parse_dicom_metadata_json(file_path: String) -> Result<String, String> {
55    crate::anatomy_context::parse_dicom_metadata_json(file_path)
56}
57
58/// Builds dicom-overlay-spec-json.
59pub fn build_dicom_overlay_spec_json(file_path: String) -> Result<String, String> {
60    crate::anatomy_context::build_dicom_overlay_spec_json(file_path)
61}
62
63/// Submits a DICOM ingest.
64pub fn submit_dicom_ingest(file_path: String, patient_did_hash: u64) -> Result<u64, String> {
65    crate::qapp_api::submit_dicom_ingest(file_path, patient_did_hash)
66}
67
68/// DICOM ingest status.
69pub fn dicom_ingest_status(job_id: u64) -> u8 {
70    crate::qapp_api::dicom_ingest_status(job_id)
71}
72
73/// Executes a DICOM volume query.
74pub fn execute_dicom_volume_query(
75    patient_did_hash: u64,
76    series_hash: u64,
77) -> Result<Vec<u8>, String> {
78    crate::qapp_api::execute_dicom_volume_query(patient_did_hash, series_hash)
79}
80
81/// Issues a qapp session token.
82pub fn issue_qapp_session_token(qapp_name: &str) -> Result<String, String> {
83    let state = crate::state::APP_STATE.get().unwrap();
84    let data_dir = state.config.lock().unwrap().storage_path.clone();
85    let qapp_dir = qapps_dir(&data_dir).join(qapp_name);
86    if !qapp_dir.exists() {
87        return Err(format!("Qapp directory not found: {qapp_name}"));
88    }
89
90    let manifest = load_qapp_package_from_dir(&qapp_dir)?;
91    let qapp_did = format!(
92        "did:qualia:qapp:{}",
93        manifest.name.to_lowercase().replace(' ', "-")
94    );
95    let vault = state.key_vault.lock().unwrap();
96    vault.issue_qapp_token(
97        &qapp_did,
98        "localhost",
99        std::time::SystemTime::now()
100            .duration_since(std::time::UNIX_EPOCH)
101            .unwrap()
102            .as_secs()
103            + 86400,
104        &uuid::Uuid::new_v4().to_string(),
105        manifest.required_shapes.clone(),
106        qualia_core_db::identity::key_vault::SubgraphLayer::Professional,
107    )
108}