@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
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { QueueWebhookService
|
|
1
|
+
import { QueueWebhookService } from '@pikku/core/services';
|
|
2
|
+
import { type SendWebhookInput, type SendWebhookResult, type WebhookAttemptResult, type WebhookDeliveryRecord, type WebhookDeliveryWithAttempts } from '@pikku/core/ecosystem/services';
|
|
2
3
|
import type { QueueService } from '@pikku/core/queue';
|
|
3
4
|
import type { Kysely } from 'kysely';
|
|
4
5
|
import type { KyselyPikkuDB } from './kysely-tables.js';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { QueueWebhookService } from '@pikku/core/services';
|
|
2
|
+
import { PIKKU_OUTGOING_WEBHOOK_QUEUE_NAME, } from '@pikku/core/ecosystem/services';
|
|
2
3
|
import { ensurePikkuSchema } from './schema/index.js';
|
|
3
4
|
import { webhookSchema } from './schema/webhook.schema.js';
|
|
4
5
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { SerializedError } from '@pikku/core';
|
|
2
|
-
import type { StepState, WorkflowRunMirror, WorkflowRunWire, WorkflowStatus, WorkflowPlannedStep, WorkflowVersionStatus } from '@pikku/core/workflow';
|
|
1
|
+
import type { SerializedError } from '@pikku/core/ecosystem/types';
|
|
2
|
+
import type { StepState, WorkflowRunMirror, WorkflowRunWire, WorkflowStatus, WorkflowPlannedStep, WorkflowVersionStatus } from '@pikku/core/ecosystem/workflow';
|
|
3
3
|
import type { Kysely } from 'kysely';
|
|
4
4
|
import type { KyselyPikkuDB } from './kysely-tables.js';
|
|
5
5
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { WorkflowRun, StepState, WorkflowRunService } from '@pikku/core/workflow';
|
|
1
|
+
import type { WorkflowRun, StepState, WorkflowRunService } from '@pikku/core/ecosystem/workflow';
|
|
2
2
|
import type { Kysely } from 'kysely';
|
|
3
3
|
import type { KyselyPikkuDB } from './kysely-tables.js';
|
|
4
4
|
export declare class KyselyWorkflowRunService implements WorkflowRunService {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { SerializedError } from '@pikku/core';
|
|
2
|
-
import { PikkuWorkflowService
|
|
1
|
+
import type { SerializedError } from '@pikku/core/ecosystem/types';
|
|
2
|
+
import { PikkuWorkflowService } from '@pikku/core/workflow';
|
|
3
|
+
import type { WorkflowPlannedStep, WorkflowQueueOptions, WorkflowRun, WorkflowRunWire, StepState, StepStatus, WorkflowStatus, WorkflowVersionStatus } from '@pikku/core/ecosystem/workflow';
|
|
3
4
|
import { type Kysely } from 'kysely';
|
|
4
5
|
import type { KyselyPikkuDB } from './kysely-tables.js';
|
|
5
6
|
export declare class KyselyWorkflowService extends PikkuWorkflowService {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PikkuWorkflowService
|
|
1
|
+
import { PikkuWorkflowService } from '@pikku/core/workflow';
|
|
2
2
|
import { sql } from 'kysely';
|
|
3
3
|
import { KyselyWorkflowRunService } from './kysely-workflow-run-service.js';
|
|
4
4
|
import { parseJson } from './kysely-json.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PikkuSchema } from './pikku-schema.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Threads, messages, tool calls, working memory and agent runs.
|
|
4
|
+
*
|
|
5
|
+
* `agentRun` carries `pendingApprovals` even though the two services that used to
|
|
6
|
+
* create this table disagreed about it: `KyselyAgentStorageService` omitted the
|
|
7
|
+
* column while `KyselyAgentRunStateService` declared it and read it back through
|
|
8
|
+
* casts. Whichever service happened to run first decided the shape, and on a
|
|
9
|
+
* database where the storage service won, resolving an approval failed. One
|
|
10
|
+
* declaration ends that — the column is here because live code reads it.
|
|
11
|
+
*/
|
|
12
|
+
export declare const agentSchema: PikkuSchema;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { sql } from 'kysely';
|
|
2
|
+
/**
|
|
3
|
+
* Threads, messages, tool calls, working memory and agent runs.
|
|
4
|
+
*
|
|
5
|
+
* `agentRun` carries `pendingApprovals` even though the two services that used to
|
|
6
|
+
* create this table disagreed about it: `KyselyAgentStorageService` omitted the
|
|
7
|
+
* column while `KyselyAgentRunStateService` declared it and read it back through
|
|
8
|
+
* casts. Whichever service happened to run first decided the shape, and on a
|
|
9
|
+
* database where the storage service won, resolving an approval failed. One
|
|
10
|
+
* declaration ends that — the column is here because live code reads it.
|
|
11
|
+
*/
|
|
12
|
+
export const agentSchema = {
|
|
13
|
+
name: 'agent',
|
|
14
|
+
statements: [
|
|
15
|
+
(db) => db.schema
|
|
16
|
+
.createTable('agentThreads')
|
|
17
|
+
.addColumn('id', 'varchar(36)', (col) => col.primaryKey())
|
|
18
|
+
.addColumn('resourceId', 'varchar(255)', (col) => col.notNull())
|
|
19
|
+
.addColumn('title', 'text')
|
|
20
|
+
.addColumn('metadata', 'text')
|
|
21
|
+
.addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
|
|
22
|
+
.addColumn('updatedAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull()),
|
|
23
|
+
(db) => db.schema
|
|
24
|
+
.createIndex('idx_agent_threads_resource')
|
|
25
|
+
.on('agentThreads')
|
|
26
|
+
.column('resourceId'),
|
|
27
|
+
(db) => db.schema
|
|
28
|
+
.createTable('agentMessage')
|
|
29
|
+
.addColumn('id', 'varchar(36)', (col) => col.primaryKey())
|
|
30
|
+
.addColumn('threadId', 'varchar(36)', (col) => col.notNull().references('agentThreads.id').onDelete('cascade'))
|
|
31
|
+
.addColumn('role', 'varchar(50)', (col) => col.notNull())
|
|
32
|
+
.addColumn('content', 'text')
|
|
33
|
+
.addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull()),
|
|
34
|
+
(db) => db.schema
|
|
35
|
+
.createIndex('idx_agent_message_thread')
|
|
36
|
+
.on('agentMessage')
|
|
37
|
+
.columns(['threadId', 'createdAt']),
|
|
38
|
+
(db) => db.schema
|
|
39
|
+
.createTable('agentToolCall')
|
|
40
|
+
.addColumn('id', 'varchar(36)', (col) => col.primaryKey())
|
|
41
|
+
.addColumn('threadId', 'varchar(36)', (col) => col.notNull().references('agentThreads.id').onDelete('cascade'))
|
|
42
|
+
.addColumn('messageId', 'varchar(36)', (col) => col.notNull().references('agentMessage.id').onDelete('cascade'))
|
|
43
|
+
.addColumn('runId', 'varchar(36)')
|
|
44
|
+
.addColumn('toolName', 'varchar(255)', (col) => col.notNull())
|
|
45
|
+
.addColumn('args', 'text', (col) => col.notNull())
|
|
46
|
+
.addColumn('result', 'text')
|
|
47
|
+
.addColumn('approvalStatus', 'varchar(50)')
|
|
48
|
+
.addColumn('approvalType', 'varchar(50)')
|
|
49
|
+
.addColumn('agentRunId', 'varchar(36)')
|
|
50
|
+
.addColumn('displayToolName', 'varchar(255)')
|
|
51
|
+
.addColumn('displayArgs', 'text')
|
|
52
|
+
.addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull()),
|
|
53
|
+
(db) => db.schema
|
|
54
|
+
.createIndex('idx_agent_tool_call_thread')
|
|
55
|
+
.on('agentToolCall')
|
|
56
|
+
.column('threadId'),
|
|
57
|
+
(db) => db.schema
|
|
58
|
+
.createIndex('idx_agent_tool_call_message')
|
|
59
|
+
.on('agentToolCall')
|
|
60
|
+
.column('messageId'),
|
|
61
|
+
(db) => db.schema
|
|
62
|
+
.createTable('agentWorkingMemory')
|
|
63
|
+
.addColumn('id', 'varchar(255)', (col) => col.notNull())
|
|
64
|
+
.addColumn('scope', 'varchar(50)', (col) => col.notNull())
|
|
65
|
+
.addColumn('data', 'text', (col) => col.notNull())
|
|
66
|
+
.addColumn('updatedAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
|
|
67
|
+
.addPrimaryKeyConstraint('agent_working_memory_pk', ['id', 'scope']),
|
|
68
|
+
(db) => db.schema
|
|
69
|
+
.createTable('agentRun')
|
|
70
|
+
.addColumn('runId', 'varchar(36)', (col) => col.primaryKey())
|
|
71
|
+
.addColumn('agentName', 'varchar(255)', (col) => col.notNull())
|
|
72
|
+
.addColumn('threadId', 'varchar(36)', (col) => col.notNull().references('agentThreads.id').onDelete('cascade'))
|
|
73
|
+
.addColumn('resourceId', 'varchar(255)', (col) => col.notNull())
|
|
74
|
+
.addColumn('status', 'varchar(50)', (col) => col.notNull().defaultTo('running'))
|
|
75
|
+
.addColumn('errorMessage', 'text')
|
|
76
|
+
.addColumn('suspendReason', 'text')
|
|
77
|
+
.addColumn('missingRpcs', 'text')
|
|
78
|
+
.addColumn('pendingApprovals', 'text')
|
|
79
|
+
.addColumn('usageInputTokens', 'integer', (col) => col.notNull().defaultTo(0))
|
|
80
|
+
.addColumn('usageOutputTokens', 'integer', (col) => col.notNull().defaultTo(0))
|
|
81
|
+
.addColumn('usageModel', 'varchar(255)', (col) => col.notNull().defaultTo(''))
|
|
82
|
+
.addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull())
|
|
83
|
+
.addColumn('updatedAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull()),
|
|
84
|
+
(db) => db.schema
|
|
85
|
+
.createIndex('idx_agent_run_thread')
|
|
86
|
+
.on('agentRun')
|
|
87
|
+
.columns(['threadId', 'createdAt']),
|
|
88
|
+
(db) => db.schema
|
|
89
|
+
.createTable('agentRunScore')
|
|
90
|
+
.addColumn('id', 'varchar(36)', (col) => col.primaryKey())
|
|
91
|
+
.addColumn('runId', 'varchar(36)', (col) => col.notNull().references('agentRun.runId').onDelete('cascade'))
|
|
92
|
+
.addColumn('scorerName', 'varchar(255)', (col) => col.notNull())
|
|
93
|
+
.addColumn('score', 'real', (col) => col.notNull())
|
|
94
|
+
.addColumn('reason', 'text')
|
|
95
|
+
.addColumn('metadata', 'text')
|
|
96
|
+
.addColumn('createdAt', 'timestamp', (col) => col.defaultTo(sql `CURRENT_TIMESTAMP`).notNull()),
|
|
97
|
+
(db) => db.schema
|
|
98
|
+
.createIndex('idx_agent_run_score_run')
|
|
99
|
+
.on('agentRunScore')
|
|
100
|
+
.columns(['runId', 'createdAt']),
|
|
101
|
+
],
|
|
102
|
+
};
|
package/dist/src/schema/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CamelCasePlugin } from 'kysely';
|
|
2
2
|
import { schemaContext, } from './pikku-schema.types.js';
|
|
3
|
-
import {
|
|
3
|
+
import { agentSchema } from './agent.schema.js';
|
|
4
4
|
import { channelSchema } from './channel.schema.js';
|
|
5
5
|
import { credentialSchema } from './credential.schema.js';
|
|
6
6
|
import { deploymentSchema } from './deployment.schema.js';
|
|
@@ -31,7 +31,7 @@ export const pikkuSchemas = [
|
|
|
31
31
|
deploymentSchema,
|
|
32
32
|
webhookSchema,
|
|
33
33
|
workflowSchema,
|
|
34
|
-
|
|
34
|
+
agentSchema,
|
|
35
35
|
scopeSchema,
|
|
36
36
|
];
|
|
37
37
|
/**
|
|
@@ -39,8 +39,8 @@ export const pikkuSchemas = [
|
|
|
39
39
|
*
|
|
40
40
|
* The plugin is applied here rather than trusted to the caller. Declarations
|
|
41
41
|
* name tables and columns the way the rest of the codebase queries them
|
|
42
|
-
* (`
|
|
43
|
-
* the physical `
|
|
42
|
+
* (`agentThreads.resourceId`), and it is `CamelCasePlugin` that turns those into
|
|
43
|
+
* the physical `agent_threads.resource_id`. Without it the same declaration
|
|
44
44
|
* silently creates quoted camelCase tables that nothing can read.
|
|
45
45
|
*/
|
|
46
46
|
const bind = (db) => db.withPlugin(new CamelCasePlugin());
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/create-audited-kysely.ts","../src/index.ts","../src/kysely-
|
|
1
|
+
{"root":["../src/create-audited-kysely.ts","../src/index.ts","../src/kysely-agent-run-scores.ts","../src/kysely-agent-run-service.ts","../src/kysely-agent-run-state-service.ts","../src/kysely-agent-storage-service.ts","../src/kysely-audit-service.ts","../src/kysely-channel-store.ts","../src/kysely-credential-service.ts","../src/kysely-deployment-service.ts","../src/kysely-eventhub-store.ts","../src/kysely-json.ts","../src/kysely-scope-service.ts","../src/kysely-secret-service.ts","../src/kysely-session-store.ts","../src/kysely-tables.ts","../src/kysely-virtual-user-run-store.ts","../src/kysely-webhook-service.ts","../src/kysely-workflow-mirror.ts","../src/kysely-workflow-run-service.ts","../src/kysely-workflow-service.ts","../src/serialize-plugin.ts","../src/schema/agent.schema.ts","../src/schema/audit.schema.ts","../src/schema/channel.schema.ts","../src/schema/credential.schema.ts","../src/schema/deployment.schema.ts","../src/schema/index.ts","../src/schema/pikku-schema.types.ts","../src/schema/scope.schema.ts","../src/schema/secret.schema.ts","../src/schema/session.schema.ts","../src/schema/virtual-user.schema.ts","../src/schema/webhook.schema.ts","../src/schema/workflow.schema.ts","../bin/pikku-kysely-pure.ts"],"version":"6.0.3"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pikku/kysely",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.16",
|
|
4
4
|
"description": "Kysely-backed Pikku services — workflow state, secrets, sessions, scopes and audit",
|
|
5
5
|
"author": "yasser.fadl@gmail.com",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"prepublishOnly": "yarn build"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"@pikku/core": "^0.12.
|
|
24
|
+
"@pikku/core": "^0.12.84"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"kysely": "^0.29.0"
|
package/src/index.ts
CHANGED
|
@@ -4,9 +4,9 @@ export { KyselyWorkflowService } from './kysely-workflow-service.js'
|
|
|
4
4
|
export { KyselyWorkflowRunService } from './kysely-workflow-run-service.js'
|
|
5
5
|
export { KyselyWorkflowMirror } from './kysely-workflow-mirror.js'
|
|
6
6
|
export { KyselyDeploymentService } from './kysely-deployment-service.js'
|
|
7
|
-
export {
|
|
8
|
-
export { KyselyAgentRunService } from './kysely-
|
|
9
|
-
export {
|
|
7
|
+
export { KyselyAgentStorageService } from './kysely-agent-storage-service.js'
|
|
8
|
+
export { KyselyAgentRunService } from './kysely-agent-run-service.js'
|
|
9
|
+
export { KyselyAgentRunStateService } from './kysely-agent-run-state-service.js'
|
|
10
10
|
export { KyselySecretService } from './kysely-secret-service.js'
|
|
11
11
|
export { KyselyCredentialService } from './kysely-credential-service.js'
|
|
12
12
|
export { KyselySessionStore } from './kysely-session-store.js'
|
|
@@ -21,7 +21,6 @@ export { KyselyVirtualUserRunStore } from './kysely-virtual-user-run-store.js'
|
|
|
21
21
|
|
|
22
22
|
export {
|
|
23
23
|
SerializePlugin,
|
|
24
|
-
SqliteSerializePlugin,
|
|
25
24
|
BaseSerializePlugin,
|
|
26
25
|
type Serializer,
|
|
27
26
|
type Deserializer,
|
|
@@ -41,9 +40,12 @@ export {
|
|
|
41
40
|
} from './schema/index.js'
|
|
42
41
|
|
|
43
42
|
export type { KyselyPikkuDB } from './kysely-tables.js'
|
|
44
|
-
export type { WorkflowRunService } from '@pikku/core/workflow'
|
|
45
|
-
export type {
|
|
43
|
+
export type { WorkflowRunService } from '@pikku/core/ecosystem/workflow'
|
|
44
|
+
export type {
|
|
45
|
+
AgentRunService,
|
|
46
|
+
AgentRunRow,
|
|
47
|
+
} from '@pikku/core/ecosystem/agent'
|
|
46
48
|
export type {
|
|
47
49
|
VirtualUserRunStore,
|
|
48
50
|
VirtualUserRunRecord,
|
|
49
|
-
} from '@pikku/core/virtual-user'
|
|
51
|
+
} from '@pikku/core/ecosystem/virtual-user'
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { Kysely } from 'kysely'
|
|
2
2
|
import type { KyselyPikkuDB } from './kysely-tables.js'
|
|
3
3
|
import type { SaveScoreInput } from '@pikku/core/services'
|
|
4
|
-
import type {
|
|
4
|
+
import type { AgentRunScore } from '@pikku/core/agent-scorer'
|
|
5
5
|
import { parseJson } from './kysely-json.js'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* Shared by the two Kysely services that implement `
|
|
8
|
+
* Shared by the two Kysely services that implement `AgentRunStateService` — they
|
|
9
9
|
* differ in how they store approvals, not in how they store grades.
|
|
10
10
|
*/
|
|
11
11
|
export const saveRunScore = async (
|
|
@@ -13,7 +13,7 @@ export const saveRunScore = async (
|
|
|
13
13
|
score: SaveScoreInput
|
|
14
14
|
): Promise<void> => {
|
|
15
15
|
await db
|
|
16
|
-
.insertInto('
|
|
16
|
+
.insertInto('agentRunScore')
|
|
17
17
|
.values({
|
|
18
18
|
id: crypto.randomUUID(),
|
|
19
19
|
runId: score.runId,
|
|
@@ -28,9 +28,9 @@ export const saveRunScore = async (
|
|
|
28
28
|
export const getRunScores = async (
|
|
29
29
|
db: Kysely<KyselyPikkuDB>,
|
|
30
30
|
runId: string
|
|
31
|
-
): Promise<
|
|
31
|
+
): Promise<AgentRunScore[]> => {
|
|
32
32
|
const rows = await db
|
|
33
|
-
.selectFrom('
|
|
33
|
+
.selectFrom('agentRunScore')
|
|
34
34
|
.selectAll()
|
|
35
35
|
.where('runId', '=', runId)
|
|
36
36
|
.orderBy('createdAt', 'asc')
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
import type { AgentThread, AgentMessage } from '@pikku/core/agent'
|
|
1
2
|
import type {
|
|
2
|
-
AIThread,
|
|
3
|
-
AIMessage,
|
|
4
3
|
AgentRunRow,
|
|
5
4
|
AgentRunService,
|
|
6
|
-
} from '@pikku/core/
|
|
5
|
+
} from '@pikku/core/ecosystem/agent'
|
|
7
6
|
import type { Kysely } from 'kysely'
|
|
8
7
|
import { sql } from 'kysely'
|
|
9
8
|
import type { KyselyPikkuDB } from './kysely-tables.js'
|
|
@@ -23,7 +22,7 @@ export class KyselyAgentRunService implements AgentRunService {
|
|
|
23
22
|
owners?: string[]
|
|
24
23
|
limit?: number
|
|
25
24
|
offset?: number
|
|
26
|
-
}): Promise<
|
|
25
|
+
}): Promise<AgentThread[]> {
|
|
27
26
|
const {
|
|
28
27
|
agentName,
|
|
29
28
|
resourceId,
|
|
@@ -37,7 +36,7 @@ export class KyselyAgentRunService implements AgentRunService {
|
|
|
37
36
|
if (owners && owners.length === 0) return []
|
|
38
37
|
|
|
39
38
|
let query = this.db
|
|
40
|
-
.selectFrom('
|
|
39
|
+
.selectFrom('agentThreads as t')
|
|
41
40
|
.select([
|
|
42
41
|
't.id',
|
|
43
42
|
't.resourceId',
|
|
@@ -52,7 +51,7 @@ export class KyselyAgentRunService implements AgentRunService {
|
|
|
52
51
|
't.id',
|
|
53
52
|
'in',
|
|
54
53
|
this.db
|
|
55
|
-
.selectFrom('
|
|
54
|
+
.selectFrom('agentRun')
|
|
56
55
|
.select('threadId')
|
|
57
56
|
.where('agentName', '=', agentName)
|
|
58
57
|
.distinct()
|
|
@@ -87,9 +86,9 @@ export class KyselyAgentRunService implements AgentRunService {
|
|
|
87
86
|
return result.map((row) => this.mapThreadRow(row))
|
|
88
87
|
}
|
|
89
88
|
|
|
90
|
-
async getThread(threadId: string): Promise<
|
|
89
|
+
async getThread(threadId: string): Promise<AgentThread | null> {
|
|
91
90
|
const row = await this.db
|
|
92
|
-
.selectFrom('
|
|
91
|
+
.selectFrom('agentThreads')
|
|
93
92
|
.select([
|
|
94
93
|
'id',
|
|
95
94
|
'resourceId',
|
|
@@ -105,16 +104,16 @@ export class KyselyAgentRunService implements AgentRunService {
|
|
|
105
104
|
return this.mapThreadRow(row)
|
|
106
105
|
}
|
|
107
106
|
|
|
108
|
-
async getThreadMessages(threadId: string): Promise<
|
|
107
|
+
async getThreadMessages(threadId: string): Promise<AgentMessage[]> {
|
|
109
108
|
const [msgResult, tcResult] = await Promise.all([
|
|
110
109
|
this.db
|
|
111
|
-
.selectFrom('
|
|
110
|
+
.selectFrom('agentMessage')
|
|
112
111
|
.select(['id', 'role', 'content', 'createdAt'])
|
|
113
112
|
.where('threadId', '=', threadId)
|
|
114
113
|
.orderBy('createdAt', 'asc')
|
|
115
114
|
.execute(),
|
|
116
115
|
this.db
|
|
117
|
-
.selectFrom('
|
|
116
|
+
.selectFrom('agentToolCall')
|
|
118
117
|
.select(['id', 'messageId', 'toolName', 'args', 'result'])
|
|
119
118
|
.where('threadId', '=', threadId)
|
|
120
119
|
.orderBy('createdAt', 'asc')
|
|
@@ -128,11 +127,11 @@ export class KyselyAgentRunService implements AgentRunService {
|
|
|
128
127
|
tcByMessage.get(msgId)!.push(tc)
|
|
129
128
|
}
|
|
130
129
|
|
|
131
|
-
const messages:
|
|
130
|
+
const messages: AgentMessage[] = []
|
|
132
131
|
for (const row of msgResult) {
|
|
133
|
-
const msg:
|
|
132
|
+
const msg: AgentMessage = {
|
|
134
133
|
id: row.id,
|
|
135
|
-
role: row.role as
|
|
134
|
+
role: row.role as AgentMessage['role'],
|
|
136
135
|
content: row.content ?? undefined,
|
|
137
136
|
createdAt: new Date(row.createdAt as unknown as string),
|
|
138
137
|
}
|
|
@@ -170,7 +169,7 @@ export class KyselyAgentRunService implements AgentRunService {
|
|
|
170
169
|
|
|
171
170
|
async getThreadRuns(threadId: string): Promise<AgentRunRow[]> {
|
|
172
171
|
const result = await this.db
|
|
173
|
-
.selectFrom('
|
|
172
|
+
.selectFrom('agentRun')
|
|
174
173
|
.select([
|
|
175
174
|
'runId',
|
|
176
175
|
'agentName',
|
|
@@ -195,7 +194,7 @@ export class KyselyAgentRunService implements AgentRunService {
|
|
|
195
194
|
|
|
196
195
|
async deleteThread(threadId: string): Promise<boolean> {
|
|
197
196
|
const result = await this.db
|
|
198
|
-
.deleteFrom('
|
|
197
|
+
.deleteFrom('agentThreads')
|
|
199
198
|
.where('id', '=', threadId)
|
|
200
199
|
.executeTakeFirst()
|
|
201
200
|
|
|
@@ -204,7 +203,7 @@ export class KyselyAgentRunService implements AgentRunService {
|
|
|
204
203
|
|
|
205
204
|
async getDistinctAgentNames(): Promise<string[]> {
|
|
206
205
|
const result = await this.db
|
|
207
|
-
.selectFrom('
|
|
206
|
+
.selectFrom('agentRun')
|
|
208
207
|
.select('agentName')
|
|
209
208
|
.distinct()
|
|
210
209
|
.orderBy('agentName')
|
|
@@ -213,7 +212,7 @@ export class KyselyAgentRunService implements AgentRunService {
|
|
|
213
212
|
return result.map((row) => row.agentName)
|
|
214
213
|
}
|
|
215
214
|
|
|
216
|
-
private mapThreadRow(row: any):
|
|
215
|
+
private mapThreadRow(row: any): AgentThread {
|
|
217
216
|
return {
|
|
218
217
|
id: row.id as string,
|
|
219
218
|
resourceId: row.resourceId as string,
|
|
@@ -2,31 +2,32 @@ import { sql } from 'kysely'
|
|
|
2
2
|
import type { Kysely } from 'kysely'
|
|
3
3
|
import type { KyselyPikkuDB } from './kysely-tables.js'
|
|
4
4
|
import type {
|
|
5
|
-
|
|
5
|
+
AgentRunStateService,
|
|
6
6
|
CreateRunInput,
|
|
7
7
|
SaveScoreInput,
|
|
8
8
|
} from '@pikku/core/services'
|
|
9
|
-
import type { AgentRunState
|
|
10
|
-
import type {
|
|
9
|
+
import type { AgentRunState } from '@pikku/core/agent'
|
|
10
|
+
import type { PendingApproval } from '@pikku/core/ecosystem/agent'
|
|
11
|
+
import type { AgentRunScore } from '@pikku/core/agent-scorer'
|
|
11
12
|
import { ensurePikkuSchema } from './schema/index.js'
|
|
12
|
-
import {
|
|
13
|
-
import { getRunScores, saveRunScore } from './kysely-
|
|
13
|
+
import { agentSchema } from './schema/agent.schema.js'
|
|
14
|
+
import { getRunScores, saveRunScore } from './kysely-agent-run-scores.js'
|
|
14
15
|
|
|
15
|
-
export class
|
|
16
|
+
export class KyselyAgentRunStateService implements AgentRunStateService {
|
|
16
17
|
private initialized = false
|
|
17
18
|
|
|
18
19
|
constructor(private db: Kysely<KyselyPikkuDB>) {}
|
|
19
20
|
|
|
20
21
|
async init(): Promise<void> {
|
|
21
22
|
if (this.initialized) return
|
|
22
|
-
await ensurePikkuSchema(this.db,
|
|
23
|
+
await ensurePikkuSchema(this.db, agentSchema)
|
|
23
24
|
this.initialized = true
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
async createRun(run: CreateRunInput): Promise<string> {
|
|
27
28
|
const runId = `run-${crypto.randomUUID()}`
|
|
28
29
|
await this.db
|
|
29
|
-
.insertInto('
|
|
30
|
+
.insertInto('agentRun')
|
|
30
31
|
.values({
|
|
31
32
|
runId,
|
|
32
33
|
agentName: run.agentName,
|
|
@@ -67,7 +68,7 @@ export class KyselyAIRunStateService implements AIRunStateService {
|
|
|
67
68
|
}
|
|
68
69
|
|
|
69
70
|
await this.db
|
|
70
|
-
.updateTable('
|
|
71
|
+
.updateTable('agentRun')
|
|
71
72
|
.set(values)
|
|
72
73
|
.where('runId', '=', runId)
|
|
73
74
|
.execute()
|
|
@@ -75,7 +76,7 @@ export class KyselyAIRunStateService implements AIRunStateService {
|
|
|
75
76
|
|
|
76
77
|
async getRun(runId: string): Promise<AgentRunState | null> {
|
|
77
78
|
const row = await this.db
|
|
78
|
-
.selectFrom('
|
|
79
|
+
.selectFrom('agentRun')
|
|
79
80
|
.selectAll()
|
|
80
81
|
.where('runId', '=', runId)
|
|
81
82
|
.executeTakeFirst()
|
|
@@ -84,7 +85,7 @@ export class KyselyAIRunStateService implements AIRunStateService {
|
|
|
84
85
|
|
|
85
86
|
async getRunsByThread(threadId: string): Promise<AgentRunState[]> {
|
|
86
87
|
const rows = await this.db
|
|
87
|
-
.selectFrom('
|
|
88
|
+
.selectFrom('agentRun')
|
|
88
89
|
.selectAll()
|
|
89
90
|
.where('threadId', '=', threadId)
|
|
90
91
|
.orderBy('createdAt', 'desc')
|
|
@@ -97,7 +98,7 @@ export class KyselyAIRunStateService implements AIRunStateService {
|
|
|
97
98
|
status: 'approved' | 'denied'
|
|
98
99
|
): Promise<boolean> {
|
|
99
100
|
const rows = await this.db
|
|
100
|
-
.selectFrom('
|
|
101
|
+
.selectFrom('agentRun')
|
|
101
102
|
.select(['runId', 'pendingApprovals'])
|
|
102
103
|
.where('status', '=', 'suspended')
|
|
103
104
|
.execute()
|
|
@@ -126,7 +127,7 @@ export class KyselyAIRunStateService implements AIRunStateService {
|
|
|
126
127
|
// Compare-and-swap on the list we read: a concurrent approver that got
|
|
127
128
|
// there first has already rewritten it, and this update matches no row.
|
|
128
129
|
const result = await this.db
|
|
129
|
-
.updateTable('
|
|
130
|
+
.updateTable('agentRun')
|
|
130
131
|
.set(updates as any)
|
|
131
132
|
.where('runId', '=', row.runId)
|
|
132
133
|
.where('status', '=', 'suspended')
|
|
@@ -142,7 +143,7 @@ export class KyselyAIRunStateService implements AIRunStateService {
|
|
|
142
143
|
toolCallId: string
|
|
143
144
|
): Promise<{ run: AgentRunState; approval: PendingApproval } | null> {
|
|
144
145
|
const rows = await this.db
|
|
145
|
-
.selectFrom('
|
|
146
|
+
.selectFrom('agentRun')
|
|
146
147
|
.selectAll()
|
|
147
148
|
.where('status', '=', 'suspended')
|
|
148
149
|
.execute()
|
|
@@ -170,7 +171,7 @@ export class KyselyAIRunStateService implements AIRunStateService {
|
|
|
170
171
|
await saveRunScore(this.db, score)
|
|
171
172
|
}
|
|
172
173
|
|
|
173
|
-
async getScores(runId: string): Promise<
|
|
174
|
+
async getScores(runId: string): Promise<AgentRunScore[]> {
|
|
174
175
|
return getRunScores(this.db, runId)
|
|
175
176
|
}
|
|
176
177
|
|