mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-24 10:40:07 +08:00
* refactor(main): 使用枚举管理 IPC 通道 - 新增 IpcChannel 枚举,用于统一管理所有的 IPC 通道 - 修改相关代码,使用 IpcChannel 枚举替代硬编码的字符串通道名称 - 此改动有助于提高代码的可维护性和可读性,避免因通道名称变更导致的错误 * refactor(ipc): 将字符串通道名称替换为 IpcChannel 枚举 - 在多个文件中将硬编码的字符串通道名称替换为 IpcChannel 枚举值 - 更新了相关文件的导入,增加了对 IpcChannel 的引用 - 通过使用枚举来管理 IPC 通道名称,提高了代码的可维护性和可读性 * refactor(ipc): 调整 IPC 通道枚举和预加载脚本 - 移除了 IpcChannel 枚举中的未使用注释 - 更新了预加载脚本中 IpcChannel 的导入路径 * refactor(ipc): 更新 IpcChannel导入路径 - 将 IpcChannel 的导入路径从 @main/enum/IpcChannel 修改为 @shared/IpcChannel - 此修改涉及多个文件,包括 AppUpdater、BackupManager、EditMcpJsonPopup 等 - 同时移除了 tsconfig.web.json 中对 src/main/**/* 的引用 * refactor(ipc): 添加 ReduxStoreReady 事件并更新事件监听 - 在 IpcChannel 枚举中添加 ReduxStoreReady 事件 - 更新 ReduxService 中的事件监听,使用新的枚举值 * refactor(main): 重构 ReduxService 中的状态变化事件处理 - 将状态变化事件名称定义为常量 STATUS_CHANGE_EVENT - 更新事件监听和触发使用新的常量 - 优化了代码结构,提高了可维护性 * refactor(i18n): 优化国际化配置和语言选择逻辑 - 在多个文件中引入 defaultLanguage 常量,统一默认语言设置 - 调整 i18n 初始化和语言变更逻辑,使用新配置 - 更新相关组件和 Hook 中的语言选择逻辑 * refactor(ConfigManager): 重构配置管理器 - 添加 ConfigKeys 枚举,用于统一配置项的键名 - 引入 defaultLanguage,作为默认语言设置 - 重构 get 和 set 方法,使用 ConfigKeys 枚举作为键名 - 优化类型定义和方法签名,提高代码可读性和可维护性 * refactor(ConfigManager): 重命名配置键 ZoomFactor 将配置键 zoomFactor 重命名为 ZoomFactor,以符合命名规范。 更新了相关方法和属性以反映这一变更。 * refactor(shared): 重构常量定义并优化文件大小格式化逻辑 - 在 constant.ts 中添加 KB、MB、GB 常量定义 - 将 defaultLanguage 移至 constant.ts - 更新 ConfigManager、useAppInit、i18n、GeneralSettings 等文件中的导入路径 - 优化 formatFileSize 函数,使用新定义的常量 * refactor(FileSize): 使用 GB/MB/KB 等常量处理文件大小计算 * refactor(ipc): 将字符串通道名称替换为 IpcChannel 枚举 - 在多个文件中将硬编码的字符串通道名称替换为 IpcChannel 枚举值 - 更新了相关文件的导入,增加了对 IpcChannel 的引用 - 通过使用枚举来管理 IPC 通道名称,提高了代码的可维护性和可读性 * refactor(ipc): 更新 IpcChannel导入路径 - 将 IpcChannel 的导入路径从 @main/enum/IpcChannel 修改为 @shared/IpcChannel - 此修改涉及多个文件,包括 AppUpdater、BackupManager、EditMcpJsonPopup 等 - 同时移除了 tsconfig.web.json 中对 src/main/**/* 的引用 * refactor(i18n): 优化国际化配置和语言选择逻辑 - 在多个文件中引入 defaultLanguage 常量,统一默认语言设置 - 调整 i18n 初始化和语言变更逻辑,使用新配置 - 更新相关组件和 Hook 中的语言选择逻辑 * refactor(shared): 重构常量定义并优化文件大小格式化逻辑 - 在 constant.ts 中添加 KB、MB、GB 常量定义 - 将 defaultLanguage 移至 constant.ts - 更新 ConfigManager、useAppInit、i18n、GeneralSettings 等文件中的导入路径 - 优化 formatFileSize 函数,使用新定义的常量 * refactor: 移除重复的导入语句 - 在 HomeWindow.tsx 和 useAppInit.ts 文件中移除了重复的 defaultLanguage导入语句 - 这个改动简化了代码结构,提高了代码的可读性和维护性
194 lines
11 KiB
TypeScript
194 lines
11 KiB
TypeScript
import type { ExtractChunkData } from '@cherrystudio/embedjs-interfaces'
|
|
import { electronAPI } from '@electron-toolkit/preload'
|
|
import { FileType, KnowledgeBaseParams, KnowledgeItem, MCPServer, Shortcut, WebDavConfig } from '@types'
|
|
import { contextBridge, ipcRenderer, OpenDialogOptions, shell } from 'electron'
|
|
import { CreateDirectoryOptions } from 'webdav'
|
|
import { IpcChannel } from '@shared/IpcChannel'
|
|
|
|
// Custom APIs for renderer
|
|
const api = {
|
|
getAppInfo: () => ipcRenderer.invoke(IpcChannel.App_Info),
|
|
reload: () => ipcRenderer.invoke(IpcChannel.App_Reload),
|
|
setProxy: (proxy: string) => ipcRenderer.invoke(IpcChannel.App_Proxy, proxy),
|
|
checkForUpdate: () => ipcRenderer.invoke(IpcChannel.App_CheckForUpdate),
|
|
showUpdateDialog: () => ipcRenderer.invoke(IpcChannel.App_ShowUpdateDialog),
|
|
setLanguage: (lang: string) => ipcRenderer.invoke(IpcChannel.App_SetLanguage, lang),
|
|
setLaunchOnBoot: (isActive: boolean) => ipcRenderer.invoke(IpcChannel.App_SetLaunchOnBoot, isActive),
|
|
setLaunchToTray: (isActive: boolean) => ipcRenderer.invoke(IpcChannel.App_SetLaunchToTray, isActive),
|
|
setTray: (isActive: boolean) => ipcRenderer.invoke(IpcChannel.App_SetTray, isActive),
|
|
setTrayOnClose: (isActive: boolean) => ipcRenderer.invoke(IpcChannel.App_SetTrayOnClose, isActive),
|
|
restartTray: () => ipcRenderer.invoke(IpcChannel.App_RestartTray),
|
|
setTheme: (theme: 'light' | 'dark') => ipcRenderer.invoke(IpcChannel.App_SetTheme, theme),
|
|
openWebsite: (url: string) => ipcRenderer.invoke(IpcChannel.Open_Website, url),
|
|
minApp: (url: string) => ipcRenderer.invoke(IpcChannel.Minapp, url),
|
|
clearCache: () => ipcRenderer.invoke(IpcChannel.App_ClearCache),
|
|
system: {
|
|
getDeviceType: () => ipcRenderer.invoke(IpcChannel.System_GetDeviceType)
|
|
},
|
|
zip: {
|
|
compress: (text: string) => ipcRenderer.invoke(IpcChannel.Zip_Compress, text),
|
|
decompress: (text: Buffer) => ipcRenderer.invoke(IpcChannel.Zip_Decompress, text)
|
|
},
|
|
backup: {
|
|
backup: (fileName: string, data: string, destinationPath?: string) =>
|
|
ipcRenderer.invoke(IpcChannel.Backup_Backup, fileName, data, destinationPath),
|
|
restore: (backupPath: string) => ipcRenderer.invoke(IpcChannel.Backup_Restore, backupPath),
|
|
backupToWebdav: (data: string, webdavConfig: WebDavConfig) =>
|
|
ipcRenderer.invoke(IpcChannel.Backup_BackupToWebdav, data, webdavConfig),
|
|
restoreFromWebdav: (webdavConfig: WebDavConfig) => ipcRenderer.invoke(IpcChannel.Backup_RestoreFromWebdav, webdavConfig),
|
|
listWebdavFiles: (webdavConfig: WebDavConfig) => ipcRenderer.invoke(IpcChannel.Backup_ListWebdavFiles, webdavConfig),
|
|
checkConnection: (webdavConfig: WebDavConfig) => ipcRenderer.invoke(IpcChannel.Backup_CheckConnection, webdavConfig),
|
|
createDirectory: (webdavConfig: WebDavConfig, path: string, options?: CreateDirectoryOptions) =>
|
|
ipcRenderer.invoke(IpcChannel.Backup_CreateDirectory, webdavConfig, path, options)
|
|
},
|
|
file: {
|
|
select: (options?: OpenDialogOptions) => ipcRenderer.invoke(IpcChannel.File_Select, options),
|
|
upload: (filePath: string) => ipcRenderer.invoke(IpcChannel.File_Upload, filePath),
|
|
delete: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_Delete, fileId),
|
|
read: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_Read, fileId),
|
|
clear: () => ipcRenderer.invoke(IpcChannel.File_Clear),
|
|
get: (filePath: string) => ipcRenderer.invoke(IpcChannel.File_Get, filePath),
|
|
create: (fileName: string) => ipcRenderer.invoke(IpcChannel.File_Create, fileName),
|
|
write: (filePath: string, data: Uint8Array | string) => ipcRenderer.invoke(IpcChannel.File_Write, filePath, data),
|
|
open: (options?: { decompress: boolean }) => ipcRenderer.invoke(IpcChannel.File_Open, options),
|
|
openPath: (path: string) => ipcRenderer.invoke(IpcChannel.File_OpenPath, path),
|
|
save: (path: string, content: string, options?: { compress: boolean }) =>
|
|
ipcRenderer.invoke(IpcChannel.File_Save, path, content, options),
|
|
selectFolder: () => ipcRenderer.invoke(IpcChannel.File_SelectFolder),
|
|
saveImage: (name: string, data: string) => ipcRenderer.invoke(IpcChannel.File_SaveImage, name, data),
|
|
base64Image: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_Base64Image, fileId),
|
|
download: (url: string) => ipcRenderer.invoke(IpcChannel.File_Download, url),
|
|
copy: (fileId: string, destPath: string) => ipcRenderer.invoke(IpcChannel.File_Copy, fileId, destPath),
|
|
binaryFile: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_BinaryFile, fileId)
|
|
},
|
|
fs: {
|
|
read: (path: string) => ipcRenderer.invoke(IpcChannel.Fs_Read, path)
|
|
},
|
|
export: {
|
|
toWord: (markdown: string, fileName: string) => ipcRenderer.invoke(IpcChannel.Export_Word, markdown, fileName)
|
|
},
|
|
openPath: (path: string) => ipcRenderer.invoke(IpcChannel.Open_Path, path),
|
|
shortcuts: {
|
|
update: (shortcuts: Shortcut[]) => ipcRenderer.invoke(IpcChannel.Shortcuts_Update, shortcuts)
|
|
},
|
|
knowledgeBase: {
|
|
create: (base: KnowledgeBaseParams) => ipcRenderer.invoke(IpcChannel.KnowledgeBase_Create, base),
|
|
reset: (base: KnowledgeBaseParams) => ipcRenderer.invoke(IpcChannel.KnowledgeBase_Reset, base),
|
|
delete: (id: string) => ipcRenderer.invoke(IpcChannel.KnowledgeBase_Delete, id),
|
|
add: ({
|
|
base,
|
|
item,
|
|
forceReload = false
|
|
}: {
|
|
base: KnowledgeBaseParams
|
|
item: KnowledgeItem
|
|
forceReload?: boolean
|
|
}) => ipcRenderer.invoke(IpcChannel.KnowledgeBase_Add, { base, item, forceReload }),
|
|
remove: ({ uniqueId, uniqueIds, base }: { uniqueId: string; uniqueIds: string[]; base: KnowledgeBaseParams }) =>
|
|
ipcRenderer.invoke(IpcChannel.KnowledgeBase_Remove, { uniqueId, uniqueIds, base }),
|
|
search: ({ search, base }: { search: string; base: KnowledgeBaseParams }) =>
|
|
ipcRenderer.invoke(IpcChannel.KnowledgeBase_Search, { search, base }),
|
|
rerank: ({ search, base, results }: { search: string; base: KnowledgeBaseParams; results: ExtractChunkData[] }) =>
|
|
ipcRenderer.invoke(IpcChannel.KnowledgeBase_Rerank, { search, base, results })
|
|
},
|
|
window: {
|
|
setMinimumSize: (width: number, height: number) =>
|
|
ipcRenderer.invoke(IpcChannel.Windows_SetMinimumSize, width, height),
|
|
resetMinimumSize: () => ipcRenderer.invoke(IpcChannel.Windows_ResetMinimumSize)
|
|
},
|
|
gemini: {
|
|
uploadFile: (file: FileType, apiKey: string) => ipcRenderer.invoke(IpcChannel.Gemini_UploadFile, file, apiKey),
|
|
base64File: (file: FileType) => ipcRenderer.invoke(IpcChannel.Gemini_Base64File, file),
|
|
retrieveFile: (file: FileType, apiKey: string) => ipcRenderer.invoke(IpcChannel.Gemini_RetrieveFile, file, apiKey),
|
|
listFiles: (apiKey: string) => ipcRenderer.invoke(IpcChannel.Gemini_ListFiles, apiKey),
|
|
deleteFile: (apiKey: string, fileId: string) => ipcRenderer.invoke(IpcChannel.Gemini_DeleteFile, apiKey, fileId)
|
|
},
|
|
selectionMenu: {
|
|
action: (action: string) => ipcRenderer.invoke(IpcChannel.SelectionMenu_Action, action)
|
|
},
|
|
config: {
|
|
set: (key: string, value: any) => ipcRenderer.invoke(IpcChannel.Config_Set, key, value),
|
|
get: (key: string) => ipcRenderer.invoke(IpcChannel.Config_Get, key)
|
|
},
|
|
miniWindow: {
|
|
show: () => ipcRenderer.invoke(IpcChannel.MiniWindow_Show),
|
|
hide: () => ipcRenderer.invoke(IpcChannel.MiniWindow_Hide),
|
|
close: () => ipcRenderer.invoke(IpcChannel.MiniWindow_Close),
|
|
toggle: () => ipcRenderer.invoke(IpcChannel.MiniWindow_Toggle)
|
|
setPin: (isPinned: boolean) => ipcRenderer.invoke(IpcChannel.MiniWindow_SetPin, isPinned)
|
|
},
|
|
aes: {
|
|
encrypt: (text: string, secretKey: string, iv: string) =>
|
|
ipcRenderer.invoke(IpcChannel.Aes_Encrypt, text, secretKey, iv),
|
|
decrypt: (encryptedData: string, iv: string, secretKey: string) =>
|
|
ipcRenderer.invoke(IpcChannel.Aes_Decrypt, encryptedData, iv, secretKey)
|
|
},
|
|
mcp: {
|
|
removeServer: (server: MCPServer) => ipcRenderer.invoke(IpcChannel.Mcp_RemoveServer, server),
|
|
restartServer: (server: MCPServer) => ipcRenderer.invoke(IpcChannel.Mcp_RestartServer, server),
|
|
stopServer: (server: MCPServer) => ipcRenderer.invoke(IpcChannel.Mcp_StopServer, server),
|
|
listTools: (server: MCPServer) => ipcRenderer.invoke(IpcChannel.Mcp_ListTools, server),
|
|
callTool: ({ server, name, args }: { server: MCPServer; name: string; args: any }) =>
|
|
ipcRenderer.invoke(IpcChannel.Mcp_CallTool, { server, name, args }),
|
|
getInstallInfo: () => ipcRenderer.invoke(IpcChannel.Mcp_GetInstallInfo)
|
|
},
|
|
shell: {
|
|
openExternal: shell.openExternal
|
|
},
|
|
copilot: {
|
|
getAuthMessage: (headers?: Record<string, string>) =>
|
|
ipcRenderer.invoke(IpcChannel.Copilot_GetAuthMessage, headers),
|
|
getCopilotToken: (device_code: string, headers?: Record<string, string>) =>
|
|
ipcRenderer.invoke(IpcChannel.Copilot_GetCopilotToken, device_code, headers),
|
|
saveCopilotToken: (access_token: string) => ipcRenderer.invoke(IpcChannel.Copilot_SaveCopilotToken, access_token),
|
|
getToken: (headers?: Record<string, string>) => ipcRenderer.invoke(IpcChannel.Copilot_GetToken, headers),
|
|
logout: () => ipcRenderer.invoke(IpcChannel.Copilot_Logout),
|
|
getUser: (token: string) => ipcRenderer.invoke(IpcChannel.Copilot_GetUser, token)
|
|
},
|
|
|
|
// Binary related APIs
|
|
isBinaryExist: (name: string) => ipcRenderer.invoke(IpcChannel.App_IsBinaryExist, name),
|
|
getBinaryPath: (name: string) => ipcRenderer.invoke(IpcChannel.App_GetBinaryPath, name),
|
|
installUVBinary: () => ipcRenderer.invoke(IpcChannel.App_InstallUvBinary),
|
|
installBunBinary: () => ipcRenderer.invoke(IpcChannel.App_InstallBunBinary)
|
|
protocol: {
|
|
onReceiveData: (callback: (data: { url: string; params: any }) => void) => {
|
|
const listener = (_event: Electron.IpcRendererEvent, data: { url: string; params: any }) => {
|
|
callback(data)
|
|
}
|
|
ipcRenderer.on('protocol-data', listener)
|
|
return () => {
|
|
ipcRenderer.off('protocol-data', listener)
|
|
}
|
|
}
|
|
},
|
|
nutstore: {
|
|
getSSOUrl: () => ipcRenderer.invoke(IpcChannel.Nutstore_GetSsoUrl),
|
|
decryptToken: (token: string) => ipcRenderer.invoke(IpcChannel.Nutstore_DecryptToken, token),
|
|
getDirectoryContents: (token: string, path: string) =>
|
|
ipcRenderer.invoke(IpcChannel.Nutstore_GetDirectoryContents, token, path)
|
|
}
|
|
}
|
|
|
|
// Use `contextBridge` APIs to expose Electron APIs to
|
|
// renderer only if context isolation is enabled, otherwise
|
|
// just add to the DOM global.
|
|
if (process.contextIsolated) {
|
|
try {
|
|
contextBridge.exposeInMainWorld('electron', electronAPI)
|
|
contextBridge.exposeInMainWorld('api', api)
|
|
contextBridge.exposeInMainWorld('obsidian', {
|
|
getVaults: () => ipcRenderer.invoke(IpcChannel.Obsidian_GetVaults),
|
|
getFolders: (vaultName: string) => ipcRenderer.invoke(IpcChannel.Obsidian_GetFiles, vaultName),
|
|
getFiles: (vaultName: string) => ipcRenderer.invoke(IpcChannel.Obsidian_GetFiles, vaultName)
|
|
})
|
|
} catch (error) {
|
|
console.error(error)
|
|
}
|
|
} else {
|
|
// @ts-ignore (define in dts)
|
|
window.electron = electronAPI
|
|
// @ts-ignore (define in dts)
|
|
window.api = api
|
|
}
|