11<script setup lang="ts">
22import type { ChatComplicationMessage } from ' @/api/openai'
3- import type { Ref } from ' vue'
43import openai from ' @/api/openai'
54import ChatGPT_logo from ' @/assets/svg/ChatGPT_logo.svg?component'
65import { urlJoin } from ' @/lib/helper'
76import { useSettingsStore , useUserStore } from ' @/pinia'
87import Icon , { SendOutlined } from ' @ant-design/icons-vue'
98import hljs from ' highlight.js'
109import nginx from ' highlight.js/lib/languages/nginx'
11- import { Marked } from ' marked'
1210
11+ import { Marked } from ' marked'
1312import { markedHighlight } from ' marked-highlight'
1413import { storeToRefs } from ' pinia'
1514import ' highlight.js/styles/vs2015.css'
1615
1716const props = defineProps <{
1817 content: string
1918 path? : string
20- historyMessages? : ChatComplicationMessage []
2119}>()
2220
23- const emit = defineEmits ([' update:history_messages' ])
24-
2521hljs .registerLanguage (' nginx' , nginx )
2622
2723const { language : current } = storeToRefs (useSettingsStore ())
2824
29- const history_messages = computed (() => props .historyMessages )
30- const messages = ref ([]) as Ref <ChatComplicationMessage [] | undefined >
31-
32- onMounted (() => {
33- messages .value = props .historyMessages
34- })
35-
36- watch (history_messages , () => {
37- messages .value = props .historyMessages
25+ const messages = defineModel <ChatComplicationMessage []>(' historyMessages' , {
26+ type: Array ,
27+ default: reactive ([]),
3828})
3929
4030const loading = ref (false )
41- const ask_buffer = ref (' ' )
31+ const askBuffer = ref (' ' )
4232
4333async function request() {
4434 loading .value = true
@@ -52,9 +42,11 @@ async function request() {
5242
5343 const { token } = storeToRefs (user )
5444
55- messages .value ?. push ( t .value )
45+ messages .value = [ ... messages . value ! , t .value ]
5646
57- emit (' update:history_messages' , messages .value )
47+ await nextTick ()
48+
49+ scrollToBottom ()
5850
5951 const res = await fetch (urlJoin (window .location .pathname , ' /api/chatgpt' ), {
6052 method: ' POST' ,
@@ -76,7 +68,7 @@ async function request() {
7668 scrollToBottom ()
7769 }, 500 )
7870 loading .value = false
79- store_record ()
71+ storeRecord ()
8072 break
8173 }
8274 apply (value ! )
@@ -146,18 +138,21 @@ async function send() {
146138 messages .value = []
147139
148140 if (messages .value .length === 0 ) {
149- messages .value . push ( {
141+ messages .value = [ {
150142 role: ' user' ,
151143 content: ` ${props .content }\n\n Current Language Code: ${current .value } ` ,
152- })
144+ }]
153145 }
154146 else {
155- messages .value . push ( {
147+ messages .value = [ ... messages . value , {
156148 role: ' user' ,
157- content: ask_buffer .value ,
158- })
159- ask_buffer .value = ' '
149+ content: askBuffer .value ,
150+ }]
151+ askBuffer .value = ' '
160152 }
153+
154+ await nextTick ()
155+
161156 await request ()
162157}
163158
@@ -167,9 +162,7 @@ const marked = new Marked(
167162 highlight(code , lang ) {
168163 const language = hljs .getLanguage (lang ) ? lang : ' nginx'
169164
170- const highlightedCode = hljs .highlight (code , { language }).value
171-
172- return ` <pre><code class="hljs ${language }">${highlightedCode }</code></pre> `
165+ return hljs .highlight (code , { language }).value
173166 },
174167 }),
175168)
@@ -180,27 +173,27 @@ marked.setOptions({
180173 breaks: false ,
181174})
182175
183- function store_record () {
176+ function storeRecord () {
184177 openai .store_record ({
185178 file_name: props .path ,
186179 messages: messages .value ,
187180 })
188181}
189182
190- function clear_record () {
183+ function clearRecord () {
191184 openai .store_record ({
192185 file_name: props .path ,
193186 messages: [],
194187 })
195188 messages .value = []
196- emit (' update:history_messages' , [])
197189}
198190
199- const editing_idx = ref (- 1 )
191+ const editingIdx = ref (- 1 )
200192
201193async function regenerate(index : number ) {
202- editing_idx .value = - 1
194+ editingIdx .value = - 1
203195 messages .value = messages .value ?.slice (0 , index )
196+ await nextTick ()
204197 await request ()
205198}
206199
@@ -237,27 +230,27 @@ const show = computed(() => !messages.value || messages.value?.length === 0)
237230 <AComment :author =" item.role === 'assistant' ? $gettext('Assistant') : $gettext('User')" >
238231 <template #content >
239232 <div
240- v-if =" item.role === 'assistant' || editing_idx !== index"
233+ v-if =" item.role === 'assistant' || editingIdx !== index"
241234 v-dompurify-html =" marked.parse(item.content)"
242235 class =" content"
243236 />
244237 <AInput
245238 v-else
246239 v-model:value =" item.content"
247- style = " padding : 0 "
240+ class = " pa- 0"
248241 :bordered =" false"
249242 />
250243 </template >
251244 <template #actions >
252245 <span
253- v-if =" item.role === 'user' && editing_idx !== index"
254- @click =" editing_idx = index"
246+ v-if =" item.role === 'user' && editingIdx !== index"
247+ @click =" editingIdx = index"
255248 >
256249 {{ $gettext('Modify') }}
257250 </span >
258- <template v-else-if =" editing_idx === index " >
251+ <template v-else-if =" editingIdx === index " >
259252 <span @click =" regenerate(index + 1)" >{{ $gettext('Save') }}</span >
260- <span @click =" editing_idx = -1" >{{ $gettext('Cancel') }}</span >
253+ <span @click =" editingIdx = -1" >{{ $gettext('Cancel') }}</span >
261254 </template >
262255 <span
263256 v-else-if =" !loading"
@@ -277,7 +270,7 @@ const show = computed(() => !messages.value || messages.value?.length === 0)
277270 :cancel-text =" $gettext('No')"
278271 :ok-text =" $gettext('OK')"
279272 :title =" $gettext('Are you sure you want to clear the record of chat?')"
280- @confirm =" clear_record "
273+ @confirm =" clearRecord "
281274 >
282275 <AButton type =" text" >
283276 {{ $gettext('Clear') }}
@@ -292,7 +285,7 @@ const show = computed(() => !messages.value || messages.value?.length === 0)
292285 </ASpace >
293286 </div >
294287 <ATextarea
295- v-model:value =" ask_buffer "
288+ v-model:value =" askBuffer "
296289 auto-size
297290 />
298291 <div class =" send-btn" >
0 commit comments