Repository navigation
Commit b8c8c88
feat(service-knowledge): register EMBEDDER_SERVICE from the ai.embedder_* settings when the knowledge service is mounted (#37265)
repo:objectstack#4 · os-justin · session_01QSeKYpEsWWE9Y5YtSsDgqZ
Fixes #37094
Clause-②: yes (widening) — `@objectstack/embedder-openai` gains one
export, `documentedMaxBatchSize(model): number | undefined`
Metadata-protocol: none
## 维护者速读
-
用户能看到什么变化:开源部署挂上知识服务后,管理员在设置页「嵌入服务」里保存服务商和密钥、重启,知识检索就能用上这个嵌入器;设置页提示改动需重启生效。
- 为什么做:知识检索平台化专题的开源版嵌入器一项——此前设置页的嵌入配置在开源版没人读,应用只能自己写代码注册嵌入器。
-
出错会怎样:没配置时照常启动,需要嵌入的检索和建索引会报错并点名缺哪项设置,不向外发任何请求;已有嵌入器(云版注册的)保持不动;保存的批量大小超过模型公布上限时按上限发送。可直接撤回。
- 要你做的:无。
## What changes
- **`KnowledgeServicePlugin` registers `EMBEDDER_SERVICE` from the
`ai.embedder_*` settings**
(`packages/services/service-knowledge/src/knowledge-service-plugin.ts`).
Read once, at `kernel:bootstrapped`:
- **Already registered → kept.** One `info` line, settings not applied.
A registration the sync accessor cannot hand out (an async factory)
counts as registered too.
- **Saved and complete → an `@objectstack/embedder-openai` embedder**
for the saved provider. The saved Base URL applies to the custom and
Azure providers; every other provider uses its preset endpoint. The boot
line names the endpoint host. "Saved" = any layer but the manifest
default (`source !== 'default'`, the same reading `plugin-email` uses
for `branding.workspace_name`); an `OS_*` override counts as saved.
"Complete" = the settings page's own Test embedder check
(`aiTestEmbedderActionHandler`), handed only the saved values: reused,
not forked.
- **Batch size passed only when saved, held to the model's documented
cap.** Unsaved (default 64) → the embedder batches at the model's
documented cap. Saved → `Math.min(saved, documentedMaxBatchSize(model))`
when the saved model has a documented cap, the saved size as saved
otherwise (no saved model, or a model the table does not know).
- **Dimensions and batch size are read as positive integers**, as
numbers or as their decimal text (an `OS_AI_EMBEDDER_DIMENSIONS=256`
override). Any other saved value is refused: `EMBEDDER_SERVICE` is the
`UnconfiguredEmbedder`, with a `warn` boot line naming the key.
- **Nothing configured** (no provider saved, `none` saved, incomplete,
settings service absent, or the `ai` namespace without the embedder
keys) → an `UnconfiguredEmbedder` is registered: `embed()` (non-empty)
and `dimensions` refuse with a message naming the missing settings,
where to save them and that a restart is needed. Boot succeeds; nothing
is sent. Boot line is `info` when nothing asked for an embedder, `warn`
when saved settings do not build one.
- The API key never reaches a log line or an error message. Pinned on
the configured path, on incomplete settings and on a provider answering
401.
- **Restart note on the settings page**: a `description` on
`embedder_provider` in `ai.manifest.ts` ("Read when the server starts: a
saved change to any embedder setting takes effect after the next
restart."), and its `help` copy in all 11 locale bundles. The
translations are required by the gate:
`settings-translation-coverage.test.ts` reds a described field with no
`keys.KEY.help` in any locale.
- **`@objectstack/embedder-openai` exports
`documentedMaxBatchSize(model): number | undefined`** (seat direction,
issue comment 7156326561): the documented per-request cap, read from the
SAME module table `OpenAIEmbedder` batches by. No copy, and the
constructor now reads the table through the same function.
`OpenAIEmbedder` is unchanged: an explicit `maxBatchSize` is still used
as given, above the cap included (pinned).
- **Docs**: `content/docs/ai/knowledge-rag.mdx` gains "Configuring the
embedder from Settings".
- **Dependency edge (one, workspace, no external)**:
`@objectstack/service-knowledge` → `@objectstack/embedder-openai`. Why
this side: service-knowledge already depends on `core` (plugin contract)
and `service-settings` (the settings reader and the reused completeness
check), so the knowledge plugin needs exactly one new edge; a plugin in
`embedder-openai` would need `core` and `service-settings` (two edges, a
crypto library among the transitive ones) and a host would have to mount
it by hand, which is the gap the card is about. Services depending on
plugins has precedent (`service-storage`, `service-print`,
`service-automation`). `embedder-openai` depends only on `spec`: no
cycle.
### Why `kernel:bootstrapped` (the phase measurement)
`ObjectKernel.bootstrap()` / `LiteKernel.bootstrap()` run every
`init()`, then every `start()`, then `kernel:ready` handlers in plugin
order, then `kernel:bootstrapped` once every `kernel:ready` handler has
settled (`plugin-lifecycle-events.ts`). An embedder registered in
`init()`, `start()` or any `kernel:ready` handler (where a
settings-reading provider would register, since the settings engine
binds in `kernel:ready`) is therefore visible at `kernel:bootstrapped`,
regardless of plugin order. Pinned both ways: a host embedder registered
at `init` and one registered in a `kernel:ready` handler are each kept.
Only a registration in `kernel:bootstrapped` / `kernel:listening` or
after boot would collide; the changeset says so. Settings are read after
`kernel:ready` for the same reason the settings plugin documents: a read
in the pre-bind window answers the default without consulting the store.
Cloud reading (cloud-confirm 7159003875 on #37094, read at cloud main
`packages/service-ai/src/plugin.ts`): `service-ai` applies its embedder
in a `kernel:ready` hook (`bindSettings` → `applyEmbedder`), so its
boot-time registration comes before this placeholder exists. Every later
`ai` settings save calls `replaceService`, never `registerService`, so
it replaces the placeholder without throwing. A cold boot with a
configured provider still logs the existing `[AI] Failed to apply
embedder settings` warning, as it does on main with nothing registered;
the placeholder does not make that worse. No cloud deployment mounts the
open `KnowledgeServicePlugin` beside `service-ai`.
## Tests
New top-level `describe` in the knowledge+settings harness file
(`src/__tests__/knowledge-settings-contribution.test.ts`), through the
real registration path: settings SAVED via `SettingsService.setMany` in
a `kernel:ready` handler, provider reached through a stubbed global
`fetch`, and an adapter that looks up `EMBEDDER_SERVICE` where it
embeds. No hand-built embedder.
- saved settings → `indexDocument` sends one `POST
https://api.openai.com/v1/embeddings` with the saved key and model;
nothing sent at boot; key in no log line
- custom endpoint → the saved Base URL
- pre-registered at `init` / at `kernel:ready` → same instance kept,
zero requests, one `info` line
- unsaved batch size on DashScope `text-embedding-v3` → requests of 10,
10, 5 (not one of 25: the default 64 is not a saved value)
- saved batch size 4 → requests of 4, 4, 1
- **pin (a)** saved 64 on `text-embedding-v3` (documented 10) → requests
of 10, 10, 5, and the boot line names the saved 64
- **pin (b)** saved 50 on a custom model with no documented cap →
requests of 50, 10 (kept as saved)
- `embedder-openai`: `documentedMaxBatchSize` answers 10 / 2048 /
`undefined` (unknown, and a prototype key); the embedder with no
`maxBatchSize` batches at exactly that value; an explicit `maxBatchSize:
25` on a cap-10 model still sends 25
- no settings → boot succeeds; `indexDocument` and the adapter's
`search` reject naming `ai.embedder_provider` and the restart;
`KnowledgeService.search` answers `[]` and logs that error per source;
`dimensions` refuses; zero requests; boot line `info`
- `none` saved → refusal says so; incomplete (provider without key) →
the page check's own message, boot line `warn`
- a preset provider uses its preset endpoint even when a Base URL is
saved → one request to the preset endpoint; the boot line names
`endpoint=api.openai.com`
- `OS_AI_EMBEDDER_DIMENSIONS=256` (arrives as text) → `dimensions` is
256 and the request carries `dimensions: 256`
- `OS_AI_EMBEDDER_DIMENSIONS=large` → refused naming
`ai.embedder_dimensions`, a `warn` boot line, zero requests
- unsaved batch size on a custom model with no documented cap, 70 inputs
→ requests of 10 × 7
- the API key in no log line and no error: incomplete settings (`custom`
without a Base URL), and a provider answering 401
Ablations (one-shot, `scripts/ablation-replace.mjs`, each mutation
landed and restored to blob == HEAD, `git diff HEAD` empty):
- treating the manifest default as saved (`if (!entry) return
undefined;`) → 1 failed / 15 passed: exactly "an unsaved batch size on a
model with no documented cap is the embedder fallback, not the default
64".
- clamp removed (the `Math.min` line → `savedBatch`) → 1 failed / 15
passed: exactly pin (a). The first attempt at this one was a no-op: the
replacement text already occurred in the anchor, the tool refused before
running, and it was re-run with a distinct replacement.
- the Base URL provider check removed → 1 failed / 15 passed: exactly "a
preset provider uses its preset endpoint even when a Base URL is saved".
- decimal text not read as a number → 1 failed / 15 passed: exactly the
`'256'` case.
- a value that is not a positive integer dropped instead of refused → 1
failed / 15 passed: exactly the `'large'` case.
- the API key appended to the incomplete-settings refusal → 1 failed /
15 passed: exactly the key-not-logged case.
## Acceptance notes
- **The clamp needed a surface widening, ruled by the seat** (comment
7156326561, option A): one read-only export from `embedder-openai`. With
no saved model the embedder uses its own default model; no cap is looked
up for a model the plugin does not name, so the saved size passes as
saved. The field's maximum (2048) equals that default model's documented
cap.
- **Contract review owed**: Clause-② is yes, so the seat's direction
asks for a same-head contract review at the tier these paths require, on
top of L1.
- **Declared beyond the named surface (mechanically forced):**
`packages/services/service-knowledge/vitest.config.ts` gains one alias
for `@objectstack/embedder-openai` (without it `check:test-source-alias`
reds: the tests would read the embedder's `dist/`); `pnpm-lock.yaml`
records the new workspace edge.
- **Search reports, it does not throw.** `KnowledgeService.search`
already catches a per-source adapter failure, logs it at `error` and
answers no hits from that source; with no embedder that logged error is
the named reason. Indexing throws it. That catch lives in
`knowledge-service.ts`, outside this card.
- **No open-edition adapter consumes `EMBEDDER_SERVICE` yet**
(`knowledge-memory` uses its own sparse vectors; `knowledge-ragflow`
embeds server-side), so in the open edition today the registered
embedder is used by custom adapters and by the coming pgvector adapter.
The `EMBEDDER_SERVICE` doc comment in
`packages/spec/src/contracts/embedder.ts` still says it is "Registered
by `@objectstack/service-ai`"; spec prose was not touched here.
- An unknown model with no saved batch size still gets the embedder's
own fallback of 10 and its one construction warning, unchanged.
## Verification
Two-minute check: `pnpm --filter @objectstack/service-knowledge exec
vitest run src/__tests__/knowledge-settings-contribution.test.ts -t
37094` → 16 passed.
Read first, by risk:
1. `knowledge-service-plugin.ts` `provideEmbedder()` and
`embedderFromSettings()`: the phase, the registered check, the
saved-vs-default reading, the clamp.
2. `UnconfiguredEmbedder`: a registered placeholder means
`EMBEDDER_SERVICE` is never absent once the knowledge service is
mounted.
3. `embedder-openai/src/index.ts` `documentedMaxBatchSize` and the
constructor's use of it.
Local readings, head `fa818f4d` (service-settings and the dispatch-gates
run at `f329965b`; the later commits touch neither service-settings nor
the derived gate set):
<details><summary>Package tests, typecheck, derived gates</summary>
- `pnpm --filter @objectstack/embedder-openai test` → 31/31; build +
`typecheck` (tsc + `check:test-typecheck`) exit 0.
- `pnpm --filter @objectstack/service-knowledge test` → Test Files 8/8,
Tests 186/186; `typecheck` exit 0, 0 errors.
- `pnpm --filter @objectstack/service-settings test` → Test Files 77/77,
Tests 1523/1523; `typecheck` exit 0 (head `35c742d2`; the later commits
do not touch service-settings).
- Dependency closure built with declarations first.
- `node scripts/pm/dispatch-gates.mjs --commands` (21 paths): 115
commands, each exit captured before any pipe: 114 exit 0, among them
`check:startup-registry-verdict`, `check:test-source-alias`,
`check:undeclared-dep-imports`, `check:workspace-manifest-cycles`,
`check:published-readme-links`, `check:dual-build-cjs-loads`,
`check:settings-bind-window`, `check:doc-authoring`,
`check:issue-citations`, `check:nul-bytes`, `check:changeset-fixed`,
`check-empty-changeset`, `check-changeset-no-major`,
`check-adr-0087-registration`, `check:type-check-coverage`.
- 1 exit 3, PREREQUISITE NOT MET, so NOT MEASURED here (CI runs it):
`check-plugin-teardown-shape --self-test` (shallow clone cannot read its
pinned fixture).
- `dispatch-gates --ran`: 115 derived, 115 run, 0 unrun.
- Repo-wide scans (`pnpm lint`) and dogfood are CI's (no `!` in the
title, no narrowing arm).
</details>
Signed: repo:objectstack#4 · os-justin ·
session_01QSeKYofJfCPWsxaVYkmaVF
## Provenance
Opened by seat repo:objectstack#4 on the maintainer's authorization in
the seat session (「#37094 开 PR 被拦:放行」). The developer's own `pr_create`
in zone a4-K's dev session was refused twice by a permission check; the
dev stopped without retrying and handed the material over. Branch
`claude/issue-37094-open-embedder-settings` @
`fa818f4d40749e28de09bbd747e9ca3c829f17ed`.
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
(spec untouched)
- [x] 2. Every name I added uses the casing for its kind.
- [ ] 3. No formatter ran; `pnpm lint` passes. (No formatter ran; `pnpm
lint` was not run locally — CI's run decides.)
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 6. `packages/spec` touched: n/a.
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English (the 维护者速读 block is the
fleet's required Chinese summary).
- [ ] The *Before Submitting* list in CONTRIBUTING.md is done. (Not
walked item by item; the local readings above are what ran.)
---
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01QSeKYpEsWWE9Y5YtSsDgqZ
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 02520ab commit b8c8c88
21 files changed
Lines changed: 837 additions & 5 deletions
File tree
- .changeset
- content/docs/ai
- packages
- plugins/embedder-openai/src
- __tests__
- services
- service-knowledge
- src
- __tests__
- service-settings/src
- manifests
- translations
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
107 | 126 | | |
108 | 127 | | |
109 | 128 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
207 | 208 | | |
208 | 209 | | |
209 | 210 | | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
210 | 229 | | |
211 | 230 | | |
212 | 231 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
123 | 138 | | |
124 | 139 | | |
125 | 140 | | |
| |||
263 | 278 | | |
264 | 279 | | |
265 | 280 | | |
| 281 | + | |
266 | 282 | | |
267 | 283 | | |
268 | 284 | | |
269 | 285 | | |
270 | 286 | | |
271 | 287 | | |
272 | 288 | | |
273 | | - | |
274 | | - | |
| 289 | + | |
| 290 | + | |
275 | 291 | | |
276 | 292 | | |
277 | 293 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| |||
0 commit comments