@sixb/pg 0.1.26 → 0.2.0
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 +25 -1
- package/dist/agents/agent-storage.d.ts +8 -3
- package/dist/agents/agent-storage.d.ts.map +1 -1
- package/dist/agents/computers.d.ts +22 -0
- package/dist/agents/computers.d.ts.map +1 -0
- package/dist/agents/rows.d.ts +3 -0
- package/dist/agents/rows.d.ts.map +1 -1
- package/dist/agents/runs.d.ts.map +1 -1
- package/dist/agents/threads.d.ts +2 -1
- package/dist/agents/threads.d.ts.map +1 -1
- package/dist/agents/token-rates.d.ts +13 -0
- package/dist/agents/token-rates.d.ts.map +1 -0
- package/dist/ai-accounting-filters.d.ts +12 -0
- package/dist/ai-accounting-filters.d.ts.map +1 -0
- package/dist/ai-cost-groups.d.ts +1 -1
- package/dist/ai-cost-groups.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +886 -158
- package/dist/index.js.map +26 -21
- package/dist/migrations.d.ts.map +1 -1
- package/dist/objects/partitions.d.ts +10 -0
- package/dist/objects/partitions.d.ts.map +1 -0
- package/dist/objects/prepare-queries.d.ts.map +1 -1
- package/dist/objects/query-compiler.d.ts +5 -0
- package/dist/objects/query-compiler.d.ts.map +1 -1
- package/dist/objects/query-indexes.d.ts.map +1 -1
- package/dist/objects/sql-text.d.ts +3 -0
- package/dist/objects/sql-text.d.ts.map +1 -0
- package/dist/objects/text-count-index.d.ts.map +1 -1
- package/dist/ontology-storage/commits.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-session.d.ts +7 -1
- package/dist/ontology-storage/materialization-session.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-writer.d.ts +2 -0
- package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
- package/dist/ontology-storage/materializations.d.ts +2 -1
- package/dist/ontology-storage/materializations.d.ts.map +1 -1
- package/dist/pg-ai-cost-storage.d.ts.map +1 -1
- package/dist/pg-client.d.ts +1 -1
- package/dist/pg-client.d.ts.map +1 -1
- package/dist/storage-errors.d.ts +4 -1
- package/dist/storage-errors.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/agents/agent-storage.ts +10 -3
- package/src/agents/computers.ts +95 -0
- package/src/agents/rows.ts +9 -1
- package/src/agents/runs.ts +25 -3
- package/src/agents/threads.ts +111 -20
- package/src/agents/token-rates.ts +76 -0
- package/src/ai-accounting-filters.ts +48 -0
- package/src/ai-cost-groups.ts +37 -12
- package/src/index.ts +1 -1
- package/src/migrations/084-agent-token-rates.sql +16 -0
- package/src/migrations/085-agent-computer-sessions.sql +27 -0
- package/src/migrations/086-partition-objects.sql +93 -0
- package/src/migrations.ts +8 -0
- package/src/objects/partitions.ts +163 -0
- package/src/objects/prepare-queries.ts +50 -2
- package/src/objects/query-compiler.ts +80 -66
- package/src/objects/query-indexes.ts +59 -46
- package/src/objects/sql-text.ts +8 -0
- package/src/objects/text-count-index.ts +17 -12
- package/src/ontology-storage/commits.ts +11 -2
- package/src/ontology-storage/materialization-session.ts +10 -1
- package/src/ontology-storage/materialization-writer.ts +14 -0
- package/src/ontology-storage/materializations.ts +81 -7
- package/src/pg-ai-cost-storage.ts +119 -15
- package/src/pg-client.ts +9 -2
- package/src/storage-errors.ts +28 -1
package/src/agents/threads.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
|
+
assertAgentThreadComputer,
|
|
2
3
|
assertAssignableComputer,
|
|
3
|
-
|
|
4
|
+
assertComputerPlace,
|
|
4
5
|
assertConversationThreadRow,
|
|
5
|
-
|
|
6
|
+
assertMovableToCloud,
|
|
7
|
+
movesComputerPlace,
|
|
6
8
|
participantsToAdd,
|
|
7
9
|
planAgentThreadFork,
|
|
10
|
+
queuedTurnInCloud,
|
|
8
11
|
remapArtifactIds,
|
|
9
12
|
snapshotAgentThreadSandboxParams,
|
|
10
13
|
transitionAgentThreadSandbox,
|
|
@@ -12,6 +15,7 @@ import {
|
|
|
12
15
|
import {
|
|
13
16
|
type AddAgentThreadParticipantsInput,
|
|
14
17
|
type AgentContextCheckpointRecord,
|
|
18
|
+
type AgentRunRecord,
|
|
15
19
|
AgentStorageError,
|
|
16
20
|
type AgentThreadParticipantRecord,
|
|
17
21
|
type AgentThreadRecord,
|
|
@@ -24,6 +28,8 @@ import {
|
|
|
24
28
|
type ListAgentThreadParticipantsInput,
|
|
25
29
|
type ListAgentThreadsInput,
|
|
26
30
|
type ListAgentThreadsResult,
|
|
31
|
+
type MoveAgentThreadToCloudInput,
|
|
32
|
+
type MoveAgentThreadToCloudResult,
|
|
27
33
|
type RemoveAgentThreadParticipantInput,
|
|
28
34
|
type TransitionAgentThreadSandboxInput,
|
|
29
35
|
} from "@sixb/core/storage"
|
|
@@ -74,7 +80,7 @@ export class PgAgentThreadStore implements AgentThreadStore {
|
|
|
74
80
|
}
|
|
75
81
|
|
|
76
82
|
async create(input: CreateAgentThreadInput): Promise<AgentThreadRecord> {
|
|
77
|
-
|
|
83
|
+
assertComputerPlace(input.runsOn, input.computer, "SixbPg")
|
|
78
84
|
const sandboxParams =
|
|
79
85
|
input.sandboxParams === undefined
|
|
80
86
|
? null
|
|
@@ -85,7 +91,11 @@ export class PgAgentThreadStore implements AgentThreadStore {
|
|
|
85
91
|
const runsOn =
|
|
86
92
|
input.runsOn === undefined
|
|
87
93
|
? { column: this.sql``, value: this.sql`` }
|
|
88
|
-
: {
|
|
94
|
+
: {
|
|
95
|
+
column: this.sql`runs_on, computer_id, computer_name, computer_folder,`,
|
|
96
|
+
value: this
|
|
97
|
+
.sql`${input.runsOn}, ${input.computer?.id ?? null}, ${input.computer?.name ?? null}, ${input.computer?.folder ?? null},`,
|
|
98
|
+
}
|
|
89
99
|
|
|
90
100
|
try {
|
|
91
101
|
const [row] = await this.sql<AgentThreadRow[]>`
|
|
@@ -133,8 +143,10 @@ export class PgAgentThreadStore implements AgentThreadStore {
|
|
|
133
143
|
return await runPgTransaction(this.sql, async (tx) => {
|
|
134
144
|
// A share lock holds back the writes that take the thread for update: an admitted request
|
|
135
145
|
// or a finalized answer lands before the copy or after it, never inside it.
|
|
136
|
-
const [source] = await tx<
|
|
137
|
-
|
|
146
|
+
const [source] = await tx<
|
|
147
|
+
Pick<AgentThreadRow, "id" | "kind" | "active_run_id" | "runs_on" | "moved_at">[]
|
|
148
|
+
>`
|
|
149
|
+
SELECT id, kind, active_run_id, runs_on, moved_at FROM agent_threads
|
|
138
150
|
WHERE project_id = ${input.projectId} AND id = ${input.sourceThreadId}
|
|
139
151
|
FOR SHARE
|
|
140
152
|
`
|
|
@@ -178,6 +190,7 @@ export class PgAgentThreadStore implements AgentThreadStore {
|
|
|
178
190
|
const plan = planAgentThreadFork(
|
|
179
191
|
input,
|
|
180
192
|
{
|
|
193
|
+
workspace: { runsOn: source.runs_on, movedAt: source.moved_at },
|
|
181
194
|
...(activeRun?.trigger_message_id
|
|
182
195
|
? { triggerMessageId: activeRun.trigger_message_id }
|
|
183
196
|
: {}),
|
|
@@ -204,6 +217,7 @@ export class PgAgentThreadStore implements AgentThreadStore {
|
|
|
204
217
|
status,
|
|
205
218
|
last_message_at,
|
|
206
219
|
message_count,
|
|
220
|
+
moved_at,
|
|
207
221
|
created_at,
|
|
208
222
|
updated_at
|
|
209
223
|
) VALUES (
|
|
@@ -217,6 +231,7 @@ export class PgAgentThreadStore implements AgentThreadStore {
|
|
|
217
231
|
${thread.status},
|
|
218
232
|
${thread.lastMessageAt ?? null},
|
|
219
233
|
${thread.messageCount},
|
|
234
|
+
${thread.movedAt ?? null},
|
|
220
235
|
${thread.createdAt},
|
|
221
236
|
${thread.updatedAt}
|
|
222
237
|
)
|
|
@@ -366,6 +381,13 @@ export class PgAgentThreadStore implements AgentThreadStore {
|
|
|
366
381
|
params.push(input.participantPrincipal.type, input.participantPrincipal.id)
|
|
367
382
|
}
|
|
368
383
|
|
|
384
|
+
if (input.activeOnComputer) {
|
|
385
|
+
whereClauses.push(
|
|
386
|
+
`computer_id = $${index++} AND computer_folder = $${index++} AND active_run_id IS NOT NULL`
|
|
387
|
+
)
|
|
388
|
+
params.push(input.activeOnComputer.id, input.activeOnComputer.folder)
|
|
389
|
+
}
|
|
390
|
+
|
|
369
391
|
const dir = input.order === "asc" ? "ASC" : "DESC"
|
|
370
392
|
const { rows, total, hasMore } = await queryAgentList<AgentThreadRow>({
|
|
371
393
|
sql: this.sql,
|
|
@@ -445,21 +467,50 @@ export class PgAgentThreadStore implements AgentThreadStore {
|
|
|
445
467
|
}
|
|
446
468
|
|
|
447
469
|
async assignComputer(input: AssignAgentThreadComputerInput): Promise<AgentThreadRecord> {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
470
|
+
assertAgentThreadComputer(input.computer, "SixbPg")
|
|
471
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
472
|
+
const { thread, active } = await lockThreadAndActiveRun(tx, input)
|
|
473
|
+
assertAssignableComputer(thread, active, input.computer, input.id, "SixbPg")
|
|
474
|
+
const movedAt = movesComputerPlace(thread, input.computer)
|
|
475
|
+
? (input.movedAt ?? new Date())
|
|
476
|
+
: null
|
|
477
|
+
const [assigned] = await tx<AgentThreadRow[]>`
|
|
478
|
+
UPDATE agent_threads
|
|
479
|
+
SET computer_id = ${input.computer.id},
|
|
480
|
+
computer_name = ${input.computer.name},
|
|
481
|
+
computer_folder = ${input.computer.folder},
|
|
482
|
+
moved_at = COALESCE(${movedAt}::timestamptz, moved_at)
|
|
483
|
+
WHERE project_id = ${input.projectId} AND id = ${input.id}
|
|
484
|
+
RETURNING *
|
|
485
|
+
`
|
|
486
|
+
return rowToThreadRecord(assigned as AgentThreadRow)
|
|
487
|
+
})
|
|
488
|
+
}
|
|
458
489
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
490
|
+
async moveToCloud(input: MoveAgentThreadToCloudInput): Promise<MoveAgentThreadToCloudResult> {
|
|
491
|
+
return runPgTransaction(this.sql, async (tx) => {
|
|
492
|
+
const { thread, active } = await lockThreadAndActiveRun(tx, input)
|
|
493
|
+
assertMovableToCloud(thread, active, input.id, "SixbPg")
|
|
494
|
+
|
|
495
|
+
const queued = queuedTurnInCloud(active)
|
|
496
|
+
if (queued) {
|
|
497
|
+
await tx`
|
|
498
|
+
UPDATE agent_runs SET spec = ${JSON.stringify(queued.spec)}::text::jsonb
|
|
499
|
+
WHERE project_id = ${input.projectId} AND id = ${queued.id}
|
|
500
|
+
`
|
|
501
|
+
}
|
|
502
|
+
const [movedThread] = await tx<AgentThreadRow[]>`
|
|
503
|
+
UPDATE agent_threads
|
|
504
|
+
SET runs_on = NULL, computer_id = NULL, computer_name = NULL, computer_folder = NULL,
|
|
505
|
+
moved_at = ${input.movedAt ?? new Date()}
|
|
506
|
+
WHERE project_id = ${input.projectId} AND id = ${input.id}
|
|
507
|
+
RETURNING *
|
|
508
|
+
`
|
|
509
|
+
return {
|
|
510
|
+
thread: rowToThreadRecord(movedThread as AgentThreadRow),
|
|
511
|
+
...(queued ? { queued } : {}),
|
|
512
|
+
}
|
|
513
|
+
})
|
|
463
514
|
}
|
|
464
515
|
|
|
465
516
|
async listParticipants(
|
|
@@ -469,6 +520,46 @@ export class PgAgentThreadStore implements AgentThreadStore {
|
|
|
469
520
|
}
|
|
470
521
|
}
|
|
471
522
|
|
|
523
|
+
/**
|
|
524
|
+
* Lock a thread that is about to move, with its active turn. Same lock order as run finalization:
|
|
525
|
+
* run, then thread. The thread names the run to lock, so it is read again once both are held, in
|
|
526
|
+
* case its turn changed in between.
|
|
527
|
+
*/
|
|
528
|
+
async function lockThreadAndActiveRun(
|
|
529
|
+
tx: PgStoreClient,
|
|
530
|
+
input: { readonly projectId: string; readonly id: string }
|
|
531
|
+
): Promise<{ readonly thread: AgentThreadRecord | null; readonly active: AgentRunRecord | null }> {
|
|
532
|
+
for (let attempt = 0; ; attempt += 1) {
|
|
533
|
+
const [peek] = await tx<Pick<AgentThreadRow, "active_run_id">[]>`
|
|
534
|
+
SELECT active_run_id FROM agent_threads
|
|
535
|
+
WHERE project_id = ${input.projectId} AND id = ${input.id}
|
|
536
|
+
`
|
|
537
|
+
const runId = peek?.active_run_id ?? null
|
|
538
|
+
const runs = runId
|
|
539
|
+
? await tx<AgentRunRow[]>`
|
|
540
|
+
SELECT * FROM agent_runs WHERE project_id = ${input.projectId} AND id = ${runId}
|
|
541
|
+
FOR UPDATE
|
|
542
|
+
`
|
|
543
|
+
: []
|
|
544
|
+
const [row] = await tx<AgentThreadRow[]>`
|
|
545
|
+
SELECT * FROM agent_threads WHERE project_id = ${input.projectId} AND id = ${input.id}
|
|
546
|
+
FOR UPDATE
|
|
547
|
+
`
|
|
548
|
+
if ((row?.active_run_id ?? null) === runId) {
|
|
549
|
+
return {
|
|
550
|
+
thread: row ? rowToThreadRecord(row) : null,
|
|
551
|
+
active: runs[0] ? rowToRunRecord(runs[0]) : null,
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
if (attempt >= 3) {
|
|
555
|
+
throw new AgentStorageError(
|
|
556
|
+
"invalid_state",
|
|
557
|
+
`[SixbPg] Agent thread '${input.id}' kept changing turns while it moved: try again.`
|
|
558
|
+
)
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
472
563
|
async function selectParticipants(
|
|
473
564
|
sql: PgStoreClient,
|
|
474
565
|
projectId: string,
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AGENT_TOKEN_RATE_SAMPLES,
|
|
3
|
+
assertAgentTokenRateRecord,
|
|
4
|
+
} from "@sixb/core/internal/agent-run-storage-provider"
|
|
5
|
+
import type { AgentTokenRateRecord, AgentTokenRateStore } from "@sixb/core/storage"
|
|
6
|
+
import { type PgStoreClient, runPgTransaction } from "../transactions"
|
|
7
|
+
|
|
8
|
+
interface TokenRateRow {
|
|
9
|
+
readonly project_id: string
|
|
10
|
+
readonly run_id: string
|
|
11
|
+
readonly provider_id: string
|
|
12
|
+
readonly model_id: string
|
|
13
|
+
readonly bytes: number
|
|
14
|
+
readonly tokens: number
|
|
15
|
+
readonly measured_at: Date
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class PgAgentTokenRateStore implements AgentTokenRateStore {
|
|
19
|
+
constructor(private readonly sql: PgStoreClient) {}
|
|
20
|
+
|
|
21
|
+
async record(input: AgentTokenRateRecord): Promise<void> {
|
|
22
|
+
assertAgentTokenRateRecord(input, "SixbPg")
|
|
23
|
+
|
|
24
|
+
await runPgTransaction(this.sql, async (tx) => {
|
|
25
|
+
await tx`
|
|
26
|
+
INSERT INTO agent_token_rates (
|
|
27
|
+
project_id, run_id, provider_id, model_id, bytes, tokens, measured_at
|
|
28
|
+
) VALUES (
|
|
29
|
+
${input.projectId}, ${input.runId}, ${input.providerId}, ${input.modelId},
|
|
30
|
+
${input.bytes}, ${input.tokens}, ${input.measuredAt}
|
|
31
|
+
)
|
|
32
|
+
ON CONFLICT (project_id, run_id) DO UPDATE SET
|
|
33
|
+
provider_id = EXCLUDED.provider_id, model_id = EXCLUDED.model_id,
|
|
34
|
+
bytes = EXCLUDED.bytes, tokens = EXCLUDED.tokens, measured_at = EXCLUDED.measured_at
|
|
35
|
+
`
|
|
36
|
+
|
|
37
|
+
// A statement of its own: one sharing the insert's snapshot would not see the new row.
|
|
38
|
+
await tx`
|
|
39
|
+
DELETE FROM agent_token_rates
|
|
40
|
+
WHERE project_id = ${input.projectId} AND provider_id = ${input.providerId}
|
|
41
|
+
AND model_id = ${input.modelId}
|
|
42
|
+
AND run_id NOT IN (
|
|
43
|
+
SELECT run_id FROM agent_token_rates
|
|
44
|
+
WHERE project_id = ${input.projectId} AND provider_id = ${input.providerId}
|
|
45
|
+
AND model_id = ${input.modelId}
|
|
46
|
+
ORDER BY measured_at DESC, run_id DESC
|
|
47
|
+
LIMIT ${AGENT_TOKEN_RATE_SAMPLES}
|
|
48
|
+
)
|
|
49
|
+
`
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async listLatest(input: {
|
|
54
|
+
readonly projectId: string
|
|
55
|
+
readonly providerId: string
|
|
56
|
+
readonly modelId: string
|
|
57
|
+
}): Promise<readonly AgentTokenRateRecord[]> {
|
|
58
|
+
const rows = await this.sql<TokenRateRow[]>`
|
|
59
|
+
SELECT project_id, run_id, provider_id, model_id, bytes, tokens, measured_at
|
|
60
|
+
FROM agent_token_rates
|
|
61
|
+
WHERE project_id = ${input.projectId} AND provider_id = ${input.providerId}
|
|
62
|
+
AND model_id = ${input.modelId}
|
|
63
|
+
ORDER BY measured_at DESC, run_id DESC
|
|
64
|
+
LIMIT ${AGENT_TOKEN_RATE_SAMPLES}
|
|
65
|
+
`
|
|
66
|
+
return rows.map((row) => ({
|
|
67
|
+
projectId: row.project_id,
|
|
68
|
+
providerId: row.provider_id,
|
|
69
|
+
modelId: row.model_id,
|
|
70
|
+
runId: row.run_id,
|
|
71
|
+
bytes: row.bytes,
|
|
72
|
+
tokens: row.tokens,
|
|
73
|
+
measuredAt: row.measured_at,
|
|
74
|
+
}))
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { normalizeAiAccountingQuery } from "@sixb/core/internal/ai-cost-storage-provider"
|
|
2
|
+
|
|
3
|
+
// The kind, source and requester filters every accounting read shares, over the joins it makes:
|
|
4
|
+
// `usage`, `execution`, `direct_agent`, `workflow_node`.
|
|
5
|
+
|
|
6
|
+
/** The filter's values, each `null` when the query leaves that part open. */
|
|
7
|
+
export function pgAccountingFilterValues(query: ReturnType<typeof normalizeAiAccountingQuery>): {
|
|
8
|
+
readonly kind: string | null
|
|
9
|
+
readonly source: string | null
|
|
10
|
+
readonly workflow: string | null
|
|
11
|
+
readonly mode: string | null
|
|
12
|
+
readonly id: string | null
|
|
13
|
+
} {
|
|
14
|
+
return {
|
|
15
|
+
kind: query.modelKind ?? null,
|
|
16
|
+
source: query.source?.kind ?? null,
|
|
17
|
+
workflow: query.source?.kind === "workflow" ? (query.source.workflowId ?? null) : null,
|
|
18
|
+
mode:
|
|
19
|
+
query.requester === undefined
|
|
20
|
+
? null
|
|
21
|
+
: query.requester === "none"
|
|
22
|
+
? "none"
|
|
23
|
+
: query.requester.type,
|
|
24
|
+
id: query.requester === undefined || query.requester === "none" ? null : query.requester.id,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The filter as SQL text over numbered `$n` parameters from `start`, for `sql.unsafe`. */
|
|
29
|
+
export function pgAccountingFilterSql(start: number): string {
|
|
30
|
+
const [kind, source, workflow, mode, id] = [0, 1, 2, 3, 4].map((offset) => `$${start + offset}`)
|
|
31
|
+
const sourceWorkflow = `COALESCE(workflow_node.workflow_id,
|
|
32
|
+
CASE WHEN execution.executor_kind = 'workflow' THEN execution.authority_primitive_id END)`
|
|
33
|
+
return `(${kind}::text IS NULL OR usage.model_kind = ${kind})
|
|
34
|
+
AND (${source}::text IS NULL OR (CASE
|
|
35
|
+
WHEN direct_agent.id IS NOT NULL THEN 'agent'
|
|
36
|
+
WHEN ${sourceWorkflow} IS NOT NULL THEN 'workflow'
|
|
37
|
+
WHEN execution.executor_kind = 'action' THEN 'action'
|
|
38
|
+
ELSE 'other'
|
|
39
|
+
END) = ${source})
|
|
40
|
+
AND (${workflow}::text IS NULL OR ${sourceWorkflow} = ${workflow})
|
|
41
|
+
AND (
|
|
42
|
+
${mode}::text IS NULL
|
|
43
|
+
OR (${mode} = 'none' AND execution.requested_by_user_id IS NULL
|
|
44
|
+
AND execution.requested_by_service_account_id IS NULL)
|
|
45
|
+
OR (${mode} = 'user' AND execution.requested_by_user_id = ${id})
|
|
46
|
+
OR (${mode} = 'serviceAccount' AND execution.requested_by_service_account_id = ${id})
|
|
47
|
+
)`
|
|
48
|
+
}
|
package/src/ai-cost-groups.ts
CHANGED
|
@@ -3,17 +3,21 @@ import {
|
|
|
3
3
|
aiModelCallGroupFragmentFromRow,
|
|
4
4
|
buildAiModelCallGroupsFromFragments,
|
|
5
5
|
normalizeAiModelCallAccountingQuery,
|
|
6
|
+
normalizeAiModelCallGroupSort,
|
|
6
7
|
TOKENLESS_MODEL_KINDS,
|
|
7
8
|
} from "@sixb/core/internal/ai-cost-storage-provider"
|
|
8
9
|
import type { ListAiModelCallGroupsInput, ListAiModelCallGroupsResult } from "@sixb/core/storage"
|
|
10
|
+
import { pgAccountingFilterSql, pgAccountingFilterValues } from "./ai-accounting-filters"
|
|
9
11
|
import type { PgStoreClient } from "./transactions"
|
|
10
12
|
|
|
11
|
-
/** Paginate initiating executions, then aggregate their matching calls without loading usage payloads. */
|
|
13
|
+
/** Paginate conversations, workflow runs and other initiating executions, then aggregate their matching calls without loading usage payloads. */
|
|
12
14
|
export async function listAiModelCallGroups(
|
|
13
15
|
sql: PgStoreClient,
|
|
14
16
|
input: ListAiModelCallGroupsInput
|
|
15
17
|
): Promise<ListAiModelCallGroupsResult> {
|
|
16
18
|
const query = normalizeAiModelCallAccountingQuery(input)
|
|
19
|
+
const sort = normalizeAiModelCallGroupSort(input.sort)
|
|
20
|
+
const filter = pgAccountingFilterValues(query)
|
|
17
21
|
const parameters = [
|
|
18
22
|
query.projectId,
|
|
19
23
|
query.from.toISOString(),
|
|
@@ -21,17 +25,23 @@ export async function listAiModelCallGroups(
|
|
|
21
25
|
query.providerId ?? null,
|
|
22
26
|
query.modelId ?? null,
|
|
23
27
|
query.valuationStatus ?? null,
|
|
28
|
+
filter.kind,
|
|
29
|
+
filter.source,
|
|
30
|
+
filter.workflow,
|
|
31
|
+
filter.mode,
|
|
32
|
+
filter.id,
|
|
24
33
|
]
|
|
25
34
|
const [count] = await sql.unsafe<{ total: string }[]>(COUNT_SQL, parameters)
|
|
26
35
|
const rows = await sql.unsafe<AiModelCallGroupRow[]>(PAGE_SQL, [
|
|
27
36
|
...parameters,
|
|
28
37
|
query.limit,
|
|
29
38
|
query.offset,
|
|
39
|
+
sort,
|
|
30
40
|
])
|
|
31
41
|
const total = Number(count?.total)
|
|
32
42
|
if (!Number.isSafeInteger(total) || total < 0)
|
|
33
43
|
throw new RangeError("[Sixb] Invalid model-call group count.")
|
|
34
|
-
const items = buildAiModelCallGroupsFromFragments(rows.map(aiModelCallGroupFragmentFromRow))
|
|
44
|
+
const items = buildAiModelCallGroupsFromFragments(rows.map(aiModelCallGroupFragmentFromRow), sort)
|
|
35
45
|
return { items, total, hasMore: query.offset + items.length < total }
|
|
36
46
|
}
|
|
37
47
|
|
|
@@ -41,8 +51,15 @@ const TOKENLESS_KINDS = TOKENLESS_MODEL_KINDS.map((kind) => `'${kind}'`).join(",
|
|
|
41
51
|
const FILTERED_SQL = `
|
|
42
52
|
WITH filtered AS (
|
|
43
53
|
SELECT COALESCE(root_agent.execution_id, usage.execution_id) AS root_execution_id,
|
|
54
|
+
-- What the call is listed under, as aiModelCallGroupKey computes it.
|
|
55
|
+
CASE
|
|
56
|
+
WHEN root_agent.thread_id IS NOT NULL THEN 'thread:' || root_agent.thread_id
|
|
57
|
+
WHEN workflow_node.workflow_run_id IS NOT NULL THEN 'workflowRun:' || workflow_node.workflow_run_id
|
|
58
|
+
ELSE COALESCE(root_agent.execution_id, usage.execution_id)
|
|
59
|
+
END AS group_key,
|
|
44
60
|
usage.execution_id, usage.provider_id, usage.requested_model_id, usage.occurred_at,
|
|
45
|
-
usage.total_tokens, usage.
|
|
61
|
+
usage.total_tokens, usage.input_tokens, usage.cache_read_input_tokens,
|
|
62
|
+
usage.model_kind, usage.output_images, usage.input_characters,
|
|
46
63
|
usage.audio_duration_ms,
|
|
47
64
|
execution.executor_kind, execution.executor_id, execution.authority_primitive_id AS primitive_id,
|
|
48
65
|
COALESCE(cost.status, 'unvalued') AS valuation_status,
|
|
@@ -55,7 +72,8 @@ WITH filtered AS (
|
|
|
55
72
|
workflow_node.node_id AS workflow_agent_step_id,
|
|
56
73
|
workflow_agent.node_run_id AS workflow_node_run_id,
|
|
57
74
|
workflow_node.workflow_id AS workflow_id,
|
|
58
|
-
workflow_node.workflow_run_id AS workflow_run_id
|
|
75
|
+
workflow_node.workflow_run_id AS workflow_run_id,
|
|
76
|
+
execution.requested_by_user_id, execution.requested_by_service_account_id
|
|
59
77
|
FROM ai_model_call_usage AS usage
|
|
60
78
|
JOIN executions AS execution ON execution.project_id = usage.project_id AND execution.id = usage.execution_id
|
|
61
79
|
LEFT JOIN agent_runs AS direct_agent
|
|
@@ -75,23 +93,28 @@ WITH filtered AS (
|
|
|
75
93
|
AND ($4::text IS NULL OR usage.provider_id = $4)
|
|
76
94
|
AND ($5::text IS NULL OR usage.requested_model_id = $5)
|
|
77
95
|
AND ($6::text IS NULL OR COALESCE(cost.status, 'unvalued') = $6)
|
|
96
|
+
AND ${pgAccountingFilterSql(7)}
|
|
78
97
|
), roots AS (
|
|
79
|
-
SELECT
|
|
80
|
-
FROM filtered GROUP BY
|
|
98
|
+
SELECT group_key, MAX(occurred_at) AS last_call_at, SUM(COALESCE(amount_nanos, 0)) AS cost_nanos
|
|
99
|
+
FROM filtered GROUP BY group_key
|
|
81
100
|
)
|
|
82
101
|
`
|
|
83
102
|
const COUNT_SQL = `${FILTERED_SQL} SELECT CAST(COUNT(*) AS TEXT) AS total FROM roots`
|
|
84
103
|
const PAGE_SQL = `${FILTERED_SQL}, page AS (
|
|
85
|
-
SELECT
|
|
86
|
-
ORDER BY
|
|
104
|
+
SELECT group_key FROM roots
|
|
105
|
+
ORDER BY CASE WHEN $14::text = 'cost' THEN cost_nanos END DESC NULLS LAST,
|
|
106
|
+
last_call_at DESC, group_key COLLATE "C" ASC
|
|
107
|
+
LIMIT $12 OFFSET $13
|
|
87
108
|
)
|
|
88
|
-
SELECT filtered.root_execution_id, execution_id, provider_id, requested_model_id,
|
|
109
|
+
SELECT filtered.group_key, root_execution_id, execution_id, provider_id, requested_model_id,
|
|
89
110
|
MIN(occurred_at) AS first_call_at, MAX(occurred_at) AS last_call_at,
|
|
90
111
|
CAST(COUNT(*) AS TEXT) AS call_count,
|
|
91
112
|
CAST(COUNT(total_tokens) AS TEXT) AS token_count,
|
|
92
113
|
CAST(COUNT(CASE WHEN total_tokens IS NULL AND model_kind IN (${TOKENLESS_KINDS}) THEN 1 END) AS TEXT)
|
|
93
114
|
AS tokenless_count,
|
|
94
115
|
CAST(SUM(total_tokens) AS TEXT) AS total_tokens,
|
|
116
|
+
CAST(SUM(input_tokens) AS TEXT) AS input_tokens,
|
|
117
|
+
CAST(SUM(cache_read_input_tokens) AS TEXT) AS cache_read_input_tokens,
|
|
95
118
|
CAST(SUM(output_images) AS TEXT) AS output_images,
|
|
96
119
|
CAST(SUM(input_characters) AS TEXT) AS input_characters,
|
|
97
120
|
CAST(SUM(audio_duration_ms) AS TEXT) AS audio_duration_ms,
|
|
@@ -108,8 +131,10 @@ SELECT filtered.root_execution_id, execution_id, provider_id, requested_model_id
|
|
|
108
131
|
MAX(workflow_agent_step_id) AS workflow_agent_step_id,
|
|
109
132
|
MAX(workflow_node_run_id) AS workflow_node_run_id,
|
|
110
133
|
MAX(workflow_id) AS workflow_id,
|
|
111
|
-
MAX(workflow_run_id) AS workflow_run_id
|
|
112
|
-
|
|
113
|
-
|
|
134
|
+
MAX(workflow_run_id) AS workflow_run_id,
|
|
135
|
+
MAX(requested_by_user_id) AS requested_by_user_id,
|
|
136
|
+
MAX(requested_by_service_account_id) AS requested_by_service_account_id
|
|
137
|
+
FROM filtered JOIN page ON page.group_key = filtered.group_key
|
|
138
|
+
GROUP BY filtered.group_key, root_execution_id, execution_id, provider_id, requested_model_id,
|
|
114
139
|
valuation_status, amount_currency
|
|
115
140
|
`
|
package/src/index.ts
CHANGED
|
@@ -98,7 +98,7 @@ export interface PostgresStorageOptions {
|
|
|
98
98
|
max?: number
|
|
99
99
|
/** Concurrent root count/facet calculations per instance; defaults to 1. */
|
|
100
100
|
maxConcurrentAggregates?: number
|
|
101
|
-
/** Idle connection timeout in milliseconds. Defaults to
|
|
101
|
+
/** Idle connection timeout in milliseconds. Defaults to 60000. */
|
|
102
102
|
idleTimeoutMillis?: number
|
|
103
103
|
|
|
104
104
|
/**
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
-- What each model's tokenizer measured on a project's agent runs, one row per run: the bytes of a
|
|
2
|
+
-- request's text as the Agent's context estimator weighs them, against the text tokens the
|
|
3
|
+
-- provider counted. A record deletes the model's rows past its newest 20, so the table stays small.
|
|
4
|
+
CREATE TABLE agent_token_rates (
|
|
5
|
+
project_id TEXT NOT NULL CHECK (length(trim(project_id)) > 0),
|
|
6
|
+
run_id TEXT NOT NULL CHECK (length(trim(run_id)) > 0),
|
|
7
|
+
provider_id TEXT NOT NULL CHECK (length(trim(provider_id)) > 0),
|
|
8
|
+
model_id TEXT NOT NULL CHECK (length(trim(model_id)) > 0),
|
|
9
|
+
bytes INTEGER NOT NULL CHECK (bytes > 0),
|
|
10
|
+
tokens INTEGER NOT NULL CHECK (tokens > 0),
|
|
11
|
+
measured_at TIMESTAMPTZ NOT NULL,
|
|
12
|
+
PRIMARY KEY (project_id, run_id)
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
CREATE INDEX idx_agent_token_rates_model
|
|
16
|
+
ON agent_token_rates (project_id, provider_id, model_id, measured_at DESC, run_id DESC);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
-- Where a computer's thread runs: the computer's name and the folder there, beside its id. A
|
|
2
|
+
-- computer kept before folders were has none until its owner moves the thread.
|
|
3
|
+
ALTER TABLE agent_threads ADD COLUMN computer_name TEXT;
|
|
4
|
+
ALTER TABLE agent_threads ADD COLUMN computer_folder TEXT;
|
|
5
|
+
UPDATE agent_threads SET computer_id = NULL WHERE computer_id IS NOT NULL;
|
|
6
|
+
-- When its turns last moved to where they run now: the files earlier turns used are not there.
|
|
7
|
+
ALTER TABLE agent_threads ADD COLUMN moved_at TIMESTAMPTZ;
|
|
8
|
+
|
|
9
|
+
-- Each open sixb on its owner's computers, a lease it renews: while one is live, the threads that
|
|
10
|
+
-- run on its computer in its folder are reachable there.
|
|
11
|
+
CREATE TABLE agent_computer_sessions (
|
|
12
|
+
project_id TEXT NOT NULL,
|
|
13
|
+
owner_principal_type TEXT NOT NULL CHECK (owner_principal_type IN ('user', 'serviceAccount', 'system')),
|
|
14
|
+
owner_principal_id TEXT NOT NULL CHECK (length(owner_principal_id) > 0),
|
|
15
|
+
id TEXT NOT NULL CHECK (length(id) > 0),
|
|
16
|
+
computer_id TEXT NOT NULL,
|
|
17
|
+
computer_name TEXT NOT NULL,
|
|
18
|
+
folder TEXT NOT NULL,
|
|
19
|
+
expires_at TIMESTAMPTZ NOT NULL,
|
|
20
|
+
PRIMARY KEY (project_id, owner_principal_type, owner_principal_id, id)
|
|
21
|
+
);
|
|
22
|
+
CREATE INDEX agent_computer_sessions_by_expiry ON agent_computer_sessions (project_id, expires_at);
|
|
23
|
+
|
|
24
|
+
-- Each open sixb asks every 20 seconds which of its owner's threads here have a turn active.
|
|
25
|
+
CREATE INDEX agent_threads_active_on_computer
|
|
26
|
+
ON agent_threads (project_id, owner_principal_type, owner_principal_id, computer_id, computer_folder)
|
|
27
|
+
WHERE active_run_id IS NOT NULL;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
-- Partition objects by object type, so a query plans against its own type's indexes alone instead
|
|
2
|
+
-- of every index of every type. Query indexes and extended statistics go with the old table:
|
|
3
|
+
-- preparation builds them again on the new partitions, right after this migration.
|
|
4
|
+
DO $$
|
|
5
|
+
DECLARE
|
|
6
|
+
item record;
|
|
7
|
+
columns_list text;
|
|
8
|
+
restore_constraints text[] := '{}';
|
|
9
|
+
restore_indexes text[] := '{}';
|
|
10
|
+
statement text;
|
|
11
|
+
BEGIN
|
|
12
|
+
-- Copying the rows takes as long as it takes, whatever timeout this role's queries run under.
|
|
13
|
+
-- Waiting for the lock does not: while this waits, every other query waits behind it, so it gives
|
|
14
|
+
-- way to a long query, and the migration fails cleanly if that query outlasts every attempt.
|
|
15
|
+
PERFORM set_config('statement_timeout', '0', true);
|
|
16
|
+
PERFORM set_config('lock_timeout', '3s', true);
|
|
17
|
+
FOR attempt IN 1..5 LOOP
|
|
18
|
+
BEGIN
|
|
19
|
+
LOCK TABLE objects IN ACCESS EXCLUSIVE MODE;
|
|
20
|
+
EXIT;
|
|
21
|
+
EXCEPTION WHEN lock_not_available THEN
|
|
22
|
+
IF attempt = 5 THEN RAISE; END IF;
|
|
23
|
+
PERFORM pg_sleep(attempt * 0.5);
|
|
24
|
+
END;
|
|
25
|
+
END LOOP;
|
|
26
|
+
|
|
27
|
+
-- objects is held now. Tables that reference it may have readers, so wait for those a little
|
|
28
|
+
-- longer, and fail cleanly rather than hold every query behind objects for good.
|
|
29
|
+
PERFORM set_config('lock_timeout', '30s', true);
|
|
30
|
+
|
|
31
|
+
FOR item IN
|
|
32
|
+
SELECT conrelid::regclass::text AS table_name, conname, pg_get_constraintdef(oid) AS definition
|
|
33
|
+
FROM pg_constraint
|
|
34
|
+
WHERE confrelid = 'objects'::regclass AND contype = 'f' AND conparentid = 0
|
|
35
|
+
LOOP
|
|
36
|
+
EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %I', item.table_name, item.conname);
|
|
37
|
+
restore_constraints := restore_constraints || format(
|
|
38
|
+
'ALTER TABLE %s ADD CONSTRAINT %I %s', item.table_name, item.conname, item.definition
|
|
39
|
+
);
|
|
40
|
+
END LOOP;
|
|
41
|
+
|
|
42
|
+
SELECT string_agg(quote_ident(attname), ', ' ORDER BY attnum) INTO columns_list
|
|
43
|
+
FROM pg_attribute
|
|
44
|
+
WHERE attrelid = 'objects'::regclass AND attnum > 0 AND NOT attisdropped AND attgenerated = '';
|
|
45
|
+
|
|
46
|
+
-- The table's general indexes stay on the parent under their names; prepared ones do not.
|
|
47
|
+
FOR item IN
|
|
48
|
+
SELECT c.relname AS name, pg_get_indexdef(c.oid) AS definition
|
|
49
|
+
FROM pg_index i
|
|
50
|
+
JOIN pg_class c ON c.oid = i.indexrelid
|
|
51
|
+
WHERE i.indrelid = 'objects'::regclass
|
|
52
|
+
AND NOT i.indisprimary
|
|
53
|
+
AND c.relname NOT LIKE 'sixb\_query\_%'
|
|
54
|
+
AND c.relname NOT LIKE 'sixb\_text\_count\_%'
|
|
55
|
+
LOOP
|
|
56
|
+
restore_indexes := restore_indexes || item.definition;
|
|
57
|
+
EXECUTE format('DROP INDEX %I', item.name);
|
|
58
|
+
END LOOP;
|
|
59
|
+
|
|
60
|
+
ALTER TABLE objects RENAME TO objects_unpartitioned;
|
|
61
|
+
ALTER INDEX objects_pkey RENAME TO objects_unpartitioned_pkey;
|
|
62
|
+
|
|
63
|
+
CREATE TABLE objects (
|
|
64
|
+
LIKE objects_unpartitioned
|
|
65
|
+
INCLUDING DEFAULTS INCLUDING GENERATED INCLUDING CONSTRAINTS INCLUDING STORAGE INCLUDING COMMENTS,
|
|
66
|
+
PRIMARY KEY (project_id, object_type_id, primary_id)
|
|
67
|
+
) PARTITION BY LIST (object_type_id);
|
|
68
|
+
|
|
69
|
+
-- Holds a type until its first preparation gives it a partition.
|
|
70
|
+
CREATE TABLE objects_default PARTITION OF objects DEFAULT;
|
|
71
|
+
|
|
72
|
+
FOR item IN SELECT DISTINCT object_type_id AS id FROM objects_unpartitioned LOOP
|
|
73
|
+
EXECUTE format(
|
|
74
|
+
'CREATE TABLE %I PARTITION OF objects FOR VALUES IN (%L)',
|
|
75
|
+
'objects_' || left(md5(item.id), 24),
|
|
76
|
+
item.id
|
|
77
|
+
);
|
|
78
|
+
END LOOP;
|
|
79
|
+
|
|
80
|
+
EXECUTE format(
|
|
81
|
+
'INSERT INTO objects (%s) SELECT %s FROM objects_unpartitioned', columns_list, columns_list
|
|
82
|
+
);
|
|
83
|
+
DROP TABLE objects_unpartitioned;
|
|
84
|
+
|
|
85
|
+
FOREACH statement IN ARRAY restore_indexes LOOP
|
|
86
|
+
EXECUTE statement;
|
|
87
|
+
END LOOP;
|
|
88
|
+
FOREACH statement IN ARRAY restore_constraints LOOP
|
|
89
|
+
EXECUTE statement;
|
|
90
|
+
END LOOP;
|
|
91
|
+
END $$;
|
|
92
|
+
|
|
93
|
+
ANALYZE objects;
|
package/src/migrations.ts
CHANGED
|
@@ -454,6 +454,9 @@ export const postgresStorageMigrations = defineMigrations<PostgresMigrationConte
|
|
|
454
454
|
pgSql("081-subagent-questions", subagentQuestionsSql),
|
|
455
455
|
pgSql("082-agent-run-cancel-requests", agentRunCancelRequestsSql),
|
|
456
456
|
pgSql("083-agent-thread-runs-on", agentThreadRunsOnSql),
|
|
457
|
+
pgSql("084-agent-token-rates", agentTokenRatesSql),
|
|
458
|
+
pgSql("085-agent-computer-sessions", agentComputerSessionsSql),
|
|
459
|
+
pgSql("086-partition-objects", partitionObjectsSql),
|
|
457
460
|
],
|
|
458
461
|
})
|
|
459
462
|
|
|
@@ -521,3 +524,8 @@ import agentRunCancelRequestsSql from "./migrations/082-agent-run-cancel-request
|
|
|
521
524
|
type: "text",
|
|
522
525
|
}
|
|
523
526
|
import agentThreadRunsOnSql from "./migrations/083-agent-thread-runs-on.sql" with { type: "text" }
|
|
527
|
+
import agentTokenRatesSql from "./migrations/084-agent-token-rates.sql" with { type: "text" }
|
|
528
|
+
import agentComputerSessionsSql from "./migrations/085-agent-computer-sessions.sql" with {
|
|
529
|
+
type: "text",
|
|
530
|
+
}
|
|
531
|
+
import partitionObjectsSql from "./migrations/086-partition-objects.sql" with { type: "text" }
|