Skip to content

Commit 7bc2c79

Browse files
committed
Filter AI endpoint presets by protocol
1 parent b768871 commit 7bc2c79

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

app/src/main/java/com/driezy/medlog/ui/screen/settings/CloudAiSettingsPanel.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ internal fun CloudAiSettingsPanel(
222222
EndpointPresetPicker(
223223
presets = uiState.cloudAiEndpointPresets,
224224
currentBaseUrl = uiState.openAiCompatibleBaseUrl,
225+
protocol = CloudAiEndpointProtocol.OPENAI_COMPATIBLE,
225226
onSelect = { preset ->
226227
baseUrlDraft = preset.api
227228
providerNameDraft = preset.name
@@ -295,10 +296,9 @@ internal fun CloudAiSettingsPanel(
295296
}
296297
if (uiState.cloudAiProvider == CloudAiProvider.ANTHROPIC) {
297298
EndpointPresetPicker(
298-
presets = uiState.cloudAiEndpointPresets.filter {
299-
it.protocol == CloudAiEndpointProtocol.ANTHROPIC
300-
},
299+
presets = uiState.cloudAiEndpointPresets,
301300
currentBaseUrl = uiState.anthropicCloudAiBaseUrl,
301+
protocol = CloudAiEndpointProtocol.ANTHROPIC,
302302
onSelect = { preset ->
303303
anthropicBaseUrlDraft = preset.api
304304
providerNameDraft = preset.name
@@ -425,16 +425,19 @@ internal fun CloudAiSettingsPanel(
425425
private fun EndpointPresetPicker(
426426
presets: List<CloudAiEndpointPreset>,
427427
currentBaseUrl: String,
428+
protocol: CloudAiEndpointProtocol,
428429
onSelect: (CloudAiEndpointPreset) -> Unit,
429430
) {
430-
if (presets.isEmpty()) return
431+
val protocolPresetCount = presets.count { it.protocol == protocol }
432+
if (protocolPresetCount == 0) return
431433

432434
var expanded by rememberSaveable { mutableStateOf(false) }
433435
var query by rememberSaveable { mutableStateOf("") }
434436
val presentation = CloudAiEndpointPresetListPresentation.from(
435437
presets = presets,
436438
query = query,
437439
currentBaseUrl = currentBaseUrl,
440+
protocol = protocol,
438441
)
439442
Column(
440443
modifier = Modifier.fillMaxWidth(),
@@ -446,7 +449,7 @@ private fun EndpointPresetPicker(
446449
verticalAlignment = Alignment.CenterVertically,
447450
) {
448451
Text(
449-
text = stringResource(R.string.settings_ai_endpoint_presets_title, presets.size),
452+
text = stringResource(R.string.settings_ai_endpoint_presets_title, protocolPresetCount),
450453
style = MaterialTheme.typography.bodySmall,
451454
color = MaterialTheme.colorScheme.onSurfaceVariant,
452455
modifier = Modifier.weight(1f),
@@ -526,11 +529,13 @@ internal data class CloudAiEndpointPresetListPresentation(
526529
presets: List<CloudAiEndpointPreset>,
527530
query: String,
528531
currentBaseUrl: String,
532+
protocol: CloudAiEndpointProtocol,
529533
): CloudAiEndpointPresetListPresentation {
530534
val normalizedQuery = query.trim().lowercase()
531535
val normalizedCurrentBaseUrl = currentBaseUrl.normalizedEndpointUrl()
532536
val rows = presets
533537
.asSequence()
538+
.filter { preset -> preset.protocol == protocol }
534539
.filter { preset ->
535540
normalizedQuery.isBlank() ||
536541
preset.name.lowercase().contains(normalizedQuery) ||

app/src/test/java/com/driezy/medlog/ui/screen/settings/CloudAiSettingsPresentationTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class CloudAiSettingsPresentationTest {
111111
),
112112
query = "zen",
113113
currentBaseUrl = "",
114+
protocol = CloudAiEndpointProtocol.OPENAI_COMPATIBLE,
114115
).rows
115116

116117
assertEquals(1, rows.size)
@@ -131,8 +132,34 @@ class CloudAiSettingsPresentationTest {
131132
),
132133
query = "",
133134
currentBaseUrl = "https://api.deepseek.com/",
135+
protocol = CloudAiEndpointProtocol.OPENAI_COMPATIBLE,
134136
).rows
135137

136138
assertEquals(true, rows.single().selected)
137139
}
140+
141+
@Test
142+
fun `endpoint preset list filters by protocol`() {
143+
val rows = CloudAiEndpointPresetListPresentation.from(
144+
presets = listOf(
145+
CloudAiEndpointPreset(
146+
id = "openai",
147+
name = "OpenAI",
148+
api = "https://api.openai.com/v1",
149+
protocol = CloudAiEndpointProtocol.OPENAI_COMPATIBLE,
150+
),
151+
CloudAiEndpointPreset(
152+
id = "anthropic",
153+
name = "Anthropic",
154+
api = "https://api.anthropic.com",
155+
protocol = CloudAiEndpointProtocol.ANTHROPIC,
156+
),
157+
),
158+
query = "",
159+
currentBaseUrl = "",
160+
protocol = CloudAiEndpointProtocol.OPENAI_COMPATIBLE,
161+
).rows
162+
163+
assertEquals(listOf("openai"), rows.map { it.id })
164+
}
138165
}

0 commit comments

Comments
 (0)