@cavelon/cli 0.1.15 → 0.1.17
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 +44 -1
- package/dist/agent-env.js +3 -0
- package/dist/agents.js +16 -7
- package/dist/approval-policy.js +1 -0
- package/dist/commands/db.js +6 -2
- package/dist/commands/init.js +46 -10
- package/dist/commands/limits-set.js +11 -1
- package/dist/commands/limits.js +7 -1
- package/dist/commands/session.js +3 -0
- package/dist/commands/setup.js +18 -5
- package/dist/confirm-token.js +2 -1
- package/dist/database-limits.js +46 -0
- package/dist/fsutil.js +24 -7
- package/dist/jsonc-config.js +223 -0
- package/dist/kit-codes.js +1 -0
- package/dist/mcp-command.js +5 -0
- package/dist/mcp-directory.js +32 -0
- package/dist/mcp-entry.js +21 -0
- package/dist/mcp.js +18 -2
- package/dist/native-approval/client.js +126 -0
- package/dist/native-approval/ipc.js +191 -0
- package/dist/native-approval/kilo-server.js +33 -0
- package/dist/native-approval/kilo-tui.js +13 -0
- package/dist/native-approval/omp-extension.js +8 -0
- package/dist/native-approval/opencode-server.js +74 -0
- package/dist/native-approval/opencode-tui.js +143 -0
- package/dist/native-approval/pi-extension.js +86 -0
- package/dist/native-approval/preview-pages.js +18 -0
- package/dist/native-approval/profile.js +120 -0
- package/dist/native-assets/JSONC-LICENSE +21 -0
- package/dist/native-assets/LICENSE +201 -0
- package/dist/native-assets/MCP-SDK-LICENSE +21 -0
- package/dist/native-assets/THIRD-PARTY-NOTICES +386 -0
- package/dist/native-assets/TYPEBOX-LICENSE +23 -0
- package/dist/native-assets/ZOD-LICENSE +21 -0
- package/dist/native-assets/kilo-server.mjs +39719 -0
- package/dist/native-assets/kilo-tui.mjs +26477 -0
- package/dist/native-assets/manifest.json +82 -0
- package/dist/native-assets/omp-extension.mjs +30729 -0
- package/dist/native-assets/opencode-server.mjs +39692 -0
- package/dist/native-assets/opencode-tui.mjs +26474 -0
- package/dist/native-assets/pi-extension.mjs +30726 -0
- package/dist/native-assets.js +44 -0
- package/dist/native-clients.js +397 -0
- package/dist/native-install.js +390 -0
- package/dist/omp-config.js +85 -0
- package/dist/printed.js +17 -5
- package/dist/setup-agents.js +82 -8
- package/dist/skills/cavelon-loop/SKILL.md +7 -0
- package/dist/yaml-config.js +215 -0
- package/package.json +7 -3
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { startOpenCodeServer } from "./opencode-server.js";
|
|
3
|
+
import { loadNativeRuntime, nativeEntryHash } from "./profile.js";
|
|
4
|
+
export async function startKiloServer(input, options) {
|
|
5
|
+
const server = await startOpenCodeServer(input, options, "kilo");
|
|
6
|
+
let effective;
|
|
7
|
+
return {
|
|
8
|
+
...server,
|
|
9
|
+
async config(config) { effective = config; },
|
|
10
|
+
tool: Object.fromEntries(Object.entries(server.tool).map(([name, tool]) => [name, {
|
|
11
|
+
...tool,
|
|
12
|
+
async execute(...args) {
|
|
13
|
+
const entry = effective?.mcp?.cavelon;
|
|
14
|
+
if (options.entryHash && (!entry || nativeEntryHash(entry) !== options.entryHash)) {
|
|
15
|
+
throw new Error("Kilo's effective Cavelon configuration differs from the owned entry; review its project, environment and managed policies before using the tools.");
|
|
16
|
+
}
|
|
17
|
+
return tool.execute(...args);
|
|
18
|
+
},
|
|
19
|
+
}])),
|
|
20
|
+
async "shell.env"(_input, output) {
|
|
21
|
+
output.env.CAVELON_AGENT = "1";
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export default {
|
|
26
|
+
id: "cavelon.native-approval.kilo",
|
|
27
|
+
async server(input, options) {
|
|
28
|
+
const profile = options?.profile === undefined ? fileURLToPath(new URL("./profile.json", import.meta.url)) : options.profile;
|
|
29
|
+
if (typeof profile !== "string")
|
|
30
|
+
throw new Error("Cavelon native approval needs its setup profile.");
|
|
31
|
+
return startKiloServer(input, await loadNativeRuntime(profile, "kilo"));
|
|
32
|
+
},
|
|
33
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { startOpenCodeTui } from "./opencode-tui.js";
|
|
3
|
+
import { loadNativeRuntime } from "./profile.js";
|
|
4
|
+
export const startKiloTui = (api, options) => startOpenCodeTui(api, options, "kilo");
|
|
5
|
+
export default {
|
|
6
|
+
id: "cavelon.native-approval.kilo",
|
|
7
|
+
async tui(api, options) {
|
|
8
|
+
const profile = options?.profile === undefined ? fileURLToPath(new URL("./profile.json", import.meta.url)) : options.profile;
|
|
9
|
+
if (typeof profile !== "string")
|
|
10
|
+
throw new Error("Cavelon native approval needs its setup profile.");
|
|
11
|
+
await startKiloTui(api, await loadNativeRuntime(profile, "kilo"));
|
|
12
|
+
},
|
|
13
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { installPiExtension } from "./pi-extension.js";
|
|
3
|
+
import { loadNativeRuntime } from "./profile.js";
|
|
4
|
+
export default async function cavelon(omp) {
|
|
5
|
+
const runtime = await loadNativeRuntime(fileURLToPath(new URL("./profile.json", import.meta.url)), "omp");
|
|
6
|
+
process.env.CAVELON_AGENT = "1";
|
|
7
|
+
installPiExtension(omp, runtime, "omp");
|
|
8
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { nativeStdioClient } from "./client.js";
|
|
4
|
+
import { callUi, NativeUiUnavailable } from "./ipc.js";
|
|
5
|
+
import { assertNativeWorkspace, hasNativeProjectOwner, loadNativeRuntime } from "./profile.js";
|
|
6
|
+
import { PERSON_WAIT_MS } from "../approval-policy.js";
|
|
7
|
+
function toolArguments(input) {
|
|
8
|
+
const schema = z.fromJSONSchema(input);
|
|
9
|
+
if (!(schema instanceof z.ZodObject))
|
|
10
|
+
throw new Error("A Cavelon tool must declare an object input schema.");
|
|
11
|
+
return schema.shape;
|
|
12
|
+
}
|
|
13
|
+
export async function startOpenCodeServer(input, options, client = "opencode") {
|
|
14
|
+
await assertNativeWorkspace(options, input.directory);
|
|
15
|
+
if (await hasNativeProjectOwner(options, client, input.directory))
|
|
16
|
+
return { dispose: async () => undefined, tool: {} };
|
|
17
|
+
const command = { ...options.command, cwd: input.directory };
|
|
18
|
+
const discovery = await nativeStdioClient(command, false, options.version);
|
|
19
|
+
const tools = await discovery.tools().finally(() => discovery.close());
|
|
20
|
+
let fallback;
|
|
21
|
+
let disposed = false;
|
|
22
|
+
const headless = async () => {
|
|
23
|
+
if (disposed)
|
|
24
|
+
throw new Error("Cavelon adapter closed.");
|
|
25
|
+
const previous = fallback;
|
|
26
|
+
if (previous && (await previous).closed && fallback === previous)
|
|
27
|
+
fallback = undefined;
|
|
28
|
+
if (!fallback) {
|
|
29
|
+
const opening = nativeStdioClient(command, false, options.version);
|
|
30
|
+
fallback = opening;
|
|
31
|
+
opening.catch(() => { if (fallback === opening)
|
|
32
|
+
fallback = undefined; });
|
|
33
|
+
}
|
|
34
|
+
return fallback;
|
|
35
|
+
};
|
|
36
|
+
const dispose = async () => {
|
|
37
|
+
disposed = true;
|
|
38
|
+
await (await fallback?.catch(() => undefined))?.close();
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
dispose,
|
|
42
|
+
tool: Object.fromEntries(tools.map(tool => [`cavelon_${tool.name}`, {
|
|
43
|
+
description: tool.description ?? tool.name,
|
|
44
|
+
args: toolArguments(tool.inputSchema),
|
|
45
|
+
async execute(args, context) {
|
|
46
|
+
if (disposed)
|
|
47
|
+
throw new Error("Cavelon adapter closed.");
|
|
48
|
+
try {
|
|
49
|
+
const result = await callUi(options.runtimeDir, context.sessionID, tool.name, args, context.abort, (options.waitMs ?? PERSON_WAIT_MS) + 15_000);
|
|
50
|
+
return { output: JSON.stringify(result), metadata: { isError: result.isError === true } };
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (error instanceof NativeUiUnavailable) {
|
|
54
|
+
const result = await (await headless()).call(tool.name, args, { signal: context.abort });
|
|
55
|
+
return { output: JSON.stringify(result), metadata: { isError: result.isError === true } };
|
|
56
|
+
}
|
|
57
|
+
return { output: JSON.stringify({ error: {
|
|
58
|
+
code: "native_ui_disconnected", message: "The native UI call failed. Inspect the instance before repeating this change.",
|
|
59
|
+
outcome_unknown: true, automatic_retry: false,
|
|
60
|
+
} }), metadata: { isError: true } };
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
}])),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export default {
|
|
67
|
+
id: "cavelon.native-approval",
|
|
68
|
+
async server(input, options) {
|
|
69
|
+
const profile = options?.profile === undefined ? fileURLToPath(new URL("./profile.json", import.meta.url)) : options.profile;
|
|
70
|
+
if (typeof profile !== "string")
|
|
71
|
+
throw new Error("Cavelon native approval needs its setup profile.");
|
|
72
|
+
return startOpenCodeServer(input, await loadNativeRuntime(profile, "opencode"));
|
|
73
|
+
},
|
|
74
|
+
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { nativeStdioClient } from "./client.js";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { startUiSocket } from "./ipc.js";
|
|
4
|
+
import { assertNativeWorkspace, hasNativeProjectOwner, loadNativeRuntime } from "./profile.js";
|
|
5
|
+
import { previewPages } from "./preview-pages.js";
|
|
6
|
+
export async function startOpenCodeTui(api, options, client = "opencode") {
|
|
7
|
+
await assertNativeWorkspace(options, api.state.path.directory);
|
|
8
|
+
if (await hasNativeProjectOwner(options, client, api.state.path.directory))
|
|
9
|
+
return;
|
|
10
|
+
const clients = new Map();
|
|
11
|
+
const pending = new Set();
|
|
12
|
+
const retiring = new Set();
|
|
13
|
+
let disposed = false;
|
|
14
|
+
const session = () => api.route.current.name === "session" ? api.route.current.params?.sessionID : undefined;
|
|
15
|
+
const retire = (id, opening) => {
|
|
16
|
+
if (clients.get(id) !== opening)
|
|
17
|
+
return;
|
|
18
|
+
clients.delete(id);
|
|
19
|
+
const closing = opening.then(client => client.close()).catch(() => undefined);
|
|
20
|
+
retiring.add(closing);
|
|
21
|
+
void closing.finally(() => retiring.delete(closing));
|
|
22
|
+
};
|
|
23
|
+
const reap = setInterval(() => {
|
|
24
|
+
for (const [id, opening] of clients)
|
|
25
|
+
if (id !== session())
|
|
26
|
+
retire(id, opening);
|
|
27
|
+
}, 100);
|
|
28
|
+
reap.unref();
|
|
29
|
+
const connect = async (id) => {
|
|
30
|
+
if (disposed)
|
|
31
|
+
throw new Error("Cavelon adapter closed.");
|
|
32
|
+
const previous = clients.get(id);
|
|
33
|
+
if (previous && (await previous).closed && clients.get(id) === previous)
|
|
34
|
+
clients.delete(id);
|
|
35
|
+
if (!clients.has(id)) {
|
|
36
|
+
const opening = nativeStdioClient({ ...options.command, cwd: api.state.path.directory }, true, options.version);
|
|
37
|
+
clients.set(id, opening);
|
|
38
|
+
opening.catch(() => { if (clients.get(id) === opening)
|
|
39
|
+
clients.delete(id); });
|
|
40
|
+
}
|
|
41
|
+
return clients.get(id);
|
|
42
|
+
};
|
|
43
|
+
const ask = (message, signal) => new Promise(resolve => {
|
|
44
|
+
if (signal.aborted || api.ui.dialog.open || disposed) {
|
|
45
|
+
resolve(false);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
let pages;
|
|
49
|
+
try {
|
|
50
|
+
pages = previewPages(message, api.renderer.width, api.renderer.height);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
resolve(false);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
let done = false;
|
|
57
|
+
let page = 0;
|
|
58
|
+
let replacing = false;
|
|
59
|
+
const finish = (answer, clear = true) => {
|
|
60
|
+
if (done)
|
|
61
|
+
return;
|
|
62
|
+
done = true;
|
|
63
|
+
signal.removeEventListener("abort", cancel);
|
|
64
|
+
if (clear)
|
|
65
|
+
api.ui.dialog.clear();
|
|
66
|
+
resolve(answer === true && !signal.aborted && !disposed);
|
|
67
|
+
};
|
|
68
|
+
const cancel = () => finish(false);
|
|
69
|
+
const closed = () => { if (!replacing)
|
|
70
|
+
finish(false, false); };
|
|
71
|
+
const show = () => {
|
|
72
|
+
if (signal.aborted) {
|
|
73
|
+
finish(false);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
replacing = true;
|
|
77
|
+
api.ui.dialog.replace(() => pages.length > 1 && page < pages.length
|
|
78
|
+
? api.ui.DialogAlert({ title: `Cavelon preview ${page + 1}/${pages.length}`, message: pages[page], onConfirm: () => {
|
|
79
|
+
replacing = true;
|
|
80
|
+
page++;
|
|
81
|
+
queueMicrotask(() => {
|
|
82
|
+
replacing = false;
|
|
83
|
+
if (done)
|
|
84
|
+
return;
|
|
85
|
+
if (api.ui.dialog.open)
|
|
86
|
+
finish(false, false);
|
|
87
|
+
else
|
|
88
|
+
show();
|
|
89
|
+
});
|
|
90
|
+
} })
|
|
91
|
+
: api.ui.DialogConfirm({
|
|
92
|
+
title: "Cavelon: approve this exact change",
|
|
93
|
+
message: pages.length === 1 ? message : `Approve the exact change shown in all ${pages.length} preview pages?`,
|
|
94
|
+
onConfirm: () => finish(true), onCancel: cancel,
|
|
95
|
+
}), closed);
|
|
96
|
+
api.ui.dialog.setSize("large");
|
|
97
|
+
replacing = false;
|
|
98
|
+
};
|
|
99
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
100
|
+
show();
|
|
101
|
+
});
|
|
102
|
+
const socket = await startUiSocket(options.runtimeDir, session, async (name, args, id, callerSignal) => {
|
|
103
|
+
const controller = new AbortController();
|
|
104
|
+
pending.add(controller);
|
|
105
|
+
const abort = () => controller.abort();
|
|
106
|
+
callerSignal.addEventListener("abort", abort, { once: true });
|
|
107
|
+
const timer = setInterval(() => { if (session() !== id)
|
|
108
|
+
controller.abort(); }, 100);
|
|
109
|
+
try {
|
|
110
|
+
if (callerSignal.aborted || session() !== id || disposed)
|
|
111
|
+
throw new Error("Native UI session changed.");
|
|
112
|
+
const bridge = await connect(id);
|
|
113
|
+
return await bridge.call(name, args, { signal: controller.signal, ask });
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
clearInterval(timer);
|
|
117
|
+
pending.delete(controller);
|
|
118
|
+
callerSignal.removeEventListener("abort", abort);
|
|
119
|
+
}
|
|
120
|
+
}).catch(error => { clearInterval(reap); throw error; });
|
|
121
|
+
let closing;
|
|
122
|
+
const close = () => closing ??= (async () => {
|
|
123
|
+
disposed = true;
|
|
124
|
+
clearInterval(reap);
|
|
125
|
+
for (const controller of pending)
|
|
126
|
+
controller.abort();
|
|
127
|
+
await socket.close();
|
|
128
|
+
await Promise.all([...clients.values()].map(async (opening) => (await opening.catch(() => undefined))?.close()));
|
|
129
|
+
await Promise.all(retiring);
|
|
130
|
+
clients.clear();
|
|
131
|
+
})();
|
|
132
|
+
api.lifecycle.onDispose(close);
|
|
133
|
+
return { close, descriptor: socket.descriptor };
|
|
134
|
+
}
|
|
135
|
+
export default {
|
|
136
|
+
id: "cavelon.native-approval",
|
|
137
|
+
async tui(api, options) {
|
|
138
|
+
const profile = options?.profile === undefined ? fileURLToPath(new URL("./profile.json", import.meta.url)) : options.profile;
|
|
139
|
+
if (typeof profile !== "string")
|
|
140
|
+
throw new Error("Cavelon native approval needs its setup profile.");
|
|
141
|
+
await startOpenCodeTui(api, await loadNativeRuntime(profile, "opencode"));
|
|
142
|
+
},
|
|
143
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Type } from "typebox";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { nativeStdioClient } from "./client.js";
|
|
4
|
+
import { assertNativeWorkspace, hasNativeProjectOwner, loadNativeRuntime } from "./profile.js";
|
|
5
|
+
import { previewPages } from "./preview-pages.js";
|
|
6
|
+
export function installPiExtension(pi, options, clientName = "pi") {
|
|
7
|
+
let opening;
|
|
8
|
+
let current;
|
|
9
|
+
let generation = 0;
|
|
10
|
+
const owned = new Set();
|
|
11
|
+
const close = async () => {
|
|
12
|
+
generation++;
|
|
13
|
+
current = undefined;
|
|
14
|
+
const previous = opening;
|
|
15
|
+
opening = undefined;
|
|
16
|
+
await (await previous?.catch(() => undefined))?.close();
|
|
17
|
+
};
|
|
18
|
+
const start = async (ctx) => {
|
|
19
|
+
if (options.scope === "project" && !ctx.isProjectTrusted()) {
|
|
20
|
+
throw new Error("Cavelon project integration requires the person's trust for this project.");
|
|
21
|
+
}
|
|
22
|
+
await assertNativeWorkspace(options, ctx.cwd);
|
|
23
|
+
const previous = opening;
|
|
24
|
+
if (previous && (await previous).closed && opening === previous)
|
|
25
|
+
opening = undefined;
|
|
26
|
+
if (!opening) {
|
|
27
|
+
const connection = nativeStdioClient({ ...options.command, cwd: ctx.cwd }, ctx.hasUI && ctx.mode === "tui", options.version);
|
|
28
|
+
opening = connection;
|
|
29
|
+
connection.catch(() => { if (opening === connection)
|
|
30
|
+
opening = undefined; });
|
|
31
|
+
}
|
|
32
|
+
return opening;
|
|
33
|
+
};
|
|
34
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
35
|
+
await close();
|
|
36
|
+
if (ctx.isProjectTrusted() && await hasNativeProjectOwner(options, clientName, ctx.cwd))
|
|
37
|
+
return;
|
|
38
|
+
current = ctx;
|
|
39
|
+
const epoch = generation;
|
|
40
|
+
const client = await start(ctx);
|
|
41
|
+
const tools = await client.tools();
|
|
42
|
+
if (epoch !== generation)
|
|
43
|
+
return;
|
|
44
|
+
const existing = new Set(pi.getAllTools().map(tool => tool.name));
|
|
45
|
+
if (tools.some(tool => existing.has(`mcp__cavelon__${tool.name}`) && !owned.has(`mcp__cavelon__${tool.name}`))) {
|
|
46
|
+
await close();
|
|
47
|
+
throw new Error("Cavelon tools already exist. Disable only the duplicate Cavelon MCP entry or replacement extension before loading native approval.");
|
|
48
|
+
}
|
|
49
|
+
for (const tool of tools) {
|
|
50
|
+
const name = `mcp__cavelon__${tool.name}`;
|
|
51
|
+
owned.add(name);
|
|
52
|
+
pi.registerTool({
|
|
53
|
+
name, label: tool.annotations?.title ?? tool.name, description: tool.description ?? tool.name,
|
|
54
|
+
parameters: Type.Unsafe(tool.inputSchema), annotations: tool.annotations, executionMode: "sequential",
|
|
55
|
+
...(clientName === "omp" ? { loadMode: "essential", mcpServerName: "cavelon", mcpToolName: tool.name } : {}),
|
|
56
|
+
async execute(_id, params, signal, _update, ctx) {
|
|
57
|
+
if (!current)
|
|
58
|
+
throw new Error("The Cavelon native session has ended.");
|
|
59
|
+
const callGeneration = generation;
|
|
60
|
+
const result = await (await start(ctx)).call(tool.name, params, {
|
|
61
|
+
signal,
|
|
62
|
+
ask: ctx.hasUI && ctx.mode === "tui" ? async (message, dialogSignal, timeout) => {
|
|
63
|
+
const pages = previewPages(message, process.stdout.columns || 80, process.stdout.rows || 24);
|
|
64
|
+
if (pages.length > 1)
|
|
65
|
+
for (let page = 0; page < pages.length; page++) {
|
|
66
|
+
if (dialogSignal.aborted || callGeneration !== generation)
|
|
67
|
+
return false;
|
|
68
|
+
if (await ctx.ui.confirm(`Cavelon preview ${page + 1}/${pages.length}: continue`, pages[page], { signal: dialogSignal, timeout }) !== true)
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
if (dialogSignal.aborted || callGeneration !== generation)
|
|
72
|
+
return false;
|
|
73
|
+
return ctx.ui.confirm("Cavelon: approve this exact change", pages.length === 1 ? message : `Approve the exact change shown in all ${pages.length} preview pages?`, { signal: dialogSignal, timeout });
|
|
74
|
+
} : undefined,
|
|
75
|
+
});
|
|
76
|
+
return { content: result.content, details: { isError: result.isError === true },
|
|
77
|
+
...(clientName === "omp" ? { isError: result.isError === true } : {}) };
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
pi.on("session_shutdown", close);
|
|
83
|
+
}
|
|
84
|
+
export default async function cavelon(pi) {
|
|
85
|
+
installPiExtension(pi, await loadNativeRuntime(fileURLToPath(new URL("./profile.json", import.meta.url)), "pi"));
|
|
86
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function previewPages(message, columns, rows) {
|
|
2
|
+
const width = Math.min(60, Math.floor((columns - 12) / 2));
|
|
3
|
+
const height = Math.min(18, rows - 12);
|
|
4
|
+
if (width < 12 || height < 3)
|
|
5
|
+
throw new Error("Enlarge the terminal before reviewing a Cavelon change.");
|
|
6
|
+
const lines = [];
|
|
7
|
+
for (const line of message.split("\n")) {
|
|
8
|
+
const characters = [...line];
|
|
9
|
+
if (!characters.length)
|
|
10
|
+
lines.push("");
|
|
11
|
+
for (let offset = 0; offset < characters.length; offset += width)
|
|
12
|
+
lines.push(characters.slice(offset, offset + width).join(""));
|
|
13
|
+
}
|
|
14
|
+
const pages = [];
|
|
15
|
+
for (let offset = 0; offset < lines.length; offset += height)
|
|
16
|
+
pages.push(lines.slice(offset, offset + height).join("\n"));
|
|
17
|
+
return pages;
|
|
18
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { ompRuntimePolicy } from "../omp-config.js";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { promises as fs } from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { readJsoncEntry } from "../jsonc-config.js";
|
|
7
|
+
function canonical(value) {
|
|
8
|
+
if (Array.isArray(value))
|
|
9
|
+
return `[${value.map(canonical).join(",")}]`;
|
|
10
|
+
if (value && typeof value === "object") {
|
|
11
|
+
const keys = Object.keys(value).toSorted((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
12
|
+
const fields = keys.map(key => JSON.stringify(key) + ":" + canonical(value[key]));
|
|
13
|
+
return "{" + fields.join(",") + "}";
|
|
14
|
+
}
|
|
15
|
+
return JSON.stringify(value);
|
|
16
|
+
}
|
|
17
|
+
export function nativeEntryHash(entry) {
|
|
18
|
+
return createHash("sha256").update(canonical(entry)).digest("hex");
|
|
19
|
+
}
|
|
20
|
+
export function nativeRuntimeDirectory(profileFile) {
|
|
21
|
+
const identity = process.getuid?.().toString() ?? os.userInfo().username;
|
|
22
|
+
const suffix = createHash("sha256").update(`${identity}\n${path.resolve(profileFile)}`).digest("hex").slice(0, 20);
|
|
23
|
+
const temp = process.platform !== "win32" && Buffer.byteLength(os.tmpdir()) > 40 ? "/tmp" : os.tmpdir();
|
|
24
|
+
return path.join(temp, `cavelon-native-${suffix}`);
|
|
25
|
+
}
|
|
26
|
+
export async function assertNativeWorkspace(runtime, directory) {
|
|
27
|
+
if (runtime.scope !== "project")
|
|
28
|
+
return;
|
|
29
|
+
if (!runtime.projectRoot || await fs.realpath(directory) !== await fs.realpath(runtime.projectRoot)) {
|
|
30
|
+
throw new Error("Cavelon project integration requires its matching project workspace.");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export async function hasNativeProjectOwner(runtime, client, directory) {
|
|
34
|
+
if (runtime.scope !== "user")
|
|
35
|
+
return false;
|
|
36
|
+
const file = path.join(directory, `.${client}`, "cavelon", "profile.json");
|
|
37
|
+
const present = await fs.stat(file).then(() => true, error => { if (error.code === "ENOENT")
|
|
38
|
+
return false; throw error; });
|
|
39
|
+
if (!present)
|
|
40
|
+
return false;
|
|
41
|
+
const project = await loadNativeRuntime(file, client);
|
|
42
|
+
if (project.scope !== "project")
|
|
43
|
+
throw new Error("The project native profile does not own this project.");
|
|
44
|
+
await assertNativeWorkspace(project, directory);
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
function projectRootValid(profile) {
|
|
48
|
+
if (profile.format === 2)
|
|
49
|
+
return profile.scope === "project" && relativePath(profile.projectRoot);
|
|
50
|
+
return profile.scope !== "project" || (typeof profile.projectRoot === "string" && path.isAbsolute(profile.projectRoot));
|
|
51
|
+
}
|
|
52
|
+
function relativePath(value) {
|
|
53
|
+
return typeof value === "string" && Boolean(value.trim()) && !/[\0\\:]/.test(value) && !path.isAbsolute(value);
|
|
54
|
+
}
|
|
55
|
+
function inside(root, file) {
|
|
56
|
+
const relative = path.relative(root, file);
|
|
57
|
+
return relative === "" || (relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative));
|
|
58
|
+
}
|
|
59
|
+
export async function loadNativeRuntime(profileFile, client) {
|
|
60
|
+
let profile;
|
|
61
|
+
const profileText = await fs.readFile(profileFile, "utf8");
|
|
62
|
+
try {
|
|
63
|
+
profile = JSON.parse(profileText);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
throw new Error("Invalid Cavelon native profile; review it and repeat setup.");
|
|
67
|
+
}
|
|
68
|
+
if (!profile || ![undefined, 2].includes(profile.format)
|
|
69
|
+
|| profile.client !== client || typeof profile.version !== "string" || !/^\d+\.\d+\.\d+(?:-[\w.-]+)?$/.test(profile.version)
|
|
70
|
+
|| (profile.format === 2 ? !relativePath(profile.configFile) : typeof profile.configFile !== "string" || !path.isAbsolute(profile.configFile))
|
|
71
|
+
|| typeof profile.entryHash !== "string" || !/^[a-f0-9]{64}$/.test(profile.entryHash)
|
|
72
|
+
|| !["user", "project"].includes(profile.scope)
|
|
73
|
+
|| !projectRootValid(profile)) {
|
|
74
|
+
throw new Error("Invalid Cavelon native profile; repeat cavelon setup or init.");
|
|
75
|
+
}
|
|
76
|
+
const base = path.dirname(await fs.realpath(profileFile));
|
|
77
|
+
const projectRoot = profile.scope === "project" ? await fs.realpath(path.resolve(base, profile.projectRoot)) : undefined;
|
|
78
|
+
const configFile = path.resolve(base, profile.configFile);
|
|
79
|
+
if (profile.format === 2 && !inside(projectRoot, configFile))
|
|
80
|
+
throw new Error("Cavelon native configuration is outside its project.");
|
|
81
|
+
const realConfig = await fs.realpath(configFile);
|
|
82
|
+
if (profile.format === 2 && !inside(projectRoot, realConfig))
|
|
83
|
+
throw new Error("Cavelon native configuration is outside its project.");
|
|
84
|
+
const text = await fs.readFile(realConfig, "utf8");
|
|
85
|
+
const commandArgs = client === "pi" || client === "omp";
|
|
86
|
+
if (commandArgs) {
|
|
87
|
+
try {
|
|
88
|
+
JSON.parse(text);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
throw new Error(`The ${client === "pi" ? "Pi" : "OMP"} MCP configuration requires plain JSON; review it before native startup.`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (client === "omp") {
|
|
95
|
+
const policy = await ompRuntimePolicy(configFile, process.env);
|
|
96
|
+
if (policy)
|
|
97
|
+
throw new Error(policy);
|
|
98
|
+
}
|
|
99
|
+
const selected = readJsoncEntry(text, [commandArgs ? "mcpServers" : "mcp", "cavelon"]);
|
|
100
|
+
if ("error" in selected || !selected.value || nativeEntryHash(selected.value) !== profile.entryHash) {
|
|
101
|
+
throw new Error("The Cavelon MCP entry changed; review it and repeat setup before using native approval.");
|
|
102
|
+
}
|
|
103
|
+
const entry = selected.value;
|
|
104
|
+
if (entry.enabled !== false || (!commandArgs && entry.type !== "local")) {
|
|
105
|
+
throw new Error("Native approval must own only Cavelon; disable its duplicate built-in MCP entry.");
|
|
106
|
+
}
|
|
107
|
+
const words = !commandArgs ? entry.command : [entry.command, ...(Array.isArray(entry.args) ? entry.args : [])];
|
|
108
|
+
if (!Array.isArray(words) || !words.length || !words.every(word => typeof word === "string") || !words[0]
|
|
109
|
+
|| (commandArgs && !Array.isArray(entry.args)))
|
|
110
|
+
throw new Error("Invalid Cavelon MCP process command.");
|
|
111
|
+
const overrides = entry[!commandArgs ? "environment" : "env"];
|
|
112
|
+
if (overrides !== undefined && (!overrides || typeof overrides !== "object" || Array.isArray(overrides)
|
|
113
|
+
|| Object.values(overrides).some(value => typeof value !== "string")))
|
|
114
|
+
throw new Error("Invalid Cavelon MCP environment.");
|
|
115
|
+
const env = Object.fromEntries(Object.entries({ ...process.env, ...overrides }).filter((entry) => typeof entry[1] === "string"));
|
|
116
|
+
return {
|
|
117
|
+
version: profile.version, scope: profile.scope, entryHash: profile.entryHash, ...(projectRoot ? { projectRoot } : {}),
|
|
118
|
+
runtimeDir: nativeRuntimeDirectory(profileFile), command: { command: words[0], args: words.slice(1), env },
|
|
119
|
+
};
|
|
120
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) Microsoft
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|