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.
@@ -0,0 +1,2503 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.upsertFacetConfigQuery = exports.listActiveFacetConfigsQuery = exports.listFacetConfigsQuery = exports.syncOrganizationIngestionReasoningDefaultsQuery = exports.upsertOrganizationIngestionSettingsQuery = exports.getOrganizationIngestionSettingsQuery = exports.ensureOrganizationIngestionSettingsQuery = exports.listEntitiesByThreadQuery = exports.upsertEntityRelationshipQuery = exports.upsertThreadEntityQuery = exports.upsertEntityQuery = exports.listThreadActivityByThreadQuery = exports.createThreadActivityQuery = exports.listThreadReconciliationByPendingThreadQuery = exports.upsertThreadReconciliationQuery = exports.deleteThreadReconciliationByPendingThreadQuery = exports.getIngestionRunThreadCountsQuery = exports.listThreadsByIDsQuery = exports.listThreadsBySupersedesQuery = exports.markThreadsSupersededQuery = exports.bulkRejectThreadsQuery = exports.rejectThreadQuery = exports.bulkApproveThreadsQuery = exports.approveThreadQuery = exports.editPendingThreadQuery = exports.updateThreadClassificationQuery = exports.promoteThreadNextEmbeddingsByOrganizationQuery = exports.setThreadNextEmbeddingQuery = exports.updateThreadEmbeddingQuery = exports.listPendingThreadsByOrganizationQuery = exports.listPendingThreadsByRunQuery = exports.listThreadsByIngestionRunQuery = exports.getThreadByOrganizationAndIDQuery = exports.createThreadQuery = exports.listDocumentTagsByDocumentIDQuery = exports.createDocumentTagQuery = exports.deleteDocumentTagsByDocumentIDQuery = exports.listIngestionHistoryQuery = exports.listIngestionRunsByDocumentQuery = exports.listIngestionRunsByOrganizationQuery = exports.updateIngestionRunStateQuery = exports.getIngestionRunByOrganizationAndIDQuery = exports.createIngestionRunQuery = exports.getDatabaseConfigQuery = exports.listDocumentsByOrganizationQuery = exports.setDocumentStatusQuery = exports.setDocumentParseResultQuery = exports.getDocumentByOrganizationAndFingerprintQuery = exports.getDocumentByOrganizationAndIDQuery = exports.createDocumentQuery = void 0;
4
+ exports.deactivateFacetConfigQuery = void 0;
5
+ exports.createDocument = createDocument;
6
+ exports.getDocumentByOrganizationAndID = getDocumentByOrganizationAndID;
7
+ exports.getDocumentByOrganizationAndFingerprint = getDocumentByOrganizationAndFingerprint;
8
+ exports.setDocumentParseResult = setDocumentParseResult;
9
+ exports.setDocumentStatus = setDocumentStatus;
10
+ exports.listDocumentsByOrganization = listDocumentsByOrganization;
11
+ exports.getDatabaseConfig = getDatabaseConfig;
12
+ exports.createIngestionRun = createIngestionRun;
13
+ exports.getIngestionRunByOrganizationAndID = getIngestionRunByOrganizationAndID;
14
+ exports.updateIngestionRunState = updateIngestionRunState;
15
+ exports.listIngestionRunsByOrganization = listIngestionRunsByOrganization;
16
+ exports.listIngestionRunsByDocument = listIngestionRunsByDocument;
17
+ exports.listIngestionHistory = listIngestionHistory;
18
+ exports.deleteDocumentTagsByDocumentID = deleteDocumentTagsByDocumentID;
19
+ exports.createDocumentTag = createDocumentTag;
20
+ exports.listDocumentTagsByDocumentID = listDocumentTagsByDocumentID;
21
+ exports.createThread = createThread;
22
+ exports.getThreadByOrganizationAndID = getThreadByOrganizationAndID;
23
+ exports.listThreadsByIngestionRun = listThreadsByIngestionRun;
24
+ exports.listPendingThreadsByRun = listPendingThreadsByRun;
25
+ exports.listPendingThreadsByOrganization = listPendingThreadsByOrganization;
26
+ exports.updateThreadEmbedding = updateThreadEmbedding;
27
+ exports.setThreadNextEmbedding = setThreadNextEmbedding;
28
+ exports.updateThreadClassification = updateThreadClassification;
29
+ exports.editPendingThread = editPendingThread;
30
+ exports.approveThread = approveThread;
31
+ exports.bulkApproveThreads = bulkApproveThreads;
32
+ exports.rejectThread = rejectThread;
33
+ exports.bulkRejectThreads = bulkRejectThreads;
34
+ exports.listThreadsBySupersedes = listThreadsBySupersedes;
35
+ exports.listThreadsByIDs = listThreadsByIDs;
36
+ exports.getIngestionRunThreadCounts = getIngestionRunThreadCounts;
37
+ exports.deleteThreadReconciliationByPendingThread = deleteThreadReconciliationByPendingThread;
38
+ exports.upsertThreadReconciliation = upsertThreadReconciliation;
39
+ exports.listThreadReconciliationByPendingThread = listThreadReconciliationByPendingThread;
40
+ exports.createThreadActivity = createThreadActivity;
41
+ exports.listThreadActivityByThread = listThreadActivityByThread;
42
+ exports.upsertEntity = upsertEntity;
43
+ exports.upsertThreadEntity = upsertThreadEntity;
44
+ exports.upsertEntityRelationship = upsertEntityRelationship;
45
+ exports.listEntitiesByThread = listEntitiesByThread;
46
+ exports.ensureOrganizationIngestionSettings = ensureOrganizationIngestionSettings;
47
+ exports.getOrganizationIngestionSettings = getOrganizationIngestionSettings;
48
+ exports.upsertOrganizationIngestionSettings = upsertOrganizationIngestionSettings;
49
+ exports.syncOrganizationIngestionReasoningDefaults = syncOrganizationIngestionReasoningDefaults;
50
+ exports.listFacetConfigs = listFacetConfigs;
51
+ exports.listActiveFacetConfigs = listActiveFacetConfigs;
52
+ exports.upsertFacetConfig = upsertFacetConfig;
53
+ exports.deactivateFacetConfig = deactivateFacetConfig;
54
+ exports.createDocumentQuery = `-- name: CreateDocument :one
55
+ INSERT INTO weave.documents (
56
+ id,
57
+ organization_id,
58
+ filename,
59
+ mime_type,
60
+ fingerprint,
61
+ storage_ref,
62
+ status,
63
+ scope,
64
+ sensitivity_tags,
65
+ uploaded_by_user_id,
66
+ metadata
67
+ ) VALUES (
68
+ $1,
69
+ $2,
70
+ $3,
71
+ $4,
72
+ $5,
73
+ $6,
74
+ $7,
75
+ $8,
76
+ $9,
77
+ $10,
78
+ $11
79
+ )
80
+ RETURNING
81
+ id,
82
+ organization_id,
83
+ filename,
84
+ mime_type,
85
+ parser_name,
86
+ fingerprint,
87
+ storage_ref,
88
+ status,
89
+ scope,
90
+ sensitivity_tags,
91
+ uploaded_by_user_id,
92
+ uploaded_at,
93
+ updated_at,
94
+ page_count,
95
+ char_count,
96
+ metadata`;
97
+ async function createDocument(client, args) {
98
+ const result = await client.query({
99
+ text: exports.createDocumentQuery,
100
+ values: [args.id, args.organizationId, args.filename, args.mimeType, args.fingerprint, args.storageRef, args.status, args.scope, args.sensitivityTags, args.uploadedByUserId, args.metadata],
101
+ rowMode: "array"
102
+ });
103
+ if (result.rows.length !== 1) {
104
+ return null;
105
+ }
106
+ const row = result.rows[0];
107
+ return {
108
+ id: row[0],
109
+ organizationId: row[1],
110
+ filename: row[2],
111
+ mimeType: row[3],
112
+ parserName: row[4],
113
+ fingerprint: row[5],
114
+ storageRef: row[6],
115
+ status: row[7],
116
+ scope: row[8],
117
+ sensitivityTags: row[9],
118
+ uploadedByUserId: row[10],
119
+ uploadedAt: row[11],
120
+ updatedAt: row[12],
121
+ pageCount: row[13],
122
+ charCount: row[14],
123
+ metadata: row[15]
124
+ };
125
+ }
126
+ exports.getDocumentByOrganizationAndIDQuery = `-- name: GetDocumentByOrganizationAndID :one
127
+ SELECT
128
+ id,
129
+ organization_id,
130
+ filename,
131
+ mime_type,
132
+ parser_name,
133
+ fingerprint,
134
+ storage_ref,
135
+ status,
136
+ scope,
137
+ sensitivity_tags,
138
+ uploaded_by_user_id,
139
+ uploaded_at,
140
+ updated_at,
141
+ page_count,
142
+ char_count,
143
+ metadata
144
+ FROM weave.documents
145
+ WHERE organization_id = $1
146
+ AND id = $2`;
147
+ async function getDocumentByOrganizationAndID(client, args) {
148
+ const result = await client.query({
149
+ text: exports.getDocumentByOrganizationAndIDQuery,
150
+ values: [args.organizationId, args.id],
151
+ rowMode: "array"
152
+ });
153
+ if (result.rows.length !== 1) {
154
+ return null;
155
+ }
156
+ const row = result.rows[0];
157
+ return {
158
+ id: row[0],
159
+ organizationId: row[1],
160
+ filename: row[2],
161
+ mimeType: row[3],
162
+ parserName: row[4],
163
+ fingerprint: row[5],
164
+ storageRef: row[6],
165
+ status: row[7],
166
+ scope: row[8],
167
+ sensitivityTags: row[9],
168
+ uploadedByUserId: row[10],
169
+ uploadedAt: row[11],
170
+ updatedAt: row[12],
171
+ pageCount: row[13],
172
+ charCount: row[14],
173
+ metadata: row[15]
174
+ };
175
+ }
176
+ exports.getDocumentByOrganizationAndFingerprintQuery = `-- name: GetDocumentByOrganizationAndFingerprint :one
177
+ SELECT
178
+ id,
179
+ organization_id,
180
+ filename,
181
+ mime_type,
182
+ parser_name,
183
+ fingerprint,
184
+ storage_ref,
185
+ status,
186
+ scope,
187
+ sensitivity_tags,
188
+ uploaded_by_user_id,
189
+ uploaded_at,
190
+ updated_at,
191
+ page_count,
192
+ char_count,
193
+ metadata
194
+ FROM weave.documents
195
+ WHERE organization_id = $1
196
+ AND fingerprint = $2`;
197
+ async function getDocumentByOrganizationAndFingerprint(client, args) {
198
+ const result = await client.query({
199
+ text: exports.getDocumentByOrganizationAndFingerprintQuery,
200
+ values: [args.organizationId, args.fingerprint],
201
+ rowMode: "array"
202
+ });
203
+ if (result.rows.length !== 1) {
204
+ return null;
205
+ }
206
+ const row = result.rows[0];
207
+ return {
208
+ id: row[0],
209
+ organizationId: row[1],
210
+ filename: row[2],
211
+ mimeType: row[3],
212
+ parserName: row[4],
213
+ fingerprint: row[5],
214
+ storageRef: row[6],
215
+ status: row[7],
216
+ scope: row[8],
217
+ sensitivityTags: row[9],
218
+ uploadedByUserId: row[10],
219
+ uploadedAt: row[11],
220
+ updatedAt: row[12],
221
+ pageCount: row[13],
222
+ charCount: row[14],
223
+ metadata: row[15]
224
+ };
225
+ }
226
+ exports.setDocumentParseResultQuery = `-- name: SetDocumentParseResult :one
227
+ UPDATE weave.documents
228
+ SET
229
+ parser_name = $1,
230
+ status = $2,
231
+ page_count = $3,
232
+ char_count = $4,
233
+ updated_at = now()
234
+ WHERE organization_id = $5
235
+ AND id = $6
236
+ RETURNING
237
+ id,
238
+ organization_id,
239
+ filename,
240
+ mime_type,
241
+ parser_name,
242
+ fingerprint,
243
+ storage_ref,
244
+ status,
245
+ scope,
246
+ sensitivity_tags,
247
+ uploaded_by_user_id,
248
+ uploaded_at,
249
+ updated_at,
250
+ page_count,
251
+ char_count,
252
+ metadata`;
253
+ async function setDocumentParseResult(client, args) {
254
+ const result = await client.query({
255
+ text: exports.setDocumentParseResultQuery,
256
+ values: [args.parserName, args.status, args.pageCount, args.charCount, args.organizationId, args.id],
257
+ rowMode: "array"
258
+ });
259
+ if (result.rows.length !== 1) {
260
+ return null;
261
+ }
262
+ const row = result.rows[0];
263
+ return {
264
+ id: row[0],
265
+ organizationId: row[1],
266
+ filename: row[2],
267
+ mimeType: row[3],
268
+ parserName: row[4],
269
+ fingerprint: row[5],
270
+ storageRef: row[6],
271
+ status: row[7],
272
+ scope: row[8],
273
+ sensitivityTags: row[9],
274
+ uploadedByUserId: row[10],
275
+ uploadedAt: row[11],
276
+ updatedAt: row[12],
277
+ pageCount: row[13],
278
+ charCount: row[14],
279
+ metadata: row[15]
280
+ };
281
+ }
282
+ exports.setDocumentStatusQuery = `-- name: SetDocumentStatus :one
283
+ UPDATE weave.documents
284
+ SET
285
+ status = $1,
286
+ updated_at = now()
287
+ WHERE organization_id = $2
288
+ AND id = $3
289
+ RETURNING
290
+ id,
291
+ organization_id,
292
+ filename,
293
+ mime_type,
294
+ parser_name,
295
+ fingerprint,
296
+ storage_ref,
297
+ status,
298
+ scope,
299
+ sensitivity_tags,
300
+ uploaded_by_user_id,
301
+ uploaded_at,
302
+ updated_at,
303
+ page_count,
304
+ char_count,
305
+ metadata`;
306
+ async function setDocumentStatus(client, args) {
307
+ const result = await client.query({
308
+ text: exports.setDocumentStatusQuery,
309
+ values: [args.status, args.organizationId, args.id],
310
+ rowMode: "array"
311
+ });
312
+ if (result.rows.length !== 1) {
313
+ return null;
314
+ }
315
+ const row = result.rows[0];
316
+ return {
317
+ id: row[0],
318
+ organizationId: row[1],
319
+ filename: row[2],
320
+ mimeType: row[3],
321
+ parserName: row[4],
322
+ fingerprint: row[5],
323
+ storageRef: row[6],
324
+ status: row[7],
325
+ scope: row[8],
326
+ sensitivityTags: row[9],
327
+ uploadedByUserId: row[10],
328
+ uploadedAt: row[11],
329
+ updatedAt: row[12],
330
+ pageCount: row[13],
331
+ charCount: row[14],
332
+ metadata: row[15]
333
+ };
334
+ }
335
+ exports.listDocumentsByOrganizationQuery = `-- name: ListDocumentsByOrganization :many
336
+ SELECT
337
+ id,
338
+ organization_id,
339
+ filename,
340
+ mime_type,
341
+ parser_name,
342
+ fingerprint,
343
+ storage_ref,
344
+ status,
345
+ scope,
346
+ sensitivity_tags,
347
+ uploaded_by_user_id,
348
+ uploaded_at,
349
+ updated_at,
350
+ page_count,
351
+ char_count,
352
+ metadata
353
+ FROM weave.documents
354
+ WHERE organization_id = $1
355
+ ORDER BY uploaded_at DESC
356
+ LIMIT $3 OFFSET $2`;
357
+ async function listDocumentsByOrganization(client, args) {
358
+ const result = await client.query({
359
+ text: exports.listDocumentsByOrganizationQuery,
360
+ values: [args.organizationId, args.pageOffset, args.pageSize],
361
+ rowMode: "array"
362
+ });
363
+ return result.rows.map(row => {
364
+ return {
365
+ id: row[0],
366
+ organizationId: row[1],
367
+ filename: row[2],
368
+ mimeType: row[3],
369
+ parserName: row[4],
370
+ fingerprint: row[5],
371
+ storageRef: row[6],
372
+ status: row[7],
373
+ scope: row[8],
374
+ sensitivityTags: row[9],
375
+ uploadedByUserId: row[10],
376
+ uploadedAt: row[11],
377
+ updatedAt: row[12],
378
+ pageCount: row[13],
379
+ charCount: row[14],
380
+ metadata: row[15]
381
+ };
382
+ });
383
+ }
384
+ exports.getDatabaseConfigQuery = `-- name: GetDatabaseConfig :one
385
+ SELECT
386
+ id,
387
+ embedding_dimension,
388
+ deployment_mode,
389
+ created_at,
390
+ updated_at
391
+ FROM weave.database_config
392
+ WHERE id = 1`;
393
+ async function getDatabaseConfig(client) {
394
+ const result = await client.query({
395
+ text: exports.getDatabaseConfigQuery,
396
+ values: [],
397
+ rowMode: "array"
398
+ });
399
+ if (result.rows.length !== 1) {
400
+ return null;
401
+ }
402
+ const row = result.rows[0];
403
+ return {
404
+ id: row[0],
405
+ embeddingDimension: row[1],
406
+ deploymentMode: row[2],
407
+ createdAt: row[3],
408
+ updatedAt: row[4]
409
+ };
410
+ }
411
+ exports.createIngestionRunQuery = `-- name: CreateIngestionRun :one
412
+ INSERT INTO weave.ingestion_runs (
413
+ id,
414
+ organization_id,
415
+ document_id,
416
+ status,
417
+ reasoning_model_id,
418
+ reasoning_provider_configuration_id,
419
+ embedding_model_id,
420
+ embedding_provider_configuration_id,
421
+ embedding_dimensions
422
+ ) VALUES (
423
+ $1,
424
+ $2,
425
+ $3,
426
+ $4,
427
+ $5,
428
+ $6,
429
+ $7,
430
+ $8,
431
+ $9
432
+ )
433
+ RETURNING
434
+ id,
435
+ organization_id,
436
+ document_id,
437
+ status,
438
+ reasoning_model_id,
439
+ reasoning_provider_configuration_id,
440
+ embedding_model_id,
441
+ embedding_provider_configuration_id,
442
+ embedding_dimensions,
443
+ thread_count,
444
+ pending_count,
445
+ approved_count,
446
+ rejected_count,
447
+ error,
448
+ token_usage,
449
+ started_at,
450
+ completed_at,
451
+ committed_at`;
452
+ async function createIngestionRun(client, args) {
453
+ const result = await client.query({
454
+ text: exports.createIngestionRunQuery,
455
+ values: [args.id, args.organizationId, args.documentId, args.status, args.reasoningModelId, args.reasoningProviderConfigurationId, args.embeddingModelId, args.embeddingProviderConfigurationId, args.embeddingDimensions],
456
+ rowMode: "array"
457
+ });
458
+ if (result.rows.length !== 1) {
459
+ return null;
460
+ }
461
+ const row = result.rows[0];
462
+ return {
463
+ id: row[0],
464
+ organizationId: row[1],
465
+ documentId: row[2],
466
+ status: row[3],
467
+ reasoningModelId: row[4],
468
+ reasoningProviderConfigurationId: row[5],
469
+ embeddingModelId: row[6],
470
+ embeddingProviderConfigurationId: row[7],
471
+ embeddingDimensions: row[8],
472
+ threadCount: row[9],
473
+ pendingCount: row[10],
474
+ approvedCount: row[11],
475
+ rejectedCount: row[12],
476
+ error: row[13],
477
+ tokenUsage: row[14],
478
+ startedAt: row[15],
479
+ completedAt: row[16],
480
+ committedAt: row[17]
481
+ };
482
+ }
483
+ exports.getIngestionRunByOrganizationAndIDQuery = `-- name: GetIngestionRunByOrganizationAndID :one
484
+ SELECT
485
+ id,
486
+ organization_id,
487
+ document_id,
488
+ status,
489
+ reasoning_model_id,
490
+ reasoning_provider_configuration_id,
491
+ embedding_model_id,
492
+ embedding_provider_configuration_id,
493
+ embedding_dimensions,
494
+ thread_count,
495
+ pending_count,
496
+ approved_count,
497
+ rejected_count,
498
+ error,
499
+ token_usage,
500
+ started_at,
501
+ completed_at,
502
+ committed_at
503
+ FROM weave.ingestion_runs
504
+ WHERE organization_id = $1
505
+ AND id = $2`;
506
+ async function getIngestionRunByOrganizationAndID(client, args) {
507
+ const result = await client.query({
508
+ text: exports.getIngestionRunByOrganizationAndIDQuery,
509
+ values: [args.organizationId, args.id],
510
+ rowMode: "array"
511
+ });
512
+ if (result.rows.length !== 1) {
513
+ return null;
514
+ }
515
+ const row = result.rows[0];
516
+ return {
517
+ id: row[0],
518
+ organizationId: row[1],
519
+ documentId: row[2],
520
+ status: row[3],
521
+ reasoningModelId: row[4],
522
+ reasoningProviderConfigurationId: row[5],
523
+ embeddingModelId: row[6],
524
+ embeddingProviderConfigurationId: row[7],
525
+ embeddingDimensions: row[8],
526
+ threadCount: row[9],
527
+ pendingCount: row[10],
528
+ approvedCount: row[11],
529
+ rejectedCount: row[12],
530
+ error: row[13],
531
+ tokenUsage: row[14],
532
+ startedAt: row[15],
533
+ completedAt: row[16],
534
+ committedAt: row[17]
535
+ };
536
+ }
537
+ exports.updateIngestionRunStateQuery = `-- name: UpdateIngestionRunState :one
538
+ UPDATE weave.ingestion_runs
539
+ SET
540
+ status = $1,
541
+ thread_count = $2,
542
+ pending_count = $3,
543
+ approved_count = $4,
544
+ rejected_count = $5,
545
+ error = $6,
546
+ token_usage = $7,
547
+ completed_at = $8,
548
+ committed_at = $9
549
+ WHERE id = $10
550
+ AND organization_id = $11
551
+ RETURNING
552
+ id,
553
+ organization_id,
554
+ document_id,
555
+ status,
556
+ reasoning_model_id,
557
+ reasoning_provider_configuration_id,
558
+ embedding_model_id,
559
+ embedding_provider_configuration_id,
560
+ embedding_dimensions,
561
+ thread_count,
562
+ pending_count,
563
+ approved_count,
564
+ rejected_count,
565
+ error,
566
+ token_usage,
567
+ started_at,
568
+ completed_at,
569
+ committed_at`;
570
+ async function updateIngestionRunState(client, args) {
571
+ const result = await client.query({
572
+ text: exports.updateIngestionRunStateQuery,
573
+ values: [args.status, args.threadCount, args.pendingCount, args.approvedCount, args.rejectedCount, args.error, args.tokenUsage, args.completedAt, args.committedAt, args.id, args.organizationId],
574
+ rowMode: "array"
575
+ });
576
+ if (result.rows.length !== 1) {
577
+ return null;
578
+ }
579
+ const row = result.rows[0];
580
+ return {
581
+ id: row[0],
582
+ organizationId: row[1],
583
+ documentId: row[2],
584
+ status: row[3],
585
+ reasoningModelId: row[4],
586
+ reasoningProviderConfigurationId: row[5],
587
+ embeddingModelId: row[6],
588
+ embeddingProviderConfigurationId: row[7],
589
+ embeddingDimensions: row[8],
590
+ threadCount: row[9],
591
+ pendingCount: row[10],
592
+ approvedCount: row[11],
593
+ rejectedCount: row[12],
594
+ error: row[13],
595
+ tokenUsage: row[14],
596
+ startedAt: row[15],
597
+ completedAt: row[16],
598
+ committedAt: row[17]
599
+ };
600
+ }
601
+ exports.listIngestionRunsByOrganizationQuery = `-- name: ListIngestionRunsByOrganization :many
602
+ SELECT
603
+ id,
604
+ organization_id,
605
+ document_id,
606
+ status,
607
+ reasoning_model_id,
608
+ reasoning_provider_configuration_id,
609
+ embedding_model_id,
610
+ embedding_provider_configuration_id,
611
+ embedding_dimensions,
612
+ thread_count,
613
+ pending_count,
614
+ approved_count,
615
+ rejected_count,
616
+ error,
617
+ token_usage,
618
+ started_at,
619
+ completed_at,
620
+ committed_at
621
+ FROM weave.ingestion_runs
622
+ WHERE organization_id = $1
623
+ ORDER BY started_at DESC
624
+ LIMIT $3 OFFSET $2`;
625
+ async function listIngestionRunsByOrganization(client, args) {
626
+ const result = await client.query({
627
+ text: exports.listIngestionRunsByOrganizationQuery,
628
+ values: [args.organizationId, args.pageOffset, args.pageSize],
629
+ rowMode: "array"
630
+ });
631
+ return result.rows.map(row => {
632
+ return {
633
+ id: row[0],
634
+ organizationId: row[1],
635
+ documentId: row[2],
636
+ status: row[3],
637
+ reasoningModelId: row[4],
638
+ reasoningProviderConfigurationId: row[5],
639
+ embeddingModelId: row[6],
640
+ embeddingProviderConfigurationId: row[7],
641
+ embeddingDimensions: row[8],
642
+ threadCount: row[9],
643
+ pendingCount: row[10],
644
+ approvedCount: row[11],
645
+ rejectedCount: row[12],
646
+ error: row[13],
647
+ tokenUsage: row[14],
648
+ startedAt: row[15],
649
+ completedAt: row[16],
650
+ committedAt: row[17]
651
+ };
652
+ });
653
+ }
654
+ exports.listIngestionRunsByDocumentQuery = `-- name: ListIngestionRunsByDocument :many
655
+ SELECT
656
+ id,
657
+ organization_id,
658
+ document_id,
659
+ status,
660
+ reasoning_model_id,
661
+ reasoning_provider_configuration_id,
662
+ embedding_model_id,
663
+ embedding_provider_configuration_id,
664
+ embedding_dimensions,
665
+ thread_count,
666
+ pending_count,
667
+ approved_count,
668
+ rejected_count,
669
+ error,
670
+ token_usage,
671
+ started_at,
672
+ completed_at,
673
+ committed_at
674
+ FROM weave.ingestion_runs
675
+ WHERE organization_id = $1
676
+ AND document_id = $2
677
+ ORDER BY started_at DESC`;
678
+ async function listIngestionRunsByDocument(client, args) {
679
+ const result = await client.query({
680
+ text: exports.listIngestionRunsByDocumentQuery,
681
+ values: [args.organizationId, args.documentId],
682
+ rowMode: "array"
683
+ });
684
+ return result.rows.map(row => {
685
+ return {
686
+ id: row[0],
687
+ organizationId: row[1],
688
+ documentId: row[2],
689
+ status: row[3],
690
+ reasoningModelId: row[4],
691
+ reasoningProviderConfigurationId: row[5],
692
+ embeddingModelId: row[6],
693
+ embeddingProviderConfigurationId: row[7],
694
+ embeddingDimensions: row[8],
695
+ threadCount: row[9],
696
+ pendingCount: row[10],
697
+ approvedCount: row[11],
698
+ rejectedCount: row[12],
699
+ error: row[13],
700
+ tokenUsage: row[14],
701
+ startedAt: row[15],
702
+ completedAt: row[16],
703
+ committedAt: row[17]
704
+ };
705
+ });
706
+ }
707
+ exports.listIngestionHistoryQuery = `-- name: ListIngestionHistory :many
708
+ SELECT
709
+ r.id AS ingestion_run_id,
710
+ d.id AS document_id,
711
+ d.filename AS document_name,
712
+ d.status AS document_status,
713
+ r.status AS ingestion_status,
714
+ r.thread_count,
715
+ r.pending_count,
716
+ r.approved_count,
717
+ r.rejected_count,
718
+ d.uploaded_at,
719
+ r.started_at,
720
+ r.completed_at,
721
+ r.committed_at
722
+ FROM weave.ingestion_runs r
723
+ JOIN weave.documents d ON d.id = r.document_id
724
+ WHERE r.organization_id = $1
725
+ ORDER BY r.started_at DESC
726
+ LIMIT $3 OFFSET $2`;
727
+ async function listIngestionHistory(client, args) {
728
+ const result = await client.query({
729
+ text: exports.listIngestionHistoryQuery,
730
+ values: [args.organizationId, args.pageOffset, args.pageSize],
731
+ rowMode: "array"
732
+ });
733
+ return result.rows.map(row => {
734
+ return {
735
+ ingestionRunId: row[0],
736
+ documentId: row[1],
737
+ documentName: row[2],
738
+ documentStatus: row[3],
739
+ ingestionStatus: row[4],
740
+ threadCount: row[5],
741
+ pendingCount: row[6],
742
+ approvedCount: row[7],
743
+ rejectedCount: row[8],
744
+ uploadedAt: row[9],
745
+ startedAt: row[10],
746
+ completedAt: row[11],
747
+ committedAt: row[12]
748
+ };
749
+ });
750
+ }
751
+ exports.deleteDocumentTagsByDocumentIDQuery = `-- name: DeleteDocumentTagsByDocumentID :exec
752
+ DELETE FROM weave.document_tags dt
753
+ USING weave.documents d
754
+ WHERE dt.document_id = d.id
755
+ AND d.organization_id = $1
756
+ AND dt.document_id = $2`;
757
+ async function deleteDocumentTagsByDocumentID(client, args) {
758
+ await client.query({
759
+ text: exports.deleteDocumentTagsByDocumentIDQuery,
760
+ values: [args.organizationId, args.documentId],
761
+ rowMode: "array"
762
+ });
763
+ }
764
+ exports.createDocumentTagQuery = `-- name: CreateDocumentTag :one
765
+ INSERT INTO weave.document_tags (
766
+ document_id,
767
+ tag,
768
+ confidence
769
+ ) VALUES (
770
+ $1,
771
+ $2,
772
+ $3
773
+ )
774
+ RETURNING document_id, tag, confidence`;
775
+ async function createDocumentTag(client, args) {
776
+ const result = await client.query({
777
+ text: exports.createDocumentTagQuery,
778
+ values: [args.documentId, args.tag, args.confidence],
779
+ rowMode: "array"
780
+ });
781
+ if (result.rows.length !== 1) {
782
+ return null;
783
+ }
784
+ const row = result.rows[0];
785
+ return {
786
+ documentId: row[0],
787
+ tag: row[1],
788
+ confidence: row[2]
789
+ };
790
+ }
791
+ exports.listDocumentTagsByDocumentIDQuery = `-- name: ListDocumentTagsByDocumentID :many
792
+ SELECT document_id, tag, confidence
793
+ FROM weave.document_tags dt
794
+ JOIN weave.documents d ON d.id = dt.document_id
795
+ WHERE d.organization_id = $1
796
+ AND dt.document_id = $2
797
+ ORDER BY confidence DESC, tag ASC`;
798
+ async function listDocumentTagsByDocumentID(client, args) {
799
+ const result = await client.query({
800
+ text: exports.listDocumentTagsByDocumentIDQuery,
801
+ values: [args.organizationId, args.documentId],
802
+ rowMode: "array"
803
+ });
804
+ return result.rows.map(row => {
805
+ return {
806
+ documentId: row[0],
807
+ tag: row[1],
808
+ confidence: row[2]
809
+ };
810
+ });
811
+ }
812
+ exports.createThreadQuery = `-- name: CreateThread :one
813
+ INSERT INTO weave.threads (
814
+ id,
815
+ organization_id,
816
+ document_id,
817
+ ingestion_run_id,
818
+ text,
819
+ knowledge_type,
820
+ facet_fields,
821
+ confidence,
822
+ temporal_validity,
823
+ classification,
824
+ retrieval_status,
825
+ source_location,
826
+ metadata
827
+ ) VALUES (
828
+ $1,
829
+ $2,
830
+ $3,
831
+ $4,
832
+ $5,
833
+ $6,
834
+ $7,
835
+ $8,
836
+ $9,
837
+ $10,
838
+ $11,
839
+ $12,
840
+ $13
841
+ )
842
+ RETURNING
843
+ id,
844
+ organization_id,
845
+ document_id,
846
+ ingestion_run_id,
847
+ text,
848
+ text_original,
849
+ knowledge_type,
850
+ facet_fields,
851
+ confidence,
852
+ temporal_validity,
853
+ classification,
854
+ retrieval_status,
855
+ source_location,
856
+ superseded_by,
857
+ supersedes,
858
+ cluster_id,
859
+ extracted_at,
860
+ approved_at,
861
+ approved_by_user_id,
862
+ edited,
863
+ metadata`;
864
+ async function createThread(client, args) {
865
+ const result = await client.query({
866
+ text: exports.createThreadQuery,
867
+ values: [args.id, args.organizationId, args.documentId, args.ingestionRunId, args.text, args.knowledgeType, args.facetFields, args.confidence, args.temporalValidity, args.classification, args.retrievalStatus, args.sourceLocation, args.metadata],
868
+ rowMode: "array"
869
+ });
870
+ if (result.rows.length !== 1) {
871
+ return null;
872
+ }
873
+ const row = result.rows[0];
874
+ return {
875
+ id: row[0],
876
+ organizationId: row[1],
877
+ documentId: row[2],
878
+ ingestionRunId: row[3],
879
+ text: row[4],
880
+ textOriginal: row[5],
881
+ knowledgeType: row[6],
882
+ facetFields: row[7],
883
+ confidence: row[8],
884
+ temporalValidity: row[9],
885
+ classification: row[10],
886
+ retrievalStatus: row[11],
887
+ sourceLocation: row[12],
888
+ supersededBy: row[13],
889
+ supersedes: row[14],
890
+ clusterId: row[15],
891
+ extractedAt: row[16],
892
+ approvedAt: row[17],
893
+ approvedByUserId: row[18],
894
+ edited: row[19],
895
+ metadata: row[20]
896
+ };
897
+ }
898
+ exports.getThreadByOrganizationAndIDQuery = `-- name: GetThreadByOrganizationAndID :one
899
+ SELECT
900
+ t.id,
901
+ t.organization_id,
902
+ t.document_id,
903
+ d.filename AS document_name,
904
+ t.ingestion_run_id,
905
+ t.text,
906
+ t.text_original,
907
+ t.knowledge_type,
908
+ t.facet_fields,
909
+ t.confidence,
910
+ t.temporal_validity,
911
+ t.classification,
912
+ t.retrieval_status,
913
+ t.source_location,
914
+ t.superseded_by,
915
+ t.supersedes,
916
+ t.cluster_id,
917
+ t.extracted_at,
918
+ t.approved_at,
919
+ t.approved_by_user_id,
920
+ t.edited,
921
+ t.metadata
922
+ FROM weave.threads t
923
+ JOIN weave.documents d ON d.id = t.document_id
924
+ WHERE t.organization_id = $1
925
+ AND t.id = $2`;
926
+ async function getThreadByOrganizationAndID(client, args) {
927
+ const result = await client.query({
928
+ text: exports.getThreadByOrganizationAndIDQuery,
929
+ values: [args.organizationId, args.id],
930
+ rowMode: "array"
931
+ });
932
+ if (result.rows.length !== 1) {
933
+ return null;
934
+ }
935
+ const row = result.rows[0];
936
+ return {
937
+ id: row[0],
938
+ organizationId: row[1],
939
+ documentId: row[2],
940
+ documentName: row[3],
941
+ ingestionRunId: row[4],
942
+ text: row[5],
943
+ textOriginal: row[6],
944
+ knowledgeType: row[7],
945
+ facetFields: row[8],
946
+ confidence: row[9],
947
+ temporalValidity: row[10],
948
+ classification: row[11],
949
+ retrievalStatus: row[12],
950
+ sourceLocation: row[13],
951
+ supersededBy: row[14],
952
+ supersedes: row[15],
953
+ clusterId: row[16],
954
+ extractedAt: row[17],
955
+ approvedAt: row[18],
956
+ approvedByUserId: row[19],
957
+ edited: row[20],
958
+ metadata: row[21]
959
+ };
960
+ }
961
+ exports.listThreadsByIngestionRunQuery = `-- name: ListThreadsByIngestionRun :many
962
+ SELECT
963
+ t.id,
964
+ t.organization_id,
965
+ t.document_id,
966
+ d.filename AS document_name,
967
+ t.ingestion_run_id,
968
+ t.text,
969
+ t.text_original,
970
+ t.knowledge_type,
971
+ t.facet_fields,
972
+ t.confidence,
973
+ t.temporal_validity,
974
+ t.classification,
975
+ t.retrieval_status,
976
+ t.source_location,
977
+ t.superseded_by,
978
+ t.supersedes,
979
+ t.cluster_id,
980
+ t.extracted_at,
981
+ t.approved_at,
982
+ t.approved_by_user_id,
983
+ t.edited,
984
+ t.metadata
985
+ FROM weave.threads t
986
+ JOIN weave.documents d ON d.id = t.document_id
987
+ WHERE t.organization_id = $1
988
+ AND t.ingestion_run_id = $2
989
+ ORDER BY t.extracted_at ASC, t.id ASC`;
990
+ async function listThreadsByIngestionRun(client, args) {
991
+ const result = await client.query({
992
+ text: exports.listThreadsByIngestionRunQuery,
993
+ values: [args.organizationId, args.ingestionRunId],
994
+ rowMode: "array"
995
+ });
996
+ return result.rows.map(row => {
997
+ return {
998
+ id: row[0],
999
+ organizationId: row[1],
1000
+ documentId: row[2],
1001
+ documentName: row[3],
1002
+ ingestionRunId: row[4],
1003
+ text: row[5],
1004
+ textOriginal: row[6],
1005
+ knowledgeType: row[7],
1006
+ facetFields: row[8],
1007
+ confidence: row[9],
1008
+ temporalValidity: row[10],
1009
+ classification: row[11],
1010
+ retrievalStatus: row[12],
1011
+ sourceLocation: row[13],
1012
+ supersededBy: row[14],
1013
+ supersedes: row[15],
1014
+ clusterId: row[16],
1015
+ extractedAt: row[17],
1016
+ approvedAt: row[18],
1017
+ approvedByUserId: row[19],
1018
+ edited: row[20],
1019
+ metadata: row[21]
1020
+ };
1021
+ });
1022
+ }
1023
+ exports.listPendingThreadsByRunQuery = `-- name: ListPendingThreadsByRun :many
1024
+ SELECT
1025
+ id,
1026
+ organization_id,
1027
+ document_id,
1028
+ ingestion_run_id,
1029
+ text,
1030
+ text_original,
1031
+ knowledge_type,
1032
+ facet_fields,
1033
+ confidence,
1034
+ temporal_validity,
1035
+ classification,
1036
+ retrieval_status,
1037
+ source_location,
1038
+ superseded_by,
1039
+ supersedes,
1040
+ cluster_id,
1041
+ extracted_at,
1042
+ approved_at,
1043
+ approved_by_user_id,
1044
+ edited,
1045
+ metadata
1046
+ FROM weave.threads
1047
+ WHERE organization_id = $1
1048
+ AND ingestion_run_id = $2
1049
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1050
+ ORDER BY extracted_at ASC, id ASC`;
1051
+ async function listPendingThreadsByRun(client, args) {
1052
+ const result = await client.query({
1053
+ text: exports.listPendingThreadsByRunQuery,
1054
+ values: [args.organizationId, args.ingestionRunId],
1055
+ rowMode: "array"
1056
+ });
1057
+ return result.rows.map(row => {
1058
+ return {
1059
+ id: row[0],
1060
+ organizationId: row[1],
1061
+ documentId: row[2],
1062
+ ingestionRunId: row[3],
1063
+ text: row[4],
1064
+ textOriginal: row[5],
1065
+ knowledgeType: row[6],
1066
+ facetFields: row[7],
1067
+ confidence: row[8],
1068
+ temporalValidity: row[9],
1069
+ classification: row[10],
1070
+ retrievalStatus: row[11],
1071
+ sourceLocation: row[12],
1072
+ supersededBy: row[13],
1073
+ supersedes: row[14],
1074
+ clusterId: row[15],
1075
+ extractedAt: row[16],
1076
+ approvedAt: row[17],
1077
+ approvedByUserId: row[18],
1078
+ edited: row[19],
1079
+ metadata: row[20]
1080
+ };
1081
+ });
1082
+ }
1083
+ exports.listPendingThreadsByOrganizationQuery = `-- name: ListPendingThreadsByOrganization :many
1084
+ SELECT
1085
+ t.id,
1086
+ t.organization_id,
1087
+ t.document_id,
1088
+ d.filename AS document_name,
1089
+ t.ingestion_run_id,
1090
+ t.text,
1091
+ t.text_original,
1092
+ t.knowledge_type,
1093
+ t.facet_fields,
1094
+ t.confidence,
1095
+ t.temporal_validity,
1096
+ t.classification,
1097
+ t.retrieval_status,
1098
+ t.source_location,
1099
+ t.superseded_by,
1100
+ t.supersedes,
1101
+ t.cluster_id,
1102
+ t.extracted_at,
1103
+ t.approved_at,
1104
+ t.approved_by_user_id,
1105
+ t.edited,
1106
+ t.metadata
1107
+ FROM weave.threads t
1108
+ JOIN weave.documents d ON d.id = t.document_id
1109
+ WHERE t.organization_id = $1
1110
+ AND t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1111
+ ORDER BY t.extracted_at DESC, t.id DESC
1112
+ LIMIT $3 OFFSET $2`;
1113
+ async function listPendingThreadsByOrganization(client, args) {
1114
+ const result = await client.query({
1115
+ text: exports.listPendingThreadsByOrganizationQuery,
1116
+ values: [args.organizationId, args.pageOffset, args.pageSize],
1117
+ rowMode: "array"
1118
+ });
1119
+ return result.rows.map(row => {
1120
+ return {
1121
+ id: row[0],
1122
+ organizationId: row[1],
1123
+ documentId: row[2],
1124
+ documentName: row[3],
1125
+ ingestionRunId: row[4],
1126
+ text: row[5],
1127
+ textOriginal: row[6],
1128
+ knowledgeType: row[7],
1129
+ facetFields: row[8],
1130
+ confidence: row[9],
1131
+ temporalValidity: row[10],
1132
+ classification: row[11],
1133
+ retrievalStatus: row[12],
1134
+ sourceLocation: row[13],
1135
+ supersededBy: row[14],
1136
+ supersedes: row[15],
1137
+ clusterId: row[16],
1138
+ extractedAt: row[17],
1139
+ approvedAt: row[18],
1140
+ approvedByUserId: row[19],
1141
+ edited: row[20],
1142
+ metadata: row[21]
1143
+ };
1144
+ });
1145
+ }
1146
+ exports.updateThreadEmbeddingQuery = `-- name: UpdateThreadEmbedding :one
1147
+ UPDATE weave.threads
1148
+ SET
1149
+ embedding = $1::vector
1150
+ WHERE organization_id = $2
1151
+ AND id = $3
1152
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1153
+ RETURNING
1154
+ id,
1155
+ organization_id,
1156
+ document_id,
1157
+ ingestion_run_id,
1158
+ text,
1159
+ text_original,
1160
+ knowledge_type,
1161
+ facet_fields,
1162
+ confidence,
1163
+ temporal_validity,
1164
+ classification,
1165
+ retrieval_status,
1166
+ source_location,
1167
+ superseded_by,
1168
+ supersedes,
1169
+ cluster_id,
1170
+ extracted_at,
1171
+ approved_at,
1172
+ approved_by_user_id,
1173
+ edited,
1174
+ metadata`;
1175
+ async function updateThreadEmbedding(client, args) {
1176
+ const result = await client.query({
1177
+ text: exports.updateThreadEmbeddingQuery,
1178
+ values: [args.embedding, args.organizationId, args.id],
1179
+ rowMode: "array"
1180
+ });
1181
+ if (result.rows.length !== 1) {
1182
+ return null;
1183
+ }
1184
+ const row = result.rows[0];
1185
+ return {
1186
+ id: row[0],
1187
+ organizationId: row[1],
1188
+ documentId: row[2],
1189
+ ingestionRunId: row[3],
1190
+ text: row[4],
1191
+ textOriginal: row[5],
1192
+ knowledgeType: row[6],
1193
+ facetFields: row[7],
1194
+ confidence: row[8],
1195
+ temporalValidity: row[9],
1196
+ classification: row[10],
1197
+ retrievalStatus: row[11],
1198
+ sourceLocation: row[12],
1199
+ supersededBy: row[13],
1200
+ supersedes: row[14],
1201
+ clusterId: row[15],
1202
+ extractedAt: row[16],
1203
+ approvedAt: row[17],
1204
+ approvedByUserId: row[18],
1205
+ edited: row[19],
1206
+ metadata: row[20]
1207
+ };
1208
+ }
1209
+ exports.setThreadNextEmbeddingQuery = `-- name: SetThreadNextEmbedding :one
1210
+ UPDATE weave.threads
1211
+ SET
1212
+ embedding_next = $1::vector
1213
+ WHERE organization_id = $2
1214
+ AND id = $3
1215
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1216
+ RETURNING
1217
+ id,
1218
+ organization_id,
1219
+ document_id,
1220
+ ingestion_run_id,
1221
+ text,
1222
+ text_original,
1223
+ knowledge_type,
1224
+ facet_fields,
1225
+ confidence,
1226
+ temporal_validity,
1227
+ classification,
1228
+ retrieval_status,
1229
+ source_location,
1230
+ superseded_by,
1231
+ supersedes,
1232
+ cluster_id,
1233
+ extracted_at,
1234
+ approved_at,
1235
+ approved_by_user_id,
1236
+ edited,
1237
+ metadata`;
1238
+ async function setThreadNextEmbedding(client, args) {
1239
+ const result = await client.query({
1240
+ text: exports.setThreadNextEmbeddingQuery,
1241
+ values: [args.embedding, args.organizationId, args.id],
1242
+ rowMode: "array"
1243
+ });
1244
+ if (result.rows.length !== 1) {
1245
+ return null;
1246
+ }
1247
+ const row = result.rows[0];
1248
+ return {
1249
+ id: row[0],
1250
+ organizationId: row[1],
1251
+ documentId: row[2],
1252
+ ingestionRunId: row[3],
1253
+ text: row[4],
1254
+ textOriginal: row[5],
1255
+ knowledgeType: row[6],
1256
+ facetFields: row[7],
1257
+ confidence: row[8],
1258
+ temporalValidity: row[9],
1259
+ classification: row[10],
1260
+ retrievalStatus: row[11],
1261
+ sourceLocation: row[12],
1262
+ supersededBy: row[13],
1263
+ supersedes: row[14],
1264
+ clusterId: row[15],
1265
+ extractedAt: row[16],
1266
+ approvedAt: row[17],
1267
+ approvedByUserId: row[18],
1268
+ edited: row[19],
1269
+ metadata: row[20]
1270
+ };
1271
+ }
1272
+ exports.promoteThreadNextEmbeddingsByOrganizationQuery = `-- name: PromoteThreadNextEmbeddingsByOrganization :execrows
1273
+ UPDATE weave.threads
1274
+ SET
1275
+ embedding = embedding_next,
1276
+ embedding_next = NULL
1277
+ WHERE organization_id = $1
1278
+ AND embedding_next IS NOT NULL`;
1279
+ exports.updateThreadClassificationQuery = `-- name: UpdateThreadClassification :one
1280
+ UPDATE weave.threads
1281
+ SET
1282
+ classification = $1,
1283
+ supersedes = $2
1284
+ WHERE organization_id = $3
1285
+ AND id = $4
1286
+ RETURNING
1287
+ id,
1288
+ organization_id,
1289
+ document_id,
1290
+ ingestion_run_id,
1291
+ text,
1292
+ text_original,
1293
+ knowledge_type,
1294
+ facet_fields,
1295
+ confidence,
1296
+ temporal_validity,
1297
+ classification,
1298
+ retrieval_status,
1299
+ source_location,
1300
+ superseded_by,
1301
+ supersedes,
1302
+ cluster_id,
1303
+ extracted_at,
1304
+ approved_at,
1305
+ approved_by_user_id,
1306
+ edited,
1307
+ metadata`;
1308
+ async function updateThreadClassification(client, args) {
1309
+ const result = await client.query({
1310
+ text: exports.updateThreadClassificationQuery,
1311
+ values: [args.classification, args.supersedes, args.organizationId, args.id],
1312
+ rowMode: "array"
1313
+ });
1314
+ if (result.rows.length !== 1) {
1315
+ return null;
1316
+ }
1317
+ const row = result.rows[0];
1318
+ return {
1319
+ id: row[0],
1320
+ organizationId: row[1],
1321
+ documentId: row[2],
1322
+ ingestionRunId: row[3],
1323
+ text: row[4],
1324
+ textOriginal: row[5],
1325
+ knowledgeType: row[6],
1326
+ facetFields: row[7],
1327
+ confidence: row[8],
1328
+ temporalValidity: row[9],
1329
+ classification: row[10],
1330
+ retrievalStatus: row[11],
1331
+ sourceLocation: row[12],
1332
+ supersededBy: row[13],
1333
+ supersedes: row[14],
1334
+ clusterId: row[15],
1335
+ extractedAt: row[16],
1336
+ approvedAt: row[17],
1337
+ approvedByUserId: row[18],
1338
+ edited: row[19],
1339
+ metadata: row[20]
1340
+ };
1341
+ }
1342
+ exports.editPendingThreadQuery = `-- name: EditPendingThread :one
1343
+ UPDATE weave.threads
1344
+ SET
1345
+ text_original = CASE
1346
+ WHEN text_original IS NULL OR text_original = '' THEN text
1347
+ ELSE text_original
1348
+ END,
1349
+ text = $1,
1350
+ edited = TRUE
1351
+ WHERE id = $2
1352
+ AND organization_id = $3
1353
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1354
+ RETURNING
1355
+ id,
1356
+ organization_id,
1357
+ document_id,
1358
+ ingestion_run_id,
1359
+ text,
1360
+ text_original,
1361
+ knowledge_type,
1362
+ facet_fields,
1363
+ confidence,
1364
+ temporal_validity,
1365
+ classification,
1366
+ retrieval_status,
1367
+ source_location,
1368
+ superseded_by,
1369
+ supersedes,
1370
+ cluster_id,
1371
+ extracted_at,
1372
+ approved_at,
1373
+ approved_by_user_id,
1374
+ edited,
1375
+ metadata`;
1376
+ async function editPendingThread(client, args) {
1377
+ const result = await client.query({
1378
+ text: exports.editPendingThreadQuery,
1379
+ values: [args.text, args.id, args.organizationId],
1380
+ rowMode: "array"
1381
+ });
1382
+ if (result.rows.length !== 1) {
1383
+ return null;
1384
+ }
1385
+ const row = result.rows[0];
1386
+ return {
1387
+ id: row[0],
1388
+ organizationId: row[1],
1389
+ documentId: row[2],
1390
+ ingestionRunId: row[3],
1391
+ text: row[4],
1392
+ textOriginal: row[5],
1393
+ knowledgeType: row[6],
1394
+ facetFields: row[7],
1395
+ confidence: row[8],
1396
+ temporalValidity: row[9],
1397
+ classification: row[10],
1398
+ retrievalStatus: row[11],
1399
+ sourceLocation: row[12],
1400
+ supersededBy: row[13],
1401
+ supersedes: row[14],
1402
+ clusterId: row[15],
1403
+ extractedAt: row[16],
1404
+ approvedAt: row[17],
1405
+ approvedByUserId: row[18],
1406
+ edited: row[19],
1407
+ metadata: row[20]
1408
+ };
1409
+ }
1410
+ exports.approveThreadQuery = `-- name: ApproveThread :one
1411
+ UPDATE weave.threads
1412
+ SET
1413
+ retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED',
1414
+ approved_at = now(),
1415
+ approved_by_user_id = $1,
1416
+ embedding = COALESCE($2::vector, embedding),
1417
+ embedding_next = NULL
1418
+ WHERE id = $3
1419
+ AND organization_id = $4
1420
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1421
+ RETURNING
1422
+ id,
1423
+ organization_id,
1424
+ document_id,
1425
+ ingestion_run_id,
1426
+ text,
1427
+ text_original,
1428
+ knowledge_type,
1429
+ facet_fields,
1430
+ confidence,
1431
+ temporal_validity,
1432
+ classification,
1433
+ retrieval_status,
1434
+ source_location,
1435
+ superseded_by,
1436
+ supersedes,
1437
+ cluster_id,
1438
+ extracted_at,
1439
+ approved_at,
1440
+ approved_by_user_id,
1441
+ edited,
1442
+ metadata`;
1443
+ async function approveThread(client, args) {
1444
+ const result = await client.query({
1445
+ text: exports.approveThreadQuery,
1446
+ values: [args.approvedByUserId, args.embedding, args.id, args.organizationId],
1447
+ rowMode: "array"
1448
+ });
1449
+ if (result.rows.length !== 1) {
1450
+ return null;
1451
+ }
1452
+ const row = result.rows[0];
1453
+ return {
1454
+ id: row[0],
1455
+ organizationId: row[1],
1456
+ documentId: row[2],
1457
+ ingestionRunId: row[3],
1458
+ text: row[4],
1459
+ textOriginal: row[5],
1460
+ knowledgeType: row[6],
1461
+ facetFields: row[7],
1462
+ confidence: row[8],
1463
+ temporalValidity: row[9],
1464
+ classification: row[10],
1465
+ retrievalStatus: row[11],
1466
+ sourceLocation: row[12],
1467
+ supersededBy: row[13],
1468
+ supersedes: row[14],
1469
+ clusterId: row[15],
1470
+ extractedAt: row[16],
1471
+ approvedAt: row[17],
1472
+ approvedByUserId: row[18],
1473
+ edited: row[19],
1474
+ metadata: row[20]
1475
+ };
1476
+ }
1477
+ exports.bulkApproveThreadsQuery = `-- name: BulkApproveThreads :many
1478
+ UPDATE weave.threads
1479
+ SET
1480
+ retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED',
1481
+ approved_at = now(),
1482
+ approved_by_user_id = $1,
1483
+ embedding_next = NULL
1484
+ WHERE organization_id = $2
1485
+ AND ingestion_run_id = $3
1486
+ AND id = ANY($4::uuid[])
1487
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1488
+ RETURNING
1489
+ id,
1490
+ organization_id,
1491
+ document_id,
1492
+ ingestion_run_id,
1493
+ text,
1494
+ text_original,
1495
+ knowledge_type,
1496
+ facet_fields,
1497
+ confidence,
1498
+ temporal_validity,
1499
+ classification,
1500
+ retrieval_status,
1501
+ source_location,
1502
+ superseded_by,
1503
+ supersedes,
1504
+ cluster_id,
1505
+ extracted_at,
1506
+ approved_at,
1507
+ approved_by_user_id,
1508
+ edited,
1509
+ metadata`;
1510
+ async function bulkApproveThreads(client, args) {
1511
+ const result = await client.query({
1512
+ text: exports.bulkApproveThreadsQuery,
1513
+ values: [args.approvedByUserId, args.organizationId, args.ingestionRunId, args.threadIds],
1514
+ rowMode: "array"
1515
+ });
1516
+ return result.rows.map(row => {
1517
+ return {
1518
+ id: row[0],
1519
+ organizationId: row[1],
1520
+ documentId: row[2],
1521
+ ingestionRunId: row[3],
1522
+ text: row[4],
1523
+ textOriginal: row[5],
1524
+ knowledgeType: row[6],
1525
+ facetFields: row[7],
1526
+ confidence: row[8],
1527
+ temporalValidity: row[9],
1528
+ classification: row[10],
1529
+ retrievalStatus: row[11],
1530
+ sourceLocation: row[12],
1531
+ supersededBy: row[13],
1532
+ supersedes: row[14],
1533
+ clusterId: row[15],
1534
+ extractedAt: row[16],
1535
+ approvedAt: row[17],
1536
+ approvedByUserId: row[18],
1537
+ edited: row[19],
1538
+ metadata: row[20]
1539
+ };
1540
+ });
1541
+ }
1542
+ exports.rejectThreadQuery = `-- name: RejectThread :one
1543
+ UPDATE weave.threads
1544
+ SET
1545
+ retrieval_status = 'THREAD_RETRIEVAL_STATUS_REJECTED'
1546
+ WHERE organization_id = $1
1547
+ AND id = $2
1548
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1549
+ RETURNING
1550
+ id,
1551
+ organization_id,
1552
+ document_id,
1553
+ ingestion_run_id,
1554
+ text,
1555
+ text_original,
1556
+ knowledge_type,
1557
+ facet_fields,
1558
+ confidence,
1559
+ temporal_validity,
1560
+ classification,
1561
+ retrieval_status,
1562
+ source_location,
1563
+ superseded_by,
1564
+ supersedes,
1565
+ cluster_id,
1566
+ extracted_at,
1567
+ approved_at,
1568
+ approved_by_user_id,
1569
+ edited,
1570
+ metadata`;
1571
+ async function rejectThread(client, args) {
1572
+ const result = await client.query({
1573
+ text: exports.rejectThreadQuery,
1574
+ values: [args.organizationId, args.id],
1575
+ rowMode: "array"
1576
+ });
1577
+ if (result.rows.length !== 1) {
1578
+ return null;
1579
+ }
1580
+ const row = result.rows[0];
1581
+ return {
1582
+ id: row[0],
1583
+ organizationId: row[1],
1584
+ documentId: row[2],
1585
+ ingestionRunId: row[3],
1586
+ text: row[4],
1587
+ textOriginal: row[5],
1588
+ knowledgeType: row[6],
1589
+ facetFields: row[7],
1590
+ confidence: row[8],
1591
+ temporalValidity: row[9],
1592
+ classification: row[10],
1593
+ retrievalStatus: row[11],
1594
+ sourceLocation: row[12],
1595
+ supersededBy: row[13],
1596
+ supersedes: row[14],
1597
+ clusterId: row[15],
1598
+ extractedAt: row[16],
1599
+ approvedAt: row[17],
1600
+ approvedByUserId: row[18],
1601
+ edited: row[19],
1602
+ metadata: row[20]
1603
+ };
1604
+ }
1605
+ exports.bulkRejectThreadsQuery = `-- name: BulkRejectThreads :many
1606
+ UPDATE weave.threads
1607
+ SET
1608
+ retrieval_status = 'THREAD_RETRIEVAL_STATUS_REJECTED'
1609
+ WHERE organization_id = $1
1610
+ AND ingestion_run_id = $2
1611
+ AND id = ANY($3::uuid[])
1612
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1613
+ RETURNING
1614
+ id,
1615
+ organization_id,
1616
+ document_id,
1617
+ ingestion_run_id,
1618
+ text,
1619
+ text_original,
1620
+ knowledge_type,
1621
+ facet_fields,
1622
+ confidence,
1623
+ temporal_validity,
1624
+ classification,
1625
+ retrieval_status,
1626
+ source_location,
1627
+ superseded_by,
1628
+ supersedes,
1629
+ cluster_id,
1630
+ extracted_at,
1631
+ approved_at,
1632
+ approved_by_user_id,
1633
+ edited,
1634
+ metadata`;
1635
+ async function bulkRejectThreads(client, args) {
1636
+ const result = await client.query({
1637
+ text: exports.bulkRejectThreadsQuery,
1638
+ values: [args.organizationId, args.ingestionRunId, args.threadIds],
1639
+ rowMode: "array"
1640
+ });
1641
+ return result.rows.map(row => {
1642
+ return {
1643
+ id: row[0],
1644
+ organizationId: row[1],
1645
+ documentId: row[2],
1646
+ ingestionRunId: row[3],
1647
+ text: row[4],
1648
+ textOriginal: row[5],
1649
+ knowledgeType: row[6],
1650
+ facetFields: row[7],
1651
+ confidence: row[8],
1652
+ temporalValidity: row[9],
1653
+ classification: row[10],
1654
+ retrievalStatus: row[11],
1655
+ sourceLocation: row[12],
1656
+ supersededBy: row[13],
1657
+ supersedes: row[14],
1658
+ clusterId: row[15],
1659
+ extractedAt: row[16],
1660
+ approvedAt: row[17],
1661
+ approvedByUserId: row[18],
1662
+ edited: row[19],
1663
+ metadata: row[20]
1664
+ };
1665
+ });
1666
+ }
1667
+ exports.markThreadsSupersededQuery = `-- name: MarkThreadsSuperseded :execrows
1668
+ UPDATE weave.threads
1669
+ SET
1670
+ retrieval_status = 'THREAD_RETRIEVAL_STATUS_SUPERSEDED',
1671
+ superseded_by = $1
1672
+ WHERE organization_id = $2
1673
+ AND id = ANY($3::uuid[])`;
1674
+ exports.listThreadsBySupersedesQuery = `-- name: ListThreadsBySupersedes :many
1675
+ SELECT
1676
+ id,
1677
+ organization_id,
1678
+ document_id,
1679
+ ingestion_run_id,
1680
+ text,
1681
+ text_original,
1682
+ knowledge_type,
1683
+ facet_fields,
1684
+ confidence,
1685
+ temporal_validity,
1686
+ classification,
1687
+ retrieval_status,
1688
+ source_location,
1689
+ superseded_by,
1690
+ supersedes,
1691
+ cluster_id,
1692
+ extracted_at,
1693
+ approved_at,
1694
+ approved_by_user_id,
1695
+ edited,
1696
+ metadata
1697
+ FROM weave.threads
1698
+ WHERE organization_id = $1
1699
+ AND $2 = ANY(supersedes)
1700
+ ORDER BY approved_at DESC NULLS LAST, extracted_at DESC, id DESC`;
1701
+ async function listThreadsBySupersedes(client, args) {
1702
+ const result = await client.query({
1703
+ text: exports.listThreadsBySupersedesQuery,
1704
+ values: [args.organizationId, args.threadId],
1705
+ rowMode: "array"
1706
+ });
1707
+ return result.rows.map(row => {
1708
+ return {
1709
+ id: row[0],
1710
+ organizationId: row[1],
1711
+ documentId: row[2],
1712
+ ingestionRunId: row[3],
1713
+ text: row[4],
1714
+ textOriginal: row[5],
1715
+ knowledgeType: row[6],
1716
+ facetFields: row[7],
1717
+ confidence: row[8],
1718
+ temporalValidity: row[9],
1719
+ classification: row[10],
1720
+ retrievalStatus: row[11],
1721
+ sourceLocation: row[12],
1722
+ supersededBy: row[13],
1723
+ supersedes: row[14],
1724
+ clusterId: row[15],
1725
+ extractedAt: row[16],
1726
+ approvedAt: row[17],
1727
+ approvedByUserId: row[18],
1728
+ edited: row[19],
1729
+ metadata: row[20]
1730
+ };
1731
+ });
1732
+ }
1733
+ exports.listThreadsByIDsQuery = `-- name: ListThreadsByIDs :many
1734
+ SELECT
1735
+ id,
1736
+ organization_id,
1737
+ document_id,
1738
+ ingestion_run_id,
1739
+ text,
1740
+ text_original,
1741
+ knowledge_type,
1742
+ facet_fields,
1743
+ confidence,
1744
+ temporal_validity,
1745
+ classification,
1746
+ retrieval_status,
1747
+ source_location,
1748
+ superseded_by,
1749
+ supersedes,
1750
+ cluster_id,
1751
+ extracted_at,
1752
+ approved_at,
1753
+ approved_by_user_id,
1754
+ edited,
1755
+ metadata
1756
+ FROM weave.threads
1757
+ WHERE organization_id = $1
1758
+ AND id = ANY($2::uuid[])
1759
+ ORDER BY extracted_at ASC, id ASC`;
1760
+ async function listThreadsByIDs(client, args) {
1761
+ const result = await client.query({
1762
+ text: exports.listThreadsByIDsQuery,
1763
+ values: [args.organizationId, args.threadIds],
1764
+ rowMode: "array"
1765
+ });
1766
+ return result.rows.map(row => {
1767
+ return {
1768
+ id: row[0],
1769
+ organizationId: row[1],
1770
+ documentId: row[2],
1771
+ ingestionRunId: row[3],
1772
+ text: row[4],
1773
+ textOriginal: row[5],
1774
+ knowledgeType: row[6],
1775
+ facetFields: row[7],
1776
+ confidence: row[8],
1777
+ temporalValidity: row[9],
1778
+ classification: row[10],
1779
+ retrievalStatus: row[11],
1780
+ sourceLocation: row[12],
1781
+ supersededBy: row[13],
1782
+ supersedes: row[14],
1783
+ clusterId: row[15],
1784
+ extractedAt: row[16],
1785
+ approvedAt: row[17],
1786
+ approvedByUserId: row[18],
1787
+ edited: row[19],
1788
+ metadata: row[20]
1789
+ };
1790
+ });
1791
+ }
1792
+ exports.getIngestionRunThreadCountsQuery = `-- name: GetIngestionRunThreadCounts :one
1793
+ SELECT
1794
+ COUNT(*)::int AS thread_count,
1795
+ COUNT(*) FILTER (
1796
+ WHERE retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1797
+ )::int AS pending_count,
1798
+ COUNT(*) FILTER (
1799
+ WHERE retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
1800
+ )::int AS approved_count,
1801
+ COUNT(*) FILTER (
1802
+ WHERE retrieval_status = 'THREAD_RETRIEVAL_STATUS_REJECTED'
1803
+ )::int AS rejected_count
1804
+ FROM weave.threads
1805
+ WHERE organization_id = $1
1806
+ AND ingestion_run_id = $2`;
1807
+ async function getIngestionRunThreadCounts(client, args) {
1808
+ const result = await client.query({
1809
+ text: exports.getIngestionRunThreadCountsQuery,
1810
+ values: [args.organizationId, args.ingestionRunId],
1811
+ rowMode: "array"
1812
+ });
1813
+ if (result.rows.length !== 1) {
1814
+ return null;
1815
+ }
1816
+ const row = result.rows[0];
1817
+ return {
1818
+ threadCount: row[0],
1819
+ pendingCount: row[1],
1820
+ approvedCount: row[2],
1821
+ rejectedCount: row[3]
1822
+ };
1823
+ }
1824
+ exports.deleteThreadReconciliationByPendingThreadQuery = `-- name: DeleteThreadReconciliationByPendingThread :exec
1825
+ DELETE FROM weave.thread_reconciliation tr
1826
+ USING weave.threads t
1827
+ WHERE tr.pending_thread_id = t.id
1828
+ AND t.organization_id = $1
1829
+ AND tr.pending_thread_id = $2`;
1830
+ async function deleteThreadReconciliationByPendingThread(client, args) {
1831
+ await client.query({
1832
+ text: exports.deleteThreadReconciliationByPendingThreadQuery,
1833
+ values: [args.organizationId, args.pendingThreadId],
1834
+ rowMode: "array"
1835
+ });
1836
+ }
1837
+ exports.upsertThreadReconciliationQuery = `-- name: UpsertThreadReconciliation :one
1838
+ INSERT INTO weave.thread_reconciliation (
1839
+ pending_thread_id,
1840
+ existing_thread_id,
1841
+ similarity,
1842
+ entity_overlap
1843
+ ) VALUES (
1844
+ $1,
1845
+ $2,
1846
+ $3,
1847
+ $4
1848
+ )
1849
+ ON CONFLICT (pending_thread_id, existing_thread_id) DO UPDATE
1850
+ SET
1851
+ similarity = EXCLUDED.similarity,
1852
+ entity_overlap = EXCLUDED.entity_overlap
1853
+ RETURNING
1854
+ pending_thread_id,
1855
+ existing_thread_id,
1856
+ similarity,
1857
+ entity_overlap`;
1858
+ async function upsertThreadReconciliation(client, args) {
1859
+ const result = await client.query({
1860
+ text: exports.upsertThreadReconciliationQuery,
1861
+ values: [args.pendingThreadId, args.existingThreadId, args.similarity, args.entityOverlap],
1862
+ rowMode: "array"
1863
+ });
1864
+ if (result.rows.length !== 1) {
1865
+ return null;
1866
+ }
1867
+ const row = result.rows[0];
1868
+ return {
1869
+ pendingThreadId: row[0],
1870
+ existingThreadId: row[1],
1871
+ similarity: row[2],
1872
+ entityOverlap: row[3]
1873
+ };
1874
+ }
1875
+ exports.listThreadReconciliationByPendingThreadQuery = `-- name: ListThreadReconciliationByPendingThread :many
1876
+ SELECT
1877
+ pending_thread_id,
1878
+ existing_thread_id,
1879
+ similarity,
1880
+ entity_overlap
1881
+ FROM weave.thread_reconciliation
1882
+ WHERE pending_thread_id IN (
1883
+ SELECT id
1884
+ FROM weave.threads
1885
+ WHERE organization_id = $1
1886
+ AND id = $2
1887
+ )
1888
+ ORDER BY similarity DESC, entity_overlap DESC, existing_thread_id ASC`;
1889
+ async function listThreadReconciliationByPendingThread(client, args) {
1890
+ const result = await client.query({
1891
+ text: exports.listThreadReconciliationByPendingThreadQuery,
1892
+ values: [args.organizationId, args.pendingThreadId],
1893
+ rowMode: "array"
1894
+ });
1895
+ return result.rows.map(row => {
1896
+ return {
1897
+ pendingThreadId: row[0],
1898
+ existingThreadId: row[1],
1899
+ similarity: row[2],
1900
+ entityOverlap: row[3]
1901
+ };
1902
+ });
1903
+ }
1904
+ exports.createThreadActivityQuery = `-- name: CreateThreadActivity :one
1905
+ INSERT INTO weave.thread_activity (
1906
+ id,
1907
+ thread_id,
1908
+ action,
1909
+ actor_id,
1910
+ metadata
1911
+ ) VALUES (
1912
+ $1,
1913
+ $2,
1914
+ $3,
1915
+ $4,
1916
+ $5
1917
+ )
1918
+ RETURNING
1919
+ id,
1920
+ thread_id,
1921
+ action,
1922
+ actor_id,
1923
+ metadata,
1924
+ created_at`;
1925
+ async function createThreadActivity(client, args) {
1926
+ const result = await client.query({
1927
+ text: exports.createThreadActivityQuery,
1928
+ values: [args.id, args.threadId, args.action, args.actorId, args.metadata],
1929
+ rowMode: "array"
1930
+ });
1931
+ if (result.rows.length !== 1) {
1932
+ return null;
1933
+ }
1934
+ const row = result.rows[0];
1935
+ return {
1936
+ id: row[0],
1937
+ threadId: row[1],
1938
+ action: row[2],
1939
+ actorId: row[3],
1940
+ metadata: row[4],
1941
+ createdAt: row[5]
1942
+ };
1943
+ }
1944
+ exports.listThreadActivityByThreadQuery = `-- name: ListThreadActivityByThread :many
1945
+ SELECT
1946
+ id,
1947
+ thread_id,
1948
+ action,
1949
+ actor_id,
1950
+ metadata,
1951
+ created_at
1952
+ FROM weave.thread_activity
1953
+ WHERE thread_id IN (
1954
+ SELECT t.id
1955
+ FROM weave.threads t
1956
+ WHERE t.organization_id = $1
1957
+ AND t.id = $2
1958
+ )
1959
+ ORDER BY created_at DESC`;
1960
+ async function listThreadActivityByThread(client, args) {
1961
+ const result = await client.query({
1962
+ text: exports.listThreadActivityByThreadQuery,
1963
+ values: [args.organizationId, args.threadId],
1964
+ rowMode: "array"
1965
+ });
1966
+ return result.rows.map(row => {
1967
+ return {
1968
+ id: row[0],
1969
+ threadId: row[1],
1970
+ action: row[2],
1971
+ actorId: row[3],
1972
+ metadata: row[4],
1973
+ createdAt: row[5]
1974
+ };
1975
+ });
1976
+ }
1977
+ exports.upsertEntityQuery = `-- name: UpsertEntity :one
1978
+ INSERT INTO weave.entities (
1979
+ id,
1980
+ organization_id,
1981
+ name,
1982
+ type,
1983
+ canonical,
1984
+ metadata
1985
+ ) VALUES (
1986
+ $1,
1987
+ $2,
1988
+ $3,
1989
+ $4,
1990
+ $5,
1991
+ $6
1992
+ )
1993
+ ON CONFLICT (organization_id, type, canonical) DO UPDATE
1994
+ SET
1995
+ name = EXCLUDED.name,
1996
+ metadata = EXCLUDED.metadata,
1997
+ updated_at = now()
1998
+ RETURNING
1999
+ id,
2000
+ organization_id,
2001
+ name,
2002
+ type,
2003
+ canonical,
2004
+ metadata,
2005
+ created_at,
2006
+ updated_at`;
2007
+ async function upsertEntity(client, args) {
2008
+ const result = await client.query({
2009
+ text: exports.upsertEntityQuery,
2010
+ values: [args.id, args.organizationId, args.name, args.type, args.canonical, args.metadata],
2011
+ rowMode: "array"
2012
+ });
2013
+ if (result.rows.length !== 1) {
2014
+ return null;
2015
+ }
2016
+ const row = result.rows[0];
2017
+ return {
2018
+ id: row[0],
2019
+ organizationId: row[1],
2020
+ name: row[2],
2021
+ type: row[3],
2022
+ canonical: row[4],
2023
+ metadata: row[5],
2024
+ createdAt: row[6],
2025
+ updatedAt: row[7]
2026
+ };
2027
+ }
2028
+ exports.upsertThreadEntityQuery = `-- name: UpsertThreadEntity :one
2029
+ INSERT INTO weave.thread_entities (
2030
+ thread_id,
2031
+ entity_id,
2032
+ role,
2033
+ confidence
2034
+ ) VALUES (
2035
+ $1,
2036
+ $2,
2037
+ $3,
2038
+ $4
2039
+ )
2040
+ ON CONFLICT (thread_id, entity_id) DO UPDATE
2041
+ SET
2042
+ role = EXCLUDED.role,
2043
+ confidence = EXCLUDED.confidence
2044
+ RETURNING
2045
+ thread_id,
2046
+ entity_id,
2047
+ role,
2048
+ confidence`;
2049
+ async function upsertThreadEntity(client, args) {
2050
+ const result = await client.query({
2051
+ text: exports.upsertThreadEntityQuery,
2052
+ values: [args.threadId, args.entityId, args.role, args.confidence],
2053
+ rowMode: "array"
2054
+ });
2055
+ if (result.rows.length !== 1) {
2056
+ return null;
2057
+ }
2058
+ const row = result.rows[0];
2059
+ return {
2060
+ threadId: row[0],
2061
+ entityId: row[1],
2062
+ role: row[2],
2063
+ confidence: row[3]
2064
+ };
2065
+ }
2066
+ exports.upsertEntityRelationshipQuery = `-- name: UpsertEntityRelationship :one
2067
+ INSERT INTO weave.entity_relationships (
2068
+ id,
2069
+ organization_id,
2070
+ source_entity_id,
2071
+ target_entity_id,
2072
+ relationship,
2073
+ source_thread_id,
2074
+ confidence
2075
+ ) VALUES (
2076
+ $1,
2077
+ $2,
2078
+ $3,
2079
+ $4,
2080
+ $5,
2081
+ $6,
2082
+ $7
2083
+ )
2084
+ ON CONFLICT (source_entity_id, target_entity_id, relationship, source_thread_id) DO UPDATE
2085
+ SET
2086
+ confidence = EXCLUDED.confidence
2087
+ RETURNING
2088
+ id,
2089
+ organization_id,
2090
+ source_entity_id,
2091
+ target_entity_id,
2092
+ relationship,
2093
+ source_thread_id,
2094
+ confidence,
2095
+ created_at`;
2096
+ async function upsertEntityRelationship(client, args) {
2097
+ const result = await client.query({
2098
+ text: exports.upsertEntityRelationshipQuery,
2099
+ values: [args.id, args.organizationId, args.sourceEntityId, args.targetEntityId, args.relationship, args.sourceThreadId, args.confidence],
2100
+ rowMode: "array"
2101
+ });
2102
+ if (result.rows.length !== 1) {
2103
+ return null;
2104
+ }
2105
+ const row = result.rows[0];
2106
+ return {
2107
+ id: row[0],
2108
+ organizationId: row[1],
2109
+ sourceEntityId: row[2],
2110
+ targetEntityId: row[3],
2111
+ relationship: row[4],
2112
+ sourceThreadId: row[5],
2113
+ confidence: row[6],
2114
+ createdAt: row[7]
2115
+ };
2116
+ }
2117
+ exports.listEntitiesByThreadQuery = `-- name: ListEntitiesByThread :many
2118
+ SELECT
2119
+ e.id,
2120
+ e.organization_id,
2121
+ e.name,
2122
+ e.type,
2123
+ e.canonical,
2124
+ e.metadata,
2125
+ e.created_at,
2126
+ e.updated_at,
2127
+ te.role,
2128
+ te.confidence AS relationship_confidence
2129
+ FROM weave.thread_entities te
2130
+ JOIN weave.entities e ON e.id = te.entity_id
2131
+ WHERE te.thread_id IN (
2132
+ SELECT t.id
2133
+ FROM weave.threads t
2134
+ WHERE t.organization_id = $1
2135
+ AND t.id = $2
2136
+ )
2137
+ ORDER BY e.type ASC, e.canonical ASC`;
2138
+ async function listEntitiesByThread(client, args) {
2139
+ const result = await client.query({
2140
+ text: exports.listEntitiesByThreadQuery,
2141
+ values: [args.organizationId, args.threadId],
2142
+ rowMode: "array"
2143
+ });
2144
+ return result.rows.map(row => {
2145
+ return {
2146
+ id: row[0],
2147
+ organizationId: row[1],
2148
+ name: row[2],
2149
+ type: row[3],
2150
+ canonical: row[4],
2151
+ metadata: row[5],
2152
+ createdAt: row[6],
2153
+ updatedAt: row[7],
2154
+ role: row[8],
2155
+ relationshipConfidence: row[9]
2156
+ };
2157
+ });
2158
+ }
2159
+ exports.ensureOrganizationIngestionSettingsQuery = `-- name: EnsureOrganizationIngestionSettings :one
2160
+ WITH upsert AS (
2161
+ INSERT INTO weave.org_ingestion_settings (organization_id)
2162
+ VALUES ($1)
2163
+ ON CONFLICT (organization_id) DO UPDATE
2164
+ SET organization_id = EXCLUDED.organization_id
2165
+ RETURNING
2166
+ organization_id,
2167
+ reasoning_model_id,
2168
+ reasoning_provider_configuration_id,
2169
+ embedding_model_id,
2170
+ embedding_provider_configuration_id,
2171
+ updated_at
2172
+ )
2173
+ SELECT
2174
+ upsert.organization_id,
2175
+ upsert.reasoning_model_id,
2176
+ upsert.reasoning_provider_configuration_id,
2177
+ upsert.embedding_model_id,
2178
+ upsert.embedding_provider_configuration_id,
2179
+ cfg.embedding_dimension AS embedding_dimensions,
2180
+ upsert.updated_at
2181
+ FROM upsert
2182
+ JOIN weave.database_config cfg
2183
+ ON cfg.id = 1`;
2184
+ async function ensureOrganizationIngestionSettings(client, args) {
2185
+ const result = await client.query({
2186
+ text: exports.ensureOrganizationIngestionSettingsQuery,
2187
+ values: [args.organizationId],
2188
+ rowMode: "array"
2189
+ });
2190
+ if (result.rows.length !== 1) {
2191
+ return null;
2192
+ }
2193
+ const row = result.rows[0];
2194
+ return {
2195
+ organizationId: row[0],
2196
+ reasoningModelId: row[1],
2197
+ reasoningProviderConfigurationId: row[2],
2198
+ embeddingModelId: row[3],
2199
+ embeddingProviderConfigurationId: row[4],
2200
+ embeddingDimensions: row[5],
2201
+ updatedAt: row[6]
2202
+ };
2203
+ }
2204
+ exports.getOrganizationIngestionSettingsQuery = `-- name: GetOrganizationIngestionSettings :one
2205
+ SELECT
2206
+ settings.organization_id,
2207
+ settings.reasoning_model_id,
2208
+ settings.reasoning_provider_configuration_id,
2209
+ settings.embedding_model_id,
2210
+ settings.embedding_provider_configuration_id,
2211
+ cfg.embedding_dimension AS embedding_dimensions,
2212
+ settings.updated_at
2213
+ FROM weave.org_ingestion_settings settings
2214
+ JOIN weave.database_config cfg
2215
+ ON cfg.id = 1
2216
+ WHERE settings.organization_id = $1`;
2217
+ async function getOrganizationIngestionSettings(client, args) {
2218
+ const result = await client.query({
2219
+ text: exports.getOrganizationIngestionSettingsQuery,
2220
+ values: [args.organizationId],
2221
+ rowMode: "array"
2222
+ });
2223
+ if (result.rows.length !== 1) {
2224
+ return null;
2225
+ }
2226
+ const row = result.rows[0];
2227
+ return {
2228
+ organizationId: row[0],
2229
+ reasoningModelId: row[1],
2230
+ reasoningProviderConfigurationId: row[2],
2231
+ embeddingModelId: row[3],
2232
+ embeddingProviderConfigurationId: row[4],
2233
+ embeddingDimensions: row[5],
2234
+ updatedAt: row[6]
2235
+ };
2236
+ }
2237
+ exports.upsertOrganizationIngestionSettingsQuery = `-- name: UpsertOrganizationIngestionSettings :one
2238
+ WITH cfg AS (
2239
+ SELECT embedding_dimension
2240
+ FROM weave.database_config
2241
+ WHERE id = 1
2242
+ AND ($1 = 0 OR $1 = embedding_dimension)
2243
+ ), upsert AS (
2244
+ INSERT INTO weave.org_ingestion_settings (
2245
+ organization_id,
2246
+ reasoning_model_id,
2247
+ reasoning_provider_configuration_id,
2248
+ embedding_model_id,
2249
+ embedding_provider_configuration_id
2250
+ )
2251
+ SELECT
2252
+ $2,
2253
+ $3,
2254
+ $4,
2255
+ $5,
2256
+ $6
2257
+ FROM cfg
2258
+ ON CONFLICT (organization_id) DO UPDATE
2259
+ SET
2260
+ reasoning_model_id = EXCLUDED.reasoning_model_id,
2261
+ reasoning_provider_configuration_id = EXCLUDED.reasoning_provider_configuration_id,
2262
+ embedding_model_id = EXCLUDED.embedding_model_id,
2263
+ embedding_provider_configuration_id = EXCLUDED.embedding_provider_configuration_id,
2264
+ updated_at = now()
2265
+ RETURNING
2266
+ organization_id,
2267
+ reasoning_model_id,
2268
+ reasoning_provider_configuration_id,
2269
+ embedding_model_id,
2270
+ embedding_provider_configuration_id,
2271
+ updated_at
2272
+ )
2273
+ SELECT
2274
+ upsert.organization_id,
2275
+ upsert.reasoning_model_id,
2276
+ upsert.reasoning_provider_configuration_id,
2277
+ upsert.embedding_model_id,
2278
+ upsert.embedding_provider_configuration_id,
2279
+ cfg.embedding_dimension AS embedding_dimensions,
2280
+ upsert.updated_at
2281
+ FROM upsert
2282
+ JOIN cfg
2283
+ ON TRUE`;
2284
+ async function upsertOrganizationIngestionSettings(client, args) {
2285
+ const result = await client.query({
2286
+ text: exports.upsertOrganizationIngestionSettingsQuery,
2287
+ values: [args.embeddingDimensions, args.organizationId, args.reasoningModelId, args.reasoningProviderConfigurationId, args.embeddingModelId, args.embeddingProviderConfigurationId],
2288
+ rowMode: "array"
2289
+ });
2290
+ if (result.rows.length !== 1) {
2291
+ return null;
2292
+ }
2293
+ const row = result.rows[0];
2294
+ return {
2295
+ organizationId: row[0],
2296
+ reasoningModelId: row[1],
2297
+ reasoningProviderConfigurationId: row[2],
2298
+ embeddingModelId: row[3],
2299
+ embeddingProviderConfigurationId: row[4],
2300
+ embeddingDimensions: row[5],
2301
+ updatedAt: row[6]
2302
+ };
2303
+ }
2304
+ exports.syncOrganizationIngestionReasoningDefaultsQuery = `-- name: SyncOrganizationIngestionReasoningDefaults :one
2305
+ WITH cfg AS (
2306
+ SELECT embedding_dimension
2307
+ FROM weave.database_config
2308
+ WHERE id = 1
2309
+ ), upsert AS (
2310
+ INSERT INTO weave.org_ingestion_settings (
2311
+ organization_id,
2312
+ reasoning_model_id,
2313
+ reasoning_provider_configuration_id
2314
+ )
2315
+ SELECT
2316
+ cs.organization_id,
2317
+ cs.default_chat_model_id,
2318
+ cs.default_provider_configuration_id
2319
+ FROM weave.organization_chat_settings cs
2320
+ WHERE cs.organization_id = $1
2321
+ ON CONFLICT (organization_id) DO UPDATE
2322
+ SET
2323
+ reasoning_model_id = COALESCE(weave.org_ingestion_settings.reasoning_model_id, EXCLUDED.reasoning_model_id),
2324
+ reasoning_provider_configuration_id = COALESCE(
2325
+ weave.org_ingestion_settings.reasoning_provider_configuration_id,
2326
+ EXCLUDED.reasoning_provider_configuration_id
2327
+ ),
2328
+ updated_at = now()
2329
+ RETURNING
2330
+ organization_id,
2331
+ reasoning_model_id,
2332
+ reasoning_provider_configuration_id,
2333
+ embedding_model_id,
2334
+ embedding_provider_configuration_id,
2335
+ updated_at
2336
+ )
2337
+ SELECT
2338
+ upsert.organization_id,
2339
+ upsert.reasoning_model_id,
2340
+ upsert.reasoning_provider_configuration_id,
2341
+ upsert.embedding_model_id,
2342
+ upsert.embedding_provider_configuration_id,
2343
+ cfg.embedding_dimension AS embedding_dimensions,
2344
+ upsert.updated_at
2345
+ FROM upsert
2346
+ JOIN cfg
2347
+ ON TRUE`;
2348
+ async function syncOrganizationIngestionReasoningDefaults(client, args) {
2349
+ const result = await client.query({
2350
+ text: exports.syncOrganizationIngestionReasoningDefaultsQuery,
2351
+ values: [args.organizationId],
2352
+ rowMode: "array"
2353
+ });
2354
+ if (result.rows.length !== 1) {
2355
+ return null;
2356
+ }
2357
+ const row = result.rows[0];
2358
+ return {
2359
+ organizationId: row[0],
2360
+ reasoningModelId: row[1],
2361
+ reasoningProviderConfigurationId: row[2],
2362
+ embeddingModelId: row[3],
2363
+ embeddingProviderConfigurationId: row[4],
2364
+ embeddingDimensions: row[5],
2365
+ updatedAt: row[6]
2366
+ };
2367
+ }
2368
+ exports.listFacetConfigsQuery = `-- name: ListFacetConfigs :many
2369
+ SELECT
2370
+ organization_id,
2371
+ knowledge_type,
2372
+ label,
2373
+ extraction_hint,
2374
+ required_fields,
2375
+ active
2376
+ FROM weave.org_facet_config
2377
+ WHERE organization_id = $1
2378
+ ORDER BY knowledge_type ASC`;
2379
+ async function listFacetConfigs(client, args) {
2380
+ const result = await client.query({
2381
+ text: exports.listFacetConfigsQuery,
2382
+ values: [args.organizationId],
2383
+ rowMode: "array"
2384
+ });
2385
+ return result.rows.map(row => {
2386
+ return {
2387
+ organizationId: row[0],
2388
+ knowledgeType: row[1],
2389
+ label: row[2],
2390
+ extractionHint: row[3],
2391
+ requiredFields: row[4],
2392
+ active: row[5]
2393
+ };
2394
+ });
2395
+ }
2396
+ exports.listActiveFacetConfigsQuery = `-- name: ListActiveFacetConfigs :many
2397
+ SELECT
2398
+ organization_id,
2399
+ knowledge_type,
2400
+ label,
2401
+ extraction_hint,
2402
+ required_fields,
2403
+ active
2404
+ FROM weave.org_facet_config
2405
+ WHERE organization_id = $1
2406
+ AND active = TRUE
2407
+ ORDER BY knowledge_type ASC`;
2408
+ async function listActiveFacetConfigs(client, args) {
2409
+ const result = await client.query({
2410
+ text: exports.listActiveFacetConfigsQuery,
2411
+ values: [args.organizationId],
2412
+ rowMode: "array"
2413
+ });
2414
+ return result.rows.map(row => {
2415
+ return {
2416
+ organizationId: row[0],
2417
+ knowledgeType: row[1],
2418
+ label: row[2],
2419
+ extractionHint: row[3],
2420
+ requiredFields: row[4],
2421
+ active: row[5]
2422
+ };
2423
+ });
2424
+ }
2425
+ exports.upsertFacetConfigQuery = `-- name: UpsertFacetConfig :one
2426
+ INSERT INTO weave.org_facet_config (
2427
+ organization_id,
2428
+ knowledge_type,
2429
+ label,
2430
+ extraction_hint,
2431
+ required_fields,
2432
+ active
2433
+ ) VALUES (
2434
+ $1,
2435
+ $2,
2436
+ $3,
2437
+ $4,
2438
+ $5,
2439
+ $6
2440
+ )
2441
+ ON CONFLICT (organization_id, knowledge_type) DO UPDATE
2442
+ SET
2443
+ label = EXCLUDED.label,
2444
+ extraction_hint = EXCLUDED.extraction_hint,
2445
+ required_fields = EXCLUDED.required_fields,
2446
+ active = EXCLUDED.active
2447
+ RETURNING
2448
+ organization_id,
2449
+ knowledge_type,
2450
+ label,
2451
+ extraction_hint,
2452
+ required_fields,
2453
+ active`;
2454
+ async function upsertFacetConfig(client, args) {
2455
+ const result = await client.query({
2456
+ text: exports.upsertFacetConfigQuery,
2457
+ values: [args.organizationId, args.knowledgeType, args.label, args.extractionHint, args.requiredFields, args.active],
2458
+ rowMode: "array"
2459
+ });
2460
+ if (result.rows.length !== 1) {
2461
+ return null;
2462
+ }
2463
+ const row = result.rows[0];
2464
+ return {
2465
+ organizationId: row[0],
2466
+ knowledgeType: row[1],
2467
+ label: row[2],
2468
+ extractionHint: row[3],
2469
+ requiredFields: row[4],
2470
+ active: row[5]
2471
+ };
2472
+ }
2473
+ exports.deactivateFacetConfigQuery = `-- name: DeactivateFacetConfig :one
2474
+ UPDATE weave.org_facet_config
2475
+ SET active = FALSE
2476
+ WHERE organization_id = $1
2477
+ AND knowledge_type = $2
2478
+ RETURNING
2479
+ organization_id,
2480
+ knowledge_type,
2481
+ label,
2482
+ extraction_hint,
2483
+ required_fields,
2484
+ active`;
2485
+ async function deactivateFacetConfig(client, args) {
2486
+ const result = await client.query({
2487
+ text: exports.deactivateFacetConfigQuery,
2488
+ values: [args.organizationId, args.knowledgeType],
2489
+ rowMode: "array"
2490
+ });
2491
+ if (result.rows.length !== 1) {
2492
+ return null;
2493
+ }
2494
+ const row = result.rows[0];
2495
+ return {
2496
+ organizationId: row[0],
2497
+ knowledgeType: row[1],
2498
+ label: row[2],
2499
+ extractionHint: row[3],
2500
+ requiredFields: row[4],
2501
+ active: row[5]
2502
+ };
2503
+ }