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 ask_rql_planner;
18pub mod audit_record_builder;
19pub mod batch_client;
20pub mod cdc_enrichment;
21pub mod citation_parser;
22pub mod cost_guard;
23pub mod dedup_cache;
24pub mod determinism_decider;
25pub mod explain_plan_builder;
26pub mod grpc_ask_message;
27pub mod local_embedding;
28pub mod mcp_ask_tool;
29pub mod metrics;
30pub mod moderation;
31pub mod ner;
32pub mod pg_wire_ask_row_encoder;
33pub mod prompt_assembler;
34pub mod prompt_template;
35pub mod provider_capabilities;
36pub mod provider_failover;
37pub mod provider_gate;
38pub mod rrf_fuser;
39pub mod sources_fingerprint;
40pub mod sse_frame_encoder;
41pub mod strict_validator;
42pub mod text_chunker;
43pub mod transport;
44pub mod urn_codec;
45pub mod vision;
46
47pub(crate) fn block_on_ai<F, T>(future: F) -> crate::RedDBResult<T>
48where
49    F: std::future::Future<Output = T> + Send + 'static,
50    T: Send + 'static,
51{
52    if let Ok(handle) = tokio::runtime::Handle::try_current() {
53        if matches!(
54            handle.runtime_flavor(),
55            tokio::runtime::RuntimeFlavor::MultiThread
56        ) {
57            return Ok(tokio::task::block_in_place(|| handle.block_on(future)));
58        }
59
60        return std::thread::Builder::new()
61            .name("reddb-ai-blocking".to_string())
62            .spawn(move || {
63                let runtime = tokio::runtime::Builder::new_current_thread()
64                    .enable_all()
65                    .build()
66                    .map_err(|err| {
67                        crate::RedDBError::Query(format!("failed to start AI runtime: {err}"))
68                    })?;
69                Ok(runtime.block_on(future))
70            })
71            .map_err(|err| {
72                crate::RedDBError::Query(format!("failed to spawn AI runtime thread: {err}"))
73            })?
74            .join()
75            .map_err(|_| crate::RedDBError::Query("AI runtime thread panicked".to_string()))?;
76    }
77
78    let runtime = tokio::runtime::Builder::new_current_thread()
79        .enable_all()
80        .build()
81        .map_err(|err| crate::RedDBError::Query(format!("failed to start AI runtime: {err}")))?;
82    Ok(runtime.block_on(future))
83}