refactor: remove unused hooks and resolve no-explicit-any

This commit is contained in:
Louis 2023-11-17 14:01:42 +07:00
parent cc68e42415
commit bb78683847
13 changed files with 61 additions and 70 deletions

View File

@ -7,7 +7,6 @@
*/
import {
ChatCompletionMessage,
ChatCompletionRole,
EventName,
MessageRequest,

View File

@ -0,0 +1,21 @@
interface Version {
name: string
quantMethod: string
bits: number
size: number
maxRamRequired: number
usecase: string
downloadLink: string
}
interface ModelSchema {
id: string
name: string
shortDescription: string
avatarUrl: string
longDescription: string
author: string
version: string
modelUrl: string
tags: string[]
versions: Version[]
}

View File

@ -1,8 +1,9 @@
export const parseToModel = (model) => {
import { ModelCatalog } from '@janhq/core'
export function parseToModel(schema: ModelSchema): ModelCatalog {
const modelVersions = []
model.versions.forEach((v) => {
schema.versions.forEach((v) => {
const version = {
id: `${model.author}-${v.name}`,
name: v.name,
quantMethod: v.quantMethod,
bits: v.bits,
@ -10,28 +11,22 @@ export const parseToModel = (model) => {
maxRamRequired: v.maxRamRequired,
usecase: v.usecase,
downloadLink: v.downloadLink,
productId: model.id,
}
modelVersions.push(version)
})
const product = {
id: model.id,
name: model.name,
shortDescription: model.shortDescription,
avatarUrl: model.avatarUrl,
author: model.author,
version: model.version,
modelUrl: model.modelUrl,
nsfw: model.nsfw,
tags: model.tags,
greeting: model.defaultGreeting,
type: model.type,
createdAt: model.createdAt,
longDescription: model.longDescription,
status: 'Downloadable',
const model: ModelCatalog = {
id: schema.id,
name: schema.name,
shortDescription: schema.shortDescription,
avatarUrl: schema.avatarUrl,
author: schema.author,
version: schema.version,
modelUrl: schema.modelUrl,
tags: schema.tags,
longDescription: schema.longDescription,
releaseDate: 0,
availableVersions: modelVersions,
}
return product
return model
}

View File

@ -1,12 +0,0 @@
import { atom } from 'jotai'
export const showConfirmDeleteConversationModalAtom = atom(false)
export const showConfirmSignOutModalAtom = atom(false)
export const showConfirmDeleteModalAtom = atom(false)
export const showingAdvancedPromptAtom = atom<boolean>(false)
export const showingProductDetailAtom = atom<boolean>(false)
export const showingMobilePaneAtom = atom<boolean>(false)
export const showingBotListModalAtom = atom<boolean>(false)
export const showingCancelDownloadModalAtom = atom<boolean>(false)
export const showingModalNoActiveModel = atom<boolean>(false)

View File

@ -35,7 +35,7 @@ export const useCreateConversation = () => {
waitingForResponse: false,
})
pluginManager
await pluginManager
.get<ConversationalPlugin>(PluginType.Conversational)
?.saveConversation(mappedConvo)
setUserConversations([mappedConvo, ...userConversations])

View File

@ -16,10 +16,6 @@ import {
getActiveConvoIdAtom,
setActiveConvoIdAtom,
} from '@/helpers/atoms/Conversation.atom'
import {
showingProductDetailAtom,
showingAdvancedPromptAtom,
} from '@/helpers/atoms/Modal.atom'
export default function useDeleteConversation() {
const { activeModel } = useActiveModel()
@ -27,8 +23,6 @@ export default function useDeleteConversation() {
userConversationsAtom
)
const setCurrentPrompt = useSetAtom(currentPromptAtom)
const setShowingProductDetail = useSetAtom(showingProductDetailAtom)
const setShowingAdvancedPrompt = useSetAtom(showingAdvancedPromptAtom)
const activeConvoId = useAtomValue(getActiveConvoIdAtom)
const setActiveConvoId = useSetAtom(setActiveConvoIdAtom)
@ -45,6 +39,7 @@ export default function useDeleteConversation() {
)
setUserConversations(currentConversations)
deleteMessages(activeConvoId)
setCurrentPrompt('')
toaster({
title: 'Succes delete a chat',
description: `Delete chat with ${activeModel?.name} has been completed`,
@ -54,9 +49,6 @@ export default function useDeleteConversation() {
} else {
setActiveConvoId(undefined)
}
setCurrentPrompt('')
setShowingProductDetail(false)
setShowingAdvancedPrompt(false)
} catch (err) {
console.error(err)
}

View File

@ -20,14 +20,13 @@ export function useGetConfiguredModels() {
const [models, setModels] = useState<ModelCatalog[]>([])
async function getConfiguredModels(): Promise<ModelCatalog[]> {
return (
((await pluginManager
.get<ModelPlugin>(PluginType.Model)
?.getConfiguredModels()) as ModelCatalog[]) ?? []
)
const models = await pluginManager
.get<ModelPlugin>(PluginType.Model)
?.getConfiguredModels()
return models ?? []
}
const fetchModels = async () => {
async function fetchModels() {
setLoading(true)
let models = await getConfiguredModels()
if (process.env.NODE_ENV === 'development') {
@ -37,10 +36,8 @@ export function useGetConfiguredModels() {
setModels(models)
}
// TODO allow user for filter
useEffect(() => {
fetchModels()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return { loading, models }

View File

@ -12,11 +12,10 @@ export function useGetDownloadedModels() {
const [downloadedModels, setDownloadedModels] = useAtom(downloadedModelAtom)
async function getDownloadedModels(): Promise<Model[]> {
const models =
((await pluginManager
.get<ModelPlugin>(PluginType.Model)
?.getDownloadedModels()) as Model[]) ?? []
return models
const models = await pluginManager
.get<ModelPlugin>(PluginType.Model)
?.getDownloadedModels()
return models ?? []
}
useEffect(() => {

View File

@ -10,15 +10,15 @@ import { currentConversationAtom } from '@/helpers/atoms/Conversation.atom'
export default function useGetInputState() {
const [inputState, setInputState] = useState<InputType>('loading')
const currentConvo = useAtomValue(currentConversationAtom)
const currentThread = useAtomValue(currentConversationAtom)
const { activeModel } = useActiveModel()
const { downloadedModels } = useGetDownloadedModels()
const handleInputState = (
convo: Thread | undefined,
thread: Thread | undefined,
currentModel: Model | undefined
) => {
if (convo == null) return
if (thread == null) return
if (currentModel == null) {
setInputState('loading')
return
@ -26,7 +26,7 @@ export default function useGetInputState() {
// check if convo model id is in downloaded models
const isModelAvailable = downloadedModels.some(
(model) => model.id === convo.modelId
(model) => model.id === thread.modelId
)
if (!isModelAvailable) {
@ -35,7 +35,7 @@ export default function useGetInputState() {
return
}
if (convo.modelId !== currentModel.id) {
if (thread.modelId !== currentModel.id) {
// in case convo model and active model is different,
// ask user to init the required model
setInputState('model-mismatch')
@ -46,11 +46,11 @@ export default function useGetInputState() {
}
useEffect(() => {
handleInputState(currentConvo, activeModel)
handleInputState(currentThread, activeModel)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return { inputState, currentConvo }
return { inputState, currentThread }
}
type InputType = 'available' | 'loading' | 'model-mismatch' | 'model-not-found'

View File

@ -34,14 +34,15 @@ export default function useGetSystemResources() {
useEffect(() => {
getSystemResources()
// Fetch interval - every 3s
// Fetch interval - every 5s
// TODO: Will we really need this?
// There is a possibility that this will be removed and replaced by the process event hook?
const intervalId = setInterval(() => {
getSystemResources()
}, 5000)
// clean up
// clean up interval
return () => clearInterval(intervalId)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return {

View File

@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { toast } from 'react-toastify'
const API_BASE_PATH: string = '/api/v1'
@ -48,7 +47,7 @@ export async function fetchApi(
method: pluginFunc,
args: args,
}),
headers: { 'Content-Type': 'application/json', 'Authorization': '' },
headers: { contentType: 'application/json', Authorization: '' },
})
if (!response.ok) {

View File

@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-types */
export class EventEmitter {
private handlers: Map<string, Function[]>
@ -28,6 +27,7 @@ export class EventEmitter {
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public emit(eventName: string, args: any): void {
if (!this.handlers.has(eventName)) {
return

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { ModelCatalog, ModelVersion } from '@janhq/core'
import { ModelCatalog } from '@janhq/core'
export const dummyModel: ModelCatalog = {
id: 'aladar/TinyLLama-v0-GGUF',