来,用吧😂:
// agent.js
import { execSync } from "node:child_process";
const API = process.env.API_URL ?? "
https://api.openai.com/v1";
const KEY = process.env.API_KEY;
const MODEL = process.env.MODEL ?? "gpt-5.6";
const messages = [{
role: "system",
content: `You are a coding agent with shell access.
Reply ONLY as JSON:
{"cmd":"shell command"} to execute a command
or {"done":"final answer"} when finished.
You may read/write files and execute any command needed.`
}, { role: "user", content: process.argv.slice(2).join(" ") }];
for (;;) {
const r = await fetch(`${API}/chat/completions`, {
method: "POST",
headers: { Authorization: `Bearer ${KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({ model: MODEL, messages })
}).then(r => r.json());
const text = r.choices[0].message.content;
console.log("\nAI:", text);
const x = JSON.parse(text.replace(/^```json\s*|```$/g, ""));
if (x.done) break;
try {
const out = execSync(x.cmd, { encoding: "utf8", timeout: 120000 });
console.log(out);
messages.push({ role: "assistant", content: text });
messages.push({ role: "user", content: `Command output:\n${out}` });
} catch (e) {
messages.push({ role: "assistant", content: text });
messages.push({ role: "user", content: `Command failed:\n${e.stdout ?? ""}\n${e.stderr ?? e.message}` });
}
}
运行:
API_KEY=xxx \
API_URL=
https://your-api.example.com/v1 \
MODEL=your-model \
node agent.js "给老子开发一个 AGI 出来!"