@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/dist/commands/tenants.js
CHANGED
|
@@ -3,7 +3,11 @@ import { CavelonError, ExitCode, usageError } from "../errors.js";
|
|
|
3
3
|
import { keyValues, moreHint, table } from "../format.js";
|
|
4
4
|
import { callStable } from "../invoke.js";
|
|
5
5
|
import { formatQuota, limitError, limitsOrWarn, readQuotas } from "../limits.js";
|
|
6
|
-
import {
|
|
6
|
+
import { containing } from "../choose.js";
|
|
7
|
+
import { readTenantless } from "../principal.js";
|
|
8
|
+
import { requireInstance } from "../session.js";
|
|
9
|
+
import { listsTenants, searchTenants } from "../tenant-choice.js";
|
|
10
|
+
import { resolveHarnessId } from "../harness-ref.js";
|
|
7
11
|
import { cavelonCommand } from "../shell.js";
|
|
8
12
|
import { rememberTenant } from "./session.js";
|
|
9
13
|
function idempotency(key) {
|
|
@@ -56,7 +60,9 @@ export const tenantCreate = {
|
|
|
56
60
|
};
|
|
57
61
|
export const tenantList = {
|
|
58
62
|
name: "tenant list",
|
|
59
|
-
summary: "List the tenants this token can see.",
|
|
63
|
+
summary: "List the tenants this token can see, with name, slug and id.",
|
|
64
|
+
description: "A personal access token in Platform mode sees every tenant; any other sees the tenants it reaches. " +
|
|
65
|
+
"An operator's token that reaches every tenant lists the person's own and finds any other with --search.",
|
|
60
66
|
readOnly: true,
|
|
61
67
|
idempotent: true,
|
|
62
68
|
mcpTool: "tenant_list",
|
|
@@ -72,16 +78,19 @@ export const tenantList = {
|
|
|
72
78
|
}
|
|
73
79
|
const limit = intOption(input, "limit", { min: 1, max: 200, fallback: 50 });
|
|
74
80
|
const cursor = stringOption(input, "cursor");
|
|
81
|
+
const search = stringOption(input, "search");
|
|
75
82
|
const offset = cursor === undefined ? 0 : Number(cursor);
|
|
76
83
|
if (!Number.isInteger(offset) || offset < 0)
|
|
77
84
|
throw usageError(`--cursor "${cursor}" is not a cursor from a previous page.`);
|
|
78
85
|
const client = await ctx.client({ tenant: false });
|
|
79
86
|
const listing = await client.get("/api/v1/tenants", {
|
|
80
|
-
query: { limit, offset, search
|
|
87
|
+
query: { limit, offset, search },
|
|
81
88
|
sendTenant: false,
|
|
82
89
|
allow: [403],
|
|
83
90
|
});
|
|
84
91
|
let page;
|
|
92
|
+
let note = "";
|
|
93
|
+
const reach = listing.status === 200 ? undefined : await readTenantless(client);
|
|
85
94
|
if (listing.status === 200) {
|
|
86
95
|
const items = listing.data.items.map((t) => ({ id: t.id, slug: t.slug, name: t.name, status: t.status ?? null, plan: t.plan ?? null }));
|
|
87
96
|
page = {
|
|
@@ -91,18 +100,28 @@ export const tenantList = {
|
|
|
91
100
|
source: "platform",
|
|
92
101
|
};
|
|
93
102
|
}
|
|
103
|
+
else if (listsTenants(reach)) {
|
|
104
|
+
const found = search && reach.reachesAll ? await searchTenants(client, search) : search ? containing(reach.tenants, search) : reach.tenants;
|
|
105
|
+
const all = found.map((t) => ({ id: t.id, slug: t.slug, name: t.name, role: t.role, is_default: t.is_default }));
|
|
106
|
+
page = { ...pageOf(all, limit, cursor), source: "token", reaches_all_tenants: reach.reachesAll };
|
|
107
|
+
if (reach.reachesAll && !search) {
|
|
108
|
+
note = `\nThis token reaches every tenant on ${client.url}${all.length ? "; these are your own" : ""}. Find any other: cavelon tenant list --search <part of the name>`;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
94
111
|
else {
|
|
95
112
|
const me = await client.get("/api/v1/auth/me", { sendTenant: false });
|
|
96
|
-
const
|
|
113
|
+
const wanted = search?.toLowerCase();
|
|
97
114
|
const all = (me.data.memberships ?? [])
|
|
98
|
-
.filter((m) => !
|
|
99
|
-
.map((m) => ({ id: m.tenant_id, slug: null, name: m.tenant_name, role: m.role ?? null }));
|
|
115
|
+
.filter((m) => !wanted || m.tenant_name.toLowerCase().includes(wanted) || Boolean(m.tenant_slug?.toLowerCase().includes(wanted)))
|
|
116
|
+
.map((m) => ({ id: m.tenant_id, slug: m.tenant_slug ?? null, name: m.tenant_name, role: m.role ?? null }));
|
|
100
117
|
page = { ...pageOf(all, limit, cursor), source: "memberships" };
|
|
101
118
|
}
|
|
102
|
-
const columns = page.source === "platform" ? ["
|
|
119
|
+
const columns = page.source === "platform" ? ["name", "slug", "status", "id"] : ["name", "slug", "role", "id"];
|
|
120
|
+
const empty = search ? `No tenant's name or slug contains "${search}".` : note ? "" : "No tenants.";
|
|
121
|
+
const next = page.items.length ? `\nChoose one: cavelon use <slug> (or \`cavelon use\` to pick from a list)` : "";
|
|
103
122
|
return {
|
|
104
123
|
data: page,
|
|
105
|
-
text: (table(page.items, columns) ||
|
|
124
|
+
text: ((table(page.items, columns) || empty) + moreHint(page.next_cursor, "cavelon tenant list") + note + next).trimStart(),
|
|
106
125
|
};
|
|
107
126
|
},
|
|
108
127
|
};
|
|
@@ -176,6 +195,10 @@ async function checkHarnessCapacity(ctx, slug) {
|
|
|
176
195
|
}
|
|
177
196
|
}
|
|
178
197
|
}
|
|
198
|
+
export async function createHarness(ctx, body, idempotencyKey) {
|
|
199
|
+
await checkHarnessCapacity(ctx, body.slug);
|
|
200
|
+
return callStable(ctx, "POST", "/api/v1/harnesses", "creating solutions", { body, headers: idempotency(idempotencyKey) });
|
|
201
|
+
}
|
|
179
202
|
export const harnessNew = {
|
|
180
203
|
name: "harness new",
|
|
181
204
|
summary: "Create an empty draft solution (harness).",
|
|
@@ -193,11 +216,7 @@ export const harnessNew = {
|
|
|
193
216
|
const description = stringOption(input, "description");
|
|
194
217
|
if (description)
|
|
195
218
|
body.description = description;
|
|
196
|
-
await
|
|
197
|
-
const harness = await callStable(ctx, "POST", "/api/v1/harnesses", "creating solutions", {
|
|
198
|
-
body,
|
|
199
|
-
headers: idempotency(stringOption(input, "idempotency-key")),
|
|
200
|
-
});
|
|
219
|
+
const harness = await createHarness(ctx, body, stringOption(input, "idempotency-key"));
|
|
201
220
|
return {
|
|
202
221
|
data: harness,
|
|
203
222
|
text: keyValues([
|
|
@@ -208,20 +227,12 @@ export const harnessNew = {
|
|
|
208
227
|
};
|
|
209
228
|
},
|
|
210
229
|
};
|
|
211
|
-
export async function resolveHarnessId(ctx, ref) {
|
|
212
|
-
if (isUuid(ref))
|
|
213
|
-
return ref;
|
|
214
|
-
const harness = await callStable(ctx, "GET", "/api/v1/harnesses/by-slug/{slug}", "finding solutions by slug", {
|
|
215
|
-
params: { slug: [ref] },
|
|
216
|
-
});
|
|
217
|
-
return harness.id;
|
|
218
|
-
}
|
|
219
230
|
export const harnessClone = {
|
|
220
231
|
name: "harness clone",
|
|
221
232
|
summary: "Copy a solution into a new draft solution.",
|
|
222
233
|
readOnly: false,
|
|
223
234
|
mcpTool: "harness_clone",
|
|
224
|
-
positionals: [{ name: "source", description: "
|
|
235
|
+
positionals: [{ name: "source", description: "Name, slug or id of the solution to copy.", required: true }],
|
|
225
236
|
options: {
|
|
226
237
|
slug: { type: "string", value: "<slug>", description: "The copy's slug." },
|
|
227
238
|
name: { type: "string", value: "<name>", description: "The copy's display name." },
|
package/dist/commands/work.js
CHANGED
|
@@ -14,7 +14,7 @@ import { cavelonCommand, shellWord } from "../shell.js";
|
|
|
14
14
|
import { readZipSummary, ZipError } from "../zip.js";
|
|
15
15
|
import { TIMEOUT_OPTION, timeoutMs, waitAndReport } from "./async.js";
|
|
16
16
|
import { stageErrors, stageErrorText } from "./loops.js";
|
|
17
|
-
import { resolveHarnessId } from "
|
|
17
|
+
import { resolveHarnessId } from "../harness-ref.js";
|
|
18
18
|
const WAIT_OPTION = { type: "boolean", description: "Wait for the work to finish (see `cavelon wait`).", cliOnly: true };
|
|
19
19
|
async function resolveKbId(ctx, ref) {
|
|
20
20
|
if (isUuid(ref))
|
|
@@ -367,7 +367,7 @@ export const testRun = {
|
|
|
367
367
|
mcpTool: "test_run",
|
|
368
368
|
options: {
|
|
369
369
|
suite: { type: "string", multiple: true, value: "<suite>", description: "Suite name or id." },
|
|
370
|
-
harness: { type: "string", value: "<harness>", description: "
|
|
370
|
+
harness: { type: "string", value: "<harness>", description: "The solution to run against: its name, slug or id." },
|
|
371
371
|
wait: WAIT_OPTION,
|
|
372
372
|
timeout: TIMEOUT_OPTION,
|
|
373
373
|
"idempotency-key": { type: "string", value: "<key>", description: "Send an Idempotency-Key with each start." },
|
package/dist/embedded.js
ADDED
package/dist/git.js
CHANGED
|
@@ -1,25 +1,30 @@
|
|
|
1
1
|
import { execFile } from "node:child_process";
|
|
2
2
|
import { promises as fs } from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
|
-
export async function
|
|
4
|
+
export async function findProgram(name, env = process.env, platform = process.platform, extensions = [".exe"]) {
|
|
5
5
|
const windows = platform === "win32";
|
|
6
6
|
const pathApi = windows ? path.win32 : path.posix;
|
|
7
7
|
const search = (windows ? (env.PATH ?? env.Path) : env.PATH) ?? "";
|
|
8
|
-
const
|
|
8
|
+
const names = windows ? extensions.map((ext) => name + ext) : [name];
|
|
9
9
|
for (const entry of search.split(pathApi.delimiter)) {
|
|
10
10
|
const dir = windows && entry.length > 1 && entry.startsWith('"') && entry.endsWith('"') ? entry.slice(1, -1) : entry;
|
|
11
11
|
if (!dir || !pathApi.isAbsolute(dir))
|
|
12
12
|
continue;
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
for (const file of names) {
|
|
14
|
+
const candidate = pathApi.join(dir, file);
|
|
15
|
+
const found = await fs
|
|
16
|
+
.stat(candidate)
|
|
17
|
+
.then((st) => st.isFile() && (windows || (st.mode & 0o111) !== 0))
|
|
18
|
+
.catch(() => false);
|
|
19
|
+
if (found)
|
|
20
|
+
return candidate;
|
|
21
|
+
}
|
|
20
22
|
}
|
|
21
23
|
return undefined;
|
|
22
24
|
}
|
|
25
|
+
export function resolveGit(env = process.env, platform = process.platform) {
|
|
26
|
+
return findProgram("git", env, platform);
|
|
27
|
+
}
|
|
23
28
|
export async function git(args, cwd) {
|
|
24
29
|
const executable = await resolveGit();
|
|
25
30
|
if (!executable)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { closest, displayName, exactMatch } from "./choose.js";
|
|
2
|
+
import { CavelonError, ExitCode } from "./errors.js";
|
|
3
|
+
import { callStable } from "./invoke.js";
|
|
4
|
+
import { isUuid } from "./session.js";
|
|
5
|
+
import { shellWord } from "./shell.js";
|
|
6
|
+
function notFound(error) {
|
|
7
|
+
return error instanceof CavelonError && error.status === 404;
|
|
8
|
+
}
|
|
9
|
+
export async function listHarnesses(ctx) {
|
|
10
|
+
return callStable(ctx, "GET", "/api/v1/harnesses", "listing solutions");
|
|
11
|
+
}
|
|
12
|
+
export async function lookupHarness(ctx, ref) {
|
|
13
|
+
if (isUuid(ref)) {
|
|
14
|
+
try {
|
|
15
|
+
return { harness: await callStable(ctx, "GET", "/api/v1/harnesses/{harness_id}", "reading solutions", { params: { harness_id: [ref] } }), candidates: [] };
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
if (!notFound(error))
|
|
19
|
+
throw error;
|
|
20
|
+
return { candidates: [] };
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
return { harness: await callStable(ctx, "GET", "/api/v1/harnesses/by-slug/{slug}", "finding solutions by slug", { params: { slug: [ref] } }), candidates: [] };
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
if (!notFound(error))
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
const all = await listHarnesses(ctx);
|
|
31
|
+
const hit = exactMatch(all, ref);
|
|
32
|
+
if (hit)
|
|
33
|
+
return { harness: hit, candidates: [] };
|
|
34
|
+
return { candidates: closest(all, ref) };
|
|
35
|
+
}
|
|
36
|
+
export function harnessNotFoundError(ref, candidates, source, command) {
|
|
37
|
+
const near = candidates.map(displayName).join(", ");
|
|
38
|
+
const lines = candidates.map((h) => ` ${command ? command(h.slug) : `--harness ${shellWord(h.slug)}`}`).join("\n");
|
|
39
|
+
return new CavelonError(ExitCode.failure, {
|
|
40
|
+
code: "solution_not_found",
|
|
41
|
+
message: `No solution "${ref}" in this tenant${source ? ` (from ${source})` : ""}.${near ? ` Closest: ${near}.` : ""}`,
|
|
42
|
+
hint: (candidates.length ? `If you meant one of them:\n${lines}\n` : "") +
|
|
43
|
+
"`cavelon harness list` shows this tenant's solutions with name, slug and id; `cavelon harness new <slug>` creates one, and an env file's harness is created by `apply`.",
|
|
44
|
+
details: candidates.length ? { candidates: candidates.map((h) => ({ id: h.id, slug: h.slug, name: h.name })) } : undefined,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export async function resolveHarnessId(ctx, ref) {
|
|
48
|
+
if (isUuid(ref))
|
|
49
|
+
return ref;
|
|
50
|
+
const { harness, candidates } = await lookupHarness(ctx, ref);
|
|
51
|
+
if (!harness)
|
|
52
|
+
throw harnessNotFoundError(ref, candidates);
|
|
53
|
+
return harness.id;
|
|
54
|
+
}
|
package/dist/http.js
CHANGED
|
@@ -2,6 +2,7 @@ import { capacityCodeIn, capacityHint } from "./capacity.js";
|
|
|
2
2
|
import { ceilingRefusal, LIMIT_ABOVE_CEILING } from "./limits.js";
|
|
3
3
|
import { CavelonError, ExitCode } from "./errors.js";
|
|
4
4
|
import { KIT_VERSION } from "./version.js";
|
|
5
|
+
const RUNTIME = process.versions.bun ? "bun/" + process.versions.bun : "node/" + process.versions.node;
|
|
5
6
|
export const DEFAULT_TIMEOUT_MS = 30_000;
|
|
6
7
|
export const NOT_HERE = [301, 302, 303, 307, 308, 401, 403, 404, 405, 406];
|
|
7
8
|
export const PERSONAL_ACCESS_TOKENS_DISABLED = "personal_access_tokens_disabled";
|
|
@@ -64,7 +65,7 @@ export class ApiClient {
|
|
|
64
65
|
}
|
|
65
66
|
headers(options = {}) {
|
|
66
67
|
const headers = {
|
|
67
|
-
"User-Agent": `cavelon/${KIT_VERSION}
|
|
68
|
+
"User-Agent": `cavelon/${KIT_VERSION} ${RUNTIME}`,
|
|
68
69
|
Accept: options.accept ?? "application/json",
|
|
69
70
|
...options.headers,
|
|
70
71
|
};
|
|
@@ -115,15 +116,16 @@ export class ApiClient {
|
|
|
115
116
|
});
|
|
116
117
|
}
|
|
117
118
|
if (!response.ok && !options.allow?.includes(response.status)) {
|
|
118
|
-
|
|
119
|
+
const tenantless = options.sendTenant !== false && !this.headers(options)["X-Tenant-Id"] && Boolean(this.target.token?.startsWith("cvpat_"));
|
|
120
|
+
throw await this.refusal(response.status, data, `${method} ${new URL(response.url || this.resolve(path)).pathname}`, response.headers, { tenantless });
|
|
119
121
|
}
|
|
120
122
|
return { status: response.status, headers: response.headers, data: data, text };
|
|
121
123
|
}
|
|
122
|
-
async refusal(status, body, what, headers) {
|
|
124
|
+
async refusal(status, body, what, headers, sent = {}) {
|
|
123
125
|
if (status === 401 && this.target.token?.startsWith("cvpat_") && (await this.personalAccessTokensOff())) {
|
|
124
126
|
return tokensDisabledError(this.url);
|
|
125
127
|
}
|
|
126
|
-
return errorFromResponse(status, body, what, headers);
|
|
128
|
+
return errorFromResponse(status, body, what, headers, sent);
|
|
127
129
|
}
|
|
128
130
|
personalAccessTokensOff() {
|
|
129
131
|
this.tokensOff ??= (async () => {
|
|
@@ -185,7 +187,11 @@ function networkError(error, url, signal) {
|
|
|
185
187
|
});
|
|
186
188
|
}
|
|
187
189
|
const BARE_CODE = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)+$/;
|
|
188
|
-
export
|
|
190
|
+
export const TENANT_ID_HINT = "A token without Platform mode works only inside a tenant. Run `cavelon use` to choose one of the tenants it reaches, " +
|
|
191
|
+
"or pass --tenant <name, slug or id> (or set CAVELON_TENANT). An older instance tells such a token nothing without a tenant, " +
|
|
192
|
+
"not even which tenants it reaches: pass --tenant <tenant-id> there; an operator copies the id in Platform › Tenants. " +
|
|
193
|
+
"A token limited to one tenant needs none.";
|
|
194
|
+
export function errorFromResponse(status, body, what, headers, sent = {}) {
|
|
189
195
|
let code;
|
|
190
196
|
let message;
|
|
191
197
|
let hint;
|
|
@@ -251,13 +257,15 @@ export function errorFromResponse(status, body, what, headers) {
|
|
|
251
257
|
details = details && typeof details === "object" ? { ...details, ...ceiling.details } : ceiling.details;
|
|
252
258
|
}
|
|
253
259
|
if (status === 400 && !hint && message && /select a tenant|tenant context|X-Tenant-Id/i.test(message)) {
|
|
254
|
-
hint = "Choose a tenant with `cavelon use
|
|
260
|
+
hint = "Choose a tenant with `cavelon use` (it lists your tenants), --tenant or CAVELON_TENANT.";
|
|
255
261
|
}
|
|
256
262
|
if ((status === 401 || status === 403) && !hint) {
|
|
257
263
|
hint =
|
|
258
264
|
status === 401
|
|
259
265
|
? "The token is missing, expired or revoked. A person runs `cavelon login` with a new one."
|
|
260
|
-
:
|
|
266
|
+
: sent.tenantless
|
|
267
|
+
? `No tenant was named. ${TENANT_ID_HINT} Otherwise check the token's permission ceiling.`
|
|
268
|
+
: "The token does not reach this. Check the tenant (`cavelon whoami`) and the token's permission ceiling.";
|
|
261
269
|
}
|
|
262
270
|
return new CavelonError(exitCode, {
|
|
263
271
|
code: code ?? defaultCode(status),
|
package/dist/install.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { embeddedContent } from "./embedded.js";
|
|
5
|
+
export const REPOSITORY = "goodguys-gmbh/cavelon-dev-kit";
|
|
6
|
+
export const NPM_PACKAGE = "@cavelon/cli";
|
|
7
|
+
const WINGET_ID = "goodguys.Cavelon";
|
|
8
|
+
const RELEASES = `https://github.com/${REPOSITORY}/releases/latest`;
|
|
9
|
+
const LABELS = {
|
|
10
|
+
script: "the install script",
|
|
11
|
+
executable: "a downloaded executable",
|
|
12
|
+
homebrew: "Homebrew",
|
|
13
|
+
winget: "winget",
|
|
14
|
+
npm: "npm i -g",
|
|
15
|
+
npx: "npx",
|
|
16
|
+
package: "a package manager",
|
|
17
|
+
source: "a source checkout",
|
|
18
|
+
};
|
|
19
|
+
export function installLabel(method) {
|
|
20
|
+
return LABELS[method];
|
|
21
|
+
}
|
|
22
|
+
export function detectInstall(facts) {
|
|
23
|
+
const { file, platform } = facts;
|
|
24
|
+
const p = platform === "win32" ? path.win32 : path.posix;
|
|
25
|
+
const segments = file.split(/[\\/]/);
|
|
26
|
+
if (facts.executable) {
|
|
27
|
+
if (segments.includes("Cellar") && segments[segments.indexOf("Cellar") + 1] === "cavelon") {
|
|
28
|
+
return { method: "homebrew", path: file, update: "brew upgrade cavelon", source: "github" };
|
|
29
|
+
}
|
|
30
|
+
const lower = segments.map((s) => s.toLowerCase());
|
|
31
|
+
const packages = lower.indexOf("packages");
|
|
32
|
+
if (platform === "win32" && packages > 0 && lower[packages - 1] === "winget" && lower[packages + 1]?.startsWith(`${WINGET_ID.toLowerCase()}_`)) {
|
|
33
|
+
return { method: "winget", path: file, update: `winget upgrade ${WINGET_ID}`, source: "github" };
|
|
34
|
+
}
|
|
35
|
+
const name = p.basename(file).toLowerCase();
|
|
36
|
+
if (name !== (platform === "win32" ? "cavelon.exe" : "cavelon")) {
|
|
37
|
+
return { method: "executable", path: file, advice: `Download the new one from ${RELEASES}.`, source: "github" };
|
|
38
|
+
}
|
|
39
|
+
return { method: "script", path: file, update: scriptUpdate(facts, p.dirname(file)), source: "github" };
|
|
40
|
+
}
|
|
41
|
+
if (segments.includes("_npx")) {
|
|
42
|
+
return { method: "npx", path: file, advice: "npx starts the newest release of the version range it is given each time; nothing to update." };
|
|
43
|
+
}
|
|
44
|
+
const end = segments.slice(-3).join("/");
|
|
45
|
+
if (end !== `node_modules/${NPM_PACKAGE}`)
|
|
46
|
+
return { method: "source", path: file };
|
|
47
|
+
const modules = p.dirname(p.dirname(file));
|
|
48
|
+
const shim = platform === "win32"
|
|
49
|
+
? p.join(p.dirname(modules), "cavelon.cmd")
|
|
50
|
+
: p.basename(p.dirname(modules)) === "lib"
|
|
51
|
+
? p.join(p.dirname(p.dirname(modules)), "bin", "cavelon")
|
|
52
|
+
: undefined;
|
|
53
|
+
if (shim && facts.exists(shim))
|
|
54
|
+
return { method: "npm", path: file, update: `npm i -g ${NPM_PACKAGE}`, source: "npm" };
|
|
55
|
+
return { method: "package", path: file, advice: `Update ${NPM_PACKAGE} with the package manager that installed it.`, source: "npm" };
|
|
56
|
+
}
|
|
57
|
+
function scriptUpdate(facts, dir) {
|
|
58
|
+
const { env } = facts;
|
|
59
|
+
if (facts.platform === "win32") {
|
|
60
|
+
const command = `irm https://github.com/${REPOSITORY}/releases/latest/download/install.ps1 | iex`;
|
|
61
|
+
const local = env.LOCALAPPDATA;
|
|
62
|
+
const usual = local ? path.win32.join(local, "Programs", "cavelon") : undefined;
|
|
63
|
+
if (usual && sameFolder(facts, usual, dir, true))
|
|
64
|
+
return command;
|
|
65
|
+
return `$env:CAVELON_INSTALL_DIR = '${dir.replaceAll("'", "''")}'; ${command}`;
|
|
66
|
+
}
|
|
67
|
+
const command = `curl -fsSL https://github.com/${REPOSITORY}/releases/latest/download/install.sh | sh`;
|
|
68
|
+
const home = env.HOME;
|
|
69
|
+
if (home && sameFolder(facts, path.posix.join(home, ".local", "bin"), dir, false))
|
|
70
|
+
return command;
|
|
71
|
+
return `${command} -s -- --dir ${shellQuote(dir)}`;
|
|
72
|
+
}
|
|
73
|
+
function sameFolder(facts, usual, dir, fold) {
|
|
74
|
+
const norm = (p) => (fold ? p.toLowerCase() : p);
|
|
75
|
+
return norm(usual) === norm(dir) || (facts.real !== undefined && norm(facts.real(usual)) === norm(dir));
|
|
76
|
+
}
|
|
77
|
+
function shellQuote(value) {
|
|
78
|
+
return /^[A-Za-z0-9_./~+-]+$/.test(value) ? value : `'${value.replaceAll("'", `'\\''`)}'`;
|
|
79
|
+
}
|
|
80
|
+
function realOrSame(file) {
|
|
81
|
+
try {
|
|
82
|
+
return realpathSync(file);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return file;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
export function currentInstall(env) {
|
|
89
|
+
const executable = embeddedContent() !== undefined;
|
|
90
|
+
const file = executable
|
|
91
|
+
? realOrSame(process.execPath)
|
|
92
|
+
: realOrSame(path.join(path.dirname(fileURLToPath(import.meta.url)), ".."));
|
|
93
|
+
return detectInstall({ executable, file, platform: process.platform, env, exists: existsSync, real: realOrSame });
|
|
94
|
+
}
|
package/dist/main.js
CHANGED
|
@@ -4,17 +4,20 @@ import { createContext, withWarnings } from "./context.js";
|
|
|
4
4
|
import { asCavelonError, ExitCode, usageError } from "./errors.js";
|
|
5
5
|
import { blockerLines } from "./format.js";
|
|
6
6
|
import { KIT_VERSION } from "./version.js";
|
|
7
|
+
import { currentInstall, installLabel } from "./install.js";
|
|
8
|
+
import { startUpdateCheck } from "./update-check.js";
|
|
7
9
|
import { COMMANDS } from "./commands/index.js";
|
|
8
|
-
export async function run(argv, io, commands = COMMANDS) {
|
|
10
|
+
export async function run(argv, io, commands = COMMANDS, updates = {}) {
|
|
9
11
|
const json = argv.includes("--json");
|
|
10
12
|
let ctx;
|
|
13
|
+
let notice;
|
|
11
14
|
try {
|
|
12
15
|
if (argv.length === 0) {
|
|
13
16
|
io.stderr.write(rootHelp(commands));
|
|
14
17
|
return ExitCode.usage;
|
|
15
18
|
}
|
|
16
19
|
if (argv[0] === "--version" || argv[0] === "-v" || argv[0] === "version") {
|
|
17
|
-
io.stdout.write(
|
|
20
|
+
io.stdout.write(versionText(updates.install ?? currentInstall(io.env), json));
|
|
18
21
|
return ExitCode.ok;
|
|
19
22
|
}
|
|
20
23
|
const found = findCommand(argv, commands);
|
|
@@ -42,16 +45,42 @@ export async function run(argv, io, commands = COMMANDS) {
|
|
|
42
45
|
tenant: typeof parsed.options.tenant === "string" ? parsed.options.tenant : undefined,
|
|
43
46
|
solutionEnv: spec.options?.env && typeof parsed.options.env === "string" ? parsed.options.env : undefined,
|
|
44
47
|
});
|
|
48
|
+
notice = startUpdateCheck({
|
|
49
|
+
io,
|
|
50
|
+
version: KIT_VERSION,
|
|
51
|
+
install: updates.install ?? currentInstall(io.env),
|
|
52
|
+
json: ctx.json,
|
|
53
|
+
command: spec.name,
|
|
54
|
+
fetch: updates.fetch,
|
|
55
|
+
});
|
|
45
56
|
const result = await spec.run(ctx, parsed);
|
|
46
57
|
printResult(ctx, result);
|
|
58
|
+
await printNotice(io, notice);
|
|
47
59
|
return result.exitCode ?? ExitCode.ok;
|
|
48
60
|
}
|
|
49
61
|
catch (error) {
|
|
50
62
|
const err = asCavelonError(error);
|
|
51
63
|
printError(io, ctx?.json ?? json, err, ctx?.warnings ?? []);
|
|
64
|
+
await printNotice(io, notice);
|
|
52
65
|
return err.exitCode;
|
|
53
66
|
}
|
|
54
67
|
}
|
|
68
|
+
function versionText(install, json) {
|
|
69
|
+
if (json) {
|
|
70
|
+
return `${JSON.stringify({ version: KIT_VERSION, install: { method: install.method, path: install.path, update: install.update ?? null } })}\n`;
|
|
71
|
+
}
|
|
72
|
+
const lines = [KIT_VERSION, `installed with: ${installLabel(install.method)} (${install.path})`];
|
|
73
|
+
if (install.update)
|
|
74
|
+
lines.push(`update with: ${install.update}`);
|
|
75
|
+
else if (install.advice)
|
|
76
|
+
lines.push(install.advice);
|
|
77
|
+
return `${lines.join("\n")}\n`;
|
|
78
|
+
}
|
|
79
|
+
async function printNotice(io, notice) {
|
|
80
|
+
const text = await notice;
|
|
81
|
+
if (text)
|
|
82
|
+
io.stderr.write(`\n${text}`);
|
|
83
|
+
}
|
|
55
84
|
function findCommand(argv, commands) {
|
|
56
85
|
const words = [];
|
|
57
86
|
for (let i = 0; i < argv.length; i++) {
|
package/dist/markers.js
CHANGED
|
@@ -43,6 +43,25 @@ export function upsertBlock(existing, body, style, options = {}) {
|
|
|
43
43
|
const next = [...lines.slice(0, found.begin + 1), ...bodyLines, ...lines.slice(found.end)].join(eol);
|
|
44
44
|
return next === existing ? { outcome: "unchanged" } : { outcome: "updated", content: next };
|
|
45
45
|
}
|
|
46
|
+
export function removeBlock(existing, style) {
|
|
47
|
+
if (existing === undefined)
|
|
48
|
+
return { outcome: "unchanged" };
|
|
49
|
+
const eol = lineEnding(existing);
|
|
50
|
+
const lines = existing.split(/\r?\n/);
|
|
51
|
+
const found = locate(lines, style);
|
|
52
|
+
if (!found)
|
|
53
|
+
return { outcome: "unchanged" };
|
|
54
|
+
if ("error" in found)
|
|
55
|
+
return { outcome: "skipped", reason: found.error };
|
|
56
|
+
let begin = found.begin;
|
|
57
|
+
if (begin > 0 && lines[begin - 1].trim() === "")
|
|
58
|
+
begin--;
|
|
59
|
+
const rest = [...lines.slice(0, begin), ...lines.slice(found.end + 1)];
|
|
60
|
+
const content = rest.join(eol);
|
|
61
|
+
if (!content.trim())
|
|
62
|
+
return { outcome: "updated", content: "", empty: true };
|
|
63
|
+
return { outcome: "updated", content };
|
|
64
|
+
}
|
|
46
65
|
export function isGenerated(content) {
|
|
47
66
|
return content !== undefined && content.includes(GENERATED_TOKEN);
|
|
48
67
|
}
|
|
@@ -81,6 +100,40 @@ export function upsertJsonEntry(existing, keys, value) {
|
|
|
81
100
|
setPath(parsed, keys, value);
|
|
82
101
|
return { outcome: current === undefined ? "appended" : "updated", content: serialize(parsed) };
|
|
83
102
|
}
|
|
103
|
+
export function removeJsonEntry(existing, keys, matches, keep = 0) {
|
|
104
|
+
if (existing === undefined)
|
|
105
|
+
return { outcome: "unchanged" };
|
|
106
|
+
let parsed;
|
|
107
|
+
try {
|
|
108
|
+
parsed = JSON.parse(existing);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return { outcome: "skipped", reason: "it is not plain JSON (comments or a syntax error)" };
|
|
112
|
+
}
|
|
113
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
114
|
+
return { outcome: "skipped", reason: "it is not a JSON object" };
|
|
115
|
+
const current = getPath(parsed, keys);
|
|
116
|
+
if (current === undefined)
|
|
117
|
+
return { outcome: "unchanged" };
|
|
118
|
+
if (!matches(current))
|
|
119
|
+
return { outcome: "skipped", reason: `its "${keys.join(".")}" was changed since cavelon wrote it` };
|
|
120
|
+
const indent = detectIndent(existing);
|
|
121
|
+
const eol = lineEnding(existing);
|
|
122
|
+
const serialize = (v) => {
|
|
123
|
+
const text = JSON.stringify(v, null, indent).replace(/\n/g, eol);
|
|
124
|
+
return /\r?\n$/.test(existing) ? text + eol : text;
|
|
125
|
+
};
|
|
126
|
+
if (serialize(parsed) !== existing)
|
|
127
|
+
return { outcome: "skipped", reason: "rewriting it would change its formatting" };
|
|
128
|
+
for (let depth = keys.length; depth > 0; depth--) {
|
|
129
|
+
const parent = (depth === 1 ? parsed : getPath(parsed, keys.slice(0, depth - 1)));
|
|
130
|
+
const key = keys[depth - 1];
|
|
131
|
+
if (depth < keys.length && (depth <= keep || Object.keys(parent[key]).length > 0))
|
|
132
|
+
break;
|
|
133
|
+
delete parent[key];
|
|
134
|
+
}
|
|
135
|
+
return { outcome: "updated", content: serialize(parsed), empty: Object.keys(parsed).length === 0 };
|
|
136
|
+
}
|
|
84
137
|
function detectIndent(text) {
|
|
85
138
|
const match = /^\{\r?\n([ \t]+)"/.exec(text);
|
|
86
139
|
if (!match)
|
package/dist/mcp.js
CHANGED
|
@@ -12,7 +12,10 @@ const INSTRUCTIONS = "Tools for one Cavelon instance, acting with the token a pe
|
|
|
12
12
|
"init and pull write files in the solution folder without confirm (pull refuses to replace package files with uncommitted changes unless force), " +
|
|
13
13
|
"and the other changing tools act at once. api refuses, even with confirm, an operation the instance marks for a person " +
|
|
14
14
|
"(x-cavelon-person-only; its reason is in the error), or on an instance that marks none, one that changes a secret, " +
|
|
15
|
-
"creates or revokes a credential (tokens, API keys, sign-in) or decides an approval
|
|
15
|
+
"creates or revokes a credential (tokens, API keys, sign-in) or decides an approval; it also refuses a body that sets a field " +
|
|
16
|
+
"the instance marks as a secret value (x-cavelon-secret): leave the field out and let a person enter the value. " +
|
|
17
|
+
"`cavelon api` run from your shell applies the same guards, and sends a changing operation only with --confirm and the token " +
|
|
18
|
+
"its preview printed. Tools read and write files only " +
|
|
16
19
|
"inside the solution folder (the folder of cavelon.yaml, or the one the server started in), never in cavelon's own " +
|
|
17
20
|
"config or cache directory. Read limits before planning a solution: it lists what the " +
|
|
18
21
|
"instance allows this tenant (upload sizes and types, run and tool limits, timeouts, quotas) and who changes each. " +
|
|
@@ -43,7 +46,7 @@ export function inputSchema(spec) {
|
|
|
43
46
|
if (option.cliOnly)
|
|
44
47
|
continue;
|
|
45
48
|
properties[name] =
|
|
46
|
-
option.type === "boolean"
|
|
49
|
+
option.type === "boolean" || option.mcpBoolean
|
|
47
50
|
? { type: "boolean", description: option.description }
|
|
48
51
|
: option.multiple
|
|
49
52
|
? { type: "array", items: { type: "string" }, description: option.description }
|
|
@@ -83,7 +86,7 @@ function inputFrom(spec, args) {
|
|
|
83
86
|
const value = args[name];
|
|
84
87
|
if (value === undefined || value === null)
|
|
85
88
|
continue;
|
|
86
|
-
if (option.type === "boolean")
|
|
89
|
+
if (option.type === "boolean" || option.mcpBoolean)
|
|
87
90
|
input.options[name] = value === true || value === "true";
|
|
88
91
|
else if (option.multiple)
|
|
89
92
|
input.options[name] = Array.isArray(value) ? value.map(String) : [String(value)];
|
package/dist/openapi.js
CHANGED
|
@@ -174,6 +174,50 @@ export function validateBody(doc, op, body) {
|
|
|
174
174
|
throw validationError(`The body does not match ${op.alias}'s schema: ${errors.join("; ")}`, errors, `See \`cavelon api describe ${op.alias}\`.`);
|
|
175
175
|
}
|
|
176
176
|
}
|
|
177
|
+
export function secretFields(doc, schema, body) {
|
|
178
|
+
const found = new Set();
|
|
179
|
+
let budget = 10_000;
|
|
180
|
+
const list = (value) => (Array.isArray(value) ? value : []);
|
|
181
|
+
const walk = (raw, value, where, depth) => {
|
|
182
|
+
if (value === undefined || value === null || depth > 64 || --budget < 0)
|
|
183
|
+
return;
|
|
184
|
+
const node = deref(doc, raw);
|
|
185
|
+
if (!node || typeof node !== "object")
|
|
186
|
+
return;
|
|
187
|
+
const s = node;
|
|
188
|
+
if (s["x-cavelon-secret"] === true) {
|
|
189
|
+
found.add(where || "(body)");
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
for (const key of ["allOf", "anyOf", "oneOf"])
|
|
193
|
+
for (const sub of list(s[key]))
|
|
194
|
+
walk(sub, value, where, depth + 1);
|
|
195
|
+
if (Array.isArray(value)) {
|
|
196
|
+
const prefix = list(s.prefixItems);
|
|
197
|
+
value.forEach((item, i) => walk(prefix[i] ?? s.items, item, `${where}[${i}]`, depth + 1));
|
|
198
|
+
}
|
|
199
|
+
else if (typeof value === "object") {
|
|
200
|
+
const props = (s.properties ?? {});
|
|
201
|
+
const patterns = Object.entries((s.patternProperties ?? {}));
|
|
202
|
+
for (const [key, item] of Object.entries(value)) {
|
|
203
|
+
const named = Object.hasOwn(props, key) ? [props[key]] : patterns.filter(([pattern]) => matches(pattern, key)).map(([, sub]) => sub);
|
|
204
|
+
const subs = named.length ? named : [s.additionalProperties];
|
|
205
|
+
for (const sub of subs)
|
|
206
|
+
walk(sub, item, where ? `${where}.${key}` : key, depth + 1);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
walk(schema, body, "", 0);
|
|
211
|
+
return [...found];
|
|
212
|
+
}
|
|
213
|
+
function matches(pattern, key) {
|
|
214
|
+
try {
|
|
215
|
+
return new RegExp(pattern, "u").test(key);
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
177
221
|
export function coerceParameter(param, raw) {
|
|
178
222
|
const types = schemaTypes(param.schema);
|
|
179
223
|
if (types.includes("integer") && /^-?\d+$/.test(raw))
|