From 01964d3d032d72916efbfc772441b0ecb072b34b Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 27 Mar 2025 15:21:13 +0700 Subject: [PATCH] chore: maintain electron build --- core/src/browser/core.ts | 6 +++- core/src/node/api/processors/app.ts | 16 +++++++++-- core/src/node/api/processors/fs.ts | 14 ++++++---- core/src/node/helper/config.ts | 28 ++++++------------- core/src/types/api/index.ts | 1 + .../engine-management-extension/src/index.ts | 2 +- .../inference-cortex-extension/src/index.ts | 3 ++ src-tauri/src/core/cmd.rs | 14 ++++++++++ web/hooks/useLoadTheme.ts | 13 ++++----- web/hooks/useSendChatMessage.ts | 1 - 10 files changed, 59 insertions(+), 39 deletions(-) diff --git a/core/src/browser/core.ts b/core/src/browser/core.ts index cf832e4d8..3025ba963 100644 --- a/core/src/browser/core.ts +++ b/core/src/browser/core.ts @@ -13,7 +13,11 @@ const executeOnMain: (extension: string, method: string, ...args: any[]) => Prom extension, method, ...args -) => globalThis.core?.api?.invokeExtensionFunc(extension, method, ...args) +) => { + if ('electronAPI' in window && window.electronAPI) + return globalThis.core?.api?.invokeExtensionFunc(extension, method, ...args) + return () => {} +} /** * Gets Jan's data folder path. diff --git a/core/src/node/api/processors/app.ts b/core/src/node/api/processors/app.ts index 2ee83e63f..4fbd8648f 100644 --- a/core/src/node/api/processors/app.ts +++ b/core/src/node/api/processors/app.ts @@ -8,7 +8,7 @@ import { normalizeFilePath, getJanDataFolderPath, } from '../../helper' -import { readdirSync } from 'fs' +import { readdirSync, readFileSync } from 'fs' export class App implements Processor { observer?: Function @@ -26,8 +26,8 @@ export class App implements Processor { /** * Joins multiple paths together, respect to the current OS. */ - joinPath(args: any[]) { - return join(...args) + joinPath(args: any) { + return join(...('args' in args ? args.args : args)) } /** @@ -86,6 +86,16 @@ export class App implements Processor { return readdirSync(themesPath) } + /** + * Read theme.json + * @param theme + * @returns + */ + readTheme({ theme }: { theme: string }) { + const themePath = join(getJanDataFolderPath(), 'themes', theme, 'theme.json') + return readFileSync(themePath, { encoding: 'utf-8' }) + } + async updateAppConfiguration(args: any) { await updateAppConfiguration(args) } diff --git a/core/src/node/api/processors/fs.ts b/core/src/node/api/processors/fs.ts index ada744d53..7bc5f1e20 100644 --- a/core/src/node/api/processors/fs.ts +++ b/core/src/node/api/processors/fs.ts @@ -21,18 +21,21 @@ export class FileSystem implements Processor { return import(FileSystem.moduleName).then((mdl) => mdl[route]( ...args.map((arg: any, index: number) => { - if(index !== 0) { + const arg0 = args[0] + if ('args' in arg0) arg = arg0.args + if (Array.isArray(arg)) arg = arg[0] + if (index !== 0) { return arg } if (index === 0 && typeof arg !== 'string') { throw new Error(`Invalid argument ${JSON.stringify(args)}`) } const path = - (arg.startsWith(`file:/`) || arg.startsWith(`file:\\`)) - ? join(getJanDataFolderPath(), normalizeFilePath(arg)) - : arg + arg.startsWith(`file:/`) || arg.startsWith(`file:\\`) + ? join(getJanDataFolderPath(), normalizeFilePath(arg)) + : arg - if(path.startsWith(`http://`) || path.startsWith(`https://`)) { + if (path.startsWith(`http://`) || path.startsWith(`https://`)) { return path } const absolutePath = resolve(path) @@ -88,5 +91,4 @@ export class FileSystem implements Processor { }) }) } - } diff --git a/core/src/node/helper/config.ts b/core/src/node/helper/config.ts index 6fb28d01f..89955a2d6 100644 --- a/core/src/node/helper/config.ts +++ b/core/src/node/helper/config.ts @@ -18,9 +18,7 @@ export const getAppConfigurations = (): AppConfiguration => { if (!fs.existsSync(configurationFile)) { // create default app config if we don't have one - console.debug( - `App config not found, creating default config at ${configurationFile}` - ) + console.debug(`App config not found, creating default config at ${configurationFile}`) fs.writeFileSync(configurationFile, JSON.stringify(appDefaultConfiguration)) return appDefaultConfiguration } @@ -31,28 +29,23 @@ export const getAppConfigurations = (): AppConfiguration => { ) return appConfigurations } catch (err) { - console.error( - `Failed to read app config, return default config instead! Err: ${err}` - ) + console.error(`Failed to read app config, return default config instead! Err: ${err}`) return defaultAppConfig() } } const getConfigurationFilePath = () => join( - global.core?.appPath() || - process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME'], + global.core?.appPath() || process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME'], configurationFileName ) -export const updateAppConfiguration = ( +export const updateAppConfiguration = ({ + configuration, +}: { configuration: AppConfiguration -): Promise => { +}): Promise => { const configurationFile = getConfigurationFilePath() - console.debug( - 'updateAppConfiguration, configurationFile: ', - configurationFile - ) fs.writeFileSync(configurationFile, JSON.stringify(configuration)) return Promise.resolve() @@ -87,14 +80,11 @@ export const getJanExtensionsPath = (): string => { */ export const defaultAppConfig = (): AppConfiguration => { const { app } = require('electron') - const defaultJanDataFolder = join( - app?.getPath('userData') ?? os?.homedir() ?? '', - 'data' - ) + const defaultJanDataFolder = join(app?.getPath('userData') ?? os?.homedir() ?? '', 'data') return { data_folder: process.env.CI === 'e2e' - ? (process.env.APP_CONFIG_PATH ?? resolve('./test-data')) + ? process.env.APP_CONFIG_PATH ?? resolve('./test-data') : defaultJanDataFolder, quick_ask: false, } diff --git a/core/src/types/api/index.ts b/core/src/types/api/index.ts index 5bae92d8e..d96ba6d06 100644 --- a/core/src/types/api/index.ts +++ b/core/src/types/api/index.ts @@ -52,6 +52,7 @@ export enum AppRoute { systemInformation = 'systemInformation', showToast = 'showToast', getThemes = 'getThemes', + readTheme = 'readTheme' } export enum AppEvent { diff --git a/extensions/engine-management-extension/src/index.ts b/extensions/engine-management-extension/src/index.ts index 9af1f33ce..029c54646 100644 --- a/extensions/engine-management-extension/src/index.ts +++ b/extensions/engine-management-extension/src/index.ts @@ -52,7 +52,7 @@ export default class JanEngineManagementExtension extends EngineManagementExtens */ async onLoad() { // Symlink Engines Directory - // await executeOnMain(NODE, 'symlinkEngines') + await executeOnMain(NODE, 'symlinkEngines') // Update default local engine this.updateDefaultEngine() diff --git a/extensions/inference-cortex-extension/src/index.ts b/extensions/inference-cortex-extension/src/index.ts index 09de67076..33bd398c3 100644 --- a/extensions/inference-cortex-extension/src/index.ts +++ b/extensions/inference-cortex-extension/src/index.ts @@ -129,6 +129,8 @@ export default class JanInferenceCortexExtension extends LocalOAIEngine { ) if (!Number.isNaN(threads_number)) this.cpu_threads = threads_number + await executeOnMain(NODE, 'run') + this.subscribeToEvents() window.addEventListener('beforeunload', () => { @@ -140,6 +142,7 @@ export default class JanInferenceCortexExtension extends LocalOAIEngine { console.log('Clean up cortex.cpp services') this.shouldReconnect = false this.clean() + await executeOnMain(NODE, 'dispose') super.onUnload() } diff --git a/src-tauri/src/core/cmd.rs b/src-tauri/src/core/cmd.rs index f55c0308d..8539c54f8 100644 --- a/src-tauri/src/core/cmd.rs +++ b/src-tauri/src/core/cmd.rs @@ -120,6 +120,20 @@ pub fn get_themes(app_handle: tauri::AppHandle) -> Vec { themes } +#[tauri::command] +pub fn read_theme(app_handle: tauri::AppHandle, theme_name: String) -> Result { + let themes_path = get_jan_data_folder_path(app_handle) + .join("themes") + .join(theme_name.clone()) + .join("theme.json"); + if themes_path.exists() { + let content = fs::read_to_string(themes_path).map_err(|e| e.to_string())?; + Ok(content) + } else { + Err(format!("Theme {} not found", theme_name.clone())) + } +} + #[tauri::command] pub fn get_configuration_file_path(app_handle: tauri::AppHandle) -> PathBuf { let app_path = app_handle.path().app_data_dir().unwrap_or_else(|err| { diff --git a/web/hooks/useLoadTheme.ts b/web/hooks/useLoadTheme.ts index 305663992..61cf62d1b 100644 --- a/web/hooks/useLoadTheme.ts +++ b/web/hooks/useLoadTheme.ts @@ -2,8 +2,6 @@ import { useCallback, useEffect } from 'react' import { useTheme } from 'next-themes' -import { fs, joinPath } from '@janhq/core' - import { useAtom } from 'jotai' import cssVars from '@/utils/jsonToCssVariables' @@ -59,13 +57,12 @@ export const useLoadTheme = () => { setThemeOptions(themesOptions) if (!selectedIdTheme.length) return setSelectedIdTheme('joi-light') - const filePath = await joinPath([ - 'file://themes', - selectedIdTheme, - 'theme.json', - ]) - const theme: Theme = JSON.parse(await fs.readFileSync(filePath, 'utf-8')) + const theme: Theme = JSON.parse( + await window.core.api.readTheme({ + theme: selectedIdTheme, + }) + ) setThemeData(theme) setNativeTheme(theme.nativeTheme) diff --git a/web/hooks/useSendChatMessage.ts b/web/hooks/useSendChatMessage.ts index dbfe035a1..f42dbdc57 100644 --- a/web/hooks/useSendChatMessage.ts +++ b/web/hooks/useSendChatMessage.ts @@ -9,7 +9,6 @@ import { Model, ConversationalExtension, EngineManager, - ToolManager, ThreadAssistantInfo, InferenceEngine, } from '@janhq/core'