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: gate quick ask with feature toggle #2331

Merged
merged 1 commit into from
Mar 12, 2024
Merged
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
2 changes: 1 addition & 1 deletion core/src/node/helper/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 4,13 @@ import fs from 'fs'
import os from 'os'
import childProcess from 'child_process'

// TODO: move this to core
const configurationFileName = 'settings.json'

// TODO: do no specify app name in framework module
const defaultJanDataFolder = join(os.homedir(), 'jan')
const defaultAppConfig: AppConfiguration = {
data_folder: defaultJanDataFolder,
quick_ask: false,
}

/**
Expand Down
1 change: 1 addition & 0 deletions core/src/types/config/appConfigEntity.ts
Original file line number Diff line number Diff line change
@@ -1,3 1,4 @@
export type AppConfiguration = {
data_folder: string
quick_ask: boolean
}
13 changes: 12 additions & 1 deletion electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 5,7 @@ import { join } from 'path'
* Managers
**/
import { windowManager } from './managers/window'
import { log } from '@janhq/core/node'
import { getAppConfigurations, log } from '@janhq/core/node'

/**
* IPC Handlers
Expand Down Expand Up @@ -95,7 95,15 @@ app.once('quit', () => {
cleanUpAndQuit()
})

app.once('window-all-closed', () => {
// Feature Toggle for Quick Ask
if (getAppConfigurations().quick_ask) return
cleanUpAndQuit()
})

function createQuickAskWindow() {
// Feature Toggle for Quick Ask
if (!getAppConfigurations().quick_ask) return
const startUrl = app.isPackaged ? `file://${quickAskPath}` : quickAskUrl
windowManager.createQuickAskWindow(preloadPath, startUrl)
}
Expand All @@ -107,6 115,9 @@ function createMainWindow() {

function registerGlobalShortcuts() {
const ret = registerShortcut(quickAskHotKey, (selectedText: string) => {
// Feature Toggle for Quick Ask
if (!getAppConfigurations().quick_ask) return

if (!windowManager.isQuickAskWindowVisible()) {
windowManager.showQuickAskWindow()
windowManager.sendQuickAskSelectedText(selectedText)
Expand Down
4 changes: 4 additions & 0 deletions electron/managers/tray.ts
Original file line number Diff line number Diff line change
@@ -1,11 1,15 @@
import { join } from 'path'
import { Tray, app, Menu } from 'electron'
import { windowManager } from '../managers/window'
import { getAppConfigurations } from '@janhq/core/node'

class TrayManager {
currentTray: Tray | undefined

createSystemTray = () => {
// Feature Toggle for Quick Ask
if (!getAppConfigurations().quick_ask) return

if (this.currentTray) {
return
}
Expand Down
4 changes: 4 additions & 0 deletions electron/managers/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,7 @@ import { BrowserWindow, app, shell } from 'electron'
import { quickAskWindowConfig } from './quickAskWindowConfig'
import { AppEvent } from '@janhq/core'
import { mainWindowConfig } from './mainWindowConfig'
import { getAppConfigurations } from '@janhq/core/node'

/**
* Manages the current window instance.
Expand Down Expand Up @@ -43,6 44,9 @@ class WindowManager {
})

windowManager.mainWindow?.on('close', function (evt) {
// Feature Toggle for Quick Ask
if (!getAppConfigurations().quick_ask) return

if (!isAppQuitting) {
evt.preventDefault()
windowManager.hideMainWindow()
Expand Down
9 changes: 7 additions & 2 deletions web/containers/Providers/DataLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 10,18 @@ import useGetSystemResources from '@/hooks/useGetSystemResources'
import useModels from '@/hooks/useModels'
import useThreads from '@/hooks/useThreads'

import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom'
import {
janDataFolderPathAtom,
quickAskEnabledAtom,
} from '@/helpers/atoms/AppConfig.atom'

type Props = {
children: ReactNode
}

const DataLoader: React.FC<Props> = ({ children }) => {
const setJanDataFolderPath = useSetAtom(janDataFolderPathAtom)
const setQuickAskEnabled = useSetAtom(quickAskEnabledAtom)

useModels()
useThreads()
Expand All @@ -29,8 33,9 @@ const DataLoader: React.FC<Props> = ({ children }) => {
?.getAppConfigurations()
?.then((appConfig: AppConfiguration) => {
setJanDataFolderPath(appConfig.data_folder)
setQuickAskEnabled(appConfig.quick_ask)
})
}, [setJanDataFolderPath])
}, [setJanDataFolderPath, setQuickAskEnabled])

console.debug('Load Data...')

Expand Down
2 changes: 2 additions & 0 deletions web/helpers/atoms/AppConfig.atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 6,7 @@ const PROXY_FEATURE_ENABLED = 'proxyFeatureEnabled'
const VULKAN_ENABLED = 'vulkanEnabled'
const IGNORE_SSL = 'ignoreSSLFeature'
const HTTPS_PROXY_FEATURE = 'httpsProxyFeature'
const QUICK_ASK_ENABLED = 'quickAskEnabled'

export const janDataFolderPathAtom = atom('')

Expand All @@ -19,3 20,4 @@ export const proxyAtom = atomWithStorage(HTTPS_PROXY_FEATURE, '')

export const ignoreSslAtom = atomWithStorage(IGNORE_SSL, false)
export const vulkanEnabledAtom = atomWithStorage(VULKAN_ENABLED, false)
export const quickAskEnabledAtom = atomWithStorage(QUICK_ASK_ENABLED, false)
1 change: 1 addition & 0 deletions web/hooks/useFactoryReset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 30,7 @@ export default function useFactoryReset() {
// set the default jan data folder to user's home directory
const configuration: AppConfiguration = {
data_folder: defaultJanDataFolder,
quick_ask: appConfiguration?.quick_ask ?? false,
}
await window.core?.api?.updateAppConfiguration(configuration)
}
Expand Down
45 changes: 42 additions & 3 deletions web/screens/Settings/Advanced/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,7 @@

import { useEffect, useState, useCallback, ChangeEvent } from 'react'

import { openExternalUrl, fs } from '@janhq/core'
import { openExternalUrl, fs, AppConfiguration } from '@janhq/core'

import {
Switch,
Expand All @@ -23,7 23,7 @@ import {
ScrollArea,
} from '@janhq/uikit'

import { useAtom } from 'jotai'
import { useAtom, useAtomValue } from 'jotai'
import { AlertTriangleIcon, AlertCircleIcon } from 'lucide-react'

import ShortcutModal from '@/containers/ShortcutModal'
Expand All @@ -42,6 42,7 @@ import {
proxyAtom,
proxyEnabledAtom,
vulkanEnabledAtom,
quickAskEnabledAtom,
} from '@/helpers/atoms/AppConfig.atom'

type GPU = {
Expand All @@ -56,6 57,8 @@ const Advanced = () => {
)
const [vulkanEnabled, setVulkanEnabled] = useAtom(vulkanEnabledAtom)
const [proxyEnabled, setProxyEnabled] = useAtom(proxyEnabledAtom)
const quickAskEnabled = useAtomValue(quickAskEnabledAtom)

const [proxy, setProxy] = useAtom(proxyAtom)
const [ignoreSSL, setIgnoreSSL] = useAtom(ignoreSslAtom)

Expand Down Expand Up @@ -87,6 90,14 @@ const Advanced = () => {
[setPartialProxy, setProxy]
)

const updateQuickAskEnabled = async (e: boolean) => {
const appConfiguration: AppConfiguration =
await window.core?.api?.getAppConfigurations()
appConfiguration.quick_ask = e
await window.core?.api?.updateAppConfiguration(appConfiguration)
window.core?.api?.relaunch()
}

useEffect(() => {
const setUseGpuIfPossible = async () => {
const settings = await readSettings()
Expand Down Expand Up @@ -361,7 372,7 @@ const Advanced = () => {
Vulkan Support
</h6>
</div>
<p className="text-xs leading-relaxed">
<p className="leading-relaxed">
Enable Vulkan with AMD GPU/APU and Intel Arc GPU for better
model performance (reload needed).
</p>
Expand Down Expand Up @@ -426,6 437,34 @@ const Advanced = () => {
/>
</div>

<div className="flex w-full items-start justify-between border-b border-border py-4 first:pt-0 last:border-none">
<div className="flex-shrink-0 space-y-1.5">
<div className="flex gap-x-2">
<h6 className="text-sm font-semibold capitalize">
Jan Quick Ask
</h6>
</div>
<p className="leading-relaxed">
Enable Quick Ask to be triggered via the default hotkey{' '}
<div className="inline-flex items-center justify-center rounded-full bg-secondary px-1 py-0.5 text-xs font-bold text-muted-foreground">
<span className="font-bold">{isMac ? '⌘' : 'Ctrl'} J</span>
</div>{' '}
(reload needed).
</p>
</div>
<Switch
checked={quickAskEnabled}
onCheckedChange={() => {
toaster({
title: 'Reload',
description:
'Quick Ask settings updated. Reload now to apply the changes.',
})
updateQuickAskEnabled(!quickAskEnabled)
}}
/>
</div>

{/* Clear log */}
<div className="flex w-full items-start justify-between border-b border-border py-4 first:pt-0 last:border-none">
<div className="flex-shrink-0 space-y-1.5">
Expand Down
Loading