This commit is contained in:
Timothy Jaeryang Baek
2026-07-16 00:58:34 -04:00
parent 771a024b40
commit c55e373b99
14 changed files with 921 additions and 161 deletions
+51 -41
View File
@@ -48,6 +48,7 @@ from open_webui.models.models import Models
from open_webui.models.notes import Notes
from open_webui.models.oauth_sessions import OAuthSessions
from open_webui.models.users import UserModel, Users
from open_webui.events import EVENTS, publish_event
from open_webui.retrieval.utils import get_sources_from_items
from open_webui.routers.images import (
CreateImageForm,
@@ -129,13 +130,37 @@ from open_webui.utils.tools import (
get_tools,
get_updated_tool_function,
)
from open_webui.utils.webhook import post_webhook
from starlette.responses import JSONResponse, Response, StreamingResponse
logging.basicConfig(stream=sys.stdout, level=GLOBAL_LOG_LEVEL)
log = logging.getLogger(__name__)
async def publish_chat_finished_event(request: Request, user: UserModel, metadata: dict, title: str, content: str):
chat_id = metadata.get('chat_id')
if getattr(request.state, 'internal', False) is True or not chat_id or chat_id.startswith(('channel:', 'local:')):
return
webui_url = await Config.get('webui.url')
await publish_event(
request,
EVENTS.CHAT_FINISHED,
actor=user,
subject_id=chat_id,
subject_type='chat',
data={
'user_id': user.id,
'chat_id': chat_id,
'message_id': metadata.get('message_id'),
'model_id': metadata.get('model_id'),
'title': title,
'url': f'{webui_url}/c/{chat_id}' if webui_url else f'/c/{chat_id}',
'message': content,
},
message=title or 'Chat finished',
)
# We believe in one maker of all models, seen and unseen,
# and in the reasoning which proceeds from the architect.
# We look for the resurrection of dead processes and the
@@ -3585,26 +3610,7 @@ async def non_streaming_chat_response_handler(response, ctx):
},
)
# Send a webhook notification if the user is not active
if (
getattr(request.state, 'internal', False) is not True
and await Config.get('ui.enable_user_webhooks')
and not await Users.is_user_active(user.id)
):
webhook_url = await Users.get_user_webhook_url_by_id(user.id)
if webhook_url:
webui_url = await Config.get('webui.url')
await post_webhook(
request.app.state.WEBUI_NAME,
webhook_url,
f'{content}\n\n{title} - {webui_url}/c/{metadata["chat_id"]}',
{
'action': 'chat',
'message': content,
'title': title,
'url': f'{webui_url}/c/{metadata["chat_id"]}',
},
)
await publish_chat_finished_event(request, user, metadata, title, content)
ctx['assistant_message'] = {
'content': content,
@@ -3617,6 +3623,29 @@ async def non_streaming_chat_response_handler(response, ctx):
response = build_response_object(response, merge_events_into_response(response_data, events))
except Exception as e:
log.debug(f'Error occurred while processing request: {e}')
chat_id = metadata.get('chat_id')
if (
getattr(request.state, 'internal', False) is not True
and chat_id
and not chat_id.startswith(('channel:', 'local:'))
):
webui_url = await Config.get('webui.url')
await publish_event(
request,
EVENTS.CHAT_FAILED,
actor=user,
subject_id=chat_id,
subject_type='chat',
data={
'user_id': user.id,
'chat_id': chat_id,
'message_id': metadata.get('message_id'),
'model_id': metadata.get('model_id'),
'url': f'{webui_url}/c/{chat_id}' if webui_url else f'/c/{chat_id}',
'message': str(e),
},
message='Chat failed',
)
pass
return response
@@ -5310,26 +5339,7 @@ async def streaming_chat_response_handler(response, ctx):
touch=False,
)
# Send a webhook notification if the user is not active
if (
getattr(request.state, 'internal', False) is not True
and await Config.get('ui.enable_user_webhooks')
and not await Users.is_user_active(user.id)
):
webhook_url = await Users.get_user_webhook_url_by_id(user.id)
if webhook_url:
webui_url = await Config.get('webui.url')
await post_webhook(
request.app.state.WEBUI_NAME,
webhook_url,
f'{content}\n\n{title} - {webui_url}/c/{metadata["chat_id"]}',
{
'action': 'chat',
'message': content,
'title': title,
'url': f'{webui_url}/c/{metadata["chat_id"]}',
},
)
await publish_chat_finished_event(request, user, metadata, title, content)
await event_emitter(
{