@coasys/ad4m 0.12.0 → 0.13.0-postmessage-ws-proxy.0

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 (388) hide show
  1. package/README.md +2 -25
  2. package/jest-setup.ts +1 -1
  3. package/lib/index.cjs +7444 -13939
  4. package/lib/index.js +5322 -11888
  5. package/lib/src/Ad4mClient.d.ts +9 -3
  6. package/lib/src/Ad4mClient.js +40 -13
  7. package/lib/src/Literal.js +6 -5
  8. package/lib/src/agent/Agent.js +26 -272
  9. package/lib/src/agent/AgentClient.d.ts +33 -21
  10. package/lib/src/agent/AgentClient.js +180 -373
  11. package/lib/src/agent/AgentStatus.js +2 -37
  12. package/lib/src/ai/AIClient.d.ts +12 -4
  13. package/lib/src/ai/AIClient.js +81 -285
  14. package/lib/src/ai/AITypes.d.ts +55 -0
  15. package/lib/src/ai/AITypes.js +18 -0
  16. package/lib/src/ai/Tasks.js +10 -133
  17. package/lib/src/apiClient.d.ts +53 -0
  18. package/lib/src/apiClient.js +234 -0
  19. package/lib/src/cache/PersistentCache.d.ts +40 -0
  20. package/lib/src/cache/PersistentCache.js +139 -0
  21. package/lib/src/expression/Expression.d.ts +1 -1
  22. package/lib/src/expression/Expression.js +13 -122
  23. package/lib/src/expression/ExpressionClient.d.ts +11 -3
  24. package/lib/src/expression/ExpressionClient.js +120 -75
  25. package/lib/src/expression/ExpressionRef.js +4 -27
  26. package/lib/src/generated/api/AIModelLoadingStatus.d.ts +7 -0
  27. package/lib/src/generated/api/AIModelLoadingStatus.js +2 -0
  28. package/lib/src/generated/api/AddLinkExpressionRequest.d.ts +6 -0
  29. package/lib/src/generated/api/AddLinkExpressionRequest.js +1 -0
  30. package/lib/src/generated/api/AddLinkRequest.d.ts +6 -0
  31. package/lib/src/generated/api/AddLinkRequest.js +1 -0
  32. package/lib/src/generated/api/AddLinksBulkRequest.d.ts +6 -0
  33. package/lib/src/generated/api/AddLinksBulkRequest.js +1 -0
  34. package/lib/src/generated/api/AddSdnaRequest.d.ts +6 -0
  35. package/lib/src/generated/api/AddSdnaRequest.js +2 -0
  36. package/lib/src/generated/api/ApplyTemplateRequest.d.ts +4 -0
  37. package/lib/src/generated/api/ApplyTemplateRequest.js +2 -0
  38. package/lib/src/generated/api/AuthInfoInput.d.ts +10 -0
  39. package/lib/src/generated/api/AuthInfoInput.js +1 -0
  40. package/lib/src/generated/api/CapabilityInput.d.ts +5 -0
  41. package/lib/src/generated/api/CapabilityInput.js +1 -0
  42. package/lib/src/generated/api/CommitBatchRequest.d.ts +3 -0
  43. package/lib/src/generated/api/CommitBatchRequest.js +2 -0
  44. package/lib/src/generated/api/ComputeLogEntry.d.ts +9 -0
  45. package/lib/src/generated/api/ComputeLogEntry.js +2 -0
  46. package/lib/src/generated/api/CreateExpressionRequest.d.ts +4 -0
  47. package/lib/src/generated/api/CreateExpressionRequest.js +2 -0
  48. package/lib/src/generated/api/CreatePerspectiveRequest.d.ts +3 -0
  49. package/lib/src/generated/api/CreatePerspectiveRequest.js +2 -0
  50. package/lib/src/generated/api/CreateSubjectRequest.d.ts +6 -0
  51. package/lib/src/generated/api/CreateSubjectRequest.js +2 -0
  52. package/lib/src/generated/api/EmbedRequest.d.ts +4 -0
  53. package/lib/src/generated/api/EmbedRequest.js +2 -0
  54. package/lib/src/generated/api/EntanglementProofInput.d.ts +6 -0
  55. package/lib/src/generated/api/EntanglementProofInput.js +2 -0
  56. package/lib/src/generated/api/ExecuteCommandsRequest.d.ts +4 -0
  57. package/lib/src/generated/api/ExecuteCommandsRequest.js +2 -0
  58. package/lib/src/generated/api/ExportRequest.d.ts +5 -0
  59. package/lib/src/generated/api/ExportRequest.js +2 -0
  60. package/lib/src/generated/api/ExpressionManyRequest.d.ts +3 -0
  61. package/lib/src/generated/api/ExpressionManyRequest.js +2 -0
  62. package/lib/src/generated/api/ExpressionProof.d.ts +4 -0
  63. package/lib/src/generated/api/ExpressionProof.js +2 -0
  64. package/lib/src/generated/api/ExpressionProofInput.d.ts +6 -0
  65. package/lib/src/generated/api/ExpressionProofInput.js +2 -0
  66. package/lib/src/generated/api/FriendSendMessageRequest.d.ts +3 -0
  67. package/lib/src/generated/api/FriendSendMessageRequest.js +2 -0
  68. package/lib/src/generated/api/FriendsListRequest.d.ts +3 -0
  69. package/lib/src/generated/api/FriendsListRequest.js +2 -0
  70. package/lib/src/generated/api/GenerateAgentRequest.d.ts +3 -0
  71. package/lib/src/generated/api/GenerateAgentRequest.js +2 -0
  72. package/lib/src/generated/api/GenerateJwtRequest.d.ts +4 -0
  73. package/lib/src/generated/api/GenerateJwtRequest.js +2 -0
  74. package/lib/src/generated/api/GetSubjectDataRequest.d.ts +4 -0
  75. package/lib/src/generated/api/GetSubjectDataRequest.js +2 -0
  76. package/lib/src/generated/api/ImportAgentRequest.d.ts +6 -0
  77. package/lib/src/generated/api/ImportAgentRequest.js +2 -0
  78. package/lib/src/generated/api/ImportRequest.d.ts +4 -0
  79. package/lib/src/generated/api/ImportRequest.js +2 -0
  80. package/lib/src/generated/api/JoinNeighbourhoodRequest.d.ts +3 -0
  81. package/lib/src/generated/api/JoinNeighbourhoodRequest.js +2 -0
  82. package/lib/src/generated/api/LanguageMetaInput.d.ts +6 -0
  83. package/lib/src/generated/api/LanguageMetaInput.js +2 -0
  84. package/lib/src/generated/api/Link.d.ts +5 -0
  85. package/lib/src/generated/api/Link.js +2 -0
  86. package/lib/src/generated/api/LinkExpression.d.ts +10 -0
  87. package/lib/src/generated/api/LinkExpression.js +1 -0
  88. package/lib/src/generated/api/LinkExpressionInput.d.ts +10 -0
  89. package/lib/src/generated/api/LinkExpressionInput.js +1 -0
  90. package/lib/src/generated/api/LinkInput.d.ts +5 -0
  91. package/lib/src/generated/api/LinkInput.js +2 -0
  92. package/lib/src/generated/api/LinkLanguageTemplatesRequest.d.ts +3 -0
  93. package/lib/src/generated/api/LinkLanguageTemplatesRequest.js +2 -0
  94. package/lib/src/generated/api/LinkMutations.d.ts +6 -0
  95. package/lib/src/generated/api/LinkMutations.js +1 -0
  96. package/lib/src/generated/api/LinkStatus.d.ts +1 -0
  97. package/lib/src/generated/api/LinkStatus.js +2 -0
  98. package/lib/src/generated/api/LockAgentRequest.d.ts +3 -0
  99. package/lib/src/generated/api/LockAgentRequest.js +2 -0
  100. package/lib/src/generated/api/ModelType.d.ts +1 -0
  101. package/lib/src/generated/api/ModelType.js +2 -0
  102. package/lib/src/generated/api/NotificationInput.d.ts +10 -0
  103. package/lib/src/generated/api/NotificationInput.js +2 -0
  104. package/lib/src/generated/api/OpenLinkRequest.d.ts +3 -0
  105. package/lib/src/generated/api/OpenLinkRequest.js +2 -0
  106. package/lib/src/generated/api/PermitCapabilityRequest.d.ts +3 -0
  107. package/lib/src/generated/api/PermitCapabilityRequest.js +2 -0
  108. package/lib/src/generated/api/PerspectiveInput.d.ts +4 -0
  109. package/lib/src/generated/api/PerspectiveInput.js +1 -0
  110. package/lib/src/generated/api/PromptRequest.d.ts +4 -0
  111. package/lib/src/generated/api/PromptRequest.js +2 -0
  112. package/lib/src/generated/api/PublishLanguageRequest.d.ts +5 -0
  113. package/lib/src/generated/api/PublishLanguageRequest.js +1 -0
  114. package/lib/src/generated/api/PublishNeighbourhoodRequest.d.ts +6 -0
  115. package/lib/src/generated/api/PublishNeighbourhoodRequest.js +1 -0
  116. package/lib/src/generated/api/QueryRequest.d.ts +7 -0
  117. package/lib/src/generated/api/QueryRequest.js +2 -0
  118. package/lib/src/generated/api/RemoveLinkRequest.d.ts +5 -0
  119. package/lib/src/generated/api/RemoveLinkRequest.js +1 -0
  120. package/lib/src/generated/api/RemoveLinksBulkRequest.d.ts +5 -0
  121. package/lib/src/generated/api/RemoveLinksBulkRequest.js +1 -0
  122. package/lib/src/generated/api/ResourceInput.d.ts +4 -0
  123. package/lib/src/generated/api/ResourceInput.js +2 -0
  124. package/lib/src/generated/api/SetDefaultModelRequest.d.ts +4 -0
  125. package/lib/src/generated/api/SetDefaultModelRequest.js +1 -0
  126. package/lib/src/generated/api/SetFreeHostingEnabledRequest.d.ts +3 -0
  127. package/lib/src/generated/api/SetFreeHostingEnabledRequest.js +2 -0
  128. package/lib/src/generated/api/SetMultiUserRequest.d.ts +3 -0
  129. package/lib/src/generated/api/SetMultiUserRequest.js +2 -0
  130. package/lib/src/generated/api/SetStatusRequest.d.ts +3 -0
  131. package/lib/src/generated/api/SetStatusRequest.js +2 -0
  132. package/lib/src/generated/api/SignMessageRequest.d.ts +3 -0
  133. package/lib/src/generated/api/SignMessageRequest.js +2 -0
  134. package/lib/src/generated/api/TrustedAgentsRequest.d.ts +3 -0
  135. package/lib/src/generated/api/TrustedAgentsRequest.js +2 -0
  136. package/lib/src/generated/api/UnlockAgentRequest.d.ts +4 -0
  137. package/lib/src/generated/api/UnlockAgentRequest.js +2 -0
  138. package/lib/src/generated/api/UpdateLinkRequest.d.ts +7 -0
  139. package/lib/src/generated/api/UpdateLinkRequest.js +1 -0
  140. package/lib/src/generated/api/UpdatePerspectiveRequest.d.ts +3 -0
  141. package/lib/src/generated/api/UpdatePerspectiveRequest.js +2 -0
  142. package/lib/src/generated/api/UserStatistics.d.ts +9 -0
  143. package/lib/src/generated/api/UserStatistics.js +2 -0
  144. package/lib/src/generated/api/VerificationRequestResult.d.ts +6 -0
  145. package/lib/src/generated/api/VerificationRequestResult.js +2 -0
  146. package/lib/src/generated/api/VerifySignatureRequest.d.ts +5 -0
  147. package/lib/src/generated/api/VerifySignatureRequest.js +2 -0
  148. package/lib/src/generated/api/WriteSettingsRequest.d.ts +3 -0
  149. package/lib/src/generated/api/WriteSettingsRequest.js +2 -0
  150. package/lib/src/generated/api/index.d.ts +42 -0
  151. package/lib/src/generated/api/index.js +7 -0
  152. package/lib/src/index.d.ts +7 -2
  153. package/lib/src/index.js +7 -2
  154. package/lib/src/jestSetup.d.ts +0 -1
  155. package/lib/src/jestSetup.js +1 -1
  156. package/lib/src/language/Icon.js +2 -21
  157. package/lib/src/language/Language.d.ts +26 -176
  158. package/lib/src/language/Language.js +32 -72
  159. package/lib/src/language/LanguageClient.d.ts +2 -2
  160. package/lib/src/language/LanguageClient.js +15 -105
  161. package/lib/src/language/LanguageHandle.js +2 -41
  162. package/lib/src/language/LanguageMeta.js +4 -75
  163. package/lib/src/language/LanguageRef.js +2 -25
  164. package/lib/src/links/Links.js +14 -111
  165. package/lib/src/model/Ad4mModel.d.ts +150 -62
  166. package/lib/src/model/Ad4mModel.js +689 -417
  167. package/lib/src/model/Ad4mModel.test.js +1434 -688
  168. package/lib/src/model/ModelQueryBuilder.d.ts +59 -38
  169. package/lib/src/model/ModelQueryBuilder.js +317 -153
  170. package/lib/src/model/Subject.js +6 -6
  171. package/lib/src/model/decorators.d.ts +36 -8
  172. package/lib/src/model/decorators.js +41 -14
  173. package/lib/src/model/hydration-batch.d.ts +34 -0
  174. package/lib/src/model/hydration-batch.js +147 -0
  175. package/lib/src/model/hydration-batching.test.d.ts +7 -0
  176. package/lib/src/model/hydration-batching.test.js +456 -0
  177. package/lib/src/model/hydration.d.ts +3 -92
  178. package/lib/src/model/hydration.js +3 -691
  179. package/lib/src/model/index.d.ts +3 -4
  180. package/lib/src/model/index.js +3 -4
  181. package/lib/src/model/json-schema.js +2 -2
  182. package/lib/src/model/model-manifest.d.ts +41 -0
  183. package/lib/src/model/model-manifest.js +11 -0
  184. package/lib/src/model/query-cache.d.ts +30 -0
  185. package/lib/src/model/query-cache.js +66 -0
  186. package/lib/src/model/query-cache.test.d.ts +1 -0
  187. package/lib/src/model/query-cache.test.js +43 -0
  188. package/lib/src/model/query-common.d.ts +1 -1
  189. package/lib/src/model/query-common.js +1 -1
  190. package/lib/src/model/query-sparql-batch.d.ts +17 -0
  191. package/lib/src/model/query-sparql-batch.js +260 -0
  192. package/lib/src/model/query-sparql.d.ts +51 -0
  193. package/lib/src/model/query-sparql.js +466 -0
  194. package/lib/src/model/query-sparql.test.d.ts +1 -0
  195. package/lib/src/model/query-sparql.test.js +312 -0
  196. package/lib/src/model/{surreal-utils.d.ts → query-utils.d.ts} +13 -9
  197. package/lib/src/model/query-utils.js +189 -0
  198. package/lib/src/model/relation-filtering.test.js +15 -15
  199. package/lib/src/model/sdna.js +1 -1
  200. package/lib/src/model/shacl-gen.js +46 -18
  201. package/lib/src/model/subscription-delta.test.d.ts +24 -0
  202. package/lib/src/model/subscription-delta.test.js +180 -0
  203. package/lib/src/model/subscription-pool.d.ts +39 -0
  204. package/lib/src/model/subscription-pool.js +165 -0
  205. package/lib/src/model/types.d.ts +60 -5
  206. package/lib/src/model/types.js +5 -2
  207. package/lib/src/neighbourhood/Neighbourhood.js +4 -32
  208. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +3 -3
  209. package/lib/src/neighbourhood/NeighbourhoodClient.js +40 -203
  210. package/lib/src/perspectives/LinkQuery.js +2 -42
  211. package/lib/src/perspectives/Perspective.d.ts +1 -1
  212. package/lib/src/perspectives/Perspective.js +10 -52
  213. package/lib/src/perspectives/PerspectiveClient.d.ts +22 -32
  214. package/lib/src/perspectives/PerspectiveClient.js +189 -454
  215. package/lib/src/perspectives/PerspectiveClient.test.d.ts +1 -0
  216. package/lib/src/perspectives/PerspectiveClient.test.js +224 -0
  217. package/lib/src/perspectives/PerspectiveDiff.js +4 -31
  218. package/lib/src/perspectives/PerspectiveHandle.js +3 -43
  219. package/lib/src/perspectives/PerspectiveProxy.d.ts +156 -78
  220. package/lib/src/perspectives/PerspectiveProxy.js +474 -389
  221. package/lib/src/perspectives/PerspectiveResolver.d.ts +0 -1
  222. package/lib/src/perspectives/PerspectiveResolver.js +0 -11
  223. package/lib/src/perspectives/WakerSubscriptionManager.d.ts +3 -3
  224. package/lib/src/perspectives/WakerSubscriptionManager.js +12 -12
  225. package/lib/src/runtime/RuntimeClient.d.ts +34 -6
  226. package/lib/src/runtime/RuntimeClient.js +131 -362
  227. package/lib/src/runtime/RuntimeResolver.d.ts +28 -1
  228. package/lib/src/runtime/RuntimeResolver.js +197 -4
  229. package/lib/src/runtime/RuntimeTypes.d.ts +101 -0
  230. package/lib/src/runtime/RuntimeTypes.js +45 -0
  231. package/lib/src/schema.gql +27 -1
  232. package/lib/src/shacl/SHACLFlow.d.ts +1 -1
  233. package/lib/src/shacl/SHACLFlow.js +9 -9
  234. package/lib/src/shacl/SHACLShape.d.ts +8 -1
  235. package/lib/src/shacl/SHACLShape.js +57 -38
  236. package/lib/src/shacl/SHACLShape.test.js +82 -0
  237. package/lib/src/typeDefs.js +3 -602
  238. package/lib/src/utils.d.ts +6 -6
  239. package/lib/src/utils.js +6 -6
  240. package/package.json +6 -18
  241. package/rollup.config.js +0 -7
  242. package/src/Ad4mClient.test.ts +1192 -1288
  243. package/src/Ad4mClient.ts +46 -15
  244. package/src/Literal.test.ts +10 -5
  245. package/src/Literal.ts +6 -5
  246. package/src/agent/Agent.ts +0 -99
  247. package/src/agent/AgentClient.ts +238 -465
  248. package/src/agent/AgentStatus.ts +0 -12
  249. package/src/ai/AIClient.ts +96 -296
  250. package/src/ai/AITypes.ts +66 -0
  251. package/src/ai/Tasks.ts +0 -52
  252. package/src/apiClient.ts +287 -0
  253. package/src/cache/PersistentCache.ts +145 -0
  254. package/src/cache/cache.test.ts +713 -0
  255. package/src/expression/Expression.ts +3 -43
  256. package/src/expression/ExpressionClient.ts +148 -77
  257. package/src/expression/ExpressionRef.ts +2 -8
  258. package/src/generated/api/AIModelLoadingStatus.ts +3 -0
  259. package/src/generated/api/AddLinkExpressionRequest.ts +4 -0
  260. package/src/generated/api/AddLinkRequest.ts +4 -0
  261. package/src/generated/api/AddLinksBulkRequest.ts +4 -0
  262. package/src/generated/api/AddSdnaRequest.ts +3 -0
  263. package/src/generated/api/ApplyTemplateRequest.ts +3 -0
  264. package/src/generated/api/AuthInfoInput.ts +4 -0
  265. package/src/generated/api/CapabilityInput.ts +4 -0
  266. package/src/generated/api/CommitBatchRequest.ts +3 -0
  267. package/src/generated/api/ComputeLogEntry.ts +3 -0
  268. package/src/generated/api/CreateExpressionRequest.ts +3 -0
  269. package/src/generated/api/CreatePerspectiveRequest.ts +3 -0
  270. package/src/generated/api/CreateSubjectRequest.ts +3 -0
  271. package/src/generated/api/EmbedRequest.ts +3 -0
  272. package/src/generated/api/EntanglementProofInput.ts +3 -0
  273. package/src/generated/api/ExecuteCommandsRequest.ts +3 -0
  274. package/src/generated/api/ExportRequest.ts +3 -0
  275. package/src/generated/api/ExpressionManyRequest.ts +3 -0
  276. package/src/generated/api/ExpressionProof.ts +3 -0
  277. package/src/generated/api/ExpressionProofInput.ts +3 -0
  278. package/src/generated/api/FriendSendMessageRequest.ts +3 -0
  279. package/src/generated/api/FriendsListRequest.ts +3 -0
  280. package/src/generated/api/GenerateAgentRequest.ts +3 -0
  281. package/src/generated/api/GenerateJwtRequest.ts +3 -0
  282. package/src/generated/api/GetSubjectDataRequest.ts +3 -0
  283. package/src/generated/api/ImportAgentRequest.ts +3 -0
  284. package/src/generated/api/ImportRequest.ts +3 -0
  285. package/src/generated/api/JoinNeighbourhoodRequest.ts +3 -0
  286. package/src/generated/api/LanguageMetaInput.ts +3 -0
  287. package/src/generated/api/Link.ts +3 -0
  288. package/src/generated/api/LinkExpression.ts +6 -0
  289. package/src/generated/api/LinkExpressionInput.ts +6 -0
  290. package/src/generated/api/LinkInput.ts +3 -0
  291. package/src/generated/api/LinkLanguageTemplatesRequest.ts +3 -0
  292. package/src/generated/api/LinkMutations.ts +5 -0
  293. package/src/generated/api/LinkStatus.ts +3 -0
  294. package/src/generated/api/LockAgentRequest.ts +3 -0
  295. package/src/generated/api/ModelType.ts +3 -0
  296. package/src/generated/api/NotificationInput.ts +3 -0
  297. package/src/generated/api/OpenLinkRequest.ts +3 -0
  298. package/src/generated/api/PermitCapabilityRequest.ts +3 -0
  299. package/src/generated/api/PerspectiveInput.ts +4 -0
  300. package/src/generated/api/PromptRequest.ts +3 -0
  301. package/src/generated/api/PublishLanguageRequest.ts +4 -0
  302. package/src/generated/api/PublishNeighbourhoodRequest.ts +4 -0
  303. package/src/generated/api/QueryRequest.ts +6 -0
  304. package/src/generated/api/RemoveLinkRequest.ts +4 -0
  305. package/src/generated/api/RemoveLinksBulkRequest.ts +4 -0
  306. package/src/generated/api/ResourceInput.ts +3 -0
  307. package/src/generated/api/SetDefaultModelRequest.ts +4 -0
  308. package/src/generated/api/SetFreeHostingEnabledRequest.ts +3 -0
  309. package/src/generated/api/SetMultiUserRequest.ts +3 -0
  310. package/src/generated/api/SetStatusRequest.ts +3 -0
  311. package/src/generated/api/SignMessageRequest.ts +3 -0
  312. package/src/generated/api/TrustedAgentsRequest.ts +3 -0
  313. package/src/generated/api/UnlockAgentRequest.ts +3 -0
  314. package/src/generated/api/UpdateLinkRequest.ts +5 -0
  315. package/src/generated/api/UpdatePerspectiveRequest.ts +3 -0
  316. package/src/generated/api/UserStatistics.ts +3 -0
  317. package/src/generated/api/VerificationRequestResult.ts +3 -0
  318. package/src/generated/api/VerifySignatureRequest.ts +3 -0
  319. package/src/generated/api/WriteSettingsRequest.ts +3 -0
  320. package/src/generated/api/index.ts +49 -0
  321. package/src/index.ts +7 -2
  322. package/src/jestSetup.ts +1 -1
  323. package/src/language/Icon.ts +0 -4
  324. package/src/language/Language.ts +51 -248
  325. package/src/language/LanguageClient.ts +21 -132
  326. package/src/language/LanguageHandle.ts +0 -14
  327. package/src/language/LanguageMeta.ts +0 -29
  328. package/src/language/LanguageRef.ts +0 -7
  329. package/src/links/Links.ts +0 -37
  330. package/src/model/Ad4mModel.test.ts +1690 -777
  331. package/src/model/Ad4mModel.ts +749 -477
  332. package/src/model/ModelQueryBuilder.ts +356 -171
  333. package/src/model/decorators.ts +71 -22
  334. package/src/model/hydration.ts +3 -772
  335. package/src/model/index.ts +3 -4
  336. package/src/model/json-schema.ts +2 -2
  337. package/src/model/query-cache.test.ts +54 -0
  338. package/src/model/query-cache.ts +79 -0
  339. package/src/model/query-common.ts +1 -1
  340. package/src/model/query-sparql.test.ts +376 -0
  341. package/src/model/query-sparql.ts +504 -0
  342. package/src/model/query-utils.ts +209 -0
  343. package/src/model/relation-filtering.test.ts +15 -15
  344. package/src/model/sdna.ts +1 -1
  345. package/src/model/shacl-gen.ts +47 -17
  346. package/src/model/types.ts +65 -5
  347. package/src/neighbourhood/Neighbourhood.ts +0 -9
  348. package/src/neighbourhood/NeighbourhoodClient.ts +42 -207
  349. package/src/neighbourhood/NeighbourhoodProxy.test.ts +34 -36
  350. package/src/perspectives/LinkQuery.ts +0 -15
  351. package/src/perspectives/Perspective.ts +1 -12
  352. package/src/perspectives/PerspectiveClient.test.ts +262 -0
  353. package/src/perspectives/PerspectiveClient.ts +254 -468
  354. package/src/perspectives/PerspectiveDiff.ts +0 -8
  355. package/src/perspectives/PerspectiveHandle.ts +1 -12
  356. package/src/perspectives/PerspectiveProxy.test.ts +125 -0
  357. package/src/perspectives/PerspectiveProxy.ts +522 -434
  358. package/src/perspectives/WakerSubscriptionManager.ts +13 -13
  359. package/src/runtime/RuntimeClient.ts +166 -369
  360. package/src/runtime/RuntimeTypes.ts +144 -0
  361. package/src/shacl/SHACLFlow.ts +9 -9
  362. package/src/shacl/SHACLShape.test.ts +94 -0
  363. package/src/shacl/SHACLShape.ts +66 -39
  364. package/src/typeDefs.ts +3 -5
  365. package/src/utils.ts +6 -6
  366. package/tsconfig.json +3 -7
  367. package/lib/src/model/query-surreal.d.ts +0 -44
  368. package/lib/src/model/query-surreal.js +0 -249
  369. package/lib/src/model/surreal-utils.js +0 -128
  370. package/shims/reflect-metadata.ts +0 -0
  371. package/shims/type-graphql.ts +0 -75
  372. package/src/PubSub.ts +0 -17
  373. package/src/agent/AgentResolver.ts +0 -263
  374. package/src/ai/AIResolver.ts +0 -372
  375. package/src/buildSchema.ts +0 -43
  376. package/src/expression/ExpressionResolver.ts +0 -64
  377. package/src/language/LanguageContext.ts +0 -37
  378. package/src/language/LanguageResolver.ts +0 -98
  379. package/src/model/Subject.ts +0 -159
  380. package/src/model/prolog-facts.ts +0 -145
  381. package/src/model/query-prolog.ts +0 -148
  382. package/src/model/query-surreal.ts +0 -280
  383. package/src/model/surreal-utils.ts +0 -147
  384. package/src/neighbourhood/NeighbourhoodResolver.ts +0 -100
  385. package/src/perspectives/PerspectiveResolver.ts +0 -372
  386. package/src/runtime/RuntimeResolver.ts +0 -617
  387. package/src/unwrapApolloResult.ts +0 -16
  388. package/tsconfig-browser.json +0 -82
