Fix: align go provider apis with python apis (#16867)

### Summary

As title.

---------

Co-authored-by: Jin Hai <haijin.chn@gmail.com>
Co-authored-by: Wang Qi <wangq8@outlook.com>
This commit is contained in:
Lynn
2026-07-14 21:06:11 +08:00
committed by GitHub
parent 55d5879622
commit bd7a3bb878
18 changed files with 790 additions and 577 deletions

View File

@@ -244,16 +244,15 @@ func (h *ProviderHandler) ShowProviderInstance(c *gin.Context) {
return
}
instanceName := c.Param("instance_name")
if instanceName == "" {
instanceIDOrName := c.Param("instance_name")
if instanceIDOrName == "" {
common.ResponseWithHttpCodeData(c, http.StatusBadRequest, 400, nil, "Instance name is required")
return
}
userID := c.GetString("user_id")
// Get tenant ID from user
instance, errorCode, err := h.modelProviderService.ShowProviderInstance(providerName, instanceName, userID)
instance, errorCode, err := h.modelProviderService.ShowProviderInstance(providerName, instanceIDOrName, userID)
if err != nil {
common.ErrorWithCode(c, errorCode, err.Error())
return
@@ -516,12 +515,15 @@ func (h *ProviderHandler) ListInstanceModels(c *gin.Context) {
common.SuccessWithData(c, modelInstances, "success")
}
type EnableOrDisableModelRequest struct {
ModelID string `json:"model_id"`
Status string `json:"status"`
type AlterModelRequest struct {
ModelID string `json:"model_id"`
Status string `json:"status"`
MaxTokens int `json:"max_tokens"`
ModelType interface{} `json:"model_type"`
Extra interface{} `json:"extra"`
}
func (h *ProviderHandler) EnableOrDisableModel(c *gin.Context) {
func (h *ProviderHandler) AlterModel(c *gin.Context) {
providerName := c.Param("provider_name")
if providerName == "" {
common.ResponseWithHttpCodeData(c, http.StatusBadRequest, 400, nil, "Provider name is required")
@@ -534,29 +536,43 @@ func (h *ProviderHandler) EnableOrDisableModel(c *gin.Context) {
return
}
var req EnableOrDisableModelRequest
var req AlterModelRequest
if err := c.ShouldBindJSON(&req); err != nil {
println("JSON bind error: %v (type: %T)", err, err)
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
return
}
userID := c.GetString("user_id")
modelID := strings.TrimSpace(req.ModelID)
modelName := strings.TrimPrefix(c.Param("model_name"), "/")
modelName = strings.TrimSpace(modelName)
modelID := strings.TrimSpace(req.ModelID)
if modelName == "" && modelID == "" {
common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "model_name or model_id is required")
return
}
status := strings.TrimSpace(req.Status)
if status != "active" && status != "inactive" {
common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "Status must be active or inactive")
if status != "" && status != "active" && status != "inactive" {
common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "status must be 'active' or 'inactive'")
return
}
code, err := h.modelProviderService.UpdateModelStatus(providerName, instanceName, modelName, userID, modelID, status)
updateDict := make(map[string]interface{})
if status != "" {
updateDict["status"] = status
}
if req.MaxTokens > 0 {
updateDict["max_tokens"] = req.MaxTokens
}
if req.ModelType != nil {
updateDict["model_type"] = req.ModelType
}
if req.Extra != nil {
updateDict["extra"] = req.Extra
}
code, err := h.modelProviderService.AlterModel(providerName, instanceName, modelName, userID, modelID, updateDict)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return
@@ -585,30 +601,6 @@ func prepareProviderInstance(providerName, instanceName, reqProviderName, reqIns
return nil
}
func prepareAddModelRequest(req *service.AddModelRequest, providerName, instanceName string) error {
if err := prepareProviderInstance(providerName, instanceName, req.ProviderName, req.InstanceName); err != nil {
return err
}
if len(req.Models) == 0 {
return errors.New("Models are required")
}
for _, model := range req.Models {
if model.ModelName == "" {
return errors.New("Model name is required")
}
if len(model.ModelTypes) == 0 {
return errors.New("Model type is required")
}
}
req.ProviderName = providerName
req.InstanceName = instanceName
return nil
}
func (h *ProviderHandler) AddModel(c *gin.Context) {
var req service.AddModelRequest
if err := c.ShouldBindJSON(&req); err != nil {
@@ -617,8 +609,21 @@ func (h *ProviderHandler) AddModel(c *gin.Context) {
return
}
if err := prepareAddModelRequest(&req, c.Param("provider_name"), c.Param("instance_name")); err != nil {
common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeDataError, nil, err.Error())
req.ProviderName = c.Param("provider_name")
req.InstanceName = c.Param("instance_name")
if req.ProviderName == "" || req.InstanceName == "" {
common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "provider_name and instance_name are required")
return
}
if req.ModelName == "" {
common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "model_name is required")
return
}
if len(req.ModelTypes) == 0 {
common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "model_type is required")
return
}
@@ -634,8 +639,7 @@ func (h *ProviderHandler) AddModel(c *gin.Context) {
}
type DropInstanceModelRequest struct {
ModelIDs []string `json:"model_ids"`
Models []string `json:"models"`
ModelNames []string `json:"model_name"`
}
func (h *ProviderHandler) DropInstanceModels(c *gin.Context) {
@@ -655,14 +659,14 @@ func (h *ProviderHandler) DropInstanceModels(c *gin.Context) {
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
return
}
if len(req.ModelIDs) == 0 && len(req.Models) == 0 {
common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "model_ids or models is required")
if len(req.ModelNames) == 0 {
common.ResponseWithHttpCodeData(c, http.StatusBadRequest, common.CodeBadRequest, nil, "model_name is required")
return
}
userID := c.GetString("user_id")
code, err := h.modelProviderService.DropInstanceModels(providerName, instanceName, userID, req.ModelIDs, req.Models)
code, err := h.modelProviderService.DropInstanceModels(providerName, instanceName, userID, req.ModelNames)
if err != nil {
common.ErrorWithCode(c, code, err.Error())
return

View File

@@ -84,7 +84,7 @@ func decodeProviderHandlerResponse(t *testing.T, recorder *httptest.ResponseReco
return body
}
func TestProviderHandlerEnableOrDisableModelRejectsMissingModelSelector(t *testing.T) {
func TestProviderHandlerAlterModelRejectsMissingModelSelector(t *testing.T) {
ctx, recorder := newProviderHandlerRequest(
t,
map[string]interface{}{"status": "active"},
@@ -92,7 +92,7 @@ func TestProviderHandlerEnableOrDisableModelRejectsMissingModelSelector(t *testi
gin.Param{Key: "instance_name", Value: "default"},
)
NewProviderHandler(nil, service.NewModelProviderService()).EnableOrDisableModel(ctx)
NewProviderHandler(nil, service.NewModelProviderService()).AlterModel(ctx)
if recorder.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d; body=%s", recorder.Code, http.StatusBadRequest, recorder.Body.String())
@@ -103,7 +103,7 @@ func TestProviderHandlerEnableOrDisableModelRejectsMissingModelSelector(t *testi
}
}
func TestProviderHandlerEnableOrDisableModelRejectsInvalidStatus(t *testing.T) {
func TestProviderHandlerAlterModelRejectsInvalidStatus(t *testing.T) {
ctx, recorder := newProviderHandlerRequest(
t,
map[string]interface{}{"status": "disabled"},
@@ -112,7 +112,7 @@ func TestProviderHandlerEnableOrDisableModelRejectsInvalidStatus(t *testing.T) {
gin.Param{Key: "model_name", Value: "gpt-test"},
)
NewProviderHandler(nil, service.NewModelProviderService()).EnableOrDisableModel(ctx)
NewProviderHandler(nil, service.NewModelProviderService()).AlterModel(ctx)
if recorder.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d; body=%s", recorder.Code, http.StatusBadRequest, recorder.Body.String())
@@ -123,7 +123,7 @@ func TestProviderHandlerEnableOrDisableModelRejectsInvalidStatus(t *testing.T) {
}
}
func TestProviderHandlerEnableOrDisableModelUpdatesStatus(t *testing.T) {
func TestProviderHandlerAlterModelUpdatesStatus(t *testing.T) {
db := setupProviderHandlerTestDB(t)
useProviderHandlerTestDB(t, db)
seedProviderHandlerModel(t, db)
@@ -136,7 +136,7 @@ func TestProviderHandlerEnableOrDisableModelUpdatesStatus(t *testing.T) {
gin.Param{Key: "model_name", Value: "gpt-test"},
)
NewProviderHandler(nil, service.NewModelProviderService()).EnableOrDisableModel(ctx)
NewProviderHandler(nil, service.NewModelProviderService()).AlterModel(ctx)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d, want %d; body=%s", recorder.Code, http.StatusOK, recorder.Body.String())