-
Notifications
You must be signed in to change notification settings - Fork 40
/
chatsh2.mjs
executable file
·456 lines (374 loc) · 13.5 KB
/
chatsh2.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/usr/bin/env node
import fs from 'fs';
import os from 'os';
import path from 'path';
import readline from 'readline';
import { chat, MODELS, tokenCount } from './Chat.mjs';
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
const MODEL = process.argv[2] || "c";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true
});
const prompt = (query) => new Promise(resolve => rl.question(query, resolve));
// STATE
const shownPaths = new Set();
let lastOutput = "";
// PROMPT
const buildSystemPrompt = () => {
const contextData = {};
for (const p of shownPaths) {
try {
const absPath = path.resolve(p);
const stat = fs.statSync(absPath);
if (stat.isDirectory()) {
contextData[absPath] = fs.readdirSync(absPath).join('\n');
} else if (stat.isFile()) {
contextData[absPath] = fs.readFileSync(absPath, 'utf-8');
}
} catch (error) {
console.error(`Error reading path '${p}': ${error.message}`);
}
}
return `You are ChatSH, an AI assistant that helps the user with system tasks using shell commands and answers questions via the terminal.
# Instructions:
- When the user asks you to perform a task, reply with sequence of XML Commands ("command mode").
- When the user asks an open-ended question, answer with plain text only ("chat mode"). Be concise.
# Available XML Commands:
1. To show a file or directory on CONTEXT:
<SHOW path="/Path/to/file/or/dir"/>
Always use this command when the user asks to "view", "read", "cat", "show", "display" (etc.) a file.
Write one line for EACH file you want to show.
2. To hide a file or directory from CONTEXT:
<HIDE path="/Path/to/file/or/dir"/>
Use this command to hide irrelevant files bloating the context.
3. To write or overwrite a file:
<WRITE path="...">
(contents)
</WRITE>
Always use this command when the user asks to "write", "create", "modify", "edit", "replace", "refactor" (etc.) a file.
When modifying an existing file, rewrite it fully, without omitting parts.
4. To remove a file or directory:
<REMOVE path="/Path/to/file/or/dir"/>
Use this command when the user asks to "delete", "remove", "erase" (etc.) a file or directory.
5. To perform any other task:
<RUN>
(shell script with arbitrary commands, EXCEPT for file editing and displaying commands such as 'cat', 'touch', and similar)
</RUN>
# Examples:
::: USER
list files with 'cat' in their name
::: CHATSH
<RUN>
find . -name "*cat*"
</RUN>
::: SYSTEM
./cat_pictures.html
./catalog_items.py
./catch_exceptions.java
./category_theory.txt
./caterpillar_dance.js
::: USER
cat the cute ones
::: CHATSH
<SHOW path="./cat_pictures.html"/>
<SHOW path="./caterpillar_dance.js"/>
::: USER
create 3 files with some random fruit
::: CHATSH
<WRITE path="file_0.txt">
apple
</WRITE>
<WRITE path="file_1.txt">
banana
</WRITE>
<WRITE path="file_2.txt">
strawberry
</WRITE>
::: USER
remove file_1.txt
::: CHATSH
<REMOVE path="file_1.txt"/>
# Avoid these common errors:
- Do NOT use <RUN/> to EDIT files. Always use <WRITE/> for that.
- Do NOT use <RUN/> to VIEW files. Always use <SHOW/> for that.
- Do NOT use <RUN/> to REMOVE files. Always use <REMOVE/> for that.
- Do NOT add any explanatory text or comments before or after XML commands.
- Do NOT output anything other XML Commands to complete a task on "command mode".
- We will EDIT your response to omit <WRITE/> tags, but you should NEVER omit them yourself.
# Context:
${((Object.entries(contextData).map(([p, content]) => `<FILE path="${p}">\n${content.trim()}\n</FILE>`).join('\n\n')) || "(none)").trim()}
(use <SHOW/> and <HIDE/> to change)
# Task:
Now, you must answer the last user message. Remember:
- If the user asked an open-ended question, answer with a short and concise reply ("chat mode").
- If the user asked you to perform XML Commands, answer with just a list of commands ("command mode"):
<CMD_0/>
<CMD_1/>
...
<CMD_N/>
REMEMBER: do NOT include explanatory text before or after XML Commands.
`.trim();
};
const HISTORY_DIR = path.join(os.homedir(), '.ai', 'chatsh2_history');
if (!fs.existsSync(HISTORY_DIR)) {
fs.mkdirSync(HISTORY_DIR, { recursive: true });
}
const conversationFile = path.join(HISTORY_DIR, `conversation_${new Date().toISOString().replace(/[:.]/g, '-')}.txt`);
const appendToHistory = (role, message) => {
const formattedMessage = `::: ${role}\n${message}\n\n`;
fs.appendFileSync(conversationFile, formattedMessage);
};
const executeUserCommand = async (input) => {
input = input.trim();
if (input.startsWith('!')) {
const command = input.slice(1);
try {
const { stdout, stderr } = await execAsync(command);
var currOutput = `\$ ${command}\n${stdout.trim()}\n${stderr.trim()}`.trim()
lastOutput = (lastOutput + (lastOutput ? '\n' : '') + currOutput).trim();
console.log('\x1b[34m%s\x1b[0m', currOutput); // Blue color for system outputs
appendToHistory('SYSTEM', currOutput);
} catch (error) {
console.error(`Error executing command: ${error.message}`);
lastOutput = error.message;
appendToHistory('ERROR', error.message);
}
return true;
} else if (input.startsWith('+')) {
const pathToAdd = input.slice(1).trim();
shownPaths.add(pathToAdd);
return true;
} else if (input.startsWith('-')) {
const pathToRemove = input.slice(1).trim();
shownPaths.delete(pathToRemove);
return true;
} else if (input === '?') {
showStats();
return true;
}
return false;
};
const parseAICommands = (text) => {
const commands = [];
const regex = /<([A-Z]+)([^>]*)>([\s\S]*?)<\/\1>|<([A-Z]+)([^>]*)\/>/g;
let match;
while ((match = regex.exec(text)) !== null) {
const [, tag, attrs, content, selfClosingTag, selfClosingAttrs] = match;
const parsedAttrs = (attrs || selfClosingAttrs || '').trim().split(/\s+/).reduce((acc, attr) => {
const [key, value] = attr.split('=');
if (key && value) {
acc[key] = value.replace(/^"|"$/g, '');
}
return acc;
}, {});
commands.push({
tag: tag || selfClosingTag,
attrs: parsedAttrs,
content: content || null
});
}
return commands;
};
const confirmAction = async (message) => {
console.log(`\x1b[31m${message} [Y/N]\x1b[0m`);
const answer = await prompt('');
process.stdout.moveCursor(0, -2);
process.stdout.clearLine(2);
return answer.toLowerCase() !== 'n';
};
const executeCommand = async (command) => {
try {
const { stdout, stderr } = await execAsync(command);
const output = `${stdout.trim()}\n${stderr.trim()}`.trim();
if (output !== "") console.log('\x1b[34m%s\x1b[0m', output); // Blue color for system outputs
appendToHistory('SYSTEM', output);
lastOutput += (lastOutput ? '\n' : '') + output;
} catch (error) {
console.error(`Error executing command: ${error.message}`);
appendToHistory('ERROR', `Error executing command: ${error.message}`);
lastOutput += (lastOutput ? '\n' : '') + error.message;
}
};
const processAIResponse = async (response) => {
const commands = parseAICommands(response);
if (commands.length > 0) {
const confirmMessage = `Execute ${commands.length} command${commands.length > 1 ? 's' : ''}?`;
if (await confirmAction(confirmMessage)) {
for (const { tag, attrs, content } of commands) {
switch (tag) {
case 'WRITE': {
if (attrs.path && content != null) {
attrs.path = path.resolve(attrs.path);
fs.writeFileSync(attrs.path, content.trim());
shownPaths.add(attrs.path);
} else {
appendToHistory('ERROR', 'Invalid WRITE command');
}
break;
}
case 'RUN': {
if (content != null) {
await executeCommand(content);
} else {
appendToHistory('ERROR', 'Invalid RUN command');
}
break;
}
case 'SHOW': {
if (attrs.path) {
attrs.path = path.resolve(attrs.path);
shownPaths.add(attrs.path);
try {
const stat = fs.statSync(attrs.path);
if (stat.isDirectory()) {
const files = fs.readdirSync(attrs.path);
console.log('\x1b[34m%s\x1b[0m', files.join('\n')); // Blue color for system outputs
} else if (stat.isFile()) {
const content = fs.readFileSync(attrs.path, 'utf-8');
console.log('\x1b[34m%s\x1b[0m', content.trim()); // Blue color for system outputs
}
} catch (error) {
console.error(`Error reading path ${attrs.path}: ${error.message}`);
appendToHistory('ERROR', `Error reading path ${attrs.path}: ${error.message}`);
}
} else {
appendToHistory('ERROR', 'Invalid SHOW command');
}
break;
}
case 'HIDE': {
if (attrs.path) {
attrs.path = path.resolve(attrs.path);
shownPaths.delete(attrs.path);
} else {
appendToHistory('ERROR', 'Invalid HIDE command');
}
break;
}
case 'REMOVE': {
if (attrs.path) {
attrs.path = path.resolve(attrs.path);
try {
fs.rmSync(attrs.path, { recursive: true, force: true });
console.log(`Removed: ${attrs.path}`);
appendToHistory('SYSTEM', `Removed: ${attrs.path}`);
shownPaths.delete(attrs.path);
} catch (error) {
console.error(`Error removing ${attrs.path}: ${error.message}`);
appendToHistory('ERROR', `Error removing ${attrs.path}: ${error.message}`);
}
} else {
appendToHistory('ERROR', 'Invalid REMOVE command');
}
break;
}
default: {
console.error(`Unknown command: ${tag}`);
}
}
}
} else {
console.log('Action skipped.');
appendToHistory('SYSTEM', 'Action skipped.');
lastOutput += (lastOutput ? '\n' : '') + 'Action skipped.';
}
}
console.log("");
};
const showStats = async () => {
const chatData = await ask(null, {});
const totalMessages = chatData.messages.length;
const systemPrompt = buildSystemPrompt();
const systemPromptTokenCount = tokenCount(systemPrompt);
const allMessages = chatData.messages.map(msg => msg.content).join(' ');
const totalChatTokenCount = tokenCount(allMessages);
console.log('\x1b[33m%s\x1b[0m', `seen_paths:`);
shownPaths.forEach(path => {
console.log('\x1b[33m%s\x1b[0m', ` - ${path}`);
});
console.log('\x1b[33m%s\x1b[0m', `msg_number: ${totalMessages}`);
console.log('\x1b[33m%s\x1b[0m', `msg_tokens: ${totalChatTokenCount}`);
console.log('\x1b[33m%s\x1b[0m', `sys_tokens: ${systemPromptTokenCount}`);
console.log('\x1b[33m%s\x1b[0m', `tot_tokens: ${systemPromptTokenCount + totalChatTokenCount}`);
console.log('');
};
const shortenAIResponse = (text) => {
return text.replace(/<WRITE[\s\S]*?<\/WRITE>/g, '(omitted)');
};
const ask = chat(MODEL);
const loadFunctionCall = (command) => {
const localPath = `./${command}.csh`;
const cshPath = `./CSH/${command}.csh`;
if (fs.existsSync(localPath)) {
return fs.readFileSync(localPath, 'utf-8');
} else if (fs.existsSync(cshPath)) {
return fs.readFileSync(cshPath, 'utf-8');
}
return null;
};
const main = async () => {
console.log(`Welcome to ChatSH. Model: ${MODELS[MODEL]||MODEL}\n`);
// Check for direct call
if (process.argv.length > 3) {
const directMessage = process.argv.slice(3).join(' ');
await processUserInput(directMessage);
process.exit(0);
}
while (true) {
process.stdout.write('\x1b[1m'); // Start bold
const userInput = await prompt('λ ');
process.stdout.write('\x1b[0m'); // End bold
if (userInput.trim() === "") {
continue;
}
await processUserInput(userInput);
}
};
const processUserInput = async (userInput) => {
appendToHistory('USER', userInput);
if (await executeUserCommand(userInput)) {
return;
}
let fullMessage = userInput;
if (lastOutput) {
fullMessage = `\`\`\`sh\n${lastOutput.trim()}\n\`\`\`\n\n${userInput}`;
lastOutput = "";
}
const systemPrompt = buildSystemPrompt();
try {
let extend = null;
if (userInput.startsWith('/')) {
const [command, ...rest] = userInput.slice(1).split(' ');
const functionContent = loadFunctionCall(command);
if (functionContent) {
extend = msg => functionContent + "\n" + msg;
fullMessage = rest.join(' ');
}
}
const assistantMessage = await ask(fullMessage, {
system: systemPrompt,
model: MODEL,
max_tokens: 8192,
system_cacheable: true,
shorten: shortenAIResponse,
extend: extend
});
console.log("");
appendToHistory('CHATSH', assistantMessage);
const logFile = path.join(HISTORY_DIR, 'chatsh2.log.txt');
const sysFile = path.join(HISTORY_DIR, 'chatsh2.sys.txt');
const msgFile = path.join(HISTORY_DIR, 'chatsh2.msg.txt');
fs.appendFileSync(logFile, fullMessage + '\n##########################################\n');
fs.writeFileSync(sysFile, systemPrompt);
fs.writeFileSync(msgFile, JSON.stringify(await ask(null, {}), null, 2));
await processAIResponse(assistantMessage);
} catch (error) {
console.error(`Error: ${error.message}`);
appendToHistory('ERROR', error.message);
}
};
main();