2024-07-02 23:42:03 -04:00
|
|
|
import { MSAgentAddUserMessage, MSAgentChatMessage, MSAgentInitMessage, MSAgentProtocolMessage, MSAgentProtocolMessageType, MSAgentRemoveUserMessage } from "@msagent-chat/protocol";
|
2024-07-01 23:34:28 -04:00
|
|
|
import { Client } from "./client.js";
|
2024-07-02 23:42:03 -04:00
|
|
|
import { TTSClient } from "./tts.js";
|
2024-07-10 01:21:06 -04:00
|
|
|
import { AgentConfig, ChatConfig } from "./config.js";
|
2024-07-03 02:12:02 -04:00
|
|
|
import * as htmlentities from 'html-entities';
|
2024-07-01 23:34:28 -04:00
|
|
|
|
|
|
|
export class MSAgentChatRoom {
|
2024-07-10 01:21:06 -04:00
|
|
|
agents: AgentConfig[];
|
2024-07-01 23:34:28 -04:00
|
|
|
clients: Client[];
|
2024-07-02 23:42:03 -04:00
|
|
|
tts: TTSClient | null;
|
|
|
|
msgId : number = 0;
|
2024-07-03 02:12:02 -04:00
|
|
|
config: ChatConfig;
|
|
|
|
|
2024-07-10 01:21:06 -04:00
|
|
|
constructor(config: ChatConfig, agents: AgentConfig[], tts: TTSClient | null) {
|
|
|
|
this.agents = agents;
|
2024-07-01 23:34:28 -04:00
|
|
|
this.clients = [];
|
2024-07-03 02:12:02 -04:00
|
|
|
this.config = config;
|
2024-07-02 23:42:03 -04:00
|
|
|
this.tts = tts;
|
2024-07-01 23:34:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
addClient(client: Client) {
|
|
|
|
this.clients.push(client);
|
|
|
|
client.on('close', () => {
|
|
|
|
this.clients.splice(this.clients.indexOf(client), 1);
|
2024-07-02 00:30:10 -04:00
|
|
|
if (client.username === null) return;
|
|
|
|
let msg: MSAgentRemoveUserMessage = {
|
|
|
|
op: MSAgentProtocolMessageType.RemoveUser,
|
|
|
|
data: {
|
|
|
|
username: client.username
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for (const _client of this.getActiveClients()) {
|
|
|
|
_client.send(msg);
|
|
|
|
}
|
2024-07-01 23:34:28 -04:00
|
|
|
});
|
2024-07-02 00:30:10 -04:00
|
|
|
client.on('join', () => {
|
2024-07-02 23:42:03 -04:00
|
|
|
let initmsg : MSAgentInitMessage = {
|
|
|
|
op: MSAgentProtocolMessageType.Init,
|
|
|
|
data: {
|
|
|
|
username: client.username!,
|
|
|
|
agent: client.agent!,
|
2024-07-03 02:12:02 -04:00
|
|
|
charlimit: this.config.charlimit,
|
2024-07-02 23:42:03 -04:00
|
|
|
users: this.clients.filter(c => c.username !== null).map(c => {
|
|
|
|
return {
|
|
|
|
username: c.username!,
|
|
|
|
agent: c.agent!
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
client.send(initmsg);
|
2024-07-02 00:30:10 -04:00
|
|
|
let msg: MSAgentAddUserMessage = {
|
|
|
|
op: MSAgentProtocolMessageType.AddUser,
|
|
|
|
data: {
|
2024-07-02 23:42:03 -04:00
|
|
|
username: client.username!,
|
|
|
|
agent: client.agent!
|
2024-07-02 00:30:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const _client of this.getActiveClients().filter(c => c !== client)) {
|
|
|
|
_client.send(msg);
|
|
|
|
}
|
|
|
|
});
|
2024-07-02 23:42:03 -04:00
|
|
|
client.on('talk', async message => {
|
|
|
|
let msg: MSAgentChatMessage = {
|
|
|
|
op: MSAgentProtocolMessageType.Chat,
|
|
|
|
data: {
|
|
|
|
username: client.username!,
|
2024-07-10 22:20:04 -04:00
|
|
|
message: message
|
2024-07-02 23:42:03 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
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);
|
2024-07-02 00:30:10 -04:00
|
|
|
}
|
2024-07-02 23:42:03 -04:00
|
|
|
});
|
2024-07-02 00:30:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private getActiveClients() {
|
|
|
|
return this.clients.filter(c => c.username !== null);
|
2024-07-01 23:34:28 -04:00
|
|
|
}
|
|
|
|
}
|