openwebui-cli/openwebui_cli/main.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

68 lines
2.3 KiB
Python

"""Main CLI entry point."""
import typer
from rich.console import Console
from . import __version__
from .commands import admin, auth, chat, config_cmd, models, rag
from .errors import CLIError
# Create main app
app = typer.Typer(
name="openwebui",
help="Official CLI for OpenWebUI",
no_args_is_help=True,
rich_markup_mode="rich",
)
# Create console for rich output
console = Console()
# Register sub-commands
app.add_typer(auth.app, name="auth", help="Authentication commands")
app.add_typer(chat.app, name="chat", help="Chat operations")
app.add_typer(models.app, name="models", help="Model management")
app.add_typer(rag.app, name="rag", help="RAG file and collection operations")
app.add_typer(admin.app, name="admin", help="Admin operations (requires admin role)")
app.add_typer(config_cmd.app, name="config", help="CLI configuration")
@app.callback(invoke_without_command=True)
def main(
ctx: typer.Context,
version: bool = typer.Option(False, "--version", "-v", help="Show version"),
profile: str | None = typer.Option(None, "--profile", "-P", help="Use named profile"),
uri: str | None = typer.Option(None, "--uri", "-U", help="Server URI"),
format: str | None = typer.Option(
None, "--format", "-f", help="Output format: text, json, yaml"
),
quiet: bool = typer.Option(False, "--quiet", "-q", help="Suppress non-essential output"),
verbose: bool = typer.Option(False, "--verbose", "--debug", help="Enable debug logging"),
timeout: int | None = typer.Option(None, "--timeout", "-t", help="Request timeout in seconds"),
) -> None:
"""OpenWebUI CLI - interact with your OpenWebUI instance from the command line."""
if version:
console.print(f"openwebui-cli version {__version__}")
raise typer.Exit()
# Store global options in context for subcommands
ctx.ensure_object(dict)
ctx.obj["profile"] = profile
ctx.obj["uri"] = uri
ctx.obj["format"] = format or "text"
ctx.obj["quiet"] = quiet
ctx.obj["verbose"] = verbose
ctx.obj["timeout"] = timeout
def cli() -> None:
"""Entry point that handles CLIError exit codes properly."""
try:
app()
except CLIError as e:
console.print(f"[red]Error:[/red] {e}")
raise typer.Exit(e.exit_code)
if __name__ == "__main__":
cli()