mcp-scraper 0.40.1 → 0.40.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 (87) hide show
  1. package/README.md +1 -1
  2. package/dist/bin/api-server.cjs +60220 -0
  3. package/dist/bin/api-server.cjs.map +1 -0
  4. package/dist/bin/api-server.d.cts +1 -0
  5. package/dist/bin/api-server.d.ts +1 -0
  6. package/dist/bin/api-server.js +38 -0
  7. package/dist/bin/api-server.js.map +1 -0
  8. package/dist/bin/mcp-scraper-cli.cjs +2671 -0
  9. package/dist/bin/mcp-scraper-cli.cjs.map +1 -0
  10. package/dist/bin/mcp-scraper-cli.d.cts +1 -0
  11. package/dist/bin/mcp-scraper-cli.d.ts +1 -0
  12. package/dist/bin/mcp-scraper-cli.js +742 -0
  13. package/dist/bin/mcp-scraper-cli.js.map +1 -0
  14. package/dist/bin/mcp-scraper-install.cjs +129 -0
  15. package/dist/bin/mcp-scraper-install.cjs.map +1 -0
  16. package/dist/bin/mcp-scraper-install.d.cts +1 -0
  17. package/dist/bin/mcp-scraper-install.d.ts +1 -0
  18. package/dist/bin/mcp-scraper-install.js +27 -0
  19. package/dist/bin/mcp-scraper-install.js.map +1 -0
  20. package/dist/bin/mcp-stdio-server.cjs +12532 -0
  21. package/dist/bin/mcp-stdio-server.cjs.map +1 -0
  22. package/dist/bin/mcp-stdio-server.d.cts +1 -0
  23. package/dist/bin/mcp-stdio-server.d.ts +1 -0
  24. package/dist/bin/mcp-stdio-server.js +135 -0
  25. package/dist/bin/mcp-stdio-server.js.map +1 -0
  26. package/dist/bin/paa-harvest.cjs +3808 -0
  27. package/dist/bin/paa-harvest.cjs.map +1 -0
  28. package/dist/bin/paa-harvest.d.cts +1 -0
  29. package/dist/bin/paa-harvest.d.ts +1 -0
  30. package/dist/bin/paa-harvest.js +44 -0
  31. package/dist/bin/paa-harvest.js.map +1 -0
  32. package/dist/chunk-345BQXZH.js +712 -0
  33. package/dist/chunk-345BQXZH.js.map +1 -0
  34. package/dist/chunk-3PIWJS6Y.js +276 -0
  35. package/dist/chunk-3PIWJS6Y.js.map +1 -0
  36. package/dist/chunk-44HZLHDV.js +52 -0
  37. package/dist/chunk-44HZLHDV.js.map +1 -0
  38. package/dist/chunk-7XBBFBYY.js +3410 -0
  39. package/dist/chunk-7XBBFBYY.js.map +1 -0
  40. package/dist/chunk-CB5C3BPB.js +135 -0
  41. package/dist/chunk-CB5C3BPB.js.map +1 -0
  42. package/dist/chunk-FQI5PFE7.js +1866 -0
  43. package/dist/chunk-FQI5PFE7.js.map +1 -0
  44. package/dist/chunk-G3P3ZDB4.js +69 -0
  45. package/dist/chunk-G3P3ZDB4.js.map +1 -0
  46. package/dist/chunk-GUVKHCKE.js +3050 -0
  47. package/dist/chunk-GUVKHCKE.js.map +1 -0
  48. package/dist/chunk-K443GQY5.js +24 -0
  49. package/dist/chunk-K443GQY5.js.map +1 -0
  50. package/dist/chunk-LP6E462I.js +11555 -0
  51. package/dist/chunk-LP6E462I.js.map +1 -0
  52. package/dist/chunk-NVXNEOUQ.js +158 -0
  53. package/dist/chunk-NVXNEOUQ.js.map +1 -0
  54. package/dist/chunk-QZXKQB7Y.js +414 -0
  55. package/dist/chunk-QZXKQB7Y.js.map +1 -0
  56. package/dist/chunk-SBLGBZZB.js +647 -0
  57. package/dist/chunk-SBLGBZZB.js.map +1 -0
  58. package/dist/chunk-X2LKCX6H.js +499 -0
  59. package/dist/chunk-X2LKCX6H.js.map +1 -0
  60. package/dist/chunk-XELSA2MS.js +108 -0
  61. package/dist/chunk-XELSA2MS.js.map +1 -0
  62. package/dist/chunk-XORPNO3Z.js +684 -0
  63. package/dist/chunk-XORPNO3Z.js.map +1 -0
  64. package/dist/chunk-ZUJLSICT.js +7 -0
  65. package/dist/chunk-ZUJLSICT.js.map +1 -0
  66. package/dist/db-W3CP562I.js +239 -0
  67. package/dist/db-W3CP562I.js.map +1 -0
  68. package/dist/editorial-reading-room/assets/app.js +335 -0
  69. package/dist/editorial-reading-room/assets/index.html +131 -0
  70. package/dist/editorial-reading-room/assets/styles.css +1052 -0
  71. package/dist/extract-bundle-K4PG3RZJ.js +568 -0
  72. package/dist/extract-bundle-K4PG3RZJ.js.map +1 -0
  73. package/dist/index.cjs +4160 -0
  74. package/dist/index.cjs.map +1 -0
  75. package/dist/index.d.cts +413 -0
  76. package/dist/index.d.ts +413 -0
  77. package/dist/index.js +338 -0
  78. package/dist/index.js.map +1 -0
  79. package/dist/location-data-repository-RLQX6SNM.js +35 -0
  80. package/dist/location-data-repository-RLQX6SNM.js.map +1 -0
  81. package/dist/server-KM3CFWCF.js +34674 -0
  82. package/dist/server-KM3CFWCF.js.map +1 -0
  83. package/dist/site-extract-repository-2SMMFKKL.js +62 -0
  84. package/dist/site-extract-repository-2SMMFKKL.js.map +1 -0
  85. package/dist/worker-FXAGFYOE.js +142 -0
  86. package/dist/worker-FXAGFYOE.js.map +1 -0
  87. package/package.json +4 -2
