refactor: titles

This commit is contained in:
cogwheel0
2025-09-25 21:15:47 +05:30
parent 0943621731
commit db0261ffed
6 changed files with 55 additions and 264 deletions

View File

@@ -219,6 +219,57 @@ StreamSubscription<String> attachUnifiedChunkedStreaming({
} catch (_) {}
}
bool refreshingSnapshot = false;
Future<void> refreshConversationSnapshot() async {
if (refreshingSnapshot) return;
final chatId = activeConversationId;
if (chatId == null || chatId.isEmpty) {
return;
}
if (api == null) return;
refreshingSnapshot = true;
try {
final conversation = await api.getConversation(chatId);
if (conversation.title.isNotEmpty && conversation.title != 'New Chat') {
onChatTitleUpdated?.call(conversation.title);
}
if (conversation.messages.isEmpty) {
return;
}
ChatMessage? foundAssistant;
for (final message in conversation.messages.reversed) {
if (message.role == 'assistant') {
foundAssistant = message;
break;
}
}
final assistant = foundAssistant;
if (assistant == null) {
return;
}
setFollowUps(assistant.id, assistant.followUps);
updateMessageById(assistant.id, (current) {
return current.copyWith(
followUps: List<String>.from(assistant.followUps),
statusHistory: assistant.statusHistory,
sources: assistant.sources,
metadata: {...?current.metadata, ...?assistant.metadata},
usage: assistant.usage,
);
});
} catch (_) {
// Best-effort refresh; ignore failures.
} finally {
refreshingSnapshot = false;
}
}
void channelLineHandlerFactory(String channel) {
void handler(dynamic line) {
try {
@@ -446,6 +497,8 @@ StreamSubscription<String> attachUnifiedChunkedStreaming({
);
} catch (_) {}
Future.microtask(refreshConversationSnapshot);
final msgs = getMessages();
if (msgs.isNotEmpty && msgs.last.role == 'assistant') {
final lastContent = msgs.last.content.trim();
@@ -897,6 +950,7 @@ StreamSubscription<String> attachUnifiedChunkedStreaming({
// If SSE-driven (no dynamic channel/background flow), finish now
if (!usingDynamicChannel && !isBackgroundFlow) {
finishStreaming();
Future.microtask(refreshConversationSnapshot);
}
socketWatchdog?.stop();
},
@@ -906,6 +960,7 @@ StreamSubscription<String> attachUnifiedChunkedStreaming({
} catch (_) {}
suppressSocketContent = false;
finishStreaming();
Future.microtask(refreshConversationSnapshot);
socketWatchdog?.stop();
},
);