MSAgent-Chat/server/src/index.ts

57 lines
1.4 KiB
TypeScript
Raw Normal View History

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';
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-02 23:42:03 -04:00
if (config.tts.enabled) {
tts = new TTSClient(config.tts);
app.register(FastifyStatic, {
root: config.tts.tempDir,
prefix: "/api/tts/"
});
}
let room = new MSAgentChatRoom(tts);
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});