From 90c7420c3489c9f236703cca3d3deeb232efdec3 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 24 Oct 2024 15:41:10 +0700 Subject: [PATCH] chore: add comments and clean unused imports --- web/containers/ModelDropdown/index.tsx | 1 - .../ChatBody/EmptyThread/index.tsx | 1 - .../Thread/ThreadCenterPanel/index.tsx | 2 +- web/utils/huggingface.ts | 30 +++++++++++++++++++ web/utils/model.ts | 6 ++++ 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/web/containers/ModelDropdown/index.tsx b/web/containers/ModelDropdown/index.tsx index a5874b3de..abd9af247 100644 --- a/web/containers/ModelDropdown/index.tsx +++ b/web/containers/ModelDropdown/index.tsx @@ -200,7 +200,6 @@ const ModelDropdown = ({ if (model) updateModelParameter(activeThread, { params: modelParams, - // modelPath: model.file_path, modelId: model.id, engine: model.engine, }) diff --git a/web/screens/Thread/ThreadCenterPanel/ChatBody/EmptyThread/index.tsx b/web/screens/Thread/ThreadCenterPanel/ChatBody/EmptyThread/index.tsx index a99e6306f..403370ade 100644 --- a/web/screens/Thread/ThreadCenterPanel/ChatBody/EmptyThread/index.tsx +++ b/web/screens/Thread/ThreadCenterPanel/ChatBody/EmptyThread/index.tsx @@ -1,6 +1,5 @@ import { memo } from 'react' -import { InferenceEngine } from '@janhq/core' import { Button } from '@janhq/joi' import { useAtomValue, useSetAtom } from 'jotai' diff --git a/web/screens/Thread/ThreadCenterPanel/index.tsx b/web/screens/Thread/ThreadCenterPanel/index.tsx index c83a38a1a..2440af2c5 100644 --- a/web/screens/Thread/ThreadCenterPanel/index.tsx +++ b/web/screens/Thread/ThreadCenterPanel/index.tsx @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { Fragment, useEffect, useState } from 'react' +import { useEffect, useState } from 'react' import { Accept, useDropzone } from 'react-dropzone' diff --git a/web/utils/huggingface.ts b/web/utils/huggingface.ts index ceddc6867..3e71f3a0f 100644 --- a/web/utils/huggingface.ts +++ b/web/utils/huggingface.ts @@ -1,5 +1,13 @@ import { AllQuantizations, getFileSize, HuggingFaceRepoData } from '@janhq/core' +/** + * Fetches data from a Hugging Face repository. + * + * @param repoId - The ID of the Hugging Face repository. + * @param huggingFaceAccessToken - Optional access token for Hugging Face API. + * @returns A promise that resolves to the HuggingFaceRepoData. + * @throws Will throw an error if the repository is not supported or if there is an error in the response. + */ export const fetchHuggingFaceRepoData = async ( repoId: string, huggingFaceAccessToken?: string @@ -60,31 +68,53 @@ export const fetchHuggingFaceRepoData = async ( return data } +/** + * Converts a repository ID or URL to a valid Hugging Face API URL. + * + * @param repoId - The repository ID or URL to convert. + * @returns A string representing the Hugging Face API URL. + * @throws {InvalidHostError} If the URL is invalid or not from huggingface.co. + * @throws {Error} If the URL cannot be parsed. + */ export function toHuggingFaceUrl(repoId: string): string { try { + // Attempt to create a URL object from the repoId const url = new URL(repoId) + + // Check if the host is huggingface.co if (url.host !== 'huggingface.co') { throw new InvalidHostError(`Invalid Hugging Face repo URL: ${repoId}`) } + // Split the pathname into parts and filter out empty strings const paths = url.pathname.split('/').filter((e) => e.trim().length > 0) + + // Ensure there are at least two parts in the path (user/repo) if (paths.length < 2) { throw new InvalidHostError(`Invalid Hugging Face repo URL: ${repoId}`) } + // Construct and return the API URL return `${url.origin}/api/models/${paths[0]}/${paths[1]}` } catch (err) { + // Re-throw InvalidHostError if it was caught if (err instanceof InvalidHostError) { throw err } + // If repoId starts with 'https' but couldn't be parsed, throw an error if (repoId.startsWith('https')) { throw new Error(`Cannot parse url: ${repoId}`) } + // If repoId is not a URL, assume it's a valid repo ID and construct the API URL return `https://huggingface.co/api/models/${repoId}` } } + +/** + * Error thrown when the host of a URL is invalid or not from huggingface.co. + */ export class InvalidHostError extends Error { constructor(message: string) { super(message) diff --git a/web/utils/model.ts b/web/utils/model.ts index 00efc1155..cb0f0ff31 100644 --- a/web/utils/model.ts +++ b/web/utils/model.ts @@ -1,3 +1,9 @@ +/** + * Extracts and normalizes the model ID from a given download URL. + * + * @param downloadUrl - The URL from which to extract the model ID. + * @returns The extracted model ID, or the original URL if extraction fails. + */ export const normalizeModelId = (downloadUrl: string): string => { return downloadUrl.split('/').pop() ?? downloadUrl }