Skip to main content

reddb_server/runtime/ai/
mod.rs

1//! AI runtime modules.
2//!
3//! Houses the LLM-touching pieces of the AskPipeline:
4//!
5//! - [`prompt_template`] — typed-slot prompt assembly with secret
6//!   redaction and injection defence (issue #122).
7//! - [`ner`] — opt-in LLM backend for AskPipeline Stage 1 entity
8//!   extraction with auth gate, response sanitization, and a
9//!   configurable heuristic fallback (issue #123).
10//!
11//! Both modules are pure additions — call-site wiring lives in
12//! [`super::ask_pipeline`] and is opt-in via `ai.ner.backend = "llm"`
13//! at runtime config time.
14
15pub mod answer_cache_key;
16pub mod ask_response_envelope;
17pub mod audit_record_builder;
18pub mod batch_client;
19pub mod citation_parser;
20pub mod cost_guard;
21pub mod dedup_cache;
22pub mod determinism_decider;
23pub mod explain_plan_builder;
24pub mod grpc_ask_message;
25pub mod mcp_ask_tool;
26pub mod metrics;
27pub mod ner;
28pub mod pg_wire_ask_row_encoder;
29pub mod prompt_assembler;
30pub mod prompt_template;
31pub mod provider_capabilities;
32pub mod provider_failover;
33pub mod rrf_fuser;
34pub mod sources_fingerprint;
35pub mod sse_frame_encoder;
36pub mod strict_validator;
37pub mod text_chunker;
38pub mod transport;
39pub mod urn_codec;
40
41pub(crate) fn block_on_ai<F, T>(future: F) -> crate::RedDBResult<T>
42where
43    F: std::future::Future<Output = T> + Send + 'static,
44    T: Send + 'static,
45{
46    if let Ok(handle) = tokio::runtime::Handle::try_current() {
47        if matches!(
48            handle.runtime_flavor(),
49            tokio::runtime::RuntimeFlavor::MultiThread
50        ) {
51            return Ok(tokio::task::block_in_place(|| handle.block_on(future)));
52        }
53
54        return std::thread::Builder::new()
55            .name("reddb-ai-blocking".to_string())
56            .spawn(move || {
57                let runtime = tokio::runtime::Builder::new_current_thread()
58                    .enable_all()
59                    .build()
60                    .map_err(|err| {
61                        crate::RedDBError::Query(format!("failed to start AI runtime: {err}"))
62                    })?;
63                Ok(runtime.block_on(future))
64            })
65            .map_err(|err| {
66                crate::RedDBError::Query(format!("failed to spawn AI runtime thread: {err}"))
67            })?
68            .join()
69            .map_err(|_| crate::RedDBError::Query("AI runtime thread panicked".to_string()))?;
70    }
71
72    let runtime = tokio::runtime::Builder::new_current_thread()
73        .enable_all()
74        .build()
75        .map_err(|err| crate::RedDBError::Query(format!("failed to start AI runtime: {err}")))?;
76    Ok(runtime.block_on(future))
77}