@sixb/sqlite 0.1.1 → 0.1.3
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/README.md +18 -13
- package/dist/action-run-storage.d.ts +5 -2
- package/dist/action-run-storage.d.ts.map +1 -1
- package/dist/agents/agent-storage.d.ts +9 -5
- package/dist/agents/agent-storage.d.ts.map +1 -1
- package/dist/agents/checkpoints.d.ts +14 -0
- package/dist/agents/checkpoints.d.ts.map +1 -0
- package/dist/agents/index.d.ts +1 -0
- package/dist/agents/index.d.ts.map +1 -1
- package/dist/agents/messages.d.ts +2 -1
- package/dist/agents/messages.d.ts.map +1 -1
- package/dist/agents/rows.d.ts +21 -11
- package/dist/agents/rows.d.ts.map +1 -1
- package/dist/agents/runs.d.ts +3 -2
- package/dist/agents/runs.d.ts.map +1 -1
- package/dist/ai-cost-storage.d.ts +15 -0
- package/dist/ai-cost-storage.d.ts.map +1 -0
- package/dist/ai-usage-storage.d.ts +22 -0
- package/dist/ai-usage-storage.d.ts.map +1 -0
- package/dist/auth-storage/service-accounts.d.ts.map +1 -1
- package/dist/auth-storage/shared.d.ts +1 -10
- package/dist/auth-storage/shared.d.ts.map +1 -1
- package/dist/connector-connection-storage.d.ts +9 -0
- package/dist/connector-connection-storage.d.ts.map +1 -0
- package/dist/execution-storage.d.ts +14 -0
- package/dist/execution-storage.d.ts.map +1 -0
- package/dist/index.d.ts +14 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4633 -1066
- package/dist/index.js.map +33 -28
- package/dist/latest-run-query.d.ts.map +1 -1
- package/dist/migrations.d.ts +0 -1
- package/dist/migrations.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-session.d.ts +50 -8
- package/dist/ontology-storage/materialization-session.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-state.d.ts +4 -5
- package/dist/ontology-storage/materialization-state.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
- package/dist/ontology-storage/materializations.d.ts +0 -2
- package/dist/ontology-storage/materializations.d.ts.map +1 -1
- package/dist/ontology-storage/outbox.d.ts.map +1 -1
- package/dist/ontology-storage/shared.d.ts +3 -3
- package/dist/ontology-storage/shared.d.ts.map +1 -1
- package/dist/ontology-storage/sources.d.ts.map +1 -1
- package/dist/pipeline-run-storage.d.ts +7 -2
- package/dist/pipeline-run-storage.d.ts.map +1 -1
- package/dist/projection-run-storage.d.ts +9 -2
- package/dist/projection-run-storage.d.ts.map +1 -1
- package/dist/run-list-query.d.ts +1 -1
- package/dist/run-list-query.d.ts.map +1 -1
- package/dist/sync-run-storage.d.ts +7 -2
- package/dist/sync-run-storage.d.ts.map +1 -1
- package/dist/testing.d.ts +6 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/transactions.d.ts +3 -0
- package/dist/transactions.d.ts.map +1 -1
- package/dist/webhook-run-storage.d.ts +9 -2
- package/dist/webhook-run-storage.d.ts.map +1 -1
- package/dist/workflow-run-storage.d.ts +7 -3
- package/dist/workflow-run-storage.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/action-run-storage.ts +51 -74
- package/src/agents/agent-storage.ts +11 -6
- package/src/agents/checkpoints.ts +175 -0
- package/src/agents/index.ts +1 -0
- package/src/agents/messages.ts +59 -0
- package/src/agents/rows.ts +52 -40
- package/src/agents/runs.ts +27 -21
- package/src/ai-cost-storage.ts +744 -0
- package/src/ai-usage-storage.ts +324 -0
- package/src/auth-storage/service-accounts.ts +14 -21
- package/src/auth-storage/shared.ts +0 -22
- package/src/connector-connection-storage.ts +784 -0
- package/src/execution-storage.ts +158 -0
- package/src/index.ts +69 -22
- package/src/latest-run-query.ts +7 -1
- package/src/migrations/004-executions.sql +142 -0
- package/src/migrations/005-workflow-executions.sql +7 -0
- package/src/migrations/006-narrow-ontology-source-root-index.sql +4 -0
- package/src/migrations/007-split-overrides.sql +154 -0
- package/src/migrations/008-action-executions.sql +4 -0
- package/src/migrations/009-agent-executions.sql +115 -0
- package/src/migrations/010-ai-usage-accounting-foundation.sql +74 -0
- package/src/migrations/011-sync-failure-record.sql +22 -0
- package/src/migrations/012-pipeline-failure-record.sql +45 -0
- package/src/migrations/013-workflow-failure-record.sql +45 -0
- package/src/migrations/014-agent-failure-record.sql +59 -0
- package/src/migrations/015-projection-failure-record.sql +20 -0
- package/src/migrations/016-webhook-run-failure-record.sql +19 -0
- package/src/migrations/017-action-failure-record.sql +65 -0
- package/src/migrations/018-ontology-outbox-failure-record.sql +80 -0
- package/src/migrations/019-webhook-delivery-failure-record.sql +62 -0
- package/src/migrations/020-drop-run-usage-projections.sql +7 -0
- package/src/migrations/021-sync-pipeline-executions.sql +101 -0
- package/src/migrations/022-projection-executions.sql +244 -0
- package/src/migrations/023-webhook-executions.sql +51 -0
- package/src/migrations/024-ontology-commit-executions.sql +58 -0
- package/src/migrations/025-connector-connections.sql +257 -0
- package/src/migrations/026-ai-cost-accounting.sql +57 -0
- package/src/migrations/027-agent-context-checkpoints.sql +28 -0
- package/src/migrations/028-object-override-edit-times.sql +3 -0
- package/src/migrations.ts +129 -4
- package/src/ontology-storage/materialization-session.ts +317 -143
- package/src/ontology-storage/materialization-state.ts +323 -112
- package/src/ontology-storage/materialization-writer.ts +145 -67
- package/src/ontology-storage/materializations.ts +67 -141
- package/src/ontology-storage/outbox.ts +10 -3
- package/src/ontology-storage/shared.ts +10 -4
- package/src/ontology-storage/sources.ts +4 -1
- package/src/pipeline-run-storage.ts +128 -43
- package/src/projection-run-storage.ts +209 -97
- package/src/run-list-query.ts +9 -4
- package/src/sync-run-storage.ts +118 -32
- package/src/testing.ts +20 -0
- package/src/transactions.ts +10 -3
- package/src/webhook-run-storage.ts +118 -74
- package/src/workflow-run-storage.ts +117 -131
- package/dist/validation.d.ts +0 -5
- package/dist/validation.d.ts.map +0 -1
- package/dist/webhook-delivery-storage.d.ts +0 -27
- package/dist/webhook-delivery-storage.d.ts.map +0 -1
- package/src/validation.ts +0 -30
- package/src/webhook-delivery-storage.ts +0 -272
|
@@ -1,272 +0,0 @@
|
|
|
1
|
-
import type { Database } from "bun:sqlite"
|
|
2
|
-
import type {
|
|
3
|
-
WebhookDeliveryClaimRecord,
|
|
4
|
-
WebhookDeliveryClaimResult,
|
|
5
|
-
WebhookDeliveryKey,
|
|
6
|
-
WebhookDeliveryRecord,
|
|
7
|
-
WebhookDeliveryStorage,
|
|
8
|
-
} from "@sixb/core/storage"
|
|
9
|
-
import { installFreshSqliteSchema } from "./migrations"
|
|
10
|
-
import {
|
|
11
|
-
closeSqliteStoreConnection,
|
|
12
|
-
openSqliteStoreConnection,
|
|
13
|
-
type SqliteStoreConnection,
|
|
14
|
-
} from "./transactions"
|
|
15
|
-
|
|
16
|
-
export interface SqliteWebhookDeliveryStorageOptions {
|
|
17
|
-
/** Path to SQLite database file. Defaults to ':memory:' for in-memory database. */
|
|
18
|
-
path?: string
|
|
19
|
-
/** Internal shared connection used by bundled SqliteStorage. */
|
|
20
|
-
connection?: SqliteStoreConnection
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class SqliteWebhookDeliveryStorage implements WebhookDeliveryStorage {
|
|
24
|
-
private readonly connection: SqliteStoreConnection
|
|
25
|
-
private readonly db: Database
|
|
26
|
-
|
|
27
|
-
constructor(options: SqliteWebhookDeliveryStorageOptions = {}) {
|
|
28
|
-
this.connection = openSqliteStoreConnection(options)
|
|
29
|
-
this.db = this.connection.db
|
|
30
|
-
|
|
31
|
-
if (this.connection.installFreshSchema) {
|
|
32
|
-
installFreshSqliteSchema(this.db)
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async claim(
|
|
37
|
-
input: WebhookDeliveryKey & { receivedAt: string }
|
|
38
|
-
): Promise<WebhookDeliveryClaimRecord> {
|
|
39
|
-
return this.db.transaction(() => {
|
|
40
|
-
// Atomic first claim: only one request can insert this scoped delivery key.
|
|
41
|
-
const inserted = this.db
|
|
42
|
-
.query(
|
|
43
|
-
`
|
|
44
|
-
INSERT OR IGNORE INTO webhook_deliveries (
|
|
45
|
-
project_id,
|
|
46
|
-
connector_id,
|
|
47
|
-
webhook_id,
|
|
48
|
-
idempotency_key,
|
|
49
|
-
status,
|
|
50
|
-
received_at
|
|
51
|
-
) VALUES (?, ?, ?, ?, 'in_progress', ?)
|
|
52
|
-
`
|
|
53
|
-
)
|
|
54
|
-
.run(
|
|
55
|
-
input.projectId,
|
|
56
|
-
input.connectorId,
|
|
57
|
-
input.webhookId,
|
|
58
|
-
input.idempotencyKey,
|
|
59
|
-
input.receivedAt
|
|
60
|
-
) as { changes: number }
|
|
61
|
-
|
|
62
|
-
if (inserted.changes > 0) {
|
|
63
|
-
return toWebhookDeliveryClaimRecord(
|
|
64
|
-
{
|
|
65
|
-
projectId: input.projectId,
|
|
66
|
-
connectorId: input.connectorId,
|
|
67
|
-
webhookId: input.webhookId,
|
|
68
|
-
idempotencyKey: input.idempotencyKey,
|
|
69
|
-
status: "in_progress",
|
|
70
|
-
receivedAt: input.receivedAt,
|
|
71
|
-
},
|
|
72
|
-
"claimed"
|
|
73
|
-
)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const existing = this.getDeliveryRecord(input)
|
|
77
|
-
|
|
78
|
-
if (existing?.status === "completed") {
|
|
79
|
-
return toWebhookDeliveryClaimRecord(existing, "duplicate")
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (existing?.status === "in_progress") {
|
|
83
|
-
return toWebhookDeliveryClaimRecord(existing, "in_progress")
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (existing?.status !== "failed") {
|
|
87
|
-
throw new Error("[SixbSqlite] Webhook delivery row disappeared during claim.")
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Failed deliveries are intentionally claimable so provider retries can run again.
|
|
91
|
-
const claimed = this.db
|
|
92
|
-
.query(
|
|
93
|
-
`
|
|
94
|
-
UPDATE webhook_deliveries
|
|
95
|
-
SET status = 'in_progress',
|
|
96
|
-
received_at = ?,
|
|
97
|
-
completed_at = NULL,
|
|
98
|
-
failed_at = NULL,
|
|
99
|
-
error = NULL
|
|
100
|
-
WHERE project_id = ?
|
|
101
|
-
AND connector_id = ?
|
|
102
|
-
AND webhook_id = ?
|
|
103
|
-
AND idempotency_key = ?
|
|
104
|
-
AND status = 'failed'
|
|
105
|
-
`
|
|
106
|
-
)
|
|
107
|
-
.run(
|
|
108
|
-
input.receivedAt,
|
|
109
|
-
input.projectId,
|
|
110
|
-
input.connectorId,
|
|
111
|
-
input.webhookId,
|
|
112
|
-
input.idempotencyKey
|
|
113
|
-
) as { changes: number }
|
|
114
|
-
|
|
115
|
-
if (claimed.changes > 0) {
|
|
116
|
-
return toWebhookDeliveryClaimRecord(this.requireDeliveryRecord(input, "claim"), "claimed")
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const current = this.requireDeliveryRecord(input, "claim")
|
|
120
|
-
return toWebhookDeliveryClaimRecord(
|
|
121
|
-
current,
|
|
122
|
-
current.status === "completed" ? "duplicate" : "in_progress"
|
|
123
|
-
)
|
|
124
|
-
})()
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
async complete(
|
|
128
|
-
input: WebhookDeliveryKey & { completedAt: string }
|
|
129
|
-
): Promise<WebhookDeliveryRecord> {
|
|
130
|
-
this.db
|
|
131
|
-
.query(
|
|
132
|
-
`
|
|
133
|
-
UPDATE webhook_deliveries
|
|
134
|
-
SET status = 'completed',
|
|
135
|
-
completed_at = ?,
|
|
136
|
-
failed_at = NULL,
|
|
137
|
-
error = NULL
|
|
138
|
-
WHERE project_id = ?
|
|
139
|
-
AND connector_id = ?
|
|
140
|
-
AND webhook_id = ?
|
|
141
|
-
AND idempotency_key = ?
|
|
142
|
-
`
|
|
143
|
-
)
|
|
144
|
-
.run(
|
|
145
|
-
input.completedAt,
|
|
146
|
-
input.projectId,
|
|
147
|
-
input.connectorId,
|
|
148
|
-
input.webhookId,
|
|
149
|
-
input.idempotencyKey
|
|
150
|
-
)
|
|
151
|
-
|
|
152
|
-
return this.requireDeliveryRecord(input, "complete")
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
async fail(
|
|
156
|
-
input: WebhookDeliveryKey & { failedAt: string; error: string }
|
|
157
|
-
): Promise<WebhookDeliveryRecord> {
|
|
158
|
-
this.db
|
|
159
|
-
.query(
|
|
160
|
-
`
|
|
161
|
-
UPDATE webhook_deliveries
|
|
162
|
-
SET status = 'failed',
|
|
163
|
-
failed_at = ?,
|
|
164
|
-
error = ?
|
|
165
|
-
WHERE project_id = ?
|
|
166
|
-
AND connector_id = ?
|
|
167
|
-
AND webhook_id = ?
|
|
168
|
-
AND idempotency_key = ?
|
|
169
|
-
`
|
|
170
|
-
)
|
|
171
|
-
.run(
|
|
172
|
-
input.failedAt,
|
|
173
|
-
input.error,
|
|
174
|
-
input.projectId,
|
|
175
|
-
input.connectorId,
|
|
176
|
-
input.webhookId,
|
|
177
|
-
input.idempotencyKey
|
|
178
|
-
)
|
|
179
|
-
|
|
180
|
-
return this.requireDeliveryRecord(input, "fail")
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
close(): void {
|
|
184
|
-
closeSqliteStoreConnection(this.connection)
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
private getDeliveryRecord(key: WebhookDeliveryKey): WebhookDeliveryRecord | null {
|
|
188
|
-
const row = this.db
|
|
189
|
-
.query(
|
|
190
|
-
`
|
|
191
|
-
SELECT
|
|
192
|
-
project_id,
|
|
193
|
-
connector_id,
|
|
194
|
-
webhook_id,
|
|
195
|
-
idempotency_key,
|
|
196
|
-
status,
|
|
197
|
-
received_at,
|
|
198
|
-
completed_at,
|
|
199
|
-
failed_at,
|
|
200
|
-
error
|
|
201
|
-
FROM webhook_deliveries
|
|
202
|
-
WHERE project_id = ?
|
|
203
|
-
AND connector_id = ?
|
|
204
|
-
AND webhook_id = ?
|
|
205
|
-
AND idempotency_key = ?
|
|
206
|
-
`
|
|
207
|
-
)
|
|
208
|
-
.get(
|
|
209
|
-
key.projectId,
|
|
210
|
-
key.connectorId,
|
|
211
|
-
key.webhookId,
|
|
212
|
-
key.idempotencyKey
|
|
213
|
-
) as SqliteWebhookDeliveryRow | null
|
|
214
|
-
|
|
215
|
-
return row ? toWebhookDeliveryRecord(row) : null
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
private requireDeliveryRecord(
|
|
219
|
-
key: WebhookDeliveryKey,
|
|
220
|
-
action: "claim" | "complete" | "fail"
|
|
221
|
-
): WebhookDeliveryRecord {
|
|
222
|
-
const record = this.getDeliveryRecord(key)
|
|
223
|
-
|
|
224
|
-
if (!record) {
|
|
225
|
-
throw new Error(
|
|
226
|
-
`[SixbSqlite] Missing webhook delivery row after ${action} for ${serializeKey(key)}.`
|
|
227
|
-
)
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return record
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
interface SqliteWebhookDeliveryRow {
|
|
235
|
-
readonly project_id: string
|
|
236
|
-
readonly connector_id: string
|
|
237
|
-
readonly webhook_id: string
|
|
238
|
-
readonly idempotency_key: string
|
|
239
|
-
readonly status: WebhookDeliveryRecord["status"]
|
|
240
|
-
readonly received_at: string
|
|
241
|
-
readonly completed_at: string | null
|
|
242
|
-
readonly failed_at: string | null
|
|
243
|
-
readonly error: string | null
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
function toWebhookDeliveryRecord(row: SqliteWebhookDeliveryRow): WebhookDeliveryRecord {
|
|
247
|
-
return {
|
|
248
|
-
projectId: row.project_id,
|
|
249
|
-
connectorId: row.connector_id,
|
|
250
|
-
webhookId: row.webhook_id,
|
|
251
|
-
idempotencyKey: row.idempotency_key,
|
|
252
|
-
status: row.status,
|
|
253
|
-
receivedAt: row.received_at,
|
|
254
|
-
completedAt: row.completed_at ?? undefined,
|
|
255
|
-
failedAt: row.failed_at ?? undefined,
|
|
256
|
-
error: row.error ?? undefined,
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
function toWebhookDeliveryClaimRecord(
|
|
261
|
-
record: WebhookDeliveryRecord,
|
|
262
|
-
claimResult: WebhookDeliveryClaimResult
|
|
263
|
-
): WebhookDeliveryClaimRecord {
|
|
264
|
-
return {
|
|
265
|
-
...record,
|
|
266
|
-
claimResult,
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
function serializeKey(key: WebhookDeliveryKey): string {
|
|
271
|
-
return JSON.stringify([key.projectId, key.connectorId, key.webhookId, key.idempotencyKey])
|
|
272
|
-
}
|