Skip to content

Commit 04bd042

Browse files
committed
refac
1 parent 485d689 commit 04bd042

2 files changed

Lines changed: 34 additions & 31 deletions

File tree

backend/open_webui/main.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@
583583
from open_webui.tasks import (
584584
redis_task_command_listener,
585585
list_task_ids_by_item_id,
586+
has_active_tasks,
587+
cleanup_task,
586588
create_task,
587589
stop_task,
588590
stop_item_tasks,
@@ -2063,22 +2065,21 @@ async def emit_cancel_event():
20632065
except BaseException as e:
20642066
log.debug(f'Error cleaning up MCP clients: {e}')
20652067

2068+
# Deregister this task, then emit chat:active=false if no others remain
20662069
try:
2067-
if metadata.get('chat_id'):
2068-
2069-
async def emit_inactive_event():
2070-
try:
2071-
event_emitter = await get_event_emitter(metadata, update_db=False)
2072-
if event_emitter:
2073-
await event_emitter({'type': 'chat:active', 'data': {'active': False}})
2074-
except Exception:
2075-
pass
2076-
2077-
try:
2078-
# Shield the event emission so it finishes even if the main task is cancelled
2079-
await asyncio.shield(emit_inactive_event())
2080-
except asyncio.CancelledError:
2081-
pass
2070+
chat_id = metadata.get('chat_id')
2071+
task_id = metadata.get('task_id')
2072+
if chat_id and task_id:
2073+
await cleanup_task(request.app.state.redis, task_id, chat_id)
2074+
if not await has_active_tasks(request.app.state.redis, chat_id):
2075+
event_emitter = await get_event_emitter(metadata, update_db=False)
2076+
if event_emitter:
2077+
try:
2078+
await asyncio.shield(
2079+
event_emitter({'type': 'chat:active', 'data': {'active': False}})
2080+
)
2081+
except asyncio.CancelledError:
2082+
pass
20822083
except Exception:
20832084
pass
20842085

@@ -2128,6 +2129,7 @@ async def emit_inactive_event():
21282129
),
21292130
id=chat_id,
21302131
)
2132+
per_model_metadata['task_id'] = task_id
21312133
task_ids.append(task_id)
21322134

21332135
# Emit chat:active=true

src/lib/components/chat/Chat.svelte

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,7 @@
13981398
autoScroll = true;
13991399
await tick();
14001400
1401+
// Mark all non-current assistant messages as done
14011402
if (history.currentId) {
14021403
for (const message of Object.values(history.messages)) {
14031404
if (
@@ -1411,23 +1412,23 @@
14111412
}
14121413
}
14131414
1414-
const taskRes = await getTaskIdsByChatId(localStorage.token, $chatId).catch((error) => {
1415-
return null;
1416-
});
1417-
1418-
if (taskRes) {
1419-
taskIds = taskRes.task_ids;
1420-
}
1421-
1422-
// If no active tasks and current message is incomplete, generation was interrupted
1415+
// Reconcile active tasks with message state:
1416+
// If the response is already done, remaining tasks are just background
1417+
// work (follow-ups, title gen) that shouldn't block the input.
1418+
const pendingTaskIds = await getTaskIdsByChatId(localStorage.token, $chatId)
1419+
.then((res) => res?.task_ids ?? [])
1420+
.catch(() => []);
14231421
const currentMessage = history.currentId ? history.messages[history.currentId] : null;
1424-
if (
1425-
currentMessage &&
1426-
currentMessage.role === 'assistant' &&
1427-
!currentMessage.done &&
1428-
(!taskIds || taskIds.length === 0)
1429-
) {
1430-
currentMessage.done = true;
1422+
const responseComplete = currentMessage?.role === 'assistant' && currentMessage?.done;
1423+
1424+
if (pendingTaskIds.length > 0 && !responseComplete) {
1425+
taskIds = pendingTaskIds;
1426+
} else {
1427+
taskIds = null;
1428+
// No active tasks and message incomplete → generation was interrupted
1429+
if (currentMessage?.role === 'assistant' && !currentMessage.done) {
1430+
currentMessage.done = true;
1431+
}
14311432
}
14321433
14331434
await tick();

0 commit comments

Comments
 (0)