add rudimentary multiroom support

This commit is contained in:
Elijah R 2024-11-25 19:24:16 -05:00
parent 2b83db8d58
commit 1385c555f4
4 changed files with 34 additions and 2 deletions

View file

@ -108,7 +108,9 @@ if (config.discord.enabled) {
// Image upload
let img = new ImageUploader(app, config.images);
let room = new MSAgentChatRoom(config.chat, rootLogger.child({module: "Room#Default"}), config.agents, db, img, tts, discord);
let primaryRoom = new MSAgentChatRoom(config.chat, rootLogger.child({module: "Room#Default"}), config.agents, db, img, tts, discord);
let rooms = new Map<string, MSAgentChatRoom>();
app.register(async (app) => {
app.get('/api/socket', { websocket: true }, async (socket, req) => {
@ -143,6 +145,21 @@ app.register(async (app) => {
});
return;
}
let room : MSAgentChatRoom;
if ((req.query as any).room !== undefined) {
let requestedRoom = (req.query as any).room;
if (rooms.has(requestedRoom)) {
room = rooms.get(requestedRoom)!;
} else {
room = new MSAgentChatRoom(config.chat, rootLogger.child({module: `Room#${requestedRoom}`}), config.agents, db, img, tts, null);
rooms.set(requestedRoom, room);
}
} else {
room = primaryRoom;
}
let o = room.clients.filter((c) => c.ip === ip);
if (o.length >= config.chat.maxConnectionsPerIP) {
o[0].socket.close();

View file

@ -28,7 +28,7 @@
</div>
<div id="logonRoomContainer">
<label for="logonRoom">Room name:</label>
<input type="text" id="logonRoom" placeholder="Coming Soon" disabled />
<input type="text" id="logonRoom" placeholder="Default" />
</div>
<div id="logonButtonsContainer">
<select id="agentSelect">

View file

@ -56,6 +56,7 @@ export class MSAgentClient {
private loginCb: (e: KeyboardEvent) => void;
private username: string | null = null;
private room: string | null = null;
private agentContainer: HTMLElement;
private agent: string | null = null;
@ -84,6 +85,13 @@ export class MSAgentClient {
return this.events.on(event, callback);
}
setRoom(room: string) {
if (this.socket !== null) {
throw new Error('Cannot set room while connected');
}
this.room = room;
}
async getAgents() {
let res = await fetch(this.url + '/api/agents');
return (await res.json()) as APIAgentInfo[];
@ -122,6 +130,9 @@ export class MSAgentClient {
throw new Error(`Unknown protocol ${url.protocol}`);
}
url.pathname = '/api/socket';
if (this.room !== null) {
url.searchParams.set('room', this.room);
}
this.socket = new WebSocket(url);
this.socket.addEventListener('open', () => res());
this.socket.addEventListener('message', (e) => {

View file

@ -13,6 +13,7 @@ const elements = {
logonWindow: document.getElementById('logonWindow') as HTMLDivElement,
logonForm: document.getElementById('logonForm') as HTMLFormElement,
logonUsername: document.getElementById('logonUsername') as HTMLInputElement,
logonRoom: document.getElementById('logonRoom') as HTMLInputElement,
logonButton: document.getElementById('logonButton') as HTMLButtonElement,
agentSelect: document.getElementById('agentSelect') as HTMLSelectElement,
@ -108,6 +109,9 @@ async function connectToRoom() {
if (loggingIn) return;
loggingIn = true;
elements.logonButton.disabled = true;
if (elements.logonRoom.value) {
Room.setRoom(elements.logonRoom.value);
}
await Room.connect();
await Room.join(elements.logonUsername.value, elements.agentSelect.value);
elements.chatInput.maxLength = Room.getCharlimit();