@sixb/pg 0.1.2 → 0.1.4

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 (40) hide show
  1. package/dist/agents/agent-storage.d.ts +5 -3
  2. package/dist/agents/agent-storage.d.ts.map +1 -1
  3. package/dist/agents/checkpoints.d.ts +16 -0
  4. package/dist/agents/checkpoints.d.ts.map +1 -0
  5. package/dist/agents/index.d.ts +1 -0
  6. package/dist/agents/index.d.ts.map +1 -1
  7. package/dist/agents/messages.d.ts +2 -1
  8. package/dist/agents/messages.d.ts.map +1 -1
  9. package/dist/agents/rows.d.ts +18 -1
  10. package/dist/agents/rows.d.ts.map +1 -1
  11. package/dist/index.d.ts +3 -2
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +1028 -35
  14. package/dist/index.js.map +15 -13
  15. package/dist/migrations.d.ts.map +1 -1
  16. package/dist/ontology-storage/materialization-state.d.ts.map +1 -1
  17. package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
  18. package/dist/pg-ai-cost-storage.d.ts +12 -0
  19. package/dist/pg-ai-cost-storage.d.ts.map +1 -0
  20. package/dist/pg-ai-usage-storage.d.ts +2 -1
  21. package/dist/pg-ai-usage-storage.d.ts.map +1 -1
  22. package/dist/pg-object-storage.d.ts +4 -3
  23. package/dist/pg-object-storage.d.ts.map +1 -1
  24. package/package.json +3 -3
  25. package/src/agents/agent-storage.ts +6 -3
  26. package/src/agents/checkpoints.ts +204 -0
  27. package/src/agents/index.ts +1 -0
  28. package/src/agents/messages.ts +71 -13
  29. package/src/agents/rows.ts +45 -0
  30. package/src/index.ts +7 -1
  31. package/src/migrations/026-ai-cost-accounting.sql +53 -0
  32. package/src/migrations/027-agent-context-checkpoints.sql +34 -0
  33. package/src/migrations/028-object-override-edit-times.sql +3 -0
  34. package/src/migrations.ts +10 -0
  35. package/src/ontology-storage/materialization-state.ts +2 -0
  36. package/src/ontology-storage/materialization-writer.ts +5 -3
  37. package/src/pg-ai-cost-storage.ts +707 -0
  38. package/src/pg-ai-usage-storage.ts +15 -0
  39. package/src/pg-object-query-compiler.ts +81 -1
  40. package/src/pg-object-storage.ts +109 -13
