@coasys/ad4m 0.12.0-rc1-dev.1 → 0.12.0-rc1-dev.2

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 (128) hide show
  1. package/jest.config.js +4 -4
  2. package/lib/index.cjs +5999 -6046
  3. package/lib/index.js +5977 -6040
  4. package/lib/src/Ad4mClient.d.ts +16 -9
  5. package/lib/src/Ad4mClient.js +25 -46
  6. package/lib/src/Ad4mClient.test.d.ts +1 -0
  7. package/lib/src/Ad4mClient.test.js +1204 -0
  8. package/lib/src/Literal.d.ts +2 -1
  9. package/lib/src/Literal.js +35 -38
  10. package/lib/src/Literal.test.d.ts +1 -0
  11. package/lib/src/Literal.test.js +41 -0
  12. package/lib/src/SmartLiteral.d.ts +2 -1
  13. package/lib/src/SmartLiteral.js +15 -30
  14. package/lib/src/agent/AgentClient.d.ts +4 -1
  15. package/lib/src/agent/AgentClient.js +99 -79
  16. package/lib/src/ai/AIClient.d.ts +2 -1
  17. package/lib/src/ai/AIClient.js +220 -224
  18. package/lib/src/expression/ExpressionClient.d.ts +1 -1
  19. package/lib/src/expression/ExpressionClient.js +86 -73
  20. package/lib/src/index.d.ts +1 -1
  21. package/lib/src/index.js +1 -1
  22. package/lib/src/language/LanguageClient.d.ts +1 -1
  23. package/lib/src/language/LanguageClient.js +52 -52
  24. package/lib/src/model/Ad4mModel.d.ts +157 -937
  25. package/lib/src/model/Ad4mModel.js +212 -2799
  26. package/lib/src/model/Ad4mModel.test.js +1002 -561
  27. package/lib/src/model/decorators.d.ts +94 -426
  28. package/lib/src/model/decorators.js +333 -741
  29. package/lib/src/model/mutation.d.ts +81 -0
  30. package/lib/src/model/mutation.js +317 -0
  31. package/lib/src/model/parentUtils.d.ts +32 -0
  32. package/lib/src/model/parentUtils.js +64 -0
  33. package/lib/src/model/prolog/generatePrologFacts.d.ts +36 -0
  34. package/lib/src/model/prolog/generatePrologFacts.js +139 -0
  35. package/lib/src/model/prolog/index.d.ts +1 -0
  36. package/lib/src/model/prolog/index.js +1 -0
  37. package/lib/src/model/query/ModelQueryBuilder.d.ts +82 -0
  38. package/lib/src/model/query/ModelQueryBuilder.js +115 -0
  39. package/lib/src/model/query/QueryBuilder.d.ts +101 -0
  40. package/lib/src/model/query/QueryBuilder.js +193 -0
  41. package/lib/src/model/query/SurrealQueryBuilder.d.ts +48 -0
  42. package/lib/src/model/query/SurrealQueryBuilder.js +371 -0
  43. package/lib/src/model/query/fetchInstance.d.ts +24 -0
  44. package/lib/src/model/query/fetchInstance.js +144 -0
  45. package/lib/src/model/query/hydration.d.ts +54 -0
  46. package/lib/src/model/query/hydration.js +214 -0
  47. package/lib/src/model/query/operations.d.ts +59 -0
  48. package/lib/src/model/query/operations.js +384 -0
  49. package/lib/src/model/query/snapshot.d.ts +59 -0
  50. package/lib/src/model/query/snapshot.js +106 -0
  51. package/lib/src/model/query/surrealCompiler.d.ts +42 -0
  52. package/lib/src/model/query/surrealCompiler.js +304 -0
  53. package/lib/src/model/schema/fromJSONSchema.d.ts +67 -0
  54. package/lib/src/model/schema/fromJSONSchema.js +264 -0
  55. package/lib/src/model/schema/metadata.d.ts +15 -0
  56. package/lib/src/model/schema/metadata.js +112 -0
  57. package/lib/src/model/subscription.d.ts +47 -0
  58. package/lib/src/model/subscription.js +231 -0
  59. package/lib/src/model/transaction.d.ts +27 -0
  60. package/lib/src/model/transaction.js +32 -0
  61. package/lib/src/model/types.d.ts +195 -0
  62. package/lib/src/model/types.js +8 -0
  63. package/lib/src/model/util.js +7 -7
  64. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +5 -1
  65. package/lib/src/neighbourhood/NeighbourhoodClient.js +263 -181
  66. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +2 -1
  67. package/lib/src/neighbourhood/NeighbourhoodProxy.js +13 -28
  68. package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +1 -5
  69. package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
  70. package/lib/src/perspectives/PerspectiveClient.js +316 -240
  71. package/lib/src/perspectives/PerspectiveProxy.d.ts +54 -92
  72. package/lib/src/perspectives/PerspectiveProxy.js +803 -444
  73. package/lib/src/runtime/RuntimeClient.d.ts +5 -1
  74. package/lib/src/runtime/RuntimeClient.js +412 -238
  75. package/lib/src/shacl/SHACLFlow.test.js +164 -105
  76. package/lib/src/shacl/SHACLShape.d.ts +17 -3
  77. package/lib/src/shacl/SHACLShape.js +127 -90
  78. package/lib/src/shacl/SHACLShape.test.js +244 -176
  79. package/lib/src/utils.d.ts +8 -0
  80. package/lib/src/utils.js +21 -7
  81. package/package.json +8 -13
  82. package/src/Ad4mClient.test.ts +1564 -1315
  83. package/src/Ad4mClient.ts +55 -54
  84. package/src/Literal.test.ts +45 -26
  85. package/src/Literal.ts +82 -66
  86. package/src/SmartLiteral.ts +74 -58
  87. package/src/agent/AgentClient.ts +144 -99
  88. package/src/ai/AIClient.ts +409 -359
  89. package/src/expression/ExpressionClient.ts +142 -96
  90. package/src/index.ts +1 -1
  91. package/src/language/LanguageClient.ts +137 -113
  92. package/src/model/Ad4mModel.test.ts +1128 -647
  93. package/src/model/Ad4mModel.ts +485 -3249
  94. package/src/model/IMPROVEMENTS.md +482 -0
  95. package/src/model/PREDICATE_COLLISION_FIX.md +651 -0
  96. package/src/model/README.md +317 -0
  97. package/src/model/SHACL_MUTATION_DECOUPLING.md +356 -0
  98. package/src/model/SUBSCRIPTION_STRATEGY.md +317 -0
  99. package/src/model/TC39_STAGE_3_DECORATORS.md +609 -0
  100. package/src/model/decorators.ts +689 -1046
  101. package/src/model/mutation.ts +461 -0
  102. package/src/model/parentUtils.ts +91 -0
  103. package/src/model/prolog/generatePrologFacts.ts +176 -0
  104. package/src/model/prolog/index.ts +1 -0
  105. package/src/model/query/ModelQueryBuilder.ts +173 -0
  106. package/src/model/query/fetchInstance.ts +181 -0
  107. package/src/model/query/hydration.ts +273 -0
  108. package/src/model/query/operations.ts +510 -0
  109. package/src/model/query/snapshot.ts +126 -0
  110. package/src/model/query/surrealCompiler.ts +376 -0
  111. package/src/model/schema/fromJSONSchema.ts +411 -0
  112. package/src/model/schema/metadata.ts +148 -0
  113. package/src/model/subscription.ts +307 -0
  114. package/src/model/transaction.ts +60 -0
  115. package/src/model/types.ts +207 -0
  116. package/src/model/util.ts +47 -48
  117. package/src/neighbourhood/NeighbourhoodClient.ts +393 -251
  118. package/src/neighbourhood/NeighbourhoodProxy.test.ts +3 -7
  119. package/src/neighbourhood/NeighbourhoodProxy.ts +58 -40
  120. package/src/perspectives/PerspectiveClient.ts +779 -540
  121. package/src/perspectives/PerspectiveProxy.ts +2860 -2053
  122. package/src/runtime/RuntimeClient.ts +775 -476
  123. package/src/shacl/SHACLFlow.test.ts +196 -129
  124. package/src/shacl/SHACLShape.test.ts +284 -190
  125. package/src/shacl/SHACLShape.ts +225 -172
  126. package/src/utils.ts +31 -17
  127. package/tsconfig.json +9 -10
  128. package/src/model/Subject.ts +0 -159
