diff --git a/packages/backend/src/managers/modelsManager.ts b/packages/backend/src/managers/modelsManager.ts index 5d8e7cb20..e2665c31c 100644 --- a/packages/backend/src/managers/modelsManager.ts +++ b/packages/backend/src/managers/modelsManager.ts @@ -27,8 +27,8 @@ import * as podmanDesktopApi from '@podman-desktop/api'; import { Downloader } from '../utils/downloader'; import type { TaskRegistry } from '../registries/TaskRegistry'; import type { Task } from '@shared/src/models/ITask'; -import type { ProgressiveEvent } from '../models/progressiveEvent'; -import { isCompletionProgressiveEvent, isExecutingProgressiveEvent } from '../models/progressiveEvent'; +import type { BaseEvent } from '../models/baseEvent'; +import { isCompletionEvent, isProgressEvent } from '../models/baseEvent'; import { Uploader } from '../utils/uploader'; export class ModelsManager implements Disposable { @@ -204,7 +204,7 @@ export class ModelsManager implements Disposable { // If we have an existing downloader running we subscribe on its events return new Promise((resolve, reject) => { const disposable = existingDownloader.onEvent(event => { - if (!isCompletionProgressiveEvent(event)) return; + if (!isCompletionEvent(event)) return; switch (event.status) { case 'completed': @@ -218,7 +218,7 @@ export class ModelsManager implements Disposable { }); } - private onDownloadEvent(event: ProgressiveEvent): void { + private onDownloadEvent(event: BaseEvent): void { // Always use the task registry as source of truth for tasks const tasks = this.taskRegistry.getTasksByLabels({ 'model-pulling': event.id }); if (tasks.length === 0) { @@ -228,10 +228,10 @@ export class ModelsManager implements Disposable { } tasks.forEach(task => { - if (isExecutingProgressiveEvent(event)) { + if (isProgressEvent(event)) { task.state = 'loading'; task.progress = event.value; - } else if (isCompletionProgressiveEvent(event)) { + } else if (isCompletionEvent(event)) { // status error or canceled if (event.status === 'error' || event.status === 'canceled') { task.state = 'error'; @@ -315,11 +315,11 @@ export class ModelsManager implements Disposable { }); const uploader = new Uploader(localModelPath); - uploader.onEvent((event: ProgressiveEvent) => { - if (isExecutingProgressiveEvent(event)) { + uploader.onEvent((event: BaseEvent) => { + if (isProgressEvent(event)) { task.state = 'loading'; task.progress = event.value; - } else if (isCompletionProgressiveEvent(event)) { + } else if (isCompletionEvent(event)) { // status error or canceled if (event.status === 'error' || event.status === 'canceled') { task.state = 'error'; diff --git a/packages/backend/src/models/progressiveEvent.ts b/packages/backend/src/models/baseEvent.ts similarity index 77% rename from packages/backend/src/models/progressiveEvent.ts rename to packages/backend/src/models/baseEvent.ts index 71fb87f54..0edcf28fd 100644 --- a/packages/backend/src/models/progressiveEvent.ts +++ b/packages/backend/src/models/baseEvent.ts @@ -16,23 +16,23 @@ * SPDX-License-Identifier: Apache-2.0 ***********************************************************************/ -export interface ProgressiveEvent { +export interface BaseEvent { id: string; status: 'error' | 'completed' | 'progress' | 'canceled'; message?: string; } -export interface CompletionProgressiveEvent extends ProgressiveEvent { +export interface CompletionEvent extends BaseEvent { status: 'completed' | 'error' | 'canceled'; duration: number; } -export interface ExecutingProgressiveEvent extends ProgressiveEvent { +export interface ProgressEvent extends BaseEvent { status: 'progress'; value: number; } -export const isCompletionProgressiveEvent = (value: unknown): value is CompletionProgressiveEvent => { +export const isCompletionEvent = (value: unknown): value is CompletionEvent => { return ( !!value && typeof value === 'object' && @@ -43,7 +43,7 @@ export const isCompletionProgressiveEvent = (value: unknown): value is Completio ); }; -export const isExecutingProgressiveEvent = (value: unknown): value is ExecutingProgressiveEvent => { +export const isProgressEvent = (value: unknown): value is ProgressEvent => { return ( !!value && typeof value === 'object' && 'status' in value && value['status'] === 'progress' && 'value' in value ); diff --git a/packages/backend/src/utils/downloader.ts b/packages/backend/src/utils/downloader.ts index 81bbe41e7..a0de9daf7 100644 --- a/packages/backend/src/utils/downloader.ts +++ b/packages/backend/src/utils/downloader.ts @@ -20,15 +20,11 @@ import { getDurationSecondsSince } from './utils'; import { createWriteStream, promises } from 'node:fs'; import https from 'node:https'; import { EventEmitter, type Event } from '@podman-desktop/api'; -import type { - CompletionProgressiveEvent, - ExecutingProgressiveEvent, - ProgressiveEvent, -} from '../models/progressiveEvent'; +import type { CompletionEvent, ProgressEvent, BaseEvent } from '../models/baseEvent'; export class Downloader { - private readonly _onEvent = new EventEmitter(); - readonly onEvent: Event = this._onEvent.event; + private readonly _onEvent = new EventEmitter(); + readonly onEvent: Event = this._onEvent.event; private requestedIdentifier: string; completed: boolean; @@ -55,7 +51,7 @@ export class Downloader { status: 'completed', message: `Duration ${durationSeconds}s.`, duration: durationSeconds, - } as CompletionProgressiveEvent); + } as CompletionEvent); } catch (err: unknown) { if (!this.abortSignal?.aborted) { this._onEvent.fire({ @@ -133,7 +129,7 @@ export class Downloader { id: this.requestedIdentifier, status: 'progress', value: progressValue, - } as ExecutingProgressiveEvent); + } as ProgressEvent); } }); resp.pipe(stream); diff --git a/packages/backend/src/utils/uploader.ts b/packages/backend/src/utils/uploader.ts index aff45e2f9..a9a600042 100644 --- a/packages/backend/src/utils/uploader.ts +++ b/packages/backend/src/utils/uploader.ts @@ -19,7 +19,7 @@ import { EventEmitter, type Event } from '@podman-desktop/api'; import { WSLUploader } from './WSLUploader'; import { getDurationSecondsSince } from './utils'; -import type { CompletionProgressiveEvent, ProgressiveEvent } from '../models/progressiveEvent'; +import type { CompletionEvent, BaseEvent } from '../models/baseEvent'; export interface UploadWorker { canUpload: () => boolean; @@ -27,8 +27,8 @@ export interface UploadWorker { } export class Uploader { - readonly #_onEvent = new EventEmitter(); - readonly onEvent: Event = this.#_onEvent.event; + readonly #_onEvent = new EventEmitter(); + readonly onEvent: Event = this.#_onEvent.event; readonly #workers: UploadWorker[] = []; constructor( @@ -55,7 +55,7 @@ export class Uploader { status: 'completed', message: `Duration ${durationSeconds}s.`, duration: durationSeconds, - } as CompletionProgressiveEvent); + } as CompletionEvent); return modelPath; } } catch (err) { @@ -78,7 +78,7 @@ export class Uploader { id, status: 'completed', message: `Use local model`, - } as CompletionProgressiveEvent); + } as CompletionEvent); return modelPath; }