Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

feat(providers): support local OpenAI-compatible gateways (#1822)

Just One More Nightc63b076a8c8b
diff --git a/.env.example b/.env.example
index 732d5fad5..4ea992a26 100644
--- a/.env.example
+++ b/.env.example
@@ -9,8 +9,9 @@
# Anthropic (recommended for best results)
ANTHROPIC_API_KEY=your_anthropic_api_key_here
-# OpenAI
+# OpenAI or an OpenAI-compatible gateway such as 9router
OPENAI_API_KEY=your_openai_api_key_here
+# OPENAI_BASE_URL=http://localhost:20128/v1
# DeepSeek
DEEPSEEK_API_KEY=your_deepseek_api_key_here
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 05b94d8fe..080f27442 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -117,6 +117,18 @@ The headline this cycle is test intelligence that needs no coverage report. 0.44
or a bar. The file page names the tests that reach it, with `via` separating a
test whose calls run into the file from one that only imports it. (#1758)
+- **OpenAI-compatible local gateways expose their models.** Custom endpoints
+ configured with `OPENAI_BASE_URL` can now return namespaced model ids such as
+ 9router's `ag/...` and `ds/...` entries; loopback requests also bypass proxy
+ environment parsing so a standard IPv6 `NO_PROXY` entry cannot block them.
+
+- **Custom OpenAI-compatible setup is a first-class init flow.** The provider
+ menu separates official OpenAI from custom/local gateways, validates the
+ endpoint, hides and optionally saves the key, verifies `/models`, supports
+ model search plus exact-id fallback, and offers retry/manual/back recovery.
+ `--no-save-key` persists the non-secret endpoint without persisting the key;
+ CI, `--yes`, and non-TTY runs remain non-interactive.
+
- **`untested_hotspot` stops accusing files that the tests run.** With no
coverage ingested it fired on any hotspot without a *paired test file*, which
is a filename convention, so a suite that names its tests for behaviour
diff --git a/docs/reference/CONFIG.md b/docs/reference/CONFIG.md
index a2f4b7371..dd426e794 100644
--- a/docs/reference/CONFIG.md
+++ b/docs/reference/CONFIG.md
@@ -528,6 +528,42 @@ export OPENAI_BASE_URL="http://localhost:8000/v1"
repowise init --provider openai --model qwen3 --reasoning off
```
+The same adapter works with local gateways such as 9router and with any
+OpenAI-compatible custom provider. Set the gateway's API key under
+`OPENAI_API_KEY`, point `OPENAI_BASE_URL` at its `/v1` endpoint, and use the
+model id returned by that endpoint's `/models` response:
+
+```bash
+export OPENAI_API_KEY="your-9router-dashboard-key"
+export OPENAI_BASE_URL="http://localhost:20128/v1"
+repowise init --provider openai --model ag/gemini-3.7-flash-medium
+```
+
+Repowise discovers namespaced model ids from compatible gateways as-is, so
+the model does not need to start with `gpt-`. For another local provider,
+replace the key, URL, and model with that provider's values.
+
+In an interactive `repowise init`, choose **OpenAI-compatible (Custom / local
+gateway)**. Repowise then validates the endpoint, collects the key without
+echoing it, verifies the gateway through `/models`, and lets you search the
+discovered models or enter an exact model id. If `/models` is unavailable, you
+can retry the endpoint/key, continue with a manual model id, or return to the
+provider menu:
+
+```text
+Base URL [http://localhost:20128/v1]:
+API key (hidden): <paste the gateway key>
+✓ Connected — discovered 47 model(s).
+Select model: ag/gemini-3.7-flash-medium
+```
+
+The endpoint is placed in the repo's gitignored `.repowise/.env`; the key is
+saved there only after confirmation, and `--no-save-key` always keeps it
+process-local. The selected runtime provider (`openai`) and exact model id are
+written to `config.yaml`. Choose the separate **openai** row for the official
+OpenAI endpoint. Scripted runs (`--yes`, CI, or non-TTY) remain non-interactive
+and should continue to use environment variables and `--provider openai`.
+
### OpenRouter
```bash
@@ -702,7 +738,7 @@ The `.repowise/.env` file is gitignored automatically.
| Variable | Description |
|----------|-------------|
| `ANTHROPIC_API_KEY` | Anthropic API key |
-| `OPENAI_API_KEY` | OpenAI API key |
+| `OPENAI_API_KEY` | OpenAI or OpenAI-compatible gateway API key |
| `GEMINI_API_KEY` / `GOOGLE_API_KEY` | Google Gemini API key |
| `OPENROUTER_API_KEY` | OpenRouter API key |
| `DEEPSEEK_API_KEY` | DeepSeek API key |
@@ -715,7 +751,7 @@ The `.repowise/.env` file is gitignored automatically.
| Variable | Description |
|----------|-------------|
| `ANTHROPIC_BASE_URL` | Override the Anthropic API base URL |
-| `OPENAI_BASE_URL` | Override the OpenAI API base URL (used for vLLM/SGLang-compatible endpoints) |
+| `OPENAI_BASE_URL` | Override the OpenAI API base URL (used for vLLM/SGLang, 9router, and other compatible endpoints) |
| `GEMINI_BASE_URL` | Override the Gemini API base URL |
| `OLLAMA_BASE_URL` | Ollama server URL (default: `http://localhost:11434`) |
| `DEEPSEEK_BASE_URL` | Override the DeepSeek API base URL |
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/command.py b/packages/cli/src/repowise/cli/commands/init_cmd/command.py
index 5d046e725..14f551291 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/command.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/command.py
@@ -38,6 +38,7 @@ from repowise.cli.helpers import (
head_commit_ts,
load_config,
load_state,
+ resolve_explicit_provider_or_prompt,
resolve_max_file_pages,
resolve_provider,
resolve_reasoning,
@@ -413,7 +414,9 @@ def _run_generation_phase(
help=(
"LLM provider name (anthropic, openai, openrouter, gemini, "
"deepseek, kimi, ollama, litellm, codex_cli, claude_cli, opencode, "
- "edenai, mock)."
+ "edenai, mock). "
+ "In a terminal, a missing key is prompted for; openai also asks for "
+ "an optional OpenAI-compatible Base URL."
),
)
@click.option("--model", default=None, help="Model identifier override.")
@@ -986,6 +989,7 @@ def init_command(
model,
reasoning,
repo_path=repo_path,
+ save_key=save_key,
)
provider_name = selection.provider_name
model = selection.model
@@ -1169,16 +1173,19 @@ def init_command(
f"Decision extraction provider: [{VALUE}]{decision_provider.provider_name}[/]"
)
else:
- # No prompt here. ``is_interactive`` (line ~800) is already false only
- # when the user passed --provider, --index-only or --yes, or stdin is
- # not a terminal — so the old fallback picker in this branch fired
- # exactly on ``--yes``, which means "do not ask me". On shells where
- # isatty() claims a terminal it cannot actually read from (Windows
- # mintty, ``docker run -t`` without -i) that prompt hit EOF and killed
- # the run instead. A --yes run with no key now lands in the template
- # wiki below rather than in a question or a crash.
+ # An explicit provider may onboard its missing key (and, for the
+ # OpenAI-compatible adapter, its endpoint) here when stdin is a real
+ # terminal. Auto-detection and scripted runs remain non-interactive:
+ # a --yes run with no key lands in the template wiki below rather than
+ # in a question or a crash.
try:
- provider = resolve_provider(provider_name, model, repo_path)
+ provider = resolve_explicit_provider_or_prompt(
+ provider_name,
+ model,
+ repo_path,
+ interactive=sys.stdin.isatty() and not yes and not index_only,
+ save_key=save_key,
+ )
except click.ClickException as exc:
# Nothing configured anywhere. Workspace init, workspace update
# and the OSS server all render a template wiki in this exact
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py b/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py
index 5c12eac60..f91a37103 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/workspace.py
@@ -36,6 +36,7 @@ from repowise.cli.helpers import (
get_head_commit,
load_config,
load_state,
+ resolve_explicit_provider_or_prompt,
resolve_max_file_pages,
resolve_provider,
resolve_reasoning,
@@ -382,8 +383,7 @@ def _ingest_and_generate_repo(repo: Any, idx: int, total: int, ctx: _WorkspaceCt
pages_generated = len(generated_pages)
docs_mode = "deterministic"
console.print(
- f" [{OK}]✓[/] Rendered {len(generated_pages)} pages from structure "
- "(no model)\n"
+ f" [{OK}]✓[/] Rendered {len(generated_pages)} pages from structure (no model)\n"
)
if ctx.dry_run:
@@ -665,7 +665,11 @@ def _workspace_init(
index_only = True
elif mode == "advanced":
selection = interactive_provider_config_select(
- console, model, reasoning, repo_path=primary_repo.path
+ console,
+ model,
+ reasoning,
+ repo_path=primary_repo.path,
+ save_key=save_key,
)
provider_name = selection.provider_name
model = selection.model
@@ -694,7 +698,11 @@ def _workspace_init(
elif not index_only:
# "full" mode
selection = interactive_provider_config_select(
- console, model, reasoning, repo_path=primary_repo.path
+ console,
+ model,
+ reasoning,
+ repo_path=primary_repo.path,
+ save_key=save_key,
)
provider_name = selection.provider_name
model = selection.model
@@ -706,7 +714,13 @@ def _workspace_init(
provider = None
if not index_only:
try:
- provider = resolve_provider(provider_name, model, primary_repo.path)
+ provider = resolve_explicit_provider_or_prompt(
+ provider_name,
+ model,
+ primary_repo.path,
+ interactive=sys.stdin.isatty() and not yes and not index_only,
+ save_key=save_key,
+ )
# Re-resolve the embedder now that interactive provider selection
# may have set the provider's API key in os.environ. Without
# this, full-mode runs would display "mock" forever because
@@ -720,6 +734,8 @@ def _workspace_init(
if resolved_reasoning != "auto":
console.print(f" Reasoning: [{VALUE}]{resolved_reasoning}[/]\n")
except Exception as exc:
+ if provider_name is not None:
+ raise
console.print(
f" [{WARN}]Provider setup failed ({exc}); falling back to index-only.[/]"
)
diff --git a/packages/cli/src/repowise/cli/helpers.py b/packages/cli/src/repowise/cli/helpers.py
index ed031d722..0aa9ccc26 100644
--- a/packages/cli/src/repowise/cli/helpers.py
+++ b/packages/cli/src/repowise/cli/helpers.py
@@ -990,6 +990,61 @@ def resolve_provider(
)
+def resolve_explicit_provider_or_prompt(
+ provider_name: str | None,
+ model: str | None,
+ repo_path: Path,
+ *,
+ interactive: bool,
+ save_key: bool = True,
+) -> Any:
+ """Resolve an explicit provider, onboarding missing credentials in a TTY.
+
+ ``resolve_provider`` stays non-interactive because hooks, CI, and library
+ callers must never block on stdin. ``init`` can opt into this wrapper when
+ it has a real terminal: a missing explicit provider key gets the same
+ inline setup as the provider picker, and an explicit OpenAI provider also
+ gets the endpoint question used by local gateways.
+ """
+ from repowise.cli.ui.provider_selection import interactive_provider_credentials
+
+ if interactive and provider_name == "openai":
+ # A pty can report TTY while stdin is not readable. Let the normal
+ # resolution below produce the actionable provider error instead of
+ # replacing it with Abort.
+ with contextlib.suppress(EOFError, click.Abort):
+ interactive_provider_credentials(
+ console,
+ provider_name,
+ repo_path=repo_path,
+ save_key=save_key,
+ )
+
+ return resolve_provider(provider_name, model, repo_path=repo_path)
+
+ try:
+ return resolve_provider(provider_name, model, repo_path=repo_path)
+ except click.ClickException as original_error:
+ if not interactive or provider_name is None:
+ raise
+
+ try:
+ configured = interactive_provider_credentials(
+ console,
+ provider_name,
+ repo_path=repo_path,
+ save_key=save_key,
+ )
+ except (EOFError, click.Abort):
+ # A pty can report TTY while stdin is not readable. Preserve the
+ # actionable provider error instead of replacing it with Abort.
+ raise original_error from None
+
+ if not configured:
+ raise original_error
+ return resolve_provider(provider_name, model, repo_path=repo_path)
+
+
def resolve_provider_or_prompt(
provider_name: str | None,
model: str | None,
diff --git a/packages/cli/src/repowise/cli/ui/openai_compatible.py b/packages/cli/src/repowise/cli/ui/openai_compatible.py
new file mode 100644
index 000000000..d4b736b0a
--- /dev/null
+++ b/packages/cli/src/repowise/cli/ui/openai_compatible.py
@@ -0,0 +1,134 @@
+"""Setup primitives for custom OpenAI-compatible gateways."""
+
+from __future__ import annotations
+
+import os
+from pathlib import Path
+from urllib.parse import urlparse, urlunparse
+
+import click
+from rich.console import Console
+
+from repowise.cli.ui.brand import WARN
+from repowise.cli.ui.env_persistence import _save_key_to_dotenv
+from repowise.core.providers.llm.base import ProviderModelOption
+
+DEFAULT_BASE_URL = "http://localhost:20128/v1"
+
+
+def normalize_base_url(value: str) -> str:
+ """Validate and normalize an OpenAI-compatible HTTP API root."""
+ raw = value.strip()
+ if not raw:
+ raise ValueError("Base URL cannot be empty")
+ if "://" not in raw:
+ raw = f"http://{raw}"
+ try:
+ parsed = urlparse(raw)
+ _ = parsed.port
+ except ValueError as exc:
+ raise ValueError(f"invalid Base URL: {exc}") from exc
+ if parsed.scheme not in {"http", "https"}:
+ raise ValueError("Base URL must use http:// or https://")
+ if not parsed.hostname:
+ raise ValueError("Base URL must include a host")
+ if parsed.username or parsed.password:
+ raise ValueError("put credentials in the API key field, not the Base URL")
+ if parsed.query or parsed.fragment:
+ raise ValueError("Base URL cannot include a query string or fragment")
+ path = parsed.path.rstrip("/")
+ return urlunparse((parsed.scheme.lower(), parsed.netloc, path, "", "", ""))
+
+
+def prompt_setup(
+ console: Console,
+ *,
+ official_base_url: str,
+) -> tuple[str, str]:
+ """Collect a validated endpoint and a non-empty gateway API key."""
+ existing_url = (os.environ.get("OPENAI_BASE_URL") or "").strip()
+ default_url = (
+ existing_url
+ if existing_url.rstrip("/") not in ("", official_base_url)
+ else DEFAULT_BASE_URL
+ )
+ while True:
+ raw_url = click.prompt(
+ " Base URL (OpenAI-compatible endpoint)",
+ default=default_url,
+ show_default=True,
+ )
+ try:
+ base_url = normalize_base_url(raw_url)
+ break
+ except ValueError as exc:
+ console.print(f" [{WARN}]{exc}.[/] Try again.")
+
+ existing_key = (os.environ.get("OPENAI_API_KEY") or "").strip()
+ while True:
+ label = (
+ " API key (hidden; Enter keeps the current key)"
+ if existing_key
+ else " API key (hidden)"
+ )
+ entered = click.prompt(
+ label,
+ default="",
+ hide_input=True,
+ show_default=False,
+ ).strip()
+ api_key = entered or existing_key
+ if api_key:
+ break
+ console.print(f" [{WARN}]An API key is required.[/] Use any gateway token it accepts.")
+
+ os.environ["OPENAI_BASE_URL"] = base_url
+ os.environ["OPENAI_API_KEY"] = api_key
+ return base_url, api_key
+
+
+def discover_models(
+ repo_path: Path | None,
+ *,
+ fallback_model: str,
+) -> tuple[ProviderModelOption, ...]:
+ """Use the core adapter's strict discovery path for an actionable check."""
+ from repowise.cli.helpers import resolve_provider
+ from repowise.core.providers.llm.openai import OpenAIProvider
+
+ provider = resolve_provider("openai", fallback_model, repo_path)
+ if not isinstance(provider, OpenAIProvider):
+ raise TypeError("the openai provider did not resolve to OpenAIProvider")
+ return provider.discover_model_options()
+
+
+def persist_setup(
+ console: Console,
+ repo_path: Path | None,
+ *,
+ base_url: str,
+ api_key: str,
+ save_key: bool,
+) -> None:
+ """Persist the endpoint and, with consent, its secret after setup succeeds."""
+ if repo_path is None:
+ return
+ _save_key_to_dotenv(repo_path, "OPENAI_BASE_URL", base_url)
+ console.print(" [green]✓[/green] Saved endpoint to .repowise/.env")
+
+ from repowise.cli.helpers import NO_SAVE_KEY_ENV
+
+ if not save_key:
+ os.environ[NO_SAVE_KEY_ENV] = "1"
+ console.print(" [dim]API key kept for this process only (--no-save-key).[/dim]")
+ return
+ save = click.confirm(
+ " Save the API key to .repowise/.env for future runs? (gitignored, owner-only)",
+ default=True,
+ )
+ if save:
+ _save_key_to_dotenv(repo_path, "OPENAI_API_KEY", api_key)
+ console.print(" [green]✓[/green] Saved API key to .repowise/.env")
+ else:
+ os.environ[NO_SAVE_KEY_ENV] = "1"
+ console.print(" [dim]API key kept for this process only.[/dim]")
diff --git a/packages/cli/src/repowise/cli/ui/provider_selection.py b/packages/cli/src/repowise/cli/ui/provider_selection.py
index ae4f0cda8..6cbc67229 100644
--- a/packages/cli/src/repowise/cli/ui/provider_selection.py
+++ b/packages/cli/src/repowise/cli/ui/provider_selection.py
@@ -17,6 +17,15 @@ from rich.table import Table
from repowise.cli.ui.brand import BRAND, BRAND_STYLE, OK, VALUE, WARN
from repowise.cli.ui.env_persistence import _save_key_to_dotenv
+from repowise.cli.ui.openai_compatible import (
+ discover_models as _discover_openai_compatible_models,
+)
+from repowise.cli.ui.openai_compatible import (
+ persist_setup as _persist_openai_compatible_setup,
+)
+from repowise.cli.ui.openai_compatible import (
+ prompt_setup as _prompt_openai_compatible_setup_values,
+)
from repowise.core.providers.llm.base import ProviderModelOption
from repowise.core.reasoning import ReasoningMode, normalize_reasoning
@@ -86,6 +95,16 @@ _PROVIDER_NOTES: dict[str, str] = {
}
_OLLAMA_DEFAULT_BASE_URL = "http://localhost:11434"
+# The OpenAI adapter is also the generic adapter for local gateways. Keep the
+# official endpoint as the prompt default, while letting a user replace it
+# inline with a vLLM/SGLang/9router URL without exporting another env var first.
+_OPENAI_DEFAULT_BASE_URL = "https://api.openai.com/v1"
+_OPENAI_COMPATIBLE_CHOICE = "openai_compatible"
+_PROVIDER_CHOICES = tuple(
+ choice
+ for provider in _PROVIDER_ENV
+ for choice in ((provider, _OPENAI_COMPATIBLE_CHOICE) if provider == "openai" else (provider,))
+)
# Enough for a loopback connect; the table renders before any prompt, so a slow
# or firewalled endpoint must not hold the whole screen.
_OLLAMA_PROBE_TIMEOUT_S = 0.3
@@ -187,6 +206,8 @@ def _detect_provider_status() -> dict[str, str]:
truthiness is load-bearing; the value is for debugging.
"""
status: dict[str, str] = {}
+ openai_base_url = (os.environ.get("OPENAI_BASE_URL") or "").rstrip("/")
+ openai_has_key = bool((os.environ.get("OPENAI_API_KEY") or "").strip())
for prov, env_var in _PROVIDER_ENV.items():
if prov == "gemini":
if os.environ.get("GEMINI_API_KEY") or os.environ.get("GOOGLE_API_KEY"):
@@ -204,6 +225,11 @@ def _detect_provider_status() -> dict[str, str]:
elif prov == "ollama":
if _detect_ollama_status():
status[prov] = ollama_base_url()
+ elif prov == "openai":
+ if openai_has_key and openai_base_url in ("", _OPENAI_DEFAULT_BASE_URL):
+ status[prov] = env_var
+ elif openai_has_key and openai_base_url:
+ status[_OPENAI_COMPATIBLE_CHOICE] = env_var
elif os.environ.get(env_var):
status[prov] = env_var
return status
@@ -300,12 +326,13 @@ def _interactive_provider_name(
model_flag: str | None,
*,
repo_path: Path | None = None,
+ save_key: bool = True,
) -> str:
"""Show provider table, handle selection + inline key entry + save.
Returns the chosen provider name.
"""
- providers = list(_PROVIDER_ENV.keys()) # gemini first
+ providers = list(_PROVIDER_CHOICES) # gemini first
detected = _detect_provider_status()
# --- provider table ---
@@ -325,11 +352,17 @@ def _interactive_provider_name(
# dim note beside the name, so the Status column can be read at a glance
# instead of decoded from three phrasings and two colours.
for idx, prov in enumerate(providers, 1):
+ runtime_provider = "openai" if prov == _OPENAI_COMPATIBLE_CHOICE else prov
ready = prov in detected
status_text = f"[{OK}]✓ ready[/]" if ready else "[dim]✗ not set up[/dim]"
- note = _PROVIDER_NOTES.get(prov, "")
- label = f"{prov} [dim]({note})[/dim]" if note else prov
- table.add_row(f"[{idx}]", label, status_text, _PROVIDER_DEFAULTS.get(prov, ""))
+ if prov == _OPENAI_COMPATIBLE_CHOICE:
+ label = "OpenAI-compatible [dim](Custom / local gateway)[/dim]"
+ default_model = "discover from /models"
+ else:
+ note = _PROVIDER_NOTES.get(prov, "")
+ label = f"{prov} [dim]({note})[/dim]" if note else prov
+ default_model = _PROVIDER_DEFAULTS.get(runtime_provider, "")
+ table.add_row(f"[{idx}]", label, status_text, default_model)
console.print()
console.print(table)
@@ -355,8 +388,19 @@ def _interactive_provider_name(
)
chosen = providers[int(chosen_idx) - 1]
+ if chosen == _OPENAI_COMPATIBLE_CHOICE:
+ return chosen
+ if chosen == "openai":
+ # The official and custom rows intentionally share the core adapter.
+ # Pin the official row so a previously saved custom endpoint cannot
+ # silently redirect an OpenAI selection back to the local gateway.
+ os.environ["OPENAI_BASE_URL"] = _OPENAI_DEFAULT_BASE_URL
+
# --- inline API key entry if missing ---
- if chosen not in detected:
+ has_official_openai_key = chosen == "openai" and bool(
+ (os.environ.get("OPENAI_API_KEY") or "").strip()
+ )
+ if chosen not in detected and not has_official_openai_key:
setup_lines = _LOCAL_PROVIDER_SETUP.get(chosen)
if setup_lines is not None:
# Nothing to paste: these authenticate out of band or run locally,
@@ -364,7 +408,12 @@ def _interactive_provider_name(
console.print()
for line in setup_lines():
console.print(line)
- return _interactive_provider_name(console, model_flag, repo_path=repo_path)
+ return _interactive_provider_name(
+ console,
+ model_flag,
+ repo_path=repo_path,
+ save_key=save_key,
+ )
env_var = _PROVIDER_ENV[chosen]
signup_url = _PROVIDER_SIGNUP.get(chosen, "")
console.print()
@@ -372,11 +421,24 @@ def _interactive_provider_name(
if signup_url:
console.print(f" Get your API key here: [{BRAND}]{signup_url}[/]")
console.print()
- key = _prompt_api_key(console, chosen, env_var, repo_path=repo_path)
+ key = _prompt_api_key(
+ console,
+ chosen,
+ env_var,
+ repo_path=repo_path,
+ save_key=save_key,
+ )
if not key:
console.print(f" [{WARN}]Skipped. Please select another provider.[/]")
- return _interactive_provider_name(console, model_flag, repo_path=repo_path)
+ return _interactive_provider_name(
+ console,
+ model_flag,
+ repo_path=repo_path,
+ save_key=save_key,
+ )
+ if chosen == "openai" and repo_path is not None:
+ _save_key_to_dotenv(repo_path, "OPENAI_BASE_URL", _OPENAI_DEFAULT_BASE_URL)
return chosen
@@ -620,13 +682,27 @@ def interactive_provider_config_select(
reasoning_flag: str | None = None,
*,
repo_path: Path | None = None,
+ save_key: bool = True,
) -> ProviderSelection:
"""Show provider/model/reasoning selection for interactive init.
Returns a :class:`ProviderSelection` carrying the chosen provider name,
model, and reasoning mode.
"""
- chosen = _interactive_provider_name(console, model_flag, repo_path=repo_path)
+ chosen = _interactive_provider_name(
+ console,
+ model_flag,
+ repo_path=repo_path,
+ save_key=save_key,
+ )
+ if chosen == _OPENAI_COMPATIBLE_CHOICE:
+ return _interactive_openai_compatible_select(
+ console,
+ model_flag,
+ reasoning_flag,
+ repo_path=repo_path,
+ save_key=save_key,
+ )
default_model = _PROVIDER_DEFAULTS.get(chosen, "")
if model_flag:
@@ -740,6 +816,7 @@ def _prompt_api_key(
env_var: str,
*,
repo_path: Path | None = None,
+ save_key: bool = True,
) -> str | None:
"""Prompt for an API key, set env var, and optionally save to .repowise/.env.
@@ -759,7 +836,7 @@ def _prompt_api_key(
console.print(f" [{OK}]✓ Key set for this session[/]")
# Offer to save for future runs
- if repo_path is not None:
+ if repo_path is not None and save_key:
save = click.confirm(
" Save this key to .repowise/.env so future runs find it? "
"(the file is git-ignored, so it will not be committed)",
@@ -777,6 +854,192 @@ def _prompt_api_key(
from repowise.cli.helpers import NO_SAVE_KEY_ENV
os.environ[NO_SAVE_KEY_ENV] = "1"
+ elif repo_path is not None:
+ # Keep --no-save-key visible to the endpoint prompt below too. The
+ # final init persistence pass already honors this flag, but the
+ # interactive prompt writes values immediately.
+ from repowise.cli.helpers import NO_SAVE_KEY_ENV
+
+ os.environ[NO_SAVE_KEY_ENV] = "1"
console.print()
return key
+
+
+def _prompt_openai_base_url(
+ console: Console,
+ *,
+ repo_path: Path | None = None,
+) -> str | None:
+ """Prompt for an optional OpenAI-compatible endpoint and persist it.
+
+ This is deliberately a separate question from the API key: the same
+ ``openai`` provider can target api.openai.com, a local gateway, or a
+ self-hosted server. An existing env value wins, so re-running init never
+ asks a question the user already answered.
+ """
+ env_var = "OPENAI_BASE_URL"
+ current = (os.environ.get(env_var) or "").strip()
+ if current:
+ return current
+
+ label = " Base URL (OpenAI-compatible endpoint)"
+ value = click.prompt(label, default=_OPENAI_DEFAULT_BASE_URL, show_default=True)
+ value = value.strip()
+ if not value:
+ return None
+
+ os.environ[env_var] = value
+ if repo_path is not None:
+ # --no-save-key governs secrets, not the endpoint needed to recreate
+ # this provider. Base URLs are non-secret and remain repo-local.
+ _save_key_to_dotenv(repo_path, env_var, value)
+ console.print(f" [{OK}]✓ Saved {env_var} to .repowise/.env[/]")
+ console.print()
+ return value
+
+
+def _prompt_exact_model(console: Console, *, default: str = "") -> ProviderModelOption:
+ while True:
+ model = Prompt.ask(
+ " Exact model id",
+ default=default,
+ show_default=bool(default),
+ console=console,
+ ).strip()
+ if model:
+ return ProviderModelOption(
+ model=model,
+ label=model,
+ reasoning_modes=("auto",),
+ source="fallback",
+ )
+ console.print(f" [{WARN}]Model id cannot be empty.[/]")
+
+
+def _interactive_openai_compatible_select(
+ console: Console,
+ model_flag: str | None,
+ reasoning_flag: str | None,
+ *,
+ repo_path: Path | None,
+ save_key: bool,
+) -> ProviderSelection:
+ """Configure, verify, and select a model from a custom OpenAI gateway."""
+ console.print()
+ console.print(" [bold]OpenAI-compatible custom gateway[/bold]")
+ console.print(" [dim]Works with 9router, vLLM, SGLang, and compatible /v1 APIs.[/dim]")
+
+ while True:
+ base_url, api_key = _prompt_openai_compatible_setup_values(
+ console,
+ official_base_url=_OPENAI_DEFAULT_BASE_URL,
+ )
+ if model_flag:
+ selected = ProviderModelOption(
+ model=model_flag,
+ label=model_flag,
+ reasoning_modes=("auto",),
+ source="fallback",
+ )
+ break
+ try:
+ options = _discover_openai_compatible_models(
+ repo_path,
+ fallback_model=_PROVIDER_DEFAULTS["openai"],
+ )
+ except Exception as exc:
+ console.print(f" [{WARN}]Could not read {base_url}/models:[/] {exc}")
+ console.print(" [1] Retry endpoint or key")
+ console.print(" [2] Enter an exact model id anyway")
+ console.print(" [3] Back to provider selection")
+ action = Prompt.ask(
+ " Continue",
+ choices=["1", "2", "3"],
+ default="1",
+ console=console,
+ )
+ if action == "1":
+ continue
+ if action == "3":
+ return interactive_provider_config_select(
+ console,
+ model_flag,
+ reasoning_flag,
+ repo_path=repo_path,
+ save_key=save_key,
+ )
+ selected = _prompt_exact_model(console)
+ break
+
+ console.print(f" [{OK}]✓ Connected[/] — discovered {len(options):,} model(s).")
+ selected = _select_model_option(
+ console,
+ "openai",
+ options,
+ default_model=options[0].model,
+ repo_path=repo_path,
+ )
+ break
+
+ _persist_openai_compatible_setup(
+ console,
+ repo_path,
+ base_url=base_url,
+ api_key=api_key,
+ save_key=save_key,
+ )
+ reasoning = _select_reasoning_mode(console, selected, reasoning_flag)
+ return ProviderSelection("openai", selected.model, reasoning)
+
+
+def interactive_provider_credentials(
+ console: Console,
+ provider: str,
+ *,
+ repo_path: Path | None = None,
+ save_key: bool = True,
+) -> bool:
+ """Onboard an explicitly selected provider from a terminal.
+
+ Returns ``True`` when a missing credential was supplied, ``False`` when
+ there was nothing this prompt could configure or the user skipped it.
+ The OpenAI provider additionally asks for a base URL, which is the small
+ piece that makes the generic adapter useful for local gateways such as
+ 9router.
+ """
+ from repowise.core.providers.llm.registry import PROVIDER_API_KEY_ENVS
+
+ env_vars = PROVIDER_API_KEY_ENVS.get(provider, ())
+ if not env_vars:
+ return False
+
+ configured = False
+ key_present = any((os.environ.get(name) or "").strip() for name in env_vars)
+ if not key_present:
+ env_var = env_vars[0]
+ console.print()
+ console.print(f" [bold]{provider}[/bold] setup")
+ console.print(f" API key goes in [{VALUE}]{env_var}[/]. It will be hidden while typing.")
+ key = _prompt_api_key(
+ console,
+ provider,
+ env_var,
+ repo_path=repo_path,
+ save_key=save_key,
+ )
+ if not key:
+ return False
+ configured = True
+
+ # The generic OpenAI adapter is the supported path for all compatible
+ # gateways. Hosted OpenAI users can simply press Enter; local users paste
+ # their gateway's /v1 URL here. Ask this when an explicit OpenAI provider
+ # has a key but no endpoint too, so an existing OPENAI_API_KEY does not
+ # force a local-gateway user back to shell configuration.
+ if provider == "openai" and _prompt_openai_base_url(
+ console,
+ repo_path=repo_path,
+ ):
+ configured = True
+ return configured
diff --git a/packages/core/README.md b/packages/core/README.md
index d10ab9afd..a7748d4b7 100644
--- a/packages/core/README.md
+++ b/packages/core/README.md
@@ -65,11 +65,15 @@ AST parsing uses [tree-sitter](https://tree-sitter.github.io/) with one `.scm` q
| Provider | Name | Notes |
|----------|------|-------|
| Anthropic | `anthropic` | Prompt caching on shared system/repo context (large-init cost reduction) |
-| OpenAI | `openai` | Any OpenAI-compatible endpoint |
+| OpenAI | `openai` | OpenAI or any OpenAI-compatible endpoint, including local gateways |
| Ollama | `ollama` | Fully offline, no API key required |
| LiteLLM | `litellm` | 100+ providers through one interface (optional dependency) |
| Mock | `mock` | In-memory stub for tests — no network calls |
+The `openai` adapter accepts custom `OPENAI_BASE_URL` values and discovers
+model ids from compatible `/models` endpoints, so local gateways such as
+9router can be used without a provider-specific adapter.
+
```python
from repowise.core.providers import get_provider
diff --git a/packages/core/src/repowise/core/providers/llm/openai.py b/packages/core/src/repowise/core/providers/llm/openai.py
index 80bcf9a35..b59a21bf9 100644
--- a/packages/core/src/repowise/core/providers/llm/openai.py
+++ b/packages/core/src/repowise/core/providers/llm/openai.py
@@ -13,9 +13,11 @@ Recommended models (as of 2026):
from __future__ import annotations
import contextlib
+import ipaddress
import os
from collections.abc import AsyncIterator
from typing import TYPE_CHECKING, Any
+from urllib.parse import urlparse
import structlog
from openai import APIError as _OpenAIAPIError
@@ -178,11 +180,24 @@ def _openai_chat_tool_kwargs(model: str, *, has_tools: bool) -> dict[str, Any]:
return {}
-def _is_openai_text_model(model_id: str) -> bool:
+def _is_openai_text_model(model_id: str, *, allow_namespaced: bool = False) -> bool:
+ """Keep chat-capable ids, including arbitrary ids from custom gateways."""
leaf = _model_leaf(model_id)
if any(marker in leaf for marker in _OPENAI_NON_TEXT_MARKERS):
return False
- return leaf.startswith(_OPENAI_TEXT_MODEL_PREFIXES)
+ return allow_namespaced or leaf.startswith(_OPENAI_TEXT_MODEL_PREFIXES)
+
+
+def _is_loopback_url(base_url: str) -> bool:
+ host = urlparse(base_url).hostname
+ if not host:
+ return False
+ if host.lower() == "localhost":
+ return True
+ try:
+ return ipaddress.ip_address(host).is_loopback
+ except ValueError:
+ return False
def _openai_option(
@@ -216,20 +231,31 @@ def _openai_model_options(
reasoning_modes=("auto", *_openai_supported_reasoning_modes(fallback_model)),
)
try:
- import httpx
-
- response = httpx.get(
- f"{base_url.rstrip('/')}/models",
- headers={"Authorization": f"Bearer {api_key}"},
- timeout=5.0,
- )
- response.raise_for_status()
- data = response.json().get("data", [])
+ return _discover_openai_model_options(api_key, base_url, fallback_model)
except Exception:
return (fallback,)
- if not isinstance(data, list):
- return (fallback,)
+
+def _discover_openai_model_options(
+ api_key: str,
+ base_url: str,
+ fallback_model: str,
+) -> tuple[ProviderModelOption, ...]:
+ """Fetch model ids, raising when an endpoint cannot prove it is usable."""
+ import httpx
+
+ request_kwargs: dict[str, Any] = {
+ "headers": {"Authorization": f"Bearer {api_key}"},
+ "timeout": 5.0,
+ }
+ if _is_loopback_url(base_url):
+ request_kwargs["trust_env"] = False
+ response = httpx.get(f"{base_url.rstrip('/')}/models", **request_kwargs)
+ response.raise_for_status()
+ payload = response.json()
+ if not isinstance(payload, dict) or not isinstance(payload.get("data"), list):
+ raise ValueError("the /models response does not contain a data list")
+ data = payload["data"]
model_ids = sorted(
{
@@ -237,11 +263,14 @@ def _openai_model_options(
for model in data
if isinstance(model, dict)
and isinstance(model.get("id"), str)
- and _is_openai_text_model(model["id"])
+ and _is_openai_text_model(
+ model["id"],
+ allow_namespaced=base_url.rstrip("/") != "https://api.openai.com/v1",
+ )
}
)
if not model_ids:
- return (fallback,)
+ raise ValueError("the /models response contains no text-generation models")
return tuple(_openai_option(model_id, fallback_model=fallback_model) for model_id in model_ids)
@@ -273,7 +302,16 @@ class OpenAIProvider(BaseProvider):
resolved_base_url = base_url or os.environ.get("OPENAI_BASE_URL")
self._api_key = resolved_key
self._base_url = resolved_base_url or "https://api.openai.com/v1"
- self._client = AsyncOpenAI(api_key=resolved_key, base_url=resolved_base_url)
+ http_client = None
+ if resolved_base_url and _is_loopback_url(resolved_base_url):
+ import httpx
+
+ http_client = httpx.AsyncClient(trust_env=False)
+ self._client = AsyncOpenAI(
+ api_key=resolved_key,
+ base_url=resolved_base_url,
+ http_client=http_client,
+ )
self._model = model
self._rate_limiter = rate_limiter
self._cost_tracker = cost_tracker
@@ -292,6 +330,10 @@ class OpenAIProvider(BaseProvider):
def available_model_options(self) -> tuple[ProviderModelOption, ...]:
return _openai_model_options(self._api_key, self._base_url, self._model)
+ def discover_model_options(self) -> tuple[ProviderModelOption, ...]:
+ """Return live `/models` options or raise an actionable endpoint error."""
+ return _discover_openai_model_options(self._api_key, self._base_url, self._model)
+
async def generate(
self,
system_prompt: str,
diff --git a/packages/core/src/repowise/core/upgrade/_data/CHANGELOG.md b/packages/core/src/repowise/core/upgrade/_data/CHANGELOG.md
index 05b94d8fe..080f27442 100644
--- a/packages/core/src/repowise/core/upgrade/_data/CHANGELOG.md
+++ b/packages/core/src/repowise/core/upgrade/_data/CHANGELOG.md
@@ -117,6 +117,18 @@ The headline this cycle is test intelligence that needs no coverage report. 0.44
or a bar. The file page names the tests that reach it, with `via` separating a
test whose calls run into the file from one that only imports it. (#1758)
+- **OpenAI-compatible local gateways expose their models.** Custom endpoints
+ configured with `OPENAI_BASE_URL` can now return namespaced model ids such as
+ 9router's `ag/...` and `ds/...` entries; loopback requests also bypass proxy
+ environment parsing so a standard IPv6 `NO_PROXY` entry cannot block them.
+
+- **Custom OpenAI-compatible setup is a first-class init flow.** The provider
+ menu separates official OpenAI from custom/local gateways, validates the
+ endpoint, hides and optionally saves the key, verifies `/models`, supports
+ model search plus exact-id fallback, and offers retry/manual/back recovery.
+ `--no-save-key` persists the non-secret endpoint without persisting the key;
+ CI, `--yes`, and non-TTY runs remain non-interactive.
+
- **`untested_hotspot` stops accusing files that the tests run.** With no
coverage ingested it fired on any hotspot without a *paired test file*, which
is a filename convention, so a suite that names its tests for behaviour
diff --git a/tests/unit/cli/test_helpers.py b/tests/unit/cli/test_helpers.py
index 61e639d9c..41fe1a261 100644
--- a/tests/unit/cli/test_helpers.py
+++ b/tests/unit/cli/test_helpers.py
@@ -816,10 +816,50 @@ class TestResolveProviderOrPrompt:
helpers.resolve_provider_or_prompt(None, None, tmp_path, interactive=False)
assert called["prompt"] == 0
+ def test_explicit_provider_prompt_resolves_after_setup(self, tmp_path, monkeypatch):
+ """An explicit provider gets inline credential setup only when interactive."""
+ import click
+
+ from repowise.cli import helpers
+
+ sentinel = object()
+ calls = {"resolve": 0, "prompt": 0}
+
+ def _resolve(provider_name, model, repo_path=None):
+ calls["resolve"] += 1
+ if calls["resolve"] == 1:
+ raise click.ClickException("Provider 'openai' requires a key")
+ assert provider_name == "anthropic"
+ assert model == "ag/gemini-3.7-flash-medium"
+ assert repo_path == tmp_path
+ return sentinel
+
+ monkeypatch.setattr(helpers, "resolve_provider", _resolve)
+
+ def _credentials(console, provider, *, repo_path=None, save_key=True):
+ calls["prompt"] += 1
+ assert provider == "anthropic"
+ assert repo_path == tmp_path
+ assert save_key is True
+ return True
+
+ monkeypatch.setattr(
+ "repowise.cli.ui.provider_selection.interactive_provider_credentials",
+ _credentials,
+ )
+
+ result = helpers.resolve_explicit_provider_or_prompt(
+ "anthropic",
+ "ag/gemini-3.7-flash-medium",
+ tmp_path,
+ interactive=True,
+ )
+
+ assert result is sentinel
+ assert calls == {"resolve": 2, "prompt": 1}
+
@pytest.mark.parametrize("exc_name", ["EOFError", "Abort"])
- def test_unanswerable_prompt_falls_back_to_clean_error(
- self, tmp_path, monkeypatch, exc_name
- ):
+ def test_unanswerable_prompt_falls_back_to_clean_error(self, tmp_path, monkeypatch, exc_name):
"""A tty that lies: the prompt hits EOF/Abort, so we surface the clean
actionable error instead of a bare 'Aborted!' — the agent-safe path."""
import click
diff --git a/tests/unit/cli/test_init_ux.py b/tests/unit/cli/test_init_ux.py
index cbd58744a..5c56dc27b 100644
--- a/tests/unit/cli/test_init_ux.py
+++ b/tests/unit/cli/test_init_ux.py
@@ -7,6 +7,7 @@ detection that decides whether picking a provider asks for an API key.
from __future__ import annotations
+import os
from io import StringIO
from typing import Any
@@ -276,7 +277,7 @@ def test_selecting_an_unreachable_ollama_never_prompts_for_a_key(monkeypatch: An
monkeypatch.setattr(provider_selection, "_detect_provider_status", detect)
- providers = list(provider_selection._PROVIDER_ENV)
+ providers = list(provider_selection._PROVIDER_CHOICES)
answers = iter([str(providers.index("ollama") + 1), str(providers.index("openai") + 1)])
monkeypatch.setattr(provider_selection.Prompt, "ask", lambda *a, **kw: next(answers))
@@ -288,3 +289,221 @@ def test_selecting_an_unreachable_ollama_never_prompts_for_a_key(monkeypatch: An
assert chosen == "openai"
assert "runs on your machine" in out
assert "ollama serve" in out
+
+
+def test_explicit_openai_setup_prompts_for_key_and_gateway_url(
+ monkeypatch: Any, tmp_path: Any
+) -> None:
+ """``init --provider openai`` can onboard a local gateway inline.
+
+ The generic OpenAI adapter is how 9router and other compatible gateways
+ are configured. A user should not have to discover and export two env vars
+ before the command can even start.
+ """
+ # Use tracked empty values rather than delenv: when the variables are
+ # initially absent, MonkeyPatch cannot restore direct os.environ writes
+ # made by the production prompt, which would leak into later tests.
+ monkeypatch.setenv("OPENAI_API_KEY", "")
+ monkeypatch.setenv("OPENAI_BASE_URL", "")
+ answers = iter(["router-secret", "http://localhost:20128/v1"])
+ monkeypatch.setattr(provider_selection.click, "prompt", lambda *_a, **_k: next(answers))
+ monkeypatch.setattr(provider_selection.click, "confirm", lambda *_a, **_k: True)
+ console, buf = _console()
+
+ configured = provider_selection.interactive_provider_credentials(
+ console,
+ "openai",
+ repo_path=tmp_path,
+ )
+
+ assert configured is True
+ assert os.environ["OPENAI_API_KEY"] == "router-secret"
+ assert os.environ["OPENAI_BASE_URL"] == "http://localhost:20128/v1"
+ env_text = (tmp_path / ".repowise" / ".env").read_text(encoding="utf-8")
+ assert "OPENAI_API_KEY=router-secret" in env_text
+ assert "OPENAI_BASE_URL=http://localhost:20128/v1" in env_text
+ assert "router-secret" not in buf.getvalue()
+
+
+def test_explicit_openai_setup_prompts_for_url_when_key_is_already_set(
+ monkeypatch: Any, tmp_path: Any
+) -> None:
+ """A pre-exported key must not hide the local-gateway URL question."""
+ monkeypatch.setenv("OPENAI_API_KEY", "router-secret")
+ monkeypatch.setenv("OPENAI_BASE_URL", "")
+ monkeypatch.setattr(
+ provider_selection.click,
+ "prompt",
+ lambda *_a, **_k: "http://localhost:20128/v1",
+ )
+ console, _ = _console()
+
+ configured = provider_selection.interactive_provider_credentials(
+ console,
+ "openai",
+ repo_path=tmp_path,
+ )
+
+ assert configured is True
+ assert os.environ["OPENAI_BASE_URL"] == "http://localhost:20128/v1"
+
+
+def test_provider_status_separates_official_and_custom_openai(monkeypatch: Any) -> None:
+ monkeypatch.setenv("OPENAI_API_KEY", "router-secret")
+ monkeypatch.setenv("OPENAI_BASE_URL", "http://localhost:20128/v1")
+ monkeypatch.setattr(provider_selection, "_detect_codex_cli_status", lambda: (False, False))
+ monkeypatch.setattr(provider_selection, "_detect_opencode_status", lambda: False)
+ monkeypatch.setattr(provider_selection, "_detect_ollama_status", lambda: False)
+
+ custom_status = provider_selection._detect_provider_status()
+ assert provider_selection._OPENAI_COMPATIBLE_CHOICE in custom_status
+ assert "openai" not in custom_status
+
+ monkeypatch.setenv("OPENAI_BASE_URL", provider_selection._OPENAI_DEFAULT_BASE_URL)
+ official_status = provider_selection._detect_provider_status()
+ assert "openai" in official_status
+ assert provider_selection._OPENAI_COMPATIBLE_CHOICE not in official_status
+
+
+def test_official_openai_reuses_key_from_previous_custom_setup(
+ monkeypatch: Any, tmp_path: Any
+) -> None:
+ monkeypatch.setenv("OPENAI_API_KEY", "existing-secret")
+ monkeypatch.setenv("OPENAI_BASE_URL", "http://localhost:20128/v1")
+ monkeypatch.setattr(provider_selection, "_detect_codex_cli_status", lambda: (False, False))
+ monkeypatch.setattr(provider_selection, "_detect_opencode_status", lambda: False)
+ monkeypatch.setattr(provider_selection, "_detect_ollama_status", lambda: False)
+ openai_idx = str(provider_selection._PROVIDER_CHOICES.index("openai") + 1)
+ monkeypatch.setattr(provider_selection.Prompt, "ask", lambda *_a, **_k: openai_idx)
+ monkeypatch.setattr(
+ provider_selection,
+ "_prompt_api_key",
+ lambda *_a, **_k: pytest.fail("an existing OpenAI key should be reused"),
+ )
+ console, _ = _console()
+
+ chosen = provider_selection._interactive_provider_name(
+ console,
+ None,
+ repo_path=tmp_path,
+ )
+
+ assert chosen == "openai"
+ assert os.environ["OPENAI_BASE_URL"] == provider_selection._OPENAI_DEFAULT_BASE_URL
+ env_text = (tmp_path / ".repowise" / ".env").read_text(encoding="utf-8")
+ assert f"OPENAI_BASE_URL={provider_selection._OPENAI_DEFAULT_BASE_URL}" in env_text
+ assert "existing-secret" not in env_text
+
+
+def test_custom_gateway_picker_discovers_model_and_persists_setup(
+ monkeypatch: Any, tmp_path: Any
+) -> None:
+ monkeypatch.setenv("OPENAI_API_KEY", "")
+ monkeypatch.setenv("OPENAI_BASE_URL", "")
+ monkeypatch.setattr(provider_selection, "_detect_provider_status", lambda: {})
+ monkeypatch.setattr(
+ provider_selection,
+ "_discover_openai_compatible_models",
+ lambda _repo, **_kwargs: (
+ provider_selection.ProviderModelOption(
+ model="ag/gemini-3.7-flash-medium",
+ reasoning_modes=("auto",),
+ recommended=True,
+ source="api",
+ ),
+ provider_selection.ProviderModelOption(
+ model="ds/deepseek-v4-flash",
+ reasoning_modes=("auto",),
+ source="api",
+ ),
+ ),
+ )
+ provider_idx = str(
+ provider_selection._PROVIDER_CHOICES.index(provider_selection._OPENAI_COMPATIBLE_CHOICE) + 1
+ )
+ menu_answers = iter([provider_idx, "1"])
+ prompt_answers = iter(["localhost:20128/v1", "router-secret"])
+ monkeypatch.setattr(provider_selection.Prompt, "ask", lambda *_a, **_k: next(menu_answers))
+ monkeypatch.setattr(provider_selection.click, "prompt", lambda *_a, **_k: next(prompt_answers))
+ monkeypatch.setattr(provider_selection.click, "confirm", lambda *_a, **_k: True)
+ console, buf = _console()
+
+ result = provider_selection.interactive_provider_config_select(
+ console,
+ None,
+ repo_path=tmp_path,
+ )
+
+ assert result.provider_name == "openai"
+ assert result.model == "ag/gemini-3.7-flash-medium"
+ assert os.environ["OPENAI_BASE_URL"] == "http://localhost:20128/v1"
+ env_text = (tmp_path / ".repowise" / ".env").read_text(encoding="utf-8")
+ assert "OPENAI_BASE_URL=http://localhost:20128/v1" in env_text
+ assert "OPENAI_API_KEY=router-secret" in env_text
+ assert "router-secret" not in buf.getvalue()
+ assert "discovered 2 model(s)" in buf.getvalue()
+ assert "OpenAI-compatible" in buf.getvalue()
+
+
+def test_custom_gateway_picker_allows_manual_model_when_discovery_fails(
+ monkeypatch: Any, tmp_path: Any
+) -> None:
+ monkeypatch.setenv("REPOWISE_NO_SAVE_KEY", "")
+ monkeypatch.setenv("OPENAI_API_KEY", "")
+ monkeypatch.setenv("OPENAI_BASE_URL", "")
+ monkeypatch.setattr(provider_selection, "_detect_provider_status", lambda: {})
+ monkeypatch.setattr(
+ provider_selection,
+ "_discover_openai_compatible_models",
+ lambda _repo, **_kwargs: (_ for _ in ()).throw(RuntimeError("401 Unauthorized")),
+ )
+ provider_idx = str(
+ provider_selection._PROVIDER_CHOICES.index(provider_selection._OPENAI_COMPATIBLE_CHOICE) + 1
+ )
+ menu_answers = iter([provider_idx, "2", "ag/manual-model"])
+ prompt_answers = iter(["http://localhost:20128/v1", "router-secret"])
+ monkeypatch.setattr(provider_selection.Prompt, "ask", lambda *_a, **_k: next(menu_answers))
+ monkeypatch.setattr(provider_selection.click, "prompt", lambda *_a, **_k: next(prompt_answers))
+ monkeypatch.setattr(provider_selection.click, "confirm", lambda *_a, **_k: False)
+ console, buf = _console()
+
+ result = provider_selection.interactive_provider_config_select(
+ console,
+ None,
+ repo_path=tmp_path,
+ )
+
+ assert result == provider_selection.ProviderSelection("openai", "ag/manual-model", "auto")
+ assert "401 Unauthorized" in buf.getvalue()
+ env_text = (tmp_path / ".repowise" / ".env").read_text(encoding="utf-8")
+ assert "OPENAI_BASE_URL=http://localhost:20128/v1" in env_text
+ assert "router-secret" not in env_text
+
+
+def test_custom_gateway_no_save_key_still_persists_endpoint(
+ monkeypatch: Any, tmp_path: Any
+) -> None:
+ monkeypatch.setenv("REPOWISE_NO_SAVE_KEY", "")
+ monkeypatch.setenv("OPENAI_API_KEY", "")
+ monkeypatch.setenv("OPENAI_BASE_URL", "")
+ monkeypatch.setattr(provider_selection, "_detect_provider_status", lambda: {})
+ provider_idx = str(
+ provider_selection._PROVIDER_CHOICES.index(provider_selection._OPENAI_COMPATIBLE_CHOICE) + 1
+ )
+ menu_answers = iter([provider_idx])
+ prompt_answers = iter(["http://localhost:20128/v1", "router-secret"])
+ monkeypatch.setattr(provider_selection.Prompt, "ask", lambda *_a, **_k: next(menu_answers))
+ monkeypatch.setattr(provider_selection.click, "prompt", lambda *_a, **_k: next(prompt_answers))
+ console, _ = _console()
+
+ result = provider_selection.interactive_provider_config_select(
+ console,
+ "ag/gemini-3.7-flash-medium",
+ repo_path=tmp_path,
+ save_key=False,
+ )
+
+ assert result.model == "ag/gemini-3.7-flash-medium"
+ env_text = (tmp_path / ".repowise" / ".env").read_text(encoding="utf-8")
+ assert "OPENAI_BASE_URL=http://localhost:20128/v1" in env_text
+ assert "OPENAI_API_KEY" not in env_text
diff --git a/tests/unit/cli/test_openai_compatible.py b/tests/unit/cli/test_openai_compatible.py
new file mode 100644
index 000000000..daa4545fa
--- /dev/null
+++ b/tests/unit/cli/test_openai_compatible.py
@@ -0,0 +1,124 @@
+"""Focused tests for custom OpenAI-compatible gateway setup primitives."""
+
+from __future__ import annotations
+
+import os
+from io import StringIO
+from typing import Any
+
+import pytest
+from rich.console import Console
+
+from repowise.cli.helpers import NO_SAVE_KEY_ENV
+from repowise.cli.ui import openai_compatible
+
+
+def _console() -> tuple[Console, StringIO]:
+ output = StringIO()
+ return Console(file=output, force_terminal=False, width=100), output
+
+
+ ("raw", "expected"),
+ [
+ ("localhost:20128/v1/", "http://localhost:20128/v1"),
+ ("https://gateway.example/openai/v1", "https://gateway.example/openai/v1"),
+ (" HTTP://LOCALHOST:20128/v1/ ", "http://LOCALHOST:20128/v1"),
+ ],
+)
+def test_normalize_base_url_accepts_supported_endpoints(raw: str, expected: str) -> None:
+ assert openai_compatible.normalize_base_url(raw) == expected
+
+
+ "raw",
+ [
+ "",
+ "ftp://localhost/v1",
+ "http://",
+ "http://localhost:not-a-port/v1",
+ "https://user:[email protected]/v1",
+ "http://x/v1?q=1",
+ "http://x/v1#fragment",
+ ],
+)
+def test_normalize_base_url_rejects_unsafe_or_invalid_values(raw: str) -> None:
+ with pytest.raises(ValueError):
+ openai_compatible.normalize_base_url(raw)
+
+
+def test_prompt_setup_retries_invalid_endpoint_and_empty_key(monkeypatch: Any) -> None:
+ monkeypatch.setenv("OPENAI_BASE_URL", "")
+ monkeypatch.setenv("OPENAI_API_KEY", "")
+ answers = iter(["ftp://router/v1", "localhost:20128/v1", "", "router-secret"])
+ monkeypatch.setattr(openai_compatible.click, "prompt", lambda *_a, **_k: next(answers))
+ console, output = _console()
+
+ base_url, api_key = openai_compatible.prompt_setup(
+ console,
+ official_base_url="https://api.openai.com/v1",
+ )
+
+ assert base_url == "http://localhost:20128/v1"
+ assert api_key == "router-secret"
+ assert os.environ["OPENAI_BASE_URL"] == base_url
+ assert os.environ["OPENAI_API_KEY"] == api_key
+ assert "must use http:// or https://" in output.getvalue()
+ assert "API key is required" in output.getvalue()
+ assert "router-secret" not in output.getvalue()
+
+
+def test_prompt_setup_can_reuse_existing_key(monkeypatch: Any) -> None:
+ monkeypatch.setenv("OPENAI_BASE_URL", "http://localhost:20128/v1")
+ monkeypatch.setenv("OPENAI_API_KEY", "existing-secret")
+ answers = iter(["http://localhost:20128/v1", ""])
+ monkeypatch.setattr(openai_compatible.click, "prompt", lambda *_a, **_k: next(answers))
+ console, _ = _console()
+
+ _, api_key = openai_compatible.prompt_setup(
+ console,
+ official_base_url="https://api.openai.com/v1",
+ )
+
+ assert api_key == "existing-secret"
+
+
+def test_persist_setup_saves_endpoint_and_key_with_consent(monkeypatch: Any, tmp_path: Any) -> None:
+ monkeypatch.setenv(NO_SAVE_KEY_ENV, "")
+ monkeypatch.setattr(openai_compatible.click, "confirm", lambda *_a, **_k: True)
+ console, _ = _console()
+
+ openai_compatible.persist_setup(
+ console,
+ tmp_path,
+ base_url="http://localhost:20128/v1",
+ api_key="router-secret",
+ save_key=True,
+ )
+
+ env_file = tmp_path / ".repowise" / ".env"
+ contents = env_file.read_text(encoding="utf-8")
+ assert "OPENAI_BASE_URL=http://localhost:20128/v1" in contents
+ assert "OPENAI_API_KEY=router-secret" in contents
+ assert env_file.stat().st_mode & 0o777 == 0o600
+
+
+def test_persist_setup_no_save_key_keeps_secret_out_of_repo(
+ monkeypatch: Any, tmp_path: Any
+) -> None:
+ monkeypatch.setenv(NO_SAVE_KEY_ENV, "")
+ console, output = _console()
+
+ openai_compatible.persist_setup(
+ console,
+ tmp_path,
+ base_url="http://localhost:20128/v1",
+ api_key="router-secret",
+ save_key=False,
+ )
+
+ contents = (tmp_path / ".repowise" / ".env").read_text(encoding="utf-8")
+ assert "OPENAI_BASE_URL=http://localhost:20128/v1" in contents
+ assert "router-secret" not in contents
+ assert os.environ[NO_SAVE_KEY_ENV] == "1"
+ assert "router-secret" not in output.getvalue()
diff --git a/tests/unit/test_providers/test_openai_provider.py b/tests/unit/test_providers/test_openai_provider.py
index 587fe144f..7ab7a0514 100644
--- a/tests/unit/test_providers/test_openai_provider.py
+++ b/tests/unit/test_providers/test_openai_provider.py
@@ -76,10 +76,13 @@ def test_gpt56_family_ladder_drops_minimal_and_max():
).supported_reasoning_modes() == ("auto", "none", "low", "medium", "high", "xhigh")
# The generic gpt-5 branch is a prefix of gpt-5.6 and must not win.
- assert "minimal" in OpenAIProvider(
- api_key="sk-test",
- model="gpt-5.4-nano",
- ).supported_reasoning_modes()
+ assert (
+ "minimal"
+ in OpenAIProvider(
+ api_key="sk-test",
+ model="gpt-5.4-nano",
+ ).supported_reasoning_modes()
+ )
def test_gpt54_model():
@@ -126,6 +129,47 @@ def test_available_model_options_uses_models_endpoint(monkeypatch):
)
+def test_available_model_options_accepts_namespaced_compatible_models(monkeypatch):
+ class FakeResponse:
+ def raise_for_status(self) -> None:
+ pass
+
+ def json(self) -> dict:
+ return {
+ "data": [
+ {"id": "ag/gemini-3.7-flash-medium"},
+ {"id": "ds/deepseek-v4-flash"},
+ {"id": "cmc/moonshotai/Kimi-K2.6"},
+ {"id": "text-embedding-3-large"},
+ ]
+ }
+
+ captured: dict[str, object] = {}
+
+ def fake_get(url, **kwargs):
+ captured["url"] = url
+ captured.update(kwargs)
+ return FakeResponse()
+
+ monkeypatch.setattr("httpx.get", fake_get)
+
+ options = OpenAIProvider(
+ api_key="sk-test",
+ base_url="http://localhost:20128/v1",
+ ).available_model_options()
+ models = [option.model for option in options]
+
+ assert models == [
+ "ag/gemini-3.7-flash-medium",
+ "cmc/moonshotai/Kimi-K2.6",
+ "ds/deepseek-v4-flash",
+ ]
+ assert captured["trust_env"] is False
+ assert next(
+ option for option in options if option.model == "ag/gemini-3.7-flash-medium"
+ ).reasoning_modes == ("auto",)
+
+
def test_available_model_options_falls_back_to_configured_model(monkeypatch):
def fake_get(*_args, **_kwargs):
raise RuntimeError("offline")
@@ -139,6 +183,38 @@ def test_available_model_options_falls_back_to_configured_model(monkeypatch):
assert options[0].source == "fallback"
+def test_discover_model_options_surfaces_endpoint_errors(monkeypatch):
+ def fake_get(*_args, **_kwargs):
+ raise RuntimeError("gateway unavailable")
+
+ monkeypatch.setattr("httpx.get", fake_get)
+
+ provider = OpenAIProvider(
+ api_key="sk-test",
+ base_url="http://localhost:20128/v1",
+ )
+ with pytest.raises(RuntimeError, match="gateway unavailable"):
+ provider.discover_model_options()
+
+
+def test_discover_model_options_rejects_malformed_payload(monkeypatch):
+ class FakeResponse:
+ def raise_for_status(self) -> None:
+ pass
+
+ def json(self) -> dict:
+ return {"models": []}
+
+ monkeypatch.setattr("httpx.get", lambda *_a, **_k: FakeResponse())
+
+ provider = OpenAIProvider(
+ api_key="sk-test",
+ base_url="http://localhost:20128/v1",
+ )
+ with pytest.raises(ValueError, match="data list"):
+ provider.discover_model_options()
+
+
# ---------------------------------------------------------------------------
# Successful generation
# ---------------------------------------------------------------------------