@codelia/cli 0.1.84 → 0.1.86
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/index.cjs +137 -151
- package/dist/index.js +137 -151
- package/package.json +10 -10
package/dist/index.cjs
CHANGED
|
@@ -37,7 +37,9 @@ var TOP_LEVEL_HELP_TEXT = [
|
|
|
37
37
|
" session ... List, pin, archive, or clean up session run logs",
|
|
38
38
|
"",
|
|
39
39
|
"Runtime options:",
|
|
40
|
-
" --
|
|
40
|
+
" --sandbox-mode <readonly|workspace-write|full-access>",
|
|
41
|
+
" --approval-mode <minimal|trusted|full-access> (legacy)",
|
|
42
|
+
" trusted retains project allow rules. To disable OS isolation, use --sandbox-mode full-access.",
|
|
41
43
|
"",
|
|
42
44
|
"TUI options (passed through):",
|
|
43
45
|
" -r, --resume [session_id]",
|
|
@@ -47,7 +49,9 @@ var TOP_LEVEL_HELP_TEXT = [
|
|
|
47
49
|
" --initial-user-message <text>",
|
|
48
50
|
" --tui-mode <auto|inline|alternate>",
|
|
49
51
|
" --debug-perf[=true|false]",
|
|
50
|
-
" --
|
|
52
|
+
" --sandbox-mode <readonly|workspace-write|full-access>",
|
|
53
|
+
"",
|
|
54
|
+
"In the TUI, use /auto-approve [status|off] to configure Auto approve."
|
|
51
55
|
].join("\n");
|
|
52
56
|
var isHelpFlag = (value) => value === "-h" || value === "--help" || value === "help";
|
|
53
57
|
var isVersionFlag = (value) => value === "-V" || value === "-v" || value === "--version" || value === "version";
|
|
@@ -96,33 +100,43 @@ var resolvePromptText = (args2) => {
|
|
|
96
100
|
}
|
|
97
101
|
return void 0;
|
|
98
102
|
};
|
|
99
|
-
var
|
|
103
|
+
var resolveRuntimeMode = (args2, flag, values) => {
|
|
100
104
|
for (let i = 0; i < args2.length; i += 1) {
|
|
101
105
|
const current = args2[i];
|
|
102
106
|
if (current === "--") {
|
|
103
107
|
break;
|
|
104
108
|
}
|
|
105
|
-
if (current === "
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
throw new Error(
|
|
109
|
-
"--approval-mode requires a value (minimal|trusted|full-access)"
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
return next.trim();
|
|
113
|
-
}
|
|
114
|
-
if (current.startsWith("--approval-mode=")) {
|
|
115
|
-
const value = current.slice("--approval-mode=".length).trim();
|
|
116
|
-
if (value.length === 0) {
|
|
117
|
-
throw new Error(
|
|
118
|
-
"--approval-mode requires a value (minimal|trusted|full-access)"
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
return value;
|
|
109
|
+
if (current === "-p" || current === "--prompt") {
|
|
110
|
+
i++;
|
|
111
|
+
continue;
|
|
122
112
|
}
|
|
113
|
+
const value = current === flag ? (args2[i + 1] ?? "").trim() : current.startsWith(`${flag}=`) ? current.slice(flag.length + 1).trim() : void 0;
|
|
114
|
+
if (value === void 0) continue;
|
|
115
|
+
if (!value) throw new Error(`${flag} requires a value (${values})`);
|
|
116
|
+
if (!values.split("|").includes(value))
|
|
117
|
+
throw new Error(`Invalid ${flag} (expected ${values})`);
|
|
118
|
+
return value;
|
|
123
119
|
}
|
|
124
120
|
return void 0;
|
|
125
121
|
};
|
|
122
|
+
var resolvePromptModeApproval = (args2) => resolveRuntimeMode(args2, "--approval-mode", "minimal|trusted|full-access");
|
|
123
|
+
var resolvePromptModeSandbox = (args2) => resolveRuntimeMode(
|
|
124
|
+
args2,
|
|
125
|
+
"--sandbox-mode",
|
|
126
|
+
"readonly|workspace-write|full-access"
|
|
127
|
+
);
|
|
128
|
+
var hasHarborShellApprovalFlag = (args2) => {
|
|
129
|
+
for (let index = 0; index < args2.length; index++) {
|
|
130
|
+
const arg = args2[index];
|
|
131
|
+
if (arg === "--") break;
|
|
132
|
+
if (["-p", "--prompt", "--sandbox-mode", "--approval-mode"].includes(arg)) {
|
|
133
|
+
index++;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (arg === "--harbor-shell-approval") return true;
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
};
|
|
126
140
|
var validatePromptText = (value) => {
|
|
127
141
|
const text = value?.trim() ?? "";
|
|
128
142
|
if (text.length === 0) {
|
|
@@ -290,10 +304,6 @@ var import_config_loader = require("@codelia/config-loader");
|
|
|
290
304
|
var import_storage2 = require("@codelia/storage");
|
|
291
305
|
var import_zod = require("zod");
|
|
292
306
|
|
|
293
|
-
// src/mcp/probe.ts
|
|
294
|
-
var import_node_child_process = require("child_process");
|
|
295
|
-
var import_node_readline = require("readline");
|
|
296
|
-
|
|
297
307
|
// src/mcp/protocol.ts
|
|
298
308
|
var import_protocol = require("@codelia/protocol");
|
|
299
309
|
var assertSupportedProtocolVersion = (value) => {
|
|
@@ -303,7 +313,6 @@ var assertSupportedProtocolVersion = (value) => {
|
|
|
303
313
|
// src/mcp/probe.ts
|
|
304
314
|
var DEFAULT_MCP_TIMEOUT_MS = 3e4;
|
|
305
315
|
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
306
|
-
var describeError = (error) => error instanceof Error ? error.message : String(error);
|
|
307
316
|
var postHttpJson = async (url, body, headers, timeoutMs) => {
|
|
308
317
|
const controller = new AbortController();
|
|
309
318
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
@@ -428,111 +437,32 @@ var probeHttpServer = async (config) => {
|
|
|
428
437
|
}
|
|
429
438
|
return total;
|
|
430
439
|
};
|
|
431
|
-
var probeStdioServer = async (config) => {
|
|
432
|
-
const
|
|
433
|
-
const
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
...process.env,
|
|
437
|
-
...config.env ?? {}
|
|
438
|
-
},
|
|
439
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
440
|
-
});
|
|
441
|
-
let seq = 0;
|
|
442
|
-
const pending = /* @__PURE__ */ new Map();
|
|
443
|
-
const stdoutReader = (0, import_node_readline.createInterface)({ input: child.stdout });
|
|
444
|
-
stdoutReader.on("line", (line) => {
|
|
445
|
-
const trimmed = line.trim();
|
|
446
|
-
if (!trimmed) return;
|
|
447
|
-
let parsed;
|
|
448
|
-
try {
|
|
449
|
-
parsed = JSON.parse(trimmed);
|
|
450
|
-
} catch {
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
if (!isRecord(parsed) || parsed.id === void 0 || parsed.id === null) {
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
const id = String(parsed.id);
|
|
457
|
-
const entry = pending.get(id);
|
|
458
|
-
if (!entry) return;
|
|
459
|
-
pending.delete(id);
|
|
460
|
-
clearTimeout(entry.timeout);
|
|
461
|
-
if (isRecord(parsed.error)) {
|
|
462
|
-
entry.reject(
|
|
463
|
-
new Error(
|
|
464
|
-
`RPC ${String(parsed.error.code)}: ${String(parsed.error.message)}`
|
|
465
|
-
)
|
|
466
|
-
);
|
|
467
|
-
return;
|
|
468
|
-
}
|
|
469
|
-
entry.resolve(parsed.result);
|
|
470
|
-
});
|
|
471
|
-
const stderrReader = (0, import_node_readline.createInterface)({ input: child.stderr });
|
|
472
|
-
stderrReader.on("line", () => void 0);
|
|
473
|
-
const failPending = (error) => {
|
|
474
|
-
for (const [id, entry] of pending) {
|
|
475
|
-
pending.delete(id);
|
|
476
|
-
clearTimeout(entry.timeout);
|
|
477
|
-
entry.reject(error);
|
|
478
|
-
}
|
|
479
|
-
};
|
|
480
|
-
child.once("error", (error) => {
|
|
481
|
-
failPending(new Error(`stdio process error: ${describeError(error)}`));
|
|
482
|
-
});
|
|
483
|
-
child.once("exit", (code, signal) => {
|
|
484
|
-
failPending(
|
|
485
|
-
new Error(`stdio exited: code=${String(code)} signal=${String(signal)}`)
|
|
486
|
-
);
|
|
487
|
-
});
|
|
488
|
-
const send = (payload) => new Promise((resolve, reject) => {
|
|
489
|
-
child.stdin.write(`${JSON.stringify(payload)}
|
|
490
|
-
`, (error) => {
|
|
491
|
-
if (error) {
|
|
492
|
-
reject(error);
|
|
493
|
-
return;
|
|
494
|
-
}
|
|
495
|
-
resolve();
|
|
496
|
-
});
|
|
497
|
-
});
|
|
498
|
-
const request = (method, params) => {
|
|
499
|
-
seq += 1;
|
|
500
|
-
const requestId = `cli-${seq}`;
|
|
501
|
-
return new Promise((resolve, reject) => {
|
|
502
|
-
const timeout = setTimeout(() => {
|
|
503
|
-
pending.delete(requestId);
|
|
504
|
-
reject(new Error(`request timed out after ${timeoutMs}ms`));
|
|
505
|
-
}, timeoutMs);
|
|
506
|
-
pending.set(requestId, { resolve, reject, timeout });
|
|
507
|
-
void send({
|
|
508
|
-
jsonrpc: "2.0",
|
|
509
|
-
id: requestId,
|
|
510
|
-
method,
|
|
511
|
-
params
|
|
512
|
-
}).catch((error) => {
|
|
513
|
-
const entry = pending.get(requestId);
|
|
514
|
-
if (!entry) return;
|
|
515
|
-
pending.delete(requestId);
|
|
516
|
-
clearTimeout(entry.timeout);
|
|
517
|
-
reject(new Error(`failed to send request: ${describeError(error)}`));
|
|
518
|
-
});
|
|
519
|
-
});
|
|
520
|
-
};
|
|
521
|
-
const notify = async (method, params) => {
|
|
522
|
-
await send({ jsonrpc: "2.0", method, params });
|
|
440
|
+
var probeStdioServer = async (config, serverId) => {
|
|
441
|
+
const { createLocalMcpStdioProbeClient } = await import("@codelia/runtime/sdk");
|
|
442
|
+
const client = await createLocalMcpStdioProbeClient(serverId, config);
|
|
443
|
+
const options = {
|
|
444
|
+
timeoutMs: config.request_timeout_ms ?? DEFAULT_MCP_TIMEOUT_MS
|
|
523
445
|
};
|
|
524
446
|
try {
|
|
525
|
-
const initResult = await request(
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
447
|
+
const initResult = await client.request(
|
|
448
|
+
"initialize",
|
|
449
|
+
{
|
|
450
|
+
protocolVersion: (0, import_protocol.getMcpProtocolVersion)(),
|
|
451
|
+
clientInfo: { name: "codelia-cli", version: "0.1.0" },
|
|
452
|
+
capabilities: {}
|
|
453
|
+
},
|
|
454
|
+
options
|
|
455
|
+
);
|
|
530
456
|
assertSupportedProtocolVersion(initResult);
|
|
531
|
-
await notify("notifications/initialized", {});
|
|
457
|
+
await client.notify("notifications/initialized", {});
|
|
532
458
|
let total = 0;
|
|
533
459
|
let cursor;
|
|
534
460
|
for (let page = 0; page < 100; page += 1) {
|
|
535
|
-
const result = await request(
|
|
461
|
+
const result = await client.request(
|
|
462
|
+
"tools/list",
|
|
463
|
+
cursor ? { cursor } : {},
|
|
464
|
+
options
|
|
465
|
+
);
|
|
536
466
|
const parsed = parseToolsResponse(result);
|
|
537
467
|
total += parsed.count;
|
|
538
468
|
if (!parsed.nextCursor) break;
|
|
@@ -540,16 +470,14 @@ var probeStdioServer = async (config) => {
|
|
|
540
470
|
}
|
|
541
471
|
return total;
|
|
542
472
|
} finally {
|
|
543
|
-
|
|
544
|
-
stdoutReader.close();
|
|
545
|
-
stderrReader.close();
|
|
473
|
+
await client.close();
|
|
546
474
|
}
|
|
547
475
|
};
|
|
548
|
-
var probeServer = async (config) => {
|
|
476
|
+
var probeServer = async (config, serverId) => {
|
|
549
477
|
if (config.transport === "http") {
|
|
550
478
|
return probeHttpServer(config);
|
|
551
479
|
}
|
|
552
|
-
return probeStdioServer(config);
|
|
480
|
+
return probeStdioServer(config, serverId);
|
|
553
481
|
};
|
|
554
482
|
|
|
555
483
|
// src/commands/mcp-config.ts
|
|
@@ -585,7 +513,7 @@ var MCP_SERVER_CONFIG_SCHEMA = import_zod.z.union([
|
|
|
585
513
|
HTTP_SERVER_CONFIG_SCHEMA,
|
|
586
514
|
STDIO_SERVER_CONFIG_SCHEMA
|
|
587
515
|
]);
|
|
588
|
-
var
|
|
516
|
+
var describeError = (error) => error instanceof Error ? error.message : String(error);
|
|
589
517
|
var readEnvValue = (key) => {
|
|
590
518
|
const value = process.env[key];
|
|
591
519
|
if (!value) return void 0;
|
|
@@ -803,14 +731,14 @@ var runMcpConfigCommand = async (subcommand, rest) => {
|
|
|
803
731
|
Authorization: `Bearer ${accessToken}`
|
|
804
732
|
}
|
|
805
733
|
} : target.config;
|
|
806
|
-
const tools = await probeServer(configForTest);
|
|
734
|
+
const tools = await probeServer(configForTest, target.id);
|
|
807
735
|
console.log(
|
|
808
736
|
`MCP test succeeded: id=${target.id} transport=${target.config.transport} source=${target.source} tools=${tools}`
|
|
809
737
|
);
|
|
810
738
|
return 0;
|
|
811
739
|
} catch (error) {
|
|
812
740
|
console.error(
|
|
813
|
-
`MCP test failed: id=${target.id} transport=${target.config.transport}: ${
|
|
741
|
+
`MCP test failed: id=${target.id} transport=${target.config.transport}: ${describeError(error)}`
|
|
814
742
|
);
|
|
815
743
|
return 1;
|
|
816
744
|
}
|
|
@@ -885,7 +813,7 @@ async function runSessionCommand(args2) {
|
|
|
885
813
|
}
|
|
886
814
|
|
|
887
815
|
// src/prompt-mode.ts
|
|
888
|
-
var
|
|
816
|
+
var import_node_child_process2 = require("child_process");
|
|
889
817
|
|
|
890
818
|
// src/atif.ts
|
|
891
819
|
var import_promises = require("fs/promises");
|
|
@@ -1103,10 +1031,12 @@ var readSessionJsonl = async (filePath) => {
|
|
|
1103
1031
|
var writeAtifFromSessionJsonl = async ({
|
|
1104
1032
|
sessionLogPath,
|
|
1105
1033
|
runId,
|
|
1106
|
-
outPath
|
|
1034
|
+
outPath,
|
|
1035
|
+
extra
|
|
1107
1036
|
}) => {
|
|
1108
1037
|
const records = await readSessionJsonl(sessionLogPath);
|
|
1109
1038
|
const trajectory = sessionRecordsToAtif(records, runId);
|
|
1039
|
+
if (extra) trajectory.extra = { ...extra, ...trajectory.extra };
|
|
1110
1040
|
await (0, import_promises.mkdir)(import_node_path2.default.dirname(outPath), { recursive: true });
|
|
1111
1041
|
const tempPath = `${outPath}.tmp-${process.pid}-${Date.now()}`;
|
|
1112
1042
|
await (0, import_promises.writeFile)(tempPath, `${JSON.stringify(trajectory, null, 2)}
|
|
@@ -1116,14 +1046,14 @@ var writeAtifFromSessionJsonl = async ({
|
|
|
1116
1046
|
};
|
|
1117
1047
|
|
|
1118
1048
|
// src/tui/launcher.ts
|
|
1119
|
-
var
|
|
1049
|
+
var import_node_child_process = require("child_process");
|
|
1120
1050
|
var import_node_fs = __toESM(require("fs"), 1);
|
|
1121
1051
|
var import_node_module = require("module");
|
|
1122
1052
|
var import_node_path3 = __toESM(require("path"), 1);
|
|
1123
1053
|
var import_storage5 = require("@codelia/storage");
|
|
1124
1054
|
|
|
1125
1055
|
// src/version.ts
|
|
1126
|
-
var CLI_VERSION = "0.1.
|
|
1056
|
+
var CLI_VERSION = "0.1.86".trim().length > 0 ? "0.1.86" : "0.0.0";
|
|
1127
1057
|
|
|
1128
1058
|
// src/tui/launcher.ts
|
|
1129
1059
|
var resolveTuiBinaryName = (platform) => platform === "win32" ? "codelia-tui.exe" : "codelia-tui";
|
|
@@ -1134,7 +1064,7 @@ var TUI_PACKAGE_BY_TARGET = {
|
|
|
1134
1064
|
"linux-x64": "@codelia/tui-linux-x64",
|
|
1135
1065
|
"win32-x64": "@codelia/tui-win32-x64"
|
|
1136
1066
|
};
|
|
1137
|
-
var
|
|
1067
|
+
var describeError2 = (error) => error instanceof Error ? error.message : String(error);
|
|
1138
1068
|
var splitArgs = (value) => value.split(/\s+/).map((part) => part.trim()).filter((part) => part.length > 0);
|
|
1139
1069
|
var quoteShellWord = (value) => `'${value.replace(/'/g, "'\\''")}'`;
|
|
1140
1070
|
var isExecutable = (candidate) => {
|
|
@@ -1151,7 +1081,7 @@ var initStorage = async () => {
|
|
|
1151
1081
|
await (0, import_storage5.ensureStorageDirs)(paths);
|
|
1152
1082
|
} catch (error) {
|
|
1153
1083
|
console.warn(
|
|
1154
|
-
`Failed to initialize storage directories: ${
|
|
1084
|
+
`Failed to initialize storage directories: ${describeError2(error)}`
|
|
1155
1085
|
);
|
|
1156
1086
|
}
|
|
1157
1087
|
};
|
|
@@ -1281,7 +1211,7 @@ var runTui = (args2) => {
|
|
|
1281
1211
|
void initStorage();
|
|
1282
1212
|
const tuiCmd = resolveTuiCommand();
|
|
1283
1213
|
const tuiArgs = process.env.CODELIA_TUI_ARGS ? splitArgs(process.env.CODELIA_TUI_ARGS) : [];
|
|
1284
|
-
const child = (0,
|
|
1214
|
+
const child = (0, import_node_child_process.spawn)(tuiCmd, [...tuiArgs, ...args2], {
|
|
1285
1215
|
stdio: "inherit",
|
|
1286
1216
|
env: resolveLaunchEnvForTui(process.env)
|
|
1287
1217
|
});
|
|
@@ -1291,7 +1221,7 @@ var runTui = (args2) => {
|
|
|
1291
1221
|
child.on("error", (error) => {
|
|
1292
1222
|
const code = typeof error === "object" && error !== null && "code" in error ? String(error.code ?? "") : "";
|
|
1293
1223
|
const expectedPackage = resolvePlatformTuiPackageName();
|
|
1294
|
-
console.error(`Failed to launch TUI (${tuiCmd}): ${
|
|
1224
|
+
console.error(`Failed to launch TUI (${tuiCmd}): ${describeError2(error)}`);
|
|
1295
1225
|
if (code === "EACCES") {
|
|
1296
1226
|
console.error(
|
|
1297
1227
|
"Permission denied while launching TUI. Verify execute permission and PATH entries."
|
|
@@ -1427,10 +1357,33 @@ var summarizeProgressEvent = (event) => {
|
|
|
1427
1357
|
};
|
|
1428
1358
|
var isRpcResponse = (value) => typeof value === "object" && value !== null && "jsonrpc" in value && "id" in value && !value.method;
|
|
1429
1359
|
var isRpcNotification = (value) => typeof value === "object" && value !== null && "jsonrpc" in value && "method" in value && !value.id;
|
|
1360
|
+
var asRecord2 = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
1361
|
+
var harborUiResponse = (value, runId, allowShell) => {
|
|
1362
|
+
const request = asRecord2(value);
|
|
1363
|
+
if (request?.jsonrpc !== "2.0" || typeof request.id !== "string" || !/^ui_[1-9]\d*$/u.test(request.id))
|
|
1364
|
+
return void 0;
|
|
1365
|
+
const response = { jsonrpc: "2.0", id: request.id };
|
|
1366
|
+
switch (request.method) {
|
|
1367
|
+
case "ui.confirm.request":
|
|
1368
|
+
return { ...response, result: { ok: false } };
|
|
1369
|
+
case "ui.prompt.request":
|
|
1370
|
+
return { ...response, result: { value: null } };
|
|
1371
|
+
case "ui.pick.request": {
|
|
1372
|
+
const params = asRecord2(request.params);
|
|
1373
|
+
const choices = allowShell && runId !== null && params?.run_id === runId && params.approval === true && params.title === "Approve shell?" && (params.multi === void 0 || params.multi === false) && Array.isArray(params.items) ? params.items : [];
|
|
1374
|
+
const once = choices.map((item) => asRecord2(item)?.id).filter(
|
|
1375
|
+
(id) => typeof id === "string" && /^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}\/allow_once$/u.test(id)
|
|
1376
|
+
);
|
|
1377
|
+
return { ...response, result: { ids: once.length === 1 ? once : [] } };
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
};
|
|
1430
1381
|
var writePromptModeAtif = async ({
|
|
1431
1382
|
atifOut,
|
|
1432
1383
|
sessionLogPath,
|
|
1433
|
-
runId
|
|
1384
|
+
runId,
|
|
1385
|
+
sandboxModeRequested,
|
|
1386
|
+
sandboxModeReported
|
|
1434
1387
|
}) => {
|
|
1435
1388
|
if (!atifOut) {
|
|
1436
1389
|
return "skipped";
|
|
@@ -1441,7 +1394,13 @@ var writePromptModeAtif = async ({
|
|
|
1441
1394
|
await writeAtifFromSessionJsonl({
|
|
1442
1395
|
sessionLogPath,
|
|
1443
1396
|
runId,
|
|
1444
|
-
outPath: atifOut
|
|
1397
|
+
outPath: atifOut,
|
|
1398
|
+
...sandboxModeRequested ? {
|
|
1399
|
+
extra: {
|
|
1400
|
+
sandbox_mode_requested: sandboxModeRequested,
|
|
1401
|
+
sandbox_mode: sandboxModeReported ?? null
|
|
1402
|
+
}
|
|
1403
|
+
} : {}
|
|
1445
1404
|
});
|
|
1446
1405
|
return "written";
|
|
1447
1406
|
};
|
|
@@ -1450,6 +1409,7 @@ var runPromptMode = async (options) => {
|
|
|
1450
1409
|
process.env.CODELIA_PROMPT_PROGRESS_STDERR
|
|
1451
1410
|
);
|
|
1452
1411
|
const atifOut = process.env.CODELIA_ATIF_OUT?.trim();
|
|
1412
|
+
const harborShellRequested = options.harborShellApproval === true && options.sandboxMode === "full-access" && process.env.CODELIA_HARBOR_SHELL_APPROVAL === "1";
|
|
1453
1413
|
const runtimeEnv = resolveRuntimeEnvForTui(process.env);
|
|
1454
1414
|
const runtimeCmd = runtimeEnv.CODELIA_RUNTIME_CMD ?? process.execPath;
|
|
1455
1415
|
const runtimeArgsValue = runtimeEnv.CODELIA_RUNTIME_ARGS ?? "packages/runtime/src/index.ts";
|
|
@@ -1457,15 +1417,20 @@ var runPromptMode = async (options) => {
|
|
|
1457
1417
|
if (runtimeArgs.length === 0) {
|
|
1458
1418
|
runtimeArgs.push("packages/runtime/src/index.ts");
|
|
1459
1419
|
}
|
|
1420
|
+
if (options.sandboxMode) {
|
|
1421
|
+
runtimeArgs.push("--sandbox-mode", options.sandboxMode);
|
|
1422
|
+
}
|
|
1460
1423
|
if (options.approvalMode) {
|
|
1461
1424
|
runtimeArgs.push("--approval-mode", options.approvalMode);
|
|
1462
1425
|
}
|
|
1463
|
-
const child = (0,
|
|
1426
|
+
const child = (0, import_node_child_process2.spawn)(runtimeCmd, runtimeArgs, {
|
|
1464
1427
|
env: runtimeEnv,
|
|
1465
1428
|
stdio: ["pipe", "pipe", "pipe"]
|
|
1466
1429
|
});
|
|
1467
1430
|
let buffer = "";
|
|
1468
1431
|
let runId = null;
|
|
1432
|
+
let reportedSandbox = null;
|
|
1433
|
+
let harborShellAllowed = false;
|
|
1469
1434
|
let sessionLogPath = null;
|
|
1470
1435
|
let finalText = "";
|
|
1471
1436
|
let terminalStatus = null;
|
|
@@ -1490,7 +1455,7 @@ var runPromptMode = async (options) => {
|
|
|
1490
1455
|
pendingResponses.set(id, { resolve, reject });
|
|
1491
1456
|
});
|
|
1492
1457
|
};
|
|
1493
|
-
const
|
|
1458
|
+
const sendMessage = (request) => {
|
|
1494
1459
|
if (startupFailure) {
|
|
1495
1460
|
throw startupFailure;
|
|
1496
1461
|
}
|
|
@@ -1505,7 +1470,9 @@ var runPromptMode = async (options) => {
|
|
|
1505
1470
|
atifWriteInFlight ??= writePromptModeAtif({
|
|
1506
1471
|
atifOut,
|
|
1507
1472
|
sessionLogPath,
|
|
1508
|
-
runId
|
|
1473
|
+
runId,
|
|
1474
|
+
sandboxModeRequested: options.sandboxMode,
|
|
1475
|
+
sandboxModeReported: reportedSandbox
|
|
1509
1476
|
});
|
|
1510
1477
|
const result = await atifWriteInFlight;
|
|
1511
1478
|
if (result === "written") {
|
|
@@ -1612,9 +1579,21 @@ var runPromptMode = async (options) => {
|
|
|
1612
1579
|
const pending = pendingResponses.get(String(parsed.id));
|
|
1613
1580
|
if (pending) {
|
|
1614
1581
|
pendingResponses.delete(String(parsed.id));
|
|
1582
|
+
if (parsed.id === "run-1") {
|
|
1583
|
+
const result = asRecord2(parsed.result);
|
|
1584
|
+
runId = typeof result?.run_id === "string" ? result.run_id : null;
|
|
1585
|
+
}
|
|
1615
1586
|
pending.resolve(parsed);
|
|
1616
1587
|
}
|
|
1617
1588
|
}
|
|
1589
|
+
if (harborShellRequested) {
|
|
1590
|
+
const response = harborUiResponse(
|
|
1591
|
+
parsed,
|
|
1592
|
+
terminalStatus ? null : runId,
|
|
1593
|
+
harborShellAllowed
|
|
1594
|
+
);
|
|
1595
|
+
if (response) sendMessage(response);
|
|
1596
|
+
}
|
|
1618
1597
|
maybeTrackRunLifecycle(parsed);
|
|
1619
1598
|
} catch {
|
|
1620
1599
|
}
|
|
@@ -1627,7 +1606,7 @@ var runPromptMode = async (options) => {
|
|
|
1627
1606
|
process.stderr.write(chunk);
|
|
1628
1607
|
});
|
|
1629
1608
|
try {
|
|
1630
|
-
|
|
1609
|
+
sendMessage({
|
|
1631
1610
|
jsonrpc: "2.0",
|
|
1632
1611
|
id: "init-1",
|
|
1633
1612
|
method: "initialize",
|
|
@@ -1637,7 +1616,8 @@ var runPromptMode = async (options) => {
|
|
|
1637
1616
|
ui_capabilities: {
|
|
1638
1617
|
supports_confirm: false,
|
|
1639
1618
|
supports_prompt: false,
|
|
1640
|
-
supports_pick:
|
|
1619
|
+
supports_pick: harborShellRequested,
|
|
1620
|
+
...harborShellRequested ? { supports_structured_approval: true } : {}
|
|
1641
1621
|
}
|
|
1642
1622
|
}
|
|
1643
1623
|
});
|
|
@@ -1645,7 +1625,11 @@ var runPromptMode = async (options) => {
|
|
|
1645
1625
|
if (initResponse.error) {
|
|
1646
1626
|
throw new Error(`initialize failed: ${initResponse.error.message}`);
|
|
1647
1627
|
}
|
|
1648
|
-
|
|
1628
|
+
const init = initResponse.result;
|
|
1629
|
+
const mode = init?.sandbox_mode?.value;
|
|
1630
|
+
reportedSandbox = mode === "readonly" || mode === "workspace-write" || mode === "full-access" ? mode : null;
|
|
1631
|
+
harborShellAllowed = harborShellRequested && init?.server_capabilities?.supports_structured_approval === true && reportedSandbox === "full-access";
|
|
1632
|
+
sendMessage({
|
|
1649
1633
|
jsonrpc: "2.0",
|
|
1650
1634
|
id: "run-1",
|
|
1651
1635
|
method: "run.start",
|
|
@@ -1658,7 +1642,6 @@ var runPromptMode = async (options) => {
|
|
|
1658
1642
|
throw new Error(`run.start failed: ${runResponse.error.message}`);
|
|
1659
1643
|
}
|
|
1660
1644
|
const runResult = typeof runResponse.result === "object" && runResponse.result !== null ? runResponse.result : null;
|
|
1661
|
-
runId = typeof runResult?.run_id === "string" ? runResult.run_id : null;
|
|
1662
1645
|
sessionLogPath = typeof runResult?.session_log_path === "string" ? runResult.session_log_path : null;
|
|
1663
1646
|
if (!runId) {
|
|
1664
1647
|
throw new Error("run.start did not return run_id");
|
|
@@ -1738,10 +1721,13 @@ var main = async () => {
|
|
|
1738
1721
|
}
|
|
1739
1722
|
case "prompt": {
|
|
1740
1723
|
const prompt = validatePromptText(resolvePromptText(args));
|
|
1741
|
-
const
|
|
1724
|
+
const sandboxMode = resolvePromptModeSandbox(args);
|
|
1725
|
+
const approvalMode = sandboxMode ? void 0 : resolvePromptModeApproval(args);
|
|
1742
1726
|
const exitCode = await runPromptMode({
|
|
1743
1727
|
prompt,
|
|
1744
|
-
approvalMode
|
|
1728
|
+
approvalMode,
|
|
1729
|
+
sandboxMode,
|
|
1730
|
+
harborShellApproval: hasHarborShellApprovalFlag(args)
|
|
1745
1731
|
});
|
|
1746
1732
|
process.exitCode = exitCode;
|
|
1747
1733
|
return;
|
package/dist/index.js
CHANGED
|
@@ -12,7 +12,9 @@ var TOP_LEVEL_HELP_TEXT = [
|
|
|
12
12
|
" session ... List, pin, archive, or clean up session run logs",
|
|
13
13
|
"",
|
|
14
14
|
"Runtime options:",
|
|
15
|
-
" --
|
|
15
|
+
" --sandbox-mode <readonly|workspace-write|full-access>",
|
|
16
|
+
" --approval-mode <minimal|trusted|full-access> (legacy)",
|
|
17
|
+
" trusted retains project allow rules. To disable OS isolation, use --sandbox-mode full-access.",
|
|
16
18
|
"",
|
|
17
19
|
"TUI options (passed through):",
|
|
18
20
|
" -r, --resume [session_id]",
|
|
@@ -22,7 +24,9 @@ var TOP_LEVEL_HELP_TEXT = [
|
|
|
22
24
|
" --initial-user-message <text>",
|
|
23
25
|
" --tui-mode <auto|inline|alternate>",
|
|
24
26
|
" --debug-perf[=true|false]",
|
|
25
|
-
" --
|
|
27
|
+
" --sandbox-mode <readonly|workspace-write|full-access>",
|
|
28
|
+
"",
|
|
29
|
+
"In the TUI, use /auto-approve [status|off] to configure Auto approve."
|
|
26
30
|
].join("\n");
|
|
27
31
|
var isHelpFlag = (value) => value === "-h" || value === "--help" || value === "help";
|
|
28
32
|
var isVersionFlag = (value) => value === "-V" || value === "-v" || value === "--version" || value === "version";
|
|
@@ -71,33 +75,43 @@ var resolvePromptText = (args2) => {
|
|
|
71
75
|
}
|
|
72
76
|
return void 0;
|
|
73
77
|
};
|
|
74
|
-
var
|
|
78
|
+
var resolveRuntimeMode = (args2, flag, values) => {
|
|
75
79
|
for (let i = 0; i < args2.length; i += 1) {
|
|
76
80
|
const current = args2[i];
|
|
77
81
|
if (current === "--") {
|
|
78
82
|
break;
|
|
79
83
|
}
|
|
80
|
-
if (current === "
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
throw new Error(
|
|
84
|
-
"--approval-mode requires a value (minimal|trusted|full-access)"
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
return next.trim();
|
|
88
|
-
}
|
|
89
|
-
if (current.startsWith("--approval-mode=")) {
|
|
90
|
-
const value = current.slice("--approval-mode=".length).trim();
|
|
91
|
-
if (value.length === 0) {
|
|
92
|
-
throw new Error(
|
|
93
|
-
"--approval-mode requires a value (minimal|trusted|full-access)"
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
return value;
|
|
84
|
+
if (current === "-p" || current === "--prompt") {
|
|
85
|
+
i++;
|
|
86
|
+
continue;
|
|
97
87
|
}
|
|
88
|
+
const value = current === flag ? (args2[i + 1] ?? "").trim() : current.startsWith(`${flag}=`) ? current.slice(flag.length + 1).trim() : void 0;
|
|
89
|
+
if (value === void 0) continue;
|
|
90
|
+
if (!value) throw new Error(`${flag} requires a value (${values})`);
|
|
91
|
+
if (!values.split("|").includes(value))
|
|
92
|
+
throw new Error(`Invalid ${flag} (expected ${values})`);
|
|
93
|
+
return value;
|
|
98
94
|
}
|
|
99
95
|
return void 0;
|
|
100
96
|
};
|
|
97
|
+
var resolvePromptModeApproval = (args2) => resolveRuntimeMode(args2, "--approval-mode", "minimal|trusted|full-access");
|
|
98
|
+
var resolvePromptModeSandbox = (args2) => resolveRuntimeMode(
|
|
99
|
+
args2,
|
|
100
|
+
"--sandbox-mode",
|
|
101
|
+
"readonly|workspace-write|full-access"
|
|
102
|
+
);
|
|
103
|
+
var hasHarborShellApprovalFlag = (args2) => {
|
|
104
|
+
for (let index = 0; index < args2.length; index++) {
|
|
105
|
+
const arg = args2[index];
|
|
106
|
+
if (arg === "--") break;
|
|
107
|
+
if (["-p", "--prompt", "--sandbox-mode", "--approval-mode"].includes(arg)) {
|
|
108
|
+
index++;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (arg === "--harbor-shell-approval") return true;
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
};
|
|
101
115
|
var validatePromptText = (value) => {
|
|
102
116
|
const text = value?.trim() ?? "";
|
|
103
117
|
if (text.length === 0) {
|
|
@@ -270,10 +284,6 @@ import {
|
|
|
270
284
|
import { resolveStoragePaths } from "@codelia/storage";
|
|
271
285
|
import { z } from "zod";
|
|
272
286
|
|
|
273
|
-
// src/mcp/probe.ts
|
|
274
|
-
import { spawn } from "child_process";
|
|
275
|
-
import { createInterface } from "readline";
|
|
276
|
-
|
|
277
287
|
// src/mcp/protocol.ts
|
|
278
288
|
import {
|
|
279
289
|
assertSupportedMcpProtocolVersion,
|
|
@@ -287,7 +297,6 @@ var assertSupportedProtocolVersion = (value) => {
|
|
|
287
297
|
// src/mcp/probe.ts
|
|
288
298
|
var DEFAULT_MCP_TIMEOUT_MS = 3e4;
|
|
289
299
|
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
290
|
-
var describeError = (error) => error instanceof Error ? error.message : String(error);
|
|
291
300
|
var postHttpJson = async (url, body, headers, timeoutMs) => {
|
|
292
301
|
const controller = new AbortController();
|
|
293
302
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
@@ -412,111 +421,32 @@ var probeHttpServer = async (config) => {
|
|
|
412
421
|
}
|
|
413
422
|
return total;
|
|
414
423
|
};
|
|
415
|
-
var probeStdioServer = async (config) => {
|
|
416
|
-
const
|
|
417
|
-
const
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
...process.env,
|
|
421
|
-
...config.env ?? {}
|
|
422
|
-
},
|
|
423
|
-
stdio: ["pipe", "pipe", "pipe"]
|
|
424
|
-
});
|
|
425
|
-
let seq = 0;
|
|
426
|
-
const pending = /* @__PURE__ */ new Map();
|
|
427
|
-
const stdoutReader = createInterface({ input: child.stdout });
|
|
428
|
-
stdoutReader.on("line", (line) => {
|
|
429
|
-
const trimmed = line.trim();
|
|
430
|
-
if (!trimmed) return;
|
|
431
|
-
let parsed;
|
|
432
|
-
try {
|
|
433
|
-
parsed = JSON.parse(trimmed);
|
|
434
|
-
} catch {
|
|
435
|
-
return;
|
|
436
|
-
}
|
|
437
|
-
if (!isRecord(parsed) || parsed.id === void 0 || parsed.id === null) {
|
|
438
|
-
return;
|
|
439
|
-
}
|
|
440
|
-
const id = String(parsed.id);
|
|
441
|
-
const entry = pending.get(id);
|
|
442
|
-
if (!entry) return;
|
|
443
|
-
pending.delete(id);
|
|
444
|
-
clearTimeout(entry.timeout);
|
|
445
|
-
if (isRecord(parsed.error)) {
|
|
446
|
-
entry.reject(
|
|
447
|
-
new Error(
|
|
448
|
-
`RPC ${String(parsed.error.code)}: ${String(parsed.error.message)}`
|
|
449
|
-
)
|
|
450
|
-
);
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
entry.resolve(parsed.result);
|
|
454
|
-
});
|
|
455
|
-
const stderrReader = createInterface({ input: child.stderr });
|
|
456
|
-
stderrReader.on("line", () => void 0);
|
|
457
|
-
const failPending = (error) => {
|
|
458
|
-
for (const [id, entry] of pending) {
|
|
459
|
-
pending.delete(id);
|
|
460
|
-
clearTimeout(entry.timeout);
|
|
461
|
-
entry.reject(error);
|
|
462
|
-
}
|
|
463
|
-
};
|
|
464
|
-
child.once("error", (error) => {
|
|
465
|
-
failPending(new Error(`stdio process error: ${describeError(error)}`));
|
|
466
|
-
});
|
|
467
|
-
child.once("exit", (code, signal) => {
|
|
468
|
-
failPending(
|
|
469
|
-
new Error(`stdio exited: code=${String(code)} signal=${String(signal)}`)
|
|
470
|
-
);
|
|
471
|
-
});
|
|
472
|
-
const send = (payload) => new Promise((resolve, reject) => {
|
|
473
|
-
child.stdin.write(`${JSON.stringify(payload)}
|
|
474
|
-
`, (error) => {
|
|
475
|
-
if (error) {
|
|
476
|
-
reject(error);
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
resolve();
|
|
480
|
-
});
|
|
481
|
-
});
|
|
482
|
-
const request = (method, params) => {
|
|
483
|
-
seq += 1;
|
|
484
|
-
const requestId = `cli-${seq}`;
|
|
485
|
-
return new Promise((resolve, reject) => {
|
|
486
|
-
const timeout = setTimeout(() => {
|
|
487
|
-
pending.delete(requestId);
|
|
488
|
-
reject(new Error(`request timed out after ${timeoutMs}ms`));
|
|
489
|
-
}, timeoutMs);
|
|
490
|
-
pending.set(requestId, { resolve, reject, timeout });
|
|
491
|
-
void send({
|
|
492
|
-
jsonrpc: "2.0",
|
|
493
|
-
id: requestId,
|
|
494
|
-
method,
|
|
495
|
-
params
|
|
496
|
-
}).catch((error) => {
|
|
497
|
-
const entry = pending.get(requestId);
|
|
498
|
-
if (!entry) return;
|
|
499
|
-
pending.delete(requestId);
|
|
500
|
-
clearTimeout(entry.timeout);
|
|
501
|
-
reject(new Error(`failed to send request: ${describeError(error)}`));
|
|
502
|
-
});
|
|
503
|
-
});
|
|
504
|
-
};
|
|
505
|
-
const notify = async (method, params) => {
|
|
506
|
-
await send({ jsonrpc: "2.0", method, params });
|
|
424
|
+
var probeStdioServer = async (config, serverId) => {
|
|
425
|
+
const { createLocalMcpStdioProbeClient } = await import("@codelia/runtime/sdk");
|
|
426
|
+
const client = await createLocalMcpStdioProbeClient(serverId, config);
|
|
427
|
+
const options = {
|
|
428
|
+
timeoutMs: config.request_timeout_ms ?? DEFAULT_MCP_TIMEOUT_MS
|
|
507
429
|
};
|
|
508
430
|
try {
|
|
509
|
-
const initResult = await request(
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
431
|
+
const initResult = await client.request(
|
|
432
|
+
"initialize",
|
|
433
|
+
{
|
|
434
|
+
protocolVersion: getMcpProtocolVersion(),
|
|
435
|
+
clientInfo: { name: "codelia-cli", version: "0.1.0" },
|
|
436
|
+
capabilities: {}
|
|
437
|
+
},
|
|
438
|
+
options
|
|
439
|
+
);
|
|
514
440
|
assertSupportedProtocolVersion(initResult);
|
|
515
|
-
await notify("notifications/initialized", {});
|
|
441
|
+
await client.notify("notifications/initialized", {});
|
|
516
442
|
let total = 0;
|
|
517
443
|
let cursor;
|
|
518
444
|
for (let page = 0; page < 100; page += 1) {
|
|
519
|
-
const result = await request(
|
|
445
|
+
const result = await client.request(
|
|
446
|
+
"tools/list",
|
|
447
|
+
cursor ? { cursor } : {},
|
|
448
|
+
options
|
|
449
|
+
);
|
|
520
450
|
const parsed = parseToolsResponse(result);
|
|
521
451
|
total += parsed.count;
|
|
522
452
|
if (!parsed.nextCursor) break;
|
|
@@ -524,16 +454,14 @@ var probeStdioServer = async (config) => {
|
|
|
524
454
|
}
|
|
525
455
|
return total;
|
|
526
456
|
} finally {
|
|
527
|
-
|
|
528
|
-
stdoutReader.close();
|
|
529
|
-
stderrReader.close();
|
|
457
|
+
await client.close();
|
|
530
458
|
}
|
|
531
459
|
};
|
|
532
|
-
var probeServer = async (config) => {
|
|
460
|
+
var probeServer = async (config, serverId) => {
|
|
533
461
|
if (config.transport === "http") {
|
|
534
462
|
return probeHttpServer(config);
|
|
535
463
|
}
|
|
536
|
-
return probeStdioServer(config);
|
|
464
|
+
return probeStdioServer(config, serverId);
|
|
537
465
|
};
|
|
538
466
|
|
|
539
467
|
// src/commands/mcp-config.ts
|
|
@@ -569,7 +497,7 @@ var MCP_SERVER_CONFIG_SCHEMA = z.union([
|
|
|
569
497
|
HTTP_SERVER_CONFIG_SCHEMA,
|
|
570
498
|
STDIO_SERVER_CONFIG_SCHEMA
|
|
571
499
|
]);
|
|
572
|
-
var
|
|
500
|
+
var describeError = (error) => error instanceof Error ? error.message : String(error);
|
|
573
501
|
var readEnvValue = (key) => {
|
|
574
502
|
const value = process.env[key];
|
|
575
503
|
if (!value) return void 0;
|
|
@@ -787,14 +715,14 @@ var runMcpConfigCommand = async (subcommand, rest) => {
|
|
|
787
715
|
Authorization: `Bearer ${accessToken}`
|
|
788
716
|
}
|
|
789
717
|
} : target.config;
|
|
790
|
-
const tools = await probeServer(configForTest);
|
|
718
|
+
const tools = await probeServer(configForTest, target.id);
|
|
791
719
|
console.log(
|
|
792
720
|
`MCP test succeeded: id=${target.id} transport=${target.config.transport} source=${target.source} tools=${tools}`
|
|
793
721
|
);
|
|
794
722
|
return 0;
|
|
795
723
|
} catch (error) {
|
|
796
724
|
console.error(
|
|
797
|
-
`MCP test failed: id=${target.id} transport=${target.config.transport}: ${
|
|
725
|
+
`MCP test failed: id=${target.id} transport=${target.config.transport}: ${describeError(error)}`
|
|
798
726
|
);
|
|
799
727
|
return 1;
|
|
800
728
|
}
|
|
@@ -873,7 +801,7 @@ async function runSessionCommand(args2) {
|
|
|
873
801
|
}
|
|
874
802
|
|
|
875
803
|
// src/prompt-mode.ts
|
|
876
|
-
import { spawn as
|
|
804
|
+
import { spawn as spawn2 } from "child_process";
|
|
877
805
|
|
|
878
806
|
// src/atif.ts
|
|
879
807
|
import { mkdir, rename, writeFile } from "fs/promises";
|
|
@@ -1091,10 +1019,12 @@ var readSessionJsonl = async (filePath) => {
|
|
|
1091
1019
|
var writeAtifFromSessionJsonl = async ({
|
|
1092
1020
|
sessionLogPath,
|
|
1093
1021
|
runId,
|
|
1094
|
-
outPath
|
|
1022
|
+
outPath,
|
|
1023
|
+
extra
|
|
1095
1024
|
}) => {
|
|
1096
1025
|
const records = await readSessionJsonl(sessionLogPath);
|
|
1097
1026
|
const trajectory = sessionRecordsToAtif(records, runId);
|
|
1027
|
+
if (extra) trajectory.extra = { ...extra, ...trajectory.extra };
|
|
1098
1028
|
await mkdir(path2.dirname(outPath), { recursive: true });
|
|
1099
1029
|
const tempPath = `${outPath}.tmp-${process.pid}-${Date.now()}`;
|
|
1100
1030
|
await writeFile(tempPath, `${JSON.stringify(trajectory, null, 2)}
|
|
@@ -1104,14 +1034,14 @@ var writeAtifFromSessionJsonl = async ({
|
|
|
1104
1034
|
};
|
|
1105
1035
|
|
|
1106
1036
|
// src/tui/launcher.ts
|
|
1107
|
-
import { spawn
|
|
1037
|
+
import { spawn } from "child_process";
|
|
1108
1038
|
import fsSync from "fs";
|
|
1109
1039
|
import { createRequire } from "module";
|
|
1110
1040
|
import path3 from "path";
|
|
1111
1041
|
import { ensureStorageDirs, resolveStoragePaths as resolveStoragePaths3 } from "@codelia/storage";
|
|
1112
1042
|
|
|
1113
1043
|
// src/version.ts
|
|
1114
|
-
var CLI_VERSION = "0.1.
|
|
1044
|
+
var CLI_VERSION = "0.1.86".trim().length > 0 ? "0.1.86" : "0.0.0";
|
|
1115
1045
|
|
|
1116
1046
|
// src/tui/launcher.ts
|
|
1117
1047
|
var resolveTuiBinaryName = (platform) => platform === "win32" ? "codelia-tui.exe" : "codelia-tui";
|
|
@@ -1122,7 +1052,7 @@ var TUI_PACKAGE_BY_TARGET = {
|
|
|
1122
1052
|
"linux-x64": "@codelia/tui-linux-x64",
|
|
1123
1053
|
"win32-x64": "@codelia/tui-win32-x64"
|
|
1124
1054
|
};
|
|
1125
|
-
var
|
|
1055
|
+
var describeError2 = (error) => error instanceof Error ? error.message : String(error);
|
|
1126
1056
|
var splitArgs = (value) => value.split(/\s+/).map((part) => part.trim()).filter((part) => part.length > 0);
|
|
1127
1057
|
var quoteShellWord = (value) => `'${value.replace(/'/g, "'\\''")}'`;
|
|
1128
1058
|
var isExecutable = (candidate) => {
|
|
@@ -1139,7 +1069,7 @@ var initStorage = async () => {
|
|
|
1139
1069
|
await ensureStorageDirs(paths);
|
|
1140
1070
|
} catch (error) {
|
|
1141
1071
|
console.warn(
|
|
1142
|
-
`Failed to initialize storage directories: ${
|
|
1072
|
+
`Failed to initialize storage directories: ${describeError2(error)}`
|
|
1143
1073
|
);
|
|
1144
1074
|
}
|
|
1145
1075
|
};
|
|
@@ -1269,7 +1199,7 @@ var runTui = (args2) => {
|
|
|
1269
1199
|
void initStorage();
|
|
1270
1200
|
const tuiCmd = resolveTuiCommand();
|
|
1271
1201
|
const tuiArgs = process.env.CODELIA_TUI_ARGS ? splitArgs(process.env.CODELIA_TUI_ARGS) : [];
|
|
1272
|
-
const child =
|
|
1202
|
+
const child = spawn(tuiCmd, [...tuiArgs, ...args2], {
|
|
1273
1203
|
stdio: "inherit",
|
|
1274
1204
|
env: resolveLaunchEnvForTui(process.env)
|
|
1275
1205
|
});
|
|
@@ -1279,7 +1209,7 @@ var runTui = (args2) => {
|
|
|
1279
1209
|
child.on("error", (error) => {
|
|
1280
1210
|
const code = typeof error === "object" && error !== null && "code" in error ? String(error.code ?? "") : "";
|
|
1281
1211
|
const expectedPackage = resolvePlatformTuiPackageName();
|
|
1282
|
-
console.error(`Failed to launch TUI (${tuiCmd}): ${
|
|
1212
|
+
console.error(`Failed to launch TUI (${tuiCmd}): ${describeError2(error)}`);
|
|
1283
1213
|
if (code === "EACCES") {
|
|
1284
1214
|
console.error(
|
|
1285
1215
|
"Permission denied while launching TUI. Verify execute permission and PATH entries."
|
|
@@ -1415,10 +1345,33 @@ var summarizeProgressEvent = (event) => {
|
|
|
1415
1345
|
};
|
|
1416
1346
|
var isRpcResponse = (value) => typeof value === "object" && value !== null && "jsonrpc" in value && "id" in value && !value.method;
|
|
1417
1347
|
var isRpcNotification = (value) => typeof value === "object" && value !== null && "jsonrpc" in value && "method" in value && !value.id;
|
|
1348
|
+
var asRecord2 = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
1349
|
+
var harborUiResponse = (value, runId, allowShell) => {
|
|
1350
|
+
const request = asRecord2(value);
|
|
1351
|
+
if (request?.jsonrpc !== "2.0" || typeof request.id !== "string" || !/^ui_[1-9]\d*$/u.test(request.id))
|
|
1352
|
+
return void 0;
|
|
1353
|
+
const response = { jsonrpc: "2.0", id: request.id };
|
|
1354
|
+
switch (request.method) {
|
|
1355
|
+
case "ui.confirm.request":
|
|
1356
|
+
return { ...response, result: { ok: false } };
|
|
1357
|
+
case "ui.prompt.request":
|
|
1358
|
+
return { ...response, result: { value: null } };
|
|
1359
|
+
case "ui.pick.request": {
|
|
1360
|
+
const params = asRecord2(request.params);
|
|
1361
|
+
const choices = allowShell && runId !== null && params?.run_id === runId && params.approval === true && params.title === "Approve shell?" && (params.multi === void 0 || params.multi === false) && Array.isArray(params.items) ? params.items : [];
|
|
1362
|
+
const once = choices.map((item) => asRecord2(item)?.id).filter(
|
|
1363
|
+
(id) => typeof id === "string" && /^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}\/allow_once$/u.test(id)
|
|
1364
|
+
);
|
|
1365
|
+
return { ...response, result: { ids: once.length === 1 ? once : [] } };
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
};
|
|
1418
1369
|
var writePromptModeAtif = async ({
|
|
1419
1370
|
atifOut,
|
|
1420
1371
|
sessionLogPath,
|
|
1421
|
-
runId
|
|
1372
|
+
runId,
|
|
1373
|
+
sandboxModeRequested,
|
|
1374
|
+
sandboxModeReported
|
|
1422
1375
|
}) => {
|
|
1423
1376
|
if (!atifOut) {
|
|
1424
1377
|
return "skipped";
|
|
@@ -1429,7 +1382,13 @@ var writePromptModeAtif = async ({
|
|
|
1429
1382
|
await writeAtifFromSessionJsonl({
|
|
1430
1383
|
sessionLogPath,
|
|
1431
1384
|
runId,
|
|
1432
|
-
outPath: atifOut
|
|
1385
|
+
outPath: atifOut,
|
|
1386
|
+
...sandboxModeRequested ? {
|
|
1387
|
+
extra: {
|
|
1388
|
+
sandbox_mode_requested: sandboxModeRequested,
|
|
1389
|
+
sandbox_mode: sandboxModeReported ?? null
|
|
1390
|
+
}
|
|
1391
|
+
} : {}
|
|
1433
1392
|
});
|
|
1434
1393
|
return "written";
|
|
1435
1394
|
};
|
|
@@ -1438,6 +1397,7 @@ var runPromptMode = async (options) => {
|
|
|
1438
1397
|
process.env.CODELIA_PROMPT_PROGRESS_STDERR
|
|
1439
1398
|
);
|
|
1440
1399
|
const atifOut = process.env.CODELIA_ATIF_OUT?.trim();
|
|
1400
|
+
const harborShellRequested = options.harborShellApproval === true && options.sandboxMode === "full-access" && process.env.CODELIA_HARBOR_SHELL_APPROVAL === "1";
|
|
1441
1401
|
const runtimeEnv = resolveRuntimeEnvForTui(process.env);
|
|
1442
1402
|
const runtimeCmd = runtimeEnv.CODELIA_RUNTIME_CMD ?? process.execPath;
|
|
1443
1403
|
const runtimeArgsValue = runtimeEnv.CODELIA_RUNTIME_ARGS ?? "packages/runtime/src/index.ts";
|
|
@@ -1445,15 +1405,20 @@ var runPromptMode = async (options) => {
|
|
|
1445
1405
|
if (runtimeArgs.length === 0) {
|
|
1446
1406
|
runtimeArgs.push("packages/runtime/src/index.ts");
|
|
1447
1407
|
}
|
|
1408
|
+
if (options.sandboxMode) {
|
|
1409
|
+
runtimeArgs.push("--sandbox-mode", options.sandboxMode);
|
|
1410
|
+
}
|
|
1448
1411
|
if (options.approvalMode) {
|
|
1449
1412
|
runtimeArgs.push("--approval-mode", options.approvalMode);
|
|
1450
1413
|
}
|
|
1451
|
-
const child =
|
|
1414
|
+
const child = spawn2(runtimeCmd, runtimeArgs, {
|
|
1452
1415
|
env: runtimeEnv,
|
|
1453
1416
|
stdio: ["pipe", "pipe", "pipe"]
|
|
1454
1417
|
});
|
|
1455
1418
|
let buffer = "";
|
|
1456
1419
|
let runId = null;
|
|
1420
|
+
let reportedSandbox = null;
|
|
1421
|
+
let harborShellAllowed = false;
|
|
1457
1422
|
let sessionLogPath = null;
|
|
1458
1423
|
let finalText = "";
|
|
1459
1424
|
let terminalStatus = null;
|
|
@@ -1478,7 +1443,7 @@ var runPromptMode = async (options) => {
|
|
|
1478
1443
|
pendingResponses.set(id, { resolve, reject });
|
|
1479
1444
|
});
|
|
1480
1445
|
};
|
|
1481
|
-
const
|
|
1446
|
+
const sendMessage = (request) => {
|
|
1482
1447
|
if (startupFailure) {
|
|
1483
1448
|
throw startupFailure;
|
|
1484
1449
|
}
|
|
@@ -1493,7 +1458,9 @@ var runPromptMode = async (options) => {
|
|
|
1493
1458
|
atifWriteInFlight ??= writePromptModeAtif({
|
|
1494
1459
|
atifOut,
|
|
1495
1460
|
sessionLogPath,
|
|
1496
|
-
runId
|
|
1461
|
+
runId,
|
|
1462
|
+
sandboxModeRequested: options.sandboxMode,
|
|
1463
|
+
sandboxModeReported: reportedSandbox
|
|
1497
1464
|
});
|
|
1498
1465
|
const result = await atifWriteInFlight;
|
|
1499
1466
|
if (result === "written") {
|
|
@@ -1600,9 +1567,21 @@ var runPromptMode = async (options) => {
|
|
|
1600
1567
|
const pending = pendingResponses.get(String(parsed.id));
|
|
1601
1568
|
if (pending) {
|
|
1602
1569
|
pendingResponses.delete(String(parsed.id));
|
|
1570
|
+
if (parsed.id === "run-1") {
|
|
1571
|
+
const result = asRecord2(parsed.result);
|
|
1572
|
+
runId = typeof result?.run_id === "string" ? result.run_id : null;
|
|
1573
|
+
}
|
|
1603
1574
|
pending.resolve(parsed);
|
|
1604
1575
|
}
|
|
1605
1576
|
}
|
|
1577
|
+
if (harborShellRequested) {
|
|
1578
|
+
const response = harborUiResponse(
|
|
1579
|
+
parsed,
|
|
1580
|
+
terminalStatus ? null : runId,
|
|
1581
|
+
harborShellAllowed
|
|
1582
|
+
);
|
|
1583
|
+
if (response) sendMessage(response);
|
|
1584
|
+
}
|
|
1606
1585
|
maybeTrackRunLifecycle(parsed);
|
|
1607
1586
|
} catch {
|
|
1608
1587
|
}
|
|
@@ -1615,7 +1594,7 @@ var runPromptMode = async (options) => {
|
|
|
1615
1594
|
process.stderr.write(chunk);
|
|
1616
1595
|
});
|
|
1617
1596
|
try {
|
|
1618
|
-
|
|
1597
|
+
sendMessage({
|
|
1619
1598
|
jsonrpc: "2.0",
|
|
1620
1599
|
id: "init-1",
|
|
1621
1600
|
method: "initialize",
|
|
@@ -1625,7 +1604,8 @@ var runPromptMode = async (options) => {
|
|
|
1625
1604
|
ui_capabilities: {
|
|
1626
1605
|
supports_confirm: false,
|
|
1627
1606
|
supports_prompt: false,
|
|
1628
|
-
supports_pick:
|
|
1607
|
+
supports_pick: harborShellRequested,
|
|
1608
|
+
...harborShellRequested ? { supports_structured_approval: true } : {}
|
|
1629
1609
|
}
|
|
1630
1610
|
}
|
|
1631
1611
|
});
|
|
@@ -1633,7 +1613,11 @@ var runPromptMode = async (options) => {
|
|
|
1633
1613
|
if (initResponse.error) {
|
|
1634
1614
|
throw new Error(`initialize failed: ${initResponse.error.message}`);
|
|
1635
1615
|
}
|
|
1636
|
-
|
|
1616
|
+
const init = initResponse.result;
|
|
1617
|
+
const mode = init?.sandbox_mode?.value;
|
|
1618
|
+
reportedSandbox = mode === "readonly" || mode === "workspace-write" || mode === "full-access" ? mode : null;
|
|
1619
|
+
harborShellAllowed = harborShellRequested && init?.server_capabilities?.supports_structured_approval === true && reportedSandbox === "full-access";
|
|
1620
|
+
sendMessage({
|
|
1637
1621
|
jsonrpc: "2.0",
|
|
1638
1622
|
id: "run-1",
|
|
1639
1623
|
method: "run.start",
|
|
@@ -1646,7 +1630,6 @@ var runPromptMode = async (options) => {
|
|
|
1646
1630
|
throw new Error(`run.start failed: ${runResponse.error.message}`);
|
|
1647
1631
|
}
|
|
1648
1632
|
const runResult = typeof runResponse.result === "object" && runResponse.result !== null ? runResponse.result : null;
|
|
1649
|
-
runId = typeof runResult?.run_id === "string" ? runResult.run_id : null;
|
|
1650
1633
|
sessionLogPath = typeof runResult?.session_log_path === "string" ? runResult.session_log_path : null;
|
|
1651
1634
|
if (!runId) {
|
|
1652
1635
|
throw new Error("run.start did not return run_id");
|
|
@@ -1726,10 +1709,13 @@ var main = async () => {
|
|
|
1726
1709
|
}
|
|
1727
1710
|
case "prompt": {
|
|
1728
1711
|
const prompt = validatePromptText(resolvePromptText(args));
|
|
1729
|
-
const
|
|
1712
|
+
const sandboxMode = resolvePromptModeSandbox(args);
|
|
1713
|
+
const approvalMode = sandboxMode ? void 0 : resolvePromptModeApproval(args);
|
|
1730
1714
|
const exitCode = await runPromptMode({
|
|
1731
1715
|
prompt,
|
|
1732
|
-
approvalMode
|
|
1716
|
+
approvalMode,
|
|
1717
|
+
sandboxMode,
|
|
1718
|
+
harborShellApproval: hasHarborShellApprovalFlag(args)
|
|
1733
1719
|
});
|
|
1734
1720
|
process.exitCode = exitCode;
|
|
1735
1721
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codelia/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.86",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -20,19 +20,19 @@
|
|
|
20
20
|
}
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@codelia/config-loader": "0.1.
|
|
24
|
-
"@codelia/protocol": "0.1.
|
|
25
|
-
"@codelia/runtime": "0.1.
|
|
26
|
-
"@codelia/storage": "0.1.
|
|
23
|
+
"@codelia/config-loader": "0.1.86",
|
|
24
|
+
"@codelia/protocol": "0.1.86",
|
|
25
|
+
"@codelia/runtime": "0.1.86",
|
|
26
|
+
"@codelia/storage": "0.1.86",
|
|
27
27
|
"cac": "^6.7.14",
|
|
28
28
|
"zod": "^4.3.6"
|
|
29
29
|
},
|
|
30
30
|
"optionalDependencies": {
|
|
31
|
-
"@codelia/tui-darwin-arm64": "0.1.
|
|
32
|
-
"@codelia/tui-darwin-x64": "0.1.
|
|
33
|
-
"@codelia/tui-linux-arm64": "0.1.
|
|
34
|
-
"@codelia/tui-linux-x64": "0.1.
|
|
35
|
-
"@codelia/tui-win32-x64": "0.1.
|
|
31
|
+
"@codelia/tui-darwin-arm64": "0.1.86",
|
|
32
|
+
"@codelia/tui-darwin-x64": "0.1.86",
|
|
33
|
+
"@codelia/tui-linux-arm64": "0.1.86",
|
|
34
|
+
"@codelia/tui-linux-x64": "0.1.86",
|
|
35
|
+
"@codelia/tui-win32-x64": "0.1.86"
|
|
36
36
|
},
|
|
37
37
|
"overrides": {
|
|
38
38
|
"gaxios": "7.1.4"
|