Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import conversation local #4034

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading