fix(llm): 测试连接补发自定义会话头,修 opencode zen 400

testLLM 单发路径既没往 context 挂 session id,也没把 profile 的
session_header_key 透传给 cfg,导致强制要求 x-opencode-session 的端点
(opencode zen)在"对话正常"时"点击测试"却 400 MissingSessionID。

- provider.go: TestConnection 挂一个一次性随机 session id 作头值
- server.go: testLLM 透传 session_header_key,并从已存 profile 兜底
- web: api.ts / llm page 把该字段传到 /llm/test
This commit is contained in:
Autumn-27 committed 2026-09-11 10:45:27 -04:00
1 parent d86dd152c3
commit 399f971f41
4 files changed
+31 -11

No files matched your search

+22 -11
View File
@@ -1393,15 +1393,16 @@ func (s *Server) setLLM(w http.ResponseWriter, r *http.Request) {
// testLLM makes a real minimal completion to verify the config works.
func (s *Server) testLLM(w http.ResponseWriter, r *http.Request) {
var req struct {
Provider string `json:"provider"`
Model string `json:"model"`
BaseURL string `json:"base_url"`
Proxy string `json:"proxy"`
APIKey string `json:"api_key"`
ThinkingType string `json:"thinking_type"`
ReasoningEffort string `json:"reasoning_effort"`
ProfileID *int64 `json:"profile_id"` // 测已存 profile 时传入:api_key 为空则用它存的 key
Streaming *bool `json:"streaming"` // 省略=流式,与保存 profile 时同一套默认
Provider string `json:"provider"`
Model string `json:"model"`
BaseURL string `json:"base_url"`
Proxy string `json:"proxy"`
APIKey string `json:"api_key"`
ThinkingType string `json:"thinking_type"`
ReasoningEffort string `json:"reasoning_effort"`
ProfileID *int64 `json:"profile_id"` // 测已存 profile 时传入:api_key 为空则用它存的 key
Streaming *bool `json:"streaming"` // 省略=流式,与保存 profile 时同一套默认
SessionHeaderKey string `json:"session_header_key"` // 非空=测试时也带该自定义会话头,值为一次性随机 session id
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeErr(w, 400, err.Error())
@@ -1417,11 +1418,21 @@ func (s *Server) testLLM(w http.ResponseWriter, r *http.Request) {
if req.Streaming != nil {
cfg.Stream = *req.Streaming
}
// 自定义会话头名照该配置来:非空则测试请求也发这个头(值为一次性随机 session id,
// 见 TestConnection)。opencode zen 等强制要求 x-opencode-session 的端点,缺了它
// 直接 400,必须在测试路径上也带上,否则"对话通、测试 400"。
cfg.SessionHeaderKey = req.SessionHeaderKey
// API Key 解析优先级:表单输入 > 指定 profile 存的 key > 全局配置的 key。
// 已存 profile 的 key 不回传浏览器,所以测试已存配置时表单为空,需从 DB 取。
if cfg.APIKey == "" && req.ProfileID != nil {
// 会话头名同理:表单未带时用已存 profile 的值兜底。
if req.ProfileID != nil && (cfg.APIKey == "" || cfg.SessionHeaderKey == "") {
if p, err := s.m.pg.ProfileByID(*req.ProfileID); err == nil && p != nil {
cfg.APIKey = p.APIKey
if cfg.APIKey == "" {
cfg.APIKey = p.APIKey
}
if cfg.SessionHeaderKey == "" {
cfg.SessionHeaderKey = p.SessionHeaderKey
}
}
}
if cfg.APIKey == "" {