weave-typescript 0.20.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,735 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.listChatArtifactVersionsQuery = exports.getChatArtifactVersionQuery = exports.insertChatArtifactVersionQuery = exports.getChatArtifactQuery = exports.listChatArtifactsForSessionQuery = exports.bumpChatArtifactVersionQuery = exports.createChatArtifactQuery = exports.clearOrganizationChatSettingsDefaultIfMatchesQuery = exports.getOrganizationChatSettingsQuery = exports.upsertOrganizationChatSettingsQuery = exports.getOrganizationApprovedModelQuery = exports.listOrganizationApprovedModelsQuery = exports.deleteOrganizationApprovedModelQuery = exports.createOrganizationApprovedModelQuery = exports.completeChatMessageQuery = exports.updateChatMessageToolCallStateQuery = exports.listChatMessagesForSessionQuery = exports.getChatMessageQuery = exports.insertChatMessageQuery = exports.listProjectsForChatSessionQuery = exports.detachProjectFromChatSessionQuery = exports.attachProjectToChatSessionQuery = exports.softDeleteChatSessionQuery = exports.touchChatSessionLastMessageQuery = exports.updateChatSessionTitleQuery = exports.updateChatSessionModelQuery = exports.listChatSessionsForUserByProjectQuery = exports.listChatSessionsForUserQuery = exports.getChatSessionQuery = exports.createChatSessionQuery = void 0;
4
+ exports.createChatSession = createChatSession;
5
+ exports.getChatSession = getChatSession;
6
+ exports.listChatSessionsForUser = listChatSessionsForUser;
7
+ exports.listChatSessionsForUserByProject = listChatSessionsForUserByProject;
8
+ exports.updateChatSessionModel = updateChatSessionModel;
9
+ exports.updateChatSessionTitle = updateChatSessionTitle;
10
+ exports.touchChatSessionLastMessage = touchChatSessionLastMessage;
11
+ exports.softDeleteChatSession = softDeleteChatSession;
12
+ exports.attachProjectToChatSession = attachProjectToChatSession;
13
+ exports.detachProjectFromChatSession = detachProjectFromChatSession;
14
+ exports.listProjectsForChatSession = listProjectsForChatSession;
15
+ exports.insertChatMessage = insertChatMessage;
16
+ exports.getChatMessage = getChatMessage;
17
+ exports.listChatMessagesForSession = listChatMessagesForSession;
18
+ exports.updateChatMessageToolCallState = updateChatMessageToolCallState;
19
+ exports.completeChatMessage = completeChatMessage;
20
+ exports.createOrganizationApprovedModel = createOrganizationApprovedModel;
21
+ exports.deleteOrganizationApprovedModel = deleteOrganizationApprovedModel;
22
+ exports.listOrganizationApprovedModels = listOrganizationApprovedModels;
23
+ exports.getOrganizationApprovedModel = getOrganizationApprovedModel;
24
+ exports.upsertOrganizationChatSettings = upsertOrganizationChatSettings;
25
+ exports.getOrganizationChatSettings = getOrganizationChatSettings;
26
+ exports.clearOrganizationChatSettingsDefaultIfMatches = clearOrganizationChatSettingsDefaultIfMatches;
27
+ exports.createChatArtifact = createChatArtifact;
28
+ exports.bumpChatArtifactVersion = bumpChatArtifactVersion;
29
+ exports.listChatArtifactsForSession = listChatArtifactsForSession;
30
+ exports.getChatArtifact = getChatArtifact;
31
+ exports.insertChatArtifactVersion = insertChatArtifactVersion;
32
+ exports.getChatArtifactVersion = getChatArtifactVersion;
33
+ exports.listChatArtifactVersions = listChatArtifactVersions;
34
+ exports.createChatSessionQuery = `-- name: CreateChatSession :one
35
+ INSERT INTO weave.chat_sessions (
36
+ id, organization_id, created_by_user_id, title,
37
+ model_id, provider_configuration_id
38
+ ) VALUES (
39
+ $1, $2, $3, $4, $5, $6
40
+ )
41
+ RETURNING id, organization_id, created_by_user_id, title, model_id, provider_configuration_id, created_at, updated_at, last_message_at, deleted_at`;
42
+ async function createChatSession(client, args) {
43
+ const result = await client.query({
44
+ text: exports.createChatSessionQuery,
45
+ values: [args.id, args.organizationId, args.createdByUserId, args.title, args.modelId, args.providerConfigurationId],
46
+ rowMode: "array"
47
+ });
48
+ if (result.rows.length !== 1) {
49
+ return null;
50
+ }
51
+ const row = result.rows[0];
52
+ return {
53
+ id: row[0],
54
+ organizationId: row[1],
55
+ createdByUserId: row[2],
56
+ title: row[3],
57
+ modelId: row[4],
58
+ providerConfigurationId: row[5],
59
+ createdAt: row[6],
60
+ updatedAt: row[7],
61
+ lastMessageAt: row[8],
62
+ deletedAt: row[9]
63
+ };
64
+ }
65
+ exports.getChatSessionQuery = `-- name: GetChatSession :one
66
+ SELECT id, organization_id, created_by_user_id, title, model_id, provider_configuration_id, created_at, updated_at, last_message_at, deleted_at FROM weave.chat_sessions
67
+ WHERE id = $1 AND deleted_at IS NULL`;
68
+ async function getChatSession(client, args) {
69
+ const result = await client.query({
70
+ text: exports.getChatSessionQuery,
71
+ values: [args.id],
72
+ rowMode: "array"
73
+ });
74
+ if (result.rows.length !== 1) {
75
+ return null;
76
+ }
77
+ const row = result.rows[0];
78
+ return {
79
+ id: row[0],
80
+ organizationId: row[1],
81
+ createdByUserId: row[2],
82
+ title: row[3],
83
+ modelId: row[4],
84
+ providerConfigurationId: row[5],
85
+ createdAt: row[6],
86
+ updatedAt: row[7],
87
+ lastMessageAt: row[8],
88
+ deletedAt: row[9]
89
+ };
90
+ }
91
+ exports.listChatSessionsForUserQuery = `-- name: ListChatSessionsForUser :many
92
+ SELECT id, organization_id, created_by_user_id, title, model_id, provider_configuration_id, created_at, updated_at, last_message_at, deleted_at FROM weave.chat_sessions
93
+ WHERE organization_id = $1
94
+ AND created_by_user_id = $2
95
+ AND deleted_at IS NULL
96
+ ORDER BY last_message_at DESC
97
+ LIMIT $3 OFFSET $4`;
98
+ async function listChatSessionsForUser(client, args) {
99
+ const result = await client.query({
100
+ text: exports.listChatSessionsForUserQuery,
101
+ values: [args.organizationId, args.createdByUserId, args.limit, args.offset],
102
+ rowMode: "array"
103
+ });
104
+ return result.rows.map(row => {
105
+ return {
106
+ id: row[0],
107
+ organizationId: row[1],
108
+ createdByUserId: row[2],
109
+ title: row[3],
110
+ modelId: row[4],
111
+ providerConfigurationId: row[5],
112
+ createdAt: row[6],
113
+ updatedAt: row[7],
114
+ lastMessageAt: row[8],
115
+ deletedAt: row[9]
116
+ };
117
+ });
118
+ }
119
+ exports.listChatSessionsForUserByProjectQuery = `-- name: ListChatSessionsForUserByProject :many
120
+ SELECT s.id, s.organization_id, s.created_by_user_id, s.title, s.model_id, s.provider_configuration_id, s.created_at, s.updated_at, s.last_message_at, s.deleted_at FROM weave.chat_sessions s
121
+ JOIN weave.chat_session_projects p ON p.session_id = s.id
122
+ WHERE s.organization_id = $1
123
+ AND s.created_by_user_id = $2
124
+ AND p.project_id = $3
125
+ AND s.deleted_at IS NULL
126
+ ORDER BY s.last_message_at DESC
127
+ LIMIT $4 OFFSET $5`;
128
+ async function listChatSessionsForUserByProject(client, args) {
129
+ const result = await client.query({
130
+ text: exports.listChatSessionsForUserByProjectQuery,
131
+ values: [args.organizationId, args.createdByUserId, args.projectId, args.limit, args.offset],
132
+ rowMode: "array"
133
+ });
134
+ return result.rows.map(row => {
135
+ return {
136
+ id: row[0],
137
+ organizationId: row[1],
138
+ createdByUserId: row[2],
139
+ title: row[3],
140
+ modelId: row[4],
141
+ providerConfigurationId: row[5],
142
+ createdAt: row[6],
143
+ updatedAt: row[7],
144
+ lastMessageAt: row[8],
145
+ deletedAt: row[9]
146
+ };
147
+ });
148
+ }
149
+ exports.updateChatSessionModelQuery = `-- name: UpdateChatSessionModel :one
150
+ UPDATE weave.chat_sessions
151
+ SET model_id = $2,
152
+ provider_configuration_id = $3,
153
+ updated_at = now()
154
+ WHERE id = $1
155
+ RETURNING id, organization_id, created_by_user_id, title, model_id, provider_configuration_id, created_at, updated_at, last_message_at, deleted_at`;
156
+ async function updateChatSessionModel(client, args) {
157
+ const result = await client.query({
158
+ text: exports.updateChatSessionModelQuery,
159
+ values: [args.id, args.modelId, args.providerConfigurationId],
160
+ rowMode: "array"
161
+ });
162
+ if (result.rows.length !== 1) {
163
+ return null;
164
+ }
165
+ const row = result.rows[0];
166
+ return {
167
+ id: row[0],
168
+ organizationId: row[1],
169
+ createdByUserId: row[2],
170
+ title: row[3],
171
+ modelId: row[4],
172
+ providerConfigurationId: row[5],
173
+ createdAt: row[6],
174
+ updatedAt: row[7],
175
+ lastMessageAt: row[8],
176
+ deletedAt: row[9]
177
+ };
178
+ }
179
+ exports.updateChatSessionTitleQuery = `-- name: UpdateChatSessionTitle :one
180
+ UPDATE weave.chat_sessions
181
+ SET title = $2, updated_at = now()
182
+ WHERE id = $1
183
+ RETURNING id, organization_id, created_by_user_id, title, model_id, provider_configuration_id, created_at, updated_at, last_message_at, deleted_at`;
184
+ async function updateChatSessionTitle(client, args) {
185
+ const result = await client.query({
186
+ text: exports.updateChatSessionTitleQuery,
187
+ values: [args.id, args.title],
188
+ rowMode: "array"
189
+ });
190
+ if (result.rows.length !== 1) {
191
+ return null;
192
+ }
193
+ const row = result.rows[0];
194
+ return {
195
+ id: row[0],
196
+ organizationId: row[1],
197
+ createdByUserId: row[2],
198
+ title: row[3],
199
+ modelId: row[4],
200
+ providerConfigurationId: row[5],
201
+ createdAt: row[6],
202
+ updatedAt: row[7],
203
+ lastMessageAt: row[8],
204
+ deletedAt: row[9]
205
+ };
206
+ }
207
+ exports.touchChatSessionLastMessageQuery = `-- name: TouchChatSessionLastMessage :exec
208
+ UPDATE weave.chat_sessions
209
+ SET last_message_at = $2, updated_at = $2
210
+ WHERE id = $1`;
211
+ async function touchChatSessionLastMessage(client, args) {
212
+ await client.query({
213
+ text: exports.touchChatSessionLastMessageQuery,
214
+ values: [args.id, args.lastMessageAt],
215
+ rowMode: "array"
216
+ });
217
+ }
218
+ exports.softDeleteChatSessionQuery = `-- name: SoftDeleteChatSession :exec
219
+ UPDATE weave.chat_sessions
220
+ SET deleted_at = now(), updated_at = now()
221
+ WHERE id = $1`;
222
+ async function softDeleteChatSession(client, args) {
223
+ await client.query({
224
+ text: exports.softDeleteChatSessionQuery,
225
+ values: [args.id],
226
+ rowMode: "array"
227
+ });
228
+ }
229
+ exports.attachProjectToChatSessionQuery = `-- name: AttachProjectToChatSession :exec
230
+ INSERT INTO weave.chat_session_projects (
231
+ session_id, project_id, attached_by_user_id
232
+ ) VALUES ($1, $2, $3)
233
+ ON CONFLICT DO NOTHING`;
234
+ async function attachProjectToChatSession(client, args) {
235
+ await client.query({
236
+ text: exports.attachProjectToChatSessionQuery,
237
+ values: [args.sessionId, args.projectId, args.attachedByUserId],
238
+ rowMode: "array"
239
+ });
240
+ }
241
+ exports.detachProjectFromChatSessionQuery = `-- name: DetachProjectFromChatSession :exec
242
+ DELETE FROM weave.chat_session_projects
243
+ WHERE session_id = $1 AND project_id = $2`;
244
+ async function detachProjectFromChatSession(client, args) {
245
+ await client.query({
246
+ text: exports.detachProjectFromChatSessionQuery,
247
+ values: [args.sessionId, args.projectId],
248
+ rowMode: "array"
249
+ });
250
+ }
251
+ exports.listProjectsForChatSessionQuery = `-- name: ListProjectsForChatSession :many
252
+ SELECT project_id FROM weave.chat_session_projects
253
+ WHERE session_id = $1`;
254
+ async function listProjectsForChatSession(client, args) {
255
+ const result = await client.query({
256
+ text: exports.listProjectsForChatSessionQuery,
257
+ values: [args.sessionId],
258
+ rowMode: "array"
259
+ });
260
+ return result.rows.map(row => {
261
+ return {
262
+ projectId: row[0]
263
+ };
264
+ });
265
+ }
266
+ exports.insertChatMessageQuery = `-- name: InsertChatMessage :one
267
+ INSERT INTO weave.chat_messages (
268
+ id, session_id, role, content,
269
+ parent_message_id, tool_call_id, tool_call_state,
270
+ model_id, ephemeral_attachment_count, ephemeral_attachment_mime_types,
271
+ token_usage, stream_state
272
+ ) VALUES (
273
+ $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12
274
+ )
275
+ RETURNING id, session_id, role, content, parent_message_id, tool_call_id, tool_call_state, model_id, ephemeral_attachment_count, ephemeral_attachment_mime_types, token_usage, stream_state, created_at, completed_at`;
276
+ async function insertChatMessage(client, args) {
277
+ const result = await client.query({
278
+ text: exports.insertChatMessageQuery,
279
+ values: [args.id, args.sessionId, args.role, args.content, args.parentMessageId, args.toolCallId, args.toolCallState, args.modelId, args.ephemeralAttachmentCount, args.ephemeralAttachmentMimeTypes, args.tokenUsage, args.streamState],
280
+ rowMode: "array"
281
+ });
282
+ if (result.rows.length !== 1) {
283
+ return null;
284
+ }
285
+ const row = result.rows[0];
286
+ return {
287
+ id: row[0],
288
+ sessionId: row[1],
289
+ role: row[2],
290
+ content: row[3],
291
+ parentMessageId: row[4],
292
+ toolCallId: row[5],
293
+ toolCallState: row[6],
294
+ modelId: row[7],
295
+ ephemeralAttachmentCount: row[8],
296
+ ephemeralAttachmentMimeTypes: row[9],
297
+ tokenUsage: row[10],
298
+ streamState: row[11],
299
+ createdAt: row[12],
300
+ completedAt: row[13]
301
+ };
302
+ }
303
+ exports.getChatMessageQuery = `-- name: GetChatMessage :one
304
+ SELECT id, session_id, role, content, parent_message_id, tool_call_id, tool_call_state, model_id, ephemeral_attachment_count, ephemeral_attachment_mime_types, token_usage, stream_state, created_at, completed_at FROM weave.chat_messages WHERE id = $1`;
305
+ async function getChatMessage(client, args) {
306
+ const result = await client.query({
307
+ text: exports.getChatMessageQuery,
308
+ values: [args.id],
309
+ rowMode: "array"
310
+ });
311
+ if (result.rows.length !== 1) {
312
+ return null;
313
+ }
314
+ const row = result.rows[0];
315
+ return {
316
+ id: row[0],
317
+ sessionId: row[1],
318
+ role: row[2],
319
+ content: row[3],
320
+ parentMessageId: row[4],
321
+ toolCallId: row[5],
322
+ toolCallState: row[6],
323
+ modelId: row[7],
324
+ ephemeralAttachmentCount: row[8],
325
+ ephemeralAttachmentMimeTypes: row[9],
326
+ tokenUsage: row[10],
327
+ streamState: row[11],
328
+ createdAt: row[12],
329
+ completedAt: row[13]
330
+ };
331
+ }
332
+ exports.listChatMessagesForSessionQuery = `-- name: ListChatMessagesForSession :many
333
+ SELECT id, session_id, role, content, parent_message_id, tool_call_id, tool_call_state, model_id, ephemeral_attachment_count, ephemeral_attachment_mime_types, token_usage, stream_state, created_at, completed_at FROM weave.chat_messages
334
+ WHERE session_id = $1
335
+ ORDER BY created_at`;
336
+ async function listChatMessagesForSession(client, args) {
337
+ const result = await client.query({
338
+ text: exports.listChatMessagesForSessionQuery,
339
+ values: [args.sessionId],
340
+ rowMode: "array"
341
+ });
342
+ return result.rows.map(row => {
343
+ return {
344
+ id: row[0],
345
+ sessionId: row[1],
346
+ role: row[2],
347
+ content: row[3],
348
+ parentMessageId: row[4],
349
+ toolCallId: row[5],
350
+ toolCallState: row[6],
351
+ modelId: row[7],
352
+ ephemeralAttachmentCount: row[8],
353
+ ephemeralAttachmentMimeTypes: row[9],
354
+ tokenUsage: row[10],
355
+ streamState: row[11],
356
+ createdAt: row[12],
357
+ completedAt: row[13]
358
+ };
359
+ });
360
+ }
361
+ exports.updateChatMessageToolCallStateQuery = `-- name: UpdateChatMessageToolCallState :one
362
+ UPDATE weave.chat_messages
363
+ SET tool_call_state = $2
364
+ WHERE id = $1
365
+ RETURNING id, session_id, role, content, parent_message_id, tool_call_id, tool_call_state, model_id, ephemeral_attachment_count, ephemeral_attachment_mime_types, token_usage, stream_state, created_at, completed_at`;
366
+ async function updateChatMessageToolCallState(client, args) {
367
+ const result = await client.query({
368
+ text: exports.updateChatMessageToolCallStateQuery,
369
+ values: [args.id, args.toolCallState],
370
+ rowMode: "array"
371
+ });
372
+ if (result.rows.length !== 1) {
373
+ return null;
374
+ }
375
+ const row = result.rows[0];
376
+ return {
377
+ id: row[0],
378
+ sessionId: row[1],
379
+ role: row[2],
380
+ content: row[3],
381
+ parentMessageId: row[4],
382
+ toolCallId: row[5],
383
+ toolCallState: row[6],
384
+ modelId: row[7],
385
+ ephemeralAttachmentCount: row[8],
386
+ ephemeralAttachmentMimeTypes: row[9],
387
+ tokenUsage: row[10],
388
+ streamState: row[11],
389
+ createdAt: row[12],
390
+ completedAt: row[13]
391
+ };
392
+ }
393
+ exports.completeChatMessageQuery = `-- name: CompleteChatMessage :one
394
+ UPDATE weave.chat_messages
395
+ SET stream_state = $2,
396
+ content = $3,
397
+ token_usage = $4,
398
+ completed_at = now()
399
+ WHERE id = $1
400
+ RETURNING id, session_id, role, content, parent_message_id, tool_call_id, tool_call_state, model_id, ephemeral_attachment_count, ephemeral_attachment_mime_types, token_usage, stream_state, created_at, completed_at`;
401
+ async function completeChatMessage(client, args) {
402
+ const result = await client.query({
403
+ text: exports.completeChatMessageQuery,
404
+ values: [args.id, args.streamState, args.content, args.tokenUsage],
405
+ rowMode: "array"
406
+ });
407
+ if (result.rows.length !== 1) {
408
+ return null;
409
+ }
410
+ const row = result.rows[0];
411
+ return {
412
+ id: row[0],
413
+ sessionId: row[1],
414
+ role: row[2],
415
+ content: row[3],
416
+ parentMessageId: row[4],
417
+ toolCallId: row[5],
418
+ toolCallState: row[6],
419
+ modelId: row[7],
420
+ ephemeralAttachmentCount: row[8],
421
+ ephemeralAttachmentMimeTypes: row[9],
422
+ tokenUsage: row[10],
423
+ streamState: row[11],
424
+ createdAt: row[12],
425
+ completedAt: row[13]
426
+ };
427
+ }
428
+ exports.createOrganizationApprovedModelQuery = `-- name: CreateOrganizationApprovedModel :one
429
+ INSERT INTO weave.organization_approved_models (
430
+ id, organization_id, provider_configuration_id, model_id, approved_by_user_id
431
+ ) VALUES (
432
+ $1, $2, $3, $4, $5
433
+ )
434
+ RETURNING id, organization_id, provider_configuration_id, model_id, approved_by_user_id, approved_at`;
435
+ async function createOrganizationApprovedModel(client, args) {
436
+ const result = await client.query({
437
+ text: exports.createOrganizationApprovedModelQuery,
438
+ values: [args.id, args.organizationId, args.providerConfigurationId, args.modelId, args.approvedByUserId],
439
+ rowMode: "array"
440
+ });
441
+ if (result.rows.length !== 1) {
442
+ return null;
443
+ }
444
+ const row = result.rows[0];
445
+ return {
446
+ id: row[0],
447
+ organizationId: row[1],
448
+ providerConfigurationId: row[2],
449
+ modelId: row[3],
450
+ approvedByUserId: row[4],
451
+ approvedAt: row[5]
452
+ };
453
+ }
454
+ exports.deleteOrganizationApprovedModelQuery = `-- name: DeleteOrganizationApprovedModel :exec
455
+ DELETE FROM weave.organization_approved_models
456
+ WHERE id = $1 AND organization_id = $2`;
457
+ async function deleteOrganizationApprovedModel(client, args) {
458
+ await client.query({
459
+ text: exports.deleteOrganizationApprovedModelQuery,
460
+ values: [args.id, args.organizationId],
461
+ rowMode: "array"
462
+ });
463
+ }
464
+ exports.listOrganizationApprovedModelsQuery = `-- name: ListOrganizationApprovedModels :many
465
+ SELECT id, organization_id, provider_configuration_id, model_id, approved_by_user_id, approved_at FROM weave.organization_approved_models
466
+ WHERE organization_id = $1
467
+ ORDER BY approved_at DESC`;
468
+ async function listOrganizationApprovedModels(client, args) {
469
+ const result = await client.query({
470
+ text: exports.listOrganizationApprovedModelsQuery,
471
+ values: [args.organizationId],
472
+ rowMode: "array"
473
+ });
474
+ return result.rows.map(row => {
475
+ return {
476
+ id: row[0],
477
+ organizationId: row[1],
478
+ providerConfigurationId: row[2],
479
+ modelId: row[3],
480
+ approvedByUserId: row[4],
481
+ approvedAt: row[5]
482
+ };
483
+ });
484
+ }
485
+ exports.getOrganizationApprovedModelQuery = `-- name: GetOrganizationApprovedModel :one
486
+ SELECT id, organization_id, provider_configuration_id, model_id, approved_by_user_id, approved_at FROM weave.organization_approved_models
487
+ WHERE organization_id = $1
488
+ AND provider_configuration_id = $2
489
+ AND model_id = $3`;
490
+ async function getOrganizationApprovedModel(client, args) {
491
+ const result = await client.query({
492
+ text: exports.getOrganizationApprovedModelQuery,
493
+ values: [args.organizationId, args.providerConfigurationId, args.modelId],
494
+ rowMode: "array"
495
+ });
496
+ if (result.rows.length !== 1) {
497
+ return null;
498
+ }
499
+ const row = result.rows[0];
500
+ return {
501
+ id: row[0],
502
+ organizationId: row[1],
503
+ providerConfigurationId: row[2],
504
+ modelId: row[3],
505
+ approvedByUserId: row[4],
506
+ approvedAt: row[5]
507
+ };
508
+ }
509
+ exports.upsertOrganizationChatSettingsQuery = `-- name: UpsertOrganizationChatSettings :one
510
+ INSERT INTO weave.organization_chat_settings (
511
+ organization_id, default_chat_model_id, default_provider_configuration_id
512
+ ) VALUES ($1, $2, $3)
513
+ ON CONFLICT (organization_id) DO UPDATE
514
+ SET default_chat_model_id = EXCLUDED.default_chat_model_id,
515
+ default_provider_configuration_id = EXCLUDED.default_provider_configuration_id,
516
+ updated_at = now()
517
+ RETURNING organization_id, default_chat_model_id, default_provider_configuration_id, updated_at`;
518
+ async function upsertOrganizationChatSettings(client, args) {
519
+ const result = await client.query({
520
+ text: exports.upsertOrganizationChatSettingsQuery,
521
+ values: [args.organizationId, args.defaultChatModelId, args.defaultProviderConfigurationId],
522
+ rowMode: "array"
523
+ });
524
+ if (result.rows.length !== 1) {
525
+ return null;
526
+ }
527
+ const row = result.rows[0];
528
+ return {
529
+ organizationId: row[0],
530
+ defaultChatModelId: row[1],
531
+ defaultProviderConfigurationId: row[2],
532
+ updatedAt: row[3]
533
+ };
534
+ }
535
+ exports.getOrganizationChatSettingsQuery = `-- name: GetOrganizationChatSettings :one
536
+ SELECT organization_id, default_chat_model_id, default_provider_configuration_id, updated_at FROM weave.organization_chat_settings
537
+ WHERE organization_id = $1`;
538
+ async function getOrganizationChatSettings(client, args) {
539
+ const result = await client.query({
540
+ text: exports.getOrganizationChatSettingsQuery,
541
+ values: [args.organizationId],
542
+ rowMode: "array"
543
+ });
544
+ if (result.rows.length !== 1) {
545
+ return null;
546
+ }
547
+ const row = result.rows[0];
548
+ return {
549
+ organizationId: row[0],
550
+ defaultChatModelId: row[1],
551
+ defaultProviderConfigurationId: row[2],
552
+ updatedAt: row[3]
553
+ };
554
+ }
555
+ exports.clearOrganizationChatSettingsDefaultIfMatchesQuery = `-- name: ClearOrganizationChatSettingsDefaultIfMatches :exec
556
+ UPDATE weave.organization_chat_settings
557
+ SET default_chat_model_id = NULL,
558
+ default_provider_configuration_id = NULL,
559
+ updated_at = now()
560
+ WHERE organization_id = $1
561
+ AND default_chat_model_id = $2
562
+ AND default_provider_configuration_id = $3`;
563
+ async function clearOrganizationChatSettingsDefaultIfMatches(client, args) {
564
+ await client.query({
565
+ text: exports.clearOrganizationChatSettingsDefaultIfMatchesQuery,
566
+ values: [args.organizationId, args.defaultChatModelId, args.defaultProviderConfigurationId],
567
+ rowMode: "array"
568
+ });
569
+ }
570
+ exports.createChatArtifactQuery = `-- name: CreateChatArtifact :one
571
+ INSERT INTO weave.chat_artifacts (
572
+ id, session_id, title, kind, language, current_version
573
+ ) VALUES ($1, $2, $3, $4, $5, 1)
574
+ RETURNING id, session_id, title, kind, language, current_version, created_at, updated_at`;
575
+ async function createChatArtifact(client, args) {
576
+ const result = await client.query({
577
+ text: exports.createChatArtifactQuery,
578
+ values: [args.id, args.sessionId, args.title, args.kind, args.language],
579
+ rowMode: "array"
580
+ });
581
+ if (result.rows.length !== 1) {
582
+ return null;
583
+ }
584
+ const row = result.rows[0];
585
+ return {
586
+ id: row[0],
587
+ sessionId: row[1],
588
+ title: row[2],
589
+ kind: row[3],
590
+ language: row[4],
591
+ currentVersion: row[5],
592
+ createdAt: row[6],
593
+ updatedAt: row[7]
594
+ };
595
+ }
596
+ exports.bumpChatArtifactVersionQuery = `-- name: BumpChatArtifactVersion :one
597
+ UPDATE weave.chat_artifacts
598
+ SET current_version = current_version + 1,
599
+ updated_at = now()
600
+ WHERE id = $1
601
+ RETURNING id, session_id, title, kind, language, current_version, created_at, updated_at`;
602
+ async function bumpChatArtifactVersion(client, args) {
603
+ const result = await client.query({
604
+ text: exports.bumpChatArtifactVersionQuery,
605
+ values: [args.id],
606
+ rowMode: "array"
607
+ });
608
+ if (result.rows.length !== 1) {
609
+ return null;
610
+ }
611
+ const row = result.rows[0];
612
+ return {
613
+ id: row[0],
614
+ sessionId: row[1],
615
+ title: row[2],
616
+ kind: row[3],
617
+ language: row[4],
618
+ currentVersion: row[5],
619
+ createdAt: row[6],
620
+ updatedAt: row[7]
621
+ };
622
+ }
623
+ exports.listChatArtifactsForSessionQuery = `-- name: ListChatArtifactsForSession :many
624
+ SELECT id, session_id, title, kind, language, current_version, created_at, updated_at FROM weave.chat_artifacts
625
+ WHERE session_id = $1
626
+ ORDER BY created_at`;
627
+ async function listChatArtifactsForSession(client, args) {
628
+ const result = await client.query({
629
+ text: exports.listChatArtifactsForSessionQuery,
630
+ values: [args.sessionId],
631
+ rowMode: "array"
632
+ });
633
+ return result.rows.map(row => {
634
+ return {
635
+ id: row[0],
636
+ sessionId: row[1],
637
+ title: row[2],
638
+ kind: row[3],
639
+ language: row[4],
640
+ currentVersion: row[5],
641
+ createdAt: row[6],
642
+ updatedAt: row[7]
643
+ };
644
+ });
645
+ }
646
+ exports.getChatArtifactQuery = `-- name: GetChatArtifact :one
647
+ SELECT id, session_id, title, kind, language, current_version, created_at, updated_at FROM weave.chat_artifacts WHERE id = $1`;
648
+ async function getChatArtifact(client, args) {
649
+ const result = await client.query({
650
+ text: exports.getChatArtifactQuery,
651
+ values: [args.id],
652
+ rowMode: "array"
653
+ });
654
+ if (result.rows.length !== 1) {
655
+ return null;
656
+ }
657
+ const row = result.rows[0];
658
+ return {
659
+ id: row[0],
660
+ sessionId: row[1],
661
+ title: row[2],
662
+ kind: row[3],
663
+ language: row[4],
664
+ currentVersion: row[5],
665
+ createdAt: row[6],
666
+ updatedAt: row[7]
667
+ };
668
+ }
669
+ exports.insertChatArtifactVersionQuery = `-- name: InsertChatArtifactVersion :one
670
+ INSERT INTO weave.chat_artifact_versions (
671
+ id, artifact_id, version, content, created_by_message_id
672
+ ) VALUES ($1, $2, $3, $4, $5)
673
+ RETURNING id, artifact_id, version, content, created_by_message_id, created_at`;
674
+ async function insertChatArtifactVersion(client, args) {
675
+ const result = await client.query({
676
+ text: exports.insertChatArtifactVersionQuery,
677
+ values: [args.id, args.artifactId, args.version, args.content, args.createdByMessageId],
678
+ rowMode: "array"
679
+ });
680
+ if (result.rows.length !== 1) {
681
+ return null;
682
+ }
683
+ const row = result.rows[0];
684
+ return {
685
+ id: row[0],
686
+ artifactId: row[1],
687
+ version: row[2],
688
+ content: row[3],
689
+ createdByMessageId: row[4],
690
+ createdAt: row[5]
691
+ };
692
+ }
693
+ exports.getChatArtifactVersionQuery = `-- name: GetChatArtifactVersion :one
694
+ SELECT id, artifact_id, version, content, created_by_message_id, created_at FROM weave.chat_artifact_versions
695
+ WHERE artifact_id = $1 AND version = $2`;
696
+ async function getChatArtifactVersion(client, args) {
697
+ const result = await client.query({
698
+ text: exports.getChatArtifactVersionQuery,
699
+ values: [args.artifactId, args.version],
700
+ rowMode: "array"
701
+ });
702
+ if (result.rows.length !== 1) {
703
+ return null;
704
+ }
705
+ const row = result.rows[0];
706
+ return {
707
+ id: row[0],
708
+ artifactId: row[1],
709
+ version: row[2],
710
+ content: row[3],
711
+ createdByMessageId: row[4],
712
+ createdAt: row[5]
713
+ };
714
+ }
715
+ exports.listChatArtifactVersionsQuery = `-- name: ListChatArtifactVersions :many
716
+ SELECT id, artifact_id, version, content, created_by_message_id, created_at FROM weave.chat_artifact_versions
717
+ WHERE artifact_id = $1
718
+ ORDER BY version`;
719
+ async function listChatArtifactVersions(client, args) {
720
+ const result = await client.query({
721
+ text: exports.listChatArtifactVersionsQuery,
722
+ values: [args.artifactId],
723
+ rowMode: "array"
724
+ });
725
+ return result.rows.map(row => {
726
+ return {
727
+ id: row[0],
728
+ artifactId: row[1],
729
+ version: row[2],
730
+ content: row[3],
731
+ createdByMessageId: row[4],
732
+ createdAt: row[5]
733
+ };
734
+ });
735
+ }