@sixb/sqlite 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 (55) hide show
  1. package/dist/agents/rows.d.ts +7 -4
  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/ai-cost-storage.d.ts +2 -1
  9. package/dist/ai-cost-storage.d.ts.map +1 -1
  10. package/dist/ai-limit-storage.d.ts +35 -0
  11. package/dist/ai-limit-storage.d.ts.map +1 -0
  12. package/dist/ai-usage-storage.d.ts +1 -0
  13. package/dist/ai-usage-storage.d.ts.map +1 -1
  14. package/dist/auth-storage/access-tokens.d.ts +1 -1
  15. package/dist/auth-storage/access-tokens.d.ts.map +1 -1
  16. package/dist/auth-storage/auth-storage.d.ts +4 -1
  17. package/dist/auth-storage/auth-storage.d.ts.map +1 -1
  18. package/dist/auth-storage/device-authorizations.d.ts +29 -0
  19. package/dist/auth-storage/device-authorizations.d.ts.map +1 -0
  20. package/dist/auth-storage/rows.d.ts +19 -1
  21. package/dist/auth-storage/rows.d.ts.map +1 -1
  22. package/dist/auth-storage/shared.d.ts +1 -1
  23. package/dist/auth-storage/shared.d.ts.map +1 -1
  24. package/dist/index.d.ts +2 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +1937 -387
  27. package/dist/index.js.map +20 -17
  28. package/dist/migrations.d.ts.map +1 -1
  29. package/dist/ontology-storage/materialization-state.d.ts +7 -3
  30. package/dist/ontology-storage/materialization-state.d.ts.map +1 -1
  31. package/dist/ontology-storage/materializations.d.ts.map +1 -1
  32. package/package.json +3 -3
  33. package/src/agents/rows.ts +36 -9
  34. package/src/agents/runs.ts +152 -13
  35. package/src/agents/threads.ts +2 -17
  36. package/src/ai-cost-groups.ts +100 -0
  37. package/src/ai-cost-storage.ts +67 -25
  38. package/src/ai-limit-storage.ts +981 -0
  39. package/src/ai-usage-storage.ts +24 -1
  40. package/src/auth-storage/access-tokens.ts +77 -68
  41. package/src/auth-storage/auth-storage.ts +80 -1
  42. package/src/auth-storage/device-authorizations.ts +153 -0
  43. package/src/auth-storage/rows.ts +41 -0
  44. package/src/auth-storage/shared.ts +1 -0
  45. package/src/index.ts +6 -0
  46. package/src/migrations/029-model-accounting.sql +79 -0
  47. package/src/migrations/030-ai-usage-limits.sql +100 -0
  48. package/src/migrations/031-subagent-runs.sql +231 -0
  49. package/src/migrations/032-conversation-run-spec.sql +78 -0
  50. package/src/migrations/033-retire-agent-definitions.sql +89 -0
  51. package/src/migrations/034-device-authorizations.sql +22 -0
  52. package/src/migrations.ts +18 -0
  53. package/src/ontology-storage/materialization-state.ts +53 -33
  54. package/src/ontology-storage/materializations.ts +4 -5
  55. package/src/workflow-run-storage.ts +8 -8
@@ -22,6 +22,7 @@ import {
22
22
  startScopeAccumulator,
23
23
  } from "@sixb/core/internal/ontology-storage-provider"
24
24
  import type {
25
+ MaterializationLinkScopeRevision,
25
26
  MaterializationLinkScopeState,
26
27
  MaterializationLinkState,
27
28
  MaterializationObjectState,
@@ -83,6 +84,19 @@ interface LinkScopeRow extends EffectiveLinkRow {
83
84
  readonly scope_sort_key: string
84
85
  }
85
86
 
87
+ function linkScopeRequestJson(
88
+ scopes: readonly { readonly source: OntologyObjectRef; readonly linkId: string }[]
89
+ ): string {
90
+ return canonicalJson(
91
+ scopes.map(({ source, linkId }) => ({
92
+ scopeSortKey: linkScopeSortKey(source, linkId),
93
+ sourceTypeId: source.objectTypeId,
94
+ sourceId: source.primaryId,
95
+ linkId,
96
+ }))
97
+ )
98
+ }
99
+
86
100
  interface ObjectOverrideRow extends SqliteStoredOverrideRow {
87
101
  readonly object_type_id: string
88
102
  readonly primary_id: string
@@ -426,23 +440,18 @@ export class SqliteMaterializationStateReader {
426
440
  })
427
441
  }
