@props-labs/mesh-os 0.1.20 → 0.2.0

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 (44) hide show
  1. package/dist/core/__fixtures__/mock_responses.d.ts +318 -0
  2. package/dist/core/__fixtures__/mock_responses.js +333 -0
  3. package/dist/core/__fixtures__/sample_embeddings.d.ts +33 -0
  4. package/dist/core/__fixtures__/sample_embeddings.js +12355 -0
  5. package/dist/core/agents.d.ts +51 -0
  6. package/dist/core/agents.js +170 -0
  7. package/dist/core/client.d.ts +3 -1
  8. package/dist/core/client.js +10 -34
  9. package/dist/core/memories.d.ts +138 -0
  10. package/dist/core/memories.js +417 -0
  11. package/dist/core/taxonomy.d.ts +44 -0
  12. package/dist/core/taxonomy.js +25 -1
  13. package/dist/core/workflows.d.ts +104 -0
  14. package/dist/core/workflows.js +332 -0
  15. package/package.json +3 -3
  16. package/src/templates/hasura/metadata/actions.yaml +6 -0
  17. package/src/templates/hasura/metadata/cron_triggers.yaml +1 -0
  18. package/src/templates/hasura/metadata/databases/databases.yaml +1 -1
  19. package/src/templates/hasura/metadata/databases/default/functions/functions.yaml +80 -0
  20. package/src/templates/hasura/metadata/databases/default/tables/tables.yaml +274 -9
  21. package/src/templates/hasura/metadata/query_collections.yaml +1 -0
  22. package/src/templates/hasura/metadata/rest_endpoints.yaml +1 -0
  23. package/src/templates/hasura/migrations/default/0_cleanup/down.sql +2 -0
  24. package/src/templates/hasura/migrations/default/0_cleanup/up.sql +59 -0
  25. package/src/templates/hasura/migrations/default/1_init/down.sql +27 -21
  26. package/src/templates/hasura/migrations/default/1_init/up.sql +446 -174
  27. package/src/templates/hasura/migrations/default/2_sample_data/down.sql +3 -0
  28. package/src/templates/hasura/migrations/default/2_sample_data/up.sql +288 -0
  29. package/src/templates/hasura/migrations/default/3_agent_relations/down.sql +76 -0
  30. package/src/templates/hasura/migrations/default/3_agent_relations/up.sql +469 -0
  31. package/src/templates/hasura/metadata/config.yaml +0 -1
  32. package/src/templates/hasura/metadata/databases/default/tables/public_agents.yaml +0 -14
  33. package/src/templates/hasura/metadata/databases/default/tables/public_memories.yaml +0 -23
  34. package/src/templates/hasura/metadata/databases/default/tables/public_memory_edges.yaml +0 -57
  35. package/src/templates/hasura/metadata/databases/default/tables/track_tables.yaml +0 -14
  36. package/src/templates/hasura/metadata/metadata.json +0 -80
  37. package/src/templates/hasura/migrations/default/2_metadata_filtering/down.sql +0 -4
  38. package/src/templates/hasura/migrations/default/2_metadata_filtering/up.sql +0 -44
  39. package/src/templates/hasura/migrations/default/3_memory_expiry/down.sql +0 -55
  40. package/src/templates/hasura/migrations/default/3_memory_expiry/up.sql +0 -108
  41. package/src/templates/hasura/migrations/default/4_remove_slug_validation/down.sql +0 -20
  42. package/src/templates/hasura/migrations/default/4_remove_slug_validation/up.sql +0 -5
  43. package/src/templates/hasura/migrations/default/5_entities/down.sql +0 -13
  44. package/src/templates/hasura/migrations/default/5_entities/up.sql +0 -155
@@ -2,7 +2,25 @@
2
2
  CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
3
3
  CREATE EXTENSION IF NOT EXISTS "vector";
4
4
 