@@ -0,0 +1,3050 @@
1
+ // src/api/db.ts
2
+ import { createClient } from "@libsql/client/http";
3
+ import { createHash, randomBytes, randomUUID, scryptSync, timingSafeEqual } from "crypto";
4
+ import { z } from "zod";
5
+
6
+ // src/api/credit-lot-integrity.ts
7
+ var CREDIT_LOTS_EPOCH = "2026-06-26 21:58:03";
8
+
9
+ // src/api/db.ts
10
+ var DB_URL = process.env.TURSO_DATABASE_URL ?? "file:./paa-api.db";
11
+ var DB_TOKEN = process.env.TURSO_AUTH_TOKEN;
12
+ var _db = null;
13
+ var _rateLimitSchemaReady = false;
14
+ var CORE_SCHEMA_VERSION = "2026-07-28.5";
15
+ var CORE_SCHEMA_TABLES = [
16
+ "admin_credit_adjustment_events",
17
+ "admin_credit_adjustments",
18
+ "billing_authorizations",
19
+ "billing_credits",
20
+ "billing_debits",
21
+ "billing_debit_allocations",
22
+ "billing_events",
23
+ "browser_profile_labels",
24
+ "connected_account_billing",
25
+ "concurrency_locks",
26
+ "credit_lot_expirations",
27
+ "credit_lots",
28
+ "credit_lot_repairs",
29
+ "free_credit_refreshes",
30
+ "harvest_attempts",
31
+ "inbox_messages",
32
+ "jobs",
33
+ "kpo_jobs",
34
+ "kpo_phase_log",
35
+ "ledger",
36
+ "oauth_clients",
37
+ "oauth_codes",
38
+ "oauth_tokens",
39
+ "page_snapshots",
40
+ "password_reset_tokens",
41
+ "rate_limits",
42
+ "request_events",
43
+ "site_audit_jobs",
44
+ "site_audit_phase_log",
45
+ "site_extract_jobs",
46
+ "site_extract_pages",
47
+ "stripe_events",
48
+ "users",
49
+ "workflow_artifacts",
50
+ "workflow_runs",
51
+ "workflow_schedules",
52
+ "workflow_webhook_deliveries",
53
+ "workshop_registrations"
54
+ ];
55
+ var CORE_SCHEMA_INDEXES = [
56
+ "admin_credit_adjustment_events_reference",
57
+ "admin_credit_adjustments_user_created",
58
+ "billing_authorizations_user_status",
59
+ "billing_credits_user_status",
60
+ "billing_debits_user_status",
61
+ "billing_events_authorization",
62
+ "billing_events_user_created_at",
63
+ "concurrency_locks_operation",
64
+ "concurrency_locks_user_status",
65
+ "credit_lots_user_active",
66
+ "credit_lot_repairs_user_applied",
67
+ "harvest_attempts_job_id",
68
+ "harvest_attempts_user_created_at",
69
+ "inbox_messages_user_sent_at",
70
+ "jobs_created_at_status_user",
71
+ "jobs_status",
72
+ "jobs_user_created_at_status",
73
+ "jobs_user_id",
74
+ "kpo_jobs_status",
75
+ "kpo_jobs_user_id",
76
+ "kpo_phase_log_job_id",
77
+ "ledger_stripe_pi_unique",
78
+ "ledger_user_id",
79
+ "oauth_tokens_identity",
80
+ "page_snapshots_user_checked_at",
81
+ "prt_user_id",
82
+ "request_events_user_created_at",
83
+ "site_audit_jobs_status",
84
+ "site_audit_jobs_user_id",
85
+ "site_audit_phase_log_job_id",
86
+ "site_extract_jobs_status",
87
+ "site_extract_jobs_idempotency",
88
+ "site_extract_jobs_user_id",
89
+ "site_extract_pages_job_id",
90
+ "workflow_artifacts_run",
91
+ "workflow_runs_idempotency",
92
+ "workflow_runs_schedule",
93
+ "workflow_runs_user_created",
94
+ "workflow_schedules_due",
95
+ "workflow_schedules_user_status",
96
+ "workflow_webhook_deliveries_run",
97
+ "workshop_registrations_event_status"
98
+ ];
99
+ var CORE_SCHEMA_TRIGGERS = [
100
+ "credit_lots_bounds_insert",
101
+ "credit_lots_bounds_update",
102
+ "credit_lots_one_migration_insert",
103
+ "credit_lots_one_migration_update"
104
+ ];
105
+ var CORE_SCHEMA_COLUMNS = {
106
+ credit_lots: ["never_expires"],
107
+ harvest_attempts: ["debug_json"],
108
+ oauth_tokens: ["replaced_by", "rotated_at"],
109
+ site_extract_jobs: [
110
+ "attempted_urls",
111
+ "successful_urls",
112
+ "failed_urls",
113
+ "idempotency_key",
114
+ "request_fingerprint",
115
+ "dispatch_attempts",
116
+ "next_dispatch_at",
117
+ "dispatch_error",
118
+ "settlement_attempts",
119
+ "next_settlement_at",
120
+ "settlement_error"
121
+ ],
122
+ stripe_events: ["attempt_count", "claim_token", "last_error", "status", "updated_at"],
123
+ users: [
124
+ "balance_mc",
125
+ "concurrency_stripe_sub_id",
126
+ "extra_concurrency_slots",
127
+ "key_active",
128
+ "memory_identity",
129
+ "memory_key",
130
+ "memory_plan",
131
+ "memory_provisioned",
132
+ "memory_subscription_id",
133
+ "password_hash",
134
+ "stripe_customer_id",
135
+ "subscription_concurrency",
136
+ "subscription_id",
137
+ "subscription_tier"
138
+ ],
139
+ workshop_registrations: ["field_notes_email_id", "reminder_48h_email_id", "reminder_24h_email_id", "reminder_1h_email_id"],
140
+ workflow_artifacts: ["content"],
141
+ workflow_runs: ["step_index", "step_log_json", "step_state_json", "total_steps", "webhook_url"]
142
+ };
143
+ function sqlStringList(values) {
144
+ return values.map((value) => `'${value.replaceAll("'", "''")}'`).join(", ");
145
+ }
146
+ async function hasCurrentCoreSchema(db) {
147
+ const checks = [
148
+ `(SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN (${sqlStringList(CORE_SCHEMA_TABLES)})) = ${CORE_SCHEMA_TABLES.length}`,
149
+ `(SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' AND name IN (${sqlStringList(CORE_SCHEMA_INDEXES)})) = ${CORE_SCHEMA_INDEXES.length}`,
150
+ `(SELECT COUNT(*) FROM sqlite_master WHERE type = 'trigger' AND name IN (${sqlStringList(CORE_SCHEMA_TRIGGERS)})) = ${CORE_SCHEMA_TRIGGERS.length}`,
151
+ ...Object.entries(CORE_SCHEMA_COLUMNS).map(
152
+ ([table, columns]) => `(SELECT COUNT(*) FROM pragma_table_info('${table}') WHERE name IN (${sqlStringList(columns)})) = ${columns.length}`
153
+ )
154
+ ];
155
+ const result = await db.execute(`SELECT ${checks.join(" AND ")} AS ready`);
156
+ return Number(result.rows[0]?.ready ?? 0) === 1;
157
+ }
158
+ async function runIdempotentCoreDataBackfills(db) {
159
+ await db.execute(`
160
+ UPDATE site_extract_jobs SET
161
+ attempted_urls = done_urls,
162
+ successful_urls = (
163
+ SELECT COUNT(*) FROM site_extract_pages
164
+ WHERE job_id = site_extract_jobs.id
165
+ AND CASE
166
+ WHEN json_valid(page) = 0 THEN 0
167
+ WHEN json_extract(page, '$.extractionStatus') IS NOT NULL
168
+ THEN json_extract(page, '$.extractionStatus') = 'successful'
169
+ ELSE COALESCE(
170
+ json_extract(page, '$.status') >= 200
171
+ AND json_extract(page, '$.status') < 300
172
+ AND json_extract(page, '$.wordCount') > 0,
173
+ 0
174
+ )
175
+ END
176
+ ),
177
+ failed_urls = MAX(0, done_urls - (
178
+ SELECT COUNT(*) FROM site_extract_pages
179
+ WHERE job_id = site_extract_jobs.id
180
+ AND CASE
181
+ WHEN json_valid(page) = 0 THEN 0
182
+ WHEN json_extract(page, '$.extractionStatus') IS NOT NULL
183
+ THEN json_extract(page, '$.extractionStatus') = 'successful'
184
+ ELSE COALESCE(
185
+ json_extract(page, '$.status') >= 200
186
+ AND json_extract(page, '$.status') < 300
187
+ AND json_extract(page, '$.wordCount') > 0,
188
+ 0
189
+ )
190
+ END
191
+ ))
192
+ WHERE done_urls > 0
193
+ AND attempted_urls = 0
194
+ AND successful_urls = 0
195
+ AND failed_urls = 0
196
+ `);
197
+ }
198
+ function getDb() {
199
+ if (!_db) _db = createClient({ url: DB_URL, authToken: DB_TOKEN });
200
+ return _db;
201
+ }
202
+ function setDbForTesting(client) {
203
+ _db = client;
204
+ }
205
+ async function migrate() {
206
+ const db = getDb();
207
+ let applied;
208
+ try {
209
+ applied = await db.execute({
210
+ sql: "SELECT 1 AS applied FROM schema_migrations WHERE version = ? LIMIT 1",
211
+ args: [CORE_SCHEMA_VERSION]
212
+ });
213
+ } catch (error) {
214
+ const message = error instanceof Error ? error.message.toLowerCase() : String(error).toLowerCase();
215
+ if (!message.includes("no such table") || !message.includes("schema_migrations")) throw error;
216
+ await db.execute(`
217
+ CREATE TABLE IF NOT EXISTS schema_migrations (
218
+ version TEXT PRIMARY KEY,
219
+ applied_at TEXT NOT NULL DEFAULT (datetime('now'))
220
+ )
221
+ `);
222
+ applied = { rows: [] };
223
+ }
224
+ if (applied.rows.length > 0 && await hasCurrentCoreSchema(db)) {
225
+ await runIdempotentCoreDataBackfills(db);
226
+ return;
227
+ }
228
+ if (await hasCurrentCoreSchema(db)) {
229
+ await runIdempotentCoreDataBackfills(db);
230
+ await db.execute({
231
+ sql: "INSERT OR IGNORE INTO schema_migrations (version) VALUES (?)",
232
+ args: [CORE_SCHEMA_VERSION]
233
+ });
234
+ return;
235
+ }
236
+ await db.execute(`
237
+ CREATE TABLE IF NOT EXISTS users (
238
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
239
+ email TEXT UNIQUE NOT NULL,
240
+ name TEXT,
241
+ api_key TEXT UNIQUE NOT NULL,
242
+ password_hash TEXT,
243
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
244
+ active INTEGER NOT NULL DEFAULT 1
245
+ )
246
+ `);
247
+ await db.execute(`
248
+ CREATE TABLE IF NOT EXISTS jobs (
249
+ id TEXT PRIMARY KEY,
250
+ user_id INTEGER NOT NULL REFERENCES users(id),
251
+ status TEXT NOT NULL DEFAULT 'pending',
252
+ query TEXT NOT NULL,
253
+ options TEXT NOT NULL,
254
+ callback_url TEXT,
255
+ result TEXT,
256
+ error TEXT,
257
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
258
+ started_at TEXT,
259
+ completed_at TEXT
260
+ )
261
+ `);
262
+ await db.execute(`CREATE INDEX IF NOT EXISTS jobs_user_id ON jobs(user_id)`);
263
+ await db.execute(`CREATE INDEX IF NOT EXISTS jobs_status ON jobs(status)`);
264
+ await db.execute(
265
+ `CREATE INDEX IF NOT EXISTS jobs_created_at_status_user ON jobs(created_at, status, user_id)`
266
+ );
267
+ await db.execute(
268
+ `CREATE INDEX IF NOT EXISTS jobs_user_created_at_status ON jobs(user_id, created_at, status)`
269
+ );
270
+ await db.execute(`
271
+ CREATE TABLE IF NOT EXISTS harvest_attempts (
272
+ id TEXT PRIMARY KEY,
273
+ job_id TEXT NOT NULL REFERENCES jobs(id),
274
+ user_id INTEGER NOT NULL REFERENCES users(id),
275
+ attempt_number INTEGER NOT NULL,
276
+ max_attempts INTEGER NOT NULL,
277
+ query TEXT NOT NULL,
278
+ location TEXT,
279
+ max_questions INTEGER NOT NULL,
280
+ status TEXT NOT NULL,
281
+ outcome TEXT,
282
+ kernel_session_id TEXT,
283
+ question_count INTEGER,
284
+ duration_ms INTEGER,
285
+ error TEXT,
286
+ will_retry INTEGER,
287
+ kernel_delete_started INTEGER NOT NULL DEFAULT 0,
288
+ kernel_delete_succeeded INTEGER,
289
+ kernel_delete_error TEXT,
290
+ browser_close_succeeded INTEGER,
291
+ browser_close_error TEXT,
292
+ debug_json TEXT,
293
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
294
+ completed_at TEXT,
295
+ UNIQUE(job_id, attempt_number)
296
+ )
297
+ `);
298
+ await db.execute(`CREATE INDEX IF NOT EXISTS harvest_attempts_job_id ON harvest_attempts(job_id)`);
299
+ await db.execute(`CREATE INDEX IF NOT EXISTS harvest_attempts_user_created_at ON harvest_attempts(user_id, created_at DESC)`);
300
+ try {
301
+ await db.execute(`ALTER TABLE harvest_attempts ADD COLUMN debug_json TEXT`);
302
+ } catch {
303
+ }
304
+ try {
305
+ await db.execute(`ALTER TABLE users ADD COLUMN password_hash TEXT`);
306
+ } catch {
307
+ }
308
+ try {
309
+ await db.execute(`ALTER TABLE users ADD COLUMN key_active INTEGER NOT NULL DEFAULT 1`);
310
+ } catch {
311
+ }
312
+ await db.execute(`
313
+ CREATE TABLE IF NOT EXISTS kpo_jobs (
314
+ id TEXT PRIMARY KEY,
315
+ user_id INTEGER NOT NULL REFERENCES users(id),
316
+ status TEXT NOT NULL,
317
+ entity_name TEXT NOT NULL,
318
+ entity_type TEXT NOT NULL,
319
+ url TEXT,
320
+ request TEXT NOT NULL,
321
+ result TEXT,
322
+ error TEXT,
323
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
324
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
325
+ )
326
+ `);
327
+ await db.execute(`
328
+ CREATE TABLE IF NOT EXISTS kpo_phase_log (
329
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
330
+ job_id TEXT NOT NULL REFERENCES kpo_jobs(id),
331
+ phase TEXT NOT NULL,
332
+ output TEXT NOT NULL,
333
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
334
+ )
335
+ `);
336
+ await db.execute(`CREATE INDEX IF NOT EXISTS kpo_jobs_user_id ON kpo_jobs(user_id)`);
337
+ await db.execute(`CREATE INDEX IF NOT EXISTS kpo_jobs_status ON kpo_jobs(status)`);
338
+ await db.execute(`CREATE INDEX IF NOT EXISTS kpo_phase_log_job_id ON kpo_phase_log(job_id)`);
339
+ try {
340
+ await db.execute(`ALTER TABLE users ADD COLUMN stripe_customer_id TEXT`);
341
+ } catch {
342
+ }
343
+ try {
344
+ await db.execute(`ALTER TABLE users ADD COLUMN balance_mc INTEGER NOT NULL DEFAULT 0`);
345
+ } catch {
346
+ }
347
+ try {
348
+ await db.execute(`ALTER TABLE users ADD COLUMN extra_concurrency_slots INTEGER NOT NULL DEFAULT 0`);
349
+ } catch {
350
+ }
351
+ try {
352
+ await db.execute(`ALTER TABLE users ADD COLUMN concurrency_stripe_sub_id TEXT`);
353
+ } catch {
354
+ }
355
+ try {
356
+ await db.execute(`ALTER TABLE users ADD COLUMN subscription_concurrency INTEGER NOT NULL DEFAULT 0`);
357
+ } catch {
358
+ }
359
+ try {
360
+ await db.execute(`ALTER TABLE users ADD COLUMN subscription_tier TEXT`);
361
+ } catch {
362
+ }
363
+ try {
364
+ await db.execute(`ALTER TABLE users ADD COLUMN subscription_id TEXT`);
365
+ } catch {
366
+ }
367
+ try {
368
+ await db.execute(`ALTER TABLE users ADD COLUMN memory_key TEXT`);
369
+ } catch {
370
+ }
371
+ try {
372
+ await db.execute(`ALTER TABLE users ADD COLUMN memory_identity TEXT`);
373
+ } catch {
374
+ }
375
+ try {
376
+ await db.execute(`ALTER TABLE users ADD COLUMN memory_plan TEXT NOT NULL DEFAULT 'free'`);
377
+ } catch {
378
+ }
379
+ try {
380
+ await db.execute(`ALTER TABLE users ADD COLUMN memory_subscription_id TEXT`);
381
+ } catch {
382
+ }
383
+ try {
384
+ await db.execute(`ALTER TABLE users ADD COLUMN memory_provisioned INTEGER NOT NULL DEFAULT 0`);
385
+ } catch {
386
+ }
387
+ await db.execute(`
388
+ CREATE TABLE IF NOT EXISTS concurrency_locks (
389
+ id TEXT PRIMARY KEY,
390
+ user_id INTEGER NOT NULL REFERENCES users(id),
391
+ operation TEXT NOT NULL,
392
+ status TEXT NOT NULL DEFAULT 'active',
393
+ acquired_at TEXT NOT NULL DEFAULT (datetime('now')),
394
+ expires_at TEXT NOT NULL,
395
+ released_at TEXT,
396
+ metadata TEXT
397
+ )
398
+ `);
399
+ await db.execute(`CREATE INDEX IF NOT EXISTS concurrency_locks_user_status ON concurrency_locks(user_id, status, expires_at)`);
400
+ await db.execute(`CREATE INDEX IF NOT EXISTS concurrency_locks_operation ON concurrency_locks(operation, status, expires_at)`);
401
+ await db.execute(`
402
+ CREATE TABLE IF NOT EXISTS ledger (
403
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
404
+ user_id INTEGER NOT NULL REFERENCES users(id),
405
+ amount_mc INTEGER NOT NULL,
406
+ operation TEXT NOT NULL,
407
+ description TEXT,
408
+ stripe_pi TEXT,
409
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
410
+ )
411
+ `);
412
+ await db.execute(`CREATE INDEX IF NOT EXISTS ledger_user_id ON ledger(user_id)`);
413
+ await db.execute(`CREATE UNIQUE INDEX IF NOT EXISTS ledger_stripe_pi_unique ON ledger(stripe_pi) WHERE stripe_pi IS NOT NULL`);
414
+ await db.execute(`
415
+ CREATE TABLE IF NOT EXISTS credit_lots (
416
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
417
+ user_id INTEGER NOT NULL REFERENCES users(id),
418
+ amount_mc INTEGER NOT NULL,
419
+ remaining_mc INTEGER NOT NULL,
420
+ source TEXT NOT NULL,
421
+ stripe_pi TEXT,
422
+ granted_at TEXT NOT NULL DEFAULT (datetime('now')),
423
+ expires_at TEXT NOT NULL,
424
+ never_expires INTEGER NOT NULL DEFAULT 0
425
+ )
426
+ `);
427
+ try {
428
+ await db.execute(`ALTER TABLE credit_lots ADD COLUMN never_expires INTEGER NOT NULL DEFAULT 0`);
429
+ } catch {
430
+ }
431
+ await db.execute(`CREATE INDEX IF NOT EXISTS credit_lots_user_active ON credit_lots(user_id, expires_at, remaining_mc)`);
432
+ await db.execute(`
433
+ CREATE TABLE IF NOT EXISTS credit_lot_expirations (
434
+ lot_id INTEGER PRIMARY KEY REFERENCES credit_lots(id),
435
+ user_id INTEGER NOT NULL REFERENCES users(id),
436
+ amount_mc INTEGER NOT NULL,
437
+ claim_token TEXT NOT NULL UNIQUE,
438
+ expired_at TEXT NOT NULL DEFAULT (datetime('now'))
439
+ )
440
+ `);
441
+ await db.execute(`
442
+ CREATE TRIGGER IF NOT EXISTS credit_lots_bounds_insert
443
+ BEFORE INSERT ON credit_lots
444
+ WHEN NEW.amount_mc < 0
445
+ OR NEW.remaining_mc < 0
446
+ OR NEW.remaining_mc > NEW.amount_mc
447
+ BEGIN
448
+ SELECT RAISE(ABORT, 'invalid credit lot bounds');
449
+ END
450
+ `);
451
+ await db.execute(`
452
+ CREATE TRIGGER IF NOT EXISTS credit_lots_bounds_update
453
+ BEFORE UPDATE OF amount_mc, remaining_mc ON credit_lots
454
+ WHEN NEW.amount_mc < 0
455
+ OR NEW.remaining_mc < 0
456
+ OR NEW.remaining_mc > NEW.amount_mc
457
+ BEGIN
458
+ SELECT RAISE(ABORT, 'invalid credit lot bounds');
459
+ END
460
+ `);
461
+ await db.execute(`
462
+ CREATE TRIGGER IF NOT EXISTS credit_lots_one_migration_insert
463
+ BEFORE INSERT ON credit_lots
464
+ WHEN NEW.source = 'migration'
465
+ AND EXISTS (
466
+ SELECT 1 FROM credit_lots
467
+ WHERE user_id = NEW.user_id AND source = 'migration'
468
+ )
469
+ BEGIN
470
+ SELECT RAISE(ABORT, 'duplicate migration lot');
471
+ END
472
+ `);
473
+ await db.execute(`
474
+ CREATE TRIGGER IF NOT EXISTS credit_lots_one_migration_update
475
+ BEFORE UPDATE OF source, user_id ON credit_lots
476
+ WHEN NEW.source = 'migration'
477
+ AND EXISTS (
478
+ SELECT 1 FROM credit_lots
479
+ WHERE user_id = NEW.user_id
480
+ AND source = 'migration'
481
+ AND id != OLD.id
482
+ )
483
+ BEGIN
484
+ SELECT RAISE(ABORT, 'duplicate migration lot');
485
+ END
486
+ `);
487
+ await db.execute(`
488
+ CREATE TABLE IF NOT EXISTS credit_lot_repairs (
489
+ repair_id TEXT PRIMARY KEY,
490
+ user_id INTEGER NOT NULL REFERENCES users(id),
491
+ report_sha256 TEXT NOT NULL,
492
+ classification TEXT NOT NULL,
493
+ before_json TEXT NOT NULL,
494
+ plan_json TEXT NOT NULL,
495
+ after_json TEXT,
496
+ status TEXT NOT NULL DEFAULT 'claimed',
497
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
498
+ applied_at TEXT
499
+ )
500
+ `);
501
+ await db.execute(`CREATE INDEX IF NOT EXISTS credit_lot_repairs_user_applied ON credit_lot_repairs(user_id, applied_at)`);
502
+ await db.execute(`
503
+ CREATE TABLE IF NOT EXISTS free_credit_refreshes (
504
+ user_id INTEGER NOT NULL REFERENCES users(id),
505
+ month TEXT NOT NULL,
506
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
507
+ PRIMARY KEY (user_id, month)
508
+ )
509
+ `);
510
+ await db.execute(`
511
+ CREATE TABLE IF NOT EXISTS billing_authorizations (
512
+ id TEXT PRIMARY KEY,
513
+ user_id INTEGER NOT NULL REFERENCES users(id),
514
+ idempotency_key TEXT NOT NULL UNIQUE,
515
+ billing_class TEXT NOT NULL,
516
+ status TEXT NOT NULL,
517
+ minimum_amount_mc INTEGER NOT NULL,
518
+ maximum_amount_mc INTEGER NOT NULL,
519
+ token_hash TEXT NOT NULL,
520
+ metadata TEXT,
521
+ expires_at TEXT NOT NULL,
522
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
523
+ settled_at TEXT
524
+ )
525
+ `);
526
+ await db.execute(`CREATE INDEX IF NOT EXISTS billing_authorizations_user_status ON billing_authorizations(user_id, status, expires_at)`);
527
+ await db.execute(`
528
+ CREATE TABLE IF NOT EXISTS billing_events (
529
+ id TEXT PRIMARY KEY,
530
+ user_id INTEGER NOT NULL REFERENCES users(id),
531
+ authorization_id TEXT REFERENCES billing_authorizations(id),
532
+ idempotency_key TEXT NOT NULL UNIQUE,
533
+ billing_class TEXT NOT NULL,
534
+ source_surface TEXT NOT NULL,
535
+ status TEXT NOT NULL,
536
+ amount_mc INTEGER NOT NULL DEFAULT 0,
537
+ raw_cost_usd_nanos INTEGER,
538
+ multiplier_bps INTEGER,
539
+ input_tokens INTEGER,
540
+ output_tokens INTEGER,
541
+ cached_tokens INTEGER,
542
+ model_steps INTEGER,
543
+ metadata TEXT,
544
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
545
+ settled_at TEXT
546
+ )
547
+ `);
548
+ await db.execute(`CREATE INDEX IF NOT EXISTS billing_events_user_created_at ON billing_events(user_id, created_at DESC)`);
549
+ await db.execute(`CREATE INDEX IF NOT EXISTS billing_events_authorization ON billing_events(authorization_id)`);
550
+ await db.execute(`
551
+ CREATE TABLE IF NOT EXISTS billing_debits (
552
+ idempotency_key TEXT PRIMARY KEY,
553
+ user_id INTEGER NOT NULL REFERENCES users(id),
554
+ amount_mc INTEGER NOT NULL,
555
+ operation TEXT NOT NULL,
556
+ description TEXT,
557
+ status TEXT NOT NULL DEFAULT 'claimed',
558
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
559
+ applied_at TEXT
560
+ )
561
+ `);
562
+ await db.execute(`CREATE INDEX IF NOT EXISTS billing_debits_user_status ON billing_debits(user_id, status)`);
563
+ await db.execute(`
564
+ CREATE TABLE IF NOT EXISTS billing_debit_allocations (
565
+ debit_idempotency_key TEXT NOT NULL REFERENCES billing_debits(idempotency_key),
566
+ lot_id INTEGER NOT NULL REFERENCES credit_lots(id),
567
+ user_id INTEGER NOT NULL REFERENCES users(id),
568
+ amount_mc INTEGER NOT NULL,
569
+ expires_at TEXT NOT NULL,
570
+ PRIMARY KEY (debit_idempotency_key, lot_id)
571
+ )
572
+ `);
573
+ await db.execute(`
574
+ CREATE TABLE IF NOT EXISTS connected_account_billing (
575
+ user_id INTEGER PRIMARY KEY REFERENCES users(id),
576
+ stripe_subscription_id TEXT,
577
+ stripe_subscription_item_id TEXT,
578
+ price_id TEXT,
579
+ quantity INTEGER NOT NULL DEFAULT 0,
580
+ status TEXT NOT NULL DEFAULT 'pending',
581
+ last_error_code TEXT,
582
+ checked_at TEXT NOT NULL DEFAULT (datetime('now')),
583
+ synced_at TEXT,
584
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
585
+ )
586
+ `);
587
+ await db.execute(`
588
+ CREATE TABLE IF NOT EXISTS billing_credits (
589
+ idempotency_key TEXT PRIMARY KEY,
590
+ user_id INTEGER NOT NULL REFERENCES users(id),
591
+ amount_mc INTEGER NOT NULL,
592
+ operation TEXT NOT NULL,
593
+ description TEXT,
594
+ status TEXT NOT NULL DEFAULT 'claimed',
595
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
596
+ applied_at TEXT
597
+ )
598
+ `);
599
+ await db.execute(`CREATE INDEX IF NOT EXISTS billing_credits_user_status ON billing_credits(user_id, status)`);
600
+ await db.execute(`
601
+ CREATE TABLE IF NOT EXISTS admin_credit_adjustments (
602
+ reference TEXT PRIMARY KEY,
603
+ user_id INTEGER NOT NULL REFERENCES users(id),
604
+ direction TEXT NOT NULL,
605
+ amount_mc INTEGER NOT NULL,
606
+ reason TEXT NOT NULL,
607
+ actor TEXT NOT NULL,
608
+ status TEXT NOT NULL DEFAULT 'applied',
609
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
610
+ updated_at TEXT
611
+ )
612
+ `);
613
+ await db.execute(`CREATE INDEX IF NOT EXISTS admin_credit_adjustments_user_created ON admin_credit_adjustments(user_id, created_at)`);
614
+ await db.execute(`
615
+ CREATE TABLE IF NOT EXISTS admin_credit_adjustment_events (
616
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
617
+ reference TEXT NOT NULL REFERENCES admin_credit_adjustments(reference),
618
+ seq INTEGER NOT NULL,
619
+ kind TEXT NOT NULL,
620
+ from_amount_mc INTEGER,
621
+ to_amount_mc INTEGER,
622
+ from_reason TEXT,
623
+ to_reason TEXT,
624
+ moved_mc INTEGER NOT NULL DEFAULT 0,
625
+ actor TEXT NOT NULL,
626
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
627
+ UNIQUE (reference, seq)
628
+ )
629
+ `);
630
+ await db.execute(`CREATE INDEX IF NOT EXISTS admin_credit_adjustment_events_reference ON admin_credit_adjustment_events(reference)`);
631
+ await db.execute(`
632
+ CREATE TABLE IF NOT EXISTS site_audit_jobs (
633
+ id TEXT PRIMARY KEY,
634
+ user_id INTEGER,
635
+ status TEXT DEFAULT 'pending',
636
+ client_domain TEXT,
637
+ session_path TEXT,
638
+ request TEXT,
639
+ result TEXT,
640
+ error TEXT,
641
+ created_at TEXT DEFAULT (datetime('now')),
642
+ updated_at TEXT DEFAULT (datetime('now'))
643
+ )
644
+ `);
645
+ await db.execute(`
646
+ CREATE TABLE IF NOT EXISTS site_audit_phase_log (
647
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
648
+ job_id TEXT REFERENCES site_audit_jobs(id),
649
+ phase TEXT,
650
+ completed_at TEXT DEFAULT (datetime('now')),
651
+ output_summary TEXT
652
+ )
653
+ `);
654
+ await db.execute(`CREATE INDEX IF NOT EXISTS site_audit_jobs_user_id ON site_audit_jobs(user_id)`);
655
+ await db.execute(`CREATE INDEX IF NOT EXISTS site_audit_jobs_status ON site_audit_jobs(status)`);
656
+ await db.execute(`CREATE INDEX IF NOT EXISTS site_audit_phase_log_job_id ON site_audit_phase_log(job_id)`);
657
+ await db.execute(`
658
+ CREATE TABLE IF NOT EXISTS site_extract_jobs (
659
+ id TEXT PRIMARY KEY,
660
+ user_id INTEGER,
661
+ status TEXT DEFAULT 'pending',
662
+ start_url TEXT,
663
+ options TEXT,
664
+ total_urls INTEGER DEFAULT 0,
665
+ done_urls INTEGER DEFAULT 0,
666
+ attempted_urls INTEGER DEFAULT 0,
667
+ successful_urls INTEGER DEFAULT 0,
668
+ failed_urls INTEGER DEFAULT 0,
669
+ idempotency_key TEXT,
670
+ request_fingerprint TEXT,
671
+ dispatch_attempts INTEGER DEFAULT 0,
672
+ next_dispatch_at TEXT,
673
+ dispatch_error TEXT,
674
+ settlement_attempts INTEGER DEFAULT 0,
675
+ next_settlement_at TEXT,
676
+ settlement_error TEXT,
677
+ artifacts TEXT,
678
+ error TEXT,
679
+ billed_mc INTEGER,
680
+ created_at TEXT DEFAULT (datetime('now')),
681
+ updated_at TEXT DEFAULT (datetime('now'))
682
+ )
683
+ `);
684
+ await db.execute(`
685
+ CREATE TABLE IF NOT EXISTS site_extract_pages (
686
+ job_id TEXT,
687
+ url TEXT,
688
+ page TEXT,
689
+ PRIMARY KEY (job_id, url)
690
+ )
691
+ `);
692
+ await db.execute(`CREATE INDEX IF NOT EXISTS site_extract_jobs_user_id ON site_extract_jobs(user_id)`);
693
+ await db.execute(`CREATE INDEX IF NOT EXISTS site_extract_jobs_status ON site_extract_jobs(status)`);
694
+ await db.execute(`CREATE INDEX IF NOT EXISTS site_extract_pages_job_id ON site_extract_pages(job_id)`);
695
+ try {
696
+ await db.execute(`ALTER TABLE site_extract_jobs ADD COLUMN attempted_urls INTEGER DEFAULT 0`);
697
+ } catch {
698
+ }
699
+ try {
700
+ await db.execute(`ALTER TABLE site_extract_jobs ADD COLUMN successful_urls INTEGER DEFAULT 0`);
701
+ } catch {
702
+ }
703
+ try {
704
+ await db.execute(`ALTER TABLE site_extract_jobs ADD COLUMN failed_urls INTEGER DEFAULT 0`);
705
+ } catch {
706
+ }
707
+ try {
708
+ await db.execute(`ALTER TABLE site_extract_jobs ADD COLUMN idempotency_key TEXT`);
709
+ } catch {
710
+ }
711
+ try {
712
+ await db.execute(`ALTER TABLE site_extract_jobs ADD COLUMN request_fingerprint TEXT`);
713
+ } catch {
714
+ }
715
+ try {
716
+ await db.execute(`ALTER TABLE site_extract_jobs ADD COLUMN dispatch_attempts INTEGER DEFAULT 0`);
717
+ } catch {
718
+ }
719
+ try {
720
+ await db.execute(`ALTER TABLE site_extract_jobs ADD COLUMN next_dispatch_at TEXT`);
721
+ } catch {
722
+ }
723
+ try {
724
+ await db.execute(`ALTER TABLE site_extract_jobs ADD COLUMN dispatch_error TEXT`);
725
+ } catch {
726
+ }
727
+ try {
728
+ await db.execute(`ALTER TABLE site_extract_jobs ADD COLUMN settlement_attempts INTEGER DEFAULT 0`);
729
+ } catch {
730
+ }
731
+ try {
732
+ await db.execute(`ALTER TABLE site_extract_jobs ADD COLUMN next_settlement_at TEXT`);
733
+ } catch {
734
+ }
735
+ try {
736
+ await db.execute(`ALTER TABLE site_extract_jobs ADD COLUMN settlement_error TEXT`);
737
+ } catch {
738
+ }
739
+ await db.execute(`CREATE UNIQUE INDEX IF NOT EXISTS site_extract_jobs_idempotency ON site_extract_jobs(user_id, idempotency_key) WHERE idempotency_key IS NOT NULL`);
740
+ await db.execute(`
741
+ CREATE TABLE IF NOT EXISTS password_reset_tokens (
742
+ token TEXT PRIMARY KEY,
743
+ user_id INTEGER NOT NULL REFERENCES users(id),
744
+ expires_at TEXT NOT NULL,
745
+ used INTEGER NOT NULL DEFAULT 0,
746
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
747
+ )
748
+ `);
749
+ await db.execute(`CREATE INDEX IF NOT EXISTS prt_user_id ON password_reset_tokens(user_id)`);
750
+ await db.execute(`
751
+ CREATE TABLE IF NOT EXISTS rate_limits (
752
+ scope TEXT NOT NULL,
753
+ key_hash TEXT NOT NULL,
754
+ window_start INTEGER NOT NULL,
755
+ count INTEGER NOT NULL DEFAULT 0,
756
+ updated_at TEXT NOT NULL DEFAULT (datetime('now')),
757
+ PRIMARY KEY (scope, key_hash, window_start)
758
+ )
759
+ `);
760
+ await db.execute(`
761
+ CREATE TABLE IF NOT EXISTS request_events (
762
+ id TEXT PRIMARY KEY,
763
+ user_id INTEGER NOT NULL REFERENCES users(id),
764
+ source TEXT NOT NULL,
765
+ status TEXT NOT NULL,
766
+ query TEXT NOT NULL,
767
+ location TEXT,
768
+ result_count INTEGER,
769
+ result TEXT,
770
+ error TEXT,
771
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
772
+ )
773
+ `);
774
+ await db.execute(`CREATE INDEX IF NOT EXISTS request_events_user_created_at ON request_events(user_id, created_at DESC)`);
775
+ await db.execute(`
776
+ CREATE TABLE IF NOT EXISTS workflow_schedules (
777
+ id TEXT PRIMARY KEY,
778
+ user_id INTEGER NOT NULL REFERENCES users(id),
779
+ workflow_id TEXT NOT NULL,
780
+ name TEXT NOT NULL,
781
+ input_json TEXT NOT NULL,
782
+ cadence TEXT NOT NULL,
783
+ cron_expr TEXT,
784
+ timezone TEXT NOT NULL DEFAULT 'UTC',
785
+ webhook_url TEXT,
786
+ status TEXT NOT NULL DEFAULT 'active',
787
+ next_run_at TEXT,
788
+ last_run_at TEXT,
789
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
790
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
791
+ )
792
+ `);
793
+ await db.execute(`CREATE INDEX IF NOT EXISTS workflow_schedules_user_status ON workflow_schedules(user_id, status, created_at DESC)`);
794
+ await db.execute(`CREATE INDEX IF NOT EXISTS workflow_schedules_due ON workflow_schedules(status, next_run_at)`);
795
+ await db.execute(`
796
+ CREATE TABLE IF NOT EXISTS workflow_runs (
797
+ id TEXT PRIMARY KEY,
798
+ user_id INTEGER NOT NULL REFERENCES users(id),
799
+ schedule_id TEXT,
800
+ workflow_id TEXT NOT NULL,
801
+ input_json TEXT NOT NULL,
802
+ status TEXT NOT NULL DEFAULT 'queued',
803
+ manifest_json TEXT,
804
+ error_message TEXT,
805
+ queued_at TEXT NOT NULL DEFAULT (datetime('now')),
806
+ started_at TEXT,
807
+ completed_at TEXT,
808
+ idempotency_key TEXT,
809
+ step_index INTEGER NOT NULL DEFAULT 0,
810
+ total_steps INTEGER,
811
+ step_state_json TEXT,
812
+ step_log_json TEXT,
813
+ webhook_url TEXT
814
+ )
815
+ `);
816
+ try {
817
+ await db.execute(`ALTER TABLE workflow_runs ADD COLUMN step_index INTEGER NOT NULL DEFAULT 0`);
818
+ } catch {
819
+ }
820
+ try {
821
+ await db.execute(`ALTER TABLE workflow_runs ADD COLUMN total_steps INTEGER`);
822
+ } catch {
823
+ }
824
+ try {
825
+ await db.execute(`ALTER TABLE workflow_runs ADD COLUMN step_state_json TEXT`);
826
+ } catch {
827
+ }
828
+ try {
829
+ await db.execute(`ALTER TABLE workflow_runs ADD COLUMN step_log_json TEXT`);
830
+ } catch {
831
+ }
832
+ try {
833
+ await db.execute(`ALTER TABLE workflow_runs ADD COLUMN webhook_url TEXT`);
834
+ } catch {
835
+ }
836
+ await db.execute(`CREATE INDEX IF NOT EXISTS workflow_runs_user_created ON workflow_runs(user_id, queued_at DESC)`);
837
+ await db.execute(`CREATE INDEX IF NOT EXISTS workflow_runs_schedule ON workflow_runs(schedule_id, queued_at DESC)`);
838
+ await db.execute(`CREATE UNIQUE INDEX IF NOT EXISTS workflow_runs_idempotency ON workflow_runs(idempotency_key) WHERE idempotency_key IS NOT NULL`);
839
+ await db.execute(`
840
+ CREATE TABLE IF NOT EXISTS workflow_artifacts (
841
+ id TEXT PRIMARY KEY,
842
+ run_id TEXT NOT NULL REFERENCES workflow_runs(id),
843
+ user_id INTEGER NOT NULL REFERENCES users(id),
844
+ kind TEXT NOT NULL,
845
+ label TEXT NOT NULL,
846
+ path TEXT NOT NULL,
847
+ content_type TEXT NOT NULL,
848
+ bytes INTEGER,
849
+ rows_count INTEGER,
850
+ content TEXT,
851
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
852
+ )
853
+ `);
854
+ try {
855
+ await db.execute(`ALTER TABLE workflow_artifacts ADD COLUMN content TEXT`);
856
+ } catch {
857
+ }
858
+ await db.execute(`CREATE INDEX IF NOT EXISTS workflow_artifacts_run ON workflow_artifacts(run_id)`);
859
+ await db.execute(`
860
+ CREATE TABLE IF NOT EXISTS workflow_webhook_deliveries (
861
+ id TEXT PRIMARY KEY,
862
+ run_id TEXT NOT NULL REFERENCES workflow_runs(id),
863
+ schedule_id TEXT,
864
+ user_id INTEGER NOT NULL REFERENCES users(id),
865
+ webhook_url TEXT NOT NULL,
866
+ status TEXT NOT NULL,
867
+ attempts INTEGER NOT NULL DEFAULT 0,
868
+ last_error TEXT,
869
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
870
+ delivered_at TEXT
871
+ )
872
+ `);
873
+ await db.execute(`CREATE INDEX IF NOT EXISTS workflow_webhook_deliveries_run ON workflow_webhook_deliveries(run_id)`);
874
+ await db.execute(`
875
+ CREATE TABLE IF NOT EXISTS stripe_events (
876
+ event_id TEXT PRIMARY KEY,
877
+ status TEXT NOT NULL DEFAULT 'processed',
878
+ attempt_count INTEGER NOT NULL DEFAULT 1,
879
+ claim_token TEXT,
880
+ last_error TEXT,
881
+ processed_at TEXT NOT NULL DEFAULT (datetime('now')),
882
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
883
+ )
884
+ `);
885
+ for (const col of [
886
+ `status TEXT NOT NULL DEFAULT 'processed'`,
887
+ "attempt_count INTEGER NOT NULL DEFAULT 1",
888
+ "claim_token TEXT",
889
+ "last_error TEXT",
890
+ "updated_at TEXT"
891
+ ]) {
892
+ try {
893
+ await db.execute(`ALTER TABLE stripe_events ADD COLUMN ${col}`);
894
+ } catch {
895
+ }
896
+ }
897
+ await db.execute(`
898
+ UPDATE stripe_events
899
+ SET updated_at = COALESCE(updated_at, processed_at),
900
+ status = COALESCE(status, 'processed'),
901
+ attempt_count = MAX(1, COALESCE(attempt_count, 1))
902
+ WHERE updated_at IS NULL
903
+ OR status IS NULL
904
+ OR attempt_count IS NULL
905
+ OR attempt_count < 1
906
+ `);
907
+ await db.execute(`
908
+ CREATE TABLE IF NOT EXISTS oauth_clients (
909
+ client_id TEXT PRIMARY KEY,
910
+ redirect_uris TEXT NOT NULL,
911
+ name TEXT,
912
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
913
+ )
914
+ `);
915
+ await db.execute(`
916
+ CREATE TABLE IF NOT EXISTS oauth_codes (
917
+ code TEXT PRIMARY KEY,
918
+ client_id TEXT NOT NULL,
919
+ redirect_uri TEXT NOT NULL,
920
+ code_challenge TEXT NOT NULL,
921
+ identity TEXT NOT NULL,
922
+ scope TEXT NOT NULL,
923
+ resource TEXT,
924
+ expires_at TEXT NOT NULL,
925
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
926
+ )
927
+ `);
928
+ await db.execute(`
929
+ CREATE TABLE IF NOT EXISTS oauth_tokens (
930
+ refresh_token TEXT PRIMARY KEY,
931
+ client_id TEXT NOT NULL,
932
+ identity TEXT NOT NULL,
933
+ scope TEXT NOT NULL,
934
+ resource TEXT,
935
+ expires_at TEXT NOT NULL,
936
+ revoked INTEGER NOT NULL DEFAULT 0,
937
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
938
+ )
939
+ `);
940
+ await db.execute(`CREATE INDEX IF NOT EXISTS oauth_tokens_identity ON oauth_tokens(identity)`);
941
+ for (const col of ["replaced_by TEXT", "rotated_at TEXT"]) {
942
+ try {
943
+ await db.execute(`ALTER TABLE oauth_tokens ADD COLUMN ${col}`);
944
+ } catch {
945
+ }
946
+ }
947
+ await db.execute(`
948
+ CREATE TABLE IF NOT EXISTS page_snapshots (
949
+ user_id INTEGER NOT NULL REFERENCES users(id),
950
+ url TEXT NOT NULL,
951
+ content_hash TEXT NOT NULL,
952
+ content TEXT NOT NULL,
953
+ title TEXT,
954
+ content_bytes INTEGER NOT NULL,
955
+ truncated INTEGER NOT NULL DEFAULT 0,
956
+ checked_at TEXT NOT NULL DEFAULT (datetime('now')),
957
+ PRIMARY KEY (user_id, url)
958
+ )
959
+ `);
960
+ await db.execute(`CREATE INDEX IF NOT EXISTS page_snapshots_user_checked_at ON page_snapshots(user_id, checked_at DESC)`);
961
+ await db.execute(`
962
+ CREATE TABLE IF NOT EXISTS inbox_messages (
963
+ id TEXT PRIMARY KEY,
964
+ user_id INTEGER NOT NULL REFERENCES users(id),
965
+ subject TEXT NOT NULL,
966
+ body_md TEXT NOT NULL,
967
+ source TEXT NOT NULL DEFAULT 'operator',
968
+ sent_at TEXT NOT NULL DEFAULT (datetime('now')),
969
+ opened_at TEXT,
970
+ deleted_at TEXT
971
+ )
972
+ `);
973
+ await db.execute(`CREATE INDEX IF NOT EXISTS inbox_messages_user_sent_at ON inbox_messages(user_id, sent_at DESC)`);
974
+ await db.execute(`
975
+ CREATE TABLE IF NOT EXISTS workshop_registrations (
976
+ id TEXT PRIMARY KEY,
977
+ event_slug TEXT NOT NULL,
978
+ email TEXT NOT NULL,
979
+ first_name TEXT NOT NULL,
980
+ last_name TEXT,
981
+ status TEXT NOT NULL,
982
+ zoom_registrant_id TEXT,
983
+ zoom_join_url TEXT,
984
+ resend_contact_id TEXT,
985
+ confirmation_email_id TEXT,
986
+ field_notes_email_id TEXT,
987
+ reminder_48h_email_id TEXT,
988
+ reminder_24h_email_id TEXT,
989
+ reminder_1h_email_id TEXT,
990
+ last_error TEXT,
991
+ registered_at TEXT NOT NULL DEFAULT (datetime('now')),
992
+ updated_at TEXT NOT NULL DEFAULT (datetime('now')),
993
+ UNIQUE(event_slug, email)
994
+ )
995
+ `);
996
+ for (const col of ["field_notes_email_id TEXT", "reminder_48h_email_id TEXT", "reminder_24h_email_id TEXT", "reminder_1h_email_id TEXT"]) {
997
+ try {
998
+ await db.execute(`ALTER TABLE workshop_registrations ADD COLUMN ${col}`);
999
+ } catch {
1000
+ }
1001
+ }
1002
+ await db.execute(`
1003
+ CREATE INDEX IF NOT EXISTS workshop_registrations_event_status
1004
+ ON workshop_registrations(event_slug, status, registered_at)
1005
+ `);
1006
+ await db.execute(`
1007
+ CREATE TABLE IF NOT EXISTS browser_profile_labels (
1008
+ user_id INTEGER NOT NULL REFERENCES users(id),
1009
+ profile TEXT NOT NULL,
1010
+ label TEXT NOT NULL,
1011
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
1012
+ updated_at TEXT,
1013
+ PRIMARY KEY (user_id, profile)
1014
+ )
1015
+ `);
1016
+ if (!await hasCurrentCoreSchema(db)) {
1017
+ throw new Error("core schema migration finished without all required tables, indexes, and columns");
1018
+ }
1019
+ await runIdempotentCoreDataBackfills(db);
1020
+ await db.execute({
1021
+ sql: "INSERT OR IGNORE INTO schema_migrations (version) VALUES (?)",
1022
+ args: [CORE_SCHEMA_VERSION]
1023
+ });
1024
+ }
1025
+ async function registerClient(clientId, redirectUris, name) {
1026
+ await getDb().execute({
1027
+ sql: "INSERT INTO oauth_clients (client_id, redirect_uris, name) VALUES (?, ?, ?)",
1028
+ args: [clientId, JSON.stringify(redirectUris), name]
1029
+ });
1030
+ }
1031
+ async function getClient(clientId) {
1032
+ const res = await getDb().execute({ sql: "SELECT * FROM oauth_clients WHERE client_id = ?", args: [clientId] });
1033
+ const row = res.rows[0];
1034
+ if (!row) return void 0;
1035
+ let uris = [];
1036
+ try {
1037
+ const parsed = JSON.parse(String(row.redirect_uris));
1038
+ if (Array.isArray(parsed)) uris = parsed.filter((u) => typeof u === "string");
1039
+ } catch {
1040
+ uris = [];
1041
+ }
1042
+ return {
1043
+ client_id: String(row.client_id),
1044
+ redirect_uris: uris,
1045
+ name: row.name != null ? String(row.name) : null,
1046
+ created_at: String(row.created_at)
1047
+ };
1048
+ }
1049
+ async function putCode(code) {
1050
+ await getDb().execute({
1051
+ sql: "INSERT INTO oauth_codes (code, client_id, redirect_uri, code_challenge, identity, scope, resource, expires_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
1052
+ args: [code.code, code.client_id, code.redirect_uri, code.code_challenge, code.identity, code.scope, code.resource, code.expires_at]
1053
+ });
1054
+ }
1055
+ async function consumeCode(code) {
1056
+ const db = getDb();
1057
+ const res = await db.execute({ sql: "SELECT * FROM oauth_codes WHERE code = ?", args: [code] });
1058
+ const row = res.rows[0];
1059
+ await db.execute({ sql: "DELETE FROM oauth_codes WHERE code = ?", args: [code] });
1060
+ if (!row) return void 0;
1061
+ return {
1062
+ code: String(row.code),
1063
+ client_id: String(row.client_id),
1064
+ redirect_uri: String(row.redirect_uri),
1065
+ code_challenge: String(row.code_challenge),
1066
+ identity: String(row.identity),
1067
+ scope: String(row.scope),
1068
+ resource: row.resource != null ? String(row.resource) : null,
1069
+ expires_at: String(row.expires_at)
1070
+ };
1071
+ }
1072
+ async function putRefresh(token) {
1073
+ await getDb().execute({
1074
+ sql: "INSERT INTO oauth_tokens (refresh_token, client_id, identity, scope, resource, expires_at, revoked) VALUES (?, ?, ?, ?, ?, ?, 0)",
1075
+ args: [token.refresh_token, token.client_id, token.identity, token.scope, token.resource, token.expires_at]
1076
+ });
1077
+ }
1078
+ async function getRefresh(refreshToken) {
1079
+ const res = await getDb().execute({ sql: "SELECT * FROM oauth_tokens WHERE refresh_token = ?", args: [refreshToken] });
1080
+ const row = res.rows[0];
1081
+ if (!row) return void 0;
1082
+ return {
1083
+ refresh_token: String(row.refresh_token),
1084
+ client_id: String(row.client_id),
1085
+ identity: String(row.identity),
1086
+ scope: String(row.scope),
1087
+ resource: row.resource != null ? String(row.resource) : null,
1088
+ expires_at: String(row.expires_at),
1089
+ revoked: Number(row.revoked ?? 0),
1090
+ replaced_by: row.replaced_by != null ? String(row.replaced_by) : null,
1091
+ rotated_at: row.rotated_at != null ? String(row.rotated_at) : null
1092
+ };
1093
+ }
1094
+ async function revokeRefresh(refreshToken) {
1095
+ await getDb().execute({ sql: "UPDATE oauth_tokens SET revoked = 1 WHERE refresh_token = ?", args: [refreshToken] });
1096
+ }
1097
+ async function rotateRefresh(oldToken, next) {
1098
+ await putRefresh(next);
1099
+ await getDb().execute({
1100
+ sql: "UPDATE oauth_tokens SET revoked = 1, replaced_by = ?, rotated_at = ? WHERE refresh_token = ?",
1101
+ args: [next.refresh_token, (/* @__PURE__ */ new Date()).toISOString(), oldToken]
1102
+ });
1103
+ }
1104
+ async function claimStripeEvent(eventId) {
1105
+ const token = randomUUID();
1106
+ const claimed = await getDb().execute({
1107
+ sql: `INSERT INTO stripe_events
1108
+ (event_id, status, attempt_count, claim_token, last_error, processed_at, updated_at)
1109
+ VALUES (?, 'processing', 1, ?, NULL, datetime('now'), datetime('now'))
1110
+ ON CONFLICT(event_id) DO UPDATE SET
1111
+ status = 'processing',
1112
+ attempt_count = stripe_events.attempt_count + 1,
1113
+ claim_token = excluded.claim_token,
1114
+ last_error = NULL,
1115
+ updated_at = datetime('now')
1116
+ WHERE stripe_events.status = 'failed'
1117
+ OR (
1118
+ stripe_events.status = 'processing'
1119
+ AND stripe_events.updated_at <= datetime('now', '-10 minutes')
1120
+ )`,
1121
+ args: [eventId, token]
1122
+ });
1123
+ if (claimed.rowsAffected > 0) return { state: "claimed", token };
1124
+ const current = await getDb().execute({
1125
+ sql: "SELECT status FROM stripe_events WHERE event_id = ? LIMIT 1",
1126
+ args: [eventId]
1127
+ });
1128
+ return { state: String(current.rows[0]?.status) === "processed" ? "processed" : "processing" };
1129
+ }
1130
+ async function completeStripeEvent(eventId, token) {
1131
+ const result = await getDb().execute({
1132
+ sql: `UPDATE stripe_events
1133
+ SET status = 'processed',
1134
+ claim_token = NULL,
1135
+ last_error = NULL,
1136
+ processed_at = datetime('now'),
1137
+ updated_at = datetime('now')
1138
+ WHERE event_id = ? AND status = 'processing' AND claim_token = ?`,
1139
+ args: [eventId, token]
1140
+ });
1141
+ return result.rowsAffected === 1;
1142
+ }
1143
+ async function failStripeEvent(eventId, token, error) {
1144
+ const result = await getDb().execute({
1145
+ sql: `UPDATE stripe_events
1146
+ SET status = 'failed',
1147
+ claim_token = NULL,
1148
+ last_error = ?,
1149
+ updated_at = datetime('now')
1150
+ WHERE event_id = ? AND status = 'processing' AND claim_token = ?`,
1151
+ args: [error.slice(0, 500), eventId, token]
1152
+ });
1153
+ return result.rowsAffected === 1;
1154
+ }
1155
+ async function stripeEventAlreadyProcessed(eventId) {
1156
+ const res = await getDb().execute({
1157
+ sql: `SELECT 1 FROM stripe_events WHERE event_id = ? AND status = 'processed' LIMIT 1`,
1158
+ args: [eventId]
1159
+ });
1160
+ return res.rows.length > 0;
1161
+ }
1162
+ function generateApiKey() {
1163
+ return "sk_" + randomBytes(24).toString("hex");
1164
+ }
1165
+ function hashPassword(password) {
1166
+ const salt = randomBytes(16).toString("hex");
1167
+ const hash = scryptSync(password, salt, 64);
1168
+ return `${salt}:${hash.toString("hex")}`;
1169
+ }
1170
+ function verifyPassword(password, stored) {
1171
+ const [salt, hash] = stored.split(":");
1172
+ const hashBuf = Buffer.from(hash, "hex");
1173
+ const derived = scryptSync(password, salt, 64);
1174
+ return timingSafeEqual(hashBuf, derived);
1175
+ }
1176
+ function rowToUser(row) {
1177
+ return {
1178
+ id: Number(row.id),
1179
+ email: String(row.email),
1180
+ name: row.name != null ? String(row.name) : null,
1181
+ api_key: String(row.api_key),
1182
+ key_active: Number(row.key_active ?? 1),
1183
+ password_hash: row.password_hash != null ? String(row.password_hash) : null,
1184
+ created_at: String(row.created_at),
1185
+ active: Number(row.active),
1186
+ stripe_customer_id: row.stripe_customer_id != null ? String(row.stripe_customer_id) : null,
1187
+ balance_mc: Number(row.balance_mc ?? 0),
1188
+ extra_concurrency_slots: Number(row.extra_concurrency_slots ?? 0),
1189
+ concurrency_stripe_sub_id: row.concurrency_stripe_sub_id != null ? String(row.concurrency_stripe_sub_id) : null,
1190
+ subscription_concurrency: Number(row.subscription_concurrency ?? 0),
1191
+ subscription_tier: row.subscription_tier != null ? String(row.subscription_tier) : null,
1192
+ subscription_id: row.subscription_id != null ? String(row.subscription_id) : null,
1193
+ memory_plan: row.memory_plan != null ? String(row.memory_plan) : "free",
1194
+ memory_subscription_id: row.memory_subscription_id != null ? String(row.memory_subscription_id) : null,
1195
+ memory_provisioned: Number(row.memory_provisioned ?? 0)
1196
+ };
1197
+ }
1198
+ async function getUserByEmail(email) {
1199
+ const res = await getDb().execute({ sql: "SELECT * FROM users WHERE email = ? AND active = 1", args: [email] });
1200
+ return res.rows[0] ? rowToUser(res.rows[0]) : void 0;
1201
+ }
1202
+ async function getUserByApiKey(api_key) {
1203
+ const res = await getDb().execute({ sql: "SELECT * FROM users WHERE api_key = ? AND active = 1 AND key_active = 1", args: [api_key] });
1204
+ return res.rows[0] ? rowToUser(res.rows[0]) : void 0;
1205
+ }
1206
+ async function getUserById(id) {
1207
+ const res = await getDb().execute({ sql: "SELECT * FROM users WHERE id = ? AND active = 1", args: [id] });
1208
+ return res.rows[0] ? rowToUser(res.rows[0]) : void 0;
1209
+ }
1210
+ async function getUserMemoryCreds(id) {
1211
+ const res = await getDb().execute({ sql: "SELECT memory_key, memory_identity FROM users WHERE id = ?", args: [id] });
1212
+ const row = res.rows[0];
1213
+ return {
1214
+ memory_key: row?.memory_key != null ? String(row.memory_key) : null,
1215
+ memory_identity: row?.memory_identity != null ? String(row.memory_identity) : null
1216
+ };
1217
+ }
1218
+ async function setUserMemoryCreds(id, memoryKeyEncrypted, memoryIdentity) {
1219
+ await getDb().execute({ sql: "UPDATE users SET memory_key = ?, memory_identity = ? WHERE id = ?", args: [memoryKeyEncrypted, memoryIdentity, id] });
1220
+ }
1221
+ async function setPassword(id, password) {
1222
+ await getDb().execute({ sql: "UPDATE users SET password_hash = ? WHERE id = ?", args: [hashPassword(password), id] });
1223
+ }
1224
+ async function getUserStats(userId) {
1225
+ const db = getDb();
1226
+ const [totR, doneR, failR, tqR, luR] = await Promise.all([
1227
+ db.execute({ sql: "SELECT COUNT(*) as n FROM jobs WHERE user_id = ?", args: [userId] }),
1228
+ db.execute({ sql: "SELECT COUNT(*) as n FROM jobs WHERE user_id = ? AND status = 'done'", args: [userId] }),
1229
+ db.execute({ sql: "SELECT COUNT(*) as n FROM jobs WHERE user_id = ? AND status = 'failed'", args: [userId] }),
1230
+ db.execute({ sql: "SELECT COALESCE(SUM(json_extract(result, '$.totalQuestions')), 0) as n FROM jobs WHERE user_id = ? AND status = 'done'", args: [userId] }),
1231
+ db.execute({ sql: "SELECT MAX(created_at) as t FROM jobs WHERE user_id = ?", args: [userId] })
1232
+ ]);
1233
+ return {
1234
+ total: Number(totR.rows[0]?.n ?? 0),
1235
+ done: Number(doneR.rows[0]?.n ?? 0),
1236
+ failed: Number(failR.rows[0]?.n ?? 0),
1237
+ totalQuestions: Number(tqR.rows[0]?.n ?? 0),
1238
+ lastUsed: luR.rows[0]?.t != null ? String(luR.rows[0].t) : null
1239
+ };
1240
+ }
1241
+ async function createUser(email, name, password, stripeCustomerId) {
1242
+ const db = getDb();
1243
+ const api_key = generateApiKey();
1244
+ const plainPassword = password ?? randomBytes(6).toString("hex");
1245
+ const password_hash = hashPassword(plainPassword);
1246
+ const result = await db.execute({
1247
+ sql: "INSERT INTO users (email, name, api_key, password_hash, stripe_customer_id) VALUES (?, ?, ?, ?, ?)",
1248
+ args: [email, name ?? null, api_key, password_hash, stripeCustomerId ?? null]
1249
+ });
1250
+ return { id: result.lastInsertRowid, email, name, api_key, password: plainPassword };
1251
+ }
1252
+ async function checkRateLimit(scope, key, limit, windowSeconds) {
1253
+ const nowSeconds = Math.floor(Date.now() / 1e3);
1254
+ const windowStart = Math.floor(nowSeconds / windowSeconds) * windowSeconds;
1255
+ const keyHash = createHash("sha256").update(key).digest("hex");
1256
+ const db = getDb();
1257
+ if (!_rateLimitSchemaReady) {
1258
+ await db.execute(`
1259
+ CREATE TABLE IF NOT EXISTS rate_limits (
1260
+ scope TEXT NOT NULL,
1261
+ key_hash TEXT NOT NULL,
1262
+ window_start INTEGER NOT NULL,
1263
+ count INTEGER NOT NULL DEFAULT 0,
1264
+ updated_at TEXT NOT NULL DEFAULT (datetime('now')),
1265
+ PRIMARY KEY (scope, key_hash, window_start)
1266
+ )
1267
+ `);
1268
+ _rateLimitSchemaReady = true;
1269
+ }
1270
+ await db.execute({
1271
+ sql: `
1272
+ INSERT INTO rate_limits (scope, key_hash, window_start, count)
1273
+ VALUES (?, ?, ?, 1)
1274
+ ON CONFLICT(scope, key_hash, window_start)
1275
+ DO UPDATE SET count = count + 1, updated_at = datetime('now')
1276
+ `,
1277
+ args: [scope, keyHash, windowStart]
1278
+ });
1279
+ const res = await db.execute({
1280
+ sql: "SELECT count FROM rate_limits WHERE scope = ? AND key_hash = ? AND window_start = ?",
1281
+ args: [scope, keyHash, windowStart]
1282
+ });
1283
+ const count = Number(res.rows[0]?.count ?? 1);
1284
+ return {
1285
+ allowed: count <= limit,
1286
+ count,
1287
+ remaining: Math.max(0, limit - count),
1288
+ resetSeconds: Math.max(1, windowStart + windowSeconds - nowSeconds)
1289
+ };
1290
+ }
1291
+ async function createPasswordResetToken(userId) {
1292
+ const token = randomBytes(32).toString("hex");
1293
+ const tokenHash = createHash("sha256").update(token).digest("hex");
1294
+ const expiresAt = new Date(Date.now() + 60 * 60 * 1e3).toISOString();
1295
+ await getDb().execute({
1296
+ sql: "INSERT INTO password_reset_tokens (token, user_id, expires_at) VALUES (?, ?, ?)",
1297
+ args: [tokenHash, userId, expiresAt]
1298
+ });
1299
+ return token;
1300
+ }
1301
+ async function consumePasswordResetToken(token) {
1302
+ const db = getDb();
1303
+ const tokenHash = createHash("sha256").update(token).digest("hex");
1304
+ const res = await db.execute({
1305
+ sql: "SELECT user_id, expires_at, used FROM password_reset_tokens WHERE token = ?",
1306
+ args: [tokenHash]
1307
+ });
1308
+ const row = res.rows[0];
1309
+ if (!row) return null;
1310
+ if (Number(row.used)) return null;
1311
+ if (new Date(String(row.expires_at)) < /* @__PURE__ */ new Date()) return null;
1312
+ await db.execute({ sql: "UPDATE password_reset_tokens SET used = 1 WHERE token = ?", args: [tokenHash] });
1313
+ return Number(row.user_id);
1314
+ }
1315
+ async function rotateApiKey(userId) {
1316
+ const newKey = generateApiKey();
1317
+ await getDb().execute({ sql: "UPDATE users SET api_key = ?, key_active = 1 WHERE id = ?", args: [newKey, userId] });
1318
+ return newKey;
1319
+ }
1320
+ async function revokeApiKey(userId) {
1321
+ await getDb().execute({ sql: "UPDATE users SET key_active = 0 WHERE id = ?", args: [userId] });
1322
+ }
1323
+ async function listUsers() {
1324
+ const res = await getDb().execute("SELECT id, email, name, created_at, active FROM users ORDER BY created_at DESC");
1325
+ return res.rows.map((r) => rowToUser({ ...r, api_key: "", key_active: 1 }));
1326
+ }
1327
+ async function deactivateUser(id) {
1328
+ await getDb().execute({ sql: "UPDATE users SET active = 0 WHERE id = ?", args: [id] });
1329
+ }
1330
+ function rowToRawJob(row) {
1331
+ return {
1332
+ id: String(row.id),
1333
+ user_id: Number(row.user_id),
1334
+ status: String(row.status),
1335
+ query: String(row.query),
1336
+ options: String(row.options),
1337
+ callback_url: row.callback_url != null ? String(row.callback_url) : null,
1338
+ result: row.result != null ? String(row.result) : null,
1339
+ error: row.error != null ? String(row.error) : null,
1340
+ created_at: String(row.created_at),
1341
+ started_at: row.started_at != null ? String(row.started_at) : null,
1342
+ completed_at: row.completed_at != null ? String(row.completed_at) : null
1343
+ };
1344
+ }
1345
+ function nullableBoolean(value) {
1346
+ return value == null ? null : Number(value) === 1;
1347
+ }
1348
+ function parseNullableJson(value) {
1349
+ if (value == null) return null;
1350
+ try {
1351
+ return JSON.parse(String(value));
1352
+ } catch {
1353
+ return null;
1354
+ }
1355
+ }
1356
+ function rowToHarvestAttemptLog(row) {
1357
+ return {
1358
+ id: String(row.id),
1359
+ job_id: String(row.job_id),
1360
+ user_id: Number(row.user_id),
1361
+ attempt_number: Number(row.attempt_number),
1362
+ max_attempts: Number(row.max_attempts),
1363
+ query: String(row.query),
1364
+ location: row.location != null ? String(row.location) : null,
1365
+ max_questions: Number(row.max_questions),
1366
+ status: String(row.status),
1367
+ outcome: row.outcome != null ? String(row.outcome) : null,
1368
+ kernel_session_id: row.kernel_session_id != null ? String(row.kernel_session_id) : null,
1369
+ question_count: row.question_count != null ? Number(row.question_count) : null,
1370
+ duration_ms: row.duration_ms != null ? Number(row.duration_ms) : null,
1371
+ error: row.error != null ? String(row.error) : null,
1372
+ will_retry: nullableBoolean(row.will_retry),
1373
+ kernel_delete_started: Number(row.kernel_delete_started ?? 0) === 1,
1374
+ kernel_delete_succeeded: nullableBoolean(row.kernel_delete_succeeded),
1375
+ kernel_delete_error: row.kernel_delete_error != null ? String(row.kernel_delete_error) : null,
1376
+ browser_close_succeeded: nullableBoolean(row.browser_close_succeeded),
1377
+ browser_close_error: row.browser_close_error != null ? String(row.browser_close_error) : null,
1378
+ debug: parseNullableJson(row.debug_json),
1379
+ created_at: String(row.created_at),
1380
+ completed_at: row.completed_at != null ? String(row.completed_at) : null
1381
+ };
1382
+ }
1383
+ function deserialize(raw) {
1384
+ return {
1385
+ ...raw,
1386
+ options: JSON.parse(raw.options),
1387
+ result: raw.result ? JSON.parse(raw.result) : null
1388
+ };
1389
+ }
1390
+ async function createJob(userId, query, options, callbackUrl) {
1391
+ const id = randomUUID();
1392
+ await getDb().execute({
1393
+ sql: "INSERT INTO jobs (id, user_id, query, options, callback_url) VALUES (?, ?, ?, ?, ?)",
1394
+ args: [id, userId, query, JSON.stringify(options), callbackUrl ?? null]
1395
+ });
1396
+ return id;
1397
+ }
1398
+ async function createRunningJob(userId, query, options) {
1399
+ const id = randomUUID();
1400
+ await getDb().execute({
1401
+ sql: `INSERT INTO jobs (id, user_id, query, options, status, started_at) VALUES (?, ?, ?, ?, 'running', datetime('now'))`,
1402
+ args: [id, userId, query, JSON.stringify(options)]
1403
+ });
1404
+ return id;
1405
+ }
1406
+ async function startHarvestAttempt(input) {
1407
+ await getDb().execute({
1408
+ sql: `
1409
+ INSERT INTO harvest_attempts (
1410
+ id, job_id, user_id, attempt_number, max_attempts, query, location, max_questions, status, created_at
1411
+ )
1412
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'running', ?)
1413
+ ON CONFLICT(job_id, attempt_number) DO UPDATE SET
1414
+ status = 'running',
1415
+ created_at = excluded.created_at,
1416
+ completed_at = NULL,
1417
+ outcome = NULL,
1418
+ error = NULL,
1419
+ debug_json = NULL
1420
+ `,
1421
+ args: [
1422
+ randomUUID(),
1423
+ input.jobId,
1424
+ input.userId,
1425
+ input.attemptNumber,
1426
+ input.maxAttempts,
1427
+ input.query,
1428
+ input.location,
1429
+ input.maxQuestions,
1430
+ input.startedAt
1431
+ ]
1432
+ });
1433
+ }
1434
+ async function finishHarvestAttempt(input) {
1435
+ await getDb().execute({
1436
+ sql: `
1437
+ UPDATE harvest_attempts SET
1438
+ status = 'finished',
1439
+ outcome = ?,
1440
+ kernel_session_id = ?,
1441
+ question_count = ?,
1442
+ duration_ms = ?,
1443
+ error = ?,
1444
+ will_retry = ?,
1445
+ kernel_delete_started = ?,
1446
+ kernel_delete_succeeded = ?,
1447
+ kernel_delete_error = ?,
1448
+ browser_close_succeeded = ?,
1449
+ browser_close_error = ?,
1450
+ debug_json = ?,
1451
+ completed_at = ?
1452
+ WHERE job_id = ? AND attempt_number = ?
1453
+ `,
1454
+ args: [
1455
+ input.outcome,
1456
+ input.kernelSessionId,
1457
+ input.questionCount,
1458
+ input.durationMs,
1459
+ input.error,
1460
+ input.willRetry ? 1 : 0,
1461
+ input.kernelDeleteStarted ? 1 : 0,
1462
+ input.kernelDeleteSucceeded == null ? null : input.kernelDeleteSucceeded ? 1 : 0,
1463
+ input.kernelDeleteError,
1464
+ input.browserCloseSucceeded == null ? null : input.browserCloseSucceeded ? 1 : 0,
1465
+ input.browserCloseError,
1466
+ input.debug == null ? null : JSON.stringify(input.debug),
1467
+ input.completedAt,
1468
+ input.jobId,
1469
+ input.attemptNumber
1470
+ ]
1471
+ });
1472
+ }
1473
+ async function listHarvestAttempts(jobId, userId) {
1474
+ const res = userId == null ? await getDb().execute({
1475
+ sql: `SELECT * FROM harvest_attempts WHERE job_id = ? ORDER BY attempt_number ASC`,
1476
+ args: [jobId]
1477
+ }) : await getDb().execute({
1478
+ sql: `
1479
+ SELECT ha.*
1480
+ FROM harvest_attempts ha
1481
+ INNER JOIN jobs j ON j.id = ha.job_id
1482
+ WHERE ha.job_id = ? AND j.user_id = ?
1483
+ ORDER BY ha.attempt_number ASC
1484
+ `,
1485
+ args: [jobId, userId]
1486
+ });
1487
+ return res.rows.map((r) => rowToHarvestAttemptLog(r));
1488
+ }
1489
+ async function getJob(id, userId) {
1490
+ const db = getDb();
1491
+ const res = userId ? await db.execute({ sql: "SELECT * FROM jobs WHERE id = ? AND user_id = ?", args: [id, userId] }) : await db.execute({ sql: "SELECT * FROM jobs WHERE id = ?", args: [id] });
1492
+ return res.rows[0] ? deserialize(rowToRawJob(res.rows[0])) : void 0;
1493
+ }
1494
+ async function listJobs(userId) {
1495
+ const res = await getDb().execute({ sql: "SELECT * FROM jobs WHERE user_id = ? ORDER BY created_at DESC LIMIT 50", args: [userId] });
1496
+ return res.rows.map((r) => deserialize(rowToRawJob(r)));
1497
+ }
1498
+ async function getPageSnapshot(userId, url) {
1499
+ const res = await getDb().execute({
1500
+ sql: "SELECT content_hash, content, title, content_bytes, truncated, checked_at FROM page_snapshots WHERE user_id = ? AND url = ?",
1501
+ args: [userId, url]
1502
+ });
1503
+ const row = res.rows[0];
1504
+ if (!row) return null;
1505
+ return {
1506
+ contentHash: String(row.content_hash),
1507
+ content: String(row.content),
1508
+ title: row.title == null ? null : String(row.title),
1509
+ contentBytes: Number(row.content_bytes),
1510
+ truncated: Number(row.truncated) === 1,
1511
+ checkedAt: String(row.checked_at)
1512
+ };
1513
+ }
1514
+ async function upsertPageSnapshot(userId, url, snapshot) {
1515
+ await getDb().execute({
1516
+ sql: `
1517
+ INSERT INTO page_snapshots (user_id, url, content_hash, content, title, content_bytes, truncated, checked_at)
1518
+ VALUES (?, ?, ?, ?, ?, ?, ?, datetime('now'))
1519
+ ON CONFLICT(user_id, url) DO UPDATE SET
1520
+ content_hash = excluded.content_hash,
1521
+ content = excluded.content,
1522
+ title = excluded.title,
1523
+ content_bytes = excluded.content_bytes,
1524
+ truncated = excluded.truncated,
1525
+ checked_at = excluded.checked_at
1526
+ `,
1527
+ args: [userId, url, snapshot.contentHash, snapshot.content, snapshot.title, snapshot.contentBytes, snapshot.truncated ? 1 : 0]
1528
+ });
1529
+ }
1530
+ var REQUEST_EVENT_RESULT_MAX_BYTES = 32 * 1024;
1531
+ function serializeRequestEventResult(result) {
1532
+ if (result == null) return null;
1533
+ const serialized = JSON.stringify(result);
1534
+ const bytes = Buffer.byteLength(serialized, "utf8");
1535
+ if (bytes <= REQUEST_EVENT_RESULT_MAX_BYTES) return serialized;
1536
+ return JSON.stringify({
1537
+ truncated: true,
1538
+ originalBytes: bytes,
1539
+ sha256: createHash("sha256").update(serialized).digest("hex")
1540
+ });
1541
+ }
1542
+ function rowToRequestEvent(row) {
1543
+ const rawResult = row.result != null ? String(row.result) : null;
1544
+ return {
1545
+ id: String(row.id),
1546
+ user_id: Number(row.user_id),
1547
+ source: String(row.source),
1548
+ status: String(row.status),
1549
+ query: String(row.query),
1550
+ location: row.location != null ? String(row.location) : null,
1551
+ result_count: row.result_count != null ? Number(row.result_count) : null,
1552
+ result: rawResult ? JSON.parse(rawResult) : null,
1553
+ error: row.error != null ? String(row.error) : null,
1554
+ created_at: String(row.created_at)
1555
+ };
1556
+ }
1557
+ async function logRequestEvent(input) {
1558
+ const id = randomUUID();
1559
+ await getDb().execute({
1560
+ sql: `
1561
+ INSERT INTO request_events (id, user_id, source, status, query, location, result_count, result, error)
1562
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1563
+ `,
1564
+ args: [
1565
+ id,
1566
+ input.userId,
1567
+ input.source,
1568
+ input.status,
1569
+ input.query,
1570
+ input.location ?? null,
1571
+ input.resultCount ?? null,
1572
+ serializeRequestEventResult(input.result),
1573
+ input.error ?? null
1574
+ ]
1575
+ });
1576
+ return id;
1577
+ }
1578
+ async function listRequestEvents(userId, limit = 100) {
1579
+ const res = await getDb().execute({
1580
+ sql: `
1581
+ SELECT id, user_id, source, status, query, location, result_count, NULL AS result, error, created_at
1582
+ FROM request_events
1583
+ WHERE user_id = ?
1584
+ ORDER BY created_at DESC
1585
+ LIMIT ?
1586
+ `,
1587
+ args: [userId, limit]
1588
+ });
1589
+ return res.rows.map((r) => rowToRequestEvent(r));
1590
+ }
1591
+ function parseJsonRecordValue(value) {
1592
+ if (value == null) return {};
1593
+ try {
1594
+ const parsed = JSON.parse(String(value));
1595
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
1596
+ } catch {
1597
+ return {};
1598
+ }
1599
+ }
1600
+ function rowToWorkflowSchedule(row) {
1601
+ return {
1602
+ id: String(row.id),
1603
+ user_id: Number(row.user_id),
1604
+ workflow_id: String(row.workflow_id),
1605
+ name: String(row.name),
1606
+ input: parseJsonRecordValue(row.input_json),
1607
+ cadence: String(row.cadence),
1608
+ cron_expr: row.cron_expr != null ? String(row.cron_expr) : null,
1609
+ timezone: String(row.timezone ?? "UTC"),
1610
+ webhook_url: row.webhook_url != null ? String(row.webhook_url) : null,
1611
+ status: String(row.status),
1612
+ next_run_at: row.next_run_at != null ? String(row.next_run_at) : null,
1613
+ last_run_at: row.last_run_at != null ? String(row.last_run_at) : null,
1614
+ created_at: String(row.created_at),
1615
+ updated_at: String(row.updated_at)
1616
+ };
1617
+ }
1618
+ function rowToWorkflowRun(row) {
1619
+ return {
1620
+ id: String(row.id),
1621
+ user_id: Number(row.user_id),
1622
+ schedule_id: row.schedule_id != null ? String(row.schedule_id) : null,
1623
+ workflow_id: String(row.workflow_id),
1624
+ input: parseJsonRecordValue(row.input_json),
1625
+ status: String(row.status),
1626
+ manifest: row.manifest_json != null ? parseNullableJson(row.manifest_json) : null,
1627
+ error_message: row.error_message != null ? String(row.error_message) : null,
1628
+ queued_at: String(row.queued_at),
1629
+ started_at: row.started_at != null ? String(row.started_at) : null,
1630
+ completed_at: row.completed_at != null ? String(row.completed_at) : null,
1631
+ idempotency_key: row.idempotency_key != null ? String(row.idempotency_key) : null,
1632
+ step_index: row.step_index != null ? Number(row.step_index) : 0,
1633
+ total_steps: row.total_steps != null ? Number(row.total_steps) : null,
1634
+ step_log: parseWorkflowStepLog(row.step_log_json),
1635
+ webhook_url: row.webhook_url != null ? String(row.webhook_url) : null
1636
+ };
1637
+ }
1638
+ function parseWorkflowStepLog(value) {
1639
+ if (value == null) return [];
1640
+ try {
1641
+ const parsed = JSON.parse(String(value));
1642
+ return Array.isArray(parsed) ? parsed : [];
1643
+ } catch {
1644
+ return [];
1645
+ }
1646
+ }
1647
+ function rowToWorkflowArtifact(row) {
1648
+ return {
1649
+ id: String(row.id),
1650
+ run_id: String(row.run_id),
1651
+ user_id: Number(row.user_id),
1652
+ kind: String(row.kind),
1653
+ label: String(row.label),
1654
+ path: String(row.path),
1655
+ content_type: String(row.content_type),
1656
+ bytes: row.bytes != null ? Number(row.bytes) : null,
1657
+ rows_count: row.rows_count != null ? Number(row.rows_count) : null,
1658
+ content: row.content != null ? String(row.content) : null,
1659
+ created_at: String(row.created_at)
1660
+ };
1661
+ }
1662
+ async function createWorkflowSchedule(input) {
1663
+ const id = randomUUID();
1664
+ await getDb().execute({
1665
+ sql: `
1666
+ INSERT INTO workflow_schedules (
1667
+ id, user_id, workflow_id, name, input_json, cadence, timezone, webhook_url, next_run_at
1668
+ )
1669
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1670
+ `,
1671
+ args: [
1672
+ id,
1673
+ input.userId,
1674
+ input.workflowId,
1675
+ input.name,
1676
+ JSON.stringify(input.workflowInput),
1677
+ input.cadence,
1678
+ input.timezone ?? "UTC",
1679
+ input.webhookUrl ?? null,
1680
+ input.nextRunAt ?? null
1681
+ ]
1682
+ });
1683
+ const schedule = await getWorkflowSchedule(id, input.userId);
1684
+ if (!schedule) throw new Error("Failed to create workflow schedule");
1685
+ return schedule;
1686
+ }
1687
+ async function getWorkflowSchedule(id, userId) {
1688
+ const res = userId == null ? await getDb().execute({ sql: "SELECT * FROM workflow_schedules WHERE id = ? AND status != ? LIMIT 1", args: [id, "deleted"] }) : await getDb().execute({ sql: "SELECT * FROM workflow_schedules WHERE id = ? AND user_id = ? AND status != ? LIMIT 1", args: [id, userId, "deleted"] });
1689
+ return res.rows[0] ? rowToWorkflowSchedule(res.rows[0]) : null;
1690
+ }
1691
+ async function listWorkflowSchedules(userId) {
1692
+ const res = await getDb().execute({
1693
+ sql: `SELECT * FROM workflow_schedules WHERE user_id = ? AND status != 'deleted' ORDER BY created_at DESC LIMIT 100`,
1694
+ args: [userId]
1695
+ });
1696
+ return res.rows.map((r) => rowToWorkflowSchedule(r));
1697
+ }
1698
+ async function patchWorkflowSchedule(input) {
1699
+ const sets = [];
1700
+ const args = [];
1701
+ if (input.name !== void 0) {
1702
+ sets.push("name = ?");
1703
+ args.push(input.name);
1704
+ }
1705
+ if (input.status !== void 0) {
1706
+ sets.push("status = ?");
1707
+ args.push(input.status);
1708
+ }
1709
+ if (input.workflowInput !== void 0) {
1710
+ sets.push("input_json = ?");
1711
+ args.push(JSON.stringify(input.workflowInput));
1712
+ }
1713
+ if (input.cadence !== void 0) {
1714
+ sets.push("cadence = ?");
1715
+ args.push(input.cadence);
1716
+ }
1717
+ if (input.timezone !== void 0) {
1718
+ sets.push("timezone = ?");
1719
+ args.push(input.timezone);
1720
+ }
1721
+ if (input.webhookUrl !== void 0) {
1722
+ sets.push("webhook_url = ?");
1723
+ args.push(input.webhookUrl);
1724
+ }
1725
+ if (input.nextRunAt !== void 0) {
1726
+ sets.push("next_run_at = ?");
1727
+ args.push(input.nextRunAt);
1728
+ }
1729
+ if (sets.length === 0) return getWorkflowSchedule(input.id, input.userId);
1730
+ sets.push(`updated_at = datetime('now')`);
1731
+ await getDb().execute({
1732
+ sql: `UPDATE workflow_schedules SET ${sets.join(", ")} WHERE id = ? AND user_id = ? AND status != 'deleted'`,
1733
+ args: [...args, input.id, input.userId]
1734
+ });
1735
+ return getWorkflowSchedule(input.id, input.userId);
1736
+ }
1737
+ async function deleteWorkflowSchedule(id, userId) {
1738
+ const res = await getDb().execute({
1739
+ sql: `UPDATE workflow_schedules SET status = 'deleted', updated_at = datetime('now') WHERE id = ? AND user_id = ? AND status != 'deleted'`,
1740
+ args: [id, userId]
1741
+ });
1742
+ return res.rowsAffected > 0;
1743
+ }
1744
+ async function listDueWorkflowSchedules(nowIso, limit = 10) {
1745
+ const res = await getDb().execute({
1746
+ sql: `
1747
+ SELECT * FROM workflow_schedules
1748
+ WHERE status = 'active' AND next_run_at IS NOT NULL AND next_run_at <= ?
1749
+ ORDER BY next_run_at ASC
1750
+ LIMIT ?
1751
+ `,
1752
+ args: [nowIso, limit]
1753
+ });
1754
+ return res.rows.map((r) => rowToWorkflowSchedule(r));
1755
+ }
1756
+ async function markWorkflowScheduleRan(id, lastRunAt, nextRunAt) {
1757
+ await getDb().execute({
1758
+ sql: `UPDATE workflow_schedules SET last_run_at = ?, next_run_at = ?, updated_at = datetime('now') WHERE id = ?`,
1759
+ args: [lastRunAt, nextRunAt, id]
1760
+ });
1761
+ }
1762
+ async function createWorkflowRun(input) {
1763
+ const id = input.id ?? randomUUID();
1764
+ const args = [
1765
+ id,
1766
+ input.userId,
1767
+ input.scheduleId ?? null,
1768
+ input.workflowId,
1769
+ JSON.stringify(input.workflowInput),
1770
+ input.idempotencyKey ?? null,
1771
+ input.webhookUrl ?? null,
1772
+ input.totalSteps ?? null
1773
+ ];
1774
+ try {
1775
+ await getDb().execute({
1776
+ sql: `
1777
+ INSERT INTO workflow_runs (id, user_id, schedule_id, workflow_id, input_json, idempotency_key, webhook_url, total_steps)
1778
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
1779
+ `,
1780
+ args
1781
+ });
1782
+ } catch (err) {
1783
+ if (input.idempotencyKey) {
1784
+ const existing = await getWorkflowRunByIdempotencyKey(input.idempotencyKey);
1785
+ if (existing) return existing;
1786
+ }
1787
+ throw err;
1788
+ }
1789
+ const run = await getWorkflowRun(id, input.userId);
1790
+ if (!run) throw new Error("Failed to create workflow run");
1791
+ return run;
1792
+ }
1793
+ async function getWorkflowRun(id, userId) {
1794
+ const res = userId == null ? await getDb().execute({ sql: "SELECT * FROM workflow_runs WHERE id = ? LIMIT 1", args: [id] }) : await getDb().execute({ sql: "SELECT * FROM workflow_runs WHERE id = ? AND user_id = ? LIMIT 1", args: [id, userId] });
1795
+ return res.rows[0] ? rowToWorkflowRun(res.rows[0]) : null;
1796
+ }
1797
+ async function getWorkflowRunByIdempotencyKey(idempotencyKey) {
1798
+ const res = await getDb().execute({ sql: "SELECT * FROM workflow_runs WHERE idempotency_key = ? LIMIT 1", args: [idempotencyKey] });
1799
+ return res.rows[0] ? rowToWorkflowRun(res.rows[0]) : null;
1800
+ }
1801
+ async function listWorkflowRuns(userId, limit = 50) {
1802
+ const res = await getDb().execute({
1803
+ sql: "SELECT * FROM workflow_runs WHERE user_id = ? ORDER BY queued_at DESC LIMIT ?",
1804
+ args: [userId, limit]
1805
+ });
1806
+ return res.rows.map((r) => rowToWorkflowRun(r));
1807
+ }
1808
+ async function markWorkflowRunRunning(id) {
1809
+ await getDb().execute({
1810
+ sql: `UPDATE workflow_runs SET status = 'running', started_at = COALESCE(started_at, datetime('now')) WHERE id = ?`,
1811
+ args: [id]
1812
+ });
1813
+ }
1814
+ async function completeWorkflowRunRecord(id, status, manifest) {
1815
+ await getDb().execute({
1816
+ sql: `UPDATE workflow_runs SET status = ?, manifest_json = ?, completed_at = datetime('now') WHERE id = ?`,
1817
+ args: [status, JSON.stringify(manifest), id]
1818
+ });
1819
+ }
1820
+ async function failWorkflowRunRecord(id, errorMessage, manifest) {
1821
+ await getDb().execute({
1822
+ sql: `UPDATE workflow_runs SET status = 'failed', error_message = ?, manifest_json = ?, completed_at = datetime('now') WHERE id = ?`,
1823
+ args: [errorMessage, manifest == null ? null : JSON.stringify(manifest), id]
1824
+ });
1825
+ }
1826
+ function workflowArtifactContentType(kind) {
1827
+ if (kind === "html_report") return "text/html; charset=utf-8";
1828
+ if (kind === "csv") return "text/csv; charset=utf-8";
1829
+ if (kind === "json") return "application/json; charset=utf-8";
1830
+ return "text/plain; charset=utf-8";
1831
+ }
1832
+ async function insertWorkflowArtifact(runId, userId, artifact) {
1833
+ await getDb().execute({
1834
+ sql: `
1835
+ INSERT INTO workflow_artifacts (id, run_id, user_id, kind, label, path, content_type, bytes, rows_count, content)
1836
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1837
+ `,
1838
+ args: [
1839
+ randomUUID(),
1840
+ runId,
1841
+ userId,
1842
+ artifact.kind,
1843
+ artifact.label,
1844
+ artifact.path,
1845
+ workflowArtifactContentType(artifact.kind),
1846
+ artifact.bytes ?? null,
1847
+ artifact.rows ?? null,
1848
+ artifact.content ?? null
1849
+ ]
1850
+ });
1851
+ }
1852
+ async function replaceWorkflowArtifacts(runId, userId, artifacts) {
1853
+ await getDb().execute({ sql: "DELETE FROM workflow_artifacts WHERE run_id = ? AND user_id = ?", args: [runId, userId] });
1854
+ for (const artifact of artifacts) await insertWorkflowArtifact(runId, userId, artifact);
1855
+ return listWorkflowArtifacts(runId, userId);
1856
+ }
1857
+ async function appendWorkflowArtifacts(runId, userId, artifacts) {
1858
+ for (const artifact of artifacts) await insertWorkflowArtifact(runId, userId, artifact);
1859
+ return listWorkflowArtifacts(runId, userId);
1860
+ }
1861
+ async function getWorkflowRunStepState(id, userId) {
1862
+ const res = userId == null ? await getDb().execute({ sql: "SELECT step_index, total_steps, step_state_json, step_log_json FROM workflow_runs WHERE id = ? LIMIT 1", args: [id] }) : await getDb().execute({ sql: "SELECT step_index, total_steps, step_state_json, step_log_json FROM workflow_runs WHERE id = ? AND user_id = ? LIMIT 1", args: [id, userId] });
1863
+ const row = res.rows[0];
1864
+ if (!row) return null;
1865
+ return {
1866
+ stepIndex: row.step_index != null ? Number(row.step_index) : 0,
1867
+ totalSteps: row.total_steps != null ? Number(row.total_steps) : null,
1868
+ state: row.step_state_json != null ? parseNullableJson(row.step_state_json) : null,
1869
+ stepLog: parseWorkflowStepLog(row.step_log_json)
1870
+ };
1871
+ }
1872
+ async function advanceWorkflowRunStep(id, input) {
1873
+ const stepLog = [...input.appendLog, input.logEntry];
1874
+ await getDb().execute({
1875
+ sql: `UPDATE workflow_runs SET status = ?, step_index = ?, total_steps = ?, step_state_json = ?, step_log_json = ?, started_at = COALESCE(started_at, datetime('now')) WHERE id = ?`,
1876
+ args: [input.status, input.stepIndex, input.totalSteps, JSON.stringify(input.state ?? null), JSON.stringify(stepLog), id]
1877
+ });
1878
+ }
1879
+ async function listWorkflowArtifacts(runId, userId) {
1880
+ const res = await getDb().execute({
1881
+ sql: "SELECT * FROM workflow_artifacts WHERE run_id = ? AND user_id = ? ORDER BY created_at ASC",
1882
+ args: [runId, userId]
1883
+ });
1884
+ return res.rows.map((r) => rowToWorkflowArtifact(r));
1885
+ }
1886
+ async function getWorkflowArtifact(id, userId) {
1887
+ const res = await getDb().execute({
1888
+ sql: "SELECT * FROM workflow_artifacts WHERE id = ? AND user_id = ? LIMIT 1",
1889
+ args: [id, userId]
1890
+ });
1891
+ return res.rows[0] ? rowToWorkflowArtifact(res.rows[0]) : null;
1892
+ }
1893
+ async function recordWorkflowWebhookDelivery(input) {
1894
+ await getDb().execute({
1895
+ sql: `
1896
+ INSERT INTO workflow_webhook_deliveries (
1897
+ id, run_id, schedule_id, user_id, webhook_url, status, attempts, last_error, delivered_at
1898
+ )
1899
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
1900
+ `,
1901
+ args: [
1902
+ randomUUID(),
1903
+ input.runId,
1904
+ input.scheduleId ?? null,
1905
+ input.userId,
1906
+ input.webhookUrl,
1907
+ input.status,
1908
+ input.attempts,
1909
+ input.lastError ?? null,
1910
+ input.status === "delivered" ? (/* @__PURE__ */ new Date()).toISOString() : null
1911
+ ]
1912
+ });
1913
+ }
1914
+ async function countActiveUsers() {
1915
+ const res = await getDb().execute(`SELECT COUNT(*) as n FROM users WHERE active = 1`);
1916
+ return Number(res.rows[0]?.n ?? 0);
1917
+ }
1918
+ async function countActiveJobsForUser(userId) {
1919
+ const res = await getDb().execute({
1920
+ sql: `SELECT COUNT(*) as n FROM jobs WHERE user_id = ? AND (status = 'pending' OR (status = 'running' AND started_at > datetime('now', '-10 minutes')))`,
1921
+ args: [userId]
1922
+ });
1923
+ return Number(res.rows[0]?.n ?? 0);
1924
+ }
1925
+ async function claimPendingJob() {
1926
+ const db = getDb();
1927
+ const res = await db.execute(`SELECT * FROM jobs WHERE status = 'pending' ORDER BY created_at LIMIT 1`);
1928
+ const job = res.rows[0] ? rowToRawJob(res.rows[0]) : void 0;
1929
+ if (!job) return void 0;
1930
+ const upd = await db.execute({
1931
+ sql: `UPDATE jobs SET status = 'running', started_at = datetime('now') WHERE id = ? AND status = 'pending'`,
1932
+ args: [job.id]
1933
+ });
1934
+ return upd.rowsAffected === 0 ? void 0 : { ...job, status: "running" };
1935
+ }
1936
+ async function completeJob(id, result) {
1937
+ await getDb().execute({ sql: `UPDATE jobs SET status = 'done', result = ?, completed_at = datetime('now') WHERE id = ?`, args: [JSON.stringify(result), id] });
1938
+ }
1939
+ async function failJob(id, error) {
1940
+ await getDb().execute({ sql: `UPDATE jobs SET status = 'failed', error = ?, completed_at = datetime('now') WHERE id = ?`, args: [error, id] });
1941
+ }
1942
+ async function cancelJob(id, reason) {
1943
+ await getDb().execute({ sql: `UPDATE jobs SET status = 'cancelled', error = ?, completed_at = datetime('now') WHERE id = ?`, args: [reason, id] });
1944
+ }
1945
+ function rowToKpoJob(row) {
1946
+ return {
1947
+ id: String(row.id),
1948
+ user_id: Number(row.user_id),
1949
+ status: String(row.status),
1950
+ entity_name: String(row.entity_name),
1951
+ entity_type: String(row.entity_type),
1952
+ url: row.url != null ? String(row.url) : null,
1953
+ request: String(row.request),
1954
+ result: row.result != null ? String(row.result) : null,
1955
+ error: row.error != null ? String(row.error) : null,
1956
+ created_at: String(row.created_at),
1957
+ updated_at: String(row.updated_at)
1958
+ };
1959
+ }
1960
+ function rowToKpoPhaseLog(row) {
1961
+ return {
1962
+ id: Number(row.id),
1963
+ job_id: String(row.job_id),
1964
+ phase: String(row.phase),
1965
+ output: String(row.output),
1966
+ created_at: String(row.created_at)
1967
+ };
1968
+ }
1969
+ async function createKpoJob(jobId, userId, entityName, entityType, request, url) {
1970
+ await getDb().execute({
1971
+ sql: `INSERT INTO kpo_jobs (id, user_id, status, entity_name, entity_type, url, request) VALUES (?, ?, 'pending', ?, ?, ?, ?)`,
1972
+ args: [jobId, userId, entityName, entityType, url ?? null, JSON.stringify(request)]
1973
+ });
1974
+ }
1975
+ async function claimPendingKpoJob() {
1976
+ const db = getDb();
1977
+ const upd = await db.execute(
1978
+ `UPDATE kpo_jobs SET status='running', updated_at=datetime('now') WHERE id = (SELECT id FROM kpo_jobs WHERE status='pending' ORDER BY created_at ASC LIMIT 1) RETURNING *`
1979
+ );
1980
+ if (upd.rowsAffected === 0) return null;
1981
+ return upd.rows[0] ? rowToKpoJob(upd.rows[0]) : null;
1982
+ }
1983
+ async function getKpoJob(jobId) {
1984
+ const res = await getDb().execute({ sql: `SELECT * FROM kpo_jobs WHERE id = ?`, args: [jobId] });
1985
+ return res.rows[0] ? rowToKpoJob(res.rows[0]) : null;
1986
+ }
1987
+ async function updateKpoJobState(jobId, status) {
1988
+ await getDb().execute({ sql: `UPDATE kpo_jobs SET status = ?, updated_at = datetime('now') WHERE id = ?`, args: [status, jobId] });
1989
+ }
1990
+ async function completeKpoJob(jobId, result) {
1991
+ await getDb().execute({ sql: `UPDATE kpo_jobs SET status = 'done', result = ?, updated_at = datetime('now') WHERE id = ?`, args: [JSON.stringify(result), jobId] });
1992
+ }
1993
+ async function failKpoJob(jobId, error) {
1994
+ await getDb().execute({ sql: `UPDATE kpo_jobs SET status = 'failed', error = ?, updated_at = datetime('now') WHERE id = ?`, args: [error, jobId] });
1995
+ }
1996
+ async function logKpoPhaseComplete(jobId, phase, output) {
1997
+ await getDb().execute({
1998
+ sql: `INSERT INTO kpo_phase_log (job_id, phase, output) VALUES (?, ?, ?)`,
1999
+ args: [jobId, phase, JSON.stringify(output)]
2000
+ });
2001
+ }
2002
+ async function getKpoPhaseLog(jobId) {
2003
+ const res = await getDb().execute({ sql: `SELECT * FROM kpo_phase_log WHERE job_id = ? ORDER BY created_at ASC`, args: [jobId] });
2004
+ return res.rows.map((r) => rowToKpoPhaseLog(r));
2005
+ }
2006
+ async function listKpoJobs(userId) {
2007
+ const res = userId != null ? await getDb().execute({ sql: `SELECT * FROM kpo_jobs WHERE user_id = ? ORDER BY created_at DESC LIMIT 50`, args: [userId] }) : await getDb().execute(`SELECT * FROM kpo_jobs ORDER BY created_at DESC LIMIT 50`);
2008
+ return res.rows.map((r) => rowToKpoJob(r));
2009
+ }
2010
+ async function getUserByStripeCustomerId(customerId) {
2011
+ const res = await getDb().execute({
2012
+ sql: "SELECT * FROM users WHERE stripe_customer_id = ? AND active = 1",
2013
+ args: [customerId]
2014
+ });
2015
+ return res.rows[0] ? rowToUser(res.rows[0]) : void 0;
2016
+ }
2017
+ async function setStripeCustomerId(userId, customerId) {
2018
+ await getDb().execute({
2019
+ sql: "UPDATE users SET stripe_customer_id = ? WHERE id = ?",
2020
+ args: [customerId, userId]
2021
+ });
2022
+ }
2023
+ async function setExtraConcurrencySlots(userId, slots) {
2024
+ await getDb().execute({
2025
+ sql: "UPDATE users SET extra_concurrency_slots = ? WHERE id = ?",
2026
+ args: [Math.max(0, slots), userId]
2027
+ });
2028
+ }
2029
+ async function setConcurrencySubId(userId, subId) {
2030
+ await getDb().execute({
2031
+ sql: "UPDATE users SET concurrency_stripe_sub_id = ? WHERE id = ?",
2032
+ args: [subId, userId]
2033
+ });
2034
+ }
2035
+ async function setSubscriptionTier(userId, tier, concurrency, subId) {
2036
+ await getDb().execute({
2037
+ sql: "UPDATE users SET subscription_tier = ?, subscription_concurrency = ?, subscription_id = ? WHERE id = ?",
2038
+ args: [tier, Math.max(0, Math.round(concurrency)), subId, userId]
2039
+ });
2040
+ }
2041
+ function rowToConnectedAccountBillingState(row) {
2042
+ return {
2043
+ user_id: Number(row.user_id),
2044
+ stripe_subscription_id: row.stripe_subscription_id != null ? String(row.stripe_subscription_id) : null,
2045
+ stripe_subscription_item_id: row.stripe_subscription_item_id != null ? String(row.stripe_subscription_item_id) : null,
2046
+ price_id: row.price_id != null ? String(row.price_id) : null,
2047
+ quantity: Math.max(0, Number(row.quantity ?? 0)),
2048
+ status: ["pending", "synced", "blocked", "error"].includes(String(row.status)) ? String(row.status) : "error",
2049
+ last_error_code: row.last_error_code != null ? String(row.last_error_code) : null,
2050
+ checked_at: String(row.checked_at),
2051
+ synced_at: row.synced_at != null ? String(row.synced_at) : null,
2052
+ updated_at: String(row.updated_at)
2053
+ };
2054
+ }
2055
+ async function getConnectedAccountBillingState(userId) {
2056
+ const res = await getDb().execute({
2057
+ sql: "SELECT * FROM connected_account_billing WHERE user_id = ? LIMIT 1",
2058
+ args: [userId]
2059
+ });
2060
+ const row = res.rows[0];
2061
+ return row ? rowToConnectedAccountBillingState(row) : null;
2062
+ }
2063
+ async function setConnectedAccountBillingState(input) {
2064
+ const quantity = Math.max(0, Math.round(input.quantity));
2065
+ await getDb().execute({
2066
+ sql: `INSERT INTO connected_account_billing
2067
+ (user_id, stripe_subscription_id, stripe_subscription_item_id, price_id, quantity, status, last_error_code, checked_at, synced_at, updated_at)
2068
+ VALUES (?, ?, ?, ?, ?, ?, ?, datetime('now'), CASE WHEN ? THEN datetime('now') ELSE NULL END, datetime('now'))
2069
+ ON CONFLICT(user_id) DO UPDATE SET
2070
+ stripe_subscription_id = excluded.stripe_subscription_id,
2071
+ stripe_subscription_item_id = excluded.stripe_subscription_item_id,
2072
+ price_id = excluded.price_id,
2073
+ quantity = excluded.quantity,
2074
+ status = excluded.status,
2075
+ last_error_code = excluded.last_error_code,
2076
+ checked_at = datetime('now'),
2077
+ synced_at = CASE WHEN ? THEN datetime('now') ELSE connected_account_billing.synced_at END,
2078
+ updated_at = datetime('now')`,
2079
+ args: [
2080
+ input.userId,
2081
+ input.stripeSubscriptionId,
2082
+ input.stripeSubscriptionItemId,
2083
+ input.priceId,
2084
+ quantity,
2085
+ input.status,
2086
+ input.lastErrorCode ?? null,
2087
+ input.synced === true ? 1 : 0,
2088
+ input.synced === true ? 1 : 0
2089
+ ]
2090
+ });
2091
+ const state = await getConnectedAccountBillingState(input.userId);
2092
+ if (!state) throw new Error("connected account billing state was not persisted");
2093
+ return state;
2094
+ }
2095
+ async function listUsersDueForConnectedAccountBillingReconciliation(staleMinutes = 60, limit = 20) {
2096
+ const modifier = `-${Math.max(1, Math.round(staleMinutes))} minutes`;
2097
+ const res = await getDb().execute({
2098
+ sql: `SELECT u.*
2099
+ FROM users u
2100
+ LEFT JOIN connected_account_billing b ON b.user_id = u.id
2101
+ WHERE u.active = 1
2102
+ AND u.subscription_id IS NOT NULL
2103
+ AND (b.checked_at IS NULL OR b.checked_at <= datetime('now', ?))
2104
+ ORDER BY COALESCE(b.checked_at, u.created_at) ASC
2105
+ LIMIT ?`,
2106
+ args: [modifier, Math.max(1, Math.min(100, Math.round(limit)))]
2107
+ });
2108
+ return res.rows.map((row) => rowToUser(row));
2109
+ }
2110
+ async function setMemoryPlan(userId, plan, subscriptionId) {
2111
+ await getDb().execute({
2112
+ sql: "UPDATE users SET memory_plan = ?, memory_subscription_id = ? WHERE id = ?",
2113
+ args: [plan, subscriptionId, userId]
2114
+ });
2115
+ }
2116
+ async function getMemoryPlan(userId) {
2117
+ const res = await getDb().execute({ sql: "SELECT memory_plan, memory_subscription_id FROM users WHERE id = ?", args: [userId] });
2118
+ const row = res.rows[0];
2119
+ return {
2120
+ plan: row?.memory_plan != null ? String(row.memory_plan) : "free",
2121
+ memory_subscription_id: row?.memory_subscription_id != null ? String(row.memory_subscription_id) : null
2122
+ };
2123
+ }
2124
+ async function getMemoryProvisioned(userId) {
2125
+ const res = await getDb().execute({ sql: "SELECT memory_provisioned FROM users WHERE id = ?", args: [userId] });
2126
+ const row = res.rows[0];
2127
+ return Number(row?.memory_provisioned ?? 0) === 1;
2128
+ }
2129
+ async function setMemoryProvisioned(userId, provisioned) {
2130
+ await getDb().execute({ sql: "UPDATE users SET memory_provisioned = ? WHERE id = ?", args: [provisioned ? 1 : 0, userId] });
2131
+ }
2132
+ async function resolveUserByMemorySubscription(subscriptionId) {
2133
+ const res = await getDb().execute({ sql: "SELECT * FROM users WHERE memory_subscription_id = ? AND active = 1", args: [subscriptionId] });
2134
+ return res.rows[0] ? rowToUser(res.rows[0]) : void 0;
2135
+ }
2136
+ var CREDIT_LOT_TTL = "+3 months";
2137
+ async function ensureMigrated(userId) {
2138
+ const db = getDb();
2139
+ const inserted = await db.execute({
2140
+ sql: `INSERT INTO credit_lots (user_id, amount_mc, remaining_mc, source, expires_at)
2141
+ SELECT ?, legacy.balance_mc, legacy.balance_mc, 'migration', datetime('now', ?)
2142
+ FROM (
2143
+ SELECT COALESCE(SUM(amount_mc), 0) AS balance_mc
2144
+ FROM ledger
2145
+ WHERE user_id = ?
2146
+ ) AS legacy
2147
+ WHERE legacy.balance_mc > 0
2148
+ AND EXISTS (
2149
+ SELECT 1 FROM ledger
2150
+ WHERE user_id = ? AND created_at < ?
2151
+ )
2152
+ AND NOT EXISTS (
2153
+ SELECT 1 FROM credit_lots WHERE user_id = ?
2154
+ )`,
2155
+ args: [userId, CREDIT_LOT_TTL, userId, userId, CREDIT_LOTS_EPOCH, userId]
2156
+ });
2157
+ return inserted.rowsAffected;
2158
+ }
2159
+ async function expireOldLots() {
2160
+ const db = getDb();
2161
+ const lots = await db.execute({
2162
+ sql: `SELECT id
2163
+ FROM credit_lots
2164
+ WHERE remaining_mc > 0
2165
+ AND never_expires = 0
2166
+ AND expires_at <= datetime('now')
2167
+ LIMIT 500`
2168
+ });
2169
+ if (lots.rows.length === 0) return { expired_lots: 0, expired_mc: 0 };
2170
+ const claims = [];
2171
+ const stmts = [];
2172
+ for (const lot of lots.rows) {
2173
+ const lotId = Number(lot.id);
2174
+ const token = randomUUID();
2175
+ claims.push({ token, insertResultIndex: stmts.length });
2176
+ stmts.push({
2177
+ sql: `INSERT OR IGNORE INTO credit_lot_expirations (lot_id, user_id, amount_mc, claim_token)
2178
+ SELECT id, user_id, remaining_mc, ?
2179
+ FROM credit_lots
2180
+ WHERE id = ?
2181
+ AND remaining_mc > 0
2182
+ AND never_expires = 0
2183
+ AND expires_at <= datetime('now')`,
2184
+ args: [token, lotId]
2185
+ });
2186
+ stmts.push({
2187
+ sql: `UPDATE credit_lots
2188
+ SET remaining_mc = 0
2189
+ WHERE id = ?
2190
+ AND EXISTS (
2191
+ SELECT 1 FROM credit_lot_expirations
2192
+ WHERE lot_id = ? AND claim_token = ?
2193
+ )`,
2194
+ args: [lotId, lotId, token]
2195
+ });
2196
+ stmts.push({
2197
+ sql: `INSERT INTO ledger (user_id, amount_mc, operation, description)
2198
+ SELECT user_id, -amount_mc, 'credit_expired', 'credits expired'
2199
+ FROM credit_lot_expirations
2200
+ WHERE lot_id = ? AND claim_token = ?`,
2201
+ args: [lotId, token]
2202
+ });
2203
+ }
2204
+ const results = await db.batch(stmts, "write");
2205
+ const claimedTokens = claims.filter((claim) => results[claim.insertResultIndex]?.rowsAffected === 1).map((claim) => claim.token);
2206
+ if (claimedTokens.length === 0) return { expired_lots: 0, expired_mc: 0 };
2207
+ const placeholders = claimedTokens.map(() => "?").join(", ");
2208
+ const expired = await db.execute({
2209
+ sql: `SELECT user_id, amount_mc
2210
+ FROM credit_lot_expirations
2211
+ WHERE claim_token IN (${placeholders})`,
2212
+ args: claimedTokens
2213
+ });
2214
+ const users = new Set(expired.rows.map((row) => Number(row.user_id)));
2215
+ for (const uid of users) await reconcileBalanceMc(uid);
2216
+ return {
2217
+ expired_lots: expired.rows.length,
2218
+ expired_mc: expired.rows.reduce((sum, row) => sum + Number(row.amount_mc), 0)
2219
+ };
2220
+ }
2221
+ async function migrateExistingBalancesToLots() {
2222
+ const db = getDb();
2223
+ const res = await db.execute({
2224
+ sql: `SELECT DISTINCT u.id AS user_id
2225
+ FROM users u
2226
+ JOIN ledger l ON l.user_id = u.id
2227
+ WHERE l.created_at < ?
2228
+ AND NOT EXISTS (SELECT 1 FROM credit_lots WHERE user_id = u.id)`,
2229
+ args: [CREDIT_LOTS_EPOCH]
2230
+ });
2231
+ let migrated = 0;
2232
+ let totalMc = 0;
2233
+ for (const row of res.rows) {
2234
+ const uid = Number(row.user_id);
2235
+ const inserted = await ensureMigrated(uid);
2236
+ if (inserted === 0) continue;
2237
+ const lot = await db.execute({
2238
+ sql: `SELECT amount_mc FROM credit_lots
2239
+ WHERE user_id = ? AND source = 'migration'
2240
+ ORDER BY id DESC LIMIT 1`,
2241
+ args: [uid]
2242
+ });
2243
+ const amountMc = Number(lot.rows[0]?.amount_mc ?? 0);
2244
+ await reconcileBalanceMc(uid);
2245
+ migrated += 1;
2246
+ totalMc += amountMc;
2247
+ }
2248
+ return { migrated, total_mc: totalMc };
2249
+ }
2250
+ async function creditMc(userId, mc, operation, description, stripePaymentIntent, options) {
2251
+ const db = getDb();
2252
+ await ensureMigrated(userId);
2253
+ let neverExpires = options?.neverExpires;
2254
+ if (neverExpires === void 0 && mc > 0) {
2255
+ const tier = await db.execute({ sql: "SELECT subscription_tier FROM users WHERE id = ? LIMIT 1", args: [userId] });
2256
+ neverExpires = String(tier.rows[0]?.subscription_tier ?? "").startsWith("sonic");
2257
+ }
2258
+ const expiry = neverExpires ? "9999-12-31 23:59:59" : null;
2259
+ if (stripePaymentIntent) {
2260
+ const stmts2 = [{
2261
+ sql: "INSERT OR IGNORE INTO ledger (user_id, amount_mc, operation, description, stripe_pi) VALUES (?, ?, ?, ?, ?)",
2262
+ args: [userId, mc, operation, description ?? null, stripePaymentIntent]
2263
+ }];
2264
+ if (mc > 0) {
2265
+ stmts2.push({
2266
+ sql: `INSERT INTO credit_lots (user_id, amount_mc, remaining_mc, source, stripe_pi, expires_at, never_expires)
2267
+ SELECT ?, ?, ?, ?, ?, COALESCE(?, datetime('now', ?)), ?
2268
+ WHERE changes() = 1`,
2269
+ args: [userId, mc, mc, operation, stripePaymentIntent, expiry, CREDIT_LOT_TTL, neverExpires ? 1 : 0]
2270
+ });
2271
+ }
2272
+ stmts2.push({
2273
+ sql: "UPDATE users SET balance_mc = balance_mc + ? WHERE id = ? AND changes() = 1",
2274
+ args: [mc, userId]
2275
+ });
2276
+ await db.batch(stmts2, "write");
2277
+ const res2 = await db.execute({ sql: "SELECT balance_mc FROM users WHERE id = ?", args: [userId] });
2278
+ return Number(res2.rows[0]?.balance_mc ?? 0);
2279
+ }
2280
+ const stmts = [];
2281
+ if (mc > 0) {
2282
+ stmts.push({ sql: `INSERT INTO credit_lots (user_id, amount_mc, remaining_mc, source, stripe_pi, expires_at, never_expires) VALUES (?, ?, ?, ?, ?, COALESCE(?, datetime('now', ?)), ?)`, args: [userId, mc, mc, operation, null, expiry, CREDIT_LOT_TTL, neverExpires ? 1 : 0] });
2283
+ }
2284
+ stmts.push({ sql: "UPDATE users SET balance_mc = balance_mc + ? WHERE id = ?", args: [mc, userId] });
2285
+ stmts.push({ sql: "INSERT INTO ledger (user_id, amount_mc, operation, description, stripe_pi) VALUES (?, ?, ?, ?, ?)", args: [userId, mc, operation, description ?? null, null] });
2286
+ await db.batch(stmts, "write");
2287
+ const res = await db.execute({ sql: "SELECT balance_mc FROM users WHERE id = ?", args: [userId] });
2288
+ return Number(res.rows[0]?.balance_mc ?? 0);
2289
+ }
2290
+ async function creditMcIdempotent(userId, mc, operation, description, idempotencyKey, options) {
2291
+ if (!Number.isSafeInteger(mc) || mc <= 0) throw new Error("idempotent credit amount must be a positive integer");
2292
+ if (!idempotencyKey || idempotencyKey.length > 500) throw new Error("idempotent credit key is required and must be bounded");
2293
+ const db = getDb();
2294
+ await ensureMigrated(userId);
2295
+ let neverExpires = options?.neverExpires;
2296
+ if (neverExpires === void 0) {
2297
+ const tier = await db.execute({ sql: "SELECT subscription_tier FROM users WHERE id = ? LIMIT 1", args: [userId] });
2298
+ neverExpires = String(tier.rows[0]?.subscription_tier ?? "").startsWith("sonic");
2299
+ }
2300
+ const expiry = neverExpires ? "9999-12-31 23:59:59" : null;
2301
+ const readBalance = async () => {
2302
+ const res = await db.execute({ sql: "SELECT balance_mc FROM users WHERE id = ?", args: [userId] });
2303
+ return Number(res.rows[0]?.balance_mc ?? 0);
2304
+ };
2305
+ let results;
2306
+ try {
2307
+ results = await db.batch([
2308
+ {
2309
+ sql: "INSERT OR IGNORE INTO billing_credits (idempotency_key, user_id, amount_mc, operation, description, status) VALUES (?, ?, ?, ?, ?, 'claimed')",
2310
+ args: [idempotencyKey, userId, mc, operation, description]
2311
+ },
2312
+ {
2313
+ sql: "UPDATE billing_credits SET status = 'applying' WHERE idempotency_key = ? AND user_id = ? AND amount_mc = ? AND operation = ? AND description = ? AND status = 'claimed'",
2314
+ args: [idempotencyKey, userId, mc, operation, description]
2315
+ },
2316
+ {
2317
+ sql: `INSERT INTO credit_lots (user_id, amount_mc, remaining_mc, source, stripe_pi, expires_at, never_expires)
2318
+ SELECT user_id, amount_mc, amount_mc, operation, NULL, COALESCE(?, datetime('now', ?)), ?
2319
+ FROM billing_credits
2320
+ WHERE idempotency_key = ? AND status = 'applying'`,
2321
+ args: [expiry, CREDIT_LOT_TTL, neverExpires ? 1 : 0, idempotencyKey]
2322
+ },
2323
+ {
2324
+ sql: `UPDATE users
2325
+ SET balance_mc = balance_mc + COALESCE((
2326
+ SELECT amount_mc FROM billing_credits WHERE idempotency_key = ? AND status = 'applying'
2327
+ ), 0)
2328
+ WHERE id = ?`,
2329
+ args: [idempotencyKey, userId]
2330
+ },
2331
+ {
2332
+ sql: "INSERT INTO ledger (user_id, amount_mc, operation, description) SELECT user_id, amount_mc, operation, description FROM billing_credits WHERE idempotency_key = ? AND status = 'applying'",
2333
+ args: [idempotencyKey]
2334
+ },
2335
+ {
2336
+ sql: "UPDATE billing_credits SET status = 'applied', applied_at = datetime('now') WHERE idempotency_key = ? AND status = 'applying'",
2337
+ args: [idempotencyKey]
2338
+ }
2339
+ ], "write");
2340
+ } catch (error) {
2341
+ try {
2342
+ const recovered = await db.execute({
2343
+ sql: "SELECT user_id, amount_mc, operation, description FROM billing_credits WHERE idempotency_key = ? AND status = 'applied' LIMIT 1",
2344
+ args: [idempotencyKey]
2345
+ });
2346
+ const row2 = recovered.rows[0];
2347
+ if (row2 && Number(row2.user_id) === userId && Number(row2.amount_mc) === mc && String(row2.operation) === operation && String(row2.description) === description) {
2348
+ return { ok: true, balance_mc: await readBalance(), duplicate: true };
2349
+ }
2350
+ } catch {
2351
+ }
2352
+ throw error;
2353
+ }
2354
+ const claim = await db.execute({
2355
+ sql: "SELECT user_id, amount_mc, operation, description, status FROM billing_credits WHERE idempotency_key = ? LIMIT 1",
2356
+ args: [idempotencyKey]
2357
+ });
2358
+ const row = claim.rows[0];
2359
+ if (!row || Number(row.user_id) !== userId || Number(row.amount_mc) !== mc || String(row.operation) !== operation || String(row.description) !== description) {
2360
+ throw new Error("idempotent credit key conflicts with another credit");
2361
+ }
2362
+ return {
2363
+ ok: String(row.status) === "applied",
2364
+ balance_mc: await readBalance(),
2365
+ duplicate: results[1]?.rowsAffected === 0 && String(row.status) === "applied"
2366
+ };
2367
+ }
2368
+ async function ledgerExistsForOperation(userId, operation) {
2369
+ const res = await getDb().execute({
2370
+ sql: "SELECT 1 FROM ledger WHERE user_id = ? AND operation = ? LIMIT 1",
2371
+ args: [userId, operation]
2372
+ });
2373
+ return res.rows.length > 0;
2374
+ }
2375
+ async function claimMonthlyFreeRefresh(userId, month) {
2376
+ const res = await getDb().execute({
2377
+ sql: "INSERT OR IGNORE INTO free_credit_refreshes (user_id, month) VALUES (?, ?)",
2378
+ args: [userId, month]
2379
+ });
2380
+ return res.rowsAffected > 0;
2381
+ }
2382
+ async function debitMc(userId, mc, operation, description) {
2383
+ const debit = await debitMcIdempotent(
2384
+ userId,
2385
+ mc,
2386
+ operation,
2387
+ description ?? "",
2388
+ `debit:${randomUUID()}`
2389
+ );
2390
+ return { ok: debit.ok, balance_mc: debit.balance_mc };
2391
+ }
2392
+ async function debitMcIdempotent(userId, mc, operation, description, idempotencyKey) {
2393
+ if (!Number.isSafeInteger(mc) || mc <= 0) throw new Error("idempotent debit amount must be a positive integer");
2394
+ if (!idempotencyKey || idempotencyKey.length > 500) throw new Error("idempotent debit key is required and must be bounded");
2395
+ const db = getDb();
2396
+ await ensureMigrated(userId);
2397
+ let results;
2398
+ try {
2399
+ results = await db.batch([
2400
+ {
2401
+ // An unfunded claim may be retried with a corrected estimate under the
2402
+ // same logical operation fingerprint. Applied claims remain immutable.
2403
+ sql: "UPDATE billing_debits SET amount_mc = ? WHERE idempotency_key = ? AND user_id = ? AND operation = ? AND description = ? AND status = 'claimed'",
2404
+ args: [mc, idempotencyKey, userId, operation, description]
2405
+ },
2406
+ {
2407
+ sql: "INSERT OR IGNORE INTO billing_debits (idempotency_key, user_id, amount_mc, operation, description, status) VALUES (?, ?, ?, ?, ?, 'claimed')",
2408
+ args: [idempotencyKey, userId, mc, operation, description]
2409
+ },
2410
+ {
2411
+ sql: `UPDATE users
2412
+ SET balance_mc = balance_mc - ?
2413
+ WHERE id = ?
2414
+ AND balance_mc >= ?
2415
+ AND (SELECT COALESCE(SUM(remaining_mc), 0)
2416
+ FROM credit_lots
2417
+ WHERE user_id = ?
2418
+ AND remaining_mc > 0
2419
+ AND (never_expires = 1 OR expires_at > datetime('now'))) >= ?
2420
+ AND EXISTS (SELECT 1 FROM billing_debits WHERE idempotency_key = ? AND user_id = ? AND amount_mc = ? AND operation = ? AND description = ? AND status = 'claimed')`,
2421
+ args: [mc, userId, mc, userId, mc, idempotencyKey, userId, mc, operation, description]
2422
+ },
2423
+ {
2424
+ sql: "UPDATE billing_debits SET status = 'applying' WHERE idempotency_key = ? AND user_id = ? AND amount_mc = ? AND operation = ? AND description = ? AND status = 'claimed' AND changes() = 1",
2425
+ args: [idempotencyKey, userId, mc, operation, description]
2426
+ },
2427
+ {
2428
+ sql: `INSERT OR IGNORE INTO billing_debit_allocations
2429
+ (debit_idempotency_key, lot_id, user_id, amount_mc, expires_at)
2430
+ WITH ordered AS MATERIALIZED (
2431
+ SELECT id,
2432
+ remaining_mc,
2433
+ expires_at,
2434
+ COALESCE(SUM(remaining_mc) OVER (
2435
+ ORDER BY never_expires ASC, expires_at ASC, granted_at ASC, id ASC
2436
+ ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
2437
+ ), 0) AS prior_mc
2438
+ FROM credit_lots
2439
+ WHERE user_id = ? AND remaining_mc > 0 AND (never_expires = 1 OR expires_at > datetime('now'))
2440
+ ), allocation AS (
2441
+ SELECT id, expires_at, MIN(remaining_mc, MAX(0, ? - prior_mc)) AS take_mc
2442
+ FROM ordered
2443
+ )
2444
+ SELECT ?, id, ?, take_mc, expires_at
2445
+ FROM allocation
2446
+ WHERE take_mc > 0
2447
+ AND EXISTS (SELECT 1 FROM billing_debits WHERE idempotency_key = ? AND status = 'applying')`,
2448
+ args: [userId, mc, idempotencyKey, userId, idempotencyKey]
2449
+ },
2450
+ {
2451
+ sql: `UPDATE credit_lots
2452
+ SET remaining_mc = remaining_mc - COALESCE((
2453
+ SELECT amount_mc FROM billing_debit_allocations
2454
+ WHERE debit_idempotency_key = ? AND lot_id = credit_lots.id
2455
+ ), 0)
2456
+ WHERE id IN (
2457
+ SELECT lot_id FROM billing_debit_allocations WHERE debit_idempotency_key = ?
2458
+ )
2459
+ AND EXISTS (SELECT 1 FROM billing_debits WHERE idempotency_key = ? AND status = 'applying')`,
2460
+ args: [idempotencyKey, idempotencyKey, idempotencyKey]
2461
+ },
2462
+ {
2463
+ sql: "INSERT INTO ledger (user_id, amount_mc, operation, description) SELECT user_id, -amount_mc, operation, description FROM billing_debits WHERE idempotency_key = ? AND status = 'applying'",
2464
+ args: [idempotencyKey]
2465
+ },
2466
+ {
2467
+ sql: "UPDATE billing_debits SET status = 'applied', applied_at = datetime('now') WHERE idempotency_key = ? AND status = 'applying'",
2468
+ args: [idempotencyKey]
2469
+ }
2470
+ ], "write");
2471
+ } catch (error) {
2472
+ try {
2473
+ const recovered = await db.execute({
2474
+ sql: "SELECT user_id, amount_mc, operation, description, status FROM billing_debits WHERE idempotency_key = ? AND status = 'applied' LIMIT 1",
2475
+ args: [idempotencyKey]
2476
+ });
2477
+ const row2 = recovered.rows[0];
2478
+ if (row2 && Number(row2.user_id) === userId && Number(row2.amount_mc) === mc && String(row2.operation) === operation && String(row2.description) === description) {
2479
+ const balance2 = await db.execute({ sql: "SELECT balance_mc FROM users WHERE id = ?", args: [userId] });
2480
+ return { ok: true, balance_mc: Number(balance2.rows[0]?.balance_mc ?? 0), duplicate: true };
2481
+ }
2482
+ } catch {
2483
+ }
2484
+ throw error;
2485
+ }
2486
+ const claim = await db.execute({
2487
+ sql: "SELECT user_id, amount_mc, operation, description, status FROM billing_debits WHERE idempotency_key = ? LIMIT 1",
2488
+ args: [idempotencyKey]
2489
+ });
2490
+ const row = claim.rows[0];
2491
+ if (!row || Number(row.user_id) !== userId || Number(row.amount_mc) !== mc || String(row.operation) !== operation || String(row.description) !== description) {
2492
+ throw new Error("idempotent debit key conflicts with another debit");
2493
+ }
2494
+ const balance = await db.execute({ sql: "SELECT balance_mc FROM users WHERE id = ?", args: [userId] });
2495
+ return {
2496
+ ok: String(row.status) === "applied",
2497
+ balance_mc: Number(balance.rows[0]?.balance_mc ?? 0),
2498
+ duplicate: results[2]?.rowsAffected === 0 && String(row.status) === "applied"
2499
+ };
2500
+ }
2501
+ async function settleDebitMcIdempotent(userId, debitIdempotencyKey, finalAmountMc, refundOperation, description, sourceSurface) {
2502
+ if (!Number.isSafeInteger(finalAmountMc) || finalAmountMc < 0) {
2503
+ throw new Error("idempotent debit settlement amount must be a non-negative integer");
2504
+ }
2505
+ if (!debitIdempotencyKey || debitIdempotencyKey.length > 500) {
2506
+ throw new Error("idempotent debit settlement key is required and must be bounded");
2507
+ }
2508
+ if (!refundOperation || !sourceSurface) {
2509
+ throw new Error("idempotent debit settlement operation and source are required");
2510
+ }
2511
+ const db = getDb();
2512
+ await ensureMigrated(userId);
2513
+ const settlementKey = `debit-settlement:${createHash("sha256").update(debitIdempotencyKey).digest("hex")}`;
2514
+ const billingClass = "wallet_hold_settlement";
2515
+ const eventId = randomUUID();
2516
+ const metadata = JSON.stringify({ debitIdempotencyKey, refundOperation, description });
2517
+ const readReceipt = async () => {
2518
+ const receipt2 = await db.execute({
2519
+ sql: `SELECT e.user_id,
2520
+ e.status,
2521
+ e.amount_mc AS final_amount_mc,
2522
+ e.billing_class,
2523
+ e.source_surface,
2524
+ e.settled_at,
2525
+ d.amount_mc AS held_amount_mc
2526
+ FROM billing_events e
2527
+ JOIN billing_debits d ON d.idempotency_key = ?
2528
+ WHERE e.idempotency_key = ?
2529
+ AND d.user_id = e.user_id
2530
+ AND d.status = 'applied'
2531
+ LIMIT 1`,
2532
+ args: [debitIdempotencyKey, settlementKey]
2533
+ });
2534
+ const row = receipt2.rows[0];
2535
+ if (!row) return null;
2536
+ if (Number(row.user_id) !== userId || String(row.billing_class) !== billingClass || String(row.source_surface) !== sourceSurface) {
2537
+ throw new Error("idempotent debit settlement key conflicts with another settlement");
2538
+ }
2539
+ if (String(row.status) !== "settled") return null;
2540
+ const heldAmountMc = Number(row.held_amount_mc);
2541
+ const durableFinalAmountMc = Number(row.final_amount_mc);
2542
+ if (!Number.isSafeInteger(heldAmountMc) || !Number.isSafeInteger(durableFinalAmountMc) || durableFinalAmountMc < 0 || durableFinalAmountMc > heldAmountMc) {
2543
+ throw new Error("idempotent debit settlement receipt is invalid");
2544
+ }
2545
+ const nominalRefundMc = heldAmountMc - durableFinalAmountMc;
2546
+ const allocationRefund = await db.execute({
2547
+ sql: `WITH ordered AS (
2548
+ SELECT a.amount_mc,
2549
+ a.expires_at,
2550
+ COALESCE(SUM(a.amount_mc) OVER (
2551
+ ORDER BY a.expires_at ASC, a.lot_id ASC
2552
+ ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
2553
+ ), 0) AS prior_mc
2554
+ FROM billing_debit_allocations a
2555
+ WHERE a.debit_idempotency_key = ?
2556
+ ), refunds AS (
2557
+ SELECT expires_at,
2558
+ amount_mc - MIN(amount_mc, MAX(0, ? - prior_mc)) AS refund_mc
2559
+ FROM ordered
2560
+ )
2561
+ SELECT (SELECT COUNT(*) FROM ordered) AS allocation_count,
2562
+ COALESCE(SUM(CASE WHEN expires_at > ? THEN refund_mc ELSE 0 END), 0) AS credited_refund_mc
2563
+ FROM refunds`,
2564
+ args: [debitIdempotencyKey, durableFinalAmountMc, String(row.settled_at)]
2565
+ });
2566
+ const allocationCount = Number(allocationRefund.rows[0]?.allocation_count ?? 0);
2567
+ const creditedRefundMc = allocationCount > 0 ? Number(allocationRefund.rows[0]?.credited_refund_mc ?? 0) : 0;
2568
+ const balance = await db.execute({ sql: "SELECT balance_mc FROM users WHERE id = ?", args: [userId] });
2569
+ return {
2570
+ balance_mc: Number(balance.rows[0]?.balance_mc ?? 0),
2571
+ final_amount_mc: durableFinalAmountMc,
2572
+ refund_mc: creditedRefundMc,
2573
+ expired_refund_mc: nominalRefundMc - creditedRefundMc,
2574
+ duplicate: true
2575
+ };
2576
+ };
2577
+ let results;
2578
+ try {
2579
+ results = await db.batch([
2580
+ {
2581
+ sql: `INSERT OR IGNORE INTO billing_events
2582
+ (id, user_id, idempotency_key, billing_class, source_surface, status, amount_mc, metadata)
2583
+ VALUES (?, ?, ?, ?, ?, 'settling', ?, ?)`,
2584
+ args: [eventId, userId, settlementKey, billingClass, sourceSurface, finalAmountMc, metadata]
2585
+ },
2586
+ {
2587
+ sql: `WITH ordered AS (
2588
+ SELECT a.lot_id,
2589
+ a.amount_mc,
2590
+ a.expires_at,
2591
+ COALESCE(SUM(a.amount_mc) OVER (
2592
+ ORDER BY a.expires_at ASC, a.lot_id ASC
2593
+ ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
2594
+ ), 0) AS prior_mc
2595
+ FROM billing_debit_allocations a
2596
+ WHERE a.debit_idempotency_key = ?
2597
+ ), refunds AS (
2598
+ SELECT lot_id,
2599
+ expires_at,
2600
+ amount_mc - MIN(amount_mc, MAX(0, ? - prior_mc)) AS refund_mc
2601
+ FROM ordered
2602
+ )
2603
+ UPDATE users
2604
+ SET balance_mc = balance_mc + CASE
2605
+ WHEN EXISTS (SELECT 1 FROM ordered) THEN COALESCE(
2606
+ (SELECT SUM(refund_mc) FROM refunds WHERE expires_at > datetime('now')),
2607
+ 0
2608
+ )
2609
+ -- Holds created before allocation provenance cannot be safely
2610
+ -- tied back to a source lot. Treat their unused portion as
2611
+ -- expired instead of minting spendable credits with a guessed
2612
+ -- expiry.
2613
+ ELSE 0
2614
+ END
2615
+ WHERE id = ?
2616
+ AND EXISTS (
2617
+ SELECT 1
2618
+ FROM billing_events e
2619
+ JOIN billing_debits d ON d.idempotency_key = ?
2620
+ WHERE e.idempotency_key = ?
2621
+ AND e.user_id = ?
2622
+ AND e.billing_class = ?
2623
+ AND e.source_surface = ?
2624
+ AND e.status = 'settling'
2625
+ AND d.user_id = e.user_id
2626
+ AND d.status = 'applied'
2627
+ AND e.amount_mc BETWEEN 0 AND d.amount_mc
2628
+ )`,
2629
+ args: [
2630
+ debitIdempotencyKey,
2631
+ finalAmountMc,
2632
+ userId,
2633
+ debitIdempotencyKey,
2634
+ settlementKey,
2635
+ userId,
2636
+ billingClass,
2637
+ sourceSurface
2638
+ ]
2639
+ },
2640
+ {
2641
+ sql: `UPDATE billing_events
2642
+ SET status = 'applying'
2643
+ WHERE idempotency_key = ?
2644
+ AND user_id = ?
2645
+ AND billing_class = ?
2646
+ AND source_surface = ?
2647
+ AND status = 'settling'
2648
+ AND changes() = 1`,
2649
+ args: [settlementKey, userId, billingClass, sourceSurface]
2650
+ },
2651
+ {
2652
+ sql: `WITH ordered AS (
2653
+ SELECT a.lot_id,
2654
+ a.amount_mc,
2655
+ a.expires_at,
2656
+ COALESCE(SUM(a.amount_mc) OVER (
2657
+ ORDER BY a.expires_at ASC, a.lot_id ASC
2658
+ ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
2659
+ ), 0) AS prior_mc
2660
+ FROM billing_debit_allocations a
2661
+ WHERE a.debit_idempotency_key = ?
2662
+ ), refunds AS (
2663
+ SELECT lot_id,
2664
+ expires_at,
2665
+ amount_mc - MIN(amount_mc, MAX(0, ? - prior_mc)) AS refund_mc
2666
+ FROM ordered
2667
+ )
2668
+ UPDATE credit_lots
2669
+ SET remaining_mc = remaining_mc + COALESCE((SELECT refund_mc FROM refunds WHERE refunds.lot_id = credit_lots.id), 0)
2670
+ WHERE id IN (
2671
+ SELECT lot_id FROM refunds
2672
+ WHERE refund_mc > 0 AND expires_at > datetime('now')
2673
+ )
2674
+ AND EXISTS (SELECT 1 FROM billing_events WHERE idempotency_key = ? AND status = 'applying')`,
2675
+ args: [debitIdempotencyKey, finalAmountMc, settlementKey]
2676
+ },
2677
+ {
2678
+ sql: `WITH ordered AS (
2679
+ SELECT a.amount_mc,
2680
+ a.expires_at,
2681
+ COALESCE(SUM(a.amount_mc) OVER (
2682
+ ORDER BY a.expires_at ASC, a.lot_id ASC
2683
+ ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
2684
+ ), 0) AS prior_mc
2685
+ FROM billing_debit_allocations a
2686
+ WHERE a.debit_idempotency_key = ?
2687
+ ), refunds AS (
2688
+ SELECT expires_at,
2689
+ amount_mc - MIN(amount_mc, MAX(0, ? - prior_mc)) AS refund_mc
2690
+ FROM ordered
2691
+ ), credited AS (
2692
+ SELECT e.user_id,
2693
+ CASE
2694
+ WHEN EXISTS (SELECT 1 FROM ordered) THEN COALESCE(
2695
+ (SELECT SUM(refund_mc) FROM refunds WHERE expires_at > datetime('now')),
2696
+ 0
2697
+ )
2698
+ ELSE 0
2699
+ END AS refund_mc
2700
+ FROM billing_events e
2701
+ JOIN billing_debits d ON d.idempotency_key = ?
2702
+ WHERE e.idempotency_key = ?
2703
+ AND e.status = 'applying'
2704
+ )
2705
+ INSERT INTO ledger (user_id, amount_mc, operation, description)
2706
+ SELECT user_id,
2707
+ refund_mc,
2708
+ ?,
2709
+ ?
2710
+ FROM credited
2711
+ WHERE refund_mc > 0`,
2712
+ args: [
2713
+ debitIdempotencyKey,
2714
+ finalAmountMc,
2715
+ debitIdempotencyKey,
2716
+ settlementKey,
2717
+ refundOperation,
2718
+ description
2719
+ ]
2720
+ },
2721
+ {
2722
+ sql: `UPDATE billing_events
2723
+ SET status = 'settled', settled_at = datetime('now')
2724
+ WHERE idempotency_key = ?
2725
+ AND status = 'applying'`,
2726
+ args: [settlementKey]
2727
+ }
2728
+ ], "write");
2729
+ } catch (error) {
2730
+ try {
2731
+ const recovered = await readReceipt();
2732
+ if (recovered) return recovered;
2733
+ } catch {
2734
+ }
2735
+ throw error;
2736
+ }
2737
+ const receipt = await readReceipt();
2738
+ if (!receipt) throw new Error("idempotent debit settlement did not reach a durable settled state");
2739
+ return { ...receipt, duplicate: results[0]?.rowsAffected === 0 };
2740
+ }
2741
+ async function getLedger(userId, limit = 50) {
2742
+ const res = await getDb().execute({
2743
+ sql: "SELECT * FROM ledger WHERE user_id = ? ORDER BY created_at DESC LIMIT ?",
2744
+ args: [userId, limit]
2745
+ });
2746
+ return res.rows.map((r) => ({
2747
+ id: Number(r.id),
2748
+ user_id: Number(r.user_id),
2749
+ amount_mc: Number(r.amount_mc),
2750
+ operation: String(r.operation),
2751
+ description: r.description != null ? String(r.description) : null,
2752
+ stripe_pi: r.stripe_pi != null ? String(r.stripe_pi) : null,
2753
+ created_at: String(r.created_at)
2754
+ }));
2755
+ }
2756
+ async function getBillingLedgerSummary(userId, since, classification) {
2757
+ const db = getDb();
2758
+ const refundPlaceholders = classification.refund.map(() => "?").join(", ");
2759
+ const grantPlaceholders = classification.grant.map(() => "?").join(", ");
2760
+ const purchasePlaceholders = classification.purchase.map(() => "?").join(", ");
2761
+ const totalsRes = await db.execute({
2762
+ sql: `SELECT
2763
+ COALESCE(SUM(CASE WHEN amount_mc < 0 AND operation NOT IN (${refundPlaceholders}) THEN -amount_mc ELSE 0 END), 0) AS spent_mc,
2764
+ COALESCE(SUM(CASE WHEN amount_mc > 0 AND operation IN (${refundPlaceholders}) THEN amount_mc ELSE 0 END), 0) AS refunded_mc,
2765
+ COALESCE(SUM(CASE WHEN amount_mc > 0 AND operation IN (${grantPlaceholders}) THEN amount_mc ELSE 0 END), 0) AS granted_mc,
2766
+ COALESCE(SUM(CASE WHEN amount_mc > 0 AND operation IN (${purchasePlaceholders}) THEN amount_mc ELSE 0 END), 0) AS purchased_mc,
2767
+ COALESCE(SUM(amount_mc), 0) AS net_mc
2768
+ FROM ledger WHERE user_id = ? AND created_at >= ?`,
2769
+ args: [
2770
+ ...classification.refund,
2771
+ ...classification.refund,
2772
+ ...classification.grant,
2773
+ ...classification.purchase,
2774
+ userId,
2775
+ since
2776
+ ]
2777
+ });
2778
+ const byOpRes = await db.execute({
2779
+ sql: `SELECT operation, -SUM(amount_mc) AS mc, COUNT(*) AS count
2780
+ FROM ledger
2781
+ WHERE user_id = ? AND created_at >= ? AND amount_mc < 0 AND operation NOT IN (${refundPlaceholders})
2782
+ GROUP BY operation
2783
+ ORDER BY mc DESC, operation ASC`,
2784
+ args: [userId, since, ...classification.refund]
2785
+ });
2786
+ const totals = totalsRes.rows[0];
2787
+ return {
2788
+ spent_mc: Number(totals?.spent_mc ?? 0),
2789
+ refunded_mc: Number(totals?.refunded_mc ?? 0),
2790
+ granted_mc: Number(totals?.granted_mc ?? 0),
2791
+ purchased_mc: Number(totals?.purchased_mc ?? 0),
2792
+ net_mc: Number(totals?.net_mc ?? 0),
2793
+ by_operation: byOpRes.rows.map((r) => ({
2794
+ operation: String(r.operation),
2795
+ mc: Number(r.mc),
2796
+ count: Number(r.count)
2797
+ }))
2798
+ };
2799
+ }
2800
+ async function ledgerExistsForStripePI(stripePI) {
2801
+ const res = await getDb().execute({
2802
+ sql: "SELECT 1 FROM ledger WHERE stripe_pi = ? LIMIT 1",
2803
+ args: [stripePI]
2804
+ });
2805
+ return res.rows.length > 0;
2806
+ }
2807
+ async function reconcileBalanceMc(userId, knownBalanceMc) {
2808
+ const db = getDb();
2809
+ void knownBalanceMc;
2810
+ for (let attempt = 0; attempt < 8; attempt += 1) {
2811
+ const snapshot = await db.execute({
2812
+ sql: `SELECT
2813
+ u.balance_mc AS cached_balance_mc,
2814
+ COUNT(l.id) AS lot_count,
2815
+ COALESCE(SUM(CASE
2816
+ WHEN l.remaining_mc > 0
2817
+ AND (l.never_expires = 1 OR l.expires_at > datetime('now'))
2818
+ THEN l.remaining_mc ELSE 0 END), 0) AS projected_balance_mc
2819
+ FROM users u
2820
+ LEFT JOIN credit_lots l ON l.user_id = u.id
2821
+ WHERE u.id = ?
2822
+ GROUP BY u.id, u.balance_mc`,
2823
+ args: [userId]
2824
+ });
2825
+ const row = snapshot.rows[0];
2826
+ if (!row) return 0;
2827
+ if (Number(row.lot_count ?? 0) === 0) {
2828
+ await ensureMigrated(Number(userId));
2829
+ const nowHasLots = await db.execute({
2830
+ sql: "SELECT 1 FROM credit_lots WHERE user_id = ? LIMIT 1",
2831
+ args: [userId]
2832
+ });
2833
+ if (nowHasLots.rows.length > 0) continue;
2834
+ }
2835
+ const cachedBalanceMc = Number(row.cached_balance_mc ?? 0);
2836
+ const projectedBalanceMc = Number(row.projected_balance_mc ?? 0);
2837
+ if (cachedBalanceMc === projectedBalanceMc) return projectedBalanceMc;
2838
+ const updated = await db.execute({
2839
+ sql: "UPDATE users SET balance_mc = ? WHERE id = ? AND balance_mc = ?",
2840
+ args: [projectedBalanceMc, userId, cachedBalanceMc]
2841
+ });
2842
+ if (updated.rowsAffected === 1) return projectedBalanceMc;
2843
+ }
2844
+ const current = await db.execute({
2845
+ sql: "SELECT balance_mc FROM users WHERE id = ? LIMIT 1",
2846
+ args: [userId]
2847
+ });
2848
+ return Number(current.rows[0]?.balance_mc ?? 0);
2849
+ }
2850
+ var SiteAuditJobRowSchema = z.object({
2851
+ id: z.string(),
2852
+ user_id: z.number(),
2853
+ status: z.string().default("pending"),
2854
+ client_domain: z.string(),
2855
+ session_path: z.string(),
2856
+ request: z.string(),
2857
+ result: z.string().nullable(),
2858
+ error: z.string().nullable(),
2859
+ created_at: z.string(),
2860
+ updated_at: z.string()
2861
+ });
2862
+ var SiteAuditPhaseLogRowSchema = z.object({
2863
+ id: z.number(),
2864
+ job_id: z.string(),
2865
+ phase: z.string(),
2866
+ completed_at: z.string(),
2867
+ output_summary: z.string()
2868
+ });
2869
+ async function listInboxMessages(userId) {
2870
+ const res = await getDb().execute({
2871
+ sql: "SELECT id, subject, sent_at, opened_at FROM inbox_messages WHERE user_id = ? AND deleted_at IS NULL ORDER BY sent_at DESC, id DESC",
2872
+ args: [userId]
2873
+ });
2874
+ return res.rows.map((r) => ({
2875
+ id: String(r.id),
2876
+ subject: String(r.subject),
2877
+ sent_at: String(r.sent_at),
2878
+ opened_at: r.opened_at != null ? String(r.opened_at) : null
2879
+ }));
2880
+ }
2881
+ async function getInboxMessage(userId, id) {
2882
+ const res = await getDb().execute({
2883
+ sql: "SELECT id, subject, body_md, source, sent_at, opened_at FROM inbox_messages WHERE id = ? AND user_id = ? AND deleted_at IS NULL",
2884
+ args: [id, userId]
2885
+ });
2886
+ const row = res.rows[0];
2887
+ if (!row) return void 0;
2888
+ return {
2889
+ id: String(row.id),
2890
+ subject: String(row.subject),
2891
+ body_md: String(row.body_md),
2892
+ source: String(row.source),
2893
+ sent_at: String(row.sent_at),
2894
+ opened_at: row.opened_at != null ? String(row.opened_at) : null
2895
+ };
2896
+ }
2897
+ async function markInboxMessageOpened(userId, id) {
2898
+ const db = getDb();
2899
+ await db.execute({
2900
+ sql: "UPDATE inbox_messages SET opened_at = datetime('now') WHERE id = ? AND user_id = ? AND deleted_at IS NULL AND opened_at IS NULL",
2901
+ args: [id, userId]
2902
+ });
2903
+ const res = await db.execute({
2904
+ sql: "SELECT opened_at FROM inbox_messages WHERE id = ? AND user_id = ? AND deleted_at IS NULL",
2905
+ args: [id, userId]
2906
+ });
2907
+ const row = res.rows[0];
2908
+ if (!row) return null;
2909
+ return row.opened_at != null ? String(row.opened_at) : null;
2910
+ }
2911
+ async function softDeleteInboxMessage(userId, id) {
2912
+ const res = await getDb().execute({
2913
+ sql: "UPDATE inbox_messages SET deleted_at = datetime('now') WHERE id = ? AND user_id = ? AND deleted_at IS NULL",
2914
+ args: [id, userId]
2915
+ });
2916
+ return res.rowsAffected > 0;
2917
+ }
2918
+ async function insertInboxMessage(userId, subject, bodyMd, source = "operator") {
2919
+ const id = randomUUID();
2920
+ await getDb().execute({
2921
+ sql: "INSERT INTO inbox_messages (id, user_id, subject, body_md, source) VALUES (?, ?, ?, ?, ?)",
2922
+ args: [id, userId, subject, bodyMd, source]
2923
+ });
2924
+ return id;
2925
+ }
2926
+ async function listActiveUserIds() {
2927
+ const res = await getDb().execute("SELECT id FROM users WHERE active = 1");
2928
+ return res.rows.map((r) => Number(r.id));
2929
+ }
2930
+
2931
+ export {
2932
+ CREDIT_LOTS_EPOCH,
2933
+ CORE_SCHEMA_VERSION,
2934
+ getDb,
2935
+ setDbForTesting,
2936
+ migrate,
2937
+ registerClient,
2938
+ getClient,
2939
+ putCode,
2940
+ consumeCode,
2941
+ putRefresh,
2942
+ getRefresh,
2943
+ revokeRefresh,
2944
+ rotateRefresh,
2945
+ claimStripeEvent,
2946
+ completeStripeEvent,
2947
+ failStripeEvent,
2948
+ stripeEventAlreadyProcessed,
2949
+ generateApiKey,
2950
+ hashPassword,
2951
+ verifyPassword,
2952
+ getUserByEmail,
2953
+ getUserByApiKey,
2954
+ getUserById,
2955
+ getUserMemoryCreds,
2956
+ setUserMemoryCreds,
2957
+ setPassword,
2958
+ getUserStats,
2959
+ createUser,
2960
+ checkRateLimit,
2961
+ createPasswordResetToken,
2962
+ consumePasswordResetToken,
2963
+ rotateApiKey,
2964
+ revokeApiKey,
2965
+ listUsers,
2966
+ deactivateUser,
2967
+ createJob,
2968
+ createRunningJob,
2969
+ startHarvestAttempt,
2970
+ finishHarvestAttempt,
2971
+ listHarvestAttempts,
2972
+ getJob,
2973
+ listJobs,
2974
+ getPageSnapshot,
2975
+ upsertPageSnapshot,
2976
+ logRequestEvent,
2977
+ listRequestEvents,
2978
+ createWorkflowSchedule,
2979
+ getWorkflowSchedule,
2980
+ listWorkflowSchedules,
2981
+ patchWorkflowSchedule,
2982
+ deleteWorkflowSchedule,
2983
+ listDueWorkflowSchedules,
2984
+ markWorkflowScheduleRan,
2985
+ createWorkflowRun,
2986
+ getWorkflowRun,
2987
+ getWorkflowRunByIdempotencyKey,
2988
+ listWorkflowRuns,
2989
+ markWorkflowRunRunning,
2990
+ completeWorkflowRunRecord,
2991
+ failWorkflowRunRecord,
2992
+ replaceWorkflowArtifacts,
2993
+ appendWorkflowArtifacts,
2994
+ getWorkflowRunStepState,
2995
+ advanceWorkflowRunStep,
2996
+ listWorkflowArtifacts,
2997
+ getWorkflowArtifact,
2998
+ recordWorkflowWebhookDelivery,
2999
+ countActiveUsers,
3000
+ countActiveJobsForUser,
3001
+ claimPendingJob,
3002
+ completeJob,
3003
+ failJob,
3004
+ cancelJob,
3005
+ createKpoJob,
3006
+ claimPendingKpoJob,
3007
+ getKpoJob,
3008
+ updateKpoJobState,
3009
+ completeKpoJob,
3010
+ failKpoJob,
3011
+ logKpoPhaseComplete,
3012
+ getKpoPhaseLog,
3013
+ listKpoJobs,
3014
+ getUserByStripeCustomerId,
3015
+ setStripeCustomerId,
3016
+ setExtraConcurrencySlots,
3017
+ setConcurrencySubId,
3018
+ setSubscriptionTier,
3019
+ getConnectedAccountBillingState,
3020
+ setConnectedAccountBillingState,
3021
+ listUsersDueForConnectedAccountBillingReconciliation,
3022
+ setMemoryPlan,
3023
+ getMemoryPlan,
3024
+ getMemoryProvisioned,
3025
+ setMemoryProvisioned,
3026
+ resolveUserByMemorySubscription,
3027
+ CREDIT_LOT_TTL,
3028
+ expireOldLots,
3029
+ migrateExistingBalancesToLots,
3030
+ creditMc,
3031
+ creditMcIdempotent,
3032
+ ledgerExistsForOperation,
3033
+ claimMonthlyFreeRefresh,
3034
+ debitMc,
3035
+ debitMcIdempotent,
3036
+ settleDebitMcIdempotent,
3037
+ getLedger,
3038
+ getBillingLedgerSummary,
3039
+ ledgerExistsForStripePI,
3040
+ reconcileBalanceMc,
3041
+ SiteAuditJobRowSchema,
3042
+ SiteAuditPhaseLogRowSchema,
3043
+ listInboxMessages,
3044
+ getInboxMessage,
3045
+ markInboxMessageOpened,
3046
+ softDeleteInboxMessage,
3047
+ insertInboxMessage,
3048
+ listActiveUserIds
3049
+ };
3050
+ //# sourceMappingURL=chunk-GUVKHCKE.js.map