428
442
 
429
- linkScopes(
443
+ linkScopeRevisions(
444
+ scopes: readonly { readonly source: OntologyObjectRef; readonly linkId: string }[]
445
+ ): readonly MaterializationLinkScopeRevision[] {
446
+ if (scopes.length === 0) return []
447
+ return this.readEffectiveLinkScopes(scopes, linkScopeRequestJson(scopes)).revisions
448
+ }
449
+
450
+ linkSlotStates(
430
451
  scopes: readonly { readonly source: OntologyObjectRef; readonly linkId: string }[]
431
452
  ): readonly MaterializationLinkScopeState[] {
432
453
  if (scopes.length === 0) return []
433
- const requested = scopes.map(({ source, linkId }) => ({
434
- scopeSortKey: linkScopeSortKey(source, linkId),
435
- sourceTypeId: source.objectTypeId,
436
- sourceId: source.primaryId,
437
- linkId,
438
- }))
439
- const accumulators = new Map(
440
- scopes.map(({ source, linkId }) => {
441
- const key = linkScopeSortKey(source, linkId)
442
- return [key, startScopeAccumulator(source, linkId)] as const
443
- })
444
- )
445
- const requestedJson = canonicalJson(requested)
454
+ const requestedJson = linkScopeRequestJson(scopes)
446
455
  const sourceRows = this.db
447
456
  .query(
448
457
  `WITH requested AS (
@@ -491,6 +500,31 @@ export class SqliteMaterializationStateReader {
491
500
  return [linkScopeSortKey(stored.ref.source, stored.ref.linkId), stored] as const
492
501
  })
493
502
  )
503
+ const { revisions, effective } = this.readEffectiveLinkScopes(scopes, requestedJson)
504
+ return scopes.map(({ source, linkId }, index) => {
505
+ const key = linkScopeSortKey(source, linkId)
506
+ return {
507
+ ...revisions[index]!,
508
+ sourceAssertion: sources.get(key) ?? null,
509
+ override: overrides.get(key) ?? null,
510
+ effective: effective.get(key) ?? null,
511
+ }
512
+ })
513
+ }
514
+
515
+ private readEffectiveLinkScopes(
516
+ scopes: readonly { readonly source: OntologyObjectRef; readonly linkId: string }[],
517
+ requestedJson: string
518
+ ): {
519
+ readonly revisions: readonly MaterializationLinkScopeRevision[]
520
+ readonly effective: ReadonlyMap<string, EffectiveLinkSnapshot | null>
521
+ } {
522
+ const accumulators = new Map(
523
+ scopes.map(({ source, linkId }) => {
524
+ const key = linkScopeSortKey(source, linkId)
525
+ return [key, startScopeAccumulator(source, linkId)] as const
526
+ })
527
+ )
494
528
  const effective = new Map<string, EffectiveLinkSnapshot | null>()
495
529
  const rows = this.db
496
530
  .query(
@@ -526,26 +560,12 @@ export class SqliteMaterializationStateReader {
526
560
  appendScopeSnapshot(accumulator, snapshot)
527
561
  effective.set(row.scope_sort_key, accumulator.effectiveCount === 1 ? snapshot : null)
528
562
  }
529
- return scopes.map(({ source, linkId }) => {
530
- const key = linkScopeSortKey(source, linkId)
531
- return {
532
- ...finishScopeAccumulator(accumulators.get(key)!),
533
- sourceAssertion: sources.get(key) ?? null,
534
- override: overrides.get(key) ?? null,
535
- effective: effective.get(key) ?? null,
536
- }
537
- })
538
- }
539
-
540
- linkScope(source: OntologyObjectRef, linkId: string): MaterializationLinkScopeState {
541
- const [scope] = this.linkScopes([{ source, linkId }])
542
- if (!scope) {
543
- throw new MaterializationConflictError(
544
- "effective-state",
545
- "Link scope lookup returned no row."
546
- )
563
+ return {
564
+ revisions: scopes.map(({ source, linkId }) =>
565
+ finishScopeAccumulator(accumulators.get(linkScopeSortKey(source, linkId))!)
566
+ ),
567
+ effective,
547
568
  }
548
- return scope
549
569
  }
550
570
 
551
571
  *incidentLinks(
@@ -88,10 +88,9 @@ export class SqliteOntologyMaterializationStorage implements OntologyMaterializa
88
88
  this.assertSource(expected, input.commit.projectId)
89
89
  for (const expected of input.expected.objects) this.assertObject(reader, expected)
90
90
  for (const expected of input.expected.links) this.assertLink(reader, expected)
91
- for (const expected of input.expected.linkScopes) {
92
- if (
93
- reader.linkScope(expected.source, expected.linkId).fingerprint !== expected.fingerprint
94
- ) {
91
+ const linkScopeRevisions = reader.linkScopeRevisions(input.expected.linkScopes)
92
+ for (const [index, expected] of input.expected.linkScopes.entries()) {
93
+ if (linkScopeRevisions[index]?.fingerprint !== expected.fingerprint) {
95
94
  throw new MaterializationConflictError(
96
95
  "effective-state",
97
96
  `Expected link scope changed for ${expected.source.objectTypeId}:${expected.source.primaryId}.${expected.linkId}.`
@@ -151,7 +150,7 @@ export class SqliteOntologyMaterializationStorage implements OntologyMaterializa
151
150
  yield {
152
151
  objects: [],
153
152
  links: [],
154
- linkScopes: reader.linkScopes(scopes.slice(offset, offset + input.pageRows)),
153
+ linkScopes: reader.linkSlotStates(scopes.slice(offset, offset + input.pageRows)),
155
154
  points: [],
156
155
  }
157
156
  }
@@ -491,7 +491,7 @@ export class SqliteWorkflowAgentNodeRunStorage implements WorkflowAgentNodeRunSt
491
491
  projectId: input.projectId,
492
492
  executionId: input.executionId,
493
493
  nodeRunId: input.nodeRunId,
494
- agentId: input.agentId,
494
+ actorId: input.actorId,
495
495
  workflowExecutionId: parent.execution_id,
496
496
  })
497
497
 
@@ -499,14 +499,14 @@ export class SqliteWorkflowAgentNodeRunStorage implements WorkflowAgentNodeRunSt
499
499
  this.db
500
500
  .query(`
501
501
  INSERT INTO workflow_agent_node_runs (
502
- project_id, node_run_id, execution_id, agent_id, status, prompt, attempt, created_at
502
+ project_id, node_run_id, execution_id, actor_id, status, prompt, attempt, created_at
503
503
  ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
504
504
  `)
505
505
  .run(
506
506
  input.projectId,
507
507
  input.nodeRunId,
508
508
  input.executionId,
509
- input.agentId,
509
+ input.actorId,
510
510
  "queued",
511
511
  input.prompt,
512
512
  0,
@@ -659,9 +659,9 @@ export class SqliteWorkflowAgentNodeRunStorage implements WorkflowAgentNodeRunSt
659
659
  if (hasEmptyStatuses(input)) return { runs: [], total: 0, hasMore: false }
660
660
  const whereClauses = ["project_id = ?"]
661
661
  const args: SqliteValue[] = [input.projectId]
662
- if (input.agentId) {
663
- whereClauses.push("agent_id = ?")
664
- args.push(input.agentId)
662
+ if (input.actorId) {
663
+ whereClauses.push("actor_id = ?")
664
+ args.push(input.actorId)
665
665
  }
666
666
  appendRunListFilters(whereClauses, args, input)
667
667
  const result = queryRunList<WorkflowAgentNodeRunDatabaseRow>({
@@ -1073,7 +1073,7 @@ interface WorkflowAgentNodeRunDatabaseRow {
1073
1073
  project_id: string
1074
1074
  node_run_id: string
1075
1075
  execution_id: string
1076
- agent_id: string
1076
+ actor_id: string
1077
1077
  status: WorkflowAgentNodeRunRecord["status"]
1078
1078
  prompt: string
1079
1079
  model_id: string | null
@@ -1107,7 +1107,7 @@ function rowToWorkflowAgentNodeRunRecord(
1107
1107
  projectId: row.project_id,
1108
1108
  nodeRunId: row.node_run_id,
1109
1109
  executionId: row.execution_id,
1110
- agentId: row.agent_id,
1110
+ actorId: row.actor_id,
1111
1111
  status: row.status,
1112
1112
  prompt: row.prompt,
1113
1113
  ...(row.model_id ? { modelId: row.model_id } : {}),