Skip to content

Commit

Permalink
Fix import conversation local
Browse files Browse the repository at this point in the history
  • Loading branch information
phambaonam committed Feb 12, 2024
1 parent bfefb99 commit 712ad4b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
3 changes: 2 additions & 1 deletion app/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/";

Expand Down
6 changes: 3 additions & 3 deletions app/store/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions app/utils/merge.ts
Original file line number Diff line number Diff line change
@@ -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];
});
}
}
33 changes: 25 additions & 8 deletions app/utils/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ const MergeStates: StateMerger = {
// merge sessions
const localSessions: Record<string, ChatSession> = {};
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) {
Expand Down Expand Up @@ -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<AppState[StoreKey.Config]>,
Expand Down

0 comments on commit 712ad4b

Please sign in to comment.