MSAgent-Chat/server/src/room.ts
Elijah R 1c1d86529b - add new agents to config.example.toml
- remove message sanitization from server side. It can be done on the client side if necessary
2024-07-10 22:20:04 -04:00

84 lines
3 KiB
TypeScript

import { MSAgentAddUserMessage, MSAgentChatMessage, MSAgentInitMessage, MSAgentProtocolMessage, MSAgentProtocolMessageType, MSAgentRemoveUserMessage } from "@msagent-chat/protocol";
import { Client } from "./client.js";
import { TTSClient } from "./tts.js";
import { AgentConfig, ChatConfig } from "./config.js";
import * as htmlentities from 'html-entities';
export class MSAgentChatRoom {
agents: AgentConfig[];
clients: Client[];
tts: TTSClient | null;
msgId : number = 0;
config: ChatConfig;
constructor(config: ChatConfig, agents: AgentConfig[], tts: TTSClient | null) {
this.agents = agents;
this.clients = [];
this.config = config;
this.tts = tts;
}
addClient(client: Client) {
this.clients.push(client);
client.on('close', () => {
this.clients.splice(this.clients.indexOf(client), 1);
if (client.username === null) return;
let msg: MSAgentRemoveUserMessage = {
op: MSAgentProtocolMessageType.RemoveUser,
data: {
username: client.username
}
};
for (const _client of this.getActiveClients()) {
_client.send(msg);
}
});
client.on('join', () => {
let initmsg : MSAgentInitMessage = {
op: MSAgentProtocolMessageType.Init,
data: {
username: client.username!,
agent: client.agent!,
charlimit: this.config.charlimit,
users: this.clients.filter(c => c.username !== null).map(c => {
return {
username: c.username!,
agent: c.agent!
}
})
}
};
client.send(initmsg);
let msg: MSAgentAddUserMessage = {
op: MSAgentProtocolMessageType.AddUser,
data: {
username: client.username!,
agent: client.agent!
}
}
for (const _client of this.getActiveClients().filter(c => c !== client)) {
_client.send(msg);
}
});
client.on('talk', async message => {
let msg: MSAgentChatMessage = {
op: MSAgentProtocolMessageType.Chat,
data: {
username: client.username!,
message: message
}
};
if (this.tts !== null) {
let filename = await this.tts.synthesizeToFile(message, (++this.msgId).toString(10));
msg.data.audio = "/api/tts/" + filename;
}
for (const _client of this.getActiveClients()) {
_client.send(msg);
}
});
}
private getActiveClients() {
return this.clients.filter(c => c.username !== null);
}
}