openwebui-cli/openwebui_cli/commands/admin.py
Danny Stocker 80510a7082 Fix code review issues: P0 bugs, style, and features
**P0 BUGS FIXED:**
-  chat_id now sent to API (critical bug - conversations couldn't continue)
-  OPENWEBUI_TOKEN env var honored (enables CI/headless workflows)
-  CLIError exit codes properly applied (correct status codes 0-5)

**DUPLICATE FUNCTION FIXED:**
-  Renamed duplicate delete() functions in rag.py to delete_file() and delete_collection()

**RUFF STYLE FIXES (25 → 0 issues):**
-  Removed unused imports (Live, Text from chat.py)
-  Fixed import sorting across all modules
-  Fixed all line-too-long errors (broke long strings, extracted variables)
-  All ruff checks now passing

**FUNCTIONAL IMPROVEMENTS:**
-  --format flag now respected in streaming mode (was only non-streaming)
-  Model parameter now optional, falls back to config.defaults.model
-  Better error message when no model specified

**PYDANTIC V2 COMPATIBILITY:**
-  Fixed ConfigDict → SettingsConfigDict for BaseSettings
-  Added types-PyYAML to dev dependencies for mypy

**ENTRY POINT UPDATE:**
-  Changed entry point from main:app to main:cli for proper error handling

**CODE QUALITY:**
- All 14 tests still passing
- All ruff style checks passing
- Mypy type coverage improved (some minor issues remain)

**IMPACT:**
- Fixes 3 critical bugs blocking production use
- Eliminates all style warnings (25 ruff issues → 0)
- Adds missing functionality (format flag, default model)
- Improves CI/automation workflows (env token support)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-30 20:51:50 +01:00

80 lines
2.5 KiB
Python

"""Admin commands (requires admin role)."""
import json
import typer
from rich.console import Console
from rich.table import Table
from ..errors import AuthError
from ..http import create_client, handle_request_error, handle_response
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":
user_name = user_data.get("name")
user_role = user_data.get("role")
raise AuthError(
f"Admin command requires admin privileges; "
f"your current user is '{user_name}' with role: [{user_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]")