feat(account): 账号测试支持选择模型

- 新增 GET /api/v1/admin/accounts/:id/models 接口获取账号可用模型
- 账号测试弹窗新增模型选择下拉框
- 测试时支持传入 model_id 参数,不传则默认使用 Sonnet
- API Key 账号支持根据 model_mapping 映射测试模型
- 将模型常量提取到 claude 包统一管理
This commit is contained in:
shaw
2025-12-19 15:59:39 +08:00
parent 733d4c2b85
commit ee86dbca9d
10 changed files with 212 additions and 40 deletions

View File

@@ -24,7 +24,6 @@ import (
const (
testClaudeAPIURL = "https://api.anthropic.com/v1/messages"
testModel = "claude-sonnet-4-5-20250929"
)
// TestEvent represents a SSE event for account testing
@@ -64,9 +63,9 @@ func generateSessionString() string {
}
// createTestPayload creates a Claude Code style test request payload
func createTestPayload() map[string]interface{} {
func createTestPayload(modelID string) map[string]interface{} {
return map[string]interface{}{
"model": testModel,
"model": modelID,
"messages": []map[string]interface{}{
{
"role": "user",
@@ -101,7 +100,8 @@ func createTestPayload() map[string]interface{} {
// TestAccountConnection tests an account's connection by sending a test request
// All account types use full Claude Code client characteristics, only auth header differs
func (s *AccountTestService) TestAccountConnection(c *gin.Context, accountID int64) error {
// modelID is optional - if empty, defaults to claude.DefaultTestModel
func (s *AccountTestService) TestAccountConnection(c *gin.Context, accountID int64, modelID string) error {
ctx := c.Request.Context()
// Get account
@@ -110,6 +110,22 @@ func (s *AccountTestService) TestAccountConnection(c *gin.Context, accountID int
return s.sendErrorAndEnd(c, "Account not found")
}
// Determine the model to use
testModelID := modelID
if testModelID == "" {
testModelID = claude.DefaultTestModel
}
// For API Key accounts with model mapping, map the model
if account.Type == "apikey" {
mapping := account.GetModelMapping()
if mapping != nil && len(mapping) > 0 {
if mappedModel, exists := mapping[testModelID]; exists {
testModelID = mappedModel
}
}
}
// Determine authentication method and API URL
var authToken string
var useBearer bool
@@ -165,11 +181,11 @@ func (s *AccountTestService) TestAccountConnection(c *gin.Context, accountID int
c.Writer.Flush()
// Create Claude Code style payload (same for all account types)
payload := createTestPayload()
payload := createTestPayload(testModelID)
payloadBytes, _ := json.Marshal(payload)
// Send test_start event
s.sendEvent(c, TestEvent{Type: "test_start", Model: testModel})
s.sendEvent(c, TestEvent{Type: "test_start", Model: testModelID})
req, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(payloadBytes))
if err != nil {