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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/jest.config.js +10 -3
  2. package/lib/index.cjs +6872 -5886
  3. package/lib/index.js +6868 -5874
  4. package/lib/src/Ad4mClient.d.ts +9 -16
  5. package/lib/src/Ad4mClient.js +46 -25
  6. package/lib/src/Literal.d.ts +1 -2
  7. package/lib/src/Literal.js +38 -35
  8. package/lib/src/SmartLiteral.d.ts +1 -2
  9. package/lib/src/SmartLiteral.js +30 -15
  10. package/lib/src/agent/AgentClient.d.ts +1 -4
  11. package/lib/src/agent/AgentClient.js +79 -99
  12. package/lib/src/ai/AIClient.d.ts +1 -2
  13. package/lib/src/ai/AIClient.js +224 -220
  14. package/lib/src/expression/ExpressionClient.d.ts +1 -1
  15. package/lib/src/expression/ExpressionClient.js +73 -86
  16. package/lib/src/index.d.ts +1 -1
  17. package/lib/src/index.js +1 -1
  18. package/lib/src/language/LanguageClient.d.ts +1 -1
  19. package/lib/src/language/LanguageClient.js +52 -52
  20. package/lib/src/model/Ad4mModel.d.ts +1351 -144
  21. package/lib/src/model/Ad4mModel.js +3737 -228
  22. package/lib/src/model/Ad4mModel.test.js +522 -979
  23. package/lib/src/model/Subject.d.ts +3 -3
  24. package/lib/src/model/Subject.js +25 -25
  25. package/lib/src/model/decorators.d.ts +444 -100
  26. package/lib/src/model/decorators.js +686 -321
  27. package/lib/src/model/util.d.ts +6 -6
  28. package/lib/src/model/util.js +24 -24
  29. package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +1 -5
  30. package/lib/src/neighbourhood/NeighbourhoodClient.js +179 -242
  31. package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +1 -2
  32. package/lib/src/neighbourhood/NeighbourhoodProxy.js +28 -13
  33. package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +5 -1
  34. package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
  35. package/lib/src/perspectives/PerspectiveClient.js +240 -316
  36. package/lib/src/perspectives/PerspectiveProxy.d.ts +99 -61
  37. package/lib/src/perspectives/PerspectiveProxy.js +485 -832
  38. package/lib/src/runtime/RuntimeClient.d.ts +1 -5
  39. package/lib/src/runtime/RuntimeClient.js +238 -412
  40. package/lib/src/shacl/SHACLFlow.test.js +105 -164
  41. package/lib/src/shacl/SHACLShape.d.ts +12 -17
  42. package/lib/src/shacl/SHACLShape.js +115 -128
  43. package/lib/src/shacl/SHACLShape.test.js +176 -244
  44. package/lib/src/shims/reflect-metadata.d.ts +0 -0
  45. package/lib/src/shims/type-graphql.d.ts +32 -0
  46. package/lib/src/src/Ad4mClient.d.ts +28 -0
  47. package/lib/src/src/Address.d.ts +1 -0
  48. package/lib/src/src/DID.d.ts +1 -0
  49. package/lib/src/src/Exception.d.ts +7 -0
  50. package/lib/src/src/Literal.d.ts +7 -0
  51. package/lib/src/src/PubSub.d.ts +17 -0
  52. package/lib/src/src/SmartLiteral.d.ts +12 -0
  53. package/lib/src/src/agent/Agent.d.ts +110 -0
  54. package/lib/src/src/agent/AgentClient.d.ts +62 -0
  55. package/lib/src/src/agent/AgentResolver.d.ts +30 -0
  56. package/lib/src/src/agent/AgentStatus.d.ts +8 -0
  57. package/lib/src/src/ai/AIClient.d.ts +32 -0
  58. package/lib/src/src/ai/AIResolver.d.ts +75 -0
  59. package/lib/src/src/ai/Tasks.d.ts +37 -0
  60. package/lib/src/src/buildSchema.d.ts +1 -0
  61. package/lib/src/src/expression/Expression.d.ts +28 -0
  62. package/lib/src/src/expression/ExpressionClient.d.ts +13 -0
  63. package/lib/src/src/expression/ExpressionRef.d.ts +9 -0
  64. package/lib/src/src/expression/ExpressionResolver.d.ts +10 -0
  65. package/lib/src/src/index.d.ts +35 -0
  66. package/lib/src/src/jestSetup.d.ts +1 -0
  67. package/lib/src/src/language/Icon.d.ts +4 -0
  68. package/lib/src/src/language/Language.d.ts +206 -0
  69. package/lib/src/src/language/LanguageClient.d.ts +17 -0
  70. package/lib/src/src/language/LanguageContext.d.ts +37 -0
  71. package/lib/src/src/language/LanguageHandle.d.ts +9 -0
  72. package/lib/src/src/language/LanguageMeta.d.ts +35 -0
  73. package/lib/src/src/language/LanguageRef.d.ts +6 -0
  74. package/lib/src/src/language/LanguageResolver.d.ts +19 -0
  75. package/lib/src/src/links/Links.d.ts +39 -0
  76. package/lib/src/src/links/Links.test.d.ts +1 -0
  77. package/lib/src/src/model/Ad4mModel.d.ts +1438 -0
  78. package/lib/src/src/model/Ad4mModel.test.d.ts +1 -0
  79. package/lib/src/src/model/Subject.d.ts +25 -0
  80. package/lib/src/src/model/decorators.d.ts +545 -0
  81. package/lib/src/src/model/util.d.ts +12 -0
  82. package/lib/src/src/neighbourhood/Neighbourhood.d.ts +10 -0
  83. package/lib/src/src/neighbourhood/NeighbourhoodClient.d.ts +25 -0
  84. package/lib/src/src/neighbourhood/NeighbourhoodProxy.d.ts +19 -0
  85. package/lib/src/src/neighbourhood/NeighbourhoodProxy.test.d.ts +1 -0
  86. package/lib/src/src/neighbourhood/NeighbourhoodResolver.d.ts +24 -0
  87. package/lib/src/src/perspectives/LinkQuery.d.ts +11 -0
  88. package/lib/src/src/perspectives/Perspective.d.ts +38 -0
  89. package/lib/src/src/perspectives/Perspective.test.d.ts +1 -0
  90. package/lib/src/src/perspectives/PerspectiveClient.d.ts +97 -0
  91. package/lib/src/src/perspectives/PerspectiveDiff.d.ts +9 -0
  92. package/lib/src/src/perspectives/PerspectiveHandle.d.ts +18 -0
  93. package/lib/src/src/perspectives/PerspectiveProxy.d.ts +865 -0
  94. package/lib/src/src/perspectives/PerspectiveResolver.d.ts +57 -0
  95. package/lib/src/src/runtime/RuntimeClient.d.ts +56 -0
  96. package/lib/src/src/runtime/RuntimeResolver.d.ts +132 -0
  97. package/lib/src/src/shacl/SHACLFlow.d.ts +171 -0
  98. package/lib/src/src/shacl/SHACLFlow.test.d.ts +1 -0
  99. package/lib/src/src/shacl/SHACLShape.d.ts +117 -0
  100. package/lib/src/src/shacl/SHACLShape.test.d.ts +1 -0
  101. package/lib/src/src/typeDefs.d.ts +1 -0
  102. package/lib/src/src/unwrapApolloResult.d.ts +2 -0
  103. package/lib/src/src/utils.d.ts +25 -0
  104. package/lib/src/utils.d.ts +0 -8
  105. package/lib/src/utils.js +7 -21
  106. package/package.json +15 -14
  107. package/src/Ad4mClient.test.ts +1315 -1564
  108. package/src/Ad4mClient.ts +54 -55
  109. package/src/Literal.test.ts +26 -45
  110. package/src/Literal.ts +66 -82
  111. package/src/SmartLiteral.ts +58 -74
  112. package/src/agent/AgentClient.ts +99 -144
  113. package/src/ai/AIClient.ts +359 -409
  114. package/src/expression/ExpressionClient.ts +96 -142
  115. package/src/index.ts +1 -1
  116. package/src/language/LanguageClient.ts +113 -137
  117. package/src/model/Ad4mModel.test.ts +616 -1114
  118. package/src/model/Ad4mModel.ts +4361 -451
  119. package/src/model/Subject.ts +159 -0
  120. package/src/model/decorators.ts +1168 -667
  121. package/src/model/util.ts +54 -53
  122. package/src/neighbourhood/NeighbourhoodClient.ts +266 -392
  123. package/src/neighbourhood/NeighbourhoodProxy.test.ts +7 -3
  124. package/src/neighbourhood/NeighbourhoodProxy.ts +40 -58
  125. package/src/perspectives/PerspectiveClient.ts +540 -779
  126. package/src/perspectives/PerspectiveProxy.ts +2067 -2860
  127. package/src/runtime/RuntimeClient.ts +476 -775
  128. package/src/shacl/SHACLFlow.test.ts +129 -196
  129. package/src/shacl/SHACLShape.test.ts +190 -284
  130. package/src/shacl/SHACLShape.ts +200 -224
  131. package/src/utils.ts +17 -31
  132. package/tsconfig.json +10 -9
  133. package/src/model/IMPROVEMENTS.md +0 -482
  134. package/src/model/PREDICATE_COLLISION_FIX.md +0 -651
  135. package/src/model/README.md +0 -317
  136. package/src/model/SHACL_MUTATION_DECOUPLING.md +0 -356
  137. package/src/model/SUBSCRIPTION_STRATEGY.md +0 -317
  138. package/src/model/TC39_STAGE_3_DECORATORS.md +0 -609
  139. package/src/model/mutation.ts +0 -461
  140. package/src/model/parentUtils.ts +0 -91
  141. package/src/model/prolog/generatePrologFacts.ts +0 -176
  142. package/src/model/prolog/index.ts +0 -1
  143. package/src/model/query/ModelQueryBuilder.ts +0 -173
  144. package/src/model/query/fetchInstance.ts +0 -181
  145. package/src/model/query/hydration.ts +0 -273
  146. package/src/model/query/operations.ts +0 -510
  147. package/src/model/query/snapshot.ts +0 -126
  148. package/src/model/query/surrealCompiler.ts +0 -376
  149. package/src/model/schema/fromJSONSchema.ts +0 -411
  150. package/src/model/schema/metadata.ts +0 -148
  151. package/src/model/subscription.ts +0 -307
  152. package/src/model/transaction.ts +0 -60
  153. package/src/model/types.ts +0 -207
