@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
|
@@ -114,14 +114,14 @@ export class KyselyAIStorageService {
|
|
|
114
114
|
const id = options?.threadId ?? crypto.randomUUID();
|
|
115
115
|
const now = new Date();
|
|
116
116
|
await this.db
|
|
117
|
-
.insertInto('
|
|
117
|
+
.insertInto('aiThreads')
|
|
118
118
|
.values({
|
|
119
119
|
id,
|
|
120
|
-
|
|
120
|
+
resourceId: resourceId,
|
|
121
121
|
title: options?.title ?? null,
|
|
122
122
|
metadata: JSON.stringify(options?.metadata ?? null),
|
|
123
|
-
|
|
124
|
-
|
|
123
|
+
createdAt: now,
|
|
124
|
+
updatedAt: now,
|
|
125
125
|
})
|
|
126
126
|
.execute();
|
|
127
127
|
return {
|
|
@@ -135,14 +135,14 @@ export class KyselyAIStorageService {
|
|
|
135
135
|
}
|
|
136
136
|
async getThread(threadId) {
|
|
137
137
|
const row = await this.db
|
|
138
|
-
.selectFrom('
|
|
138
|
+
.selectFrom('aiThreads')
|
|
139
139
|
.select([
|
|
140
140
|
'id',
|
|
141
|
-
'
|
|
141
|
+
'resourceId',
|
|
142
142
|
'title',
|
|
143
143
|
'metadata',
|
|
144
|
-
'
|
|
145
|
-
'
|
|
144
|
+
'createdAt',
|
|
145
|
+
'updatedAt',
|
|
146
146
|
])
|
|
147
147
|
.where('id', '=', threadId)
|
|
148
148
|
.executeTakeFirst();
|
|
@@ -151,75 +151,75 @@ export class KyselyAIStorageService {
|
|
|
151
151
|
}
|
|
152
152
|
return {
|
|
153
153
|
id: row.id,
|
|
154
|
-
resourceId: row.
|
|
154
|
+
resourceId: row.resourceId,
|
|
155
155
|
title: row.title ?? undefined,
|
|
156
156
|
metadata: parseJson(row.metadata),
|
|
157
|
-
createdAt: new Date(row.
|
|
158
|
-
updatedAt: new Date(row.
|
|
157
|
+
createdAt: new Date(row.createdAt),
|
|
158
|
+
updatedAt: new Date(row.updatedAt),
|
|
159
159
|
};
|
|
160
160
|
}
|
|
161
161
|
async getThreads(resourceId) {
|
|
162
162
|
const result = await this.db
|
|
163
|
-
.selectFrom('
|
|
163
|
+
.selectFrom('aiThreads')
|
|
164
164
|
.select([
|
|
165
165
|
'id',
|
|
166
|
-
'
|
|
166
|
+
'resourceId',
|
|
167
167
|
'title',
|
|
168
168
|
'metadata',
|
|
169
|
-
'
|
|
170
|
-
'
|
|
169
|
+
'createdAt',
|
|
170
|
+
'updatedAt',
|
|
171
171
|
])
|
|
172
|
-
.where('
|
|
173
|
-
.orderBy('
|
|
172
|
+
.where('resourceId', '=', resourceId)
|
|
173
|
+
.orderBy('updatedAt', 'desc')
|
|
174
174
|
.execute();
|
|
175
175
|
return result.map((row) => ({
|
|
176
176
|
id: row.id,
|
|
177
|
-
resourceId: row.
|
|
177
|
+
resourceId: row.resourceId,
|
|
178
178
|
title: row.title ?? undefined,
|
|
179
179
|
metadata: parseJson(row.metadata),
|
|
180
|
-
createdAt: new Date(row.
|
|
181
|
-
updatedAt: new Date(row.
|
|
180
|
+
createdAt: new Date(row.createdAt),
|
|
181
|
+
updatedAt: new Date(row.updatedAt),
|
|
182
182
|
}));
|
|
183
183
|
}
|
|
184
184
|
async deleteThread(threadId) {
|
|
185
|
-
await this.db.deleteFrom('
|
|
185
|
+
await this.db.deleteFrom('aiThreads').where('id', '=', threadId).execute();
|
|
186
186
|
}
|
|
187
187
|
async getMessages(threadId, options) {
|
|
188
188
|
let msgQuery = this.db
|
|
189
|
-
.selectFrom('
|
|
190
|
-
.select(['id', 'role', 'content', '
|
|
191
|
-
.where('
|
|
189
|
+
.selectFrom('aiMessage')
|
|
190
|
+
.select(['id', 'role', 'content', 'createdAt'])
|
|
191
|
+
.where('threadId', '=', threadId);
|
|
192
192
|
if (options?.cursor) {
|
|
193
193
|
const cursorRow = await this.db
|
|
194
|
-
.selectFrom('
|
|
195
|
-
.select('
|
|
194
|
+
.selectFrom('aiMessage')
|
|
195
|
+
.select('createdAt')
|
|
196
196
|
.where('id', '=', options.cursor)
|
|
197
197
|
.executeTakeFirst();
|
|
198
198
|
if (cursorRow) {
|
|
199
|
-
msgQuery = msgQuery.where('
|
|
199
|
+
msgQuery = msgQuery.where('createdAt', '<', cursorRow.createdAt);
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
202
|
let msgResult;
|
|
203
203
|
if (options?.cursor || options?.lastN) {
|
|
204
204
|
const innerResult = await msgQuery
|
|
205
|
-
.orderBy('
|
|
205
|
+
.orderBy('createdAt', 'desc')
|
|
206
206
|
.limit(options?.lastN ?? 50)
|
|
207
207
|
.execute();
|
|
208
208
|
innerResult.reverse();
|
|
209
209
|
msgResult = innerResult;
|
|
210
210
|
}
|
|
211
211
|
else {
|
|
212
|
-
msgResult = await msgQuery.orderBy('
|
|
212
|
+
msgResult = await msgQuery.orderBy('createdAt', 'asc').execute();
|
|
213
213
|
}
|
|
214
214
|
const tcResult = await this.db
|
|
215
|
-
.selectFrom('
|
|
216
|
-
.select(['id', '
|
|
217
|
-
.where('
|
|
218
|
-
.orderBy('
|
|
215
|
+
.selectFrom('aiToolCall')
|
|
216
|
+
.select(['id', 'messageId', 'toolName', 'args', 'result'])
|
|
217
|
+
.where('threadId', '=', threadId)
|
|
218
|
+
.orderBy('createdAt', 'asc')
|
|
219
219
|
.execute();
|
|
220
220
|
const tcByMessage = new Map();
|
|
221
221
|
for (const tc of tcResult) {
|
|
222
|
-
const msgId = tc.
|
|
222
|
+
const msgId = tc.messageId;
|
|
223
223
|
if (!tcByMessage.has(msgId))
|
|
224
224
|
tcByMessage.set(msgId, []);
|
|
225
225
|
tcByMessage.get(msgId).push(tc);
|
|
@@ -246,13 +246,13 @@ export class KyselyAIStorageService {
|
|
|
246
246
|
id: row.id,
|
|
247
247
|
role: row.role,
|
|
248
248
|
content: parsedContent,
|
|
249
|
-
createdAt: new Date(row.
|
|
249
|
+
createdAt: new Date(row.createdAt),
|
|
250
250
|
};
|
|
251
251
|
const tcs = tcByMessage.get(msg.id);
|
|
252
252
|
if (tcs?.length) {
|
|
253
253
|
msg.toolCalls = tcs.map((tc) => ({
|
|
254
254
|
id: tc.id,
|
|
255
|
-
name: tc.
|
|
255
|
+
name: tc.toolName,
|
|
256
256
|
args: parseJson(tc.args),
|
|
257
257
|
}));
|
|
258
258
|
const completed = tcs.filter((tc) => tc.result != null);
|
|
@@ -263,7 +263,7 @@ export class KyselyAIStorageService {
|
|
|
263
263
|
role: 'tool',
|
|
264
264
|
toolResults: completed.map((tc) => ({
|
|
265
265
|
id: tc.id,
|
|
266
|
-
name: tc.
|
|
266
|
+
name: tc.toolName,
|
|
267
267
|
result: tc.result,
|
|
268
268
|
})),
|
|
269
269
|
createdAt: msg.createdAt,
|
|
@@ -282,25 +282,25 @@ export class KyselyAIStorageService {
|
|
|
282
282
|
const nonToolMessages = messages.filter((m) => m.role !== 'tool');
|
|
283
283
|
if (nonToolMessages.length > 0) {
|
|
284
284
|
await this.db
|
|
285
|
-
.insertInto('
|
|
285
|
+
.insertInto('aiMessage')
|
|
286
286
|
.values(nonToolMessages.map((msg) => ({
|
|
287
287
|
id: msg.id,
|
|
288
|
-
|
|
288
|
+
threadId: threadId,
|
|
289
289
|
role: msg.role,
|
|
290
290
|
content: msg.content != null ? JSON.stringify(msg.content) : null,
|
|
291
|
-
|
|
291
|
+
createdAt: msg.createdAt ?? new Date(),
|
|
292
292
|
})))
|
|
293
293
|
.execute();
|
|
294
294
|
}
|
|
295
295
|
const toolCalls = nonToolMessages.flatMap((msg) => msg.toolCalls?.map((tc) => ({ ...tc, messageId: msg.id })) ?? []);
|
|
296
296
|
if (toolCalls.length > 0) {
|
|
297
297
|
await this.db
|
|
298
|
-
.insertInto('
|
|
298
|
+
.insertInto('aiToolCall')
|
|
299
299
|
.values(toolCalls.map((tc) => ({
|
|
300
300
|
id: tc.id,
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
301
|
+
threadId: threadId,
|
|
302
|
+
messageId: tc.messageId,
|
|
303
|
+
toolName: tc.name,
|
|
304
304
|
args: JSON.stringify(tc.args),
|
|
305
305
|
})))
|
|
306
306
|
.execute();
|
|
@@ -310,21 +310,21 @@ export class KyselyAIStorageService {
|
|
|
310
310
|
continue;
|
|
311
311
|
for (const tr of toolMsg.toolResults) {
|
|
312
312
|
await this.db
|
|
313
|
-
.updateTable('
|
|
313
|
+
.updateTable('aiToolCall')
|
|
314
314
|
.set({ result: tr.result })
|
|
315
315
|
.where('id', '=', tr.id)
|
|
316
316
|
.execute();
|
|
317
317
|
}
|
|
318
318
|
}
|
|
319
319
|
await this.db
|
|
320
|
-
.updateTable('
|
|
321
|
-
.set({
|
|
320
|
+
.updateTable('aiThreads')
|
|
321
|
+
.set({ updatedAt: new Date() })
|
|
322
322
|
.where('id', '=', threadId)
|
|
323
323
|
.execute();
|
|
324
324
|
}
|
|
325
325
|
async getWorkingMemory(id, scope) {
|
|
326
326
|
const row = await this.db
|
|
327
|
-
.selectFrom('
|
|
327
|
+
.selectFrom('aiWorkingMemory')
|
|
328
328
|
.select('data')
|
|
329
329
|
.where('id', '=', id)
|
|
330
330
|
.where('scope', '=', scope)
|
|
@@ -335,37 +335,37 @@ export class KyselyAIStorageService {
|
|
|
335
335
|
}
|
|
336
336
|
async saveWorkingMemory(id, scope, data) {
|
|
337
337
|
await this.db
|
|
338
|
-
.insertInto('
|
|
338
|
+
.insertInto('aiWorkingMemory')
|
|
339
339
|
.values({
|
|
340
340
|
id,
|
|
341
341
|
scope,
|
|
342
342
|
data: JSON.stringify(data),
|
|
343
|
-
|
|
343
|
+
updatedAt: new Date(),
|
|
344
344
|
})
|
|
345
345
|
.onConflict((oc) => oc.columns(['id', 'scope']).doUpdateSet({
|
|
346
346
|
data: JSON.stringify(data),
|
|
347
|
-
|
|
347
|
+
updatedAt: new Date(),
|
|
348
348
|
}))
|
|
349
349
|
.execute();
|
|
350
350
|
}
|
|
351
351
|
async createRun(run) {
|
|
352
352
|
const runId = crypto.randomUUID();
|
|
353
353
|
await this.db
|
|
354
|
-
.insertInto('
|
|
354
|
+
.insertInto('aiRun')
|
|
355
355
|
.values({
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
356
|
+
runId: runId,
|
|
357
|
+
agentName: run.agentName,
|
|
358
|
+
threadId: run.threadId,
|
|
359
|
+
resourceId: run.resourceId,
|
|
360
360
|
status: run.status,
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
361
|
+
errorMessage: run.errorMessage ?? null,
|
|
362
|
+
suspendReason: run.suspendReason ?? null,
|
|
363
|
+
missingRpcs: run.missingRpcs ? JSON.stringify(run.missingRpcs) : null,
|
|
364
|
+
usageInputTokens: run.usage.inputTokens,
|
|
365
|
+
usageOutputTokens: run.usage.outputTokens,
|
|
366
|
+
usageModel: run.usage.model,
|
|
367
|
+
createdAt: run.createdAt,
|
|
368
|
+
updatedAt: run.updatedAt,
|
|
369
369
|
})
|
|
370
370
|
.execute();
|
|
371
371
|
if (run.pendingApprovals?.length) {
|
|
@@ -374,42 +374,42 @@ export class KyselyAIStorageService {
|
|
|
374
374
|
return runId;
|
|
375
375
|
}
|
|
376
376
|
async updateRun(runId, updates) {
|
|
377
|
-
const setValues = {
|
|
377
|
+
const setValues = { updatedAt: new Date() };
|
|
378
378
|
if (updates.status !== undefined) {
|
|
379
379
|
setValues.status = updates.status;
|
|
380
380
|
}
|
|
381
381
|
if (updates.errorMessage !== undefined) {
|
|
382
|
-
setValues.
|
|
382
|
+
setValues.errorMessage = updates.errorMessage;
|
|
383
383
|
}
|
|
384
384
|
if (updates.suspendReason !== undefined) {
|
|
385
|
-
setValues.
|
|
385
|
+
setValues.suspendReason = updates.suspendReason;
|
|
386
386
|
}
|
|
387
387
|
if (updates.missingRpcs !== undefined) {
|
|
388
|
-
setValues.
|
|
388
|
+
setValues.missingRpcs = JSON.stringify(updates.missingRpcs);
|
|
389
389
|
}
|
|
390
390
|
if (updates.usage !== undefined) {
|
|
391
|
-
setValues.
|
|
392
|
-
setValues.
|
|
393
|
-
setValues.
|
|
391
|
+
setValues.usageInputTokens = updates.usage.inputTokens;
|
|
392
|
+
setValues.usageOutputTokens = updates.usage.outputTokens;
|
|
393
|
+
setValues.usageModel = updates.usage.model;
|
|
394
394
|
}
|
|
395
395
|
await this.db
|
|
396
|
-
.updateTable('
|
|
396
|
+
.updateTable('aiRun')
|
|
397
397
|
.set(setValues)
|
|
398
|
-
.where('
|
|
398
|
+
.where('runId', '=', runId)
|
|
399
399
|
.execute();
|
|
400
400
|
if (updates.pendingApprovals !== undefined) {
|
|
401
401
|
await this.db
|
|
402
|
-
.updateTable('
|
|
402
|
+
.updateTable('aiToolCall')
|
|
403
403
|
.set({
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
404
|
+
approvalStatus: null,
|
|
405
|
+
runId: null,
|
|
406
|
+
approvalType: null,
|
|
407
|
+
agentRunId: null,
|
|
408
|
+
displayToolName: null,
|
|
409
|
+
displayArgs: null,
|
|
410
410
|
})
|
|
411
|
-
.where('
|
|
412
|
-
.where('
|
|
411
|
+
.where('runId', '=', runId)
|
|
412
|
+
.where('approvalStatus', 'is not', null)
|
|
413
413
|
.execute();
|
|
414
414
|
if (updates.pendingApprovals.length) {
|
|
415
415
|
await this.setApprovals(runId, updates.pendingApprovals);
|
|
@@ -418,78 +418,78 @@ export class KyselyAIStorageService {
|
|
|
418
418
|
}
|
|
419
419
|
async getRun(runId) {
|
|
420
420
|
const row = await this.db
|
|
421
|
-
.selectFrom('
|
|
421
|
+
.selectFrom('aiRun')
|
|
422
422
|
.select([
|
|
423
|
-
'
|
|
424
|
-
'
|
|
425
|
-
'
|
|
426
|
-
'
|
|
423
|
+
'runId',
|
|
424
|
+
'agentName',
|
|
425
|
+
'threadId',
|
|
426
|
+
'resourceId',
|
|
427
427
|
'status',
|
|
428
|
-
'
|
|
429
|
-
'
|
|
430
|
-
'
|
|
431
|
-
'
|
|
432
|
-
'
|
|
433
|
-
'
|
|
434
|
-
'
|
|
435
|
-
'
|
|
428
|
+
'errorMessage',
|
|
429
|
+
'suspendReason',
|
|
430
|
+
'missingRpcs',
|
|
431
|
+
'usageInputTokens',
|
|
432
|
+
'usageOutputTokens',
|
|
433
|
+
'usageModel',
|
|
434
|
+
'createdAt',
|
|
435
|
+
'updatedAt',
|
|
436
436
|
])
|
|
437
|
-
.where('
|
|
437
|
+
.where('runId', '=', runId)
|
|
438
438
|
.executeTakeFirst();
|
|
439
439
|
if (!row)
|
|
440
440
|
return null;
|
|
441
441
|
const approvals = await this.db
|
|
442
|
-
.selectFrom('
|
|
442
|
+
.selectFrom('aiToolCall')
|
|
443
443
|
.select([
|
|
444
444
|
'id',
|
|
445
|
-
'
|
|
445
|
+
'toolName',
|
|
446
446
|
'args',
|
|
447
|
-
'
|
|
448
|
-
'
|
|
449
|
-
'
|
|
450
|
-
'
|
|
447
|
+
'approvalType',
|
|
448
|
+
'agentRunId',
|
|
449
|
+
'displayToolName',
|
|
450
|
+
'displayArgs',
|
|
451
451
|
])
|
|
452
|
-
.where('
|
|
453
|
-
.where('
|
|
452
|
+
.where('runId', '=', runId)
|
|
453
|
+
.where('approvalStatus', '=', 'pending')
|
|
454
454
|
.execute();
|
|
455
455
|
return this.mapRunRow(row, approvals);
|
|
456
456
|
}
|
|
457
457
|
async getRunsByThread(threadId) {
|
|
458
458
|
const result = await this.db
|
|
459
|
-
.selectFrom('
|
|
459
|
+
.selectFrom('aiRun')
|
|
460
460
|
.select([
|
|
461
|
-
'
|
|
462
|
-
'
|
|
463
|
-
'
|
|
464
|
-
'
|
|
461
|
+
'runId',
|
|
462
|
+
'agentName',
|
|
463
|
+
'threadId',
|
|
464
|
+
'resourceId',
|
|
465
465
|
'status',
|
|
466
|
-
'
|
|
467
|
-
'
|
|
468
|
-
'
|
|
469
|
-
'
|
|
470
|
-
'
|
|
471
|
-
'
|
|
472
|
-
'
|
|
473
|
-
'
|
|
466
|
+
'errorMessage',
|
|
467
|
+
'suspendReason',
|
|
468
|
+
'missingRpcs',
|
|
469
|
+
'usageInputTokens',
|
|
470
|
+
'usageOutputTokens',
|
|
471
|
+
'usageModel',
|
|
472
|
+
'createdAt',
|
|
473
|
+
'updatedAt',
|
|
474
474
|
])
|
|
475
|
-
.where('
|
|
476
|
-
.orderBy('
|
|
475
|
+
.where('threadId', '=', threadId)
|
|
476
|
+
.orderBy('createdAt', 'desc')
|
|
477
477
|
.execute();
|
|
478
478
|
const runs = [];
|
|
479
479
|
for (const row of result) {
|
|
480
480
|
const approvals = await this.db
|
|
481
|
-
.selectFrom('
|
|
481
|
+
.selectFrom('aiToolCall')
|
|
482
482
|
.select([
|
|
483
483
|
'id',
|
|
484
|
-
'
|
|
484
|
+
'toolName',
|
|
485
485
|
'args',
|
|
486
|
-
'
|
|
487
|
-
'
|
|
488
|
-
'
|
|
489
|
-
'
|
|
486
|
+
'approvalType',
|
|
487
|
+
'agentRunId',
|
|
488
|
+
'displayToolName',
|
|
489
|
+
'displayArgs',
|
|
490
490
|
])
|
|
491
|
-
.where('
|
|
492
|
-
.where('
|
|
491
|
+
.where('runId', '=', row.runId)
|
|
492
|
+
.where('approvalStatus', '=', 'pending')
|
|
493
493
|
.execute();
|
|
494
494
|
runs.push(this.mapRunRow(row, approvals));
|
|
495
495
|
}
|
|
@@ -499,25 +499,25 @@ export class KyselyAIStorageService {
|
|
|
499
499
|
for (const a of approvals) {
|
|
500
500
|
if (a.type === 'agent-call') {
|
|
501
501
|
await this.db
|
|
502
|
-
.updateTable('
|
|
502
|
+
.updateTable('aiToolCall')
|
|
503
503
|
.set({
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
504
|
+
approvalStatus: 'pending',
|
|
505
|
+
runId: runId,
|
|
506
|
+
approvalType: 'agent-call',
|
|
507
|
+
agentRunId: a.agentRunId,
|
|
508
|
+
displayToolName: a.displayToolName,
|
|
509
|
+
displayArgs: JSON.stringify(a.displayArgs),
|
|
510
510
|
})
|
|
511
511
|
.where('id', '=', a.toolCallId)
|
|
512
512
|
.execute();
|
|
513
513
|
}
|
|
514
514
|
else {
|
|
515
515
|
await this.db
|
|
516
|
-
.updateTable('
|
|
516
|
+
.updateTable('aiToolCall')
|
|
517
517
|
.set({
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
518
|
+
approvalStatus: 'pending',
|
|
519
|
+
runId: runId,
|
|
520
|
+
approvalType: 'tool-call',
|
|
521
521
|
})
|
|
522
522
|
.where('id', '=', a.toolCallId)
|
|
523
523
|
.execute();
|
|
@@ -527,80 +527,80 @@ export class KyselyAIStorageService {
|
|
|
527
527
|
mapRunRow(row, approvalRows) {
|
|
528
528
|
const pendingApprovals = approvalRows?.length
|
|
529
529
|
? approvalRows.map((a) => {
|
|
530
|
-
if (a.
|
|
530
|
+
if (a.approvalType === 'agent-call') {
|
|
531
531
|
return {
|
|
532
532
|
type: 'agent-call',
|
|
533
533
|
toolCallId: a.id,
|
|
534
|
-
agentName: a.
|
|
535
|
-
agentRunId: a.
|
|
536
|
-
displayToolName: a.
|
|
537
|
-
displayArgs: parseJson(a.
|
|
534
|
+
agentName: a.toolName,
|
|
535
|
+
agentRunId: a.agentRunId,
|
|
536
|
+
displayToolName: a.displayToolName,
|
|
537
|
+
displayArgs: parseJson(a.displayArgs),
|
|
538
538
|
};
|
|
539
539
|
}
|
|
540
540
|
return {
|
|
541
541
|
type: 'tool-call',
|
|
542
542
|
toolCallId: a.id,
|
|
543
|
-
toolName: a.
|
|
543
|
+
toolName: a.toolName,
|
|
544
544
|
args: parseJson(a.args),
|
|
545
545
|
};
|
|
546
546
|
})
|
|
547
547
|
: undefined;
|
|
548
548
|
return {
|
|
549
|
-
runId: row.
|
|
550
|
-
agentName: row.
|
|
551
|
-
threadId: row.
|
|
552
|
-
resourceId: row.
|
|
549
|
+
runId: row.runId,
|
|
550
|
+
agentName: row.agentName,
|
|
551
|
+
threadId: row.threadId,
|
|
552
|
+
resourceId: row.resourceId,
|
|
553
553
|
status: row.status,
|
|
554
|
-
errorMessage: row.
|
|
555
|
-
suspendReason: row.
|
|
556
|
-
missingRpcs: parseJson(row.
|
|
554
|
+
errorMessage: row.errorMessage ?? undefined,
|
|
555
|
+
suspendReason: row.suspendReason,
|
|
556
|
+
missingRpcs: parseJson(row.missingRpcs),
|
|
557
557
|
pendingApprovals,
|
|
558
558
|
usage: {
|
|
559
|
-
inputTokens: Number(row.
|
|
560
|
-
outputTokens: Number(row.
|
|
561
|
-
model: row.
|
|
559
|
+
inputTokens: Number(row.usageInputTokens),
|
|
560
|
+
outputTokens: Number(row.usageOutputTokens),
|
|
561
|
+
model: row.usageModel,
|
|
562
562
|
},
|
|
563
|
-
createdAt: new Date(row.
|
|
564
|
-
updatedAt: new Date(row.
|
|
563
|
+
createdAt: new Date(row.createdAt),
|
|
564
|
+
updatedAt: new Date(row.updatedAt),
|
|
565
565
|
};
|
|
566
566
|
}
|
|
567
567
|
async findRunByToolCallId(toolCallId) {
|
|
568
568
|
const tc = await this.db
|
|
569
|
-
.selectFrom('
|
|
569
|
+
.selectFrom('aiToolCall')
|
|
570
570
|
.select([
|
|
571
571
|
'id',
|
|
572
|
-
'
|
|
572
|
+
'toolName',
|
|
573
573
|
'args',
|
|
574
|
-
'
|
|
575
|
-
'
|
|
576
|
-
'
|
|
577
|
-
'
|
|
578
|
-
'
|
|
574
|
+
'runId',
|
|
575
|
+
'approvalType',
|
|
576
|
+
'agentRunId',
|
|
577
|
+
'displayToolName',
|
|
578
|
+
'displayArgs',
|
|
579
579
|
])
|
|
580
580
|
.where('id', '=', toolCallId)
|
|
581
|
-
.where('
|
|
581
|
+
.where('approvalStatus', '=', 'pending')
|
|
582
582
|
.executeTakeFirst();
|
|
583
|
-
if (!tc || !tc.
|
|
583
|
+
if (!tc || !tc.runId)
|
|
584
584
|
return null;
|
|
585
|
-
const run = await this.getRun(tc.
|
|
585
|
+
const run = await this.getRun(tc.runId);
|
|
586
586
|
if (!run)
|
|
587
587
|
return null;
|
|
588
588
|
let approval;
|
|
589
|
-
if (tc.
|
|
589
|
+
if (tc.approvalType === 'agent-call') {
|
|
590
590
|
approval = {
|
|
591
591
|
type: 'agent-call',
|
|
592
592
|
toolCallId: tc.id,
|
|
593
|
-
agentName: tc.
|
|
594
|
-
agentRunId: tc.
|
|
595
|
-
displayToolName: tc.
|
|
596
|
-
displayArgs: parseJson(tc.
|
|
593
|
+
agentName: tc.toolName,
|
|
594
|
+
agentRunId: tc.agentRunId,
|
|
595
|
+
displayToolName: tc.displayToolName,
|
|
596
|
+
displayArgs: parseJson(tc.displayArgs),
|
|
597
597
|
};
|
|
598
598
|
}
|
|
599
599
|
else {
|
|
600
600
|
approval = {
|
|
601
601
|
type: 'tool-call',
|
|
602
602
|
toolCallId: tc.id,
|
|
603
|
-
toolName: tc.
|
|
603
|
+
toolName: tc.toolName,
|
|
604
604
|
args: parseJson(tc.args),
|
|
605
605
|
};
|
|
606
606
|
}
|
|
@@ -608,8 +608,8 @@ export class KyselyAIStorageService {
|
|
|
608
608
|
}
|
|
609
609
|
async resolveApproval(toolCallId, status) {
|
|
610
610
|
await this.db
|
|
611
|
-
.updateTable('
|
|
612
|
-
.set({
|
|
611
|
+
.updateTable('aiToolCall')
|
|
612
|
+
.set({ approvalStatus: status })
|
|
613
613
|
.where('id', '=', toolCallId)
|
|
614
614
|
.execute();
|
|
615
615
|
}
|