@ariaflowagents/rag 0.7.0 → 0.9.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/dist/embedders/AiSdkEmbedder.d.ts +24 -0
- package/dist/embedders/AiSdkEmbedder.d.ts.map +1 -0
- package/dist/embedders/AiSdkEmbedder.js +37 -0
- package/dist/embedders/AiSdkEmbedder.js.map +1 -0
- package/dist/embedders/index.d.ts +3 -0
- package/dist/embedders/index.d.ts.map +1 -0
- package/dist/embedders/index.js +2 -0
- package/dist/embedders/index.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -1
- package/dist/pipeline/RagPipeline.d.ts +52 -0
- package/dist/pipeline/RagPipeline.d.ts.map +1 -0
- package/dist/pipeline/RagPipeline.js +105 -0
- package/dist/pipeline/RagPipeline.js.map +1 -0
- package/dist/pipeline/index.d.ts +3 -0
- package/dist/pipeline/index.d.ts.map +1 -0
- package/dist/pipeline/index.js +2 -0
- package/dist/pipeline/index.js.map +1 -0
- package/dist/rerankers/LLMReranker.d.ts +28 -0
- package/dist/rerankers/LLMReranker.d.ts.map +1 -0
- package/dist/rerankers/LLMReranker.js +70 -0
- package/dist/rerankers/LLMReranker.js.map +1 -0
- package/dist/rerankers/index.d.ts +3 -0
- package/dist/rerankers/index.d.ts.map +1 -0
- package/dist/rerankers/index.js +2 -0
- package/dist/rerankers/index.js.map +1 -0
- package/dist/retrievers/HybridRetriever.d.ts +39 -0
- package/dist/retrievers/HybridRetriever.d.ts.map +1 -0
- package/dist/retrievers/HybridRetriever.js +56 -0
- package/dist/retrievers/HybridRetriever.js.map +1 -0
- package/dist/retrievers/VectorRetriever.d.ts +26 -0
- package/dist/retrievers/VectorRetriever.d.ts.map +1 -0
- package/dist/retrievers/VectorRetriever.js +36 -0
- package/dist/retrievers/VectorRetriever.js.map +1 -0
- package/dist/retrievers/index.d.ts +5 -0
- package/dist/retrievers/index.d.ts.map +1 -0
- package/dist/retrievers/index.js +3 -0
- package/dist/retrievers/index.js.map +1 -0
- package/dist/tools/createVectorRetrievalTool.d.ts +69 -0
- package/dist/tools/createVectorRetrievalTool.d.ts.map +1 -0
- package/dist/tools/createVectorRetrievalTool.js +81 -0
- package/dist/tools/createVectorRetrievalTool.js.map +1 -0
- package/dist/tools/index.d.ts +3 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +2 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/types.d.ts +260 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/vectorStores/InMemoryVectorStore.d.ts +21 -0
- package/dist/vectorStores/InMemoryVectorStore.d.ts.map +1 -0
- package/dist/vectorStores/InMemoryVectorStore.js +158 -0
- package/dist/vectorStores/InMemoryVectorStore.js.map +1 -0
- package/dist/vectorStores/index.d.ts +2 -0
- package/dist/vectorStores/index.d.ts.map +1 -0
- package/dist/vectorStores/index.js +2 -0
- package/dist/vectorStores/index.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VectorRetriever.js","sourceRoot":"","sources":["../../src/retrievers/VectorRetriever.ts"],"names":[],"mappings":"AAmBA;;;;;GAKG;AACH,MAAM,OAAO,eAAe;IACT,WAAW,CAAc;IACzB,QAAQ,CAAW;IACnB,SAAS,CAAS;IAClB,WAAW,CAAS;IAErC,YAAY,OAA+B;QACzC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;QACvC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACjC,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACnC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC;IACxC,CAAC;IAED,KAAK,CAAC,QAAQ,CACZ,KAAa,EACb,OAA0B;QAE1B,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACrD,MAAM,IAAI,GAAG,OAAO,EAAE,IAAI,IAAI,IAAI,CAAC,WAAW,CAAC;QAE/C,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,EAAE;YAC3D,WAAW;YACX,IAAI;YACJ,MAAM,EAAE,OAAO,EAAE,MAAM;YACvB,gBAAgB,EAAE,IAAI;SACvB,CAAC,CAAC;QAEH,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;YACvB,EAAE,EAAE,CAAC,CAAC,EAAE;YACR,IAAI,EAAE,CAAC,CAAC,QAAQ,IAAI,EAAE;YACtB,KAAK,EAAE,CAAC,CAAC,KAAK;YACd,QAAQ,EAAE,CAAC,CAAC,QAAQ;YACpB,QAAQ,EAAE,IAAI,CAAC,SAAS;SACzB,CAAC,CAAC,CAAC;IACN,CAAC;CACF"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { VectorRetriever } from './VectorRetriever.js';
|
|
2
|
+
export type { VectorRetrieverOptions } from './VectorRetriever.js';
|
|
3
|
+
export { HybridRetriever } from './HybridRetriever.js';
|
|
4
|
+
export type { HybridRetrieverSource, HybridRetrieverOptions } from './HybridRetriever.js';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/retrievers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,YAAY,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAEnE,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,YAAY,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/retrievers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAGvD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Retriever, VectorFilter } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Description of a filterable metadata field, provided to the LLM
|
|
4
|
+
* so it knows which fields exist and what values they accept.
|
|
5
|
+
*/
|
|
6
|
+
export interface FilterableFieldDescriptor {
|
|
7
|
+
/** The metadata field name as stored in the vector index. */
|
|
8
|
+
field: string;
|
|
9
|
+
/** Human-readable description of what this field represents. */
|
|
10
|
+
description: string;
|
|
11
|
+
/** The value type. Helps the LLM construct valid filter values. */
|
|
12
|
+
type: 'string' | 'number' | 'boolean' | 'string[]';
|
|
13
|
+
/** Example values, if useful for guiding the LLM. */
|
|
14
|
+
examples?: (string | number | boolean)[];
|
|
15
|
+
}
|
|
16
|
+
export interface VectorRetrievalToolOptions {
|
|
17
|
+
/** The retriever to use (VectorRetriever, HybridRetriever, RagPipeline, etc.). */
|
|
18
|
+
retriever: Retriever;
|
|
19
|
+
/** Default number of results. Default: 10. */
|
|
20
|
+
topK?: number;
|
|
21
|
+
/** Custom tool description. */
|
|
22
|
+
description?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Enable agentic metadata filtering. When true, the tool schema includes
|
|
25
|
+
* a `filter` parameter that allows the LLM to construct metadata filters
|
|
26
|
+
* dynamically at query time.
|
|
27
|
+
*
|
|
28
|
+
* Default: false.
|
|
29
|
+
*/
|
|
30
|
+
enableAgenticFilters?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Descriptions of filterable metadata fields. Included in the tool
|
|
33
|
+
* description so the LLM knows which fields exist.
|
|
34
|
+
*
|
|
35
|
+
* Only relevant when enableAgenticFilters is true.
|
|
36
|
+
*/
|
|
37
|
+
filterableFields?: FilterableFieldDescriptor[];
|
|
38
|
+
/**
|
|
39
|
+
* A static filter applied to every query, merged with any agentic filter
|
|
40
|
+
* via $and. Use this for tenant isolation or access control.
|
|
41
|
+
*/
|
|
42
|
+
staticFilter?: VectorFilter;
|
|
43
|
+
}
|
|
44
|
+
export interface VectorRetrievalToolOutput {
|
|
45
|
+
results: {
|
|
46
|
+
id: string;
|
|
47
|
+
text: string;
|
|
48
|
+
score?: number;
|
|
49
|
+
sourceId?: string;
|
|
50
|
+
reason?: string;
|
|
51
|
+
}[];
|
|
52
|
+
}
|
|
53
|
+
export type VectorRetrievalToolInput = {
|
|
54
|
+
query: string;
|
|
55
|
+
topK?: number;
|
|
56
|
+
filter?: Record<string, unknown>;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Creates a retrieval tool that can be added to any AriaFlow agent.
|
|
60
|
+
*
|
|
61
|
+
* The tool wraps any Retriever implementation, allowing the LLM to
|
|
62
|
+
* decide when to search for relevant knowledge. Uses the Vercel AI SDK
|
|
63
|
+
* `tool()` directly so this package has no dependency on @ariaflowagents/core.
|
|
64
|
+
*/
|
|
65
|
+
export declare function createVectorRetrievalTool(options: VectorRetrievalToolOptions): import("ai").Tool<{
|
|
66
|
+
query: string;
|
|
67
|
+
topK?: number | undefined;
|
|
68
|
+
}, VectorRetrievalToolOutput>;
|
|
69
|
+
//# sourceMappingURL=createVectorRetrievalTool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createVectorRetrievalTool.d.ts","sourceRoot":"","sources":["../../src/tools/createVectorRetrievalTool.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3D;;;GAGG;AACH,MAAM,WAAW,yBAAyB;IACxC,6DAA6D;IAC7D,KAAK,EAAE,MAAM,CAAC;IACd,gEAAgE;IAChE,WAAW,EAAE,MAAM,CAAC;IACpB,mEAAmE;IACnE,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,UAAU,CAAC;IACnD,qDAAqD;IACrD,QAAQ,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC;CAC1C;AAED,MAAM,WAAW,0BAA0B;IACzC,kFAAkF;IAClF,SAAS,EAAE,SAAS,CAAC;IACrB,8CAA8C;IAC9C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,+BAA+B;IAC/B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;;OAMG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,yBAAyB,EAAE,CAAC;IAC/C;;;OAGG;IACH,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B;AAED,MAAM,WAAW,yBAAyB;IACxC,OAAO,EAAE;QACP,EAAE,EAAE,MAAM,CAAC;QACX,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,EAAE,CAAC;CACL;AAED,MAAM,MAAM,wBAAwB,GAAG;IACrC,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,0BAA0B;;;8BAwF5E"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { tool } from 'ai';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a retrieval tool that can be added to any AriaFlow agent.
|
|
5
|
+
*
|
|
6
|
+
* The tool wraps any Retriever implementation, allowing the LLM to
|
|
7
|
+
* decide when to search for relevant knowledge. Uses the Vercel AI SDK
|
|
8
|
+
* `tool()` directly so this package has no dependency on @ariaflowagents/core.
|
|
9
|
+
*/
|
|
10
|
+
export function createVectorRetrievalTool(options) {
|
|
11
|
+
const { retriever, topK: defaultTopK = 10, enableAgenticFilters = false, filterableFields, staticFilter, } = options;
|
|
12
|
+
// Build tool description, including filterable field metadata when enabled
|
|
13
|
+
let toolDescription = options.description ??
|
|
14
|
+
'Search the knowledge base for relevant information. Use this when ' +
|
|
15
|
+
'you need to find specific facts, policies, or context to answer ' +
|
|
16
|
+
'the user\'s question accurately.';
|
|
17
|
+
if (enableAgenticFilters && filterableFields?.length) {
|
|
18
|
+
const fieldDescriptions = filterableFields
|
|
19
|
+
.map(f => {
|
|
20
|
+
let desc = ` - "${f.field}" (${f.type}): ${f.description}`;
|
|
21
|
+
if (f.examples?.length) {
|
|
22
|
+
desc += ` [examples: ${f.examples.map(e => JSON.stringify(e)).join(', ')}]`;
|
|
23
|
+
}
|
|
24
|
+
return desc;
|
|
25
|
+
})
|
|
26
|
+
.join('\n');
|
|
27
|
+
toolDescription +=
|
|
28
|
+
'\n\nYou can optionally filter results by metadata. Available filter fields:\n' +
|
|
29
|
+
fieldDescriptions +
|
|
30
|
+
'\n\nFilter syntax: { "fieldName": "value" } for equality, ' +
|
|
31
|
+
'{ "fieldName": { "$in": ["a", "b"] } } for set membership, ' +
|
|
32
|
+
'{ "fieldName": { "$gt": 5 } } for comparison.';
|
|
33
|
+
}
|
|
34
|
+
// Build zod schema -- conditionally include filter param
|
|
35
|
+
const baseParams = {
|
|
36
|
+
query: z.string().describe('The search query to find relevant information.'),
|
|
37
|
+
topK: z
|
|
38
|
+
.number()
|
|
39
|
+
.optional()
|
|
40
|
+
.describe('Number of results to return. Default: ' + defaultTopK),
|
|
41
|
+
};
|
|
42
|
+
const parameters = enableAgenticFilters
|
|
43
|
+
? z.object({
|
|
44
|
+
...baseParams,
|
|
45
|
+
filter: z
|
|
46
|
+
.record(z.unknown())
|
|
47
|
+
.optional()
|
|
48
|
+
.describe('Optional metadata filter to narrow results. ' +
|
|
49
|
+
'Use field names from the available filter fields.'),
|
|
50
|
+
})
|
|
51
|
+
: z.object(baseParams);
|
|
52
|
+
return tool({
|
|
53
|
+
description: toolDescription,
|
|
54
|
+
inputSchema: parameters,
|
|
55
|
+
execute: async (input) => {
|
|
56
|
+
// Merge static filter (invariant) with agentic filter (LLM-constructed)
|
|
57
|
+
const agenticFilter = input.filter;
|
|
58
|
+
let mergedFilter;
|
|
59
|
+
if (staticFilter && agenticFilter) {
|
|
60
|
+
mergedFilter = { $and: [staticFilter, agenticFilter] };
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
mergedFilter = staticFilter ?? agenticFilter;
|
|
64
|
+
}
|
|
65
|
+
const results = await retriever.retrieve(input.query, {
|
|
66
|
+
topK: input.topK ?? defaultTopK,
|
|
67
|
+
filter: mergedFilter,
|
|
68
|
+
});
|
|
69
|
+
return {
|
|
70
|
+
results: results.map(r => ({
|
|
71
|
+
id: r.id,
|
|
72
|
+
text: r.text,
|
|
73
|
+
score: r.score,
|
|
74
|
+
sourceId: r.sourceId,
|
|
75
|
+
reason: r.reason,
|
|
76
|
+
})),
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=createVectorRetrievalTool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createVectorRetrievalTool.js","sourceRoot":"","sources":["../../src/tools/createVectorRetrievalTool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAC1B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AA+DxB;;;;;;GAMG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAmC;IAC3E,MAAM,EACJ,SAAS,EACT,IAAI,EAAE,WAAW,GAAG,EAAE,EACtB,oBAAoB,GAAG,KAAK,EAC5B,gBAAgB,EAChB,YAAY,GACb,GAAG,OAAO,CAAC;IAEZ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,CAAC;QACJ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAEzB,OAAO,IAAI,CAAC;QACV,WAAW,EAAE,eAAe;QAC5B,WAAW,EAAE,UAAU;QACvB,OAAO,EAAE,KAAK,EACZ,KAAyE,EACrC,EAAE;YACtC,wEAAwE;YACxE,MAAM,aAAa,GAAG,KAAK,CAAC,MAAkC,CAAC;YAC/D,IAAI,YAAsC,CAAC;YAE3C,IAAI,YAAY,IAAI,aAAa,EAAE,CAAC;gBAClC,YAAY,GAAG,EAAE,IAAI,EAAE,CAAC,YAAY,EAAE,aAAa,CAAC,EAAE,CAAC;YACzD,CAAC;iBAAM,CAAC;gBACN,YAAY,GAAG,YAAY,IAAI,aAAa,CAAC;YAC/C,CAAC;YAED,MAAM,OAAO,GAAG,MAAM,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,EAAE;gBACpD,IAAI,EAAE,KAAK,CAAC,IAAI,IAAI,WAAW;gBAC/B,MAAM,EAAE,YAAY;aACrB,CAAC,CAAC;YAEH,OAAO;gBACL,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;oBACzB,EAAE,EAAE,CAAC,CAAC,EAAE;oBACR,IAAI,EAAE,CAAC,CAAC,IAAI;oBACZ,KAAK,EAAE,CAAC,CAAC,KAAK;oBACd,QAAQ,EAAE,CAAC,CAAC,QAAQ;oBACpB,MAAM,EAAE,CAAC,CAAC,MAAM;iBACjB,CAAC,CAAC;aACJ,CAAC;QACJ,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { createVectorRetrievalTool } from './createVectorRetrievalTool.js';
|
|
2
|
+
export type { VectorRetrievalToolOptions, VectorRetrievalToolOutput, VectorRetrievalToolInput, FilterableFieldDescriptor, } from './createVectorRetrievalTool.js';
|
|
3
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAC3E,YAAY,EACV,0BAA0B,EAC1B,yBAAyB,EACzB,wBAAwB,EACxB,yBAAyB,GAC1B,MAAM,gCAAgC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tools/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -11,6 +11,11 @@ export interface KnowledgeSource {
|
|
|
11
11
|
getChunks(): KnowledgeChunk[];
|
|
12
12
|
dumpContent?(): string;
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Chunker interface for the CAG pattern. Returns KnowledgeChunk[].
|
|
16
|
+
* Used by createStaticKnowledgeSource, createMarkdownChunker,
|
|
17
|
+
* createRecursiveChunker.
|
|
18
|
+
*/
|
|
14
19
|
export interface Chunker {
|
|
15
20
|
chunk(text: string, options?: ChunkOptions): KnowledgeChunk[];
|
|
16
21
|
}
|
|
@@ -37,4 +42,259 @@ export type LLMRetrieverOptions = {
|
|
|
37
42
|
includeReasons?: boolean;
|
|
38
43
|
candidateMaxChars?: number;
|
|
39
44
|
};
|
|
45
|
+
/**
|
|
46
|
+
* Extended chunk type with positional and token metadata.
|
|
47
|
+
* KnowledgeChunk is a subset of this (id + text + meta).
|
|
48
|
+
*/
|
|
49
|
+
export interface Chunk {
|
|
50
|
+
/** Unique identifier for this chunk within its source. */
|
|
51
|
+
id: string;
|
|
52
|
+
/** The chunk text content. */
|
|
53
|
+
text: string;
|
|
54
|
+
/** Arbitrary metadata (heading, section name, source path, etc.). */
|
|
55
|
+
metadata?: Record<string, unknown>;
|
|
56
|
+
/** Character offset of the chunk start in the original text. */
|
|
57
|
+
startIndex?: number;
|
|
58
|
+
/** Character offset of the chunk end in the original text. */
|
|
59
|
+
endIndex?: number;
|
|
60
|
+
/** Estimated token count for this chunk. */
|
|
61
|
+
tokens?: number;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A document loaded from an external source.
|
|
65
|
+
* Documents are the input to the chunking stage of the RAG pipeline.
|
|
66
|
+
*/
|
|
67
|
+
export interface Document {
|
|
68
|
+
/** Unique identifier for this document. */
|
|
69
|
+
id: string;
|
|
70
|
+
/** The document's text content. */
|
|
71
|
+
text: string;
|
|
72
|
+
/** Source metadata (file path, URL, content type, title, etc.). */
|
|
73
|
+
metadata?: Record<string, unknown>;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Contract for loading documents from external sources.
|
|
77
|
+
*/
|
|
78
|
+
export interface DocumentLoader {
|
|
79
|
+
/**
|
|
80
|
+
* Load documents from the configured source.
|
|
81
|
+
*
|
|
82
|
+
* @returns Array of documents. A single source may produce multiple
|
|
83
|
+
* documents (e.g., a directory loader produces one per file).
|
|
84
|
+
*/
|
|
85
|
+
load(): Promise<Document[]>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Contract for embedding text into dense vector representations.
|
|
89
|
+
*
|
|
90
|
+
* Implementations may wrap any embedding provider: Vercel AI SDK models,
|
|
91
|
+
* OpenAI API directly, local models (Ollama, transformers.js), or
|
|
92
|
+
* third-party services (Cohere, Voyage, Jina).
|
|
93
|
+
*/
|
|
94
|
+
export interface Embedder {
|
|
95
|
+
/**
|
|
96
|
+
* Embed a single text string into a dense vector.
|
|
97
|
+
*/
|
|
98
|
+
embed(text: string): Promise<readonly number[]>;
|
|
99
|
+
/**
|
|
100
|
+
* Embed multiple text strings into dense vectors.
|
|
101
|
+
* Implementations SHOULD batch the underlying API calls where the
|
|
102
|
+
* provider supports it, rather than calling embed() in a loop.
|
|
103
|
+
*/
|
|
104
|
+
embedMany(texts: string[]): Promise<readonly (readonly number[])[]>;
|
|
105
|
+
/**
|
|
106
|
+
* The dimensionality of the embedding vectors produced by this embedder.
|
|
107
|
+
* May be undefined if the dimension is not known until the first embed() call.
|
|
108
|
+
*/
|
|
109
|
+
readonly dimension?: number;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* A single entry to upsert into a vector index.
|
|
113
|
+
*/
|
|
114
|
+
export interface VectorEntry {
|
|
115
|
+
/** Unique identifier for this vector. Overwrites if exists. */
|
|
116
|
+
id: string;
|
|
117
|
+
/** Dense vector representation. */
|
|
118
|
+
vector: readonly number[];
|
|
119
|
+
/** Arbitrary key-value metadata for filtering. */
|
|
120
|
+
metadata?: Record<string, unknown>;
|
|
121
|
+
/** Original text content. Stored alongside the vector for retrieval. */
|
|
122
|
+
document?: string;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Result of a vector similarity query.
|
|
126
|
+
*/
|
|
127
|
+
export interface VectorQueryResult {
|
|
128
|
+
/** The vector entry's unique identifier. */
|
|
129
|
+
id: string;
|
|
130
|
+
/** Similarity score. Higher values indicate greater similarity. */
|
|
131
|
+
score: number;
|
|
132
|
+
/** Metadata associated with the vector entry. */
|
|
133
|
+
metadata?: Record<string, unknown>;
|
|
134
|
+
/** Original text content, if stored and requested. */
|
|
135
|
+
document?: string;
|
|
136
|
+
/** The raw vector, if includeVectors was true. */
|
|
137
|
+
vector?: readonly number[];
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Parameters for querying a vector index.
|
|
141
|
+
*/
|
|
142
|
+
export interface VectorQueryParams {
|
|
143
|
+
/** The query vector to find similar entries for. */
|
|
144
|
+
queryVector: readonly number[];
|
|
145
|
+
/** Maximum number of results to return. Default: 10. */
|
|
146
|
+
topK?: number;
|
|
147
|
+
/** Metadata filter in MongoDB-style operator syntax. */
|
|
148
|
+
filter?: VectorFilter;
|
|
149
|
+
/** Whether to include the raw vectors in results. Default: false. */
|
|
150
|
+
includeVectors?: boolean;
|
|
151
|
+
/** Whether to include the stored document text. Default: true. */
|
|
152
|
+
includeDocuments?: boolean;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Parameters for creating a vector index.
|
|
156
|
+
*/
|
|
157
|
+
export interface CreateIndexParams {
|
|
158
|
+
/** Name of the index to create. */
|
|
159
|
+
indexName: string;
|
|
160
|
+
/** Dimensionality of the vectors. Must match the embedder's output. */
|
|
161
|
+
dimension: number;
|
|
162
|
+
/** Distance metric for similarity computation. Default: 'cosine'. */
|
|
163
|
+
metric?: 'cosine' | 'euclidean' | 'dotproduct';
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Statistics about a vector index.
|
|
167
|
+
*/
|
|
168
|
+
export interface IndexStats {
|
|
169
|
+
/** Dimensionality of stored vectors. */
|
|
170
|
+
dimension: number;
|
|
171
|
+
/** Total number of vectors in the index. */
|
|
172
|
+
count: number;
|
|
173
|
+
/** Distance metric configured for this index. */
|
|
174
|
+
metric: 'cosine' | 'euclidean' | 'dotproduct';
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* MongoDB-style filter operators for vector metadata queries.
|
|
178
|
+
*/
|
|
179
|
+
export type VectorFilter = VectorFilterCondition | {
|
|
180
|
+
$and: VectorFilter[];
|
|
181
|
+
} | {
|
|
182
|
+
$or: VectorFilter[];
|
|
183
|
+
} | {
|
|
184
|
+
$not: VectorFilter;
|
|
185
|
+
};
|
|
186
|
+
export type VectorFilterCondition = Record<string, unknown | {
|
|
187
|
+
$eq?: unknown;
|
|
188
|
+
} | {
|
|
189
|
+
$ne?: unknown;
|
|
190
|
+
} | {
|
|
191
|
+
$gt?: number;
|
|
192
|
+
} | {
|
|
193
|
+
$gte?: number;
|
|
194
|
+
} | {
|
|
195
|
+
$lt?: number;
|
|
196
|
+
} | {
|
|
197
|
+
$lte?: number;
|
|
198
|
+
} | {
|
|
199
|
+
$in?: unknown[];
|
|
200
|
+
} | {
|
|
201
|
+
$nin?: unknown[];
|
|
202
|
+
} | {
|
|
203
|
+
$exists?: boolean;
|
|
204
|
+
}>;
|
|
205
|
+
/**
|
|
206
|
+
* Contract for persistent vector storage and similarity search.
|
|
207
|
+
*
|
|
208
|
+
* This is the primary extension point for third-party vector database
|
|
209
|
+
* integrations. Each provider implements this interface in a separate
|
|
210
|
+
* npm package (e.g., @ariaflowagents/vector-pg).
|
|
211
|
+
*/
|
|
212
|
+
export interface VectorStore {
|
|
213
|
+
/**
|
|
214
|
+
* Insert or update vector entries in an index.
|
|
215
|
+
* If an entry with the same ID exists, it is overwritten.
|
|
216
|
+
*/
|
|
217
|
+
upsert(indexName: string, entries: VectorEntry[]): Promise<void>;
|
|
218
|
+
/**
|
|
219
|
+
* Query an index for vectors similar to the given query vector.
|
|
220
|
+
*/
|
|
221
|
+
query(indexName: string, params: VectorQueryParams): Promise<VectorQueryResult[]>;
|
|
222
|
+
/**
|
|
223
|
+
* Create a new vector index with the specified configuration.
|
|
224
|
+
* Implementations SHOULD be idempotent.
|
|
225
|
+
*/
|
|
226
|
+
createIndex(params: CreateIndexParams): Promise<void>;
|
|
227
|
+
/**
|
|
228
|
+
* List all index names in this vector store.
|
|
229
|
+
*/
|
|
230
|
+
listIndexes(): Promise<string[]>;
|
|
231
|
+
/**
|
|
232
|
+
* Delete an index and all its vectors.
|
|
233
|
+
*/
|
|
234
|
+
deleteIndex(indexName: string): Promise<void>;
|
|
235
|
+
/**
|
|
236
|
+
* Delete specific vectors from an index.
|
|
237
|
+
*/
|
|
238
|
+
deleteVectors?(indexName: string, params: {
|
|
239
|
+
ids?: string[];
|
|
240
|
+
filter?: VectorFilter;
|
|
241
|
+
}): Promise<void>;
|
|
242
|
+
/**
|
|
243
|
+
* Get statistics about an index.
|
|
244
|
+
*/
|
|
245
|
+
describeIndex?(indexName: string): Promise<IndexStats>;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* A single result from a retrieval operation.
|
|
249
|
+
*/
|
|
250
|
+
export interface RetrievalResult {
|
|
251
|
+
/** Identifier of the retrieved chunk or document. */
|
|
252
|
+
id: string;
|
|
253
|
+
/** The retrieved text content. */
|
|
254
|
+
text: string;
|
|
255
|
+
/** Relevance score. Higher is more relevant. */
|
|
256
|
+
score?: number;
|
|
257
|
+
/** Metadata associated with the retrieved item. */
|
|
258
|
+
metadata?: Record<string, unknown>;
|
|
259
|
+
/** Identifier of the source index or knowledge source. */
|
|
260
|
+
sourceId?: string;
|
|
261
|
+
/** Human-readable reason for selection. */
|
|
262
|
+
reason?: string;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Options for retrieval operations.
|
|
266
|
+
*/
|
|
267
|
+
export interface RetrievalOptions {
|
|
268
|
+
/** Maximum number of results to return. */
|
|
269
|
+
topK?: number;
|
|
270
|
+
/** Metadata filter (for vector-backed retrievers). */
|
|
271
|
+
filter?: VectorFilter;
|
|
272
|
+
/** Optional hint or context to guide the retriever. */
|
|
273
|
+
hint?: string;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Contract for retrieving relevant content given a query.
|
|
277
|
+
*/
|
|
278
|
+
export interface Retriever {
|
|
279
|
+
/**
|
|
280
|
+
* Retrieve relevant content for a query.
|
|
281
|
+
*/
|
|
282
|
+
retrieve(query: string, options?: RetrievalOptions): Promise<RetrievalResult[]>;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Options for reranking operations.
|
|
286
|
+
*/
|
|
287
|
+
export interface RerankerOptions {
|
|
288
|
+
/** Maximum number of results to return after reranking. */
|
|
289
|
+
topK?: number;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Contract for post-retrieval result reranking.
|
|
293
|
+
*/
|
|
294
|
+
export interface Reranker {
|
|
295
|
+
/**
|
|
296
|
+
* Rerank retrieval results by relevance to the query.
|
|
297
|
+
*/
|
|
298
|
+
rerank(query: string, results: RetrievalResult[], options?: RerankerOptions): Promise<RetrievalResult[]>;
|
|
299
|
+
}
|
|
40
300
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AAMxC,MAAM,MAAM,cAAc,GAAG;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC,CAAC;AAEF,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,IAAI,cAAc,EAAE,CAAC;IAC9B,WAAW,CAAC,IAAI,MAAM,CAAC;CACxB;AAED;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACtB,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,cAAc,EAAE,CAAC;CAC/D;AAED,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CACN,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,eAAe,EAAE,EAC1B,OAAO,CAAC,EAAE;QAAE,IAAI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AW,eAAe;IAC9B,2DAA2D;IAC3D,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB;;OAEG;IACH,MAAM,CACJ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,eAAe,EAAE,EAC1B,OAAO,CAAC,EAAE,eAAe,GACxB,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC;CAC/B"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { VectorStore, VectorEntry, VectorQueryParams, VectorQueryResult, CreateIndexParams, IndexStats, VectorFilter } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* In-memory vector store for development and testing.
|
|
4
|
+
* Uses brute-force similarity search (O(n) per query).
|
|
5
|
+
* Not suitable for production workloads.
|
|
6
|
+
*/
|
|
7
|
+
export declare class InMemoryVectorStore implements VectorStore {
|
|
8
|
+
private indexes;
|
|
9
|
+
createIndex(params: CreateIndexParams): Promise<void>;
|
|
10
|
+
upsert(indexName: string, entries: VectorEntry[]): Promise<void>;
|
|
11
|
+
query(indexName: string, params: VectorQueryParams): Promise<VectorQueryResult[]>;
|
|
12
|
+
listIndexes(): Promise<string[]>;
|
|
13
|
+
deleteIndex(indexName: string): Promise<void>;
|
|
14
|
+
deleteVectors(indexName: string, params: {
|
|
15
|
+
ids?: string[];
|
|
16
|
+
filter?: VectorFilter;
|
|
17
|
+
}): Promise<void>;
|
|
18
|
+
describeIndex(indexName: string): Promise<IndexStats>;
|
|
19
|
+
private getIndex;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=InMemoryVectorStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InMemoryVectorStore.d.ts","sourceRoot":"","sources":["../../src/vectorStores/InMemoryVectorStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,WAAW,EACX,iBAAiB,EACjB,iBAAiB,EACjB,iBAAiB,EACjB,UAAU,EACV,YAAY,EACb,MAAM,aAAa,CAAC;AAQrB;;;;GAIG;AACH,qBAAa,mBAAoB,YAAW,WAAW;IACrD,OAAO,CAAC,OAAO,CAAgC;IAEzC,WAAW,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IASrD,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAahE,KAAK,CACT,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,iBAAiB,GACxB,OAAO,CAAC,iBAAiB,EAAE,CAAC;IA2BzB,WAAW,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAIhC,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI7C,aAAa,CACjB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE;QAAE,GAAG,CAAC,EAAE,MAAM,EAAE,CAAC;QAAC,MAAM,CAAC,EAAE,YAAY,CAAA;KAAE,GAChD,OAAO,CAAC,IAAI,CAAC;IAgBV,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAS3D,OAAO,CAAC,QAAQ;CAKjB"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory vector store for development and testing.
|
|
3
|
+
* Uses brute-force similarity search (O(n) per query).
|
|
4
|
+
* Not suitable for production workloads.
|
|
5
|
+
*/
|
|
6
|
+
export class InMemoryVectorStore {
|
|
7
|
+
indexes = new Map();
|
|
8
|
+
async createIndex(params) {
|
|
9
|
+
if (this.indexes.has(params.indexName))
|
|
10
|
+
return;
|
|
11
|
+
this.indexes.set(params.indexName, {
|
|
12
|
+
dimension: params.dimension,
|
|
13
|
+
metric: params.metric ?? 'cosine',
|
|
14
|
+
entries: new Map(),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
async upsert(indexName, entries) {
|
|
18
|
+
const index = this.getIndex(indexName);
|
|
19
|
+
for (const entry of entries) {
|
|
20
|
+
if (entry.vector.length !== index.dimension) {
|
|
21
|
+
throw new Error(`Vector dimension mismatch: expected ${index.dimension}, ` +
|
|
22
|
+
`got ${entry.vector.length} for entry "${entry.id}"`);
|
|
23
|
+
}
|
|
24
|
+
index.entries.set(entry.id, { ...entry });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async query(indexName, params) {
|
|
28
|
+
const index = this.getIndex(indexName);
|
|
29
|
+
const topK = params.topK ?? 10;
|
|
30
|
+
const scored = [];
|
|
31
|
+
for (const [id, entry] of index.entries) {
|
|
32
|
+
if (params.filter && !matchesFilter(entry.metadata ?? {}, params.filter)) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const score = computeSimilarity(params.queryVector, entry.vector, index.metric);
|
|
36
|
+
scored.push({
|
|
37
|
+
id,
|
|
38
|
+
score,
|
|
39
|
+
metadata: entry.metadata,
|
|
40
|
+
document: params.includeDocuments !== false ? entry.document : undefined,
|
|
41
|
+
vector: params.includeVectors ? [...entry.vector] : undefined,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
scored.sort((a, b) => b.score - a.score);
|
|
45
|
+
return scored.slice(0, topK);
|
|
46
|
+
}
|
|
47
|
+
async listIndexes() {
|
|
48
|
+
return Array.from(this.indexes.keys());
|
|
49
|
+
}
|
|
50
|
+
async deleteIndex(indexName) {
|
|
51
|
+
this.indexes.delete(indexName);
|
|
52
|
+
}
|
|
53
|
+
async deleteVectors(indexName, params) {
|
|
54
|
+
const index = this.getIndex(indexName);
|
|
55
|
+
if (params.ids) {
|
|
56
|
+
for (const id of params.ids) {
|
|
57
|
+
index.entries.delete(id);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (params.filter) {
|
|
61
|
+
for (const [id, entry] of index.entries) {
|
|
62
|
+
if (matchesFilter(entry.metadata ?? {}, params.filter)) {
|
|
63
|
+
index.entries.delete(id);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async describeIndex(indexName) {
|
|
69
|
+
const index = this.getIndex(indexName);
|
|
70
|
+
return {
|
|
71
|
+
dimension: index.dimension,
|
|
72
|
+
count: index.entries.size,
|
|
73
|
+
metric: index.metric,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
getIndex(name) {
|
|
77
|
+
const index = this.indexes.get(name);
|
|
78
|
+
if (!index)
|
|
79
|
+
throw new Error(`Index "${name}" does not exist.`);
|
|
80
|
+
return index;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// -- Similarity computation -------------------------------------------------
|
|
84
|
+
function computeSimilarity(a, b, metric) {
|
|
85
|
+
let dotProduct = 0;
|
|
86
|
+
let magnitudeA = 0;
|
|
87
|
+
let magnitudeB = 0;
|
|
88
|
+
for (let i = 0; i < a.length; i++) {
|
|
89
|
+
dotProduct += a[i] * b[i];
|
|
90
|
+
magnitudeA += a[i] * a[i];
|
|
91
|
+
magnitudeB += b[i] * b[i];
|
|
92
|
+
}
|
|
93
|
+
if (metric === 'dotproduct')
|
|
94
|
+
return dotProduct;
|
|
95
|
+
if (metric === 'euclidean') {
|
|
96
|
+
let sumSqDiff = 0;
|
|
97
|
+
for (let i = 0; i < a.length; i++) {
|
|
98
|
+
const diff = a[i] - b[i];
|
|
99
|
+
sumSqDiff += diff * diff;
|
|
100
|
+
}
|
|
101
|
+
return 1 / (1 + Math.sqrt(sumSqDiff));
|
|
102
|
+
}
|
|
103
|
+
// cosine
|
|
104
|
+
const magA = Math.sqrt(magnitudeA);
|
|
105
|
+
const magB = Math.sqrt(magnitudeB);
|
|
106
|
+
if (magA === 0 || magB === 0)
|
|
107
|
+
return 0;
|
|
108
|
+
return dotProduct / (magA * magB);
|
|
109
|
+
}
|
|
110
|
+
// -- Filter matching --------------------------------------------------------
|
|
111
|
+
function matchesFilter(metadata, filter) {
|
|
112
|
+
if ('$and' in filter) {
|
|
113
|
+
return filter.$and.every(f => matchesFilter(metadata, f));
|
|
114
|
+
}
|
|
115
|
+
if ('$or' in filter) {
|
|
116
|
+
return filter.$or.some(f => matchesFilter(metadata, f));
|
|
117
|
+
}
|
|
118
|
+
if ('$not' in filter) {
|
|
119
|
+
return !matchesFilter(metadata, filter.$not);
|
|
120
|
+
}
|
|
121
|
+
for (const [field, condition] of Object.entries(filter)) {
|
|
122
|
+
const value = metadata[field];
|
|
123
|
+
if (condition === null || condition === undefined) {
|
|
124
|
+
if (value !== condition)
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
else if (typeof condition === 'object' && !Array.isArray(condition)) {
|
|
128
|
+
const ops = condition;
|
|
129
|
+
if ('$eq' in ops && value !== ops.$eq)
|
|
130
|
+
return false;
|
|
131
|
+
if ('$ne' in ops && value === ops.$ne)
|
|
132
|
+
return false;
|
|
133
|
+
if ('$gt' in ops && (typeof value !== 'number' || value <= ops.$gt))
|
|
134
|
+
return false;
|
|
135
|
+
if ('$gte' in ops && (typeof value !== 'number' || value < ops.$gte))
|
|
136
|
+
return false;
|
|
137
|
+
if ('$lt' in ops && (typeof value !== 'number' || value >= ops.$lt))
|
|
138
|
+
return false;
|
|
139
|
+
if ('$lte' in ops && (typeof value !== 'number' || value > ops.$lte))
|
|
140
|
+
return false;
|
|
141
|
+
if ('$in' in ops && !ops.$in.includes(value))
|
|
142
|
+
return false;
|
|
143
|
+
if ('$nin' in ops && ops.$nin.includes(value))
|
|
144
|
+
return false;
|
|
145
|
+
if ('$exists' in ops) {
|
|
146
|
+
const exists = value !== undefined;
|
|
147
|
+
if (ops.$exists !== exists)
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
if (value !== condition)
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=InMemoryVectorStore.js.map
|