-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.go
More file actions
209 lines (174 loc) · 6.39 KB
/
Copy pathsettings.go
File metadata and controls
209 lines (174 loc) · 6.39 KB
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
package main
import (
"context"
"encoding/json"
"os"
"path/filepath"
"sync"
"wails-cast/pkg/folders"
)
const (
settingsFileName = "settings.json"
)
func getDefaultSettings() Settings {
return Settings{
SubtitleBurnIn: true,
IgnoreClosedCaptions: false,
DefaultTranslationLanguage: "English",
LLMProvider: "opencode",
LLMApiKey: "",
LLMModel: "",
LLMBaseURL: "",
DefaultQuality: "5M",
SubtitleFontSize: 24,
MaxOutputWidth: 0,
TranslatePromptTemplate: "Create a subtitle translation in {{.TargetLanguage}} based on the references in other languages.\nMultiple language tracks from the same video are provided as reference to help you understand context and maintain consistent terminology.\n\nInput format:\ndelay: <seconds>\nduration: <seconds>\n<text>\n\n{{.SubtitleContent}}\n\nOutput the translation in the same format inside <llm_output></llm_output> tags.",
MaxSubtitleSamples: 4,
NoTranscodeCache: false,
RemoteAPIEnabled: false,
RemoteAPIPort: 9999,
RemoteAPIToken: "",
QbtURL: "http://127.0.0.1:8080",
QbtUser: "admin",
QbtPass: "",
QbtSavePath: "",
}
}
type Settings struct {
SubtitleBurnIn bool `json:"subtitleBurnIn"`
IgnoreClosedCaptions bool `json:"ignoreClosedCaptions"`
DefaultTranslationLanguage string `json:"defaultTranslationLanguage"`
// LLMProvider selects which backend to use for AI features.
// Supported values: "opencode" (default), "openai-compat".
LLMProvider string `json:"llmProvider"`
// LLMApiKey is the API key for the selected provider.
// For "opencode", if empty, falls back to ai.LoadOpenCodeAPIKey().
// For "openai-compat", this is the Bearer token.
LLMApiKey string `json:"llmApiKey"`
// LLMModel is the model to request from the provider.
LLMModel string `json:"llmModel"`
// LLMBaseURL is the base URL for the provider endpoint.
// Only used when LLMProvider == "openai-compat".
// For "opencode", the fixed ai.OpenCodeBaseURL is used instead.
LLMBaseURL string `json:"llmBaseURL"`
DefaultQuality string `json:"defaultQuality"`
SubtitleFontSize int `json:"subtitleFontSize"`
// SubtitleDelaySeconds shifts subtitle timing for the current playback.
// Positive = subtitles appear later, negative = earlier.
SubtitleDelaySeconds float64 `json:"subtitleDelaySeconds"`
// SubtitleBold / SubtitleItalic style rendered subtitles.
SubtitleBold bool `json:"subtitleBold"`
SubtitleItalic bool `json:"subtitleItalic"`
MaxOutputWidth int `json:"maxOutputWidth"`
TranslatePromptTemplate string `json:"translatePromptTemplate"`
MaxSubtitleSamples int `json:"maxSubtitleSamples"`
NoTranscodeCache bool `json:"noTranscodeCache"`
// Library feature settings.
LibraryRoot string `json:"libraryRoot"`
TMDBApiKey string `json:"tmdbApiKey"`
// Remote API (HTTP server for companion apps, e.g. Android)
RemoteAPIEnabled bool `json:"remoteApiEnabled"`
RemoteAPIPort int `json:"remoteApiPort"`
RemoteAPIToken string `json:"remoteApiToken"` // empty = no auth required
// qBittorrent integration (used by the instance that owns the library, e.g.
// the fedora host). The remote API's /torrent/* endpoints proxy magnet links
// to qBittorrent's Web API and report download progress.
QbtURL string `json:"qbtURL"` // qBittorrent Web UI base URL
QbtUser string `json:"qbtUser"` // Web UI username
QbtPass string `json:"qbtPass"` // Web UI password
QbtSavePath string `json:"qbtSavePath"` // download dir; empty = LibraryRoot
}
// legacySettings is used during load to migrate old field names to the new
// unified LLM fields. We unmarshal into this struct first, then copy any
// non-empty legacy values into Settings if the new fields are still empty.
type legacySettings struct {
GeminiApiKey string `json:"geminiApiKey"`
GeminiModel string `json:"geminiModel"`
OpenAICompatBaseURL string `json:"openAICompatBaseURL"`
OpenAICompatAPIKey string `json:"openAICompatApiKey"`
OpenAICompatModel string `json:"openAICompatModel"`
}
type SettingsStore struct {
settings Settings
filePath string
ctx context.Context
mu sync.RWMutex
}
func NewSettingsStore() *SettingsStore {
appConfigDir := folders.GetConfig()
os.MkdirAll(appConfigDir, 0755)
settingsPath := filepath.Join(appConfigDir, settingsFileName)
store := &SettingsStore{
settings: getDefaultSettings(),
filePath: settingsPath,
}
store.load()
return store
}
func (s *SettingsStore) SetContext(ctx context.Context) {
s.ctx = ctx
}
func (s *SettingsStore) load() error {
s.mu.Lock()
defer s.mu.Unlock()
data, err := os.ReadFile(s.filePath)
if err != nil {
if os.IsNotExist(err) {
return nil // No settings file yet, use defaults
}
return err
}
if err := json.Unmarshal(data, &s.settings); err != nil {
return err
}
// Migration: if the new unified LLM fields are empty but the legacy fields
// are present in the file, copy them over so we don't silently lose the
// user's saved credentials. The next save will persist only the new names.
var legacy legacySettings
if err := json.Unmarshal(data, &legacy); err == nil {
if s.settings.LLMApiKey == "" {
switch s.settings.LLMProvider {
case "openai-compat":
s.settings.LLMApiKey = legacy.OpenAICompatAPIKey
default:
s.settings.LLMApiKey = legacy.GeminiApiKey
}
}
if s.settings.LLMModel == "" {
switch s.settings.LLMProvider {
case "openai-compat":
s.settings.LLMModel = legacy.OpenAICompatModel
default:
s.settings.LLMModel = legacy.GeminiModel
}
}
if s.settings.LLMBaseURL == "" && s.settings.LLMProvider == "openai-compat" {
s.settings.LLMBaseURL = legacy.OpenAICompatBaseURL
}
}
return nil
}
func (s *SettingsStore) save() error {
data, err := json.MarshalIndent(s.settings, "", " ")
if err != nil {
return err
}
return os.WriteFile(s.filePath, data, 0644)
}
func (s *SettingsStore) Get() *Settings {
s.mu.RLock()
defer s.mu.RUnlock()
return &s.settings
}
func (s *SettingsStore) Update(settings Settings) error {
s.mu.Lock()
defer s.mu.Unlock()
s.settings = settings
return s.save()
}
func (s *SettingsStore) Reset() error {
s.mu.Lock()
defer s.mu.Unlock()
s.settings = getDefaultSettings()
return s.save()
}