@coasys/ad4m 0.13.0-test-1 → 0.13.0-test-3

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 (115) hide show
  1. package/lib/index.cjs +2377 -849
  2. package/lib/index.js +2354 -846
  3. package/lib/src/Literal.js +6 -5
  4. package/lib/src/agent/AgentClient.d.ts +5 -1
  5. package/lib/src/agent/AgentClient.js +48 -2
  6. package/lib/src/expression/ExpressionRef.js +2 -2
  7. package/lib/src/index.d.ts +0 -1
  8. package/lib/src/index.js +0 -1
  9. package/lib/src/language/Language.d.ts +26 -176
  10. package/lib/src/language/Language.js +28 -2
  11. package/lib/src/model/Ad4mModel.d.ts +52 -28
  12. package/lib/src/model/Ad4mModel.js +255 -118
  13. package/lib/src/model/Ad4mModel.test.js +1727 -537
  14. package/lib/src/model/ModelQueryBuilder.d.ts +67 -34
  15. package/lib/src/model/ModelQueryBuilder.js +208 -79
  16. package/lib/src/model/Subject.js +6 -6
  17. package/lib/src/model/decorators.d.ts +14 -8
  18. package/lib/src/model/decorators.js +41 -14
  19. package/lib/src/model/hydration-batch.d.ts +34 -0
  20. package/lib/src/model/hydration-batch.js +147 -0
  21. package/lib/src/model/hydration-batching.test.d.ts +7 -0
  22. package/lib/src/model/hydration-batching.test.js +456 -0
  23. package/lib/src/model/hydration.d.ts +4 -3
  24. package/lib/src/model/hydration.js +365 -86
  25. package/lib/src/model/index.d.ts +4 -1
  26. package/lib/src/model/index.js +4 -1
  27. package/lib/src/model/json-schema.js +2 -2
  28. package/lib/src/model/query-cache.d.ts +30 -0
  29. package/lib/src/model/query-cache.js +66 -0
  30. package/lib/src/model/query-cache.test.d.ts +1 -0
  31. package/lib/src/model/query-cache.test.js +43 -0
  32. package/lib/src/model/query-common.d.ts +1 -1
  33. package/lib/src/model/query-common.js +1 -1
  34. package/lib/src/model/query-sparql-batch.d.ts +17 -0
  35. package/lib/src/model/query-sparql-batch.js +260 -0
  36. package/lib/src/model/query-sparql.d.ts +124 -0
  37. package/lib/src/model/query-sparql.js +767 -0
  38. package/lib/src/model/query-sparql.test.d.ts +1 -0
  39. package/lib/src/model/query-sparql.test.js +495 -0
  40. package/lib/src/model/{surreal-utils.d.ts → query-utils.d.ts} +12 -9
  41. package/lib/src/model/{surreal-utils.js → query-utils.js} +43 -27
  42. package/lib/src/model/relation-filtering.test.js +15 -15
  43. package/lib/src/model/sdna.js +1 -1
  44. package/lib/src/model/shacl-gen.js +42 -18
  45. package/lib/src/model/subscription-delta.test.d.ts +24 -0
  46. package/lib/src/model/subscription-delta.test.js +180 -0
  47. package/lib/src/model/subscription-pool.d.ts +39 -0
  48. package/lib/src/model/subscription-pool.js +165 -0
  49. package/lib/src/model/types.d.ts +52 -4
  50. package/lib/src/model/types.js +1 -1
  51. package/lib/src/perspectives/PerspectiveClient.d.ts +3 -17
  52. package/lib/src/perspectives/PerspectiveClient.js +42 -66
  53. package/lib/src/perspectives/PerspectiveClient.test.d.ts +1 -0
  54. package/lib/src/perspectives/PerspectiveClient.test.js +169 -0
  55. package/lib/src/perspectives/PerspectiveProxy.d.ts +54 -30
  56. package/lib/src/perspectives/PerspectiveProxy.js +239 -177
  57. package/lib/src/perspectives/PerspectiveResolver.d.ts +0 -1
  58. package/lib/src/perspectives/PerspectiveResolver.js +0 -11
  59. package/lib/src/perspectives/WakerSubscriptionManager.d.ts +3 -3
  60. package/lib/src/perspectives/WakerSubscriptionManager.js +7 -8
  61. package/lib/src/runtime/RuntimeResolver.d.ts +11 -0
  62. package/lib/src/runtime/RuntimeResolver.js +56 -0
  63. package/lib/src/schema.gql +11 -1
  64. package/lib/src/shacl/SHACLFlow.d.ts +1 -1
  65. package/lib/src/shacl/SHACLFlow.js +9 -9
  66. package/lib/src/shacl/SHACLShape.d.ts +1 -1
  67. package/lib/src/shacl/SHACLShape.js +38 -38
  68. package/lib/src/typeDefs.js +11 -1
  69. package/lib/src/utils.d.ts +6 -6
  70. package/lib/src/utils.js +6 -6
  71. package/package.json +1 -1
  72. package/src/Ad4mClient.test.ts +56 -1
  73. package/src/Literal.test.ts +10 -5
  74. package/src/Literal.ts +6 -5
  75. package/src/agent/AgentClient.ts +53 -1
  76. package/src/expression/ExpressionRef.ts +2 -2
  77. package/src/index.ts +0 -1
  78. package/src/language/Language.ts +55 -231
  79. package/src/model/Ad4mModel.test.ts +1709 -572
  80. package/src/model/Ad4mModel.ts +274 -120
  81. package/src/model/ModelQueryBuilder.ts +234 -87
  82. package/src/model/Subject.ts +6 -6
  83. package/src/model/decorators.ts +51 -22
  84. package/src/model/hydration-batch.ts +205 -0
  85. package/src/model/hydration-batching.test.ts +563 -0
  86. package/src/model/hydration.ts +349 -84
  87. package/src/model/index.ts +4 -1
  88. package/src/model/json-schema.ts +2 -2
  89. package/src/model/query-cache.test.ts +54 -0
  90. package/src/model/query-cache.ts +79 -0
  91. package/src/model/query-common.ts +1 -1
  92. package/src/model/query-sparql-batch.ts +301 -0
  93. package/src/model/query-sparql.test.ts +568 -0
  94. package/src/model/query-sparql.ts +820 -0
  95. package/src/model/{surreal-utils.ts → query-utils.ts} +44 -27
  96. package/src/model/relation-filtering.test.ts +15 -15
  97. package/src/model/sdna.ts +1 -1
  98. package/src/model/shacl-gen.ts +42 -17
  99. package/src/model/subscription-delta.test.ts +207 -0
  100. package/src/model/subscription-pool.ts +211 -0
  101. package/src/model/types.ts +53 -4
  102. package/src/perspectives/PerspectiveClient.test.ts +204 -0
  103. package/src/perspectives/PerspectiveClient.ts +41 -68
  104. package/src/perspectives/PerspectiveProxy.ts +244 -181
  105. package/src/perspectives/PerspectiveResolver.ts +0 -8
  106. package/src/perspectives/WakerSubscriptionManager.ts +8 -9
  107. package/src/runtime/RuntimeResolver.ts +43 -0
  108. package/src/shacl/SHACLFlow.ts +9 -9
  109. package/src/shacl/SHACLShape.ts +39 -39
  110. package/src/utils.ts +6 -6
  111. package/lib/src/model/query-surreal.d.ts +0 -44
  112. package/lib/src/model/query-surreal.js +0 -249
  113. package/src/language/LanguageContext.ts +0 -37
  114. package/src/model/query-surreal.ts +0 -280
  115. package/tsconfig-browser.json +0 -82
