@devilsdev/rag-core 0.0.0-stage → 3.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.
- package/LICENSE +21 -0
- package/README.md +18 -2
- package/all-types.d.ts +1 -0
- package/dist/all.d.ts +498 -0
- package/dist/index.cjs +6839 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +5 -0
- package/dist/types/adapters/ai-sdk.d.ts +53 -0
- package/dist/types/agentic/agentic-pipeline.d.ts +103 -0
- package/dist/types/agentic/iterative-retriever.d.ts +85 -0
- package/dist/types/agentic/query-planner.d.ts +145 -0
- package/dist/types/agentic/self-critique.d.ts +45 -0
- package/dist/types/ai/adaptive-retrieval.d.ts +44 -0
- package/dist/types/ai/federation/federated-learning-coordinator.d.ts +62 -0
- package/dist/types/ai/multimodal-processing.d.ts +32 -0
- package/dist/types/ai/multimodal.d.ts +39 -0
- package/dist/types/ai/retrieval-engine.d.ts +37 -0
- package/dist/types/ai/training/model-training-orchestrator.d.ts +31 -0
- package/dist/types/cache/component-keys.d.ts +7 -0
- package/dist/types/cache/memory-cache.d.ts +57 -0
- package/dist/types/cache/pipeline-cache.d.ts +66 -0
- package/dist/types/cache/redis-cache.d.ts +49 -0
- package/dist/types/chunking/chunking-engine.d.ts +70 -0
- package/dist/types/chunking/strategies/fixed-size.d.ts +17 -0
- package/dist/types/chunking/strategies/recursive.d.ts +20 -0
- package/dist/types/chunking/strategies/semantic.d.ts +19 -0
- package/dist/types/chunking/strategies/sentence.d.ts +13 -0
- package/dist/types/chunking/strategies/structure-aware.d.ts +19 -0
- package/dist/types/citation/citation-tracker.d.ts +64 -0
- package/dist/types/citation/hallucination-detector.d.ts +40 -0
- package/dist/types/citation/source-mapper.d.ts +37 -0
- package/dist/types/cli/services/instrument.d.ts +14 -0
- package/dist/types/cli/services/rag-service.d.ts +145 -0
- package/dist/types/cli/shared/config-context.d.ts +9 -0
- package/dist/types/config/component-factory.d.ts +58 -0
- package/dist/types/config/enhanced-ragrc-schema.d.ts +648 -0
- package/dist/types/config/load-config.d.ts +18 -0
- package/dist/types/config/normalize-config.d.ts +9 -0
- package/dist/types/connectors/anthropic-connector.d.ts +124 -0
- package/dist/types/connectors/base-connector.d.ts +87 -0
- package/dist/types/connectors/cohere-connector.d.ts +94 -0
- package/dist/types/connectors/connector-utils.d.ts +121 -0
- package/dist/types/connectors/local-embedder.d.ts +95 -0
- package/dist/types/connectors/memory-retriever.d.ts +100 -0
- package/dist/types/connectors/ollama-connector.d.ts +115 -0
- package/dist/types/connectors/openai-connector.d.ts +131 -0
- package/dist/types/connectors/rag-prompt.d.ts +40 -0
- package/dist/types/core/context-packing.d.ts +48 -0
- package/dist/types/core/create-pipeline.d.ts +6 -0
- package/dist/types/core/observability/event-logger.d.ts +212 -0
- package/dist/types/core/observability/metrics.d.ts +584 -0
- package/dist/types/core/performance/parallel-processor.d.ts +92 -0
- package/dist/types/core/plugin-registry.d.ts +141 -0
- package/dist/types/core/stage-contracts.d.ts +36 -0
- package/dist/types/core/validate-plugin.d.ts +45 -0
- package/dist/types/cost/cost-calculator.d.ts +152 -0
- package/dist/types/cost/cost-tracker.d.ts +126 -0
- package/dist/types/cost/model-router.d.ts +111 -0
- package/dist/types/cost/token-budget.d.ts +110 -0
- package/dist/types/dag/core/error-context.d.ts +239 -0
- package/dist/types/dag/core/execution-scheduler.d.ts +193 -0
- package/dist/types/dag/core/topology-validation.d.ts +226 -0
- package/dist/types/dag/dag-engine.d.ts +169 -0
- package/dist/types/dag/dag-node.d.ts +37 -0
- package/dist/types/dashboard/dashboard-generator.d.ts +127 -0
- package/dist/types/dashboard/metrics-aggregator.d.ts +218 -0
- package/dist/types/debug/execution-tracer.d.ts +57 -0
- package/dist/types/debug/trace-inspector.d.ts +31 -0
- package/dist/types/dev/dev-server.d.ts +136 -0
- package/dist/types/dev/hot-reload.d.ts +226 -0
- package/dist/types/ecosystem/plugin-analytics-dashboard.d.ts +141 -0
- package/dist/types/ecosystem/plugin-certification.d.ts +174 -0
- package/dist/types/ecosystem/plugin-hub.d.ts +166 -0
- package/dist/types/ecosystem/plugin-installer.d.ts +40 -0
- package/dist/types/ecosystem/plugin-probe.d.ts +21 -0
- package/dist/types/ecosystem/plugin-scanner.d.ts +30 -0
- package/dist/types/ecosystem/tar-extract.d.ts +24 -0
- package/dist/types/enterprise/audit-logging.d.ts +224 -0
- package/dist/types/enterprise/data-governance.d.ts +203 -0
- package/dist/types/enterprise/multi-tenancy.d.ts +137 -0
- package/dist/types/enterprise/sso-integration.d.ts +420 -0
- package/dist/types/evaluate/compare.d.ts +140 -0
- package/dist/types/evaluate/context-metrics.d.ts +55 -0
- package/dist/types/evaluate/dataset.d.ts +43 -0
- package/dist/types/evaluate/evaluator.d.ts +14 -0
- package/dist/types/evaluate/faithfulness.d.ts +41 -0
- package/dist/types/evaluate/groundedness.d.ts +60 -0
- package/dist/types/evaluate/pipeline-evaluator.d.ts +88 -0
- package/dist/types/evaluate/relevance.d.ts +21 -0
- package/dist/types/evaluate/report.d.ts +56 -0
- package/dist/types/evaluate/scoring.d.ts +123 -0
- package/dist/types/evaluate/semantic-similarity.d.ts +14 -0
- package/dist/types/evaluate/synthetic.d.ts +64 -0
- package/dist/types/experimental.d.ts +15 -0
- package/dist/types/graph/entity-extractor.d.ts +84 -0
- package/dist/types/graph/graph-index.d.ts +49 -0
- package/dist/types/graph/graph-retriever.d.ts +81 -0
- package/dist/types/graph/knowledge-graph.d.ts +129 -0
- package/dist/types/grounding/check-answer.d.ts +101 -0
- package/dist/types/guardrails/guardrails-pipeline.d.ts +63 -0
- package/dist/types/guardrails/injection-detector.d.ts +47 -0
- package/dist/types/guardrails/post-generation-guard.d.ts +61 -0
- package/dist/types/guardrails/pre-retrieval-guard.d.ts +64 -0
- package/dist/types/guardrails/retrieval-guard.d.ts +57 -0
- package/dist/types/loader/csv-loader.d.ts +10 -0
- package/dist/types/loader/document-reader.d.ts +18 -0
- package/dist/types/loader/html-loader.d.ts +21 -0
- package/dist/types/loader/markdown-loader.d.ts +21 -0
- package/dist/types/mcp/mcp-server.d.ts +85 -0
- package/dist/types/mcp/mcp-tool-builder.d.ts +55 -0
- package/dist/types/observability/otel.d.ts +24 -0
- package/dist/types/reranker/cascade-reranker.d.ts +44 -0
- package/dist/types/reranker/embedding-reranker.d.ts +81 -0
- package/dist/types/reranker/scoring-reranker.d.ts +45 -0
- package/dist/types/retrieval/bm25-search.d.ts +47 -0
- package/dist/types/retrieval/file-vector-store.d.ts +103 -0
- package/dist/types/retrieval/hybrid-retriever.d.ts +39 -0
- package/dist/types/retrieval/pgvector-retriever.d.ts +125 -0
- package/dist/types/retrieval/qdrant-retriever.d.ts +108 -0
- package/dist/types/retrieval/rank-fusion.d.ts +10 -0
- package/dist/types/security/jwt-config.d.ts +80 -0
- package/dist/types/security/jwt-validator.d.ts +195 -0
- package/dist/types/security/plugin-sandbox.d.ts +159 -0
- package/dist/types/security/plugin-signature-verifier.d.ts +139 -0
- package/dist/types/security/validator.d.ts +15 -0
- package/dist/types/streaming/embedding-stream.d.ts +65 -0
- package/dist/types/streaming/sse-adapter.d.ts +59 -0
- package/dist/types/streaming/stream-router.d.ts +88 -0
- package/dist/types/streaming/streaming-embedder.d.ts +99 -0
- package/dist/types/streaming/websocket-adapter.d.ts +71 -0
- package/dist/types/templates/scaffold.d.ts +55 -0
- package/dist/types/templates/template-registry.d.ts +63 -0
- package/dist/types/testing/contract-tests.d.ts +57 -0
- package/dist/types/testing.d.ts +173 -0
- package/dist/types/utils/batch-processor.d.ts +205 -0
- package/dist/types/utils/connection-pool.d.ts +158 -0
- package/dist/types/utils/embed-query.d.ts +20 -0
- package/dist/types/utils/error-formatter.d.ts +382 -0
- package/dist/types/utils/logger.d.ts +75 -0
- package/dist/types/utils/optional-peer.d.ts +16 -0
- package/dist/types/utils/rate-limiter.d.ts +71 -0
- package/dist/types/utils/retry.d.ts +37 -0
- package/dist/types/utils/run-abort.d.ts +34 -0
- package/dist/types/utils/secure-logging.d.ts +51 -0
- package/dist/types/utils/structured-logger.d.ts +126 -0
- package/package.json +61 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ali Kahwaji
|
|
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,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @devilsdev/rag-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
createRagPipeline and its building blocks: retrieval, reranking, caching, cost tracking and model routing.
|
|
4
|
+
|
|
5
|
+
Part of [@devilsdev/rag-pipeline-utils](https://www.npmjs.com/package/@devilsdev/rag-pipeline-utils), split into packages so you install only what you use. The objects are the same ones the full package exports; versions move in lockstep.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @devilsdev/rag-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Exports: `createRagPipeline`, `createPipeline`, `packContext`, `createError`, `wrapError`, `ERROR_CODES`, `retry`, `withRetry`, `ChunkingEngine`, `BM25Search`, `reciprocalRankFusion`, `HybridRetriever`, `ScoringReranker`, `EmbeddingReranker`, `CascadeReranker`, `MemoryCache`, `RedisCache`, `CostCalculator`, `PRICING_METADATA`, `CostTracker`, `TokenBudget`, `createModelRouter`, `classifyQuery`, `MemoryRetriever`, `BaseConnector`.
|
|
12
|
+
|
|
13
|
+
Documentation: https://devilsdev.github.io/rag-pipeline-utils/
|
|
14
|
+
|
|
15
|
+
The other packages:
|
|
16
|
+
|
|
17
|
+
- [`@devilsdev/rag-connectors`](https://www.npmjs.com/package/@devilsdev/rag-connectors)
|
|
18
|
+
- [`@devilsdev/rag-eval`](https://www.npmjs.com/package/@devilsdev/rag-eval)
|
|
19
|
+
- [`@devilsdev/rag-guardrails`](https://www.npmjs.com/package/@devilsdev/rag-guardrails)
|
package/all-types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./dist/all";
|
package/dist/all.d.ts
ADDED
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RAG Pipeline Utils - TypeScript Definitions
|
|
3
|
+
* @module rag-pipeline-utils
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// =============================================================================
|
|
7
|
+
// Pipeline
|
|
8
|
+
// =============================================================================
|
|
9
|
+
|
|
10
|
+
/** Options for {@link createRagPipeline}. */
|
|
11
|
+
export interface RagPipelineConfig {
|
|
12
|
+
/** Resolves plugin names given as strings; defaults to the shared {@link pluginRegistry}. */
|
|
13
|
+
registry?: PluginRegistry;
|
|
14
|
+
/** Called with no arguments at the start of every run; its result is ignored. */
|
|
15
|
+
loader?: string | LoaderPlugin;
|
|
16
|
+
/** Embeds the query when `run()` gets a `query` and no `queryVector`. */
|
|
17
|
+
embedder?: string | EmbedderPlugin;
|
|
18
|
+
/** Finds the documents for a query. Without one, every run retrieves nothing. */
|
|
19
|
+
retriever?: string | RetrieverPlugin;
|
|
20
|
+
/** Writes the answer. Without one the pipeline is retrieval-only and `answer` is null. */
|
|
21
|
+
llm?: string | LLMPlugin;
|
|
22
|
+
/** Reorders the retrieved documents before generation. */
|
|
23
|
+
reranker?: string | RerankerPlugin;
|
|
24
|
+
/**
|
|
25
|
+
* Cache query embeddings, retrieval results and answers. `true` caches
|
|
26
|
+
* all three in memory; a CacheBackend (MemoryCache, RedisCache, your own)
|
|
27
|
+
* caches all three in it; or configure each layer.
|
|
28
|
+
*/
|
|
29
|
+
cache?: boolean | CacheBackend | CacheOptions;
|
|
30
|
+
/**
|
|
31
|
+
* OpenTelemetry spans for every run and stage. `true` uses the global
|
|
32
|
+
* tracer provider. Needs the optional peer dependency `@opentelemetry/api`.
|
|
33
|
+
*/
|
|
34
|
+
telemetry?: boolean | TelemetryOptions;
|
|
35
|
+
/** Other keys are ignored. */
|
|
36
|
+
[key: string]: any;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A tracer that can start active spans, such as an OpenTelemetry `Tracer`.
|
|
41
|
+
* Declared here so the types don't depend on the optional peer dependency.
|
|
42
|
+
*/
|
|
43
|
+
export interface TelemetryTracer {
|
|
44
|
+
startActiveSpan(...args: any[]): any;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The `telemetry` option of createRagPipeline. */
|
|
48
|
+
export interface TelemetryOptions {
|
|
49
|
+
/** Default: `trace.getTracer("@devilsdev/rag-pipeline-utils", version)`. */
|
|
50
|
+
tracer?: TelemetryTracer;
|
|
51
|
+
/** The @opentelemetry/api module, for @devilsdev/rag-pipeline-utils/edge, which cannot load it itself. */
|
|
52
|
+
api?: any;
|
|
53
|
+
/** Also record the query text and the retrieved document ids and scores. Default false. */
|
|
54
|
+
recordContent?: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A cache store: get / set / delete, optionally stats. */
|
|
58
|
+
export interface CacheBackend {
|
|
59
|
+
/** Resolves the stored value, or undefined when the key is missing or expired. */
|
|
60
|
+
get(key: string): Promise<unknown | undefined>;
|
|
61
|
+
/** Stores a value, expiring it after `ttlSeconds` when given. */
|
|
62
|
+
set(key: string, value: unknown, options?: { ttlSeconds?: number }): Promise<void>;
|
|
63
|
+
/** Removes a key. */
|
|
64
|
+
delete?(key: string): Promise<void>;
|
|
65
|
+
/** Backend-specific counters, such as size or hits. */
|
|
66
|
+
stats?(): Promise<Record<string, number>>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Per-layer cache settings: `true` for memory, a CacheBackend, or false/omitted for none. */
|
|
70
|
+
export interface CacheOptions {
|
|
71
|
+
embeddings?: boolean | CacheBackend;
|
|
72
|
+
retrieval?: boolean | CacheBackend;
|
|
73
|
+
generation?: boolean | CacheBackend;
|
|
74
|
+
/** Defaults: embeddings 30 days, retrieval 1 hour, generation 24 hours. */
|
|
75
|
+
ttlSeconds?: { embeddings?: number; retrieval?: number; generation?: number };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** What a cache layer did for one run: served it, stored it, or was not used. */
|
|
79
|
+
export type CacheOutcome = "hit" | "miss" | "skip";
|
|
80
|
+
|
|
81
|
+
/** Counts for one cache layer since the pipeline was created. */
|
|
82
|
+
export interface CacheLayerStats {
|
|
83
|
+
hits: number;
|
|
84
|
+
misses: number;
|
|
85
|
+
skipped: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The argument of {@link Pipeline.run}. Give a `query`, a `queryVector`, or both. */
|
|
89
|
+
export interface PipelineRunOptions {
|
|
90
|
+
/** The question. Embedded by the embedder unless `queryVector` is given. */
|
|
91
|
+
query?: string;
|
|
92
|
+
/** A precomputed query embedding; skips the embedder. */
|
|
93
|
+
queryVector?: number[];
|
|
94
|
+
/** Per-run options. The whole object is also passed to `llm.generate`. */
|
|
95
|
+
options?: {
|
|
96
|
+
/** Documents to retrieve. Default 3. */
|
|
97
|
+
topK?: number;
|
|
98
|
+
/** Abort the run after this many milliseconds; it resolves `{ success: false, error: "Execution timeout" }`. */
|
|
99
|
+
timeout?: number;
|
|
100
|
+
/** Cancel the run; it resolves `{ success: false, error: "Execution aborted" }`. */
|
|
101
|
+
signal?: AbortSignal;
|
|
102
|
+
/** Resolve to an async iterable of {@link StreamChunk} instead of a result. */
|
|
103
|
+
stream?: boolean;
|
|
104
|
+
/** Attribute the answer's sentences to the retrieved documents (see CitationTracker). */
|
|
105
|
+
citations?: boolean;
|
|
106
|
+
citationOptions?: Record<string, any>;
|
|
107
|
+
/** Score the run (see PipelineEvaluator). */
|
|
108
|
+
evaluate?: boolean;
|
|
109
|
+
evaluateOptions?: Record<string, any>;
|
|
110
|
+
/** Ground-truth answer, used by evaluation for context recall. */
|
|
111
|
+
referenceAnswer?: string;
|
|
112
|
+
/** Filter or transform the retrieved documents before reranking. */
|
|
113
|
+
filterResults?: (docs: SearchResult[]) => SearchResult[] | Promise<SearchResult[]>;
|
|
114
|
+
/**
|
|
115
|
+
* Fit the documents the LLM sees into this many tokens (see packContext).
|
|
116
|
+
* `results` is then the packed list, and `packing` says what was cut.
|
|
117
|
+
*/
|
|
118
|
+
maxContextTokens?: number;
|
|
119
|
+
/** Token counter for `maxContextTokens` (default: about four characters per token). */
|
|
120
|
+
countTokens?: (text: string) => number;
|
|
121
|
+
[key: string]: any;
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* What {@link Pipeline.run} resolves to without streaming. A failed, timed-out
|
|
127
|
+
* or aborted run resolves `{ success: false, error }`; `run` never rejects.
|
|
128
|
+
*/
|
|
129
|
+
export interface PipelineRunResult {
|
|
130
|
+
success: boolean;
|
|
131
|
+
query?: string;
|
|
132
|
+
/** The retrieved (filtered, reranked) documents; with `maxContextTokens`, the packed ones the LLM saw. */
|
|
133
|
+
results?: SearchResult[];
|
|
134
|
+
/** The LLM's answer; null when the pipeline has no llm. */
|
|
135
|
+
answer?: string | null;
|
|
136
|
+
/** Present when `options.citations` was set and there is an answer. */
|
|
137
|
+
citations?: any;
|
|
138
|
+
/** Present when `options.evaluate` was set. */
|
|
139
|
+
evaluation?: any;
|
|
140
|
+
/** The error message when `success` is false. */
|
|
141
|
+
error?: string;
|
|
142
|
+
/** Per-layer cache outcome; present when the pipeline has a cache. */
|
|
143
|
+
cache?: {
|
|
144
|
+
embeddings?: CacheOutcome;
|
|
145
|
+
retrieval?: CacheOutcome;
|
|
146
|
+
generation?: CacheOutcome;
|
|
147
|
+
};
|
|
148
|
+
/** What `maxContextTokens` kept and cut; present when it was set. */
|
|
149
|
+
packing?: ContextPacking;
|
|
150
|
+
/** How long the run took, from the start of run(); on successful results. */
|
|
151
|
+
timing?: { totalMs: number };
|
|
152
|
+
/** The model router's decision, when the llm is a createModelRouter() and was called. */
|
|
153
|
+
route?: RouteDecision;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Which model a createModelRouter() chose for a run. */
|
|
157
|
+
export interface RouteDecision {
|
|
158
|
+
/** The query class from the router's classifier. */
|
|
159
|
+
class: string;
|
|
160
|
+
/** The model that answered. */
|
|
161
|
+
model: string;
|
|
162
|
+
/** Estimated cost in USD of the chosen model for this query. */
|
|
163
|
+
estimatedCost: number;
|
|
164
|
+
/** Models that failed before `model` answered (with `fallback: true`). */
|
|
165
|
+
fallbackFrom?: string[];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** When a streamed answer started and finished, from the start of run(). */
|
|
169
|
+
export interface StreamTiming {
|
|
170
|
+
/** When the first non-empty token arrived; null when none did. */
|
|
171
|
+
firstTokenMs: number | null;
|
|
172
|
+
totalMs: number;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** How the documents were fitted into `maxContextTokens`. */
|
|
176
|
+
export interface ContextPacking {
|
|
177
|
+
/** Tokens used by the kept documents, wrappers included. */
|
|
178
|
+
tokens: number;
|
|
179
|
+
maxTokens: number;
|
|
180
|
+
/** Ids (or positions) of the documents passed to the LLM, in order. */
|
|
181
|
+
kept: Array<string | number>;
|
|
182
|
+
/** Ids of the documents left out because they didn't fit. */
|
|
183
|
+
dropped: Array<string | number>;
|
|
184
|
+
/** Ids of documents cut to fit (marked `metadata.truncated`). */
|
|
185
|
+
truncated: Array<string | number>;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* One chunk of a streamed answer. The last chunk has `done: true` and, when
|
|
190
|
+
* requested, the run's `citations` and `evaluation`. Without an llm the
|
|
191
|
+
* stream is a single done chunk carrying `results`.
|
|
192
|
+
*/
|
|
193
|
+
export interface StreamChunk {
|
|
194
|
+
token: string;
|
|
195
|
+
done: boolean;
|
|
196
|
+
results?: SearchResult[];
|
|
197
|
+
citations?: any;
|
|
198
|
+
evaluation?: any;
|
|
199
|
+
/** On the last chunk, when `maxContextTokens` was set. */
|
|
200
|
+
packing?: ContextPacking;
|
|
201
|
+
/** On the last chunk: time to first token and total time. */
|
|
202
|
+
timing?: StreamTiming;
|
|
203
|
+
/** On the last chunk, when the llm is a createModelRouter(). */
|
|
204
|
+
route?: RouteDecision;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** A pipeline returned by {@link createRagPipeline}. */
|
|
208
|
+
export interface Pipeline {
|
|
209
|
+
/**
|
|
210
|
+
* Answer one query: (embed) → retrieve → (filter) → (rerank) → generate →
|
|
211
|
+
* (citations, evaluation). Resolves a {@link PipelineRunResult}, or with
|
|
212
|
+
* `options.stream` an async iterable of {@link StreamChunk}.
|
|
213
|
+
*/
|
|
214
|
+
run(options: PipelineRunOptions & { options: { stream: true } }): Promise<PipelineRunResult | AsyncIterable<StreamChunk>>;
|
|
215
|
+
/** Answer one query; see the streaming overload above. */
|
|
216
|
+
run(options?: PipelineRunOptions & { options?: { stream?: false } }): Promise<PipelineRunResult>;
|
|
217
|
+
/** Answer one query; with `options.stream` it may resolve a stream. */
|
|
218
|
+
run(options?: PipelineRunOptions): Promise<PipelineRunResult | AsyncIterable<StreamChunk>>;
|
|
219
|
+
/** Does nothing; kept for compatibility. */
|
|
220
|
+
cleanup(): void;
|
|
221
|
+
/** Hits, misses and skips per cache layer; null without a cache. */
|
|
222
|
+
cacheStats(): {
|
|
223
|
+
embeddings?: CacheLayerStats;
|
|
224
|
+
retrieval?: CacheLayerStats;
|
|
225
|
+
generation?: CacheLayerStats;
|
|
226
|
+
} | null;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// =============================================================================
|
|
230
|
+
// Documents and results
|
|
231
|
+
// =============================================================================
|
|
232
|
+
|
|
233
|
+
/** A document as stored in a retriever. */
|
|
234
|
+
export interface Document {
|
|
235
|
+
id: string;
|
|
236
|
+
content: string;
|
|
237
|
+
metadata?: Record<string, any>;
|
|
238
|
+
/** The document's embedding (the built-in stores call it `vector`). */
|
|
239
|
+
vector?: number[];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** A retrieved document, as the built-in retrievers return it. */
|
|
243
|
+
export interface SearchResult {
|
|
244
|
+
id: string;
|
|
245
|
+
/** Similarity; higher is closer. */
|
|
246
|
+
score: number;
|
|
247
|
+
content?: string;
|
|
248
|
+
metadata?: Record<string, any>;
|
|
249
|
+
[key: string]: any;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** What the pipeline passes to {@link RetrieverPlugin.retrieve}. */
|
|
253
|
+
export interface RetrieveOptions {
|
|
254
|
+
query?: string;
|
|
255
|
+
queryVector?: number[];
|
|
256
|
+
topK: number;
|
|
257
|
+
signal?: AbortSignal;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// =============================================================================
|
|
261
|
+
// Plugin contracts: what createRagPipeline calls on each component
|
|
262
|
+
// =============================================================================
|
|
263
|
+
|
|
264
|
+
/** A loader. To register it with pluginRegistry it needs `metadata: { name, version, type: "loader" }`. */
|
|
265
|
+
export interface LoaderPlugin {
|
|
266
|
+
/** The pipeline calls `load()` with no arguments at the start of each run and ignores the result. */
|
|
267
|
+
load(source?: string, options?: any): unknown;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** An embedding returned by an embedder: a vector, a batch whose first vector is used, or `[{ values | vector | embedding }]`. */
|
|
271
|
+
export type EmbeddingOutput =
|
|
272
|
+
| number[]
|
|
273
|
+
| number[][]
|
|
274
|
+
| Array<{ values?: number[]; vector?: number[]; embedding?: number[] }>;
|
|
275
|
+
|
|
276
|
+
/** An embedder. To register it with pluginRegistry it needs `metadata: { name, version, type: "embedder" }`. */
|
|
277
|
+
export interface EmbedderPlugin {
|
|
278
|
+
/** Embeds one text. Used for the query when `embedQuery` is absent. */
|
|
279
|
+
embed(text: string, options?: { signal?: AbortSignal; [key: string]: any }): EmbeddingOutput | Promise<EmbeddingOutput>;
|
|
280
|
+
/** Embeds a query; preferred over `embed` when present. */
|
|
281
|
+
embedQuery?(text: string, options?: { signal?: AbortSignal; [key: string]: any }): EmbeddingOutput | Promise<EmbeddingOutput>;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** A retriever. To register it with pluginRegistry it needs `store()` and `metadata: { name, version, type: "retriever" }`. */
|
|
285
|
+
export interface RetrieverPlugin {
|
|
286
|
+
/** Returns up to `topK` documents for the query, closest first. */
|
|
287
|
+
retrieve(options: RetrieveOptions): SearchResult[] | Promise<SearchResult[]>;
|
|
288
|
+
/** Adds documents with their embeddings. Not called by the pipeline; required by pluginRegistry and the CLI. */
|
|
289
|
+
store?(documents: Array<Document & { vector: number[] }>): unknown;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** An LLM. To register it with pluginRegistry it needs `metadata: { name, version, type: "llm" }`. */
|
|
293
|
+
export interface LLMPlugin {
|
|
294
|
+
/**
|
|
295
|
+
* Answers the query from the retrieved documents. Must resolve to the
|
|
296
|
+
* answer text: the pipeline converts the result with `String()`.
|
|
297
|
+
* `options` is the run's `options` plus its AbortSignal.
|
|
298
|
+
*/
|
|
299
|
+
generate(query: string, documents: SearchResult[], options?: { signal?: AbortSignal; [key: string]: any }): string | Promise<string>;
|
|
300
|
+
/** Streams the answer; used with `options.stream` when present, otherwise `generate` is yielded as one chunk. */
|
|
301
|
+
generateStream?(query: string, documents: SearchResult[], options?: { signal?: AbortSignal; [key: string]: any }): AsyncIterable<string | { token: string; done?: boolean }>;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** A reranker. To register it with pluginRegistry it needs `metadata: { name, version, type: "reranker" }`. */
|
|
305
|
+
export interface RerankerPlugin {
|
|
306
|
+
/** Reorders (and may trim) the retrieved documents for the query. */
|
|
307
|
+
rerank(query: string, documents: SearchResult[], options?: { topK?: number; signal?: AbortSignal }): SearchResult[] | Promise<SearchResult[]>;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** The interface of {@link pluginRegistry}. */
|
|
311
|
+
export interface PluginRegistry {
|
|
312
|
+
/**
|
|
313
|
+
* Registers a plugin after checking it against its type's contract and,
|
|
314
|
+
* when signature verification is enabled, its signed manifest.
|
|
315
|
+
* Types: loader, embedder, retriever, reranker, llm, evaluator.
|
|
316
|
+
*/
|
|
317
|
+
register(type: string, name: string, implementation: any, manifest?: any, options?: { content?: any }): Promise<PluginRegistry>;
|
|
318
|
+
/** Returns a registered plugin; throws when it is not registered. */
|
|
319
|
+
get(type: string, name: string): any;
|
|
320
|
+
/** The names registered for a type. */
|
|
321
|
+
list(type: string): string[];
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** The interface of the `logger` export's methods and of `logger.logger`. */
|
|
325
|
+
export interface Logger {
|
|
326
|
+
/** Logs at info level; `meta` may be an object, an Error or a value. */
|
|
327
|
+
info(message: string, meta?: any): void;
|
|
328
|
+
/** Logs at error level. */
|
|
329
|
+
error(message: string, meta?: any): void;
|
|
330
|
+
/** Logs at warn level. */
|
|
331
|
+
warn(message: string, meta?: any): void;
|
|
332
|
+
/** Logs at debug level. */
|
|
333
|
+
debug(message: string, meta?: any): void;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** An error created by {@link createError} or {@link wrapError}. */
|
|
337
|
+
export interface EnhancedError extends Error {
|
|
338
|
+
code: string;
|
|
339
|
+
context: Record<string, any>;
|
|
340
|
+
suggestions: string[];
|
|
341
|
+
documentation?: string;
|
|
342
|
+
originalError?: Error | null;
|
|
343
|
+
timestamp: string;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// =============================================================================
|
|
347
|
+
// Pipeline factory and errors
|
|
348
|
+
// =============================================================================
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Creates a RAG pipeline from components: plugin objects, or names
|
|
352
|
+
* registered in the registry. A name that is not registered throws here.
|
|
353
|
+
*/
|
|
354
|
+
export function createRagPipeline(config?: RagPipelineConfig): Pipeline;
|
|
355
|
+
export { createRagPipeline as createPipeline };
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Creates an error whose message and suggestions come from the code's
|
|
359
|
+
* template, with `{placeholders}` filled from `context`.
|
|
360
|
+
* @param code - One of {@link ERROR_CODES}
|
|
361
|
+
* @param context - Values for the message template's placeholders
|
|
362
|
+
* @param originalError - The underlying error, kept as `originalError`
|
|
363
|
+
*/
|
|
364
|
+
export function createError(code: string, context?: Record<string, any>, originalError?: Error | null): EnhancedError;
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Wraps an error in an EnhancedError with the given code, keeping the
|
|
368
|
+
* original as `originalError` and its message as `context.originalMessage`.
|
|
369
|
+
* @param error - The error to wrap
|
|
370
|
+
* @param code - One of {@link ERROR_CODES}
|
|
371
|
+
* @param context - Values for the message template's placeholders
|
|
372
|
+
*/
|
|
373
|
+
export function wrapError(error: Error, code: string, context?: Record<string, any>): EnhancedError;
|
|
374
|
+
|
|
375
|
+
/** The error codes {@link createError} accepts; each key equals its value. */
|
|
376
|
+
export const ERROR_CODES: Record<string, string>;
|
|
377
|
+
|
|
378
|
+
/** The DAG class under its older name. */
|
|
379
|
+
export { DAG as DAGEngine } from "./types/dag/dag-engine";
|
|
380
|
+
|
|
381
|
+
/** The same function as loadRagConfig. */
|
|
382
|
+
export { loadRagConfig as loadConfig } from "./types/config/load-config";
|
|
383
|
+
|
|
384
|
+
// =============================================================================
|
|
385
|
+
// Declarations generated from JSDoc (scripts/generate-types.js)
|
|
386
|
+
// =============================================================================
|
|
387
|
+
export { packContext } from "./types/core/context-packing";
|
|
388
|
+
export { loadRagConfig } from "./types/config/load-config";
|
|
389
|
+
export { validateRagrc } from "./types/config/enhanced-ragrc-schema";
|
|
390
|
+
export { normalizeConfig } from "./types/config/normalize-config";
|
|
391
|
+
import pluginRegistry = require("./types/core/plugin-registry");
|
|
392
|
+
export { pluginRegistry };
|
|
393
|
+
export { validatePlugin } from "./types/core/validate-plugin";
|
|
394
|
+
import logger = require("./types/utils/logger");
|
|
395
|
+
export { logger };
|
|
396
|
+
import errorFormatter = require("./types/utils/error-formatter");
|
|
397
|
+
export { errorFormatter };
|
|
398
|
+
export { BatchProcessor } from "./types/utils/batch-processor";
|
|
399
|
+
import RateLimiter = require("./types/utils/rate-limiter");
|
|
400
|
+
export { RateLimiter };
|
|
401
|
+
export { retry } from "./types/utils/retry";
|
|
402
|
+
export { withRetry } from "./types/utils/retry";
|
|
403
|
+
export { ConnectionPoolManager } from "./types/utils/connection-pool";
|
|
404
|
+
export { JWTValidator } from "./types/security/jwt-validator";
|
|
405
|
+
export { PluginSignatureVerifier } from "./types/security/plugin-signature-verifier";
|
|
406
|
+
export { validateInput } from "./types/security/validator";
|
|
407
|
+
export { DAG } from "./types/dag/dag-engine";
|
|
408
|
+
export { evaluateRagDataset } from "./types/evaluate/evaluator";
|
|
409
|
+
export { scoreAnswer } from "./types/evaluate/scoring";
|
|
410
|
+
export { computeBLEU } from "./types/evaluate/scoring";
|
|
411
|
+
export { computeROUGE } from "./types/evaluate/scoring";
|
|
412
|
+
export { checkNumericConsistency } from "./types/evaluate/scoring";
|
|
413
|
+
export { extractNumbers } from "./types/evaluate/scoring";
|
|
414
|
+
export { computeFaithfulness } from "./types/evaluate/faithfulness";
|
|
415
|
+
export { computeSemanticSimilarity } from "./types/evaluate/semantic-similarity";
|
|
416
|
+
export { validateDataset } from "./types/evaluate/dataset";
|
|
417
|
+
export { generateSyntheticDataset } from "./types/evaluate/synthetic";
|
|
418
|
+
export { runEvaluation } from "./types/evaluate/compare";
|
|
419
|
+
export { compareConfigurations } from "./types/evaluate/compare";
|
|
420
|
+
export { evaluationVerdict } from "./types/evaluate/report";
|
|
421
|
+
export { renderEvaluationReport } from "./types/evaluate/report";
|
|
422
|
+
export { computeAnswerRelevance } from "./types/evaluate/relevance";
|
|
423
|
+
export { computeContextPrecision } from "./types/evaluate/context-metrics";
|
|
424
|
+
export { computeContextRecall } from "./types/evaluate/context-metrics";
|
|
425
|
+
export { computeGroundedness } from "./types/evaluate/groundedness";
|
|
426
|
+
export { PipelineEvaluator } from "./types/evaluate/pipeline-evaluator";
|
|
427
|
+
export { ChunkingEngine } from "./types/chunking/chunking-engine";
|
|
428
|
+
export { CitationTracker } from "./types/citation/citation-tracker";
|
|
429
|
+
export { checkAnswer } from "./types/grounding/check-answer";
|
|
430
|
+
export { normalizeSources } from "./types/grounding/check-answer";
|
|
431
|
+
export { createAiSdkAdapter } from "./types/adapters/ai-sdk";
|
|
432
|
+
export { MemoryCache } from "./types/cache/memory-cache";
|
|
433
|
+
export { RedisCache } from "./types/cache/redis-cache";
|
|
434
|
+
export { mapSentenceToSources } from "./types/citation/source-mapper";
|
|
435
|
+
export { buildIDFWeights } from "./types/citation/source-mapper";
|
|
436
|
+
export { detectHallucinations } from "./types/citation/hallucination-detector";
|
|
437
|
+
export { BM25Search } from "./types/retrieval/bm25-search";
|
|
438
|
+
export { reciprocalRankFusion } from "./types/retrieval/rank-fusion";
|
|
439
|
+
export { HybridRetriever } from "./types/retrieval/hybrid-retriever";
|
|
440
|
+
export { FileVectorStore } from "./types/retrieval/file-vector-store";
|
|
441
|
+
export { PreRetrievalGuard } from "./types/guardrails/pre-retrieval-guard";
|
|
442
|
+
export { detectInjection } from "./types/guardrails/injection-detector";
|
|
443
|
+
export { RetrievalGuard } from "./types/guardrails/retrieval-guard";
|
|
444
|
+
export { PostGenerationGuard } from "./types/guardrails/post-generation-guard";
|
|
445
|
+
export { GuardrailsPipeline } from "./types/guardrails/guardrails-pipeline";
|
|
446
|
+
export { QueryPlanner } from "./types/agentic/query-planner";
|
|
447
|
+
export { IterativeRetriever } from "./types/agentic/iterative-retriever";
|
|
448
|
+
export { SelfCritiqueChecker } from "./types/agentic/self-critique";
|
|
449
|
+
export { AgenticPipeline } from "./types/agentic/agentic-pipeline";
|
|
450
|
+
export { CostCalculator } from "./types/cost/cost-calculator";
|
|
451
|
+
export { PRICING_METADATA } from "./types/cost/cost-calculator";
|
|
452
|
+
export { CostTracker } from "./types/cost/cost-tracker";
|
|
453
|
+
export { TokenBudget } from "./types/cost/token-budget";
|
|
454
|
+
export { createModelRouter } from "./types/cost/model-router";
|
|
455
|
+
export { classifyQuery } from "./types/cost/model-router";
|
|
456
|
+
export { ExecutionTracer } from "./types/debug/execution-tracer";
|
|
457
|
+
export { TraceInspector } from "./types/debug/trace-inspector";
|
|
458
|
+
export { MCPServer } from "./types/mcp/mcp-server";
|
|
459
|
+
export { MCPToolBuilder } from "./types/mcp/mcp-tool-builder";
|
|
460
|
+
export { ProjectScaffolder } from "./types/templates/scaffold";
|
|
461
|
+
export { TemplateRegistry } from "./types/templates/template-registry";
|
|
462
|
+
export { SSEAdapter } from "./types/streaming/sse-adapter";
|
|
463
|
+
export { WebSocketAdapter } from "./types/streaming/websocket-adapter";
|
|
464
|
+
export { StreamRouter } from "./types/streaming/stream-router";
|
|
465
|
+
export { BaseConnector } from "./types/connectors/base-connector";
|
|
466
|
+
export { OllamaConnector } from "./types/connectors/ollama-connector";
|
|
467
|
+
export { LocalEmbedder } from "./types/connectors/local-embedder";
|
|
468
|
+
export { MemoryRetriever } from "./types/connectors/memory-retriever";
|
|
469
|
+
export { PgVectorRetriever } from "./types/retrieval/pgvector-retriever";
|
|
470
|
+
export { QdrantRetriever } from "./types/retrieval/qdrant-retriever";
|
|
471
|
+
export { OpenAIConnector } from "./types/connectors/openai-connector";
|
|
472
|
+
export { AnthropicConnector } from "./types/connectors/anthropic-connector";
|
|
473
|
+
export { CohereConnector } from "./types/connectors/cohere-connector";
|
|
474
|
+
export { StreamingEmbedder } from "./types/streaming/streaming-embedder";
|
|
475
|
+
export { createEmbeddingStream } from "./types/streaming/embedding-stream";
|
|
476
|
+
export { ScoringReranker } from "./types/reranker/scoring-reranker";
|
|
477
|
+
export { EmbeddingReranker } from "./types/reranker/embedding-reranker";
|
|
478
|
+
export { CascadeReranker } from "./types/reranker/cascade-reranker";
|
|
479
|
+
export { KnowledgeGraph } from "./types/graph/knowledge-graph";
|
|
480
|
+
export { EntityExtractor } from "./types/graph/entity-extractor";
|
|
481
|
+
export { GraphRetriever } from "./types/graph/graph-retriever";
|
|
482
|
+
export { GraphIndex } from "./types/graph/graph-index";
|
|
483
|
+
export { DashboardGenerator } from "./types/dashboard/dashboard-generator";
|
|
484
|
+
export { MetricsAggregator } from "./types/dashboard/metrics-aggregator";
|
|
485
|
+
import ParallelProcessor = require("./types/core/performance/parallel-processor");
|
|
486
|
+
export { ParallelProcessor };
|
|
487
|
+
import eventLogger = require("./types/core/observability/event-logger");
|
|
488
|
+
export { eventLogger };
|
|
489
|
+
import metrics = require("./types/core/observability/metrics");
|
|
490
|
+
export { metrics };
|
|
491
|
+
export { AuditLogger } from "./types/enterprise/audit-logging";
|
|
492
|
+
export { PluginHub } from "./types/ecosystem/plugin-hub";
|
|
493
|
+
export { PluginAnalytics } from "./types/ecosystem/plugin-hub";
|
|
494
|
+
export { PluginAnalyticsDashboard } from "./types/ecosystem/plugin-analytics-dashboard";
|
|
495
|
+
export { HotReloadManager } from "./types/dev/hot-reload";
|
|
496
|
+
export { createHotReloadManager } from "./types/dev/hot-reload";
|
|
497
|
+
export { DevServer } from "./types/dev/dev-server";
|
|
498
|
+
export { createDevServer } from "./types/dev/dev-server";
|