@props-labs/mesh-os 0.1.23 → 0.2.2
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.
- package/dist/core/__fixtures__/mock_responses.d.ts +337 -0
- package/dist/core/__fixtures__/mock_responses.js +355 -0
- package/dist/core/__fixtures__/sample_embeddings.d.ts +33 -0
- package/dist/core/__fixtures__/sample_embeddings.js +12355 -0
- package/dist/core/agents.d.ts +51 -0
- package/dist/core/agents.js +170 -0
- package/dist/core/client.d.ts +5 -7
- package/dist/core/client.js +5 -16
- package/dist/core/memories.d.ts +138 -0
- package/dist/core/memories.js +417 -0
- package/dist/core/workflows.d.ts +88 -25
- package/dist/core/workflows.js +247 -133
- package/package.json +3 -3
- package/src/templates/hasura/metadata/actions.yaml +6 -0
- package/src/templates/hasura/metadata/cron_triggers.yaml +1 -0
- package/src/templates/hasura/metadata/databases/databases.yaml +1 -1
- package/src/templates/hasura/metadata/databases/default/functions/functions.yaml +80 -0
- package/src/templates/hasura/metadata/databases/default/tables/tables.yaml +274 -9
- package/src/templates/hasura/metadata/query_collections.yaml +1 -0
- package/src/templates/hasura/metadata/rest_endpoints.yaml +1 -0
- package/src/templates/hasura/migrations/default/0_cleanup/down.sql +2 -0
- package/src/templates/hasura/migrations/default/0_cleanup/up.sql +59 -0
- package/src/templates/hasura/migrations/default/1_init/down.sql +27 -21
- package/src/templates/hasura/migrations/default/1_init/up.sql +446 -174
- package/src/templates/hasura/migrations/default/2_sample_data/down.sql +3 -0
- package/src/templates/hasura/migrations/default/2_sample_data/up.sql +288 -0
- package/src/templates/hasura/migrations/default/3_agent_relations/down.sql +76 -0
- package/src/templates/hasura/migrations/default/3_agent_relations/up.sql +469 -0
- package/dist/core/entities.d.ts +0 -58
- package/dist/core/entities.js +0 -347
- package/src/templates/hasura/metadata/config.yaml +0 -1
- package/src/templates/hasura/metadata/databases/default/tables/public_agents.yaml +0 -14
- package/src/templates/hasura/metadata/databases/default/tables/public_memories.yaml +0 -23
- package/src/templates/hasura/metadata/databases/default/tables/public_memory_edges.yaml +0 -57
- package/src/templates/hasura/metadata/databases/default/tables/track_tables.yaml +0 -14
- package/src/templates/hasura/metadata/metadata.json +0 -80
- package/src/templates/hasura/migrations/default/2_metadata_filtering/down.sql +0 -4
- package/src/templates/hasura/migrations/default/2_metadata_filtering/up.sql +0 -44
- package/src/templates/hasura/migrations/default/3_memory_expiry/down.sql +0 -55
- package/src/templates/hasura/migrations/default/3_memory_expiry/up.sql +0 -108
- package/src/templates/hasura/migrations/default/4_remove_slug_validation/down.sql +0 -20
- package/src/templates/hasura/migrations/default/4_remove_slug_validation/up.sql +0 -5
- package/src/templates/hasura/migrations/default/5_entities/down.sql +0 -13
- package/src/templates/hasura/migrations/default/5_entities/up.sql +0 -155
package/dist/core/entities.js
DELETED
@@ -1,347 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.EntityManager = void 0;
|
4
|
-
/**
|
5
|
-
* Entity management functionality for MeshOS.
|
6
|
-
*/
|
7
|
-
const taxonomy_1 = require("./taxonomy");
|
8
|
-
class EntityManager {
|
9
|
-
constructor(url, headers, createEmbedding) {
|
10
|
-
this.url = url;
|
11
|
-
this.headers = headers;
|
12
|
-
this.createEmbedding = createEmbedding;
|
13
|
-
}
|
14
|
-
/**
|
15
|
-
* Execute a GraphQL query.
|
16
|
-
*/
|
17
|
-
async executeQuery(query, variables) {
|
18
|
-
const response = await fetch(this.url, {
|
19
|
-
method: 'POST',
|
20
|
-
headers: this.headers,
|
21
|
-
body: JSON.stringify({
|
22
|
-
query,
|
23
|
-
variables: variables || {}
|
24
|
-
})
|
25
|
-
});
|
26
|
-
if (!response.ok) {
|
27
|
-
throw new Error(`HTTP error! status: ${response.status}`);
|
28
|
-
}
|
29
|
-
const result = await response.json();
|
30
|
-
if (result.errors) {
|
31
|
-
throw new Error(result.errors[0].message);
|
32
|
-
}
|
33
|
-
return result.data;
|
34
|
-
}
|
35
|
-
/**
|
36
|
-
* Add a new entity.
|
37
|
-
*/
|
38
|
-
async add(entity) {
|
39
|
-
// Validate entity data
|
40
|
-
const validatedData = taxonomy_1.entitySchema.omit({ id: true, createdAt: true, updatedAt: true }).parse(entity);
|
41
|
-
// Create embedding from name and description
|
42
|
-
const embeddingText = `${validatedData.name} ${validatedData.description || ''}`.trim();
|
43
|
-
const embedding = await this.createEmbedding(embeddingText);
|
44
|
-
const embeddingStr = `[${embedding.join(',')}]`;
|
45
|
-
const query = `
|
46
|
-
mutation AddEntity($entity: entities_insert_input!) {
|
47
|
-
insert_entities_one(object: $entity) {
|
48
|
-
id
|
49
|
-
ref_id
|
50
|
-
type
|
51
|
-
name
|
52
|
-
description
|
53
|
-
metadata
|
54
|
-
status
|
55
|
-
created_at
|
56
|
-
updated_at
|
57
|
-
}
|
58
|
-
}
|
59
|
-
`;
|
60
|
-
const result = await this.executeQuery(query, {
|
61
|
-
entity: {
|
62
|
-
ref_id: validatedData.refId,
|
63
|
-
type: validatedData.type,
|
64
|
-
name: validatedData.name,
|
65
|
-
description: validatedData.description,
|
66
|
-
metadata: validatedData.metadata,
|
67
|
-
status: validatedData.status,
|
68
|
-
embedding: embeddingStr
|
69
|
-
}
|
70
|
-
});
|
71
|
-
// Convert snake_case to camelCase
|
72
|
-
const { ref_id, created_at, updated_at, ...rest } = result.insert_entities_one;
|
73
|
-
return {
|
74
|
-
...rest,
|
75
|
-
refId: ref_id,
|
76
|
-
createdAt: created_at,
|
77
|
-
updatedAt: updated_at,
|
78
|
-
};
|
79
|
-
}
|
80
|
-
/**
|
81
|
-
* Get an entity by ID.
|
82
|
-
*/
|
83
|
-
async get(id) {
|
84
|
-
const query = `
|
85
|
-
query GetEntity($id: uuid!) {
|
86
|
-
entities_by_pk(id: $id) {
|
87
|
-
id
|
88
|
-
ref_id
|
89
|
-
type
|
90
|
-
name
|
91
|
-
description
|
92
|
-
metadata
|
93
|
-
status
|
94
|
-
created_at
|
95
|
-
updated_at
|
96
|
-
}
|
97
|
-
}
|
98
|
-
`;
|
99
|
-
const result = await this.executeQuery(query, { id });
|
100
|
-
if (!result.entities_by_pk) {
|
101
|
-
return null;
|
102
|
-
}
|
103
|
-
// Convert snake_case to camelCase
|
104
|
-
const { ref_id, created_at, updated_at, ...rest } = result.entities_by_pk;
|
105
|
-
return {
|
106
|
-
...rest,
|
107
|
-
refId: ref_id,
|
108
|
-
createdAt: created_at,
|
109
|
-
updatedAt: updated_at,
|
110
|
-
};
|
111
|
-
}
|
112
|
-
/**
|
113
|
-
* Get an entity by reference ID.
|
114
|
-
*/
|
115
|
-
async getByRefId(refId) {
|
116
|
-
const query = `
|
117
|
-
query GetEntityByRefId($refId: String!) {
|
118
|
-
entities(where: {ref_id: {_eq: $refId}}, limit: 1) {
|
119
|
-
id
|
120
|
-
ref_id
|
121
|
-
type
|
122
|
-
name
|
123
|
-
description
|
124
|
-
metadata
|
125
|
-
status
|
126
|
-
created_at
|
127
|
-
updated_at
|
128
|
-
}
|
129
|
-
}
|
130
|
-
`;
|
131
|
-
const result = await this.executeQuery(query, { refId });
|
132
|
-
if (!result.entities[0]) {
|
133
|
-
return null;
|
134
|
-
}
|
135
|
-
// Convert snake_case to camelCase
|
136
|
-
const { ref_id, created_at, updated_at, ...rest } = result.entities[0];
|
137
|
-
return {
|
138
|
-
...rest,
|
139
|
-
refId: ref_id,
|
140
|
-
createdAt: created_at,
|
141
|
-
updatedAt: updated_at,
|
142
|
-
};
|
143
|
-
}
|
144
|
-
/**
|
145
|
-
* Update an entity.
|
146
|
-
*/
|
147
|
-
async update(id, updates) {
|
148
|
-
// If name or description is updated, update embedding
|
149
|
-
let embedding;
|
150
|
-
if (updates.name || updates.description) {
|
151
|
-
const entity = await this.get(id);
|
152
|
-
if (!entity) {
|
153
|
-
throw new Error(`Entity ${id} not found`);
|
154
|
-
}
|
155
|
-
const embeddingText = `${updates.name || entity.name} ${updates.description || entity.description || ''}`.trim();
|
156
|
-
embedding = await this.createEmbedding(embeddingText);
|
157
|
-
}
|
158
|
-
const query = `
|
159
|
-
mutation UpdateEntity($id: uuid!, $updates: entities_set_input!) {
|
160
|
-
update_entities_by_pk(pk_columns: {id: $id}, _set: $updates) {
|
161
|
-
id
|
162
|
-
ref_id
|
163
|
-
type
|
164
|
-
name
|
165
|
-
description
|
166
|
-
metadata
|
167
|
-
status
|
168
|
-
created_at
|
169
|
-
updated_at
|
170
|
-
}
|
171
|
-
}
|
172
|
-
`;
|
173
|
-
const updateData = {
|
174
|
-
ref_id: updates.refId,
|
175
|
-
type: updates.type,
|
176
|
-
name: updates.name,
|
177
|
-
description: updates.description,
|
178
|
-
metadata: updates.metadata,
|
179
|
-
status: updates.status,
|
180
|
-
};
|
181
|
-
if (embedding) {
|
182
|
-
updateData.embedding = `[${embedding.join(',')}]`;
|
183
|
-
}
|
184
|
-
// Remove undefined values
|
185
|
-
Object.keys(updateData).forEach(key => updateData[key] === undefined && delete updateData[key]);
|
186
|
-
const result = await this.executeQuery(query, {
|
187
|
-
id,
|
188
|
-
updates: updateData
|
189
|
-
});
|
190
|
-
// Convert snake_case to camelCase
|
191
|
-
const { ref_id, created_at, updated_at, ...rest } = result.update_entities_by_pk;
|
192
|
-
return {
|
193
|
-
...rest,
|
194
|
-
refId: ref_id,
|
195
|
-
createdAt: created_at,
|
196
|
-
updatedAt: updated_at,
|
197
|
-
};
|
198
|
-
}
|
199
|
-
/**
|
200
|
-
* Delete an entity.
|
201
|
-
*/
|
202
|
-
async delete(id) {
|
203
|
-
const query = `
|
204
|
-
mutation DeleteEntity($id: uuid!) {
|
205
|
-
delete_entities_by_pk(id: $id) {
|
206
|
-
id
|
207
|
-
}
|
208
|
-
}
|
209
|
-
`;
|
210
|
-
const result = await this.executeQuery(query, { id });
|
211
|
-
return result.delete_entities_by_pk !== null;
|
212
|
-
}
|
213
|
-
/**
|
214
|
-
* Search entities by semantic similarity.
|
215
|
-
*/
|
216
|
-
async search(options) {
|
217
|
-
const { query, threshold = 0.7, limit = 10, type, status } = options;
|
218
|
-
// Create embedding for search query
|
219
|
-
const embedding = await this.createEmbedding(query);
|
220
|
-
const embeddingStr = `[${embedding.join(',')}]`;
|
221
|
-
const searchQuery = `
|
222
|
-
query SearchEntities(
|
223
|
-
$embedding: vector!,
|
224
|
-
$threshold: float8!,
|
225
|
-
$limit: Int!,
|
226
|
-
$type: String,
|
227
|
-
$status: String
|
228
|
-
) {
|
229
|
-
entities(
|
230
|
-
where: {
|
231
|
-
_and: [
|
232
|
-
{type: {_eq: $type}},
|
233
|
-
{status: {_eq: $status}}
|
234
|
-
]
|
235
|
-
},
|
236
|
-
order_by: {embedding: {_cosine_distance: $embedding}},
|
237
|
-
limit: $limit
|
238
|
-
) {
|
239
|
-
id
|
240
|
-
ref_id
|
241
|
-
type
|
242
|
-
name
|
243
|
-
description
|
244
|
-
metadata
|
245
|
-
status
|
246
|
-
created_at
|
247
|
-
updated_at
|
248
|
-
similarity: _cosine_similarity_embedding_with($embedding)
|
249
|
-
}
|
250
|
-
}
|
251
|
-
`;
|
252
|
-
const result = await this.executeQuery(searchQuery, {
|
253
|
-
embedding: embeddingStr,
|
254
|
-
threshold,
|
255
|
-
limit,
|
256
|
-
type,
|
257
|
-
status
|
258
|
-
});
|
259
|
-
return result.entities.map(entity => {
|
260
|
-
const { ref_id, created_at, updated_at, ...rest } = entity;
|
261
|
-
return {
|
262
|
-
...rest,
|
263
|
-
refId: ref_id,
|
264
|
-
createdAt: created_at,
|
265
|
-
updatedAt: updated_at,
|
266
|
-
};
|
267
|
-
}).filter(entity => (entity.similarity || 0) >= threshold);
|
268
|
-
}
|
269
|
-
/**
|
270
|
-
* Link an entity to a memory.
|
271
|
-
*/
|
272
|
-
async linkMemory(entityId, memoryId, relationship, confidence) {
|
273
|
-
const query = `
|
274
|
-
mutation LinkEntityMemory($link: entity_memory_links_insert_input!) {
|
275
|
-
insert_entity_memory_links_one(object: $link) {
|
276
|
-
id
|
277
|
-
entity_id
|
278
|
-
memory_id
|
279
|
-
relationship
|
280
|
-
confidence
|
281
|
-
created_at
|
282
|
-
}
|
283
|
-
}
|
284
|
-
`;
|
285
|
-
const result = await this.executeQuery(query, {
|
286
|
-
link: {
|
287
|
-
entity_id: entityId,
|
288
|
-
memory_id: memoryId,
|
289
|
-
relationship,
|
290
|
-
confidence: confidence || 1.0
|
291
|
-
}
|
292
|
-
});
|
293
|
-
// Convert snake_case to camelCase
|
294
|
-
const { entity_id, memory_id, created_at, ...rest } = result.insert_entity_memory_links_one;
|
295
|
-
return {
|
296
|
-
...rest,
|
297
|
-
entityId: entity_id,
|
298
|
-
memoryId: memory_id,
|
299
|
-
createdAt: created_at,
|
300
|
-
};
|
301
|
-
}
|
302
|
-
/**
|
303
|
-
* Get all memories linked to an entity.
|
304
|
-
*/
|
305
|
-
async getLinkedMemories(entityId, relationship) {
|
306
|
-
const query = `
|
307
|
-
query GetLinkedMemories($entityId: uuid!, $relationship: String) {
|
308
|
-
entity_memory_links(
|
309
|
-
where: {
|
310
|
-
entity_id: {_eq: $entityId},
|
311
|
-
relationship: {_eq: $relationship}
|
312
|
-
}
|
313
|
-
) {
|
314
|
-
memory {
|
315
|
-
id
|
316
|
-
agent_id
|
317
|
-
content
|
318
|
-
metadata
|
319
|
-
created_at
|
320
|
-
updated_at
|
321
|
-
expires_at
|
322
|
-
}
|
323
|
-
relationship
|
324
|
-
confidence
|
325
|
-
}
|
326
|
-
}
|
327
|
-
`;
|
328
|
-
const result = await this.executeQuery(query, {
|
329
|
-
entityId,
|
330
|
-
relationship
|
331
|
-
});
|
332
|
-
return result.entity_memory_links.map(link => {
|
333
|
-
const { memory, relationship, confidence } = link;
|
334
|
-
const { agent_id, created_at, updated_at, expires_at, ...rest } = memory;
|
335
|
-
return {
|
336
|
-
...rest,
|
337
|
-
agentId: agent_id,
|
338
|
-
createdAt: created_at,
|
339
|
-
updatedAt: updated_at,
|
340
|
-
expiresAt: expires_at,
|
341
|
-
relationship,
|
342
|
-
confidence
|
343
|
-
};
|
344
|
-
});
|
345
|
-
}
|
346
|
-
}
|
347
|
-
exports.EntityManager = EntityManager;
|
@@ -1 +0,0 @@
|
|
1
|
-
|
@@ -1,23 +0,0 @@
|
|
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
|
-
arguments:
|
18
|
-
query_embedding: $query_embedding
|
19
|
-
match_threshold: $match_threshold
|
20
|
-
match_count: $match_count
|
21
|
-
filter_agent_id: $filter_agent_id
|
22
|
-
metadata_filter: $metadata_filter
|
23
|
-
comment: Computes similarity score for vector search
|
@@ -1,57 +0,0 @@
|
|
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: {}
|
@@ -1,80 +0,0 @@
|
|
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
|
-
}
|
@@ -1,44 +0,0 @@
|
|
1
|
-
-- Drop the existing search_memories function
|
2
|
-
DROP FUNCTION IF EXISTS public.search_memories;
|
3
|
-
|
4
|
-
-- Create the updated search_memories function with metadata filtering
|
5
|
-
CREATE OR REPLACE FUNCTION public.search_memories(
|
6
|
-
query_embedding vector(1536),
|
7
|
-
match_threshold float8,
|
8
|
-
match_count integer,
|
9
|
-
filter_agent_id uuid DEFAULT NULL,
|
10
|
-
metadata_filter jsonb DEFAULT NULL
|
11
|
-
)
|
12
|
-
RETURNS SETOF public.memories_with_similarity
|
13
|
-
LANGUAGE sql
|
14
|
-
STABLE
|
15
|
-
AS $$
|
16
|
-
WITH normalized_query AS (
|
17
|
-
SELECT l2_normalize(query_embedding) AS normalized_vector
|
18
|
-
)
|
19
|
-
SELECT
|
20
|
-
m.id,
|
21
|
-
m.agent_id,
|
22
|
-
m.content,
|
23
|
-
m.metadata,
|
24
|
-
m.embedding,
|
25
|
-
m.created_at,
|
26
|
-
m.updated_at,
|
27
|
-
-(m.embedding <#> (SELECT normalized_vector FROM normalized_query)) as similarity
|
28
|
-
FROM memories m
|
29
|
-
WHERE
|
30
|
-
CASE
|
31
|
-
WHEN filter_agent_id IS NOT NULL THEN m.agent_id = filter_agent_id
|
32
|
-
ELSE TRUE
|
33
|
-
END
|
34
|
-
AND CASE
|
35
|
-
WHEN metadata_filter IS NOT NULL THEN m.metadata @> metadata_filter
|
36
|
-
ELSE TRUE
|
37
|
-
END
|
38
|
-
AND -(m.embedding <#> (SELECT normalized_vector FROM normalized_query)) >= match_threshold
|
39
|
-
ORDER BY -(m.embedding <#> (SELECT normalized_vector FROM normalized_query)) DESC
|
40
|
-
LIMIT match_count;
|
41
|
-
$$;
|
42
|
-
|
43
|
-
-- Track the function in Hasura
|
44
|
-
COMMENT ON FUNCTION public.search_memories IS E'@graphql({"type": "Query"})';
|
@@ -1,55 +0,0 @@
|
|
1
|
-
-- Drop the updated search_memories function
|
2
|
-
DROP FUNCTION IF EXISTS public.search_memories(vector(1536), float8, integer, uuid, jsonb, jsonb, jsonb);
|
3
|
-
|
4
|
-
-- Remove expires_at column
|
5
|
-
ALTER TABLE public.memories DROP COLUMN expires_at;
|
6
|
-
|
7
|
-
-- Drop and recreate the view without expires_at
|
8
|
-
DROP VIEW IF EXISTS public.memories_with_similarity;
|
9
|
-
CREATE OR REPLACE VIEW public.memories_with_similarity AS
|
10
|
-
SELECT
|
11
|
-
m.*,
|
12
|
-
0::float8 as similarity
|
13
|
-
FROM memories m;
|
14
|
-
|
15
|
-
-- Restore the previous version of search_memories from 2_metadata_filtering
|
16
|
-
CREATE OR REPLACE FUNCTION public.search_memories(
|
17
|
-
query_embedding vector(1536),
|
18
|
-
match_threshold float8,
|
19
|
-
match_count integer,
|
20
|
-
filter_agent_id uuid DEFAULT NULL,
|
21
|
-
metadata_filter jsonb DEFAULT NULL
|
22
|
-
)
|
23
|
-
RETURNS SETOF public.memories_with_similarity
|
24
|
-
LANGUAGE sql
|
25
|
-
STABLE
|
26
|
-
AS $$
|
27
|
-
WITH normalized_query AS (
|
28
|
-
SELECT l2_normalize(query_embedding) AS normalized_vector
|
29
|
-
)
|
30
|
-
SELECT
|
31
|
-
m.id,
|
32
|
-
m.agent_id,
|
33
|
-
m.content,
|
34
|
-
m.metadata,
|
35
|
-
m.embedding,
|
36
|
-
m.created_at,
|
37
|
-
m.updated_at,
|
38
|
-
-(m.embedding <#> (SELECT normalized_vector FROM normalized_query)) as similarity
|
39
|
-
FROM memories m
|
40
|
-
WHERE
|
41
|
-
CASE
|
42
|
-
WHEN filter_agent_id IS NOT NULL THEN m.agent_id = filter_agent_id
|
43
|
-
ELSE TRUE
|
44
|
-
END
|
45
|
-
AND CASE
|
46
|
-
WHEN metadata_filter IS NOT NULL THEN m.metadata @> metadata_filter
|
47
|
-
ELSE TRUE
|
48
|
-
END
|
49
|
-
AND -(m.embedding <#> (SELECT normalized_vector FROM normalized_query)) >= match_threshold
|
50
|
-
ORDER BY -(m.embedding <#> (SELECT normalized_vector FROM normalized_query)) DESC
|
51
|
-
LIMIT match_count;
|
52
|
-
$$;
|
53
|
-
|
54
|
-
-- Track the function in Hasura
|
55
|
-
COMMENT ON FUNCTION public.search_memories IS E'@graphql({"type": "Query"})';
|