Skip to content

Commit 4f92fbe

Browse files
hooked up weather widget
1 parent d7405ea commit 4f92fbe

File tree

4 files changed

+58
-14
lines changed

4 files changed

+58
-14
lines changed

routers/chat.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ async def handle_assistant_stream(
162162
stream_name=f'toolDelta{step_counter}'
163163
)
164164
)
165+
time.sleep(0.25) # Give the client time to render the message
165166

166167
if isinstance(event, ThreadRunStepDelta) and event.data.delta.step_details.type == "tool_calls":
167168
tool_call = event.data.delta.step_details.tool_calls[0]
@@ -258,9 +259,22 @@ async def event_generator():
258259
logger.error(f"Failed to parse function arguments: {err}")
259260
location = "Unknown"
260261

261-
weather_output = get_weather(location)
262+
weather_output: dict = get_weather(location)
262263
logger.info(f"Weather output: {weather_output}")
263264

265+
# Render the weather widget
266+
weather_widget_html: str = templates.get_template(
267+
"components/weather-widget.html"
268+
).render(
269+
location=weather_output.get("location", "Unknown"),
270+
temperature=weather_output.get("temperature", "Unknown"),
271+
unit=weather_output.get("unit", "F"), # Default to Fahrenheit
272+
conditions=weather_output.get("conditions", "Unknown")
273+
)
274+
275+
# Yield the rendered HTML
276+
yield sse_format("toolOutput", weather_widget_html)
277+
264278
data_for_tool = {
265279
"tool_outputs": {
266280
"output": str(weather_output),

static/styles.css

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,3 +502,37 @@ pre {
502502
text-align: center;
503503
margin-bottom: 8px;
504504
}
505+
506+
.toolOutput {
507+
display: flex;
508+
flex-direction: row;
509+
align-items: center;
510+
justify-content: space-between;
511+
padding: 10px;
512+
border-radius: var(--border-radius);
513+
background-color: #f9f9f9;
514+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
515+
margin-bottom: 10px;
516+
gap: 10px;
517+
}
518+
519+
.toolOutput h2,
520+
.toolOutput p {
521+
margin: 0;
522+
padding: 0 10px;
523+
flex: 1;
524+
text-align: center;
525+
}
526+
527+
@media (max-width: 600px) {
528+
.toolOutput {
529+
flex-direction: column;
530+
align-items: flex-start;
531+
}
532+
533+
.toolOutput h2,
534+
.toolOutput p {
535+
text-align: left;
536+
padding: 5px 0;
537+
}
538+
}

templates/components/assistant-run.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
<div class="assistant-run" hx-swap="beforeend"
33
hx-ext="sse"
44
sse-connect="/assistants/{{ assistant_id }}/messages/{{ thread_id }}/receive"
5-
sse-swap="messageCreated,toolCallCreated"
5+
sse-swap="messageCreated,toolCallCreated,toolOutput"
66
sse-close="endStream">
77
</div>
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
<!-- weather-widget.html -->
2-
<div class="{{ 'weatherWidget' }} {{ 'weatherEmptyState' if isEmpty else conditionClassMap.get(conditions, 'weatherBGSunny') }}">
3-
<div class="weatherWidgetData">
4-
{% if isEmpty %}
5-
<p>Enter a city to see local weather</p>
6-
<p>try: what's the weather like in Berkeley?</p>
7-
{% else %}
8-
<p>{{ location }}</p>
9-
<h2>{{ temperature if temperature != '---' else temperature }}°F</h2>
10-
<p>{{ conditions }}</p>
11-
{% endif %}
12-
</div>
13-
</div>
2+
<div class="toolOutput">
3+
<!-- Location -->
4+
<h2>{{location}}</h2>
5+
<!-- Temperature -->
6+
<p>{{temperature}}&deg;{{unit}}</p>
7+
<!-- Conditions -->
8+
<p>{{conditions}}</p>
9+
</div>

0 commit comments

Comments
 (0)