From 942074cfc39ed39ffff69c1176bc81cc24673fcf Mon Sep 17 00:00:00 2001 From: John McCann Cunniff Jr Date: Sun, 24 Mar 2024 22:44:38 -0400 Subject: [PATCH] FIX revert to upstream --- server/index.ts | 67 +++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/server/index.ts b/server/index.ts index d008d9d..858e002 100644 --- a/server/index.ts +++ b/server/index.ts @@ -10,12 +10,17 @@ import express, { import bodyParser from "body-parser"; import { createClient } from "@supabase/supabase-js"; import { Completion, User, DocumentChange } from "shared"; -import { CompletionSource } from "./types"; -import { constructOpenAICompletionRequest } from "./completion"; +import { CompletionType, DEFAULT_CONFIGURATION } from "./types"; +import { + constructChatCompletionRequest, + constructTextCompletionRequest, +} from "./completion"; +import * as dotenv from "dotenv"; + +dotenv.config(); const logger = pino({ level: "info", - prettifier: require("pino-pretty")(), }); const app = express(); @@ -42,9 +47,7 @@ if (!OPENAI_API_KEY) { throw new Error("Missing OPENAI_API_KEY env variable"); } -export const supabase = createClient(supabaseUrl, supabaseKey, { - auth: { persistSession: false }, -}); +export const supabase = createClient(supabaseUrl, supabaseKey); interface UserRequest extends Request { body: { @@ -83,14 +86,14 @@ const authMiddleware = ( next: NextFunction, ) => { const authKey = req.headers["auth-key"]; - if (!authKey || authKey !== process.env.AUTH_KEY) { + if ((!authKey || authKey !== process.env.AUTH_KEY) && process.env.PROD) { return res.status(403).send("Forbidden: Invalid AUTH key"); } next(); }; -app.get("/", async (req: Request, res) => { - return res.status(200).json({'status': 'alive'}).send(); +app.get("/health", async (_req, res) => { + return res.status(200).send("ok"); }); app.post("/user", authMiddleware, async (req: UserRequest, res) => { @@ -128,8 +131,10 @@ app.post("/user", authMiddleware, async (req: UserRequest, res) => { supabase.from("UserSettings").insert([ { net_id: req.body.netId, - model: OpenAIModel.Turbo35, - source: CompletionSource.OpenAI, + model: DEFAULT_CONFIGURATION.model, + url: DEFAULT_CONFIGURATION.url, + max_tokens: DEFAULT_CONFIGURATION.maxTokens, + completion_type: DEFAULT_CONFIGURATION.completionType, enabled: true, }, ]), @@ -176,9 +181,6 @@ app.get("/settings/:id", authMiddleware, async (req: Request, res) => { ); }); -// Settings -// completion endpoint - app.post("/completion", authMiddleware, async (req: CompletionRequest, res) => { return await ResultAsync.fromPromise( supabase.from("UserSettings").select("*").eq("net_id", req.body.netId), @@ -200,18 +202,37 @@ app.post("/completion", authMiddleware, async (req: CompletionRequest, res) => { }); } - const source = ok.data[0].source; + const completionType = ok.data[0].completion_type; + const model = ok.data[0].model; + const url = ok.data[0].url; + const maxTokens = ok.data[0].max_tokens; + + logger.info( + `user ${req.body.netId} has completion enabled with model ${model} and completion type ${completionType}`, + ); let request: Promise; - switch (source) { - case CompletionSource.OpenAI: { - request = constructOpenAICompletionRequest({ + switch (completionType) { + case CompletionType.Chat: { + request = constructChatCompletionRequest({ + prompt: req.body.prompt, + context: req.body.context, + model, + fileExtension: req.body.fileExtension, + url, + maxTokens, + }); + break; + } + case CompletionType.Text: { + request = constructTextCompletionRequest({ prompt: req.body.prompt, context: req.body.context, - model: ok.data[0].model, + model, fileExtension: req.body.fileExtension, - maxTokens: 1000, + url, + maxTokens, }); break; } @@ -222,20 +243,18 @@ app.post("/completion", authMiddleware, async (req: CompletionRequest, res) => { } return ResultAsync.fromPromise(request, (error) => { - logger.info(error); return error; }).match( (ok) => { - res + return res .status(200) .json({ - completion: ok.data.response, + completion: ok, }) .send(); }, (err) => { logger.error(err); - req.log.error(err); return res.status(500).send(); }, );