2024-07-01 23:34:28 -04:00
|
|
|
import Fastify from 'fastify';
|
|
|
|
import FastifyWS from '@fastify/websocket';
|
2024-07-02 23:42:03 -04:00
|
|
|
import FastifyStatic from '@fastify/static';
|
2024-07-01 23:34:28 -04:00
|
|
|
import { Client } from './client.js';
|
2024-07-02 00:30:10 -04:00
|
|
|
import { MSAgentChatRoom } from './room.js';
|
2024-07-02 23:42:03 -04:00
|
|
|
import * as toml from 'toml';
|
|
|
|
import { IConfig } from './config.js';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import { TTSClient } from './tts.js';
|
2024-07-03 02:12:02 -04:00
|
|
|
import path from 'path';
|
|
|
|
import { fileURLToPath } from 'url';
|
2024-07-02 23:42:03 -04:00
|
|
|
|
|
|
|
let config: IConfig;
|
|
|
|
let configPath: string;
|
|
|
|
if (process.argv.length < 3)
|
|
|
|
configPath = "./config.toml";
|
|
|
|
else
|
|
|
|
configPath = process.argv[2];
|
|
|
|
|
|
|
|
if (!fs.existsSync(configPath)) {
|
|
|
|
console.error(`${configPath} not found. Please copy config.example.toml and fill out fields.`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
let configRaw = fs.readFileSync(configPath, "utf-8");
|
|
|
|
config = toml.parse(configRaw);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Failed to read or parse ${configPath}: ${(e as Error).message}`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2024-07-01 23:34:28 -04:00
|
|
|
|
|
|
|
const app = Fastify({
|
|
|
|
logger: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
app.register(FastifyWS);
|
|
|
|
|
2024-07-02 23:42:03 -04:00
|
|
|
let tts = null;
|
2024-07-02 00:30:10 -04:00
|
|
|
|
2024-07-03 02:12:02 -04:00
|
|
|
app.register(FastifyStatic, {
|
|
|
|
root: path.dirname(fileURLToPath(import.meta.url)) + "/../../webapp/dist/",
|
|
|
|
prefix: "/",
|
|
|
|
decorateReply: true
|
|
|
|
});
|
|
|
|
|
2024-07-02 23:42:03 -04:00
|
|
|
if (config.tts.enabled) {
|
|
|
|
tts = new TTSClient(config.tts);
|
|
|
|
app.register(FastifyStatic, {
|
|
|
|
root: config.tts.tempDir,
|
2024-07-03 02:12:02 -04:00
|
|
|
prefix: "/api/tts/",
|
|
|
|
decorateReply: false
|
2024-07-02 23:42:03 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-03 02:12:02 -04:00
|
|
|
|
|
|
|
let room = new MSAgentChatRoom(config.chat, tts);
|
2024-07-02 23:42:03 -04:00
|
|
|
|
|
|
|
app.register(async app => {
|
|
|
|
app.get("/socket", {websocket: true}, (socket, req) => {
|
|
|
|
let client = new Client(socket, room);
|
|
|
|
room.addClient(client);
|
|
|
|
});
|
2024-07-01 23:34:28 -04:00
|
|
|
});
|
|
|
|
|
2024-07-02 23:42:03 -04:00
|
|
|
|
|
|
|
app.listen({host: config.http.host, port: config.http.port});
|