@@ -57,6 +57,7 @@ export class PerspectiveClient {
57
57
  #expressionClient?: ExpressionClient
58
58
  #neighbourhoodClient?: NeighbourhoodClient
59
59
  #aiClient?: AIClient
60
+ #proxyCache: Map<string, PerspectiveProxy> = new Map()
60
61
 
61
62
  constructor(client: ApolloClient<any>, subscribe: boolean = true) {
62
63
  this.#apolloClient = client
@@ -72,6 +73,18 @@ export class PerspectiveClient {
72
73
  }
73
74
  }
74
75
 
76
+ /** Get or create a proxy for a UUID. If the proxy exists in cache, update its handle and return it. */
77
+ private getOrCreateProxy(handle: PerspectiveHandle): PerspectiveProxy {
78
+ const existing = this.#proxyCache.get(handle.uuid)
79
+ if (existing) {
80
+ existing.updateHandle(handle)
81
+ return existing
82
+ }
83
+ const proxy = new PerspectiveProxy(handle, this)
84
+ this.#proxyCache.set(handle.uuid, proxy)
85
+ return proxy
86
+ }
87
+
75
88
  setExpressionClient(client: ExpressionClient) {
76
89
  this.#expressionClient = client
77
90
  }
@@ -96,10 +109,13 @@ export class PerspectiveClient {
96
109
  }
97
110
  }`
98
111
  }))
99
- return perspectives.map(handle => new PerspectiveProxy(handle, this))
112
+ return perspectives.map(handle => this.getOrCreateProxy(handle))
100
113
  }
101
114
 
102
115
  async byUUID(uuid: string): Promise<PerspectiveProxy|null> {
116
+ const cached = this.#proxyCache.get(uuid)
117
+ if (cached) return cached
118
+
103
119
  const { perspective } = unwrapApolloResult(await this.#apolloClient.query({
104
120
  query: gql`query perspective($uuid: String!) {
105
121
  perspective(uuid: $uuid) {
@@ -109,7 +125,7 @@ export class PerspectiveClient {
109
125
  variables: { uuid }
110
126
  }))
