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
18 changes: 18 additions & 0 deletions .changeset/37094-open-embedder-from-settings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@objectstack/service-knowledge': patch
'@objectstack/service-settings': patch
'@objectstack/embedder-openai': patch
---

feat(service-knowledge): the open edition registers `EMBEDDER_SERVICE` from the `ai.embedder_*` settings when the knowledge service is mounted

Clause-②: yes (widening) — `@objectstack/embedder-openai` gains one export, `documentedMaxBatchSize(model): number | undefined`
Metadata-protocol: none

- **An embedder from the settings page.** `KnowledgeServicePlugin` reads the saved `ai.embedder_*` settings once, at `kernel:bootstrapped`, and registers an `@objectstack/embedder-openai` embedder as `EMBEDDER_SERVICE` (`'embedder'`) for the saved provider. The saved Base URL applies only to the custom and Azure providers; other providers use their preset endpoint. The boot line names the endpoint host. "Saved" means any layer but the manifest default, an `OS_*` override included; `ai.embedder_dimensions` and `ai.embedder_batch_size` are read as positive integers whether they arrive as numbers or as text (an `OS_AI_EMBEDDER_DIMENSIONS=256` override), and a value that is not one is refused with a `warn`, not dropped. Completeness is the Test embedder button's own check. `@objectstack/service-knowledge` now depends on `@objectstack/embedder-openai`.
- **An embedder already registered is kept.** One registered by ObjectOS Cloud or by a host's own plugin in `init()`, `start()` or a `kernel:ready` handler is kept, with one `info` line, and the settings are not applied.
- **The batch size is passed only when it was saved, and is held to the model's documented cap.** Unsaved (the form shows 64), the embedder batches at the model's documented cap. Saved, it is passed as `min(saved, documented cap)` when the saved model has a documented cap (DashScope `text-embedding-v3`: a saved 64 sends 10 per request), and as saved for a model with none.
- **No embedder configured: the boot succeeds, and every use of the embedder answers an error naming the missing settings.** With no provider saved, `none` saved, or incomplete settings, `EMBEDDER_SERVICE` is an embedder whose `embed()` and `dimensions` refuse with that reason and the restart it needs, so an adapter that embeds refuses indexing and search with it. Nothing is sent to a provider until one is saved, and the API key is never logged or put in an error.
- **A saved change waits for a restart, and the page says so** — help under the Embedder provider field (`@objectstack/service-settings`, all locales). There is no hot replacement.
- **New export `documentedMaxBatchSize(model)` from `@objectstack/embedder-openai`**: the provider-documented per-request input cap for a model, read from the same known-model table `OpenAIEmbedder` batches by, or `undefined` for a model the table does not document. Read-only. `OpenAIEmbedder` is unchanged: an explicit `maxBatchSize` passed to it directly is still used as given, above the documented cap included.
- If your own plugin registers `EMBEDDER_SERVICE` after `kernel:ready` (in `kernel:bootstrapped` or later, or after boot), move that registration into `init()`, `start()` or `kernel:ready`: the knowledge plugin has registered one by then, and a second registration of the name is refused.
19 changes: 19 additions & 0 deletions content/docs/ai/knowledge-rag.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ installed ([Connect an MCP Client](/docs/ai/connect-mcp)).
- **Inline event sync.** When records on indexed objects change, the kernel's `IRealtimeService` events drive `KnowledgeService.handleRecordUpsert/Delete` automatically. No cron, no queue (yet — Phase 2).
- **Adapter swap, zero caller changes.** Move from `memory` to `ragflow` to a custom adapter without touching your `search` calls, the `search_knowledge` tool, or any agent prompt.

## Configuring the embedder from Settings

When `KnowledgeServicePlugin` is mounted beside the settings service, Settings → AI & Embedder offers an **Embedder** section (`ai.embedder_*`): provider, API key, model, Base URL (for a custom or Azure endpoint), dimensions and batch size. The knowledge plugin reads those settings once, when the server starts, and registers the `EMBEDDER_SERVICE` (`'embedder'`) that knowledge adapters embed with:

- **An embedder is already registered** — by ObjectOS Cloud, or by your own plugin — **it is kept.** The plugin logs one line saying so and does not apply the settings. It checks at `kernel:bootstrapped`, after every plugin's `init()`, `start()` and `kernel:ready` handler, so an embedder registered in any of those is seen. Register yours there, not later.
- **A provider is saved and complete** → an `@objectstack/embedder-openai` embedder for that provider. The saved Base URL applies only to the custom and Azure providers; other providers use their preset endpoint. The boot line names the endpoint host. "Complete" is what the **Test embedder** button checks: a provider, an API key (Ollama excepted) and, for a custom or Azure endpoint, a Base URL.
- **Dimensions and batch size are positive integers**, saved as numbers or set as text through an `OS_*` override (`OS_AI_EMBEDDER_DIMENSIONS=256`). A value that is not a positive integer is refused, with a `warn` at boot and the error below on every use — never silently ignored.
- **The batch size is passed only when you saved it.** Left unsaved (the form shows 64), the embedder sends as many inputs per request as the model's documented cap allows — 10 for DashScope `text-embedding-v3`, 2048 for OpenAI `text-embedding-3-*` — and 10 for a model it does not know. A saved batch size is held to the saved model's documented cap (`documentedMaxBatchSize(model)` from `@objectstack/embedder-openai`): a saved 64 on `text-embedding-v3` sends 10. For a model without a documented cap the saved size is sent as saved.
- **Nothing is configured** — no provider saved, `none` saved, or the saved settings incomplete → the server still boots, and every use of the embedder answers an error naming the missing settings: an adapter that embeds refuses indexing and search with it, and nothing is sent anywhere. Nothing is sent to a provider until you have saved one, and the API key is never logged or put in an error.

