feat(ai): 新增 AI 模块并集成至备课/错题集/试卷/改题四大业务场景
- 新增 src/modules/ai 独立模块,遵循三层架构(actions → services → shared/lib/ai) - 通过 AiClientProvider + useAiClient 实现 React Context 依赖注入,业务组件零直接 import - 6 个 Server Actions 均调用 requirePermission() 权限校验,返回 ActionState<T> - withAiTracking 统一埋点,覆盖 chat/similar_question/grading_assist/lesson_content/question_variant/weakness_analysis - 集成场景:作业批改 AiGradingAssist、错题集 AiErrorBookAnalysis、备课 AiLessonContentGenerator、试卷 AiQuestionVariantGenerator - 全量 i18n(en/zh-CN ai.json),Error Boundary + Skeleton 边界处理 - 同步架构图 004/005,新增审计报告 ai-module-audit-report.md
This commit is contained in:
@@ -1,24 +1,43 @@
|
||||
import "server-only";
|
||||
|
||||
import { z } from "zod";
|
||||
import { env } from "@/env.mjs";
|
||||
import { createAiChatCompletion } from "@/shared/lib/ai";
|
||||
import {
|
||||
getKnowledgePointsByTextbookId,
|
||||
getKnowledgePointsByChapterId,
|
||||
} from "@/modules/textbooks/data-access";
|
||||
import type { LessonPlanDocument } from "./types";
|
||||
|
||||
const SuggestedKpSchema = z.object({
|
||||
id: z.string().min(1),
|
||||
name: z.string().min(1),
|
||||
reason: z.string(),
|
||||
});
|
||||
|
||||
const SuggestedKpListSchema = z.array(SuggestedKpSchema);
|
||||
|
||||
/** 从 unknown 节点安全提取文本(类型守卫从 unknown 收窄) */
|
||||
const extractNodeText = (node: unknown): string => {
|
||||
if (!node || typeof node !== "object") return ""
|
||||
// 从 unknown 收窄为 Record<string, unknown> 以进行字段检查
|
||||
const record = node as Record<string, unknown>
|
||||
const data = record.data
|
||||
if (!data || typeof data !== "object") return ""
|
||||
// 从 unknown 收窄为 Record<string, unknown> 以进行字段检查
|
||||
const dataRecord = data as Record<string, unknown>
|
||||
const html = typeof dataRecord.html === "string" ? dataRecord.html : ""
|
||||
const sourceText = typeof dataRecord.sourceText === "string" ? dataRecord.sourceText : ""
|
||||
return html || sourceText || ""
|
||||
}
|
||||
|
||||
export async function suggestKnowledgePoints(
|
||||
doc: LessonPlanDocument,
|
||||
doc: { nodes: unknown[] },
|
||||
textbookId?: string,
|
||||
chapterId?: string,
|
||||
): Promise<{ id: string; name: string; reason: string }[]> {
|
||||
// 1. 提取课案纯文本
|
||||
const text = doc.nodes
|
||||
.map((b) => {
|
||||
const d = b.data as { html?: string; sourceText?: string };
|
||||
return d.html ?? d.sourceText ?? "";
|
||||
})
|
||||
.map((b) => extractNodeText(b))
|
||||
.join("\n")
|
||||
.slice(0, 3000);
|
||||
|
||||
@@ -51,14 +70,12 @@ ${text}
|
||||
// 尝试从返回内容中提取 JSON 数组
|
||||
const jsonMatch = content.match(/\[[\s\S]*\]/);
|
||||
if (!jsonMatch) return [];
|
||||
const parsed = JSON.parse(jsonMatch[0]) as {
|
||||
id: string;
|
||||
name: string;
|
||||
reason: string;
|
||||
}[];
|
||||
const parsed: unknown = JSON.parse(jsonMatch[0]);
|
||||
const validated = SuggestedKpListSchema.safeParse(parsed);
|
||||
if (!validated.success) return [];
|
||||
// 过滤掉不在候选池中的 id
|
||||
const validIds = new Set(kpList.map((k) => k.id));
|
||||
return parsed.filter((p) => validIds.has(p.id));
|
||||
return validated.data.filter((p) => validIds.has(p.id));
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user