Skip to content

Commit e154fbb

Browse files
Refactor component templates
1 parent 6ca9c8a commit e154fbb

File tree

10 files changed

+39
-37
lines changed

10 files changed

+39
-37
lines changed

routers/messages.py

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,14 @@
2020
tags=["assistants_messages"]
2121
)
2222

23-
# Load Jinja2 templates
23+
# Jinja2 templates
2424
templates = Jinja2Templates(directory="templates")
2525

26+
# Utility function for submitting tool outputs to the assistant
2627
class ToolCallOutputs(BaseModel):
2728
tool_outputs: Any
2829
runId: str
2930

30-
user_message_html = (
31-
"<div class=\"chat-turn\" hx-swap=\"beforeend\" "
32-
"sse-connect=\"/assistants/{assistant_id}/messages/{thread_id}/receive\" "
33-
"sse-swap=\"messageCreated\" "
34-
"sse-close=\"endStream\">"
35-
"<div class=\"userMessage\">"
36-
"{user_input}"
37-
"</div>"
38-
"</div>"
39-
)
40-
41-
assistant_step_html = (
42-
"<div class=\"{event_type}\" " # assistantMessage or toolCall
43-
"sse-swap=\"{event_name}\" " #event_type plus a counter
44-
"hx-swap=\"beforeend\">"
45-
"</div>"
46-
)
47-
4831
async def post_tool_outputs(client: AsyncOpenAI, data: dict, thread_id: str):
4932

5033
try:
@@ -64,7 +47,11 @@ async def post_tool_outputs(client: AsyncOpenAI, data: dict, thread_id: str):
6447
logger.error(f"Error submitting tool outputs: {e}")
6548
raise HTTPException(status_code=500, detail=str(e))
6649

50+
# TODO: Handle message created event by rendering assistant-step.html with the
51+
# event type ("assistantMessage" or "toolCall", in this case "assistantMessage")
52+
# and name ("assistantMessage" plus a counter)
6753

54+
# Custom event handler for the assistant run stream
6855
class CustomEventHandler(AssistantEventHandler):
6956
def __init__(self):
7057
super().__init__()
@@ -88,7 +75,8 @@ def on_tool_call_delta(self, delta, snapshot):
8875
yield
8976

9077

91-
# Send a new message to a thread
78+
# Route to submit a new user message to a thread and mount a component that
79+
# will start an assistant run stream
9280
@router.post("/send")
9381
async def post_message(
9482
request: Request,
@@ -105,18 +93,20 @@ async def post_message(
10593

10694
)
10795

108-
return (
109-
user_message_html.format(user_input=userInput) +
110-
assistant_step_html.format(
111-
event_type="assistantMessage",
112-
event_name="message",
113-
assistant_id=assistant_id,
114-
thread_id=thread_id
115-
)
96+
# Render the component templates with the context
97+
user_message_html = templates.get_template("user_message.html").render(user_input=userInput)
98+
assistant_run_html = templates.get_template("assistant_run.html").render(
99+
assistant_id=assistant_id,
100+
thread_id=thread_id
116101
)
117102

103+
return (
104+
user_message_html +
105+
assistant_run_html
106+
)
118107

119108

109+
# Route to stream the response from the assistant via server-sent events
120110
@router.get("/receive")
121111
async def stream_response(
122112
assistant_id: str,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!-- assistant-run.html -->
2+
<div class="assistant-run" hx-swap="beforeend"
3+
sse-connect="/assistants/{{ assistant_id }}/messages/{{ thread_id }}/receive"
4+
sse-swap="messageCreated"
5+
sse-close="endStream">
6+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!-- assistant-step.html -->
2+
<div class="{event_type}"
3+
sse-swap="{event_name}"
4+
hx-swap="beforeend">
5+
</div>

templates/components/chat-turn.html

Lines changed: 0 additions & 9 deletions
This file was deleted.

templates/components/file-viewer.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!-- file-viewer.html -->
12
<div class="fileViewer">
23
<div class="filesList {{ 'grow' if files|length != 0 else '' }}"
34
hx-get="/assistants/{{ assistant_id }}/files"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!-- user-message.html -->
2+
<div class="userMessage">
3+
{user_input}
4+
</div>

templates/components/weather-widget.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!-- weather-widget.html -->
12
<div class="{{ 'weatherWidget' }} {{ 'weatherEmptyState' if isEmpty else conditionClassMap.get(conditions, 'weatherBGSunny') }}">
23
<div class="weatherWidgetData">
34
{% if isEmpty %}

templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!-- index.html -->
12
{% extends "layout.html" %}
23

34
{% block content %}

templates/layout.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!-- layout.html -->
12
<!DOCTYPE html>
23
<html lang="en">
34
<head>

templates/setup.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
<!-- setup.html -->
12
{% extends "layout.html" %}
23

34
{% block content %}
45
<main class="main">
6+
57
<div class="container">
68
<div class="setupContainer">
79
<h1 class="title">Setup Required</h1>

0 commit comments

Comments
 (0)