@pikku/kysely 0.13.15 → 0.13.18

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 (65) hide show
  1. package/CHANGELOG.md +229 -0
  2. package/bin/pikku-kysely-pure.ts +0 -0
  3. package/dist/src/coercion-plugin.d.ts +41 -0
  4. package/dist/src/coercion-plugin.js +150 -0
  5. package/dist/src/create-audited-kysely.d.ts +1 -1
  6. package/dist/src/index.d.ts +6 -5
  7. package/dist/src/index.js +5 -4
  8. package/dist/src/{kysely-ai-run-scores.d.ts → kysely-agent-run-scores.d.ts} +3 -3
  9. package/dist/src/{kysely-ai-run-scores.js → kysely-agent-run-scores.js} +3 -3
  10. package/dist/src/{kysely-ai-agent-run-service.d.ts → kysely-agent-run-service.d.ts} +4 -4
  11. package/dist/src/{kysely-ai-agent-run-service.js → kysely-agent-run-service.js} +8 -8
  12. package/dist/src/{kysely-ai-run-state-service.d.ts → kysely-agent-run-state-service.d.ts} +6 -5
  13. package/dist/src/{kysely-ai-run-state-service.js → kysely-agent-run-state-service.js} +11 -11
  14. package/dist/src/{kysely-ai-storage-service.d.ts → kysely-agent-storage-service.d.ts} +10 -10
  15. package/dist/src/{kysely-ai-storage-service.js → kysely-agent-storage-service.js} +31 -28
  16. package/dist/src/kysely-audit-service.d.ts +3 -1
  17. package/dist/src/kysely-channel-store.d.ts +1 -1
  18. package/dist/src/kysely-credential-service.js +1 -1
  19. package/dist/src/kysely-deployment-service.d.ts +2 -1
  20. package/dist/src/kysely-scope-service.d.ts +2 -1
  21. package/dist/src/kysely-secret-service.d.ts +1 -1
  22. package/dist/src/kysely-secret-service.js +2 -2
  23. package/dist/src/kysely-session-store.d.ts +1 -1
  24. package/dist/src/kysely-tables.d.ts +12 -12
  25. package/dist/src/kysely-webhook-service.d.ts +2 -1
  26. package/dist/src/kysely-webhook-service.js +2 -1
  27. package/dist/src/kysely-workflow-mirror.d.ts +1 -1
  28. package/dist/src/kysely-workflow-service.d.ts +3 -2
  29. package/dist/src/kysely-workflow-service.js +1 -1
  30. package/dist/src/schema/{ai.schema.d.ts → agent.schema.d.ts} +4 -4
  31. package/dist/src/schema/{ai.schema.js → agent.schema.js} +29 -29
  32. package/dist/src/schema/index.js +4 -4
  33. package/dist/src/serialize-plugin.d.ts +0 -4
  34. package/dist/src/serialize-plugin.js +0 -4
  35. package/dist/tsconfig.tsbuildinfo +1 -1
  36. package/package.json +7 -7
  37. package/src/coercion-plugin.test.ts +160 -0
  38. package/src/coercion-plugin.ts +187 -0
  39. package/src/create-audited-kysely.test.ts +1 -1
  40. package/src/create-audited-kysely.ts +1 -1
  41. package/src/index.ts +10 -5
  42. package/src/{kysely-ai-run-scores.ts → kysely-agent-run-scores.ts} +5 -5
  43. package/src/{kysely-ai-agent-run-service.ts → kysely-agent-run-service.ts} +18 -18
  44. package/src/{kysely-ai-run-state-service.ts → kysely-agent-run-state-service.ts} +16 -15
  45. package/src/{kysely-ai-storage-service.ts → kysely-agent-storage-service.ts} +53 -43
  46. package/src/kysely-audit-service.test.ts +1 -1
  47. package/src/kysely-audit-service.ts +4 -6
  48. package/src/kysely-channel-store.ts +1 -1
  49. package/src/kysely-credential-service.ts +5 -5
  50. package/src/kysely-deployment-service.ts +2 -1
  51. package/src/kysely-scope-service.ts +5 -2
  52. package/src/kysely-secret-service.ts +7 -6
  53. package/src/kysely-services.test.ts +10 -10
  54. package/src/kysely-session-store.ts +1 -1
  55. package/src/kysely-tables.ts +12 -12
  56. package/src/kysely-virtual-user-run-store.ts +1 -1
  57. package/src/kysely-webhook-service.test.ts +1 -1
  58. package/src/kysely-webhook-service.ts +2 -2
  59. package/src/kysely-workflow-mirror.ts +1 -1
  60. package/src/kysely-workflow-service.test.ts +0 -1
  61. package/src/kysely-workflow-service.ts +11 -11
  62. package/src/schema/{ai.schema.ts → agent.schema.ts} +29 -29
  63. package/src/schema/index.ts +4 -4
  64. package/src/schema/schema.test.ts +6 -6
  65. package/src/serialize-plugin.ts +0 -5
