mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-25 23:00:53 -05:00
refac
This commit is contained in:
1 parent
cbaefab3d4
commit
18f7633afa
6 files changed
+51
-12
No files matched your search
@@ -1345,8 +1345,10 @@ async def chat_completion(
|
||||
raise HTTPException(status_code=404, detail='Temporary chat expired or unavailable')
|
||||
|
||||
initial_title_generation = None
|
||||
if is_new_chat and not agent_model and tasks and TASKS.TITLE_GENERATION in tasks:
|
||||
if is_new_chat and tasks and TASKS.TITLE_GENERATION in tasks:
|
||||
initial_title_generation = tasks.pop(TASKS.TITLE_GENERATION)
|
||||
elif is_new_chat and agent_model:
|
||||
initial_title_generation = False # Still give agent chats a first-message title.
|
||||
|
||||
if metadata.get('chat_id') and user:
|
||||
chat_id = metadata['chat_id']
|
||||
|
||||
@@ -1015,11 +1015,15 @@ class ChatTable:
|
||||
await session.commit()
|
||||
return result.rowcount or 0
|
||||
|
||||
async def update_chat_title_by_id(self, id: str, title: str) -> ChatModel | None:
|
||||
async def update_chat_title_by_id(
|
||||
self, id: str, title: str, *, expected_title: str | None = None
|
||||
) -> ChatModel | None:
|
||||
try:
|
||||
async with self._chat_transaction(id) as (session, chat_item):
|
||||
if chat_item is None:
|
||||
return None
|
||||
if expected_title is not None and chat_item.title != expected_title:
|
||||
return None
|
||||
clean_title = self._clean_null_bytes(title)
|
||||
chat_item.title = clean_title
|
||||
chat_item.chat = {**(chat_item.chat or {}), 'title': clean_title}
|
||||
|
||||
@@ -160,13 +160,15 @@ async def generate_title(request: Request, form_data: dict, user=Depends(get_ver
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail='No model specified for title generation. Please ensure a model is selected for this chat.',
|
||||
)
|
||||
if model_id not in models:
|
||||
if model_id not in models and not model_id.startswith('agent:'):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=ERROR_MESSAGES.MODEL_NOT_FOUND(),
|
||||
)
|
||||
|
||||
task_model_id, task_model_params = await get_task_model_generation_config(model_id, models)
|
||||
if task_model_id not in models or task_model_id.startswith('agent:'):
|
||||
return JSONResponse(content={'detail': 'No task model configured for agent chat titles'})
|
||||
|
||||
log.debug('generating chat title using model %s for user %s ', task_model_id, user.email)
|
||||
|
||||
|
||||
@@ -3733,6 +3733,19 @@ async def background_tasks_handler(ctx):
|
||||
user_message = user_message[:100] + '...'
|
||||
|
||||
title = None
|
||||
expected_title = None
|
||||
if str(message['model']).startswith('agent:'):
|
||||
first_user = next((m for m in messages if m.get('role') == 'user'), {})
|
||||
title = (
|
||||
replace_skill_mentions_with_labels(first_user.get('content', '')).strip()[:50] or 'New Chat'
|
||||
)
|
||||
updated = await Chats.update_chat_title_by_id(
|
||||
metadata['chat_id'], title, expected_title='New Chat'
|
||||
)
|
||||
if not updated:
|
||||
return # The chat was deleted or already renamed.
|
||||
await event_emitter({'type': 'chat:title', 'data': title})
|
||||
expected_title = title
|
||||
if tasks[TASKS.TITLE_GENERATION]:
|
||||
res = await generate_title(
|
||||
request,
|
||||
@@ -3761,22 +3774,29 @@ async def background_tasks_handler(ctx):
|
||||
title_string = title_string[title_string.find('{') : title_string.rfind('}') + 1]
|
||||
|
||||
try:
|
||||
title = JSONCodec.loads(title_string).get('title', user_message)
|
||||
title = JSONCodec.loads(title_string).get('title', expected_title or user_message)
|
||||
except Exception as e:
|
||||
title = ''
|
||||
|
||||
if not title:
|
||||
title = replace_skill_mentions_with_labels(messages[0].get('content', user_message))
|
||||
title = expected_title or replace_skill_mentions_with_labels(
|
||||
messages[0].get('content', user_message)
|
||||
)
|
||||
|
||||
await Chats.update_chat_title_by_id(metadata['chat_id'], title)
|
||||
|
||||
await event_emitter(
|
||||
{
|
||||
'type': 'chat:title',
|
||||
'data': title,
|
||||
}
|
||||
if expected_title is not None and (not isinstance(title, str) or not title.strip()):
|
||||
title = expected_title
|
||||
updated = await Chats.update_chat_title_by_id(
|
||||
metadata['chat_id'], title, expected_title=expected_title
|
||||
)
|
||||
|
||||
if updated:
|
||||
await event_emitter(
|
||||
{
|
||||
'type': 'chat:title',
|
||||
'data': title,
|
||||
}
|
||||
)
|
||||
|
||||
if title == None and len(messages) == 2 and (not messages_map or len(messages_map) <= 2):
|
||||
title = replace_skill_mentions_with_labels(messages[0].get('content', user_message))
|
||||
|
||||
|
||||
@@ -15,6 +15,16 @@ log = logging.getLogger(__name__)
|
||||
# Let the right tool be given for the work at hand,
|
||||
# not the one that flatters, but the one that serves.
|
||||
def get_task_model_id(default_model_id: str, task_model: str, task_model_external: str, models) -> str:
|
||||
if default_model_id.startswith('agent:'):
|
||||
# Agent sessions cannot serve ordinary completion requests for background tasks.
|
||||
return next(
|
||||
(
|
||||
model
|
||||
for model in (task_model_external, task_model)
|
||||
if model and model in models and not model.startswith('agent:')
|
||||
),
|
||||
default_model_id,
|
||||
)
|
||||
task_model_id = default_model_id
|
||||
# Use an available task model override for the local or external connection.
|
||||
if models.get(task_model_id, {}).get('connection_type') == 'local':
|
||||
|
||||
@@ -1282,6 +1282,7 @@
|
||||
</div>
|
||||
<div
|
||||
bind:clientHeight={footerHeight}
|
||||
role="presentation"
|
||||
class="min-w-0 shrink-0 pointer-events-auto"
|
||||
class:hidden={fullWidth && !gitPanelRect}
|
||||
style={gitPanelRect
|
||||
|
||||
Reference in new issue
Block a user