@@ -150,13 +150,20 @@ function initAiChat() {
150150 const overlay = document . createElement ( 'div' ) ;
151151 overlay . className = 'ai-modal-overlay' ;
152152 overlay . id = 'ai-settings-modal-overlay' ;
153+ // 修改点:在 header 中增加了新的 SVG 按钮 (#ai-new-chat-btn)
153154 overlay . innerHTML = `
154155 <div class="ai-modal">
155156 <div class="ai-modal-header">
156157 <span class="ai-modal-title">AI 配置设置</span>
157- <button class="ai-modal-close" id="ai-modal-close-btn" title="关闭">
158- <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
159- </button>
158+ <div style="display:flex; align-items:center; gap:8px;">
159+ <!-- 新增:新对话按钮 -->
160+ <button id="ai-new-chat-btn" class="ai-icon-btn" title="开启新对话 (清空历史)">
161+ <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="17 8 12 3 7 8"></polyline><line x1="12" y1="3" x2="12" y2="15"></line></svg>
162+ </button>
163+ <button class="ai-modal-close" id="ai-modal-close-btn" title="关闭">
164+ <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
165+ </button>
166+ </div>
160167 </div>
161168 <div class="ai-modal-body">
162169 <div class="ai-form-group">
@@ -186,6 +193,33 @@ function initAiChat() {
186193 <button class="ai-btn ai-btn-save" id="ai-modal-save-btn">保存配置</button>
187194 </div>
188195 </div>` ;
196+
197+ // 注入简单的样式以支持新按钮的外观
198+ const style = document . createElement ( 'style' ) ;
199+ style . textContent = `
200+ .ai-icon-btn {
201+ background: transparent;
202+ border: 1px solid transparent;
203+ border-radius: 4px;
204+ cursor: pointer;
205+ display: flex;
206+ align-items: center;
207+ justify-content: center;
208+ color: #57606a;
209+ transition: all 0.2s;
210+ padding: 4px;
211+ }
212+ .ai-icon-btn:hover {
213+ background-color: #ffeef0;
214+ color: #cf222e;
215+ border-color: #fdaeb7;
216+ }
217+ .ai-icon-btn svg {
218+ display: block;
219+ }
220+ ` ;
221+ document . head . appendChild ( style ) ;
222+
189223 document . body . appendChild ( overlay ) ;
190224 return overlay ;
191225 }
@@ -209,6 +243,28 @@ function initAiChat() {
209243 if ( modalOverlay ) modalOverlay . classList . remove ( 'active' ) ;
210244 }
211245
246+ // --- 新增:开启新对话逻辑 ---
247+ function startNewChat ( ) {
248+ // 1. 清空内存历史
249+ messageHistory = [ ] ;
250+
251+ // 2. 清空本地存储
252+ localStorage . removeItem ( 'ai_chat_history' ) ;
253+
254+ // 3. 清空界面
255+ if ( chatList ) {
256+ chatList . innerHTML = '' ;
257+ }
258+
259+ // 4. 关闭模态框
260+ closeModal ( ) ;
261+
262+ // 5. 聚焦输入框,方便用户立即开始
263+ if ( inputArea ) inputArea . focus ( ) ;
264+
265+ console . log ( '已开启新对话,历史记录已清空。' ) ;
266+ }
267+
212268 function saveConfigFromModal ( ) {
213269 const newConfig = {
214270 apiKey : document . getElementById ( 'cfg-api-key' ) . value . trim ( ) ,
@@ -238,12 +294,28 @@ function initAiChat() {
238294
239295 function bindModalEvents ( ) {
240296 if ( ! modalOverlay ) return ;
297+
298+ // 绑定关闭按钮
241299 modalOverlay . querySelector ( '#ai-modal-close-btn' ) . onclick = closeModal ;
300+
301+ // 绑定取消按钮
242302 modalOverlay . querySelector ( '#ai-modal-cancel-btn' ) . onclick = closeModal ;
303+
304+ // 绑定保存按钮
243305 modalOverlay . querySelector ( '#ai-modal-save-btn' ) . onclick = saveConfigFromModal ;
306+
307+ // --- 新增:绑定新对话按钮 ---
308+ const newChatBtn = modalOverlay . querySelector ( '#ai-new-chat-btn' ) ;
309+ if ( newChatBtn ) {
310+ newChatBtn . onclick = startNewChat ;
311+ }
312+
313+ // 点击遮罩层关闭
244314 modalOverlay . onclick = ( e ) => {
245315 if ( e . target === modalOverlay ) closeModal ( ) ;
246316 } ;
317+
318+ // ESC 键关闭
247319 document . onkeydown = ( e ) => {
248320 if ( e . key === 'Escape' && modalOverlay ?. classList . contains ( 'active' ) ) closeModal ( ) ;
249321 } ;
0 commit comments