Skip to main content

harn_vm/stdlib/template/
llm_context.rs

1//! Ambient render-time LLM context exposed to `.harn.prompt` templates.
2//!
3//! When `render()` / `render_prompt()` / `render_string()` is invoked from
4//! within an LLM-aware frame (`llm_call`, the default handler stack, or
5//! `agent_loop`), the active provider/model/family/capabilities are
6//! published as the reserved `llm` scope key so authors can write
7//! capability-aware partials without manual plumbing:
8//!
9//! ```text
10//! {{ if llm.capabilities.native_tools }}
11//!   Call `finish_task` when done.
12//! {{ else }}
13//!   When done, output: `<<DONE>>`
14//! {{ end }}
15//! ```
16//!
17//! Bare `render()` calls outside any LLM frame leave `llm = nil`, so
18//! templates branch on `{{ if llm }}` for the doc-gen / CI paths.
19//!
20//! The context lives in a thread-local stack so concurrent agent_loop
21//! iterations on different threads stay isolated; nested
22//! push/pop pairs (e.g. an inner `llm_call` from a middleware handler)
23//! shadow the outer frame for the duration of the inner render.
24
25use crate::value::VmDictExt;
26use std::cell::RefCell;
27use std::collections::BTreeMap;
28
29use crate::value::VmValue;
30
31/// Resolved provider/model identity plus the corresponding capability
32/// snapshot, materialized at LLM-frame entry and injected as the `llm`
33/// binding during any `render()` call inside that frame.
34#[derive(Debug, Clone)]
35pub struct LlmRenderContext {
36    pub provider: String,
37    pub model: String,
38    pub family: String,
39    /// Snapshot of `provider_capabilities(provider, model)` — a
40    /// `VmValue::Dict` shaped exactly like the builtin's return value.
41    pub capabilities: VmValue,
42}
43
44impl LlmRenderContext {
45    /// Build a context from resolved provider/model strings, looking up
46    /// the capability snapshot and deriving the canonical model family.
47    pub fn resolve(provider: &str, model: &str) -> Self {
48        let caps = crate::llm::capabilities::lookup(provider, model);
49        let capabilities =
50            crate::llm::config_builtins::capabilities_to_vm_value(provider, model, &caps);
51        Self {
52            provider: provider.to_string(),
53            model: model.to_string(),
54            family: crate::llm_config::model_family(provider, model),
55            capabilities,
56        }
57    }
58
59    /// Materialize the context as the `llm` scope value:
60    /// `{provider, model, family, capabilities: <provider_capabilities dict>}`.
61    pub fn to_vm_value(&self) -> VmValue {
62        let mut dict = BTreeMap::new();
63        dict.put_str("provider", self.provider.as_str());
64        dict.put_str("model", self.model.as_str());
65        dict.put_str("family", self.family.as_str());
66        dict.insert("capabilities".to_string(), self.capabilities.clone());
67        VmValue::dict(dict)
68    }
69}
70
71thread_local! {
72    static LLM_RENDER_STACK: RefCell<Vec<LlmRenderContext>> = const { RefCell::new(Vec::new()) };
73}
74
75/// Push a frame onto the ambient render-context stack. Pair with
76/// `pop_llm_render_context` (or use `LlmRenderContextGuard`) so the
77/// stack stays balanced even on the unwind path.
78pub fn push_llm_render_context(ctx: LlmRenderContext) {
79    LLM_RENDER_STACK.with(|stack| stack.borrow_mut().push(ctx));
80}
81
82/// Pop the most recently pushed frame. Returns `None` (rather than
83/// panicking) if the stack was empty, since the host may legitimately
84/// unwind through a balanced push/pop sequence.
85pub fn pop_llm_render_context() -> Option<LlmRenderContext> {
86    LLM_RENDER_STACK.with(|stack| stack.borrow_mut().pop())
87}
88
89/// Return a clone of the active frame, or `None` if no LLM context is
90/// in scope. Render entry-points use this to decide whether to inject
91/// the `llm` binding.
92pub fn current_llm_render_context() -> Option<LlmRenderContext> {
93    LLM_RENDER_STACK.with(|stack| stack.borrow().last().cloned())
94}
95
96/// Per-task ambient-scope swap of the LLM render-context stack. See
97/// `orchestration::ambient_scope`: this frame is pushed for the duration of an
98/// `llm_call` (held across the model HTTP `.await`), so concurrent fan-out
99/// children each running their own `llm_call` would otherwise render templates
100/// with a sibling's provider/model/capabilities.
101pub(crate) fn swap_llm_render_stack(next: Vec<LlmRenderContext>) -> Vec<LlmRenderContext> {
102    LLM_RENDER_STACK.with(|stack| std::mem::replace(&mut *stack.borrow_mut(), next))
103}
104
105/// Reset the stack — wired into `reset_thread_local_state` so tests
106/// and serialized adapter sessions start clean.
107pub(crate) fn reset_llm_render_stack() {
108    LLM_RENDER_STACK.with(|stack| stack.borrow_mut().clear());
109}
110
111/// RAII guard that pushes a context on construction and pops on drop.
112/// Use this in Rust hosts (e.g. `llm_call_impl`) so the stack stays
113/// balanced across `?`-shortcircuits and panics.
114pub struct LlmRenderContextGuard {
115    /// Tagged so a misuse (drop-order inversion across nested guards)
116    /// surfaces as a `debug_assert` instead of silently popping the
117    /// wrong frame. Carries no runtime cost in release builds.
118    expected_depth: usize,
119}
120
121impl LlmRenderContextGuard {
122    pub fn enter(ctx: LlmRenderContext) -> Self {
123        push_llm_render_context(ctx);
124        let depth = LLM_RENDER_STACK.with(|stack| stack.borrow().len());
125        Self {
126            expected_depth: depth,
127        }
128    }
129}
130
131impl Drop for LlmRenderContextGuard {
132    fn drop(&mut self) {
133        let depth = LLM_RENDER_STACK.with(|stack| stack.borrow().len());
134        debug_assert_eq!(
135            depth, self.expected_depth,
136            "LlmRenderContextGuard nested-drop order violated",
137        );
138        pop_llm_render_context();
139    }
140}
141
142#[cfg(test)]
143mod tests {
144    use super::*;
145
146    fn derive_family(provider: &str, model: &str) -> String {
147        crate::llm_config::model_family(provider, model)
148    }
149
150    #[test]
151    fn family_from_model_id_takes_precedence() {
152        assert_eq!(
153            derive_family("openrouter", "anthropic/claude-3-5-sonnet"),
154            "anthropic-claude"
155        );
156        assert_eq!(derive_family("openrouter", "openai/gpt-4o"), "openai-gpt");
157        assert_eq!(
158            derive_family("openrouter", "google/gemini-1.5-pro"),
159            "google-gemini"
160        );
161        assert_eq!(derive_family("llamacpp", "qwen3.6-35b-a3b"), "qwen");
162    }
163
164    #[test]
165    fn family_falls_back_to_provider_alias() {
166        assert_eq!(
167            derive_family("anthropic", "unknown-future-model"),
168            "anthropic-claude"
169        );
170        assert_eq!(derive_family("azure", "deployment-xyz"), "openai-gpt");
171        assert_eq!(derive_family("vertex", "model-xyz"), "google-gemini");
172        assert_eq!(derive_family("local", "anonymous-snapshot"), "local");
173        assert_eq!(derive_family("", ""), "unknown");
174    }
175
176    #[test]
177    fn push_pop_stack_round_trip() {
178        reset_llm_render_stack();
179        assert!(current_llm_render_context().is_none());
180        push_llm_render_context(LlmRenderContext::resolve("anthropic", "claude-3-5-sonnet"));
181        assert_eq!(
182            current_llm_render_context().map(|c| c.family),
183            Some("anthropic-claude".to_string()),
184        );
185        push_llm_render_context(LlmRenderContext::resolve("openai", "gpt-4o"));
186        assert_eq!(
187            current_llm_render_context().map(|c| c.family),
188            Some("openai-gpt".to_string()),
189        );
190        pop_llm_render_context();
191        assert_eq!(
192            current_llm_render_context().map(|c| c.family),
193            Some("anthropic-claude".to_string()),
194        );
195        pop_llm_render_context();
196        assert!(current_llm_render_context().is_none());
197    }
198
199    #[test]
200    fn guard_pops_on_drop() {
201        reset_llm_render_stack();
202        {
203            let _guard = LlmRenderContextGuard::enter(LlmRenderContext::resolve(
204                "anthropic",
205                "claude-3-5-sonnet",
206            ));
207            assert!(current_llm_render_context().is_some());
208        }
209        assert!(current_llm_render_context().is_none());
210    }
211}