@sixb/sqlite 0.1.6 → 0.1.8

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 (91) 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/execution-storage.d.ts +3 -2
  25. package/dist/execution-storage.d.ts.map +1 -1
  26. package/dist/index.d.ts +4 -1
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +3443 -789
  29. package/dist/index.js.map +29 -20
  30. package/dist/migrations.d.ts.map +1 -1
  31. package/dist/objects/index.d.ts +3 -0
  32. package/dist/objects/index.d.ts.map +1 -0
  33. package/dist/{object-query-compiler.d.ts → objects/query-compiler.d.ts} +16 -1
  34. package/dist/objects/query-compiler.d.ts.map +1 -0
  35. package/dist/objects/read-scope.d.ts +17 -0
  36. package/dist/objects/read-scope.d.ts.map +1 -0
  37. package/dist/objects/reader.d.ts +51 -0
  38. package/dist/objects/reader.d.ts.map +1 -0
  39. package/dist/objects/rows.d.ts +33 -0
  40. package/dist/objects/rows.d.ts.map +1 -0
  41. package/dist/objects/selected-reader.d.ts +9 -0
  42. package/dist/objects/selected-reader.d.ts.map +1 -0
  43. package/dist/{object-storage.d.ts → objects/storage.d.ts} +12 -9
  44. package/dist/objects/storage.d.ts.map +1 -0
  45. package/dist/share-grant-storage.d.ts +20 -0
  46. package/dist/share-grant-storage.d.ts.map +1 -0
  47. package/dist/share-session-storage.d.ts +19 -0
  48. package/dist/share-session-storage.d.ts.map +1 -0
  49. package/dist/storage-errors.d.ts +1 -0
  50. package/dist/storage-errors.d.ts.map +1 -1
  51. package/dist/transactions.d.ts +8 -0
  52. package/dist/transactions.d.ts.map +1 -1
  53. package/package.json +3 -3
  54. package/src/agents/rows.ts +36 -9
  55. package/src/agents/runs.ts +152 -13
  56. package/src/agents/threads.ts +2 -17
  57. package/src/ai-cost-groups.ts +100 -0
  58. package/src/ai-cost-storage.ts +67 -25
  59. package/src/ai-limit-storage.ts +981 -0
  60. package/src/ai-usage-storage.ts +24 -1
  61. package/src/auth-storage/access-tokens.ts +77 -68
  62. package/src/auth-storage/auth-storage.ts +80 -1
  63. package/src/auth-storage/device-authorizations.ts +153 -0
  64. package/src/auth-storage/rows.ts +41 -0
  65. package/src/auth-storage/shared.ts +1 -0
  66. package/src/execution-storage.ts +22 -3
  67. package/src/index.ts +24 -3
  68. package/src/migrations/029-model-accounting.sql +79 -0
  69. package/src/migrations/030-ai-usage-limits.sql +100 -0
  70. package/src/migrations/031-subagent-runs.sql +231 -0
  71. package/src/migrations/032-conversation-run-spec.sql +78 -0
  72. package/src/migrations/033-retire-agent-definitions.sql +89 -0
  73. package/src/migrations/034-device-authorizations.sql +22 -0
  74. package/src/migrations/035-share-grants.sql +66 -0
  75. package/src/migrations/036-share-sessions.sql +226 -0
  76. package/src/migrations.ts +22 -0
  77. package/src/objects/index.ts +2 -0
  78. package/src/{object-query-compiler.ts → objects/query-compiler.ts} +104 -54
  79. package/src/objects/read-scope.ts +316 -0
  80. package/src/objects/reader.ts +526 -0
  81. package/src/objects/rows.ts +123 -0
  82. package/src/objects/selected-reader.ts +119 -0
  83. package/src/objects/storage.ts +296 -0
  84. package/src/share-grant-storage.ts +252 -0
  85. package/src/share-session-storage.ts +189 -0
  86. package/src/storage-errors.ts +4 -0
  87. package/src/transactions.ts +32 -0
  88. package/src/workflow-run-storage.ts +8 -8
  89. package/dist/object-query-compiler.d.ts.map +0 -1
  90. package/dist/object-storage.d.ts.map +0 -1
  91. package/src/object-storage.ts +0 -709
