153 lines
6.5 KiB
Go
153 lines
6.5 KiB
Go
package multiagent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"cyberstrike-ai/internal/agent"
|
|
"cyberstrike-ai/internal/config"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func TestPiModelInstructionRoleCompatibility(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, provider, url, profile, model, api string
|
|
systemOnly bool
|
|
}{
|
|
{"gateway", "openai", "https://gateway.example/v1", "auto", "fixture", "openai-completions", true},
|
|
{"deepseek gateway", "openai", "https://gateway.example/v1", "deepseek_compat", "fixture", "openai-completions", true},
|
|
{"local", "openai_compatible", "http://localhost:8000/v1", "auto", "fixture", "openai-completions", true},
|
|
{"official", "openai", "https://api.openai.com/v1/", "auto", "o3", "openai-completions", false},
|
|
{"default endpoint", "openai", "", "auto", "o3", "openai-completions", false},
|
|
{"official with deepseek profile", "openai", "https://api.openai.com/v1", "deepseek_compat", "fixture", "openai-completions", true},
|
|
{"responses", "openai", "https://api.openai.com/v1", "auto", "gpt-6.1-sol", "openai-responses", false},
|
|
{"anthropic", "claude", "https://api.anthropic.com", "auto", "fixture", "anthropic-messages", false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
oa := config.OpenAIConfig{Provider: tc.provider, BaseURL: tc.url, Model: tc.model,
|
|
Reasoning: config.OpenAIReasoningConfig{Mode: "on", Effort: "low", Profile: tc.profile}}
|
|
model := piModelConfig(oa, nil, "")
|
|
if model.API != tc.api || model.Thinking != "low" {
|
|
t.Fatalf("unexpected API or thinking: %+v", model)
|
|
}
|
|
value, set := model.Compat["supportsDeveloperRole"]
|
|
if tc.systemOnly && (!set || value != false) {
|
|
t.Fatalf("compatible endpoint must explicitly use system messages: %+v", model.Compat)
|
|
}
|
|
if !tc.systemOnly && set {
|
|
t.Fatalf("native protocol compatibility must remain SDK-controlled: %+v", model.Compat)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPiModelNativeVendorProtocols(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
provider, api, baseURL string
|
|
}{
|
|
{"gemini", "google-generative-ai", "https://generativelanguage.googleapis.com/v1beta"},
|
|
{"google", "google-generative-ai", "https://generativelanguage.googleapis.com/v1beta"},
|
|
{"vertex", "google-vertex", "https://aiplatform.googleapis.com/v1"},
|
|
{"google-vertex", "google-vertex", "https://aiplatform.googleapis.com/v1"},
|
|
{"bedrock", "bedrock-converse-stream", "https://bedrock-runtime.us-east-1.amazonaws.com"},
|
|
{"mistral", "mistral-conversations", "https://api.mistral.ai"},
|
|
{"azure-responses", "azure-openai-responses", ""},
|
|
} {
|
|
t.Run(tc.provider, func(t *testing.T) {
|
|
oa := config.OpenAIConfig{Provider: tc.provider, Model: "fixture", ContextProfile: "local_auto",
|
|
Reasoning: config.OpenAIReasoningConfig{Mode: "on", Effort: "low", Profile: "deepseek_compat",
|
|
ExtraRequestFields: map[string]any{"store": nil}}}
|
|
model := piModelConfig(oa, nil, "")
|
|
if model.API != tc.api || model.BaseURL != tc.baseURL {
|
|
t.Fatalf("unexpected native mapping: %+v", model)
|
|
}
|
|
if len(model.Compat) != 0 {
|
|
t.Fatalf("openai-completions compat leaked into %s: %v", tc.api, model.Compat)
|
|
}
|
|
if value, ok := model.ExtraFields["store"]; !ok || value != nil {
|
|
t.Fatalf("configured extra fields were dropped: %v", model.ExtraFields)
|
|
}
|
|
if _, ok := oa.Reasoning.ExtraRequestFields["store"]; !ok {
|
|
t.Fatal("channel config mutated")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPiModelNativeVendorKeepsConfiguredEndpoint(t *testing.T) {
|
|
for _, provider := range []string{"gemini", "vertex", "bedrock", "mistral", "azure-responses"} {
|
|
t.Run(provider, func(t *testing.T) {
|
|
oa := config.OpenAIConfig{Provider: provider, Model: "fixture", BaseURL: "https://gateway.example/v1/"}
|
|
if model := piModelConfig(oa, nil, ""); model.BaseURL != "https://gateway.example/v1" {
|
|
t.Fatalf("configured endpoint was replaced: %+v", model)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPiCompatibleReasoningUsesSystemMessages(t *testing.T) {
|
|
for _, profile := range []string{"auto", "deepseek_compat"} {
|
|
for _, background := range []bool{false, true} {
|
|
name := profile + "/agent"
|
|
if background {
|
|
name = profile + "/background"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
cfg := piTestConfig(t)
|
|
cfg.OpenAI.Reasoning = config.OpenAIReasoningConfig{Mode: "on", Effort: "low", Profile: profile}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var body struct {
|
|
Messages []struct {
|
|
Role string
|
|
Content json.RawMessage
|
|
}
|
|
Effort string `json:"reasoning_effort"`
|
|
Thinking struct{ Type string }
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Error(err)
|
|
http.Error(w, "invalid request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
for _, message := range body.Messages {
|
|
if message.Role == "developer" {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
_, _ = w.Write([]byte(`{"message":"developer is not one of ['system', 'assistant', 'user', 'tool', 'function']","type":"invalid_request_error","param":null,"code":"invalid_parameter_error"}`))
|
|
return
|
|
}
|
|
}
|
|
if len(body.Messages) == 0 || body.Messages[0].Role != "system" || !strings.Contains(string(body.Messages[0].Content), "instruction-role fixture") {
|
|
t.Errorf("system instruction missing: %+v", body.Messages)
|
|
}
|
|
if body.Effort != "low" || (profile == "deepseek_compat" && body.Thinking.Type != "enabled") {
|
|
t.Errorf("reasoning was not preserved: %+v", body)
|
|
}
|
|
piReply(w, "", "", nil, "role verified")
|
|
}))
|
|
defer server.Close()
|
|
cfg.OpenAI.BaseURL = server.URL + "/v1"
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
defer cancel()
|
|
if background {
|
|
message, _, err := CompleteBackgroundStepWithRuntime(ctx, cfg.AgentRuntime, cfg.OpenAI,
|
|
[]agent.ChatMessage{{Role: "system", Content: "instruction-role fixture"}, {Role: "user", Content: "Say ok"}}, nil, 1024, zap.NewNop())
|
|
if err != nil || message == nil || message.Content != "role verified" {
|
|
t.Fatalf("message=%+v err=%v", message, err)
|
|
}
|
|
} else {
|
|
result, err := RunSingleAgent(ctx, cfg, &cfg.MultiAgent, piTestAgent(cfg, nil), nil, zap.NewNop(), "", "", "Say ok", nil, nil, nil, nil, "instruction-role fixture")
|
|
if err != nil || result == nil || result.Response != "role verified" || result.RuntimeStatus != "completed" {
|
|
t.Fatalf("result=%+v err=%v", result, err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|