@denizsvnc/ai-rag-engine 0.0.0-stage → 2.2.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 (71) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/LICENSE +21 -0
  3. package/README.md +115 -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 +148 -0
  71. package/package.json +95 -4
@@ -0,0 +1,8 @@
1
+ export interface ICacheAdapter {
2
+ get(key: string): Promise<any | null>;
3
+ set(key: string, value: any, ttlMs?: number): Promise<void>;
4
+ delete(key: string): Promise<boolean>;
5
+ invalidate(patternOrKey: string | RegExp): Promise<number>;
6
+ clear(): Promise<void>;
7
+ size?(): number;
8
+ }
@@ -0,0 +1,73 @@
1
+ import type { IIngestionBackend } from './ingestion.interface.js';
2
+ import type { IJobQueue } from '../core/queue/job-queue.interface.js';
3
+ import type { ConfirmationNonceStore } from '../core/security/nonce-store.js';
4
+ import type { IVectorStoreAdapter } from './vector.interface.js';
5
+ import type { IRulesAdapter } from './rules.interface.js';
6
+ import type { IFAQAdapter } from './faq.interface.js';
7
+ import type { ICacheAdapter } from './cache.interface.js';
8
+ import type { IAgentTool } from './tool.interface.js';
9
+ import type { ILLMProvider } from './llm.interface.js';
10
+ import type { SecurityConfig } from '../core/security/security.types.js';
11
+ import type { ResponseMetrics, TokenTrackerOptions } from '../core/metrics/token-tracker.js';
12
+ export interface EngineConfig {
13
+ llmProvider: ILLMProvider;
14
+ confirmation?: {
15
+ secret?: string | undefined;
16
+ nonceStore?: ConfirmationNonceStore | undefined;
17
+ } | undefined;
18
+ security?: SecurityConfig | undefined;
19
+ smlConfig?: {
20
+ enabled: boolean;
21
+ modelName?: string | undefined;
22
+ matchThreshold?: number | undefined;
23
+ } | undefined;
24
+ inputs: {
25
+ vectorStore?: IVectorStoreAdapter | undefined;
26
+ rules?: IRulesAdapter | undefined;
27
+ faq?: IFAQAdapter | undefined;
28
+ };
29
+ tools?: {
30
+ registryFilePath: string;
31
+ items: IAgentTool[];
32
+ } | undefined;
33
+ cache?: {
34
+ enabled: boolean;
35
+ adapter?: ICacheAdapter | undefined;
36
+ filePath?: string | undefined;
37
+ minHitsToPersist?: number | undefined;
38
+ ttlMs?: number | undefined;
39
+ toolResultTtlMs?: number | undefined;
40
+ cacheToolResults?: boolean | undefined;
41
+ } | undefined;
42
+ retrieval?: {
43
+ strategy?: 'dense' | 'sparse' | 'hybrid' | undefined;
44
+ topK?: number | undefined;
45
+ minRelevanceScore?: number | undefined;
46
+ } | undefined;
47
+ metrics?: TokenTrackerOptions | undefined;
48
+ ingestion?: {
49
+ backend?: IIngestionBackend | undefined;
50
+ chunkSize?: number | undefined;
51
+ chunkOverlap?: number | undefined;
52
+ jobQueue?: IJobQueue | undefined;
53
+ } | undefined;
54
+ }
55
+ export interface RAGResponse {
56
+ answer: string;
57
+ intent: string;
58
+ resolvedBy: string;
59
+ sources?: string[] | undefined;
60
+ appliedRules?: string[] | undefined;
61
+ executedTools?: Array<{
62
+ name: string;
63
+ args: any;
64
+ }> | undefined;
65
+ confirmationRequests?: Array<{
66
+ toolName: string;
67
+ proposedArgs: unknown;
68
+ confirmationTicket: string;
69
+ }> | undefined;
70
+ securityWarning?: string | undefined;
71
+ latencyMs: number;
72
+ metrics?: ResponseMetrics | undefined;
73
+ }
@@ -0,0 +1,17 @@
1
+ export interface FAQItem {
2
+ id?: string | undefined;
3
+ tenantId?: string | undefined;
4
+ question: string;
5
+ answer: string;
6
+ category?: string | undefined;
7
+ embedding?: number[] | undefined;
8
+ }
9
+ export interface FAQMatchResult {
10
+ question: string;
11
+ answer: string;
12
+ score: number;
13
+ }
14
+ export interface IFAQAdapter {
15
+ getFaqs(tenantId?: string): Promise<FAQItem[]>;
16
+ findBestMatch?(query: string, threshold?: number, tenantId?: string): Promise<FAQMatchResult | null>;
17
+ }
@@ -0,0 +1,14 @@
1
+ import type { DocumentUploadPayload } from '../services/document-ingestion.service.js';
2
+ import type { Job, JobStatus } from '../core/queue/job-queue.interface.js';
3
+ export interface IIngestionBackend {
4
+ submitDocument(payload: DocumentUploadPayload): Promise<{
5
+ jobId: string;
6
+ status: JobStatus;
7
+ }>;
8
+ getJobStatus(jobId: string, tenantId?: string): Promise<Job | null>;
9
+ retryJob?(jobId: string, tenantId: string): Promise<void>;
10
+ }
11
+ export interface IngestionSettings {
12
+ chunkSize: number;
13
+ chunkOverlap: number;
14
+ }
@@ -0,0 +1,22 @@
1
+ import type { IAgentTool } from './tool.interface.js';
2
+ export interface ILLMRequest {
3
+ systemPrompt: string;
4
+ userPrompt: string;
5
+ tools?: IAgentTool[];
6
+ complexity?: 'fast' | 'smart';
7
+ }
8
+ export interface ILLMResponse {
9
+ content: string;
10
+ toolCalls?: Array<{
11
+ name: string;
12
+ arguments: Record<string, any>;
13
+ }>;
14
+ usage?: {
15
+ promptTokens: number;
16
+ completionTokens: number;
17
+ totalTokens?: number;
18
+ };
19
+ }
20
+ export interface ILLMProvider {
21
+ generate(request: ILLMRequest): Promise<ILLMResponse>;
22
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Multi-tenant and user execution context for queries and tool actions.
3
+ */
4
+ export interface QueryContext {
5
+ /**
6
+ * Unique identifier of the tenant / organization (for multi-tenant isolation).
7
+ */
8
+ tenantId?: string | undefined;
9
+ /**
10
+ * Unique identifier of the requesting user.
11
+ */
12
+ userId?: string | undefined;
13
+ /**
14
+ * RBAC roles assigned to the requesting user (e.g. ['admin', 'manager', 'read_only']).
15
+ */
16
+ roles?: string[] | undefined;
17
+ /**
18
+ * Arbitrary contextual metadata passed through the pipeline.
19
+ */
20
+ metadata?: Record<string, any> | undefined;
21
+ /**
22
+ * Set to true to bypass cache lookup.
23
+ */
24
+ skipCache?: boolean | undefined;
25
+ /**
26
+ * @deprecated Ignored. Mutations require a signed confirmationTicket.
27
+ */
28
+ confirmedAction?: boolean | undefined;
29
+ /**
30
+ * Cryptographically signed HMAC-SHA256 confirmation ticket for destructive/mutating operations (P0 security).
31
+ */
32
+ confirmationTicket?: string | undefined;
33
+ }
@@ -0,0 +1,3 @@
1
+ export interface IRulesAdapter {
2
+ getActiveRules(tenantId?: string): Promise<string[]>;
3
+ }
@@ -0,0 +1,50 @@
1
+ import { z } from 'zod';
2
+ import type { QueryContext } from './query-context.interface.js';
3
+ export interface ToolParameterSchema {
4
+ type: 'object';
5
+ properties: Record<string, {
6
+ type: 'string' | 'number' | 'boolean' | 'array';
7
+ description: string;
8
+ enum?: string[];
9
+ }>;
10
+ required?: string[];
11
+ }
12
+ export interface IAgentTool<TArgs = any, TResult = any> {
13
+ name: string;
14
+ description: string;
15
+ parameters?: ToolParameterSchema | undefined;
16
+ cacheResultTTL?: number | undefined;
17
+ /**
18
+ * Optional Zod schema for strict runtime validation of arguments before execution.
19
+ */
20
+ zodSchema?: z.ZodSchema<TArgs> | undefined;
21
+ /**
22
+ * Flag declaring if this tool mutates system state (e.g. updating stock, making payments).
23
+ * If true, requires explicit confirmation or appropriate permissions.
24
+ */
25
+ isMutating?: boolean | undefined;
26
+ /**
27
+ * Role-Based Access Control: List of roles allowed to invoke this tool.
28
+ * If defined, caller context.roles must include at least one matching role.
29
+ */
30
+ requiredRoles?: string[] | undefined;
31
+ /**
32
+ * If true, execution requires a valid single-use context.confirmationTicket.
33
+ */
34
+ requiresConfirmation?: boolean | undefined;
35
+ /**
36
+ * The business logic function to execute.
37
+ */
38
+ execute: (args: TArgs, context?: QueryContext) => Promise<TResult>;
39
+ }
40
+ export interface ToolAuditLog {
41
+ timestamp: string;
42
+ toolName: string;
43
+ tenantId?: string | undefined;
44
+ userId?: string | undefined;
45
+ args: any;
46
+ isMutating: boolean;
47
+ status: 'SUCCESS' | 'VALIDATION_FAILED' | 'UNAUTHORIZED' | 'CONFIRMATION_REQUIRED' | 'EXECUTION_FAILED';
48
+ error?: string | undefined;
49
+ durationMs: number;
50
+ }
@@ -0,0 +1,15 @@
1
+ export interface VectorDocument {
2
+ id: string;
3
+ content: string;
4
+ metadata?: Record<string, any> | undefined;
5
+ version?: number | undefined;
6
+ score?: number | undefined;
7
+ }
8
+ export interface IVectorStoreAdapter {
9
+ getRevision?(tenantId: string): Promise<string>;
10
+ search(query: string, topK?: number, filter?: Record<string, any>): Promise<VectorDocument[]>;
11
+ searchHybrid?(query: string, topK?: number, filter?: Record<string, any>): Promise<VectorDocument[]>;
12
+ addDocuments?(docs: Omit<VectorDocument, 'score'>[]): Promise<void>;
13
+ deleteDocument?(id: string, filter?: Record<string, any>): Promise<boolean>;
14
+ clear?(filter?: Record<string, any>): Promise<void>;
15
+ }
@@ -0,0 +1 @@
1
+ import 'dotenv/config';