@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.
@@ -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('ai_threads')
117
+ .insertInto('aiThreads')
118
118
  .values({
119
119
  id,
120
- resource_id: resourceId,
120
+ resourceId: resourceId,
121
121
  title: options?.title ?? null,
122
122
  metadata: JSON.stringify(options?.metadata ?? null),
123
- created_at: now,
124
- updated_at: now,
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('ai_threads')
138
+ .selectFrom('aiThreads')
139
139
  .select([
140
140
  'id',
141
- 'resource_id',
141
+ 'resourceId',
142
142
  'title',
143
143
  'metadata',
144
- 'created_at',
145
- 'updated_at',
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.resource_id,
154
+ resourceId: row.resourceId,
155
155
  title: row.title ?? undefined,
156
156
  metadata: parseJson(row.metadata),
157
- createdAt: new Date(row.created_at),
158
- updatedAt: new Date(row.updated_at),
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('ai_threads')
163
+ .selectFrom('aiThreads')
164
164
  .select([
165
165
  'id',
166
- 'resource_id',
166
+ 'resourceId',
167
167
  'title',
168
168
  'metadata',
169
- 'created_at',
170
- 'updated_at',
169
+ 'createdAt',
170
+ 'updatedAt',
171
171
  ])
172
- .where('resource_id', '=', resourceId)
173
- .orderBy('updated_at', 'desc')
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.resource_id,
177
+ resourceId: row.resourceId,
178
178
  title: row.title ?? undefined,
179
179
  metadata: parseJson(row.metadata),
180
- createdAt: new Date(row.created_at),
181
- updatedAt: new Date(row.updated_at),
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('ai_threads').where('id', '=', threadId).execute();
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('ai_message')
190
- .select(['id', 'role', 'content', 'created_at'])
191
- .where('thread_id', '=', threadId);
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('ai_message')
195
- .select('created_at')
194
+ .selectFrom('aiMessage')
195
+ .select('createdAt')
196
196
  .where('id', '=', options.cursor)
197
197
  .executeTakeFirst();
198
198
  if (cursorRow) {
199
- msgQuery = msgQuery.where('created_at', '<', cursorRow.created_at);
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('created_at', 'desc')
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('created_at', 'asc').execute();
212
+ msgResult = await msgQuery.orderBy('createdAt', 'asc').execute();
213
213
  }
214
214
  const tcResult = await this.db
215
- .selectFrom('ai_tool_call')
216
- .select(['id', 'message_id', 'tool_name', 'args', 'result'])
217
- .where('thread_id', '=', threadId)
218
- .orderBy('created_at', 'asc')
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.message_id;
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.created_at),
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.tool_name,
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.tool_name,
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('ai_message')
285
+ .insertInto('aiMessage')
286
286
  .values(nonToolMessages.map((msg) => ({
287
287
  id: msg.id,
288
- thread_id: threadId,
288
+ threadId: threadId,
289
289
  role: msg.role,
290
290
  content: msg.content != null ? JSON.stringify(msg.content) : null,
291
- created_at: msg.createdAt ?? new Date(),
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('ai_tool_call')
298
+ .insertInto('aiToolCall')
299
299
  .values(toolCalls.map((tc) => ({
300
300
  id: tc.id,
301
- thread_id: threadId,
302
- message_id: tc.messageId,
303
- tool_name: tc.name,
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('ai_tool_call')
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('ai_threads')
321
- .set({ updated_at: new Date() })
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('ai_working_memory')
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('ai_working_memory')
338
+ .insertInto('aiWorkingMemory')
339
339
  .values({
340
340
  id,
341
341
  scope,
342
342
  data: JSON.stringify(data),
343
- updated_at: new Date(),
343
+ updatedAt: new Date(),
344
344
  })
345
345
  .onConflict((oc) => oc.columns(['id', 'scope']).doUpdateSet({
346
346
  data: JSON.stringify(data),
347
- updated_at: new Date(),
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('ai_run')
354
+ .insertInto('aiRun')
355
355
  .values({
356
- run_id: runId,
357
- agent_name: run.agentName,
358
- thread_id: run.threadId,
359
- resource_id: run.resourceId,
356
+ runId: runId,
357
+ agentName: run.agentName,
358
+ threadId: run.threadId,
359
+ resourceId: run.resourceId,
360
360
  status: run.status,
361
- error_message: run.errorMessage ?? null,
362
- suspend_reason: run.suspendReason ?? null,
363
- missing_rpcs: run.missingRpcs ? JSON.stringify(run.missingRpcs) : null,
364
- usage_input_tokens: run.usage.inputTokens,
365
- usage_output_tokens: run.usage.outputTokens,
366
- usage_model: run.usage.model,
367
- created_at: run.createdAt,
368
- updated_at: run.updatedAt,
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 = { updated_at: new Date() };
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.error_message = updates.errorMessage;
382
+ setValues.errorMessage = updates.errorMessage;
383
383
  }
384
384
  if (updates.suspendReason !== undefined) {
385
- setValues.suspend_reason = updates.suspendReason;
385
+ setValues.suspendReason = updates.suspendReason;
386
386
  }
387
387
  if (updates.missingRpcs !== undefined) {
388
- setValues.missing_rpcs = JSON.stringify(updates.missingRpcs);
388
+ setValues.missingRpcs = JSON.stringify(updates.missingRpcs);
389
389
  }
390
390
  if (updates.usage !== undefined) {
391
- setValues.usage_input_tokens = updates.usage.inputTokens;
392
- setValues.usage_output_tokens = updates.usage.outputTokens;
393
- setValues.usage_model = updates.usage.model;
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('ai_run')
396
+ .updateTable('aiRun')
397
397
  .set(setValues)
398
- .where('run_id', '=', runId)
398
+ .where('runId', '=', runId)
399
399
  .execute();
400
400
  if (updates.pendingApprovals !== undefined) {
401
401
  await this.db
402
- .updateTable('ai_tool_call')
402
+ .updateTable('aiToolCall')
403
403
  .set({
404
- approval_status: null,
405
- run_id: null,
406
- approval_type: null,
407
- agent_run_id: null,
408
- display_tool_name: null,
409
- display_args: null,
404
+ approvalStatus: null,
405
+ runId: null,
406
+ approvalType: null,
407
+ agentRunId: null,
408
+ displayToolName: null,
409
+ displayArgs: null,
410
410
  })
411
- .where('run_id', '=', runId)
412
- .where('approval_status', 'is not', null)
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('ai_run')
421
+ .selectFrom('aiRun')
422
422
  .select([
423
- 'run_id',
424
- 'agent_name',
425
- 'thread_id',
426
- 'resource_id',
423
+ 'runId',
424
+ 'agentName',
425
+ 'threadId',
426
+ 'resourceId',
427
427
  'status',
428
- 'error_message',
429
- 'suspend_reason',
430
- 'missing_rpcs',
431
- 'usage_input_tokens',
432
- 'usage_output_tokens',
433
- 'usage_model',
434
- 'created_at',
435
- 'updated_at',
428
+ 'errorMessage',
429
+ 'suspendReason',
430
+ 'missingRpcs',
431
+ 'usageInputTokens',
432
+ 'usageOutputTokens',
433
+ 'usageModel',
434
+ 'createdAt',
435
+ 'updatedAt',
436
436
  ])
437
- .where('run_id', '=', runId)
437
+ .where('runId', '=', runId)
438
438
  .executeTakeFirst();
439
439
  if (!row)
440
440
  return null;
441
441
  const approvals = await this.db
442
- .selectFrom('ai_tool_call')
442
+ .selectFrom('aiToolCall')
443
443
  .select([
444
444
  'id',
445
- 'tool_name',
445
+ 'toolName',
446
446
  'args',
447
- 'approval_type',
448
- 'agent_run_id',
449
- 'display_tool_name',
450
- 'display_args',
447
+ 'approvalType',
448
+ 'agentRunId',
449
+ 'displayToolName',
450
+ 'displayArgs',
451
451
  ])
452
- .where('run_id', '=', runId)
453
- .where('approval_status', '=', 'pending')
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('ai_run')
459
+ .selectFrom('aiRun')
460
460
  .select([
461
- 'run_id',
462
- 'agent_name',
463
- 'thread_id',
464
- 'resource_id',
461
+ 'runId',
462
+ 'agentName',
463
+ 'threadId',
464
+ 'resourceId',
465
465
  'status',
466
- 'error_message',
467
- 'suspend_reason',
468
- 'missing_rpcs',
469
- 'usage_input_tokens',
470
- 'usage_output_tokens',
471
- 'usage_model',
472
- 'created_at',
473
- 'updated_at',
466
+ 'errorMessage',
467
+ 'suspendReason',
468
+ 'missingRpcs',
469
+ 'usageInputTokens',
470
+ 'usageOutputTokens',
471
+ 'usageModel',
472
+ 'createdAt',
473
+ 'updatedAt',
474
474
  ])
475
- .where('thread_id', '=', threadId)
476
- .orderBy('created_at', 'desc')
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('ai_tool_call')
481
+ .selectFrom('aiToolCall')
482
482
  .select([
483
483
  'id',
484
- 'tool_name',
484
+ 'toolName',
485
485
  'args',
486
- 'approval_type',
487
- 'agent_run_id',
488
- 'display_tool_name',
489
- 'display_args',
486
+ 'approvalType',
487
+ 'agentRunId',
488
+ 'displayToolName',
489
+ 'displayArgs',
490
490
  ])
491
- .where('run_id', '=', row.run_id)
492
- .where('approval_status', '=', 'pending')
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('ai_tool_call')
502
+ .updateTable('aiToolCall')
503
503
  .set({
504
- approval_status: 'pending',
505
- run_id: runId,
506
- approval_type: 'agent-call',
507
- agent_run_id: a.agentRunId,
508
- display_tool_name: a.displayToolName,
509
- display_args: JSON.stringify(a.displayArgs),
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('ai_tool_call')
516
+ .updateTable('aiToolCall')
517
517
  .set({
518
- approval_status: 'pending',
519
- run_id: runId,
520
- approval_type: 'tool-call',
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.approval_type === 'agent-call') {
530
+ if (a.approvalType === 'agent-call') {
531
531
  return {
532
532
  type: 'agent-call',
533
533
  toolCallId: a.id,
534
- agentName: a.tool_name,
535
- agentRunId: a.agent_run_id,
536
- displayToolName: a.display_tool_name,
537
- displayArgs: parseJson(a.display_args),
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.tool_name,
543
+ toolName: a.toolName,
544
544
  args: parseJson(a.args),
545
545
  };
546
546
  })
547
547
  : undefined;
548
548
  return {
549
- runId: row.run_id,
550
- agentName: row.agent_name,
551
- threadId: row.thread_id,
552
- resourceId: row.resource_id,
549
+ runId: row.runId,
550
+ agentName: row.agentName,
551
+ threadId: row.threadId,
552
+ resourceId: row.resourceId,
553
553
  status: row.status,
554
- errorMessage: row.error_message ?? undefined,
555
- suspendReason: row.suspend_reason,
556
- missingRpcs: parseJson(row.missing_rpcs),
554
+ errorMessage: row.errorMessage ?? undefined,
555
+ suspendReason: row.suspendReason,
556
+ missingRpcs: parseJson(row.missingRpcs),
557
557
  pendingApprovals,
558
558
  usage: {
559
- inputTokens: Number(row.usage_input_tokens),
560
- outputTokens: Number(row.usage_output_tokens),
561
- model: row.usage_model,
559
+ inputTokens: Number(row.usageInputTokens),
560
+ outputTokens: Number(row.usageOutputTokens),
561
+ model: row.usageModel,
562
562
  },
563
- createdAt: new Date(row.created_at),
564
- updatedAt: new Date(row.updated_at),
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('ai_tool_call')
569
+ .selectFrom('aiToolCall')
570
570
  .select([
571
571
  'id',
572
- 'tool_name',
572
+ 'toolName',
573
573
  'args',
574
- 'run_id',
575
- 'approval_type',
576
- 'agent_run_id',
577
- 'display_tool_name',
578
- 'display_args',
574
+ 'runId',
575
+ 'approvalType',
576
+ 'agentRunId',
577
+ 'displayToolName',
578
+ 'displayArgs',
579
579
  ])
580
580
  .where('id', '=', toolCallId)
581
- .where('approval_status', '=', 'pending')
581
+ .where('approvalStatus', '=', 'pending')
582
582
  .executeTakeFirst();
583
- if (!tc || !tc.run_id)
583
+ if (!tc || !tc.runId)
584
584
  return null;
585
- const run = await this.getRun(tc.run_id);
585
+ const run = await this.getRun(tc.runId);
586
586
  if (!run)
587
587
  return null;
588
588
  let approval;
589
- if (tc.approval_type === 'agent-call') {
589
+ if (tc.approvalType === 'agent-call') {
590
590
  approval = {
591
591
  type: 'agent-call',
592
592
  toolCallId: tc.id,
593
- agentName: tc.tool_name,
594
- agentRunId: tc.agent_run_id,
595
- displayToolName: tc.display_tool_name,
596
- displayArgs: parseJson(tc.display_args),
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.tool_name,
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('ai_tool_call')
612
- .set({ approval_status: status })
611
+ .updateTable('aiToolCall')
612
+ .set({ approvalStatus: status })
613
613
  .where('id', '=', toolCallId)
614
614
  .execute();
615
615
  }