111
127
  if(!perspective) return null
112
- return new PerspectiveProxy(perspective, this)
128
+ return this.getOrCreateProxy(perspective)
113
129
  }
114
130
 
115
131
  async snapshotByUUID(uuid: string): Promise<Perspective|null> {
@@ -156,24 +172,15 @@ export class PerspectiveClient {
156
172
  return JSON.parse(perspectiveQueryProlog)
157
173
  }
158
174
 
159
- /**
160
- * Executes a read-only SurrealQL query against a perspective's link cache.
161
- *
162
- * Security: Only SELECT, RETURN, and other read-only queries are permitted.
163
- * Mutating operations (DELETE, UPDATE, INSERT, etc.) are blocked.
164
- *
165
- * Note: GraphQL field name is "perspectiveQuerySurrealDb" (lowercase "b" in "Db")
166
- * as generated from Rust method "perspective_query_surreal_db"
167
- */
168
- async querySurrealDB(uuid: string, query: string): Promise<any> {
169
- const { perspectiveQuerySurrealDb } = unwrapApolloResult(await this.#apolloClient.query({
170
- query: gql`query perspectiveQuerySurrealDb($uuid: String!, $query: String!) {
171
- perspectiveQuerySurrealDb(uuid: $uuid, query: $query)
175
+ async querySparql(uuid: string, query: string): Promise<any> {
176
+ const { perspectiveQuerySparql } = unwrapApolloResult(await this.#apolloClient.query({
177
+ query: gql`query perspectiveQuerySparql($uuid: String!, $query: String!) {
178
+ perspectiveQuerySparql(uuid: $uuid, query: $query)
172
179
  }`,
173
180
  variables: { uuid, query }
174
181
  }))
175
182
 
176
- return JSON.parse(perspectiveQuerySurrealDb)
183
+ return JSON.parse(perspectiveQuerySparql)
177
184
  }
178
185
 
179
186
  async subscribeQuery(uuid: string, query: string): Promise<{ subscriptionId: string, result: AllInstancesResult, isInit?: boolean }> {
@@ -201,53 +208,6 @@ export class PerspectiveClient {
201
208
  return { subscriptionId, result: finalResult, isInit }
202
209
  }
203
210
 
204
- async perspectiveSubscribeSurrealQuery(uuid: string, query: string): Promise<{ subscriptionId: string, result: AllInstancesResult, isInit?: boolean }> {
205
- const { perspectiveSubscribeSurrealQuery } = unwrapApolloResult(await this.#apolloClient.mutate({
206
- mutation: gql`mutation perspectiveSubscribeSurrealQuery($uuid: String!, $query: String!) {
207
- perspectiveSubscribeSurrealQuery(uuid: $uuid, query: $query) {
208
- subscriptionId
209
- result
210
- }
211
- }`,
212
- variables: { uuid, query }
213
- }))
214
- const { subscriptionId, result } = perspectiveSubscribeSurrealQuery
215
- let finalResult = result;
216
- let isInit = false;
217
- if(finalResult.startsWith("#init#")) {
218
- finalResult = finalResult.substring(6)
219
- isInit = true;
220
- }
221
- try {
222
- finalResult = JSON.parse(finalResult)
223
- } catch (e) {
224
- console.error('Error parsing perspectiveSubscribeSurrealQuery result:', e)
225
- }
226
- return { subscriptionId, result: finalResult, isInit }
227
- }
228
-
229
- async perspectiveKeepAliveSurrealQuery(uuid: string, subscriptionId: string): Promise<boolean> {
230
- const { perspectiveKeepAliveSurrealQuery } = unwrapApolloResult(await this.#apolloClient.mutate({
231
- mutation: gql`mutation perspectiveKeepAliveSurrealQuery($uuid: String!, $subscriptionId: String!) {
232
- perspectiveKeepAliveSurrealQuery(uuid: $uuid, subscriptionId: $subscriptionId)
233
- }`,
234
- variables: { uuid, subscriptionId }
235
- }))
236
-
237
- return perspectiveKeepAliveSurrealQuery
238
- }
239
-
240
- async perspectiveDisposeSurrealQuerySubscription(uuid: string, subscriptionId: string): Promise<boolean> {
241
- const { perspectiveDisposeSurrealQuerySubscription } = unwrapApolloResult(await this.#apolloClient.mutate({
242
- mutation: gql`mutation perspectiveDisposeSurrealQuerySubscription($uuid: String!, $subscriptionId: String!) {
243
- perspectiveDisposeSurrealQuerySubscription(uuid: $uuid, subscriptionId: $subscriptionId)
244
- }`,
245
- variables: { uuid, subscriptionId }
246
- }))
247
-
248
- return perspectiveDisposeSurrealQuerySubscription
249
- }
250
-
251
211
  subscribeToQueryUpdates(subscriptionId: string, onData: (result: AllInstancesResult) => void): () => void {
252
212
  const subscription = this.#apolloClient.subscribe({
253
213
  query: gql`
@@ -315,7 +275,7 @@ export class PerspectiveClient {
315
275
  }`,
316
276
  variables: { name }
317
277
  }))
318
- return new PerspectiveProxy(perspectiveAdd, this)
278
+ return this.getOrCreateProxy(perspectiveAdd)
319
279
  }
320
280
 
321
281
  async update(uuid: string, name: string): Promise<PerspectiveProxy> {
@@ -327,10 +287,13 @@ export class PerspectiveClient {
327
287
  }`,
328
288
  variables: { uuid, name }
329
289
  }))
