@sixb/pg 0.1.5 → 0.1.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 (51) hide show
  1. package/dist/agents/rows.d.ts +8 -5
  2. package/dist/agents/rows.d.ts.map +1 -1
  3. package/dist/agents/runs.d.ts +3 -2
  4. package/dist/agents/runs.d.ts.map +1 -1
  5. package/dist/agents/threads.d.ts.map +1 -1
  6. package/dist/ai-cost-groups.d.ts +5 -0
  7. package/dist/ai-cost-groups.d.ts.map +1 -0
  8. package/dist/auth-storage/auth-storage.d.ts +4 -1
  9. package/dist/auth-storage/auth-storage.d.ts.map +1 -1
  10. package/dist/auth-storage/device-authorizations.d.ts +29 -0
  11. package/dist/auth-storage/device-authorizations.d.ts.map +1 -0
  12. package/dist/auth-storage/rows.d.ts +19 -1
  13. package/dist/auth-storage/rows.d.ts.map +1 -1
  14. package/dist/auth-storage/shared.d.ts +1 -1
  15. package/dist/auth-storage/shared.d.ts.map +1 -1
  16. package/dist/index.d.ts +2 -1
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +1804 -464
  19. package/dist/index.js.map +19 -16
  20. package/dist/migrations.d.ts.map +1 -1
  21. package/dist/ontology-storage/materialization-state.d.ts +7 -3
  22. package/dist/ontology-storage/materialization-state.d.ts.map +1 -1
  23. package/dist/pg-ai-cost-storage.d.ts +2 -1
  24. package/dist/pg-ai-cost-storage.d.ts.map +1 -1
  25. package/dist/pg-ai-limit-storage.d.ts +34 -0
  26. package/dist/pg-ai-limit-storage.d.ts.map +1 -0
  27. package/dist/pg-ai-usage-storage.d.ts +28 -0
  28. package/dist/pg-ai-usage-storage.d.ts.map +1 -1
  29. package/package.json +4 -4
  30. package/src/agents/rows.ts +38 -9
  31. package/src/agents/runs.ts +148 -11
  32. package/src/agents/threads.ts +1 -17
  33. package/src/ai-cost-groups.ts +102 -0
  34. package/src/auth-storage/auth-storage.ts +84 -0
  35. package/src/auth-storage/device-authorizations.ts +131 -0
  36. package/src/auth-storage/rows.ts +41 -0
  37. package/src/auth-storage/shared.ts +1 -0
  38. package/src/index.ts +8 -1
  39. package/src/migrations/029-model-accounting.sql +24 -0
  40. package/src/migrations/030-ai-usage-limits.sql +94 -0
  41. package/src/migrations/031-subagent-runs.sql +97 -0
  42. package/src/migrations/032-conversation-run-spec.sql +23 -0
  43. package/src/migrations/033-retire-agent-definitions.sql +28 -0
  44. package/src/migrations/034-device-authorizations.sql +24 -0
  45. package/src/migrations.ts +18 -0
  46. package/src/ontology-storage/materialization-state.ts +45 -29
  47. package/src/ontology-storage/materializations.ts +3 -3
  48. package/src/pg-ai-cost-storage.ts +71 -24
  49. package/src/pg-ai-limit-storage.ts +901 -0
  50. package/src/pg-ai-usage-storage.ts +34 -0
  51. package/src/pg-workflow-run-storage.ts +8 -8
@@ -1,3 +1,4 @@
1
+ import { isModelReasoning, normalizeModelProviderIds } from "@sixb/core/models"
1
2
  import type { ReadonlyJsonObject } from "@sixb/core/storage"
