@@ -46,13 +46,15 @@ async function request() {
4646
4747 console .log (' fetching...' )
4848
49+ messages .value .push (t .value )
50+
51+ emit (' update:history_messages' , messages .value )
52+
4953 let res = await fetch (urlJoin (window .location .pathname , ' /api/chat_gpt' ), {
5054 method: ' POST' ,
5155 headers: {' Accept' : ' text/event-stream' , Authorization: token .value },
52- body: JSON .stringify ({messages: messages .value })
56+ body: JSON .stringify ({messages: messages .value . slice ( 0 , messages . value ?. length - 1 ) })
5357 })
54-
55- messages .value .push (t .value )
5658 // read body as stream
5759 console .log (' reading...' )
5860 let reader = res .body ! .getReader ()
@@ -62,6 +64,8 @@ async function request() {
6264
6365 let buffer = ' '
6466
67+ let hasCodeBlockIndicator = false
68+
6569 while (true ) {
6670 let {done, value} = await reader .read ()
6771 if (done ) {
@@ -78,17 +82,32 @@ async function request() {
7882 const decoder = new TextDecoder (' utf-8' )
7983 const raw = decoder .decode (input )
8084
81- const regex = / {"content":"(. +? )"}/ g
82- const matches = raw .match (regex )
85+ // console.log(input, raw)
86+
87+ const line = raw .split (' \n\n ' )
88+
89+ line ?.forEach (v => {
90+ const data = v .slice (' event:message\n data:' .length )
91+ if (! data ) {
92+ return
93+ }
94+ const content = JSON .parse (data ).content
95+
96+ if (! hasCodeBlockIndicator ) {
97+ hasCodeBlockIndicator = content .indexOf (' `' ) > - 1
98+ }
8399
84- matches ?.forEach (v => {
85- const content = JSON .parse (v ).content
86100 for (let c of content ) {
87101 buffer += c
88- if (isCodeBlockComplete (buffer )) {
89- t .value .content = buffer
102+ if (hasCodeBlockIndicator ) {
103+ if (isCodeBlockComplete (buffer )) {
104+ t .value .content = buffer
105+ hasCodeBlockIndicator = false
106+ } else {
107+ t .value .content = buffer + ' \n ```'
108+ }
90109 } else {
91- t .value .content = buffer + ' \n ``` '
110+ t .value .content = buffer
92111 }
93112 }
94113 })
@@ -167,11 +186,12 @@ async function regenerate(index: number) {
167186
168187const editing_idx = ref (- 1 )
169188
170- const button_shape = computed (() => loading .value ? ' square' : ' circle' )
189+ const show = computed (() => messages ?.value ?.length > 1 )
190+
171191 </script >
172192
173193<template >
174- <a-card class =" chatgpt" title =" ChatGPT" v-if =" messages?.length>1 " >
194+ <a-card class =" chatgpt" title =" ChatGPT" v-if =" show " >
175195 <div class =" chatgpt-container" >
176196 <a-list
177197 class =" chatgpt-log"
0 commit comments