jan/web/hooks/useHardwareManagement.ts
Louis 9d64e9bef4
chore: merge release/v0.5.16 into dev (#4833)
* chore: bump cortex 1.0.11-rc10

* chore: bump to latest cortex release

* feat: Cortex API Authorization

* chore: correct CI CD repo name

* chore: correct new menloresearch repo name

* feat: rotate api token for each run (#4820)

* feat: rotate api token for each run

* chore: correct github repo url

* chore: correct github api url

* chore: should not filter out models first launch

* chore: bump cortex release

* chore: should get hardware information on launch (#4821)

* chore: should have an option to not revalidate hardware information

* chore: cortex.cpp gpu activation could cause a race condition (#4825)

* fix: jan beta logo displayed in jan release (#4828)

---------

Co-authored-by: David <davidpt.janai@gmail.com>
Co-authored-by: Nguyen Ngoc Minh <91668012+Minh141120@users.noreply.github.com>
2025-03-24 11:53:30 +07:00

100 lines
2.4 KiB
TypeScript

import { useMemo } from 'react'
import { ExtensionTypeEnum, HardwareManagementExtension } from '@janhq/core'
import { useSetAtom } from 'jotai'
import useSWR from 'swr'
import { extensionManager } from '@/extension/ExtensionManager'
import {
cpuUsageAtom,
ramUtilitizedAtom,
totalRamAtom,
usedRamAtom,
} from '@/helpers/atoms/SystemBar.atom'
// fetcher function
async function fetchExtensionData<T>(
extension: HardwareManagementExtension | null,
method: (extension: HardwareManagementExtension) => Promise<T>
): Promise<T> {
if (!extension) {
throw new Error('Extension not found')
}
return method(extension)
}
const getExtension = () =>
extensionManager.get<HardwareManagementExtension>(
ExtensionTypeEnum.Hardware
) ?? null
/**
* @returns A Promise that resolves to an object of list engines.
*/
export function useGetHardwareInfo(updatePeriodically: boolean = true) {
const setCpuUsage = useSetAtom(cpuUsageAtom)
const setUsedRam = useSetAtom(usedRamAtom)
const setTotalRam = useSetAtom(totalRamAtom)
const setRamUtilitized = useSetAtom(ramUtilitizedAtom)
const extension = useMemo(
() =>
extensionManager.get<HardwareManagementExtension>(
ExtensionTypeEnum.Hardware
) ?? null,
[]
)
const {
data: hardware,
error,
mutate,
} = useSWR(
extension ? 'hardware' : null,
() => fetchExtensionData(extension, (ext) => ext.getHardware()),
{
revalidateOnFocus: false,
revalidateOnReconnect: false,
refreshInterval: updatePeriodically ? 2000 : undefined,
}
)
const usedMemory =
Number(hardware?.ram.total) - Number(hardware?.ram.available)
if (hardware?.ram?.total && hardware?.ram?.available)
setUsedRam(Number(usedMemory))
if (hardware?.ram?.total) setTotalRam(hardware.ram.total)
const ramUtilitized =
((Number(usedMemory) ?? 0) / (hardware?.ram.total ?? 1)) * 100
setRamUtilitized(Math.round(ramUtilitized))
setCpuUsage(Math.round(hardware?.cpu.usage ?? 0))
return { hardware, error, mutate }
}
/**
* set gpus activate
* @returns A Promise that resolves set gpus activate.
*/
export const setActiveGpus = async (data: { gpus: number[] }) => {
const extension = getExtension()
if (!extension) {
throw new Error('Extension is not available')
}
try {
const response = await extension.setAvtiveGpu(data)
return response
} catch (error) {
console.error('Failed to install engine variant:', error)
throw error
}
}