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: { data: {
username: string username: string
agent: string agent: string
charlimit: number
users: { users: {
username: string, username: string,
agent: string agent: string

View file

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

View file

@ -41,6 +41,10 @@ export class Client extends EventEmitter {
send(msg: MSAgentProtocolMessage) { send(msg: MSAgentProtocolMessage) {
return new Promise<void>((res, rej) => { return new Promise<void>((res, rej) => {
if (this.socket.readyState !== WebSocket.OPEN) {
res();
return;
}
this.socket.send(JSON.stringify(msg), err => { this.socket.send(JSON.stringify(msg), err => {
if (err) { if (err) {
rej(err); rej(err);
@ -66,7 +70,15 @@ export class Client extends EventEmitter {
this.socket.close(); this.socket.close();
return; 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.agent = htmlentities.encode(joinMsg.data.agent);
this.emit('join'); this.emit('join');
break; break;
@ -76,7 +88,8 @@ export class Client extends EventEmitter {
if (!talkMsg.data || !talkMsg.data.msg) { if (!talkMsg.data || !talkMsg.data.msg) {
return; 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; break;
} }
} }

View file

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

View file

@ -7,6 +7,8 @@ import * as toml from 'toml';
import { IConfig } from './config.js'; import { IConfig } from './config.js';
import * as fs from 'fs'; import * as fs from 'fs';
import { TTSClient } from './tts.js'; import { TTSClient } from './tts.js';
import path from 'path';
import { fileURLToPath } from 'url';
let config: IConfig; let config: IConfig;
let configPath: string; let configPath: string;
@ -36,15 +38,23 @@ app.register(FastifyWS);
let tts = null; let tts = null;
app.register(FastifyStatic, {
root: path.dirname(fileURLToPath(import.meta.url)) + "/../../webapp/dist/",
prefix: "/",
decorateReply: true
});
if (config.tts.enabled) { if (config.tts.enabled) {
tts = new TTSClient(config.tts); tts = new TTSClient(config.tts);
app.register(FastifyStatic, { app.register(FastifyStatic, {
root: config.tts.tempDir, 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.register(async app => {
app.get("/socket", {websocket: true}, (socket, req) => { 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 { MSAgentAddUserMessage, MSAgentChatMessage, MSAgentInitMessage, MSAgentProtocolMessage, MSAgentProtocolMessageType, MSAgentRemoveUserMessage } from "@msagent-chat/protocol";
import { Client } from "./client.js"; import { Client } from "./client.js";
import { TTSClient } from "./tts.js"; import { TTSClient } from "./tts.js";
import { ChatConfig } from "./config.js";
import * as htmlentities from 'html-entities';
export class MSAgentChatRoom { export class MSAgentChatRoom {
clients: Client[]; clients: Client[];
tts: TTSClient | null; tts: TTSClient | null;
msgId : number = 0; msgId : number = 0;
constructor(tts: TTSClient | null) { config: ChatConfig;
constructor(config: ChatConfig, tts: TTSClient | null) {
this.clients = []; this.clients = [];
this.config = config;
this.tts = tts; this.tts = tts;
} }
@ -32,6 +37,7 @@ export class MSAgentChatRoom {
data: { data: {
username: client.username!, username: client.username!,
agent: client.agent!, agent: client.agent!,
charlimit: this.config.charlimit,
users: this.clients.filter(c => c.username !== null).map(c => { users: this.clients.filter(c => c.username !== null).map(c => {
return { return {
username: c.username!, username: c.username!,
@ -57,7 +63,7 @@ export class MSAgentChatRoom {
op: MSAgentProtocolMessageType.Chat, op: MSAgentProtocolMessageType.Chat,
data: { data: {
username: client.username!, username: client.username!,
message message: htmlentities.encode(message)
} }
}; };
if (this.tts !== null) { if (this.tts !== null) {

View file

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

View file

@ -39,9 +39,10 @@ elements.chatSendBtn.addEventListener('click', () => {
}); });
async function connectToRoom() { 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.connect();
await Room.join(elements.logonUsername.value, "test"); await Room.join(elements.logonUsername.value, "test");
elements.chatInput.maxLength = Room.getCharlimit();
logonWindow.hide(); logonWindow.hide();
elements.logonView.style.display = "none"; elements.logonView.style.display = "none";
elements.chatView.style.display = "block"; elements.chatView.style.display = "block";