From 49e40872a159cb2bf2ad1155465c1efa843606ce Mon Sep 17 00:00:00 2001 From: Lukas Parsons Date: Thu, 19 Mar 2026 01:31:07 -0400 Subject: [PATCH] fixing ai's fk up --- apps/server/src/app.ts | 20 ++++- apps/server/src/config.ts | 9 ++- apps/server/src/main.ts | 9 ++- .../src/services/transcriptionService.ts | 38 +++++++-- apps/web/vite.config.ts | 3 +- package.json | 5 +- scripts/dev.sh | 79 +++++++++++++++++++ 7 files changed, 147 insertions(+), 16 deletions(-) create mode 100644 scripts/dev.sh diff --git a/apps/server/src/app.ts b/apps/server/src/app.ts index 48aa619..2430384 100644 --- a/apps/server/src/app.ts +++ b/apps/server/src/app.ts @@ -13,7 +13,25 @@ import { config } from "./config.js"; export function createApp() { const app = express(); - app.use(cors({ origin: config.webBaseUrl })); + app.use(cors({ + origin: [ + "http://localhost:5173", + "http://localhost:5174", + "http://localhost:5175", + "http://localhost:5176", + "http://localhost:5177", + "http://localhost:5178", + "http://localhost:5179", + "http://127.0.0.1:5173", + "http://127.0.0.1:5174", + "http://127.0.0.1:5175", + "http://127.0.0.1:5176", + "http://127.0.0.1:5177", + "http://127.0.0.1:5178", + "http://127.0.0.1:5179", + ], + credentials: true + })); app.use(express.json()); app.get("/health", (_req, res) => res.json({ ok: true })); diff --git a/apps/server/src/config.ts b/apps/server/src/config.ts index 8273328..830d027 100644 --- a/apps/server/src/config.ts +++ b/apps/server/src/config.ts @@ -1,8 +1,13 @@ import dotenv from "dotenv"; import path from "node:path"; +import { fileURLToPath } from "node:url"; +import { dirname } from "node:path"; -// .env lives at the monorepo root, two levels above apps/server/ -dotenv.config({ path: path.resolve(process.cwd(), "../../.env") }); +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +const envPath = path.resolve(__dirname, "../../../.env"); +dotenv.config({ path: envPath }); const required = (key: string, fallback?: string): string => { const value = process.env[key] ?? fallback; diff --git a/apps/server/src/main.ts b/apps/server/src/main.ts index aa5f398..ff1959a 100644 --- a/apps/server/src/main.ts +++ b/apps/server/src/main.ts @@ -13,9 +13,12 @@ async function bootstrap() { console.log(`Server listening on :${config.port}`); }); scheduler.start(); - await discordService.start().catch((err) => { - console.error("[discord] Failed to connect — bot features unavailable:", err.message); - }); + try { + await discordService.start(); + } catch (err) { + console.error("[discord] Failed to connect — bot features unavailable:", err); + console.error("[discord] Server will continue running without Discord features."); + } } bootstrap().catch((err) => { diff --git a/apps/server/src/services/transcriptionService.ts b/apps/server/src/services/transcriptionService.ts index c3b4763..3bf5b78 100644 --- a/apps/server/src/services/transcriptionService.ts +++ b/apps/server/src/services/transcriptionService.ts @@ -49,19 +49,41 @@ class TranscriptionBuffer { const userBuffers = new Map(); const activeTranscriptions = new Map(); -async function transcribeWithWhisper(audioBuffer: Buffer): Promise { - if (!config.openaiApiKey) { - throw new Error("OpenAI API key not configured for transcription"); - } +function createWavBuffer(pcmData: Buffer, sampleRate: number = 48000): Buffer { + const numChannels = 1; + const bitsPerSample = 16; + const byteRate = (sampleRate * numChannels * bitsPerSample) / 8; + const blockAlign = (numChannels * bitsPerSample) / 8; + const dataSize = pcmData.length * 2; + + const header = Buffer.alloc(44); + header.write('RIFF', 0); + header.writeUInt32LE(36 + dataSize, 4); + header.write('WAVE', 8); + header.write('fmt ', 12); + header.writeUInt32LE(16, 16); + header.writeUInt16LE(1, 20); + header.writeUInt16LE(numChannels, 22); + header.writeUInt32LE(sampleRate, 24); + header.writeUInt32LE(byteRate, 28); + header.writeUInt16LE(blockAlign, 32); + header.writeUInt16LE(bitsPerSample, 34); + header.write('data', 36); + header.writeUInt32LE(dataSize, 40); + + return Buffer.concat([header, pcmData]); +} +async function transcribeWithWhisper(audioBuffer: Buffer): Promise { + const wavBuffer = createWavBuffer(audioBuffer); const form = new FormData(); - form.append("file", audioBuffer, { filename: "audio.wav", contentType: "audio/wav" }); - form.append("model", config.whisperModel); + form.append("audio_file", wavBuffer, { filename: "audio.wav", contentType: "audio/wav" }); form.append("language", config.whisperLanguage); + form.append("task", "transcribe"); + form.append("output", "json"); const response = await new Promise((resolve, reject) => { - const req = form.submit(`${config.whisperBaseUrl}/audio/transcriptions`); - req.setHeader("Authorization", `Bearer ${config.openaiApiKey}`); + const req = form.submit(`${config.whisperBaseUrl}/asr`); req.on("response", (res) => { const chunks: Buffer[] = []; diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts index cadfa39..700d5e6 100644 --- a/apps/web/vite.config.ts +++ b/apps/web/vite.config.ts @@ -4,7 +4,8 @@ import react from "@vitejs/plugin-react"; export default defineConfig({ plugins: [react()], server: { - port: 5173 + port: 5173, + strictPort: true } }); diff --git a/package.json b/package.json index da85cfd..8aab06d 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,10 @@ "apps/web" ], "scripts": { - "dev": "concurrently -n server,web -c cyan,magenta \"npm run dev -w @dnd-hub/server\" \"npm run dev -w @dnd-hub/web\"", + "dev": "bash scripts/dev.sh", + "dev:manual": "concurrently -n server,web -c cyan,magenta --kill-others \"npm run dev -w @dnd-hub/server\" \"npm run dev -w @dnd-hub/web\"", + "dev:server": "cd apps/server && tsx watch src/main.ts", + "dev:web": "cd apps/web && vite", "build": "npm run build -w @dnd-hub/server && npm run build -w @dnd-hub/web", "test": "npm run test -w @dnd-hub/server && npm run test -w @dnd-hub/web" }, diff --git a/scripts/dev.sh b/scripts/dev.sh new file mode 100644 index 0000000..f22f55f --- /dev/null +++ b/scripts/dev.sh @@ -0,0 +1,79 @@ +#!/bin/bash + +# Kill existing processes on our ports using PowerShell - with retry +kill_port() { + local port=$1 + for i in 1 2 3; do + powershell -Command "Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue | ForEach-Object { Stop-Process -Id \$_.OwningProcess -Force -ErrorAction SilentlyContinue }" 2>/dev/null + sleep 1 + # Check if port is free + if ! powershell -Command "Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue" 2>/dev/null | grep -q LISTENING; then + break + fi + done +} + +echo "Cleaning up existing processes..." +kill_port 4000 +kill_port 5173 +sleep 2 + +# Get the script directory and resolve to absolute Windows path +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" + +# Start server in background +cd "$ROOT_DIR/apps/server" +nohup npx tsx watch src/main.ts > "$ROOT_DIR/data/server.log" 2>&1 & +SERVER_PID=$! +echo "Server started (PID: $SERVER_PID)" + +# Start web in background +cd "$ROOT_DIR/apps/web" +nohup npm run dev > "$ROOT_DIR/data/web.log" 2>&1 & +WEB_PID=$! +echo "Web started (PID: $WEB_PID)" + +# Wait for both to be ready +echo "Waiting for services to start..." +sleep 6 + +# Check health +sleep 2 +if curl -s http://127.0.0.1:4000/health 2>/dev/null | grep -q "ok"; then + echo "✓ Server is running on http://localhost:4000" +else + echo "✗ Server failed to start" + echo "Server log:" + cat "$ROOT_DIR/data/server.log" 2>/dev/null | tail -20 + exit 1 +fi + +if curl -s http://127.0.0.1:5173/ 2>/dev/null | grep -qi "campaign"; then + echo "✓ Web is running on http://localhost:5173" +else + # Fallback: check if port is listening + if netstat -ano 2>/dev/null | grep -q ":5173.*LISTENING"; then + echo "✓ Web is running on http://localhost:5173" + else + echo "✗ Web failed to start" + echo "Web log:" + cat "$ROOT_DIR/data/web.log" 2>/dev/null | tail -10 + fi +fi + +echo "" +echo "Both services running. Press Ctrl+C to stop." + +# Handle Ctrl+C +cleanup() { + echo "Stopping services..." + taskkill //F //PID $SERVER_PID 2>/dev/null + taskkill //F //PID $WEB_PID 2>/dev/null + exit 0 +} + +trap cleanup INT TERM + +# Keep script running +wait