This commit is contained in:
OpenClaw-User 2026-03-14 17:10:10 +08:00
commit 7a86964fb0
3 changed files with 55 additions and 0 deletions

View file

@ -453,6 +453,7 @@ func ConvertProvidersToModelList(cfg *Config) []ModelConfig {
// Check if this is the user's configured provider // Check if this is the user's configured provider
if slices.Contains(m.providerNames, userProvider) && userModel != "" { if slices.Contains(m.providerNames, userProvider) && userModel != "" {
// Use the user's configured model instead of default // Use the user's configured model instead of default
mc.ModelName = userModel
mc.Model = buildModelWithProtocol(m.protocol, userModel) mc.Model = buildModelWithProtocol(m.protocol, userModel)
} else if userProvider == "" && userModel != "" && !legacyModelNameApplied { } else if userProvider == "" && userModel != "" && !legacyModelNameApplied {
// Legacy config: no explicit provider field but model is specified // Legacy config: no explicit provider field but model is specified

View file

@ -283,6 +283,10 @@ func TestConvertProvidersToModelList_PreservesUserModel_OpenAI(t *testing.T) {
t.Fatalf("len(result) = %d, want 1", len(result)) t.Fatalf("len(result) = %d, want 1", len(result))
} }
if result[0].ModelName != "gpt-4-turbo" {
t.Errorf("ModelName = %q, want %q", result[0].ModelName, "gpt-4-turbo")
}
if result[0].Model != "openai/gpt-4-turbo" { if result[0].Model != "openai/gpt-4-turbo" {
t.Errorf("Model = %q, want %q", result[0].Model, "openai/gpt-4-turbo") t.Errorf("Model = %q, want %q", result[0].Model, "openai/gpt-4-turbo")
} }
@ -331,11 +335,43 @@ func TestConvertProvidersToModelList_PreservesUserModel_Qwen(t *testing.T) {
t.Fatalf("len(result) = %d, want 1", len(result)) t.Fatalf("len(result) = %d, want 1", len(result))
} }
if result[0].ModelName != "qwen-plus" {
t.Errorf("ModelName = %q, want %q", result[0].ModelName, "qwen-plus")
}
if result[0].Model != "qwen/qwen-plus" { if result[0].Model != "qwen/qwen-plus" {
t.Errorf("Model = %q, want %q", result[0].Model, "qwen/qwen-plus") t.Errorf("Model = %q, want %q", result[0].Model, "qwen/qwen-plus")
} }
} }
func TestConvertProvidersToModelList_PreservesUserModelName_Ollama(t *testing.T) {
cfg := &Config{
Agents: AgentsConfig{
Defaults: AgentDefaults{
Provider: "ollama",
Model: "llama3.2",
},
},
Providers: ProvidersConfig{
Ollama: ProviderConfig{
APIBase: "http://localhost:11434/v1",
},
},
}
result := ConvertProvidersToModelList(cfg)
if len(result) != 1 {
t.Fatalf("len(result) = %d, want 1", len(result))
}
if result[0].ModelName != "llama3.2" {
t.Fatalf("ModelName = %q, want %q", result[0].ModelName, "llama3.2")
}
if result[0].Model != "ollama/llama3.2" {
t.Fatalf("Model = %q, want %q", result[0].Model, "ollama/llama3.2")
}
}
func TestConvertProvidersToModelList_UsesDefaultWhenNoUserModel(t *testing.T) { func TestConvertProvidersToModelList_UsesDefaultWhenNoUserModel(t *testing.T) {
cfg := &Config{ cfg := &Config{
Agents: AgentsConfig{ Agents: AgentsConfig{

View file

@ -344,6 +344,24 @@ func TestCreateProviderReturnsClaudeProviderForAnthropicOAuth(t *testing.T) {
// TODO: Test custom APIBase when createClaudeAuthProvider supports it // TODO: Test custom APIBase when createClaudeAuthProvider supports it
} }
func TestCreateProvider_LegacyOllamaProviderUsesUserModelAlias(t *testing.T) {
cfg := config.DefaultConfig()
cfg.Agents.Defaults.Provider = "ollama"
cfg.Agents.Defaults.Model = "llama3.2"
cfg.Providers.Ollama.APIBase = "http://localhost:11434/v1"
provider, modelID, err := CreateProvider(cfg)
if err != nil {
t.Fatalf("CreateProvider() error = %v", err)
}
if provider == nil {
t.Fatal("CreateProvider() returned nil provider")
}
if modelID != "llama3.2" {
t.Fatalf("modelID = %q, want %q", modelID, "llama3.2")
}
}
func TestCreateProviderReturnsCodexProviderForOpenAIOAuth(t *testing.T) { func TestCreateProviderReturnsCodexProviderForOpenAIOAuth(t *testing.T) {
// TODO: This test requires openai protocol to support auth_method: "oauth" // TODO: This test requires openai protocol to support auth_method: "oauth"
// which is not yet implemented in the new factory_provider.go // which is not yet implemented in the new factory_provider.go