mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-18 04:41:56 +00:00
fix(cli): allow rotating broken OpenRouter / AI Gateway key in hermes model flow (#25750)
Before: when `OPENROUTER_API_KEY` (or `AI_GATEWAY_API_KEY`) was already set in ~/.hermes/.env, `hermes model openrouter` / `hermes model ai-gateway` skipped the API-key prompt entirely and jumped straight to the model picker. Users with a broken / expired / wrong key had no way to replace it without editing ~/.hermes/.env by hand or re-running `hermes setup` from scratch. Both flows now route through the existing `_prompt_api_key()` helper, which surfaces [K]eep / [R]eplace / [C]lear when a key is already configured — the same UX the generic API-key providers (z.ai, MiniMax, Gemini, etc.) and the Daytona setup already use.
This commit is contained in:
parent
1dca6a6960
commit
17e0e9d174
1 changed files with 28 additions and 34 deletions
|
|
@ -2414,30 +2414,31 @@ def _prompt_provider_choice(choices, *, default=0):
|
||||||
def _model_flow_openrouter(config, current_model=""):
|
def _model_flow_openrouter(config, current_model=""):
|
||||||
"""OpenRouter provider: ensure API key, then pick model."""
|
"""OpenRouter provider: ensure API key, then pick model."""
|
||||||
from hermes_cli.auth import (
|
from hermes_cli.auth import (
|
||||||
|
ProviderConfig,
|
||||||
_prompt_model_selection,
|
_prompt_model_selection,
|
||||||
_save_model_choice,
|
_save_model_choice,
|
||||||
deactivate_provider,
|
deactivate_provider,
|
||||||
)
|
)
|
||||||
from hermes_cli.config import get_env_value, save_env_value
|
from hermes_cli.config import get_env_value
|
||||||
|
|
||||||
api_key = get_env_value("OPENROUTER_API_KEY")
|
# Route through _prompt_api_key so users can replace a stale/broken key
|
||||||
if not api_key:
|
# in-flow (K/R/C) instead of having to edit ~/.hermes/.env by hand. The
|
||||||
print("No OpenRouter API key configured.")
|
# previous bypass-when-key-exists branch left no way to recover from a
|
||||||
|
# bad paste short of re-running `hermes setup` from scratch. OpenRouter
|
||||||
|
# isn't in PROVIDER_REGISTRY so we synthesize a minimal pconfig.
|
||||||
|
pconfig = ProviderConfig(
|
||||||
|
id="openrouter",
|
||||||
|
name="OpenRouter",
|
||||||
|
auth_type="api_key",
|
||||||
|
api_key_env_vars=("OPENROUTER_API_KEY",),
|
||||||
|
)
|
||||||
|
existing_key = get_env_value("OPENROUTER_API_KEY") or ""
|
||||||
|
if not existing_key:
|
||||||
print("Get one at: https://openrouter.ai/keys")
|
print("Get one at: https://openrouter.ai/keys")
|
||||||
print()
|
print()
|
||||||
try:
|
_resolved, abort = _prompt_api_key(pconfig, existing_key, provider_id="openrouter")
|
||||||
import getpass
|
if abort:
|
||||||
|
return
|
||||||
key = getpass.getpass("OpenRouter API key (or Enter to cancel): ").strip()
|
|
||||||
except (KeyboardInterrupt, EOFError):
|
|
||||||
print()
|
|
||||||
return
|
|
||||||
if not key:
|
|
||||||
print("Cancelled.")
|
|
||||||
return
|
|
||||||
save_env_value("OPENROUTER_API_KEY", key)
|
|
||||||
print("API key saved.")
|
|
||||||
print()
|
|
||||||
|
|
||||||
from hermes_cli.models import model_ids, get_pricing_for_provider
|
from hermes_cli.models import model_ids, get_pricing_for_provider
|
||||||
|
|
||||||
|
|
@ -2473,33 +2474,26 @@ def _model_flow_openrouter(config, current_model=""):
|
||||||
def _model_flow_ai_gateway(config, current_model=""):
|
def _model_flow_ai_gateway(config, current_model=""):
|
||||||
"""Vercel AI Gateway provider: ensure API key, then pick model with pricing."""
|
"""Vercel AI Gateway provider: ensure API key, then pick model with pricing."""
|
||||||
from hermes_cli.auth import (
|
from hermes_cli.auth import (
|
||||||
|
PROVIDER_REGISTRY,
|
||||||
_prompt_model_selection,
|
_prompt_model_selection,
|
||||||
_save_model_choice,
|
_save_model_choice,
|
||||||
deactivate_provider,
|
deactivate_provider,
|
||||||
)
|
)
|
||||||
from hermes_cli.config import get_env_value, save_env_value
|
from hermes_cli.config import get_env_value
|
||||||
|
|
||||||
api_key = get_env_value("AI_GATEWAY_API_KEY")
|
# Route through _prompt_api_key so users can replace a stale/broken key
|
||||||
if not api_key:
|
# in-flow (K/R/C) instead of having to edit ~/.hermes/.env by hand.
|
||||||
print("No Vercel AI Gateway API key configured.")
|
pconfig = PROVIDER_REGISTRY["ai-gateway"]
|
||||||
|
existing_key = get_env_value("AI_GATEWAY_API_KEY") or ""
|
||||||
|
if not existing_key:
|
||||||
print(
|
print(
|
||||||
"Create API key here: https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai-gateway&title=AI+Gateway"
|
"Create API key here: https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai-gateway&title=AI+Gateway"
|
||||||
)
|
)
|
||||||
print("Add a payment method to get $5 in free credits.")
|
print("Add a payment method to get $5 in free credits.")
|
||||||
print()
|
print()
|
||||||
try:
|
_resolved, abort = _prompt_api_key(pconfig, existing_key, provider_id="ai-gateway")
|
||||||
import getpass
|
if abort:
|
||||||
|
return
|
||||||
key = getpass.getpass("AI Gateway API key (or Enter to cancel): ").strip()
|
|
||||||
except (KeyboardInterrupt, EOFError):
|
|
||||||
print()
|
|
||||||
return
|
|
||||||
if not key:
|
|
||||||
print("Cancelled.")
|
|
||||||
return
|
|
||||||
save_env_value("AI_GATEWAY_API_KEY", key)
|
|
||||||
print("API key saved.")
|
|
||||||
print()
|
|
||||||
|
|
||||||
from hermes_cli.models import ai_gateway_model_ids, get_pricing_for_provider
|
from hermes_cli.models import ai_gateway_model_ids, get_pricing_for_provider
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue