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:
@@ -0,0 +1,290 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"speech-learning-api/internal/domain"
|
||||
"speech-learning-api/internal/services"
|
||||
"speech-learning-api/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) SetAIService(a *services.AIService) { s.ai = a }
|
||||
func (s *Server) aiAvailable(w http.ResponseWriter) bool {
|
||||
if s.ai == nil {
|
||||
writeError(w, 503, errors.New("Direkte KI-Anbindung nicht eingerichtet"))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
func aiHTTPError(w http.ResponseWriter, err error) {
|
||||
status := 500
|
||||
if errors.Is(err, store.ErrAIBudget) || errors.Is(err, store.ErrAIDisabled) || errors.Is(err, store.ErrAIAttempts) {
|
||||
status = 409
|
||||
}
|
||||
if errors.Is(err, store.ErrAIConflict) {
|
||||
status = 409
|
||||
}
|
||||
if errors.Is(err, store.ErrNotFound) {
|
||||
status = 404
|
||||
}
|
||||
if status == 500 {
|
||||
writeError(w, status, errors.New("KI-Verwaltung momentan nicht verfügbar"))
|
||||
return
|
||||
}
|
||||
writeError(w, status, err)
|
||||
}
|
||||
func (s *Server) aiSettings(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.aiAvailable(w) {
|
||||
return
|
||||
}
|
||||
profiles, err := s.ai.Repo.Profiles(r.Context())
|
||||
if err != nil {
|
||||
aiHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
budget, err := s.ai.Repo.Budget(r.Context())
|
||||
if err != nil {
|
||||
aiHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"profiles": profiles, "budget": budget, "providers": map[string]bool{"deepseek": s.ai.Configured("deepseek"), "mistral": s.ai.Configured("mistral")}, "prompt_version": s.ai.PromptVersion, "prompts": s.ai.Prompts, "schemas": s.ai.Schemas})
|
||||
}
|
||||
func (s *Server) aiSaveProfile(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.aiAvailable(w) {
|
||||
return
|
||||
}
|
||||
var p domain.AIProfile
|
||||
if !adaptiveJSON(w, r, &p) {
|
||||
return
|
||||
}
|
||||
if p.Action != r.PathValue("action") {
|
||||
writeError(w, 400, errors.New("Arbeitsschritt stimmt nicht überein"))
|
||||
return
|
||||
}
|
||||
if err := services.ValidAIProfile(p); err != nil {
|
||||
writeError(w, 400, err)
|
||||
return
|
||||
}
|
||||
saved, err := s.ai.Repo.SaveProfile(r.Context(), currentUserID(r), p)
|
||||
if err != nil {
|
||||
aiHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, saved)
|
||||
}
|
||||
func (s *Server) aiSaveBudget(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.aiAvailable(w) {
|
||||
return
|
||||
}
|
||||
var b struct {
|
||||
Month int64 `json:"monthly_limit_microusd"`
|
||||
Day int64 `json:"daily_limit_microusd"`
|
||||
}
|
||||
if !adaptiveJSON(w, r, &b) {
|
||||
return
|
||||
}
|
||||
if b.Month < 0 || b.Day < 0 || b.Month > 1000000000000 || b.Day > 1000000000000 {
|
||||
writeError(w, 400, errors.New("Ungültiges Budget"))
|
||||
return
|
||||
}
|
||||
if err := s.ai.Repo.SaveBudget(r.Context(), currentUserID(r), b.Month, b.Day); err != nil {
|
||||
aiHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
value, err := s.ai.Repo.Budget(r.Context())
|
||||
if err != nil {
|
||||
aiHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, value)
|
||||
}
|
||||
func (s *Server) aiModels(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.aiAvailable(w) {
|
||||
return
|
||||
}
|
||||
p := r.PathValue("provider")
|
||||
if p != "deepseek" && p != "mistral" {
|
||||
writeError(w, 400, errors.New("Unbekannter Anbieter"))
|
||||
return
|
||||
}
|
||||
models, err := s.ai.Models(r.Context(), p)
|
||||
if err != nil {
|
||||
writeError(w, 502, errors.New("Modellliste nicht verfügbar; API-Key und Anbieterzugang prüfen"))
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, models)
|
||||
}
|
||||
func (s *Server) aiUsage(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.aiAvailable(w) {
|
||||
return
|
||||
}
|
||||
q := r.URL.Query()
|
||||
offset := 0
|
||||
if q.Get("offset") != "" {
|
||||
n, err := strconv.Atoi(q.Get("offset"))
|
||||
if err != nil || n < 0 || n > 1000000 {
|
||||
writeError(w, 400, errors.New("Ungültige Seite"))
|
||||
return
|
||||
}
|
||||
offset = n
|
||||
}
|
||||
for _, key := range []string{"from", "to"} {
|
||||
if q.Get(key) != "" {
|
||||
if _, err := time.Parse(time.RFC3339, q.Get(key)); err != nil {
|
||||
writeError(w, 400, errors.New("Zeitraum muss RFC3339 sein"))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
result, err := s.ai.Repo.Usage(r.Context(), domain.AIUsageFilter{From: q.Get("from"), To: q.Get("to"), Provider: q.Get("provider"), Model: q.Get("model"), User: q.Get("user"), Action: q.Get("action"), Offset: offset})
|
||||
if err != nil {
|
||||
aiHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, result)
|
||||
}
|
||||
func (s *Server) aiCallDetail(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.aiAvailable(w) {
|
||||
return
|
||||
}
|
||||
c, err := s.ai.Repo.Call(r.Context(), r.PathValue("id"))
|
||||
if err != nil {
|
||||
aiHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, c)
|
||||
}
|
||||
func (s *Server) aiResolve(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.aiAvailable(w) {
|
||||
return
|
||||
}
|
||||
var b struct {
|
||||
Cost int64 `json:"cost_microusd"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
if !adaptiveJSON(w, r, &b) {
|
||||
return
|
||||
}
|
||||
if b.Cost < 0 || b.Cost > 1000000000000 || len(b.Reason) < 5 || len(b.Reason) > 1000 {
|
||||
writeError(w, 400, errors.New("Kosten und Begründung erforderlich"))
|
||||
return
|
||||
}
|
||||
if err := s.ai.Repo.Resolve(r.Context(), currentUserID(r), r.PathValue("id"), b.Cost, b.Reason); err != nil {
|
||||
aiHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, map[string]bool{"ok": true})
|
||||
}
|
||||
func (s *Server) aiTest(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.aiAvailable(w) {
|
||||
return
|
||||
}
|
||||
var b struct {
|
||||
RequestID string `json:"request_id"`
|
||||
Action string `json:"action"`
|
||||
Input json.RawMessage `json:"input"`
|
||||
}
|
||||
if !aiTestJSON(w, r, &b) {
|
||||
return
|
||||
}
|
||||
if !courseRequestIDPattern.MatchString(b.RequestID) {
|
||||
writeError(w, 400, errors.New("request_id erforderlich"))
|
||||
return
|
||||
}
|
||||
if _, ok := s.ai.Prompts[b.Action]; !ok {
|
||||
writeError(w, 400, errors.New("Unbekannter Arbeitsschritt"))
|
||||
return
|
||||
}
|
||||
if len(b.Input) == 0 {
|
||||
writeError(w, 400, errors.New("Testeingabe fehlt"))
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 75*time.Second)
|
||||
defer cancel()
|
||||
ctx, operation, err := s.ai.Begin(ctx, currentUserID(r), "test_"+currentUserID(r)+"_"+b.RequestID, true)
|
||||
if err != nil {
|
||||
aiHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
var result json.RawMessage
|
||||
// Typed contracts prevent a test from showing malformed JSON as successful.
|
||||
output := services.AIOutputForAction(b.Action)
|
||||
err = s.ai.Call(ctx, b.Action, b.Input, output)
|
||||
if err != nil {
|
||||
writeJSON(w, 200, map[string]any{"ok": false, "operation_id": operation.ID, "error": s.ai.AdminError(err), "profile": operation.Profiles[b.Action], "prompt_version": operation.PromptVersion})
|
||||
return
|
||||
}
|
||||
result, err = json.Marshal(output)
|
||||
if err != nil {
|
||||
aiHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"ok": true, "operation_id": operation.ID, "output": result, "profile": operation.Profiles[b.Action], "prompt_version": operation.PromptVersion})
|
||||
}
|
||||
|
||||
func (s *Server) aiQuality(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.aiAvailable(w) {
|
||||
return
|
||||
}
|
||||
var b struct {
|
||||
RequestID string `json:"request_id"`
|
||||
Suite string `json:"suite"`
|
||||
}
|
||||
if !adaptiveJSON(w, r, &b) {
|
||||
return
|
||||
}
|
||||
if !courseRequestIDPattern.MatchString(b.RequestID) {
|
||||
writeError(w, 400, errors.New("request_id erforderlich"))
|
||||
return
|
||||
}
|
||||
if b.Suite == "" {
|
||||
b.Suite = "grammar"
|
||||
}
|
||||
if b.Suite != "grammar" && b.Suite != "writing" {
|
||||
writeError(w, 400, errors.New("Unbekannte Qualitätsprüfung"))
|
||||
return
|
||||
}
|
||||
operationID := "quality_" + currentUserID(r) + "_" + b.RequestID
|
||||
if b.Suite != "grammar" {
|
||||
operationID = "quality_" + b.Suite + "_" + currentUserID(r) + "_" + b.RequestID
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 80*time.Second)
|
||||
defer cancel()
|
||||
ctx, o, err := s.ai.Begin(ctx, currentUserID(r), operationID, true)
|
||||
if err != nil {
|
||||
aiHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
results, err := s.ai.QualitySuite(ctx, b.Suite)
|
||||
if err != nil {
|
||||
aiHTTPError(w, err)
|
||||
return
|
||||
}
|
||||
passed := true
|
||||
for _, result := range results {
|
||||
passed = passed && result.Passed
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"suite": b.Suite, "passed": passed, "results": results, "operation_id": o.ID, "profile": o.Profiles["evaluate_course_attempt"]})
|
||||
}
|
||||
|
||||
func aiTestJSON(w http.ResponseWriter, r *http.Request, out any) bool {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
|
||||
defer r.Body.Close()
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(out); err != nil {
|
||||
writeError(w, 400, errors.New("Ungültige Testeingabe; maximal 1 MiB"))
|
||||
return false
|
||||
}
|
||||
if decoder.Decode(new(any)) != io.EOF {
|
||||
writeError(w, 400, errors.New("Zusätzliche Daten hinter der Testeingabe"))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user