5
- -- Create function to validate slugs
5
+ -- Create helper functions first
6
+ CREATE OR REPLACE FUNCTION update_updated_at_column()
7
+ RETURNS TRIGGER AS $$
8
+ BEGIN
9
+ NEW.updated_at = CURRENT_TIMESTAMP;
10
+ RETURN NEW;
11
+ END;
12
+ $$ language 'plpgsql';
13
+
14
+ CREATE OR REPLACE FUNCTION normalize_embedding()
15
+ RETURNS TRIGGER AS $$
16
+ BEGIN
17
+ IF NEW.embedding IS NOT NULL THEN
18
+ NEW.embedding = l2_normalize(NEW.embedding);
19
+ END IF;
20
+ RETURN NEW;
21
+ END;
22
+ $$ LANGUAGE plpgsql;
23
+
6
24
  CREATE OR REPLACE FUNCTION validate_slug(slug text)
7
25
  RETURNS boolean AS $$
8
26
  BEGIN
@@ -10,209 +28,463 @@ BEGIN
10
28
  END;
11
29
  $$ LANGUAGE plpgsql IMMUTABLE;
12
30
 
13
- -- Create agents table
14
- CREATE TABLE IF NOT EXISTS public.agents (
31
+ -- Create type_schemas table
32
+ CREATE TABLE IF NOT EXISTS type_schemas (
33
+ type TEXT PRIMARY KEY,
34
+ schema JSONB NOT NULL,
35
+ metadata_schema JSONB,
36
+ embedding_config JSONB,
37
+ chunking_config JSONB,
38
+ validation_rules JSONB,
39
+ behaviors JSONB,
40
+ created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
41
+ updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
42
+ );
43
+
44
+ -- Create workflow_schemas table
45
+ CREATE TABLE IF NOT EXISTS workflow_schemas (
46
+ type TEXT PRIMARY KEY,
47
+ input_schema JSONB NOT NULL,
48
+ output_schema JSONB NOT NULL,
49
+ metadata_schema JSONB,
50
+ validation_rules JSONB,
51
+ behaviors JSONB,
52
+ created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
53
+ updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
54
+ );
55
+
56
+ -- Create workflow_runs table
57
+ CREATE TABLE IF NOT EXISTS workflow_runs (
15
58
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
16
- slug TEXT UNIQUE,
17
- name TEXT NOT NULL,
18
- description TEXT,
19
- metadata JSONB,
20
- status TEXT NOT NULL DEFAULT 'active',
21
- created_at TIMESTAMPTZ DEFAULT NOW(),
22
- updated_at TIMESTAMPTZ DEFAULT NOW(),
23
- CONSTRAINT valid_slug CHECK (slug IS NULL OR validate_slug(slug))
59
+ type TEXT NOT NULL REFERENCES workflow_schemas(type),
60
+ status TEXT NOT NULL DEFAULT 'pending',
61
+ input JSONB NOT NULL,
62
+ metadata JSONB DEFAULT '{}'::jsonb,
63
+ created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
64
+ updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
65
+
66
+ CONSTRAINT valid_workflow_status CHECK (status IN ('pending', 'running', 'completed', 'failed', 'cancelled'))
67
+ );
68
+
69
+ -- Create workflow_results table
70
+ CREATE TABLE IF NOT EXISTS workflow_results (
71
+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
72
+ workflow_id UUID NOT NULL REFERENCES workflow_runs(id) ON DELETE CASCADE,
73
+ type TEXT NOT NULL DEFAULT 'final',
74
+ result JSONB NOT NULL,
75
+ metadata JSONB DEFAULT '{}'::jsonb,
76
+ created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
77
+ updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
78
+
79
+ CONSTRAINT valid_result_type CHECK (type IN ('interim', 'final'))
24
80
  );
25
81
 
26
82
  -- Create memories table
27
- CREATE TABLE IF NOT EXISTS public.memories (
83
+ CREATE TABLE IF NOT EXISTS memories (
28
84
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
29
- agent_id UUID REFERENCES public.agents(id) ON DELETE CASCADE,
85
+ type TEXT NOT NULL REFERENCES type_schemas(type),
86
+ status TEXT NOT NULL DEFAULT 'active',
87
+ metadata JSONB DEFAULT '{}'::jsonb,
30
88
  content TEXT NOT NULL,
31
- metadata JSONB,
32
- embedding vector(1536), -- OpenAI text-embedding-3-small dimension
33
- created_at TIMESTAMPTZ DEFAULT NOW(),
34
- updated_at TIMESTAMPTZ DEFAULT NOW()
89
+ created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
90
+ updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
91
+
92
+ CONSTRAINT valid_memory_status CHECK (status IN ('active', 'archived', 'deleted'))
35
93
  );
36
94
 
37
- -- Create memory edges table
38
- CREATE TABLE IF NOT EXISTS public.memory_edges (
95
+ -- Create memory_chunks table
96
+ CREATE TABLE IF NOT EXISTS memory_chunks (
39
97
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
40
- source_memory UUID REFERENCES public.memories(id) ON DELETE CASCADE,
41
- target_memory UUID REFERENCES public.memories(id) ON DELETE CASCADE,
42
- relationship TEXT NOT NULL,
98
+ memory_id UUID NOT NULL REFERENCES memories(id) ON DELETE CASCADE,
99
+ chunk_index INTEGER NOT NULL,
100
+ content TEXT NOT NULL,
101
+ embedding vector(1536),
102
+ metadata JSONB DEFAULT '{}'::jsonb,
103
+ created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
104
+ updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
105
+
106
+ -- Ensure ordering within a memory
107
+ UNIQUE (memory_id, chunk_index)
108
+ );
109
+
110
+ -- Create memory_edges table
111
+ CREATE TABLE IF NOT EXISTS memory_edges (
112
+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
113
+ source_memory UUID NOT NULL REFERENCES memories(id) ON DELETE CASCADE,
114
+ target_memory UUID NOT NULL REFERENCES memories(id) ON DELETE CASCADE,
115
+ type TEXT NOT NULL,
116
+ metadata JSONB DEFAULT '{}'::jsonb,
43
117
  weight FLOAT DEFAULT 1.0,
44
- created_at TIMESTAMPTZ DEFAULT NOW()
118
+ created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
119
+ updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
45
120
  );
46
121
 
47
- -- Create index for vector similarity search
48
- CREATE INDEX IF NOT EXISTS idx_memories_embedding ON public.memories
49
- USING ivfflat (embedding vector_cosine_ops)
50
- WITH (lists = 100);
122
+ -- Create all triggers safely
123
+ DO $$
124
+ DECLARE
125
+ trigger_record record;
126
+ BEGIN
127
+ -- First drop any existing user-defined triggers (excluding RI triggers)
128
+ FOR trigger_record IN
129
+ SELECT
130
+ tgname as trigger_name,
131
+ relname as table_name
132
+ FROM pg_trigger t
133
+ JOIN pg_class c ON t.tgrelid = c.oid
134
+ JOIN pg_namespace n ON c.relnamespace = n.oid
135
+ WHERE n.nspname = 'public'
136
+ AND NOT tgname LIKE 'RI_ConstraintTrigger_%' -- Exclude referential integrity triggers
137
+ AND NOT tgname LIKE 'pg_trigger_%' -- Exclude other system triggers
138
+ LOOP
139
+ EXECUTE format('DROP TRIGGER IF EXISTS %I ON public.%I;',
140
+ trigger_record.trigger_name,
141
+ trigger_record.table_name
142
+ );
143
+ END LOOP;
51
144
 
52
- -- Create index for memory edges lookup
53
- CREATE INDEX IF NOT EXISTS idx_memory_edges_source ON public.memory_edges(source_memory);
54
- CREATE INDEX IF NOT EXISTS idx_memory_edges_target ON public.memory_edges(target_memory);
145
+ -- Now create all triggers
146
+ -- Type Schemas
147
+ IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_type_schemas_updated_at') THEN
148
+ CREATE TRIGGER update_type_schemas_updated_at
149
+ BEFORE UPDATE ON type_schemas
150
+ FOR EACH ROW
151
+ EXECUTE FUNCTION update_updated_at_column();
152
+ END IF;
55
153
 
56
- -- Create function to update updated_at timestamp
57
- CREATE OR REPLACE FUNCTION update_updated_at_column()
58
- RETURNS TRIGGER AS $$
59
- BEGIN
60
- NEW.updated_at = NOW();
61
- RETURN NEW;
62
- END;
63
- $$ language 'plpgsql';
154
+ -- Workflow Schemas
155
+ IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_workflow_schemas_updated_at') THEN
156
+ CREATE TRIGGER update_workflow_schemas_updated_at
157
+ BEFORE UPDATE ON workflow_schemas
158
+ FOR EACH ROW
159
+ EXECUTE FUNCTION update_updated_at_column();
160
+ END IF;
64
161
 
65
- -- Create triggers for updated_at
66
- CREATE TRIGGER update_agents_updated_at
67
- BEFORE UPDATE ON public.agents
68
- FOR EACH ROW
69
- EXECUTE FUNCTION update_updated_at_column();
162
+ -- Workflow Runs
163
+ IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_workflow_runs_updated_at') THEN
164
+ CREATE TRIGGER update_workflow_runs_updated_at
165
+ BEFORE UPDATE ON workflow_runs
166
+ FOR EACH ROW
167
+ EXECUTE FUNCTION update_updated_at_column();
168
+ END IF;
70
169
 
71
- CREATE TRIGGER update_memories_updated_at
72
- BEFORE UPDATE ON public.memories
73
- FOR EACH ROW
74
- EXECUTE FUNCTION update_updated_at_column();
170
+ -- Workflow Results
171
+ IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_workflow_results_updated_at') THEN
172
+ CREATE TRIGGER update_workflow_results_updated_at
173
+ BEFORE UPDATE ON workflow_results
174
+ FOR EACH ROW
175
+ EXECUTE FUNCTION update_updated_at_column();
176
+ END IF;
75
177
 
76
- -- Create a view for memories with similarity
77
- CREATE OR REPLACE VIEW public.memories_with_similarity AS
78
- SELECT
79
- m.*,
80
- 0::float8 as similarity -- Default similarity, will be replaced in search
81
- FROM memories m;
178
+ -- Memories
179
+ IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_memories_updated_at') THEN
180
+ CREATE TRIGGER update_memories_updated_at
181
+ BEFORE UPDATE ON memories
182
+ FOR EACH ROW
183
+ EXECUTE FUNCTION update_updated_at_column();
184
+ END IF;
82
185
 
83
- -- Add function to normalize embeddings
84
- CREATE OR REPLACE FUNCTION normalize_embedding()
85
- RETURNS TRIGGER AS $$
186
+ -- Memory Chunks
187
+ IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_memory_chunks_updated_at') THEN
188
+ CREATE TRIGGER update_memory_chunks_updated_at
189
+ BEFORE UPDATE ON memory_chunks
190
+ FOR EACH ROW
191
+ EXECUTE FUNCTION update_updated_at_column();
192
+ END IF;
193
+
194
+ IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'normalize_memory_chunk_embedding') THEN
195
+ CREATE TRIGGER normalize_memory_chunk_embedding
196
+ BEFORE INSERT OR UPDATE OF embedding ON memory_chunks
197
+ FOR EACH ROW
198
+ EXECUTE FUNCTION normalize_embedding();
199
+ END IF;
200
+
201
+ -- Memory Edges
202
+ IF NOT EXISTS (SELECT 1 FROM pg_trigger WHERE tgname = 'update_memory_edges_updated_at') THEN
203
+ CREATE TRIGGER update_memory_edges_updated_at
204
+ BEFORE UPDATE ON memory_edges
205
+ FOR EACH ROW
206
+ EXECUTE FUNCTION update_updated_at_column();
207
+ END IF;
208
+ END $$;
209
+
210
+ -- Create indexes safely
211
+ DO $$
86
212
  BEGIN
87
- IF NEW.embedding IS NOT NULL THEN
88
- -- Normalize the embedding vector using l2_normalize
89
- NEW.embedding = l2_normalize(NEW.embedding);
213
+ -- Drop existing indexes first
214
+ DROP INDEX IF EXISTS idx_memory_chunks_embedding;
215
+ DROP INDEX IF EXISTS idx_memory_edges_source;
216
+ DROP INDEX IF EXISTS idx_memory_edges_target;
217
+ DROP INDEX IF EXISTS idx_memory_edges_type;
218
+
219
+ -- Create new indexes
220
+ IF NOT EXISTS (
221
+ SELECT 1 FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
222
+ WHERE c.relname = 'idx_memory_chunks_embedding' AND n.nspname = 'public'
223
+ ) THEN
224
+ CREATE INDEX idx_memory_chunks_embedding
225
+ ON memory_chunks USING ivfflat (embedding vector_cosine_ops)
226
+ WITH (lists = 100);
90
227
  END IF;
91
- RETURN NEW;
92
- END;
93
- $$ LANGUAGE plpgsql;
94
228
 
95
- -- Create trigger to normalize embeddings on insert and update
96
- CREATE TRIGGER normalize_memory_embedding
97
- BEFORE INSERT OR UPDATE OF embedding ON public.memories
98
- FOR EACH ROW
99
- EXECUTE FUNCTION normalize_embedding();
100
-
101
- -- Add a debug function to check vector normalization
102
- CREATE OR REPLACE FUNCTION debug_vector_info(v vector(1536))
103
- RETURNS TABLE (
104
- original_norm float8,
105
- normalized_norm float8,
106
- is_normalized boolean
107
- ) AS $$
108
- SELECT
109
- sqrt(v <-> v) as original_norm,
110
- sqrt(l2_normalize(v) <-> l2_normalize(v)) as normalized_norm,
111
- abs(1 - sqrt(v <-> v)) < 0.000001 as is_normalized;
112
- $$ LANGUAGE SQL IMMUTABLE;
113
-
114
- -- Modify the search function to work with normalized embeddings
115
- CREATE OR REPLACE FUNCTION public.search_memories(
116
- query_embedding vector(1536),
117
- match_threshold float8,
229
+ IF NOT EXISTS (
230
+ SELECT 1 FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
231
+ WHERE c.relname = 'idx_memory_edges_source' AND n.nspname = 'public'
232
+ ) THEN
233
+ CREATE INDEX idx_memory_edges_source ON memory_edges(source_memory);
234
+ END IF;
235
+
236
+ IF NOT EXISTS (
237
+ SELECT 1 FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
238
+ WHERE c.relname = 'idx_memory_edges_target' AND n.nspname = 'public'
239
+ ) THEN
240
+ CREATE INDEX idx_memory_edges_target ON memory_edges(target_memory);
241
+ END IF;
242
+
243
+ IF NOT EXISTS (
244
+ SELECT 1 FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
245
+ WHERE c.relname = 'idx_memory_edges_type' AND n.nspname = 'public'
246
+ ) THEN
247
+ CREATE INDEX idx_memory_edges_type ON memory_edges(type);
248
+ END IF;
249
+ END $$;
250
+
251
+ -- Create views for search results
252
+ CREATE OR REPLACE VIEW memory_connections_with_details AS
253
+ SELECT
254
+ e.source_memory as source_id,
255
+ e.target_memory as target_id,
256
+ e.type as relationship,
257
+ e.weight,
258
+ e.metadata,
259
+ e.created_at,
260
+ e.updated_at,
261
+ source_mem.content as source_content,
262
+ target_mem.content as target_content,
263
+ source_mem.type as source_type,
264
+ target_mem.type as target_type
265
+ FROM memory_edges e
266
+ JOIN memories source_mem ON e.source_memory = source_mem.id
267
+ JOIN memories target_mem ON e.target_memory = target_mem.id;
268
+
269
+ -- Create views for search results
270
+ CREATE OR REPLACE VIEW memory_chunks_with_details AS
271
+ SELECT
272
+ mc.id as chunk_id,
273
+ mc.memory_id,
274
+ mc.chunk_index,
275
+ mc.content as chunk_content,
276
+ mc.embedding,
277
+ mc.metadata as chunk_metadata,
278
+ mc.created_at as chunk_created_at,
279
+ mc.updated_at as chunk_updated_at,
280
+ m.content as memory_content,
281
+ m.type as memory_type,
282
+ m.status as memory_status,
283
+ m.metadata as memory_metadata,
284
+ m.created_at as memory_created_at,
285
+ m.updated_at as memory_updated_at,
286
+ m.agent_id
287
+ FROM memory_chunks mc
288
+ JOIN memories m ON mc.memory_id = m.id;
289
+
290
+ -- Create view for memory embeddings info
291
+ CREATE OR REPLACE VIEW memory_embeddings_info AS
292
+ SELECT
293
+ id as memory_id,
294
+ content,
295
+ sqrt(embedding <-> embedding) as embedding_norm,
296
+ abs(1 - sqrt(embedding <-> embedding)) < 0.000001 as is_normalized
297
+ FROM memory_chunks
298
+ WHERE embedding IS NOT NULL;
299
+
300
+ -- Create view for search results
301
+ CREATE OR REPLACE VIEW memory_search_results AS
302
+ SELECT
303
+ chunk_id,
304
+ memory_id,
305
+ chunk_index,
306
+ chunk_content,
307
+ chunk_metadata,
308
+ chunk_created_at,
309
+ chunk_updated_at,
310
+ memory_content,
311
+ memory_type,
312
+ memory_status,
313
+ memory_metadata,
314
+ memory_created_at,
315
+ memory_updated_at,
316
+ agent_id,
317
+ 0.0::float as similarity
318
+ FROM memory_chunks_with_details
319
+ WHERE false; -- Empty by default, will be populated by function
320
+
321
+ -- Create helper functions
322
+ CREATE OR REPLACE FUNCTION get_connected_memories(
323
+ memory_id uuid,
324
+ relationship_type text DEFAULT NULL,
325
+ max_depth integer DEFAULT 1
326
+ )
327
+ RETURNS SETOF memory_connections_with_details
328
+ LANGUAGE sql STABLE AS $$
329
+ WITH RECURSIVE memory_graph AS (
330
+ -- Base case
331
+ SELECT
332
+ source_memory,
333
+ target_memory,
334
+ type as relationship,
335
+ weight,
336
+ metadata,
337
+ created_at,
338
+ updated_at,
339
+ 1 as depth
340
+ FROM memory_edges
341
+ WHERE
342
+ (source_memory = memory_id OR target_memory = memory_id)
343
+ AND (relationship_type IS NULL OR type = relationship_type)
344
+
345
+ UNION
346
+
347
+ -- Recursive case
348
+ SELECT
349
+ e.source_memory,
350
+ e.target_memory,
351
+ e.type,
352
+ e.weight,
353
+ e.metadata,
354
+ e.created_at,
355
+ e.updated_at,
356
+ g.depth + 1
357
+ FROM memory_edges e
358
+ INNER JOIN memory_graph g ON
359
+ (e.source_memory = g.target_memory OR e.target_memory = g.source_memory)
360
+ WHERE
361
+ g.depth < max_depth
362
+ AND (relationship_type IS NULL OR e.type = relationship_type)
363
+ )
364
+ SELECT DISTINCT
365
+ mc.*
366
+ FROM memory_graph g
367
+ JOIN memory_connections_with_details mc ON
368
+ (mc.source_id = g.source_memory AND mc.target_id = g.target_memory);
369
+ $$;
370
+
371
+ CREATE OR REPLACE FUNCTION inspect_memory_embeddings()
372
+ RETURNS SETOF memory_embeddings_info
373
+ LANGUAGE sql STABLE AS $$
374
+ SELECT * FROM memory_embeddings_info;
375
+ $$;
376
+
377
+ CREATE OR REPLACE FUNCTION search_memory_chunks(
378
+ query_embedding vector,
379
+ match_threshold double precision,
118
380
  match_count integer,
119
- filter_agent_id uuid DEFAULT NULL
381
+ filter_agent_id uuid DEFAULT NULL::uuid,
382
+ memory_metadata_filter jsonb DEFAULT NULL::jsonb,
383
+ chunk_metadata_filter jsonb DEFAULT NULL::jsonb,
384
+ created_at_filter jsonb DEFAULT NULL::jsonb
120
385
  )
121
- RETURNS SETOF public.memories_with_similarity
122
- LANGUAGE sql
123
- STABLE
124
- AS $$
386
+ RETURNS SETOF memory_search_results
387
+ LANGUAGE sql STABLE AS $$
125
388
  WITH normalized_query AS (
126
389
  SELECT l2_normalize(query_embedding) AS normalized_vector
127
390
  )
128
- SELECT
129
- m.id,
130
- m.agent_id,
131
- m.content,
132
- m.metadata,
133
- m.embedding,
134
- m.created_at,
135
- m.updated_at,
136
- -(m.embedding <#> (SELECT normalized_vector FROM normalized_query)) as similarity
137
- FROM memories m
391
+ SELECT
392
+ cd.chunk_id,
393
+ cd.memory_id,
394
+ cd.chunk_index,
395
+ cd.chunk_content,
396
+ cd.chunk_metadata,
397
+ cd.chunk_created_at,
398
+ cd.chunk_updated_at,
399
+ cd.memory_content,
400
+ cd.memory_type,
401
+ cd.memory_status,
402
+ cd.memory_metadata,
403
+ cd.memory_created_at,
404
+ cd.memory_updated_at,
405
+ cd.agent_id,
406
+ -(cd.embedding <#> (SELECT normalized_vector FROM normalized_query)) as similarity
407
+ FROM memory_chunks_with_details cd
138
408
  WHERE
139
- CASE
140
- WHEN filter_agent_id IS NOT NULL THEN m.agent_id = filter_agent_id
409
+ CASE
410
+ WHEN filter_agent_id IS NOT NULL THEN cd.agent_id = filter_agent_id
411
+ ELSE TRUE
412
+ END
413
+ AND CASE
414
+ WHEN memory_metadata_filter IS NOT NULL THEN cd.memory_metadata @> memory_metadata_filter
415
+ ELSE TRUE
416
+ END
417
+ AND CASE
418
+ WHEN chunk_metadata_filter IS NOT NULL THEN cd.chunk_metadata @> chunk_metadata_filter
141
419
  ELSE TRUE
142
420
  END
143
- -- Re-enable threshold with corrected sign
144
- AND -(m.embedding <#> (SELECT normalized_vector FROM normalized_query)) >= match_threshold
145
- ORDER BY -(m.embedding <#> (SELECT normalized_vector FROM normalized_query)) DESC
421
+ AND CASE
422
+ WHEN created_at_filter IS NOT NULL THEN (
423
+ CASE
424
+ WHEN created_at_filter ? '_gt' THEN cd.chunk_created_at > (created_at_filter->>'_gt')::timestamptz AT TIME ZONE 'UTC'
425
+ ELSE TRUE
426
+ END
427
+ AND CASE
428
+ WHEN created_at_filter ? '_gte' THEN cd.chunk_created_at >= (created_at_filter->>'_gte')::timestamptz AT TIME ZONE 'UTC'
429
+ ELSE TRUE
430
+ END
431
+ AND CASE
432
+ WHEN created_at_filter ? '_lt' THEN cd.chunk_created_at < (created_at_filter->>'_lt')::timestamptz AT TIME ZONE 'UTC'
433
+ ELSE TRUE
434
+ END
435
+ AND CASE
436
+ WHEN created_at_filter ? '_lte' THEN cd.chunk_created_at <= (created_at_filter->>'_lte')::timestamptz AT TIME ZONE 'UTC'
437
+ ELSE TRUE
438
+ END
439
+ AND CASE
440
+ WHEN created_at_filter ? '_eq' THEN cd.chunk_created_at = (created_at_filter->>'_eq')::timestamptz AT TIME ZONE 'UTC'
441
+ ELSE TRUE
442
+ END
443
+ AND CASE
444
+ WHEN created_at_filter ? '_is_null' AND (created_at_filter->>'_is_null')::boolean THEN cd.chunk_created_at IS NULL
445
+ WHEN created_at_filter ? '_is_null' AND NOT (created_at_filter->>'_is_null')::boolean THEN cd.chunk_created_at IS NOT NULL
446
+ ELSE TRUE
447
+ END
448
+ )
449
+ ELSE TRUE
450
+ END
451
+ AND cd.embedding IS NOT NULL
452
+ AND -(cd.embedding <#> (SELECT normalized_vector FROM normalized_query)) >= match_threshold
453
+ ORDER BY
454
+ -(cd.embedding <#> (SELECT normalized_vector FROM normalized_query)) DESC
146
455
  LIMIT match_count;
147
456
  $$;
148
457
 
149
- -- Track the function in Hasura
150
- COMMENT ON FUNCTION public.search_memories IS E'@graphql({"type": "Query"})';
458
+ -- Add helpful comments
459
+ COMMENT ON TABLE type_schemas IS
460
+ 'Defines valid types for memories and their associated schemas and configurations.';
151
461
 
152
- -- Create function to get connected memories
153
- CREATE OR REPLACE FUNCTION get_connected_memories(
154
- memory_id uuid,
155
- relationship_type text DEFAULT NULL,
156
- max_depth integer DEFAULT 1
157
- )
158
- RETURNS TABLE (
159
- source_id UUID,
160
- target_id UUID,
161
- relationship TEXT,
162
- weight FLOAT,
163
- depth INTEGER
164
- ) AS $$
165
- WITH RECURSIVE memory_graph AS (
166
- -- Base case
167
- SELECT
168
- source_memory,
169
- target_memory,
170
- relationship,
171
- weight,
172
- 1 as depth
173
- FROM public.memory_edges
174
- WHERE
175
- (source_memory = memory_id OR target_memory = memory_id)
176
- AND (relationship_type IS NULL OR relationship = relationship_type)
177
-
178
- UNION
179
-
180
- -- Recursive case
181
- SELECT
182
- e.source_memory,
183
- e.target_memory,
184
- e.relationship,
185
- e.weight,
186
- g.depth + 1
187
- FROM public.memory_edges e
188
- INNER JOIN memory_graph g ON
189
- (e.source_memory = g.target_memory OR e.target_memory = g.source_memory)
190
- WHERE
191
- g.depth < max_depth
192
- AND (relationship_type IS NULL OR e.relationship = relationship_type)
193
- )
194
- SELECT DISTINCT
195
- source_memory as source_id,
196
- target_memory as target_id,
197
- relationship,
198
- weight,
199
- depth
200
- FROM memory_graph;
201
- $$ LANGUAGE SQL STABLE;
202
-
203
- -- Add a function to inspect memory embeddings
204
- CREATE OR REPLACE FUNCTION inspect_memory_embeddings()
205
- RETURNS TABLE (
206
- memory_id UUID,
207
- content TEXT,
208
- embedding_norm float8,
209
- is_normalized boolean
210
- ) AS $$
211
- SELECT
212
- id,
213
- content,
214
- sqrt(embedding <-> embedding) as embedding_norm,
215
- abs(1 - sqrt(embedding <-> embedding)) < 0.000001 as is_normalized
216
- FROM memories
217
- WHERE embedding IS NOT NULL;
218
- $$ LANGUAGE SQL STABLE;
462
+ COMMENT ON TABLE workflow_schemas IS
463
+ 'Defines valid workflow types and their input/output schemas.';
464
+
465
+ COMMENT ON TABLE workflow_runs IS
466
+ 'Records of workflow executions with their inputs and status.';
467
+
468
+ COMMENT ON TABLE workflow_results IS
469
+ 'Results of completed workflow runs.';
470
+
471
+ COMMENT ON TABLE memories IS
472
+ 'Core memory storage with type validation against type_schemas.';
473
+
474
+ COMMENT ON TABLE memory_chunks IS
475
+ 'Chunked content from memories with vector embeddings for semantic search.';
476
+
477
+ COMMENT ON TABLE memory_edges IS
478
+ 'Relationships between memories with type and weight.';
479
+
480
+ COMMENT ON VIEW memory_embeddings_info IS
481
+ 'View providing information about memory chunk embeddings including normalization status.';
482
+
483
+ COMMENT ON FUNCTION inspect_memory_embeddings IS
484
+ 'Inspect memory chunk embeddings to verify normalization and get embedding norms.';
485
+
486
+ COMMENT ON VIEW memory_chunks_with_details IS
487
+ 'View combining memory chunks with their parent memory details for search results.';
488
+
489
+ COMMENT ON FUNCTION search_memory_chunks IS
490
+ 'Search for similar memory chunks and return both chunk and memory details with similarity scores.';
@@ -0,0 +1,3 @@
1
+ -- Remove sample data in reverse order
2
+ DELETE FROM workflow_schemas WHERE type IN ('document_analysis', 'code_review');
3
+ DELETE FROM type_schemas WHERE type IN ('text_document', 'code_snippet');