@@ -0,0 +1,312 @@
1
+ import { buildSPARQLOrderLimitOffset, buildSPARQLQuery, hasJsOnlyWhereFilters } from './query-sparql';
2
+ // Minimal stubs for ModelMetadata — buildSPARQLOrderLimitOffset only uses the query arg
3
+ const emptyMetadata = { properties: {}, relations: {} };
4
+ // Metadata with a required (non-literal) property and a literal-stored property
5
+ const richMetadata = {
6
+ properties: {
7
+ name: {
8
+ name: 'name',
9
+ predicate: 'flux://name',
10
+ required: true,
11
+ resolveLanguage: 'literal',
12
+ },
13
+ category: {
14
+ name: 'category',
15
+ predicate: 'flux://category',
16
+ required: true,
17
+ resolveLanguage: 'did:lang:abc', // non-literal → URI stored
18
+ },
19
+ description: {
20
+ name: 'description',
21
+ predicate: 'flux://description',
22
+ initial: 'literal://string:empty',
23
+ resolveLanguage: 'literal',
24
+ },
25
+ },
26
+ relations: {},
27
+ };
28
+ const emptyRelations = {};
29
+ describe('hasJsOnlyWhereFilters', () => {
30
+ it('returns false when no where clause', () => {
31
+ expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, undefined)).toBe(false);
32
+ });
33
+ it('returns false when where has literal-stored property with equality filter (pushed to SPARQL)', () => {
34
+ expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { name: 'Pasta' })).toBe(false);
35
+ });
36
+ it('returns true when where has literal-stored property with comparison operator', () => {
37
+ // gt/lt/gte/lte/between/contains remain JS-only
38
+ const meta = { properties: { rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' } }, relations: {} };
39
+ expect(hasJsOnlyWhereFilters(meta, emptyRelations, { rating: { gt: 5 } })).toBe(true);
40
+ });
41
+ it('returns false when where only has non-literal property filter', () => {
42
+ expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { category: 'some://uri' })).toBe(false);
43
+ });
44
+ it('returns true when where has author filter', () => {
45
+ expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { author: 'did:key:abc' })).toBe(true);
46
+ });
47
+ it('returns true when where has timestamp filter', () => {
48
+ expect(hasJsOnlyWhereFilters(richMetadata, emptyRelations, { timestamp: { gt: 100 } })).toBe(true);
49
+ });
50
+ it('returns true for belongsToOne reverse relation', () => {
51
+ const relMeta = { parent: { kind: 'belongsToOne', predicate: 'flux://parent' } };
52
+ const meta = { properties: { parent: { name: 'parent', predicate: 'flux://parent' } }, relations: {} };
53
+ expect(hasJsOnlyWhereFilters(meta, relMeta, { parent: 'some://id' })).toBe(true);
54
+ });
55
+ });
56
+ describe('buildSPARQLQuery — SPARQL-level pagination via subquery', () => {
57
+ const modelClass = {};
58
+ it('includes LIMIT/OFFSET in a pagination subquery when query has them and no JS-only filters', () => {
59
+ const query = { limit: 10, offset: 0, where: { category: 'some://uri' } };
60
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
61
+ // SPARQL should contain LIMIT inside the pagination subquery
62
+ expect(sparql).toContain('LIMIT 10');
63
+ // The outer query should still exist
64
+ expect(sparql).toContain('SELECT ?source ?predicate ?target');
65
+ // Should have a subquery pattern
66
+ expect(sparql).toContain('SELECT DISTINCT ?source');
67
+ });
68
+ it('does NOT include LIMIT/OFFSET when JS-only where filters exist', () => {
69
+ // author is a JS-only filter
70
+ const query = { limit: 10, offset: 0, where: { author: 'did:key:abc' } };
71
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
72
+ expect(sparql).not.toContain('LIMIT');
73
+ expect(sparql).not.toContain('OFFSET');
74
+ });
75
+ });
76
+ describe('buildSPARQLOrderLimitOffset', () => {
77
+ it('always returns empty string (pagination uses subquery pattern instead)', () => {
78
+ expect(buildSPARQLOrderLimitOffset(emptyMetadata, {})).toBe('');
79
+ expect(buildSPARQLOrderLimitOffset(emptyMetadata, { limit: 50 })).toBe('');
80
+ expect(buildSPARQLOrderLimitOffset(emptyMetadata, { limit: 50, offset: 100 })).toBe('');
81
+ expect(buildSPARQLOrderLimitOffset(emptyMetadata, { order: { timestamp: 'DESC' } })).toBe('');
82
+ expect(buildSPARQLOrderLimitOffset(emptyMetadata, {
83
+ order: { timestamp: 'DESC' },
84
+ limit: 50,
85
+ offset: 100,
86
+ })).toBe('');
87
+ });
88
+ });
89
+ describe('buildSPARQLQuery — parse_literal push-down filters', () => {
90
+ const modelClass = {};
91
+ it('generates parse_literal FILTER for equality on literal property', () => {
92
+ const query = { where: { name: 'Alice' } };
93
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
94
+ expect(sparql).toContain('<ad4m://fn/parse_literal>');
95
+ expect(sparql).toContain('STR(<ad4m://fn/parse_literal>(?wTarget_name)) = "Alice"');
96
+ });
97
+ it('generates parse_literal IN FILTER for array on literal property', () => {
98
+ const query = { where: { name: ['Alice', 'Bob'] } };
99
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
100
+ expect(sparql).toContain('STR(<ad4m://fn/parse_literal>(?wTarget_name)) IN ("Alice", "Bob")');
101
+ });
102
+ it('generates parse_literal NOT FILTER for not condition', () => {
103
+ const query = { where: { name: { not: 'deleted' } } };
104
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
105
+ expect(sparql).toContain('STR(<ad4m://fn/parse_literal>(?wTarget_name)) != "deleted"');
106
+ });
107
+ it('does NOT use parse_literal for comparison operators (gt/lt)', () => {
108
+ const meta = {
109
+ properties: {
110
+ rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' },
111
+ },
112
+ relations: {},
113
+ };
114
+ const query = { where: { rating: { gt: 5 } } };
115
+ const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
116
+ expect(sparql).not.toContain('parse_literal');
117
+ expect(sparql).toContain('?wTarget_cmp_rating');
118
+ });
119
+ it('does NOT use parse_literal for non-literal properties', () => {
120
+ const query = { where: { category: 'some://uri' } };
121
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
122
+ expect(sparql).not.toContain('parse_literal');
123
+ expect(sparql).toContain('<some://uri>');
124
+ });
125
+ });
126
+ describe('buildSPARQLQuery — structural correctness', () => {
127
+ const modelClass = {};
128
+ it('generates valid SPARQL with parent filter', () => {
129
+ const query = {
130
+ limit: 50,
131
+ offset: 0,
132
+ parent: { id: 'flux://channel-123', predicate: 'flux://has_message' },
133
+ where: { category: 'some://uri' },
134
+ };
135
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
136
+ expect(sparql).toContain('<flux://channel-123>');
137
+ // SPARQL-level pagination via subquery
138
+ expect(sparql).toContain('LIMIT 50');
139
+ expect(sparql).toContain('SELECT DISTINCT ?source');
140
+ });
141
+ it('does not allow injection through where clause IRI values', () => {
142
+ const query = {
143
+ where: { category: 'some://uri"> . } UNION { SELECT * WHERE { ?s ?p ?o' },
144
+ };
145
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
146
+ // Value should be wrapped in angle brackets as an IRI
147
+ expect(sparql).toContain('<some://uri');
148
+ });
149
+ });
150
+ describe('buildSPARQLQuery — NOT with array push-down', () => {
151
+ const modelClass = {};
152
+ it('generates NOT IN filter for NOT with array of values', () => {
153
+ const query = { where: { name: { not: ['deleted', 'archived'] } } };
154
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
155
+ // Should use NOT IN with parse_literal for literal properties
156
+ expect(sparql).toContain('NOT IN');
157
+ expect(sparql).toContain('"deleted"');
158
+ expect(sparql).toContain('"archived"');
159
+ });
160
+ });
161
+ describe('buildSPARQLQuery — mixed push-down + JS-only', () => {
162
+ const modelClass = {};
163
+ const mixedMeta = {
164
+ properties: {
165
+ name: { name: 'name', predicate: 'flux://name', required: true, resolveLanguage: 'literal' },
166
+ rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' },
167
+ },
168
+ relations: {},
169
+ };
170
+ it('pushes equality to SPARQL but keeps gt as JS-only', () => {
171
+ const query = { where: { name: 'Alice', rating: { gt: 5 } } };
172
+ const sparql = buildSPARQLQuery(mixedMeta, emptyRelations, query, modelClass);
173
+ // name equality should be pushed to SPARQL
174
+ expect(sparql).toContain('parse_literal');
175
+ expect(sparql).toContain('Alice');
176
+ // hasJsOnlyWhereFilters should return true because of rating.gt
177
+ expect(hasJsOnlyWhereFilters(mixedMeta, emptyRelations, query.where)).toBe(true);
178
+ });
179
+ });
180
+ describe('buildSPARQLQuery — non-literal and flag properties', () => {
181
+ const modelClass = {};
182
+ it('does NOT use parse_literal for non-literal (resolveLanguage) properties', () => {
183
+ const meta = {
184
+ properties: {
185
+ avatar: { name: 'avatar', predicate: 'flux://avatar', required: true, resolveLanguage: 'did:lang:some-language' },
186
+ },
187
+ relations: {},
188
+ };
189
+ const query = { where: { avatar: 'https://example.com/pic.jpg' } };
190
+ const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
191
+ expect(sparql).not.toContain('parse_literal');
192
+ });
193
+ it('does NOT use parse_literal for flag properties', () => {
194
+ const meta = {
195
+ properties: {
196
+ isPublic: { name: 'isPublic', predicate: 'flux://isPublic', flag: true, initial: 'flux://true' },
197
+ },
198
+ relations: {},
199
+ };
200
+ const query = { where: { isPublic: 'flux://true' } };
201
+ const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
202
+ expect(sparql).not.toContain('parse_literal');
203
+ });
204
+ });
205
+ describe('buildSPARQLQuery — IRI correctness', () => {
206
+ const modelClass = {};
207
+ it('uses <ad4m://fn/parse_literal> IRI, not fn::parse_literal SurrealDB syntax', () => {
208
+ const query = { where: { name: 'test' } };
209
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
210
+ expect(sparql).toContain('<ad4m://fn/parse_literal>');
211
+ expect(sparql).not.toContain('fn::parse_literal');
212
+ });
213
+ });
214
+ // ──────────────────────────────────────────────────────────
215
+ // SPARQL-level pagination (LIMIT/OFFSET in generated queries)
216
+ // ──────────────────────────────────────────────────────────
217
+ describe('SPARQL-level pagination', () => {
218
+ const modelClass = {};
219
+ it('includes LIMIT in SPARQL when query specifies limit', () => {
220
+ const query = { limit: 30 };
221
+ const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
222
+ expect(sparql).toContain('LIMIT 30');
223
+ expect(sparql).toContain('SELECT DISTINCT ?source');
224
+ });
225
+ it('includes OFFSET in SPARQL when query specifies offset > 0', () => {
226
+ const query = { limit: 20, offset: 40 };
227
+ const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
228
+ expect(sparql).toContain('LIMIT 20');
229
+ expect(sparql).toContain('OFFSET 40');
230
+ });
231
+ it('does NOT include OFFSET when offset is 0', () => {
232
+ const query = { limit: 10, offset: 0 };
233
+ const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
234
+ expect(sparql).toContain('LIMIT 10');
235
+ expect(sparql).not.toContain('OFFSET');
236
+ });
237
+ it('includes ORDER BY in subquery when query.order is specified', () => {
238
+ const query = { limit: 10, order: { name: 'DESC' } };
239
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
240
+ expect(sparql).toContain('ORDER BY DESC(');
241
+ expect(sparql).toContain('LIMIT 10');
242
+ });
243
+ it('does NOT default to ORDER BY timestamp when paginating without explicit order', () => {
244
+ const query = { limit: 30 };
245
+ const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
246
+ expect(sparql).not.toContain('ORDER BY');
247
+ expect(sparql).not.toContain('pg_minTs');
248
+ });
249
+ it('does NOT push pagination to SPARQL when JS-only where filters exist (author)', () => {
250
+ const query = { limit: 10, where: { author: 'did:key:abc' } };
251
+ const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
252
+ expect(sparql).not.toContain('LIMIT');
253
+ expect(sparql).not.toContain('OFFSET');
254
+ });
255
+ it('does NOT push pagination to SPARQL when JS-only where filters exist (gt operator)', () => {
256
+ const meta = {
257
+ properties: {
258
+ rating: { name: 'rating', predicate: 'flux://rating', required: true, resolveLanguage: 'literal' },
259
+ },
260
+ relations: {},
261
+ };
262
+ const query = { limit: 10, where: { rating: { gt: 5 } } };
263
+ const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
264
+ expect(sparql).not.toContain('LIMIT');
265
+ });
266
+ it('wraps pagination in a subquery (outer SELECT fetches all links for the page)', () => {
267
+ const query = { limit: 5, offset: 10 };
268
+ const sparql = buildSPARQLQuery(emptyMetadata, emptyRelations, query, modelClass);
269
+ expect(sparql).toContain('SELECT ?source ?predicate ?target ?author ?timestamp');
270
+ expect(sparql).toContain('SELECT DISTINCT ?source');
271
+ expect(sparql).toContain('LIMIT 5');
272
+ expect(sparql).toContain('OFFSET 10');
273
+ });
274
+ });
275
+ describe('buildSPARQLQuery — set-difference patterns', () => {
276
+ const modelClass = {};
277
+ // Subgroups are grandchildren of channels (channel → conversation → subgroup).
278
+ // Never assume direct child relationships in set-difference queries.
279
+ // A global EXISTS pattern (no parent scoping) should match items regardless of depth.
280
+ it('generates global EXISTS without parent scoping when no parent filter', () => {
281
+ const meta = {
282
+ properties: {
283
+ status: { name: 'status', predicate: 'flux://status', required: true, resolveLanguage: 'literal' },
284
+ },
285
+ relations: {},
286
+ };
287
+ const query = { where: { status: 'active' } };
288
+ const sparql = buildSPARQLQuery(meta, emptyRelations, query, modelClass);
289
+ // Without a parent filter, the query should not scope to any specific parent
290
+ // This ensures grandchildren (items at any depth) are matched
291
+ expect(sparql).not.toContain('flux://has_child');
292
+ expect(sparql).toContain('SELECT ?source ?predicate ?target');
293
+ });
294
+ });
295
+ describe('buildSPARQLQuery — RDF 1.2 reifier patterns', () => {
296
+ const modelClass = {};
297
+ it('uses rdf:reifies instead of GRAPH for link metadata', () => {
298
+ const query = { limit: 10 };
299
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
300
+ expect(sparql).toContain('<http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies>');
301
+ expect(sparql).toContain('<http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies> <<(');
302
+ expect(sparql).toContain('?_reifier');
303
+ expect(sparql).not.toContain('GRAPH');
304
+ expect(sparql).not.toContain('?linkGraph');
305
+ });
306
+ it('binds author and timestamp on the reifier node', () => {
307
+ const query = {};
308
+ const sparql = buildSPARQLQuery(richMetadata, emptyRelations, query, modelClass);
309
+ expect(sparql).toContain('?_reifier <ad4m://ontology/author> ?author');
310
+ expect(sparql).toContain('?_reifier <ad4m://ontology/timestamp> ?timestamp');
311
+ });
312
+ });
@@ -1,35 +1,39 @@
1
1
  /**
2
- * Shared SurrealQL compilation utilities.
2
+ * Shared query compilation utilities.
3
3
  *
4
4
  * These functions are used by both the runtime query builder (`Ad4mModel`) and
5
5
  * the compile-time SHACL generator (`decorators.ts`).
6
+ *
7
+ * All output uses native SPARQL syntax.
6
8
  */
