Luca448 commited on
Commit
73cfae1
·
1 Parent(s): 5d78b92

Feat: Run AI correction and enrichment directly inside the vocabulary modal flow before saving to the database

Browse files
Files changed (2) hide show
  1. index.html +1 -1
  2. src/views/vocabulary-modal.js +41 -11
index.html CHANGED
@@ -74,7 +74,7 @@
74
  </head>
75
  <body class="dark-theme in-chat-view" style="visibility: hidden;">
76
 
77
- <div id="live-version-badge" style="position: fixed; top: 50px; left: 50%; transform: translateX(-50%); font-size: 10px; color: var(--text-secondary); opacity: 0.6; z-index: 99999; pointer-events: none; font-weight: 500; font-family: monospace; letter-spacing: 1px; background: rgba(0,0,0,0.2); padding: 2px 8px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.05);">BUILD 1.8.319</div>
78
 
79
  <!-- SVG Filters for Liquid Glass Refraction -->
80
  <svg width="0" height="0" style="position: absolute; pointer-events: none;">
 
74
  </head>
75
  <body class="dark-theme in-chat-view" style="visibility: hidden;">
76
 
77
+ <div id="live-version-badge" style="position: fixed; top: 50px; left: 50%; transform: translateX(-50%); font-size: 10px; color: var(--text-secondary); opacity: 0.6; z-index: 99999; pointer-events: none; font-weight: 500; font-family: monospace; letter-spacing: 1px; background: rgba(0,0,0,0.2); padding: 2px 8px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.05);">BUILD 1.8.320</div>
78
 
79
  <!-- SVG Filters for Liquid Glass Refraction -->
80
  <svg width="0" height="0" style="position: absolute; pointer-events: none;">
src/views/vocabulary-modal.js CHANGED
@@ -271,19 +271,49 @@ Nur das rohe JSON:`;
271
  const isDuplicate = existingCards.some(card => card.front.toLowerCase() === parsed.front.toLowerCase());
272
 
273
  if (!isDuplicate) {
274
- let finalBack = parsed.back;
275
-
276
- if (parsed.isDeepL) {
277
- // Add a temporary marker so the background job knows this needs enrichment
278
- finalBack = `${parsed.back}\n\n<!-- NEEDS_ENRICHMENT -->`;
279
- } else if (parsed.explanation && parsed.example) {
280
- finalBack = `${parsed.back}\n\n**Erklärung:** ${parsed.explanation}\n\n**Beispiel:** ${parsed.example}`;
281
- }
 
 
 
 
 
 
 
 
282
 
283
- const searchTerms = encodeURIComponent(parsed.imageQuery || parsed.front);
284
- const imageUrl = `https://tse1.mm.bing.net/th?q=${searchTerms}&w=800&h=1000&c=7&rs=1`;
 
 
 
 
 
285
 
286
- db.addFlashcard(schnelldeck.id, parsed.front, finalBack, imageUrl);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
  }
288
 
289
  // Show success with translation
 
271
  const isDuplicate = existingCards.some(card => card.front.toLowerCase() === parsed.front.toLowerCase());
272
 
273
  if (!isDuplicate) {
274
+ // Run AI correction and enrichment in the background BEFORE saving to DB
275
+ // This keeps the modal fast but ensures typos aren't saved
276
+ setTimeout(async () => {
277
+ try {
278
+ const { aiRouter } = await import('../ai/router.js');
279
+ const prompt = `Erstelle eine kurze Erklärung und ein passendes Beispiel für diese Vokabelkarte, sowie einen Suchbegriff für ein Bild.
280
+ Wort: "${parsed.front}"
281
+ Übersetzung: "${parsed.back}"
282
+
283
+ WICHTIG: Prüfe, ob das "Wort" offensichtlich falsch geschrieben ist (z.B. phonetisch nach Gehör geschrieben).
284
+ Falls es einen Tippfehler enthält, gib das richtig geschriebene Wort im Feld "correctedWord" zurück. Wenn es korrekt geschrieben ist, lass das Feld weg oder setze es auf null.
285
+
286
+ Generiere:
287
+ - "explanation": 1-2 kurze Sätze in der Sprache der Übersetzung, die genau dieses Wort erklären. WICHTIG: Markiere das Lernwort (und seine Abwandlungen) IMMER fett mit HTML-Tags (z.B. <b>Wort</b>).
288
+ - "example": Ein prägnanter Beispielsatz mit der Übersetzung. Das Lernwort MUSS auch hier mit <b>...</b> fett markiert sein.
289
+ - "imageQuery": Ein präziser englischer Suchbegriff für die Bing-Bildersuche. Das Bild muss EINDEUTIG zur Übersetzung passen. Füge IMMER 'single photo, clear focus, high quality' am Ende hinzu. Keine abstrakten Cartoons.
290
 
291
+ Antworte AUSSCHLIESSLICH mit einem validen JSON-Objekt. Keine Markdown-Formatierung.
292
+ Beispiel für Tippfehler bei "abanden":
293
+ {"correctedWord": "abandon", "explanation": "Jemanden oder etwas <b>verlassen</b> oder aufgeben.", "example": "Er musste das alte Auto <b>verlassen</b>.", "imageQuery": "abandoned old car single photo clear focus"}`;
294
+
295
+ const response = await aiRouter.sendMessage(prompt, {
296
+ model: 'gemini-3.1-flash-lite'
297
+ });
298
 
299
+ let jsonStr = response.trim();
300
+ if (jsonStr.startsWith('```json')) jsonStr = jsonStr.replace(/```json/g, '').replace(/```/g, '').trim();
301
+ else if (jsonStr.startsWith('```')) jsonStr = jsonStr.replace(/```/g, '').trim();
302
+
303
+ const aiData = JSON.parse(jsonStr);
304
+ const finalFront = aiData.correctedWord ? aiData.correctedWord : parsed.front;
305
+ const finalBack = `${parsed.back}\n\n**Erklärung:** ${aiData.explanation}\n\n**Beispiel:** ${aiData.example}`;
306
+ const searchTerms = encodeURIComponent(aiData.imageQuery || finalFront);
307
+ const imageUrl = `https://tse1.mm.bing.net/th?q=${searchTerms}&w=800&h=1000&c=7&rs=1`;
308
+
309
+ db.addFlashcard(schnelldeck.id, finalFront, finalBack, imageUrl);
310
+ window.dispatchEvent(new CustomEvent('flashcards-updated'));
311
+ } catch (e) {
312
+ console.warn("Background AI enrichment failed, saving original.", e);
313
+ db.addFlashcard(schnelldeck.id, parsed.front, parsed.back, '');
314
+ window.dispatchEvent(new CustomEvent('flashcards-updated'));
315
+ }
316
+ }, 100);
317
  }
318
 
319
  // Show success with translation