@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,762 @@
1
+ -- ============================================================================
2
+ -- Migration 007: Create Hyperbolic Geometry Functions
3
+ -- RuVector PostgreSQL Bridge - Claude Flow V3
4
+ --
5
+ -- Creates SQL functions for hyperbolic embeddings including Poincare ball
6
+ -- and Lorentz model operations for hierarchical data representation.
7
+ -- Compatible with PostgreSQL 14+ and pgvector 0.5+
8
+ -- ============================================================================
9
+
10
+ BEGIN;
11
+
12
+ -- ----------------------------------------------------------------------------
13
+ -- Constants and Utilities
14
+ -- ----------------------------------------------------------------------------
15
+
16
+ -- Clamp value to valid Poincare ball radius (|x| < 1)
17
+ CREATE OR REPLACE FUNCTION claude_flow.clamp_to_poincare(
18
+ x REAL,
19
+ epsilon REAL DEFAULT 1e-5
20
+ ) RETURNS REAL AS $$
21
+ BEGIN
22
+ IF x >= 1.0 - epsilon THEN
23
+ RETURN 1.0 - epsilon;
24
+ ELSIF x <= -1.0 + epsilon THEN
25
+ RETURN -1.0 + epsilon;
26
+ ELSE
27
+ RETURN x;
28
+ END IF;
29
+ END;
30
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
31
+
32
+ -- Clamp vector to valid Poincare ball
33
+ CREATE OR REPLACE FUNCTION claude_flow.clamp_vector_to_poincare(
34
+ v REAL[],
35
+ epsilon REAL DEFAULT 1e-5
36
+ ) RETURNS REAL[] AS $$
37
+ DECLARE
38
+ norm REAL;
39
+ max_norm REAL;
40
+ i INTEGER;
41
+ result REAL[];
42
+ BEGIN
43
+ norm := claude_flow.vector_magnitude(v);
44
+ max_norm := 1.0 - epsilon;
45
+
46
+ IF norm >= max_norm THEN
47
+ result := ARRAY[]::REAL[];
48
+ FOR i IN 1..array_length(v, 1) LOOP
49
+ result := array_append(result, v[i] * max_norm / norm);
50
+ END LOOP;
51
+ RETURN result;
52
+ ELSE
53
+ RETURN v;
54
+ END IF;
55
+ END;
56
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
57
+
58
+ -- Hyperbolic arctanh (inverse hyperbolic tangent)
59
+ CREATE OR REPLACE FUNCTION claude_flow.arctanh(
60
+ x REAL
61
+ ) RETURNS REAL AS $$
62
+ BEGIN
63
+ -- Clamp to avoid domain errors
64
+ x := GREATEST(-0.9999999, LEAST(0.9999999, x));
65
+ RETURN 0.5 * ln((1.0 + x) / (1.0 - x));
66
+ END;
67
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
68
+
69
+ -- Hyperbolic arcsinh (inverse hyperbolic sine)
70
+ CREATE OR REPLACE FUNCTION claude_flow.arcsinh(
71
+ x REAL
72
+ ) RETURNS REAL AS $$
73
+ BEGIN
74
+ RETURN ln(x + sqrt(x * x + 1.0));
75
+ END;
76
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
77
+
78
+ -- Hyperbolic arccosh (inverse hyperbolic cosh)
79
+ CREATE OR REPLACE FUNCTION claude_flow.arccosh(
80
+ x REAL
81
+ ) RETURNS REAL AS $$
82
+ BEGIN
83
+ IF x < 1.0 THEN
84
+ x := 1.0; -- Clamp to valid domain
85
+ END IF;
86
+ RETURN ln(x + sqrt(x * x - 1.0));
87
+ END;
88
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
89
+
90
+ -- ----------------------------------------------------------------------------
91
+ -- Poincare Ball Model Functions
92
+ -- The Poincare ball is the unit ball B^n = {x : ||x|| < 1} with
93
+ -- the Riemannian metric g_x = (2 / (1 - ||x||^2))^2 * g_E
94
+ -- ----------------------------------------------------------------------------
95
+
96
+ -- Poincare distance between two points
97
+ -- d(x, y) = arcosh(1 + 2 * ||x - y||^2 / ((1 - ||x||^2)(1 - ||y||^2)))
98
+ CREATE OR REPLACE FUNCTION claude_flow.poincare_distance(
99
+ v1 REAL[],
100
+ v2 REAL[],
101
+ curvature REAL DEFAULT -1.0
102
+ ) RETURNS REAL AS $$
103
+ DECLARE
104
+ c REAL;
105
+ diff_sq REAL := 0;
106
+ norm_v1_sq REAL := 0;
107
+ norm_v2_sq REAL := 0;
108
+ i INTEGER;
109
+ numerator REAL;
110
+ denominator REAL;
111
+ arg REAL;
112
+ BEGIN
113
+ c := abs(curvature);
114
+
115
+ -- Compute squared norms and squared difference
116
+ FOR i IN 1..array_length(v1, 1) LOOP
117
+ diff_sq := diff_sq + (v1[i] - v2[i]) * (v1[i] - v2[i]);
118
+ norm_v1_sq := norm_v1_sq + v1[i] * v1[i];
119
+ norm_v2_sq := norm_v2_sq + v2[i] * v2[i];
120
+ END LOOP;
121
+
122
+ -- Poincare distance formula
123
+ numerator := 2.0 * c * diff_sq;
124
+ denominator := (1.0 - c * norm_v1_sq) * (1.0 - c * norm_v2_sq);
125
+
126
+ IF denominator <= 0 THEN
127
+ -- Points are on or outside the ball boundary
128
+ RETURN 1e9; -- Return large distance
129
+ END IF;
130
+
131
+ arg := 1.0 + numerator / denominator;
132
+ RETURN (1.0 / sqrt(c)) * claude_flow.arccosh(arg);
133
+ END;
134
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
135
+
136
+ -- ----------------------------------------------------------------------------
137
+ -- Lorentz (Hyperboloid) Model Functions
138
+ -- The Lorentz model uses the upper sheet of the hyperboloid:
139
+ -- H^n = {x in R^(n+1) : <x,x>_L = -1, x_0 > 0}
140
+ -- where <x,y>_L = -x_0*y_0 + x_1*y_1 + ... + x_n*y_n
141
+ -- ----------------------------------------------------------------------------
142
+
143
+ -- Lorentz inner product (Minkowski inner product)
144
+ CREATE OR REPLACE FUNCTION claude_flow.lorentz_inner_product(
145
+ v1 REAL[],
146
+ v2 REAL[]
147
+ ) RETURNS REAL AS $$
148
+ DECLARE
149
+ result REAL;
150
+ i INTEGER;
151
+ BEGIN
152
+ IF array_length(v1, 1) != array_length(v2, 1) THEN
153
+ RAISE EXCEPTION 'Vector dimensions must match';
154
+ END IF;
155
+
156
+ -- First component has negative sign (time-like)
157
+ result := -v1[1] * v2[1];
158
+
159
+ -- Remaining components are space-like
160
+ FOR i IN 2..array_length(v1, 1) LOOP
161
+ result := result + v1[i] * v2[i];
162
+ END LOOP;
163
+
164
+ RETURN result;
165
+ END;
166
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
167
+
168
+ -- Lorentz distance
169
+ -- d(x, y) = arccosh(-<x, y>_L)
170
+ CREATE OR REPLACE FUNCTION claude_flow.lorentz_distance(
171
+ v1 REAL[],
172
+ v2 REAL[]
173
+ ) RETURNS REAL AS $$
174
+ DECLARE
175
+ inner REAL;
176
+ BEGIN
177
+ inner := claude_flow.lorentz_inner_product(v1, v2);
178
+ -- The inner product should be <= -1 for valid hyperboloid points
179
+ RETURN claude_flow.arccosh(-inner);
180
+ END;
181
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
182
+
183
+ -- ----------------------------------------------------------------------------
184
+ -- Exponential Map (Tangent space -> Manifold)
185
+ -- exp_x(v) maps a tangent vector v at point x to a point on the manifold
186
+ -- ----------------------------------------------------------------------------
187
+
188
+ -- Exponential map in Poincare ball
189
+ CREATE OR REPLACE FUNCTION claude_flow.exp_map(
190
+ base REAL[], -- Base point on the manifold
191
+ tangent REAL[], -- Tangent vector at base
192
+ curvature REAL DEFAULT -1.0
193
+ ) RETURNS REAL[] AS $$
194
+ DECLARE
195
+ c REAL;
196
+ lambda_x REAL;
197
+ tangent_norm REAL;
198
+ sqrt_c REAL;
199
+ tanh_arg REAL;
200
+ coeff REAL;
201
+ result REAL[];
202
+ i INTEGER;
203
+ base_norm_sq REAL := 0;
204
+ BEGIN
205
+ c := abs(curvature);
206
+ sqrt_c := sqrt(c);
207
+
208
+ -- Compute ||base||^2
209
+ FOR i IN 1..array_length(base, 1) LOOP
210
+ base_norm_sq := base_norm_sq + base[i] * base[i];
211
+ END LOOP;
212
+
213
+ -- Conformal factor at base point
214
+ lambda_x := 2.0 / (1.0 - c * base_norm_sq);
215
+
216
+ -- Compute ||tangent||
217
+ tangent_norm := claude_flow.vector_magnitude(tangent);
218
+
219
+ IF tangent_norm < 1e-10 THEN
220
+ RETURN base; -- No movement
221
+ END IF;
222
+
223
+ -- tanh(sqrt(c) * lambda_x * ||v|| / 2)
224
+ tanh_arg := sqrt_c * lambda_x * tangent_norm / 2.0;
225
+ coeff := tanh(tanh_arg) / (sqrt_c * tangent_norm);
226
+
227
+ -- exp_x(v) = x ⊕_c (tanh(...) * v / ||v||)
228
+ -- Using Mobius addition
229
+ DECLARE
230
+ scaled_tangent REAL[] := ARRAY[]::REAL[];
231
+ BEGIN
232
+ FOR i IN 1..array_length(tangent, 1) LOOP
233
+ scaled_tangent := array_append(scaled_tangent, coeff * tangent[i]);
234
+ END LOOP;
235
+
236
+ result := claude_flow.mobius_add(base, scaled_tangent, curvature);
237
+ END;
238
+
239
+ RETURN claude_flow.clamp_vector_to_poincare(result);
240
+ END;
241
+ $$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
242
+
243
+ -- ----------------------------------------------------------------------------
244
+ -- Logarithmic Map (Manifold -> Tangent space)
245
+ -- log_x(y) maps a point y on the manifold to the tangent space at x
246
+ -- ----------------------------------------------------------------------------
247
+
248
+ -- Logarithmic map in Poincare ball
249
+ CREATE OR REPLACE FUNCTION claude_flow.log_map(
250
+ base REAL[], -- Base point on the manifold
251
+ point REAL[] -- Target point on the manifold
252
+ ) RETURNS REAL[] AS $$
253
+ DECLARE
254
+ c REAL := 1.0; -- curvature magnitude
255
+ sqrt_c REAL := 1.0;
256
+ lambda_x REAL;
257
+ neg_base REAL[];
258
+ mobius_result REAL[];
259
+ mobius_norm REAL;
260
+ arctanh_arg REAL;
261
+ coeff REAL;
262
+ result REAL[];
263
+ i INTEGER;
264
+ base_norm_sq REAL := 0;
265
+ BEGIN
266
+ -- Compute ||base||^2
267
+ FOR i IN 1..array_length(base, 1) LOOP
268
+ base_norm_sq := base_norm_sq + base[i] * base[i];
269
+ END LOOP;
270
+
271
+ -- Conformal factor
272
+ lambda_x := 2.0 / (1.0 - c * base_norm_sq);
273
+
274
+ -- Compute -base ⊕_c point
275
+ neg_base := ARRAY[]::REAL[];
276
+ FOR i IN 1..array_length(base, 1) LOOP
277
+ neg_base := array_append(neg_base, -base[i]);
278
+ END LOOP;
279
+
280
+ mobius_result := claude_flow.mobius_add(neg_base, point, -c);
281
+ mobius_norm := claude_flow.vector_magnitude(mobius_result);
282
+
283
+ IF mobius_norm < 1e-10 THEN
284
+ -- Points are the same, return zero tangent
285
+ result := ARRAY[]::REAL[];
286
+ FOR i IN 1..array_length(base, 1) LOOP
287
+ result := array_append(result, 0.0);
288
+ END LOOP;
289
+ RETURN result;
290
+ END IF;
291
+
292
+ -- arctanh(sqrt(c) * ||..||) * 2 / (sqrt(c) * lambda_x)
293
+ arctanh_arg := sqrt_c * mobius_norm;
294
+ coeff := (2.0 / (sqrt_c * lambda_x)) * claude_flow.arctanh(arctanh_arg) / mobius_norm;
295
+
296
+ result := ARRAY[]::REAL[];
297
+ FOR i IN 1..array_length(mobius_result, 1) LOOP
298
+ result := array_append(result, coeff * mobius_result[i]);
299
+ END LOOP;
300
+
301
+ RETURN result;
302
+ END;
303
+ $$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
304
+
305
+ -- ----------------------------------------------------------------------------
306
+ -- Mobius Addition (Gyrovector addition in hyperbolic space)
307
+ -- x ⊕_c y = ((1 + 2c<x,y> + c||y||^2)x + (1 - c||x||^2)y) /
308
+ -- (1 + 2c<x,y> + c^2||x||^2||y||^2)
309
+ -- ----------------------------------------------------------------------------
310
+
311
+ CREATE OR REPLACE FUNCTION claude_flow.mobius_add(
312
+ v1 REAL[],
313
+ v2 REAL[],
314
+ curvature REAL DEFAULT -1.0
315
+ ) RETURNS REAL[] AS $$
316
+ DECLARE
317
+ c REAL;
318
+ dot_product REAL := 0;
319
+ norm_v1_sq REAL := 0;
320
+ norm_v2_sq REAL := 0;
321
+ numerator_coeff1 REAL;
322
+ numerator_coeff2 REAL;
323
+ denominator REAL;
324
+ result REAL[];
325
+ i INTEGER;
326
+ BEGIN
327
+ c := abs(curvature);
328
+
329
+ -- Compute dot product and squared norms
330
+ FOR i IN 1..array_length(v1, 1) LOOP
331
+ dot_product := dot_product + v1[i] * v2[i];
332
+ norm_v1_sq := norm_v1_sq + v1[i] * v1[i];
333
+ norm_v2_sq := norm_v2_sq + v2[i] * v2[i];
334
+ END LOOP;
335
+
336
+ -- Compute coefficients
337
+ numerator_coeff1 := 1.0 + 2.0 * c * dot_product + c * norm_v2_sq;
338
+ numerator_coeff2 := 1.0 - c * norm_v1_sq;
339
+ denominator := 1.0 + 2.0 * c * dot_product + c * c * norm_v1_sq * norm_v2_sq;
340
+
341
+ IF abs(denominator) < 1e-10 THEN
342
+ denominator := 1e-10; -- Avoid division by zero
343
+ END IF;
344
+
345
+ result := ARRAY[]::REAL[];
346
+ FOR i IN 1..array_length(v1, 1) LOOP
347
+ result := array_append(result,
348
+ (numerator_coeff1 * v1[i] + numerator_coeff2 * v2[i]) / denominator
349
+ );
350
+ END LOOP;
351
+
352
+ RETURN claude_flow.clamp_vector_to_poincare(result);
353
+ END;
354
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
355
+
356
+ -- ----------------------------------------------------------------------------
357
+ -- Mobius Scalar Multiplication
358
+ -- r ⊗_c x = (1/sqrt(c)) * tanh(r * arctanh(sqrt(c) * ||x||)) * (x / ||x||)
359
+ -- ----------------------------------------------------------------------------
360
+
361
+ CREATE OR REPLACE FUNCTION claude_flow.mobius_scalar_mul(
362
+ r REAL,
363
+ v REAL[],
364
+ curvature REAL DEFAULT -1.0
365
+ ) RETURNS REAL[] AS $$
366
+ DECLARE
367
+ c REAL;
368
+ sqrt_c REAL;
369
+ v_norm REAL;
370
+ arctanh_arg REAL;
371
+ tanh_result REAL;
372
+ coeff REAL;
373
+ result REAL[];
374
+ i INTEGER;
375
+ BEGIN
376
+ c := abs(curvature);
377
+ sqrt_c := sqrt(c);
378
+
379
+ v_norm := claude_flow.vector_magnitude(v);
380
+
381
+ IF v_norm < 1e-10 THEN
382
+ -- Return zero vector
383
+ result := ARRAY[]::REAL[];
384
+ FOR i IN 1..array_length(v, 1) LOOP
385
+ result := array_append(result, 0.0);
386
+ END LOOP;
387
+ RETURN result;
388
+ END IF;
389
+
390
+ arctanh_arg := sqrt_c * v_norm;
391
+ tanh_result := tanh(r * claude_flow.arctanh(arctanh_arg));
392
+ coeff := (1.0 / sqrt_c) * tanh_result / v_norm;
393
+
394
+ result := ARRAY[]::REAL[];
395
+ FOR i IN 1..array_length(v, 1) LOOP
396
+ result := array_append(result, coeff * v[i]);
397
+ END LOOP;
398
+
399
+ RETURN claude_flow.clamp_vector_to_poincare(result);
400
+ END;
401
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
402
+
403
+ -- ----------------------------------------------------------------------------
404
+ -- Parallel Transport (Move tangent vector from one point to another)
405
+ -- ----------------------------------------------------------------------------
406
+
407
+ CREATE OR REPLACE FUNCTION claude_flow.parallel_transport(
408
+ v REAL[], -- Tangent vector to transport
409
+ from_point REAL[], -- Source point
410
+ to_point REAL[], -- Destination point
411
+ curvature REAL DEFAULT -1.0
412
+ ) RETURNS REAL[] AS $$
413
+ DECLARE
414
+ c REAL;
415
+ lambda_from REAL;
416
+ lambda_to REAL;
417
+ gyration REAL[];
418
+ transported REAL[];
419
+ from_norm_sq REAL := 0;
420
+ to_norm_sq REAL := 0;
421
+ i INTEGER;
422
+ BEGIN
423
+ c := abs(curvature);
424
+
425
+ -- Compute squared norms
426
+ FOR i IN 1..array_length(from_point, 1) LOOP
427
+ from_norm_sq := from_norm_sq + from_point[i] * from_point[i];
428
+ END LOOP;
429
+
430
+ FOR i IN 1..array_length(to_point, 1) LOOP
431
+ to_norm_sq := to_norm_sq + to_point[i] * to_point[i];
432
+ END LOOP;
433
+
434
+ -- Conformal factors
435
+ lambda_from := 2.0 / (1.0 - c * from_norm_sq);
436
+ lambda_to := 2.0 / (1.0 - c * to_norm_sq);
437
+
438
+ -- Simplified parallel transport (scaling by ratio of conformal factors)
439
+ transported := ARRAY[]::REAL[];
440
+ FOR i IN 1..array_length(v, 1) LOOP
441
+ transported := array_append(transported, v[i] * lambda_from / lambda_to);
442
+ END LOOP;
443
+
444
+ RETURN transported;
445
+ END;
446
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
447
+
448
+ -- ----------------------------------------------------------------------------
449
+ -- Hyperbolic Centroid (Frechet/Karcher mean)
450
+ -- Einstein midpoint for two points in Poincare ball
451
+ -- ----------------------------------------------------------------------------
452
+
453
+ CREATE OR REPLACE FUNCTION claude_flow.hyperbolic_centroid(
454
+ points REAL[][],
455
+ curvature REAL DEFAULT -1.0,
456
+ max_iterations INTEGER DEFAULT 100,
457
+ tolerance REAL DEFAULT 1e-6
458
+ ) RETURNS REAL[] AS $$
459
+ DECLARE
460
+ c REAL;
461
+ n INTEGER;
462
+ dim INTEGER;
463
+ centroid REAL[];
464
+ new_centroid REAL[];
465
+ i INTEGER;
466
+ j INTEGER;
467
+ iter INTEGER;
468
+ total_weight REAL;
469
+ tangent_sum REAL[];
470
+ tangent REAL[];
471
+ diff REAL;
472
+ BEGIN
473
+ c := abs(curvature);
474
+ n := array_length(points, 1);
475
+ dim := array_length(points[1], 1);
476
+
477
+ -- Initialize centroid at origin
478
+ centroid := ARRAY[]::REAL[];
479
+ FOR j IN 1..dim LOOP
480
+ centroid := array_append(centroid, 0.0);
481
+ END LOOP;
482
+
483
+ -- Iterative optimization (gradient descent in tangent space)
484
+ FOR iter IN 1..max_iterations LOOP
485
+ -- Compute sum of log maps (tangent vectors pointing to each point)
486
+ tangent_sum := ARRAY[]::REAL[];
487
+ FOR j IN 1..dim LOOP
488
+ tangent_sum := array_append(tangent_sum, 0.0);
489
+ END LOOP;
490
+
491
+ FOR i IN 1..n LOOP
492
+ tangent := claude_flow.log_map(centroid, points[i]);
493
+ FOR j IN 1..dim LOOP
494
+ tangent_sum[j] := tangent_sum[j] + tangent[j];
495
+ END LOOP;
496
+ END LOOP;
497
+
498
+ -- Average tangent
499
+ FOR j IN 1..dim LOOP
500
+ tangent_sum[j] := tangent_sum[j] / n;
501
+ END LOOP;
502
+
503
+ -- Move centroid in direction of average tangent
504
+ new_centroid := claude_flow.exp_map(centroid, tangent_sum, curvature);
505
+
506
+ -- Check convergence
507
+ diff := claude_flow.poincare_distance(centroid, new_centroid, curvature);
508
+ centroid := new_centroid;
509
+
510
+ IF diff < tolerance THEN
511
+ EXIT;
512
+ END IF;
513
+ END LOOP;
514
+
515
+ RETURN centroid;
516
+ END;
517
+ $$ LANGUAGE plpgsql STABLE;
518
+
519
+ -- ----------------------------------------------------------------------------
520
+ -- Poincare to Lorentz Conversion
521
+ -- ----------------------------------------------------------------------------
522
+
523
+ CREATE OR REPLACE FUNCTION claude_flow.poincare_to_lorentz(
524
+ poincare REAL[],
525
+ curvature REAL DEFAULT -1.0
526
+ ) RETURNS REAL[] AS $$
527
+ DECLARE
528
+ c REAL;
529
+ norm_sq REAL := 0;
530
+ denominator REAL;
531
+ lorentz REAL[];
532
+ i INTEGER;
533
+ BEGIN
534
+ c := abs(curvature);
535
+
536
+ -- Compute ||x||^2
537
+ FOR i IN 1..array_length(poincare, 1) LOOP
538
+ norm_sq := norm_sq + poincare[i] * poincare[i];
539
+ END LOOP;
540
+
541
+ denominator := 1.0 - c * norm_sq;
542
+
543
+ IF denominator <= 0 THEN
544
+ RAISE EXCEPTION 'Point is on or outside the Poincare ball boundary';
545
+ END IF;
546
+
547
+ -- Lorentz coordinates: (x_0, x_1, ..., x_n)
548
+ -- x_0 = (1 + c*||x||^2) / (1 - c*||x||^2)
549
+ -- x_i = 2*sqrt(c)*x_i / (1 - c*||x||^2)
550
+ lorentz := ARRAY[]::REAL[];
551
+ lorentz := array_append(lorentz, (1.0 + c * norm_sq) / denominator);
552
+
553
+ FOR i IN 1..array_length(poincare, 1) LOOP
554
+ lorentz := array_append(lorentz, 2.0 * sqrt(c) * poincare[i] / denominator);
555
+ END LOOP;
556
+
557
+ RETURN lorentz;
558
+ END;
559
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
560
+
561
+ -- ----------------------------------------------------------------------------
562
+ -- Lorentz to Poincare Conversion
563
+ -- ----------------------------------------------------------------------------
564
+
565
+ CREATE OR REPLACE FUNCTION claude_flow.lorentz_to_poincare(
566
+ lorentz REAL[],
567
+ curvature REAL DEFAULT -1.0
568
+ ) RETURNS REAL[] AS $$
569
+ DECLARE
570
+ c REAL;
571
+ sqrt_c REAL;
572
+ denominator REAL;
573
+ poincare REAL[];
574
+ i INTEGER;
575
+ BEGIN
576
+ c := abs(curvature);
577
+ sqrt_c := sqrt(c);
578
+
579
+ -- x_0 is the first component
580
+ denominator := lorentz[1] + 1.0;
581
+
582
+ IF denominator <= 0 THEN
583
+ RAISE EXCEPTION 'Invalid Lorentz point';
584
+ END IF;
585
+
586
+ -- Poincare coordinates: x_i = x_i / (sqrt(c) * (x_0 + 1))
587
+ poincare := ARRAY[]::REAL[];
588
+ FOR i IN 2..array_length(lorentz, 1) LOOP
589
+ poincare := array_append(poincare, lorentz[i] / (sqrt_c * denominator));
590
+ END LOOP;
591
+
592
+ RETURN claude_flow.clamp_vector_to_poincare(poincare);
593
+ END;
594
+ $$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
595
+
596
+ -- ----------------------------------------------------------------------------
597
+ -- Hyperbolic Embedding Storage
598
+ -- ----------------------------------------------------------------------------
599
+
600
+ -- Store hyperbolic embedding
601
+ CREATE OR REPLACE FUNCTION claude_flow.store_hyperbolic_embedding(
602
+ p_namespace TEXT,
603
+ p_name TEXT,
604
+ p_poincare_embedding REAL[],
605
+ p_curvature REAL DEFAULT -1.0,
606
+ p_depth INTEGER DEFAULT 0,
607
+ p_parent_id UUID DEFAULT NULL,
608
+ p_metadata JSONB DEFAULT '{}'
609
+ ) RETURNS UUID AS $$
610
+ DECLARE
611
+ v_id UUID;
612
+ v_lorentz REAL[];
613
+ BEGIN
614
+ -- Convert to Lorentz representation
615
+ v_lorentz := claude_flow.poincare_to_lorentz(p_poincare_embedding, p_curvature);
616
+
617
+ INSERT INTO claude_flow.hyperbolic_embeddings (
618
+ namespace,
619
+ name,
620
+ poincare_embedding,
621
+ lorentz_embedding,
622
+ dimensions,
623
+ curvature,
624
+ depth,
625
+ parent_id,
626
+ metadata
627
+ ) VALUES (
628
+ p_namespace,
629
+ p_name,
630
+ p_poincare_embedding,
631
+ v_lorentz,
632
+ array_length(p_poincare_embedding, 1),
633
+ p_curvature,
634
+ p_depth,
635
+ p_parent_id,
636
+ p_metadata
637
+ )
638
+ ON CONFLICT (namespace, name) DO UPDATE
639
+ SET poincare_embedding = EXCLUDED.poincare_embedding,
640
+ lorentz_embedding = EXCLUDED.lorentz_embedding,
641
+ dimensions = EXCLUDED.dimensions,
642
+ curvature = EXCLUDED.curvature,
643
+ depth = EXCLUDED.depth,
644
+ parent_id = EXCLUDED.parent_id,
645
+ metadata = EXCLUDED.metadata
646
+ RETURNING id INTO v_id;
647
+
648
+ -- Update parent's children count
649
+ IF p_parent_id IS NOT NULL THEN
650
+ UPDATE claude_flow.hyperbolic_embeddings
651
+ SET children_count = children_count + 1
652
+ WHERE id = p_parent_id;
653
+ END IF;
654
+
655
+ RETURN v_id;
656
+ END;
657
+ $$ LANGUAGE plpgsql;
658
+
659
+ -- Find nearest neighbors in hyperbolic space
660
+ CREATE OR REPLACE FUNCTION claude_flow.hyperbolic_knn(
661
+ p_query REAL[],
662
+ p_k INTEGER DEFAULT 10,
663
+ p_namespace TEXT DEFAULT NULL,
664
+ p_curvature REAL DEFAULT -1.0
665
+ ) RETURNS TABLE (
666
+ id UUID,
667
+ name TEXT,
668
+ distance REAL,
669
+ depth INTEGER,
670
+ metadata JSONB
671
+ ) AS $$
672
+ BEGIN
673
+ RETURN QUERY
674
+ SELECT
675
+ h.id,
676
+ h.name,
677
+ claude_flow.poincare_distance(p_query, h.poincare_embedding, p_curvature) AS distance,
678
+ h.depth,
679
+ h.metadata
680
+ FROM claude_flow.hyperbolic_embeddings h
681
+ WHERE (p_namespace IS NULL OR h.namespace = p_namespace)
682
+ ORDER BY claude_flow.poincare_distance(p_query, h.poincare_embedding, p_curvature)
683
+ LIMIT p_k;
684
+ END;
685
+ $$ LANGUAGE plpgsql STABLE;
686
+
687
+ -- Get subtree rooted at a node
688
+ CREATE OR REPLACE FUNCTION claude_flow.get_hyperbolic_subtree(
689
+ p_root_id UUID,
690
+ p_max_depth INTEGER DEFAULT 10
691
+ ) RETURNS TABLE (
692
+ id UUID,
693
+ name TEXT,
694
+ depth INTEGER,
695
+ level INTEGER,
696
+ poincare_embedding REAL[],
697
+ metadata JSONB
698
+ ) AS $$
699
+ BEGIN
700
+ RETURN QUERY
701
+ WITH RECURSIVE subtree AS (
702
+ SELECT
703
+ h.id,
704
+ h.name,
705
+ h.depth,
706
+ 0 AS level,
707
+ h.poincare_embedding,
708
+ h.metadata
709
+ FROM claude_flow.hyperbolic_embeddings h
710
+ WHERE h.id = p_root_id
711
+
712
+ UNION ALL
713
+
714
+ SELECT
715
+ c.id,
716
+ c.name,
717
+ c.depth,
718
+ s.level + 1,
719
+ c.poincare_embedding,
720
+ c.metadata
721
+ FROM claude_flow.hyperbolic_embeddings c
722
+ JOIN subtree s ON c.parent_id = s.id
723
+ WHERE s.level < p_max_depth
724
+ )
725
+ SELECT * FROM subtree;
726
+ END;
727
+ $$ LANGUAGE plpgsql STABLE;
728
+
729
+ -- ----------------------------------------------------------------------------
730
+ -- Record migration
731
+ -- ----------------------------------------------------------------------------
732
+ INSERT INTO claude_flow.migrations (name, checksum)
733
+ VALUES ('007_create_hyperbolic_functions', md5('007_create_hyperbolic_functions'))
734
+ ON CONFLICT (name) DO NOTHING;
735
+
736
+ COMMIT;
737
+
738
+ -- ============================================================================
739
+ -- Rollback Script
740
+ -- ============================================================================
741
+ -- BEGIN;
742
+ -- DROP FUNCTION IF EXISTS claude_flow.get_hyperbolic_subtree(UUID, INTEGER);
743
+ -- DROP FUNCTION IF EXISTS claude_flow.hyperbolic_knn(REAL[], INTEGER, TEXT, REAL);
744
+ -- DROP FUNCTION IF EXISTS claude_flow.store_hyperbolic_embedding(TEXT, TEXT, REAL[], REAL, INTEGER, UUID, JSONB);
745
+ -- DROP FUNCTION IF EXISTS claude_flow.lorentz_to_poincare(REAL[], REAL);
746
+ -- DROP FUNCTION IF EXISTS claude_flow.poincare_to_lorentz(REAL[], REAL);
747
+ -- DROP FUNCTION IF EXISTS claude_flow.hyperbolic_centroid(REAL[][], REAL, INTEGER, REAL);
748
+ -- DROP FUNCTION IF EXISTS claude_flow.parallel_transport(REAL[], REAL[], REAL[], REAL);
749
+ -- DROP FUNCTION IF EXISTS claude_flow.mobius_scalar_mul(REAL, REAL[], REAL);
750
+ -- DROP FUNCTION IF EXISTS claude_flow.mobius_add(REAL[], REAL[], REAL);
751
+ -- DROP FUNCTION IF EXISTS claude_flow.log_map(REAL[], REAL[]);
752
+ -- DROP FUNCTION IF EXISTS claude_flow.exp_map(REAL[], REAL[], REAL);
753
+ -- DROP FUNCTION IF EXISTS claude_flow.lorentz_distance(REAL[], REAL[]);
754
+ -- DROP FUNCTION IF EXISTS claude_flow.lorentz_inner_product(REAL[], REAL[]);
755
+ -- DROP FUNCTION IF EXISTS claude_flow.poincare_distance(REAL[], REAL[], REAL);
756
+ -- DROP FUNCTION IF EXISTS claude_flow.arccosh(REAL);
757
+ -- DROP FUNCTION IF EXISTS claude_flow.arcsinh(REAL);
758
+ -- DROP FUNCTION IF EXISTS claude_flow.arctanh(REAL);
759
+ -- DROP FUNCTION IF EXISTS claude_flow.clamp_vector_to_poincare(REAL[], REAL);
760
+ -- DROP FUNCTION IF EXISTS claude_flow.clamp_to_poincare(REAL, REAL);
761
+ -- DELETE FROM claude_flow.migrations WHERE name = '007_create_hyperbolic_functions';
762
+ -- COMMIT;