Skip to content

Commit 8934bfb

Browse files
committed
refac
1 parent fd56086 commit 8934bfb

3 files changed

Lines changed: 113 additions & 15 deletions

File tree

backend/open_webui/utils/context_compaction.py

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,81 @@ async def compact_messages_for_request(
6666
if not compacted_messages or not recent_messages:
6767
return messages, previous_summary, False
6868

69-
summary = await _generate_summary(
70-
request,
71-
user,
72-
model_id,
73-
models,
74-
compacted_messages,
75-
recent_messages,
76-
previous_summary,
77-
config['prompt_template'],
78-
)
69+
event_emitter = None
70+
if metadata.get('chat_id') and metadata.get('message_id'):
71+
from open_webui.socket.main import get_event_emitter
72+
73+
event_emitter = await get_event_emitter(metadata)
74+
75+
if event_emitter:
76+
await event_emitter(
77+
{
78+
'type': 'context_compaction',
79+
'data': {
80+
'action': 'context_compaction',
81+
'description': 'Compacting context',
82+
'done': False,
83+
},
84+
}
85+
)
86+
87+
try:
88+
summary = await _generate_summary(
89+
request,
90+
user,
91+
model_id,
92+
models,
93+
compacted_messages,
94+
recent_messages,
95+
previous_summary,
96+
config['prompt_template'],
97+
)
98+
except Exception:
99+
if event_emitter:
100+
await event_emitter(
101+
{
102+
'type': 'context_compaction',
103+
'data': {
104+
'action': 'context_compaction',
105+
'description': 'Context compaction failed',
106+
'done': True,
107+
'error': True,
108+
},
109+
}
110+
)
111+
raise
79112

80113
chat_id = metadata.get('chat_id')
81-
message_id = metadata.get('message_id')
82-
if chat_id and message_id and not chat_id.startswith(('local:', 'channel:')):
83-
await Chats.upsert_message_to_chat_by_id_and_message_id(chat_id, message_id, {'contextSummary': summary})
114+
checkpoint_message_id = metadata.get('user_message_id') or metadata.get('message_id')
115+
if chat_id and checkpoint_message_id and not chat_id.startswith(('local:', 'channel:')):
116+
await Chats.upsert_message_to_chat_by_id_and_message_id(
117+
chat_id,
118+
checkpoint_message_id,
119+
{'contextSummary': summary},
120+
)
84121

85122
log.info(
86-
'Compacted chat context for chat=%s message=%s dropped=%d kept=%d summary_chars=%d',
123+
'Compacted chat context for chat=%s checkpoint=%s response=%s dropped=%d kept=%d summary_chars=%d',
87124
chat_id,
88-
message_id,
125+
checkpoint_message_id,
126+
metadata.get('message_id'),
89127
len(compacted_messages),
90128
len(recent_messages),
91129
len(summary),
92130
)
93131

132+
if event_emitter:
133+
await event_emitter(
134+
{
135+
'type': 'context_compaction',
136+
'data': {
137+
'action': 'context_compaction',
138+
'description': 'Context compacted',
139+
'done': True,
140+
},
141+
}
142+
)
143+
94144
return recent_messages, summary, True
95145

96146

src/lib/components/chat/Chat.svelte

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
let generating = false;
165165
let dragged = false;
166166
let generationController = null;
167+
let contextCompactionToastId = null;
167168
168169
let chat = null;
169170
let tags = [];
@@ -498,6 +499,43 @@
498499
}
499500
};
500501
502+
const dismissContextCompactionToast = () => {
503+
if (contextCompactionToastId !== null) {
504+
toast.dismiss(contextCompactionToastId);
505+
contextCompactionToastId = null;
506+
}
507+
};
508+
509+
const handleContextCompactionStatus = (status) => {
510+
if (status?.action !== 'context_compaction') {
511+
return;
512+
}
513+
514+
if (status?.done) {
515+
if (contextCompactionToastId !== null) {
516+
if (status?.error) {
517+
toast.error($i18n.t('Context compaction failed'), {
518+
id: contextCompactionToastId,
519+
duration: 3000
520+
});
521+
} else {
522+
toast.success($i18n.t('Context compacted'), {
523+
id: contextCompactionToastId,
524+
duration: 1800
525+
});
526+
}
527+
contextCompactionToastId = null;
528+
}
529+
return;
530+
}
531+
532+
if (contextCompactionToastId === null) {
533+
contextCompactionToastId = toast.loading($i18n.t('Compacting context'), {
534+
duration: Infinity
535+
});
536+
}
537+
};
538+
501539
const chatEventHandler = async (event, cb) => {
502540
console.log(event);
503541
@@ -515,9 +553,12 @@
515553
} else {
516554
message.statusHistory = [data];
517555
}
556+
} else if (type === 'context_compaction') {
557+
handleContextCompactionStatus(data);
518558
} else if (type === 'chat:completion') {
519559
chatCompletionEventHandler(data, message, event.chat_id);
520560
} else if (type === 'chat:tasks:cancel') {
561+
dismissContextCompactionToast();
521562
if (event.message_id === history.currentId) {
522563
taskIds = null;
523564
// Set all response messages to done
@@ -895,6 +936,7 @@
895936
selectedFolderSubscribe();
896937
window.removeEventListener('message', onMessageHandler);
897938
$socket?.off('events', chatEventHandler);
939+
dismissContextCompactionToast();
898940
audioQueueInstance?.destroy();
899941
audioQueue.set(null);
900942
} catch (e) {

src/routes/+layout.svelte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,4 +1198,10 @@
11981198
richColors
11991199
position="top-right"
12001200
closeButton
1201+
toastOptions={{
1202+
classes: {
1203+
closeButton:
1204+
'!bg-white/80 !text-gray-500 !border-gray-200 hover:!bg-gray-50 hover:!text-gray-700 dark:!bg-gray-850 dark:!text-gray-400 dark:!border-gray-700 dark:hover:!bg-gray-800 dark:hover:!text-gray-200'
1205+
}
1206+
}}
12011207
/>

0 commit comments

Comments
 (0)