@@ -0,0 +1,53 @@
1
+ CREATE TABLE ai_model_call_valuations (
2
+ project_id TEXT NOT NULL CHECK (length(btrim(project_id)) > 0),
3
+ usage_record_id TEXT NOT NULL CHECK (length(btrim(usage_record_id)) > 0),
4
+ status TEXT NOT NULL CHECK (status IN ('rated', 'unpriceable')),
5
+ provider_id TEXT CHECK (provider_id IS NULL OR length(btrim(provider_id)) > 0),
6
+ model_id TEXT CHECK (model_id IS NULL OR length(btrim(model_id)) > 0),
7
+ currency TEXT CHECK (currency IS NULL OR currency ~ '^[A-Z]{3}$'),
8
+ amount_nanos BIGINT CHECK (amount_nanos IS NULL OR amount_nanos >= 0),
9
+ reason TEXT CHECK (
10
+ reason IS NULL OR reason IN (
11
+ 'missingBillingIdentity',
12
+ 'missingCatalogEntry',
13
+ 'missingUsageMeter',
14
+ 'unsupportedPricingDimension',
15
+ 'invalidUsageForFormula'
16
+ )
17
+ ),
18
+ details JSONB NOT NULL CHECK (jsonb_typeof(details) = 'object'),
19
+ rated_at TIMESTAMPTZ NOT NULL,
20
+ PRIMARY KEY (project_id, usage_record_id),
21
+ FOREIGN KEY (project_id, usage_record_id)
22
+ REFERENCES ai_model_call_usage(project_id, id)
23
+ ON DELETE RESTRICT,
24
+ CHECK (
25
+ (
26
+ status = 'rated'
27
+ AND provider_id IS NOT NULL
28
+ AND model_id IS NOT NULL
29
+ AND currency IS NOT NULL
30
+ AND amount_nanos IS NOT NULL
31
+ AND reason IS NULL
32
+ )
33
+ OR
34
+ (
35
+ status = 'unpriceable'
36
+ AND currency IS NULL
37
+ AND amount_nanos IS NULL
38
+ AND reason IS NOT NULL
39
+ AND (
40
+ (reason = 'missingBillingIdentity' AND provider_id IS NULL AND model_id IS NULL)
41
+ OR
42
+ (reason <> 'missingBillingIdentity' AND provider_id IS NOT NULL AND model_id IS NOT NULL)
43
+ )
44
+ )
45
+ )
46
+ );
47
+
48
+ CREATE INDEX idx_ai_model_call_usage_provider_model_time
49
+ ON ai_model_call_usage (project_id, provider_id, requested_model_id, occurred_at, id);
50
+ CREATE INDEX idx_ai_model_call_usage_model_time
51
+ ON ai_model_call_usage (project_id, requested_model_id, occurred_at, id);
52
+ CREATE INDEX idx_ai_model_call_valuations_summary
53
+ ON ai_model_call_valuations (project_id, status, currency, usage_record_id);
@@ -0,0 +1,34 @@
1
+ CREATE TABLE agent_context_checkpoints (
2
+ project_id TEXT NOT NULL,
3
+ id TEXT NOT NULL,
4
+ thread_id TEXT NOT NULL,
5
+ created_by_run_id TEXT NOT NULL,
6
+ previous_checkpoint_id TEXT,
7
+ reason TEXT NOT NULL CHECK (reason IN ('threshold', 'overflow')),
8
+ summary TEXT NOT NULL CHECK (length(trim(summary)) > 0),
9
+ summary_format_version INTEGER NOT NULL CHECK (summary_format_version = 1),
10
+ summarized_through_seq INTEGER NOT NULL CHECK (summarized_through_seq >= 1),
11
+ observed_head_seq INTEGER NOT NULL,
12
+ estimated_input_tokens_before BIGINT NOT NULL CHECK (estimated_input_tokens_before >= 0),
13
+ estimated_input_tokens_after BIGINT NOT NULL CHECK (estimated_input_tokens_after >= 0),
14
+ summary_model_id TEXT NOT NULL CHECK (length(trim(summary_model_id)) > 0),
15
+ created_at TIMESTAMPTZ NOT NULL,
16
+ PRIMARY KEY (project_id, id),
17
+ UNIQUE (project_id, created_by_run_id),
18
+ FOREIGN KEY (project_id, thread_id)
19
+ REFERENCES agent_threads(project_id, id) ON DELETE CASCADE,
20
+ FOREIGN KEY (project_id, created_by_run_id)
21
+ REFERENCES agent_runs(project_id, id) ON DELETE RESTRICT,
22
+ FOREIGN KEY (project_id, previous_checkpoint_id)
23
+ REFERENCES agent_context_checkpoints(project_id, id) ON DELETE RESTRICT,
24
+ CHECK (summarized_through_seq < observed_head_seq)
25
+ );
26
+
27
+ CREATE INDEX idx_agent_context_checkpoints_thread_latest
28
+ ON agent_context_checkpoints (
29
+ project_id,
30
+ thread_id,
31
+ summarized_through_seq DESC,
32
+ created_at DESC,
33
+ id DESC
34
+ );
@@ -0,0 +1,3 @@
1
+ ALTER TABLE ontology_object_overrides
2
+ ADD COLUMN edited_at JSONB NOT NULL DEFAULT '{}'::jsonb
3
+ CHECK (jsonb_typeof(edited_at) = 'object');
package/src/migrations.ts CHANGED
@@ -66,6 +66,13 @@ import ontologyCommitExecutionsSql from "./migrations/024-ontology-commit-execut
66
66
  import connectorConnectionsSql from "./migrations/025-connector-connections.sql" with {
67
67
  type: "text",
68
68
  }
