@stbestichhh/term-ai 1.0.0 → 1.1.0
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/index.js +38 -3
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
import fs from 'node:fs';
|
|
4
4
|
import os from 'node:os';
|
|
5
5
|
import path from 'node:path';
|
|
6
|
+
import readline from 'node:readline';
|
|
7
|
+
import { execSync } from 'node:child_process';
|
|
8
|
+
|
|
6
9
|
import { GoogleGenAI } from '@google/genai';
|
|
7
10
|
|
|
8
11
|
const CONFIG_DIR = path.join(process.env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config'), 'tai');
|
|
@@ -28,6 +31,20 @@ function saveApiKey(key) {
|
|
|
28
31
|
console.log(`API key saved to ${CONFIG_FILE}`);
|
|
29
32
|
}
|
|
30
33
|
|
|
34
|
+
function askConfirmation(query) {
|
|
35
|
+
const rl = readline.createInterface({
|
|
36
|
+
input: process.stdin,
|
|
37
|
+
output: process.stdout,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return new Promise((resolve) => {
|
|
41
|
+
rl.question(query, (answer) => {
|
|
42
|
+
rl.close();
|
|
43
|
+
resolve(answer.trim().toLowerCase());
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
31
48
|
const args = process.argv.slice(2);
|
|
32
49
|
|
|
33
50
|
if (args[0] === 'config' && args[1] === 'set-key') {
|
|
@@ -64,15 +81,33 @@ async function run() {
|
|
|
64
81
|
contents: prompt,
|
|
65
82
|
config: {
|
|
66
83
|
systemInstruction:
|
|
67
|
-
|
|
84
|
+
`You are a command-line assistant. Provide concise, direct terminal commands and short explanations without conversational fluff. Wrap command in markdown code block with language as 'bash', 'sh' or 'tai'. In response show no more than one command.`,
|
|
68
85
|
},
|
|
69
86
|
});
|
|
70
87
|
|
|
71
|
-
|
|
88
|
+
const text = response.text.trim();
|
|
89
|
+
const match = text.match(/```(?:tai|bash|sh)?\s*([\s\S]*?)\s*```/i);
|
|
90
|
+
const command = match ? match[1].trim() : text.trim();
|
|
91
|
+
|
|
92
|
+
const cleanOutput = text
|
|
93
|
+
.replace(/```(?:tai|bash|sh)?\n?/gi, '')
|
|
94
|
+
.replace(/```/g, '')
|
|
95
|
+
.trim();
|
|
96
|
+
|
|
97
|
+
console.log(`\n${cleanOutput}\n`);
|
|
98
|
+
|
|
99
|
+
const answer = await askConfirmation(`Execute this command? [Y/n] `);
|
|
100
|
+
|
|
101
|
+
if (['y', 'yes', ''].includes(answer)) {
|
|
102
|
+
console.log('\x1b[33mExecuting...\x1b[0m\n');
|
|
103
|
+
execSync(command, { stdio: 'inherit' });
|
|
104
|
+
} else {
|
|
105
|
+
console.log(`Aborted.`)
|
|
106
|
+
}
|
|
72
107
|
} catch (error) {
|
|
73
108
|
console.error('Error:', error.message);
|
|
74
109
|
process.exit(1);
|
|
75
110
|
}
|
|
76
111
|
}
|
|
77
112
|
|
|
78
|
-
run();
|
|
113
|
+
void run();
|