**A saved change takes effect after a restart** — the page says so under the provider field. There is no hot replacement.

An adapter that embeds resolves the service where it embeds, not at boot:

```ts
const embedder = ctx.getService<IEmbedder>(EMBEDDER_SERVICE); // from @objectstack/spec/contracts
const vectors = await embedder.embed(chunks.map((c) => c.content));
```

## Embedder input limits

Every embedding model accepts a text up to some length, counted in tokens. A provider that is sent a longer text truncates it **without an error** — the tail is never embedded, so a search for it finds nothing. `@objectstack/embedder-openai` refuses such an input instead:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { describe, it, expect, vi } from 'vitest';
import {
OpenAIEmbedder,
createOpenAIEmbedder,
documentedMaxBatchSize,
OPENAI_COMPATIBLE_PRESETS,
} from '../index';
import { estimateTokens } from '@objectstack/spec/shared';
Expand Down Expand Up @@ -197,6 +198,24 @@ describe('OpenAIEmbedder batching (per-request input cap)', () => {
expect(out).toEqual(Array.from({ length: 7 }, (_, i) => [i]));
});

it('documentedMaxBatchSize answers the cap the embedder batches by, and nothing for an unknown model', async () => {
expect(documentedMaxBatchSize('text-embedding-v3')).toBe(10);
expect(documentedMaxBatchSize('text-embedding-3-small')).toBe(2048);
expect(documentedMaxBatchSize('my-gateway-model')).toBeUndefined();
expect(documentedMaxBatchSize('constructor')).toBeUndefined();
// The same table: with no maxBatchSize, the embedder batches at exactly that cap.
const f = echoFetch();
await new OpenAIEmbedder({ apiKey: 'k', model: 'text-embedding-v3', fetch: f as unknown as typeof fetch }).embed(inputs(12));
expect(sentSizes(f)).toEqual([documentedMaxBatchSize('text-embedding-v3'), 2]);
});

it('an explicit maxBatchSize above the documented cap is still used as given', async () => {
const f = echoFetch();
const e = new OpenAIEmbedder({ apiKey: 'k', model: 'text-embedding-v3', maxBatchSize: 25, fetch: f as unknown as typeof fetch });
await e.embed(inputs(30));
expect(sentSizes(f)).toEqual([25, 5]);
});

it('refuses a maxBatchSize that is not a positive integer', () => {
for (const bad of [0, -1, 1.5, Number.NaN]) {
expect(() => new OpenAIEmbedder({ apiKey: 'k', maxBatchSize: bad })).toThrow(
Expand Down
20 changes: 18 additions & 2 deletions packages/plugins/embedder-openai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ const KNOWN_MAX_BATCH_SIZE: Record<string, number> = {
*/
const DEFAULT_MAX_BATCH_SIZE = 10;

/**
* The provider-documented per-request input cap for `model`, read from the
* known-model table `OpenAIEmbedder` batches by, or `undefined` when the table
* documents none (the embedder then falls back to {@link DEFAULT_MAX_BATCH_SIZE}
* with a warning).
*
* Read-only: a caller that takes a batch size from elsewhere — an operator's
* saved setting — can hold it at or under the model's cap before passing it as
* `maxBatchSize`. It changes nothing about `OpenAIEmbedder` itself: an explicit
* `maxBatchSize` is still used as given, above the documented cap included.
*/
export function documentedMaxBatchSize(model: string): number | undefined {
return Object.prototype.hasOwnProperty.call(KNOWN_MAX_BATCH_SIZE, model) ? KNOWN_MAX_BATCH_SIZE[model] : undefined;
}

/**
* Documented per-input token limits (the longest single text one
* `/embeddings` input may be). A model listed here refuses an over-limit
Expand Down Expand Up @@ -250,15 +265,16 @@ export class OpenAIEmbedder implements IEmbedder {
// Once per instance, in ONE warning: the configuration is what is
// unknown, not any one call.
const unknown: string[] = [];
const documentedCap = documentedMaxBatchSize(this.model);
if (opts.maxBatchSize !== undefined) {
if (!Number.isInteger(opts.maxBatchSize) || opts.maxBatchSize < 1) {
throw new Error(
`OpenAIEmbedder: maxBatchSize must be a positive integer, got ${String(opts.maxBatchSize)}`,
);
}
this.maxBatchSize = opts.maxBatchSize;
} else if (KNOWN_MAX_BATCH_SIZE[this.model] !== undefined) {
this.maxBatchSize = KNOWN_MAX_BATCH_SIZE[this.model];
} else if (documentedCap !== undefined) {
this.maxBatchSize = documentedCap;
} else {
this.maxBatchSize = DEFAULT_MAX_BATCH_SIZE;
unknown.push(
Expand Down
1 change: 1 addition & 0 deletions packages/services/service-knowledge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"@objectstack/core": "workspace:*",
"@objectstack/embedder-openai": "workspace:*",
"@objectstack/service-settings": "workspace:*",
"@objectstack/spec": "workspace:*"
},
Expand Down
Loading
Loading