|
| 1 | +"""Main Streamlit application.""" |
| 2 | + |
| 3 | +import uuid |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +import streamlit as st |
| 7 | + |
| 8 | +from components.chat import render_message, render_sidebar |
| 9 | +from components.config import render_agentcore_config |
| 10 | +from models.message import Message |
| 11 | +from services.conversation_client import ConversationClient |
| 12 | +from services.agentcore_client import AgentCoreClient |
| 13 | + |
| 14 | + |
| 15 | +def init_session_state(): |
| 16 | + """Initialize session state variables.""" |
| 17 | + if "conversation_id" not in st.session_state: |
| 18 | + st.session_state.conversation_id = None |
| 19 | + if "messages" not in st.session_state: |
| 20 | + st.session_state.messages = [] |
| 21 | + if "user_id" not in st.session_state: |
| 22 | + st.session_state.user_id = f"user_{str(uuid.uuid4())[:8]}" |
| 23 | + if "agent_runtime_arn" not in st.session_state: |
| 24 | + st.session_state.agent_runtime_arn = "" |
| 25 | + if "region" not in st.session_state: |
| 26 | + st.session_state.region = "us-east-1" |
| 27 | + if "use_agentcore" not in st.session_state: |
| 28 | + st.session_state.use_agentcore = False |
| 29 | + if "auth_token" not in st.session_state: |
| 30 | + st.session_state.auth_token = "" |
| 31 | + |
| 32 | + |
| 33 | +def main(): |
| 34 | + """Main application.""" |
| 35 | + st.set_page_config( |
| 36 | + page_title="CX Agent Chat", |
| 37 | + page_icon="🤖", |
| 38 | + layout="wide", |
| 39 | + initial_sidebar_state="expanded", |
| 40 | + ) |
| 41 | + |
| 42 | + # Custom CSS for better design |
| 43 | + st.markdown(""" |
| 44 | + <style> |
| 45 | + .stButton > button { |
| 46 | + border-radius: 20px; |
| 47 | + border: 1px solid #e0e0e0; |
| 48 | + transition: all 0.3s ease; |
| 49 | + } |
| 50 | + .stButton > button:hover { |
| 51 | + transform: translateY(-2px); |
| 52 | + box-shadow: 0 4px 8px rgba(0,0,0,0.1); |
| 53 | + } |
| 54 | + .feedback-section { |
| 55 | + background-color: #f8f9fa; |
| 56 | + padding: 10px; |
| 57 | + border-radius: 10px; |
| 58 | + margin: 10px 0; |
| 59 | + } |
| 60 | + </style> |
| 61 | + """, unsafe_allow_html=True) |
| 62 | + |
| 63 | + st.title("🤖 CX Agent Chat") |
| 64 | + if st.session_state.get('use_agentcore', False): |
| 65 | + st.caption("Powered by AWS Bedrock AgentCore Runtime") |
| 66 | + else: |
| 67 | + st.caption("Powered by LiteLLM Gateway hosted on AWS") |
| 68 | + |
| 69 | + init_session_state() |
| 70 | + |
| 71 | + # Render configuration |
| 72 | + config_valid = render_agentcore_config() |
| 73 | + model = render_sidebar() |
| 74 | + |
| 75 | + # Initialize client based on configuration |
| 76 | + if st.session_state.use_agentcore and config_valid: |
| 77 | + auth_token = st.session_state.get('auth_token', '') |
| 78 | + client = AgentCoreClient( |
| 79 | + agent_runtime_arn=st.session_state.agent_runtime_arn, |
| 80 | + region=st.session_state.region, |
| 81 | + auth_token=auth_token |
| 82 | + ) |
| 83 | + if auth_token: |
| 84 | + st.info("🚀 Connected to AgentCore Runtime") |
| 85 | + else: |
| 86 | + st.warning("⚠️ AgentCore configured but no auth token provided") |
| 87 | + else: |
| 88 | + client = ConversationClient() |
| 89 | + if st.session_state.use_agentcore: |
| 90 | + st.warning("⚠️ AgentCore configuration invalid, using local backend") |
| 91 | + else: |
| 92 | + st.info("🔧 Using local backend") |
| 93 | + |
| 94 | + # Display chat messages |
| 95 | + for message in st.session_state.messages: |
| 96 | + render_message(message, client) |
| 97 | + |
| 98 | + # Chat input |
| 99 | + if prompt := st.chat_input("Type your message..."): |
| 100 | + # Create conversation if needed |
| 101 | + if not st.session_state.conversation_id: |
| 102 | + with st.spinner("Creating conversation..."): |
| 103 | + conversation_id = client.create_conversation(st.session_state.user_id) |
| 104 | + if not conversation_id: |
| 105 | + st.stop() |
| 106 | + st.session_state.conversation_id = conversation_id |
| 107 | + |
| 108 | + # Add user message |
| 109 | + user_message = Message(role="user", content=prompt, timestamp=datetime.now()) |
| 110 | + st.session_state.messages.append(user_message) |
| 111 | + render_message(user_message) |
| 112 | + |
| 113 | + # Send message and get response |
| 114 | + with st.spinner("Thinking..."): |
| 115 | + response = client.send_message( |
| 116 | + st.session_state.conversation_id, prompt, model |
| 117 | + ) |
| 118 | + |
| 119 | + if response: |
| 120 | + # Debug: Show what we received |
| 121 | + st.write("Debug - API Response:", response) |
| 122 | + |
| 123 | + # Add assistant message |
| 124 | + metadata = { |
| 125 | + "model": model, |
| 126 | + "status": response.get("status", "success"), |
| 127 | + } |
| 128 | + |
| 129 | + # Add tools_used to metadata if available |
| 130 | + if "tools_used" in response and response["tools_used"]: |
| 131 | + metadata["tools_used"] = ",".join(response["tools_used"]) |
| 132 | + |
| 133 | + assistant_message = Message( |
| 134 | + role="assistant", |
| 135 | + content=response["response"], |
| 136 | + timestamp=datetime.now(), |
| 137 | + metadata=metadata, |
| 138 | + ) |
| 139 | + st.session_state.messages.append(assistant_message) |
| 140 | + render_message(assistant_message, client) |
| 141 | + |
| 142 | + st.rerun() |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + main() |
0 commit comments