Skip to content

Commit

Permalink
simplify ai init (#4932)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lendemor authored Mar 11, 2025
1 parent 41d3514 commit 5c8104f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 59 deletions.
8 changes: 6 additions & 2 deletions reflex/reflex.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from reflex.config import environment, get_config
from reflex.custom_components.custom_components import custom_components_cli
from reflex.state import reset_disk_state_manager
from reflex.utils import console, telemetry
from reflex.utils import console, redir, telemetry

# Disable typer+rich integration for help panels
typer.core.rich = None # pyright: ignore [reportPrivateImportUsage]
Expand Down Expand Up @@ -70,6 +70,10 @@ def _init(
# Show system info
exec.output_system_info()

if ai:
redir.reflex_build_redirect()
return

# Validate the app name.
app_name = prerequisites.validate_app_name(name)
console.rule(f"[bold]Initializing {app_name}")
Expand All @@ -83,7 +87,7 @@ def _init(
prerequisites.initialize_frontend_dependencies()

# Initialize the app.
template = prerequisites.initialize_app(app_name, template, ai)
template = prerequisites.initialize_app(app_name, template)

# Initialize the .gitignore.
prerequisites.initialize_gitignore()
Expand Down
48 changes: 3 additions & 45 deletions reflex/utils/prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from reflex import constants, model
from reflex.compiler import templates
from reflex.config import Config, environment, get_config
from reflex.utils import console, net, path_ops, processes, redir
from reflex.utils import console, net, path_ops, processes
from reflex.utils.exceptions import (
GeneratedCodeHasNoFunctionDefsError,
SystemPackageMissingError,
Expand Down Expand Up @@ -1695,31 +1695,6 @@ def validate_and_create_app_using_remote_template(
)


def generate_template_using_ai(template: str | None = None) -> str:
"""Generate a template using AI(Flexgen).
Args:
template: The name of the template.
Returns:
The generation hash.
Raises:
Exit: If the template and ai flags are used.
"""
if template is None:
# If AI is requested and no template specified, redirect the user to reflex.build.
return redir.reflex_build_redirect()
elif is_generation_hash(template):
# Otherwise treat the template as a generation hash.
return template
else:
console.error(
"Cannot use `--template` option with `--ai` option. Please remove `--template` option."
)
raise typer.Exit(2)


def fetch_remote_templates(
template: str,
) -> tuple[str, dict[str, Template]]:
Expand All @@ -1744,15 +1719,12 @@ def fetch_remote_templates(
return template, available_templates


def initialize_app(
app_name: str, template: str | None = None, ai: bool = False
) -> str | None:
def initialize_app(app_name: str, template: str | None = None) -> str | None:
"""Initialize the app either from a remote template or a blank app. If the config file exists, it is considered as reinit.
Args:
app_name: The name of the app.
template: The name of the template to use.
ai: Whether to use AI to generate the template.
Returns:
The name of the template.
Expand All @@ -1768,11 +1740,6 @@ def initialize_app(
telemetry.send("reinit")
return

generation_hash = None
if ai:
generation_hash = generate_template_using_ai(template)
template = constants.Templates.DEFAULT

templates: dict[str, Template] = {}

# Don't fetch app templates if the user directly asked for DEFAULT.
Expand All @@ -1781,11 +1748,7 @@ def initialize_app(

if template is None:
template = prompt_for_template_options(get_init_cli_prompt_options())
if template == constants.Templates.AI:
generation_hash = generate_template_using_ai()
# change to the default to allow creation of default app
template = constants.Templates.DEFAULT
elif template == constants.Templates.CHOOSE_TEMPLATES:
if template == constants.Templates.CHOOSE_TEMPLATES:
console.print(
f"Go to the templates page ({constants.Templates.REFLEX_TEMPLATES_URL}) and copy the command to init with a template."
)
Expand All @@ -1800,11 +1763,6 @@ def initialize_app(
app_name=app_name, template=template, templates=templates
)

# If a reflex.build generation hash is available, download the code and apply it to the main module.
if generation_hash:
initialize_main_module_index_from_generation(
app_name, generation_hash=generation_hash
)
telemetry.send("init", template=template)

return template
Expand Down
15 changes: 3 additions & 12 deletions reflex/utils/redir.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Utilities to handle redirection to browser UI."""

import time
import uuid
import webbrowser

import httpx
Expand Down Expand Up @@ -48,14 +47,6 @@ def open_browser_and_wait(
return response


def reflex_build_redirect() -> str:
"""Open the browser window to reflex.build and wait for the user to select a generation.
Returns:
The selected generation hash.
"""
token = str(uuid.uuid4())
target_url = constants.Templates.REFLEX_BUILD_URL.format(reflex_init_token=token)
poll_url = constants.Templates.REFLEX_BUILD_POLL_URL.format(reflex_init_token=token)
response = open_browser_and_wait(target_url, poll_url)
return response.json()["generation_hash"]
def reflex_build_redirect() -> None:
"""Open the browser window to reflex.build."""
open_browser(constants.Templates.REFLEX_BUILD_FRONTEND)

0 comments on commit 5c8104f

Please sign in to comment.