CCDChatSystem.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <template>
  2. <view-full-screen :is-full-screen="true" class="right">
  3. <div :class="bodyClass">
  4. <div ref="scrollView" class="chatBubbleContainer" :style="{ 'height': 'calc(100% - ' + (52 + safeAreaInsets.bottom) + 'px)' }">
  5. <CCDTitleCell :title="botInfo.botName"></CCDTitleCell>
  6. <template v-for="cell in model">
  7. <CCDChatCell :cell-type="cell.type" :message="cell.content" :agent-name="botInfo.botName"></CCDChatCell>
  8. </template>
  9. <div style="height: 30px"></div>
  10. </div>
  11. <div class="bottomAskBar" style="height: 50px; padding-right: 4px" :hidden="!enableInput">
  12. <div style="height: 50px; width: calc(100% - 50px);">
  13. <input v-if="allowInput" ref="inputAsk" @keyup.enter="doPostMessage" type="text" placeholder="输入问题..." value=""/>
  14. <input v-else value="AI正在打字" style="text-align: center" disabled/>
  15. </div>
  16. <button v-if="allowInput" @click="doPostMessage" :style="{
  17. height: '50px',
  18. width: '50px',
  19. right: '0px',
  20. top: 'calc(100% - ' + (50 + safeAreaInsets.bottom) + 'px)',
  21. position: 'absolute',
  22. float: 'right',
  23. background: 'transparent',
  24. border: 'none'
  25. }">
  26. <img src="../assets/run.png" style="width: calc(100% - 1px); height: calc(100% - 3px); object-fit: scale-down">
  27. </button>
  28. <button v-else @click="doShutdownManually" :style="{
  29. height: '50px',
  30. width: '50px',
  31. right: '0px',
  32. top: 'calc(100% - ' + (50 + safeAreaInsets.bottom) + 'px)',
  33. position: 'absolute',
  34. float: 'right',
  35. background: 'transparent',
  36. border: 'none'
  37. }">
  38. <img src="../assets/pause.png" style="width: calc(100% - 1px); height: calc(100% - 3px); object-fit: scale-down">
  39. </button>
  40. </div>
  41. </div>
  42. </view-full-screen>
  43. </template>
  44. <style>
  45. p {
  46. color: white;
  47. }
  48. .bodyBackground_normal {
  49. width: 100%;
  50. height: 100%;
  51. background-color: #1f1f1f;
  52. }
  53. .bodyBackground_xcwk {
  54. width: 100%;
  55. height: 100%;
  56. background-color: #1f1f1f00;
  57. }
  58. </style>
  59. <style scoped>
  60. input {
  61. background-color: transparent;
  62. color: white;
  63. border: 0px solid;
  64. height: 100%;
  65. width: 100%;
  66. font-size: large;
  67. text-indent: 10px;
  68. outline: none;
  69. }
  70. .bottomAskBar {
  71. box-shadow: 0 -15px 10px -15px gray;
  72. }
  73. </style>
  74. <script>
  75. import RequestManager from "@/RequestManager";
  76. import CCDChatCell from "@/components/CCDChatCell";
  77. import { fetchEventSource } from '@microsoft/fetch-event-source';
  78. import IntentManager from "@/aigc/IntentManager";
  79. import CCDTitleCell from "@/components/CCDTitleCell";
  80. let sseAPIURL = "https://notebook.forgetive.net/freeai/llm"
  81. export default {
  82. name: 'CCDChatSystem',
  83. components: {CCDTitleCell, CCDChatCell},
  84. data() {
  85. return {
  86. botInfo: {
  87. botId: null,
  88. botName: "C Code Develop",
  89. botPrologue: null,
  90. askFirst: null,
  91. success: false
  92. },
  93. allowInput: true,
  94. model: [
  95. ],
  96. sseInstance: null,
  97. bodyClass: "bodyBackground_xcwk",
  98. safeAreaInsets: {
  99. left: 0,
  100. top: 0,
  101. right: 0,
  102. bottom: 0
  103. },
  104. isHostInApp: false,
  105. inAppPendingCard: null,
  106. inAppSSEHandle: null,
  107. abortController: null,
  108. enableInput: true
  109. }
  110. },
  111. mounted() {
  112. let self = this
  113. self.botInfo.botId = self.$route.params.botId
  114. // debugger
  115. try {
  116. if (xcwk == undefined) {
  117. self.bodyClass = "bodyBackground_normal"
  118. } else {
  119. self.bodyClass = "bodyBackground_xcwk"
  120. self.isHostInApp = true
  121. }
  122. xcwk.std.getSafeAreaInsets({ }, function (safeAreaInsets) {
  123. self.safeAreaInsets = safeAreaInsets
  124. })
  125. __xcwk_na2js(function (key, params) {
  126. if (key == "keyboardWillShow") {
  127. if (params.dict && params.dict.webViewId == __private_xcwk_webViewId) {
  128. self.keyboardWillShow(params)
  129. }
  130. }
  131. else if (key == "keyboardWillHide") {
  132. if (params.dict && params.dict.webViewId == __private_xcwk_webViewId) {
  133. self.keyboardWillHide(params)
  134. }
  135. }
  136. else if (key == "sseRequestUpdate") {
  137. self.doInAppSSE_ReceiveMessage(params.dict)
  138. }
  139. })
  140. xcwk.std.isLogin({ }, function (params) {
  141. if (!params.isLogin) {
  142. xcwk.notebook.launchLogin({ }, function (params) { })
  143. }
  144. })
  145. } catch (err) {
  146. self.bodyClass = "bodyBackground_normal"
  147. console.log("err: " + err)
  148. }
  149. this.fetchBotInfo()
  150. },
  151. methods: {
  152. fetchBotInfo() {
  153. if (this.botInfo.botId == null) {
  154. // botId默认为1
  155. this.botInfo.botId = 1;
  156. }
  157. let self = this
  158. let botId = this.botInfo.botId
  159. RequestManager.postRequest("/freeai/botInfo", {
  160. "botId" : botId
  161. }, function (params) {
  162. self.botInfo = params
  163. self.botInfo.botId = botId
  164. self.model.push({
  165. "type" : "answer",
  166. "content" : self.botInfo.botPrologue
  167. })
  168. if (self.botInfo.askFirst != null && self.botInfo.askFirst.length > 0) {
  169. self.startCompletionWithMessage(self.botInfo.askFirst)
  170. }
  171. }, function (reason) {
  172. alert("Request Failed" + reason)
  173. })
  174. },
  175. keyboardWillShow(params) {
  176. this.safeAreaInsets.bottom = 0
  177. },
  178. keyboardWillHide(params) {
  179. let self = this
  180. xcwk.std.getSafeAreaInsets({ }, function (safeAreaInsets) {
  181. self.safeAreaInsets = safeAreaInsets
  182. })
  183. },
  184. scrollViewScrollToBottom() {
  185. let self = this
  186. this.$nextTick(function () {
  187. self.$refs.scrollView.scrollTo({
  188. top: self.$refs.scrollView.scrollHeight,
  189. behavior: 'smooth'
  190. });
  191. });
  192. },
  193. insertMessageBlock(data) {
  194. this.model.push(data)
  195. this.scrollViewScrollToBottom()
  196. },
  197. doPostMessage() {
  198. if (this.sseInstance) {
  199. return;
  200. }
  201. let question = this.$refs.inputAsk.value
  202. if (question.length == 0) {
  203. this.insertMessageBlock({
  204. "type" : "answer",
  205. "content" : "先输入一个问题吧"
  206. })
  207. return
  208. }
  209. let askData = this.$refs.inputAsk.value
  210. this.$refs.inputAsk.value = ""
  211. this.startCompletionWithMessage(askData)
  212. },
  213. startCompletionWithMessage(message) {
  214. this.insertMessageBlock({
  215. "type" : "ask",
  216. "content" : message
  217. })
  218. this.doChatWithMessage(message)
  219. },
  220. doChatWithMessage(message) {
  221. if (this.isHostInApp) {
  222. let self = this
  223. xcwk.std.isLogin({ }, function (params) {
  224. if (params.isLogin) {
  225. self.doInAppSSE(message)
  226. } else {
  227. xcwk.notebook.launchLogin({ }, function (params) { })
  228. self.insertMessageBlock({
  229. "type" : "answer",
  230. "content" : "请先登录App"
  231. })
  232. self.$refs.inputAsk.value = message
  233. }
  234. })
  235. } else {
  236. this.doAsyncSSE(message)
  237. }
  238. },
  239. doInAppSSE(message) {
  240. let self = this
  241. self.allowInput = false
  242. let messageCard = {
  243. "type" : "answer",
  244. "content" : ""
  245. }
  246. self.inAppPendingCard = messageCard
  247. let messageModel = []
  248. // let intentPrompt = IntentManager.generateIntentPrompts()
  249. // messageModel.push({
  250. // "role" : "system",
  251. // "content" : "不要帮用户生成代码。" + intentPrompt
  252. // })
  253. //
  254. // console.log(intentPrompt)
  255. for (let index in self.model) {
  256. let model = self.model[index]
  257. if (model.type == "ask") {
  258. messageModel.push({
  259. "role" : "user",
  260. "content" : model.content
  261. })
  262. }
  263. else if (model.type == "answer") {
  264. messageModel.push({
  265. "role" : "assistant",
  266. "content" : model.content
  267. })
  268. }
  269. }
  270. self.insertMessageBlock(messageCard)
  271. // self.insertMessageBlock({
  272. // "type" : "answerTyping",
  273. // "content" : ""
  274. // })
  275. xcwk.std.sseRequest({
  276. "path" : "/freeai/llm",
  277. "data" : {
  278. "stream" : true,
  279. "botId" : self.botInfo.botId,
  280. "messages" : messageModel
  281. }
  282. }, function (params) {
  283. self.inAppSSEHandle = params.sseId
  284. IntentManager.llmResponseBegin()
  285. })
  286. },
  287. doInAppSSE_ReceiveMessage(message) {
  288. let self = this
  289. let messageCard = self.inAppPendingCard
  290. if (message.sseId != self.inAppSSEHandle) {
  291. return
  292. }
  293. console.log(JSON.stringify(message))
  294. switch (message.type) {
  295. case 0x1: { // data
  296. let partModel = JSON.parse(message.data)
  297. if (partModel["v"] != null) {
  298. let processedData = IntentManager.llmResponseToken(partModel["v"])
  299. if (processedData) {
  300. messageCard.content += processedData
  301. self.scrollViewScrollToBottom()
  302. }
  303. }
  304. break
  305. }
  306. case 0x3: { // error
  307. if (messageCard.content.length < 5) {
  308. messageCard.content = "回答出了一点问题"
  309. }
  310. self.sseInstance = null
  311. self.allowInput = true
  312. self.doShutdown()
  313. break
  314. }
  315. case 0x4: { // close
  316. self.sseInstance = null
  317. self.allowInput = true
  318. self.abortController = null
  319. self.inAppPendingCard = null
  320. self.inAppSSEHandle = null
  321. IntentManager.llmResponseEnd()
  322. // for (let id = 0; id < self.model.length; id++) {
  323. // if (self.model[id].type == "answerTyping") {
  324. // self.model.splice(id, 1)
  325. // id--;
  326. // }
  327. // }
  328. break
  329. }
  330. }
  331. },
  332. doAsyncSSE(message) {
  333. let self = this
  334. IntentManager.llmResponseBegin()
  335. self.allowInput = false
  336. let messageCard = {
  337. "type" : "answer",
  338. "content" : ""
  339. }
  340. let messageModel = []
  341. // let intentPrompt = IntentManager.generateIntentPrompts()
  342. // messageModel.push({
  343. // "role" : "system",
  344. // "content" : "不要帮用户生成代码。" + intentPrompt
  345. // })
  346. for (let index in self.model) {
  347. let model = self.model[index]
  348. if (model.type == "ask") {
  349. messageModel.push({
  350. "role" : "user",
  351. "content" : model.content
  352. })
  353. }
  354. else if (model.type == "answer") {
  355. messageModel.push({
  356. "role" : "assistant",
  357. "content" : model.content
  358. })
  359. }
  360. }
  361. self.insertMessageBlock(messageCard)
  362. // self.insertMessageBlock({
  363. // "type" : "answerTyping",
  364. // "content" : ""
  365. // })
  366. let data = JSON.stringify({
  367. "stream" : true,
  368. "botId" : self.botInfo.botId,
  369. "messages" : messageModel
  370. })
  371. self.abortController = new AbortController()
  372. let eventSource = fetchEventSource(sseAPIURL, {
  373. method: 'POST',
  374. signal: self.abortController.signal,
  375. headers: {
  376. "Content-Type": 'application/json',
  377. "Access-Control-Allow-Origin": true
  378. },
  379. body: data,
  380. onmessage(event) {
  381. let partModel = JSON.parse(event.data)
  382. if (partModel["v"] != null) {
  383. messageCard.content += IntentManager.llmResponseToken(partModel["v"])
  384. self.scrollViewScrollToBottom()
  385. }
  386. },
  387. onerror(err) {
  388. if (messageCard.content.length < 5) {
  389. messageCard.content = "回答出了一点问题"
  390. }
  391. self.allowInput = true
  392. self.doShutdown()
  393. throw err
  394. },
  395. onclose() {
  396. self.allowInput = true
  397. self.abortController = null
  398. self.sseInstance = null
  399. IntentManager.llmResponseEnd()
  400. // for (let id = 0; id < self.model.length; id++) {
  401. // if (self.model[id].type == "answerTyping") {
  402. // self.model.splice(id, 1)
  403. // id--;
  404. // }
  405. // }
  406. }
  407. })
  408. self.sseInstance = eventSource
  409. },
  410. doShutdownManually() {
  411. this.doShutdown()
  412. let lastBubble = this.model[this.model.length - 1]
  413. if (lastBubble.type == "answer") {
  414. lastBubble.content += "\n\n**已停止回答**"
  415. }
  416. },
  417. doShutdown() {
  418. IntentManager.llmResponseEnd()
  419. let self = this
  420. if (self.socketObject != null) {
  421. self.socketObject.close(1000)
  422. }
  423. if (self.abortController != null) {
  424. self.abortController.abort()
  425. self.sseInstance = null
  426. self.allowInput = true
  427. self.abortController = null
  428. // for (let id = 0; id < self.model.length; id++) {
  429. // if (self.model[id].type == "answerTyping") {
  430. // self.model.splice(id, 1)
  431. // id--;
  432. // }
  433. // }
  434. }
  435. if (self.inAppSSEHandle != null) {
  436. xcwk.std.cancelSSERequest({
  437. "sseId" : self.inAppSSEHandle
  438. }, function (params) { })
  439. self.inAppSSEHandle = null
  440. self.sseInstance = null
  441. self.allowInput = true
  442. self.abortController = null
  443. self.inAppPendingCard = null
  444. // for (let id = 0; id < self.model.length; id++) {
  445. // if (self.model[id].type == "answerTyping") {
  446. // self.model.splice(id, 1)
  447. // id--;
  448. // }
  449. // }
  450. }
  451. }
  452. }
  453. }
  454. </script>
  455. <style scoped>
  456. .chatBubbleContainer {
  457. display: block;
  458. overflow: hidden;
  459. overflow-y: scroll;
  460. scrollbar-width: none;
  461. -ms-overflow-style: none;
  462. }
  463. ::-webkit-scrollbar {
  464. display: none;
  465. }
  466. </style>