@sixb/pg 0.2.5 → 0.2.7

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 (56) hide show
  1. package/dist/agents/agent-storage.d.ts +2 -0
  2. package/dist/agents/agent-storage.d.ts.map +1 -1
  3. package/dist/agents/approvals.d.ts +2 -1
  4. package/dist/agents/approvals.d.ts.map +1 -1
  5. package/dist/agents/checkpoints.d.ts +2 -1
  6. package/dist/agents/checkpoints.d.ts.map +1 -1
  7. package/dist/agents/inbox.d.ts.map +1 -1
  8. package/dist/agents/messages.d.ts +2 -1
  9. package/dist/agents/messages.d.ts.map +1 -1
  10. package/dist/agents/reads.d.ts +9 -0
  11. package/dist/agents/reads.d.ts.map +1 -0
  12. package/dist/agents/runs.d.ts +2 -1
  13. package/dist/agents/runs.d.ts.map +1 -1
  14. package/dist/agents/threads.d.ts.map +1 -1
  15. package/dist/index.js +593 -100
  16. package/dist/index.js.map +32 -31
  17. package/dist/latest-run-query.d.ts +3 -1
  18. package/dist/latest-run-query.d.ts.map +1 -1
  19. package/dist/migrations.d.ts.map +1 -1
  20. package/dist/pg-ai-cost-storage.d.ts.map +1 -1
  21. package/dist/pg-artifact-storage.d.ts.map +1 -1
  22. package/dist/pg-sync-run-storage.d.ts +2 -1
  23. package/dist/pg-sync-run-storage.d.ts.map +1 -1
  24. package/package.json +3 -3
  25. package/src/agents/agent-storage.ts +3 -0
  26. package/src/agents/approvals.ts +46 -2
  27. package/src/agents/checkpoints.ts +26 -0
  28. package/src/agents/inbox.ts +4 -1
  29. package/src/agents/messages.ts +81 -2
  30. package/src/agents/reads.ts +160 -0
  31. package/src/agents/runs.ts +25 -0
  32. package/src/agents/threads.ts +99 -15
  33. package/src/latest-run-query.ts +8 -1
  34. package/src/migrations/107-agent-inbox-edits.sql +3 -0
  35. package/src/migrations/108-agent-thread-reads.sql +73 -0
  36. package/src/migrations/109-sync-run-connections.sql +6 -0
  37. package/src/migrations.ts +6 -0
  38. package/src/ontology-storage/materialization-session.ts +1 -1
  39. package/src/ontology-storage/materializations.ts +1 -1
  40. package/src/ontology-storage/outbox.ts +3 -3
  41. package/src/ontology-storage/replacement-plans.ts +1 -1
  42. package/src/ontology-storage/source-roots.ts +1 -1
  43. package/src/ontology-storage/sources.ts +2 -2
  44. package/src/ontology-storage/vector-indexing.ts +1 -1
  45. package/src/pg-action-run-storage.ts +1 -1
  46. package/src/pg-activity-storage.ts +3 -3
  47. package/src/pg-ai-cost-storage.ts +36 -21
  48. package/src/pg-ai-usage-storage.ts +1 -1
  49. package/src/pg-artifact-storage.ts +22 -7
  50. package/src/pg-dataset-ingest-storage.ts +3 -3
  51. package/src/pg-execution-storage.ts +1 -1
  52. package/src/pg-projection-run-storage.ts +1 -1
  53. package/src/pg-sync-run-storage.ts +69 -0
  54. package/src/pg-workflow-run-storage.ts +1 -1
  55. package/src/share-grant-storage.ts +1 -1
  56. package/src/transactions.ts +2 -2
@@ -143,7 +143,7 @@ class PgDatasetIngestPersistence implements DatasetIngestPersistence {
143
143
  UPDATE dataset_ingest_deliveries
144
144
  SET flush_id = ${flushId}
145
145
  WHERE project_id = ${projectId} AND dataset_id = ${datasetId}
146
- AND id = ANY(${this.sql.array([...deliveryIds])}::bigint[])
146
+ AND id = ANY(${[...deliveryIds]}::bigint[])
147
147
  `
148
148
  }
149
149
 
@@ -240,7 +240,7 @@ class PgDatasetIngestPersistence implements DatasetIngestPersistence {
240
240
  flush_id, flush_started_at, failure_streak, last_flushed_at
241
241
  FROM dataset_ingest_state
242
242
  WHERE project_id = ${projectId}
243
- AND (${datasetIds === undefined} OR dataset_id = ANY(${this.sql.array([...(datasetIds ?? [])])}::text[]))
243
+ AND (${datasetIds === undefined} OR dataset_id = ANY(${[...(datasetIds ?? [])]}::text[]))
244
244
  `
245
245
  return rows.map(stateFromRow)
246
246
  }
