Complete Python CLI for OpenWebUI with: - Auth commands (login, logout, whoami, token, refresh) - Chat send with streaming support - RAG files/collections management - Models list/info - Admin stats (minimal v1.0) - Config management with profiles and keyring Stack: typer, httpx, rich, pydantic, keyring Based on RFC v1.2 with 22-step implementation checklist 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
"""Admin commands (requires admin role)."""
|
|
|
|
import json
|
|
|
|
import typer
|
|
from rich.console import Console
|
|
from rich.table import Table
|
|
|
|
from ..http import create_client, handle_response, handle_request_error
|
|
from ..errors import AuthError
|
|
|
|
app = typer.Typer(no_args_is_help=True)
|
|
console = Console()
|
|
|
|
|
|
@app.command()
|
|
def stats(
|
|
ctx: typer.Context,
|
|
period: str = typer.Option("day", "--period", "-p", help="Period: day, week, month"),
|
|
) -> None:
|
|
"""Show usage statistics."""
|
|
obj = ctx.obj or {}
|
|
|
|
try:
|
|
with create_client(
|
|
profile=obj.get("profile"),
|
|
uri=obj.get("uri"),
|
|
) as client:
|
|
# Try to get stats from various endpoints
|
|
try:
|
|
response = client.get("/api/v1/admin/stats")
|
|
data = handle_response(response)
|
|
except Exception:
|
|
# Fallback to basic info
|
|
response = client.get("/api/v1/auths/")
|
|
user_data = handle_response(response)
|
|
|
|
if user_data.get("role") != "admin":
|
|
raise AuthError(
|
|
f"Admin command requires admin privileges; "
|
|
f"your current user is '{user_data.get('name')}' with role: [{user_data.get('role')}]"
|
|
)
|
|
|
|
# Build basic stats
|
|
data = {
|
|
"user": user_data.get("name"),
|
|
"role": user_data.get("role"),
|
|
"status": "connected",
|
|
}
|
|
|
|
if obj.get("format") == "json":
|
|
console.print(json.dumps(data, indent=2))
|
|
else:
|
|
table = Table(title="Server Statistics")
|
|
table.add_column("Metric", style="cyan")
|
|
table.add_column("Value", style="green")
|
|
|
|
for key, value in data.items():
|
|
table.add_row(str(key), str(value))
|
|
|
|
console.print(table)
|
|
|
|
except AuthError:
|
|
raise
|
|
except Exception as e:
|
|
handle_request_error(e)
|
|
|
|
|
|
@app.command()
|
|
def users(ctx: typer.Context) -> None:
|
|
"""List users (v1.1 feature - placeholder)."""
|
|
console.print("[yellow]Admin users will be available in v1.1[/yellow]")
|
|
|
|
|
|
@app.command()
|
|
def config(ctx: typer.Context) -> None:
|
|
"""Server configuration (v1.1 feature - placeholder)."""
|
|
console.print("[yellow]Admin config will be available in v1.1[/yellow]")
|