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:
@@ -284,6 +284,9 @@ export function checkRemoteModel(modelConfig: {
|
||||
baseUrl: string;
|
||||
apiKey?: string;
|
||||
provider?: string;
|
||||
// 编辑已存在模型时传 modelId,后端会自动从存储中带出 apiKey
|
||||
// (前端不再回显明文密钥,所以测试连接必须用这个回填路径)
|
||||
modelId?: string;
|
||||
} & BaseModelTestPayload): Promise<{
|
||||
available: boolean;
|
||||
message?: string;
|
||||
@@ -308,6 +311,7 @@ export function testEmbeddingModel(modelConfig: {
|
||||
apiKey?: string;
|
||||
dimension?: number;
|
||||
provider?: string;
|
||||
modelId?: string;
|
||||
} & BaseModelTestPayload): Promise<{ available: boolean; message?: string; dimension?: number }> {
|
||||
return new Promise((resolve, reject) => {
|
||||
post('/api/v1/initialization/embedding/test', modelConfig)
|
||||
@@ -327,6 +331,7 @@ export function checkRerankModel(modelConfig: {
|
||||
baseUrl: string;
|
||||
apiKey?: string;
|
||||
provider?: string;
|
||||
modelId?: string;
|
||||
} & BaseModelTestPayload): Promise<{
|
||||
available: boolean;
|
||||
message?: string;
|
||||
@@ -349,6 +354,7 @@ export function checkASRModel(modelConfig: {
|
||||
baseUrl: string;
|
||||
apiKey?: string;
|
||||
provider?: string;
|
||||
modelId?: string;
|
||||
} & BaseModelTestPayload): Promise<{
|
||||
available: boolean;
|
||||
message?: string;
|
||||
|
||||
@@ -951,6 +951,13 @@ const checkRemoteAPI = async () => {
|
||||
: {}
|
||||
|
||||
// 根据模型类型调用不同的校验接口
|
||||
// 编辑模式下 apiKey 由 <CredentialResource> 独立管理、不在 formData 里。
|
||||
// 把 modelId 透传给后端,让它在 apiKey 为空时自动用存储的解密值兜底,
|
||||
// 避免出现"测试连接没带 apiKey 直接失败"的情况。
|
||||
const idPayload = isEdit.value && props.modelData?.id
|
||||
? { modelId: props.modelData.id as string }
|
||||
: {}
|
||||
|
||||
switch (props.modelType) {
|
||||
case 'chat':
|
||||
// 对话模型(KnowledgeQA)
|
||||
@@ -959,6 +966,7 @@ const checkRemoteAPI = async () => {
|
||||
baseUrl: formData.value.baseUrl,
|
||||
apiKey: formData.value.apiKey || '',
|
||||
provider: formData.value.provider,
|
||||
...idPayload,
|
||||
...headerPayload,
|
||||
})
|
||||
break
|
||||
@@ -972,6 +980,7 @@ const checkRemoteAPI = async () => {
|
||||
apiKey: formData.value.apiKey || '',
|
||||
dimension: formData.value.dimension,
|
||||
provider: formData.value.provider,
|
||||
...idPayload,
|
||||
...headerPayload,
|
||||
})
|
||||
// 如果测试成功且返回了维度,自动填充
|
||||
@@ -988,6 +997,7 @@ const checkRemoteAPI = async () => {
|
||||
baseUrl: formData.value.baseUrl,
|
||||
apiKey: formData.value.apiKey || '',
|
||||
provider: formData.value.provider,
|
||||
...idPayload,
|
||||
...headerPayload,
|
||||
})
|
||||
break
|
||||
@@ -1000,6 +1010,7 @@ const checkRemoteAPI = async () => {
|
||||
baseUrl: formData.value.baseUrl,
|
||||
apiKey: formData.value.apiKey || '',
|
||||
provider: formData.value.provider,
|
||||
...idPayload,
|
||||
...headerPayload,
|
||||
})
|
||||
break
|
||||
@@ -1011,6 +1022,7 @@ const checkRemoteAPI = async () => {
|
||||
baseUrl: formData.value.baseUrl,
|
||||
apiKey: formData.value.apiKey || '',
|
||||
provider: formData.value.provider,
|
||||
...idPayload,
|
||||
...headerPayload,
|
||||
})
|
||||
break
|
||||
|
||||
@@ -658,7 +658,7 @@ const stepTitles = computed(() => [
|
||||
t('datasource.prereqMemberBrief')) }}</div>
|
||||
<div class="ds-prereq-item-desc">{{ t(`datasource.prereqStep3Desc_${form.type}`,
|
||||
t('datasource.prereqMemberDesc'))
|
||||
}}</div>
|
||||
}}</div>
|
||||
</div>
|
||||
</div>
|
||||
<a :href="currentDef.permissionPageUrl" target="_blank" rel="noopener" class="doc-link ds-prereq-link">
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user