forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

mod.rs94 lines, 4.2 KiBHistoryRawDownload
1//! Talking to the AI services.
2//!
3//! The AI features live in separate processes (separate repos, likely separate
4//! languages), so this is the only seam between them and the API. It is a QUIC
5//! bridge: the backend listens, services dial in and register the capabilities
6//! they serve, and each request rides its own QUIC bidirectional stream over
7//! the service's single long-lived connection.
8//!
9//! Why QUIC rather than HTTP: stream multiplexing is in the transport, so N
10//! concurrent requests to one service need no correlation-id bookkeeping and
11//! no head-of-line blocking — a 30-second inference on stream 7 does not delay
12//! the answer on stream 9. Connection setup happens once, not per request.
13//!
14//! * [`api`] — which REST paths a service may read back through the bridge
15//! * [`protocol`] — frames on the wire
16//! * [`capability`] — the capabilities the **backend** serves, and their scopes
17//! * [`chat`] — the JSON payloads carried for the `chat.reply` capability
18//! * [`rag`] — the `rag.index` payloads, and the course-note dispatch behind them
19//! * [`rag_chat`] — the `rag.chat` payloads: a scoped question and the citations behind its answer
20//! * [`rag_study`] — the `rag.summarize`/`rag.questions` payloads: one range of one corpus, answered in one shot
21//! * [`podcast`] — the `podcast.*` job payloads, the relay behind submit/cancel,
22//! and the job-state report the service calls back
23//! * [`server`] — the listener, handshake, and [`server::AiBridge::dispatch`]
24//! * [`registry`] — who is connected here, and who gets the next request
25//! * [`tls`] — the listener's certificate
26//!
27//! The transport is generic; the features land on top of it. So far that is
28//! the chatbot (`web::chatbot`), which routes on the `chat.reply` capability,
29//! course-note indexing (`web::course_notes`), which routes on `rag.index`,
30//! and the podcast relay (`web::podcast`), which dispatches `podcast.submit`/
31//! `podcast.cancel`, serves `podcast.report`, and streams the ingested audio.
32
33pub mod api;
34pub mod capability;
35pub mod chat;
36pub mod error;
37pub mod insight;
38pub mod podcast;
39pub mod protocol;
40pub mod rag;
41pub mod rag_chat;
42pub mod rag_study;
43pub mod registry;
44pub mod server;
45pub mod tls;
46
47pub use chat::{ChatReplyPayload, ChatRequestPayload, ChatRole, ChatTurn};
48pub use error::AiError;
49pub use insight::{
50 ClassRequest, ClassResponse, RefreshRequest, RefreshResponse, StudentRequest, StudentResponse,
51};
52pub use rag::{RagFile, RagIndexPayload};
53pub use rag_chat::{
54 RagChatReplyPayload, RagChatRequestPayload, RagCitation, RagScopePair, RagTurn,
55};
56pub use registry::{AiRegistry, WorkerSnapshot};
57pub use server::{AiBridge, BridgeConfig};
58
59use crate::config::Config;
60
61/// Start the QUIC bridge the AI services dial into, if one is configured.
62///
63/// Unconfigured is a supported deployment, not a degraded one: the school API
64/// predates the AI features and must keep running without them, so an absent
65/// `AI_QUIC_ADDR` returns `None` and nothing else changes. A *misconfigured*
66/// bridge is the opposite — a bad address, an unreadable certificate or a
67/// missing token means the operator asked for AI and would otherwise get a
68/// silently dead feature, so that fails the boot.
69///
70/// In particular a set address with no `AI_SHARED_TOKEN` must never fall back
71/// to listening: an unauthenticated bridge would let anyone who can reach the
72/// UDP port register a worker and answer real users' messages.
73pub async fn start_bridge(cfg: &Config) -> Result<Option<AiBridge>, AiError> {
74 let Some(raw_addr) = cfg.ai_quic_addr.as_deref() else {
75 tracing::info!("AI bridge disabled (AI_QUIC_ADDR unset)");
76 return Ok(None);
77 };
78 let addr = raw_addr
79 .parse()
80 .map_err(|e| AiError::Setup(format!("AI_QUIC_ADDR `{raw_addr}` is not an address: {e}")))?;
81 let token = cfg
82 .ai_shared_token
83 .clone()
84 .ok_or_else(|| AiError::Setup("AI_SHARED_TOKEN must be set when AI_QUIC_ADDR is".into()))?;
85 let bridge = AiBridge::bind(BridgeConfig {
86 addr,
87 token,
88 cert_path: cfg.ai_tls_cert.clone(),
89 key_path: cfg.ai_tls_key.clone(),
90 request_timeout: std::time::Duration::from_secs(cfg.ai_request_timeout_secs),
91 })
92 .await?;
93 Ok(Some(bridge))
94}