forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { transform } from 'lodash-es'; | ||
import { useMemo } from 'react'; | ||
import { shallow } from 'zustand/shallow'; | ||
|
||
import { Migration } from '@/migrations'; | ||
import { useSessionStore } from '@/store/session'; | ||
import { useSettings } from '@/store/settings'; | ||
import { | ||
ConfigFileAgents, | ||
ConfigFileAll, | ||
ConfigFileSessions, | ||
ConfigFileSettings, | ||
} from '@/types/exportConfig'; | ||
import { exportConfigFile } from '@/utils/config'; | ||
|
||
export const useExportConfig = () => { | ||
const [sessions] = useSessionStore((s) => [s.sessions], shallow); | ||
|
||
const [settings] = useSettings((s) => [s.settings, s.importSettings], shallow); | ||
|
||
const exportAgents = () => { | ||
const config: ConfigFileAgents = { | ||
exportType: 'agents', | ||
state: { | ||
sessions: transform(sessions, (result, value, key) => { | ||
result[key] = { ...value, chats: {}, topics: {} }; | ||
}), | ||
}, | ||
version: Migration.targetVersion, | ||
}; | ||
|
||
exportConfigFile(config, 'agents'); | ||
}; | ||
|
||
const exportSessions = () => { | ||
const config: ConfigFileSessions = { | ||
exportType: 'sessions', | ||
state: { sessions }, | ||
version: Migration.targetVersion, | ||
}; | ||
|
||
exportConfigFile(config, 'sessions'); | ||
}; | ||
|
||
const exportSettings = () => { | ||
const config: ConfigFileSettings = { | ||
exportType: 'settings', | ||
state: { settings }, | ||
version: Migration.targetVersion, | ||
}; | ||
|
||
exportConfigFile(config, 'settings'); | ||
}; | ||
|
||
const exportAll = () => { | ||
// 将 入参转换为 配置文件格式 | ||
const config: ConfigFileAll = { | ||
exportType: 'all', | ||
state: { sessions, settings }, | ||
version: Migration.targetVersion, | ||
}; | ||
exportConfigFile(config, 'config'); | ||
}; | ||
|
||
return useMemo( | ||
() => ({ exportAgents, exportAll, exportSessions, exportSettings }), | ||
[sessions, settings], | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { notification } from 'antd'; | ||
|
||
import { CURRENT_CONFIG_VERSION, Migration } from '@/migrations'; | ||
import { ConfigState } from '@/types/exportConfig'; | ||
|
||
export const exportConfigFile = (config: object, fileName?: string) => { | ||
const file = `LobeChat-${fileName || '-config'}-v${CURRENT_CONFIG_VERSION}.json`; | ||
|
||
// 创建一个 Blob 对象 | ||
const blob = new Blob([JSON.stringify(config)], { type: 'application/json' }); | ||
|
||
// 创建一个 URL 对象,用于下载 | ||
const url = URL.createObjectURL(blob); | ||
|
||
// 创建一个 <a> 元素,设置下载链接和文件名 | ||
const a = document.createElement('a'); | ||
a.href = url; | ||
a.download = file; | ||
|
||
// 触发 <a> 元素的点击事件,开始下载 | ||
document.body.append(a); | ||
a.click(); | ||
|
||
// 下载完成后,清除 URL 对象 | ||
URL.revokeObjectURL(url); | ||
a.remove(); | ||
}; | ||
|
||
export const handleImport = (info: any, onConfigImport: (config: ConfigState) => void) => { | ||
const reader = new FileReader(); | ||
//读取完文件之后的回调函数 | ||
reader.onloadend = function (evt) { | ||
const fileString = evt.target?.result; | ||
const fileJson = fileString as string; | ||
|
||
try { | ||
const { state } = Migration.migrate(JSON.parse(fileJson)); | ||
|
||
onConfigImport(state); | ||
} catch (error) { | ||
notification.error({ | ||
description: `出错原因: ${(error as Error).message}`, | ||
message: '导入失败', | ||
}); | ||
} | ||
}; | ||
//@ts-ignore file 类型不明确 | ||
reader.readAsText(info.file.originFileObj, 'utf8'); | ||
}; |