-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·208 lines (180 loc) · 5.31 KB
/
index.js
File metadata and controls
executable file
·208 lines (180 loc) · 5.31 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
#!/usr/bin/env node
const fs = require('fs');
const https = require('https');
const http = require('http');
const args = process.argv.slice(2);
if (args.includes('-h') || args.includes('--help') || args.length === 0) {
console.log(`
🔥 roast-my-code - AI brutally roasts your code
Usage:
roast <file> Roast a file
roast <file> --harsh Extra brutal mode
roast <file> --kind Gentle roasting (for sensitive devs)
cat file.js | roast - Roast from stdin
Environment:
OPENAI_API_KEY Use OpenAI (gpt-4o-mini)
ANTHROPIC_API_KEY Use Anthropic (claude-3-haiku)
OLLAMA_HOST Use Ollama (default: http://localhost:11434)
Examples:
roast src/index.js
roast app.py --harsh
OPENAI_API_KEY=sk-... roast main.go
`);
process.exit(0);
}
const harsh = args.includes('--harsh');
const kind = args.includes('--kind');
const file = args.find(a => !a.startsWith('--'));
let code = '';
async function main() {
// Read code
if (file === '-') {
code = fs.readFileSync(0, 'utf8');
} else if (file && fs.existsSync(file)) {
code = fs.readFileSync(file, 'utf8');
} else {
console.error('Error: File not found');
process.exit(1);
}
if (code.length > 10000) {
code = code.slice(0, 10000) + '\n... [truncated]';
}
const tone = harsh ? 'extremely brutal, savage, no mercy' :
kind ? 'gentle but still funny' :
'brutally honest but hilarious';
const prompt = `You are a code roaster. Your job is to roast the following code in a ${tone} way.
Rules:
- Be funny and creative with your insults
- Point out actual code smells, anti-patterns, and issues
- Use programming humor and references
- Keep it under 300 words
- End with a brutal rating out of 10
CODE TO ROAST:
\`\`\`
${code}
\`\`\`
Now roast this code like it owes you money:`;
console.log('\n🔥 Roasting your code...\n');
try {
const roast = await callAI(prompt);
console.log(roast);
console.log();
} catch (err) {
console.error('Error:', err.message);
console.error('\nMake sure you have set one of: OPENAI_API_KEY, ANTHROPIC_API_KEY, or have Ollama running');
process.exit(1);
}
}
async function callAI(prompt) {
// Try OpenAI
if (process.env.OPENAI_API_KEY) {
return callOpenAI(prompt);
}
// Try Anthropic
if (process.env.ANTHROPIC_API_KEY) {
return callAnthropic(prompt);
}
// Try Ollama
return callOllama(prompt);
}
function callOpenAI(prompt) {
return new Promise((resolve, reject) => {
const data = JSON.stringify({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: prompt }],
max_tokens: 1000
});
const req = https.request({
hostname: 'api.openai.com',
path: '/v1/chat/completions',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
}
}, res => {
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => {
try {
const json = JSON.parse(body);
if (json.error) reject(new Error(json.error.message));
else resolve(json.choices[0].message.content);
} catch (e) {
reject(new Error('Failed to parse OpenAI response'));
}
});
});
req.on('error', reject);
req.write(data);
req.end();
});
}
function callAnthropic(prompt) {
return new Promise((resolve, reject) => {
const data = JSON.stringify({
model: 'claude-3-haiku-20240307',
max_tokens: 1000,
messages: [{ role: 'user', content: prompt }]
});
const req = https.request({
hostname: 'api.anthropic.com',
path: '/v1/messages',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.ANTHROPIC_API_KEY,
'anthropic-version': '2023-06-01'
}
}, res => {
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => {
try {
const json = JSON.parse(body);
if (json.error) reject(new Error(json.error.message));
else resolve(json.content[0].text);
} catch (e) {
reject(new Error('Failed to parse Anthropic response'));
}
});
});
req.on('error', reject);
req.write(data);
req.end();
});
}
function callOllama(prompt) {
return new Promise((resolve, reject) => {
const host = process.env.OLLAMA_HOST || 'http://localhost:11434';
const url = new URL('/api/generate', host);
const data = JSON.stringify({
model: 'llama3.2',
prompt: prompt,
stream: false
});
const proto = url.protocol === 'https:' ? https : http;
const req = proto.request({
hostname: url.hostname,
port: url.port,
path: url.pathname,
method: 'POST',
headers: { 'Content-Type': 'application/json' }
}, res => {
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => {
try {
const json = JSON.parse(body);
resolve(json.response);
} catch (e) {
reject(new Error('Failed to parse Ollama response. Is Ollama running?'));
}
});
});
req.on('error', () => reject(new Error('Could not connect to Ollama. Is it running?')));
req.write(data);
req.end();
});
}
main();