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
|
package phonetic
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/sashabaranov/go-openai"
"google.golang.org/genai"
appconfig "codeberg.org/snonux/totalrecall/internal/config"
)
const (
// ProviderGemini routes phonetic requests to Gemini.
ProviderGemini Provider = "gemini"
// ProviderOpenAI routes phonetic requests to OpenAI.
ProviderOpenAI Provider = "openai"
defaultGeminiModel = "gemini-2.5-flash"
defaultOpenAIModel = openai.GPT4o
phoneticTimeout = 30 * time.Second
phoneticRetryCount = 3
phoneticTemperature = 0.3
// 200 tokens gives ample room for IPA of multi-syllable Bulgarian phrases.
// 50 was too tight: Gemini 2.5 Flash can emit several thinking tokens before
// the IPA bracket pair, causing the output to be silently truncated mid-symbol.
phoneticMaxTokens = 200
phoneticSystemPrompt = "You are a Bulgarian language expert. Provide only the IPA (International Phonetic Alphabet) transcription for Bulgarian words. Return ONLY the IPA transcription in square brackets, nothing else. No explanations, no word labels, just the IPA."
)
var geminiIPAPattern = regexp.MustCompile(`\[[^\[\]\n]+\]`)
var errNoGeminiPhoneticResponse = errors.New("no response from Gemini")
// Provider selects the phonetic backend.
type Provider string
// Config holds phonetic fetcher settings and API credentials.
type Config struct {
Provider Provider
OpenAIKey string
GoogleAPIKey string
}
// Fetcher handles fetching phonetic information for Bulgarian words.
type Fetcher struct {
provider Provider
openAIKey string
googleAPIKey string
openAIClient *openai.Client
geminiClient *genai.Client
geminiInitErr error
}
var newGeminiClient = genai.NewClient
var fetchOpenAIPhonetic = func(ctx context.Context, client *openai.Client, word string) (string, error) {
req := openai.ChatCompletionRequest{
Model: defaultOpenAIModel,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: phoneticSystemPrompt,
},
{
Role: openai.ChatMessageRoleUser,
Content: word,
},
},
Temperature: phoneticTemperature,
MaxTokens: phoneticMaxTokens,
}
resp, err := client.CreateChatCompletion(ctx, req)
if err != nil {
return "", fmt.Errorf("OpenAI API error: %w", err)
}
if len(resp.Choices) == 0 || resp.Choices[0].Message.Content == "" {
return "", fmt.Errorf("no response from OpenAI")
}
return strings.TrimSpace(resp.Choices[0].Message.Content), nil
}
var fetchGeminiPhonetic = func(ctx context.Context, client *genai.Client, word string) (string, error) {
temp := float32(phoneticTemperature)
thinkingBudget := int32(0) // IPA lookup needs no reasoning; disable thinking so
// it doesn't consume MaxOutputTokens before any visible text is emitted.
resp, err := client.Models.GenerateContent(ctx, defaultGeminiModel, []*genai.Content{
genai.NewContentFromText(buildGeminiPhoneticPrompt(word), genai.RoleUser),
}, &genai.GenerateContentConfig{
SystemInstruction: &genai.Content{
Parts: []*genai.Part{{Text: phoneticSystemPrompt}},
},
Temperature: &temp,
MaxOutputTokens: phoneticMaxTokens,
ThinkingConfig: &genai.ThinkingConfig{ThinkingBudget: &thinkingBudget},
})
if err != nil {
return "", fmt.Errorf("gemini API error: %w", err)
}
return normalizeGeminiPhoneticResponse(resp.Text())
}
// NewFetcher creates a new phonetic information fetcher.
func NewFetcher(config *Config) *Fetcher {
normalized := normalizeConfig(config)
fetcher := &Fetcher{
provider: normalized.Provider,
openAIKey: normalized.OpenAIKey,
googleAPIKey: normalized.GoogleAPIKey,
}
switch fetcher.provider {
case ProviderOpenAI:
if fetcher.openAIKey != "" {
fetcher.openAIClient = openai.NewClient(fetcher.openAIKey)
}
case ProviderGemini:
if fetcher.googleAPIKey != "" {
client, err := newGeminiClient(context.Background(), &genai.ClientConfig{
APIKey: fetcher.googleAPIKey,
})
if err != nil {
fetcher.geminiInitErr = err
} else {
fetcher.geminiClient = client
}
}
}
return fetcher
}
// FetchAndSave fetches phonetic information for a word and saves it to the word directory.
func (f *Fetcher) FetchAndSave(word, wordDir string) error {
phoneticInfo, err := f.Fetch(word)
if err != nil {
return err
}
phoneticFile := filepath.Join(wordDir, "phonetic.txt")
if err := os.WriteFile(phoneticFile, []byte(phoneticInfo), 0644); err != nil {
return fmt.Errorf("failed to write phonetic file: %w", err)
}
return nil
}
// Fetch fetches phonetic information for a word.
func (f *Fetcher) Fetch(word string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), phoneticTimeout)
defer cancel()
return f.fetchPhoneticInfo(ctx, word)
}
// Provider reports the configured phonetic backend.
func (f *Fetcher) Provider() Provider {
return f.provider
}
func (f *Fetcher) fetchPhoneticInfo(ctx context.Context, word string) (string, error) {
switch f.provider {
case ProviderOpenAI:
return f.fetchWithOpenAI(ctx, word)
case ProviderGemini:
return f.fetchWithGemini(ctx, word)
default:
return "", fmt.Errorf("unknown phonetic provider: %s", f.provider)
}
}
func (f *Fetcher) fetchWithOpenAI(ctx context.Context, word string) (string, error) {
if f.openAIKey == "" {
return "", fmt.Errorf("OpenAI API key not configured")
}
if f.openAIClient == nil {
return "", fmt.Errorf("OpenAI client not initialized")
}
return fetchOpenAIPhonetic(ctx, f.openAIClient, word)
}
func (f *Fetcher) fetchWithGemini(ctx context.Context, word string) (string, error) {
if f.googleAPIKey == "" {
return "", fmt.Errorf("google API key not configured")
}
if f.geminiInitErr != nil {
return "", fmt.Errorf("gemini client initialization failed: %w", f.geminiInitErr)
}
if f.geminiClient == nil {
return "", fmt.Errorf("gemini client not initialized")
}
var lastErr error
for attempt := 0; attempt < phoneticRetryCount; attempt++ {
phoneticInfo, err := fetchGeminiPhonetic(ctx, f.geminiClient, word)
if err == nil {
return phoneticInfo, nil
}
if !errors.Is(err, errNoGeminiPhoneticResponse) {
return "", err
}
lastErr = err
}
if lastErr != nil {
return "", lastErr
}
return "", errNoGeminiPhoneticResponse
}
func buildGeminiPhoneticPrompt(word string) string {
return fmt.Sprintf("Bulgarian text or phrase:\n%s\n\nReturn only its IPA transcription in square brackets.", strings.TrimSpace(word))
}
func normalizeGeminiPhoneticResponse(raw string) (string, error) {
trimmed := stripMarkdownCodeFence(strings.TrimSpace(raw))
if trimmed == "" {
return "", errNoGeminiPhoneticResponse
}
if match := geminiIPAPattern.FindString(trimmed); match != "" {
return match, nil
}
return trimmed, nil
}
func stripMarkdownCodeFence(raw string) string {
trimmed := strings.TrimSpace(raw)
if !strings.HasPrefix(trimmed, "```") {
return trimmed
}
trimmed = strings.TrimPrefix(trimmed, "```")
if newline := strings.Index(trimmed, "\n"); newline >= 0 {
trimmed = trimmed[newline+1:]
}
if closing := strings.LastIndex(trimmed, "```"); closing >= 0 {
trimmed = trimmed[:closing]
}
return strings.TrimSpace(trimmed)
}
func normalizeConfig(config *Config) Config {
normalized := Config{
Provider: ProviderGemini,
OpenAIKey: "",
GoogleAPIKey: "",
}
if config == nil {
return normalized
}
normalized.Provider = normalizeProvider(config.Provider)
normalized.OpenAIKey = strings.TrimSpace(config.OpenAIKey)
normalized.GoogleAPIKey = strings.TrimSpace(config.GoogleAPIKey)
return normalized
}
// normalizeProvider delegates to the shared config.NormalizeProvider so the
// normalization rule (lowercase, trim, default to "gemini") has one home.
func normalizeProvider(provider Provider) Provider {
return Provider(appconfig.NormalizeProvider(string(provider)))
}
|