File size: 2,783 Bytes
e38a84f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env node

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}`);
});