feat(models): support custom HTTP headers across all remote model calls
Introduce per-model `custom_headers` config (similar to OpenAI Python SDK's `extra_headers`) so users can inject gateway auth tokens, trace IDs, etc. into every outbound API request for chat / embedding / rerank / VLLM / ASR models. Reserved headers like Authorization / Content-Type are always preserved to avoid breaking auth or signing flows. Along the way, unify production and "test connection" paths onto a single `ConfigFromModel(*types.Model, appID, appSecret)` constructor in each model package. Both `service.modelService.GetXxxModel` and the four `handler.initialization.Check*Model` / `TestEmbeddingModel` endpoints now go through the same field mapping, so new parameters only need to be added in one place. Backend: - Add `CustomHeaders map[string]string` to `types.ModelParameters` with JSON/YAML `omitempty` tags for forward compatibility. - New `internal/utils/extraheaders.go` providing `ApplyCustomHeaders` (for hand-rolled HTTP paths) and `WrapHTTPClientWithHeaders` / `CustomHeadersRoundTripper` (for SDK paths like go-openai). Reserved headers (Authorization, api-key, Content-Type, Accept, Host, Content-Length, User-Agent) are filtered out. - Inject headers in every remote model path: chat (SDK + raw HTTP), embedding (openai/aliyun/jina/volcengine/nvidia/azure_openai), rerank (remote_api/aliyun/jina/nvidia/zhipu — via shared `customHeaderSetter` interface), VLM and ASR (via http.Client wrapping). - Add `ConfigFromModel` to each of chat/embedding/rerank/vlm/asr, consolidating field mapping (ExtraConfig, CustomHeaders, InterfaceType defaulting, WeKnoraCloud credentials, etc.) and cover it with dedicated unit tests per package. - Refactor `service/model.go`: replace ~100 lines of hand-written Config literals with one-line `ConfigFromModel` calls; drop the now-unused `stringMapToAnyMap` helper. - Refactor `handler/initialization.go` test-connection endpoints: merge four ad-hoc request structs into one `ModelTestRequest`, extract `buildTestModel` and `resolveTenantWeKnoraCloudCreds` helpers, and route all four endpoints through `ConfigFromModel` + `NewXxx` so the test path is now behaviorally identical to production. Frontend: - Add `custom_headers` to the `ModelConfig` API type. - `ModelEditorDialog.vue`: new Key-Value header editor (add/remove rows), auto-converts map <-> array when loading/saving, and passes the current header set to the `Test Connection` button so the preview reflects exactly what production will send. - `ModelSettings.vue`: serialize the header array back into a map for the backend (drops empty rows). - i18n: add custom-header labels / descriptions / placeholders to zh-CN, en-US, ru-RU, ko-KR. Tests: - `internal/utils/extraheaders_test.go` covers reserved-header filtering and round-tripper wrapping (including nil-client fallback). - New `config_from_model_test.go` in chat / embedding / rerank / vlm / asr verifies all fields (CustomHeaders, ExtraConfig, InterfaceType defaulting, AppID/AppSecret) are propagated end-to-end.
This commit is contained in:
@@ -350,7 +350,11 @@ function convertToLegacyFormat(model: ModelConfig) {
|
||||
provider: model.parameters.provider || '', // 添加 provider 字段
|
||||
dimension: model.parameters.embedding_parameters?.dimension,
|
||||
isBuiltin: model.is_builtin || false,
|
||||
supportsVision: model.parameters.supports_vision || false
|
||||
supportsVision: model.parameters.supports_vision || false,
|
||||
// 将后端 map 形式转换为前端可编辑的数组形式
|
||||
customHeaders: model.parameters.custom_headers
|
||||
? Object.entries(model.parameters.custom_headers).map(([key, value]) => ({ key, value: String(value) }))
|
||||
: []
|
||||
}
|
||||
}
|
||||
|
||||
@@ -426,6 +430,18 @@ const handleModelSave = async (modelData: any) => {
|
||||
}
|
||||
}
|
||||
|
||||
// 将前端 Key-Value 数组形式的自定义 Header 转换成后端期望的 map
|
||||
const customHeadersMap: Record<string, string> = {}
|
||||
if (Array.isArray(modelData.customHeaders)) {
|
||||
for (const item of modelData.customHeaders) {
|
||||
const key = (item?.key ?? '').trim()
|
||||
const value = (item?.value ?? '').trim()
|
||||
if (key && value) {
|
||||
customHeadersMap[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 将前端格式转换为后端格式
|
||||
const apiModelData: ModelConfig = {
|
||||
name: modelData.modelName.trim(), // 使用 modelName 作为 name,并去除首尾空格
|
||||
@@ -436,6 +452,7 @@ const handleModelSave = async (modelData: any) => {
|
||||
base_url: modelData.baseUrl?.trim() || '',
|
||||
api_key: modelData.apiKey?.trim() || '',
|
||||
provider: modelData.provider || '', // 添加 provider 字段
|
||||
...(Object.keys(customHeadersMap).length > 0 ? { custom_headers: customHeadersMap } : {}),
|
||||
...(currentModelType.value === 'embedding' && modelData.dimension ? {
|
||||
embedding_parameters: {
|
||||
dimension: modelData.dimension,
|
||||
|
||||
Reference in New Issue
Block a user