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:
wizardchen
2026-04-23 23:13:10 +08:00
committed by lyingbug
parent a6d4266c93
commit fd3e2992b1
37 changed files with 1105 additions and 334 deletions
+15 -4
View File
@@ -250,13 +250,22 @@ export function getCurrentConfigByKB(kbId: string): Promise<InitializationConfig
});
}
// 所有"测试连接"接口共用的通用可选参数。
// customHeaders / extraConfig / interfaceType 对应后端 ModelTestRequest 里的同名字段,
// 会被透传给真正的模型装配流程,保证测试连接与生产调用走完全相同的路径。
interface BaseModelTestPayload {
customHeaders?: Record<string, string>;
extraConfig?: Record<string, string>;
interfaceType?: string;
}
// 检查远程API模型
export function checkRemoteModel(modelConfig: {
modelName: string;
baseUrl: string;
apiKey?: string;
provider?: string;
}): Promise<{
} & BaseModelTestPayload): Promise<{
available: boolean;
message?: string;
}> {
@@ -280,7 +289,7 @@ export function testEmbeddingModel(modelConfig: {
apiKey?: string;
dimension?: number;
provider?: string;
}): Promise<{ available: boolean; message?: string; dimension?: number }> {
} & BaseModelTestPayload): Promise<{ available: boolean; message?: string; dimension?: number }> {
return new Promise((resolve, reject) => {
post('/api/v1/initialization/embedding/test', modelConfig)
.then((response: any) => {
@@ -298,7 +307,8 @@ export function checkRerankModel(modelConfig: {
modelName: string;
baseUrl: string;
apiKey?: string;
}): Promise<{
provider?: string;
} & BaseModelTestPayload): Promise<{
available: boolean;
message?: string;
}> {
@@ -319,7 +329,8 @@ export function checkASRModel(modelConfig: {
modelName: string;
baseUrl: string;
apiKey?: string;
}): Promise<{
provider?: string;
} & BaseModelTestPayload): Promise<{
available: boolean;
message?: string;
}> {