Files
sub2api/backend/internal/service/openai_apikey_responses_probe_test.go
T
alfadb b88f8e4c04 fix(openai-probe): /responses 能力探测增加工具调用校验
原探测仅以 HTTP 状态码判定 /v1/responses 端点是否存在(404/405 视为不存在,其余视为存在),无法识别"端点存在、基础补全可用、但工具调用不可用"的上游。火山方舟 coding/v3 的 kimi-k2.6 即属此类:携带 tools 的请求仅返回 reasoning、不产出 function_call,导致账号被误判 openai_responses_supported=true。网关据此走 /responses 转换路径后工具调用结果丢失,带工具的请求确定性失败。

改动:
- 探测请求携带一个工具并以 tool_choice=required 强制调用,仅当响应 output 数组包含 function_call 项时判定为支持;2xx 但无 function_call 判定为不支持,使网关改走 /v1/chat/completions 直转路径(同一模型在该路径下工具调用正常)。
- 探测模型改用账号 model_mapping 中的真实上游模型;占位模型在第三方上游会返回 400 model-not-found,无法判定能力。
- 探测超时 8s 调整为 15s(探测在后台异步执行,为推理型模型先推理再产出工具调用预留时间)。
- 非 2xx(404/405 除外)仍保守判定为支持,不改变既有账号行为;运行时回退函数 isResponsesEndpointSupportedByStatus 保持不变。
- 新增单元测试覆盖判定逻辑与探测模型选择。
2026-06-16 15:59:00 +08:00

79 lines
2.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package service
import (
"testing"
"github.com/Wei-Shaw/sub2api/internal/pkg/openai"
"github.com/stretchr/testify/require"
)
func TestDecideResponsesProbeSupport(t *testing.T) {
fnCall := []byte(`{"output":[{"type":"reasoning"},{"type":"function_call","name":"probe_ping"}]}`)
reasoningOnly := []byte(`{"output":[{"type":"reasoning"}]}`)
cases := []struct {
name string
status int
body []byte
want bool
}{
// Endpoint clearly absent on third-party OpenAI-compatible upstreams.
{"404 endpoint absent", 404, fnCall, false},
{"405 method not allowed", 405, fnCall, false},
// 2xx: tool capability is judged by presence of a function_call output item.
{"200 with function_call", 200, fnCall, true},
// Volcengine Ark coding/v3 × kimi-k2.6: reasoning only, no function_call.
{"200 reasoning only", 200, reasoningOnly, false},
{"200 invalid json", 200, []byte("not-json"), false},
{"200 no output field", 200, []byte(`{"status":"completed"}`), false},
// Non-2xx (other than 404/405): endpoint exists, capability undecidable -> conservative true.
{"400 conservative true", 400, reasoningOnly, true},
{"401 conservative true", 401, nil, true},
{"500 conservative true", 500, nil, true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.want, decideResponsesProbeSupport(tc.status, tc.body))
})
}
}
func TestResponsesProbeBodyHasFunctionCall(t *testing.T) {
require.True(t, responsesProbeBodyHasFunctionCall([]byte(`{"output":[{"type":"function_call"}]}`)))
require.True(t, responsesProbeBodyHasFunctionCall([]byte(`{"output":[{"type":"reasoning"},{"type":"function_call"}]}`)))
require.False(t, responsesProbeBodyHasFunctionCall([]byte(`{"output":[{"type":"reasoning"}]}`)))
require.False(t, responsesProbeBodyHasFunctionCall([]byte(`{"output":[]}`)))
require.False(t, responsesProbeBodyHasFunctionCall([]byte(`{}`)))
require.False(t, responsesProbeBodyHasFunctionCall([]byte(`garbage`)))
}
func TestSelectResponsesProbeModel(t *testing.T) {
// No model_mapping -> fall back to DefaultTestModel (OpenAI official APIKey).
require.Equal(t, openai.DefaultTestModel, selectResponsesProbeModel(&Account{}))
// model_mapping values are upstream models; pick first by sort for reproducibility.
acct := &Account{Credentials: map[string]any{
"model_mapping": map[string]any{
"client-b": "zeta-model",
"client-a": "alpha-model",
},
}}
require.Equal(t, "alpha-model", selectResponsesProbeModel(acct))
// Wildcard / blank upstream values are skipped.
acctWild := &Account{Credentials: map[string]any{
"model_mapping": map[string]any{
"a": "*",
"b": " ",
"c": "real-model",
},
}}
require.Equal(t, "real-model", selectResponsesProbeModel(acctWild))
// Only wildcard mappings -> DefaultTestModel.
acctAllWild := &Account{Credentials: map[string]any{
"model_mapping": map[string]any{"a": "gpt-*"},
}}
require.Equal(t, openai.DefaultTestModel, selectResponsesProbeModel(acctAllWild))
}