|
| 1 | +from os import getenv |
| 2 | + |
| 3 | +import nest_asyncio |
| 4 | +import streamlit as st |
| 5 | +from agents import get_github_agent |
| 6 | +from agno.agent import Agent |
| 7 | +from agno.utils.log import logger |
| 8 | +from utils import ( |
| 9 | + CUSTOM_CSS, |
| 10 | + about_widget, |
| 11 | + add_message, |
| 12 | + display_tool_calls, |
| 13 | + sidebar_widget, |
| 14 | +) |
| 15 | + |
| 16 | +nest_asyncio.apply() |
| 17 | +st.set_page_config( |
| 18 | + page_title="GitHub Repo Analyzer", |
| 19 | + page_icon="👨💻", |
| 20 | + layout="wide", |
| 21 | +) |
| 22 | + |
| 23 | +# Load custom CSS with dark mode support |
| 24 | +st.markdown(CUSTOM_CSS, unsafe_allow_html=True) |
| 25 | + |
| 26 | + |
| 27 | +def main() -> None: |
| 28 | + ##################################################################### |
| 29 | + # App header |
| 30 | + #################################################################### |
| 31 | + st.markdown( |
| 32 | + "<h1 class='main-header'>👨💻 GitHub Repo Analyzer</h1>", unsafe_allow_html=True |
| 33 | + ) |
| 34 | + st.markdown("Analyze GitHub repositories") |
| 35 | + |
| 36 | + #################################################################### |
| 37 | + # Initialize Agent |
| 38 | + #################################################################### |
| 39 | + github_agent: Agent |
| 40 | + if ( |
| 41 | + "github_agent" not in st.session_state |
| 42 | + or st.session_state["github_agent"] is None |
| 43 | + ): |
| 44 | + logger.info("---*--- Creating new Github agent ---*---") |
| 45 | + github_agent = get_github_agent() |
| 46 | + st.session_state["github_agent"] = github_agent |
| 47 | + st.session_state["messages"] = [] |
| 48 | + st.session_state["github_token"] = getenv("GITHUB_ACCESS_TOKEN") |
| 49 | + else: |
| 50 | + github_agent = st.session_state["github_agent"] |
| 51 | + |
| 52 | + #################################################################### |
| 53 | + # Load Agent Session from the database |
| 54 | + #################################################################### |
| 55 | + try: |
| 56 | + st.session_state["github_agent_session_id"] = github_agent.load_session() |
| 57 | + except Exception: |
| 58 | + st.warning("Could not create Agent session, is the database running?") |
| 59 | + return |
| 60 | + |
| 61 | + #################################################################### |
| 62 | + # Load runs from memory (v2 Memory) only on initial load |
| 63 | + #################################################################### |
| 64 | + if github_agent.memory is not None and not st.session_state.get("messages"): |
| 65 | + session_id = st.session_state.get("github_agent_session_id") |
| 66 | + # Fetch stored runs for this session |
| 67 | + agent_runs = github_agent.memory.get_runs(session_id) |
| 68 | + if agent_runs: |
| 69 | + logger.debug("Loading run history") |
| 70 | + st.session_state["messages"] = [] |
| 71 | + for run_response in agent_runs: |
| 72 | + # Iterate through stored messages in the run |
| 73 | + for msg in run_response.messages or []: |
| 74 | + if msg.role in ["user", "assistant"] and msg.content is not None: |
| 75 | + # Include any tool calls attached to this message |
| 76 | + add_message( |
| 77 | + msg.role, msg.content, getattr(msg, "tool_calls", None) |
| 78 | + ) |
| 79 | + else: |
| 80 | + logger.debug("No run history found") |
| 81 | + st.session_state["messages"] = [] |
| 82 | + |
| 83 | + #################################################################### |
| 84 | + # Sidebar |
| 85 | + #################################################################### |
| 86 | + sidebar_widget() |
| 87 | + |
| 88 | + #################################################################### |
| 89 | + # Get user input |
| 90 | + #################################################################### |
| 91 | + if prompt := st.chat_input("👋 Ask me about GitHub repositories!"): |
| 92 | + add_message("user", prompt) |
| 93 | + |
| 94 | + #################################################################### |
| 95 | + # Display chat history |
| 96 | + #################################################################### |
| 97 | + for message in st.session_state["messages"]: |
| 98 | + if message["role"] in ["user", "assistant"]: |
| 99 | + _content = message["content"] |
| 100 | + if _content is not None: |
| 101 | + with st.chat_message(message["role"]): |
| 102 | + # Display tool calls if they exist in the message |
| 103 | + if "tool_calls" in message and message["tool_calls"]: |
| 104 | + display_tool_calls(st.empty(), message["tool_calls"]) |
| 105 | + st.markdown(_content) |
| 106 | + |
| 107 | + #################################################################### |
| 108 | + # Generate response for user message |
| 109 | + #################################################################### |
| 110 | + last_message = ( |
| 111 | + st.session_state["messages"][-1] if st.session_state["messages"] else None |
| 112 | + ) |
| 113 | + if last_message and last_message.get("role") == "user": |
| 114 | + question = last_message["content"] |
| 115 | + with st.chat_message("assistant"): |
| 116 | + # Create container for tool calls |
| 117 | + tool_calls_container = st.empty() |
| 118 | + resp_container = st.empty() |
| 119 | + with st.spinner("🤔 Thinking..."): |
| 120 | + response = "" |
| 121 | + try: |
| 122 | + # Run the agent and stream the response |
| 123 | + run_response = github_agent.run( |
| 124 | + question, stream=True, stream_intermediate_steps=True |
| 125 | + ) |
| 126 | + for _resp_chunk in run_response: |
| 127 | + # Display tool calls if available |
| 128 | + if _resp_chunk.tools and len(_resp_chunk.tools) > 0: |
| 129 | + display_tool_calls(tool_calls_container, _resp_chunk.tools) |
| 130 | + |
| 131 | + # Display response if available and event is RunResponse |
| 132 | + if ( |
| 133 | + _resp_chunk.event == "RunResponse" |
| 134 | + and _resp_chunk.content is not None |
| 135 | + ): |
| 136 | + response += _resp_chunk.content |
| 137 | + resp_container.markdown(response) |
| 138 | + |
| 139 | + add_message("assistant", response, github_agent.run_response.tools) |
| 140 | + except Exception as e: |
| 141 | + logger.exception(e) |
| 142 | + error_message = f"Sorry, I encountered an error: {str(e)}" |
| 143 | + add_message("assistant", error_message) |
| 144 | + st.error(error_message) |
| 145 | + |
| 146 | + #################################################################### |
| 147 | + # About section |
| 148 | + #################################################################### |
| 149 | + about_widget() |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + main() |
0 commit comments