@pikku/kysely 0.13.24 → 0.13.28
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 +271 -0
- package/dist/src/index.d.ts +4 -1
- package/dist/src/index.js +3 -1
- package/dist/src/kysely-analytics-service.d.ts +30 -0
- package/dist/src/kysely-analytics-service.js +80 -0
- package/dist/src/kysely-feature-flag-store.d.ts +37 -0
- package/dist/src/kysely-feature-flag-store.js +239 -0
- package/dist/src/kysely-scope-service.d.ts +1 -0
- package/dist/src/kysely-scope-service.js +29 -0
- package/dist/src/kysely-tables.d.ts +45 -0
- package/dist/src/schema/analytics.schema.d.ts +17 -0
- package/dist/src/schema/analytics.schema.js +48 -0
- package/dist/src/schema/flag.schema.d.ts +16 -0
- package/dist/src/schema/flag.schema.js +62 -0
- package/dist/src/schema/index.d.ts +2 -0
- package/dist/src/schema/index.js +6 -0
- package/package.json +11 -6
- package/run-tests.sh +9 -1
- package/bin/pikku-kysely-pure.ts +0 -59
- package/dist/src/kysely-lock-vault.d.ts +0 -34
- package/dist/src/kysely-lock-vault.js +0 -77
- package/dist/src/schema/data-lock.schema.d.ts +0 -15
- package/dist/src/schema/data-lock.schema.js +0 -27
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/classification-crypto.test.ts +0 -143
- package/src/classification-crypto.ts +0 -163
- package/src/classification-plugin.pglite.test.ts +0 -226
- package/src/classification-plugin.test.ts +0 -265
- package/src/classification-plugin.ts +0 -249
- package/src/coercion-plugin.test.ts +0 -160
- package/src/coercion-plugin.ts +0 -187
- package/src/create-audited-kysely.test.ts +0 -560
- package/src/create-audited-kysely.ts +0 -254
- package/src/index.ts +0 -81
- package/src/kysely-agent-run-scores.ts +0 -47
- package/src/kysely-agent-run-service.ts +0 -244
- package/src/kysely-agent-run-state-service.ts +0 -200
- package/src/kysely-agent-storage-service.ts +0 -632
- package/src/kysely-audit-service.test.ts +0 -352
- package/src/kysely-audit-service.ts +0 -237
- package/src/kysely-channel-store.ts +0 -107
- package/src/kysely-credential-service.ts +0 -286
- package/src/kysely-deployment-service.ts +0 -163
- package/src/kysely-eventhub-store.ts +0 -53
- package/src/kysely-json.ts +0 -5
- package/src/kysely-scope-service.test.ts +0 -418
- package/src/kysely-scope-service.ts +0 -425
- package/src/kysely-secret-service.ts +0 -242
- package/src/kysely-services.test.ts +0 -405
- package/src/kysely-session-store.ts +0 -57
- package/src/kysely-tables.test.ts +0 -110
- package/src/kysely-tables.ts +0 -434
- package/src/kysely-virtual-user-run-store.test.ts +0 -369
- package/src/kysely-virtual-user-run-store.ts +0 -235
- package/src/kysely-virtual-user-schedule-store.test.ts +0 -187
- package/src/kysely-virtual-user-schedule-store.ts +0 -187
- package/src/kysely-webhook-service.test.ts +0 -153
- package/src/kysely-webhook-service.ts +0 -139
- package/src/kysely-workflow-mirror.test.ts +0 -259
- package/src/kysely-workflow-mirror.ts +0 -374
- package/src/kysely-workflow-run-service.ts +0 -305
- package/src/kysely-workflow-service.test.ts +0 -544
- package/src/kysely-workflow-service.ts +0 -820
- package/src/schema/agent.schema.ts +0 -162
- package/src/schema/audit.schema.ts +0 -57
- package/src/schema/channel.schema.ts +0 -38
- package/src/schema/credential.schema.ts +0 -58
- package/src/schema/deployment.schema.ts +0 -47
- package/src/schema/index.ts +0 -441
- package/src/schema/pikku-schema.types.ts +0 -169
- package/src/schema/schema.test.ts +0 -591
- package/src/schema/scope.schema.ts +0 -110
- package/src/schema/secret.schema.ts +0 -49
- package/src/schema/session.schema.ts +0 -20
- package/src/schema/virtual-user-schedule.schema.ts +0 -48
- package/src/schema/virtual-user.schema.ts +0 -106
- package/src/schema/webhook.schema.ts +0 -58
- package/src/schema/workflow.schema.ts +0 -150
- package/src/serialize-plugin.ts +0 -233
- package/tsconfig.json +0 -17
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import { sql } from 'kysely'
|
|
2
|
-
import type { Kysely } from 'kysely'
|
|
3
|
-
import type { KyselyPikkuDB } from './kysely-tables.js'
|
|
4
|
-
import type { AgentRunState } from '@pikku/core/agent'
|
|
5
|
-
import type { AgentRunScore } from '@pikku/core/agent-scorer'
|
|
6
|
-
import type {
|
|
7
|
-
AgentRunStateService,
|
|
8
|
-
CreateRunInput,
|
|
9
|
-
SaveScoreInput,
|
|
10
|
-
} from '@pikku/core/services'
|
|
11
|
-
import type { PendingApproval } from '@pikku/core/agent'
|
|
12
|
-
import { requirePikkuSchema } from './schema/index.js'
|
|
13
|
-
import { agentSchema } from './schema/agent.schema.js'
|
|
14
|
-
import { getRunScores, saveRunScore } from './kysely-agent-run-scores.js'
|
|
15
|
-
|
|
16
|
-
export class KyselyAgentRunStateService implements AgentRunStateService {
|
|
17
|
-
private initialized = false
|
|
18
|
-
|
|
19
|
-
constructor(private db: Kysely<KyselyPikkuDB>) {}
|
|
20
|
-
|
|
21
|
-
async init(): Promise<void> {
|
|
22
|
-
if (this.initialized) return
|
|
23
|
-
await requirePikkuSchema(this.db, agentSchema)
|
|
24
|
-
this.initialized = true
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async createRun(run: CreateRunInput): Promise<string> {
|
|
28
|
-
const runId = `run-${crypto.randomUUID()}`
|
|
29
|
-
await this.db
|
|
30
|
-
.insertInto('agentRun')
|
|
31
|
-
.values({
|
|
32
|
-
runId,
|
|
33
|
-
agentName: run.agentName,
|
|
34
|
-
threadId: run.threadId,
|
|
35
|
-
resourceId: run.resourceId,
|
|
36
|
-
status: run.status ?? 'running',
|
|
37
|
-
errorMessage: run.errorMessage ?? null,
|
|
38
|
-
suspendReason: run.suspendReason ?? null,
|
|
39
|
-
missingRpcs: run.missingRpcs ? JSON.stringify(run.missingRpcs) : null,
|
|
40
|
-
usageInputTokens: run.usage?.inputTokens ?? 0,
|
|
41
|
-
usageOutputTokens: run.usage?.outputTokens ?? 0,
|
|
42
|
-
usageModel: run.usage?.model ?? '',
|
|
43
|
-
})
|
|
44
|
-
.execute()
|
|
45
|
-
return runId
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async updateRun(
|
|
49
|
-
runId: string,
|
|
50
|
-
updates: Partial<AgentRunState>
|
|
51
|
-
): Promise<void> {
|
|
52
|
-
const values: Record<string, unknown> = {
|
|
53
|
-
updatedAt: sql`CURRENT_TIMESTAMP`,
|
|
54
|
-
}
|
|
55
|
-
if (updates.status !== undefined) values.status = updates.status
|
|
56
|
-
if (updates.errorMessage !== undefined)
|
|
57
|
-
values.errorMessage = updates.errorMessage
|
|
58
|
-
if (updates.suspendReason !== undefined)
|
|
59
|
-
values.suspendReason = updates.suspendReason
|
|
60
|
-
if (updates.missingRpcs !== undefined)
|
|
61
|
-
values.missingRpcs = JSON.stringify(updates.missingRpcs)
|
|
62
|
-
if (updates.pendingApprovals !== undefined)
|
|
63
|
-
values.pendingApprovals = JSON.stringify(updates.pendingApprovals)
|
|
64
|
-
if (updates.usage) {
|
|
65
|
-
values.usageInputTokens = updates.usage.inputTokens
|
|
66
|
-
values.usageOutputTokens = updates.usage.outputTokens
|
|
67
|
-
values.usageModel = updates.usage.model
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
await this.db
|
|
71
|
-
.updateTable('agentRun')
|
|
72
|
-
.set(values)
|
|
73
|
-
.where('runId', '=', runId)
|
|
74
|
-
.execute()
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async getRun(runId: string): Promise<AgentRunState | null> {
|
|
78
|
-
const row = await this.db
|
|
79
|
-
.selectFrom('agentRun')
|
|
80
|
-
.selectAll()
|
|
81
|
-
.where('runId', '=', runId)
|
|
82
|
-
.executeTakeFirst()
|
|
83
|
-
return row ? this.toRunState(row) : null
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async getRunsByThread(threadId: string): Promise<AgentRunState[]> {
|
|
87
|
-
const rows = await this.db
|
|
88
|
-
.selectFrom('agentRun')
|
|
89
|
-
.selectAll()
|
|
90
|
-
.where('threadId', '=', threadId)
|
|
91
|
-
.orderBy('createdAt', 'desc')
|
|
92
|
-
.execute()
|
|
93
|
-
return rows.map((r) => this.toRunState(r))
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async resolveApproval(
|
|
97
|
-
toolCallId: string,
|
|
98
|
-
status: 'approved' | 'denied'
|
|
99
|
-
): Promise<boolean> {
|
|
100
|
-
const rows = await this.db
|
|
101
|
-
.selectFrom('agentRun')
|
|
102
|
-
.select(['runId', 'pendingApprovals'])
|
|
103
|
-
.where('status', '=', 'suspended')
|
|
104
|
-
.execute()
|
|
105
|
-
|
|
106
|
-
for (const row of rows) {
|
|
107
|
-
let approvals: PendingApproval[] = []
|
|
108
|
-
if (row.pendingApprovals) {
|
|
109
|
-
try {
|
|
110
|
-
approvals = JSON.parse(row.pendingApprovals)
|
|
111
|
-
} catch {
|
|
112
|
-
console.warn(
|
|
113
|
-
`Failed to parse pendingApprovals for run ${row.runId}, treating as empty`
|
|
114
|
-
)
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
const filtered = approvals.filter((a) => a.toolCallId !== toolCallId)
|
|
118
|
-
if (filtered.length !== approvals.length) {
|
|
119
|
-
const updates: Record<string, unknown> = {
|
|
120
|
-
pendingApprovals:
|
|
121
|
-
filtered.length > 0 ? JSON.stringify(filtered) : null,
|
|
122
|
-
updatedAt: sql`CURRENT_TIMESTAMP`,
|
|
123
|
-
}
|
|
124
|
-
if (filtered.length === 0) {
|
|
125
|
-
updates.status = status
|
|
126
|
-
}
|
|
127
|
-
// Compare-and-swap on the list we read: a concurrent approver that got
|
|
128
|
-
// there first has already rewritten it, and this update matches no row.
|
|
129
|
-
const result = await this.db
|
|
130
|
-
.updateTable('agentRun')
|
|
131
|
-
.set(updates as any)
|
|
132
|
-
.where('runId', '=', row.runId)
|
|
133
|
-
.where('status', '=', 'suspended')
|
|
134
|
-
.where('pendingApprovals', '=', row.pendingApprovals)
|
|
135
|
-
.executeTakeFirst()
|
|
136
|
-
return Number(result?.numUpdatedRows ?? 0) > 0
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
return false
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async findRunByToolCallId(
|
|
143
|
-
toolCallId: string
|
|
144
|
-
): Promise<{ run: AgentRunState; approval: PendingApproval } | null> {
|
|
145
|
-
const rows = await this.db
|
|
146
|
-
.selectFrom('agentRun')
|
|
147
|
-
.selectAll()
|
|
148
|
-
.where('status', '=', 'suspended')
|
|
149
|
-
.execute()
|
|
150
|
-
|
|
151
|
-
for (const row of rows) {
|
|
152
|
-
let approvals: PendingApproval[] = []
|
|
153
|
-
if (row.pendingApprovals) {
|
|
154
|
-
try {
|
|
155
|
-
approvals = JSON.parse(row.pendingApprovals)
|
|
156
|
-
} catch {
|
|
157
|
-
console.warn(
|
|
158
|
-
`Failed to parse pendingApprovals for run ${row.runId}, treating as empty`
|
|
159
|
-
)
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
const approval = approvals.find((a) => a.toolCallId === toolCallId)
|
|
163
|
-
if (approval) {
|
|
164
|
-
return { run: this.toRunState(row), approval }
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
return null
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
async saveScore(score: SaveScoreInput): Promise<void> {
|
|
171
|
-
await saveRunScore(this.db, score)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
async getScores(runId: string): Promise<AgentRunScore[]> {
|
|
175
|
-
return getRunScores(this.db, runId)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
private toRunState(row: any): AgentRunState {
|
|
179
|
-
return {
|
|
180
|
-
runId: row.runId,
|
|
181
|
-
agentName: row.agentName,
|
|
182
|
-
threadId: row.threadId,
|
|
183
|
-
resourceId: row.resourceId,
|
|
184
|
-
status: row.status,
|
|
185
|
-
errorMessage: row.errorMessage ?? undefined,
|
|
186
|
-
suspendReason: row.suspendReason ?? undefined,
|
|
187
|
-
missingRpcs: row.missingRpcs ? JSON.parse(row.missingRpcs) : undefined,
|
|
188
|
-
pendingApprovals: row.pendingApprovals
|
|
189
|
-
? JSON.parse(row.pendingApprovals)
|
|
190
|
-
: undefined,
|
|
191
|
-
usage: {
|
|
192
|
-
inputTokens: row.usageInputTokens ?? 0,
|
|
193
|
-
outputTokens: row.usageOutputTokens ?? 0,
|
|
194
|
-
model: row.usageModel ?? '',
|
|
195
|
-
},
|
|
196
|
-
createdAt: new Date(row.createdAt),
|
|
197
|
-
updatedAt: new Date(row.updatedAt),
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|