Spaces:
Sleeping
Sleeping
| import fs from "node:fs/promises"; | |
| import http from "node:http"; | |
| import path from "node:path"; | |
| import { buildAgentCardPayload, DEFAULT_AGENT_DIR, DEFAULT_MODEL_REF } from "./agent-card.js"; | |
| const HOST = process.env.HOST ?? "127.0.0.1"; | |
| const PORT = Number(process.env.PORT ?? 4173); | |
| const ROOT_DIR = process.cwd(); | |
| const PUBLIC_DIR = path.join(ROOT_DIR, "public"); | |
| const AGENT_DIR = path.join(ROOT_DIR, DEFAULT_AGENT_DIR); | |
| const DEMO_MODEL_REF = process.env.HF_LAUNCH_DEMO_MODEL ?? DEFAULT_MODEL_REF; | |
| function contentTypeFor(filePath) { | |
| if (filePath.endsWith(".html")) { | |
| return "text/html; charset=utf-8"; | |
| } | |
| if (filePath.endsWith(".js")) { | |
| return "application/javascript; charset=utf-8"; | |
| } | |
| if (filePath.endsWith(".css")) { | |
| return "text/css; charset=utf-8"; | |
| } | |
| if (filePath.endsWith(".json")) { | |
| return "application/json; charset=utf-8"; | |
| } | |
| return "text/plain; charset=utf-8"; | |
| } | |
| function sendJson(response, statusCode, payload) { | |
| response.writeHead(statusCode, { | |
| "Content-Type": "application/json; charset=utf-8", | |
| "Cache-Control": "no-store" | |
| }); | |
| response.end(`${JSON.stringify(payload, null, 2)}\n`); | |
| } | |
| async function serveStatic(response, filePath) { | |
| const body = await fs.readFile(filePath); | |
| response.writeHead(200, { | |
| "Content-Type": contentTypeFor(filePath) | |
| }); | |
| response.end(body); | |
| } | |
| function notFound(response) { | |
| response.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }); | |
| response.end("Not found\n"); | |
| } | |
| function internalError(response, error) { | |
| sendJson(response, 500, { | |
| error: error.message | |
| }); | |
| } | |
| async function handleRequest(request, response) { | |
| const url = new URL(request.url ?? "/", `http://${request.headers.host ?? `${HOST}:${PORT}`}`); | |
| if (url.pathname === "/api/agent-card") { | |
| const payload = await buildAgentCardPayload({ | |
| rootDir: ROOT_DIR, | |
| agentDir: AGENT_DIR, | |
| modelRef: url.searchParams.get("model") ?? DEMO_MODEL_REF | |
| }); | |
| sendJson(response, 200, payload); | |
| return; | |
| } | |
| const relativePath = url.pathname === "/" ? "index.html" : url.pathname.slice(1); | |
| const filePath = path.join(PUBLIC_DIR, relativePath); | |
| if (!filePath.startsWith(PUBLIC_DIR)) { | |
| notFound(response); | |
| return; | |
| } | |
| try { | |
| await serveStatic(response, filePath); | |
| } catch (error) { | |
| if (error.code === "ENOENT") { | |
| notFound(response); | |
| return; | |
| } | |
| throw error; | |
| } | |
| } | |
| const server = http.createServer((request, response) => { | |
| handleRequest(request, response).catch((error) => { | |
| internalError(response, error); | |
| }); | |
| }); | |
| server.listen(PORT, HOST, () => { | |
| console.log(`hf-launch demo available at http://${HOST}:${PORT}`); | |
| console.log(`agent: ${AGENT_DIR}`); | |
| console.log(`model: ${DEMO_MODEL_REF}`); | |
| }); | |