-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (112 loc) · 3.74 KB
/
Copy pathscript.js
File metadata and controls
136 lines (112 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function parseMarkdown(markdown) {
return markdown
.replace(/^# (.*$)/gim, '<h1>$1</h1>')
.replace(/^## (.*$)/gim, '<h1>$1</h1>')
.replace(/^### (.*$)/gim, '<h1>$1</h1>')
.replace(/\*\*(.*?)\*\*/gim, '<b>$1</b>')
.replace(/\*(.*?)\*/gim, '<i>$1</i>')
.replace(/\n/g, '<br>');
}
const form = document.getElementById('chat-form');
const userInput = document.getElementById('user-input');
const chatBox = document.getElementById('chat-box');
const apiKey = document.getElementById('api-key');
const modelSelect = document.getElementById('model-select');
let messages = [];
let chatSessions = JSON.parse(localStorage.getItem('chatSessions')) || [];
const toggleHistoryBtn = document.getElementById('toggle-history');
const sidebar = document.getElementById('sidebar');
const historyList = document.getElementById('history-list');
function enterMessage(content, sender) {
const msg = document.createElement('div');
msg.className = sender === 'user' ? 'user-msg' : 'bot-msg';
chatBox.appendChild(msg);
msg.innerHTML = parseMarkdown(content);
chatBox.scrollTop = chatBox.scrollHeight;
}
async function streaming(model, apikey, messages) {
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apikey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model,
messages,
stream: true
})
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
let result = '';
const botDiv = document.createElement('div');
botDiv.className = 'bot-msg';
chatBox.appendChild(botDiv);
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
const chunk = decoder.decode(value, { stream: true });
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data:')) {
try {
const json = JSON.parse(line.slice(5));
const token = json.choices[0]?.delta?.content || '';
result += token;
botDiv.innerHTML = parseMarkdown(result);
chatBox.scrollTop = chatBox.scrollHeight;
} catch (e) {
console.warn('Invalid chunk:', line);
}
}
}
}
messages.push({ role: 'assistant', content: result });
localStorage.setItem('chatHistory', JSON.stringify(messages));
}
form.addEventListener('submit', async (e) => {
e.preventDefault();
const text = userInput.value;
const key = apiKey.value;
const model = modelSelect.value;
messages.push({ role: 'user', content: text });
enterMessage(text, 'user');
userInput.value = '';
await streaming(model, key, messages);
});
toggleHistoryBtn.addEventListener('click', () => {
sidebar.classList.toggle('open');
loadHistoryList();
});
window.addEventListener('beforeunload', () => {
if (messages.length > 0) {
chatSessions.push([...messages]);
localStorage.setItem('chatSessions', JSON.stringify(chatSessions));
}
});
function loadHistoryList() {
historyList.innerHTML = '';
const sessions = JSON.parse(localStorage.getItem('chatSessions')) || [];
sessions.forEach((session, idx) => {
const li = document.createElement('li');
li.textContent = `Chat #${idx + 1} `;
li.addEventListener('click', () => {
chatBox.innerHTML = '';
session.forEach(msg => enterMessage(msg.content, msg.role));
});
historyList.appendChild(li);
});
}
saved.forEach((msg, index) => {
if (msg.role === 'user') {
const li = document.createElement('li');
li.textContent = msg.content;
li.addEventListener('click', () => {
alert(`Message at index ${index}: ${msg.content}`);
});
historyList.appendChild(li);
}
});