Files
sam e70c27faf9 fix(configuration): one provider table, so OpenRouter gets an endpoint (#2048)
The provider step wrote class "openrouter", which no ai-sdk class serves and
which egress cannot resolve a host for, so a provider the wizard offered could
never build a model session. The table now names the class that serves each
provider and the base URL it needs where the class has no default, setup writes
base_url with it, and the flag help is built from the same list.
2026-11-21 03:48:28 +11:00

178 lines
6.4 KiB
Go

package setup
import (
"context"
"fmt"
"strings"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite"
)
// cloudProvider is one selectable cloud LLM provider: the name its config table
// and model aliases use, the class that serves it, the env var its secret is
// stored under, and the base URL its class has no default for. The provider
// step and the secret sink both read this table, so they always name the same
// secret, and a provider cannot be offered without an endpoint the agent's
// model proxy can resolve.
type cloudProvider struct {
name string // display name in the prompt
key string // provider name in config.toml and the model alias prefix
class string // the ai-sdk class that serves it
apiKeyEnv string // the env var its secret is stored under
baseURL string // the endpoint, when the class has no default for it
}
//nolint:gosec // G101: env var names, not values.
var cloudProviders = []cloudProvider{
{name: "OpenAI", key: "openai", class: "openai", apiKeyEnv: "OPENAI_API_KEY"},
{name: "Anthropic", key: "anthropic", class: "anthropic", apiKeyEnv: "ANTHROPIC_API_KEY"},
{name: "OpenRouter", key: "openrouter", class: "openai-compatible", apiKeyEnv: "OPENROUTER_API_KEY", baseURL: "https://openrouter.ai/api/v1"},
{name: "Gemini", key: "gemini", class: "gemini", apiKeyEnv: "GEMINI_API_KEY"},
{name: "Groq", key: "groq", class: "groq", apiKeyEnv: "GROQ_API_KEY"},
{name: "DeepSeek", key: "deepseek", class: "deepseek", apiKeyEnv: "DEEPSEEK_API_KEY"},
{name: "Mistral", key: "mistral", class: "mistral", apiKeyEnv: "MISTRAL_API_KEY"},
}
// ProviderNames lists the provider names setup accepts, in prompt order.
func ProviderNames() []string {
names := make([]string, 0, len(cloudProviders)+1)
for _, cp := range cloudProviders {
names = append(names, cp.key)
}
return append(names, "ollama")
}
func stepProvider(ctx context.Context, p Prompter, discovery ModelDiscovery, secrets SecretSink, params Params) (tableEdits, string, error) {
options := make([]string, 0, len(cloudProviders)+1)
for _, cp := range cloudProviders {
options = append(options, cp.name)
}
options = append(options, "Self-hosted (Ollama)")
var choice int
if strings.TrimSpace(params.Provider) != "" {
var err error
choice, err = providerChoice(params.Provider)
if err != nil {
return nil, "", fmt.Errorf("setup: provider: %w", err)
}
} else {
var err error
choice, err = p.Select(ctx, "LLM provider:", options)
if err != nil {
return nil, "", fmt.Errorf("setup: provider: %w", err)
}
}
if choice == len(cloudProviders) {
if params.ProviderAPIKeyRef != (config.SecretRef{}) {
return nil, "", fmt.Errorf("setup: a provider key reference was given, but the self-hosted provider is keyless, so nothing would use it")
}
return stepSelfHostedModel(ctx, p, discovery, params.Model)
}
return stepCloudProvider(ctx, p, secrets, cloudProviders[choice], params.Model, params.ProviderAPIKeyRef)
}
// providerChoice maps a typed Params.Provider class to the same select index
// the interactive prompt uses, so a supplied provider and a prompted one land
// in exactly the same step logic.
func providerChoice(name string) (int, error) {
name = strings.ToLower(strings.TrimSpace(name))
if name == "ollama" {
return len(cloudProviders), nil
}
for i, cp := range cloudProviders {
if cp.key == name {
return i, nil
}
}
return -1, fmt.Errorf("unknown provider %q", name)
}
func stepSelfHostedModel(ctx context.Context, p Prompter, discovery ModelDiscovery, modelParam string) (tableEdits, string, error) {
var model string
if strings.TrimSpace(modelParam) != "" {
model = modelParam
} else if discovery != nil {
if models, err := discovery.ListOllamaModels(ctx); err == nil && len(models) > 0 {
idx, err := p.Select(ctx, "Ollama model:", models)
if err != nil {
return nil, "", fmt.Errorf("setup: model selection: %w", err)
}
model = models[idx]
}
}
if model == "" {
var err error
model, err = p.ReadLine(ctx, "Ollama model name (e.g. llama3): ", "llama3")
if err != nil {
return nil, "", fmt.Errorf("setup: model name: %w", err)
}
}
edits := tableEdits{"providers.ollama": {"class": tomlwrite.String("ollama")}}
return edits, "ollama/" + model, nil
}
func stepCloudProvider(ctx context.Context, p Prompter, secrets SecretSink, cp cloudProvider, modelParam string, keyRef config.SecretRef) (tableEdits, string, error) {
table := "providers." + cp.key
edits := tableEdits{table: {"class": tomlwrite.String(cp.class)}}
if cp.baseURL != "" {
edits[table]["base_url"] = tomlwrite.String(cp.baseURL)
}
if keyRef != (config.SecretRef{}) {
// A reference means the value already lives in a secret engine: write
// the reference and ask nothing. Setup neither reads nor stores it,
// because it does not own it.
edits[table]["api_key"] = tomlwrite.Ref(keyRef.Engine, keyRef.Key)
} else {
keyEdit, err := cloudKeyEdit(ctx, p, secrets, cp)
if err != nil {
return nil, "", err
}
if keyEdit != "" {
edits[table]["api_key"] = keyEdit
}
}
model, err := cloudModel(ctx, p, cp, modelParam)
if err != nil {
return nil, "", err
}
return edits, cp.key + "/" + model, nil
}
// cloudKeyEdit prompts for the API key and returns its edit. Empty means
// configure later.
func cloudKeyEdit(ctx context.Context, p Prompter, secrets SecretSink, cp cloudProvider) (string, error) {
key, err := p.ReadSecret(ctx, fmt.Sprintf("%s API key: ", cp.name))
if err != nil {
return "", fmt.Errorf("setup: %s api key: %w", cp.name, err)
}
if strings.TrimSpace(key) == "" {
return "", nil
}
if err := secrets.Put("env", cp.apiKeyEnv, key); err != nil {
return "", fmt.Errorf("setup: store %s api key: %w", cp.name, err)
}
return tomlwrite.Ref("env", cp.apiKeyEnv), nil
}
// cloudModel resolves the model name: the parameter when given, otherwise a
// prompt, and an error when neither yields one.
func cloudModel(ctx context.Context, p Prompter, cp cloudProvider, modelParam string) (string, error) {
model := modelParam
if strings.TrimSpace(model) == "" {
var err error
model, err = p.ReadLine(ctx, fmt.Sprintf("Model for %s (e.g. gpt-5.4): ", cp.name), "")
if err != nil {
return "", fmt.Errorf("setup: model name: %w", err)
}
}
if strings.TrimSpace(model) == "" {
return "", fmt.Errorf("setup: a model name is required for %s", cp.name)
}
return model, nil
}