@sixb/pg 0.1.20 → 0.1.22

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 (139) hide show
  1. package/README.md +1 -0
  2. package/dist/agents/agent-storage.d.ts +8 -2
  3. package/dist/agents/agent-storage.d.ts.map +1 -1
  4. package/dist/agents/approvals.d.ts +48 -0
  5. package/dist/agents/approvals.d.ts.map +1 -0
  6. package/dist/agents/checkpoints.d.ts +1 -0
  7. package/dist/agents/checkpoints.d.ts.map +1 -1
  8. package/dist/agents/index.d.ts +1 -0
  9. package/dist/agents/index.d.ts.map +1 -1
  10. package/dist/agents/memory.d.ts +19 -0
  11. package/dist/agents/memory.d.ts.map +1 -0
  12. package/dist/agents/messages.d.ts.map +1 -1
  13. package/dist/agents/rows.d.ts +15 -1
  14. package/dist/agents/rows.d.ts.map +1 -1
  15. package/dist/agents/runs.d.ts +3 -1
  16. package/dist/agents/runs.d.ts.map +1 -1
  17. package/dist/agents/settings.d.ts +29 -0
  18. package/dist/agents/settings.d.ts.map +1 -0
  19. package/dist/agents/threads.d.ts +10 -1
  20. package/dist/agents/threads.d.ts.map +1 -1
  21. package/dist/ai-cost-groups.d.ts.map +1 -1
  22. package/dist/auth-storage/auth-storage.d.ts +3 -2
  23. package/dist/auth-storage/auth-storage.d.ts.map +1 -1
  24. package/dist/auth-storage/sessions.d.ts +2 -1
  25. package/dist/auth-storage/sessions.d.ts.map +1 -1
  26. package/dist/auth-storage/users.d.ts +8 -4
  27. package/dist/auth-storage/users.d.ts.map +1 -1
  28. package/dist/index.d.ts +4 -0
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +6001 -2923
  31. package/dist/index.js.map +45 -37
  32. package/dist/migrations.d.ts.map +1 -1
  33. package/dist/objects/conditional-reader.d.ts +17 -0
  34. package/dist/objects/conditional-reader.d.ts.map +1 -0
  35. package/dist/objects/query-compiler.d.ts +27 -1
  36. package/dist/objects/query-compiler.d.ts.map +1 -1
  37. package/dist/objects/query-indexes.d.ts.map +1 -1
  38. package/dist/objects/read-scope.d.ts +10 -1
  39. package/dist/objects/read-scope.d.ts.map +1 -1
  40. package/dist/objects/reader.d.ts +2 -0
  41. package/dist/objects/reader.d.ts.map +1 -1
  42. package/dist/objects/scoped-reader.d.ts +12 -0
  43. package/dist/objects/scoped-reader.d.ts.map +1 -0
  44. package/dist/objects/selected-reader.d.ts +1 -0
  45. package/dist/objects/selected-reader.d.ts.map +1 -1
  46. package/dist/objects/storage.d.ts +7 -1
  47. package/dist/objects/storage.d.ts.map +1 -1
  48. package/dist/ontology-storage/index.d.ts +2 -0
  49. package/dist/ontology-storage/index.d.ts.map +1 -1
  50. package/dist/ontology-storage/materialization-session.d.ts +6 -0
  51. package/dist/ontology-storage/materialization-session.d.ts.map +1 -1
  52. package/dist/ontology-storage/materialization-writer.d.ts +15 -0
  53. package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
  54. package/dist/ontology-storage/materializations.d.ts.map +1 -1
  55. package/dist/ontology-storage/replacement-plans.d.ts.map +1 -1
  56. package/dist/ontology-storage/sources.d.ts +2 -1
  57. package/dist/ontology-storage/sources.d.ts.map +1 -1
  58. package/dist/ontology-storage/unique-values.d.ts +60 -0
  59. package/dist/ontology-storage/unique-values.d.ts.map +1 -0
  60. package/dist/pg-action-run-storage.d.ts +4 -0
  61. package/dist/pg-action-run-storage.d.ts.map +1 -1
  62. package/dist/pg-activity-storage.d.ts +22 -0
  63. package/dist/pg-activity-storage.d.ts.map +1 -0
  64. package/dist/pg-ai-cost-storage.d.ts.map +1 -1
  65. package/dist/pg-ai-usage-storage.d.ts +8 -1
  66. package/dist/pg-ai-usage-storage.d.ts.map +1 -1
  67. package/dist/pg-artifact-storage.d.ts +26 -0
  68. package/dist/pg-artifact-storage.d.ts.map +1 -0
  69. package/dist/pg-execution-storage.d.ts +3 -1
  70. package/dist/pg-execution-storage.d.ts.map +1 -1
  71. package/dist/pg-workflow-run-storage.d.ts +4 -0
  72. package/dist/pg-workflow-run-storage.d.ts.map +1 -1
  73. package/dist/share-grant-storage.d.ts +2 -1
  74. package/dist/share-grant-storage.d.ts.map +1 -1
  75. package/dist/storage-errors.d.ts +2 -0
  76. package/dist/storage-errors.d.ts.map +1 -1
  77. package/package.json +3 -3
  78. package/src/agents/agent-storage.ts +11 -2
  79. package/src/agents/approvals.ts +400 -0
  80. package/src/agents/checkpoints.ts +8 -4
  81. package/src/agents/index.ts +1 -0
  82. package/src/agents/memory.ts +175 -0
  83. package/src/agents/messages.ts +14 -7
  84. package/src/agents/rows.ts +46 -1
  85. package/src/agents/runs.ts +154 -22
  86. package/src/agents/settings.ts +105 -0
  87. package/src/agents/threads.ts +379 -3
  88. package/src/ai-cost-groups.ts +11 -1
  89. package/src/auth-storage/auth-storage.ts +45 -13
  90. package/src/auth-storage/sessions.ts +20 -0
  91. package/src/auth-storage/users.ts +105 -43
  92. package/src/index.ts +10 -0
  93. package/src/migrations/063-artifacts.sql +67 -0
  94. package/src/migrations/064-drop-output-diagnostics.sql +14 -0
  95. package/src/migrations/065-plan-work-payload-statistics.sql +5 -0
  96. package/src/migrations/066-activity.sql +61 -0
  97. package/src/migrations/067-activity-timeline.sql +15 -0
  98. package/src/migrations/068-activity-interactions.sql +20 -0
  99. package/src/migrations/069-artifact-kinds.sql +4 -0
  100. package/src/migrations/070-artifact-state.sql +16 -0
  101. package/src/migrations/071-agent-thread-participants.sql +16 -0
  102. package/src/migrations/072-forked-context-checkpoints.sql +23 -0
  103. package/src/migrations/073-unique-values.sql +25 -0
  104. package/src/migrations/074-generated-media-usage.sql +21 -0
  105. package/src/migrations/075-model-call-files.sql +11 -0
  106. package/src/migrations/076-agent-memory.sql +33 -0
  107. package/src/migrations/077-artifact-threads.sql +21 -0
  108. package/src/migrations/078-agent-approvals.sql +58 -0
  109. package/src/migrations/079-agent-approval-resume.sql +20 -0
  110. package/src/migrations/080-subagent-threads.sql +145 -0
  111. package/src/migrations/081-subagent-questions.sql +6 -0
  112. package/src/migrations.ts +52 -0
  113. package/src/objects/conditional-reader.ts +44 -0
  114. package/src/objects/prepare-queries.ts +1 -1
  115. package/src/objects/query-compiler.ts +96 -33
  116. package/src/objects/query-indexes.ts +29 -10
  117. package/src/objects/read-scope.ts +140 -25
  118. package/src/objects/reader.ts +50 -6
  119. package/src/objects/scoped-reader.ts +101 -0
  120. package/src/objects/selected-reader.ts +16 -76
  121. package/src/objects/storage.ts +24 -5
  122. package/src/objects/text-count-index.ts +1 -0
  123. package/src/ontology-storage/index.ts +3 -0
  124. package/src/ontology-storage/materialization-session.ts +8 -0
  125. package/src/ontology-storage/materialization-writer.ts +76 -1
  126. package/src/ontology-storage/materializations.ts +20 -1
  127. package/src/ontology-storage/replacement-plans.ts +25 -7
  128. package/src/ontology-storage/sources.ts +37 -0
  129. package/src/ontology-storage/unique-values.ts +274 -0
  130. package/src/pg-action-run-storage.ts +23 -0
  131. package/src/pg-activity-storage.ts +486 -0
  132. package/src/pg-ai-cost-storage.ts +62 -12
  133. package/src/pg-ai-usage-storage.ts +90 -1
  134. package/src/pg-artifact-storage.ts +521 -0
  135. package/src/pg-execution-storage.ts +83 -0
  136. package/src/pg-workflow-run-storage.ts +23 -0
  137. package/src/share-grant-storage.ts +21 -0
  138. package/src/storage-errors.ts +7 -0
  139. package/src/transactions.ts +1 -1
