el_runtime/ports.rs
1//! Port traits the Inference Runtime depends on (the collaborator contexts).
2
3use el_core::{Result, Token};
4use el_safety::SafetySteerer;
5
6/// The inference engine adapter (`RuntimeAcl`). Implemented for real by Candle
7/// in the excluded adapter `el-engine-candle` (ADR-002).
8///
9/// Logits are integer milli-logits to keep the orchestrator deterministic and
10/// float-free; a real engine quantises its float logits at the ACL boundary.
11pub trait InferenceEngine {
12 /// Encode the (compressed) prompt; returns the resulting KV length.
13 fn prefill(&mut self, tokens: &[Token]) -> Result<u32>;
14 /// Produce next-token logits given the committed context.
15 fn next_logits(&mut self, committed: &[Token]) -> Vec<i32>;
16 /// The end-of-sequence token id.
17 fn eos_token(&self) -> Token;
18}
19
20/// Prompt Compression port (LLMLingua-2 — context 2).
21pub trait PromptCompressor {
22 fn compress(&self, tokens: &[Token]) -> Vec<Token>;
23}
24
25/// Grammar Constraint port (llguidance — context 4). Returns a per-token allow
26/// mask of length `vocab`; `true` = legal this step.
27pub trait GrammarMasker {
28 fn mask(&self, recent: &[Token], vocab: usize) -> Vec<bool>;
29}
30
31/// Opt-in LAN relay (ADR-004 HybridMode). Implementations MUST stay on the local
32/// network — there is no cloud variant.
33pub trait HybridRelay {
34 fn consult(&self, query_tokens: &[Token]) -> Vec<Token>;
35}
36
37/// The collaborator ports bound to a session. `relay` is `None` by default —
38/// air-gapped.
39pub struct Ports {
40 pub compressor: Box<dyn PromptCompressor>,
41 pub grammar: Box<dyn GrammarMasker>,
42 pub safety: Box<dyn SafetySteerer>,
43 pub relay: Option<Box<dyn HybridRelay>>,
44}
45
46impl Ports {
47 /// Defaults: identity compression, all-tokens-allowed grammar, no safety
48 /// steering, no relay (air-gapped).
49 pub fn permissive() -> Self {
50 Self {
51 compressor: Box::new(super::defaults::IdentityCompressor),
52 grammar: Box::new(super::defaults::AllowAllMasker),
53 safety: Box::new(el_safety::NoSafety),
54 relay: None,
55 }
56 }
57}