fix: resolve localStorage Promise handling in modal dynamic import wrappers
- Fix loadConversationHistory() calls missing await (was returning Promise instead of data)
- Fix saveConversationHistory() calls missing await in 4 locations
- Fix clearConversationHistory() calls missing await
- Fix getChatHistory() calls missing await in 6 locations
- Fix updateChatHistory() calls missing await in 5 locations
All dynamic import wrapper functions are now properly awaited, resolving:
- "History length: undefined" error (Promise.length was undefined)
- "Raw history from localStorage: Promise { <state>: "pending" }" console output
- localStorage operations not completing before UI updates
This fixes the localStorage breakage caused by async wrapper functions not being awaited.
This commit is contained in:
parent
2315e476c8
commit
f3531d1591
3 changed files with 51 additions and 17 deletions
BIN
src/.widget-library.js.swp
Normal file
BIN
src/.widget-library.js.swp
Normal file
Binary file not shown.
|
|
@ -529,7 +529,7 @@ function addCodeBlockCopyButtons(element) {
|
|||
action: async () => {
|
||||
if (confirm(getUIText("clearChatConfirm"))) {
|
||||
chatBox.innerHTML = "";
|
||||
clearConversationHistory();
|
||||
await clearConversationHistory();
|
||||
|
||||
// Also clear the chat history in the chat.js module
|
||||
const currentHistory = await getChatHistory();
|
||||
|
|
@ -955,7 +955,7 @@ function addCodeBlockCopyButtons(element) {
|
|||
action: async () => {
|
||||
try {
|
||||
const { getChatHistory } = await import("./chat.js");
|
||||
const history = getChatHistory();
|
||||
const history = await getChatHistory();
|
||||
let rawContent = "";
|
||||
history.forEach((msg) => {
|
||||
if (msg.role === "user") {
|
||||
|
|
@ -976,7 +976,7 @@ function addCodeBlockCopyButtons(element) {
|
|||
action: async () => {
|
||||
try {
|
||||
const { getChatHistory } = await import("./chat.js");
|
||||
const history = getChatHistory();
|
||||
const history = await getChatHistory();
|
||||
let htmlContent =
|
||||
'<div style="font-family: system-ui, -apple-system, sans-serif;">';
|
||||
history.forEach((msg) => {
|
||||
|
|
@ -1241,14 +1241,14 @@ function addCodeBlockCopyButtons(element) {
|
|||
userDeleteBtn.onmouseleave = () => {
|
||||
userDeleteBtn.style.background = colors.actionBg;
|
||||
};
|
||||
userDeleteBtn.onclick = () => {
|
||||
userDeleteBtn.onclick = async () => {
|
||||
// Remove from chat history
|
||||
const currentHistory = getChatHistory();
|
||||
const currentHistory = await getChatHistory();
|
||||
const updatedHistory = currentHistory.filter(
|
||||
(msg) => !(msg.role === "user" && msg.content === message),
|
||||
);
|
||||
updateChatHistory(updatedHistory);
|
||||
saveConversationHistory(updatedHistory);
|
||||
await updateChatHistory(updatedHistory);
|
||||
await saveConversationHistory(updatedHistory);
|
||||
|
||||
userMsg.remove();
|
||||
};
|
||||
|
|
@ -1285,12 +1285,12 @@ function addCodeBlockCopyButtons(element) {
|
|||
|
||||
// Add the AI response to chat history manually since sendMessage generator doesn't do it
|
||||
const { getChatHistory, updateChatHistory } = await import("./chat.js");
|
||||
const currentHistory = getChatHistory();
|
||||
const currentHistory = await getChatHistory();
|
||||
currentHistory.push({ role: "assistant", content: response });
|
||||
updateChatHistory(currentHistory);
|
||||
await updateChatHistory(currentHistory);
|
||||
|
||||
// Save conversation history after successful response
|
||||
saveConversationHistory(currentHistory);
|
||||
await saveConversationHistory(currentHistory);
|
||||
console.log(
|
||||
"Conversation history saved:",
|
||||
currentHistory.length,
|
||||
|
|
@ -1605,7 +1605,7 @@ function addCodeBlockCopyButtons(element) {
|
|||
// Load conversation history
|
||||
const loadHistory = async () => {
|
||||
try {
|
||||
const history = loadConversationHistory();
|
||||
const history = await loadConversationHistory();
|
||||
console.log("Raw history from localStorage:", history);
|
||||
console.log("History length:", history ? history.length : 0);
|
||||
if (history && history.length > 0) {
|
||||
|
|
@ -1636,7 +1636,7 @@ You have complete knowledge of this page content and can reference any details,
|
|||
];
|
||||
|
||||
// Update the chat.js module's history
|
||||
updateChatHistory(newHistory);
|
||||
await updateChatHistory(newHistory);
|
||||
console.log(
|
||||
"Chat history synced with loaded data:",
|
||||
newHistory.length,
|
||||
|
|
@ -1784,10 +1784,10 @@ You have complete knowledge of this page content and can reference any details,
|
|||
};
|
||||
userDeleteBtn.onclick = async () => {
|
||||
// Remove from chat history by index
|
||||
const currentHistory = loadConversationHistory();
|
||||
const currentHistory = await loadConversationHistory();
|
||||
if (index >= 0 && index < currentHistory.length) {
|
||||
currentHistory.splice(index, 1);
|
||||
saveConversationHistory(currentHistory);
|
||||
await saveConversationHistory(currentHistory);
|
||||
|
||||
// Update the chat.js module history to match localStorage
|
||||
const { updateChatHistory } = await import("./chat.js");
|
||||
|
|
@ -1796,7 +1796,7 @@ You have complete knowledge of this page content and can reference any details,
|
|||
{ role: "system", content: getSystemMessage() },
|
||||
...currentHistory,
|
||||
];
|
||||
updateChatHistory(newHistory);
|
||||
await updateChatHistory(newHistory);
|
||||
}
|
||||
|
||||
userMsg.remove();
|
||||
|
|
@ -2096,7 +2096,7 @@ You have complete knowledge of this page content and can reference any details,
|
|||
];
|
||||
|
||||
// Update the chat.js module's history
|
||||
updateChatHistory(newHistory);
|
||||
await updateChatHistory(newHistory);
|
||||
console.log("Added intro message to chat history");
|
||||
|
||||
// Add TTS and delete buttons to intro message
|
||||
|
|
@ -2263,7 +2263,7 @@ You have complete knowledge of this page content and can reference any details,
|
|||
introMsg.appendChild(messageActions);
|
||||
|
||||
// Save this intro as part of the conversation
|
||||
saveConversationHistory(getChatHistory());
|
||||
await saveConversationHistory(await getChatHistory());
|
||||
} catch (error) {
|
||||
console.error("Failed to generate contextual intro:", error);
|
||||
// Fallback to basic intro
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue