1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
|
package gui
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"fyne.io/fyne/v2"
"codeberg.org/snonux/totalrecall/internal/audio"
"codeberg.org/snonux/totalrecall/internal/image"
"codeberg.org/snonux/totalrecall/internal/phonetic"
"codeberg.org/snonux/totalrecall/internal/translation"
)
// GenerationOrchestrator coordinates audio, image, and phonetics generation
// for a single card. It holds all injectable factory functions so tests can
// substitute fakes without touching the UI layer.
// image.ClientFactories groups the two image-factory functions so the field
// definitions are not duplicated between this type and processor.Processor.
type GenerationOrchestrator struct {
config *Config
audioConfig *audio.Config
phonetics *phonetic.Fetcher
translator *translation.Translator
// imageFactories groups the two image-provider construction functions.
// Production code uses image.DefaultClientFactories(); tests replace fields.
imageFactories image.ClientFactories
// newAudioProvider constructs an audio.Provider from a Config.
// Production code uses audio.NewProvider; tests replace it with a fake.
newAudioProvider audio.ProviderFactory
}
// NewGenerationOrchestrator constructs an orchestrator wired to the given app
// configuration and service dependencies. imageFactories and newAudio are the
// injectable test seams — pass image.DefaultClientFactories() and
// audio.NewProvider for production behaviour.
func NewGenerationOrchestrator(
config *Config,
audioCfg *audio.Config,
phonetics *phonetic.Fetcher,
translator *translation.Translator,
imageFactories image.ClientFactories,
newAudio audio.ProviderFactory,
) *GenerationOrchestrator {
return &GenerationOrchestrator{
config: config,
audioConfig: audioCfg,
phonetics: phonetics,
translator: translator,
imageFactories: imageFactories,
newAudioProvider: newAudio,
}
}
// --- Translation helpers ---
// TranslateWord translates a Bulgarian word to English.
func (o *GenerationOrchestrator) TranslateWord(word string) (string, error) {
if o.translator == nil {
return "", fmt.Errorf("translation service not configured")
}
return o.translator.TranslateWord(word)
}
// TranslateEnglishToBulgarian translates an English word to Bulgarian.
func (o *GenerationOrchestrator) TranslateEnglishToBulgarian(word string) (string, error) {
if o.translator == nil {
return "", fmt.Errorf("translation service not configured")
}
return o.translator.TranslateEnglishToBulgarian(word)
}
// --- Audio provider helpers ---
// audioProviderName returns the lowercase provider name from config, defaulting
// to the shared audio default when none is set.
func (o *GenerationOrchestrator) audioProviderName() string {
if o.audioConfig != nil {
if provider := strings.ToLower(strings.TrimSpace(o.audioConfig.Provider)); provider != "" {
return provider
}
}
return audio.DefaultProviderConfig().Provider
}
// audioVoices returns the configured provider's voice list.
func (o *GenerationOrchestrator) audioVoices() []string {
return audio.VoicesFor(o.audioProviderName())
}
// audioVoiceAndSpeed selects the voice and speed for a generation run.
// For Gemini with a pinned voice the configured voice is used; otherwise a
// random voice is selected from the available list.
func (o *GenerationOrchestrator) audioVoiceAndSpeed() (string, float64) {
switch o.audioProviderName() {
case "gemini":
if o.audioConfig != nil {
if voice := strings.TrimSpace(o.audioConfig.GeminiVoice); voice != "" {
return voice, o.geminiSpeed()
}
}
return randomVoice(o.audioVoices()), o.geminiSpeed()
default:
return randomVoice(o.audioVoices()), randomOpenAISpeed()
}
}
// geminiSpeed returns the configured Gemini TTS speed or the default.
func (o *GenerationOrchestrator) geminiSpeed() float64 {
if o.audioConfig != nil && o.audioConfig.GeminiSpeed > 0 {
return o.audioConfig.GeminiSpeed
}
return audio.DefaultProviderConfig().GeminiSpeed
}
// geminiVoicePinned returns true when a specific Gemini voice is locked in
// config, meaning fallback voice selection should be skipped.
func (o *GenerationOrchestrator) geminiVoicePinned() bool {
return o.audioConfig != nil && strings.TrimSpace(o.audioConfig.GeminiVoice) != ""
}
// audioOutputFormat resolves the effective output format (e.g. "mp3" or "wav").
func (o *GenerationOrchestrator) audioOutputFormat() string {
if o.config != nil && strings.TrimSpace(o.config.AudioFormat) != "" {
return o.config.AudioFormat
}
if o.audioConfig != nil && strings.TrimSpace(o.audioConfig.OutputFormat) != "" {
return o.audioConfig.OutputFormat
}
return audio.DefaultProviderConfig().OutputFormat
}
// audioConfigForGeneration builds an audio.Config for a single generation call,
// overriding the voice and speed with the values selected for this run.
func (o *GenerationOrchestrator) audioConfigForGeneration(voice string, speed float64) audio.Config {
audioConfig := audio.Config{}
if o.audioConfig != nil {
audioConfig = *o.audioConfig
}
audioConfig.Provider = o.audioProviderName()
if o.config != nil {
audioConfig.OutputDir = o.config.OutputDir
}
audioConfig.OutputFormat = o.audioOutputFormat()
switch audioConfig.Provider {
case "gemini":
audioConfig.GeminiVoice = voice
audioConfig.GeminiSpeed = speed
if strings.TrimSpace(audioConfig.GeminiTTSModel) == "" {
audioConfig.GeminiTTSModel = audio.DefaultProviderConfig().GeminiTTSModel
}
default:
audioConfig.OpenAIVoice = voice
audioConfig.OpenAISpeed = speed
}
return audioConfig
}
// generateAudioFile generates a single audio file for text using the given
// voice and speed. It is the lowest-level generation call.
func (o *GenerationOrchestrator) generateAudioFile(ctx context.Context, text, outputFile, voice string, speed float64) error {
audioConfig := o.audioConfigForGeneration(voice, speed)
provider, err := o.newAudioProvider(&audioConfig)
if err != nil {
return err
}
return provider.GenerateAudio(ctx, text, outputFile)
}
// --- Audio generation public methods ---
// GenerateAudio generates audio for an en-bg card's single audio file.
// Returns the path to the generated file.
func (o *GenerationOrchestrator) GenerateAudio(ctx context.Context, word, cardDir string) (string, error) {
if cardDir == "" {
return "", fmt.Errorf("card directory not provided")
}
// Check if this is a regeneration by looking for an existing audio file.
isRegeneration := false
audioFile := filepath.Join(cardDir, fmt.Sprintf("audio.%s", o.audioOutputFormat()))
if _, err := os.Stat(audioFile); err == nil {
isRegeneration = true
}
voice, speed := o.audioVoiceAndSpeed()
if isRegeneration {
fmt.Printf("Regenerating audio for '%s' with voice: %s, speed: %.2f\n", word, voice, speed)
} else {
fmt.Printf("Generating audio for '%s' with voice: %s, speed: %.2f\n", word, voice, speed)
}
finalVoice, err := o.runAudioWithFallbacks(ctx, word, audioFile, voice, speed)
if err != nil {
return "", err
}
audioCfg := o.audioConfigForGeneration(finalVoice, speed)
if err := o.saveAudioAttribution(word, audioFile, finalVoice, speed); err != nil {
fmt.Printf("Warning: Failed to save audio attribution: %v\n", err)
}
if err := o.saveAudioMetadata(cardDir, audioCfg, finalVoice, speed, "en-bg", audioFile, ""); err != nil {
fmt.Printf("Warning: Failed to save audio metadata: %v\n", err)
}
return audioFile, nil
}
// GenerateAudioFront generates the front audio file for a bg-bg card.
func (o *GenerationOrchestrator) GenerateAudioFront(ctx context.Context, word, cardDir string) (string, error) {
if cardDir == "" {
return "", fmt.Errorf("card directory not provided")
}
voice, speed := o.audioVoiceAndSpeed()
fmt.Printf("Generating front audio for '%s' with voice: %s, speed: %.2f\n", word, voice, speed)
frontFile := filepath.Join(cardDir, fmt.Sprintf("audio_front.%s", o.audioOutputFormat()))
finalVoice, err := o.runAudioWithFallbacks(ctx, word, frontFile, voice, speed)
if err != nil {
return "", fmt.Errorf("failed to generate front audio: %w", err)
}
audioCfg := o.audioConfigForGeneration(finalVoice, speed)
if err := o.saveAudioAttribution(word, frontFile, finalVoice, speed); err != nil {
fmt.Printf("Warning: Failed to save audio attribution: %v\n", err)
}
// Resolve the existing back audio path to keep the metadata complete.
_, existingBack := resolveBgBgAudioFilesInDir(cardDir)
if err := o.saveAudioMetadata(cardDir, audioCfg, finalVoice, speed, "bg-bg", frontFile, existingBack); err != nil {
fmt.Printf("Warning: Failed to save audio metadata: %v\n", err)
}
return frontFile, nil
}
// GenerateAudioBack generates the back audio file for a bg-bg card.
func (o *GenerationOrchestrator) GenerateAudioBack(ctx context.Context, text, cardDir string) (string, error) {
if cardDir == "" {
return "", fmt.Errorf("card directory not provided")
}
voice, speed := o.audioVoiceAndSpeed()
fmt.Printf("Generating back audio for '%s' with voice: %s, speed: %.2f\n", text, voice, speed)
backFile := filepath.Join(cardDir, fmt.Sprintf("audio_back.%s", o.audioOutputFormat()))
finalVoice, err := o.runAudioWithFallbacks(ctx, text, backFile, voice, speed)
if err != nil {
return "", fmt.Errorf("failed to generate back audio: %w", err)
}
audioCfg := o.audioConfigForGeneration(finalVoice, speed)
if err := o.saveAudioAttribution(text, backFile, finalVoice, speed); err != nil {
fmt.Printf("Warning: Failed to save audio attribution: %v\n", err)
}
// Resolve the existing front audio path to keep the metadata complete.
existingFront, _ := resolveBgBgAudioFilesInDir(cardDir)
if err := o.saveAudioMetadata(cardDir, audioCfg, finalVoice, speed, "bg-bg", existingFront, backFile); err != nil {
fmt.Printf("Warning: Failed to save audio metadata: %v\n", err)
}
return backFile, nil
}
// GenerateAudioBgBg generates audio for both sides of a bg-bg card in a single
// call, using the same voice for both to maintain consistency.
func (o *GenerationOrchestrator) GenerateAudioBgBg(ctx context.Context, front, back, cardDir string) (string, string, error) {
if cardDir == "" {
return "", "", fmt.Errorf("card directory not provided")
}
voice, speed := o.audioVoiceAndSpeed()
fmt.Printf("Generating front audio for '%s' with voice: %s, speed: %.2f\n", front, voice, speed)
frontFile := filepath.Join(cardDir, fmt.Sprintf("audio_front.%s", o.audioOutputFormat()))
backFile := filepath.Join(cardDir, fmt.Sprintf("audio_back.%s", o.audioOutputFormat()))
// runPair generates both files using the given candidate voice.
runPair := func(candidate string) error {
if err := o.generateAudioFile(ctx, front, frontFile, candidate, speed); err != nil {
return fmt.Errorf("failed to generate front audio: %w", err)
}
fmt.Printf("Generating back audio for '%s' with voice: %s, speed: %.2f\n", back, candidate, speed)
if err := o.generateAudioFile(ctx, back, backFile, candidate, speed); err != nil {
return fmt.Errorf("failed to generate back audio: %w", err)
}
return nil
}
finalVoice, err := o.runPairWithFallbacks(voice, runPair)
if err != nil {
return "", "", err
}
audioCfg := o.audioConfigForGeneration(finalVoice, speed)
if err := o.saveAudioAttribution(front, frontFile, finalVoice, speed); err != nil {
fmt.Printf("Warning: Failed to save audio attribution: %v\n", err)
}
if err := o.saveAudioAttribution(back, backFile, finalVoice, speed); err != nil {
fmt.Printf("Warning: Failed to save audio attribution: %v\n", err)
}
if err := o.saveAudioMetadata(cardDir, audioCfg, finalVoice, speed, "bg-bg", frontFile, backFile); err != nil {
fmt.Printf("Warning: Failed to save audio metadata: %v\n", err)
}
return frontFile, backFile, nil
}
// runAudioWithFallbacks runs a single-file audio generation with Gemini voice
// fallback support. Returns the voice that was ultimately used.
func (o *GenerationOrchestrator) runAudioWithFallbacks(ctx context.Context, text, outputFile, voice string, speed float64) (string, error) {
if o.audioProviderName() == "gemini" && !o.geminiVoicePinned() {
return audio.RunWithVoiceFallbacks(voice, func(candidate string) error {
if candidate != voice {
fmt.Printf("Retrying Gemini audio with voice: %s\n", candidate)
}
return o.generateAudioFile(ctx, text, outputFile, candidate, speed)
}, nil)
}
return voice, o.generateAudioFile(ctx, text, outputFile, voice, speed)
}
// runPairWithFallbacks runs a pair-generation function with Gemini voice
// fallback support. Returns the voice that was ultimately used.
func (o *GenerationOrchestrator) runPairWithFallbacks(voice string, runPair func(string) error) (string, error) {
if o.audioProviderName() == "gemini" && !o.geminiVoicePinned() {
return audio.RunWithVoiceFallbacks(voice, func(candidate string) error {
if candidate != voice {
fmt.Printf("Retrying Gemini audio with voice: %s\n", candidate)
}
return runPair(candidate)
}, nil)
}
return voice, runPair(voice)
}
// saveAudioAttribution saves attribution metadata for a generated audio file.
// Uses BuildAttributionFor so no switch on provider name is needed here.
func (o *GenerationOrchestrator) saveAudioAttribution(word, audioFile, voice string, speed float64) error {
processedText := audio.ProcessedTextForWord(word)
providerName := o.audioProviderName()
cfg := o.audioConfig
if cfg == nil {
cfg = audio.DefaultProviderConfig()
}
// Override voice and speed with the values used for this specific generation.
cfgCopy := *cfg
cfgCopy.Provider = providerName
cfgCopy.GeminiVoice = voice
cfgCopy.GeminiSpeed = speed
cfgCopy.OpenAIVoice = voice
cfgCopy.OpenAISpeed = speed
instruction := audio.InstructionForProvider(providerName, &cfgCopy)
params := audio.AttributionParamsFrom(&cfgCopy, word, instruction, processedText, time.Now())
attribution := audio.BuildAttributionFor(providerName, params)
attrPath := audio.AttributionPath(audioFile)
if err := os.WriteFile(attrPath, []byte(attribution), 0644); err != nil {
return fmt.Errorf("failed to write audio attribution file: %w", err)
}
return nil
}
// saveAudioMetadata writes a sidecar metadata file alongside the audio file.
func (o *GenerationOrchestrator) saveAudioMetadata(cardDir string, audioCfg audio.Config, voice string, speed float64, cardType, audioFile, audioFileBack string) error {
metadataFile := filepath.Join(cardDir, "audio_metadata.txt")
if cardType == "bg-bg" {
if audioFile == "" {
audioFile, _ = resolveBgBgAudioFilesInDir(cardDir)
}
if audioFileBack == "" {
_, audioFileBack = resolveBgBgAudioFilesInDir(cardDir)
}
}
metadata := audio.BuildSidecarMetadata(audio.SidecarMetadataParams{
Provider: audioCfg.Provider,
OutputFormat: audioCfg.OutputFormat,
CardType: cardType,
AudioFile: audioFile,
AudioFileBack: audioFileBack,
OpenAIModel: audioCfg.OpenAIModel,
OpenAIVoice: voice,
OpenAISpeed: speed,
OpenAIInstruction: audioCfg.OpenAIInstruction,
GeminiTTSModel: audioCfg.GeminiTTSModel,
GeminiVoice: voice,
GeminiSpeed: speed,
})
if err := os.WriteFile(metadataFile, []byte(metadata), 0644); err != nil {
return fmt.Errorf("failed to write audio metadata file: %w", err)
}
return nil
}
// --- Image generation ---
// GenerateImagesWithPrompt downloads a single image for a word, using an
// optional custom prompt and translation hint.
func (o *GenerationOrchestrator) GenerateImagesWithPrompt(ctx context.Context, word, customPrompt, translation, cardDir string) (string, error) {
searcher, err := o.newImageSearcher()
if err != nil {
return "", err
}
if cardDir == "" {
return "", fmt.Errorf("card directory not provided")
}
downloadOpts := &image.DownloadOptions{
OutputDir: cardDir,
OverwriteExisting: true,
CreateDir: true,
FileNamePattern: "image",
MaxSizeBytes: 5 * 1024 * 1024, // 5 MB
}
downloader := image.NewDownloader(searcher, downloadOpts)
// Set up a prompt callback so the on-disk metadata and UI update as soon
// as the prompt is known (before the image download completes).
searcher.SetPromptCallback(o.imagePromptCallback(cardDir, word))
searchOpts := image.DefaultSearchOptions(word)
if customPrompt != "" {
searchOpts.CustomPrompt = customPrompt
}
if translation != "" {
searchOpts.Translation = translation
}
_, path, err := downloader.DownloadBestMatchWithOptions(ctx, searchOpts)
if err != nil {
return "", err
}
// The prompt has already been saved and UI updated via the callback.
return path, nil
}
// imagePromptCallback returns a closure that saves the image prompt to disk
// and notifies the current-word UI update if this word is still current.
// The closure captures the orchestrator's promptUpdateFn to avoid a direct
// dependency on Application.
func (o *GenerationOrchestrator) imagePromptCallback(cardDir, word string) func(prompt string) {
return func(prompt string) {
promptFile := filepath.Join(cardDir, "image_prompt.txt")
if err := os.WriteFile(promptFile, []byte(prompt), 0644); err != nil {
fmt.Printf("Warning: Failed to save prompt for '%s': %v\n", word, err)
}
}
}
// newImageSearcher constructs the appropriate image client based on the
// configured image provider. Returns image.PromptAwareClient so callers can
// call SetPromptCallback directly without a type-assertion. The factory
// functions are sourced from imageFactories (the shared image.ClientFactories
// value) to avoid duplicating the factory signatures in this package.
func (o *GenerationOrchestrator) newImageSearcher() (image.PromptAwareClient, error) {
switch o.config.ImageProvider {
case imageProviderOpenAI:
if o.config.OpenAIKey == "" {
return nil, fmt.Errorf("OpenAI API key is required for image generation")
}
openaiConfig := &image.OpenAIConfig{
APIKey: o.config.OpenAIKey,
Model: "dall-e-2", // DALL-E 2 supports 512×512
Size: "512x512",
Quality: "standard",
Style: "natural",
}
return o.imageFactories.NewOpenAIClient(openaiConfig), nil
case imageProviderNanoBanana:
cfg := o.config
if cfg == nil {
cfg = DefaultConfig()
}
if cfg.GoogleAPIKey == "" {
return nil, fmt.Errorf("google API key is required for image generation")
}
nanoBananaConfig := &image.NanoBananaConfig{
APIKey: cfg.GoogleAPIKey,
Model: cfg.NanoBananaModel,
TextModel: cfg.NanoBananaTextModel,
}
return o.imageFactories.NewNanoBananaClient(nanoBananaConfig), nil
default:
return nil, fmt.Errorf("unknown image provider: %s", o.config.ImageProvider)
}
}
// --- Phonetics ---
// GetPhoneticInfo fetches phonetic information for a Bulgarian word.
func (o *GenerationOrchestrator) GetPhoneticInfo(word string) (string, error) {
if o.phonetics == nil {
return "", fmt.Errorf("phonetic fetcher not initialized")
}
phoneticInfo, err := o.phonetics.Fetch(word)
if err != nil {
return "", fmt.Errorf("failed to get phonetic info: %w", err)
}
return phoneticInfo, nil
}
// --- Parallel generation (orchestration) ---
// GenerateResult holds the outcome of a parallel generation run.
type GenerateResult struct {
AudioFile string
AudioFileBack string
ImageFile string
PhoneticInfo string
}
// audioGenResult is an internal channel payload for audio goroutines.
type audioGenResult struct {
file string
fileBack string
err error
}
// imageGenResult is an internal channel payload for image goroutines.
type imageGenResult struct {
file string
err error
}
// phoneticGenResult is an internal channel payload for phonetic goroutines.
type phoneticGenResult struct {
info string
err error
}
// GenerateMaterials generates audio, image, and phonetics in parallel for a
// word. translation is the existing translation (may be empty). isBgBg flags
// bg-bg card type. imagePrompt is an optional custom prompt; imageTranslation
// is the translation hint for image prompts.
// The promptUI callback is called on the generating goroutine when the image
// prompt becomes known so callers can update the UI.
// Returns a GenerateResult or an error if any mandatory step fails.
func (o *GenerationOrchestrator) GenerateMaterials(
ctx context.Context,
word, translation, cardDir string,
isBgBg bool,
imagePrompt string,
promptUI func(prompt string),
) (GenerateResult, error) {
audioChan := make(chan audioGenResult, 1)
imageChan := make(chan imageGenResult, 1)
phoneticChan := make(chan phoneticGenResult, 1)
// 1. Audio generation
go func() {
var audioFile, audioFileBack string
var err error
if isBgBg && translation != "" {
audioFile, audioFileBack, err = o.GenerateAudioBgBg(ctx, word, translation, cardDir)
} else {
audioFile, err = o.GenerateAudio(ctx, word, cardDir)
}
audioChan <- audioGenResult{file: audioFile, fileBack: audioFileBack, err: err}
}()
// 2. Image generation (includes scene description from the AI)
go func() {
imageFile, err := o.generateImagesWithPromptAndNotify(ctx, word, imagePrompt, translation, cardDir, promptUI)
imageChan <- imageGenResult{file: imageFile, err: err}
}()
// 3. Phonetic information fetching
go func() {
phoneticInfo, err := o.GetPhoneticInfo(word)
if err != nil {
fmt.Printf("Warning: Failed to get phonetic info: %v\n", err)
phoneticInfo = "Failed to fetch phonetic information"
} else {
fmt.Printf("Successfully fetched phonetic info for '%s': %s\n", word, phoneticInfo)
}
savePhoneticIfValid(phoneticInfo, cardDir, word)
phoneticChan <- phoneticGenResult{info: phoneticInfo}
}()
// Collect results.
audioRes := <-audioChan
if audioRes.err != nil {
// Drain remaining channels to avoid goroutine leaks.
<-imageChan
<-phoneticChan
return GenerateResult{}, fmt.Errorf("audio generation failed: %w", audioRes.err)
}
imageRes := <-imageChan
if imageRes.err != nil {
<-phoneticChan
return GenerateResult{}, fmt.Errorf("image download failed: %w", imageRes.err)
}
phoneticRes := <-phoneticChan
return GenerateResult{
AudioFile: audioRes.file,
AudioFileBack: audioRes.fileBack,
ImageFile: imageRes.file,
PhoneticInfo: phoneticRes.info,
}, nil
}
// generateImagesWithPromptAndNotify is a thin wrapper around
// GenerateImagesWithPrompt that additionally calls promptUI after saving the
// prompt file, so the UI can display the prompt immediately.
func (o *GenerationOrchestrator) generateImagesWithPromptAndNotify(
ctx context.Context,
word, customPrompt, translation, cardDir string,
promptUI func(prompt string),
) (string, error) {
searcher, err := o.newImageSearcher()
if err != nil {
return "", err
}
if cardDir == "" {
return "", fmt.Errorf("card directory not provided")
}
downloadOpts := &image.DownloadOptions{
OutputDir: cardDir,
OverwriteExisting: true,
CreateDir: true,
FileNamePattern: "image",
MaxSizeBytes: 5 * 1024 * 1024,
}
downloader := image.NewDownloader(searcher, downloadOpts)
// Wrap the prompt callback so the UI is notified in addition to the file save.
searcher.SetPromptCallback(func(prompt string) {
promptFile := filepath.Join(cardDir, "image_prompt.txt")
if err := os.WriteFile(promptFile, []byte(prompt), 0644); err != nil {
fmt.Printf("Warning: Failed to save prompt for '%s': %v\n", word, err)
}
if promptUI != nil {
fyne.Do(func() {
promptUI(prompt)
})
}
})
searchOpts := image.DefaultSearchOptions(word)
if customPrompt != "" {
searchOpts.CustomPrompt = customPrompt
}
if translation != "" {
searchOpts.Translation = translation
}
_, path, err := downloader.DownloadBestMatchWithOptions(ctx, searchOpts)
return path, err
}
// savePhoneticIfValid saves phonetic info to disk when the info is valid.
func savePhoneticIfValid(phoneticInfo, cardDir, word string) {
if phoneticInfo == "" || phoneticInfo == "Failed to fetch phonetic information" {
return
}
phoneticFile := filepath.Join(cardDir, "phonetic.txt")
if err := os.WriteFile(phoneticFile, []byte(phoneticInfo), 0644); err != nil {
fmt.Printf("Warning: Failed to save phonetic info for '%s': %v\n", word, err)
}
}
|