Skip to content
Draft
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
22 changes: 22 additions & 0 deletions .changeset/38710-knowledge-search-cut-once.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@objectstack/spec': patch
'@objectstack/core': patch
'@objectstack/service-knowledge': patch
'@objectstack/knowledge-turso': patch
'@objectstack/knowledge-pgvector': patch
'@objectstack/service-cluster': patch
'@objectstack/cli': patch
'@objectstack/verify': patch
---

fix(knowledge): a Chinese question is cut into whole words once, with the platform segmenter and the administrator's search words, before every keyword search

Clause-②: yes (the adapter contract `AdapterSearchOptions` gains a field)
Metadata-protocol: none

Who sees it: anyone asking the knowledge base a question in Chinese, in an environment whose knowledge search runs in keyword or hybrid mode.

- **Whole words, not sliding windows.** The knowledge service now cuts each question once per source face with the segmenter (the Chinese dictionary plus the search words and stop words kept under Setup → Search Terms) and hands those words to the store. 「我在集团干了 12 年了,一年有几天年假?」 is matched as `集团`, `一年`, `年假`, `12` instead of sixteen overlapping fragments such as `我在集`, so 年假, 脱密, 应急采购 and 超预算 are matched whole. English questions are cut as before.
- **Adapter contract.** `AdapterSearchOptions` gains one optional member, `keywordTerms?: readonly string[]` (ADR-0161, the dated note of 2026-12-05 under K8). The Turso and pgvector stores match the words they are handed; when the member is absent (no segmenter is running, or it failed) they cut the question themselves with `keywordTerms` from `@objectstack/core`, as before. Their private copies of that function are gone. The adapter contract suite (`@objectstack/service-knowledge/testing`) gains the member's case: a store with a keyword leg cuts as `@objectstack/core` does when the member is absent, matches the handed words without cutting when it is present, and matches nothing when it is empty; a store without one answers the same either way.
- **A change to a search word reaches the next question.** The service caches the words per environment for 30 seconds at most; adding, editing or deleting a word clears the cache at once, and on a multi-replica deployment the change is broadcast to the other replicas on the `search-term.changed` cluster lane.
- **The segmenter is mounted with knowledge.** An environment that requires `knowledge` now also mounts `@objectstack/service-segmenter`; `@objectstack/cli` and `@objectstack/verify` depend on it.
16 changes: 16 additions & 0 deletions .changeset/38710-search-terms-setup-entry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@objectstack/service-segmenter': patch
'@objectstack/platform-objects': patch
---

feat(segmenter): Setup → Search Terms, with one entry per word and knowledge source

Clause-②: yes (the adapter contract `AdapterSearchOptions` gains a field)
Metadata-protocol: none

Who sees it: a platform administrator in Setup, and every knowledge search that reads the words they keep.

