Adaptiven A1-Kurs und statistisches Aussprachetraining integrieren
Direkte DeepSeek- und Mistral-Anbindung mit versionierten Aktionsprofilen, Budgetreservierung, Tokenabrechnung und administrativen Qualitätsprüfungen ersetzen die Flowise-Abhängigkeit im regulären Betrieb. 840 geprüfte A1-Aufgaben mit geschützter Testauswertung und konkreten offenen Arbeitsaufträgen ergänzen den Kurs. Inhalts- und Sprachbewertung bleiben von Azure-Aussprachewerten getrennt; historische Aufgaben und Antworten bleiben erhalten. Echte Wort- und Phonemmessungen werden mit Herkunft, Aktualität und Nachweisdichte gebündelt. Go bestimmt Trainingsziele und Wiederholungen, das Modell formuliert dazu passende Nachsprechübungen. Profilansicht und Auswahlgründe werden angezeigt. Validierung: Go-Race-Tests mit lokaler PostgreSQL-Integration, go vet, Web-Build, Audit aller 840 Aufgaben und drei Browser-Smokes mit lokalen Modell-/Speech-Fixtures bestanden. Keine Produktionsänderungen oder Live-Aufrufe.
This commit is contained in:
@@ -36,12 +36,26 @@ func (s *PostgresStore) Init(ctx context.Context) error {
|
||||
if _, err := s.db.ExecContext(ctx, schemaSQL); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx, courseSchemaSQL); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, stmt := range seedSQL {
|
||||
if _, err := s.db.ExecContext(ctx, stmt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
for _, stmt := range courseSeedSQL {
|
||||
if _, err := s.db.ExecContext(ctx, stmt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := NewAdaptiveStore(s.db).Init(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.BackfillPronunciation(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return NewAIStore(s.db).Init(ctx)
|
||||
}
|
||||
|
||||
func (s *PostgresStore) EnsureGeneratedLesson(ctx context.Context, targetLanguage, cefrLevel string) (domain.Lesson, error) {
|
||||
@@ -92,25 +106,51 @@ func (s *PostgresStore) SaveGeneratedExercise(ctx context.Context, userID string
|
||||
PromptText: valueOr(generated.DisplayPrompt, "Sprich den Satz klar nach."),
|
||||
TargetText: generated.ReferenceText,
|
||||
Difficulty: 1,
|
||||
Source: "flowise",
|
||||
Source: valueOr(generated.Source, "flowise"),
|
||||
}
|
||||
if strings.TrimSpace(exercise.TargetText) == "" {
|
||||
return domain.Lesson{}, domain.Exercise{}, errors.New("generated exercise reference_text is required")
|
||||
}
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return domain.Lesson{}, domain.Exercise{}, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if generated.TrainingPlan != nil {
|
||||
var assigned sql.NullString
|
||||
if err := tx.QueryRowContext(ctx, `SELECT exercise_id FROM pronunciation_plans WHERE id=$1 AND user_id=$2 FOR UPDATE`, generated.TrainingPlan.ID, userID).Scan(&assigned); err != nil {
|
||||
return domain.Lesson{}, domain.Exercise{}, err
|
||||
}
|
||||
if assigned.Valid {
|
||||
tx.Rollback()
|
||||
existing, err := s.GetExercise(ctx, assigned.String, userID)
|
||||
return lesson, existing, err
|
||||
}
|
||||
}
|
||||
row := tx.QueryRowContext(ctx, `
|
||||
INSERT INTO exercises (
|
||||
id, lesson_id, target_language, cefr_level, type, prompt_text, target_text,
|
||||
difficulty, source, created_by_user_id, tts_text, focus_points_json,
|
||||
success_criteria_json, reason_for_choice, agent_context_json
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'flowise', $9, $10, $11, $12, $13, $14)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $15, $9, $10, $11, $12, $13, $14)
|
||||
RETURNING id, lesson_id, target_language, cefr_level, type, prompt_text, target_text, difficulty, source
|
||||
`, exercise.ID, exercise.LessonID, exercise.TargetLanguage, exercise.CEFRLevel, exercise.Type,
|
||||
exercise.PromptText, exercise.TargetText, exercise.Difficulty, userID, generated.TTSText,
|
||||
string(focusPoints), string(successCriteria), generated.ReasonForChoice, mustJSON(generated))
|
||||
string(focusPoints), string(successCriteria), generated.ReasonForChoice, mustJSON(generated), exercise.Source)
|
||||
if err := row.Scan(&exercise.ID, &exercise.LessonID, &exercise.TargetLanguage, &exercise.CEFRLevel, &exercise.Type, &exercise.PromptText, &exercise.TargetText, &exercise.Difficulty, &exercise.Source); err != nil {
|
||||
return domain.Lesson{}, domain.Exercise{}, err
|
||||
}
|
||||
if err := savePronunciationPlan(ctx, tx, userID, exercise.ID, generated.TrainingPlan); err != nil {
|
||||
return domain.Lesson{}, domain.Exercise{}, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return domain.Lesson{}, domain.Exercise{}, err
|
||||
}
|
||||
if generated.TrainingPlan != nil {
|
||||
exercise.TrainingReason = generated.TrainingPlan.Reason
|
||||
exercise.TrainingCategory = generated.TrainingPlan.Category
|
||||
}
|
||||
return lesson, exercise, nil
|
||||
}
|
||||
|
||||
@@ -202,16 +242,17 @@ func (s *PostgresStore) CreateLearnerProfile(ctx context.Context, userID, target
|
||||
if err := row.Scan(&profile.ID, &profile.UserID, &profile.TargetLanguage, &profile.CEFRLevel, &profile.DailyGoalMinutes, &profile.CreatedAt); err != nil {
|
||||
return domain.LearnerProfile{}, err
|
||||
}
|
||||
return profile, nil
|
||||
_, err := s.db.ExecContext(ctx, `UPDATE users SET active_profile_id=COALESCE(active_profile_id,$2) WHERE id=$1`, userID, profile.ID)
|
||||
return profile, err
|
||||
}
|
||||
|
||||
func (s *PostgresStore) GetPrimaryProfile(ctx context.Context, userID string) (domain.LearnerProfile, error) {
|
||||
var p domain.LearnerProfile
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
SELECT id, user_id, target_language, cefr_level, daily_goal_minutes, created_at
|
||||
FROM learner_profiles
|
||||
WHERE user_id = $1
|
||||
ORDER BY created_at
|
||||
SELECT p.id, p.user_id, p.target_language, p.cefr_level, p.daily_goal_minutes, p.created_at
|
||||
FROM learner_profiles p JOIN users u ON u.id=p.user_id
|
||||
WHERE p.user_id = $1
|
||||
ORDER BY (p.id=u.active_profile_id) DESC NULLS LAST, p.created_at, p.id
|
||||
LIMIT 1
|
||||
`, userID)
|
||||
if err := row.Scan(&p.ID, &p.UserID, &p.TargetLanguage, &p.CEFRLevel, &p.DailyGoalMinutes, &p.CreatedAt); err != nil {
|
||||
@@ -232,7 +273,12 @@ func (s *PostgresStore) UpdatePreferences(ctx context.Context, userID, uiLanguag
|
||||
if dailyGoalMinutes <= 0 {
|
||||
dailyGoalMinutes = 10
|
||||
}
|
||||
return s.CreateLearnerProfile(ctx, userID, valueOr(targetLanguage, "en-US"), valueOr(cefrLevel, "A1"), dailyGoalMinutes)
|
||||
profile, err := s.CreateLearnerProfile(ctx, userID, valueOr(targetLanguage, "en-US"), valueOr(cefrLevel, "A1"), dailyGoalMinutes)
|
||||
if err != nil {
|
||||
return profile, err
|
||||
}
|
||||
_, err = s.db.ExecContext(ctx, `UPDATE users SET active_profile_id=$2 WHERE id=$1`, userID, profile.ID)
|
||||
return profile, err
|
||||
}
|
||||
|
||||
func (s *PostgresStore) ListLanguages(ctx context.Context) ([]domain.LanguageProfile, error) {
|
||||
@@ -310,14 +356,41 @@ func (s *PostgresStore) NextLessonAndExercise(ctx context.Context, profile domai
|
||||
return lesson, exercise, nil
|
||||
}
|
||||
|
||||
// Include generated but unanswered exercises and attempted seed exercises.
|
||||
// Owner/language/level restrictions apply before taking the recent window.
|
||||
func (s *PostgresStore) RecentPronunciationTexts(ctx context.Context, userID, language, level string) ([]string, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT target_text FROM (
|
||||
SELECT e.target_text, e.created_at AS used_at FROM exercises e
|
||||
WHERE e.created_by_user_id=$1 AND e.target_language=$2 AND e.cefr_level=$3
|
||||
UNION ALL
|
||||
SELECT e.target_text, a.created_at AS used_at FROM attempts a JOIN exercises e ON e.id=a.exercise_id
|
||||
WHERE a.user_id=$1 AND e.target_language=$2 AND e.cefr_level=$3
|
||||
AND (e.created_by_user_id IS NULL OR e.created_by_user_id=$1)
|
||||
) history GROUP BY target_text ORDER BY max(used_at) DESC, target_text LIMIT 5`, userID, language, level)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
texts := []string{}
|
||||
for rows.Next() {
|
||||
var text string
|
||||
if err := rows.Scan(&text); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
texts = append(texts, text)
|
||||
}
|
||||
return texts, rows.Err()
|
||||
}
|
||||
|
||||
func (s *PostgresStore) GetExercise(ctx context.Context, id, userID string) (domain.Exercise, error) {
|
||||
var e domain.Exercise
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
SELECT id, lesson_id, target_language, cefr_level, type, prompt_text, target_text, difficulty, COALESCE(source, 'seed')
|
||||
SELECT id, lesson_id, target_language, cefr_level, type, prompt_text, target_text, difficulty, COALESCE(source, 'seed'), COALESCE((SELECT data->>'reason' FROM pronunciation_plans WHERE exercise_id=exercises.id),''), COALESCE((SELECT data->>'category' FROM pronunciation_plans WHERE exercise_id=exercises.id),'')
|
||||
FROM exercises
|
||||
WHERE id = $1 AND (COALESCE(source, 'seed') = 'seed' OR created_by_user_id = $2)
|
||||
`, id, userID)
|
||||
if err := row.Scan(&e.ID, &e.LessonID, &e.TargetLanguage, &e.CEFRLevel, &e.Type, &e.PromptText, &e.TargetText, &e.Difficulty, &e.Source); err != nil {
|
||||
if err := row.Scan(&e.ID, &e.LessonID, &e.TargetLanguage, &e.CEFRLevel, &e.Type, &e.PromptText, &e.TargetText, &e.Difficulty, &e.Source, &e.TrainingReason, &e.TrainingCategory); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return domain.Exercise{}, ErrNotFound
|
||||
}
|
||||
@@ -334,20 +407,45 @@ func (s *PostgresStore) SaveAttempt(ctx context.Context, attempt domain.Attempt)
|
||||
if err != nil {
|
||||
return domain.Attempt{}, err
|
||||
}
|
||||
row := s.db.QueryRowContext(ctx, `
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return domain.Attempt{}, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
var metadata any
|
||||
if attempt.Measurement != nil {
|
||||
raw, err := json.Marshal(attempt.Measurement)
|
||||
if err != nil {
|
||||
return domain.Attempt{}, err
|
||||
}
|
||||
metadata = raw
|
||||
attempt.Provider = attempt.Measurement.Provider
|
||||
}
|
||||
if attempt.Provider == "" {
|
||||
attempt.Provider = "unknown"
|
||||
}
|
||||
row := tx.QueryRowContext(ctx, `
|
||||
INSERT INTO attempts (
|
||||
id, user_id, exercise_id, target_language, recognized_text,
|
||||
overall_score, accuracy_score, fluency_score, completeness_score, prosody_score,
|
||||
word_scores_json, teacher_feedback
|
||||
word_scores_json, teacher_feedback, provider, speech_metadata
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
|
||||
RETURNING created_at
|
||||
`, attempt.ID, attempt.UserID, attempt.ExerciseID, attempt.TargetLanguage, attempt.RecognizedText,
|
||||
attempt.OverallScore, attempt.AccuracyScore, attempt.FluencyScore, attempt.CompletenessScore, attempt.ProsodyScore,
|
||||
string(wordScores), attempt.TeacherFeedback)
|
||||
string(wordScores), attempt.TeacherFeedback, attempt.Provider, metadata)
|
||||
if err := row.Scan(&attempt.CreatedAt); err != nil {
|
||||
return domain.Attempt{}, err
|
||||
}
|
||||
if attempt.Measurement != nil {
|
||||
if err := recordPronunciation(ctx, tx, attempt.UserID, "practice", attempt.ID, "practice:"+attempt.ExerciseID, attempt.TargetLanguage, "", attempt.CreatedAt, *attempt.Measurement); err != nil {
|
||||
return domain.Attempt{}, err
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return domain.Attempt{}, err
|
||||
}
|
||||
return attempt, nil
|
||||
}
|
||||
|
||||
@@ -400,6 +498,10 @@ func (s *PostgresStore) Progress(ctx context.Context, userID string, limit int)
|
||||
|
||||
// RecentAttempts are ordered newest first. A newer successful score clears an
|
||||
// older weak score for the same word within the progress window.
|
||||
func PronunciationEvidence(attempts []domain.Attempt) domain.ProgressSummary {
|
||||
return domain.ProgressSummary{RecentAttempts: attempts, WeakWords: latestWeakWords(attempts), WeakPhonemes: latestWeakPhonemes(attempts)}
|
||||
}
|
||||
|
||||
func latestWeakWords(attempts []domain.Attempt) []domain.WordScore {
|
||||
seen := make(map[string]bool)
|
||||
var weak []domain.WordScore
|
||||
|
||||
Reference in New Issue
Block a user