-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathembedder.ts
More file actions
55 lines (53 loc) · 2.26 KB
/
Copy pathembedder.ts
File metadata and controls
55 lines (53 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* `IEmbedder` — protocol-level contract for text → vector providers.
*
* Implemented by embedder plugins (`@objectstack/embedder-openai`,
* `@objectstack/embedder-ollama`, `@objectstack/embedder-transformers-js`,
* …) and consumed by every knowledge adapter that needs to compute
* vectors (e.g. `@objectstack/knowledge-turso`,
* `@objectstack/knowledge-sqlite-vec`).
*
* The surface is deliberately minimal so the same protocol covers
* cloud APIs (OpenAI, 阿里通义, 智谱, 硅基流动, Doubao, …), local
* Ollama daemons, in-process WASM/ONNX runtimes, and any OpenAI-shape
* compatible endpoint. Implementations are responsible for batching,
* retry, and rate-limit handling against their upstream.
*
* Conventions:
* - Output order MUST match input order exactly.
* - Vectors SHOULD be L2-normalised so downstream cosine == dot.
* - `dimensions` MUST be stable for the lifetime of the instance —
* knowledge adapters size their vector columns from this value.
*
* See `content/docs/protocol/knowledge.mdx`.
*/
export interface IEmbedder {
/** Stable id for logs and diagnostics (e.g. `'openai'`, `'ollama'`). */
readonly id: string;
/**
* Output vector dimensionality. Knowledge adapters use this to size
* their fixed-width vector columns / index parameters.
*/
readonly dimensions: number;
/**
* Embed a batch of strings. Output order matches input order.
* Implementations SHOULD handle empty input by returning `[]`.
*/
embed(texts: string[]): Promise<number[][]>;
}
/**
* DI service token for the kernel-registered `IEmbedder` instance.
*
* Plugins that need an embedder (e.g. `@objectstack/knowledge-turso`,
* `@objectstack/knowledge-sqlite-vec`) SHOULD prefer resolving this
* service over taking the embedder as a constructor option, so
* operators can configure the embedder once (in `Settings → AI &
* Embedder`) and have every knowledge adapter pick it up.
*
* Registered by `@objectstack/service-ai` when the operator selects a
* non-`none` embedder provider in settings. If absent, knowledge
* adapters fall back to their constructor-supplied embedder (or refuse
* to start).
*/
export const EMBEDDER_SERVICE = 'embedder' as const;