Documentation
¶
Overview ¶
Package vector is the chassis-owned vector store: a durable, tenant-scoped place to upsert embeddings, find nearest neighbours and read items back, exposed to txcl as txco://vector/{collection,upsert,search,get,update,delete}.
**Separation of concerns.** This package stores and searches vectors and nothing else — it never talks to an embedding provider. Producing the vectors is ai://embed's job (chassis/embed). The split mirrors the design: embedding belongs to AI; vector storage/retrieval belongs to infrastructure.
**Durable and bounded, not in-RAM.** A platform store must not hold tenants' embeddings in process memory — a restart would reload everything and a large upload would OOM the node. Vectors live on disk in a dedicated store (the bundled backend is SQLite + sqlite-vec; another backend slots in behind this same interface). A search is a local index read, the store doing its job — like txco://kv hitting BoltDB.
**v1 is exact, not approximate.** The bundled backend brute-force-scans with sqlite-vec's distance functions and pushes metadata filters into the SQL WHERE before ranking. No ANN, no recall tuning. Fine into the ~100k-vector range; the crossover to ANN is scale, not catalog size.
Index ¶
- Constants
- func Register(name string, c Constructor)
- type CodedError
- type Collection
- type CollectionConflictError
- type CollectionNotFoundError
- type Condition
- type Config
- type Constructor
- type DimensionMismatchError
- type Filter
- type GetResult
- type InvalidArgError
- type Item
- type Match
- type Metric
- type Op
- type Record
- type Selector
- type Store
Constants ¶
const ( // MaxGetIDs is the most ids one Get may name. MaxGetIDs = 500 // DefaultGetLimit is a filter Get's limit when none is given. DefaultGetLimit = 50 // MaxGetLimit is the largest limit a filter Get may ask for. MaxGetLimit = 500 // MaxGetTextBytes bounds the text of one answer: past it the answer stops // at a whole item and says More. An item is never cut, and the first item // always comes back, so a single item larger than the bound comes back // alone. 1 MiB stays far under the op payload ceiling. MaxGetTextBytes = 1 << 20 )
The bounds on one Get (txco://vector/get). The op handler refuses a request past them with the same numbers; the backends enforce them too.
Variables ¶
This section is empty.
Functions ¶
func Register ¶ added in v0.2.18
func Register(name string, c Constructor)
Register adds a backend constructor. Called from a backend package's init().
Types ¶
type CodedError ¶
CodedError carries a stable txco_vector_* code surfaced on the op envelope's `vector.error.code` so rule authors dispatch uniformly with `WHEN @vector.error EXEC ...`.
type Collection ¶
type Collection struct {
Name string `json:"name"`
EmbeddingModel string `json:"embedding_model"`
Dimensions int `json:"dimensions"`
Metric Metric `json:"metric"`
}
Collection pins a vector space. Vectors are only comparable within one (embedding model, dimensions, metric); the store rejects upserts that don't match, so a model swap is a new collection + re-embed, never silent space mixing.
type CollectionConflictError ¶
type CollectionConflictError struct {
Collection string
Field string // "dimensions" | "embedding_model" | "metric"
Existing string
Requested string
}
CollectionConflictError is returned by EnsureCollection when the requested pin differs from the existing one (a model/dimension/metric change is a new collection + re-embed, never an in-place mutation).
func (*CollectionConflictError) Code ¶
func (e *CollectionConflictError) Code() string
func (*CollectionConflictError) Error ¶
func (e *CollectionConflictError) Error() string
type CollectionNotFoundError ¶
CollectionNotFoundError is returned by Upsert/Search/Delete for an unknown (tenant, collection).
func (*CollectionNotFoundError) Code ¶
func (e *CollectionNotFoundError) Code() string
func (*CollectionNotFoundError) Error ¶
func (e *CollectionNotFoundError) Error() string
type Condition ¶
Condition is one metadata predicate. Value is a scalar for eq/gte/lte/gt/lt and a slice for in/not_in.
type Config ¶ added in v0.2.18
type Config struct {
// DBPath is the bundled SQLite backend's file path (--vector-db-path).
DBPath string
}
Config carries backend-selecting options resolved from chassis config. A backend reads only the fields it needs (the bundled SQLite backend uses DBPath). Adding a field here doesn't affect existing backends (same posture as cron.Config).
type Constructor ¶ added in v0.2.18
Constructor builds a Store from resolved config.
type DimensionMismatchError ¶
DimensionMismatchError is returned when a vector's length differs from the collection's pinned dimension.
func (*DimensionMismatchError) Code ¶
func (e *DimensionMismatchError) Code() string
func (*DimensionMismatchError) Error ¶
func (e *DimensionMismatchError) Error() string
type Filter ¶
type Filter struct {
Conditions []Condition
}
Filter is a conjunction (AND) of conditions applied to item metadata, pushed into the store's WHERE clause before nearest-neighbour ranking — so a tight filter both narrows results and avoids the "ANN returns 10, filter leaves 2" recall trap. An empty Filter matches everything.
type GetResult ¶ added in v0.2.60
type GetResult struct {
Items []Record `json:"items"`
Missing []string `json:"missing,omitempty"`
More bool `json:"more"`
}
GetResult is what a Get answers. Items are in the order the IDs were asked (an absent id is skipped and listed in Missing), or in ascending id order for a Filter. More reports that the answer stopped short: the filter admits items past the limit, or the bound on the answer's text was reached.
func ResultByFilter ¶ added in v0.2.60
ResultByFilter assembles a get by filter from rows in ascending id order. The backend reads limit+1 rows, so a row past the limit means the filter admits more; the answer also stops at the text bound.
func ResultByIDs ¶ added in v0.2.60
ResultByIDs assembles a get by ids: the items in the order asked, an id the collection does not hold listed in Missing, and the answer stopped at the text bound. asked is NormalizeGet's ids; found is what the backend read, by id.
type InvalidArgError ¶
type InvalidArgError struct {
Reason string
}
InvalidArgError flags a malformed request (empty collection name, non-positive dimension, unsupported metric, empty id).
func (*InvalidArgError) Code ¶
func (e *InvalidArgError) Code() string
func (*InvalidArgError) Error ¶
func (e *InvalidArgError) Error() string
type Item ¶
type Item struct {
ID string `json:"id"`
Vector []float32 `json:"vector"`
Metadata map[string]any `json:"metadata,omitempty"`
Text string `json:"text,omitempty"`
}
Item is one vector to store. ID is unique within (tenant, collection); re-upserting an ID replaces it. Metadata is the filterable sidecar; Text is optional and returned on search so callers avoid a second lookup.
type Match ¶
type Match struct {
ID string `json:"id"`
Distance float64 `json:"distance"`
Score float64 `json:"score"`
Metadata map[string]any `json:"metadata,omitempty"`
Text string `json:"text,omitempty"`
}
Match is one search hit. Distance is the raw metric distance (cosine distance ∈ [0,2]); Score is the normalised similarity 1−distance (cosine similarity ∈ [-1,1], higher = closer) so rerankers get a clean signal.
type Metric ¶
type Metric string
Metric is the distance metric a collection is compared under. v1 supports cosine only; the field exists so a collection pins it and a future backend can honour l2/ip without a schema change.
const (
MetricCosine Metric = "cosine"
)
type Record ¶ added in v0.2.60
type Record struct {
ID string `json:"id"`
Metadata map[string]any `json:"metadata,omitempty"`
Text string `json:"text,omitempty"`
}
Record is a stored item as Get reads it back: its id, metadata and text, and never its vector (a reader of items has no use for 1,536 floats, and they would swamp the envelope and the trace).
type Selector ¶ added in v0.2.60
Selector names the items a Get reads: by IDs or by Filter, exactly one of the two.
func NormalizeGet ¶ added in v0.2.60
NormalizeGet checks a Get's selector and paging, and returns the selector with duplicate ids removed (first occurrence kept) and the limit a filter Get should use. A backend calls it first and queries with what it returns.
type Store ¶
type Store interface {
// EnsureCollection creates the collection if absent (pinning model, dims,
// metric) and is a no-op if it already exists with a compatible pin;
// a conflicting pin (different dims/model/metric) returns CollectionConflictError.
EnsureCollection(ctx context.Context, tenant string, c Collection) error
// DescribeCollection returns the pinned collection. found is false (nil
// error) when it doesn't exist.
DescribeCollection(ctx context.Context, tenant, name string) (c Collection, found bool, err error)
// Upsert inserts or replaces items by ID. Every vector's length must equal
// the collection's pinned dimension (else DimensionMismatchError). Returns
// the number of items written.
Upsert(ctx context.Context, tenant, collection string, items []Item) (int, error)
// Search returns up to limit nearest matches to query under the
// collection's metric, restricted to items satisfying filter. query's
// length must equal the pinned dimension.
Search(ctx context.Context, tenant, collection string, query []float32, limit int, filter Filter) ([]Match, error)
// Delete removes items by ID. Returns the number removed.
Delete(ctx context.Context, tenant, collection string, ids []string) (int, error)
// UpdateMetadata merges set into the metadata of every item matching
// filter, leaving vectors and text untouched (a re-label, never a
// re-embed). The merge is shallow, RFC 7396 style: each key in set
// replaces the item's key; a nil value removes the key. filter must carry
// at least one condition — an unconditional rewrite is refused with
// InvalidArgError. Returns the number of items matched.
UpdateMetadata(ctx context.Context, tenant, collection string, filter Filter, set map[string]any) (int, error)
// Get reads the items sel names, without ranking and without a query
// vector. By IDs (at most MaxGetIDs, none empty, a duplicate read once):
// in the order asked, an absent id listed in Missing. By Filter (at least
// one condition, the search grammar): ascending id, only ids after
// `after` when it is set, at most limit (≤ 0 means DefaultGetLimit; more
// than MaxGetLimit is refused). Either way the answer stops at a whole
// item once its text would pass MaxGetTextBytes, and says More. Returns
// CollectionNotFoundError if the collection doesn't exist, and
// InvalidArgError for a selector that names both or neither.
Get(ctx context.Context, tenant, collection string, sel Selector, after string, limit int) (GetResult, error)
// ListIDs returns the IDs of every item currently in the collection (order
// unspecified). The store-seed reconciler uses it to compute which managed
// items a re-applied pack dropped (a seeded collection is owned by its pack,
// so reconcile is a desired-state sync: store − pack → delete). Returns
// CollectionNotFoundError if the collection doesn't exist.
ListIDs(ctx context.Context, tenant, collection string) ([]string, error)
// ListCollections returns the tenant's collections (pins only, no items),
// sorted by name — the inspect surface (`txco vector ls`). Not a hot path.
ListCollections(ctx context.Context, tenant string) ([]Collection, error)
// DropCollection removes a collection and all its items, returning the
// number of items removed. A missing collection is not an error (returns 0).
// This is the explicit whole-collection teardown the store-seed reconciler
// deliberately does NOT perform automatically (removing a pack file stops
// managing a collection but doesn't destroy it).
DropCollection(ctx context.Context, tenant, name string) (int, error)
// reconciler runs once on the origin) versus per-node (reconciled on every
// node). The bundled SQLite backend is per-node (returns false).
Shared() bool
// Close releases the underlying handle.
Close() error
}
Store is the backend-agnostic vector store interface. Implementations must be safe for concurrent use. tenant scopes every call (collections are namespaced by (tenant, name)); the op layer pins it from TenantScope, never from the envelope.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package sqlitevec is the bundled vector.Store backend: SQLite + sqlite-vec, in a dedicated database file (never the config-reloaded runtime DB).
|
Package sqlitevec is the bundled vector.Store backend: SQLite + sqlite-vec, in a dedicated database file (never the config-reloaded runtime DB). |
|
Package vectortest holds the backend-agnostic conformance suite for vector.Store implementations.
|
Package vectortest holds the backend-agnostic conformance suite for vector.Store implementations. |