valise 0.1.4

Single-file retrieval capsule for local-first AI: text and vector search over one portable file
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Declarative collection schemas: [`Schema`] and its field specs
//! ([`Text`] / [`Vector`]), plus the per-field codec ([`Codec`]) and
//! calibration ([`Calibrate`]) choices.
//!
//! This is the surface described in `docs/SIMPLE_API_SPEC.md` §2. A
//! `Schema` is purely declarative — it is resolved against the store's
//! [`SchemaRegistry`](crate::db::space::SchemaRegistry) when bound via
//! [`crate::db::Store::collection`], where inline field specs auto-define
//! private `~auto/{collection}/{field}` spaces and shared bindings
//! ([`Text::space`] / [`Vector::space`]) reuse a space from
//! [`crate::db::Store::define_space`].
//!
//! [`Codec`] and [`Calibrate`] are deliberately **opaque, constructor-only**
//! types: adding a parameter to a codec family, or a new family, is then a
//! non-breaking change (new constructors instead of new public enum
//! variants).

use crate::codec::CodecParams;
use crate::db::space::{Lang, Metric, Space};

/// Default deferred-calibration sample size (vectors), per spec §2.
pub(crate) const DEFAULT_CALIBRATE_SAMPLE: usize = 50_000;

/// The set of named fields a collection's records may fill. Built fluently
/// and bound (with validation) by [`crate::db::Store::collection`].
///
/// ```no_run
/// use valise::prelude::*;
/// # fn demo(store: &Store) -> Result<()> {
/// store.collection("notes", Schema::new()
///     .text("body")
///     .vector("embedding", Vector::dim(768)))?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Default)]
pub struct Schema {
    pub(crate) fields: Vec<(String, FieldSpec)>,
}

impl Schema {
    #[must_use]
    pub fn new() -> Schema {
        Schema { fields: Vec::new() }
    }

    /// Add a BM25-indexed text field with the default analyzer
    /// ([`Text::english`]). At most one text field per collection in v1.
    #[must_use]
    pub fn text(self, name: &str) -> Schema {
        self.text_with(name, Text::english())
    }

    /// Add a text field with an explicit [`Text`] spec.
    #[must_use]
    pub fn text_with(mut self, name: &str, spec: Text) -> Schema {
        self.fields.push((name.to_owned(), FieldSpec::from(spec)));
        self
    }

    /// Add a vector field. The dimension is mandatory, so the spec is always
    /// explicit: `Vector::dim(768)` or `Vector::space(&shared)`.
    #[must_use]
    pub fn vector(mut self, name: &str, spec: Vector) -> Schema {
        self.fields.push((name.to_owned(), FieldSpec::from(spec)));
        self
    }
}

/// A field specification — either text or vector. Obtained via
/// `impl Into<FieldSpec>` from [`Text`] or [`Vector`]; also what
/// [`crate::db::Store::define_space`] accepts.
#[derive(Clone, Debug)]
pub struct FieldSpec {
    pub(crate) kind: FieldSpecKind,
}

#[derive(Clone, Debug)]
pub(crate) enum FieldSpecKind {
    Text(Text),
    Vector(Vector),
}

impl From<Text> for FieldSpec {
    fn from(spec: Text) -> Self {
        FieldSpec {
            kind: FieldSpecKind::Text(spec),
        }
    }
}

impl From<Vector> for FieldSpec {
    fn from(spec: Vector) -> Self {
        FieldSpec {
            kind: FieldSpecKind::Vector(spec),
        }
    }
}

/// Text field spec: an inline analyzer choice (auto-defines a private text
/// space) or a binding to a shared space from
/// [`crate::db::Store::define_space`].
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Text {
    pub(crate) binding: TextBinding,
}

#[derive(Clone, Debug)]
pub(crate) enum TextBinding {
    Inline { lang: Lang },
    Shared(Space),
}

impl Text {
    /// The English analyzer pipeline (NFKC, case-fold, Porter2 stemming,
    /// Lucene stopwords) — the default for [`Schema::text`].
    #[must_use]
    pub fn english() -> Text {
        Text {
            binding: TextBinding::Inline {
                lang: Lang::English,
            },
        }
    }

    /// A pass-through whitespace tokenizer (no normalization/stemming).
    #[must_use]
    pub fn raw() -> Text {
        Text {
            binding: TextBinding::Inline { lang: Lang::Raw },
        }
    }

    /// Bind to a shared text space previously created with
    /// [`crate::db::Store::define_space`].
    #[must_use]
    pub fn space(s: &Space) -> Text {
        Text {
            binding: TextBinding::Shared(s.clone()),
        }
    }
}

/// Vector field spec: an inline `(dim, metric, codec, calibrate)` choice
/// (auto-defines a private vector space) or a binding to a shared space.
///
/// On a shared binding ([`Vector::space`]) the configuration methods are
/// rejected at declaration time — metric/codec/calibration belong to the
/// space and are configured at [`crate::db::Store::define_space`].
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Vector {
    pub(crate) binding: VectorBinding,
    /// Set when a configuration method was called on a shared-space binding;
    /// surfaced as an error when the schema is declared.
    pub(crate) shared_misuse: Option<&'static str>,
}

