weave-typescript 0.51.3 → 0.53.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 (185) hide show
  1. package/dist/google/protobuf/duration.pb.d.ts +99 -0
  2. package/dist/google/protobuf/duration.pb.js +93 -0
  3. package/dist/index.d.ts +1 -0
  4. package/dist/index.js +2 -1
  5. package/dist/weave/authz/v1/index.d.ts +1 -0
  6. package/dist/weave/authz/v1/index.js +38 -0
  7. package/dist/weave/authz/v1/options.pb.d.ts +52 -0
  8. package/dist/weave/authz/v1/options.pb.js +403 -0
  9. package/dist/weave/filter/v1/index.d.ts +1 -0
  10. package/dist/weave/filter/v1/index.js +38 -0
  11. package/dist/{weaveapi/atc/v1/message.pb.d.ts → weave/filter/v1/options.pb.d.ts} +33 -21
  12. package/dist/weave/filter/v1/options.pb.js +417 -0
  13. package/dist/weave/index.d.ts +2 -0
  14. package/dist/weave/index.js +39 -0
  15. package/dist/weaveapi/agent/v1/agent.pb.d.ts +137 -188
  16. package/dist/weaveapi/agent/v1/agent.pb.js +891 -2151
  17. package/dist/weaveapi/agent/v1/index.d.ts +1 -1
  18. package/dist/weaveapi/agent/v1/index.js +2 -2
  19. package/dist/weaveapi/agent/v1/run.pb.d.ts +289 -0
  20. package/dist/weaveapi/agent/v1/run.pb.js +2763 -0
  21. package/dist/weaveapi/auth/v1/auth.pb.d.ts +67 -14
  22. package/dist/weaveapi/auth/v1/auth.pb.js +829 -89
  23. package/dist/weaveapi/auth/v1/index.d.ts +4 -1
  24. package/dist/weaveapi/auth/v1/index.js +5 -2
  25. package/dist/weaveapi/auth/v1/{service.pb.d.ts → organization_admin.pb.d.ts} +39 -530
  26. package/dist/weaveapi/auth/v1/{service.pb.js → organization_admin.pb.js} +2978 -5812
  27. package/dist/weaveapi/auth/v1/personal_access_token.pb.d.ts +114 -0
  28. package/dist/weaveapi/auth/v1/personal_access_token.pb.js +750 -0
  29. package/dist/weaveapi/auth/v1/profile.pb.d.ts +161 -0
  30. package/dist/weaveapi/auth/v1/profile.pb.js +1007 -0
  31. package/dist/weaveapi/auth/v1/session.pb.d.ts +460 -0
  32. package/dist/weaveapi/auth/v1/session.pb.js +3544 -0
  33. package/dist/weaveapi/chat/v1/chat.pb.d.ts +377 -149
  34. package/dist/weaveapi/chat/v1/chat.pb.js +3492 -1614
  35. package/dist/weaveapi/chat/v1/index.d.ts +0 -1
  36. package/dist/weaveapi/chat/v1/index.js +1 -2
  37. package/dist/weaveapi/consolidation/v1/consolidation.pb.d.ts +95 -0
  38. package/dist/weaveapi/consolidation/v1/consolidation.pb.js +770 -1
  39. package/dist/weaveapi/consolidation/v1/index.d.ts +0 -1
  40. package/dist/weaveapi/consolidation/v1/index.js +1 -2
  41. package/dist/weaveapi/content/v1/content.pb.d.ts +63 -0
  42. package/dist/weaveapi/content/v1/content.pb.js +470 -0
  43. package/dist/weaveapi/content/v1/index.d.ts +1 -0
  44. package/dist/weaveapi/content/v1/index.js +38 -0
  45. package/dist/weaveapi/gateway/v1/gateway.pb.d.ts +247 -523
  46. package/dist/weaveapi/gateway/v1/gateway.pb.js +1648 -5783
  47. package/dist/weaveapi/gateway/v1/index.d.ts +0 -1
  48. package/dist/weaveapi/gateway/v1/index.js +1 -2
  49. package/dist/weaveapi/index.d.ts +5 -5
  50. package/dist/weaveapi/index.js +6 -6
  51. package/dist/weaveapi/inference/v1/generate.pb.d.ts +120 -0
  52. package/dist/weaveapi/inference/v1/generate.pb.js +1159 -0
  53. package/dist/weaveapi/inference/v1/index.d.ts +4 -0
  54. package/dist/weaveapi/{provider → inference}/v1/index.js +4 -2
  55. package/dist/weaveapi/{model → inference}/v1/model.pb.d.ts +54 -15
  56. package/dist/weaveapi/{model → inference}/v1/model.pb.js +573 -116
  57. package/dist/weaveapi/{model/v1/service.pb.d.ts → inference/v1/model_route.pb.d.ts} +184 -110
  58. package/dist/weaveapi/{model/v1/service.pb.js → inference/v1/model_route.pb.js} +2240 -1061
  59. package/dist/weaveapi/inference/v1/provider.pb.d.ts +264 -0
  60. package/dist/weaveapi/inference/v1/provider.pb.js +2590 -0
  61. package/dist/weaveapi/ingestion/v1/index.d.ts +0 -2
  62. package/dist/weaveapi/ingestion/v1/index.js +1 -3
  63. package/dist/weaveapi/ingestion/v1/ingestion.pb.d.ts +543 -0
  64. package/dist/weaveapi/ingestion/v1/ingestion.pb.js +5550 -16
  65. package/dist/weaveapi/initialization/v1/index.d.ts +1 -0
  66. package/dist/weaveapi/initialization/v1/index.js +38 -0
  67. package/dist/weaveapi/initialization/v1/initialization.pb.d.ts +99 -0
  68. package/dist/weaveapi/initialization/v1/initialization.pb.js +510 -0
  69. package/dist/weaveapi/organization/v1/index.d.ts +0 -1
  70. package/dist/weaveapi/organization/v1/index.js +1 -2
  71. package/dist/weaveapi/organization/v1/organization.pb.d.ts +241 -0
  72. package/dist/weaveapi/organization/v1/organization.pb.js +1838 -1
  73. package/dist/weaveapi/reporting/index.d.ts +1 -0
  74. package/dist/weaveapi/reporting/index.js +38 -0
  75. package/dist/weaveapi/{task → reporting}/v1/index.d.ts +1 -1
  76. package/dist/weaveapi/{model → reporting}/v1/index.js +2 -2
  77. package/dist/weaveapi/reporting/v1/service.pb.d.ts +38 -0
  78. package/dist/weaveapi/reporting/v1/service.pb.js +167 -0
  79. package/dist/weaveapi/reporting/v1/usage.pb.d.ts +150 -0
  80. package/dist/weaveapi/reporting/v1/usage.pb.js +1837 -0
  81. package/dist/weaveapi/retrieval/v1/index.d.ts +0 -1
  82. package/dist/weaveapi/retrieval/v1/index.js +1 -2
  83. package/dist/weaveapi/retrieval/v1/retrieval.pb.d.ts +96 -2
  84. package/dist/weaveapi/retrieval/v1/retrieval.pb.js +690 -10
  85. package/dist/weaveapi/tool/index.d.ts +1 -0
  86. package/dist/weaveapi/tool/index.js +38 -0
  87. package/dist/weaveapi/tool/v1/index.d.ts +1 -0
  88. package/dist/weaveapi/tool/v1/index.js +38 -0
  89. package/dist/weaveapi/tool/v1/tool.pb.d.ts +370 -0
  90. package/dist/weaveapi/tool/v1/tool.pb.js +2717 -0
  91. package/dist/weaveapi/workflow/v1/index.d.ts +1 -1
  92. package/dist/weaveapi/workflow/v1/index.js +2 -2
  93. package/dist/weaveapi/workflow/v1/run.pb.d.ts +244 -0
  94. package/dist/weaveapi/workflow/v1/run.pb.js +2084 -0
  95. package/dist/weaveapi/workflow/v1/workflow.pb.d.ts +122 -275
  96. package/dist/weaveapi/workflow/v1/workflow.pb.js +790 -3261
  97. package/dist/weavesql/weavedb/agent_sql.d.ts +173 -477
  98. package/dist/weavesql/weavedb/agent_sql.js +281 -758
  99. package/dist/weavesql/weavedb/authorization_outbox_sql.d.ts +261 -0
  100. package/dist/weavesql/weavedb/authorization_outbox_sql.js +445 -0
  101. package/dist/weavesql/weavedb/chat_sql.d.ts +103 -345
  102. package/dist/weavesql/weavedb/chat_sql.js +312 -597
  103. package/dist/weavesql/weavedb/document_consolidation_sql.d.ts +217 -0
  104. package/dist/weavesql/weavedb/document_consolidation_sql.js +486 -0
  105. package/dist/weavesql/weavedb/document_ingestion_sql.d.ts +1433 -0
  106. package/dist/weavesql/weavedb/document_ingestion_sql.js +3488 -0
  107. package/dist/weavesql/weavedb/document_retrieval_sql.d.ts +314 -0
  108. package/dist/weavesql/weavedb/document_retrieval_sql.js +965 -0
  109. package/dist/weavesql/weavedb/document_table_sql.d.ts +6 -6
  110. package/dist/weavesql/weavedb/document_table_sql.js +12 -12
  111. package/dist/weavesql/weavedb/identity_sql.d.ts +1021 -0
  112. package/dist/weavesql/weavedb/identity_sql.js +1818 -0
  113. package/dist/weavesql/weavedb/inference_model_route_sql.d.ts +167 -0
  114. package/dist/weavesql/weavedb/inference_model_route_sql.js +483 -0
  115. package/dist/weavesql/weavedb/inference_provider_sql.d.ts +125 -0
  116. package/dist/weavesql/weavedb/inference_provider_sql.js +228 -0
  117. package/dist/weavesql/weavedb/initialization_sql.d.ts +15 -0
  118. package/dist/weavesql/weavedb/initialization_sql.js +33 -0
  119. package/dist/weavesql/weavedb/model_catalog_sql.d.ts +6 -6
  120. package/dist/weavesql/weavedb/model_catalog_sql.js +6 -6
  121. package/dist/weavesql/weavedb/organization_sql.d.ts +24 -102
  122. package/dist/weavesql/weavedb/organization_sql.js +56 -262
  123. package/dist/weavesql/weavedb/tool_sql.d.ts +209 -0
  124. package/dist/weavesql/weavedb/tool_sql.js +407 -0
  125. package/dist/weavesql/weavedb/usage_sql.d.ts +356 -0
  126. package/dist/weavesql/weavedb/usage_sql.js +568 -0
  127. package/dist/weavesql/weavedb/workflow_sql.d.ts +159 -546
  128. package/dist/weavesql/weavedb/workflow_sql.js +281 -909
  129. package/package.json +1 -1
  130. package/dist/weaveapi/agent/v1/service.pb.d.ts +0 -397
  131. package/dist/weaveapi/agent/v1/service.pb.js +0 -3152
  132. package/dist/weaveapi/atc/v1/event.pb.d.ts +0 -134
  133. package/dist/weaveapi/atc/v1/event.pb.js +0 -1124
  134. package/dist/weaveapi/atc/v1/index.d.ts +0 -5
  135. package/dist/weaveapi/atc/v1/index.js +0 -42
  136. package/dist/weaveapi/atc/v1/message.pb.js +0 -287
  137. package/dist/weaveapi/atc/v1/process.pb.d.ts +0 -91
  138. package/dist/weaveapi/atc/v1/process.pb.js +0 -658
  139. package/dist/weaveapi/atc/v1/run.pb.d.ts +0 -71
  140. package/dist/weaveapi/atc/v1/run.pb.js +0 -525
  141. package/dist/weaveapi/atc/v1/service.pb.d.ts +0 -472
  142. package/dist/weaveapi/atc/v1/service.pb.js +0 -3093
  143. package/dist/weaveapi/chat/v1/service.pb.d.ts +0 -345
  144. package/dist/weaveapi/chat/v1/service.pb.js +0 -2545
  145. package/dist/weaveapi/consolidation/v1/service.pb.d.ts +0 -93
  146. package/dist/weaveapi/consolidation/v1/service.pb.js +0 -427
  147. package/dist/weaveapi/gateway/v1/service.pb.d.ts +0 -235
  148. package/dist/weaveapi/gateway/v1/service.pb.js +0 -651
  149. package/dist/weaveapi/ingestion/v1/service.pb.d.ts +0 -420
  150. package/dist/weaveapi/ingestion/v1/service.pb.js +0 -2311
  151. package/dist/weaveapi/ingestion/v1/table.pb.d.ts +0 -121
  152. package/dist/weaveapi/ingestion/v1/table.pb.js +0 -1478
  153. package/dist/weaveapi/model/v1/index.d.ts +0 -2
  154. package/dist/weaveapi/organization/v1/service.pb.d.ts +0 -261
  155. package/dist/weaveapi/organization/v1/service.pb.js +0 -1561
  156. package/dist/weaveapi/project/v1/index.d.ts +0 -2
  157. package/dist/weaveapi/project/v1/index.js +0 -39
  158. package/dist/weaveapi/project/v1/project.pb.d.ts +0 -105
  159. package/dist/weaveapi/project/v1/project.pb.js +0 -1004
  160. package/dist/weaveapi/project/v1/service.pb.d.ts +0 -247
  161. package/dist/weaveapi/project/v1/service.pb.js +0 -1994
  162. package/dist/weaveapi/provider/v1/index.d.ts +0 -2
  163. package/dist/weaveapi/provider/v1/provider.pb.d.ts +0 -137
  164. package/dist/weaveapi/provider/v1/provider.pb.js +0 -1000
  165. package/dist/weaveapi/provider/v1/service.pb.d.ts +0 -223
  166. package/dist/weaveapi/provider/v1/service.pb.js +0 -1888
  167. package/dist/weaveapi/retrieval/v1/service.pb.d.ts +0 -93
  168. package/dist/weaveapi/retrieval/v1/service.pb.js +0 -385
  169. package/dist/weaveapi/task/v1/index.js +0 -39
  170. package/dist/weaveapi/task/v1/service.pb.d.ts +0 -172
  171. package/dist/weaveapi/task/v1/service.pb.js +0 -1436
  172. package/dist/weaveapi/task/v1/task.pb.d.ts +0 -85
  173. package/dist/weaveapi/task/v1/task.pb.js +0 -802
  174. package/dist/weaveapi/workflow/v1/service.pb.d.ts +0 -392
  175. package/dist/weaveapi/workflow/v1/service.pb.js +0 -3146
  176. /package/dist/{weaveapi/atc → weave/authz}/index.d.ts +0 -0
  177. /package/dist/{weaveapi/atc → weave/authz}/index.js +0 -0
  178. /package/dist/{weaveapi/model → weave/filter}/index.d.ts +0 -0
  179. /package/dist/{weaveapi/model → weave/filter}/index.js +0 -0
  180. /package/dist/weaveapi/{project → content}/index.d.ts +0 -0
  181. /package/dist/weaveapi/{project → content}/index.js +0 -0
  182. /package/dist/weaveapi/{provider → inference}/index.d.ts +0 -0
  183. /package/dist/weaveapi/{provider → inference}/index.js +0 -0
  184. /package/dist/weaveapi/{task → initialization}/index.d.ts +0 -0
  185. /package/dist/weaveapi/{task → initialization}/index.js +0 -0
