Skip to content

Commit 3c75db3

Browse files
committed
Hide blocked free models from mini app
1 parent 444e9fe commit 3c75db3

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

internal/adminapi/adminapi_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,40 @@ func TestTGAdminModelsMarksFreeSearchResultsUnverified(t *testing.T) {
293293
}
294294
}
295295

296+
func TestTGAdminModelsHidesBlockedFreeSearchResults(t *testing.T) {
297+
s, _ := newTGModelTestServer(t)
298+
s.cfgRef.Telegram.BotToken = "123:abc"
299+
s.cfgRef.Telegram.OwnerChatID = 42
300+
checks := map[string]modelCheckStatus{
301+
modelCheckKey("openrouter", "qwen/qwen3.5-flash:free"): {
302+
Status: "free_blocked",
303+
CheckedAt: time.Now().Format(time.RFC3339),
304+
Error: "api error (HTTP 404): This model is unavailable for free. The paid version is available now.",
305+
},
306+
}
307+
data, err := json.Marshal(checks)
308+
if err != nil {
309+
t.Fatal(err)
310+
}
311+
s.settings = &fakeSettingsStore{values: map[string]string{settingKeyModelChecks: string(data)}}
312+
313+
req := httptest.NewRequest(http.MethodGet, "/tg-admin/api/models?role=default&q=:free", nil)
314+
req.Header.Set("X-Telegram-Init-Data", signedTGInitData(t, "123:abc", 42, time.Now()))
315+
rec := httptest.NewRecorder()
316+
s.handleTGAdminRouter(rec, req)
317+
318+
if rec.Code != http.StatusOK {
319+
t.Fatalf("status = %d, want %d, body: %s", rec.Code, http.StatusOK, rec.Body.String())
320+
}
321+
var payload tgAdminModelsResponse
322+
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
323+
t.Fatal(err)
324+
}
325+
if len(payload.Models) != 0 {
326+
t.Fatalf("blocked free models should be hidden: %+v", payload.Models)
327+
}
328+
}
329+
296330
func TestTGAdminRecommendedModelsAppendFreeCandidatesUnrecommended(t *testing.T) {
297331
s, _ := newTGModelTestServer(t)
298332
s.cfgRef.Telegram.BotToken = "123:abc"
@@ -327,6 +361,42 @@ func TestTGAdminRecommendedModelsAppendFreeCandidatesUnrecommended(t *testing.T)
327361
}
328362
}
329363

364+
func TestTGAdminRecommendedModelsHideBlockedFreeCandidates(t *testing.T) {
365+
s, _ := newTGModelTestServer(t)
366+
s.cfgRef.Telegram.BotToken = "123:abc"
367+
s.cfgRef.Telegram.OwnerChatID = 42
368+
checks := map[string]modelCheckStatus{
369+
modelCheckKey("openrouter", "qwen/qwen3.5-flash:free"): {
370+
Status: "free_blocked",
371+
CheckedAt: time.Now().Format(time.RFC3339),
372+
Error: "api error (HTTP 404): This model is unavailable for free. The paid version is available now.",
373+
},
374+
}
375+
data, err := json.Marshal(checks)
376+
if err != nil {
377+
t.Fatal(err)
378+
}
379+
s.settings = &fakeSettingsStore{values: map[string]string{settingKeyModelChecks: string(data)}}
380+
381+
req := httptest.NewRequest(http.MethodGet, "/tg-admin/api/models?role=simple", nil)
382+
req.Header.Set("X-Telegram-Init-Data", signedTGInitData(t, "123:abc", 42, time.Now()))
383+
rec := httptest.NewRecorder()
384+
s.handleTGAdminRouter(rec, req)
385+
386+
if rec.Code != http.StatusOK {
387+
t.Fatalf("status = %d, want %d, body: %s", rec.Code, http.StatusOK, rec.Body.String())
388+
}
389+
var payload tgAdminModelsResponse
390+
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
391+
t.Fatal(err)
392+
}
393+
for _, model := range payload.Models {
394+
if model.ID == "qwen/qwen3.5-flash:free" {
395+
t.Fatalf("blocked free candidate should be hidden: %+v", payload.Models)
396+
}
397+
}
398+
}
399+
330400
func TestTGAdminModelCheckPersistsFreeModelStatus(t *testing.T) {
331401
s, _ := newTGModelTestServer(t)
332402
s.cfgRef.Telegram.BotToken = "123:abc"

internal/adminapi/tgadmin.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ func (s *Server) buildTGAdminModels(ctx context.Context, role, provider, query s
431431
if len(models) == 0 {
432432
models = tgAdminBrowseModels(allCaps, aaModels, query, role)
433433
}
434+
models = filterBlockedFreeModels(models, provider, checks)
434435
resp.Recommended = recommendedMode
435436
if len(models) > limit {
436437
models = models[:limit]
@@ -514,6 +515,21 @@ func tgAdminBrowseModels(allCaps map[string]llm.Capabilities, aaModels map[strin
514515
return models
515516
}
516517

518+
func filterBlockedFreeModels(models []uiModel, provider string, checks map[string]modelCheckStatus) []uiModel {
519+
if len(models) == 0 || len(checks) == 0 {
520+
return models
521+
}
522+
out := models[:0]
523+
for _, m := range models {
524+
check := checks[modelCheckKey(provider, m.ID)]
525+
if m.Free && check.Status == "free_blocked" {
526+
continue
527+
}
528+
out = append(out, m)
529+
}
530+
return out
531+
}
532+
517533
func appendFreeCandidates(models, free []uiModel, role string) []uiModel {
518534
switch role {
519535
case "simple", "classifier":

0 commit comments

Comments
 (0)