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.
169 lines
6.4 KiB
Go
169 lines
6.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"speech-learning-api/internal/domain"
|
|
)
|
|
|
|
// Goals are associated with existing seed IDs in queries so historic attempts
|
|
// participate without rewriting seeds or changing their solutions.
|
|
func (s *PostgresStore) SelectCoursePractice(ctx context.Context, userID, moduleID string) (domain.CoursePracticeSelection, error) {
|
|
var best domain.CoursePracticeSelection
|
|
bestPriority := 99
|
|
var bestUsed time.Time
|
|
for _, goal := range domain.IntroductionGoals() {
|
|
state := domain.CourseLearningState{RecentResults: []bool{}}
|
|
var lastUsed time.Time
|
|
err := s.db.QueryRowContext(ctx, `SELECT count(a.id),count(a.id) FILTER (WHERE a.correct),
|
|
GREATEST(COALESCE(max(a.created_at),'epoch'::timestamptz),COALESCE(max(t.created_at) FILTER (WHERE t.owner_user_id=$1),'epoch'::timestamptz))
|
|
FROM course_tasks t LEFT JOIN course_attempts a ON a.task_id=t.id AND a.user_id=$1
|
|
WHERE t.module_id=$2 AND (t.owner_user_id IS NULL OR t.owner_user_id=$1)
|
|
AND (t.learning_goal_id=$3 OR t.id=$4)`, userID, moduleID, goal.ID, goal.SeedTaskID).
|
|
Scan(&state.GoalAttemptCount, &state.GoalCorrectCount, &lastUsed)
|
|
if err != nil {
|
|
return best, err
|
|
}
|
|
rows, err := s.db.QueryContext(ctx, `SELECT a.correct FROM course_attempts a JOIN course_tasks t ON t.id=a.task_id
|
|
WHERE a.user_id=$1 AND t.module_id=$2 AND (t.owner_user_id IS NULL OR t.owner_user_id=$1)
|
|
AND (t.learning_goal_id=$3 OR t.id=$4) ORDER BY a.created_at DESC,a.id DESC LIMIT 5`, userID, moduleID, goal.ID, goal.SeedTaskID)
|
|
if err != nil {
|
|
return best, err
|
|
}
|
|
for rows.Next() {
|
|
var correct bool
|
|
if err := rows.Scan(&correct); err != nil {
|
|
rows.Close()
|
|
return best, err
|
|
}
|
|
state.RecentResults = append(state.RecentResults, correct)
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return best, err
|
|
}
|
|
priority := 2
|
|
if len(state.RecentResults) == 0 {
|
|
priority = 1
|
|
} else if !state.RecentResults[0] {
|
|
priority = 0
|
|
}
|
|
for i, j := 0, len(state.RecentResults)-1; i < j; i, j = i+1, j-1 {
|
|
state.RecentResults[i], state.RecentResults[j] = state.RecentResults[j], state.RecentResults[i]
|
|
}
|
|
if priority < bestPriority || (priority == bestPriority && lastUsed.Before(bestUsed)) {
|
|
best = domain.CoursePracticeSelection{Goal: goal, State: state, AvoidPrompts: []string{}}
|
|
bestPriority, bestUsed = priority, lastUsed
|
|
}
|
|
}
|
|
rows, err := s.db.QueryContext(ctx, `SELECT prompt FROM course_tasks WHERE module_id=$1 AND owner_user_id=$2 ORDER BY created_at DESC,id DESC LIMIT 5`, moduleID, userID)
|
|
if err != nil {
|
|
return best, err
|
|
}
|
|
for rows.Next() {
|
|
var prompt string
|
|
if err := rows.Scan(&prompt); err != nil {
|
|
rows.Close()
|
|
return best, err
|
|
}
|
|
best.AvoidPrompts = append(best.AvoidPrompts, prompt)
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
return best, err
|
|
}
|
|
|
|
func (s *PostgresStore) coursePracticeTask(ctx context.Context, userID, taskID string) (domain.CoursePracticeResult, error) {
|
|
var result domain.CoursePracticeResult
|
|
var options []byte
|
|
err := s.db.QueryRowContext(ctx, `SELECT t.id,t.module_id,t.skill,t.kind,t.prompt,t.options_json,t.source,
|
|
EXISTS(SELECT 1 FROM course_attempts a WHERE a.user_id=$1 AND a.task_id=t.id),
|
|
EXISTS(SELECT 1 FROM course_attempts a WHERE a.user_id=$1 AND a.task_id=t.id AND a.correct)
|
|
FROM course_tasks t WHERE t.id=$2 AND (t.owner_user_id IS NULL OR t.owner_user_id=$1)`, userID, taskID).
|
|
Scan(&result.Task.ID, &result.Task.ModuleID, &result.Task.Skill, &result.Task.Kind, &result.Task.Prompt, &options, &result.Source, &result.Task.Attempted, &result.Task.Correct)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return result, ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
err = json.Unmarshal(options, &result.Task.Options)
|
|
result.Task.Source = result.Source
|
|
return result, err
|
|
}
|
|
|
|
func (s *PostgresStore) CoursePracticeByRequest(ctx context.Context, userID, moduleID, requestID string) (domain.CoursePracticeResult, error) {
|
|
var id string
|
|
err := s.db.QueryRowContext(ctx, `SELECT id FROM course_tasks WHERE owner_user_id=$1 AND module_id=$2 AND generation_request_id=$3`, userID, moduleID, requestID).Scan(&id)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return domain.CoursePracticeResult{}, ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return domain.CoursePracticeResult{}, err
|
|
}
|
|
return s.coursePracticeTask(ctx, userID, id)
|
|
}
|
|
|
|
func (s *PostgresStore) CoursePractice(ctx context.Context, userID, moduleID string) ([]domain.CourseTask, error) {
|
|
rows, err := s.db.QueryContext(ctx, `SELECT id FROM course_tasks WHERE owner_user_id=$1 AND module_id=$2 ORDER BY created_at DESC,id DESC LIMIT 50`, userID, moduleID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ids := []string{}
|
|
for rows.Next() {
|
|
var id string
|
|
if err := rows.Scan(&id); err != nil {
|
|
rows.Close()
|
|
return nil, err
|
|
}
|
|
ids = append(ids, id)
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tasks := make([]domain.CourseTask, 0, len(ids))
|
|
for _, id := range ids {
|
|
result, err := s.coursePracticeTask(ctx, userID, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tasks = append(tasks, result.Task)
|
|
}
|
|
return tasks, nil
|
|
}
|
|
|
|
func (s *PostgresStore) CoursePracticeFallback(ctx context.Context, userID, seedID string) (domain.CoursePracticeResult, error) {
|
|
return s.coursePracticeTask(ctx, userID, seedID)
|
|
}
|
|
|
|
func (s *PostgresStore) SaveGeneratedCourseTask(ctx context.Context, userID, requestID string, out domain.GeneratedCourseTask) (domain.CoursePracticeResult, error) {
|
|
id := NewID("ct")
|
|
options, err := json.Marshal(out.Options)
|
|
if err != nil {
|
|
return domain.CoursePracticeResult{}, err
|
|
}
|
|
answers, err := json.Marshal([]string{out.Solution})
|
|
if err != nil {
|
|
return domain.CoursePracticeResult{}, err
|
|
}
|
|
hash := sha256.Sum256([]byte(strings.ToLower(strings.Join(strings.Fields(out.Prompt), " "))))
|
|
_, err = s.db.ExecContext(ctx, `INSERT INTO course_tasks
|
|
(id,module_id,skill,kind,prompt,options_json,accepted_answers_json,solution,explanation,sort_order,source,owner_user_id,learning_goal_id,schema_version,generation_request_id,prompt_fingerprint)
|
|
VALUES ($1,$2,$3,'choice',$4,$5,$6,$7,$8,1000,'flowise',$9,$10,$11,$12,$13)
|
|
ON CONFLICT DO NOTHING`, id, out.ModuleID, out.Skill, out.Prompt, options, answers, out.Solution, out.Explanation, userID, out.LearningGoalID, out.SchemaVersion, requestID, hex.EncodeToString(hash[:]))
|
|
if err != nil {
|
|
return domain.CoursePracticeResult{}, err
|
|
}
|
|
return s.CoursePracticeByRequest(ctx, userID, out.ModuleID, requestID)
|
|
}
|