forked from VictorTaelin/AI-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatsh.mjs
executable file
·303 lines (231 loc) · 6.92 KB
/
chatsh.mjs
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
#!/usr/bin/env node
import readline from 'readline';
import { exec } from 'child_process';
import { promisify } from 'util';
import { chat, MODELS } from './Chat.mjs';
const execAsync = promisify(exec);
// Default model if not specified
const DEFAULT_MODEL = "s";
// Get model from environment variable or use default
const MODEL = process.argv[2] || DEFAULT_MODEL;
console.log(`Welcome to ChatSH. Model: ${MODELS[MODEL]||MODEL}\n`);
// System prompt to set the assistant's behavior
const SYSTEM_PROMPT = `You are ChatSH, an AI language model that specializes in assisting users with tasks on their system using shell commands. ChatSH operates in two modes: COMMAND MODE and CHAT MODE.
# GUIDE for COMMAND NODE:
1. The USER asks you to perform a SYSTEM TASK.
2. ChatSH answers with a SHELL SCRIPT to perform the task.
# GUIDE for CHAT MODE:
1. The USER asks an ARBITRARY QUESTION or OPEN-ENDED MESSAGE.
2. ChatSH answers it with a concise, factual response.
# EXAMPLE 1:
<USER>
Show me local files.
</USER>
<ChatSH>
\`\`\`sh
ls
\`\`\`
</ChatSH>
<SYSTEM>
example.gif example.tst example_dir/
</SYSTEM>
<USER>
Is there any text file?
</USER>
<ChatSH>
Yes, there is a file named example.txt in the current directory
</ChatSH>
<USER>
Show me the contents of example.txt.
</USER>
<ChatSH>
cat example.txt
</ChatSH>
<SYSTEM>
Na matemática, um grupo é um conjunto de elementos associados a uma operação que combina dois elementos quaisquer para formar um terceiro. Para se qualificar como grupo o conjunto e a operação devem satisfazer algumas condições chamadas axiomas de grupo: associatividade, elemento neutro e elementos inversos.
</SYSTEM>
<USER>
Can you translate the first sentence to English?
</USER>
<ChatSH>
In mathematics, a group is a set of elements associated with an operation that combines any two elements to form a third element.
</ChatSH>
<USER>
What is the monster group?
</USER>
<ChatSH>
The monster group is the largest sporadic simple group, with order approximately 8×10^53.
</ChatSH>
<USER>
What is a simple group?
</USER>
<ChatSH>
A simple group is a nontrivial group that contains no proper nontrivial normal subgroups.
</ChatSH>
<USER>
Move the text file to that dir.
</USER>
<ChatSH>
\`\`\`sh
mv example.txt example_dir/
\`\`\`
</ChatSH>
# EXAMPLE 2:
<USER>
Fix main.ts
</USER>
<ChatSH>
\`\`\`sh
cat main.ts
tsc --noEmit main.ts
\`\`\`
</ChatSH>
<SYSTEM>
import List from './list';
import map from './map';
const n_1_2: List<number> = { $: "cons", head: 1, tail: { $: "cons", head: 2, tail: { $: "nil" }}};
const n_2_4: List<number> = map(n_1_2, x => x * 2);
console.log(JSON.stringify(n_2_4));
map.ts:7:22 - error TS2345: Argument of type '(val: T) => U' is not assignable to parameter of type 'List<unknown>'.
7 let tail = map(fun, list.tail);
~~~
</SYSTEM>
<ChatSH>
\`\`\`sh
ls
\`\`\`
</ChatSH>
<SYSTEM>
list.ts main.ts map.ts
</SYSTEM>
<ChatSH>
\`\`\`sh
cat map.ts
\`\`\`
</ChatSH>
<SYSTEM>
import List from './list';
function map<T, U>(list: List<T>, fun: (val: T) => U): List<U> {
switch (list.$) {
case "cons":
let head = fun(list.head);
let tail = map(fun, list.tail);
return { $: "cons", head, tail };
case "nil":
return { $: "nil" };
}
}
export default map;
</SYSTEM>
<ChatSH>
\`\`\`sh
cat > map.ts << EOL
import List from './list';
function map<T, U>(list: List<T>, fun: (val: T) => U): List<U> {
switch (list.$) {
case "cons":
let head = fun(list.head);
let tail = map(list.tail, fun);
return { $: "cons", head, tail };
case "nil":
return { $: "nil" };
}
}
export default map;
EOL
tsc --noEmit map.ts
\`\`\`
</ChatSH>
<SYSTEM>
</SYSTEM>
<ChatSH>
Done.
</ChatSH
## NOTES:
- In COMMAND MODE, ChatSH MUST answer with A SINGLE SH BLOCK.
- In COMMAND MODE, ChatSH MUST NOT answer with ENGLISH EXPLANATION.
- In TEXT MODE, ChatSH MUST ALWAYS answer with TEXT.
- In TEXT MODE, ChatSH MUST NEVER answer with SH BLOCK.
- ChatSH MUST be CONCISE, OBJECTIVE, CORRECT and USEFUL.
- ChatSH MUST NEVER attempt to install new tools. Assume they're available.
- ChatSH's interpreter can only process one SH per answer.
- On TypeScript:
- Use 'tsc --noEmit file.ts' to type-check.
- Use 'tsx file.ts' to run.
- Never generate js files.
- When a task is completed, STOP using commands. Just answer with "Done.".
- The system shell in use is: ${await get_shell()}.`;
// Create readline interface for user input/output
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true
});
// Create a stateful asker
const ask = chat(MODEL);
// Utility function to prompt the user for input
async function prompt(query) {
return new Promise(resolve => {
rl.question(query, resolve);
});
}
// If there are words after the 'chatsh', set them as the initialUserMessage
var initialUserMessage = process.argv.slice(3).join(' ');
// Main interaction loop
async function main() {
let lastOutput = "";
while (true) {
let userMessage;
if (initialUserMessage) {
userMessage = initialUserMessage;
initialUserMessage = null;
} else {
process.stdout.write('\x1b[1m'); // blue color
userMessage = await prompt('λ ');
process.stdout.write('\x1b[0m'); // reset color
}
try {
const fullMessage = userMessage.trim() !== ''
? `<SYSTEM>\n${lastOutput.trim()}\n</SYSTEM>\n<USER>\n${userMessage}\n</USER>\n`
: `<SYSTEM>\n${lastOutput.trim()}\n</SYSTEM>`;
const assistantMessage = await ask(fullMessage, { system: SYSTEM_PROMPT, model: MODEL });
console.log();
const code = extractCode(assistantMessage);
lastOutput = "";
if (code) {
console.log("\x1b[31mPress enter to execute, or 'N' to cancel.\x1b[0m");
const answer = await prompt('');
// TODO: delete the warning above from the terminal
process.stdout.moveCursor(0, -2);
process.stdout.clearLine(2);
if (answer.toLowerCase() === 'n') {
console.log('Execution skipped.');
lastOutput = "Command skipped.\n";
} else {
try {
const {stdout, stderr} = await execAsync(code);
const output = `${stdout.trim()}${stderr.trim()}`;
console.log('\x1b[2m' + output.trim() + '\x1b[0m');
lastOutput = output;
} catch(error) {
const output = `${error.stdout?.trim()||''}${error.stderr?.trim()||''}`;
console.log('\x1b[2m' + output.trim() + '\x1b[0m');
lastOutput = output;
}
}
}
} catch(error) {
console.error(`Error: ${error.message}`);
}
}
}
// Utility function to extract code from the assistant's message
function extractCode(text) {
const match = text.match(/```sh([\s\S]*?)```/);
return match ? match[1].trim() : null;
}
async function get_shell() {
const shellInfo = (await execAsync('uname -a && $SHELL --version')).stdout.trim();
return shellInfo;
}
main();