uni-plugin-extism 2.0.4

Extism (bytes-in/bytes-out WASM) loader for the uni-db plugin framework
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
//! Plugin-export readers — `manifest` and `register`.
//!
//! Every Extism plugin exposes two canonical-JSON control-surface exports:
//!
//! - **`manifest`** — returns the plugin's [`ExtismPluginManifest`]
//!   (id, version, declared capabilities, resource limits, …). Read once
//!   at load time to drive the capability intersection.
//! - **`register`** — returns a [`RegistrationManifest`] enumerating every
//!   qname the plugin provides plus its wire-level signature. Read after
//!   capability negotiation; one [`RegistrationEntry`] is converted to a
//!   `ScalarPluginFn` / `AggregatePluginFn` / `ProcedurePlugin` adapter
//!   downstream (M6a.1.5).
//!
//! This module splits parsing (pure, byte-slice in / value out) from the
//! Extism-call wrapper (`read_*_export`). The split lets us unit-test JSON
//! contracts without standing up a wasm plugin; the call-wrapper is
//! exercised end-to-end by the M6a.1.7 example plugin.

use serde::{Deserialize, Serialize};

use crate::error::ExtismError;
use crate::loader::ExtismPluginManifest;

/// Wire-level scalar / aggregate / procedure signature shipped by a
/// plugin's `register` export.
///
/// String-based for wire stability — plugins shouldn't have to encode
/// `arrow_schema::DataType` JSON. Translation to internal `FnSignature`
/// / `AggSignature` / `ProcedureSignature` happens at adapter
/// construction time (M6a.1.5 / M6a.2).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct WireFnSignature {
    /// Argument types in `WireArgType` form.
    pub args: Vec<WireArgType>,
    /// Return type.
    pub returns: WireArgType,
    /// Volatility — `"immutable"`, `"stable"`, or `"volatile"`. Default
    /// `"immutable"`.
    #[serde(default = "default_volatility")]
    pub volatility: String,
    /// Null handling — `"propagate"` (default) or `"user_handled"`.
    #[serde(default = "default_null_handling")]
    pub null_handling: String,
}

fn default_volatility() -> String {
    "immutable".to_owned()
}

fn default_null_handling() -> String {
    "propagate".to_owned()
}

/// Wire-level argument type shipped by a plugin.
///
/// Each variant maps to the corresponding `uni_plugin::traits::scalar::ArgType`
/// at adapter time. Primitive types use the lowercase Arrow names
/// (`"int64"`, `"float64"`, `"utf8"`, `"boolean"`, `"date64"`,
/// `"timestamp_ms"`, `"binary"`, `"largebinary"`).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum WireArgType {
    /// A native Arrow primitive — `kind: "primitive", arrow: "<name>"`.
    Primitive {
        /// Arrow primitive name.
        arrow: String,
    },
    /// A `CypherValue` shipped via `LargeBinary` opaque transport.
    CypherValue,
    /// A fixed-size vector — `kind: "vector", len: N, element: "<arrow>"`.
    Vector {
        /// Number of elements per row.
        len: usize,
        /// Element type.
        element: String,
    },
    /// Variadic — repeats `inner` zero or more times.
    Variadic {
        /// Inner element type.
        inner: Box<WireArgType>,
    },
}

/// One registration entry — a single qname plus its kind + signature.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum RegistrationEntry {
    /// A Cypher scalar function.
    Scalar {
        /// Fully-qualified name (`"ns.fn"`).
        qname: String,
        /// Signature.
        signature: WireFnSignature,
    },
    /// A Cypher aggregate function. Wire shape mirrors Scalar with the
    /// state type carried as a separate `WireArgType`.
    Aggregate {
        /// Fully-qualified name.
        qname: String,
        /// Per-row input + return types.
        signature: WireFnSignature,
        /// State type — opaque to the wire; Adapter side wraps as
        /// Arrow Binary.
        state: WireArgType,
    },
    /// A Cypher procedure.
    Procedure {
        /// Fully-qualified name.
        qname: String,
        /// Argument signatures.
        args: Vec<WireArgType>,
        /// Yielded column types, in declared order.
        yields: Vec<WireArgType>,
        /// Mode — `"read"`, `"write"`, `"schema"`, or `"dbms"`. Default `"read"`.
        #[serde(default = "default_proc_mode")]
        mode: String,
    },
}

fn default_proc_mode() -> String {
    "read".to_owned()
}

impl RegistrationEntry {
    /// Fully-qualified name of this entry.
    #[must_use]
    pub fn qname(&self) -> &str {
        match self {
            Self::Scalar { qname, .. }
            | Self::Aggregate { qname, .. }
            | Self::Procedure { qname, .. } => qname,
        }
    }
}

/// Top-level `register` export payload.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct RegistrationManifest {
    /// One entry per qname provided by the plugin.
    pub entries: Vec<RegistrationEntry>,
}

