lariv-rs 0.1.0

Compile-time plugin web application framework built on Axum, SeaORM, Maud, and HTMX
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Rune environment registry — plugins register bindings for assistant scripts.
//!
//! Mirrors [`crate::llm_tools::LlmToolsCapability`]: deferred registrar hooks at mount,
//! mounted as [`Arc<RuneEnvCapability>`] for cheap request-extension clones.
//!
//! Plugins expose static JSON values or request-scoped native functions that Rune scripts
//! can call during LLM Assistant tool runs.
//!
//! # Lifecycle
//!
//! 1. Attach via [`with_rune_env`].
//! 2. Plugins implement [`RuneEnvRegistrar`] to call [`RuneEnvCapability::register_static`]
//!    or [`RuneEnvCapability::register_contextual`].
//! 3. At mount, hooks fold → [`Arc<RuneEnvCapability>`] on the app HList.
//! 4. Tool handlers call [`RuneEnvCapability::resolve`] with [`RuneEnvCtx`] per invocation.
//!
//! # Core types
//!
//! - [`RuneEnvTag`] — capability tag
//! - [`RuneEnvCapability`] — mounted binding registry
//! - [`RuneEnvCtx`] — request-time context for contextual bindings
//! - [`NativeBinding`] / [`NativeFn`] — resolved callable or static value
//! - [`ResolvedRuneEnv`] — materialized bindings for one script run
//! - [`RuneEnvCap`] — builder-phase [`CapStore`]
//! - [`RuneEnvRegistrar`] — plugin hook trait
//!
//! # Examples
//!
//! ```rust ignore
//! impl RuneEnvRegistrar for RegisterRuneEnvHook {
//!     fn register_rune_env(self, env: &mut RuneEnvCapability) {
//!         env.register_static("app_version", "app_version: string constant", json!("1.0"));
//!         env.register_contextual(
//!             "current_user",
//!             "current_user() -> #{ id, name }",
//!             |ctx| NativeBinding::Value(json!(lookup_user(ctx.db))),
//!         );
//!     }
//! }
//!
//! let app = with_rune_env(app);
//! ```

use std::collections::HashMap;
use std::sync::Arc;

use frunk::{HCons, HNil, hlist::HList};
use rune::Value;
use sea_orm::DatabaseConnection;
use serde_json::{Value as JsonValue, json};

use crate::{
    app::App,
    capability::{ApplyHooks, CapStore, Capability, mount_with_hooks},
    plugins::filesystem::storage::DynFilestore,
    tag::Tagged,
    traits::add::{AddCapability, CapTagAbsent},
};

/// Capability tag for the Rune script environment registry.
pub struct RuneEnvTag;

/// Request-time context when resolving contextual bindings.
pub struct RuneEnvCtx<'a> {
    pub db: &'a DatabaseConnection,
    pub store: Arc<DynFilestore>,
}

pub type NativeFn =
    Arc<dyn for<'a> Fn(&RuneEnvCtx<'a>, &[Value]) -> Result<Value, String> + Send + Sync>;

/// Drive an async future from a sync Rune native function.
///
/// `block_in_place` only works on the multi-thread runtime. Deployments such as
/// Uniquity use a current-thread runtime (large-stack install/mount), so this
/// falls back to a dedicated thread with its own runtime.
pub fn block_on_async<T, F>(fut: F) -> T
where
    T: Send,
    F: std::future::Future<Output = T> + Send,
{
    match tokio::runtime::Handle::try_current() {
        Ok(handle) if handle.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread => {
            tokio::task::block_in_place(|| handle.block_on(fut))
        }
        Ok(_) | Err(_) => std::thread::scope(|scope| {
            scope
                .spawn(|| {
                    tokio::runtime::Builder::new_current_thread()
                        .enable_all()
                        .build()
                        .expect("rune native runtime")
                        .block_on(fut)
                })
                .join()
                .unwrap_or_else(|payload| std::panic::resume_unwind(payload))
        }),
    }
}

/// Resolved native binding (static JSON value or callable).
pub enum NativeBinding {
    Value(JsonValue),
    Function(NativeFn),
}

type ContextualFactory = Arc<dyn for<'a> Fn(&RuneEnvCtx<'a>) -> NativeBinding + Send + Sync>;

#[derive(Clone)]
enum StoredBindingKind {
    Static(JsonValue),
    Contextual(ContextualFactory),
}

#[derive(Clone)]
struct StoredBinding {
    kind: StoredBindingKind,
    /// Human-readable signature / docs for the skill Content field hint.
    doc: String,
}

/// Runtime registry of Rune environment entries.
#[derive(Clone, Default)]
pub struct RuneEnvCapability {
    bindings: Vec<(String, StoredBinding)>,
}

impl RuneEnvCapability {
    /// Empty binding registry (starting point for [`RuneEnvRegistrar`] hooks).
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a static JSON value (same for every request).
    ///
    /// `doc` is shown on the skill Content field hint when this binding is mounted.
    pub fn register_static(
        &mut self,
        name: impl Into<String>,
        doc: impl Into<String>,
        value: JsonValue,
    ) -> &mut Self {
        self.upsert(
            name.into(),
            StoredBinding {
                kind: StoredBindingKind::Static(value),
                doc: doc.into(),
            },
        );
        self
    }

