CCDChatSystem.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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></CCDTitleCell>
  6. <template v-for="cell in model">
  7. <CCDChatCell :cell-type="cell.type" :message="cell.content"></CCDChatCell>
  8. </template>
  9. <div style="height: 30px"></div>
  10. </div>
  11. <div class="bottomAskBar" style="height: 50px; padding-right: 4px">
  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 CCDChatCell from "@/components/CCDChatCell";
  76. import { fetchEventSource } from '@microsoft/fetch-event-source';
  77. import IntentManager from "@/aigc/IntentManager";
  78. import CCDTitleCell from "@/components/CCDTitleCell";
  79. let sseAPIURL = "https://notebook.forgetive.net/freeai/llm"
  80. export default {
  81. name: 'CCDChatSystem',
  82. components: {CCDTitleCell, CCDChatCell},
  83. data() {
  84. return {
  85. allowInput: true,
  86. model: [
  87. {
  88. type: "answer",
  89. content: "您好,我是 C Code Develop & C Notebook AI助手,可以帮您解决代码问题。从编程技巧到历史人文,我无所不知。快来跟我聊天吧~"
  90. }
  91. ],
  92. sseInstance: null,
  93. bodyClass: "bodyBackground_xcwk",
  94. safeAreaInsets: {
  95. left: 0,
  96. top: 0,
  97. right: 0,
  98. bottom: 0
  99. },
  100. isHostInApp: false,
  101. inAppPendingCard: null,
  102. inAppSSEHandle: null,
  103. abortController: null
  104. }
  105. },
  106. mounted() {
  107. let self = this
  108. try {
  109. if (xcwk == undefined) {
  110. self.bodyClass = "bodyBackground_normal"
  111. } else {
  112. self.bodyClass = "bodyBackground_xcwk"
  113. self.isHostInApp = true
  114. }
  115. xcwk.std.getSafeAreaInsets({ }, function (safeAreaInsets) {
  116. self.safeAreaInsets = safeAreaInsets
  117. })
  118. __xcwk_na2js(function (key, params) {
  119. if (key == "keyboardWillShow") {
  120. if (params.dict && params.dict.webViewId == __private_xcwk_webViewId) {
  121. self.keyboardWillShow(params)
  122. }
  123. }
  124. else if (key == "keyboardWillHide") {
  125. if (params.dict && params.dict.webViewId == __private_xcwk_webViewId) {
  126. self.keyboardWillHide(params)
  127. }
  128. }
  129. else if (key == "sseRequestUpdate") {
  130. self.doInAppSSE_ReceiveMessage(params.dict)
  131. }
  132. })
  133. xcwk.std.isLogin({ }, function (params) {
  134. if (!params.isLogin) {
  135. xcwk.notebook.launchLogin({ }, function (params) { })
  136. }
  137. })
  138. } catch (err) {
  139. self.bodyClass = "bodyBackground_normal"
  140. console.log("err: " + err)
  141. }
  142. },
  143. methods: {
  144. keyboardWillShow(params) {
  145. this.safeAreaInsets.bottom = 0
  146. },
  147. keyboardWillHide(params) {
  148. let self = this
  149. xcwk.std.getSafeAreaInsets({ }, function (safeAreaInsets) {
  150. self.safeAreaInsets = safeAreaInsets
  151. })
  152. },
  153. scrollViewScrollToBottom() {
  154. let self = this
  155. this.$nextTick(function () {
  156. self.$refs.scrollView.scrollTo({
  157. top: self.$refs.scrollView.scrollHeight,
  158. behavior: 'smooth'
  159. });
  160. });
  161. },
  162. insertMessageBlock(data) {
  163. this.model.push(data)
  164. this.scrollViewScrollToBottom()
  165. },
  166. doPostMessage() {
  167. if (this.sseInstance) {
  168. return;
  169. }
  170. let question = this.$refs.inputAsk.value
  171. if (question.length == 0) {
  172. this.insertMessageBlock({
  173. "type" : "answer",
  174. "content" : "先输入一个问题吧"
  175. })
  176. return
  177. }
  178. this.insertMessageBlock({
  179. "type" : "ask",
  180. "content" : question
  181. })
  182. this.$refs.inputAsk.value = ""
  183. this.doChatWithMessage(question)
  184. },
  185. doChatWithMessage(message) {
  186. if (this.isHostInApp) {
  187. let self = this
  188. xcwk.std.isLogin({ }, function (params) {
  189. if (params.isLogin) {
  190. self.doInAppSSE(message)
  191. } else {
  192. xcwk.notebook.launchLogin({ }, function (params) { })
  193. self.insertMessageBlock({
  194. "type" : "answer",
  195. "content" : "请先登录App"
  196. })
  197. self.$refs.inputAsk.value = message
  198. }
  199. })
  200. } else {
  201. this.doAsyncSSE(message)
  202. }
  203. },
  204. doInAppSSE(message) {
  205. let self = this
  206. self.allowInput = false
  207. let messageCard = {
  208. "type" : "answer",
  209. "content" : ""
  210. }
  211. self.inAppPendingCard = messageCard
  212. let messageModel = []
  213. let intentPrompt = IntentManager.generateIntentPrompts()
  214. messageModel.push({
  215. "role" : "system",
  216. "content" : "不要帮用户生成代码。" + intentPrompt
  217. })
  218. console.log(intentPrompt)
  219. for (let index in self.model) {
  220. let model = self.model[index]
  221. if (model.type == "ask") {
  222. messageModel.push({
  223. "role" : "user",
  224. "content" : model.content
  225. })
  226. }
  227. else if (model.type == "answer") {
  228. messageModel.push({
  229. "role" : "assistant",
  230. "content" : model.content
  231. })
  232. }
  233. }
  234. self.insertMessageBlock(messageCard)
  235. // self.insertMessageBlock({
  236. // "type" : "answerTyping",
  237. // "content" : ""
  238. // })
  239. xcwk.std.sseRequest({
  240. "path" : "/freeai/llm",
  241. "data" : {
  242. "stream" : true,
  243. "messages" : messageModel
  244. }
  245. }, function (params) {
  246. self.inAppSSEHandle = params.sseId
  247. IntentManager.llmResponseBegin()
  248. })
  249. },
  250. doInAppSSE_ReceiveMessage(message) {
  251. let self = this
  252. let messageCard = self.inAppPendingCard
  253. if (message.sseId != self.inAppSSEHandle) {
  254. return
  255. }
  256. console.log(JSON.stringify(message))
  257. switch (message.type) {
  258. case 0x1: { // data
  259. let partModel = JSON.parse(message.data)
  260. if (partModel["v"] != null) {
  261. let processedData = IntentManager.llmResponseToken(partModel["v"])
  262. if (processedData) {
  263. messageCard.content += data
  264. self.scrollViewScrollToBottom()
  265. }
  266. }
  267. break
  268. }
  269. case 0x3: { // error
  270. if (messageCard.content.length < 5) {
  271. messageCard.content = "回答出了一点问题"
  272. }
  273. self.sseInstance = null
  274. self.allowInput = true
  275. self.doShutdown()
  276. break
  277. }
  278. case 0x4: { // close
  279. self.sseInstance = null
  280. self.allowInput = true
  281. self.abortController = null
  282. self.inAppPendingCard = null
  283. self.inAppSSEHandle = null
  284. IntentManager.llmResponseEnd()
  285. // for (let id = 0; id < self.model.length; id++) {
  286. // if (self.model[id].type == "answerTyping") {
  287. // self.model.splice(id, 1)
  288. // id--;
  289. // }
  290. // }
  291. break
  292. }
  293. }
  294. },
  295. doAsyncSSE(message) {
  296. let self = this
  297. IntentManager.llmResponseBegin()
  298. self.allowInput = false
  299. let messageCard = {
  300. "type" : "answer",
  301. "content" : ""
  302. }
  303. let messageModel = []
  304. let intentPrompt = IntentManager.generateIntentPrompts()
  305. messageModel.push({
  306. "role" : "system",
  307. "content" : "不要帮用户生成代码。" + intentPrompt
  308. })
  309. for (let index in self.model) {
  310. let model = self.model[index]
  311. if (model.type == "ask") {
  312. messageModel.push({
  313. "role" : "user",
  314. "content" : model.content
  315. })
  316. }
  317. else if (model.type == "answer") {
  318. messageModel.push({
  319. "role" : "assistant",
  320. "content" : model.content
  321. })
  322. }
  323. }
  324. self.insertMessageBlock(messageCard)
  325. // self.insertMessageBlock({
  326. // "type" : "answerTyping",
  327. // "content" : ""
  328. // })
  329. self.abortController = new AbortController()
  330. let eventSource = fetchEventSource(sseAPIURL, {
  331. method: 'POST',
  332. signal: self.abortController.signal,
  333. headers: {
  334. "Content-Type": 'application/json',
  335. "Access-Control-Allow-Origin": true
  336. },
  337. body: JSON.stringify({
  338. "stream" : true,
  339. "messages" : messageModel
  340. }),
  341. onmessage(event) {
  342. let partModel = JSON.parse(event.data)
  343. if (partModel["v"] != null) {
  344. messageCard.content += IntentManager.llmResponseToken(partModel["v"])
  345. self.scrollViewScrollToBottom()
  346. }
  347. },
  348. onerror(err) {
  349. if (messageCard.content.length < 5) {
  350. messageCard.content = "回答出了一点问题"
  351. }
  352. self.allowInput = true
  353. self.doShutdown()
  354. throw err
  355. },
  356. onclose() {
  357. self.allowInput = true
  358. self.abortController = null
  359. self.sseInstance = null
  360. IntentManager.llmResponseEnd()
  361. // for (let id = 0; id < self.model.length; id++) {
  362. // if (self.model[id].type == "answerTyping") {
  363. // self.model.splice(id, 1)
  364. // id--;
  365. // }
  366. // }
  367. }
  368. })
  369. self.sseInstance = eventSource
  370. },
  371. doShutdownManually() {
  372. this.doShutdown()
  373. let lastBubble = this.model[this.model.length - 1]
  374. if (lastBubble.type == "answer") {
  375. lastBubble.content += "\n\n**已停止回答**"
  376. }
  377. },
  378. doShutdown() {
  379. IntentManager.llmResponseEnd()
  380. let self = this
  381. if (self.socketObject != null) {
  382. self.socketObject.close(1000)
  383. }
  384. if (self.abortController != null) {
  385. self.abortController.abort()
  386. self.sseInstance = null
  387. self.allowInput = true
  388. self.abortController = null
  389. // for (let id = 0; id < self.model.length; id++) {
  390. // if (self.model[id].type == "answerTyping") {
  391. // self.model.splice(id, 1)
  392. // id--;
  393. // }
  394. // }
  395. }
  396. if (self.inAppSSEHandle != null) {
  397. xcwk.std.cancelSSERequest({
  398. "sseId" : self.inAppSSEHandle
  399. }, function (params) { })
  400. self.inAppSSEHandle = null
  401. self.sseInstance = null
  402. self.allowInput = true
  403. self.abortController = null
  404. self.inAppPendingCard = null
  405. // for (let id = 0; id < self.model.length; id++) {
  406. // if (self.model[id].type == "answerTyping") {
  407. // self.model.splice(id, 1)
  408. // id--;
  409. // }
  410. // }
  411. }
  412. }
  413. }
  414. }
  415. </script>
  416. <style scoped>
  417. .chatBubbleContainer {
  418. display: block;
  419. overflow: hidden;
  420. overflow-y: scroll;
  421. scrollbar-width: none;
  422. -ms-overflow-style: none;
  423. }
  424. ::-webkit-scrollbar {
  425. display: none;
  426. }
  427. </style>