/// Parse the bytes returned by a plugin's `manifest` export into an
/// [`ExtismPluginManifest`].
///
/// # Errors
///
/// - [`ExtismError::ManifestInvalid`] if the JSON doesn't parse or
///   doesn't match the expected shape.
pub fn parse_manifest_json(bytes: &[u8]) -> Result<ExtismPluginManifest, ExtismError> {
    serde_json::from_slice(bytes)
        .map_err(|e| ExtismError::ManifestInvalid(format!("json parse: {e}")))
}

/// Parse the bytes returned by a plugin's `register` export into a
/// [`RegistrationManifest`].
///
/// # Errors
///
/// - [`ExtismError::OutputDecode`] if the JSON doesn't parse or doesn't
///   match the expected shape.
pub fn parse_registration_json(bytes: &[u8]) -> Result<RegistrationManifest, ExtismError> {
    serde_json::from_slice(bytes)
        .map_err(|e| ExtismError::OutputDecode(format!("register json parse: {e}")))
}

/// Call a live plugin's `manifest` export and parse the response.
///
/// The `manifest` export takes no input and returns canonical-JSON
/// matching [`ExtismPluginManifest`]. The plugin produces this once and
/// caches internally; the host reads it once at load and never again.
///
/// # Errors
///
/// - [`ExtismError::InvalidPlugin`] if the export doesn't exist or the
///   underlying Extism call fails.
/// - [`ExtismError::ManifestInvalid`] if the returned JSON is malformed.
pub fn read_manifest_export(
    plugin: &mut extism::Plugin,
) -> Result<ExtismPluginManifest, ExtismError> {
    if !plugin.function_exists("manifest") {
        return Err(ExtismError::InvalidPlugin(
            "plugin does not export required `manifest` function".to_owned(),
        ));
    }
    let bytes: &[u8] = plugin
        .call("manifest", "")
        .map_err(|e| ExtismError::InvalidPlugin(format!("call manifest: {e}")))?;
    parse_manifest_json(bytes)
}