@@ -0,0 +1,3488 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ 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.deleteDocumentThreadByOrganizationAndIDQuery = exports.editDocumentThreadQuery = exports.editPendingThreadQuery = exports.updateThreadClassificationQuery = exports.promoteThreadNextEmbeddingsByOrganizationQuery = exports.setThreadNextEmbeddingQuery = exports.updateThreadEmbeddingQuery = exports.listPendingThreadsByOrganizationQuery = exports.listPendingThreadsByRunQuery = exports.listAccessibleThreadsByDocumentQuery = exports.listThreadsByDocumentQuery = exports.listThreadsByIngestionRunQuery = exports.getThreadByOrganizationAndIDQuery = exports.createThreadQuery = exports.deleteDocumentByOrganizationAndIDQuery = exports.listDocumentTagsByDocumentIDQuery = exports.createDocumentTagQuery = exports.deleteDocumentTagByDocumentIDAndTagQuery = exports.deleteDocumentTagsByDocumentIDQuery = exports.listIngestionHistoryQuery = exports.listIngestionRunsByDocumentQuery = exports.listIngestionRunsByOrganizationQuery = exports.updateIngestionRunStateQuery = exports.getIngestionRunByOrganizationAndIDQuery = exports.createIngestionRunQuery = exports.getDatabaseConfigQuery = exports.getAccessibleDocumentSummaryQuery = exports.listAccessibleDocumentSummariesQuery = exports.listDocumentsByOrganizationQuery = exports.setDocumentStatusQuery = exports.setDocumentParseResultQuery = exports.getDocumentByOrganizationAndFingerprintQuery = exports.getDocumentByOrganizationAndIDQuery = exports.createDocumentQuery = void 0;
4
+ exports.deactivateFacetConfigQuery = exports.upsertFacetConfigQuery = exports.listActiveFacetConfigsQuery = exports.listFacetConfigsQuery = exports.deleteThreadFacetsByThreadQuery = exports.upsertThreadFacetQuery = exports.listThreadFacetsByThreadIDsQuery = exports.syncOrganizationIngestionReasoningDefaultsQuery = exports.upsertOrganizationIngestionStageSettingsQuery = exports.upsertOrganizationIngestionSettingsQuery = exports.getOrganizationIngestionSettingsQuery = exports.ensureOrganizationIngestionSettingsQuery = exports.listEntitiesByThreadQuery = 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.listAccessibleDocumentSummaries = listAccessibleDocumentSummaries;
12
+ exports.getAccessibleDocumentSummary = getAccessibleDocumentSummary;
13
+ exports.getDatabaseConfig = getDatabaseConfig;
14
+ exports.createIngestionRun = createIngestionRun;
15
+ exports.getIngestionRunByOrganizationAndID = getIngestionRunByOrganizationAndID;
16
+ exports.updateIngestionRunState = updateIngestionRunState;
17
+ exports.listIngestionRunsByOrganization = listIngestionRunsByOrganization;
18
+ exports.listIngestionRunsByDocument = listIngestionRunsByDocument;
19
+ exports.listIngestionHistory = listIngestionHistory;
20
+ exports.deleteDocumentTagsByDocumentID = deleteDocumentTagsByDocumentID;
21
+ exports.createDocumentTag = createDocumentTag;
22
+ exports.listDocumentTagsByDocumentID = listDocumentTagsByDocumentID;
23
+ exports.createThread = createThread;
24
+ exports.getThreadByOrganizationAndID = getThreadByOrganizationAndID;
25
+ exports.listThreadsByIngestionRun = listThreadsByIngestionRun;
26
+ exports.listThreadsByDocument = listThreadsByDocument;
27
+ exports.listAccessibleThreadsByDocument = listAccessibleThreadsByDocument;
28
+ exports.listPendingThreadsByRun = listPendingThreadsByRun;
29
+ exports.listPendingThreadsByOrganization = listPendingThreadsByOrganization;
30
+ exports.updateThreadEmbedding = updateThreadEmbedding;
31
+ exports.setThreadNextEmbedding = setThreadNextEmbedding;
32
+ exports.updateThreadClassification = updateThreadClassification;
33
+ exports.editPendingThread = editPendingThread;
34
+ exports.editDocumentThread = editDocumentThread;
35
+ exports.approveThread = approveThread;
36
+ exports.bulkApproveThreads = bulkApproveThreads;
37
+ exports.rejectThread = rejectThread;
38
+ exports.bulkRejectThreads = bulkRejectThreads;
39
+ exports.listThreadsBySupersedes = listThreadsBySupersedes;
40
+ exports.listThreadsByIDs = listThreadsByIDs;
41
+ exports.getIngestionRunThreadCounts = getIngestionRunThreadCounts;
42
+ exports.deleteThreadReconciliationByPendingThread = deleteThreadReconciliationByPendingThread;
43
+ exports.upsertThreadReconciliation = upsertThreadReconciliation;
44
+ exports.listThreadReconciliationByPendingThread = listThreadReconciliationByPendingThread;
45
+ exports.createThreadActivity = createThreadActivity;
46
+ exports.listThreadActivityByThread = listThreadActivityByThread;
47
+ exports.upsertEntity = upsertEntity;
48
+ exports.upsertThreadEntity = upsertThreadEntity;
49
+ exports.upsertEntityRelationship = upsertEntityRelationship;
50
+ exports.listEntitiesByThread = listEntitiesByThread;
51
+ exports.ensureOrganizationIngestionSettings = ensureOrganizationIngestionSettings;
52
+ exports.getOrganizationIngestionSettings = getOrganizationIngestionSettings;
53
+ exports.upsertOrganizationIngestionSettings = upsertOrganizationIngestionSettings;
54
+ exports.upsertOrganizationIngestionStageSettings = upsertOrganizationIngestionStageSettings;
55
+ exports.syncOrganizationIngestionReasoningDefaults = syncOrganizationIngestionReasoningDefaults;
56
+ exports.listThreadFacetsByThreadIDs = listThreadFacetsByThreadIDs;
57
+ exports.upsertThreadFacet = upsertThreadFacet;
58
+ exports.listFacetConfigs = listFacetConfigs;
59
+ exports.listActiveFacetConfigs = listActiveFacetConfigs;
60
+ exports.upsertFacetConfig = upsertFacetConfig;
61
+ exports.deactivateFacetConfig = deactivateFacetConfig;
62
+ exports.createDocumentQuery = `-- name: CreateDocument :one
63
+ INSERT INTO weave.documents (
64
+ id,
65
+ organization_id,
66
+ filename,
67
+ mime_type,
68
+ fingerprint,
69
+ storage_ref,
70
+ status,
71
+ scope,
72
+ uploaded_by_user_id,
73
+ metadata
74
+ ) VALUES (
75
+ $1,
76
+ $2,
77
+ $3,
78
+ $4,
79
+ $5,
80
+ $6,
81
+ $7,
82
+ $8,
83
+ $9,
84
+ $10
85
+ )
86
+ RETURNING
87
+ id,
88
+ organization_id,
89
+ filename,
90
+ mime_type,
91
+ parser_name,
92
+ fingerprint,
93
+ storage_ref,
94
+ status,
95
+ scope,
96
+ uploaded_by_user_id,
97
+ uploaded_at,
98
+ updated_at,
99
+ page_count,
100
+ char_count,
101
+ metadata`;
102
+ async function createDocument(client, args) {
103
+ const result = await client.query({
104
+ text: exports.createDocumentQuery,
105
+ values: [args.id, args.organizationId, args.filename, args.mimeType, args.fingerprint, args.storageRef, args.status, args.scope, args.uploadedByUserId, args.metadata],
106
+ rowMode: "array"
107
+ });
108
+ if (result.rows.length !== 1) {
109
+ return null;
110
+ }
111
+ const row = result.rows[0];
112
+ return {
113
+ id: row[0],
114
+ organizationId: row[1],
115
+ filename: row[2],
116
+ mimeType: row[3],
117
+ parserName: row[4],
118
+ fingerprint: row[5],
119
+ storageRef: row[6],
120
+ status: row[7],
121
+ scope: row[8],
122
+ uploadedByUserId: row[9],
123
+ uploadedAt: row[10],
124
+ updatedAt: row[11],
125
+ pageCount: row[12],
126
+ charCount: row[13],
127
+ metadata: row[14]
128
+ };
129
+ }
130
+ exports.getDocumentByOrganizationAndIDQuery = `-- name: GetDocumentByOrganizationAndID :one
131
+ SELECT
132
+ id,
133
+ organization_id,
134
+ filename,
135
+ mime_type,
136
+ parser_name,
137
+ fingerprint,
138
+ storage_ref,
139
+ status,
140
+ scope,
141
+ uploaded_by_user_id,
142
+ uploaded_at,
143
+ updated_at,
144
+ page_count,
145
+ char_count,
146
+ metadata
147
+ FROM weave.documents
148
+ WHERE organization_id = $1
149
+ AND id = $2`;
150
+ async function getDocumentByOrganizationAndID(client, args) {
151
+ const result = await client.query({
152
+ text: exports.getDocumentByOrganizationAndIDQuery,
153
+ values: [args.organizationId, args.id],
154
+ rowMode: "array"
155
+ });
156
+ if (result.rows.length !== 1) {
157
+ return null;
158
+ }
159
+ const row = result.rows[0];
160
+ return {
161
+ id: row[0],
162
+ organizationId: row[1],
163
+ filename: row[2],
164
+ mimeType: row[3],
165
+ parserName: row[4],
166
+ fingerprint: row[5],
167
+ storageRef: row[6],
168
+ status: row[7],
169
+ scope: row[8],
170
+ uploadedByUserId: row[9],
171
+ uploadedAt: row[10],
172
+ updatedAt: row[11],
173
+ pageCount: row[12],
174
+ charCount: row[13],
175
+ metadata: row[14]
176
+ };
177
+ }
178
+ exports.getDocumentByOrganizationAndFingerprintQuery = `-- name: GetDocumentByOrganizationAndFingerprint :one
179
+ SELECT
180
+ id,
181
+ organization_id,
182
+ filename,
183
+ mime_type,
184
+ parser_name,
185
+ fingerprint,
186
+ storage_ref,
187
+ status,
188
+ scope,
189
+ uploaded_by_user_id,
190
+ uploaded_at,
191
+ updated_at,
192
+ page_count,
193
+ char_count,
194
+ metadata
195
+ FROM weave.documents
196
+ WHERE organization_id = $1
197
+ AND fingerprint = $2`;
198
+ async function getDocumentByOrganizationAndFingerprint(client, args) {
199
+ const result = await client.query({
200
+ text: exports.getDocumentByOrganizationAndFingerprintQuery,
201
+ values: [args.organizationId, args.fingerprint],
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
+ filename: row[2],
212
+ mimeType: row[3],
213
+ parserName: row[4],
214
+ fingerprint: row[5],
215
+ storageRef: row[6],
216
+ status: row[7],
217
+ scope: row[8],
218
+ uploadedByUserId: row[9],
219
+ uploadedAt: row[10],
220
+ updatedAt: row[11],
221
+ pageCount: row[12],
222
+ charCount: row[13],
223
+ metadata: row[14]
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
+ uploaded_by_user_id,
247
+ uploaded_at,
248
+ updated_at,
249
+ page_count,
250
+ char_count,
251
+ metadata`;
252
+ async function setDocumentParseResult(client, args) {
253
+ const result = await client.query({
254
+ text: exports.setDocumentParseResultQuery,
255
+ values: [args.parserName, args.status, args.pageCount, args.charCount, args.organizationId, args.id],
256
+ rowMode: "array"
257
+ });
258
+ if (result.rows.length !== 1) {
259
+ return null;
260
+ }
261
+ const row = result.rows[0];
262
+ return {
263
+ id: row[0],
264
+ organizationId: row[1],
265
+ filename: row[2],
266
+ mimeType: row[3],
267
+ parserName: row[4],
268
+ fingerprint: row[5],
269
+ storageRef: row[6],
270
+ status: row[7],
271
+ scope: row[8],
272
+ uploadedByUserId: row[9],
273
+ uploadedAt: row[10],
274
+ updatedAt: row[11],
275
+ pageCount: row[12],
276
+ charCount: row[13],
277
+ metadata: row[14]
278
+ };
279
+ }
280
+ exports.setDocumentStatusQuery = `-- name: SetDocumentStatus :one
281
+ UPDATE weave.documents
282
+ SET
283
+ status = $1,
284
+ updated_at = now()
285
+ WHERE organization_id = $2
286
+ AND id = $3
287
+ RETURNING
288
+ id,
289
+ organization_id,
290
+ filename,
291
+ mime_type,
292
+ parser_name,
293
+ fingerprint,
294
+ storage_ref,
295
+ status,
296
+ scope,
297
+ uploaded_by_user_id,
298
+ uploaded_at,
299
+ updated_at,
300
+ page_count,
301
+ char_count,
302
+ metadata`;
303
+ async function setDocumentStatus(client, args) {
304
+ const result = await client.query({
305
+ text: exports.setDocumentStatusQuery,
306
+ values: [args.status, args.organizationId, args.id],
307
+ rowMode: "array"
308
+ });
309
+ if (result.rows.length !== 1) {
310
+ return null;
311
+ }
312
+ const row = result.rows[0];
313
+ return {
314
+ id: row[0],
315
+ organizationId: row[1],
316
+ filename: row[2],
317
+ mimeType: row[3],
318
+ parserName: row[4],
319
+ fingerprint: row[5],
320
+ storageRef: row[6],
321
+ status: row[7],
322
+ scope: row[8],
323
+ uploadedByUserId: row[9],
324
+ uploadedAt: row[10],
325
+ updatedAt: row[11],
326
+ pageCount: row[12],
327
+ charCount: row[13],
328
+ metadata: row[14]
329
+ };
330
+ }
331
+ exports.listDocumentsByOrganizationQuery = `-- name: ListDocumentsByOrganization :many
332
+ SELECT
333
+ id,
334
+ organization_id,
335
+ filename,
336
+ mime_type,
337
+ parser_name,
338
+ fingerprint,
339
+ storage_ref,
340
+ status,
341
+ scope,
342
+ uploaded_by_user_id,
343
+ uploaded_at,
344
+ updated_at,
345
+ page_count,
346
+ char_count,
347
+ metadata
348
+ FROM weave.documents
349
+ WHERE organization_id = $1
350
+ ORDER BY uploaded_at DESC
351
+ LIMIT $3 OFFSET $2`;
352
+ async function listDocumentsByOrganization(client, args) {
353
+ const result = await client.query({
354
+ text: exports.listDocumentsByOrganizationQuery,
355
+ values: [args.organizationId, args.pageOffset, args.pageSize],
356
+ rowMode: "array"
357
+ });
358
+ return result.rows.map(row => {
359
+ return {
360
+ id: row[0],
361
+ organizationId: row[1],
362
+ filename: row[2],
363
+ mimeType: row[3],
364
+ parserName: row[4],
365
+ fingerprint: row[5],
366
+ storageRef: row[6],
367
+ status: row[7],
368
+ scope: row[8],
369
+ uploadedByUserId: row[9],
370
+ uploadedAt: row[10],
371
+ updatedAt: row[11],
372
+ pageCount: row[12],
373
+ charCount: row[13],
374
+ metadata: row[14]
375
+ };
376
+ });
377
+ }
378
+ exports.listAccessibleDocumentSummariesQuery = `-- name: ListAccessibleDocumentSummaries :many
379
+ WITH latest_runs AS (
380
+ SELECT DISTINCT ON (r.document_id)
381
+ r.document_id,
382
+ r.id AS latest_ingestion_run_id,
383
+ r.status AS latest_ingestion_status
384
+ FROM weave.ingestion_runs r
385
+ WHERE r.organization_id = $1
386
+ ORDER BY r.document_id, r.started_at DESC
387
+ ),
388
+ thread_counts AS (
389
+ SELECT
390
+ t.document_id,
391
+ COUNT(*)::int AS thread_count,
392
+ COUNT(*) FILTER (
393
+ WHERE t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
394
+ )::int AS pending_count,
395
+ COUNT(*) FILTER (
396
+ WHERE t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
397
+ )::int AS approved_count,
398
+ COUNT(*) FILTER (
399
+ WHERE t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_REJECTED'
400
+ )::int AS rejected_count
401
+ FROM weave.threads t
402
+ WHERE t.organization_id = $1
403
+ GROUP BY t.document_id
404
+ ),
405
+ tag_data AS (
406
+ SELECT
407
+ dt.document_id,
408
+ array_agg(dt.tag ORDER BY dt.tag ASC) AS tags,
409
+ array_agg(dt.confidence ORDER BY dt.tag ASC) AS tag_confidences
410
+ FROM weave.document_tags dt
411
+ JOIN weave.documents d ON d.id = dt.document_id
412
+ WHERE d.organization_id = $1
413
+ GROUP BY dt.document_id
414
+ )
415
+ SELECT
416
+ d.id,
417
+ d.organization_id,
418
+ d.filename,
419
+ d.mime_type,
420
+ d.parser_name,
421
+ d.status,
422
+ d.scope,
423
+ COALESCE(tag_data.tags, '{}'::text[]) AS tags,
424
+ COALESCE(tag_data.tag_confidences, '{}'::real[]) AS tag_confidences,
425
+ d.uploaded_by_user_id,
426
+ d.uploaded_at,
427
+ d.updated_at,
428
+ d.page_count,
429
+ d.char_count,
430
+ d.metadata,
431
+ COALESCE(latest_runs.latest_ingestion_run_id, '00000000-0000-0000-0000-000000000000'::uuid) AS latest_ingestion_run_id,
432
+ COALESCE(latest_runs.latest_ingestion_status, 'INGESTION_RUN_STATUS_UNSPECIFIED') AS latest_ingestion_status,
433
+ COALESCE(thread_counts.thread_count, 0)::int AS thread_count,
434
+ COALESCE(thread_counts.pending_count, 0)::int AS pending_count,
435
+ COALESCE(thread_counts.approved_count, 0)::int AS approved_count,
436
+ COALESCE(thread_counts.rejected_count, 0)::int AS rejected_count
437
+ FROM weave.documents d
438
+ LEFT JOIN latest_runs ON latest_runs.document_id = d.id
439
+ LEFT JOIN thread_counts ON thread_counts.document_id = d.id
440
+ LEFT JOIN tag_data ON tag_data.document_id = d.id
441
+ WHERE d.organization_id = $1
442
+ AND (
443
+ d.scope = 'DOCUMENT_SCOPE_ORG'
444
+ OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
445
+ AND d.uploaded_by_user_id = $2)
446
+ OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
447
+ AND (
448
+ EXISTS (
449
+ SELECT 1
450
+ FROM weave.document_restricted_groups drg
451
+ JOIN weave.restricted_group_user_memberships rgum
452
+ ON rgum.organization_id = drg.organization_id
453
+ AND rgum.restricted_group_id = drg.restricted_group_id
454
+ AND rgum.user_id = $2
455
+ WHERE drg.organization_id = d.organization_id
456
+ AND drg.document_id = d.id
457
+ )
458
+ OR EXISTS (
459
+ SELECT 1
460
+ FROM weave.document_restricted_groups drg
461
+ JOIN weave.restricted_group_team_memberships rgtm
462
+ ON rgtm.organization_id = drg.organization_id
463
+ AND rgtm.restricted_group_id = drg.restricted_group_id
464
+ JOIN weave.organization_team_membership otm
465
+ ON otm.organization_id = rgtm.organization_id
466
+ AND otm.organization_team_id = rgtm.org_team_id
467
+ AND otm.user_id = $2
468
+ WHERE drg.organization_id = d.organization_id
469
+ AND drg.document_id = d.id
470
+ )))
471
+ )
472
+ AND (cardinality($3::text[]) = 0 OR d.status = ANY($3::text[]))
473
+ AND (cardinality($4::text[]) = 0 OR latest_runs.latest_ingestion_status = ANY($4::text[]))
474
+ AND (cardinality($5::text[]) = 0 OR d.scope = ANY($5::text[]))
475
+ AND (cardinality($6::text[]) = 0 OR tag_data.tags && $6::text[])
476
+ AND (
477
+ cardinality($7::uuid[]) = 0
478
+ OR EXISTS (
479
+ SELECT 1
480
+ FROM weave.document_restricted_groups drg
481
+ WHERE drg.organization_id = d.organization_id
482
+ AND drg.document_id = d.id
483
+ AND drg.restricted_group_id = ANY($7::uuid[])
484
+ )
485
+ )
486
+ AND ($8::timestamptz IS NULL OR d.uploaded_at >= $8::timestamptz)
487
+ AND ($9::timestamptz IS NULL OR d.uploaded_at <= $9::timestamptz)
488
+ ORDER BY d.uploaded_at DESC, d.id DESC
489
+ LIMIT $11 OFFSET $10`;
490
+ async function listAccessibleDocumentSummaries(client, args) {
491
+ const result = await client.query({
492
+ text: exports.listAccessibleDocumentSummariesQuery,
493
+ values: [args.organizationId, args.actingUserId, args.documentStatuses, args.ingestionStatuses, args.scopes, args.tags, args.restrictedGroupIds, args.uploadedAfter, args.uploadedBefore, args.pageOffset, args.pageSize],
494
+ rowMode: "array"
495
+ });
496
+ return result.rows.map(row => {
497
+ return {
498
+ id: row[0],
499
+ organizationId: row[1],
500
+ filename: row[2],
501
+ mimeType: row[3],
502
+ parserName: row[4],
503
+ status: row[5],
504
+ scope: row[6],
505
+ tags: row[7],
506
+ tagConfidences: row[8],
507
+ uploadedByUserId: row[9],
508
+ uploadedAt: row[10],
509
+ updatedAt: row[11],
510
+ pageCount: row[12],
511
+ charCount: row[13],
512
+ metadata: row[14],
513
+ latestIngestionRunId: row[15],
514
+ latestIngestionStatus: row[16],
515
+ threadCount: row[17],
516
+ pendingCount: row[18],
517
+ approvedCount: row[19],
518
+ rejectedCount: row[20]
519
+ };
520
+ });
521
+ }
522
+ exports.getAccessibleDocumentSummaryQuery = `-- name: GetAccessibleDocumentSummary :one
523
+ WITH latest_runs AS (
524
+ SELECT DISTINCT ON (r.document_id)
525
+ r.document_id,
526
+ r.id AS latest_ingestion_run_id,
527
+ r.status AS latest_ingestion_status
528
+ FROM weave.ingestion_runs r
529
+ WHERE r.organization_id = $1
530
+ AND r.document_id = $2
531
+ ORDER BY r.document_id, r.started_at DESC
532
+ ),
533
+ thread_counts AS (
534
+ SELECT
535
+ t.document_id,
536
+ COUNT(*)::int AS thread_count,
537
+ COUNT(*) FILTER (
538
+ WHERE t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
539
+ )::int AS pending_count,
540
+ COUNT(*) FILTER (
541
+ WHERE t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
542
+ )::int AS approved_count,
543
+ COUNT(*) FILTER (
544
+ WHERE t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_REJECTED'
545
+ )::int AS rejected_count
546
+ FROM weave.threads t
547
+ WHERE t.organization_id = $1
548
+ AND t.document_id = $2
549
+ GROUP BY t.document_id
550
+ ),
551
+ tag_data AS (
552
+ SELECT
553
+ dt.document_id,
554
+ array_agg(dt.tag ORDER BY dt.tag ASC) AS tags,
555
+ array_agg(dt.confidence ORDER BY dt.tag ASC) AS tag_confidences
556
+ FROM weave.document_tags dt
557
+ JOIN weave.documents d ON d.id = dt.document_id
558
+ WHERE d.organization_id = $1
559
+ AND dt.document_id = $2
560
+ GROUP BY dt.document_id
561
+ )
562
+ SELECT
563
+ d.id,
564
+ d.organization_id,
565
+ d.filename,
566
+ d.mime_type,
567
+ d.parser_name,
568
+ d.status,
569
+ d.scope,
570
+ COALESCE(tag_data.tags, '{}'::text[]) AS tags,
571
+ COALESCE(tag_data.tag_confidences, '{}'::real[]) AS tag_confidences,
572
+ d.uploaded_by_user_id,
573
+ d.uploaded_at,
574
+ d.updated_at,
575
+ d.page_count,
576
+ d.char_count,
577
+ d.metadata,
578
+ COALESCE(latest_runs.latest_ingestion_run_id, '00000000-0000-0000-0000-000000000000'::uuid) AS latest_ingestion_run_id,
579
+ COALESCE(latest_runs.latest_ingestion_status, 'INGESTION_RUN_STATUS_UNSPECIFIED') AS latest_ingestion_status,
580
+ COALESCE(thread_counts.thread_count, 0)::int AS thread_count,
581
+ COALESCE(thread_counts.pending_count, 0)::int AS pending_count,
582
+ COALESCE(thread_counts.approved_count, 0)::int AS approved_count,
583
+ COALESCE(thread_counts.rejected_count, 0)::int AS rejected_count
584
+ FROM weave.documents d
585
+ LEFT JOIN latest_runs ON latest_runs.document_id = d.id
586
+ LEFT JOIN thread_counts ON thread_counts.document_id = d.id
587
+ LEFT JOIN tag_data ON tag_data.document_id = d.id
588
+ WHERE d.organization_id = $1
589
+ AND d.id = $2
590
+ AND (
591
+ d.scope = 'DOCUMENT_SCOPE_ORG'
592
+ OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
593
+ AND d.uploaded_by_user_id = $3)
594
+ OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
595
+ AND (
596
+ EXISTS (
597
+ SELECT 1
598
+ FROM weave.document_restricted_groups drg
599
+ JOIN weave.restricted_group_user_memberships rgum
600
+ ON rgum.organization_id = drg.organization_id
601
+ AND rgum.restricted_group_id = drg.restricted_group_id
602
+ AND rgum.user_id = $3
603
+ WHERE drg.organization_id = d.organization_id
604
+ AND drg.document_id = d.id
605
+ )
606
+ OR EXISTS (
607
+ SELECT 1
608
+ FROM weave.document_restricted_groups drg
609
+ JOIN weave.restricted_group_team_memberships rgtm
610
+ ON rgtm.organization_id = drg.organization_id
611
+ AND rgtm.restricted_group_id = drg.restricted_group_id
612
+ JOIN weave.organization_team_membership otm
613
+ ON otm.organization_id = rgtm.organization_id
614
+ AND otm.organization_team_id = rgtm.org_team_id
615
+ AND otm.user_id = $3
616
+ WHERE drg.organization_id = d.organization_id
617
+ AND drg.document_id = d.id
618
+ )))
619
+ )`;
620
+ async function getAccessibleDocumentSummary(client, args) {
621
+ const result = await client.query({
622
+ text: exports.getAccessibleDocumentSummaryQuery,
623
+ values: [args.organizationId, args.documentId, args.actingUserId],
624
+ rowMode: "array"
625
+ });
626
+ if (result.rows.length !== 1) {
627
+ return null;
628
+ }
629
+ const row = result.rows[0];
630
+ return {
631
+ id: row[0],
632
+ organizationId: row[1],
633
+ filename: row[2],
634
+ mimeType: row[3],
635
+ parserName: row[4],
636
+ status: row[5],
637
+ scope: row[6],
638
+ tags: row[7],
639
+ tagConfidences: row[8],
640
+ uploadedByUserId: row[9],
641
+ uploadedAt: row[10],
642
+ updatedAt: row[11],
643
+ pageCount: row[12],
644
+ charCount: row[13],
645
+ metadata: row[14],
646
+ latestIngestionRunId: row[15],
647
+ latestIngestionStatus: row[16],
648
+ threadCount: row[17],
649
+ pendingCount: row[18],
650
+ approvedCount: row[19],
651
+ rejectedCount: row[20]
652
+ };
653
+ }
654
+ exports.getDatabaseConfigQuery = `-- name: GetDatabaseConfig :one
655
+ SELECT
656
+ id,
657
+ embedding_dimension,
658
+ deployment_mode,
659
+ created_at,
660
+ updated_at
661
+ FROM weave.database_config
662
+ WHERE id = 1`;
663
+ async function getDatabaseConfig(client) {
664
+ const result = await client.query({
665
+ text: exports.getDatabaseConfigQuery,
666
+ values: [],
667
+ rowMode: "array"
668
+ });
669
+ if (result.rows.length !== 1) {
670
+ return null;
671
+ }
672
+ const row = result.rows[0];
673
+ return {
674
+ id: row[0],
675
+ embeddingDimension: row[1],
676
+ deploymentMode: row[2],
677
+ createdAt: row[3],
678
+ updatedAt: row[4]
679
+ };
680
+ }
681
+ exports.createIngestionRunQuery = `-- name: CreateIngestionRun :one
682
+ INSERT INTO weave.ingestion_runs (
683
+ id,
684
+ organization_id,
685
+ document_id,
686
+ status,
687
+ reasoning_model_id,
688
+ reasoning_provider_configuration_id,
689
+ embedding_model_id,
690
+ embedding_provider_configuration_id,
691
+ embedding_dimensions,
692
+ stage_model_settings,
693
+ create_thread_loop_count
694
+ ) VALUES (
695
+ $1,
696
+ $2,
697
+ $3,
698
+ $4,
699
+ $5,
700
+ $6,
701
+ $7,
702
+ $8,
703
+ $9,
704
+ $10,
705
+ $11
706
+ )
707
+ RETURNING
708
+ id,
709
+ organization_id,
710
+ document_id,
711
+ status,
712
+ reasoning_model_id,
713
+ reasoning_provider_configuration_id,
714
+ embedding_model_id,
715
+ embedding_provider_configuration_id,
716
+ embedding_dimensions,
717
+ stage_model_settings,
718
+ create_thread_loop_count,
719
+ thread_count,
720
+ pending_count,
721
+ approved_count,
722
+ rejected_count,
723
+ error,
724
+ token_usage,
725
+ started_at,
726
+ completed_at,
727
+ committed_at`;
728
+ async function createIngestionRun(client, args) {
729
+ const result = await client.query({
730
+ text: exports.createIngestionRunQuery,
731
+ values: [args.id, args.organizationId, args.documentId, args.status, args.reasoningModelId, args.reasoningProviderConfigurationId, args.embeddingModelId, args.embeddingProviderConfigurationId, args.embeddingDimensions, args.stageModelSettings, args.createThreadLoopCount],
732
+ rowMode: "array"
733
+ });
734
+ if (result.rows.length !== 1) {
735
+ return null;
736
+ }
737
+ const row = result.rows[0];
738
+ return {
739
+ id: row[0],
740
+ organizationId: row[1],
741
+ documentId: row[2],
742
+ status: row[3],
743
+ reasoningModelId: row[4],
744
+ reasoningProviderConfigurationId: row[5],
745
+ embeddingModelId: row[6],
746
+ embeddingProviderConfigurationId: row[7],
747
+ embeddingDimensions: row[8],
748
+ stageModelSettings: row[9],
749
+ createThreadLoopCount: row[10],
750
+ threadCount: row[11],
751
+ pendingCount: row[12],
752
+ approvedCount: row[13],
753
+ rejectedCount: row[14],
754
+ error: row[15],
755
+ tokenUsage: row[16],
756
+ startedAt: row[17],
757
+ completedAt: row[18],
758
+ committedAt: row[19]
759
+ };
760
+ }
761
+ exports.getIngestionRunByOrganizationAndIDQuery = `-- name: GetIngestionRunByOrganizationAndID :one
762
+ SELECT
763
+ id,
764
+ organization_id,
765
+ document_id,
766
+ status,
767
+ reasoning_model_id,
768
+ reasoning_provider_configuration_id,
769
+ embedding_model_id,
770
+ embedding_provider_configuration_id,
771
+ embedding_dimensions,
772
+ stage_model_settings,
773
+ create_thread_loop_count,
774
+ thread_count,
775
+ pending_count,
776
+ approved_count,
777
+ rejected_count,
778
+ error,
779
+ token_usage,
780
+ started_at,
781
+ completed_at,
782
+ committed_at
783
+ FROM weave.ingestion_runs
784
+ WHERE organization_id = $1
785
+ AND id = $2`;
786
+ async function getIngestionRunByOrganizationAndID(client, args) {
787
+ const result = await client.query({
788
+ text: exports.getIngestionRunByOrganizationAndIDQuery,
789
+ values: [args.organizationId, args.id],
790
+ rowMode: "array"
791
+ });
792
+ if (result.rows.length !== 1) {
793
+ return null;
794
+ }
795
+ const row = result.rows[0];
796
+ return {
797
+ id: row[0],
798
+ organizationId: row[1],
799
+ documentId: row[2],
800
+ status: row[3],
801
+ reasoningModelId: row[4],
802
+ reasoningProviderConfigurationId: row[5],
803
+ embeddingModelId: row[6],
804
+ embeddingProviderConfigurationId: row[7],
805
+ embeddingDimensions: row[8],
806
+ stageModelSettings: row[9],
807
+ createThreadLoopCount: row[10],
808
+ threadCount: row[11],
809
+ pendingCount: row[12],
810
+ approvedCount: row[13],
811
+ rejectedCount: row[14],
812
+ error: row[15],
813
+ tokenUsage: row[16],
814
+ startedAt: row[17],
815
+ completedAt: row[18],
816
+ committedAt: row[19]
817
+ };
818
+ }
819
+ exports.updateIngestionRunStateQuery = `-- name: UpdateIngestionRunState :one
820
+ UPDATE weave.ingestion_runs
821
+ SET
822
+ status = $1,
823
+ thread_count = $2,
824
+ pending_count = $3,
825
+ approved_count = $4,
826
+ rejected_count = $5,
827
+ error = $6,
828
+ token_usage = $7,
829
+ completed_at = $8,
830
+ committed_at = $9
831
+ WHERE id = $10
832
+ AND organization_id = $11
833
+ RETURNING
834
+ id,
835
+ organization_id,
836
+ document_id,
837
+ status,
838
+ reasoning_model_id,
839
+ reasoning_provider_configuration_id,
840
+ embedding_model_id,
841
+ embedding_provider_configuration_id,
842
+ embedding_dimensions,
843
+ stage_model_settings,
844
+ create_thread_loop_count,
845
+ thread_count,
846
+ pending_count,
847
+ approved_count,
848
+ rejected_count,
849
+ error,
850
+ token_usage,
851
+ started_at,
852
+ completed_at,
853
+ committed_at`;
854
+ async function updateIngestionRunState(client, args) {
855
+ const result = await client.query({
856
+ text: exports.updateIngestionRunStateQuery,
857
+ values: [args.status, args.threadCount, args.pendingCount, args.approvedCount, args.rejectedCount, args.error, args.tokenUsage, args.completedAt, args.committedAt, args.id, args.organizationId],
858
+ rowMode: "array"
859
+ });
860
+ if (result.rows.length !== 1) {
861
+ return null;
862
+ }
863
+ const row = result.rows[0];
864
+ return {
865
+ id: row[0],
866
+ organizationId: row[1],
867
+ documentId: row[2],
868
+ status: row[3],
869
+ reasoningModelId: row[4],
870
+ reasoningProviderConfigurationId: row[5],
871
+ embeddingModelId: row[6],
872
+ embeddingProviderConfigurationId: row[7],
873
+ embeddingDimensions: row[8],
874
+ stageModelSettings: row[9],
875
+ createThreadLoopCount: row[10],
876
+ threadCount: row[11],
877
+ pendingCount: row[12],
878
+ approvedCount: row[13],
879
+ rejectedCount: row[14],
880
+ error: row[15],
881
+ tokenUsage: row[16],
882
+ startedAt: row[17],
883
+ completedAt: row[18],
884
+ committedAt: row[19]
885
+ };
886
+ }
887
+ exports.listIngestionRunsByOrganizationQuery = `-- name: ListIngestionRunsByOrganization :many
888
+ SELECT
889
+ id,
890
+ organization_id,
891
+ document_id,
892
+ status,
893
+ reasoning_model_id,
894
+ reasoning_provider_configuration_id,
895
+ embedding_model_id,
896
+ embedding_provider_configuration_id,
897
+ embedding_dimensions,
898
+ stage_model_settings,
899
+ create_thread_loop_count,
900
+ thread_count,
901
+ pending_count,
902
+ approved_count,
903
+ rejected_count,
904
+ error,
905
+ token_usage,
906
+ started_at,
907
+ completed_at,
908
+ committed_at
909
+ FROM weave.ingestion_runs
910
+ WHERE organization_id = $1
911
+ ORDER BY started_at DESC
912
+ LIMIT $3 OFFSET $2`;
913
+ async function listIngestionRunsByOrganization(client, args) {
914
+ const result = await client.query({
915
+ text: exports.listIngestionRunsByOrganizationQuery,
916
+ values: [args.organizationId, args.pageOffset, args.pageSize],
917
+ rowMode: "array"
918
+ });
919
+ return result.rows.map(row => {
920
+ return {
921
+ id: row[0],
922
+ organizationId: row[1],
923
+ documentId: row[2],
924
+ status: row[3],
925
+ reasoningModelId: row[4],
926
+ reasoningProviderConfigurationId: row[5],
927
+ embeddingModelId: row[6],
928
+ embeddingProviderConfigurationId: row[7],
929
+ embeddingDimensions: row[8],
930
+ stageModelSettings: row[9],
931
+ createThreadLoopCount: row[10],
932
+ threadCount: row[11],
933
+ pendingCount: row[12],
934
+ approvedCount: row[13],
935
+ rejectedCount: row[14],
936
+ error: row[15],
937
+ tokenUsage: row[16],
938
+ startedAt: row[17],
939
+ completedAt: row[18],
940
+ committedAt: row[19]
941
+ };
942
+ });
943
+ }
944
+ exports.listIngestionRunsByDocumentQuery = `-- name: ListIngestionRunsByDocument :many
945
+ SELECT
946
+ id,
947
+ organization_id,
948
+ document_id,
949
+ status,
950
+ reasoning_model_id,
951
+ reasoning_provider_configuration_id,
952
+ embedding_model_id,
953
+ embedding_provider_configuration_id,
954
+ embedding_dimensions,
955
+ stage_model_settings,
956
+ create_thread_loop_count,
957
+ thread_count,
958
+ pending_count,
959
+ approved_count,
960
+ rejected_count,
961
+ error,
962
+ token_usage,
963
+ started_at,
964
+ completed_at,
965
+ committed_at
966
+ FROM weave.ingestion_runs
967
+ WHERE organization_id = $1
968
+ AND document_id = $2
969
+ ORDER BY started_at DESC`;
970
+ async function listIngestionRunsByDocument(client, args) {
971
+ const result = await client.query({
972
+ text: exports.listIngestionRunsByDocumentQuery,
973
+ values: [args.organizationId, args.documentId],
974
+ rowMode: "array"
975
+ });
976
+ return result.rows.map(row => {
977
+ return {
978
+ id: row[0],
979
+ organizationId: row[1],
980
+ documentId: row[2],
981
+ status: row[3],
982
+ reasoningModelId: row[4],
983
+ reasoningProviderConfigurationId: row[5],
984
+ embeddingModelId: row[6],
985
+ embeddingProviderConfigurationId: row[7],
986
+ embeddingDimensions: row[8],
987
+ stageModelSettings: row[9],
988
+ createThreadLoopCount: row[10],
989
+ threadCount: row[11],
990
+ pendingCount: row[12],
991
+ approvedCount: row[13],
992
+ rejectedCount: row[14],
993
+ error: row[15],
994
+ tokenUsage: row[16],
995
+ startedAt: row[17],
996
+ completedAt: row[18],
997
+ committedAt: row[19]
998
+ };
999
+ });
1000
+ }
1001
+ exports.listIngestionHistoryQuery = `-- name: ListIngestionHistory :many
1002
+ SELECT
1003
+ r.id AS ingestion_run_id,
1004
+ d.id AS document_id,
1005
+ d.filename AS document_name,
1006
+ d.status AS document_status,
1007
+ r.status AS ingestion_status,
1008
+ r.thread_count,
1009
+ r.pending_count,
1010
+ r.approved_count,
1011
+ r.rejected_count,
1012
+ d.uploaded_at,
1013
+ r.started_at,
1014
+ r.completed_at,
1015
+ r.committed_at
1016
+ FROM weave.ingestion_runs r
1017
+ JOIN weave.documents d ON d.id = r.document_id
1018
+ WHERE r.organization_id = $1
1019
+ ORDER BY r.started_at DESC
1020
+ LIMIT $3 OFFSET $2`;
1021
+ async function listIngestionHistory(client, args) {
1022
+ const result = await client.query({
1023
+ text: exports.listIngestionHistoryQuery,
1024
+ values: [args.organizationId, args.pageOffset, args.pageSize],
1025
+ rowMode: "array"
1026
+ });
1027
+ return result.rows.map(row => {
1028
+ return {
1029
+ ingestionRunId: row[0],
1030
+ documentId: row[1],
1031
+ documentName: row[2],
1032
+ documentStatus: row[3],
1033
+ ingestionStatus: row[4],
1034
+ threadCount: row[5],
1035
+ pendingCount: row[6],
1036
+ approvedCount: row[7],
1037
+ rejectedCount: row[8],
1038
+ uploadedAt: row[9],
1039
+ startedAt: row[10],
1040
+ completedAt: row[11],
1041
+ committedAt: row[12]
1042
+ };
1043
+ });
1044
+ }
1045
+ exports.deleteDocumentTagsByDocumentIDQuery = `-- name: DeleteDocumentTagsByDocumentID :exec
1046
+ DELETE FROM weave.document_tags dt
1047
+ USING weave.documents d
1048
+ WHERE dt.document_id = d.id
1049
+ AND d.organization_id = $1
1050
+ AND dt.document_id = $2`;
1051
+ async function deleteDocumentTagsByDocumentID(client, args) {
1052
+ await client.query({
1053
+ text: exports.deleteDocumentTagsByDocumentIDQuery,
1054
+ values: [args.organizationId, args.documentId],
1055
+ rowMode: "array"
1056
+ });
1057
+ }
1058
+ exports.deleteDocumentTagByDocumentIDAndTagQuery = `-- name: DeleteDocumentTagByDocumentIDAndTag :execrows
1059
+ DELETE FROM weave.document_tags dt
1060
+ USING weave.documents d
1061
+ WHERE dt.document_id = d.id
1062
+ AND d.organization_id = $1
1063
+ AND dt.document_id = $2
1064
+ AND dt.tag = $3`;
1065
+ exports.createDocumentTagQuery = `-- name: CreateDocumentTag :one
1066
+ INSERT INTO weave.document_tags (
1067
+ document_id,
1068
+ tag,
1069
+ confidence
1070
+ ) VALUES (
1071
+ $1,
1072
+ $2,
1073
+ $3
1074
+ )
1075
+ RETURNING document_id, tag, confidence`;
1076
+ async function createDocumentTag(client, args) {
1077
+ const result = await client.query({
1078
+ text: exports.createDocumentTagQuery,
1079
+ values: [args.documentId, args.tag, args.confidence],
1080
+ rowMode: "array"
1081
+ });
1082
+ if (result.rows.length !== 1) {
1083
+ return null;
1084
+ }
1085
+ const row = result.rows[0];
1086
+ return {
1087
+ documentId: row[0],
1088
+ tag: row[1],
1089
+ confidence: row[2]
1090
+ };
1091
+ }
1092
+ exports.listDocumentTagsByDocumentIDQuery = `-- name: ListDocumentTagsByDocumentID :many
1093
+ SELECT document_id, tag, confidence
1094
+ FROM weave.document_tags dt
1095
+ JOIN weave.documents d ON d.id = dt.document_id
1096
+ WHERE d.organization_id = $1
1097
+ AND dt.document_id = $2
1098
+ ORDER BY confidence DESC, tag ASC`;
1099
+ async function listDocumentTagsByDocumentID(client, args) {
1100
+ const result = await client.query({
1101
+ text: exports.listDocumentTagsByDocumentIDQuery,
1102
+ values: [args.organizationId, args.documentId],
1103
+ rowMode: "array"
1104
+ });
1105
+ return result.rows.map(row => {
1106
+ return {
1107
+ documentId: row[0],
1108
+ tag: row[1],
1109
+ confidence: row[2]
1110
+ };
1111
+ });
1112
+ }
1113
+ exports.deleteDocumentByOrganizationAndIDQuery = `-- name: DeleteDocumentByOrganizationAndID :execrows
1114
+ DELETE FROM weave.documents
1115
+ WHERE organization_id = $1
1116
+ AND id = $2`;
1117
+ exports.createThreadQuery = `-- name: CreateThread :one
1118
+ INSERT INTO weave.threads (
1119
+ id,
1120
+ organization_id,
1121
+ document_id,
1122
+ ingestion_run_id,
1123
+ text,
1124
+ knowledge_type,
1125
+ facet_fields,
1126
+ confidence,
1127
+ temporal_validity,
1128
+ classification,
1129
+ retrieval_status,
1130
+ source_location,
1131
+ metadata
1132
+ ) VALUES (
1133
+ $1,
1134
+ $2,
1135
+ $3,
1136
+ $4,
1137
+ $5,
1138
+ $6,
1139
+ $7,
1140
+ $8,
1141
+ $9,
1142
+ $10,
1143
+ $11,
1144
+ $12,
1145
+ $13
1146
+ )
1147
+ RETURNING
1148
+ id,
1149
+ organization_id,
1150
+ document_id,
1151
+ ingestion_run_id,
1152
+ text,
1153
+ text_original,
1154
+ knowledge_type,
1155
+ facet_fields,
1156
+ confidence,
1157
+ temporal_validity,
1158
+ classification,
1159
+ retrieval_status,
1160
+ source_location,
1161
+ superseded_by,
1162
+ supersedes,
1163
+ cluster_id,
1164
+ extracted_at,
1165
+ approved_at,
1166
+ approved_by_user_id,
1167
+ edited,
1168
+ metadata`;
1169
+ async function createThread(client, args) {
1170
+ const result = await client.query({
1171
+ text: exports.createThreadQuery,
1172
+ 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],
1173
+ rowMode: "array"
1174
+ });
1175
+ if (result.rows.length !== 1) {
1176
+ return null;
1177
+ }
1178
+ const row = result.rows[0];
1179
+ return {
1180
+ id: row[0],
1181
+ organizationId: row[1],
1182
+ documentId: row[2],
1183
+ ingestionRunId: row[3],
1184
+ text: row[4],
1185
+ textOriginal: row[5],
1186
+ knowledgeType: row[6],
1187
+ facetFields: row[7],
1188
+ confidence: row[8],
1189
+ temporalValidity: row[9],
1190
+ classification: row[10],
1191
+ retrievalStatus: row[11],
1192
+ sourceLocation: row[12],
1193
+ supersededBy: row[13],
1194
+ supersedes: row[14],
1195
+ clusterId: row[15],
1196
+ extractedAt: row[16],
1197
+ approvedAt: row[17],
1198
+ approvedByUserId: row[18],
1199
+ edited: row[19],
1200
+ metadata: row[20]
1201
+ };
1202
+ }
1203
+ exports.getThreadByOrganizationAndIDQuery = `-- name: GetThreadByOrganizationAndID :one
1204
+ SELECT
1205
+ t.id,
1206
+ t.organization_id,
1207
+ t.document_id,
1208
+ d.filename AS document_name,
1209
+ t.ingestion_run_id,
1210
+ t.text,
1211
+ t.text_original,
1212
+ t.knowledge_type,
1213
+ t.facet_fields,
1214
+ t.confidence,
1215
+ t.temporal_validity,
1216
+ t.classification,
1217
+ t.retrieval_status,
1218
+ t.source_location,
1219
+ t.superseded_by,
1220
+ t.supersedes,
1221
+ t.cluster_id,
1222
+ t.extracted_at,
1223
+ t.approved_at,
1224
+ t.approved_by_user_id,
1225
+ t.edited,
1226
+ t.metadata
1227
+ FROM weave.threads t
1228
+ JOIN weave.documents d ON d.id = t.document_id
1229
+ WHERE t.organization_id = $1
1230
+ AND t.id = $2`;
1231
+ async function getThreadByOrganizationAndID(client, args) {
1232
+ const result = await client.query({
1233
+ text: exports.getThreadByOrganizationAndIDQuery,
1234
+ values: [args.organizationId, args.id],
1235
+ rowMode: "array"
1236
+ });
1237
+ if (result.rows.length !== 1) {
1238
+ return null;
1239
+ }
1240
+ const row = result.rows[0];
1241
+ return {
1242
+ id: row[0],
1243
+ organizationId: row[1],
1244
+ documentId: row[2],
1245
+ documentName: row[3],
1246
+ ingestionRunId: row[4],
1247
+ text: row[5],
1248
+ textOriginal: row[6],
1249
+ knowledgeType: row[7],
1250
+ facetFields: row[8],
1251
+ confidence: row[9],
1252
+ temporalValidity: row[10],
1253
+ classification: row[11],
1254
+ retrievalStatus: row[12],
1255
+ sourceLocation: row[13],
1256
+ supersededBy: row[14],
1257
+ supersedes: row[15],
1258
+ clusterId: row[16],
1259
+ extractedAt: row[17],
1260
+ approvedAt: row[18],
1261
+ approvedByUserId: row[19],
1262
+ edited: row[20],
1263
+ metadata: row[21]
1264
+ };
1265
+ }
1266
+ exports.listThreadsByIngestionRunQuery = `-- name: ListThreadsByIngestionRun :many
1267
+ SELECT
1268
+ t.id,
1269
+ t.organization_id,
1270
+ t.document_id,
1271
+ d.filename AS document_name,
1272
+ t.ingestion_run_id,
1273
+ t.text,
1274
+ t.text_original,
1275
+ t.knowledge_type,
1276
+ t.facet_fields,
1277
+ t.confidence,
1278
+ t.temporal_validity,
1279
+ t.classification,
1280
+ t.retrieval_status,
1281
+ t.source_location,
1282
+ t.superseded_by,
1283
+ t.supersedes,
1284
+ t.cluster_id,
1285
+ t.extracted_at,
1286
+ t.approved_at,
1287
+ t.approved_by_user_id,
1288
+ t.edited,
1289
+ t.metadata
1290
+ FROM weave.threads t
1291
+ JOIN weave.documents d ON d.id = t.document_id
1292
+ WHERE t.organization_id = $1
1293
+ AND t.ingestion_run_id = $2
1294
+ ORDER BY t.extracted_at ASC, t.id ASC`;
1295
+ async function listThreadsByIngestionRun(client, args) {
1296
+ const result = await client.query({
1297
+ text: exports.listThreadsByIngestionRunQuery,
1298
+ values: [args.organizationId, args.ingestionRunId],
1299
+ rowMode: "array"
1300
+ });
1301
+ return result.rows.map(row => {
1302
+ return {
1303
+ id: row[0],
1304
+ organizationId: row[1],
1305
+ documentId: row[2],
1306
+ documentName: row[3],
1307
+ ingestionRunId: row[4],
1308
+ text: row[5],
1309
+ textOriginal: row[6],
1310
+ knowledgeType: row[7],
1311
+ facetFields: row[8],
1312
+ confidence: row[9],
1313
+ temporalValidity: row[10],
1314
+ classification: row[11],
1315
+ retrievalStatus: row[12],
1316
+ sourceLocation: row[13],
1317
+ supersededBy: row[14],
1318
+ supersedes: row[15],
1319
+ clusterId: row[16],
1320
+ extractedAt: row[17],
1321
+ approvedAt: row[18],
1322
+ approvedByUserId: row[19],
1323
+ edited: row[20],
1324
+ metadata: row[21]
1325
+ };
1326
+ });
1327
+ }
1328
+ exports.listThreadsByDocumentQuery = `-- name: ListThreadsByDocument :many
1329
+ SELECT
1330
+ t.id,
1331
+ t.organization_id,
1332
+ t.document_id,
1333
+ d.filename AS document_name,
1334
+ t.ingestion_run_id,
1335
+ t.text,
1336
+ t.text_original,
1337
+ t.knowledge_type,
1338
+ t.facet_fields,
1339
+ t.confidence,
1340
+ t.temporal_validity,
1341
+ t.classification,
1342
+ t.retrieval_status,
1343
+ t.source_location,
1344
+ t.superseded_by,
1345
+ t.supersedes,
1346
+ t.cluster_id,
1347
+ t.extracted_at,
1348
+ t.approved_at,
1349
+ t.approved_by_user_id,
1350
+ t.edited,
1351
+ t.metadata
1352
+ FROM weave.threads t
1353
+ JOIN weave.documents d ON d.id = t.document_id
1354
+ WHERE t.organization_id = $1
1355
+ AND t.document_id = $2
1356
+ ORDER BY t.extracted_at ASC, t.id ASC`;
1357
+ async function listThreadsByDocument(client, args) {
1358
+ const result = await client.query({
1359
+ text: exports.listThreadsByDocumentQuery,
1360
+ values: [args.organizationId, args.documentId],
1361
+ rowMode: "array"
1362
+ });
1363
+ return result.rows.map(row => {
1364
+ return {
1365
+ id: row[0],
1366
+ organizationId: row[1],
1367
+ documentId: row[2],
1368
+ documentName: row[3],
1369
+ ingestionRunId: row[4],
1370
+ text: row[5],
1371
+ textOriginal: row[6],
1372
+ knowledgeType: row[7],
1373
+ facetFields: row[8],
1374
+ confidence: row[9],
1375
+ temporalValidity: row[10],
1376
+ classification: row[11],
1377
+ retrievalStatus: row[12],
1378
+ sourceLocation: row[13],
1379
+ supersededBy: row[14],
1380
+ supersedes: row[15],
1381
+ clusterId: row[16],
1382
+ extractedAt: row[17],
1383
+ approvedAt: row[18],
1384
+ approvedByUserId: row[19],
1385
+ edited: row[20],
1386
+ metadata: row[21]
1387
+ };
1388
+ });
1389
+ }
1390
+ exports.listAccessibleThreadsByDocumentQuery = `-- name: ListAccessibleThreadsByDocument :many
1391
+ SELECT
1392
+ t.id,
1393
+ t.organization_id,
1394
+ t.document_id,
1395
+ d.filename AS document_name,
1396
+ t.ingestion_run_id,
1397
+ t.text,
1398
+ t.text_original,
1399
+ t.knowledge_type,
1400
+ t.facet_fields,
1401
+ t.confidence,
1402
+ t.temporal_validity,
1403
+ t.classification,
1404
+ t.retrieval_status,
1405
+ t.source_location,
1406
+ t.superseded_by,
1407
+ t.supersedes,
1408
+ t.cluster_id,
1409
+ t.extracted_at,
1410
+ t.approved_at,
1411
+ t.approved_by_user_id,
1412
+ t.edited,
1413
+ t.metadata
1414
+ FROM weave.threads t
1415
+ JOIN weave.documents d
1416
+ ON d.organization_id = t.organization_id
1417
+ AND d.id = t.document_id
1418
+ WHERE t.organization_id = $1
1419
+ AND t.document_id = $2
1420
+ AND (
1421
+ d.scope = 'DOCUMENT_SCOPE_ORG'
1422
+ OR (d.scope = 'DOCUMENT_SCOPE_PRIVATE'
1423
+ AND d.uploaded_by_user_id = $3)
1424
+ OR (d.scope = 'DOCUMENT_SCOPE_RESTRICTED'
1425
+ AND (
1426
+ EXISTS (
1427
+ SELECT 1
1428
+ FROM weave.document_restricted_groups drg
1429
+ JOIN weave.restricted_group_user_memberships rgum
1430
+ ON rgum.organization_id = drg.organization_id
1431
+ AND rgum.restricted_group_id = drg.restricted_group_id
1432
+ AND rgum.user_id = $3
1433
+ WHERE drg.organization_id = d.organization_id
1434
+ AND drg.document_id = d.id
1435
+ )
1436
+ OR EXISTS (
1437
+ SELECT 1
1438
+ FROM weave.document_restricted_groups drg
1439
+ JOIN weave.restricted_group_team_memberships rgtm
1440
+ ON rgtm.organization_id = drg.organization_id
1441
+ AND rgtm.restricted_group_id = drg.restricted_group_id
1442
+ JOIN weave.organization_team_membership otm
1443
+ ON otm.organization_id = rgtm.organization_id
1444
+ AND otm.organization_team_id = rgtm.org_team_id
1445
+ AND otm.user_id = $3
1446
+ WHERE drg.organization_id = d.organization_id
1447
+ AND drg.document_id = d.id
1448
+ )))
1449
+ )
1450
+ ORDER BY t.extracted_at ASC, t.id ASC
1451
+ LIMIT $5 OFFSET $4`;
1452
+ async function listAccessibleThreadsByDocument(client, args) {
1453
+ const result = await client.query({
1454
+ text: exports.listAccessibleThreadsByDocumentQuery,
1455
+ values: [args.organizationId, args.documentId, args.actingUserId, args.pageOffset, args.pageSize],
1456
+ rowMode: "array"
1457
+ });
1458
+ return result.rows.map(row => {
1459
+ return {
1460
+ id: row[0],
1461
+ organizationId: row[1],
1462
+ documentId: row[2],
1463
+ documentName: row[3],
1464
+ ingestionRunId: row[4],
1465
+ text: row[5],
1466
+ textOriginal: row[6],
1467
+ knowledgeType: row[7],
1468
+ facetFields: row[8],
1469
+ confidence: row[9],
1470
+ temporalValidity: row[10],
1471
+ classification: row[11],
1472
+ retrievalStatus: row[12],
1473
+ sourceLocation: row[13],
1474
+ supersededBy: row[14],
1475
+ supersedes: row[15],
1476
+ clusterId: row[16],
1477
+ extractedAt: row[17],
1478
+ approvedAt: row[18],
1479
+ approvedByUserId: row[19],
1480
+ edited: row[20],
1481
+ metadata: row[21]
1482
+ };
1483
+ });
1484
+ }
1485
+ exports.listPendingThreadsByRunQuery = `-- name: ListPendingThreadsByRun :many
1486
+ SELECT
1487
+ id,
1488
+ organization_id,
1489
+ document_id,
1490
+ ingestion_run_id,
1491
+ text,
1492
+ text_original,
1493
+ knowledge_type,
1494
+ facet_fields,
1495
+ confidence,
1496
+ temporal_validity,
1497
+ classification,
1498
+ retrieval_status,
1499
+ source_location,
1500
+ superseded_by,
1501
+ supersedes,
1502
+ cluster_id,
1503
+ extracted_at,
1504
+ approved_at,
1505
+ approved_by_user_id,
1506
+ edited,
1507
+ metadata
1508
+ FROM weave.threads
1509
+ WHERE organization_id = $1
1510
+ AND ingestion_run_id = $2
1511
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1512
+ ORDER BY extracted_at ASC, id ASC`;
1513
+ async function listPendingThreadsByRun(client, args) {
1514
+ const result = await client.query({
1515
+ text: exports.listPendingThreadsByRunQuery,
1516
+ values: [args.organizationId, args.ingestionRunId],
1517
+ rowMode: "array"
1518
+ });
1519
+ return result.rows.map(row => {
1520
+ return {
1521
+ id: row[0],
1522
+ organizationId: row[1],
1523
+ documentId: row[2],
1524
+ ingestionRunId: row[3],
1525
+ text: row[4],
1526
+ textOriginal: row[5],
1527
+ knowledgeType: row[6],
1528
+ facetFields: row[7],
1529
+ confidence: row[8],
1530
+ temporalValidity: row[9],
1531
+ classification: row[10],
1532
+ retrievalStatus: row[11],
1533
+ sourceLocation: row[12],
1534
+ supersededBy: row[13],
1535
+ supersedes: row[14],
1536
+ clusterId: row[15],
1537
+ extractedAt: row[16],
1538
+ approvedAt: row[17],
1539
+ approvedByUserId: row[18],
1540
+ edited: row[19],
1541
+ metadata: row[20]
1542
+ };
1543
+ });
1544
+ }
1545
+ exports.listPendingThreadsByOrganizationQuery = `-- name: ListPendingThreadsByOrganization :many
1546
+ SELECT
1547
+ t.id,
1548
+ t.organization_id,
1549
+ t.document_id,
1550
+ d.filename AS document_name,
1551
+ t.ingestion_run_id,
1552
+ t.text,
1553
+ t.text_original,
1554
+ t.knowledge_type,
1555
+ t.facet_fields,
1556
+ t.confidence,
1557
+ t.temporal_validity,
1558
+ t.classification,
1559
+ t.retrieval_status,
1560
+ t.source_location,
1561
+ t.superseded_by,
1562
+ t.supersedes,
1563
+ t.cluster_id,
1564
+ t.extracted_at,
1565
+ t.approved_at,
1566
+ t.approved_by_user_id,
1567
+ t.edited,
1568
+ t.metadata
1569
+ FROM weave.threads t
1570
+ JOIN weave.documents d ON d.id = t.document_id
1571
+ WHERE t.organization_id = $1
1572
+ AND t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1573
+ ORDER BY t.extracted_at DESC, t.id DESC
1574
+ LIMIT $3 OFFSET $2`;
1575
+ async function listPendingThreadsByOrganization(client, args) {
1576
+ const result = await client.query({
1577
+ text: exports.listPendingThreadsByOrganizationQuery,
1578
+ values: [args.organizationId, args.pageOffset, args.pageSize],
1579
+ rowMode: "array"
1580
+ });
1581
+ return result.rows.map(row => {
1582
+ return {
1583
+ id: row[0],
1584
+ organizationId: row[1],
1585
+ documentId: row[2],
1586
+ documentName: row[3],
1587
+ ingestionRunId: row[4],
1588
+ text: row[5],
1589
+ textOriginal: row[6],
1590
+ knowledgeType: row[7],
1591
+ facetFields: row[8],
1592
+ confidence: row[9],
1593
+ temporalValidity: row[10],
1594
+ classification: row[11],
1595
+ retrievalStatus: row[12],
1596
+ sourceLocation: row[13],
1597
+ supersededBy: row[14],
1598
+ supersedes: row[15],
1599
+ clusterId: row[16],
1600
+ extractedAt: row[17],
1601
+ approvedAt: row[18],
1602
+ approvedByUserId: row[19],
1603
+ edited: row[20],
1604
+ metadata: row[21]
1605
+ };
1606
+ });
1607
+ }
1608
+ exports.updateThreadEmbeddingQuery = `-- name: UpdateThreadEmbedding :one
1609
+ UPDATE weave.threads
1610
+ SET
1611
+ embedding = $1::vector
1612
+ WHERE organization_id = $2
1613
+ AND id = $3
1614
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1615
+ RETURNING
1616
+ id,
1617
+ organization_id,
1618
+ document_id,
1619
+ ingestion_run_id,
1620
+ text,
1621
+ text_original,
1622
+ knowledge_type,
1623
+ facet_fields,
1624
+ confidence,
1625
+ temporal_validity,
1626
+ classification,
1627
+ retrieval_status,
1628
+ source_location,
1629
+ superseded_by,
1630
+ supersedes,
1631
+ cluster_id,
1632
+ extracted_at,
1633
+ approved_at,
1634
+ approved_by_user_id,
1635
+ edited,
1636
+ metadata`;
1637
+ async function updateThreadEmbedding(client, args) {
1638
+ const result = await client.query({
1639
+ text: exports.updateThreadEmbeddingQuery,
1640
+ values: [args.embedding, args.organizationId, args.id],
1641
+ rowMode: "array"
1642
+ });
1643
+ if (result.rows.length !== 1) {
1644
+ return null;
1645
+ }
1646
+ const row = result.rows[0];
1647
+ return {
1648
+ id: row[0],
1649
+ organizationId: row[1],
1650
+ documentId: row[2],
1651
+ ingestionRunId: row[3],
1652
+ text: row[4],
1653
+ textOriginal: row[5],
1654
+ knowledgeType: row[6],
1655
+ facetFields: row[7],
1656
+ confidence: row[8],
1657
+ temporalValidity: row[9],
1658
+ classification: row[10],
1659
+ retrievalStatus: row[11],
1660
+ sourceLocation: row[12],
1661
+ supersededBy: row[13],
1662
+ supersedes: row[14],
1663
+ clusterId: row[15],
1664
+ extractedAt: row[16],
1665
+ approvedAt: row[17],
1666
+ approvedByUserId: row[18],
1667
+ edited: row[19],
1668
+ metadata: row[20]
1669
+ };
1670
+ }
1671
+ exports.setThreadNextEmbeddingQuery = `-- name: SetThreadNextEmbedding :one
1672
+ UPDATE weave.threads
1673
+ SET
1674
+ embedding_next = $1::vector
1675
+ WHERE organization_id = $2
1676
+ AND id = $3
1677
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1678
+ RETURNING
1679
+ id,
1680
+ organization_id,
1681
+ document_id,
1682
+ ingestion_run_id,
1683
+ text,
1684
+ text_original,
1685
+ knowledge_type,
1686
+ facet_fields,
1687
+ confidence,
1688
+ temporal_validity,
1689
+ classification,
1690
+ retrieval_status,
1691
+ source_location,
1692
+ superseded_by,
1693
+ supersedes,
1694
+ cluster_id,
1695
+ extracted_at,
1696
+ approved_at,
1697
+ approved_by_user_id,
1698
+ edited,
1699
+ metadata`;
1700
+ async function setThreadNextEmbedding(client, args) {
1701
+ const result = await client.query({
1702
+ text: exports.setThreadNextEmbeddingQuery,
1703
+ values: [args.embedding, args.organizationId, args.id],
1704
+ rowMode: "array"
1705
+ });
1706
+ if (result.rows.length !== 1) {
1707
+ return null;
1708
+ }
1709
+ const row = result.rows[0];
1710
+ return {
1711
+ id: row[0],
1712
+ organizationId: row[1],
1713
+ documentId: row[2],
1714
+ ingestionRunId: row[3],
1715
+ text: row[4],
1716
+ textOriginal: row[5],
1717
+ knowledgeType: row[6],
1718
+ facetFields: row[7],
1719
+ confidence: row[8],
1720
+ temporalValidity: row[9],
1721
+ classification: row[10],
1722
+ retrievalStatus: row[11],
1723
+ sourceLocation: row[12],
1724
+ supersededBy: row[13],
1725
+ supersedes: row[14],
1726
+ clusterId: row[15],
1727
+ extractedAt: row[16],
1728
+ approvedAt: row[17],
1729
+ approvedByUserId: row[18],
1730
+ edited: row[19],
1731
+ metadata: row[20]
1732
+ };
1733
+ }
1734
+ exports.promoteThreadNextEmbeddingsByOrganizationQuery = `-- name: PromoteThreadNextEmbeddingsByOrganization :execrows
1735
+ UPDATE weave.threads
1736
+ SET
1737
+ embedding = embedding_next,
1738
+ embedding_next = NULL
1739
+ WHERE organization_id = $1
1740
+ AND embedding_next IS NOT NULL`;
1741
+ exports.updateThreadClassificationQuery = `-- name: UpdateThreadClassification :one
1742
+ UPDATE weave.threads
1743
+ SET
1744
+ classification = $1,
1745
+ supersedes = $2
1746
+ WHERE organization_id = $3
1747
+ AND id = $4
1748
+ RETURNING
1749
+ id,
1750
+ organization_id,
1751
+ document_id,
1752
+ ingestion_run_id,
1753
+ text,
1754
+ text_original,
1755
+ knowledge_type,
1756
+ facet_fields,
1757
+ confidence,
1758
+ temporal_validity,
1759
+ classification,
1760
+ retrieval_status,
1761
+ source_location,
1762
+ superseded_by,
1763
+ supersedes,
1764
+ cluster_id,
1765
+ extracted_at,
1766
+ approved_at,
1767
+ approved_by_user_id,
1768
+ edited,
1769
+ metadata`;
1770
+ async function updateThreadClassification(client, args) {
1771
+ const result = await client.query({
1772
+ text: exports.updateThreadClassificationQuery,
1773
+ values: [args.classification, args.supersedes, args.organizationId, args.id],
1774
+ rowMode: "array"
1775
+ });
1776
+ if (result.rows.length !== 1) {
1777
+ return null;
1778
+ }
1779
+ const row = result.rows[0];
1780
+ return {
1781
+ id: row[0],
1782
+ organizationId: row[1],
1783
+ documentId: row[2],
1784
+ ingestionRunId: row[3],
1785
+ text: row[4],
1786
+ textOriginal: row[5],
1787
+ knowledgeType: row[6],
1788
+ facetFields: row[7],
1789
+ confidence: row[8],
1790
+ temporalValidity: row[9],
1791
+ classification: row[10],
1792
+ retrievalStatus: row[11],
1793
+ sourceLocation: row[12],
1794
+ supersededBy: row[13],
1795
+ supersedes: row[14],
1796
+ clusterId: row[15],
1797
+ extractedAt: row[16],
1798
+ approvedAt: row[17],
1799
+ approvedByUserId: row[18],
1800
+ edited: row[19],
1801
+ metadata: row[20]
1802
+ };
1803
+ }
1804
+ exports.editPendingThreadQuery = `-- name: EditPendingThread :one
1805
+ UPDATE weave.threads
1806
+ SET
1807
+ text_original = CASE
1808
+ WHEN text_original IS NULL OR text_original = '' THEN text
1809
+ ELSE text_original
1810
+ END,
1811
+ text = $1,
1812
+ edited = TRUE
1813
+ WHERE id = $2
1814
+ AND organization_id = $3
1815
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1816
+ RETURNING
1817
+ id,
1818
+ organization_id,
1819
+ document_id,
1820
+ ingestion_run_id,
1821
+ text,
1822
+ text_original,
1823
+ knowledge_type,
1824
+ facet_fields,
1825
+ confidence,
1826
+ temporal_validity,
1827
+ classification,
1828
+ retrieval_status,
1829
+ source_location,
1830
+ superseded_by,
1831
+ supersedes,
1832
+ cluster_id,
1833
+ extracted_at,
1834
+ approved_at,
1835
+ approved_by_user_id,
1836
+ edited,
1837
+ metadata`;
1838
+ async function editPendingThread(client, args) {
1839
+ const result = await client.query({
1840
+ text: exports.editPendingThreadQuery,
1841
+ values: [args.text, args.id, args.organizationId],
1842
+ rowMode: "array"
1843
+ });
1844
+ if (result.rows.length !== 1) {
1845
+ return null;
1846
+ }
1847
+ const row = result.rows[0];
1848
+ return {
1849
+ id: row[0],
1850
+ organizationId: row[1],
1851
+ documentId: row[2],
1852
+ ingestionRunId: row[3],
1853
+ text: row[4],
1854
+ textOriginal: row[5],
1855
+ knowledgeType: row[6],
1856
+ facetFields: row[7],
1857
+ confidence: row[8],
1858
+ temporalValidity: row[9],
1859
+ classification: row[10],
1860
+ retrievalStatus: row[11],
1861
+ sourceLocation: row[12],
1862
+ supersededBy: row[13],
1863
+ supersedes: row[14],
1864
+ clusterId: row[15],
1865
+ extractedAt: row[16],
1866
+ approvedAt: row[17],
1867
+ approvedByUserId: row[18],
1868
+ edited: row[19],
1869
+ metadata: row[20]
1870
+ };
1871
+ }
1872
+ exports.editDocumentThreadQuery = `-- name: EditDocumentThread :one
1873
+ WITH updated AS (
1874
+ UPDATE weave.threads AS t
1875
+ SET
1876
+ text_original = CASE
1877
+ WHEN t.text_original IS NULL OR t.text_original = '' THEN t.text
1878
+ ELSE t.text_original
1879
+ END,
1880
+ text = $1,
1881
+ source_location = COALESCE($2::jsonb, t.source_location),
1882
+ metadata = t.metadata || COALESCE($3::jsonb, '{}'::jsonb),
1883
+ edited = TRUE
1884
+ WHERE t.id = $4
1885
+ AND t.organization_id = $5
1886
+ AND t.document_id = $6
1887
+ AND t.retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1888
+ RETURNING
1889
+ id,
1890
+ organization_id,
1891
+ document_id,
1892
+ ingestion_run_id,
1893
+ text,
1894
+ text_original,
1895
+ knowledge_type,
1896
+ facet_fields,
1897
+ confidence,
1898
+ temporal_validity,
1899
+ classification,
1900
+ retrieval_status,
1901
+ source_location,
1902
+ superseded_by,
1903
+ supersedes,
1904
+ cluster_id,
1905
+ extracted_at,
1906
+ approved_at,
1907
+ approved_by_user_id,
1908
+ edited,
1909
+ metadata
1910
+ )
1911
+ SELECT
1912
+ updated.id,
1913
+ updated.organization_id,
1914
+ updated.document_id,
1915
+ d.filename AS document_name,
1916
+ updated.ingestion_run_id,
1917
+ updated.text,
1918
+ updated.text_original,
1919
+ updated.knowledge_type,
1920
+ updated.facet_fields,
1921
+ updated.confidence,
1922
+ updated.temporal_validity,
1923
+ updated.classification,
1924
+ updated.retrieval_status,
1925
+ updated.source_location,
1926
+ updated.superseded_by,
1927
+ updated.supersedes,
1928
+ updated.cluster_id,
1929
+ updated.extracted_at,
1930
+ updated.approved_at,
1931
+ updated.approved_by_user_id,
1932
+ updated.edited,
1933
+ updated.metadata
1934
+ FROM updated
1935
+ JOIN weave.documents d ON d.id = updated.document_id`;
1936
+ async function editDocumentThread(client, args) {
1937
+ const result = await client.query({
1938
+ text: exports.editDocumentThreadQuery,
1939
+ values: [args.text, args.sourceLocation, args.metadata, args.id, args.organizationId, args.documentId],
1940
+ rowMode: "array"
1941
+ });
1942
+ if (result.rows.length !== 1) {
1943
+ return null;
1944
+ }
1945
+ const row = result.rows[0];
1946
+ return {
1947
+ id: row[0],
1948
+ organizationId: row[1],
1949
+ documentId: row[2],
1950
+ documentName: row[3],
1951
+ ingestionRunId: row[4],
1952
+ text: row[5],
1953
+ textOriginal: row[6],
1954
+ knowledgeType: row[7],
1955
+ facetFields: row[8],
1956
+ confidence: row[9],
1957
+ temporalValidity: row[10],
1958
+ classification: row[11],
1959
+ retrievalStatus: row[12],
1960
+ sourceLocation: row[13],
1961
+ supersededBy: row[14],
1962
+ supersedes: row[15],
1963
+ clusterId: row[16],
1964
+ extractedAt: row[17],
1965
+ approvedAt: row[18],
1966
+ approvedByUserId: row[19],
1967
+ edited: row[20],
1968
+ metadata: row[21]
1969
+ };
1970
+ }
1971
+ exports.deleteDocumentThreadByOrganizationAndIDQuery = `-- name: DeleteDocumentThreadByOrganizationAndID :execrows
1972
+ DELETE FROM weave.threads
1973
+ WHERE organization_id = $1
1974
+ AND document_id = $2
1975
+ AND id = $3
1976
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'`;
1977
+ exports.approveThreadQuery = `-- name: ApproveThread :one
1978
+ UPDATE weave.threads
1979
+ SET
1980
+ retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED',
1981
+ approved_at = now(),
1982
+ approved_by_user_id = $1,
1983
+ embedding = COALESCE($2::vector, embedding),
1984
+ embedding_next = NULL
1985
+ WHERE id = $3
1986
+ AND organization_id = $4
1987
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
1988
+ RETURNING
1989
+ id,
1990
+ organization_id,
1991
+ document_id,
1992
+ ingestion_run_id,
1993
+ text,
1994
+ text_original,
1995
+ knowledge_type,
1996
+ facet_fields,
1997
+ confidence,
1998
+ temporal_validity,
1999
+ classification,
2000
+ retrieval_status,
2001
+ source_location,
2002
+ superseded_by,
2003
+ supersedes,
2004
+ cluster_id,
2005
+ extracted_at,
2006
+ approved_at,
2007
+ approved_by_user_id,
2008
+ edited,
2009
+ metadata`;
2010
+ async function approveThread(client, args) {
2011
+ const result = await client.query({
2012
+ text: exports.approveThreadQuery,
2013
+ values: [args.approvedByUserId, args.embedding, args.id, args.organizationId],
2014
+ rowMode: "array"
2015
+ });
2016
+ if (result.rows.length !== 1) {
2017
+ return null;
2018
+ }
2019
+ const row = result.rows[0];
2020
+ return {
2021
+ id: row[0],
2022
+ organizationId: row[1],
2023
+ documentId: row[2],
2024
+ ingestionRunId: row[3],
2025
+ text: row[4],
2026
+ textOriginal: row[5],
2027
+ knowledgeType: row[6],
2028
+ facetFields: row[7],
2029
+ confidence: row[8],
2030
+ temporalValidity: row[9],
2031
+ classification: row[10],
2032
+ retrievalStatus: row[11],
2033
+ sourceLocation: row[12],
2034
+ supersededBy: row[13],
2035
+ supersedes: row[14],
2036
+ clusterId: row[15],
2037
+ extractedAt: row[16],
2038
+ approvedAt: row[17],
2039
+ approvedByUserId: row[18],
2040
+ edited: row[19],
2041
+ metadata: row[20]
2042
+ };
2043
+ }
2044
+ exports.bulkApproveThreadsQuery = `-- name: BulkApproveThreads :many
2045
+ UPDATE weave.threads
2046
+ SET
2047
+ retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED',
2048
+ approved_at = now(),
2049
+ approved_by_user_id = $1,
2050
+ embedding_next = NULL
2051
+ WHERE organization_id = $2
2052
+ AND ingestion_run_id = $3
2053
+ AND id = ANY($4::uuid[])
2054
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
2055
+ RETURNING
2056
+ id,
2057
+ organization_id,
2058
+ document_id,
2059
+ ingestion_run_id,
2060
+ text,
2061
+ text_original,
2062
+ knowledge_type,
2063
+ facet_fields,
2064
+ confidence,
2065
+ temporal_validity,
2066
+ classification,
2067
+ retrieval_status,
2068
+ source_location,
2069
+ superseded_by,
2070
+ supersedes,
2071
+ cluster_id,
2072
+ extracted_at,
2073
+ approved_at,
2074
+ approved_by_user_id,
2075
+ edited,
2076
+ metadata`;
2077
+ async function bulkApproveThreads(client, args) {
2078
+ const result = await client.query({
2079
+ text: exports.bulkApproveThreadsQuery,
2080
+ values: [args.approvedByUserId, args.organizationId, args.ingestionRunId, args.threadIds],
2081
+ rowMode: "array"
2082
+ });
2083
+ return result.rows.map(row => {
2084
+ return {
2085
+ id: row[0],
2086
+ organizationId: row[1],
2087
+ documentId: row[2],
2088
+ ingestionRunId: row[3],
2089
+ text: row[4],
2090
+ textOriginal: row[5],
2091
+ knowledgeType: row[6],
2092
+ facetFields: row[7],
2093
+ confidence: row[8],
2094
+ temporalValidity: row[9],
2095
+ classification: row[10],
2096
+ retrievalStatus: row[11],
2097
+ sourceLocation: row[12],
2098
+ supersededBy: row[13],
2099
+ supersedes: row[14],
2100
+ clusterId: row[15],
2101
+ extractedAt: row[16],
2102
+ approvedAt: row[17],
2103
+ approvedByUserId: row[18],
2104
+ edited: row[19],
2105
+ metadata: row[20]
2106
+ };
2107
+ });
2108
+ }
2109
+ exports.rejectThreadQuery = `-- name: RejectThread :one
2110
+ UPDATE weave.threads
2111
+ SET
2112
+ retrieval_status = 'THREAD_RETRIEVAL_STATUS_REJECTED'
2113
+ WHERE organization_id = $1
2114
+ AND id = $2
2115
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
2116
+ RETURNING
2117
+ id,
2118
+ organization_id,
2119
+ document_id,
2120
+ ingestion_run_id,
2121
+ text,
2122
+ text_original,
2123
+ knowledge_type,
2124
+ facet_fields,
2125
+ confidence,
2126
+ temporal_validity,
2127
+ classification,
2128
+ retrieval_status,
2129
+ source_location,
2130
+ superseded_by,
2131
+ supersedes,
2132
+ cluster_id,
2133
+ extracted_at,
2134
+ approved_at,
2135
+ approved_by_user_id,
2136
+ edited,
2137
+ metadata`;
2138
+ async function rejectThread(client, args) {
2139
+ const result = await client.query({
2140
+ text: exports.rejectThreadQuery,
2141
+ values: [args.organizationId, args.id],
2142
+ rowMode: "array"
2143
+ });
2144
+ if (result.rows.length !== 1) {
2145
+ return null;
2146
+ }
2147
+ const row = result.rows[0];
2148
+ return {
2149
+ id: row[0],
2150
+ organizationId: row[1],
2151
+ documentId: row[2],
2152
+ ingestionRunId: row[3],
2153
+ text: row[4],
2154
+ textOriginal: row[5],
2155
+ knowledgeType: row[6],
2156
+ facetFields: row[7],
2157
+ confidence: row[8],
2158
+ temporalValidity: row[9],
2159
+ classification: row[10],
2160
+ retrievalStatus: row[11],
2161
+ sourceLocation: row[12],
2162
+ supersededBy: row[13],
2163
+ supersedes: row[14],
2164
+ clusterId: row[15],
2165
+ extractedAt: row[16],
2166
+ approvedAt: row[17],
2167
+ approvedByUserId: row[18],
2168
+ edited: row[19],
2169
+ metadata: row[20]
2170
+ };
2171
+ }
2172
+ exports.bulkRejectThreadsQuery = `-- name: BulkRejectThreads :many
2173
+ UPDATE weave.threads
2174
+ SET
2175
+ retrieval_status = 'THREAD_RETRIEVAL_STATUS_REJECTED'
2176
+ WHERE organization_id = $1
2177
+ AND ingestion_run_id = $2
2178
+ AND id = ANY($3::uuid[])
2179
+ AND retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
2180
+ RETURNING
2181
+ id,
2182
+ organization_id,
2183
+ document_id,
2184
+ ingestion_run_id,
2185
+ text,
2186
+ text_original,
2187
+ knowledge_type,
2188
+ facet_fields,
2189
+ confidence,
2190
+ temporal_validity,
2191
+ classification,
2192
+ retrieval_status,
2193
+ source_location,
2194
+ superseded_by,
2195
+ supersedes,
2196
+ cluster_id,
2197
+ extracted_at,
2198
+ approved_at,
2199
+ approved_by_user_id,
2200
+ edited,
2201
+ metadata`;
2202
+ async function bulkRejectThreads(client, args) {
2203
+ const result = await client.query({
2204
+ text: exports.bulkRejectThreadsQuery,
2205
+ values: [args.organizationId, args.ingestionRunId, args.threadIds],
2206
+ rowMode: "array"
2207
+ });
2208
+ return result.rows.map(row => {
2209
+ return {
2210
+ id: row[0],
2211
+ organizationId: row[1],
2212
+ documentId: row[2],
2213
+ ingestionRunId: row[3],
2214
+ text: row[4],
2215
+ textOriginal: row[5],
2216
+ knowledgeType: row[6],
2217
+ facetFields: row[7],
2218
+ confidence: row[8],
2219
+ temporalValidity: row[9],
2220
+ classification: row[10],
2221
+ retrievalStatus: row[11],
2222
+ sourceLocation: row[12],
2223
+ supersededBy: row[13],
2224
+ supersedes: row[14],
2225
+ clusterId: row[15],
2226
+ extractedAt: row[16],
2227
+ approvedAt: row[17],
2228
+ approvedByUserId: row[18],
2229
+ edited: row[19],
2230
+ metadata: row[20]
2231
+ };
2232
+ });
2233
+ }
2234
+ exports.markThreadsSupersededQuery = `-- name: MarkThreadsSuperseded :execrows
2235
+ UPDATE weave.threads
2236
+ SET
2237
+ retrieval_status = 'THREAD_RETRIEVAL_STATUS_SUPERSEDED',
2238
+ superseded_by = $1
2239
+ WHERE organization_id = $2
2240
+ AND id = ANY($3::uuid[])`;
2241
+ exports.listThreadsBySupersedesQuery = `-- name: ListThreadsBySupersedes :many
2242
+ SELECT
2243
+ id,
2244
+ organization_id,
2245
+ document_id,
2246
+ ingestion_run_id,
2247
+ text,
2248
+ text_original,
2249
+ knowledge_type,
2250
+ facet_fields,
2251
+ confidence,
2252
+ temporal_validity,
2253
+ classification,
2254
+ retrieval_status,
2255
+ source_location,
2256
+ superseded_by,
2257
+ supersedes,
2258
+ cluster_id,
2259
+ extracted_at,
2260
+ approved_at,
2261
+ approved_by_user_id,
2262
+ edited,
2263
+ metadata
2264
+ FROM weave.threads
2265
+ WHERE organization_id = $1
2266
+ AND $2 = ANY(supersedes)
2267
+ ORDER BY approved_at DESC NULLS LAST, extracted_at DESC, id DESC`;
2268
+ async function listThreadsBySupersedes(client, args) {
2269
+ const result = await client.query({
2270
+ text: exports.listThreadsBySupersedesQuery,
2271
+ values: [args.organizationId, args.threadId],
2272
+ rowMode: "array"
2273
+ });
2274
+ return result.rows.map(row => {
2275
+ return {
2276
+ id: row[0],
2277
+ organizationId: row[1],
2278
+ documentId: row[2],
2279
+ ingestionRunId: row[3],
2280
+ text: row[4],
2281
+ textOriginal: row[5],
2282
+ knowledgeType: row[6],
2283
+ facetFields: row[7],
2284
+ confidence: row[8],
2285
+ temporalValidity: row[9],
2286
+ classification: row[10],
2287
+ retrievalStatus: row[11],
2288
+ sourceLocation: row[12],
2289
+ supersededBy: row[13],
2290
+ supersedes: row[14],
2291
+ clusterId: row[15],
2292
+ extractedAt: row[16],
2293
+ approvedAt: row[17],
2294
+ approvedByUserId: row[18],
2295
+ edited: row[19],
2296
+ metadata: row[20]
2297
+ };
2298
+ });
2299
+ }
2300
+ exports.listThreadsByIDsQuery = `-- name: ListThreadsByIDs :many
2301
+ SELECT
2302
+ id,
2303
+ organization_id,
2304
+ document_id,
2305
+ ingestion_run_id,
2306
+ text,
2307
+ text_original,
2308
+ knowledge_type,
2309
+ facet_fields,
2310
+ confidence,
2311
+ temporal_validity,
2312
+ classification,
2313
+ retrieval_status,
2314
+ source_location,
2315
+ superseded_by,
2316
+ supersedes,
2317
+ cluster_id,
2318
+ extracted_at,
2319
+ approved_at,
2320
+ approved_by_user_id,
2321
+ edited,
2322
+ metadata
2323
+ FROM weave.threads
2324
+ WHERE organization_id = $1
2325
+ AND id = ANY($2::uuid[])
2326
+ ORDER BY extracted_at ASC, id ASC`;
2327
+ async function listThreadsByIDs(client, args) {
2328
+ const result = await client.query({
2329
+ text: exports.listThreadsByIDsQuery,
2330
+ values: [args.organizationId, args.threadIds],
2331
+ rowMode: "array"
2332
+ });
2333
+ return result.rows.map(row => {
2334
+ return {
2335
+ id: row[0],
2336
+ organizationId: row[1],
2337
+ documentId: row[2],
2338
+ ingestionRunId: row[3],
2339
+ text: row[4],
2340
+ textOriginal: row[5],
2341
+ knowledgeType: row[6],
2342
+ facetFields: row[7],
2343
+ confidence: row[8],
2344
+ temporalValidity: row[9],
2345
+ classification: row[10],
2346
+ retrievalStatus: row[11],
2347
+ sourceLocation: row[12],
2348
+ supersededBy: row[13],
2349
+ supersedes: row[14],
2350
+ clusterId: row[15],
2351
+ extractedAt: row[16],
2352
+ approvedAt: row[17],
2353
+ approvedByUserId: row[18],
2354
+ edited: row[19],
2355
+ metadata: row[20]
2356
+ };
2357
+ });
2358
+ }
2359
+ exports.getIngestionRunThreadCountsQuery = `-- name: GetIngestionRunThreadCounts :one
2360
+ SELECT
2361
+ COUNT(*)::int AS thread_count,
2362
+ COUNT(*) FILTER (
2363
+ WHERE retrieval_status = 'THREAD_RETRIEVAL_STATUS_PENDING'
2364
+ )::int AS pending_count,
2365
+ COUNT(*) FILTER (
2366
+ WHERE retrieval_status = 'THREAD_RETRIEVAL_STATUS_APPROVED'
2367
+ )::int AS approved_count,
2368
+ COUNT(*) FILTER (
2369
+ WHERE retrieval_status = 'THREAD_RETRIEVAL_STATUS_REJECTED'
2370
+ )::int AS rejected_count
2371
+ FROM weave.threads
2372
+ WHERE organization_id = $1
2373
+ AND ingestion_run_id = $2`;
2374
+ async function getIngestionRunThreadCounts(client, args) {
2375
+ const result = await client.query({
2376
+ text: exports.getIngestionRunThreadCountsQuery,
2377
+ values: [args.organizationId, args.ingestionRunId],
2378
+ rowMode: "array"
2379
+ });
2380
+ if (result.rows.length !== 1) {
2381
+ return null;
2382
+ }
2383
+ const row = result.rows[0];
2384
+ return {
2385
+ threadCount: row[0],
2386
+ pendingCount: row[1],
2387
+ approvedCount: row[2],
2388
+ rejectedCount: row[3]
2389
+ };
2390
+ }
2391
+ exports.deleteThreadReconciliationByPendingThreadQuery = `-- name: DeleteThreadReconciliationByPendingThread :exec
2392
+ DELETE FROM weave.thread_reconciliation tr
2393
+ USING weave.threads t
2394
+ WHERE tr.pending_thread_id = t.id
2395
+ AND t.organization_id = $1
2396
+ AND tr.pending_thread_id = $2`;
2397
+ async function deleteThreadReconciliationByPendingThread(client, args) {
2398
+ await client.query({
2399
+ text: exports.deleteThreadReconciliationByPendingThreadQuery,
2400
+ values: [args.organizationId, args.pendingThreadId],
2401
+ rowMode: "array"
2402
+ });
2403
+ }
2404
+ exports.upsertThreadReconciliationQuery = `-- name: UpsertThreadReconciliation :one
2405
+ INSERT INTO weave.thread_reconciliation (
2406
+ pending_thread_id,
2407
+ existing_thread_id,
2408
+ similarity,
2409
+ entity_overlap
2410
+ ) VALUES (
2411
+ $1,
2412
+ $2,
2413
+ $3,
2414
+ $4
2415
+ )
2416
+ ON CONFLICT (pending_thread_id, existing_thread_id) DO UPDATE
2417
+ SET
2418
+ similarity = EXCLUDED.similarity,
2419
+ entity_overlap = EXCLUDED.entity_overlap
2420
+ RETURNING
2421
+ pending_thread_id,
2422
+ existing_thread_id,
2423
+ similarity,
2424
+ entity_overlap`;
2425
+ async function upsertThreadReconciliation(client, args) {
2426
+ const result = await client.query({
2427
+ text: exports.upsertThreadReconciliationQuery,
2428
+ values: [args.pendingThreadId, args.existingThreadId, args.similarity, args.entityOverlap],
2429
+ rowMode: "array"
2430
+ });
2431
+ if (result.rows.length !== 1) {
2432
+ return null;
2433
+ }
2434
+ const row = result.rows[0];
2435
+ return {
2436
+ pendingThreadId: row[0],
2437
+ existingThreadId: row[1],
2438
+ similarity: row[2],
2439
+ entityOverlap: row[3]
2440
+ };
2441
+ }
2442
+ exports.listThreadReconciliationByPendingThreadQuery = `-- name: ListThreadReconciliationByPendingThread :many
2443
+ SELECT
2444
+ pending_thread_id,
2445
+ existing_thread_id,
2446
+ similarity,
2447
+ entity_overlap
2448
+ FROM weave.thread_reconciliation
2449
+ WHERE pending_thread_id IN (
2450
+ SELECT id
2451
+ FROM weave.threads
2452
+ WHERE organization_id = $1
2453
+ AND id = $2
2454
+ )
2455
+ ORDER BY similarity DESC, entity_overlap DESC, existing_thread_id ASC`;
2456
+ async function listThreadReconciliationByPendingThread(client, args) {
2457
+ const result = await client.query({
2458
+ text: exports.listThreadReconciliationByPendingThreadQuery,
2459
+ values: [args.organizationId, args.pendingThreadId],
2460
+ rowMode: "array"
2461
+ });
2462
+ return result.rows.map(row => {
2463
+ return {
2464
+ pendingThreadId: row[0],
2465
+ existingThreadId: row[1],
2466
+ similarity: row[2],
2467
+ entityOverlap: row[3]
2468
+ };
2469
+ });
2470
+ }
2471
+ exports.createThreadActivityQuery = `-- name: CreateThreadActivity :one
2472
+ INSERT INTO weave.thread_activity (
2473
+ id,
2474
+ thread_id,
2475
+ action,
2476
+ actor_id,
2477
+ metadata
2478
+ ) VALUES (
2479
+ $1,
2480
+ $2,
2481
+ $3,
2482
+ $4,
2483
+ $5
2484
+ )
2485
+ RETURNING
2486
+ id,
2487
+ thread_id,
2488
+ action,
2489
+ actor_id,
2490
+ metadata,
2491
+ created_at`;
2492
+ async function createThreadActivity(client, args) {
2493
+ const result = await client.query({
2494
+ text: exports.createThreadActivityQuery,
2495
+ values: [args.id, args.threadId, args.action, args.actorId, args.metadata],
2496
+ rowMode: "array"
2497
+ });
2498
+ if (result.rows.length !== 1) {
2499
+ return null;
2500
+ }
2501
+ const row = result.rows[0];
2502
+ return {
2503
+ id: row[0],
2504
+ threadId: row[1],
2505
+ action: row[2],
2506
+ actorId: row[3],
2507
+ metadata: row[4],
2508
+ createdAt: row[5]
2509
+ };
2510
+ }
2511
+ exports.listThreadActivityByThreadQuery = `-- name: ListThreadActivityByThread :many
2512
+ SELECT
2513
+ id,
2514
+ thread_id,
2515
+ action,
2516
+ actor_id,
2517
+ metadata,
2518
+ created_at
2519
+ FROM weave.thread_activity
2520
+ WHERE thread_id IN (
2521
+ SELECT t.id
2522
+ FROM weave.threads t
2523
+ WHERE t.organization_id = $1
2524
+ AND t.id = $2
2525
+ )
2526
+ ORDER BY created_at DESC`;
2527
+ async function listThreadActivityByThread(client, args) {
2528
+ const result = await client.query({
2529
+ text: exports.listThreadActivityByThreadQuery,
2530
+ values: [args.organizationId, args.threadId],
2531
+ rowMode: "array"
2532
+ });
2533
+ return result.rows.map(row => {
2534
+ return {
2535
+ id: row[0],
2536
+ threadId: row[1],
2537
+ action: row[2],
2538
+ actorId: row[3],
2539
+ metadata: row[4],
2540
+ createdAt: row[5]
2541
+ };
2542
+ });
2543
+ }
2544
+ exports.upsertEntityQuery = `-- name: UpsertEntity :one
2545
+ INSERT INTO weave.entities (
2546
+ id,
2547
+ organization_id,
2548
+ name,
2549
+ type,
2550
+ canonical,
2551
+ metadata
2552
+ ) VALUES (
2553
+ $1,
2554
+ $2,
2555
+ $3,
2556
+ $4,
2557
+ $5,
2558
+ $6
2559
+ )
2560
+ ON CONFLICT (organization_id, type, canonical) DO UPDATE
2561
+ SET
2562
+ name = EXCLUDED.name,
2563
+ metadata = EXCLUDED.metadata,
2564
+ updated_at = now()
2565
+ RETURNING
2566
+ id,
2567
+ organization_id,
2568
+ name,
2569
+ type,
2570
+ canonical,
2571
+ metadata,
2572
+ created_at,
2573
+ updated_at`;
2574
+ async function upsertEntity(client, args) {
2575
+ const result = await client.query({
2576
+ text: exports.upsertEntityQuery,
2577
+ values: [args.id, args.organizationId, args.name, args.type, args.canonical, args.metadata],
2578
+ rowMode: "array"
2579
+ });
2580
+ if (result.rows.length !== 1) {
2581
+ return null;
2582
+ }
2583
+ const row = result.rows[0];
2584
+ return {
2585
+ id: row[0],
2586
+ organizationId: row[1],
2587
+ name: row[2],
2588
+ type: row[3],
2589
+ canonical: row[4],
2590
+ metadata: row[5],
2591
+ createdAt: row[6],
2592
+ updatedAt: row[7]
2593
+ };
2594
+ }
2595
+ exports.upsertThreadEntityQuery = `-- name: UpsertThreadEntity :one
2596
+ INSERT INTO weave.thread_entities (
2597
+ thread_id,
2598
+ entity_id,
2599
+ role,
2600
+ confidence
2601
+ ) VALUES (
2602
+ $1,
2603
+ $2,
2604
+ $3,
2605
+ $4
2606
+ )
2607
+ ON CONFLICT (thread_id, entity_id) DO UPDATE
2608
+ SET
2609
+ role = EXCLUDED.role,
2610
+ confidence = EXCLUDED.confidence
2611
+ RETURNING
2612
+ thread_id,
2613
+ entity_id,
2614
+ role,
2615
+ confidence`;
2616
+ async function upsertThreadEntity(client, args) {
2617
+ const result = await client.query({
2618
+ text: exports.upsertThreadEntityQuery,
2619
+ values: [args.threadId, args.entityId, args.role, args.confidence],
2620
+ rowMode: "array"
2621
+ });
2622
+ if (result.rows.length !== 1) {
2623
+ return null;
2624
+ }
2625
+ const row = result.rows[0];
2626
+ return {
2627
+ threadId: row[0],
2628
+ entityId: row[1],
2629
+ role: row[2],
2630
+ confidence: row[3]
2631
+ };
2632
+ }
2633
+ exports.upsertEntityRelationshipQuery = `-- name: UpsertEntityRelationship :one
2634
+ INSERT INTO weave.entity_relationships (
2635
+ id,
2636
+ organization_id,
2637
+ source_entity_id,
2638
+ target_entity_id,
2639
+ relationship,
2640
+ source_thread_id,
2641
+ confidence
2642
+ ) VALUES (
2643
+ $1,
2644
+ $2,
2645
+ $3,
2646
+ $4,
2647
+ $5,
2648
+ $6,
2649
+ $7
2650
+ )
2651
+ ON CONFLICT (source_entity_id, target_entity_id, relationship, source_thread_id) DO UPDATE
2652
+ SET
2653
+ confidence = EXCLUDED.confidence
2654
+ RETURNING
2655
+ id,
2656
+ organization_id,
2657
+ source_entity_id,
2658
+ target_entity_id,
2659
+ relationship,
2660
+ source_thread_id,
2661
+ confidence,
2662
+ created_at`;
2663
+ async function upsertEntityRelationship(client, args) {
2664
+ const result = await client.query({
2665
+ text: exports.upsertEntityRelationshipQuery,
2666
+ values: [args.id, args.organizationId, args.sourceEntityId, args.targetEntityId, args.relationship, args.sourceThreadId, args.confidence],
2667
+ rowMode: "array"
2668
+ });
2669
+ if (result.rows.length !== 1) {
2670
+ return null;
2671
+ }
2672
+ const row = result.rows[0];
2673
+ return {
2674
+ id: row[0],
2675
+ organizationId: row[1],
2676
+ sourceEntityId: row[2],
2677
+ targetEntityId: row[3],
2678
+ relationship: row[4],
2679
+ sourceThreadId: row[5],
2680
+ confidence: row[6],
2681
+ createdAt: row[7]
2682
+ };
2683
+ }
2684
+ exports.listEntitiesByThreadQuery = `-- name: ListEntitiesByThread :many
2685
+ SELECT
2686
+ e.id,
2687
+ e.organization_id,
2688
+ e.name,
2689
+ e.type,
2690
+ e.canonical,
2691
+ e.metadata,
2692
+ e.created_at,
2693
+ e.updated_at,
2694
+ te.role,
2695
+ te.confidence AS relationship_confidence
2696
+ FROM weave.thread_entities te
2697
+ JOIN weave.entities e ON e.id = te.entity_id
2698
+ WHERE te.thread_id IN (
2699
+ SELECT t.id
2700
+ FROM weave.threads t
2701
+ WHERE t.organization_id = $1
2702
+ AND t.id = $2
2703
+ )
2704
+ ORDER BY e.type ASC, e.canonical ASC`;
2705
+ async function listEntitiesByThread(client, args) {
2706
+ const result = await client.query({
2707
+ text: exports.listEntitiesByThreadQuery,
2708
+ values: [args.organizationId, args.threadId],
2709
+ rowMode: "array"
2710
+ });
2711
+ return result.rows.map(row => {
2712
+ return {
2713
+ id: row[0],
2714
+ organizationId: row[1],
2715
+ name: row[2],
2716
+ type: row[3],
2717
+ canonical: row[4],
2718
+ metadata: row[5],
2719
+ createdAt: row[6],
2720
+ updatedAt: row[7],
2721
+ role: row[8],
2722
+ relationshipConfidence: row[9]
2723
+ };
2724
+ });
2725
+ }
2726
+ exports.ensureOrganizationIngestionSettingsQuery = `-- name: EnsureOrganizationIngestionSettings :one
2727
+ WITH upsert AS (
2728
+ INSERT INTO weave.org_ingestion_settings (organization_id)
2729
+ VALUES ($1)
2730
+ ON CONFLICT (organization_id) DO UPDATE
2731
+ SET organization_id = EXCLUDED.organization_id
2732
+ RETURNING
2733
+ organization_id,
2734
+ reasoning_model_id,
2735
+ reasoning_provider_configuration_id,
2736
+ embedding_model_id,
2737
+ embedding_provider_configuration_id,
2738
+ classify_model_id,
2739
+ classify_provider_configuration_id,
2740
+ create_thread_model_id,
2741
+ create_thread_provider_configuration_id,
2742
+ extract_model_id,
2743
+ extract_provider_configuration_id,
2744
+ embed_model_id,
2745
+ embed_provider_configuration_id,
2746
+ reconcile_model_id,
2747
+ reconcile_provider_configuration_id,
2748
+ diff_model_id,
2749
+ diff_provider_configuration_id,
2750
+ create_thread_loop_count,
2751
+ updated_at
2752
+ )
2753
+ SELECT
2754
+ upsert.organization_id,
2755
+ upsert.reasoning_model_id,
2756
+ upsert.reasoning_provider_configuration_id,
2757
+ upsert.embedding_model_id,
2758
+ upsert.embedding_provider_configuration_id,
2759
+ upsert.classify_model_id,
2760
+ upsert.classify_provider_configuration_id,
2761
+ upsert.create_thread_model_id,
2762
+ upsert.create_thread_provider_configuration_id,
2763
+ upsert.extract_model_id,
2764
+ upsert.extract_provider_configuration_id,
2765
+ upsert.embed_model_id,
2766
+ upsert.embed_provider_configuration_id,
2767
+ upsert.reconcile_model_id,
2768
+ upsert.reconcile_provider_configuration_id,
2769
+ upsert.diff_model_id,
2770
+ upsert.diff_provider_configuration_id,
2771
+ cfg.embedding_dimension AS embedding_dimensions,
2772
+ cfg.embedding_dimension AS embed_dimensions,
2773
+ upsert.create_thread_loop_count,
2774
+ upsert.updated_at
2775
+ FROM upsert
2776
+ JOIN weave.database_config cfg
2777
+ ON cfg.id = 1`;
2778
+ async function ensureOrganizationIngestionSettings(client, args) {
2779
+ const result = await client.query({
2780
+ text: exports.ensureOrganizationIngestionSettingsQuery,
2781
+ values: [args.organizationId],
2782
+ rowMode: "array"
2783
+ });
2784
+ if (result.rows.length !== 1) {
2785
+ return null;
2786
+ }
2787
+ const row = result.rows[0];
2788
+ return {
2789
+ organizationId: row[0],
2790
+ reasoningModelId: row[1],
2791
+ reasoningProviderConfigurationId: row[2],
2792
+ embeddingModelId: row[3],
2793
+ embeddingProviderConfigurationId: row[4],
2794
+ classifyModelId: row[5],
2795
+ classifyProviderConfigurationId: row[6],
2796
+ createThreadModelId: row[7],
2797
+ createThreadProviderConfigurationId: row[8],
2798
+ extractModelId: row[9],
2799
+ extractProviderConfigurationId: row[10],
2800
+ embedModelId: row[11],
2801
+ embedProviderConfigurationId: row[12],
2802
+ reconcileModelId: row[13],
2803
+ reconcileProviderConfigurationId: row[14],
2804
+ diffModelId: row[15],
2805
+ diffProviderConfigurationId: row[16],
2806
+ embeddingDimensions: row[17],
2807
+ embedDimensions: row[18],
2808
+ createThreadLoopCount: row[19],
2809
+ updatedAt: row[20]
2810
+ };
2811
+ }
2812
+ exports.getOrganizationIngestionSettingsQuery = `-- name: GetOrganizationIngestionSettings :one
2813
+ SELECT
2814
+ settings.organization_id,
2815
+ settings.reasoning_model_id,
2816
+ settings.reasoning_provider_configuration_id,
2817
+ settings.embedding_model_id,
2818
+ settings.embedding_provider_configuration_id,
2819
+ settings.classify_model_id,
2820
+ settings.classify_provider_configuration_id,
2821
+ settings.create_thread_model_id,
2822
+ settings.create_thread_provider_configuration_id,
2823
+ settings.extract_model_id,
2824
+ settings.extract_provider_configuration_id,
2825
+ settings.embed_model_id,
2826
+ settings.embed_provider_configuration_id,
2827
+ settings.reconcile_model_id,
2828
+ settings.reconcile_provider_configuration_id,
2829
+ settings.diff_model_id,
2830
+ settings.diff_provider_configuration_id,
2831
+ cfg.embedding_dimension AS embedding_dimensions,
2832
+ cfg.embedding_dimension AS embed_dimensions,
2833
+ settings.create_thread_loop_count,
2834
+ settings.updated_at
2835
+ FROM weave.org_ingestion_settings settings
2836
+ JOIN weave.database_config cfg
2837
+ ON cfg.id = 1
2838
+ WHERE settings.organization_id = $1`;
2839
+ async function getOrganizationIngestionSettings(client, args) {
2840
+ const result = await client.query({
2841
+ text: exports.getOrganizationIngestionSettingsQuery,
2842
+ values: [args.organizationId],
2843
+ rowMode: "array"
2844
+ });
2845
+ if (result.rows.length !== 1) {
2846
+ return null;
2847
+ }
2848
+ const row = result.rows[0];
2849
+ return {
2850
+ organizationId: row[0],
2851
+ reasoningModelId: row[1],
2852
+ reasoningProviderConfigurationId: row[2],
2853
+ embeddingModelId: row[3],
2854
+ embeddingProviderConfigurationId: row[4],
2855
+ classifyModelId: row[5],
2856
+ classifyProviderConfigurationId: row[6],
2857
+ createThreadModelId: row[7],
2858
+ createThreadProviderConfigurationId: row[8],
2859
+ extractModelId: row[9],
2860
+ extractProviderConfigurationId: row[10],
2861
+ embedModelId: row[11],
2862
+ embedProviderConfigurationId: row[12],
2863
+ reconcileModelId: row[13],
2864
+ reconcileProviderConfigurationId: row[14],
2865
+ diffModelId: row[15],
2866
+ diffProviderConfigurationId: row[16],
2867
+ embeddingDimensions: row[17],
2868
+ embedDimensions: row[18],
2869
+ createThreadLoopCount: row[19],
2870
+ updatedAt: row[20]
2871
+ };
2872
+ }
2873
+ exports.upsertOrganizationIngestionSettingsQuery = `-- name: UpsertOrganizationIngestionSettings :one
2874
+ WITH cfg AS (
2875
+ SELECT embedding_dimension
2876
+ FROM weave.database_config
2877
+ WHERE id = 1
2878
+ AND ($1 = 0 OR $1 = embedding_dimension)
2879
+ ), upsert AS (
2880
+ INSERT INTO weave.org_ingestion_settings (
2881
+ organization_id,
2882
+ reasoning_model_id,
2883
+ reasoning_provider_configuration_id,
2884
+ embedding_model_id,
2885
+ embedding_provider_configuration_id,
2886
+ classify_model_id,
2887
+ classify_provider_configuration_id,
2888
+ create_thread_model_id,
2889
+ create_thread_provider_configuration_id,
2890
+ extract_model_id,
2891
+ extract_provider_configuration_id,
2892
+ embed_model_id,
2893
+ embed_provider_configuration_id,
2894
+ reconcile_model_id,
2895
+ reconcile_provider_configuration_id,
2896
+ diff_model_id,
2897
+ diff_provider_configuration_id
2898
+ )
2899
+ SELECT
2900
+ $2,
2901
+ $3,
2902
+ $4,
2903
+ $5,
2904
+ $6,
2905
+ $3,
2906
+ $4,
2907
+ $3,
2908
+ $4,
2909
+ $3,
2910
+ $4,
2911
+ $5,
2912
+ $6,
2913
+ $3,
2914
+ $4,
2915
+ $3,
2916
+ $4
2917
+ FROM cfg
2918
+ ON CONFLICT (organization_id) DO UPDATE
2919
+ SET
2920
+ reasoning_model_id = EXCLUDED.reasoning_model_id,
2921
+ reasoning_provider_configuration_id = EXCLUDED.reasoning_provider_configuration_id,
2922
+ embedding_model_id = EXCLUDED.embedding_model_id,
2923
+ embedding_provider_configuration_id = EXCLUDED.embedding_provider_configuration_id,
2924
+ classify_model_id = EXCLUDED.classify_model_id,
2925
+ classify_provider_configuration_id = EXCLUDED.classify_provider_configuration_id,
2926
+ create_thread_model_id = EXCLUDED.create_thread_model_id,
2927
+ create_thread_provider_configuration_id = EXCLUDED.create_thread_provider_configuration_id,
2928
+ extract_model_id = EXCLUDED.extract_model_id,
2929
+ extract_provider_configuration_id = EXCLUDED.extract_provider_configuration_id,
2930
+ embed_model_id = EXCLUDED.embed_model_id,
2931
+ embed_provider_configuration_id = EXCLUDED.embed_provider_configuration_id,
2932
+ reconcile_model_id = EXCLUDED.reconcile_model_id,
2933
+ reconcile_provider_configuration_id = EXCLUDED.reconcile_provider_configuration_id,
2934
+ diff_model_id = EXCLUDED.diff_model_id,
2935
+ diff_provider_configuration_id = EXCLUDED.diff_provider_configuration_id,
2936
+ updated_at = now()
2937
+ RETURNING
2938
+ organization_id,
2939
+ reasoning_model_id,
2940
+ reasoning_provider_configuration_id,
2941
+ embedding_model_id,
2942
+ embedding_provider_configuration_id,
2943
+ classify_model_id,
2944
+ classify_provider_configuration_id,
2945
+ create_thread_model_id,
2946
+ create_thread_provider_configuration_id,
2947
+ extract_model_id,
2948
+ extract_provider_configuration_id,
2949
+ embed_model_id,
2950
+ embed_provider_configuration_id,
2951
+ reconcile_model_id,
2952
+ reconcile_provider_configuration_id,
2953
+ diff_model_id,
2954
+ diff_provider_configuration_id,
2955
+ create_thread_loop_count,
2956
+ updated_at
2957
+ )
2958
+ SELECT
2959
+ upsert.organization_id,
2960
+ upsert.reasoning_model_id,
2961
+ upsert.reasoning_provider_configuration_id,
2962
+ upsert.embedding_model_id,
2963
+ upsert.embedding_provider_configuration_id,
2964
+ upsert.classify_model_id,
2965
+ upsert.classify_provider_configuration_id,
2966
+ upsert.create_thread_model_id,
2967
+ upsert.create_thread_provider_configuration_id,
2968
+ upsert.extract_model_id,
2969
+ upsert.extract_provider_configuration_id,
2970
+ upsert.embed_model_id,
2971
+ upsert.embed_provider_configuration_id,
2972
+ upsert.reconcile_model_id,
2973
+ upsert.reconcile_provider_configuration_id,
2974
+ upsert.diff_model_id,
2975
+ upsert.diff_provider_configuration_id,
2976
+ cfg.embedding_dimension AS embedding_dimensions,
2977
+ cfg.embedding_dimension AS embed_dimensions,
2978
+ upsert.create_thread_loop_count,
2979
+ upsert.updated_at
2980
+ FROM upsert
2981
+ JOIN cfg
2982
+ ON TRUE`;
2983
+ async function upsertOrganizationIngestionSettings(client, args) {
2984
+ const result = await client.query({
2985
+ text: exports.upsertOrganizationIngestionSettingsQuery,
2986
+ values: [args.embeddingDimensions, args.organizationId, args.reasoningModelId, args.reasoningProviderConfigurationId, args.embeddingModelId, args.embeddingProviderConfigurationId],
2987
+ rowMode: "array"
2988
+ });
2989
+ if (result.rows.length !== 1) {
2990
+ return null;
2991
+ }
2992
+ const row = result.rows[0];
2993
+ return {
2994
+ organizationId: row[0],
2995
+ reasoningModelId: row[1],
2996
+ reasoningProviderConfigurationId: row[2],
2997
+ embeddingModelId: row[3],
2998
+ embeddingProviderConfigurationId: row[4],
2999
+ classifyModelId: row[5],
3000
+ classifyProviderConfigurationId: row[6],
3001
+ createThreadModelId: row[7],
3002
+ createThreadProviderConfigurationId: row[8],
3003
+ extractModelId: row[9],
3004
+ extractProviderConfigurationId: row[10],
3005
+ embedModelId: row[11],
3006
+ embedProviderConfigurationId: row[12],
3007
+ reconcileModelId: row[13],
3008
+ reconcileProviderConfigurationId: row[14],
3009
+ diffModelId: row[15],
3010
+ diffProviderConfigurationId: row[16],
3011
+ embeddingDimensions: row[17],
3012
+ embedDimensions: row[18],
3013
+ createThreadLoopCount: row[19],
3014
+ updatedAt: row[20]
3015
+ };
3016
+ }
3017
+ exports.upsertOrganizationIngestionStageSettingsQuery = `-- name: UpsertOrganizationIngestionStageSettings :one
3018
+ WITH cfg AS (
3019
+ SELECT embedding_dimension
3020
+ FROM weave.database_config
3021
+ WHERE id = 1
3022
+ AND ($1 = 0 OR $1 = embedding_dimension)
3023
+ ), upsert AS (
3024
+ INSERT INTO weave.org_ingestion_settings (
3025
+ organization_id,
3026
+ classify_model_id,
3027
+ classify_provider_configuration_id,
3028
+ create_thread_model_id,
3029
+ create_thread_provider_configuration_id,
3030
+ extract_model_id,
3031
+ extract_provider_configuration_id,
3032
+ embed_model_id,
3033
+ embed_provider_configuration_id,
3034
+ reconcile_model_id,
3035
+ reconcile_provider_configuration_id,
3036
+ diff_model_id,
3037
+ diff_provider_configuration_id,
3038
+ create_thread_loop_count
3039
+ )
3040
+ SELECT
3041
+ $2,
3042
+ $3,
3043
+ $4,
3044
+ $5,
3045
+ $6,
3046
+ $7,
3047
+ $8,
3048
+ $9,
3049
+ $10,
3050
+ $11,
3051
+ $12,
3052
+ $13,
3053
+ $14,
3054
+ $15
3055
+ FROM cfg
3056
+ ON CONFLICT (organization_id) DO UPDATE
3057
+ SET
3058
+ classify_model_id = EXCLUDED.classify_model_id,
3059
+ classify_provider_configuration_id = EXCLUDED.classify_provider_configuration_id,
3060
+ create_thread_model_id = EXCLUDED.create_thread_model_id,
3061
+ create_thread_provider_configuration_id = EXCLUDED.create_thread_provider_configuration_id,
3062
+ extract_model_id = EXCLUDED.extract_model_id,
3063
+ extract_provider_configuration_id = EXCLUDED.extract_provider_configuration_id,
3064
+ embed_model_id = EXCLUDED.embed_model_id,
3065
+ embed_provider_configuration_id = EXCLUDED.embed_provider_configuration_id,
3066
+ reconcile_model_id = EXCLUDED.reconcile_model_id,
3067
+ reconcile_provider_configuration_id = EXCLUDED.reconcile_provider_configuration_id,
3068
+ diff_model_id = EXCLUDED.diff_model_id,
3069
+ diff_provider_configuration_id = EXCLUDED.diff_provider_configuration_id,
3070
+ create_thread_loop_count = EXCLUDED.create_thread_loop_count,
3071
+ updated_at = now()
3072
+ RETURNING
3073
+ organization_id,
3074
+ classify_model_id,
3075
+ classify_provider_configuration_id,
3076
+ create_thread_model_id,
3077
+ create_thread_provider_configuration_id,
3078
+ extract_model_id,
3079
+ extract_provider_configuration_id,
3080
+ embed_model_id,
3081
+ embed_provider_configuration_id,
3082
+ reconcile_model_id,
3083
+ reconcile_provider_configuration_id,
3084
+ diff_model_id,
3085
+ diff_provider_configuration_id,
3086
+ create_thread_loop_count,
3087
+ updated_at
3088
+ )
3089
+ SELECT
3090
+ upsert.organization_id,
3091
+ upsert.classify_model_id,
3092
+ upsert.classify_provider_configuration_id,
3093
+ upsert.create_thread_model_id,
3094
+ upsert.create_thread_provider_configuration_id,
3095
+ upsert.extract_model_id,
3096
+ upsert.extract_provider_configuration_id,
3097
+ upsert.embed_model_id,
3098
+ upsert.embed_provider_configuration_id,
3099
+ upsert.reconcile_model_id,
3100
+ upsert.reconcile_provider_configuration_id,
3101
+ upsert.diff_model_id,
3102
+ upsert.diff_provider_configuration_id,
3103
+ cfg.embedding_dimension AS embedding_dimensions,
3104
+ cfg.embedding_dimension AS embed_dimensions,
3105
+ upsert.create_thread_loop_count,
3106
+ upsert.updated_at
3107
+ FROM upsert
3108
+ JOIN cfg
3109
+ ON TRUE`;
3110
+ async function upsertOrganizationIngestionStageSettings(client, args) {
3111
+ const result = await client.query({
3112
+ text: exports.upsertOrganizationIngestionStageSettingsQuery,
3113
+ values: [args.embedDimensions, args.organizationId, args.classifyModelId, args.classifyProviderConfigurationId, args.createThreadModelId, args.createThreadProviderConfigurationId, args.extractModelId, args.extractProviderConfigurationId, args.embedModelId, args.embedProviderConfigurationId, args.reconcileModelId, args.reconcileProviderConfigurationId, args.diffModelId, args.diffProviderConfigurationId, args.createThreadLoopCount],
3114
+ rowMode: "array"
3115
+ });
3116
+ if (result.rows.length !== 1) {
3117
+ return null;
3118
+ }
3119
+ const row = result.rows[0];
3120
+ return {
3121
+ organizationId: row[0],
3122
+ classifyModelId: row[1],
3123
+ classifyProviderConfigurationId: row[2],
3124
+ createThreadModelId: row[3],
3125
+ createThreadProviderConfigurationId: row[4],
3126
+ extractModelId: row[5],
3127
+ extractProviderConfigurationId: row[6],
3128
+ embedModelId: row[7],
3129
+ embedProviderConfigurationId: row[8],
3130
+ reconcileModelId: row[9],
3131
+ reconcileProviderConfigurationId: row[10],
3132
+ diffModelId: row[11],
3133
+ diffProviderConfigurationId: row[12],
3134
+ embeddingDimensions: row[13],
3135
+ embedDimensions: row[14],
3136
+ createThreadLoopCount: row[15],
3137
+ updatedAt: row[16]
3138
+ };
3139
+ }
3140
+ exports.syncOrganizationIngestionReasoningDefaultsQuery = `-- name: SyncOrganizationIngestionReasoningDefaults :one
3141
+ WITH cfg AS (
3142
+ SELECT embedding_dimension
3143
+ FROM weave.database_config
3144
+ WHERE id = 1
3145
+ ), upsert AS (
3146
+ INSERT INTO weave.org_ingestion_settings (organization_id)
3147
+ VALUES ($1)
3148
+ ON CONFLICT (organization_id) DO UPDATE
3149
+ SET updated_at = weave.org_ingestion_settings.updated_at
3150
+ RETURNING
3151
+ organization_id,
3152
+ reasoning_model_id,
3153
+ reasoning_provider_configuration_id,
3154
+ embedding_model_id,
3155
+ embedding_provider_configuration_id,
3156
+ classify_model_id,
3157
+ classify_provider_configuration_id,
3158
+ create_thread_model_id,
3159
+ create_thread_provider_configuration_id,
3160
+ extract_model_id,
3161
+ extract_provider_configuration_id,
3162
+ embed_model_id,
3163
+ embed_provider_configuration_id,
3164
+ reconcile_model_id,
3165
+ reconcile_provider_configuration_id,
3166
+ diff_model_id,
3167
+ diff_provider_configuration_id,
3168
+ create_thread_loop_count,
3169
+ updated_at
3170
+ )
3171
+ SELECT
3172
+ upsert.organization_id,
3173
+ upsert.reasoning_model_id,
3174
+ upsert.reasoning_provider_configuration_id,
3175
+ upsert.embedding_model_id,
3176
+ upsert.embedding_provider_configuration_id,
3177
+ upsert.classify_model_id,
3178
+ upsert.classify_provider_configuration_id,
3179
+ upsert.create_thread_model_id,
3180
+ upsert.create_thread_provider_configuration_id,
3181
+ upsert.extract_model_id,
3182
+ upsert.extract_provider_configuration_id,
3183
+ upsert.embed_model_id,
3184
+ upsert.embed_provider_configuration_id,
3185
+ upsert.reconcile_model_id,
3186
+ upsert.reconcile_provider_configuration_id,
3187
+ upsert.diff_model_id,
3188
+ upsert.diff_provider_configuration_id,
3189
+ cfg.embedding_dimension AS embedding_dimensions,
3190
+ cfg.embedding_dimension AS embed_dimensions,
3191
+ upsert.create_thread_loop_count,
3192
+ upsert.updated_at
3193
+ FROM upsert
3194
+ JOIN cfg
3195
+ ON TRUE`;
3196
+ async function syncOrganizationIngestionReasoningDefaults(client, args) {
3197
+ const result = await client.query({
3198
+ text: exports.syncOrganizationIngestionReasoningDefaultsQuery,
3199
+ values: [args.organizationId],
3200
+ rowMode: "array"
3201
+ });
3202
+ if (result.rows.length !== 1) {
3203
+ return null;
3204
+ }
3205
+ const row = result.rows[0];
3206
+ return {
3207
+ organizationId: row[0],
3208
+ reasoningModelId: row[1],
3209
+ reasoningProviderConfigurationId: row[2],
3210
+ embeddingModelId: row[3],
3211
+ embeddingProviderConfigurationId: row[4],
3212
+ classifyModelId: row[5],
3213
+ classifyProviderConfigurationId: row[6],
3214
+ createThreadModelId: row[7],
3215
+ createThreadProviderConfigurationId: row[8],
3216
+ extractModelId: row[9],
3217
+ extractProviderConfigurationId: row[10],
3218
+ embedModelId: row[11],
3219
+ embedProviderConfigurationId: row[12],
3220
+ reconcileModelId: row[13],
3221
+ reconcileProviderConfigurationId: row[14],
3222
+ diffModelId: row[15],
3223
+ diffProviderConfigurationId: row[16],
3224
+ embeddingDimensions: row[17],
3225
+ embedDimensions: row[18],
3226
+ createThreadLoopCount: row[19],
3227
+ updatedAt: row[20]
3228
+ };
3229
+ }
3230
+ exports.listThreadFacetsByThreadIDsQuery = `-- name: ListThreadFacetsByThreadIDs :many
3231
+ SELECT
3232
+ thread_id,
3233
+ organization_id,
3234
+ knowledge_type,
3235
+ score,
3236
+ facet_fields,
3237
+ rationale,
3238
+ is_primary,
3239
+ created_at,
3240
+ updated_at
3241
+ FROM weave.thread_facets
3242
+ WHERE organization_id = $1
3243
+ AND thread_id = ANY($2::uuid[])
3244
+ ORDER BY thread_id ASC, is_primary DESC, score DESC, knowledge_type ASC`;
3245
+ async function listThreadFacetsByThreadIDs(client, args) {
3246
+ const result = await client.query({
3247
+ text: exports.listThreadFacetsByThreadIDsQuery,
3248
+ values: [args.organizationId, args.threadIds],
3249
+ rowMode: "array"
3250
+ });
3251
+ return result.rows.map(row => {
3252
+ return {
3253
+ threadId: row[0],
3254
+ organizationId: row[1],
3255
+ knowledgeType: row[2],
3256
+ score: row[3],
3257
+ facetFields: row[4],
3258
+ rationale: row[5],
3259
+ isPrimary: row[6],
3260
+ createdAt: row[7],
3261
+ updatedAt: row[8]
3262
+ };
3263
+ });
3264
+ }
3265
+ exports.upsertThreadFacetQuery = `-- name: UpsertThreadFacet :one
3266
+ WITH scoped_thread AS (
3267
+ SELECT t.id
3268
+ FROM weave.threads t
3269
+ WHERE t.organization_id = $1
3270
+ AND t.id = $2
3271
+ ), clear_primary AS (
3272
+ UPDATE weave.thread_facets tf
3273
+ SET is_primary = FALSE,
3274
+ updated_at = now()
3275
+ WHERE tf.organization_id = $1
3276
+ AND tf.thread_id = $2
3277
+ AND $3::boolean
3278
+ RETURNING tf.thread_id
3279
+ ), upsert AS (
3280
+ INSERT INTO weave.thread_facets (
3281
+ thread_id,
3282
+ organization_id,
3283
+ knowledge_type,
3284
+ score,
3285
+ facet_fields,
3286
+ rationale,
3287
+ is_primary
3288
+ )
3289
+ SELECT
3290
+ scoped_thread.id,
3291
+ $1,
3292
+ $4,
3293
+ $5,
3294
+ $6,
3295
+ $7,
3296
+ $3
3297
+ FROM scoped_thread
3298
+ ON CONFLICT (thread_id, knowledge_type) DO UPDATE
3299
+ SET
3300
+ score = EXCLUDED.score,
3301
+ facet_fields = EXCLUDED.facet_fields,
3302
+ rationale = EXCLUDED.rationale,
3303
+ is_primary = EXCLUDED.is_primary,
3304
+ updated_at = now()
3305
+ RETURNING
3306
+ thread_id,
3307
+ organization_id,
3308
+ knowledge_type,
3309
+ score,
3310
+ facet_fields,
3311
+ rationale,
3312
+ is_primary,
3313
+ created_at,
3314
+ updated_at
3315
+ )
3316
+ SELECT
3317
+ thread_id,
3318
+ organization_id,
3319
+ knowledge_type,
3320
+ score,
3321
+ facet_fields,
3322
+ rationale,
3323
+ is_primary,
3324
+ created_at,
3325
+ updated_at
3326
+ FROM upsert`;
3327
+ async function upsertThreadFacet(client, args) {
3328
+ const result = await client.query({
3329
+ text: exports.upsertThreadFacetQuery,
3330
+ values: [args.organizationId, args.threadId, args.isPrimary, args.knowledgeType, args.score, args.facetFields, args.rationale],
3331
+ rowMode: "array"
3332
+ });
3333
+ if (result.rows.length !== 1) {
3334
+ return null;
3335
+ }
3336
+ const row = result.rows[0];
3337
+ return {
3338
+ threadId: row[0],
3339
+ organizationId: row[1],
3340
+ knowledgeType: row[2],
3341
+ score: row[3],
3342
+ facetFields: row[4],
3343
+ rationale: row[5],
3344
+ isPrimary: row[6],
3345
+ createdAt: row[7],
3346
+ updatedAt: row[8]
3347
+ };
3348
+ }
3349
+ exports.deleteThreadFacetsByThreadQuery = `-- name: DeleteThreadFacetsByThread :execrows
3350
+ DELETE FROM weave.thread_facets
3351
+ WHERE organization_id = $1
3352
+ AND thread_id = $2`;
3353
+ exports.listFacetConfigsQuery = `-- name: ListFacetConfigs :many
3354
+ SELECT
3355
+ organization_id,
3356
+ knowledge_type,
3357
+ label,
3358
+ extraction_hint,
3359
+ required_fields,
3360
+ active
3361
+ FROM weave.org_facet_config
3362
+ WHERE organization_id = $1
3363
+ ORDER BY knowledge_type ASC`;
3364
+ async function listFacetConfigs(client, args) {
3365
+ const result = await client.query({
3366
+ text: exports.listFacetConfigsQuery,
3367
+ values: [args.organizationId],
3368
+ rowMode: "array"
3369
+ });
3370
+ return result.rows.map(row => {
3371
+ return {
3372
+ organizationId: row[0],
3373
+ knowledgeType: row[1],
3374
+ label: row[2],
3375
+ extractionHint: row[3],
3376
+ requiredFields: row[4],
3377
+ active: row[5]
3378
+ };
3379
+ });
3380
+ }
3381
+ exports.listActiveFacetConfigsQuery = `-- name: ListActiveFacetConfigs :many
3382
+ SELECT
3383
+ organization_id,
3384
+ knowledge_type,
3385
+ label,
3386
+ extraction_hint,
3387
+ required_fields,
3388
+ active
3389
+ FROM weave.org_facet_config
3390
+ WHERE organization_id = $1
3391
+ AND active = TRUE
3392
+ ORDER BY knowledge_type ASC`;
3393
+ async function listActiveFacetConfigs(client, args) {
3394
+ const result = await client.query({
3395
+ text: exports.listActiveFacetConfigsQuery,
3396
+ values: [args.organizationId],
3397
+ rowMode: "array"
3398
+ });
3399
+ return result.rows.map(row => {
3400
+ return {
3401
+ organizationId: row[0],
3402
+ knowledgeType: row[1],
3403
+ label: row[2],
3404
+ extractionHint: row[3],
3405
+ requiredFields: row[4],
3406
+ active: row[5]
3407
+ };
3408
+ });
3409
+ }
3410
+ exports.upsertFacetConfigQuery = `-- name: UpsertFacetConfig :one
3411
+ INSERT INTO weave.org_facet_config (
3412
+ organization_id,
3413
+ knowledge_type,
3414
+ label,
3415
+ extraction_hint,
3416
+ required_fields,
3417
+ active
3418
+ ) VALUES (
3419
+ $1,
3420
+ $2,
3421
+ $3,
3422
+ $4,
3423
+ $5,
3424
+ $6
3425
+ )
3426
+ ON CONFLICT (organization_id, knowledge_type) DO UPDATE
3427
+ SET
3428
+ label = EXCLUDED.label,
3429
+ extraction_hint = EXCLUDED.extraction_hint,
3430
+ required_fields = EXCLUDED.required_fields,
3431
+ active = EXCLUDED.active
3432
+ RETURNING
3433
+ organization_id,
3434
+ knowledge_type,
3435
+ label,
3436
+ extraction_hint,
3437
+ required_fields,
3438
+ active`;
3439
+ async function upsertFacetConfig(client, args) {
3440
+ const result = await client.query({
3441
+ text: exports.upsertFacetConfigQuery,
3442
+ values: [args.organizationId, args.knowledgeType, args.label, args.extractionHint, args.requiredFields, args.active],
3443
+ rowMode: "array"
3444
+ });
3445
+ if (result.rows.length !== 1) {
3446
+ return null;
3447
+ }
3448
+ const row = result.rows[0];
3449
+ return {
3450
+ organizationId: row[0],
3451
+ knowledgeType: row[1],
3452
+ label: row[2],
3453
+ extractionHint: row[3],
3454
+ requiredFields: row[4],
3455
+ active: row[5]
3456
+ };
3457
+ }
3458
+ exports.deactivateFacetConfigQuery = `-- name: DeactivateFacetConfig :one
3459
+ UPDATE weave.org_facet_config
3460
+ SET active = FALSE
3461
+ WHERE organization_id = $1
3462
+ AND knowledge_type = $2
3463
+ RETURNING
3464
+ organization_id,
3465
+ knowledge_type,
3466
+ label,
3467
+ extraction_hint,
3468
+ required_fields,
3469
+ active`;
3470
+ async function deactivateFacetConfig(client, args) {
3471
+ const result = await client.query({
3472
+ text: exports.deactivateFacetConfigQuery,
3473
+ values: [args.organizationId, args.knowledgeType],
3474
+ rowMode: "array"
3475
+ });
3476
+ if (result.rows.length !== 1) {
3477
+ return null;
3478
+ }
3479
+ const row = result.rows[0];
3480
+ return {
3481
+ organizationId: row[0],
3482
+ knowledgeType: row[1],
3483
+ label: row[2],
3484
+ extractionHint: row[3],
3485
+ requiredFields: row[4],
3486
+ active: row[5]
3487
+ };
3488
+ }