1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| <script> async function onConversation() { let message = prompt.value
if (loading.value) return
if (!message || message.trim() === '') return
controller = new AbortController()
addChat( +uuid, { dateTime: new Date().toLocaleString(), text: message, inversion: true, error: false, conversationOptions: null, requestOptions: { prompt: message, options: null }, }, ) scrollToBottom()
loading.value = true prompt.value = ''
let options: Chat.ConversationRequest = {} const lastContext = conversationList.value[conversationList.value.length - 1]?.conversationOptions
if (lastContext && usingContext.value) options = { ...lastContext }
addChat( +uuid, { dateTime: new Date().toLocaleString(), text: t('chat.thinking'), loading: true, inversion: false, error: false, conversationOptions: null, requestOptions: { prompt: message, options: { ...options } }, }, ) scrollToBottom()
try { let lastText = '' const fetchChatAPIOnce = async () => { await ChatAPI<Chat.ConversationResponse>({ prompt: message, options, signal: controller.signal, onDownloadProgress: ({ event }) => { const xhr = event.target const { responseText } = xhr // Always process the final line const lastIndex = responseText.lastIndexOf('\n', responseText.length - 2) let chunk = responseText if (lastIndex !== -1) chunk = responseText.substring(lastIndex) try { const data = JSON.parse(chunk) updateChat( +uuid, dataSources.value.length - 1, { dateTime: new Date().toLocaleString(), text: lastText + (data.text ?? ''), inversion: false, error: false, loading: true, conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id }, requestOptions: { prompt: message, options: { ...options } }, }, )
if (openLongReply && data.detail.choices[0].finish_reason === 'length') { options.parentMessageId = data.id lastText = data.text message = '' return fetchChatAPIOnce() }
scrollToBottomIfAtBottom() } catch (error) { // } }, }) updateChatSome(+uuid, dataSources.value.length - 1, { loading: false }) }
await fetchChatAPIOnce() } catch (error: any) { const errorMessage = error?.message ?? t('common.wrong')
if (error.message === 'canceled') { updateChatSome( +uuid, dataSources.value.length - 1, { loading: false, }, ) scrollToBottomIfAtBottom() return }
const currentChat = getChatByUuidAndIndex(+uuid, dataSources.value.length - 1)
if (currentChat?.text && currentChat.text !== '') { updateChatSome( +uuid, dataSources.value.length - 1, { text: `${currentChat.text}\n[${errorMessage}]`, error: false, loading: false, }, ) return }
updateChat( +uuid, dataSources.value.length - 1, { dateTime: new Date().toLocaleString(), text: errorMessage, inversion: false, error: true, loading: false, conversationOptions: null, requestOptions: { prompt: message, options: { ...options } }, }, ) scrollToBottomIfAtBottom() } finally { loading.value = false } } </script>
|