Jelajahi Sumber

聊天增加sliceInfo字段

李富豪 6 bulan lalu
induk
melakukan
09e398f646
3 mengubah file dengan 38 tambahan dan 7 penghapusan
  1. 33 6
      app/client/platforms/bigModel.ts
  2. 4 0
      app/store/chat.ts
  3. 1 1
      package.json

+ 33 - 6
app/client/platforms/bigModel.ts

@@ -108,7 +108,10 @@ export class BigModelApi implements LLMApi {
       };
 
       controller.signal.onabort = finish;
+
       let start = true;
+      let sliceInfoPromise: Promise<void> | null = null;
+
       fetchEventSource(chatPath, {
         ...chatPayload,
         async onopen(res: any) {
@@ -168,17 +171,40 @@ export class BigModelApi implements LLMApi {
           const chatMode = useChatStore.getState().chatMode;
           if (chatMode === 'LOCAL' && start) {// 切片
             start = false;
-            const res: any = await api.get(`deepseek/api/slice/search/${info.id}`);
-            const sliceInfo = {
-              ...res.data,
-            }
-            delete sliceInfo.code;
-            console.log(sliceInfo, 'sliceInfo');
+            sliceInfoPromise = (async () => {
+              try {
+                const res: any = await api.get(`deepseek/api/slice/search/${info.id}`);
+                const sliceInfo = {
+                  ...res.data,
+                };
+                delete sliceInfo.code;
+                useChatStore.getState().updateCurrentSession((session) => {
+                  session.messages = session.messages.map((item, index) => {
+                    if (index === session.messages.length - 1 && item.role !== 'user') {
+                      return {
+                        ...item,
+                        sliceInfo: sliceInfo,
+                      };
+                    } else {
+                      return {
+                        ...item,
+                      }
+                    }
+                  });
+                });
+              } catch (error) {
+                console.error(error);
+              }
+            })();
           }
         },
         async onclose() {
           finish();
+          if (sliceInfoPromise) {
+            await sliceInfoPromise; // 等待 sliceInfo 加载完成
+          }
           const session = useChatStore.getState().sessions[0];
+
           const item = session.messages.find(item => item.role === 'user');
           const dialogName = item ? item.content : '新的聊天';
           const data = {
@@ -191,6 +217,7 @@ export class BigModelApi implements LLMApi {
               date: item.date,
               role: item.role,
               content: item.content,
+              sliceInfo: item.sliceInfo,
             })),
           };
           const messages = session.messages.slice();

+ 4 - 0
app/store/chat.ts

@@ -39,6 +39,10 @@ export type ChatMessage = RequestMessage & {
     id: string,
     url: string,
   },
+  sliceInfo?: {
+    knowledge_id: string,
+    doc: any[],
+  }
 };
 
 export function createMessage(override: Partial<ChatMessage>): ChatMessage {

+ 1 - 1
package.json

@@ -5,7 +5,7 @@
   "description": "AI问答-客户端",
   "license": "MIT",
   "scripts": {
-    "dev": "next dev",
+    "dev": "next dev -p 4000",
     "out": "yarn run build && yarn run export",
     "mask": "npx tsx app/masks/build.ts",
     "mask:watch": "npx watch \"yarn mask\" app/masks",