Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.
C
feat(lyrics): bump Gemini to 3.8 Flash and add thinking and timeout settings (#102)
anna2890b0ddf4d9
diff --git a/docs/tweaks.md b/docs/tweaks.mdindex cd4ff7fa..8e0da2ec 100644--- a/docs/tweaks.md+++ b/docs/tweaks.md@@ -164,7 +164,7 @@ Shared:NaturalLanguage, both languages downloaded in the Translate app or the item says where to) and by AppleIntelligence's model (iOS 26 where it is on and speaks the language; permissive guardrails for changingthe user's own text, 12 lines a session, greedy and capped at 60 tokens a line, exactly one string a line by a generation schema; Translate retried once, as its first ask can fail while it loads), both in- OnDeviceTranslation.swift, the frameworks weak linked; and by an AI translator on the user's own key, picked on the Lyrics page (SGKeyAITranslator): Gemini, an OpenAI-compatible server (chat completions; OpenAI's unless a server URL is given, such as OpenRouter's or an Ollama on the network, which Spotify's ATS allows for local addresses) or Anthropic (the Messages API). Each provider is one entry of LyricsTranslation.m's table: its Keychain slot (one key each, never in the settings, so an export never has one), its model presets (the cheapest first; OpenAI-compatible has none and needs a model), its request and its reply's reader. The answer's shape is forced where the API can (Gemini a bare array, the others {"lines": [...]} by a strict schema), the reader takes either, and a 400 that names an optional field (Gemini's thinking level, OpenAI's schema or reasoning effort, Anthropic's output_config) is asked once more without them. The prompt is shared (Vitrine's with {language} and {count}, editable in a sheet, SGKeyAIPrompt) and the provider adds its sentence on the answer's shape; the model (spotifyglass.lyrics.<id>.model) and the server are read at each ask and checked as they are read, and the log names the provider, the model, the host and the prompt's SHA-256 prefix, never the key. Temperature is not offered: Anthropic's API turns it down and OpenAI's reasoning models take only the default. The menu item says where the lyrics go. Translate on iPhone and Apple Intelligence each have a switch that leaves them out of the menu (SGKeyLyricsTranslateOnDevice, SGKeyLyricsAppleIntelligence, both on). A translation that reads the same as its line (SGLyricsBareText) is not drawn under it, and the line still counts as translated. The translate items show for every song with Translate any song on (SGKeyLyricsTranslateEverySong, off), else only for a song NaturalLanguage finds in another language than the Lyrics page's (or the iPhone's): one distinct line in five it is at least 80% sure of, or two in another script (a verse in Hangul among English), not the whole song's guess (short lines sway that: Havana reads as Dutch), so songs half in English count and a repeated chorus weighs as one line; Translate on iPhone translates from the language most of those lines are in and leaves lines already in the target as they are. Apple Intelligence's lines show and are saved a batch at a time, only lines without a translation are sent, and a batch it turns down is skipped (asking again sends only those lines). Every finished translation is kept by SavedTranslations.m in Caches/Vitrine/Translations, one JSON file a song and language with each line's text to its translation under "lines" and who made it and when ("by", "model", "made"; a file from before has the lines alone and reads as Unknown), the newest 200, synced when the song's lines are shown: each line knows whether its translation was made here (SGKaraokeLine.translationMade), and a made one with nothing saved is taken off, so the Lyrics page's Saved translations row deletes them at once, posting SGLyricsTranslationsDidChangeNotification as Translate any song does for the open lyrics to sync and redraw. The translate menu names who made a saved translation and when, and for one an AI made (or no one known) offers Delete This Translation, which removes the song's file and the launch's answer for it, so asking again sends the song. Gemini's reply read and checked on the Mac+ OnDeviceTranslation.swift, the frameworks weak linked; and by an AI translator on the user's own key, picked on the Lyrics page (SGKeyAITranslator): Gemini, an OpenAI-compatible server (chat completions; OpenAI's unless a server URL is given, such as OpenRouter's or an Ollama on the network, which Spotify's ATS allows for local addresses) or Anthropic (the Messages API). Each provider is one entry of LyricsTranslation.m's table: its Keychain slot (one key each, never in the settings, so an export never has one), its model presets (the cheapest first; OpenAI-compatible has none and needs a model), its request and its reply's reader. The answer's shape is forced where the API can (Gemini a bare array, the others {"lines": [...]} by a strict schema), the reader takes either, and a 400 that names an optional field (Gemini's thinking level, OpenAI's schema or reasoning effort, Anthropic's output_config) is asked once more without them. The prompt is shared (Vitrine's with {language} and {count}, editable in a sheet, SGKeyAIPrompt) and the provider adds its sentence on the answer's shape; the model (spotifyglass.lyrics.<id>.model), the server, Gemini's thinking level (SGKeyAIThinking: Low, Medium or High, unset low, as 3.8 Flash refuses minimal) and the request timeout (SGKeyAITimeout: 30 to 300 s, unset 120, the request's timeoutInterval) are read at each ask and checked as they are read, a new thinking level asks again, and the log names the provider, the model, the host and the prompt's SHA-256 prefix, never the key. Temperature is not offered: Anthropic's API turns it down and OpenAI's reasoning models take only the default. The menu item says where the lyrics go. Translate on iPhone and Apple Intelligence each have a switch that leaves them out of the menu (SGKeyLyricsTranslateOnDevice, SGKeyLyricsAppleIntelligence, both on). A translation that reads the same as its line (SGLyricsBareText) is not drawn under it, and the line still counts as translated. The translate items show for every song with Translate any song on (SGKeyLyricsTranslateEverySong, off), else only for a song NaturalLanguage finds in another language than the Lyrics page's (or the iPhone's): one distinct line in five it is at least 80% sure of, or two in another script (a verse in Hangul among English), not the whole song's guess (short lines sway that: Havana reads as Dutch), so songs half in English count and a repeated chorus weighs as one line; Translate on iPhone translates from the language most of those lines are in and leaves lines already in the target as they are. Apple Intelligence's lines show and are saved a batch at a time, only lines without a translation are sent, and a batch it turns down is skipped (asking again sends only those lines). Every finished translation is kept by SavedTranslations.m in Caches/Vitrine/Translations, one JSON file a song and language with each line's text to its translation under "lines" and who made it and when ("by", "model", "made"; a file from before has the lines alone and reads as Unknown), the newest 200, synced when the song's lines are shown: each line knows whether its translation was made here (SGKaraokeLine.translationMade), and a made one with nothing saved is taken off, so the Lyrics page's Saved translations row deletes them at once, posting SGLyricsTranslationsDidChangeNotification as Translate any song does for the open lyrics to sync and redraw. The translate menu names who made a saved translation and when, and for one an AI made (or no one known) offers Delete This Translation, which removes the song's file and the launch's answer for it, so asking again sends the song. Gemini's reply read and checked on the Macagainst harness/lyrics-translation/LockScreenLyrics/ the line being sung in the system's now playing (Lock screen translation, SGKeyLockScreenTranslation,off: a translation the line has in its place, its words timed over the line's and cut in piecesdiff --git a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.hindex 151738bb..9c8f73dd 100644--- a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h+++ b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h@@ -24,6 +24,9 @@ NSArray<SGModRow *> *SGOnDeviceTranslatorRows(void);// is kept in the Keychain beside its key, not here. All read at each translation.#define SGKeyAITranslator @"spotifyglass.lyrics.ai"#define SGKeyAIPrompt @"spotifyglass.lyrics.prompt"+// Gemini's thinking level (unset: low) and the request timeout in seconds for every provider (unset: 120).+#define SGKeyAIThinking @"spotifyglass.lyrics.gemini.thinking"+#define SGKeyAITimeout @"spotifyglass.lyrics.ai.timeout"// Who made a translation: an AI provider's id with its model, or one of the two on the iPhone. A translation// saved before sources were kept has neither, and reads as unknown.diff --git a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.mindex 23bef6e0..a0619e0b 100644--- a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m+++ b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m@@ -53,12 +53,19 @@ static NSString *string(id value) {return [value isKindOfClass:NSString.class] ? value : nil;}+static const NSInteger kDefaultAITimeout = 120;++static NSTimeInterval aiTimeout(void) {+ NSInteger seconds = [NSUserDefaults.standardUserDefaults integerForKey:SGKeyAITimeout];+ return seconds > 0 ? seconds : kDefaultAITimeout;+}+static NSMutableURLRequest *post(NSString *url, NSDictionary *body) {NSURL *endpoint = url ? [NSURL URLWithString:url] : nil;if (!endpoint) return nil;NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:endpoint];request.HTTPMethod = @"POST";- request.timeoutInterval = 60;+ request.timeoutInterval = aiTimeout();[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];request.HTTPBody = [NSJSONSerialization dataWithJSONObject:body options:0 error:nil];return request;@@ -70,6 +77,12 @@ static NSDictionary *linesSchema(void) {@"required": @[@"lines"], @"additionalProperties": @NO};}+static NSString *geminiThinkingLevel(void) {+ // Only the menu's levels: a stored "minimal" from an early build of this row would make 3.8 Flash answer 400 every ask.+ NSString *level = [NSUserDefaults.standardUserDefaults stringForKey:SGKeyAIThinking];+ return [@[@"medium", @"high"] containsObject:level] ? level : @"low";+}+// Gemini: generateContent, the key in x-goog-api-key, a bare array forced by responseSchema.static NSMutableURLRequest *geminiRequest(NSString *model, NSString *prompt, NSString *key, BOOL plain) {// Lyrics are often explicit, and translating them is what the user asked for, so every filter that can@@ -83,7 +96,7 @@ static NSMutableURLRequest *geminiRequest(NSString *model, NSString *prompt, NSS@"responseSchema": @{@"type": @"ARRAY", @"items": @{@"type": @"STRING"}}} mutableCopy];// Gemini 3 thinks at length by default, which a whole song's translation does not need and which made it// slow enough to time out. Plain: a model named in Other… that does not know the field.- if (!plain) config[@"thinkingConfig"] = @{@"thinkingLevel": @"low"};+ if (!plain) config[@"thinkingConfig"] = @{@"thinkingLevel": geminiThinkingLevel()};NSMutableURLRequest *request = post([NSString stringWithFormat:@"https://generativelanguage.googleapis.com/v1beta/models/%@:generateContent", model],@{@"contents": @[@{@"parts": @[@{@"text": prompt}]}], @"safetySettings": safety, @"generationConfig": config});[request setValue:key forHTTPHeaderField:@"x-goog-api-key"];@@ -185,7 +198,7 @@ static const SGAIProvider kProviders[] = {@"Answer with a JSON array of the {count} translations as strings.",// A pinned id first, the one users report working. gemini-flash-latest follows each Flash release, preview// ones included, with notice only by email to the key's owner, so it is offered but not the default.- {@"gemini-3.6-flash", @"gemini-3.5-flash-lite", @"gemini-flash-latest"}, {@"3.6 Flash", @"3.5 Flash-Lite", @"Flash (latest)"},+ {@"gemini-3.8-flash", @"gemini-3.5-flash-lite", @"gemini-flash-latest"}, {@"3.8 Flash", @"3.5 Flash-Lite", @"Flash (latest)"},YES, {@"thinking"}, geminiRequest, geminiAnswer},{@"openai", @"OpenAI-compatible", nil, @"Vitrine.OpenAI",@"Sends the song's lyrics to the server below: OpenAI's own unless you name another, such as OpenRouter, or an "@@ -390,7 +403,9 @@ NSArray<NSString *> *SGAITranslationsIn(NSString *provider, id root, NSInteger s// {"error": {"message": ...}} from all three; a bare string from some compatible servers.NSString *says = string(dig(reply, @[@"error", @"message"])) ?: string(reply[@"error"]);if (error) {- *problem = [NSString stringWithFormat:@"%@ could not be reached.", who];+ *problem = error.code == NSURLErrorTimedOut+ ? [NSString stringWithFormat:@"%@ timed out. Try a longer timeout on the Lyrics page.", who]+ : [NSString stringWithFormat:@"%@ could not be reached.", who];} else if (status == 401 || status == 403 || (status == 400 && [says localizedCaseInsensitiveContainsString:@"API key"])) {*problem = [NSString stringWithFormat:@"%@ turned the key down. Check it on the Lyrics page.", who];} else if (status == 400 || status == 404) {@@ -439,7 +454,7 @@ static void send(const SGAIProvider *p, NSURLRequest *request, NSURLRequest *pla}] resume];}-// Per launch, by "track|language|" and then the provider, server, model and prompt: a translation asked again+// Per launch, by "track|language|" and then the provider, server, model, Gemini's thinking and prompt: a translation asked again// after any of them changed is a new one.static NSMutableDictionary<NSString *, NSArray<NSString *> *> *sg_done;@@ -461,8 +476,9 @@ void SGLyricsTranslateWithAI(NSString *trackID, NSArray<SGKaraokeLine *> *lines,return;}if (!sg_done) sg_done = [NSMutableDictionary dictionary];- NSString *memo = [NSString stringWithFormat:@"%@|%@|%@|%@|%@|%@", trackID, languageTag, p->tag,- isOpenAI(p) ? openAIServer() : @"", modelID, template];+ NSString *memo = [NSString stringWithFormat:@"%@|%@|%@|%@|%@|%@|%@", trackID, languageTag, p->tag,+ isOpenAI(p) ? openAIServer() : @"", modelID,+ [p->tag isEqualToString:@"gemini"] ? geminiThinkingLevel() : @"", template];if (trackID && sg_done[memo].count == lines.count) {done(sg_done[memo], nil);return;@@ -744,5 +760,25 @@ NSArray<SGModRow *> *SGAITranslatorRows(void) {SGPresentDark(nav);[SGTopController() presentViewController:nav animated:YES completion:nil];});- return @[provider, key, models, prompt];+ // Low first: 3.8 Flash refuses minimal.+ NSArray<NSString *> *thinkingLevels = @[@"Low", @"Medium", @"High"];+ SGModRow *thinking = SGMenuRow(@"Thinking", thinkingLevels, ^NSString *{+ return geminiThinkingLevel().capitalizedString;+ }, ^(NSInteger index) {+ if (index == 0) [NSUserDefaults.standardUserDefaults removeObjectForKey:SGKeyAIThinking];+ else [NSUserDefaults.standardUserDefaults setObject:thinkingLevels[index].lowercaseString forKey:SGKeyAIThinking];+ translatorChanged();+ });+ thinking.visible = ^{ return [chosen()->tag isEqualToString:@"gemini"]; };+ NSArray<NSNumber *> *timeoutSeconds = @[@30, @60, @90, @(kDefaultAITimeout), @180, @240, @300];+ NSMutableArray<NSString *> *timeouts = [NSMutableArray array];+ for (NSNumber *seconds in timeoutSeconds) [timeouts addObject:[NSString stringWithFormat:@"%@ s", seconds]];+ SGModRow *timeout = SGMenuRow(@"Timeout", timeouts, ^NSString *{+ return [NSString stringWithFormat:@"%ld s", (long)aiTimeout()];+ }, ^(NSInteger index) {+ NSInteger seconds = timeoutSeconds[index].integerValue;+ if (seconds == kDefaultAITimeout) [NSUserDefaults.standardUserDefaults removeObjectForKey:SGKeyAITimeout];+ else [NSUserDefaults.standardUserDefaults setInteger:seconds forKey:SGKeyAITimeout];+ });+ return @[provider, key, models, thinking, timeout, prompt];}