jan/web/app/_hooks/useDownloadModel.ts
Louis 5fc1ba7067
feature: @janhq/plugin-core module & plugins update (#321)
* @janhq/plugin-core module

* refactor web to use exported services from module

* refactor data-plugin to provide DAL & move model logics to model management plugin

* model-management in TS

* add ci auto package, increate version, and publish to npm repository

* chore: storage operations

* chore: hybrid data-plugin esm & cjs module

* chore: PouchDB Driver

* chore: documentation

---------

Co-authored-by: Hien To <hien@jan.ai>
Co-authored-by: Service Account <service@jan.ai>
2023-10-14 15:59:28 +07:00

56 lines
1.9 KiB
TypeScript

import { executeSerial } from "@/_services/pluginService";
import { DataService, ModelManagementService } from "@janhq/plugin-core";
import { ModelVersion } from "@/_models/ModelVersion";
import { Product } from "@/_models/Product";
import { AssistantModel } from "@/_models/AssistantModel";
export default function useDownloadModel() {
const assistanModel = (
model: Product,
modelVersion: ModelVersion
): AssistantModel => {
return {
_id: modelVersion._id,
name: modelVersion.name,
quantMethod: modelVersion.quantMethod,
bits: modelVersion.bits,
size: modelVersion.size,
maxRamRequired: modelVersion.maxRamRequired,
usecase: modelVersion.usecase,
downloadLink: modelVersion.downloadLink,
startDownloadAt: modelVersion.startDownloadAt,
finishDownloadAt: modelVersion.finishDownloadAt,
productId: model._id,
productName: model.name,
shortDescription: model.shortDescription,
longDescription: model.longDescription,
avatarUrl: model.avatarUrl,
author: model.author,
version: model.version,
modelUrl: model.modelUrl,
nsfw: model.nsfw === true ? false : true,
greeting: model.greeting,
type: model.type,
createdAt: new Date(model.createdAt).getTime(),
updatedAt: new Date(model.updatedAt ?? "").getTime(),
status: "",
releaseDate: -1,
tags: model.tags,
};
};
const downloadModel = async (model: Product, modelVersion: ModelVersion) => {
modelVersion.startDownloadAt = Date.now();
const assistantModel = assistanModel(model, modelVersion);
await executeSerial(ModelManagementService.StoreModel, assistantModel);
await executeSerial(ModelManagementService.DownloadModel, {
downloadUrl: modelVersion.downloadLink,
fileName: modelVersion._id,
});
};
return {
downloadModel,
};
}