Flowise-Übungen stabilisieren und Lernfortschritt korrigieren
Vermeide kürzlich angezeigte Sätze bei neuen Übungen und frage Flowise bei Wiederholungen einmal erneut. Validiere strukturierte Antworten, protokolliere die Übungsquelle und schütze nutzerspezifische Übungs-IDs. Berechne schwierige Wörter anhand der neuesten Aussprache pro Wort. Passe das Lehrerfeedback an gute Bewertungen an und verbessere das Laden neuer Übungen im Web. Ergänze gezielte Go-Tests.
This commit is contained in:
@@ -21,9 +21,9 @@ type DataStore interface {
|
||||
UpdatePreferences(ctx context.Context, userID, uiLanguage, targetLanguage, cefrLevel string, dailyGoalMinutes int) (domain.LearnerProfile, error)
|
||||
ListLanguages(ctx context.Context) ([]domain.LanguageProfile, error)
|
||||
GetLanguage(ctx context.Context, code string) (domain.LanguageProfile, error)
|
||||
NextLessonAndExercise(ctx context.Context, profile domain.LearnerProfile) (domain.Lesson, domain.Exercise, error)
|
||||
NextLessonAndExercise(ctx context.Context, profile domain.LearnerProfile, avoidReferenceText ...string) (domain.Lesson, domain.Exercise, error)
|
||||
SaveGeneratedExercise(ctx context.Context, userID string, generated domain.GeneratedExercise) (domain.Lesson, domain.Exercise, error)
|
||||
GetExercise(ctx context.Context, id string) (domain.Exercise, error)
|
||||
GetExercise(ctx context.Context, id, userID string) (domain.Exercise, error)
|
||||
SaveAttempt(ctx context.Context, attempt domain.Attempt) (domain.Attempt, error)
|
||||
Progress(ctx context.Context, userID string, limit int) (domain.ProgressSummary, error)
|
||||
CreateConversationSession(ctx context.Context, userID, targetLanguage, cefrLevel string) (domain.ConversationSession, error)
|
||||
@@ -190,7 +190,7 @@ func (s *Server) dashboard(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
lesson, exercise, err := s.nextAgentExercise(r.Context(), user, profile, progress)
|
||||
lesson, exercise, err := s.nextAgentExercise(r.Context(), user, profile, progress, nil)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusNotFound, err)
|
||||
return
|
||||
@@ -211,6 +211,31 @@ func (s *Server) progress(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) nextLesson(w http.ResponseWriter, r *http.Request) {
|
||||
userID := currentUserID(r)
|
||||
var req struct {
|
||||
PreviousExerciseID string `json:"previous_exercise_id"`
|
||||
RecentExerciseIDs []string `json:"recent_exercise_ids"`
|
||||
}
|
||||
if err := decodeJSON(r, &req); err != nil && !errors.Is(err, io.EOF) {
|
||||
writeError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
recentIDs := req.RecentExerciseIDs
|
||||
if req.PreviousExerciseID != "" && (len(recentIDs) == 0 || recentIDs[len(recentIDs)-1] != req.PreviousExerciseID) {
|
||||
recentIDs = append(recentIDs, req.PreviousExerciseID)
|
||||
}
|
||||
if len(recentIDs) > 3 {
|
||||
writeError(w, http.StatusBadRequest, errors.New("at most three recent exercises are allowed"))
|
||||
return
|
||||
}
|
||||
avoidReferenceTexts := make([]string, 0, len(recentIDs))
|
||||
for _, id := range recentIDs {
|
||||
previous, err := s.store.GetExercise(r.Context(), id, userID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusNotFound, err)
|
||||
return
|
||||
}
|
||||
avoidReferenceTexts = append(avoidReferenceTexts, previous.TargetText)
|
||||
}
|
||||
user, err := s.store.GetUser(r.Context(), userID)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusNotFound, err)
|
||||
@@ -226,7 +251,7 @@ func (s *Server) nextLesson(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
lesson, exercise, err := s.nextAgentExercise(r.Context(), user, profile, progress)
|
||||
lesson, exercise, err := s.nextAgentExercise(r.Context(), user, profile, progress, avoidReferenceTexts)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusNotFound, err)
|
||||
return
|
||||
@@ -234,21 +259,28 @@ func (s *Server) nextLesson(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"lesson": lesson, "exercise": exercise})
|
||||
}
|
||||
|
||||
func (s *Server) nextAgentExercise(ctx context.Context, user domain.User, profile domain.LearnerProfile, progress domain.ProgressSummary) (domain.Lesson, domain.Exercise, error) {
|
||||
func (s *Server) nextAgentExercise(ctx context.Context, user domain.User, profile domain.LearnerProfile, progress domain.ProgressSummary, avoidReferenceTexts []string) (domain.Lesson, domain.Exercise, error) {
|
||||
avoidReferenceText := ""
|
||||
if len(avoidReferenceTexts) > 0 {
|
||||
avoidReferenceText = avoidReferenceTexts[len(avoidReferenceTexts)-1]
|
||||
}
|
||||
generated, err := s.teacher.NextExercise(ctx, domain.GeneratedExerciseRequest{
|
||||
UserID: user.ID,
|
||||
Profile: profile,
|
||||
Progress: progress,
|
||||
TargetLanguage: profile.TargetLanguage,
|
||||
CEFRLevel: profile.CEFRLevel,
|
||||
UILanguage: user.UILanguage,
|
||||
SessionID: flowiseSessionID(user.ID, profile.TargetLanguage, "practice"),
|
||||
RecentWeakWords: progress.WeakWords,
|
||||
PreferredType: "repeat_sentence",
|
||||
UserID: user.ID,
|
||||
Profile: profile,
|
||||
Progress: progress,
|
||||
TargetLanguage: profile.TargetLanguage,
|
||||
CEFRLevel: profile.CEFRLevel,
|
||||
UILanguage: user.UILanguage,
|
||||
SessionID: flowiseSessionID(user.ID, profile.TargetLanguage, "practice"),
|
||||
RecentWeakWords: progress.WeakWords,
|
||||
PreferredType: "repeat_sentence",
|
||||
AvoidReferenceText: avoidReferenceText,
|
||||
AvoidReferenceTexts: avoidReferenceTexts,
|
||||
})
|
||||
if err == nil && generated.Source == "flowise" && strings.TrimSpace(generated.ReferenceText) != "" {
|
||||
lesson, exercise, saveErr := s.store.SaveGeneratedExercise(ctx, user.ID, generated)
|
||||
if saveErr == nil {
|
||||
s.logger.Info("next exercise selected", "request_id", ctx.Value(requestIDKey), "source", exercise.Source, "exercise_id", exercise.ID)
|
||||
return lesson, exercise, nil
|
||||
}
|
||||
s.logger.Warn("saving generated exercise failed, using seed fallback", "error", saveErr)
|
||||
@@ -256,7 +288,11 @@ func (s *Server) nextAgentExercise(ctx context.Context, user domain.User, profil
|
||||
if err != nil {
|
||||
s.logger.Warn("flowise next exercise failed, using seed fallback", "error", err)
|
||||
}
|
||||
return s.store.NextLessonAndExercise(ctx, profile)
|
||||
lesson, exercise, err := s.store.NextLessonAndExercise(ctx, profile, avoidReferenceTexts...)
|
||||
if err == nil {
|
||||
s.logger.Info("next exercise selected", "request_id", ctx.Value(requestIDKey), "source", exercise.Source, "exercise_id", exercise.ID)
|
||||
}
|
||||
return lesson, exercise, err
|
||||
}
|
||||
|
||||
func (s *Server) createAttempt(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -265,7 +301,7 @@ func (s *Server) createAttempt(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
exerciseID := r.FormValue("exercise_id")
|
||||
exercise, err := s.store.GetExercise(r.Context(), exerciseID)
|
||||
exercise, err := s.store.GetExercise(r.Context(), exerciseID, currentUserID(r))
|
||||
if err != nil {
|
||||
writeError(w, http.StatusNotFound, err)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"speech-learning-api/internal/domain"
|
||||
"speech-learning-api/internal/store"
|
||||
)
|
||||
|
||||
type exerciseLookupStore struct {
|
||||
DataStore
|
||||
exerciseID string
|
||||
userID string
|
||||
}
|
||||
|
||||
func (s *exerciseLookupStore) GetExercise(_ context.Context, exerciseID, userID string) (domain.Exercise, error) {
|
||||
s.exerciseID = exerciseID
|
||||
s.userID = userID
|
||||
return domain.Exercise{}, store.ErrNotFound
|
||||
}
|
||||
|
||||
func TestCreateAttemptScopesExerciseLookupToCurrentUser(t *testing.T) {
|
||||
var body bytes.Buffer
|
||||
form := multipart.NewWriter(&body)
|
||||
if err := form.WriteField("exercise_id", "ex_other_user"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := form.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/practice/attempts", &body)
|
||||
req.Header.Set("Content-Type", form.FormDataContentType())
|
||||
req = req.WithContext(context.WithValue(req.Context(), userIDKey, "usr_current"))
|
||||
recorder := httptest.NewRecorder()
|
||||
lookup := &exerciseLookupStore{}
|
||||
server := &Server{store: lookup}
|
||||
|
||||
server.createAttempt(recorder, req)
|
||||
|
||||
if recorder.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want 404", recorder.Code)
|
||||
}
|
||||
if lookup.exerciseID != "ex_other_user" || lookup.userID != "usr_current" {
|
||||
t.Fatalf("lookup used exercise %q and user %q", lookup.exerciseID, lookup.userID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNextLessonScopesPreviousExerciseToCurrentUser(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodPost, "/lessons/next", bytes.NewBufferString(`{"previous_exercise_id":"ex_other_user"}`))
|
||||
req = req.WithContext(context.WithValue(req.Context(), userIDKey, "usr_current"))
|
||||
recorder := httptest.NewRecorder()
|
||||
lookup := &exerciseLookupStore{}
|
||||
server := &Server{store: lookup}
|
||||
|
||||
server.nextLesson(recorder, req)
|
||||
|
||||
if recorder.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want 404", recorder.Code)
|
||||
}
|
||||
if lookup.exerciseID != "ex_other_user" || lookup.userID != "usr_current" {
|
||||
t.Fatalf("lookup used exercise %q and user %q", lookup.exerciseID, lookup.userID)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user