@pikku/kysely 0.13.15 → 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.
- package/CHANGELOG.md +94 -0
- package/dist/src/index.d.ts +7 -7
- package/dist/src/index.js +4 -4
- package/dist/src/kysely-agent-run-scores.d.ts +10 -0
- package/dist/src/kysely-agent-run-scores.js +34 -0
- package/dist/src/kysely-agent-run-service.d.ts +22 -0
- package/dist/src/kysely-agent-run-service.js +190 -0
- package/dist/src/kysely-agent-run-state-service.d.ts +24 -0
- package/dist/src/kysely-agent-run-state-service.js +169 -0
- package/dist/src/kysely-agent-storage-service.d.ts +40 -0
- package/dist/src/kysely-agent-storage-service.js +533 -0
- package/dist/src/kysely-ai-agent-run-service.d.ts +2 -1
- package/dist/src/kysely-ai-run-state-service.d.ts +2 -1
- package/dist/src/kysely-audit-service.d.ts +3 -1
- package/dist/src/kysely-deployment-service.d.ts +1 -1
- package/dist/src/kysely-deployment-service.js +2 -2
- package/dist/src/kysely-eventhub-store.d.ts +1 -1
- package/dist/src/kysely-eventhub-store.js +1 -1
- package/dist/src/kysely-scope-service.d.ts +4 -3
- package/dist/src/kysely-tables.d.ts +13 -13
- package/dist/src/kysely-virtual-user-run-store.d.ts +1 -1
- package/dist/src/kysely-webhook-service.d.ts +2 -1
- package/dist/src/kysely-webhook-service.js +2 -1
- package/dist/src/kysely-workflow-mirror.d.ts +2 -2
- package/dist/src/kysely-workflow-run-service.d.ts +1 -1
- package/dist/src/kysely-workflow-service.d.ts +3 -2
- package/dist/src/kysely-workflow-service.js +1 -1
- package/dist/src/schema/agent.schema.d.ts +12 -0
- package/dist/src/schema/agent.schema.js +102 -0
- package/dist/src/schema/index.js +4 -4
- package/dist/src/serialize-plugin.d.ts +0 -4
- package/dist/src/serialize-plugin.js +0 -4
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/index.ts +9 -7
- package/src/{kysely-ai-run-scores.ts → kysely-agent-run-scores.ts} +5 -5
- package/src/{kysely-ai-agent-run-service.ts → kysely-agent-run-service.ts} +17 -18
- package/src/{kysely-ai-run-state-service.ts → kysely-agent-run-state-service.ts} +16 -15
- package/src/{kysely-ai-storage-service.ts → kysely-agent-storage-service.ts} +53 -43
- package/src/kysely-audit-service.ts +4 -2
- package/src/kysely-deployment-service.ts +3 -3
- package/src/kysely-eventhub-store.ts +1 -1
- package/src/kysely-scope-service.ts +7 -4
- package/src/kysely-services.test.ts +11 -11
- package/src/kysely-tables.ts +13 -13
- package/src/kysely-virtual-user-run-store.ts +2 -2
- package/src/kysely-webhook-service.ts +2 -2
- package/src/kysely-workflow-mirror.ts +2 -2
- package/src/kysely-workflow-run-service.ts +1 -1
- package/src/kysely-workflow-service.ts +3 -12
- package/src/schema/{ai.schema.ts → agent.schema.ts} +29 -29
- package/src/schema/index.ts +4 -4
- package/src/schema/schema.test.ts +6 -6
- package/src/serialize-plugin.ts +0 -5
|
@@ -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
|
|
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 {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { Kysely } from 'kysely';
|
|
2
2
|
import type { KyselyPikkuDB } from './kysely-tables.js';
|
|
3
3
|
import type { AIRunStateService, CreateRunInput, SaveScoreInput } from '@pikku/core/services';
|
|
4
|
-
import type { AgentRunState
|
|
4
|
+
import type { AgentRunState } from '@pikku/core/ai-agent';
|
|
5
|
+
import type { PendingApproval } from '@pikku/core/ecosystem/ai-agent';
|
|
5
6
|
import type { AIRunScore } from '@pikku/core/ai-scorer';
|
|
6
7
|
export declare class KyselyAIRunStateService implements AIRunStateService {
|
|
7
8
|
private db;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AuditService } from '@pikku/core';
|
|
2
|
+
import type { AuditEvent, AuditEventBatch } from '@pikku/core/ecosystem/services';
|
|
3
|
+
import type { AuditFacets, AuditQuery, AuditQueryResult } from '@pikku/core/ecosystem/types';
|
|
2
4
|
import type { Kysely } from 'kysely';
|
|
3
5
|
/**
|
|
4
6
|
* Durable {@link AuditService} that persists AuditEvents to an `audit` table
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DeploymentService, DeploymentServiceConfig, DeploymentConfig } from '@pikku/core/services';
|
|
1
|
+
import type { DeploymentService, DeploymentServiceConfig, DeploymentConfig } from '@pikku/core/ecosystem/services';
|
|
2
2
|
import type { JWTService, SecretService } from '@pikku/core/services';
|
|
3
3
|
import type { Kysely } from 'kysely';
|
|
4
4
|
import type { KyselyPikkuDB } from './kysely-tables.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { buildRemoteHeaders } from '@pikku/core/remote';
|
|
2
|
-
import { getAllFunctionNames } from '@pikku/core/function';
|
|
1
|
+
import { buildRemoteHeaders } from '@pikku/core/ecosystem/remote';
|
|
2
|
+
import { getAllFunctionNames } from '@pikku/core/ecosystem/function';
|
|
3
3
|
import { ensurePikkuSchema } from './schema/index.js';
|
|
4
4
|
import { deploymentSchema } from './schema/deployment.schema.js';
|
|
5
5
|
export class KyselyDeploymentService {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EventHubStore } from '@pikku/core/channel';
|
|
1
|
+
import { EventHubStore } from '@pikku/core/ecosystem/channel';
|
|
2
2
|
import type { Kysely } from 'kysely';
|
|
3
3
|
import type { KyselyPikkuDB } from './kysely-tables.js';
|
|
4
4
|
export declare class KyselyEventHubStore extends EventHubStore {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
1
|
+
import type { ScopeService } from '@pikku/core/services';
|
|
2
|
+
import type { Role } from '@pikku/core/ecosystem/services';
|
|
3
|
+
import type { FlatScope } from '@pikku/core/ecosystem/scope';
|
|
4
|
+
import type { SystemRole } from '@pikku/core/ecosystem/role';
|
|
4
5
|
import type { Kysely } from 'kysely';
|
|
5
6
|
import type { KyselyPikkuDB } from './kysely-tables.js';
|
|
6
7
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Generated } from 'kysely';
|
|
2
|
-
import type { WorkflowStatus, StepStatus, WorkflowVersionStatus } from '@pikku/core/workflow';
|
|
2
|
+
import type { WorkflowStatus, StepStatus, WorkflowVersionStatus } from '@pikku/core/ecosystem/workflow';
|
|
3
3
|
export interface ChannelsTable {
|
|
4
4
|
channelId: string;
|
|
5
5
|
channelName: string;
|
|
@@ -75,7 +75,7 @@ export interface WorkflowVersionsTable {
|
|
|
75
75
|
status: Generated<WorkflowVersionStatus>;
|
|
76
76
|
createdAt: Generated<Date>;
|
|
77
77
|
}
|
|
78
|
-
export interface
|
|
78
|
+
export interface AgentThreadsTable {
|
|
79
79
|
id: string;
|
|
80
80
|
resourceId: string;
|
|
81
81
|
title: string | null;
|
|
@@ -83,14 +83,14 @@ export interface AIThreadsTable {
|
|
|
83
83
|
createdAt: Generated<Date>;
|
|
84
84
|
updatedAt: Generated<Date>;
|
|
85
85
|
}
|
|
86
|
-
export interface
|
|
86
|
+
export interface AgentMessageTable {
|
|
87
87
|
id: string;
|
|
88
88
|
threadId: string;
|
|
89
89
|
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
90
90
|
content: string | null;
|
|
91
91
|
createdAt: Generated<Date>;
|
|
92
92
|
}
|
|
93
|
-
export interface
|
|
93
|
+
export interface AgentToolCallTable {
|
|
94
94
|
id: string;
|
|
95
95
|
threadId: string;
|
|
96
96
|
messageId: string;
|
|
@@ -105,13 +105,13 @@ export interface AIToolCallTable {
|
|
|
105
105
|
displayArgs: string | null;
|
|
106
106
|
createdAt: Generated<Date>;
|
|
107
107
|
}
|
|
108
|
-
export interface
|
|
108
|
+
export interface AgentWorkingMemoryTable {
|
|
109
109
|
id: string;
|
|
110
110
|
scope: string;
|
|
111
111
|
data: string;
|
|
112
112
|
updatedAt: Generated<Date>;
|
|
113
113
|
}
|
|
114
|
-
export interface
|
|
114
|
+
export interface AgentRunTable {
|
|
115
115
|
runId: Generated<string>;
|
|
116
116
|
agentName: string;
|
|
117
117
|
threadId: string;
|
|
@@ -133,7 +133,7 @@ export interface AIRunTable {
|
|
|
133
133
|
createdAt: Generated<Date>;
|
|
134
134
|
updatedAt: Generated<Date>;
|
|
135
135
|
}
|
|
136
|
-
export interface
|
|
136
|
+
export interface AgentRunScoreTable {
|
|
137
137
|
id: string;
|
|
138
138
|
runId: string;
|
|
139
139
|
scorerName: string;
|
|
@@ -301,12 +301,12 @@ export interface KyselyPikkuDB {
|
|
|
301
301
|
workflowStep: WorkflowStepTable;
|
|
302
302
|
workflowStepHistory: WorkflowStepHistoryTable;
|
|
303
303
|
workflowVersions: WorkflowVersionsTable;
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
304
|
+
agentThreads: AgentThreadsTable;
|
|
305
|
+
agentMessage: AgentMessageTable;
|
|
306
|
+
agentToolCall: AgentToolCallTable;
|
|
307
|
+
agentWorkingMemory: AgentWorkingMemoryTable;
|
|
308
|
+
agentRun: AgentRunTable;
|
|
309
|
+
agentRunScore: AgentRunScoreTable;
|
|
310
310
|
pikkuDeployments: PikkuDeploymentsTable;
|
|
311
311
|
pikkuDeploymentFunctions: PikkuDeploymentFunctionsTable;
|
|
312
312
|
secrets: SecretsTable;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Kysely } from 'kysely';
|
|
2
|
-
import type { VirtualUserRunOutcome, VirtualUserRunRecord, VirtualUserRunStart, VirtualUserRunStore } from '@pikku/core/virtual-user';
|
|
2
|
+
import type { VirtualUserRunOutcome, VirtualUserRunRecord, VirtualUserRunStart, VirtualUserRunStore } from '@pikku/core/ecosystem/virtual-user';
|
|
3
3
|
import type { KyselyPikkuDB } from './kysely-tables.js';
|
|
4
4
|
/**
|
|
5
5
|
* Records virtual-user runs in a `virtualUserRun` table.
|