capable-kit 0.0.1-alpha.0 → 0.0.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -0
- package/dist/ai-sdk.d.mts +2 -2
- package/dist/ai-sdk.mjs +1 -1
- package/dist/app.d.mts +1 -1
- package/dist/app.mjs +1 -1
- package/dist/bin.mjs +395 -47
- package/dist/bin.mjs.map +1 -1
- package/dist/cli.d.mts +27 -2
- package/dist/cli.mjs +2 -2
- package/dist/core.d.mts +3 -3
- package/dist/core.mjs +1 -1
- package/dist/eve.d.mts +1 -1
- package/dist/eve.mjs +1 -1
- package/dist/http.d.mts +1 -1
- package/dist/http.mjs +1 -1
- package/dist/{index-DLcYNpfo.d.mts → index-BB5ogMv-.d.mts} +17 -14
- package/dist/{index-C9ZThFPN.d.mts → index-BxJhwhKm.d.mts} +2 -2
- package/dist/{index-BuXwkN8G.d.mts → index-CThfifn8.d.mts} +4 -4
- package/dist/{index-u9ZOETR5.d.mts → index-Dp-D73pf.d.mts} +3 -3
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +2 -2
- package/dist/mcp.d.mts +2 -2
- package/dist/mcp.mjs +1 -139
- package/dist/mount--YHrgRHJ.mjs +140 -0
- package/dist/mount--YHrgRHJ.mjs.map +1 -0
- package/dist/runtime-support.d.mts +20 -0
- package/dist/runtime-support.mjs +325 -0
- package/dist/runtime-support.mjs.map +1 -0
- package/dist/server.d.mts +1 -1
- package/dist/server.mjs +1 -81
- package/dist/{src-CT4fbWqb.mjs → src-3UaBqI8H.mjs} +26 -16
- package/dist/src-3UaBqI8H.mjs.map +1 -0
- package/dist/{src-DX-_J6l2.mjs → src-B53ZbNtC.mjs} +6 -7
- package/dist/src-B53ZbNtC.mjs.map +1 -0
- package/dist/{src-CvWOGyGW.mjs → src-BjBxOv7x.mjs} +148 -96
- package/dist/src-BjBxOv7x.mjs.map +1 -0
- package/dist/src-DpSA-hBO.mjs +82 -0
- package/dist/src-DpSA-hBO.mjs.map +1 -0
- package/dist/{src-DlSJtEhv.mjs → src-XRJ9hgYX.mjs} +7 -7
- package/dist/src-XRJ9hgYX.mjs.map +1 -0
- package/dist/{subscription-CBnnIU42.d.mts → subscription-McaLyHM2.d.mts} +2 -2
- package/package.json +59 -17
- package/src/ai-sdk.ts +1 -0
- package/src/app.ts +1 -0
- package/src/bin.ts +4 -0
- package/src/cli.ts +1 -0
- package/src/core.ts +1 -0
- package/src/eve.ts +1 -0
- package/src/http.ts +1 -0
- package/src/index.test.ts +56 -0
- package/src/index.ts +38 -0
- package/src/mcp.ts +2 -0
- package/src/runtime-support.ts +1 -0
- package/src/server.ts +1 -0
- package/dist/mcp.mjs.map +0 -1
- package/dist/server.mjs.map +0 -1
- package/dist/src-CT4fbWqb.mjs.map +0 -1
- package/dist/src-CvWOGyGW.mjs.map +0 -1
- package/dist/src-DX-_J6l2.mjs.map +0 -1
- package/dist/src-DlSJtEhv.mjs.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Capable
|
|
2
|
+
|
|
3
|
+
Capable is a TypeScript framework for defining a capability once and exposing it
|
|
4
|
+
through the surfaces your application needs: typed calls, HTTP, CLI, MCP, AI
|
|
5
|
+
tools, and more.
|
|
6
|
+
|
|
7
|
+
Schemas may come from any library that implements both Standard Schema and
|
|
8
|
+
Standard JSON Schema. This example uses Zod as a provider:
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
pnpm add capable-kit zod
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { capability, defineApp } from "capable-kit";
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
|
|
18
|
+
const greet = capability({
|
|
19
|
+
id: "greet",
|
|
20
|
+
input: z.object({ name: z.string() }),
|
|
21
|
+
output: z.object({ message: z.string() }),
|
|
22
|
+
handler: ({ input }) => ({ message: `Hello, ${input.name}` }),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export default defineApp({
|
|
26
|
+
name: "Greeting",
|
|
27
|
+
capabilities: { greet },
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Capable is early alpha software. APIs may change without compatibility layers.
|
|
32
|
+
|
|
33
|
+
The package provides the `cap` CLI command. `capable` remains an alias:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
cap dev
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Package contents
|
|
40
|
+
|
|
41
|
+
The published package includes compiled ESM in `dist/`, declaration files, and
|
|
42
|
+
the original TypeScript authoring source in `src/` for inspection and debugging.
|
package/dist/ai-sdk.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { A as CapabilityGroupDefinition, c as CapabilityInput, f as CapabilityOutput, i as CapabilityDefinition, j as CapabilityTree, t as AnyCapability } from "./index-
|
|
2
|
-
import { c as AppCapabilities, o as AnyApp } from "./index-
|
|
1
|
+
import { A as CapabilityGroupDefinition, c as CapabilityInput, f as CapabilityOutput, i as CapabilityDefinition, j as CapabilityTree, t as AnyCapability } from "./index-BB5ogMv-.mjs";
|
|
2
|
+
import { c as AppCapabilities, o as AnyApp } from "./index-BxJhwhKm.mjs";
|
|
3
3
|
import { Tool, ToolExecutionOptions } from "ai";
|
|
4
4
|
//#region packages/ai-sdk/src/index.d.ts
|
|
5
5
|
type AiSdkCapabilityMeta = false | {
|
package/dist/ai-sdk.mjs
CHANGED
package/dist/app.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { C as JsonSchema, D as ManifestNode, E as ManifestGroup, O as createAppManifest, S as ApplicationManifest, T as ManifestError, _ as createApp, a as APP_RUNTIME_SYMBOL, b as isApp, c as AppCapabilities, d as AppContextProvider, f as ApplicationTransport, g as TransportError, h as CapabilityResult, i as APP_CAPABILITIES_SYMBOL, l as AppCapabilityCall, m as CapabilityCall, n as mergeApps, o as AnyApp, p as CallableTree, r as selectApp, s as App, t as ApplicationSelection, u as AppCapabilityIsSubscribable, v as createRemoteApp, w as ManifestCapability, x as subscribe, y as getAppRuntime } from "./index-
|
|
1
|
+
import { C as JsonSchema, D as ManifestNode, E as ManifestGroup, O as createAppManifest, S as ApplicationManifest, T as ManifestError, _ as createApp, a as APP_RUNTIME_SYMBOL, b as isApp, c as AppCapabilities, d as AppContextProvider, f as ApplicationTransport, g as TransportError, h as CapabilityResult, i as APP_CAPABILITIES_SYMBOL, l as AppCapabilityCall, m as CapabilityCall, n as mergeApps, o as AnyApp, p as CallableTree, r as selectApp, s as App, t as ApplicationSelection, u as AppCapabilityIsSubscribable, v as createRemoteApp, w as ManifestCapability, x as subscribe, y as getAppRuntime } from "./index-BxJhwhKm.mjs";
|
|
2
2
|
export { APP_CAPABILITIES_SYMBOL, APP_RUNTIME_SYMBOL, type AnyApp, type App, type AppCapabilities, type AppCapabilityCall, type AppCapabilityIsSubscribable, type AppContextProvider, type ApplicationManifest, type ApplicationSelection, type ApplicationTransport, type CallableTree, type CapabilityCall, type CapabilityResult, type JsonSchema, type ManifestCapability, type ManifestError, type ManifestGroup, type ManifestNode, TransportError, createApp, createAppManifest, createRemoteApp, getAppRuntime, isApp, mergeApps, selectApp, subscribe };
|
package/dist/app.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as TransportError, c as getAppRuntime, d as createAppManifest, i as APP_RUNTIME_SYMBOL, l as isApp, n as selectApp, o as createApp, r as APP_CAPABILITIES_SYMBOL, s as createRemoteApp, t as mergeApps, u as subscribe } from "./src-
|
|
1
|
+
import { a as TransportError, c as getAppRuntime, d as createAppManifest, i as APP_RUNTIME_SYMBOL, l as isApp, n as selectApp, o as createApp, r as APP_CAPABILITIES_SYMBOL, s as createRemoteApp, t as mergeApps, u as subscribe } from "./src-3UaBqI8H.mjs";
|
|
2
2
|
export { APP_CAPABILITIES_SYMBOL, APP_RUNTIME_SYMBOL, TransportError, createApp, createAppManifest, createRemoteApp, getAppRuntime, isApp, mergeApps, selectApp, subscribe };
|
package/dist/bin.mjs
CHANGED
|
@@ -1,18 +1,52 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { t as APPLICATION_SYMBOL } from "./src-
|
|
3
|
-
import { l as isApp, o as createApp, s as createRemoteApp } from "./src-
|
|
4
|
-
import { n as runCli } from "./src-
|
|
5
|
-
import { n as createHttpTransport } from "./src-
|
|
2
|
+
import { t as APPLICATION_SYMBOL } from "./src-B53ZbNtC.mjs";
|
|
3
|
+
import { l as isApp, o as createApp, s as createRemoteApp } from "./src-3UaBqI8H.mjs";
|
|
4
|
+
import { a as createCliStyle, c as renderSection, l as supportsColor, n as runCli, o as descriptionColumn, s as renderRows } from "./src-BjBxOv7x.mjs";
|
|
5
|
+
import { n as createHttpTransport } from "./src-XRJ9hgYX.mjs";
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
import { spawn } from "node:child_process";
|
|
8
8
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
9
|
-
import {
|
|
9
|
+
import { existsSync } from "node:fs";
|
|
10
|
+
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
10
11
|
import { build, createDevServer, createNitro, prepare } from "nitro/builder";
|
|
11
12
|
//#region packages/nitro-host/src/index.ts
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
const virtualServerEntry = "#capable/entry";
|
|
14
|
+
/**
|
|
15
|
+
* Support code for the virtual entry. It sits beside this module as
|
|
16
|
+
* TypeScript in the repository and as bundled JavaScript in a published
|
|
17
|
+
* package, so resolve whichever extension is actually there.
|
|
18
|
+
*/
|
|
19
|
+
function runtimeSupportModule() {
|
|
20
|
+
for (const extension of [
|
|
21
|
+
".ts",
|
|
22
|
+
".mjs",
|
|
23
|
+
".js"
|
|
24
|
+
]) {
|
|
25
|
+
const candidate = fileURLToPath(new URL(`./runtime-support${extension}`, import.meta.url));
|
|
26
|
+
if (existsSync(candidate)) return candidate;
|
|
27
|
+
}
|
|
28
|
+
throw new Error("Missing the Capable Nitro runtime support module");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Render Nitro's in-memory server entry. Keeping the entry virtual lets Nitro
|
|
32
|
+
* transform the project's `#capable/*` aliases without writing generated files
|
|
33
|
+
* into the project.
|
|
34
|
+
*/
|
|
35
|
+
function renderServerEntry(project, options) {
|
|
36
|
+
const hasServer = Boolean(project.serverEntry);
|
|
37
|
+
return [
|
|
38
|
+
`import { createCapableNitroEntry } from ${JSON.stringify(runtimeSupportModule())};`,
|
|
39
|
+
"import definition from \"#capable/app\";",
|
|
40
|
+
...hasServer ? ["import server from \"#capable/server\";"] : [],
|
|
41
|
+
"",
|
|
42
|
+
"export default createCapableNitroEntry({",
|
|
43
|
+
" definition,",
|
|
44
|
+
...hasServer ? [" server,"] : [],
|
|
45
|
+
` dev: ${options.dev},`,
|
|
46
|
+
"});",
|
|
47
|
+
""
|
|
48
|
+
].join("\n");
|
|
49
|
+
}
|
|
16
50
|
async function startCapableNitroDevServer(options) {
|
|
17
51
|
const nitro = await createNitro(createCapableNitroConfig(options.project, { dev: true }));
|
|
18
52
|
const server = createDevServer(nitro);
|
|
@@ -57,10 +91,10 @@ async function resolveCapableNitroPreviewCommand(outputDir) {
|
|
|
57
91
|
try {
|
|
58
92
|
manifest = JSON.parse(await readFile(manifestPath, "utf8"));
|
|
59
93
|
} catch {
|
|
60
|
-
throw new Error(`Could not read a runnable Capable build at ${manifestPath}. Run \`
|
|
94
|
+
throw new Error(`Could not read a runnable Capable build at ${manifestPath}. Run \`cap build\` with the node-server preset first.`);
|
|
61
95
|
}
|
|
62
96
|
const command = manifest.commands?.preview;
|
|
63
|
-
if (typeof command !== "string" || !command.trim()) throw new Error(`The Nitro ${String(manifest.preset ?? "deployment")} preset does not produce a locally runnable preview command. Build with the node-server preset to use \`
|
|
97
|
+
if (typeof command !== "string" || !command.trim()) throw new Error(`The Nitro ${String(manifest.preset ?? "deployment")} preset does not produce a locally runnable preview command. Build with the node-server preset to use \`cap preview\`.`);
|
|
64
98
|
return {
|
|
65
99
|
command,
|
|
66
100
|
cwd
|
|
@@ -78,7 +112,8 @@ function createCapableNitroConfig(project, options) {
|
|
|
78
112
|
"#capable/server": serverEntry,
|
|
79
113
|
"#capable/app": entry
|
|
80
114
|
} : { "#capable/app": entry },
|
|
81
|
-
|
|
115
|
+
virtual: { [virtualServerEntry]: renderServerEntry(project, { dev: options.dev }) },
|
|
116
|
+
serverEntry: virtualServerEntry,
|
|
82
117
|
serverDir: false,
|
|
83
118
|
scanDirs: [],
|
|
84
119
|
imports: false,
|
|
@@ -90,8 +125,137 @@ function createCapableNitroConfig(project, options) {
|
|
|
90
125
|
};
|
|
91
126
|
}
|
|
92
127
|
//#endregion
|
|
128
|
+
//#region packages/toolchain/src/help.ts
|
|
129
|
+
const HELP_OPTION = {
|
|
130
|
+
label: "-h, --help",
|
|
131
|
+
description: "Show help"
|
|
132
|
+
};
|
|
133
|
+
const APP_OPTION = {
|
|
134
|
+
label: "--app <path>",
|
|
135
|
+
description: "Project directory or App entry"
|
|
136
|
+
};
|
|
137
|
+
const COMMANDS = [
|
|
138
|
+
{
|
|
139
|
+
label: "init [directory]",
|
|
140
|
+
description: "Create a starter Capable App"
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
label: "dev [project]",
|
|
144
|
+
description: "Run the development server"
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
label: "build [project]",
|
|
148
|
+
description: "Build the App for deployment"
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
label: "preview [project]",
|
|
152
|
+
description: "Serve the built App"
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
label: "cli <command...>",
|
|
156
|
+
description: "Call Capabilities from your terminal"
|
|
157
|
+
}
|
|
158
|
+
];
|
|
159
|
+
const TOPICS = {
|
|
160
|
+
init: {
|
|
161
|
+
usage: "cap init [directory]",
|
|
162
|
+
summary: "Create a runnable App package without overwriting existing work.",
|
|
163
|
+
options: [HELP_OPTION]
|
|
164
|
+
},
|
|
165
|
+
dev: {
|
|
166
|
+
usage: "cap dev [project] [options]",
|
|
167
|
+
summary: "Run the development server with live reload.",
|
|
168
|
+
options: [
|
|
169
|
+
{
|
|
170
|
+
label: "--host <host>",
|
|
171
|
+
description: "Hostname to bind (default: localhost)"
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
label: "--port <port>",
|
|
175
|
+
description: "Port to bind (default: 3000)"
|
|
176
|
+
},
|
|
177
|
+
APP_OPTION,
|
|
178
|
+
HELP_OPTION
|
|
179
|
+
]
|
|
180
|
+
},
|
|
181
|
+
build: {
|
|
182
|
+
usage: "cap build [project] [options]",
|
|
183
|
+
summary: "Build the App for deployment.",
|
|
184
|
+
options: [
|
|
185
|
+
{
|
|
186
|
+
label: "--output <dir>",
|
|
187
|
+
description: "Build directory (default: .output)"
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
label: "--preset <preset>",
|
|
191
|
+
description: "Nitro preset (default: node-server)"
|
|
192
|
+
},
|
|
193
|
+
APP_OPTION,
|
|
194
|
+
HELP_OPTION
|
|
195
|
+
]
|
|
196
|
+
},
|
|
197
|
+
preview: {
|
|
198
|
+
usage: "cap preview [project] [options]",
|
|
199
|
+
summary: "Serve a previous build the way production will.",
|
|
200
|
+
options: [
|
|
201
|
+
{
|
|
202
|
+
label: "--output <dir>",
|
|
203
|
+
description: "Build directory (default: .output)"
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
label: "--host <host>",
|
|
207
|
+
description: "Hostname to bind (default: localhost)"
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
label: "--port <port>",
|
|
211
|
+
description: "Port to bind (default: 3000)"
|
|
212
|
+
},
|
|
213
|
+
APP_OPTION,
|
|
214
|
+
HELP_OPTION
|
|
215
|
+
]
|
|
216
|
+
},
|
|
217
|
+
cli: {
|
|
218
|
+
usage: "cap cli [options] <command...>",
|
|
219
|
+
summary: "Call your App's Capabilities from the terminal.",
|
|
220
|
+
options: [{
|
|
221
|
+
label: "--app <path|url>",
|
|
222
|
+
description: "Project, App entry, or a running Capable server URL"
|
|
223
|
+
}, HELP_OPTION],
|
|
224
|
+
footer: "Run `cap cli -- --help` to list the commands your App derives."
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
function renderCapableHelp(topic, options = {}) {
|
|
228
|
+
const style = createCliStyle(options.color ?? supportsColor());
|
|
229
|
+
return topic ? renderTopicHelp(topic, style) : renderRootHelp(style);
|
|
230
|
+
}
|
|
231
|
+
function renderRootHelp(style) {
|
|
232
|
+
const options = [HELP_OPTION];
|
|
233
|
+
const column = descriptionColumn([...COMMANDS, ...options]);
|
|
234
|
+
return page([
|
|
235
|
+
banner("cap <command> [options]", "Develop, build, and call Capable Apps", style, "Capable"),
|
|
236
|
+
renderSection("Commands", renderRows(COMMANDS, style, { column }), style),
|
|
237
|
+
renderSection("Options", renderRows(options, style, { column }), style),
|
|
238
|
+
renderSection("Project discovery", ` ${style.dim("[project] or --app → CAPABLE_APP → package.json \"capable\" → capable.app.*")}`, style),
|
|
239
|
+
` ${style.dim("Run `cap <command> --help` for details on a command.")}`
|
|
240
|
+
]);
|
|
241
|
+
}
|
|
242
|
+
function renderTopicHelp(topic, style) {
|
|
243
|
+
const { usage, summary, options, footer } = TOPICS[topic];
|
|
244
|
+
return page([
|
|
245
|
+
banner(usage, summary, style),
|
|
246
|
+
renderSection("Options", renderRows(options, style), style),
|
|
247
|
+
footer ? ` ${style.dim(footer)}` : void 0
|
|
248
|
+
]);
|
|
249
|
+
}
|
|
250
|
+
function banner(usage, summary, style, title) {
|
|
251
|
+
return title ? [` ${style.bold(title)}\n ${style.dim(summary)}`, ` ${style.accent(usage)}`].join("\n\n") : ` ${style.bold(usage)}\n\n ${style.dim(summary)}`;
|
|
252
|
+
}
|
|
253
|
+
function page(sections) {
|
|
254
|
+
return `\n${sections.filter(Boolean).join("\n\n")}\n`;
|
|
255
|
+
}
|
|
256
|
+
//#endregion
|
|
93
257
|
//#region packages/toolchain/src/project.ts
|
|
94
|
-
const
|
|
258
|
+
const CAPABLE_APPLICATION_ENTRIES = [
|
|
95
259
|
"capable.app.ts",
|
|
96
260
|
"capable.app.tsx",
|
|
97
261
|
"capable.app.js",
|
|
@@ -117,7 +281,7 @@ async function resolveCapableProject(options = {}) {
|
|
|
117
281
|
entry = candidate;
|
|
118
282
|
rootDir = await findPackageRoot(path.dirname(candidate)) ?? path.dirname(candidate);
|
|
119
283
|
} else if (candidateStat?.isDirectory()) {
|
|
120
|
-
entry = await findEntry(candidate,
|
|
284
|
+
entry = await findEntry(candidate, CAPABLE_APPLICATION_ENTRIES);
|
|
121
285
|
rootDir = await findPackageRoot(candidate) ?? candidate;
|
|
122
286
|
} else throw projectNotFound(candidate, configured);
|
|
123
287
|
if (!entry) throw projectNotFound(candidate, configured);
|
|
@@ -152,7 +316,127 @@ async function findEntry(directory, names) {
|
|
|
152
316
|
}
|
|
153
317
|
}
|
|
154
318
|
function projectNotFound(candidate, configured) {
|
|
155
|
-
return /* @__PURE__ */ new Error(configured ? `Could not find a Capable application at ${candidate}` : `Could not find ${
|
|
319
|
+
return /* @__PURE__ */ new Error(configured ? `Could not find a Capable application at ${candidate}` : `Could not find ${CAPABLE_APPLICATION_ENTRIES.join(", ")} in ${candidate}`);
|
|
320
|
+
}
|
|
321
|
+
//#endregion
|
|
322
|
+
//#region packages/toolchain/src/init.ts
|
|
323
|
+
function starter(appName) {
|
|
324
|
+
return `import { capability, defineApp } from "capable-kit";
|
|
325
|
+
import { z } from "zod";
|
|
326
|
+
|
|
327
|
+
const hello = capability({
|
|
328
|
+
id: "hello",
|
|
329
|
+
title: "Say hello",
|
|
330
|
+
effect: "read",
|
|
331
|
+
output: z.object({ message: z.string() }),
|
|
332
|
+
handler: () => ({ message: "Hello from Capable" }),
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
export default defineApp({
|
|
336
|
+
name: ${JSON.stringify(appName)},
|
|
337
|
+
capabilities: { hello },
|
|
338
|
+
});
|
|
339
|
+
`;
|
|
340
|
+
}
|
|
341
|
+
const SCRIPTS = {
|
|
342
|
+
dev: "cap dev",
|
|
343
|
+
build: "cap build",
|
|
344
|
+
preview: "cap preview"
|
|
345
|
+
};
|
|
346
|
+
const DEPENDENCIES = {
|
|
347
|
+
"capable-kit": "latest",
|
|
348
|
+
zod: "^4.0.0"
|
|
349
|
+
};
|
|
350
|
+
async function initializeCapableProject(options) {
|
|
351
|
+
const directory = path.resolve(options.directory);
|
|
352
|
+
await mkdir(directory, { recursive: true });
|
|
353
|
+
const packageJson = path.join(directory, "package.json");
|
|
354
|
+
const existingPackage = await readPackageJson(packageJson);
|
|
355
|
+
if (typeof existingPackage?.capable === "string") throw new Error(`package.json already configures a Capable application at ${existingPackage.capable}`);
|
|
356
|
+
for (const name of CAPABLE_APPLICATION_ENTRIES) {
|
|
357
|
+
const existing = path.join(directory, name);
|
|
358
|
+
if ((await stat(existing).catch(() => void 0))?.isFile()) throw new Error(`A Capable application already exists at ${existing}`);
|
|
359
|
+
}
|
|
360
|
+
const entry = path.join(directory, CAPABLE_APPLICATION_ENTRIES[0]);
|
|
361
|
+
const packageResult = preparePackageJson(directory, existingPackage);
|
|
362
|
+
await writeFile(entry, starter(applicationName(directory, packageResult.value)), { flag: "wx" });
|
|
363
|
+
if (packageResult.action !== "unchanged") await writeFile(packageJson, `${JSON.stringify(packageResult.value, null, 2)}\n`, existingPackage ? void 0 : { flag: "wx" });
|
|
364
|
+
return {
|
|
365
|
+
entry,
|
|
366
|
+
packageJson,
|
|
367
|
+
packageAction: packageResult.action
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
async function readPackageJson(filename) {
|
|
371
|
+
let source;
|
|
372
|
+
try {
|
|
373
|
+
source = await readFile(filename, "utf8");
|
|
374
|
+
} catch (error) {
|
|
375
|
+
if (error.code === "ENOENT") return void 0;
|
|
376
|
+
throw error;
|
|
377
|
+
}
|
|
378
|
+
const value = JSON.parse(source);
|
|
379
|
+
if (!isRecord(value)) throw new Error(`${filename} must contain a JSON object`);
|
|
380
|
+
return value;
|
|
381
|
+
}
|
|
382
|
+
function preparePackageJson(directory, existing) {
|
|
383
|
+
if (!existing) return {
|
|
384
|
+
action: "created",
|
|
385
|
+
value: {
|
|
386
|
+
name: packageName(directory),
|
|
387
|
+
private: true,
|
|
388
|
+
version: "0.0.0",
|
|
389
|
+
type: "module",
|
|
390
|
+
scripts: SCRIPTS,
|
|
391
|
+
dependencies: DEPENDENCIES
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
let changed = false;
|
|
395
|
+
const value = { ...existing };
|
|
396
|
+
const scripts = readObjectField(value, "scripts");
|
|
397
|
+
for (const [name, command] of Object.entries(SCRIPTS)) {
|
|
398
|
+
if (name in scripts) continue;
|
|
399
|
+
scripts[name] = command;
|
|
400
|
+
changed = true;
|
|
401
|
+
}
|
|
402
|
+
if (changed || !("scripts" in value)) value.scripts = scripts;
|
|
403
|
+
const dependencies = readObjectField(value, "dependencies");
|
|
404
|
+
for (const [name, version] of Object.entries(DEPENDENCIES)) {
|
|
405
|
+
if (hasDeclaredDependency(value, name)) continue;
|
|
406
|
+
dependencies[name] = version;
|
|
407
|
+
changed = true;
|
|
408
|
+
}
|
|
409
|
+
if (Object.keys(dependencies).length || "dependencies" in value) value.dependencies = dependencies;
|
|
410
|
+
return {
|
|
411
|
+
value,
|
|
412
|
+
action: changed ? "updated" : "unchanged"
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
function readObjectField(value, field) {
|
|
416
|
+
const current = value[field];
|
|
417
|
+
if (current === void 0) return {};
|
|
418
|
+
if (!isRecord(current)) throw new Error(`package.json "${field}" must be an object`);
|
|
419
|
+
return { ...current };
|
|
420
|
+
}
|
|
421
|
+
function hasDeclaredDependency(packageJson, name) {
|
|
422
|
+
return [
|
|
423
|
+
"dependencies",
|
|
424
|
+
"devDependencies",
|
|
425
|
+
"peerDependencies",
|
|
426
|
+
"optionalDependencies"
|
|
427
|
+
].some((field) => {
|
|
428
|
+
const dependencies = packageJson[field];
|
|
429
|
+
return isRecord(dependencies) && name in dependencies;
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
function packageName(directory) {
|
|
433
|
+
return path.basename(directory).toLowerCase().replace(/[^a-z0-9._-]+/g, "-").replace(/^[-._]+|[-._]+$/g, "") || "capable-app";
|
|
434
|
+
}
|
|
435
|
+
function applicationName(directory, packageJson) {
|
|
436
|
+
return ((typeof packageJson.name === "string" ? packageJson.name.split("/").at(-1) : void 0) || path.basename(directory)).split(/[-_.\s]+/).filter(Boolean).map((part) => `${part[0].toUpperCase()}${part.slice(1)}`).join(" ") || "Capable App";
|
|
437
|
+
}
|
|
438
|
+
function isRecord(value) {
|
|
439
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
156
440
|
}
|
|
157
441
|
//#endregion
|
|
158
442
|
//#region packages/toolchain/src/index.ts
|
|
@@ -163,7 +447,7 @@ async function runCapableCommand(argv, options = {}) {
|
|
|
163
447
|
try {
|
|
164
448
|
const command = parseCapableCommand(argv);
|
|
165
449
|
if (command.command === "help") {
|
|
166
|
-
stdout(renderCapableHelp());
|
|
450
|
+
stdout(renderCapableHelp(command.topic, { color: !options.stdout && Boolean(process.stdout.isTTY) }));
|
|
167
451
|
return 0;
|
|
168
452
|
}
|
|
169
453
|
if (command.command === "cli") return runCli({
|
|
@@ -176,6 +460,23 @@ async function runCapableCommand(argv, options = {}) {
|
|
|
176
460
|
...options.stderr ? { stderr: options.stderr } : {}
|
|
177
461
|
} } : {}
|
|
178
462
|
});
|
|
463
|
+
if (command.command === "init") {
|
|
464
|
+
const cwd = path.resolve(options.cwd ?? process.cwd());
|
|
465
|
+
const result = await initializeCapableProject({ directory: path.resolve(cwd, command.project ?? ".") });
|
|
466
|
+
const entry = path.relative(cwd, result.entry) || path.basename(result.entry);
|
|
467
|
+
const packageJson = path.relative(cwd, result.packageJson) || path.basename(result.packageJson);
|
|
468
|
+
stdout([
|
|
469
|
+
`Created ${entry}`,
|
|
470
|
+
...result.packageAction === "unchanged" ? [] : [`${capitalize(result.packageAction)} ${packageJson}`],
|
|
471
|
+
"",
|
|
472
|
+
"Next:",
|
|
473
|
+
...command.project ? [` cd ${shellArgument(command.project)}`] : [],
|
|
474
|
+
" pnpm install",
|
|
475
|
+
" pnpm dev",
|
|
476
|
+
""
|
|
477
|
+
].join("\n"));
|
|
478
|
+
return 0;
|
|
479
|
+
}
|
|
179
480
|
const project = await resolveCapableProject({
|
|
180
481
|
...command.project ? { project: command.project } : {},
|
|
181
482
|
...options.cwd ? { cwd: options.cwd } : {},
|
|
@@ -188,7 +489,10 @@ async function runCapableCommand(argv, options = {}) {
|
|
|
188
489
|
...command.port !== void 0 ? { port: command.port } : {}
|
|
189
490
|
});
|
|
190
491
|
try {
|
|
191
|
-
stdout(await renderDevReady(server, {
|
|
492
|
+
stdout(await renderDevReady(server, {
|
|
493
|
+
hasAuthoredServer: Boolean(project.serverEntry),
|
|
494
|
+
color: !options.stdout && Boolean(process.stdout.isTTY)
|
|
495
|
+
}));
|
|
192
496
|
await waitForShutdown();
|
|
193
497
|
} finally {
|
|
194
498
|
await server.close();
|
|
@@ -224,7 +528,7 @@ async function runCapableCommand(argv, options = {}) {
|
|
|
224
528
|
} catch (error) {
|
|
225
529
|
stderr(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
226
530
|
if (error instanceof UsageError) {
|
|
227
|
-
stderr("Run `
|
|
531
|
+
stderr("Run `cap --help` for usage.\n");
|
|
228
532
|
return 2;
|
|
229
533
|
}
|
|
230
534
|
return 1;
|
|
@@ -233,10 +537,14 @@ async function runCapableCommand(argv, options = {}) {
|
|
|
233
537
|
function parseCapableCommand(argv) {
|
|
234
538
|
const [rawCommand, ...rest] = argv;
|
|
235
539
|
if (rawCommand === void 0 || rawCommand === "help" || rawCommand === "--help" || rawCommand === "-h") return { command: "help" };
|
|
236
|
-
if (rawCommand === "start") throw new UsageError("`
|
|
540
|
+
if (rawCommand === "start") throw new UsageError("`cap start` was renamed to `cap preview`.");
|
|
237
541
|
if (rawCommand === "cli") return parseCli(rest);
|
|
542
|
+
if (rawCommand === "init") return parseInit(rest);
|
|
238
543
|
if (rawCommand !== "dev" && rawCommand !== "build" && rawCommand !== "preview") throw new UsageError(`Unknown Capable command: ${rawCommand}`);
|
|
239
|
-
if (rest.includes("--help") || rest.includes("-h")) return {
|
|
544
|
+
if (rest.includes("--help") || rest.includes("-h")) return {
|
|
545
|
+
command: "help",
|
|
546
|
+
topic: rawCommand
|
|
547
|
+
};
|
|
240
548
|
const values = [...rest];
|
|
241
549
|
let project;
|
|
242
550
|
let host;
|
|
@@ -246,7 +554,7 @@ function parseCapableCommand(argv) {
|
|
|
246
554
|
while (values.length) {
|
|
247
555
|
const token = values.shift();
|
|
248
556
|
if (!token.startsWith("-")) {
|
|
249
|
-
if (project) throw new UsageError(`Unexpected argument for
|
|
557
|
+
if (project) throw new UsageError(`Unexpected argument for cap ${rawCommand}: ${token}`);
|
|
250
558
|
project = token;
|
|
251
559
|
continue;
|
|
252
560
|
}
|
|
@@ -283,12 +591,28 @@ function parseCapableCommand(argv) {
|
|
|
283
591
|
...preset ? { preset } : {}
|
|
284
592
|
};
|
|
285
593
|
}
|
|
594
|
+
function parseInit(argv) {
|
|
595
|
+
if (argv.includes("--help") || argv.includes("-h")) return {
|
|
596
|
+
command: "help",
|
|
597
|
+
topic: "init"
|
|
598
|
+
};
|
|
599
|
+
if (argv.length > 1) throw new UsageError(`Unexpected argument for cap init: ${argv[1]}`);
|
|
600
|
+
const project = argv[0];
|
|
601
|
+
if (project?.startsWith("-")) throw unsupportedOption("init", project);
|
|
602
|
+
return {
|
|
603
|
+
command: "init",
|
|
604
|
+
...project ? { project } : {}
|
|
605
|
+
};
|
|
606
|
+
}
|
|
286
607
|
function parseCli(argv) {
|
|
287
608
|
const values = [...argv];
|
|
288
609
|
let app;
|
|
289
610
|
while (values.length) {
|
|
290
611
|
const token = values[0];
|
|
291
|
-
if (token === "--help" || token === "-h") return {
|
|
612
|
+
if (token === "--help" || token === "-h") return {
|
|
613
|
+
command: "help",
|
|
614
|
+
topic: "cli"
|
|
615
|
+
};
|
|
292
616
|
if (token === "--") {
|
|
293
617
|
values.shift();
|
|
294
618
|
break;
|
|
@@ -315,22 +639,13 @@ function parsePort(value) {
|
|
|
315
639
|
return port;
|
|
316
640
|
}
|
|
317
641
|
function unsupportedOption(command, option) {
|
|
318
|
-
return new UsageError(`Unknown option for
|
|
642
|
+
return new UsageError(`Unknown option for cap ${command}: ${option}`);
|
|
319
643
|
}
|
|
320
|
-
function
|
|
321
|
-
return [
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
" capable dev [project] [--host localhost] [--port 3000]",
|
|
326
|
-
" capable build [project] [--output .output] [--preset node-server]",
|
|
327
|
-
" capable preview [project] [--output .output] [--host localhost] [--port 3000]",
|
|
328
|
-
" capable cli [--app <project-or-url>] <command...>",
|
|
329
|
-
"",
|
|
330
|
-
"Project discovery:",
|
|
331
|
-
" [project] or --app → CAPABLE_APP → package.json \"capable\" → capable.app.*",
|
|
332
|
-
""
|
|
333
|
-
].join("\n");
|
|
644
|
+
function capitalize(value) {
|
|
645
|
+
return `${value[0]?.toUpperCase() ?? ""}${value.slice(1)}`;
|
|
646
|
+
}
|
|
647
|
+
function shellArgument(value) {
|
|
648
|
+
return /^[a-zA-Z0-9_./-]+$/.test(value) ? value : `'${value.replaceAll("'", "'\\''")}'`;
|
|
334
649
|
}
|
|
335
650
|
async function resolveCliApp(configured, options) {
|
|
336
651
|
if (configured && isHttpUrl(configured)) return createRemoteApp(createHttpTransport({ url: new URL("/_capable/", configured) }));
|
|
@@ -368,18 +683,51 @@ async function renderDevReady(server, options) {
|
|
|
368
683
|
const manifest = await readJson(`${origin}/_capable/manifest`);
|
|
369
684
|
const openapi = await readJson(`${origin}/openapi.json`);
|
|
370
685
|
const mcp = await probeMcp(origin);
|
|
371
|
-
const
|
|
686
|
+
const paint = createPainter(options.color ?? false);
|
|
687
|
+
const routes = [["Local", displayOrigin(origin)]];
|
|
688
|
+
if (manifest) routes.push(["Standard", "/_capable"]);
|
|
689
|
+
if (openapi) routes.push(["HTTP", "/api"], ["OpenAPI", "/openapi.json"]);
|
|
690
|
+
if (mcp) routes.push(["MCP", "/mcp"]);
|
|
691
|
+
if (!manifest && !openapi && options.hasAuthoredServer) routes.push(["Server", "capable.server.ts"]);
|
|
692
|
+
const width = routes.reduce((max, [label]) => Math.max(max, label.length), 0);
|
|
693
|
+
const heading = manifest ? `${paint.bold(manifest.name)} ${paint.dim(manifest.version)}` : paint.bold("Capable");
|
|
694
|
+
const summary = manifest ? `${plural(countCapabilities(manifest.capabilities), "capability", "capabilities")} ready, watching for changes` : "Ready, watching for changes";
|
|
695
|
+
return [
|
|
696
|
+
"",
|
|
697
|
+
`${paint.accent("◆")} ${heading}`,
|
|
698
|
+
"",
|
|
699
|
+
...routes.map(([label, value]) => `${paint.dim(label.padEnd(width))} ${value}`),
|
|
372
700
|
"",
|
|
373
|
-
|
|
701
|
+
`${paint.accent("✓")} ${paint.dim(summary)}`,
|
|
702
|
+
"",
|
|
703
|
+
...manifest ? [
|
|
704
|
+
`${paint.dim("Call these capabilities from your terminal:")}`,
|
|
705
|
+
` ${paint.bold("cap cli --help")} ${paint.dim("every command, typed and documented")}`,
|
|
706
|
+
""
|
|
707
|
+
] : [],
|
|
374
708
|
""
|
|
375
|
-
];
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
709
|
+
].join("\n");
|
|
710
|
+
}
|
|
711
|
+
/** Wildcard and loopback hosts are noise in a dev banner; show localhost. */
|
|
712
|
+
function displayOrigin(origin) {
|
|
713
|
+
try {
|
|
714
|
+
const url = new URL(origin);
|
|
715
|
+
if (url.hostname === "[::1]" || url.hostname === "::1" || url.hostname === "127.0.0.1" || url.hostname === "0.0.0.0" || url.hostname === "[::]") url.hostname = "localhost";
|
|
716
|
+
return url.toString().replace(/\/$/, "");
|
|
717
|
+
} catch {
|
|
718
|
+
return origin;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
function plural(count, one, many) {
|
|
722
|
+
return `${count} ${count === 1 ? one : many}`;
|
|
723
|
+
}
|
|
724
|
+
function createPainter(enabled) {
|
|
725
|
+
const wrap = (code) => (text) => enabled ? `\u001B[${code}m${text}\u001B[0m` : text;
|
|
726
|
+
return {
|
|
727
|
+
bold: wrap("1"),
|
|
728
|
+
dim: wrap("2"),
|
|
729
|
+
accent: wrap("36")
|
|
730
|
+
};
|
|
383
731
|
}
|
|
384
732
|
async function readJson(url) {
|
|
385
733
|
try {
|