From 6c0a5fa91cdbf6ffb74667ee61ca96bebfdfbc50 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Thu, 11 Sep 2025 21:56:59 +0400 Subject: [PATCH] refac --- backend/open_webui/functions.py | 9 +++++---- backend/open_webui/main.py | 8 -------- backend/open_webui/routers/openai.py | 9 +++++---- backend/open_webui/utils/middleware.py | 18 ++++++++++-------- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/backend/open_webui/functions.py b/backend/open_webui/functions.py index 4122cbbe0d..7224d28113 100644 --- a/backend/open_webui/functions.py +++ b/backend/open_webui/functions.py @@ -221,10 +221,11 @@ async def generate_function_chat_completion( oauth_token = None try: - oauth_token = request.app.state.oauth_manager.get_oauth_token( - user.id, - request.cookies.get("oauth_session_id", None), - ) + if request.cookies.get("oauth_session_id", None): + oauth_token = request.app.state.oauth_manager.get_oauth_token( + user.id, + request.cookies.get("oauth_session_id", None), + ) except Exception as e: log.error(f"Error getting OAuth token: {e}") diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index a5d55f75ab..6b536c78bc 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -1418,14 +1418,6 @@ async def chat_completion( model_item = form_data.pop("model_item", {}) tasks = form_data.pop("background_tasks", None) - oauth_token = None - try: - oauth_token = request.app.state.oauth_manager.get_oauth_token( - user.id, request.cookies.get("oauth_session_id", None) - ) - except Exception as e: - log.error(f"Error getting OAuth token: {e}") - metadata = {} try: if not model_item.get("direct", False): diff --git a/backend/open_webui/routers/openai.py b/backend/open_webui/routers/openai.py index 184f47038d..8d8883a02c 100644 --- a/backend/open_webui/routers/openai.py +++ b/backend/open_webui/routers/openai.py @@ -171,10 +171,11 @@ def get_headers_and_cookies( oauth_token = None try: - oauth_token = request.app.state.oauth_manager.get_oauth_token( - user.id, - request.cookies.get("oauth_session_id", None), - ) + if request.cookies.get("oauth_session_id", None): + oauth_token = request.app.state.oauth_manager.get_oauth_token( + user.id, + request.cookies.get("oauth_session_id", None), + ) except Exception as e: log.error(f"Error getting OAuth token: {e}") diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 3dfbb38ae5..42e413afaf 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -817,10 +817,11 @@ async def process_chat_payload(request, form_data, user, metadata, model): oauth_token = None try: - oauth_token = request.app.state.oauth_manager.get_oauth_token( - user.id, - request.cookies.get("oauth_session_id", None), - ) + if request.cookies.get("oauth_session_id", None): + oauth_token = request.app.state.oauth_manager.get_oauth_token( + user.id, + request.cookies.get("oauth_session_id", None), + ) except Exception as e: log.error(f"Error getting OAuth token: {e}") @@ -1496,10 +1497,11 @@ async def process_chat_response( oauth_token = None try: - oauth_token = request.app.state.oauth_manager.get_oauth_token( - user.id, - request.cookies.get("oauth_session_id", None), - ) + if request.cookies.get("oauth_session_id", None): + oauth_token = request.app.state.oauth_manager.get_oauth_token( + user.id, + request.cookies.get("oauth_session_id", None), + ) except Exception as e: log.error(f"Error getting OAuth token: {e}")