Skip to content

Commit

Permalink
FIX revert to upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
wabscale committed Mar 25, 2024
1 parent bb34c8b commit 942074c
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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: {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
},
]),
Expand Down Expand Up @@ -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),
Expand All @@ -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<any>;

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;
}
Expand All @@ -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();
},
);
Expand Down

0 comments on commit 942074c

Please sign in to comment.