Mark-Lasfar
commited on
Commit
·
cf6fa01
1
Parent(s):
bd1eb98
add chat
Browse files- static/js/chat.js +26 -11
static/js/chat.js
CHANGED
|
@@ -800,6 +800,7 @@ async function submitMessage() {
|
|
| 800 |
|
| 801 |
isRequestActive = true;
|
| 802 |
abortController = new AbortController();
|
|
|
|
| 803 |
|
| 804 |
try {
|
| 805 |
const response = await sendRequest(endpoint, payload ? JSON.stringify(payload) : formData, headers);
|
|
@@ -842,25 +843,27 @@ async function submitMessage() {
|
|
| 842 |
renderMarkdown(streamMsg);
|
| 843 |
streamMsg.dataset.done = '1';
|
| 844 |
}
|
|
|
|
| 845 |
if (!(await checkAuth())) {
|
| 846 |
conversationHistory.push({ role: 'assistant', content: responseText });
|
| 847 |
sessionStorage.setItem('conversationHistory', JSON.stringify(conversationHistory));
|
| 848 |
}
|
| 849 |
-
if (checkAuth() && response.headers.get('X-Conversation-ID')) {
|
| 850 |
-
currentConversationId = response.headers.get('X-Conversation-ID');
|
| 851 |
-
currentConversationTitle = response.headers.get('X-Conversation-Title') || 'Untitled Conversation';
|
| 852 |
-
if (uiElements.conversationTitle) uiElements.conversationTitle.textContent = currentConversationTitle;
|
| 853 |
-
history.pushState(null, '', `/chat/${currentConversationId}`);
|
| 854 |
-
await loadConversations();
|
| 855 |
-
}
|
| 856 |
finalizeRequest();
|
| 857 |
} catch (error) {
|
| 858 |
handleRequestError(error);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 859 |
}
|
| 860 |
}
|
| 861 |
|
| 862 |
let attemptCount = 0;
|
| 863 |
-
function addAttemptHistory(who, text) {
|
| 864 |
attemptCount++;
|
| 865 |
const container = document.createElement('div');
|
| 866 |
container.className = 'message-container';
|
|
@@ -876,17 +879,29 @@ function addAttemptHistory(who, text) {
|
|
| 876 |
prevBtn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor"><path d="M15 19l-7-7 7-7"></path></svg>';
|
| 877 |
prevBtn.title = 'Previous Attempt';
|
| 878 |
prevBtn.onclick = () => {
|
| 879 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 880 |
};
|
| 881 |
const nextBtn = document.createElement('button');
|
| 882 |
nextBtn.className = 'action-btn';
|
| 883 |
nextBtn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor"><path d="M9 5l7 7-7 7"></path></svg>';
|
| 884 |
nextBtn.title = 'Next Attempt';
|
| 885 |
nextBtn.onclick = () => {
|
| 886 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 887 |
};
|
| 888 |
historyActions.appendChild(prevBtn);
|
| 889 |
-
historyActions.appendChild(document.createTextNode(`Attempt ${
|
| 890 |
historyActions.appendChild(nextBtn);
|
| 891 |
|
| 892 |
container.appendChild(div);
|
|
|
|
| 800 |
|
| 801 |
isRequestActive = true;
|
| 802 |
abortController = new AbortController();
|
| 803 |
+
let attemptHistory = [];
|
| 804 |
|
| 805 |
try {
|
| 806 |
const response = await sendRequest(endpoint, payload ? JSON.stringify(payload) : formData, headers);
|
|
|
|
| 843 |
renderMarkdown(streamMsg);
|
| 844 |
streamMsg.dataset.done = '1';
|
| 845 |
}
|
| 846 |
+
attemptHistory.push({ role: 'assistant', content: responseText });
|
| 847 |
if (!(await checkAuth())) {
|
| 848 |
conversationHistory.push({ role: 'assistant', content: responseText });
|
| 849 |
sessionStorage.setItem('conversationHistory', JSON.stringify(conversationHistory));
|
| 850 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 851 |
finalizeRequest();
|
| 852 |
} catch (error) {
|
| 853 |
handleRequestError(error);
|
| 854 |
+
attemptHistory.push({ role: 'assistant', content: `Error: ${error.message || 'An error occurred'}` });
|
| 855 |
+
}
|
| 856 |
+
|
| 857 |
+
// تخزين تاريخ المحاولات
|
| 858 |
+
if (attemptHistory.length > 0) {
|
| 859 |
+
attemptHistory.forEach((attempt, index) => {
|
| 860 |
+
addAttemptHistory(attempt.role, attempt.content, index + 1);
|
| 861 |
+
});
|
| 862 |
}
|
| 863 |
}
|
| 864 |
|
| 865 |
let attemptCount = 0;
|
| 866 |
+
function addAttemptHistory(who, text, attemptNum) {
|
| 867 |
attemptCount++;
|
| 868 |
const container = document.createElement('div');
|
| 869 |
container.className = 'message-container';
|
|
|
|
| 879 |
prevBtn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor"><path d="M15 19l-7-7 7-7"></path></svg>';
|
| 880 |
prevBtn.title = 'Previous Attempt';
|
| 881 |
prevBtn.onclick = () => {
|
| 882 |
+
if (attemptNum > 1) {
|
| 883 |
+
const prevAttempt = attemptHistory[attemptNum - 2];
|
| 884 |
+
if (prevAttempt) {
|
| 885 |
+
streamMsg.dataset.text = prevAttempt.content;
|
| 886 |
+
renderMarkdown(streamMsg);
|
| 887 |
+
}
|
| 888 |
+
}
|
| 889 |
};
|
| 890 |
const nextBtn = document.createElement('button');
|
| 891 |
nextBtn.className = 'action-btn';
|
| 892 |
nextBtn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor"><path d="M9 5l7 7-7 7"></path></svg>';
|
| 893 |
nextBtn.title = 'Next Attempt';
|
| 894 |
nextBtn.onclick = () => {
|
| 895 |
+
if (attemptNum < attemptHistory.length) {
|
| 896 |
+
const nextAttempt = attemptHistory[attemptNum];
|
| 897 |
+
if (nextAttempt) {
|
| 898 |
+
streamMsg.dataset.text = nextAttempt.content;
|
| 899 |
+
renderMarkdown(streamMsg);
|
| 900 |
+
}
|
| 901 |
+
}
|
| 902 |
};
|
| 903 |
historyActions.appendChild(prevBtn);
|
| 904 |
+
historyActions.appendChild(document.createTextNode(`Attempt ${attemptNum}`));
|
| 905 |
historyActions.appendChild(nextBtn);
|
| 906 |
|
| 907 |
container.appendChild(div);
|