capable-kit 0.0.1-alpha.0
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/dist/ai-sdk.d.mts +40 -0
- package/dist/ai-sdk.mjs +58 -0
- package/dist/ai-sdk.mjs.map +1 -0
- package/dist/app.d.mts +2 -0
- package/dist/app.mjs +2 -0
- package/dist/bin.d.mts +1 -0
- package/dist/bin.mjs +447 -0
- package/dist/bin.mjs.map +1 -0
- package/dist/cli.d.mts +45 -0
- package/dist/cli.mjs +2 -0
- package/dist/core.d.mts +3 -0
- package/dist/core.mjs +3 -0
- package/dist/eve.d.mts +28 -0
- package/dist/eve.mjs +61 -0
- package/dist/eve.mjs.map +1 -0
- package/dist/http.d.mts +2 -0
- package/dist/http.mjs +2 -0
- package/dist/index-BuXwkN8G.d.mts +42 -0
- package/dist/index-C9ZThFPN.d.mts +125 -0
- package/dist/index-DLcYNpfo.d.mts +252 -0
- package/dist/index-u9ZOETR5.d.mts +57 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.mjs +4 -0
- package/dist/mcp.d.mts +36 -0
- package/dist/mcp.mjs +140 -0
- package/dist/mcp.mjs.map +1 -0
- package/dist/projection-Cgq2u0Wt.mjs +18 -0
- package/dist/projection-Cgq2u0Wt.mjs.map +1 -0
- package/dist/server.d.mts +2 -0
- package/dist/server.mjs +82 -0
- package/dist/server.mjs.map +1 -0
- package/dist/src-CT4fbWqb.mjs +455 -0
- package/dist/src-CT4fbWqb.mjs.map +1 -0
- package/dist/src-CvWOGyGW.mjs +438 -0
- package/dist/src-CvWOGyGW.mjs.map +1 -0
- package/dist/src-DX-_J6l2.mjs +539 -0
- package/dist/src-DX-_J6l2.mjs.map +1 -0
- package/dist/src-DlSJtEhv.mjs +441 -0
- package/dist/src-DlSJtEhv.mjs.map +1 -0
- package/dist/subscription-CBnnIU42.d.mts +18 -0
- package/package.json +35 -0
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import { a as isCapabilityGroup, f as subscribeLocatedCapability, h as schemaToJsonSchema, l as executeLocatedCapability, u as isCapability, v as CapableError } from "./src-DX-_J6l2.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.v2",
|
|
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.v2") throw new Error(`${label} uses unsupported protocol "${manifest.protocol}"`);
|
|
15
|
+
const capabilities = flattenManifestCapabilities(manifest.capabilities);
|
|
16
|
+
const ids = /* @__PURE__ */ new Set();
|
|
17
|
+
const paths = /* @__PURE__ */ new Set();
|
|
18
|
+
for (const capability of capabilities) {
|
|
19
|
+
const path = capability.path.join(".");
|
|
20
|
+
if (ids.has(capability.id)) throw new Error(`${label} contains duplicate Capability id "${capability.id}"`);
|
|
21
|
+
if (paths.has(path)) throw new Error(`${label} contains duplicate Capability path "${path}"`);
|
|
22
|
+
ids.add(capability.id);
|
|
23
|
+
paths.add(path);
|
|
24
|
+
}
|
|
25
|
+
validateManifestPaths(manifest.capabilities, [], label);
|
|
26
|
+
}
|
|
27
|
+
function mergeApplicationManifests(manifests) {
|
|
28
|
+
const conflicts = collectManifestConflicts(manifests);
|
|
29
|
+
if (conflicts.length) throw new Error(`Cannot merge Apps:\n${conflicts.map((item) => `- ${item}`).join("\n")}`);
|
|
30
|
+
return Object.freeze({
|
|
31
|
+
protocol: "capable.capabilities.v2",
|
|
32
|
+
name: manifests.map(({ name }) => name).join(" + "),
|
|
33
|
+
version: manifests.map(({ version }) => version).join(" + "),
|
|
34
|
+
capabilities: mergeManifestNodes(manifests.flatMap(({ capabilities }) => capabilities))
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function selectManifest(source, selection) {
|
|
38
|
+
return Object.freeze({
|
|
39
|
+
...source,
|
|
40
|
+
capabilities: filterManifestNodes(source.capabilities, selection)
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function flattenManifestCapabilities(nodes) {
|
|
44
|
+
return nodes.flatMap((node) => node.type === "capability" ? [node] : flattenManifestCapabilities(node.children));
|
|
45
|
+
}
|
|
46
|
+
function findManifestCapability(nodes, path) {
|
|
47
|
+
let current = nodes;
|
|
48
|
+
for (let index = 0; index < path.length; index++) {
|
|
49
|
+
const key = path[index];
|
|
50
|
+
const node = current.find((candidate) => candidate.type === "group" ? candidate.key === key : candidate.path.at(-1) === key);
|
|
51
|
+
if (!node) return void 0;
|
|
52
|
+
if (node.type === "capability") return index === path.length - 1 ? node : void 0;
|
|
53
|
+
current = node.children;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function manifestNodes(tree, located) {
|
|
57
|
+
const nodes = [];
|
|
58
|
+
for (const [key, value] of Object.entries(tree)) {
|
|
59
|
+
if (isCapability(value)) {
|
|
60
|
+
const entry = located.get(value);
|
|
61
|
+
if (entry) nodes.push(manifestCapability(entry));
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const decorated = isCapabilityGroup(value);
|
|
65
|
+
const children = manifestNodes(decorated ? value.capabilities : value, located);
|
|
66
|
+
nodes.push(Object.freeze({
|
|
67
|
+
type: "group",
|
|
68
|
+
key,
|
|
69
|
+
...decorated && value.title ? { title: value.title } : {},
|
|
70
|
+
...decorated && value.description ? { description: value.description } : {},
|
|
71
|
+
meta: decorated ? value.meta : {},
|
|
72
|
+
children
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
return nodes;
|
|
76
|
+
}
|
|
77
|
+
function manifestCapability(entry) {
|
|
78
|
+
const capability = entry.capability;
|
|
79
|
+
return Object.freeze({
|
|
80
|
+
type: "capability",
|
|
81
|
+
id: capability.id,
|
|
82
|
+
path: entry.path,
|
|
83
|
+
...capability.title ? { title: capability.title } : {},
|
|
84
|
+
...capability.description ? { description: capability.description } : {},
|
|
85
|
+
deprecated: capability.deprecated,
|
|
86
|
+
...capability.effect ? { effect: capability.effect } : {},
|
|
87
|
+
...capability.subscribe ? { subscription: { mode: "replace" } } : {},
|
|
88
|
+
...capability.input ? { input: schemaToJsonSchema(capability.input) } : {},
|
|
89
|
+
...capability.output ? { output: schemaToJsonSchema(capability.output) } : {},
|
|
90
|
+
errors: Object.fromEntries(Object.entries(entry.errors).map(([code, definition]) => [code, {
|
|
91
|
+
...definition.message ? { message: definition.message } : {},
|
|
92
|
+
...typeof definition.status === "number" ? { status: definition.status } : {},
|
|
93
|
+
...definition.data ? { data: schemaToJsonSchema(definition.data) } : {},
|
|
94
|
+
...definition.meta ? { meta: definition.meta } : {}
|
|
95
|
+
}])),
|
|
96
|
+
meta: capability.meta
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
function validateManifestPaths(nodes, parent, label) {
|
|
100
|
+
for (const node of nodes) {
|
|
101
|
+
if (node.type === "group") {
|
|
102
|
+
validateManifestPaths(node.children, [...parent, node.key], label);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (!samePath(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`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function collectManifestConflicts(manifests) {
|
|
109
|
+
const conflicts = [];
|
|
110
|
+
for (const manifest of manifests) try {
|
|
111
|
+
validateManifest(manifest, manifest.name);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
conflicts.push(error instanceof Error ? error.message : String(error));
|
|
114
|
+
}
|
|
115
|
+
const capabilities = manifests.flatMap((manifest, source) => flattenManifestCapabilities(manifest.capabilities).map((capability) => ({
|
|
116
|
+
capability,
|
|
117
|
+
source,
|
|
118
|
+
label: manifest.name
|
|
119
|
+
})));
|
|
120
|
+
for (let leftIndex = 0; leftIndex < capabilities.length; leftIndex++) for (let rightIndex = leftIndex + 1; rightIndex < capabilities.length; rightIndex++) {
|
|
121
|
+
const left = capabilities[leftIndex];
|
|
122
|
+
const right = capabilities[rightIndex];
|
|
123
|
+
if (left.source === right.source) continue;
|
|
124
|
+
const sameId = left.capability.id === right.capability.id;
|
|
125
|
+
if (samePath(left.capability.path, right.capability.path)) {
|
|
126
|
+
conflicts.push(sameId ? `Capability "${left.capability.id}" is mounted 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}"`);
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (sameId) conflicts.push(`Capability id "${left.capability.id}" appears at both "${left.capability.path.join(".")}" in "${left.label}" and "${right.capability.path.join(".")}"`);
|
|
130
|
+
if (isPathPrefix(left.capability.path, right.capability.path) || isPathPrefix(right.capability.path, left.capability.path)) {
|
|
131
|
+
const leaf = left.capability.path.length < right.capability.path.length ? left : right;
|
|
132
|
+
const subtree = leaf === left ? right : left;
|
|
133
|
+
conflicts.push(`Leaf "${leaf.capability.path.join(".")}" from "${leaf.label}" collides with subtree Capability "${subtree.capability.path.join(".")}" from "${subtree.label}"`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return [...new Set(conflicts)];
|
|
137
|
+
}
|
|
138
|
+
function mergeManifestNodes(nodes) {
|
|
139
|
+
const merged = /* @__PURE__ */ new Map();
|
|
140
|
+
for (const node of nodes) {
|
|
141
|
+
const key = node.type === "group" ? node.key : node.path.at(-1);
|
|
142
|
+
const previous = merged.get(key);
|
|
143
|
+
if (!previous) {
|
|
144
|
+
merged.set(key, node);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (previous.type !== "group" || node.type !== "group") throw new Error(`Manifest node collision at "${key}"`);
|
|
148
|
+
merged.set(key, Object.freeze({
|
|
149
|
+
type: "group",
|
|
150
|
+
key,
|
|
151
|
+
...mergeGroupField("title", previous.title, node.title, key),
|
|
152
|
+
...mergeGroupField("description", previous.description, node.description, key),
|
|
153
|
+
meta: mergeGroupMeta(previous.meta, node.meta, key),
|
|
154
|
+
children: mergeManifestNodes([...previous.children, ...node.children])
|
|
155
|
+
}));
|
|
156
|
+
}
|
|
157
|
+
return [...merged.values()];
|
|
158
|
+
}
|
|
159
|
+
function mergeGroupField(field, left, right, path) {
|
|
160
|
+
if (left && right && left !== right) throw new Error(`Group "${path}" has conflicting ${field}`);
|
|
161
|
+
const value = left ?? right;
|
|
162
|
+
return value ? { [field]: value } : {};
|
|
163
|
+
}
|
|
164
|
+
function mergeGroupMeta(left, right, path) {
|
|
165
|
+
const merged = { ...left };
|
|
166
|
+
for (const [key, value] of Object.entries(right)) {
|
|
167
|
+
if (key in merged && JSON.stringify(merged[key]) !== JSON.stringify(value)) throw new Error(`Group "${path}" has conflicting metadata for "${key}"`);
|
|
168
|
+
merged[key] = value;
|
|
169
|
+
}
|
|
170
|
+
return Object.freeze(merged);
|
|
171
|
+
}
|
|
172
|
+
function filterManifestNodes(nodes, selection) {
|
|
173
|
+
const filtered = [];
|
|
174
|
+
for (const node of nodes) {
|
|
175
|
+
if (node.type === "capability") {
|
|
176
|
+
if ((selection.include ? matchesSelection(selection.include, node.path) : true) && !matchesSelection(selection.exclude, node.path)) filtered.push(node);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const children = filterManifestNodes(node.children, selection);
|
|
180
|
+
if (children.length) filtered.push(Object.freeze({
|
|
181
|
+
...node,
|
|
182
|
+
children
|
|
183
|
+
}));
|
|
184
|
+
}
|
|
185
|
+
return filtered;
|
|
186
|
+
}
|
|
187
|
+
function matchesSelection(tree, path) {
|
|
188
|
+
let current = tree;
|
|
189
|
+
for (const segment of path) {
|
|
190
|
+
if (current === true) return true;
|
|
191
|
+
if (!current || typeof current !== "object" || Array.isArray(current)) return false;
|
|
192
|
+
current = current[segment];
|
|
193
|
+
}
|
|
194
|
+
return current === true || Boolean(current) && typeof current === "object" && !Array.isArray(current);
|
|
195
|
+
}
|
|
196
|
+
function samePath(left, right) {
|
|
197
|
+
return left.length === right.length && left.every((segment, index) => segment === right[index]);
|
|
198
|
+
}
|
|
199
|
+
function isPathPrefix(prefix, path) {
|
|
200
|
+
return prefix.length < path.length && prefix.every((segment, index) => segment === path[index]);
|
|
201
|
+
}
|
|
202
|
+
//#endregion
|
|
203
|
+
//#region packages/app/src/runtime.ts
|
|
204
|
+
const APP_RUNTIME_SYMBOL = Symbol.for("capable.app.runtime");
|
|
205
|
+
const APP_CAPABILITIES_SYMBOL = Symbol.for("capable.app.capabilities");
|
|
206
|
+
const APP_CALL_SYMBOL = Symbol.for("capable.app.call");
|
|
207
|
+
function getAppRuntime(app) {
|
|
208
|
+
return app[APP_RUNTIME_SYMBOL];
|
|
209
|
+
}
|
|
210
|
+
function isApp(value) {
|
|
211
|
+
return Boolean(value && (typeof value === "object" || typeof value === "function") && value[APP_RUNTIME_SYMBOL]);
|
|
212
|
+
}
|
|
213
|
+
function appFromRuntime(runtime) {
|
|
214
|
+
let manifestPromise;
|
|
215
|
+
const manifest = () => manifestPromise ??= runtime.manifest();
|
|
216
|
+
const read = (path, root, key) => {
|
|
217
|
+
if (root && key === APP_RUNTIME_SYMBOL) return runtime;
|
|
218
|
+
if (!root && key === APP_CALL_SYMBOL) return {
|
|
219
|
+
runtime,
|
|
220
|
+
path
|
|
221
|
+
};
|
|
222
|
+
if (key === "then") return void 0;
|
|
223
|
+
if (typeof key !== "string") return void 0;
|
|
224
|
+
return proxy([...path, key], false);
|
|
225
|
+
};
|
|
226
|
+
const proxy = (path, root) => {
|
|
227
|
+
if (root) return new Proxy({}, { get(_target, key) {
|
|
228
|
+
return read(path, true, key);
|
|
229
|
+
} });
|
|
230
|
+
return new Proxy(() => void 0, {
|
|
231
|
+
get(_target, key) {
|
|
232
|
+
return read(path, false, key);
|
|
233
|
+
},
|
|
234
|
+
async apply(_target, _this, args) {
|
|
235
|
+
const capability = findManifestCapability((await manifest()).capabilities, path);
|
|
236
|
+
if (!capability) throw new Error(`No Capability exists at "${path.join(".")}"`);
|
|
237
|
+
return (await runtime.call({
|
|
238
|
+
id: capability.id,
|
|
239
|
+
input: args[0] ?? {},
|
|
240
|
+
...args[1]?.signal ? { signal: args[1].signal } : {}
|
|
241
|
+
})).output;
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
};
|
|
245
|
+
return proxy([], true);
|
|
246
|
+
}
|
|
247
|
+
function subscribe(capabilityCall, ...args) {
|
|
248
|
+
const binding = capabilityCall[APP_CALL_SYMBOL];
|
|
249
|
+
if (!binding) throw new Error("subscribe() requires a Capability call from a Capable App");
|
|
250
|
+
return (async function* () {
|
|
251
|
+
const capability = findManifestCapability((await binding.runtime.manifest()).capabilities, binding.path);
|
|
252
|
+
if (!capability) throw new Error(`No Capability exists at "${binding.path.join(".")}"`);
|
|
253
|
+
if (!capability.subscription) throw new Error(`Capability "${capability.id}" is not subscribable`);
|
|
254
|
+
const stream = binding.runtime.subscribe({
|
|
255
|
+
id: capability.id,
|
|
256
|
+
input: args[0] ?? {},
|
|
257
|
+
...args[1]?.signal ? { signal: args[1].signal } : {}
|
|
258
|
+
});
|
|
259
|
+
try {
|
|
260
|
+
for await (const result of stream) yield result.output;
|
|
261
|
+
} finally {
|
|
262
|
+
await stream.return?.();
|
|
263
|
+
}
|
|
264
|
+
})();
|
|
265
|
+
}
|
|
266
|
+
const localManifestCache = /* @__PURE__ */ new WeakMap();
|
|
267
|
+
const localCapabilityCache = /* @__PURE__ */ new WeakMap();
|
|
268
|
+
function createApp(definition, ...[options]) {
|
|
269
|
+
let manifest = localManifestCache.get(definition);
|
|
270
|
+
if (!manifest) {
|
|
271
|
+
manifest = createAppManifest(definition);
|
|
272
|
+
localManifestCache.set(definition, manifest);
|
|
273
|
+
}
|
|
274
|
+
let byId = localCapabilityCache.get(definition);
|
|
275
|
+
if (!byId) {
|
|
276
|
+
byId = new Map(definition.locatedCapabilities.map((entry) => [entry.capability.id, entry]));
|
|
277
|
+
localCapabilityCache.set(definition, byId);
|
|
278
|
+
}
|
|
279
|
+
return appFromRuntime(Object.freeze({
|
|
280
|
+
async manifest() {
|
|
281
|
+
return manifest;
|
|
282
|
+
},
|
|
283
|
+
async call({ id, input = {}, signal }) {
|
|
284
|
+
const located = byId.get(id);
|
|
285
|
+
if (!located) throw new TransportError("NOT_FOUND", { status: 404 });
|
|
286
|
+
let context;
|
|
287
|
+
try {
|
|
288
|
+
context = options?.context ? await options.context({
|
|
289
|
+
id,
|
|
290
|
+
...signal ? { signal } : {}
|
|
291
|
+
}) : void 0;
|
|
292
|
+
} catch (cause) {
|
|
293
|
+
throw new TransportError("INTERNAL_SERVER_ERROR", { cause });
|
|
294
|
+
}
|
|
295
|
+
let output;
|
|
296
|
+
try {
|
|
297
|
+
output = await executeLocatedCapability(located, input, {
|
|
298
|
+
context,
|
|
299
|
+
...signal ? { signal } : {}
|
|
300
|
+
});
|
|
301
|
+
} catch (error) {
|
|
302
|
+
if (error instanceof CapableError) throw new TransportError(error.code, {
|
|
303
|
+
...error.message ? { message: error.message } : {},
|
|
304
|
+
...typeof error.status === "number" ? { status: error.status } : {},
|
|
305
|
+
...error.data === void 0 ? {} : { data: error.data },
|
|
306
|
+
cause: error
|
|
307
|
+
});
|
|
308
|
+
throw new TransportError("INTERNAL_SERVER_ERROR", { cause: error });
|
|
309
|
+
}
|
|
310
|
+
let markdown;
|
|
311
|
+
try {
|
|
312
|
+
markdown = located.capability.toMarkdown ? await located.capability.toMarkdown(output, { context }) : void 0;
|
|
313
|
+
} catch (cause) {
|
|
314
|
+
throw new TransportError("INTERNAL_SERVER_ERROR", { cause });
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
output,
|
|
318
|
+
...markdown ? { markdown } : {}
|
|
319
|
+
};
|
|
320
|
+
},
|
|
321
|
+
async *subscribe({ id, input = {}, signal }) {
|
|
322
|
+
const located = byId.get(id);
|
|
323
|
+
if (!located) throw new TransportError("NOT_FOUND", { status: 404 });
|
|
324
|
+
if (!located.capability.subscribe) throw new TransportError("NOT_FOUND", { status: 404 });
|
|
325
|
+
let context;
|
|
326
|
+
try {
|
|
327
|
+
context = options?.context ? await options.context({
|
|
328
|
+
id,
|
|
329
|
+
...signal ? { signal } : {}
|
|
330
|
+
}) : void 0;
|
|
331
|
+
} catch (cause) {
|
|
332
|
+
throw new TransportError("INTERNAL_SERVER_ERROR", { cause });
|
|
333
|
+
}
|
|
334
|
+
const stream = subscribeLocatedCapability(located, input, {
|
|
335
|
+
context,
|
|
336
|
+
...signal ? { signal } : {}
|
|
337
|
+
});
|
|
338
|
+
try {
|
|
339
|
+
for await (const output of stream) {
|
|
340
|
+
let markdown;
|
|
341
|
+
try {
|
|
342
|
+
markdown = located.capability.toMarkdown ? await located.capability.toMarkdown(output, { context }) : void 0;
|
|
343
|
+
} catch (cause) {
|
|
344
|
+
throw new TransportError("INTERNAL_SERVER_ERROR", { cause });
|
|
345
|
+
}
|
|
346
|
+
yield {
|
|
347
|
+
output,
|
|
348
|
+
...markdown ? { markdown } : {}
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
} catch (error) {
|
|
352
|
+
throw toTransportError(error);
|
|
353
|
+
} finally {
|
|
354
|
+
await stream.return?.();
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}));
|
|
358
|
+
}
|
|
359
|
+
function createRemoteApp(transport) {
|
|
360
|
+
let resolved;
|
|
361
|
+
const manifest = () => resolved ??= transport.manifest().then((manifest) => {
|
|
362
|
+
validateManifest(manifest, "Remote App");
|
|
363
|
+
return manifest;
|
|
364
|
+
});
|
|
365
|
+
return appFromRuntime(Object.freeze({
|
|
366
|
+
manifest,
|
|
367
|
+
async call(call) {
|
|
368
|
+
await manifest();
|
|
369
|
+
return transport.call(call);
|
|
370
|
+
},
|
|
371
|
+
async *subscribe(call) {
|
|
372
|
+
await manifest();
|
|
373
|
+
yield* transport.subscribe(call);
|
|
374
|
+
}
|
|
375
|
+
}));
|
|
376
|
+
}
|
|
377
|
+
var TransportError = class extends Error {
|
|
378
|
+
code;
|
|
379
|
+
status;
|
|
380
|
+
data;
|
|
381
|
+
constructor(code, options = {}) {
|
|
382
|
+
super(options.message ?? code, { cause: options.cause });
|
|
383
|
+
this.name = "TransportError";
|
|
384
|
+
this.code = code;
|
|
385
|
+
this.status = options.status;
|
|
386
|
+
this.data = options.data;
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
function toTransportError(error) {
|
|
390
|
+
if (error instanceof TransportError) return error;
|
|
391
|
+
if (error instanceof CapableError) return new TransportError(error.code, {
|
|
392
|
+
...error.message ? { message: error.message } : {},
|
|
393
|
+
...typeof error.status === "number" ? { status: error.status } : {},
|
|
394
|
+
...error.data === void 0 ? {} : { data: error.data },
|
|
395
|
+
cause: error
|
|
396
|
+
});
|
|
397
|
+
return new TransportError("INTERNAL_SERVER_ERROR", { cause: error });
|
|
398
|
+
}
|
|
399
|
+
//#endregion
|
|
400
|
+
//#region packages/app/src/composition.ts
|
|
401
|
+
function mergeApps(...applications) {
|
|
402
|
+
const runtimes = applications.map(getAppRuntime);
|
|
403
|
+
let manifestPromise;
|
|
404
|
+
let dispatch;
|
|
405
|
+
const resolve = () => manifestPromise ??= Promise.all(runtimes.map((runtime) => runtime.manifest())).then((manifests) => {
|
|
406
|
+
const nextDispatch = /* @__PURE__ */ new Map();
|
|
407
|
+
manifests.forEach((manifest, index) => {
|
|
408
|
+
for (const capability of flattenManifestCapabilities(manifest.capabilities)) nextDispatch.set(capability.id, runtimes[index]);
|
|
409
|
+
});
|
|
410
|
+
dispatch = nextDispatch;
|
|
411
|
+
return mergeApplicationManifests(manifests);
|
|
412
|
+
});
|
|
413
|
+
return appFromRuntime(Object.freeze({
|
|
414
|
+
manifest: resolve,
|
|
415
|
+
async call(call) {
|
|
416
|
+
await resolve();
|
|
417
|
+
const target = dispatch?.get(call.id);
|
|
418
|
+
if (!target) throw new TransportError("NOT_FOUND", { status: 404 });
|
|
419
|
+
return target.call(call);
|
|
420
|
+
},
|
|
421
|
+
async *subscribe(call) {
|
|
422
|
+
await resolve();
|
|
423
|
+
const target = dispatch?.get(call.id);
|
|
424
|
+
if (!target) throw new TransportError("NOT_FOUND", { status: 404 });
|
|
425
|
+
yield* target.subscribe(call);
|
|
426
|
+
}
|
|
427
|
+
}));
|
|
428
|
+
}
|
|
429
|
+
function selectApp(application, selection) {
|
|
430
|
+
const runtime = getAppRuntime(application);
|
|
431
|
+
let resolved;
|
|
432
|
+
let ids;
|
|
433
|
+
const manifest = () => resolved ??= runtime.manifest().then((source) => {
|
|
434
|
+
const selected = selectManifest(source, selection);
|
|
435
|
+
ids = new Set(flattenManifestCapabilities(selected.capabilities).map((capability) => capability.id));
|
|
436
|
+
return selected;
|
|
437
|
+
});
|
|
438
|
+
return appFromRuntime(Object.freeze({
|
|
439
|
+
manifest,
|
|
440
|
+
async call(call) {
|
|
441
|
+
await manifest();
|
|
442
|
+
if (!ids?.has(call.id)) throw new TransportError("NOT_FOUND", { status: 404 });
|
|
443
|
+
return runtime.call(call);
|
|
444
|
+
},
|
|
445
|
+
async *subscribe(call) {
|
|
446
|
+
await manifest();
|
|
447
|
+
if (!ids?.has(call.id)) throw new TransportError("NOT_FOUND", { status: 404 });
|
|
448
|
+
yield* runtime.subscribe(call);
|
|
449
|
+
}
|
|
450
|
+
}));
|
|
451
|
+
}
|
|
452
|
+
//#endregion
|
|
453
|
+
export { TransportError as a, getAppRuntime as c, createAppManifest as d, APP_RUNTIME_SYMBOL as i, isApp as l, selectApp as n, createApp as o, APP_CAPABILITIES_SYMBOL as r, createRemoteApp as s, mergeApps as t, subscribe as u };
|
|
454
|
+
|
|
455
|
+
//# sourceMappingURL=src-CT4fbWqb.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"src-CT4fbWqb.mjs","names":[],"sources":["../../app/src/manifest.ts","../../app/src/runtime.ts","../../app/src/composition.ts"],"sourcesContent":["import {\n isCapability,\n isCapabilityGroup,\n schemaToJsonSchema,\n type AppDefinition,\n type LocatedCapability,\n} from \"@capable/core\";\n\nexport type JsonSchema = Readonly<Record<string, unknown>>;\n\nexport type ManifestError = {\n readonly message?: string;\n readonly data?: JsonSchema;\n readonly status?: number;\n readonly meta?: Readonly<Record<string, unknown>>;\n};\n\nexport type ManifestCapability = {\n readonly type: \"capability\";\n readonly id: string;\n readonly path: readonly string[];\n readonly title?: string;\n readonly description?: string;\n readonly deprecated: boolean;\n readonly effect?: \"read\" | \"write\" | \"destructive\";\n readonly subscription?: { readonly mode: \"replace\" };\n readonly input?: JsonSchema;\n readonly output?: JsonSchema;\n readonly errors: Readonly<Record<string, ManifestError>>;\n readonly meta: Readonly<Record<string, unknown>>;\n};\n\nexport type ManifestGroup = {\n readonly type: \"group\";\n readonly key: string;\n readonly title?: string;\n readonly description?: string;\n readonly meta: Readonly<Record<string, unknown>>;\n readonly children: readonly ManifestNode[];\n};\n\nexport type ManifestNode = ManifestGroup | ManifestCapability;\n\nexport type ApplicationManifest = {\n readonly protocol: \"capable.capabilities.v2\";\n readonly name: string;\n readonly version: string;\n readonly description?: string;\n readonly capabilities: readonly ManifestNode[];\n};\n\nexport function createAppManifest(\n definition: AppDefinition<any>,\n): ApplicationManifest {\n const byCapability = new Map(\n definition.locatedCapabilities.map((entry) => [entry.capability, entry]),\n );\n return Object.freeze({\n protocol: \"capable.capabilities.v2\",\n name: definition.name,\n version: definition.version,\n ...(definition.description ? { description: definition.description } : {}),\n capabilities: manifestNodes(definition.capabilities, byCapability),\n });\n}\n\nexport function validateManifest(\n manifest: ApplicationManifest,\n label: string,\n): void {\n if (manifest.protocol !== \"capable.capabilities.v2\") {\n throw new Error(`${label} uses unsupported protocol \"${manifest.protocol}\"`);\n }\n const capabilities = flattenManifestCapabilities(manifest.capabilities);\n const ids = new Set<string>();\n const paths = new Set<string>();\n for (const capability of capabilities) {\n const path = capability.path.join(\".\");\n if (ids.has(capability.id)) {\n throw new Error(\n `${label} contains duplicate Capability id \"${capability.id}\"`,\n );\n }\n if (paths.has(path)) {\n throw new Error(`${label} contains duplicate Capability path \"${path}\"`);\n }\n ids.add(capability.id);\n paths.add(path);\n }\n validateManifestPaths(manifest.capabilities, [], label);\n}\n\nexport function mergeApplicationManifests(\n manifests: readonly ApplicationManifest[],\n): ApplicationManifest {\n const conflicts = collectManifestConflicts(manifests);\n if (conflicts.length) {\n throw new Error(\n `Cannot merge Apps:\\n${conflicts.map((item) => `- ${item}`).join(\"\\n\")}`,\n );\n }\n return Object.freeze({\n protocol: \"capable.capabilities.v2\",\n name: manifests.map(({ name }) => name).join(\" + \"),\n version: manifests.map(({ version }) => version).join(\" + \"),\n capabilities: mergeManifestNodes(\n manifests.flatMap(({ capabilities }) => capabilities),\n ),\n });\n}\n\nexport function selectManifest(\n source: ApplicationManifest,\n selection: {\n readonly include?: unknown;\n readonly exclude?: unknown;\n },\n): ApplicationManifest {\n return Object.freeze({\n ...source,\n capabilities: filterManifestNodes(source.capabilities, selection),\n });\n}\n\nexport function flattenManifestCapabilities(\n nodes: readonly ManifestNode[],\n): readonly ManifestCapability[] {\n return nodes.flatMap((node) =>\n node.type === \"capability\"\n ? [node]\n : flattenManifestCapabilities(node.children)\n );\n}\n\nexport function findManifestCapability(\n nodes: readonly ManifestNode[],\n path: readonly string[],\n): ManifestCapability | undefined {\n let current = nodes;\n for (let index = 0; index < path.length; index++) {\n const key = path[index]!;\n const node = current.find((candidate) =>\n candidate.type === \"group\"\n ? candidate.key === key\n : candidate.path.at(-1) === key\n );\n if (!node) return undefined;\n if (node.type === \"capability\") {\n return index === path.length - 1 ? node : undefined;\n }\n current = node.children;\n }\n return undefined;\n}\n\nfunction manifestNodes(\n tree: Readonly<Record<string, unknown>>,\n located: ReadonlyMap<unknown, LocatedCapability>,\n): readonly ManifestNode[] {\n const nodes: ManifestNode[] = [];\n for (const [key, value] of Object.entries(tree)) {\n if (isCapability(value)) {\n const entry = located.get(value);\n if (entry) nodes.push(manifestCapability(entry));\n continue;\n }\n const decorated = isCapabilityGroup(value);\n const childTree = decorated\n ? value.capabilities\n : value as Readonly<Record<string, unknown>>;\n const children = manifestNodes(childTree, located);\n nodes.push(Object.freeze({\n type: \"group\",\n key,\n ...(decorated && value.title ? { title: value.title } : {}),\n ...(decorated && value.description\n ? { description: value.description }\n : {}),\n meta: decorated ? value.meta : {},\n children,\n } satisfies ManifestGroup));\n }\n return nodes;\n}\n\nfunction manifestCapability(entry: LocatedCapability): ManifestCapability {\n const capability = entry.capability;\n return Object.freeze({\n type: \"capability\",\n id: capability.id,\n path: entry.path,\n ...(capability.title ? { title: capability.title } : {}),\n ...(capability.description ? { description: capability.description } : {}),\n deprecated: capability.deprecated,\n ...(capability.effect ? { effect: capability.effect } : {}),\n ...(capability.subscribe\n ? { subscription: { mode: \"replace\" as const } }\n : {}),\n ...(capability.input\n ? { input: schemaToJsonSchema(capability.input) }\n : {}),\n ...(capability.output\n ? { output: schemaToJsonSchema(capability.output) }\n : {}),\n errors: Object.fromEntries(\n Object.entries(entry.errors).map(([code, definition]) => [\n code,\n {\n ...(definition.message ? { message: definition.message } : {}),\n ...(typeof definition.status === \"number\"\n ? { status: definition.status }\n : {}),\n ...(definition.data\n ? { data: schemaToJsonSchema(definition.data) }\n : {}),\n ...(definition.meta ? { meta: definition.meta } : {}),\n } satisfies ManifestError,\n ]),\n ),\n meta: capability.meta,\n });\n}\n\nfunction validateManifestPaths(\n nodes: readonly ManifestNode[],\n parent: readonly string[],\n label: string,\n): void {\n for (const node of nodes) {\n if (node.type === \"group\") {\n validateManifestPaths(node.children, [...parent, node.key], label);\n continue;\n }\n if (!samePath(node.path, [...parent, node.path.at(-1)!])) {\n throw new Error(\n `${label} Capability \"${node.id}\" declares path \"${node.path.join(\".\")}\" that does not match its manifest placement`,\n );\n }\n }\n}\n\nfunction collectManifestConflicts(\n manifests: readonly ApplicationManifest[],\n): readonly string[] {\n const conflicts: string[] = [];\n for (const manifest of manifests) {\n try {\n validateManifest(manifest, manifest.name);\n } catch (error) {\n conflicts.push(error instanceof Error ? error.message : String(error));\n }\n }\n const capabilities = manifests.flatMap((manifest, source) =>\n flattenManifestCapabilities(manifest.capabilities).map((capability) => ({\n capability,\n source,\n label: manifest.name,\n }))\n );\n for (let leftIndex = 0; leftIndex < capabilities.length; leftIndex++) {\n for (\n let rightIndex = leftIndex + 1;\n rightIndex < capabilities.length;\n rightIndex++\n ) {\n const left = capabilities[leftIndex]!;\n const right = capabilities[rightIndex]!;\n if (left.source === right.source) continue;\n const sameId = left.capability.id === right.capability.id;\n const sameCapabilityPath = samePath(\n left.capability.path,\n right.capability.path,\n );\n if (sameCapabilityPath) {\n conflicts.push(\n sameId\n ? `Capability \"${left.capability.id}\" is mounted twice at \"${left.capability.path.join(\".\")}\" by \"${left.label}\" and \"${right.label}\"`\n : `Path \"${left.capability.path.join(\".\")}\" contains both \"${left.capability.id}\" from \"${left.label}\" and \"${right.capability.id}\" from \"${right.label}\"`,\n );\n continue;\n }\n if (sameId) {\n conflicts.push(\n `Capability id \"${left.capability.id}\" appears at both \"${left.capability.path.join(\".\")}\" in \"${left.label}\" and \"${right.capability.path.join(\".\")}\"`,\n );\n }\n if (\n isPathPrefix(left.capability.path, right.capability.path) ||\n isPathPrefix(right.capability.path, left.capability.path)\n ) {\n const leaf = left.capability.path.length < right.capability.path.length\n ? left\n : right;\n const subtree = leaf === left ? right : left;\n conflicts.push(\n `Leaf \"${leaf.capability.path.join(\".\")}\" from \"${leaf.label}\" collides with subtree Capability \"${subtree.capability.path.join(\".\")}\" from \"${subtree.label}\"`,\n );\n }\n }\n }\n return [...new Set(conflicts)];\n}\n\nfunction mergeManifestNodes(\n nodes: readonly ManifestNode[],\n): readonly ManifestNode[] {\n const merged = new Map<string, ManifestNode>();\n for (const node of nodes) {\n const key = node.type === \"group\" ? node.key : node.path.at(-1)!;\n const previous = merged.get(key);\n if (!previous) {\n merged.set(key, node);\n continue;\n }\n if (previous.type !== \"group\" || node.type !== \"group\") {\n throw new Error(`Manifest node collision at \"${key}\"`);\n }\n merged.set(key, Object.freeze({\n type: \"group\",\n key,\n ...mergeGroupField(\"title\", previous.title, node.title, key),\n ...mergeGroupField(\n \"description\",\n previous.description,\n node.description,\n key,\n ),\n meta: mergeGroupMeta(previous.meta, node.meta, key),\n children: mergeManifestNodes([...previous.children, ...node.children]),\n }));\n }\n return [...merged.values()];\n}\n\nfunction mergeGroupField(\n field: \"title\" | \"description\",\n left: string | undefined,\n right: string | undefined,\n path: string,\n): Record<string, string> {\n if (left && right && left !== right) {\n throw new Error(`Group \"${path}\" has conflicting ${field}`);\n }\n const value = left ?? right;\n return value ? { [field]: value } : {};\n}\n\nfunction mergeGroupMeta(\n left: Readonly<Record<string, unknown>>,\n right: Readonly<Record<string, unknown>>,\n path: string,\n): Readonly<Record<string, unknown>> {\n const merged: Record<string, unknown> = { ...left };\n for (const [key, value] of Object.entries(right)) {\n if (\n key in merged &&\n JSON.stringify(merged[key]) !== JSON.stringify(value)\n ) {\n throw new Error(\n `Group \"${path}\" has conflicting metadata for \"${key}\"`,\n );\n }\n merged[key] = value;\n }\n return Object.freeze(merged);\n}\n\nfunction filterManifestNodes(\n nodes: readonly ManifestNode[],\n selection: {\n readonly include?: unknown;\n readonly exclude?: unknown;\n },\n): readonly ManifestNode[] {\n const filtered: ManifestNode[] = [];\n for (const node of nodes) {\n if (node.type === \"capability\") {\n const included = selection.include\n ? matchesSelection(selection.include, node.path)\n : true;\n if (included && !matchesSelection(selection.exclude, node.path)) {\n filtered.push(node);\n }\n continue;\n }\n const children = filterManifestNodes(node.children, selection);\n if (children.length) filtered.push(Object.freeze({ ...node, children }));\n }\n return filtered;\n}\n\nfunction matchesSelection(\n tree: unknown,\n path: readonly string[],\n): boolean {\n let current = tree;\n for (const segment of path) {\n if (current === true) return true;\n if (!current || typeof current !== \"object\" || Array.isArray(current)) {\n return false;\n }\n current = (current as Record<string, unknown>)[segment];\n }\n return current === true || (\n Boolean(current) && typeof current === \"object\" && !Array.isArray(current)\n );\n}\n\nfunction samePath(\n left: readonly string[],\n right: readonly string[],\n): boolean {\n return left.length === right.length &&\n left.every((segment, index) => segment === right[index]);\n}\n\nfunction isPathPrefix(\n prefix: readonly string[],\n path: readonly string[],\n): boolean {\n return prefix.length < path.length &&\n prefix.every((segment, index) => segment === path[index]);\n}\n","import {\n CapableError,\n executeLocatedCapability,\n subscribeLocatedCapability,\n type AnyCapability,\n type AnyObjectSchema,\n type AppDefinition,\n type AppContext,\n type InferSchemaInput,\n type LocatedCapability,\n type MaybePromise,\n type CapabilityDefinition,\n type CapabilityGroupDefinition,\n type CapabilityOutput,\n type CapabilityTree,\n type SubscribableCapability,\n} from \"@capable/core\";\nimport {\n createAppManifest,\n findManifestCapability,\n validateManifest,\n type ApplicationManifest,\n} from \"./manifest.ts\";\n\nexport type CapabilityCall = {\n readonly id: string;\n readonly input?: unknown;\n readonly signal?: AbortSignal;\n};\n\nexport type CapabilityResult = {\n readonly output: unknown;\n readonly markdown?: string;\n};\n\nexport interface ApplicationTransport {\n manifest(\n options?: { readonly signal?: AbortSignal },\n ): Promise<ApplicationManifest>;\n call(call: CapabilityCall): Promise<CapabilityResult>;\n subscribe(call: CapabilityCall): AsyncIterableIterator<CapabilityResult>;\n}\n\nexport const APP_RUNTIME_SYMBOL = Symbol.for(\"capable.app.runtime\");\nexport const APP_CAPABILITIES_SYMBOL = Symbol.for(\"capable.app.capabilities\");\nconst APP_CALL_SYMBOL = Symbol.for(\"capable.app.call\");\n\ntype AppCapabilityType<TCapability extends AnyCapability> = {\n readonly \"~capable/capability\"?: TCapability;\n};\n\ntype AppCapabilityInput<TCapability extends AnyCapability> =\n TCapability extends CapabilityDefinition<any, infer TInput, any, any, any>\n ? TInput extends AnyObjectSchema\n ? InferSchemaInput<TInput>\n : Record<never, never>\n : never;\n\ntype AppCapabilityOutput<TCapability extends AnyCapability> =\n TCapability extends CapabilityDefinition<any, any, infer TOutput, any, any>\n ? TOutput\n : never;\n\ntype AppCapabilityArgs<TCapability extends AnyCapability> =\n TCapability extends CapabilityDefinition<any, infer TInput, any, any, any>\n ? TInput extends undefined\n ? [\n input?: Record<never, never>,\n options?: { readonly signal?: AbortSignal },\n ]\n : [\n input: AppCapabilityInput<TCapability>,\n options?: { readonly signal?: AbortSignal },\n ]\n : never;\n\nexport type AppCapabilityCall<TCapability extends AnyCapability> = ((\n ...args: AppCapabilityArgs<TCapability>\n) => Promise<AppCapabilityOutput<TCapability>>) &\n AppCapabilityType<TCapability>;\n\nexport type AppCapabilityIsSubscribable<TCall> =\n TCall extends AppCapabilityType<infer TCapability>\n ? TCapability extends CapabilityDefinition<any, any, any, any, any, true>\n ? true\n : false\n : false;\n\nexport type CallableTree<TTree> =\n TTree extends AnyCapability\n ? AppCapabilityCall<TTree>\n : TTree extends CapabilityGroupDefinition<infer TCapabilities, any, any>\n ? CallableTree<TCapabilities>\n : TTree extends CapabilityTree\n ? { readonly [TKey in keyof TTree]: CallableTree<TTree[TKey]> }\n : never;\n\nexport type App<\n TCapabilities extends CapabilityTree = CapabilityTree,\n> = CallableTree<TCapabilities> & {\n readonly [APP_RUNTIME_SYMBOL]: ApplicationTransport;\n readonly [APP_CAPABILITIES_SYMBOL]?: TCapabilities;\n};\n\nexport type AnyApp = {\n readonly [APP_RUNTIME_SYMBOL]: ApplicationTransport;\n readonly [APP_CAPABILITIES_SYMBOL]?: CapabilityTree;\n};\n\ntype DefinitionCapabilities<TDefinition extends AppDefinition<any, any>> =\n TDefinition extends AppDefinition<infer TCapabilities>\n ? TCapabilities\n : never;\n\nexport type AppCapabilities<\n TApplication extends AnyApp,\n> = TApplication extends {\n readonly [APP_CAPABILITIES_SYMBOL]?: infer TCapabilities extends CapabilityTree;\n}\n ? TCapabilities\n : never;\n\nexport function getAppRuntime(\n app: AnyApp,\n): ApplicationTransport {\n return app[APP_RUNTIME_SYMBOL];\n}\n\nexport function isApp(value: unknown): value is AnyApp {\n return Boolean(\n value &&\n (typeof value === \"object\" || typeof value === \"function\") &&\n (value as Partial<AnyApp>)[APP_RUNTIME_SYMBOL],\n );\n}\n\nexport function appFromRuntime<\n TCapabilities extends CapabilityTree,\n>(\n runtime: ApplicationTransport,\n): App<TCapabilities> {\n let manifestPromise: Promise<ApplicationManifest> | undefined;\n const manifest = () => manifestPromise ??= runtime.manifest();\n\n const read = (path: readonly string[], root: boolean, key: PropertyKey) => {\n if (root && key === APP_RUNTIME_SYMBOL) return runtime;\n if (!root && key === APP_CALL_SYMBOL) return { runtime, path };\n if (key === \"then\") return undefined;\n if (typeof key !== \"string\") return undefined;\n return proxy([...path, key], false);\n };\n\n const proxy = (path: readonly string[], root: boolean): unknown => {\n if (root) {\n return new Proxy({}, {\n get(_target, key) {\n return read(path, true, key);\n },\n });\n }\n return new Proxy(() => undefined, {\n get(_target, key) {\n return read(path, false, key);\n },\n async apply(_target, _this, args: [unknown?, { signal?: AbortSignal }?]) {\n const capability = findManifestCapability(\n (await manifest()).capabilities,\n path,\n );\n if (!capability) {\n throw new Error(`No Capability exists at \"${path.join(\".\")}\"`);\n }\n const result = await runtime.call({\n id: capability.id,\n input: args[0] ?? {},\n ...(args[1]?.signal ? { signal: args[1].signal } : {}),\n });\n return result.output;\n },\n });\n };\n\n return proxy([], true) as App<TCapabilities>;\n}\n\nexport function subscribe<\n const TCapability extends SubscribableCapability,\n>(\n capabilityCall: AppCapabilityCall<TCapability>,\n ...args: AppCapabilityArgs<TCapability>\n): AsyncIterableIterator<CapabilityOutput<TCapability>> {\n const binding = (\n capabilityCall as unknown as Record<PropertyKey, unknown>\n )[APP_CALL_SYMBOL] as\n | {\n readonly runtime: ApplicationTransport;\n readonly path: readonly string[];\n }\n | undefined;\n if (!binding) {\n throw new Error(\"subscribe() requires a Capability call from a Capable App\");\n }\n\n return (async function* () {\n const manifest = await binding.runtime.manifest();\n const capability = findManifestCapability(\n manifest.capabilities,\n binding.path,\n );\n if (!capability) {\n throw new Error(`No Capability exists at \"${binding.path.join(\".\")}\"`);\n }\n if (!capability.subscription) {\n throw new Error(`Capability \"${capability.id}\" is not subscribable`);\n }\n const stream = binding.runtime.subscribe({\n id: capability.id,\n input: args[0] ?? {},\n ...(args[1]?.signal ? { signal: args[1].signal } : {}),\n });\n try {\n for await (const result of stream) {\n yield result.output as CapabilityOutput<TCapability>;\n }\n } finally {\n await stream.return?.();\n }\n })();\n}\n\nexport type AppContextProvider<TContext> = (call: {\n readonly id: string;\n readonly signal?: AbortSignal;\n}) => MaybePromise<TContext>;\n\ntype CreateAppOptions<TContext> = {\n readonly context: AppContextProvider<TContext>;\n};\n\ntype CreateAppArgs<TContext> = [TContext] extends [undefined]\n ? [options?: Partial<CreateAppOptions<TContext>>]\n : [options: CreateAppOptions<TContext>];\n\nconst localManifestCache = new WeakMap<object, ApplicationManifest>();\nconst localCapabilityCache = new WeakMap<\n object,\n ReadonlyMap<string, LocatedCapability>\n>();\n\nexport function createApp<\n const TDefinition extends AppDefinition<any, any>,\n>(\n definition: TDefinition,\n ...[options]: CreateAppArgs<AppContext<TDefinition>>\n): App<DefinitionCapabilities<TDefinition>> {\n let manifest = localManifestCache.get(definition);\n if (!manifest) {\n manifest = createAppManifest(definition);\n localManifestCache.set(definition, manifest);\n }\n let byId = localCapabilityCache.get(definition);\n if (!byId) {\n byId = new Map(\n definition.locatedCapabilities.map((entry) => [entry.capability.id, entry]),\n );\n localCapabilityCache.set(definition, byId);\n }\n\n const runtime: ApplicationTransport = Object.freeze({\n async manifest() {\n return manifest;\n },\n async call({ id, input = {}, signal }: CapabilityCall) {\n const located = byId.get(id);\n if (!located) throw new TransportError(\"NOT_FOUND\", { status: 404 });\n\n let context: unknown;\n try {\n context = options?.context\n ? await options.context({\n id,\n ...(signal ? { signal } : {}),\n })\n : undefined;\n } catch (cause) {\n throw new TransportError(\"INTERNAL_SERVER_ERROR\", { cause });\n }\n\n let output: unknown;\n try {\n output = await executeLocatedCapability(located, input, {\n context,\n ...(signal ? { signal } : {}),\n });\n } catch (error) {\n if (error instanceof CapableError) {\n throw new TransportError(error.code, {\n ...(error.message ? { message: error.message } : {}),\n ...(typeof error.status === \"number\"\n ? { status: error.status }\n : {}),\n ...(error.data === undefined ? {} : { data: error.data }),\n cause: error,\n });\n }\n throw new TransportError(\"INTERNAL_SERVER_ERROR\", { cause: error });\n }\n\n let markdown: string | undefined;\n try {\n markdown = located.capability.toMarkdown\n ? await located.capability.toMarkdown(output, { context })\n : undefined;\n } catch (cause) {\n throw new TransportError(\"INTERNAL_SERVER_ERROR\", { cause });\n }\n\n return {\n output,\n ...(markdown ? { markdown } : {}),\n };\n },\n async *subscribe({ id, input = {}, signal }: CapabilityCall) {\n const located = byId.get(id);\n if (!located) throw new TransportError(\"NOT_FOUND\", { status: 404 });\n if (!located.capability.subscribe) {\n throw new TransportError(\"NOT_FOUND\", { status: 404 });\n }\n\n let context: unknown;\n try {\n context = options?.context\n ? await options.context({\n id,\n ...(signal ? { signal } : {}),\n })\n : undefined;\n } catch (cause) {\n throw new TransportError(\"INTERNAL_SERVER_ERROR\", { cause });\n }\n\n const stream = subscribeLocatedCapability(located, input, {\n context,\n ...(signal ? { signal } : {}),\n });\n try {\n for await (const output of stream) {\n let markdown: string | undefined;\n try {\n markdown = located.capability.toMarkdown\n ? await located.capability.toMarkdown(output, { context })\n : undefined;\n } catch (cause) {\n throw new TransportError(\"INTERNAL_SERVER_ERROR\", { cause });\n }\n yield {\n output,\n ...(markdown ? { markdown } : {}),\n };\n }\n } catch (error) {\n throw toTransportError(error);\n } finally {\n await stream.return?.();\n }\n },\n });\n return appFromRuntime(runtime);\n}\n\nexport function createRemoteApp<\n TDefinition extends AppDefinition<any, any> = AppDefinition<CapabilityTree>,\n>(\n transport: ApplicationTransport,\n): App<DefinitionCapabilities<TDefinition>> {\n let resolved: Promise<ApplicationManifest> | undefined;\n const manifest = () =>\n resolved ??= transport.manifest().then((manifest) => {\n validateManifest(manifest, \"Remote App\");\n return manifest;\n });\n return appFromRuntime(Object.freeze({\n manifest,\n async call(call: CapabilityCall) {\n await manifest();\n return transport.call(call);\n },\n async *subscribe(call: CapabilityCall) {\n await manifest();\n yield* transport.subscribe(call);\n },\n }));\n}\n\nexport class TransportError extends Error {\n readonly code: string;\n readonly status: number | undefined;\n readonly data: unknown;\n\n constructor(\n code: string,\n options: {\n readonly message?: string;\n readonly status?: number;\n readonly data?: unknown;\n readonly cause?: unknown;\n } = {},\n ) {\n super(options.message ?? code, { cause: options.cause });\n this.name = \"TransportError\";\n this.code = code;\n this.status = options.status;\n this.data = options.data;\n }\n}\n\nfunction toTransportError(error: unknown): TransportError {\n if (error instanceof TransportError) return error;\n if (error instanceof CapableError) {\n return new TransportError(error.code, {\n ...(error.message ? { message: error.message } : {}),\n ...(typeof error.status === \"number\" ? { status: error.status } : {}),\n ...(error.data === undefined ? {} : { data: error.data }),\n cause: error,\n });\n }\n return new TransportError(\"INTERNAL_SERVER_ERROR\", { cause: error });\n}\n","import type {\n AnyCapability,\n CapabilityGroupDefinition,\n CapabilityTree,\n} from \"@capable/core\";\nimport {\n appFromRuntime,\n getAppRuntime,\n TransportError,\n type AnyApp,\n type App,\n type AppCapabilities,\n type ApplicationTransport,\n type CapabilityCall,\n} from \"./runtime.ts\";\nimport {\n flattenManifestCapabilities,\n mergeApplicationManifests,\n selectManifest,\n type ApplicationManifest,\n} from \"./manifest.ts\";\n\ntype UnionToIntersection<TValue> =\n (TValue extends unknown ? (value: TValue) => void : never) extends\n (value: infer TIntersection) => void ? TIntersection : never;\n\ntype MergedCapabilities<\n TApplications extends readonly AnyApp[],\n> = UnionToIntersection<AppCapabilities<TApplications[number]>> &\n CapabilityTree;\n\nexport function mergeApps<\n const TApplications extends readonly [\n AnyApp,\n ...AnyApp[],\n ],\n>(\n ...applications: TApplications\n): App<MergedCapabilities<TApplications>> {\n const runtimes = applications.map(getAppRuntime);\n let manifestPromise: Promise<ApplicationManifest> | undefined;\n let dispatch: ReadonlyMap<string, ApplicationTransport> | undefined;\n\n const resolve = () =>\n manifestPromise ??= Promise.all(\n runtimes.map((runtime) => runtime.manifest()),\n ).then((manifests) => {\n const nextDispatch = new Map<string, ApplicationTransport>();\n manifests.forEach((manifest, index) => {\n for (\n const capability of flattenManifestCapabilities(\n manifest.capabilities,\n )\n ) {\n nextDispatch.set(capability.id, runtimes[index]!);\n }\n });\n dispatch = nextDispatch;\n return mergeApplicationManifests(manifests);\n });\n\n return appFromRuntime(Object.freeze({\n manifest: resolve,\n async call(call: CapabilityCall) {\n await resolve();\n const target = dispatch?.get(call.id);\n if (!target) throw new TransportError(\"NOT_FOUND\", { status: 404 });\n return target.call(call);\n },\n async *subscribe(call: CapabilityCall) {\n await resolve();\n const target = dispatch?.get(call.id);\n if (!target) throw new TransportError(\"NOT_FOUND\", { status: 404 });\n yield* target.subscribe(call);\n },\n }));\n}\n\ntype SelectionNode<TTree> =\n TTree extends AnyCapability\n ? true\n : TTree extends CapabilityGroupDefinition<infer TCapabilities, any, any>\n ? true | SelectionNode<TCapabilities>\n : TTree extends CapabilityTree\n ? true | {\n readonly [TKey in keyof TTree]?: SelectionNode<TTree[TKey]>;\n }\n : never;\n\nexport type ApplicationSelection<\n TCapabilities extends CapabilityTree = CapabilityTree,\n> = {\n readonly include?: Exclude<SelectionNode<TCapabilities>, true>;\n readonly exclude?: Exclude<SelectionNode<TCapabilities>, true>;\n};\n\nexport function selectApp<\n const TCapabilities extends CapabilityTree,\n>(\n application: App<TCapabilities>,\n selection: ApplicationSelection<TCapabilities>,\n): App<TCapabilities> {\n const runtime = getAppRuntime(application);\n let resolved: Promise<ApplicationManifest> | undefined;\n let ids: ReadonlySet<string> | undefined;\n const manifest = () =>\n resolved ??= runtime.manifest().then((source) => {\n const selected = selectManifest(source, selection);\n ids = new Set(\n flattenManifestCapabilities(selected.capabilities).map(\n (capability) => capability.id,\n ),\n );\n return selected;\n });\n return appFromRuntime(Object.freeze({\n manifest,\n async call(call: CapabilityCall) {\n await manifest();\n if (!ids?.has(call.id)) {\n throw new TransportError(\"NOT_FOUND\", { status: 404 });\n }\n return runtime.call(call);\n },\n async *subscribe(call: CapabilityCall) {\n await manifest();\n if (!ids?.has(call.id)) {\n throw new TransportError(\"NOT_FOUND\", { status: 404 });\n }\n yield* runtime.subscribe(call);\n },\n }));\n}\n"],"mappings":";;AAmDA,SAAgB,kBACd,YACqB;CACrB,MAAM,eAAe,IAAI,IACvB,WAAW,oBAAoB,KAAK,UAAU,CAAC,MAAM,YAAY,KAAK,CAAC,CACzE;CACA,OAAO,OAAO,OAAO;EACnB,UAAU;EACV,MAAM,WAAW;EACjB,SAAS,WAAW;EACpB,GAAI,WAAW,cAAc,EAAE,aAAa,WAAW,YAAY,IAAI,CAAC;EACxE,cAAc,cAAc,WAAW,cAAc,YAAY;CACnE,CAAC;AACH;AAEA,SAAgB,iBACd,UACA,OACM;CACN,IAAI,SAAS,aAAa,2BACxB,MAAM,IAAI,MAAM,GAAG,MAAM,8BAA8B,SAAS,SAAS,EAAE;CAE7E,MAAM,eAAe,4BAA4B,SAAS,YAAY;CACtE,MAAM,sBAAM,IAAI,IAAY;CAC5B,MAAM,wBAAQ,IAAI,IAAY;CAC9B,KAAK,MAAM,cAAc,cAAc;EACrC,MAAM,OAAO,WAAW,KAAK,KAAK,GAAG;EACrC,IAAI,IAAI,IAAI,WAAW,EAAE,GACvB,MAAM,IAAI,MACR,GAAG,MAAM,qCAAqC,WAAW,GAAG,EAC9D;EAEF,IAAI,MAAM,IAAI,IAAI,GAChB,MAAM,IAAI,MAAM,GAAG,MAAM,uCAAuC,KAAK,EAAE;EAEzE,IAAI,IAAI,WAAW,EAAE;EACrB,MAAM,IAAI,IAAI;CAChB;CACA,sBAAsB,SAAS,cAAc,CAAC,GAAG,KAAK;AACxD;AAEA,SAAgB,0BACd,WACqB;CACrB,MAAM,YAAY,yBAAyB,SAAS;CACpD,IAAI,UAAU,QACZ,MAAM,IAAI,MACR,uBAAuB,UAAU,K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