@caveman-ai/cli 4.1.0 → 4.2.0
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/dist/binaries.generated.js +1 -1
- package/dist/caveman-pi-extension.mjs +272 -3
- package/dist/codemode.js +1402 -0
- package/dist/index.js +268 -71
- package/dist/provider-routing.js +1 -1
- package/dist/reserved-verbs.generated.js +0 -1
- package/dist/verified-methods.mirror.js +3 -3
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// Generated by scripts/gen-binaries.mjs. Do not edit.
|
|
2
|
-
export const BINARY_RELEASE = "bin-v4.
|
|
2
|
+
export const BINARY_RELEASE = "bin-v4.2.0";
|
|
3
3
|
export const BINARY_RELEASE_BASE_DEFAULT = "https://github.com/JuliusBrussee/caveman/releases/download";
|
|
4
4
|
export const BINARY_SIGNING_PUBKEY = "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEKR5zq0dz0mTUtkiX0b6jqtyG3uQV\n89PGD2n9UBV1ikbhu0f1c+vHtcN9mk6wKzyBLdEPudI/Jnvci+8OAen/vw==\n-----END PUBLIC KEY-----\n";
|
|
@@ -76,7 +76,7 @@ function verifiedProviderRoute(gateway, api, provider, originalBaseUrl, publishe
|
|
|
76
76
|
}
|
|
77
77
|
var COPIED_HEADERS = ["content-type", "content-encoding", "accept", "accept-encoding", "idempotency-key", "openai-organization", "openai-project", "session_id", "x-session-id", "x-client-request-id", "x-session-affinity", "anthropic-version", "anthropic-beta", "api-version", "user-agent"];
|
|
78
78
|
var COMPAT_HEADERS = {
|
|
79
|
-
openrouter: ["http-referer", "x-openrouter-title", "x-openrouter-categories"],
|
|
79
|
+
openrouter: ["http-referer", "x-title", "x-openrouter-title", "x-openrouter-categories"],
|
|
80
80
|
nvidia: ["x-billing-invoke-origin"],
|
|
81
81
|
opencode: ["x-opencode-session", "x-opencode-client", "x-opencode-project", "x-opencode-request"],
|
|
82
82
|
"opencode-go": ["x-opencode-session", "x-opencode-client", "x-opencode-project", "x-opencode-request"]
|
|
@@ -830,7 +830,7 @@ var RecoveryClient = class {
|
|
|
830
830
|
await this.call("initialize", {
|
|
831
831
|
protocolVersion: "2024-11-05",
|
|
832
832
|
capabilities: {},
|
|
833
|
-
clientInfo: { name: "caveman-pi-extension", version: "0.
|
|
833
|
+
clientInfo: { name: "caveman-pi-extension", version: "0.4.0" }
|
|
834
834
|
}, void 0, INIT_TIMEOUT_MS);
|
|
835
835
|
this.notify("notifications/initialized");
|
|
836
836
|
if (this.disposed) {
|
|
@@ -946,6 +946,218 @@ var RecoveryClient = class {
|
|
|
946
946
|
}
|
|
947
947
|
}
|
|
948
948
|
};
|
|
949
|
+
var CodemodeClient = class {
|
|
950
|
+
binary;
|
|
951
|
+
extraArgs;
|
|
952
|
+
child;
|
|
953
|
+
initialized = false;
|
|
954
|
+
ensuring;
|
|
955
|
+
nextId = 1;
|
|
956
|
+
pending = /* @__PURE__ */ new Map();
|
|
957
|
+
buffer = "";
|
|
958
|
+
disposed = false;
|
|
959
|
+
constructor(binary = resolveMcpBinary(), extraArgs = defaultCodemodeArgs()) {
|
|
960
|
+
this.binary = binary;
|
|
961
|
+
this.extraArgs = extraArgs;
|
|
962
|
+
}
|
|
963
|
+
async ensure() {
|
|
964
|
+
if (this.disposed || !this.binary) return false;
|
|
965
|
+
if (this.child && this.initialized) return true;
|
|
966
|
+
this.ensuring ??= this.start().finally(() => {
|
|
967
|
+
this.ensuring = void 0;
|
|
968
|
+
});
|
|
969
|
+
return this.ensuring;
|
|
970
|
+
}
|
|
971
|
+
async callTool(name, args, signal) {
|
|
972
|
+
if (!await this.ensure()) {
|
|
973
|
+
return { text: JSON.stringify({ error: "cave_codemode_unavailable", message: "caveman-mcp codemode is not available this session" }), isError: true };
|
|
974
|
+
}
|
|
975
|
+
try {
|
|
976
|
+
const timeoutMs = codemodeCallTimeoutMs(name, args);
|
|
977
|
+
const result = await this.call("tools/call", { name, arguments: args }, signal, timeoutMs);
|
|
978
|
+
const text = (result.content ?? []).map((block) => block.type === "text" && typeof block.text === "string" ? block.text : "").join("");
|
|
979
|
+
return { text, isError: Boolean(result.isError) };
|
|
980
|
+
} catch (error) {
|
|
981
|
+
return { text: JSON.stringify({ error: "cave_codemode_transport", message: error.message }), isError: true };
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
dispose() {
|
|
985
|
+
this.disposed = true;
|
|
986
|
+
const child = this.child;
|
|
987
|
+
this.child = void 0;
|
|
988
|
+
this.initialized = false;
|
|
989
|
+
for (const [, entry] of this.pending) {
|
|
990
|
+
clearTimeout(entry.timer);
|
|
991
|
+
entry.reject(new Error("codemode client disposed"));
|
|
992
|
+
}
|
|
993
|
+
this.pending.clear();
|
|
994
|
+
if (child) {
|
|
995
|
+
try {
|
|
996
|
+
child.stdin?.end();
|
|
997
|
+
} catch {
|
|
998
|
+
}
|
|
999
|
+
const term = setTimeout(() => {
|
|
1000
|
+
try {
|
|
1001
|
+
child.kill("SIGTERM");
|
|
1002
|
+
} catch {
|
|
1003
|
+
}
|
|
1004
|
+
}, 500);
|
|
1005
|
+
const kill = setTimeout(() => {
|
|
1006
|
+
try {
|
|
1007
|
+
child.kill("SIGKILL");
|
|
1008
|
+
} catch {
|
|
1009
|
+
}
|
|
1010
|
+
}, 2e3);
|
|
1011
|
+
child.once("exit", () => {
|
|
1012
|
+
clearTimeout(term);
|
|
1013
|
+
clearTimeout(kill);
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
async start() {
|
|
1018
|
+
let child;
|
|
1019
|
+
try {
|
|
1020
|
+
const prefixArgs = codemodePrefixArgs();
|
|
1021
|
+
const portable = portableInvocation(this.binary, [...prefixArgs, "codemode", ...this.extraArgs]);
|
|
1022
|
+
child = spawn(portable.command, portable.args, { stdio: ["pipe", "pipe", "ignore"], windowsHide: true });
|
|
1023
|
+
} catch {
|
|
1024
|
+
return false;
|
|
1025
|
+
}
|
|
1026
|
+
this.child = child;
|
|
1027
|
+
this.initialized = false;
|
|
1028
|
+
this.buffer = "";
|
|
1029
|
+
child.on("error", () => this.onChildGone(child));
|
|
1030
|
+
child.on("exit", () => this.onChildGone(child));
|
|
1031
|
+
child.stdin?.on("error", () => this.onChildGone(child));
|
|
1032
|
+
child.stdout?.setEncoding("utf8");
|
|
1033
|
+
child.stdout?.on("data", (chunk) => this.onData(chunk));
|
|
1034
|
+
try {
|
|
1035
|
+
await this.call("initialize", {
|
|
1036
|
+
protocolVersion: "2024-11-05",
|
|
1037
|
+
capabilities: {},
|
|
1038
|
+
clientInfo: { name: "caveman-pi-codemode", version: "0.4.0" }
|
|
1039
|
+
}, void 0, CALL_TIMEOUT_MS);
|
|
1040
|
+
try {
|
|
1041
|
+
this.child?.stdin?.write(JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" }) + "\n");
|
|
1042
|
+
} catch {
|
|
1043
|
+
}
|
|
1044
|
+
if (this.disposed) {
|
|
1045
|
+
this.onChildGone(child);
|
|
1046
|
+
try {
|
|
1047
|
+
child.kill("SIGTERM");
|
|
1048
|
+
} catch {
|
|
1049
|
+
}
|
|
1050
|
+
return false;
|
|
1051
|
+
}
|
|
1052
|
+
this.initialized = true;
|
|
1053
|
+
return true;
|
|
1054
|
+
} catch {
|
|
1055
|
+
this.onChildGone(child);
|
|
1056
|
+
try {
|
|
1057
|
+
child.kill("SIGTERM");
|
|
1058
|
+
} catch {
|
|
1059
|
+
}
|
|
1060
|
+
return false;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
onChildGone(child) {
|
|
1064
|
+
if (this.child !== child) return;
|
|
1065
|
+
this.child = void 0;
|
|
1066
|
+
this.initialized = false;
|
|
1067
|
+
for (const [, entry] of this.pending) {
|
|
1068
|
+
clearTimeout(entry.timer);
|
|
1069
|
+
entry.reject(new Error("caveman-mcp codemode exited"));
|
|
1070
|
+
}
|
|
1071
|
+
this.pending.clear();
|
|
1072
|
+
}
|
|
1073
|
+
onData(chunk) {
|
|
1074
|
+
this.buffer += chunk;
|
|
1075
|
+
let newline;
|
|
1076
|
+
while ((newline = this.buffer.indexOf("\n")) !== -1) {
|
|
1077
|
+
const line = this.buffer.slice(0, newline).trim();
|
|
1078
|
+
this.buffer = this.buffer.slice(newline + 1);
|
|
1079
|
+
if (!line) continue;
|
|
1080
|
+
let message;
|
|
1081
|
+
try {
|
|
1082
|
+
message = JSON.parse(line);
|
|
1083
|
+
} catch {
|
|
1084
|
+
continue;
|
|
1085
|
+
}
|
|
1086
|
+
if (typeof message.id !== "number") continue;
|
|
1087
|
+
const entry = this.pending.get(message.id);
|
|
1088
|
+
if (!entry) continue;
|
|
1089
|
+
this.pending.delete(message.id);
|
|
1090
|
+
clearTimeout(entry.timer);
|
|
1091
|
+
if (message.error) entry.reject(new Error(message.error.message || "caveman-mcp codemode error"));
|
|
1092
|
+
else entry.resolve(message.result);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
call(method, params, signal, timeoutMs = CALL_TIMEOUT_MS) {
|
|
1096
|
+
const child = this.child;
|
|
1097
|
+
if (!child?.stdin?.writable) return Promise.reject(new Error("caveman-mcp codemode not running"));
|
|
1098
|
+
if (signal?.aborted) return Promise.reject(new Error("codemode call cancelled"));
|
|
1099
|
+
const id = this.nextId++;
|
|
1100
|
+
return new Promise((resolve2, reject) => {
|
|
1101
|
+
const timer = setTimeout(() => {
|
|
1102
|
+
this.pending.delete(id);
|
|
1103
|
+
reject(new Error(`caveman-mcp codemode ${method} timed out`));
|
|
1104
|
+
}, timeoutMs);
|
|
1105
|
+
timer.unref?.();
|
|
1106
|
+
const onAbort = () => {
|
|
1107
|
+
this.pending.delete(id);
|
|
1108
|
+
clearTimeout(timer);
|
|
1109
|
+
reject(new Error("codemode call cancelled"));
|
|
1110
|
+
};
|
|
1111
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1112
|
+
this.pending.set(id, {
|
|
1113
|
+
resolve: (value) => {
|
|
1114
|
+
signal?.removeEventListener("abort", onAbort);
|
|
1115
|
+
resolve2(value);
|
|
1116
|
+
},
|
|
1117
|
+
reject: (error) => {
|
|
1118
|
+
signal?.removeEventListener("abort", onAbort);
|
|
1119
|
+
reject(error);
|
|
1120
|
+
},
|
|
1121
|
+
timer
|
|
1122
|
+
});
|
|
1123
|
+
child.stdin.write(JSON.stringify({ jsonrpc: "2.0", id, method, params }) + "\n", (error) => {
|
|
1124
|
+
if (error) {
|
|
1125
|
+
this.pending.delete(id);
|
|
1126
|
+
clearTimeout(timer);
|
|
1127
|
+
signal?.removeEventListener("abort", onAbort);
|
|
1128
|
+
reject(error);
|
|
1129
|
+
}
|
|
1130
|
+
});
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
1133
|
+
};
|
|
1134
|
+
function codemodeCallTimeoutMs(name, args) {
|
|
1135
|
+
const isExec = name === "codemode_execute" || name === "codemode_mutate";
|
|
1136
|
+
const raw = typeof args.timeout_ms === "number" && Number.isFinite(args.timeout_ms) && args.timeout_ms > 0 ? Math.min(args.timeout_ms, 3e5) : 0;
|
|
1137
|
+
const minExecMs = isExec ? 75e3 : CALL_TIMEOUT_MS;
|
|
1138
|
+
return Math.max(CALL_TIMEOUT_MS, minExecMs, raw > 0 ? raw + 15e3 : 0);
|
|
1139
|
+
}
|
|
1140
|
+
function codemodePrefixArgs(env = process.env) {
|
|
1141
|
+
const raw = env.CAVEMAN_MCP_PREFIX_ARGS?.trim();
|
|
1142
|
+
if (!raw) return [];
|
|
1143
|
+
try {
|
|
1144
|
+
const parsed = JSON.parse(raw);
|
|
1145
|
+
if (Array.isArray(parsed)) {
|
|
1146
|
+
return parsed.filter((item) => typeof item === "string");
|
|
1147
|
+
}
|
|
1148
|
+
} catch {
|
|
1149
|
+
}
|
|
1150
|
+
return [];
|
|
1151
|
+
}
|
|
1152
|
+
function defaultCodemodeArgs(env = process.env) {
|
|
1153
|
+
const args = [];
|
|
1154
|
+
const cfg = env.CAVEMAN_PI_CODEMODE_CONFIG?.trim();
|
|
1155
|
+
if (cfg) args.push("--config", cfg);
|
|
1156
|
+
else args.push("--discover");
|
|
1157
|
+
const ws = env.CAVEMAN_PI_CODEMODE_WORKSPACE?.trim();
|
|
1158
|
+
if (ws) args.push("--workspace", ws);
|
|
1159
|
+
return args;
|
|
1160
|
+
}
|
|
949
1161
|
|
|
950
1162
|
// ../pi-extension/src/tool-output.ts
|
|
951
1163
|
async function shrinkToolResult(bridge, sessionId, event, recovery, onNoReply) {
|
|
@@ -1058,6 +1270,8 @@ async function readLiveRunState(gateway) {
|
|
|
1058
1270
|
function index_default(pi) {
|
|
1059
1271
|
const bridge = new HookBridge();
|
|
1060
1272
|
const recovery = new RecoveryClient();
|
|
1273
|
+
const codemodeEnabled = process.env.CAVEMAN_PI_CODEMODE === "1";
|
|
1274
|
+
const codemode = codemodeEnabled ? new CodemodeClient() : void 0;
|
|
1061
1275
|
let router;
|
|
1062
1276
|
let sessionId = "default";
|
|
1063
1277
|
let coreContext;
|
|
@@ -1112,6 +1326,59 @@ function index_default(pi) {
|
|
|
1112
1326
|
return { content: [{ type: "text", text: result.text }], details: { recovery_handle: params.recovery_handle } };
|
|
1113
1327
|
}
|
|
1114
1328
|
});
|
|
1329
|
+
if (codemode) {
|
|
1330
|
+
pi.registerTool({
|
|
1331
|
+
name: "codemode_search_tools",
|
|
1332
|
+
label: "Search Code-Mode MCP tools",
|
|
1333
|
+
description: "Search connected MCP & built-in repo/pr/browse tools by keyword and return compact typed signatures plus read/write access tags.",
|
|
1334
|
+
parameters: Type.Object({
|
|
1335
|
+
query: Type.String({ description: "Keywords describing the action or resource." }),
|
|
1336
|
+
server: Type.Optional(Type.String({ description: "Optional MCP server filter." })),
|
|
1337
|
+
limit: Type.Optional(Type.Number({ description: "Max tools to return (default 8, max 25)." }))
|
|
1338
|
+
}),
|
|
1339
|
+
async execute(_toolCallId, params, signal) {
|
|
1340
|
+
const result = await codemode.callTool("codemode_search_tools", params, signal);
|
|
1341
|
+
if (result.isError) throw new Error(result.text || "codemode_search_tools failed");
|
|
1342
|
+
return { content: [{ type: "text", text: result.text }], details: { tool: "codemode_search_tools" } };
|
|
1343
|
+
}
|
|
1344
|
+
});
|
|
1345
|
+
pi.registerTool({
|
|
1346
|
+
name: "codemode_execute",
|
|
1347
|
+
label: "Run read-only Code-Mode pipeline",
|
|
1348
|
+
description: "Run a read-only multi-step sandboxed MCP pipeline (`steps`) or embedded QuickJS async JS snippet (`code`) in one turn. Large raw payloads are archived in CCR.",
|
|
1349
|
+
parameters: Type.Object({
|
|
1350
|
+
steps: Type.Optional(Type.Array(Type.Record(Type.String(), Type.Unknown()), { description: "Ordered read-only pipeline steps." })),
|
|
1351
|
+
code: Type.Optional(Type.String({ description: "Embedded QuickJS async JS body using `await mcp.server.tool(args)`." })),
|
|
1352
|
+
return_step: Type.Optional(Type.String({ description: "Step id to return in steps mode." })),
|
|
1353
|
+
max_output_chars: Type.Optional(Type.Number({ description: "Optional max output chars before engine compression." })),
|
|
1354
|
+
timeout_ms: Type.Optional(Type.Number({ description: "Optional timeout ms for code mode." }))
|
|
1355
|
+
}),
|
|
1356
|
+
async execute(_toolCallId, params, signal) {
|
|
1357
|
+
const result = await codemode.callTool("codemode_execute", params, signal);
|
|
1358
|
+
if (result.isError) throw new Error(result.text || "codemode_execute failed");
|
|
1359
|
+
return { content: [{ type: "text", text: result.text }], details: { tool: "codemode_execute" } };
|
|
1360
|
+
}
|
|
1361
|
+
});
|
|
1362
|
+
pi.registerTool({
|
|
1363
|
+
name: "codemode_mutate",
|
|
1364
|
+
label: "Run write-capable Code-Mode call",
|
|
1365
|
+
description: "Execute state-changing MCP tool call(s) via single `tool`+`args`, `steps`, or `code`.",
|
|
1366
|
+
parameters: Type.Object({
|
|
1367
|
+
tool: Type.Optional(Type.String({ description: "Single tool formatted as `server.tool_name`." })),
|
|
1368
|
+
args: Type.Optional(Type.Record(Type.String(), Type.Unknown(), { description: "Arguments for single-tool mode." })),
|
|
1369
|
+
steps: Type.Optional(Type.Array(Type.Record(Type.String(), Type.Unknown()), { description: "Optional multi-step pipeline with writes permitted." })),
|
|
1370
|
+
code: Type.Optional(Type.String({ description: "Optional embedded QuickJS async JS body with writes permitted." })),
|
|
1371
|
+
return_step: Type.Optional(Type.String({ description: "Step id to return in steps mode." })),
|
|
1372
|
+
max_output_chars: Type.Optional(Type.Number({ description: "Optional max output chars." })),
|
|
1373
|
+
timeout_ms: Type.Optional(Type.Number({ description: "Optional timeout ms for code mode." }))
|
|
1374
|
+
}),
|
|
1375
|
+
async execute(_toolCallId, params, signal) {
|
|
1376
|
+
const result = await codemode.callTool("codemode_mutate", params, signal);
|
|
1377
|
+
if (result.isError) throw new Error(result.text || "codemode_mutate failed");
|
|
1378
|
+
return { content: [{ type: "text", text: result.text }], details: { tool: "codemode_mutate" } };
|
|
1379
|
+
}
|
|
1380
|
+
});
|
|
1381
|
+
}
|
|
1115
1382
|
pi.on("session_start", GUARD_session_start(async (_event, ctx) => {
|
|
1116
1383
|
router ??= new ProviderRouter(pi, (message, kind) => notify(ctx, message, kind));
|
|
1117
1384
|
gateDone = false;
|
|
@@ -1202,7 +1469,8 @@ function index_default(pi) {
|
|
|
1202
1469
|
return false;
|
|
1203
1470
|
}
|
|
1204
1471
|
};
|
|
1205
|
-
|
|
1472
|
+
const isCodeModeTool = (name) => name === "codemode_execute" || name === "codemode_mutate" || name === "codemode_search_tools";
|
|
1473
|
+
pi.on("tool_result", GUARD_tool_result((event, ctx) => event.toolName === RECOVERY_TOOL || isCodeModeTool(event.toolName) || !recoveryCallable() ? void 0 : shrinkToolResult(bridge, sessionId, event, recovery, () => {
|
|
1206
1474
|
if (!runtimeReachable || warnedNoReply) return;
|
|
1207
1475
|
warnedNoReply = true;
|
|
1208
1476
|
notify(ctx, "Caveman: tool-output shrinking may be off this session (the native runtime gave no reply)", "warning");
|
|
@@ -1219,6 +1487,7 @@ function index_default(pi) {
|
|
|
1219
1487
|
await bridge.call("SessionEnd", { session_id: sessionId });
|
|
1220
1488
|
await router?.closeGate(ctx);
|
|
1221
1489
|
recovery.dispose();
|
|
1490
|
+
codemode?.dispose();
|
|
1222
1491
|
}));
|
|
1223
1492
|
}
|
|
1224
1493
|
export {
|