@secondlayer/shared 3.0.0-alpha.0 → 3.0.0-beta.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.
@@ -0,0 +1,475 @@
1
+ import { Kysely } from "kysely";
2
+ import { ColumnType, Generated, Selectable } from "kysely";
3
+ interface BlocksTable {
4
+ height: number;
5
+ hash: string;
6
+ parent_hash: string;
7
+ burn_block_height: number;
8
+ timestamp: number;
9
+ canonical: Generated<boolean>;
10
+ created_at: Generated<Date>;
11
+ }
12
+ interface TransactionsTable {
13
+ tx_id: string;
14
+ block_height: number;
15
+ tx_index: Generated<number>;
16
+ type: string;
17
+ sender: string;
18
+ status: string;
19
+ contract_id: string | null;
20
+ function_name: string | null;
21
+ function_args: Generated<unknown | null>;
22
+ raw_result: Generated<string | null>;
23
+ raw_tx: string;
24
+ created_at: Generated<Date>;
25
+ }
26
+ interface EventsTable {
27
+ id: Generated<string>;
28
+ tx_id: string;
29
+ block_height: number;
30
+ event_index: number;
31
+ type: string;
32
+ data: unknown;
33
+ created_at: Generated<Date>;
34
+ }
35
+ interface IndexProgressTable {
36
+ network: string;
37
+ last_indexed_block: Generated<number>;
38
+ last_contiguous_block: Generated<number>;
39
+ highest_seen_block: Generated<number>;
40
+ updated_at: Generated<Date>;
41
+ }
42
+ interface SubgraphsTable {
43
+ id: Generated<string>;
44
+ name: string;
45
+ version: Generated<string>;
46
+ status: Generated<string>;
47
+ definition: Record<string, unknown>;
48
+ schema_hash: string;
49
+ handler_path: string;
50
+ schema_name: string | null;
51
+ start_block: Generated<number>;
52
+ last_processed_block: Generated<number>;
53
+ reindex_from_block: number | null;
54
+ reindex_to_block: number | null;
55
+ last_error: string | null;
56
+ last_error_at: Date | null;
57
+ total_processed: Generated<number>;
58
+ total_errors: Generated<number>;
59
+ account_id: string;
60
+ handler_code: string | null;
61
+ source_code: string | null;
62
+ project_id: string | null;
63
+ created_at: Generated<Date>;
64
+ updated_at: Generated<Date>;
65
+ }
66
+ interface SubgraphGapsTable {
67
+ id: Generated<string>;
68
+ subgraph_id: string;
69
+ subgraph_name: string;
70
+ gap_start: number;
71
+ gap_end: number;
72
+ reason: string;
73
+ detected_at: Generated<Date>;
74
+ resolved_at: Date | null;
75
+ }
76
+ interface ApiKeysTable {
77
+ id: Generated<string>;
78
+ key_hash: string;
79
+ key_prefix: string;
80
+ name: string | null;
81
+ status: Generated<string>;
82
+ rate_limit: Generated<number>;
83
+ ip_address: string;
84
+ account_id: string;
85
+ last_used_at: Date | null;
86
+ revoked_at: Date | null;
87
+ created_at: Generated<Date>;
88
+ }
89
+ interface AccountsTable {
90
+ id: Generated<string>;
91
+ email: string;
92
+ plan: Generated<string>;
93
+ display_name: string | null;
94
+ bio: string | null;
95
+ avatar_url: string | null;
96
+ slug: string | null;
97
+ stripe_customer_id: string | null;
98
+ created_at: Generated<Date>;
99
+ }
100
+ interface SessionsTable {
101
+ id: Generated<string>;
102
+ token_hash: string;
103
+ token_prefix: string;
104
+ account_id: string;
105
+ ip_address: string;
106
+ expires_at: Generated<Date>;
107
+ revoked_at: Date | null;
108
+ last_used_at: Date | null;
109
+ created_at: Generated<Date>;
110
+ }
111
+ interface MagicLinksTable {
112
+ id: Generated<string>;
113
+ email: string;
114
+ token: string;
115
+ code: string | null;
116
+ expires_at: Date;
117
+ used_at: Date | null;
118
+ failed_attempts: Generated<number>;
119
+ created_at: Generated<Date>;
120
+ }
121
+ interface UsageDailyTable {
122
+ account_id: string;
123
+ tenant_id: string | null;
124
+ date: string;
125
+ api_requests: Generated<number>;
126
+ deliveries: Generated<number>;
127
+ }
128
+ interface UsageSnapshotsTable {
129
+ id: Generated<string>;
130
+ account_id: string;
131
+ measured_at: Generated<Date>;
132
+ storage_bytes: Generated<number>;
133
+ }
134
+ interface WaitlistTable {
135
+ id: Generated<string>;
136
+ email: string;
137
+ source: Generated<string>;
138
+ status: Generated<string>;
139
+ created_at: Generated<Date>;
140
+ }
141
+ interface AccountInsightsTable {
142
+ id: Generated<string>;
143
+ account_id: string;
144
+ category: string;
145
+ insight_type: string;
146
+ resource_id: string | null;
147
+ severity: string;
148
+ title: string;
149
+ body: string;
150
+ data: unknown;
151
+ dismissed_at: Date | null;
152
+ expires_at: Date | null;
153
+ created_at: Generated<Date>;
154
+ }
155
+ interface AccountAgentRunsTable {
156
+ id: Generated<string>;
157
+ account_id: string;
158
+ started_at: Generated<Date>;
159
+ completed_at: Date | null;
160
+ status: Generated<string>;
161
+ input_tokens: Generated<number>;
162
+ output_tokens: Generated<number>;
163
+ cost_usd: Generated<number>;
164
+ insights_created: Generated<number>;
165
+ error: string | null;
166
+ }
167
+ interface SubgraphProcessingStatsTable {
168
+ id: Generated<string>;
169
+ subgraph_name: string;
170
+ api_key_id: string | null;
171
+ bucket_start: Date | null;
172
+ bucket_end: Date | null;
173
+ blocks_processed: number | null;
174
+ total_time_ms: number | null;
175
+ handler_time_ms: number | null;
176
+ flush_time_ms: number | null;
177
+ max_block_time_ms: number | null;
178
+ max_handler_time_ms: number | null;
179
+ avg_ops_per_block: number | null;
180
+ is_catchup: Generated<boolean>;
181
+ created_at: Generated<Date>;
182
+ }
183
+ interface SubgraphTableSnapshotsTable {
184
+ id: Generated<string>;
185
+ subgraph_name: string;
186
+ api_key_id: string | null;
187
+ table_name: string;
188
+ row_count: number | null;
189
+ created_at: Generated<Date>;
190
+ }
191
+ interface SubgraphHealthSnapshotsTable {
192
+ id: Generated<string>;
193
+ subgraph_id: string;
194
+ total_processed: number;
195
+ total_errors: number;
196
+ last_processed_block: number | null;
197
+ captured_at: Generated<Date>;
198
+ }
199
+ interface SubgraphUsageDailyTable {
200
+ subgraph_id: string;
201
+ date: string;
202
+ query_count: Generated<number>;
203
+ }
204
+ interface ProjectsTable {
205
+ id: Generated<string>;
206
+ name: string;
207
+ slug: string;
208
+ account_id: string;
209
+ settings: Generated<Record<string, unknown>>;
210
+ network: Generated<string>;
211
+ node_rpc: string | null;
212
+ created_at: Generated<Date>;
213
+ updated_at: Generated<Date>;
214
+ }
215
+ interface TeamMembersTable {
216
+ id: Generated<string>;
217
+ project_id: string;
218
+ account_id: string;
219
+ role: Generated<string>;
220
+ invited_by: string | null;
221
+ created_at: Generated<Date>;
222
+ }
223
+ interface TeamInvitationsTable {
224
+ id: Generated<string>;
225
+ project_id: string;
226
+ email: string;
227
+ role: Generated<string>;
228
+ token: string;
229
+ invited_by: string | null;
230
+ expires_at: Date;
231
+ accepted_at: Date | null;
232
+ created_at: Generated<Date>;
233
+ }
234
+ interface ChatSessionsTable {
235
+ id: Generated<string>;
236
+ account_id: string;
237
+ title: string | null;
238
+ summary: unknown | null;
239
+ created_at: Generated<Date>;
240
+ updated_at: Generated<Date>;
241
+ }
242
+ interface ChatMessagesTable {
243
+ id: Generated<string>;
244
+ chat_session_id: string;
245
+ role: string;
246
+ parts: unknown;
247
+ metadata: unknown | null;
248
+ created_at: Generated<Date>;
249
+ }
250
+ interface Database {
251
+ blocks: BlocksTable;
252
+ transactions: TransactionsTable;
253
+ events: EventsTable;
254
+ index_progress: IndexProgressTable;
255
+ subgraphs: SubgraphsTable;
256
+ api_keys: ApiKeysTable;
257
+ accounts: AccountsTable;
258
+ sessions: SessionsTable;
259
+ magic_links: MagicLinksTable;
260
+ usage_daily: UsageDailyTable;
261
+ usage_snapshots: UsageSnapshotsTable;
262
+ waitlist: WaitlistTable;
263
+ account_insights: AccountInsightsTable;
264
+ account_agent_runs: AccountAgentRunsTable;
265
+ subgraph_health_snapshots: SubgraphHealthSnapshotsTable;
266
+ subgraph_processing_stats: SubgraphProcessingStatsTable;
267
+ subgraph_table_snapshots: SubgraphTableSnapshotsTable;
268
+ subgraph_gaps: SubgraphGapsTable;
269
+ subgraph_usage_daily: SubgraphUsageDailyTable;
270
+ projects: ProjectsTable;
271
+ team_members: TeamMembersTable;
272
+ team_invitations: TeamInvitationsTable;
273
+ chat_sessions: ChatSessionsTable;
274
+ chat_messages: ChatMessagesTable;
275
+ tenants: TenantsTable;
276
+ tenant_usage_monthly: TenantUsageMonthlyTable;
277
+ tenant_compute_addons: TenantComputeAddonsTable;
278
+ account_spend_caps: AccountSpendCapsTable;
279
+ provisioning_audit_log: ProvisioningAuditLogTable;
280
+ subscriptions: SubscriptionsTable;
281
+ subscription_outbox: SubscriptionOutboxTable;
282
+ subscription_deliveries: SubscriptionDeliveriesTable;
283
+ }
284
+ type TenantStatus = "provisioning" | "active" | "suspended" | "error" | "deleted";
285
+ interface TenantsTable {
286
+ id: Generated<string>;
287
+ account_id: string;
288
+ slug: string;
289
+ status: ColumnType<TenantStatus, TenantStatus | undefined, TenantStatus>;
290
+ plan: string;
291
+ cpus: ColumnType<number, number | string, number | string>;
292
+ memory_mb: number;
293
+ storage_limit_mb: number;
294
+ storage_used_mb: number | null;
295
+ pg_container_id: string | null;
296
+ api_container_id: string | null;
297
+ processor_container_id: string | null;
298
+ target_database_url_enc: Buffer;
299
+ tenant_jwt_secret_enc: Buffer;
300
+ anon_key_enc: Buffer;
301
+ service_key_enc: Buffer;
302
+ api_url_internal: string;
303
+ api_url_public: string;
304
+ suspended_at: Date | null;
305
+ last_health_check_at: Date | null;
306
+ last_active_at: Generated<Date>;
307
+ service_gen: Generated<number>;
308
+ anon_gen: Generated<number>;
309
+ project_id: string | null;
310
+ created_at: Generated<Date>;
311
+ updated_at: Generated<Date>;
312
+ }
313
+ interface TenantUsageMonthlyTable {
314
+ id: Generated<string>;
315
+ tenant_id: string;
316
+ period_month: Date;
317
+ storage_peak_mb: Generated<number>;
318
+ storage_avg_mb: Generated<number>;
319
+ storage_last_mb: Generated<number>;
320
+ measurements: Generated<number>;
321
+ first_at: Generated<Date>;
322
+ last_at: Generated<Date>;
323
+ }
324
+ interface TenantComputeAddonsTable {
325
+ id: Generated<string>;
326
+ tenant_id: string;
327
+ memory_mb_delta: Generated<number>;
328
+ cpu_delta: Generated<number | string>;
329
+ storage_mb_delta: Generated<number>;
330
+ effective_from: Generated<Date>;
331
+ effective_until: Date | null;
332
+ stripe_subscription_item_id: string | null;
333
+ created_at: Generated<Date>;
334
+ }
335
+ interface AccountSpendCapsTable {
336
+ account_id: string;
337
+ monthly_cap_cents: number | null;
338
+ compute_cap_cents: number | null;
339
+ storage_cap_cents: number | null;
340
+ ai_cap_cents: number | null;
341
+ alert_threshold_pct: Generated<number>;
342
+ alert_sent_at: Date | null;
343
+ frozen_at: Date | null;
344
+ updated_at: Generated<Date>;
345
+ }
346
+ type ProvisioningAuditEvent = "provision.start" | "provision.success" | "provision.failure" | "suspend" | "resume" | "resize" | "keys.rotate" | "bastion.key.upload" | "bastion.key.revoke" | "teardown";
347
+ type ProvisioningAuditStatus = "ok" | "error";
348
+ interface ProvisioningAuditLogTable {
349
+ id: Generated<string>;
350
+ tenant_id: string | null;
351
+ tenant_slug: string | null;
352
+ account_id: string | null;
353
+ actor: string;
354
+ event: ProvisioningAuditEvent;
355
+ status: ProvisioningAuditStatus;
356
+ detail: unknown | null;
357
+ error: string | null;
358
+ created_at: Generated<Date>;
359
+ }
360
+ type SubscriptionStatus = "active" | "paused" | "error";
361
+ type SubscriptionFormat = "standard-webhooks" | "inngest" | "trigger" | "cloudflare" | "cloudevents" | "raw";
362
+ type SubscriptionRuntime = "inngest" | "trigger" | "cloudflare" | "node";
363
+ interface SubscriptionsTable {
364
+ id: Generated<string>;
365
+ account_id: string;
366
+ project_id: string | null;
367
+ name: string;
368
+ status: ColumnType<SubscriptionStatus, SubscriptionStatus | undefined, SubscriptionStatus>;
369
+ subgraph_name: string;
370
+ table_name: string;
371
+ filter: Generated<unknown>;
372
+ format: ColumnType<SubscriptionFormat, SubscriptionFormat | undefined, SubscriptionFormat>;
373
+ runtime: SubscriptionRuntime | null;
374
+ url: string;
375
+ signing_secret_enc: Buffer;
376
+ auth_config: Generated<unknown>;
377
+ max_retries: Generated<number>;
378
+ timeout_ms: Generated<number>;
379
+ concurrency: Generated<number>;
380
+ circuit_failures: Generated<number>;
381
+ circuit_opened_at: Date | null;
382
+ last_delivery_at: Date | null;
383
+ last_success_at: Date | null;
384
+ last_error: string | null;
385
+ created_at: Generated<Date>;
386
+ updated_at: Generated<Date>;
387
+ }
388
+ type Subscription = Selectable<SubscriptionsTable>;
389
+ type OutboxStatus = "pending" | "delivered" | "dead";
390
+ interface SubscriptionOutboxTable {
391
+ id: Generated<string>;
392
+ subscription_id: string;
393
+ subgraph_name: string;
394
+ table_name: string;
395
+ block_height: number | bigint;
396
+ tx_id: string | null;
397
+ row_pk: unknown;
398
+ event_type: string;
399
+ payload: unknown;
400
+ dedup_key: string;
401
+ attempt: Generated<number>;
402
+ next_attempt_at: Generated<Date>;
403
+ status: ColumnType<OutboxStatus, OutboxStatus | undefined, OutboxStatus>;
404
+ is_replay: Generated<boolean>;
405
+ delivered_at: Date | null;
406
+ failed_at: Date | null;
407
+ locked_by: string | null;
408
+ locked_until: Date | null;
409
+ created_at: Generated<Date>;
410
+ }
411
+ interface SubscriptionDeliveriesTable {
412
+ id: Generated<string>;
413
+ outbox_id: string;
414
+ subscription_id: string;
415
+ attempt: number;
416
+ status_code: number | null;
417
+ response_headers: unknown | null;
418
+ response_body: string | null;
419
+ error_message: string | null;
420
+ duration_ms: number | null;
421
+ dispatched_at: Generated<Date>;
422
+ }
423
+ /**
424
+ * Subscription CRUD. `signing_secret_enc` is transparently encrypted via
425
+ * `encryptSecret`/`decryptSecret`. Plaintext secrets only leave via the
426
+ * return value of `create` (one-time display) and `rotateSecret`.
427
+ */
428
+ interface CreateSubscriptionInput {
429
+ accountId: string;
430
+ projectId?: string | null;
431
+ name: string;
432
+ subgraphName: string;
433
+ tableName: string;
434
+ filter?: unknown;
435
+ format?: SubscriptionFormat;
436
+ runtime?: SubscriptionRuntime | null;
437
+ url: string;
438
+ authConfig?: unknown;
439
+ maxRetries?: number;
440
+ timeoutMs?: number;
441
+ concurrency?: number;
442
+ }
443
+ interface CreateSubscriptionResult {
444
+ subscription: Subscription;
445
+ /** Plaintext signing secret — surfaced once, never stored decrypted. */
446
+ signingSecret: string;
447
+ }
448
+ declare function createSubscription(db: Kysely<Database>, input: CreateSubscriptionInput): Promise<CreateSubscriptionResult>;
449
+ declare function listSubscriptions(db: Kysely<Database>, accountId: string): Promise<Subscription[]>;
450
+ declare function getSubscription(db: Kysely<Database>, accountId: string, id: string): Promise<Subscription | null>;
451
+ declare function getSubscriptionByName(db: Kysely<Database>, accountId: string, name: string): Promise<Subscription | null>;
452
+ interface UpdateSubscriptionInput {
453
+ name?: string;
454
+ filter?: unknown;
455
+ format?: SubscriptionFormat;
456
+ runtime?: SubscriptionRuntime | null;
457
+ url?: string;
458
+ authConfig?: unknown;
459
+ maxRetries?: number;
460
+ timeoutMs?: number;
461
+ concurrency?: number;
462
+ }
463
+ declare function updateSubscription(db: Kysely<Database>, accountId: string, id: string, patch: UpdateSubscriptionInput): Promise<Subscription | null>;
464
+ declare function toggleSubscriptionStatus(db: Kysely<Database>, accountId: string, id: string, status: SubscriptionStatus): Promise<Subscription | null>;
465
+ declare function deleteSubscription(db: Kysely<Database>, accountId: string, id: string): Promise<boolean>;
466
+ interface RotateSecretResult {
467
+ subscription: Subscription;
468
+ signingSecret: string;
469
+ }
470
+ declare function rotateSubscriptionSecret(db: Kysely<Database>, accountId: string, id: string): Promise<RotateSecretResult | null>;
471
+ /** Decrypt a subscription's signing secret for HMAC signing at emit time. */
472
+ declare function getSubscriptionSigningSecret(sub: Subscription): string;
473
+ /** Fire `subscriptions:changed` notify so the emitter hot-reloads its cache. */
474
+ declare function notifySubscriptionsChanged(db: Kysely<Database>, accountId: string): Promise<void>;
475
+ export { updateSubscription, toggleSubscriptionStatus, rotateSubscriptionSecret, notifySubscriptionsChanged, listSubscriptions, getSubscriptionSigningSecret, getSubscriptionByName, getSubscription, deleteSubscription, createSubscription, UpdateSubscriptionInput, RotateSecretResult, CreateSubscriptionResult, CreateSubscriptionInput };