@@ -38,54 +38,47 @@ import { backendState } from './state';
3838import { consoleStore } from '$lib/stores/console' ;
3939
4040/**
41- * Initialize backend from URL parameters.
42- * Reads `?backend=flask` and `?host=...` from the current URL.
43- * Call this early in page mount, before any backend usage.
41+ * Resolve and initialize the execution backend at startup.
42+ *
43+ * Single entry point replacing the old `initBackendFromUrl` + `autoDetectBackend`
44+ * pair (which were both awaited and could each switch+init independently).
45+ * Precedence:
46+ * 1. `?backend=flask[&host=...]` URL override — explicit wins.
47+ * 2. Same-origin Flask server (pip-package mode), detected via `/api/health`.
48+ * 3. Default: Pyodide (created lazily by `getBackend()` on first use).
49+ * Only switches/initializes a Flask backend; the Pyodide default needs no work
50+ * here (it initializes on first `init()`/`exec()`).
4451 */
45- export async function initBackendFromUrl ( ) : Promise < void > {
52+ export async function resolveBackend ( ) : Promise < void > {
4653 if ( typeof window === 'undefined' ) return ;
4754 const params = new URLSearchParams ( window . location . search ) ;
48- const backendParam = params . get ( 'backend' ) ;
49- const hostParam = params . get ( 'host' ) ;
5055
51- if ( backendParam === 'flask' ) {
52- if ( hostParam ) {
53- setFlaskHost ( hostParam ) ;
54- }
56+ // 1. Explicit URL override.
57+ if ( params . get ( 'backend' ) === 'flask' ) {
58+ const host = params . get ( 'host' ) ;
59+ if ( host ) setFlaskHost ( host ) ;
5560 switchBackend ( 'flask' ) ;
5661 await init ( ) ;
62+ return ;
5763 }
58- }
59-
60- /**
61- * Auto-detect if a Flask backend is available at the same origin.
62- * Used when the frontend is served by the Flask server (pip package mode).
63- * URL parameters take precedence — if `?backend=` is set, auto-detection is skipped.
64- */
65- export async function autoDetectBackend ( ) : Promise < void > {
66- if ( typeof window === 'undefined' ) return ;
67-
68- // URL params override auto-detection
69- const params = new URLSearchParams ( window . location . search ) ;
70- if ( params . has ( 'backend' ) ) return ;
7164
65+ // 2. Auto-detect a same-origin Flask server.
7266 try {
7367 const response = await fetch ( '/api/health' , {
7468 method : 'GET' ,
7569 signal : AbortSignal . timeout ( 2000 )
7670 } ) ;
77- if ( response . ok ) {
78- const data = await response . json ( ) ;
79- if ( data . status === 'ok' ) {
80- setFlaskHost ( window . location . origin ) ;
81- switchBackend ( 'flask' ) ;
82- // Run full init — sets up callbacks, logs progress, initializes worker
83- await init ( ) ;
84- }
71+ if ( response . ok && ( await response . json ( ) ) ?. status === 'ok' ) {
72+ setFlaskHost ( window . location . origin ) ;
73+ switchBackend ( 'flask' ) ;
74+ await init ( ) ;
75+ return ;
8576 }
8677 } catch {
87- // No Flask backend at same origin — will use Pyodide
78+ // No Flask backend at same origin — fall through to the Pyodide default.
8879 }
80+
81+ // 3. Default: Pyodide (lazy).
8982}
9083
9184// Alias for backward compatibility
0 commit comments