Skip to content

Commit 3554db0

Browse files
committed
feat(knowledge): knowledge-turso docs, changeset and its error-code ledger row
Claude-Session: https://claude.ai/code/session_01JQwxbaMuehXzctoWq7iRsV Co-authored-by: Claude <noreply@anthropic.com>
1 parent 215a03f commit 3554db0

10 files changed

Lines changed: 624 additions & 15 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@objectstack/knowledge-turso': patch
3+
'@objectstack/spec': patch
4+
---
5+
6+
feat(knowledge): the libSQL vector store moves into the open framework, with generations, listing and the adapter contract suite
7+
8+
Clause-②: yes
9+
Metadata-protocol: none
10+
11+
ADR-0161 Execution row 7; cloud ADR-0119 D5 (#37107).
12+
13+
- **`@objectstack/knowledge-turso` now ships from this repository**, Apache-2.0 (its `LICENSE` file is the Apache-2.0 text). `KnowledgeTursoPlugin` registers a `TursoKnowledgeAdapter` (adapter id `turso`) that keeps passages and vectors in a libSQL database of its own with libSQL's native vectors: the `url` option (`file:`, `:memory:`, `libsql://`), else `knowledge.db` under the project's `.objectstack/data/` (`defaultStoreUrl`). A URL naming the platform's own database file (`objectstack.db`, `dev.db`, `standalone.db`, or the file `OS_DATABASE_URL` names) is refused: two engines writing one SQLite file lose rows. On a file the store sets WAL and a busy timeout (`busyTimeoutMs`, default 5000), and in one process every call of the store runs its database work one at a time.
14+
- **The platform embedder and splitter.** The package's own OpenAI embedding provider, hash embedder and chunker are gone: it embeds with the kernel's `embedder` service, read at each call (or the `embedder` option), reports `embedding` (model, dimensions, input limit), and declares `capabilities.chunks`, indexing the service's passages as given. Removed exports of the cloud-repo releases: `HashEmbedder`, `HashEmbeddingProvider`, `EmbeddingProvider`, `TursoAdapterOptions` and the default export; removed options: `embedding` (now `embedder`), `chunkTarget`, `overFetch`, and `adapterConfig.tableName` / `dimensions` on a source.
15+
- **Generations under the index fingerprint (ADR-0161 K7; K8, item 8).** A generation table `knowledge_generation` and one passage table per source and generation, `knowledge_<source>_g<n>`. A write or a search under another fingerprint is refused `409 KNOWLEDGE_REBUILD_REQUIRED` (registered in the error-code ledger for this package). `beginBuild` under a new fingerprint opens a generation beside the active one, which keeps answering its own fingerprint; `completeBuild` switches to it, records the recipe hash and drops the old table; `indexStatus` answers the fingerprint, the recipe hash and whether a build is under way. One builder per source, held by a unique index on the generation table. A table of the cloud-repo releases (`knowledge_<source>`) is never served: a search answers `KNOWLEDGE_REBUILD_REQUIRED`, and the table is dropped when the first complete reindex finishes.
16+
- **Listing, hashes, organizations.** `listDocuments` pages the generation the writes go to, each document's `docHash` as stored; hits carry `docHash` and the passage's `headingPath`; the organization filter and the metadata `filter` run in SQL before ranking, by an exact cosine scan (no DiskANN index, which ranks before it filters). A passage whose text is unchanged is not embedded again.
17+
- **When it cannot run**, because the database cannot be opened or `@libsql/client` has no native build for the platform (Windows on ARM), the plugin still registers and boot goes on; `healthCheck` answers `ok: false` and every call answers `503 SERVICE_UNAVAILABLE`, its reason in the error's `remedy`.
18+
- The package runs `runKnowledgeAdapterContract` with every member declared, on `:memory:` and on a `file:` URL, in its own unit tests.

‎.changeset/config.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"@objectstack/service-storage",
8080
"@objectstack/knowledge-memory",
8181
"@objectstack/knowledge-pgvector",
82+
"@objectstack/knowledge-turso",
8283
"@objectstack/knowledge-ragflow",
8384
"@objectstack/embedder-openai",
8485
"@objectstack/account",

‎content/docs/ai/knowledge-rag.mdx‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Part of the [AI module](/docs/ai) — how agents retrieve knowledge through the
99
ObjectStack ships a Knowledge Protocol that lets you retrieve from pluggable backends (RAGFlow, LlamaIndex, Dify, custom pgvector, …) with one call: `KnowledgeService.search(query, { sourceIds?, topK? })`. The framework defines the contract and runs permission-aware filtering; the adapter plugin does the actual retrieval. See [the protocol design](/docs/protocol/knowledge) for the rationale.
1010

1111
<Callout type="info">
12-
**This whole stack is open.** The `@objectstack/service-knowledge` service, the adapter plugins (`@objectstack/knowledge-memory`, `@objectstack/knowledge-ragflow`, `@objectstack/knowledge-pgvector`), the `@objectstack/embedder-openai` embedder, and the permission-aware retrieval + event sync below all ship in the **open-source framework**. The workflow is: declare your knowledge sources, pick an adapter, and call `search` — retrieval respects the same row-level security as any ObjectQL query. Only the in-product chat *runtime* that consumes retrieval ships in ObjectOS; see the callout under [Retrieving knowledge](#retrieving-knowledge).
12+
**This whole stack is open.** The `@objectstack/service-knowledge` service, the adapter plugins (`@objectstack/knowledge-memory`, `@objectstack/knowledge-ragflow`, `@objectstack/knowledge-pgvector`, `@objectstack/knowledge-turso`), the `@objectstack/embedder-openai` embedder, and the permission-aware retrieval + event sync below all ship in the **open-source framework**. The workflow is: declare your knowledge sources, pick an adapter, and call `search` — retrieval respects the same row-level security as any ObjectQL query. Only the in-product chat *runtime* that consumes retrieval ships in ObjectOS; see the callout under [Retrieving knowledge](#retrieving-knowledge).
1313
</Callout>
1414

1515
## Wiring
@@ -178,6 +178,20 @@ new KnowledgePgvectorPlugin({ queryPrefix: '' /* e.g. BGE's query instruction */
178178

179179
Details — the tables, an approximate index as an operator's step, rebuilds — are in [the protocol's pgvector section](/docs/protocol/knowledge#41-knowledge-pgvector--the-open-vector-store-on-postgresql).
180180

181+
## Local vector store: a libSQL file
182+
183+
`@objectstack/knowledge-turso` keeps passages and vectors in a libSQL file of its own — by default `knowledge.db` under the project's `.objectstack/data/`, never the platform database's file — so `os dev` on SQLite gets semantic search, Chinese text included, with nothing to install. Point a source at it with `adapter: 'turso'`:
184+
185+
```ts
186+
new KnowledgeTursoPlugin(), // or { url: 'file:/var/lib/app/knowledge.db' }, ':memory:', a libsql:// URL
187+
```
188+
189+
- It searches by an exact scan; the organization filter and the metadata `filter` run in SQL before ranking. 10,000 passages of 1,536 dimensions take about 82 MB and a search about 50 ms; larger sources belong on pgvector.
190+
- A changed model or split is rebuilt beside the old index, which keeps answering until the reindex completes.
191+
- Where libSQL has no native build (Windows on ARM), boot goes on and its sources answer `SERVICE_UNAVAILABLE`.
192+
193+
Details are in [the protocol's turso section](/docs/protocol/knowledge#42-knowledge-turso--the-open-vector-store-in-a-libsql-file).
194+
181195
## Why we did not build a vector DB
182196

183197
Mature OSS (RAGFlow, LlamaIndex, Dify) already nail embedding, hybrid retrieval, rerank, and OCR. ObjectStack's value is **the metadata-native source declaration, one structural split for every adapter, and the permission-aware filter on top of those engines** — not yet-another-RAG-stack. Customers pick the retrieval engine that matches their data; we make sure it talks to ObjectStack the same way every time.

‎content/docs/protocol/knowledge.mdx‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ delegates the rest to plugins.
8484
│ • Event sync (record.* → upsert) │ │ (reference impl, REST) │
8585
│ • Audit + metrics │ │ • knowledge-pgvector │
8686
│ │ │ (PostgreSQL + pgvector) │
87+
│ │ │ • knowledge-turso │
88+
│ │ │ (libSQL file, local) │
8789
│ Registers `knowledge` service │ │ (llamaindex / dify: planned)│
8890
└──────────────────────────────────────┘ └──────────────────────────────┘
8991
▲
@@ -371,7 +373,8 @@ keeps generations, the switch at the end of a build. `implements` declares the o
371373
checks the declaration against the adapter. `./testing` is a test-only,
372374
ESM-only entry that imports `vitest`; nothing in the runtime entry reaches it.
373375
`knowledge-memory` runs it as the reference implementation; `knowledge-pgvector`
374-
runs it against a live PostgreSQL in CI.
376+
runs it against a live PostgreSQL in CI; `knowledge-turso` runs it on libSQL
377+
`:memory:` and on a `file:` URL in its own unit tests.
375378

376379
### 3.5 The split
377380

@@ -482,6 +485,7 @@ import { KnowledgeServicePlugin } from '@objectstack/service-knowledge';
482485
import { KnowledgeMemoryPlugin } from '@objectstack/knowledge-memory';
483486
import { KnowledgeRagflowPlugin } from '@objectstack/knowledge-ragflow';
484487
import { KnowledgePgvectorPlugin } from '@objectstack/knowledge-pgvector';
488+
import { KnowledgeTursoPlugin } from '@objectstack/knowledge-turso';
485489

486490
defineStack({
487491
plugins: [
@@ -492,6 +496,7 @@ defineStack({
492496
apiKey: process.env.RAGFLOW_KEY!,
493497
}),
494498
new KnowledgePgvectorPlugin(), // self-hosted, PostgreSQL + pgvector
499+
new KnowledgeTursoPlugin(), // local / not on PostgreSQL, a libSQL file
495500
],
496501
});
497502
```
@@ -565,6 +570,53 @@ an exact scan.
565570
sensitive as the record it came from, and it is in every backup of the
566571
database.
567572

573+
### 4.2 `knowledge-turso` — the open vector store in a libSQL file
574+
575+
`@objectstack/knowledge-turso` keeps a source's passages and vectors in a
576+
[libSQL](https://github.com/tursodatabase/libsql) database of its own, with
577+
libSQL's native vectors (`F32_BLOB`, `vector32`, `vector_distance_cos`): the
578+
store for local development and for a deployment not on PostgreSQL. It takes
579+
the service's passages, embeds them with the kernel's `embedder` service (or
580+
the `embedder` it is given), and answers a search by an exact scan.
581+
582+
- **Which database.** The `url` option (`file:`, `:memory:` or a
583+
`libsql://` Turso URL), else `knowledge.db` under the project's
584+
`.objectstack/data/`, beside the platform database. Never the platform's
585+
own file: two engines writing one SQLite file in one process lose rows, so a
586+
URL naming `objectstack.db`, `dev.db`, `standalone.db` or the file
587+
`OS_DATABASE_URL` names is refused. On a file the store sets WAL and a busy
588+
timeout (`busyTimeoutMs`, default 5000); in one process every call of the
589+
store runs its database work one at a time.
590+
- **Tables.** `knowledge_generation`, one row per source and generation
591+
(fingerprint, recipe hash, width, model, active or being built; at most one
592+
of each per source), and one passage table per source and generation,
593+
`knowledge_<source>_g<n>`, its vector column sized for that generation. A
594+
source id that is not snake_case (at most 48 characters) is refused before
595+
any SQL.
596+
- **Search.** An exact scan by cosine distance; the organization filter and
597+
the metadata `filter` (JSON equality per key) run in the same `WHERE`,
598+
before ranking. The store creates no DiskANN index: `vector_top_k` ranks
599+
before it filters, and with one, 1,000 vectors of 1,536 dimensions grew a
600+
file from 8.2 MB to 329 MB and took 21.8 s to index. Without it the same
601+
1,000 passages take 8.3 MB and a search about 5 ms; 10,000 take 82 MB and
602+
about 50 ms. A larger source belongs on `knowledge-pgvector`.
603+
- **Rebuilds.** A changed fingerprint is refused (§3.6). A complete reindex
604+
under a new fingerprint builds a new generation table beside the active
605+
one, which keeps answering its own fingerprint, and switches at the end of
606+
the build; the old table is dropped. A build of a third fingerprint
607+
abandons one left unfinished. A table of the package's earlier releases
608+
(`knowledge_<source>`, no generation) is never served: a search answers
609+
`KNOWLEDGE_REBUILD_REQUIRED`, and the table is dropped when the first
610+
complete reindex finishes.
611+
- **Re-embedding.** A passage whose text is unchanged (its `chunk_hash`) is
612+
not embedded again within a generation.
613+
- **When it cannot run.** A database that cannot be opened, or a platform
614+
with no native libSQL build (there is none for Windows on ARM): the plugin
615+
still registers and boot goes on; it warns with the reason, `healthCheck`
616+
answers `ok: false`, and every call answers `SERVICE_UNAVAILABLE`.
617+
- **The text is stored.** Each row holds its passage's text, as sensitive as
618+
the record it came from, and in every copy of the file.
619+
568620
---
569621

570622
## 5. Permission-aware retrieval (the unique value-add)
@@ -867,6 +919,7 @@ up.
867919
| `knowledge-llamaindex` | ⏳ Phase 2 | |
868920
| `knowledge-dify` | ⏳ Phase 3 / community | |
869921
| `knowledge-pgvector` | ✅ | PostgreSQL + pgvector; exact scan, organization and metadata filter in SQL, index fingerprint (§4.1) |
922+
| `knowledge-turso` | ✅ | A libSQL file of its own; exact scan, organization and metadata filter in SQL, a generation built beside the active one (§4.2) |
870923

871924
### 7.1 Non-goals for Phase 1
872925

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# @objectstack/knowledge-turso
2+
3+
`IKnowledgeAdapter` on [libSQL](https://github.com/tursodatabase/libsql)'s native vectors — the open vector store for local development (`os dev`) and for deployments not on PostgreSQL. It takes the knowledge service's passages, embeds them with the kernel's `embedder` service, stores passages and vectors in a libSQL database of its own, and answers `search()` by an exact cosine scan.
4+
5+
```ts
6+
import { ObjectKernel } from '@objectstack/core';
7+
import { KnowledgeServicePlugin } from '@objectstack/service-knowledge';
8+
import { KnowledgeTursoPlugin } from '@objectstack/knowledge-turso';
9+
10+
const kernel = new ObjectKernel();
11+
kernel.use(new KnowledgeServicePlugin({
12+
sources: [{ id: 'task_notes', label: 'Notes', adapter: 'turso',
13+
source: { kind: 'object', object: 'task', contentFields: ['notes'] }}],
14+
}));
15+
kernel.use(new KnowledgeTursoPlugin()); // .objectstack/data/knowledge.db
16+
```
17+
18+
## Options
19+
20+
| Option | Default | |
21+
|:--|:--|:--|
22+
| `id` | `'turso'` | The adapter id a source names in `adapter`. |
23+
| `url` | `file:` + `<projectRoot>/.objectstack/data/knowledge.db` | A `file:` URL, `:memory:`, or a `libsql://` (Turso) URL. Never the platform's own database file. |
24+
| `authToken` | — | The token of a `libsql://` URL. |
25+
| `client` | — | A libSQL client to use as it is (not configured, not closed by the plugin). |
26+
| `busyTimeoutMs` | `5000` | How long a write waits for another process's lock on a `file:` store. |
27+
| `projectRoot` | `process.cwd()` | The directory the default file is under. |
28+
| `queryPrefix` | `''` | Text put before every query before it is embedded. Passages are embedded as they are. |
29+
| `embedder` | the kernel's `embedder` service | The embedder to call, read at each call. |
30+
31+
## Its own file, never the platform's
32+
33+
The store opens a libSQL file of its own. Two engines writing one SQLite file in one process lose rows (measured: a row lost silently under WAL, a failed commit in delete mode), so a URL that names the platform's database — `objectstack.db`, `dev.db`, `standalone.db`, or the file `OS_DATABASE_URL` names — is refused: the plugin registers, warns, and every call answers `SERVICE_UNAVAILABLE`.
34+
35+
On a file the store sets WAL and a busy timeout: without them a second process writing the same file lost most of its writes in measurement. Within one process every call of the store runs its database work one at a time.
36+
37+
## Tables
38+
39+
- `knowledge_generation` — one row per source and generation: the index fingerprint it was built under, the recipe hash, the vector width, the model, and whether it is active or being built. At most one of each per source (a unique index: one builder per source).
40+
- `knowledge_<source>_g<n>` — the passages of one generation of one source, with an `F32_BLOB(<width>)` vector column sized for that generation.
41+
42+
## Search
43+
44+
An exact scan by cosine distance (`vector_distance_cos`). The organization filter and the metadata `filter` (JSON equality per key) run in the same `WHERE`, before ranking — libSQL's DiskANN index (`vector_top_k`) ranks first and filters after, so the store creates none. Measured on Linux x64 with 1,536-wide vectors: 1,000 passages take 8.3 MB and a search about 5 ms; 10,000 passages take 82 MB and a search about 50 ms. (With a DiskANN index the same 1,000 vectors grew a file to 329 MB, and the index build took 21.8 s.) For larger sources, use `@objectstack/knowledge-pgvector`.
45+
46+
## Rebuilds
47+
48+
Every source's index is built under one index fingerprint (the embedding model and dimensions, the split, the content list — ADR-0161 K7). A write or a search under another fingerprint is refused `KNOWLEDGE_REBUILD_REQUIRED` (409) and never served from the stored vectors. A complete reindex under a new fingerprint builds a new generation table beside the active one (the old one keeps answering its own fingerprint meanwhile) and switches to it when the reindex completes; the old table is then dropped. A passage whose text is unchanged is not embedded again within a generation.
49+
50+
A table written by this package's earlier releases (`knowledge_<source>`, from the cloud repository) is never served: a search answers "reindex needed", and the table is dropped when the first complete reindex finishes.
51+
52+
## When it cannot run
53+
54+
When the database cannot be opened, or `@libsql/client` has no native build for the platform (there is none for Windows on ARM), the plugin still registers and boot goes on: it warns with the reason, `healthCheck` answers `ok: false`, and every call answers `SERVICE_UNAVAILABLE` (one sentence; the reason travels in the error's `remedy`, for the operator).
55+
56+
## What it stores
57+
58+
Each row holds its passage's text, title and metadata as well as its vector: the text is as sensitive as the record it came from, and it is in every copy of the file.
59+
60+
## Tests
61+
62+
`pnpm test` runs the adapter contract suite of `@objectstack/service-knowledge/testing` on `:memory:` and on a `file:` URL, and the acceptance cases. libSQL runs in the test process: no service, no image.
63+
64+
## License
65+
66+
Apache-2.0. See [LICENSING.md](../../../LICENSING.md).

0 commit comments

Comments
 (0)