@pikku/kysely 0.13.13 → 0.13.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/dist/src/index.d.ts +8 -11
  3. package/dist/src/index.js +5 -6
  4. package/dist/src/kysely-agent-run-scores.d.ts +10 -0
  5. package/dist/src/kysely-agent-run-scores.js +34 -0
  6. package/dist/src/kysely-agent-run-service.d.ts +22 -0
  7. package/dist/src/kysely-agent-run-service.js +190 -0
  8. package/dist/src/kysely-agent-run-state-service.d.ts +24 -0
  9. package/dist/src/kysely-agent-run-state-service.js +169 -0
  10. package/dist/src/kysely-agent-storage-service.d.ts +40 -0
  11. package/dist/src/kysely-agent-storage-service.js +533 -0
  12. package/dist/src/kysely-ai-agent-run-service.d.ts +2 -1
  13. package/dist/src/kysely-ai-run-scores.d.ts +10 -0
  14. package/dist/src/kysely-ai-run-scores.js +34 -0
  15. package/dist/src/kysely-ai-run-state-service.d.ts +6 -2
  16. package/dist/src/kysely-ai-run-state-service.js +7 -0
  17. package/dist/src/kysely-ai-storage-service.d.ts +4 -1
  18. package/dist/src/kysely-ai-storage-service.js +7 -0
  19. package/dist/src/kysely-audit-service.d.ts +3 -1
  20. package/dist/src/kysely-deployment-service.d.ts +1 -1
  21. package/dist/src/kysely-deployment-service.js +2 -2
  22. package/dist/src/kysely-eventhub-store.d.ts +1 -1
  23. package/dist/src/kysely-eventhub-store.js +1 -1
  24. package/dist/src/kysely-scope-service.d.ts +4 -3
  25. package/dist/src/kysely-tables.d.ts +23 -11
  26. package/dist/src/kysely-virtual-user-run-store.d.ts +1 -1
  27. package/dist/src/kysely-webhook-service.d.ts +2 -1
  28. package/dist/src/kysely-webhook-service.js +2 -1
  29. package/dist/src/kysely-workflow-mirror.d.ts +2 -2
  30. package/dist/src/kysely-workflow-run-service.d.ts +1 -1
  31. package/dist/src/kysely-workflow-service.d.ts +3 -2
  32. package/dist/src/kysely-workflow-service.js +1 -1
  33. package/dist/src/schema/agent.schema.d.ts +12 -0
  34. package/dist/src/schema/agent.schema.js +102 -0
  35. package/dist/src/schema/ai.schema.js +13 -0
  36. package/dist/src/schema/index.js +4 -4
  37. package/dist/src/serialize-plugin.d.ts +0 -4
  38. package/dist/src/serialize-plugin.js +0 -4
  39. package/dist/tsconfig.tsbuildinfo +1 -1
  40. package/package.json +2 -2
  41. package/src/index.ts +9 -22
  42. package/src/kysely-agent-run-scores.ts +47 -0
  43. package/src/{kysely-ai-agent-run-service.ts → kysely-agent-run-service.ts} +17 -18
  44. package/src/{kysely-ai-run-state-service.ts → kysely-agent-run-state-service.ts} +27 -12
  45. package/src/{kysely-ai-storage-service.ts → kysely-agent-storage-service.ts} +61 -40
  46. package/src/kysely-audit-service.ts +4 -2
  47. package/src/kysely-deployment-service.ts +3 -3
  48. package/src/kysely-eventhub-store.ts +1 -1
  49. package/src/kysely-scope-service.ts +7 -4
  50. package/src/kysely-services.test.ts +11 -10
  51. package/src/kysely-tables.ts +24 -11
  52. package/src/kysely-virtual-user-run-store.ts +2 -2
  53. package/src/kysely-webhook-service.ts +2 -2
  54. package/src/kysely-workflow-mirror.ts +2 -2
  55. package/src/kysely-workflow-run-service.ts +1 -1
  56. package/src/kysely-workflow-service.ts +3 -12
  57. package/src/schema/{ai.schema.ts → agent.schema.ts} +46 -25
  58. package/src/schema/index.ts +4 -4
  59. package/src/schema/schema.test.ts +6 -6
  60. package/src/serialize-plugin.ts +0 -5
