chore: should have an option to not revalidate hardware information

This commit is contained in:
Louis 2025-03-21 10:32:56 +07:00
parent 8c0f88fb4e
commit 1d4567082b
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2
4 changed files with 41 additions and 32 deletions

View File

@ -9,24 +9,26 @@ import PQueue from 'p-queue'
export default class JSONHardwareManagementExtension extends HardwareManagementExtension { export default class JSONHardwareManagementExtension extends HardwareManagementExtension {
queue = new PQueue({ concurrency: 1 }) queue = new PQueue({ concurrency: 1 })
/**
* Extended API instance for making requests to the Cortex API.
* @returns
*/
api: KyInstance
/** /**
* Called when the extension is loaded. * Called when the extension is loaded.
*/ */
async onLoad() { async onLoad() {
const apiKey = await window.core?.api.appToken() ?? 'cortex.cpp' // Run Healthcheck
this.api = ky.extend({ this.queue.add(() => this.healthz())
}
/**
* Get the API instance
* @returns
*/
async apiInstance(): Promise<KyInstance> {
const apiKey = (await window.core?.api.appToken()) ?? 'cortex.cpp'
return ky.extend({
prefixUrl: API_URL, prefixUrl: API_URL,
headers: { headers: {
Authorization: `Bearer ${apiKey}`, Authorization: `Bearer ${apiKey}`,
}, },
}) })
// Run Healthcheck
this.queue.add(() => this.healthz())
} }
/** /**
@ -39,11 +41,13 @@ export default class JSONHardwareManagementExtension extends HardwareManagementE
* @returns * @returns
*/ */
async healthz(): Promise<void> { async healthz(): Promise<void> {
return this.api return this.apiInstance().then((api) =>
api
.get('healthz', { .get('healthz', {
retry: { limit: 20, delay: () => 500, methods: ['get'] }, retry: { limit: 20, delay: () => 500, methods: ['get'] },
}) })
.then(() => {}) .then(() => {})
)
} }
/** /**
@ -51,10 +55,12 @@ export default class JSONHardwareManagementExtension extends HardwareManagementE
*/ */
async getHardware(): Promise<HardwareInformation> { async getHardware(): Promise<HardwareInformation> {
return this.queue.add(() => return this.queue.add(() =>
this.api this.apiInstance().then((api) =>
api
.get('v1/hardware') .get('v1/hardware')
.json<HardwareInformation>() .json<HardwareInformation>()
.then((e) => e) .then((e) => e)
)
) as Promise<HardwareInformation> ) as Promise<HardwareInformation>
} }
@ -66,7 +72,9 @@ export default class JSONHardwareManagementExtension extends HardwareManagementE
activated_gpus: number[] activated_gpus: number[]
}> { }> {
return this.queue.add(() => return this.queue.add(() =>
this.api.post('v1/hardware/activate', { json: data }).then((e) => e) this.apiInstance().then((api) =>
api.post('v1/hardware/activate', { json: data }).then((e) => e)
)
) as Promise<{ ) as Promise<{
message: string message: string
activated_gpus: number[] activated_gpus: number[]

View File

@ -95,7 +95,7 @@ export default class JanModelExtension extends ModelExtension {
*/ */
return this.queue.add(() => return this.queue.add(() =>
this.api this.api
.post('v1/models/pull', { json: { model, id, name } }) .post('v1/models/pull', { json: { model, id, name }, timeout: false })
.json() .json()
.catch(async (e) => { .catch(async (e) => {
throw (await e.response?.json()) ?? e throw (await e.response?.json()) ?? e
@ -232,7 +232,10 @@ export default class JanModelExtension extends ModelExtension {
return this.queue return this.queue
.add(() => .add(() =>
this.api this.api
.patch(`v1/models/${model.id}`, { json: { ...model } }) .patch(`v1/models/${model.id}`, {
json: { ...model },
timeout: false,
})
.json() .json()
.then() .then()
) )
@ -267,6 +270,7 @@ export default class JanModelExtension extends ModelExtension {
this.api this.api
.post('v1/models/import', { .post('v1/models/import', {
json: { model, modelPath, name, option }, json: { model, modelPath, name, option },
timeout: false,
}) })
.json() .json()
.catch((e) => console.debug(e)) // Ignore error .catch((e) => console.debug(e)) // Ignore error
@ -313,6 +317,7 @@ export default class JanModelExtension extends ModelExtension {
json: { json: {
source, source,
}, },
timeout: false,
}) })
) )
} }
@ -382,11 +387,7 @@ export default class JanModelExtension extends ModelExtension {
[key: string]: any [key: string]: any
}): Promise<void> { }): Promise<void> {
return this.queue return this.queue
.add(() => .add(() => this.api.patch('v1/configs', { json: body }).then(() => {}))
this.api
.patch('v1/configs', { json: body })
.then(() => {})
)
.catch((e) => console.debug(e)) .catch((e) => console.debug(e))
} }

View File

@ -35,7 +35,7 @@ const DataLoader: React.FC = () => {
const setJanSettingScreen = useSetAtom(janSettingScreenAtom) const setJanSettingScreen = useSetAtom(janSettingScreenAtom)
const { getData: loadModels } = useModels() const { getData: loadModels } = useModels()
const { mutate } = useGetEngines() const { mutate } = useGetEngines()
const { mutate: getHardwareInfo } = useGetHardwareInfo() const { mutate: getHardwareInfo } = useGetHardwareInfo(false)
useThreads() useThreads()
useAssistants() useAssistants()

View File

@ -32,7 +32,7 @@ const getExtension = () =>
/** /**
* @returns A Promise that resolves to an object of list engines. * @returns A Promise that resolves to an object of list engines.
*/ */
export function useGetHardwareInfo() { export function useGetHardwareInfo(updatePeriodically: boolean = true) {
const setCpuUsage = useSetAtom(cpuUsageAtom) const setCpuUsage = useSetAtom(cpuUsageAtom)
const setUsedRam = useSetAtom(usedRamAtom) const setUsedRam = useSetAtom(usedRamAtom)
const setTotalRam = useSetAtom(totalRamAtom) const setTotalRam = useSetAtom(totalRamAtom)
@ -56,7 +56,7 @@ export function useGetHardwareInfo() {
{ {
revalidateOnFocus: false, revalidateOnFocus: false,
revalidateOnReconnect: false, revalidateOnReconnect: false,
refreshInterval: 2000, refreshInterval: updatePeriodically ? 2000 : undefined,
} }
) )