-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathknowledge-adapter.ts
More file actions
74 lines (65 loc) · 2.57 KB
/
Copy pathknowledge-adapter.ts
File metadata and controls
74 lines (65 loc) · 2.57 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import type {
KnowledgeDocument,
KnowledgeHit,
} from '../ai/knowledge-document.zod';
import type { KnowledgeSource } from '../ai/knowledge-source.zod';
/**
* `IKnowledgeAdapter` — plugin-side contract.
*
* Each backend (RAGFlow, LlamaIndex, Dify, Vectara, an in-memory
* cosine adapter, a custom pgvector adapter, …) ships as its own
* plugin package and implements this interface. The adapter is then
* registered with the host's `IKnowledgeService` via
* `registerAdapter(id, this)` during the plugin's `start()` hook.
*
* The surface is **deliberately minimal**: no chunk strategy, no
* embedding configuration, no rerank toggles. Adapters that need
* extra knobs expose them via their plugin options object.
*
* See `content/docs/protocol/knowledge.mdx`.
*/
/**
* Context passed to every adapter call. Carries the resolved source
* (so the adapter can locate its dataset / collection / table) and
* optional trace metadata. Adapters MUST treat unknown fields as
* forwards-compatible.
*/
export interface AdapterContext {
/** The source these documents belong to. */
source: KnowledgeSource;
/** Trace id for correlation across logs / metrics. */
traceId?: string;
/** Caller-supplied free-form diagnostics tag. */
reason?: 'event-sync' | 'reindex' | 'manual' | 'tool-call' | (string & {});
}
/** Options accepted by `IKnowledgeAdapter.search`. */
export interface AdapterSearchOptions {
/** The source the search is scoped to. */
source: KnowledgeSource;
/** Max hits to return. */
topK: number;
/** Adapter-specific filter (e.g. `{ status: 'active' }`). */
filter?: Record<string, unknown>;
/** Trace id for correlation. */
traceId?: string;
}
/**
* Plugin-implemented adapter. **One per backend**. Stateless across
* source ids — the source is supplied per-call.
*/
export interface IKnowledgeAdapter {
/** Stable adapter id used in `KnowledgeSource.adapter`. */
readonly id: string;
/** Insert or replace documents in the adapter's backend. */
upsert(docs: KnowledgeDocument[], ctx: AdapterContext): Promise<void>;
/** Run a search query. Returns hits sorted by descending score. */
search(query: string, opts: AdapterSearchOptions): Promise<KnowledgeHit[]>;
/** Remove documents by id. */
delete(documentIds: string[], ctx: AdapterContext): Promise<void>;
/**
* Optional liveness probe. When implemented, used by the service's
* health endpoint and Studio "test connection" actions.
*/
healthCheck?(): Promise<{ ok: boolean; message?: string }>;
}