Skip to content

Commit 3bef9e1

Browse files
Redicrect back to setup page with success or failure message after operation
1 parent a291805 commit 3bef9e1

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

routers/setup.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from fastapi.templating import Jinja2Templates
88
from openai import AsyncOpenAI
99
from openai.types.beta import Assistant
10+
from urllib.parse import quote
1011

1112
from utils.create_assistant import create_or_update_assistant, ToolTypes
1213
from utils.create_assistant import update_env_file
@@ -49,16 +50,18 @@ async def set_openai_api_key(api_key: str = Form(...)) -> RedirectResponse:
4950
async def read_setup(
5051
request: Request,
5152
client: AsyncOpenAI = Depends(lambda: AsyncOpenAI()),
52-
message: Optional[str] = ""
53+
status: Optional[str] = None,
54+
message_text: Optional[str] = None
5355
) -> Response:
5456
# Check if assistant ID is missing
5557
current_tools = []
58+
setup_message = "" # Message specific to setup state (e.g., API key missing)
5659
load_dotenv(override=True)
5760
openai_api_key = os.getenv("OPENAI_API_KEY")
5861
assistant_id = os.getenv("ASSISTANT_ID")
5962

6063
if not openai_api_key:
61-
message = "OpenAI API key is missing."
64+
setup_message = "OpenAI API key is missing."
6265
else:
6366
if assistant_id:
6467
try:
@@ -68,13 +71,15 @@ async def read_setup(
6871
logger.error(f"Failed to retrieve assistant {assistant_id}: {e}")
6972
# If we can't retrieve the assistant, proceed as if it doesn't exist
7073
assistant_id = None
71-
message = "Error retrieving existing assistant. Please create a new one."
74+
setup_message = "Error retrieving existing assistant. Please create a new one."
7275

7376
return templates.TemplateResponse(
7477
"setup.html",
7578
{
7679
"request": request,
77-
"message": message,
80+
"setup_message": setup_message,
81+
"status": status, # Pass status from query params
82+
"status_message": message_text, # Pass message from query params
7883
"assistant_id": assistant_id,
7984
"current_tools": current_tools
8085
}
@@ -91,6 +96,7 @@ async def create_update_assistant(
9196
Returns the assistant ID and status of the operation.
9297
"""
9398
current_assistant_id = os.getenv("ASSISTANT_ID")
99+
action = "updated" if current_assistant_id else "created"
94100
new_assistant_id = await create_or_update_assistant(
95101
client=client,
96102
assistant_id=current_assistant_id,
@@ -99,6 +105,13 @@ async def create_update_assistant(
99105
)
100106

101107
if not new_assistant_id:
102-
raise HTTPException(status_code=400, detail="Failed to create or update assistant")
103-
104-
return RedirectResponse(url="/", status_code=303)
108+
status = "error"
109+
message_text = f"Failed to {action} assistant."
110+
else:
111+
status = "success"
112+
message_text = f"Assistant {action} successfully."
113+
114+
# URL encode the message text
115+
encoded_message = quote(message_text)
116+
redirect_url = f"/setup/?status={status}&message_text={encoded_message}"
117+
return RedirectResponse(url=redirect_url, status_code=303)

static/styles.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,3 +618,17 @@ pre {
618618
text-decoration: underline;
619619
}
620620
/* --- End Nav --- */
621+
622+
.status-message {
623+
margin-bottom: 1rem;
624+
font-weight: bold;
625+
text-align: center;
626+
}
627+
628+
.status-message.success {
629+
color: green;
630+
}
631+
632+
.status-message.error {
633+
color: red;
634+
}

templates/setup.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33

44
{% block content %}
55
<div class="setupContainer">
6+
{% if status and status_message %}
7+
<p class="status-message {{ status }}">{{ status_message }}</p>
8+
{% endif %}
9+
610
<h1 class="title">{% if assistant_id %}Update Assistant{% else %}Setup Required{% endif %}</h1>
7-
8-
{% if "API key" in message %}
11+
12+
{% if "API key" in setup_message %}
913
<div class="setupForm">
10-
<p class="setupMessage">Please enter your OpenAI API key to continue:</p>
14+
<p class="setupMessage">{{ setup_message }}</p>
1115
<form action="{{ url_for('set_openai_api_key') }}" method="POST">
1216
<div class="inputWrapper">
1317
<input

0 commit comments

Comments
 (0)