File size: 1,697 Bytes
b16e3f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cors from "@elysiajs/cors";
import { Elysia } from "elysia";
import Stream from "@elysiajs/stream";
import { handleChatCompletion } from "./src/openai";
import bearer from "@elysiajs/bearer";
import { ENV } from "./env";
import { AppLogger } from "./src/tools";
import { AgentManager } from "./src/agent";

AgentManager.getInstance();

const app = new Elysia()
  .use(
    cors({
      origin: "*",
      allowedHeaders: ["*"],
      methods: ["GET", "POST", "OPTIONS"],
    })
  )
  .use(bearer())
  .onBeforeHandle(({ bearer, set, request }) => {
    if (bearer !== ENV.API_TOKEN) {
      AppLogger.w(
        "handle",
        `Incomming ${request.method.toUpperCase()} request have been denied`,
        {
          url: request.url,
          reason: "Unauthorized",
        }
      );
      set.status = 401;
      return {
        status: 401,
        body: "Unauthorized",
      };
    }
    AppLogger.i("handle", `Incomming ${request.method.toUpperCase()} request`, {
      url: request.url,
    });
  })
  .options("*", () => "OK")
  .post("/v1/chat/completions", async ({ body }) => {
    const req = body as any;
    const isStream = req.stream ?? false;
    if (!isStream) {
      return handleChatCompletion({
        messages: req.messages,
      });
    }
    return new Stream(async (stream) => {
      return handleChatCompletion({
        messages: req.messages,
        streamRes: stream,
      });
    });
  })
  .all("*", () => "Not Found")
  .listen(3000);

AppLogger.i("START", "Server running on port", ENV.APP_PORT);
AppLogger.i("START", "Server running with token", ENV.API_TOKEN);
AppLogger.i("START", "Server running with endpoints", ["/v1/chat/completions"]);