@opensaas/stack-rag 0.1.7 → 0.4.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +223 -0
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +9 -0
- package/dist/config/index.js.map +1 -1
- package/dist/config/plugin.d.ts.map +1 -1
- package/dist/config/plugin.js +32 -0
- package/dist/config/plugin.js.map +1 -1
- package/dist/config/plugin.test.js +70 -14
- package/dist/config/plugin.test.js.map +1 -1
- package/dist/config/types.d.ts +135 -0
- package/dist/config/types.d.ts.map +1 -1
- package/dist/fields/embedding.d.ts +4 -4
- package/dist/fields/embedding.d.ts.map +1 -1
- package/dist/fields/embedding.js.map +1 -1
- package/dist/fields/searchable.d.ts +1 -1
- package/dist/fields/searchable.d.ts.map +1 -1
- package/dist/fields/searchable.js +1 -0
- package/dist/fields/searchable.js.map +1 -1
- package/dist/fields/searchable.test.js +1 -0
- package/dist/fields/searchable.test.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/providers/ollama.js +4 -4
- package/dist/providers/ollama.js.map +1 -1
- package/dist/providers/openai.js +1 -1
- package/dist/providers/openai.js.map +1 -1
- package/dist/runtime/build-time.d.ts +100 -0
- package/dist/runtime/build-time.d.ts.map +1 -0
- package/dist/runtime/build-time.js +185 -0
- package/dist/runtime/build-time.js.map +1 -0
- package/dist/runtime/index.d.ts +3 -0
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +6 -0
- package/dist/runtime/index.js.map +1 -1
- package/dist/runtime/markdown.d.ts +33 -0
- package/dist/runtime/markdown.d.ts.map +1 -0
- package/dist/runtime/markdown.js +94 -0
- package/dist/runtime/markdown.js.map +1 -0
- package/dist/runtime/provider-helpers.d.ts +56 -0
- package/dist/runtime/provider-helpers.d.ts.map +1 -0
- package/dist/runtime/provider-helpers.js +95 -0
- package/dist/runtime/provider-helpers.js.map +1 -0
- package/dist/runtime/types.d.ts +29 -0
- package/dist/runtime/types.d.ts.map +1 -0
- package/dist/runtime/types.js +6 -0
- package/dist/runtime/types.js.map +1 -0
- package/dist/storage/index.d.ts +1 -0
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +1 -0
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/json-file.d.ts +53 -0
- package/dist/storage/json-file.d.ts.map +1 -0
- package/dist/storage/json-file.js +124 -0
- package/dist/storage/json-file.js.map +1 -0
- package/dist/storage/storage.test.js +1 -0
- package/dist/storage/storage.test.js.map +1 -1
- package/package.json +11 -10
- package/src/config/index.ts +9 -0
- package/src/config/plugin.test.ts +70 -14
- package/src/config/plugin.ts +37 -0
- package/src/config/types.ts +158 -0
- package/src/fields/embedding.ts +6 -4
- package/src/fields/searchable.test.ts +2 -1
- package/src/fields/searchable.ts +2 -1
- package/src/index.ts +6 -0
- package/src/providers/ollama.ts +5 -5
- package/src/providers/openai.ts +1 -1
- package/src/runtime/build-time.ts +216 -0
- package/src/runtime/index.ts +18 -0
- package/src/runtime/markdown.ts +119 -0
- package/src/runtime/provider-helpers.ts +115 -0
- package/src/runtime/types.ts +30 -0
- package/src/storage/index.ts +1 -0
- package/src/storage/json-file.ts +157 -0
- package/src/storage/storage.test.ts +1 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build-time utilities for generating and managing embeddings
|
|
3
|
+
* Used by CLI tools and custom build scripts
|
|
4
|
+
*/
|
|
5
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
6
|
+
import { createHash } from 'node:crypto';
|
|
7
|
+
/**
|
|
8
|
+
* Simple character-based text chunking for build-time generation
|
|
9
|
+
*
|
|
10
|
+
* Simpler than the runtime chunking strategies, optimized for build-time batch processing.
|
|
11
|
+
* Splits text into fixed-size chunks with overlap.
|
|
12
|
+
*
|
|
13
|
+
* @param text - Text to chunk
|
|
14
|
+
* @param chunkSize - Size of each chunk in characters
|
|
15
|
+
* @param overlap - Overlap between chunks in characters
|
|
16
|
+
* @returns Array of text chunks
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { simpleChunkText } from '@opensaas/stack-rag/runtime'
|
|
21
|
+
*
|
|
22
|
+
* const chunks = simpleChunkText("Long document...", 500, 50)
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function simpleChunkText(text, chunkSize, overlap) {
|
|
26
|
+
const chunks = [];
|
|
27
|
+
let start = 0;
|
|
28
|
+
while (start < text.length) {
|
|
29
|
+
const end = Math.min(start + chunkSize, text.length);
|
|
30
|
+
chunks.push(text.slice(start, end));
|
|
31
|
+
start += chunkSize - overlap;
|
|
32
|
+
}
|
|
33
|
+
return chunks;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Compute SHA256 hash of content for change detection
|
|
37
|
+
*
|
|
38
|
+
* @param content - Content to hash
|
|
39
|
+
* @returns Hexadecimal hash string
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* import { hashContent } from '@opensaas/stack-rag/runtime'
|
|
44
|
+
*
|
|
45
|
+
* const hash = hashContent("document content")
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export function hashContent(content) {
|
|
49
|
+
return createHash('sha256').update(content).digest('hex');
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Load existing embeddings index from file
|
|
53
|
+
*
|
|
54
|
+
* Used for differential updates - only regenerate embeddings for changed content.
|
|
55
|
+
*
|
|
56
|
+
* @param filePath - Path to embeddings JSON file
|
|
57
|
+
* @returns Loaded index or null if file doesn't exist or can't be loaded
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* import { loadExistingIndex } from '@opensaas/stack-rag/runtime'
|
|
62
|
+
*
|
|
63
|
+
* const existing = loadExistingIndex('.embeddings/docs.json')
|
|
64
|
+
* if (existing) {
|
|
65
|
+
* console.log(`Found ${Object.keys(existing.documents).length} existing documents`)
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export function loadExistingIndex(filePath) {
|
|
70
|
+
if (!existsSync(filePath)) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const content = readFileSync(filePath, 'utf-8');
|
|
75
|
+
return JSON.parse(content);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
console.warn(`Warning: Could not load existing embeddings from ${filePath}`);
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Generate embeddings for a document with chunking
|
|
84
|
+
*
|
|
85
|
+
* Main utility for build-time embedding generation. Chunks the document,
|
|
86
|
+
* generates embeddings for each chunk, and returns a complete EmbeddedDocument.
|
|
87
|
+
*
|
|
88
|
+
* @param documentId - Unique identifier for the document
|
|
89
|
+
* @param content - Document content (plain text)
|
|
90
|
+
* @param provider - Embedding provider instance
|
|
91
|
+
* @param options - Generation options
|
|
92
|
+
* @returns Complete embedded document ready to be added to index
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* import { generateDocumentEmbeddings } from '@opensaas/stack-rag/runtime'
|
|
97
|
+
* import { createEmbeddingProvider } from '@opensaas/stack-rag/providers'
|
|
98
|
+
*
|
|
99
|
+
* const provider = createEmbeddingProvider({
|
|
100
|
+
* type: 'openai',
|
|
101
|
+
* apiKey: process.env.OPENAI_API_KEY
|
|
102
|
+
* })
|
|
103
|
+
*
|
|
104
|
+
* const doc = await generateDocumentEmbeddings(
|
|
105
|
+
* 'docs/getting-started',
|
|
106
|
+
* 'Document content here...',
|
|
107
|
+
* provider,
|
|
108
|
+
* {
|
|
109
|
+
* title: 'Getting Started',
|
|
110
|
+
* chunkSize: 500,
|
|
111
|
+
* chunkOverlap: 50,
|
|
112
|
+
* metadata: { section: 'guides' }
|
|
113
|
+
* }
|
|
114
|
+
* )
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export async function generateDocumentEmbeddings(documentId, content, provider, options) {
|
|
118
|
+
const { title, chunkSize, chunkOverlap, metadata = {} } = options;
|
|
119
|
+
// Hash content for differential updates
|
|
120
|
+
const contentHash = hashContent(content);
|
|
121
|
+
// Prepare all text chunks to embed
|
|
122
|
+
const allTextChunks = [];
|
|
123
|
+
const chunkTypes = [];
|
|
124
|
+
// Add title chunk first if title exists
|
|
125
|
+
if (title) {
|
|
126
|
+
allTextChunks.push(title);
|
|
127
|
+
chunkTypes.push('title');
|
|
128
|
+
}
|
|
129
|
+
// Chunk the content
|
|
130
|
+
const contentChunks = simpleChunkText(content, chunkSize, chunkOverlap);
|
|
131
|
+
allTextChunks.push(...contentChunks);
|
|
132
|
+
contentChunks.forEach(() => chunkTypes.push('content'));
|
|
133
|
+
// Generate embeddings in batch for all chunks
|
|
134
|
+
const allEmbeddings = await provider.embedBatch(allTextChunks);
|
|
135
|
+
// Build chunks with embeddings
|
|
136
|
+
const chunks = [];
|
|
137
|
+
let embeddingIndex = 0;
|
|
138
|
+
let contentChunkIndex = 0;
|
|
139
|
+
for (let i = 0; i < chunkTypes.length; i++) {
|
|
140
|
+
const type = chunkTypes[i];
|
|
141
|
+
if (type === 'title') {
|
|
142
|
+
// Title chunk
|
|
143
|
+
chunks.push({
|
|
144
|
+
text: allTextChunks[embeddingIndex],
|
|
145
|
+
embedding: allEmbeddings[embeddingIndex],
|
|
146
|
+
metadata: {
|
|
147
|
+
chunkIndex: -1, // Special index for title
|
|
148
|
+
startOffset: 0,
|
|
149
|
+
endOffset: 0,
|
|
150
|
+
isTitle: true,
|
|
151
|
+
...metadata,
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
// Content chunk
|
|
157
|
+
chunks.push({
|
|
158
|
+
text: allTextChunks[embeddingIndex],
|
|
159
|
+
embedding: allEmbeddings[embeddingIndex],
|
|
160
|
+
metadata: {
|
|
161
|
+
chunkIndex: contentChunkIndex,
|
|
162
|
+
startOffset: contentChunkIndex * (chunkSize - chunkOverlap),
|
|
163
|
+
endOffset: Math.min((contentChunkIndex + 1) * chunkSize - contentChunkIndex * chunkOverlap, content.length),
|
|
164
|
+
...metadata,
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
contentChunkIndex++;
|
|
168
|
+
}
|
|
169
|
+
embeddingIndex++;
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
id: documentId,
|
|
173
|
+
title,
|
|
174
|
+
chunks,
|
|
175
|
+
embeddingMetadata: {
|
|
176
|
+
model: provider.model,
|
|
177
|
+
provider: provider.type,
|
|
178
|
+
dimensions: provider.dimensions,
|
|
179
|
+
generatedAt: new Date().toISOString(),
|
|
180
|
+
},
|
|
181
|
+
generatedAt: new Date().toISOString(),
|
|
182
|
+
contentHash,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
//# sourceMappingURL=build-time.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-time.js","sourceRoot":"","sources":["../../src/runtime/build-time.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAIxC;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,eAAe,CAAC,IAAY,EAAE,SAAiB,EAAE,OAAe;IAC9E,MAAM,MAAM,GAAa,EAAE,CAAA;IAC3B,IAAI,KAAK,GAAG,CAAC,CAAA;IAEb,OAAO,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,SAAS,EAAE,IAAI,C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,EAAE,aAAa,CAAC,cAAc,CAAC;gBACxC,QAAQ,EAAE;oBACR,UAAU,EAAE,iBAAiB;oBAC7B,WAAW,EAAE,iBAAiB,GAAG,CAAC,SAAS,GAAG,YAAY,CAAC;oBAC3D,SAAS,EAAE,IAAI,CAAC,GAAG,CACjB,CAAC,iBAAiB,GAAG,CAAC,CAAC,GAAG,SAAS,GAAG,iBAAiB,GAAG,YAAY,EACtE,OAAO,CAAC,MAAM,CACf;oBACD,GAAG,QAAQ;iBACZ;aACF,CAAC,CAAA;YACF,iBAAiB,EAAE,CAAA;QACrB,CAAC;QAED,cAAc,EAAE,CAAA;IAClB,CAAC;IAED,OAAO;QACL,EAAE,EAAE,UAAU;QACd,KAAK;QACL,MAAM;QACN,iBAAiB,EAAE;YACjB,KAAK,EAAE,QAAQ,CAAC,KAAK;YACrB,QAAQ,EAAE,QAAQ,CAAC,IAAI;YACvB,UAAU,EAAE,QAAQ,CAAC,UAAU;YAC/B,WAAW,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;SACtC;QACD,WAAW,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;QACrC,WAAW;KACZ,CAAA;AACH,CAAC"}
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -11,4 +11,7 @@ export { chunkText, estimateTokenCount, mergeSmallChunks, type ChunkingStrategy,
|
|
|
11
11
|
export { generateEmbedding, generateEmbeddings, shouldRegenerateEmbedding, hashText, validateEmbeddingDimensions, mergeEmbeddings, type GenerateEmbeddingOptions, type GenerateEmbeddingsOptions, type ChunkedEmbedding, } from './embeddings.js';
|
|
12
12
|
export { semanticSearch, findSimilar, type SemanticSearchOptions, type FindSimilarOptions, } from './search.js';
|
|
13
13
|
export { batchProcess, RateLimiter, ProcessingQueue, type BatchProcessOptions, type BatchProgress, type BatchError, type BatchProcessResult, } from './batch.js';
|
|
14
|
+
export { simpleChunkText, hashContent, loadExistingIndex, generateDocumentEmbeddings, } from './build-time.js';
|
|
15
|
+
export { stripMarkdown, extractMarkdownText } from './markdown.js';
|
|
16
|
+
export { createProviderFromEnv, getProviderConfigFromEnv, type ProviderType, } from './provider-helpers.js';
|
|
14
17
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EACL,SAAS,EACT,kBAAkB,EAClB,gBAAgB,EAChB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,SAAS,GACf,MAAM,eAAe,CAAA;AAGtB,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,yBAAyB,EACzB,QAAQ,EACR,2BAA2B,EAC3B,eAAe,EACf,KAAK,wBAAwB,EAC7B,KAAK,yBAAyB,EAC9B,KAAK,gBAAgB,GACtB,MAAM,iBAAiB,CAAA;AAGxB,OAAO,EACL,cAAc,EACd,WAAW,EACX,KAAK,qBAAqB,EAC1B,KAAK,kBAAkB,GACxB,MAAM,aAAa,CAAA;AAGpB,OAAO,EACL,YAAY,EACZ,WAAW,EACX,eAAe,EACf,KAAK,mBAAmB,EACxB,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,kBAAkB,GACxB,MAAM,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EACL,SAAS,EACT,kBAAkB,EAClB,gBAAgB,EAChB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,SAAS,GACf,MAAM,eAAe,CAAA;AAGtB,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,yBAAyB,EACzB,QAAQ,EACR,2BAA2B,EAC3B,eAAe,EACf,KAAK,wBAAwB,EAC7B,KAAK,yBAAyB,EAC9B,KAAK,gBAAgB,GACtB,MAAM,iBAAiB,CAAA;AAGxB,OAAO,EACL,cAAc,EACd,WAAW,EACX,KAAK,qBAAqB,EAC1B,KAAK,kBAAkB,GACxB,MAAM,aAAa,CAAA;AAGpB,OAAO,EACL,YAAY,EACZ,WAAW,EACX,eAAe,EACf,KAAK,mBAAmB,EACxB,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,kBAAkB,GACxB,MAAM,YAAY,CAAA;AAGnB,OAAO,EACL,eAAe,EACf,WAAW,EACX,iBAAiB,EACjB,0BAA0B,GAC3B,MAAM,iBAAiB,CAAA;AAGxB,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AAGlE,OAAO,EACL,qBAAqB,EACrB,wBAAwB,EACxB,KAAK,YAAY,GAClB,MAAM,uBAAuB,CAAA"}
|
package/dist/runtime/index.js
CHANGED
|
@@ -15,4 +15,10 @@ export { generateEmbedding, generateEmbeddings, shouldRegenerateEmbedding, hashT
|
|
|
15
15
|
export { semanticSearch, findSimilar, } from './search.js';
|
|
16
16
|
// Batch processing
|
|
17
17
|
export { batchProcess, RateLimiter, ProcessingQueue, } from './batch.js';
|
|
18
|
+
// Build-time utilities
|
|
19
|
+
export { simpleChunkText, hashContent, loadExistingIndex, generateDocumentEmbeddings, } from './build-time.js';
|
|
20
|
+
// Markdown processing
|
|
21
|
+
export { stripMarkdown, extractMarkdownText } from './markdown.js';
|
|
22
|
+
// Provider helpers
|
|
23
|
+
export { createProviderFromEnv, getProviderConfigFromEnv, } from './provider-helpers.js';
|
|
18
24
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,gBAAgB;AAChB,OAAO,EACL,SAAS,EACT,kBAAkB,EAClB,gBAAgB,GAIjB,MAAM,eAAe,CAAA;AAEtB,uBAAuB;AACvB,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,yBAAyB,EACzB,QAAQ,EACR,2BAA2B,EAC3B,eAAe,GAIhB,MAAM,iBAAiB,CAAA;AAExB,kBAAkB;AAClB,OAAO,EACL,cAAc,EACd,WAAW,GAGZ,MAAM,aAAa,CAAA;AAEpB,mBAAmB;AACnB,OAAO,EACL,YAAY,EACZ,WAAW,EACX,eAAe,GAKhB,MAAM,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/runtime/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,gBAAgB;AAChB,OAAO,EACL,SAAS,EACT,kBAAkB,EAClB,gBAAgB,GAIjB,MAAM,eAAe,CAAA;AAEtB,uBAAuB;AACvB,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,yBAAyB,EACzB,QAAQ,EACR,2BAA2B,EAC3B,eAAe,GAIhB,MAAM,iBAAiB,CAAA;AAExB,kBAAkB;AAClB,OAAO,EACL,cAAc,EACd,WAAW,GAGZ,MAAM,aAAa,CAAA;AAEpB,mBAAmB;AACnB,OAAO,EACL,YAAY,EACZ,WAAW,EACX,eAAe,GAKhB,MAAM,YAAY,CAAA;AAEnB,uBAAuB;AACvB,OAAO,EACL,eAAe,EACf,WAAW,EACX,iBAAiB,EACjB,0BAA0B,GAC3B,MAAM,iBAAiB,CAAA;AAExB,sBAAsB;AACtB,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AAElE,mBAAmB;AACnB,OAAO,EACL,qBAAqB,EACrB,wBAAwB,GAEzB,MAAM,uBAAuB,CAAA"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown processing utilities for content preparation
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Strip markdown formatting for cleaner text suitable for embeddings
|
|
6
|
+
*
|
|
7
|
+
* Removes code blocks, formatting markers, links, images, and HTML tags
|
|
8
|
+
* while preserving the actual content.
|
|
9
|
+
*
|
|
10
|
+
* @param markdown - Markdown text to process
|
|
11
|
+
* @returns Plain text with markdown removed
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { stripMarkdown } from '@opensaas/stack-rag/runtime'
|
|
16
|
+
*
|
|
17
|
+
* const markdown = '# Hello\n\nThis is **bold** text with a [link](url).'
|
|
18
|
+
* const plain = stripMarkdown(markdown)
|
|
19
|
+
* // Returns: 'Hello\n\nThis is bold text with a link.'
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function stripMarkdown(markdown: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Extract text content from common markdown structures
|
|
25
|
+
*
|
|
26
|
+
* More aggressive than stripMarkdown - extracts only text content,
|
|
27
|
+
* removes all structural elements.
|
|
28
|
+
*
|
|
29
|
+
* @param markdown - Markdown text
|
|
30
|
+
* @returns Extracted plain text
|
|
31
|
+
*/
|
|
32
|
+
export declare function extractMarkdownText(markdown: string): string;
|
|
33
|
+
//# sourceMappingURL=markdown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown.d.ts","sourceRoot":"","sources":["../../src/runtime/markdown.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CA8BtD;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAuD5D"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown processing utilities for content preparation
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Strip markdown formatting for cleaner text suitable for embeddings
|
|
6
|
+
*
|
|
7
|
+
* Removes code blocks, formatting markers, links, images, and HTML tags
|
|
8
|
+
* while preserving the actual content.
|
|
9
|
+
*
|
|
10
|
+
* @param markdown - Markdown text to process
|
|
11
|
+
* @returns Plain text with markdown removed
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { stripMarkdown } from '@opensaas/stack-rag/runtime'
|
|
16
|
+
*
|
|
17
|
+
* const markdown = '# Hello\n\nThis is **bold** text with a [link](url).'
|
|
18
|
+
* const plain = stripMarkdown(markdown)
|
|
19
|
+
* // Returns: 'Hello\n\nThis is bold text with a link.'
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function stripMarkdown(markdown) {
|
|
23
|
+
let text = markdown;
|
|
24
|
+
// Remove code blocks
|
|
25
|
+
text = text.replace(/```[\s\S]*?```/g, '');
|
|
26
|
+
text = text.replace(/`[^`]+`/g, '');
|
|
27
|
+
// Remove headings markers but keep text
|
|
28
|
+
text = text.replace(/^#+\s+/gm, '');
|
|
29
|
+
// Remove bold/italic markers
|
|
30
|
+
text = text.replace(/\*\*([^*]+)\*\*/g, '$1');
|
|
31
|
+
text = text.replace(/\*([^*]+)\*/g, '$1');
|
|
32
|
+
text = text.replace(/__([^_]+)__/g, '$1');
|
|
33
|
+
text = text.replace(/_([^_]+)_/g, '$1');
|
|
34
|
+
// Remove links but keep text
|
|
35
|
+
text = text.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1');
|
|
36
|
+
// Remove images
|
|
37
|
+
text = text.replace(/!\[([^\]]*)\]\([^)]+\)/g, '');
|
|
38
|
+
// Remove HTML tags
|
|
39
|
+
text = text.replace(/<[^>]+>/g, '');
|
|
40
|
+
// Normalize whitespace
|
|
41
|
+
text = text.replace(/\n{3,}/g, '\n\n');
|
|
42
|
+
text = text.replace(/[ \t]+/g, ' ');
|
|
43
|
+
return text.trim();
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Extract text content from common markdown structures
|
|
47
|
+
*
|
|
48
|
+
* More aggressive than stripMarkdown - extracts only text content,
|
|
49
|
+
* removes all structural elements.
|
|
50
|
+
*
|
|
51
|
+
* @param markdown - Markdown text
|
|
52
|
+
* @returns Extracted plain text
|
|
53
|
+
*/
|
|
54
|
+
export function extractMarkdownText(markdown) {
|
|
55
|
+
let text = markdown;
|
|
56
|
+
// Remove YAML frontmatter
|
|
57
|
+
text = text.replace(/^---[\s\S]*?---\n/m, '');
|
|
58
|
+
// Remove code blocks entirely (including content)
|
|
59
|
+
text = text.replace(/```[\s\S]*?```/g, '');
|
|
60
|
+
// Remove inline code
|
|
61
|
+
text = text.replace(/`[^`]+`/g, '');
|
|
62
|
+
// Remove horizontal rules
|
|
63
|
+
text = text.replace(/^[-*_]{3,}$/gm, '');
|
|
64
|
+
// Remove blockquotes markers
|
|
65
|
+
text = text.replace(/^>\s+/gm, '');
|
|
66
|
+
// Remove list markers
|
|
67
|
+
text = text.replace(/^[\s]*[-*+]\s+/gm, '');
|
|
68
|
+
text = text.replace(/^[\s]*\d+\.\s+/gm, '');
|
|
69
|
+
// Remove headings markers
|
|
70
|
+
text = text.replace(/^#+\s+/gm, '');
|
|
71
|
+
// Remove emphasis markers
|
|
72
|
+
text = text.replace(/\*\*([^*]+)\*\*/g, '$1');
|
|
73
|
+
text = text.replace(/\*([^*]+)\*/g, '$1');
|
|
74
|
+
text = text.replace(/__([^_]+)__/g, '$1');
|
|
75
|
+
text = text.replace(/_([^_]+)_/g, '$1');
|
|
76
|
+
// Remove strikethrough
|
|
77
|
+
text = text.replace(/~~([^~]+)~~/g, '$1');
|
|
78
|
+
// Remove links but keep text
|
|
79
|
+
text = text.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1');
|
|
80
|
+
// Remove reference-style links
|
|
81
|
+
text = text.replace(/\[([^\]]+)\]\[[^\]]*\]/g, '$1');
|
|
82
|
+
// Remove images
|
|
83
|
+
text = text.replace(/!\[([^\]]*)\]\([^)]+\)/g, '');
|
|
84
|
+
// Remove HTML tags
|
|
85
|
+
text = text.replace(/<[^>]+>/g, '');
|
|
86
|
+
// Remove HTML entities
|
|
87
|
+
text = text.replace(/&[a-z]+;/gi, '');
|
|
88
|
+
// Normalize whitespace
|
|
89
|
+
text = text.replace(/\n{3,}/g, '\n\n');
|
|
90
|
+
text = text.replace(/[ \t]+/g, ' ');
|
|
91
|
+
text = text.replace(/^\s+|\s+$/gm, '');
|
|
92
|
+
return text.trim();
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=markdown.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"markdown.js","sourceRoot":"","sources":["../../src/runtime/markdown.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,aAAa,CAAC,QAAgB;IAC5C,IAAI,IAAI,GAAG,QAAQ,CAAA;IAEnB,qBAAqB;IACrB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,iBAAiB,EAAE,EAAE,CAAC,CAAA;IAC1C,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAA;IAEnC,wCAAwC;IACxC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAA;IAEnC,6BAA6B;IAC7B,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,kBAAkB,EAAE,IAAI,CAAC,CAAA;IAC7C,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,IAAI,CAAC,CAAA;IACzC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,IAAI,CAAC,CAAA;IACzC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,IAAI,CAAC,CAAA;IAEvC,6BAA6B;IAC7B,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,wBAAwB,EAAE,IAAI,CAAC,CAAA;IAEnD,gBAAgB;IAChB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,yBAAyB,EAAE,EAAE,CAAC,CAAA;IAElD,mBAAmB;IACnB,IAAI,GAAG,IAAI,CAAC,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AG,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,IAAI,CAAC,CAAA;IACzC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,IAAI,CAAC,CAAA;IAEvC,uBAAuB;IACvB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,IAAI,CAAC,CAAA;IAEzC,6BAA6B;IAC7B,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,wBAAwB,EAAE,IAAI,CAAC,CAAA;IAEnD,+BAA+B;IAC/B,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,yBAAyB,EAAE,IAAI,CAAC,CAAA;IAEpD,gBAAgB;IAChB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,yBAAyB,EAAE,EAAE,CAAC,CAAA;IAElD,mBAAmB;IACnB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAA;IAEnC,uBAAuB;IACvB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAA;IAErC,uBAAuB;IACvB,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAA;IACtC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;IACnC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAAA;IAEtC,OAAO,IAAI,CAAC,IAAI,EAAE,CAAA;AACpB,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper utilities for working with embedding providers
|
|
3
|
+
* Simplifies provider creation from environment variables
|
|
4
|
+
*/
|
|
5
|
+
import type { EmbeddingProvider } from '../providers/types.js';
|
|
6
|
+
import type { EmbeddingProviderConfig } from '../config/types.js';
|
|
7
|
+
import 'dotenv/config';
|
|
8
|
+
/**
|
|
9
|
+
* Provider type from environment or configuration
|
|
10
|
+
*/
|
|
11
|
+
export type ProviderType = 'openai' | 'ollama';
|
|
12
|
+
/**
|
|
13
|
+
* Create an embedding provider from environment variables
|
|
14
|
+
*
|
|
15
|
+
* Reads configuration from environment variables:
|
|
16
|
+
* - EMBEDDING_PROVIDER: 'openai' or 'ollama' (default: 'openai')
|
|
17
|
+
* - OPENAI_API_KEY: Required if using OpenAI
|
|
18
|
+
* - OLLAMA_BASE_URL: Ollama endpoint (default: 'http://localhost:11434')
|
|
19
|
+
*
|
|
20
|
+
* @param overrides - Optional overrides for environment config
|
|
21
|
+
* @returns Configured embedding provider
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* import { createProviderFromEnv } from '@opensaas/stack-rag/runtime'
|
|
26
|
+
*
|
|
27
|
+
* // Uses EMBEDDING_PROVIDER and OPENAI_API_KEY from env
|
|
28
|
+
* const provider = createProviderFromEnv()
|
|
29
|
+
*
|
|
30
|
+
* // Override provider type
|
|
31
|
+
* const ollamaProvider = createProviderFromEnv({ provider: 'ollama' })
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function createProviderFromEnv(overrides?: {
|
|
35
|
+
provider?: ProviderType;
|
|
36
|
+
openaiApiKey?: string;
|
|
37
|
+
ollamaBaseUrl?: string;
|
|
38
|
+
model?: string;
|
|
39
|
+
}): EmbeddingProvider;
|
|
40
|
+
/**
|
|
41
|
+
* Get provider configuration from environment
|
|
42
|
+
*
|
|
43
|
+
* Useful for inspecting what provider would be used without creating it.
|
|
44
|
+
*
|
|
45
|
+
* @returns Provider configuration object
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* import { getProviderConfigFromEnv } from '@opensaas/stack-rag/runtime'
|
|
50
|
+
*
|
|
51
|
+
* const config = getProviderConfigFromEnv()
|
|
52
|
+
* console.log(`Using ${config.type} provider`)
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function getProviderConfigFromEnv(): EmbeddingProviderConfig;
|
|
56
|
+
//# sourceMappingURL=provider-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-helpers.d.ts","sourceRoot":"","sources":["../../src/runtime/provider-helpers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AAC9D,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAA;AACjE,OAAO,eAAe,CAAA;AAEtB;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,QAAQ,CAAA;AAE9C;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,qBAAqB,CAAC,SAAS,CAAC,EAAE;IAChD,QAAQ,CAAC,EAAE,YAAY,CAAA;IACvB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf,GAAG,iBAAiB,CA+BpB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,wBAAwB,IAAI,uBAAuB,CAwBlE"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper utilities for working with embedding providers
|
|
3
|
+
* Simplifies provider creation from environment variables
|
|
4
|
+
*/
|
|
5
|
+
import { createEmbeddingProvider } from '../providers/index.js';
|
|
6
|
+
import 'dotenv/config';
|
|
7
|
+
/**
|
|
8
|
+
* Create an embedding provider from environment variables
|
|
9
|
+
*
|
|
10
|
+
* Reads configuration from environment variables:
|
|
11
|
+
* - EMBEDDING_PROVIDER: 'openai' or 'ollama' (default: 'openai')
|
|
12
|
+
* - OPENAI_API_KEY: Required if using OpenAI
|
|
13
|
+
* - OLLAMA_BASE_URL: Ollama endpoint (default: 'http://localhost:11434')
|
|
14
|
+
*
|
|
15
|
+
* @param overrides - Optional overrides for environment config
|
|
16
|
+
* @returns Configured embedding provider
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { createProviderFromEnv } from '@opensaas/stack-rag/runtime'
|
|
21
|
+
*
|
|
22
|
+
* // Uses EMBEDDING_PROVIDER and OPENAI_API_KEY from env
|
|
23
|
+
* const provider = createProviderFromEnv()
|
|
24
|
+
*
|
|
25
|
+
* // Override provider type
|
|
26
|
+
* const ollamaProvider = createProviderFromEnv({ provider: 'ollama' })
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function createProviderFromEnv(overrides) {
|
|
30
|
+
const providerType = overrides?.provider || process.env.EMBEDDING_PROVIDER || 'openai';
|
|
31
|
+
let config;
|
|
32
|
+
if (providerType === 'openai') {
|
|
33
|
+
const apiKey = overrides?.openaiApiKey || process.env.OPENAI_API_KEY;
|
|
34
|
+
if (!apiKey) {
|
|
35
|
+
throw new Error('OPENAI_API_KEY environment variable is required when using OpenAI provider');
|
|
36
|
+
}
|
|
37
|
+
config = {
|
|
38
|
+
type: 'openai',
|
|
39
|
+
apiKey,
|
|
40
|
+
model: overrides?.model ||
|
|
41
|
+
'text-embedding-3-small',
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
else if (providerType === 'ollama') {
|
|
45
|
+
config = {
|
|
46
|
+
type: 'ollama',
|
|
47
|
+
baseURL: overrides?.ollamaBaseUrl || process.env.OLLAMA_BASE_URL || 'http://localhost:11434',
|
|
48
|
+
model: overrides?.model || 'nomic-embed-text',
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
throw new Error(`Unknown provider type: ${providerType}. Supported: openai, ollama`);
|
|
53
|
+
}
|
|
54
|
+
return createEmbeddingProvider(config);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get provider configuration from environment
|
|
58
|
+
*
|
|
59
|
+
* Useful for inspecting what provider would be used without creating it.
|
|
60
|
+
*
|
|
61
|
+
* @returns Provider configuration object
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* import { getProviderConfigFromEnv } from '@opensaas/stack-rag/runtime'
|
|
66
|
+
*
|
|
67
|
+
* const config = getProviderConfigFromEnv()
|
|
68
|
+
* console.log(`Using ${config.type} provider`)
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export function getProviderConfigFromEnv() {
|
|
72
|
+
const providerType = process.env.EMBEDDING_PROVIDER || 'openai';
|
|
73
|
+
if (providerType === 'openai') {
|
|
74
|
+
const apiKey = process.env.OPENAI_API_KEY;
|
|
75
|
+
if (!apiKey) {
|
|
76
|
+
throw new Error('OPENAI_API_KEY environment variable is required');
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
type: 'openai',
|
|
80
|
+
apiKey,
|
|
81
|
+
model: 'text-embedding-3-small',
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
else if (providerType === 'ollama') {
|
|
85
|
+
return {
|
|
86
|
+
type: 'ollama',
|
|
87
|
+
baseURL: process.env.OLLAMA_BASE_URL || 'http://localhost:11434',
|
|
88
|
+
model: 'nomic-embed-text',
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
throw new Error(`Unknown provider type: ${providerType}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=provider-helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-helpers.js","sourceRoot":"","sources":["../../src/runtime/provider-helpers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAA;AAG/D,OAAO,eAAe,CAAA;AAOtB;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,qBAAqB,CAAC,SAKrC;IACC,MAAM,YAAY,GAChB,SAAS,EAAE,QAAQ,IAAK,OAAO,CAAC,GAAG,CAAC,kBAA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I,YAAY,KAAK,QAAQ,EAAE,CAAC;QACrC,OAAO;YACL,IAAI,EAAE,QAAQ;YACd,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,wBAAwB;YAChE,KAAK,EAAE,kBAAkB;SAC1B,CAAA;IACH,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,0BAA0B,YAAY,EAAE,CAAC,CAAA;IAC3D,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for RAG plugin runtime services
|
|
3
|
+
* These types are used for type-safe access to context.plugins.rag
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Runtime services provided by the RAG plugin
|
|
7
|
+
* Available via context.plugins.rag
|
|
8
|
+
*/
|
|
9
|
+
export interface RAGRuntimeServices {
|
|
10
|
+
/**
|
|
11
|
+
* Generate embedding for a given text
|
|
12
|
+
* Uses the configured embedding provider
|
|
13
|
+
*
|
|
14
|
+
* @param text - The text to generate an embedding for
|
|
15
|
+
* @param providerName - Optional provider name if multiple providers are configured
|
|
16
|
+
* @returns Vector embedding as array of numbers
|
|
17
|
+
*/
|
|
18
|
+
generateEmbedding: (text: string, providerName?: string) => Promise<number[]>;
|
|
19
|
+
/**
|
|
20
|
+
* Generate embeddings for multiple texts (batch)
|
|
21
|
+
* More efficient than calling generateEmbedding multiple times
|
|
22
|
+
*
|
|
23
|
+
* @param texts - Array of texts to generate embeddings for
|
|
24
|
+
* @param providerName - Optional provider name if multiple providers are configured
|
|
25
|
+
* @returns Array of vector embeddings
|
|
26
|
+
*/
|
|
27
|
+
generateEmbeddings: (texts: string[], providerName?: string) => Promise<number[][]>;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/runtime/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;GAGG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;;;;;OAOG;IACH,iBAAiB,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;IAE7E;;;;;;;OAOG;IACH,kBAAkB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,YAAY,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;CACpF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/runtime/types.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
|
package/dist/storage/index.d.ts
CHANGED
|
@@ -36,6 +36,7 @@ export declare function registerVectorStorage(type: string, factory: (config: Ve
|
|
|
36
36
|
export declare function createVectorStorage(config: VectorStorageConfig): VectorStorage;
|
|
37
37
|
export * from './types.js';
|
|
38
38
|
export { JsonVectorStorage, createJsonStorage } from './json.js';
|
|
39
|
+
export { JsonFileStorage, createJsonFileStorage } from './json-file.js';
|
|
39
40
|
export { PgVectorStorage, createPgVectorStorage } from './pgvector.js';
|
|
40
41
|
export { SqliteVssStorage, createSqliteVssStorage } from './sqlite-vss.js';
|
|
41
42
|
export { buildAccessControlFilter, mergeAccessFilter, prismaFilterToSQL } from './access-filter.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/storage/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAC/C,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAA;AA4B7D;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,CAAC,MAAM,EAAE,mBAAmB,KAAK,aAAa,GACtD,IAAI,CAEN;AAED;;;;;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@@ -68,6 +68,7 @@ export function createVectorStorage(config) {
|
|
|
68
68
|
// Export types and individual storage backends
|
|
69
69
|
export * from './types.js';
|
|
70
70
|
export { JsonVectorStorage, createJsonStorage } from './json.js';
|
|
71
|
+
export { JsonFileStorage, createJsonFileStorage } from './json-file.js';
|
|
71
72
|
export { PgVectorStorage, createPgVectorStorage } from './pgvector.js';
|
|
72
73
|
export { SqliteVssStorage, createSqliteVssStorage } from './sqlite-vss.js';
|
|
73
74
|
// Export access control utilities
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/storage/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAA;AAC7C,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAA;AAExD;;;GAGG;AACH,MAAM,gBAAgB,GAAG,IAAI,GAAG,EAA0D,CAAA;AAE1F;;GAEG;AACH,gBAAgB,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,iBAAiB,EAAE,CAAC,CAAA;AACvD,gBAAgB,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC,MAAM,EAAE,EAAE;IAC1C,IAAI,MAAM,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAA;IAC7D,CAAC;IACD,OAAO,qBAAqB,CAAC,MAA4D,CAAC,CAAA;AAC5F,CAAC,CAAC,CAAA;AACF,gBAAgB,CAAC,GAAG,CAAC,YAAY,EAAE,CAAC,MAAM,EAAE,EAAE;IAC5C,IAAI,MAAM,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAA;IAC/D,CAAC;IACD,OAAO,sBAAsB,CAAC,MAA6D,CAAC,CAAA;AAC9F,CAAC,CAA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