@ -15,6 +15,8 @@ import { t } from '@/locales'
let controller = new AbortController ( )
const openLongReply = import . meta . env . VITE _GLOB _OPEN _LONG _REPLY === 'true'
const route = useRoute ( )
const dialog = useDialog ( )
const ms = useMessage ( )
@ -41,7 +43,7 @@ function handleSubmit() {
}
async function onConversation ( ) {
cons t message = prompt . value
le t message = prompt . value
if ( loading . value )
return
@ -88,40 +90,53 @@ async function onConversation() {
scrollToBottom ( )
try {
await fetchChatAPIProcess < Chat .ConversationResponse > ( {
prompt : message ,
options ,
signal : controller . signal ,
onDownloadProgress : ( { event } ) => {
const xhr = event . target
const { responseText } = xhr
/ / A l w a y s p r o c e s s t h e f i n a l l i n e
const lastIndex = responseText . lastIndexOf ( '\n' )
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 : data . text ? ? '' ,
inversion : false ,
error : false ,
loading : false ,
conversationOptions : { conversationId : data . conversationId , parentMessageId : data . id } ,
requestOptions : { prompt : message , options : { ... options } } ,
} ,
)
scrollToBottom ( )
}
catch ( error ) {
let lastText = ''
const fetchChatAPIOnce = async ( ) => {
await fetchChatAPIProcess < Chat .ConversationResponse > ( {
prompt : message ,
options ,
signal : controller . signal ,
onDownloadProgress : ( { event } ) => {
const xhr = event . target
const { responseText } = xhr
/ / A l w a y s p r o c e s s t h e f i n a l l i n e
const lastIndex = responseText . lastIndexOf ( '\n' )
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 : false ,
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 ( )
}
scrollToBottom ( )
}
catch ( error ) {
/ /
}
} ,
} )
}
} ,
} )
}
await fetchChatAPIOnce ( )
}
catch ( error : any ) {
const errorMessage = error ? . message ? ? t ( 'common.wrong' )
@ -181,7 +196,7 @@ async function onRegenerate(index: number) {
const { requestOptions } = dataSources . value [ index ]
cons t message = requestOptions ? . prompt ? ? ''
le t message = requestOptions ? . prompt ? ? ''
let options : Chat . ConversationRequest = { }
@ -205,39 +220,50 @@ async function onRegenerate(index: number) {
)
try {
await fetchChatAPIProcess < Chat .ConversationResponse > ( {
prompt : message ,
options ,
signal : controller . signal ,
onDownloadProgress : ( { event } ) => {
const xhr = event . target
const { responseText } = xhr
/ / A l w a y s p r o c e s s t h e f i n a l l i n e
const lastIndex = responseText . lastIndexOf ( '\n' )
let chunk = responseText
if ( lastIndex !== - 1 )
chunk = responseText . substring ( lastIndex )
try {
const data = JSON . parse ( chunk )
updateChat (
+ uuid ,
index ,
{
dateTime : new Date ( ) . toLocaleString ( ) ,
text : data . text ? ? '' ,
inversion : false ,
error : false ,
loading : false ,
conversationOptions : { conversationId : data . conversationId , parentMessageId : data . id } ,
requestOptions : { prompt : message , ... options } ,
} ,
)
}
catch ( error ) {
/ /
}
} ,
} )
let lastText = ''
const fetchChatAPIOnce = async ( ) => {
await fetchChatAPIProcess < Chat .ConversationResponse > ( {
prompt : message ,
options ,
signal : controller . signal ,
onDownloadProgress : ( { event } ) => {
const xhr = event . target
const { responseText } = xhr
/ / A l w a y s p r o c e s s t h e f i n a l l i n e
const lastIndex = responseText . lastIndexOf ( '\n' )
let chunk = responseText
if ( lastIndex !== - 1 )
chunk = responseText . substring ( lastIndex )
try {
const data = JSON . parse ( chunk )
updateChat (
+ uuid ,
index ,
{
dateTime : new Date ( ) . toLocaleString ( ) ,
text : lastText + data . text ? ? '' ,
inversion : false ,
error : false ,
loading : false ,
conversationOptions : { conversationId : data . conversationId , parentMessageId : data . id } ,
requestOptions : { prompt : message , ... options } ,
} ,
)
if ( openLongReply && data . detail . choices [ 0 ] . finish _reason === 'length' ) {
options . parentMessageId = data . id
lastText = data . text
message = ''
return fetchChatAPIOnce ( )
}
}
catch ( error ) {
/ /
}
} ,
} )
}
await fetchChatAPIOnce ( )
}
catch ( error : any ) {
if ( error . message === 'canceled' ) {