Skip to content

Commit 0cc2db3

Browse files
Merge pull request #31 from Promptly-Technologies-LLC/sse-with-oob-swap
Instead of splitting the SSE stream, use a consolidated stream with oob swap for some events
2 parents 5a9d40f + c18a2fc commit 0cc2db3

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

routers/chat.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
import time
32
from datetime import datetime
43
from typing import AsyncGenerator, Optional, Union
54
from fastapi.templating import Jinja2Templates
@@ -113,16 +112,19 @@ async def handle_assistant_stream(
113112
"messageCreated",
114113
templates.get_template("components/assistant-step.html").render(
115114
step_type="assistantMessage",
116-
stream_name=f"textDelta{step_id}"
115+
step_id=step_id
117116
)
118117
)
119118

120119
if isinstance(event, ThreadMessageDelta) and event.data.delta.content:
121120
content: MessageContentDelta = event.data.delta.content[0]
122121
if isinstance(content, TextDeltaBlock) and content.text and content.text.value:
122+
step_id = event.data.id
123+
text_value = content.text.value
124+
sse_data = f'<span hx-swap-oob="beforeend:#step-{step_id}">{text_value}</span>'
123125
yield sse_format(
124-
f"textDelta{step_id}",
125-
content.text.value
126+
"textDelta",
127+
sse_data
126128
)
127129

128130
if isinstance(event, ThreadRunStepCreated) and event.data.type == "tool_calls":

templates/components/assistant-run.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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,toolOutput,imageOutput,fileOutput"
5+
sse-swap="messageCreated,toolCallCreated,toolOutput,imageOutput,fileOutput,textDelta"
66
sse-close="endStream"
77
data-assistant-id="{{ assistant_id }}"
88
data-thread-id="{{ thread_id }}">
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<!-- assistant-step.html -->
2-
<div class="{{ step_type }}" sse-swap="{{ stream_name }}"></div>
2+
<div class="{{ step_type }}" id="step-{{ step_id }}"></div>

0 commit comments

Comments
 (0)