From b52259b11e43642dddf17f52e04d00efe41c33bc Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Mon, 16 Nov 2026 18:03:02 +0800 Subject: [PATCH] feat: add Grok and GitHub CLI OAuth login --- pyproject.toml | 2 +- src/bub/builtin/auth.py | 35 ++++++++++++++++--- tests/test_builtin_cli.py | 25 +++++++++++++ uv.lock | 2 +- .../src/content/docs/docs/reference/cli.mdx | 11 ++++++ .../content/docs/zh-cn/docs/reference/cli.mdx | 11 ++++++ 6 files changed, 80 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f040919aa..3a3cb66d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,7 @@ classifiers = [ # The agent kernel, bub.core. Everything else (framework, CLI, channels, builtin agent) is in the `app` extra. dependencies = [ - "republic>=0.6.0a1", + "republic>=0.6.0", "pydantic>=2.12.5", "loguru>=0.7.3", "typing-extensions>=4.15.0", diff --git a/src/bub/builtin/auth.py b/src/bub/builtin/auth.py index 63b7a0ffb..2dc0d5648 100644 --- a/src/bub/builtin/auth.py +++ b/src/bub/builtin/auth.py @@ -5,12 +5,14 @@ import asyncio import os import subprocess +from collections.abc import Iterator +from contextlib import contextmanager from pathlib import Path from typing import Annotated import typer from republic.errors import AuthenticationError -from republic.providers import CodexAuth +from republic.providers import CodexAuth, GitHubCLIAuth, GrokAuth app = typer.Typer(name="login", help="Account authentication") @@ -27,7 +29,7 @@ def login_codex( codex_home: Annotated[Path | None, typer.Option("--codex-home", help="Codex credential directory")] = None, ) -> None: """Log in with the Codex CLI using file credential storage.""" - try: + with _login_result(): if codex_home is None: auth = asyncio.run(CodexAuth.login(executable=executable, device_auth=device_auth)) else: @@ -42,9 +44,34 @@ def login_codex( except subprocess.CalledProcessError as exc: raise AuthenticationError(f"Codex login failed (exit {exc.returncode})") from None auth = CodexAuth.from_file(home / "auth.json") + typer.echo(f"account_id: {auth.account_id}") + typer.echo("Use BUB_MODEL=codex: for ChatGPT plan access.") + + +@app.command("grok") +def login_grok( + executable: Annotated[str, typer.Option("--executable", help="Grok CLI executable")] = "grok", + device_auth: Annotated[bool, typer.Option("--device-auth", help="Use device authorization")] = False, +) -> None: + """Log in with the official Grok CLI; Republic maintains its credentials.""" + with _login_result(): + asyncio.run(GrokAuth.login(executable=executable, device_auth=device_auth)) + typer.echo("Use BUB_MODEL=grok: to select this provider.") + + +@app.command("github-copilot") +def login_github_copilot() -> None: + """Log in with GitHub CLI; Republic reuses its credentials.""" + with _login_result(): + asyncio.run(GitHubCLIAuth.login()) + typer.echo("Use BUB_MODEL=github-copilot: to select this provider.") + + +@contextmanager +def _login_result() -> Iterator[None]: + try: + yield except AuthenticationError as exc: typer.secho(f"login: failed: {exc}", fg="red", err=True) raise typer.Exit(1) from None typer.echo("login: ok") - typer.echo(f"account_id: {auth.account_id}") - typer.echo("Use BUB_MODEL=codex: for ChatGPT plan access.") diff --git a/tests/test_builtin_cli.py b/tests/test_builtin_cli.py index d50ba1650..a6e6694ba 100644 --- a/tests/test_builtin_cli.py +++ b/tests/test_builtin_cli.py @@ -638,6 +638,31 @@ def test_login_command_reports_the_authenticated_account( assert "test-token" not in result.stdout +@pytest.mark.parametrize("provider,auth_class", [("grok", "GrokAuth"), ("github-copilot", "GitHubCLIAuth")]) +@pytest.mark.parametrize("fails", [False, True]) +def test_login_reports_success_or_failure( + monkeypatch: pytest.MonkeyPatch, provider: str, auth_class: str, fails: bool +) -> None: + from republic.errors import AuthenticationError + + import bub.builtin.auth as auth + + async def login(**kwargs: Any) -> None: + if fails: + raise AuthenticationError("Login cancelled") + + monkeypatch.setattr(getattr(auth, auth_class), "login", login) + result = CliRunner().invoke(_create_app(), ["login", provider]) + + assert result.exit_code == (1 if fails else 0) + if fails: + assert "login: failed: Login cancelled" in result.stderr + assert "login: ok" not in result.stdout + else: + assert "login: ok" in result.stdout + assert f"BUB_MODEL={provider}:" in result.stdout + + def test_login_rejects_unknown_provider() -> None: result = CliRunner().invoke(_create_app(), ["login", "github"]) diff --git a/uv.lock b/uv.lock index 2c7e257dd..396a65c7b 100644 --- a/uv.lock +++ b/uv.lock @@ -376,7 +376,7 @@ requires-dist = [ { name = "pyyaml", marker = "extra == 'logfire'", specifier = ">=6.0.3" }, { name = "rapidfuzz", marker = "extra == 'app'", specifier = ">=3.14.5" }, { name = "rapidfuzz", marker = "extra == 'logfire'", specifier = ">=3.14.5" }, - { name = "republic", specifier = ">=0.6.0a1" }, + { name = "republic", specifier = ">=0.6.0" }, { name = "rich", marker = "extra == 'app'", specifier = ">=14.3.4" }, { name = "rich", marker = "extra == 'logfire'", specifier = ">=14.3.4" }, { name = "typer", marker = "extra == 'app'", specifier = ">=0.24.1" }, diff --git a/website/src/content/docs/docs/reference/cli.mdx b/website/src/content/docs/docs/reference/cli.mdx index 7afa94665..ff918c96a 100644 --- a/website/src/content/docs/docs/reference/cli.mdx +++ b/website/src/content/docs/docs/reference/cli.mdx @@ -311,6 +311,17 @@ export BUB_MODEL=codex: Do not set `BUB_API_KEY` at the same time. On failure it prints `login: failed: ...` and exits with code `1`. Credentials are usually written to `$CODEX_HOME/auth.json` or `~/.codex/auth.json`. +## Other account logins {#account-logins} + +```bash +bub login grok [--executable grok] [--device-auth] +bub login github-copilot +``` + +Grok uses the official Grok CLI; GitHub Copilot uses `gh auth login`. Install the corresponding CLI first. Republic reuses its credentials and handles token refresh or exchange. Bub adds no credential store. + +Select a model with `BUB_MODEL=grok:` or `BUB_MODEL=github-copilot:`, without setting an API key. Login does not change the configured model. GitHub Copilot access requires an eligible account and subscription. + ## `bub plugins` {#bub-plugins} Prints each discovered plugin with the package and version that provide it. diff --git a/website/src/content/docs/zh-cn/docs/reference/cli.mdx b/website/src/content/docs/zh-cn/docs/reference/cli.mdx index 90ba36ba8..e35bb1974 100644 --- a/website/src/content/docs/zh-cn/docs/reference/cli.mdx +++ b/website/src/content/docs/zh-cn/docs/reference/cli.mdx @@ -236,6 +236,17 @@ Use BUB_MODEL=codex: for ChatGPT plan access. 使用时设置 `BUB_MODEL=codex:`,不要同时设置 `BUB_API_KEY`。失败时输出 `login: failed: ...` 并以退出码 `1` 结束。 +## 其他账号登录 {#account-logins} + +```bash +bub login grok [--executable grok] [--device-auth] +bub login github-copilot +``` + +Grok 使用官方 Grok CLI;GitHub Copilot 使用 `gh auth login`。请先安装对应 CLI。Republic 复用其凭据并处理令牌刷新或交换,Bub 不增加凭据存储。 + +登录后设置 `BUB_MODEL=grok:` 或 `BUB_MODEL=github-copilot:`,无需配置 API key。登录命令不会修改模型配置。GitHub Copilot 需要账号具有相应订阅及访问权限。 + ## `bub plugins` {#bub-plugins} 列出已发现的插件及其包和版本号。