/// Call a live plugin's `register` export and parse the response.
///
/// The `register` export takes no input and returns canonical-JSON
/// matching [`RegistrationManifest`]. The host reads this after
/// capability negotiation and converts each entry into an adapter
/// implementing the corresponding capability trait.
///
/// # Errors
///
/// - [`ExtismError::InvalidPlugin`] if the export doesn't exist or the
///   underlying Extism call fails.
/// - [`ExtismError::OutputDecode`] if the returned JSON is malformed.
pub fn read_register_export(
    plugin: &mut extism::Plugin,
) -> Result<RegistrationManifest, ExtismError> {
    if !plugin.function_exists("register") {
        return Err(ExtismError::InvalidPlugin(
            "plugin does not export required `register` function".to_owned(),
        ));
    }
    let bytes: &[u8] = plugin
        .call("register", "")
        .map_err(|e| ExtismError::InvalidPlugin(format!("call register: {e}")))?;
    parse_registration_json(bytes)
}

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

    #[test]
    fn parses_minimal_manifest() {
        let json = br#"{"id":"a.b","version":"0.0.1"}"#;
        let m = parse_manifest_json(json).unwrap();
        assert_eq!(m.id, "a.b");
        assert_eq!(m.version, "0.0.1");
        assert!(m.capabilities.is_empty());
        assert!(m.fuel_per_call.is_none());
    }

    #[test]
    fn parses_manifest_with_resource_limits() {
        let json = br#"{
            "id": "a.b",
            "version": "0.0.1",
            "capabilities": ["filesystem"],
            "fuel_per_call": 1000,
            "memory_max_pages": 4,
            "timeout_ms": 500
        }"#;
        let m = parse_manifest_json(json).unwrap();
        assert_eq!(m.fuel_per_call, Some(1000));
        assert_eq!(m.memory_max_pages, Some(4));
        assert_eq!(m.timeout_ms, Some(500));
        assert!(m.declared_capability_set().contains_variant(
            &uni_plugin::Capability::Filesystem {
                read: vec![],
                write: vec![],
            }
        ));
    }

    #[test]
    fn rejects_unknown_manifest_field() {
        let json = br#"{"id":"a.b","version":"0.0.1","mystery":"surprise"}"#;
        let err = parse_manifest_json(json).unwrap_err();
        assert!(matches!(err, ExtismError::ManifestInvalid(_)));
    }

    #[test]
    fn parses_empty_registration() {
        let json = br#"{"entries":[]}"#;
        let r = parse_registration_json(json).unwrap();
        assert!(r.entries.is_empty());
    }

    #[test]
    fn parses_scalar_registration_entry() {
        let json = br#"{
            "entries": [{
                "kind": "scalar",
                "qname": "geo.haversine",
                "signature": {
                    "args": [
                        {"kind":"primitive","arrow":"float64"},
                        {"kind":"primitive","arrow":"float64"},
                        {"kind":"primitive","arrow":"float64"},
                        {"kind":"primitive","arrow":"float64"}
                    ],
                    "returns": {"kind":"primitive","arrow":"float64"}
                }
            }]
        }"#;
        let r = parse_registration_json(json).unwrap();
        assert_eq!(r.entries.len(), 1);
        match &r.entries[0] {
            RegistrationEntry::Scalar { qname, signature } => {
                assert_eq!(qname, "geo.haversine");
                assert_eq!(signature.args.len(), 4);
                assert_eq!(signature.volatility, "immutable");
                assert_eq!(signature.null_handling, "propagate");
                assert!(matches!(
                    signature.returns,
                    WireArgType::Primitive { ref arrow } if arrow == "float64"
                ));
            }
            other => panic!("expected Scalar, got: {other:?}"),
        }
    }

    #[test]
    fn parses_aggregate_registration_entry() {
        let json = br#"{
            "entries": [{
                "kind": "aggregate",
                "qname": "stats.weighted_mean",
                "signature": {
                    "args": [
                        {"kind":"primitive","arrow":"float64"},
                        {"kind":"primitive","arrow":"float64"}
                    ],
                    "returns": {"kind":"primitive","arrow":"float64"},
                    "volatility": "stable"
                },
                "state": {"kind":"primitive","arrow":"binary"}
            }]
        }"#;
        let r = parse_registration_json(json).unwrap();
        match &r.entries[0] {
            RegistrationEntry::Aggregate {
                qname,
                signature,
                state,
            } => {
                assert_eq!(qname, "stats.weighted_mean");
                assert_eq!(signature.volatility, "stable");
                assert!(matches!(state, WireArgType::Primitive { arrow } if arrow == "binary"));
            }
            other => panic!("expected Aggregate, got: {other:?}"),
        }
    }

    #[test]
    fn parses_procedure_registration_entry() {
        let json = br#"{
            "entries": [{
                "kind": "procedure",
                "qname": "myorg.scan",
                "args": [{"kind":"primitive","arrow":"utf8"}],
                "yields": [
                    {"kind":"primitive","arrow":"int64"},
                    {"kind":"cypher_value"}
                ],
                "mode": "write"
            }]
        }"#;
        let r = parse_registration_json(json).unwrap();
        match &r.entries[0] {
            RegistrationEntry::Procedure {
                qname,
                args,
                yields,
                mode,
            } => {
                assert_eq!(qname, "myorg.scan");
                assert_eq!(args.len(), 1);
                assert_eq!(yields.len(), 2);
                assert_eq!(mode, "write");
                assert!(matches!(yields[1], WireArgType::CypherValue));
            }
            other => panic!("expected Procedure, got: {other:?}"),
        }
    }

    #[test]
    fn procedure_mode_defaults_to_read() {
        let json = br#"{
            "entries": [{
                "kind": "procedure",
                "qname": "myorg.scan",
                "args": [],
                "yields": []
            }]
        }"#;
        let r = parse_registration_json(json).unwrap();
        match &r.entries[0] {
            RegistrationEntry::Procedure { mode, .. } => assert_eq!(mode, "read"),
            _ => unreachable!(),
        }
    }

    #[test]
    fn registration_entry_exposes_qname() {
        let e = RegistrationEntry::Scalar {
            qname: "x.y".to_owned(),
            signature: WireFnSignature {
                args: vec![],
                returns: WireArgType::CypherValue,
                volatility: "immutable".to_owned(),
                null_handling: "propagate".to_owned(),
            },
        };
        assert_eq!(e.qname(), "x.y");
    }

    #[test]
    fn rejects_unknown_registration_kind() {
        let json = br#"{"entries":[{"kind":"telegraphic","qname":"x"}]}"#;
        let err = parse_registration_json(json).unwrap_err();
        assert!(matches!(err, ExtismError::OutputDecode(_)));
    }

    #[test]
    fn parses_vector_and_variadic_argtypes() {
        let json = br#"{
            "entries": [{
                "kind": "scalar",
                "qname": "vec.norm",
                "signature": {
                    "args": [
                        {"kind":"vector","len":128,"element":"float32"},
                        {"kind":"variadic","inner":{"kind":"primitive","arrow":"int64"}}
                    ],
                    "returns": {"kind":"primitive","arrow":"float32"}
                }
            }]
        }"#;
        let r = parse_registration_json(json).unwrap();
        match &r.entries[0] {
            RegistrationEntry::Scalar { signature, .. } => {
                assert!(matches!(
                    signature.args[0],
                    WireArgType::Vector { len: 128, ref element } if element == "float32"
                ));
                assert!(matches!(
                    signature.args[1],
                    WireArgType::Variadic { ref inner } if matches!(
                        **inner,
                        WireArgType::Primitive { ref arrow } if arrow == "int64"
                    )
                ));
            }
            _ => unreachable!(),
        }
    }
}