@@ -1,24 +1,16 @@
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 {
5
- Link,
6
- LinkExpressionInput,
7
- LinkExpression,
8
- LinkInput,
9
- LinkMutations,
10
- LinkExpressionMutations,
11
- } from "../links/Links";
4
+ import { Link, LinkExpressionInput, LinkExpression, LinkInput, LinkMutations, LinkExpressionMutations } from "../links/Links";
12
5
  import { NeighbourhoodClient } from "../neighbourhood/NeighbourhoodClient";
13
6
  import { NeighbourhoodProxy } from "../neighbourhood/NeighbourhoodProxy";
14
7
  import unwrapApolloResult from "../unwrapApolloResult";
15
8
  import { LinkQuery } from "./LinkQuery";
16
9
  import { Perspective } from "./Perspective";
17
10
  import { PerspectiveHandle, PerspectiveState } from "./PerspectiveHandle";
18
- import { LinkStatus, PerspectiveProxy } from "./PerspectiveProxy";
11
+ import { LinkStatus, PerspectiveProxy } from './PerspectiveProxy';
19
12
  import { AIClient } from "../ai/AIClient";
20
13
  import { AllInstancesResult } from "../model/Ad4mModel";
21
- import { isSocketCloseError } from "../utils";
22
14
 
23
15
  const LINK_EXPRESSION_FIELDS = `
24
16
  author
@@ -26,7 +18,7 @@ timestamp
26
18
  status
27
19
  data { source, predicate, target }
28
20
  proof { valid, invalid, signature, key }
29
- `;
21
+ `
30
22
 
31
23
  const PERSPECTIVE_HANDLE_FIELDS = `
