@sparkleideas/plugins 3.0.0-alpha.10

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 (80) hide show
  1. package/README.md +401 -0
  2. package/__tests__/collection-manager.test.ts +332 -0
  3. package/__tests__/dependency-graph.test.ts +434 -0
  4. package/__tests__/enhanced-plugin-registry.test.ts +488 -0
  5. package/__tests__/plugin-registry.test.ts +368 -0
  6. package/__tests__/ruvector-bridge.test.ts +2429 -0
  7. package/__tests__/ruvector-integration.test.ts +1602 -0
  8. package/__tests__/ruvector-migrations.test.ts +1099 -0
  9. package/__tests__/ruvector-quantization.test.ts +846 -0
  10. package/__tests__/ruvector-streaming.test.ts +1088 -0
  11. package/__tests__/sdk.test.ts +325 -0
  12. package/__tests__/security.test.ts +348 -0
  13. package/__tests__/utils/ruvector-test-utils.ts +860 -0
  14. package/examples/plugin-creator/index.ts +636 -0
  15. package/examples/plugin-creator/plugin-creator.test.ts +312 -0
  16. package/examples/ruvector/README.md +288 -0
  17. package/examples/ruvector/attention-patterns.ts +394 -0
  18. package/examples/ruvector/basic-usage.ts +288 -0
  19. package/examples/ruvector/docker-compose.yml +75 -0
  20. package/examples/ruvector/gnn-analysis.ts +501 -0
  21. package/examples/ruvector/hyperbolic-hierarchies.ts +557 -0
  22. package/examples/ruvector/init-db.sql +119 -0
  23. package/examples/ruvector/quantization.ts +680 -0
  24. package/examples/ruvector/self-learning.ts +447 -0
  25. package/examples/ruvector/semantic-search.ts +576 -0
  26. package/examples/ruvector/streaming-large-data.ts +507 -0
  27. package/examples/ruvector/transactions.ts +594 -0
  28. package/examples/ruvector-plugins/hook-pattern-library.ts +486 -0
  29. package/examples/ruvector-plugins/index.ts +79 -0
  30. package/examples/ruvector-plugins/intent-router.ts +354 -0
  31. package/examples/ruvector-plugins/mcp-tool-optimizer.ts +424 -0
  32. package/examples/ruvector-plugins/reasoning-bank.ts +657 -0
  33. package/examples/ruvector-plugins/ruvector-plugins.test.ts +518 -0
  34. package/examples/ruvector-plugins/semantic-code-search.ts +498 -0
  35. package/examples/ruvector-plugins/shared/index.ts +20 -0
  36. package/examples/ruvector-plugins/shared/vector-utils.ts +257 -0
  37. package/examples/ruvector-plugins/sona-learning.ts +445 -0
  38. package/package.json +97 -0
  39. package/src/collections/collection-manager.ts +661 -0
  40. package/src/collections/index.ts +56 -0
  41. package/src/collections/official/index.ts +1040 -0
  42. package/src/core/base-plugin.ts +416 -0
  43. package/src/core/plugin-interface.ts +215 -0
  44. package/src/hooks/index.ts +685 -0
  45. package/src/index.ts +378 -0
  46. package/src/integrations/agentic-flow.ts +743 -0
  47. package/src/integrations/index.ts +88 -0
  48. package/src/integrations/ruvector/ARCHITECTURE.md +1245 -0
  49. package/src/integrations/ruvector/attention-advanced.ts +1040 -0
  50. package/src/integrations/ruvector/attention-executor.ts +782 -0
  51. package/src/integrations/ruvector/attention-mechanisms.ts +757 -0
  52. package/src/integrations/ruvector/attention.ts +1063 -0
  53. package/src/integrations/ruvector/gnn.ts +3050 -0
  54. package/src/integrations/ruvector/hyperbolic.ts +1948 -0
  55. package/src/integrations/ruvector/index.ts +394 -0
  56. package/src/integrations/ruvector/migrations/001_create_extension.sql +135 -0
  57. package/src/integrations/ruvector/migrations/002_create_vector_tables.sql +259 -0
  58. package/src/integrations/ruvector/migrations/003_create_indices.sql +328 -0
  59. package/src/integrations/ruvector/migrations/004_create_functions.sql +598 -0
  60. package/src/integrations/ruvector/migrations/005_create_attention_functions.sql +654 -0
  61. package/src/integrations/ruvector/migrations/006_create_gnn_functions.sql +728 -0
  62. package/src/integrations/ruvector/migrations/007_create_hyperbolic_functions.sql +762 -0
  63. package/src/integrations/ruvector/migrations/index.ts +35 -0
  64. package/src/integrations/ruvector/migrations/migrations.ts +647 -0
  65. package/src/integrations/ruvector/quantization.ts +2036 -0
  66. package/src/integrations/ruvector/ruvector-bridge.ts +2000 -0
  67. package/src/integrations/ruvector/self-learning.ts +2376 -0
  68. package/src/integrations/ruvector/streaming.ts +1737 -0
  69. package/src/integrations/ruvector/types.ts +1945 -0
  70. package/src/providers/index.ts +643 -0
  71. package/src/registry/dependency-graph.ts +568 -0
  72. package/src/registry/enhanced-plugin-registry.ts +994 -0
  73. package/src/registry/plugin-registry.ts +604 -0
  74. package/src/sdk/index.ts +563 -0
  75. package/src/security/index.ts +594 -0
  76. package/src/types/index.ts +446 -0
  77. package/src/workers/index.ts +700 -0
  78. package/tmp.json +0 -0
  79. package/tsconfig.json +25 -0
  80. package/vitest.config.ts +23 -0
