@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.
- package/README.md +1 -0
- package/dist/agents/agent-storage.d.ts +8 -2
- package/dist/agents/agent-storage.d.ts.map +1 -1
- package/dist/agents/approvals.d.ts +48 -0
- package/dist/agents/approvals.d.ts.map +1 -0
- package/dist/agents/checkpoints.d.ts +1 -0
- package/dist/agents/checkpoints.d.ts.map +1 -1
- package/dist/agents/index.d.ts +1 -0
- package/dist/agents/index.d.ts.map +1 -1
- package/dist/agents/memory.d.ts +19 -0
- package/dist/agents/memory.d.ts.map +1 -0
- package/dist/agents/messages.d.ts.map +1 -1
- package/dist/agents/rows.d.ts +15 -1
- package/dist/agents/rows.d.ts.map +1 -1
- package/dist/agents/runs.d.ts +3 -1
- package/dist/agents/runs.d.ts.map +1 -1
- package/dist/agents/settings.d.ts +29 -0
- package/dist/agents/settings.d.ts.map +1 -0
- package/dist/agents/threads.d.ts +10 -1
- package/dist/agents/threads.d.ts.map +1 -1
- package/dist/ai-cost-groups.d.ts.map +1 -1
- package/dist/auth-storage/auth-storage.d.ts +3 -2
- package/dist/auth-storage/auth-storage.d.ts.map +1 -1
- package/dist/auth-storage/sessions.d.ts +2 -1
- package/dist/auth-storage/sessions.d.ts.map +1 -1
- package/dist/auth-storage/users.d.ts +8 -4
- package/dist/auth-storage/users.d.ts.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6001 -2923
- package/dist/index.js.map +45 -37
- package/dist/migrations.d.ts.map +1 -1
- package/dist/objects/conditional-reader.d.ts +17 -0
- package/dist/objects/conditional-reader.d.ts.map +1 -0
- package/dist/objects/query-compiler.d.ts +27 -1
- package/dist/objects/query-compiler.d.ts.map +1 -1
- package/dist/objects/query-indexes.d.ts.map +1 -1
- package/dist/objects/read-scope.d.ts +10 -1
- package/dist/objects/read-scope.d.ts.map +1 -1
- package/dist/objects/reader.d.ts +2 -0
- package/dist/objects/reader.d.ts.map +1 -1
- package/dist/objects/scoped-reader.d.ts +12 -0
- package/dist/objects/scoped-reader.d.ts.map +1 -0
- package/dist/objects/selected-reader.d.ts +1 -0
- package/dist/objects/selected-reader.d.ts.map +1 -1
- package/dist/objects/storage.d.ts +7 -1
- package/dist/objects/storage.d.ts.map +1 -1
- package/dist/ontology-storage/index.d.ts +2 -0
- package/dist/ontology-storage/index.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-session.d.ts +6 -0
- package/dist/ontology-storage/materialization-session.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-writer.d.ts +15 -0
- package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
- package/dist/ontology-storage/materializations.d.ts.map +1 -1
- package/dist/ontology-storage/replacement-plans.d.ts.map +1 -1
- package/dist/ontology-storage/sources.d.ts +2 -1
- package/dist/ontology-storage/sources.d.ts.map +1 -1
- package/dist/ontology-storage/unique-values.d.ts +60 -0
- package/dist/ontology-storage/unique-values.d.ts.map +1 -0
- package/dist/pg-action-run-storage.d.ts +4 -0
- package/dist/pg-action-run-storage.d.ts.map +1 -1
- package/dist/pg-activity-storage.d.ts +22 -0
- package/dist/pg-activity-storage.d.ts.map +1 -0
- package/dist/pg-ai-cost-storage.d.ts.map +1 -1
- package/dist/pg-ai-usage-storage.d.ts +8 -1
- package/dist/pg-ai-usage-storage.d.ts.map +1 -1
- package/dist/pg-artifact-storage.d.ts +26 -0
- package/dist/pg-artifact-storage.d.ts.map +1 -0
- package/dist/pg-execution-storage.d.ts +3 -1
- package/dist/pg-execution-storage.d.ts.map +1 -1
- package/dist/pg-workflow-run-storage.d.ts +4 -0
- package/dist/pg-workflow-run-storage.d.ts.map +1 -1
- package/dist/share-grant-storage.d.ts +2 -1
- package/dist/share-grant-storage.d.ts.map +1 -1
- package/dist/storage-errors.d.ts +2 -0
- package/dist/storage-errors.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/agents/agent-storage.ts +11 -2
- package/src/agents/approvals.ts +400 -0
- package/src/agents/checkpoints.ts +8 -4
- package/src/agents/index.ts +1 -0
- package/src/agents/memory.ts +175 -0
- package/src/agents/messages.ts +14 -7
- package/src/agents/rows.ts +46 -1
- package/src/agents/runs.ts +154 -22
- package/src/agents/settings.ts +105 -0
- package/src/agents/threads.ts +379 -3
- package/src/ai-cost-groups.ts +11 -1
- package/src/auth-storage/auth-storage.ts +45 -13
- package/src/auth-storage/sessions.ts +20 -0
- package/src/auth-storage/users.ts +105 -43
- package/src/index.ts +10 -0
- package/src/migrations/063-artifacts.sql +67 -0
- package/src/migrations/064-drop-output-diagnostics.sql +14 -0
- package/src/migrations/065-plan-work-payload-statistics.sql +5 -0
- package/src/migrations/066-activity.sql +61 -0
- package/src/migrations/067-activity-timeline.sql +15 -0
- package/src/migrations/068-activity-interactions.sql +20 -0
- package/src/migrations/069-artifact-kinds.sql +4 -0
- package/src/migrations/070-artifact-state.sql +16 -0
- package/src/migrations/071-agent-thread-participants.sql +16 -0
- package/src/migrations/072-forked-context-checkpoints.sql +23 -0
- package/src/migrations/073-unique-values.sql +25 -0
- package/src/migrations/074-generated-media-usage.sql +21 -0
- package/src/migrations/075-model-call-files.sql +11 -0
- package/src/migrations/076-agent-memory.sql +33 -0
- package/src/migrations/077-artifact-threads.sql +21 -0
- package/src/migrations/078-agent-approvals.sql +58 -0
- package/src/migrations/079-agent-approval-resume.sql +20 -0
- package/src/migrations/080-subagent-threads.sql +145 -0
- package/src/migrations/081-subagent-questions.sql +6 -0
- package/src/migrations.ts +52 -0
- package/src/objects/conditional-reader.ts +44 -0
- package/src/objects/prepare-queries.ts +1 -1
- package/src/objects/query-compiler.ts +96 -33
- package/src/objects/query-indexes.ts +29 -10
- package/src/objects/read-scope.ts +140 -25
- package/src/objects/reader.ts +50 -6
- package/src/objects/scoped-reader.ts +101 -0
- package/src/objects/selected-reader.ts +16 -76
- package/src/objects/storage.ts +24 -5
- package/src/objects/text-count-index.ts +1 -0
- package/src/ontology-storage/index.ts +3 -0
- package/src/ontology-storage/materialization-session.ts +8 -0
- package/src/ontology-storage/materialization-writer.ts +76 -1
- package/src/ontology-storage/materializations.ts +20 -1
- package/src/ontology-storage/replacement-plans.ts +25 -7
- package/src/ontology-storage/sources.ts +37 -0
- package/src/ontology-storage/unique-values.ts +274 -0
- package/src/pg-action-run-storage.ts +23 -0
- package/src/pg-activity-storage.ts +486 -0
- package/src/pg-ai-cost-storage.ts +62 -12
- package/src/pg-ai-usage-storage.ts +90 -1
- package/src/pg-artifact-storage.ts +521 -0
- package/src/pg-execution-storage.ts +83 -0
- package/src/pg-workflow-run-storage.ts +23 -0
- package/src/share-grant-storage.ts +21 -0
- package/src/storage-errors.ts +7 -0
- 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"]
|
package/src/storage-errors.ts
CHANGED
|
@@ -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
|
package/src/transactions.ts
CHANGED
|
@@ -159,7 +159,7 @@ export async function runPgRepeatableReadTransaction<T>(
|
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
throw new Error(
|
|
162
|
-
'[SixbPg]
|
|
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
|
|