Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .changeset/37107-knowledge-turso.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@objectstack/knowledge-turso': patch
'@objectstack/spec': patch
'@objectstack/service-settings': patch
---

feat(knowledge): the libSQL vector store moves into the open framework, with generations, listing and the adapter contract suite

Clause-②: yes
Metadata-protocol: none

ADR-0161 Execution row 7; cloud ADR-0119 D5 (#37107).

- **`@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. On a remote database (`libsql://`) each request is bounded (`requestTimeoutMs`, default 10000; a write sends one request per document): past it the call answers `SERVICE_UNAVAILABLE`, a write stopped before its commit is rolled back, and the queue moves on.
- **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.
- **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. The unique index keeps one generation under construction per source; it does not refuse a second builder: a second `beginBuild` under the same fingerprint joins the build, and one under another fingerprint replaces it, after which the first builder's writes are refused `KNOWLEDGE_REBUILD_REQUIRED`. Either way no generation mixes two fingerprints. A table of the cloud-repo releases (`knowledge_<source>` with exactly their columns) is never served: a search answers `KNOWLEDGE_REBUILD_REQUIRED`, and the table is dropped when the first complete reindex finishes.
- **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, the record scope (`scope`, ADR-0161 K8 item 17) 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.
- **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`.
- **Settings → Knowledge, "Test connection" on Turso** no longer says the package ships from another repository, or that a store with no URL reuses the platform's database: it names `@objectstack/knowledge-turso`, registered beside the knowledge service, and its default file.
- The package runs `runKnowledgeAdapterContract` with every member declared (`scope` included), on `:memory:` and on a `file:` URL, in its own unit tests.
1 change: 1 addition & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@objectstack/service-storage",
"@objectstack/knowledge-memory",
"@objectstack/knowledge-pgvector",
"@objectstack/knowledge-turso",
"@objectstack/knowledge-ragflow",
"@objectstack/embedder-openai",
"@objectstack/account",
Expand Down
16 changes: 15 additions & 1 deletion content/docs/ai/knowledge-rag.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Part of the [AI module](/docs/ai) — how agents retrieve knowledge through the
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.

<Callout type="info">
**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).
**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).
</Callout>

## Wiring
Expand Down Expand Up @@ -212,6 +212,20 @@ new KnowledgePgvectorPlugin({ queryPrefix: '' /* e.g. BGE's query instruction */

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).

## Local vector store: a libSQL file

`@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 a deployment on SQLite gets semantic search, Chinese text included, with no database server to run. Until `os dev` mounts it for you, add the package and register `KnowledgeTursoPlugin` beside the knowledge service yourself. Point a source at it with `adapter: 'turso'`:

```ts
new KnowledgeTursoPlugin(), // or { url: 'file:/var/lib/app/knowledge.db' }, ':memory:', a libsql:// URL
```

- It searches by an exact scan; the organization filter, the record scope 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.
- A changed model or split is rebuilt beside the old index, which keeps answering until the reindex completes.
- Where libSQL has no native build (Windows on ARM), boot goes on and its sources answer `SERVICE_UNAVAILABLE`.

Details are in [the protocol's turso section](/docs/protocol/knowledge#42-knowledge-turso--the-open-vector-store-in-a-libsql-file).

## Why we did not build a vector DB

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.
Expand Down
58 changes: 57 additions & 1 deletion content/docs/protocol/knowledge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ delegates the rest to plugins.
│ • Event sync (record.* → upsert) │ │ (reference impl, REST) │
│ • Audit + metrics │ │ • knowledge-pgvector │
│ │ │ (PostgreSQL + pgvector) │
│ │ │ • knowledge-turso │
│ │ │ (libSQL file, local) │
│ Registers `knowledge` service │ │ (llamaindex / dify: planned)│
└──────────────────────────────────────┘ └──────────────────────────────┘
▲
Expand Down Expand Up @@ -374,7 +376,8 @@ keeps generations, the switch at the end of a build. `implements` declares the o
checks the declaration against the adapter. `./testing` is a test-only,
ESM-only entry that imports `vitest`; nothing in the runtime entry reaches it.
`knowledge-memory` runs it as the reference implementation; `knowledge-pgvector`
runs it against a live PostgreSQL in CI.
runs it against a live PostgreSQL in CI; `knowledge-turso` runs it on libSQL
`:memory:` and on a `file:` URL in its own unit tests.

### 3.5 The split

Expand Down Expand Up @@ -485,6 +488,7 @@ import { KnowledgeServicePlugin } from '@objectstack/service-knowledge';
import { KnowledgeMemoryPlugin } from '@objectstack/knowledge-memory';
import { KnowledgeRagflowPlugin } from '@objectstack/knowledge-ragflow';
import { KnowledgePgvectorPlugin } from '@objectstack/knowledge-pgvector';
import { KnowledgeTursoPlugin } from '@objectstack/knowledge-turso';

defineStack({
plugins: [
Expand All @@ -495,6 +499,7 @@ defineStack({
apiKey: process.env.RAGFLOW_KEY!,
}),
new KnowledgePgvectorPlugin(), // self-hosted, PostgreSQL + pgvector
new KnowledgeTursoPlugin(), // local / not on PostgreSQL, a libSQL file
],
});
```
Expand Down Expand Up @@ -569,6 +574,56 @@ an exact scan.
sensitive as the record it came from, and it is in every backup of the
database.

### 4.2 `knowledge-turso` — the open vector store in a libSQL file

`@objectstack/knowledge-turso` keeps a source's passages and vectors in a
[libSQL](https://github.com/tursodatabase/libsql) database of its own, with
libSQL's native vectors (`F32_BLOB`, `vector32`, `vector_distance_cos`): the
store for local development and for a deployment not on PostgreSQL. It takes
the service's passages, embeds them with the kernel's `embedder` service (or
the `embedder` it is given), and answers a search by an exact scan.

- **Which database.** The `url` option (`file:`, `:memory:` or a
`libsql://` Turso URL), else `knowledge.db` under the project's
`.objectstack/data/`, beside the platform database. Never the platform's
own file: two engines writing one SQLite file in one process lose rows, so a
URL naming `objectstack.db`, `dev.db`, `standalone.db` or the file
`OS_DATABASE_URL` names is refused. On a file the store sets WAL and a busy
timeout (`busyTimeoutMs`, default 5000); in one process every call of the
store runs its database work one at a time.
- **Tables.** `knowledge_generation`, one row per source and generation
(fingerprint, recipe hash, width, model, active or being built; at most one
of each per source, by a unique index — a second build joins the one under
way when its fingerprint is the same, and replaces it otherwise, so the
first builder's writes are then refused), and one passage table per source and generation,
`knowledge_<source>_g<n>`, its vector column sized for that generation. A
source id that is not snake_case is refused before
any SQL.
- **Search.** An exact scan by cosine distance; the organization filter, the
[record scope](#record-scope) and the metadata `filter` (JSON equality per
key) run in the same `WHERE`, before ranking. The store creates no DiskANN index: `vector_top_k` ranks
before it filters, and with one, 1,000 vectors of 1,536 dimensions grew a
file from 8.2 MB to 329 MB and took 21.8 s to index. Without it the same
1,000 passages take 8.3 MB and a search about 5 ms; 10,000 take 82 MB and
about 50 ms. A larger source belongs on `knowledge-pgvector`.
- **Rebuilds.** A changed fingerprint is refused (§3.6). A complete reindex
under a new fingerprint builds a new generation table beside the active
one, which keeps answering its own fingerprint, and switches at the end of
the build; the old table is dropped. A build of a third fingerprint
abandons one left unfinished. A table of the package's earlier releases
(`knowledge_<source>` with exactly their columns) is never served: a search
answers `KNOWLEDGE_REBUILD_REQUIRED`, and the table is dropped when the first
complete reindex finishes. A table of any other shape under one of the
store's names is neither served nor dropped.
- **Re-embedding.** A passage whose text is unchanged (its `chunk_hash`) is
not embedded again within a generation.
- **When it cannot run.** A database that cannot be opened, or a platform
with no native libSQL build (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`.
- **The text is stored.** Each row holds its passage's text, as sensitive as
the record it came from, and in every copy of the file.

---

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

### 7.1 Non-goals for Phase 1

Expand Down
Loading
Loading