-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
258 lines (234 loc) · 7.81 KB
/
main.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { Logger } from '@nestjs/common';
import { LLMProvider } from './llm-provider';
import express, { Express, Request, Response, NextFunction } from 'express';
import { downloadAll } from './downloader/universal-utils';
import { MessageInput } from './types';
import * as dotenv from 'dotenv';
// Load environment variables
dotenv.config();
process.on('uncaughtException', error => {
console.error('Uncaught Exception:', error);
console.error('Stack trace:', error.stack);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise);
console.error('Reason:', reason);
if (reason instanceof Error) {
console.error('Stack trace:', reason.stack);
}
process.exit(1);
});
export class App {
private readonly logger = new Logger(App.name);
private app: Express;
private server: ReturnType<Express['listen']> | null = null;
private readonly PORT: number;
private llmProvider: LLMProvider;
constructor(llmProvider: LLMProvider) {
this.app = express();
this.app.use(
express.json({
limit: '50mb',
verify: (req, res, buf) => {
if (buf?.length > 50 * 1024 * 1024) {
throw new Error('Request body exceeds 50MB limit');
}
},
}),
);
this.PORT = parseInt(process.env.PORT || '8001', 10);
this.llmProvider = llmProvider;
this.logger.log(`App initialized with PORT: ${this.PORT}`);
this.app.use(
(err: Error, req: Request, res: Response, next: NextFunction) => {
this.logger.error('Global error handler caught:', err);
console.error('Stack trace:', err.stack);
res.status(500).json({
error: 'Internal server error',
message: err.message,
stack: process.env.NODE_ENV === 'development' ? err.stack : undefined,
});
},
);
// Handle shutdown signals
const signals = ['SIGTERM', 'SIGINT'];
for (const signal of signals) {
process.on(signal, async () => {
this.logger.log(
`Received ${signal} signal. Starting graceful shutdown...`,
);
await this.shutdown();
});
}
}
async shutdown(): Promise<void> {
this.logger.log('Initiating graceful shutdown...');
try {
// Close server first to stop accepting new connections
if (this.server) {
await new Promise<void>((resolve, reject) => {
this.server!.close(err => {
if (err) reject(err);
else resolve();
});
});
this.logger.log('HTTP server closed successfully');
}
// Cleanup LLM provider resources
if (this.llmProvider) {
await this.llmProvider.initialize(); // Ensure it's properly initialized before cleanup
this.logger.log('LLM provider resources cleaned up');
}
this.logger.log('Graceful shutdown completed');
process.exit(0);
} catch (error) {
this.logger.error('Error during graceful shutdown:', error);
process.exit(1);
}
}
setupRoutes(): void {
this.logger.log('Setting up routes...');
this.app.post('/chat/completions', this.handleChatRequest.bind(this));
this.app.get('/tags', this.handleModelTagsRequest.bind(this));
this.app.get('/models', this.handleModelTagsRequest.bind(this));
this.logger.log('Routes set up successfully.');
}
private async handleChatRequest(req: Request, res: Response): Promise<void> {
this.logger.log('Received chat request.');
try {
const input = req.body as MessageInput & { stream?: boolean };
const model = input.model;
this.logger.log(`Received chat request for model: ${model}`);
this.logger.debug(
`Request messages: "${JSON.stringify(input.messages).slice(0, 100)}"`,
);
if (input.stream) {
// Handle streaming response
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
this.logger.debug('Response headers set for streaming.');
const stream = this.llmProvider.chatStream(input);
for await (const chunk of stream) {
if (!res.writableEnded) {
res.write(`data: ${JSON.stringify(chunk)}\n\n`);
}
}
if (!res.writableEnded) {
res.write('data: [DONE]\n\n');
res.end();
}
} else {
// Handle regular response
// TODO make it to dynamic Now is 200 second by defult.
const response = await this.llmProvider.chat(input, 200000);
res.json({
model: input.model,
choices: [
{
message: {
role: 'assistant',
content: response,
},
},
],
});
}
} catch (error) {
this.logger.error('Error in chat endpoint:', error);
console.error(
'Stack trace:',
error instanceof Error ? error.stack : error,
);
res.status(500).json({
error: 'Internal server error',
message: error instanceof Error ? error.message : 'Unknown error',
stack:
process.env.NODE_ENV === 'development'
? error instanceof Error
? error.stack
: undefined
: undefined,
});
}
}
private async handleModelTagsRequest(
req: Request,
res: Response,
): Promise<void> {
this.logger.log('Received tags request.');
try {
this.logger.debug(JSON.stringify(req.body));
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
this.logger.debug('Response headers set for streaming.');
await this.llmProvider.getModelTags(res);
} catch (error) {
this.logger.error('Error in tags endpoint:', error);
console.error(
'Stack trace:',
error instanceof Error ? error.stack : error,
);
res.status(500).json({
error: 'Internal server error',
message: error instanceof Error ? error.message : 'Unknown error',
stack:
process.env.NODE_ENV === 'development'
? error instanceof Error
? error.stack
: undefined
: undefined,
});
}
}
async start(): Promise<void> {
try {
this.setupRoutes();
// Create server instance and store it
this.server = this.app.listen(this.PORT, () => {
this.logger.log(`Server running on port ${this.PORT}`);
});
// Handle server-specific errors
this.server.on('error', error => {
this.logger.error('Server error:', error);
console.error(
'Stack trace:',
error instanceof Error ? error.stack : error,
);
});
} catch (error) {
this.logger.error('Failed to start server:', error);
console.error(
'Stack trace:',
error instanceof Error ? error.stack : error,
);
throw error;
}
}
}
async function main() {
const logger = new Logger('Main');
try {
logger.log('Starting application initialization...');
await downloadAll();
logger.log('Models downloaded successfully.');
const llmProvider = new LLMProvider();
await llmProvider.initialize();
logger.log('LLM provider initialized successfully.');
const app = new App(llmProvider);
await app.start();
logger.log('Application started successfully.');
} catch (error) {
logger.error('Failed to start the application:', error);
logger.error('Stack trace:', error instanceof Error ? error.stack : error);
process.exit(1);
}
}
main().catch(error => {
console.error('Fatal error during application startup:');
console.error(error);
console.error('Stack trace:', error instanceof Error ? error.stack : error);
process.exit(1);
});