weave-typescript 0.49.0 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/dist/weaveapi/consolidation/v1/consolidation.pb.d.ts +13 -14
  2. package/dist/weaveapi/consolidation/v1/consolidation.pb.js +62 -74
  3. package/dist/weaveapi/ingestion/v1/ingestion.pb.d.ts +82 -50
  4. package/dist/weaveapi/ingestion/v1/ingestion.pb.js +708 -216
  5. package/dist/weaveapi/ingestion/v1/service.pb.d.ts +50 -7
  6. package/dist/weaveapi/ingestion/v1/service.pb.js +279 -16
  7. package/dist/weaveapi/ingestion/v1/table.pb.d.ts +121 -0
  8. package/dist/weaveapi/ingestion/v1/table.pb.js +1478 -0
  9. package/dist/weaveapi/model/v1/model.pb.d.ts +3 -3
  10. package/dist/weaveapi/model/v1/model.pb.js +33 -33
  11. package/dist/weaveapi/model/v1/service.pb.d.ts +3 -3
  12. package/dist/weaveapi/model/v1/service.pb.js +33 -33
  13. package/dist/weaveapi/retrieval/v1/retrieval.pb.d.ts +123 -0
  14. package/dist/weaveapi/retrieval/v1/retrieval.pb.js +2096 -113
  15. package/dist/weaveapi/retrieval/v1/service.pb.d.ts +29 -1
  16. package/dist/weaveapi/retrieval/v1/service.pb.js +142 -0
  17. package/dist/weavesql/weavedb/agent_sql.d.ts +27 -0
  18. package/dist/weavesql/weavedb/agent_sql.js +43 -1
  19. package/dist/weavesql/weavedb/consolidation_sql.d.ts +33 -33
  20. package/dist/weavesql/weavedb/consolidation_sql.js +56 -56
  21. package/dist/weavesql/weavedb/document_table_sql.d.ts +533 -0
  22. package/dist/weavesql/weavedb/document_table_sql.js +1352 -0
  23. package/dist/weavesql/weavedb/ingestion_sql.d.ts +67 -34
  24. package/dist/weavesql/weavedb/ingestion_sql.js +169 -73
  25. package/dist/weavesql/weavedb/retrieval_sql.d.ts +11 -11
  26. package/dist/weavesql/weavedb/retrieval_sql.js +13 -13
  27. package/package.json +1 -1
