#pragma once // py_subprocess — spawn Python con args y capturar stdout. Lo usan los paneles // AutoExtract y Recipes para invocar funciones del registry (cdp_open_url_and_wait, // cdp_get_ax_tree, llm_propose_scraping_schema, cdp_extract_recipe, infer_json_rows_schema). // // Decisiones: // - Heredoc inline: el script Python se pasa via -c "" para evitar archivos temporales. // - PATH: usa "python3" o "python". Fallback: ${FN_REGISTRY_ROOT}/python/.venv/Scripts/python.exe // (Windows venv layout) o /python/.venv/bin/python3 (POSIX). // - Stdout: capturado completo. El llamante parsea JSON. // - Stderr: redirigido a stdout para facilitar diagnostico (logs visibles). // - Sin consola visible en Windows (CREATE_NO_WINDOW). // - Async wrapper opcional: lanzar en thread y publicar resultado via callback. #include #include #include namespace navegator { struct PyResult { int exit_code = -1; std::string stdout_data; std::string error; // mensaje propio si CreateProcess/popen fallo }; // Devuelve la ruta al interprete python a usar. Prioridad: // 1. ${FN_REGISTRY_ROOT}/python/.venv/Scripts/python.exe (Windows) // 2. ${FN_REGISTRY_ROOT}/python/.venv/bin/python3 (POSIX/MinGW) // 3. "python3" en PATH // 4. "python" en PATH (Windows default) std::string py_resolve_interpreter(); // Devuelve FN_REGISTRY_ROOT. Si no esta seteada, intenta deducirla: // - Working dir del exe ".../fn_registry/projects/navegator/apps/". // - Subiendo 4 niveles desde exe_dir. std::string py_resolve_registry_root(); // Lanza python con argv. argv[0] DEBE ser el interprete (de py_resolve_interpreter()). // Hereda env. Timeout en ms (0 = sin timeout). Devuelve PyResult con stdout + exit. PyResult py_run(const std::vector& argv, int timeout_ms = 60000); // Helper: ejecuta un script inline via `python -c ""` con args extra. PyResult py_run_inline(const std::string& code, const std::vector& extra_args, int timeout_ms = 60000); // Async: ejecuta en thread y llama on_done en el thread del worker. // Captura args por valor; el caller debe sincronizar acceso compartido. void py_run_async(const std::vector& argv, int timeout_ms, std::function on_done); } // namespace navegator