Browse Source

Merge branch 'chd' of http://192.168.1.202:8087/chd/pvs_chat into wzg

wzg 6 months ago
parent
commit
1d2e39aea3
4 changed files with 38 additions and 54 deletions
  1. 4 23
      js/ai-service.js
  2. 27 28
      js/chat-ui.js
  3. 6 3
      js/content.js
  4. 1 0
      sidebar.html

+ 4 - 23
js/ai-service.js

@@ -5,9 +5,12 @@
 class AIService {
   constructor() {
     this.apiEndpoint = CONFIG.AI_API.ENDPOINT;
+    this.messageContainer = document.getElementById("chat-messages");
     this.model = CONFIG.AI_API.MODEL;
+
     this.context = [];
     this.currentExcelData = null; // 保存当前Excel数据
+    this.response = ''
     // 使用默认API密钥
     this.apiKey = CONFIG.AI_API.DEFAULT_API_KEY;
     this.controller = null; // 用于中断请求的 AbortController
@@ -63,29 +66,7 @@ class AIService {
         messages: this.formatMessages(message),
         stream: true
       });
-      // console.log(completion);
-      // const signal = response.controller.signal;
-      // let text = ''
-      // try {
-      //   const iterator = response.iterator();
-      //   for await (const chunk of iterator) {
-      //     if (chunk) {
-      //       console.log(chunk);
-
-      //       const decodedChunk = chunk.choices[0].delta.content;
-      //       if (decodedChunk) {
-      //         text += decodedChunk;
-      //       }
-      //     }
-      //   }
-      //   console.log(text);
-      // } catch (error) {
-      //   if (signal.aborted) {
-      //     console.log("Stream reading aborted");
-      //   } else {
-      //     console.error("Error reading stream:", error);
-      //   }
-      // }
+
       return response;
 
 

+ 27 - 28
js/chat-ui.js

@@ -64,19 +64,19 @@ class ChatUI {
     );
 
     // 创建打断按钮
-    this.stopButton.className = "stop-button";
-    this.stopButton.innerHTML = `
-      <svg viewBox="0 0 24 24" fill="currentColor">
-        <path d="M6 6h12v12H6z"/>
-      </svg>
-      <span>停止生成</span>
-    `;
+    // this.stopButton.className = "stop-button";
+    // this.stopButton.innerHTML = `
+    //   <svg viewBox="0 0 24 24" fill="currentColor">
+    //     <path d="M6 6h12v12H6z"/>
+    //   </svg>
+    //   <span>停止生成</span>
+    // `;
 
     // 将打断按钮添加到输入按钮区域
-    document.querySelector(".input-buttons").appendChild(this.stopButton);
+    // document.querySelector(".input-buttons").appendChild(this.stopButton);
 
     // 添加打断按钮事件监听
-    this.stopButton.addEventListener("click", () => this.handleStop());
+    // this.stopButton.addEventListener("click", () => this.handleStop());
 
     this.isTyping = false; // 添加打字状态标记
     this.loadingDiv = null; // 添加加载动画的引用
@@ -245,9 +245,9 @@ class ChatUI {
     this.adjustInputHeight();
 
     try {
-      this.stopButton.classList.add("show");
+      // this.stopButton.classList.add("show");
       this.setInputState(true); // 禁用输入框
-      this.loadingDiv = this.createLoadingMessage();
+      this.loadingDiv = ''
       console.log(this.aiService.currentExcelData);
 
       // 如果存在Excel数据,添加上下文提示
@@ -276,7 +276,7 @@ ${this.iframeInfo}`;
       this.isTyping = false;
       await this.addMessage(response, "assistant", true);
 
-      this.stopButton.classList.remove("show");
+      // this.stopButton.classList.remove("show");
       this.setInputState(false); // 这里会自动聚焦输入框
 
       this.aiService.updateContext(response, "assistant");
@@ -286,7 +286,7 @@ ${this.iframeInfo}`;
         this.loadingDiv = null;
       }
 
-      this.stopButton.classList.remove("show");
+      // this.stopButton.classList.remove("show");
       this.setInputState(false); // 这里也会自动聚焦输入框
       this.isTyping = false;
 
@@ -381,6 +381,7 @@ ${this.iframeInfo}`;
     } else {
       this.newMessageIndicator.classList.add("show");
     }
+    return paragraph
   }
 
   /**
@@ -568,18 +569,10 @@ ${this.iframeInfo}`;
   }
   createFillButton() {
     const button = document.createElement("button");
+    button.id = 'fill-button'
     button.className = "fill-button";
     button.innerHTML = `填充`;
-    button.addEventListener("click", () => {
-      window.parent.postMessage(
-        {
-          type: "FILL_INPUT",
-          data: this.excelData,
-        },
-        "*"
-      );
-      //从夫窗口获取信息
-    });
+  
     return button;
   }
   /**
@@ -664,7 +657,7 @@ ${this.iframeInfo}`;
         this.loadingDiv = null;
       }
 
-      this.stopButton.classList.remove("show");
+      // this.stopButton.classList.remove("show");
       this.setInputState(false); // 中断后也自动聚焦输入框
 
       this.addMessage("用户手动停止生成", "assistant", false, true);
@@ -682,7 +675,7 @@ ${this.iframeInfo}`;
       this.stopButton.classList.add("show");
     } else {
       this.inputWrapper.classList.remove("generating");
-      this.stopButton.classList.remove("show");
+      // this.stopButton.classList.remove("show");
       // AI回复完成后,自动聚焦到输入框
       this.input.focus();
     }
@@ -737,7 +730,7 @@ ${this.iframeInfo}`;
     try {
       // 显示加载状态
       this.setInputState(true);
-      this.loadingDiv = this.createLoadingMessage();
+      this.loadingDiv = ''
 
       // 通过postMessage请求页面分析结果
       this.iframeInfo = await new Promise((resolve) => {
@@ -806,7 +799,7 @@ ${this.iframeInfo}`;
         if (this.excelTypes[extension]) {
           try {
             this.setInputState(true);
-            this.loadingDiv = this.createLoadingMessage();
+            this.loadingDiv = ''
 
             // 读取Excel文件
             const data = await this.readExcelFile(file);
@@ -873,6 +866,12 @@ ${this.iframeInfo}`;
       if (!this.excelData[header]) this.excelData[header] = []
       this.excelData[header].push(data[1][i])
     })
+    window.parent.postMessage({
+      type: "EXCEL_DATA", data: {
+        excelData: this.excelData,
+      }
+    }, "*");
+   
     return `我将向你展示一个通过SheetJS库读取的Excel文件内容和一个form表单。请帮我理解这些数据:
 
 文件名:${fileName}
@@ -943,7 +942,7 @@ ${this.iframeInfo}
 // 等待DOM加载完成后再初始化
 document.addEventListener("DOMContentLoaded", () => {
   try {
-    const chatUI = new ChatUI();
+    window.chatUI = new ChatUI();
 
 
   } catch (error) {

+ 6 - 3
js/content.js

@@ -208,11 +208,14 @@ class SidebarManager {
         }
       }
       if (event.data.type === "HANDLE_FILL_INPUT") {
-        const { formData, excelData } = event.data.data
-        excelDataA = excelData
-        console.log(formChildren, excelDataA);
+        const { formData } = event.data.data
+        console.log(formChildren);
         await handleFillInput(formData, 0)
       }
+      if (event.data.type === "EXCEL_DATA") {
+        const { excelData } = event.data.data
+        excelDataA = excelData
+      }
     });
 
     // 监听来自iframe的消息

+ 1 - 0
sidebar.html

@@ -197,6 +197,7 @@
               id="stop-button"
               class="icon-button stop-button"
               title="停止生成"
+              style="display: none;"
             >
               <svg
                 viewBox="0 0 24 24"