@sixb/sqlite 0.1.0 → 0.1.2

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 (110) hide show
  1. package/README.md +18 -13
  2. package/dist/action-run-storage.d.ts +5 -2
  3. package/dist/action-run-storage.d.ts.map +1 -1
  4. package/dist/agents/agent-storage.d.ts +4 -2
  5. package/dist/agents/agent-storage.d.ts.map +1 -1
  6. package/dist/agents/rows.d.ts +3 -10
  7. package/dist/agents/rows.d.ts.map +1 -1
  8. package/dist/agents/runs.d.ts +3 -2
  9. package/dist/agents/runs.d.ts.map +1 -1
  10. package/dist/ai-usage-storage.d.ts +22 -0
  11. package/dist/ai-usage-storage.d.ts.map +1 -0
  12. package/dist/auth-storage/service-accounts.d.ts.map +1 -1
  13. package/dist/auth-storage/shared.d.ts +1 -10
  14. package/dist/auth-storage/shared.d.ts.map +1 -1
  15. package/dist/connector-connection-storage.d.ts +9 -0
  16. package/dist/connector-connection-storage.d.ts.map +1 -0
  17. package/dist/execution-storage.d.ts +14 -0
  18. package/dist/execution-storage.d.ts.map +1 -0
  19. package/dist/index.d.ts +13 -9
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +4114 -1149
  22. package/dist/index.js.map +30 -27
  23. package/dist/latest-run-query.d.ts.map +1 -1
  24. package/dist/migrations.d.ts +0 -1
  25. package/dist/migrations.d.ts.map +1 -1
  26. package/dist/ontology-storage/materialization-session.d.ts +50 -8
  27. package/dist/ontology-storage/materialization-session.d.ts.map +1 -1
  28. package/dist/ontology-storage/materialization-state.d.ts +4 -5
  29. package/dist/ontology-storage/materialization-state.d.ts.map +1 -1
  30. package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
  31. package/dist/ontology-storage/materializations.d.ts +0 -2
  32. package/dist/ontology-storage/materializations.d.ts.map +1 -1
  33. package/dist/ontology-storage/outbox.d.ts.map +1 -1
  34. package/dist/ontology-storage/shared.d.ts +3 -3
  35. package/dist/ontology-storage/shared.d.ts.map +1 -1
  36. package/dist/ontology-storage/sources.d.ts.map +1 -1
  37. package/dist/pipeline-run-storage.d.ts +7 -2
  38. package/dist/pipeline-run-storage.d.ts.map +1 -1
  39. package/dist/projection-run-storage.d.ts +9 -2
  40. package/dist/projection-run-storage.d.ts.map +1 -1
  41. package/dist/run-list-query.d.ts +1 -1
  42. package/dist/run-list-query.d.ts.map +1 -1
  43. package/dist/sync-run-storage.d.ts +7 -2
  44. package/dist/sync-run-storage.d.ts.map +1 -1
  45. package/dist/testing.d.ts +6 -0
  46. package/dist/testing.d.ts.map +1 -0
  47. package/dist/transactions.d.ts +3 -0
  48. package/dist/transactions.d.ts.map +1 -1
  49. package/dist/webhook-run-storage.d.ts +9 -2
  50. package/dist/webhook-run-storage.d.ts.map +1 -1
  51. package/dist/workflow-run-storage.d.ts +7 -3
  52. package/dist/workflow-run-storage.d.ts.map +1 -1
  53. package/package.json +4 -3
  54. package/src/action-run-storage.ts +51 -74
  55. package/src/agents/agent-storage.ts +5 -3
  56. package/src/agents/rows.ts +8 -40
  57. package/src/agents/runs.ts +27 -21
  58. package/src/ai-usage-storage.ts +324 -0
  59. package/src/auth-storage/service-accounts.ts +14 -21
  60. package/src/auth-storage/shared.ts +0 -22
  61. package/src/connector-connection-storage.ts +784 -0
  62. package/src/execution-storage.ts +158 -0
  63. package/src/index.ts +63 -22
  64. package/src/latest-run-query.ts +7 -1
  65. package/src/migrations/002-workflow-run-output.sql +27 -0
  66. package/src/migrations/003-merge-sync-runs.sql +71 -0
  67. package/src/migrations/004-executions.sql +142 -0
  68. package/src/migrations/005-workflow-executions.sql +7 -0
  69. package/src/migrations/006-narrow-ontology-source-root-index.sql +4 -0
  70. package/src/migrations/007-split-overrides.sql +154 -0
  71. package/src/migrations/008-action-executions.sql +4 -0
  72. package/src/migrations/009-agent-executions.sql +115 -0
  73. package/src/migrations/010-ai-usage-accounting-foundation.sql +74 -0
  74. package/src/migrations/011-sync-failure-record.sql +22 -0
  75. package/src/migrations/012-pipeline-failure-record.sql +45 -0
  76. package/src/migrations/013-workflow-failure-record.sql +45 -0
  77. package/src/migrations/014-agent-failure-record.sql +59 -0
  78. package/src/migrations/015-projection-failure-record.sql +20 -0
  79. package/src/migrations/016-webhook-run-failure-record.sql +19 -0
  80. package/src/migrations/017-action-failure-record.sql +65 -0
  81. package/src/migrations/018-ontology-outbox-failure-record.sql +80 -0
  82. package/src/migrations/019-webhook-delivery-failure-record.sql +62 -0
  83. package/src/migrations/020-drop-run-usage-projections.sql +7 -0
  84. package/src/migrations/021-sync-pipeline-executions.sql +101 -0
  85. package/src/migrations/022-projection-executions.sql +244 -0
  86. package/src/migrations/023-webhook-executions.sql +51 -0
  87. package/src/migrations/024-ontology-commit-executions.sql +58 -0
  88. package/src/migrations/025-connector-connections.sql +257 -0
  89. package/src/migrations.ts +126 -5
  90. package/src/ontology-storage/materialization-session.ts +317 -143
  91. package/src/ontology-storage/materialization-state.ts +398 -150
  92. package/src/ontology-storage/materialization-writer.ts +142 -67
  93. package/src/ontology-storage/materializations.ts +67 -141
  94. package/src/ontology-storage/outbox.ts +10 -3
  95. package/src/ontology-storage/shared.ts +10 -4
  96. package/src/ontology-storage/sources.ts +4 -1
  97. package/src/pipeline-run-storage.ts +128 -43
  98. package/src/projection-run-storage.ts +209 -97
  99. package/src/run-list-query.ts +9 -4
  100. package/src/sync-run-storage.ts +120 -34
  101. package/src/testing.ts +20 -0
  102. package/src/transactions.ts +10 -3
  103. package/src/webhook-run-storage.ts +118 -74
  104. package/src/workflow-run-storage.ts +124 -133
  105. package/dist/validation.d.ts +0 -5
  106. package/dist/validation.d.ts.map +0 -1
  107. package/dist/webhook-delivery-storage.d.ts +0 -27
  108. package/dist/webhook-delivery-storage.d.ts.map +0 -1
  109. package/src/validation.ts +0 -30
  110. 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
- }