From 5c6413d31f2791b948f1172a3a33c549dba3a06c Mon Sep 17 00:00:00 2001 From: Rusyaidi Date: Thu, 26 Oct 2023 22:24:53 +0800 Subject: [PATCH] Fixed broken KAI response due to improper use of finally. --- app/_layout.js | 1 + app/index.js | 31 ++++++++------------ components/ChatMenu/ChatWindow/ChatWindow.js | 2 +- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/app/_layout.js b/app/_layout.js index e0694dc..623d9a6 100644 --- a/app/_layout.js +++ b/app/_layout.js @@ -77,6 +77,7 @@ const Layout = () => { headerStyle: {backgroundColor: Color.Header}, headerTitleStyle: {color: Color.Text}, headerTintColor: Color.White, + contentStyle: {backgroundColor: Color.Background} }}> { const mescontent = ((createnew ) ? chatCache + data : messages.at(-1).mes + data) .replaceAll(currentInstruct.input_sequence, ``) .replaceAll(currentInstruct.output_sequence, ``) - const newmessage = (createnew) ? createChatEntry(charName, false, "", APIType, - (APIType === API.KAI) ? 'concedo/koboldcpp' : JSON.stringify(hordeModels) + const newmessage = (createnew) ? createChatEntry(charName, false, "", APIType, 'concedo/koboldcpp' ) : messages.at(-1) newmessage.mes = mescontent newmessage.swipes[newmessage.swipe_id] = mescontent newmessage.gen_finished = humanizedISO8601DateTime() newmessage.swipe_info[newmessage.swipe_id].gen_finished = humanizedISO8601DateTime() - return createnew ? [...messages , newmessage] : [...messages.slice(0,-1), newmessage] + const finalized_messages = createnew ? [...messages , newmessage] : [...messages.slice(0,-1), newmessage] + return finalized_messages } catch (error) { console.log("Couldnt write due to:" + error) return messages } finally { - if(!save) return - console.log(`Saving chat`) - saveChatFile(messages, charName, currentChat) + if(save) { + console.log(`Saving chat`) + saveChatFile(messages, charName, currentChat) + } } }) } @@ -224,7 +225,7 @@ const Home = () => { const KAIresponse = () => { console.log(`Using KAI`) const controller = new AbortController(); - const timeout = setTimeout(() => controller.abort(), 5000); + const timeout = setTimeout(() => controller.abort(), 60000); fetch(`${kaiendpoint}/api/extra/generate/stream`, { reactNative: {textStreaming: true}, method: `POST`, @@ -243,13 +244,9 @@ const Home = () => { } const text = new TextDecoder().decode(value) let events = text.split('\n') - console.log(events) - if(events.length !== 2) { - const data = - ((events.length === 4)? - JSON.parse(events.at(1).substring(5)) : - JSON.parse(events.at(0).substring(5)) - ).token + for(e of events) + if(e.startsWith('data')) { + const data = JSON.parse(e.substring(5)).token insertGeneratedMessage(data) } return reader.read().then(processText) @@ -301,8 +298,6 @@ const Home = () => { }) } - - return ( @@ -389,7 +384,6 @@ const Home = () => { onChangeText={(text) => setNewMessage(text)} multiline={true} /> - { nowGenerating ? { @@ -428,11 +422,10 @@ const Home = () => { const styles = StyleSheet.create({ container: { - flex:1 + flex:1, }, safeArea: { flex: 1, - backgroundColor: Color.Background }, welcometext : { diff --git a/components/ChatMenu/ChatWindow/ChatWindow.js b/components/ChatMenu/ChatWindow/ChatWindow.js index 7499af4..519bb21 100644 --- a/components/ChatMenu/ChatWindow/ChatWindow.js +++ b/components/ChatMenu/ChatWindow/ChatWindow.js @@ -18,7 +18,7 @@ const ChatWindow = ({messages}) => { useEffect(() => { if (!nowGenerating) scrollViewRef.current?.scrollToEnd() },[nowGenerating]) - + return (