fix(initialization): fall back to stored apiKey when test-connecting an existing model

Direct regression from the previous credential subresource refactor: the
edit-mode ModelEditorDialog stopped binding apiKey into formData (the
field now lives in <CredentialResource>), so "Test connection" sent
apiKey="" and every upstream call (DeepSeek / lkeap / OpenAI etc.) 401'd.

Fix in the backend so the frontend stays simple:

- ModelTestRequest gains an optional ModelID field.
- New fillSecretsFromStoredModel helper: when ModelID is set AND a secret
  field on the request is empty, copy it from the stored (decrypted) model.
  Non-empty request values still win, so users typing a new key to verify
  it before saving behave the same as before.
- The four test entrypoints (chat / embedding / rerank / asr) call the
  helper right after ShouldBindJSON.

Frontend: ModelEditorDialog.testConnection forwards modelId when isEdit
is true. The four API client signatures accept the optional field.

WebSearch and MCP are unaffected: WebSearch already has a TestProviderByID
ID-based path, MCP has no standalone raw test endpoint.
This commit is contained in:
wizardchen
2026-05-17 14:24:12 +08:00
committed by lyingbug
parent 7643dd457e
commit a9d37970ae
4 changed files with 60 additions and 1 deletions
+41
View File
@@ -1537,6 +1537,43 @@ type ModelTestRequest struct {
Dimension int `json:"dimension,omitempty"`
CustomHeaders map[string]string `json:"customHeaders,omitempty"`
ExtraConfig map[string]string `json:"extraConfig,omitempty"`
// ModelID, when set, instructs the handler to substitute any missing
// secrets (APIKey, AppSecret via ExtraConfig) from the stored model
// record before assembling the test client. This lets the "Test
// connection" button work on existing models without making the
// frontend reload — and ship — the plaintext API key. Other fields
// (BaseURL, ModelName, etc.) on this request still override the
// stored values, so a user can validate a new endpoint against the
// existing credentials in one click.
ModelID string `json:"modelId,omitempty"`
}
// fillSecretsFromStoredModel mutates req in place: if req.ModelID is set
// and a secret field on the request is empty, the corresponding value from
// the stored (and decrypted) model is copied in. Non-empty request values
// are always preferred — they represent the user actively typing a new key
// they want to verify. Missing or inaccessible model is treated as a no-op
// (the connection test will fail downstream with a clearer "missing apiKey"
// error than we could produce here).
func (h *InitializationHandler) fillSecretsFromStoredModel(ctx context.Context, req *ModelTestRequest) {
if req == nil || req.ModelID == "" {
return
}
if req.APIKey != "" {
// Already supplied — nothing to merge. (We don't need to look up
// AppSecret separately since the WeKnoraCloud path resolves it
// from the tenant, not the model record.)
return
}
stored, err := h.modelService.GetModelByID(ctx, req.ModelID)
if err != nil || stored == nil {
logger.Warnf(ctx, "test-connection: stored model %s not found, leaving secrets empty: %v",
utils.SanitizeForLog(req.ModelID), err)
return
}
if req.APIKey == "" {
req.APIKey = stored.Parameters.APIKey
}
}
// RemoteModelCheckRequest 兼容旧 swagger 定义。
@@ -1612,6 +1649,7 @@ func (h *InitializationHandler) CheckRemoteModel(c *gin.Context) {
c.Error(errors.NewBadRequestError(err.Error()))
return
}
h.fillSecretsFromStoredModel(ctx, &req)
if req.ModelName == "" || req.BaseURL == "" {
logger.Error(ctx, "Model name and base URL are required")
@@ -1668,6 +1706,7 @@ func (h *InitializationHandler) TestEmbeddingModel(c *gin.Context) {
c.Error(errors.NewBadRequestError(err.Error()))
return
}
h.fillSecretsFromStoredModel(ctx, &req)
if req.Source == "" {
req.Source = string(types.ModelSourceRemote)
}
@@ -1817,6 +1856,7 @@ func (h *InitializationHandler) CheckRerankModel(c *gin.Context) {
c.Error(errors.NewBadRequestError(err.Error()))
return
}
h.fillSecretsFromStoredModel(ctx, &req)
if req.ModelName == "" || req.BaseURL == "" {
logger.Error(ctx, "Model name and base URL are required")
@@ -1874,6 +1914,7 @@ func (h *InitializationHandler) CheckASRModel(c *gin.Context) {
c.Error(errors.NewBadRequestError(err.Error()))
return
}
h.fillSecretsFromStoredModel(ctx, &req)
if req.ModelName == "" || req.BaseURL == "" {
logger.Error(ctx, "Model name and base URL are required for ASR check")