import {z} from 'zod' export type CreateApiStudentExamsExamIdGenerateCorrectionArgs = {exam_id: string}; export type CreateApiStudentExamsExamIdGenerateCorrectionOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const studentExamPerformance = z.object({total_questions: z.number().int(), correct: z.number().int(), partial: z.number().int(), wrong: z.number().int()}); export const examExerciseOutput = z.object({id: z.number().int(), title: z.string(), content: z.string(), solution: z.string().nullable().optional(), points: z.number().optional(), figure_id: z.string().nullable().optional(), solution_figure_id: z.string().nullable().optional(), figure_content: z.string().nullable().optional(), figure_solution: z.string().nullable().optional(), preamble: z.string().nullable().optional(), estimated_minutes: z.number().int().nullable().optional()}); export const studentExam = z.object({_id: z.string().optional(), user_id: z.string(), exam_title: z.string(), time_limit: z.string(), exam_subject_images: z.array(z.string()).optional(), exam_subject_transcribed: z.string().optional(), exam_started_at: z.string().nullable().optional(), exam_finished_at: z.string().nullable().optional(), time_spent_seconds: z.number().int().nullable().optional(), student_work_images: z.array(z.string()).optional(), student_work_diagrams: z.array(z.string()).optional(), student_work_transcribed: z.string().optional(), correction_transcribed: z.string().optional(), performance: studentExamPerformance.nullable().optional(), feedback: z.string().nullable().optional(), structured_exercises: z.array(examExerciseOutput).optional(), created_at: z.string().optional(), updated_at: z.string().nullable().optional(), is_finished: z.boolean().optional()}); export const generateFeedbackResponse = z.object({status: z.string(), exam: studentExam}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Generate Exam Correction * * POST /student-exams/{exam_id}/generate-correction */ export const createApiStudentExamsExamIdGenerateCorrection = async (args: CreateApiStudentExamsExamIdGenerateCorrectionArgs, options: CreateApiStudentExamsExamIdGenerateCorrectionOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/student-exams/${encodeURIComponent(String(args.exam_id))}/generate-correction${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(generateFeedbackResponse, await res.json()) };