-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.js
More file actions
226 lines (186 loc) · 8.63 KB
/
ai.js
File metadata and controls
226 lines (186 loc) · 8.63 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
let conversationHistory = [];
document.addEventListener('DOMContentLoaded', () => {
const aiModeBtn = document.getElementById('ai-mode-btn');
const aiModalOverlay = document.getElementById('ai-modal-overlay');
const aiModalCloseBtn = document.getElementById('ai-modal-close-btn');
const aiSendBtn = document.getElementById('ai-send-btn');
const aiStopBtn = document.getElementById('ai-stop-btn');
const aiInput = document.getElementById('ai-input');
const aiInputContainer = document.getElementById('ai-input-container');
const aiChatContainer = document.getElementById('ai-chat-container');
const editor = document.getElementById('editor');
const aiActionsTemplate = document.getElementById('ai-actions-template');
const aiNewChatBtn = document.getElementById('ai-new-chat-btn');
let apiKey = localStorage.getItem('gemini-api-key');
let abortController = null;
aiModeBtn.addEventListener('click', () => {
aiModalOverlay.classList.remove('hidden');
});
aiModalCloseBtn.addEventListener('click', () => {
aiModalOverlay.classList.add('hidden');
});
const sendMessage = async () => {
const prompt = aiInput.value.trim();
if (prompt && !aiInput.disabled) {
appendMessage('user', prompt);
aiInput.value = '';
let fullPrompt = prompt;
if (prompt.includes('@code')) {
fullPrompt = prompt.replace('@code', `\n\n\`\`\`\n${editor.value}\n\`\`\`\n\n`);
}
conversationHistory.push({ role: 'user', parts: [{ text: fullPrompt }] });
await getAiResponse();
}
};
aiSendBtn.addEventListener('click', sendMessage);
aiInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});
aiStopBtn.addEventListener('click', () => {
if (abortController) {
abortController.abort();
if (window.toast) window.toast('AI response stopped', 'info');
}
});
async function getAiResponse() {
if (!apiKey) {
appendMessage('ai', 'API key is not set. Please set it in the settings.');
return;
}
abortController = new AbortController();
// UI Updates for streaming
aiInput.disabled = true;
aiInputContainer.classList.add('streaming');
aiSendBtn.classList.add('hidden');
aiStopBtn.classList.remove('hidden');
const aiMessage = appendMessage('ai', '', true);
try {
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:streamGenerateContent?alt=sse&key=${apiKey}`, {
method: 'POST',
signal: abortController.signal,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
contents: conversationHistory
})
});
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let accumulatedText = '';
aiMessage.innerHTML = '';
while (true) {
const { value, done } = 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 jsonString = line.substring(6);
try {
const data = JSON.parse(jsonString);
if (data.candidates && data.candidates[0].content.parts[0].text) {
const textPart = data.candidates[0].content.parts[0].text;
accumulatedText += textPart;
aiMessage.innerHTML = marked.parse(accumulatedText);
aiChatContainer.scrollTop = aiChatContainer.scrollHeight;
}
} catch (e) {
console.error("Failed to parse JSON from stream:", jsonString, e);
}
}
}
}
finalizeMessage(aiMessage, accumulatedText);
conversationHistory.push({ role: 'model', parts: [{ text: accumulatedText }] });
} catch (error) {
if (error.name === 'AbortError') {
aiMessage.innerHTML += '<p><em>Response cancelled by user.</em></p>';
} else {
console.error('Error fetching AI response:', error);
aiMessage.innerHTML = marked.parse('Sorry, I encountered an error. Please try again.');
}
} finally {
abortController = null;
aiInput.disabled = false;
aiInputContainer.classList.remove('streaming');
aiSendBtn.classList.remove('hidden');
aiStopBtn.classList.add('hidden');
aiInput.focus();
}
}
function appendMessage(sender, text, isLoading = false) {
const messageElement = document.createElement('div');
messageElement.classList.add('chat-message', `${sender}-message`);
if (isLoading) {
messageElement.innerHTML = '<div class="loading-spinner"></div>';
} else {
messageElement.innerHTML = marked.parse(text);
}
aiChatContainer.appendChild(messageElement);
aiChatContainer.scrollTop = aiChatContainer.scrollHeight;
return messageElement;
}
function finalizeMessage(messageElement, text) {
messageElement.innerHTML = marked.parse(text);
const codeBlocks = text.match(/```(\w+)?\n([\s\S]*?)```/g);
if (codeBlocks) {
const actionsContainer = aiActionsTemplate.cloneNode(true);
actionsContainer.removeAttribute('id');
actionsContainer.style.display = 'flex';
const replaceBtn = actionsContainer.querySelector('.replace-btn');
const replaceSaveBtn = actionsContainer.querySelector('.replace-save-btn');
const codeToInsert = codeBlocks.map(block => block.replace(/```(\w+)?\n|```/g, '')).join('\n');
replaceBtn.onclick = () => {
editor.value = codeToInsert;
editor.dispatchEvent(new Event('input', { bubbles: true }));
aiModalOverlay.classList.add('hidden');
if (window.toast) window.toast('Code replaced', 'success');
};
replaceSaveBtn.onclick = () => {
editor.value = codeToInsert;
editor.dispatchEvent(new Event('input', { bubbles: true }));
editor.dispatchEvent(new CustomEvent('save-content'));
aiModalOverlay.classList.add('hidden');
if (window.toast) window.toast('Code replaced & saved', 'success');
};
messageElement.appendChild(actionsContainer);
}
aiChatContainer.scrollTop = aiChatContainer.scrollHeight;
}
// Settings Modal Logic
const settingsBtn = document.getElementById('settings-btn');
const settingsModalOverlay = document.getElementById('settings-modal-overlay');
const settingsModalCloseBtn = document.getElementById('settings-modal-close-btn');
const apiKeyInput = document.getElementById('api-key-input');
const settingsSaveBtn = document.getElementById('settings-save-btn');
settingsBtn.addEventListener('click', () => {
apiKeyInput.value = localStorage.getItem('gemini-api-key') || '';
settingsModalOverlay.classList.remove('hidden');
});
settingsModalCloseBtn.addEventListener('click', () => {
settingsModalOverlay.classList.add('hidden');
});
settingsSaveBtn.addEventListener('click', () => {
apiKey = apiKeyInput.value.trim();
localStorage.setItem('gemini-api-key', apiKey);
settingsModalOverlay.classList.add('hidden');
if (window.toast) window.toast('API key saved', 'success');
});
aiNewChatBtn.addEventListener('click', () => {
conversationHistory = [];
aiChatContainer.style.transition = 'opacity 0.3s ease';
aiChatContainer.style.opacity = '0';
setTimeout(() => {
aiChatContainer.innerHTML = '';
aiChatContainer.style.opacity = '1';
if (window.toast) window.toast('Chat cleared', 'info');
}, 300);
});
});