2
3
  import {
3
4
  type AiModelCallUsageInput,
@@ -36,7 +37,9 @@ export class PgAiUsageStorage implements AiUsageStorage {
36
37
  attempt,
37
38
  call_id,
38
39
  provider_id,
40
+ provider_ids,
39
41
  requested_model_id,
42
+ requested_reasoning,
40
43
  response_model_id,
41
44
  response_id,
42
45
  input_tokens,
@@ -58,7 +61,13 @@ export class PgAiUsageStorage implements AiUsageStorage {
58
61
  ${record.attempt},
59
62
  ${record.callId},
60
63
  ${record.providerId},
64
+ ${record.providerIds === undefined ? null : JSON.stringify(record.providerIds)}::jsonb,
61
65
  ${record.requestedModelId},
66
+ ${
67
+ record.requestedReasoning === undefined
68
+ ? null
69
+ : JSON.stringify(record.requestedReasoning)
70
+ }::text::jsonb,
62
71
  ${record.responseModelId ?? null},
63
72
  ${record.responseId},
64
73
  ${record.usage.inputTokens ?? null},
@@ -181,7 +190,17 @@ export class PgAiUsageStorage implements AiUsageStorage {
181
190
  callId: row.call_id,
182
191
  requesterGroupIds: groupRows.map((group) => group.group_id),
183
192
  providerId: row.provider_id,
193
+ ...(row.provider_ids === null
194
+ ? {}
195
+ : {
196
+ providerIds: normalizeModelProviderIds(
197
+ typeof row.provider_ids === "string" ? JSON.parse(row.provider_ids) : row.provider_ids
198
+ ),
199
+ }),
184
200
  requestedModelId: row.requested_model_id,
201
+ ...(row.requested_reasoning === null
202
+ ? {}
203
+ : { requestedReasoning: requestedReasoningFromRow(row.requested_reasoning) }),
185
204
  ...(row.response_model_id === null ? {} : { responseModelId: row.response_model_id }),
186
205
  responseId: row.response_id,
187
206
  usage: {
@@ -237,7 +256,9 @@ interface AiUsageRow {
237
256
  readonly attempt: number | string
238
257
  readonly call_id: string
239
258
  readonly provider_id: string
259
+ readonly provider_ids: Record<string, unknown> | string | null
240
260
  readonly requested_model_id: string
261
+ readonly requested_reasoning: string | Record<string, unknown> | null
241
262
  readonly response_model_id: string | null
242
263
  readonly response_id: string
243
264
  readonly input_tokens: number | string | null
@@ -298,6 +319,19 @@ function rawUsageFromRow(value: Exclude<AiUsageRow["raw_usage"], null>): Readonl
298
319
  return typeof value === "string" ? (JSON.parse(value) as ReadonlyJsonObject) : value
299
320
  }
300
321
 
322
+ export function requestedReasoningFromRow(
323
+ value: Exclude<AiUsageRow["requested_reasoning"], null>
324
+ ): AiModelCallUsageRecord["requestedReasoning"] {
325
+ const parsed: unknown =
326
+ typeof value === "string" && (value.startsWith('"') || value.startsWith("{"))
327
+ ? JSON.parse(value)
328
+ : value
329
+ if (!isModelReasoning(parsed)) {
330
+ throw new Error("[SixbPg] Stored AI usage requested reasoning is invalid.")
331
+ }
332
+ return parsed
333
+ }
334
+
301
335
  function assertNonBlankProjectId(projectId: string): void {
302
336
  if (typeof projectId !== "string" || projectId.trim().length === 0) {
303
337
  throw new TypeError("[Sixb] AI usage projectId must be nonblank.")
@@ -407,16 +407,16 @@ export class PgWorkflowAgentNodeRunStorage implements WorkflowAgentNodeRunStorag
407
407
  projectId: input.projectId,
408
408
  executionId: input.executionId,
409
409
  nodeRunId: input.nodeRunId,
410
- agentId: input.agentId,
410
+ actorId: input.actorId,
411
411
  workflowExecutionId: parent.execution_id,
412
412
  })
413
413
 
414
414
  try {
415
415
  const [row] = await this.sql<WorkflowAgentNodeRunDatabaseRow[]>`
416
416
  INSERT INTO workflow_agent_node_runs (
417
- project_id, node_run_id, execution_id, agent_id, status, prompt, attempt, created_at
417
+ project_id, node_run_id, execution_id, actor_id, status, prompt, attempt, created_at
418
418
  ) VALUES (
419
- ${input.projectId}, ${input.nodeRunId}, ${input.executionId}, ${input.agentId},
419
+ ${input.projectId}, ${input.nodeRunId}, ${input.executionId}, ${input.actorId},
420
420
  ${"queued"}, ${input.prompt}, ${0}, ${input.createdAt ?? new Date()}
421
421
  ) RETURNING *
422
422
  `
@@ -554,9 +554,9 @@ export class PgWorkflowAgentNodeRunStorage implements WorkflowAgentNodeRunStorag
554
554
  const whereClauses = ["project_id = $1"]
555
555
  const params: SqlParameter[] = [input.projectId]
556
556
  let index = 2
557
- if (input.agentId) {
558
- whereClauses.push(`agent_id = $${index++}`)
559
- params.push(input.agentId)
557
+ if (input.actorId) {
558
+ whereClauses.push(`actor_id = $${index++}`)
559
+ params.push(input.actorId)
560
560
  }
561
561
  index = appendRunListFilters(whereClauses, params, index, input)
562
562
  const result = await queryRunList<WorkflowAgentNodeRunDatabaseRow>({
@@ -910,7 +910,7 @@ interface WorkflowAgentNodeRunDatabaseRow {
910
910
  project_id: string
911
911
  node_run_id: string
912
912
  execution_id: string
913
- agent_id: string
913
+ actor_id: string
914
914
  status: WorkflowAgentNodeRunRecord["status"]
915
915
  prompt: string
916
916
  model_id: string | null
@@ -968,7 +968,7 @@ function rowToWorkflowAgentNodeRunRecord(
968
968
  projectId: row.project_id,
969
969
  nodeRunId: row.node_run_id,
970
970
  executionId: row.execution_id,
971
- agentId: row.agent_id,
971
+ actorId: row.actor_id,
972
972
  status: row.status,
973
973
  prompt: row.prompt,
974
974
  ...(row.model_id ? { modelId: row.model_id } : {}),