zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
49.39k stars 3k forks source link

Environment variable indirection: configurable env var names for API keys #17288

Open NiloCK opened 2 months ago

NiloCK commented 2 months ago

Check for existing issues

Describe the feature

Zed looks for an Anthropic api key through the environment variable variable ANTHROPIC_API_KEY. This value seems to be hard coded.

I'd like it to use a namespaced key (eg, ANTHROPIC_API_KEY_NILOCK_ZED).

If applicable, add mockups / screenshots to help present your vision of the feature

The settings for LLMs could include a setting for this. From Default Settings:

  // Different settings for specific language models.
  "language_models": {
    "anthropic": {
      "version": "1",
      "api_url": "https://api.anthropic.com",
      "api_key_variable": "ANTHROPIC_API_KEY" // could add this as a configurable item
    },
notpeter commented 2 months ago

Initially I was skeptical of this idea, but I'm warming to it. I wonder if instead of polluting the settings namespace at various depths with *_variable keys we could instead explicitly support a layer of env var redirection:

{
  "environment": {
    "ANTHROPIC_API_KEY": "ANTHROPIC_API_KEY_NILOCK_ZED",
    "GITHUB_TOKEN": "WORK_GITHUB_TOKEN"
  }
}
NiloCK commented 2 months ago

Looks like a clean & universal solution.

I have a pretty strong preference for giving individual apps (and individual repos) their own API keys as an accounting mechanism.

Thanks for considering it.

notpeter commented 2 months ago

We could even support shell-style variable fallback syntax if we wanted to be fancy.

So you could an override a default environment variable if it were present but fallback to a default if the override was unset. Just spit-ballin:

  "environment": {
    "ANTHROPIC_API_KEY": "${ANTHROPIC_API_KEY_NILOCK_ZED:-$ANTHROPIC_API_KEY}",
  }