Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/app/api/error-items/[id]/geogebra/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,18 @@ export async function POST(
}

const aiService = getAIService();

// Use frontend-provided data if available (more up-to-date), fall back to DB
const body = await req.json().catch(() => ({}));
const questionText = body.questionText || errorItem.questionText;
const answerText = body.answerText || errorItem.answerText || "";
const previousErrors = body.previousErrors || "";

const result = await aiService.analyzeForGeogebra(
errorItem.questionText,
errorItem.answerText || "",
errorItem.analysis || ""
questionText,
answerText,
errorItem.analysis || "",
previousErrors
);

// If suitable, save the commands to the database
Expand Down
591 changes: 112 additions & 479 deletions src/app/error-items/[id]/page.tsx

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions src/components/geogebra-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ interface GeogebraDemoProps {
showAlgebraInput?: boolean;
showMenuBar?: boolean;
className?: string;
/** Called when user clicks "重新生成" — parent should re-call AI and update commands */
onRegenerate?: () => Promise<void>;
onRegenerate?: (errors?: string) => Promise<void>;
onSaveCommands?: (commands: string) => Promise<void>;
}

// ── Singleton script loader ─────────────────────────────────────────────
Expand Down Expand Up @@ -123,7 +123,7 @@ function runCommands(api: any, cmds: string[]) {
// ── Component ───────────────────────────────────────────────────────────
export function GeogebraDemo({
commands,
height = 450,
height = 700,
showToolBar = false,
showAlgebraInput = false,
showMenuBar = false,
Expand All @@ -139,7 +139,7 @@ export function GeogebraDemo({

const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [expanded, setExpanded] = useState(false);
const [expanded, setExpanded] = useState(true);
const [regenerating, setRegenerating] = useState(false);

const cmds = useMemo(() => parseCommands(commands), [commands]);
Expand Down Expand Up @@ -177,6 +177,12 @@ export function GeogebraDemo({
appletOnLoad: (api: any) => {
if (dead) return;
apiRef.current = api;
const ggbDiv = ggbHostRef.current?.firstElementChild as HTMLElement | null;
if (ggbDiv) {
ggbDiv.style.width = '100%';
const w = ggbHostRef.current?.offsetWidth || ggbDiv.offsetWidth || 800;
api.setSize(w, height);
}
runCommands(api, cmds);
setLoading(false);
},
Expand Down Expand Up @@ -217,9 +223,13 @@ export function GeogebraDemo({
const ggbDiv = ggbHostRef.current?.firstElementChild as HTMLElement | null;
if (!ggbDiv) return;
const h = expanded ? 700 : height;
ggbDiv.style.width = '100%';
ggbDiv.style.height = `${h}px`;
const api = apiRef.current;
if (api?.setSize) api.setSize(ggbDiv.offsetWidth, h);
if (api?.setSize) {
const w = ggbHostRef.current?.offsetWidth || ggbDiv.offsetWidth || 800;
api.setSize(w, h);
}
}, [expanded, height]);

// ── Reset ───────────────────────────────────────────────────────────
Expand Down Expand Up @@ -284,7 +294,7 @@ export function GeogebraDemo({
* The effect writes innerHTML directly, so React never
* tries to reconcile DOM nodes inside this div.
*/}
<div ref={ggbHostRef} />
<div ref={ggbHostRef} style={{ width: '100%' }} />

{/* Loading overlay — sibling, not child of ggbHost */}
{loading && (
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ai/azure-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ Knowledge Points: ${knowledgePoints.join(", ")}
}
}

async analyzeForGeogebra(questionText: string, answerText: string, analysis: string): Promise<GeogebraAnalysisResult> {
const prompt = generateGeogebraPrompt(questionText, answerText, analysis);
async analyzeForGeogebra(questionText: string, answerText: string, analysis: string, previousErrors?: string): Promise<GeogebraAnalysisResult> {
const prompt = generateGeogebraPrompt(questionText, answerText, analysis, previousErrors);

logger.info({
provider: 'Azure OpenAI',
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ai/gemini-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ export class GeminiProvider implements AIService {
}
}

async analyzeForGeogebra(questionText: string, answerText: string, analysis: string): Promise<GeogebraAnalysisResult> {
const prompt = generateGeogebraPrompt(questionText, answerText, analysis);
async analyzeForGeogebra(questionText: string, answerText: string, analysis: string, previousErrors?: string): Promise<GeogebraAnalysisResult> {
const prompt = generateGeogebraPrompt(questionText, answerText, analysis, previousErrors);

logger.info({
provider: 'Gemini',
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ai/openai-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ export class OpenAIProvider implements AIService {
}
}

async analyzeForGeogebra(questionText: string, answerText: string, analysis: string): Promise<GeogebraAnalysisResult> {
const prompt = generateGeogebraPrompt(questionText, answerText, analysis);
async analyzeForGeogebra(questionText: string, answerText: string, analysis: string, previousErrors?: string): Promise<GeogebraAnalysisResult> {
const prompt = generateGeogebraPrompt(questionText, answerText, analysis, previousErrors);

logger.info({
provider: 'OpenAI',
Expand Down
27 changes: 24 additions & 3 deletions src/lib/ai/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ export const DEFAULT_GEOGEBRA_PROMPT = `【角色与核心任务 (ROLE AND CORE
【解析内容 (ANALYSIS)】
{{analysis}}

{{error_feedback}}

【判断标准 (SUITABILITY CRITERIA)】
适合用 GeoGebra 演示的题目类型:
1. **函数与图像**:一次函数、二次函数、反比例函数、指数函数、对数函数、三角函数等
Expand Down Expand Up @@ -592,22 +594,41 @@ export const DEFAULT_GEOGEBRA_PROMPT = `【角色与核心任务 (ROLE AND CORE
5. 确保坐标范围能让所有关键图形和交点清晰可见
6. description 用简体中文
7. 如果题目涉及参数讨论(如讨论 a 的取值范围),用滑动条 (Slider) 展示参数变化效果
8. 对于函数题,应画出函数图像并标注关键点(交点、顶点、渐近线等)`;
8. 对于函数题,应画出函数图像并标注关键点(交点、顶点、渐近线等)
9. 【!!! 命令语法检查 (COMMAND SYNTAX CHECK) !!!】在生成命令后,必须逐条检查以下内容:
a. 每个变量在使用前必须已被定义(例如如果使用 F = Intersect(f, g),则 f 和 g 必须在之前已定义)
b. 所有命令名称必须是 GeoGebra 官方支持的指令(如平行线用 Line(P, l) 而非 ParallelLine(P, l))
c. 命令顺序必须合理:先定义基础对象(点、线、函数),再定义派生对象(交点、轨迹、变换)
d. 每个命令的语法必须正确(参数数量、类型、顺序与 GeoGebra 官方文档一致)
e. 如果使用了循环或条件语句,确保语法正确
f. 坐标轴范围 (setCoordSystem) 必须在所有绘图命令之前设置`;

/**
* 生成 GeoGebra 分析提示词
*/
export function generateGeogebraPrompt(
questionText: string,
answerText: string,
analysis: string
analysis: string,
previousErrors?: string
): string {
return DEFAULT_GEOGEBRA_PROMPT.replace(
let prompt = DEFAULT_GEOGEBRA_PROMPT.replace(
"{{question_text}}",
questionText
)
.replace("{{answer_text}}", answerText)
.replace("{{analysis}}", analysis);

if (previousErrors?.trim()) {
prompt = prompt.replace(
"{{error_feedback}}",
`【上次执行错误 (PREVIOUS ERRORS)】\n上次生成的 GeoGebra 命令执行时出现了以下错误,请分析错误原因并在这次生成中修正:\n${previousErrors}\n\n【错误修正要求 (ERROR FIX RULES)】\n请严格按以下步骤修正:\n1. 检查每条失败命令,确认是语法错误、变量未定义还是命令名称错误\n2. 如果是"未定义变量X",在命令之前添加定义该变量的命令,或调整命令顺序\n3. 如果是"未知的指令",替换为 GeoGebra 官方支持的等效命令(如 Line(P, l) 替代 ParallelLine(P, l))\n4. 重新排列所有命令的顺序,确保每个对象在使用前已定义\n5. 生成后再次逐条检查:每条命令的语法、变量引用、依赖关系是否正确`
);
} else {
prompt = prompt.replace("{{error_feedback}}", "");
}

return prompt;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ai/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface AIService {
analyzeImage(imageBase64: string, mimeType?: string, language?: 'zh' | 'en', grade?: 7 | 8 | 9 | 10 | 11 | 12 | null, subject?: string | null, gradeSemester?: string | null): Promise<ParsedQuestionFromSchema>;
generateSimilarQuestion(originalQuestion: string, knowledgePoints: string[], language?: 'zh' | 'en', difficulty?: DifficultyLevel, gradeSemester?: string | null): Promise<ParsedQuestionFromSchema>;
reanswerQuestion(questionText: string, language?: 'zh' | 'en', subject?: string | null, imageBase64?: string, gradeSemester?: string | null): Promise<ReanswerQuestionResult>;
analyzeForGeogebra(questionText: string, answerText: string, analysis: string): Promise<GeogebraAnalysisResult>;
analyzeForGeogebra(questionText: string, answerText: string, analysis: string, previousErrors?: string): Promise<GeogebraAnalysisResult>;
}

export interface AIConfig {
Expand Down
Loading