midnight-mcp 0.1.41 → 0.2.2

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 (103) hide show
  1. package/README.md +32 -1
  2. package/dist/bin.d.ts +1 -0
  3. package/dist/bin.js +60 -0
  4. package/dist/chunk-HOWO4K5A.js +2197 -0
  5. package/dist/chunk-S7G4OHA4.js +8306 -0
  6. package/dist/db-YDGUWI5K.js +7 -0
  7. package/dist/index.d.ts +205 -3
  8. package/dist/index.js +28 -16
  9. package/package.json +16 -6
  10. package/dist/config/compact-version.d.ts +0 -183
  11. package/dist/config/compact-version.js +0 -423
  12. package/dist/db/index.d.ts +0 -3
  13. package/dist/db/index.js +0 -2
  14. package/dist/db/vectorStore.d.ts +0 -69
  15. package/dist/db/vectorStore.js +0 -196
  16. package/dist/pipeline/embeddings.d.ts +0 -25
  17. package/dist/pipeline/embeddings.js +0 -103
  18. package/dist/pipeline/github.d.ts +0 -84
  19. package/dist/pipeline/github.js +0 -399
  20. package/dist/pipeline/index.d.ts +0 -11
  21. package/dist/pipeline/index.js +0 -6
  22. package/dist/pipeline/indexer.d.ts +0 -41
  23. package/dist/pipeline/indexer.js +0 -254
  24. package/dist/pipeline/parser.d.ts +0 -46
  25. package/dist/pipeline/parser.js +0 -436
  26. package/dist/pipeline/releases.d.ts +0 -112
  27. package/dist/pipeline/releases.js +0 -298
  28. package/dist/pipeline/repository.d.ts +0 -372
  29. package/dist/pipeline/repository.js +0 -520
  30. package/dist/prompts/index.d.ts +0 -3
  31. package/dist/prompts/index.js +0 -2
  32. package/dist/prompts/templates.d.ts +0 -26
  33. package/dist/prompts/templates.js +0 -443
  34. package/dist/resources/code.d.ts +0 -15
  35. package/dist/resources/code.js +0 -122
  36. package/dist/resources/content/code-content.d.ts +0 -6
  37. package/dist/resources/content/code-content.js +0 -802
  38. package/dist/resources/content/docs-content.d.ts +0 -14
  39. package/dist/resources/content/docs-content.js +0 -1202
  40. package/dist/resources/content/index.d.ts +0 -6
  41. package/dist/resources/content/index.js +0 -6
  42. package/dist/resources/docs.d.ts +0 -15
  43. package/dist/resources/docs.js +0 -98
  44. package/dist/resources/index.d.ts +0 -6
  45. package/dist/resources/index.js +0 -13
  46. package/dist/resources/schemas.d.ts +0 -16
  47. package/dist/resources/schemas.js +0 -407
  48. package/dist/scripts/index-repos.d.ts +0 -12
  49. package/dist/scripts/index-repos.js +0 -53
  50. package/dist/server.d.ts +0 -43
  51. package/dist/server.js +0 -696
  52. package/dist/services/index.d.ts +0 -6
  53. package/dist/services/index.js +0 -6
  54. package/dist/services/sampling.d.ts +0 -62
  55. package/dist/services/sampling.js +0 -277
  56. package/dist/tools/analyze.d.ts +0 -106
  57. package/dist/tools/analyze.js +0 -431
  58. package/dist/tools/generation.d.ts +0 -9
  59. package/dist/tools/generation.js +0 -285
  60. package/dist/tools/health.d.ts +0 -120
  61. package/dist/tools/health.js +0 -365
  62. package/dist/tools/index.d.ts +0 -14
  63. package/dist/tools/index.js +0 -22
  64. package/dist/tools/meta.d.ts +0 -61
  65. package/dist/tools/meta.js +0 -282
  66. package/dist/tools/repository/constants.d.ts +0 -19
  67. package/dist/tools/repository/constants.js +0 -324
  68. package/dist/tools/repository/handlers.d.ts +0 -373
  69. package/dist/tools/repository/handlers.js +0 -724
  70. package/dist/tools/repository/index.d.ts +0 -9
  71. package/dist/tools/repository/index.js +0 -13
  72. package/dist/tools/repository/schemas.d.ts +0 -153
  73. package/dist/tools/repository/schemas.js +0 -106
  74. package/dist/tools/repository/tools.d.ts +0 -7
  75. package/dist/tools/repository/tools.js +0 -484
  76. package/dist/tools/repository/validation.d.ts +0 -106
  77. package/dist/tools/repository/validation.js +0 -820
  78. package/dist/tools/repository.d.ts +0 -6
  79. package/dist/tools/repository.js +0 -7
  80. package/dist/tools/search.d.ts +0 -76
  81. package/dist/tools/search.js +0 -423
  82. package/dist/types/index.d.ts +0 -2
  83. package/dist/types/index.js +0 -2
  84. package/dist/types/mcp.d.ts +0 -187
  85. package/dist/types/mcp.js +0 -6
  86. package/dist/utils/cache.d.ts +0 -77
  87. package/dist/utils/cache.js +0 -172
  88. package/dist/utils/config.d.ts +0 -70
  89. package/dist/utils/config.js +0 -294
  90. package/dist/utils/errors.d.ts +0 -111
  91. package/dist/utils/errors.js +0 -165
  92. package/dist/utils/health.d.ts +0 -29
  93. package/dist/utils/health.js +0 -132
  94. package/dist/utils/hosted-api.d.ts +0 -67
  95. package/dist/utils/hosted-api.js +0 -119
  96. package/dist/utils/index.d.ts +0 -16
  97. package/dist/utils/index.js +0 -15
  98. package/dist/utils/logger.d.ts +0 -48
  99. package/dist/utils/logger.js +0 -124
  100. package/dist/utils/rate-limit.d.ts +0 -61
  101. package/dist/utils/rate-limit.js +0 -148
  102. package/dist/utils/validation.d.ts +0 -52
  103. package/dist/utils/validation.js +0 -255
