Skip to content

Commit c137466

Browse files
fix: improve script
1 parent ff5b081 commit c137466

File tree

1 file changed

+90
-47
lines changed

1 file changed

+90
-47
lines changed

.github/scripts/starklings-evaluate.js

Lines changed: 90 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,111 @@ const fs = require('fs');
22
const { execSync } = require('child_process');
33

44
function parseInfoToml(infoPath) {
5-
const fs = require('fs');
65
const content = fs.readFileSync(infoPath, 'utf8');
7-
86
const exercises = [];
97
const lines = content.split('\n');
10-
118
let currentExercise = null;
12-
9+
let collectingHint = false;
10+
let hintLines = [];
11+
1312
for (const line of lines) {
14-
const cleanLine = line.trim();
15-
16-
if (cleanLine.startsWith('[[exercises]]')) {
17-
if (currentExercise) exercises.push(currentExercise);
18-
currentExercise = {};
19-
} else if (cleanLine.startsWith('name = ')) {
20-
currentExercise.name = cleanLine.match(/name = "(.+)"/)[1];
21-
} else if (cleanLine.startsWith('path = ')) {
22-
currentExercise.path = cleanLine.match(/path = "(.+)"/)[1];
23-
} else if (cleanLine.startsWith('mode = ')) {
24-
currentExercise.mode = cleanLine.match(/mode = "(.+)"/)[1];
25-
}
13+
const cleanLine = line.trim();
14+
15+
if (cleanLine.startsWith('[[exercises]]')) {
16+
if (currentExercise) {
17+
if (hintLines.length > 0) {
18+
currentExercise.hint = hintLines.join('\n').replace(/^"""/, '').replace(/"""$/, '');
19+
}
20+
exercises.push(currentExercise);
21+
}
22+
currentExercise = {};
23+
collectingHint = false;
24+
hintLines = [];
25+
} else if (cleanLine.startsWith('hint = """')) {
26+
collectingHint = true;
27+
hintLines.push(cleanLine.replace('hint = """', '').trim());
28+
} else if (collectingHint) {
29+
if (cleanLine.endsWith('"""')) {
30+
hintLines.push(cleanLine.replace('"""', '').trim());
31+
collectingHint = false;
32+
} else {
33+
hintLines.push(cleanLine);
34+
}
35+
} else if (cleanLine.startsWith('name = ')) {
36+
currentExercise.name = cleanLine.match(/name = "(.+)"/)[1];
37+
} else if (cleanLine.startsWith('path = ')) {
38+
currentExercise.path = cleanLine.match(/path = "(.+)"/)[1];
39+
} else if (cleanLine.startsWith('mode = ')) {
40+
currentExercise.mode = cleanLine.match(/mode = "(.+)"/)[1];
41+
}
42+
}
43+
44+
// N'oublie pas le dernier exercice
45+
if (currentExercise) {
46+
if (hintLines.length > 0) {
47+
currentExercise.hint = hintLines.join('\n').replace(/"""$/, '');
48+
}
49+
exercises.push(currentExercise);
2650
}
2751

28-
if (currentExercice) exercises.push(currentExercise);
2952
return exercises;
3053
}
3154

32-
async function callCairoCoderAPI(exerciseContent) {
55+
function getExerciseObjective(exerciseName) {
56+
const objectives = {
57+
'intro1': 'Introduction to Cairo syntax and basic program structure',
58+
'intro2': 'Understanding Cairo compilation and execution',
59+
'variables1': 'Learn to declare variables with the `let` keyword',
60+
'variables2': 'Understand type annotations and basic felt252 type',
61+
'variables3': 'Learn to initialize variables with values',
62+
'variables4': 'Understand mutability with the `mut` keyword',
63+
'variables5': 'Learn about variable shadowing',
64+
'variables6': 'Understand constants with the `const` keyword',
65+
// ... continuer pour tous les exercices
66+
};
67+
return objectives[exerciseName] || 'Practice Cairo programming concepts';
68+
}
69+
70+
async function callCairoCoderAPI(exerciseContent, exercise) {
71+
const prompt = `You are solving a Cairo programming exercise.
72+
73+
Exercise: ${exercise.name}
74+
Objective: ${getExerciseObjective(exercise.name)}
75+
${exercise.hint ? `Hint: ${exercise.hint}` : ''}
76+
77+
Instructions:
78+
1. Read and understand the exercise requirements
79+
2. Fix any compilation errors
80+
3. Remove the "// I AM NOT DONE" comment when complete
81+
4. Ensure the solution demonstrates the intended concept
82+
83+
Code to fix:
84+
${exerciseContent}`;
85+
3386
const response = await fetch('http://localhost:3001/v1/chat/completions', {
34-
method: 'POST',
35-
headers: {
36-
'Content-Type': 'application/json'
37-
},
38-
body: JSON.stringify({
39-
model: 'cairo-coder',
40-
messages: [
41-
{
42-
role: 'user',
43-
content: `Complete this Cairo exercise by fixing the code. Remove the "// I AM NOT DONE" comment and fix any compilation errors:\n\n${exerciseContent}`
44-
}
45-
]
46-
})
87+
method: 'POST',
88+
headers: { 'Content-Type': 'application/json' },
89+
body: JSON.stringify({
90+
model: 'cairo-coder',
91+
messages: [{ role: 'user', content: prompt }]
92+
})
4793
});
48-
49-
const data = await response.json();
50-
return data.choices[0].message.content;
51-
}
94+
95+
return response.json();
96+
}
5297

5398
async function main() {
54-
// 1. Parser info.toml pour lister tous les exercices
55-
const exercises = parseInfoToml('./starklings/info.toml');
56-
57-
let passed = 0;
58-
let total = exercises.length;
59-
60-
for (const exercise of exercises) {
61-
console.log(`Testing ${exercise.name}...`);
62-
const exerciseContent = fs.readFileSync(`./starklings/${exercise.path}`, 'utf8');
63-
const response = await callCairoCoderAPI(exerciseContent);
64-
fs.writeFileSync(`./starklings/${exercise.path}`, response);
99+
const exercises = parseInfoToml('./starklings/info.toml');
100+
let passed = 0;
101+
let total = exercises.length;
65102

66-
// Tester avec starklings
103+
for (const exercise of exercises) {
104+
console.log(`Testing ${exercise.name}...`);
105+
const exerciseContent = fs.readFileSync(`./starklings/${exercise.path}`, 'utf8');
106+
const response = await callCairoCoderAPI(exerciseContent, exercise);
107+
108+
fs.writeFileSync(`./starklings/${exercise.path}`, response);
109+
67110
try {
68111
execSync(`cd starklings && cargo run -r --bin starklings run ${exercise.name}`, { stdio: 'pipe' });
69112
console.log(`✅ ${exercise.name}`);

0 commit comments

Comments
 (0)