Skip to content

Commit 4936506

Browse files
committed
Refactor conversation handling and enhance conversation management logic
- Updated `ui.js` to replace `createConversation` with `activateEmptyOrCreateConversation`, streamlining conversation initialization. - Introduced `isConversationEmpty` and `dedupeEmptyConversations` functions in `conversations.js` to manage empty conversations more effectively. - Enhanced `loadStore` to deduplicate empty conversations and save the updated store when necessary, improving overall conversation management.
1 parent a123cbd commit 4936506

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

frontend/src/ui.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
saveHistory,
2020
} from "./utils/history.js";
2121
import {
22-
createConversation,
22+
activateEmptyOrCreateConversation,
2323
getActiveConversation,
2424
getConversationDiscipline,
2525
setConversationDiscipline,
@@ -187,8 +187,8 @@ export function init() {
187187
}
188188

189189
function finishConversationReset() {
190-
createConversation({ activate: true });
191-
sessionId = regenerateSessionId();
190+
const { created } = activateEmptyOrCreateConversation();
191+
sessionId = created ? regenerateSessionId() : getOrCreateSessionId();
192192
chatView.clearChat();
193193
chatView.showLanding();
194194
input.value = "";

frontend/src/utils/conversations.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,45 @@ function newId() {
6565
return crypto.randomUUID().replace(/-/g, "");
6666
}
6767

68+
/**
69+
* Conversa sem mensagem útil do usuário (ainda sem conteúdo real enviado).
70+
* @param {StoredConversation | { turns?: ConversationTurn[] } | null | undefined} conv
71+
*/
72+
export function isConversationEmpty(conv) {
73+
const turns = conv?.turns;
74+
if (!Array.isArray(turns) || !turns.length) return true;
75+
return !turns.some((t) => {
76+
if (t?.role !== "user") return false;
77+
let text = stripLeadingDisciplineCommand(String(t.text || "").trim());
78+
text = text.replace(/^\/(?:doc|content|reset|limpar)\b\s*/i, "").trim();
79+
return text.length > 0;
80+
});
81+
}
82+
83+
/**
84+
* @param {{ activeId: string | null, conversations: StoredConversation[] }} store
85+
*/
86+
function dedupeEmptyConversations(store) {
87+
const empties = store.conversations.filter(isConversationEmpty);
88+
if (empties.length <= 1) return store;
89+
90+
const activeEmpty = store.activeId && empties.find((c) => c.id === store.activeId);
91+
const keep =
92+
activeEmpty ||
93+
[...empties].sort((a, b) => (b.updatedAt || 0) - (a.updatedAt || 0))[0];
94+
if (!keep) return store;
95+
96+
return {
97+
...store,
98+
activeId: store.activeId && store.conversations.some((c) => c.id === store.activeId)
99+
? store.activeId
100+
: keep.id,
101+
conversations: store.conversations.filter(
102+
(c) => !isConversationEmpty(c) || c.id === keep.id,
103+
),
104+
};
105+
}
106+
68107
/**
69108
* @param {string} text
70109
*/
@@ -220,10 +259,16 @@ export function loadStore() {
220259
const conversations = Array.isArray(parsed?.conversations)
221260
? parsed.conversations.filter((c) => c && c.id).map(normalizeConversation)
222261
: [];
223-
return {
262+
const store = {
224263
activeId: typeof parsed?.activeId === "string" ? parsed.activeId : null,
225264
conversations,
226265
};
266+
const deduped = dedupeEmptyConversations(store);
267+
if (deduped.conversations.length !== store.conversations.length) {
268+
saveStore(deduped);
269+
return deduped;
270+
}
271+
return store;
227272
} catch {
228273
return emptyStore();
229274
}
@@ -277,6 +322,21 @@ export function createConversation(opts = {}) {
277322
return conv;
278323
}
279324

325+
/**
326+
* Ativa a conversa vazia existente ou cria uma nova (no máximo uma vazia por vez).
327+
* @returns {{ conversation: StoredConversation, created: boolean }}
328+
*/
329+
export function activateEmptyOrCreateConversation() {
330+
const store = loadStore();
331+
const existing = store.conversations.find(isConversationEmpty);
332+
if (existing) {
333+
store.activeId = existing.id;
334+
saveStore(store);
335+
return { conversation: existing, created: false };
336+
}
337+
return { conversation: createConversation({ activate: true }), created: true };
338+
}
339+
280340
/**
281341
* @param {string} id
282342
* @returns {StoredConversation | null}

0 commit comments

Comments
 (0)