capable-kit 0.0.1-alpha.7 → 0.1.0-alpha.10
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/CHANGELOG.md +214 -0
- package/README.md +6 -0
- package/dist/ai-sdk.d.mts +3 -3
- package/dist/ai-sdk.mjs +9 -7
- package/dist/ai-sdk.mjs.map +1 -1
- package/dist/app.d.mts +2 -2
- package/dist/app.mjs +2 -2
- package/dist/bin.mjs +12 -9
- package/dist/bin.mjs.map +1 -1
- package/dist/cli.d.mts +9 -1
- package/dist/cli.mjs +1 -1
- package/dist/core.d.mts +3 -3
- package/dist/core.mjs +1 -1
- package/dist/eve.d.mts +82 -5
- package/dist/eve.mjs +364 -23
- package/dist/eve.mjs.map +1 -1
- package/dist/http.d.mts +2 -2
- package/dist/http.mjs +2 -2
- package/dist/{index-Dbc4g9WM.d.mts → index-1A44jLuE.d.mts} +54 -16
- package/dist/{index-DwXlaTXI.d.mts → index-CHuCLJIs.d.mts} +20 -4
- package/dist/{index-D2CRRUzH.d.mts → index-D-NV4PiU.d.mts} +6 -5
- package/dist/{index-Be8-UVBC.d.mts → index-WiBWfyTn.d.mts} +66 -27
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +3 -3
- package/dist/mcp.d.mts +40 -5
- package/dist/mcp.mjs +1 -1
- package/dist/mount-BmLHp349.mjs +235 -0
- package/dist/mount-BmLHp349.mjs.map +1 -0
- package/dist/react-query.d.mts +2 -2
- package/dist/react-query.mjs +1 -1
- package/dist/react-query.mjs.map +1 -1
- package/dist/runtime-support.d.mts +1 -1
- package/dist/runtime-support.mjs +8 -8
- package/dist/runtime-support.mjs.map +1 -1
- package/dist/server.d.mts +1 -1
- package/dist/server.mjs +1 -1
- package/dist/{src-Db6-XSnU.mjs → src-3LE9S4QE.mjs} +67 -38
- package/dist/src-3LE9S4QE.mjs.map +1 -0
- package/dist/{src-DdV82An-.mjs → src-B2WeUwUT.mjs} +69 -34
- package/dist/src-B2WeUwUT.mjs.map +1 -0
- package/dist/{src-CDxL6w3d.mjs → src-C5jGDnuu.mjs} +9 -6
- package/dist/src-C5jGDnuu.mjs.map +1 -0
- package/dist/src-DePlcG_t.mjs +787 -0
- package/dist/src-DePlcG_t.mjs.map +1 -0
- package/dist/{src-BF70JLxI.mjs → src-_6OmFRYa.mjs} +185 -38
- package/dist/src-_6OmFRYa.mjs.map +1 -0
- package/dist/{subscription-Bq792Ad2.d.mts → subscription-DPdwovhw.d.mts} +2 -2
- package/dist/swr.d.mts +2 -2
- package/dist/swr.mjs +1 -1
- package/dist/swr.mjs.map +1 -1
- package/docs/CONTEXT.md +42 -21
- package/docs/README.md +3 -0
- package/docs/internal/README.md +3 -0
- package/docs/internal/adr/0001-capability-is-the-semantic-boundary.md +1 -1
- package/docs/internal/adr/0002-version-the-capable-value-codec.md +24 -0
- package/docs/internal/adr/0003-capability-reference-customization.md +68 -0
- package/docs/internal/architecture.md +64 -19
- package/docs/internal/capability-first-model.md +127 -18
- package/docs/internal/eve-recovery-validation.md +86 -0
- package/docs/internal/feedback/v-ray/README.md +258 -0
- package/docs/internal/feedback/v-ray/inference.mjs +43 -0
- package/docs/internal/feedback/v-ray/reproduce.mjs +36 -0
- package/docs/internal/implementation-friction.md +481 -1
- package/docs/internal/releases.md +79 -0
- package/docs/site/capable-orpc-and-trpc.mdx +5 -5
- package/docs/site/concepts/apps.mdx +17 -8
- package/docs/site/concepts/architecture.mdx +10 -2
- package/docs/site/concepts/capabilities.mdx +41 -4
- package/docs/site/concepts/errors.mdx +11 -1
- package/docs/site/concepts/presentations.mdx +20 -0
- package/docs/site/hosting/any-fetch-host.mdx +2 -2
- package/docs/site/hosting/next-js.mdx +38 -4
- package/docs/site/hosting/standalone-server.mdx +2 -2
- package/docs/site/introduction.mdx +9 -6
- package/docs/site/patterns/composition.mdx +159 -11
- package/docs/site/patterns/remote-apps.mdx +68 -10
- package/docs/site/patterns/subscriptions.mdx +79 -32
- package/docs/site/quickstart.mdx +19 -14
- package/docs/site/reference/packages-and-entry-points.mdx +52 -5
- package/docs/site/reference/surface-metadata.mdx +7 -2
- package/docs/site/reference/the-cap-cli.mdx +2 -2
- package/docs/site/surfaces/ai-sdk.mdx +3 -3
- package/docs/site/surfaces/cli.mdx +47 -3
- package/docs/site/surfaces/eve.mdx +67 -20
- package/docs/site/surfaces/mcp.mdx +73 -5
- package/docs/site/surfaces/react-query.mdx +3 -3
- package/docs/site/surfaces/swr.mdx +3 -3
- package/docs/site/surfaces/typescript.mdx +8 -8
- package/docs/site/surfaces.mdx +1 -1
- package/package-docs.mjs +3 -0
- package/package.json +11 -11
- package/src/bin.ts +0 -0
- package/src/index.test.ts +2 -1
- package/src/index.ts +6 -1
- package/src/mcp.ts +4 -0
- package/src/package.test.ts +9 -0
- package/dist/mount-xvMsjz1B.mjs +0 -178
- package/dist/mount-xvMsjz1B.mjs.map +0 -1
- package/dist/src-BF70JLxI.mjs.map +0 -1
- package/dist/src-C2MGIlT0.mjs +0 -464
- package/dist/src-C2MGIlT0.mjs.map +0 -1
- package/dist/src-CDxL6w3d.mjs.map +0 -1
- package/dist/src-Db6-XSnU.mjs.map +0 -1
- package/dist/src-DdV82An-.mjs.map +0 -1
|
@@ -0,0 +1,787 @@
|
|
|
1
|
+
import { _ as normalizePresentation, a as isCapabilityGroup, f as subscribeLocatedCapability, h as schemaToJsonSchema, l as executeLocatedCapability, u as isCapability, x as CapableError } from "./src-B2WeUwUT.mjs";
|
|
2
|
+
//#region packages/app/src/manifest.ts
|
|
3
|
+
function createAppManifest(definition) {
|
|
4
|
+
const byCapability = new Map(definition.locatedCapabilities.map((entry) => [entry.capability, entry]));
|
|
5
|
+
return Object.freeze({
|
|
6
|
+
protocol: "capable.capabilities.v4",
|
|
7
|
+
name: definition.name,
|
|
8
|
+
version: definition.version,
|
|
9
|
+
...definition.description ? { description: definition.description } : {},
|
|
10
|
+
capabilities: manifestNodes(definition.capabilities, byCapability)
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
function validateManifest(manifest, label) {
|
|
14
|
+
if (manifest.protocol !== "capable.capabilities.v4") throw new Error(`${label} uses unsupported protocol "${manifest.protocol}"`);
|
|
15
|
+
const capabilities = flattenManifestCapabilities(manifest.capabilities);
|
|
16
|
+
const ids = /* @__PURE__ */ new Map();
|
|
17
|
+
const paths = /* @__PURE__ */ new Set();
|
|
18
|
+
for (const capability of capabilities) {
|
|
19
|
+
if (![
|
|
20
|
+
"unary",
|
|
21
|
+
"live",
|
|
22
|
+
"events"
|
|
23
|
+
].includes(capability.execution)) throw new Error(`${label} contains unsupported execution kind for Capability "${capability.id}"`);
|
|
24
|
+
if (capability.execution !== "unary" && capability.effect !== "read") throw new Error(`${label} subscribable Capability "${capability.id}" must have effect "read"`);
|
|
25
|
+
const path = capability.path.join(".");
|
|
26
|
+
const previous = ids.get(capability.id);
|
|
27
|
+
if (previous && !sameCapabilityContract(previous, capability)) throw new Error(`${label} contains conflicting definitions for Capability id "${capability.id}"`);
|
|
28
|
+
if (paths.has(path)) throw new Error(`${label} contains duplicate Capability path "${path}"`);
|
|
29
|
+
ids.set(capability.id, capability);
|
|
30
|
+
paths.add(path);
|
|
31
|
+
}
|
|
32
|
+
validateManifestPaths(manifest.capabilities, [], label);
|
|
33
|
+
}
|
|
34
|
+
function composeApplicationManifests(sources) {
|
|
35
|
+
const conflicts = collectManifestConflicts(sources);
|
|
36
|
+
if (conflicts.length) throw new Error(`Cannot compose Apps:\n${conflicts.map((item) => `- ${item}`).join("\n")}`);
|
|
37
|
+
const manifests = sources.map(({ manifest }) => manifest);
|
|
38
|
+
const applications = sources.filter((source, index) => sources.findIndex(({ application }) => application === source.application) === index).map(({ manifest }) => manifest);
|
|
39
|
+
return Object.freeze({
|
|
40
|
+
protocol: "capable.capabilities.v4",
|
|
41
|
+
name: applications.map(({ name }) => name).join(" + "),
|
|
42
|
+
version: applications.map(({ version }) => version).join(" + "),
|
|
43
|
+
capabilities: mergeManifestNodes(manifests.flatMap(({ capabilities }) => capabilities))
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function selectManifest(source, selection) {
|
|
47
|
+
validateSelectionTree(selection.include, source.capabilities, "include", []);
|
|
48
|
+
validateSelectionTree(selection.exclude, source.capabilities, "exclude", []);
|
|
49
|
+
return Object.freeze({
|
|
50
|
+
...source,
|
|
51
|
+
capabilities: filterManifestNodes(source.capabilities, selection)
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function validateSelectionTree(tree, nodes, label, parent) {
|
|
55
|
+
if (tree === void 0 || tree === true) return;
|
|
56
|
+
if (!tree || typeof tree !== "object" || Array.isArray(tree)) throw new Error(`Application selection ${label} at "${parent.join(".") || "<root>"}" must be an object or true`);
|
|
57
|
+
for (const [key, child] of Object.entries(tree)) {
|
|
58
|
+
const path = [...parent, key];
|
|
59
|
+
const node = nodes.find((candidate) => candidate.type === "group" ? candidate.key === key : candidate.path.at(-1) === key);
|
|
60
|
+
if (!node) throw new Error(`Application selection ${label} references unknown path "${path.join(".")}"`);
|
|
61
|
+
if (child === true) continue;
|
|
62
|
+
if (node.type === "capability") throw new Error(`Application selection ${label} at "${path.join(".")}" must be true`);
|
|
63
|
+
validateSelectionTree(child, node.children, label, path);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function flattenManifestCapabilities(nodes) {
|
|
67
|
+
return nodes.flatMap((node) => node.type === "capability" ? [node] : flattenManifestCapabilities(node.children));
|
|
68
|
+
}
|
|
69
|
+
function findManifestCapability(nodes, path) {
|
|
70
|
+
const node = findManifestNode(nodes, path);
|
|
71
|
+
return node?.type === "capability" ? node : void 0;
|
|
72
|
+
}
|
|
73
|
+
function findManifestNode(nodes, path) {
|
|
74
|
+
let current = nodes;
|
|
75
|
+
for (let index = 0; index < path.length; index++) {
|
|
76
|
+
const key = path[index];
|
|
77
|
+
const node = current.find((candidate) => candidate.type === "group" ? candidate.key === key : candidate.path.at(-1) === key);
|
|
78
|
+
if (!node) return void 0;
|
|
79
|
+
if (node.type === "capability") return index === path.length - 1 ? node : void 0;
|
|
80
|
+
if (index === path.length - 1) return node;
|
|
81
|
+
current = node.children;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function manifestNodes(tree, located) {
|
|
85
|
+
const nodes = [];
|
|
86
|
+
for (const [key, value] of Object.entries(tree)) {
|
|
87
|
+
if (isCapability(value)) {
|
|
88
|
+
const entry = located.get(value);
|
|
89
|
+
if (entry) nodes.push(manifestCapability(entry));
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
const decorated = isCapabilityGroup(value);
|
|
93
|
+
const children = manifestNodes(decorated ? value.capabilities : value, located);
|
|
94
|
+
nodes.push(Object.freeze({
|
|
95
|
+
type: "group",
|
|
96
|
+
key,
|
|
97
|
+
...decorated && value.title ? { title: value.title } : {},
|
|
98
|
+
...decorated && value.description ? { description: value.description } : {},
|
|
99
|
+
meta: decorated ? value.meta : {},
|
|
100
|
+
children
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
return nodes;
|
|
104
|
+
}
|
|
105
|
+
function manifestCapability(entry) {
|
|
106
|
+
const capability = entry.capability;
|
|
107
|
+
return Object.freeze({
|
|
108
|
+
type: "capability",
|
|
109
|
+
id: capability.id,
|
|
110
|
+
path: entry.path,
|
|
111
|
+
...capability.title ? { title: capability.title } : {},
|
|
112
|
+
...capability.description ? { description: capability.description } : {},
|
|
113
|
+
deprecated: capability.deprecated,
|
|
114
|
+
...capability.effect ? { effect: capability.effect } : {},
|
|
115
|
+
execution: capability.execution,
|
|
116
|
+
...capability.input ? { input: manifestJsonSchema(capability.input, "input") } : {},
|
|
117
|
+
...capability.output ? { output: manifestJsonSchema(capability.output, "output") } : {},
|
|
118
|
+
errors: Object.fromEntries(Object.entries(entry.errors).map(([code, definition]) => [code, {
|
|
119
|
+
...definition.message ? { message: definition.message } : {},
|
|
120
|
+
...typeof definition.status === "number" ? { status: definition.status } : {},
|
|
121
|
+
...definition.data ? { data: manifestJsonSchema(definition.data, "output") } : {},
|
|
122
|
+
...definition.meta ? { meta: definition.meta } : {}
|
|
123
|
+
}])),
|
|
124
|
+
meta: capability.meta
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function manifestJsonSchema(schema, direction) {
|
|
128
|
+
return schemaToJsonSchema(schema, direction, { allowUnrepresentable: true });
|
|
129
|
+
}
|
|
130
|
+
function validateManifestPaths(nodes, parent, label) {
|
|
131
|
+
for (const node of nodes) {
|
|
132
|
+
if (node.type === "group") {
|
|
133
|
+
validateManifestPaths(node.children, [...parent, node.key], label);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (!samePath$1(node.path, [...parent, node.path.at(-1)])) throw new Error(`${label} Capability "${node.id}" declares path "${node.path.join(".")}" that does not match its manifest placement`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function collectManifestConflicts(sources) {
|
|
140
|
+
const conflicts = [];
|
|
141
|
+
for (const { manifest } of sources) try {
|
|
142
|
+
validateManifest(manifest, manifest.name);
|
|
143
|
+
} catch (error) {
|
|
144
|
+
conflicts.push(error instanceof Error ? error.message : String(error));
|
|
145
|
+
}
|
|
146
|
+
const capabilities = sources.flatMap(({ manifest, owners }, source) => flattenManifestCapabilities(manifest.capabilities).map((capability) => ({
|
|
147
|
+
capability,
|
|
148
|
+
source,
|
|
149
|
+
owner: requiredOwner(owners, capability.id, manifest.name),
|
|
150
|
+
label: manifest.name
|
|
151
|
+
})));
|
|
152
|
+
for (let leftIndex = 0; leftIndex < capabilities.length; leftIndex++) for (let rightIndex = leftIndex + 1; rightIndex < capabilities.length; rightIndex++) {
|
|
153
|
+
const left = capabilities[leftIndex];
|
|
154
|
+
const right = capabilities[rightIndex];
|
|
155
|
+
if (left.source === right.source) continue;
|
|
156
|
+
const sameId = left.capability.id === right.capability.id;
|
|
157
|
+
if (samePath$1(left.capability.path, right.capability.path)) {
|
|
158
|
+
conflicts.push(sameId ? `Capability "${left.capability.id}" is placed twice at "${left.capability.path.join(".")}" by "${left.label}" and "${right.label}"` : `Path "${left.capability.path.join(".")}" contains both "${left.capability.id}" from "${left.label}" and "${right.capability.id}" from "${right.label}"`);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (sameId) {
|
|
162
|
+
if (left.owner !== right.owner) conflicts.push(`Capability id "${left.capability.id}" is owned by different Apps at "${left.capability.path.join(".")}" in "${left.label}" and "${right.capability.path.join(".")}"`);
|
|
163
|
+
}
|
|
164
|
+
if (isPathPrefix(left.capability.path, right.capability.path) || isPathPrefix(right.capability.path, left.capability.path)) {
|
|
165
|
+
const leaf = left.capability.path.length < right.capability.path.length ? left : right;
|
|
166
|
+
const subtree = leaf === left ? right : left;
|
|
167
|
+
conflicts.push(`Leaf "${leaf.capability.path.join(".")}" from "${leaf.label}" collides with subtree Capability "${subtree.capability.path.join(".")}" from "${subtree.label}"`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return [...new Set(conflicts)];
|
|
171
|
+
}
|
|
172
|
+
function requiredOwner(owners, capabilityId, application) {
|
|
173
|
+
const owner = owners.get(capabilityId);
|
|
174
|
+
if (!owner) throw new Error(`Missing execution owner for Capability "${capabilityId}" in "${application}"`);
|
|
175
|
+
return owner;
|
|
176
|
+
}
|
|
177
|
+
function sameCapabilityContract(left, right) {
|
|
178
|
+
const { path: _leftPath, customization: _leftCustomization, ...leftContract } = left;
|
|
179
|
+
const { path: _rightPath, customization: _rightCustomization, ...rightContract } = right;
|
|
180
|
+
return JSON.stringify(leftContract) === JSON.stringify(rightContract);
|
|
181
|
+
}
|
|
182
|
+
function mergeManifestNodes(nodes) {
|
|
183
|
+
const merged = /* @__PURE__ */ new Map();
|
|
184
|
+
for (const node of nodes) {
|
|
185
|
+
const key = node.type === "group" ? node.key : node.path.at(-1);
|
|
186
|
+
const previous = merged.get(key);
|
|
187
|
+
if (!previous) {
|
|
188
|
+
merged.set(key, node);
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (previous.type !== "group" || node.type !== "group") throw new Error(`Manifest node collision at "${key}"`);
|
|
192
|
+
merged.set(key, Object.freeze({
|
|
193
|
+
type: "group",
|
|
194
|
+
key,
|
|
195
|
+
...mergeGroupField("title", previous.title, node.title, key),
|
|
196
|
+
...mergeGroupField("description", previous.description, node.description, key),
|
|
197
|
+
meta: mergeGroupMeta(previous.meta, node.meta, key),
|
|
198
|
+
children: mergeManifestNodes([...previous.children, ...node.children])
|
|
199
|
+
}));
|
|
200
|
+
}
|
|
201
|
+
return [...merged.values()];
|
|
202
|
+
}
|
|
203
|
+
function mergeGroupField(field, left, right, path) {
|
|
204
|
+
if (left && right && left !== right) throw new Error(`Group "${path}" has conflicting ${field}`);
|
|
205
|
+
const value = left ?? right;
|
|
206
|
+
return value ? { [field]: value } : {};
|
|
207
|
+
}
|
|
208
|
+
function mergeGroupMeta(left, right, path) {
|
|
209
|
+
const merged = { ...left };
|
|
210
|
+
for (const [key, value] of Object.entries(right)) {
|
|
211
|
+
if (key in merged && JSON.stringify(merged[key]) !== JSON.stringify(value)) throw new Error(`Group "${path}" has conflicting metadata for "${key}"`);
|
|
212
|
+
merged[key] = value;
|
|
213
|
+
}
|
|
214
|
+
return Object.freeze(merged);
|
|
215
|
+
}
|
|
216
|
+
function filterManifestNodes(nodes, selection) {
|
|
217
|
+
const filtered = [];
|
|
218
|
+
for (const node of nodes) {
|
|
219
|
+
if (node.type === "capability") {
|
|
220
|
+
if ((selection.include ? matchesManifestSelection(selection.include, node.path) : true) && !matchesManifestSelection(selection.exclude, node.path)) filtered.push(node);
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const children = filterManifestNodes(node.children, selection);
|
|
224
|
+
if (children.length) filtered.push(Object.freeze({
|
|
225
|
+
...node,
|
|
226
|
+
children
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
229
|
+
return filtered;
|
|
230
|
+
}
|
|
231
|
+
function matchesManifestSelection(tree, path) {
|
|
232
|
+
let current = tree;
|
|
233
|
+
for (const segment of path) {
|
|
234
|
+
if (current === true) return true;
|
|
235
|
+
if (!current || typeof current !== "object" || Array.isArray(current)) return false;
|
|
236
|
+
current = current[segment];
|
|
237
|
+
}
|
|
238
|
+
return current === true || Boolean(current) && typeof current === "object" && !Array.isArray(current);
|
|
239
|
+
}
|
|
240
|
+
function samePath$1(left, right) {
|
|
241
|
+
return left.length === right.length && left.every((segment, index) => segment === right[index]);
|
|
242
|
+
}
|
|
243
|
+
function isPathPrefix(prefix, path) {
|
|
244
|
+
return prefix.length < path.length && prefix.every((segment, index) => segment === path[index]);
|
|
245
|
+
}
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region packages/app/src/runtime.ts
|
|
248
|
+
function mapAsyncIterator(source, transform, transformError = (error) => error) {
|
|
249
|
+
let failed = false;
|
|
250
|
+
return {
|
|
251
|
+
[Symbol.asyncIterator]() {
|
|
252
|
+
return this;
|
|
253
|
+
},
|
|
254
|
+
async next() {
|
|
255
|
+
try {
|
|
256
|
+
const result = await source.next();
|
|
257
|
+
return result.done ? {
|
|
258
|
+
done: true,
|
|
259
|
+
value: void 0
|
|
260
|
+
} : {
|
|
261
|
+
done: false,
|
|
262
|
+
value: await transform(result.value)
|
|
263
|
+
};
|
|
264
|
+
} catch (error) {
|
|
265
|
+
failed = true;
|
|
266
|
+
await source.return?.().catch(() => void 0);
|
|
267
|
+
throw transformError(error);
|
|
268
|
+
}
|
|
269
|
+
},
|
|
270
|
+
async return() {
|
|
271
|
+
if (failed) return {
|
|
272
|
+
done: true,
|
|
273
|
+
value: void 0
|
|
274
|
+
};
|
|
275
|
+
try {
|
|
276
|
+
await source.return?.();
|
|
277
|
+
return {
|
|
278
|
+
done: true,
|
|
279
|
+
value: void 0
|
|
280
|
+
};
|
|
281
|
+
} catch (error) {
|
|
282
|
+
throw transformError(error);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
const APP_RUNTIME_SYMBOL = Symbol.for("capable.app.runtime");
|
|
288
|
+
const APP_CAPABILITIES_SYMBOL = Symbol.for("capable.app.capabilities");
|
|
289
|
+
const APP_CALL_SYMBOL = Symbol.for("capable.app.call");
|
|
290
|
+
/** @internal Shared by composition and subscriptions; ordinary functions are not references. */
|
|
291
|
+
function getCapabilityBinding(value) {
|
|
292
|
+
return typeof value === "function" ? value[APP_CALL_SYMBOL] : void 0;
|
|
293
|
+
}
|
|
294
|
+
function getAppRuntime(app) {
|
|
295
|
+
return app[APP_RUNTIME_SYMBOL];
|
|
296
|
+
}
|
|
297
|
+
/** @internal Composition uses path bindings without exposing them to transports. */
|
|
298
|
+
function getInternalAppRuntime(app) {
|
|
299
|
+
const runtime = app[APP_RUNTIME_SYMBOL];
|
|
300
|
+
if (!("resolve" in runtime) || typeof runtime.resolve !== "function") throw new TypeError("Capable App is missing its internal path resolver");
|
|
301
|
+
return runtime;
|
|
302
|
+
}
|
|
303
|
+
function isApp(value) {
|
|
304
|
+
return Boolean(value && (typeof value === "object" || typeof value === "function") && value[APP_RUNTIME_SYMBOL]);
|
|
305
|
+
}
|
|
306
|
+
function appFromRuntime(runtime) {
|
|
307
|
+
const read = (path, root, key) => {
|
|
308
|
+
if (root && key === APP_RUNTIME_SYMBOL) return runtime;
|
|
309
|
+
if (!root && key === APP_CALL_SYMBOL) return {
|
|
310
|
+
runtime,
|
|
311
|
+
path
|
|
312
|
+
};
|
|
313
|
+
if (key === "then") return void 0;
|
|
314
|
+
if (typeof key !== "string") return void 0;
|
|
315
|
+
return proxy([...path, key], false);
|
|
316
|
+
};
|
|
317
|
+
const proxy = (path, root) => {
|
|
318
|
+
if (root) return new Proxy({}, { get(_target, key) {
|
|
319
|
+
return read(path, true, key);
|
|
320
|
+
} });
|
|
321
|
+
return new Proxy(() => void 0, {
|
|
322
|
+
get(_target, key) {
|
|
323
|
+
return read(path, false, key);
|
|
324
|
+
},
|
|
325
|
+
async apply(_target, _this, args) {
|
|
326
|
+
const signal = args[1]?.signal;
|
|
327
|
+
const binding = await runtime.resolve(path);
|
|
328
|
+
if (!binding) throw new Error(`No Capability exists at "${path.join(".")}"`);
|
|
329
|
+
if (binding.capability.execution === "events") throw wrongExecutionMode(binding.capability.id, "subscribe");
|
|
330
|
+
return (await binding.transport.call({
|
|
331
|
+
id: binding.capability.id,
|
|
332
|
+
input: args[0] ?? {},
|
|
333
|
+
...signal ? { signal } : {}
|
|
334
|
+
})).output;
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
};
|
|
338
|
+
return proxy([], true);
|
|
339
|
+
}
|
|
340
|
+
function subscribe(capabilityCall, ...args) {
|
|
341
|
+
const binding = getCapabilityBinding(capabilityCall);
|
|
342
|
+
if (!binding) throw new Error("subscribe() requires a Capability call from a Capable App");
|
|
343
|
+
return (async function* () {
|
|
344
|
+
const signal = args[1]?.signal;
|
|
345
|
+
const resolved = await binding.runtime.resolve(binding.path);
|
|
346
|
+
if (!resolved) throw new Error(`No Capability exists at "${binding.path.join(".")}"`);
|
|
347
|
+
if (resolved.capability.execution === "unary") throw wrongExecutionMode(resolved.capability.id, "call");
|
|
348
|
+
yield* mapAsyncIterator(await resolved.transport.subscribe({
|
|
349
|
+
id: resolved.capability.id,
|
|
350
|
+
input: args[0] ?? {},
|
|
351
|
+
...signal ? { signal } : {}
|
|
352
|
+
}), (result) => result.output);
|
|
353
|
+
})();
|
|
354
|
+
}
|
|
355
|
+
const localManifestCache = /* @__PURE__ */ new WeakMap();
|
|
356
|
+
const localCapabilityCache = /* @__PURE__ */ new WeakMap();
|
|
357
|
+
function createApp(definition, ...[options]) {
|
|
358
|
+
let manifest = localManifestCache.get(definition);
|
|
359
|
+
if (!manifest) {
|
|
360
|
+
manifest = createAppManifest(definition);
|
|
361
|
+
localManifestCache.set(definition, manifest);
|
|
362
|
+
}
|
|
363
|
+
let byId = localCapabilityCache.get(definition);
|
|
364
|
+
if (!byId) {
|
|
365
|
+
byId = new Map(definition.locatedCapabilities.map((entry) => [entry.capability.id, entry]));
|
|
366
|
+
localCapabilityCache.set(definition, byId);
|
|
367
|
+
}
|
|
368
|
+
const runtime = Object.freeze({
|
|
369
|
+
async manifest() {
|
|
370
|
+
return manifest;
|
|
371
|
+
},
|
|
372
|
+
async resolve(path) {
|
|
373
|
+
const capability = findManifestCapability(manifest.capabilities, path);
|
|
374
|
+
return capability ? {
|
|
375
|
+
capability,
|
|
376
|
+
transport: runtime
|
|
377
|
+
} : void 0;
|
|
378
|
+
},
|
|
379
|
+
async call({ id, input = {}, signal, presentation: wantsPresentation }) {
|
|
380
|
+
const located = byId.get(id);
|
|
381
|
+
if (!located) throw new TransportError("NOT_FOUND", { status: 404 });
|
|
382
|
+
if (located.capability.execution === "events") throw wrongExecutionMode(id, "subscribe");
|
|
383
|
+
let context;
|
|
384
|
+
try {
|
|
385
|
+
context = options?.context ? await options.context({
|
|
386
|
+
id,
|
|
387
|
+
...signal ? { signal } : {}
|
|
388
|
+
}) : void 0;
|
|
389
|
+
} catch (cause) {
|
|
390
|
+
throw new TransportError("INTERNAL_SERVER_ERROR", { cause });
|
|
391
|
+
}
|
|
392
|
+
let output;
|
|
393
|
+
try {
|
|
394
|
+
output = await executeLocatedCapability(located, input, {
|
|
395
|
+
context,
|
|
396
|
+
...signal ? { signal } : {}
|
|
397
|
+
});
|
|
398
|
+
} catch (error) {
|
|
399
|
+
if (error instanceof CapableError) throw new TransportError(error.code, {
|
|
400
|
+
...error.message ? { message: error.message } : {},
|
|
401
|
+
...typeof error.status === "number" ? { status: error.status } : {},
|
|
402
|
+
...error.data === void 0 ? {} : { data: error.data },
|
|
403
|
+
cause: error
|
|
404
|
+
});
|
|
405
|
+
throw new TransportError("INTERNAL_SERVER_ERROR", { cause: error });
|
|
406
|
+
}
|
|
407
|
+
const presentation = await materializePresentation(located.capability, output, context, wantsPresentation);
|
|
408
|
+
return {
|
|
409
|
+
output,
|
|
410
|
+
...presentation ? { presentation } : {}
|
|
411
|
+
};
|
|
412
|
+
},
|
|
413
|
+
async subscribe({ id, input = {}, signal, presentation: wantsPresentation }) {
|
|
414
|
+
const located = byId.get(id);
|
|
415
|
+
if (!located) throw new TransportError("NOT_FOUND", { status: 404 });
|
|
416
|
+
if (!located.capability.subscribe) throw wrongExecutionMode(id, "call");
|
|
417
|
+
let context;
|
|
418
|
+
try {
|
|
419
|
+
context = options?.context ? await options.context({
|
|
420
|
+
id,
|
|
421
|
+
...signal ? { signal } : {}
|
|
422
|
+
}) : void 0;
|
|
423
|
+
} catch (cause) {
|
|
424
|
+
throw new TransportError("INTERNAL_SERVER_ERROR", { cause });
|
|
425
|
+
}
|
|
426
|
+
let stream;
|
|
427
|
+
try {
|
|
428
|
+
stream = await subscribeLocatedCapability(located, input, {
|
|
429
|
+
context,
|
|
430
|
+
...signal ? { signal } : {}
|
|
431
|
+
});
|
|
432
|
+
} catch (error) {
|
|
433
|
+
throw toTransportError(error);
|
|
434
|
+
}
|
|
435
|
+
return mapAsyncIterator(stream, async (output) => {
|
|
436
|
+
const presentation = await materializePresentation(located.capability, output, context, wantsPresentation);
|
|
437
|
+
return {
|
|
438
|
+
output,
|
|
439
|
+
...presentation ? { presentation } : {}
|
|
440
|
+
};
|
|
441
|
+
}, toTransportError);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
return appFromRuntime(runtime);
|
|
445
|
+
}
|
|
446
|
+
function createRemoteApp(transport) {
|
|
447
|
+
const manifest = cacheAsync(async () => {
|
|
448
|
+
const manifest = await transport.manifest();
|
|
449
|
+
validateManifest(manifest, "Remote App");
|
|
450
|
+
return manifest;
|
|
451
|
+
});
|
|
452
|
+
return appFromRuntime(Object.freeze({
|
|
453
|
+
manifest,
|
|
454
|
+
async resolve(path) {
|
|
455
|
+
const capability = findManifestCapability((await manifest()).capabilities, path);
|
|
456
|
+
return capability ? {
|
|
457
|
+
capability,
|
|
458
|
+
transport
|
|
459
|
+
} : void 0;
|
|
460
|
+
},
|
|
461
|
+
async call(call) {
|
|
462
|
+
await manifest();
|
|
463
|
+
return transport.call(call);
|
|
464
|
+
},
|
|
465
|
+
async subscribe(call) {
|
|
466
|
+
await manifest();
|
|
467
|
+
return transport.subscribe(call);
|
|
468
|
+
}
|
|
469
|
+
}));
|
|
470
|
+
}
|
|
471
|
+
function cacheAsync(load) {
|
|
472
|
+
let cached;
|
|
473
|
+
return () => {
|
|
474
|
+
if (cached) return cached;
|
|
475
|
+
const attempt = Promise.resolve().then(load);
|
|
476
|
+
cached = attempt;
|
|
477
|
+
attempt.catch(() => {
|
|
478
|
+
if (cached === attempt) cached = void 0;
|
|
479
|
+
});
|
|
480
|
+
return attempt;
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
var TransportError = class extends Error {
|
|
484
|
+
code;
|
|
485
|
+
status;
|
|
486
|
+
data;
|
|
487
|
+
constructor(code, options = {}) {
|
|
488
|
+
super(options.message ?? code, { cause: options.cause });
|
|
489
|
+
this.name = "TransportError";
|
|
490
|
+
this.code = code;
|
|
491
|
+
this.status = options.status;
|
|
492
|
+
this.data = options.data;
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
async function materializePresentation(capability, output, context, requested) {
|
|
496
|
+
if (!requested || !capability.toPresentation) return void 0;
|
|
497
|
+
try {
|
|
498
|
+
const presentation = normalizePresentation(await capability.toPresentation(output, { context }));
|
|
499
|
+
return presentation.length > 0 ? presentation : void 0;
|
|
500
|
+
} catch (cause) {
|
|
501
|
+
throw new TransportError("INTERNAL_SERVER_ERROR", { cause });
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
function toTransportError(error) {
|
|
505
|
+
if (error instanceof TransportError) return error;
|
|
506
|
+
if (error instanceof CapableError) return new TransportError(error.code, {
|
|
507
|
+
...error.message ? { message: error.message } : {},
|
|
508
|
+
...typeof error.status === "number" ? { status: error.status } : {},
|
|
509
|
+
...error.data === void 0 ? {} : { data: error.data },
|
|
510
|
+
cause: error
|
|
511
|
+
});
|
|
512
|
+
return new TransportError("INTERNAL_SERVER_ERROR", { cause: error });
|
|
513
|
+
}
|
|
514
|
+
function wrongExecutionMode(id, supported) {
|
|
515
|
+
return new TransportError("METHOD_NOT_ALLOWED", {
|
|
516
|
+
status: 405,
|
|
517
|
+
message: `Capability "${id}" only supports ${supported}`
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
//#endregion
|
|
521
|
+
//#region packages/app/src/customization.ts
|
|
522
|
+
const CUSTOMIZED_SYMBOL = Symbol.for("capable.customized");
|
|
523
|
+
/** Customize a bound local or remote reference without changing its execution. */
|
|
524
|
+
function customize(reference, options) {
|
|
525
|
+
const previous = isCustomizedCapability(reference) ? reference : void 0;
|
|
526
|
+
const original = previous ? previous.reference : reference;
|
|
527
|
+
if (!getCapabilityBinding(original)) throw new TypeError("customize() requires a Capability reference from a Capable App");
|
|
528
|
+
if (!isRecord(options)) throw new TypeError("customize() options must be an object");
|
|
529
|
+
for (const key of Object.keys(options)) if (key !== "title" && key !== "description" && key !== "meta") throw new TypeError(`customize() does not support "${key}"`);
|
|
530
|
+
for (const key of ["title", "description"]) if (options[key] !== void 0 && typeof options[key] !== "string") throw new TypeError(`customize() ${key} must be a string`);
|
|
531
|
+
if (options.meta !== void 0 && !isRecord(options.meta)) throw new TypeError("customize() meta must be an object");
|
|
532
|
+
return Object.freeze({
|
|
533
|
+
[CUSTOMIZED_SYMBOL]: true,
|
|
534
|
+
reference: original,
|
|
535
|
+
customization: mergeCustomization(previous?.customization, options)
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
function isCustomizedCapability(value) {
|
|
539
|
+
return isRecord(value) && value[CUSTOMIZED_SYMBOL] === true;
|
|
540
|
+
}
|
|
541
|
+
/** Outer customizations replace supplied namespaces; no generic deep merge. */
|
|
542
|
+
function mergeCustomization(source, override) {
|
|
543
|
+
return snapshot({
|
|
544
|
+
...source,
|
|
545
|
+
...override,
|
|
546
|
+
...source?.meta || override.meta ? { meta: {
|
|
547
|
+
...source?.meta,
|
|
548
|
+
...override.meta
|
|
549
|
+
} } : {}
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
/** Surface-facing fields only. Policies and executors retain the source contract. */
|
|
553
|
+
function getCapabilityDisplay(capability) {
|
|
554
|
+
const { customization } = capability;
|
|
555
|
+
return {
|
|
556
|
+
...capability.title !== void 0 ? { title: capability.title } : {},
|
|
557
|
+
...capability.description !== void 0 ? { description: capability.description } : {},
|
|
558
|
+
...customization?.title !== void 0 ? { title: customization.title } : {},
|
|
559
|
+
...customization?.description !== void 0 ? { description: customization.description } : {},
|
|
560
|
+
meta: {
|
|
561
|
+
...capability.meta,
|
|
562
|
+
...customization?.meta
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
/** Navigation overrides come from the destination only for relocated leaves. */
|
|
567
|
+
function getCapabilityNavigationMeta(capability, surface) {
|
|
568
|
+
return capability.customization?.navigation === "destination" ? capability.customization.meta?.[surface] : capability.meta[surface];
|
|
569
|
+
}
|
|
570
|
+
function snapshot(value, ancestors = /* @__PURE__ */ new Set()) {
|
|
571
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
|
|
572
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
573
|
+
if (Array.isArray(value) || isRecord(value)) {
|
|
574
|
+
if (ancestors.has(value)) throw new TypeError("customize() metadata must not contain cycles");
|
|
575
|
+
ancestors.add(value);
|
|
576
|
+
const copy = Array.isArray(value) ? value.map((child) => snapshot(child, ancestors)) : Object.fromEntries(Object.entries(value).filter(([, child]) => child !== void 0).sort(([left], [right]) => left.localeCompare(right)).map(([key, child]) => [key, snapshot(child, ancestors)]));
|
|
577
|
+
ancestors.delete(value);
|
|
578
|
+
return Object.freeze(copy);
|
|
579
|
+
}
|
|
580
|
+
throw new TypeError("customize() metadata must contain only JSON values");
|
|
581
|
+
}
|
|
582
|
+
function isRecord(value) {
|
|
583
|
+
return value !== null && typeof value === "object" && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
|
|
584
|
+
}
|
|
585
|
+
//#endregion
|
|
586
|
+
//#region packages/app/src/composition.ts
|
|
587
|
+
/** Compose executable Apps, subtrees, and Capability references into one tree. */
|
|
588
|
+
function composeApps(...nodes) {
|
|
589
|
+
const placements = nodes.flatMap((node) => collectPlacements(node, []));
|
|
590
|
+
return appFromRuntime(createCompositionRuntime(placements.filter((placement, index) => placements.findIndex((candidate) => candidate.runtime === placement.runtime && samePath(candidate.path, placement.path) && samePath(candidate.sourcePath, placement.sourcePath) && JSON.stringify(candidate.customization) === JSON.stringify(placement.customization)) === index)));
|
|
591
|
+
}
|
|
592
|
+
function collectPlacements(node, path) {
|
|
593
|
+
const customized = isCustomizedCapability(node) ? node : void 0;
|
|
594
|
+
const binding = getCapabilityBinding(customized ? customized.reference : node);
|
|
595
|
+
if (binding) return [{
|
|
596
|
+
path: Object.freeze(path),
|
|
597
|
+
runtime: binding.runtime,
|
|
598
|
+
sourcePath: Object.freeze(binding.path),
|
|
599
|
+
...customized ? { customization: customized.customization } : {}
|
|
600
|
+
}];
|
|
601
|
+
if (isApp(node)) return [{
|
|
602
|
+
path: Object.freeze(path),
|
|
603
|
+
runtime: getInternalAppRuntime(node),
|
|
604
|
+
sourcePath: Object.freeze([])
|
|
605
|
+
}];
|
|
606
|
+
if (!node || typeof node !== "object" || Array.isArray(node)) throw new TypeError("composeApps() entries must be Apps, executable subtree references, Capability references, or named object trees");
|
|
607
|
+
const entries = Object.entries(node);
|
|
608
|
+
if (entries.length === 0) throw new Error(`composeApps() contains an empty object at "${path.join(".") || "<root>"}"`);
|
|
609
|
+
return entries.flatMap(([key, child]) => {
|
|
610
|
+
if (!key) throw new Error("composeApps() keys must not be empty");
|
|
611
|
+
if (key === "then") throw new Error("composeApps() uses reserved key \"then\"");
|
|
612
|
+
return collectPlacements(child, [...path, key]);
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
function createCompositionRuntime(placements) {
|
|
616
|
+
return derivedRuntime(async () => {
|
|
617
|
+
const sources = await Promise.all(placements.map(resolvePlacement));
|
|
618
|
+
const manifest = composeApplicationManifests(sources);
|
|
619
|
+
const dispatch = /* @__PURE__ */ new Map();
|
|
620
|
+
for (const { owners } of sources) for (const [id, target] of owners) dispatch.set(id, target);
|
|
621
|
+
return {
|
|
622
|
+
manifest,
|
|
623
|
+
dispatch
|
|
624
|
+
};
|
|
625
|
+
}, async (path) => {
|
|
626
|
+
const matches = (await Promise.all(placements.filter((placement) => isPathPrefixOrEqual(placement.path, path)).map(async (placement) => {
|
|
627
|
+
const resolved = await placement.runtime.resolve([...placement.sourcePath, ...path.slice(placement.path.length)]);
|
|
628
|
+
if (!resolved) return void 0;
|
|
629
|
+
const relocatedLeaf = samePath(placement.path, path);
|
|
630
|
+
if (placement.customization && !relocatedLeaf) throw new TypeError("customize() requires a Capability reference, not an App subtree");
|
|
631
|
+
return {
|
|
632
|
+
transport: resolved.transport,
|
|
633
|
+
capability: placeCapability(resolved.capability, {
|
|
634
|
+
path,
|
|
635
|
+
relocatedLeaf,
|
|
636
|
+
...relocatedLeaf && placement.customization ? { customization: placement.customization } : {}
|
|
637
|
+
})
|
|
638
|
+
};
|
|
639
|
+
}))).filter((match) => match !== void 0).filter((match, index, all) => all.findIndex((candidate) => candidate.capability.id === match.capability.id && candidate.transport === match.transport && JSON.stringify(candidate.capability.customization) === JSON.stringify(match.capability.customization)) === index);
|
|
640
|
+
if (matches.length > 1) throw new Error(`Cannot resolve ambiguous App path "${path.join(".")}"`);
|
|
641
|
+
return matches[0];
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
async function resolvePlacement(placement) {
|
|
645
|
+
const source = await placement.runtime.manifest();
|
|
646
|
+
const placed = placeSourceManifest(source, placement);
|
|
647
|
+
const targets = /* @__PURE__ */ new Map();
|
|
648
|
+
for (const capability of placed.sourceCapabilities) {
|
|
649
|
+
const resolved = await placement.runtime.resolve(capability.path);
|
|
650
|
+
if (!resolved) throw missingReference(capability.path);
|
|
651
|
+
const previous = targets.get(capability.id);
|
|
652
|
+
if (previous && previous !== resolved.transport) throw new Error(`Capability id "${capability.id}" has multiple execution owners in "${source.name}"`);
|
|
653
|
+
targets.set(capability.id, resolved.transport);
|
|
654
|
+
}
|
|
655
|
+
return {
|
|
656
|
+
manifest: placed.manifest,
|
|
657
|
+
application: placement.runtime,
|
|
658
|
+
owners: targets
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
function placeSourceManifest(source, placement) {
|
|
662
|
+
if (placement.sourcePath.length === 0) return {
|
|
663
|
+
manifest: placeManifest(placement.path, source),
|
|
664
|
+
sourceCapabilities: flattenManifestCapabilities(source.capabilities)
|
|
665
|
+
};
|
|
666
|
+
const node = findManifestNode(source.capabilities, placement.sourcePath);
|
|
667
|
+
if (!node) throw missingReference(placement.sourcePath);
|
|
668
|
+
if (node.type === "capability") {
|
|
669
|
+
if (placement.path.length === 0) throw new TypeError("Capability references require a named key in composeApps()");
|
|
670
|
+
return {
|
|
671
|
+
manifest: Object.freeze({
|
|
672
|
+
...source,
|
|
673
|
+
capabilities: wrapGroups(Object.freeze([placeCapability(node, {
|
|
674
|
+
path: placement.path,
|
|
675
|
+
relocatedLeaf: true,
|
|
676
|
+
...placement.customization ? { customization: placement.customization } : {}
|
|
677
|
+
})]), placement.path.slice(0, -1))
|
|
678
|
+
}),
|
|
679
|
+
sourceCapabilities: Object.freeze([node])
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
if (placement.customization) throw new TypeError("customize() requires a Capability reference, not an App subtree");
|
|
683
|
+
return {
|
|
684
|
+
manifest: Object.freeze({
|
|
685
|
+
...source,
|
|
686
|
+
capabilities: placeGroup(node, placement.sourcePath, placement.path)
|
|
687
|
+
}),
|
|
688
|
+
sourceCapabilities: flattenManifestCapabilities(node.children)
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
function placeGroup(group, sourcePath, path) {
|
|
692
|
+
if (path.length === 0) return rebaseManifestPaths(group.children, sourcePath, []);
|
|
693
|
+
const placed = Object.freeze({
|
|
694
|
+
...group,
|
|
695
|
+
key: path.at(-1),
|
|
696
|
+
meta: Object.freeze({}),
|
|
697
|
+
children: rebaseManifestPaths(group.children, sourcePath, path)
|
|
698
|
+
});
|
|
699
|
+
return wrapGroups(Object.freeze([placed]), path.slice(0, -1));
|
|
700
|
+
}
|
|
701
|
+
function placeCapability(capability, placement) {
|
|
702
|
+
return Object.freeze({
|
|
703
|
+
...capability,
|
|
704
|
+
path: placement.path,
|
|
705
|
+
...placement.relocatedLeaf ? { customization: mergeCustomization(capability.customization, {
|
|
706
|
+
...placement.customization,
|
|
707
|
+
navigation: "destination"
|
|
708
|
+
}) } : {}
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
function missingReference(path) {
|
|
712
|
+
return /* @__PURE__ */ new Error(`No executable App node exists at "${path.join(".")}"`);
|
|
713
|
+
}
|
|
714
|
+
/** One cached manifest/dispatch snapshot for both composition and selection. */
|
|
715
|
+
function derivedRuntime(load, resolve) {
|
|
716
|
+
const snapshot = cacheAsync(load);
|
|
717
|
+
const target = async (id) => {
|
|
718
|
+
const runtime = (await snapshot()).dispatch.get(id);
|
|
719
|
+
if (!runtime) throw new TransportError("NOT_FOUND", { status: 404 });
|
|
720
|
+
return runtime;
|
|
721
|
+
};
|
|
722
|
+
return Object.freeze({
|
|
723
|
+
manifest: async () => (await snapshot()).manifest,
|
|
724
|
+
resolve,
|
|
725
|
+
async call(call) {
|
|
726
|
+
return (await target(call.id)).call(call);
|
|
727
|
+
},
|
|
728
|
+
async subscribe(call) {
|
|
729
|
+
return (await target(call.id)).subscribe(call);
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
function selectApp(application, selection) {
|
|
734
|
+
const runtime = getInternalAppRuntime(application);
|
|
735
|
+
const manifest = cacheAsync(async () => selectManifest(await runtime.manifest(), selection));
|
|
736
|
+
return appFromRuntime(derivedRuntime(async () => {
|
|
737
|
+
const selected = await manifest();
|
|
738
|
+
return {
|
|
739
|
+
manifest: selected,
|
|
740
|
+
dispatch: new Map(flattenManifestCapabilities(selected.capabilities).map((capability) => [capability.id, runtime]))
|
|
741
|
+
};
|
|
742
|
+
}, async (path) => {
|
|
743
|
+
if (!findManifestCapability((await manifest()).capabilities, path)) return void 0;
|
|
744
|
+
return runtime.resolve(path);
|
|
745
|
+
}));
|
|
746
|
+
}
|
|
747
|
+
function placeManifest(path, source) {
|
|
748
|
+
if (path.length === 0) return source;
|
|
749
|
+
return Object.freeze({
|
|
750
|
+
...source,
|
|
751
|
+
capabilities: wrapGroups(rebaseManifestPaths(source.capabilities, [], path), path, source)
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
function wrapGroups(nodes, path, source) {
|
|
755
|
+
let capabilities = nodes;
|
|
756
|
+
for (let index = path.length - 1; index >= 0; index--) {
|
|
757
|
+
const leaf = index === path.length - 1;
|
|
758
|
+
capabilities = Object.freeze([Object.freeze({
|
|
759
|
+
type: "group",
|
|
760
|
+
key: path[index],
|
|
761
|
+
...leaf && source ? { title: source.name } : {},
|
|
762
|
+
...leaf && source?.description ? { description: source.description } : {},
|
|
763
|
+
meta: Object.freeze({}),
|
|
764
|
+
children: capabilities
|
|
765
|
+
})]);
|
|
766
|
+
}
|
|
767
|
+
return capabilities;
|
|
768
|
+
}
|
|
769
|
+
function rebaseManifestPaths(nodes, sourcePath, destinationPath) {
|
|
770
|
+
return Object.freeze(nodes.map((node) => node.type === "capability" ? Object.freeze({
|
|
771
|
+
...node,
|
|
772
|
+
path: Object.freeze([...destinationPath, ...node.path.slice(sourcePath.length)])
|
|
773
|
+
}) : Object.freeze({
|
|
774
|
+
...node,
|
|
775
|
+
children: rebaseManifestPaths(node.children, sourcePath, destinationPath)
|
|
776
|
+
})));
|
|
777
|
+
}
|
|
778
|
+
function isPathPrefixOrEqual(prefix, path) {
|
|
779
|
+
return prefix.length <= path.length && prefix.every((segment, index) => segment === path[index]);
|
|
780
|
+
}
|
|
781
|
+
function samePath(left, right) {
|
|
782
|
+
return left.length === right.length && left.every((segment, index) => segment === right[index]);
|
|
783
|
+
}
|
|
784
|
+
//#endregion
|
|
785
|
+
export { getCapabilityNavigationMeta as a, TransportError as c, getAppRuntime as d, isApp as f, flattenManifestCapabilities as h, getCapabilityDisplay as i, createApp as l, createAppManifest as m, selectApp as n, APP_CAPABILITIES_SYMBOL as o, subscribe as p, customize as r, APP_RUNTIME_SYMBOL as s, composeApps as t, createRemoteApp as u };
|
|
786
|
+
|
|
787
|
+
//# sourceMappingURL=src-DePlcG_t.mjs.map
|