@coasys/ad4m 0.10.0 → 0.10.1-dev-1
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.
- package/lib/index.cjs +3785 -843
- package/lib/index.js +3777 -839
- package/lib/src/Ad4mClient.js +2 -1
- package/lib/src/Literal.js +4 -1
- package/lib/src/ai/AIClient.d.ts +8 -2
- package/lib/src/ai/AIClient.js +25 -14
- package/lib/src/ai/AIResolver.d.ts +25 -6
- package/lib/src/ai/AIResolver.js +102 -18
- package/lib/src/index.d.ts +4 -3
- package/lib/src/index.js +4 -3
- package/lib/src/model/Ad4mModel.d.ts +730 -0
- package/lib/src/model/Ad4mModel.js +1399 -0
- package/lib/src/{subject → model}/Subject.d.ts +2 -2
- package/lib/src/{subject → model}/Subject.js +18 -18
- package/lib/src/model/decorators.d.ts +502 -0
- package/lib/src/model/decorators.js +683 -0
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +2 -2
- package/lib/src/neighbourhood/NeighbourhoodClient.js +13 -10
- package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +2 -2
- package/lib/src/neighbourhood/NeighbourhoodProxy.js +4 -4
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.d.ts +1 -0
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +73 -0
- package/lib/src/neighbourhood/NeighbourhoodResolver.d.ts +2 -2
- package/lib/src/neighbourhood/NeighbourhoodResolver.js +6 -4
- package/lib/src/perspectives/PerspectiveClient.d.ts +23 -11
- package/lib/src/perspectives/PerspectiveClient.js +147 -51
- package/lib/src/perspectives/PerspectiveProxy.d.ts +494 -54
- package/lib/src/perspectives/PerspectiveProxy.js +684 -67
- package/lib/src/perspectives/PerspectiveResolver.d.ts +24 -9
- package/lib/src/perspectives/PerspectiveResolver.js +151 -46
- package/lib/src/runtime/RuntimeClient.d.ts +8 -2
- package/lib/src/runtime/RuntimeClient.js +64 -0
- package/lib/src/runtime/RuntimeResolver.d.ts +27 -1
- package/lib/src/runtime/RuntimeResolver.js +138 -0
- package/lib/src/schema.gql +55 -16
- package/lib/src/typeDefs.js +55 -16
- package/package.json +4 -3
- package/src/Ad4mClient.test.ts +51 -19
- package/src/Ad4mClient.ts +3 -1
- package/src/Literal.ts +4 -1
- package/src/ai/AIClient.ts +35 -14
- package/src/ai/AIResolver.ts +78 -16
- package/src/index.ts +4 -3
- package/src/model/Ad4mModel.ts +1651 -0
- package/src/{subject → model}/Subject.ts +16 -16
- package/src/{subject/SDNADecorators.ts → model/decorators.ts} +384 -50
- package/src/neighbourhood/NeighbourhoodClient.ts +13 -10
- package/src/neighbourhood/NeighbourhoodProxy.test.ts +100 -0
- package/src/neighbourhood/NeighbourhoodProxy.ts +4 -4
- package/src/neighbourhood/NeighbourhoodResolver.ts +2 -2
- package/src/perspectives/PerspectiveClient.ts +156 -51
- package/src/perspectives/PerspectiveProxy.ts +766 -82
- package/src/perspectives/PerspectiveResolver.ts +134 -23
- package/src/runtime/RuntimeClient.ts +71 -2
- package/src/runtime/RuntimeResolver.ts +91 -1
- package/lib/src/subject/SDNADecorators.d.ts +0 -188
- package/lib/src/subject/SDNADecorators.js +0 -360
- package/lib/src/subject/SubjectEntity.d.ts +0 -102
- package/lib/src/subject/SubjectEntity.js +0 -267
- package/src/subject/SubjectEntity.ts +0 -315
- package/yarn-error.log +0 -90
- /package/lib/src/{subject → model}/util.d.ts +0 -0
- /package/lib/src/{subject → model}/util.js +0 -0
- /package/src/{subject → model}/util.ts +0 -0
|
@@ -9,18 +9,260 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _PerspectiveProxy_handle, _PerspectiveProxy_client, _PerspectiveProxy_perspectiveLinkAddedCallbacks, _PerspectiveProxy_perspectiveLinkRemovedCallbacks, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, _PerspectiveProxy_perspectiveSyncStateChangeCallbacks;
|
|
12
|
+
var _QuerySubscriptionProxy_instances, _QuerySubscriptionProxy_uuid, _QuerySubscriptionProxy_subscriptionId, _QuerySubscriptionProxy_client, _QuerySubscriptionProxy_callbacks, _QuerySubscriptionProxy_keepaliveTimer, _QuerySubscriptionProxy_unsubscribe, _QuerySubscriptionProxy_latestResult, _QuerySubscriptionProxy_disposed, _QuerySubscriptionProxy_initialized, _QuerySubscriptionProxy_initTimeoutId, _QuerySubscriptionProxy_query, _QuerySubscriptionProxy_notifyCallbacks, _PerspectiveProxy_handle, _PerspectiveProxy_client, _PerspectiveProxy_perspectiveLinkAddedCallbacks, _PerspectiveProxy_perspectiveLinkRemovedCallbacks, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, _PerspectiveProxy_perspectiveSyncStateChangeCallbacks;
|
|
13
13
|
import { LinkQuery } from "./LinkQuery";
|
|
14
14
|
import { Literal } from "../Literal";
|
|
15
|
-
import { Subject } from "../
|
|
16
|
-
import { collectionAdderToName, collectionRemoverToName, collectionSetterToName } from "../
|
|
17
|
-
/**
|
|
15
|
+
import { Subject } from "../model/Subject";
|
|
16
|
+
import { collectionAdderToName, collectionRemoverToName, collectionSetterToName } from "../model/util";
|
|
17
|
+
/** Proxy object for a subscribed Prolog query that provides real-time updates
|
|
18
18
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
19
|
+
* This class handles:
|
|
20
|
+
* - Keeping the subscription alive by sending periodic keepalive signals
|
|
21
|
+
* - Managing callbacks for result updates
|
|
22
|
+
* - Subscribing to query updates via GraphQL subscriptions
|
|
23
|
+
* - Maintaining the latest query result
|
|
24
|
+
* - Ensuring subscription is fully initialized before allowing access
|
|
25
|
+
* - Cleaning up resources when disposed
|
|
26
|
+
*
|
|
27
|
+
* The subscription will remain active as long as keepalive signals are sent.
|
|
28
|
+
* Make sure to call dispose() when you're done with the subscription to clean up
|
|
29
|
+
* resources, stop keepalive signals, and notify the backend to remove the subscription.
|
|
30
|
+
*
|
|
31
|
+
* The subscription goes through an initialization process where it waits for the first
|
|
32
|
+
* result to come through the subscription channel. You can await the `initialized`
|
|
33
|
+
* promise to ensure the subscription is ready. The initialization will timeout after
|
|
34
|
+
* 30 seconds if no result is received.
|
|
35
|
+
*
|
|
36
|
+
* Example usage:
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const subscription = await perspective.subscribeInfer("my_query(X)");
|
|
39
|
+
* // At this point the subscription is already initialized since subscribeInfer waits
|
|
40
|
+
*
|
|
41
|
+
* // Set up callback for future updates
|
|
42
|
+
* const removeCallback = subscription.onResult(result => {
|
|
43
|
+
* console.log("New result:", result);
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // Later: clean up subscription and notify backend
|
|
47
|
+
* subscription.dispose();
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export class QuerySubscriptionProxy {
|
|
51
|
+
/** Creates a new query subscription
|
|
52
|
+
* @param uuid - The UUID of the perspective
|
|
53
|
+
* @param query - The Prolog query to subscribe to
|
|
54
|
+
* @param client - The PerspectiveClient instance to use for communication
|
|
55
|
+
*/
|
|
56
|
+
constructor(uuid, query, client) {
|
|
57
|
+
_QuerySubscriptionProxy_instances.add(this);
|
|
58
|
+
_QuerySubscriptionProxy_uuid.set(this, void 0);
|
|
59
|
+
_QuerySubscriptionProxy_subscriptionId.set(this, void 0);
|
|
60
|
+
_QuerySubscriptionProxy_client.set(this, void 0);
|
|
61
|
+
_QuerySubscriptionProxy_callbacks.set(this, void 0);
|
|
62
|
+
_QuerySubscriptionProxy_keepaliveTimer.set(this, void 0);
|
|
63
|
+
_QuerySubscriptionProxy_unsubscribe.set(this, void 0);
|
|
64
|
+
_QuerySubscriptionProxy_latestResult.set(this, void 0);
|
|
65
|
+
_QuerySubscriptionProxy_disposed.set(this, false);
|
|
66
|
+
_QuerySubscriptionProxy_initialized.set(this, void 0);
|
|
67
|
+
_QuerySubscriptionProxy_initTimeoutId.set(this, void 0);
|
|
68
|
+
_QuerySubscriptionProxy_query.set(this, void 0);
|
|
69
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_uuid, uuid, "f");
|
|
70
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_query, query, "f");
|
|
71
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_client, client, "f");
|
|
72
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_callbacks, new Set(), "f");
|
|
73
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_latestResult, null, "f");
|
|
74
|
+
}
|
|
75
|
+
async subscribe() {
|
|
76
|
+
// initialize the query subscription
|
|
77
|
+
const result = await __classPrivateFieldGet(this, _QuerySubscriptionProxy_client, "f").subscribeQuery(__classPrivateFieldGet(this, _QuerySubscriptionProxy_uuid, "f"), __classPrivateFieldGet(this, _QuerySubscriptionProxy_query, "f"));
|
|
78
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_subscriptionId, result.subscriptionId, "f");
|
|
79
|
+
// Subscribe to query updates
|
|
80
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_initialized, new Promise((resolve, reject) => {
|
|
81
|
+
// Add timeout to prevent hanging promises
|
|
82
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_initTimeoutId, setTimeout(() => {
|
|
83
|
+
reject(new Error('Subscription initialization timed out after 30 seconds. Resubscribing...'));
|
|
84
|
+
this.subscribe();
|
|
85
|
+
}, 30000), "f"); // 30 seconds timeout
|
|
86
|
+
// Subscribe to query updates
|
|
87
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_unsubscribe, __classPrivateFieldGet(this, _QuerySubscriptionProxy_client, "f").subscribeToQueryUpdates(__classPrivateFieldGet(this, _QuerySubscriptionProxy_subscriptionId, "f"), (result) => {
|
|
88
|
+
if (__classPrivateFieldGet(this, _QuerySubscriptionProxy_initTimeoutId, "f")) {
|
|
89
|
+
clearTimeout(__classPrivateFieldGet(this, _QuerySubscriptionProxy_initTimeoutId, "f"));
|
|
90
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_initTimeoutId, undefined, "f");
|
|
91
|
+
}
|
|
92
|
+
resolve(true);
|
|
93
|
+
// if the result is one of those repeated initialization results
|
|
94
|
+
// and we got a result before, we don't notify the callbacks
|
|
95
|
+
// so they don't get confused (we could have gotten another
|
|
96
|
+
// more recent result in between)
|
|
97
|
+
if (result.isInit && __classPrivateFieldGet(this, _QuerySubscriptionProxy_latestResult, "f")) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_latestResult, result, "f");
|
|
101
|
+
__classPrivateFieldGet(this, _QuerySubscriptionProxy_instances, "m", _QuerySubscriptionProxy_notifyCallbacks).call(this, result);
|
|
102
|
+
}), "f");
|
|
103
|
+
}), "f");
|
|
104
|
+
// Start keepalive loop using platform-agnostic setTimeout
|
|
105
|
+
const keepaliveLoop = async () => {
|
|
106
|
+
if (__classPrivateFieldGet(this, _QuerySubscriptionProxy_disposed, "f"))
|
|
107
|
+
return;
|
|
108
|
+
try {
|
|
109
|
+
await __classPrivateFieldGet(this, _QuerySubscriptionProxy_client, "f").keepAliveQuery(__classPrivateFieldGet(this, _QuerySubscriptionProxy_uuid, "f"), __classPrivateFieldGet(this, _QuerySubscriptionProxy_subscriptionId, "f"));
|
|
110
|
+
}
|
|
111
|
+
catch (e) {
|
|
112
|
+
console.error('Error in keepalive:', e);
|
|
113
|
+
// try to reinitialize the subscription
|
|
114
|
+
console.log('Reinitializing subscription for query:', __classPrivateFieldGet(this, _QuerySubscriptionProxy_query, "f"));
|
|
115
|
+
await this.subscribe();
|
|
116
|
+
console.log('Subscription reinitialized');
|
|
117
|
+
}
|
|
118
|
+
// Schedule next keepalive if not disposed
|
|
119
|
+
if (!__classPrivateFieldGet(this, _QuerySubscriptionProxy_disposed, "f")) {
|
|
120
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_keepaliveTimer, setTimeout(keepaliveLoop, 30000), "f");
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
// Start the first keepalive loop
|
|
124
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_keepaliveTimer, setTimeout(keepaliveLoop, 30000), "f");
|
|
125
|
+
}
|
|
126
|
+
/** Get the subscription ID for this query subscription
|
|
127
|
+
*
|
|
128
|
+
* This is a unique identifier assigned when the subscription was created.
|
|
129
|
+
* It can be used to reference this specific subscription, for example when
|
|
130
|
+
* sending keepalive signals.
|
|
131
|
+
*
|
|
132
|
+
* @returns The subscription ID string
|
|
133
|
+
*/
|
|
134
|
+
get id() {
|
|
135
|
+
return __classPrivateFieldGet(this, _QuerySubscriptionProxy_subscriptionId, "f");
|
|
136
|
+
}
|
|
137
|
+
/** Promise that resolves when the subscription has received its first result
|
|
138
|
+
* through the subscription channel. This ensures the subscription is fully
|
|
139
|
+
* set up before allowing access to results or updates.
|
|
140
|
+
*
|
|
141
|
+
* The promise will reject if no result is received within 30 seconds.
|
|
142
|
+
*
|
|
143
|
+
* Note: You typically don't need to await this directly since the subscription
|
|
144
|
+
* creation methods (like subscribeInfer) already wait for initialization.
|
|
145
|
+
*/
|
|
146
|
+
get initialized() {
|
|
147
|
+
return __classPrivateFieldGet(this, _QuerySubscriptionProxy_initialized, "f");
|
|
148
|
+
}
|
|
149
|
+
/** Get the latest query result
|
|
150
|
+
*
|
|
151
|
+
* This returns the most recent result from the query, which could be either:
|
|
152
|
+
* - The initial result from when the subscription was created
|
|
153
|
+
* - The latest update received through the subscription
|
|
154
|
+
*
|
|
155
|
+
* @returns The latest query result as a string (usually a JSON array of bindings)
|
|
156
|
+
*/
|
|
157
|
+
get result() {
|
|
158
|
+
return __classPrivateFieldGet(this, _QuerySubscriptionProxy_latestResult, "f");
|
|
159
|
+
}
|
|
160
|
+
/** Add a callback that will be called whenever new results arrive
|
|
161
|
+
*
|
|
162
|
+
* The callback will be called immediately with the current result,
|
|
163
|
+
* and then again each time the query results change.
|
|
164
|
+
*
|
|
165
|
+
* @param callback - Function that takes a result string and processes it
|
|
166
|
+
* @returns A function that can be called to remove this callback
|
|
167
|
+
*
|
|
168
|
+
* Example:
|
|
169
|
+
* ```typescript
|
|
170
|
+
* const removeCallback = subscription.onResult(result => {
|
|
171
|
+
* const bindings = JSON.parse(result);
|
|
172
|
+
* console.log("New bindings:", bindings);
|
|
173
|
+
* });
|
|
174
|
+
*
|
|
175
|
+
* // Later: stop receiving updates
|
|
176
|
+
* removeCallback();
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
onResult(callback) {
|
|
180
|
+
__classPrivateFieldGet(this, _QuerySubscriptionProxy_callbacks, "f").add(callback);
|
|
181
|
+
return () => __classPrivateFieldGet(this, _QuerySubscriptionProxy_callbacks, "f").delete(callback);
|
|
182
|
+
}
|
|
183
|
+
/** Clean up the subscription and stop keepalive signals
|
|
184
|
+
*
|
|
185
|
+
* This method:
|
|
186
|
+
* 1. Stops the keepalive timer
|
|
187
|
+
* 2. Unsubscribes from GraphQL subscription updates
|
|
188
|
+
* 3. Clears all registered callbacks
|
|
189
|
+
* 4. Cleans up any pending initialization timeout
|
|
190
|
+
*
|
|
191
|
+
* After calling this method, the subscription is no longer active and
|
|
192
|
+
* will not receive any more updates. The instance should be discarded.
|
|
193
|
+
*/
|
|
194
|
+
dispose() {
|
|
195
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_disposed, true, "f");
|
|
196
|
+
clearTimeout(__classPrivateFieldGet(this, _QuerySubscriptionProxy_keepaliveTimer, "f"));
|
|
197
|
+
if (__classPrivateFieldGet(this, _QuerySubscriptionProxy_unsubscribe, "f")) {
|
|
198
|
+
__classPrivateFieldGet(this, _QuerySubscriptionProxy_unsubscribe, "f").call(this);
|
|
199
|
+
}
|
|
200
|
+
__classPrivateFieldGet(this, _QuerySubscriptionProxy_callbacks, "f").clear();
|
|
201
|
+
if (__classPrivateFieldGet(this, _QuerySubscriptionProxy_initTimeoutId, "f")) {
|
|
202
|
+
clearTimeout(__classPrivateFieldGet(this, _QuerySubscriptionProxy_initTimeoutId, "f"));
|
|
203
|
+
__classPrivateFieldSet(this, _QuerySubscriptionProxy_initTimeoutId, undefined, "f");
|
|
204
|
+
}
|
|
205
|
+
// Tell the backend to dispose of the subscription
|
|
206
|
+
if (__classPrivateFieldGet(this, _QuerySubscriptionProxy_subscriptionId, "f")) {
|
|
207
|
+
__classPrivateFieldGet(this, _QuerySubscriptionProxy_client, "f").disposeQuerySubscription(__classPrivateFieldGet(this, _QuerySubscriptionProxy_uuid, "f"), __classPrivateFieldGet(this, _QuerySubscriptionProxy_subscriptionId, "f"))
|
|
208
|
+
.catch(e => console.error('Error disposing query subscription:', e));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
_QuerySubscriptionProxy_uuid = new WeakMap(), _QuerySubscriptionProxy_subscriptionId = new WeakMap(), _QuerySubscriptionProxy_client = new WeakMap(), _QuerySubscriptionProxy_callbacks = new WeakMap(), _QuerySubscriptionProxy_keepaliveTimer = new WeakMap(), _QuerySubscriptionProxy_unsubscribe = new WeakMap(), _QuerySubscriptionProxy_latestResult = new WeakMap(), _QuerySubscriptionProxy_disposed = new WeakMap(), _QuerySubscriptionProxy_initialized = new WeakMap(), _QuerySubscriptionProxy_initTimeoutId = new WeakMap(), _QuerySubscriptionProxy_query = new WeakMap(), _QuerySubscriptionProxy_instances = new WeakSet(), _QuerySubscriptionProxy_notifyCallbacks = function _QuerySubscriptionProxy_notifyCallbacks(result) {
|
|
213
|
+
for (const callback of __classPrivateFieldGet(this, _QuerySubscriptionProxy_callbacks, "f")) {
|
|
214
|
+
try {
|
|
215
|
+
callback(result);
|
|
216
|
+
}
|
|
217
|
+
catch (e) {
|
|
218
|
+
console.error('Error in query subscription callback:', e);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* PerspectiveProxy provides a high-level interface for working with AD4M Perspectives - agent-centric semantic graphs
|
|
224
|
+
* that store and organize links between expressions.
|
|
225
|
+
*
|
|
226
|
+
* A Perspective is fundamentally a collection of links (subject-predicate-object triples) that represent an agent's view
|
|
227
|
+
* of their digital world. Through PerspectiveProxy, you can:
|
|
228
|
+
* - Add, remove, and query links
|
|
229
|
+
* - Work with Social DNA (subject classes and flows)
|
|
230
|
+
* - Subscribe to real-time updates
|
|
231
|
+
* - Share perspectives as Neighbourhoods
|
|
232
|
+
* - Execute Prolog queries for complex graph patterns
|
|
233
|
+
*
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```typescript
|
|
237
|
+
* // Create and work with links
|
|
238
|
+
* const perspective = await ad4m.perspective.add("My Space");
|
|
239
|
+
* await perspective.add({
|
|
240
|
+
* source: "did:key:alice",
|
|
241
|
+
* predicate: "knows",
|
|
242
|
+
* target: "did:key:bob"
|
|
243
|
+
* });
|
|
244
|
+
*
|
|
245
|
+
* // Query links
|
|
246
|
+
* const friends = await perspective.get({
|
|
247
|
+
* source: "did:key:alice",
|
|
248
|
+
* predicate: "knows"
|
|
249
|
+
* });
|
|
250
|
+
*
|
|
251
|
+
* // Use Social DNA
|
|
252
|
+
* await perspective.addSdna(todoClass, "subject_class");
|
|
253
|
+
* const todo = await perspective.createSubject("Todo", "expression://123");
|
|
254
|
+
*
|
|
255
|
+
* // Subscribe to changes
|
|
256
|
+
* perspective.addListener("link-added", (link) => {
|
|
257
|
+
* console.log("New link added:", link);
|
|
258
|
+
* });
|
|
259
|
+
* ```
|
|
22
260
|
*/
|
|
23
261
|
export class PerspectiveProxy {
|
|
262
|
+
/**
|
|
263
|
+
* Creates a new PerspectiveProxy instance.
|
|
264
|
+
* Note: Don't create this directly, use ad4m.perspective.add() instead.
|
|
265
|
+
*/
|
|
24
266
|
constructor(handle, ad4m) {
|
|
25
267
|
_PerspectiveProxy_handle.set(this, void 0);
|
|
26
268
|
_PerspectiveProxy_client.set(this, void 0);
|
|
@@ -44,52 +286,255 @@ export class PerspectiveProxy {
|
|
|
44
286
|
__classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addPerspectiveLinkUpdatedListener(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, __classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, "f"));
|
|
45
287
|
__classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addPerspectiveSyncStateChangeListener(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, __classPrivateFieldGet(this, _PerspectiveProxy_perspectiveSyncStateChangeCallbacks, "f"));
|
|
46
288
|
}
|
|
47
|
-
|
|
48
|
-
|
|
289
|
+
/**
|
|
290
|
+
* Executes a set of actions on an expression with optional parameters.
|
|
291
|
+
* Used internally by Social DNA flows and subject class operations.
|
|
292
|
+
*
|
|
293
|
+
* Actions are specified as an array of commands that modify links in the perspective.
|
|
294
|
+
* Each action is an object with the following format:
|
|
295
|
+
* ```typescript
|
|
296
|
+
* {
|
|
297
|
+
* action: "addLink" | "removeLink" | "setSingleTarget" | "collectionSetter",
|
|
298
|
+
* source: string, // Usually "this" to reference the current expression
|
|
299
|
+
* predicate: string, // The predicate URI
|
|
300
|
+
* target: string // The target value or "value" for parameters
|
|
301
|
+
* }
|
|
302
|
+
* ```
|
|
303
|
+
*
|
|
304
|
+
* Available commands:
|
|
305
|
+
* - `addLink`: Creates a new link
|
|
306
|
+
* - `removeLink`: Removes an existing link
|
|
307
|
+
* - `setSingleTarget`: Removes all existing links with the same source/predicate and adds a new one
|
|
308
|
+
* - `collectionSetter`: Special command for setting collection properties
|
|
309
|
+
*
|
|
310
|
+
* When used with parameters, the special value "value" in the target field will be
|
|
311
|
+
* replaced with the actual parameter value.
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* ```typescript
|
|
315
|
+
* // Add a state link and remove an old one
|
|
316
|
+
* await perspective.executeAction([
|
|
317
|
+
* {
|
|
318
|
+
* action: "addLink",
|
|
319
|
+
* source: "this",
|
|
320
|
+
* predicate: "todo://state",
|
|
321
|
+
* target: "todo://doing"
|
|
322
|
+
* },
|
|
323
|
+
* {
|
|
324
|
+
* action: "removeLink",
|
|
325
|
+
* source: "this",
|
|
326
|
+
* predicate: "todo://state",
|
|
327
|
+
* target: "todo://ready"
|
|
328
|
+
* }
|
|
329
|
+
* ], "expression://123");
|
|
330
|
+
*
|
|
331
|
+
* // Set a property using a parameter
|
|
332
|
+
* await perspective.executeAction([
|
|
333
|
+
* {
|
|
334
|
+
* action: "setSingleTarget",
|
|
335
|
+
* source: "this",
|
|
336
|
+
* predicate: "todo://title",
|
|
337
|
+
* target: "value"
|
|
338
|
+
* }
|
|
339
|
+
* ], "expression://123", [
|
|
340
|
+
* { name: "title", value: "New Title" }
|
|
341
|
+
* ]);
|
|
342
|
+
* ```
|
|
343
|
+
*
|
|
344
|
+
* @param actions - Array of action objects to execute
|
|
345
|
+
* @param expression - Target expression address (replaces "this" in actions)
|
|
346
|
+
* @param parameters - Optional parameters that replace "value" in actions
|
|
347
|
+
* @param batchId - Optional batch ID to group this operation with others
|
|
348
|
+
*/
|
|
349
|
+
async executeAction(actions, expression, parameters, batchId) {
|
|
350
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").executeCommands(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, JSON.stringify(actions), expression, JSON.stringify(parameters), batchId);
|
|
49
351
|
}
|
|
50
|
-
/**
|
|
352
|
+
/**
|
|
353
|
+
* Retrieves links from the perspective that match the given query.
|
|
354
|
+
*
|
|
355
|
+
* @param query - Query parameters to filter links
|
|
356
|
+
* @returns Array of matching LinkExpressions
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* ```typescript
|
|
360
|
+
* // Get all links where Alice knows someone
|
|
361
|
+
* const links = await perspective.get({
|
|
362
|
+
* source: "did:key:alice",
|
|
363
|
+
* predicate: "knows"
|
|
364
|
+
* });
|
|
365
|
+
*
|
|
366
|
+
* // Get all comments on a post
|
|
367
|
+
* const comments = await perspective.get({
|
|
368
|
+
* source: "post://123",
|
|
369
|
+
* predicate: "comment"
|
|
370
|
+
* });
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
51
373
|
async get(query) {
|
|
52
374
|
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").queryLinks(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, query);
|
|
53
375
|
}
|
|
54
|
-
/**
|
|
376
|
+
/**
|
|
377
|
+
* Executes a Prolog query against the perspective's knowledge base.
|
|
378
|
+
* This is a powerful way to find complex patterns in the graph.
|
|
379
|
+
*
|
|
380
|
+
* @param query - Prolog query string
|
|
381
|
+
* @returns Query results or false if no results
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
* ```typescript
|
|
385
|
+
* // Find friends of friends
|
|
386
|
+
* const results = await perspective.infer(`
|
|
387
|
+
* triple(A, "knows", B),
|
|
388
|
+
* triple(B, "knows", C),
|
|
389
|
+
* A \= C
|
|
390
|
+
* `);
|
|
391
|
+
*
|
|
392
|
+
* // Find all active todos
|
|
393
|
+
* const todos = await perspective.infer(`
|
|
394
|
+
* instance(Todo, "Todo"),
|
|
395
|
+
* property_getter("Todo", Todo, "state", "active")
|
|
396
|
+
* `);
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
55
399
|
async infer(query) {
|
|
56
400
|
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").queryProlog(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, query);
|
|
57
401
|
}
|
|
58
|
-
/**
|
|
59
|
-
|
|
60
|
-
|
|
402
|
+
/**
|
|
403
|
+
* Adds a new link to the perspective.
|
|
404
|
+
*
|
|
405
|
+
* @param link - The link to add
|
|
406
|
+
* @param status - Whether the link should be shared in a Neighbourhood
|
|
407
|
+
* @param batchId - Optional batch ID to group this operation with others
|
|
408
|
+
* @returns The created LinkExpression
|
|
409
|
+
*
|
|
410
|
+
* @example
|
|
411
|
+
* ```typescript
|
|
412
|
+
* // Add a simple relationship
|
|
413
|
+
* await perspective.add({
|
|
414
|
+
* source: "did:key:alice",
|
|
415
|
+
* predicate: "follows",
|
|
416
|
+
* target: "did:key:bob"
|
|
417
|
+
* });
|
|
418
|
+
*
|
|
419
|
+
* // Add a local-only link
|
|
420
|
+
* await perspective.add({
|
|
421
|
+
* source: "note://123",
|
|
422
|
+
* predicate: "tag",
|
|
423
|
+
* target: "private"
|
|
424
|
+
* }, "local");
|
|
425
|
+
* ```
|
|
426
|
+
*/
|
|
427
|
+
async add(link, status = 'shared', batchId) {
|
|
428
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addLink(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, link, status, batchId);
|
|
61
429
|
}
|
|
62
|
-
/**
|
|
63
|
-
|
|
64
|
-
|
|
430
|
+
/**
|
|
431
|
+
* Adds multiple links to the perspective in a single operation.
|
|
432
|
+
* More efficient than adding links one by one.
|
|
433
|
+
*
|
|
434
|
+
* @param links - Array of links to add
|
|
435
|
+
* @param status - Whether the links should be shared
|
|
436
|
+
* @param batchId - Optional batch ID to group this operation with others
|
|
437
|
+
* @returns Array of created LinkExpressions
|
|
438
|
+
*/
|
|
439
|
+
async addLinks(links, status = 'shared', batchId) {
|
|
440
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addLinks(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, links, status, batchId);
|
|
65
441
|
}
|
|
66
|
-
/**
|
|
67
|
-
|
|
68
|
-
|
|
442
|
+
/**
|
|
443
|
+
* Removes multiple links from the perspective.
|
|
444
|
+
*
|
|
445
|
+
* @param links - Array of links to remove
|
|
446
|
+
* @param batchId - Optional batch ID to group this operation with others
|
|
447
|
+
* @returns Array of removed LinkExpressions
|
|
448
|
+
*/
|
|
449
|
+
async removeLinks(links, batchId) {
|
|
450
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").removeLinks(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, links, batchId);
|
|
69
451
|
}
|
|
70
|
-
/**
|
|
452
|
+
/**
|
|
453
|
+
* Applies a set of link mutations (adds and removes) in a single operation.
|
|
454
|
+
* Useful for atomic updates to the perspective.
|
|
455
|
+
*
|
|
456
|
+
* @param mutations - Object containing links to add and remove
|
|
457
|
+
* @param status - Whether new links should be shared
|
|
458
|
+
* @returns Object containing results of the mutations
|
|
459
|
+
*/
|
|
71
460
|
async linkMutations(mutations, status = 'shared') {
|
|
72
461
|
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").linkMutations(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, mutations, status);
|
|
73
462
|
}
|
|
74
|
-
/**
|
|
75
|
-
|
|
76
|
-
|
|
463
|
+
/**
|
|
464
|
+
* Adds a pre-signed LinkExpression to the perspective.
|
|
465
|
+
*
|
|
466
|
+
* @param link - The signed LinkExpression to add
|
|
467
|
+
* @param status - Whether the link should be shared
|
|
468
|
+
* @param batchId - Optional batch ID to group this operation with others
|
|
469
|
+
* @returns The added LinkExpression
|
|
470
|
+
*/
|
|
471
|
+
async addLinkExpression(link, status = 'shared', batchId) {
|
|
472
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").addLinkExpression(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, link, status, batchId);
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Updates an existing link with new data.
|
|
476
|
+
*
|
|
477
|
+
* @param oldLink - The existing link to update
|
|
478
|
+
* @param newLink - The new link data
|
|
479
|
+
* @param batchId - Optional batch ID to group this operation with others
|
|
480
|
+
*/
|
|
481
|
+
async update(oldLink, newLink, batchId) {
|
|
482
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").updateLink(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, oldLink, newLink, batchId);
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Removes a link from the perspective.
|
|
486
|
+
*
|
|
487
|
+
* @param link - The link to remove
|
|
488
|
+
* @param batchId - Optional batch ID to group this operation with others
|
|
489
|
+
*/
|
|
490
|
+
async remove(link, batchId) {
|
|
491
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").removeLink(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, link, batchId);
|
|
77
492
|
}
|
|
78
|
-
|
|
79
|
-
|
|
493
|
+
/** Creates a new batch for grouping operations */
|
|
494
|
+
async createBatch() {
|
|
495
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").createBatch(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
80
496
|
}
|
|
81
|
-
|
|
82
|
-
|
|
497
|
+
/** Commits a batch of operations */
|
|
498
|
+
async commitBatch(batchId) {
|
|
499
|
+
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").commitBatch(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, batchId);
|
|
83
500
|
}
|
|
501
|
+
/**
|
|
502
|
+
* Retrieves and renders an Expression referenced in this perspective.
|
|
503
|
+
*
|
|
504
|
+
* @param expressionURI - URI of the Expression to retrieve
|
|
505
|
+
* @returns The rendered Expression
|
|
506
|
+
*/
|
|
84
507
|
async getExpression(expressionURI) {
|
|
85
508
|
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").getExpression(expressionURI);
|
|
86
509
|
}
|
|
510
|
+
/**
|
|
511
|
+
* Creates a new Expression in the specified Language.
|
|
512
|
+
*
|
|
513
|
+
* @param content - Content for the new Expression
|
|
514
|
+
* @param languageAddress - Address of the Language to use
|
|
515
|
+
* @returns URI of the created Expression
|
|
516
|
+
*/
|
|
87
517
|
async createExpression(content, languageAddress) {
|
|
88
518
|
return await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").createExpression(content, languageAddress);
|
|
89
519
|
}
|
|
90
|
-
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
520
|
+
/**
|
|
521
|
+
* Subscribes to link changes in the perspective.
|
|
522
|
+
*
|
|
523
|
+
* @param type - Type of change to listen for
|
|
524
|
+
* @param cb - Callback function
|
|
525
|
+
*
|
|
526
|
+
* @example
|
|
527
|
+
* ```typescript
|
|
528
|
+
* // Listen for new links
|
|
529
|
+
* perspective.addListener("link-added", (link) => {
|
|
530
|
+
* console.log("New link:", link);
|
|
531
|
+
* });
|
|
532
|
+
*
|
|
533
|
+
* // Listen for removed links
|
|
534
|
+
* perspective.addListener("link-removed", (link) => {
|
|
535
|
+
* console.log("Link removed:", link);
|
|
536
|
+
* });
|
|
537
|
+
* ```
|
|
93
538
|
*/
|
|
94
539
|
async addListener(type, cb) {
|
|
95
540
|
if (type === 'link-added') {
|
|
@@ -102,16 +547,26 @@ export class PerspectiveProxy {
|
|
|
102
547
|
__classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, "f").push(cb);
|
|
103
548
|
}
|
|
104
549
|
}
|
|
105
|
-
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
550
|
+
/**
|
|
551
|
+
* Subscribes to sync state changes if this perspective is shared.
|
|
552
|
+
*
|
|
553
|
+
* @param cb - Callback function
|
|
554
|
+
*
|
|
555
|
+
* @example
|
|
556
|
+
* ```typescript
|
|
557
|
+
* perspective.addSyncStateChangeListener((state) => {
|
|
558
|
+
* console.log("Sync state:", state);
|
|
559
|
+
* });
|
|
560
|
+
* ```
|
|
108
561
|
*/
|
|
109
562
|
async addSyncStateChangeListener(cb) {
|
|
110
563
|
__classPrivateFieldGet(this, _PerspectiveProxy_perspectiveSyncStateChangeCallbacks, "f").push(cb);
|
|
111
564
|
}
|
|
112
|
-
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
565
|
+
/**
|
|
566
|
+
* Unsubscribes from link changes.
|
|
567
|
+
*
|
|
568
|
+
* @param type - Type of change to stop listening for
|
|
569
|
+
* @param cb - The callback function to remove
|
|
115
570
|
*/
|
|
116
571
|
async removeListener(type, cb) {
|
|
117
572
|
if (type === 'link-added') {
|
|
@@ -127,13 +582,20 @@ export class PerspectiveProxy {
|
|
|
127
582
|
__classPrivateFieldGet(this, _PerspectiveProxy_perspectiveLinkUpdatedCallbacks, "f").splice(index, 1);
|
|
128
583
|
}
|
|
129
584
|
}
|
|
130
|
-
/**
|
|
131
|
-
*
|
|
585
|
+
/**
|
|
586
|
+
* Creates a snapshot of the current perspective state.
|
|
587
|
+
* Useful for backup or sharing.
|
|
588
|
+
*
|
|
589
|
+
* @returns Perspective object containing all links
|
|
132
590
|
*/
|
|
133
591
|
async snapshot() {
|
|
134
592
|
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").snapshotByUUID(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
135
593
|
}
|
|
136
|
-
/**
|
|
594
|
+
/**
|
|
595
|
+
* Loads a perspective snapshot, replacing current content.
|
|
596
|
+
*
|
|
597
|
+
* @param snapshot - Perspective snapshot to load
|
|
598
|
+
*/
|
|
137
599
|
async loadSnapshot(snapshot) {
|
|
138
600
|
//Clean the input data from __typename
|
|
139
601
|
const cleanedSnapshot = JSON.parse(JSON.stringify(snapshot));
|
|
@@ -145,11 +607,21 @@ export class PerspectiveProxy {
|
|
|
145
607
|
await this.addLinkExpression(link);
|
|
146
608
|
}
|
|
147
609
|
}
|
|
148
|
-
/**
|
|
149
|
-
*
|
|
150
|
-
*
|
|
610
|
+
/**
|
|
611
|
+
* Gets a single target value matching a query.
|
|
612
|
+
* Useful when you expect only one result.
|
|
613
|
+
*
|
|
614
|
+
* @param query - Query to find the target
|
|
615
|
+
* @returns Target value or void if not found
|
|
151
616
|
*
|
|
152
|
-
*
|
|
617
|
+
* @example
|
|
618
|
+
* ```typescript
|
|
619
|
+
* // Get a user's name
|
|
620
|
+
* const name = await perspective.getSingleTarget({
|
|
621
|
+
* source: "did:key:alice",
|
|
622
|
+
* predicate: "name"
|
|
623
|
+
* });
|
|
624
|
+
* ```
|
|
153
625
|
*/
|
|
154
626
|
async getSingleTarget(query) {
|
|
155
627
|
delete query.target;
|
|
@@ -159,12 +631,21 @@ export class PerspectiveProxy {
|
|
|
159
631
|
else
|
|
160
632
|
return null;
|
|
161
633
|
}
|
|
162
|
-
/**
|
|
163
|
-
*
|
|
164
|
-
* and then add the given link.
|
|
165
|
-
* This ensures there is only one target for the given source and predicate.
|
|
634
|
+
/**
|
|
635
|
+
* Sets a single target value, removing any existing targets.
|
|
166
636
|
*
|
|
167
|
-
*
|
|
637
|
+
* @param link - Link defining the new target
|
|
638
|
+
* @param status - Whether the link should be shared
|
|
639
|
+
*
|
|
640
|
+
* @example
|
|
641
|
+
* ```typescript
|
|
642
|
+
* // Set a user's status
|
|
643
|
+
* await perspective.setSingleTarget({
|
|
644
|
+
* source: "did:key:alice",
|
|
645
|
+
* predicate: "status",
|
|
646
|
+
* target: "online"
|
|
647
|
+
* });
|
|
648
|
+
* ```
|
|
168
649
|
*/
|
|
169
650
|
async setSingleTarget(link, status = 'shared') {
|
|
170
651
|
const query = new LinkQuery({ source: link.source, predicate: link.predicate });
|
|
@@ -222,10 +703,63 @@ export class PerspectiveProxy {
|
|
|
222
703
|
* This will return all SDNA code elements in an array.
|
|
223
704
|
*/
|
|
224
705
|
async getSdna() {
|
|
225
|
-
|
|
706
|
+
// First, find all the name literals that are linked from ad4m://self with SDNA predicates
|
|
707
|
+
const sdnaPredicates = [
|
|
708
|
+
"ad4m://has_subject_class",
|
|
709
|
+
"ad4m://has_flow",
|
|
710
|
+
"ad4m://has_custom_sdna"
|
|
711
|
+
];
|
|
712
|
+
const allSdnaCode = [];
|
|
713
|
+
for (const predicate of sdnaPredicates) {
|
|
714
|
+
// Find name literals linked from ad4m://self with this predicate
|
|
715
|
+
const nameLinks = await this.get(new LinkQuery({
|
|
716
|
+
source: "ad4m://self",
|
|
717
|
+
predicate: predicate
|
|
718
|
+
}));
|
|
719
|
+
// For each name literal found, get the actual SDNA code
|
|
720
|
+
for (const nameLink of nameLinks) {
|
|
721
|
+
const nameLiteral = nameLink.data.target;
|
|
722
|
+
// Now find the SDNA code linked from this name with predicate "ad4m://sdna"
|
|
723
|
+
const sdnaLinks = await this.get(new LinkQuery({
|
|
724
|
+
source: nameLiteral,
|
|
725
|
+
predicate: "ad4m://sdna"
|
|
726
|
+
}));
|
|
727
|
+
// Extract the SDNA code from each link
|
|
728
|
+
for (const sdnaLink of sdnaLinks) {
|
|
729
|
+
const code = Literal.fromUrl(sdnaLink.data.target).get();
|
|
730
|
+
if (typeof code === 'string') {
|
|
731
|
+
allSdnaCode.push(code);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
return allSdnaCode;
|
|
737
|
+
}
|
|
738
|
+
/** Returns the Social DNA code for a specific class
|
|
739
|
+
* This will return the SDNA code for the specified class, or null if not found.
|
|
740
|
+
*/
|
|
741
|
+
async getSdnaForClass(className) {
|
|
742
|
+
// First, find the name literal for this class that is linked from ad4m://self
|
|
743
|
+
const nameLiteral = Literal.from(className);
|
|
744
|
+
const links = await this.get(new LinkQuery({
|
|
745
|
+
source: "ad4m://self",
|
|
746
|
+
target: nameLiteral.toUrl(),
|
|
747
|
+
predicate: "ad4m://has_subject_class"
|
|
748
|
+
}));
|
|
749
|
+
if (links.length === 0) {
|
|
750
|
+
return null;
|
|
751
|
+
}
|
|
752
|
+
// Now find the SDNA code linked from this name with predicate "ad4m://sdna"
|
|
753
|
+
const sdnaLinks = await this.get(new LinkQuery({
|
|
754
|
+
source: nameLiteral.toUrl(),
|
|
226
755
|
predicate: "ad4m://sdna"
|
|
227
756
|
}));
|
|
228
|
-
|
|
757
|
+
if (sdnaLinks.length === 0) {
|
|
758
|
+
return null;
|
|
759
|
+
}
|
|
760
|
+
// Extract the SDNA code from the first link
|
|
761
|
+
const code = Literal.fromUrl(sdnaLinks[0].data.target).get();
|
|
762
|
+
return typeof code === 'string' ? code : null;
|
|
229
763
|
}
|
|
230
764
|
/** Adds the given Social DNA code to the perspective's SDNA code */
|
|
231
765
|
async addSdna(name, sdnaCode, sdnaType) {
|
|
@@ -240,7 +774,7 @@ export class PerspectiveProxy {
|
|
|
240
774
|
return [];
|
|
241
775
|
}
|
|
242
776
|
}
|
|
243
|
-
async
|
|
777
|
+
async stringOrTemplateObjectToSubjectClassName(subjectClass) {
|
|
244
778
|
if (typeof subjectClass === "string")
|
|
245
779
|
return subjectClass;
|
|
246
780
|
else {
|
|
@@ -254,24 +788,37 @@ export class PerspectiveProxy {
|
|
|
254
788
|
}
|
|
255
789
|
}
|
|
256
790
|
}
|
|
257
|
-
/**
|
|
258
|
-
*
|
|
259
|
-
* conforms to the given subject class.
|
|
791
|
+
/**
|
|
792
|
+
* Creates a new subject instance of the given subject class
|
|
260
793
|
*
|
|
261
794
|
* @param subjectClass Either a string with the name of the subject class, or an object
|
|
262
|
-
* with the properties of the subject class.
|
|
263
|
-
* that matches the given properties will be used.
|
|
795
|
+
* with the properties of the subject class.
|
|
264
796
|
* @param exprAddr The address of the expression to be turned into a subject instance
|
|
797
|
+
* @param initialValues Optional initial values for properties. If provided, these will be
|
|
798
|
+
* merged with constructor actions for better performance.
|
|
799
|
+
* @param batchId Optional batch ID for grouping operations. If provided, returns the expression address
|
|
800
|
+
* instead of the subject proxy since the subject won't exist until the batch is committed.
|
|
801
|
+
* @returns A proxy object for the created subject, or just the expression address if in batch mode
|
|
265
802
|
*/
|
|
266
|
-
async createSubject(subjectClass, exprAddr) {
|
|
803
|
+
async createSubject(subjectClass, exprAddr, initialValues, batchId) {
|
|
267
804
|
let className;
|
|
268
805
|
if (typeof subjectClass === "string") {
|
|
269
806
|
className = subjectClass;
|
|
270
|
-
await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").createSubject(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, JSON.stringify({
|
|
807
|
+
await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").createSubject(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, JSON.stringify({
|
|
808
|
+
className,
|
|
809
|
+
initialValues
|
|
810
|
+
}), exprAddr, initialValues ? JSON.stringify(initialValues) : undefined, batchId);
|
|
271
811
|
}
|
|
272
812
|
else {
|
|
273
813
|
let query = this.buildQueryFromTemplate(subjectClass);
|
|
274
|
-
await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").createSubject(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, JSON.stringify({
|
|
814
|
+
await __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").createSubject(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid, JSON.stringify({
|
|
815
|
+
query,
|
|
816
|
+
initialValues
|
|
817
|
+
}), exprAddr, initialValues ? JSON.stringify(initialValues) : undefined, batchId);
|
|
818
|
+
}
|
|
819
|
+
// Skip subject proxy creation when in batch mode since the subject won't exist until batch is committed
|
|
820
|
+
if (batchId) {
|
|
821
|
+
return exprAddr;
|
|
275
822
|
}
|
|
276
823
|
return this.getSubjectProxy(exprAddr, subjectClass);
|
|
277
824
|
}
|
|
@@ -289,15 +836,17 @@ export class PerspectiveProxy {
|
|
|
289
836
|
* with the properties of the subject class. In the latter case, the first subject class
|
|
290
837
|
* that matches the given properties will be used.
|
|
291
838
|
* @param exprAddr The address of the expression to be turned into a subject instance
|
|
839
|
+
* @param batchId Optional batch ID for grouping operations. If provided, the removal will be part of the batch
|
|
840
|
+
* and won't be executed until the batch is committed.
|
|
292
841
|
*/
|
|
293
|
-
async removeSubject(subjectClass, exprAddr) {
|
|
294
|
-
let className = await this.
|
|
842
|
+
async removeSubject(subjectClass, exprAddr, batchId) {
|
|
843
|
+
let className = await this.stringOrTemplateObjectToSubjectClassName(subjectClass);
|
|
295
844
|
let result = await this.infer(`subject_class("${className}", C), destructor(C, Actions)`);
|
|
296
845
|
if (!result.length) {
|
|
297
|
-
throw "No
|
|
846
|
+
throw "No destructor found for given subject class: " + className;
|
|
298
847
|
}
|
|
299
848
|
let actions = result.map(x => eval(x.Actions));
|
|
300
|
-
await this.executeAction(actions[0], exprAddr, undefined);
|
|
849
|
+
await this.executeAction(actions[0], exprAddr, undefined, batchId);
|
|
301
850
|
}
|
|
302
851
|
/** Checks if the given expression is a subject instance of the given subject class
|
|
303
852
|
* @param expression The expression to be checked
|
|
@@ -306,7 +855,7 @@ export class PerspectiveProxy {
|
|
|
306
855
|
* that matches the given properties will be used.
|
|
307
856
|
*/
|
|
308
857
|
async isSubjectInstance(expression, subjectClass) {
|
|
309
|
-
let className = await this.
|
|
858
|
+
let className = await this.stringOrTemplateObjectToSubjectClassName(subjectClass);
|
|
310
859
|
let isInstance = false;
|
|
311
860
|
const maxAttempts = 5;
|
|
312
861
|
let attempts = 0;
|
|
@@ -329,7 +878,7 @@ export class PerspectiveProxy {
|
|
|
329
878
|
if (!await this.isSubjectInstance(base, subjectClass)) {
|
|
330
879
|
throw `Expression ${base} is not a subject instance of given class: ${JSON.stringify(subjectClass)}`;
|
|
331
880
|
}
|
|
332
|
-
let className = await this.
|
|
881
|
+
let className = await this.stringOrTemplateObjectToSubjectClassName(subjectClass);
|
|
333
882
|
let subject = new Subject(this, base, className);
|
|
334
883
|
await subject.init();
|
|
335
884
|
return subject;
|
|
@@ -391,7 +940,11 @@ export class PerspectiveProxy {
|
|
|
391
940
|
}
|
|
392
941
|
// Collect all collections of the object in a list
|
|
393
942
|
if (Object.getPrototypeOf(obj).__collections) {
|
|
394
|
-
Object.keys(Object.getPrototypeOf(obj).__collections).filter(key => key !== 'isSubjectInstance').forEach(c =>
|
|
943
|
+
Object.keys(Object.getPrototypeOf(obj).__collections).filter(key => key !== 'isSubjectInstance').forEach(c => {
|
|
944
|
+
if (!collections.includes(c)) {
|
|
945
|
+
collections.push(c);
|
|
946
|
+
}
|
|
947
|
+
});
|
|
395
948
|
}
|
|
396
949
|
else {
|
|
397
950
|
collections.push(...Object.keys(obj).filter(key => Array.isArray(obj[key])).filter(key => key !== 'isSubjectInstance'));
|
|
@@ -490,5 +1043,69 @@ export class PerspectiveProxy {
|
|
|
490
1043
|
getNeighbourhoodProxy() {
|
|
491
1044
|
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").getNeighbourhoodProxy(__classPrivateFieldGet(this, _PerspectiveProxy_handle, "f").uuid);
|
|
492
1045
|
}
|
|
1046
|
+
/**
|
|
1047
|
+
* Returns a proxy object for working with AI capabilities.
|
|
1048
|
+
*
|
|
1049
|
+
* @returns AIClient instance
|
|
1050
|
+
*
|
|
1051
|
+
* @example
|
|
1052
|
+
* ```typescript
|
|
1053
|
+
* // Use AI to analyze perspective content
|
|
1054
|
+
* const summary = await perspective.ai.summarize();
|
|
1055
|
+
*
|
|
1056
|
+
* // Generate new content
|
|
1057
|
+
* const suggestion = await perspective.ai.suggest("next action");
|
|
1058
|
+
* ```
|
|
1059
|
+
*/
|
|
1060
|
+
get ai() {
|
|
1061
|
+
return __classPrivateFieldGet(this, _PerspectiveProxy_client, "f").aiClient;
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Creates a subscription for a Prolog query that updates in real-time.
|
|
1065
|
+
*
|
|
1066
|
+
* This method:
|
|
1067
|
+
* 1. Creates the subscription on the Rust side
|
|
1068
|
+
* 2. Sets up the subscription callback
|
|
1069
|
+
* 3. Waits for the initial result to come through the subscription channel
|
|
1070
|
+
* 4. Returns a fully initialized QuerySubscriptionProxy
|
|
1071
|
+
*
|
|
1072
|
+
* The returned subscription is guaranteed to be ready to receive updates,
|
|
1073
|
+
* as this method waits for the initialization process to complete.
|
|
1074
|
+
*
|
|
1075
|
+
* The subscription will be automatically cleaned up on both frontend and backend
|
|
1076
|
+
* when dispose() is called. Make sure to call dispose() when you're done to
|
|
1077
|
+
* prevent memory leaks and ensure proper cleanup of resources.
|
|
1078
|
+
*
|
|
1079
|
+
* @param query - Prolog query string
|
|
1080
|
+
* @returns Initialized QuerySubscriptionProxy instance
|
|
1081
|
+
*
|
|
1082
|
+
* @example
|
|
1083
|
+
* ```typescript
|
|
1084
|
+
* // Subscribe to active todos
|
|
1085
|
+
* const subscription = await perspective.subscribeInfer(`
|
|
1086
|
+
* instance(Todo, "Todo"),
|
|
1087
|
+
* property_getter("Todo", Todo, "state", "active")
|
|
1088
|
+
* `);
|
|
1089
|
+
*
|
|
1090
|
+
* // Subscription is already initialized here
|
|
1091
|
+
* console.log("Initial result:", subscription.result);
|
|
1092
|
+
*
|
|
1093
|
+
* // Set up callback for future updates
|
|
1094
|
+
* subscription.onResult((todos) => {
|
|
1095
|
+
* console.log("Active todos:", todos);
|
|
1096
|
+
* });
|
|
1097
|
+
*
|
|
1098
|
+
* // Clean up subscription when done
|
|
1099
|
+
* subscription.dispose();
|
|
1100
|
+
* ```
|
|
1101
|
+
*/
|
|
1102
|
+
async subscribeInfer(query) {
|
|
1103
|
+
const subscriptionProxy = new QuerySubscriptionProxy(this.uuid, query, __classPrivateFieldGet(this, _PerspectiveProxy_client, "f"));
|
|
1104
|
+
// Start the subscription on the Rust side first to get the real subscription ID
|
|
1105
|
+
await subscriptionProxy.subscribe();
|
|
1106
|
+
// Wait for the initial result
|
|
1107
|
+
await subscriptionProxy.initialized;
|
|
1108
|
+
return subscriptionProxy;
|
|
1109
|
+
}
|
|
493
1110
|
}
|
|
494
1111
|
_PerspectiveProxy_handle = new WeakMap(), _PerspectiveProxy_client = new WeakMap(), _PerspectiveProxy_perspectiveLinkAddedCallbacks = new WeakMap(), _PerspectiveProxy_perspectiveLinkRemovedCallbacks = new WeakMap(), _PerspectiveProxy_perspectiveLinkUpdatedCallbacks = new WeakMap(), _PerspectiveProxy_perspectiveSyncStateChangeCallbacks = new WeakMap();
|