-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.ts
39 lines (32 loc) · 896 Bytes
/
server.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
import { Hono } from "hono";
import { SentientAI } from "./src/SentientAI";
const app = new Hono();
const sentai = new SentientAI();
app.get("/", (c) => {
return c.text("hello world, Sentient AI!");
});
app.post("/ask", async (c) => {
const apiKey = c.req.header("API-KEY");
if (!apiKey) {
console.warn("no SENTAI API-KEY provided");
}
try {
let content = c.req.query("q") || c.req.query("content");
if (!content) {
const body = await c.req.json();
content = body.q || body.content;
}
if (!content) {
return c.json({ error: "question is required." }, 400);
}
const response = await sentai.agent.execute(content);
return c.json({ data: response });
} catch (e) {
console.error(e);
return c.json({ error: "Internal server error." }, 400);
}
});
export default {
port: process.env.PORT || 8000,
fetch: app.fetch,
};