Livio Meuli 6f9be8a6c4 Implement ChatManager with API integration, SystemPrompter, and comprehensive test suite
- Add ChatManager class for OpenAI-compatible API communication (silicon.fhgr.ch:7080)
- Add SystemPrompter for intelligent prompt generation with file context
- Integrate ChatManager into frontend chat component
- Add comprehensive pytest tests (40+ tests covering unit and integration scenarios)
- Implement error handling for API failures, timeouts, and connection issues
- Add environment variable-based configuration for API credentials
- Update frontend state initialization to include ChatManager
- All tests passing with mock/patch isolation for API calls
2026-04-09 14:19:57 +02:00

50 lines
2.0 KiB
Python

import streamlit as st
from backend.managers.chat_manager import ChatManager
from backend.managers.system_prompter import SystemPrompter
def render_chat():
st.subheader("Chat with AI Assistant")
chat_section = st.container()
setup_section = st.container()
with chat_section:
if st.session_state.chat_history:
for message in st.session_state.chat_history:
st.markdown(f"**{message['role'].capitalize()}:** {message['content']}")
# Clear the input field before the widget is rendered (Streamlit requirement)
if st.session_state.get("_clear_chat_input"):
st.session_state.chat_input = ""
st.session_state._clear_chat_input = False
user_input = st.text_input("Type your message here:", key="chat_input")
if st.button("Send", key="send_button") and user_input:
chat_manager = st.session_state.chat_manager
# Inject system prompt on the first message
if not chat_manager.get_history():
system_prompt = SystemPrompter.generate_prompt()
chat_manager.add_message("system", system_prompt)
st.session_state.chat_history.append({"role": "user", "content": user_input})
try:
ai_response = chat_manager.send_message(user_input)
except Exception as e:
ai_response = f"Error: {e}"
st.session_state.chat_history.append({"role": "assistant", "content": ai_response})
st.session_state._clear_chat_input = True # Clear input on next rerun
st.rerun()
with setup_section:
st.info("This is where you can set up your AI assistant. For now, this section is just a placeholder.")
st.toggle("Use debug system prompt", key="use_system_prompt", value=True)
# Here you could add options to configure the AI assistant, such as selecting a model, setting parameters, etc.
if __name__ == "__main__":
render_chat()