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
|
package processor
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"codeberg.org/snonux/totalrecall/internal"
"codeberg.org/snonux/totalrecall/internal/anki"
"codeberg.org/snonux/totalrecall/internal/audio"
"codeberg.org/snonux/totalrecall/internal/store"
)
type failedAssetKind string
const (
failedAssetAudio failedAssetKind = "audio"
failedAssetImage failedAssetKind = "image"
failedAssetBgBgAudioPair failedAssetKind = "front and back audio"
failedAssetBgBgFrontAudio failedAssetKind = "front audio"
failedAssetBgBgBackAudio failedAssetKind = "back audio"
)
type failedAssetPlan struct {
Card store.CardDirectory
CardType internal.CardType
Translation string
ImagePrompt string
Assets []failedAssetKind
}
// RetryFailedAssets scans the existing card output directory for incomplete or
// failed asset generations and retries them in deterministic order. The retry
// loop stops immediately on the first fresh error so users can rerun the same
// command after an upstream rate limit clears.
func (p *Processor) RetryFailedAssets() error {
if err := os.MkdirAll(p.Flags.OutputDir, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
plans, err := p.scanFailedAssetPlans()
if err != nil {
return err
}
if len(plans) == 0 {
fmt.Printf("No failed assets found in: %s\n", p.Flags.OutputDir)
return nil
}
totalAssets := 0
for _, plan := range plans {
totalAssets += len(plan.Assets)
}
fmt.Printf("Found %d failed asset(s) across %d card(s) in %s\n", totalAssets, len(plans), p.Flags.OutputDir)
regenerated := 0
for _, plan := range plans {
fmt.Printf("\nRetrying card: %s\n", plan.Card.Word)
for _, asset := range plan.Assets {
fmt.Printf(" Regenerating %s...\n", asset)
assetCtx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
err := p.regenerateFailedAsset(assetCtx, plan, asset)
cancel()
if err != nil {
return fmt.Errorf("stopped after %d successful regeneration(s); %s for %q failed: %w", regenerated, asset, plan.Card.Word, err)
}
regenerated++
}
}
fmt.Printf("\nRegenerated %d failed asset(s).\n", regenerated)
return nil
}
func (p *Processor) scanFailedAssetPlans() ([]failedAssetPlan, error) {
cards := p.cardStore.ListCardDirectories(nil)
plans := make([]failedAssetPlan, 0, len(cards))
for _, card := range cards {
plan := p.buildFailedAssetPlan(card)
if len(plan.Assets) == 0 {
continue
}
plans = append(plans, plan)
}
return plans, nil
}
func (p *Processor) buildFailedAssetPlan(card store.CardDirectory) failedAssetPlan {
plan := failedAssetPlan{
Card: card,
CardType: internal.LoadCardType(card.Path),
Translation: readStoredTranslation(card.Path),
ImagePrompt: readStoredImagePrompt(card.Path),
}
if !p.Flags.SkipAudio {
if plan.CardType.IsBgBg() {
frontReady := audioAssetReady(card.Path, "audio_front", p.EffectiveAudioFormat())
backReady := audioAssetReady(card.Path, "audio_back", p.EffectiveAudioFormat())
switch {
case !frontReady && !backReady:
plan.Assets = append(plan.Assets, failedAssetBgBgAudioPair)
case !frontReady:
plan.Assets = append(plan.Assets, failedAssetBgBgFrontAudio)
case !backReady:
plan.Assets = append(plan.Assets, failedAssetBgBgBackAudio)
}
} else if !audioAssetReady(card.Path, "audio", p.EffectiveAudioFormat()) {
plan.Assets = append(plan.Assets, failedAssetAudio)
}
}
if !p.Flags.SkipImages && !imageAssetReady(card.Path) {
plan.Assets = append(plan.Assets, failedAssetImage)
}
return plan
}
func (p *Processor) regenerateFailedAsset(ctx context.Context, plan failedAssetPlan, asset failedAssetKind) error {
switch asset {
case failedAssetAudio:
return p.generateAudio(ctx, plan.Card.Word)
case failedAssetImage:
return p.downloadImagesWithPrompt(ctx, plan.Card.Word, plan.Translation, plan.ImagePrompt)
case failedAssetBgBgAudioPair:
if strings.TrimSpace(plan.Translation) == "" {
return fmt.Errorf("missing back-side text in translation.txt")
}
return p.generateAudioBgBg(ctx, plan.Card.Word, plan.Translation)
case failedAssetBgBgFrontAudio:
return p.generateCardAudioSideInDir(ctx, plan.Card.Word, plan.Card.Path, "audio_front", "front audio")
case failedAssetBgBgBackAudio:
if strings.TrimSpace(plan.Translation) == "" {
return fmt.Errorf("missing back-side text in translation.txt")
}
return p.generateCardAudioSideInDir(ctx, plan.Translation, plan.Card.Path, "audio_back", "back audio")
default:
return fmt.Errorf("unknown failed asset kind %q", asset)
}
}
func (p *Processor) generateCardAudioSideInDir(ctx context.Context, text, wordDir, filenameBase, label string) error {
provider := p.AudioProviderName()
voice := p.audioVoiceForProvider()
p.logSelectedAudioVoice(provider, voice)
run := func(candidate string) error {
if candidate != voice {
fmt.Printf(" Retrying Gemini audio with voice: %s\n", candidate)
}
fmt.Printf(" Generating %s for '%s'...\n", label, text)
return p.generateAudioWithVoiceAndFilenameInDir(ctx, text, candidate, filenameBase, wordDir)
}
if provider == "gemini" && p.GeminiVoice() == "" {
_, err := audio.RunWithVoiceFallbacks(voice, run, func(candidate string) {
fmt.Printf(" Warning: Gemini returned no audio for voice %s\n", candidate)
})
return err
}
return run(voice)
}
func audioAssetReady(wordDir, baseName, preferredFormat string) bool {
paths := anki.ResolveAudioPaths(wordDir, baseName, preferredFormat)
if len(paths) == 0 {
return false
}
for _, path := range paths {
if !fileExistsAndNonEmpty(path) || !fileExistsAndNonEmpty(audio.AttributionPath(path)) {
return false
}
}
return fileExistsAndNonEmpty(filepath.Join(wordDir, "audio_metadata.txt"))
}
func imageAssetReady(wordDir string) bool {
if firstUsableImagePath(wordDir) == "" {
return false
}
if !fileExistsAndNonEmpty(filepath.Join(wordDir, "image_attribution.txt")) {
return false
}
return readStoredImagePrompt(wordDir) != ""
}
func firstUsableImagePath(wordDir string) string {
imagePatterns := []string{
"image_*.jpg",
"image_*.png",
"image_*.webp",
"image.jpg",
"image.png",
"image.webp",
}
for _, pattern := range imagePatterns {
if strings.Contains(pattern, "*") {
matches, _ := filepath.Glob(filepath.Join(wordDir, pattern))
for _, match := range matches {
if fileExistsAndNonEmpty(match) {
return match
}
}
continue
}
path := filepath.Join(wordDir, pattern)
if fileExistsAndNonEmpty(path) {
return path
}
}
return ""
}
func readStoredTranslation(wordDir string) string {
data, err := os.ReadFile(filepath.Join(wordDir, "translation.txt"))
if err != nil {
return ""
}
parts := strings.SplitN(string(data), "=", 2)
if len(parts) != 2 {
return strings.TrimSpace(string(data))
}
return strings.TrimSpace(parts[1])
}
func readStoredImagePrompt(wordDir string) string {
data, err := os.ReadFile(filepath.Join(wordDir, "image_prompt.txt"))
if err != nil {
return ""
}
prompt := strings.TrimSpace(string(data))
if prompt == "" || looksLikeFailedPrompt(prompt) {
return ""
}
return prompt
}
func looksLikeFailedPrompt(prompt string) bool {
lower := strings.ToLower(strings.TrimSpace(prompt))
if lower == "" {
return true
}
markers := []string{
"rate limit",
"too many requests",
"quota exceeded",
"resource exhausted",
"failed to generate",
"generation failed",
"temporarily unavailable",
"http 429",
}
for _, marker := range markers {
if strings.Contains(lower, marker) {
return true
}
}
return false
}
func fileExistsAndNonEmpty(path string) bool {
info, err := os.Stat(path)
if err != nil || info.IsDir() {
return false
}
return info.Size() > 0
}
|