69
+ import aiCostAccountingSql from "./migrations/026-ai-cost-accounting.sql" with { type: "text" }
70
+ import agentContextCheckpointsSql from "./migrations/027-agent-context-checkpoints.sql" with {
71
+ type: "text",
72
+ }
73
+ import objectOverrideEditTimesSql from "./migrations/028-object-override-edit-times.sql" with {
74
+ type: "text",
75
+ }
69
76
  import type { SQL, SQLClient } from "./pg-client"
70
77
 
71
78
  export interface PostgresMigrationContext {
@@ -344,6 +351,9 @@ export const postgresStorageMigrations = defineMigrations<PostgresMigrationConte
344
351
  pgSql("023-webhook-executions", webhookExecutionsSql),
345
352
  pgSql("024-ontology-commit-executions", ontologyCommitExecutionsSql),
346
353
  pgSql("025-connector-connections", connectorConnectionsSql),
354
+ pgSql("026-ai-cost-accounting", aiCostAccountingSql),
355
+ pgSql("027-agent-context-checkpoints", agentContextCheckpointsSql),
356
+ pgSql("028-object-override-edit-times", objectOverrideEditTimesSql),
347
357
  ],
348
358
  })
349
359
 
@@ -98,6 +98,7 @@ interface LinkScopeRow extends EffectiveLinkRow {
98
98
  interface ObjectOverrideRow extends PgStoredOverrideRow {
99
99
  readonly object_type_id: string
100
100
  readonly primary_id: string
101
+ readonly edited_at: unknown
101
102
  }
102
103
 
103
104
  interface LinkOverrideRow extends PgStoredOverrideRow {
@@ -1060,6 +1061,7 @@ function storedObjectOverride(row: ObjectOverrideRow): StoredObjectOverride {
1060
1061
  return {
1061
1062
  ref,
1062
1063
  value: structuredClone(row.value) as StoredObjectOverride["value"],
1064
+ editedAt: structuredClone(row.edited_at) as Readonly<Record<string, string>>,
1063
1065
  lastCommitId: row.last_commit_id,
1064
1066
  updatedAt: toIsoString(row.updated_at),
1065
1067
  }
@@ -25,6 +25,7 @@ export class PgMaterializationWriter {
25
25
  objectTypeId: item.ref.objectTypeId,
26
26
  primaryId: item.ref.primaryId,
27
27
  value: item.value,
28
+ editedAt: item.editedAt,
28
29
  lastCommitId: item.lastCommitId,
29
30
  updatedAt: item.updatedAt,
30
31
  expectedLastCommitId: item.expectedLastCommitId,
@@ -36,10 +37,10 @@ export class PgMaterializationWriter {
36
37
  SELECT value FROM jsonb_array_elements(${jsonParameter(this.sql, objectInserts)}::jsonb)
37
38
  )
38
39
  INSERT INTO ontology_object_overrides (
39
- project_id, object_type_id, primary_id, value, last_commit_id, updated_at
40
+ project_id, object_type_id, primary_id, value, edited_at, last_commit_id, updated_at
40
41
  )
41
42
  SELECT ${projectId}, value->>'objectTypeId', value->>'primaryId', value->'value',
42
- value->>'lastCommitId',
43
+ value->'editedAt', value->>'lastCommitId',
43
44
  (value->>'updatedAt')::timestamptz
44
45
  FROM staged
45
46
  ON CONFLICT DO NOTHING
@@ -55,7 +56,8 @@ export class PgMaterializationWriter {
55
56
  SELECT value FROM jsonb_array_elements(${jsonParameter(this.sql, objectUpdates)}::jsonb)
56
57
  )
57
58
  UPDATE ontology_object_overrides AS overrides
58
- SET value = staged.value->'value', last_commit_id = staged.value->>'lastCommitId',
59
+ SET value = staged.value->'value', edited_at = staged.value->'editedAt',
60
+ last_commit_id = staged.value->>'lastCommitId',
59
61
  updated_at = (staged.value->>'updatedAt')::timestamptz
60
62
  FROM staged
61
63
  WHERE overrides.project_id = ${projectId}