-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathserver.ts
More file actions
92 lines (79 loc) · 2.34 KB
/
server.ts
File metadata and controls
92 lines (79 loc) · 2.34 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Hono } from 'hono';
import { logger } from './src/logger/winston';
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) {
logger.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.execute(content);
return c.json({ data: response });
} catch (e) {
console.log('error', e);
logger.error('Error in /ask', { error: e });
return c.json({ error: 'Internal server error.' }, 400);
}
});
app.post('/stream', async c => {
const apiKey = c.req.header('API-KEY');
if (!apiKey) {
logger.warn('no SENTAI API-KEY provided');
}
try {
const formData = await c.req.formData();
let content = formData.get('text');
const recentMessages = formData.get('recentMessages');
if (recentMessages) {
content = recentMessages + '\n' + content;
}
return sentai.stream(content as string);
} catch (e) {
console.log('error', e);
logger.error('Error in /stream', { error: e });
return c.json({ error: 'Internal server error.' }, 400);
}
});
app.get('/raw', async c => {
const apiKey = c.req.header('API-KEY');
if (!apiKey) {
logger.warn('no SENTAI API-KEY provided');
}
try {
const toolName = c.req.query('tool');
if (!toolName) {
return c.json({ error: 'tool parameter is required' }, 400);
}
// Get all query parameters and pass them as params
const params: Record<string, any> = {};
for (const [key, value] of Object.entries(c.req.query())) {
if (key !== 'tool') {
params[key] = value;
}
}
const rawData = await sentai.getRawData(toolName, params);
return c.json({ data: rawData });
} catch (e: any) {
console.log('error', e);
logger.error('Error in /raw', { error: e });
return c.json({ error: e.message || 'Internal server error' }, 500);
}
});
export default {
port: process.env.PORT || 8000,
fetch: app.fetch,
idleTimeout: 120,
};