feat: add HF token setting
This commit is contained in:
parent
9cea579c8e
commit
b8259e7794
@ -1,6 +1,6 @@
|
|||||||
import { invoke } from '@tauri-apps/api/core';
|
import { invoke } from '@tauri-apps/api/core'
|
||||||
import { listen } from '@tauri-apps/api/event';
|
import { listen } from '@tauri-apps/api/event'
|
||||||
import { BaseExtension, events } from '@janhq/core';
|
import { BaseExtension, events } from '@janhq/core'
|
||||||
|
|
||||||
export enum Settings {
|
export enum Settings {
|
||||||
hfToken = 'hf-token',
|
hfToken = 'hf-token',
|
||||||
@ -39,26 +39,36 @@ export default class DownloadManager extends BaseExtension {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onSettingUpdate<T>(key: string, value: T): void {
|
||||||
|
if (key === Settings.hfToken) {
|
||||||
|
this.hfToken = value as string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async downloadFiles(
|
async downloadFiles(
|
||||||
items: DownloadItem[],
|
items: DownloadItem[],
|
||||||
taskId: string,
|
taskId: string,
|
||||||
onProgress?: (transferred: number, total: number) => void
|
onProgress?: (transferred: number, total: number) => void
|
||||||
) {
|
) {
|
||||||
// relay tauri events to onProgress callback
|
// relay tauri events to onProgress callback
|
||||||
const unlisten = await listen<DownloadEvent>(`download-${taskId}`, (event) => {
|
const unlisten = await listen<DownloadEvent>(
|
||||||
|
`download-${taskId}`,
|
||||||
|
(event) => {
|
||||||
if (onProgress) {
|
if (onProgress) {
|
||||||
let payload = event.payload
|
let payload = event.payload
|
||||||
onProgress(payload.transferred, payload.total)
|
onProgress(payload.transferred, payload.total)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await invoke<void>(
|
await invoke<void>('download_files', {
|
||||||
"download_files",
|
items,
|
||||||
{ items, taskId, headers: this._getHeaders() },
|
taskId,
|
||||||
)
|
headers: this._getHeaders(),
|
||||||
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error downloading task", taskId, error)
|
console.error('Error downloading task', taskId, error)
|
||||||
throw error
|
throw error
|
||||||
} finally {
|
} finally {
|
||||||
unlisten()
|
unlisten()
|
||||||
@ -67,16 +77,16 @@ export default class DownloadManager extends BaseExtension {
|
|||||||
|
|
||||||
async cancelDownload(taskId: string) {
|
async cancelDownload(taskId: string) {
|
||||||
try {
|
try {
|
||||||
await invoke<void>("cancel_download_task", { taskId })
|
await invoke<void>('cancel_download_task', { taskId })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error cancelling download:", error)
|
console.error('Error cancelling download:', error)
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_getHeaders() {
|
_getHeaders() {
|
||||||
return {
|
return {
|
||||||
...(this.hfToken && { Authorization: `Bearer ${this.hfToken}` })
|
...(this.hfToken && { Authorization: `Bearer ${this.hfToken}` }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,14 @@
|
|||||||
import { create } from 'zustand'
|
import { create } from 'zustand'
|
||||||
import { persist, createJSONStorage } from 'zustand/middleware'
|
import { persist, createJSONStorage } from 'zustand/middleware'
|
||||||
import { localStorageKey } from '@/constants/localStorage'
|
import { localStorageKey } from '@/constants/localStorage'
|
||||||
|
import { ExtensionManager } from '@/lib/extension'
|
||||||
|
|
||||||
type LeftPanelStoreState = {
|
type LeftPanelStoreState = {
|
||||||
currentLanguage: Language
|
currentLanguage: Language
|
||||||
spellCheckChatInput: boolean
|
spellCheckChatInput: boolean
|
||||||
experimentalFeatures: boolean
|
experimentalFeatures: boolean
|
||||||
|
huggingfaceToken?: string
|
||||||
|
setHuggingfaceToken: (token: string) => void
|
||||||
setExperimentalFeatures: (value: boolean) => void
|
setExperimentalFeatures: (value: boolean) => void
|
||||||
setSpellCheckChatInput: (value: boolean) => void
|
setSpellCheckChatInput: (value: boolean) => void
|
||||||
setCurrentLanguage: (value: Language) => void
|
setCurrentLanguage: (value: Language) => void
|
||||||
@ -17,9 +20,29 @@ export const useGeneralSetting = create<LeftPanelStoreState>()(
|
|||||||
currentLanguage: 'en',
|
currentLanguage: 'en',
|
||||||
spellCheckChatInput: true,
|
spellCheckChatInput: true,
|
||||||
experimentalFeatures: false,
|
experimentalFeatures: false,
|
||||||
|
huggingfaceToken: undefined,
|
||||||
setExperimentalFeatures: (value) => set({ experimentalFeatures: value }),
|
setExperimentalFeatures: (value) => set({ experimentalFeatures: value }),
|
||||||
setSpellCheckChatInput: (value) => set({ spellCheckChatInput: value }),
|
setSpellCheckChatInput: (value) => set({ spellCheckChatInput: value }),
|
||||||
setCurrentLanguage: (value) => set({ currentLanguage: value }),
|
setCurrentLanguage: (value) => set({ currentLanguage: value }),
|
||||||
|
setHuggingfaceToken: (token) => {
|
||||||
|
set({ huggingfaceToken: token })
|
||||||
|
ExtensionManager.getInstance()
|
||||||
|
.getByName('@janhq/download-extension')
|
||||||
|
?.getSettings()
|
||||||
|
.then((settings) => {
|
||||||
|
if (settings) {
|
||||||
|
const newSettings = settings.map((e) => {
|
||||||
|
if (e.key === 'hf-token') {
|
||||||
|
e.controllerProps.value = token
|
||||||
|
}
|
||||||
|
return e
|
||||||
|
})
|
||||||
|
ExtensionManager.getInstance()
|
||||||
|
.getByName('@janhq/download-extension')
|
||||||
|
?.updateSettings(newSettings)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
name: localStorageKey.settingGeneral,
|
name: localStorageKey.settingGeneral,
|
||||||
|
|||||||
@ -212,6 +212,8 @@
|
|||||||
"factoryResetDesc": "This will reset all app settings to their defaults. This can't be undone. We only recommend this if the app is corrupted.",
|
"factoryResetDesc": "This will reset all app settings to their defaults. This can't be undone. We only recommend this if the app is corrupted.",
|
||||||
"cancel": "Cancel",
|
"cancel": "Cancel",
|
||||||
"reset": "Reset",
|
"reset": "Reset",
|
||||||
|
"huggingfaceToken": "HuggingFace Token",
|
||||||
|
"huggingfaceTokenDesc": "Your HuggingFace API token for accessing models.",
|
||||||
"resources": "Resources",
|
"resources": "Resources",
|
||||||
"documentation": "Documentation",
|
"documentation": "Documentation",
|
||||||
"documentationDesc": "Learn how to use Jan and explore its features.",
|
"documentationDesc": "Learn how to use Jan and explore its features.",
|
||||||
|
|||||||
@ -45,6 +45,7 @@ import { isDev } from '@/lib/utils'
|
|||||||
import { emit } from '@tauri-apps/api/event'
|
import { emit } from '@tauri-apps/api/event'
|
||||||
import { stopAllModels } from '@/services/models'
|
import { stopAllModels } from '@/services/models'
|
||||||
import { SystemEvent } from '@/types/events'
|
import { SystemEvent } from '@/types/events'
|
||||||
|
import { Input } from '@/components/ui/input'
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
export const Route = createFileRoute(route.settings.general as any)({
|
export const Route = createFileRoute(route.settings.general as any)({
|
||||||
@ -58,6 +59,8 @@ function General() {
|
|||||||
setSpellCheckChatInput,
|
setSpellCheckChatInput,
|
||||||
experimentalFeatures,
|
experimentalFeatures,
|
||||||
setExperimentalFeatures,
|
setExperimentalFeatures,
|
||||||
|
huggingfaceToken,
|
||||||
|
setHuggingfaceToken,
|
||||||
} = useGeneralSetting()
|
} = useGeneralSetting()
|
||||||
|
|
||||||
const openFileTitle = (): string => {
|
const openFileTitle = (): string => {
|
||||||
@ -245,20 +248,6 @@ function General() {
|
|||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
{/* Advanced */}
|
|
||||||
<Card title="Advanced">
|
|
||||||
<CardItem
|
|
||||||
title="Experimental Features"
|
|
||||||
description="Enable experimental features. They may be unstable or change at any time."
|
|
||||||
actions={
|
|
||||||
<Switch
|
|
||||||
checked={experimentalFeatures}
|
|
||||||
onCheckedChange={(e) => setExperimentalFeatures(e)}
|
|
||||||
/>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
{/* Data folder */}
|
{/* Data folder */}
|
||||||
<Card title={t('common:dataFolder')}>
|
<Card title={t('common:dataFolder')}>
|
||||||
<CardItem
|
<CardItem
|
||||||
@ -399,20 +388,15 @@ function General() {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
|
{/* Advanced */}
|
||||||
{/* Other */}
|
<Card title="Advanced">
|
||||||
<Card title={t('common:others')}>
|
|
||||||
<CardItem
|
<CardItem
|
||||||
title={t('settings:others.spellCheck', {
|
title="Experimental Features"
|
||||||
ns: 'settings',
|
description="Enable experimental features. They may be unstable or change at any time."
|
||||||
})}
|
|
||||||
description={t('settings:others.spellCheckDesc', {
|
|
||||||
ns: 'settings',
|
|
||||||
})}
|
|
||||||
actions={
|
actions={
|
||||||
<Switch
|
<Switch
|
||||||
checked={spellCheckChatInput}
|
checked={experimentalFeatures}
|
||||||
onCheckedChange={(e) => setSpellCheckChatInput(e)}
|
onCheckedChange={(e) => setExperimentalFeatures(e)}
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
@ -464,6 +448,41 @@ function General() {
|
|||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
|
{/* Other */}
|
||||||
|
<Card title={t('common:others')}>
|
||||||
|
<CardItem
|
||||||
|
title={t('settings:others.spellCheck', {
|
||||||
|
ns: 'settings',
|
||||||
|
})}
|
||||||
|
description={t('settings:others.spellCheckDesc', {
|
||||||
|
ns: 'settings',
|
||||||
|
})}
|
||||||
|
actions={
|
||||||
|
<Switch
|
||||||
|
checked={spellCheckChatInput}
|
||||||
|
onCheckedChange={(e) => setSpellCheckChatInput(e)}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
<CardItem
|
||||||
|
title={t('settings:general.huggingfaceToken', {
|
||||||
|
ns: 'settings',
|
||||||
|
})}
|
||||||
|
description={t('settings:general.huggingfaceTokenDesc', {
|
||||||
|
ns: 'settings',
|
||||||
|
})}
|
||||||
|
actions={
|
||||||
|
<Input
|
||||||
|
id="hf-token"
|
||||||
|
value={huggingfaceToken || ''}
|
||||||
|
onChange={(e) => setHuggingfaceToken(e.target.value)}
|
||||||
|
placeholder={'hf_xxx'}
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</Card>
|
||||||
|
|
||||||
{/* Resources */}
|
{/* Resources */}
|
||||||
<Card title={t('settings:general.resources')}>
|
<Card title={t('settings:general.resources')}>
|
||||||
<CardItem
|
<CardItem
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user