ChatGPT.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <script setup lang="ts">
  2. import Icon, { SendOutlined } from '@ant-design/icons-vue'
  3. import { storeToRefs } from 'pinia'
  4. import { marked } from 'marked'
  5. import hljs from 'highlight.js'
  6. import type { Ref } from 'vue'
  7. import { urlJoin } from '@/lib/helper'
  8. import { useSettingsStore, useUserStore } from '@/pinia'
  9. import 'highlight.js/styles/vs2015.css'
  10. import type { ChatComplicationMessage } from '@/api/openai'
  11. import openai from '@/api/openai'
  12. import ChatGPT_logo from '@/assets/svg/ChatGPT_logo.svg?component'
  13. const props = defineProps<{
  14. content: string
  15. path?: string
  16. historyMessages?: ChatComplicationMessage[]
  17. }>()
  18. const emit = defineEmits(['update:history_messages'])
  19. const { language: current } = storeToRefs(useSettingsStore())
  20. const history_messages = computed(() => props.historyMessages)
  21. const messages = ref([]) as Ref<ChatComplicationMessage[] | undefined >
  22. onMounted(() => {
  23. messages.value = props.historyMessages
  24. })
  25. watch(history_messages, () => {
  26. messages.value = props.historyMessages
  27. })
  28. const loading = ref(false)
  29. const ask_buffer = ref('')
  30. // eslint-disable-next-line sonarjs/cognitive-complexity
  31. async function request() {
  32. loading.value = true
  33. const t = ref({
  34. role: 'assistant',
  35. content: '',
  36. })
  37. const user = useUserStore()
  38. const { token } = storeToRefs(user)
  39. messages.value?.push(t.value)
  40. emit('update:history_messages', messages.value)
  41. const res = await fetch(urlJoin(window.location.pathname, '/api/chatgpt'), {
  42. method: 'POST',
  43. headers: { Accept: 'text/event-stream', Authorization: token.value },
  44. body: JSON.stringify({ filepath: props.path, messages: messages.value?.slice(0, messages.value?.length - 1) }),
  45. })
  46. const reader = res.body!.getReader()
  47. let buffer = ''
  48. let hasCodeBlockIndicator = false
  49. while (true) {
  50. try {
  51. const { done, value } = await reader.read()
  52. if (done) {
  53. setTimeout(() => {
  54. scrollToBottom()
  55. }, 500)
  56. loading.value = false
  57. store_record()
  58. break
  59. }
  60. apply(value!)
  61. }
  62. catch (e) {
  63. break
  64. }
  65. }
  66. function apply(input: Uint8Array) {
  67. const decoder = new TextDecoder('utf-8')
  68. const raw = decoder.decode(input)
  69. // console.log(input, raw)
  70. const line = raw.split('\n\n')
  71. line?.forEach(v => {
  72. const data = v.slice('event:message\ndata:'.length)
  73. if (!data)
  74. return
  75. const content = JSON.parse(data).content
  76. if (!hasCodeBlockIndicator)
  77. hasCodeBlockIndicator = content.includes('`')
  78. for (const c of content) {
  79. buffer += c
  80. if (hasCodeBlockIndicator) {
  81. if (isCodeBlockComplete(buffer)) {
  82. t.value.content = buffer
  83. hasCodeBlockIndicator = false
  84. }
  85. else {
  86. t.value.content = `${buffer}\n\`\`\``
  87. }
  88. }
  89. else {
  90. t.value.content = buffer
  91. }
  92. }
  93. // keep container scroll to bottom
  94. scrollToBottom()
  95. })
  96. }
  97. function isCodeBlockComplete(text: string) {
  98. const codeBlockRegex = /```/g
  99. const matches = text.match(codeBlockRegex)
  100. if (matches)
  101. return matches.length % 2 === 0
  102. else
  103. return true
  104. }
  105. function scrollToBottom() {
  106. const container = document.querySelector('.right-settings .ant-card-body')
  107. if (container)
  108. container.scrollTop = container.scrollHeight
  109. }
  110. }
  111. async function send() {
  112. if (!messages.value)
  113. messages.value = []
  114. if (messages.value.length === 0) {
  115. messages.value.push({
  116. role: 'user',
  117. content: `${props.content}\n\nCurrent Language Code: ${current.value}`,
  118. })
  119. }
  120. else {
  121. messages.value.push({
  122. role: 'user',
  123. content: ask_buffer.value,
  124. })
  125. ask_buffer.value = ''
  126. }
  127. await request()
  128. }
  129. const renderer = new marked.Renderer()
  130. renderer.code = (code, lang: string) => {
  131. const language = hljs.getLanguage(lang) ? lang : 'nginx'
  132. const highlightedCode = hljs.highlight(code, { language }).value
  133. return `<pre><code class="hljs ${language}">${highlightedCode}</code></pre>`
  134. }
  135. marked.setOptions({
  136. renderer,
  137. pedantic: false,
  138. gfm: true,
  139. breaks: false,
  140. })
  141. function store_record() {
  142. openai.store_record({
  143. file_name: props.path,
  144. messages: messages.value,
  145. })
  146. }
  147. function clear_record() {
  148. openai.store_record({
  149. file_name: props.path,
  150. messages: [],
  151. })
  152. messages.value = []
  153. emit('update:history_messages', [])
  154. }
  155. const editing_idx = ref(-1)
  156. async function regenerate(index: number) {
  157. editing_idx.value = -1
  158. messages.value = messages.value?.slice(0, index)
  159. await request()
  160. }
  161. const show = computed(() => !messages.value || messages.value?.length === 0)
  162. </script>
  163. <template>
  164. <div
  165. v-if="show"
  166. class="chat-start"
  167. >
  168. <AButton
  169. :loading="loading"
  170. @click="send"
  171. >
  172. <Icon
  173. v-if="!loading"
  174. :component="ChatGPT_logo"
  175. />
  176. {{ $gettext('Ask ChatGPT for Help') }}
  177. </AButton>
  178. </div>
  179. <div
  180. v-else
  181. class="chatgpt-container"
  182. >
  183. <AList
  184. class="chatgpt-log"
  185. item-layout="horizontal"
  186. :data-source="messages"
  187. >
  188. <template #renderItem="{ item, index }">
  189. <AListItem>
  190. <AComment :author="item.role === 'assistant' ? $gettext('Assistant') : $gettext('User')">
  191. <template #content>
  192. <div
  193. v-if="item.role === 'assistant' || editing_idx !== index"
  194. class="content"
  195. v-html="marked.parse(item.content)"
  196. />
  197. <AInput
  198. v-else
  199. v-model:value="item.content"
  200. style="padding: 0"
  201. :bordered="false"
  202. />
  203. </template>
  204. <template #actions>
  205. <span
  206. v-if="item.role === 'user' && editing_idx !== index"
  207. @click="editing_idx = index"
  208. >
  209. {{ $gettext('Modify') }}
  210. </span>
  211. <template v-else-if="editing_idx === index">
  212. <span @click="regenerate(index + 1)">{{ $gettext('Save') }}</span>
  213. <span @click="editing_idx = -1">{{ $gettext('Cancel') }}</span>
  214. </template>
  215. <span
  216. v-else-if="!loading"
  217. @click="regenerate(index)"
  218. >
  219. {{ $gettext('Reload') }}
  220. </span>
  221. </template>
  222. </AComment>
  223. </AListItem>
  224. </template>
  225. </AList>
  226. <div class="input-msg">
  227. <div class="control-btn">
  228. <ASpace v-show="!loading">
  229. <APopconfirm
  230. :cancel-text="$gettext('No')"
  231. :ok-text="$gettext('OK')"
  232. :title="$gettext('Are you sure you want to clear the record of chat?')"
  233. @confirm="clear_record"
  234. >
  235. <AButton type="text">
  236. {{ $gettext('Clear') }}
  237. </AButton>
  238. </APopconfirm>
  239. <AButton
  240. type="text"
  241. @click="regenerate((messages?.length ?? 1) - 1)"
  242. >
  243. {{ $gettext('Regenerate response') }}
  244. </AButton>
  245. </ASpace>
  246. </div>
  247. <ATextarea
  248. v-model:value="ask_buffer"
  249. auto-size
  250. />
  251. <div class="send-btn">
  252. <AButton
  253. size="small"
  254. type="text"
  255. :loading="loading"
  256. @click="send"
  257. >
  258. <SendOutlined />
  259. </AButton>
  260. </div>
  261. </div>
  262. </div>
  263. </template>
  264. <style lang="less" scoped>
  265. .chatgpt-container {
  266. margin: 0 auto;
  267. max-width: 800px;
  268. .chatgpt-log {
  269. .content {
  270. width: 100%;
  271. :deep(.hljs) {
  272. border-radius: 5px;
  273. }
  274. }
  275. :deep(.ant-list-item) {
  276. padding: 0;
  277. }
  278. :deep(.ant-comment-content) {
  279. width: 100%;
  280. }
  281. :deep(.ant-comment) {
  282. width: 100%;
  283. }
  284. :deep(.ant-comment-content-detail) {
  285. width: 100%;
  286. p {
  287. margin-bottom: 10px;
  288. }
  289. }
  290. :deep(.ant-list-item:first-child) {
  291. display: none;
  292. }
  293. }
  294. .input-msg {
  295. position: relative;
  296. .control-btn {
  297. display: flex;
  298. justify-content: center;
  299. }
  300. .send-btn {
  301. position: absolute;
  302. right: 0;
  303. bottom: 3px;
  304. }
  305. }
  306. }
  307. </style>