@opensaas/stack-rag 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +10 -0
  3. package/CLAUDE.md +565 -0
  4. package/LICENSE +21 -0
  5. package/README.md +406 -0
  6. package/dist/config/index.d.ts +63 -0
  7. package/dist/config/index.d.ts.map +1 -0
  8. package/dist/config/index.js +94 -0
  9. package/dist/config/index.js.map +1 -0
  10. package/dist/config/plugin.d.ts +38 -0
  11. package/dist/config/plugin.d.ts.map +1 -0
  12. package/dist/config/plugin.js +215 -0
  13. package/dist/config/plugin.js.map +1 -0
  14. package/dist/config/plugin.test.d.ts +2 -0
  15. package/dist/config/plugin.test.d.ts.map +1 -0
  16. package/dist/config/plugin.test.js +554 -0
  17. package/dist/config/plugin.test.js.map +1 -0
  18. package/dist/config/types.d.ts +249 -0
  19. package/dist/config/types.d.ts.map +1 -0
  20. package/dist/config/types.js +5 -0
  21. package/dist/config/types.js.map +1 -0
  22. package/dist/fields/embedding.d.ts +85 -0
  23. package/dist/fields/embedding.d.ts.map +1 -0
  24. package/dist/fields/embedding.js +81 -0
  25. package/dist/fields/embedding.js.map +1 -0
  26. package/dist/fields/embedding.test.d.ts +2 -0
  27. package/dist/fields/embedding.test.d.ts.map +1 -0
  28. package/dist/fields/embedding.test.js +323 -0
  29. package/dist/fields/embedding.test.js.map +1 -0
  30. package/dist/fields/index.d.ts +6 -0
  31. package/dist/fields/index.d.ts.map +1 -0
  32. package/dist/fields/index.js +5 -0
  33. package/dist/fields/index.js.map +1 -0
  34. package/dist/index.d.ts +8 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +9 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/mcp/index.d.ts +19 -0
  39. package/dist/mcp/index.d.ts.map +1 -0
  40. package/dist/mcp/index.js +18 -0
  41. package/dist/mcp/index.js.map +1 -0
  42. package/dist/providers/index.d.ts +38 -0
  43. package/dist/providers/index.d.ts.map +1 -0
  44. package/dist/providers/index.js +68 -0
  45. package/dist/providers/index.js.map +1 -0
  46. package/dist/providers/ollama.d.ts +49 -0
  47. package/dist/providers/ollama.d.ts.map +1 -0
  48. package/dist/providers/ollama.js +151 -0
  49. package/dist/providers/ollama.js.map +1 -0
  50. package/dist/providers/openai.d.ts +41 -0
  51. package/dist/providers/openai.d.ts.map +1 -0
  52. package/dist/providers/openai.js +126 -0
  53. package/dist/providers/openai.js.map +1 -0
  54. package/dist/providers/providers.test.d.ts +2 -0
  55. package/dist/providers/providers.test.d.ts.map +1 -0
  56. package/dist/providers/providers.test.js +224 -0
  57. package/dist/providers/providers.test.js.map +1 -0
  58. package/dist/providers/types.d.ts +88 -0
  59. package/dist/providers/types.d.ts.map +1 -0
  60. package/dist/providers/types.js +2 -0
  61. package/dist/providers/types.js.map +1 -0
  62. package/dist/runtime/batch.d.ts +183 -0
  63. package/dist/runtime/batch.d.ts.map +1 -0
  64. package/dist/runtime/batch.js +240 -0
  65. package/dist/runtime/batch.js.map +1 -0
  66. package/dist/runtime/batch.test.d.ts +2 -0
  67. package/dist/runtime/batch.test.d.ts.map +1 -0
  68. package/dist/runtime/batch.test.js +251 -0
  69. package/dist/runtime/batch.test.js.map +1 -0
  70. package/dist/runtime/chunking.d.ts +42 -0
  71. package/dist/runtime/chunking.d.ts.map +1 -0
  72. package/dist/runtime/chunking.js +264 -0
  73. package/dist/runtime/chunking.js.map +1 -0
  74. package/dist/runtime/chunking.test.d.ts +2 -0
  75. package/dist/runtime/chunking.test.d.ts.map +1 -0
  76. package/dist/runtime/chunking.test.js +212 -0
  77. package/dist/runtime/chunking.test.js.map +1 -0
  78. package/dist/runtime/embeddings.d.ts +147 -0
  79. package/dist/runtime/embeddings.d.ts.map +1 -0
  80. package/dist/runtime/embeddings.js +201 -0
  81. package/dist/runtime/embeddings.js.map +1 -0
  82. package/dist/runtime/embeddings.test.d.ts +2 -0
  83. package/dist/runtime/embeddings.test.d.ts.map +1 -0
  84. package/dist/runtime/embeddings.test.js +366 -0
  85. package/dist/runtime/embeddings.test.js.map +1 -0
  86. package/dist/runtime/index.d.ts +14 -0
  87. package/dist/runtime/index.d.ts.map +1 -0
  88. package/dist/runtime/index.js +18 -0
  89. package/dist/runtime/index.js.map +1 -0
  90. package/dist/runtime/search.d.ts +135 -0
  91. package/dist/runtime/search.d.ts.map +1 -0
  92. package/dist/runtime/search.js +101 -0
  93. package/dist/runtime/search.js.map +1 -0
  94. package/dist/storage/index.d.ts +41 -0
  95. package/dist/storage/index.d.ts.map +1 -0
  96. package/dist/storage/index.js +73 -0
  97. package/dist/storage/index.js.map +1 -0
  98. package/dist/storage/json.d.ts +34 -0
  99. package/dist/storage/json.d.ts.map +1 -0
  100. package/dist/storage/json.js +82 -0
  101. package/dist/storage/json.js.map +1 -0
  102. package/dist/storage/pgvector.d.ts +53 -0
  103. package/dist/storage/pgvector.d.ts.map +1 -0
  104. package/dist/storage/pgvector.js +168 -0
  105. package/dist/storage/pgvector.js.map +1 -0
  106. package/dist/storage/sqlite-vss.d.ts +49 -0
  107. package/dist/storage/sqlite-vss.d.ts.map +1 -0
  108. package/dist/storage/sqlite-vss.js +148 -0
  109. package/dist/storage/sqlite-vss.js.map +1 -0
  110. package/dist/storage/storage.test.d.ts +2 -0
  111. package/dist/storage/storage.test.d.ts.map +1 -0
  112. package/dist/storage/storage.test.js +440 -0
  113. package/dist/storage/storage.test.js.map +1 -0
  114. package/dist/storage/types.d.ts +79 -0
  115. package/dist/storage/types.d.ts.map +1 -0
  116. package/dist/storage/types.js +49 -0
  117. package/dist/storage/types.js.map +1 -0
  118. package/package.json +82 -0
  119. package/src/config/index.ts +116 -0
  120. package/src/config/plugin.test.ts +664 -0
  121. package/src/config/plugin.ts +257 -0
  122. package/src/config/types.ts +283 -0
  123. package/src/fields/embedding.test.ts +408 -0
  124. package/src/fields/embedding.ts +150 -0
  125. package/src/fields/index.ts +6 -0
  126. package/src/index.ts +33 -0
  127. package/src/mcp/index.ts +21 -0
  128. package/src/providers/index.ts +81 -0
  129. package/src/providers/ollama.ts +186 -0
  130. package/src/providers/openai.ts +161 -0
  131. package/src/providers/providers.test.ts +275 -0
  132. package/src/providers/types.ts +100 -0
  133. package/src/runtime/batch.test.ts +332 -0
  134. package/src/runtime/batch.ts +424 -0
  135. package/src/runtime/chunking.test.ts +258 -0
  136. package/src/runtime/chunking.ts +334 -0
  137. package/src/runtime/embeddings.test.ts +441 -0
  138. package/src/runtime/embeddings.ts +380 -0
  139. package/src/runtime/index.ts +51 -0
  140. package/src/runtime/search.ts +243 -0
  141. package/src/storage/index.ts +86 -0
  142. package/src/storage/json.ts +106 -0
  143. package/src/storage/pgvector.ts +206 -0
  144. package/src/storage/sqlite-vss.ts +193 -0
  145. package/src/storage/storage.test.ts +521 -0
  146. package/src/storage/types.ts +126 -0
  147. package/tsconfig.json +13 -0
  148. package/tsconfig.tsbuildinfo +1 -0
  149. package/vitest.config.ts +18 -0
