Skip to content

Add text chat UI #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@
</div>
<audio id="bot-audio" autoplay></audio>
</div>
<div id="bot-text-container" style="display: none;">
<div id="text-chat-log"></div>
</div>
<form id="input-area" style="display: none;">
<input type="text" id="message-input" placeholder="Type your message to the bot here..." required>
<button type="submit" id="send-btn">Send</button>
</form>
<div class="debug-panel">
<div id="debug-log"></div>
</div>
Expand Down
123 changes: 121 additions & 2 deletions client/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class WebRTCApp {
private selfViewVideo!: HTMLVideoElement;
private videoContainer!: HTMLElement;
private botName!: HTMLElement;
private msgInput!: HTMLInputElement;
private textContainer!: HTMLElement;
private textChatLog!: HTMLElement;
private botContainer!: HTMLElement;
private inputArea!: HTMLElement;

// State
private connected: boolean = false;
Expand Down Expand Up @@ -133,6 +138,7 @@ class WebRTCApp {
},
onBotTranscript: (transcript) => {
this.log(`Bot transcript: ${transcript.text}`);
this.chatLog(transcript.text.trim(), "Bot");
},

// Media tracks
Expand All @@ -154,7 +160,20 @@ class WebRTCApp {

// Other events
onServerMessage: (msg) => {
this.log(`Server message: ${msg}`);
this.log(`Server message: ${JSON.stringify(msg)}`);

// if server message is about UI; hide UI containers
if ('show_text_container' in msg && msg.show_text_container) {
this.textContainer.style.display = "flex";
this.inputArea.style.display = "flex";
}
if ('show_video_container' in msg && !msg.show_video_container) {
this.videoContainer.style.display = "none";
this.botContainer.style.display = "none";
}
if ('show_debug_container' in msg && !msg.show_debug_container) {
this.debugLog.style.display = "none";
}
},
},
};
Expand Down Expand Up @@ -195,7 +214,10 @@ class WebRTCApp {
this.audioElement = document.getElementById(
"bot-audio"
) as HTMLAudioElement;
this.debugLog = document.getElementById("debug-log") as HTMLElement;
this.debugLog = document.getElementsByClassName("debug-panel")[0] as HTMLElement;
this.textChatLog = document.getElementById(
"text-chat-log"
) as HTMLElement;
this.micToggleBtn = document.getElementById(
"mic-toggle"
) as HTMLButtonElement;
Expand Down Expand Up @@ -228,6 +250,18 @@ class WebRTCApp {
"bot-video-container"
) as HTMLElement;
this.botName = document.getElementById("bot-name") as HTMLElement;
this.msgInput = document.getElementById(
"message-input"
) as HTMLInputElement;
this.textContainer = document.getElementById(
"bot-text-container"
) as HTMLInputElement;
this.botContainer = document.getElementsByClassName(
"bot-container"
)[0] as HTMLElement;
this.inputArea = document.getElementById(
"input-area"
) as HTMLInputElement;
}

private setupDOMEventListeners(): void {
Expand Down Expand Up @@ -306,8 +340,47 @@ class WebRTCApp {
this.cameraChevronBtn.classList.remove("active");
}
});

// Text chat handlers
this.inputArea.addEventListener("submit", (ev) => {
ev.preventDefault();
this.handleTextSubmit();
});
}

async handleTextSubmit() {
const text = this.msgInput.value;
const message = text.trim();

if (message.length === 0) {
return; // Ignore empty messages
}

try {
await this.rtviClient?.action({
service: "llm",
action: "append_to_messages",
arguments: [
{
name: "messages",
value: [
{
role: "user",
content: message,
},
],
},
],
});
this.chatLog(message.trim(), "User");
} catch (e) {
console.error(e);
} finally {
this.msgInput.value = ""
}
};


private togglePopover(popover: HTMLElement, chevronBtn: HTMLElement): void {
popover.classList.toggle("show");
chevronBtn.classList.toggle("active");
Expand Down Expand Up @@ -400,6 +473,49 @@ class WebRTCApp {
}
}

