Merge pull request #4822 from menloresearch/chore/add-option-to-not-revalidate-hardware-infor

chore: should have an option to not revalidate hardware information
This commit is contained in:
David 2025-03-21 10:46:05 +07:00 committed by GitHub
commit 2271c8d3d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 41 additions and 32 deletions

View File

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

View File

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

View File

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

View File

@ -32,7 +32,7 @@ const getExtension = () =>
/**
* @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 setUsedRam = useSetAtom(usedRamAtom)
const setTotalRam = useSetAtom(totalRamAtom)
@ -56,7 +56,7 @@ export function useGetHardwareInfo() {
{
revalidateOnFocus: false,
revalidateOnReconnect: false,
refreshInterval: 2000,
refreshInterval: updatePeriodically ? 2000 : undefined,
}
)