Restrict AI Studio to teachers/admins only + Save to Lesson feature
Access Control: - AI Studio view checks user role: only SCHOOL_ADMIN, SUPER_ADMIN, TEACHER, VICE_PRINCIPAL - Students and parents see 'Kein Zugriff' (No Access) message - AI Studio removed from student navigation - API routes (/api/ai/snapgen, /api/ai/perchance, /api/ai/lesson-plan, /api/ai/agent) enforce role checks New Features: - 'Save to Lesson' dialog in Video and Image generation tabs - Pick Subject > Topic > Lesson from existing curriculum - Saves AI video/image URL to SubjectLesson record - Stores prompt, provider, timestamp, and teacher who generated - Students will see this content in homework and lesson materials - AI Lesson Plan API route (/api/ai/lesson-plan) - AI Agent Chat API route (/api/ai/agent) with context-aware system prompts - SubjectLesson model: Added aiVideoUrl, aiImageUrl, aiThumbnailUrl, aiPrompt, aiProvider, aiGeneratedAt, aiGeneratedBy - Homework model: Added aiLessonId, aiVideoUrl, aiImageUrl for linking AI content Navigation: - Removed duplicate AI Studio entry from teacher nav - Removed AI Studio from student nav (students can't access)
This commit is contained in:
1 parent
763ebef22b
commit
fe3eb4a272
6 files changed
+422
-10
No files matched your search
@@ -0,0 +1,85 @@
|
||||
// @ts-nocheck
|
||||
import { NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/db';
|
||||
import { getSession } from '@/lib/auth';
|
||||
import ZAI from 'z-ai-web-dev-sdk';
|
||||
|
||||
/**
|
||||
* AI Agent Chat API (Agentic Mode)
|
||||
* POST /api/ai/agent
|
||||
* Body: { message, context, language, conversationHistory }
|
||||
*
|
||||
* Only teachers and admins can use the agentic AI.
|
||||
*/
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const session = await getSession();
|
||||
if (!session?.user) {
|
||||
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
|
||||
}
|
||||
|
||||
// Only admin/teacher can use AI agent
|
||||
const role = session.user.role;
|
||||
if (role !== 'SCHOOL_ADMIN' && role !== 'SUPER_ADMIN' && role !== 'TEACHER' && role !== 'VICE_PRINCIPAL') {
|
||||
return NextResponse.json(
|
||||
{ error: 'Only teachers and admins can use the AI agent' },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
const { schoolId } = session.user;
|
||||
if (!schoolId) {
|
||||
return NextResponse.json({ error: 'No school assigned' }, { status: 403 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { message, context = 'teacher-assistant', language = 'de', conversationHistory = [] } = body;
|
||||
|
||||
if (!message || typeof message !== 'string') {
|
||||
return NextResponse.json({ error: 'Message is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Build system prompt based on context
|
||||
const systemPrompts: Record<string, string> = {
|
||||
'teacher-assistant': `Du bist ein KI-Assistent für Lehrkräfte an einer deutschen Schule.
|
||||
Du hilfst bei Unterrichtsvorbereitung, Bewertungsvorschlägen, Kompetenzanalyse und Differenzierung.
|
||||
Du kennst das deutsche Schulsystem (Grundschule, Gymnasium) und die kompetenzorientierte Beurteilung.
|
||||
Antworte auf Deutsch, es sei denn es wird explizit anders verlangt.
|
||||
Sei konkret, praktisch und praxisnah.`,
|
||||
'lesson-planner': `Du bist ein Experte für Unterrichtsplanung nach dem kompetenzorientierten Ansatz.
|
||||
Du erstellst strukturierte Unterrichtsentwürfe mit Phasen, Lernzielen und Differenzierung.`,
|
||||
'assessment-helper': `Du bist ein Experte für Leistungsbewertung.
|
||||
Du hilfst bei der Erstellung von Bewertungsrastern, Kompetenzchecklisten und Notenbegründungen.`,
|
||||
};
|
||||
|
||||
const systemPrompt = systemPrompts[context] || systemPrompts['teacher-assistant'];
|
||||
|
||||
// Build messages array
|
||||
const messages = [
|
||||
{ role: 'system', content: systemPrompt },
|
||||
...conversationHistory.map((msg: any) => ({
|
||||
role: msg.role,
|
||||
content: msg.content,
|
||||
})),
|
||||
{ role: 'user', content: message },
|
||||
];
|
||||
|
||||
// Generate using z-ai-web-dev-sdk
|
||||
const zai = await ZAI.create();
|
||||
const response = await zai.chat.completions.create({
|
||||
messages,
|
||||
thinking: { type: 'disabled' },
|
||||
});
|
||||
|
||||
const responseContent = response.choices?.[0]?.message?.content || 'Ich konnte keine Antwort generieren.';
|
||||
|
||||
return NextResponse.json({ response: responseContent });
|
||||
} catch (error) {
|
||||
console.error('AI Agent error:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user