@@ -1,423 +0,0 @@
1
- /**
2
- * Compact Language Version Configuration
3
- *
4
- * MAINTAINER: Update these values when Compact language syntax changes!
5
- * See docs/SYNTAX_MAINTENANCE.md for the full update checklist.
6
- */
7
- /**
8
- * Supported Compact language version range
9
- * Update when new compiler versions are released
10
- */
11
- export const COMPACT_VERSION = {
12
- /** Minimum supported version */
13
- min: "0.16",
14
- /** Maximum supported version */
15
- max: "0.18",
16
- /** When this config was last updated */
17
- lastUpdated: "2025-01-26",
18
- /** Source of truth for syntax patterns */
19
- referenceSource: "https://github.com/piotr-iohk/template-contract",
20
- };
21
- /**
22
- * Current pragma format that should be used in contracts
23
- */
24
- export const RECOMMENDED_PRAGMA = `pragma language_version >= ${COMPACT_VERSION.min} && <= ${COMPACT_VERSION.max};`;
25
- /**
26
- * Known deprecated patterns (add new ones here when Compact evolves)
27
- */
28
- export const DEPRECATED_PATTERNS = {
29
- /** Deprecated in: 0.16 */
30
- ledgerBlock: {
31
- pattern: /ledger\s*\{/,
32
- since: "0.16",
33
- replacement: "export ledger fieldName: Type;",
34
- description: "Block-style ledger declarations",
35
- },
36
- /** Deprecated in: 0.15 */
37
- cellWrapper: {
38
- pattern: /Cell\s*<\s*\w+\s*>/,
39
- since: "0.15",
40
- replacement: "Type (without Cell wrapper)",
41
- description: "Cell<T> type wrapper",
42
- },
43
- /** Never existed */
44
- voidType: {
45
- pattern: /:\s*Void\b/,
46
- since: "always",
47
- replacement: "[] (empty tuple)",
48
- description: "Void return type",
49
- },
50
- };
51
- /**
52
- * Reference contracts known to compile successfully
53
- * Use these to verify syntax is still correct
54
- */
55
- export const REFERENCE_CONTRACTS = [
56
- {
57
- name: "template-contract",
58
- repo: "piotr-iohk/template-contract",
59
- description: "Official Midnight template contract",
60
- },
61
- {
62
- name: "tokenomics-project",
63
- repo: "piotr-iohk/tokenomics-project",
64
- description: "Token implementation example",
65
- },
66
- {
67
- name: "zswap-example",
68
- repo: "piotr-iohk/zswap-example",
69
- description: "Privacy-preserving swap example",
70
- },
71
- {
72
- name: "reentrancy-example",
73
- repo: "piotr-iohk/reentrancy-example",
74
- description: "Cross-contract call patterns",
75
- },
76
- ];
77
- /**
78
- * Get the version info as a string for display
79
- */
80
- export function getVersionInfo() {
81
- return `Compact ${COMPACT_VERSION.min}-${COMPACT_VERSION.max} (updated ${COMPACT_VERSION.lastUpdated})`;
82
- }
83
- /**
84
- * Check if a version is within supported range
85
- */
86
- export function isVersionSupported(version) {
87
- const [major, minor] = version.split(".").map(Number);
88
- const [minMajor, minMinor] = COMPACT_VERSION.min.split(".").map(Number);
89
- const [maxMajor, maxMinor] = COMPACT_VERSION.max.split(".").map(Number);
90
- const versionNum = major * 100 + minor;
91
- const minNum = minMajor * 100 + minMinor;
92
- const maxNum = maxMajor * 100 + maxMinor;
93
- return versionNum >= minNum && versionNum <= maxNum;
94
- }
95
- /**
96
- * Built-in functions vs patterns you must implement yourself
97
- * CRITICAL: These are the actual stdlib functions available in Compact
98
- */
99
- export const BUILTIN_FUNCTIONS = {
100
- /** Actually built into the language/stdlib */
101
- stdlib: [
102
- {
103
- name: "persistentHash",
104
- signature: "persistentHash<T>(value: T): Bytes<32>",
105
- description: "Poseidon hash that produces consistent results across calls",
106
- },
107
- {
108
- name: "persistentCommit",
109
- signature: "persistentCommit<T>(value: T): Bytes<32>",
110
- description: "Creates a hiding commitment to a value",
111
- },
112
- {
113
- name: "pad",
114
- signature: "pad(length: number, value: string): Bytes<N>",
115
- description: "Pads a string to fixed-length bytes",
116
- },
117
- {
118
- name: "disclose",
119
- signature: "disclose(value: T): T",
120
- description: "Explicitly reveals a witness value (required in conditionals)",
121
- },
122
- {
123
- name: "assert",
124
- signature: "assert(condition: Boolean, message?: string): []",
125
- description: "Fails circuit if condition is false",
126
- },
127
- {
128
- name: "default",
129
- signature: "default<T>(): T",
130
- description: "Returns default value for a type (0 for numbers, empty for collections)",
131
- },
132
- ],
133
- /** NOT built-in - you must implement these patterns yourself */
134
- notBuiltIn: [
135
- {
136
- name: "public_key",
137
- wrongUsage: "public_key(sk) // ERROR: unbound identifier",
138
- correctPattern: `// Derive public key using persistentHash
139
- const pk = persistentHash<Vector<2, Bytes<32>>>([
140
- pad(32, "midnight:pk:"),
141
- sk
142
- ]);`,
143
- description: "Public key derivation is NOT a builtin - use persistentHash pattern",
144
- },
145
- {
146
- name: "verify_signature",
147
- wrongUsage: "verify_signature(msg, sig, pk) // Does not exist",
148
- correctPattern: `// Signature verification must be done via witnesses
149
- // The prover verifies off-chain, then provides the boolean result
150
- witness signature_valid(): Boolean;`,
151
- description: "Signature verification is done off-chain in the prover",
152
- },
153
- {
154
- name: "random",
155
- wrongUsage: "random() // Does not exist in ZK circuits",
156
- correctPattern: `// Randomness must come from witnesses (prover-provided)
157
- witness get_random_value(): Field;`,
158
- description: "ZK circuits are deterministic - randomness must come from witnesses",
159
- },
160
- ],
161
- };
162
- /**
163
- * Type compatibility rules - what types can be compared/operated together
164
- */
165
- export const TYPE_COMPATIBILITY = {
166
- comparisons: [
167
- { types: "Field == Field", works: true, note: "Direct comparison" },
168
- {
169
- types: "Field == Uint<N>",
170
- works: false,
171
- fix: "Cast with `value as Field`",
172
- },
173
- {
174
- types: "Field >= 0",
175
- works: false,
176
- fix: "Use bounded Uint<0..N> parameter instead",
177
- },
178
- { types: "Uint<N> == Uint<N>", works: true, note: "Same-width comparison" },
179
- {
180
- types: "Uint<0..2> == Uint<0..2>",
181
- works: true,
182
- note: "Bounded integers",
183
- },
184
- { types: "Bytes<32> == Bytes<32>", works: true, note: "Direct comparison" },
185
- { types: "Boolean == Boolean", works: true, note: "Direct comparison" },
186
- ],
187
- arithmetic: [
188
- { types: "Field + Field", works: true, note: "Field arithmetic" },
189
- { types: "Field + Uint<N>", works: false, fix: "Cast Uint to Field first" },
190
- {
191
- types: "Uint<N> + Uint<N>",
192
- works: true,
193
- note: "Result is bounded type, cast back: (a + b) as Uint<64>",
194
- },
195
- {
196
- types: "Uint<64> + Uint<64>",
197
- works: true,
198
- note: "Result is Uint<0..36893488147419103230>, must cast: (a + b) as Uint<64>",
199
- },
200
- {
201
- types: "Uint<64> * Uint<64>",
202
- works: true,
203
- note: "Result is wide bounded type, cast back to target type",
204
- },
205
- ],
206
- typeCasting: [
207
- {
208
- from: "Uint<64>",
209
- to: "Bytes<32>",
210
- direct: false,
211
- fix: "Go through Field: (amount as Field) as Bytes<32>",
212
- },
213
- {
214
- from: "Uint<N>",
215
- to: "Field",
216
- direct: true,
217
- note: "Safe cast: value as Field",
218
- },
219
- {
220
- from: "arithmetic result",
221
- to: "Uint<64>",
222
- direct: true,
223
- note: "Required cast: (a + b) as Uint<64>",
224
- },
225
- ],
226
- assignments: [
227
- {
228
- types: "Field = Uint<N>",
229
- works: false,
230
- fix: "Cast with `value as Field`",
231
- },
232
- {
233
- types: "Uint<N> = Field",
234
- works: false,
235
- fix: "Use bounded param or explicit cast",
236
- },
237
- ],
238
- tips: [
239
- "Use Uint<0..N> for circuit parameters that need range validation",
240
- "Field is unbounded - use for hashes, commitments, general computation",
241
- "Uint<N> is bounded - use when you need range checks",
242
- "Casting with `as Field` is safe but loses range information",
243
- ],
244
- };
245
- /**
246
- * Ledger type limitations - what works in circuits vs TypeScript
247
- */
248
- export const LEDGER_TYPE_LIMITS = {
249
- Counter: {
250
- circuitOperations: [
251
- { method: ".increment(n)", works: true, note: "Adds n to counter" },
252
- {
253
- method: ".decrement(n)",
254
- works: true,
255
- note: "Subtracts n from counter",
256
- },
257
- { method: ".resetToDefault()", works: true, note: "Resets to 0" },
258
- { method: ".value()", works: false, note: "NOT available in circuits" },
259
- ],
260
- typescriptAccess: "Access counter value via `ledgerState.counter` in TypeScript SDK",
261
- reason: "ZK circuits cannot read current ledger state - only modify it",
262
- },
263
- Map: {
264
- circuitOperations: [
265
- {
266
- method: ".insert(key, value)",
267
- works: true,
268
- note: "Adds/updates entry",
269
- },
270
- { method: ".remove(key)", works: true, note: "Removes entry" },
271
- {
272
- method: ".lookup(key)",
273
- works: true,
274
- note: "Returns Option<ValueType> - use in circuits",
275
- },
276
- {
277
- method: ".member(key)",
278
- works: true,
279
- note: "Returns Boolean - checks if key exists",
280
- },
281
- ],
282
- typescriptAccess: "Query map via `contractState.data.get(key)` in TypeScript SDK",
283
- note: "Map.lookup() and Map.member() ARE available in circuits (verified with OpenZeppelin contracts)",
284
- },
285
- Set: {
286
- circuitOperations: [
287
- { method: ".insert(value)", works: true, note: "Adds to set" },
288
- { method: ".remove(value)", works: true, note: "Removes from set" },
289
- {
290
- method: ".member(value)",
291
- works: true,
292
- note: "Returns Boolean - checks if value exists in set",
293
- },
294
- ],
295
- typescriptAccess: "Check membership via `contractState.set.has(value)` in TypeScript SDK",
296
- note: "Set.member() IS available in circuits",
297
- },
298
- MerkleTree: {
299
- circuitOperations: [
300
- { method: ".insert(leaf)", works: true, note: "Adds leaf to tree" },
301
- { method: ".root()", works: false, note: "NOT available in circuits" },
302
- ],
303
- typescriptAccess: "Get root via `contractState.tree.root` in TypeScript SDK",
304
- pattern: `// To verify a merkle proof in circuit:
305
- witness get_merkle_root(): Bytes<32>;
306
- witness get_merkle_proof(leaf: Bytes<32>): Vector<32, Bytes<32>>;
307
-
308
- // Verify proof using persistentHash to compute expected root`,
309
- },
310
- };
311
- /**
312
- * Common compilation errors with their fixes
313
- * Maps actual compiler error messages to solutions
314
- */
315
- export const COMMON_ERRORS = [
316
- {
317
- error: 'unbound identifier "public_key"',
318
- cause: "Trying to use public_key() as if it's a builtin function",
319
- fix: `Use persistentHash pattern instead:
320
- const pk = persistentHash<Vector<2, Bytes<32>>>([pad(32, "midnight:pk:"), sk]);`,
321
- },
322
- {
323
- error: "incompatible combination of types Field and Uint",
324
- cause: "Comparing or operating on Field with Uint without casting",
325
- fix: `Cast Uint to Field: (myUint as Field)
326
- Or use bounded Uint<0..N> for parameters that need constraints`,
327
- },
328
- {
329
- error: 'operation "value" undefined for ledger field type Counter',
330
- cause: "Trying to read Counter.value() inside a circuit",
331
- fix: `Counter values cannot be read in circuits. Options:
332
- 1. Use a witness: witness get_counter_value(): Uint<64>;
333
- 2. Read from TypeScript SDK: ledgerState.counter
334
- 3. Track the value in a separate Field ledger variable`,
335
- },
336
- {
337
- error: "implicit disclosure of witness value",
338
- cause: "Using witness value in conditional without disclose()",
339
- fix: `Wrap witness comparisons in disclose():
340
- if (disclose(witness_value == expected)) { ... }`,
341
- },
342
- {
343
- error: 'parse error: found "{" looking for an identifier',
344
- cause: "Using old ledger { } block syntax",
345
- fix: `Use individual exports instead:
346
- export ledger field1: Type1;
347
- export ledger field2: Type2;`,
348
- },
349
- {
350
- error: 'parse error: found "{" looking for ";"',
351
- cause: "Using Void as return type (doesn't exist)",
352
- fix: `Use empty tuple [] for no return value:
353
- export circuit myCircuit(): [] { ... }`,
354
- },
355
- {
356
- error: 'unbound identifier "Cell"',
357
- cause: "Using deprecated Cell<T> wrapper (removed in 0.15)",
358
- fix: `Remove Cell wrapper, just use the type directly:
359
- export ledger myField: Field; // Not Cell<Field>`,
360
- },
361
- {
362
- error: "member access requires struct type",
363
- cause: "Trying to access a field on a non-struct type",
364
- fix: `Make sure you're accessing a struct field, not a primitive.
365
- Map.lookup() and Map.member() ARE available in circuits.
366
- Check that the base type is actually a struct.`,
367
- },
368
- {
369
- error: "potential witness-value disclosure must be declared",
370
- cause: "Circuit parameter flows to ledger operation without disclose()",
371
- fix: `Disclose parameters at the start of the circuit:
372
- export circuit my_circuit(param: Bytes<32>): [] {
373
- const d_param = disclose(param); // Acknowledge on-chain visibility
374
- ledger.insert(d_param, value); // Now use disclosed value
375
- }`,
376
- },
377
- {
378
- error: "expected second argument of insert to have type Uint<64> but received Uint<0..N>",
379
- cause: "Arithmetic result has bounded type, needs cast back to target",
380
- fix: `Cast arithmetic results back to the target type:
381
- const new_balance = (current + amount) as Uint<64>;
382
- ledger_map.insert(key, new_balance);`,
383
- },
384
- {
385
- error: "cannot cast from type Uint<64> to type Bytes<32>",
386
- cause: "Direct Uint to Bytes cast not allowed",
387
- fix: `Go through Field first:
388
- const amount_field = amount as Field;
389
- const amount_bytes = amount_field as Bytes<32>;
390
- // Or chained: (amount as Field) as Bytes<32>`,
391
- },
392
- {
393
- error: "cannot prove assertion",
394
- cause: "Assert condition cannot be proven true",
395
- fix: `Check your logic. Common causes:
396
- 1. Witness returns unexpected value
397
- 2. Range check fails (use bounded Uint)
398
- 3. Logic error in circuit`,
399
- },
400
- {
401
- error: 'parse error: found ":" looking for ")"',
402
- cause: "Using Rust-style :: for enum variant access",
403
- fix: `Use dot notation for enum variants:
404
- WRONG: Choice::rock, GameState::waiting
405
- CORRECT: Choice.rock, GameState.waiting`,
406
- },
407
- {
408
- error: 'parse error: found "{" after witness declaration',
409
- cause: "Trying to add implementation body to witness",
410
- fix: `Witnesses are declarations only - no body allowed:
411
- WRONG: witness get_caller(): Bytes<32> { return ...; }
412
- CORRECT: witness get_caller(): Bytes<32>;
413
- Implementation goes in TypeScript prover, not Compact.`,
414
- },
415
- {
416
- error: 'unbound identifier "function"',
417
- cause: 'Using "pure function" instead of "pure circuit"',
418
- fix: `Use "pure circuit" for helper functions:
419
- WRONG: pure function helper(...): Type { }
420
- CORRECT: pure circuit helper(...): Type { }`,
421
- },
422
- ];
423
- //# sourceMappingURL=compact-version.js.map
@@ -1,3 +0,0 @@
1
- export { vectorStore } from "./vectorStore.js";
2
- export type { CodeDocument, SearchResult, SearchFilter } from "./vectorStore.js";
3
- //# sourceMappingURL=index.d.ts.map
package/dist/db/index.js DELETED
@@ -1,2 +0,0 @@
1
- export { vectorStore } from "./vectorStore.js";
2
- //# sourceMappingURL=index.js.map
@@ -1,69 +0,0 @@
1
- export interface CodeDocument {
2
- id: string;
3
- content: string;
4
- embedding?: number[];
5
- metadata: {
6
- repository: string;
7
- filePath: string;
8
- language: string;
9
- startLine: number;
10
- endLine: number;
11
- codeType: string;
12
- codeName: string;
13
- isPublic: boolean;
14
- repoVersion?: string;
15
- pragmaVersion?: string;
16
- indexedAt?: string;
17
- };
18
- }
19
- export interface SearchResult {
20
- id: string;
21
- content: string;
22
- score: number;
23
- metadata: CodeDocument["metadata"];
24
- }
25
- export interface SearchFilter {
26
- repository?: string;
27
- language?: string;
28
- codeType?: string;
29
- isPublic?: boolean;
30
- }
31
- declare class VectorStore {
32
- private client;
33
- private collection;
34
- private collectionName;
35
- private initialized;
36
- /**
37
- * Initialize the vector store connection
38
- */
39
- initialize(): Promise<void>;
40
- /**
41
- * Add documents to the vector store
42
- */
43
- addDocuments(documents: CodeDocument[]): Promise<void>;
44
- /**
45
- * Search for similar documents
46
- */
47
- search(query: string, limit?: number, filter?: SearchFilter): Promise<SearchResult[]>;
48
- /**
49
- * Delete documents by file path
50
- */
51
- deleteByPath(repository: string, filePath: string): Promise<void>;
52
- /**
53
- * Delete all documents for a repository
54
- */
55
- deleteRepository(repository: string): Promise<void>;
56
- /**
57
- * Get collection statistics
58
- */
59
- getStats(): Promise<{
60
- count: number;
61
- }>;
62
- /**
63
- * Clear all data from the collection
64
- */
65
- clear(): Promise<void>;
66
- }
67
- export declare const vectorStore: VectorStore;
68
- export {};
69
- //# sourceMappingURL=vectorStore.d.ts.map
@@ -1,196 +0,0 @@
1
- import { ChromaClient } from "chromadb";
2
- import { config, logger } from "../utils/index.js";
3
- import { embeddingGenerator } from "../pipeline/embeddings.js";
4
- class VectorStore {
5
- client = null;
6
- collection = null;
7
- collectionName = "midnight-code";
8
- initialized = false;
9
- /**
10
- * Initialize the vector store connection
11
- */
12
- async initialize() {
13
- if (this.initialized)
14
- return;
15
- try {
16
- this.client = new ChromaClient({
17
- path: config.chromaUrl,
18
- });
19
- // Get or create the collection
20
- this.collection = await this.client.getOrCreateCollection({
21
- name: this.collectionName,
22
- metadata: {
23
- description: "Midnight blockchain code and documentation",
24
- },
25
- });
26
- this.initialized = true;
27
- logger.info("Vector store initialized successfully");
28
- }
29
- catch (error) {
30
- logger.error("Failed to initialize vector store", {
31
- error: String(error),
32
- });
33
- // Continue without vector store - use fallback search
34
- logger.warn("Vector store unavailable, using in-memory fallback");
35
- }
36
- }
37
- /**
38
- * Add documents to the vector store
39
- */
40
- async addDocuments(documents) {
41
- if (!this.collection) {
42
- logger.warn("Vector store not initialized, skipping document storage");
43
- return;
44
- }
45
- try {
46
- const ids = documents.map((d) => d.id);
47
- const embeddings = documents.map((d) => d.embedding);
48
- const metadatas = documents.map((d) => ({
49
- repository: d.metadata.repository,
50
- filePath: d.metadata.filePath,
51
- language: d.metadata.language,
52
- startLine: d.metadata.startLine,
53
- endLine: d.metadata.endLine,
54
- codeType: d.metadata.codeType,
55
- codeName: d.metadata.codeName,
56
- isPublic: d.metadata.isPublic,
57
- }));
58
- const documentContents = documents.map((d) => d.content);
59
- await this.collection.add({
60
- ids,
61
- embeddings,
62
- metadatas,
63
- documents: documentContents,
64
- });
65
- logger.debug(`Added ${documents.length} documents to vector store`);
66
- }
67
- catch (error) {
68
- logger.error("Failed to add documents to vector store", {
69
- error: String(error),
70
- });
71
- throw error;
72
- }
73
- }
74
- /**
75
- * Search for similar documents
76
- */
77
- async search(query, limit = 10, filter) {
78
- if (!this.collection) {
79
- logger.warn("Vector store not initialized, returning empty results");
80
- return [];
81
- }
82
- try {
83
- // Generate embedding for the query
84
- const queryEmbedding = await embeddingGenerator.generateEmbedding(query);
85
- // Build where filter
86
- const whereFilter = {};
87
- if (filter?.repository) {
88
- whereFilter.repository = filter.repository;
89
- }
90
- if (filter?.language) {
91
- whereFilter.language = filter.language;
92
- }
93
- if (filter?.codeType) {
94
- whereFilter.codeType = filter.codeType;
95
- }
96
- if (filter?.isPublic !== undefined) {
97
- whereFilter.isPublic = filter.isPublic;
98
- }
99
- const results = await this.collection.query({
100
- queryEmbeddings: [queryEmbedding.embedding],
101
- nResults: limit,
102
- where: Object.keys(whereFilter).length > 0 ? whereFilter : undefined,
103
- });
104
- // Format results
105
- const searchResults = [];
106
- if (results.ids[0] && results.documents[0] && results.metadatas[0]) {
107
- for (let i = 0; i < results.ids[0].length; i++) {
108
- const metadata = results.metadatas[0][i];
109
- searchResults.push({
110
- id: results.ids[0][i],
111
- content: results.documents[0][i] || "",
112
- score: results.distances ? 1 - (results.distances[0][i] || 0) : 0,
113
- metadata,
114
- });
115
- }
116
- }
117
- return searchResults;
118
- }
119
- catch (error) {
120
- logger.error("Search failed", { error: String(error) });
121
- return [];
122
- }
123
- }
124
- /**
125
- * Delete documents by file path
126
- */
127
- async deleteByPath(repository, filePath) {
128
- if (!this.collection)
129
- return;
130
- try {
131
- await this.collection.delete({
132
- where: {
133
- repository,
134
- filePath,
135
- },
136
- });
137
- logger.debug(`Deleted documents for ${repository}:${filePath}`);
138
- }
139
- catch (error) {
140
- logger.error("Failed to delete documents", { error: String(error) });
141
- }
142
- }
143
- /**
144
- * Delete all documents for a repository
145
- */
146
- async deleteRepository(repository) {
147
- if (!this.collection)
148
- return;
149
- try {
150
- await this.collection.delete({
151
- where: { repository },
152
- });
153
- logger.info(`Deleted all documents for repository ${repository}`);
154
- }
155
- catch (error) {
156
- logger.error("Failed to delete repository documents", {
157
- error: String(error),
158
- });
159
- }
160
- }
161
- /**
162
- * Get collection statistics
163
- */
164
- async getStats() {
165
- if (!this.collection) {
166
- return { count: 0 };
167
- }
168
- try {
169
- const count = await this.collection.count();
170
- return { count };
171
- }
172
- catch (error) {
173
- logger.error("Failed to get stats", { error: String(error) });
174
- return { count: 0 };
175
- }
176
- }
177
- /**
178
- * Clear all data from the collection
179
- */
180
- async clear() {
181
- if (!this.client)
182
- return;
183
- try {
184
- await this.client.deleteCollection({ name: this.collectionName });
185
- this.collection = await this.client.getOrCreateCollection({
186
- name: this.collectionName,
187
- });
188
- logger.info("Vector store cleared");
189
- }
190
- catch (error) {
191
- logger.error("Failed to clear vector store", { error: String(error) });
192
- }
193
- }
194
- }
195
- export const vectorStore = new VectorStore();
196
- //# sourceMappingURL=vectorStore.js.map