@@ -0,0 +1,1352 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.retryDocumentBlobDeletionJobQuery = exports.completeDocumentBlobDeletionJobQuery = exports.claimDocumentBlobDeletionJobQuery = exports.listReadyDocumentBlobDeletionJobsQuery = exports.enqueueDocumentBlobDeletionJobQuery = exports.listSessionTabularRetrievalContextQuery = exports.insertSessionTabularRetrievalContextQuery = exports.findDocumentTableArtifactScopeMismatchesQuery = exports.loadAuthorizedDocumentTableArtifactQuery = exports.deleteDocumentTableMetadataByDocumentIDQuery = exports.listDocumentBlobRefsForDeletionQuery = exports.listCandidateDocumentTablesByDocumentIDsQuery = exports.listCandidateDocumentTablesByIDsQuery = exports.searchCandidateDocumentTablesByMetadataQuery = exports.listThreadTableLinksByThreadIDsQuery = exports.attachThreadTableLinkQuery = exports.getDocumentTableByPermissionQuery = exports.listDocumentTablesByPermissionQuery = exports.markDocumentTablesInactiveQuery = exports.setDocumentTableActiveQuery = exports.createDocumentTableQuery = void 0;
4
+ exports.createDocumentTable = createDocumentTable;
5
+ exports.setDocumentTableActive = setDocumentTableActive;
6
+ exports.listDocumentTablesByPermission = listDocumentTablesByPermission;
7
+ exports.getDocumentTableByPermission = getDocumentTableByPermission;
8
+ exports.attachThreadTableLink = attachThreadTableLink;
9
+ exports.listThreadTableLinksByThreadIDs = listThreadTableLinksByThreadIDs;
10
+ exports.searchCandidateDocumentTablesByMetadata = searchCandidateDocumentTablesByMetadata;
11
+ exports.listCandidateDocumentTablesByIDs = listCandidateDocumentTablesByIDs;
12
+ exports.listCandidateDocumentTablesByDocumentIDs = listCandidateDocumentTablesByDocumentIDs;
13
+ exports.listDocumentBlobRefsForDeletion = listDocumentBlobRefsForDeletion;
14
+ exports.loadAuthorizedDocumentTableArtifact = loadAuthorizedDocumentTableArtifact;
15
+ exports.findDocumentTableArtifactScopeMismatches = findDocumentTableArtifactScopeMismatches;
16
+ exports.insertSessionTabularRetrievalContext = insertSessionTabularRetrievalContext;
17
+ exports.listSessionTabularRetrievalContext = listSessionTabularRetrievalContext;
18
+ exports.enqueueDocumentBlobDeletionJob = enqueueDocumentBlobDeletionJob;
19
+ exports.listReadyDocumentBlobDeletionJobs = listReadyDocumentBlobDeletionJobs;
20
+ exports.claimDocumentBlobDeletionJob = claimDocumentBlobDeletionJob;
21
+ exports.createDocumentTableQuery = `-- name: CreateDocumentTable :one
22
+ INSERT INTO weave.document_tables (
23
+ id,
24
+ organization_id,
25
+ document_id,
26
+ name,
27
+ description,
28
+ summary,
29
+ source_format,
30
+ parser_name,
31
+ table_version,
32
+ active,
33
+ artifact_id,
34
+ artifact_version,
35
+ artifact_storage_ref,
36
+ manifest_storage_ref,
37
+ manifest_version,
38
+ manifest_sha256,
39
+ manifest_size_bytes,
40
+ artifact_scope_kind,
41
+ artifact_scope_key,
42
+ authorization_snapshot,
43
+ safe_table_alias,
44
+ row_count,
45
+ column_count,
46
+ search_text,
47
+ source_location,
48
+ metadata
49
+ ) VALUES (
50
+ $1,
51
+ $2,
52
+ $3,
53
+ $4,
54
+ $5,
55
+ $6,
56
+ $7,
57
+ $8,
58
+ $9,
59
+ $10,
60
+ $11,
61
+ $12,
62
+ $13,
63
+ $14,
64
+ $15,
65
+ $16,
66
+ $17,
67
+ $18,
68
+ $19,
69
+ $20,
70
+ $21,
71
+ $22,
72
+ $23,
73
+ $24,
74
+ $25,
75
+ $26
76
+ )
77
+ RETURNING
78
+ id,
79
+ organization_id,
80
+ document_id,
81
+ name,
82
+ description,
83
+ summary,
84
+ source_format,
85
+ parser_name,
86
+ table_version,
87
+ active,
88
+ artifact_id,
89
+ artifact_version,
90
+ artifact_storage_ref,
91
+ manifest_storage_ref,
92
+ manifest_version,
93
+ manifest_sha256,
94
+ manifest_size_bytes,
95
+ artifact_scope_kind,
96
+ artifact_scope_key,
97
+ authorization_snapshot,
98
+ safe_table_alias,
99
+ row_count,
100
+ column_count,
101
+ search_text,
102
+ source_location,
103
+ metadata,
104
+ created_at,
105
+ updated_at`;
106
+ async function createDocumentTable(client, args) {
107
+ const result = await client.query({
108
+ text: exports.createDocumentTableQuery,
109
+ values: [args.id, args.organizationId, args.documentId, args.name, args.description, args.summary, args.sourceFormat, args.parserName, args.tableVersion, args.active, args.artifactId, args.artifactVersion, args.artifactStorageRef, args.manifestStorageRef, args.manifestVersion, args.manifestSha256, args.manifestSizeBytes, args.artifactScopeKind, args.artifactScopeKey, args.authorizationSnapshot, args.safeTableAlias, args.rowCount, args.columnCount, args.searchText, args.sourceLocation, args.metadata],
110
+ rowMode: "array"
111
+ });
112
+ if (result.rows.length !== 1) {
113
+ return null;
114
+ }
115
+ const row = result.rows[0];
116
+ return {
117
+ id: row[0],
118
+ organizationId: row[1],
119
+ documentId: row[2],
120
+ name: row[3],
121
+ description: row[4],
122
+ summary: row[5],
123
+ sourceFormat: row[6],
124
+ parserName: row[7],
125
+ tableVersion: row[8],
126
+ active: row[9],
127
+ artifactId: row[10],
128
+ artifactVersion: row[11],
129
+ artifactStorageRef: row[12],
130
+ manifestStorageRef: row[13],
131
+ manifestVersion: row[14],
132
+ manifestSha256: row[15],
133
+ manifestSizeBytes: row[16],
134
+ artifactScopeKind: row[17],
135
+ artifactScopeKey: row[18],
136
+ authorizationSnapshot: row[19],
137
+ safeTableAlias: row[20],
138
+ rowCount: row[21],
139
+ columnCount: row[22],
140
+ searchText: row[23],
141
+ sourceLocation: row[24],
142
+ metadata: row[25],
143
+ createdAt: row[26],
144
+ updatedAt: row[27]
145
+ };
146
+ }
147
+ exports.setDocumentTableActiveQuery = `-- name: SetDocumentTableActive :one
148
+ UPDATE weave.document_tables
149
+ SET
150
+ active = $1,
151
+ artifact_id = $2,
152
+ artifact_version = $3,
153
+ artifact_storage_ref = $4,
154
+ manifest_storage_ref = $5,
155
+ manifest_version = $6,
156
+ manifest_sha256 = $7,
157
+ manifest_size_bytes = $8,
158
+ artifact_scope_kind = $9,
159
+ artifact_scope_key = $10,
160
+ authorization_snapshot = $11,
161
+ row_count = $12,
162
+ column_count = $13,
163
+ search_text = $14,
164
+ metadata = $15,
165
+ updated_at = now()
166
+ WHERE organization_id = $16
167
+ AND document_id = $17
168
+ AND id = $18
169
+ RETURNING
170
+ id,
171
+ organization_id,
172
+ document_id,
173
+ name,
174
+ description,
175
+ summary,
176
+ source_format,
177
+ parser_name,
178
+ table_version,
179
+ active,
180
+ artifact_id,
181
+ artifact_version,
182
+ artifact_storage_ref,
183
+ manifest_storage_ref,
184
+ manifest_version,
185
+ manifest_sha256,
186
+ manifest_size_bytes,
187
+ artifact_scope_kind,
188
+ artifact_scope_key,
189
+ authorization_snapshot,
190
+ safe_table_alias,
191
+ row_count,
192
+ column_count,
193
+ search_text,
194
+ source_location,
195
+ metadata,
196
+ created_at,
197
+ updated_at`;
198
+ async function setDocumentTableActive(client, args) {
199
+ const result = await client.query({
200
+ text: exports.setDocumentTableActiveQuery,
201
+ values: [args.active, args.artifactId, args.artifactVersion, args.artifactStorageRef, args.manifestStorageRef, args.manifestVersion, args.manifestSha256, args.manifestSizeBytes, args.artifactScopeKind, args.artifactScopeKey, args.authorizationSnapshot, args.rowCount, args.columnCount, args.searchText, args.metadata, args.organizationId, args.documentId, args.tableId],
202
+ rowMode: "array"
203
+ });
204
+ if (result.rows.length !== 1) {
205
+ return null;
206
+ }
207
+ const row = result.rows[0];
208
+ return {
209
+ id: row[0],
210
+ organizationId: row[1],
211
+ documentId: row[2],
212
+ name: row[3],
213
+ description: row[4],
214
+ summary: row[5],
215
+ sourceFormat: row[6],
216
+ parserName: row[7],
217
+ tableVersion: row[8],
218
+ active: row[9],
219
+ artifactId: row[10],
220
+ artifactVersion: row[11],
221
+ artifactStorageRef: row[12],
222
+ manifestStorageRef: row[13],
223
+ manifestVersion: row[14],
224
+ manifestSha256: row[15],
225
+ manifestSizeBytes: row[16],
226
+ artifactScopeKind: row[17],
227
+ artifactScopeKey: row[18],
228
+ authorizationSnapshot: row[19],
229
+ safeTableAlias: row[20],
230
+ rowCount: row[21],
231
+ columnCount: row[22],
232
+ searchText: row[23],
233
+ sourceLocation: row[24],
234
+ metadata: row[25],
235
+ createdAt: row[26],
236
+ updatedAt: row[27]
237
+ };
238
+ }
239
+ exports.markDocumentTablesInactiveQuery = `-- name: MarkDocumentTablesInactive :execrows
240
+ UPDATE weave.document_tables
241
+ SET
242
+ active = FALSE,
243
+ updated_at = now()
244
+ WHERE organization_id = $1
245
+ AND document_id = $2
246
+ AND active = TRUE`;
247
+ exports.listDocumentTablesByPermissionQuery = `-- name: ListDocumentTablesByPermission :many
248
+ SELECT
249
+ dt.id,
250
+ dt.organization_id,
251
+ dt.document_id,
252
+ d.filename AS document_name,
253
+ dt.name,
254
+ dt.description,
255
+ dt.summary,
256
+ dt.source_format,
257
+ dt.parser_name,
258
+ dt.table_version,
259
+ dt.active,
260
+ dt.artifact_id,
261
+ dt.artifact_version,
262
+ dt.artifact_storage_ref,
263
+ dt.manifest_storage_ref,
264
+ dt.manifest_version,
265
+ dt.manifest_sha256,
266
+ dt.manifest_size_bytes,
267
+ dt.artifact_scope_kind,
268
+ dt.artifact_scope_key,
269
+ dt.authorization_snapshot,
270
+ dt.safe_table_alias,
271
+ dt.row_count,
272
+ dt.column_count,
273
+ dt.search_text,
274
+ dt.source_location,
275
+ dt.metadata,
276
+ dt.created_at,
277
+ dt.updated_at
278
+ FROM weave.document_tables dt
279
+ JOIN weave.documents d
280
+ ON d.organization_id = dt.organization_id
281
+ AND d.id = dt.document_id
282
+ WHERE dt.organization_id = $1
283
+ AND dt.document_id = $2
284
+ AND ($3::boolean OR dt.active = TRUE)
285
+ AND (
286
+ d.scope = 'DOCUMENT_SCOPE_ORG'
287
+ OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
288
+ AND d.uploaded_by_user_id = $4)
289
+ OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
290
+ AND (
291
+ EXISTS (
292
+ SELECT 1
293
+ FROM weave.document_restricted_groups drg
294
+ JOIN weave.restricted_group_user_memberships rgum
295
+ ON rgum.organization_id = drg.organization_id
296
+ AND rgum.restricted_group_id = drg.restricted_group_id
297
+ AND rgum.user_id = $4
298
+ WHERE drg.organization_id = d.organization_id
299
+ AND drg.document_id = d.id
300
+ )
301
+ OR EXISTS (
302
+ SELECT 1
303
+ FROM weave.document_restricted_groups drg
304
+ JOIN weave.restricted_group_team_memberships rgtm
305
+ ON rgtm.organization_id = drg.organization_id
306
+ AND rgtm.restricted_group_id = drg.restricted_group_id
307
+ JOIN weave.org_team_memberships otm
308
+ ON otm.organization_id = rgtm.organization_id
309
+ AND otm.org_team_id = rgtm.org_team_id
310
+ AND otm.user_id = $4
311
+ WHERE drg.organization_id = d.organization_id
312
+ AND drg.document_id = d.id
313
+ )))
314
+ )
315
+ ORDER BY dt.table_version DESC, dt.name ASC, dt.id ASC
316
+ LIMIT $6 OFFSET $5`;
317
+ async function listDocumentTablesByPermission(client, args) {
318
+ const result = await client.query({
319
+ text: exports.listDocumentTablesByPermissionQuery,
320
+ values: [args.organizationId, args.documentId, args.includeInactive, args.actingUserId, args.pageOffset, args.pageSize],
321
+ rowMode: "array"
322
+ });
323
+ return result.rows.map(row => {
324
+ return {
325
+ id: row[0],
326
+ organizationId: row[1],
327
+ documentId: row[2],
328
+ documentName: row[3],
329
+ name: row[4],
330
+ description: row[5],
331
+ summary: row[6],
332
+ sourceFormat: row[7],
333
+ parserName: row[8],
334
+ tableVersion: row[9],
335
+ active: row[10],
336
+ artifactId: row[11],
337
+ artifactVersion: row[12],
338
+ artifactStorageRef: row[13],
339
+ manifestStorageRef: row[14],
340
+ manifestVersion: row[15],
341
+ manifestSha256: row[16],
342
+ manifestSizeBytes: row[17],
343
+ artifactScopeKind: row[18],
344
+ artifactScopeKey: row[19],
345
+ authorizationSnapshot: row[20],
346
+ safeTableAlias: row[21],
347
+ rowCount: row[22],
348
+ columnCount: row[23],
349
+ searchText: row[24],
350
+ sourceLocation: row[25],
351
+ metadata: row[26],
352
+ createdAt: row[27],
353
+ updatedAt: row[28]
354
+ };
355
+ });
356
+ }
357
+ exports.getDocumentTableByPermissionQuery = `-- name: GetDocumentTableByPermission :one
358
+ SELECT
359
+ dt.id,
360
+ dt.organization_id,
361
+ dt.document_id,
362
+ d.filename AS document_name,
363
+ dt.name,
364
+ dt.description,
365
+ dt.summary,
366
+ dt.source_format,
367
+ dt.parser_name,
368
+ dt.table_version,
369
+ dt.active,
370
+ dt.artifact_id,
371
+ dt.artifact_version,
372
+ dt.artifact_storage_ref,
373
+ dt.manifest_storage_ref,
374
+ dt.manifest_version,
375
+ dt.manifest_sha256,
376
+ dt.manifest_size_bytes,
377
+ dt.artifact_scope_kind,
378
+ dt.artifact_scope_key,
379
+ dt.authorization_snapshot,
380
+ dt.safe_table_alias,
381
+ dt.row_count,
382
+ dt.column_count,
383
+ dt.search_text,
384
+ dt.source_location,
385
+ dt.metadata,
386
+ dt.created_at,
387
+ dt.updated_at
388
+ FROM weave.document_tables dt
389
+ JOIN weave.documents d
390
+ ON d.organization_id = dt.organization_id
391
+ AND d.id = dt.document_id
392
+ WHERE dt.organization_id = $1
393
+ AND dt.document_id = $2
394
+ AND dt.id = $3
395
+ AND dt.active = TRUE
396
+ AND (
397
+ d.scope = 'DOCUMENT_SCOPE_ORG'
398
+ OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
399
+ AND d.uploaded_by_user_id = $4)
400
+ OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
401
+ AND (
402
+ EXISTS (
403
+ SELECT 1
404
+ FROM weave.document_restricted_groups drg
405
+ JOIN weave.restricted_group_user_memberships rgum
406
+ ON rgum.organization_id = drg.organization_id
407
+ AND rgum.restricted_group_id = drg.restricted_group_id
408
+ AND rgum.user_id = $4
409
+ WHERE drg.organization_id = d.organization_id
410
+ AND drg.document_id = d.id
411
+ )
412
+ OR EXISTS (
413
+ SELECT 1
414
+ FROM weave.document_restricted_groups drg
415
+ JOIN weave.restricted_group_team_memberships rgtm
416
+ ON rgtm.organization_id = drg.organization_id
417
+ AND rgtm.restricted_group_id = drg.restricted_group_id
418
+ JOIN weave.org_team_memberships otm
419
+ ON otm.organization_id = rgtm.organization_id
420
+ AND otm.org_team_id = rgtm.org_team_id
421
+ AND otm.user_id = $4
422
+ WHERE drg.organization_id = d.organization_id
423
+ AND drg.document_id = d.id
424
+ )))
425
+ )`;
426
+ async function getDocumentTableByPermission(client, args) {
427
+ const result = await client.query({
428
+ text: exports.getDocumentTableByPermissionQuery,
429
+ values: [args.organizationId, args.documentId, args.tableId, args.actingUserId],
430
+ rowMode: "array"
431
+ });
432
+ if (result.rows.length !== 1) {
433
+ return null;
434
+ }
435
+ const row = result.rows[0];
436
+ return {
437
+ id: row[0],
438
+ organizationId: row[1],
439
+ documentId: row[2],
440
+ documentName: row[3],
441
+ name: row[4],
442
+ description: row[5],
443
+ summary: row[6],
444
+ sourceFormat: row[7],
445
+ parserName: row[8],
446
+ tableVersion: row[9],
447
+ active: row[10],
448
+ artifactId: row[11],
449
+ artifactVersion: row[12],
450
+ artifactStorageRef: row[13],
451
+ manifestStorageRef: row[14],
452
+ manifestVersion: row[15],
453
+ manifestSha256: row[16],
454
+ manifestSizeBytes: row[17],
455
+ artifactScopeKind: row[18],
456
+ artifactScopeKey: row[19],
457
+ authorizationSnapshot: row[20],
458
+ safeTableAlias: row[21],
459
+ rowCount: row[22],
460
+ columnCount: row[23],
461
+ searchText: row[24],
462
+ sourceLocation: row[25],
463
+ metadata: row[26],
464
+ createdAt: row[27],
465
+ updatedAt: row[28]
466
+ };
467
+ }
468
+ exports.attachThreadTableLinkQuery = `-- name: AttachThreadTableLink :one
469
+ INSERT INTO weave.thread_table_links (
470
+ organization_id,
471
+ document_id,
472
+ thread_id,
473
+ table_id,
474
+ artifact_id,
475
+ artifact_version,
476
+ table_name,
477
+ table_summary,
478
+ safe_table_alias,
479
+ source_location,
480
+ authorization_snapshot
481
+ ) VALUES (
482
+ $1,
483
+ $2,
484
+ $3,
485
+ $4,
486
+ $5,
487
+ $6,
488
+ $7,
489
+ $8,
490
+ $9,
491
+ $10,
492
+ $11
493
+ )
494
+ ON CONFLICT (thread_id, table_id) DO UPDATE
495
+ SET
496
+ artifact_id = EXCLUDED.artifact_id,
497
+ artifact_version = EXCLUDED.artifact_version,
498
+ table_name = EXCLUDED.table_name,
499
+ table_summary = EXCLUDED.table_summary,
500
+ safe_table_alias = EXCLUDED.safe_table_alias,
501
+ source_location = EXCLUDED.source_location,
502
+ authorization_snapshot = EXCLUDED.authorization_snapshot
503
+ RETURNING
504
+ organization_id,
505
+ document_id,
506
+ thread_id,
507
+ table_id,
508
+ artifact_id,
509
+ artifact_version,
510
+ table_name,
511
+ table_summary,
512
+ safe_table_alias,
513
+ source_location,
514
+ authorization_snapshot,
515
+ created_at`;
516
+ async function attachThreadTableLink(client, args) {
517
+ const result = await client.query({
518
+ text: exports.attachThreadTableLinkQuery,
519
+ values: [args.organizationId, args.documentId, args.threadId, args.tableId, args.artifactId, args.artifactVersion, args.tableName, args.tableSummary, args.safeTableAlias, args.sourceLocation, args.authorizationSnapshot],
520
+ rowMode: "array"
521
+ });
522
+ if (result.rows.length !== 1) {
523
+ return null;
524
+ }
525
+ const row = result.rows[0];
526
+ return {
527
+ organizationId: row[0],
528
+ documentId: row[1],
529
+ threadId: row[2],
530
+ tableId: row[3],
531
+ artifactId: row[4],
532
+ artifactVersion: row[5],
533
+ tableName: row[6],
534
+ tableSummary: row[7],
535
+ safeTableAlias: row[8],
536
+ sourceLocation: row[9],
537
+ authorizationSnapshot: row[10],
538
+ createdAt: row[11]
539
+ };
540
+ }
541
+ exports.listThreadTableLinksByThreadIDsQuery = `-- name: ListThreadTableLinksByThreadIDs :many
542
+ SELECT
543
+ ttl.organization_id,
544
+ ttl.document_id,
545
+ ttl.thread_id,
546
+ ttl.table_id,
547
+ ttl.artifact_id,
548
+ ttl.artifact_version,
549
+ ttl.table_name,
550
+ ttl.table_summary,
551
+ ttl.safe_table_alias,
552
+ ttl.source_location,
553
+ ttl.authorization_snapshot,
554
+ ttl.created_at,
555
+ dt.source_format,
556
+ dt.parser_name,
557
+ dt.row_count,
558
+ dt.column_count,
559
+ dt.manifest_storage_ref,
560
+ dt.manifest_version,
561
+ dt.manifest_sha256,
562
+ dt.manifest_size_bytes
563
+ FROM weave.thread_table_links ttl
564
+ JOIN weave.document_tables dt
565
+ ON dt.organization_id = ttl.organization_id
566
+ AND dt.id = ttl.table_id
567
+ AND dt.active = TRUE
568
+ WHERE ttl.organization_id = $1
569
+ AND ttl.thread_id = ANY($2::uuid[])
570
+ ORDER BY ttl.thread_id ASC, ttl.created_at ASC`;
571
+ async function listThreadTableLinksByThreadIDs(client, args) {
572
+ const result = await client.query({
573
+ text: exports.listThreadTableLinksByThreadIDsQuery,
574
+ values: [args.organizationId, args.threadIds],
575
+ rowMode: "array"
576
+ });
577
+ return result.rows.map(row => {
578
+ return {
579
+ organizationId: row[0],
580
+ documentId: row[1],
581
+ threadId: row[2],
582
+ tableId: row[3],
583
+ artifactId: row[4],
584
+ artifactVersion: row[5],
585
+ tableName: row[6],
586
+ tableSummary: row[7],
587
+ safeTableAlias: row[8],
588
+ sourceLocation: row[9],
589
+ authorizationSnapshot: row[10],
590
+ createdAt: row[11],
591
+ sourceFormat: row[12],
592
+ parserName: row[13],
593
+ rowCount: row[14],
594
+ columnCount: row[15],
595
+ manifestStorageRef: row[16],
596
+ manifestVersion: row[17],
597
+ manifestSha256: row[18],
598
+ manifestSizeBytes: row[19]
599
+ };
600
+ });
601
+ }
602
+ exports.searchCandidateDocumentTablesByMetadataQuery = `-- name: SearchCandidateDocumentTablesByMetadata :many
603
+ SELECT
604
+ dt.id,
605
+ dt.organization_id,
606
+ dt.document_id,
607
+ d.filename AS document_name,
608
+ dt.name,
609
+ dt.description,
610
+ dt.summary,
611
+ dt.source_format,
612
+ dt.parser_name,
613
+ dt.table_version,
614
+ dt.artifact_id,
615
+ dt.artifact_version,
616
+ dt.artifact_storage_ref,
617
+ dt.manifest_storage_ref,
618
+ dt.manifest_version,
619
+ dt.manifest_sha256,
620
+ dt.manifest_size_bytes,
621
+ dt.artifact_scope_kind,
622
+ dt.artifact_scope_key,
623
+ dt.authorization_snapshot,
624
+ dt.safe_table_alias,
625
+ dt.row_count,
626
+ dt.column_count,
627
+ dt.source_location,
628
+ ts_rank_cd(
629
+ to_tsvector('simple', dt.search_text),
630
+ plainto_tsquery('simple', $1)
631
+ )::real AS score
632
+ FROM weave.document_tables dt
633
+ JOIN weave.documents d
634
+ ON d.organization_id = dt.organization_id
635
+ AND d.id = dt.document_id
636
+ WHERE dt.organization_id = $2
637
+ AND dt.active = TRUE
638
+ AND (
639
+ d.scope = 'DOCUMENT_SCOPE_ORG'
640
+ OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
641
+ AND d.uploaded_by_user_id = $3)
642
+ OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
643
+ AND (
644
+ EXISTS (
645
+ SELECT 1
646
+ FROM weave.document_restricted_groups drg
647
+ JOIN weave.restricted_group_user_memberships rgum
648
+ ON rgum.organization_id = drg.organization_id
649
+ AND rgum.restricted_group_id = drg.restricted_group_id
650
+ AND rgum.user_id = $3
651
+ WHERE drg.organization_id = d.organization_id
652
+ AND drg.document_id = d.id
653
+ )
654
+ OR EXISTS (
655
+ SELECT 1
656
+ FROM weave.document_restricted_groups drg
657
+ JOIN weave.restricted_group_team_memberships rgtm
658
+ ON rgtm.organization_id = drg.organization_id
659
+ AND rgtm.restricted_group_id = drg.restricted_group_id
660
+ JOIN weave.org_team_memberships otm
661
+ ON otm.organization_id = rgtm.organization_id
662
+ AND otm.org_team_id = rgtm.org_team_id
663
+ AND otm.user_id = $3
664
+ WHERE drg.organization_id = d.organization_id
665
+ AND drg.document_id = d.id
666
+ )))
667
+ )
668
+ AND to_tsvector('simple', dt.search_text) @@ plainto_tsquery('simple', $1)
669
+ ORDER BY score DESC, dt.updated_at DESC
670
+ LIMIT $4`;
671
+ async function searchCandidateDocumentTablesByMetadata(client, args) {
672
+ const result = await client.query({
673
+ text: exports.searchCandidateDocumentTablesByMetadataQuery,
674
+ values: [args.query, args.organizationId, args.actingUserId, args.resultLimit],
675
+ rowMode: "array"
676
+ });
677
+ return result.rows.map(row => {
678
+ return {
679
+ id: row[0],
680
+ organizationId: row[1],
681
+ documentId: row[2],
682
+ documentName: row[3],
683
+ name: row[4],
684
+ description: row[5],
685
+ summary: row[6],
686
+ sourceFormat: row[7],
687
+ parserName: row[8],
688
+ tableVersion: row[9],
689
+ artifactId: row[10],
690
+ artifactVersion: row[11],
691
+ artifactStorageRef: row[12],
692
+ manifestStorageRef: row[13],
693
+ manifestVersion: row[14],
694
+ manifestSha256: row[15],
695
+ manifestSizeBytes: row[16],
696
+ artifactScopeKind: row[17],
697
+ artifactScopeKey: row[18],
698
+ authorizationSnapshot: row[19],
699
+ safeTableAlias: row[20],
700
+ rowCount: row[21],
701
+ columnCount: row[22],
702
+ sourceLocation: row[23],
703
+ score: row[24]
704
+ };
705
+ });
706
+ }
707
+ exports.listCandidateDocumentTablesByIDsQuery = `-- name: ListCandidateDocumentTablesByIDs :many
708
+ SELECT
709
+ dt.id,
710
+ dt.organization_id,
711
+ dt.document_id,
712
+ d.filename AS document_name,
713
+ dt.name,
714
+ dt.summary,
715
+ dt.source_format,
716
+ dt.parser_name,
717
+ dt.artifact_id,
718
+ dt.artifact_version,
719
+ dt.artifact_storage_ref,
720
+ dt.manifest_storage_ref,
721
+ dt.manifest_version,
722
+ dt.manifest_sha256,
723
+ dt.manifest_size_bytes,
724
+ dt.artifact_scope_kind,
725
+ dt.artifact_scope_key,
726
+ dt.authorization_snapshot,
727
+ dt.safe_table_alias,
728
+ dt.row_count,
729
+ dt.column_count,
730
+ dt.source_location
731
+ FROM weave.document_tables dt
732
+ JOIN weave.documents d
733
+ ON d.organization_id = dt.organization_id
734
+ AND d.id = dt.document_id
735
+ WHERE dt.organization_id = $1
736
+ AND dt.active = TRUE
737
+ AND dt.id = ANY($2::uuid[])
738
+ AND (
739
+ d.scope = 'DOCUMENT_SCOPE_ORG'
740
+ OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
741
+ AND d.uploaded_by_user_id = $3)
742
+ OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
743
+ AND (
744
+ EXISTS (
745
+ SELECT 1
746
+ FROM weave.document_restricted_groups drg
747
+ JOIN weave.restricted_group_user_memberships rgum
748
+ ON rgum.organization_id = drg.organization_id
749
+ AND rgum.restricted_group_id = drg.restricted_group_id
750
+ AND rgum.user_id = $3
751
+ WHERE drg.organization_id = d.organization_id
752
+ AND drg.document_id = d.id
753
+ )
754
+ OR EXISTS (
755
+ SELECT 1
756
+ FROM weave.document_restricted_groups drg
757
+ JOIN weave.restricted_group_team_memberships rgtm
758
+ ON rgtm.organization_id = drg.organization_id
759
+ AND rgtm.restricted_group_id = drg.restricted_group_id
760
+ JOIN weave.org_team_memberships otm
761
+ ON otm.organization_id = rgtm.organization_id
762
+ AND otm.org_team_id = rgtm.org_team_id
763
+ AND otm.user_id = $3
764
+ WHERE drg.organization_id = d.organization_id
765
+ AND drg.document_id = d.id
766
+ )))
767
+ )
768
+ ORDER BY dt.updated_at DESC, dt.id ASC
769
+ LIMIT $4`;
770
+ async function listCandidateDocumentTablesByIDs(client, args) {
771
+ const result = await client.query({
772
+ text: exports.listCandidateDocumentTablesByIDsQuery,
773
+ values: [args.organizationId, args.tableIds, args.actingUserId, args.resultLimit],
774
+ rowMode: "array"
775
+ });
776
+ return result.rows.map(row => {
777
+ return {
778
+ id: row[0],
779
+ organizationId: row[1],
780
+ documentId: row[2],
781
+ documentName: row[3],
782
+ name: row[4],
783
+ summary: row[5],
784
+ sourceFormat: row[6],
785
+ parserName: row[7],
786
+ artifactId: row[8],
787
+ artifactVersion: row[9],
788
+ artifactStorageRef: row[10],
789
+ manifestStorageRef: row[11],
790
+ manifestVersion: row[12],
791
+ manifestSha256: row[13],
792
+ manifestSizeBytes: row[14],
793
+ artifactScopeKind: row[15],
794
+ artifactScopeKey: row[16],
795
+ authorizationSnapshot: row[17],
796
+ safeTableAlias: row[18],
797
+ rowCount: row[19],
798
+ columnCount: row[20],
799
+ sourceLocation: row[21]
800
+ };
801
+ });
802
+ }
803
+ exports.listCandidateDocumentTablesByDocumentIDsQuery = `-- name: ListCandidateDocumentTablesByDocumentIDs :many
804
+ SELECT
805
+ dt.id,
806
+ dt.organization_id,
807
+ dt.document_id,
808
+ d.filename AS document_name,
809
+ dt.name,
810
+ dt.summary,
811
+ dt.source_format,
812
+ dt.parser_name,
813
+ dt.artifact_id,
814
+ dt.artifact_version,
815
+ dt.artifact_storage_ref,
816
+ dt.manifest_storage_ref,
817
+ dt.manifest_version,
818
+ dt.manifest_sha256,
819
+ dt.manifest_size_bytes,
820
+ dt.artifact_scope_kind,
821
+ dt.artifact_scope_key,
822
+ dt.authorization_snapshot,
823
+ dt.safe_table_alias,
824
+ dt.row_count,
825
+ dt.column_count,
826
+ dt.source_location
827
+ FROM weave.document_tables dt
828
+ JOIN weave.documents d
829
+ ON d.organization_id = dt.organization_id
830
+ AND d.id = dt.document_id
831
+ WHERE dt.organization_id = $1
832
+ AND dt.active = TRUE
833
+ AND dt.document_id = ANY($2::uuid[])
834
+ AND (
835
+ d.scope = 'DOCUMENT_SCOPE_ORG'
836
+ OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
837
+ AND d.uploaded_by_user_id = $3)
838
+ OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
839
+ AND (
840
+ EXISTS (
841
+ SELECT 1
842
+ FROM weave.document_restricted_groups drg
843
+ JOIN weave.restricted_group_user_memberships rgum
844
+ ON rgum.organization_id = drg.organization_id
845
+ AND rgum.restricted_group_id = drg.restricted_group_id
846
+ AND rgum.user_id = $3
847
+ WHERE drg.organization_id = d.organization_id
848
+ AND drg.document_id = d.id
849
+ )
850
+ OR EXISTS (
851
+ SELECT 1
852
+ FROM weave.document_restricted_groups drg
853
+ JOIN weave.restricted_group_team_memberships rgtm
854
+ ON rgtm.organization_id = drg.organization_id
855
+ AND rgtm.restricted_group_id = drg.restricted_group_id
856
+ JOIN weave.org_team_memberships otm
857
+ ON otm.organization_id = rgtm.organization_id
858
+ AND otm.org_team_id = rgtm.org_team_id
859
+ AND otm.user_id = $3
860
+ WHERE drg.organization_id = d.organization_id
861
+ AND drg.document_id = d.id
862
+ )))
863
+ )
864
+ ORDER BY dt.updated_at DESC, dt.id ASC
865
+ LIMIT $4`;
866
+ async function listCandidateDocumentTablesByDocumentIDs(client, args) {
867
+ const result = await client.query({
868
+ text: exports.listCandidateDocumentTablesByDocumentIDsQuery,
869
+ values: [args.organizationId, args.documentIds, args.actingUserId, args.resultLimit],
870
+ rowMode: "array"
871
+ });
872
+ return result.rows.map(row => {
873
+ return {
874
+ id: row[0],
875
+ organizationId: row[1],
876
+ documentId: row[2],
877
+ documentName: row[3],
878
+ name: row[4],
879
+ summary: row[5],
880
+ sourceFormat: row[6],
881
+ parserName: row[7],
882
+ artifactId: row[8],
883
+ artifactVersion: row[9],
884
+ artifactStorageRef: row[10],
885
+ manifestStorageRef: row[11],
886
+ manifestVersion: row[12],
887
+ manifestSha256: row[13],
888
+ manifestSizeBytes: row[14],
889
+ artifactScopeKind: row[15],
890
+ artifactScopeKey: row[16],
891
+ authorizationSnapshot: row[17],
892
+ safeTableAlias: row[18],
893
+ rowCount: row[19],
894
+ columnCount: row[20],
895
+ sourceLocation: row[21]
896
+ };
897
+ });
898
+ }
899
+ exports.listDocumentBlobRefsForDeletionQuery = `-- name: ListDocumentBlobRefsForDeletion :many
900
+ SELECT
901
+ d.id AS document_id,
902
+ 'DOCUMENT_BLOB_KIND_SOURCE'::text AS blob_kind,
903
+ NULL::uuid AS artifact_id,
904
+ d.storage_ref
905
+ FROM weave.documents d
906
+ WHERE d.organization_id = $1
907
+ AND d.id = $2
908
+ AND d.storage_ref <> ''
909
+ UNION ALL
910
+ SELECT
911
+ dt.document_id,
912
+ 'DOCUMENT_BLOB_KIND_TABLE_ARTIFACT'::text AS blob_kind,
913
+ dt.artifact_id,
914
+ dt.artifact_storage_ref AS storage_ref
915
+ FROM weave.document_tables dt
916
+ WHERE dt.organization_id = $1
917
+ AND dt.document_id = $2
918
+ AND dt.artifact_storage_ref <> ''
919
+ UNION ALL
920
+ SELECT
921
+ dt.document_id,
922
+ 'DOCUMENT_BLOB_KIND_TABLE_MANIFEST'::text AS blob_kind,
923
+ dt.artifact_id,
924
+ dt.manifest_storage_ref AS storage_ref
925
+ FROM weave.document_tables dt
926
+ WHERE dt.organization_id = $1
927
+ AND dt.document_id = $2
928
+ AND dt.manifest_storage_ref <> ''
929
+ ORDER BY blob_kind ASC, artifact_id ASC`;
930
+ async function listDocumentBlobRefsForDeletion(client, args) {
931
+ const result = await client.query({
932
+ text: exports.listDocumentBlobRefsForDeletionQuery,
933
+ values: [args.organizationId, args.documentId],
934
+ rowMode: "array"
935
+ });
936
+ return result.rows.map(row => {
937
+ return {
938
+ documentId: row[0],
939
+ blobKind: row[1],
940
+ artifactId: row[2],
941
+ storageRef: row[3]
942
+ };
943
+ });
944
+ }
945
+ exports.deleteDocumentTableMetadataByDocumentIDQuery = `-- name: DeleteDocumentTableMetadataByDocumentID :execrows
946
+ DELETE FROM weave.document_tables
947
+ WHERE organization_id = $1
948
+ AND document_id = $2`;
949
+ exports.loadAuthorizedDocumentTableArtifactQuery = `-- name: LoadAuthorizedDocumentTableArtifact :one
950
+ SELECT
951
+ dt.id,
952
+ dt.organization_id,
953
+ dt.document_id,
954
+ d.filename AS document_name,
955
+ dt.name,
956
+ dt.artifact_id,
957
+ dt.artifact_version,
958
+ dt.artifact_storage_ref,
959
+ dt.artifact_scope_kind,
960
+ dt.artifact_scope_key,
961
+ dt.authorization_snapshot,
962
+ dt.safe_table_alias,
963
+ dt.source_location
964
+ FROM weave.document_tables dt
965
+ JOIN weave.documents d
966
+ ON d.organization_id = dt.organization_id
967
+ AND d.id = dt.document_id
968
+ WHERE dt.organization_id = $1
969
+ AND dt.active = TRUE
970
+ AND dt.artifact_id = $2
971
+ AND ($3::uuid IS NULL OR dt.document_id = $3::uuid)
972
+ AND ($4::uuid IS NULL OR dt.id = $4::uuid)
973
+ AND (
974
+ d.scope = 'DOCUMENT_SCOPE_ORG'
975
+ OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
976
+ AND d.uploaded_by_user_id = $5)
977
+ OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
978
+ AND (
979
+ EXISTS (
980
+ SELECT 1
981
+ FROM weave.document_restricted_groups drg
982
+ JOIN weave.restricted_group_user_memberships rgum
983
+ ON rgum.organization_id = drg.organization_id
984
+ AND rgum.restricted_group_id = drg.restricted_group_id
985
+ AND rgum.user_id = $5
986
+ WHERE drg.organization_id = d.organization_id
987
+ AND drg.document_id = d.id
988
+ )
989
+ OR EXISTS (
990
+ SELECT 1
991
+ FROM weave.document_restricted_groups drg
992
+ JOIN weave.restricted_group_team_memberships rgtm
993
+ ON rgtm.organization_id = drg.organization_id
994
+ AND rgtm.restricted_group_id = drg.restricted_group_id
995
+ JOIN weave.org_team_memberships otm
996
+ ON otm.organization_id = rgtm.organization_id
997
+ AND otm.org_team_id = rgtm.org_team_id
998
+ AND otm.user_id = $5
999
+ WHERE drg.organization_id = d.organization_id
1000
+ AND drg.document_id = d.id
1001
+ )))
1002
+ )`;
1003
+ async function loadAuthorizedDocumentTableArtifact(client, args) {
1004
+ const result = await client.query({
1005
+ text: exports.loadAuthorizedDocumentTableArtifactQuery,
1006
+ values: [args.organizationId, args.artifactId, args.documentId, args.tableId, args.actingUserId],
1007
+ rowMode: "array"
1008
+ });
1009
+ if (result.rows.length !== 1) {
1010
+ return null;
1011
+ }
1012
+ const row = result.rows[0];
1013
+ return {
1014
+ id: row[0],
1015
+ organizationId: row[1],
1016
+ documentId: row[2],
1017
+ documentName: row[3],
1018
+ name: row[4],
1019
+ artifactId: row[5],
1020
+ artifactVersion: row[6],
1021
+ artifactStorageRef: row[7],
1022
+ artifactScopeKind: row[8],
1023
+ artifactScopeKey: row[9],
1024
+ authorizationSnapshot: row[10],
1025
+ safeTableAlias: row[11],
1026
+ sourceLocation: row[12]
1027
+ };
1028
+ }
1029
+ exports.findDocumentTableArtifactScopeMismatchesQuery = `-- name: FindDocumentTableArtifactScopeMismatches :many
1030
+ SELECT
1031
+ dt.id,
1032
+ dt.organization_id,
1033
+ dt.document_id,
1034
+ dt.artifact_id,
1035
+ dt.artifact_scope_kind,
1036
+ dt.artifact_scope_key,
1037
+ d.scope AS current_scope,
1038
+ CASE
1039
+ WHEN d.scope = 'DOCUMENT_SCOPE_ORG'
1040
+ THEN ''::text
1041
+ WHEN d.scope = 'DOCUMENT_SCOPE_PRIVATE'
1042
+ THEN d.uploaded_by_user_id::text
1043
+ WHEN d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
1044
+ THEN COALESCE(rg_scope.restricted_group_id::text, '')
1045
+ ELSE ''::text
1046
+ END AS current_scope_key
1047
+ FROM weave.document_tables dt
1048
+ JOIN weave.documents d
1049
+ ON d.organization_id = dt.organization_id
1050
+ AND d.id = dt.document_id
1051
+ LEFT JOIN LATERAL (
1052
+ SELECT drg.restricted_group_id
1053
+ FROM weave.document_restricted_groups drg
1054
+ WHERE drg.organization_id = d.organization_id
1055
+ AND drg.document_id = d.id
1056
+ ORDER BY drg.restricted_group_id ASC
1057
+ LIMIT 1
1058
+ ) rg_scope ON TRUE
1059
+ WHERE dt.organization_id = $1
1060
+ AND dt.active = TRUE
1061
+ AND (
1062
+ dt.artifact_scope_kind <> d.scope
1063
+ OR dt.artifact_scope_key <> CASE
1064
+ WHEN d.scope = 'DOCUMENT_SCOPE_ORG'
1065
+ THEN ''::text
1066
+ WHEN d.scope = 'DOCUMENT_SCOPE_PRIVATE'
1067
+ THEN d.uploaded_by_user_id::text
1068
+ WHEN d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
1069
+ THEN COALESCE(rg_scope.restricted_group_id::text, '')
1070
+ ELSE ''::text
1071
+ END
1072
+ )`;
1073
+ async function findDocumentTableArtifactScopeMismatches(client, args) {
1074
+ const result = await client.query({
1075
+ text: exports.findDocumentTableArtifactScopeMismatchesQuery,
1076
+ values: [args.organizationId],
1077
+ rowMode: "array"
1078
+ });
1079
+ return result.rows.map(row => {
1080
+ return {
1081
+ id: row[0],
1082
+ organizationId: row[1],
1083
+ documentId: row[2],
1084
+ artifactId: row[3],
1085
+ artifactScopeKind: row[4],
1086
+ artifactScopeKey: row[5],
1087
+ currentScope: row[6],
1088
+ currentScopeKey: row[7]
1089
+ };
1090
+ });
1091
+ }
1092
+ exports.insertSessionTabularRetrievalContextQuery = `-- name: InsertSessionTabularRetrievalContext :one
1093
+ INSERT INTO weave.session_tabular_retrieval_context (
1094
+ session_id,
1095
+ turn_number,
1096
+ organization_id,
1097
+ table_id,
1098
+ artifact_id,
1099
+ query,
1100
+ sql,
1101
+ result_row_count,
1102
+ trace
1103
+ ) VALUES (
1104
+ $1,
1105
+ $2,
1106
+ $3,
1107
+ $4,
1108
+ $5,
1109
+ $6,
1110
+ $7,
1111
+ $8,
1112
+ $9
1113
+ )
1114
+ RETURNING
1115
+ session_id,
1116
+ turn_number,
1117
+ organization_id,
1118
+ table_id,
1119
+ artifact_id,
1120
+ query,
1121
+ sql,
1122
+ result_row_count,
1123
+ trace,
1124
+ retrieved_at`;
1125
+ async function insertSessionTabularRetrievalContext(client, args) {
1126
+ const result = await client.query({
1127
+ text: exports.insertSessionTabularRetrievalContextQuery,
1128
+ values: [args.sessionId, args.turnNumber, args.organizationId, args.tableId, args.artifactId, args.query, args.sql, args.resultRowCount, args.trace],
1129
+ rowMode: "array"
1130
+ });
1131
+ if (result.rows.length !== 1) {
1132
+ return null;
1133
+ }
1134
+ const row = result.rows[0];
1135
+ return {
1136
+ sessionId: row[0],
1137
+ turnNumber: row[1],
1138
+ organizationId: row[2],
1139
+ tableId: row[3],
1140
+ artifactId: row[4],
1141
+ query: row[5],
1142
+ sql: row[6],
1143
+ resultRowCount: row[7],
1144
+ trace: row[8],
1145
+ retrievedAt: row[9]
1146
+ };
1147
+ }
1148
+ exports.listSessionTabularRetrievalContextQuery = `-- name: ListSessionTabularRetrievalContext :many
1149
+ SELECT
1150
+ session_id,
1151
+ turn_number,
1152
+ organization_id,
1153
+ table_id,
1154
+ artifact_id,
1155
+ query,
1156
+ sql,
1157
+ result_row_count,
1158
+ trace,
1159
+ retrieved_at
1160
+ FROM weave.session_tabular_retrieval_context
1161
+ WHERE session_id = $1
1162
+ AND organization_id = $2
1163
+ ORDER BY turn_number DESC, retrieved_at DESC
1164
+ LIMIT $3`;
1165
+ async function listSessionTabularRetrievalContext(client, args) {
1166
+ const result = await client.query({
1167
+ text: exports.listSessionTabularRetrievalContextQuery,
1168
+ values: [args.sessionId, args.organizationId, args.resultLimit],
1169
+ rowMode: "array"
1170
+ });
1171
+ return result.rows.map(row => {
1172
+ return {
1173
+ sessionId: row[0],
1174
+ turnNumber: row[1],
1175
+ organizationId: row[2],
1176
+ tableId: row[3],
1177
+ artifactId: row[4],
1178
+ query: row[5],
1179
+ sql: row[6],
1180
+ resultRowCount: row[7],
1181
+ trace: row[8],
1182
+ retrievedAt: row[9]
1183
+ };
1184
+ });
1185
+ }
1186
+ exports.enqueueDocumentBlobDeletionJobQuery = `-- name: EnqueueDocumentBlobDeletionJob :one
1187
+ INSERT INTO weave.document_blob_deletion_jobs (
1188
+ id,
1189
+ organization_id,
1190
+ document_id,
1191
+ artifact_id,
1192
+ blob_kind,
1193
+ storage_ref,
1194
+ status,
1195
+ next_retry_at
1196
+ ) VALUES (
1197
+ $1,
1198
+ $2,
1199
+ $3,
1200
+ $4::uuid,
1201
+ $5,
1202
+ $6,
1203
+ $7,
1204
+ $8
1205
+ )
1206
+ RETURNING
1207
+ id,
1208
+ organization_id,
1209
+ document_id,
1210
+ artifact_id,
1211
+ blob_kind,
1212
+ storage_ref,
1213
+ status,
1214
+ attempt_count,
1215
+ next_retry_at,
1216
+ last_error,
1217
+ created_at,
1218
+ updated_at`;
1219
+ async function enqueueDocumentBlobDeletionJob(client, args) {
1220
+ const result = await client.query({
1221
+ text: exports.enqueueDocumentBlobDeletionJobQuery,
1222
+ values: [args.id, args.organizationId, args.documentId, args.artifactId, args.blobKind, args.storageRef, args.status, args.nextRetryAt],
1223
+ rowMode: "array"
1224
+ });
1225
+ if (result.rows.length !== 1) {
1226
+ return null;
1227
+ }
1228
+ const row = result.rows[0];
1229
+ return {
1230
+ id: row[0],
1231
+ organizationId: row[1],
1232
+ documentId: row[2],
1233
+ artifactId: row[3],
1234
+ blobKind: row[4],
1235
+ storageRef: row[5],
1236
+ status: row[6],
1237
+ attemptCount: row[7],
1238
+ nextRetryAt: row[8],
1239
+ lastError: row[9],
1240
+ createdAt: row[10],
1241
+ updatedAt: row[11]
1242
+ };
1243
+ }
1244
+ exports.listReadyDocumentBlobDeletionJobsQuery = `-- name: ListReadyDocumentBlobDeletionJobs :many
1245
+ SELECT
1246
+ id,
1247
+ organization_id,
1248
+ document_id,
1249
+ artifact_id,
1250
+ blob_kind,
1251
+ storage_ref,
1252
+ status,
1253
+ attempt_count,
1254
+ next_retry_at,
1255
+ last_error,
1256
+ created_at,
1257
+ updated_at
1258
+ FROM weave.document_blob_deletion_jobs
1259
+ WHERE status IN (
1260
+ 'DOCUMENT_BLOB_DELETION_JOB_STATUS_PENDING',
1261
+ 'DOCUMENT_BLOB_DELETION_JOB_STATUS_RETRYABLE'
1262
+ )
1263
+ AND next_retry_at <= now()
1264
+ ORDER BY next_retry_at ASC, created_at ASC
1265
+ LIMIT $1`;
1266
+ async function listReadyDocumentBlobDeletionJobs(client, args) {
1267
+ const result = await client.query({
1268
+ text: exports.listReadyDocumentBlobDeletionJobsQuery,
1269
+ values: [args.resultLimit],
1270
+ rowMode: "array"
1271
+ });
1272
+ return result.rows.map(row => {
1273
+ return {
1274
+ id: row[0],
1275
+ organizationId: row[1],
1276
+ documentId: row[2],
1277
+ artifactId: row[3],
1278
+ blobKind: row[4],
1279
+ storageRef: row[5],
1280
+ status: row[6],
1281
+ attemptCount: row[7],
1282
+ nextRetryAt: row[8],
1283
+ lastError: row[9],
1284
+ createdAt: row[10],
1285
+ updatedAt: row[11]
1286
+ };
1287
+ });
1288
+ }
1289
+ exports.claimDocumentBlobDeletionJobQuery = `-- name: ClaimDocumentBlobDeletionJob :one
1290
+ UPDATE weave.document_blob_deletion_jobs
1291
+ SET
1292
+ status = 'DOCUMENT_BLOB_DELETION_JOB_STATUS_CLAIMED',
1293
+ attempt_count = attempt_count + 1,
1294
+ updated_at = now()
1295
+ WHERE id = $1
1296
+ AND status IN (
1297
+ 'DOCUMENT_BLOB_DELETION_JOB_STATUS_PENDING',
1298
+ 'DOCUMENT_BLOB_DELETION_JOB_STATUS_RETRYABLE'
1299
+ )
1300
+ RETURNING
1301
+ id,
1302
+ organization_id,
1303
+ document_id,
1304
+ artifact_id,
1305
+ blob_kind,
1306
+ storage_ref,
1307
+ status,
1308
+ attempt_count,
1309
+ next_retry_at,
1310
+ last_error,
1311
+ created_at,
1312
+ updated_at`;
1313
+ async function claimDocumentBlobDeletionJob(client, args) {
1314
+ const result = await client.query({
1315
+ text: exports.claimDocumentBlobDeletionJobQuery,
1316
+ values: [args.id],
1317
+ rowMode: "array"
1318
+ });
1319
+ if (result.rows.length !== 1) {
1320
+ return null;
1321
+ }
1322
+ const row = result.rows[0];
1323
+ return {
1324
+ id: row[0],
1325
+ organizationId: row[1],
1326
+ documentId: row[2],
1327
+ artifactId: row[3],
1328
+ blobKind: row[4],
1329
+ storageRef: row[5],
1330
+ status: row[6],
1331
+ attemptCount: row[7],
1332
+ nextRetryAt: row[8],
1333
+ lastError: row[9],
1334
+ createdAt: row[10],
1335
+ updatedAt: row[11]
1336
+ };
1337
+ }
1338
+ exports.completeDocumentBlobDeletionJobQuery = `-- name: CompleteDocumentBlobDeletionJob :execrows
1339
+ UPDATE weave.document_blob_deletion_jobs
1340
+ SET
1341
+ status = 'DOCUMENT_BLOB_DELETION_JOB_STATUS_COMPLETE',
1342
+ last_error = '',
1343
+ updated_at = now()
1344
+ WHERE id = $1`;
1345
+ exports.retryDocumentBlobDeletionJobQuery = `-- name: RetryDocumentBlobDeletionJob :execrows
1346
+ UPDATE weave.document_blob_deletion_jobs
1347
+ SET
1348
+ status = $1,
1349
+ last_error = $2,
1350
+ next_retry_at = $3,
1351
+ updated_at = now()
1352
+ WHERE id = $4`;