@sparkleideas/plugins 3.0.0-alpha.8

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,259 @@
1
+ -- ============================================================================
2
+ -- Migration 002: Create Vector Tables
3
+ -- RuVector PostgreSQL Bridge - Claude Flow V3
4
+ --
5
+ -- Creates core vector storage tables for embeddings, attention, and GNN cache.
6
+ -- Compatible with PostgreSQL 14+ and pgvector 0.5+
7
+ -- ============================================================================
8
+
9
+ BEGIN;
10
+
11
+ -- ----------------------------------------------------------------------------
12
+ -- Table: vectors
13
+ -- Primary vector storage table with metadata support
14
+ -- ----------------------------------------------------------------------------
15
+ CREATE TABLE IF NOT EXISTS claude_flow.vectors (
16
+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
17
+ embedding vector(1536), -- Default dimension, will be cast dynamically
18
+ metadata JSONB NOT NULL DEFAULT '{}',
19
+ content TEXT, -- Optional text content associated with vector
20
+ namespace TEXT NOT NULL DEFAULT 'default',
21
+ collection TEXT NOT NULL DEFAULT 'default',
22
+ source TEXT, -- Source identifier (file path, URL, etc.)
23
+ hash TEXT, -- Content hash for deduplication
24
+ version INTEGER NOT NULL DEFAULT 1,
25
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
26
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
27
+ expires_at TIMESTAMPTZ, -- Optional TTL
28
+
29
+ -- Constraints
30
+ CONSTRAINT vectors_hash_unique UNIQUE (namespace, collection, hash)
31
+ );
32
+
33
+ -- Trigger to update updated_at timestamp
34
+ CREATE OR REPLACE FUNCTION claude_flow.update_updated_at()
35
+ RETURNS TRIGGER AS $$
36
+ BEGIN
37
+ NEW.updated_at = NOW();
38
+ RETURN NEW;
39
+ END;
40
+ $$ LANGUAGE plpgsql;
41
+
42
+ CREATE TRIGGER vectors_updated_at
43
+ BEFORE UPDATE ON claude_flow.vectors
44
+ FOR EACH ROW
45
+ EXECUTE FUNCTION claude_flow.update_updated_at();
46
+
47
+ -- ----------------------------------------------------------------------------
48
+ -- Table: embeddings
49
+ -- Namespace-aware embeddings storage with dimension flexibility
50
+ -- ----------------------------------------------------------------------------
51
+ CREATE TABLE IF NOT EXISTS claude_flow.embeddings (
52
+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
53
+ namespace TEXT NOT NULL,
54
+ name TEXT NOT NULL,
55
+ embedding vector(1536),
56
+ dimensions INTEGER NOT NULL DEFAULT 1536,
57
+ model TEXT, -- Model used to generate embedding (e.g., 'text-embedding-3-large')
58
+ metadata JSONB NOT NULL DEFAULT '{}',
59
+ tags TEXT[] DEFAULT ARRAY[]::TEXT[],
60
+ importance REAL DEFAULT 0.5, -- Importance score for prioritization
61
+ access_count INTEGER DEFAULT 0,
62
+ last_accessed_at TIMESTAMPTZ,
63
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
64
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
65
+
66
+ -- Constraints
67
+ CONSTRAINT embeddings_namespace_name_unique UNIQUE (namespace, name)
68
+ );
69
+
70
+ CREATE TRIGGER embeddings_updated_at
71
+ BEFORE UPDATE ON claude_flow.embeddings
72
+ FOR EACH ROW
73
+ EXECUTE FUNCTION claude_flow.update_updated_at();
74
+
75
+ -- ----------------------------------------------------------------------------
76
+ -- Table: attention_cache
77
+ -- Caches attention computation results for performance
78
+ -- ----------------------------------------------------------------------------
79
+ CREATE TABLE IF NOT EXISTS claude_flow.attention_cache (
80
+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
81
+ cache_key TEXT NOT NULL UNIQUE, -- Hash of input parameters
82
+ query_hash TEXT NOT NULL,
83
+ keys_hash TEXT NOT NULL,
84
+ values_hash TEXT NOT NULL,
85
+ num_heads INTEGER NOT NULL,
86
+ attention_type TEXT NOT NULL CHECK (attention_type IN ('standard', 'multi_head', 'flash', 'sparse', 'linear')),
87
+
88
+ -- Cached results (stored as float arrays for flexibility)
89
+ attention_weights REAL[],
90
+ attention_output REAL[],
91
+ output_dimensions INTEGER[], -- Shape of the output
92
+
93
+ -- Performance metrics
94
+ computation_time_ms REAL,
95
+ memory_usage_bytes BIGINT,
96
+
97
+ -- Metadata
98
+ metadata JSONB DEFAULT '{}',
99
+ hit_count INTEGER DEFAULT 0,
100
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
101
+ last_accessed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
102
+ expires_at TIMESTAMPTZ, -- TTL for cache eviction
103
+
104
+ -- Constraints
105
+ CONSTRAINT attention_cache_valid_heads CHECK (num_heads > 0)
106
+ );
107
+
108
+ -- Index for cache lookups
109
+ CREATE INDEX IF NOT EXISTS idx_attention_cache_key ON claude_flow.attention_cache (cache_key);
110
+ CREATE INDEX IF NOT EXISTS idx_attention_cache_type ON claude_flow.attention_cache (attention_type);
111
+ CREATE INDEX IF NOT EXISTS idx_attention_cache_expires ON claude_flow.attention_cache (expires_at) WHERE expires_at IS NOT NULL;
112
+
113
+ -- ----------------------------------------------------------------------------
114
+ -- Table: gnn_cache
115
+ -- Caches Graph Neural Network computation results
116
+ -- ----------------------------------------------------------------------------
117
+ CREATE TABLE IF NOT EXISTS claude_flow.gnn_cache (
118
+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
119
+ cache_key TEXT NOT NULL UNIQUE,
120
+ graph_hash TEXT NOT NULL, -- Hash of graph structure
121
+ layer_type TEXT NOT NULL CHECK (layer_type IN ('gcn', 'gat', 'graphsage', 'gin', 'mpnn')),
122
+ layer_index INTEGER NOT NULL DEFAULT 0,
123
+
124
+ -- Graph structure (adjacency representation)
125
+ num_nodes INTEGER NOT NULL,
126
+ num_edges INTEGER NOT NULL,
127
+ node_features_dim INTEGER NOT NULL,
128
+ edge_features_dim INTEGER DEFAULT 0,
129
+
130
+ -- Cached results
131
+ node_embeddings REAL[], -- Output node embeddings
132
+ edge_embeddings REAL[], -- Output edge embeddings (if applicable)
133
+ output_dim INTEGER NOT NULL,
134
+
135
+ -- GNN-specific parameters
136
+ aggregation TEXT DEFAULT 'mean', -- mean, sum, max, attention
137
+ num_heads INTEGER DEFAULT 1, -- For GAT
138
+ dropout_rate REAL DEFAULT 0.0,
139
+
140
+ -- Performance metrics
141
+ computation_time_ms REAL,
142
+ memory_usage_bytes BIGINT,
143
+
144
+ -- Metadata
145
+ metadata JSONB DEFAULT '{}',
146
+ hit_count INTEGER DEFAULT 0,
147
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
148
+ last_accessed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
149
+ expires_at TIMESTAMPTZ,
150
+
151
+ -- Constraints
152
+ CONSTRAINT gnn_cache_valid_dimensions CHECK (num_nodes > 0 AND node_features_dim > 0)
153
+ );
154
+
155
+ -- Indexes for GNN cache
156
+ CREATE INDEX IF NOT EXISTS idx_gnn_cache_key ON claude_flow.gnn_cache (cache_key);
157
+ CREATE INDEX IF NOT EXISTS idx_gnn_cache_type ON claude_flow.gnn_cache (layer_type);
158
+ CREATE INDEX IF NOT EXISTS idx_gnn_cache_graph ON claude_flow.gnn_cache (graph_hash);
159
+ CREATE INDEX IF NOT EXISTS idx_gnn_cache_expires ON claude_flow.gnn_cache (expires_at) WHERE expires_at IS NOT NULL;
160
+
161
+ -- ----------------------------------------------------------------------------
162
+ -- Table: hyperbolic_embeddings
163
+ -- Stores hyperbolic (Poincare/Lorentz) embeddings for hierarchical data
164
+ -- ----------------------------------------------------------------------------
165
+ CREATE TABLE IF NOT EXISTS claude_flow.hyperbolic_embeddings (
166
+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
167
+ namespace TEXT NOT NULL DEFAULT 'default',
168
+ name TEXT NOT NULL,
169
+
170
+ -- Hyperbolic coordinates
171
+ poincare_embedding REAL[] NOT NULL, -- Poincare ball coordinates
172
+ lorentz_embedding REAL[], -- Lorentz model coordinates (optional)
173
+ dimensions INTEGER NOT NULL,
174
+ curvature REAL NOT NULL DEFAULT -1.0, -- Negative curvature
175
+
176
+ -- Hierarchy information
177
+ depth INTEGER DEFAULT 0,
178
+ parent_id UUID REFERENCES claude_flow.hyperbolic_embeddings(id),
179
+ children_count INTEGER DEFAULT 0,
180
+
181
+ -- Metadata
182
+ metadata JSONB DEFAULT '{}',
183
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
184
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
185
+
186
+ -- Constraints
187
+ CONSTRAINT hyperbolic_valid_curvature CHECK (curvature < 0),
188
+ CONSTRAINT hyperbolic_namespace_name_unique UNIQUE (namespace, name)
189
+ );
190
+
191
+ CREATE TRIGGER hyperbolic_embeddings_updated_at
192
+ BEFORE UPDATE ON claude_flow.hyperbolic_embeddings
193
+ FOR EACH ROW
194
+ EXECUTE FUNCTION claude_flow.update_updated_at();
195
+
196
+ -- ----------------------------------------------------------------------------
197
+ -- Table: collections
198
+ -- Manages vector collections with configuration
199
+ -- ----------------------------------------------------------------------------
200
+ CREATE TABLE IF NOT EXISTS claude_flow.collections (
201
+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
202
+ name TEXT NOT NULL UNIQUE,
203
+ namespace TEXT NOT NULL DEFAULT 'default',
204
+ dimensions INTEGER NOT NULL DEFAULT 1536,
205
+ metric TEXT NOT NULL DEFAULT 'cosine' CHECK (metric IN ('cosine', 'euclidean', 'dot', 'hamming', 'manhattan')),
206
+
207
+ -- Index configuration
208
+ index_type TEXT DEFAULT 'hnsw' CHECK (index_type IN ('hnsw', 'ivfflat', 'flat')),
209
+ hnsw_m INTEGER DEFAULT 16,
210
+ hnsw_ef_construction INTEGER DEFAULT 64,
211
+ ivfflat_lists INTEGER DEFAULT 100,
212
+
213
+ -- Collection stats
214
+ vector_count INTEGER DEFAULT 0,
215
+ total_size_bytes BIGINT DEFAULT 0,
216
+
217
+ -- Metadata
218
+ description TEXT,
219
+ metadata JSONB DEFAULT '{}',
220
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
221
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
222
+
223
+ -- Constraints
224
+ CONSTRAINT collections_valid_dimensions CHECK (dimensions > 0 AND dimensions <= 16384),
225
+ CONSTRAINT collections_valid_hnsw CHECK (hnsw_m >= 2 AND hnsw_m <= 100)
226
+ );
227
+
228
+ CREATE TRIGGER collections_updated_at
229
+ BEFORE UPDATE ON claude_flow.collections
230
+ FOR EACH ROW
231
+ EXECUTE FUNCTION claude_flow.update_updated_at();
232
+
233
+ -- Insert default collection
234
+ INSERT INTO claude_flow.collections (name, namespace, dimensions, metric)
235
+ VALUES ('default', 'default', 1536, 'cosine')
236
+ ON CONFLICT (name) DO NOTHING;
237
+
238
+ -- ----------------------------------------------------------------------------
239
+ -- Record migration
240
+ -- ----------------------------------------------------------------------------
241
+ INSERT INTO claude_flow.migrations (name, checksum)
242
+ VALUES ('002_create_vector_tables', md5('002_create_vector_tables'))
243
+ ON CONFLICT (name) DO NOTHING;
244
+
245
+ COMMIT;
246
+
247
+ -- ============================================================================
248
+ -- Rollback Script
249
+ -- ============================================================================
250
+ -- BEGIN;
251
+ -- DROP TABLE IF EXISTS claude_flow.collections CASCADE;
252
+ -- DROP TABLE IF EXISTS claude_flow.hyperbolic_embeddings CASCADE;
253
+ -- DROP TABLE IF EXISTS claude_flow.gnn_cache CASCADE;
254
+ -- DROP TABLE IF EXISTS claude_flow.attention_cache CASCADE;
255
+ -- DROP TABLE IF EXISTS claude_flow.embeddings CASCADE;
256
+ -- DROP TABLE IF EXISTS claude_flow.vectors CASCADE;
257
+ -- DROP FUNCTION IF EXISTS claude_flow.update_updated_at();
258
+ -- DELETE FROM claude_flow.migrations WHERE name = '002_create_vector_tables';
259
+ -- COMMIT;
@@ -0,0 +1,328 @@
1
+ -- ============================================================================
2
+ -- Migration 003: Create Indices
3
+ -- RuVector PostgreSQL Bridge - Claude Flow V3
4
+ --
5
+ -- Creates HNSW, IVFFlat, GIN, and B-tree indices for optimal query performance.
6
+ -- Compatible with PostgreSQL 14+ and pgvector 0.5+
7
+ -- ============================================================================
8
+
9
+ BEGIN;
10
+
11
+ -- ----------------------------------------------------------------------------
12
+ -- HNSW Indices (Hierarchical Navigable Small World)
13
+ -- Best for: High recall, moderate dataset sizes, real-time applications
14
+ -- Parameters: m = edges per node, ef_construction = index build quality
15
+ -- ----------------------------------------------------------------------------
16
+
17
+ -- Vectors table - HNSW index for cosine similarity (default)
18
+ CREATE INDEX IF NOT EXISTS idx_vectors_embedding_hnsw_cosine
19
+ ON claude_flow.vectors
20
+ USING hnsw (embedding vector_cosine_ops)
21
+ WITH (m = 16, ef_construction = 64);
22
+
23
+ -- Vectors table - HNSW index for L2 distance (euclidean)
24
+ CREATE INDEX IF NOT EXISTS idx_vectors_embedding_hnsw_l2
25
+ ON claude_flow.vectors
26
+ USING hnsw (embedding vector_l2_ops)
27
+ WITH (m = 16, ef_construction = 64);
28
+
29
+ -- Vectors table - HNSW index for inner product (dot)
30
+ CREATE INDEX IF NOT EXISTS idx_vectors_embedding_hnsw_ip
31
+ ON claude_flow.vectors
32
+ USING hnsw (embedding vector_ip_ops)
33
+ WITH (m = 16, ef_construction = 64);
34
+
35
+ -- Embeddings table - HNSW index
36
+ CREATE INDEX IF NOT EXISTS idx_embeddings_embedding_hnsw_cosine
37
+ ON claude_flow.embeddings
38
+ USING hnsw (embedding vector_cosine_ops)
39
+ WITH (m = 16, ef_construction = 64);
40
+
41
+ -- ----------------------------------------------------------------------------
42
+ -- IVFFlat Indices (Inverted File with Flat vectors)
43
+ -- Best for: Large datasets with bulk loading, lower memory usage
44
+ -- Parameters: lists = number of inverted lists (sqrt(n) to n/100)
45
+ -- ----------------------------------------------------------------------------
46
+
47
+ -- Create IVFFlat indices (commented by default - enable based on use case)
48
+ -- Note: IVFFlat requires training data, so we use a lower lists count initially
49
+
50
+ -- Vectors table - IVFFlat index for cosine similarity
51
+ -- CREATE INDEX IF NOT EXISTS idx_vectors_embedding_ivfflat_cosine
52
+ -- ON claude_flow.vectors
53
+ -- USING ivfflat (embedding vector_cosine_ops)
54
+ -- WITH (lists = 100);
55
+
56
+ -- Vectors table - IVFFlat index for L2 distance
57
+ -- CREATE INDEX IF NOT EXISTS idx_vectors_embedding_ivfflat_l2
58
+ -- ON claude_flow.vectors
59
+ -- USING ivfflat (embedding vector_l2_ops)
60
+ -- WITH (lists = 100);
61
+
62
+ -- ----------------------------------------------------------------------------
63
+ -- Metadata GIN Indices
64
+ -- Best for: JSONB containment and key/value queries
65
+ -- ----------------------------------------------------------------------------
66
+
67
+ -- Vectors metadata index
68
+ CREATE INDEX IF NOT EXISTS idx_vectors_metadata_gin
69
+ ON claude_flow.vectors
70
+ USING gin (metadata jsonb_path_ops);
71
+
72
+ -- Embeddings metadata index
73
+ CREATE INDEX IF NOT EXISTS idx_embeddings_metadata_gin
74
+ ON claude_flow.embeddings
75
+ USING gin (metadata jsonb_path_ops);
76
+
77
+ -- Attention cache metadata index
78
+ CREATE INDEX IF NOT EXISTS idx_attention_cache_metadata_gin
79
+ ON claude_flow.attention_cache
80
+ USING gin (metadata jsonb_path_ops);
81
+
82
+ -- GNN cache metadata index
83
+ CREATE INDEX IF NOT EXISTS idx_gnn_cache_metadata_gin
84
+ ON claude_flow.gnn_cache
85
+ USING gin (metadata jsonb_path_ops);
86
+
87
+ -- Hyperbolic embeddings metadata index
88
+ CREATE INDEX IF NOT EXISTS idx_hyperbolic_metadata_gin
89
+ ON claude_flow.hyperbolic_embeddings
90
+ USING gin (metadata jsonb_path_ops);
91
+
92
+ -- Collections metadata index
93
+ CREATE INDEX IF NOT EXISTS idx_collections_metadata_gin
94
+ ON claude_flow.collections
95
+ USING gin (metadata jsonb_path_ops);
96
+
97
+ -- ----------------------------------------------------------------------------
98
+ -- Namespace and Collection B-tree Indices
99
+ -- Best for: Equality and range queries on text columns
100
+ -- ----------------------------------------------------------------------------
101
+
102
+ -- Vectors namespace and collection index
103
+ CREATE INDEX IF NOT EXISTS idx_vectors_namespace_collection
104
+ ON claude_flow.vectors (namespace, collection);
105
+
106
+ -- Vectors namespace only
107
+ CREATE INDEX IF NOT EXISTS idx_vectors_namespace
108
+ ON claude_flow.vectors (namespace);
109
+
110
+ -- Embeddings namespace index
111
+ CREATE INDEX IF NOT EXISTS idx_embeddings_namespace
112
+ ON claude_flow.embeddings (namespace);
113
+
114
+ -- Embeddings tags index (using GIN for array containment)
115
+ CREATE INDEX IF NOT EXISTS idx_embeddings_tags_gin
116
+ ON claude_flow.embeddings
117
+ USING gin (tags);
118
+
119
+ -- Hyperbolic namespace index
120
+ CREATE INDEX IF NOT EXISTS idx_hyperbolic_namespace
121
+ ON claude_flow.hyperbolic_embeddings (namespace);
122
+
123
+ -- Collections namespace index
124
+ CREATE INDEX IF NOT EXISTS idx_collections_namespace
125
+ ON claude_flow.collections (namespace);
126
+
127
+ -- ----------------------------------------------------------------------------
128
+ -- Timestamp B-tree Indices
129
+ -- Best for: Time-based queries and TTL cleanup
130
+ -- ----------------------------------------------------------------------------
131
+
132
+ -- Vectors timestamp indices
133
+ CREATE INDEX IF NOT EXISTS idx_vectors_created_at
134
+ ON claude_flow.vectors (created_at DESC);
135
+
136
+ CREATE INDEX IF NOT EXISTS idx_vectors_updated_at
137
+ ON claude_flow.vectors (updated_at DESC);
138
+
139
+ CREATE INDEX IF NOT EXISTS idx_vectors_expires_at
140
+ ON claude_flow.vectors (expires_at)
141
+ WHERE expires_at IS NOT NULL;
142
+
143
+ -- Embeddings timestamp indices
144
+ CREATE INDEX IF NOT EXISTS idx_embeddings_created_at
145
+ ON claude_flow.embeddings (created_at DESC);
146
+
147
+ CREATE INDEX IF NOT EXISTS idx_embeddings_last_accessed
148
+ ON claude_flow.embeddings (last_accessed_at DESC)
149
+ WHERE last_accessed_at IS NOT NULL;
150
+
151
+ -- Attention cache timestamp indices
152
+ CREATE INDEX IF NOT EXISTS idx_attention_cache_created_at
153
+ ON claude_flow.attention_cache (created_at DESC);
154
+
155
+ CREATE INDEX IF NOT EXISTS idx_attention_cache_last_accessed
156
+ ON claude_flow.attention_cache (last_accessed_at DESC);
157
+
158
+ -- GNN cache timestamp indices
159
+ CREATE INDEX IF NOT EXISTS idx_gnn_cache_created_at
160
+ ON claude_flow.gnn_cache (created_at DESC);
161
+
162
+ CREATE INDEX IF NOT EXISTS idx_gnn_cache_last_accessed
163
+ ON claude_flow.gnn_cache (last_accessed_at DESC);
164
+
165
+ -- Hyperbolic embeddings timestamp indices
166
+ CREATE INDEX IF NOT EXISTS idx_hyperbolic_created_at
167
+ ON claude_flow.hyperbolic_embeddings (created_at DESC);
168
+
169
+ -- Collections timestamp indices
170
+ CREATE INDEX IF NOT EXISTS idx_collections_created_at
171
+ ON claude_flow.collections (created_at DESC);
172
+
173
+ -- ----------------------------------------------------------------------------
174
+ -- Hash/Deduplication Indices
175
+ -- Best for: Quick lookups by content hash
176
+ -- ----------------------------------------------------------------------------
177
+
178
+ -- Vectors hash index for deduplication
179
+ CREATE INDEX IF NOT EXISTS idx_vectors_hash
180
+ ON claude_flow.vectors (hash)
181
+ WHERE hash IS NOT NULL;
182
+
183
+ -- Vectors source index
184
+ CREATE INDEX IF NOT EXISTS idx_vectors_source
185
+ ON claude_flow.vectors (source)
186
+ WHERE source IS NOT NULL;
187
+
188
+ -- ----------------------------------------------------------------------------
189
+ -- Hierarchy Indices (for hyperbolic embeddings)
190
+ -- Best for: Tree traversal queries
191
+ -- ----------------------------------------------------------------------------
192
+
193
+ -- Parent lookup index
194
+ CREATE INDEX IF NOT EXISTS idx_hyperbolic_parent
195
+ ON claude_flow.hyperbolic_embeddings (parent_id)
196
+ WHERE parent_id IS NOT NULL;
197
+
198
+ -- Depth index for level-based queries
199
+ CREATE INDEX IF NOT EXISTS idx_hyperbolic_depth
200
+ ON claude_flow.hyperbolic_embeddings (depth);
201
+
202
+ -- ----------------------------------------------------------------------------
203
+ -- Access Pattern Indices
204
+ -- Best for: LRU/LFU cache eviction queries
205
+ -- ----------------------------------------------------------------------------
206
+
207
+ -- Embeddings access count index
208
+ CREATE INDEX IF NOT EXISTS idx_embeddings_access_count
209
+ ON claude_flow.embeddings (access_count DESC);
210
+
211
+ -- Attention cache hit count index
212
+ CREATE INDEX IF NOT EXISTS idx_attention_cache_hit_count
213
+ ON claude_flow.attention_cache (hit_count DESC);
214
+
215
+ -- GNN cache hit count index
216
+ CREATE INDEX IF NOT EXISTS idx_gnn_cache_hit_count
217
+ ON claude_flow.gnn_cache (hit_count DESC);
218
+
219
+ -- ----------------------------------------------------------------------------
220
+ -- Partial Indices for Common Queries
221
+ -- Best for: Frequently filtered subsets
222
+ -- ----------------------------------------------------------------------------
223
+
224
+ -- Active (non-expired) vectors
225
+ CREATE INDEX IF NOT EXISTS idx_vectors_active
226
+ ON claude_flow.vectors (namespace, created_at DESC)
227
+ WHERE expires_at IS NULL OR expires_at > NOW();
228
+
229
+ -- High-importance embeddings
230
+ CREATE INDEX IF NOT EXISTS idx_embeddings_high_importance
231
+ ON claude_flow.embeddings (importance DESC)
232
+ WHERE importance >= 0.7;
233
+
234
+ -- ----------------------------------------------------------------------------
235
+ -- Composite Indices for Common Query Patterns
236
+ -- ----------------------------------------------------------------------------
237
+
238
+ -- Vectors: namespace + collection + created_at (for filtered time queries)
239
+ CREATE INDEX IF NOT EXISTS idx_vectors_ns_coll_created
240
+ ON claude_flow.vectors (namespace, collection, created_at DESC);
241
+
242
+ -- Embeddings: namespace + importance + access_count (for prioritization)
243
+ CREATE INDEX IF NOT EXISTS idx_embeddings_ns_importance
244
+ ON claude_flow.embeddings (namespace, importance DESC, access_count DESC);
245
+
246
+ -- ----------------------------------------------------------------------------
247
+ -- Index Statistics and Maintenance
248
+ -- ----------------------------------------------------------------------------
249
+
250
+ -- Analyze tables after index creation
251
+ ANALYZE claude_flow.vectors;
252
+ ANALYZE claude_flow.embeddings;
253
+ ANALYZE claude_flow.attention_cache;
254
+ ANALYZE claude_flow.gnn_cache;
255
+ ANALYZE claude_flow.hyperbolic_embeddings;
256
+ ANALYZE claude_flow.collections;
257
+
258
+ -- ----------------------------------------------------------------------------
259
+ -- Record migration
260
+ -- ----------------------------------------------------------------------------
261
+ INSERT INTO claude_flow.migrations (name, checksum)
262
+ VALUES ('003_create_indices', md5('003_create_indices'))
263
+ ON CONFLICT (name) DO NOTHING;
264
+
265
+ COMMIT;
266
+
267
+ -- ============================================================================
268
+ -- Rollback Script
269
+ -- ============================================================================
270
+ -- BEGIN;
271
+ -- -- Drop composite indices
272
+ -- DROP INDEX IF EXISTS claude_flow.idx_embeddings_ns_importance;
273
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_ns_coll_created;
274
+ --
275
+ -- -- Drop partial indices
276
+ -- DROP INDEX IF EXISTS claude_flow.idx_embeddings_high_importance;
277
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_active;
278
+ --
279
+ -- -- Drop access pattern indices
280
+ -- DROP INDEX IF EXISTS claude_flow.idx_gnn_cache_hit_count;
281
+ -- DROP INDEX IF EXISTS claude_flow.idx_attention_cache_hit_count;
282
+ -- DROP INDEX IF EXISTS claude_flow.idx_embeddings_access_count;
283
+ --
284
+ -- -- Drop hierarchy indices
285
+ -- DROP INDEX IF EXISTS claude_flow.idx_hyperbolic_depth;
286
+ -- DROP INDEX IF EXISTS claude_flow.idx_hyperbolic_parent;
287
+ --
288
+ -- -- Drop hash indices
289
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_source;
290
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_hash;
291
+ --
292
+ -- -- Drop timestamp indices
293
+ -- DROP INDEX IF EXISTS claude_flow.idx_collections_created_at;
294
+ -- DROP INDEX IF EXISTS claude_flow.idx_hyperbolic_created_at;
295
+ -- DROP INDEX IF EXISTS claude_flow.idx_gnn_cache_last_accessed;
296
+ -- DROP INDEX IF EXISTS claude_flow.idx_gnn_cache_created_at;
297
+ -- DROP INDEX IF EXISTS claude_flow.idx_attention_cache_last_accessed;
298
+ -- DROP INDEX IF EXISTS claude_flow.idx_attention_cache_created_at;
299
+ -- DROP INDEX IF EXISTS claude_flow.idx_embeddings_last_accessed;
300
+ -- DROP INDEX IF EXISTS claude_flow.idx_embeddings_created_at;
301
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_expires_at;
302
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_updated_at;
303
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_created_at;
304
+ --
305
+ -- -- Drop namespace indices
306
+ -- DROP INDEX IF EXISTS claude_flow.idx_collections_namespace;
307
+ -- DROP INDEX IF EXISTS claude_flow.idx_hyperbolic_namespace;
308
+ -- DROP INDEX IF EXISTS claude_flow.idx_embeddings_tags_gin;
309
+ -- DROP INDEX IF EXISTS claude_flow.idx_embeddings_namespace;
310
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_namespace;
311
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_namespace_collection;
312
+ --
313
+ -- -- Drop GIN indices
314
+ -- DROP INDEX IF EXISTS claude_flow.idx_collections_metadata_gin;
315
+ -- DROP INDEX IF EXISTS claude_flow.idx_hyperbolic_metadata_gin;
316
+ -- DROP INDEX IF EXISTS claude_flow.idx_gnn_cache_metadata_gin;
317
+ -- DROP INDEX IF EXISTS claude_flow.idx_attention_cache_metadata_gin;
318
+ -- DROP INDEX IF EXISTS claude_flow.idx_embeddings_metadata_gin;
319
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_metadata_gin;
320
+ --
321
+ -- -- Drop HNSW indices
322
+ -- DROP INDEX IF EXISTS claude_flow.idx_embeddings_embedding_hnsw_cosine;
323
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_embedding_hnsw_ip;
324
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_embedding_hnsw_l2;
325
+ -- DROP INDEX IF EXISTS claude_flow.idx_vectors_embedding_hnsw_cosine;
326
+ --
327
+ -- DELETE FROM claude_flow.migrations WHERE name = '003_create_indices';
328
+ -- COMMIT;