7
9
  import type { WhereCondition, Where, ModelMetadata } from "./types";
8
10
  /**
9
- * Format a JavaScript value for embedding in a SurrealQL expression.
11
+ * Format a JavaScript value for embedding in a query expression.
10
12
  *
11
13
  * - Strings are single-quoted with backslash / quote / newline escaping
12
14
  * - Numbers and booleans are passed through as-is
13
15
  * - Arrays are recursed
14
16
  */
15
- export declare function formatSurrealValue(value: any): string;
17
+ export declare function formatQueryValue(value: any): string;
16
18
  /**
17
- * Compile a single Where condition for a property into a SurrealQL
18
- * graph-traversal sub-expression.
19
+ * Compile a single Where condition for a property into a SPARQL triple pattern
20
+ * that can be embedded in a getter's WHERE clause.
19
21
  *
20
- * Operates on the **target node** — generates `count(->link[WHERE ...]) > 0`
21
- * style checks that can be used inside a `[WHERE ...]` filter on nodes.
22
+ * The generated pattern operates on `?target` — e.g.:
23
+ * `?target <pred> <value> .`
24
+ * `FILTER EXISTS { ?target <pred> ?_v0 }`
22
25
  *
23
26
  * @param predicate - The predicate URI for the property
24
27
  * @param condition - The Where condition value
25
28
  * @param opts - Optional settings (e.g. `resolveLanguage`)
26
- * @returns A single SurrealQL condition string
29
+ * @returns A single SPARQL condition string
27
30
  */
