(role, content, image, audio, targetChatId = null)
| 2456 | }, |
| 2457 | |
| 2458 | add(role, content, image, audio, targetChatId = null) { |
| 2459 | // If targetChatId is provided, add to that chat, otherwise use active chat |
| 2460 | const chat = targetChatId ? this.getChat(targetChatId) : this.activeChat(); |
| 2461 | if (!chat) return; |
| 2462 | |
| 2463 | const N = chat.history.length - 1; |
| 2464 | if (role === "thinking" || role === "reasoning") { |
| 2465 | let c = ""; |
| 2466 | const lines = content.split("\n"); |
| 2467 | lines.forEach((line) => { |
| 2468 | c += DOMPurify.sanitize(marked.parse(line)); |
| 2469 | }); |
| 2470 | chat.history.push({ role, content, html: c, image, audio }); |
| 2471 | } |
| 2472 | else if (chat.history.length && chat.history[N].role === role) { |
| 2473 | chat.history[N].content += content; |
| 2474 | chat.history[N].html = DOMPurify.sanitize( |
| 2475 | marked.parse(chat.history[N].content) |
| 2476 | ); |
| 2477 | if (image && image.length > 0) { |
| 2478 | chat.history[N].image = [...(chat.history[N].image || []), ...image]; |
| 2479 | } |
| 2480 | if (audio && audio.length > 0) { |
| 2481 | chat.history[N].audio = [...(chat.history[N].audio || []), ...audio]; |
| 2482 | } |
| 2483 | } else { |
| 2484 | let c = ""; |
| 2485 | const lines = content.split("\n"); |
| 2486 | lines.forEach((line) => { |
| 2487 | c += DOMPurify.sanitize(marked.parse(line)); |
| 2488 | }); |
| 2489 | chat.history.push({ |
| 2490 | role, |
| 2491 | content, |
| 2492 | html: c, |
| 2493 | image: image || [], |
| 2494 | audio: audio || [] |
| 2495 | }); |
| 2496 | |
| 2497 | if (role === "user" && chat.name === "New Chat" && content.trim()) { |
| 2498 | const name = content.trim().substring(0, 50); |
| 2499 | chat.name = name.length < content.trim().length ? name + "..." : name; |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | chat.updatedAt = Date.now(); |
| 2504 | |
| 2505 | const chatContainer = document.getElementById('chat'); |
| 2506 | if (chatContainer) { |
| 2507 | chatContainer.scrollTo({ |
| 2508 | top: chatContainer.scrollHeight, |
| 2509 | behavior: 'smooth' |
| 2510 | }); |
| 2511 | } |
| 2512 | if (role === "thinking" || role === "reasoning") { |
| 2513 | setTimeout(() => { |
| 2514 | if (typeof window.scrollThinkingBoxToBottom === 'function') { |
| 2515 | window.scrollThinkingBoxToBottom(); |
nothing calls this directly
no test coverage detected