-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupercode.mjs
More file actions
234 lines (205 loc) · 9.05 KB
/
Copy pathsupercode.mjs
File metadata and controls
234 lines (205 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env node
/**
* SuperCode — single-file, zero-dependency AI coding agent
* Requires: Node.js 18+ | Set OPENROUTER_API_KEY env var
* Usage: node supercode.mjs [prompt]
*/
import readline from "readline";
import fs from "fs";
import path from "path";
import { execSync } from "child_process";
import { createRequire } from "module";
const MODEL = process.env.SUPERCODE_MODEL || "deepseek/deepseek-r1";
const API_KEY = process.env.OPENROUTER_API_KEY || process.env.GEMINI_API_KEY || "";
const BASE_URL = "https://openrouter.ai/api/v1/chat/completions";
const CWD = process.cwd();
// ── Colours ───────────────────────────────────────────────────────────────────
const c = {
reset: "\x1b[0m", bold: "\x1b[1m",
purple: "\x1b[35m", cyan: "\x1b[36m",
green: "\x1b[32m", red: "\x1b[31m",
grey: "\x1b[90m", yellow: "\x1b[33m",
};
const p = (color, txt) => process.stdout.write(color + txt + c.reset);
// ── System prompt ─────────────────────────────────────────────────────────────
const SYSTEM = `You are SuperCode, an expert AI coding agent running in ${CWD}.
You can use these tools by outputting JSON blocks:
<tool>{"name":"read","path":"src/index.ts"}</tool>
<tool>{"name":"write","path":"src/foo.ts","content":"..."}</tool>
<tool>{"name":"edit","path":"src/foo.ts","old":"old text","new":"new text"}</tool>
<tool>{"name":"bash","cmd":"npm test"}</tool>
<tool>{"name":"glob","pattern":"**/*.ts"}</tool>
<tool>{"name":"grep","pattern":"TODO","path":"."}</tool>
Rules:
- Always read files before editing them
- Use bash to run tests after making changes
- Make targeted edits, not full rewrites
- Think step by step before acting`;
// ── Tools ─────────────────────────────────────────────────────────────────────
function runTool(call) {
try {
switch (call.name) {
case "read": {
const abs = path.resolve(CWD, call.path);
if (!fs.existsSync(abs)) return `File not found: ${abs}`;
const lines = fs.readFileSync(abs, "utf8").split("\n");
const start = (call.offset || 1) - 1;
const end = call.limit ? start + call.limit : lines.length;
return lines.slice(start, end).map((l,i)=>`${start+i+1}: ${l}`).join("\n");
}
case "write": {
const abs = path.resolve(CWD, call.path);
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, call.content, "utf8");
return `Written ${call.content.length} bytes to ${call.path}`;
}
case "edit": {
const abs = path.resolve(CWD, call.path);
if (!fs.existsSync(abs)) return `File not found: ${abs}`;
let content = fs.readFileSync(abs, "utf8");
if (!content.includes(call.old)) return `oldString not found in ${call.path}`;
content = content.replace(call.old, call.new);
fs.writeFileSync(abs, content, "utf8");
return `Edited ${call.path}`;
}
case "bash": {
const out = execSync(call.cmd, { cwd: CWD, timeout: 30000, encoding: "utf8", stdio: ["pipe","pipe","pipe"] });
return out || "(no output)";
}
case "glob": {
const result = execSync(`find ${CWD} -type f -name "${call.pattern.replace("**/*","*")}" 2>/dev/null | grep -v node_modules | grep -v .git | grep -v dist | head -50`, { encoding:"utf8" });
return result || "No files found";
}
case "grep": {
try {
const result = execSync(`grep -rn "${call.pattern}" ${path.resolve(CWD, call.path||".")} --include="*.ts" --include="*.js" --include="*.tsx" --include="*.jsx" --include="*.py" -l 2>/dev/null | head -20`, { encoding:"utf8" });
return result || "No matches";
} catch { return "No matches"; }
}
default:
return `Unknown tool: ${call.name}`;
}
} catch (err) {
return `Error: ${err.message}`;
}
}
// ── Parse tool calls from model output ───────────────────────────────────────
function parseTools(text) {
const calls = [];
const re = /<tool>([\s\S]*?)<\/tool>/g;
let m;
while ((m = re.exec(text)) !== null) {
try { calls.push(JSON.parse(m[1])); } catch {}
}
return calls;
}
// ── Stream from OpenRouter ────────────────────────────────────────────────────
async function streamChat(messages) {
if (!API_KEY) {
console.error(`\n${c.red}✗ No API key found.${c.reset}`);
console.error(` Set your OpenRouter key:\n export OPENROUTER_API_KEY=sk-or-v1-...\n`);
process.exit(1);
}
const res = await fetch(BASE_URL, {
method: "POST",
headers: {
"Authorization": `Bearer ${API_KEY}`,
"Content-Type": "application/json",
"HTTP-Referer": "https://github.com/jbellsolutions/supercode",
"X-Title": "SuperCode",
},
body: JSON.stringify({ model: MODEL, messages, stream: true }),
});
if (!res.ok) {
const err = await res.text();
throw new Error(`API error ${res.status}: ${err}`);
}
let fullText = "";
const decoder = new TextDecoder();
let buf = "";
for await (const chunk of res.body) {
buf += decoder.decode(chunk, { stream: true });
const lines = buf.split("\n");
buf = lines.pop();
for (const line of lines) {
if (!line.startsWith("data: ")) continue;
const data = line.slice(6).trim();
if (data === "[DONE]") continue;
try {
const delta = JSON.parse(data).choices?.[0]?.delta?.content || "";
if (delta) {
process.stdout.write(c.reset + delta);
fullText += delta;
}
} catch {}
}
}
console.log();
return fullText;
}
// ── Agent loop ────────────────────────────────────────────────────────────────
async function runAgent(userPrompt, messages = []) {
messages.push({ role: "user", content: userPrompt });
let turns = 0;
const MAX = 20;
while (turns++ < MAX) {
p(c.grey, `\n[${MODEL}] `);
const reply = await streamChat([
{ role: "system", content: SYSTEM },
...messages,
]);
messages.push({ role: "assistant", content: reply });
const tools = parseTools(reply);
if (tools.length === 0) break; // done
for (const call of tools) {
p(c.cyan, `\n⚙ ${call.name}`);
if (call.path) p(c.grey, ` ${call.path}`);
if (call.cmd) p(c.grey, ` ${call.cmd}`);
console.log();
const result = runTool(call);
const preview = result.split("\n").slice(0, 5).join("\n");
p(c.green, ` ✓ ${preview.slice(0, 200)}\n`);
messages.push({ role: "user", content: `<tool_result name="${call.name}">${result}</tool_result>` });
}
}
return messages;
}
// ── REPL ──────────────────────────────────────────────────────────────────────
async function repl() {
console.log(`\n${c.bold}${c.purple} ⚡ SuperCode${c.reset} ${c.grey}${MODEL} · ${CWD}${c.reset}`);
console.log(`${c.grey} Type your prompt. /exit to quit. /model <name> to switch.\n${c.reset}`);
if (!API_KEY) {
console.log(`${c.yellow} ⚠ No OPENROUTER_API_KEY set.${c.reset}`);
console.log(`${c.grey} Get a free key at https://openrouter.ai/keys${c.reset}`);
console.log(`${c.grey} Then run: OPENROUTER_API_KEY=sk-or-v1-... node supercode.mjs\n${c.reset}`);
}
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
let messages = [];
const prompt = () => {
rl.question(`\n${c.purple}❯${c.reset} `, async (input) => {
const line = input.trim();
if (!line) return prompt();
if (line === "/exit" || line === "/quit") { rl.close(); return; }
if (line === "/clear") { messages = []; p(c.grey, " History cleared.\n"); return prompt(); }
if (line.startsWith("/model ")) {
process.env.SUPERCODE_MODEL = line.slice(7).trim();
p(c.green, ` Model: ${process.env.SUPERCODE_MODEL}\n`);
return prompt();
}
try {
messages = await runAgent(line, messages);
} catch (err) {
p(c.red, `\n✗ ${err.message}\n`);
}
prompt();
});
};
prompt();
}
// ── Entry point ───────────────────────────────────────────────────────────────
const prompt = process.argv.slice(2).join(" ");
if (prompt) {
runAgent(prompt).catch(err => { console.error(err.message); process.exit(1); });
} else {
repl();
}