a few fixes and improvements

This commit is contained in:
Elijah R 2024-07-03 02:12:02 -04:00
parent 7d30426a08
commit ba3ce91410
8 changed files with 53 additions and 7 deletions

View file

@ -37,6 +37,7 @@ export interface MSAgentInitMessage extends MSAgentProtocolMessage {
data: {
username: string
agent: string
charlimit: number
users: {
username: string,
agent: string

View file

@ -2,6 +2,9 @@
host = "127.0.0.1"
port = 3000
[chat]
charlimit = 100
[tts]
enabled = true
# https://git.computernewb.com/computernewb/SAPIServer

View file

@ -41,6 +41,10 @@ export class Client extends EventEmitter {
send(msg: MSAgentProtocolMessage) {
return new Promise<void>((res, rej) => {
if (this.socket.readyState !== WebSocket.OPEN) {
res();
return;
}
this.socket.send(JSON.stringify(msg), err => {
if (err) {
rej(err);
@ -66,7 +70,15 @@ export class Client extends EventEmitter {
this.socket.close();
return;
}
this.username = htmlentities.encode(joinMsg.data.username);
let username = htmlentities.encode(joinMsg.data.username);
if (this.room.clients.some(u => u.username === username)) {
let i = 1;
let uo = username;
do {
username = uo + i++;
} while (this.room.clients.some(u => u.username === username))
}
this.username = username;
this.agent = htmlentities.encode(joinMsg.data.agent);
this.emit('join');
break;
@ -76,7 +88,8 @@ export class Client extends EventEmitter {
if (!talkMsg.data || !talkMsg.data.msg) {
return;
}
this.emit('talk', htmlentities.encode(talkMsg.data.msg));
if (talkMsg.data.msg.length > this.room.config.charlimit) return;
this.emit('talk', talkMsg.data.msg);
break;
}
}

View file

@ -3,6 +3,7 @@ export interface IConfig {
host: string;
port: number;
}
chat: ChatConfig;
tts: TTSConfig
}
@ -12,4 +13,8 @@ export interface TTSConfig {
voice: string;
tempDir: string;
wavExpirySeconds: number;
}
export interface ChatConfig {
charlimit: number;
}

View file

@ -7,6 +7,8 @@ import * as toml from 'toml';
import { IConfig } from './config.js';
import * as fs from 'fs';
import { TTSClient } from './tts.js';
import path from 'path';
import { fileURLToPath } from 'url';
let config: IConfig;
let configPath: string;
@ -36,15 +38,23 @@ app.register(FastifyWS);
let tts = null;
app.register(FastifyStatic, {
root: path.dirname(fileURLToPath(import.meta.url)) + "/../../webapp/dist/",
prefix: "/",
decorateReply: true
});
if (config.tts.enabled) {
tts = new TTSClient(config.tts);
app.register(FastifyStatic, {
root: config.tts.tempDir,
prefix: "/api/tts/"
prefix: "/api/tts/",
decorateReply: false
});
}
let room = new MSAgentChatRoom(tts);
let room = new MSAgentChatRoom(config.chat, tts);
app.register(async app => {
app.get("/socket", {websocket: true}, (socket, req) => {

View file

@ -1,13 +1,18 @@
import { MSAgentAddUserMessage, MSAgentChatMessage, MSAgentInitMessage, MSAgentProtocolMessage, MSAgentProtocolMessageType, MSAgentRemoveUserMessage } from "@msagent-chat/protocol";
import { Client } from "./client.js";
import { TTSClient } from "./tts.js";
import { ChatConfig } from "./config.js";
import * as htmlentities from 'html-entities';
export class MSAgentChatRoom {
clients: Client[];
tts: TTSClient | null;
msgId : number = 0;
constructor(tts: TTSClient | null) {
config: ChatConfig;
constructor(config: ChatConfig, tts: TTSClient | null) {
this.clients = [];
this.config = config;
this.tts = tts;
}
@ -32,6 +37,7 @@ export class MSAgentChatRoom {
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!,
@ -57,7 +63,7 @@ export class MSAgentChatRoom {
op: MSAgentProtocolMessageType.Chat,
data: {
username: client.username!,
message
message: htmlentities.encode(message)
}
};
if (this.tts !== null) {

View file

@ -16,6 +16,7 @@ export class MSAgentClient {
private events: Emitter;
private users: User[];
private playingAudio: Map<string, HTMLAudioElement> = new Map();
private charlimit: number = 0;
private username: string | null = null;
private agent: string | null = null;
@ -99,6 +100,10 @@ export class MSAgentClient {
this.send(talkMsg);
}
getCharlimit() {
return this.charlimit;
}
private handleMessage(data: string) {
let msg: MSAgentProtocolMessage;
try {
@ -112,6 +117,7 @@ export class MSAgentClient {
let initMsg = msg as MSAgentInitMessage;
this.username = initMsg.data.username;
this.agent = initMsg.data.agent;
this.charlimit = initMsg.data.charlimit;
this.users.push(...initMsg.data.users.map(u => new User(u.username, u.agent)));
this.events.emit('join');
break;
@ -119,6 +125,7 @@ export class MSAgentClient {
case MSAgentProtocolMessageType.AddUser: {
let addUserMsg = msg as MSAgentAddUserMessage
let user = new User(addUserMsg.data.username, addUserMsg.data.agent);
this.users.push(user);
this.events.emit('adduser', user);
break;
}

View file

@ -39,9 +39,10 @@ elements.chatSendBtn.addEventListener('click', () => {
});
async function connectToRoom() {
Room = new MSAgentClient("http://127.0.0.1:3000");
Room = new MSAgentClient(`${window.location.protocol}//${window.location.host}`);
await Room.connect();
await Room.join(elements.logonUsername.value, "test");
elements.chatInput.maxLength = Room.getCharlimit();
logonWindow.hide();
elements.logonView.style.display = "none";
elements.chatView.style.display = "block";