Browse Source

reflex init --ai fixups (#3691)

* reflex.build polling: catch exceptions and retry

Extend interval to 2 seconds to reduce server load.

* Add function to check if a string looks like a generation hash

* reflex init: allow --template to be a generation hash if --ai is specified
Masen Furer 10 tháng trước cách đây
mục cha
commit
8b45d289bd
3 tập tin đã thay đổi với 41 bổ sung11 xóa
  1. 20 6
      reflex/reflex.py
  2. 12 0
      reflex/utils/prerequisites.py
  3. 9 5
      reflex/utils/redir.py

+ 20 - 6
reflex/reflex.py

@@ -92,16 +92,30 @@ def _init(
     # Set up the web project.
     prerequisites.initialize_frontend_dependencies()
 
-    # Check if AI is requested and redirect the user to reflex.build.
+    # Integrate with reflex.build.
+    generation_hash = None
     if ai:
-        prerequisites.initialize_app(app_name, template=constants.Templates.DEFAULT)
-        generation_hash = redir.reflex_build_redirect()
+        if template is None:
+            # If AI is requested and no template specified, redirect the user to reflex.build.
+            generation_hash = redir.reflex_build_redirect()
+        elif prerequisites.is_generation_hash(template):
+            # Otherwise treat the template as a generation hash.
+            generation_hash = template
+        else:
+            console.error(
+                "Cannot use `--template` option with `--ai` option. Please remove `--template` option."
+            )
+            raise typer.Exit(2)
+        template = constants.Templates.DEFAULT
+
+    # Initialize the app.
+    prerequisites.initialize_app(app_name, template)
+
+    # If a reflex.build generation hash is available, download the code and apply it to the main module.
+    if generation_hash:
         prerequisites.initialize_main_module_index_from_generation(
             app_name, generation_hash=generation_hash
         )
-    else:
-        # Initialize the app.
-        prerequisites.initialize_app(app_name, template)
 
     # Migrate Pynecone projects to Reflex.
     prerequisites.migrate_to_reflex()

+ 12 - 0
reflex/utils/prerequisites.py

@@ -1598,3 +1598,15 @@ def is_windows_bun_supported() -> bool:
         and cpu_info.model_name is not None
         and "ARM" not in cpu_info.model_name
     )
+
+
+def is_generation_hash(template: str) -> bool:
+    """Check if the template looks like a generation hash.
+
+    Args:
+        template: The template name.
+
+    Returns:
+        True if the template is composed of 32 or more hex characters.
+    """
+    return re.match(r"^[0-9a-f]{32,}$", template) is not None

+ 9 - 5
reflex/utils/redir.py

@@ -11,7 +11,7 @@ from . import console
 
 
 def open_browser_and_wait(
-    target_url: str, poll_url: str, interval: int = 1
+    target_url: str, poll_url: str, interval: int = 2
 ) -> httpx.Response:
     """Open a browser window to target_url and request poll_url until it returns successfully.
 
@@ -27,10 +27,14 @@ def open_browser_and_wait(
         console.warn(
             f"Unable to automatically open the browser. Please navigate to {target_url} in your browser."
         )
-    console.info("Complete the workflow in the browser to continue.")
-    while response := httpx.get(poll_url, follow_redirects=True):
-        if response.is_success:
-            break
+    console.info("[b]Complete the workflow in the browser to continue.[/b]")
+    while True:
+        try:
+            response = httpx.get(poll_url, follow_redirects=True)
+            if response.is_success:
+                break
+        except httpx.RequestError as err:
+            console.info(f"Will retry after error occurred while polling: {err}.")
         time.sleep(interval)
     return response