28
31
  export declare function buildWhereCondition(predicate: string, condition: WhereCondition, opts?: {
29
32
  resolveLanguage?: string;
33
+ varIndex?: number;
30
34
  }): string;
31
35
  /**
32
- * Compile a full `Where` clause to an array of SurrealQL condition strings.
36
+ * Compile a full `Where` clause to an array of SPARQL condition strings.
33
37
  *
34
38
  * When `metadata` is provided, property names are resolved to predicates
35
39
  * using the model's property metadata. When `metadata` is `null`, property
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Shared query compilation utilities.
3
+ *
4
+ * These functions are used by both the runtime query builder (`Ad4mModel`) and
5
+ * the compile-time SHACL generator (`decorators.ts`).
6
+ *
7
+ * All output uses native SPARQL syntax.
8
+ */
9
+ import { escapeQueryString } from "../utils";
10
+ /**
11
+ * Format a JavaScript value for embedding in a query expression.
12
+ *
13
+ * - Strings are single-quoted with backslash / quote / newline escaping
14
+ * - Numbers and booleans are passed through as-is
15
+ * - Arrays are recursed
16
+ */
17
+ export function formatQueryValue(value) {
18
+ if (typeof value === 'string') {
19
+ // Escape backslashes first, then single quotes and other special characters
20
+ const escaped = value
21
+ .replace(/\\/g, '\\\\') // Backslash -> \\
22
+ .replace(/'/g, "\\'") // Single quote -> \'
23
+ .replace(/"/g, '\\"') // Double quote -> \"
24
+ .replace(/\n/g, '\\n') // Newline -> \n
25
+ .replace(/\r/g, '\\r') // Carriage return -> \r
26
+ .replace(/\t/g, '\\t'); // Tab -> \t
27
+ return `'${escaped}'`;
28
+ }
29
+ else if (typeof value === 'number' || typeof value === 'boolean') {
30
+ return String(value);
31
+ }
32
+ else if (Array.isArray(value)) {
33
+ return `[${value.map(v => formatQueryValue(v)).join(', ')}]`;
34
+ }
35
+ else {
36
+ return String(value);
37
+ }
38
+ }
39
+ /**
40
+ * Compile a single Where condition for a property into a SPARQL triple pattern
41
+ * that can be embedded in a getter's WHERE clause.
42
+ *
43
+ * The generated pattern operates on `?target` — e.g.:
44
+ * `?target <pred> <value> .`
45
+ * `FILTER EXISTS { ?target <pred> ?_v0 }`
46
+ *
47
+ * @param predicate - The predicate URI for the property
48
+ * @param condition - The Where condition value
49
+ * @param opts - Optional settings (e.g. `resolveLanguage`)
50
+ * @returns A single SPARQL condition string
51
+ */
52
+ export function buildWhereCondition(predicate, condition, opts) {
53
+ const escapedPredicate = escapeQueryString(predicate);
54
+ // For literal-resolved properties, we match against the raw IRI (which is a literal: URI)
55
+ // For language-resolved properties, we match against the plain URI
56
+ // For literal-resolved properties, values are stored as IRIs like <literal:string:VALUE>
57
+ // For language-resolved properties, values are plain URIs
58
+ const isLiteral = !opts?.resolveLanguage || opts.resolveLanguage === 'literal';
59
+ const varSuffix = opts?.varIndex ?? 0;
60
+ function formatValue(v) {
61
+ if (typeof v === 'string') {
62
+ return isLiteral
63
+ ? `<literal:string:${escapeQueryString(v)}>`
64
+ : `<${escapeQueryString(v)}>`;
65
+ }
66
+ // Numbers/booleans — also stored as literal:string: in AD4M
67
+ return `<literal:string:${v}>`;
68
+ }
69
+ /**
70
+ * For literal properties, generate a FILTER-based match that handles both
71
+ * the raw value and the literal-encoded value. This mirrors the Rust
72
+ * `build_projection_where_patterns` approach which uses:
73
+ * FILTER(STR(?var) = "value" || STR(?var) = "literal:string:encodedValue")
74
+ * This is more robust than exact IRI matching because it handles edge cases
75
+ * where the stored format might vary.
76
+ */
77
+ function formatLiteralFilter(varName, v) {
78
+ const raw = escapeQueryString(String(v));
79
+ const encoded = encodeURIComponent(String(v))
80
+ .replace(/!/g, '%21')
81
+ .replace(/'/g, '%27')
82
+ .replace(/\(/g, '%28')
83
+ .replace(/\)/g, '%29')
84
+ .replace(/\*/g, '%2A');
85
+ if (typeof v === 'string') {
86
+ return `FILTER(STR(${varName}) = "${raw}" || STR(${varName}) = "literal:string:${encoded}")`;
87
+ }
88
+ else if (typeof v === 'number') {
89
+ return `FILTER(STR(${varName}) = "${v}" || STR(${varName}) = "literal:number:${v}")`;
90
+ }
91
+ else if (typeof v === 'boolean') {
92
+ return `FILTER(STR(${varName}) = "${v}" || STR(${varName}) = "literal:boolean:${v}")`;
93
+ }
94
+ return `FILTER(STR(${varName}) = "${raw}")`;
95
+ }
96
+ if (Array.isArray(condition)) {
97
+ // Array values → FILTER IN
98
+ if (isLiteral) {
99
+ const varName = `?_wc${varSuffix}`;
100
+ const inValues = condition.flatMap(v => {
101
+ const raw = escapeQueryString(String(v));
102
+ const encoded = encodeURIComponent(String(v))
103
+ .replace(/!/g, '%21').replace(/'/g, '%27')
104
+ .replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A');
105
+ return [`"${raw}"`, `"literal:string:${encoded}"`];
106
+ }).join(', ');
107
+ return `?target <${escapedPredicate}> ${varName} . FILTER(STR(${varName}) IN (${inValues}))`;
108
+ }
109
+ const formattedValues = condition.map(formatValue).join(', ');
110
+ return `FILTER EXISTS { ?target <${escapedPredicate}> ?_val . FILTER(?_val IN (${formattedValues})) }`;
111
+ }
112
+ else if (typeof condition === 'object' && condition !== null) {
113
+ // Operator object
114
+ const ops = condition;
115
+ const parts = [];
116
+ if (ops.not !== undefined) {
117
+ if (Array.isArray(ops.not)) {
118
+ const formattedValues = ops.not.map((v) => formatValue(v)).join(', ');
119
+ // FILTER NOT EXISTS here is scoped to a specific predicate, not a global scan,
120
+ // so O(N²) behavior is bounded by the number of values for this property.
121
+ parts.push(`FILTER NOT EXISTS { ?target <${escapedPredicate}> ?_nval . FILTER(?_nval IN (${formattedValues})) }`);
122
+ }
123
+ else {
124
+ // FILTER NOT EXISTS here is scoped to a specific predicate, not a global scan,
125
+ // so O(N²) behavior is bounded by the number of values for this property.
126
+ parts.push(`FILTER NOT EXISTS { ?target <${escapedPredicate}> ${formatValue(ops.not)} }`);
127
+ }
128
+ }
129
+ // Comparison operators — ensure property exists
130
+ const hasComparisonOps = ops.gt !== undefined || ops.gte !== undefined ||
131
+ ops.lt !== undefined || ops.lte !== undefined ||
132
+ ops.between !== undefined || ops.contains !== undefined;
133
+ if (hasComparisonOps) {
134
+ parts.push(`FILTER EXISTS { ?target <${escapedPredicate}> ?_cmp }`);
135
+ }
136
+ return parts.join(' ');
137
+ }
138
+ else {
139
+ // Simple equality
140
+ if (isLiteral) {
141
+ // Use FILTER matching (mirrors Rust build_projection_where_patterns)
142
+ // to handle both raw values and literal-encoded IRIs robustly.
143
+ const varName = `?_wc${varSuffix}`;
144
+ return `?target <${escapedPredicate}> ${varName} . ${formatLiteralFilter(varName, condition)}`;
145
+ }
146
+ return `?target <${escapedPredicate}> ${formatValue(condition)} .`;
147
+ }
148
+ }
149
+ /**
150
+ * Compile a full `Where` clause to an array of SPARQL condition strings.
151
+ *
152
+ * When `metadata` is provided, property names are resolved to predicates
153
+ * using the model's property metadata. When `metadata` is `null`, property
154
+ * names are treated as raw predicate URIs.
155
+ *
156
+ * Only handles **regular properties** — special fields (`id`, `author`,
157
+ * `timestamp`) and relation fields are skipped (they are only relevant at
158
+ * runtime query time, not for compile-time getter generation).
159
+ */
160
+ export function compileWhereClause(where, metadata) {
161
+ const conditions = [];
162
+ for (const [propertyName, condition] of Object.entries(where)) {
163
+ // Skip special fields that don't apply at compile time
164
+ if (['id', 'author', 'timestamp'].includes(propertyName))
165
+ continue;
166
+ let predicate;
167
+ let resolveLanguage;
168
+ if (metadata) {
169
+ const propMeta = metadata.properties[propertyName];
170
+ if (propMeta) {
171
+ predicate = propMeta.predicate;
172
+ resolveLanguage = propMeta.resolveLanguage;
173
+ }
174
+ else {
175
+ // Property not found in metadata — treat name as raw predicate URI
176
+ predicate = propertyName;
177
+ }
178
+ }
179
+ else {
180
+ // No metadata — treat name as raw predicate URI
181
+ predicate = propertyName;
182
+ }
183
+ const cond = buildWhereCondition(predicate, condition, { resolveLanguage, varIndex: conditions.length });
184
+ if (cond) {
185
+ conditions.push(cond);
186
+ }
187
+ }
188
+ return conditions;
189
+ }