瀏覽代碼

simplify ai init (#4932)

Thomas Brandého 2 月之前
父節點
當前提交
5c8104ff5b
共有 3 個文件被更改,包括 12 次插入59 次删除
  1. 6 2
      reflex/reflex.py
  2. 3 45
      reflex/utils/prerequisites.py
  3. 3 12
      reflex/utils/redir.py

+ 6 - 2
reflex/reflex.py

@@ -13,7 +13,7 @@ from reflex import constants
 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]
@@ -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}")
@@ -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()

+ 3 - 45
reflex/utils/prerequisites.py

@@ -37,7 +37,7 @@ from redis.exceptions import RedisError
 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,
@@ -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]]:
@@ -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.
@@ -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.
@@ -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."
             )
@@ -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

+ 3 - 12
reflex/utils/redir.py

@@ -1,7 +1,6 @@
 """Utilities to handle redirection to browser UI."""
 
 import time
-import uuid
 import webbrowser
 
 import httpx
@@ -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)