private chatLog(message: string, type: string = "normal"): void {
if (!this.textChatLog) return;

const now = new Date();
const timeString = now.toISOString().replace("T", " ").substring(0, 19);

const entry = document.createElement("div");
entry.classList.add("message")
// Create elements for time, author, and message
const timeElement = document.createElement("span");
timeElement.className = "message-time";
timeElement.textContent = timeString;

const authorElement = document.createElement("span");
authorElement.className = "message-author";
authorElement.textContent = type;

const messageElement = document.createElement("span");
messageElement.className = "message-content";
messageElement.textContent = message.trim();

// Append them to the entry
entry.appendChild(timeElement);
entry.appendChild(authorElement);
entry.appendChild(messageElement);

// Apply styling based on message type
switch (type.toLowerCase()) {
case "user":
entry.classList.add("user-message");
break;
case "bot":
entry.classList.add("bot-message");
break;
case "error":
entry.classList.add("error-message");
break;
}

this.textChatLog.appendChild(entry);
this.textChatLog.scrollTop = this.textChatLog.scrollHeight;
}

private log(message: string, type: string = "normal"): void {
if (!this.debugLog) return;

Expand Down Expand Up @@ -429,6 +545,9 @@ class WebRTCApp {
this.debugLog.innerHTML = "";
this.log("Log cleared", "status");
}
if (this.textChatLog) {
this.textChatLog.innerHTML = "";
}
}

private onConnectedHandler(): void {
Expand Down
119 changes: 115 additions & 4 deletions client/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@
* SPDX-License-Identifier: BSD 2-Clause License
*/

*,
*::before,
*::after {
box-sizing: border-box;
}

html {
margin: 0;
padding: 0;
height: 100%;
}

body {
margin: 0;
padding: 10px 0 0 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
Arial, sans-serif;
background-color: #f4f4f4;
Expand All @@ -19,6 +31,7 @@ body {
.container {
margin: 0 auto;
max-width: 1500px;
height: 100dvh;
width: 90%;
padding: 0;
display: flex;
Expand Down Expand Up @@ -239,11 +252,12 @@ body {

/* Main content layout */
.main-content {
display: flex;
display: grid;
grid-template-columns: 1fr 1fr;
background-color: #fff;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
width: 100%;
}

/* Make both containers equal width */
Expand All @@ -261,6 +275,7 @@ body {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
}

#bot-video-container {
Expand All @@ -275,12 +290,32 @@ body {
object-fit: contain;
}

#bot-text-container {
aspect-ratio: 32 / 9;
grid-column: span 2;
min-height: 300px;
overflow: hidden;
width: 100%;
}

.debug-panel {
border-left: 1px solid #ddd;
display: flex;
flex-direction: column;
/* Make this panel exactly the same dimensions as the video container */
aspect-ratio: 16 / 9;
width: 100%;
}

#text-chat-log {
background-color: #f8f8f8;
padding: 16px;
font-size: 13px;
line-height: 1.5;
color: #222;
min-height: 100%;
overflow-y: auto;
width: 100%;
}

#debug-log {
Expand Down Expand Up @@ -316,7 +351,45 @@ body {
font-weight: 500;
}

#text-chat-log .message {
padding: 8px;
margin-bottom: 6px;
border-radius: 4px;
background-color: #efefef;
max-width: 90%;
}

#text-chat-log .message-time {
display: inline-block;
font-size: 10px;
color: #aaa;
margin-right: 8px;
}
#text-chat-log .message-author {
display: inline-block;
font-size: 10px;
}
#text-chat-log .message-content {
display: block;
margin-top: 4px;
}

#text-chat-log .user-message {
background: #ddeeff;
color: #1e40af;
font-weight: 500;
margin-left: auto;
}

#text-chat-log .bot-message {
color: #047857;
font-weight: 500;
margin-right: auto;
}


#debug-log .error-message {
background: #ffeeee;
color: #dc2626;
font-weight: 500;
}
Expand Down Expand Up @@ -381,6 +454,34 @@ audio {
display: none; /* Hide audio but keep it functional */
}

#input-area {
display: flex;
grid-column: span 2;
width: 100%;
}

#message-input {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
margin-right: 10px;
width: 100%;
}

#send-btn {
padding: 10px 16px;
border: none;
border-radius: 4px;
font-weight: 600;
font-size: 14px;
cursor: pointer;
background-color: #4caf50;
color: white;
display: flex;
align-items: center;
justify-content: center;
}

/* Media queries */
@media (max-width: 768px) {
.container {
Expand All @@ -406,7 +507,7 @@ audio {
}

.main-content {
flex-direction: column;
grid-template-columns: 1fr;
}

.bot-container,
Expand All @@ -415,6 +516,16 @@ audio {
flex: 0 0 auto;
}

#bot-text-container {
aspect-ratio: 1 / 1;
grid-column: span 1;
max-height: 50dvh;
}

#input-area {
grid-column: span 1;
}

.debug-panel {
border-left: none;
border-top: 1px solid #ddd;
Expand Down