heyeric 1.1.0 → 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 +44 -39
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -74,22 +74,28 @@ Recent command history:
|
|
|
74
74
|
}
|
|
75
75
|
systemPrompt += `
|
|
76
76
|
Description to convert:`;
|
|
77
|
-
|
|
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) {
|
|
78
92
|
const resp = await fetch(apiUrl, {
|
|
79
93
|
method: "POST",
|
|
80
94
|
headers: {
|
|
81
95
|
"Content-Type": "application/json",
|
|
82
96
|
Authorization: `Bearer ${apiKey}`
|
|
83
97
|
},
|
|
84
|
-
body: JSON.stringify({
|
|
85
|
-
model,
|
|
86
|
-
messages: [
|
|
87
|
-
{ role: "system", content: systemPrompt },
|
|
88
|
-
{ role: "user", content: query }
|
|
89
|
-
],
|
|
90
|
-
stream: false,
|
|
91
|
-
max_tokens: 200
|
|
92
|
-
})
|
|
98
|
+
body: JSON.stringify({ model, messages, stream: false, max_tokens: 200 })
|
|
93
99
|
});
|
|
94
100
|
if (!resp.ok) {
|
|
95
101
|
const body = await resp.text();
|
|
@@ -99,42 +105,41 @@ async function main() {
|
|
|
99
105
|
if (!data.choices?.length || !data.choices[0].message?.content) {
|
|
100
106
|
die("No completion returned from API");
|
|
101
107
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
command = command.slice(prefix.length);
|
|
106
|
-
break;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
command = command.trim();
|
|
110
|
-
command = command.split("\n")[0];
|
|
111
|
-
command = command.trim().replace(/```$/, "");
|
|
112
|
-
command = command.replace(/^`+|`+$/g, "");
|
|
113
|
-
command = command.trim();
|
|
114
|
-
if (!command) die("API returned empty command");
|
|
115
|
-
process.stderr.write(`\x1B[1;32m\u276F\x1B[0m \x1B[1m${command}\x1B[0m
|
|
116
|
-
`);
|
|
117
|
-
process.stderr.write(`\x1B[2mEnter to run \xB7 Type to refine \xB7 Ctrl+C to cancel\x1B[0m
|
|
118
|
-
`);
|
|
108
|
+
return sanitize(data.choices[0].message.content);
|
|
109
|
+
}
|
|
110
|
+
function prompt() {
|
|
119
111
|
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
120
|
-
|
|
112
|
+
return new Promise((resolve) => {
|
|
121
113
|
rl.question("\x1B[2m\u203A \x1B[0m", (input) => {
|
|
122
114
|
rl.close();
|
|
123
115
|
resolve(input.trim());
|
|
124
116
|
});
|
|
125
117
|
});
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
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 });
|
|
133
142
|
}
|
|
134
|
-
const child = spawn(command, { shell: true, stdio: "inherit" });
|
|
135
|
-
child.on("close", (code) => {
|
|
136
|
-
process.exit(code ?? 0);
|
|
137
|
-
});
|
|
138
143
|
}
|
|
139
144
|
main().catch((err) => {
|
|
140
145
|
die(err.message);
|