@@ -0,0 +1,249 @@
1
+ /**
2
+ * RAG (Retrieval-Augmented Generation) configuration types
3
+ */
4
+ /**
5
+ * Text chunking strategies for splitting documents
6
+ */
7
+ export type ChunkingStrategy = 'none' | 'recursive' | 'sentence' | 'sliding-window';
8
+ /**
9
+ * Chunking configuration options
10
+ */
11
+ export type ChunkingConfig = {
12
+ /**
13
+ * Chunking strategy to use
14
+ * @default 'recursive'
15
+ */
16
+ strategy?: ChunkingStrategy;
17
+ /**
18
+ * Maximum tokens per chunk
19
+ * @default 500
20
+ */
21
+ maxTokens?: number;
22
+ /**
23
+ * Overlap between chunks (tokens)
24
+ * Only applies to 'recursive' and 'sliding-window' strategies
25
+ * @default 50
26
+ */
27
+ overlap?: number;
28
+ };
29
+ /**
30
+ * Embedding provider names
31
+ */
32
+ export type EmbeddingProviderName = 'openai' | 'ollama' | string;
33
+ /**
34
+ * OpenAI embedding model options
35
+ */
36
+ export type OpenAIEmbeddingModel = 'text-embedding-3-small' | 'text-embedding-3-large' | 'text-embedding-ada-002';
37
+ /**
38
+ * OpenAI embedding provider configuration
39
+ */
40
+ export type OpenAIEmbeddingConfig = {
41
+ type: 'openai';
42
+ /**
43
+ * OpenAI API key
44
+ */
45
+ apiKey: string;
46
+ /**
47
+ * Model to use for embeddings
48
+ * @default 'text-embedding-3-small'
49
+ */
50
+ model?: OpenAIEmbeddingModel;
51
+ /**
52
+ * Organization ID (optional)
53
+ */
54
+ organization?: string;
55
+ /**
56
+ * Base URL for API requests (optional, for Azure or custom endpoints)
57
+ */
58
+ baseURL?: string;
59
+ };
60
+ /**
61
+ * Ollama embedding provider configuration
62
+ */
63
+ export type OllamaEmbeddingConfig = {
64
+ type: 'ollama';
65
+ /**
66
+ * Ollama API endpoint
67
+ * @default 'http://localhost:11434'
68
+ */
69
+ baseURL?: string;
70
+ /**
71
+ * Model to use for embeddings
72
+ * @default 'nomic-embed-text'
73
+ */
74
+ model?: string;
75
+ };
76
+ /**
77
+ * Generic embedding provider configuration
78
+ * For custom or third-party providers
79
+ */
80
+ export type CustomEmbeddingConfig = {
81
+ type: string;
82
+ [key: string]: unknown;
83
+ };
84
+ /**
85
+ * Embedding provider configuration union
86
+ */
87
+ export type EmbeddingProviderConfig = OpenAIEmbeddingConfig | OllamaEmbeddingConfig | CustomEmbeddingConfig;
88
+ /**
89
+ * Vector storage backend types
90
+ */
91
+ export type VectorStorageBackend = 'pgvector' | 'sqlite-vss' | 'json' | string;
92
+ /**
93
+ * pgvector storage configuration
94
+ */
95
+ export type PgVectorStorageConfig = {
96
+ type: 'pgvector';
97
+ /**
98
+ * Distance function for similarity search
99
+ * @default 'cosine'
100
+ */
101
+ distanceFunction?: 'cosine' | 'l2' | 'inner_product';
102
+ };
103
+ /**
104
+ * SQLite VSS storage configuration
105
+ */
106
+ export type SqliteVssStorageConfig = {
107
+ type: 'sqlite-vss';
108
+ /**
109
+ * Distance function for similarity search
110
+ * @default 'cosine'
111
+ */
112
+ distanceFunction?: 'cosine' | 'l2';
113
+ };
114
+ /**
115
+ * JSON-based storage (in-memory search)
116
+ * Stores vectors as JSON and uses JavaScript for similarity search
117
+ * Good for development and small datasets
118
+ */
119
+ export type JsonStorageConfig = {
120
+ type: 'json';
121
+ };
122
+ /**
123
+ * Generic storage configuration for custom backends
124
+ */
125
+ export type CustomStorageConfig = {
126
+ type: string;
127
+ [key: string]: unknown;
128
+ };
129
+ /**
130
+ * Vector storage configuration union
131
+ */
132
+ export type VectorStorageConfig = PgVectorStorageConfig | SqliteVssStorageConfig | JsonStorageConfig | CustomStorageConfig;
133
+ /**
134
+ * Main RAG configuration
135
+ */
136
+ export type RAGConfig = {
137
+ /**
138
+ * Default embedding provider
139
+ * Can be overridden per field
140
+ */
141
+ provider?: EmbeddingProviderConfig;
142
+ /**
143
+ * Named embedding providers
144
+ * Allows using different providers for different fields
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * providers: {
149
+ * openai: openaiEmbeddings({ apiKey: '...' }),
150
+ * ollama: ollamaEmbeddings({ model: 'nomic-embed-text' })
151
+ * }
152
+ * ```
153
+ */
154
+ providers?: Record<string, EmbeddingProviderConfig>;
155
+ /**
156
+ * Vector storage backend
157
+ * @default { type: 'json' }
158
+ */
159
+ storage?: VectorStorageConfig;
160
+ /**
161
+ * Default chunking configuration
162
+ * Can be overridden per field
163
+ */
164
+ chunking?: ChunkingConfig;
165
+ /**
166
+ * Whether to enable MCP tools for semantic search
167
+ * Requires MCP to be enabled in main config
168
+ * @default true
169
+ */
170
+ enableMcpTools?: boolean;
171
+ /**
172
+ * Batch size for embedding generation
173
+ * @default 10
174
+ */
175
+ batchSize?: number;
176
+ /**
177
+ * Rate limit (requests per minute) for embedding API calls
178
+ * @default 100
179
+ */
180
+ rateLimit?: number;
181
+ };
182
+ /**
183
+ * Normalized RAG configuration with defaults applied
184
+ */
185
+ export type NormalizedRAGConfig = {
186
+ provider: EmbeddingProviderConfig | null;
187
+ providers: Record<string, EmbeddingProviderConfig>;
188
+ storage: VectorStorageConfig;
189
+ chunking: Required<ChunkingConfig>;
190
+ enableMcpTools: boolean;
191
+ batchSize: number;
192
+ rateLimit: number;
193
+ };
194
+ /**
195
+ * Embedding metadata stored alongside vectors
196
+ */
197
+ export type EmbeddingMetadata = {
198
+ /**
199
+ * Embedding model used
200
+ */
201
+ model: string;
202
+ /**
203
+ * Provider type (openai, ollama, etc.)
204
+ */
205
+ provider: string;
206
+ /**
207
+ * Vector dimensions
208
+ */
209
+ dimensions: number;
210
+ /**
211
+ * When the embedding was generated
212
+ */
213
+ generatedAt: string;
214
+ /**
215
+ * Hash of source text (for detecting changes)
216
+ */
217
+ sourceHash?: string;
218
+ };
219
+ /**
220
+ * Stored embedding with vector and metadata
221
+ */
222
+ export type StoredEmbedding = {
223
+ /**
224
+ * The embedding vector
225
+ */
226
+ vector: number[];
227
+ /**
228
+ * Metadata about the embedding
229
+ */
230
+ metadata: EmbeddingMetadata;
231
+ };
232
+ /**
233
+ * Semantic search result
234
+ */
235
+ export type SearchResult<T = unknown> = {
236
+ /**
237
+ * The matching item
238
+ */
239
+ item: T;
240
+ /**
241
+ * Similarity score (0-1, higher is more similar)
242
+ */
243
+ score: number;
244
+ /**
245
+ * Distance metric (depends on storage backend)
246
+ */
247
+ distance: number;
248
+ };
249
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/config/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,WAAW,GAAG,UAAU,GAAG,gBAAgB,CAAA;AAEnF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B;;;OAGG;IACH,QAAQ,CAAC,EAAE,gBAAgB,CAAA;IAC3B;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,CAAA;AAED;;GAEG;A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G;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,UAAU,EAAE,MAAM,CAAA;IAClB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAA;IACnB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B;;OAEG;IACH,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB;;OAEG;IACH,QAAQ,EAAE,iBAAiB,CAAA;CAC5B,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,GAAG,OAAO,IAAI;IACtC;;OAEG;IACH,IAAI,EAAE,CAAC,CAAA;IACP;;OAEG;IACH,KAAK,EAAE,MAAM,CAAA;IACb;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * RAG (Retrieval-Augmented Generation) configuration types
3
+ */
4
+ export {};
5
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/config/types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
@@ -0,0 +1,85 @@
1
+ import type { BaseFieldConfig } from '@opensaas/stack-core';
2
+ import type { StoredEmbedding, EmbeddingProviderName, ChunkingConfig } from '../config/types.js';
3
+ /**
4
+ * Embedding field configuration
5
+ * Stores vector embeddings as JSON with metadata
6
+ */
7
+ export type EmbeddingField = BaseFieldConfig<StoredEmbedding | null, StoredEmbedding | null> & {
8
+ type: 'embedding';
9
+ /**
10
+ * Source field name to generate embeddings from
11
+ * When this field changes, embeddings will be automatically regenerated
12
+ *
13
+ * @example 'content' or 'title'
14
+ */
15
+ sourceField?: string;
16
+ /**
17
+ * Embedding provider to use
18
+ * References a provider name from RAG config
19
+ * Falls back to default provider if not specified
20
+ *
21
+ * @example 'openai' or 'ollama'
22
+ */
23
+ provider?: EmbeddingProviderName;
24
+ /**
25
+ * Vector dimensions
26
+ * Must match the provider's output dimensions
27
+ * @default 1536 (OpenAI text-embedding-3-small)
28
+ */
29
+ dimensions?: number;
30
+ /**
31
+ * Chunking configuration for long texts
32
+ * Only applies if sourceField is set
33
+ */
34
+ chunking?: ChunkingConfig;
35
+ /**
36
+ * Whether to automatically generate embeddings
37
+ * @default true if sourceField is set
38
+ */
39
+ autoGenerate?: boolean;
40
+ /**
41
+ * UI configuration
42
+ */
43
+ ui?: {
44
+ /**
45
+ * Whether to show the embedding vector in the UI
46
+ * @default false (usually too large to display)
47
+ */
48
+ showVector?: boolean;
49
+ /**
50
+ * Whether to show embedding metadata
51
+ * @default true
52
+ */
53
+ showMetadata?: boolean;
54
+ };
55
+ };
56
+ /**
57
+ * Embedding field builder
58
+ * Creates a field that stores vector embeddings with metadata
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * import { embedding } from '@opensaas/stack-rag/fields'
63
+ *
64
+ * // Manual embedding storage
65
+ * fields: {
66
+ * embedding: embedding({
67
+ * dimensions: 1536,
68
+ * provider: 'openai'
69
+ * })
70
+ * }
71
+ *
72
+ * // Automatic embedding generation from source field
73
+ * fields: {
74
+ * content: text(),
75
+ * contentEmbedding: embedding({
76
+ * sourceField: 'content',
77
+ * provider: 'openai',
78
+ * dimensions: 1536,
79
+ * autoGenerate: true
80
+ * })
81
+ * }
82
+ * ```
83
+ */
84
+ export declare function embedding(options?: Omit<EmbeddingField, 'type'>): EmbeddingField;
85
+ //# sourceMappingURL=embedding.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"embedding.d.ts","sourceRoot":"","sources":["../../src/fields/embedding.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAEhG;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,eAAe,CAAC,eAAe,GAAG,IAAI,EAAE,eAAe,GAAG,IAAI,CAAC,GAAG;IAC7F,IAAI,EAAE,WAAW,CAAA;IAEjB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;IAEpB;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,qBAAqB,CAAA;IAEhC;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IAEnB;;;OAGG;IACH,QAAQ,CAAC,EAAE,cAAc,CAAA;IAEzB;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAA;IAEtB;;OAEG;IACH,EAAE,CAAC,EAAE;QACH;;;WAGG;QACH,UAAU,CAAC,EAAE,OAAO,CAAA;QAEpB;;;WAGG;QACH,YAAY,CAAC,EAAE,OAAO,CAAA;KACvB,CAAA;CACF,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,SAAS,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,cAAc,EAAE,MAAM,CAAC,GAAG,cAAc,CAwDhF"}
@@ -0,0 +1,81 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ * Embedding field builder
4
+ * Creates a field that stores vector embeddings with metadata
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { embedding } from '@opensaas/stack-rag/fields'
9
+ *
10
+ * // Manual embedding storage
11
+ * fields: {
12
+ * embedding: embedding({
13
+ * dimensions: 1536,
14
+ * provider: 'openai'
15
+ * })
16
+ * }
17
+ *
18
+ * // Automatic embedding generation from source field
19
+ * fields: {
20
+ * content: text(),
21
+ * contentEmbedding: embedding({
22
+ * sourceField: 'content',
23
+ * provider: 'openai',
24
+ * dimensions: 1536,
25
+ * autoGenerate: true
26
+ * })
27
+ * }
28
+ * ```
29
+ */
30
+ export function embedding(options) {
31
+ const dimensions = options?.dimensions || 1536;
32
+ const autoGenerate = options?.autoGenerate ?? options?.sourceField != null;
33
+ return {
34
+ type: 'embedding',
35
+ ...options,
36
+ dimensions,
37
+ autoGenerate,
38
+ getZodSchema: (_fieldName, _operation) => {
39
+ // Embedding schema validation
40
+ const embeddingSchema = z.object({
41
+ vector: z.array(z.number()).length(dimensions, {
42
+ message: `Embedding vector must have exactly ${dimensions} dimensions`,
43
+ }),
44
+ metadata: z.object({
45
+ model: z.string(),
46
+ provider: z.string(),
47
+ dimensions: z.number(),
48
+ generatedAt: z.string(),
49
+ sourceHash: z.string().optional(),
50
+ }),
51
+ });
52
+ // Embeddings are always optional in input
53
+ // They are generated automatically via hooks if sourceField is set
54
+ return embeddingSchema.nullable().optional();
55
+ },
56
+ getPrismaType: (_fieldName) => {
57
+ // Store as JSON in database
58
+ // For pgvector, we could use vector() type, but JSON is more portable
59
+ return {
60
+ type: 'Json',
61
+ modifiers: '?', // Always optional
62
+ };
63
+ },
64
+ getTypeScriptType: () => {
65
+ return {
66
+ type: 'StoredEmbedding | null',
67
+ optional: true,
68
+ };
69
+ },
70
+ getTypeScriptImports: () => {
71
+ return [
72
+ {
73
+ names: ['StoredEmbedding'],
74
+ from: '@opensaas/stack-rag',
75
+ typeOnly: true,
76
+ },
77
+ ];
78
+ },
79
+ };
80
+ }
81
+ //# sourceMappingURL=embedding.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"embedding.js","sourceRoot":"","sources":["../../src/fields/embedding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAiEvB;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,SAAS,CAAC,OAAsC;IAC9D,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,IAAI,IAAI,CAAA;IAC9C,MAAM,YAAY,GAAG,OAAO,EAAE,YAAY,IAAI,OAAO,EAAE,WAAW,IAAI,IAAI,CAAA;IAE1E,OAAO;QACL,IAAI,EAAE,WAAW;QACjB,GAAG,OAAO;QACV,UAAU;QACV,YAAY;QAEZ,YAAY,EAAE,CAAC,UAAkB,EAAE,UAA+B,EAAE,EAAE;YACpE,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Root":"","sources":["../../src/fields/embedding.test.ts"],"names":[],"mappings":""}