From 3a0c32ea8fa4fce665944c4d0adfaa5b436ddaaf Mon Sep 17 00:00:00 2001 From: Kshitij <160704796+kshitij-ka@users.noreply.github.com> Date: Tue, 28 Apr 2026 23:55:41 +0530 Subject: [PATCH] feat: add web server backend. --- web/server/.env.example | 8 + web/server/bridge/retrieve.py | 55 ++ web/server/index.js | 367 ++++++++++ web/server/package-lock.json | 867 ++++++++++++++++++++++++ web/server/package.json | 20 + web/server/services/llmService.js | 164 +++++ web/server/services/retrieverService.js | 184 +++++ web/server/start.js | 40 ++ 8 files changed, 1705 insertions(+) create mode 100644 web/server/.env.example create mode 100644 web/server/bridge/retrieve.py create mode 100644 web/server/index.js create mode 100644 web/server/package-lock.json create mode 100644 web/server/package.json create mode 100644 web/server/services/llmService.js create mode 100644 web/server/services/retrieverService.js create mode 100644 web/server/start.js diff --git a/web/server/.env.example b/web/server/.env.example new file mode 100644 index 0000000..2d29ca3 --- /dev/null +++ b/web/server/.env.example @@ -0,0 +1,8 @@ +# Copy this file to .env and fill in your values. +# NEVER commit .env — it is listed in .gitignore. + +# Groq API key — obtain from https://console.groq.com/keys +GROQ_API_KEY=your_groq_api_key_here + +# Server port (optional, defaults to 5000) +PORT=5000 diff --git a/web/server/bridge/retrieve.py b/web/server/bridge/retrieve.py new file mode 100644 index 0000000..e42347f --- /dev/null +++ b/web/server/bridge/retrieve.py @@ -0,0 +1,55 @@ +""" +Persistent retrieval daemon. +Loads the index ONCE on startup, then reads newline-delimited JSON requests +from stdin and writes newline-delimited JSON responses to stdout forever. + +Protocol (one line each direction): + <- {"query": "...", "top_n": 5} + -> {"results": [...], "latency_seconds": 0.15} + -> {"error": "..."} (on failure — process stays alive) + +inference.py is imported as a module — zero lines of it are modified. +""" +import sys +import json +import os + +ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../..")) +sys.path.insert(0, os.path.join(ROOT, "src")) +os.chdir(ROOT) + +import inference # noqa: E402 + +def main(): + # Load once — this is the expensive step (~18s cold, ~0s warm) + try: + _, retriever = inference.load_or_build(force_rebuild=False) + except Exception as exc: + # Fatal: can't serve anything + sys.stdout.write(json.dumps({"error": f"Init failed: {exc}"}) + "\n") + sys.stdout.flush() + sys.exit(1) + + # Signal to Node that we're ready + sys.stdout.write(json.dumps({"ready": True}) + "\n") + sys.stdout.flush() + + # Serve requests forever + for raw_line in sys.stdin: + raw_line = raw_line.strip() + if not raw_line: + continue + try: + req = json.loads(raw_line) + query = req.get("query", "") + top_n = int(req.get("top_n", 5)) + results, latency = retriever.retrieve(query, top_n=top_n) + response = {"results": results, "latency_seconds": round(latency, 4)} + except Exception as exc: + response = {"error": str(exc)} + + sys.stdout.write(json.dumps(response) + "\n") + sys.stdout.flush() + +if __name__ == "__main__": + main() diff --git a/web/server/index.js b/web/server/index.js new file mode 100644 index 0000000..9d7aeaf --- /dev/null +++ b/web/server/index.js @@ -0,0 +1,367 @@ +require("dotenv").config(); + +const express = require("express"); +const cors = require("cors"); +const path = require("path"); +const fs = require("fs"); + +const { generateExplanation, answerQuestion, rewriteQuery } = require("./services/llmService"); +const { retrieve } = require("./services/retrieverService"); + +const app = express(); +const PORT = process.env.PORT || 5000; + +// ── Startup checks ────────────────────────────────────────────────────────── + +if (!process.env.GROQ_API_KEY) { + console.warn( + "[WARN] GROQ_API_KEY is not set. AI features will return fallback values.\n" + + " Copy web/server/.env.example to web/server/.env and add your key." + ); +} + +app.use(cors()); +app.use(express.json()); + +// ── Load data ─────────────────────────────────────────────────────────────── + +const DATA_DIR = path.join(__dirname, "../../data/processed"); + +let standards = []; +let chunks = []; + +try { + standards = JSON.parse(fs.readFileSync(path.join(DATA_DIR, "standards.json"), "utf-8")); + chunks = JSON.parse(fs.readFileSync(path.join(DATA_DIR, "standards_chunks.json"), "utf-8")); + console.log(`[init] Loaded ${standards.length} standards, ${chunks.length} chunks`); +} catch (e) { + console.error("[init] Failed to load data:", e.message); +} + +// Pre-build lookups +const standardsById = {}; +const chunksByStd = {}; // standard_id → [chunk, …] +const byCategory = {}; +const categories = new Set(); + +for (const s of standards) { + standardsById[s.standard_id] = s; + categories.add(s.category); + if (!byCategory[s.category]) byCategory[s.category] = []; + byCategory[s.category].push(s); +} +for (const c of chunks) { + if (!chunksByStd[c.standard_id]) chunksByStd[c.standard_id] = []; + chunksByStd[c.standard_id].push(c); +} + +// ── Structured logger ─────────────────────────────────────────────────────── + +function log(endpoint, data) { + const ts = new Date().toISOString(); + console.log(`[${ts}] ${endpoint} |`, JSON.stringify(data)); +} + +// ── Keyword-based search helper (unchanged from original) ─────────────────── + +function normalize(str) { + return str.toLowerCase().replace(/[^a-z0-9]/g, " ").replace(/\s+/g, " ").trim(); +} + +function scoreStandard(standard, query) { + const q = normalize(query); + const qTokens = q.split(" ").filter(Boolean); + + const idNorm = normalize(standard.standard_id); + const titleNorm = normalize(standard.title); + const summaryNorm = normalize(standard.summary || ""); + const kwNorm = normalize((standard.keywords || []).join(" ")); + const catNorm = normalize(standard.category); + + let s = 0; + if (idNorm.includes(q)) s += 100; + for (const tok of qTokens) { + if (tok.length < 2) continue; + if (idNorm.includes(tok)) s += 20; + if (titleNorm.includes(tok)) s += 10; + if (kwNorm.includes(tok)) s += 6; + if (summaryNorm.includes(tok)) s += 3; + if (catNorm.includes(tok)) s += 2; + } + return s; +} + +// ── Best chunk selector ───────────────────────────────────────────────────── + +function bestChunk(standardId, question) { + const stdChunks = chunksByStd[standardId] || []; + if (!stdChunks.length) return null; + + const qTokens = normalize(question).split(" ").filter((t) => t.length > 2); + let best = stdChunks[0]; + let bestScore = 0; + + for (const c of stdChunks) { + const textNorm = normalize(c.text); + const score = qTokens.reduce((acc, t) => acc + (textNorm.includes(t) ? 1 : 0), 0); + if (score > bestScore) { bestScore = score; best = c; } + } + return best; +} + +// ═══════════════════════════════════════════════════════════════════════════ +// Routes +// ═══════════════════════════════════════════════════════════════════════════ + +// ── GET /api/standards ────────────────────────────────────────────────────── +app.get("/api/standards", (req, res) => { + const { q = "", category = "", page = "1", limit = "20" } = req.query; + const pageNum = Math.max(1, parseInt(page)); + const limitNum = Math.min(100, Math.max(1, parseInt(limit))); + + let results = standards; + if (category) results = results.filter((s) => s.category === category); + + if (q.trim()) { + results = results + .map((s) => ({ s, score: scoreStandard(s, q.trim()) })) + .filter(({ score }) => score > 0) + .sort((a, b) => b.score - a.score) + .map(({ s }) => s); + } + + const total = results.length; + const totalPages = Math.ceil(total / limitNum); + const paginated = results.slice((pageNum - 1) * limitNum, pageNum * limitNum); + + res.json({ data: paginated, meta: { total, page: pageNum, limit: limitNum, totalPages } }); +}); + +// ── GET /api/standards/:id ────────────────────────────────────────────────── +app.get("/api/standards/:id", (req, res) => { + const id = decodeURIComponent(req.params.id); + const standard = standardsById[id]; + if (!standard) return res.status(404).json({ error: "Standard not found" }); + res.json(standard); +}); + +// ── GET /api/categories ───────────────────────────────────────────────────── +app.get("/api/categories", (req, res) => { + const result = [...categories].sort().map((cat) => ({ + name: cat, + count: byCategory[cat]?.length || 0, + })); + res.json(result); +}); + +// ── GET /api/stats ────────────────────────────────────────────────────────── +app.get("/api/stats", (req, res) => { + res.json({ + totalStandards: standards.length, + totalCategories: categories.size, + totalChunks: chunks.length, + }); +}); + +// ── POST /api/recommend ───────────────────────────────────────────────────── +/** + * Input: { query: string, top_n?: number, rewrite?: boolean } + * Flow: + * 1. Optionally rewrite query with LLM (parallel, non-blocking on failure) + * 2. Call Python inference.py via bridge (retrieval logic untouched) + * 3. Enrich each result with LLM explanation (Promise.allSettled — no blocking) + * 4. Return standards + explanations + timing breakdown + * + * Output: { standards, latency: { retrieval_ms, llm_ms, total_ms } } + */ +app.post("/api/recommend", async (req, res) => { + const { query, top_n = 5, rewrite = false } = req.body; + + if (!query || typeof query !== "string" || !query.trim()) { + return res.status(400).json({ error: "query is required." }); + } + if (query.length > 500) { + return res.status(400).json({ error: "query must be 500 characters or fewer." }); + } + + const t0 = Date.now(); + + // Step 1 — Optional query rewrite (fires concurrently, falls back silently) + let effectiveQuery = query.trim(); + if (rewrite && process.env.GROQ_API_KEY) { + effectiveQuery = await rewriteQuery(query.trim()); // never throws + } + + // Step 2 — Python retrieval (inference.py untouched) + let retrievalResult; + const tRetStart = Date.now(); + try { + retrievalResult = await retrieve(effectiveQuery, Math.min(top_n, 10)); + } catch (err) { + console.error("[recommend] Retrieval error:", err.message); + return res.status(502).json({ error: "Retrieval service unavailable. Please try again." }); + } + const retrievalMs = Date.now() - tRetStart; + + const { results: retrieved, latency_seconds: pyLatency } = retrievalResult; + + // Step 3 — LLM explanations fired in parallel (allSettled — never blocks on failure) + const tLlmStart = Date.now(); + const explanationJobs = retrieved.map((r) => { + const std = standardsById[r.standard_id]; + if (!std) return Promise.resolve({ status: "fulfilled", value: r.title }); + return generateExplanation(std).then( + (exp) => ({ status: "fulfilled", value: exp }), + () => ({ status: "rejected", value: std.summary || std.title || "" }), + ); + }); + const explanations = await Promise.all(explanationJobs); + const llmMs = Date.now() - tLlmStart; + + // Step 4 — Assemble response + const standardsOut = retrieved.map((r, i) => { + const std = standardsById[r.standard_id] || {}; + return { + standard_id: r.standard_id, + title: r.title, + category: r.category, + matched_section: r.matched_section, + score: r.score, + explanation: explanations[i].value, + keywords: std.keywords || [], + }; + }); + + const totalMs = Date.now() - t0; + + log("POST /api/recommend", { + query: effectiveQuery, + results: retrieved.length, + retrieval_ms: retrievalMs, + llm_ms: llmMs, + total_ms: totalMs, + }); + + res.json({ + query: effectiveQuery, + standards: standardsOut, + latency: { + retrieval_ms: retrievalMs, + llm_ms: llmMs, + total_ms: totalMs, + }, + }); +}); + +// ── POST /api/ask ─────────────────────────────────────────────────────────── +/** + * Input: { question: string, standard_id: string } + * Flow: + * 1. Find best matching chunk for the question within the standard + * 2. Pass chunk text to answerQuestion() — strictly grounded + * 3. Return answer + chunk source info + * + * Output: { answer, source: { standard_id, section, chunk_id } } + */ +app.post("/api/ask", async (req, res) => { + const { question, standard_id } = req.body; + + if (!question || typeof question !== "string" || !question.trim()) { + return res.status(400).json({ error: "question is required." }); + } + if (!standard_id || typeof standard_id !== "string") { + return res.status(400).json({ error: "standard_id is required." }); + } + if (question.length > 500) { + return res.status(400).json({ error: "question must be 500 characters or fewer." }); + } + + const t0 = Date.now(); + + const chunk = bestChunk(standard_id, question); + if (!chunk) { + return res.status(404).json({ error: "No content found for this standard." }); + } + + const tLlm = Date.now(); + const answer = await answerQuestion(question.trim(), chunk.text); // never throws + const llmMs = Date.now() - tLlm; + const totalMs = Date.now() - t0; + + log("POST /api/ask", { + standard_id, + question: question.slice(0, 80), + llm_ms: llmMs, + total_ms: totalMs, + }); + + res.json({ + answer, + source: { + standard_id: chunk.standard_id, + section: chunk.section, + chunk_id: chunk.chunk_id, + }, + latency: { llm_ms: llmMs, total_ms: totalMs }, + }); +}); + +// ── POST /api/chat ────────────────────────────────────────────────────────── +/** + * Conversational QA grounded in a standard's full text. + * Uses answerQuestion() from llmService — key never leaves server. + */ +app.post("/api/chat", async (req, res) => { + if (!process.env.GROQ_API_KEY) { + return res.status(503).json({ error: "AI features are not configured on this server." }); + } + + const { standard_id, question } = req.body; + + if (!question || typeof question !== "string" || !question.trim()) { + return res.status(400).json({ error: "question is required." }); + } + if (question.length > 500) { + return res.status(400).json({ error: "question must be 500 characters or fewer." }); + } + + const std = standard_id ? standardsById[standard_id] : null; + let chunkText = ""; + + if (std) { + const chunk = bestChunk(standard_id, question); + chunkText = chunk ? chunk.text : ""; + + // Augment chunk with structured sections for richer context + const sections = Object.entries(std.key_sections || {}) + .map(([n, t]) => `${n}: ${t}`) + .join("\n"); + if (sections) chunkText = `${chunkText}\n\n${sections}`.trim(); + } + + const t0 = Date.now(); + const answer = await answerQuestion(question.trim(), chunkText || "Context not available."); + const totalMs = Date.now() - t0; + + log("POST /api/chat", { standard_id, llm_ms: totalMs }); + res.json({ answer }); +}); + +// ── Start ─────────────────────────────────────────────────────────────────── +const server = app.listen(PORT, () => { + console.log(`[init] BIS API running on http://localhost:${PORT}`); +}); + +server.on("error", (err) => { + if (err.code === "EADDRINUSE") { + console.error( + `[ERROR] Port ${PORT} is already in use.\n` + + ` Another server process is still running. Stop it first:\n` + + ` Windows: netstat -ano | findstr :${PORT} then taskkill /PID /F\n` + + ` Or change PORT in web/server/.env` + ); + } else { + console.error("[ERROR] Server failed to start:", err.message); + } + process.exit(1); +}); diff --git a/web/server/package-lock.json b/web/server/package-lock.json new file mode 100644 index 0000000..35d6cd7 --- /dev/null +++ b/web/server/package-lock.json @@ -0,0 +1,867 @@ +{ + "name": "server", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "server", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "cors": "^2.8.6", + "dotenv": "^17.4.2", + "express": "^5.2.1" + } + }, + "node_modules/accepts": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz", + "integrity": "sha512-5cvg6CtKwfgdmVqY1WIiXKc3Q1bkRqGLi+2W/6ao+6Y7gu/RCwRuAhGEzh5B4KlszSuTLgZYuqFqo5bImjNKng==", + "license": "MIT", + "dependencies": { + "mime-types": "^3.0.0", + "negotiator": "^1.0.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/body-parser": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.2.2.tgz", + "integrity": "sha512-oP5VkATKlNwcgvxi0vM0p/D3n2C3EReYVX+DNYs5TjZFn/oQt2j+4sVJtSMr18pdRr8wjTcBl6LoV+FUwzPmNA==", + "license": "MIT", + "dependencies": { + "bytes": "^3.1.2", + "content-type": "^1.0.5", + "debug": "^4.4.3", + "http-errors": "^2.0.0", + "iconv-lite": "^0.7.0", + "on-finished": "^2.4.1", + "qs": "^6.14.1", + "raw-body": "^3.0.1", + "type-is": "^2.0.1" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/bytes": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", + "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/call-bound": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.4.tgz", + "integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "get-intrinsic": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/content-disposition": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-1.1.0.tgz", + "integrity": "sha512-5jRCH9Z/+DRP7rkvY83B+yGIGX96OYdJmzngqnw2SBSxqCFPd0w2km3s5iawpGX8krnwSGmF0FW5Nhr0Hfai3g==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/content-type": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", + "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz", + "integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/cookie-signature": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.2.2.tgz", + "integrity": "sha512-D76uU73ulSXrD1UXF4KE2TMxVVwhsnCgfAyTg9k8P6KGZjlXKrOLe4dJQKI3Bxi5wjesZoFXJWElNWBjPZMbhg==", + "license": "MIT", + "engines": { + "node": ">=6.6.0" + } + }, + "node_modules/cors": { + "version": "2.8.6", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.6.tgz", + "integrity": "sha512-tJtZBBHA6vjIAaF6EnIaq6laBBP9aq/Y3ouVJjEfoHbRBcHBAHYcMh/w8LDrk2PvIMMq8gmopa5D4V8RmbrxGw==", + "license": "MIT", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/depd": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", + "integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/dotenv": { + "version": "17.4.2", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.4.2.tgz", + "integrity": "sha512-nI4U3TottKAcAD9LLud4Cb7b2QztQMUEfHbvhTH09bqXTxnSie8WnjPALV/WMCrJZ6UV/qHJ6L03OqO3LcdYZw==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/ee-first": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", + "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==", + "license": "MIT" + }, + "node_modules/encodeurl": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz", + "integrity": "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-object-atoms": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz", + "integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/escape-html": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", + "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==", + "license": "MIT" + }, + "node_modules/etag": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", + "integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/express": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/express/-/express-5.2.1.tgz", + "integrity": "sha512-hIS4idWWai69NezIdRt2xFVofaF4j+6INOpJlVOLDO8zXGpUVEVzIYk12UUi2JzjEzWL3IOAxcTubgz9Po0yXw==", + "license": "MIT", + "dependencies": { + "accepts": "^2.0.0", + "body-parser": "^2.2.1", + "content-disposition": "^1.0.0", + "content-type": "^1.0.5", + "cookie": "^0.7.1", + "cookie-signature": "^1.2.1", + "debug": "^4.4.0", + "depd": "^2.0.0", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "finalhandler": "^2.1.0", + "fresh": "^2.0.0", + "http-errors": "^2.0.0", + "merge-descriptors": "^2.0.0", + "mime-types": "^3.0.0", + "on-finished": "^2.4.1", + "once": "^1.4.0", + "parseurl": "^1.3.3", + "proxy-addr": "^2.0.7", + "qs": "^6.14.0", + "range-parser": "^1.2.1", + "router": "^2.2.0", + "send": "^1.1.0", + "serve-static": "^2.2.0", + "statuses": "^2.0.1", + "type-is": "^2.0.1", + "vary": "^1.1.2" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/finalhandler": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-2.1.1.tgz", + "integrity": "sha512-S8KoZgRZN+a5rNwqTxlZZePjT/4cnm0ROV70LedRHZ0p8u9fRID0hJUZQpkKLzro8LfmC8sx23bY6tVNxv8pQA==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.0", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "on-finished": "^2.4.1", + "parseurl": "^1.3.3", + "statuses": "^2.0.1" + }, + "engines": { + "node": ">= 18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/forwarded": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", + "integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/fresh": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/fresh/-/fresh-2.0.0.tgz", + "integrity": "sha512-Rx/WycZ60HOaqLKAi6cHRKKI7zxWbJ31MhntmtwMoaTeF7XFH9hhBp8vITaMidfljRQ6eYWCKkaTK+ykVJHP2A==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-intrinsic": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "license": "MIT", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/hasown": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.3.tgz", + "integrity": "sha512-ej4AhfhfL2Q2zpMmLo7U1Uv9+PyhIZpgQLGT1F9miIGmiCJIoCgSmczFdrc97mWT4kVY72KA+WnnhJ5pghSvSg==", + "license": "MIT", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/http-errors": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz", + "integrity": "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==", + "license": "MIT", + "dependencies": { + "depd": "~2.0.0", + "inherits": "~2.0.4", + "setprototypeof": "~1.2.0", + "statuses": "~2.0.2", + "toidentifier": "~1.0.1" + }, + "engines": { + "node": ">= 0.8" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/iconv-lite": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.2.tgz", + "integrity": "sha512-im9DjEDQ55s9fL4EYzOAv0yMqmMBSZp6G0VvFyTMPKWxiSBHUj9NW/qqLmXUwXrrM7AvqSlTCfvqRb0cM8yYqw==", + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "license": "ISC" + }, + "node_modules/ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==", + "license": "MIT", + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/is-promise": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-4.0.0.tgz", + "integrity": "sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==", + "license": "MIT" + }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/media-typer": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-1.1.0.tgz", + "integrity": "sha512-aisnrDP4GNe06UcKFnV5bfMNPBUw4jsLGaWwWfnH3v02GnBuXX2MCVn5RbrWo0j3pczUilYblq7fQ7Nw2t5XKw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/merge-descriptors": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-2.0.0.tgz", + "integrity": "sha512-Snk314V5ayFLhp3fkUREub6WtjBfPdCPY1Ln8/8munuLuiYhsABgBVWsozAG+MWMbVEvcdcpbi9R7ww22l9Q3g==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/mime-db": { + "version": "1.54.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.54.0.tgz", + "integrity": "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-3.0.2.tgz", + "integrity": "sha512-Lbgzdk0h4juoQ9fCKXW4by0UJqj+nOOrI9MJ1sSj4nI8aI2eo1qmvQEie4VD1glsS250n15LsWsYtCugiStS5A==", + "license": "MIT", + "dependencies": { + "mime-db": "^1.54.0" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, + "node_modules/negotiator": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-1.0.0.tgz", + "integrity": "sha512-8Ofs/AUQh8MaEcrlq5xOX0CQ9ypTF5dl78mjlMNfOK08fzpgTHQRQPBxcPlEtIw0yRpws+Zo/3r+5WRby7u3Gg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/object-inspect": { + "version": "1.13.4", + "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz", + "integrity": "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==", + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/on-finished": { + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz", + "integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==", + "license": "MIT", + "dependencies": { + "ee-first": "1.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "license": "ISC", + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/parseurl": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", + "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/path-to-regexp": { + "version": "8.4.2", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.4.2.tgz", + "integrity": "sha512-qRcuIdP69NPm4qbACK+aDogI5CBDMi1jKe0ry5rSQJz8JVLsC7jV8XpiJjGRLLol3N+R5ihGYcrPLTno6pAdBA==", + "license": "MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/proxy-addr": { + "version": "2.0.7", + "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", + "integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==", + "license": "MIT", + "dependencies": { + "forwarded": "0.2.0", + "ipaddr.js": "1.9.1" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/qs": { + "version": "6.15.1", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.15.1.tgz", + "integrity": "sha512-6YHEFRL9mfgcAvql/XhwTvf5jKcOiiupt2FiJxHkiX1z4j7WL8J/jRHYLluORvc1XxB5rV20KoeK00gVJamspg==", + "license": "BSD-3-Clause", + "dependencies": { + "side-channel": "^1.1.0" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/range-parser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", + "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/raw-body": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-3.0.2.tgz", + "integrity": "sha512-K5zQjDllxWkf7Z5xJdV0/B0WTNqx6vxG70zJE4N0kBs4LovmEYWJzQGxC9bS9RAKu3bgM40lrd5zoLJ12MQ5BA==", + "license": "MIT", + "dependencies": { + "bytes": "~3.1.2", + "http-errors": "~2.0.1", + "iconv-lite": "~0.7.0", + "unpipe": "~1.0.0" + }, + "engines": { + "node": ">= 0.10" + } + }, + "node_modules/router": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/router/-/router-2.2.0.tgz", + "integrity": "sha512-nLTrUKm2UyiL7rlhapu/Zl45FwNgkZGaCpZbIHajDYgwlJCOzLSk+cIPAnsEqV955GjILJnKbdQC1nVPz+gAYQ==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.0", + "depd": "^2.0.0", + "is-promise": "^4.0.0", + "parseurl": "^1.3.3", + "path-to-regexp": "^8.0.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", + "license": "MIT" + }, + "node_modules/send": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/send/-/send-1.2.1.tgz", + "integrity": "sha512-1gnZf7DFcoIcajTjTwjwuDjzuz4PPcY2StKPlsGAQ1+YH20IRVrBaXSWmdjowTJ6u8Rc01PoYOGHXfP1mYcZNQ==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.3", + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "etag": "^1.8.1", + "fresh": "^2.0.0", + "http-errors": "^2.0.1", + "mime-types": "^3.0.2", + "ms": "^2.1.3", + "on-finished": "^2.4.1", + "range-parser": "^1.2.1", + "statuses": "^2.0.2" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/serve-static": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-2.2.1.tgz", + "integrity": "sha512-xRXBn0pPqQTVQiC8wyQrKs2MOlX24zQ0POGaj0kultvoOCstBQM5yvOhAVSUwOMjQtTvsPWoNCHfPGwaaQJhTw==", + "license": "MIT", + "dependencies": { + "encodeurl": "^2.0.0", + "escape-html": "^1.0.3", + "parseurl": "^1.3.3", + "send": "^1.2.0" + }, + "engines": { + "node": ">= 18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/setprototypeof": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", + "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==", + "license": "ISC" + }, + "node_modules/side-channel": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.0.tgz", + "integrity": "sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.3", + "side-channel-list": "^1.0.0", + "side-channel-map": "^1.0.1", + "side-channel-weakmap": "^1.0.2" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-list": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.1.tgz", + "integrity": "sha512-mjn/0bi/oUURjc5Xl7IaWi/OJJJumuoJFQJfDDyO46+hBWsfaVM65TBHq2eoZBhzl9EchxOijpkbRC8SVBQU0w==", + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "object-inspect": "^1.13.4" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-map": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz", + "integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/side-channel-weakmap": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz", + "integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==", + "license": "MIT", + "dependencies": { + "call-bound": "^1.0.2", + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.5", + "object-inspect": "^1.13.3", + "side-channel-map": "^1.0.1" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/statuses": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz", + "integrity": "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/toidentifier": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", + "integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==", + "license": "MIT", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/type-is": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/type-is/-/type-is-2.0.1.tgz", + "integrity": "sha512-OZs6gsjF4vMp32qrCbiVSkrFmXtG/AZhY3t0iAMrMBiAZyV9oALtXO8hsrHbMXF9x6L3grlFuwW2oAz7cav+Gw==", + "license": "MIT", + "dependencies": { + "content-type": "^1.0.5", + "media-typer": "^1.1.0", + "mime-types": "^3.0.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/unpipe": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", + "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==", + "license": "MIT", + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "license": "ISC" + } + } +} diff --git a/web/server/package.json b/web/server/package.json new file mode 100644 index 0000000..246cef9 --- /dev/null +++ b/web/server/package.json @@ -0,0 +1,20 @@ +{ + "name": "server", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "node start.js", + "dev": "node start.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "dependencies": { + "cors": "^2.8.6", + "dotenv": "^17.4.2", + "express": "^5.2.1" + } +} diff --git a/web/server/services/llmService.js b/web/server/services/llmService.js new file mode 100644 index 0000000..1ffeb3e --- /dev/null +++ b/web/server/services/llmService.js @@ -0,0 +1,164 @@ +"use strict"; +/** + * llmService.js + * All Groq LLM calls live here. Three functions: + * generateExplanation(standard) — 2-3 sentence plain-English summary + * answerQuestion(question, chunk) — grounded QA, strict context-only + * rewriteQuery(query) — optional query expansion + * + * Key rules enforced here: + * - GROQ_API_KEY never leaves this file toward the client + * - max_tokens kept short to minimise latency (<400 tokens each) + * - Every function returns a fallback value on failure — callers never throw + */ + +const GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"; +const MODEL = "llama-3.1-8b-instant"; + +// ── Core fetch wrapper ────────────────────────────────────────────────────── + +async function _groqCall({ systemPrompt, userMessage, maxTokens = 256, temperature = 0.2 }) { + const key = process.env.GROQ_API_KEY; + if (!key) throw new Error("GROQ_API_KEY not set"); + + const res = await fetch(GROQ_API_URL, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${key}`, + }, + body: JSON.stringify({ + model: MODEL, + messages: [ + { role: "system", content: systemPrompt }, + { role: "user", content: userMessage }, + ], + max_tokens: maxTokens, + temperature, + }), + }); + + if (!res.ok) { + const body = await res.json().catch(() => ({})); + throw new Error(`Groq ${res.status}: ${body?.error?.message || "unknown error"}`); + } + + const data = await res.json(); + return data.choices?.[0]?.message?.content?.trim() ?? ""; +} + +// ── 1. generateExplanation ────────────────────────────────────────────────── + +/** + * Produces a 2-3 sentence plain-English explanation of a standard. + * Falls back to the standard's own summary on failure — never throws. + * + * @param {{ standard_id: string, title: string, summary?: string, content?: string, key_sections?: object }} standard + * @returns {Promise} + */ +async function generateExplanation(standard) { + const context = buildStandardContext(standard); + + try { + return await _groqCall({ + systemPrompt: + "You are a technical writer for the Bureau of Indian Standards (BIS). " + + "Explain building material standards in simple English for engineers and contractors. " + + "Use ONLY the provided standard text — do not add, invent, or infer anything not explicitly stated. " + + "Write exactly 2-3 sentences. No bullet points. No headings.", + userMessage: + `Explain this BIS standard in simple terms using ONLY the provided information:\n\n${context}`, + maxTokens: 180, + temperature: 0.2, + }); + } catch (err) { + // Graceful fallback — retrieval is unaffected + return standard.summary || standard.title || ""; + } +} + +// ── 2. answerQuestion ─────────────────────────────────────────────────────── + +/** + * Answers a user question strictly from a chunk of standard text. + * Returns "Not found in standard" when context doesn't contain the answer. + * Never throws. + * + * @param {string} question + * @param {string} chunkText — raw chunk text from standards_chunks.json + * @returns {Promise} + */ +async function answerQuestion(question, chunkText) { + if (!question?.trim() || !chunkText?.trim()) { + return "Not found in standard"; + } + + try { + return await _groqCall({ + systemPrompt: + "You are a precise technical assistant for BIS Indian Standards. " + + "Answer questions using ONLY the provided context text. " + + "If the answer is not present in the context, respond with exactly: 'Not found in standard'. " + + "Do not speculate. Do not reference other standards not mentioned in the context. " + + "Keep answers under 100 words.", + userMessage: + `CONTEXT:\n${chunkText}\n\nQUESTION: ${question.trim()}`, + maxTokens: 200, + temperature: 0.1, + }); + } catch (err) { + return "Not found in standard"; + } +} + +// ── 3. rewriteQuery (optional) ────────────────────────────────────────────── + +/** + * Rewrites a vague natural-language query into precise IS-standard keywords. + * Falls back to the original query on failure — retrieval is never blocked. + * + * @param {string} query + * @returns {Promise} + */ +async function rewriteQuery(query) { + if (!query?.trim()) return query; + + try { + const rewritten = await _groqCall({ + systemPrompt: + "You are a search query optimizer for the BIS SP-21 building materials standards database. " + + "Convert the user's natural-language query into 3-6 precise technical keywords " + + "suitable for searching Indian Standards (IS) documents. " + + "Output ONLY the keywords separated by spaces — no explanation, no punctuation.", + userMessage: query.trim(), + maxTokens: 40, + temperature: 0.1, + }); + // Sanity check — if rewrite is too long or garbled, fall back + const words = rewritten.trim().split(/\s+/); + if (words.length >= 2 && words.length <= 10) return rewritten.trim(); + return query; + } catch { + return query; + } +} + +// ── Helpers ───────────────────────────────────────────────────────────────── + +function buildStandardContext(standard) { + const parts = [`Standard: ${standard.standard_id} — ${standard.title}`]; + if (standard.category) parts.push(`Category: ${standard.category}`); + if (standard.summary) parts.push(`Summary: ${standard.summary}`); + + const sections = Object.entries(standard.key_sections || {}).slice(0, 3); + if (sections.length) { + parts.push("Key Sections:"); + for (const [name, text] of sections) { + // Cap each section to 300 chars to keep token count low + parts.push(` ${name}: ${text.slice(0, 300)}`); + } + } + return parts.join("\n"); +} + +module.exports = { generateExplanation, answerQuestion, rewriteQuery }; diff --git a/web/server/services/retrieverService.js b/web/server/services/retrieverService.js new file mode 100644 index 0000000..02c4391 --- /dev/null +++ b/web/server/services/retrieverService.js @@ -0,0 +1,184 @@ +"use strict"; +/** + * retrieverService.js — persistent Python daemon. + * + * Spawns retrieve.py ONCE when the Node server starts. The Python process + * loads the FAISS index and BM25 index once, then serves queries via + * newline-delimited JSON on stdin/stdout. + * + * First query: ~150ms (index already warm from startup). + * Cold start happens in the background while the server is booting. + * + * inference.py is never modified. + */ + +const { spawn } = require("child_process"); +const path = require("path"); +const readline = require("readline"); +const { EventEmitter } = require("events"); + +const BRIDGE = path.join(__dirname, "../bridge/retrieve.py"); +const ROOT = path.join(__dirname, "../../.."); +const PYTHON = process.env.PYTHON_BIN || "python"; + +const BOOT_TIMEOUT_MS = 90_000; // Python cold-start budget +const QUERY_TIMEOUT_MS = 10_000; // per-query budget once warm + +class PythonRetriever extends EventEmitter { + constructor() { + super(); + this._proc = null; + this._rl = null; + this._ready = false; + this._error = null; + this._queue = []; // requests queued before ready: [{query,top_n,resolve,reject,timer}] + this._pending = []; // in-flight requests sent to Python: [{resolve,reject,timer}] + this._start(); + } + + _start() { + this._ready = false; + this._error = null; + + console.log("[retriever] Starting Python daemon (first boot ~20s)…"); + + this._proc = spawn(PYTHON, [BRIDGE], { + cwd: ROOT, + env: { ...process.env }, + stdio: ["pipe", "pipe", "pipe"], + }); + + this._rl = readline.createInterface({ input: this._proc.stdout, crlfDelay: Infinity }); + + this._rl.on("line", (raw) => this._onLine(raw)); + + this._proc.stderr.on("data", (d) => { + const text = d.toString(); + // Suppress routine model-loading noise + if ( + text.includes("Loading cached") || + text.includes("BM25 index") || + text.includes("Loaded ") || + text.includes("Building BM25") + ) return; + process.stderr.write("[py] " + text); + }); + + this._proc.on("close", (code) => { + console.warn(`[retriever] Daemon exited (code ${code}), restarting on next query.`); + this._ready = false; + this._proc = null; + this._rl = null; + // Reject anything still in flight + for (const p of [...this._queue, ...this._pending]) { + clearTimeout(p.timer); + p.reject(new Error("Python retriever restarted unexpectedly.")); + } + this._queue.length = 0; + this._pending.length = 0; + }); + + this._proc.on("error", (err) => { + console.error("[retriever] Spawn error:", err.message); + this._error = err.message; + for (const p of [...this._queue, ...this._pending]) { + clearTimeout(p.timer); + p.reject(new Error(`Retriever spawn failed: ${err.message}`)); + } + this._queue.length = 0; + this._pending.length = 0; + }); + } + + _onLine(raw) { + raw = raw.trim(); + if (!raw) return; + + let msg; + try { msg = JSON.parse(raw); } + catch { return; } // ignore non-JSON (e.g. sentence-transformers progress bars) + + // ── Startup handshake ── + if (!this._ready) { + if (msg.ready) { + this._ready = true; + console.log(`[retriever] Ready — flushing ${this._queue.length} queued request(s).`); + // Send all queued requests in order + for (const item of this._queue) { + this._pending.push(item); + this._send(item); + } + this._queue.length = 0; + } else if (msg.error) { + this._error = msg.error; + console.error("[retriever] Init failed:", msg.error); + for (const p of this._queue) { clearTimeout(p.timer); p.reject(new Error(msg.error)); } + this._queue.length = 0; + } + return; + } + + // ── Query response — FIFO ── + const item = this._pending.shift(); + if (!item) return; + clearTimeout(item.timer); + + if (msg.error) { + item.reject(new Error(msg.error)); + } else { + item.resolve({ + results: msg.results || [], + latency_seconds: msg.latency_seconds ?? 0, + }); + } + } + + _send(item) { + if (!this._proc || this._proc.killed) return; + this._proc.stdin.write( + JSON.stringify({ query: item.query, top_n: item.top_n }) + "\n" + ); + } + + /** + * @param {string} query + * @param {number} topN + * @returns {Promise<{ results: Array, latency_seconds: number }>} + */ + retrieve(query, topN = 5) { + // Restart if crashed + if (!this._proc) this._start(); + + return new Promise((resolve, reject) => { + if (this._error) { + return reject(new Error(this._error)); + } + + const timeoutMs = this._ready ? QUERY_TIMEOUT_MS : BOOT_TIMEOUT_MS; + const item = { query, top_n: topN, resolve, reject, timer: null }; + + item.timer = setTimeout(() => { + // Remove from whichever queue it's in + let idx = this._queue.indexOf(item); + if (idx !== -1) this._queue.splice(idx, 1); + idx = this._pending.indexOf(item); + if (idx !== -1) this._pending.splice(idx, 1); + reject(new Error(`Retriever timed out after ${timeoutMs}ms`)); + }, timeoutMs); + + if (this._ready) { + // Send immediately and wait for response + this._pending.push(item); + this._send(item); + } else { + // Queue until daemon signals ready + this._queue.push(item); + } + }); + } +} + +// Singleton — one daemon for the lifetime of the Node process +const retriever = new PythonRetriever(); + +module.exports = { retrieve: (q, n) => retriever.retrieve(q, n) }; diff --git a/web/server/start.js b/web/server/start.js new file mode 100644 index 0000000..21aa4e8 --- /dev/null +++ b/web/server/start.js @@ -0,0 +1,40 @@ +/** + * start.js — safe server launcher + * Kills any process already on PORT before starting index.js. + * Run with: node web/server/start.js + */ +const { execSync, spawn } = require("child_process"); +const PORT = process.env.PORT || 5000; + +function killPort(port) { + try { + if (process.platform === "win32") { + const out = execSync(`netstat -ano | findstr :${port}`, { encoding: "utf-8" }).trim(); + const pids = new Set(); + for (const line of out.split("\n")) { + if (!line.includes("LISTENING")) continue; + const pid = line.trim().split(/\s+/).pop(); + if (pid && pid !== "0") pids.add(pid); + } + for (const pid of pids) { + console.log(`[start] Killing stale process PID ${pid} on port ${port}`); + execSync(`taskkill /PID ${pid} /F`, { stdio: "ignore" }); + } + } else { + execSync(`fuser -k ${port}/tcp`, { stdio: "ignore" }); + } + } catch { + // No process on that port — fine + } +} + +killPort(PORT); + +const proc = spawn(process.execPath, [require("path").join(__dirname, "index.js")], { + stdio: "inherit", + env: process.env, +}); + +proc.on("exit", (code) => process.exit(code ?? 0)); +process.on("SIGINT", () => proc.kill("SIGINT")); +process.on("SIGTERM", () => proc.kill("SIGTERM"));