refac
This commit is contained in:
1 parent
bab71ed08b
commit
d484a2a99e
4 files changed
+25
-9
No files matched your search
@@ -220,6 +220,7 @@ from open_webui.utils.chat import (
|
|||||||
from open_webui.utils.chat import (
|
from open_webui.utils.chat import (
|
||||||
generate_chat_completion as chat_completion_handler,
|
generate_chat_completion as chat_completion_handler,
|
||||||
)
|
)
|
||||||
|
from open_webui.utils.chat_id import is_saved_chat_id
|
||||||
from open_webui.utils.chat_variables import (
|
from open_webui.utils.chat_variables import (
|
||||||
normalize_chat_variables,
|
normalize_chat_variables,
|
||||||
)
|
)
|
||||||
@@ -264,7 +265,7 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
async def emit_chat_list_event(metadata: dict, chat_id: str):
|
async def emit_chat_list_event(metadata: dict, chat_id: str):
|
||||||
if not chat_id or chat_id.startswith(('channel:', 'local:')):
|
if not is_saved_chat_id(chat_id):
|
||||||
return
|
return
|
||||||
|
|
||||||
event_emitter = await get_event_emitter(metadata, update_db=False)
|
event_emitter = await get_event_emitter(metadata, update_db=False)
|
||||||
@@ -1141,7 +1142,11 @@ async def chat_completion(
|
|||||||
chat_id = form_data.get('chat_id') or ''
|
chat_id = form_data.get('chat_id') or ''
|
||||||
chat_variables = form_data.pop('chat_variables', None)
|
chat_variables = form_data.pop('chat_variables', None)
|
||||||
if chat_variables is None:
|
if chat_variables is None:
|
||||||
existing_chat = await Chats.get_chat_by_id(chat_id) if chat_id else None
|
existing_chat = (
|
||||||
|
await Chats.get_chat_by_id(chat_id)
|
||||||
|
if is_saved_chat_id(chat_id)
|
||||||
|
else None
|
||||||
|
)
|
||||||
chat_variables = existing_chat.variables if existing_chat else {}
|
chat_variables = existing_chat.variables if existing_chat else {}
|
||||||
|
|
||||||
chat_variables = normalize_chat_variables(chat_variables)
|
chat_variables = normalize_chat_variables(chat_variables)
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ from open_webui.socket.utils import RedisDict, RedisLock, YdocManager
|
|||||||
from open_webui.tasks import create_task, stop_item_tasks
|
from open_webui.tasks import create_task, stop_item_tasks
|
||||||
from open_webui.utils.access_control import has_permission
|
from open_webui.utils.access_control import has_permission
|
||||||
from open_webui.utils.auth import get_verified_user_by_token
|
from open_webui.utils.auth import get_verified_user_by_token
|
||||||
|
from open_webui.utils.chat_id import is_saved_chat_id
|
||||||
from open_webui.utils.redis import (
|
from open_webui.utils.redis import (
|
||||||
build_sentinel_url,
|
build_sentinel_url,
|
||||||
get_redis_connection,
|
get_redis_connection,
|
||||||
@@ -945,6 +946,7 @@ async def get_event_emitter(request_info, update_db=True):
|
|||||||
chat_id = request_info['chat_id']
|
chat_id = request_info['chat_id']
|
||||||
message_id = request_info['message_id']
|
message_id = request_info['message_id']
|
||||||
internal = request_info.get('internal') is True
|
internal = request_info.get('internal') is True
|
||||||
|
save_to_chat = update_db and message_id and is_saved_chat_id(chat_id)
|
||||||
|
|
||||||
if internal and event_data.get('type') == 'notification':
|
if internal and event_data.get('type') == 'notification':
|
||||||
return
|
return
|
||||||
@@ -963,7 +965,7 @@ async def get_event_emitter(request_info, update_db=True):
|
|||||||
room=room,
|
room=room,
|
||||||
)
|
)
|
||||||
|
|
||||||
if update_db and message_id and not (request_info.get('chat_id') or '').startswith('local:'):
|
if save_to_chat:
|
||||||
event_type = event_data.get('type')
|
event_type = event_data.get('type')
|
||||||
|
|
||||||
if event_type == 'status':
|
if event_type == 'status':
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ from open_webui.routers.retrieval import search_web as _search_web
|
|||||||
from open_webui.tasks import stop_item_tasks
|
from open_webui.tasks import stop_item_tasks
|
||||||
from open_webui.events import EVENTS, publish_event
|
from open_webui.events import EVENTS, publish_event
|
||||||
from open_webui.socket.main import sio
|
from open_webui.socket.main import sio
|
||||||
|
from open_webui.utils.chat_id import is_saved_chat_id
|
||||||
from open_webui.utils.notifications import notify_target
|
from open_webui.utils.notifications import notify_target
|
||||||
from open_webui.utils.sanitize import sanitize_code
|
from open_webui.utils.sanitize import sanitize_code
|
||||||
|
|
||||||
@@ -378,7 +379,7 @@ async def generate_image(
|
|||||||
image_files = [{'type': 'image', 'url': img['url']} for img in images]
|
image_files = [{'type': 'image', 'url': img['url']} for img in images]
|
||||||
|
|
||||||
# Persist files to DB if chat context is available
|
# Persist files to DB if chat context is available
|
||||||
if __chat_id__ and __message_id__ and images:
|
if is_saved_chat_id(__chat_id__) and __message_id__ and images:
|
||||||
db_files = await Chats.add_message_files_by_id_and_message_id(
|
db_files = await Chats.add_message_files_by_id_and_message_id(
|
||||||
__chat_id__,
|
__chat_id__,
|
||||||
__message_id__,
|
__message_id__,
|
||||||
@@ -446,7 +447,7 @@ async def edit_image(
|
|||||||
image_files = [{'type': 'image', 'url': img['url']} for img in images]
|
image_files = [{'type': 'image', 'url': img['url']} for img in images]
|
||||||
|
|
||||||
# Persist files to DB if chat context is available
|
# Persist files to DB if chat context is available
|
||||||
if __chat_id__ and __message_id__ and images:
|
if is_saved_chat_id(__chat_id__) and __message_id__ and images:
|
||||||
db_files = await Chats.add_message_files_by_id_and_message_id(
|
db_files = await Chats.add_message_files_by_id_and_message_id(
|
||||||
__chat_id__,
|
__chat_id__,
|
||||||
__message_id__,
|
__message_id__,
|
||||||
@@ -3164,8 +3165,8 @@ async def create_tasks(
|
|||||||
:param tasks: List of task items. Each item: content (string, required), status (pending|in_progress|completed|cancelled, default pending), id (optional, auto-generated).
|
:param tasks: List of task items. Each item: content (string, required), status (pending|in_progress|completed|cancelled, default pending), id (optional, auto-generated).
|
||||||
:return: JSON with the full task list and summary counts
|
:return: JSON with the full task list and summary counts
|
||||||
"""
|
"""
|
||||||
if __chat_id__ is None:
|
if not is_saved_chat_id(__chat_id__):
|
||||||
return json.dumps({'error': 'Chat context not available'})
|
return json.dumps({'error': 'Saved chat context not available'})
|
||||||
|
|
||||||
try:
|
try:
|
||||||
all_tasks = []
|
all_tasks = []
|
||||||
@@ -3216,8 +3217,8 @@ async def update_task(
|
|||||||
:param status: New status: completed, in_progress, pending, or cancelled (default: completed)
|
:param status: New status: completed, in_progress, pending, or cancelled (default: completed)
|
||||||
:return: JSON with the updated task list and summary counts
|
:return: JSON with the updated task list and summary counts
|
||||||
"""
|
"""
|
||||||
if __chat_id__ is None:
|
if not is_saved_chat_id(__chat_id__):
|
||||||
return json.dumps({'error': 'Chat context not available'})
|
return json.dumps({'error': 'Saved chat context not available'})
|
||||||
|
|
||||||
try:
|
try:
|
||||||
status = status.strip().lower()
|
status = status.strip().lower()
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
NON_SAVED_CHAT_ID_PREFIXES = ('local:', 'channel:')
|
||||||
|
|
||||||
|
|
||||||
|
def is_saved_chat_id(chat_id: Optional[str]) -> bool:
|
||||||
|
return bool(chat_id) and not chat_id.startswith(NON_SAVED_CHAT_ID_PREFIXES)
|
||||||
Reference in new issue
Block a user