@codelia/cli 0.1.20 → 0.1.22
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 +331 -2
- package/dist/index.js +331 -2
- package/package.json +10 -10
package/dist/index.cjs
CHANGED
|
@@ -30,20 +30,40 @@ var TOP_LEVEL_HELP_TEXT = [
|
|
|
30
30
|
"Top-level options:",
|
|
31
31
|
" -h, --help Show this help",
|
|
32
32
|
" -V, -v, --version Show codelia version",
|
|
33
|
+
" -p, --prompt <text> Run one headless prompt and exit",
|
|
33
34
|
"",
|
|
34
35
|
"Commands:",
|
|
35
36
|
" mcp ... Manage MCP servers and auth",
|
|
36
37
|
"",
|
|
38
|
+
"Runtime options:",
|
|
39
|
+
" --approval-mode <minimal|trusted|full-access>",
|
|
40
|
+
"",
|
|
37
41
|
"TUI options (passed through):",
|
|
38
42
|
" -r, --resume [session_id]",
|
|
39
43
|
" --debug[=true|false]",
|
|
40
44
|
" --diagnostics[=true|false]",
|
|
41
45
|
" --initial-message <text>",
|
|
42
46
|
" --initial-user-message <text>",
|
|
43
|
-
" --debug-perf[=true|false]"
|
|
47
|
+
" --debug-perf[=true|false]",
|
|
48
|
+
" --approval-mode <minimal|trusted|full-access>"
|
|
44
49
|
].join("\n");
|
|
45
50
|
var isHelpFlag = (value) => value === "-h" || value === "--help" || value === "help";
|
|
46
51
|
var isVersionFlag = (value) => value === "-V" || value === "-v" || value === "--version" || value === "version";
|
|
52
|
+
var hasPromptFlag = (args2) => {
|
|
53
|
+
for (let i = 0; i < args2.length; i += 1) {
|
|
54
|
+
const current = args2[i];
|
|
55
|
+
if (current === "--") {
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
if (current === "-p" || current === "--prompt") {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
if (current.startsWith("--prompt=")) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
};
|
|
47
67
|
var resolveTopLevelAction = (args2) => {
|
|
48
68
|
if (args2[0] === "mcp") {
|
|
49
69
|
return "mcp";
|
|
@@ -54,8 +74,59 @@ var resolveTopLevelAction = (args2) => {
|
|
|
54
74
|
if (args2.length === 1 && isVersionFlag(args2[0])) {
|
|
55
75
|
return "version";
|
|
56
76
|
}
|
|
77
|
+
if (hasPromptFlag(args2)) {
|
|
78
|
+
return "prompt";
|
|
79
|
+
}
|
|
57
80
|
return "tui";
|
|
58
81
|
};
|
|
82
|
+
var resolvePromptText = (args2) => {
|
|
83
|
+
for (let i = 0; i < args2.length; i += 1) {
|
|
84
|
+
const current = args2[i];
|
|
85
|
+
if (current === "-p" || current === "--prompt") {
|
|
86
|
+
const next = args2[i + 1];
|
|
87
|
+
if (!next) return void 0;
|
|
88
|
+
return next;
|
|
89
|
+
}
|
|
90
|
+
if (current.startsWith("--prompt=")) {
|
|
91
|
+
return current.slice("--prompt=".length);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return void 0;
|
|
95
|
+
};
|
|
96
|
+
var resolvePromptModeApproval = (args2) => {
|
|
97
|
+
for (let i = 0; i < args2.length; i += 1) {
|
|
98
|
+
const current = args2[i];
|
|
99
|
+
if (current === "--") {
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
if (current === "--approval-mode") {
|
|
103
|
+
const next = args2[i + 1];
|
|
104
|
+
if (!next || next.trim().length === 0) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
"--approval-mode requires a value (minimal|trusted|full-access)"
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
return next.trim();
|
|
110
|
+
}
|
|
111
|
+
if (current.startsWith("--approval-mode=")) {
|
|
112
|
+
const value = current.slice("--approval-mode=".length).trim();
|
|
113
|
+
if (value.length === 0) {
|
|
114
|
+
throw new Error(
|
|
115
|
+
"--approval-mode requires a value (minimal|trusted|full-access)"
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return void 0;
|
|
122
|
+
};
|
|
123
|
+
var validatePromptText = (value) => {
|
|
124
|
+
const text = value?.trim() ?? "";
|
|
125
|
+
if (text.length === 0) {
|
|
126
|
+
throw new Error("Prompt is required after -p/--prompt");
|
|
127
|
+
}
|
|
128
|
+
return text;
|
|
129
|
+
};
|
|
59
130
|
|
|
60
131
|
// src/args.ts
|
|
61
132
|
var import_cac = require("cac");
|
|
@@ -764,6 +835,9 @@ var runMcpCommand = async (values) => {
|
|
|
764
835
|
return 1;
|
|
765
836
|
};
|
|
766
837
|
|
|
838
|
+
// src/prompt-mode.ts
|
|
839
|
+
var import_node_child_process3 = require("child_process");
|
|
840
|
+
|
|
767
841
|
// src/tui/launcher.ts
|
|
768
842
|
var import_node_child_process2 = require("child_process");
|
|
769
843
|
var import_node_fs = __toESM(require("fs"), 1);
|
|
@@ -772,7 +846,7 @@ var import_node_path2 = __toESM(require("path"), 1);
|
|
|
772
846
|
var import_storage3 = require("@codelia/storage");
|
|
773
847
|
|
|
774
848
|
// src/version.ts
|
|
775
|
-
var CLI_VERSION = "0.1.
|
|
849
|
+
var CLI_VERSION = "0.1.22".trim().length > 0 ? "0.1.22" : "0.0.0";
|
|
776
850
|
|
|
777
851
|
// src/tui/launcher.ts
|
|
778
852
|
var resolveTuiBinaryName = (platform) => platform === "win32" ? "codelia-tui.exe" : "codelia-tui";
|
|
@@ -957,6 +1031,251 @@ var runTui = (args2) => {
|
|
|
957
1031
|
});
|
|
958
1032
|
};
|
|
959
1033
|
|
|
1034
|
+
// src/prompt-mode.ts
|
|
1035
|
+
var parseShellLikeArgs = (value) => {
|
|
1036
|
+
const out = [];
|
|
1037
|
+
let current = "";
|
|
1038
|
+
let quote = null;
|
|
1039
|
+
let escaping = false;
|
|
1040
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
1041
|
+
const ch = value[i];
|
|
1042
|
+
if (escaping) {
|
|
1043
|
+
current += ch;
|
|
1044
|
+
escaping = false;
|
|
1045
|
+
continue;
|
|
1046
|
+
}
|
|
1047
|
+
if (ch === "\\" && quote !== "single") {
|
|
1048
|
+
escaping = true;
|
|
1049
|
+
continue;
|
|
1050
|
+
}
|
|
1051
|
+
if (quote === "single") {
|
|
1052
|
+
if (ch === "'") {
|
|
1053
|
+
quote = null;
|
|
1054
|
+
} else {
|
|
1055
|
+
current += ch;
|
|
1056
|
+
}
|
|
1057
|
+
continue;
|
|
1058
|
+
}
|
|
1059
|
+
if (quote === "double") {
|
|
1060
|
+
if (ch === '"') {
|
|
1061
|
+
quote = null;
|
|
1062
|
+
} else {
|
|
1063
|
+
current += ch;
|
|
1064
|
+
}
|
|
1065
|
+
continue;
|
|
1066
|
+
}
|
|
1067
|
+
if (ch === "'") {
|
|
1068
|
+
quote = "single";
|
|
1069
|
+
continue;
|
|
1070
|
+
}
|
|
1071
|
+
if (ch === '"') {
|
|
1072
|
+
quote = "double";
|
|
1073
|
+
continue;
|
|
1074
|
+
}
|
|
1075
|
+
if (/\s/.test(ch)) {
|
|
1076
|
+
if (current.length > 0) {
|
|
1077
|
+
out.push(current);
|
|
1078
|
+
current = "";
|
|
1079
|
+
}
|
|
1080
|
+
continue;
|
|
1081
|
+
}
|
|
1082
|
+
current += ch;
|
|
1083
|
+
}
|
|
1084
|
+
if (current.length > 0) {
|
|
1085
|
+
out.push(current);
|
|
1086
|
+
}
|
|
1087
|
+
return out;
|
|
1088
|
+
};
|
|
1089
|
+
var toLine = (value) => `${JSON.stringify(value)}
|
|
1090
|
+
`;
|
|
1091
|
+
var isRpcResponse = (value) => typeof value === "object" && value !== null && "jsonrpc" in value && "id" in value && !value.method;
|
|
1092
|
+
var isRpcNotification = (value) => typeof value === "object" && value !== null && "jsonrpc" in value && "method" in value && !value.id;
|
|
1093
|
+
var runPromptMode = async (options) => {
|
|
1094
|
+
const runtimeEnv = resolveRuntimeEnvForTui(process.env);
|
|
1095
|
+
const runtimeCmd = runtimeEnv.CODELIA_RUNTIME_CMD ?? process.execPath;
|
|
1096
|
+
const runtimeArgsValue = runtimeEnv.CODELIA_RUNTIME_ARGS ?? "packages/runtime/src/index.ts";
|
|
1097
|
+
const runtimeArgs = parseShellLikeArgs(runtimeArgsValue);
|
|
1098
|
+
if (runtimeArgs.length === 0) {
|
|
1099
|
+
runtimeArgs.push("packages/runtime/src/index.ts");
|
|
1100
|
+
}
|
|
1101
|
+
if (options.approvalMode) {
|
|
1102
|
+
runtimeArgs.push("--approval-mode", options.approvalMode);
|
|
1103
|
+
}
|
|
1104
|
+
const child = (0, import_node_child_process3.spawn)(runtimeCmd, runtimeArgs, {
|
|
1105
|
+
env: runtimeEnv,
|
|
1106
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
1107
|
+
});
|
|
1108
|
+
let buffer = "";
|
|
1109
|
+
let runId = null;
|
|
1110
|
+
let finalText = "";
|
|
1111
|
+
let terminalStatus = null;
|
|
1112
|
+
let terminalMessage;
|
|
1113
|
+
const pendingResponses = /* @__PURE__ */ new Map();
|
|
1114
|
+
let startupFailure = null;
|
|
1115
|
+
const failPending = (error) => {
|
|
1116
|
+
if (startupFailure) return;
|
|
1117
|
+
startupFailure = error;
|
|
1118
|
+
for (const pending of pendingResponses.values()) {
|
|
1119
|
+
pending.reject(error);
|
|
1120
|
+
}
|
|
1121
|
+
pendingResponses.clear();
|
|
1122
|
+
};
|
|
1123
|
+
const waitResponse = (id) => {
|
|
1124
|
+
if (startupFailure) {
|
|
1125
|
+
return Promise.reject(startupFailure);
|
|
1126
|
+
}
|
|
1127
|
+
return new Promise((resolve, reject) => {
|
|
1128
|
+
pendingResponses.set(id, { resolve, reject });
|
|
1129
|
+
});
|
|
1130
|
+
};
|
|
1131
|
+
const sendRequest = (request) => {
|
|
1132
|
+
if (startupFailure) {
|
|
1133
|
+
throw startupFailure;
|
|
1134
|
+
}
|
|
1135
|
+
const wrote = child.stdin.write(toLine(request));
|
|
1136
|
+
if (!wrote) {
|
|
1137
|
+
}
|
|
1138
|
+
};
|
|
1139
|
+
const maybeTrackRunLifecycle = (value) => {
|
|
1140
|
+
if (!isRpcNotification(value)) return;
|
|
1141
|
+
if (value.method === "run.status") {
|
|
1142
|
+
const params = value.params ?? {};
|
|
1143
|
+
if (!runId || params.run_id !== runId) return;
|
|
1144
|
+
if (params.status === "completed" || params.status === "error" || params.status === "cancelled") {
|
|
1145
|
+
terminalStatus = params.status;
|
|
1146
|
+
terminalMessage = params.message;
|
|
1147
|
+
}
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
if (value.method === "run.end") {
|
|
1151
|
+
const params = value.params ?? {};
|
|
1152
|
+
if (!runId || params.run_id !== runId) return;
|
|
1153
|
+
if (typeof params.final === "string") {
|
|
1154
|
+
finalText = params.final;
|
|
1155
|
+
}
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
if (value.method === "agent.event") {
|
|
1159
|
+
const params = value.params ?? {};
|
|
1160
|
+
if (!runId || params.run_id !== runId) return;
|
|
1161
|
+
if (params.event?.type === "final" && typeof params.event.content === "string") {
|
|
1162
|
+
finalText = params.event.content;
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
};
|
|
1166
|
+
child.on("error", (error) => {
|
|
1167
|
+
failPending(
|
|
1168
|
+
error instanceof Error ? error : new Error(`runtime process error: ${String(error)}`)
|
|
1169
|
+
);
|
|
1170
|
+
});
|
|
1171
|
+
child.on("close", (code, signal) => {
|
|
1172
|
+
if (terminalStatus) return;
|
|
1173
|
+
failPending(
|
|
1174
|
+
new Error(
|
|
1175
|
+
`runtime exited before completion (code=${String(code)} signal=${String(signal)})`
|
|
1176
|
+
)
|
|
1177
|
+
);
|
|
1178
|
+
});
|
|
1179
|
+
child.stdin.on("error", (error) => {
|
|
1180
|
+
failPending(
|
|
1181
|
+
error instanceof Error ? error : new Error(`runtime stdin error: ${String(error)}`)
|
|
1182
|
+
);
|
|
1183
|
+
});
|
|
1184
|
+
child.stdout.setEncoding("utf8");
|
|
1185
|
+
child.stdout.on("data", (chunk) => {
|
|
1186
|
+
buffer += chunk;
|
|
1187
|
+
let index = buffer.indexOf("\n");
|
|
1188
|
+
while (index >= 0) {
|
|
1189
|
+
const line = buffer.slice(0, index).trim();
|
|
1190
|
+
buffer = buffer.slice(index + 1);
|
|
1191
|
+
if (line.length > 0) {
|
|
1192
|
+
try {
|
|
1193
|
+
const parsed = JSON.parse(line);
|
|
1194
|
+
if (isRpcResponse(parsed)) {
|
|
1195
|
+
const pending = pendingResponses.get(String(parsed.id));
|
|
1196
|
+
if (pending) {
|
|
1197
|
+
pendingResponses.delete(String(parsed.id));
|
|
1198
|
+
pending.resolve(parsed);
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
maybeTrackRunLifecycle(parsed);
|
|
1202
|
+
} catch {
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
index = buffer.indexOf("\n");
|
|
1206
|
+
}
|
|
1207
|
+
});
|
|
1208
|
+
try {
|
|
1209
|
+
sendRequest({
|
|
1210
|
+
jsonrpc: "2.0",
|
|
1211
|
+
id: "init-1",
|
|
1212
|
+
method: "initialize",
|
|
1213
|
+
params: {
|
|
1214
|
+
protocol_version: "0",
|
|
1215
|
+
client: { name: "codelia-cli-prompt", version: "0.1.0" },
|
|
1216
|
+
ui_capabilities: {
|
|
1217
|
+
supports_confirm: false,
|
|
1218
|
+
supports_prompt: false,
|
|
1219
|
+
supports_pick: false
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
});
|
|
1223
|
+
const initResponse = await waitResponse("init-1");
|
|
1224
|
+
if (initResponse.error) {
|
|
1225
|
+
throw new Error(`initialize failed: ${initResponse.error.message}`);
|
|
1226
|
+
}
|
|
1227
|
+
sendRequest({
|
|
1228
|
+
jsonrpc: "2.0",
|
|
1229
|
+
id: "run-1",
|
|
1230
|
+
method: "run.start",
|
|
1231
|
+
params: {
|
|
1232
|
+
input: { type: "text", text: options.prompt }
|
|
1233
|
+
}
|
|
1234
|
+
});
|
|
1235
|
+
const runResponse = await waitResponse("run-1");
|
|
1236
|
+
if (runResponse.error) {
|
|
1237
|
+
throw new Error(`run.start failed: ${runResponse.error.message}`);
|
|
1238
|
+
}
|
|
1239
|
+
const runResult = typeof runResponse.result === "object" && runResponse.result !== null ? runResponse.result : null;
|
|
1240
|
+
runId = typeof runResult?.run_id === "string" ? runResult.run_id : null;
|
|
1241
|
+
if (!runId) {
|
|
1242
|
+
throw new Error("run.start did not return run_id");
|
|
1243
|
+
}
|
|
1244
|
+
await new Promise((resolve, reject) => {
|
|
1245
|
+
const timeout = setInterval(() => {
|
|
1246
|
+
if (terminalStatus) {
|
|
1247
|
+
clearInterval(timeout);
|
|
1248
|
+
resolve();
|
|
1249
|
+
return;
|
|
1250
|
+
}
|
|
1251
|
+
if (startupFailure) {
|
|
1252
|
+
clearInterval(timeout);
|
|
1253
|
+
reject(startupFailure);
|
|
1254
|
+
}
|
|
1255
|
+
}, 50);
|
|
1256
|
+
});
|
|
1257
|
+
} catch (error) {
|
|
1258
|
+
console.error(
|
|
1259
|
+
`Prompt run failed: ${error instanceof Error ? error.message : String(error)}`
|
|
1260
|
+
);
|
|
1261
|
+
return 1;
|
|
1262
|
+
} finally {
|
|
1263
|
+
if (!child.killed) {
|
|
1264
|
+
child.kill();
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
if (terminalStatus === "completed") {
|
|
1268
|
+
if (finalText.trim().length > 0) {
|
|
1269
|
+
console.log(finalText);
|
|
1270
|
+
}
|
|
1271
|
+
return 0;
|
|
1272
|
+
}
|
|
1273
|
+
if (terminalMessage) {
|
|
1274
|
+
console.error(terminalMessage);
|
|
1275
|
+
}
|
|
1276
|
+
return terminalStatus === "cancelled" ? 130 : 1;
|
|
1277
|
+
};
|
|
1278
|
+
|
|
960
1279
|
// src/index.ts
|
|
961
1280
|
var args = process.argv.slice(2);
|
|
962
1281
|
var main = async () => {
|
|
@@ -972,6 +1291,16 @@ var main = async () => {
|
|
|
972
1291
|
process.exitCode = exitCode;
|
|
973
1292
|
return;
|
|
974
1293
|
}
|
|
1294
|
+
case "prompt": {
|
|
1295
|
+
const prompt = validatePromptText(resolvePromptText(args));
|
|
1296
|
+
const approvalMode = resolvePromptModeApproval(args);
|
|
1297
|
+
const exitCode = await runPromptMode({
|
|
1298
|
+
prompt,
|
|
1299
|
+
approvalMode
|
|
1300
|
+
});
|
|
1301
|
+
process.exitCode = exitCode;
|
|
1302
|
+
return;
|
|
1303
|
+
}
|
|
975
1304
|
case "tui":
|
|
976
1305
|
runTui(args);
|
|
977
1306
|
return;
|
package/dist/index.js
CHANGED
|
@@ -5,20 +5,40 @@ var TOP_LEVEL_HELP_TEXT = [
|
|
|
5
5
|
"Top-level options:",
|
|
6
6
|
" -h, --help Show this help",
|
|
7
7
|
" -V, -v, --version Show codelia version",
|
|
8
|
+
" -p, --prompt <text> Run one headless prompt and exit",
|
|
8
9
|
"",
|
|
9
10
|
"Commands:",
|
|
10
11
|
" mcp ... Manage MCP servers and auth",
|
|
11
12
|
"",
|
|
13
|
+
"Runtime options:",
|
|
14
|
+
" --approval-mode <minimal|trusted|full-access>",
|
|
15
|
+
"",
|
|
12
16
|
"TUI options (passed through):",
|
|
13
17
|
" -r, --resume [session_id]",
|
|
14
18
|
" --debug[=true|false]",
|
|
15
19
|
" --diagnostics[=true|false]",
|
|
16
20
|
" --initial-message <text>",
|
|
17
21
|
" --initial-user-message <text>",
|
|
18
|
-
" --debug-perf[=true|false]"
|
|
22
|
+
" --debug-perf[=true|false]",
|
|
23
|
+
" --approval-mode <minimal|trusted|full-access>"
|
|
19
24
|
].join("\n");
|
|
20
25
|
var isHelpFlag = (value) => value === "-h" || value === "--help" || value === "help";
|
|
21
26
|
var isVersionFlag = (value) => value === "-V" || value === "-v" || value === "--version" || value === "version";
|
|
27
|
+
var hasPromptFlag = (args2) => {
|
|
28
|
+
for (let i = 0; i < args2.length; i += 1) {
|
|
29
|
+
const current = args2[i];
|
|
30
|
+
if (current === "--") {
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
if (current === "-p" || current === "--prompt") {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
if (current.startsWith("--prompt=")) {
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
};
|
|
22
42
|
var resolveTopLevelAction = (args2) => {
|
|
23
43
|
if (args2[0] === "mcp") {
|
|
24
44
|
return "mcp";
|
|
@@ -29,8 +49,59 @@ var resolveTopLevelAction = (args2) => {
|
|
|
29
49
|
if (args2.length === 1 && isVersionFlag(args2[0])) {
|
|
30
50
|
return "version";
|
|
31
51
|
}
|
|
52
|
+
if (hasPromptFlag(args2)) {
|
|
53
|
+
return "prompt";
|
|
54
|
+
}
|
|
32
55
|
return "tui";
|
|
33
56
|
};
|
|
57
|
+
var resolvePromptText = (args2) => {
|
|
58
|
+
for (let i = 0; i < args2.length; i += 1) {
|
|
59
|
+
const current = args2[i];
|
|
60
|
+
if (current === "-p" || current === "--prompt") {
|
|
61
|
+
const next = args2[i + 1];
|
|
62
|
+
if (!next) return void 0;
|
|
63
|
+
return next;
|
|
64
|
+
}
|
|
65
|
+
if (current.startsWith("--prompt=")) {
|
|
66
|
+
return current.slice("--prompt=".length);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return void 0;
|
|
70
|
+
};
|
|
71
|
+
var resolvePromptModeApproval = (args2) => {
|
|
72
|
+
for (let i = 0; i < args2.length; i += 1) {
|
|
73
|
+
const current = args2[i];
|
|
74
|
+
if (current === "--") {
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
if (current === "--approval-mode") {
|
|
78
|
+
const next = args2[i + 1];
|
|
79
|
+
if (!next || next.trim().length === 0) {
|
|
80
|
+
throw new Error(
|
|
81
|
+
"--approval-mode requires a value (minimal|trusted|full-access)"
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
return next.trim();
|
|
85
|
+
}
|
|
86
|
+
if (current.startsWith("--approval-mode=")) {
|
|
87
|
+
const value = current.slice("--approval-mode=".length).trim();
|
|
88
|
+
if (value.length === 0) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
"--approval-mode requires a value (minimal|trusted|full-access)"
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return value;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return void 0;
|
|
97
|
+
};
|
|
98
|
+
var validatePromptText = (value) => {
|
|
99
|
+
const text = value?.trim() ?? "";
|
|
100
|
+
if (text.length === 0) {
|
|
101
|
+
throw new Error("Prompt is required after -p/--prompt");
|
|
102
|
+
}
|
|
103
|
+
return text;
|
|
104
|
+
};
|
|
34
105
|
|
|
35
106
|
// src/args.ts
|
|
36
107
|
import { cac } from "cac";
|
|
@@ -748,6 +819,9 @@ var runMcpCommand = async (values) => {
|
|
|
748
819
|
return 1;
|
|
749
820
|
};
|
|
750
821
|
|
|
822
|
+
// src/prompt-mode.ts
|
|
823
|
+
import { spawn as spawn3 } from "child_process";
|
|
824
|
+
|
|
751
825
|
// src/tui/launcher.ts
|
|
752
826
|
import { spawn as spawn2 } from "child_process";
|
|
753
827
|
import fsSync from "fs";
|
|
@@ -756,7 +830,7 @@ import path2 from "path";
|
|
|
756
830
|
import { ensureStorageDirs, resolveStoragePaths as resolveStoragePaths2 } from "@codelia/storage";
|
|
757
831
|
|
|
758
832
|
// src/version.ts
|
|
759
|
-
var CLI_VERSION = "0.1.
|
|
833
|
+
var CLI_VERSION = "0.1.22".trim().length > 0 ? "0.1.22" : "0.0.0";
|
|
760
834
|
|
|
761
835
|
// src/tui/launcher.ts
|
|
762
836
|
var resolveTuiBinaryName = (platform) => platform === "win32" ? "codelia-tui.exe" : "codelia-tui";
|
|
@@ -941,6 +1015,251 @@ var runTui = (args2) => {
|
|
|
941
1015
|
});
|
|
942
1016
|
};
|
|
943
1017
|
|
|
1018
|
+
// src/prompt-mode.ts
|
|
1019
|
+
var parseShellLikeArgs = (value) => {
|
|
1020
|
+
const out = [];
|
|
1021
|
+
let current = "";
|
|
1022
|
+
let quote = null;
|
|
1023
|
+
let escaping = false;
|
|
1024
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
1025
|
+
const ch = value[i];
|
|
1026
|
+
if (escaping) {
|
|
1027
|
+
current += ch;
|
|
1028
|
+
escaping = false;
|
|
1029
|
+
continue;
|
|
1030
|
+
}
|
|
1031
|
+
if (ch === "\\" && quote !== "single") {
|
|
1032
|
+
escaping = true;
|
|
1033
|
+
continue;
|
|
1034
|
+
}
|
|
1035
|
+
if (quote === "single") {
|
|
1036
|
+
if (ch === "'") {
|
|
1037
|
+
quote = null;
|
|
1038
|
+
} else {
|
|
1039
|
+
current += ch;
|
|
1040
|
+
}
|
|
1041
|
+
continue;
|
|
1042
|
+
}
|
|
1043
|
+
if (quote === "double") {
|
|
1044
|
+
if (ch === '"') {
|
|
1045
|
+
quote = null;
|
|
1046
|
+
} else {
|
|
1047
|
+
current += ch;
|
|
1048
|
+
}
|
|
1049
|
+
continue;
|
|
1050
|
+
}
|
|
1051
|
+
if (ch === "'") {
|
|
1052
|
+
quote = "single";
|
|
1053
|
+
continue;
|
|
1054
|
+
}
|
|
1055
|
+
if (ch === '"') {
|
|
1056
|
+
quote = "double";
|
|
1057
|
+
continue;
|
|
1058
|
+
}
|
|
1059
|
+
if (/\s/.test(ch)) {
|
|
1060
|
+
if (current.length > 0) {
|
|
1061
|
+
out.push(current);
|
|
1062
|
+
current = "";
|
|
1063
|
+
}
|
|
1064
|
+
continue;
|
|
1065
|
+
}
|
|
1066
|
+
current += ch;
|
|
1067
|
+
}
|
|
1068
|
+
if (current.length > 0) {
|
|
1069
|
+
out.push(current);
|
|
1070
|
+
}
|
|
1071
|
+
return out;
|
|
1072
|
+
};
|
|
1073
|
+
var toLine = (value) => `${JSON.stringify(value)}
|
|
1074
|
+
`;
|
|
1075
|
+
var isRpcResponse = (value) => typeof value === "object" && value !== null && "jsonrpc" in value && "id" in value && !value.method;
|
|
1076
|
+
var isRpcNotification = (value) => typeof value === "object" && value !== null && "jsonrpc" in value && "method" in value && !value.id;
|
|
1077
|
+
var runPromptMode = async (options) => {
|
|
1078
|
+
const runtimeEnv = resolveRuntimeEnvForTui(process.env);
|
|
1079
|
+
const runtimeCmd = runtimeEnv.CODELIA_RUNTIME_CMD ?? process.execPath;
|
|
1080
|
+
const runtimeArgsValue = runtimeEnv.CODELIA_RUNTIME_ARGS ?? "packages/runtime/src/index.ts";
|
|
1081
|
+
const runtimeArgs = parseShellLikeArgs(runtimeArgsValue);
|
|
1082
|
+
if (runtimeArgs.length === 0) {
|
|
1083
|
+
runtimeArgs.push("packages/runtime/src/index.ts");
|
|
1084
|
+
}
|
|
1085
|
+
if (options.approvalMode) {
|
|
1086
|
+
runtimeArgs.push("--approval-mode", options.approvalMode);
|
|
1087
|
+
}
|
|
1088
|
+
const child = spawn3(runtimeCmd, runtimeArgs, {
|
|
1089
|
+
env: runtimeEnv,
|
|
1090
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
1091
|
+
});
|
|
1092
|
+
let buffer = "";
|
|
1093
|
+
let runId = null;
|
|
1094
|
+
let finalText = "";
|
|
1095
|
+
let terminalStatus = null;
|
|
1096
|
+
let terminalMessage;
|
|
1097
|
+
const pendingResponses = /* @__PURE__ */ new Map();
|
|
1098
|
+
let startupFailure = null;
|
|
1099
|
+
const failPending = (error) => {
|
|
1100
|
+
if (startupFailure) return;
|
|
1101
|
+
startupFailure = error;
|
|
1102
|
+
for (const pending of pendingResponses.values()) {
|
|
1103
|
+
pending.reject(error);
|
|
1104
|
+
}
|
|
1105
|
+
pendingResponses.clear();
|
|
1106
|
+
};
|
|
1107
|
+
const waitResponse = (id) => {
|
|
1108
|
+
if (startupFailure) {
|
|
1109
|
+
return Promise.reject(startupFailure);
|
|
1110
|
+
}
|
|
1111
|
+
return new Promise((resolve, reject) => {
|
|
1112
|
+
pendingResponses.set(id, { resolve, reject });
|
|
1113
|
+
});
|
|
1114
|
+
};
|
|
1115
|
+
const sendRequest = (request) => {
|
|
1116
|
+
if (startupFailure) {
|
|
1117
|
+
throw startupFailure;
|
|
1118
|
+
}
|
|
1119
|
+
const wrote = child.stdin.write(toLine(request));
|
|
1120
|
+
if (!wrote) {
|
|
1121
|
+
}
|
|
1122
|
+
};
|
|
1123
|
+
const maybeTrackRunLifecycle = (value) => {
|
|
1124
|
+
if (!isRpcNotification(value)) return;
|
|
1125
|
+
if (value.method === "run.status") {
|
|
1126
|
+
const params = value.params ?? {};
|
|
1127
|
+
if (!runId || params.run_id !== runId) return;
|
|
1128
|
+
if (params.status === "completed" || params.status === "error" || params.status === "cancelled") {
|
|
1129
|
+
terminalStatus = params.status;
|
|
1130
|
+
terminalMessage = params.message;
|
|
1131
|
+
}
|
|
1132
|
+
return;
|
|
1133
|
+
}
|
|
1134
|
+
if (value.method === "run.end") {
|
|
1135
|
+
const params = value.params ?? {};
|
|
1136
|
+
if (!runId || params.run_id !== runId) return;
|
|
1137
|
+
if (typeof params.final === "string") {
|
|
1138
|
+
finalText = params.final;
|
|
1139
|
+
}
|
|
1140
|
+
return;
|
|
1141
|
+
}
|
|
1142
|
+
if (value.method === "agent.event") {
|
|
1143
|
+
const params = value.params ?? {};
|
|
1144
|
+
if (!runId || params.run_id !== runId) return;
|
|
1145
|
+
if (params.event?.type === "final" && typeof params.event.content === "string") {
|
|
1146
|
+
finalText = params.event.content;
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
child.on("error", (error) => {
|
|
1151
|
+
failPending(
|
|
1152
|
+
error instanceof Error ? error : new Error(`runtime process error: ${String(error)}`)
|
|
1153
|
+
);
|
|
1154
|
+
});
|
|
1155
|
+
child.on("close", (code, signal) => {
|
|
1156
|
+
if (terminalStatus) return;
|
|
1157
|
+
failPending(
|
|
1158
|
+
new Error(
|
|
1159
|
+
`runtime exited before completion (code=${String(code)} signal=${String(signal)})`
|
|
1160
|
+
)
|
|
1161
|
+
);
|
|
1162
|
+
});
|
|
1163
|
+
child.stdin.on("error", (error) => {
|
|
1164
|
+
failPending(
|
|
1165
|
+
error instanceof Error ? error : new Error(`runtime stdin error: ${String(error)}`)
|
|
1166
|
+
);
|
|
1167
|
+
});
|
|
1168
|
+
child.stdout.setEncoding("utf8");
|
|
1169
|
+
child.stdout.on("data", (chunk) => {
|
|
1170
|
+
buffer += chunk;
|
|
1171
|
+
let index = buffer.indexOf("\n");
|
|
1172
|
+
while (index >= 0) {
|
|
1173
|
+
const line = buffer.slice(0, index).trim();
|
|
1174
|
+
buffer = buffer.slice(index + 1);
|
|
1175
|
+
if (line.length > 0) {
|
|
1176
|
+
try {
|
|
1177
|
+
const parsed = JSON.parse(line);
|
|
1178
|
+
if (isRpcResponse(parsed)) {
|
|
1179
|
+
const pending = pendingResponses.get(String(parsed.id));
|
|
1180
|
+
if (pending) {
|
|
1181
|
+
pendingResponses.delete(String(parsed.id));
|
|
1182
|
+
pending.resolve(parsed);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
maybeTrackRunLifecycle(parsed);
|
|
1186
|
+
} catch {
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
index = buffer.indexOf("\n");
|
|
1190
|
+
}
|
|
1191
|
+
});
|
|
1192
|
+
try {
|
|
1193
|
+
sendRequest({
|
|
1194
|
+
jsonrpc: "2.0",
|
|
1195
|
+
id: "init-1",
|
|
1196
|
+
method: "initialize",
|
|
1197
|
+
params: {
|
|
1198
|
+
protocol_version: "0",
|
|
1199
|
+
client: { name: "codelia-cli-prompt", version: "0.1.0" },
|
|
1200
|
+
ui_capabilities: {
|
|
1201
|
+
supports_confirm: false,
|
|
1202
|
+
supports_prompt: false,
|
|
1203
|
+
supports_pick: false
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
});
|
|
1207
|
+
const initResponse = await waitResponse("init-1");
|
|
1208
|
+
if (initResponse.error) {
|
|
1209
|
+
throw new Error(`initialize failed: ${initResponse.error.message}`);
|
|
1210
|
+
}
|
|
1211
|
+
sendRequest({
|
|
1212
|
+
jsonrpc: "2.0",
|
|
1213
|
+
id: "run-1",
|
|
1214
|
+
method: "run.start",
|
|
1215
|
+
params: {
|
|
1216
|
+
input: { type: "text", text: options.prompt }
|
|
1217
|
+
}
|
|
1218
|
+
});
|
|
1219
|
+
const runResponse = await waitResponse("run-1");
|
|
1220
|
+
if (runResponse.error) {
|
|
1221
|
+
throw new Error(`run.start failed: ${runResponse.error.message}`);
|
|
1222
|
+
}
|
|
1223
|
+
const runResult = typeof runResponse.result === "object" && runResponse.result !== null ? runResponse.result : null;
|
|
1224
|
+
runId = typeof runResult?.run_id === "string" ? runResult.run_id : null;
|
|
1225
|
+
if (!runId) {
|
|
1226
|
+
throw new Error("run.start did not return run_id");
|
|
1227
|
+
}
|
|
1228
|
+
await new Promise((resolve, reject) => {
|
|
1229
|
+
const timeout = setInterval(() => {
|
|
1230
|
+
if (terminalStatus) {
|
|
1231
|
+
clearInterval(timeout);
|
|
1232
|
+
resolve();
|
|
1233
|
+
return;
|
|
1234
|
+
}
|
|
1235
|
+
if (startupFailure) {
|
|
1236
|
+
clearInterval(timeout);
|
|
1237
|
+
reject(startupFailure);
|
|
1238
|
+
}
|
|
1239
|
+
}, 50);
|
|
1240
|
+
});
|
|
1241
|
+
} catch (error) {
|
|
1242
|
+
console.error(
|
|
1243
|
+
`Prompt run failed: ${error instanceof Error ? error.message : String(error)}`
|
|
1244
|
+
);
|
|
1245
|
+
return 1;
|
|
1246
|
+
} finally {
|
|
1247
|
+
if (!child.killed) {
|
|
1248
|
+
child.kill();
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
if (terminalStatus === "completed") {
|
|
1252
|
+
if (finalText.trim().length > 0) {
|
|
1253
|
+
console.log(finalText);
|
|
1254
|
+
}
|
|
1255
|
+
return 0;
|
|
1256
|
+
}
|
|
1257
|
+
if (terminalMessage) {
|
|
1258
|
+
console.error(terminalMessage);
|
|
1259
|
+
}
|
|
1260
|
+
return terminalStatus === "cancelled" ? 130 : 1;
|
|
1261
|
+
};
|
|
1262
|
+
|
|
944
1263
|
// src/index.ts
|
|
945
1264
|
var args = process.argv.slice(2);
|
|
946
1265
|
var main = async () => {
|
|
@@ -956,6 +1275,16 @@ var main = async () => {
|
|
|
956
1275
|
process.exitCode = exitCode;
|
|
957
1276
|
return;
|
|
958
1277
|
}
|
|
1278
|
+
case "prompt": {
|
|
1279
|
+
const prompt = validatePromptText(resolvePromptText(args));
|
|
1280
|
+
const approvalMode = resolvePromptModeApproval(args);
|
|
1281
|
+
const exitCode = await runPromptMode({
|
|
1282
|
+
prompt,
|
|
1283
|
+
approvalMode
|
|
1284
|
+
});
|
|
1285
|
+
process.exitCode = exitCode;
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
959
1288
|
case "tui":
|
|
960
1289
|
runTui(args);
|
|
961
1290
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codelia/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.22",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=20 <25"
|
|
@@ -19,19 +19,19 @@
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@codelia/config-loader": "0.1.
|
|
23
|
-
"@codelia/protocol": "0.1.
|
|
24
|
-
"@codelia/runtime": "0.1.
|
|
25
|
-
"@codelia/storage": "0.1.
|
|
22
|
+
"@codelia/config-loader": "0.1.22",
|
|
23
|
+
"@codelia/protocol": "0.1.22",
|
|
24
|
+
"@codelia/runtime": "0.1.22",
|
|
25
|
+
"@codelia/storage": "0.1.22",
|
|
26
26
|
"cac": "^6.7.14",
|
|
27
27
|
"zod": "^4.3.6"
|
|
28
28
|
},
|
|
29
29
|
"optionalDependencies": {
|
|
30
|
-
"@codelia/tui-darwin-arm64": "0.1.
|
|
31
|
-
"@codelia/tui-darwin-x64": "0.1.
|
|
32
|
-
"@codelia/tui-linux-arm64": "0.1.
|
|
33
|
-
"@codelia/tui-linux-x64": "0.1.
|
|
34
|
-
"@codelia/tui-win32-x64": "0.1.
|
|
30
|
+
"@codelia/tui-darwin-arm64": "0.1.22",
|
|
31
|
+
"@codelia/tui-darwin-x64": "0.1.22",
|
|
32
|
+
"@codelia/tui-linux-arm64": "0.1.22",
|
|
33
|
+
"@codelia/tui-linux-x64": "0.1.22",
|
|
34
|
+
"@codelia/tui-win32-x64": "0.1.22"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "tsup --config tsup.config.ts",
|