大幅优化同步

This commit is contained in:
JiangYingjin
2025-03-03 13:06:43 +08:00
parent 88f8ca822f
commit 3bc977cebd
6 changed files with 102 additions and 22 deletions

View File

@@ -29,6 +29,7 @@ import { ModelConfig, ModelType, useAppConfig } from "./config";
import { useAccessStore } from "./access";
import { collectModelsWithDefaultModel } from "../utils/model";
import { createEmptyMask, Mask } from "./mask";
import { useSyncStore } from "./sync";
const localStorage = safeLocalStorage();
@@ -226,6 +227,8 @@ export const useChatStore = createPersistStore(
currentSessionIndex: 0,
sessions: [newSession, ...state.sessions],
}));
useSyncStore.getState().upload();
},
clearSessions() {
@@ -233,6 +236,8 @@ export const useChatStore = createPersistStore(
sessions: [createEmptySession()],
currentSessionIndex: 0,
}));
useSyncStore.getState().upload();
},
selectSession(index: number) {
@@ -264,6 +269,8 @@ export const useChatStore = createPersistStore(
sessions: newSessions,
};
});
useSyncStore.getState().upload();
},
newSession(mask?: Mask) {
@@ -327,6 +334,8 @@ export const useChatStore = createPersistStore(
sessions,
}));
useSyncStore.getState().upload();
showToast(
Locale.Home.DeleteToast,
{
@@ -433,6 +442,8 @@ export const useChatStore = createPersistStore(
get().onNewMessage(botMessage, session);
}
ChatControllerPool.remove(session.id, botMessage.id);
useSyncStore.getState().upload();
},
onBeforeTool(tool: ChatMessageTool) {
(botMessage.tools = botMessage?.tools || []).push(tool);
@@ -727,8 +738,10 @@ export const useChatStore = createPersistStore(
console.log("[Memory] ", message);
get().updateTargetSession(session, (session) => {
session.lastSummarizeIndex = lastSummarizeIndex;
session.memoryPrompt = message; // Update the memory prompt for stored it in local storage
session.memoryPrompt = message;
});
useSyncStore.getState().upload();
}
},
onError(err) {

View File

@@ -22,6 +22,12 @@ export interface WebDavConfig {
const isApp = !!getClientConfig()?.isApp;
export type SyncStore = GetStoreState<typeof useSyncStore>;
export enum SyncAction {
SYNC = "SYNC",
UPLOAD = "UPLOAD",
DOWNLOAD = "DOWNLOAD",
}
const DEFAULT_SYNC_STATE = {
provider: ProviderType.WebDAV,
useProxy: true,
@@ -88,41 +94,75 @@ export const useSyncStore = createPersistStore(
return client;
},
async sync(overwrite = false) {
async sync(action: SyncAction = SyncAction.SYNC) {
if (!(await this.hasAccount())) {
console.log("[Sync] No account found, skipping sync.");
return;
}
const localState = getLocalAppState();
const provider = get().provider;
const config = get()[provider];
const client = this.getClient();
try {
const remoteState = await client.get(config.username);
if (!remoteState || remoteState === "") {
await client.set(config.username, JSON.stringify(localState));
console.log(
"[Sync] Remote state is empty, using local state instead.",
);
return;
} else {
if (!overwrite) {
if (action === SyncAction.SYNC) {
console.log("[Sync] Syncing state", config.username);
try {
const remoteState = await client.get(config.username);
if (!remoteState || remoteState === "") {
await client.set(config.username, JSON.stringify(localState));
console.log(
"[Sync] Remote state is empty, using local state instead.",
);
return;
} else {
const parsedRemoteState = JSON.parse(
await client.get(config.username),
) as AppState;
mergeAppState(localState, parsedRemoteState);
setLocalAppState(localState);
}
} catch (e) {
console.log("[Sync] failed to get remote state", e);
throw e;
}
await client.set(config.username, JSON.stringify(localState));
} else if (action === SyncAction.UPLOAD) {
console.log("[Sync] Uploading state", localState);
await client.set(config.username, JSON.stringify(localState));
} else if (action === SyncAction.DOWNLOAD) {
console.log("[Sync] Downloading state", config.username);
const remoteState = await client.get(config.username);
if (!remoteState || remoteState === "") {
console.log(
"[Sync] Remote state is empty, using local state instead.",
);
return;
} else {
const parsedRemoteState = JSON.parse(remoteState) as AppState;
setLocalAppState(parsedRemoteState);
}
} catch (e) {
console.log("[Sync] failed to get remote state", e);
throw e;
}
await client.set(config.username, JSON.stringify(localState));
this.markSyncTime();
},
async overwrite() {
await this.sync(true);
async download() {
await this.sync(SyncAction.DOWNLOAD);
},
async upload() {
await this.sync(SyncAction.UPLOAD);
},
async hasAccount() {
const provider = get().provider;
const config = get()[provider] as any;
console.log("[Sync] hasAccount", provider, config);
// console.log("[Sync] hasAccount", !!(provider === ProviderType.WebDAV ? config.username && config.password : config.username && config.apiKey));
return provider === ProviderType.WebDAV
? config.username && config.password
: config.username && config.apiKey;
},
async check() {