@nixxie-cms/ai-rag 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +163 -0
  3. package/dist/declarations/src/AiRagService.d.ts +50 -0
  4. package/dist/declarations/src/AiRagService.d.ts.map +1 -0
  5. package/dist/declarations/src/admin-page.d.ts +29 -0
  6. package/dist/declarations/src/admin-page.d.ts.map +1 -0
  7. package/dist/declarations/src/chunking.d.ts +8 -0
  8. package/dist/declarations/src/chunking.d.ts.map +1 -0
  9. package/dist/declarations/src/collection.d.ts +18 -0
  10. package/dist/declarations/src/collection.d.ts.map +1 -0
  11. package/dist/declarations/src/express.d.ts +36 -0
  12. package/dist/declarations/src/express.d.ts.map +1 -0
  13. package/dist/declarations/src/graphql.d.ts +23 -0
  14. package/dist/declarations/src/graphql.d.ts.map +1 -0
  15. package/dist/declarations/src/index.d.ts +39 -0
  16. package/dist/declarations/src/index.d.ts.map +1 -0
  17. package/dist/declarations/src/plugin.d.ts +53 -0
  18. package/dist/declarations/src/plugin.d.ts.map +1 -0
  19. package/dist/declarations/src/prompt.d.ts +14 -0
  20. package/dist/declarations/src/prompt.d.ts.map +1 -0
  21. package/dist/declarations/src/providers/AnthropicRagProvider.d.ts +16 -0
  22. package/dist/declarations/src/providers/AnthropicRagProvider.d.ts.map +1 -0
  23. package/dist/declarations/src/providers/GeminiRagProvider.d.ts +19 -0
  24. package/dist/declarations/src/providers/GeminiRagProvider.d.ts.map +1 -0
  25. package/dist/declarations/src/providers/OllamaRagProvider.d.ts +23 -0
  26. package/dist/declarations/src/providers/OllamaRagProvider.d.ts.map +1 -0
  27. package/dist/declarations/src/providers/OpenAiRagProvider.d.ts +17 -0
  28. package/dist/declarations/src/providers/OpenAiRagProvider.d.ts.map +1 -0
  29. package/dist/declarations/src/providers/ServiceRagProvider.d.ts +17 -0
  30. package/dist/declarations/src/providers/ServiceRagProvider.d.ts.map +1 -0
  31. package/dist/declarations/src/providers/index.d.ts +14 -0
  32. package/dist/declarations/src/providers/index.d.ts.map +1 -0
  33. package/dist/declarations/src/providers/types.d.ts +45 -0
  34. package/dist/declarations/src/providers/types.d.ts.map +1 -0
  35. package/dist/declarations/src/similarity.d.ts +12 -0
  36. package/dist/declarations/src/similarity.d.ts.map +1 -0
  37. package/dist/declarations/src/types.d.ts +319 -0
  38. package/dist/declarations/src/types.d.ts.map +1 -0
  39. package/dist/declarations/src/vector-store.d.ts +34 -0
  40. package/dist/declarations/src/vector-store.d.ts.map +1 -0
  41. package/dist/nixxie-cms-ai-rag.cjs.d.ts +2 -0
  42. package/dist/nixxie-cms-ai-rag.cjs.js +2507 -0
  43. package/dist/nixxie-cms-ai-rag.esm.js +2481 -0
  44. package/package.json +37 -0
  45. package/src/AiRagService.ts +640 -0
  46. package/src/admin-page.ts +135 -0
  47. package/src/chunking.ts +78 -0
  48. package/src/collection.ts +79 -0
  49. package/src/express.ts +212 -0
  50. package/src/graphql.ts +196 -0
  51. package/src/guard.ts +75 -0
  52. package/src/index.ts +102 -0
  53. package/src/plugin.ts +162 -0
  54. package/src/prompt.ts +62 -0
  55. package/src/providers/AnthropicRagProvider.ts +91 -0
  56. package/src/providers/GeminiRagProvider.ts +147 -0
  57. package/src/providers/OllamaRagProvider.ts +157 -0
  58. package/src/providers/OpenAiRagProvider.ts +108 -0
  59. package/src/providers/ServiceRagProvider.ts +44 -0
  60. package/src/providers/index.ts +67 -0
  61. package/src/providers/types.ts +44 -0
  62. package/src/semaphore.ts +26 -0
  63. package/src/similarity.ts +31 -0
  64. package/src/types.ts +346 -0
  65. package/src/vector-store.ts +136 -0
