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
This commit is contained in:
Masen Furer 2024-07-19 14:05:53 -07:00 committed by GitHub
parent 921a5cd632
commit 8b45d289bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 11 deletions

View File

@ -92,16 +92,30 @@ def _init(
# Set up the web project. # Set up the web project.
prerequisites.initialize_frontend_dependencies() 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: if ai:
prerequisites.initialize_app(app_name, template=constants.Templates.DEFAULT) if template is None:
generation_hash = redir.reflex_build_redirect() # 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( prerequisites.initialize_main_module_index_from_generation(
app_name, generation_hash=generation_hash app_name, generation_hash=generation_hash
) )
else:
# Initialize the app.
prerequisites.initialize_app(app_name, template)
# Migrate Pynecone projects to Reflex. # Migrate Pynecone projects to Reflex.
prerequisites.migrate_to_reflex() prerequisites.migrate_to_reflex()

View File

@ -1598,3 +1598,15 @@ def is_windows_bun_supported() -> bool:
and cpu_info.model_name is not None and cpu_info.model_name is not None
and "ARM" not in cpu_info.model_name 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

View File

@ -11,7 +11,7 @@ from . import console
def open_browser_and_wait( 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: ) -> httpx.Response:
"""Open a browser window to target_url and request poll_url until it returns successfully. """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( console.warn(
f"Unable to automatically open the browser. Please navigate to {target_url} in your browser." 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.") console.info("[b]Complete the workflow in the browser to continue.[/b]")
while response := httpx.get(poll_url, follow_redirects=True): while True:
if response.is_success: try:
break 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) time.sleep(interval)
return response return response