File size: 2,910 Bytes
753ebc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env node

/**

 * Dev entry: ensure ElevenLabs signing server is up, then run mentra-miniapp dev.

 * Loads .env.local so ELEVENLABS_API_KEY / MENTRA_PUBLIC_* are available.

 */

import {spawn} from "node:child_process"
import {dirname, join} from "node:path"
import {fileURLToPath} from "node:url"

import {loadEnvLocal} from "./load-env-local.mjs"

const root = join(dirname(fileURLToPath(import.meta.url)), "..")

// Must load .env.local before reading ELEVENLABS_SIGNING_SERVER_PORT.
loadEnvLocal(root)

const port = Number(process.env.ELEVENLABS_SIGNING_SERVER_PORT || 8788)
const healthUrl = `http://127.0.0.1:${port}/health`

async function signerHealthy() {
  try {
    const res = await fetch(healthUrl, {signal: AbortSignal.timeout(1500)})
    return res.ok
  } catch {
    return false
  }
}

let signerChild = null

if (!(await signerHealthy())) {
  if (!process.env.ELEVENLABS_API_KEY) {
    console.warn(
      `[dev] ElevenLabs signer not running on :${port} and ELEVENLABS_API_KEY is unset.\n` +
        `      Put the key in .env.local (or export it), then re-run bun run dev.\n` +
        `      Continuing without signer — ElevenLabs Start will fail until it is up.`,
    )
  } else {
    console.log(`[dev] starting ElevenLabs signer on :${port}`)
    signerChild = spawn(process.execPath, [join(root, "scripts/signed-url-server.mjs")], {
      cwd: root,
      env: process.env,
      stdio: ["ignore", "inherit", "inherit"],
    })
    signerChild.on("exit", (code, signal) => {
      if (code !== 0 && code !== null) {
        console.warn(`[dev] signer exited code=${code} signal=${signal ?? ""}`)
      }
      signerChild = null
    })
    // Brief wait so health is up before miniapp clients hit Start.
    for (let i = 0; i < 20; i++) {
      await new Promise((r) => setTimeout(r, 100))
      if (await signerHealthy()) break
    }
    if (!(await signerHealthy())) {
      console.warn(`[dev] signer did not become healthy on :${port}`)
    } else {
      console.log(`[dev] signer ready at http://0.0.0.0:${port}`)
    }
  }
} else {
  console.log(`[dev] ElevenLabs signer already healthy on :${port}`)
}

// Use the workspace CLI bin (not bunx → npm), so LAN IP / QR updates match
// the local @mentra/miniapp-cli sources in this monorepo.
const miniapp = spawn("bun", [join(root, "node_modules/@mentra/miniapp-cli/src/index.ts"), "dev"], {
  cwd: root,
  env: process.env,
  stdio: "inherit",
})

function shutdown(signal) {
  if (miniapp && !miniapp.killed) miniapp.kill(signal)
  if (signerChild && !signerChild.killed) signerChild.kill(signal)
}

process.on("SIGINT", () => shutdown("SIGINT"))
process.on("SIGTERM", () => shutdown("SIGTERM"))

miniapp.on("exit", (code) => {
  if (signerChild && !signerChild.killed) signerChild.kill("SIGTERM")
  process.exit(code ?? 0)
})