330
- return new PerspectiveProxy(perspectiveUpdate, this)
290
+ // Evict stale proxy — getOrCreateProxy will create a fresh one with updated handle
291
+ this.#proxyCache.delete(uuid)
292
+ return this.getOrCreateProxy(perspectiveUpdate)
331
293
  }
332
294
 
333
295
  async remove(uuid: string): Promise<{perspectiveRemove: boolean}> {
296
+ this.#proxyCache.delete(uuid)
334
297
  return unwrapApolloResult(await this.#apolloClient.mutate({
335
298
  mutation: gql`mutation perspectiveRemove($uuid: String!) {
336
299
  perspectiveRemove(uuid: $uuid)
@@ -497,9 +460,12 @@ export class PerspectiveClient {
497
460
  }
498
461
  `}).subscribe({
499
462
  next: result => {
463
+ const handle = result.data.perspectiveAdded
500
464
  this.#perspectiveAddedCallbacks.forEach(cb => {
501
- cb(result.data.perspectiveAdded)
465
+ cb(handle)
502
466
  })
467
+ // Populate cache so subsequent byUUID() calls get a hit
468
+ this.getOrCreateProxy(handle)
503
469
  },
504
470
  error: (e) => console.error(e)
505
471
  })
@@ -516,9 +482,13 @@ export class PerspectiveClient {
516
482
  }
517
483
  `}).subscribe({
518
484
  next: result => {
485
+ const handle = result.data.perspectiveUpdated
519
486
  this.#perspectiveUpdatedCallbacks.forEach(cb => {
520
- cb(result.data.perspectiveUpdated)
487
+ cb(handle)
521
488
  })
489
+ // Evict stale proxy and create fresh one with updated handle
490
+ this.#proxyCache.delete(handle.uuid)
491
+ this.getOrCreateProxy(handle)
522
492
  },
523
493
  error: (e) => console.error(e)
524
494
  })
@@ -556,9 +526,12 @@ export class PerspectiveClient {
556
526
  }
557
527
  `}).subscribe({
558
528
  next: result => {
529
+ const uuid = result.data.perspectiveRemoved
559
530
  this.#perspectiveRemovedCallbacks.forEach(cb => {
560
- cb(result.data.perspectiveRemoved)
531
+ cb(uuid)
561
532
  })
533
+ // Evict from cache
534
+ this.#proxyCache.delete(uuid)
562
535
  },
563
536
  error: (e) => console.error(e)
564
537
  })