@denizsvnc/ai-rag-engine 0.0.0-stage → 2.2.1

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 (71) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/LICENSE +21 -0
  3. package/README.md +167 -2
  4. package/dist/adapters/cache/index.d.ts +1 -0
  5. package/dist/adapters/cache/redis-cache.adapter.d.ts +49 -0
  6. package/dist/adapters/faq/local-faq.adapter.d.ts +29 -0
  7. package/dist/adapters/faq/url-faq.adapter.d.ts +14 -0
  8. package/dist/adapters/llm/custom.adapter.d.ts +42 -0
  9. package/dist/adapters/llm/fallback.adapter.d.ts +10 -0
  10. package/dist/adapters/llm/gemini.adapter.d.ts +9 -0
  11. package/dist/adapters/llm/ollama.adapter.d.ts +30 -0
  12. package/dist/adapters/llm/openai.adapter.d.ts +10 -0
  13. package/dist/adapters/llm/openrouter.adapter.d.ts +6 -0
  14. package/dist/adapters/llm/tiered.adapter.d.ts +22 -0
  15. package/dist/adapters/rules/static-rules.adapter.d.ts +6 -0
  16. package/dist/adapters/vector/memory-vector.adapter.d.ts +33 -0
  17. package/dist/adapters/vector/postgres-vector.adapter.d.ts +51 -0
  18. package/dist/api/auth.d.ts +47 -0
  19. package/dist/api/middleware.d.ts +3 -0
  20. package/dist/api/rate-limit.d.ts +8 -0
  21. package/dist/api/routes.d.ts +9 -0
  22. package/dist/api/server.d.ts +1 -0
  23. package/dist/api/server.js +4433 -0
  24. package/dist/config/default.d.ts +22 -0
  25. package/dist/config/ingestion-runtime.d.ts +11 -0
  26. package/dist/core/cache-manager.d.ts +29 -0
  27. package/dist/core/engine.d.ts +75 -0
  28. package/dist/core/ingestion/ingestion-worker.d.ts +19 -0
  29. package/dist/core/ingestion/postgres-ingestion-store.d.ts +56 -0
  30. package/dist/core/ingestion/queued-ingestion.service.d.ts +35 -0
  31. package/dist/core/metrics/prometheus-exporter.d.ts +12 -0
  32. package/dist/core/metrics/rag-evaluator.d.ts +63 -0
  33. package/dist/core/metrics/token-tracker.d.ts +69 -0
  34. package/dist/core/prompt-builder.d.ts +20 -0
  35. package/dist/core/queue/bullmq-queue.d.ts +39 -0
  36. package/dist/core/queue/in-memory-queue.d.ts +24 -0
  37. package/dist/core/queue/job-queue.interface.d.ts +22 -0
  38. package/dist/core/queue/queue-factory.d.ts +12 -0
  39. package/dist/core/security/confirmation.d.ts +64 -0
  40. package/dist/core/security/injection-detector.d.ts +25 -0
  41. package/dist/core/security/nonce-store.d.ts +21 -0
  42. package/dist/core/security/pii-guard.d.ts +38 -0
  43. package/dist/core/security/sanitizer.d.ts +23 -0
  44. package/dist/core/security/secrets.d.ts +2 -0
  45. package/dist/core/security/security-guard.d.ts +18 -0
  46. package/dist/core/security/security.types.d.ts +38 -0
  47. package/dist/core/sml-engine.d.ts +65 -0
  48. package/dist/core/tool-registry.d.ts +17 -0
  49. package/dist/core/vector/bm25.d.ts +44 -0
  50. package/dist/core/vector/hybrid-search.d.ts +15 -0
  51. package/dist/index.d.ts +51 -0
  52. package/dist/index.js +4509 -0
  53. package/dist/interfaces/cache.interface.d.ts +8 -0
  54. package/dist/interfaces/engine-config.interface.d.ts +73 -0
  55. package/dist/interfaces/faq.interface.d.ts +17 -0
  56. package/dist/interfaces/ingestion.interface.d.ts +14 -0
  57. package/dist/interfaces/llm.interface.d.ts +22 -0
  58. package/dist/interfaces/query-context.interface.d.ts +33 -0
  59. package/dist/interfaces/rules.interface.d.ts +3 -0
  60. package/dist/interfaces/tool.interface.d.ts +50 -0
  61. package/dist/interfaces/vector.interface.d.ts +15 -0
  62. package/dist/scripts/migrate-vector.d.ts +1 -0
  63. package/dist/scripts/migrate-vector.js +627 -0
  64. package/dist/services/document-ingestion.service.d.ts +116 -0
  65. package/dist/utils/http.d.ts +2 -0
  66. package/dist/utils/math.d.ts +3 -0
  67. package/dist/worker.d.ts +1 -0
  68. package/dist/worker.js +1787 -0
  69. package/docs/RELEASING.md +55 -0
  70. package/docs/USAGE.md +154 -0
  71. package/package.json +95 -4
