@myassis/gateway 1.0.26 → 1.0.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +30 -18
- package/dist/services/session/Session.js +3 -3
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -209,38 +209,50 @@ else {
|
|
|
209
209
|
app.use(errorHandler_js_1.errorHandler);
|
|
210
210
|
// 创建 HTTP Server(而非 app.listen,以便挂载 WebSocket)
|
|
211
211
|
const server = http_1.default.createServer(app);
|
|
212
|
-
// 初始化 WebSocket 服务
|
|
213
|
-
WebSocketService_js_1.webSocketService.initialize(server);
|
|
214
|
-
// 启动任务调度器
|
|
215
|
-
TaskSchedulerService_js_1.taskSchedulerService.start();
|
|
216
212
|
// 端口自动顺延:被占用时自动尝试下一个端口
|
|
213
|
+
const configuredPort = index_js_1.appConfig.port;
|
|
214
|
+
let schedulerStarted = false;
|
|
215
|
+
let webSocketInitialized = false;
|
|
217
216
|
const startServer = (port) => {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
index_js_1.appConfig.port = port;
|
|
221
|
-
if (port !== index_js_1.appConfig.port) {
|
|
222
|
-
logger.info(`Port ${index_js_1.appConfig.port} is in use, fallback to port ${port}`);
|
|
223
|
-
}
|
|
224
|
-
logger.info(`我的助手 Gateway Service running on port ${port}`);
|
|
225
|
-
}).on('error', (err) => {
|
|
217
|
+
const handleListenError = (err) => {
|
|
218
|
+
server.removeListener('error', handleListenError);
|
|
226
219
|
if (err.code === 'EADDRINUSE') {
|
|
227
220
|
const nextPort = port + 1;
|
|
228
|
-
if (nextPort <= (
|
|
221
|
+
if (nextPort <= (configuredPort + 10)) {
|
|
229
222
|
logger.warn(`Port ${port} is in use, trying ${nextPort}...`);
|
|
230
223
|
startServer(nextPort);
|
|
231
224
|
}
|
|
232
225
|
else {
|
|
233
|
-
logger.error(`All ports from ${
|
|
226
|
+
logger.error(`All ports from ${configuredPort} to ${configuredPort + 10} are in use`);
|
|
234
227
|
process.exit(1);
|
|
235
228
|
}
|
|
229
|
+
return;
|
|
236
230
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
231
|
+
logger.error(`Server error: ${err.message}`);
|
|
232
|
+
process.exit(1);
|
|
233
|
+
};
|
|
234
|
+
server.once('error', handleListenError);
|
|
235
|
+
server.listen(port, () => {
|
|
236
|
+
server.removeListener('error', handleListenError);
|
|
237
|
+
if (port !== configuredPort) {
|
|
238
|
+
logger.info(`Port ${configuredPort} is in use, fallback to port ${port}`);
|
|
239
|
+
}
|
|
240
|
+
// 动态更新 appConfig.port(供 WebSocket 广播实际端口)
|
|
241
|
+
index_js_1.appConfig.port = port;
|
|
242
|
+
// HTTP Server 监听成功后再初始化 WebSocket,避免端口冲突时 WebSocketServer 误报 EADDRINUSE
|
|
243
|
+
if (!webSocketInitialized) {
|
|
244
|
+
WebSocketService_js_1.webSocketService.initialize(server);
|
|
245
|
+
webSocketInitialized = true;
|
|
246
|
+
}
|
|
247
|
+
// HTTP Server 启动成功后再启动任务调度器,避免端口占用时重复启动后台任务
|
|
248
|
+
if (!schedulerStarted) {
|
|
249
|
+
TaskSchedulerService_js_1.taskSchedulerService.start();
|
|
250
|
+
schedulerStarted = true;
|
|
240
251
|
}
|
|
252
|
+
logger.info(`我的助手 Gateway Service running on port ${port}`);
|
|
241
253
|
});
|
|
242
254
|
};
|
|
243
|
-
startServer(
|
|
255
|
+
startServer(configuredPort);
|
|
244
256
|
// 优雅关闭
|
|
245
257
|
process.on('SIGTERM', () => {
|
|
246
258
|
logger.info('收到 SIGTERM,正在关闭...');
|
|
@@ -501,6 +501,9 @@ class Session {
|
|
|
501
501
|
}
|
|
502
502
|
};
|
|
503
503
|
const toolCalls = [];
|
|
504
|
+
// 获取本地工具定义
|
|
505
|
+
const tools = (0, index_js_1.getToolDefinitions)();
|
|
506
|
+
const models = (await dataService_js_1.modelsService.list(token)).data.map(x => (0, models_js_1.toModel)(x));
|
|
504
507
|
// 递归处理函数(支持多轮工具调用)
|
|
505
508
|
const processModelResponse = async () => {
|
|
506
509
|
if (!this.abortController || !this.abortController.signal || this.abortController.signal.aborted) {
|
|
@@ -547,9 +550,6 @@ class Session {
|
|
|
547
550
|
}
|
|
548
551
|
}
|
|
549
552
|
}
|
|
550
|
-
// 获取本地工具定义
|
|
551
|
-
const tools = (0, index_js_1.getToolDefinitions)();
|
|
552
|
-
const models = (await dataService_js_1.modelsService.list(token)).data.map(x => (0, models_js_1.toModel)(x));
|
|
553
553
|
// 使用 LLMClient 进行流式调用
|
|
554
554
|
if (!this.abortController || !this.abortController.signal || this.abortController.signal.aborted) {
|
|
555
555
|
throw Error('aborted');
|