#[derive(Clone, Debug)]
pub(crate) enum VectorBinding {
    Inline(VectorInline),
    Shared(Space),
}

/// The resolved inline vector-field configuration.
#[derive(Clone, Debug)]
pub(crate) struct VectorInline {
    pub dim: u32,
    pub metric: Metric,
    pub codec: Codec,
    pub calibrate: Calibrate,
}

impl Vector {
    /// A `dim`-dimensional cosine field with the default codec
    /// ([`Codec::qam`]) and auto-calibration ([`Calibrate::auto`]). `dim`
    /// must be a positive multiple of 64 (checked at declaration).
    #[must_use]
    pub fn dim(dim: u32) -> Vector {
        Vector {
            binding: VectorBinding::Inline(VectorInline {
                dim,
                metric: Metric::Cosine,
                codec: Codec::default(),
                calibrate: Calibrate::default(),
            }),
            shared_misuse: None,
        }
    }

    /// Bind to a shared vector space previously created with
    /// [`crate::db::Store::define_space`]. Configuration methods are not
    /// allowed on a shared binding.
    #[must_use]
    pub fn space(s: &Space) -> Vector {
        Vector {
            binding: VectorBinding::Shared(s.clone()),
            shared_misuse: None,
        }
    }

    /// Set the distance metric (default [`Metric::Cosine`]).
    #[must_use]
    pub fn metric(mut self, m: Metric) -> Vector {
        match &mut self.binding {
            VectorBinding::Inline(i) => i.metric = m,
            VectorBinding::Shared(_) => {
                self.shared_misuse.get_or_insert("metric");
            }
        }
        self
    }

    /// Set the quantization codec (default [`Codec::qam`]).
    #[must_use]
    pub fn codec(mut self, c: Codec) -> Vector {
        match &mut self.binding {
            VectorBinding::Inline(i) => i.codec = c,
            VectorBinding::Shared(_) => {
                self.shared_misuse.get_or_insert("codec");
            }
        }
        self
    }

    /// Set the calibration mode (default [`Calibrate::auto`]).
    #[must_use]
    pub fn calibrate(mut self, c: Calibrate) -> Vector {
        match &mut self.binding {
            VectorBinding::Inline(i) => i.calibrate = c,
            VectorBinding::Shared(_) => {
                self.shared_misuse.get_or_insert("calibrate");
            }
        }
        self
    }
}

/// Per-field vector codec choice. Opaque and constructor-only: families
/// gain parameters (and new families appear) as new constructors, never as
/// public enum variants — so codec evolution is non-breaking.
#[derive(Clone, Debug, PartialEq)]
pub struct Codec {
    pub(crate) spec: CodecSpec,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum CodecSpec {
    Qam { amp_bits: u8, phase_bits: u8 },
    Upq { cells: u32, design: UpqDesign },
    Prebuilt(CodecParams),
}

impl Codec {
    /// QAM Lloyd-Max at the production default `(5, 6)` bits — the
    /// [`Default`] codec.
    #[must_use]
    pub fn qam() -> Codec {
        Codec {
            spec: CodecSpec::Qam {
                amp_bits: crate::codec::qam_lloyd_max::DEFAULT_AMP_BITS,
                phase_bits: crate::codec::qam_lloyd_max::DEFAULT_PHASE_BITS,
            },
        }
    }

    /// QAM Lloyd-Max at an explicit `(amp_bits, phase_bits)` budget. Higher
    /// bits trade storage for recall.
    #[must_use]
    pub fn qam_bits(amp_bits: u8, phase_bits: u8) -> Codec {
        Codec {
            spec: CodecSpec::Qam {
                amp_bits,
                phase_bits,
            },
        }
    }

    /// UPQ (unrestricted polar quantization) at the default operating point
    /// (2048 cells, empirical ring design).
    #[must_use]
    pub fn upq() -> Codec {
        Codec::upq_with(crate::codec::upq::DEFAULT_UPQ_CELLS, UpqDesign::Empirical)
    }

    /// UPQ at an explicit cell budget (empirical ring design).
    #[must_use]
    pub fn upq_cells(cells: u32) -> Codec {
        Codec::upq_with(cells, UpqDesign::Empirical)
    }

    /// UPQ at an explicit cell budget and ring design.
    #[must_use]
    pub fn upq_with(cells: u32, design: UpqDesign) -> Codec {
        Codec {
            spec: CodecSpec::Upq { cells, design },
        }
    }

