2026-08-31 16:48:49 +08:00

29 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { FastifyRequest } from 'fastify'
import { readData } from '../data/store.js'
import { dataFiles } from '../data/files.js'
import { ApiError } from '../errors.js'
import type { Question } from '../schemas/entities.js'
import type { z } from 'zod'
import { AiExplainBodySchema } from '../schemas/api.js'
/**
* AI 讲解:返回结构化的讲解结果。
* TODO 任务11组装题目上下文调用 OpenAI 兼容接口,超时或未配置 Token 时回退到题库解析。
* 当前固定返回题库解析回退结果,保证接口契约与前端可以先联调。
*/
export async function explainQuestion(request: FastifyRequest) {
const body = request.body as z.infer<typeof AiExplainBodySchema>
const questions = await readData<Question[]>(dataFiles.questions, [])
const question = questions.find((q) => q.id === body.questionId)
if (!question) throw ApiError.notFound('题目不存在')
return {
source: 'fallback' as const,
summary: `AI 讲解尚未接入,已回退到题库标准解析(用户答案:${body.userAnswer}`,
steps: [question.analysis],
knowledgePoints: question.tags,
commonMistakes: [],
answer: question.answer
}
}