32
24
  uuid
@@ -49,393 +41,343 @@ neighbourhood {
49
41
  }
50
42
  author
51
43
  }
52
- `;
44
+ `
53
45
 
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;
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
60
50
 
61
51
  export class PerspectiveClient {
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 {
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 {
105
94
  perspectives {
106
95
  ${PERSPECTIVE_HANDLE_FIELDS}
107
96
  }
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!) {
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!) {
118
105
  perspective(uuid: $uuid) {
119
106
  ${PERSPECTIVE_HANDLE_FIELDS}
120
107
  }
121
108
  }`,
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!) {
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!) {
133
118
  perspectiveSnapshot(uuid: $uuid) {
134
119
  links { ${LINK_EXPRESSION_FIELDS} }
135
120
  }
136
121
  }`,
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!) {
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!) {
160
139
  perspectiveQueryLinks(query: $query, uuid: $uuid) {
161
140
  ${LINK_EXPRESSION_FIELDS}
162
141
  }
163
142
  }`,
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
224
- }
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);
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
276
260
  }
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!) {
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!) {
338
312
  perspectiveAdd(name: $name) {
339
313
  ${PERSPECTIVE_HANDLE_FIELDS}
340
314
  }
341
315
  }`,
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!) {
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!) {
352
324
  perspectiveUpdate(uuid: $uuid, name: $name) {
353
325
  ${PERSPECTIVE_HANDLE_FIELDS}
354
326
  }
355
327
  }`,
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) {
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) {
384
345
  perspectiveAddLink(uuid: $uuid, link: $link, status: $status, batchId: $batchId) {
385
346
  ${LINK_EXPRESSION_FIELDS}
386
347
  }
387
348
  }`,
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) {
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) {
403
357
  perspectiveAddLinks(uuid: $uuid, links: $links, status: $status, batchId: $batchId) {
404
358
  ${LINK_EXPRESSION_FIELDS}
405
359
  }
406
360
  }`,
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) {
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) {
421
369
  perspectiveRemoveLinks(uuid: $uuid, links: $links, batchId: $batchId) {
422
370
  ${LINK_EXPRESSION_FIELDS}
423
371
  }
424
372
  }`,
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){
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){
439
381
  perspectiveLinkMutations(mutations: $mutations, uuid: $uuid, status: $status) {
440
382
  additions {
441
383
  ${LINK_EXPRESSION_FIELDS}
@@ -445,391 +387,223 @@ export class PerspectiveClient {
445
387
  }
446
388
  }
447
389
  }`,
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) {
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) {
463
398
  perspectiveAddLinkExpression(uuid: $uuid, link: $link, status: $status, batchId: $batchId) {
464
399
  ${LINK_EXPRESSION_FIELDS}
465
400
  }
466
401
  }`,
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) {
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) {
482
410
  perspectiveUpdateLink(uuid: $uuid, oldLink: $oldLink, newLink: $newLink, batchId: $batchId) {
483
411
  ${LINK_EXPRESSION_FIELDS}
484
412
  }
485
413
  }`,
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 {
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 {
679
496
  perspectiveAdded { ${PERSPECTIVE_HANDLE_FIELDS} }
680
497
  }
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 {
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 {
703
515
  perspectiveUpdated { ${PERSPECTIVE_HANDLE_FIELDS} }
704
516
  }
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 {
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 {
730
534
  perspectiveSyncStateChange(uuid: "${uuid}")
731
535
  }
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 {
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 {
780
570
  perspectiveLinkAdded(uuid: "${uuid}") { ${LINK_EXPRESSION_FIELDS} }
781
571
  }
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 {
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 {
805
587
  perspectiveLinkRemoved(uuid: "${uuid}") { ${LINK_EXPRESSION_FIELDS} }
806
588
  }
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 {
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 {
833
607
  perspectiveLinkUpdated(uuid: "${uuid}") {
834
608
  oldLink {
835
609
  ${LINK_EXPRESSION_FIELDS}
@@ -839,53 +613,41 @@ export class PerspectiveClient {
839
613
  }
840
614
  }
841
615
  }
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!) {
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!) {
889
651
  perspectiveCommitBatch(uuid: $uuid, batchId: $batchId) {
890
652
  additions {
891
653
  ${LINK_EXPRESSION_FIELDS}
@@ -895,9 +657,8 @@ export class PerspectiveClient {
895
657
  }
896
658
  }
897
659
  }`,
898
- variables: { uuid, batchId },
899
- }),
900
- );
901
- return perspectiveCommitBatch;
902
- }
903
- }
660
+ variables: { uuid, batchId }
661
+ }))
662
+ return perspectiveCommitBatch
663
+ }
664
+ }