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
This commit is contained in:
Russell Ballestrini 2025-07-03 18:14:44 -04:00
parent 185d57092b
commit 7b9a37ebea
2 changed files with 64 additions and 6 deletions

View file

@ -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();
}
}

View file

@ -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