@@ -265,7 +265,7 @@ class PgDatasetIngestPersistence implements DatasetIngestPersistence {
265
265
  count(*) FILTER (WHERE rejected_at IS NOT NULL)::text AS rejected
266
266
  FROM dataset_ingest_deliveries
267
267
  WHERE project_id = ${projectId}
268
- AND (${datasetIds === undefined} OR dataset_id = ANY(${this.sql.array([...(datasetIds ?? [])])}::text[]))
268
+ AND (${datasetIds === undefined} OR dataset_id = ANY(${[...(datasetIds ?? [])]}::text[]))
269
269
  GROUP BY dataset_id
270
270
  `
271
271
  return rows.map((row) => ({
@@ -170,7 +170,7 @@ export class PgExecutionStorage implements ExecutionStorage {
170
170
  input: ListExecutionDescendantsInput
171
171
  ): Promise<readonly ExecutionDescendant[]> {
172
172
  if (input.executionIds.length === 0) return []
173
- const ids = this.sql.array([...new Set(input.executionIds)])
173
+ const ids = [...new Set(input.executionIds)]
174
174
 
175
175
  // UNION, not UNION ALL: an execution asked about may sit below another one asked about, and
176
176
  // the walk would otherwise reach what it caused twice.
@@ -516,7 +516,7 @@ export class PgProjectionRunStorage implements ProjectionRunStorage {
516
516
  WHERE project_id = ${input.projectId}
517
517
  AND projection_id = ${run.projection_id}
518
518
  AND materialization_protocol = ${"replacement"}
519
- AND status = ANY(${this.sql.array([...SUPERSEDING_PROJECTION_RUN_STATUSES])}::text[])
519
+ AND status = ANY(${[...SUPERSEDING_PROJECTION_RUN_STATUSES]}::text[])
520
520
  AND dataset_version_created_at COLLATE "C" > ${run.dataset_version_created_at} COLLATE "C"
521
521
  ORDER BY dataset_version_created_at COLLATE "C" DESC, id DESC
522
522
  LIMIT 1
@@ -11,6 +11,7 @@ import type {
11
11
  ConfirmRunExecutionInput,
12
12
  ExecutionStorage,
13
13
  FinishSyncRunInput,
14
+ ListLatestSyncConnectionRunsInput,
14
15
  ListLatestSyncRunsInput,
15
16
  ListLatestSyncRunsResult,
16
17
  ListLostRunsInput,
@@ -71,6 +72,7 @@ export class PgSyncRunStorage implements SyncRunStorage {
71
72
  id,
72
73
  execution_id,
73
74
  sync_id,
75
+ connection_id,
74
76
  dataset_id,
75
77
  mode,
76
78
  status,
@@ -83,6 +85,7 @@ export class PgSyncRunStorage implements SyncRunStorage {
83
85
  ${input.id},
84
86
  ${input.executionId},
85
87
  ${input.syncId},
88
+ ${input.connectionId ?? null},
86
89
  ${input.datasetId},
87
90
  ${input.mode},
88
91
  ${"queued"},
@@ -269,6 +272,22 @@ export class PgSyncRunStorage implements SyncRunStorage {
269
272
  params.push(...input.syncIds)
270
273
  }
271
274
 
275
+ if (input.connectionId === null) {
276
+ whereClauses.push("connection_id IS NULL")
277
+ } else if (input.connectionId !== undefined) {
278
+ whereClauses.push(`connection_id = $${index++}`)
279
+ params.push(input.connectionId)
280
+ }
281
+
282
+ if (input.visibleConnectionIds) {
283
+ whereClauses.push(
284
+ visibleConnectionsClause(input.visibleConnectionIds, (value) => {
285
+ params.push(value)
286
+ return `$${index++}`
287
+ })
288
+ )
289
+ }
290
+
272
291
  if (input.datasetId) {
273
292
  whereClauses.push(`dataset_id = $${index++}`)
274
293
  params.push(input.datasetId)
@@ -302,6 +321,7 @@ export class PgSyncRunStorage implements SyncRunStorage {
302
321
  }
303
322
 
304
323
  async listLatestBySyncIds(input: ListLatestSyncRunsInput): Promise<ListLatestSyncRunsResult> {
324
+ const { visibleConnectionIds } = input
305
325
  const rows = await queryLatestRunsByOwnerId<DatabaseRow>({
306
326
  sql: this.sql,
307
327
  tableName: "sync_runs",
@@ -310,12 +330,59 @@ export class PgSyncRunStorage implements SyncRunStorage {
310
330
  projectId: input.projectId,
311
331
  ownerIdFor: (row) => row.sync_id,
312
332
  selectList: "*, checkpoint IS NOT NULL AS checkpoint_present",
333
+ ...(visibleConnectionIds === undefined
334
+ ? {}
335
+ : { filter: (bind) => visibleConnectionsClause(visibleConnectionIds, bind) }),
313
336
  })
314
337
 
315
338
  return {
316
339
  runs: rows.map(rowToSyncRunRecord),
317
340
  }
318
341
  }
342
+
343
+ async listLatestByConnection(
344
+ input: ListLatestSyncConnectionRunsInput
345
+ ): Promise<ListLatestSyncRunsResult> {
346
+ if (input.syncIds.length === 0 || input.connectionIds?.length === 0) return { runs: [] }
347
+
348
+ const params: SqlParameter[] = [input.projectId]
349
+ const placeholders = (values: readonly string[]) =>
350
+ values
351
+ .map((value) => {
352
+ params.push(value)
353
+ return `$${params.length}`
354
+ })
355
+ .join(", ")
356
+ const syncFilter = `sync_id IN (${placeholders(input.syncIds)})`
357
+ const connectionFilter =
358
+ input.connectionIds === undefined
359
+ ? "connection_id IS NOT NULL"
360
+ : `connection_id IN (${placeholders(input.connectionIds)})`
361
+
362
+ const rows = await this.sql.unsafe<DatabaseRow[]>(
363
+ `
364
+ SELECT DISTINCT ON (sync_id, connection_id)
365
+ *, checkpoint IS NOT NULL AS checkpoint_present
366
+ FROM sync_runs
367
+ WHERE project_id = $1 AND ${syncFilter} AND ${connectionFilter}
368
+ ORDER BY sync_id, connection_id, COALESCE(started_at, queued_at) DESC, id DESC
369
+ `,
370
+ params
371
+ )
372
+
373
+ return { runs: rows.map(rowToSyncRunRecord) }
374
+ }
375
+ }
376
+
377
+ /** `visibleConnectionIds` as a condition: runs of those connections, and runs that read none. */
378
+ function visibleConnectionsClause(
379
+ connectionIds: readonly string[],
380
+ bind: (value: string) => string
381
+ ): string {
382
+ if (connectionIds.length === 0) return "connection_id IS NULL"
383
+
384
+ const placeholders = connectionIds.map((id) => bind(id)).join(", ")
385
+ return `(connection_id IS NULL OR connection_id IN (${placeholders}))`
319
386
  }
320
387
 
321
388
  function staleExecution(id: string): SyncRunError {
@@ -336,6 +403,7 @@ function rowToSyncRunRecord(row: DatabaseRow): SyncRunRecord {
336
403
  projectId: row.project_id,
337
404
  executionId: row.execution_id,
338
405
  syncId: row.sync_id,
406
+ ...(row.connection_id === null ? {} : { connectionId: row.connection_id }),
339
407
  datasetId: row.dataset_id,
340
408
  mode: row.mode,
341
409
  status: row.status,
@@ -373,6 +441,7 @@ interface DatabaseRow extends PgRunExecutionColumns {
373
441
  id: string
374
442
  execution_id: string
375
443
  sync_id: string
444
+ connection_id: string | null
376
445
  dataset_id: string
377
446
  mode: SyncRunRecord["mode"]
378
447
  status: SyncRunRecord["status"]
@@ -299,7 +299,7 @@ export class PgWorkflowRunStorage implements WorkflowRunStorage {
299
299
  SELECT *
300
300
  FROM workflow_runs
301
301
  WHERE project_id = ${params.projectId}
302
- AND execution_id = ANY(${this.sql.array(executionIds)}::text[])
302
+ AND execution_id = ANY(${executionIds}::text[])
303
303
  `
304
304
 
305
305
  const byExecution = new Map(rows.map((row) => [row.execution_id, row]))
@@ -96,7 +96,7 @@ export class PgShareGrantStorage implements ShareGrantStorage {
96
96
  SELECT *
97
97
  FROM share_grants
98
98
  WHERE project_id = ${input.projectId}
99
- AND id = ANY(${this.sql.array(ids)}::text[])
99
+ AND id = ANY(${ids}::text[])
100
100
  `
101
101
 
102
102
  const byId = new Map(rows.map((row) => [row.id, row]))
@@ -174,8 +174,8 @@ export async function lockAdvisoryKeys(sql: SQLClient, keys: readonly string[]):
174
174
  await sql`
175
175
  SELECT pg_advisory_xact_lock(locks.first_key, locks.second_key)
176
176
  FROM unnest(
177
- ${sql.array(locks.map(([first]) => first))}::integer[],
178
- ${sql.array(locks.map(([, second]) => second))}::integer[]
177
+ ${locks.map(([first]) => first)}::integer[],
178
+ ${locks.map(([, second]) => second)}::integer[]
179
179
  ) WITH ORDINALITY AS locks(first_key, second_key, lock_order)
180
180
  ORDER BY locks.lock_order
181
181
  `