-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathlaunch-console.js
More file actions
315 lines (272 loc) · 8.64 KB
/
launch-console.js
File metadata and controls
315 lines (272 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env node
const readline = require('readline');
const { spawn } = require('child_process');
const backendCompat = require('../utils/backend-compat');
const SafeConfigLoader = require('../utils/safe-config-loader');
const pathResolver = require('../utils/path-resolver');
const COLOR = {
reset: '\x1b[0m',
bold: '\x1b[1m',
dim: '\x1b[2m',
gray: '\x1b[90m',
white: '\x1b[37m',
red: '\x1b[91m',
green: '\x1b[92m',
cyan: '\x1b[96m',
champagne: '\x1b[38;2;247;231;206m',
};
const MODE_OPTIONS = [
{
key: '1',
mode: 'openclaw',
label: 'OpenClaw',
color: COLOR.red,
summary: 'classic red flame, keeps the existing default path',
},
{
key: '2',
mode: 'hermes',
label: 'Hermes',
color: COLOR.champagne,
summary: 'champagne flame, Hermes compatibility mode',
},
{
key: '3',
mode: 'auto',
label: 'Auto',
color: COLOR.cyan,
summary: 'prefer OpenClaw, fall back to Hermes if needed',
},
];
const VALID_MODES = new Set(['openclaw', 'hermes', 'auto']);
function normalizeMode(value) {
const mode = String(value || '').trim().toLowerCase();
return VALID_MODES.has(mode) ? mode : null;
}
function getPetConfigPath() {
return `${pathResolver.getProjectRoot()}/pet-config.json`;
}
function readLauncherSettings() {
return SafeConfigLoader.load(getPetConfigPath(), {});
}
function saveLauncherSettings(config) {
return SafeConfigLoader.save(getPetConfigPath(), config);
}
function rememberLastChoice(mode, config = readLauncherSettings()) {
const normalizedMode = normalizeMode(mode);
if (!normalizedMode) {
return false;
}
const nextConfig = { ...config, lastCompatMode: normalizedMode };
return saveLauncherSettings(nextConfig);
}
function persistLauncherChoice(mode, config = readLauncherSettings()) {
const normalizedMode = normalizeMode(mode);
if (!normalizedMode) {
return false;
}
const nextConfig = { ...config, lastCompatMode: normalizedMode };
if (!normalizeMode(config.compatMode)) {
nextConfig.compatMode = normalizedMode;
}
return saveLauncherSettings(nextConfig);
}
function getCompatSnapshot() {
try {
backendCompat.clearCache();
} catch (_) {}
return backendCompat.resolve();
}
function resolveLauncherDefault(snapshot, settings = {}) {
const fixedMode = normalizeMode(settings.compatMode);
if (fixedMode) {
return {
mode: fixedMode,
source: 'pet-config.compatMode',
fixed: true,
requiresChoice: false,
};
}
const lastMode = normalizeMode(settings.lastCompatMode);
if (lastMode) {
return {
mode: lastMode,
source: 'pet-config.lastCompatMode',
fixed: false,
requiresChoice: false,
};
}
return {
mode: null,
source: 'first-launch',
fixed: false,
requiresChoice: true,
};
}
function getDefaultChoice(snapshot, settings = {}) {
return resolveLauncherDefault(snapshot, settings).mode;
}
function describeDefault(snapshot, defaultMode, meta = null) {
if (defaultMode === 'auto') {
const targetLabel = snapshot?.active?.label || 'OpenClaw';
return meta?.fixed ? `Auto (fixed) -> ${targetLabel}` : `Auto -> ${targetLabel}`;
}
const baseLabel = MODE_OPTIONS.find((option) => option.mode === defaultMode)?.label || 'OpenClaw';
return meta?.fixed ? `${baseLabel} (fixed)` : baseLabel;
}
function resolveModeInput(input, defaultMode) {
const normalized = String(input || '').trim().toLowerCase();
if (!normalized) {
return defaultMode || null;
}
const byKey = MODE_OPTIONS.find((option) => option.key === normalized);
if (byKey) {
return byKey.mode;
}
const byMode = MODE_OPTIONS.find((option) => option.mode === normalized);
if (byMode) {
return byMode.mode;
}
const byLabel = MODE_OPTIONS.find((option) => option.label.toLowerCase() === normalized);
if (byLabel) {
return byLabel.mode;
}
return null;
}
function buildChildEnv(mode) {
const env = { ...process.env };
const normalizedMode = normalizeMode(mode);
if (normalizedMode) {
env.KKCLAW_COMPAT_MODE = normalizedMode;
} else {
delete env.KKCLAW_COMPAT_MODE;
}
return env;
}
function printMenu(snapshot, defaultMeta) {
const defaultMode = defaultMeta.mode;
const defaultLabel = defaultMode ? describeDefault(snapshot, defaultMode, defaultMeta) : null;
const sourceLine = defaultMeta.requiresChoice
? 'First launch detected: choose the backend you want KKClaw to keep using'
: defaultMeta.fixed
? 'Saved choice from `pet-config.json` -> `compatMode`'
: defaultMeta.source === 'pet-config.lastCompatMode'
? 'Defaulting to your last launch choice'
: 'Defaulting to automatic backend detection';
console.log('');
console.log(`${COLOR.bold}${COLOR.white}KKClaw Console Launcher${COLOR.reset}`);
console.log(`${COLOR.gray}Choose a backend for this launch.${COLOR.reset}`);
if (defaultLabel) {
console.log(`${COLOR.gray}Press Enter to keep: ${COLOR.bold}${defaultLabel}${COLOR.reset}`);
} else {
console.log(`${COLOR.gray}No backend is selected yet. You need to choose one this first time.${COLOR.reset}`);
}
console.log(`${COLOR.gray}${sourceLine}${COLOR.reset}`);
console.log(`${COLOR.gray}Tip: set ${COLOR.bold}compatMode${COLOR.reset}${COLOR.gray} in pet-config.json if you want to change the saved backend later.${COLOR.reset}`);
console.log('');
for (const option of MODE_OPTIONS) {
const installed =
option.mode === 'openclaw'
? snapshot?.openclaw?.installed
: option.mode === 'hermes'
? snapshot?.hermes?.installed
: true;
const availability = installed ? `${COLOR.green}detected${COLOR.reset}` : `${COLOR.gray}not detected${COLOR.reset}`;
console.log(` ${option.key}) ${option.color}${COLOR.bold}${option.label}${COLOR.reset} ${COLOR.dim}- ${option.summary}${COLOR.reset} ${COLOR.gray}[${availability}${COLOR.gray}]${COLOR.reset}`);
}
console.log('');
}
function askQuestion(promptText) {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(promptText, (answer) => {
rl.close();
resolve(answer);
});
});
}
async function promptForMode(snapshot) {
const explicitEnv = String(process.env.KKCLAW_COMPAT_MODE || '').trim().toLowerCase();
if (normalizeMode(explicitEnv)) {
return explicitEnv;
}
const settings = readLauncherSettings();
const defaultMeta = resolveLauncherDefault(snapshot, settings);
const defaultMode = defaultMeta.mode;
if (defaultMode && !defaultMeta.requiresChoice) {
return defaultMode;
}
if (!process.stdin.isTTY || !process.stdout.isTTY) {
return defaultMode || 'auto';
}
while (true) {
printMenu(snapshot, defaultMeta);
const answer = await askQuestion(`${COLOR.bold}Select backend [1/2/3]${COLOR.reset}: `);
const selectedMode = resolveModeInput(answer, defaultMode);
if (selectedMode) {
return selectedMode;
}
console.log(
`${COLOR.red}Invalid choice.${COLOR.reset} ${
defaultMeta.requiresChoice
? 'First launch requires an explicit choice: enter 1, 2, 3, openclaw, hermes, or auto.'
: 'Enter 1, 2, 3, openclaw, hermes, or auto.'
}`
);
console.log('');
}
}
function launchElectron(mode) {
return new Promise((resolve, reject) => {
const child = spawn(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['start'], {
stdio: 'inherit',
env: buildChildEnv(mode),
shell: false,
windowsHide: true,
});
child.on('error', reject);
child.on('close', (code) => resolve(code ?? 0));
});
}
async function launchConsole() {
const snapshot = getCompatSnapshot();
const settings = readLauncherSettings();
const selectedMode = await promptForMode(snapshot);
persistLauncherChoice(selectedMode, settings);
const selectedOption = MODE_OPTIONS.find((option) => option.mode === selectedMode);
console.log(
`${COLOR.gray}Starting with ${selectedOption ? selectedOption.color : COLOR.white}${COLOR.bold}${selectedOption?.label || selectedMode}${COLOR.reset}${COLOR.gray}...${COLOR.reset}`
);
console.log('');
return launchElectron(selectedMode);
}
module.exports = {
MODE_OPTIONS,
buildChildEnv,
describeDefault,
getCompatSnapshot,
getDefaultChoice,
getPetConfigPath,
launchConsole,
launchElectron,
promptForMode,
persistLauncherChoice,
readLauncherSettings,
rememberLastChoice,
resolveModeInput,
resolveLauncherDefault,
saveLauncherSettings,
};
if (require.main === module) {
launchConsole()
.then((code) => {
process.exit(code);
})
.catch((error) => {
console.error(error instanceof Error ? error.message : String(error));
process.exit(1);
});
}