- **Setup → Search Terms** lists the search words and stop words (`sys_search_term`) for an administrator with Manage Platform Settings, with the console's Import entry for loading a list from one file. The entry is labelled in every platform locale (检索词条 in Simplified Chinese).
- **One entry per word and source.** A new read-only field, `term_key`, holds the word normalised (NFKC, trimmed, lower case), and a unique index on `term_key` with `knowledge_source` refuses a second entry for the same word and source: `OA审批` and `oa审批` are one word. An entry with no knowledge source applies to every source.
- **A source that does not exist is refused.** Saving a word for a knowledge source the environment does not have answers `VALIDATION_ERROR` (400) on `knowledge_source`; a word whose source was later removed is skipped with one warning in the log.
- `keyOf` (the normalisation) is exported from `@objectstack/service-segmenter`.
10 changes: 10 additions & 0 deletions content/docs/protocol/knowledge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,16 @@ about a call and reads no vector: the question is embedded at most once for
each adapter instance in a search, by the adapter. An adapter without
`embedQuery` is handed nothing and embeds the question on every call, as
before.
`AdapterSearchOptions.keywordTerms` is the question already cut into the
words a keyword or hybrid search matches (ADR-0161, the dated note of
2026-12-05 under K8). The service cuts the question once per source face with
the platform segmenter, the Chinese dictionary plus the search words and stop
words an administrator keeps under Setup → Search Terms, and hands the same
words to every adapter. An adapter that receives the member matches those
words; an empty list means the question has no word worth matching, and the
keyword side then finds nothing. When the member is absent (no segmenter is
running, or the segmenter failed), the adapter cuts the question itself, as
before. A search under `vector` is never handed it.
`listDocuments` lists the documents the
adapter holds for a source, a page of at most `limit` entries at a time, each
with the `docHash` it was stored with; `nextCursor` is absent once the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- **Amended** (2026-12-01, [#38438](https://github.com/objectstack-ai/objectstack/issues/38438)): an eighth dated note, under the list of [Consequences](#consequences), records that the service keeps a sync state for each source, where the bullet "A stored fingerprint and recipe hash are the only index state the service relies on" says "The service keeps none of its own". Built by [#38307](https://github.com/objectstack-ai/objectstack/pull/38307). Whether an index is due for a build is still read from the stored fingerprint and recipe hash. It decides nothing and adds no contract member. The status line above is left as it is.
- **Amended** (2026-12-01, [#38676](https://github.com/objectstack-ai/objectstack/issues/38676)): a ninth dated note, under "Rules for every member" in [K8](#k8--the-contract-additions-decided-item-by-item), registers two contract members, both optional: `IKnowledgeAdapter.embedQuery`, by which an adapter embeds the question of a search as its own `search` embeds it, and `AdapterSearchOptions.queryVector`, by which the knowledge service carries that vector back to the adapter instance that made it, for every `search` call of that search, the refill round included. It is read with the "Embedding calls" row of [K1](#k1--the-boundary), with the second sentence of the list under that section and with item 15 of K8, and it amends none of them: the adapter still embeds, with its own embedder and query prefix, at the point where it embeds today, and the service computes no vector. Decided by the maintainer on 2026-12-01, by his choice of the decision card's option 「写进约定,一次提问只转一次」 at 00:22Z (ruling record [`7296268226`](https://github.com/objectstack-ai/objectstack/issues/38676#issuecomment-7296268226) on #38676), which carries one condition: 「附带条件:存储驱动原本会给出的拒绝提示(比如“需要重建索引”)要保持在前面,不能被转向量的报错盖掉。」 The note states that condition as an outcome and names the means it takes, a function the adapter calls where it embeds today, which fixes the second member's type. A cross-reference stands under [K1](#k1--the-boundary). The status line above is left as it is.
- **Amended** (2026-12-03, [#39346](https://github.com/objectstack-ai/objectstack/issues/39346)): a tenth dated note under the table of [K8](#k8--the-contract-additions-decided-item-by-item), below the note of 2026-11-28 on item 12, amends one sentence of that note. Under "◆ Ruled: an author's save" it says of the package duplicate door: "What it answers where no knowledge service is registered, and to a caller who may not search what a source indexes, is unchanged". The first half of that sentence is amended: where no knowledge service is registered, the duplicate is not refused for that reason and copies each knowledge source unserved; the caller is asked the capability gate and is not asked whether they may search what a source indexes, because nothing on such a deployment can judge it. Ruling D of #37983 stands: an author's save there is refused as before. Decided by the maintainer on 2026-12-03, by his choice of the decision card's option 「A 放行复制,知识源不生效」 (ruling record [`7380897321`](https://github.com/objectstack-ai/objectstack/issues/38374#issuecomment-7380897321) on #38374). It adds no contract member, no door value and no settings key. The status line above is left as it is.
- **Amended** (2026-12-05, [#38710](https://github.com/objectstack-ai/objectstack/issues/38710)): an eleventh dated note, under "Rules for every member" in [K8](#k8--the-contract-additions-decided-item-by-item), registers one contract member, optional: `AdapterSearchOptions.keywordTerms`, the keyword terms of the question, cut once by the knowledge service with the kernel's segmenter and the administrator's search words, and handed to every adapter the search calls. It is read with the "Hybrid retrieval" row of [K1](#k1--the-boundary) and amends none of its words: the adapter still decides how a term is matched and how the legs are fused. Written on the maintainer's words of 2026-11-30 that open #38710, 「首要解决的应该是 用中文分词词典在应用层切词(Node 有现成的库),只留真正的词。」, and on the rulings of that card (records `7272241375` and `7272436453`). The status line above is left as it is.

---

Expand Down Expand Up @@ -507,6 +508,17 @@ Rules for every member:
>
> This note registers exactly two members, `IKnowledgeAdapter.embedQuery` and `AdapterSearchOptions.queryVector`, and no other: the list stays closed beyond them (rules 3 and 5). It declares, and PR #38798 builds it; the PR that lands this note changes no file under `packages/`. A reader that finds one of the two shapes cannot be built as written stops and says so on its card (rule 3). Until #38798 lands, `main` has neither member: at `0fc9eab900` no line under `packages/` or `content/` names `embedQuery` or `queryVector`, and every `search` call of `knowledge-turso` and `knowledge-pgvector` that reaches its vector leg embeds the query itself. The code this note cites was read on `main` at `0fc9eab900`, and PR #38798 at head `4561e81d0e`. The two tables of K1, the table of K8, the rules above and every earlier note are left as written; read the "Embedding calls" row, the second sentence of the list under K1 and item 15 with this note.


> **Note (2026-12-05, [#38710](https://github.com/objectstack-ai/objectstack/issues/38710)) — one member, `AdapterSearchOptions.keywordTerms`: the knowledge service cuts the question into its keyword terms once, and every adapter the search calls matches those terms.** The maintainer's words that open #38710, verbatim and untranslated, as its filing gate records them (2026-11-30): 「首要解决的应该是 用中文分词词典在应用层切词(Node 有现成的库),只留真正的词。」 and, on tuning, 「但是管理员如果需要调优和维护呢」. In English: the first thing to solve is to cut words with a Chinese dictionary segmenter in the application layer (Node has a ready library), keeping only real words; and what if an administrator needs to tune and maintain it. His rulings on that card chose the segmenter, `@node-rs/jieba`, and the administrator's object now (record [`7272241375`](https://github.com/objectstack-ai/objectstack/issues/38710#issuecomment-7272241375)), and the package, `@objectstack/service-segmenter`, with two layers of words, the platform's and the administrator's 检索词条 (`sys_search_term`), one set per deployment (record [`7272436453`](https://github.com/objectstack-ai/objectstack/issues/38710#issuecomment-7272436453)). The card's body left one design point to its second step, 「词条怎么到适配器」: the service hands the adapter the entries, or the service hands it the terms it has cut. ◆ This note takes the second, for the approver to accept with it or strike.
>
> - **The member.** `AdapterSearchOptions.keywordTerms?: readonly string[]`, on `packages/spec/src/contracts/knowledge-adapter.ts#AdapterSearchOptions`: the terms the keyword leg matches, in the shape the sliding-window rule `keywordTerms` of `@objectstack/core` answers (distinct, compared case-insensitively, at most `MAX_KEYWORD_TERMS`). **Present**: the adapter matches these terms, and does not cut the question itself; empty, the keyword leg has nothing to match. **Absent**: the adapter cuts the question by `keywordTerms` of `@objectstack/core`, as before this member. The service hands it when the kernel holds a segmenter (`ISegmenterService`, resolved where a search cuts, never at boot), and never under `vector`.
> - **◆ Why the terms, and not the entries.** An adapter is a storage plugin: it holds no data engine and resolves no service, so the entries would reach it only as data the service read anyway, and each adapter would then call the segmenter itself. Handing the terms keeps one cut per source of a search, shared by its two rounds, the same terms on every store, and adapters with no knowledge of the segmenter or of the object. An entry scoped to one source changes that source's terms only, so the service cuts once for each source it searches.
> - **The "Hybrid retrieval" row of K1.** The row gives the adapter "How vector and keyword scores are blended, which index or scan is used, and how candidates are found." From this note the words of the question are the platform's to cut, on the maintainer's 「在应用层切词」. Which index or scan matches a term (an FTS phrase, `LIKE`, a `tsvector` word), and how the legs are fused, stay the adapter's; neither store's statement changes shape. The row is left as written.
> - **What the service reads.** The enabled and disabled entries of `sys_search_term`, as the contract's `SegmenterEntry`: the entries with no source first, then the source's own, each by `created_at`, then `id`, never `updated_at`; `enabled` passed as a boolean. The read is cached per service, one per environment's kernel, dropped when a record changes on this replica, on a peer's message on the cluster channel `search-term.changed` (an address, never a value), and after a short time, because that channel delivers at most once.
> - **Not in this note.** A segmenter inside an adapter, a tokenizer on a knowledge source (the note of 2026-11-30 above keeps it out), and record search, which the segmenter contract reserves.
>
> This note registers exactly one member, `AdapterSearchOptions.keywordTerms`, and no other: the list stays closed beyond it (rules 3 and 5). It lands in the PR that builds it, with its first readers, `knowledge-turso` and `knowledge-pgvector`, the knowledge service that hands it, and its case in the contract test suite (`packages/services/service-knowledge/src/testing.ts#runKnowledgeAdapterContract`) (rule 1): an adapter that declares a keyword leg, searched under `keyword`, answers without the member what it answers when handed the cut of `keywordTerms` of `@objectstack/core`; handed terms, it matches them and does not cut the question; handed `[]`, it answers nothing. `knowledge-memory` does not read it and is searched as before (rule 2), which the suite pins for an adapter without a keyword leg: handed or not, a search answers the same. The rules above and every earlier note are left as written.

### K9 — One `search_knowledge` definition, and every knowledge tool goes through the tool registry and the MCP seams

**What landed** (PR #36823, merged as `8aecf6d770`, read in the tree at `254e396a29`). The tool has one definition, `packages/services/service-knowledge/src/knowledge-tools.ts#getKnowledgeToolDefinitions`, and one executor, bound by `packages/services/service-knowledge/src/knowledge-tools.ts#createKnowledgeTools`. The knowledge service hands both out through `knowledgeTools()`. Both MCP transports register that definition and call that executor as the principal they bind, where the grant admits `data:read` (`packages/mcp/src/mcp-http-tools.ts#registerKnowledgeTools`; the HTTP bridge in `packages/runtime/src/domains/mcp.ts`, the stdio bridge in `packages/mcp/src/stdio-data-bridge.ts`).
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"@objectstack/service-queue": "workspace:*",
"@objectstack/service-realtime": "workspace:*",
"@objectstack/service-record-context": "workspace:*",
"@objectstack/service-segmenter": "workspace:*",
"@objectstack/service-settings": "workspace:*",
"@objectstack/service-sms": "workspace:*",
"@objectstack/service-storage": "workspace:*",
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/test/serve-capability-identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { CAPABILITY_PROVIDERS, KNOWLEDGE_STORE_PROVIDERS, providesCapability } from '@objectstack/core';
import { CAPABILITY_PROVIDERS, KNOWLEDGE_SEGMENTER_PROVIDER, KNOWLEDGE_STORE_PROVIDERS, providesCapability } from '@objectstack/core';
import Serve from '../src/commands/serve.js';

// Pay the first transform of these dist-resolved workspace deps at MODULE LOAD:
Expand All @@ -50,6 +50,7 @@ import '@objectstack/driver-turso';
import '@objectstack/driver-sql';
import '@objectstack/platform-objects/plugin';
import '@objectstack/plugin-security';
import '@objectstack/spec/migrations';

const HERE = resolve(fileURLToPath(import.meta.url), '..');

Expand Down Expand Up @@ -228,6 +229,11 @@ describe('#7652: declared identities match what the provider packages register',
Object.values(KNOWLEDGE_STORE_PROVIDERS).map((store) => ({
cap: `knowledge:${store.export}`, pkg: store.pkg, export: store.export, identities: store.identities,
})),
// And the segmenter it mounts beside them.
[{
cap: `knowledge:${KNOWLEDGE_SEGMENTER_PROVIDER.export}`, pkg: KNOWLEDGE_SEGMENTER_PROVIDER.pkg,
export: KNOWLEDGE_SEGMENTER_PROVIDER.export, identities: [...KNOWLEDGE_SEGMENTER_PROVIDER.identities],
}],
);

/**
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/capability-composition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ describe('resolveKnowledgeStore — which store the knowledge token mounts', ()
});

describe('resolveCapabilityExtras — what a boot mounts after a main provider', () => {
it('knowledge: exactly one store, with its argument; never the in-memory one', () => {
it('knowledge: exactly one store, with its argument, then the segmenter; never the in-memory store', () => {
const extras = resolveCapabilityExtras('knowledge', { stack: {}, packageRoot: '/a', database: { driver: 'postgres', url: 'postgres://h/d' } });
expect(extras).toEqual([
{
Expand All @@ -303,6 +303,11 @@ describe('resolveCapabilityExtras — what a boot mounts after a main provider',
identities: ['com.objectstack.plugin.knowledge-pgvector', 'KnowledgePgvectorPlugin'],
argument: { url: 'postgres://h/d' },
},
{
pkg: '@objectstack/service-segmenter',
export: 'SegmenterServicePlugin',
identities: ['com.objectstack.service.segmenter', 'SegmenterServicePlugin'],
},
]);
expect(Object.values(KNOWLEDGE_STORE_PROVIDERS).map((s) => s.pkg)).not.toContain('@objectstack/knowledge-memory');
});
Expand Down
Loading
Loading