Skip to content

Commit 55f395b

Browse files
committed
feat: update status check logic
1 parent 2f293e4 commit 55f395b

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed

app/app.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,35 @@ func (a *App) testModelsAPI(apiUrl, apiKey, transformer string) (int, error) {
17231723
if resp.StatusCode != http.StatusOK {
17241724
return resp.StatusCode, fmt.Errorf("HTTP %d", resp.StatusCode)
17251725
}
1726-
return resp.StatusCode, nil
1726+
1727+
// Check if models list is not empty
1728+
body, err := io.ReadAll(resp.Body)
1729+
if err != nil {
1730+
return resp.StatusCode, fmt.Errorf("failed to read response")
1731+
}
1732+
1733+
var result map[string]interface{}
1734+
if err := json.Unmarshal(body, &result); err != nil {
1735+
return resp.StatusCode, fmt.Errorf("failed to parse response")
1736+
}
1737+
1738+
// OpenAI/Claude format: {"data": [...]}
1739+
if data, ok := result["data"].([]interface{}); ok {
1740+
if len(data) == 0 {
1741+
return resp.StatusCode, fmt.Errorf("no models found")
1742+
}
1743+
return resp.StatusCode, nil
1744+
}
1745+
1746+
// Gemini format: {"models": [...]}
1747+
if models, ok := result["models"].([]interface{}); ok {
1748+
if len(models) == 0 {
1749+
return resp.StatusCode, fmt.Errorf("no models found")
1750+
}
1751+
return resp.StatusCode, nil
1752+
}
1753+
1754+
return resp.StatusCode, fmt.Errorf("unexpected response format")
17271755
}
17281756

17291757
func (a *App) testTokenCountAPI(apiUrl, apiKey string) (int, error) {
@@ -1756,6 +1784,22 @@ func (a *App) testTokenCountAPI(apiUrl, apiKey string) (int, error) {
17561784
if resp.StatusCode != http.StatusOK {
17571785
return resp.StatusCode, fmt.Errorf("HTTP %d", resp.StatusCode)
17581786
}
1787+
1788+
// Verify response contains input_tokens
1789+
respBody, err := io.ReadAll(resp.Body)
1790+
if err != nil {
1791+
return resp.StatusCode, fmt.Errorf("failed to read response")
1792+
}
1793+
1794+
var result map[string]interface{}
1795+
if err := json.Unmarshal(respBody, &result); err != nil {
1796+
return resp.StatusCode, fmt.Errorf("failed to parse response")
1797+
}
1798+
1799+
if _, ok := result["input_tokens"]; !ok {
1800+
return resp.StatusCode, fmt.Errorf("invalid response: no input_tokens")
1801+
}
1802+
17591803
return resp.StatusCode, nil
17601804
}
17611805

@@ -1779,6 +1823,18 @@ func (a *App) testBillingAPI(apiUrl, apiKey string) (int, error) {
17791823
if resp.StatusCode != http.StatusOK {
17801824
return resp.StatusCode, fmt.Errorf("HTTP %d", resp.StatusCode)
17811825
}
1826+
1827+
// Verify response is valid JSON (billing API returns grant info)
1828+
respBody, err := io.ReadAll(resp.Body)
1829+
if err != nil {
1830+
return resp.StatusCode, fmt.Errorf("failed to read response")
1831+
}
1832+
1833+
var result map[string]interface{}
1834+
if err := json.Unmarshal(respBody, &result); err != nil {
1835+
return resp.StatusCode, fmt.Errorf("failed to parse response")
1836+
}
1837+
17821838
return resp.StatusCode, nil
17831839
}
17841840

app/frontend/src/modules/endpoints.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@ export function saveEndpointTestStatus(endpointName, success) {
2727
}
2828
}
2929

30-
// 获取端点显示状态(结合请求统计和测试状态)
31-
export function getEndpointDisplayStatus(endpointName, stats) {
32-
// 1. 有成功请求记录 → true
33-
if (stats && stats.requests > 0 && stats.requests > stats.errors) {
34-
return true;
35-
}
36-
// 2. localStorage 测试状态
30+
// 获取端点显示状态(只看测试状态,不看历史请求统计)
31+
export function getEndpointDisplayStatus(endpointName) {
3732
return getEndpointTestStatus(endpointName);
3833
}
3934

@@ -157,8 +152,8 @@ export async function renderEndpoints(endpoints) {
157152
item.draggable = true;
158153
item.dataset.name = ep.name;
159154
item.dataset.index = index;
160-
// 获取显示状态:结合请求统计和测试状态
161-
const displayStatus = getEndpointDisplayStatus(ep.name, stats);
155+
// 获取显示状态(只看测试状态)
156+
const displayStatus = getEndpointDisplayStatus(ep.name);
162157
let testStatusIcon = '⚠️'; // 默认未测试
163158
if (displayStatus === true) {
164159
testStatusIcon = '✅';
@@ -423,17 +418,29 @@ export function initEndpointSuccessListener() {
423418
}
424419
}
425420

421+
// 清除所有端点测试状态
422+
export function clearAllEndpointTestStatus() {
423+
try {
424+
localStorage.removeItem(ENDPOINT_TEST_STATUS_KEY);
425+
} catch (error) {
426+
console.error('Failed to clear endpoint test status:', error);
427+
}
428+
}
429+
426430
// 启动时零消耗检测所有端点
427431
export async function checkAllEndpointsOnStartup() {
428432
try {
433+
// 先清除所有状态
434+
clearAllEndpointTestStatus();
435+
429436
const results = await testAllEndpointsZeroCost();
430437
for (const [name, status] of Object.entries(results)) {
431438
if (status === 'ok') {
432439
saveEndpointTestStatus(name, true);
433440
} else if (status === 'invalid_key') {
434441
saveEndpointTestStatus(name, false);
435442
}
436-
// 'unknown' 不更新,保持 ⚠️
443+
// 'unknown' 保持未设置状态,显示 ⚠️
437444
}
438445
// 刷新端点列表显示
439446
if (window.loadConfig) {

0 commit comments

Comments
 (0)