    /// Engine escape hatch: a prebuilt, family-tagged parameter set (e.g.
    /// from [`crate::ValiseFile::codec_params`]). Registered eagerly at
    /// declaration — no calibration sample is needed or used.
    #[must_use]
    pub fn from_params(params: CodecParams) -> Codec {
        Codec {
            spec: CodecSpec::Prebuilt(params),
        }
    }
}

impl Default for Codec {
    fn default() -> Self {
        Codec::qam()
    }
}

/// UPQ ring-design source. See `docs/FORMAT.md` §14 / `upq_params.rs`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum UpqDesign {
    /// Rings fitted on the calibration sample's empirical distribution
    /// (data-adaptive; the default).
    Empirical,
    /// Rings designed on the analytic unit-Rayleigh density.
    Rayleigh,
}

impl UpqDesign {
    pub(crate) fn to_engine(&self) -> crate::format::upq_params::UpqDesignSource {
        match self {
            UpqDesign::Empirical => crate::format::upq_params::UpqDesignSource::Empirical,
            UpqDesign::Rayleigh => crate::format::upq_params::UpqDesignSource::Rayleigh,
        }
    }
}

/// When and from what data a vector field's codec is calibrated. Opaque and
/// constructor-only for the same evolution reasons as [`Codec`].
#[derive(Clone, Debug)]
pub struct Calibrate {
    pub(crate) mode: CalibrateMode,
}

#[derive(Clone, Debug)]
pub(crate) enum CalibrateMode {
    Auto { sample: usize },
    Now(Vec<Vec<f32>>),
}

impl Calibrate {
    /// Fit the codec at the first commit that contains vectors for the
    /// field, from up to 50 000 staged vectors (the [`Default`]).
    #[must_use]
    pub fn auto() -> Calibrate {
        Calibrate::auto_sample(DEFAULT_CALIBRATE_SAMPLE)
    }

    /// [`Calibrate::auto`] with an explicit sample-size cap.
    #[must_use]
    pub fn auto_sample(sample: usize) -> Calibrate {
        Calibrate {
            mode: CalibrateMode::Auto { sample },
        }
    }

    /// Calibrate eagerly at declaration from a caller-supplied
    /// representative sample.
    #[must_use]
    pub fn now(sample: Vec<Vec<f32>>) -> Calibrate {
        Calibrate {
            mode: CalibrateMode::Now(sample),
        }
    }
}

impl Default for Calibrate {
    fn default() -> Self {
        Calibrate::auto()
    }
}

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

    #[test]
    fn codec_constructors_and_default() {
        // The default is the production QAM (5, 6).
        assert_eq!(Codec::default(), Codec::qam());
        assert_eq!(
            Codec::qam().spec,
            CodecSpec::Qam {
                amp_bits: 5,
                phase_bits: 6
            }
        );
        assert_eq!(
            Codec::qam_bits(8, 8).spec,
            CodecSpec::Qam {
                amp_bits: 8,
                phase_bits: 8
            }
        );
        // The UPQ default is 2048 cells, empirical design.
        assert_eq!(Codec::upq(), Codec::upq_with(2048, UpqDesign::Empirical));
        assert_eq!(
            Codec::upq_cells(512).spec,
            CodecSpec::Upq {
                cells: 512,
                design: UpqDesign::Empirical
            }
        );
        assert_eq!(
            Codec::upq_with(1024, UpqDesign::Rayleigh).spec,
            CodecSpec::Upq {
                cells: 1024,
                design: UpqDesign::Rayleigh
            }
        );
    }

    #[test]
    fn calibrate_default_is_auto_50k() {
        let c = Calibrate::default();
        assert!(matches!(c.mode, CalibrateMode::Auto { sample: 50_000 }));
        let c = Calibrate::auto_sample(8);
        assert!(matches!(c.mode, CalibrateMode::Auto { sample: 8 }));
        let c = Calibrate::now(vec![vec![0.0; 4]]);
        assert!(matches!(c.mode, CalibrateMode::Now(ref s) if s.len() == 1));
    }

    #[test]
    fn vector_records_shared_binding_misuse() {
        let s = Space::vector("emb", 64);
        // First misuse wins (named in the eventual declaration error).
        let v = Vector::space(&s)
            .codec(Codec::upq())
            .calibrate(Calibrate::auto());
        assert_eq!(v.shared_misuse, Some("codec"));
        let v = Vector::space(&s).metric(Metric::Dot);
        assert_eq!(v.shared_misuse, Some("metric"));
        // Inline bindings accept configuration.
        let v = Vector::dim(64).metric(Metric::Dot).codec(Codec::upq());
        assert!(v.shared_misuse.is_none());
    }

    #[test]
    fn schema_builder_collects_fields_in_order() {
        let s = Space::vector("emb", 64);
        let schema = Schema::new()
            .text("body")
            .vector("dense", Vector::dim(64))
            .vector("shared", Vector::space(&s));
        let names: Vec<&str> = schema.fields.iter().map(|(n, _)| n.as_str()).collect();
        assert_eq!(names, ["body", "dense", "shared"]);
        assert!(matches!(
            schema.fields[0].1.kind,
            FieldSpecKind::Text(Text {
                binding: TextBinding::Inline {
                    lang: Lang::English
                },
                ..
            })
        ));
    }
}