Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
35 changes: 31 additions & 4 deletions src/bub/builtin/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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:
Expand All @@ -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:<model> 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:<model> 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:<model> 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:<model> for ChatGPT plan access.")
25 changes: 25 additions & 0 deletions tests/test_builtin_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions website/src/content/docs/docs/reference/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,17 @@ export BUB_MODEL=codex:<model>

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:<model>` or `BUB_MODEL=github-copilot:<model>`, 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.
Expand Down
11 changes: 11 additions & 0 deletions website/src/content/docs/zh-cn/docs/reference/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ Use BUB_MODEL=codex:<model> for ChatGPT plan access.

使用时设置 `BUB_MODEL=codex:<model>`,不要同时设置 `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:<model>` 或 `BUB_MODEL=github-copilot:<model>`,无需配置 API key。登录命令不会修改模型配置。GitHub Copilot 需要账号具有相应订阅及访问权限。

## `bub plugins` {#bub-plugins}

列出已发现的插件及其包和版本号。
Expand Down
Loading