311 |
312 | ${item.role == "assistant" ? gpt_image : user_image}
313 | ${
314 | item.role == "assistant"
315 | ? ``
316 | : ``
317 | }
318 |
319 |
320 | ${
321 | item.role == "assistant"
322 | ? markdown.render(item.content)
323 | : item.content
324 | }
325 |
326 |
327 | `;
328 | }
329 |
330 | document.querySelectorAll(`code`).forEach((el) => {
331 | hljs.highlightElement(el);
332 | });
333 |
334 | message_box.scrollTo({ top: message_box.scrollHeight, behavior: "smooth" });
335 |
336 | setTimeout(() => {
337 | message_box.scrollTop = message_box.scrollHeight;
338 | }, 500);
339 | };
340 |
341 | const get_conversation = async (conversation_id) => {
342 | let conversation = await JSON.parse(
343 | localStorage.getItem(`conversation:${conversation_id}`)
344 | );
345 | return conversation.items;
346 | };
347 |
348 | const add_conversation = async (conversation_id, title) => {
349 | if (localStorage.getItem(`conversation:${conversation_id}`) == null) {
350 | localStorage.setItem(
351 | `conversation:${conversation_id}`,
352 | JSON.stringify({
353 | id: conversation_id,
354 | title: title,
355 | items: [],
356 | })
357 | );
358 | }
359 | };
360 |
361 | const add_message = async (conversation_id, role, content) => {
362 | before_adding = JSON.parse(
363 | localStorage.getItem(`conversation:${conversation_id}`)
364 | );
365 |
366 | before_adding.items.push({
367 | role: role,
368 | content: content,
369 | });
370 |
371 | localStorage.setItem(
372 | `conversation:${conversation_id}`,
373 | JSON.stringify(before_adding)
374 | ); // update conversation
375 | };
376 |
377 | const load_conversations = async (limit, offset, loader) => {
378 | //console.log(loader);
379 | //if (loader === undefined) box_conversations.appendChild(spinner);
380 |
381 | let conversations = [];
382 | for (let i = 0; i < localStorage.length; i++) {
383 | if (localStorage.key(i).startsWith("conversation:")) {
384 | let conversation = localStorage.getItem(localStorage.key(i));
385 | conversations.push(JSON.parse(conversation));
386 | }
387 | }
388 |
389 | //if (loader === undefined) spinner.parentNode.removeChild(spinner)
390 | await clear_conversations();
391 |
392 | for (conversation of conversations) {
393 | box_conversations.innerHTML += `
394 |