Enhance Exercise Management and AI Integration
Test Suite / playwright-tests (push) Waiting to run
Deploy Development / deploy (push) Successful in 43s
Test Suite / pytest-backend (push) Failing after 1s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 13s
Test Suite / k6 /health Baseline (push) Has been cancelled

- Updated the exercise form to include a tabbed navigation structure, improving user experience with sections for Stammdaten, Anleitung, Einordnung, Varianten, and Medien & Mehr.
- Introduced the concept of **Freigabelevel** (visibility level) in the UI, replacing previous terminology for clarity and consistency across components.
- Implemented new AI endpoints for exercise suggestions and regeneration, allowing for dynamic content generation without direct database writes.
- Removed the legacy `is_primary` flag from exercise skills in the UI, ensuring that intensity levels (`niedrig`, `mittel`, `hoch`) are the primary focus for skill management.
- Enhanced the variant management process with improved saving mechanisms and UI updates to reflect changes more intuitively.
This commit is contained in:
Lars committed 2026-05-22 07:52:31 +02:00
1 parent 7245bbb1da
commit e4451e1362
25 files changed
+1240 -78

No files matched your search

@@ -15,6 +15,7 @@ import {
} from '../../utils/exerciseInlineMediaRefs'
import { autoScrollForDragNearEdges } from '../../utils/dragAutoScroll'
import { normalizeSkillLevelSlug } from '../../constants/skillLevels'
import { stripHtmlToText } from '../../utils/htmlUtils'
import ExerciseCatalogAssocEditor from './ExerciseCatalogAssocEditor'
import ExerciseSkillsEditor from './ExerciseSkillsEditor'
import { useAuth } from '../../context/AuthContext'
@@ -71,6 +72,23 @@ const comboTinyNumberInputSx = {
textAlign: 'center',
}
function escapeHtmlText(s) {
return String(s)
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
}
/** Plaintext fuer RichTextEditor: ein bis mehrere Absaetze, ohne bestehendes HTML zu zerstoeren. */
function aiPlainSummaryToMinimalHtml(text) {
const raw = String(text || '').trim()
if (!raw) return ''
const parts = raw.split(/\n+/).map((p) => p.trim()).filter(Boolean)
const paras = parts.length ? parts : [raw]
return paras.map((p) => `<p>${escapeHtmlText(p)}</p>`).join('')
}
function emptyComboSlotRow() {
return {
title: '',
@@ -417,6 +435,8 @@ function detailToForm(exercise) {
intensity: normalizeExerciseSkillIntensity(s.intensity),
required_level: normalizeSkillLevelSlug(s.required_level),
target_level: normalizeSkillLevelSlug(s.target_level),
is_primary: !!s.is_primary,
ai_suggested: !!s.ai_suggested,
})) || [],
exercise_kind:
String(exercise.exercise_kind || 'simple').toLowerCase() === 'combination'
@@ -503,6 +523,7 @@ function ExerciseFormPageRoot() {
const [variantDraft, setVariantDraft] = useState(() => emptyVariantDraft())
const [variantSavingId, setVariantSavingId] = useState(null)
const [variantBusy, setVariantBusy] = useState(false)
const [aiSuggestBusy, setAiSuggestBusy] = useState(false)
const [variantEditSelection, setVariantEditSelection] = useState(null)
const [activeFormTab, setActiveFormTab] = useState('stammdaten')
const variantsSavedSnapshotRef = useRef({})
@@ -855,6 +876,83 @@ function ExerciseFormPageRoot() {
)
}
const runExerciseAiSuggestion = async (mode) => {
const gPlain = stripHtmlToText(formData.goal || '').trim()
const ePlain = stripHtmlToText(formData.execution || '').trim()
if (!gPlain && !ePlain) {
toast.error('Ziel oder Durchführung ausfüllen — die KI benötigt Kontext.')
return
}
const summaryOn = mode !== 'skills'
const skillsOn = mode !== 'summary'
const focusHint = (formData.focus_areas_multi || [])
.map((row) => {
const id = row?.focus_area_id
const fa = focusAreas.find((x) => Number(x.id) === Number(id))
return (fa?.name || '').trim()
})
.filter(Boolean)
.join(', ')
setAiSuggestBusy(true)
try {
const res = await api.suggestExerciseAi({
title: (formData.title || '').trim(),
goal: formData.goal || '',
execution: formData.execution || '',
focus_area_hint: focusHint || undefined,
include_summary: summaryOn,
include_skills: skillsOn,
})
let applied = false
if (summaryOn && res.summary?.text) {
updateFormField('summary', aiPlainSummaryToMinimalHtml(res.summary.text))
applied = true
}
if (skillsOn && Array.isArray(res.skills) && res.skills.length) {
setFormDirty(true)
setFormData((prev) => {
const next = [...(prev.skills || [])]
for (const sug of res.skills) {
const sid = Number(sug.skill_id)
if (!Number.isFinite(sid)) continue
const row = {
skill_id: sid,
intensity: normalizeExerciseSkillIntensity(sug.intensity),
required_level: normalizeSkillLevelSlug(sug.required_level) || 'grundlagen',
target_level:
normalizeSkillLevelSlug(sug.target_level) ||
normalizeSkillLevelSlug(sug.required_level) ||
'grundlagen',
is_primary: !!sug.is_primary,
ai_suggested: true,
}
const ix = next.findIndex((s) => Number(s.skill_id) === sid)
if (ix >= 0) next[ix] = { ...next[ix], ...row }
else next.push(row)
}
return { ...prev, skills: next }
})
applied = true
}
if (!applied) {
toast.info('Die KI lieferte keinen verwertbaren Vorschlag für die gewählten Bereiche.')
} else {
toast.success('KI-Vorschlag ins Formular übernommen — bitte prüfen und speichern.')
}
} catch (err) {
toast.error(err?.message || String(err))
} finally {
setAiSuggestBusy(false)
}
}
const refreshVariants = useCallback(async () => {
if (!exerciseId) return
const ex = await api.getExercise(exerciseId)
@@ -1309,7 +1407,28 @@ function ExerciseFormPageRoot() {
</div>
<div className="form-row">
<label className="form-label">Kurzbeschreibung</label>
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
gap: '8px',
flexWrap: 'wrap',
}}
>
<label className="form-label" style={{ marginBottom: 0 }}>
Kurzbeschreibung
</label>
<button
type="button"
className="btn btn-secondary"
style={{ fontSize: '12px' }}
disabled={aiSuggestBusy}
onClick={() => runExerciseAiSuggestion('summary')}
>
KI: Kurzfassung
</button>
</div>
<RichTextEditor
value={formData.summary}
onChange={(html) => updateFormField('summary', html)}
@@ -1966,6 +2085,42 @@ function ExerciseFormPageRoot() {
title="Einordnung"
hint="Fokus, Stile, Zielgruppen und Fähigkeiten für Suche, Filter und Skill-Profil."
>
<div
style={{
display: 'flex',
flexWrap: 'wrap',
gap: '8px',
marginBottom: '12px',
alignItems: 'center',
}}
>
<button
type="button"
className="btn btn-secondary"
style={{ fontSize: '12px' }}
disabled={aiSuggestBusy}
onClick={() => runExerciseAiSuggestion('skills')}
>
KI: Fähigkeiten
</button>
<button
type="button"
className="btn btn-secondary"
style={{ fontSize: '12px' }}
disabled={aiSuggestBusy}
onClick={() => runExerciseAiSuggestion('both')}
>
KI: Kurzfassung und Fähigkeiten
</button>
<span style={{ fontSize: '12px', color: 'var(--text3)' }}>
Benötigt Ziel oder Durchführung sowie optional{' '}
<button type="button" className="exercise-form-inline-tab-link" onClick={() => setActiveFormTab('anleitung')}>
Anleitung
</button>
· Vorschläge werden ins Formular übernommen und nicht automatisch gespeichert.
</span>
</div>
<section className="exercise-form-meta-panel" aria-label="Klassifikation">
<div className="exercise-form-meta-panel__grid">
<ExerciseCatalogAssocEditor
@@ -2504,11 +2659,9 @@ function ExerciseFormPageRoot() {
/>
<p style={{ fontSize: '12px', color: 'var(--text3)', marginTop: '16px' }}>
<strong>KI-Ausbaustufe:</strong> Backend laut Spec{' '}
<code style={{ fontSize: '11px' }}>POST /api/exercises/ai/suggest</code> und{' '}
<code style={{ fontSize: '11px' }}>POST /api/exercises/{'{id}'}/ai/regenerate</code> — z. B.{' '}
<code>OPENROUTER_API_KEY</code>, Vorschläge nur nach Trainer-Bestätigung übernehmen (siehe{' '}
<code>api.suggestExerciseAi</code>).
<strong>KI-Unterstützung:</strong> OpenRouter gestützte Vorschläge für Kurzfassung und Fähigkeitenzuordnung
(<code>suggestExerciseAi</code> / <code>regenerateExerciseAi</code>). Übernahme nur im Formular; Speichern
wie gewohnt.
</p>
<UnsavedChangesPrompt
blocker={blocker}