Files
picoclaw/pkg/providers/legacy_provider.go
T
yinwm e1583f3b13 refactor: simplify legacy_provider.go from 349 to 49 lines
- Move OAuth helper functions to factory_provider.go
- Add auto-migration in LoadConfig: old providers -> model_list
- Add Workspace field to ModelConfig for CLI-based providers
- Fix OAuth handling to use auth store instead of raw APIKey
- Update tests to use new model_list configuration format

This eliminates the giant switch-case in legacy_provider.go,
achieving the goal of "zero-code provider addition" from the
design document (issue #283).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 01:30:19 +08:00

50 lines
1.5 KiB
Go

// PicoClaw - Ultra-lightweight personal AI agent
// License: MIT
//
// Copyright (c) 2026 PicoClaw contributors
package providers
import (
"fmt"
"github.com/sipeed/picoclaw/pkg/config"
)
// CreateProvider creates a provider based on the configuration.
// It uses the model_list configuration (new format) to create providers.
// The old providers config is automatically converted to model_list during config loading.
// Returns the provider, the model ID to use, and any error.
func CreateProvider(cfg *config.Config) (LLMProvider, string, error) {
model := cfg.Agents.Defaults.Model
// Ensure model_list is populated (should be done by LoadConfig, but handle edge cases)
if len(cfg.ModelList) == 0 && cfg.HasProvidersConfig() {
cfg.ModelList = config.ConvertProvidersToModelList(cfg)
}
// Must have model_list at this point
if len(cfg.ModelList) == 0 {
return nil, "", fmt.Errorf("no providers configured. Please add entries to model_list in your config")
}
// Get model config from model_list
modelCfg, err := cfg.GetModelConfig(model)
if err != nil {
return nil, "", fmt.Errorf("model %q not found in model_list: %w", model, err)
}
// Inject global workspace if not set in model config
if modelCfg.Workspace == "" {
modelCfg.Workspace = cfg.WorkspacePath()
}
// Use factory to create provider
provider, modelID, err := CreateProviderFromConfig(modelCfg)
if err != nil {
return nil, "", fmt.Errorf("failed to create provider for model %q: %w", model, err)
}
return provider, modelID, nil
}