From 1d4567082b04b897fbb741511737be37ef6b50bf Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 21 Mar 2025 10:32:56 +0700 Subject: [PATCH] chore: should have an option to not revalidate hardware information --- .../src/index.ts | 52 +++++++++++-------- extensions/model-extension/src/index.ts | 15 +++--- web/containers/Providers/DataLoader.tsx | 2 +- web/hooks/useHardwareManagement.ts | 4 +- 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/extensions/hardware-management-extension/src/index.ts b/extensions/hardware-management-extension/src/index.ts index 849b6206f..472b65c07 100644 --- a/extensions/hardware-management-extension/src/index.ts +++ b/extensions/hardware-management-extension/src/index.ts @@ -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 { + 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 { - 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 { return this.queue.add(() => - this.api - .get('v1/hardware') - .json() - .then((e) => e) + this.apiInstance().then((api) => + api + .get('v1/hardware') + .json() + .then((e) => e) + ) ) as Promise } @@ -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[] diff --git a/extensions/model-extension/src/index.ts b/extensions/model-extension/src/index.ts index aaa010738..8618103db 100644 --- a/extensions/model-extension/src/index.ts +++ b/extensions/model-extension/src/index.ts @@ -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 { 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)) } diff --git a/web/containers/Providers/DataLoader.tsx b/web/containers/Providers/DataLoader.tsx index 251f239c7..1174150f4 100644 --- a/web/containers/Providers/DataLoader.tsx +++ b/web/containers/Providers/DataLoader.tsx @@ -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() diff --git a/web/hooks/useHardwareManagement.ts b/web/hooks/useHardwareManagement.ts index d39b3c1fc..90dbdb2b5 100644 --- a/web/hooks/useHardwareManagement.ts +++ b/web/hooks/useHardwareManagement.ts @@ -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, } )