qualia_client_core/inference_policy/
product.rs1use super::{actor::InferenceActorSession, store};
3use qualia_core_db::llm_agent::{
4 native_request::{NativeHostCapability, NativePreparationSpec, NativeProductSession},
5 LocalLlmAgent,
6};
7use std::sync::{Arc, Mutex, OnceLock};
8
9const MAX_ROUTES: usize = 16;
10type Routes = [Option<(String, Arc<NativeProductSession>)>; MAX_ROUTES];
11static ROUTES: OnceLock<Mutex<Routes>> = OnceLock::new();
12
13pub fn bind_chat_inference_session(
17 chat_id: &str,
18 actor: &InferenceActorSession,
19 preparation: NativePreparationSpec,
20) -> Result<(), String> {
21 if chat_id.is_empty() || chat_id.len() > 4096 {
22 return Err("A bounded chat session identifier is required".into());
23 }
24 let product = prepare_session(actor, preparation)?;
25 let mut routes = ROUTES
26 .get_or_init(|| Mutex::new(std::array::from_fn(|_| None)))
27 .lock()
28 .map_err(|_| "Chat authority routes unavailable")?;
29 let index = routes
31 .iter()
32 .position(|r| r.as_ref().is_some_and(|(id, _)| id == chat_id))
33 .or_else(|| routes.iter().position(Option::is_none))
34 .ok_or("Chat authority route capacity exhausted; unbind an old chat")?;
35 routes[index] = Some((chat_id.to_owned(), Arc::new(product)));
36 Ok(())
37}
38
39fn prepare_session(
40 actor: &InferenceActorSession,
41 preparation: NativePreparationSpec,
42) -> Result<NativeProductSession, String> {
43 store::with_host(|h, key, _| {
44 if !Arc::ptr_eq(&h.owner, actor.owner()) {
45 return Err("Actor belongs to a different configured authority".into());
46 }
47 let capability = NativeHostCapability::from_installed(
48 Arc::clone(&h.host),
49 key.clone(),
50 Arc::clone(&h.owner),
51 )
52 .map_err(|e| e.to_string())?;
53 actor.product_session(Arc::new(capability), preparation)
54 })
55}
56
57pub fn bind_mcp_inference_session(
60 actor: &InferenceActorSession,
61 preparation: NativePreparationSpec,
62) -> Result<(), String> {
63 let product = prepare_session(actor, preparation)?;
64 qualia_core_db::mcp::inference_session::configure(Arc::new(product))
65}
66
67pub fn native_product_session(
69 actor: &InferenceActorSession,
70 preparation: NativePreparationSpec,
71) -> Result<Arc<NativeProductSession>, String> {
72 prepare_session(actor, preparation).map(Arc::new)
73}
74
75pub fn bind_poet_inference_session(
78 actor: &InferenceActorSession,
79 preparation: NativePreparationSpec,
80) -> Result<(), String> {
81 let product = prepare_session(actor, preparation)?;
82 qualia_core_db::services::poet_llm_authority::configure(Arc::new(product));
83 Ok(())
84}
85
86pub fn unbind_chat_inference_session(chat_id: &str) {
87 if let Some(routes) = ROUTES.get() {
88 let mut routes = routes.lock().unwrap_or_else(|e| e.into_inner());
89 for route in routes.iter_mut() {
90 if route.as_ref().is_some_and(|(id, _)| id == chat_id) {
91 *route = None;
92 }
93 }
94 }
95}
96
97pub(super) fn clear() {
98 qualia_core_db::mcp::inference_session::disconnect();
99 qualia_core_db::services::poet_llm_authority::disconnect();
100 if let Some(routes) = ROUTES.get() {
101 *routes.lock().unwrap_or_else(|e| e.into_inner()) = std::array::from_fn(|_| None);
102 }
103}
104
105pub(crate) fn bind_turn(chat_id: &str, agent: &LocalLlmAgent) -> Result<(), String> {
106 store::with_host(|_, _, _| Ok(()))?;
109 let product = ROUTES
110 .get()
111 .ok_or("Log in and bind inference authority for this chat")?
112 .lock()
113 .map_err(|_| "Chat authority routes unavailable")?
114 .iter()
115 .flatten()
116 .find(|(id, _)| id == chat_id)
117 .map(|(_, p)| Arc::clone(p))
118 .ok_or("Log in and bind inference authority for this chat")?;
119 agent
120 .bind_native_governed_request(product.bind().map_err(|e| e.to_string())?)
121 .map_err(|e| e.to_string())
122}