Browse Source

【功能】【3.5.0】【上线】支持智能体

xcbosa mbp16 1 year ago
parent
commit
8f1649a9c4

+ 45 - 0
src/RequestManager.js

@@ -0,0 +1,45 @@
+export default {
+
+  name: "RequestManager",
+  supportXCWK() {
+    try {
+      if (xcwk == undefined) {
+        return false
+      } else {
+        return true
+      }
+    } catch (e) {
+      return false
+    }
+  },
+  postRequest(urlSuffix, data, successCB, failedCB) {
+    if (this.supportXCWK()) {
+      // 端能力请求
+      this.postRequestInApp(urlSuffix, data, successCB, failedCB)
+    } else {
+      // 原生请求
+      this.postRequestNative(urlSuffix, data, successCB, failedCB)
+    }
+  },
+  postRequestNative(urlSuffix, data, successCB, failedCB) {
+    let xhr = new XMLHttpRequest()
+    if (urlSuffix.length > 0 && urlSuffix[0] == '/') {
+      urlSuffix = urlSuffix.substring(1)
+    }
+    xhr.open("POST", "https://notebook.forgetive.net/" + urlSuffix, true)
+    xhr.setRequestHeader('Content-Type', 'application/json')
+    xhr.send(JSON.stringify(data))
+    xhr.onreadystatechange = function() {
+      if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
+        let response = JSON.parse(xhr.responseText)
+        successCB(response)
+      } else if (xhr.status !== 200) {
+        failedCB(xhr.statusText)
+      }
+    };
+  },
+  postRequestInApp(urlSuffix, data, successCB, failedCB) {
+
+  }
+
+}

+ 2 - 1
src/components/CCDChatCell.vue

@@ -9,7 +9,7 @@
     </div>
     </div>
   </template>
   </template>
   <template v-if="cellType == 'answer'">
   <template v-if="cellType == 'answer'">
-    <p class="userNickNameContainer" style="margin-left: 20px; color: #ffc745;">C Code Develop</p>
+    <p class="userNickNameContainer" style="margin-left: 20px; color: #ffc745;">{{ agentName }}</p>
     <div class="shineLeft" style="">
     <div class="shineLeft" style="">
       <div style="margin: 10px;">
       <div style="margin: 10px;">
         <p class="markdownContainer" v-html="parseMarkdown(message)"></p>
         <p class="markdownContainer" v-html="parseMarkdown(message)"></p>
