@devilsdev/rag-pipeline-utils 2.4.5 → 2.6.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 (258) hide show
  1. package/.ragrc.schema.json +83 -192
  2. package/CHANGELOG.md +431 -0
  3. package/README.md +418 -117
  4. package/SECURITY.md +5 -5
  5. package/dist/experimental.cjs +2 -0
  6. package/dist/experimental.d.ts +1 -0
  7. package/dist/experimental.mjs +5 -0
  8. package/dist/index.cjs +6442 -1355
  9. package/dist/index.d.ts +129 -2
  10. package/dist/index.mjs +1 -1
  11. package/dist/testing.cjs +2 -0
  12. package/dist/testing.d.ts +1 -0
  13. package/dist/testing.mjs +5 -0
  14. package/dist/types/adapters/ai-sdk.d.ts +53 -0
  15. package/dist/types/agentic/agentic-pipeline.d.ts +103 -0
  16. package/dist/types/agentic/iterative-retriever.d.ts +85 -0
  17. package/dist/types/agentic/query-planner.d.ts +145 -0
  18. package/dist/types/agentic/self-critique.d.ts +45 -0
  19. package/dist/types/ai/adaptive-retrieval.d.ts +44 -0
  20. package/dist/types/ai/federation/federated-learning-coordinator.d.ts +62 -0
  21. package/dist/types/ai/multimodal-processing.d.ts +32 -0
  22. package/dist/types/ai/multimodal.d.ts +39 -0
  23. package/dist/types/ai/retrieval-engine.d.ts +37 -0
  24. package/dist/types/ai/training/model-training-orchestrator.d.ts +31 -0
  25. package/dist/types/cache/component-keys.d.ts +7 -0
  26. package/dist/types/cache/memory-cache.d.ts +54 -0
  27. package/dist/types/cache/pipeline-cache.d.ts +66 -0
  28. package/dist/types/cache/redis-cache.d.ts +46 -0
  29. package/dist/types/chunking/chunking-engine.d.ts +70 -0
  30. package/dist/types/chunking/strategies/fixed-size.d.ts +17 -0
  31. package/dist/types/chunking/strategies/recursive.d.ts +20 -0
  32. package/dist/types/chunking/strategies/semantic.d.ts +19 -0
  33. package/dist/types/chunking/strategies/sentence.d.ts +13 -0
  34. package/dist/types/chunking/strategies/structure-aware.d.ts +19 -0
  35. package/dist/types/citation/citation-tracker.d.ts +64 -0
  36. package/dist/types/citation/hallucination-detector.d.ts +40 -0
  37. package/dist/types/citation/source-mapper.d.ts +37 -0
  38. package/dist/types/connectors/anthropic-connector.d.ts +97 -0
  39. package/dist/types/connectors/base-connector.d.ts +87 -0
  40. package/dist/types/connectors/cohere-connector.d.ts +94 -0
  41. package/dist/types/connectors/connector-utils.d.ts +110 -0
  42. package/dist/types/connectors/local-embedder.d.ts +95 -0
  43. package/dist/types/connectors/memory-retriever.d.ts +99 -0
  44. package/dist/types/connectors/ollama-connector.d.ts +115 -0
  45. package/dist/types/connectors/openai-connector.d.ts +129 -0
  46. package/dist/types/connectors/rag-prompt.d.ts +40 -0
  47. package/dist/types/core/create-pipeline.d.ts +8 -0
  48. package/dist/types/core/plugin-registry.d.ts +136 -0
  49. package/dist/types/core/stage-contracts.d.ts +36 -0
  50. package/dist/types/core/validate-plugin.d.ts +45 -0
  51. package/dist/types/cost/cost-calculator.d.ts +152 -0
  52. package/dist/types/cost/cost-tracker.d.ts +126 -0
  53. package/dist/types/cost/token-budget.d.ts +110 -0
  54. package/dist/types/dag/core/error-context.d.ts +239 -0
  55. package/dist/types/dag/core/execution-scheduler.d.ts +191 -0
  56. package/dist/types/dag/core/topology-validation.d.ts +226 -0
  57. package/dist/types/dag/dag-engine.d.ts +156 -0
  58. package/dist/types/dag/dag-node.d.ts +37 -0
  59. package/dist/types/dashboard/dashboard-generator.d.ts +127 -0
  60. package/dist/types/dashboard/metrics-aggregator.d.ts +218 -0
  61. package/dist/types/debug/execution-tracer.d.ts +57 -0
  62. package/dist/types/debug/trace-inspector.d.ts +31 -0
  63. package/dist/types/ecosystem/plugin-certification.d.ts +169 -0
  64. package/dist/types/enterprise/data-governance.d.ts +203 -0
  65. package/dist/types/enterprise/multi-tenancy.d.ts +137 -0
  66. package/dist/types/enterprise/sso-integration.d.ts +420 -0
  67. package/dist/types/evaluate/context-metrics.d.ts +55 -0
  68. package/dist/types/evaluate/faithfulness.d.ts +41 -0
  69. package/dist/types/evaluate/groundedness.d.ts +60 -0
  70. package/dist/types/evaluate/pipeline-evaluator.d.ts +88 -0
  71. package/dist/types/evaluate/relevance.d.ts +21 -0
  72. package/dist/types/evaluate/scoring.d.ts +123 -0
  73. package/dist/types/evaluate/semantic-similarity.d.ts +14 -0
  74. package/dist/types/experimental.d.ts +15 -0
  75. package/dist/types/graph/entity-extractor.d.ts +84 -0
  76. package/dist/types/graph/graph-index.d.ts +49 -0
  77. package/dist/types/graph/graph-retriever.d.ts +81 -0
  78. package/dist/types/graph/knowledge-graph.d.ts +129 -0
  79. package/dist/types/grounding/check-answer.d.ts +101 -0
  80. package/dist/types/guardrails/guardrails-pipeline.d.ts +63 -0
  81. package/dist/types/guardrails/injection-detector.d.ts +47 -0
  82. package/dist/types/guardrails/post-generation-guard.d.ts +61 -0
  83. package/dist/types/guardrails/pre-retrieval-guard.d.ts +64 -0
  84. package/dist/types/guardrails/retrieval-guard.d.ts +57 -0
  85. package/dist/types/mcp/mcp-server.d.ts +85 -0
  86. package/dist/types/mcp/mcp-tool-builder.d.ts +55 -0
  87. package/dist/types/reranker/cascade-reranker.d.ts +40 -0
  88. package/dist/types/reranker/embedding-reranker.d.ts +77 -0
  89. package/dist/types/reranker/scoring-reranker.d.ts +41 -0
  90. package/dist/types/retrieval/bm25-search.d.ts +43 -0
  91. package/dist/types/retrieval/file-vector-store.d.ts +99 -0
  92. package/dist/types/retrieval/hybrid-retriever.d.ts +35 -0
  93. package/dist/types/retrieval/rank-fusion.d.ts +10 -0
  94. package/dist/types/security/jwt-config.d.ts +80 -0
  95. package/dist/types/security/jwt-validator.d.ts +195 -0
  96. package/dist/types/security/plugin-sandbox.d.ts +158 -0
  97. package/dist/types/security/plugin-signature-verifier.d.ts +139 -0
  98. package/dist/types/security/validator.d.ts +5 -0
  99. package/dist/types/streaming/embedding-stream.d.ts +65 -0
  100. package/dist/types/streaming/sse-adapter.d.ts +59 -0
  101. package/dist/types/streaming/stream-router.d.ts +88 -0
  102. package/dist/types/streaming/streaming-embedder.d.ts +99 -0
  103. package/dist/types/streaming/websocket-adapter.d.ts +71 -0
  104. package/dist/types/templates/scaffold.d.ts +55 -0
  105. package/dist/types/templates/template-registry.d.ts +63 -0
  106. package/dist/types/testing.d.ts +168 -0
  107. package/dist/types/utils/embed-query.d.ts +20 -0
  108. package/dist/types/utils/logger.d.ts +50 -0
  109. package/dist/types/utils/rate-limiter.d.ts +66 -0
  110. package/dist/types/utils/retry.d.ts +27 -0
  111. package/dist/types/utils/run-abort.d.ts +34 -0
  112. package/dist/types/utils/secure-logging.d.ts +51 -0
  113. package/dist/types/utils/structured-logger.d.ts +126 -0
  114. package/package.json +72 -33
  115. package/src/adapters/ai-sdk.js +215 -0
  116. package/src/agentic/agentic-pipeline.js +75 -7
  117. package/src/agentic/iterative-retriever.js +47 -10
  118. package/src/agentic/query-planner.js +329 -78
  119. package/src/cache/component-keys.js +38 -0
  120. package/src/cache/memory-cache.js +87 -0
  121. package/src/cache/pipeline-cache.js +260 -0
  122. package/src/cache/redis-cache.js +83 -0
  123. package/src/citation/citation-tracker.js +6 -4
  124. package/src/citation/hallucination-detector.js +2 -2
  125. package/src/citation/source-mapper.js +15 -28
  126. package/src/cli/commands/ai.js +106 -0
  127. package/src/cli/commands/plugin-hub/event-handlers.js +6 -0
  128. package/src/cli/commands/plugin-hub/hub-commands.js +0 -74
  129. package/src/cli/commands/plugin-hub/local-commands.js +143 -0
  130. package/src/cli/commands/plugin-hub.js +114 -49
  131. package/src/cli/diagnostics/config-checker.js +76 -63
  132. package/src/cli/diagnostics/environment-checker.js +62 -31
  133. package/src/cli/diagnostics/plugin-checker.js +36 -89
  134. package/src/cli/doctor-command.js +39 -5
  135. package/src/cli/enhanced-cli-commands.js +104 -233
  136. package/src/cli/handlers/ai-handler.js +364 -0
  137. package/src/cli/handlers/diagnostics-handler.js +78 -31
  138. package/src/cli/handlers/ingest-handler.js +28 -22
  139. package/src/cli/handlers/init-handler.js +148 -25
  140. package/src/cli/handlers/query-handler.js +203 -25
  141. package/src/cli/handlers/run-handler.js +16 -167
  142. package/src/cli/handlers/serve-handler.js +60 -0
  143. package/src/cli/services/bench-stats.js +54 -0
  144. package/src/cli/services/benchmark.js +161 -0
  145. package/src/cli/services/feedback.js +155 -0
  146. package/src/cli/services/instrument.js +83 -0
  147. package/src/cli/services/rag-service.js +361 -0
  148. package/src/cli/shared/cli-utils.js +24 -7
  149. package/src/cli/shared/config-context.js +21 -0
  150. package/src/cli/wizard/config-builder.js +81 -0
  151. package/src/cli/wizard/init-wizard.js +602 -0
  152. package/src/config/component-factory.js +125 -0
  153. package/src/config/enhanced-ragrc-schema.js +0 -2
  154. package/src/config/load-config.js +29 -10
  155. package/src/config/load-plugin-config.js +46 -2
  156. package/src/config/validate-schema.js +4 -1
  157. package/src/connectors/anthropic-connector.js +115 -57
  158. package/src/connectors/base-connector.js +74 -19
  159. package/src/connectors/cohere-connector.js +108 -34
  160. package/src/connectors/connector-utils.js +302 -0
  161. package/src/connectors/local-embedder.js +63 -5
  162. package/src/connectors/memory-retriever.js +125 -14
  163. package/src/connectors/ollama-connector.js +112 -51
  164. package/src/connectors/openai-connector.js +153 -44
  165. package/src/connectors/rag-prompt.js +88 -0
  166. package/src/core/create-pipeline.js +307 -61
  167. package/src/core/performance/parallel-processor.js +62 -1
  168. package/src/core/plugin-registry.js +125 -26
  169. package/src/core/stage-contracts.js +36 -0
  170. package/src/core/validate-plugin.js +85 -0
  171. package/src/cost/cost-calculator.js +205 -32
  172. package/src/debug/execution-tracer.js +14 -4
  173. package/src/dev/dev-server.js +173 -11
  174. package/src/dev/hot-reload.js +44 -4
  175. package/src/ecosystem/plugin-checks.js +181 -0
  176. package/src/ecosystem/plugin-hub.js +50 -125
  177. package/src/ecosystem/plugin-installer.js +298 -0
  178. package/src/ecosystem/plugin-probe.js +127 -0
  179. package/src/ecosystem/plugin-scanner.js +185 -0
  180. package/src/ecosystem/tar-extract.js +114 -0
  181. package/src/enterprise/audit-logging.js +158 -29
  182. package/src/enterprise/sso-integration.js +716 -228
  183. package/src/evaluate/context-metrics.js +28 -16
  184. package/src/evaluate/evaluator.js +3 -3
  185. package/src/evaluate/faithfulness.js +18 -14
  186. package/src/evaluate/groundedness.js +43 -12
  187. package/src/evaluate/pipeline-evaluator.js +35 -8
  188. package/src/evaluate/scoring.js +251 -3
  189. package/src/evaluate/semantic-similarity.js +79 -0
  190. package/src/experimental.js +71 -0
  191. package/src/grounding/check-answer.js +173 -0
  192. package/src/guardrails/guardrails-pipeline.js +69 -7
  193. package/src/guardrails/injection-detector.js +283 -0
  194. package/src/guardrails/post-generation-guard.js +19 -0
  195. package/src/guardrails/pre-retrieval-guard.js +36 -36
  196. package/src/guardrails/retrieval-guard.js +58 -8
  197. package/src/index.js +58 -1
  198. package/src/loader/csv-loader.js +3 -2
  199. package/src/loader/document-reader.js +91 -0
  200. package/src/loader/html-loader.js +3 -1
  201. package/src/loader/markdown-loader.js +3 -2
  202. package/src/reranker/cascade-reranker.js +3 -0
  203. package/src/reranker/embedding-reranker.js +136 -21
  204. package/src/reranker/llm-reranker.js +79 -12
  205. package/src/reranker/scoring-reranker.js +3 -0
  206. package/src/retrieval/bm25-search.js +12 -6
  207. package/src/retrieval/file-vector-store.js +241 -0
  208. package/src/retrieval/hybrid-retriever.js +22 -19
  209. package/src/security/jwt-config.js +13 -2
  210. package/src/security/jwt-validator.js +139 -34
  211. package/src/security/plugin-sandbox.js +18 -1
  212. package/src/security/plugin-signature-verifier.js +245 -70
  213. package/src/testing.js +254 -0
  214. package/src/utils/deprecate.js +69 -0
  215. package/src/utils/embed-query.js +33 -0
  216. package/src/utils/input-sanitizer.js +19 -4
  217. package/src/utils/logger.js +34 -4
  218. package/src/utils/run-abort.js +102 -0
  219. package/src/ai/federated-learning.js +0 -211
  220. package/src/ai/federation.js +0 -71
  221. package/src/ai/index.js +0 -22
  222. package/src/ai/model-trainer.js +0 -217
  223. package/src/ai/model-training.js +0 -202
  224. package/src/ai/multimodal/multi-modal-processor.js +0 -345
  225. package/src/ai/orchestrator.js +0 -65
  226. package/src/ai/retrieval/adaptive-retrieval-engine.js +0 -398
  227. package/src/cli/commands/ai-ml/adaptive-commands.js +0 -178
  228. package/src/cli/commands/ai-ml/federated-commands.js +0 -207
  229. package/src/cli/commands/ai-ml/index.js +0 -17
  230. package/src/cli/commands/ai-ml/multimodal-commands.js +0 -187
  231. package/src/cli/commands/ai-ml/shared.js +0 -51
  232. package/src/cli/commands/ai-ml/training-commands.js +0 -228
  233. package/src/cli/commands/ai-ml.js +0 -386
  234. package/src/cli/commands/docs.js +0 -228
  235. package/src/cli/commands/dx.js +0 -538
  236. package/src/cli/commands/wizard.js +0 -627
  237. package/src/cli/input-sanitization-middleware.js +0 -356
  238. package/src/cli/interactive-wizard.js +0 -373
  239. package/src/cli/templates/development.template.json +0 -64
  240. package/src/cli/templates/minimal.template.json +0 -33
  241. package/src/cli/templates/production.template.json +0 -104
  242. package/src/cli/templates/testing.template.json +0 -45
  243. package/src/cli/wizard/configuration.js +0 -148
  244. package/src/cli/wizard/observability.js +0 -79
  245. package/src/cli/wizard/performance.js +0 -76
  246. package/src/cli/wizard/plugin-selection.js +0 -297
  247. package/src/cli/wizard/preview-save.js +0 -172
  248. package/src/cli/wizard/project-setup.js +0 -66
  249. package/src/core/pipeline-factory.js +0 -59
  250. package/src/dag/enhanced-dag-engine.js +0 -894
  251. package/src/dx/index.js +0 -40
  252. package/src/dx/integration-templates.js +0 -662
  253. package/src/dx/performance-profiler.js +0 -968
  254. package/src/dx/realtime-debugger.js +0 -755
  255. package/src/dx/visual-pipeline-builder.js +0 -578
  256. package/src/ingest.js +0 -59
  257. package/src/pipeline/pipeline.js +0 -18
  258. package/src/query.js +0 -117
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Detect prompt-injection attempts in text.
3
+ *
4
+ * Built-in rules run on the folded text. Custom patterns run on the
5
+ * normalised text (case preserved) and on the folded text.
6
+ *
7
+ * @param {string} text
8
+ * @param {object} [opts]
9
+ * @param {Array<RegExp|string>} [opts.patterns=[]] - Extra patterns
10
+ * @param {boolean} [opts.useDefaults=true] - Include the built-in rules
11
+ * @returns {{ detected: boolean, matches: Array<{ id: string, pattern: string, match: string }>, normalized: string }}
12
+ */
13
+ export function detectInjection(text: string, opts?: {
14
+ patterns?: Array<RegExp | string>;
15
+ useDefaults?: boolean;
16
+ }): {
17
+ detected: boolean;
18
+ matches: Array<{
19
+ id: string;
20
+ pattern: string;
21
+ match: string;
22
+ }>;
23
+ normalized: string;
24
+ };
25
+ /**
26
+ * Normalise text for matching, without leetspeak/homoglyph folding.
27
+ * Case is preserved so case-sensitive custom patterns still work.
28
+ * @param {string} text
29
+ * @returns {string}
30
+ */
31
+ export function normalizeText(text: string): string;
32
+ /**
33
+ * Fully folded, lower-case form used by the built-in patterns.
34
+ * @param {string} text
35
+ * @returns {string}
36
+ */
37
+ export function foldText(text: string): string;
38
+ /**
39
+ * Built-in injection patterns. They run against the folded, lower-case text,
40
+ * so they are written in lower case and must stay narrow enough not to flag
41
+ * ordinary questions (e.g. "How does Redis act as a cache?").
42
+ * @type {Array<{ id: string, pattern: RegExp }>}
43
+ */
44
+ export const DEFAULT_RULES: Array<{
45
+ id: string;
46
+ pattern: RegExp;
47
+ }>;
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Post-generation guard that checks LLM output for PII leakage,
3
+ * groundedness against source documents, and length constraints.
4
+ * Sanitizes output by redacting detected PII.
5
+ *
6
+ * @extends EventEmitter
7
+ */
8
+ export class PostGenerationGuard extends EventEmitter<[never]> {
9
+ /**
10
+ * @param {object} [options] - Override default configuration
11
+ * @param {boolean} [options.enablePIIDetection=true] - Whether to detect PII in output
12
+ * @param {boolean} [options.enableGroundednessCheck=true] - Whether to check groundedness
13
+ * @param {number} [options.groundednessThreshold=0.3] - Minimum groundedness score
14
+ * @param {number} [options.maxResponseLength=50000] - Maximum allowed response length
15
+ * @param {number} [options.minResponseLength=0] - Minimum response length
16
+ * @param {object} [options.piiPatterns] - Custom PII regex patterns (merged with defaults)
17
+ */
18
+ constructor(options?: {
19
+ enablePIIDetection?: boolean;
20
+ enableGroundednessCheck?: boolean;
21
+ groundednessThreshold?: number;
22
+ maxResponseLength?: number;
23
+ minResponseLength?: number;
24
+ piiPatterns?: object;
25
+ });
26
+ config: any;
27
+ /**
28
+ * Check generated output for safety issues, PII, and groundedness.
29
+ *
30
+ * @param {string} output - The LLM-generated output text
31
+ * @param {Array<object>} [retrievedDocs=[]] - Source documents for groundedness checking
32
+ * @returns {{
33
+ * safe: boolean,
34
+ * issues: string[],
35
+ * sanitizedOutput: string,
36
+ * groundednessScore?: number
37
+ * }}
38
+ * @fires PostGenerationGuard#checked
39
+ */
40
+ check(output: string, retrievedDocs?: Array<object>): {
41
+ safe: boolean;
42
+ issues: string[];
43
+ sanitizedOutput: string;
44
+ groundednessScore?: number;
45
+ };
46
+ /**
47
+ * Replace PII matches in arbitrary text with [REDACTED], using the same
48
+ * patterns as check(). Used to scrub text derived from the answer, such as
49
+ * citation sentences.
50
+ *
51
+ * @param {string} text
52
+ * @returns {string}
53
+ */
54
+ redact(text: string): string;
55
+ }
56
+ /**
57
+ * Default configuration for the PostGenerationGuard.
58
+ * @type {object}
59
+ */
60
+ export const DEFAULT_CONFIG: object;
61
+ import { EventEmitter } from "events";
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Pre-retrieval guard that validates and sanitizes user queries before
3
+ * they reach the retrieval layer. Checks for injection attacks, topic
4
+ * violations, and input length constraints.
5
+ *
6
+ * @extends EventEmitter
7
+ */
8
+ export class PreRetrievalGuard extends EventEmitter<[never]> {
9
+ /**
10
+ * @param {object} [options] - Override default configuration
11
+ * @param {number} [options.maxQueryLength=10000] - Maximum allowed query length
12
+ * @param {boolean} [options.enableInjectionDetection=true] - Whether to check for prompt injection
13
+ * @param {boolean} [options.enableTopicFiltering=false] - Whether to enforce topic constraints
14
+ * @param {string[]} [options.blockedTopics=[]] - Topics to reject
15
+ * @param {string[]} [options.allowedTopics=[]] - If non-empty, only these topics are permitted
16
+ * @param {RegExp[]} [options.injectionPatterns] - Additional injection patterns (merged with defaults)
17
+ */
18
+ constructor(options?: {
19
+ maxQueryLength?: number;
20
+ enableInjectionDetection?: boolean;
21
+ enableTopicFiltering?: boolean;
22
+ blockedTopics?: string[];
23
+ allowedTopics?: string[];
24
+ injectionPatterns?: RegExp[];
25
+ });
26
+ config: any;
27
+ /** @private Extra patterns supplied by the caller */
28
+ private _customPatterns;
29
+ /**
30
+ * Check a query for safety issues, injection attempts, and topic violations.
31
+ *
32
+ * @param {string} query - The user query to validate
33
+ * @returns {{
34
+ * safe: boolean,
35
+ * issues: string[],
36
+ * sanitizedQuery: string,
37
+ * injection: { detected: boolean, matches: Array<{id: string, pattern: string, match: string}> }
38
+ * }}
39
+ * `sanitizedQuery` is the query truncated to maxQueryLength; it is never
40
+ * rewritten. Injection findings are reported in `issues` and `injection`
41
+ * and make `safe` false; blocking is up to the caller (GuardrailsPipeline
42
+ * blocks in strict mode).
43
+ * @fires PreRetrievalGuard#checked
44
+ */
45
+ check(query: string): {
46
+ safe: boolean;
47
+ issues: string[];
48
+ sanitizedQuery: string;
49
+ injection: {
50
+ detected: boolean;
51
+ matches: Array<{
52
+ id: string;
53
+ pattern: string;
54
+ match: string;
55
+ }>;
56
+ };
57
+ };
58
+ }
59
+ /**
60
+ * Default configuration for the PreRetrievalGuard.
61
+ * @type {object}
62
+ */
63
+ export const DEFAULT_CONFIG: object;
64
+ import { EventEmitter } from "events";
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Retrieval guard that filters search results based on relevance score,
3
+ * document freshness, and access control lists. Applied between the
4
+ * retrieval and generation stages of the pipeline.
5
+ *
6
+ * @extends EventEmitter
7
+ */
8
+ export class RetrievalGuard extends EventEmitter<[never]> {
9
+ /**
10
+ * @param {object} [options] - Override default configuration
11
+ * @param {number} [options.minRelevanceScore=0] - Minimum relevance score threshold
12
+ * @param {number} [options.maxAgeDays=0] - Maximum document age in days (0 = disabled)
13
+ * @param {boolean} [options.enforceACL=false] - Whether to enforce access control
14
+ * @param {boolean} [options.detectInjection=false] - Scan document text for prompt injection
15
+ * @param {'remove'|'flag'} [options.injectionAction='remove'] - Remove flagged documents or keep and mark them
16
+ * @param {RegExp[]} [options.injectionPatterns=[]] - Extra injection patterns for the scan
17
+ */
18
+ constructor(options?: {
19
+ minRelevanceScore?: number;
20
+ maxAgeDays?: number;
21
+ enforceACL?: boolean;
22
+ detectInjection?: boolean;
23
+ injectionAction?: "remove" | "flag";
24
+ injectionPatterns?: RegExp[];
25
+ });
26
+ config: any;
27
+ /**
28
+ * Filter retrieval results based on configured guards.
29
+ *
30
+ * @param {Array<object>} results - Array of retrieval results, each with .score and optional .metadata
31
+ * @param {object} [context={}] - Request context
32
+ * @param {string[]} [context.userPermissions] - User's permission roles for ACL filtering
33
+ * @returns {{
34
+ * results: Array<object>,
35
+ * removed: Array<object>,
36
+ * metadata: { reasons: Object<string, number> }
37
+ * }}
38
+ * @fires RetrievalGuard#filtered
39
+ */
40
+ filter(results: Array<object>, context?: {
41
+ userPermissions?: string[];
42
+ }): {
43
+ results: Array<object>;
44
+ removed: Array<object>;
45
+ metadata: {
46
+ reasons: {
47
+ [x: string]: number;
48
+ };
49
+ };
50
+ };
51
+ }
52
+ /**
53
+ * Default configuration for the RetrievalGuard.
54
+ * @type {object}
55
+ */
56
+ export const DEFAULT_CONFIG: object;
57
+ import { EventEmitter } from "events";
@@ -0,0 +1,85 @@
1
+ /**
2
+ * MCP server that registers RAG pipelines as callable MCP tools.
3
+ *
4
+ * @extends EventEmitter
5
+ * @fires MCPServer#tool_use
6
+ *
7
+ * @example
8
+ * const server = MCPServer.fromPipeline(myPipeline, { name: 'my_tool' });
9
+ * const tools = server.getToolDefinitions();
10
+ * const result = await server.handleToolUse('my_tool', { query: 'What is X?' });
11
+ */
12
+ export class MCPServer extends EventEmitter<[never]> {
13
+ /**
14
+ * Convenience factory: create an MCPServer with a single pipeline registered.
15
+ *
16
+ * @param {Object} pipeline - Pipeline instance with a `run` method
17
+ * @param {Object} [options={}] - Server and registration options
18
+ * @param {string} [options.name='rag_query'] - Tool name for the pipeline
19
+ * @param {Object} [options.config] - Pipeline-specific configuration
20
+ * @returns {MCPServer} Configured server instance
21
+ */
22
+ static fromPipeline(pipeline: any, options?: {
23
+ name?: string;
24
+ config?: any;
25
+ }): MCPServer;
26
+ /**
27
+ * @param {Object} [options={}] - Server configuration
28
+ * @param {boolean} [options.includeMetadata] - Include metadata in responses
29
+ * @param {number} [options.maxResponseLength] - Maximum response text length
30
+ * @param {Object} [options.toolOptions] - Options forwarded to MCPToolBuilder
31
+ */
32
+ constructor(options?: {
33
+ includeMetadata?: boolean;
34
+ maxResponseLength?: number;
35
+ toolOptions?: any;
36
+ });
37
+ config: any;
38
+ toolBuilder: MCPToolBuilder;
39
+ /** @type {Map<string, {pipeline: Object, config: Object, toolDefinition: Object}>} */
40
+ pipelines: Map<string, {
41
+ pipeline: any;
42
+ config: any;
43
+ toolDefinition: any;
44
+ }>;
45
+ /**
46
+ * Register a pipeline that can be called as an MCP tool.
47
+ *
48
+ * @param {string} name - Tool name used to invoke this pipeline
49
+ * @param {Object} pipeline - Pipeline instance with a `run` method
50
+ * @param {Object} [config={}] - Pipeline-specific configuration
51
+ * @returns {void}
52
+ */
53
+ registerPipeline(name: string, pipeline: any, config?: any): void;
54
+ /**
55
+ * Return array of all registered tool definitions.
56
+ * Each is a valid MCP tool schema.
57
+ *
58
+ * @returns {Array<Object>} Array of MCP tool definition objects
59
+ */
60
+ getToolDefinitions(): Array<any>;
61
+ /**
62
+ * Handle an MCP tool_use request by running the matching pipeline.
63
+ *
64
+ * @param {string} toolName - Name of the tool to invoke
65
+ * @param {Object} toolInput - Tool input parameters
66
+ * @param {string} toolInput.query - The search query (required)
67
+ * @param {number} [toolInput.topK] - Number of results to retrieve
68
+ * @param {boolean} [toolInput.stream] - Enable streaming response
69
+ * @param {boolean} [toolInput.citations] - Include source citations
70
+ * @param {boolean} [toolInput.evaluate] - Include quality evaluation metrics
71
+ * @returns {Promise<Object>} MCP-formatted response with type and text fields
72
+ * @throws {Error} If tool name is not registered or query is missing
73
+ */
74
+ handleToolUse(toolName: string, toolInput: {
75
+ query: string;
76
+ topK?: number;
77
+ stream?: boolean;
78
+ citations?: boolean;
79
+ evaluate?: boolean;
80
+ }): Promise<any>;
81
+ }
82
+ /** @type {Object} Default configuration for the MCP server */
83
+ export const DEFAULT_CONFIG: any;
84
+ import { EventEmitter } from "events";
85
+ import { MCPToolBuilder } from "./mcp-tool-builder";
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Builds MCP-compatible tool definitions from RAG pipeline configuration.
3
+ *
4
+ * @example
5
+ * const builder = new MCPToolBuilder({ toolName: 'my_rag_tool' });
6
+ * const definition = builder.buildToolDefinition({ capabilities: { stream: true } });
7
+ */
8
+ export class MCPToolBuilder {
9
+ /**
10
+ * @param {Object} [options={}] - Builder configuration
11
+ * @param {string} [options.toolName] - Name for the generated MCP tool
12
+ * @param {string} [options.toolDescription] - Description for the generated MCP tool
13
+ */
14
+ constructor(options?: {
15
+ toolName?: string;
16
+ toolDescription?: string;
17
+ });
18
+ config: any;
19
+ /**
20
+ * Generate an MCP-compatible tool definition from pipeline configuration.
21
+ *
22
+ * @param {Object} [pipelineConfig={}] - Pipeline-specific configuration
23
+ * @param {Object} [pipelineConfig.capabilities] - Pipeline capabilities to reflect in schema
24
+ * @param {boolean} [pipelineConfig.capabilities.stream] - Whether streaming is supported
25
+ * @param {boolean} [pipelineConfig.capabilities.citations] - Whether citations are supported
26
+ * @param {boolean} [pipelineConfig.capabilities.evaluate] - Whether evaluation is supported
27
+ * @param {number} [pipelineConfig.capabilities.maxTopK] - Maximum allowed topK value
28
+ * @returns {Object} MCP tool definition with name, description, and input_schema
29
+ */
30
+ buildToolDefinition(pipelineConfig?: {
31
+ capabilities?: {
32
+ stream?: boolean;
33
+ citations?: boolean;
34
+ evaluate?: boolean;
35
+ maxTopK?: number;
36
+ };
37
+ }): any;
38
+ /**
39
+ * Build tool definitions for multiple named pipelines.
40
+ *
41
+ * @param {Array<Object>} pipelines - Array of pipeline descriptors
42
+ * @param {string} pipelines[].name - Tool name for this pipeline
43
+ * @param {string} [pipelines[].description] - Tool description
44
+ * @param {Object} [pipelines[].pipeline] - Pipeline instance (unused during definition building)
45
+ * @param {Object} [pipelines[].config] - Pipeline configuration passed to buildToolDefinition
46
+ * @returns {Array<Object>} Array of MCP tool definitions
47
+ */
48
+ buildMultipleTools(pipelines: Array<any>): Array<any>;
49
+ }
50
+ /**
51
+ * @module mcp/mcp-tool-builder
52
+ * @description Builds MCP tool definitions from pipeline configuration.
53
+ */
54
+ /** @type {Object} Default configuration for the MCP tool builder */
55
+ export const DEFAULT_CONFIG: any;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Chains multiple rerankers in sequence, passing the output of each
3
+ * stage as input to the next. Implements the reranker contract:
4
+ * rerank(query, documents, options).
5
+ *
6
+ * @extends EventEmitter
7
+ */
8
+ export class CascadeReranker extends EventEmitter<[never]> {
9
+ /**
10
+ * @param {object[]} [rerankers=[]] - Array of objects with .rerank() method
11
+ * @param {object} [options]
12
+ */
13
+ constructor(rerankers?: object[], options?: object);
14
+ rerankers: any[];
15
+ config: any;
16
+ /**
17
+ * Run documents through each reranker in sequence.
18
+ *
19
+ * @param {string} query - The search query
20
+ * @param {Array<object|string>} documents - Documents to rerank
21
+ * @param {object} [options]
22
+ * @param {number} [options.topK] - Maximum number of results to return
23
+ * @param {AbortSignal} [options.signal] - Forwarded to every stage; checked between stages
24
+ * @returns {Promise<object[]>} Reranked documents from the final stage
25
+ * @throws {Error} If no rerankers are configured
26
+ */
27
+ rerank(query: string, documents: Array<object | string>, options?: {
28
+ topK?: number;
29
+ signal?: AbortSignal;
30
+ }): Promise<object[]>;
31
+ /**
32
+ * Add a reranker to the end of the cascade.
33
+ *
34
+ * @param {object} reranker - Must have a .rerank() method
35
+ */
36
+ addReranker(reranker: object): void;
37
+ }
38
+ /** @type {object} */
39
+ export const DEFAULT_CONFIG: object;
40
+ import { EventEmitter } from "events";
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Reranks documents by computing cosine similarity between query and
3
+ * document embeddings produced by an external embedder.
4
+ * Implements the reranker contract: rerank(query, documents, options).
5
+ *
6
+ * Document embeddings are cached in a bounded LRU keyed by a SHA-256 of the
7
+ * document text, and embedding calls run with bounded concurrency.
8
+ *
9
+ * @extends EventEmitter
10
+ */
11
+ export class EmbeddingReranker extends EventEmitter<[never]> {
12
+ /**
13
+ * @param {object} embedder - Must expose .embedQuery(text) or .embed(text)
14
+ * @param {object} [options]
15
+ * @param {number} [options.cacheSize=1000] - Max cached document embeddings (0 disables caching)
16
+ * @param {number} [options.concurrency=4] - Max concurrent embedding calls
17
+ */
18
+ constructor(embedder: object, options?: {
19
+ cacheSize?: number;
20
+ concurrency?: number;
21
+ });
22
+ embedder: any;
23
+ config: {
24
+ cacheSize: number;
25
+ concurrency: number;
26
+ };
27
+ /** @type {Map<string, Promise<number[]>>} LRU: oldest entry first */
28
+ _cache: Map<string, Promise<number[]>>;
29
+ cacheStats: {
30
+ hits: number;
31
+ misses: number;
32
+ };
33
+ /**
34
+ * Call the embedder, forwarding the signal only when one is given.
35
+ * @private
36
+ */
37
+ private _embed;
38
+ /**
39
+ * Embed a document, using the LRU cache. In-flight promises are cached so
40
+ * duplicate documents in one batch are embedded once; failed embeddings
41
+ * are evicted so they can be retried.
42
+ * @private
43
+ */
44
+ private _embedDocument;
45
+ /**
46
+ * Clear the document embedding cache.
47
+ */
48
+ clearCache(): void;
49
+ /**
50
+ * Rerank documents by cosine similarity to the query embedding.
51
+ *
52
+ * @param {string} query - The search query
53
+ * @param {Array<object|string>} documents - Documents to rerank
54
+ * @param {object} [options]
55
+ * @param {number} [options.topK] - Maximum number of results to return
56
+ * @param {AbortSignal} [options.signal] - Cancels pending embedding calls
57
+ * @returns {Promise<object[]>} Reranked documents with .relevanceScore
58
+ */
59
+ rerank(query: string, documents: Array<object | string>, options?: {
60
+ topK?: number;
61
+ signal?: AbortSignal;
62
+ }): Promise<object[]>;
63
+ /**
64
+ * Compute cosine similarity between two vectors.
65
+ *
66
+ * @param {number[]} a - First vector
67
+ * @param {number[]} b - Second vector
68
+ * @returns {number} Cosine similarity in [-1, 1], or 0 for zero vectors
69
+ */
70
+ _cosineSimilarity(a: number[], b: number[]): number;
71
+ }
72
+ /** @type {{ cacheSize: number, concurrency: number }} */
73
+ export const DEFAULT_CONFIG: {
74
+ cacheSize: number;
75
+ concurrency: number;
76
+ };
77
+ import { EventEmitter } from "events";
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Reranks documents using BM25-like scoring with IDF weighting.
3
+ * Implements the reranker contract: rerank(query, documents, options).
4
+ *
5
+ * @extends EventEmitter
6
+ */
7
+ export class ScoringReranker extends EventEmitter<[never]> {
8
+ /**
9
+ * @param {object} [options]
10
+ * @param {number} [options.k1=1.2] - Term frequency saturation parameter
11
+ * @param {number} [options.b=0.75] - Length normalisation parameter
12
+ */
13
+ constructor(options?: {
14
+ k1?: number;
15
+ b?: number;
16
+ });
17
+ config: {
18
+ k1: number;
19
+ b: number;
20
+ };
21
+ /**
22
+ * Rerank documents against a query using BM25 scoring.
23
+ *
24
+ * @param {string} query - The search query
25
+ * @param {Array<object|string>} documents - Documents to rerank
26
+ * @param {object} [options]
27
+ * @param {number} [options.topK] - Maximum number of results to return
28
+ * @param {AbortSignal} [options.signal] - Rejects with AbortError when already aborted
29
+ * @returns {Promise<object[]>} Reranked documents with .relevanceScore
30
+ */
31
+ rerank(query: string, documents: Array<object | string>, options?: {
32
+ topK?: number;
33
+ signal?: AbortSignal;
34
+ }): Promise<object[]>;
35
+ }
36
+ /** @type {{ k1: number, b: number }} */
37
+ export const DEFAULT_CONFIG: {
38
+ k1: number;
39
+ b: number;
40
+ };
41
+ import { EventEmitter } from "events";
@@ -0,0 +1,43 @@
1
+ export class BM25Search extends EventEmitter<[never]> {
2
+ constructor(options?: {});
3
+ config: {
4
+ k1: number;
5
+ b: number;
6
+ };
7
+ invertedIndex: Map<any, any>;
8
+ documents: any[];
9
+ /** @type {number[]} token count per document, computed at index time */
10
+ docLengths: number[];
11
+ avgDocLength: number;
12
+ docCount: number;
13
+ /**
14
+ * Index documents for BM25 search
15
+ * @param {Array} documents - [{id, content|text, metadata?}]
16
+ */
17
+ index(documents: any[]): void;
18
+ /**
19
+ * Search using BM25 scoring
20
+ * @param {string} query
21
+ * @param {object} options - {k: 10}
22
+ * @returns {Array} [{id, score, content, metadata}] sorted by BM25 score
23
+ */
24
+ search(query: string, options?: object): any[];
25
+ /**
26
+ * Clear the index
27
+ */
28
+ clear(): void;
29
+ /**
30
+ * Get index stats
31
+ * @returns {{docCount: number, uniqueTerms: number, avgDocLength: number}}
32
+ */
33
+ getStats(): {
34
+ docCount: number;
35
+ uniqueTerms: number;
36
+ avgDocLength: number;
37
+ };
38
+ }
39
+ export namespace DEFAULT_CONFIG {
40
+ let k1: number;
41
+ let b: number;
42
+ }
43
+ import { EventEmitter } from "events";
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Vector store persisted to a single JSON file, so separate processes (e.g.
3
+ * `rag-pipeline ingest` then `rag-pipeline query`) share one index. Intended
4
+ * for local use and small corpora: every query is a linear scan.
5
+ *
6
+ * The index records which embedder produced the vectors (name, dimensions and
7
+ * optional serialized state) and refuses queries whose vector dimension does
8
+ * not match, instead of silently comparing incompatible vectors.
9
+ */
10
+ export class FileVectorStore {
11
+ /**
12
+ * @param {object} options
13
+ * @param {string} options.path - Index file location
14
+ */
15
+ constructor(options?: {
16
+ path: string;
17
+ });
18
+ path: string;
19
+ _index: any;
20
+ _loadedDigest: string;
21
+ _cacheId: string;
22
+ _revision: number;
23
+ /**
24
+ * Cache identity for createRagPipeline({ cache }): a digest of the index
25
+ * as loaded, so processes reading the same unchanged index share cached
26
+ * results; after this instance changes the index the key also carries the
27
+ * instance and a revision.
28
+ * @param {'retriever'} role
29
+ * @returns {string|null}
30
+ */
31
+ getCacheKey(role: "retriever"): string | null;
32
+ _emptyIndex(): {
33
+ version: number;
34
+ embedder: any;
35
+ documents: {};
36
+ };
37
+ /**
38
+ * Read the index from disk (cached after the first call).
39
+ * @returns {{version: number, embedder: object|null, documents: object}}
40
+ */
41
+ load(): {
42
+ version: number;
43
+ embedder: object | null;
44
+ documents: object;
45
+ };
46
+ /** Write the index atomically: temp file in the same directory, then rename. */
47
+ save(): void;
48
+ /** @returns {object|null} Embedder metadata recorded with the vectors */
49
+ getEmbedderInfo(): object | null;
50
+ /** @param {{name: string, dimensions: number, state?: object}} info */
51
+ setEmbedderInfo(info: {
52
+ name: string;
53
+ dimensions: number;
54
+ state?: object;
55
+ }): void;
56
+ /**
57
+ * Add or replace documents. Ids are caller-chosen; re-storing an id
58
+ * replaces the previous entry.
59
+ *
60
+ * @param {Array<{id: string, vector: number[], content: string, metadata?: object}>} docs
61
+ */
62
+ store(docs: Array<{
63
+ id: string;
64
+ vector: number[];
65
+ content: string;
66
+ metadata?: object;
67
+ }>): Promise<void>;
68
+ /**
69
+ * Remove every document whose metadata.source equals `source`.
70
+ * @param {string} source
71
+ * @returns {number} Number of documents removed
72
+ */
73
+ removeBySource(source: string): number;
74
+ /** Remove every document and the recorded embedder info. */
75
+ clear(): void;
76
+ /** @returns {Array<{id: string, content: string, metadata: object}>} */
77
+ list(): Array<{
78
+ id: string;
79
+ content: string;
80
+ metadata: object;
81
+ }>;
82
+ /** @returns {number} */
83
+ size(): number;
84
+ /**
85
+ * Cosine-similarity search.
86
+ *
87
+ * @param {{queryVector: number[], topK?: number}} request
88
+ * @returns {Promise<Array<{id: string, score: number, content: string, metadata: object}>>}
89
+ */
90
+ retrieve(request?: {
91
+ queryVector: number[];
92
+ topK?: number;
93
+ }): Promise<Array<{
94
+ id: string;
95
+ score: number;
96
+ content: string;
97
+ metadata: object;
98
+ }>>;
99
+ }