@@ -0,0 +1,782 @@
1
+ /**
2
+ * RuVector PostgreSQL Bridge - Attention Executor and Factory
3
+ *
4
+ * Provides execution layer and factory for attention mechanisms.
5
+ *
6
+ * @module @sparkleideas/plugins/integrations/ruvector/attention-executor
7
+ */
8
+
9
+ import type {
10
+ AttentionMechanism,
11
+ AttentionConfig,
12
+ AttentionInput,
13
+ AttentionOutput,
14
+ AttentionStats,
15
+ } from './types.js';
16
+
17
+ import {
18
+ AttentionRegistry,
19
+ type IAttentionMechanism,
20
+ type AttentionOptions,
21
+ type AttentionCategory,
22
+ MultiHeadAttention,
23
+ SelfAttention,
24
+ CrossAttention,
25
+ CausalAttention,
26
+ BidirectionalAttention,
27
+ LocalAttention,
28
+ GlobalAttention,
29
+ FlashAttention,
30
+ FlashAttentionV2,
31
+ MemoryEfficientAttention,
32
+ ChunkAttention,
33
+ SlidingWindowAttention,
34
+ DilatedAttention,
35
+ } from './attention.js';
36
+
37
+ import {
38
+ SparseAttention,
39
+ BlockSparseAttention,
40
+ LinearAttention,
41
+ PerformerAttention,
42
+ LinformerAttention,
43
+ ReformerAttention,
44
+ RelativePositionAttention,
45
+ RotaryPositionAttention,
46
+ ALiBiAttention,
47
+ AxialAttention,
48
+ } from './attention-mechanisms.js';
49
+
50
+ import {
51
+ GraphAttention,
52
+ HyperbolicAttention,
53
+ SphericalAttention,
54
+ ToroidalAttention,
55
+ TemporalAttention,
56
+ RecurrentAttention,
57
+ StateSpaceAttention,
58
+ CrossModalAttention,
59
+ PerceiverAttention,
60
+ FlamingoAttention,
61
+ RetrievalAttention,
62
+ KNNAttention,
63
+ MemoryAugmentedAttention,
64
+ SynthesizerAttention,
65
+ RoutingAttention,
66
+ MixtureOfExpertsAttention,
67
+ } from './attention-advanced.js';
68
+
69
+ // ============================================================================
70
+ // Attention Executor
71
+ // ============================================================================
72
+
73
+ /**
74
+ * Options for attention execution.
75
+ */
76
+ export interface ExecutionOptions {
77
+ /** Whether to generate SQL only (dry run) */
78
+ dryRun?: boolean;
79
+ /** Whether to collect statistics */
80
+ collectStats?: boolean;
81
+ /** Timeout in milliseconds */
82
+ timeoutMs?: number;
83
+ /** Whether to cache results */
84
+ cacheResults?: boolean;
85
+ /** Cache key prefix */
86
+ cacheKeyPrefix?: string;
87
+ }
88
+
89
+ /**
90
+ * Result from attention execution.
91
+ */
92
+ export interface ExecutionResult {
93
+ /** Output vectors */
94
+ output: number[][];
95
+ /** Attention weights (if requested) */
96
+ attentionWeights?: number[][][][];
97
+ /** Generated SQL query */
98
+ sql?: string;
99
+ /** Execution statistics */
100
+ stats?: AttentionStats;
101
+ }
102
+
103
+ /**
104
+ * Executes attention operations via PostgreSQL RuVector functions.
105
+ */
106
+ export class AttentionExecutor {
107
+ private registry: AttentionRegistry;
108
+ private defaultOptions: ExecutionOptions;
109
+ private queryExecutor: ((sql: string) => Promise<unknown>) | null = null;
110
+
111
+ constructor(registry?: AttentionRegistry, options?: ExecutionOptions) {
112
+ this.registry = registry ?? createDefaultRegistry();
113
+ this.defaultOptions = {
114
+ dryRun: false,
115
+ collectStats: true,
116
+ timeoutMs: 30000,
117
+ cacheResults: false,
118
+ ...options,
119
+ };
120
+ }
121
+
122
+ /**
123
+ * Set the query executor for database operations.
124
+ */
125
+ setQueryExecutor(executor: (sql: string) => Promise<unknown>): void {
126
+ this.queryExecutor = executor;
127
+ }
128
+
129
+ /**
130
+ * Execute attention operation.
131
+ */
132
+ async execute(
133
+ mechanism: AttentionMechanism,
134
+ input: AttentionInput,
135
+ options?: ExecutionOptions
136
+ ): Promise<ExecutionResult> {
137
+ const opts = { ...this.defaultOptions, ...options };
138
+ const impl = this.registry.get(mechanism);
139
+ const startTime = Date.now();
140
+
141
+ // Generate SQL
142
+ const sql = impl.toSQL(input);
143
+
144
+ if (opts.dryRun) {
145
+ return {
146
+ output: [],
147
+ sql,
148
+ stats: this.createStats(startTime, input, true),
149
+ };
150
+ }
151
+
152
+ // Execute via database or local computation
153
+ let output: number[][];
154
+
155
+ if (this.queryExecutor) {
156
+ // Execute via PostgreSQL
157
+ const result = await this.executeSQL(sql, opts.timeoutMs);
158
+ output = this.parseResult(result);
159
+ } else {
160
+ // Local computation fallback
161
+ output = await impl.computeBatch(
162
+ input.query as number[][],
163
+ input.key as number[][],
164
+ input.value as number[][]
165
+ );
166
+ }
167
+
168
+ return {
169
+ output,
170
+ sql,
171
+ stats: opts.collectStats ? this.createStats(startTime, input, false) : undefined,
172
+ };
173
+ }
174
+
175
+ /**
176
+ * Execute batch of attention operations.
177
+ */
178
+ async executeBatch(
179
+ mechanism: AttentionMechanism,
180
+ inputs: AttentionInput[],
181
+ options?: ExecutionOptions
182
+ ): Promise<ExecutionResult[]> {
183
+ return Promise.all(inputs.map(input => this.execute(mechanism, input, options)));
184
+ }
185
+
186
+ /**
187
+ * Execute with multiple mechanisms and combine results.
188
+ */
189
+ async executeMultiple(
190
+ mechanisms: AttentionMechanism[],
191
+ input: AttentionInput,
192
+ combineMethod: 'average' | 'concat' | 'weighted' = 'average',
193
+ weights?: number[],
194
+ options?: ExecutionOptions
195
+ ): Promise<ExecutionResult> {
196
+ const results = await Promise.all(
197
+ mechanisms.map(m => this.execute(m, input, options))
198
+ );
199
+
200
+ const outputs = results.map(r => r.output);
201
+
202
+ let combinedOutput: number[][];
203
+ switch (combineMethod) {
204
+ case 'concat':
205
+ combinedOutput = outputs[0].map((_, i) =>
206
+ outputs.flatMap(o => o[i])
207
+ );
208
+ break;
209
+ case 'weighted':
210
+ const w = weights ?? mechanisms.map(() => 1 / mechanisms.length);
211
+ combinedOutput = outputs[0].map((_, i) =>
212
+ outputs[0][i].map((_, j) =>
213
+ outputs.reduce((sum, o, k) => sum + w[k] * o[i][j], 0)
214
+ )
215
+ );
216
+ break;
217
+ case 'average':
218
+ default:
219
+ combinedOutput = outputs[0].map((_, i) =>
220
+ outputs[0][i].map((_, j) =>
221
+ outputs.reduce((sum, o) => sum + o[i][j], 0) / outputs.length
222
+ )
223
+ );
224
+ }
225
+
226
+ return {
227
+ output: combinedOutput,
228
+ sql: results.map(r => r.sql).join(';\n'),
229
+ stats: results[0].stats,
230
+ };
231
+ }
232
+
233
+ /**
234
+ * Generate SQL without execution.
235
+ */
236
+ generateSQL(mechanism: AttentionMechanism, input: AttentionInput): string {
237
+ return this.registry.get(mechanism).toSQL(input);
238
+ }
239
+
240
+ /**
241
+ * Generate batch SQL.
242
+ */
243
+ generateBatchSQL(mechanism: AttentionMechanism, inputs: AttentionInput[]): string {
244
+ const sqls = inputs.map(input => this.generateSQL(mechanism, input));
245
+ return `WITH batch_attention AS (\n ${sqls.join(',\n ')}\n)\nSELECT * FROM batch_attention;`;
246
+ }
247
+
248
+ private async executeSQL(sql: string, timeoutMs?: number): Promise<unknown> {
249
+ if (!this.queryExecutor) {
250
+ throw new Error('No query executor configured');
251
+ }
252
+
253
+ if (timeoutMs) {
254
+ return Promise.race([
255
+ this.queryExecutor(sql),
256
+ new Promise((_, reject) =>
257
+ setTimeout(() => reject(new Error('Query timeout')), timeoutMs)
258
+ ),
259
+ ]);
260
+ }
261
+
262
+ return this.queryExecutor(sql);
263
+ }
264
+
265
+ private parseResult(result: unknown): number[][] {
266
+ if (Array.isArray(result)) {
267
+ return result.map(row => {
268
+ if (Array.isArray(row)) return row;
269
+ if (typeof row === 'object' && row !== null) {
270
+ const values = Object.values(row);
271
+ if (values.length === 1 && Array.isArray(values[0])) {
272
+ return values[0] as number[];
273
+ }
274
+ }
275
+ return [];
276
+ });
277
+ }
278
+ return [];
279
+ }
280
+
281
+ private createStats(startTime: number, input: AttentionInput, dryRun: boolean): AttentionStats {
282
+ const seqLen = input.query.length;
283
+ const dim = (input.query[0] as number[]).length;
284
+
285
+ return {
286
+ computeTimeMs: dryRun ? 0 : Date.now() - startTime,
287
+ memoryBytes: seqLen * dim * 4 * 3, // Approximate: Q, K, V as float32
288
+ tokensProcessed: seqLen,
289
+ flops: seqLen * seqLen * dim * 2, // Approximate FLOPs for attention
290
+ };
291
+ }
292
+ }
293
+
294
+ // ============================================================================
295
+ // Attention Factory
296
+ // ============================================================================
297
+
298
+ /**
299
+ * Factory for creating configured attention instances.
300
+ */
301
+ export class AttentionFactory {
302
+ private registry: AttentionRegistry;
303
+ private defaults: Partial<AttentionConfig>;
304
+
305
+ constructor(registry?: AttentionRegistry, defaults?: Partial<AttentionConfig>) {
306
+ this.registry = registry ?? createDefaultRegistry();
307
+ this.defaults = {
308
+ numHeads: 8,
309
+ headDim: 64,
310
+ embedDim: 512,
311
+ dropout: 0.0,
312
+ useBias: true,
313
+ causal: false,
314
+ maxSeqLen: 2048,
315
+ ...defaults,
316
+ };
317
+ }
318
+
319
+ /**
320
+ * Create an attention mechanism instance.
321
+ */
322
+ create(type: AttentionMechanism, config?: Partial<AttentionConfig>): IAttentionMechanism {
323
+ const mechanism = this.registry.get(type);
324
+ mechanism.configure({
325
+ ...this.defaults,
326
+ ...config,
327
+ } as AttentionOptions);
328
+ return mechanism;
329
+ }
330
+
331
+ /**
332
+ * Create multiple attention mechanisms.
333
+ */
334
+ createMultiple(
335
+ types: AttentionMechanism[],
336
+ configs?: Partial<AttentionConfig>[]
337
+ ): Map<AttentionMechanism, IAttentionMechanism> {
338
+ const result = new Map<AttentionMechanism, IAttentionMechanism>();
339
+ types.forEach((type, i) => {
340
+ result.set(type, this.create(type, configs?.[i]));
341
+ });
342
+ return result;
343
+ }
344
+
345
+ /**
346
+ * Create attention mechanisms by category.
347
+ */
348
+ createByCategory(
349
+ category: AttentionCategory,
350
+ config?: Partial<AttentionConfig>
351
+ ): Map<AttentionMechanism, IAttentionMechanism> {
352
+ const types = this.registry.listByCategory(category);
353
+ return this.createMultiple(types, types.map(() => config ?? {}));
354
+ }
355
+
356
+ /**
357
+ * Create an attention stack (multiple mechanisms in sequence).
358
+ */
359
+ createStack(
360
+ types: AttentionMechanism[],
361
+ config?: Partial<AttentionConfig>
362
+ ): AttentionStack {
363
+ const mechanisms = types.map(t => this.create(t, config));
364
+ return new AttentionStack(mechanisms);
365
+ }
366
+
367
+ /**
368
+ * Create optimal attention for given constraints.
369
+ */
370
+ createOptimal(constraints: {
371
+ maxSeqLen: number;
372
+ maxMemoryMB?: number;
373
+ preferSpeed?: boolean;
374
+ preferAccuracy?: boolean;
375
+ }): IAttentionMechanism {
376
+ const { maxSeqLen, maxMemoryMB, preferSpeed, preferAccuracy } = constraints;
377
+
378
+ // Decision logic for optimal attention selection
379
+ if (maxSeqLen > 8192) {
380
+ // Very long sequences
381
+ if (preferSpeed) {
382
+ return this.create('linformer');
383
+ } else if (preferAccuracy) {
384
+ return this.create('sparse_attention');
385
+ }
386
+ return this.create('flash_attention_v2');
387
+ } else if (maxSeqLen > 2048) {
388
+ // Long sequences
389
+ if (maxMemoryMB && maxMemoryMB < 1024) {
390
+ return this.create('flash_attention');
391
+ }
392
+ return this.create('sliding_window');
393
+ } else if (maxSeqLen > 512) {
394
+ // Medium sequences
395
+ if (preferSpeed) {
396
+ return this.create('linear_attention');
397
+ }
398
+ return this.create('multi_head');
399
+ } else {
400
+ // Short sequences
401
+ return this.create('multi_head');
402
+ }
403
+ }
404
+
405
+ /**
406
+ * Get available mechanisms.
407
+ */
408
+ getAvailable(): AttentionMechanism[] {
409
+ return this.registry.listAvailable();
410
+ }
411
+
412
+ /**
413
+ * Get mechanism metadata.
414
+ */
415
+ getMechanismInfo(type: AttentionMechanism): {
416
+ name: string;
417
+ description: string;
418
+ category: AttentionCategory;
419
+ } {
420
+ const impl = this.registry.get(type);
421
+ return {
422
+ name: impl.name,
423
+ description: impl.description,
424
+ category: impl.category,
425
+ };
426
+ }
427
+
428
+ /**
429
+ * Set default configuration.
430
+ */
431
+ setDefaults(config: Partial<AttentionConfig>): void {
432
+ this.defaults = { ...this.defaults, ...config };
433
+ }
434
+ }
435
+
436
+ // ============================================================================
437
+ // Attention Stack
438
+ // ============================================================================
439
+
440
+ /**
441
+ * Stack of attention mechanisms applied in sequence.
442
+ */
443
+ export class AttentionStack {
444
+ private mechanisms: IAttentionMechanism[];
445
+
446
+ constructor(mechanisms: IAttentionMechanism[]) {
447
+ this.mechanisms = mechanisms;
448
+ }
449
+
450
+ /**
451
+ * Compute attention through the stack.
452
+ */
453
+ async compute(
454
+ query: number[],
455
+ keys: number[][],
456
+ values: number[][]
457
+ ): Promise<number[]> {
458
+ let current = query;
459
+ for (const mechanism of this.mechanisms) {
460
+ current = await mechanism.compute(current, keys, values);
461
+ }
462
+ return current;
463
+ }
464
+
465
+ /**
466
+ * Compute batch through the stack.
467
+ */
468
+ async computeBatch(
469
+ queries: number[][],
470
+ keys: number[][],
471
+ values: number[][]
472
+ ): Promise<number[][]> {
473
+ let current = queries;
474
+ for (const mechanism of this.mechanisms) {
475
+ current = await mechanism.computeBatch(current, keys, values);
476
+ }
477
+ return current;
478
+ }
479
+
480
+ /**
481
+ * Get the mechanisms in the stack.
482
+ */
483
+ getMechanisms(): IAttentionMechanism[] {
484
+ return [...this.mechanisms];
485
+ }
486
+
487
+ /**
488
+ * Add a mechanism to the stack.
489
+ */
490
+ push(mechanism: IAttentionMechanism): void {
491
+ this.mechanisms.push(mechanism);
492
+ }
493
+
494
+ /**
495
+ * Remove the last mechanism.
496
+ */
497
+ pop(): IAttentionMechanism | undefined {
498
+ return this.mechanisms.pop();
499
+ }
500
+ }
501
+
502
+ // ============================================================================
503
+ // Default Registry Creation
504
+ // ============================================================================
505
+
506
+ /**
507
+ * Create a registry with all 39 attention mechanisms.
508
+ */
509
+ export function createDefaultRegistry(): AttentionRegistry {
510
+ const registry = new AttentionRegistry();
511
+
512
+ // Core mechanisms
513
+ registry.register(new MultiHeadAttention());
514
+ registry.register(new SelfAttention());
515
+ registry.register(new CrossAttention());
516
+ registry.register(new CausalAttention());
517
+ registry.register(new BidirectionalAttention());
518
+ registry.register(new LocalAttention());
519
+ registry.register(new GlobalAttention());
520
+
521
+ // Efficient mechanisms
522
+ registry.register(new FlashAttention());
523
+ registry.register(new FlashAttentionV2());
524
+ registry.register(new MemoryEfficientAttention());
525
+ registry.register(new ChunkAttention());
526
+ registry.register(new SlidingWindowAttention());
527
+ registry.register(new DilatedAttention());
528
+
529
+ // Sparse mechanisms
530
+ registry.register(new SparseAttention());
531
+ registry.register(new BlockSparseAttention());
532
+
533
+ // Linear mechanisms
534
+ registry.register(new LinearAttention());
535
+ registry.register(new PerformerAttention());
536
+ registry.register(new LinformerAttention());
537
+ registry.register(new ReformerAttention());
538
+ registry.register(new SynthesizerAttention());
539
+ registry.register(new RoutingAttention());
540
+ registry.register(new MixtureOfExpertsAttention());
541
+
542
+ // Positional mechanisms
543
+ registry.register(new RelativePositionAttention());
544
+ registry.register(new RotaryPositionAttention());
545
+ registry.register(new ALiBiAttention());
546
+ registry.register(new AxialAttention());
547
+
548
+ // Graph mechanisms
549
+ registry.register(new GraphAttention());
550
+ registry.register(new HyperbolicAttention());
551
+ registry.register(new SphericalAttention());
552
+ registry.register(new ToroidalAttention());
553
+
554
+ // Temporal mechanisms
555
+ registry.register(new TemporalAttention());
556
+ registry.register(new RecurrentAttention());
557
+ registry.register(new StateSpaceAttention());
558
+
559
+ // Multimodal mechanisms
560
+ registry.register(new CrossModalAttention());
561
+ registry.register(new PerceiverAttention());
562
+ registry.register(new FlamingoAttention());
563
+
564
+ // Retrieval mechanisms
565
+ registry.register(new RetrievalAttention());
566
+ registry.register(new KNNAttention());
567
+ registry.register(new MemoryAugmentedAttention());
568
+
569
+ return registry;
570
+ }
571
+
572
+ // ============================================================================
573
+ // SQL Query Builder
574
+ // ============================================================================
575
+
576
+ /**
577
+ * Builds optimized SQL queries for attention operations.
578
+ */
579
+ export class AttentionSQLBuilder {
580
+ private schema: string;
581
+
582
+ constructor(schema: string = 'ruvector') {
583
+ this.schema = schema;
584
+ }
585
+
586
+ /**
587
+ * Build a complete attention query with setup and execution.
588
+ */
589
+ buildComplete(
590
+ mechanism: AttentionMechanism,
591
+ tableName: string,
592
+ queryColumn: string,
593
+ keyColumn: string,
594
+ valueColumn: string,
595
+ options: {
596
+ numHeads?: number;
597
+ scale?: number;
598
+ causal?: boolean;
599
+ limit?: number;
600
+ } = {}
601
+ ): string {
602
+ const { numHeads = 8, scale, causal = false, limit = 100 } = options;
603
+ const computedScale = scale ?? Math.sqrt(64);
604
+
605
+ return `
606
+ -- Set RuVector parameters
607
+ SET ${this.schema}.attention_num_heads = ${numHeads};
608
+ SET ${this.schema}.attention_scale = ${computedScale};
609
+ SET ${this.schema}.attention_causal = ${causal};
610
+
611
+ -- Execute attention
612
+ SELECT
613
+ id,
614
+ ${this.schema}.${this.mapMechanismToFunction(mechanism)}(
615
+ ${queryColumn},
616
+ ARRAY_AGG(${keyColumn}) OVER (ORDER BY id),
617
+ ARRAY_AGG(${valueColumn}) OVER (ORDER BY id)
618
+ ) AS attention_output
619
+ FROM ${tableName}
620
+ LIMIT ${limit};
621
+ `.trim();
622
+ }
623
+
624
+ /**
625
+ * Build batch attention query.
626
+ */
627
+ buildBatch(
628
+ mechanism: AttentionMechanism,
629
+ queries: string,
630
+ keys: string,
631
+ values: string,
632
+ options: {
633
+ numHeads?: number;
634
+ scale?: number;
635
+ } = {}
636
+ ): string {
637
+ const { numHeads = 8, scale } = options;
638
+ const computedScale = scale ?? Math.sqrt(64);
639
+
640
+ return `
641
+ SELECT ${this.schema}.${this.mapMechanismToFunction(mechanism)}_batch(
642
+ ${queries}::vector[],
643
+ ${keys}::vector[],
644
+ ${values}::vector[],
645
+ ${numHeads},
646
+ ${computedScale}
647
+ ) AS attention_outputs;
648
+ `.trim();
649
+ }
650
+
651
+ /**
652
+ * Build attention with retrieved context.
653
+ */
654
+ buildWithRetrieval(
655
+ mechanism: AttentionMechanism,
656
+ queryVector: string,
657
+ tableName: string,
658
+ vectorColumn: string,
659
+ k: number = 10
660
+ ): string {
661
+ return `
662
+ WITH retrieved AS (
663
+ SELECT
664
+ ${vectorColumn} as key_vector,
665
+ ${vectorColumn} as value_vector
666
+ FROM ${tableName}
667
+ ORDER BY ${vectorColumn} <-> ${queryVector}
668
+ LIMIT ${k}
669
+ )
670
+ SELECT ${this.schema}.${this.mapMechanismToFunction(mechanism)}(
671
+ ${queryVector},
672
+ ARRAY(SELECT key_vector FROM retrieved),
673
+ ARRAY(SELECT value_vector FROM retrieved)
674
+ ) AS attention_output;
675
+ `.trim();
676
+ }
677
+
678
+ private mapMechanismToFunction(mechanism: AttentionMechanism): string {
679
+ const mapping: Record<AttentionMechanism, string> = {
680
+ 'multi_head': 'multi_head_attention',
681
+ 'self_attention': 'self_attention',
682
+ 'cross_attention': 'cross_attention',
683
+ 'sparse_attention': 'sparse_attention',
684
+ 'linear_attention': 'linear_attention',
685
+ 'local_attention': 'local_attention',
686
+ 'global_attention': 'global_attention',
687
+ 'flash_attention': 'flash_attention',
688
+ 'flash_attention_v2': 'flash_attention_v2',
689
+ 'memory_efficient': 'memory_efficient_attention',
690
+ 'chunk_attention': 'chunk_attention',
691
+ 'sliding_window': 'sliding_window_attention',
692
+ 'dilated_attention': 'dilated_attention',
693
+ 'block_sparse': 'block_sparse_attention',
694
+ 'relative_position': 'relative_position_attention',
695
+ 'rotary_position': 'rotary_position_attention',
696
+ 'alibi': 'alibi_attention',
697
+ 'causal': 'causal_attention',
698
+ 'bidirectional': 'bidirectional_attention',
699
+ 'axial': 'axial_attention',
700
+ 'performer': 'performer_attention',
701
+ 'linformer': 'linformer_attention',
702
+ 'reformer': 'reformer_attention',
703
+ 'synthesizer': 'synthesizer_attention',
704
+ 'routing': 'routing_attention',
705
+ 'mixture_of_experts': 'moe_attention',
706
+ 'graph_attention': 'graph_attention',
707
+ 'hyperbolic_attention': 'hyperbolic_attention',
708
+ 'spherical_attention': 'spherical_attention',
709
+ 'toroidal_attention': 'toroidal_attention',
710
+ 'temporal_attention': 'temporal_attention',
711
+ 'recurrent_attention': 'recurrent_attention',
712
+ 'state_space': 'state_space_attention',
713
+ 'cross_modal': 'cross_modal_attention',
714
+ 'perceiver': 'perceiver_attention',
715
+ 'flamingo': 'flamingo_attention',
716
+ 'retrieval_attention': 'retrieval_attention',
717
+ 'knn_attention': 'knn_attention',
718
+ 'memory_augmented': 'memory_augmented_attention',
719
+ };
720
+ return mapping[mechanism] ?? 'multi_head_attention';
721
+ }
722
+ }
723
+
724
+ // ============================================================================
725
+ // Exports
726
+ // ============================================================================
727
+
728
+ export {
729
+ AttentionRegistry,
730
+ type IAttentionMechanism,
731
+ type AttentionOptions,
732
+ type AttentionCategory,
733
+ } from './attention.js';
734
+
735
+ // Re-export all mechanism classes for direct instantiation
736
+ export {
737
+ MultiHeadAttention,
738
+ SelfAttention,
739
+ CrossAttention,
740
+ CausalAttention,
741
+ BidirectionalAttention,
742
+ LocalAttention,
743
+ GlobalAttention,
744
+ FlashAttention,
745
+ FlashAttentionV2,
746
+ MemoryEfficientAttention,
747
+ ChunkAttention,
748
+ SlidingWindowAttention,
749
+ DilatedAttention,
750
+ } from './attention.js';
751
+
752
+ export {
753
+ SparseAttention,
754
+ BlockSparseAttention,
755
+ LinearAttention,
756
+ PerformerAttention,
757
+ LinformerAttention,
758
+ ReformerAttention,
759
+ RelativePositionAttention,
760
+ RotaryPositionAttention,
761
+ ALiBiAttention,
762
+ AxialAttention,
763
+ } from './attention-mechanisms.js';
764
+
765
+ export {
766
+ GraphAttention,
767
+ HyperbolicAttention,
768
+ SphericalAttention,
769
+ ToroidalAttention,
770
+ TemporalAttention,
771
+ RecurrentAttention,
772
+ StateSpaceAttention,
773
+ CrossModalAttention,
774
+ PerceiverAttention,
775
+ FlamingoAttention,
776
+ RetrievalAttention,
777
+ KNNAttention,
778
+ MemoryAugmentedAttention,
779
+ SynthesizerAttention,
780
+ RoutingAttention,
781
+ MixtureOfExpertsAttention,
782
+ } from './attention-advanced.js';