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