Files

59 lines
2.0 KiB
Python

"""Configuration resolution for the opencode-bridge extension.
All connection fields (URL / user / password / request timeout) are resolved
exclusively from environment variables + built-in defaults at server startup.
They are NOT stored in the JupyterLab plugin settings.
Env vars:
OPENCODE_BRIDGE_URL — OpenCode Serve base URL (default http://127.0.0.1:4096)
OPENCODE_BRIDGE_USER — HTTP Basic Auth username (default 'opencode')
OPENCODE_BRIDGE_PASSWORD — HTTP Basic Auth password (default '' = no auth)
OPENCODE_BRIDGE_TIMEOUT — request timeout in seconds (default 120)
"""
from __future__ import annotations
import os
from typing import NamedTuple, Optional, Tuple
ENV_URL = "OPENCODE_BRIDGE_URL"
ENV_USER = "OPENCODE_BRIDGE_USER"
ENV_PASSWORD = "OPENCODE_BRIDGE_PASSWORD"
ENV_TIMEOUT = "OPENCODE_BRIDGE_TIMEOUT"
DEFAULT_URL = "http://127.0.0.1:4096"
DEFAULT_USER = "opencode"
DEFAULT_REQUEST_TIMEOUT = 120
class OpenCodeConfig(NamedTuple):
url: str
user: str
password: str
request_timeout_seconds: int
@property
def auth(self) -> Optional[Tuple[str, str]]:
"""Return (user, password) for HTTP Basic Auth, or None if no password set."""
if not self.password:
return None
return (self.user, self.password)
def resolve_config(jupyter_settings: dict) -> OpenCodeConfig:
"""Resolve OpenCode connection config from environment variables + defaults.
The `jupyter_settings` argument is accepted for API compatibility but no
fields are read from it — all connection config is now startup-only
(environment variables).
"""
_ = jupyter_settings # intentionally unused; see module docstring
return OpenCodeConfig(
url=os.environ.get(ENV_URL) or DEFAULT_URL,
user=os.environ.get(ENV_USER) or DEFAULT_USER,
password=os.environ.get(ENV_PASSWORD) or "",
request_timeout_seconds=int(
os.environ.get(ENV_TIMEOUT) or DEFAULT_REQUEST_TIMEOUT
)
)