@props-labs/mesh-os 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ - name: default
2
+ kind: postgres
3
+ configuration:
4
+ connection_info:
5
+ database_url:
6
+ from_env: HASURA_GRAPHQL_DATABASE_URL
7
+ isolation_level: read-committed
8
+ pool_settings:
9
+ connection_lifetime: 600
10
+ idle_timeout: 180
11
+ max_connections: 50
12
+ retries: 1
13
+ use_prepared_statements: true
14
+ tables: "!include default/tables/tables.yaml"
15
+ functions: []
@@ -0,0 +1,14 @@
1
+ table:
2
+ name: agents
3
+ schema: public
4
+ configuration:
5
+ custom_root_fields: {}
6
+ custom_name: agents
7
+ array_relationships:
8
+ - name: memories
9
+ using:
10
+ foreign_key_constraint_on:
11
+ column: agent_id
12
+ table:
13
+ name: memories
14
+ schema: public
@@ -0,0 +1,17 @@
1
+ table:
2
+ name: memories
3
+ schema: public
4
+ configuration:
5
+ custom_root_fields: {}
6
+ custom_name: memories
7
+ object_relationships:
8
+ - name: agent
9
+ using:
10
+ foreign_key_constraint_on: agent_id
11
+ computed_fields:
12
+ - name: similarity_score
13
+ definition:
14
+ function:
15
+ name: search_memories
16
+ schema: public
17
+ comment: Computes similarity score for vector search
@@ -0,0 +1,57 @@
1
+ table:
2
+ name: memory_edges
3
+ schema: public
4
+ is_enum: false
5
+ configuration:
6
+ column_config: {}
7
+ custom_column_names: {}
8
+ custom_name: memory_edges
9
+ custom_root_fields: {}
10
+
11
+ array_relationships: []
12
+ computed_fields: []
13
+ delete_permissions:
14
+ - role: admin
15
+ permission:
16
+ filter: {}
17
+
18
+ event_triggers: []
19
+ insert_permissions:
20
+ - role: admin
21
+ permission:
22
+ check: {}
23
+ columns:
24
+ - id
25
+ - source_memory
26
+ - target_memory
27
+ - relationship
28
+ - weight
29
+ - created_at
30
+
31
+ object_relationships:
32
+ - name: source
33
+ using:
34
+ foreign_key_constraint_on: source_memory
35
+ - name: target
36
+ using:
37
+ foreign_key_constraint_on: target_memory
38
+
39
+ select_permissions:
40
+ - role: admin
41
+ permission:
42
+ columns:
43
+ - id
44
+ - source_memory
45
+ - target_memory
46
+ - relationship
47
+ - weight
48
+ - created_at
49
+ filter: {}
50
+
51
+ update_permissions:
52
+ - role: admin
53
+ permission:
54
+ columns:
55
+ - weight
56
+ filter: {}
57
+ check: {}
@@ -0,0 +1,54 @@
1
+ - table:
2
+ name: agents
3
+ schema: public
4
+ configuration:
5
+ custom_root_fields: {}
6
+ custom_name: agents
7
+ array_relationships:
8
+ - name: memories
9
+ using:
10
+ foreign_key_constraint_on:
11
+ column: agent_id
12
+ table:
13
+ name: memories
14
+ schema: public
15
+
16
+ - table:
17
+ name: memories
18
+ schema: public
19
+ configuration:
20
+ custom_root_fields: {}
21
+ custom_name: memories
22
+ object_relationships:
23
+ - name: agent
24
+ using:
25
+ foreign_key_constraint_on: agent_id
26
+ array_relationships:
27
+ - name: incoming_edges
28
+ using:
29
+ foreign_key_constraint_on:
30
+ column: target_memory
31
+ table:
32
+ name: memory_edges
33
+ schema: public
34
+ - name: outgoing_edges
35
+ using:
36
+ foreign_key_constraint_on:
37
+ column: source_memory
38
+ table:
39
+ name: memory_edges
40
+ schema: public
41
+
42
+ - table:
43
+ name: memory_edges
44
+ schema: public
45
+ configuration:
46
+ custom_root_fields: {}
47
+ custom_name: memory_edges
48
+ object_relationships:
49
+ - name: source
50
+ using:
51
+ foreign_key_constraint_on: source_memory
52
+ - name: target
53
+ using:
54
+ foreign_key_constraint_on: target_memory
@@ -0,0 +1,14 @@
1
+ - table:
2
+ schema: public
3
+ name: agents
4
+ is_enum: false
5
+
6
+ - table:
7
+ schema: public
8
+ name: memories
9
+ is_enum: false
10
+
11
+ - table:
12
+ schema: public
13
+ name: memory_edges
14
+ is_enum: false
@@ -0,0 +1,80 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [
4
+ {
5
+ "name": "default",
6
+ "kind": "postgres",
7
+ "tables": [
8
+ {
9
+ "table": {
10
+ "schema": "public",
11
+ "name": "agents"
12
+ },
13
+ "array_relationships": [
14
+ {
15
+ "name": "memories",
16
+ "using": {
17
+ "foreign_key_constraint_on": {
18
+ "column": "agent_id",
19
+ "table": {
20
+ "schema": "public",
21
+ "name": "memories"
22
+ }
23
+ }
24
+ }
25
+ }
26
+ ]
27
+ },
28
+ {
29
+ "table": {
30
+ "schema": "public",
31
+ "name": "memories"
32
+ },
33
+ "object_relationships": [
34
+ {
35
+ "name": "agent",
36
+ "using": {
37
+ "foreign_key_constraint_on": "agent_id"
38
+ }
39
+ }
40
+ ],
41
+ "computed_fields": [
42
+ {
43
+ "name": "similarity_score",
44
+ "definition": {
45
+ "function": {
46
+ "schema": "public",
47
+ "name": "search_memories"
48
+ }
49
+ },
50
+ "comment": "Computes similarity score for vector search"
51
+ }
52
+ ]
53
+ }
54
+ ],
55
+ "functions": [
56
+ {
57
+ "function": {
58
+ "schema": "public",
59
+ "name": "search_memories"
60
+ }
61
+ }
62
+ ],
63
+ "configuration": {
64
+ "connection_info": {
65
+ "use_prepared_statements": true,
66
+ "database_url": {
67
+ "from_env": "HASURA_GRAPHQL_DATABASE_URL"
68
+ },
69
+ "isolation_level": "read-committed",
70
+ "pool_settings": {
71
+ "connection_lifetime": 600,
72
+ "retries": 1,
73
+ "idle_timeout": 180,
74
+ "max_connections": 50
75
+ }
76
+ }
77
+ }
78
+ }
79
+ ]
80
+ }
@@ -0,0 +1 @@
1
+ version: 3
@@ -0,0 +1,13 @@
1
+ -- Drop functions
2
+ DROP FUNCTION IF EXISTS search_memories;
3
+ DROP FUNCTION IF EXISTS get_connected_memories;
4
+ DROP FUNCTION IF EXISTS update_updated_at_column;
5
+
6
+ -- Drop tables
7
+ DROP TABLE IF EXISTS public.memory_edges;
8
+ DROP TABLE IF EXISTS public.memories;
9
+ DROP TABLE IF EXISTS public.agents;
10
+
11
+ -- Drop extensions
12
+ DROP EXTENSION IF EXISTS vector;
13
+ DROP EXTENSION IF EXISTS "uuid-ossp";
@@ -0,0 +1,218 @@
1
+ -- Enable required extensions
2
+ CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
3
+ CREATE EXTENSION IF NOT EXISTS "vector";
4
+
5
+ -- Create function to validate slugs
6
+ CREATE OR REPLACE FUNCTION validate_slug(slug text)
7
+ RETURNS boolean AS $$
8
+ BEGIN
9
+ RETURN slug ~ '^[a-z][a-z0-9_-]*[a-z0-9]$';
10
+ END;
11
+ $$ LANGUAGE plpgsql IMMUTABLE;
12
+
13
+ -- Create agents table
14
+ CREATE TABLE IF NOT EXISTS public.agents (
15
+ 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))
24
+ );
25
+
26
+ -- Create memories table
27
+ CREATE TABLE IF NOT EXISTS public.memories (
28
+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
29
+ agent_id UUID REFERENCES public.agents(id) ON DELETE CASCADE,
30
+ 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()
35
+ );
36
+
37
+ -- Create memory edges table
38
+ CREATE TABLE IF NOT EXISTS public.memory_edges (
39
+ 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,
43
+ weight FLOAT DEFAULT 1.0,
44
+ created_at TIMESTAMPTZ DEFAULT NOW()
45
+ );
46
+
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);
51
+
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);
55
+
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';
64
+
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();
70
+
71
+ CREATE TRIGGER update_memories_updated_at
72
+ BEFORE UPDATE ON public.memories
73
+ FOR EACH ROW
74
+ EXECUTE FUNCTION update_updated_at_column();
75
+
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;
82
+
83
+ -- Add function to normalize embeddings
84
+ CREATE OR REPLACE FUNCTION normalize_embedding()
85
+ RETURNS TRIGGER AS $$
86
+ BEGIN
87
+ IF NEW.embedding IS NOT NULL THEN
88
+ -- Normalize the embedding vector using l2_normalize
89
+ NEW.embedding = l2_normalize(NEW.embedding);
90
+ END IF;
91
+ RETURN NEW;
92
+ END;
93
+ $$ LANGUAGE plpgsql;
94
+
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,
118
+ match_count integer,
119
+ filter_agent_id uuid DEFAULT NULL
120
+ )
121
+ RETURNS SETOF public.memories_with_similarity
122
+ LANGUAGE sql
123
+ STABLE
124
+ AS $$
125
+ WITH normalized_query AS (
126
+ SELECT l2_normalize(query_embedding) AS normalized_vector
127
+ )
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
138
+ WHERE
139
+ CASE
140
+ WHEN filter_agent_id IS NOT NULL THEN m.agent_id = filter_agent_id
141
+ ELSE TRUE
142
+ 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
146
+ LIMIT match_count;
147
+ $$;
148
+
149
+ -- Track the function in Hasura
150
+ COMMENT ON FUNCTION public.search_memories IS E'@graphql({"type": "Query"})';
151
+
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;