refac
This commit is contained in:
1 parent
f152ad36b3
commit
552bbcecfa
3 files changed
+85
-63
No files matched your search
@@ -948,17 +948,27 @@ async def get_event_emitter(request_info, update_db=True):
|
|||||||
|
|
||||||
async def get_event_call(request_info):
|
async def get_event_call(request_info):
|
||||||
async def __event_caller__(event_data):
|
async def __event_caller__(event_data):
|
||||||
response = await sio.call(
|
session_id = request_info['session_id']
|
||||||
'events',
|
|
||||||
{
|
# Fast-fail if the client has disconnected.
|
||||||
'chat_id': request_info.get('chat_id', None),
|
if session_id not in SESSION_POOL:
|
||||||
'message_id': request_info.get('message_id', None),
|
log.warning(f'Event caller: session {session_id} no longer connected')
|
||||||
'data': event_data,
|
return {'error': 'Client session disconnected.'}
|
||||||
},
|
|
||||||
to=request_info['session_id'],
|
try:
|
||||||
timeout=WEBSOCKET_EVENT_CALLER_TIMEOUT,
|
return await sio.call(
|
||||||
)
|
'events',
|
||||||
return response
|
{
|
||||||
|
'chat_id': request_info.get('chat_id', None),
|
||||||
|
'message_id': request_info.get('message_id', None),
|
||||||
|
'data': event_data,
|
||||||
|
},
|
||||||
|
to=session_id,
|
||||||
|
timeout=WEBSOCKET_EVENT_CALLER_TIMEOUT,
|
||||||
|
)
|
||||||
|
except TimeoutError:
|
||||||
|
log.warning(f'Event caller timed out for session {session_id}')
|
||||||
|
return {'error': 'Event call timed out. The browser tab may be inactive or closed.'}
|
||||||
|
|
||||||
if 'session_id' in request_info and 'chat_id' in request_info and 'message_id' in request_info:
|
if 'session_id' in request_info and 'chat_id' in request_info and 'message_id' in request_info:
|
||||||
return __event_caller__
|
return __event_caller__
|
||||||
|
|||||||
@@ -471,9 +471,15 @@ async def execute_code(
|
|||||||
|
|
||||||
# Parse the output - pyodide returns dict with stdout, stderr, result
|
# Parse the output - pyodide returns dict with stdout, stderr, result
|
||||||
if isinstance(output, dict):
|
if isinstance(output, dict):
|
||||||
stdout = output.get('stdout', '')
|
# Handle error responses from event_caller (e.g. session disconnected, timeout)
|
||||||
stderr = output.get('stderr', '')
|
if output.get('error') and not output.get('stdout') and not output.get('result'):
|
||||||
result = output.get('result', '')
|
stderr = output['error']
|
||||||
|
stdout = ''
|
||||||
|
result = ''
|
||||||
|
else:
|
||||||
|
stdout = output.get('stdout', '')
|
||||||
|
stderr = output.get('stderr', '')
|
||||||
|
result = output.get('result', '')
|
||||||
else:
|
else:
|
||||||
stdout = ''
|
stdout = ''
|
||||||
stderr = ''
|
stderr = ''
|
||||||
|
|||||||
+55
-49
@@ -483,59 +483,18 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((event.chat_id !== $chatId && !$temporaryChatEnabled) || isInBackground) {
|
// Session-targeted RPC calls (code execution, tool calls, direct completion)
|
||||||
if (type === 'chat:completion') {
|
// must ALWAYS be processed regardless of active chat or tab visibility,
|
||||||
const { done, content, title } = data;
|
// because the backend's sio.call blocks waiting for our callback response.
|
||||||
const displayTitle = title || $i18n.t('New Chat');
|
if (data?.session_id === $socket.id) {
|
||||||
|
|
||||||
if (done) {
|
|
||||||
if (
|
|
||||||
($settings?.notificationSound ?? true) &&
|
|
||||||
($settings?.notificationSoundAlways ?? false)
|
|
||||||
) {
|
|
||||||
playingNotificationSound.set(true);
|
|
||||||
|
|
||||||
const audio = new Audio(`/audio/notification.mp3`);
|
|
||||||
audio.play().finally(() => {
|
|
||||||
// Ensure the global state is reset after the sound finishes
|
|
||||||
playingNotificationSound.set(false);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($isLastActiveTab) {
|
|
||||||
if ($settings?.notificationEnabled ?? false) {
|
|
||||||
new Notification(`${displayTitle} • Open WebUI`, {
|
|
||||||
body: content,
|
|
||||||
icon: `${WEBUI_BASE_URL}/static/favicon.png`
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
toast.custom(NotificationToast, {
|
|
||||||
componentProps: {
|
|
||||||
onClick: () => {
|
|
||||||
goto(`/c/${event.chat_id}`);
|
|
||||||
},
|
|
||||||
content: content,
|
|
||||||
title: displayTitle
|
|
||||||
},
|
|
||||||
duration: 15000,
|
|
||||||
unstyled: true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else if (type === 'chat:title') {
|
|
||||||
currentChatPage.set(1);
|
|
||||||
await chats.set(await getChatList(localStorage.token, $currentChatPage));
|
|
||||||
} else if (type === 'chat:tags') {
|
|
||||||
tags.set(await getAllTags(localStorage.token));
|
|
||||||
}
|
|
||||||
} else if (data?.session_id === $socket.id) {
|
|
||||||
if (type === 'execute:python') {
|
if (type === 'execute:python') {
|
||||||
console.log('execute:python', data);
|
console.log('execute:python', data);
|
||||||
executePythonAsWorker(data.id, data.code, cb, data.files || []);
|
executePythonAsWorker(data.id, data.code, cb, data.files || []);
|
||||||
|
return;
|
||||||
} else if (type === 'execute:tool') {
|
} else if (type === 'execute:tool') {
|
||||||
console.log('execute:tool', data);
|
console.log('execute:tool', data);
|
||||||
executeTool(data, cb, event.chat_id);
|
executeTool(data, cb, event.chat_id);
|
||||||
|
return;
|
||||||
} else if (type === 'request:chat:completion') {
|
} else if (type === 'request:chat:completion') {
|
||||||
console.log(data, $socket.id);
|
console.log(data, $socket.id);
|
||||||
const { session_id, channel, form_data, model } = data;
|
const { session_id, channel, form_data, model } = data;
|
||||||
@@ -621,8 +580,55 @@
|
|||||||
done: true
|
done: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
return;
|
||||||
console.log('chatEventHandler', event);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((event.chat_id !== $chatId && !$temporaryChatEnabled) || isInBackground) {
|
||||||
|
if (type === 'chat:completion') {
|
||||||
|
const { done, content, title } = data;
|
||||||
|
const displayTitle = title || $i18n.t('New Chat');
|
||||||
|
|
||||||
|
if (done) {
|
||||||
|
if (
|
||||||
|
($settings?.notificationSound ?? true) &&
|
||||||
|
($settings?.notificationSoundAlways ?? false)
|
||||||
|
) {
|
||||||
|
playingNotificationSound.set(true);
|
||||||
|
|
||||||
|
const audio = new Audio(`/audio/notification.mp3`);
|
||||||
|
audio.play().finally(() => {
|
||||||
|
// Ensure the global state is reset after the sound finishes
|
||||||
|
playingNotificationSound.set(false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($isLastActiveTab) {
|
||||||
|
if ($settings?.notificationEnabled ?? false) {
|
||||||
|
new Notification(`${displayTitle} • Open WebUI`, {
|
||||||
|
body: content,
|
||||||
|
icon: `${WEBUI_BASE_URL}/static/favicon.png`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.custom(NotificationToast, {
|
||||||
|
componentProps: {
|
||||||
|
onClick: () => {
|
||||||
|
goto(`/c/${event.chat_id}`);
|
||||||
|
},
|
||||||
|
content: content,
|
||||||
|
title: displayTitle
|
||||||
|
},
|
||||||
|
duration: 15000,
|
||||||
|
unstyled: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if (type === 'chat:title') {
|
||||||
|
currentChatPage.set(1);
|
||||||
|
await chats.set(await getChatList(localStorage.token, $currentChatPage));
|
||||||
|
} else if (type === 'chat:tags') {
|
||||||
|
tags.set(await getAllTags(localStorage.token));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user