Skip to main content

qualia_client_core/api/
conditioning.rs

1//! Active semantic conditioning profile (Prompt Precision).
2//!
3//! The profile document is the same JSON shape the POET HTTP API accepts under
4//! `conditioning`. Once activated it is compiled per turn and lowered for
5//! every inference route — native chat, orchestrated graph-mutation decode,
6//! Ollama, and remote MCP — with a per-route receipt on each result.
7
8use std::path::Path;
9
10fn conditioning_storage() -> Result<String, String> {
11    let state = crate::state::APP_STATE
12        .get()
13        .ok_or("Application not initialized")?;
14    let storage = state
15        .config
16        .lock()
17        .map_err(|e| e.to_string())?
18        .storage_path
19        .clone();
20    Ok(storage)
21}
22
23/// Validate (by compiling) and activate a semantic conditioning profile for
24/// all inference routes. The prior profile is replaced atomically enough for
25/// a single-user store: activation only succeeds when the new profile
26/// compiles, so a bad document can never displace a working one.
27pub fn conditioning_profile_activate(profile_json: String) -> Result<serde_json::Value, String> {
28    let storage = conditioning_storage()?;
29    let profile: serde_json::Value =
30        serde_json::from_str(&profile_json).map_err(|e| format!("invalid profile JSON: {e}"))?;
31    let receipt = crate::conditioning::activate_profile(Path::new(&storage), &profile)?;
32    serde_json::to_value(serde_json::json!({
33        "active": true,
34        "profile_id": receipt.profile_id,
35        "plan_id": receipt.plan_id,
36        "selected_evidence": receipt.selected_evidence,
37        "token_budget": receipt.token_budget,
38    }))
39    .map_err(|e| e.to_string())
40}
41
42/// Deactivate the active profile. Routes revert to their unconditioned shape.
43pub fn conditioning_profile_deactivate() -> Result<serde_json::Value, String> {
44    let storage = conditioning_storage()?;
45    let deactivated = crate::conditioning::deactivate_profile(Path::new(&storage));
46    Ok(serde_json::json!({ "active": false, "deactivated": deactivated }))
47}
48
49/// Current activation state (`active`, `profile_id`, store path).
50pub fn conditioning_profile_status() -> Result<serde_json::Value, String> {
51    let storage = conditioning_storage()?;
52    Ok(crate::conditioning::active_profile_status(Path::new(
53        &storage,
54    )))
55}