chat-ui.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. class ChatUI {
  2. constructor() {
  3. this.messageContainer = document.getElementById("chat-messages");
  4. this.input = document.getElementById("chat-input");
  5. this.sendButton = document.getElementById("send-button");
  6. this.promptButton = document.getElementById("prompt-button");
  7. this.promptPanel = document.getElementById("prompt-panel");
  8. // 确保AI服务已经初始化
  9. if (!window.aiService) {
  10. throw new Error("AI Service not initialized");
  11. }
  12. this.aiService = window.aiService;
  13. this.typingSpeed = 50;
  14. this.inputWrapper = document.querySelector(".input-wrapper");
  15. // 先添加过渡动画类
  16. this.input.classList.add("input-transition");
  17. // 设置初始高度
  18. this.input.style.height = "40px";
  19. // 获取总结按钮
  20. this.summarizeButton = document.querySelector(".summarize-button");
  21. // 初始化其他组件
  22. this.setupEventListeners();
  23. this.init();
  24. // 使用微任务确保DOM更新后再调整高度
  25. Promise.resolve().then(() => {
  26. // 第一帧:等待过渡动画类生效
  27. requestAnimationFrame(() => {
  28. // 第二帧:执行高度调整
  29. requestAnimationFrame(() => {
  30. this.adjustInputHeight();
  31. // 触发一次输入框focus以确保正确的高度
  32. this.input.focus();
  33. });
  34. });
  35. });
  36. // 添加新消息指示器
  37. this.createNewMessageIndicator();
  38. // 监听滚动事件
  39. this.messageContainer.addEventListener(
  40. "scroll",
  41. this.handleScroll.bind(this)
  42. );
  43. // 创建打断按钮
  44. this.stopButton = document.createElement("button");
  45. this.stopButton.className = "stop-button";
  46. this.stopButton.innerHTML = `
  47. <svg viewBox="0 0 24 24" fill="currentColor">
  48. <path d="M6 6h12v12H6z"/>
  49. </svg>
  50. <span>停止生成</span>
  51. `;
  52. // 将打断按钮添加到输入按钮区域
  53. document.querySelector(".input-buttons").appendChild(this.stopButton);
  54. // 添加打断按钮事件监听
  55. this.stopButton.addEventListener("click", () => this.handleStop());
  56. this.isTyping = false; // 添加打字状态标记
  57. this.loadingDiv = null; // 添加加载动画的引用
  58. // 初始化页面信息卡片
  59. this.initPageInfoCard();
  60. }
  61. async init() {
  62. try {
  63. await this.aiService.init();
  64. await this.checkApiKey();
  65. } catch (error) {
  66. console.error("ChatUI initialization failed:", error);
  67. this.addMessage("初始化失败,请刷新页面重试。", "assistant");
  68. }
  69. }
  70. setupEventListeners() {
  71. // 发送消息
  72. this.sendButton.addEventListener("click", () => this.handleSend());
  73. this.input.addEventListener("keydown", (e) => {
  74. if (e.key === "Enter" && !e.shiftKey) {
  75. e.preventDefault();
  76. this.handleSend();
  77. }
  78. });
  79. // 自动调整输入框高度
  80. this.input.addEventListener("input", () => this.adjustInputHeight());
  81. // 添加focus事件监听,确保在获得焦点时高度正确
  82. this.input.addEventListener("focus", () => {
  83. requestAnimationFrame(() => this.adjustInputHeight());
  84. });
  85. // 快捷指令面板
  86. this.promptButton.addEventListener("click", () => this.togglePromptPanel());
  87. document.querySelector(".close-prompt").addEventListener("click", () => {
  88. this.promptPanel.classList.remove("show");
  89. });
  90. // 点击快捷指令
  91. document.querySelectorAll(".prompt-item").forEach((item) => {
  92. item.addEventListener("click", () => {
  93. // 获取文本内容并处理格式
  94. const text = item.textContent
  95. .trim() // 移除首尾空白
  96. .replace(/\s+/g, " ") // 将多个空白字符替换为单个空格
  97. .replace(/[\n\r]+/g, ""); // 移除所有换行符
  98. // 设置到输入框
  99. this.input.value = text;
  100. this.promptPanel.classList.remove("show");
  101. // 调整输入框高度并聚焦
  102. this.adjustInputHeight();
  103. this.input.focus();
  104. });
  105. });
  106. // 点击外部关闭快捷指令面板
  107. document.addEventListener("click", (e) => {
  108. if (
  109. !this.promptPanel.contains(e.target) &&
  110. !this.promptButton.contains(e.target)
  111. ) {
  112. this.promptPanel.classList.remove("show");
  113. }
  114. });
  115. // 添加总结按钮点击事件
  116. if (this.summarizeButton) {
  117. this.summarizeButton.addEventListener("click", () => {
  118. this.handleSummarize();
  119. });
  120. }
  121. // 工具箱面板
  122. const toolsButton = document.getElementById("tools-button");
  123. const toolsPanel = document.getElementById("tools-panel");
  124. const closeTools = document.querySelector(".close-tools");
  125. toolsButton.addEventListener("click", () => {
  126. toolsPanel.classList.toggle("show");
  127. // 关闭其他面板
  128. this.promptPanel.classList.remove("show");
  129. });
  130. closeTools.addEventListener("click", () => {
  131. toolsPanel.classList.remove("show");
  132. });
  133. // 点击外部关闭工具箱面板
  134. document.addEventListener("click", (e) => {
  135. if (!toolsPanel.contains(e.target) && !toolsButton.contains(e.target)) {
  136. toolsPanel.classList.remove("show");
  137. }
  138. });
  139. // 文件上传功能
  140. const uploadButton = document.getElementById("upload-button");
  141. const fileInput = document.getElementById("file-input");
  142. uploadButton.addEventListener("click", () => {
  143. fileInput.click();
  144. });
  145. fileInput.addEventListener("change", (e) => {
  146. const files = Array.from(e.target.files);
  147. if (files.length > 0) {
  148. // 处理文件上传
  149. this.handleFileUpload(files);
  150. }
  151. // 清空文件输入框,以便可以重复选择同一文件
  152. fileInput.value = "";
  153. });
  154. }
  155. async handleSend() {
  156. const message = this.input.value.trim();
  157. if (!message) return;
  158. // 处理命令
  159. if (message.startsWith("/setapi ")) {
  160. const apiKey = message.substring(8).trim();
  161. await this.aiService.setApiKey(apiKey);
  162. // 根据是否使用默认密钥显示不同消息
  163. if (apiKey === CONFIG.AI_API.DEFAULT_API_KEY) {
  164. this.addMessage("已恢复使用默认API密钥", "assistant", false);
  165. } else {
  166. this.addMessage("自定义API密钥已设置", "assistant", false);
  167. }
  168. this.input.value = "";
  169. return;
  170. }
  171. // 添加用户消息
  172. this.addMessage(message, "user", false);
  173. // 更新上下文
  174. this.aiService.updateContext(message, "user");
  175. // 清空输入框
  176. this.input.value = "";
  177. this.adjustInputHeight();
  178. try {
  179. this.stopButton.classList.add("show");
  180. this.setInputState(true); // 禁用输入框
  181. this.loadingDiv = this.createLoadingMessage();
  182. const response = await this.aiService.sendMessage(message);
  183. if (this.loadingDiv) {
  184. this.loadingDiv.remove();
  185. this.loadingDiv = null;
  186. }
  187. if (!response) return;
  188. this.isTyping = false;
  189. await this.addMessage(response, "assistant", true);
  190. this.stopButton.classList.remove("show");
  191. this.setInputState(false); // 这里会自动聚焦输入框
  192. this.aiService.updateContext(response, "assistant");
  193. } catch (error) {
  194. if (this.loadingDiv) {
  195. this.loadingDiv.remove();
  196. this.loadingDiv = null;
  197. }
  198. this.stopButton.classList.remove("show");
  199. this.setInputState(false); // 这里也会自动聚焦输入框
  200. this.isTyping = false;
  201. if (error.message === "REQUEST_ABORTED") {
  202. return;
  203. }
  204. this.addMessage("抱歉,发生了一些错误,请稍后重试。", "assistant", false);
  205. console.error("AI response error:", error);
  206. }
  207. }
  208. /**
  209. * 添加消息到聊天界面
  210. * @param {string} content 消息内容
  211. * @param {string} type 消息类型(user/assistant)
  212. * @param {boolean} typing 是否使用打字效果
  213. * @param {boolean} isInterrupted 是否是中断消息
  214. */
  215. async addMessage(
  216. content,
  217. type,
  218. typing = type === "assistant",
  219. isInterrupted = false
  220. ) {
  221. const messageDiv = document.createElement("div");
  222. messageDiv.className = `message ${type}`;
  223. const messageContent = document.createElement("div");
  224. messageContent.className = "message-content";
  225. if (isInterrupted) {
  226. messageContent.classList.add("interrupted");
  227. }
  228. const paragraph = document.createElement("p");
  229. messageContent.appendChild(paragraph);
  230. // 创建操作栏(时间戳和复制按钮)
  231. const actionsDiv = document.createElement("div");
  232. actionsDiv.className = "message-actions";
  233. // 只为非中断的AI消息添加复制按钮
  234. if (type === "assistant" && !isInterrupted) {
  235. const copyButton = this.createCopyButton(content);
  236. actionsDiv.appendChild(copyButton);
  237. }
  238. // 添加时间戳
  239. const timestamp = document.createElement("div");
  240. timestamp.className = "message-timestamp";
  241. timestamp.textContent = this.formatTime(new Date());
  242. actionsDiv.appendChild(timestamp);
  243. messageContent.appendChild(actionsDiv);
  244. messageDiv.appendChild(messageContent);
  245. this.messageContainer.appendChild(messageDiv);
  246. if (typing) {
  247. messageContent.classList.add("typing");
  248. await this.typeMessage(paragraph, content);
  249. messageContent.classList.remove("typing");
  250. } else {
  251. paragraph.innerHTML = this.formatMessage(content);
  252. }
  253. const { scrollTop, scrollHeight, clientHeight } = this.messageContainer;
  254. const wasAtBottom = scrollHeight - scrollTop - clientHeight < 100;
  255. if (wasAtBottom) {
  256. this.scrollToBottom();
  257. } else {
  258. this.newMessageIndicator.classList.add("show");
  259. }
  260. }
  261. /**
  262. * 实现打字机效果
  263. * @param {HTMLElement} element 要添加文字的元素
  264. * @param {string} text 要显示的文字
  265. */
  266. async typeMessage(element, text) {
  267. let index = 0;
  268. const rawText = text;
  269. const tempDiv = document.createElement("div");
  270. this.isTyping = true; // 开始打字
  271. return new Promise((resolve) => {
  272. const type = () => {
  273. // 检查是否被中断
  274. if (!this.isTyping) {
  275. resolve();
  276. return;
  277. }
  278. if (index < rawText.length) {
  279. const currentText = rawText.substring(0, index + 1);
  280. tempDiv.innerHTML = this.formatMessage(currentText);
  281. element.innerHTML = tempDiv.innerHTML;
  282. index++;
  283. this.scrollToBottom();
  284. setTimeout(type, this.typingSpeed);
  285. } else {
  286. this.isTyping = false; // 打字结束
  287. resolve();
  288. }
  289. };
  290. type();
  291. });
  292. }
  293. adjustInputHeight() {
  294. const scrollPos = this.input.scrollTop;
  295. // 获取当前高度
  296. const currentHeight = this.input.style.height;
  297. // 重置高度
  298. this.input.style.height = "auto";
  299. // 计算新高度
  300. const newHeight = Math.min(this.input.scrollHeight, 120);
  301. // 如果高度有变化才设置
  302. if (currentHeight !== `${newHeight}px`) {
  303. this.input.style.height = `${newHeight}px`;
  304. }
  305. // 恢复滚动位置
  306. this.input.scrollTop = scrollPos;
  307. }
  308. scrollToBottom() {
  309. this.messageContainer.scrollTop = this.messageContainer.scrollHeight;
  310. }
  311. togglePromptPanel() {
  312. this.promptPanel.classList.toggle("show");
  313. }
  314. escapeHtml(html) {
  315. const div = document.createElement("div");
  316. div.textContent = html;
  317. return div.innerHTML;
  318. }
  319. async checkApiKey() {
  320. // 只有当没有任何API密钥时才显示提示
  321. if (!this.aiService.apiKey) {
  322. this.addMessage(
  323. "请先设置DeepSeek API密钥。输入格式:/setapi YOUR_API_KEY",
  324. "assistant"
  325. );
  326. }
  327. }
  328. /**
  329. * 创建加载动画消息
  330. */
  331. createLoadingMessage() {
  332. const loadingDiv = document.createElement("div");
  333. loadingDiv.className = "message assistant";
  334. loadingDiv.innerHTML = `
  335. <div class="message-content loading">
  336. <div class="typing-indicator">
  337. <span></span>
  338. <span></span>
  339. <span></span>
  340. </div>
  341. </div>
  342. `;
  343. this.messageContainer.appendChild(loadingDiv);
  344. this.scrollToBottom();
  345. return loadingDiv;
  346. }
  347. /**
  348. * 格式化时间
  349. */
  350. formatTime(date) {
  351. const hours = date.getHours().toString().padStart(2, "0");
  352. const minutes = date.getMinutes().toString().padStart(2, "0");
  353. return `${hours}:${minutes}`;
  354. }
  355. createNewMessageIndicator() {
  356. this.newMessageIndicator = document.createElement("div");
  357. this.newMessageIndicator.className = "new-messages-indicator";
  358. this.newMessageIndicator.textContent = "新消息";
  359. this.newMessageIndicator.addEventListener("click", () => {
  360. this.scrollToBottom();
  361. });
  362. document
  363. .querySelector(".chat-container")
  364. .appendChild(this.newMessageIndicator);
  365. }
  366. handleScroll() {
  367. const { scrollTop, scrollHeight, clientHeight } = this.messageContainer;
  368. const isNearBottom = scrollHeight - scrollTop - clientHeight < 100;
  369. if (isNearBottom) {
  370. this.newMessageIndicator.classList.remove("show");
  371. }
  372. }
  373. /**
  374. * 格式化消息内容
  375. * @param {string} text 原始文本
  376. * @returns {string} 格式化后的HTML
  377. */
  378. formatMessage(text) {
  379. if (!text) return "";
  380. return (
  381. text
  382. // 处理标题 (h1 ~ h6)
  383. .replace(/^#{1,6}\s+(.+)$/gm, (match, content) => {
  384. const level = match.trim().split("#").length - 1;
  385. return `<h${level}>${content.trim()}</h${level}>`;
  386. })
  387. // 处理换行
  388. .replace(/\n/g, "<br>")
  389. // 处理连续空格
  390. .replace(/ {2,}/g, (match) => "&nbsp;".repeat(match.length))
  391. // 处理代码块
  392. .replace(
  393. /```([\s\S]*?)```/g,
  394. (match, code) =>
  395. `<pre><code>${this.escapeHtml(code.trim())}</code></pre>`
  396. )
  397. // 处理行内代码
  398. .replace(
  399. /`([^`]+)`/g,
  400. (match, code) => `<code>${this.escapeHtml(code)}</code>`
  401. )
  402. // 处理粗体
  403. .replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>")
  404. // 处理斜体
  405. .replace(/\*(.*?)\*/g, "<em>$1</em>")
  406. // 处理链接
  407. .replace(
  408. /\[([^\]]+)\]\(([^)]+)\)/g,
  409. '<a href="$2" target="_blank">$1</a>'
  410. )
  411. // 处理无序列表
  412. .replace(/^[*-]\s+(.+)$/gm, "<li>$1</li>")
  413. .replace(/(<li>.*<\/li>)/gs, "<ul>$1</ul>")
  414. // 处理有序列表
  415. .replace(/^\d+\.\s+(.+)$/gm, "<li>$1</li>")
  416. .replace(/(<li>.*<\/li>)/gs, "<ol>$1</ol>")
  417. // 处理分隔线
  418. .replace(/^---+$/gm, "<hr>")
  419. // 处理引用
  420. .replace(/^>\s+(.+)$/gm, "<blockquote>$1</blockquote>")
  421. );
  422. }
  423. /**
  424. * 创建复制按钮
  425. * @param {string} content 要复制的内容
  426. * @returns {HTMLElement} 复制按钮元素
  427. */
  428. createCopyButton(content) {
  429. const button = document.createElement("button");
  430. button.className = "copy-button";
  431. button.innerHTML = `
  432. <svg viewBox="0 0 24 24" fill="currentColor">
  433. <path d="M16 1H4C2.9 1 2 1.9 2 3v14h2V3h12V1zm3 4H8C6.9 5 6 5.9 6 7v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/>
  434. </svg>
  435. <span>复制</span>
  436. `;
  437. button.addEventListener("click", async () => {
  438. try {
  439. // 直接使用传入的原始内容
  440. await this.copyToClipboard(content);
  441. // 显示复制成功状态
  442. button.classList.add("copied");
  443. button.innerHTML = `
  444. <svg viewBox="0 0 24 24" fill="currentColor">
  445. <path d="M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z"/>
  446. </svg>
  447. <span>已复制</span>
  448. `;
  449. // 2秒后恢复原始状态
  450. setTimeout(() => {
  451. button.classList.remove("copied");
  452. button.innerHTML = `
  453. <svg viewBox="0 0 24 24" fill="currentColor">
  454. <path d="M16 1H4C2.9 1 2 1.9 2 3v14h2V3h12V1zm3 4H8C6.9 5 6 5.9 6 7v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/>
  455. </svg>
  456. <span>复制</span>
  457. `;
  458. }, 2000);
  459. } catch (err) {
  460. console.error("Failed to copy text:", err);
  461. }
  462. });
  463. return button;
  464. }
  465. /**
  466. * 复制文本到剪贴板
  467. * @param {string} text 要复制的文本
  468. */
  469. async copyToClipboard(text) {
  470. try {
  471. // 通过postMessage发送复制请求到content script
  472. window.parent.postMessage(
  473. {
  474. type: "COPY_TO_CLIPBOARD",
  475. text: text,
  476. },
  477. "*"
  478. );
  479. return true;
  480. } catch (err) {
  481. console.error("Failed to copy text:", err);
  482. throw err;
  483. }
  484. }
  485. // 处理打断请求
  486. async handleStop() {
  487. try {
  488. this.aiService.abortRequest();
  489. if (this.isTyping) {
  490. this.isTyping = false;
  491. }
  492. if (this.loadingDiv) {
  493. this.loadingDiv.remove();
  494. this.loadingDiv = null;
  495. }
  496. this.stopButton.classList.remove("show");
  497. this.setInputState(false); // 中断后也自动聚焦输入框
  498. this.addMessage("用户手动停止生成", "assistant", false, true);
  499. } catch (error) {
  500. console.error("Failed to stop generation:", error);
  501. }
  502. }
  503. // 设置输入框状态
  504. setInputState(isGenerating) {
  505. this.input.disabled = isGenerating;
  506. this.input.placeholder = isGenerating ? "回复生成中..." : "输入消息...";
  507. if (isGenerating) {
  508. this.inputWrapper.classList.add("generating");
  509. } else {
  510. this.inputWrapper.classList.remove("generating");
  511. // AI回复完成后,自动聚焦到输入框
  512. this.input.focus();
  513. }
  514. }
  515. /**
  516. * 初始化页面信息卡片
  517. */
  518. initPageInfoCard() {
  519. // 从父窗口获取页面信息
  520. window.addEventListener("message", (event) => {
  521. if (event.data.type === "PAGE_INFO") {
  522. const pageInfo = event.data.pageInfo;
  523. this.updatePageInfoCard(pageInfo);
  524. }
  525. });
  526. }
  527. updatePageInfoCard(pageInfo) {
  528. // 更新卡片内容
  529. const favicon = document.querySelector(".page-favicon");
  530. const title = document.querySelector(".page-title");
  531. // 如果favicon发生变化,更新图标
  532. if (favicon.src !== pageInfo.favicon) {
  533. favicon.src = pageInfo.favicon;
  534. favicon.onerror = () => {
  535. // 如果图标加载失败,使用默认图标
  536. favicon.src = chrome.runtime.getURL("images/icon16.png");
  537. };
  538. }
  539. // 如果标题发生变化,更新标题
  540. if (title.textContent !== pageInfo.title) {
  541. title.textContent = pageInfo.title;
  542. }
  543. }
  544. /**
  545. * 获取页面favicon
  546. * @returns {string} favicon URL
  547. */
  548. getPageFavicon() {
  549. // 尝试获取页面favicon
  550. const iconLink = document.querySelector('link[rel*="icon"]');
  551. if (iconLink) return iconLink.href;
  552. // 如果没有找到,返回网站根目录的favicon.ico
  553. const url = new URL(window.location.href);
  554. return `${url.protocol}//${url.hostname}/favicon.ico`;
  555. }
  556. /**
  557. * 处理总结请求
  558. */
  559. async handleSummarize() {
  560. try {
  561. // 显示加载状态
  562. this.setInputState(true);
  563. this.loadingDiv = this.createLoadingMessage();
  564. // 通过postMessage请求页面分析结果
  565. const pageInfo = await new Promise((resolve) => {
  566. // 创建一次性消息监听器
  567. const messageHandler = (event) => {
  568. if (event.data.type === "PAGE_ANALYSIS_RESULT") {
  569. window.removeEventListener("message", messageHandler);
  570. resolve(event.data.pageInfo);
  571. }
  572. };
  573. // 添加消息监听
  574. window.addEventListener("message", messageHandler);
  575. // 发送分析请求到父页面
  576. window.parent.postMessage({ type: "ANALYZE_PAGE" }, "*");
  577. });
  578. // 构建提示词
  579. const prompt = this.aiService.getSummaryPrompt(pageInfo);
  580. // 发送分析请求
  581. const response = await this.aiService.sendMessage(prompt);
  582. // 显示分析结果
  583. if (this.loadingDiv) {
  584. this.loadingDiv.remove();
  585. this.loadingDiv = null;
  586. }
  587. await this.addMessage(response, "assistant", true);
  588. this.setInputState(false);
  589. } catch (error) {
  590. console.error("Failed to summarize page:", error);
  591. this.addMessage(
  592. "抱歉,页面总结过程中出现错误,请稍后重试。",
  593. "assistant",
  594. false
  595. );
  596. this.setInputState(false);
  597. }
  598. }
  599. /**
  600. * 处理文件上传
  601. * @param {File[]} files 上传的文件数组
  602. */
  603. async handleFileUpload(files) {
  604. // 目前仅显示文件信息,后续可以添加实际的上传和处理逻辑
  605. const fileNames = files.map((file) => file.name).join(", ");
  606. this.addMessage(`已选择文件:${fileNames}`, "user", false);
  607. }
  608. }
  609. // 等待DOM加载完成后再初始化
  610. document.addEventListener("DOMContentLoaded", () => {
  611. try {
  612. const chatUI = new ChatUI();
  613. } catch (error) {
  614. console.error("Failed to initialize ChatUI:", error);
  615. }
  616. });