Skip to content
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

Implement auto-scroll to top of message for new chat messages #23

Open
wants to merge 1 commit 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
25 changes: 12 additions & 13 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ async def read_root(request: Request) -> HTMLResponse:
)


@app.post("/chat")
@app.post("/chat", response_class=HTMLResponse)
async def chat(request: Request, message: str = Form(...)) -> HTMLResponse:
user_message_id = str(uuid.uuid4())
bot_message_id = str(uuid.uuid4())

# Prepare messages with the correct order
prepared_messages, citations = await rag_service.prepare_messages_with_sources(
system_prompt=f"<system-prompt>{SYSTEM_PROMPT}</system-prompt>",
Expand All @@ -84,19 +87,15 @@ async def chat(request: Request, message: str = Form(...)) -> HTMLResponse:
chat_history.append(Message(role=MessageRole.user, content=message))
chat_history.append(Message(role=MessageRole.assistant, content=bot_response))

message_id = str(uuid.uuid4())

response_html = templates.TemplateResponse(
"bot_message.html",
{
"request": request,
"bot_response_html": bot_response_html,
"citations": citations,
"message_id": message_id,
},
)
response_html = f"""
<div id="user-message-{user_message_id}" class="user-message">{message}</div>
<div id="bot-message-{bot_message_id}" class="bot-message">{bot_response_html}</div>
<script>
scrollToMessage('bot-message-{bot_message_id}');
</script>
"""

return response_html
return HTMLResponse(content=response_html)


@app.get("/api/chat_history")
Expand Down
11 changes: 11 additions & 0 deletions app/static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ document.body.addEventListener('htmx:beforeRequest', function(event) {
// Append typing indicator
var typingIndicator = document.getElementById('typing-indicator').content.cloneNode(true);
chatContainer.appendChild(typingIndicator);

// Scroll to bottom after appending user message
scrollToBottom();
});

document.body.addEventListener('htmx:afterSwap', function(event) {
Expand All @@ -39,4 +42,12 @@ document.body.addEventListener('htmx:afterSwap', function(event) {

// Add 'show' class to trigger animation
newMessage.classList.add('show');

// Scroll to bottom after appending bot message
scrollToBottom();
});

function scrollToBottom() {
var chatContainer = document.getElementById('chat-container');
chatContainer.scrollTop = chatContainer.scrollHeight;
}
3 changes: 2 additions & 1 deletion app/static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ body {
flex-direction: column;
background-color: #242424;
border: 1px solid #3a3a3a;
scroll-behavior: smooth;
}

.message {
Expand Down Expand Up @@ -148,4 +149,4 @@ body {
.citation-content {
flex-grow: 1;
word-break: break-word;
}
}
14 changes: 12 additions & 2 deletions app/templates/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ <h1 class="mb-4">{{ chat_title }}</h1>
</div>
<!-- Chat messages will be inserted here -->
</div>
<form class="mt-3" hx-post="/chat" hx-target="#chat-container" hx-swap="beforeend" hx-indicator="#typing-indicator">
<form class="mt-3" hx-post="/chat" hx-target="#chat-container" hx-swap="beforeend" hx-indicator="#typing-indicator"
hx-on::after-request="scrollToMessage('user-message-{{ message_id }}')"
hx-on::after-settle="scrollToMessage('bot-message-{{ new_message_id }}')">
<div class="input-group">
<input type="text" name="message" id="message-input" class="form-control" placeholder="Type your message..." required>
<button class="btn btn-primary" type="submit">Send</button>
Expand All @@ -35,7 +37,15 @@ <h1 class="mb-4">{{ chat_title }}</h1>
</div>
</template>

<script>
function scrollToMessage(messageId) {
const element = document.getElementById(messageId);
if (element) {
element.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
}
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="/static/script.js"></script>
</body>
</html>
</html>
Loading