@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
@@ -3,7 +3,7 @@ import type {
3
3
  WorkflowStatus,
4
4
  StepStatus,
5
5
  WorkflowVersionStatus,
6
- } from '@pikku/core/workflow'
6
+ } from '@pikku/core/ecosystem/workflow'
7
7
 
8
8
  export interface ChannelsTable {
9
9
  channelId: string
@@ -86,7 +86,7 @@ export interface WorkflowVersionsTable {
86
86
  createdAt: Generated<Date>
87
87
  }
88
88
 
89
- export interface AIThreadsTable {
89
+ export interface AgentThreadsTable {
90
90
  id: string
91
91
  resourceId: string
92
92
  title: string | null
@@ -95,7 +95,7 @@ export interface AIThreadsTable {
95
95
  updatedAt: Generated<Date>
96
96
  }
97
97
 
98
- export interface AIMessageTable {
98
+ export interface AgentMessageTable {
99
99
  id: string
100
100
  threadId: string
101
101
  role: 'system' | 'user' | 'assistant' | 'tool'
@@ -103,7 +103,7 @@ export interface AIMessageTable {
103
103
  createdAt: Generated<Date>
104
104
  }
105
105
 
106
- export interface AIToolCallTable {
106
+ export interface AgentToolCallTable {
107
107
  id: string
108
108
  threadId: string
109
109
  messageId: string
@@ -119,14 +119,14 @@ export interface AIToolCallTable {
119
119
  createdAt: Generated<Date>
120
120
  }
121
121
 
122
- export interface AIWorkingMemoryTable {
122
+ export interface AgentWorkingMemoryTable {
123
123
  id: string
124
124
  scope: string
125
125
  data: string
126
126
  updatedAt: Generated<Date>
127
127
  }
128
128
 
129
- export interface AIRunTable {
129
+ export interface AgentRunTable {
130
130
  runId: Generated<string>
131
131
  agentName: string
132
132
  threadId: string
@@ -151,6 +151,18 @@ export interface AIRunTable {
151
151
  updatedAt: Generated<Date>
152
152
  }
153
153
 
154
+ export interface AgentRunScoreTable {
155
+ id: string
156
+ runId: string
157
+ scorerName: string
158
+ /** 0..1, so grades are comparable across scorers. */
159
+ score: number
160
+ reason: string | null
161
+ /** JSON-serialized structured detail the scorer returned alongside the score. */
162
+ metadata: string | null
163
+ createdAt: Generated<Date>
164
+ }
165
+
154
166
  export interface PikkuDeploymentsTable {
155
167
  deploymentId: string
156
168
  endpoint: string
@@ -324,11 +336,12 @@ export interface KyselyPikkuDB {
324
336
  workflowStep: WorkflowStepTable
325
337
  workflowStepHistory: WorkflowStepHistoryTable
326
338
  workflowVersions: WorkflowVersionsTable
327
- aiThreads: AIThreadsTable
328
- aiMessage: AIMessageTable
329
- aiToolCall: AIToolCallTable
330
- aiWorkingMemory: AIWorkingMemoryTable
331
- aiRun: AIRunTable
339
+ agentThreads: AgentThreadsTable
340
+ agentMessage: AgentMessageTable
341
+ agentToolCall: AgentToolCallTable
342
+ agentWorkingMemory: AgentWorkingMemoryTable
343
+ agentRun: AgentRunTable
344
+ agentRunScore: AgentRunScoreTable
332
345
  pikkuDeployments: PikkuDeploymentsTable
333
346
  pikkuDeploymentFunctions: PikkuDeploymentFunctionsTable
334
347
  secrets: SecretsTable
@@ -1,12 +1,12 @@
1
1
  import type { Kysely, Selectable } from 'kysely'
2
2
  import { randomUUID } from 'node:crypto'
3
+ import type { VirtualUserDisposition } from '@pikku/core/virtual-user'
3
4
  import type {
4
- VirtualUserDisposition,
5
5
  VirtualUserRunOutcome,
6
6
  VirtualUserRunRecord,
7
7
  VirtualUserRunStart,
8
8
  VirtualUserRunStore,
9
- } from '@pikku/core/virtual-user'
9
+ } from '@pikku/core/ecosystem/virtual-user'
10
10
  import type { KyselyPikkuDB, VirtualUserRunTable } from './kysely-tables.js'
11
11
  import { parseJson } from './kysely-json.js'
12
12
  import { ensurePikkuSchema } from './schema/index.js'
@@ -1,12 +1,12 @@
1
+ import { QueueWebhookService } from '@pikku/core/services'
1
2
  import {
2
3
  PIKKU_OUTGOING_WEBHOOK_QUEUE_NAME,
3
- QueueWebhookService,
4
4
  type SendWebhookInput,
5
5
  type SendWebhookResult,
6
6
  type WebhookAttemptResult,
7
7
  type WebhookDeliveryRecord,
8
8
  type WebhookDeliveryWithAttempts,
9
- } from '@pikku/core/services'
9
+ } from '@pikku/core/ecosystem/services'
10
10
  import type { QueueService } from '@pikku/core/queue'
11
11
  import type { Kysely } from 'kysely'
12
12
  import type { KyselyPikkuDB } from './kysely-tables.js'
@@ -1,4 +1,4 @@
1
- import type { SerializedError } from '@pikku/core'
1
+ import type { SerializedError } from '@pikku/core/ecosystem/types'
2
2
  import type {
3
3
  StepState,
4
4
  WorkflowRunMirror,
@@ -6,7 +6,7 @@ import type {
6
6
  WorkflowStatus,
7
7
  WorkflowPlannedStep,
8
8
  WorkflowVersionStatus,
9
- } from '@pikku/core/workflow'
9
+ } from '@pikku/core/ecosystem/workflow'
10
10
  import type { Kysely } from 'kysely'
11
11
  import type { KyselyPikkuDB } from './kysely-tables.js'
12
12
  import { ensurePikkuSchema } from './schema/index.js'
@@ -3,7 +3,7 @@ import type {
3
3
  StepState,
4
4
  WorkflowStatus,
5
5
  WorkflowRunService,
6
- } from '@pikku/core/workflow'
6
+ } from '@pikku/core/ecosystem/workflow'
7
7
  import type { Kysely } from 'kysely'
8
8
  import type { KyselyPikkuDB } from './kysely-tables.js'
9
9
  import { parseJson } from './kysely-json.js'
@@ -1,15 +1,6 @@
1
- import type { SerializedError } from '@pikku/core'
2
- import {
3
- PikkuWorkflowService,
4
- type WorkflowPlannedStep,
5
- type WorkflowQueueOptions,
6
- type WorkflowRun,
7
- type WorkflowRunWire,
8
- type StepState,
9
- type StepStatus,
10
- type WorkflowStatus,
11
- type WorkflowVersionStatus,
12
- } from '@pikku/core/workflow'
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'
13
4
  import { sql, type Kysely } from 'kysely'
14
5
  import type { KyselyPikkuDB } from './kysely-tables.js'
15
6
  import { KyselyWorkflowRunService } from './kysely-workflow-run-service.js'
@@ -4,19 +4,19 @@ import type { PikkuSchema } from './pikku-schema.types.js'
4
4
  /**
5
5
  * Threads, messages, tool calls, working memory and agent runs.
6
6
  *
7
- * `aiRun` carries `pendingApprovals` even though the two services that used to
8
- * create this table disagreed about it: `KyselyAIStorageService` omitted the
9
- * column while `KyselyAIRunStateService` declared it and read it back through
7
+ * `agentRun` carries `pendingApprovals` even though the two services that used to
8
+ * create this table disagreed about it: `KyselyAgentStorageService` omitted the
9
+ * column while `KyselyAgentRunStateService` declared it and read it back through
10
10
  * casts. Whichever service happened to run first decided the shape, and on a
11
11
  * database where the storage service won, resolving an approval failed. One
12
12
  * declaration ends that — the column is here because live code reads it.
13
13
  */
14
- export const aiSchema: PikkuSchema = {
15
- name: 'ai',
14
+ export const agentSchema: PikkuSchema = {
15
+ name: 'agent',
16
16
  statements: [
17
17
  (db) =>
18
18
  db.schema
19
- .createTable('aiThreads')
19
+ .createTable('agentThreads')
20
20
  .addColumn('id', 'varchar(36)', (col) => col.primaryKey())
21
21
  .addColumn('resourceId', 'varchar(255)', (col) => col.notNull())
22
22
  .addColumn('title', 'text')
@@ -30,16 +30,16 @@ export const aiSchema: PikkuSchema = {
30
30
 
31
31
  (db) =>
32
32
  db.schema
33
- .createIndex('idx_ai_threads_resource')
34
- .on('aiThreads')
33
+ .createIndex('idx_agent_threads_resource')
34
+ .on('agentThreads')
35
35
  .column('resourceId'),
36
36
 
37
37
  (db) =>
38
38
  db.schema
39
- .createTable('aiMessage')
39
+ .createTable('agentMessage')
40
40
  .addColumn('id', 'varchar(36)', (col) => col.primaryKey())
41
41
  .addColumn('threadId', 'varchar(36)', (col) =>
42
- col.notNull().references('aiThreads.id').onDelete('cascade')
42
+ col.notNull().references('agentThreads.id').onDelete('cascade')
43
43
  )
44
44
  .addColumn('role', 'varchar(50)', (col) => col.notNull())
45
45
  .addColumn('content', 'text')
@@ -49,19 +49,19 @@ export const aiSchema: PikkuSchema = {
49
49
 
50
50
  (db) =>
51
51
  db.schema
52
- .createIndex('idx_ai_message_thread')
53
- .on('aiMessage')
52
+ .createIndex('idx_agent_message_thread')
53
+ .on('agentMessage')
54
54
  .columns(['threadId', 'createdAt']),
55
55
 
56
56
  (db) =>
57
57
  db.schema
58
- .createTable('aiToolCall')
58
+ .createTable('agentToolCall')
59
59
  .addColumn('id', 'varchar(36)', (col) => col.primaryKey())
60
60
  .addColumn('threadId', 'varchar(36)', (col) =>
61
- col.notNull().references('aiThreads.id').onDelete('cascade')
61
+ col.notNull().references('agentThreads.id').onDelete('cascade')
62
62
  )
63
63
  .addColumn('messageId', 'varchar(36)', (col) =>
64
- col.notNull().references('aiMessage.id').onDelete('cascade')
64
+ col.notNull().references('agentMessage.id').onDelete('cascade')
65
65
  )
66
66
  .addColumn('runId', 'varchar(36)')
67
67
  .addColumn('toolName', 'varchar(255)', (col) => col.notNull())
@@ -78,34 +78,34 @@ export const aiSchema: PikkuSchema = {
78
78
 
79
79
  (db) =>
80
80
  db.schema
81
- .createIndex('idx_ai_tool_call_thread')
82
- .on('aiToolCall')
81
+ .createIndex('idx_agent_tool_call_thread')
82
+ .on('agentToolCall')
83
83
  .column('threadId'),
84
84
 
85
85
  (db) =>
86
86
  db.schema
87
- .createIndex('idx_ai_tool_call_message')
88
- .on('aiToolCall')
87
+ .createIndex('idx_agent_tool_call_message')
88
+ .on('agentToolCall')
89
89
  .column('messageId'),
90
90
 
91
91
  (db) =>
92
92
  db.schema
93
- .createTable('aiWorkingMemory')
93
+ .createTable('agentWorkingMemory')
94
94
  .addColumn('id', 'varchar(255)', (col) => col.notNull())
95
95
  .addColumn('scope', 'varchar(50)', (col) => col.notNull())
96
96
  .addColumn('data', 'text', (col) => col.notNull())
97
97
  .addColumn('updatedAt', 'timestamp', (col) =>
98
98
  col.defaultTo(sql`CURRENT_TIMESTAMP`).notNull()
99
99
  )
100
- .addPrimaryKeyConstraint('ai_working_memory_pk', ['id', 'scope']),
100
+ .addPrimaryKeyConstraint('agent_working_memory_pk', ['id', 'scope']),
101
101
 
102
102
  (db) =>
103
103
  db.schema
104
- .createTable('aiRun')
104
+ .createTable('agentRun')
105
105
  .addColumn('runId', 'varchar(36)', (col) => col.primaryKey())
106
106
  .addColumn('agentName', 'varchar(255)', (col) => col.notNull())
107
107
  .addColumn('threadId', 'varchar(36)', (col) =>
108
- col.notNull().references('aiThreads.id').onDelete('cascade')
108
+ col.notNull().references('agentThreads.id').onDelete('cascade')
109
109
  )
110
110
  .addColumn('resourceId', 'varchar(255)', (col) => col.notNull())
111
111
  .addColumn('status', 'varchar(50)', (col) =>
@@ -133,8 +133,29 @@ export const aiSchema: PikkuSchema = {
133
133
 
134
134
  (db) =>
135
135
  db.schema
136
- .createIndex('idx_ai_run_thread')
137
- .on('aiRun')
136
+ .createIndex('idx_agent_run_thread')
137
+ .on('agentRun')
138
138
  .columns(['threadId', 'createdAt']),
139
+
140
+ (db) =>
141
+ db.schema
142
+ .createTable('agentRunScore')
143
+ .addColumn('id', 'varchar(36)', (col) => col.primaryKey())
144
+ .addColumn('runId', 'varchar(36)', (col) =>
145
+ col.notNull().references('agentRun.runId').onDelete('cascade')
146
+ )
147
+ .addColumn('scorerName', 'varchar(255)', (col) => col.notNull())
148
+ .addColumn('score', 'real', (col) => col.notNull())
149
+ .addColumn('reason', 'text')
150
+ .addColumn('metadata', 'text')
151
+ .addColumn('createdAt', 'timestamp', (col) =>
152
+ col.defaultTo(sql`CURRENT_TIMESTAMP`).notNull()
153
+ ),
154
+
155
+ (db) =>
156
+ db.schema
157
+ .createIndex('idx_agent_run_score_run')
158
+ .on('agentRunScore')
159
+ .columns(['runId', 'createdAt']),
139
160
  ],
140
161
  }
@@ -6,7 +6,7 @@ import {
6
6
  type SchemaContext,
7
7
  type SchemaRequirement,
8
8
  } from './pikku-schema.types.js'
9
- import { aiSchema } from './ai.schema.js'
9
+ import { agentSchema } from './agent.schema.js'
10
10
  import { channelSchema } from './channel.schema.js'
11
11
  import { credentialSchema } from './credential.schema.js'
12
12
  import { deploymentSchema } from './deployment.schema.js'
@@ -53,7 +53,7 @@ export const pikkuSchemas: PikkuSchema[] = [
53
53
  deploymentSchema,
54
54
  webhookSchema,
55
55
  workflowSchema,
56
- aiSchema,
56
+ agentSchema,
57
57
  scopeSchema,
58
58
  ]
59
59
 
@@ -62,8 +62,8 @@ export const pikkuSchemas: PikkuSchema[] = [
62
62
  *
63
63
  * The plugin is applied here rather than trusted to the caller. Declarations
64
64
  * name tables and columns the way the rest of the codebase queries them
65
- * (`aiThreads.resourceId`), and it is `CamelCasePlugin` that turns those into
66
- * the physical `ai_threads.resource_id`. Without it the same declaration
65
+ * (`agentThreads.resourceId`), and it is `CamelCasePlugin` that turns those into
66
+ * the physical `agent_threads.resource_id`. Without it the same declaration
67
67
  * silently creates quoted camelCase tables that nothing can read.
68
68
  */
69
69
  const bind = (db: Kysely<any>) => db.withPlugin(new CamelCasePlugin())
@@ -60,13 +60,13 @@ describe('pikku runtime schema', () => {
60
60
  test('declares logical names and compiles them to physical ones', () => {
61
61
  const sql = compileFor('postgres')
62
62
 
63
- // The declaration says `aiThreads.resourceId`, matching how every query in
64
- // the package addresses it; the plugin is what makes the table `ai_threads`.
65
- assert.match(sql, /create table "ai_threads"/)
63
+ // The declaration says `agentThreads.resourceId`, matching how every query in
64
+ // the package addresses it; the plugin is what makes the table `agent_threads`.
65
+ assert.match(sql, /create table "agent_threads"/)
66
66
  assert.match(sql, /"resource_id" varchar\(255\) not null/)
67
67
  assert.doesNotMatch(
68
68
  sql,
69
- /"aiThreads"/,
69
+ /"agentThreads"/,
70
70
  'a camelCase table name means the plugin was not applied'
71
71
  )
72
72
  })
@@ -74,7 +74,7 @@ describe('pikku runtime schema', () => {
74
74
  test('rewrites foreign key targets too', () => {
75
75
  assert.match(
76
76
  compileFor('postgres'),
77
- /references "ai_threads" \("id"\)/,
77
+ /references "agent_threads" \("id"\)/,
78
78
  'an unrewritten reference target points at a table that does not exist'
79
79
  )
80
80
  })
@@ -87,7 +87,7 @@ describe('pikku runtime schema', () => {
87
87
  }
88
88
  })
89
89
 
90
- test('carries the ai_run column the two old declarations disagreed on', () => {
90
+ test('carries the agent_run column the two old declarations disagreed on', () => {
91
91
  assert.match(compileFor('postgres'), /"pending_approvals" text/)
92
92
  })
93
93
 
@@ -231,8 +231,3 @@ export class SerializePlugin extends BaseSerializePlugin {
231
231
  super(serializer, deserializer, skipNodeKind)
232
232
  }
233
233
  }
234
-
235
- /**
236
- * @deprecated use {@link SerializePlugin} instead
237
- */
238
- export const SqliteSerializePlugin = SerializePlugin