Skip to content

Commit

Permalink
Merge pull request #14 from artefactory-skaff/feature/sidebar-label
Browse files Browse the repository at this point in the history
Add label in sidebar
  • Loading branch information
AlexisVLRT authored May 16, 2024
2 parents c186880 + f863098 commit 6a11f13
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
37 changes: 29 additions & 8 deletions backend/api_plugins/sessions/sessions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from datetime import datetime
from pathlib import Path
from typing import List, Optional, Sequence
from typing import Optional, Sequence
from uuid import uuid4

from fastapi import APIRouter, Depends, FastAPI, Response
Expand Down Expand Up @@ -38,23 +38,44 @@ async def chat_new(
@app.get("/session/list")
async def chat_list(
current_user: User = authentication, dependencies=dependencies
) -> List[dict]:
) -> list[dict]:
user_email = current_user.email if current_user else "unauthenticated"
chats = []
with Database() as connection:
result = connection.execute(
"SELECT id, timestamp FROM session WHERE user_id = ? ORDER BY timestamp"
" DESC",
(user_email,),
# Check if message_history table exists (first time running the app will not
# have this table created yet)
message_history_exists = connection.fetchone(
"SELECT name FROM sqlite_master WHERE type='table' AND"
" name='message_history'"
)
chats = [{"id": row[0], "timestamp": row[1]} for row in result]
if message_history_exists:
# Join session with message_history and get the first message
result = connection.execute(
"SELECT s.id, s.timestamp, mh.message FROM session s LEFT JOIN"
" (SELECT *, ROW_NUMBER() OVER (PARTITION BY session_id ORDER BY"
" timestamp ASC) as rn FROM message_history) mh ON s.id ="
" mh.session_id AND mh.rn = 1 WHERE s.user_id = ? ORDER BY"
" s.timestamp DESC",
(user_email,),
)
for row in result:
# Extract the first message content if available
first_message_content = (
json.loads(row[2])["data"]["content"] if row[2] else ""
)
chat = {
"id": row[0],
"timestamp": row[1],
"first_message": first_message_content,
}
chats.append(chat)
return chats

@app.get("/session/{session_id}")
async def chat(
session_id: str, current_user: User = authentication, dependencies=dependencies
) -> dict:
messages: List[Message] = []
messages: list[Message] = []
with Database() as connection:
result = connection.execute(
"SELECT id, timestamp, session_id, message FROM message_history WHERE"
Expand Down
25 changes: 24 additions & 1 deletion frontend/lib/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ def sidebar():
st.sidebar.markdown(time_ago)
for chat in chats:
chat_id = chat["id"]
chat_first_message = chat["first_message"]
label = (
truncate_label(chat_first_message, 100)
if chat_first_message
else "*No content*"
)
if st.sidebar.button(
chat_id, key=chat_id, use_container_width=True
label=label,
key=chat_id,
use_container_width=True,
):
st.session_state["chat_id"] = chat_id
messages = [
Expand All @@ -54,3 +62,18 @@ def list_sessions():
def get_session(session_id: str):
session = query("get", f"/session/{session_id}").json()
return session


def truncate_label(string: str, max_len: int) -> str:
"""Truncate a string to a maximum length, appending ellipsis if necessary.
Args:
string (str): String to be truncated.
max_len (int): Maximum allowed length of the string after truncation.
Returns:
str: Truncated string.
"""
if string and len(string) > max_len:
string = string[: max_len - 3] + "..."
return string

0 comments on commit 6a11f13

Please sign in to comment.