Skip to content

Commit 3d2ce97

Browse files
Fix event type on imageOutput SSE
1 parent 07a9a12 commit 3d2ce97

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

routers/chat.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,9 @@ async def handle_assistant_stream(
247247
elif output.type == "image" and output.image and output.image.file_id:
248248
logger.debug(f"Image Output - File ID: {output.image.file_id}")
249249
# Create the image HTML on the backend
250-
image_html = f'<img src="/assistants/{assistant_id}/messages/{thread_id}/files/{output.image.file_id}/content" class="code-interpreter-image">'
250+
image_html = f'<img src="/assistants/{assistant_id}/files/{output.image.file_id}/content" class="code-interpreter-image">'
251251
yield sse_format(
252-
f"toolDelta{step_id}",
252+
f"imageOutput",
253253
image_html
254254
)
255255

@@ -362,28 +362,3 @@ async def event_generator() -> AsyncGenerator[str, None]:
362362
"Connection": "keep-alive",
363363
}
364364
)
365-
366-
# Route to serve image files from OpenAI API
367-
@router.get("/files/{file_id}/content")
368-
async def get_file_content(
369-
file_id: str,
370-
client: AsyncOpenAI = Depends(lambda: AsyncOpenAI())
371-
) -> StreamingResponse:
372-
"""
373-
Streams file content from OpenAI API.
374-
This route is used to serve images and other files generated by the code interpreter.
375-
"""
376-
try:
377-
# Get the file content from OpenAI
378-
file_content = await client.files.content(file_id)
379-
file_bytes = file_content.read() # Remove await since read() returns bytes directly
380-
381-
# Return the file content as a streaming response
382-
# Note: In a production environment, you might want to add caching
383-
return StreamingResponse(
384-
content=iter([file_bytes]),
385-
media_type="image/png" # You might want to make this dynamic based on file type
386-
)
387-
except Exception as e:
388-
logger.error(f"Error getting file content: {e}")
389-
raise HTTPException(status_code=500, detail=str(e))

routers/files.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,27 @@ async def delete_file(
105105
vector_store_id = await get_or_create_vector_store(assistant_id, client)
106106
await client.beta.vector_stores.files.delete(vector_store_id=vector_store_id, file_id=fileId)
107107
return {"message": "File deleted successfully"}
108+
109+
@router.get("/{file_id}/content")
110+
async def get_file_content(
111+
file_id: str,
112+
client: AsyncOpenAI = Depends(lambda: AsyncOpenAI())
113+
) -> StreamingResponse:
114+
"""
115+
Streams file content from OpenAI API.
116+
This route is used to serve images and other files generated by the code interpreter.
117+
"""
118+
try:
119+
# Get the file content from OpenAI
120+
file_content = await client.files.content(file_id)
121+
file_bytes = file_content.read() # Remove await since read() returns bytes directly
122+
123+
# Return the file content as a streaming response
124+
# Note: In a production environment, you might want to add caching
125+
return StreamingResponse(
126+
content=iter([file_bytes]),
127+
media_type="image/png" # You might want to make this dynamic based on file type
128+
)
129+
except Exception as e:
130+
logger.error(f"Error getting file content: {e}")
131+
raise HTTPException(status_code=500, detail=str(e))

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,codeInterpreterOutput"
5+
sse-swap="messageCreated,toolCallCreated,toolOutput,imageOutput,fileOutput"
66
sse-close="endStream"
77
data-assistant-id="{{ assistant_id }}"
88
data-thread-id="{{ thread_id }}">

0 commit comments

Comments
 (0)