package config import ( "crypto/sha256" "fmt" "strings" ) // ProviderKeyEnvName is the variable a run reads provider id's credential from // when the provider names no api_key_env. It is derived from the id alone, so // the daemon that resolves the credential and the container that holds only its // sentinel agree on the name without the key travelling. func ProviderKeyEnvName(id string) string { sum := sha256.Sum256([]byte("provider\x00" + id)) return fmt.Sprintf("ARCHIE_PROVIDER_%X_API_KEY", sum[:8]) } // KeyEnv is the variable a run reads provider id's credential from: its // api_key_env, or the name derived for a provider that carries only a secret // reference. A provider with neither holds no credential and names no variable. func (p Provider) KeyEnv(id string) string { switch { case p.APIKeyEnv != "": return p.APIKeyEnv case p.APIKey == (SecretRef{}): return "" default: return ProviderKeyEnvName(id) } } // ModelProviders returns c's providers that one of its model aliases resolves // to, each carrying the variable its credential is read from: the operator's // api_key_env, or a name derived from the provider when its credential is a // secret reference. A provider no alias names is left out, so nothing requires // a credential for a provider a run never reaches. func (c Config) ModelProviders() map[string]Provider { out := make(map[string]Provider, len(c.Providers)) for _, ref := range c.Models { id, _, ok := strings.Cut(strings.TrimSpace(ref), "/") if !ok { continue } provider, ok := c.Providers[id] if !ok { continue } provider.APIKeyEnv = provider.KeyEnv(id) out[id] = provider } return out }