From 7b9a37ebea49eacb76e2c4cc73f18966a33833b8 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Thu, 3 Jul 2025 18:14:44 -0400 Subject: [PATCH] fix: resolve JavaScript import errors in modal functionality - Fix fetchModelsFromEndpoints import path from ./chat.js to ./models.js - Fix loadConversationHistory and clearConversationHistory imports from ./chat.js to ./storage.js - Replace direct chatHistory access with proper async getChatHistory/updateChatHistory calls - Add sendMessageWithCustomHistory function to chat.js for modal integration --- src/chat.js | 57 ++++++++++++++++++++++++++++++++++++ src/uncloseai-embed-modal.js | 13 ++++---- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/src/chat.js b/src/chat.js index 82ad1f6..6f539c9 100644 --- a/src/chat.js +++ b/src/chat.js @@ -154,3 +154,60 @@ export function updateChatHistory(newHistory) { export function getChatHistory() { return chatHistory; } + +// Generator function to send a message with custom history (doesn't modify global chatHistory) +export async function* sendMessageWithCustomHistory(customHistory) { + // Dynamically determine the API URL based on the selected model. + const apiUrl = `${getSelectedModelEndpoint()}/chat/completions`; + + const response = await fetch(apiUrl, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${API_KEY}`, + }, + body: JSON.stringify({ + model: getSelectedModel(), + messages: customHistory, + stream: true, + }), + }); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + const reader = response.body.getReader(); + const decoder = new TextDecoder(); + + try { + while (true) { + const { done, value } = await reader.read(); + if (done) break; + + const chunk = decoder.decode(value); + const lines = chunk.split("\n"); + + for (const line of lines) { + if (line.startsWith("data: ")) { + const data = line.slice(6); + if (data === "[DONE]") { + return; + } + + try { + const parsed = JSON.parse(data); + const content = parsed.choices?.[0]?.delta?.content; + if (content) { + yield content; + } + } catch (error) { + // Skip malformed JSON + } + } + } + } + } finally { + reader.releaseLock(); + } +} diff --git a/src/uncloseai-embed-modal.js b/src/uncloseai-embed-modal.js index 2f06fdb..b7b5ef4 100644 --- a/src/uncloseai-embed-modal.js +++ b/src/uncloseai-embed-modal.js @@ -46,12 +46,12 @@ async function updateChatHistory(history) { } async function clearConversationHistory() { - const { clearConversationHistory } = await import("./chat.js"); + const { clearConversationHistory } = await import("./storage.js"); return clearConversationHistory(); } async function loadConversationHistory() { - const { loadConversationHistory } = await import("./chat.js"); + const { loadConversationHistory } = await import("./storage.js"); return loadConversationHistory(); } @@ -61,7 +61,7 @@ async function saveConversationHistory(history) { } async function fetchModelsFromEndpoints() { - const { fetchModelsFromEndpoints } = await import("./chat.js"); + const { fetchModelsFromEndpoints } = await import("./models.js"); return fetchModelsFromEndpoints(); } @@ -532,9 +532,10 @@ function addCodeBlockCopyButtons(element) { clearConversationHistory(); // Also clear the chat history in the chat.js module - const systemMsg = chatHistory.find((msg) => msg.role === "system"); - chatHistory.length = 0; // Clear array - if (systemMsg) chatHistory.push(systemMsg); + const currentHistory = await getChatHistory(); + const systemMsg = currentHistory.find((msg) => msg.role === "system"); + const newHistory = systemMsg ? [systemMsg] : []; + await updateChatHistory(newHistory); console.log("Chat history cleared, reset to system message only"); // Re-add intro message