@muhkoo/cli 0.7.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +122 -0
- package/bin/muhkoo.js +17 -0
- package/package.json +44 -0
- package/src/commands/_eject-runner.mjs +59 -0
- package/src/commands/agents.js +88 -0
- package/src/commands/apps.js +119 -0
- package/src/commands/deploy.js +101 -0
- package/src/commands/domains.js +75 -0
- package/src/commands/eject.js +40 -0
- package/src/commands/functions.js +108 -0
- package/src/commands/hosting.js +76 -0
- package/src/commands/login.js +66 -0
- package/src/commands/logout.js +17 -0
- package/src/commands/logs.js +45 -0
- package/src/commands/provision.js +232 -0
- package/src/commands/tables.js +60 -0
- package/src/commands/whoami.js +26 -0
- package/src/index.js +105 -0
- package/src/lib/args.js +32 -0
- package/src/lib/auth.js +62 -0
- package/src/lib/bases.js +28 -0
- package/src/lib/browser.js +20 -0
- package/src/lib/config.js +66 -0
- package/src/lib/http.js +59 -0
- package/src/lib/pkce.js +18 -0
- package/src/lib/spaces.js +41 -0
- package/src/lib/ui.js +102 -0
- package/src/lib/webLogin.js +140 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `muhkoo eject` — print the system prompt + tools config that a `@Muhkoo*`-
|
|
3
|
+
* decorated agent description compiles to. Useful for reviewing what an agent
|
|
4
|
+
* will actually be told and allowed to do before provisioning it.
|
|
5
|
+
*
|
|
6
|
+
* Runs the target through `tsx` (so decorators + TypeScript work) against the
|
|
7
|
+
* `@muhkoo/connect` installed in the target's own project.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { spawn } from "node:child_process";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { dirname, join } from "node:path";
|
|
13
|
+
import { die } from "../lib/ui.js";
|
|
14
|
+
|
|
15
|
+
export const help = `muhkoo eject — preview an agent's compiled prompt + tools
|
|
16
|
+
|
|
17
|
+
Usage:
|
|
18
|
+
muhkoo eject <path/to/agentApp.ts> [ExportName]
|
|
19
|
+
|
|
20
|
+
Requires the target project to have @muhkoo/connect installed and a tsconfig
|
|
21
|
+
with experimentalDecorators enabled. Uses tsx (via npx) to run.`;
|
|
22
|
+
|
|
23
|
+
export default async function eject(args) {
|
|
24
|
+
const target = args._[1];
|
|
25
|
+
if (!target) die("Usage: muhkoo eject <path/to/agentApp.ts> [ExportName]");
|
|
26
|
+
const exportName = args._[2];
|
|
27
|
+
|
|
28
|
+
const runner = join(dirname(fileURLToPath(import.meta.url)), "_eject-runner.mjs");
|
|
29
|
+
const argv = ["--yes", "tsx", runner, target];
|
|
30
|
+
if (exportName) argv.push(exportName);
|
|
31
|
+
|
|
32
|
+
await new Promise((resolve) => {
|
|
33
|
+
const child = spawn("npx", argv, { stdio: "inherit" });
|
|
34
|
+
child.on("error", (e) => die(`Could not run tsx via npx: ${e.message}`));
|
|
35
|
+
child.on("exit", (code) => {
|
|
36
|
+
if (code !== 0) process.exit(code ?? 1);
|
|
37
|
+
resolve();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `muhkoo functions` — list, inspect, remove, and enable/disable serverless
|
|
3
|
+
* functions, plus read deployed source. Deploys happen via `muhkoo provision`.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { readFile } from "node:fs/promises";
|
|
7
|
+
import { devContext } from "../lib/config.js";
|
|
8
|
+
import { devCall, ensure } from "../lib/http.js";
|
|
9
|
+
import { resolveSpace } from "../lib/spaces.js";
|
|
10
|
+
import { table, json, ok, info, die } from "../lib/ui.js";
|
|
11
|
+
|
|
12
|
+
export const help = `muhkoo functions — manage serverless functions
|
|
13
|
+
|
|
14
|
+
Usage:
|
|
15
|
+
muhkoo functions ls <appId> [--json]
|
|
16
|
+
muhkoo functions get <appId> <functionId> [--json]
|
|
17
|
+
muhkoo functions code <appId> <functionId> print the deployed source
|
|
18
|
+
muhkoo functions deploy <appId> --name <n> --file <path> [--http]
|
|
19
|
+
muhkoo functions rm <appId> <functionId>
|
|
20
|
+
muhkoo functions enable <appId> <functionId> --space <spaceId|#channel>
|
|
21
|
+
muhkoo functions disable <appId> <functionId> --space <spaceId|#channel>`;
|
|
22
|
+
|
|
23
|
+
export default async function functions(args) {
|
|
24
|
+
const sub = args._[1];
|
|
25
|
+
const ctx = await devContext(args);
|
|
26
|
+
const appId = args._[2] || args.app;
|
|
27
|
+
if (!appId) die("Missing app id. Usage: muhkoo functions <ls|get|code|deploy|rm|enable|disable> <appId>");
|
|
28
|
+
|
|
29
|
+
switch (sub) {
|
|
30
|
+
case "ls":
|
|
31
|
+
case undefined:
|
|
32
|
+
return list(ctx, appId, args);
|
|
33
|
+
case "get":
|
|
34
|
+
return get(ctx, appId, args);
|
|
35
|
+
case "code":
|
|
36
|
+
return code(ctx, appId, args);
|
|
37
|
+
case "deploy":
|
|
38
|
+
return deployOne(ctx, appId, args);
|
|
39
|
+
case "rm":
|
|
40
|
+
case "delete":
|
|
41
|
+
return remove(ctx, appId, args);
|
|
42
|
+
case "enable":
|
|
43
|
+
return toggle(ctx, appId, args, "enable");
|
|
44
|
+
case "disable":
|
|
45
|
+
return toggle(ctx, appId, args, "disable");
|
|
46
|
+
default:
|
|
47
|
+
die(`Unknown subcommand "functions ${sub}". See \`muhkoo functions --help\`.`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function list(ctx, appId, args) {
|
|
52
|
+
const r = ensure(await devCall(ctx, "GET", `/api/apps/${appId}/functions`), "List functions");
|
|
53
|
+
const items = r.body?.functions || [];
|
|
54
|
+
if (args.json) return json(items);
|
|
55
|
+
if (!items.length) return info("No functions. Define them in a provision spec and run `muhkoo provision`.");
|
|
56
|
+
table(
|
|
57
|
+
["NAME", "FUNCTION ID", "TRIGGERS"],
|
|
58
|
+
items.map((f) => [f.name, f.functionId, Object.keys(f.triggers || {}).join(",") || ""]),
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function get(ctx, appId, args) {
|
|
63
|
+
const id = args._[3];
|
|
64
|
+
if (!id) die("Usage: muhkoo functions get <appId> <functionId>");
|
|
65
|
+
const r = ensure(await devCall(ctx, "GET", `/api/apps/${appId}/functions/${id}`), "Get function");
|
|
66
|
+
json(r.body);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function code(ctx, appId, args) {
|
|
70
|
+
const id = args._[3];
|
|
71
|
+
if (!id) die("Usage: muhkoo functions code <appId> <functionId>");
|
|
72
|
+
const r = ensure(await devCall(ctx, "GET", `/api/apps/${appId}/functions/${id}/code`), "Read function code");
|
|
73
|
+
process.stdout.write((r.body?.code ?? "") + "\n");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function deployOne(ctx, appId, args) {
|
|
77
|
+
const name = args.name;
|
|
78
|
+
const file = args.file;
|
|
79
|
+
if (!name || !file) die("Usage: muhkoo functions deploy <appId> --name <n> --file <path> [--http]");
|
|
80
|
+
const source = await readFile(file, "utf8");
|
|
81
|
+
const input = { name, displayName: args.display || name, code: source };
|
|
82
|
+
if (args.http) input.triggers = { http: { enabled: true, methods: ["GET", "POST"] } };
|
|
83
|
+
const list = await devCall(ctx, "GET", `/api/apps/${appId}/functions`);
|
|
84
|
+
const match = (list.ok ? list.body?.functions || [] : []).find((f) => f.name === name);
|
|
85
|
+
const r = match
|
|
86
|
+
? await devCall(ctx, "PATCH", `/api/apps/${appId}/functions/${match.functionId}`, input)
|
|
87
|
+
: await devCall(ctx, "POST", `/api/apps/${appId}/functions`, input);
|
|
88
|
+
if (r.status === 402) die("Functions need a paid plan.");
|
|
89
|
+
ensure(r, "Deploy function");
|
|
90
|
+
ok(`${match ? "Redeployed" : "Deployed"} ${name} (${r.body.config.functionId}).`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function remove(ctx, appId, args) {
|
|
94
|
+
const id = args._[3];
|
|
95
|
+
if (!id) die("Usage: muhkoo functions rm <appId> <functionId>");
|
|
96
|
+
ensure(await devCall(ctx, "DELETE", `/api/apps/${appId}/functions/${id}`), "Delete function");
|
|
97
|
+
ok(`Deleted function ${id}.`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function toggle(ctx, appId, args, action) {
|
|
101
|
+
const id = args._[3];
|
|
102
|
+
if (!id) die(`Usage: muhkoo functions ${action} <appId> <functionId> --space <spaceId|#channel>`);
|
|
103
|
+
const targetSpaceId = await resolveSpace(ctx, args);
|
|
104
|
+
const r = await devCall(ctx, "POST", `/api/apps/${appId}/functions/${id}/${action}`, { targetSpaceId });
|
|
105
|
+
if (r.status === 402) die("Functions need a paid plan.");
|
|
106
|
+
ensure(r, `${action} function`);
|
|
107
|
+
ok(`${action === "enable" ? "Enabled" : "Disabled"} ${id} on ${targetSpaceId}.`);
|
|
108
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `muhkoo hosting` — inspect and manage an app's hosted releases.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { devContext } from "../lib/config.js";
|
|
6
|
+
import { devCall, ensure } from "../lib/http.js";
|
|
7
|
+
import { table, json, ok, info, die, c } from "../lib/ui.js";
|
|
8
|
+
|
|
9
|
+
export const help = `muhkoo hosting — manage hosted releases
|
|
10
|
+
|
|
11
|
+
Usage:
|
|
12
|
+
muhkoo hosting status <appId> [--json]
|
|
13
|
+
muhkoo hosting rollback <appId> --release <releaseId>
|
|
14
|
+
muhkoo hosting rm-release <appId> <releaseId>
|
|
15
|
+
muhkoo hosting unpublish <appId>`;
|
|
16
|
+
|
|
17
|
+
export default async function hosting(args) {
|
|
18
|
+
const sub = args._[1];
|
|
19
|
+
const ctx = await devContext(args);
|
|
20
|
+
const appId = args._[2] || args.app;
|
|
21
|
+
if (!appId) die("Missing app id. Usage: muhkoo hosting <status|rollback|...> <appId>");
|
|
22
|
+
|
|
23
|
+
switch (sub) {
|
|
24
|
+
case "status":
|
|
25
|
+
case undefined:
|
|
26
|
+
return status(ctx, appId, args);
|
|
27
|
+
case "rollback":
|
|
28
|
+
return rollback(ctx, appId, args);
|
|
29
|
+
case "rm-release":
|
|
30
|
+
return rmRelease(ctx, appId, args);
|
|
31
|
+
case "unpublish":
|
|
32
|
+
return unpublish(ctx, appId);
|
|
33
|
+
default:
|
|
34
|
+
die(`Unknown subcommand "hosting ${sub}". See \`muhkoo hosting --help\`.`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function status(ctx, appId, args) {
|
|
39
|
+
const r = ensure(await devCall(ctx, "GET", `/api/apps/${appId}/hosting`), "Hosting status");
|
|
40
|
+
if (args.json) return json(r.body);
|
|
41
|
+
const h = r.body;
|
|
42
|
+
info(`${c.bold("url")} ${h.url ?? "(unpublished)"}`);
|
|
43
|
+
info(`${c.bold("published")} ${h.published ? "yes" : "no"}`);
|
|
44
|
+
if (h.currentReleaseId) info(`${c.bold("current")} ${h.currentReleaseId}`);
|
|
45
|
+
if (Array.isArray(h.releases) && h.releases.length) {
|
|
46
|
+
info("");
|
|
47
|
+
table(
|
|
48
|
+
["RELEASE", "FILES", "KiB", "CREATED"],
|
|
49
|
+
h.releases.map((rel) => [
|
|
50
|
+
rel.releaseId + (rel.releaseId === h.currentReleaseId ? " *" : ""),
|
|
51
|
+
rel.files,
|
|
52
|
+
(rel.bytes / 1024).toFixed(0),
|
|
53
|
+
rel.createdAt ? new Date(rel.createdAt).toISOString() : "",
|
|
54
|
+
]),
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function rollback(ctx, appId, args) {
|
|
60
|
+
const releaseId = args.release || args._[3];
|
|
61
|
+
if (!releaseId) die("Usage: muhkoo hosting rollback <appId> --release <releaseId>");
|
|
62
|
+
ensure(await devCall(ctx, "POST", `/api/apps/${appId}/hosting/rollback`, { releaseId }), "Rollback");
|
|
63
|
+
ok(`Rolled back to ${releaseId}.`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function rmRelease(ctx, appId, args) {
|
|
67
|
+
const releaseId = args._[3];
|
|
68
|
+
if (!releaseId) die("Usage: muhkoo hosting rm-release <appId> <releaseId>");
|
|
69
|
+
ensure(await devCall(ctx, "DELETE", `/api/apps/${appId}/hosting/releases/${releaseId}`), "Delete release");
|
|
70
|
+
ok(`Deleted release ${releaseId}.`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function unpublish(ctx, appId) {
|
|
74
|
+
ensure(await devCall(ctx, "DELETE", `/api/apps/${appId}/hosting`), "Unpublish");
|
|
75
|
+
ok(`Unpublished ${appId} — the live pointer was removed.`);
|
|
76
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `muhkoo login` — authenticate as a developer and store the session token in
|
|
3
|
+
* `~/.muhkoo/config.json`. Uses the zero-knowledge login (username + password
|
|
4
|
+
* are never sent to the server). You can also paste a token from the portal.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { resolveBase, loadConfig, saveConfig } from "../lib/config.js";
|
|
8
|
+
import { zkLogin } from "../lib/auth.js";
|
|
9
|
+
import { webLogin, DEFAULT_CLI_APP_ID } from "../lib/webLogin.js";
|
|
10
|
+
import { devCall } from "../lib/http.js";
|
|
11
|
+
import { firstOf } from "../lib/args.js";
|
|
12
|
+
import { prompt, promptHidden, ok, step, info, die, c } from "../lib/ui.js";
|
|
13
|
+
|
|
14
|
+
export const help = `muhkoo login — sign in as a developer
|
|
15
|
+
|
|
16
|
+
Usage:
|
|
17
|
+
muhkoo login --web [--base <env|url>] sign in via the browser
|
|
18
|
+
muhkoo login [--username <u>] [--password <p>] [--base <env|url>]
|
|
19
|
+
muhkoo login --token <sessionToken> [--base <env|url>]
|
|
20
|
+
|
|
21
|
+
--web opens auth.muhkoo.dev in your browser (password, passkey, or Google) and
|
|
22
|
+
captures the session over a localhost redirect — no credentials touch the CLI.
|
|
23
|
+
Otherwise credentials are read from flags, then $MUHKOO_USERNAME/$MUHKOO_PASSWORD,
|
|
24
|
+
then a prompt. The session token is saved to ~/.muhkoo/config.json.`;
|
|
25
|
+
|
|
26
|
+
export default async function login(args) {
|
|
27
|
+
const cfg = await loadConfig();
|
|
28
|
+
const baseUrl = await resolveBase(args, cfg);
|
|
29
|
+
|
|
30
|
+
let token = args.token || process.env.MUHKOO_DEV_TOKEN;
|
|
31
|
+
let username = args.username || process.env.MUHKOO_USERNAME;
|
|
32
|
+
let commitment;
|
|
33
|
+
|
|
34
|
+
if (!token && (args.web || args.browser)) {
|
|
35
|
+
const appId = firstOf(args["app-id"], process.env.MUHKOO_CLI_APP_ID, cfg.cliAppId, DEFAULT_CLI_APP_ID);
|
|
36
|
+
const res = await webLogin({ baseUrl, appId });
|
|
37
|
+
token = res.token;
|
|
38
|
+
commitment = res.commitment;
|
|
39
|
+
username = res.username;
|
|
40
|
+
} else if (!token) {
|
|
41
|
+
if (!username) username = await prompt("Username: ");
|
|
42
|
+
let password = args.password || process.env.MUHKOO_PASSWORD;
|
|
43
|
+
if (!password) password = await promptHidden("Password: ");
|
|
44
|
+
if (!username || !password) die("A username and password are required.");
|
|
45
|
+
const res = await zkLogin({ baseUrl, username, password });
|
|
46
|
+
token = res.token;
|
|
47
|
+
commitment = res.commitment;
|
|
48
|
+
username = res.username;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Confirm the token works and surface the developer's status.
|
|
52
|
+
step("Verifying session…");
|
|
53
|
+
const me = await devCall({ baseUrl, token }, "GET", "/api/developer/me");
|
|
54
|
+
if (!me.ok && me.status === 401) die("That session token is invalid or expired.");
|
|
55
|
+
|
|
56
|
+
await saveConfig({ ...cfg, base: baseUrl, token, username, commitment });
|
|
57
|
+
ok(`Signed in${username ? ` as ${c.bold(username)}` : ""} → ${baseUrl}`);
|
|
58
|
+
if (me.ok && me.body?.needsBootstrap) {
|
|
59
|
+
info(
|
|
60
|
+
`\n${c.yellow("Note:")} your developer account isn't set up yet. Run ` +
|
|
61
|
+
`\`muhkoo apps create --slug <name> --email you@example.com\` to bootstrap it.`,
|
|
62
|
+
);
|
|
63
|
+
} else if (me.ok && me.body?.tier) {
|
|
64
|
+
info(` plan: ${me.body.tier}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `muhkoo logout` — clear the stored session token (keeps the default base).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { loadConfig, saveConfig } from "../lib/config.js";
|
|
6
|
+
import { ok } from "../lib/ui.js";
|
|
7
|
+
|
|
8
|
+
export const help = `muhkoo logout — clear the stored session token`;
|
|
9
|
+
|
|
10
|
+
export default async function logout() {
|
|
11
|
+
const cfg = await loadConfig();
|
|
12
|
+
delete cfg.token;
|
|
13
|
+
delete cfg.username;
|
|
14
|
+
delete cfg.commitment;
|
|
15
|
+
await saveConfig(cfg);
|
|
16
|
+
ok("Signed out.");
|
|
17
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `muhkoo logs` — inspect an app's server-log Space.
|
|
3
|
+
*
|
|
4
|
+
* Logs are end-to-end encrypted to the app owner's identity. This command shows
|
|
5
|
+
* the log Space and recent (still-encrypted) history so you can confirm logging
|
|
6
|
+
* is wired up and flowing. Decrypted, live tailing currently lives in the portal
|
|
7
|
+
* Tools panel; CLI-side decryption is a planned follow-up.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { devContext } from "../lib/config.js";
|
|
11
|
+
import { devCall, ensure } from "../lib/http.js";
|
|
12
|
+
import { json, info, warn, die, c } from "../lib/ui.js";
|
|
13
|
+
|
|
14
|
+
export const help = `muhkoo logs — inspect an app's server-log Space
|
|
15
|
+
|
|
16
|
+
Usage:
|
|
17
|
+
muhkoo logs <appId> [--env live|test] [--limit 50] [--json]
|
|
18
|
+
|
|
19
|
+
Note: log entries are E2E-encrypted to the owner. This shows the log Space and
|
|
20
|
+
recent ciphertext; decrypted viewing is in the portal Tools panel for now.`;
|
|
21
|
+
|
|
22
|
+
export default async function logs(args) {
|
|
23
|
+
const ctx = await devContext(args);
|
|
24
|
+
const appId = args._[1] || args.app;
|
|
25
|
+
if (!appId) die("Usage: muhkoo logs <appId>");
|
|
26
|
+
const env = args.env || "live";
|
|
27
|
+
const limit = args.limit || 50;
|
|
28
|
+
|
|
29
|
+
const space = ensure(
|
|
30
|
+
await devCall(ctx, "GET", `/api/apps/${appId}/logs/space?env=${env}`),
|
|
31
|
+
"Log space",
|
|
32
|
+
);
|
|
33
|
+
const hist = ensure(
|
|
34
|
+
await devCall(ctx, "GET", `/api/apps/${appId}/logs/history?env=${env}&limit=${limit}`),
|
|
35
|
+
"Log history",
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
if (args.json) return json({ space: space.body, history: hist.body });
|
|
39
|
+
|
|
40
|
+
const entries = hist.body?.messages || hist.body?.entries || [];
|
|
41
|
+
info(`${c.bold("log space")} ${space.body?.spaceId ?? "(not provisioned yet)"}`);
|
|
42
|
+
info(`${c.bold("env")} ${env}`);
|
|
43
|
+
info(`${c.bold("entries")} ${entries.length} (encrypted)`);
|
|
44
|
+
warn("Entries are E2E-encrypted. View decrypted logs in the portal → Tools → Logs.");
|
|
45
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `muhkoo provision` — create or update an app's backend from a design spec
|
|
3
|
+
* (tables, agents, functions), then optionally enable agents/functions on their
|
|
4
|
+
* channels. Idempotent: re-running updates rather than duplicating. Writes the
|
|
5
|
+
* app id + issued keys to an output file so re-runs and deploys can read them.
|
|
6
|
+
*
|
|
7
|
+
* Spec (JSON):
|
|
8
|
+
* {
|
|
9
|
+
* "slug": "team-standup",
|
|
10
|
+
* "allowedOrigins": "*",
|
|
11
|
+
* "email": "you@example.com", // only to bootstrap a new developer
|
|
12
|
+
* "tables": [ <DbTableSpec>, ... ],
|
|
13
|
+
* "agents": [ { ...AgentCreateInput, "enableChannel": "general" }, ... ],
|
|
14
|
+
* "functions": [ { ...FunctionDeployInput, "enableChannel": "general"? }, ... ]
|
|
15
|
+
* }
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { readFile, writeFile, access } from "node:fs/promises";
|
|
19
|
+
import { devContext } from "../lib/config.js";
|
|
20
|
+
import { devCall, appKeyGet } from "../lib/http.js";
|
|
21
|
+
import { appsSuffixFor } from "../lib/bases.js";
|
|
22
|
+
import { step, ok, warn, info, die, c } from "../lib/ui.js";
|
|
23
|
+
|
|
24
|
+
export const help = `muhkoo provision — provision an app backend from a spec
|
|
25
|
+
|
|
26
|
+
Usage:
|
|
27
|
+
muhkoo provision --spec app.json [--out .muhkoo-app.json] [--dry-run]
|
|
28
|
+
muhkoo provision --spec app.json --enable enable agents/functions on channels
|
|
29
|
+
|
|
30
|
+
Auth: a developer session (run \`muhkoo login\`, or pass --token / set MUHKOO_DEV_TOKEN).`;
|
|
31
|
+
|
|
32
|
+
async function readJson(path) {
|
|
33
|
+
return JSON.parse(await readFile(path, "utf8"));
|
|
34
|
+
}
|
|
35
|
+
async function writeJson(path, obj) {
|
|
36
|
+
await writeFile(path, JSON.stringify(obj, null, 2) + "\n");
|
|
37
|
+
}
|
|
38
|
+
async function fileExists(path) {
|
|
39
|
+
try {
|
|
40
|
+
await access(path);
|
|
41
|
+
return true;
|
|
42
|
+
} catch {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default async function provision(args) {
|
|
48
|
+
if (!args.spec) die("Missing --spec <file.json>.");
|
|
49
|
+
const DRY = !!args["dry-run"];
|
|
50
|
+
const OUT = args.out || ".muhkoo-app.json";
|
|
51
|
+
const spec = await readJson(args.spec);
|
|
52
|
+
const prior = (await fileExists(OUT)) ? await readJson(OUT) : null;
|
|
53
|
+
const ctx = await devContext(args);
|
|
54
|
+
info(`API base: ${ctx.baseUrl}${DRY ? " (dry run)" : ""}\n`);
|
|
55
|
+
|
|
56
|
+
if (args.enable) {
|
|
57
|
+
if (!prior?.appId) die(`--enable needs a prior ${OUT} from a provisioning run.`);
|
|
58
|
+
await enableOnChannels(ctx, prior);
|
|
59
|
+
ok("Done.");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
await ensureDeveloper(ctx, spec.email, DRY);
|
|
64
|
+
const app = await createOrReuseApp(ctx, spec, prior, DRY);
|
|
65
|
+
const tables = await putTables(ctx, app.appId, spec.tables, DRY);
|
|
66
|
+
const agents = await createAgents(ctx, app.appId, spec.agents, DRY);
|
|
67
|
+
const functions = await deployFunctions(ctx, app.appId, spec.functions, DRY);
|
|
68
|
+
|
|
69
|
+
const hostingUrl = `https://${app.slug}.${appsSuffixFor(ctx.baseUrl)}`;
|
|
70
|
+
const record = {
|
|
71
|
+
appId: app.appId,
|
|
72
|
+
slug: app.slug,
|
|
73
|
+
baseUrl: ctx.baseUrl,
|
|
74
|
+
hostingUrl,
|
|
75
|
+
keys: app.keys?.length ? app.keys : prior?.keys || [],
|
|
76
|
+
tables,
|
|
77
|
+
agents,
|
|
78
|
+
functions,
|
|
79
|
+
};
|
|
80
|
+
if (!DRY) await writeJson(OUT, record);
|
|
81
|
+
|
|
82
|
+
const pk = record.keys.find((k) => k.env === "test" && k.type === "pk")?.plaintext;
|
|
83
|
+
info("");
|
|
84
|
+
ok("Provisioned.");
|
|
85
|
+
info(` appId: ${record.appId}`);
|
|
86
|
+
info(` slug: ${record.slug}`);
|
|
87
|
+
if (pk) info(` test pk: ${pk}`);
|
|
88
|
+
info(` tables: ${tables.join(", ") || "(none)"}`);
|
|
89
|
+
info(` hosting: ${hostingUrl} (run \`muhkoo deploy\` once your client is built)`);
|
|
90
|
+
if ((agents || []).some((a) => a.enableChannel) || (functions || []).some((f) => f.enableChannel)) {
|
|
91
|
+
info("\nNext: run the app once so its channels exist, then:");
|
|
92
|
+
info(` muhkoo provision --spec ${args.spec} --enable`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function ensureDeveloper(ctx, email, DRY) {
|
|
97
|
+
const me = await devCall(ctx, "GET", "/api/developer/me");
|
|
98
|
+
if (me.ok && !me.body?.needsBootstrap) return;
|
|
99
|
+
if (!email) {
|
|
100
|
+
die('Developer account not set up. Add an "email" to the spec to bootstrap it.');
|
|
101
|
+
}
|
|
102
|
+
step(`Bootstrapping developer account (${email})…`);
|
|
103
|
+
if (DRY) return;
|
|
104
|
+
const r = await devCall(ctx, "POST", "/api/developer/bootstrap", { email });
|
|
105
|
+
if (!r.ok) die(`Developer bootstrap failed (${r.status}): ${JSON.stringify(r.body)}`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function createOrReuseApp(ctx, spec, prior, DRY) {
|
|
109
|
+
if (prior?.appId) {
|
|
110
|
+
step(`Reusing existing app ${prior.slug} (${prior.appId}).`);
|
|
111
|
+
return prior;
|
|
112
|
+
}
|
|
113
|
+
step(`Creating app "${spec.slug}"…`);
|
|
114
|
+
if (DRY) return { appId: "<dry-run>", slug: spec.slug, keys: [] };
|
|
115
|
+
const body = { slug: spec.slug, allowedOrigins: spec.allowedOrigins || "*" };
|
|
116
|
+
if (spec.email) body.email = spec.email;
|
|
117
|
+
let r = await devCall(ctx, "POST", "/api/apps", body);
|
|
118
|
+
if (!r.ok && r.status === 402 && spec.email) {
|
|
119
|
+
await ensureDeveloper(ctx, spec.email, DRY);
|
|
120
|
+
r = await devCall(ctx, "POST", "/api/apps", body);
|
|
121
|
+
}
|
|
122
|
+
if (!r.ok) die(`Create app failed (${r.status}): ${JSON.stringify(r.body)}`);
|
|
123
|
+
const keys = r.body.keys || [];
|
|
124
|
+
const pk = keys.find((k) => k.env === "test" && k.type === "pk")?.plaintext;
|
|
125
|
+
info(` ${c.green("✓")} appId ${r.body.appId}` + (pk ? ` test pk ${pk.slice(0, 18)}…` : ""));
|
|
126
|
+
return { appId: r.body.appId, slug: r.body.slug, keys };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function putTables(ctx, appId, tables, DRY) {
|
|
130
|
+
const done = [];
|
|
131
|
+
for (const spec of tables || []) {
|
|
132
|
+
step(`Table "${spec.table}" (${(spec.columns || []).length} cols)…`);
|
|
133
|
+
if (DRY) {
|
|
134
|
+
done.push(spec.table);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const r = await devCall(ctx, "PUT", `/api/apps/${appId}/db/tables/${encodeURIComponent(spec.table)}`, spec);
|
|
138
|
+
if (!r.ok) die(`Table "${spec.table}" failed (${r.status}): ${JSON.stringify(r.body)}`);
|
|
139
|
+
info(` ${c.green("✓")} ${spec.table}${r.body?.version ? ` v${r.body.version}` : ""}`);
|
|
140
|
+
done.push(spec.table);
|
|
141
|
+
}
|
|
142
|
+
return done;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function createAgents(ctx, appId, agents, DRY) {
|
|
146
|
+
const created = [];
|
|
147
|
+
if (!(agents || []).length) return created;
|
|
148
|
+
const list = await devCall(ctx, "GET", `/api/apps/${appId}/agents`);
|
|
149
|
+
const existing = list.ok ? list.body?.agents || [] : [];
|
|
150
|
+
for (const a of agents) {
|
|
151
|
+
const { enableChannel, ...input } = a;
|
|
152
|
+
step(`Agent ${input.handle}…`);
|
|
153
|
+
if (DRY) {
|
|
154
|
+
created.push({ handle: input.handle, enableChannel });
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const match = existing.find((e) => e.handle === input.handle);
|
|
158
|
+
const r = match
|
|
159
|
+
? await devCall(ctx, "PATCH", `/api/apps/${appId}/agents/${match.agentId}`, input)
|
|
160
|
+
: await devCall(ctx, "POST", `/api/apps/${appId}/agents`, input);
|
|
161
|
+
if (r.status === 402) {
|
|
162
|
+
warn(`Agents need a paid plan — skipped ${input.handle}. (Config saved for later.)`);
|
|
163
|
+
created.push({ handle: input.handle, enableChannel, skipped: "paid-tier", input });
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (!r.ok) die(`Agent ${input.handle} failed (${r.status}): ${JSON.stringify(r.body)}`);
|
|
167
|
+
const agentId = r.body.config.agentId;
|
|
168
|
+
info(` ${c.green("✓")} ${input.handle} (${agentId})${match ? " [updated]" : ""}`);
|
|
169
|
+
created.push({ handle: input.handle, agentId, enableChannel });
|
|
170
|
+
}
|
|
171
|
+
return created;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async function deployFunctions(ctx, appId, fns, DRY) {
|
|
175
|
+
const deployed = [];
|
|
176
|
+
if (!(fns || []).length) return deployed;
|
|
177
|
+
const list = await devCall(ctx, "GET", `/api/apps/${appId}/functions`);
|
|
178
|
+
const existing = list.ok ? list.body?.functions || [] : [];
|
|
179
|
+
for (const f of fns) {
|
|
180
|
+
const { enableChannel, ...input } = f;
|
|
181
|
+
step(`Function ${input.name}…`);
|
|
182
|
+
if (DRY) {
|
|
183
|
+
deployed.push({ name: input.name, enableChannel });
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const match = existing.find((e) => e.name === input.name);
|
|
187
|
+
const r = match
|
|
188
|
+
? await devCall(ctx, "PATCH", `/api/apps/${appId}/functions/${match.functionId}`, input)
|
|
189
|
+
: await devCall(ctx, "POST", `/api/apps/${appId}/functions`, input);
|
|
190
|
+
if (r.status === 402) {
|
|
191
|
+
warn(`Functions need a paid plan — skipped ${input.name}. (Config saved for later.)`);
|
|
192
|
+
deployed.push({ name: input.name, enableChannel, skipped: "paid-tier", input });
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (!r.ok) die(`Function ${input.name} failed (${r.status}): ${JSON.stringify(r.body)}`);
|
|
196
|
+
const functionId = r.body.config.functionId;
|
|
197
|
+
info(` ${c.green("✓")} ${input.name} (${functionId})${match ? " [redeployed]" : ""}`);
|
|
198
|
+
deployed.push({ name: input.name, functionId, enableChannel });
|
|
199
|
+
}
|
|
200
|
+
return deployed;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Resolve channel names → spaceIds (app-key route) and enable agents/functions. */
|
|
204
|
+
async function enableOnChannels(ctx, record) {
|
|
205
|
+
step("Enabling agents/functions on their channels…");
|
|
206
|
+
const appKey = record.keys?.find((k) => k.env === "test" && k.type === "pk")?.plaintext;
|
|
207
|
+
if (!appKey) die("No test publishable key on record — cannot resolve channels.");
|
|
208
|
+
const resolve = async (name) => {
|
|
209
|
+
const r = await appKeyGet(ctx.baseUrl, appKey, `/api/app/channels/${encodeURIComponent(name)}`);
|
|
210
|
+
return r?.spaceId || null;
|
|
211
|
+
};
|
|
212
|
+
for (const a of record.agents || []) {
|
|
213
|
+
if (!a.agentId || !a.enableChannel) continue;
|
|
214
|
+
const spaceId = await resolve(a.enableChannel);
|
|
215
|
+
if (!spaceId) {
|
|
216
|
+
warn(`channel "${a.enableChannel}" not found yet — run the app once, then re-run with --enable.`);
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const r = await devCall(ctx, "POST", `/api/apps/${record.appId}/agents/${a.agentId}/enable`, { targetSpaceId: spaceId });
|
|
220
|
+
info(r.ok ? ` ${c.green("✓")} enabled ${a.handle} on #${a.enableChannel}` : ` ${c.red("✗")} enable ${a.handle}: ${r.status}`);
|
|
221
|
+
}
|
|
222
|
+
for (const f of record.functions || []) {
|
|
223
|
+
if (!f.functionId || !f.enableChannel) continue;
|
|
224
|
+
const spaceId = await resolve(f.enableChannel);
|
|
225
|
+
if (!spaceId) {
|
|
226
|
+
warn(`channel "${f.enableChannel}" not found yet — run the app once, then re-run with --enable.`);
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const r = await devCall(ctx, "POST", `/api/apps/${record.appId}/functions/${f.functionId}/enable`, { targetSpaceId: spaceId });
|
|
230
|
+
info(r.ok ? ` ${c.green("✓")} enabled fn ${f.name} on #${f.enableChannel}` : ` ${c.red("✗")} enable ${f.name}: ${r.status}`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `muhkoo tables` — inspect and remove an app's database tables. Tables are
|
|
3
|
+
* usually created via `muhkoo provision`; this is for listing and teardown.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { devContext } from "../lib/config.js";
|
|
7
|
+
import { devCall, ensure } from "../lib/http.js";
|
|
8
|
+
import { table, json, ok, info, die } from "../lib/ui.js";
|
|
9
|
+
|
|
10
|
+
export const help = `muhkoo tables — inspect app database tables
|
|
11
|
+
|
|
12
|
+
Usage:
|
|
13
|
+
muhkoo tables ls <appId> [--json]
|
|
14
|
+
muhkoo tables get <appId> <table> [--json]
|
|
15
|
+
muhkoo tables rm <appId> <table>`;
|
|
16
|
+
|
|
17
|
+
export default async function tables(args) {
|
|
18
|
+
const sub = args._[1];
|
|
19
|
+
const ctx = await devContext(args);
|
|
20
|
+
const appId = args._[2] || args.app;
|
|
21
|
+
if (!appId) die("Missing app id. Usage: muhkoo tables <ls|get|rm> <appId> [table]");
|
|
22
|
+
|
|
23
|
+
switch (sub) {
|
|
24
|
+
case "ls":
|
|
25
|
+
case undefined:
|
|
26
|
+
return list(ctx, appId, args);
|
|
27
|
+
case "get":
|
|
28
|
+
return get(ctx, appId, args);
|
|
29
|
+
case "rm":
|
|
30
|
+
case "delete":
|
|
31
|
+
return remove(ctx, appId, args);
|
|
32
|
+
default:
|
|
33
|
+
die(`Unknown subcommand "tables ${sub}". See \`muhkoo tables --help\`.`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function list(ctx, appId, args) {
|
|
38
|
+
const r = ensure(await devCall(ctx, "GET", `/api/apps/${appId}/db/tables`), "List tables");
|
|
39
|
+
const items = r.body?.tables || r.body || [];
|
|
40
|
+
if (args.json) return json(items);
|
|
41
|
+
if (!items.length) return info("No tables. Define them in a provision spec and run `muhkoo provision`.");
|
|
42
|
+
table(
|
|
43
|
+
["TABLE", "COLUMNS", "VERSION"],
|
|
44
|
+
items.map((t) => [t.table ?? t.name, (t.columns || []).length, t.version ?? ""]),
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function get(ctx, appId, args) {
|
|
49
|
+
const name = args._[3];
|
|
50
|
+
if (!name) die("Usage: muhkoo tables get <appId> <table>");
|
|
51
|
+
const r = ensure(await devCall(ctx, "GET", `/api/apps/${appId}/db/tables/${encodeURIComponent(name)}`), "Get table");
|
|
52
|
+
json(r.body);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function remove(ctx, appId, args) {
|
|
56
|
+
const name = args._[3];
|
|
57
|
+
if (!name) die("Usage: muhkoo tables rm <appId> <table>");
|
|
58
|
+
ensure(await devCall(ctx, "DELETE", `/api/apps/${appId}/db/tables/${encodeURIComponent(name)}`), "Delete table");
|
|
59
|
+
ok(`Deleted table ${name}.`);
|
|
60
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `muhkoo whoami` — show the signed-in developer (plan, email, base).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { devContext } from "../lib/config.js";
|
|
6
|
+
import { devCall, ensure } from "../lib/http.js";
|
|
7
|
+
import { json, info, c } from "../lib/ui.js";
|
|
8
|
+
|
|
9
|
+
export const help = `muhkoo whoami — show the signed-in developer
|
|
10
|
+
|
|
11
|
+
Usage:
|
|
12
|
+
muhkoo whoami [--json] [--base <env|url>]`;
|
|
13
|
+
|
|
14
|
+
export default async function whoami(args) {
|
|
15
|
+
const ctx = await devContext(args);
|
|
16
|
+
const me = ensure(await devCall(ctx, "GET", "/api/developer/me"), "whoami");
|
|
17
|
+
if (args.json) return json({ ...me.body, base: ctx.baseUrl });
|
|
18
|
+
if (me.body?.needsBootstrap) {
|
|
19
|
+
info(`${c.yellow("Developer account not set up yet")} on ${ctx.baseUrl}.`);
|
|
20
|
+
info("Create your first app to bootstrap it: `muhkoo apps create --slug <name> --email <you@x.com>`");
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
info(`${c.bold("base")} ${ctx.baseUrl}`);
|
|
24
|
+
if (me.body?.email) info(`${c.bold("email")} ${me.body.email}`);
|
|
25
|
+
if (me.body?.tier) info(`${c.bold("plan")} ${me.body.tier}`);
|
|
26
|
+
}
|