Skip to content

Commit 5c8104f

Browse files
authored
simplify ai init (#4932)
1 parent 41d3514 commit 5c8104f

File tree

3 files changed

+12
-59
lines changed

3 files changed

+12
-59
lines changed

reflex/reflex.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from reflex.config import environment, get_config
1414
from reflex.custom_components.custom_components import custom_components_cli
1515
from reflex.state import reset_disk_state_manager
16-
from reflex.utils import console, telemetry
16+
from reflex.utils import console, redir, telemetry
1717

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

73+
if ai:
74+
redir.reflex_build_redirect()
75+
return
76+
7377
# Validate the app name.
7478
app_name = prerequisites.validate_app_name(name)
7579
console.rule(f"[bold]Initializing {app_name}")
@@ -83,7 +87,7 @@ def _init(
8387
prerequisites.initialize_frontend_dependencies()
8488

8589
# Initialize the app.
86-
template = prerequisites.initialize_app(app_name, template, ai)
90+
template = prerequisites.initialize_app(app_name, template)
8791

8892
# Initialize the .gitignore.
8993
prerequisites.initialize_gitignore()

reflex/utils/prerequisites.py

+3-45
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from reflex import constants, model
3838
from reflex.compiler import templates
3939
from reflex.config import Config, environment, get_config
40-
from reflex.utils import console, net, path_ops, processes, redir
40+
from reflex.utils import console, net, path_ops, processes
4141
from reflex.utils.exceptions import (
4242
GeneratedCodeHasNoFunctionDefsError,
4343
SystemPackageMissingError,
@@ -1695,31 +1695,6 @@ def validate_and_create_app_using_remote_template(
16951695
)
16961696

16971697

1698-
def generate_template_using_ai(template: str | None = None) -> str:
1699-
"""Generate a template using AI(Flexgen).
1700-
1701-
Args:
1702-
template: The name of the template.
1703-
1704-
Returns:
1705-
The generation hash.
1706-
1707-
Raises:
1708-
Exit: If the template and ai flags are used.
1709-
"""
1710-
if template is None:
1711-
# If AI is requested and no template specified, redirect the user to reflex.build.
1712-
return redir.reflex_build_redirect()
1713-
elif is_generation_hash(template):
1714-
# Otherwise treat the template as a generation hash.
1715-
return template
1716-
else:
1717-
console.error(
1718-
"Cannot use `--template` option with `--ai` option. Please remove `--template` option."
1719-
)
1720-
raise typer.Exit(2)
1721-
1722-
17231698
def fetch_remote_templates(
17241699
template: str,
17251700
) -> tuple[str, dict[str, Template]]:
@@ -1744,15 +1719,12 @@ def fetch_remote_templates(
17441719
return template, available_templates
17451720

17461721

1747-
def initialize_app(
1748-
app_name: str, template: str | None = None, ai: bool = False
1749-
) -> str | None:
1722+
def initialize_app(app_name: str, template: str | None = None) -> str | None:
17501723
"""Initialize the app either from a remote template or a blank app. If the config file exists, it is considered as reinit.
17511724
17521725
Args:
17531726
app_name: The name of the app.
17541727
template: The name of the template to use.
1755-
ai: Whether to use AI to generate the template.
17561728
17571729
Returns:
17581730
The name of the template.
@@ -1768,11 +1740,6 @@ def initialize_app(
17681740
telemetry.send("reinit")
17691741
return
17701742

1771-
generation_hash = None
1772-
if ai:
1773-
generation_hash = generate_template_using_ai(template)
1774-
template = constants.Templates.DEFAULT
1775-
17761743
templates: dict[str, Template] = {}
17771744

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

17821749
if template is None:
17831750
template = prompt_for_template_options(get_init_cli_prompt_options())
1784-
if template == constants.Templates.AI:
1785-
generation_hash = generate_template_using_ai()
1786-
# change to the default to allow creation of default app
1787-
template = constants.Templates.DEFAULT
1788-
elif template == constants.Templates.CHOOSE_TEMPLATES:
1751+
if template == constants.Templates.CHOOSE_TEMPLATES:
17891752
console.print(
17901753
f"Go to the templates page ({constants.Templates.REFLEX_TEMPLATES_URL}) and copy the command to init with a template."
17911754
)
@@ -1800,11 +1763,6 @@ def initialize_app(
18001763
app_name=app_name, template=template, templates=templates
18011764
)
18021765

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

18101768
return template

reflex/utils/redir.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Utilities to handle redirection to browser UI."""
22

33
import time
4-
import uuid
54
import webbrowser
65

76
import httpx
@@ -48,14 +47,6 @@ def open_browser_and_wait(
4847
return response
4948

5049

51-
def reflex_build_redirect() -> str:
52-
"""Open the browser window to reflex.build and wait for the user to select a generation.
53-
54-
Returns:
55-
The selected generation hash.
56-
"""
57-
token = str(uuid.uuid4())
58-
target_url = constants.Templates.REFLEX_BUILD_URL.format(reflex_init_token=token)
59-
poll_url = constants.Templates.REFLEX_BUILD_POLL_URL.format(reflex_init_token=token)
60-
response = open_browser_and_wait(target_url, poll_url)
61-
return response.json()["generation_hash"]
50+
def reflex_build_redirect() -> None:
51+
"""Open the browser window to reflex.build."""
52+
open_browser(constants.Templates.REFLEX_BUILD_FRONTEND)

0 commit comments

Comments
 (0)