@@ -1,4 +1,5 @@
1
1
  import {
2
+ compareExecutionCreation,
2
3
  type ExecutionStorageRow,
3
4
  executionRecordFromStorageRow,
4
5
  executionRecordToStorageRow,
@@ -6,8 +7,12 @@ import {
6
7
  } from "@sixb/core/internal/execution-storage"
7
8
  import type {
8
9
  CreateExecutionInput,
10
+ ExecutionDescendant,
9
11
  ExecutionRecord,
10
12
  ExecutionStorage,
13
+ ListExecutionDescendantsInput,
14
+ ListSessionExecutionsInput,
15
+ SessionExecutions,
11
16
  ShareSessionStorage,
12
17
  } from "@sixb/core/storage"
13
18
  import { ExecutionStorageError } from "@sixb/core/storage"
@@ -111,6 +116,84 @@ export class PgExecutionStorage implements ExecutionStorage {
111
116
  `
112
117
  return row ? pgExecutionRecord(row) : null
113
118
  }
119
+
120
+ async listBySession(input: ListSessionExecutionsInput): Promise<SessionExecutions> {
121
+ const session =
122
+ input.session.type === "user"
123
+ ? this.sql`authority_session_id = ${input.session.id}`
124
+ : this.sql`authority_delegation_session_id = ${input.session.id}`
125
+
126
+ // Each iteration of a recursive query reads the rows the previous one produced, so the walk goes
127
+ // level by level, roots first; with no ORDER BY outside, PostgreSQL evaluates only the rows the
128
+ // limit returns. Within the last level reached, which rows come first is unspecified.
129
+ const rows = await this.sql<(PgExecutionRow & { readonly depth: number })[]>`
130
+ WITH RECURSIVE tree AS (
131
+ (
132
+ SELECT *, 0 AS depth
133
+ FROM executions
134
+ WHERE project_id = ${input.projectId}
135
+ AND ${session}
136
+ AND parent_execution_id IS NULL
137
+ AND created_at >= ${input.from}
138
+ AND created_at < ${input.to}
139
+ ORDER BY created_at, id
140
+ LIMIT ${input.limit + 1}
141
+ )
142
+ UNION ALL
143
+ SELECT child.*, tree.depth + 1
144
+ FROM tree
145
+ JOIN executions child
146
+ ON child.project_id = tree.project_id
147
+ AND child.parent_execution_id = tree.id
148
+ )
149
+ SELECT * FROM tree
150
+ LIMIT ${input.limit + 1}
151
+ `
152
+
153
+ // Keep the first `limit` by depth, then oldest first, as the contract keeps them.
154
+ const records = rows
155
+ .map((row) => ({ depth: row.depth, record: pgExecutionRecord(row) }))
156
+ .sort(
157
+ (left, right) =>
158
+ left.depth - right.depth || compareExecutionCreation(left.record, right.record)
159
+ )
160
+ .slice(0, input.limit)
161
+ .map(({ record }) => record)
162
+
163
+ return {
164
+ executions: records.sort(compareExecutionCreation),
165
+ truncated: rows.length > input.limit,
166
+ }
167
+ }
168
+
169
+ async listDescendants(
170
+ input: ListExecutionDescendantsInput
171
+ ): Promise<readonly ExecutionDescendant[]> {
172
+ if (input.executionIds.length === 0) return []
173
+ const ids = this.sql.array([...new Set(input.executionIds)])
174
+
175
+ // UNION, not UNION ALL: an execution asked about may sit below another one asked about, and
176
+ // the walk would otherwise reach what it caused twice.
177
+ const rows = await this.sql<{ id: string; parent_id: string }[]>`
178
+ WITH RECURSIVE tree(id, parent_id) AS (
179
+ SELECT child.id, child.parent_execution_id
180
+ FROM executions child
181
+ WHERE child.project_id = ${input.projectId}
182
+ AND child.parent_execution_id = ANY(${ids}::text[])
183
+ UNION
184
+ SELECT child.id, child.parent_execution_id
185
+ FROM tree
186
+ JOIN executions child
187
+ ON child.project_id = ${input.projectId}
188
+ AND child.parent_execution_id = tree.id
189
+ )
190
+ SELECT id, parent_id
191
+ FROM tree
192
+ WHERE id <> ALL(${ids}::text[])
193
+ ORDER BY id COLLATE "C"
194
+ `
195
+ return rows.map((row) => ({ id: row.id, parentId: row.parent_id }))
196
+ }
114
197
  }
115
198
 
116
199
  /** An `executions` row as its record, for any store that reads executions alongside its own rows. */
@@ -314,6 +314,29 @@ export class PgWorkflowRunStorage implements WorkflowRunStorage {
314
314
  return row ? rowToWorkflowRunRecord(row) : null
315
315
  }
316
316
 
317
+ async getByExecutionIds(params: {
318
+ readonly projectId: string
319
+ readonly executionIds: readonly string[]
320
+ }): Promise<readonly WorkflowRunRecord[]> {
321
+ const executionIds = [...new Set(params.executionIds)]
322
+ if (executionIds.length === 0) {
323
+ return []
324
+ }
325
+
326
+ const rows = await this.sql<WorkflowRunDatabaseRow[]>`
327
+ SELECT *
328
+ FROM workflow_runs
329
+ WHERE project_id = ${params.projectId}
330
+ AND execution_id = ANY(${this.sql.array(executionIds)}::text[])
331
+ `
332
+
333
+ const byExecution = new Map(rows.map((row) => [row.execution_id, row]))
334
+ return executionIds.flatMap((executionId) => {
335
+ const row = byExecution.get(executionId)
336
+ return row ? [rowToWorkflowRunRecord(row)] : []
337
+ })
338
+ }
339
+
317
340
  async list(input: ListWorkflowRunsInput): Promise<ListWorkflowRunsResult> {
318
341
  if (hasEmptyStatuses(input) || input.workflowIds?.length === 0) {
319
342
  return {
@@ -8,6 +8,7 @@ import {
8
8
  import type {
9
9
  CreateShareGrantInput,
10
10
  GetShareGrantByIdInput,
11
+ GetShareGrantsByIdsInput,
11
12
  ListShareGrantsInput,
12
13
  ListShareGrantsResult,
13
14
  RevokeShareGrantInput,
@@ -85,6 +86,26 @@ export class PgShareGrantStorage implements ShareGrantStorage {
85
86
  return row ? recordFromRow(row) : null
86
87
  }
87
88
 
89
+ async getByIds(input: GetShareGrantsByIdsInput): Promise<readonly ShareGrantRecord[]> {
90
+ const ids = [...new Set(input.ids)]
91
+ if (ids.length === 0) {
92
+ return []
93
+ }
94
+
95
+ const rows = await this.sql<PgShareGrantRow[]>`
96
+ SELECT *
97
+ FROM share_grants
98
+ WHERE project_id = ${input.projectId}
99
+ AND id = ANY(${this.sql.array(ids)}::text[])
100
+ `
101
+
102
+ const byId = new Map(rows.map((row) => [row.id, row]))
103
+ return ids.flatMap((id) => {
104
+ const row = byId.get(id)
105
+ return row ? [recordFromRow(row)] : []
106
+ })
107
+ }
108
+
88
109
  async list(input: ListShareGrantsInput): Promise<ListShareGrantsResult> {
89
110
  const normalized = normalizeListShareGrantsInput(input)
90
111
  const where = ["project_id = $1"]
@@ -81,6 +81,13 @@ export function isUniqueViolation(error: unknown): boolean {
81
81
  return pgErrorCode(error) === UNIQUE_VIOLATION
82
82
  }
83
83
 
84
+ /** True when the error is a unique violation of exactly the named constraint. */
85
+ export function isUniqueViolationOf(error: unknown, constraint: string): boolean {
86
+ if (!isUniqueViolation(error)) return false
87
+
88
+ return (error as { readonly constraint_name?: unknown }).constraint_name === constraint
89
+ }
90
+
84
91
  /** True when the error is a PostgreSQL foreign-key violation (SQLSTATE 23503). */
85
92
  export function isForeignKeyViolation(error: unknown): boolean {
86
93
  return pgErrorCode(error) === FOREIGN_KEY_VIOLATION
@@ -159,7 +159,7 @@ export async function runPgRepeatableReadTransaction<T>(
159
159
  }
160
160
 
161
161
  throw new Error(
162
- '[SixbPg] Selected object reads cannot join an unverified PostgreSQL transaction. Use storage.transaction(..., { isolation: "serializable" }) when reading through tx.objects.'
162
+ '[SixbPg] Scoped object reads cannot join an unverified PostgreSQL transaction. Use storage.transaction(..., { isolation: "serializable" }) when reading through tx.objects.'
163
163
  )
164
164
  }
165
165