// Same compilation mechanism as stan-playground: POST the program to the // compilation server, then download the compiled artifact. The server // (stan-wasm-wasi) produces a pure-WASI command module at // /download/{model_id}/main.wasm, runnable on wasm-exec workers. export const DEFAULT_WASI_SERVER_URL = "https://stan-wasm-wasi.fly.dev"; const compileStanProgram = async ( serverUrl: string, stanProgram: string, onStatus: (s: string) => void, ): Promise<{ artifactUrl?: string }> => { try { onStatus("compiling..."); const compileURL = `${serverUrl}/compile`; const runCompile = await fetch(compileURL, { method: "POST", headers: { "Content-Type": "text/plain", Authorization: "Bearer 1234", }, body: stanProgram, }); if (!runCompile.ok) { onStatus(`failed to compile: ${await messageOrStatus(runCompile)}`); return {}; } const compileResp = await runCompile.json(); const artifactUrl = `${serverUrl}/download/${compileResp.model_id}/main.wasm`; // download to make sure it is there onStatus("checking download of main.wasm"); const downloadCheck = await fetch(artifactUrl, { method: "HEAD" }); if (!downloadCheck.ok) { onStatus( `failed to download main.wasm: ${await messageOrStatus(downloadCheck)}`, ); return {}; } onStatus("compiled"); return { artifactUrl }; } catch (e) { onStatus(`failed to compile: ${e}`); return {}; } }; const messageOrStatus = async (response: Response) => { try { const j = await response.json(); return j?.message ?? response.statusText; } catch { return response.statusText; } }; export default compileStanProgram;