fix: fillter exception handling

This commit is contained in:
Timothy Jaeryang Baek 2025-09-01 14:14:20 +04:00
parent 4b97884fce
commit f56889c5c7
3 changed files with 23 additions and 9 deletions

View file

@ -1519,7 +1519,7 @@ async def chat_completion(
try: try:
event_emitter = get_event_emitter(metadata) event_emitter = get_event_emitter(metadata)
await event_emitter( await event_emitter(
{"type": "task-cancelled"}, {"type": "chat:tasks:cancel"},
) )
except Exception as e: except Exception as e:
pass pass
@ -1535,14 +1535,21 @@ async def chat_completion(
"error": {"content": str(e)}, "error": {"content": str(e)},
}, },
) )
event_emitter = get_event_emitter(metadata)
await event_emitter(
{
"type": "chat:message:error",
"data": {"error": {"content": str(e)}},
}
)
await event_emitter(
{"type": "chat:tasks:cancel"},
)
except: except:
pass pass
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e),
)
if ( if (
metadata.get("session_id") metadata.get("session_id")
and metadata.get("chat_id") and metadata.get("chat_id")

View file

@ -885,7 +885,7 @@ async def process_chat_payload(request, form_data, user, metadata, model):
extra_params=extra_params, extra_params=extra_params,
) )
except Exception as e: except Exception as e:
raise Exception(f"Error: {e}") raise Exception(f"{e}")
features = form_data.pop("features", None) features = form_data.pop("features", None)
if features: if features:
@ -1316,7 +1316,7 @@ async def process_chat_response(
{ {
"type": "chat:message:error", "type": "chat:message:error",
"data": {"error": {"content": error}}, "data": {"error": {"content": error}},
}, }
) )
if "selected_model_id" in response_data: if "selected_model_id" in response_data:
@ -2624,7 +2624,7 @@ async def process_chat_response(
await background_tasks_handler() await background_tasks_handler()
except asyncio.CancelledError: except asyncio.CancelledError:
log.warning("Task was cancelled!") log.warning("Task was cancelled!")
await event_emitter({"type": "task-cancelled"}) await event_emitter({"type": "chat:tasks:cancel"})
if not ENABLE_REALTIME_CHAT_SAVE: if not ENABLE_REALTIME_CHAT_SAVE:
# Save message in the database # Save message in the database

View file

@ -318,6 +318,13 @@
} }
} else if (type === 'chat:completion') { } else if (type === 'chat:completion') {
chatCompletionEventHandler(data, message, event.chat_id); chatCompletionEventHandler(data, message, event.chat_id);
} else if (type === 'chat:tasks:cancel') {
taskIds = null;
const responseMessage = history.messages[history.currentId];
// Set all response messages to done
for (const messageId of history.messages[responseMessage.parentId].childrenIds) {
history.messages[messageId].done = true;
}
} else if (type === 'chat:message:delta' || type === 'message') { } else if (type === 'chat:message:delta' || type === 'message') {
message.content += data.content; message.content += data.content;
} else if (type === 'chat:message' || type === 'replace') { } else if (type === 'chat:message' || type === 'replace') {