@pikku/kysely 0.10.0 → 0.12.0
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 +11 -0
- package/dist/src/index.d.ts +9 -1
- package/dist/src/index.js +6 -1
- package/dist/src/kysely-agent-run-service.d.ts +19 -0
- package/dist/src/kysely-agent-run-service.js +171 -0
- package/dist/src/kysely-ai-storage-service.d.ts +37 -0
- package/dist/src/kysely-ai-storage-service.js +586 -0
- package/dist/src/kysely-channel-store.d.ts +7 -3
- package/dist/src/kysely-channel-store.js +61 -24
- package/dist/src/kysely-deployment-service.d.ts +17 -0
- package/dist/src/kysely-deployment-service.js +128 -0
- package/dist/src/kysely-eventhub-store.d.ts +7 -3
- package/dist/src/kysely-eventhub-store.js +34 -18
- package/dist/src/kysely-json.d.ts +1 -0
- package/dist/src/kysely-json.js +7 -0
- package/dist/src/kysely-tables.d.ts +136 -0
- package/dist/src/kysely-tables.js +1 -0
- package/dist/src/kysely-workflow-run-service.d.ts +29 -0
- package/dist/src/kysely-workflow-run-service.js +194 -0
- package/dist/src/kysely-workflow-service.d.ts +47 -0
- package/dist/src/kysely-workflow-service.js +485 -0
- package/dist/src/pikku-kysely.d.ts +3 -2
- package/dist/src/pikku-kysely.js +25 -5
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +12 -6
- package/src/index.ts +11 -1
- package/src/kysely-agent-run-service.ts +205 -0
- package/src/kysely-ai-storage-service.ts +713 -0
- package/src/kysely-channel-store.ts +82 -26
- package/src/kysely-deployment-service.ts +171 -0
- package/src/kysely-eventhub-store.ts +38 -17
- package/src/kysely-json.ts +5 -0
- package/src/kysely-services.test.ts +800 -0
- package/src/kysely-tables.ts +150 -0
- package/src/kysely-workflow-run-service.ts +242 -0
- package/src/kysely-workflow-service.ts +642 -0
- package/src/pikku-kysely.ts +28 -6
- package/sql/serverless-tables.sql +0 -16
|
@@ -0,0 +1,586 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
import { parseJson } from './kysely-json.js';
|
|
3
|
+
export class KyselyAIStorageService {
|
|
4
|
+
db;
|
|
5
|
+
initialized = false;
|
|
6
|
+
constructor(db) {
|
|
7
|
+
this.db = db;
|
|
8
|
+
}
|
|
9
|
+
async init() {
|
|
10
|
+
if (this.initialized) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
await this.db.schema
|
|
14
|
+
.createTable('ai_threads')
|
|
15
|
+
.ifNotExists()
|
|
16
|
+
.addColumn('id', 'text', (col) => col.primaryKey())
|
|
17
|
+
.addColumn('resource_id', 'text', (col) => col.notNull())
|
|
18
|
+
.addColumn('title', 'text')
|
|
19
|
+
.addColumn('metadata', 'text')
|
|
20
|
+
.addColumn('created_at', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
|
|
21
|
+
.addColumn('updated_at', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
|
|
22
|
+
.execute();
|
|
23
|
+
await this.db.schema
|
|
24
|
+
.createIndex('idx_ai_threads_resource')
|
|
25
|
+
.ifNotExists()
|
|
26
|
+
.on('ai_threads')
|
|
27
|
+
.column('resource_id')
|
|
28
|
+
.execute();
|
|
29
|
+
await this.db.schema
|
|
30
|
+
.createTable('ai_message')
|
|
31
|
+
.ifNotExists()
|
|
32
|
+
.addColumn('id', 'text', (col) => col.primaryKey())
|
|
33
|
+
.addColumn('thread_id', 'text', (col) => col.notNull().references('ai_threads.id').onDelete('cascade'))
|
|
34
|
+
.addColumn('role', 'text', (col) => col.notNull())
|
|
35
|
+
.addColumn('content', 'text')
|
|
36
|
+
.addColumn('created_at', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
|
|
37
|
+
.execute();
|
|
38
|
+
await this.db.schema
|
|
39
|
+
.createIndex('idx_ai_message_thread')
|
|
40
|
+
.ifNotExists()
|
|
41
|
+
.on('ai_message')
|
|
42
|
+
.columns(['thread_id', 'created_at'])
|
|
43
|
+
.execute();
|
|
44
|
+
await this.db.schema
|
|
45
|
+
.createTable('ai_tool_call')
|
|
46
|
+
.ifNotExists()
|
|
47
|
+
.addColumn('id', 'text', (col) => col.primaryKey())
|
|
48
|
+
.addColumn('thread_id', 'text', (col) => col.notNull().references('ai_threads.id').onDelete('cascade'))
|
|
49
|
+
.addColumn('message_id', 'text', (col) => col.notNull().references('ai_message.id').onDelete('cascade'))
|
|
50
|
+
.addColumn('run_id', 'text')
|
|
51
|
+
.addColumn('tool_name', 'text', (col) => col.notNull())
|
|
52
|
+
.addColumn('args', 'text', (col) => col.notNull().defaultTo('{}'))
|
|
53
|
+
.addColumn('result', 'text')
|
|
54
|
+
.addColumn('approval_status', 'text')
|
|
55
|
+
.addColumn('approval_type', 'text')
|
|
56
|
+
.addColumn('agent_run_id', 'text')
|
|
57
|
+
.addColumn('display_tool_name', 'text')
|
|
58
|
+
.addColumn('display_args', 'text')
|
|
59
|
+
.addColumn('created_at', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
|
|
60
|
+
.execute();
|
|
61
|
+
await this.db.schema
|
|
62
|
+
.createIndex('idx_ai_tool_call_thread')
|
|
63
|
+
.ifNotExists()
|
|
64
|
+
.on('ai_tool_call')
|
|
65
|
+
.column('thread_id')
|
|
66
|
+
.execute();
|
|
67
|
+
await this.db.schema
|
|
68
|
+
.createIndex('idx_ai_tool_call_message')
|
|
69
|
+
.ifNotExists()
|
|
70
|
+
.on('ai_tool_call')
|
|
71
|
+
.column('message_id')
|
|
72
|
+
.execute();
|
|
73
|
+
await this.db.schema
|
|
74
|
+
.createTable('ai_working_memory')
|
|
75
|
+
.ifNotExists()
|
|
76
|
+
.addColumn('id', 'text', (col) => col.notNull())
|
|
77
|
+
.addColumn('scope', 'text', (col) => col.notNull())
|
|
78
|
+
.addColumn('data', 'text', (col) => col.notNull())
|
|
79
|
+
.addColumn('updated_at', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
|
|
80
|
+
.addPrimaryKeyConstraint('ai_working_memory_pk', ['id', 'scope'])
|
|
81
|
+
.execute();
|
|
82
|
+
await this.db.schema
|
|
83
|
+
.createTable('ai_run')
|
|
84
|
+
.ifNotExists()
|
|
85
|
+
.addColumn('run_id', 'text', (col) => col.primaryKey())
|
|
86
|
+
.addColumn('agent_name', 'text', (col) => col.notNull())
|
|
87
|
+
.addColumn('thread_id', 'text', (col) => col.notNull().references('ai_threads.id').onDelete('cascade'))
|
|
88
|
+
.addColumn('resource_id', 'text', (col) => col.notNull())
|
|
89
|
+
.addColumn('status', 'text', (col) => col.notNull().defaultTo('running'))
|
|
90
|
+
.addColumn('suspend_reason', 'text')
|
|
91
|
+
.addColumn('missing_rpcs', 'text')
|
|
92
|
+
.addColumn('usage_input_tokens', 'integer', (col) => col.notNull().defaultTo(0))
|
|
93
|
+
.addColumn('usage_output_tokens', 'integer', (col) => col.notNull().defaultTo(0))
|
|
94
|
+
.addColumn('usage_model', 'text', (col) => col.notNull().defaultTo(''))
|
|
95
|
+
.addColumn('created_at', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
|
|
96
|
+
.addColumn('updated_at', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
|
|
97
|
+
.execute();
|
|
98
|
+
await this.db.schema
|
|
99
|
+
.createIndex('idx_ai_run_thread')
|
|
100
|
+
.ifNotExists()
|
|
101
|
+
.on('ai_run')
|
|
102
|
+
.columns(['thread_id', 'created_at'])
|
|
103
|
+
.execute();
|
|
104
|
+
this.initialized = true;
|
|
105
|
+
}
|
|
106
|
+
async createThread(resourceId, options) {
|
|
107
|
+
const id = options?.threadId ?? crypto.randomUUID();
|
|
108
|
+
const now = new Date();
|
|
109
|
+
await this.db
|
|
110
|
+
.insertInto('ai_threads')
|
|
111
|
+
.values({
|
|
112
|
+
id,
|
|
113
|
+
resource_id: resourceId,
|
|
114
|
+
title: options?.title ?? null,
|
|
115
|
+
metadata: JSON.stringify(options?.metadata ?? null),
|
|
116
|
+
created_at: now,
|
|
117
|
+
updated_at: now,
|
|
118
|
+
})
|
|
119
|
+
.execute();
|
|
120
|
+
return {
|
|
121
|
+
id,
|
|
122
|
+
resourceId,
|
|
123
|
+
title: options?.title,
|
|
124
|
+
metadata: options?.metadata,
|
|
125
|
+
createdAt: now,
|
|
126
|
+
updatedAt: now,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
async getThread(threadId) {
|
|
130
|
+
const row = await this.db
|
|
131
|
+
.selectFrom('ai_threads')
|
|
132
|
+
.select([
|
|
133
|
+
'id',
|
|
134
|
+
'resource_id',
|
|
135
|
+
'title',
|
|
136
|
+
'metadata',
|
|
137
|
+
'created_at',
|
|
138
|
+
'updated_at',
|
|
139
|
+
])
|
|
140
|
+
.where('id', '=', threadId)
|
|
141
|
+
.executeTakeFirst();
|
|
142
|
+
if (!row) {
|
|
143
|
+
throw new Error(`Thread not found: ${threadId}`);
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
id: row.id,
|
|
147
|
+
resourceId: row.resource_id,
|
|
148
|
+
title: row.title ?? undefined,
|
|
149
|
+
metadata: parseJson(row.metadata),
|
|
150
|
+
createdAt: new Date(row.created_at),
|
|
151
|
+
updatedAt: new Date(row.updated_at),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
async getThreads(resourceId) {
|
|
155
|
+
const result = await this.db
|
|
156
|
+
.selectFrom('ai_threads')
|
|
157
|
+
.select([
|
|
158
|
+
'id',
|
|
159
|
+
'resource_id',
|
|
160
|
+
'title',
|
|
161
|
+
'metadata',
|
|
162
|
+
'created_at',
|
|
163
|
+
'updated_at',
|
|
164
|
+
])
|
|
165
|
+
.where('resource_id', '=', resourceId)
|
|
166
|
+
.orderBy('updated_at', 'desc')
|
|
167
|
+
.execute();
|
|
168
|
+
return result.map((row) => ({
|
|
169
|
+
id: row.id,
|
|
170
|
+
resourceId: row.resource_id,
|
|
171
|
+
title: row.title ?? undefined,
|
|
172
|
+
metadata: parseJson(row.metadata),
|
|
173
|
+
createdAt: new Date(row.created_at),
|
|
174
|
+
updatedAt: new Date(row.updated_at),
|
|
175
|
+
}));
|
|
176
|
+
}
|
|
177
|
+
async deleteThread(threadId) {
|
|
178
|
+
await this.db.deleteFrom('ai_threads').where('id', '=', threadId).execute();
|
|
179
|
+
}
|
|
180
|
+
async getMessages(threadId, options) {
|
|
181
|
+
let msgQuery = this.db
|
|
182
|
+
.selectFrom('ai_message')
|
|
183
|
+
.select(['id', 'role', 'content', 'created_at'])
|
|
184
|
+
.where('thread_id', '=', threadId);
|
|
185
|
+
if (options?.cursor) {
|
|
186
|
+
const cursorRow = await this.db
|
|
187
|
+
.selectFrom('ai_message')
|
|
188
|
+
.select('created_at')
|
|
189
|
+
.where('id', '=', options.cursor)
|
|
190
|
+
.executeTakeFirst();
|
|
191
|
+
if (cursorRow) {
|
|
192
|
+
msgQuery = msgQuery.where('created_at', '<', cursorRow.created_at);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (options?.cursor || options?.lastN) {
|
|
196
|
+
const innerResult = await msgQuery
|
|
197
|
+
.orderBy('created_at', 'desc')
|
|
198
|
+
.limit(options?.lastN ?? 50)
|
|
199
|
+
.execute();
|
|
200
|
+
innerResult.reverse();
|
|
201
|
+
var msgResult = innerResult;
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
var msgResult = await msgQuery.orderBy('created_at', 'asc').execute();
|
|
205
|
+
}
|
|
206
|
+
const tcResult = await this.db
|
|
207
|
+
.selectFrom('ai_tool_call')
|
|
208
|
+
.select(['id', 'message_id', 'tool_name', 'args', 'result'])
|
|
209
|
+
.where('thread_id', '=', threadId)
|
|
210
|
+
.orderBy('created_at', 'asc')
|
|
211
|
+
.execute();
|
|
212
|
+
const tcByMessage = new Map();
|
|
213
|
+
for (const tc of tcResult) {
|
|
214
|
+
const msgId = tc.message_id;
|
|
215
|
+
if (!tcByMessage.has(msgId))
|
|
216
|
+
tcByMessage.set(msgId, []);
|
|
217
|
+
tcByMessage.get(msgId).push(tc);
|
|
218
|
+
}
|
|
219
|
+
const messages = [];
|
|
220
|
+
for (const row of msgResult) {
|
|
221
|
+
const msg = {
|
|
222
|
+
id: row.id,
|
|
223
|
+
role: row.role,
|
|
224
|
+
content: row.content ?? undefined,
|
|
225
|
+
createdAt: new Date(row.created_at),
|
|
226
|
+
};
|
|
227
|
+
const tcs = tcByMessage.get(msg.id);
|
|
228
|
+
if (tcs?.length) {
|
|
229
|
+
msg.toolCalls = tcs.map((tc) => ({
|
|
230
|
+
id: tc.id,
|
|
231
|
+
name: tc.tool_name,
|
|
232
|
+
args: parseJson(tc.args),
|
|
233
|
+
}));
|
|
234
|
+
const completed = tcs.filter((tc) => tc.result != null);
|
|
235
|
+
if (completed.length) {
|
|
236
|
+
messages.push(msg);
|
|
237
|
+
messages.push({
|
|
238
|
+
id: `tool-results-${msg.id}`,
|
|
239
|
+
role: 'tool',
|
|
240
|
+
toolResults: completed.map((tc) => ({
|
|
241
|
+
id: tc.id,
|
|
242
|
+
name: tc.tool_name,
|
|
243
|
+
result: tc.result,
|
|
244
|
+
})),
|
|
245
|
+
createdAt: msg.createdAt,
|
|
246
|
+
});
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
messages.push(msg);
|
|
251
|
+
}
|
|
252
|
+
return messages;
|
|
253
|
+
}
|
|
254
|
+
async saveMessages(threadId, messages) {
|
|
255
|
+
if (messages.length === 0)
|
|
256
|
+
return;
|
|
257
|
+
const toolMessages = messages.filter((m) => m.role === 'tool');
|
|
258
|
+
const nonToolMessages = messages.filter((m) => m.role !== 'tool');
|
|
259
|
+
if (nonToolMessages.length > 0) {
|
|
260
|
+
await this.db
|
|
261
|
+
.insertInto('ai_message')
|
|
262
|
+
.values(nonToolMessages.map((msg) => ({
|
|
263
|
+
id: msg.id,
|
|
264
|
+
thread_id: threadId,
|
|
265
|
+
role: msg.role,
|
|
266
|
+
content: msg.content ?? null,
|
|
267
|
+
created_at: msg.createdAt ?? new Date(),
|
|
268
|
+
})))
|
|
269
|
+
.execute();
|
|
270
|
+
}
|
|
271
|
+
const toolCalls = nonToolMessages.flatMap((msg) => msg.toolCalls?.map((tc) => ({ ...tc, messageId: msg.id })) ?? []);
|
|
272
|
+
if (toolCalls.length > 0) {
|
|
273
|
+
await this.db
|
|
274
|
+
.insertInto('ai_tool_call')
|
|
275
|
+
.values(toolCalls.map((tc) => ({
|
|
276
|
+
id: tc.id,
|
|
277
|
+
thread_id: threadId,
|
|
278
|
+
message_id: tc.messageId,
|
|
279
|
+
tool_name: tc.name,
|
|
280
|
+
args: JSON.stringify(tc.args),
|
|
281
|
+
})))
|
|
282
|
+
.execute();
|
|
283
|
+
}
|
|
284
|
+
for (const toolMsg of toolMessages) {
|
|
285
|
+
if (!toolMsg.toolResults)
|
|
286
|
+
continue;
|
|
287
|
+
for (const tr of toolMsg.toolResults) {
|
|
288
|
+
await this.db
|
|
289
|
+
.updateTable('ai_tool_call')
|
|
290
|
+
.set({ result: tr.result })
|
|
291
|
+
.where('id', '=', tr.id)
|
|
292
|
+
.execute();
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
await this.db
|
|
296
|
+
.updateTable('ai_threads')
|
|
297
|
+
.set({ updated_at: new Date() })
|
|
298
|
+
.where('id', '=', threadId)
|
|
299
|
+
.execute();
|
|
300
|
+
}
|
|
301
|
+
async getWorkingMemory(id, scope) {
|
|
302
|
+
const row = await this.db
|
|
303
|
+
.selectFrom('ai_working_memory')
|
|
304
|
+
.select('data')
|
|
305
|
+
.where('id', '=', id)
|
|
306
|
+
.where('scope', '=', scope)
|
|
307
|
+
.executeTakeFirst();
|
|
308
|
+
if (!row)
|
|
309
|
+
return null;
|
|
310
|
+
return parseJson(row.data);
|
|
311
|
+
}
|
|
312
|
+
async saveWorkingMemory(id, scope, data) {
|
|
313
|
+
await this.db
|
|
314
|
+
.insertInto('ai_working_memory')
|
|
315
|
+
.values({
|
|
316
|
+
id,
|
|
317
|
+
scope,
|
|
318
|
+
data: JSON.stringify(data),
|
|
319
|
+
updated_at: new Date(),
|
|
320
|
+
})
|
|
321
|
+
.onConflict((oc) => oc.columns(['id', 'scope']).doUpdateSet({
|
|
322
|
+
data: JSON.stringify(data),
|
|
323
|
+
updated_at: new Date(),
|
|
324
|
+
}))
|
|
325
|
+
.execute();
|
|
326
|
+
}
|
|
327
|
+
async createRun(run) {
|
|
328
|
+
const runId = crypto.randomUUID();
|
|
329
|
+
await this.db
|
|
330
|
+
.insertInto('ai_run')
|
|
331
|
+
.values({
|
|
332
|
+
run_id: runId,
|
|
333
|
+
agent_name: run.agentName,
|
|
334
|
+
thread_id: run.threadId,
|
|
335
|
+
resource_id: run.resourceId,
|
|
336
|
+
status: run.status,
|
|
337
|
+
suspend_reason: run.suspendReason ?? null,
|
|
338
|
+
missing_rpcs: run.missingRpcs ? JSON.stringify(run.missingRpcs) : null,
|
|
339
|
+
usage_input_tokens: run.usage.inputTokens,
|
|
340
|
+
usage_output_tokens: run.usage.outputTokens,
|
|
341
|
+
usage_model: run.usage.model,
|
|
342
|
+
created_at: run.createdAt,
|
|
343
|
+
updated_at: run.updatedAt,
|
|
344
|
+
})
|
|
345
|
+
.execute();
|
|
346
|
+
if (run.pendingApprovals?.length) {
|
|
347
|
+
await this.setApprovals(runId, run.pendingApprovals);
|
|
348
|
+
}
|
|
349
|
+
return runId;
|
|
350
|
+
}
|
|
351
|
+
async updateRun(runId, updates) {
|
|
352
|
+
const setValues = { updated_at: new Date() };
|
|
353
|
+
if (updates.status !== undefined) {
|
|
354
|
+
setValues.status = updates.status;
|
|
355
|
+
}
|
|
356
|
+
if (updates.suspendReason !== undefined) {
|
|
357
|
+
setValues.suspend_reason = updates.suspendReason;
|
|
358
|
+
}
|
|
359
|
+
if (updates.missingRpcs !== undefined) {
|
|
360
|
+
setValues.missing_rpcs = JSON.stringify(updates.missingRpcs);
|
|
361
|
+
}
|
|
362
|
+
if (updates.usage !== undefined) {
|
|
363
|
+
setValues.usage_input_tokens = updates.usage.inputTokens;
|
|
364
|
+
setValues.usage_output_tokens = updates.usage.outputTokens;
|
|
365
|
+
setValues.usage_model = updates.usage.model;
|
|
366
|
+
}
|
|
367
|
+
await this.db
|
|
368
|
+
.updateTable('ai_run')
|
|
369
|
+
.set(setValues)
|
|
370
|
+
.where('run_id', '=', runId)
|
|
371
|
+
.execute();
|
|
372
|
+
if (updates.pendingApprovals !== undefined) {
|
|
373
|
+
await this.db
|
|
374
|
+
.updateTable('ai_tool_call')
|
|
375
|
+
.set({
|
|
376
|
+
approval_status: null,
|
|
377
|
+
run_id: null,
|
|
378
|
+
approval_type: null,
|
|
379
|
+
agent_run_id: null,
|
|
380
|
+
display_tool_name: null,
|
|
381
|
+
display_args: null,
|
|
382
|
+
})
|
|
383
|
+
.where('run_id', '=', runId)
|
|
384
|
+
.where('approval_status', 'is not', null)
|
|
385
|
+
.execute();
|
|
386
|
+
if (updates.pendingApprovals.length) {
|
|
387
|
+
await this.setApprovals(runId, updates.pendingApprovals);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
async getRun(runId) {
|
|
392
|
+
const row = await this.db
|
|
393
|
+
.selectFrom('ai_run')
|
|
394
|
+
.select([
|
|
395
|
+
'run_id',
|
|
396
|
+
'agent_name',
|
|
397
|
+
'thread_id',
|
|
398
|
+
'resource_id',
|
|
399
|
+
'status',
|
|
400
|
+
'suspend_reason',
|
|
401
|
+
'missing_rpcs',
|
|
402
|
+
'usage_input_tokens',
|
|
403
|
+
'usage_output_tokens',
|
|
404
|
+
'usage_model',
|
|
405
|
+
'created_at',
|
|
406
|
+
'updated_at',
|
|
407
|
+
])
|
|
408
|
+
.where('run_id', '=', runId)
|
|
409
|
+
.executeTakeFirst();
|
|
410
|
+
if (!row)
|
|
411
|
+
return null;
|
|
412
|
+
const approvals = await this.db
|
|
413
|
+
.selectFrom('ai_tool_call')
|
|
414
|
+
.select([
|
|
415
|
+
'id',
|
|
416
|
+
'tool_name',
|
|
417
|
+
'args',
|
|
418
|
+
'approval_type',
|
|
419
|
+
'agent_run_id',
|
|
420
|
+
'display_tool_name',
|
|
421
|
+
'display_args',
|
|
422
|
+
])
|
|
423
|
+
.where('run_id', '=', runId)
|
|
424
|
+
.where('approval_status', '=', 'pending')
|
|
425
|
+
.execute();
|
|
426
|
+
return this.mapRunRow(row, approvals);
|
|
427
|
+
}
|
|
428
|
+
async getRunsByThread(threadId) {
|
|
429
|
+
const result = await this.db
|
|
430
|
+
.selectFrom('ai_run')
|
|
431
|
+
.select([
|
|
432
|
+
'run_id',
|
|
433
|
+
'agent_name',
|
|
434
|
+
'thread_id',
|
|
435
|
+
'resource_id',
|
|
436
|
+
'status',
|
|
437
|
+
'suspend_reason',
|
|
438
|
+
'missing_rpcs',
|
|
439
|
+
'usage_input_tokens',
|
|
440
|
+
'usage_output_tokens',
|
|
441
|
+
'usage_model',
|
|
442
|
+
'created_at',
|
|
443
|
+
'updated_at',
|
|
444
|
+
])
|
|
445
|
+
.where('thread_id', '=', threadId)
|
|
446
|
+
.orderBy('created_at', 'desc')
|
|
447
|
+
.execute();
|
|
448
|
+
const runs = [];
|
|
449
|
+
for (const row of result) {
|
|
450
|
+
const approvals = await this.db
|
|
451
|
+
.selectFrom('ai_tool_call')
|
|
452
|
+
.select([
|
|
453
|
+
'id',
|
|
454
|
+
'tool_name',
|
|
455
|
+
'args',
|
|
456
|
+
'approval_type',
|
|
457
|
+
'agent_run_id',
|
|
458
|
+
'display_tool_name',
|
|
459
|
+
'display_args',
|
|
460
|
+
])
|
|
461
|
+
.where('run_id', '=', row.run_id)
|
|
462
|
+
.where('approval_status', '=', 'pending')
|
|
463
|
+
.execute();
|
|
464
|
+
runs.push(this.mapRunRow(row, approvals));
|
|
465
|
+
}
|
|
466
|
+
return runs;
|
|
467
|
+
}
|
|
468
|
+
async setApprovals(runId, approvals) {
|
|
469
|
+
for (const a of approvals) {
|
|
470
|
+
if (a.type === 'agent-call') {
|
|
471
|
+
await this.db
|
|
472
|
+
.updateTable('ai_tool_call')
|
|
473
|
+
.set({
|
|
474
|
+
approval_status: 'pending',
|
|
475
|
+
run_id: runId,
|
|
476
|
+
approval_type: 'agent-call',
|
|
477
|
+
agent_run_id: a.agentRunId,
|
|
478
|
+
display_tool_name: a.displayToolName,
|
|
479
|
+
display_args: JSON.stringify(a.displayArgs),
|
|
480
|
+
})
|
|
481
|
+
.where('id', '=', a.toolCallId)
|
|
482
|
+
.execute();
|
|
483
|
+
}
|
|
484
|
+
else {
|
|
485
|
+
await this.db
|
|
486
|
+
.updateTable('ai_tool_call')
|
|
487
|
+
.set({
|
|
488
|
+
approval_status: 'pending',
|
|
489
|
+
run_id: runId,
|
|
490
|
+
approval_type: 'tool-call',
|
|
491
|
+
})
|
|
492
|
+
.where('id', '=', a.toolCallId)
|
|
493
|
+
.execute();
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
mapRunRow(row, approvalRows) {
|
|
498
|
+
const pendingApprovals = approvalRows?.length
|
|
499
|
+
? approvalRows.map((a) => {
|
|
500
|
+
if (a.approval_type === 'agent-call') {
|
|
501
|
+
return {
|
|
502
|
+
type: 'agent-call',
|
|
503
|
+
toolCallId: a.id,
|
|
504
|
+
agentName: a.tool_name,
|
|
505
|
+
agentRunId: a.agent_run_id,
|
|
506
|
+
displayToolName: a.display_tool_name,
|
|
507
|
+
displayArgs: parseJson(a.display_args),
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
return {
|
|
511
|
+
type: 'tool-call',
|
|
512
|
+
toolCallId: a.id,
|
|
513
|
+
toolName: a.tool_name,
|
|
514
|
+
args: parseJson(a.args),
|
|
515
|
+
};
|
|
516
|
+
})
|
|
517
|
+
: undefined;
|
|
518
|
+
return {
|
|
519
|
+
runId: row.run_id,
|
|
520
|
+
agentName: row.agent_name,
|
|
521
|
+
threadId: row.thread_id,
|
|
522
|
+
resourceId: row.resource_id,
|
|
523
|
+
status: row.status,
|
|
524
|
+
suspendReason: row.suspend_reason,
|
|
525
|
+
missingRpcs: parseJson(row.missing_rpcs),
|
|
526
|
+
pendingApprovals,
|
|
527
|
+
usage: {
|
|
528
|
+
inputTokens: Number(row.usage_input_tokens),
|
|
529
|
+
outputTokens: Number(row.usage_output_tokens),
|
|
530
|
+
model: row.usage_model,
|
|
531
|
+
},
|
|
532
|
+
createdAt: new Date(row.created_at),
|
|
533
|
+
updatedAt: new Date(row.updated_at),
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
async findRunByToolCallId(toolCallId) {
|
|
537
|
+
const tc = await this.db
|
|
538
|
+
.selectFrom('ai_tool_call')
|
|
539
|
+
.select([
|
|
540
|
+
'id',
|
|
541
|
+
'tool_name',
|
|
542
|
+
'args',
|
|
543
|
+
'run_id',
|
|
544
|
+
'approval_type',
|
|
545
|
+
'agent_run_id',
|
|
546
|
+
'display_tool_name',
|
|
547
|
+
'display_args',
|
|
548
|
+
])
|
|
549
|
+
.where('id', '=', toolCallId)
|
|
550
|
+
.where('approval_status', '=', 'pending')
|
|
551
|
+
.executeTakeFirst();
|
|
552
|
+
if (!tc || !tc.run_id)
|
|
553
|
+
return null;
|
|
554
|
+
const run = await this.getRun(tc.run_id);
|
|
555
|
+
if (!run)
|
|
556
|
+
return null;
|
|
557
|
+
let approval;
|
|
558
|
+
if (tc.approval_type === 'agent-call') {
|
|
559
|
+
approval = {
|
|
560
|
+
type: 'agent-call',
|
|
561
|
+
toolCallId: tc.id,
|
|
562
|
+
agentName: tc.tool_name,
|
|
563
|
+
agentRunId: tc.agent_run_id,
|
|
564
|
+
displayToolName: tc.display_tool_name,
|
|
565
|
+
displayArgs: parseJson(tc.display_args),
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
else {
|
|
569
|
+
approval = {
|
|
570
|
+
type: 'tool-call',
|
|
571
|
+
toolCallId: tc.id,
|
|
572
|
+
toolName: tc.tool_name,
|
|
573
|
+
args: parseJson(tc.args),
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
return { run, approval };
|
|
577
|
+
}
|
|
578
|
+
async resolveApproval(toolCallId, status) {
|
|
579
|
+
await this.db
|
|
580
|
+
.updateTable('ai_tool_call')
|
|
581
|
+
.set({ approval_status: status })
|
|
582
|
+
.where('id', '=', toolCallId)
|
|
583
|
+
.execute();
|
|
584
|
+
}
|
|
585
|
+
async close() { }
|
|
586
|
+
}
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { CoreUserSession } from '@pikku/core';
|
|
2
2
|
import { Channel, ChannelStore } from '@pikku/core/channel';
|
|
3
3
|
import { Kysely } from 'kysely';
|
|
4
|
+
import type { KyselyPikkuDB } from './kysely-tables.js';
|
|
4
5
|
export declare class KyselyChannelStore extends ChannelStore {
|
|
5
|
-
private
|
|
6
|
-
|
|
6
|
+
private db;
|
|
7
|
+
private initialized;
|
|
8
|
+
constructor(db: Kysely<KyselyPikkuDB>);
|
|
9
|
+
init(): Promise<void>;
|
|
7
10
|
addChannel({ channelId, channelName, openingData, }: Channel): Promise<void>;
|
|
8
11
|
removeChannels(channelIds: string[]): Promise<void>;
|
|
9
|
-
setUserSession(channelId: string, session:
|
|
12
|
+
setUserSession(channelId: string, session: CoreUserSession | null): Promise<void>;
|
|
10
13
|
getChannelAndSession(channelId: string): Promise<Channel & {
|
|
11
14
|
session: CoreUserSession;
|
|
12
15
|
}>;
|
|
16
|
+
close(): Promise<void>;
|
|
13
17
|
}
|