@coasys/ad4m 0.13.1-staging.2 → 0.13.1-staging.4
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 +610 -208
- package/lib/index.js +607 -209
- package/lib/src/Ad4mClient.d.ts +7 -0
- package/lib/src/Ad4mClient.js +9 -0
- package/lib/src/agent/AgentClient.d.ts +16 -1
- package/lib/src/agent/AgentClient.js +21 -2
- package/lib/src/ai/AITypes.d.ts +6 -2
- package/lib/src/apiClient.d.ts +13 -0
- package/lib/src/apiClient.js +67 -12
- package/lib/src/generated/api/Apps.d.ts +4 -0
- package/lib/src/generated/api/AuthInfo.d.ts +4 -0
- package/lib/src/generated/api/Events.d.ts +10 -12
- package/lib/src/generated/api/FlowStateChangedEvent.d.ts +3 -2
- package/lib/src/generated/api/Model.d.ts +4 -0
- package/lib/src/generated/api/ModelApi.d.ts +5 -1
- package/lib/src/generated/api/ModelApiInput.d.ts +7 -1
- package/lib/src/generated/api/Notification.d.ts +5 -1
- package/lib/src/generated/api/NotificationInput.d.ts +4 -0
- package/lib/src/generated/api/PerspectiveEvent.d.ts +8 -0
- package/lib/src/generated/api/{PerspectiveLinkWithOwner.d.ts → PerspectiveLinkEvent.d.ts} +4 -2
- package/lib/src/generated/api/{PerspectiveLinkUpdatedWithOwner.d.ts → PerspectiveLinkUpdatedEvent.d.ts} +4 -2
- package/lib/src/generated/api/PerspectiveRemovedEvent.d.ts +7 -0
- package/lib/src/generated/api/RequestCapabilityRequest.d.ts +1 -0
- package/lib/src/generated/api/RpcMethods.d.ts +10 -1
- package/lib/src/generated/api/RuntimeInfo.d.ts +5 -0
- package/lib/src/generated/api/RuntimeUpdateNotificationParams.d.ts +6 -1
- package/lib/src/generated/api/UsersRevokeAllSessionsParams.d.ts +7 -0
- package/{src/generated/api/PerspectiveRemovedWithOwner.ts → lib/src/generated/api/UsersRevokeAllSessionsParams.js} +1 -2
- package/lib/src/index.d.ts +1 -0
- package/lib/src/index.js +1 -0
- package/lib/src/model/Ad4mModel.js +45 -18
- package/lib/src/model/ModelQueryBuilder.d.ts +7 -3
- package/lib/src/model/ModelQueryBuilder.js +9 -4
- package/lib/src/model/query-common.js +7 -1
- package/lib/src/model/types.d.ts +45 -2
- package/lib/src/model/types.js +8 -0
- package/lib/src/perspectives/Container.d.ts +41 -0
- package/lib/src/perspectives/Container.js +62 -0
- package/lib/src/perspectives/PerspectiveProxy.d.ts +6 -3
- package/lib/src/perspectives/PerspectiveProxy.js +10 -4
- package/lib/src/perspectives/WakerSubscriptionManager.d.ts +94 -12
- package/lib/src/perspectives/WakerSubscriptionManager.js +276 -75
- package/lib/src/runtime/RuntimeClient.d.ts +6 -3
- package/lib/src/runtime/RuntimeClient.js +2 -0
- package/lib/src/runtime/RuntimeTypes.d.ts +7 -1
- package/package.json +2 -2
- package/src/Ad4mClient.test.ts +54 -19
- package/src/Ad4mClient.ts +10 -0
- package/src/agent/AgentClient.ts +26 -2
- package/src/ai/AITypes.ts +6 -2
- package/src/apiClient.test.ts +128 -3
- package/src/apiClient.ts +64 -11
- package/src/generated/api/Apps.ts +6 -1
- package/src/generated/api/AuthInfo.ts +6 -1
- package/src/generated/api/Events.ts +10 -12
- package/src/generated/api/FlowStateChangedEvent.ts +3 -2
- package/src/generated/api/Model.ts +4 -0
- package/src/generated/api/ModelApi.ts +5 -1
- package/src/generated/api/ModelApiInput.ts +8 -1
- package/src/generated/api/Notification.ts +5 -1
- package/src/generated/api/NotificationInput.ts +6 -1
- package/src/generated/api/PerspectiveEvent.ts +7 -0
- package/src/generated/api/{PerspectiveLinkUpdatedWithOwner.ts → PerspectiveLinkEvent.ts} +4 -1
- package/src/generated/api/PerspectiveLinkUpdatedEvent.ts +7 -0
- package/src/generated/api/PerspectiveRemovedEvent.ts +6 -0
- package/src/generated/api/RequestCapabilityRequest.ts +1 -1
- package/src/generated/api/RpcMethods.ts +4 -1
- package/src/generated/api/RuntimeInfo.ts +6 -1
- package/src/generated/api/RuntimeUpdateNotificationParams.ts +7 -1
- package/src/generated/api/UsersRevokeAllSessionsParams.ts +8 -0
- package/src/index.ts +1 -0
- package/src/model/Ad4mModel.ts +51 -19
- package/src/model/ModelQueryBuilder.ts +9 -6
- package/src/model/containment-scope.test.ts +104 -0
- package/src/model/query-common.ts +10 -1
- package/src/model/types.ts +56 -2
- package/src/perspectives/Container.test.ts +65 -0
- package/src/perspectives/Container.ts +46 -0
- package/src/perspectives/PerspectiveProxy.test.ts +11 -0
- package/src/perspectives/PerspectiveProxy.ts +10 -4
- package/src/perspectives/WakerSubscriptionManager.test.ts +400 -82
- package/src/perspectives/WakerSubscriptionManager.ts +321 -85
- package/src/perspectives/link-updated-type.test.ts +2 -2
- package/src/perspectives/listener-leaks.test.ts +3 -3
- package/src/runtime/RuntimeClient.ts +6 -3
- package/src/runtime/RuntimeTypes.ts +10 -1
- package/lib/src/generated/api/MessageReceivedEvent.d.ts +0 -7
- package/lib/src/generated/api/PerspectiveRemovedWithOwner.d.ts +0 -5
- package/lib/src/generated/api/PerspectiveWithOwner.d.ts +0 -6
- package/lib/src/generated/api/PerspectiveWithOwner.js +0 -1
- package/src/generated/api/MessageReceivedEvent.ts +0 -7
- package/src/generated/api/PerspectiveLinkWithOwner.ts +0 -4
- package/src/generated/api/PerspectiveWithOwner.ts +0 -4
- /package/lib/src/generated/api/{MessageReceivedEvent.js → PerspectiveEvent.js} +0 -0
- /package/lib/src/generated/api/{PerspectiveLinkUpdatedWithOwner.js → PerspectiveLinkEvent.js} +0 -0
- /package/lib/src/generated/api/{PerspectiveLinkWithOwner.js → PerspectiveLinkUpdatedEvent.js} +0 -0
- /package/lib/src/generated/api/{PerspectiveRemovedWithOwner.js → PerspectiveRemovedEvent.js} +0 -0
|
@@ -3,4 +3,8 @@ import type { LocalModel } from "./LocalModel";
|
|
|
3
3
|
import type { ModelApi } from "./ModelApi";
|
|
4
4
|
import type { ModelType } from "./ModelType";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* A model as RPC results show it. Its provider key never leaves the executor:
|
|
8
|
+
* `api.hasApiKey` says whether one is set.
|
|
9
|
+
*/
|
|
6
10
|
export type Model = { id: string, name: string, api: ModelApi | null, local: LocalModel | null, modelType: ModelType, };
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
|
2
2
|
import type { ModelApiType } from "./ModelApiType";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* A remote model's endpoint as RPC results show it: whether a provider key is set,
|
|
6
|
+
* never the key.
|
|
7
|
+
*/
|
|
8
|
+
export type ModelApi = { baseUrl: string, hasApiKey: boolean, model: string, apiType: ModelApiType,
|
|
5
9
|
/**
|
|
6
10
|
* Ceiling for the context window requested from the provider, in tokens.
|
|
7
11
|
*
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
|
2
2
|
|
|
3
|
-
export type ModelApiInput = { baseUrl: string,
|
|
3
|
+
export type ModelApiInput = { baseUrl: string,
|
|
4
|
+
/**
|
|
5
|
+
* The provider key. An empty key stores no key, and so does `ai.addModel` without
|
|
6
|
+
* one. `ai.updateModel` without one keeps the stored key; it refuses the update if
|
|
7
|
+
* `baseUrl` changes too, because a key goes only to the endpoint it was set for.
|
|
8
|
+
* Never serialised back out.
|
|
9
|
+
*/
|
|
10
|
+
apiKey?: string, model: string, apiType: string,
|
|
4
11
|
/**
|
|
5
12
|
* Optional ceiling for the provider's context window, in tokens.
|
|
6
13
|
* See [`crate::types::ModelApi::max_num_ctx`].
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* A notification as RPC results and events show it: whether a webhook credential is
|
|
5
|
+
* set, never the credential.
|
|
6
|
+
*/
|
|
7
|
+
export type Notification = { id: string, granted: boolean, description: string, appName: string, appUrl: string, appIconPath: string, trigger: string, perspectiveIds: Array<string>, webhookUrl: string, hasWebhookAuth: boolean,
|
|
4
8
|
/**
|
|
5
9
|
* The owner's DID: a managed user's, or the main agent's.
|
|
6
10
|
*/
|
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
|
2
2
|
|
|
3
|
-
export type NotificationInput = { description: string, appName: string, appUrl: string, appIconPath?: string, trigger: string, perspectiveIds: Array<string>, webhookUrl: string,
|
|
3
|
+
export type NotificationInput = { description: string, appName: string, appUrl: string, appIconPath?: string, trigger: string, perspectiveIds: Array<string>, webhookUrl: string,
|
|
4
|
+
/**
|
|
5
|
+
* Sent as the bearer credential to `webhookUrl`. Results and events say only
|
|
6
|
+
* whether one is set (`hasWebhookAuth`).
|
|
7
|
+
*/
|
|
8
|
+
webhookAuth: string, };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
|
2
|
+
import type { PerspectiveHandle } from "./PerspectiveHandle";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `perspective-added` / `perspective-updated`: one event to all of the perspective's owners.
|
|
6
|
+
*/
|
|
7
|
+
export type PerspectiveEvent = { perspectiveUuid: string, perspective: PerspectiveHandle, };
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
|
2
2
|
import type { DecoratedLinkExpression } from "./DecoratedLinkExpression";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* `link-added` / `link-removed`: one event to all of the perspective's owners.
|
|
6
|
+
*/
|
|
7
|
+
export type PerspectiveLinkEvent = { perspectiveUuid: string, link: DecoratedLinkExpression, };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
|
2
|
+
import type { DecoratedLinkExpression } from "./DecoratedLinkExpression";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `link-updated`: one event to all of the perspective's owners.
|
|
6
|
+
*/
|
|
7
|
+
export type PerspectiveLinkUpdatedEvent = { perspectiveUuid: string, newLink: DecoratedLinkExpression, oldLink: DecoratedLinkExpression, };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `perspective-removed`: one event to all of the perspective's owners.
|
|
5
|
+
*/
|
|
6
|
+
export type PerspectiveRemovedEvent = { perspectiveUuid: string, uuid: string, };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
|
2
2
|
import type { AuthInfoInput } from "./AuthInfoInput";
|
|
3
3
|
|
|
4
|
-
export type RequestCapabilityRequest = { authInfo: AuthInfoInput, };
|
|
4
|
+
export type RequestCapabilityRequest = { authInfo: AuthInfoInput, agent?: string, };
|
|
@@ -175,6 +175,7 @@ import type { UsersEmailTestParams } from "./UsersEmailTestParams";
|
|
|
175
175
|
import type { UsersEmailTestResult } from "./UsersEmailTestResult";
|
|
176
176
|
import type { UsersLoginParams } from "./UsersLoginParams";
|
|
177
177
|
import type { UsersRequestVerificationParams } from "./UsersRequestVerificationParams";
|
|
178
|
+
import type { UsersRevokeAllSessionsParams } from "./UsersRevokeAllSessionsParams";
|
|
178
179
|
import type { UsersSetCreditsParams } from "./UsersSetCreditsParams";
|
|
179
180
|
import type { UsersVerifyEmailParams } from "./UsersVerifyEmailParams";
|
|
180
181
|
import type { ValidOutput } from "./ValidOutput";
|
|
@@ -323,7 +324,7 @@ export interface RpcMethods {
|
|
|
323
324
|
"runtime.importData": { params: ImportRequest; result: RuntimeImportResult };
|
|
324
325
|
"runtime.inbox": { params: Record<string, never>; result: Array<PerspectiveExpression> };
|
|
325
326
|
"runtime.info": { params: Record<string, never>; result: RuntimeInfo };
|
|
326
|
-
"runtime.linkLanguageTemplates": { params: Record<string, never>; result: Array<
|
|
327
|
+
"runtime.linkLanguageTemplates": { params: Record<string, never>; result: Array<LanguageMeta> };
|
|
327
328
|
"runtime.networkMetrics": { params: Record<string, never>; result: string };
|
|
328
329
|
"runtime.notifications": { params: Record<string, never>; result: Array<Notification> };
|
|
329
330
|
"runtime.openLink": { params: OpenLinkRequest; result: boolean };
|
|
@@ -371,8 +372,10 @@ export interface RpcMethods {
|
|
|
371
372
|
"user.freeAccess": { params: SetUserFreeAccessRequest; result: boolean };
|
|
372
373
|
"user.list": { params: Record<string, never>; result: Array<UserStatistics> };
|
|
373
374
|
"user.login": { params: UsersLoginParams; result: string };
|
|
375
|
+
"user.logout": { params: Record<string, never>; result: boolean };
|
|
374
376
|
"user.multiUserEnabled": { params: Record<string, never>; result: boolean };
|
|
375
377
|
"user.requestVerification": { params: UsersRequestVerificationParams; result: VerificationRequestResult };
|
|
378
|
+
"user.revokeAllSessions": { params: UsersRevokeAllSessionsParams; result: boolean };
|
|
376
379
|
"user.setMultiUserEnabled": { params: SetMultiUserRequest; result: boolean };
|
|
377
380
|
"user.verifyEmail": { params: UsersVerifyEmailParams; result: string };
|
|
378
381
|
"user.wallet": { params: UsersEmailParams; result: string };
|
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
|
2
2
|
|
|
3
|
-
export type RuntimeInfo = { ad4mExecutorVersion: string,
|
|
3
|
+
export type RuntimeInfo = { ad4mExecutorVersion: string,
|
|
4
|
+
/**
|
|
5
|
+
* The commit the executor was built from, or nothing when the build could not tell (no git
|
|
6
|
+
* checkout). Every build between two releases reports one version; this names the build.
|
|
7
|
+
*/
|
|
8
|
+
ad4mExecutorCommit: string | null, isInitialized: boolean, isUnlocked: boolean, };
|
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
|
2
2
|
|
|
3
|
-
export type RuntimeUpdateNotificationParams = { id: string, description: string, appName: string, appUrl: string, appIconPath?: string, trigger: string, perspectiveIds: Array<string>, webhookUrl: string,
|
|
3
|
+
export type RuntimeUpdateNotificationParams = { id: string, description: string, appName: string, appUrl: string, appIconPath?: string, trigger: string, perspectiveIds: Array<string>, webhookUrl: string,
|
|
4
|
+
/**
|
|
5
|
+
* Without it the update keeps the stored credential, and fails if `webhookUrl`
|
|
6
|
+
* changes too, because a credential goes only to the webhook it was set for. An empty
|
|
7
|
+
* string clears it.
|
|
8
|
+
*/
|
|
9
|
+
webhookAuth?: string, };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
|
2
|
+
|
|
3
|
+
export type UsersRevokeAllSessionsParams = {
|
|
4
|
+
/**
|
|
5
|
+
* The user whose sessions to revoke. A user may leave it out or name themselves; the
|
|
6
|
+
* operator must name the user.
|
|
7
|
+
*/
|
|
8
|
+
email?: string, };
|
package/src/index.ts
CHANGED
|
@@ -21,6 +21,7 @@ export * from "./perspectives/AutoProcessor";
|
|
|
21
21
|
export * from "./perspectives/InterpretationModels";
|
|
22
22
|
export * from "./perspectives/FlowModels";
|
|
23
23
|
export * from "./perspectives/TextContent";
|
|
24
|
+
export * from "./perspectives/Container";
|
|
24
25
|
// Also exports FlowFireOutcome / FlowProposeResult, the result shapes of the
|
|
25
26
|
// flow write API — they live beside the `proposeTransition()` surface.
|
|
26
27
|
export * from "./perspectives/FlowInstance";
|
package/src/model/Ad4mModel.ts
CHANGED
|
@@ -18,13 +18,13 @@ import {
|
|
|
18
18
|
normalizeValue,
|
|
19
19
|
} from "./hydration";
|
|
20
20
|
import type {
|
|
21
|
-
Scope, IncludeMap, Query,
|
|
21
|
+
Scope, TraverseScope, IncludeMap, Query,
|
|
22
22
|
GetOptions, AllInstancesResult, ResultsWithTotalCount,
|
|
23
23
|
PaginationResult, PropertyMetadata, RelationMetadata, ModelMetadata,
|
|
24
24
|
IncludeProjection,
|
|
25
25
|
TypedQuery, IncludeExtras, IncludeOf, LinksMap,
|
|
26
26
|
} from "./types";
|
|
27
|
-
import { isTraverseScope } from "./types";
|
|
27
|
+
import { isContainedScope, isContainmentTraversal, isTraverseScope } from "./types";
|
|
28
28
|
|
|
29
29
|
|
|
30
30
|
|
|
@@ -373,6 +373,37 @@ function jsonToModelInstance<T extends Ad4mModel>(
|
|
|
373
373
|
return instance;
|
|
374
374
|
}
|
|
375
375
|
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Put `childId` under `parent`. A containment scope `{ id }` writes through the
|
|
379
|
+
* built-in `Container` (#1446): the executor runs the adder of the parent's own
|
|
380
|
+
* `children` relation when its class has one (a block's `we://child`), else
|
|
381
|
+
* writes `ad4m://has_child`. The other forms write their one predicate.
|
|
382
|
+
*/
|
|
383
|
+
async function linkToParent(
|
|
384
|
+
perspective: PerspectiveProxy,
|
|
385
|
+
parent: Exclude<Scope, TraverseScope>,
|
|
386
|
+
childCtor: Function,
|
|
387
|
+
childId: string,
|
|
388
|
+
batchId?: string,
|
|
389
|
+
): Promise<void> {
|
|
390
|
+
if (isContainedScope(parent)) {
|
|
391
|
+
await perspective.updateSubject(
|
|
392
|
+
"ad4m://Container",
|
|
393
|
+
parent.id,
|
|
394
|
+
{ children: { action: "adder", value: [childId] } },
|
|
395
|
+
batchId,
|
|
396
|
+
);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
const link = new Link({
|
|
400
|
+
source: parent.id,
|
|
401
|
+
predicate: resolveParentPredicate(parent, childCtor),
|
|
402
|
+
target: childId,
|
|
403
|
+
});
|
|
404
|
+
await perspective.add(link, 'shared', batchId);
|
|
405
|
+
}
|
|
406
|
+
|
|
376
407
|
/**
|
|
377
408
|
* Base class for defining data models in AD4M.
|
|
378
409
|
*
|
|
@@ -1105,7 +1136,21 @@ export class Ad4mModel {
|
|
|
1105
1136
|
}
|
|
1106
1137
|
|
|
1107
1138
|
const queryInput: any = {};
|
|
1108
|
-
if (query.parent) {
|
|
1139
|
+
if (query.parent && isContainedScope(query.parent)) {
|
|
1140
|
+
// Containment: the executor resolves the predicates from the installed
|
|
1141
|
+
// classes, so only the id travels.
|
|
1142
|
+
queryInput.parent = { id: query.parent.id };
|
|
1143
|
+
} else if (query.parent && isContainmentTraversal(query.parent)) {
|
|
1144
|
+
// A traversal over containment: no predicate travels either.
|
|
1145
|
+
const t = query.parent;
|
|
1146
|
+
const traverse: Record<string, unknown> = { ids: t.ids };
|
|
1147
|
+
if (t.transitive !== undefined) traverse.transitive = t.transitive;
|
|
1148
|
+
if (t.direction !== undefined) traverse.direction = t.direction;
|
|
1149
|
+
if (t.limitPerAnchor !== undefined) traverse.limitPerAnchor = t.limitPerAnchor;
|
|
1150
|
+
if (t.levels !== undefined) traverse.levels = t.levels;
|
|
1151
|
+
if (t.maxDepth !== undefined) traverse.maxDepth = t.maxDepth;
|
|
1152
|
+
queryInput.parent = traverse;
|
|
1153
|
+
} else if (query.parent) {
|
|
1109
1154
|
const parentPredicate = resolveParentPredicate(query.parent, this);
|
|
1110
1155
|
// A traversal keeps its own shape on the wire: the executor reads `ids`,
|
|
1111
1156
|
// and flattening it to one `id` here is how a request for a whole level
|
|
@@ -1125,6 +1170,7 @@ export class Ad4mModel {
|
|
|
1125
1170
|
if (t.direction !== undefined) traverse.direction = t.direction;
|
|
1126
1171
|
if (t.limitPerAnchor !== undefined) traverse.limitPerAnchor = t.limitPerAnchor;
|
|
1127
1172
|
if (t.levels !== undefined) traverse.levels = t.levels;
|
|
1173
|
+
if (t.maxDepth !== undefined) traverse.maxDepth = t.maxDepth;
|
|
1128
1174
|
queryInput.parent = traverse;
|
|
1129
1175
|
} else {
|
|
1130
1176
|
queryInput.parent = { id: query.parent.id, predicate: parentPredicate };
|
|
@@ -2240,14 +2286,7 @@ export class Ad4mModel {
|
|
|
2240
2286
|
if (options?.parent && !options?.batchId) {
|
|
2241
2287
|
const batchId = await perspective.createBatch();
|
|
2242
2288
|
await instance.save(batchId);
|
|
2243
|
-
|
|
2244
|
-
const predicate = resolveParentPredicate(parent, this);
|
|
2245
|
-
const link = new Link({
|
|
2246
|
-
source: parent.id,
|
|
2247
|
-
predicate,
|
|
2248
|
-
target: instance.id,
|
|
2249
|
-
});
|
|
2250
|
-
await perspective.add(link, 'shared', batchId);
|
|
2289
|
+
await linkToParent(perspective, requireSingleParent(options.parent), this, instance.id, batchId);
|
|
2251
2290
|
await perspective.commitBatch(batchId);
|
|
2252
2291
|
// Hydrate the instance now that the batch has been committed (mirrors the
|
|
2253
2292
|
// behaviour of save() when it manages its own batch).
|
|
@@ -2259,14 +2298,7 @@ export class Ad4mModel {
|
|
|
2259
2298
|
|
|
2260
2299
|
// Create parent → child link if a parent scope was provided
|
|
2261
2300
|
if (options?.parent) {
|
|
2262
|
-
|
|
2263
|
-
const predicate = resolveParentPredicate(parent, this);
|
|
2264
|
-
const link = new Link({
|
|
2265
|
-
source: parent.id,
|
|
2266
|
-
predicate,
|
|
2267
|
-
target: instance.id,
|
|
2268
|
-
});
|
|
2269
|
-
await perspective.add(link, 'shared', options?.batchId);
|
|
2301
|
+
await linkToParent(perspective, requireSingleParent(options.parent), this, instance.id, options?.batchId);
|
|
2270
2302
|
}
|
|
2271
2303
|
|
|
2272
2304
|
return instance;
|
|
@@ -188,9 +188,10 @@ export class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
188
188
|
* 3. **String id + model class** — same metadata scan (or field lookup if
|
|
189
189
|
* options include `field`).
|
|
190
190
|
* 4. **String id + string predicate** — raw escape hatch, no metadata lookup.
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
191
|
+
* 5. **String id alone** — containment through the built-in `Container`:
|
|
192
|
+
* the id's `ad4m://has_child` targets, and the targets of the `children`
|
|
193
|
+
* relation of every class the id is an instance of. The executor
|
|
194
|
+
* resolves the predicates; nothing is looked up here.
|
|
194
195
|
*
|
|
195
196
|
* @param idOrInstance - The parent's expression URI **or** an Ad4mModel instance
|
|
196
197
|
* @param modelOrPredicate - A model class (predicate auto-resolved) **or** a raw predicate string
|
|
@@ -213,6 +214,9 @@ export class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
213
214
|
*
|
|
214
215
|
* // String id + raw predicate (escape hatch)
|
|
215
216
|
* Recipe.query(perspective).parent(cookbookId, "cookbook://recipe").get();
|
|
217
|
+
*
|
|
218
|
+
* // String id alone: whatever cookbookId contains
|
|
219
|
+
* Recipe.query(perspective).parent(cookbookId).get();
|
|
216
220
|
* ```
|
|
217
221
|
*/
|
|
218
222
|
parent(idOrInstance: string | Ad4mModel, modelOrPredicate?: typeof Ad4mModel | string | { field: string }, options?: { field?: string }): ModelQueryBuilder<T> {
|
|
@@ -242,9 +246,8 @@ export class ModelQueryBuilder<T extends Ad4mModel> {
|
|
|
242
246
|
// Ad4mModel instance — derive model class from constructor
|
|
243
247
|
this.queryParams.parent = { id, model: idOrInstance.constructor as typeof Ad4mModel, ...(field && { field }) };
|
|
244
248
|
} else {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
);
|
|
249
|
+
// String id alone → containment form of Scope
|
|
250
|
+
this.queryParams.parent = { id };
|
|
248
251
|
}
|
|
249
252
|
return this;
|
|
250
253
|
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The containment scope `{ id }` (#1446): sent to the executor as the id
|
|
3
|
+
* alone, which resolves the predicates through the built-in `Container`.
|
|
4
|
+
*/
|
|
5
|
+
import { Ad4mModel } from "./Ad4mModel";
|
|
6
|
+
import { Model, Property } from "./decorators";
|
|
7
|
+
import { ModelQueryBuilder } from "./ModelQueryBuilder";
|
|
8
|
+
import { resolveParentPredicate } from "./query-common";
|
|
9
|
+
import { isContainedScope } from "./types";
|
|
10
|
+
|
|
11
|
+
@Model({ name: "ScopedNote" })
|
|
12
|
+
class ScopedNote extends Ad4mModel {
|
|
13
|
+
@Property({ through: "test://note_body" })
|
|
14
|
+
body: string = "";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@Model({ name: "ScopedTask" })
|
|
18
|
+
class ScopedTask extends Ad4mModel {
|
|
19
|
+
@Property({ through: "test://task_title" })
|
|
20
|
+
title: string = "";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe("containment scope { id }", () => {
|
|
24
|
+
it("travels as the id alone", () => {
|
|
25
|
+
const { queryJson } = (ScopedNote as any).prepareModelQueryParams({ parent: { id: "ad4m://p" } });
|
|
26
|
+
expect(JSON.parse(queryJson).parent).toEqual({ id: "ad4m://p" });
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("is what .parent(id) with no second argument asks for", () => {
|
|
30
|
+
const builder: any = new ModelQueryBuilder({} as any, ScopedNote).parent("ad4m://p");
|
|
31
|
+
expect(builder.queryParams.parent).toEqual({ id: "ad4m://p" });
|
|
32
|
+
expect(isContainedScope(builder.queryParams.parent)).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("is one scope over a union: every class sends the same", () => {
|
|
36
|
+
const { queryJson } = Ad4mModel.prepareUnionQueryParams([ScopedNote, ScopedTask] as any, {
|
|
37
|
+
parent: { id: "ad4m://p" },
|
|
38
|
+
});
|
|
39
|
+
expect(JSON.parse(queryJson).parent).toEqual({ id: "ad4m://p" });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("names no single predicate, so the client-side resolver refuses it", () => {
|
|
43
|
+
expect(() => resolveParentPredicate({ id: "ad4m://p" }, ScopedNote)).toThrow(
|
|
44
|
+
"containment scope",
|
|
45
|
+
);
|
|
46
|
+
expect(isContainedScope({ id: "x", predicate: "p" })).toBe(false);
|
|
47
|
+
expect(isContainedScope({ ids: ["x"] } as any)).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("create({ parent: { id } })", () => {
|
|
52
|
+
it("puts the record under the parent through the built-in Container", async () => {
|
|
53
|
+
const calls: any[][] = [];
|
|
54
|
+
const client: any = new Proxy({}, {
|
|
55
|
+
get: (_t, key) => async (...args: any[]) => {
|
|
56
|
+
calls.push([key, ...args]);
|
|
57
|
+
return true;
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
const { PerspectiveProxy } = await import("../perspectives/PerspectiveProxy");
|
|
61
|
+
const proxy = new PerspectiveProxy({ uuid: "p" } as any, client);
|
|
62
|
+
const note = await ScopedNote.create(proxy as any, { body: "hi" }, {
|
|
63
|
+
parent: { id: "ad4m://p" },
|
|
64
|
+
batchId: "b",
|
|
65
|
+
});
|
|
66
|
+
const writes = calls.filter(([key]) => key === "updateSubject" || key === "addLink");
|
|
67
|
+
expect(writes).toEqual([
|
|
68
|
+
[
|
|
69
|
+
"updateSubject",
|
|
70
|
+
"p",
|
|
71
|
+
"ad4m://Container",
|
|
72
|
+
"ad4m://p",
|
|
73
|
+
JSON.stringify({ children: { action: "adder", value: [note.id] } }),
|
|
74
|
+
"b",
|
|
75
|
+
],
|
|
76
|
+
]);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("containment traversal { ids } with no predicate", () => {
|
|
81
|
+
it("travels without a predicate, with maxDepth", () => {
|
|
82
|
+
const { queryJson } = (ScopedNote as any).prepareModelQueryParams({
|
|
83
|
+
parent: { ids: ["ad4m://m"], direction: "in", transitive: true, maxDepth: 3 },
|
|
84
|
+
});
|
|
85
|
+
expect(JSON.parse(queryJson).parent).toEqual({
|
|
86
|
+
ids: ["ad4m://m"],
|
|
87
|
+
direction: "in",
|
|
88
|
+
transitive: true,
|
|
89
|
+
maxDepth: 3,
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("a traversal over a predicate still sends it", () => {
|
|
94
|
+
const { queryJson } = (ScopedNote as any).prepareModelQueryParams({
|
|
95
|
+
parent: { ids: ["ad4m://m"], predicate: "test://p", transitive: true, maxDepth: 2 },
|
|
96
|
+
});
|
|
97
|
+
expect(JSON.parse(queryJson).parent).toEqual({
|
|
98
|
+
ids: ["ad4m://m"],
|
|
99
|
+
predicate: "test://p",
|
|
100
|
+
transitive: true,
|
|
101
|
+
maxDepth: 2,
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import type { Scope, TraverseScope } from "./types";
|
|
6
|
-
import { isTraverseScope } from "./types";
|
|
6
|
+
import { isContainedScope, isContainmentTraversal, isTraverseScope } from "./types";
|
|
7
7
|
import { getRelationsMetadata } from "./decorators";
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -40,6 +40,15 @@ export function resolveParentPredicate(
|
|
|
40
40
|
// Raw form — explicit predicate
|
|
41
41
|
if ('predicate' in parent) return parent.predicate;
|
|
42
42
|
|
|
43
|
+
// Containment form — one predicate per containment edge, resolved by the
|
|
44
|
+
// executor from the installed classes. There is no single one to return.
|
|
45
|
+
if (isContainedScope(parent) || isContainmentTraversal(parent)) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
'parent(): a containment scope { id } names no single predicate; the executor resolves ' +
|
|
48
|
+
'it. Use the model query path (findAll / query / subscribe) to read it.',
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
43
52
|
// Model form — resolve from relation metadata
|
|
44
53
|
const { model } = parent;
|
|
45
54
|
const relMeta = getRelationsMetadata(model);
|
package/src/model/types.ts
CHANGED
|
@@ -103,11 +103,43 @@ export type Order = { [propertyName: string]: "ASC" | "DESC" };
|
|
|
103
103
|
* **Traverse form** — bounded traversal from one or more anchors, for reading
|
|
104
104
|
* a tree rather than one node's children. Read-only: AutoProcessor's write
|
|
105
105
|
* scopes reject it, because it names no single parent to write under.
|
|
106
|
+
*
|
|
107
|
+
* **Containment form** — `{ id }` alone: everything contained in `id`, through
|
|
108
|
+
* the built-in `Container`. That is the targets of `id`'s `ad4m://has_child`
|
|
109
|
+
* links, whatever `id` is, and the targets of the `children` relation (by name,
|
|
110
|
+
* or aliased to `children`) of every class `id` is an instance of. The caller
|
|
111
|
+
* names no class and no predicate; the executor resolves them from the
|
|
112
|
+
* installed classes. See {@link ContainedScope}.
|
|
106
113
|
*/
|
|
107
114
|
export type Scope =
|
|
108
115
|
| { model: typeof Ad4mModel; id: string; field?: string }
|
|
109
116
|
| { id: string; predicate: string }
|
|
110
|
-
| TraverseScope
|
|
117
|
+
| TraverseScope
|
|
118
|
+
| ContainedScope;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Contained in `id`, through `Container`: `{ id }` and nothing else. A
|
|
122
|
+
* misspelt extra field (`{ id, predicat }`) is an error in the executor,
|
|
123
|
+
* never a containment read.
|
|
124
|
+
*/
|
|
125
|
+
export interface ContainedScope {
|
|
126
|
+
id: string;
|
|
127
|
+
// Never set: they make the other forms. Declared so the type does not
|
|
128
|
+
// also describe `{ id, predicate }` or `{ id, model }`.
|
|
129
|
+
predicate?: never;
|
|
130
|
+
model?: never;
|
|
131
|
+
field?: never;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Whether this traversal follows containment rather than a named predicate. */
|
|
135
|
+
export function isContainmentTraversal(scope: Scope): scope is TraverseByContainment {
|
|
136
|
+
return isTraverseScope(scope) && !('predicate' in scope && scope.predicate) && !('model' in scope && scope.model);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Whether this scope is the containment form, `{ id }` alone. */
|
|
140
|
+
export function isContainedScope(scope: Scope): scope is ContainedScope {
|
|
141
|
+
return !isTraverseScope(scope) && !('predicate' in scope) && !('model' in scope);
|
|
142
|
+
}
|
|
111
143
|
|
|
112
144
|
/**
|
|
113
145
|
* Walk a predicate from several anchors at once, and optionally all the way
|
|
@@ -121,7 +153,24 @@ export type Scope =
|
|
|
121
153
|
* or by the model and relation that own it. See {@link TraverseByPredicate} and
|
|
122
154
|
* {@link TraverseByModel}.
|
|
123
155
|
*/
|
|
124
|
-
export type TraverseScope = TraverseByPredicate | TraverseByModel;
|
|
156
|
+
export type TraverseScope = TraverseByPredicate | TraverseByModel | TraverseByContainment;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A traversal over containment, through the built-in `Container`: it names no
|
|
160
|
+
* predicate, and follows every edge the executor resolves from the installed
|
|
161
|
+
* classes — `ad4m://has_child` on any node, and each class's own `children`
|
|
162
|
+
* relation on its instances. `direction: 'in'` from a record reaches whatever
|
|
163
|
+
* holds it: `Container.findAll(p, { parent: { ids: [x], direction: 'in' } })`.
|
|
164
|
+
*
|
|
165
|
+
* The rows are the queried class; the nodes a transitive walk passes through
|
|
166
|
+
* are any container. With no `order`, a transitive walk's rows come nearest
|
|
167
|
+
* first. `levels` is refused on it, and so is `limitPerAnchor` on a
|
|
168
|
+
* transitive one; bound it with `maxDepth`.
|
|
169
|
+
*/
|
|
170
|
+
export interface TraverseByContainment extends TraverseOptions {
|
|
171
|
+
predicate?: never;
|
|
172
|
+
model?: never;
|
|
173
|
+
}
|
|
125
174
|
|
|
126
175
|
/** A traversal naming its predicate literally, as the Raw scope does. */
|
|
127
176
|
export interface TraverseByPredicate extends TraverseOptions {
|
|
@@ -194,6 +243,11 @@ export interface TraverseOptions {
|
|
|
194
243
|
* every depth.
|
|
195
244
|
*/
|
|
196
245
|
limitPerAnchor?: number;
|
|
246
|
+
/**
|
|
247
|
+
* With `transitive`, stop the walk after this many hops (`1` is the direct
|
|
248
|
+
* neighbours). Refused without `transitive` and alongside `levels`.
|
|
249
|
+
*/
|
|
250
|
+
maxDepth?: number;
|
|
197
251
|
/**
|
|
198
252
|
* Walk the relation level by level, keeping this many results per anchor at each depth —
|
|
199
253
|
* `[10, 5, 3]` is "ten replies, five under each of those, three under each of *those*".
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SDK's Container mirrors the executor's built-in
|
|
3
|
+
* (`hardwired_sdna/container.json`), is read by its URI, and is never
|
|
4
|
+
* registered in a perspective (#1446).
|
|
5
|
+
*/
|
|
6
|
+
import * as fs from "fs";
|
|
7
|
+
import * as path from "path";
|
|
8
|
+
import { Container } from "./Container";
|
|
9
|
+
import { Ad4mModel } from "../model/Ad4mModel";
|
|
10
|
+
import { HasMany, Model } from "../model/decorators";
|
|
11
|
+
|
|
12
|
+
const SDNA = JSON.parse(
|
|
13
|
+
fs.readFileSync(
|
|
14
|
+
path.resolve(__dirname, "../../../rust-executor/src/perspectives/hardwired_sdna/container.json"),
|
|
15
|
+
"utf-8",
|
|
16
|
+
),
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
describe("Container", () => {
|
|
20
|
+
it("matches the executor's built-in shape", () => {
|
|
21
|
+
const { shape } = (Container as any).generateSHACL();
|
|
22
|
+
expect(shape.targetClass).toBe(SDNA.target_class);
|
|
23
|
+
expect(shape.isAbstract).toBe(SDNA.abstract);
|
|
24
|
+
expect(shape.properties.map((p: any) => [p.path, p.name])).toEqual(
|
|
25
|
+
SDNA.properties.map((p: any) => [p.path, p.name]),
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("is queried by its URI", () => {
|
|
30
|
+
expect((Container as any).prepareModelQueryParams({}).className).toBe("ad4m://Container");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("is implemented through a class's own relation, aliased to children", () => {
|
|
34
|
+
@Model({ name: "ImplBlock", implements: [Container] })
|
|
35
|
+
class ImplBlock extends Ad4mModel {
|
|
36
|
+
@HasMany({ through: "we://child", alias: { Container: "children" } })
|
|
37
|
+
blocks: string[] = [];
|
|
38
|
+
}
|
|
39
|
+
// The executor finds the relation by the alias in the shape, not by `implements`.
|
|
40
|
+
const { shape } = (ImplBlock as any).generateSHACL();
|
|
41
|
+
const blocks = shape.properties.find((p: any) => p.name === "blocks");
|
|
42
|
+
expect([blocks.path, blocks.alias]).toEqual(["we://child", { Container: "children" }]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("is not implemented by a relation that answers to another name", () => {
|
|
46
|
+
expect(() => {
|
|
47
|
+
@Model({ name: "UnaliasedBlock", implements: [Container] })
|
|
48
|
+
class UnaliasedBlock extends Ad4mModel {
|
|
49
|
+
@HasMany({ through: "we://child" })
|
|
50
|
+
blocks: string[] = [];
|
|
51
|
+
}
|
|
52
|
+
}).toThrow("UnaliasedBlock implements Container but has no relation or alias named `children`");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("register() writes nothing", async () => {
|
|
56
|
+
const { PerspectiveProxy } = await import("./PerspectiveProxy");
|
|
57
|
+
const calls: string[] = [];
|
|
58
|
+
const proxy = Object.create(PerspectiveProxy.prototype);
|
|
59
|
+
Object.defineProperty(proxy, "addSdna", { value: () => { calls.push("addSdna"); } });
|
|
60
|
+
Object.defineProperty(proxy, "addSdnaAll", { value: () => { calls.push("addSdnaAll"); } });
|
|
61
|
+
await expect(proxy.ensureSubjectClasses([Container])).resolves.toBeUndefined();
|
|
62
|
+
await expect(proxy.ensureSubjectClass(Container)).resolves.toBeUndefined();
|
|
63
|
+
expect(calls).toEqual([]);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Ad4mModel } from "../model/Ad4mModel";
|
|
2
|
+
import { HasMany, Model } from "../model/decorators";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The built-in abstract class `Container { children }` (#1446): what holds
|
|
6
|
+
* other records — a channel, a thread, a post, a call, a document, a board.
|
|
7
|
+
*
|
|
8
|
+
* X is contained in P when `P ad4m://has_child X` exists, whatever P is (an
|
|
9
|
+
* instance of any class, or a node no class describes, such as
|
|
10
|
+
* `ad4m://self`), or when P is an instance of a class whose `children`
|
|
11
|
+
* relation (by name, or aliased to `children`) links X:
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* @Model({ name: "Block", implements: [Container] })
|
|
15
|
+
* class Block extends Ad4mModel {
|
|
16
|
+
* @HasMany({ through: "we://child", alias: { Container: "children" } })
|
|
17
|
+
* blocks: string[] = [];
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // Messages held by P, through has_child or a Block's we://child:
|
|
21
|
+
* await Message.findAll(perspective, { parent: { id: P } });
|
|
22
|
+
* // Everything that holds X:
|
|
23
|
+
* await Container.findAll(perspective, { parent: { ids: [X], direction: "in" } });
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* A row's `children` is everything it holds, merged across its classes. A
|
|
27
|
+
* node no class describes is a row of its own when it holds something by
|
|
28
|
+
* `has_child`. Always scope a `Container` query (`where: { id }`, a
|
|
29
|
+
* `children` filter or `parent`): unscoped, it reads every holder in the
|
|
30
|
+
* perspective.
|
|
31
|
+
*
|
|
32
|
+
* Built in: the executor declares it (mirrors
|
|
33
|
+
* `rust-executor/src/perspectives/hardwired_sdna/container.json`) and reads
|
|
34
|
+
* it by its URI, `ad4m://Container`, so a perspective never stores it and an
|
|
35
|
+
* app class named `Container` does not replace it. `register()` on it writes
|
|
36
|
+
* nothing.
|
|
37
|
+
*/
|
|
38
|
+
@Model({ name: "Container", abstract: true })
|
|
39
|
+
export class Container extends Ad4mModel {
|
|
40
|
+
/** The URI the executor reads it by. */
|
|
41
|
+
static readonly builtinUri = "ad4m://Container";
|
|
42
|
+
|
|
43
|
+
/** What it holds, whatever their class. */
|
|
44
|
+
@HasMany({ through: "ad4m://has_child" })
|
|
45
|
+
children: string[] = [];
|
|
46
|
+
}
|