@@ -0,0 +1,14 @@
1
+ import type { RagEmbeddingConfig, RagGenerationConfig } from "../types.js";
2
+ import { AnthropicRagProvider } from "./AnthropicRagProvider.js";
3
+ import { GeminiRagProvider } from "./GeminiRagProvider.js";
4
+ import { OllamaRagProvider } from "./OllamaRagProvider.js";
5
+ import { OpenAiRagProvider } from "./OpenAiRagProvider.js";
6
+ import { ServiceRagProvider } from "./ServiceRagProvider.js";
7
+ import type { EmbeddingProvider, GenerationProvider } from "./types.js";
8
+ /** Build the generation provider from config (default provider: anthropic). */
9
+ export declare function resolveGenerationProvider(config?: RagGenerationConfig): GenerationProvider;
10
+ /** Build the embedding provider from config (default provider: openai). */
11
+ export declare function resolveEmbeddingProvider(config?: RagEmbeddingConfig): EmbeddingProvider;
12
+ export { AnthropicRagProvider, OpenAiRagProvider, GeminiRagProvider, OllamaRagProvider, ServiceRagProvider, };
13
+ export type { GenerationProvider, EmbeddingProvider, RagMessage, RagGenerateOptions, RagGenerateResult, RagStreamChunk, RagUsage, } from "./types.js";
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"../../../../src/providers","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,mBAAmB,EAAmB,oBAAgB;AACxF,OAAO,EAAE,oBAAoB,EAAE,kCAA8B;AAC7D,OAAO,EAAE,iBAAiB,EAAE,+BAA2B;AACvD,OAAO,EAAE,iBAAiB,EAAE,+BAA2B;AACvD,OAAO,EAAE,iBAAiB,EAAE,+BAA2B;AACvD,OAAO,EAAE,kBAAkB,EAAE,gCAA4B;AACzD,OAAO,KAAK,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,mBAAe;AAEpE,+EAA+E;AAC/E,wBAAgB,yBAAyB,CAAC,MAAM,GAAE,mBAAwB,GAAG,kBAAkB,CAiB9F;AAED,2EAA2E;AAC3E,wBAAgB,wBAAwB,CAAC,MAAM,GAAE,kBAAuB,GAAG,iBAAiB,CAoB3F;AAED,OAAO,EACL,oBAAoB,EACpB,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,kBAAkB,GACnB,CAAA;AACD,YAAY,EACV,kBAAkB,EAClB,iBAAiB,EACjB,UAAU,EACV,kBAAkB,EAClB,iBAAiB,EACjB,cAAc,EACd,QAAQ,GACT,mBAAe"}
@@ -0,0 +1,45 @@
1
+ /** A single chat turn handed to a provider. */
2
+ export type RagMessage = {
3
+ role: 'user' | 'assistant';
4
+ content: string;
5
+ };
6
+ export type RagGenerateOptions = {
7
+ model?: string;
8
+ system?: string;
9
+ temperature?: number;
10
+ maxTokens?: number;
11
+ topP?: number;
12
+ /** Provider-specific extras merged into the request body. */
13
+ extra?: Record<string, unknown>;
14
+ };
15
+ export type RagUsage = {
16
+ inputTokens?: number;
17
+ outputTokens?: number;
18
+ };
19
+ export type RagGenerateResult = {
20
+ text: string;
21
+ model: string;
22
+ usage?: RagUsage;
23
+ };
24
+ /** One streamed delta. The final chunk carries `done: true` and any usage. */
25
+ export type RagStreamChunk = {
26
+ delta?: string;
27
+ done?: boolean;
28
+ usage?: RagUsage;
29
+ model?: string;
30
+ };
31
+ /** A provider that can answer (and ideally stream) a chat. */
32
+ export interface GenerationProvider {
33
+ readonly name: string;
34
+ readonly defaultModel: string;
35
+ generate(messages: RagMessage[], options?: RagGenerateOptions): Promise<RagGenerateResult>;
36
+ /** Optional native streaming. When absent, the service emulates it from `generate`. */
37
+ stream?(messages: RagMessage[], options?: RagGenerateOptions): AsyncIterable<RagStreamChunk>;
38
+ }
39
+ /** A provider that can turn text into embedding vectors. */
40
+ export interface EmbeddingProvider {
41
+ readonly name: string;
42
+ readonly defaultModel: string;
43
+ embed(texts: string[], model?: string): Promise<number[][]>;
44
+ }
45
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"../../../../src/providers","sources":["types.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAC/C,MAAM,MAAM,UAAU,GAAG;IAAE,IAAI,EAAE,MAAM,GAAG,WAAW,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAA;AAExE,MAAM,MAAM,kBAAkB,GAAG;IAC/B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,MAA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,EAAE,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;CAC5D"}
@@ -0,0 +1,12 @@
1
+ /** Dot product of two equal-length vectors. */
2
+ export declare function dot(a: number[], b: number[]): number;
3
+ /** Euclidean norm of a vector. */
4
+ export declare function norm(a: number[]): number;
5
+ /**
6
+ * Cosine similarity mapped from [-1, 1] into [0, 1] so it can be used as a relevance
7
+ * score and compared against a `minScore` threshold. Returns 0 for a zero vector.
8
+ */
9
+ export declare function cosineSimilarity(a: number[], b: number[]): number;
10
+ /** Pre-normalise a vector to unit length (lets retrieval use a plain dot product). */
11
+ export declare function normalize(a: number[]): number[];
12
+ //# sourceMappingURL=similarity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"similarity.d.ts","sourceRoot":"../../../src","sources":["similarity.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAC/C,wBAAgB,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAKpD;AAED,kCAAkC;AAClC,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAExC;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAMjE;AAED,sFAAsF;AACtF,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAI/C"}
@@ -0,0 +1,319 @@
1
+ import type { NixxieAiService, NixxieRagChunk, NixxieRagCitation } from '@nixxie-cms/core';
2
+ export type { NixxieAiRagService, NixxieRagDocument, NixxieRagDocumentInput, NixxieRagDocumentQuery, NixxieRagChunk, NixxieRagCitation, NixxieRagAnswer, NixxieRagAskOptions, NixxieRagRetrieveOptions, NixxieRagStreamEvent, NixxieRagIndexStats, } from '@nixxie-cms/core';
3
+ /** Supported model providers for generation and/or embeddings. */
4
+ export type RagProviderName = 'anthropic' | 'openai' | 'gemini' | 'ollama';
5
+ /**
6
+ * How to talk to a model provider. Either point at an existing `NixxieAiService`
7
+ * (e.g. the one configured as `context.services.ai`) via `service`, or give the
8
+ * provider + credentials and let ai-rag build the client.
9
+ */
10
+ export type RagProviderConfig = {
11
+ /** Provider to use. Defaults to 'anthropic' for generation, 'openai' for embeddings. */
12
+ provider?: RagProviderName;
13
+ /** API key. Not required for `ollama` (local server) or when `service` is given. */
14
+ apiKey?: string;
15
+ /** Model id. Sensible per-provider defaults are used when omitted. */
16
+ model?: string;
17
+ /**
18
+ * Override the API base URL. Required for `ollama` if it is not on
19
+ * http://localhost:11434. Also used for gateways/proxies/Azure.
20
+ */
21
+ baseUrl?: string;
22
+ /**
23
+ * Reuse an already-constructed service instead of building a client. Handy for
24
+ * sharing `context.services.ai`. When set, other fields are ignored for that role.
25
+ */
26
+ service?: NixxieAiService;
27
+ /** Extra provider-specific options merged into each request body. */
28
+ extra?: Record<string, unknown>;
29
+ };
30
+ /** Generation (answering) model configuration. */
31
+ export type RagGenerationConfig = RagProviderConfig & {
32
+ /**
33
+ * Sampling temperature. Lower is more grounded/deterministic.
34
+ * @default 0.2
35
+ */
36
+ temperature?: number;
37
+ /**
38
+ * Maximum output tokens per answer.
39
+ * @default 1024
40
+ */
41
+ maxTokens?: number;
42
+ /** Nucleus sampling cutoff, passed through when the provider supports it. */
43
+ topP?: number;
44
+ /**
45
+ * Base system prompt. The retrieved context and grounding rules are appended to it.
46
+ * @default a strict, citation-first assistant prompt
47
+ */
48
+ systemPrompt?: string;
49
+ /**
50
+ * Fully override how the final prompt is assembled from the question + retrieved
51
+ * chunks. Return the messages sent to the model. When omitted, a built-in template
52
+ * is used that injects numbered sources and citation instructions.
53
+ */
54
+ buildPrompt?: (args: PromptBuildArgs) => PromptBuildResult;
55
+ };
56
+ /** Embedding model configuration (often a different — even local — provider). */
57
+ export type RagEmbeddingConfig = RagProviderConfig & {
58
+ /**
59
+ * Expected embedding dimensionality. Used to validate stored vectors and to detect
60
+ * a model change that requires a full reindex. Optional — inferred on first embed.
61
+ */
62
+ dimensions?: number;
63
+ /**
64
+ * How many chunks to embed per provider call.
65
+ * @default 64
66
+ */
67
+ batchSize?: number;
68
+ };
69
+ /** Where embeddings live and how retrieval searches them. */
70
+ export type RagRetrievalConfig = {
71
+ /**
72
+ * Number of chunks fed to the model as context.
73
+ * @default 5
74
+ */
75
+ topK?: number;
76
+ /**
77
+ * Minimum cosine similarity (0–1) for a chunk to be considered relevant. Chunks below
78
+ * this are dropped; if nothing clears the bar the guard can refuse.
79
+ * @default 0.2
80
+ */
81
+ minScore?: number;
82
+ /**
83
+ * Cap the total characters of retrieved context injected into the prompt.
84
+ * @default 6000
85
+ */
86
+ maxContextChars?: number;
87
+ /**
88
+ * Pluggable vector store. Defaults to a SQL-backed store over the chunk collection
89
+ * (works on any database) doing cosine similarity in Node. Swap for pgvector or an
90
+ * external vector DB by implementing `VectorStore`.
91
+ */
92
+ vectorStore?: VectorStore;
93
+ /**
94
+ * Optional second-pass reranker over the candidate chunks (e.g. a cross-encoder).
95
+ * Receives the query and the top candidates; returns them re-scored/re-ordered.
96
+ */
97
+ rerank?: (query: string, chunks: NixxieRagChunk[]) => Promise<NixxieRagChunk[]>;
98
+ /**
99
+ * Over-fetch this multiple of `topK` before reranking/scoring.
100
+ * @default 4
101
+ */
102
+ candidateMultiplier?: number;
103
+ };
104
+ export type RagChunkingStrategy = 'recursive' | 'sentence' | 'fixed';
105
+ /** How documents are split into chunks before embedding. */
106
+ export type RagChunkingConfig = {
107
+ /**
108
+ * Splitting strategy. 'recursive' splits on paragraph→line→sentence→word boundaries
109
+ * to keep chunks coherent; 'sentence' groups whole sentences; 'fixed' is a hard window.
110
+ * @default 'recursive'
111
+ */
112
+ strategy?: RagChunkingStrategy;
113
+ /**
114
+ * Target chunk size in characters.
115
+ * @default 1200
116
+ */
117
+ chunkSize?: number;
118
+ /**
119
+ * Overlap in characters between consecutive chunks (preserves context across cuts).
120
+ * @default 200
121
+ */
122
+ chunkOverlap?: number;
123
+ };
124
+ /** Indexing behaviour and scheduling. */
125
+ export type RagIndexingConfig = {
126
+ /**
127
+ * Re-embed a document automatically when its KB row is created/updated and remove its
128
+ * chunks when the row is deleted (wired by `ragPlugin()` via collection hooks).
129
+ * @default true
130
+ */
131
+ auto?: boolean;
132
+ /**
133
+ * Run a full reindex on a schedule. Cron expression or interval in milliseconds.
134
+ * Requires the jobs service (`context.services.jobs`); the plugin registers the job.
135
+ * @example '0 3 * * *' // 3am daily
136
+ */
137
+ schedule?: string | number;
138
+ /**
139
+ * Index any pending/changed documents once, right after the database connects.
140
+ * @default true
141
+ */
142
+ onConnect?: boolean;
143
+ /**
144
+ * How many documents to index concurrently.
145
+ * @default 4
146
+ */
147
+ concurrency?: number;
148
+ };
149
+ /** Hallucination guarding — keep answers grounded in the knowledge base. */
150
+ export type RagGuardConfig = {
151
+ /**
152
+ * Master switch for all guarding below.
153
+ * @default true
154
+ */
155
+ enabled?: boolean;
156
+ /**
157
+ * Refuse to answer (returning `refusal`) when the best retrieved chunk scores below
158
+ * `minScore`, i.e. the KB has nothing relevant.
159
+ * @default true
160
+ */
161
+ refuseWhenNoContext?: boolean;
162
+ /**
163
+ * Message returned when the assistant refuses for lack of grounding.
164
+ * @default "I don't have enough information in my knowledge base to answer that."
165
+ */
166
+ refusal?: string;
167
+ /**
168
+ * Instruct the model to cite sources inline as [n] and only use provided context.
169
+ * @default true
170
+ */
171
+ requireCitations?: boolean;
172
+ /**
173
+ * Run a second, cheap model pass that checks the drafted answer is supported by the
174
+ * retrieved context and strips/flags unsupported claims. Costs an extra call.
175
+ * @default false
176
+ */
177
+ groundingCheck?: boolean;
178
+ /** Model id for the grounding check. Defaults to the generation model. */
179
+ groundingModel?: string;
180
+ /**
181
+ * If the grounding check finds the answer is not supported, replace it with `refusal`
182
+ * instead of returning the ungrounded text.
183
+ * @default true
184
+ */
185
+ refuseWhenUngrounded?: boolean;
186
+ /**
187
+ * Allow the model to fall back to its own world knowledge when no context is found
188
+ * (sets a softer prompt). Off by default for a strictly grounded assistant.
189
+ * @default false
190
+ */
191
+ allowModelKnowledge?: boolean;
192
+ };
193
+ /** Conversation handling. */
194
+ export type RagChatConfig = {
195
+ /**
196
+ * Maximum prior turns kept when building the prompt (excludes the system prompt).
197
+ * @default 10
198
+ */
199
+ historyLimit?: number;
200
+ /**
201
+ * Default to streaming responses where supported (the HTTP route always can stream).
202
+ * @default true
203
+ */
204
+ streaming?: boolean;
205
+ };
206
+ /** Hard limits and safety rails. */
207
+ export type RagLimitsConfig = {
208
+ /**
209
+ * Reject queries longer than this many characters before doing any work.
210
+ * @default 8000
211
+ */
212
+ maxQueryChars?: number;
213
+ /**
214
+ * Maximum concurrent in-flight generations. Excess calls wait.
215
+ * @default 8
216
+ */
217
+ maxConcurrentChats?: number;
218
+ };
219
+ /** Names of the collections ai-rag reads/writes. The plugin can create them for you. */
220
+ export type RagCollectionsConfig = {
221
+ /**
222
+ * Collection holding source documents (the KB rows users add).
223
+ * @default 'KnowledgeBase'
224
+ */
225
+ documents?: string;
226
+ /**
227
+ * Collection holding indexed chunks + embeddings.
228
+ * @default 'KnowledgeChunk'
229
+ */
230
+ chunks?: string;
231
+ };
232
+ /** The full, flexible configuration for `createAiRag()`. */
233
+ export type AiRagConfig = {
234
+ /** Collection names backing the knowledge base. */
235
+ collections?: RagCollectionsConfig;
236
+ /** Generation (answering) model. Defaults to Anthropic Claude. */
237
+ generation?: RagGenerationConfig;
238
+ /** Embedding model. Defaults to OpenAI text-embedding-3-small. */
239
+ embedding?: RagEmbeddingConfig;
240
+ /** Retrieval + vector store settings. */
241
+ retrieval?: RagRetrievalConfig;
242
+ /** Document chunking settings. */
243
+ chunking?: RagChunkingConfig;
244
+ /** Indexing behaviour and scheduling. */
245
+ indexing?: RagIndexingConfig;
246
+ /** Hallucination guarding. */
247
+ guard?: RagGuardConfig;
248
+ /** Conversation handling. */
249
+ chat?: RagChatConfig;
250
+ /** Hard limits and safety rails. */
251
+ limits?: RagLimitsConfig;
252
+ };
253
+ export type PromptBuildArgs = {
254
+ /** The user's latest question. */
255
+ question: string;
256
+ /** Prior conversation turns (already trimmed to `chat.historyLimit`). */
257
+ history: {
258
+ role: 'user' | 'assistant';
259
+ content: string;
260
+ }[];
261
+ /** The retrieved, scored chunks chosen as context. */
262
+ context: NixxieRagChunk[];
263
+ /** The resolved base system prompt. */
264
+ systemPrompt: string;
265
+ /** Whether citations are required by the guard. */
266
+ requireCitations: boolean;
267
+ };
268
+ export type PromptBuildResult = {
269
+ system: string;
270
+ messages: {
271
+ role: 'user' | 'assistant';
272
+ content: string;
273
+ }[];
274
+ };
275
+ /** A stored chunk + its embedding, as persisted by a `VectorStore`. */
276
+ export type VectorRecord = {
277
+ id: string;
278
+ documentId: string;
279
+ content: string;
280
+ embedding: number[];
281
+ title?: string;
282
+ source?: string;
283
+ tags?: string[];
284
+ metadata?: Record<string, unknown>;
285
+ };
286
+ export type VectorQuery = {
287
+ /** The query embedding. */
288
+ embedding: number[];
289
+ /** Max results. */
290
+ topK: number;
291
+ /** Only match chunks whose document carries ALL of these tags. */
292
+ tags?: string[];
293
+ /** Minimum score to return. */
294
+ minScore?: number;
295
+ };
296
+ /**
297
+ * Pluggable similarity store. The default `SqlVectorStore` keeps vectors in the chunk
298
+ * collection and scores them in Node; implement this interface to back retrieval with
299
+ * pgvector, Qdrant, Pinecone, etc.
300
+ */
301
+ export type VectorStore = {
302
+ /** Called once after the database connects, with a sudo context. */
303
+ init?(context: import('@nixxie-cms/core').NixxieContext): Promise<void> | void;
304
+ /** Insert or replace all chunks for a document (replacing any previous chunks). */
305
+ upsert(documentId: string, records: VectorRecord[]): Promise<void>;
306
+ /** Delete every chunk belonging to a document. */
307
+ deleteByDocument(documentId: string): Promise<void>;
308
+ /** Return the closest chunks to the query embedding, scored in [0,1]. */
309
+ query(query: VectorQuery): Promise<Array<VectorRecord & {
310
+ score: number;
311
+ }>>;
312
+ /** Total number of indexed chunks (optional; used for stats). */
313
+ count?(): Promise<number>;
314
+ /** Remove everything (used by a forced full reindex). */
315
+ clear?(): Promise<void>;
316
+ };
317
+ /** Internal: the fully-resolved configuration with all defaults applied. */
318
+ export type ResolvedRagCitation = NixxieRagCitation;
319
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"../../../src","sources":["types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAE1F,YAAY,EACV,kBAAkB,EAClB,iBAAiB,EACjB,sBAAsB,EACtB,sBAAsB,EACtB,cAAc,EACd,iBAAiB,EACjB,eAAe,EACf,mBAAmB,EACnB,wBAAwB,EACxB,oBAAoB,EACpB,mBAAmB,GACpB,MAAM,kBAAkB,CAAA;AAEzB,kEAAkE;AAClE,MAAM,MAAM,eAAe,GAAG,WAAW,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAA;AAE1E;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,wFAAwF;IACxF,QAAQ,CAAC,EAAE,eAAe,CAAA;IAC1B,oFAAoF;IACpF,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,sEAAsE;IACtE,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;OAGG;IACH,OAAO,CAAC,EAAE,eAAe,CAAA;IACzB,qEAAqE;IACrE,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAChC,CAAA;AAED,kDAAkD;AAClD,MAAM,MAAM,mBAAmB,GAAG,iBAAiB,GAAG;IACpD;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,6EAA6E;IAC7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E,OAAO,CAAC,CAAA;CACnC,CAAA;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,2BAA2B;IAC3B,SAAS,EAAE,MAAM,EAAE,CAAA;IACnB,mBAAmB;IACnB,IAAI,EAAE,MAAM,CAAA;IACZ,kEAAkE;IAClE,IAAI,CAAC,EAAE,MAAM,EAAE,CAAA;IACf,+BAA+B;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,oEAAoE;IACpE,IAAI,CAAC,CAAC,OAAO,EAAE,OAAO,kBAAkB,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;IAC9E,mFAAmF;IACnF,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAClE,kDAAkD;IAClD,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACnD,yEAAyE;IACzE,KAAK,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,KAAK,CAAC,YAAY,GAAG;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC,CAAA;IAC3E,iEAAiE;IACjE,KAAK,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,CAAA;IACzB,yDAAyD;IACzD,KAAK,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACxB,CAAA;AAED,4EAA4E;AAC5E,MAAM,MAAM,mBAAmB,GAAG,iBAAiB,CAAA"}
@@ -0,0 +1,34 @@
1
+ import type { NixxieContext } from '@nixxie-cms/core';
2
+ import type { VectorQuery, VectorRecord, VectorStore } from "./types.js";
3
+ /**
4
+ * Default vector store: persists embeddings in the chunk collection in the host app's own
5
+ * database (any provider) and scores candidates with cosine similarity in Node. Simple and
6
+ * portable; for very large knowledge bases swap in a pgvector / external store via
7
+ * `retrieval.vectorStore`.
8
+ */
9
+ export declare class SqlVectorStore implements VectorStore {
10
+ private collection;
11
+ private model;
12
+ constructor(collection: string);
13
+ init(context: NixxieContext): void;
14
+ private requireModel;
15
+ upsert(documentId: string, records: VectorRecord[]): Promise<void>;
16
+ deleteByDocument(documentId: string): Promise<void>;
17
+ query(query: VectorQuery): Promise<Array<VectorRecord & {
18
+ score: number;
19
+ }>>;
20
+ count(): Promise<number>;
21
+ clear(): Promise<void>;
22
+ }
23
+ /** Ephemeral in-process vector store. Useful for tests and small/transient knowledge bases. */
24
+ export declare class InMemoryVectorStore implements VectorStore {
25
+ private byDocument;
26
+ upsert(documentId: string, records: VectorRecord[]): Promise<void>;
27
+ deleteByDocument(documentId: string): Promise<void>;
28
+ query(query: VectorQuery): Promise<Array<VectorRecord & {
29
+ score: number;
30
+ }>>;
31
+ count(): Promise<number>;
32
+ clear(): Promise<void>;
33
+ }
34
+ //# sourceMappingURL=vector-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vector-store.d.ts","sourceRoot":"../../../src","sources":["vector-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAErD,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,EAAE,mBAAe;AA6BrE;;;;;GAKG;AACH,qBAAa,cAAe,YAAW,WAAW;IAGpC,OAAO,CAAC,UAAU;IAF9B,OAAO,CAAC,KAAK,CAAmB;gBAEZ,UAAU,EAAE,MAAM;IAEtC,IAAI,CAAC,OAAO,EAAE,aAAa,GAAG,IAAI;IAIlC,OAAO,CAAC,YAAY;IASd,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAqBlE,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAInD,KAAK,CAAC,KAAK,EAAE,WAAW,GA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