forked from SmythOS/sre
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
112 lines (89 loc) · 3.46 KB
/
script.js
File metadata and controls
112 lines (89 loc) · 3.46 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
import fs from 'fs';
import { stdin, stdout } from 'process';
export function askForValues(message, prompts, options = {}) {
console.log(message);
const result = {};
// Save original terminal mode
const wasRaw = stdin.isRaw;
for (const [key, label] of Object.entries(prompts)) {
stdout.write(`${label} `);
// Set terminal to raw mode to capture Ctrl+C
stdin.setRawMode(true);
stdin.resume();
let value = '';
let cancelled = false;
const promptLength = label.length + 1;
while (true) {
const buffer = Buffer.alloc(1);
const bytesRead = fs.readSync(stdin.fd, buffer, 0, 1, null);
if (bytesRead === 0) break;
const char = buffer.toString('utf8');
const code = buffer[0];
// ✅ Check for Ctrl+C (code 3)
if (code === 3) {
cancelled = true;
break;
}
// Check for Enter (code 13 or 10)
if (code === 13 || code === 10) {
stdout.write('\n');
break;
}
// Check for Backspace (code 127 or 8)
if (code === 127 || code === 8) {
if (value.length > 0) {
const oldValue = value;
value = value.slice(0, -1);
// ✅ Calculate lines needed for old and new values
const terminalWidth = stdout.columns || 80;
const oldTotalLength = promptLength + oldValue.length;
const newTotalLength = promptLength + value.length;
const oldLines = Math.ceil(oldTotalLength / terminalWidth);
const newLines = Math.ceil(newTotalLength / terminalWidth);
// Move to start of first line
for (let i = 1; i < oldLines; i++) {
stdout.write('\x1b[1A'); // Move up
}
stdout.write('\r'); // Go to start of line
// Clear all old lines
for (let i = 0; i < oldLines; i++) {
stdout.write('\x1b[K'); // Clear line
if (i < oldLines - 1) {
stdout.write('\n'); // Move to next line to clear it
}
}
// Move back to start
for (let i = 1; i < oldLines; i++) {
stdout.write('\x1b[1A');
}
stdout.write('\r');
// Redraw prompt and new value
stdout.write(`${label} ${value}`);
}
continue;
}
// Regular character
if (code >= 32 && code <= 126) {
value += char;
stdout.write(char);
}
}
// Restore terminal mode
stdin.setRawMode(wasRaw);
stdin.pause();
if (cancelled) {
console.log('\n❌ Operation cancelled by user.');
process.exit(130);
}
if (value.trim()) result[key] = value.trim();
}
return result;
}
const apiKeys = askForValues('Please enter the API keys for your LLM providers (Press Enter to skip any key):', {
openai: 'OpenAI : ',
anthropic: 'Anthropic : ',
googleai: 'Google AI : ',
xai: 'xAI : ',
groq: 'Groq : ',
});
console.log('\n\n\n', apiKeys, '\n\n\n');