mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-12-01 12:43:30 +01:00
fix(pi): make store omission an explicit channel opt-in
This commit is contained in:
1 parent
6d4a5c7e53
commit
5c2402c983
3 files changed
+107
-59
No files matched your search
@@ -129,6 +129,8 @@ ai:
|
||||
allow_client_reasoning: true # false 时忽略对话请求体 reasoning,仅以下方为准
|
||||
profile: openai_compat # auto | deepseek_compat | openai_compat | output_config_effort
|
||||
# extra_request_fields: {} # 可选:管理员自定义根级 JSON 片段(高级)
|
||||
# 仅当 Chat Completions 网关明确拒绝 store 字段时,设置 {store: null} 省略该字段。
|
||||
# 未配置时保留 Pi 默认行为;store: false/true 仍原样发送。
|
||||
# qwen-plus:
|
||||
# name: Qwen Plus Fallback
|
||||
# provider: openai_compatible
|
||||
|
||||
@@ -14,63 +14,112 @@ import (
|
||||
)
|
||||
|
||||
func TestPiGatewayStoreCompatibility(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, base, provider string
|
||||
omit bool
|
||||
}{
|
||||
{"default OpenAI", "", "openai", false},
|
||||
{"official OpenAI", "https://api.openai.com/v1", "openai", false},
|
||||
{"case insensitive host", "https://API.OPENAI.COM/v1", "openai", false},
|
||||
{"custom gateway", "https://gateway.example/v1", "openai_compatible", true},
|
||||
{"misleading hostname", "https://api.openai.com.example/v1", "openai", true},
|
||||
{"local endpoint", "http://localhost:8000/v1", "openai", true},
|
||||
{"DeepSeek", "https://api.deepseek.com/v1", "openai", true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
model := piModelConfig(config.OpenAIConfig{BaseURL: tc.base, Provider: tc.provider, Model: "fixture"}, nil, "")
|
||||
value, exists := model.Compat["supportsStore"]
|
||||
if tc.omit && (!exists || value != false) {
|
||||
t.Fatalf("expected store omission: %v", model.Compat)
|
||||
}
|
||||
if !tc.omit && exists {
|
||||
t.Fatalf("official default changed: %v", model.Compat)
|
||||
}
|
||||
})
|
||||
for _, base := range []string{"", "https://api.openai.com/v1", "https://gateway.example/v1"} {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
fields map[string]interface{}
|
||||
omit bool
|
||||
}{
|
||||
{"default", nil, false},
|
||||
{"explicit false", map[string]interface{}{"store": false}, false},
|
||||
{"explicit true", map[string]interface{}{"store": true}, false},
|
||||
{"explicit omission", map[string]interface{}{"store": nil}, true},
|
||||
} {
|
||||
t.Run(base+"/"+tc.name, func(t *testing.T) {
|
||||
oa := config.OpenAIConfig{BaseURL: base, Provider: "openai_compatible", Model: "fixture", Reasoning: config.OpenAIReasoningConfig{ExtraRequestFields: tc.fields}}
|
||||
model := piModelConfig(oa, nil, "")
|
||||
value, exists := model.Compat["supportsStore"]
|
||||
if tc.omit {
|
||||
if !exists || value != false {
|
||||
t.Fatalf("expected omission: %v", model.Compat)
|
||||
}
|
||||
if _, exists := model.ExtraFields["store"]; exists {
|
||||
t.Fatal("null must not reach provider")
|
||||
}
|
||||
if _, exists := oa.Reasoning.ExtraRequestFields["store"]; !exists {
|
||||
t.Fatal("channel config mutated")
|
||||
}
|
||||
} else {
|
||||
if exists {
|
||||
t.Fatalf("default compatibility changed: %v", model.Compat)
|
||||
}
|
||||
if got, exists := model.ExtraFields["store"]; exists != (tc.fields != nil) || got != tc.fields["store"] {
|
||||
t.Fatalf("explicit field changed: %v", model.ExtraFields)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This fixture validates the real Go -> Pi worker -> HTTP path, not just the
|
||||
// compatibility map. It models a strict gateway; it is not a live vendor test.
|
||||
func TestPiStrictGatewayOmitsStore(t *testing.T) {
|
||||
cfg := piTestConfig(t)
|
||||
cfg.OpenAI.Model = "deepseek:fixture"
|
||||
cfg.OpenAI.Provider = "openai_compatible"
|
||||
var calls atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls.Add(1)
|
||||
var body map[string]any
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Error(err)
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
func TestPiGatewayStoreHTTP(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
fields map[string]interface{}
|
||||
want any
|
||||
present bool
|
||||
}{
|
||||
{"default", nil, false, true},
|
||||
{"explicit false", map[string]interface{}{"store": false}, false, true},
|
||||
{"explicit true", map[string]interface{}{"store": true}, true, true},
|
||||
{"strict gateway omission", map[string]interface{}{"store": nil}, nil, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cfg := piTestConfig(t)
|
||||
cfg.OpenAI.Model = "deepseek:fixture"
|
||||
cfg.OpenAI.Provider = "openai_compatible"
|
||||
cfg.OpenAI.Reasoning.ExtraRequestFields = tc.fields
|
||||
var calls atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls.Add(1)
|
||||
var body map[string]any
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Error(err)
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
if value, present := body["store"]; present != tc.present || value != tc.want {
|
||||
t.Errorf("store=%v present=%v, want %v present=%v", value, present, tc.want, tc.present)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(400)
|
||||
_, _ = w.Write([]byte(`{"error":{"message":"request field is not supported by provider"}}`))
|
||||
return
|
||||
}
|
||||
if body["stream"] != true || body["stream_options"] == nil || body["tools"] == nil {
|
||||
t.Errorf("streaming, usage or tools lost: %v", body)
|
||||
}
|
||||
piReply(w, "", "", nil, "ok")
|
||||
}))
|
||||
defer server.Close()
|
||||
cfg.OpenAI.BaseURL = server.URL + "/v1"
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
result, err := RunSingleAgent(ctx, cfg, &cfg.MultiAgent, piTestAgent(cfg, nil), nil, zap.NewNop(), "", "", "Say ok", nil, nil, nil, nil, "")
|
||||
if err != nil || result == nil || result.Response != "ok" || result.RuntimeStatus != "completed" || calls.Load() != 1 {
|
||||
t.Fatalf("result=%+v err=%v calls=%d", result, err, calls.Load())
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPiStoreOmissionDoesNotChangeOtherAPIs(t *testing.T) {
|
||||
for _, oa := range []config.OpenAIConfig{
|
||||
{Provider: "claude", BaseURL: "https://api.anthropic.com", Model: "fixture"},
|
||||
{Provider: "openai", BaseURL: "https://api.openai.com/v1", Model: "gpt-6-astra"},
|
||||
} {
|
||||
oa.Reasoning.ExtraRequestFields = map[string]interface{}{"store": nil}
|
||||
model := piModelConfig(oa, nil, "")
|
||||
if model.API == "openai-completions" {
|
||||
t.Fatal("fixture did not select other API")
|
||||
}
|
||||
if _, present := body["store"]; present {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(400)
|
||||
_, _ = w.Write([]byte(`{"error":{"message":"request field is not supported by provider"}}`))
|
||||
return
|
||||
if _, ok := model.Compat["supportsStore"]; ok {
|
||||
t.Fatalf("other API modified: %v", model)
|
||||
}
|
||||
if body["stream"] != true || body["stream_options"] == nil || body["tools"] == nil {
|
||||
t.Errorf("streaming, usage or tools lost: %v", body)
|
||||
if value, ok := model.ExtraFields["store"]; !ok || value != nil {
|
||||
t.Fatal("other API extension changed")
|
||||
}
|
||||
piReply(w, "", "", nil, "ok")
|
||||
}))
|
||||
defer server.Close()
|
||||
cfg.OpenAI.BaseURL = server.URL + "/v1"
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
result, err := RunSingleAgent(ctx, cfg, &cfg.MultiAgent, piTestAgent(cfg, nil), nil, zap.NewNop(), "", "", "Say ok", nil, nil, nil, nil, "")
|
||||
if err != nil || result == nil || result.Response != "ok" || result.RuntimeStatus != "completed" || calls.Load() != 1 {
|
||||
t.Fatalf("result=%+v err=%v calls=%d", result, err, calls.Load())
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"cyberstrike-ai/internal/agentruntime"
|
||||
"cyberstrike-ai/internal/config"
|
||||
"cyberstrike-ai/internal/reasoning"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -41,14 +40,12 @@ func piModelConfig(oa config.OpenAIConfig, intent *reasoning.ClientIntent, conve
|
||||
extra = oa.Reasoning.ExtraRequestFields
|
||||
}
|
||||
compat := map[string]any{}
|
||||
if api == "openai-completions" {
|
||||
// Pi assumes unknown gateways support OpenAI's optional storage field.
|
||||
// Strict compatible providers may reject even store:false. Keep the
|
||||
// explicit opt-out on OpenAI itself, but omit it on other endpoints.
|
||||
endpoint, err := url.Parse(base)
|
||||
if err != nil || !strings.EqualFold(endpoint.Hostname(), "api.openai.com") {
|
||||
compat["supportsStore"] = false
|
||||
}
|
||||
// Omission is opt-in: removing an explicit store:false globally could
|
||||
// change retention behavior on otherwise working compatible gateways.
|
||||
// ProviderExtraFields returns a copy, so this cannot mutate channel config.
|
||||
if value, exists := extra["store"]; api == "openai-completions" && exists && value == nil {
|
||||
compat["supportsStore"] = false
|
||||
delete(extra, "store")
|
||||
}
|
||||
if oa.IsLocalContextProfile() {
|
||||
compat["maxTokensField"] = "max_tokens"
|
||||
|
||||
Reference in new issue
Block a user