@@ -13,8 +13,8 @@ import { KyselyEventHubStore } from './kysely-eventhub-store.js'
13
13
  import { KyselyWorkflowService } from './kysely-workflow-service.js'
14
14
  import { KyselyWorkflowRunService } from './kysely-workflow-run-service.js'
15
15
  import { KyselyDeploymentService } from './kysely-deployment-service.js'
16
- import { KyselyAIStorageService } from './kysely-ai-storage-service.js'
17
- import { KyselyAgentRunService } from './kysely-ai-agent-run-service.js'
16
+ import { KyselyAgentStorageService } from './kysely-agent-storage-service.js'
17
+ import { KyselyAgentRunService } from './kysely-agent-run-service.js'
18
18
  import { KyselySecretService } from './kysely-secret-service.js'
19
19
  import { KyselyCredentialService } from './kysely-credential-service.js'
20
20
  import { KyselySessionStore } from './kysely-session-store.js'
@@ -42,12 +42,12 @@ async function dropAllTables(db: Kysely<KyselyPikkuDB>): Promise<void> {
42
42
  const tables = [
43
43
  'pikku_deployment_functions',
44
44
  'pikku_deployments',
45
- 'ai_tool_call',
46
- 'ai_message',
47
- 'ai_run_score',
48
- 'ai_run',
49
- 'ai_working_memory',
50
- 'ai_threads',
45
+ 'agent_tool_call',
46
+ 'agent_message',
47
+ 'agent_run_score',
48
+ 'agent_run',
49
+ 'agent_working_memory',
50
+ 'agent_threads',
51
51
  'channel_subscriptions',
52
52
  'channels',
53
53
  'workflow_step_history',
@@ -97,8 +97,8 @@ function registerTests(
97
97
  await s.init()
98
98
  return s
99
99
  },
100
- aiStorageService: async () => {
101
- const s = new KyselyAIStorageService(getDb())
100
+ agentStorageService: async () => {
101
+ const s = new KyselyAgentStorageService(getDb())
102
102
  await s.init()
103
103
  return s
104
104
  },
@@ -1,5 +1,5 @@
1
- import type { CoreUserSession } from '@pikku/core'
2
1
  import type { SessionStore } from '@pikku/core/services'
2
+ import type { CoreUserSession } from '@pikku/core/types'
3
3
  import type { Kysely } from 'kysely'
4
4
  import type { KyselyPikkuDB } from './kysely-tables.js'
5
5
  import { parseJson } from './kysely-json.js'
@@ -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,7 +151,7 @@ export interface AIRunTable {
151
151
  updatedAt: Generated<Date>
152
152
  }
153
153
 
154
- export interface AIRunScoreTable {
154
+ export interface AgentRunScoreTable {
155
155
  id: string
156
156
  runId: string
157
157
  scorerName: string
@@ -336,12 +336,12 @@ export interface KyselyPikkuDB {
336
336
  workflowStep: WorkflowStepTable
337
337
  workflowStepHistory: WorkflowStepHistoryTable
338
338
  workflowVersions: WorkflowVersionsTable
339
- aiThreads: AIThreadsTable
340
- aiMessage: AIMessageTable
341
- aiToolCall: AIToolCallTable
342
- aiWorkingMemory: AIWorkingMemoryTable
343
- aiRun: AIRunTable
344
- aiRunScore: AIRunScoreTable
339
+ agentThreads: AgentThreadsTable
340
+ agentMessage: AgentMessageTable
341
+ agentToolCall: AgentToolCallTable
342
+ agentWorkingMemory: AgentWorkingMemoryTable
343
+ agentRun: AgentRunTable
344
+ agentRunScore: AgentRunScoreTable
345
345
  pikkuDeployments: PikkuDeploymentsTable
346
346
  pikkuDeploymentFunctions: PikkuDeploymentFunctionsTable
347
347
  secrets: SecretsTable
@@ -1,7 +1,7 @@
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,
@@ -2,7 +2,7 @@ import { describe, test, beforeEach } from 'node:test'
2
2
  import assert from 'node:assert/strict'
3
3
  import { CamelCasePlugin, Kysely, SqliteDialect } from 'kysely'
4
4
  import Database from 'better-sqlite3'
5
- import { setSingletonServices } from '@pikku/core/ecosystem'
5
+ import { setSingletonServices } from '@pikku/core/state'
6
6
 
7
7
  import type { KyselyPikkuDB } from './kysely-tables.js'
8
8
  import { SerializePlugin } from './serialize-plugin.js'
@@ -1,13 +1,13 @@
1
+ import { QueueWebhookService } from '@pikku/core/services'
2
+ import type { QueueService } from '@pikku/core/queue'
1
3
  import {
2
4
  PIKKU_OUTGOING_WEBHOOK_QUEUE_NAME,
3
- QueueWebhookService,
4
5
  type SendWebhookInput,
5
6
  type SendWebhookResult,
6
7
  type WebhookAttemptResult,
7
8
  type WebhookDeliveryRecord,
8
9
  type WebhookDeliveryWithAttempts,
9
10
  } from '@pikku/core/services'
10
- import type { QueueService } from '@pikku/core/queue'
11
11
  import type { Kysely } from 'kysely'
12
12
  import type { KyselyPikkuDB } from './kysely-tables.js'
13
13
  import { ensurePikkuSchema } from './schema/index.js'
@@ -1,4 +1,4 @@
1
- import type { SerializedError } from '@pikku/core'
1
+ import type { SerializedError } from '@pikku/core/errors'
2
2
  import type {
3
3
  StepState,
4
4
  WorkflowRunMirror,
@@ -428,4 +428,3 @@ describe('KyselyWorkflowService — a transition that addresses no history row',
428
428
  assert.equal(history[0]!.status, 'succeeded')
429
429
  })
430
430
  })
431
-
@@ -1,14 +1,14 @@
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,
1
+ import type { SerializedError } from '@pikku/core/errors'
2
+ import { PikkuWorkflowService } from '@pikku/core/workflow'
3
+ import type {
4
+ WorkflowPlannedStep,
5
+ WorkflowQueueOptions,
6
+ WorkflowRun,
7
+ WorkflowRunWire,
8
+ StepState,
9
+ StepStatus,
10
+ WorkflowStatus,
11
+ WorkflowVersionStatus,
12
12
  } from '@pikku/core/workflow'
13
13
  import { sql, type Kysely } from 'kysely'
14
14
  import type { KyselyPikkuDB } from './kysely-tables.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,16 +133,16 @@ 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
139
 
140
140
  (db) =>
141
141
  db.schema
142
- .createTable('aiRunScore')
142
+ .createTable('agentRunScore')
143
143
  .addColumn('id', 'varchar(36)', (col) => col.primaryKey())
144
144
  .addColumn('runId', 'varchar(36)', (col) =>
145
- col.notNull().references('aiRun.runId').onDelete('cascade')
145
+ col.notNull().references('agentRun.runId').onDelete('cascade')
146
146
  )
147
147
  .addColumn('scorerName', 'varchar(255)', (col) => col.notNull())
148
148
  .addColumn('score', 'real', (col) => col.notNull())
@@ -154,8 +154,8 @@ export const aiSchema: PikkuSchema = {
154
154
 
155
155
  (db) =>
156
156
  db.schema
157
- .createIndex('idx_ai_run_score_run')
158
- .on('aiRunScore')
157
+ .createIndex('idx_agent_run_score_run')
158
+ .on('agentRunScore')
159
159
  .columns(['runId', 'createdAt']),
160
160
  ],
161
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