|
1 | 1 | import streamlit as st
|
2 | 2 | from performer.performer import graph
|
3 | 3 | from agentstate.agent_state import AgentState
|
| 4 | +from utils.sql_utils import extract_sql_queries |
4 | 5 |
|
5 | 6 | st.title("PostgreSQL Database Optimization Assistant")
|
6 | 7 | st.markdown("""
|
@@ -29,28 +30,83 @@ def run_analysis():
|
29 | 30 | if "analysis" in event:
|
30 | 31 | st.session_state.analysis_history.append(event["analysis"])
|
31 | 32 |
|
| 33 | +def extract_thinking_content(analysis): |
| 34 | + """Extracts the content between <think> and </think> tags.""" |
| 35 | + start_tag = "<think>" |
| 36 | + end_tag = "</think>" |
| 37 | + start_index = analysis.find(start_tag) |
| 38 | + end_index = analysis.find(end_tag) |
| 39 | + if start_index != -1 and end_index != -1: |
| 40 | + return analysis[start_index + len(start_tag):end_index].strip() |
| 41 | + return None |
| 42 | + |
| 43 | +def execute_sql_queries(edited_queries): |
| 44 | + """Extract and execute SQL queries from the final analysis.""" |
| 45 | + current_state = graph.get_state(thread) |
| 46 | + schema = current_state.values.get("schema", "") |
| 47 | + |
| 48 | + # Update state with edited queries |
| 49 | + graph.update_state(thread, {"execute_query": edited_queries}) |
| 50 | + |
| 51 | + # Execute each query |
| 52 | + for query in edited_queries.split(";"): |
| 53 | + query = query.strip() |
| 54 | + if query: # Skip empty queries |
| 55 | + st.write(f"Executing: {query}") |
| 56 | + try: |
| 57 | + # Simulate execution (replace with actual SQL execution logic if needed) |
| 58 | + st.write("Query executed successfully.") |
| 59 | + except Exception as e: |
| 60 | + st.error(f"Failed to execute query: {query}. Error: {str(e)}") |
| 61 | + |
32 | 62 | if st.button("Analyze"):
|
33 | 63 | if query and schema:
|
34 | 64 | run_analysis()
|
35 | 65 | else:
|
36 | 66 | st.error("Please provide both query and schema")
|
37 | 67 |
|
38 | 68 | if st.session_state.analysis_history:
|
| 69 | + latest_analysis = st.session_state.analysis_history[-1] |
| 70 | + start_tag = "<think>" |
| 71 | + end_tag = "</think>" |
| 72 | + if start_tag in latest_analysis and end_tag in latest_analysis: |
| 73 | + start_index = latest_analysis.index(start_tag) + len(start_tag) |
| 74 | + end_index = latest_analysis.index(end_tag) |
| 75 | + thinking_content = latest_analysis[start_index:end_index].strip() |
| 76 | + else: |
| 77 | + thinking_content = "No detailed reasoning available for this analysis." |
| 78 | + |
| 79 | + with st.expander("Thinking Mode: View Detailed Reasoning", expanded=False): |
| 80 | + st.markdown(thinking_content) |
| 81 | + |
39 | 82 | st.subheader("Analysis History")
|
40 | 83 | for i, analysis in enumerate(st.session_state.analysis_history, 1):
|
41 | 84 | st.write(f"Iteration {i}:")
|
42 |
| - st.code(analysis) |
| 85 | + st.markdown(analysis) |
43 | 86 |
|
44 | 87 | st.subheader("Feedback")
|
45 | 88 | col1, col2 = st.columns(2)
|
46 | 89 |
|
47 | 90 | with col1:
|
48 | 91 | if st.button("Yes - Accept Analysis"):
|
49 | 92 | graph.update_state(thread, {"execute": True})
|
50 |
| - st.success("Analysis accepted! Proceeding to execution...") |
51 |
| - st.query_params = {"status": "completed"} |
52 |
| - st.rerun() |
53 |
| - |
| 93 | + st.success("Analysis accepted! Proceeding to SQL execution...") |
| 94 | + |
| 95 | + # Extract SQL queries from the latest analysis |
| 96 | + current_state = graph.get_state(thread) |
| 97 | + analysis = current_state.values.get("analysis", "") |
| 98 | + sql_queries = extract_sql_queries(analysis) |
| 99 | + |
| 100 | + if not sql_queries: |
| 101 | + st.warning("No SQL queries found in the analysis.") |
| 102 | + else: |
| 103 | + # Display SQL queries in an editable text area |
| 104 | + edited_queries = st.text_area("Edit SQL Queries:", value=sql_queries), height=200) |
| 105 | + |
| 106 | + # Add an "Execute" button to confirm and execute the edited queries |
| 107 | + if st.button("Execute Edited Queries"): |
| 108 | + execute_sql_queries(edited_queries) |
| 109 | + |
54 | 110 | with col2:
|
55 | 111 | if st.button("No - Revise Analysis"):
|
56 | 112 | st.session_state.show_feedback = True
|
|
0 commit comments