@@ -1,16 +1,24 @@
1
1
  import { ApolloClient, gql } from "@apollo/client/core";
2
2
  import { ExpressionRendered } from "../expression/Expression";
3
3
  import { ExpressionClient } from "../expression/ExpressionClient";
4
- import { Link, LinkExpressionInput, LinkExpression, LinkInput, LinkMutations, LinkExpressionMutations } from "../links/Links";
4
+ import {
5
+ Link,
6
+ LinkExpressionInput,
7
+ LinkExpression,
8
+ LinkInput,
9
+ LinkMutations,
10
+ LinkExpressionMutations,
11
+ } from "../links/Links";
5
12
  import { NeighbourhoodClient } from "../neighbourhood/NeighbourhoodClient";
6
13
  import { NeighbourhoodProxy } from "../neighbourhood/NeighbourhoodProxy";
7
14
  import unwrapApolloResult from "../unwrapApolloResult";
8
15
  import { LinkQuery } from "./LinkQuery";
9
16
  import { Perspective } from "./Perspective";
10
17
  import { PerspectiveHandle, PerspectiveState } from "./PerspectiveHandle";
11
- import { LinkStatus, PerspectiveProxy } from './PerspectiveProxy';
18
+ import { LinkStatus, PerspectiveProxy } from "./PerspectiveProxy";
12
19
  import { AIClient } from "../ai/AIClient";
13
20
  import { AllInstancesResult } from "../model/Ad4mModel";
21
+ import { isSocketCloseError } from "../utils";
14
22
 
15
23
  const LINK_EXPRESSION_FIELDS = `
16
24
  author
@@ -18,7 +26,7 @@ timestamp
18
26
  status
19
27
  data { source, predicate, target }
20
28
  proof { valid, invalid, signature, key }
21
- `
29
+ `;
22
30
 
23
31
  const PERSPECTIVE_HANDLE_FIELDS = `
24
32
  uuid
@@ -41,343 +49,393 @@ neighbourhood {
41
49
  }
42
50
  author
43
51
  }
44
- `
52
+ `;
45
53
 
46
- export type PerspectiveHandleCallback = (perspective: PerspectiveHandle) => null
47
- export type UuidCallback = (uuid: string) => null
48
- export type LinkCallback = (link: LinkExpression) => null
49
- export type SyncStateChangeCallback = (state: PerspectiveState) => null
54
+ export type PerspectiveHandleCallback = (
55
+ perspective: PerspectiveHandle,
56
+ ) => null;
57
+ export type UuidCallback = (uuid: string) => null;
58
+ export type LinkCallback = (link: LinkExpression) => null;
59
+ export type SyncStateChangeCallback = (state: PerspectiveState) => null;
50
60
 
