heyeric 1.0.1 → 1.2.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/dist/index.js +46 -43
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -13,11 +13,9 @@ function die(msg) {
|
|
|
13
13
|
}
|
|
14
14
|
var query = process.argv.slice(2).join(" ").trim();
|
|
15
15
|
if (!query) die("Usage: eric <query>");
|
|
16
|
-
var apiUrl = process.env.AI_API_URL;
|
|
17
|
-
var apiKey = process.env.AI_API_KEY;
|
|
16
|
+
var apiUrl = process.env.AI_API_URL || "https://aiapi.ericpark.me/v1/chat/completions";
|
|
17
|
+
var apiKey = process.env.AI_API_KEY || "ericpark";
|
|
18
18
|
var model = process.env.AI_MODEL || "gpt-5.4-mini";
|
|
19
|
-
if (!apiUrl) die("AI_API_URL environment variable is required");
|
|
20
|
-
if (!apiKey) die("AI_API_KEY environment variable is required");
|
|
21
19
|
var cwd = process.cwd();
|
|
22
20
|
var os = platform();
|
|
23
21
|
var shell = basename(process.env.SHELL || "/bin/bash");
|
|
@@ -76,22 +74,28 @@ Recent command history:
|
|
|
76
74
|
}
|
|
77
75
|
systemPrompt += `
|
|
78
76
|
Description to convert:`;
|
|
79
|
-
|
|
77
|
+
function sanitize(raw) {
|
|
78
|
+
let command = raw.trim();
|
|
79
|
+
for (const prefix of ["```bash", "```sh", "```"]) {
|
|
80
|
+
if (command.startsWith(prefix)) {
|
|
81
|
+
command = command.slice(prefix.length);
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
command = command.trim();
|
|
86
|
+
command = command.split("\n")[0];
|
|
87
|
+
command = command.trim().replace(/```$/, "");
|
|
88
|
+
command = command.replace(/^`+|`+$/g, "");
|
|
89
|
+
return command.trim();
|
|
90
|
+
}
|
|
91
|
+
async function askLLM(messages) {
|
|
80
92
|
const resp = await fetch(apiUrl, {
|
|
81
93
|
method: "POST",
|
|
82
94
|
headers: {
|
|
83
95
|
"Content-Type": "application/json",
|
|
84
96
|
Authorization: `Bearer ${apiKey}`
|
|
85
97
|
},
|
|
86
|
-
body: JSON.stringify({
|
|
87
|
-
model,
|
|
88
|
-
messages: [
|
|
89
|
-
{ role: "system", content: systemPrompt },
|
|
90
|
-
{ role: "user", content: query }
|
|
91
|
-
],
|
|
92
|
-
stream: false,
|
|
93
|
-
max_tokens: 200
|
|
94
|
-
})
|
|
98
|
+
body: JSON.stringify({ model, messages, stream: false, max_tokens: 200 })
|
|
95
99
|
});
|
|
96
100
|
if (!resp.ok) {
|
|
97
101
|
const body = await resp.text();
|
|
@@ -101,42 +105,41 @@ async function main() {
|
|
|
101
105
|
if (!data.choices?.length || !data.choices[0].message?.content) {
|
|
102
106
|
die("No completion returned from API");
|
|
103
107
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
command = command.slice(prefix.length);
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
command = command.trim();
|
|
112
|
-
command = command.split("\n")[0];
|
|
113
|
-
command = command.trim().replace(/```$/, "");
|
|
114
|
-
command = command.replace(/^`+|`+$/g, "");
|
|
115
|
-
command = command.trim();
|
|
116
|
-
if (!command) die("API returned empty command");
|
|
117
|
-
process.stderr.write(`\x1B[1;32m\u276F\x1B[0m \x1B[1m${command}\x1B[0m
|
|
118
|
-
`);
|
|
119
|
-
process.stderr.write(`\x1B[2mEnter to run \xB7 Type to refine \xB7 Ctrl+C to cancel\x1B[0m
|
|
120
|
-
`);
|
|
108
|
+
return sanitize(data.choices[0].message.content);
|
|
109
|
+
}
|
|
110
|
+
function prompt() {
|
|
121
111
|
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
122
|
-
|
|
112
|
+
return new Promise((resolve) => {
|
|
123
113
|
rl.question("\x1B[2m\u203A \x1B[0m", (input) => {
|
|
124
114
|
rl.close();
|
|
125
115
|
resolve(input.trim());
|
|
126
116
|
});
|
|
127
117
|
});
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
|
|
118
|
+
}
|
|
119
|
+
async function main() {
|
|
120
|
+
const messages = [
|
|
121
|
+
{ role: "system", content: systemPrompt },
|
|
122
|
+
{ role: "user", content: query }
|
|
123
|
+
];
|
|
124
|
+
let command = await askLLM(messages);
|
|
125
|
+
if (!command) die("API returned empty command");
|
|
126
|
+
messages.push({ role: "assistant", content: command });
|
|
127
|
+
while (true) {
|
|
128
|
+
process.stderr.write(`\x1B[1;32m\u276F\x1B[0m \x1B[1m${command}\x1B[0m
|
|
129
|
+
`);
|
|
130
|
+
process.stderr.write(`\x1B[2mEnter to run \xB7 Type to refine \xB7 Ctrl+C to cancel\x1B[0m
|
|
131
|
+
`);
|
|
132
|
+
const answer = await prompt();
|
|
133
|
+
if (!answer) {
|
|
134
|
+
const child = spawn(command, { shell: true, stdio: "inherit" });
|
|
135
|
+
child.on("close", (code) => process.exit(code ?? 0));
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
messages.push({ role: "user", content: answer });
|
|
139
|
+
command = await askLLM(messages);
|
|
140
|
+
if (!command) die("API returned empty command");
|
|
141
|
+
messages.push({ role: "assistant", content: command });
|
|
135
142
|
}
|
|
136
|
-
const child = spawn(command, { shell: true, stdio: "inherit" });
|
|
137
|
-
child.on("close", (code) => {
|
|
138
|
-
process.exit(code ?? 0);
|
|
139
|
-
});
|
|
140
143
|
}
|
|
141
144
|
main().catch((err) => {
|
|
142
145
|
die(err.message);
|