import { TeacherError } from './config-client'; export interface TeacherSuggestions { intro: string; questions: string[]; } /** Validate model output before it can become interactive student questions. */ export function parseTeacherSuggestions(response: string): TeacherSuggestions { const invalid = () => new TeacherError( 502, 'teacher_suggestions_invalid', '老师这次没能整理好可以讨论的问题,请再试一次,或直接告诉老师你的想法。' ); const text = response.trim(); const fenced = text.match(/^```(?:json)?\s*\n([\s\S]*?)\n```$/i); let parsed: unknown; try { parsed = JSON.parse(fenced ? fenced[1] : text); } catch { throw invalid(); } if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) throw invalid(); const { intro, questions } = parsed as Record; if ( typeof intro !== 'string' || !intro.trim() || intro.trim().length > 400 || !Array.isArray(questions) || questions.length < 2 || questions.length > 3 || questions.some((question) => typeof question !== 'string' || !question.trim() || question.trim().length > 120) ) throw invalid(); const uniqueQuestions = [...new Set(questions.map((question: string) => question.trim()))]; if (uniqueQuestions.length < 2) throw invalid(); return { intro: intro.trim(), questions: uniqueQuestions }; }