heyeric 1.0.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 +120 -0
- package/package.json +21 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { execSync } from "child_process";
|
|
5
|
+
import { readFileSync } from "fs";
|
|
6
|
+
import { platform } from "os";
|
|
7
|
+
import { basename } from "path";
|
|
8
|
+
function die(msg) {
|
|
9
|
+
process.stderr.write(`eric: ${msg}
|
|
10
|
+
`);
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
var query = process.argv.slice(2).join(" ").trim();
|
|
14
|
+
if (!query) die("Usage: eric <query>");
|
|
15
|
+
var apiUrl = process.env.AI_API_URL;
|
|
16
|
+
var apiKey = process.env.AI_API_KEY;
|
|
17
|
+
var model = process.env.AI_MODEL || "gpt-5.4-mini";
|
|
18
|
+
if (!apiUrl) die("AI_API_URL environment variable is required");
|
|
19
|
+
if (!apiKey) die("AI_API_KEY environment variable is required");
|
|
20
|
+
var cwd = process.cwd();
|
|
21
|
+
var os = platform();
|
|
22
|
+
var shell = basename(process.env.SHELL || "/bin/bash");
|
|
23
|
+
function getLs() {
|
|
24
|
+
try {
|
|
25
|
+
const out = execSync("ls -la", { encoding: "utf-8", timeout: 3e3 });
|
|
26
|
+
return out.split("\n").slice(0, 50).join("\n");
|
|
27
|
+
} catch {
|
|
28
|
+
return "";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function getHistory() {
|
|
32
|
+
try {
|
|
33
|
+
const home = process.env.HOME || "";
|
|
34
|
+
const histPath = shell === "zsh" ? `${home}/.zsh_history` : shell === "fish" ? `${home}/.local/share/fish/fish_history` : `${home}/.bash_history`;
|
|
35
|
+
const raw = readFileSync(histPath, "utf-8");
|
|
36
|
+
const lines = raw.trim().split("\n").slice(-20);
|
|
37
|
+
return lines.map((line) => {
|
|
38
|
+
if (shell === "zsh" && line.startsWith(": ")) {
|
|
39
|
+
const idx = line.indexOf(";");
|
|
40
|
+
if (idx !== -1) return line.substring(idx + 1);
|
|
41
|
+
}
|
|
42
|
+
if (shell === "fish" && line.startsWith("- cmd: ")) {
|
|
43
|
+
return line.substring(7);
|
|
44
|
+
}
|
|
45
|
+
return line;
|
|
46
|
+
});
|
|
47
|
+
} catch {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
var lsOutput = getLs();
|
|
52
|
+
var history = getHistory();
|
|
53
|
+
var systemPrompt = `You are a bash command generator.
|
|
54
|
+
Convert the user's natural-language description into a single bash command.
|
|
55
|
+
Return ONLY the command. No explanation. No markdown. No backticks. No newlines.
|
|
56
|
+
|
|
57
|
+
OS: ${os}
|
|
58
|
+
Shell: ${shell}
|
|
59
|
+
Current directory: ${cwd}
|
|
60
|
+
`;
|
|
61
|
+
if (lsOutput) {
|
|
62
|
+
systemPrompt += `
|
|
63
|
+
Directory contents:
|
|
64
|
+
${lsOutput}
|
|
65
|
+
`;
|
|
66
|
+
}
|
|
67
|
+
if (history.length > 0) {
|
|
68
|
+
systemPrompt += `
|
|
69
|
+
Recent command history:
|
|
70
|
+
`;
|
|
71
|
+
for (const h of history) {
|
|
72
|
+
systemPrompt += ` ${h}
|
|
73
|
+
`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
systemPrompt += `
|
|
77
|
+
Description to convert:`;
|
|
78
|
+
async function main() {
|
|
79
|
+
const resp = await fetch(apiUrl, {
|
|
80
|
+
method: "POST",
|
|
81
|
+
headers: {
|
|
82
|
+
"Content-Type": "application/json",
|
|
83
|
+
Authorization: `Bearer ${apiKey}`
|
|
84
|
+
},
|
|
85
|
+
body: JSON.stringify({
|
|
86
|
+
model,
|
|
87
|
+
messages: [
|
|
88
|
+
{ role: "system", content: systemPrompt },
|
|
89
|
+
{ role: "user", content: query }
|
|
90
|
+
],
|
|
91
|
+
stream: false,
|
|
92
|
+
max_tokens: 200
|
|
93
|
+
})
|
|
94
|
+
});
|
|
95
|
+
if (!resp.ok) {
|
|
96
|
+
const body = await resp.text();
|
|
97
|
+
die(`API error ${resp.status}: ${body}`);
|
|
98
|
+
}
|
|
99
|
+
const data = await resp.json();
|
|
100
|
+
if (!data.choices?.length || !data.choices[0].message?.content) {
|
|
101
|
+
die("No completion returned from API");
|
|
102
|
+
}
|
|
103
|
+
let command = data.choices[0].message.content.trim();
|
|
104
|
+
for (const prefix of ["```bash", "```sh", "```"]) {
|
|
105
|
+
if (command.startsWith(prefix)) {
|
|
106
|
+
command = command.slice(prefix.length);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
command = command.trim();
|
|
111
|
+
command = command.split("\n")[0];
|
|
112
|
+
command = command.trim().replace(/```$/, "");
|
|
113
|
+
command = command.replace(/^`+|`+$/g, "");
|
|
114
|
+
command = command.trim();
|
|
115
|
+
if (!command) die("API returned empty command");
|
|
116
|
+
process.stdout.write(command + "\n");
|
|
117
|
+
}
|
|
118
|
+
main().catch((err) => {
|
|
119
|
+
die(err.message);
|
|
120
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "heyeric",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Natural language to bash commands via LLM",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"eric": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsup src/index.ts --format esm --clean",
|
|
11
|
+
"dev": "tsx src/index.ts"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"tsup": "^8",
|
|
18
|
+
"tsx": "^4",
|
|
19
|
+
"typescript": "^5"
|
|
20
|
+
}
|
|
21
|
+
}
|