@@ -28,6 +28,7 @@ export default {
   name: "CCDChatCell",
   name: "CCDChatCell",
   props: {
   props: {
     cellType: String,
     cellType: String,
+    agentName: String,
     message: String
     message: String
   },
   },
   methods: {
   methods: {

+ 66 - 27
src/components/CCDChatSystem.vue

@@ -2,13 +2,13 @@
   <view-full-screen :is-full-screen="true"  class="right">
   <view-full-screen :is-full-screen="true"  class="right">
     <div :class="bodyClass">
     <div :class="bodyClass">
       <div ref="scrollView" class="chatBubbleContainer" :style="{ 'height': 'calc(100% - ' + (52 + safeAreaInsets.bottom) + 'px)' }">
       <div ref="scrollView" class="chatBubbleContainer" :style="{ 'height': 'calc(100% - ' + (52 + safeAreaInsets.bottom) + 'px)' }">
-        <CCDTitleCell></CCDTitleCell>
+        <CCDTitleCell :title="botInfo.botName"></CCDTitleCell>
         <template v-for="cell in model">
         <template v-for="cell in model">
-          <CCDChatCell :cell-type="cell.type" :message="cell.content"></CCDChatCell>
+          <CCDChatCell :cell-type="cell.type" :message="cell.content" :agent-name="botInfo.botName"></CCDChatCell>
         </template>
         </template>
         <div style="height: 30px"></div>
         <div style="height: 30px"></div>
       </div>
       </div>
-      <div class="bottomAskBar" style="height: 50px; padding-right: 4px">
+      <div class="bottomAskBar" style="height: 50px; padding-right: 4px" :hidden="!enableInput">
         <div style="height: 50px; width: calc(100% - 50px);">
         <div style="height: 50px; width: calc(100% - 50px);">
           <input v-if="allowInput" ref="inputAsk" @keyup.enter="doPostMessage" type="text" placeholder="输入问题..." value=""/>
           <input v-if="allowInput" ref="inputAsk" @keyup.enter="doPostMessage" type="text" placeholder="输入问题..." value=""/>
           <input v-else value="AI正在打字" style="text-align: center" disabled/>
           <input v-else value="AI正在打字" style="text-align: center" disabled/>
@@ -74,6 +74,7 @@ input {
 </style>
 </style>
 
 
 <script>
 <script>
+import RequestManager from "@/RequestManager";
 import CCDChatCell from "@/components/CCDChatCell";
 import CCDChatCell from "@/components/CCDChatCell";
 import { fetchEventSource } from '@microsoft/fetch-event-source';
 import { fetchEventSource } from '@microsoft/fetch-event-source';
 import IntentManager from "@/aigc/IntentManager";
 import IntentManager from "@/aigc/IntentManager";
@@ -86,12 +87,15 @@ export default {
   components: {CCDTitleCell, CCDChatCell},
   components: {CCDTitleCell, CCDChatCell},
   data() {
   data() {
     return {
     return {
+      botInfo: {
+        botId: null,
+        botName: "C Code Develop",
+        botPrologue: null,
+        askFirst: null,
+        success: false
+      },
       allowInput: true,
       allowInput: true,
       model: [
       model: [
-        {
-          type: "answer",
-          content: "您好,我是 C Code Develop & C Notebook AI助手,可以帮您解决代码问题。从编程技巧到历史人文,我无所不知。快来跟我聊天吧~"
-        }
       ],
       ],
       sseInstance: null,
       sseInstance: null,
       bodyClass: "bodyBackground_xcwk",
       bodyClass: "bodyBackground_xcwk",
@@ -104,11 +108,14 @@ export default {
       isHostInApp: false,
       isHostInApp: false,
       inAppPendingCard: null,
       inAppPendingCard: null,
       inAppSSEHandle: null,
       inAppSSEHandle: null,
-      abortController: null
+      abortController: null,
+      enableInput: true
     }
     }
   },
   },
   mounted() {
   mounted() {
     let self = this
     let self = this
+    self.botInfo.botId = self.$route.params.botId
+    // debugger
     try {
     try {
       if (xcwk == undefined) {
       if (xcwk == undefined) {
         self.bodyClass = "bodyBackground_normal"
         self.bodyClass = "bodyBackground_normal"
@@ -143,8 +150,32 @@ export default {
       self.bodyClass = "bodyBackground_normal"
       self.bodyClass = "bodyBackground_normal"
       console.log("err: " + err)
       console.log("err: " + err)
     }
     }
+    this.fetchBotInfo()
   },
   },
   methods: {
   methods: {
+    fetchBotInfo() {
+      if (this.botInfo.botId == null) {
+        // botId默认为1
+        this.botInfo.botId = 1;
+      }
+      let self = this
+      let botId = this.botInfo.botId
+      RequestManager.postRequest("/freeai/botInfo", {
+        "botId" : botId
+      }, function (params) {
+        self.botInfo = params
+        self.botInfo.botId = botId
+        self.model.push({
+          "type" : "answer",
+          "content" : self.botInfo.botPrologue
+        })
+        if (self.botInfo.askFirst != null && self.botInfo.askFirst.length > 0) {
+          self.startCompletionWithMessage(self.botInfo.askFirst)
+        }
+      }, function (reason) {
+        alert("Request Failed" + reason)
+      })
+    },
     keyboardWillShow(params) {
     keyboardWillShow(params) {
       this.safeAreaInsets.bottom = 0
       this.safeAreaInsets.bottom = 0
     },
     },
@@ -179,12 +210,16 @@ export default {
         })
         })
         return
         return
       }
       }
+      let askData = this.$refs.inputAsk.value
+      this.$refs.inputAsk.value = ""
+      this.startCompletionWithMessage(askData)
+    },
+    startCompletionWithMessage(message) {
       this.insertMessageBlock({
       this.insertMessageBlock({
         "type" : "ask",
         "type" : "ask",
-        "content" : question
+        "content" : message
       })
       })
-      this.$refs.inputAsk.value = ""
-      this.doChatWithMessage(question)
+      this.doChatWithMessage(message)
     },
     },
     doChatWithMessage(message) {
     doChatWithMessage(message) {
       if (this.isHostInApp) {
       if (this.isHostInApp) {
@@ -217,13 +252,13 @@ export default {
 
 
       let messageModel = []
       let messageModel = []
 
 
-      let intentPrompt = IntentManager.generateIntentPrompts()
-      messageModel.push({
-        "role" : "system",
-        "content" : "不要帮用户生成代码。" + intentPrompt
-      })
-
-      console.log(intentPrompt)
+      // let intentPrompt = IntentManager.generateIntentPrompts()
+      // messageModel.push({
+      //   "role" : "system",
+      //   "content" : "不要帮用户生成代码。" + intentPrompt
+      // })
+      //
+      // console.log(intentPrompt)
 
 
       for (let index in self.model) {
       for (let index in self.model) {
         let model = self.model[index]
         let model = self.model[index]
@@ -251,6 +286,7 @@ export default {
         "path" : "/freeai/llm",
         "path" : "/freeai/llm",
         "data" : {
         "data" : {
           "stream" : true,
           "stream" : true,
+          "botId" : self.botInfo.botId,
           "messages" : messageModel
           "messages" : messageModel
         }
         }
       }, function (params) {
       }, function (params) {
@@ -314,11 +350,11 @@ export default {
       }
       }
 
 
       let messageModel = []
       let messageModel = []
-      let intentPrompt = IntentManager.generateIntentPrompts()
-      messageModel.push({
-        "role" : "system",
-        "content" : "不要帮用户生成代码。" + intentPrompt
-      })
+      // let intentPrompt = IntentManager.generateIntentPrompts()
+      // messageModel.push({
+      //   "role" : "system",
+      //   "content" : "不要帮用户生成代码。" + intentPrompt
+      // })
 
 
       for (let index in self.model) {
       for (let index in self.model) {
         let model = self.model[index]
         let model = self.model[index]
@@ -342,6 +378,12 @@ export default {
       //   "content" : ""
       //   "content" : ""
       // })
       // })
 
 
+      let data = JSON.stringify({
+        "stream" : true,
+        "botId" : self.botInfo.botId,
+        "messages" : messageModel
+      })
+
       self.abortController = new AbortController()
       self.abortController = new AbortController()
       let eventSource = fetchEventSource(sseAPIURL, {
       let eventSource = fetchEventSource(sseAPIURL, {
         method: 'POST',
         method: 'POST',
@@ -350,10 +392,7 @@ export default {
           "Content-Type": 'application/json',
           "Content-Type": 'application/json',
           "Access-Control-Allow-Origin": true
           "Access-Control-Allow-Origin": true
         },
         },
-        body: JSON.stringify({
-          "stream" : true,
-          "messages" : messageModel
-        }),
+        body: data,
         onmessage(event) {
         onmessage(event) {
           let partModel = JSON.parse(event.data)
           let partModel = JSON.parse(event.data)
           if (partModel["v"] != null) {
           if (partModel["v"] != null) {

+ 5 - 3
src/components/CCDTitleCell.vue

@@ -3,15 +3,17 @@
     <div style="height: 50px"></div>
     <div style="height: 50px"></div>
     <div style="text-align: center">
     <div style="text-align: center">
       <img src="../assets/roundicon.png" style="width: 36px; height: 36px; display: inline; vertical-align: middle">
       <img src="../assets/roundicon.png" style="width: 36px; height: 36px; display: inline; vertical-align: middle">
-      <p style="font-size: x-large; font-weight: bold; display: inline; vertical-align: middle; margin-left: 5px; color: #fff5be">C Code Develop</p>
-      <p style="font-size: x-large; display: inline; vertical-align: middle; color: #beffc1">ChatBot 2</p>
+      <p style="font-size: x-large; font-weight: bold; display: inline; vertical-align: middle; margin-left: 5px; color: #fff5be">{{ title }}</p>
     </div>
     </div>
   </div>
   </div>
 </template>
 </template>
 
 
 <script>
 <script>
 export default {
 export default {
-  name: "CCDTitleCell"
+  name: "CCDTitleCell",
+  props: {
+    "title" : String
+  }
 }
 }
 </script>
 </script>
 
 

+ 7 - 0
src/router/index.js

@@ -2,16 +2,23 @@ import Vue from 'vue'
 import Router from 'vue-router'
 import Router from 'vue-router'
 import viewFullScreen from "view-full-screen"
 import viewFullScreen from "view-full-screen"
 import CCDChatSystem from "@/components/CCDChatSystem";
 import CCDChatSystem from "@/components/CCDChatSystem";
+import CCDTitleCell from "@/components/CCDTitleCell";
 
 
 Vue.use(Router)
 Vue.use(Router)
 Vue.use(viewFullScreen)
 Vue.use(viewFullScreen)
 
 
 export default new Router({
 export default new Router({
+  mode: "history",
   routes: [
   routes: [
     {
     {
       path: '/',
       path: '/',
       name: 'CCDChatSystem',
       name: 'CCDChatSystem',
       component: CCDChatSystem
       component: CCDChatSystem
+    },
+    {
+      path: "/bot/:botId",
+      name: "CCDChatSystem",
+      component: CCDChatSystem
     }
     }
   ]
   ]
 })
 })