Skip to content

Commit 10416fd

Browse files
committed
Fix Groq AI integration: separate token settings and auto-configure endpoint
- Split API token settings: Groq now has dedicated token field with 'Enter your Groq API key' description - HF/OpenAI-compatible retains generic 'Enter your HuggingFace or OpenAI-compatible API key' description - Auto-configure API endpoint based on provider: Groq uses api.groq.com, OpenAI uses api.openai.com - Added GROQ_TOKEN to SettingsWithoutKey for separate Groq token management - Updated AIIntegrationScreen to route Groq provider to GROQ_TOKEN setting - Fixes API key rejection error when using Groq provider
1 parent 7454c97 commit 10416fd

5 files changed

Lines changed: 43 additions & 2 deletions

File tree

app/src/main/java/helium314/keyboard/settings/SettingsContainer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ object SettingsWithoutKey {
8888
const val GEMINI_TARGET_LANGUAGE = "gemini_target_language"
8989
const val OFFLINE_MODEL_PATH = "offline_model_path"
9090
const val AI_PROVIDER = "ai_provider"
91+
const val GROQ_TOKEN = "groq_token"
9192
const val HUGGINGFACE_TOKEN = "huggingface_token"
9293
const val HUGGINGFACE_MODEL = "huggingface_model"
9394
const val HUGGINGFACE_ENDPOINT = "huggingface_endpoint"

app/src/main/java/helium314/keyboard/settings/screens/AIIntegrationScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private fun StandardAIIntegrationScreen(onClickBack: () -> Unit) {
4545
// Show settings based on selected provider
4646
when (currentProvider) {
4747
"GROQ" -> {
48-
add(SettingsWithoutKey.HUGGINGFACE_TOKEN)
48+
add(SettingsWithoutKey.GROQ_TOKEN)
4949
add(SettingsWithoutKey.GROQ_MODEL)
5050
add(SettingsWithoutKey.GEMINI_TARGET_LANGUAGE)
5151
}

app/src/main/java/helium314/keyboard/settings/screens/AdvancedScreen.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,35 @@ fun createAdvancedSettings(context: Context) = listOfNotNull(
352352
}
353353
)
354354
},
355+
Setting(context, SettingsWithoutKey.GROQ_TOKEN, R.string.groq_token_title, R.string.groq_token_summary) { setting ->
356+
var showDialog by rememberSaveable { mutableStateOf(false) }
357+
val ctx = LocalContext.current
358+
val service = remember { helium314.keyboard.latin.utils.ProofreadService(ctx) }
359+
Preference(
360+
name = setting.title,
361+
description = setting.description,
362+
onClick = { showDialog = true }
363+
)
364+
if (showDialog) {
365+
TextInputDialog(
366+
onDismissRequest = { showDialog = false },
367+
textInputLabel = { Text(stringResource(R.string.groq_token_hint)) },
368+
initialText = service.getHuggingFaceToken() ?: "",
369+
onConfirmed = { service.setHuggingFaceToken(it) },
370+
title = { Text(stringResource(R.string.groq_token_title)) },
371+
neutralButtonText = if (service.getHuggingFaceToken() != null) stringResource(R.string.delete) else null,
372+
onNeutral = { service.setHuggingFaceToken(null) },
373+
extraContent = {
374+
val uriHandler = androidx.compose.ui.platform.LocalUriHandler.current
375+
TextButton(
376+
onClick = { uriHandler.openUri("https://console.groq.com/keys") }
377+
) {
378+
Text(stringResource(R.string.get_hf_token))
379+
}
380+
}
381+
)
382+
}
383+
},
355384
Setting(context, SettingsWithoutKey.HUGGINGFACE_TOKEN, R.string.huggingface_token_title, R.string.huggingface_token_summary) { setting ->
356385
var showDialog by rememberSaveable { mutableStateOf(false) }
357386
val ctx = LocalContext.current

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@
388388
<string name="ai_provider_openai">HF/OpenAI-compatible</string>
389389
<string name="huggingface_token_title">API Token</string>
390390
<string name="huggingface_token_summary">Enter your HuggingFace or OpenAI-compatible API key</string>
391+
<string name="groq_token_title">API Token</string>
392+
<string name="groq_token_summary">Enter your Groq API key</string>
393+
<string name="groq_token_hint">Enter API key</string>
391394
<string name="huggingface_token_hint">Enter API key</string>
392395
<string name="get_hf_token">Get Groq Key</string>
393396
<string name="huggingface_no_token">API token not set. Go to Settings → Advanced.</string>

app/src/standard/java/helium314/keyboard/latin/utils/ProofreadService.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,14 @@ class ProofreadService(private val context: Context) {
133133
}
134134

135135
// HuggingFace API endpoint
136-
fun getHuggingFaceEndpoint(): String = securePrefs.getString(KEY_HF_ENDPOINT, DEFAULT_HF_ENDPOINT) ?: DEFAULT_HF_ENDPOINT
136+
fun getHuggingFaceEndpoint(): String {
137+
// Auto-select correct endpoint based on provider
138+
val defaultEndpoint = when (getProvider()) {
139+
AIProvider.GROQ -> GROQ_ENDPOINT
140+
else -> DEFAULT_HF_ENDPOINT
141+
}
142+
return securePrefs.getString(KEY_HF_ENDPOINT, defaultEndpoint) ?: defaultEndpoint
143+
}
137144

138145
fun setHuggingFaceEndpoint(endpoint: String) {
139146
securePrefs.edit().putString(KEY_HF_ENDPOINT, endpoint.trim()).apply()
@@ -407,6 +414,7 @@ class ProofreadService(private val context: Context) {
407414
private const val DEFAULT_TARGET_LANGUAGE = "English"
408415
private const val DEFAULT_HF_MODEL = "gpt-4o-mini"
409416
private const val DEFAULT_HF_ENDPOINT = "https://api.openai.com/v1/chat/completions"
417+
private const val GROQ_ENDPOINT = "https://api.groq.com/openai/v1/chat/completions"
410418

411419
val AVAILABLE_MODELS = listOf(
412420
"gemini-flash-latest",

0 commit comments

Comments
 (0)