51
61
  export class PerspectiveClient {
52
- #apolloClient: ApolloClient<any>
53
- #perspectiveAddedCallbacks: PerspectiveHandleCallback[]
54
- #perspectiveUpdatedCallbacks: PerspectiveHandleCallback[]
55
- #perspectiveRemovedCallbacks: UuidCallback[]
56
- #perspectiveSyncStateChangeCallbacks: SyncStateChangeCallback[]
57
- #expressionClient?: ExpressionClient
58
- #neighbourhoodClient?: NeighbourhoodClient
59
- #aiClient?: AIClient
60
-
61
- constructor(client: ApolloClient<any>, subscribe: boolean = true) {
62
- this.#apolloClient = client
63
- this.#perspectiveAddedCallbacks = []
64
- this.#perspectiveUpdatedCallbacks = []
65
- this.#perspectiveRemovedCallbacks = []
66
- this.#perspectiveSyncStateChangeCallbacks = []
67
-
68
- if(subscribe) {
69
- this.subscribePerspectiveAdded()
70
- this.subscribePerspectiveUpdated()
71
- this.subscribePerspectiveRemoved()
72
- }
73
- }
74
-
75
- setExpressionClient(client: ExpressionClient) {
76
- this.#expressionClient = client
77
- }
78
-
79
- setNeighbourhoodClient(client: NeighbourhoodClient) {
80
- this.#neighbourhoodClient = client
81
- }
82
-
83
- setAIClient(client: AIClient) {
84
- this.#aiClient = client
85
- }
86
-
87
- get aiClient(): AIClient {
88
- return this.#aiClient
89
- }
90
-
91
- async all(): Promise<PerspectiveProxy[]> {
92
- const { perspectives } = unwrapApolloResult(await this.#apolloClient.query({
93
- query: gql`query perspectives {
62
+ private _apolloClient: ApolloClient<any>;
63
+ private _perspectiveAddedCallbacks: PerspectiveHandleCallback[];
64
+ private _perspectiveUpdatedCallbacks: PerspectiveHandleCallback[];
65
+ private _perspectiveRemovedCallbacks: UuidCallback[];
66
+ private _perspectiveSyncStateChangeCallbacks: SyncStateChangeCallback[];
67
+ private _expressionClient?: ExpressionClient;
68
+ private _neighbourhoodClient?: NeighbourhoodClient;
69
+ private _aiClient?: AIClient;
70
+
71
+ constructor(client: ApolloClient<any>, subscribe: boolean = true) {
72
+ this._apolloClient = client;
73
+ this._perspectiveAddedCallbacks = [];
74
+ this._perspectiveUpdatedCallbacks = [];
75
+ this._perspectiveRemovedCallbacks = [];
76
+ this._perspectiveSyncStateChangeCallbacks = [];
77
+
78
+ if (subscribe) {
79
+ this.subscribePerspectiveAdded();
80
+ this.subscribePerspectiveUpdated();
81
+ this.subscribePerspectiveRemoved();
82
+ }
83
+ }
84
+
85
+ setExpressionClient(client: ExpressionClient) {
86
+ this._expressionClient = client;
87
+ }
88
+
89
+ setNeighbourhoodClient(client: NeighbourhoodClient) {
90
+ this._neighbourhoodClient = client;
91
+ }
92
+
93
+ setAIClient(client: AIClient) {
94
+ this._aiClient = client;
95
+ }
96
+
97
+ get aiClient(): AIClient {
98
+ return this._aiClient;
99
+ }
100
+
101
+ async all(): Promise<PerspectiveProxy[]> {
102
+ const { perspectives } = unwrapApolloResult(
103
+ await this._apolloClient.query({
104
+ query: gql`query perspectives {
94
105
  perspectives {
95
106
  ${PERSPECTIVE_HANDLE_FIELDS}
96
107
  }
97
- }`
98
- }))
99
- return perspectives.map(handle => new PerspectiveProxy(handle, this))
100
- }
101
-
102
- async byUUID(uuid: string): Promise<PerspectiveProxy|null> {
103
- const { perspective } = unwrapApolloResult(await this.#apolloClient.query({
104
- query: gql`query perspective($uuid: String!) {
108
+ }`,
109
+ }),
110
+ );
111
+ return perspectives.map((handle) => new PerspectiveProxy(handle, this));
112
+ }
113
+
114
+ async byUUID(uuid: string): Promise<PerspectiveProxy | null> {
115
+ const { perspective } = unwrapApolloResult(
116
+ await this._apolloClient.query({
117
+ query: gql`query perspective($uuid: String!) {
105
118
  perspective(uuid: $uuid) {
106
119
  ${PERSPECTIVE_HANDLE_FIELDS}
107
120
  }
108
121
  }`,
109
- variables: { uuid }
110
- }))
111
- if(!perspective) return null
112
- return new PerspectiveProxy(perspective, this)
113
- }
114
-
115
- async snapshotByUUID(uuid: string): Promise<Perspective|null> {
116
- const { perspectiveSnapshot } = unwrapApolloResult(await this.#apolloClient.query({
117
- query: gql`query perspectiveSnapshot($uuid: String!) {
122
+ variables: { uuid },
123
+ }),
124
+ );
125
+ if (!perspective) return null;
126
+ return new PerspectiveProxy(perspective, this);
127
+ }
128
+
129
+ async snapshotByUUID(uuid: string): Promise<Perspective | null> {
130
+ const { perspectiveSnapshot } = unwrapApolloResult(
131
+ await this._apolloClient.query({
132
+ query: gql`query perspectiveSnapshot($uuid: String!) {
118
133
  perspectiveSnapshot(uuid: $uuid) {
119
134
  links { ${LINK_EXPRESSION_FIELDS} }
120
135
  }
121
136
  }`,
122
- variables: { uuid }
123
- }))
124
- return perspectiveSnapshot
125
- }
126
- async publishSnapshotByUUID(uuid: string): Promise<string|null> {
127
- const { perspectivePublishSnapshot } = unwrapApolloResult(await this.#apolloClient.mutate({
128
- mutation: gql`mutation perspectivePublishSnapshot($uuid: String!) {
129
- perspectivePublishSnapshot(uuid: $uuid)
130
- }`,
131
- variables: { uuid }
132
- }))
133
- return perspectivePublishSnapshot
134
- }
135
-
136
- async queryLinks(uuid: string, query: LinkQuery): Promise<LinkExpression[]> {
137
- const { perspectiveQueryLinks } = unwrapApolloResult(await this.#apolloClient.query({
138
- query: gql`query perspectiveQueryLinks($uuid: String!, $query: LinkQuery!) {
137
+ variables: { uuid },
138
+ }),
139
+ );
140
+ return perspectiveSnapshot;
141
+ }
142
+ async publishSnapshotByUUID(uuid: string): Promise<string | null> {
143
+ const { perspectivePublishSnapshot } = unwrapApolloResult(
144
+ await this._apolloClient.mutate({
145
+ mutation: gql`
146
+ mutation perspectivePublishSnapshot($uuid: String!) {
147
+ perspectivePublishSnapshot(uuid: $uuid)
148
+ }
149
+ `,
150
+ variables: { uuid },
151
+ }),
152
+ );
153
+ return perspectivePublishSnapshot;
154
+ }
155
+
156
+ async queryLinks(uuid: string, query: LinkQuery): Promise<LinkExpression[]> {
157
+ const { perspectiveQueryLinks } = unwrapApolloResult(
158
+ await this._apolloClient.query({
159
+ query: gql`query perspectiveQueryLinks($uuid: String!, $query: LinkQuery!) {
139
160
  perspectiveQueryLinks(query: $query, uuid: $uuid) {
140
161
  ${LINK_EXPRESSION_FIELDS}
141
162
  }
142
163
  }`,
143
- variables: { uuid, query }
144
- }))
145
- return perspectiveQueryLinks
146
- }
147
-
148
- async queryProlog(uuid: string, query: string): Promise<any> {
149
- const { perspectiveQueryProlog } = unwrapApolloResult(await this.#apolloClient.query({
150
- query: gql`query perspectiveQueryProlog($uuid: String!, $query: String!) {
151
- perspectiveQueryProlog(uuid: $uuid, query: $query)
152
- }`,
153
- variables: { uuid, query }
154
- }))
155
-
156
- return JSON.parse(perspectiveQueryProlog)
157
- }
158
-
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)
172
- }`,
173
- variables: { uuid, query }
174
- }))
175
-
176
- return JSON.parse(perspectiveQuerySurrealDb)
177
- }
178
-
179
- async subscribeQuery(uuid: string, query: string): Promise<{ subscriptionId: string, result: AllInstancesResult, isInit?: boolean }> {
180
- const { perspectiveSubscribeQuery } = unwrapApolloResult(await this.#apolloClient.mutate({
181
- mutation: gql`mutation perspectiveSubscribeQuery($uuid: String!, $query: String!) {
182
- perspectiveSubscribeQuery(uuid: $uuid, query: $query) {
183
- subscriptionId
184
- result
185
- }
186
- }`,
187
- variables: { uuid, query }
188
- }))
189
- const { subscriptionId, result } = perspectiveSubscribeQuery
190
- let finalResult = result;
191
- let isInit = false;
192
- if(finalResult.startsWith("#init#")) {
193
- finalResult = finalResult.substring(6)
194
- isInit = true;
195
- }
196
- try {
197
- finalResult = JSON.parse(finalResult)
198
- } catch (e) {
199
- console.error('Error parsing perspectiveSubscribeQuery result:', e)
200
- }
201
- return { subscriptionId, result: finalResult, isInit }
202
- }
203
-
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
- subscribeToQueryUpdates(subscriptionId: string, onData: (result: AllInstancesResult) => void): () => void {
252
- const subscription = this.#apolloClient.subscribe({
253
- query: gql`
254
- subscription perspectiveQuerySubscription($subscriptionId: String!) {
255
- perspectiveQuerySubscription(subscriptionId: $subscriptionId)
256
- }
257
- `,
258
- variables: {
259
- subscriptionId
164
+ variables: { uuid, query },
165
+ }),
166
+ );
167
+ return perspectiveQueryLinks;
168
+ }
169
+
170
+ async queryProlog(uuid: string, query: string): Promise<any> {
171
+ const { perspectiveQueryProlog } = unwrapApolloResult(
172
+ await this._apolloClient.query({
173
+ query: gql`
174
+ query perspectiveQueryProlog($uuid: String!, $query: String!) {
175
+ perspectiveQueryProlog(uuid: $uuid, query: $query)
176
+ }
177
+ `,
178
+ variables: { uuid, query },
179
+ }),
180
+ );
181
+
182
+ return JSON.parse(perspectiveQueryProlog);
183
+ }
184
+
185
+ /**
186
+ * Executes a read-only SurrealQL query against a perspective's link cache.
187
+ *
188
+ * Security: Only SELECT, RETURN, and other read-only queries are permitted.
189
+ * Mutating operations (DELETE, UPDATE, INSERT, etc.) are blocked.
190
+ *
191
+ * Note: GraphQL field name is "perspectiveQuerySurrealDb" (lowercase "b" in "Db")
192
+ * as generated from Rust method "perspective_query_surreal_db"
193
+ */
194
+ async querySurrealDB(uuid: string, query: string): Promise<any> {
195
+ const { perspectiveQuerySurrealDb } = unwrapApolloResult(
196
+ await this._apolloClient.query({
197
+ query: gql`
198
+ query perspectiveQuerySurrealDb($uuid: String!, $query: String!) {
199
+ perspectiveQuerySurrealDb(uuid: $uuid, query: $query)
200
+ }
201
+ `,
202
+ variables: { uuid, query },
203
+ }),
204
+ );
205
+
206
+ return JSON.parse(perspectiveQuerySurrealDb);
207
+ }
208
+
209
+ async subscribeQuery(
210
+ uuid: string,
211
+ query: string,
212
+ ): Promise<{
213
+ subscriptionId: string;
214
+ result: AllInstancesResult;
215
+ isInit?: boolean;
216
+ }> {
217
+ const { perspectiveSubscribeQuery } = unwrapApolloResult(
218
+ await this._apolloClient.mutate({
219
+ mutation: gql`
220
+ mutation perspectiveSubscribeQuery($uuid: String!, $query: String!) {
221
+ perspectiveSubscribeQuery(uuid: $uuid, query: $query) {
222
+ subscriptionId
223
+ result
260
224
  }
261
- }).subscribe({
262
- next: (result) => {
263
- if (result.data && result.data.perspectiveQuerySubscription) {
264
- let finalResult = result.data.perspectiveQuerySubscription;
265
- let isInit = false;
266
- if(finalResult.startsWith("#init#")) {
267
- finalResult = finalResult.substring(6)
268
- isInit = true;
269
- }
270
- try {
271
- finalResult = JSON.parse(finalResult)
272
- if(isInit && typeof finalResult === 'object') {
273
- finalResult.isInit = true;
274
- }
275
- } catch (e) {
276
- console.error('Error parsing perspectiveQuerySubscription:', e)
277
- }
278
- onData(finalResult);
279
- }
280
- },
281
- error: (e) => console.error('Error in query subscription:', e)
282
- });
283
-
284
- return () => subscription.unsubscribe();
285
- }
286
-
287
- async keepAliveQuery(uuid: string, subscriptionId: string): Promise<boolean> {
288
- const { perspectiveKeepAliveQuery } = unwrapApolloResult(await this.#apolloClient.mutate({
289
- mutation: gql`mutation perspectiveKeepAliveQuery($uuid: String!, $subscriptionId: String!) {
290
- perspectiveKeepAliveQuery(uuid: $uuid, subscriptionId: $subscriptionId)
291
- }`,
292
- variables: { uuid, subscriptionId }
293
- }))
294
-
295
- return perspectiveKeepAliveQuery
296
- }
297
-
298
- async disposeQuerySubscription(uuid: string, subscriptionId: string): Promise<boolean> {
299
- const { perspectiveDisposeQuerySubscription } = unwrapApolloResult(await this.#apolloClient.mutate({
300
- mutation: gql`mutation perspectiveDisposeQuerySubscription($uuid: String!, $subscriptionId: String!) {
301
- perspectiveDisposeQuerySubscription(uuid: $uuid, subscriptionId: $subscriptionId)
302
- }`,
303
- variables: { uuid, subscriptionId }
304
- }))
305
-
306
- return perspectiveDisposeQuerySubscription
307
- }
308
-
309
- async add(name: string): Promise<PerspectiveProxy> {
310
- const { perspectiveAdd } = unwrapApolloResult(await this.#apolloClient.mutate({
311
- mutation: gql`mutation perspectiveAdd($name: String!) {
225
+ }
226
+ `,
227
+ variables: { uuid, query },
228
+ }),
229
+ );
230
+ const { subscriptionId, result } = perspectiveSubscribeQuery;
231
+ let finalResult = result;
232
+ let isInit = false;
233
+ if (finalResult.startsWith("#init#")) {
234
+ finalResult = finalResult.substring(6);
235
+ isInit = true;
236
+ }
237
+ try {
238
+ finalResult = JSON.parse(finalResult);
239
+ } catch (e) {
240
+ console.error("Error parsing perspectiveSubscribeQuery result:", e);
241
+ }
242
+ return { subscriptionId, result: finalResult, isInit };
243
+ }
244
+
245
+ subscribeToQueryUpdates(
246
+ subscriptionId: string,
247
+ onData: (result: AllInstancesResult) => void,
248
+ ): () => void {
249
+ const subscription = this._apolloClient
250
+ .subscribe({
251
+ query: gql`
252
+ subscription perspectiveQuerySubscription($subscriptionId: String!) {
253
+ perspectiveQuerySubscription(subscriptionId: $subscriptionId)
254
+ }
255
+ `,
256
+ variables: {
257
+ subscriptionId,
258
+ },
259
+ })
260
+ .subscribe({
261
+ next: (result) => {
262
+ if (result.data && result.data.perspectiveQuerySubscription) {
263
+ let finalResult = result.data.perspectiveQuerySubscription;
264
+ let isInit = false;
265
+ if (finalResult.startsWith("#init#")) {
266
+ finalResult = finalResult.substring(6);
267
+ isInit = true;
268
+ }
269
+ try {
270
+ finalResult = JSON.parse(finalResult);
271
+ if (isInit && typeof finalResult === "object") {
272
+ finalResult.isInit = true;
273
+ }
274
+ } catch (e) {
275
+ console.error("Error parsing perspectiveQuerySubscription:", e);
276
+ }
277
+ onData(finalResult);
278
+ }
279
+ },
280
+ error: (e) => {
281
+ if (!isSocketCloseError(e))
282
+ console.error("Error in query subscription:", e);
283
+ },
284
+ });
285
+
286
+ return () => subscription.unsubscribe();
287
+ }
288
+
289
+ async keepAliveQuery(uuid: string, subscriptionId: string): Promise<boolean> {
290
+ const { perspectiveKeepAliveQuery } = unwrapApolloResult(
291
+ await this._apolloClient.mutate({
292
+ mutation: gql`
293
+ mutation perspectiveKeepAliveQuery(
294
+ $uuid: String!
295
+ $subscriptionId: String!
296
+ ) {
297
+ perspectiveKeepAliveQuery(
298
+ uuid: $uuid
299
+ subscriptionId: $subscriptionId
300
+ )
301
+ }
302
+ `,
303
+ variables: { uuid, subscriptionId },
304
+ }),
305
+ );
306
+
307
+ return perspectiveKeepAliveQuery;
308
+ }
309
+
310
+ async disposeQuerySubscription(
311
+ uuid: string,
312
+ subscriptionId: string,
313
+ ): Promise<boolean> {
314
+ const { perspectiveDisposeQuerySubscription } = unwrapApolloResult(
315
+ await this._apolloClient.mutate({
316
+ mutation: gql`
317
+ mutation perspectiveDisposeQuerySubscription(
318
+ $uuid: String!
319
+ $subscriptionId: String!
320
+ ) {
321
+ perspectiveDisposeQuerySubscription(
322
+ uuid: $uuid
323
+ subscriptionId: $subscriptionId
324
+ )
325
+ }
326
+ `,
327
+ variables: { uuid, subscriptionId },
328
+ }),
329
+ );
330
+
331
+ return perspectiveDisposeQuerySubscription;
332
+ }
333
+
334
+ async add(name: string): Promise<PerspectiveProxy> {
335
+ const { perspectiveAdd } = unwrapApolloResult(
336
+ await this._apolloClient.mutate({
337
+ mutation: gql`mutation perspectiveAdd($name: String!) {
312
338
  perspectiveAdd(name: $name) {
313
339
  ${PERSPECTIVE_HANDLE_FIELDS}
314
340
  }
315
341
  }`,
316
- variables: { name }
317
- }))
318
- return new PerspectiveProxy(perspectiveAdd, this)
319
- }
320
-
321
- async update(uuid: string, name: string): Promise<PerspectiveProxy> {
322
- const { perspectiveUpdate } = unwrapApolloResult(await this.#apolloClient.mutate({
323
- mutation: gql`mutation perspectiveUpdate($uuid: String!, $name: String!) {
342
+ variables: { name },
343
+ }),
344
+ );
345
+ return new PerspectiveProxy(perspectiveAdd, this);
346
+ }
347
+
348
+ async update(uuid: string, name: string): Promise<PerspectiveProxy> {
349
+ const { perspectiveUpdate } = unwrapApolloResult(
350
+ await this._apolloClient.mutate({
351
+ mutation: gql`mutation perspectiveUpdate($uuid: String!, $name: String!) {
324
352
  perspectiveUpdate(uuid: $uuid, name: $name) {
325
353
  ${PERSPECTIVE_HANDLE_FIELDS}
326
354
  }
327
355
  }`,
328
- variables: { uuid, name }
329
- }))
330
- return new PerspectiveProxy(perspectiveUpdate, this)
331
- }
332
-
333
- async remove(uuid: string): Promise<{perspectiveRemove: boolean}> {
334
- return unwrapApolloResult(await this.#apolloClient.mutate({
335
- mutation: gql`mutation perspectiveRemove($uuid: String!) {
336
- perspectiveRemove(uuid: $uuid)
337
- }`,
338
- variables: { uuid }
339
- }))
340
- }
341
-
342
- async addLink(uuid: string, link: Link, status: LinkStatus = 'shared', batchId?: string): Promise<LinkExpression> {
343
- const { perspectiveAddLink } = unwrapApolloResult(await this.#apolloClient.mutate({
344
- mutation: gql`mutation perspectiveAddLink($uuid: String!, $link: LinkInput!, $status: String!, $batchId: String) {
356
+ variables: { uuid, name },
357
+ }),
358
+ );
359
+ return new PerspectiveProxy(perspectiveUpdate, this);
360
+ }
361
+
362
+ async remove(uuid: string): Promise<{ perspectiveRemove: boolean }> {
363
+ return unwrapApolloResult(
364
+ await this._apolloClient.mutate({
365
+ mutation: gql`
366
+ mutation perspectiveRemove($uuid: String!) {
367
+ perspectiveRemove(uuid: $uuid)
368
+ }
369
+ `,
370
+ variables: { uuid },
371
+ }),
372
+ );
373
+ }
374
+
375
+ async addLink(
376
+ uuid: string,
377
+ link: Link,
378
+ status: LinkStatus = "shared",
379
+ batchId?: string,
380
+ ): Promise<LinkExpression> {
381
+ const { perspectiveAddLink } = unwrapApolloResult(
382
+ await this._apolloClient.mutate({
383
+ mutation: gql`mutation perspectiveAddLink($uuid: String!, $link: LinkInput!, $status: String!, $batchId: String) {
345
384
  perspectiveAddLink(uuid: $uuid, link: $link, status: $status, batchId: $batchId) {
346
385
  ${LINK_EXPRESSION_FIELDS}
347
386
  }
348
387
  }`,
349
- variables: { uuid, link, status, batchId }
350
- }))
351
- return perspectiveAddLink
352
- }
353
-
354
- async addLinks(uuid: string, links: Link[], status: LinkStatus = 'shared', batchId?: string): Promise<LinkExpression[]> {
355
- const { perspectiveAddLinks } = unwrapApolloResult(await this.#apolloClient.mutate({
356
- mutation: gql`mutation perspectiveAddLinks($uuid: String!, $links: [LinkInput!]!, $status: String!, $batchId: String) {
388
+ variables: { uuid, link, status, batchId },
389
+ }),
390
+ );
391
+ return perspectiveAddLink;
392
+ }
393
+
394
+ async addLinks(
395
+ uuid: string,
396
+ links: Link[],
397
+ status: LinkStatus = "shared",
398
+ batchId?: string,
399
+ ): Promise<LinkExpression[]> {
400
+ const { perspectiveAddLinks } = unwrapApolloResult(
401
+ await this._apolloClient.mutate({
402
+ mutation: gql`mutation perspectiveAddLinks($uuid: String!, $links: [LinkInput!]!, $status: String!, $batchId: String) {
357
403
  perspectiveAddLinks(uuid: $uuid, links: $links, status: $status, batchId: $batchId) {
358
404
  ${LINK_EXPRESSION_FIELDS}
359
405
  }
360
406
  }`,
361
- variables: { uuid, links, status, batchId }
362
- }))
363
- return perspectiveAddLinks
364
- }
365
-
366
- async removeLinks(uuid: string, links: LinkExpressionInput[], batchId?: string): Promise<LinkExpression[]> {
367
- const { perspectiveRemoveLinks } = unwrapApolloResult(await this.#apolloClient.mutate({
368
- mutation: gql`mutation perspectiveRemoveLinks($uuid: String!, $links: [LinkExpressionInput!]!, $batchId: String) {
407
+ variables: { uuid, links, status, batchId },
408
+ }),
409
+ );
410
+ return perspectiveAddLinks;
411
+ }
412
+
413
+ async removeLinks(
414
+ uuid: string,
415
+ links: LinkExpressionInput[],
416
+ batchId?: string,
417
+ ): Promise<LinkExpression[]> {
418
+ const { perspectiveRemoveLinks } = unwrapApolloResult(
419
+ await this._apolloClient.mutate({
420
+ mutation: gql`mutation perspectiveRemoveLinks($uuid: String!, $links: [LinkExpressionInput!]!, $batchId: String) {
369
421
  perspectiveRemoveLinks(uuid: $uuid, links: $links, batchId: $batchId) {
370
422
  ${LINK_EXPRESSION_FIELDS}
371
423
  }
372
424
  }`,
373
- variables: { uuid, links, batchId }
374
- }))
375
- return perspectiveRemoveLinks
376
- }
377
-
378
- async linkMutations(uuid: string, mutations: LinkMutations, status?: LinkStatus): Promise<LinkExpressionMutations> {
379
- const { perspectiveLinkMutations } = unwrapApolloResult(await this.#apolloClient.mutate({
380
- mutation: gql`mutation perspectiveLinkMutations($uuid: String!, $mutations: LinkMutations!, $status: String){
425
+ variables: { uuid, links, batchId },
426
+ }),
427
+ );
428
+ return perspectiveRemoveLinks;
429
+ }
430
+
431
+ async linkMutations(
432
+ uuid: string,
433
+ mutations: LinkMutations,
434
+ status?: LinkStatus,
435
+ ): Promise<LinkExpressionMutations> {
436
+ const { perspectiveLinkMutations } = unwrapApolloResult(
437
+ await this._apolloClient.mutate({
438
+ mutation: gql`mutation perspectiveLinkMutations($uuid: String!, $mutations: LinkMutations!, $status: String){
381
439
  perspectiveLinkMutations(mutations: $mutations, uuid: $uuid, status: $status) {
382
440
  additions {
383
441
  ${LINK_EXPRESSION_FIELDS}
@@ -387,223 +445,391 @@ export class PerspectiveClient {
387
445
  }
388
446
  }
389
447
  }`,
390
- variables: { uuid, mutations, status }
391
- }))
392
- return perspectiveLinkMutations
393
- }
394
-
395
- async addLinkExpression(uuid: string, link: LinkExpression, status: LinkStatus = 'shared', batchId?: string): Promise<LinkExpression> {
396
- const { perspectiveAddLinkExpression } = unwrapApolloResult(await this.#apolloClient.mutate({
397
- mutation: gql`mutation perspectiveAddLinkExpression($uuid: String!, $link: LinkExpressionInput!, $status: String!, $batchId: String) {
448
+ variables: { uuid, mutations, status },
449
+ }),
450
+ );
451
+ return perspectiveLinkMutations;
452
+ }
453
+
454
+ async addLinkExpression(
455
+ uuid: string,
456
+ link: LinkExpression,
457
+ status: LinkStatus = "shared",
458
+ batchId?: string,
459
+ ): Promise<LinkExpression> {
460
+ const { perspectiveAddLinkExpression } = unwrapApolloResult(
461
+ await this._apolloClient.mutate({
462
+ mutation: gql`mutation perspectiveAddLinkExpression($uuid: String!, $link: LinkExpressionInput!, $status: String!, $batchId: String) {
398
463
  perspectiveAddLinkExpression(uuid: $uuid, link: $link, status: $status, batchId: $batchId) {
399
464
  ${LINK_EXPRESSION_FIELDS}
400
465
  }
401
466
  }`,
402
- variables: { uuid, link, status, batchId }
403
- }))
404
- return perspectiveAddLinkExpression
405
- }
406
-
407
- async updateLink(uuid: string, oldLink: LinkExpressionInput, newLink: Link, batchId?: string): Promise<LinkExpression> {
408
- const { perspectiveUpdateLink } = unwrapApolloResult(await this.#apolloClient.mutate({
409
- mutation: gql`mutation perspectiveUpdateLink($uuid: String!, $oldLink: LinkExpressionInput!, $newLink: LinkInput!, $batchId: String) {
467
+ variables: { uuid, link, status, batchId },
468
+ }),
469
+ );
470
+ return perspectiveAddLinkExpression;
471
+ }
472
+
473
+ async updateLink(
474
+ uuid: string,
475
+ oldLink: LinkExpressionInput,
476
+ newLink: Link,
477
+ batchId?: string,
478
+ ): Promise<LinkExpression> {
479
+ const { perspectiveUpdateLink } = unwrapApolloResult(
480
+ await this._apolloClient.mutate({
481
+ mutation: gql`mutation perspectiveUpdateLink($uuid: String!, $oldLink: LinkExpressionInput!, $newLink: LinkInput!, $batchId: String) {
410
482
  perspectiveUpdateLink(uuid: $uuid, oldLink: $oldLink, newLink: $newLink, batchId: $batchId) {
411
483
  ${LINK_EXPRESSION_FIELDS}
412
484
  }
413
485
  }`,
414
- variables: { uuid, oldLink, newLink, batchId }
415
- }))
416
- return perspectiveUpdateLink
417
- }
418
-
419
- async removeLink(uuid: string, link: LinkExpressionInput, batchId?: string): Promise<boolean> {
420
- delete link.__typename
421
- delete link.data.__typename
422
- delete link.proof.__typename
423
- delete link.status
424
- const { perspectiveRemoveLink } = unwrapApolloResult(await this.#apolloClient.mutate({
425
- mutation: gql`mutation perspectiveRemoveLink($link: LinkExpressionInput!, $uuid: String!, $batchId: String) {
426
- perspectiveRemoveLink(link: $link, uuid: $uuid, batchId: $batchId)
427
- }`,
428
- variables: { uuid, link, batchId }
429
- }))
430
- return perspectiveRemoveLink
431
- }
432
-
433
- /**
434
- * Adds Social DNA code to a perspective.
435
- *
436
- * Preferred usage: pass shaclJson (from SHACLShape.toJSON()) as the primary schema definition.
437
- * The sdnaCode parameter is kept for backward compatibility but SHACL is the source of truth
438
- * for all SDNA operations. Prolog engines remain available for complex queries.
439
- *
440
- * @param sdnaCode - Legacy Prolog code (pass empty string when using shaclJson)
441
- * @param shaclJson - SHACL JSON string from SHACLShape.toJSON() (recommended)
442
- */
443
- async addSdna(uuid: string, name: string, sdnaCode: string | undefined, sdnaType: "subject_class" | "flow" | "custom", shaclJson?: string): Promise<boolean> {
444
- return unwrapApolloResult(await this.#apolloClient.mutate({
445
- mutation: gql`mutation perspectiveAddSdna($uuid: String!, $name: String!, $sdnaCode: String, $sdnaType: String!, $shaclJson: String) {
446
- perspectiveAddSdna(uuid: $uuid, name: $name, sdnaCode: $sdnaCode, sdnaType: $sdnaType, shaclJson: $shaclJson)
447
- }`,
448
- variables: { uuid, name, sdnaCode: sdnaCode || "", sdnaType, shaclJson }
449
- })).perspectiveAddSdna
450
- }
451
-
452
- async executeCommands(uuid: string, commands: string, expression: string, parameters: string, batchId?: string): Promise<boolean> {
453
- return unwrapApolloResult(await this.#apolloClient.mutate({
454
- mutation: gql`mutation perspectiveExecuteCommands($uuid: String!, $commands: String!, $expression: String!, $parameters: String, $batchId: String) {
455
- perspectiveExecuteCommands(uuid: $uuid, commands: $commands, expression: $expression, parameters: $parameters, batchId: $batchId)
456
- }`,
457
- variables: { uuid, commands, expression, parameters, batchId }
458
- })).perspectiveExecuteCommands
459
- }
460
-
461
- async createSubject(uuid: string, subjectClass: string, expressionAddress: string, initialValues?: string, batchId?: string): Promise<boolean> {
462
- return unwrapApolloResult(await this.#apolloClient.mutate({
463
- mutation: gql`mutation perspectiveCreateSubject($uuid: String!, $subjectClass: String!, $expressionAddress: String!, $initialValues: String, $batchId: String) {
464
- perspectiveCreateSubject(uuid: $uuid, subjectClass: $subjectClass, expressionAddress: $expressionAddress, initialValues: $initialValues, batchId: $batchId)
465
- }`,
466
- variables: { uuid, subjectClass, expressionAddress, initialValues, batchId }
467
- })).perspectiveCreateSubject
468
- }
469
-
470
- async getSubjectData(uuid: string, subjectClass: string, expressionAddress: string): Promise<string> {
471
- return unwrapApolloResult(await this.#apolloClient.mutate({
472
- mutation: gql`mutation perspectiveGetSubjectData($uuid: String!, $subjectClass: String!, $expressionAddress: String!) {
473
- perspectiveGetSubjectData(uuid: $uuid, subjectClass: $subjectClass, expressionAddress: $expressionAddress)
474
- }`,
475
- variables: { uuid, subjectClass, expressionAddress }
476
- })).perspectiveGetSubjectData
477
- }
478
-
479
- // ExpressionClient functions, needed for Subjects:
480
- async getExpression(expressionURI: string): Promise<ExpressionRendered> {
481
- return await this.#expressionClient.get(expressionURI)
482
- }
483
-
484
- async createExpression(content: any, languageAddress: string): Promise<string> {
485
- return await this.#expressionClient.create(content, languageAddress)
486
- }
487
-
488
- // Subscriptions:
489
- addPerspectiveAddedListener(cb: PerspectiveHandleCallback) {
490
- this.#perspectiveAddedCallbacks.push(cb)
491
- }
492
-
493
- subscribePerspectiveAdded() {
494
- this.#apolloClient.subscribe({
495
- query: gql` subscription {
486
+ variables: { uuid, oldLink, newLink, batchId },
487
+ }),
488
+ );
489
+ return perspectiveUpdateLink;
490
+ }
491
+
492
+ async removeLink(
493
+ uuid: string,
494
+ link: LinkExpressionInput,
495
+ batchId?: string,
496
+ ): Promise<boolean> {
497
+ delete link.__typename;
498
+ delete link.data.__typename;
499
+ delete link.proof.__typename;
500
+ delete link.status;
501
+ const { perspectiveRemoveLink } = unwrapApolloResult(
502
+ await this._apolloClient.mutate({
503
+ mutation: gql`
504
+ mutation perspectiveRemoveLink(
505
+ $link: LinkExpressionInput!
506
+ $uuid: String!
507
+ $batchId: String
508
+ ) {
509
+ perspectiveRemoveLink(link: $link, uuid: $uuid, batchId: $batchId)
510
+ }
511
+ `,
512
+ variables: { uuid, link, batchId },
513
+ }),
514
+ );
515
+ return perspectiveRemoveLink;
516
+ }
517
+
518
+ /**
519
+ * Adds Social DNA code to a perspective.
520
+ *
521
+ * Preferred usage: pass shaclJson (from SHACLShape.toJSON()) as the primary schema definition.
522
+ * The sdnaCode parameter is kept for backward compatibility but SHACL is the source of truth
523
+ * for all SDNA operations. Prolog engines remain available for complex queries.
524
+ *
525
+ * @param sdnaCode - Legacy Prolog code (pass empty string when using shaclJson)
526
+ * @param shaclJson - SHACL JSON string from SHACLShape.toJSON() (recommended)
527
+ */
528
+ async addSdna(
529
+ uuid: string,
530
+ name: string,
531
+ sdnaCode: string | undefined,
532
+ sdnaType: "subject_class" | "flow" | "custom",
533
+ shaclJson?: string,
534
+ ): Promise<boolean> {
535
+ return unwrapApolloResult(
536
+ await this._apolloClient.mutate({
537
+ mutation: gql`
538
+ mutation perspectiveAddSdna(
539
+ $uuid: String!
540
+ $name: String!
541
+ $sdnaCode: String
542
+ $sdnaType: String!
543
+ $shaclJson: String
544
+ ) {
545
+ perspectiveAddSdna(
546
+ uuid: $uuid
547
+ name: $name
548
+ sdnaCode: $sdnaCode
549
+ sdnaType: $sdnaType
550
+ shaclJson: $shaclJson
551
+ )
552
+ }
553
+ `,
554
+ variables: {
555
+ uuid,
556
+ name,
557
+ sdnaCode: sdnaCode || "",
558
+ sdnaType,
559
+ shaclJson,
560
+ },
561
+ }),
562
+ ).perspectiveAddSdna;
563
+ }
564
+
565
+ async executeCommands(
566
+ uuid: string,
567
+ commands: string,
568
+ expression: string,
569
+ parameters: string,
570
+ batchId?: string,
571
+ ): Promise<boolean> {
572
+ return unwrapApolloResult(
573
+ await this._apolloClient.mutate({
574
+ mutation: gql`
575
+ mutation perspectiveExecuteCommands(
576
+ $uuid: String!
577
+ $commands: String!
578
+ $expression: String!
579
+ $parameters: String
580
+ $batchId: String
581
+ ) {
582
+ perspectiveExecuteCommands(
583
+ uuid: $uuid
584
+ commands: $commands
585
+ expression: $expression
586
+ parameters: $parameters
587
+ batchId: $batchId
588
+ )
589
+ }
590
+ `,
591
+ variables: { uuid, commands, expression, parameters, batchId },
592
+ }),
593
+ ).perspectiveExecuteCommands;
594
+ }
595
+
596
+ async createSubject(
597
+ uuid: string,
598
+ subjectClass: string,
599
+ expressionAddress: string,
600
+ initialValues?: string,
601
+ batchId?: string,
602
+ ): Promise<boolean> {
603
+ return unwrapApolloResult(
604
+ await this._apolloClient.mutate({
605
+ mutation: gql`
606
+ mutation perspectiveCreateSubject(
607
+ $uuid: String!
608
+ $subjectClass: String!
609
+ $expressionAddress: String!
610
+ $initialValues: String
611
+ $batchId: String
612
+ ) {
613
+ perspectiveCreateSubject(
614
+ uuid: $uuid
615
+ subjectClass: $subjectClass
616
+ expressionAddress: $expressionAddress
617
+ initialValues: $initialValues
618
+ batchId: $batchId
619
+ )
620
+ }
621
+ `,
622
+ variables: {
623
+ uuid,
624
+ subjectClass,
625
+ expressionAddress,
626
+ initialValues,
627
+ batchId,
628
+ },
629
+ }),
630
+ ).perspectiveCreateSubject;
631
+ }
632
+
633
+ async getSubjectData(
634
+ uuid: string,
635
+ subjectClass: string,
636
+ expressionAddress: string,
637
+ ): Promise<string> {
638
+ return unwrapApolloResult(
639
+ await this._apolloClient.mutate({
640
+ mutation: gql`
641
+ mutation perspectiveGetSubjectData(
642
+ $uuid: String!
643
+ $subjectClass: String!
644
+ $expressionAddress: String!
645
+ ) {
646
+ perspectiveGetSubjectData(
647
+ uuid: $uuid
648
+ subjectClass: $subjectClass
649
+ expressionAddress: $expressionAddress
650
+ )
651
+ }
652
+ `,
653
+ variables: { uuid, subjectClass, expressionAddress },
654
+ }),
655
+ ).perspectiveGetSubjectData;
656
+ }
657
+
658
+ // ExpressionClient functions, needed for Subjects:
659
+ async getExpression(expressionURI: string): Promise<ExpressionRendered> {
660
+ return await this._expressionClient.get(expressionURI);
661
+ }
662
+
663
+ async createExpression(
664
+ content: any,
665
+ languageAddress: string,
666
+ ): Promise<string> {
667
+ return await this._expressionClient.create(content, languageAddress);
668
+ }
669
+
670
+ // Subscriptions:
671
+ addPerspectiveAddedListener(cb: PerspectiveHandleCallback) {
672
+ this._perspectiveAddedCallbacks.push(cb);
673
+ }
674
+
675
+ subscribePerspectiveAdded() {
676
+ this._apolloClient
677
+ .subscribe({
678
+ query: gql` subscription {
496
679
  perspectiveAdded { ${PERSPECTIVE_HANDLE_FIELDS} }
497
680
  }
498
- `}).subscribe({
499
- next: result => {
500
- this.#perspectiveAddedCallbacks.forEach(cb => {
501
- cb(result.data.perspectiveAdded)
502
- })
503
- },
504
- error: (e) => console.error(e)
505
- })
506
- }
507
-
508
- addPerspectiveUpdatedListener(cb: PerspectiveHandleCallback) {
509
- this.#perspectiveUpdatedCallbacks.push(cb)
510
- }
511
-
512
- subscribePerspectiveUpdated() {
513
- this.#apolloClient.subscribe({
514
- query: gql` subscription {
681
+ `,
682
+ })
683
+ .subscribe({
684
+ next: (result) => {
685
+ this._perspectiveAddedCallbacks.forEach((cb) => {
686
+ cb(result.data.perspectiveAdded);
687
+ });
688
+ },
689
+ error: (e) => {
690
+ if (!isSocketCloseError(e)) console.error(e);
691
+ },
692
+ });
693
+ }
694
+
695
+ addPerspectiveUpdatedListener(cb: PerspectiveHandleCallback) {
696
+ this._perspectiveUpdatedCallbacks.push(cb);
697
+ }
698
+
699
+ subscribePerspectiveUpdated() {
700
+ this._apolloClient
701
+ .subscribe({
702
+ query: gql` subscription {
515
703
  perspectiveUpdated { ${PERSPECTIVE_HANDLE_FIELDS} }
516
704
  }
517
- `}).subscribe({
518
- next: result => {
519
- this.#perspectiveUpdatedCallbacks.forEach(cb => {
520
- cb(result.data.perspectiveUpdated)
521
- })
522
- },
523
- error: (e) => console.error(e)
524
- })
525
- }
526
-
527
- addPerspectiveSyncedListener(cb: SyncStateChangeCallback) {
528
- this.#perspectiveSyncStateChangeCallbacks.push(cb)
529
- }
530
-
531
- async addPerspectiveSyncStateChangeListener(uuid: String, cb: SyncStateChangeCallback[]): Promise<void> {
532
- this.#apolloClient.subscribe({
533
- query: gql` subscription {
705
+ `,
706
+ })
707
+ .subscribe({
708
+ next: (result) => {
709
+ this._perspectiveUpdatedCallbacks.forEach((cb) => {
710
+ cb(result.data.perspectiveUpdated);
711
+ });
712
+ },
713
+ error: (e) => {
714
+ if (!isSocketCloseError(e)) console.error(e);
715
+ },
716
+ });
717
+ }
718
+
719
+ addPerspectiveSyncedListener(cb: SyncStateChangeCallback) {
720
+ this._perspectiveSyncStateChangeCallbacks.push(cb);
721
+ }
722
+
723
+ async addPerspectiveSyncStateChangeListener(
724
+ uuid: String,
725
+ cb: SyncStateChangeCallback[],
726
+ ): Promise<void> {
727
+ this._apolloClient
728
+ .subscribe({
729
+ query: gql` subscription {
534
730
  perspectiveSyncStateChange(uuid: "${uuid}")
535
731
  }
536
- `}).subscribe({
537
- next: result => {
538
- cb.forEach(c => {
539
- c(result.data.perspectiveSyncStateChange)
540
- })
541
- },
542
- error: (e) => console.error(e)
543
- })
544
-
545
- await new Promise<void>(resolve => setTimeout(resolve, 500))
546
- }
547
-
548
- addPerspectiveRemovedListener(cb: UuidCallback) {
549
- this.#perspectiveRemovedCallbacks.push(cb)
550
- }
551
-
552
- subscribePerspectiveRemoved() {
553
- this.#apolloClient.subscribe({
554
- query: gql` subscription {
555
- perspectiveRemoved
556
- }
557
- `}).subscribe({
558
- next: result => {
559
- this.#perspectiveRemovedCallbacks.forEach(cb => {
560
- cb(result.data.perspectiveRemoved)
561
- })
562
- },
563
- error: (e) => console.error(e)
564
- })
565
- }
566
-
567
- async addPerspectiveLinkAddedListener(uuid: String, cb: LinkCallback[]): Promise<void> {
568
- this.#apolloClient.subscribe({
569
- query: gql` subscription {
732
+ `,
733
+ })
734
+ .subscribe({
735
+ next: (result) => {
736
+ cb.forEach((c) => {
737
+ c(result.data.perspectiveSyncStateChange);
738
+ });
739
+ },
740
+ error: (e) => {
741
+ if (!isSocketCloseError(e)) console.error(e);
742
+ },
743
+ });
744
+
745
+ await new Promise<void>((resolve) => setTimeout(resolve, 500));
746
+ }
747
+
748
+ addPerspectiveRemovedListener(cb: UuidCallback) {
749
+ this._perspectiveRemovedCallbacks.push(cb);
750
+ }
751
+
752
+ subscribePerspectiveRemoved() {
753
+ this._apolloClient
754
+ .subscribe({
755
+ query: gql`
756
+ subscription {
757
+ perspectiveRemoved
758
+ }
759
+ `,
760
+ })
761
+ .subscribe({
762
+ next: (result) => {
763
+ this._perspectiveRemovedCallbacks.forEach((cb) => {
764
+ cb(result.data.perspectiveRemoved);
765
+ });
766
+ },
767
+ error: (e) => {
768
+ if (!isSocketCloseError(e)) console.error(e);
769
+ },
770
+ });
771
+ }
772
+
773
+ async addPerspectiveLinkAddedListener(
774
+ uuid: String,
775
+ cb: LinkCallback[],
776
+ ): Promise<void> {
777
+ this._apolloClient
778
+ .subscribe({
779
+ query: gql` subscription {
570
780
  perspectiveLinkAdded(uuid: "${uuid}") { ${LINK_EXPRESSION_FIELDS} }
571
781
  }
572
- `}).subscribe({
573
- next: result => {
574
- cb.forEach(c => {
575
- c(result.data.perspectiveLinkAdded)
576
- })
577
- },
578
- error: (e) => console.error(e)
579
- })
580
-
581
- await new Promise<void>(resolve => setTimeout(resolve, 500))
582
- }
583
-
584
- async addPerspectiveLinkRemovedListener(uuid: String, cb: LinkCallback[]): Promise<void> {
585
- this.#apolloClient.subscribe({
586
- query: gql` subscription {
782
+ `,
783
+ })
784
+ .subscribe({
785
+ next: (result) => {
786
+ cb.forEach((c) => {
787
+ c(result.data.perspectiveLinkAdded);
788
+ });
789
+ },
790
+ error: (e) => {
791
+ if (!isSocketCloseError(e)) console.error(e);
792
+ },
793
+ });
794
+
795
+ await new Promise<void>((resolve) => setTimeout(resolve, 500));
796
+ }
797
+
798
+ async addPerspectiveLinkRemovedListener(
799
+ uuid: String,
800
+ cb: LinkCallback[],
801
+ ): Promise<void> {
802
+ this._apolloClient
803
+ .subscribe({
804
+ query: gql` subscription {
587
805
  perspectiveLinkRemoved(uuid: "${uuid}") { ${LINK_EXPRESSION_FIELDS} }
588
806
  }
589
- `}).subscribe({
590
- next: result => {
591
- cb.forEach(c => {
592
- if (!result.data.perspectiveLinkRemoved.status) {
593
- delete result.data.perspectiveLinkRemoved.status
594
- }
595
- c(result.data.perspectiveLinkRemoved)
596
- })
597
- },
598
- error: (e) => console.error(e)
599
- })
600
-
601
- await new Promise<void>(resolve => setTimeout(resolve, 500))
602
- }
603
-
604
- async addPerspectiveLinkUpdatedListener(uuid: String, cb: LinkCallback[]): Promise<void> {
605
- this.#apolloClient.subscribe({
606
- query: gql` subscription {
807
+ `,
808
+ })
809
+ .subscribe({
810
+ next: (result) => {
811
+ cb.forEach((c) => {
812
+ if (!result.data.perspectiveLinkRemoved.status) {
813
+ delete result.data.perspectiveLinkRemoved.status;
814
+ }
815
+ c(result.data.perspectiveLinkRemoved);
816
+ });
817
+ },
818
+ error: (e) => {
819
+ if (!isSocketCloseError(e)) console.error(e);
820
+ },
821
+ });
822
+
823
+ await new Promise<void>((resolve) => setTimeout(resolve, 500));
824
+ }
825
+
826
+ async addPerspectiveLinkUpdatedListener(
827
+ uuid: String,
828
+ cb: LinkCallback[],
829
+ ): Promise<void> {
830
+ this._apolloClient
831
+ .subscribe({
832
+ query: gql` subscription {
607
833
  perspectiveLinkUpdated(uuid: "${uuid}") {
608
834
  oldLink {
609
835
  ${LINK_EXPRESSION_FIELDS}
@@ -613,41 +839,53 @@ export class PerspectiveClient {
613
839
  }
614
840
  }
615
841
  }
616
- `}).subscribe({
617
- next: result => {
618
- cb.forEach(c => {
619
- if (!result.data.perspectiveLinkUpdated.newLink.status) {
620
- delete result.data.perspectiveLinkUpdated.newLink.status
621
- }
622
- if (!result.data.perspectiveLinkUpdated.oldLink.status) {
623
- delete result.data.perspectiveLinkUpdated.oldLink.status
624
- }
625
- c(result.data.perspectiveLinkUpdated)
626
- })
627
- },
628
- error: (e) => console.error(e)
629
- })
630
-
631
- await new Promise<void>(resolve => setTimeout(resolve, 500))
632
- }
633
-
634
- getNeighbourhoodProxy(uuid: string): NeighbourhoodProxy {
635
- return new NeighbourhoodProxy(this.#neighbourhoodClient, uuid)
636
- }
637
-
638
- async createBatch(uuid: string): Promise<string> {
639
- const { perspectiveCreateBatch } = unwrapApolloResult(await this.#apolloClient.mutate({
640
- mutation: gql`mutation perspectiveCreateBatch($uuid: String!) {
641
- perspectiveCreateBatch(uuid: $uuid)
642
- }`,
643
- variables: { uuid }
644
- }))
645
- return perspectiveCreateBatch
646
- }
647
-
648
- async commitBatch(uuid: string, batchId: string): Promise<LinkExpressionMutations> {
649
- const { perspectiveCommitBatch } = unwrapApolloResult(await this.#apolloClient.mutate({
650
- mutation: gql`mutation perspectiveCommitBatch($uuid: String!, $batchId: String!) {
842
+ `,
843
+ })
844
+ .subscribe({
845
+ next: (result) => {
846
+ cb.forEach((c) => {
847
+ if (!result.data.perspectiveLinkUpdated.newLink.status) {
848
+ delete result.data.perspectiveLinkUpdated.newLink.status;
849
+ }
850
+ if (!result.data.perspectiveLinkUpdated.oldLink.status) {
851
+ delete result.data.perspectiveLinkUpdated.oldLink.status;
852
+ }
853
+ c(result.data.perspectiveLinkUpdated);
854
+ });
855
+ },
856
+ error: (e) => {
857
+ if (!isSocketCloseError(e)) console.error(e);
858
+ },
859
+ });
860
+
861
+ await new Promise<void>((resolve) => setTimeout(resolve, 500));
862
+ }
863
+
864
+ getNeighbourhoodProxy(uuid: string): NeighbourhoodProxy {
865
+ return new NeighbourhoodProxy(this._neighbourhoodClient, uuid);
866
+ }
867
+
868
+ async createBatch(uuid: string): Promise<string> {
869
+ const { perspectiveCreateBatch } = unwrapApolloResult(
870
+ await this._apolloClient.mutate({
871
+ mutation: gql`
872
+ mutation perspectiveCreateBatch($uuid: String!) {
873
+ perspectiveCreateBatch(uuid: $uuid)
874
+ }
875
+ `,
876
+ variables: { uuid },
877
+ }),
878
+ );
879
+ return perspectiveCreateBatch;
880
+ }
881
+
882
+ async commitBatch(
883
+ uuid: string,
884
+ batchId: string,
885
+ ): Promise<LinkExpressionMutations> {
886
+ const { perspectiveCommitBatch } = unwrapApolloResult(
887
+ await this._apolloClient.mutate({
888
+ mutation: gql`mutation perspectiveCommitBatch($uuid: String!, $batchId: String!) {
651
889
  perspectiveCommitBatch(uuid: $uuid, batchId: $batchId) {
652
890
  additions {
653
891
  ${LINK_EXPRESSION_FIELDS}
@@ -657,8 +895,9 @@ export class PerspectiveClient {
657
895
  }
658
896
  }
659
897
  }`,
660
- variables: { uuid, batchId }
661
- }))
662
- return perspectiveCommitBatch
663
- }
664
- }
898
+ variables: { uuid, batchId },
899
+ }),
900
+ );
901
+ return perspectiveCommitBatch;
902
+ }
903
+ }