feat(*): Add AI functionality to grading endpoint

This commit is contained in:
Josh Creek
2025-04-16 23:11:48 +01:00
parent e3a58ccfb2
commit 6eef08c86b
3 changed files with 131 additions and 18 deletions
+33 -18
View File
@@ -1,25 +1,40 @@
import type { RequestHandler } from '@sveltejs/kit';
import type { RequestEvent } from '@sveltejs/kit';
import { gradeWithOpenAI } from '$lib/utils/ai';
// TODO - don't just mock the grading logic
export const POST: RequestHandler = async (event: RequestEvent) => {
const data = await event.request.formData();
const file = data.get('file');
const text = data.get('text');
try {
const data = await event.request.formData();
const file = data.get('file');
const text = data.get('text');
let submission = '';
// Simulation of grading and feedback
let feedback = '';
if (file) {
feedback = 'Received file: ' + (file as File).name + '. [AI feedback coming soon]';
} else if (typeof text === 'string' && text.trim().length > 0) {
feedback = 'Received text submission. [AI feedback coming soon]';
} else {
return new Response(JSON.stringify({ error: 'No valid input provided.' }), { status: 400 });
if (file && typeof file === 'object' && 'arrayBuffer' in file && 'name' in file) {
// Only support .txt for now for hackathon speed
const fileObj = file as File;
if (!fileObj.name.endsWith('.txt')) {
return new Response(JSON.stringify({ error: 'Only .txt files are supported at this time.' }), { status: 400 });
}
const buffer = await fileObj.arrayBuffer();
submission = new TextDecoder('utf-8').decode(buffer);
} else if (typeof text === 'string' && text.trim().length > 0) {
submission = text.trim();
} else {
return new Response(JSON.stringify({ error: 'No valid input provided.' }), { status: 400 });
}
// Call the AI grading agent
const aiResult = await gradeWithOpenAI(submission);
return new Response(JSON.stringify({
success: true,
feedback: `Grade: ${aiResult.grade}\nStrengths: ${aiResult.strengths}\nWeaknesses: ${aiResult.weaknesses}\nIndividualized Activity: ${aiResult.individualized_activity}\nReasoning: ${aiResult.reasoning}`,
...aiResult
}), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
} catch (err: any) {
return new Response(JSON.stringify({ error: err.message || 'Internal server error.' }), { status: 500 });
}
// Return mock feedback for now
return new Response(JSON.stringify({ success: true, feedback }), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
};