feat(backend): support dedicated OpenRouter API key for autopilot

Add AUTOPILOT_OPEN_ROUTER_API_KEY env var that, when set, is used
instead of OPEN_ROUTER_API_KEY for CoPilot/AutoPilot SDK calls.
Falls back to the regular key when not configured.
This commit is contained in:
Zamil Majdy
2026-03-27 00:03:32 +07:00
parent 85f0d8353a
commit 73399b3945
3 changed files with 17 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ from pydantic import Field, field_validator
from pydantic_settings import BaseSettings
from backend.util.clients import OPENROUTER_BASE_URL
from backend.util.settings import Settings
class ChatConfig(BaseSettings):
@@ -171,7 +172,12 @@ class ChatConfig(BaseSettings):
base = (self.base_url or "").rstrip("/")
if base.endswith("/v1"):
base = base[:-3]
return bool(self.api_key and base and base.startswith("http"))
return bool(self.autopilot_api_key and base and base.startswith("http"))
@property
def autopilot_api_key(self) -> str | None:
"""Return the dedicated AutoPilot OpenRouter API key, falling back to api_key."""
return Settings().secrets.autopilot_open_router_api_key or self.api_key
@property
def e2b_active(self) -> bool:

View File

@@ -524,9 +524,10 @@ def _build_sdk_env(
base = (config.base_url or "").rstrip("/")
if base.endswith("/v1"):
base = base[:-3]
env: dict[str, str] = {
"ANTHROPIC_BASE_URL": base,
"ANTHROPIC_AUTH_TOKEN": config.api_key or "",
"ANTHROPIC_AUTH_TOKEN": config.autopilot_api_key or "",
"ANTHROPIC_API_KEY": "", # force CLI to use AUTH_TOKEN
}
@@ -1776,10 +1777,10 @@ async def stream_chat_completion_sdk(
# Fail fast when no API credentials are available at all.
sdk_env = _build_sdk_env(session_id=session_id, user_id=user_id)
if not config.api_key and not config.use_claude_code_subscription:
if not config.autopilot_api_key and not config.use_claude_code_subscription:
raise RuntimeError(
"No API key configured. Set OPEN_ROUTER_API_KEY, "
"CHAT_API_KEY, or ANTHROPIC_API_KEY for API access, "
"No API key configured. Set AUTOPILOT_OPEN_ROUTER_API_KEY, "
"OPEN_ROUTER_API_KEY, CHAT_API_KEY, or ANTHROPIC_API_KEY for API access, "
"or CHAT_USE_CLAUDE_CODE_SUBSCRIPTION=true to use "
"Claude Code CLI subscription (requires `claude login`)."
)

View File

@@ -619,6 +619,11 @@ class Secrets(UpdateTrackingModel["Secrets"], BaseSettings):
anthropic_api_key: str = Field(default="", description="Anthropic API key")
groq_api_key: str = Field(default="", description="Groq API key")
open_router_api_key: str = Field(default="", description="Open Router API Key")
autopilot_open_router_api_key: str = Field(
default="",
description="Dedicated Open Router API Key for AutoPilot. "
"When set, autopilot uses this key instead of the shared open_router_api_key.",
)
llama_api_key: str = Field(default="", description="Llama API Key")
v0_api_key: str = Field(default="", description="v0 by Vercel API key")
webshare_proxy_username: str = Field(