    /// Register a request-scoped binding factory (evaluated at [`Self::resolve`] time).
    ///
    /// `doc` should describe the Rune call signature and return shape (used by the skill
    /// Content field hint for the bindings present in this deployment).
    pub fn register_contextual<F>(
        &mut self,
        name: impl Into<String>,
        doc: impl Into<String>,
        factory: F,
    ) -> &mut Self
    where
        F: for<'a> Fn(&RuneEnvCtx<'a>) -> NativeBinding + Send + Sync + 'static,
    {
        self.upsert(
            name.into(),
            StoredBinding {
                kind: StoredBindingKind::Contextual(Arc::new(factory)),
                doc: doc.into(),
            },
        );
        self
    }

    fn upsert(&mut self, name: String, binding: StoredBinding) {
        if let Some(existing) = self.bindings.iter_mut().find(|(n, _)| *n == name) {
            existing.1 = binding;
        } else {
            self.bindings.push((name, binding));
        }
    }

    /// All registered binding names (static and contextual).
    pub fn all_names(&self) -> Vec<String> {
        self.bindings.iter().map(|(n, _)| n.clone()).collect()
    }

    /// Documentation strings for registered bindings (registration order).
    ///
    /// Empty docs are omitted. Used to patch the skill Content field hint from mounted plugins.
    pub fn binding_docs(&self) -> Vec<&str> {
        self.bindings
            .iter()
            .map(|(_, b)| b.doc.as_str())
            .filter(|d| !d.is_empty())
            .collect()
    }

    /// Resolve static + contextual bindings for one tool invocation.
    pub fn resolve(&self, ctx: &RuneEnvCtx<'_>) -> ResolvedRuneEnv {
        let mut statics = Vec::new();
        let mut functions = Vec::new();
        for (name, binding) in &self.bindings {
            match &binding.kind {
                StoredBindingKind::Static(v) => statics.push((name.clone(), v.clone())),
                StoredBindingKind::Contextual(factory) => match factory(ctx) {
                    NativeBinding::Value(v) => statics.push((name.clone(), v)),
                    NativeBinding::Function(f) => functions.push((name.clone(), f)),
                },
            }
        }
        ResolvedRuneEnv { statics, functions }
    }
}

/// Bindings materialized for a single script run.
pub struct ResolvedRuneEnv {
    pub statics: Vec<(String, JsonValue)>,
    pub functions: Vec<(String, NativeFn)>,
}

/// Plugin hook for appending bindings onto a [`RuneEnvCapability`].
///
/// Must mutate in place (not chain by-value returns).
pub trait RuneEnvRegistrar {
    fn register_rune_env(self, rune_env: &mut RuneEnvCapability);
}

pub type RuneEnvCap<Hooks> = CapStore<RuneEnvTag, Hooks, RuneEnvCapability>;

impl<Hooks> RuneEnvCap<Hooks> {
    /// Eagerly fold registrar hooks into items (testing / pre-mount inspection).
    pub fn resolve_hooks<Proof>(self) -> RuneEnvCap<HNil>
    where
        Hooks: ApplyHooks<RuneEnvCapability, Proof, Output = RuneEnvCapability>,
    {
        CapStore::with_items(self.hooks.apply_hooks(self.items))
    }
}

impl<Plugin, H, Tail, TailProof> ApplyHooks<RuneEnvCapability, (TailProof, ())>
    for HCons<Tagged<Plugin, H>, Tail>
where
    Tail: ApplyHooks<RuneEnvCapability, TailProof, Output = RuneEnvCapability>,
    H: RuneEnvRegistrar,
{
    type Output = RuneEnvCapability;

    fn apply_hooks(self, items: RuneEnvCapability) -> Self::Output {
        let mut items = self.tail.apply_hooks(items);
        self.head.value.register_rune_env(&mut items);
        items
    }
}

impl<Hooks> Capability for RuneEnvCap<Hooks>
where
    Hooks: ApplyHooks<RuneEnvCapability, (), Output = RuneEnvCapability>,
{
    type Value = Arc<RuneEnvCapability>;
    type Output = Tagged<RuneEnvTag, Arc<RuneEnvCapability>>;
    type Hooks = Hooks;
    type Items = RuneEnvCapability;

    fn mount(self) -> Self::Output {
        mount_with_hooks(self, Arc::new)
    }
}

/// Attach an empty Rune environment capability to the app builder.
pub fn with_rune_env<L, Proof>(app: App<L>) -> App<HCons<RuneEnvCap<HNil>, L>>
where
    L: HList + CapTagAbsent<RuneEnvTag, Proof>,
{
    app.add_capability(CapStore::with_items(RuneEnvCapability::new()))
}

/// Convert a Rune VM value into JSON (objects, arrays, and scalars).
pub fn rune_to_json(v: &Value) -> Result<JsonValue, String> {
    rune_to_json_at(v, "$")
}

fn rune_to_json_at(v: &Value, path: &str) -> Result<JsonValue, String> {
    if v.into_unit().is_ok() {
        return Ok(JsonValue::Null);
    }
    if let Ok(b) = v.as_bool() {
        return Ok(json!(b));
    }
    if let Ok(n) = v.as_integer::<i64>() {
        return Ok(json!(n));
    }
    if let Ok(n) = v.as_unsigned() {
        return Ok(json!(n));
    }
    if let Ok(n) = v.as_float() {
        return Ok(json!(n));
    }
    if let Ok(c) = v.as_char() {
        return Ok(json!(c.to_string()));
    }
    if let Ok(s) = rune::from_value::<String>(v.clone()) {
        return Ok(json!(s));
    }
    if let Ok(vec) = v.borrow_ref::<rune::runtime::Vec>() {
        let arr: Result<Vec<_>, _> = vec
            .iter()
            .enumerate()
            .map(|(i, item)| rune_to_json_at(item, &format!("{path}[{i}]")))
            .collect();
        return Ok(JsonValue::Array(arr?));
    }
    if let Ok(obj) = v.borrow_ref::<rune::runtime::Object>() {
        let mut map = serde_json::Map::new();
        for (k, val) in obj.iter() {
            map.insert(k.to_string(), rune_to_json_at(val, &format!("{path}.{k}"))?);
        }
        return Ok(JsonValue::Object(map));
    }
    if let Ok(tuple) = v.borrow_tuple_ref() {
        if tuple.is_empty() {
            return Ok(JsonValue::Null);
        }
        let items: Result<Vec<_>, _> = tuple
            .iter()
            .enumerate()
            .map(|(i, item)| rune_to_json_at(item, &format!("{path}[{i}]")))
            .collect();
        return Ok(JsonValue::Array(items?));
    }
    Err(format!(
        "unsupported Rune type `{}` at {path}",
        v.type_info()
    ))
}

/// Convert JSON into a Rune VM value. Objects become `#{}` maps, not strings.
pub fn json_to_rune(value: JsonValue) -> Result<Value, String> {
    match value {
        JsonValue::Null => Ok(Value::from(())),
        JsonValue::Bool(b) => Ok(Value::from(b)),
        JsonValue::Number(n) => {
            if let Some(i) = n.as_i64() {
                Ok(Value::from(i))
            } else if let Some(u) = n.as_u64() {
                if let Ok(i) = i64::try_from(u) {
                    Ok(Value::from(i))
                } else if let Some(f) = n.as_f64() {
                    Ok(Value::from(f))
                } else {
                    Err("number out of range".into())
                }
            } else if let Some(f) = n.as_f64() {
                Ok(Value::from(f))
            } else {
                Err("unsupported number".into())
            }
        }
        JsonValue::String(s) => rune::to_value(s).map_err(|e| e.to_string()),
        JsonValue::Array(items) => {
            let vals: Result<Vec<_>, _> = items.into_iter().map(json_to_rune).collect();
            rune::to_value(vals?).map_err(|e| e.to_string())
        }
        JsonValue::Object(map) => {
            let mut out = HashMap::new();
            for (k, v) in map {
                out.insert(k, json_to_rune(v)?);
            }
            rune::to_value(out).map_err(|e| e.to_string())
        }
    }
}

/// Curated Rune standard-library module names (for `list_rune_env` tool output).
pub fn standard_library_names() -> &'static [&'static str] {
    &[
        "std::array",
        "std::bytes",
        "std::char",
        "std::clone",
        "std::cmp",
        "std::collections",
        "std::convert",
        "std::core",
        "std::env",
        "std::fmt",
        "std::fs",
        "std::future",
        "std::hash",
        "std::io",
        "std::iter",
        "std::math",
        "std::object",
        "std::ops",
        "std::option",
        "std::panic",
        "std::pin",
        "std::prelude",
        "std::rand",
        "std::result",
        "std::slice",
        "std::string",
        "std::tuple",
        "std::type",
        "std::vec",
    ]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn register_static_and_names() {
        let mut cap = RuneEnvCapability::new();
        cap.register_static("pi", "pi: float", JsonValue::from(3.14));
        cap.register_static("pi", "pi: float (updated)", JsonValue::from(3.14159));
        assert_eq!(cap.all_names(), vec!["pi".to_string()]);
        assert_eq!(cap.binding_docs(), vec!["pi: float (updated)"]);
    }

    #[tokio::test]
    async fn block_on_async_works_on_current_thread() {
        let n = block_on_async(async { 7i64 });
        assert_eq!(n, 7);
    }

    #[test]
    fn json_object_roundtrips_as_object_not_string() {
        let src = json!({
            "site": { "id": 1, "name": "Aayush Developer" },
            "purchase_orders": [{ "id": 8, "number": "P26RIN100294" }],
        });
        let rune = json_to_rune(src.clone()).expect("json_to_rune");
        let back = rune_to_json(&rune).expect("rune_to_json");
        assert_eq!(back, src);
        assert!(back.is_object());
    }

    #[test]
    fn unsigned_integer_converts_to_json() {
        let v = Value::from(7u64);
        assert_eq!(rune_to_json(&v).expect("unsigned"), json!(7));
    }
}