-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathama.ts
52 lines (42 loc) · 1.51 KB
/
ama.ts
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
import type { NextApiRequest, NextApiResponse } from "next";
import { AxiosError } from "axios";
import { ama } from "@/docs/agents/ama";
export default async function handler(request: NextApiRequest, response: NextApiResponse) {
switch (request.method) {
case "POST":
try {
const { question, language, doc } = request.body;
const hyvResponse = await ama({ question, language, doc });
const message = await hyvResponse.message;
const paths = hyvResponse.paths;
response.status(200).json({ message, paths });
} catch (error) {
const err = error as AxiosError;
let errorMessage = "";
// If we have an error message from the data.error.message, use that
if (err.response?.data?.error?.message && err.response.data.error.message !== "") {
errorMessage = err.response.data.error.message;
}
// If there's no message but there's a code, use the code
else if (err.response?.data?.error?.code) {
errorMessage = err.response.data.error.code;
}
// If there's neither a message nor a code, use the error's own message
else if (err.message) {
errorMessage = err.message;
} else {
errorMessage = "UNKNOWN_ERROR";
}
if (errorMessage.includes("JSON5: invalid character")) {
errorMessage =
"There was a problem when interacting with the AI, please try again!";
}
console.log(errorMessage);
response.status(500).json({ message: errorMessage });
}
break;
default:
response.status(404).send("Not Found");
break;
}
}