@cavelon/cli 0.1.2 → 0.1.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/README.md +27 -13
- package/dist/agent-env.js +24 -0
- package/dist/agents.js +15 -7
- package/dist/choose.js +112 -0
- package/dist/commands/api.js +133 -27
- package/dist/commands/index.js +2 -0
- package/dist/commands/init.js +162 -24
- package/dist/commands/limits-set.js +2 -2
- package/dist/commands/limits.js +1 -1
- package/dist/commands/loops.js +1 -1
- package/dist/commands/sandboxes.js +2 -2
- package/dist/commands/session.js +176 -46
- package/dist/commands/setup.js +426 -0
- package/dist/commands/solution.js +48 -42
- package/dist/commands/tenants.js +33 -22
- package/dist/commands/work.js +2 -2
- package/dist/embedded.js +7 -0
- package/dist/git.js +14 -9
- package/dist/harness-ref.js +54 -0
- package/dist/http.js +15 -7
- package/dist/install.js +94 -0
- package/dist/main.js +31 -2
- package/dist/markers.js +53 -0
- package/dist/mcp.js +6 -3
- package/dist/openapi.js +44 -0
- package/dist/paths.js +7 -6
- package/dist/principal.js +43 -0
- package/dist/prompt.js +55 -11
- package/dist/results.js +65 -5
- package/dist/run-program.js +92 -0
- package/dist/session.js +72 -15
- package/dist/setup-agents.js +544 -0
- package/dist/skills/cavelon-authoring/SKILL.md +4 -1
- package/dist/skills/cavelon-loop/SKILL.md +29 -6
- package/dist/skills/cavelon-testing/SKILL.md +15 -1
- package/dist/tenant-choice.js +93 -0
- package/dist/update-check.js +118 -0
- package/dist/version.js +2 -1
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -6,13 +6,20 @@ learns each instance's API, features and docs from what the instance publishes
|
|
|
6
6
|
(its OpenAPI, `/api/v1/meta/capabilities`, `/api/v1/meta/error-catalog` and
|
|
7
7
|
`/llms.txt`), so a new core release needs no new `cavelon` release.
|
|
8
8
|
|
|
9
|
-
Node.js 20.3 or newer.
|
|
9
|
+
Node.js 20.3 or newer. Run it through `npx`, which needs no install; to type
|
|
10
|
+
`cavelon`, install it globally (or define an alias for the `npx` command):
|
|
10
11
|
|
|
11
12
|
```bash
|
|
13
|
+
npx -y @cavelon/cli --help
|
|
12
14
|
npm i -g @cavelon/cli
|
|
13
|
-
cavelon --help
|
|
14
15
|
```
|
|
15
16
|
|
|
17
|
+
Without Node.js, one line installs a standalone `cavelon` into your user
|
|
18
|
+
folder: `curl -fsSL https://github.com/goodguys-gmbh/cavelon-dev-kit/releases/latest/download/install.sh | sh`
|
|
19
|
+
on macOS and Linux, `irm https://github.com/goodguys-gmbh/cavelon-dev-kit/releases/latest/download/install.ps1 | iex`
|
|
20
|
+
in Windows PowerShell. Then `cavelon setup` sets up your coding agents and logs
|
|
21
|
+
you in.
|
|
22
|
+
|
|
16
23
|
The full documentation is in the repository's
|
|
17
24
|
[`docs/`](https://github.com/goodguys-gmbh/cavelon-dev-kit/tree/main/docs):
|
|
18
25
|
[installation](https://github.com/goodguys-gmbh/cavelon-dev-kit/blob/main/docs/installation.md)
|
|
@@ -45,9 +52,13 @@ This page is a compact reference.
|
|
|
45
52
|
standard input instead. **`cavelon` never takes a token as an argument**, so it
|
|
46
53
|
stays out of shell history. Run it in a terminal of your own: an agent's
|
|
47
54
|
shell (`!` in Claude Code) has no terminal to type the token into.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
token
|
|
55
|
+
`login` finds the tenants the token reaches: it uses the only one, or asks
|
|
56
|
+
you to choose by number or part of a name. Without a terminal it stores the
|
|
57
|
+
token and prints one `cavelon use <tenant>` line per tenant (exit 2).
|
|
58
|
+
3. `cavelon whoami` shows who the token acts as, in which tenant (name, slug
|
|
59
|
+
and id), its name and when it expires (it warns from seven days before), and
|
|
60
|
+
whether it may activate solutions. `cavelon use` chooses another tenant from
|
|
61
|
+
the same list.
|
|
51
62
|
|
|
52
63
|
The token goes into the operating system's credential store (Keychain, Windows
|
|
53
64
|
Credential Manager, Secret Service). Where there is none, it goes into
|
|
@@ -67,14 +78,14 @@ Environment variables replace the login:
|
|
|
67
78
|
|---|---|
|
|
68
79
|
| `CAVELON_URL` | the instance URL |
|
|
69
80
|
| `CAVELON_TOKEN` | a personal access token (`cvpat_…`) or a tenant API key (`cbp_…`); needs `CAVELON_URL` |
|
|
70
|
-
| `CAVELON_TENANT` | the tenant slug or id, for a personal token that reaches several tenants |
|
|
81
|
+
| `CAVELON_TENANT` | the tenant's name, slug or id, for a personal token that reaches several tenants |
|
|
71
82
|
|
|
72
83
|
### Which instance, token and tenant
|
|
73
84
|
|
|
74
85
|
Highest first:
|
|
75
86
|
|
|
76
|
-
1. command-line options: `--instance <url>`, `--tenant <
|
|
77
|
-
token option);
|
|
87
|
+
1. command-line options: `--instance <url>`, `--tenant <name, slug or id>`
|
|
88
|
+
(there is no token option);
|
|
78
89
|
2. `CAVELON_URL`, `CAVELON_TOKEN`, `CAVELON_TENANT`;
|
|
79
90
|
3. the nearest `cavelon.yaml` from the current directory upwards (instance URL
|
|
80
91
|
and tenant, never a token);
|
|
@@ -97,22 +108,23 @@ Every command takes `--json` (one JSON document on stdout, errors included),
|
|
|
97
108
|
|
|
98
109
|
| Command | Marked | What it does |
|
|
99
110
|
|---|---|---|
|
|
111
|
+
| `setup [--agents] [--yes] [--check] [--remove]` | changing (destructive) | Set up the coding agents on this computer (the plugin for Claude Code and Codex; the MCP server and skills in the user settings of Cursor, VS Code with GitHub Copilot, Gemini CLI and Kiro) and log in. `--check` reports what works, `--remove` undoes it. A person runs this; not an MCP tool. |
|
|
100
112
|
| `login [--token-stdin]` | changing | Store a token for an instance. A person runs this. |
|
|
101
113
|
| `logout [--all]` | changing | Delete the stored token. |
|
|
102
|
-
| `whoami` | read-only | Owner or key, tenant, role, and where the credential came from. |
|
|
103
|
-
| `use <tenant
|
|
114
|
+
| `whoami` | read-only | Owner or key, tenant (name, slug and id), role, and where the credential came from. |
|
|
115
|
+
| `use [<tenant>]` / `use --clear` | changing | Choose the tenant for this instance, by name, slug or id, or from a list. |
|
|
104
116
|
| `status [--offline]` | read-only | Instance, credential, tenant, solution folder, running operations, quotas close to full. |
|
|
105
117
|
| `limits [--key <key>] [--source <source>]` | read-only | The instance's limits for this tenant by source, who changes each and how; the tenant's quotas with their use (the monthly Processing Step cap among them); branch concurrency. |
|
|
106
118
|
| `limits set <key> <value> [--confirm]` | changing (destructive) | Change a limit through the operation the instance names: a tenant admin's, a quota of the Tenant Owner's (inference budget, Processing Step cap), or an operator's run cap with a Platform-mode token; without `--confirm`, shows the old and new value. |
|
|
107
119
|
| `models list` | read-only | The tenant's Model Registry rows with their endpoint and `max_concurrent_requests`; never a key. |
|
|
108
120
|
| `models set-limit <model> <n\|none> [--confirm]` | changing (destructive) | Set or clear a row's `max_concurrent_requests`; without `--confirm`, shows the old and new value. |
|
|
109
121
|
| `tenant create <slug> [--name] [--use]` | changing | Create a tenant (personal token in Platform mode with `tenants.manage`). |
|
|
110
|
-
| `tenant list [--search]` | read-only | Tenants the token can see. |
|
|
111
|
-
| `harness list [--readiness]` | read-only | The tenant's solutions. |
|
|
122
|
+
| `tenant list [--search]` | read-only | Tenants the token can see, with name, slug, role and id. |
|
|
123
|
+
| `harness list [--readiness]` | read-only | The tenant's solutions, with slug, name, status and id. |
|
|
112
124
|
| `harness new <slug> [--name] [--description]` | changing | Create an empty draft solution. |
|
|
113
125
|
| `harness clone <source> [--slug] [--name] [--no-tests] [--no-triggers]` | changing | Copy a solution into a new draft. |
|
|
114
126
|
| `activate [--harness] [--env]` | changing | Activate through the readiness gate only, never by force. |
|
|
115
|
-
| `init [--harness] [--agents <list>] [--hook] [--update] [--from <file> [--force]]` | changing (files) | Make this folder a solution: `cavelon.yaml`, `package/`, `tests/`, `env/`, `.cavelon/`; `--from` writes a package file into it. |
|
|
127
|
+
| `init [--harness] [--agents <list>] [--hook] [--update] [--from <file> [--force]]` | changing (files) | Make this folder a solution: `cavelon.yaml`, `package/`, `tests/`, `env/`, `.cavelon/`; on a terminal it asks for the tenant and the solution (or a new one by name); `--from` writes a package file into it. |
|
|
116
128
|
| `pull [--harness] [--force]` | changing (files) | Write the instance's package into `package/` and `tests/`, the inventory into `.cavelon/`. |
|
|
117
129
|
| `validate [--offline]` | read-only | Check the package files against the cached package schema. |
|
|
118
130
|
| `apply [--env] [--harness] [--mode]` | changing | Preview the files against the instance; prints and stores a preview id. |
|
|
@@ -699,7 +711,9 @@ an agent only with a new plugin or `init --update`. With `cavelon` installed,
|
|
|
699
711
|
|---|---|
|
|
700
712
|
| `~/.config/cavelon/config.json` | the current instance, the tenant chosen with `use`, which store holds the token. Never a token. |
|
|
701
713
|
| `~/.config/cavelon/credentials.json` | the token per instance (0600), only where there is no OS credential store |
|
|
714
|
+
| `~/.config/cavelon/setup.json` | what `cavelon setup` changed in your coding agents, so `setup --remove` undoes exactly that (0600) |
|
|
702
715
|
| `~/.cache/cavelon/<instance>/<version>/` | the instance's capabilities, OpenAPI, error catalog, package schema and docs index |
|
|
716
|
+
| `~/.cache/cavelon/update-check.json` | when the latest release was last looked up and announced; `CAVELON_NO_UPDATE_CHECK=1` turns the check off |
|
|
703
717
|
|
|
704
718
|
`CAVELON_CONFIG_DIR` and `CAVELON_CACHE_DIR` move them; `XDG_CONFIG_HOME` and
|
|
705
719
|
`XDG_CACHE_HOME` are honoured. `CAVELON_CREDENTIAL_STORE=file` skips the OS store.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const AGENT_VARIABLES = [
|
|
2
|
+
{ variable: "CAVELON_AGENT", agent: "any agent whose shell environment sets it" },
|
|
3
|
+
{ variable: "CLAUDECODE", agent: "Claude Code" },
|
|
4
|
+
{ variable: "CODEX_THREAD_ID", agent: "Codex" },
|
|
5
|
+
{ variable: "CODEX_SANDBOX", agent: "Codex" },
|
|
6
|
+
{ variable: "CURSOR_AGENT", agent: "Cursor" },
|
|
7
|
+
{ variable: "GEMINI_CLI", agent: "Gemini CLI" },
|
|
8
|
+
{ variable: "COPILOT_CLI", agent: "GitHub Copilot CLI" },
|
|
9
|
+
{ variable: "COPILOT_AGENT", agent: "GitHub Copilot in VS Code" },
|
|
10
|
+
{ variable: "AI_AGENT", agent: "an agent that follows the AI_AGENT convention" },
|
|
11
|
+
];
|
|
12
|
+
function isSet(value) {
|
|
13
|
+
const text = value?.trim().toLowerCase();
|
|
14
|
+
return Boolean(text) && text !== "0" && text !== "false";
|
|
15
|
+
}
|
|
16
|
+
export function agentVariable(env) {
|
|
17
|
+
return AGENT_VARIABLES.find(({ variable }) => isSet(env[variable]))?.variable;
|
|
18
|
+
}
|
|
19
|
+
export function drivenByAgent(ctx) {
|
|
20
|
+
if (ctx.mode === "mcp")
|
|
21
|
+
return { by: "mcp" };
|
|
22
|
+
const variable = agentVariable(ctx.io.env);
|
|
23
|
+
return variable ? { by: "agent", variable } : undefined;
|
|
24
|
+
}
|
package/dist/agents.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { promises as fs } from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { embeddedContent } from "./embedded.js";
|
|
4
5
|
import { usageError } from "./errors.js";
|
|
5
6
|
import { GENERATED_TOKEN } from "./markers.js";
|
|
6
7
|
import { KIT_VERSION } from "./version.js";
|
|
7
8
|
export const MCP_COMMAND = { command: "npx", args: ["-y", "@cavelon/cli@0.1", "mcp"] };
|
|
9
|
+
export const INSTALLED_MCP_COMMAND = { command: "cavelon", args: ["mcp"] };
|
|
8
10
|
export function mcpCommand(platform = process.platform) {
|
|
9
11
|
return platform === "win32" ? { command: "cmd", args: ["/c", MCP_COMMAND.command, ...MCP_COMMAND.args] } : MCP_COMMAND;
|
|
10
12
|
}
|
|
@@ -15,14 +17,16 @@ const jsonServer = (file, key = "mcpServers", extra = {}) => ({
|
|
|
15
17
|
format: "json",
|
|
16
18
|
keys: [key, "cavelon"],
|
|
17
19
|
entry: { ...extra, ...mcpCommand() },
|
|
18
|
-
others: otherPlatforms().map((p) => ({ ...extra, ...mcpCommand(p) })),
|
|
20
|
+
others: [...otherPlatforms().map((p) => ({ ...extra, ...mcpCommand(p) })), { ...extra, ...INSTALLED_MCP_COMMAND }],
|
|
19
21
|
});
|
|
20
22
|
const tomlServer = (file) => {
|
|
21
|
-
const block = (
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
const block = ({ command, args }) => ["[mcp_servers.cavelon]", `command = "${command}"`, `args = [${args.map((a) => JSON.stringify(a)).join(", ")}]`].join("\n");
|
|
24
|
+
return {
|
|
25
|
+
file,
|
|
26
|
+
format: "toml",
|
|
27
|
+
block: block(mcpCommand()),
|
|
28
|
+
others: [...otherPlatforms().map((p) => block(mcpCommand(p))), block(INSTALLED_MCP_COMMAND)],
|
|
24
29
|
};
|
|
25
|
-
return { file, format: "toml", block: block(process.platform), others: otherPlatforms().map(block) };
|
|
26
30
|
};
|
|
27
31
|
function agent(name, label, mcp) {
|
|
28
32
|
if (!mcp)
|
|
@@ -67,6 +71,9 @@ export function skillsSource() {
|
|
|
67
71
|
return [path.join(here, "skills"), path.resolve(here, "..", "..", "plugin", "skills")];
|
|
68
72
|
}
|
|
69
73
|
export async function bundledSkills() {
|
|
74
|
+
const embedded = embeddedContent();
|
|
75
|
+
if (embedded)
|
|
76
|
+
return embedded.skills;
|
|
70
77
|
for (const dir of skillsSource()) {
|
|
71
78
|
let entries;
|
|
72
79
|
try {
|
|
@@ -96,10 +103,11 @@ export async function bundledSkills() {
|
|
|
96
103
|
}
|
|
97
104
|
return [];
|
|
98
105
|
}
|
|
99
|
-
export function generatedCopy(file) {
|
|
106
|
+
export function generatedCopy(file, by = "init") {
|
|
100
107
|
if (!file.path.endsWith(".md"))
|
|
101
108
|
return file.content;
|
|
102
|
-
const
|
|
109
|
+
const how = by === "init" ? "`cavelon init --agents` (cavelon " + KIT_VERSION + "); `cavelon init --update` replaces this file" : "`cavelon setup` (cavelon " + KIT_VERSION + "); `cavelon setup` replaces it and `cavelon setup --remove` removes it";
|
|
110
|
+
const mark = `<!-- ${GENERATED_TOKEN}: written by ${how}. Put your own notes in another file. -->`;
|
|
103
111
|
const front = /^---\r?\n[\s\S]*?\r?\n---\r?\n/.exec(file.content);
|
|
104
112
|
if (front)
|
|
105
113
|
return `${front[0]}${mark}\n${file.content.slice(front[0].length)}`;
|
package/dist/choose.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { CavelonError, ExitCode } from "./errors.js";
|
|
2
|
+
import { readLine } from "./prompt.js";
|
|
3
|
+
export const MAX_LISTED = 20;
|
|
4
|
+
const MAX_TRIES = 5;
|
|
5
|
+
function lower(value) {
|
|
6
|
+
return (value ?? "").toLowerCase();
|
|
7
|
+
}
|
|
8
|
+
export function exactMatch(items, ref) {
|
|
9
|
+
const wanted = ref.trim().toLowerCase();
|
|
10
|
+
return (items.find((t) => t.id === ref.trim()) ??
|
|
11
|
+
items.find((t) => lower(t.slug) === wanted) ??
|
|
12
|
+
(() => {
|
|
13
|
+
const byName = items.filter((t) => lower(t.name) === wanted);
|
|
14
|
+
return byName.length === 1 ? byName[0] : undefined;
|
|
15
|
+
})());
|
|
16
|
+
}
|
|
17
|
+
export function containing(items, text) {
|
|
18
|
+
const wanted = text.trim().toLowerCase();
|
|
19
|
+
return items.filter((t) => lower(t.name).includes(wanted) || lower(t.slug).includes(wanted));
|
|
20
|
+
}
|
|
21
|
+
function editDistance(a, b) {
|
|
22
|
+
let previous = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
23
|
+
for (let i = 1; i <= a.length; i++) {
|
|
24
|
+
const current = [i];
|
|
25
|
+
for (let j = 1; j <= b.length; j++) {
|
|
26
|
+
current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
27
|
+
}
|
|
28
|
+
previous = current;
|
|
29
|
+
}
|
|
30
|
+
return previous[b.length];
|
|
31
|
+
}
|
|
32
|
+
export function closest(items, ref, max = 5) {
|
|
33
|
+
const wanted = ref.trim().toLowerCase();
|
|
34
|
+
if (!wanted)
|
|
35
|
+
return items.slice(0, max);
|
|
36
|
+
const scored = items.flatMap((item) => {
|
|
37
|
+
const words = [lower(item.slug), lower(item.name)].filter(Boolean);
|
|
38
|
+
if (words.some((w) => w.includes(wanted)))
|
|
39
|
+
return [{ item, score: 0 }];
|
|
40
|
+
if (words.some((w) => w.length >= 3 && wanted.includes(w)))
|
|
41
|
+
return [{ item, score: 1 }];
|
|
42
|
+
const distance = Math.min(...words.map((w) => editDistance(w, wanted)));
|
|
43
|
+
return distance <= Math.max(2, Math.floor(wanted.length / 3)) ? [{ item, score: distance + 1 }] : [];
|
|
44
|
+
});
|
|
45
|
+
return scored
|
|
46
|
+
.sort((a, b) => a.score - b.score)
|
|
47
|
+
.slice(0, max)
|
|
48
|
+
.map((s) => s.item);
|
|
49
|
+
}
|
|
50
|
+
export function displayName(item) {
|
|
51
|
+
if (item.name && item.slug && item.name !== item.slug)
|
|
52
|
+
return `${item.name} (${item.slug})`;
|
|
53
|
+
return item.name ?? item.slug ?? item.id;
|
|
54
|
+
}
|
|
55
|
+
function line(index, item, extra, preferred) {
|
|
56
|
+
const slug = item.slug && item.slug !== item.name ? ` ${item.slug}` : "";
|
|
57
|
+
const notes = [extra, preferred ? "default, press Enter" : undefined].filter(Boolean).join(", ");
|
|
58
|
+
return ` ${String(index).padStart(2)} ${item.name ?? item.slug ?? item.id}${slug}${notes ? ` (${notes})` : ""}`;
|
|
59
|
+
}
|
|
60
|
+
export async function pick(ctx, options) {
|
|
61
|
+
const write = (text) => ctx.io.stderr.write(`${text}\n`);
|
|
62
|
+
let shown = options.items.slice(0, MAX_LISTED);
|
|
63
|
+
const show = (items) => {
|
|
64
|
+
items.forEach((item, i) => write(line(i + 1, item, options.extra?.(item), item === options.preferred)));
|
|
65
|
+
if (options.other)
|
|
66
|
+
write(` ${String(items.length + 1).padStart(2)} ${options.other.label}`);
|
|
67
|
+
};
|
|
68
|
+
write(options.intro);
|
|
69
|
+
show(shown);
|
|
70
|
+
if (options.items.length > shown.length)
|
|
71
|
+
write(` … and ${options.items.length - shown.length} more: type part of a name to find one.`);
|
|
72
|
+
const hint = shown.length || options.other ? "type its number or part of its name" : "type part of its name";
|
|
73
|
+
for (let tries = 0; tries < MAX_TRIES; tries++) {
|
|
74
|
+
const answer = await readLine(ctx.io, `${options.question} ${ctx.style.dim(`(${hint})`)} `);
|
|
75
|
+
if (!answer) {
|
|
76
|
+
if (options.preferred)
|
|
77
|
+
return { item: options.preferred };
|
|
78
|
+
write("Type a number from the list, or part of a name.");
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (options.other && answer.toLowerCase() === options.other.word)
|
|
82
|
+
return { other: true };
|
|
83
|
+
if (/^\d+$/.test(answer)) {
|
|
84
|
+
const n = Number(answer);
|
|
85
|
+
if (n >= 1 && n <= shown.length)
|
|
86
|
+
return { item: shown[n - 1] };
|
|
87
|
+
if (options.other && n === shown.length + 1)
|
|
88
|
+
return { other: true };
|
|
89
|
+
write(`There is no number ${n} in the list.`);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
let found = containing(options.items, answer);
|
|
93
|
+
if (!found.length && options.search)
|
|
94
|
+
found = await options.search(answer);
|
|
95
|
+
const exact = exactMatch(found, answer);
|
|
96
|
+
if (exact)
|
|
97
|
+
return { item: exact };
|
|
98
|
+
if (found.length === 1)
|
|
99
|
+
return { item: found[0] };
|
|
100
|
+
if (!found.length) {
|
|
101
|
+
const near = closest(options.items, answer);
|
|
102
|
+
write(`Nothing is called "${answer}".${near.length ? ` Did you mean ${near.map(displayName).join(", ")}?` : ""} Try another part of the name.`);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
shown = found.slice(0, MAX_LISTED);
|
|
106
|
+
write(`${found.length} match "${answer}":`);
|
|
107
|
+
show(shown);
|
|
108
|
+
if (found.length > shown.length)
|
|
109
|
+
write(` … and ${found.length - shown.length} more: type more of the name.`);
|
|
110
|
+
}
|
|
111
|
+
throw new CavelonError(ExitCode.usage, { code: "cancelled", message: "Nothing was chosen.", hint: "Run the command again, or name it with an option." });
|
|
112
|
+
}
|
package/dist/commands/api.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
1
2
|
import { promises as fs } from "node:fs";
|
|
2
3
|
import path from "node:path";
|
|
4
|
+
import { AGENT_VARIABLES, drivenByAgent } from "../agent-env.js";
|
|
3
5
|
import { boolOption, CURSOR_OPTION, intOption, LIMIT_OPTION, listOption, pageOf, positional, stringOption, } from "../command.js";
|
|
4
6
|
import { CavelonError, ExitCode, usageError, validationError } from "../errors.js";
|
|
5
7
|
import { clip, moreHint, table } from "../format.js";
|
|
6
8
|
import { readAll } from "../io.js";
|
|
7
9
|
import { callOperation, previewRequest } from "../invoke.js";
|
|
8
10
|
import { confinedPath } from "../paths.js";
|
|
9
|
-
import { describeSchema, findOperation, jsonBodySchema, operations, schemaTypes } from "../openapi.js";
|
|
11
|
+
import { describeSchema, findOperation, jsonBodySchema, operations, schemaTypes, secretFields } from "../openapi.js";
|
|
12
|
+
import { canonical } from "../package-files.js";
|
|
10
13
|
import { cavelonCommand } from "../shell.js";
|
|
11
14
|
export function splitJsonBody(args) {
|
|
12
15
|
const out = [];
|
|
@@ -26,6 +29,25 @@ export function splitJsonBody(args) {
|
|
|
26
29
|
}
|
|
27
30
|
return out;
|
|
28
31
|
}
|
|
32
|
+
const CONFIRM_TOKEN = /^[0-9a-f]{12}$/;
|
|
33
|
+
export function splitConfirm(args) {
|
|
34
|
+
const out = [];
|
|
35
|
+
for (let i = 0; i < args.length; i++) {
|
|
36
|
+
const arg = args[i];
|
|
37
|
+
if (arg !== "--confirm") {
|
|
38
|
+
out.push(arg);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const next = args[i + 1];
|
|
42
|
+
if (next !== undefined && CONFIRM_TOKEN.test(next)) {
|
|
43
|
+
out.push(`--confirm=${next}`);
|
|
44
|
+
i++;
|
|
45
|
+
}
|
|
46
|
+
else
|
|
47
|
+
out.push("--confirm=");
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
29
51
|
function looksLikeBody(value) {
|
|
30
52
|
if (value === "-" || value.startsWith("@") || /^\s*[[{"]/.test(value))
|
|
31
53
|
return true;
|
|
@@ -37,7 +59,7 @@ function looksLikeBody(value) {
|
|
|
37
59
|
return false;
|
|
38
60
|
}
|
|
39
61
|
}
|
|
40
|
-
export async function readBody(ctx, raw) {
|
|
62
|
+
export async function readBody(ctx, raw, confine = ctx.mode === "mcp") {
|
|
41
63
|
let text = raw;
|
|
42
64
|
if (raw === "-" && ctx.mode === "mcp") {
|
|
43
65
|
throw usageError("A body from standard input is not available over MCP; pass the JSON itself.");
|
|
@@ -45,7 +67,7 @@ export async function readBody(ctx, raw) {
|
|
|
45
67
|
if (raw === "-")
|
|
46
68
|
text = await readAll(ctx.io.stdin);
|
|
47
69
|
else if (raw.startsWith("@"))
|
|
48
|
-
text = await fs.readFile(await confinedPath(ctx, raw.slice(1), "The body file"), "utf8");
|
|
70
|
+
text = await fs.readFile(await confinedPath(ctx, raw.slice(1), "The body file", confine), "utf8");
|
|
49
71
|
try {
|
|
50
72
|
return JSON.parse(text);
|
|
51
73
|
}
|
|
@@ -98,13 +120,50 @@ export function keptForPerson(op, all) {
|
|
|
98
120
|
const rule = byPathWords(op);
|
|
99
121
|
return rule ? { source: "kit", reason: rule.does, hint: rule.hint } : undefined;
|
|
100
122
|
}
|
|
101
|
-
function
|
|
123
|
+
function notSentBy(driven) {
|
|
124
|
+
return driven.by === "mcp"
|
|
125
|
+
? "no tool sends it, with or without confirm."
|
|
126
|
+
: `cavelon api does not send it when a coding agent runs it (${driven.variable} is set), with or without --confirm.`;
|
|
127
|
+
}
|
|
128
|
+
function personHint(hint, driven) {
|
|
129
|
+
return driven.by === "mcp" ? hint : `${hint} A person's own terminal is not guarded: it does not set ${driven.variable}.`;
|
|
130
|
+
}
|
|
131
|
+
function refusal(op, kept, driven) {
|
|
102
132
|
const named = `${op.alias} (${op.method} ${op.path})`;
|
|
103
|
-
const after =
|
|
133
|
+
const after = notSentBy(driven);
|
|
104
134
|
if (kept.source === "kit")
|
|
105
135
|
return `${named} ${kept.reason}; that stays with a person, so ${after}`;
|
|
106
136
|
return `${named} is for a person only, as the instance marks it${kept.reason ? ` (${kept.reason})` : ""}; ${after}`;
|
|
107
137
|
}
|
|
138
|
+
function secretsIn(doc, op, args) {
|
|
139
|
+
const found = new Set();
|
|
140
|
+
const fileFields = args.files?.length ? Object.fromEntries(args.files.map((f) => [f.field, f.path])) : undefined;
|
|
141
|
+
for (const media of Object.values(op.requestBody?.content ?? {})) {
|
|
142
|
+
if (args.body !== undefined)
|
|
143
|
+
for (const field of secretFields(doc, media.schema, args.body))
|
|
144
|
+
found.add(field);
|
|
145
|
+
if (fileFields)
|
|
146
|
+
for (const field of secretFields(doc, media.schema, fileFields))
|
|
147
|
+
found.add(field);
|
|
148
|
+
}
|
|
149
|
+
return [...found];
|
|
150
|
+
}
|
|
151
|
+
function secretRefusal(op, fields, driven) {
|
|
152
|
+
const named = fields.map((f) => `"${f}"`).join(", ");
|
|
153
|
+
return new CavelonError(ExitCode.usage, {
|
|
154
|
+
code: "secret_field_for_a_person",
|
|
155
|
+
message: `The request to ${op.alias} sets ${named}, which the instance marks as a secret value (x-cavelon-secret); ` +
|
|
156
|
+
`a person enters secret values, so ${notSentBy(driven)}`,
|
|
157
|
+
hint: personHint("A person sets a secret value in their terminal with `cavelon secrets set <name>`, or enters it in the Admin. Leave the field out to send the rest.", driven),
|
|
158
|
+
details: { fields },
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
function confirmToken(target, request, files) {
|
|
162
|
+
return createHash("sha256")
|
|
163
|
+
.update(canonical({ instance: target.url ?? null, tenant: target.tenant ?? null, request, files }))
|
|
164
|
+
.digest("hex")
|
|
165
|
+
.slice(0, 12);
|
|
166
|
+
}
|
|
108
167
|
function truncate(data, limit) {
|
|
109
168
|
if (limit <= 0)
|
|
110
169
|
return { data };
|
|
@@ -123,10 +182,14 @@ export const api = {
|
|
|
123
182
|
description: "The operation is its operationId or the short name before FastAPI's path suffix (list_harnesses).\n" +
|
|
124
183
|
"Parameters: -p name=value or name=value. Body: --json '<json>', --json @file.json or --json - (stdin).\n" +
|
|
125
184
|
"The body is checked against the operation's schema before it is sent.\n" +
|
|
126
|
-
"As an MCP tool,
|
|
127
|
-
|
|
128
|
-
"
|
|
129
|
-
"
|
|
185
|
+
"As an MCP tool, or run by a coding agent (" +
|
|
186
|
+
AGENT_VARIABLES.filter((v) => v.variable !== "CAVELON_AGENT").map((v) => v.variable).join(", ") +
|
|
187
|
+
"\nor CAVELON_AGENT=1 is set), an operation that changes something returns what it would send and sends it only\n" +
|
|
188
|
+
"with confirm (as an MCP tool) or --confirm <token> (the token the preview printed).\n" +
|
|
189
|
+
"Run by an agent, one the instance marks for a person only (x-cavelon-person-only) is refused, as is a body that\n" +
|
|
190
|
+
"sets a field the instance marks as a secret value (x-cavelon-secret) and a file outside the solution folder. On an\n" +
|
|
191
|
+
"instance that marks no operation, one that changes a secret, creates or revokes a credential or decides an\n" +
|
|
192
|
+
"approval is refused. A person's own terminal sends at once.",
|
|
130
193
|
readOnly: false,
|
|
131
194
|
destructive: true,
|
|
132
195
|
mcpTool: "api",
|
|
@@ -140,8 +203,11 @@ export const api = {
|
|
|
140
203
|
file: { type: "string", multiple: true, value: "<field=path>", description: "Attach a file to a multipart body." },
|
|
141
204
|
output: { type: "string", value: "<file>", description: "Write the response body to a file instead of printing it.", cliOnly: true },
|
|
142
205
|
confirm: {
|
|
143
|
-
type: "
|
|
144
|
-
|
|
206
|
+
type: "string",
|
|
207
|
+
value: "<token>",
|
|
208
|
+
mcpBoolean: true,
|
|
209
|
+
description: "Send an operation that changes something: run by a coding agent, the token its preview printed; as an MCP tool, true. " +
|
|
210
|
+
"Without it, nothing is sent. A person's terminal sends at once.",
|
|
145
211
|
},
|
|
146
212
|
limit: { type: "string", value: "<n>", description: "Show at most n items of a list response (default 50, 0 for all)." },
|
|
147
213
|
},
|
|
@@ -150,49 +216,52 @@ export const api = {
|
|
|
150
216
|
"cavelon api get_harness_by_slug slug=support",
|
|
151
217
|
"cavelon api create_knowledge_base --json '{\"name\": \"FAQ\"}'",
|
|
152
218
|
],
|
|
153
|
-
preprocess: splitJsonBody,
|
|
219
|
+
preprocess: (args) => splitConfirm(splitJsonBody(args)),
|
|
154
220
|
async run(ctx, input) {
|
|
155
221
|
const name = positional(input, "operation");
|
|
156
222
|
const doc = await (await ctx.contracts()).openapi();
|
|
157
223
|
const op = findOperation(doc, name);
|
|
224
|
+
const driven = drivenByAgent(ctx);
|
|
225
|
+
const confine = driven !== undefined;
|
|
158
226
|
const args = { params: parseParams(input) };
|
|
159
227
|
const rawBody = stringOption(input, "body");
|
|
160
228
|
if (rawBody !== undefined)
|
|
161
|
-
args.body = await readBody(ctx, rawBody);
|
|
229
|
+
args.body = await readBody(ctx, rawBody, confine);
|
|
162
230
|
for (const spec of listOption(input, "file")) {
|
|
163
231
|
const at = spec.indexOf("=");
|
|
164
232
|
if (at <= 0)
|
|
165
233
|
throw usageError(`--file "${spec}" is not field=path.`);
|
|
166
|
-
const file = await confinedPath(ctx, spec.slice(at + 1), "The file");
|
|
234
|
+
const file = await confinedPath(ctx, spec.slice(at + 1), "The file", confine);
|
|
167
235
|
args.files = [...(args.files ?? []), { field: spec.slice(0, at), path: file }];
|
|
168
236
|
}
|
|
169
|
-
|
|
237
|
+
const rawOutput = stringOption(input, "output");
|
|
238
|
+
const output = rawOutput ? await confinedPath(ctx, rawOutput, "The output file", confine) : undefined;
|
|
239
|
+
if (driven) {
|
|
170
240
|
const kept = keptForPerson(op, operations(doc));
|
|
171
241
|
if (kept) {
|
|
172
242
|
throw new CavelonError(ExitCode.usage, {
|
|
173
243
|
code: "operation_for_a_person",
|
|
174
|
-
message: refusal(op, kept),
|
|
175
|
-
hint: kept.hint,
|
|
176
|
-
details: { source: kept.source, ...(kept.reason ? { reason: kept.reason } : {}) },
|
|
244
|
+
message: refusal(op, kept, driven),
|
|
245
|
+
hint: personHint(kept.hint, driven),
|
|
246
|
+
details: { source: kept.source, ...(kept.reason ? { reason: kept.reason } : {}), ...(driven.by === "agent" ? { agent_variable: driven.variable } : {}) },
|
|
177
247
|
});
|
|
178
248
|
}
|
|
249
|
+
const secrets = secretsIn(doc, op, args);
|
|
250
|
+
if (secrets.length)
|
|
251
|
+
throw secretRefusal(op, secrets, driven);
|
|
179
252
|
}
|
|
180
|
-
if (
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
data: { operation: op.alias, ...request, ...(files.length ? { files } : {}), sent: false, confirm: "Call api again with the same arguments and confirm: true." },
|
|
185
|
-
text: `Would send ${request.method} ${request.path}. Nothing was sent.`,
|
|
186
|
-
};
|
|
253
|
+
if (driven && !op.readOnly) {
|
|
254
|
+
const preview = await previewUnlessConfirmed(ctx, input, doc, op, args, driven);
|
|
255
|
+
if (preview)
|
|
256
|
+
return preview;
|
|
187
257
|
}
|
|
188
258
|
const client = await ctx.client();
|
|
189
259
|
const result = await callOperation(ctx, client, doc, op, args);
|
|
190
|
-
const output = stringOption(input, "output");
|
|
191
260
|
if (output) {
|
|
192
261
|
const content = result.bytes ?? (typeof result.data === "string" ? result.data : JSON.stringify(result.data, null, 2));
|
|
193
262
|
await fs.writeFile(output, content);
|
|
194
263
|
const size = typeof content === "string" ? Buffer.byteLength(content) : content.length;
|
|
195
|
-
return { data: { status: result.status, written:
|
|
264
|
+
return { data: { status: result.status, written: rawOutput, bytes: size }, text: `Wrote ${size} bytes to ${rawOutput}.` };
|
|
196
265
|
}
|
|
197
266
|
if (result.bytes) {
|
|
198
267
|
throw validationError(`${op.alias} answered ${result.contentType} (${result.bytes.length} bytes).`, undefined, "Save it with --output <file>.");
|
|
@@ -205,6 +274,43 @@ export const api = {
|
|
|
205
274
|
return { data, text };
|
|
206
275
|
},
|
|
207
276
|
};
|
|
277
|
+
async function previewUnlessConfirmed(ctx, input, doc, op, args, driven) {
|
|
278
|
+
if (driven.by === "mcp" && boolOption(input, "confirm"))
|
|
279
|
+
return undefined;
|
|
280
|
+
const request = previewRequest(doc, op, args);
|
|
281
|
+
const files = (args.files ?? []).map((f) => ({ field: f.field, file: path.relative(ctx.io.cwd, f.path) || f.path }));
|
|
282
|
+
const shown = { operation: op.alias, ...request, ...(files.length ? { files } : {}), sent: false };
|
|
283
|
+
if (driven.by === "mcp") {
|
|
284
|
+
return {
|
|
285
|
+
data: { ...shown, confirm: "Call api again with the same arguments and confirm: true." },
|
|
286
|
+
text: `Would send ${request.method} ${request.path}. Nothing was sent.`,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
const session = await ctx.session();
|
|
290
|
+
const token = confirmToken({ url: session.url, tenant: session.tenant }, request, files);
|
|
291
|
+
const given = stringOption(input, "confirm");
|
|
292
|
+
if (given === token)
|
|
293
|
+
return undefined;
|
|
294
|
+
const stale = Boolean(given);
|
|
295
|
+
const confirm = `Show the person this request, then run the same command again with --confirm ${token} to send exactly it.`;
|
|
296
|
+
const lines = [`Would send ${request.method} ${request.path}. Nothing was sent.`];
|
|
297
|
+
if (stale)
|
|
298
|
+
lines.push("The --confirm token is not this request's: the request changed since its preview, or the token is another one's.");
|
|
299
|
+
if (Object.keys(request.query).length)
|
|
300
|
+
lines.push(`Query: ${JSON.stringify(request.query)}`);
|
|
301
|
+
if (Object.keys(request.headers).length)
|
|
302
|
+
lines.push(`Headers: ${JSON.stringify(request.headers)}`);
|
|
303
|
+
if (request.body !== null)
|
|
304
|
+
lines.push("Body:", JSON.stringify(request.body, null, 2));
|
|
305
|
+
for (const f of files)
|
|
306
|
+
lines.push(`File: ${f.field} = ${f.file}`);
|
|
307
|
+
lines.push(confirm);
|
|
308
|
+
return {
|
|
309
|
+
data: { ...shown, confirm_token: token, confirm, ...(stale ? { token_mismatch: true } : {}) },
|
|
310
|
+
text: lines.join("\n"),
|
|
311
|
+
...(stale ? { exitCode: ExitCode.conflict } : {}),
|
|
312
|
+
};
|
|
313
|
+
}
|
|
208
314
|
export const apiList = {
|
|
209
315
|
name: "api list",
|
|
210
316
|
summary: "List the operations the instance publishes.",
|
package/dist/commands/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import { wait, watch } from "./async.js";
|
|
|
4
4
|
import { docsGet, docsSearch } from "./docs.js";
|
|
5
5
|
import { login, logout, status, use, whoami } from "./session.js";
|
|
6
6
|
import { init } from "./init.js";
|
|
7
|
+
import { setup } from "./setup.js";
|
|
7
8
|
import { limits } from "./limits.js";
|
|
8
9
|
import { limitsSet } from "./limits-set.js";
|
|
9
10
|
import { modelsList, modelsSetLimit } from "./models.js";
|
|
@@ -53,6 +54,7 @@ const mcp = {
|
|
|
53
54
|
},
|
|
54
55
|
};
|
|
55
56
|
export const COMMANDS = [
|
|
57
|
+
setup,
|
|
56
58
|
login,
|
|
57
59
|
logout,
|
|
58
60
|
whoami,
|