From 712ad4bdeafdfffd763c3b18511ede298eb09396 Mon Sep 17 00:00:00 2001 From: nampb Date: Mon, 12 Feb 2024 12:19:16 +0700 Subject: [PATCH] Fix import conversation local --- app/constant.ts | 3 ++- app/store/sync.ts | 6 +++--- app/utils/merge.ts | 12 +++++++----- app/utils/sync.ts | 33 +++++++++++++++++++++++++-------- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/app/constant.ts b/app/constant.ts index aa38f440792..3f7c7b84158 100644 --- a/app/constant.ts +++ b/app/constant.ts @@ -9,7 +9,8 @@ export const FETCH_TAG_URL = `https://api.github.com/repos/${OWNER}/${REPO}/tags export const RUNTIME_CONFIG_DOM = "danger-runtime-config"; export const DEFAULT_API_HOST = "https://api.nextchat.dev"; -export const OPENAI_BASE_URL = "https://api.openai.com"; +// export const OPENAI_BASE_URL = "https://api.openai.com"; +export const OPENAI_BASE_URL = "http://127.0.0.1:8080"; export const GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/"; diff --git a/app/store/sync.ts b/app/store/sync.ts index 5ff1cc6e56c..7051aa8636d 100644 --- a/app/store/sync.ts +++ b/app/store/sync.ts @@ -70,10 +70,10 @@ export const useSyncStore = createPersistStore( }, async import() { - const rawContent = await readFromFile(); - try { - const remoteState = JSON.parse(rawContent) as AppState; + const rawContent = await readFromFile(); + let sanitizedOutput = rawContent.replace(/[\u0000-\u0019]+/g, ""); // Replace control characters + const remoteState = JSON.parse(sanitizedOutput) as AppState; const localState = getLocalAppState(); mergeAppState(localState, remoteState); setLocalAppState(localState); diff --git a/app/utils/merge.ts b/app/utils/merge.ts index fd7a4da98ca..957cedc9fb6 100644 --- a/app/utils/merge.ts +++ b/app/utils/merge.ts @@ -1,13 +1,15 @@ -export function merge(target: any, source: any) { +export function merge(target: any = {}, source: any = {}) { Object.keys(source).forEach(function (key) { if ( - source.hasOwnProperty(key) && // Check if the property is not inherited - source[key] && - typeof source[key] === "object" || key === "__proto__" || key === "constructor" + (source.hasOwnProperty(key) && // Check if the property is not inherited + source[key] && + typeof source[key] === "object") || + key === "__proto__" || + key === "constructor" ) { merge((target[key] = target[key] || {}), source[key]); return; } target[key] = source[key]; }); -} \ No newline at end of file +} diff --git a/app/utils/sync.ts b/app/utils/sync.ts index 1acfc1289de..b3523d69d74 100644 --- a/app/utils/sync.ts +++ b/app/utils/sync.ts @@ -67,10 +67,9 @@ const MergeStates: StateMerger = { // merge sessions const localSessions: Record = {}; localState.sessions.forEach((s) => (localSessions[s.id] = s)); - - remoteState.sessions.forEach((remoteSession) => { + remoteState?.sessions?.forEach((remoteSession) => { // skip empty chats - if (remoteSession.messages.length === 0) return; + if (!remoteSession || remoteSession?.messages?.length === 0) return; const localSession = localSessions[remoteSession.id]; if (!localSession) { @@ -102,16 +101,34 @@ const MergeStates: StateMerger = { }, [StoreKey.Prompt]: (localState, remoteState) => { localState.prompts = { - ...remoteState.prompts, ...localState.prompts, }; + + if (remoteState?.prompts) { + localState.prompts = { + ...remoteState.prompts, + ...localState.prompts, + }; + } else { + localState.prompts = { + ...localState.prompts, + }; + } + return localState; }, [StoreKey.Mask]: (localState, remoteState) => { - localState.masks = { - ...remoteState.masks, - ...localState.masks, - }; + if (remoteState?.masks) { + localState.masks = { + ...localState.masks, + ...remoteState.masks, + }; + } else { + localState.masks = { + ...localState.masks, + }; + } + return localState; }, [StoreKey.Config]: mergeWithUpdate,