GUI und neue Erklärungen verwenden gemeinsam native_language. Deutsch und Russisch sind die beiden Muttersprachen; Englisch bleibt die Lernsprache. Kompatibilitätsfelder werden atomar gekoppelt, widersprüchliche Eingaben abgewiesen und bestehende Profile sowie eingefrorene Inhalte erhalten. Ergänzt vollständige russische GUI-Kataloge und 840 geprüfte Aufgabenansichten in sieben Modulen einschließlich A1-Test, mit getrennten Sprachrollen für Ausgangs- und Lernmaterial. Äquivalente Ansichten teilen ihre Nachweisidentität. Stabile API-Fehlercodes, neue deterministische Hilfen und Trainingsbegründungen berücksichtigen die Muttersprache. Dokumentiert das Sprachmodell in AGENTS.md. Validierung: Go-Race-Suite mit eigener lokaler PostgreSQL-Testdatenbank, go vet, Web-Build, Offline-Bank-/Variantenaudit, Diff-Prüfung sowie lokale Browser-Smokes für Sprachen, Kurs, Administration, Aussprache und Aufnahme. Keine Live-Anbieter, Produktionsprofile, Deployment-Schritte oder historischen Neubewertungen.
779 lines
27 KiB
Go
779 lines
27 KiB
Go
package services
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"embed"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"speech-learning-api/internal/config"
|
|
"speech-learning-api/internal/domain"
|
|
"speech-learning-api/internal/store"
|
|
)
|
|
|
|
//go:embed aiprompts/*
|
|
var aiFiles embed.FS
|
|
|
|
type aiContextKey struct{}
|
|
type AIService struct {
|
|
Repo *store.AIStore
|
|
Client *http.Client
|
|
Keys map[string]string
|
|
URLs map[string]string
|
|
Prompts map[string]string
|
|
Schemas map[string]string
|
|
PromptVersion string
|
|
}
|
|
|
|
func NewAIService(repo *store.AIStore, cfg config.Config) *AIService {
|
|
a := &AIService{Repo: repo, Client: &http.Client{}, Keys: map[string]string{"deepseek": cfg.DeepSeekAPIKey, "mistral": cfg.MistralAPIKey}, URLs: map[string]string{"deepseek": "https://api.deepseek.com", "mistral": "https://api.mistral.ai/v1"}, Prompts: map[string]string{}, Schemas: map[string]string{}}
|
|
schemaNames := map[string]string{"next_exercise": "pronunciation", "analyze_course_progress": "analysis", "plan_course_task": "brief", "evaluate_course_attempt": "evaluation", "summarize_course_exam": "exam-summary", "author_course_task": "author", "review_course_task": "review", "author_course_exam": "author", "review_course_exam": "review"}
|
|
for _, action := range domain.AIActions {
|
|
b, _ := aiFiles.ReadFile("aiprompts/" + action + ".txt")
|
|
a.Prompts[action] = string(b)
|
|
if name := schemaNames[action]; name != "" {
|
|
b, _ = aiFiles.ReadFile("aiprompts/" + name + ".json")
|
|
a.Schemas[action] = string(b)
|
|
}
|
|
}
|
|
doc, _ := json.Marshal([]any{a.Prompts, a.Schemas})
|
|
h := sha256.Sum256(doc)
|
|
a.PromptVersion = hex.EncodeToString(h[:])
|
|
return a
|
|
}
|
|
func ValidAIProfile(p domain.AIProfile) error {
|
|
if !courseMember(p.Action, domain.AIActions) || !courseMember(p.Provider, []string{"deepseek", "mistral"}) || p.Version < 1 || len(p.Model) > 200 || p.MaxTokens < 128 || p.ContextTokens < p.MaxTokens || p.ContextTokens > 2097152 || p.TimeoutSeconds < 5 || p.TimeoutSeconds > 90 {
|
|
return errors.New("Ungültiges Modellprofil oder Token-/Zeitlimit")
|
|
}
|
|
if p.Temperature != nil && (math.IsNaN(*p.Temperature) || math.IsInf(*p.Temperature, 0) || *p.Temperature < 0 || *p.Temperature > 2) {
|
|
return errors.New("Ungültige Temperatur")
|
|
}
|
|
if p.ReasoningEffort != "" && !courseMember(p.ReasoningEffort, []string{"none", "minimal", "low", "medium", "high", "xhigh", "max"}) {
|
|
return errors.New("Ungültiger Denkaufwand")
|
|
}
|
|
for _, price := range []int64{p.InputPrice, p.CachedInputPrice, p.OutputPrice} {
|
|
if price < 0 || price > 1000000000 {
|
|
return errors.New("Ungültiger Preis")
|
|
}
|
|
}
|
|
if p.CachedInputPrice > p.InputPrice {
|
|
return errors.New("Cachepreis darf den normalen Eingabepreis nicht überschreiten")
|
|
}
|
|
if p.Enabled && !courseText(p.Model, 200) {
|
|
return errors.New("Modell-ID fehlt. Modell auswählen oder eintragen, bevor das Profil aktiviert wird")
|
|
}
|
|
if p.Enabled && !p.PricingSet {
|
|
return errors.New("Preise nicht bestätigt. Anbieterpreise eintragen und 'Preise mit aktuellen Anbieterpreisen abgeglichen' aktivieren")
|
|
}
|
|
if mistralWithoutReasoning(p) && p.ReasoningEffort != "" && p.ReasoningEffort != "none" {
|
|
return errors.New("Dieses Ministral-Modell unterstützt keinen Denkaufwand. Bitte 'Nicht senden' auswählen")
|
|
}
|
|
if p.Provider == "deepseek" && p.JSONSchema {
|
|
return errors.New("DeepSeek verwendet JSON-Modus mit anschließender Vertragsprüfung; kein Mistral-Schemamodus")
|
|
}
|
|
return nil
|
|
}
|
|
func (a *AIService) Begin(ctx context.Context, user, id string, test bool) (context.Context, domain.AIOperation, error) {
|
|
profiles, err := a.Repo.Profiles(ctx)
|
|
if err != nil {
|
|
return ctx, domain.AIOperation{}, err
|
|
}
|
|
o, err := a.Repo.Operation(ctx, domain.AIOperation{ID: id, UserID: user, Test: test, Profiles: profiles, Prompts: a.Prompts, Schemas: a.Schemas, PromptVersion: a.PromptVersion})
|
|
if err != nil {
|
|
return ctx, o, err
|
|
}
|
|
return context.WithValue(ctx, aiContextKey{}, o), o, nil
|
|
}
|
|
func (a *AIService) BeginOperation(ctx context.Context, user, id string) (context.Context, error) {
|
|
ctx, _, err := a.Begin(ctx, user, id, false)
|
|
return ctx, err
|
|
}
|
|
func (a *AIService) Configured(provider string) bool { return a.Keys[provider] != "" }
|
|
|
|
// Price units: micro-USD per million tokens. All ledger arithmetic is integer.
|
|
func aiTokenCost(tokens, price int64) int64 { return (tokens*price + 999999) / 1000000 }
|
|
func (a *AIService) Call(ctx context.Context, action string, input, out any) error {
|
|
o, ok := ctx.Value(aiContextKey{}).(domain.AIOperation)
|
|
if !ok {
|
|
return errors.New("KI-Aufruf ohne gespeicherten Auftragskontext")
|
|
}
|
|
p, ok := o.Profiles[action]
|
|
if !ok || (!p.Enabled && !o.Test) || p.Model == "" || !p.PricingSet || !a.Configured(p.Provider) {
|
|
return store.ErrAIDisabled
|
|
}
|
|
if err := ValidAIProfile(p); err != nil {
|
|
return err
|
|
}
|
|
prompt := o.Prompts[action]
|
|
if prompt == "" {
|
|
return errors.New("KI-Prompt fehlt")
|
|
}
|
|
if schema := o.Schemas[action]; schema != "" && !p.JSONSchema {
|
|
prompt += "\n\nVerbindlicher Ausgabevertrag (nur die Antwort erzeugen, nicht das Schema):\n" + schema
|
|
}
|
|
payload, err := json.Marshal(map[string]any{"schema_version": 2, "task": action, "input": input})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(payload) > 1<<20 {
|
|
return errors.New("KI-Kontext ist zu groß")
|
|
}
|
|
body := map[string]any{"model": p.Model, "messages": []any{map[string]string{"role": "system", "content": prompt}, map[string]string{"role": "user", "content": string(payload)}}, "max_tokens": p.MaxTokens, "stream": false, "response_format": map[string]any{"type": "json_object"}}
|
|
if p.Temperature != nil && !(p.Provider == "deepseek" && p.ReasoningEffort != "" && p.ReasoningEffort != "none") {
|
|
body["temperature"] = *p.Temperature
|
|
}
|
|
applyAIReasoning(body, p)
|
|
if p.JSONSchema && o.Schemas[action] != "" {
|
|
var schema any
|
|
if err = json.Unmarshal([]byte(o.Schemas[action]), &schema); err != nil {
|
|
return err
|
|
}
|
|
if p.Provider == "mistral" {
|
|
schema = mistralStructuralSchema(schema)
|
|
constrainTaskSchema(schema, action, input)
|
|
}
|
|
body["response_format"] = map[string]any{"type": "json_schema", "json_schema": map[string]any{"name": action, "schema": schema, "strict": true}}
|
|
}
|
|
raw, err := json.Marshal(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Conservative byte bound includes schema and protocol overhead, not only visible text.
|
|
if int64(len(raw))+1024+p.MaxTokens > p.ContextTokens {
|
|
return errors.New("KI-Eingabe überschreitet das konfigurierte Kontextbudget")
|
|
}
|
|
digest := sha256.Sum256(raw)
|
|
hash := hex.EncodeToString(digest[:])
|
|
if cached, err := a.Repo.Cached(ctx, o.ID, action, hash); err != nil {
|
|
return err
|
|
} else if cached != nil {
|
|
return decodeAdaptiveDocument([]byte(cached.Output), out)
|
|
}
|
|
// Reserve the entire declared context window at the greater token price.
|
|
// Unlike character-based estimates this also covers hidden reasoning tokens.
|
|
// Operators must set context_tokens <= the provider's actual enforced window.
|
|
price := p.InputPrice
|
|
if p.OutputPrice > price {
|
|
price = p.OutputPrice
|
|
}
|
|
reserve := aiTokenCost(p.ContextTokens, price) + 3
|
|
var lastErr error
|
|
for attempt := 0; attempt < 2; attempt++ {
|
|
c, err := a.Repo.Reserve(ctx, domain.AICall{OperationID: o.ID, UserID: o.UserID, Action: action, Provider: p.Provider, Model: p.Model, Profile: p, PromptVersion: o.PromptVersion, InputHash: hash, Reserved: reserve, Input: string(raw), Test: o.Test})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
started := time.Now()
|
|
work, cancel := context.WithTimeout(ctx, time.Duration(p.TimeoutSeconds)*time.Second)
|
|
req, err := http.NewRequestWithContext(work, http.MethodPost, a.URLs[p.Provider]+"/chat/completions", bytes.NewReader(raw))
|
|
if err != nil {
|
|
cancel()
|
|
return err
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+a.Keys[p.Provider])
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, callErr := a.Client.Do(req)
|
|
retry := false
|
|
if callErr != nil {
|
|
c.Status = "unknown"
|
|
c.Error = "Verbindung abgebrochen; möglicher Anbieter-Verbrauch unbekannt."
|
|
err = errors.New(c.Error)
|
|
} else {
|
|
data, readErr := io.ReadAll(io.LimitReader(resp.Body, 1048577))
|
|
resp.Body.Close()
|
|
if readErr != nil || len(data) > 1048576 {
|
|
c.Status = "unknown"
|
|
c.Error = "Anbieterantwort unvollständig oder zu groß"
|
|
err = errors.New(c.Error)
|
|
} else if resp.StatusCode != 200 {
|
|
c.Status = "failed"
|
|
c.Error = fmt.Sprintf("Anbieter HTTP %d", resp.StatusCode)
|
|
var failure struct {
|
|
Message string `json:"message"`
|
|
Error struct {
|
|
Message string `json:"message"`
|
|
} `json:"error"`
|
|
}
|
|
if json.Unmarshal(data, &failure) == nil {
|
|
message := failure.Error.Message
|
|
if message == "" {
|
|
message = failure.Message
|
|
}
|
|
for _, key := range a.Keys {
|
|
if key != "" {
|
|
message = strings.ReplaceAll(message, key, "[redacted]")
|
|
}
|
|
}
|
|
if len(message) > 500 {
|
|
message = message[:500]
|
|
}
|
|
if message != "" {
|
|
c.Error += " : " + message
|
|
}
|
|
}
|
|
|
|
err = errors.New(c.Error)
|
|
// Client-side validation/rate-limit rejection did not generate a completion.
|
|
// For server failures billing is uncertain, so retain the reservation.
|
|
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
|
|
zero := int64(0)
|
|
c.Cost = &zero
|
|
}
|
|
// A rate/capacity rejection needs a later request or another model,
|
|
// not an immediate identical retry against the same limit.
|
|
retry = resp.StatusCode >= 500
|
|
} else {
|
|
err = readAICompletion(data, &c, out)
|
|
if err != nil {
|
|
c.Status = "invalid"
|
|
c.Error = err.Error()
|
|
} else {
|
|
c.Status = "succeeded"
|
|
}
|
|
retry = err != nil
|
|
}
|
|
}
|
|
cancel()
|
|
c.DurationMS = time.Since(started).Milliseconds()
|
|
finish, stop := context.WithTimeout(context.Background(), 5*time.Second)
|
|
saveErr := a.Repo.Finish(finish, c)
|
|
stop()
|
|
if saveErr != nil {
|
|
return fmt.Errorf("KI-Verbrauch konnte nicht gespeichert werden: %w", saveErr)
|
|
}
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if !retry || ctx.Err() != nil {
|
|
return err
|
|
}
|
|
lastErr = err
|
|
}
|
|
return fmt.Errorf("KI-Aufruf nach zwei Versuchen fehlgeschlagen: %w", lastErr)
|
|
}
|
|
|
|
func applyAIReasoning(body map[string]any, p domain.AIProfile) {
|
|
if p.ReasoningEffort != "" && !mistralWithoutReasoning(p) {
|
|
body["reasoning_effort"] = p.ReasoningEffort
|
|
if p.Provider == "deepseek" {
|
|
if p.ReasoningEffort == "none" {
|
|
body["thinking"] = map[string]string{"type": "disabled"}
|
|
delete(body, "reasoning_effort")
|
|
} else {
|
|
body["thinking"] = map[string]string{"type": "enabled"}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ministral instruct models do not accept reasoning_effort, even "none".
|
|
// Omit this parameter for historical profiles as well as new requests.
|
|
func mistralWithoutReasoning(p domain.AIProfile) bool {
|
|
return p.Provider == "mistral" && strings.HasPrefix(strings.ToLower(p.Model), "ministral-")
|
|
}
|
|
|
|
// Mistral's constrained decoder rejects the task schema's allOf/if/then/else
|
|
// clauses. Send its structural fields to the decoder. The full contract stays
|
|
// persisted with the operation and the Go validation still checks renderer
|
|
// dependencies, required media, answer references and field limits.
|
|
// Work on a copy so persisted schemas and their prompt version remain intact.
|
|
func mistralStructuralSchema(value any) any {
|
|
switch v := value.(type) {
|
|
case map[string]any:
|
|
out := make(map[string]any, len(v))
|
|
for key, child := range v {
|
|
if key != "allOf" && key != "if" && key != "then" && key != "else" && key != "not" {
|
|
out[key] = mistralStructuralSchema(child)
|
|
}
|
|
}
|
|
return out
|
|
case []any:
|
|
out := make([]any, len(v))
|
|
for i, child := range v {
|
|
out[i] = mistralStructuralSchema(child)
|
|
}
|
|
return out
|
|
default:
|
|
return value
|
|
}
|
|
}
|
|
|
|
// Narrow only renderer metadata already fixed by the saved request. Learning
|
|
// content and solutions remain the author's responsibility, never Go templates.
|
|
func constrainTaskSchema(schema any, action string, input any) {
|
|
if !courseMember(action, []string{"author_course_task", "review_course_task", "author_course_exam", "review_course_exam"}) {
|
|
return
|
|
}
|
|
root, _ := schema.(map[string]any)
|
|
props, _ := root["properties"].(map[string]any)
|
|
tasks, _ := props["tasks"].(map[string]any)
|
|
item, _ := tasks["items"].(map[string]any)
|
|
fields, _ := item["properties"].(map[string]any)
|
|
if tasks == nil || fields == nil {
|
|
return
|
|
}
|
|
exam := strings.HasSuffix(action, "_exam")
|
|
count := 1
|
|
if exam {
|
|
count = 12
|
|
}
|
|
tasks["maxItems"] = count
|
|
if strings.HasPrefix(action, "author_") {
|
|
tasks["minItems"] = count
|
|
}
|
|
b, _ := json.Marshal(input)
|
|
var request map[string]any
|
|
if json.Unmarshal(b, &request) != nil {
|
|
return
|
|
}
|
|
if strings.HasPrefix(action, "review_") {
|
|
wrapper, _ := request["request"].(map[string]any)
|
|
request, _ = wrapper["input"].(map[string]any)
|
|
}
|
|
brief, _ := request["brief"].(map[string]any)
|
|
if brief == nil {
|
|
return
|
|
}
|
|
for _, key := range []string{"module_id", "target_language", "course_version", "skill", "kind"} {
|
|
if text, ok := brief[key].(string); ok && text != "" {
|
|
fields[key] = map[string]any{"const": text}
|
|
}
|
|
}
|
|
require := func(object map[string]any, name string) {
|
|
list, _ := object["required"].([]any)
|
|
for _, field := range list {
|
|
if field == name {
|
|
return
|
|
}
|
|
}
|
|
object["required"] = append(list, name)
|
|
}
|
|
kind, _ := brief["kind"].(string)
|
|
skill, _ := brief["skill"].(string)
|
|
key, _ := fields["answer_key"].(map[string]any)
|
|
keyFields, _ := key["properties"].(map[string]any)
|
|
if kind == "choice" {
|
|
require(item, "options")
|
|
require(key, "correct_option_id")
|
|
} else if kind != "" {
|
|
delete(fields, "options")
|
|
delete(keyFields, "correct_option_id")
|
|
}
|
|
for _, field := range []struct {
|
|
name string
|
|
needed bool
|
|
}{
|
|
{"audio_text", skill == "listening"},
|
|
{"passage", skill == "reading"},
|
|
{"reference_text", kind == "repeat_sentence"},
|
|
} {
|
|
if field.needed {
|
|
require(item, field.name)
|
|
} else {
|
|
delete(fields, field.name)
|
|
}
|
|
}
|
|
}
|
|
func readAICompletion(data []byte, c *domain.AICall, out any) error {
|
|
var r struct {
|
|
Choices []struct {
|
|
Message struct {
|
|
Content json.RawMessage `json:"content"`
|
|
} `json:"message"`
|
|
FinishReason string `json:"finish_reason"`
|
|
} `json:"choices"`
|
|
Usage *struct {
|
|
Prompt *int64 `json:"prompt_tokens"`
|
|
Completion *int64 `json:"completion_tokens"`
|
|
Cache *int64 `json:"prompt_cache_hit_tokens"`
|
|
NumCached *int64 `json:"num_cached_tokens"`
|
|
LegacyPromptDetails struct {
|
|
Cached *int64 `json:"cached_tokens"`
|
|
} `json:"prompt_token_details"`
|
|
PromptDetails struct {
|
|
Cached *int64 `json:"cached_tokens"`
|
|
} `json:"prompt_tokens_details"`
|
|
CompletionDetails struct {
|
|
Reasoning *int64 `json:"reasoning_tokens"`
|
|
} `json:"completion_tokens_details"`
|
|
} `json:"usage"`
|
|
}
|
|
if err := json.Unmarshal(data, &r); err != nil {
|
|
return errors.New("Anbieterantwort ist kein gültiges JSON")
|
|
}
|
|
if r.Usage != nil && r.Usage.Prompt != nil && r.Usage.Completion != nil && *r.Usage.Prompt >= 0 && *r.Usage.Completion >= 0 {
|
|
cached := r.Usage.Cache
|
|
if cached == nil {
|
|
cached = r.Usage.PromptDetails.Cached
|
|
}
|
|
if cached == nil {
|
|
cached = r.Usage.NumCached
|
|
}
|
|
if cached == nil {
|
|
cached = r.Usage.LegacyPromptDetails.Cached
|
|
}
|
|
c.Usage = &domain.AIUsage{InputTokens: *r.Usage.Prompt, OutputTokens: *r.Usage.Completion, CachedTokens: cached, ReasoningTokens: r.Usage.CompletionDetails.Reasoning}
|
|
cache := int64(0)
|
|
if cached != nil && *cached >= 0 && *cached <= c.Usage.InputTokens {
|
|
cache = *cached
|
|
} else {
|
|
c.Usage.CachedTokens = nil
|
|
}
|
|
if c.Usage.InputTokens > 2097152 || c.Usage.OutputTokens > 2097152 {
|
|
return errors.New("Ungültige Tokenverbrauchswerte")
|
|
}
|
|
cost := aiTokenCost(c.Usage.InputTokens-cache, c.Profile.InputPrice) + aiTokenCost(cache, c.Profile.CachedInputPrice) + aiTokenCost(c.Usage.OutputTokens, c.Profile.OutputPrice)
|
|
c.Cost = &cost
|
|
}
|
|
if len(r.Choices) != 1 {
|
|
return errors.New("Genau eine Modellantwort erwartet")
|
|
}
|
|
if err := json.Unmarshal(r.Choices[0].Message.Content, &c.Output); err != nil {
|
|
var parts []struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
}
|
|
if json.Unmarshal(r.Choices[0].Message.Content, &parts) != nil {
|
|
return errors.New("Unbekanntes Antwortformat")
|
|
}
|
|
for _, part := range parts {
|
|
if part.Type == "text" {
|
|
c.Output += part.Text
|
|
}
|
|
}
|
|
}
|
|
if r.Choices[0].FinishReason != "stop" {
|
|
return fmt.Errorf("Unvollständige Modellantwort (%s)", r.Choices[0].FinishReason)
|
|
}
|
|
if err := decodeAdaptiveDocument([]byte(c.Output), out); err != nil {
|
|
return err
|
|
}
|
|
return validateAIOutput(c.Action, out)
|
|
}
|
|
func (a *AIService) Models(ctx context.Context, provider string) (json.RawMessage, error) {
|
|
if !a.Configured(provider) {
|
|
return nil, store.ErrAIDisabled
|
|
}
|
|
work, cancel := context.WithTimeout(ctx, 15*time.Second)
|
|
defer cancel()
|
|
req, err := http.NewRequestWithContext(work, "GET", a.URLs[provider]+"/models", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+a.Keys[provider])
|
|
resp, err := a.Client.Do(req)
|
|
if err != nil {
|
|
return nil, errors.New("Modellliste nicht erreichbar")
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != 200 {
|
|
return nil, fmt.Errorf("Modellliste HTTP %d", resp.StatusCode)
|
|
}
|
|
b, err := io.ReadAll(io.LimitReader(resp.Body, 1048577))
|
|
if err != nil || len(b) > 1048576 || !json.Valid(b) {
|
|
return nil, errors.New("Ungültige Modellliste")
|
|
}
|
|
return b, nil
|
|
}
|
|
func (a *AIService) Teacher(ctx context.Context, task string, input, out any) error {
|
|
return a.Call(ctx, task, input, out)
|
|
}
|
|
func (a *AIService) Generate(ctx context.Context, input any) ([]domain.AdaptiveTask, error) {
|
|
b, err := json.Marshal(input)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var in map[string]any
|
|
if err = json.Unmarshal(b, &in); err != nil {
|
|
return nil, err
|
|
}
|
|
author, review := "author_course_task", "review_course_task"
|
|
if exam, _ := in["exam"].(bool); exam {
|
|
author, review = "author_course_exam", "review_course_exam"
|
|
}
|
|
var draft struct {
|
|
SchemaVersion int `json:"schema_version"`
|
|
Tasks []domain.AdaptiveTask `json:"tasks"`
|
|
}
|
|
if err = a.Call(ctx, author, input, &draft); err != nil {
|
|
return nil, err
|
|
}
|
|
if draft.SchemaVersion != 2 {
|
|
return nil, errors.New("Ungültige Autoren-Version")
|
|
}
|
|
var checked AIReview
|
|
// Solve choices without the author's key or explanation to avoid anchoring.
|
|
// Open answers retain their rubric for the pedagogical review.
|
|
if err = a.Call(ctx, review, map[string]any{"request": map[string]any{"task": "generate_course_task", "schema_version": 2, "input": input}, "candidate": map[string]any{"schema_version": 2, "tasks": blindReviewTasks(draft.Tasks)}}, &checked); err != nil {
|
|
return nil, err
|
|
}
|
|
expected := 1
|
|
if exam, _ := in["exam"].(bool); exam {
|
|
expected = 12
|
|
}
|
|
if len(draft.Tasks) != expected {
|
|
return nil, errors.New("Falsche Aufgabenanzahl")
|
|
}
|
|
if err = validateIndependentReview(draft.Tasks, checked); err != nil {
|
|
return nil, err
|
|
}
|
|
// A reviewer returns a decision only and cannot rewrite the author's task.
|
|
for i := range draft.Tasks {
|
|
draft.Tasks[i].Source = "ai"
|
|
}
|
|
return draft.Tasks, nil
|
|
}
|
|
|
|
type AIChoiceCheck struct {
|
|
TaskIndex int `json:"task_index"`
|
|
Options []AIOptionCheck `json:"options"`
|
|
}
|
|
type AIOptionCheck struct {
|
|
OptionID string `json:"option_id"`
|
|
IsCorrect bool `json:"is_correct"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
type AIReview struct {
|
|
SchemaVersion int `json:"schema_version"`
|
|
Approved bool `json:"approved"`
|
|
ChoiceChecks []AIChoiceCheck `json:"choice_checks"`
|
|
ReviewNotes string `json:"review_notes"`
|
|
}
|
|
|
|
func blindReviewTasks(tasks []domain.AdaptiveTask) []domain.AdaptiveTask {
|
|
blind := append([]domain.AdaptiveTask(nil), tasks...)
|
|
for i := range blind {
|
|
if blind[i].Kind == "choice" {
|
|
blind[i].AnswerKey = nil
|
|
}
|
|
}
|
|
return blind
|
|
}
|
|
|
|
func validateIndependentReview(tasks []domain.AdaptiveTask, review AIReview) error {
|
|
if review.SchemaVersion != 2 || !review.Approved {
|
|
return errors.New("Aufgabe vom fachlichen Prüfer nicht freigegeben")
|
|
}
|
|
seen := map[int]bool{}
|
|
for _, check := range review.ChoiceChecks {
|
|
if check.TaskIndex < 0 || check.TaskIndex >= len(tasks) || seen[check.TaskIndex] {
|
|
return errors.New("Ungültige Zuordnung der unabhängigen Lösungsprüfung")
|
|
}
|
|
task := tasks[check.TaskIndex]
|
|
if task.Kind != "choice" || task.AnswerKey == nil || len(check.Options) != len(task.Options) {
|
|
return errors.New("Unvollständige unabhängige Optionsprüfung")
|
|
}
|
|
options := map[string]bool{}
|
|
for _, option := range task.Options {
|
|
options[option.ID] = true
|
|
}
|
|
valid := []string{}
|
|
for _, option := range check.Options {
|
|
if !options[option.OptionID] || !courseText(option.Reason, 1000) {
|
|
return errors.New("Ungültige oder doppelte unabhängige Optionsprüfung")
|
|
}
|
|
delete(options, option.OptionID)
|
|
if option.IsCorrect {
|
|
valid = append(valid, option.OptionID)
|
|
}
|
|
}
|
|
if len(valid) != 1 || valid[0] != task.AnswerKey.CorrectOptionID {
|
|
return errors.New("Unabhängige Lösung stimmt nicht eindeutig mit der Autorenlösung überein")
|
|
}
|
|
seen[check.TaskIndex] = true
|
|
}
|
|
for i, task := range tasks {
|
|
if task.Kind == "choice" && !seen[i] {
|
|
return errors.New("Unabhängige Lösungsprüfung fehlt")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (a *AIService) NextExercise(ctx context.Context, input domain.GeneratedExerciseRequest) (domain.GeneratedExercise, error) {
|
|
ctx, err := a.BeginOperation(ctx, input.UserID, store.NewID("pronunciation"))
|
|
if err != nil {
|
|
return domain.GeneratedExercise{}, err
|
|
}
|
|
request := pronunciationGenerationInput(input)
|
|
var generated domain.GeneratedExercise
|
|
for i := 0; i < 2; i++ {
|
|
generated = domain.GeneratedExercise{}
|
|
err = a.Call(ctx, "next_exercise", request, &generated)
|
|
if err == nil {
|
|
reason := generatedExerciseValidationError(generated, input)
|
|
if reason == "" {
|
|
reason = pronunciationTrainingValidation(generated, request)
|
|
}
|
|
if reason != "" {
|
|
err = errors.New(reason)
|
|
request["technical_error"] = reason + ". Return a complete corrected JSON object. Keep explanations short and single-line; reason_for_choice must be one short sentence (maximum 300 characters). Avoid every sentence in avoid_reference_texts."
|
|
} else {
|
|
generated.ID = ""
|
|
generated.Source = "ai"
|
|
return generated, nil
|
|
}
|
|
}
|
|
if err != nil && (errors.Is(err, store.ErrAIBudget) || errors.Is(err, store.ErrAIDisabled)) {
|
|
break
|
|
}
|
|
}
|
|
return generated, err
|
|
}
|
|
|
|
// Preserve the existing non-AI feedback and demo conversation behavior.
|
|
func (a *AIService) FeedbackForAttempt(ctx context.Context, text string, r domain.PronunciationResult, weak []domain.WordScore) (domain.TeacherResponse, error) {
|
|
return localizedFeedback(ctx, text, r), nil
|
|
}
|
|
func (a *AIService) StartConversation(ctx context.Context, lang, level string) (domain.TeacherResponse, error) {
|
|
return (&FlowiseTeacher{}).StartConversation(ctx, lang, level)
|
|
}
|
|
func (a *AIService) ContinueConversation(ctx context.Context, lang, level, text string) (domain.TeacherResponse, error) {
|
|
return (&FlowiseTeacher{}).ContinueConversation(ctx, lang, level, text)
|
|
}
|
|
|
|
func AIErrorSafe(err error) string {
|
|
if errors.Is(err, store.ErrAIBudget) || errors.Is(err, store.ErrAIDisabled) || errors.Is(err, store.ErrAIAttempts) {
|
|
return err.Error()
|
|
}
|
|
return "KI-Anfrage fehlgeschlagen; Details im Adminbereich."
|
|
}
|
|
|
|
func AIOutputForAction(action string) any {
|
|
switch action {
|
|
case "analyze_course_progress":
|
|
return &domain.CourseAnalysis{}
|
|
case "plan_course_task":
|
|
return &domain.CourseBrief{}
|
|
case "evaluate_course_attempt":
|
|
return &domain.CourseEvaluation{}
|
|
case "summarize_course_exam":
|
|
return &struct {
|
|
Summary string `json:"summary"`
|
|
}{}
|
|
case "next_exercise":
|
|
return &domain.GeneratedExercise{}
|
|
case "author_course_task", "author_course_exam":
|
|
return &struct {
|
|
SchemaVersion int `json:"schema_version"`
|
|
Tasks []domain.AdaptiveTask `json:"tasks"`
|
|
}{}
|
|
default:
|
|
return &AIReview{}
|
|
}
|
|
}
|
|
|
|
func validateAIOutput(action string, out any) error {
|
|
switch v := out.(type) {
|
|
case *domain.CourseEvaluation:
|
|
return ValidateCourseEvaluation(*v)
|
|
case *domain.CourseAnalysis:
|
|
if v.SchemaVersion != 2 || v.TargetLanguage == "" || v.CourseVersion != domain.CourseVersion || !courseText(v.Summary, 6000) || v.RecommendedModuleID == "" || len(v.Modules) != 7 {
|
|
return errors.New("Unvollständige Lernanalyse")
|
|
}
|
|
case *domain.CourseBrief:
|
|
if v.SchemaVersion != 2 || v.ModuleID == "" || v.TargetLanguage == "" || v.CourseVersion != domain.CourseVersion || !courseMember(v.Skill, domain.CourseSkills) || !courseText(v.Objective, 1500) || !courseText(v.Difficulty, 200) || !courseText(v.Reason, 2000) {
|
|
return errors.New("Unvollständiger Lernauftrag")
|
|
}
|
|
case *AIReview:
|
|
if v.SchemaVersion != 2 || !courseText(v.ReviewNotes, 1000) || v.ChoiceChecks == nil || len(v.ChoiceChecks) > 12 {
|
|
return errors.New("Unvollständige unabhängige Aufgabenprüfung")
|
|
}
|
|
seen := map[int]bool{}
|
|
for _, check := range v.ChoiceChecks {
|
|
if check.TaskIndex < 0 || check.TaskIndex > 11 || seen[check.TaskIndex] || len(check.Options) < 2 || len(check.Options) > 6 {
|
|
return errors.New("Ungültige unabhängige Optionsprüfung")
|
|
}
|
|
seen[check.TaskIndex] = true
|
|
options := map[string]bool{}
|
|
for _, option := range check.Options {
|
|
if !courseText(option.OptionID, 80) || !courseText(option.Reason, 1000) || options[option.OptionID] {
|
|
return errors.New("Ungültige unabhängige Optionsprüfung")
|
|
}
|
|
options[option.OptionID] = true
|
|
}
|
|
}
|
|
default:
|
|
var fields map[string]json.RawMessage
|
|
b, err := json.Marshal(out)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = json.Unmarshal(b, &fields); err != nil {
|
|
return err
|
|
}
|
|
if action == "summarize_course_exam" {
|
|
var summary string
|
|
json.Unmarshal(fields["summary"], &summary)
|
|
if !courseText(summary, 6000) {
|
|
return errors.New("Testzusammenfassung fehlt")
|
|
}
|
|
}
|
|
if strings.HasPrefix(action, "author_") || strings.HasPrefix(action, "review_") {
|
|
var version int
|
|
json.Unmarshal(fields["schema_version"], &version)
|
|
if version != 2 {
|
|
return errors.New("Ungültige Aufgabenversion")
|
|
}
|
|
var tasks []domain.AdaptiveTask
|
|
if err = json.Unmarshal(fields["tasks"], &tasks); err != nil {
|
|
return err
|
|
}
|
|
if strings.HasPrefix(action, "review_") {
|
|
var approved bool
|
|
json.Unmarshal(fields["approved"], &approved)
|
|
if !approved {
|
|
return nil
|
|
}
|
|
}
|
|
count := 1
|
|
if strings.HasSuffix(action, "_exam") {
|
|
count = 12
|
|
}
|
|
if len(tasks) != count {
|
|
return errors.New("Falsche Aufgabenanzahl")
|
|
}
|
|
for _, task := range tasks {
|
|
if err = ValidateAdaptiveTask(task, task.TargetLanguage, task.ModuleID, ""); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type AIQualityResult struct {
|
|
TaskPrompt string `json:"task_prompt,omitempty"`
|
|
Answer string `json:"answer"`
|
|
Expected string `json:"expected"`
|
|
Passed bool `json:"passed"`
|
|
Output *domain.CourseEvaluation `json:"output,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
func (a *AIService) Quality(ctx context.Context) ([]AIQualityResult, error) {
|
|
return a.QualitySuite(ctx, "grammar")
|
|
}
|
|
|
|
func (a *AIService) AdminError(err error) string {
|
|
message := err.Error()
|
|
for _, key := range a.Keys {
|
|
if key != "" {
|
|
message = strings.ReplaceAll(message, key, "[redacted]")
|
|
}
|
|
}
|
|
if len(message) > 1000 {
|
|
message = message[:1000]
|
|
}
|
|
return message
|
|
}
|