package/CHANGELOG.md ADDED
@@ -0,0 +1,43 @@
1
+ # Changelog
2
+
3
+ This file follows Semantic Versioning.
4
+
5
+ ## 2.2.1 — 2026-11-10
6
+
7
+ ### Documentation
8
+
9
+ - Updated `README.md`, `README_tr.md`, and `README_de.md` with verified, type-safe production examples featuring `GeminiAdapter`, `PostgresVectorAdapter`, `RedisCacheAdapter`, `LocalFaqAdapter`, and `SecurityConfig`.
10
+ - Removed legacy pre-publication disclaimers and streamlined installation documentation for live npm consumption.
11
+
12
+ ## 2.2.0 — 2026-11-10
13
+
14
+ ### Added
15
+
16
+ - Scoped npm distribution preparation for `@denizsvnc/ai-rag-engine`, explicit Node.js/ESM contract and package metadata.
17
+ - Package allowlist verification and independent JavaScript/strict TypeScript consumer tests.
18
+ - PostgreSQL/pgvector retrieval, document version manifests and revision-aware shared query caching.
19
+ - Separate BullMQ ingestion worker, shared PostgreSQL upload/outbox, tenant quotas, retries and crash recovery.
20
+ - Redis tenant rate limiting, provider timeouts and latency histogram accounting.
21
+
22
+ ### Fixed
23
+
24
+ - Root import no longer eagerly loads Transformers or mutates its global cache configuration.
25
+ - Builds clean stale artifacts, target Node-compatible JavaScript and omit broken declaration maps/demo data.
26
+ - Public declarations resolve under NodeNext; runtime TypeScript tools are unnecessary.
27
+ - Newly written local cache and tool-registry files use POSIX mode 0600.
28
+
29
+ ### Migration notes
30
+
31
+ - Package identity changes from the repository's unscoped `ai-rag-engine` to `@denizsvnc/ai-rag-engine`. Update imports and installation commands. Registry scope ownership remains unverified until the owner authenticates.
32
+ - Node.js 20 is no longer in the supported matrix; use Node.js 22.14+.
33
+ - `LocalFaqAdapter()` starts empty. Supply `filePath` or call `setFaqs()`; checkout demo FAQs are not business defaults.
34
+ - Internal `dist/` files are private; public entry points remain the root API and the documented CLI commands.
35
+ - `SMLEngine.cacheDir` / `RAG_MODEL_CACHE_DIR` applies per instance; the SDK no longer sets Transformers `env.cacheDir` globally.
36
+ - Persistent ingestion/vector storage requires migrations and shared settings across API/worker.
37
+
38
+ ## 2.1.0 — Repository history (not verified as an npm release)
39
+
40
+ - JWT authentication and RBAC, tenant boundaries and signed single-use tool confirmation.
41
+ - Semantic routing, FAQ matching, hybrid retrieval, local queue and telemetry APIs.
42
+
43
+ The public registry returned 404 for scoped and unscoped names during this preparation. This is not proof of namespace ownership or publish authorization.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,168 @@
1
- # Temporary Holding Version
1
+ <div align="center">
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ <img src="https://raw.githubusercontent.com/Denizsvnc/ai-rag-engine/main/assets/logo.jpg" alt="AI-RAG-ENGINE Logo" width="160" style="border-radius: 28px; box-shadow: 0 12px 40px rgba(124, 108, 245, 0.45); margin-bottom: 16px;" />
4
+
5
+ # ⚡ AI-RAG-ENGINE
6
+
7
+ **TypeScript RAG library with pluggable LLM and vector adapters, local semantic routing, tenant-aware retrieval, and durable document ingestion.**
8
+
9
+ [![Version](https://img.shields.io/badge/npm-@denizsvnc/ai--rag--engine-6C5CE7?logo=npm&logoColor=white)](https://www.npmjs.com/package/@denizsvnc/ai-rag-engine)
10
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.8+-3178C6?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
11
+ [![Node.js](https://img.shields.io/badge/Node.js-22+-339933?logo=node.js&logoColor=white)](https://nodejs.org/)
12
+ [![CI](https://github.com/Denizsvnc/ai-rag-engine/actions/workflows/ci.yml/badge.svg)](https://github.com/Denizsvnc/ai-rag-engine/actions)
13
+ [![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
14
+
15
+ ---
16
+
17
+ ### 🌐 Documentation Languages
18
+
19
+ **[English](README.md)** • **[Türkçe](README_tr.md)** • **[Deutsch](README_de.md)**
20
+
21
+ ---
22
+
23
+ </div>
24
+
25
+ ## Requirements and installation
26
+
27
+ ESM only. Node.js **22.14 or newer**; the compatibility matrix covers Node 22 and 24. TypeScript consumers should use TypeScript 5.8+ with NodeNext or Bundler resolution. Runtime applications do not need TypeScript or build tools.
28
+
29
+ ```sh
30
+ npm install @denizsvnc/ai-rag-engine
31
+ ```
32
+
33
+ ## Quick Start
34
+
35
+ This deterministic example needs no credentials, model download, HTTP server, or database. The provider and vector adapter are test doubles demonstrating the integration contract; use real adapters for application data.
36
+
37
+ ```js
38
+ import { RAGEngine } from '@denizsvnc/ai-rag-engine';
39
+
40
+ const engine = new RAGEngine({
41
+ smlConfig: { enabled: false },
42
+ cache: { enabled: false },
43
+ inputs: {
44
+ vectorStore: {
45
+ async search(_query, _topK, filter) {
46
+ if (filter?.tenantId !== 'demo') return [];
47
+ return [{ id: 'returns', content: 'Returns are accepted within 14 days.' }];
48
+ }
49
+ }
50
+ },
51
+ llmProvider: {
52
+ async generate({ systemPrompt }) {
53
+ return { content: systemPrompt.includes('14 days') ? 'Returns: 14 days.' : 'Unknown.' };
54
+ }
55
+ }
56
+ });
57
+
58
+ await engine.init();
59
+ const result = await engine.query('What is the return window?', {
60
+ tenantId: 'demo', userId: 'demo-user', roles: ['viewer']
61
+ });
62
+ console.log(result.answer); // Returns: 14 days.
63
+ ```
64
+
65
+ ### Production Example (Multi-Tenant with Gemini, pgvector & Redis)
66
+
67
+ For production applications with external LLMs, multi-tenant persistence, and distributed caching:
68
+
69
+ ```ts
70
+ import {
71
+ RAGEngine,
72
+ GeminiAdapter,
73
+ PostgresVectorAdapter,
74
+ RedisCacheAdapter,
75
+ LocalFaqAdapter
76
+ } from '@denizsvnc/ai-rag-engine';
77
+
78
+ const engine = new RAGEngine({
79
+ // 1. LLM Provider (Gemini, OpenAI, Ollama, or Custom)
80
+ llmProvider: new GeminiAdapter(
81
+ process.env.GEMINI_API_KEY,
82
+ 'gemini-1.5-flash'
83
+ ),
84
+
85
+ // 2. Knowledge Retrieval & Semantic FAQ Matching
86
+ inputs: {
87
+ vectorStore: new PostgresVectorAdapter({
88
+ connectionString: process.env.DATABASE_URL,
89
+ tableName: 'enterprise_embeddings'
90
+ }),
91
+ faq: new LocalFaqAdapter({
92
+ filePath: './faq.json',
93
+ defaultMatchThreshold: 0.65
94
+ })
95
+ },
96
+
97
+ // 3. Distributed Cache & Security Guardrails
98
+ cache: {
99
+ enabled: true,
100
+ adapter: new RedisCacheAdapter({
101
+ url: process.env.REDIS_URL,
102
+ defaultTtlMs: 3600000 // 1 hour
103
+ })
104
+ },
105
+ security: {
106
+ enabled: true,
107
+ blockHighRiskInjections: true,
108
+ pii: {
109
+ enabled: true,
110
+ maskTCKN: true,
111
+ maskCreditCards: true,
112
+ maskEmails: true
113
+ }
114
+ }
115
+ });
116
+
117
+ // Query with tenant isolation & role-based access
118
+ const response = await engine.query('Q3 compliance audit summary', {
119
+ tenantId: 'tenant-acme',
120
+ userId: 'usr_8472',
121
+ roles: ['compliance_officer']
122
+ });
123
+
124
+ console.log(response.answer);
125
+ console.log('Intent:', response.intent);
126
+ console.log('Latency (ms):', response.latencyMs);
127
+ console.log('Sources:', response.sources);
128
+ ```
129
+
130
+ Importing the SDK does not start a server, load `.env`, open Redis/PostgreSQL connections, download models, or change the Transformers global environment. Constructors and explicit initialization perform the operations described in [the usage guide](docs/USAGE.md).
131
+
132
+ ## Capabilities and operational boundaries
133
+
134
+ | Capability | Implementation / boundary |
135
+ | --- | --- |
136
+ | LLM providers | OpenAI, Gemini, Ollama, private OpenAI-compatible endpoints, configurable fallback/tiering |
137
+ | Semantic FAQ and tool routing | Local Transformers/ONNX embeddings; initial use downloads model files unless local-only |
138
+ | In-memory vectors and queue | Development / single-process use; data does not survive restart |
139
+ | Persistent vectors | PostgreSQL/pgvector, tenant-scoped CRUD, dense + PostgreSQL full-text RRF retrieval |
140
+ | Durable ingestion | Separate BullMQ worker, PostgreSQL uploads/outbox, retries, idempotent replacement, revision-aware cache |
141
+ | Security boundaries | Authenticated HTTP context, tool RBAC/schema checks, signed single-use approval, tenant-scoped data access |
142
+ | Metrics | Token/latency accounting and Prometheus histogram export |
143
+
144
+ Prompt filters and PII patterns are defenses with limitations, not guarantees of complete detection. PostgreSQL full-text retrieval is not BM25. The current PDF extractor is limited and does not perform OCR. The dependency tree includes native ONNX runtimes and is substantially larger than the SDK tarball; model weights are not bundled.
145
+
146
+ ## Documentation
147
+
148
+ - [Configuration, real providers, vectors, FAQ, tools and troubleshooting](docs/USAGE.md)
149
+ - [Versioning, manual publication and release checklist](docs/RELEASING.md)
150
+ - [Changelog](CHANGELOG.md)
151
+ - [Detailed release-readiness report](docs/NPM_RELEASE_READINESS.md)
152
+
153
+ ## Repository verification
154
+
155
+ ```sh
156
+ npm ci
157
+ npm run typecheck
158
+ PG_TEST_URL=postgresql://postgres:test@localhost/rag_test \
159
+ REDIS_TEST_URL=redis://localhost:6379 npm test
160
+ npm run test:security
161
+ npm run test:package
162
+ npm run test:consumer
163
+ npm audit --audit-level=high
164
+ ```
165
+
166
+ Tests with external services require disposable PostgreSQL/pgvector and Redis. Missing service variables explicitly skip those tests locally; CI provisions both and requires them. `test:consumer` installs the packed SDK in a separate project and verifies an inert ESM import, deterministic RAG and strict TypeScript declarations. `test:consumer:integration` adds actual local embeddings/generation and multi-process recovery; it requires test services and model downloads.
167
+
168
+ MIT licensed. [Issue tracker](https://github.com/Denizsvnc/ai-rag-engine/issues).
@@ -0,0 +1 @@
1
+ export * from './redis-cache.adapter.js';
@@ -0,0 +1,49 @@
1
+ import type { ICacheAdapter } from '../../interfaces/cache.interface.js';
2
+ /** ioredis-compatible command interface; injected clients own their connection lifecycle. */
3
+ export interface IRedisClientLike {
4
+ get(key: string): Promise<string | null>;
5
+ set(key: string, value: string, ...args: any[]): Promise<any>;
6
+ del(key: string): Promise<number>;
7
+ del(keys: string[]): Promise<number>;
8
+ keys?(pattern: string): Promise<string[]>;
9
+ scan?(cursor: string, ...args: any[]): Promise<[string, string[]]>;
10
+ quit?(): Promise<any>;
11
+ disconnect?(): void;
12
+ ping?(): Promise<string>;
13
+ }
14
+ export interface RedisCacheOptions {
15
+ url?: string | undefined;
16
+ client?: IRedisClientLike | undefined;
17
+ keyPrefix?: string | undefined;
18
+ defaultTtlMs?: number | undefined;
19
+ /** Without Redis, enables local LRU. With Redis, read/write failures degrade to misses/skipped writes, never local copies. */
20
+ enableFallbackMemory?: boolean | undefined;
21
+ fallbackMaxItems?: number | undefined;
22
+ }
23
+ /** Shared Redis is authoritative. Local LRU is used only when no Redis was configured. */
24
+ export declare class RedisCacheAdapter implements ICacheAdapter {
25
+ private options;
26
+ private client;
27
+ private readonly keyPrefix;
28
+ private readonly defaultTtlMs;
29
+ private readonly enableFallback;
30
+ private readonly remoteConfigured;
31
+ private readonly fallbackCache;
32
+ private isConnected;
33
+ constructor(options?: RedisCacheOptions);
34
+ init(): Promise<void>;
35
+ get(key: string): Promise<any | null>;
36
+ set(key: string, value: any, ttlMs?: number): Promise<void>;
37
+ delete(key: string): Promise<boolean>;
38
+ /** Strings match keywords (case-insensitive), or anchored * / ? wildcard patterns. Regexes match namespaced keys. */
39
+ invalidate(patternOrKey: string | RegExp): Promise<number>;
40
+ clear(): Promise<void>;
41
+ getIsConnected(): boolean;
42
+ /** Number of local-only LRU entries; not a Redis key count. */
43
+ size(): number;
44
+ private removeMatching;
45
+ private keyBatches;
46
+ private matcher;
47
+ private requireClient;
48
+ private prefixKey;
49
+ }
@@ -0,0 +1,29 @@
1
+ import type { IFAQAdapter, FAQItem, FAQMatchResult } from '../../interfaces/faq.interface.js';
2
+ import { SMLEngine } from '../../core/sml-engine.js';
3
+ export interface LocalFaqAdapterOptions {
4
+ filePath?: string | undefined;
5
+ smlEngine?: SMLEngine | undefined;
6
+ defaultMatchThreshold?: number | undefined;
7
+ }
8
+ /**
9
+ * Loads pre-computed or dynamically embedded FAQs from a local JSON file.
10
+ * Matches questions using local cosine similarity without an LLM generation call.
11
+ */
12
+ export declare class LocalFaqAdapter implements IFAQAdapter {
13
+ private filePath;
14
+ private smlEngine;
15
+ private defaultThreshold;
16
+ private items;
17
+ private isLoaded;
18
+ constructor(options?: LocalFaqAdapterOptions);
19
+ /**
20
+ * Initializes the adapter by reading the JSON file and caching embeddings in RAM.
21
+ */
22
+ init(): Promise<void>;
23
+ setFaqs(items: FAQItem[]): Promise<void>;
24
+ getFaqs(tenantId?: string): Promise<FAQItem[]>;
25
+ /**
26
+ * Finds the best semantically matching FAQ item for a given user query within the caller's tenant boundary.
27
+ */
28
+ findBestMatch(query: string, threshold?: number, tenantId?: string): Promise<FAQMatchResult | null>;
29
+ }
@@ -0,0 +1,14 @@
1
+ import type { IFAQAdapter } from '../../interfaces/faq.interface.js';
2
+ export declare class UrlFaqAdapter implements IFAQAdapter {
3
+ private endpoint;
4
+ private headers?;
5
+ /**
6
+ * @param endpoint Remote URL returning FAQ JSON data
7
+ * @param headers Optional request headers (e.g. authorization tokens)
8
+ */
9
+ constructor(endpoint: string, headers?: Record<string, string>);
10
+ getFaqs(): Promise<Array<{
11
+ question: string;
12
+ answer: string;
13
+ }>>;
14
+ }
@@ -0,0 +1,42 @@
1
+ import type { ILLMProvider, ILLMRequest, ILLMResponse } from '../../interfaces/llm.interface.js';
2
+ export interface CustomLLMConfig {
3
+ /**
4
+ * Full URL to the OpenAI-compatible chat completions endpoint.
5
+ * e.g., 'http://localhost:8000/v1/chat/completions' (vLLM)
6
+ * e.g., 'http://internal-llm.company.local:8080/v1/chat/completions'
7
+ */
8
+ endpoint?: string;
9
+ /**
10
+ * Model identifier expected by the inference server.
11
+ * e.g., 'llama-3.1-8b-instruct', 'mistral-large', 'qwen-2.5-72b'
12
+ */
13
+ model?: string;
14
+ /**
15
+ * Optional API key or bearer token. Left empty if running within a secured private VPC.
16
+ */
17
+ apiKey?: string | undefined;
18
+ /**
19
+ * Additional HTTP headers (e.g. corporate proxy headers, tenant IDs, authorization).
20
+ */
21
+ customHeaders?: Record<string, string> | undefined;
22
+ /**
23
+ * Sampling temperature (defaults to 0.1 for deterministic RAG answers).
24
+ */
25
+ temperature?: number | undefined;
26
+ }
27
+ /**
28
+ * Adapter for private, self-hosted, or internal enterprise LLM APIs.
29
+ * Works seamlessly with vLLM, LocalAI, HuggingFace TGI, LM Studio, Azure OpenAI,
30
+ * and any internal API gateway adhering to the OpenAI-compatible /v1/chat/completions spec.
31
+ */
32
+ export declare class CustomLLMAdapter implements ILLMProvider {
33
+ protected endpoint: string;
34
+ protected model: string;
35
+ protected apiKey?: string | undefined;
36
+ protected customHeaders: Record<string, string>;
37
+ protected temperature: number;
38
+ constructor(config?: CustomLLMConfig);
39
+ static createFast(config?: Partial<CustomLLMConfig>): CustomLLMAdapter;
40
+ static createSmart(config?: Partial<CustomLLMConfig>): CustomLLMAdapter;
41
+ generate(request: ILLMRequest): Promise<ILLMResponse>;
42
+ }
@@ -0,0 +1,10 @@
1
+ import type { ILLMProvider, ILLMRequest, ILLMResponse } from '../../interfaces/llm.interface.js';
2
+ export interface FallbackProviderOptions {
3
+ logFailover?: boolean;
4
+ }
5
+ export declare class FallbackLLMProvider implements ILLMProvider {
6
+ private providers;
7
+ private logFailover;
8
+ constructor(providers: ILLMProvider[], options?: FallbackProviderOptions);
9
+ generate(request: ILLMRequest): Promise<ILLMResponse>;
10
+ }
@@ -0,0 +1,9 @@
1
+ import type { ILLMProvider, ILLMRequest, ILLMResponse } from '../../interfaces/llm.interface.js';
2
+ export declare class GeminiAdapter implements ILLMProvider {
3
+ private apiKey;
4
+ private model;
5
+ constructor(apiKey?: string, model?: string);
6
+ static createFast(apiKey?: string): GeminiAdapter;
7
+ static createSmart(apiKey?: string): GeminiAdapter;
8
+ generate(request: ILLMRequest): Promise<ILLMResponse>;
9
+ }
@@ -0,0 +1,30 @@
1
+ import { CustomLLMAdapter } from './custom.adapter.js';
2
+ export interface OllamaConfig {
3
+ /**
4
+ * Base host for the Ollama instance (e.g. 'http://localhost:11434' or 'http://192.168.1.50:11434').
5
+ * Defaults to process.env.OLLAMA_HOST || 'http://localhost:11434'.
6
+ */
7
+ host?: string | undefined;
8
+ /**
9
+ * Model tag pulled in Ollama (e.g., 'llama3.2', 'deepseek-r1:8b', 'mistral', 'qwen2.5:7b').
10
+ * Defaults to process.env.OLLAMA_MODEL || 'llama3.2'.
11
+ */
12
+ model?: string | undefined;
13
+ /**
14
+ * Sampling temperature (defaults to 0.1).
15
+ */
16
+ temperature?: number | undefined;
17
+ /**
18
+ * Optional custom headers (e.g. if Ollama is behind an Nginx reverse proxy).
19
+ */
20
+ customHeaders?: Record<string, string> | undefined;
21
+ }
22
+ /**
23
+ * Dedicated adapter for self-hosted Ollama instances.
24
+ * Automatically targets Ollama's OpenAI-compatible /v1/chat/completions endpoint.
25
+ */
26
+ export declare class OllamaAdapter extends CustomLLMAdapter {
27
+ constructor(config?: OllamaConfig);
28
+ static createFast(config?: Partial<OllamaConfig>): OllamaAdapter;
29
+ static createSmart(config?: Partial<OllamaConfig>): OllamaAdapter;
30
+ }
@@ -0,0 +1,10 @@
1
+ import type { ILLMProvider, ILLMRequest, ILLMResponse } from '../../interfaces/llm.interface.js';
2
+ export declare class OpenAIAdapter implements ILLMProvider {
3
+ protected apiKey: string;
4
+ protected model: string;
5
+ protected endpoint: string;
6
+ constructor(apiKey?: string, model?: string, endpoint?: string);
7
+ static createFast(apiKey?: string): OpenAIAdapter;
8
+ static createSmart(apiKey?: string): OpenAIAdapter;
9
+ generate(request: ILLMRequest): Promise<ILLMResponse>;
10
+ }
@@ -0,0 +1,6 @@
1
+ import { OpenAIAdapter } from './openai.adapter.js';
2
+ export declare class OpenRouterAdapter extends OpenAIAdapter {
3
+ constructor(apiKey?: string, model?: string, endpoint?: string);
4
+ static createFast(apiKey?: string): OpenRouterAdapter;
5
+ static createSmart(apiKey?: string): OpenRouterAdapter;
6
+ }
@@ -0,0 +1,22 @@
1
+ import type { ILLMProvider, ILLMRequest, ILLMResponse } from '../../interfaces/llm.interface.js';
2
+ export interface TieredLLMConfig {
3
+ fastProvider: ILLMProvider;
4
+ smartProvider: ILLMProvider;
5
+ classifier?: ((request: ILLMRequest) => 'fast' | 'smart') | undefined;
6
+ }
7
+ export declare class TieredLLMProvider implements ILLMProvider {
8
+ private fastProvider;
9
+ private smartProvider;
10
+ private customClassifier?;
11
+ constructor(config: TieredLLMConfig);
12
+ /**
13
+ * Determines whether the request requires high reasoning (smart) or standard execution (fast).
14
+ */
15
+ classifyComplexity(request: ILLMRequest): 'fast' | 'smart';
16
+ generate(request: ILLMRequest): Promise<ILLMResponse>;
17
+ /**
18
+ * Automatically constructs a TieredLLMProvider based on available configuration in environment variables.
19
+ * Enforces enterprise data privacy compliance by respecting ALLOW_PUBLIC_OPENSOURCE_LLM.
20
+ */
21
+ static createFromEnv(): TieredLLMProvider;
22
+ }
@@ -0,0 +1,6 @@
1
+ import type { IRulesAdapter } from '../../interfaces/rules.interface.js';
2
+ export declare class StaticRulesAdapter implements IRulesAdapter {
3
+ private rules;
4
+ constructor(rules: string[]);
5
+ getActiveRules(tenantId?: string): Promise<string[]>;
6
+ }
@@ -0,0 +1,33 @@
1
+ import type { IVectorStoreAdapter, VectorDocument } from '../../interfaces/vector.interface.js';
2
+ import { SMLEngine } from '../../core/sml-engine.js';
3
+ import { type HybridScoredDocument } from '../../core/vector/hybrid-search.js';
4
+ export declare class MemoryVectorAdapter implements IVectorStoreAdapter {
5
+ private smlEngine;
6
+ private documents;
7
+ private bm25Index;
8
+ constructor(smlEngine?: SMLEngine);
9
+ /**
10
+ * Adds documents to memory, computes their embeddings, and indexes them in BM25.
11
+ */
12
+ addDocuments(docs: Omit<VectorDocument, 'score'>[]): Promise<void>;
13
+ insert(doc: Omit<VectorDocument, 'score'>): Promise<void>;
14
+ /**
15
+ * Searches for the top-k most similar documents against the query vector,
16
+ * with optional metadata filtering (e.g. tenantId).
17
+ */
18
+ search(query: string, topK?: number, filter?: Record<string, any>): Promise<VectorDocument[]>;
19
+ /**
20
+ * Hybrid Search combining Dense ONNX Vector search with Sparse BM25 lexical search
21
+ * via Reciprocal Rank Fusion (RRF).
22
+ */
23
+ searchHybrid(query: string, topK?: number, filter?: Record<string, any>): Promise<HybridScoredDocument[]>;
24
+ /**
25
+ * Deletes a document by ID from both Dense memory and BM25 index.
26
+ */
27
+ deleteDocument(id: string, filter?: Record<string, any>): Promise<boolean>;
28
+ /**
29
+ * Clears all stored documents.
30
+ */
31
+ clear(filter?: Record<string, any>): Promise<void>;
32
+ private matchesFilter;
33
+ }
@@ -0,0 +1,51 @@
1
+ import { Pool } from 'pg';
2
+ import type { IVectorStoreAdapter, VectorDocument } from '../../interfaces/vector.interface.js';
3
+ export interface EmbeddingProvider {
4
+ getEmbedding(text: string): Promise<ArrayLike<number>>;
5
+ }
6
+ export interface PostgresVectorOptions {
7
+ connectionString?: string | undefined;
8
+ pool?: Pool | undefined;
9
+ tableName?: string | undefined;
10
+ modelName?: string | undefined;
11
+ dimensions?: number | undefined;
12
+ embeddingProvider?: EmbeddingProvider | undefined;
13
+ /** Disable after migrations have been applied by a database owner. */
14
+ autoMigrate?: boolean | undefined;
15
+ /** Optional approximate index. Exact search is the default. */
16
+ createHnswIndex?: boolean | undefined;
17
+ }
18
+ /** Persistent, tenant-scoped pgvector store. Every read/delete requires a tenant filter. */
19
+ export declare class PostgresVectorAdapter implements IVectorStoreAdapter {
20
+ private readonly options;
21
+ private readonly pool;
22
+ private readonly table;
23
+ private readonly configTable;
24
+ private readonly manifests;
25
+ private readonly epochs;
26
+ private readonly modelName;
27
+ private readonly dimensions;
28
+ private readonly embeddings;
29
+ private initPromise;
30
+ constructor(options: PostgresVectorOptions);
31
+ init(): Promise<void>;
32
+ private initialize;
33
+ /** Upserts are atomic per batch and IDs are unique within a tenant. */
34
+ addDocuments(documents: Omit<VectorDocument, 'score'>[]): Promise<void>;
35
+ /** Atomically replaces all chunks; retries and older document revisions cannot overwrite newer work. */
36
+ replaceDocument(documentId: string, tenantId: string, version: number, documents: Omit<VectorDocument, 'score'>[]): Promise<'applied' | 'unchanged' | 'superseded'>;
37
+ getRevision(tenantId: string): Promise<string>;
38
+ private prepare;
39
+ private write;
40
+ private bumpRevision;
41
+ search(query: string, topK?: number, filter?: Record<string, any>): Promise<VectorDocument[]>;
42
+ /** PostgreSQL lexical search + dense search, fused with RRF. This is not BM25. */
43
+ searchHybrid(query: string, topK?: number, filter?: Record<string, any>): Promise<VectorDocument[]>;
44
+ deleteDocument(id: string, filter?: Record<string, any>): Promise<boolean>;
45
+ clear(filter?: Record<string, any>): Promise<void>;
46
+ close(): Promise<void>;
47
+ private tenant;
48
+ private vector;
49
+ private validateTopK;
50
+ private transaction;
51
+ }
@@ -0,0 +1,47 @@
1
+ import type { RequestHandler } from 'express';
2
+ export interface AuthTokenPayload {
3
+ userId: string;
4
+ tenantId: string;
5
+ roles: string[];
6
+ metadata?: Record<string, any> | undefined;
7
+ iss?: string | undefined;
8
+ aud?: string | undefined;
9
+ nbf?: number | undefined;
10
+ iat?: number | undefined;
11
+ exp?: number | undefined;
12
+ }
13
+ export interface AuthMiddlewareOptions {
14
+ jwtSecret?: string | undefined;
15
+ issuer?: string | undefined;
16
+ audience?: string | undefined;
17
+ apiKeys?: Record<string, AuthTokenPayload> | undefined;
18
+ allowDevAnonymous?: boolean | undefined;
19
+ }
20
+ declare global {
21
+ namespace Express {
22
+ interface Request {
23
+ auth?: AuthTokenPayload | undefined;
24
+ }
25
+ }
26
+ }
27
+ /**
28
+ * Creates a cryptographically signed HMAC-SHA256 JWT token using native Node.js crypto.
29
+ * Zero external dependencies.
30
+ */
31
+ export declare function createAuthToken(payload: Omit<AuthTokenPayload, 'iat'>, secret?: string, expiresInSeconds?: number): string;
32
+ /**
33
+ * Verifies and decodes an HMAC-SHA256 JWT token using native Node.js crypto.
34
+ */
35
+ export declare function verifyAuthToken(token: string, secret?: string, options?: {
36
+ issuer?: string | undefined;
37
+ audience?: string | undefined;
38
+ }): AuthTokenPayload;
39
+ /**
40
+ * Express middleware that enforces trusted server-side authentication.
41
+ * Prevents client-controlled role spoofing (P0 security barrier).
42
+ */
43
+ export declare function createAuthMiddleware(options: AuthMiddlewareOptions): RequestHandler;
44
+ /**
45
+ * Route-level RBAC Guard: Ensures the authenticated user possesses at least one of the required roles.
46
+ */
47
+ export declare function requireRoles(requiredRoles: string[]): RequestHandler;
@@ -0,0 +1,3 @@
1
+ import type { Request, Response, NextFunction } from 'express';
2
+ import { RAGEngine } from '../core/engine.js';
3
+ export declare function createRagMiddleware(engine: RAGEngine): (req: Request, res: Response, next: NextFunction) => Promise<Response<any, Record<string, any>> | undefined>;
@@ -0,0 +1,8 @@
1
+ import type { Redis } from 'ioredis';
2
+ import type { RequestHandler } from 'express';
3
+ /** Atomic fixed-window tenant limit shared by all API instances. */
4
+ export declare function createTenantRateLimiter(redis: Pick<Redis, 'eval'>, options: {
5
+ limit: number;
6
+ windowMs: number;
7
+ prefix?: string;
8
+ }): RequestHandler;
@@ -0,0 +1,9 @@
1
+ import { Router, type RequestHandler } from 'express';
2
+ import { RAGEngine } from '../core/engine.js';
3
+ import { type AuthMiddlewareOptions } from './auth.js';
4
+ export interface ApiRouteOptions {
5
+ rateLimiter?: RequestHandler | undefined;
6
+ auth?: AuthMiddlewareOptions | undefined;
7
+ enforceAuth?: boolean | undefined;
8
+ }
9
+ export declare function createApiRoutes(engine: RAGEngine, options?: ApiRouteOptions): Router;
@@ -0,0 +1 @@
1
+ import 'dotenv/config';