weave-typescript 0.27.0 → 0.28.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.
- package/dist/weaveapi/consolidation/v1/consolidation.pb.d.ts +134 -0
- package/dist/weaveapi/consolidation/v1/consolidation.pb.js +1620 -0
- package/dist/weaveapi/consolidation/v1/service.pb.d.ts +93 -0
- package/dist/weaveapi/consolidation/v1/service.pb.js +427 -0
- package/dist/weaveapi/ingestion/v1/ingestion.pb.d.ts +433 -0
- package/dist/weaveapi/ingestion/v1/ingestion.pb.js +5626 -0
- package/dist/weaveapi/ingestion/v1/service.pb.d.ts +181 -0
- package/dist/weaveapi/ingestion/v1/service.pb.js +917 -0
- package/dist/weaveapi/model/v1/model.pb.d.ts +10 -0
- package/dist/weaveapi/model/v1/model.pb.js +181 -1
- package/dist/weaveapi/model/v1/service.pb.d.ts +50 -1
- package/dist/weaveapi/model/v1/service.pb.js +464 -1
- package/dist/weaveapi/retrieval/v1/retrieval.pb.d.ts +161 -0
- package/dist/weaveapi/retrieval/v1/retrieval.pb.js +1885 -0
- package/dist/weaveapi/retrieval/v1/service.pb.d.ts +51 -0
- package/dist/weaveapi/retrieval/v1/service.pb.js +164 -0
- package/dist/weavesql/weavedb/consolidation_sql.d.ts +212 -0
- package/dist/weavesql/weavedb/consolidation_sql.js +470 -0
- package/dist/weavesql/weavedb/document_security_sql.d.ts +128 -0
- package/dist/weavesql/weavedb/document_security_sql.js +284 -0
- package/dist/weavesql/weavedb/ingestion_sql.d.ts +1108 -0
- package/dist/weavesql/weavedb/ingestion_sql.js +2503 -0
- package/dist/weavesql/weavedb/retrieval_sql.d.ts +284 -0
- package/dist/weavesql/weavedb/retrieval_sql.js +769 -0
- package/package.json +2 -2
|
@@ -0,0 +1,769 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.listApprovedReconcilePoolByOrganizationQuery = exports.deleteSessionRetrievalContextQuery = exports.listSessionRetrievalContextQuery = exports.insertSessionRetrievalContextQuery = exports.searchConceptClusterThreadsBySimilarityAndUserPermissionsQuery = exports.searchConceptClusterThreadsBySimilarityQuery = exports.searchLeafConceptClustersBySimilarityQuery = exports.searchApprovedThreadsByEntitiesAndUserPermissionsQuery = exports.searchApprovedThreadsByEntitiesQuery = exports.searchApprovedThreadsSemanticByKnowledgeTypesAndUserPermissionsQuery = exports.searchApprovedThreadsSemanticByKnowledgeTypesQuery = exports.searchApprovedThreadsSemanticByUserPermissionsQuery = exports.searchApprovedThreadsSemanticQuery = exports.refreshThreadReinforcementScoresQuery = void 0;
|
|
4
|
+
exports.refreshThreadReinforcementScores = refreshThreadReinforcementScores;
|
|
5
|
+
exports.searchApprovedThreadsSemantic = searchApprovedThreadsSemantic;
|
|
6
|
+
exports.searchApprovedThreadsSemanticByUserPermissions = searchApprovedThreadsSemanticByUserPermissions;
|
|
7
|
+
exports.searchApprovedThreadsSemanticByKnowledgeTypes = searchApprovedThreadsSemanticByKnowledgeTypes;
|
|
8
|
+
exports.searchApprovedThreadsSemanticByKnowledgeTypesAndUserPermissions = searchApprovedThreadsSemanticByKnowledgeTypesAndUserPermissions;
|
|
9
|
+
exports.searchApprovedThreadsByEntities = searchApprovedThreadsByEntities;
|
|
10
|
+
exports.searchApprovedThreadsByEntitiesAndUserPermissions = searchApprovedThreadsByEntitiesAndUserPermissions;
|
|
11
|
+
exports.searchLeafConceptClustersBySimilarity = searchLeafConceptClustersBySimilarity;
|
|
12
|
+
exports.searchConceptClusterThreadsBySimilarity = searchConceptClusterThreadsBySimilarity;
|
|
13
|
+
exports.searchConceptClusterThreadsBySimilarityAndUserPermissions = searchConceptClusterThreadsBySimilarityAndUserPermissions;
|
|
14
|
+
exports.insertSessionRetrievalContext = insertSessionRetrievalContext;
|
|
15
|
+
exports.listSessionRetrievalContext = listSessionRetrievalContext;
|
|
16
|
+
exports.deleteSessionRetrievalContext = deleteSessionRetrievalContext;
|
|
17
|
+
exports.listApprovedReconcilePoolByOrganization = listApprovedReconcilePoolByOrganization;
|
|
18
|
+
exports.refreshThreadReinforcementScoresQuery = `-- name: RefreshThreadReinforcementScores :exec
|
|
19
|
+
REFRESH MATERIALIZED VIEW CONCURRENTLY weave.thread_reinforcement_scores`;
|
|
20
|
+
async function refreshThreadReinforcementScores(client) {
|
|
21
|
+
await client.query({
|
|
22
|
+
text: exports.refreshThreadReinforcementScoresQuery,
|
|
23
|
+
values: [],
|
|
24
|
+
rowMode: "array"
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.searchApprovedThreadsSemanticQuery = `-- name: SearchApprovedThreadsSemantic :many
|
|
28
|
+
SELECT
|
|
29
|
+
t.id AS thread_id,
|
|
30
|
+
t.text,
|
|
31
|
+
t.knowledge_type,
|
|
32
|
+
t.facet_fields,
|
|
33
|
+
similarity.score,
|
|
34
|
+
t.classification,
|
|
35
|
+
t.document_id,
|
|
36
|
+
d.filename AS document_name,
|
|
37
|
+
t.source_location,
|
|
38
|
+
t.approved_at,
|
|
39
|
+
t.temporal_validity,
|
|
40
|
+
COALESCE(entity_data.entities, '{}'::text[]) AS entities,
|
|
41
|
+
COALESCE(rs.reinforcement_count, 0) AS reinforcement_count,
|
|
42
|
+
COALESCE(cc.name, '') AS concept_cluster
|
|
43
|
+
FROM weave.threads t
|
|
44
|
+
JOIN weave.documents d ON d.id = t.document_id
|
|
45
|
+
CROSS JOIN LATERAL (
|
|
46
|
+
SELECT CAST(
|
|
47
|
+
1.0::double precision
|
|
48
|
+
- (t.embedding <=> $1::vector)::double precision
|
|
49
|
+
AS double precision
|
|
50
|
+
) AS score
|
|
51
|
+
) similarity
|
|
52
|
+
LEFT JOIN weave.thread_reinforcement_scores rs ON rs.thread_id = t.id
|
|
53
|
+
LEFT JOIN weave.concept_clusters cc ON cc.id = t.cluster_id
|
|
54
|
+
LEFT JOIN LATERAL (
|
|
55
|
+
SELECT array_agg(DISTINCT e.canonical ORDER BY e.canonical) AS entities
|
|
56
|
+
FROM weave.thread_entities te
|
|
57
|
+
JOIN weave.entities e ON e.id = te.entity_id
|
|
58
|
+
WHERE te.thread_id = t.id
|
|
59
|
+
) entity_data ON TRUE
|
|
60
|
+
WHERE t.organization_id = $2
|
|
61
|
+
AND t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
|
|
62
|
+
AND t.embedding IS NOT NULL
|
|
63
|
+
AND similarity.score >= $3::double precision
|
|
64
|
+
ORDER BY score DESC, t.approved_at DESC
|
|
65
|
+
LIMIT $4`;
|
|
66
|
+
async function searchApprovedThreadsSemantic(client, args) {
|
|
67
|
+
const result = await client.query({
|
|
68
|
+
text: exports.searchApprovedThreadsSemanticQuery,
|
|
69
|
+
values: [args.queryEmbedding, args.organizationId, args.minSimilarity, args.resultLimit],
|
|
70
|
+
rowMode: "array"
|
|
71
|
+
});
|
|
72
|
+
return result.rows.map(row => {
|
|
73
|
+
return {
|
|
74
|
+
threadId: row[0],
|
|
75
|
+
text: row[1],
|
|
76
|
+
knowledgeType: row[2],
|
|
77
|
+
facetFields: row[3],
|
|
78
|
+
score: row[4],
|
|
79
|
+
classification: row[5],
|
|
80
|
+
documentId: row[6],
|
|
81
|
+
documentName: row[7],
|
|
82
|
+
sourceLocation: row[8],
|
|
83
|
+
approvedAt: row[9],
|
|
84
|
+
temporalValidity: row[10],
|
|
85
|
+
entities: row[11],
|
|
86
|
+
reinforcementCount: row[12],
|
|
87
|
+
conceptCluster: row[13]
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
exports.searchApprovedThreadsSemanticByUserPermissionsQuery = `-- name: SearchApprovedThreadsSemanticByUserPermissions :many
|
|
92
|
+
SELECT
|
|
93
|
+
t.id AS thread_id,
|
|
94
|
+
t.text,
|
|
95
|
+
t.knowledge_type,
|
|
96
|
+
t.facet_fields,
|
|
97
|
+
similarity.score,
|
|
98
|
+
t.classification,
|
|
99
|
+
t.document_id,
|
|
100
|
+
d.filename AS document_name,
|
|
101
|
+
t.source_location,
|
|
102
|
+
t.approved_at,
|
|
103
|
+
t.temporal_validity,
|
|
104
|
+
COALESCE(entity_data.entities, '{}'::text[]) AS entities,
|
|
105
|
+
COALESCE(rs.reinforcement_count, 0) AS reinforcement_count,
|
|
106
|
+
COALESCE(cc.name, '') AS concept_cluster
|
|
107
|
+
FROM weave.threads t
|
|
108
|
+
JOIN weave.documents d ON d.id = t.document_id
|
|
109
|
+
CROSS JOIN LATERAL (
|
|
110
|
+
SELECT CAST(
|
|
111
|
+
1.0::double precision
|
|
112
|
+
- (t.embedding <=> $1::vector)::double precision
|
|
113
|
+
AS double precision
|
|
114
|
+
) AS score
|
|
115
|
+
) similarity
|
|
116
|
+
LEFT JOIN weave.thread_reinforcement_scores rs ON rs.thread_id = t.id
|
|
117
|
+
LEFT JOIN weave.concept_clusters cc ON cc.id = t.cluster_id
|
|
118
|
+
LEFT JOIN LATERAL (
|
|
119
|
+
SELECT array_agg(DISTINCT e.canonical ORDER BY e.canonical) AS entities
|
|
120
|
+
FROM weave.thread_entities te
|
|
121
|
+
JOIN weave.entities e ON e.id = te.entity_id
|
|
122
|
+
WHERE te.thread_id = t.id
|
|
123
|
+
) entity_data ON TRUE
|
|
124
|
+
WHERE t.organization_id = $2
|
|
125
|
+
AND t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
|
|
126
|
+
AND t.embedding IS NOT NULL
|
|
127
|
+
AND similarity.score >= $3::double precision
|
|
128
|
+
AND (
|
|
129
|
+
d.scope = 'DOCUMENT_SCOPE_ORG'
|
|
130
|
+
OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
|
|
131
|
+
AND d.uploaded_by_user_id = $4)
|
|
132
|
+
OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
|
|
133
|
+
AND EXISTS (
|
|
134
|
+
SELECT 1
|
|
135
|
+
FROM weave.user_sensitivity_clearances c
|
|
136
|
+
WHERE c.organization_id = $2
|
|
137
|
+
AND c.user_id = $4
|
|
138
|
+
AND cardinality(d.sensitivity_tags) = 1
|
|
139
|
+
AND c.sensitivity_tag_slug = d.sensitivity_tags[1]
|
|
140
|
+
))
|
|
141
|
+
)
|
|
142
|
+
ORDER BY score DESC, t.approved_at DESC
|
|
143
|
+
LIMIT $5`;
|
|
144
|
+
async function searchApprovedThreadsSemanticByUserPermissions(client, args) {
|
|
145
|
+
const result = await client.query({
|
|
146
|
+
text: exports.searchApprovedThreadsSemanticByUserPermissionsQuery,
|
|
147
|
+
values: [args.queryEmbedding, args.organizationId, args.minSimilarity, args.actingUserId, args.resultLimit],
|
|
148
|
+
rowMode: "array"
|
|
149
|
+
});
|
|
150
|
+
return result.rows.map(row => {
|
|
151
|
+
return {
|
|
152
|
+
threadId: row[0],
|
|
153
|
+
text: row[1],
|
|
154
|
+
knowledgeType: row[2],
|
|
155
|
+
facetFields: row[3],
|
|
156
|
+
score: row[4],
|
|
157
|
+
classification: row[5],
|
|
158
|
+
documentId: row[6],
|
|
159
|
+
documentName: row[7],
|
|
160
|
+
sourceLocation: row[8],
|
|
161
|
+
approvedAt: row[9],
|
|
162
|
+
temporalValidity: row[10],
|
|
163
|
+
entities: row[11],
|
|
164
|
+
reinforcementCount: row[12],
|
|
165
|
+
conceptCluster: row[13]
|
|
166
|
+
};
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
exports.searchApprovedThreadsSemanticByKnowledgeTypesQuery = `-- name: SearchApprovedThreadsSemanticByKnowledgeTypes :many
|
|
170
|
+
SELECT
|
|
171
|
+
t.id AS thread_id,
|
|
172
|
+
t.text,
|
|
173
|
+
t.knowledge_type,
|
|
174
|
+
t.facet_fields,
|
|
175
|
+
similarity.score,
|
|
176
|
+
t.classification,
|
|
177
|
+
t.document_id,
|
|
178
|
+
d.filename AS document_name,
|
|
179
|
+
t.source_location,
|
|
180
|
+
t.approved_at,
|
|
181
|
+
t.temporal_validity,
|
|
182
|
+
COALESCE(entity_data.entities, '{}'::text[]) AS entities,
|
|
183
|
+
COALESCE(rs.reinforcement_count, 0) AS reinforcement_count,
|
|
184
|
+
COALESCE(cc.name, '') AS concept_cluster
|
|
185
|
+
FROM weave.threads t
|
|
186
|
+
JOIN weave.documents d ON d.id = t.document_id
|
|
187
|
+
CROSS JOIN LATERAL (
|
|
188
|
+
SELECT CAST(
|
|
189
|
+
1.0::double precision
|
|
190
|
+
- (t.embedding <=> $1::vector)::double precision
|
|
191
|
+
AS double precision
|
|
192
|
+
) AS score
|
|
193
|
+
) similarity
|
|
194
|
+
LEFT JOIN weave.thread_reinforcement_scores rs ON rs.thread_id = t.id
|
|
195
|
+
LEFT JOIN weave.concept_clusters cc ON cc.id = t.cluster_id
|
|
196
|
+
LEFT JOIN LATERAL (
|
|
197
|
+
SELECT array_agg(DISTINCT e.canonical ORDER BY e.canonical) AS entities
|
|
198
|
+
FROM weave.thread_entities te
|
|
199
|
+
JOIN weave.entities e ON e.id = te.entity_id
|
|
200
|
+
WHERE te.thread_id = t.id
|
|
201
|
+
) entity_data ON TRUE
|
|
202
|
+
WHERE t.organization_id = $2
|
|
203
|
+
AND t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
|
|
204
|
+
AND t.embedding IS NOT NULL
|
|
205
|
+
AND t.knowledge_type = ANY($3::text[])
|
|
206
|
+
AND similarity.score >= $4::double precision
|
|
207
|
+
ORDER BY score DESC, t.approved_at DESC
|
|
208
|
+
LIMIT $5`;
|
|
209
|
+
async function searchApprovedThreadsSemanticByKnowledgeTypes(client, args) {
|
|
210
|
+
const result = await client.query({
|
|
211
|
+
text: exports.searchApprovedThreadsSemanticByKnowledgeTypesQuery,
|
|
212
|
+
values: [args.queryEmbedding, args.organizationId, args.knowledgeTypes, args.minSimilarity, args.resultLimit],
|
|
213
|
+
rowMode: "array"
|
|
214
|
+
});
|
|
215
|
+
return result.rows.map(row => {
|
|
216
|
+
return {
|
|
217
|
+
threadId: row[0],
|
|
218
|
+
text: row[1],
|
|
219
|
+
knowledgeType: row[2],
|
|
220
|
+
facetFields: row[3],
|
|
221
|
+
score: row[4],
|
|
222
|
+
classification: row[5],
|
|
223
|
+
documentId: row[6],
|
|
224
|
+
documentName: row[7],
|
|
225
|
+
sourceLocation: row[8],
|
|
226
|
+
approvedAt: row[9],
|
|
227
|
+
temporalValidity: row[10],
|
|
228
|
+
entities: row[11],
|
|
229
|
+
reinforcementCount: row[12],
|
|
230
|
+
conceptCluster: row[13]
|
|
231
|
+
};
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
exports.searchApprovedThreadsSemanticByKnowledgeTypesAndUserPermissionsQuery = `-- name: SearchApprovedThreadsSemanticByKnowledgeTypesAndUserPermissions :many
|
|
235
|
+
SELECT
|
|
236
|
+
t.id AS thread_id,
|
|
237
|
+
t.text,
|
|
238
|
+
t.knowledge_type,
|
|
239
|
+
t.facet_fields,
|
|
240
|
+
similarity.score,
|
|
241
|
+
t.classification,
|
|
242
|
+
t.document_id,
|
|
243
|
+
d.filename AS document_name,
|
|
244
|
+
t.source_location,
|
|
245
|
+
t.approved_at,
|
|
246
|
+
t.temporal_validity,
|
|
247
|
+
COALESCE(entity_data.entities, '{}'::text[]) AS entities,
|
|
248
|
+
COALESCE(rs.reinforcement_count, 0) AS reinforcement_count,
|
|
249
|
+
COALESCE(cc.name, '') AS concept_cluster
|
|
250
|
+
FROM weave.threads t
|
|
251
|
+
JOIN weave.documents d ON d.id = t.document_id
|
|
252
|
+
CROSS JOIN LATERAL (
|
|
253
|
+
SELECT CAST(
|
|
254
|
+
1.0::double precision
|
|
255
|
+
- (t.embedding <=> $1::vector)::double precision
|
|
256
|
+
AS double precision
|
|
257
|
+
) AS score
|
|
258
|
+
) similarity
|
|
259
|
+
LEFT JOIN weave.thread_reinforcement_scores rs ON rs.thread_id = t.id
|
|
260
|
+
LEFT JOIN weave.concept_clusters cc ON cc.id = t.cluster_id
|
|
261
|
+
LEFT JOIN LATERAL (
|
|
262
|
+
SELECT array_agg(DISTINCT e.canonical ORDER BY e.canonical) AS entities
|
|
263
|
+
FROM weave.thread_entities te
|
|
264
|
+
JOIN weave.entities e ON e.id = te.entity_id
|
|
265
|
+
WHERE te.thread_id = t.id
|
|
266
|
+
) entity_data ON TRUE
|
|
267
|
+
WHERE t.organization_id = $2
|
|
268
|
+
AND t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
|
|
269
|
+
AND t.embedding IS NOT NULL
|
|
270
|
+
AND t.knowledge_type = ANY($3::text[])
|
|
271
|
+
AND similarity.score >= $4::double precision
|
|
272
|
+
AND (
|
|
273
|
+
d.scope = 'DOCUMENT_SCOPE_ORG'
|
|
274
|
+
OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
|
|
275
|
+
AND d.uploaded_by_user_id = $5)
|
|
276
|
+
OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
|
|
277
|
+
AND EXISTS (
|
|
278
|
+
SELECT 1
|
|
279
|
+
FROM weave.user_sensitivity_clearances c
|
|
280
|
+
WHERE c.organization_id = $2
|
|
281
|
+
AND c.user_id = $5
|
|
282
|
+
AND cardinality(d.sensitivity_tags) = 1
|
|
283
|
+
AND c.sensitivity_tag_slug = d.sensitivity_tags[1]
|
|
284
|
+
))
|
|
285
|
+
)
|
|
286
|
+
ORDER BY score DESC, t.approved_at DESC
|
|
287
|
+
LIMIT $6`;
|
|
288
|
+
async function searchApprovedThreadsSemanticByKnowledgeTypesAndUserPermissions(client, args) {
|
|
289
|
+
const result = await client.query({
|
|
290
|
+
text: exports.searchApprovedThreadsSemanticByKnowledgeTypesAndUserPermissionsQuery,
|
|
291
|
+
values: [args.queryEmbedding, args.organizationId, args.knowledgeTypes, args.minSimilarity, args.actingUserId, args.resultLimit],
|
|
292
|
+
rowMode: "array"
|
|
293
|
+
});
|
|
294
|
+
return result.rows.map(row => {
|
|
295
|
+
return {
|
|
296
|
+
threadId: row[0],
|
|
297
|
+
text: row[1],
|
|
298
|
+
knowledgeType: row[2],
|
|
299
|
+
facetFields: row[3],
|
|
300
|
+
score: row[4],
|
|
301
|
+
classification: row[5],
|
|
302
|
+
documentId: row[6],
|
|
303
|
+
documentName: row[7],
|
|
304
|
+
sourceLocation: row[8],
|
|
305
|
+
approvedAt: row[9],
|
|
306
|
+
temporalValidity: row[10],
|
|
307
|
+
entities: row[11],
|
|
308
|
+
reinforcementCount: row[12],
|
|
309
|
+
conceptCluster: row[13]
|
|
310
|
+
};
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
exports.searchApprovedThreadsByEntitiesQuery = `-- name: SearchApprovedThreadsByEntities :many
|
|
314
|
+
SELECT DISTINCT
|
|
315
|
+
t.id AS thread_id,
|
|
316
|
+
t.text,
|
|
317
|
+
t.knowledge_type,
|
|
318
|
+
t.facet_fields,
|
|
319
|
+
te.confidence::double precision AS score,
|
|
320
|
+
t.classification,
|
|
321
|
+
t.document_id,
|
|
322
|
+
d.filename AS document_name,
|
|
323
|
+
t.source_location,
|
|
324
|
+
t.approved_at,
|
|
325
|
+
t.temporal_validity,
|
|
326
|
+
COALESCE(entity_data.entities, '{}'::text[]) AS entities,
|
|
327
|
+
COALESCE(rs.reinforcement_count, 0) AS reinforcement_count,
|
|
328
|
+
COALESCE(cc.name, '') AS concept_cluster
|
|
329
|
+
FROM weave.threads t
|
|
330
|
+
JOIN weave.documents d ON d.id = t.document_id
|
|
331
|
+
JOIN weave.thread_entities te ON te.thread_id = t.id
|
|
332
|
+
JOIN weave.entities e ON e.id = te.entity_id
|
|
333
|
+
LEFT JOIN weave.thread_reinforcement_scores rs ON rs.thread_id = t.id
|
|
334
|
+
LEFT JOIN weave.concept_clusters cc ON cc.id = t.cluster_id
|
|
335
|
+
LEFT JOIN LATERAL (
|
|
336
|
+
SELECT array_agg(DISTINCT e2.canonical ORDER BY e2.canonical) AS entities
|
|
337
|
+
FROM weave.thread_entities te2
|
|
338
|
+
JOIN weave.entities e2 ON e2.id = te2.entity_id
|
|
339
|
+
WHERE te2.thread_id = t.id
|
|
340
|
+
) entity_data ON TRUE
|
|
341
|
+
WHERE t.organization_id = $1
|
|
342
|
+
AND t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
|
|
343
|
+
AND e.canonical = ANY($2::text[])
|
|
344
|
+
ORDER BY score DESC, t.approved_at DESC
|
|
345
|
+
LIMIT $3`;
|
|
346
|
+
async function searchApprovedThreadsByEntities(client, args) {
|
|
347
|
+
const result = await client.query({
|
|
348
|
+
text: exports.searchApprovedThreadsByEntitiesQuery,
|
|
349
|
+
values: [args.organizationId, args.entityCanonicals, args.resultLimit],
|
|
350
|
+
rowMode: "array"
|
|
351
|
+
});
|
|
352
|
+
return result.rows.map(row => {
|
|
353
|
+
return {
|
|
354
|
+
threadId: row[0],
|
|
355
|
+
text: row[1],
|
|
356
|
+
knowledgeType: row[2],
|
|
357
|
+
facetFields: row[3],
|
|
358
|
+
score: row[4],
|
|
359
|
+
classification: row[5],
|
|
360
|
+
documentId: row[6],
|
|
361
|
+
documentName: row[7],
|
|
362
|
+
sourceLocation: row[8],
|
|
363
|
+
approvedAt: row[9],
|
|
364
|
+
temporalValidity: row[10],
|
|
365
|
+
entities: row[11],
|
|
366
|
+
reinforcementCount: row[12],
|
|
367
|
+
conceptCluster: row[13]
|
|
368
|
+
};
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
exports.searchApprovedThreadsByEntitiesAndUserPermissionsQuery = `-- name: SearchApprovedThreadsByEntitiesAndUserPermissions :many
|
|
372
|
+
SELECT DISTINCT
|
|
373
|
+
t.id AS thread_id,
|
|
374
|
+
t.text,
|
|
375
|
+
t.knowledge_type,
|
|
376
|
+
t.facet_fields,
|
|
377
|
+
te.confidence::double precision AS score,
|
|
378
|
+
t.classification,
|
|
379
|
+
t.document_id,
|
|
380
|
+
d.filename AS document_name,
|
|
381
|
+
t.source_location,
|
|
382
|
+
t.approved_at,
|
|
383
|
+
t.temporal_validity,
|
|
384
|
+
COALESCE(entity_data.entities, '{}'::text[]) AS entities,
|
|
385
|
+
COALESCE(rs.reinforcement_count, 0) AS reinforcement_count,
|
|
386
|
+
COALESCE(cc.name, '') AS concept_cluster
|
|
387
|
+
FROM weave.threads t
|
|
388
|
+
JOIN weave.documents d ON d.id = t.document_id
|
|
389
|
+
JOIN weave.thread_entities te ON te.thread_id = t.id
|
|
390
|
+
JOIN weave.entities e ON e.id = te.entity_id
|
|
391
|
+
LEFT JOIN weave.thread_reinforcement_scores rs ON rs.thread_id = t.id
|
|
392
|
+
LEFT JOIN weave.concept_clusters cc ON cc.id = t.cluster_id
|
|
393
|
+
LEFT JOIN LATERAL (
|
|
394
|
+
SELECT array_agg(DISTINCT e2.canonical ORDER BY e2.canonical) AS entities
|
|
395
|
+
FROM weave.thread_entities te2
|
|
396
|
+
JOIN weave.entities e2 ON e2.id = te2.entity_id
|
|
397
|
+
WHERE te2.thread_id = t.id
|
|
398
|
+
) entity_data ON TRUE
|
|
399
|
+
WHERE t.organization_id = $1
|
|
400
|
+
AND t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
|
|
401
|
+
AND e.canonical = ANY($2::text[])
|
|
402
|
+
AND (
|
|
403
|
+
d.scope = 'DOCUMENT_SCOPE_ORG'
|
|
404
|
+
OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
|
|
405
|
+
AND d.uploaded_by_user_id = $3)
|
|
406
|
+
OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
|
|
407
|
+
AND EXISTS (
|
|
408
|
+
SELECT 1
|
|
409
|
+
FROM weave.user_sensitivity_clearances c
|
|
410
|
+
WHERE c.organization_id = $1
|
|
411
|
+
AND c.user_id = $3
|
|
412
|
+
AND cardinality(d.sensitivity_tags) = 1
|
|
413
|
+
AND c.sensitivity_tag_slug = d.sensitivity_tags[1]
|
|
414
|
+
))
|
|
415
|
+
)
|
|
416
|
+
ORDER BY score DESC, t.approved_at DESC
|
|
417
|
+
LIMIT $4`;
|
|
418
|
+
async function searchApprovedThreadsByEntitiesAndUserPermissions(client, args) {
|
|
419
|
+
const result = await client.query({
|
|
420
|
+
text: exports.searchApprovedThreadsByEntitiesAndUserPermissionsQuery,
|
|
421
|
+
values: [args.organizationId, args.entityCanonicals, args.actingUserId, args.resultLimit],
|
|
422
|
+
rowMode: "array"
|
|
423
|
+
});
|
|
424
|
+
return result.rows.map(row => {
|
|
425
|
+
return {
|
|
426
|
+
threadId: row[0],
|
|
427
|
+
text: row[1],
|
|
428
|
+
knowledgeType: row[2],
|
|
429
|
+
facetFields: row[3],
|
|
430
|
+
score: row[4],
|
|
431
|
+
classification: row[5],
|
|
432
|
+
documentId: row[6],
|
|
433
|
+
documentName: row[7],
|
|
434
|
+
sourceLocation: row[8],
|
|
435
|
+
approvedAt: row[9],
|
|
436
|
+
temporalValidity: row[10],
|
|
437
|
+
entities: row[11],
|
|
438
|
+
reinforcementCount: row[12],
|
|
439
|
+
conceptCluster: row[13]
|
|
440
|
+
};
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
exports.searchLeafConceptClustersBySimilarityQuery = `-- name: SearchLeafConceptClustersBySimilarity :many
|
|
444
|
+
SELECT
|
|
445
|
+
cc.id,
|
|
446
|
+
cc.organization_id,
|
|
447
|
+
cc.realm_kind,
|
|
448
|
+
cc.realm_key,
|
|
449
|
+
cc.name,
|
|
450
|
+
cc.description,
|
|
451
|
+
cc.knowledge_type,
|
|
452
|
+
cc.level,
|
|
453
|
+
cc.parent_id,
|
|
454
|
+
cc.thread_count,
|
|
455
|
+
cc.consolidation_run_id,
|
|
456
|
+
cc.created_at,
|
|
457
|
+
cc.updated_at,
|
|
458
|
+
similarity.score
|
|
459
|
+
FROM weave.concept_clusters cc
|
|
460
|
+
CROSS JOIN LATERAL (
|
|
461
|
+
SELECT CAST(
|
|
462
|
+
1.0::double precision
|
|
463
|
+
- (cc.embedding <=> $1::vector)::double precision
|
|
464
|
+
AS double precision
|
|
465
|
+
) AS score
|
|
466
|
+
) similarity
|
|
467
|
+
WHERE cc.organization_id = $2
|
|
468
|
+
AND cc.realm_kind = $3
|
|
469
|
+
AND cc.realm_key = $4
|
|
470
|
+
AND cc.level = 0
|
|
471
|
+
AND cc.embedding IS NOT NULL
|
|
472
|
+
AND similarity.score >= $5::double precision
|
|
473
|
+
ORDER BY score DESC, cc.updated_at DESC
|
|
474
|
+
LIMIT $6`;
|
|
475
|
+
async function searchLeafConceptClustersBySimilarity(client, args) {
|
|
476
|
+
const result = await client.query({
|
|
477
|
+
text: exports.searchLeafConceptClustersBySimilarityQuery,
|
|
478
|
+
values: [args.queryEmbedding, args.organizationId, args.realmKind, args.realmKey, args.minSimilarity, args.resultLimit],
|
|
479
|
+
rowMode: "array"
|
|
480
|
+
});
|
|
481
|
+
return result.rows.map(row => {
|
|
482
|
+
return {
|
|
483
|
+
id: row[0],
|
|
484
|
+
organizationId: row[1],
|
|
485
|
+
realmKind: row[2],
|
|
486
|
+
realmKey: row[3],
|
|
487
|
+
name: row[4],
|
|
488
|
+
description: row[5],
|
|
489
|
+
knowledgeType: row[6],
|
|
490
|
+
level: row[7],
|
|
491
|
+
parentId: row[8],
|
|
492
|
+
threadCount: row[9],
|
|
493
|
+
consolidationRunId: row[10],
|
|
494
|
+
createdAt: row[11],
|
|
495
|
+
updatedAt: row[12],
|
|
496
|
+
score: row[13]
|
|
497
|
+
};
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
exports.searchConceptClusterThreadsBySimilarityQuery = `-- name: SearchConceptClusterThreadsBySimilarity :many
|
|
501
|
+
SELECT
|
|
502
|
+
t.id AS thread_id,
|
|
503
|
+
t.text,
|
|
504
|
+
t.knowledge_type,
|
|
505
|
+
t.facet_fields,
|
|
506
|
+
similarity.score,
|
|
507
|
+
t.classification,
|
|
508
|
+
t.document_id,
|
|
509
|
+
d.filename AS document_name,
|
|
510
|
+
t.source_location,
|
|
511
|
+
t.approved_at,
|
|
512
|
+
t.temporal_validity,
|
|
513
|
+
COALESCE(entity_data.entities, '{}'::text[]) AS entities,
|
|
514
|
+
COALESCE(rs.reinforcement_count, 0) AS reinforcement_count,
|
|
515
|
+
cc.name AS concept_cluster
|
|
516
|
+
FROM weave.concept_clusters cc
|
|
517
|
+
CROSS JOIN LATERAL (
|
|
518
|
+
SELECT CAST(
|
|
519
|
+
1.0::double precision
|
|
520
|
+
- (cc.embedding <=> $1::vector)::double precision
|
|
521
|
+
AS double precision
|
|
522
|
+
) AS score
|
|
523
|
+
) similarity
|
|
524
|
+
JOIN weave.threads t ON t.cluster_id = cc.id
|
|
525
|
+
JOIN weave.documents d ON d.id = t.document_id
|
|
526
|
+
LEFT JOIN weave.thread_reinforcement_scores rs ON rs.thread_id = t.id
|
|
527
|
+
LEFT JOIN LATERAL (
|
|
528
|
+
SELECT array_agg(DISTINCT e.canonical ORDER BY e.canonical) AS entities
|
|
529
|
+
FROM weave.thread_entities te
|
|
530
|
+
JOIN weave.entities e ON e.id = te.entity_id
|
|
531
|
+
WHERE te.thread_id = t.id
|
|
532
|
+
) entity_data ON TRUE
|
|
533
|
+
WHERE cc.organization_id = $2
|
|
534
|
+
AND cc.realm_kind = $3
|
|
535
|
+
AND cc.realm_key = $4
|
|
536
|
+
AND cc.level = 0
|
|
537
|
+
AND cc.embedding IS NOT NULL
|
|
538
|
+
AND t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
|
|
539
|
+
AND similarity.score >= $5::double precision
|
|
540
|
+
ORDER BY score DESC, t.approved_at DESC
|
|
541
|
+
LIMIT $6`;
|
|
542
|
+
async function searchConceptClusterThreadsBySimilarity(client, args) {
|
|
543
|
+
const result = await client.query({
|
|
544
|
+
text: exports.searchConceptClusterThreadsBySimilarityQuery,
|
|
545
|
+
values: [args.queryEmbedding, args.organizationId, args.realmKind, args.realmKey, args.minSimilarity, args.resultLimit],
|
|
546
|
+
rowMode: "array"
|
|
547
|
+
});
|
|
548
|
+
return result.rows.map(row => {
|
|
549
|
+
return {
|
|
550
|
+
threadId: row[0],
|
|
551
|
+
text: row[1],
|
|
552
|
+
knowledgeType: row[2],
|
|
553
|
+
facetFields: row[3],
|
|
554
|
+
score: row[4],
|
|
555
|
+
classification: row[5],
|
|
556
|
+
documentId: row[6],
|
|
557
|
+
documentName: row[7],
|
|
558
|
+
sourceLocation: row[8],
|
|
559
|
+
approvedAt: row[9],
|
|
560
|
+
temporalValidity: row[10],
|
|
561
|
+
entities: row[11],
|
|
562
|
+
reinforcementCount: row[12],
|
|
563
|
+
conceptCluster: row[13]
|
|
564
|
+
};
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
exports.searchConceptClusterThreadsBySimilarityAndUserPermissionsQuery = `-- name: SearchConceptClusterThreadsBySimilarityAndUserPermissions :many
|
|
568
|
+
SELECT
|
|
569
|
+
t.id AS thread_id,
|
|
570
|
+
t.text,
|
|
571
|
+
t.knowledge_type,
|
|
572
|
+
t.facet_fields,
|
|
573
|
+
similarity.score,
|
|
574
|
+
t.classification,
|
|
575
|
+
t.document_id,
|
|
576
|
+
d.filename AS document_name,
|
|
577
|
+
t.source_location,
|
|
578
|
+
t.approved_at,
|
|
579
|
+
t.temporal_validity,
|
|
580
|
+
COALESCE(entity_data.entities, '{}'::text[]) AS entities,
|
|
581
|
+
COALESCE(rs.reinforcement_count, 0) AS reinforcement_count,
|
|
582
|
+
cc.name AS concept_cluster
|
|
583
|
+
FROM weave.concept_clusters cc
|
|
584
|
+
CROSS JOIN LATERAL (
|
|
585
|
+
SELECT CAST(
|
|
586
|
+
1.0::double precision
|
|
587
|
+
- (cc.embedding <=> $1::vector)::double precision
|
|
588
|
+
AS double precision
|
|
589
|
+
) AS score
|
|
590
|
+
) similarity
|
|
591
|
+
JOIN weave.threads t ON t.cluster_id = cc.id
|
|
592
|
+
JOIN weave.documents d ON d.id = t.document_id
|
|
593
|
+
LEFT JOIN weave.thread_reinforcement_scores rs ON rs.thread_id = t.id
|
|
594
|
+
LEFT JOIN LATERAL (
|
|
595
|
+
SELECT array_agg(DISTINCT e.canonical ORDER BY e.canonical) AS entities
|
|
596
|
+
FROM weave.thread_entities te
|
|
597
|
+
JOIN weave.entities e ON e.id = te.entity_id
|
|
598
|
+
WHERE te.thread_id = t.id
|
|
599
|
+
) entity_data ON TRUE
|
|
600
|
+
WHERE cc.organization_id = $2
|
|
601
|
+
AND cc.realm_kind = $3
|
|
602
|
+
AND cc.realm_key = $4
|
|
603
|
+
AND cc.level = 0
|
|
604
|
+
AND cc.embedding IS NOT NULL
|
|
605
|
+
AND t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
|
|
606
|
+
AND similarity.score >= $5::double precision
|
|
607
|
+
AND (
|
|
608
|
+
d.scope = 'DOCUMENT_SCOPE_ORG'
|
|
609
|
+
OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
|
|
610
|
+
AND d.uploaded_by_user_id = $6)
|
|
611
|
+
OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
|
|
612
|
+
AND EXISTS (
|
|
613
|
+
SELECT 1
|
|
614
|
+
FROM weave.user_sensitivity_clearances c
|
|
615
|
+
WHERE c.organization_id = $2
|
|
616
|
+
AND c.user_id = $6
|
|
617
|
+
AND cardinality(d.sensitivity_tags) = 1
|
|
618
|
+
AND c.sensitivity_tag_slug = d.sensitivity_tags[1]
|
|
619
|
+
))
|
|
620
|
+
)
|
|
621
|
+
ORDER BY score DESC, t.approved_at DESC
|
|
622
|
+
LIMIT $7`;
|
|
623
|
+
async function searchConceptClusterThreadsBySimilarityAndUserPermissions(client, args) {
|
|
624
|
+
const result = await client.query({
|
|
625
|
+
text: exports.searchConceptClusterThreadsBySimilarityAndUserPermissionsQuery,
|
|
626
|
+
values: [args.queryEmbedding, args.organizationId, args.realmKind, args.realmKey, args.minSimilarity, args.actingUserId, args.resultLimit],
|
|
627
|
+
rowMode: "array"
|
|
628
|
+
});
|
|
629
|
+
return result.rows.map(row => {
|
|
630
|
+
return {
|
|
631
|
+
threadId: row[0],
|
|
632
|
+
text: row[1],
|
|
633
|
+
knowledgeType: row[2],
|
|
634
|
+
facetFields: row[3],
|
|
635
|
+
score: row[4],
|
|
636
|
+
classification: row[5],
|
|
637
|
+
documentId: row[6],
|
|
638
|
+
documentName: row[7],
|
|
639
|
+
sourceLocation: row[8],
|
|
640
|
+
approvedAt: row[9],
|
|
641
|
+
temporalValidity: row[10],
|
|
642
|
+
entities: row[11],
|
|
643
|
+
reinforcementCount: row[12],
|
|
644
|
+
conceptCluster: row[13]
|
|
645
|
+
};
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
exports.insertSessionRetrievalContextQuery = `-- name: InsertSessionRetrievalContext :one
|
|
649
|
+
INSERT INTO weave.session_retrieval_context (
|
|
650
|
+
session_id,
|
|
651
|
+
turn_number,
|
|
652
|
+
thread_id,
|
|
653
|
+
relevance
|
|
654
|
+
) SELECT
|
|
655
|
+
$1,
|
|
656
|
+
$2,
|
|
657
|
+
t.id,
|
|
658
|
+
$3
|
|
659
|
+
FROM weave.threads t
|
|
660
|
+
WHERE t.organization_id = $4
|
|
661
|
+
AND t.id = $5
|
|
662
|
+
RETURNING
|
|
663
|
+
session_id,
|
|
664
|
+
turn_number,
|
|
665
|
+
thread_id,
|
|
666
|
+
relevance,
|
|
667
|
+
retrieved_at`;
|
|
668
|
+
async function insertSessionRetrievalContext(client, args) {
|
|
669
|
+
const result = await client.query({
|
|
670
|
+
text: exports.insertSessionRetrievalContextQuery,
|
|
671
|
+
values: [args.sessionId, args.turnNumber, args.relevance, args.organizationId, args.threadId],
|
|
672
|
+
rowMode: "array"
|
|
673
|
+
});
|
|
674
|
+
if (result.rows.length !== 1) {
|
|
675
|
+
return null;
|
|
676
|
+
}
|
|
677
|
+
const row = result.rows[0];
|
|
678
|
+
return {
|
|
679
|
+
sessionId: row[0],
|
|
680
|
+
turnNumber: row[1],
|
|
681
|
+
threadId: row[2],
|
|
682
|
+
relevance: row[3],
|
|
683
|
+
retrievedAt: row[4]
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
exports.listSessionRetrievalContextQuery = `-- name: ListSessionRetrievalContext :many
|
|
687
|
+
SELECT
|
|
688
|
+
session_id,
|
|
689
|
+
turn_number,
|
|
690
|
+
thread_id,
|
|
691
|
+
relevance,
|
|
692
|
+
retrieved_at
|
|
693
|
+
FROM weave.session_retrieval_context
|
|
694
|
+
WHERE session_id = $1
|
|
695
|
+
AND thread_id IN (
|
|
696
|
+
SELECT id
|
|
697
|
+
FROM weave.threads
|
|
698
|
+
WHERE organization_id = $2
|
|
699
|
+
)
|
|
700
|
+
ORDER BY turn_number DESC, relevance DESC, retrieved_at DESC
|
|
701
|
+
LIMIT $3`;
|
|
702
|
+
async function listSessionRetrievalContext(client, args) {
|
|
703
|
+
const result = await client.query({
|
|
704
|
+
text: exports.listSessionRetrievalContextQuery,
|
|
705
|
+
values: [args.sessionId, args.organizationId, args.resultLimit],
|
|
706
|
+
rowMode: "array"
|
|
707
|
+
});
|
|
708
|
+
return result.rows.map(row => {
|
|
709
|
+
return {
|
|
710
|
+
sessionId: row[0],
|
|
711
|
+
turnNumber: row[1],
|
|
712
|
+
threadId: row[2],
|
|
713
|
+
relevance: row[3],
|
|
714
|
+
retrievedAt: row[4]
|
|
715
|
+
};
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
exports.deleteSessionRetrievalContextQuery = `-- name: DeleteSessionRetrievalContext :exec
|
|
719
|
+
DELETE FROM weave.session_retrieval_context
|
|
720
|
+
WHERE session_id = $1
|
|
721
|
+
AND thread_id IN (
|
|
722
|
+
SELECT id
|
|
723
|
+
FROM weave.threads
|
|
724
|
+
WHERE organization_id = $2
|
|
725
|
+
)`;
|
|
726
|
+
async function deleteSessionRetrievalContext(client, args) {
|
|
727
|
+
await client.query({
|
|
728
|
+
text: exports.deleteSessionRetrievalContextQuery,
|
|
729
|
+
values: [args.sessionId, args.organizationId],
|
|
730
|
+
rowMode: "array"
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
exports.listApprovedReconcilePoolByOrganizationQuery = `-- name: ListApprovedReconcilePoolByOrganization :many
|
|
734
|
+
SELECT
|
|
735
|
+
t.id AS thread_id,
|
|
736
|
+
t.text,
|
|
737
|
+
t.knowledge_type,
|
|
738
|
+
t.embedding::text AS embedding_text,
|
|
739
|
+
COALESCE(entity_data.entities, '{}'::text[]) AS entity_canonicals,
|
|
740
|
+
t.approved_at
|
|
741
|
+
FROM weave.threads t
|
|
742
|
+
LEFT JOIN LATERAL (
|
|
743
|
+
SELECT array_agg(DISTINCT e.canonical ORDER BY e.canonical) AS entities
|
|
744
|
+
FROM weave.thread_entities te
|
|
745
|
+
JOIN weave.entities e ON e.id = te.entity_id
|
|
746
|
+
WHERE te.thread_id = t.id
|
|
747
|
+
) entity_data ON TRUE
|
|
748
|
+
WHERE t.organization_id = $1
|
|
749
|
+
AND t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
|
|
750
|
+
AND t.embedding IS NOT NULL
|
|
751
|
+
ORDER BY t.approved_at DESC, t.id ASC
|
|
752
|
+
LIMIT $3 OFFSET $2`;
|
|
753
|
+
async function listApprovedReconcilePoolByOrganization(client, args) {
|
|
754
|
+
const result = await client.query({
|
|
755
|
+
text: exports.listApprovedReconcilePoolByOrganizationQuery,
|
|
756
|
+
values: [args.organizationId, args.pageOffset, args.pageSize],
|
|
757
|
+
rowMode: "array"
|
|
758
|
+
});
|
|
759
|
+
return result.rows.map(row => {
|
|
760
|
+
return {
|
|
761
|
+
threadId: row[0],
|
|
762
|
+
text: row[1],
|
|
763
|
+
knowledgeType: row[2],
|
|
764
|
+
embeddingText: row[3],
|
|
765
|
+
entityCanonicals: row[4],
|
|
766
|
+
approvedAt: row[5]
|
|
767
|
+
};
|
|
768
|
+
});
|
|
769
|
+
}
|