@@ -0,0 +1,40 @@
1
+ import type { AgentStorageService, AgentRunStateService, CreateRunInput, SaveScoreInput } from '@pikku/core/services';
2
+ import type { AgentThread, AgentMessage, AgentRunState } from '@pikku/core/agent';
3
+ import type { AgentRunScore } from '@pikku/core/agent-scorer';
4
+ import type { Kysely } from 'kysely';
5
+ import type { KyselyPikkuDB } from './kysely-tables.js';
6
+ export declare class KyselyAgentStorageService implements AgentStorageService, AgentRunStateService {
7
+ private db;
8
+ private initialized;
9
+ constructor(db: Kysely<KyselyPikkuDB>);
10
+ init(): Promise<void>;
11
+ createThread(resourceId: string, options?: {
12
+ threadId?: string;
13
+ title?: string;
14
+ metadata?: Record<string, unknown>;
15
+ }): Promise<AgentThread>;
16
+ getThread(threadId: string): Promise<AgentThread>;
17
+ getThreads(resourceId: string): Promise<AgentThread[]>;
18
+ deleteThread(threadId: string): Promise<void>;
19
+ getMessages(threadId: string, options?: {
20
+ lastN?: number;
21
+ cursor?: string;
22
+ }): Promise<AgentMessage[]>;
23
+ saveMessages(threadId: string, messages: AgentMessage[]): Promise<void>;
24
+ getWorkingMemory(id: string, scope: 'resource' | 'thread'): Promise<Record<string, unknown> | null>;
25
+ saveWorkingMemory(id: string, scope: 'resource' | 'thread', data: Record<string, unknown>): Promise<void>;
26
+ createRun(run: CreateRunInput): Promise<string>;
27
+ updateRun(runId: string, updates: Partial<AgentRunState>): Promise<void>;
28
+ getRun(runId: string): Promise<AgentRunState | null>;
29
+ getRunsByThread(threadId: string): Promise<AgentRunState[]>;
30
+ private setApprovals;
31
+ private mapRunRow;
32
+ findRunByToolCallId(toolCallId: string): Promise<{
33
+ run: AgentRunState;
34
+ approval: NonNullable<AgentRunState['pendingApprovals']>[number];
35
+ } | null>;
36
+ resolveApproval(toolCallId: string, status: 'approved' | 'denied'): Promise<boolean>;
37
+ saveScore(score: SaveScoreInput): Promise<void>;
38
+ getScores(runId: string): Promise<AgentRunScore[]>;
39
+ close(): Promise<void>;
40
+ }
@@ -0,0 +1,533 @@
1
+ import { parseJson } from './kysely-json.js';
2
+ import { getRunScores, saveRunScore } from './kysely-agent-run-scores.js';
3
+ import { ensurePikkuSchema } from './schema/index.js';
4
+ import { agentSchema } from './schema/agent.schema.js';
5
+ export class KyselyAgentStorageService {
6
+ db;
7
+ initialized = false;
8
+ constructor(db) {
9
+ this.db = db;
10
+ }
11
+ async init() {
12
+ if (this.initialized)
13
+ return;
14
+ await ensurePikkuSchema(this.db, agentSchema);
15
+ this.initialized = true;
16
+ }
17
+ async createThread(resourceId, options) {
18
+ const id = options?.threadId ?? crypto.randomUUID();
19
+ const now = new Date();
20
+ await this.db
21
+ .insertInto('agentThreads')
22
+ .values({
23
+ id,
24
+ resourceId: resourceId,
25
+ title: options?.title ?? null,
26
+ metadata: JSON.stringify(options?.metadata ?? null),
27
+ createdAt: now,
28
+ updatedAt: now,
29
+ })
30
+ .execute();
31
+ return {
32
+ id,
33
+ resourceId,
34
+ title: options?.title,
35
+ metadata: options?.metadata,
36
+ createdAt: now,
37
+ updatedAt: now,
38
+ };
39
+ }
40
+ async getThread(threadId) {
41
+ const row = await this.db
42
+ .selectFrom('agentThreads')
43
+ .select([
44
+ 'id',
45
+ 'resourceId',
46
+ 'title',
47
+ 'metadata',
48
+ 'createdAt',
49
+ 'updatedAt',
50
+ ])
51
+ .where('id', '=', threadId)
52
+ .executeTakeFirst();
53
+ if (!row) {
54
+ throw new Error(`Thread not found: ${threadId}`);
55
+ }
56
+ return {
57
+ id: row.id,
58
+ resourceId: row.resourceId,
59
+ title: row.title ?? undefined,
60
+ metadata: parseJson(row.metadata),
61
+ createdAt: new Date(row.createdAt),
62
+ updatedAt: new Date(row.updatedAt),
63
+ };
64
+ }
65
+ async getThreads(resourceId) {
66
+ const result = await this.db
67
+ .selectFrom('agentThreads')
68
+ .select([
69
+ 'id',
70
+ 'resourceId',
71
+ 'title',
72
+ 'metadata',
73
+ 'createdAt',
74
+ 'updatedAt',
75
+ ])
76
+ .where('resourceId', '=', resourceId)
77
+ .orderBy('updatedAt', 'desc')
78
+ .execute();
79
+ return result.map((row) => ({
80
+ id: row.id,
81
+ resourceId: row.resourceId,
82
+ title: row.title ?? undefined,
83
+ metadata: parseJson(row.metadata),
84
+ createdAt: new Date(row.createdAt),
85
+ updatedAt: new Date(row.updatedAt),
86
+ }));
87
+ }
88
+ async deleteThread(threadId) {
89
+ await this.db
90
+ .deleteFrom('agentThreads')
91
+ .where('id', '=', threadId)
92
+ .execute();
93
+ }
94
+ async getMessages(threadId, options) {
95
+ let msgQuery = this.db
96
+ .selectFrom('agentMessage')
97
+ .select(['id', 'role', 'content', 'createdAt'])
98
+ .where('threadId', '=', threadId);
99
+ if (options?.cursor) {
100
+ const cursorRow = await this.db
101
+ .selectFrom('agentMessage')
102
+ .select('createdAt')
103
+ .where('id', '=', options.cursor)
104
+ .executeTakeFirst();
105
+ if (cursorRow) {
106
+ msgQuery = msgQuery.where('createdAt', '<', cursorRow.createdAt);
107
+ }
108
+ }
109
+ let msgResult;
110
+ if (options?.cursor || options?.lastN) {
111
+ const innerResult = await msgQuery
112
+ .orderBy('createdAt', 'desc')
113
+ .limit(options?.lastN ?? 50)
114
+ .execute();
115
+ innerResult.reverse();
116
+ msgResult = innerResult;
117
+ }
118
+ else {
119
+ msgResult = await msgQuery.orderBy('createdAt', 'asc').execute();
120
+ }
121
+ const tcResult = await this.db
122
+ .selectFrom('agentToolCall')
123
+ .select(['id', 'messageId', 'toolName', 'args', 'result'])
124
+ .where('threadId', '=', threadId)
125
+ .orderBy('createdAt', 'asc')
126
+ .execute();
127
+ const tcByMessage = new Map();
128
+ for (const tc of tcResult) {
129
+ const msgId = tc.messageId;
130
+ if (!tcByMessage.has(msgId))
131
+ tcByMessage.set(msgId, []);
132
+ tcByMessage.get(msgId).push(tc);
133
+ }
134
+ const messages = [];
135
+ for (const row of msgResult) {
136
+ const rawContent = row.content;
137
+ let parsedContent = rawContent ?? undefined;
138
+ if (rawContent) {
139
+ try {
140
+ const parsed = JSON.parse(rawContent);
141
+ if (Array.isArray(parsed)) {
142
+ parsedContent = parsed;
143
+ }
144
+ else if (typeof parsed === 'string') {
145
+ parsedContent = parsed;
146
+ }
147
+ }
148
+ catch {
149
+ // Not JSON, use raw string
150
+ }
151
+ }
152
+ const msg = {
153
+ id: row.id,
154
+ role: row.role,
155
+ content: parsedContent,
156
+ createdAt: new Date(row.createdAt),
157
+ };
158
+ const tcs = tcByMessage.get(msg.id);
159
+ if (tcs?.length) {
160
+ msg.toolCalls = tcs.map((tc) => ({
161
+ id: tc.id,
162
+ name: tc.toolName,
163
+ args: parseJson(tc.args),
164
+ }));
165
+ const completed = tcs.filter((tc) => tc.result != null);
166
+ if (completed.length) {
167
+ messages.push(msg);
168
+ messages.push({
169
+ id: `tool-results-${msg.id}`,
170
+ role: 'tool',
171
+ toolResults: completed.map((tc) => ({
172
+ id: tc.id,
173
+ name: tc.toolName,
174
+ result: tc.result,
175
+ })),
176
+ createdAt: msg.createdAt,
177
+ });
178
+ continue;
179
+ }
180
+ }
181
+ messages.push(msg);
182
+ }
183
+ return messages;
184
+ }
185
+ async saveMessages(threadId, messages) {
186
+ if (messages.length === 0)
187
+ return;
188
+ const toolMessages = messages.filter((m) => m.role === 'tool');
189
+ const nonToolMessages = messages.filter((m) => m.role !== 'tool');
190
+ if (nonToolMessages.length > 0) {
191
+ await this.db
192
+ .insertInto('agentMessage')
193
+ .values(nonToolMessages.map((msg) => ({
194
+ id: msg.id,
195
+ threadId: threadId,
196
+ role: msg.role,
197
+ content: msg.content != null ? JSON.stringify(msg.content) : null,
198
+ createdAt: msg.createdAt ?? new Date(),
199
+ })))
200
+ .execute();
201
+ }
202
+ const toolCalls = nonToolMessages.flatMap((msg) => msg.toolCalls?.map((tc) => ({ ...tc, messageId: msg.id })) ?? []);
203
+ if (toolCalls.length > 0) {
204
+ await this.db
205
+ .insertInto('agentToolCall')
206
+ .values(toolCalls.map((tc) => ({
207
+ id: tc.id,
208
+ threadId: threadId,
209
+ messageId: tc.messageId,
210
+ toolName: tc.name,
211
+ args: JSON.stringify(tc.args),
212
+ })))
213
+ .execute();
214
+ }
215
+ for (const toolMsg of toolMessages) {
216
+ if (!toolMsg.toolResults)
217
+ continue;
218
+ for (const tr of toolMsg.toolResults) {
219
+ await this.db
220
+ .updateTable('agentToolCall')
221
+ .set({ result: tr.result })
222
+ .where('id', '=', tr.id)
223
+ .execute();
224
+ }
225
+ }
226
+ await this.db
227
+ .updateTable('agentThreads')
228
+ .set({ updatedAt: new Date() })
229
+ .where('id', '=', threadId)
230
+ .execute();
231
+ }
232
+ async getWorkingMemory(id, scope) {
233
+ const row = await this.db
234
+ .selectFrom('agentWorkingMemory')
235
+ .select('data')
236
+ .where('id', '=', id)
237
+ .where('scope', '=', scope)
238
+ .executeTakeFirst();
239
+ if (!row)
240
+ return null;
241
+ return parseJson(row.data);
242
+ }
243
+ async saveWorkingMemory(id, scope, data) {
244
+ await this.db
245
+ .insertInto('agentWorkingMemory')
246
+ .values({
247
+ id,
248
+ scope,
249
+ data: JSON.stringify(data),
250
+ updatedAt: new Date(),
251
+ })
252
+ .onConflict((oc) => oc.columns(['id', 'scope']).doUpdateSet({
253
+ data: JSON.stringify(data),
254
+ updatedAt: new Date(),
255
+ }))
256
+ .execute();
257
+ }
258
+ async createRun(run) {
259
+ const runId = crypto.randomUUID();
260
+ await this.db
261
+ .insertInto('agentRun')
262
+ .values({
263
+ runId: runId,
264
+ agentName: run.agentName,
265
+ threadId: run.threadId,
266
+ resourceId: run.resourceId,
267
+ status: run.status,
268
+ errorMessage: run.errorMessage ?? null,
269
+ suspendReason: run.suspendReason ?? null,
270
+ missingRpcs: run.missingRpcs ? JSON.stringify(run.missingRpcs) : null,
271
+ usageInputTokens: run.usage.inputTokens,
272
+ usageOutputTokens: run.usage.outputTokens,
273
+ usageModel: run.usage.model,
274
+ createdAt: run.createdAt,
275
+ updatedAt: run.updatedAt,
276
+ })
277
+ .execute();
278
+ if (run.pendingApprovals?.length) {
279
+ await this.setApprovals(runId, run.pendingApprovals);
280
+ }
281
+ return runId;
282
+ }
283
+ async updateRun(runId, updates) {
284
+ const setValues = { updatedAt: new Date() };
285
+ if (updates.status !== undefined) {
286
+ setValues.status = updates.status;
287
+ }
288
+ if (updates.errorMessage !== undefined) {
289
+ setValues.errorMessage = updates.errorMessage;
290
+ }
291
+ if (updates.suspendReason !== undefined) {
292
+ setValues.suspendReason = updates.suspendReason;
293
+ }
294
+ if (updates.missingRpcs !== undefined) {
295
+ setValues.missingRpcs = JSON.stringify(updates.missingRpcs);
296
+ }
297
+ if (updates.usage !== undefined) {
298
+ setValues.usageInputTokens = updates.usage.inputTokens;
299
+ setValues.usageOutputTokens = updates.usage.outputTokens;
300
+ setValues.usageModel = updates.usage.model;
301
+ }
302
+ await this.db
303
+ .updateTable('agentRun')
304
+ .set(setValues)
305
+ .where('runId', '=', runId)
306
+ .execute();
307
+ if (updates.pendingApprovals !== undefined) {
308
+ await this.db
309
+ .updateTable('agentToolCall')
310
+ .set({
311
+ approvalStatus: null,
312
+ runId: null,
313
+ approvalType: null,
314
+ agentRunId: null,
315
+ displayToolName: null,
316
+ displayArgs: null,
317
+ })
318
+ .where('runId', '=', runId)
319
+ .where('approvalStatus', 'is not', null)
320
+ .execute();
321
+ if (updates.pendingApprovals.length) {
322
+ await this.setApprovals(runId, updates.pendingApprovals);
323
+ }
324
+ }
325
+ }
326
+ async getRun(runId) {
327
+ const row = await this.db
328
+ .selectFrom('agentRun')
329
+ .select([
330
+ 'runId',
331
+ 'agentName',
332
+ 'threadId',
333
+ 'resourceId',
334
+ 'status',
335
+ 'errorMessage',
336
+ 'suspendReason',
337
+ 'missingRpcs',
338
+ 'usageInputTokens',
339
+ 'usageOutputTokens',
340
+ 'usageModel',
341
+ 'createdAt',
342
+ 'updatedAt',
343
+ ])
344
+ .where('runId', '=', runId)
345
+ .executeTakeFirst();
346
+ if (!row)
347
+ return null;
348
+ const approvals = await this.db
349
+ .selectFrom('agentToolCall')
350
+ .select([
351
+ 'id',
352
+ 'toolName',
353
+ 'args',
354
+ 'approvalType',
355
+ 'agentRunId',
356
+ 'displayToolName',
357
+ 'displayArgs',
358
+ ])
359
+ .where('runId', '=', runId)
360
+ .where('approvalStatus', '=', 'pending')
361
+ .execute();
362
+ return this.mapRunRow(row, approvals);
363
+ }
364
+ async getRunsByThread(threadId) {
365
+ const result = await this.db
366
+ .selectFrom('agentRun')
367
+ .select([
368
+ 'runId',
369
+ 'agentName',
370
+ 'threadId',
371
+ 'resourceId',
372
+ 'status',
373
+ 'errorMessage',
374
+ 'suspendReason',
375
+ 'missingRpcs',
376
+ 'usageInputTokens',
377
+ 'usageOutputTokens',
378
+ 'usageModel',
379
+ 'createdAt',
380
+ 'updatedAt',
381
+ ])
382
+ .where('threadId', '=', threadId)
383
+ .orderBy('createdAt', 'desc')
384
+ .execute();
385
+ const runs = [];
386
+ for (const row of result) {
387
+ const approvals = await this.db
388
+ .selectFrom('agentToolCall')
389
+ .select([
390
+ 'id',
391
+ 'toolName',
392
+ 'args',
393
+ 'approvalType',
394
+ 'agentRunId',
395
+ 'displayToolName',
396
+ 'displayArgs',
397
+ ])
398
+ .where('runId', '=', row.runId)
399
+ .where('approvalStatus', '=', 'pending')
400
+ .execute();
401
+ runs.push(this.mapRunRow(row, approvals));
402
+ }
403
+ return runs;
404
+ }
405
+ async setApprovals(runId, approvals) {
406
+ for (const a of approvals) {
407
+ if (a.type === 'agent-call') {
408
+ await this.db
409
+ .updateTable('agentToolCall')
410
+ .set({
411
+ approvalStatus: 'pending',
412
+ runId: runId,
413
+ approvalType: 'agent-call',
414
+ agentRunId: a.agentRunId,
415
+ displayToolName: a.displayToolName,
416
+ displayArgs: JSON.stringify(a.displayArgs),
417
+ })
418
+ .where('id', '=', a.toolCallId)
419
+ .execute();
420
+ }
421
+ else {
422
+ await this.db
423
+ .updateTable('agentToolCall')
424
+ .set({
425
+ approvalStatus: 'pending',
426
+ runId: runId,
427
+ approvalType: 'tool-call',
428
+ })
429
+ .where('id', '=', a.toolCallId)
430
+ .execute();
431
+ }
432
+ }
433
+ }
434
+ mapRunRow(row, approvalRows) {
435
+ const pendingApprovals = approvalRows?.length
436
+ ? approvalRows.map((a) => {
437
+ if (a.approvalType === 'agent-call') {
438
+ return {
439
+ type: 'agent-call',
440
+ toolCallId: a.id,
441
+ agentName: a.toolName,
442
+ agentRunId: a.agentRunId,
443
+ displayToolName: a.displayToolName,
444
+ displayArgs: parseJson(a.displayArgs),
445
+ };
446
+ }
447
+ return {
448
+ type: 'tool-call',
449
+ toolCallId: a.id,
450
+ toolName: a.toolName,
451
+ args: parseJson(a.args),
452
+ };
453
+ })
454
+ : undefined;
455
+ return {
456
+ runId: row.runId,
457
+ agentName: row.agentName,
458
+ threadId: row.threadId,
459
+ resourceId: row.resourceId,
460
+ status: row.status,
461
+ errorMessage: row.errorMessage ?? undefined,
462
+ suspendReason: row.suspendReason,
463
+ missingRpcs: parseJson(row.missingRpcs),
464
+ pendingApprovals,
465
+ usage: {
466
+ inputTokens: Number(row.usageInputTokens),
467
+ outputTokens: Number(row.usageOutputTokens),
468
+ model: row.usageModel,
469
+ },
470
+ createdAt: new Date(row.createdAt),
471
+ updatedAt: new Date(row.updatedAt),
472
+ };
473
+ }
474
+ async findRunByToolCallId(toolCallId) {
475
+ const tc = await this.db
476
+ .selectFrom('agentToolCall')
477
+ .select([
478
+ 'id',
479
+ 'toolName',
480
+ 'args',
481
+ 'runId',
482
+ 'approvalType',
483
+ 'agentRunId',
484
+ 'displayToolName',
485
+ 'displayArgs',
486
+ ])
487
+ .where('id', '=', toolCallId)
488
+ .where('approvalStatus', '=', 'pending')
489
+ .executeTakeFirst();
490
+ if (!tc || !tc.runId)
491
+ return null;
492
+ const run = await this.getRun(tc.runId);
493
+ if (!run)
494
+ return null;
495
+ let approval;
496
+ if (tc.approvalType === 'agent-call') {
497
+ approval = {
498
+ type: 'agent-call',
499
+ toolCallId: tc.id,
500
+ agentName: tc.toolName,
501
+ agentRunId: tc.agentRunId,
502
+ displayToolName: tc.displayToolName,
503
+ displayArgs: parseJson(tc.displayArgs),
504
+ };
505
+ }
506
+ else {
507
+ approval = {
508
+ type: 'tool-call',
509
+ toolCallId: tc.id,
510
+ toolName: tc.toolName,
511
+ args: parseJson(tc.args),
512
+ };
513
+ }
514
+ return { run, approval };
515
+ }
516
+ async resolveApproval(toolCallId, status) {
517
+ // Only the caller that moves the tool call off 'pending' has claimed it.
518
+ const result = await this.db
519
+ .updateTable('agentToolCall')
520
+ .set({ approvalStatus: status })
521
+ .where('id', '=', toolCallId)
522
+ .where('approvalStatus', '=', 'pending')
523
+ .executeTakeFirst();
524
+ return Number(result?.numUpdatedRows ?? 0) > 0;
525
+ }
526
+ async saveScore(score) {
527
+ await saveRunScore(this.db, score);
528
+ }
529
+ async getScores(runId) {
530
+ return getRunScores(this.db, runId);
531
+ }
532
+ async close() { }
533
+ }
@@ -1,4 +1,5 @@
1
- import type { AIThread, AIMessage, AgentRunRow, AgentRunService } from '@pikku/core/ai-agent';
1
+ import type { AIThread, AIMessage } from '@pikku/core/ai-agent';
2
+ import type { AgentRunRow, AgentRunService } from '@pikku/core/ecosystem/ai-agent';
2
3
  import type { Kysely } from 'kysely';
