Skip to content

Commit

Permalink
Merge pull request #125 from JoshuaC215/fedebotu-history-fix
Browse files Browse the repository at this point in the history
Add Chat Sharing Dialog & BugFix Empty History
  • Loading branch information
JoshuaC215 authored Dec 29, 2024
2 parents c5105a3 + 82202d2 commit ab6ee76
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
27 changes: 21 additions & 6 deletions src/streamlit_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import os
import urllib.parse
from collections.abc import AsyncGenerator

import streamlit as st
Expand Down Expand Up @@ -72,8 +73,11 @@ async def main() -> None:
thread_id = get_script_run_ctx().session_id
messages = []
else:
history: ChatHistory = agent_client.get_history(thread_id=thread_id)
messages = history.messages
try:
messages: ChatHistory = agent_client.get_history(thread_id=thread_id).messages
except AgentClientError:
st.error("No message history found for this Thread ID.")
messages = []
st.session_state.messages = messages
st.session_state.thread_id = thread_id

Expand Down Expand Up @@ -112,10 +116,21 @@ def architecture_dialog() -> None:
"Prompts, responses and feedback in this app are anonymously recorded and saved to LangSmith for product evaluation and improvement purposes only."
)

st.markdown(
f"Thread ID: **{st.session_state.thread_id}**",
help=f"Set URL query parameter ?thread_id={st.session_state.thread_id} to continue this conversation",
)
@st.dialog("Share/resume chat")
def share_chat_dialog() -> None:
session = st.runtime.get_instance()._session_mgr.list_active_sessions()[0]
st_base_url = urllib.parse.urlunparse(
[session.client.request.protocol, session.client.request.host, "", "", "", ""]
)
# if it's not localhost, switch to https by default
if not st_base_url.startswith("https") and "localhost" not in st_base_url:
st_base_url = st_base_url.replace("http", "https")
chat_url = f"{st_base_url}?thread_id={st.session_state.thread_id}"
st.markdown(f"**Chat URL:**\n```text\n{chat_url}\n```")
st.info("Copy the above URL to share or revisit this chat")

if st.button(":material/upload: Share/resume chat", use_container_width=True):
share_chat_dialog()

"[View the source code](https://github.com/JoshuaC215/agent-service-toolkit)"
st.caption(
Expand Down
4 changes: 2 additions & 2 deletions tests/app/test_streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_app_thread_id_history(mock_agent_client):
"""Test the thread_id is generated"""

at = AppTest.from_file("../../src/streamlit_app.py").run()
assert at.sidebar.markdown[-2].value == "Thread ID: **test session id**"
assert at.session_state.thread_id == "test session id"

# Reset and set thread_id
at = AppTest.from_file("../../src/streamlit_app.py")
Expand All @@ -85,7 +85,7 @@ def test_app_thread_id_history(mock_agent_client):
mock_agent_client.get_history.return_value = ChatHistory(messages=HISTORY)
at.run()
print(at)
assert at.sidebar.markdown[-2].value == "Thread ID: **1234**"
assert at.session_state.thread_id == "1234"
mock_agent_client.get_history.assert_called_with(thread_id="1234")
assert at.chat_message[0].avatar == "user"
assert at.chat_message[0].markdown[0].value == "What is the weather?"
Expand Down

0 comments on commit ab6ee76

Please sign in to comment.