@@ -0,0 +1,316 @@
1
+ import type { CompiledSelectedObjectReadScope } from "@sixb/core/storage"
2
+ import {
3
+ type CompiledObjectQuery,
4
+ type SqliteObjectQuerySource,
5
+ type SqliteValue,
6
+ sqliteJsonEachValue,
7
+ } from "./query-compiler"
8
+
9
+ export interface SqliteSelectedObjectReadSource extends SqliteObjectQuerySource {
10
+ readonly objectPropertyPermissionsTable: string
11
+ readonly traversalProbe: {
12
+ readonly sql: string
13
+ readonly args: readonly SqliteValue[]
14
+ }
15
+ }
16
+
17
+ /**
18
+ * Prepare one immutable selected scope as live, path-sensitive SQLite relations.
19
+ *
20
+ * The complete compiled scope is serialized once. Every statement receives that one JSON document
21
+ * and the bound project id, keeping the host-parameter count constant as selections grow.
22
+ */
23
+ export function compileSqliteSelectedObjectReadSource(
24
+ projectId: string,
25
+ scope: CompiledSelectedObjectReadScope,
26
+ maxTraversalFacts: number
27
+ ): SqliteSelectedObjectReadSource {
28
+ const scopeJson = JSON.stringify({
29
+ roots: scope.roots,
30
+ objects: scope.objects,
31
+ steps: scope.steps,
32
+ })
33
+ const compiled = compileSelectedReadScopeCte(scopeJson, projectId)
34
+ const wrapStatement = (sql: string, args: readonly SqliteValue[] = []) => ({
35
+ sql: `${compiled.sql}\n${sql}`,
36
+ args: [...compiled.args, ...args],
37
+ })
38
+
39
+ return Object.freeze({
40
+ objectsTable: "_sixb_scope_objects",
41
+ linksTable: "_sixb_scope_links",
42
+ objectPropertyPermissionsTable: "_sixb_scope_object_permissions",
43
+ wrapStatement,
44
+ wrapQuery: (query: CompiledObjectQuery): CompiledObjectQuery => ({
45
+ ...query,
46
+ ...wrapStatement(query.sql, query.args),
47
+ totalSql: `${compiled.sql}\n${query.totalSql}`,
48
+ totalArgs: [...compiled.args, ...query.totalArgs],
49
+ ...(query.hasMoreProbe
50
+ ? {
51
+ hasMoreProbe: {
52
+ ...query.hasMoreProbe,
53
+ ...wrapStatement(query.hasMoreProbe.sql, query.hasMoreProbe.args),
54
+ },
55
+ }
56
+ : {}),
57
+ }),
58
+ traversalProbe: wrapStatement(
59
+ `
60
+ SELECT COUNT(*) AS total
61
+ FROM (
62
+ SELECT 1 AS traversal_fact
63
+ FROM _sixb_scope_root_facts
64
+
65
+ UNION ALL
66
+
67
+ SELECT 1 AS traversal_fact
68
+ FROM _sixb_scope_reached_links
69
+
70
+ LIMIT ?
71
+ ) AS bounded_traversal_facts
72
+ `,
73
+ [BigInt(maxTraversalFacts) + 1n]
74
+ ),
75
+ })
76
+ }
77
+
78
+ interface CompiledReadScopeCte {
79
+ readonly sql: string
80
+ readonly args: readonly SqliteValue[]
81
+ }
82
+
83
+ function compileSelectedReadScopeCte(scopeJson: string, projectId: string): CompiledReadScopeCte {
84
+ return {
85
+ sql: `
86
+ WITH RECURSIVE
87
+ _sixb_scope_document(scope) AS (
88
+ SELECT json(?)
89
+ ),
90
+ _sixb_scope_roots(root_id, node_id, object_type_id, primary_id) AS (
91
+ SELECT
92
+ CAST(root.key AS INTEGER),
93
+ CAST(json_extract(root.value, '$.nodeId') AS INTEGER),
94
+ CAST(json_extract(root.value, '$.objectTypeId') AS TEXT),
95
+ CAST(json_extract(root.value, '$.primaryId') AS TEXT)
96
+ FROM _sixb_scope_document AS document,
97
+ json_each(json_extract(document.scope, '$.roots')) AS root
98
+ ),
99
+ _sixb_scope_root_facts(project_id, root_id, node_id, object_type_id, primary_id) AS (
100
+ SELECT anchor.project_id, root.root_id, root.node_id, root.object_type_id, root.primary_id
101
+ FROM _sixb_scope_roots AS root
102
+ JOIN objects AS anchor
103
+ ON anchor.project_id = ?
104
+ AND anchor.object_type_id = root.object_type_id
105
+ AND anchor.primary_id = root.primary_id
106
+ ),
107
+ _sixb_scope_object_selections(node_id, object_type_id, property_ids) AS (
108
+ SELECT
109
+ CAST(json_extract(selection.value, '$.nodeId') AS INTEGER),
110
+ CAST(json_extract(selection.value, '$.objectTypeId') AS TEXT),
111
+ json_extract(selection.value, '$.propertyIds')
112
+ FROM _sixb_scope_document AS document,
113
+ json_each(json_extract(document.scope, '$.objects')) AS selection
114
+ ),
115
+ _sixb_scope_link_selections(
116
+ step_id,
117
+ node_id,
118
+ parent_node_id,
119
+ source_object_type_id,
120
+ link_id,
121
+ target_object_type_id,
122
+ property_ids
123
+ ) AS (
124
+ SELECT
125
+ CAST(step.key AS INTEGER),
126
+ CAST(json_extract(step.value, '$.nodeId') AS INTEGER),
127
+ CAST(json_extract(step.value, '$.parentNodeId') AS INTEGER),
128
+ CAST(json_extract(step.value, '$.sourceObjectTypeId') AS TEXT),
129
+ CAST(json_extract(step.value, '$.linkId') AS TEXT),
130
+ CAST(json_extract(step.value, '$.targetObjectTypeId') AS TEXT),
131
+ json_extract(step.value, '$.propertyIds')
132
+ FROM _sixb_scope_document AS document,
133
+ json_each(json_extract(document.scope, '$.steps')) AS step
134
+ ),
135
+ _sixb_scope_reachable(project_id, node_id, object_type_id, primary_id) AS (
136
+ SELECT root.project_id, root.node_id, root.object_type_id, root.primary_id
137
+ FROM _sixb_scope_root_facts AS root
138
+
139
+ UNION
140
+
141
+ SELECT edge.project_id, selection.node_id, edge.target_type_id, edge.target_id
142
+ FROM _sixb_scope_reachable AS parent
143
+ JOIN _sixb_scope_link_selections AS selection
144
+ ON selection.parent_node_id = parent.node_id
145
+ AND selection.source_object_type_id = parent.object_type_id
146
+ JOIN links AS edge
147
+ ON edge.project_id = parent.project_id
148
+ AND edge.source_type_id = parent.object_type_id
149
+ AND edge.source_id = parent.primary_id
150
+ AND edge.link_id = selection.link_id
151
+ AND edge.target_type_id = selection.target_object_type_id
152
+ JOIN objects AS target
153
+ ON target.project_id = edge.project_id
154
+ AND target.object_type_id = edge.target_type_id
155
+ AND target.primary_id = edge.target_id
156
+ ),
157
+ _sixb_scope_object_permissions(project_id, object_type_id, primary_id, property_id) AS (
158
+ SELECT DISTINCT
159
+ reachable.project_id,
160
+ reachable.object_type_id,
161
+ reachable.primary_id,
162
+ CAST(selected_property.value AS TEXT)
163
+ FROM _sixb_scope_reachable AS reachable
164
+ JOIN _sixb_scope_object_selections AS selection
165
+ ON selection.node_id = reachable.node_id
166
+ AND selection.object_type_id = reachable.object_type_id
167
+ JOIN json_each(selection.property_ids) AS selected_property
168
+ ),
169
+ _sixb_scope_object_ids(project_id, object_type_id, primary_id) AS (
170
+ SELECT DISTINCT project_id, object_type_id, primary_id
171
+ FROM _sixb_scope_reachable
172
+ ),
173
+ _sixb_scope_objects AS (
174
+ SELECT
175
+ stored.project_id,
176
+ stored.object_type_id,
177
+ stored.primary_id,
178
+ COALESCE(
179
+ (
180
+ SELECT json_group_object(property.key, ${sqliteJsonEachValue("property")})
181
+ FROM json_each(stored.properties) AS property
182
+ JOIN _sixb_scope_object_permissions AS permission
183
+ ON permission.project_id = stored.project_id
184
+ AND permission.object_type_id = stored.object_type_id
185
+ AND permission.primary_id = stored.primary_id
186
+ AND permission.property_id = property.key
187
+ ),
188
+ json('{}')
189
+ ) AS properties,
190
+ stored.created_at,
191
+ stored.updated_at,
192
+ stored.version,
193
+ stored.last_commit_id
194
+ FROM objects AS stored
195
+ JOIN _sixb_scope_object_ids AS visible
196
+ ON visible.project_id = stored.project_id
197
+ AND visible.object_type_id = stored.object_type_id
198
+ AND visible.primary_id = stored.primary_id
199
+ ),
200
+ _sixb_scope_reached_links(
201
+ project_id,
202
+ step_id,
203
+ node_id,
204
+ parent_node_id,
205
+ source_type_id,
206
+ source_id,
207
+ link_id,
208
+ target_type_id,
209
+ target_id,
210
+ property_ids
211
+ ) AS (
212
+ SELECT
213
+ edge.project_id,
214
+ selection.step_id,
215
+ selection.node_id,
216
+ selection.parent_node_id,
217
+ edge.source_type_id,
218
+ edge.source_id,
219
+ edge.link_id,
220
+ edge.target_type_id,
221
+ edge.target_id,
222
+ selection.property_ids
223
+ FROM _sixb_scope_reachable AS parent
224
+ JOIN _sixb_scope_link_selections AS selection
225
+ ON selection.parent_node_id = parent.node_id
226
+ AND selection.source_object_type_id = parent.object_type_id
227
+ JOIN links AS edge
228
+ ON edge.project_id = parent.project_id
229
+ AND edge.source_type_id = parent.object_type_id
230
+ AND edge.source_id = parent.primary_id
231
+ AND edge.link_id = selection.link_id
232
+ AND edge.target_type_id = selection.target_object_type_id
233
+ JOIN objects AS target
234
+ ON target.project_id = edge.project_id
235
+ AND target.object_type_id = edge.target_type_id
236
+ AND target.primary_id = edge.target_id
237
+ ),
238
+ _sixb_scope_link_permissions(
239
+ project_id,
240
+ source_type_id,
241
+ source_id,
242
+ link_id,
243
+ target_type_id,
244
+ target_id,
245
+ property_id
246
+ ) AS (
247
+ SELECT DISTINCT
248
+ reached.project_id,
249
+ reached.source_type_id,
250
+ reached.source_id,
251
+ reached.link_id,
252
+ reached.target_type_id,
253
+ reached.target_id,
254
+ CAST(selected_property.value AS TEXT)
255
+ FROM _sixb_scope_reached_links AS reached
256
+ JOIN json_each(reached.property_ids) AS selected_property
257
+ ),
258
+ _sixb_scope_link_ids(
259
+ project_id,
260
+ source_type_id,
261
+ source_id,
262
+ link_id,
263
+ target_type_id,
264
+ target_id
265
+ ) AS (
266
+ SELECT DISTINCT
267
+ project_id,
268
+ source_type_id,
269
+ source_id,
270
+ link_id,
271
+ target_type_id,
272
+ target_id
273
+ FROM _sixb_scope_reached_links
274
+ ),
275
+ _sixb_scope_links AS (
276
+ SELECT
277
+ stored.project_id,
278
+ stored.source_type_id,
279
+ stored.source_id,
280
+ stored.link_id,
281
+ stored.target_type_id,
282
+ stored.target_id,
283
+ CASE
284
+ WHEN stored.properties IS NULL THEN NULL
285
+ ELSE NULLIF(
286
+ (
287
+ SELECT json_group_object(property.key, ${sqliteJsonEachValue("property")})
288
+ FROM json_each(stored.properties) AS property
289
+ JOIN _sixb_scope_link_permissions AS permission
290
+ ON permission.project_id = stored.project_id
291
+ AND permission.source_type_id = stored.source_type_id
292
+ AND permission.source_id = stored.source_id
293
+ AND permission.link_id = stored.link_id
294
+ AND permission.target_type_id = stored.target_type_id
295
+ AND permission.target_id = stored.target_id
296
+ AND permission.property_id = property.key
297
+ ),
298
+ json('{}')
299
+ )
300
+ END AS properties,
301
+ stored.created_at,
302
+ stored.updated_at,
303
+ stored.last_commit_id
304
+ FROM links AS stored
305
+ JOIN _sixb_scope_link_ids AS visible
306
+ ON visible.project_id = stored.project_id
307
+ AND visible.source_type_id = stored.source_type_id
308
+ AND visible.source_id = stored.source_id
309
+ AND visible.link_id = stored.link_id
310
+ AND visible.target_type_id = stored.target_type_id
311
+ AND visible.target_id = stored.target_id
312
+ )
313
+ `,
314
+ args: [scopeJson, projectId],
315
+ }
316
+ }