@pikku/kysely 0.12.6 → 0.12.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/src/kysely-agent-run-service.js +59 -59
- package/dist/src/kysely-ai-storage-service.js +174 -174
- package/dist/src/kysely-channel-store.js +12 -12
- package/dist/src/kysely-credential-service.js +29 -29
- package/dist/src/kysely-deployment-service.js +22 -22
- package/dist/src/kysely-eventhub-store.js +8 -8
- package/dist/src/kysely-secret-service.js +19 -19
- package/dist/src/kysely-tables.d.ts +91 -91
- package/dist/src/kysely-workflow-run-service.js +73 -73
- package/dist/src/kysely-workflow-service.js +127 -127
- package/package.json +1 -1
- package/src/kysely-agent-run-service.ts +59 -59
- package/src/kysely-ai-storage-service.ts +174 -174
- package/src/kysely-channel-store.ts +12 -12
- package/src/kysely-credential-service.ts +29 -29
- package/src/kysely-deployment-service.ts +24 -24
- package/src/kysely-eventhub-store.ts +8 -8
- package/src/kysely-secret-service.ts +19 -19
- package/src/kysely-services.test.ts +19 -18
- package/src/kysely-tables.ts +91 -91
- package/src/kysely-workflow-run-service.ts +76 -76
- package/src/kysely-workflow-service.ts +129 -129
|
@@ -183,14 +183,14 @@ export class KyselyAIStorageService
|
|
|
183
183
|
const now = new Date()
|
|
184
184
|
|
|
185
185
|
await this.db
|
|
186
|
-
.insertInto('
|
|
186
|
+
.insertInto('aiThreads')
|
|
187
187
|
.values({
|
|
188
188
|
id,
|
|
189
|
-
|
|
189
|
+
resourceId: resourceId,
|
|
190
190
|
title: options?.title ?? null,
|
|
191
191
|
metadata: JSON.stringify(options?.metadata ?? null),
|
|
192
|
-
|
|
193
|
-
|
|
192
|
+
createdAt: now,
|
|
193
|
+
updatedAt: now,
|
|
194
194
|
})
|
|
195
195
|
.execute()
|
|
196
196
|
|
|
@@ -206,14 +206,14 @@ export class KyselyAIStorageService
|
|
|
206
206
|
|
|
207
207
|
async getThread(threadId: string): Promise<AIThread> {
|
|
208
208
|
const row = await this.db
|
|
209
|
-
.selectFrom('
|
|
209
|
+
.selectFrom('aiThreads')
|
|
210
210
|
.select([
|
|
211
211
|
'id',
|
|
212
|
-
'
|
|
212
|
+
'resourceId',
|
|
213
213
|
'title',
|
|
214
214
|
'metadata',
|
|
215
|
-
'
|
|
216
|
-
'
|
|
215
|
+
'createdAt',
|
|
216
|
+
'updatedAt',
|
|
217
217
|
])
|
|
218
218
|
.where('id', '=', threadId)
|
|
219
219
|
.executeTakeFirst()
|
|
@@ -224,41 +224,41 @@ export class KyselyAIStorageService
|
|
|
224
224
|
|
|
225
225
|
return {
|
|
226
226
|
id: row.id,
|
|
227
|
-
resourceId: row.
|
|
227
|
+
resourceId: row.resourceId,
|
|
228
228
|
title: row.title ?? undefined,
|
|
229
229
|
metadata: parseJson(row.metadata),
|
|
230
|
-
createdAt: new Date(row.
|
|
231
|
-
updatedAt: new Date(row.
|
|
230
|
+
createdAt: new Date(row.createdAt),
|
|
231
|
+
updatedAt: new Date(row.updatedAt),
|
|
232
232
|
}
|
|
233
233
|
}
|
|
234
234
|
|
|
235
235
|
async getThreads(resourceId: string): Promise<AIThread[]> {
|
|
236
236
|
const result = await this.db
|
|
237
|
-
.selectFrom('
|
|
237
|
+
.selectFrom('aiThreads')
|
|
238
238
|
.select([
|
|
239
239
|
'id',
|
|
240
|
-
'
|
|
240
|
+
'resourceId',
|
|
241
241
|
'title',
|
|
242
242
|
'metadata',
|
|
243
|
-
'
|
|
244
|
-
'
|
|
243
|
+
'createdAt',
|
|
244
|
+
'updatedAt',
|
|
245
245
|
])
|
|
246
|
-
.where('
|
|
247
|
-
.orderBy('
|
|
246
|
+
.where('resourceId', '=', resourceId)
|
|
247
|
+
.orderBy('updatedAt', 'desc')
|
|
248
248
|
.execute()
|
|
249
249
|
|
|
250
250
|
return result.map((row) => ({
|
|
251
251
|
id: row.id,
|
|
252
|
-
resourceId: row.
|
|
252
|
+
resourceId: row.resourceId,
|
|
253
253
|
title: row.title ?? undefined,
|
|
254
254
|
metadata: parseJson(row.metadata),
|
|
255
|
-
createdAt: new Date(row.
|
|
256
|
-
updatedAt: new Date(row.
|
|
255
|
+
createdAt: new Date(row.createdAt),
|
|
256
|
+
updatedAt: new Date(row.updatedAt),
|
|
257
257
|
}))
|
|
258
258
|
}
|
|
259
259
|
|
|
260
260
|
async deleteThread(threadId: string): Promise<void> {
|
|
261
|
-
await this.db.deleteFrom('
|
|
261
|
+
await this.db.deleteFrom('aiThreads').where('id', '=', threadId).execute()
|
|
262
262
|
}
|
|
263
263
|
|
|
264
264
|
async getMessages(
|
|
@@ -266,44 +266,44 @@ export class KyselyAIStorageService
|
|
|
266
266
|
options?: { lastN?: number; cursor?: string }
|
|
267
267
|
): Promise<AIMessage[]> {
|
|
268
268
|
let msgQuery = this.db
|
|
269
|
-
.selectFrom('
|
|
270
|
-
.select(['id', 'role', 'content', '
|
|
271
|
-
.where('
|
|
269
|
+
.selectFrom('aiMessage')
|
|
270
|
+
.select(['id', 'role', 'content', 'createdAt'])
|
|
271
|
+
.where('threadId', '=', threadId)
|
|
272
272
|
|
|
273
273
|
if (options?.cursor) {
|
|
274
274
|
const cursorRow = await this.db
|
|
275
|
-
.selectFrom('
|
|
276
|
-
.select('
|
|
275
|
+
.selectFrom('aiMessage')
|
|
276
|
+
.select('createdAt')
|
|
277
277
|
.where('id', '=', options.cursor)
|
|
278
278
|
.executeTakeFirst()
|
|
279
279
|
|
|
280
280
|
if (cursorRow) {
|
|
281
|
-
msgQuery = msgQuery.where('
|
|
281
|
+
msgQuery = msgQuery.where('createdAt', '<', cursorRow.createdAt)
|
|
282
282
|
}
|
|
283
283
|
}
|
|
284
284
|
|
|
285
285
|
let msgResult
|
|
286
286
|
if (options?.cursor || options?.lastN) {
|
|
287
287
|
const innerResult = await msgQuery
|
|
288
|
-
.orderBy('
|
|
288
|
+
.orderBy('createdAt', 'desc')
|
|
289
289
|
.limit(options?.lastN ?? 50)
|
|
290
290
|
.execute()
|
|
291
291
|
innerResult.reverse()
|
|
292
292
|
msgResult = innerResult
|
|
293
293
|
} else {
|
|
294
|
-
msgResult = await msgQuery.orderBy('
|
|
294
|
+
msgResult = await msgQuery.orderBy('createdAt', 'asc').execute()
|
|
295
295
|
}
|
|
296
296
|
|
|
297
297
|
const tcResult = await this.db
|
|
298
|
-
.selectFrom('
|
|
299
|
-
.select(['id', '
|
|
300
|
-
.where('
|
|
301
|
-
.orderBy('
|
|
298
|
+
.selectFrom('aiToolCall')
|
|
299
|
+
.select(['id', 'messageId', 'toolName', 'args', 'result'])
|
|
300
|
+
.where('threadId', '=', threadId)
|
|
301
|
+
.orderBy('createdAt', 'asc')
|
|
302
302
|
.execute()
|
|
303
303
|
|
|
304
304
|
const tcByMessage = new Map<string, (typeof tcResult)[number][]>()
|
|
305
305
|
for (const tc of tcResult) {
|
|
306
|
-
const msgId = tc.
|
|
306
|
+
const msgId = tc.messageId
|
|
307
307
|
if (!tcByMessage.has(msgId)) tcByMessage.set(msgId, [])
|
|
308
308
|
tcByMessage.get(msgId)!.push(tc)
|
|
309
309
|
}
|
|
@@ -328,14 +328,14 @@ export class KyselyAIStorageService
|
|
|
328
328
|
id: row.id,
|
|
329
329
|
role: row.role as AIMessage['role'],
|
|
330
330
|
content: parsedContent,
|
|
331
|
-
createdAt: new Date(row.
|
|
331
|
+
createdAt: new Date(row.createdAt),
|
|
332
332
|
}
|
|
333
333
|
|
|
334
334
|
const tcs = tcByMessage.get(msg.id)
|
|
335
335
|
if (tcs?.length) {
|
|
336
336
|
msg.toolCalls = tcs.map((tc) => ({
|
|
337
337
|
id: tc.id,
|
|
338
|
-
name: tc.
|
|
338
|
+
name: tc.toolName,
|
|
339
339
|
args: parseJson(tc.args) as Record<string, unknown>,
|
|
340
340
|
}))
|
|
341
341
|
|
|
@@ -347,7 +347,7 @@ export class KyselyAIStorageService
|
|
|
347
347
|
role: 'tool',
|
|
348
348
|
toolResults: completed.map((tc) => ({
|
|
349
349
|
id: tc.id,
|
|
350
|
-
name: tc.
|
|
350
|
+
name: tc.toolName,
|
|
351
351
|
result: tc.result!,
|
|
352
352
|
})),
|
|
353
353
|
createdAt: msg.createdAt,
|
|
@@ -370,14 +370,14 @@ export class KyselyAIStorageService
|
|
|
370
370
|
|
|
371
371
|
if (nonToolMessages.length > 0) {
|
|
372
372
|
await this.db
|
|
373
|
-
.insertInto('
|
|
373
|
+
.insertInto('aiMessage')
|
|
374
374
|
.values(
|
|
375
375
|
nonToolMessages.map((msg) => ({
|
|
376
376
|
id: msg.id,
|
|
377
|
-
|
|
377
|
+
threadId: threadId,
|
|
378
378
|
role: msg.role,
|
|
379
379
|
content: msg.content != null ? JSON.stringify(msg.content) : null,
|
|
380
|
-
|
|
380
|
+
createdAt: msg.createdAt ?? new Date(),
|
|
381
381
|
}))
|
|
382
382
|
)
|
|
383
383
|
.execute()
|
|
@@ -388,13 +388,13 @@ export class KyselyAIStorageService
|
|
|
388
388
|
)
|
|
389
389
|
if (toolCalls.length > 0) {
|
|
390
390
|
await this.db
|
|
391
|
-
.insertInto('
|
|
391
|
+
.insertInto('aiToolCall')
|
|
392
392
|
.values(
|
|
393
393
|
toolCalls.map((tc) => ({
|
|
394
394
|
id: tc.id,
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
395
|
+
threadId: threadId,
|
|
396
|
+
messageId: tc.messageId,
|
|
397
|
+
toolName: tc.name,
|
|
398
398
|
args: JSON.stringify(tc.args),
|
|
399
399
|
}))
|
|
400
400
|
)
|
|
@@ -405,7 +405,7 @@ export class KyselyAIStorageService
|
|
|
405
405
|
if (!toolMsg.toolResults) continue
|
|
406
406
|
for (const tr of toolMsg.toolResults) {
|
|
407
407
|
await this.db
|
|
408
|
-
.updateTable('
|
|
408
|
+
.updateTable('aiToolCall')
|
|
409
409
|
.set({ result: tr.result })
|
|
410
410
|
.where('id', '=', tr.id)
|
|
411
411
|
.execute()
|
|
@@ -413,8 +413,8 @@ export class KyselyAIStorageService
|
|
|
413
413
|
}
|
|
414
414
|
|
|
415
415
|
await this.db
|
|
416
|
-
.updateTable('
|
|
417
|
-
.set({
|
|
416
|
+
.updateTable('aiThreads')
|
|
417
|
+
.set({ updatedAt: new Date() })
|
|
418
418
|
.where('id', '=', threadId)
|
|
419
419
|
.execute()
|
|
420
420
|
}
|
|
@@ -424,7 +424,7 @@ export class KyselyAIStorageService
|
|
|
424
424
|
scope: 'resource' | 'thread'
|
|
425
425
|
): Promise<Record<string, unknown> | null> {
|
|
426
426
|
const row = await this.db
|
|
427
|
-
.selectFrom('
|
|
427
|
+
.selectFrom('aiWorkingMemory')
|
|
428
428
|
.select('data')
|
|
429
429
|
.where('id', '=', id)
|
|
430
430
|
.where('scope', '=', scope)
|
|
@@ -440,17 +440,17 @@ export class KyselyAIStorageService
|
|
|
440
440
|
data: Record<string, unknown>
|
|
441
441
|
): Promise<void> {
|
|
442
442
|
await this.db
|
|
443
|
-
.insertInto('
|
|
443
|
+
.insertInto('aiWorkingMemory')
|
|
444
444
|
.values({
|
|
445
445
|
id,
|
|
446
446
|
scope,
|
|
447
447
|
data: JSON.stringify(data),
|
|
448
|
-
|
|
448
|
+
updatedAt: new Date(),
|
|
449
449
|
})
|
|
450
450
|
.onConflict((oc) =>
|
|
451
451
|
oc.columns(['id', 'scope']).doUpdateSet({
|
|
452
452
|
data: JSON.stringify(data),
|
|
453
|
-
|
|
453
|
+
updatedAt: new Date(),
|
|
454
454
|
})
|
|
455
455
|
)
|
|
456
456
|
.execute()
|
|
@@ -460,21 +460,21 @@ export class KyselyAIStorageService
|
|
|
460
460
|
const runId = crypto.randomUUID()
|
|
461
461
|
|
|
462
462
|
await this.db
|
|
463
|
-
.insertInto('
|
|
463
|
+
.insertInto('aiRun')
|
|
464
464
|
.values({
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
465
|
+
runId: runId,
|
|
466
|
+
agentName: run.agentName,
|
|
467
|
+
threadId: run.threadId,
|
|
468
|
+
resourceId: run.resourceId,
|
|
469
469
|
status: run.status,
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
470
|
+
errorMessage: run.errorMessage ?? null,
|
|
471
|
+
suspendReason: run.suspendReason ?? null,
|
|
472
|
+
missingRpcs: run.missingRpcs ? JSON.stringify(run.missingRpcs) : null,
|
|
473
|
+
usageInputTokens: run.usage.inputTokens,
|
|
474
|
+
usageOutputTokens: run.usage.outputTokens,
|
|
475
|
+
usageModel: run.usage.model,
|
|
476
|
+
createdAt: run.createdAt,
|
|
477
|
+
updatedAt: run.updatedAt,
|
|
478
478
|
})
|
|
479
479
|
.execute()
|
|
480
480
|
|
|
@@ -489,45 +489,45 @@ export class KyselyAIStorageService
|
|
|
489
489
|
runId: string,
|
|
490
490
|
updates: Partial<AgentRunState>
|
|
491
491
|
): Promise<void> {
|
|
492
|
-
const setValues: Record<string,
|
|
492
|
+
const setValues: Record<string, unknown> = { updatedAt: new Date() }
|
|
493
493
|
|
|
494
494
|
if (updates.status !== undefined) {
|
|
495
495
|
setValues.status = updates.status
|
|
496
496
|
}
|
|
497
497
|
if (updates.errorMessage !== undefined) {
|
|
498
|
-
setValues.
|
|
498
|
+
setValues.errorMessage = updates.errorMessage
|
|
499
499
|
}
|
|
500
500
|
if (updates.suspendReason !== undefined) {
|
|
501
|
-
setValues.
|
|
501
|
+
setValues.suspendReason = updates.suspendReason
|
|
502
502
|
}
|
|
503
503
|
if (updates.missingRpcs !== undefined) {
|
|
504
|
-
setValues.
|
|
504
|
+
setValues.missingRpcs = JSON.stringify(updates.missingRpcs)
|
|
505
505
|
}
|
|
506
506
|
if (updates.usage !== undefined) {
|
|
507
|
-
setValues.
|
|
508
|
-
setValues.
|
|
509
|
-
setValues.
|
|
507
|
+
setValues.usageInputTokens = updates.usage.inputTokens
|
|
508
|
+
setValues.usageOutputTokens = updates.usage.outputTokens
|
|
509
|
+
setValues.usageModel = updates.usage.model
|
|
510
510
|
}
|
|
511
511
|
|
|
512
512
|
await this.db
|
|
513
|
-
.updateTable('
|
|
513
|
+
.updateTable('aiRun')
|
|
514
514
|
.set(setValues)
|
|
515
|
-
.where('
|
|
515
|
+
.where('runId', '=', runId)
|
|
516
516
|
.execute()
|
|
517
517
|
|
|
518
518
|
if (updates.pendingApprovals !== undefined) {
|
|
519
519
|
await this.db
|
|
520
|
-
.updateTable('
|
|
520
|
+
.updateTable('aiToolCall')
|
|
521
521
|
.set({
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
522
|
+
approvalStatus: null,
|
|
523
|
+
runId: null,
|
|
524
|
+
approvalType: null,
|
|
525
|
+
agentRunId: null,
|
|
526
|
+
displayToolName: null,
|
|
527
|
+
displayArgs: null,
|
|
528
528
|
})
|
|
529
|
-
.where('
|
|
530
|
-
.where('
|
|
529
|
+
.where('runId', '=', runId)
|
|
530
|
+
.where('approvalStatus', 'is not', null)
|
|
531
531
|
.execute()
|
|
532
532
|
|
|
533
533
|
if (updates.pendingApprovals.length) {
|
|
@@ -538,40 +538,40 @@ export class KyselyAIStorageService
|
|
|
538
538
|
|
|
539
539
|
async getRun(runId: string): Promise<AgentRunState | null> {
|
|
540
540
|
const row = await this.db
|
|
541
|
-
.selectFrom('
|
|
541
|
+
.selectFrom('aiRun')
|
|
542
542
|
.select([
|
|
543
|
-
'
|
|
544
|
-
'
|
|
545
|
-
'
|
|
546
|
-
'
|
|
543
|
+
'runId',
|
|
544
|
+
'agentName',
|
|
545
|
+
'threadId',
|
|
546
|
+
'resourceId',
|
|
547
547
|
'status',
|
|
548
|
-
'
|
|
549
|
-
'
|
|
550
|
-
'
|
|
551
|
-
'
|
|
552
|
-
'
|
|
553
|
-
'
|
|
554
|
-
'
|
|
555
|
-
'
|
|
548
|
+
'errorMessage',
|
|
549
|
+
'suspendReason',
|
|
550
|
+
'missingRpcs',
|
|
551
|
+
'usageInputTokens',
|
|
552
|
+
'usageOutputTokens',
|
|
553
|
+
'usageModel',
|
|
554
|
+
'createdAt',
|
|
555
|
+
'updatedAt',
|
|
556
556
|
])
|
|
557
|
-
.where('
|
|
557
|
+
.where('runId', '=', runId)
|
|
558
558
|
.executeTakeFirst()
|
|
559
559
|
|
|
560
560
|
if (!row) return null
|
|
561
561
|
|
|
562
562
|
const approvals = await this.db
|
|
563
|
-
.selectFrom('
|
|
563
|
+
.selectFrom('aiToolCall')
|
|
564
564
|
.select([
|
|
565
565
|
'id',
|
|
566
|
-
'
|
|
566
|
+
'toolName',
|
|
567
567
|
'args',
|
|
568
|
-
'
|
|
569
|
-
'
|
|
570
|
-
'
|
|
571
|
-
'
|
|
568
|
+
'approvalType',
|
|
569
|
+
'agentRunId',
|
|
570
|
+
'displayToolName',
|
|
571
|
+
'displayArgs',
|
|
572
572
|
])
|
|
573
|
-
.where('
|
|
574
|
-
.where('
|
|
573
|
+
.where('runId', '=', runId)
|
|
574
|
+
.where('approvalStatus', '=', 'pending')
|
|
575
575
|
.execute()
|
|
576
576
|
|
|
577
577
|
return this.mapRunRow(row, approvals)
|
|
@@ -579,41 +579,41 @@ export class KyselyAIStorageService
|
|
|
579
579
|
|
|
580
580
|
async getRunsByThread(threadId: string): Promise<AgentRunState[]> {
|
|
581
581
|
const result = await this.db
|
|
582
|
-
.selectFrom('
|
|
582
|
+
.selectFrom('aiRun')
|
|
583
583
|
.select([
|
|
584
|
-
'
|
|
585
|
-
'
|
|
586
|
-
'
|
|
587
|
-
'
|
|
584
|
+
'runId',
|
|
585
|
+
'agentName',
|
|
586
|
+
'threadId',
|
|
587
|
+
'resourceId',
|
|
588
588
|
'status',
|
|
589
|
-
'
|
|
590
|
-
'
|
|
591
|
-
'
|
|
592
|
-
'
|
|
593
|
-
'
|
|
594
|
-
'
|
|
595
|
-
'
|
|
596
|
-
'
|
|
589
|
+
'errorMessage',
|
|
590
|
+
'suspendReason',
|
|
591
|
+
'missingRpcs',
|
|
592
|
+
'usageInputTokens',
|
|
593
|
+
'usageOutputTokens',
|
|
594
|
+
'usageModel',
|
|
595
|
+
'createdAt',
|
|
596
|
+
'updatedAt',
|
|
597
597
|
])
|
|
598
|
-
.where('
|
|
599
|
-
.orderBy('
|
|
598
|
+
.where('threadId', '=', threadId)
|
|
599
|
+
.orderBy('createdAt', 'desc')
|
|
600
600
|
.execute()
|
|
601
601
|
|
|
602
602
|
const runs: AgentRunState[] = []
|
|
603
603
|
for (const row of result) {
|
|
604
604
|
const approvals = await this.db
|
|
605
|
-
.selectFrom('
|
|
605
|
+
.selectFrom('aiToolCall')
|
|
606
606
|
.select([
|
|
607
607
|
'id',
|
|
608
|
-
'
|
|
608
|
+
'toolName',
|
|
609
609
|
'args',
|
|
610
|
-
'
|
|
611
|
-
'
|
|
612
|
-
'
|
|
613
|
-
'
|
|
610
|
+
'approvalType',
|
|
611
|
+
'agentRunId',
|
|
612
|
+
'displayToolName',
|
|
613
|
+
'displayArgs',
|
|
614
614
|
])
|
|
615
|
-
.where('
|
|
616
|
-
.where('
|
|
615
|
+
.where('runId', '=', row.runId)
|
|
616
|
+
.where('approvalStatus', '=', 'pending')
|
|
617
617
|
.execute()
|
|
618
618
|
runs.push(this.mapRunRow(row, approvals))
|
|
619
619
|
}
|
|
@@ -627,24 +627,24 @@ export class KyselyAIStorageService
|
|
|
627
627
|
for (const a of approvals) {
|
|
628
628
|
if (a.type === 'agent-call') {
|
|
629
629
|
await this.db
|
|
630
|
-
.updateTable('
|
|
630
|
+
.updateTable('aiToolCall')
|
|
631
631
|
.set({
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
632
|
+
approvalStatus: 'pending',
|
|
633
|
+
runId: runId,
|
|
634
|
+
approvalType: 'agent-call',
|
|
635
|
+
agentRunId: a.agentRunId,
|
|
636
|
+
displayToolName: a.displayToolName,
|
|
637
|
+
displayArgs: JSON.stringify(a.displayArgs),
|
|
638
638
|
})
|
|
639
639
|
.where('id', '=', a.toolCallId)
|
|
640
640
|
.execute()
|
|
641
641
|
} else {
|
|
642
642
|
await this.db
|
|
643
|
-
.updateTable('
|
|
643
|
+
.updateTable('aiToolCall')
|
|
644
644
|
.set({
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
645
|
+
approvalStatus: 'pending',
|
|
646
|
+
runId: runId,
|
|
647
|
+
approvalType: 'tool-call',
|
|
648
648
|
})
|
|
649
649
|
.where('id', '=', a.toolCallId)
|
|
650
650
|
.execute()
|
|
@@ -655,42 +655,42 @@ export class KyselyAIStorageService
|
|
|
655
655
|
private mapRunRow(row: any, approvalRows?: any[]): AgentRunState {
|
|
656
656
|
const pendingApprovals = approvalRows?.length
|
|
657
657
|
? approvalRows.map((a: any) => {
|
|
658
|
-
if (a.
|
|
658
|
+
if (a.approvalType === 'agent-call') {
|
|
659
659
|
return {
|
|
660
660
|
type: 'agent-call' as const,
|
|
661
661
|
toolCallId: a.id as string,
|
|
662
|
-
agentName: a.
|
|
663
|
-
agentRunId: a.
|
|
664
|
-
displayToolName: a.
|
|
665
|
-
displayArgs: parseJson(a.
|
|
662
|
+
agentName: a.toolName as string,
|
|
663
|
+
agentRunId: a.agentRunId as string,
|
|
664
|
+
displayToolName: a.displayToolName as string,
|
|
665
|
+
displayArgs: parseJson(a.displayArgs) as unknown,
|
|
666
666
|
}
|
|
667
667
|
}
|
|
668
668
|
return {
|
|
669
669
|
type: 'tool-call' as const,
|
|
670
670
|
toolCallId: a.id as string,
|
|
671
|
-
toolName: a.
|
|
671
|
+
toolName: a.toolName as string,
|
|
672
672
|
args: parseJson(a.args) as unknown,
|
|
673
673
|
}
|
|
674
674
|
})
|
|
675
675
|
: undefined
|
|
676
676
|
|
|
677
677
|
return {
|
|
678
|
-
runId: row.
|
|
679
|
-
agentName: row.
|
|
680
|
-
threadId: row.
|
|
681
|
-
resourceId: row.
|
|
678
|
+
runId: row.runId as string,
|
|
679
|
+
agentName: row.agentName as string,
|
|
680
|
+
threadId: row.threadId as string,
|
|
681
|
+
resourceId: row.resourceId as string,
|
|
682
682
|
status: row.status as AgentRunState['status'],
|
|
683
|
-
errorMessage: row.
|
|
684
|
-
suspendReason: row.
|
|
685
|
-
missingRpcs: parseJson(row.
|
|
683
|
+
errorMessage: row.errorMessage ?? undefined,
|
|
684
|
+
suspendReason: row.suspendReason as AgentRunState['suspendReason'],
|
|
685
|
+
missingRpcs: parseJson(row.missingRpcs),
|
|
686
686
|
pendingApprovals,
|
|
687
687
|
usage: {
|
|
688
|
-
inputTokens: Number(row.
|
|
689
|
-
outputTokens: Number(row.
|
|
690
|
-
model: row.
|
|
688
|
+
inputTokens: Number(row.usageInputTokens),
|
|
689
|
+
outputTokens: Number(row.usageOutputTokens),
|
|
690
|
+
model: row.usageModel as string,
|
|
691
691
|
},
|
|
692
|
-
createdAt: new Date(row.
|
|
693
|
-
updatedAt: new Date(row.
|
|
692
|
+
createdAt: new Date(row.createdAt as string),
|
|
693
|
+
updatedAt: new Date(row.updatedAt as string),
|
|
694
694
|
}
|
|
695
695
|
}
|
|
696
696
|
|
|
@@ -699,41 +699,41 @@ export class KyselyAIStorageService
|
|
|
699
699
|
approval: NonNullable<AgentRunState['pendingApprovals']>[number]
|
|
700
700
|
} | null> {
|
|
701
701
|
const tc = await this.db
|
|
702
|
-
.selectFrom('
|
|
702
|
+
.selectFrom('aiToolCall')
|
|
703
703
|
.select([
|
|
704
704
|
'id',
|
|
705
|
-
'
|
|
705
|
+
'toolName',
|
|
706
706
|
'args',
|
|
707
|
-
'
|
|
708
|
-
'
|
|
709
|
-
'
|
|
710
|
-
'
|
|
711
|
-
'
|
|
707
|
+
'runId',
|
|
708
|
+
'approvalType',
|
|
709
|
+
'agentRunId',
|
|
710
|
+
'displayToolName',
|
|
711
|
+
'displayArgs',
|
|
712
712
|
])
|
|
713
713
|
.where('id', '=', toolCallId)
|
|
714
|
-
.where('
|
|
714
|
+
.where('approvalStatus', '=', 'pending')
|
|
715
715
|
.executeTakeFirst()
|
|
716
716
|
|
|
717
|
-
if (!tc || !tc.
|
|
717
|
+
if (!tc || !tc.runId) return null
|
|
718
718
|
|
|
719
|
-
const run = await this.getRun(tc.
|
|
719
|
+
const run = await this.getRun(tc.runId)
|
|
720
720
|
if (!run) return null
|
|
721
721
|
|
|
722
722
|
let approval: NonNullable<AgentRunState['pendingApprovals']>[number]
|
|
723
|
-
if (tc.
|
|
723
|
+
if (tc.approvalType === 'agent-call') {
|
|
724
724
|
approval = {
|
|
725
725
|
type: 'agent-call',
|
|
726
726
|
toolCallId: tc.id,
|
|
727
|
-
agentName: tc.
|
|
728
|
-
agentRunId: tc.
|
|
729
|
-
displayToolName: tc.
|
|
730
|
-
displayArgs: parseJson(tc.
|
|
727
|
+
agentName: tc.toolName,
|
|
728
|
+
agentRunId: tc.agentRunId!,
|
|
729
|
+
displayToolName: tc.displayToolName!,
|
|
730
|
+
displayArgs: parseJson(tc.displayArgs) as unknown,
|
|
731
731
|
}
|
|
732
732
|
} else {
|
|
733
733
|
approval = {
|
|
734
734
|
type: 'tool-call',
|
|
735
735
|
toolCallId: tc.id,
|
|
736
|
-
toolName: tc.
|
|
736
|
+
toolName: tc.toolName,
|
|
737
737
|
args: parseJson(tc.args) as unknown,
|
|
738
738
|
}
|
|
739
739
|
}
|
|
@@ -746,8 +746,8 @@ export class KyselyAIStorageService
|
|
|
746
746
|
status: 'approved' | 'denied'
|
|
747
747
|
): Promise<void> {
|
|
748
748
|
await this.db
|
|
749
|
-
.updateTable('
|
|
750
|
-
.set({
|
|
749
|
+
.updateTable('aiToolCall')
|
|
750
|
+
.set({ approvalStatus: status })
|
|
751
751
|
.where('id', '=', toolCallId)
|
|
752
752
|
.execute()
|
|
753
753
|
}
|