3
4
  import type { KyselyPikkuDB } from './kysely-tables.js';
4
5
  export declare class KyselyAgentRunService implements AgentRunService {
@@ -0,0 +1,10 @@
1
+ import type { Kysely } from 'kysely';
2
+ import type { KyselyPikkuDB } from './kysely-tables.js';
3
+ import type { SaveScoreInput } from '@pikku/core/services';
4
+ import type { AIRunScore } from '@pikku/core/ai-scorer';
5
+ /**
6
+ * Shared by the two Kysely services that implement `AIRunStateService` — they
7
+ * differ in how they store approvals, not in how they store grades.
8
+ */
9
+ export declare const saveRunScore: (db: Kysely<KyselyPikkuDB>, score: SaveScoreInput) => Promise<void>;
10
+ export declare const getRunScores: (db: Kysely<KyselyPikkuDB>, runId: string) => Promise<AIRunScore[]>;
@@ -0,0 +1,34 @@
1
+ import { parseJson } from './kysely-json.js';
2
+ /**
3
+ * Shared by the two Kysely services that implement `AIRunStateService` — they
4
+ * differ in how they store approvals, not in how they store grades.
5
+ */
6
+ export const saveRunScore = async (db, score) => {
7
+ await db
8
+ .insertInto('aiRunScore')
9
+ .values({
10
+ id: crypto.randomUUID(),
11
+ runId: score.runId,
12
+ scorerName: score.scorerName,
13
+ score: score.score,
14
+ reason: score.reason ?? null,
15
+ metadata: score.metadata ? JSON.stringify(score.metadata) : null,
16
+ })
17
+ .execute();
18
+ };
19
+ export const getRunScores = async (db, runId) => {
20
+ const rows = await db
21
+ .selectFrom('aiRunScore')
22
+ .selectAll()
23
+ .where('runId', '=', runId)
24
+ .orderBy('createdAt', 'asc')
25
+ .execute();
26
+ return rows.map((row) => ({
27
+ runId: row.runId,
28
+ scorerName: row.scorerName,
29
+ score: Number(row.score),
30
+ reason: row.reason ?? undefined,
31
+ metadata: parseJson(row.metadata),
32
+ createdAt: new Date(row.createdAt),
33
+ }));
34
+ };
@@ -1,7 +1,9 @@
1
1
  import type { Kysely } from 'kysely';
2
2
  import type { KyselyPikkuDB } from './kysely-tables.js';
3
- import type { AIRunStateService, CreateRunInput } from '@pikku/core/services';
4
- import type { AgentRunState, PendingApproval } from '@pikku/core/ai-agent';
3
+ import type { AIRunStateService, CreateRunInput, SaveScoreInput } from '@pikku/core/services';
4
+ import type { AgentRunState } from '@pikku/core/ai-agent';
5
+ import type { PendingApproval } from '@pikku/core/ecosystem/ai-agent';
6
+ import type { AIRunScore } from '@pikku/core/ai-scorer';
5
7
  export declare class KyselyAIRunStateService implements AIRunStateService {
6
8
  private db;
7
9
  private initialized;
@@ -16,5 +18,7 @@ export declare class KyselyAIRunStateService implements AIRunStateService {
16
18
  run: AgentRunState;
17
19
  approval: PendingApproval;
18
20
  } | null>;
21
+ saveScore(score: SaveScoreInput): Promise<void>;
22
+ getScores(runId: string): Promise<AIRunScore[]>;
19
23
  private toRunState;
20
24
  }
@@ -1,6 +1,7 @@
1
1
  import { sql } from 'kysely';
2
2
  import { ensurePikkuSchema } from './schema/index.js';
3
3
  import { aiSchema } from './schema/ai.schema.js';
4
+ import { getRunScores, saveRunScore } from './kysely-ai-run-scores.js';
4
5
  export class KyselyAIRunStateService {
5
6
  db;
6
7
  initialized = false;
@@ -137,6 +138,12 @@ export class KyselyAIRunStateService {
137
138
  }
138
139
  return null;
139
140
  }
141
+ async saveScore(score) {
142
+ await saveRunScore(this.db, score);
143
+ }
144
+ async getScores(runId) {
145
+ return getRunScores(this.db, runId);
146
+ }
140
147
  toRunState(row) {
141
148
  return {
142
149
  runId: row.runId,
@@ -1,5 +1,6 @@
1
- import type { AIStorageService, AIRunStateService, CreateRunInput } from '@pikku/core/services';
1
+ import type { AIStorageService, AIRunStateService, CreateRunInput, SaveScoreInput } from '@pikku/core/services';
2
2
  import type { AIThread, AIMessage, AgentRunState } from '@pikku/core/ai-agent';
3
+ import type { AIRunScore } from '@pikku/core/ai-scorer';
3
4
  import type { Kysely } from 'kysely';
4
5
  import type { KyselyPikkuDB } from './kysely-tables.js';
5
6
  export declare class KyselyAIStorageService implements AIStorageService, AIRunStateService {
@@ -33,5 +34,7 @@ export declare class KyselyAIStorageService implements AIStorageService, AIRunSt
33
34
  approval: NonNullable<AgentRunState['pendingApprovals']>[number];
34
35
  } | null>;
35
36
  resolveApproval(toolCallId: string, status: 'approved' | 'denied'): Promise<boolean>;
37
+ saveScore(score: SaveScoreInput): Promise<void>;
38
+ getScores(runId: string): Promise<AIRunScore[]>;
36
39
  close(): Promise<void>;
37
40
  }