Update import statements, set TTSEnabled in TTSModeSettings, and fix fetchChatModels error handling

This commit is contained in:
n4ze3m 2024-04-13 14:25:09 +05:30
parent a3810cd534
commit 9262a5bb7c
6 changed files with 24 additions and 17 deletions

View File

@ -4,7 +4,7 @@ import { useLocation, NavLink } from "react-router-dom"
import { Sidebar } from "../Option/Sidebar" import { Sidebar } from "../Option/Sidebar"
import { Drawer, Select, Tooltip } from "antd" import { Drawer, Select, Tooltip } from "antd"
import { useQuery } from "@tanstack/react-query" import { useQuery } from "@tanstack/react-query"
import { getAllModels } from "~/services/ollama" import { fetchChatModels, getAllModels } from "~/services/ollama"
import { useMessageOption } from "~/hooks/useMessageOption" import { useMessageOption } from "~/hooks/useMessageOption"
import { import {
ChevronLeft, ChevronLeft,
@ -46,7 +46,7 @@ export default function OptionLayout({
isFetching: isModelsFetching isFetching: isModelsFetching
} = useQuery({ } = useQuery({
queryKey: ["fetchModel"], queryKey: ["fetchModel"],
queryFn: () => getAllModels({ returnEmpty: true }), queryFn: () => fetchChatModels({ returnEmpty: true }),
refetchInterval: 15000 refetchInterval: 15000
}) })

View File

@ -1,6 +1,7 @@
import { SaveButton } from "@/components/Common/SaveButton" import { SaveButton } from "@/components/Common/SaveButton"
import { getSearchSettings, setSearchSettings } from "@/services/search" import { getSearchSettings, setSearchSettings } from "@/services/search"
import { getTTSSettings, setTTSSettings } from "@/services/tts" import { getTTSSettings, setTTSSettings } from "@/services/tts"
import { useWebUI } from "@/store/webui"
import { SUPPORTED_SERACH_PROVIDERS } from "@/utils/search-provider" import { SUPPORTED_SERACH_PROVIDERS } from "@/utils/search-provider"
import { useForm } from "@mantine/form" import { useForm } from "@mantine/form"
import { useQuery, useQueryClient } from "@tanstack/react-query" import { useQuery, useQueryClient } from "@tanstack/react-query"
@ -9,7 +10,7 @@ import { useTranslation } from "react-i18next"
export const TTSModeSettings = ({ hideTitle }: { hideTitle?: boolean }) => { export const TTSModeSettings = ({ hideTitle }: { hideTitle?: boolean }) => {
const { t } = useTranslation("settings") const { t } = useTranslation("settings")
const queryClient = useQueryClient() const { setTTSEnabled } = useWebUI()
const form = useForm({ const form = useForm({
initialValues: { initialValues: {
@ -46,6 +47,7 @@ export const TTSModeSettings = ({ hideTitle }: { hideTitle?: boolean }) => {
<form <form
onSubmit={form.onSubmit(async (values) => { onSubmit={form.onSubmit(async (values) => {
await setTTSSettings(values) await setTTSSettings(values)
setTTSEnabled(values.ttsEnabled)
})} })}
className="space-y-4"> className="space-y-4">
<div className="flex sm:flex-row flex-col space-y-4 sm:space-y-0 sm:justify-between"> <div className="flex sm:flex-row flex-col space-y-4 sm:space-y-0 sm:justify-between">

View File

@ -75,14 +75,6 @@ export const useMessageOption = () => {
const navigate = useNavigate() const navigate = useNavigate()
const textareaRef = React.useRef<HTMLTextAreaElement>(null) const textareaRef = React.useRef<HTMLTextAreaElement>(null)
React.useEffect(() => {
const checkTTSEnabled = async () => {
const tts = await isTTSEnabled()
setTTSEnabled(tts)
}
checkTTSEnabled()
}, [])
const clearChat = () => { const clearChat = () => {
navigate("/") navigate("/")

View File

@ -113,11 +113,18 @@ export const deleteModel = async (model: string) => {
return response.json() return response.json()
} }
export const fetchChatModels = async () => { export const fetchChatModels = async ({
returnEmpty = false
}: {
returnEmpty?: boolean
}) => {
try { try {
const baseUrl = await getOllamaURL() const baseUrl = await getOllamaURL()
const response = await fetch(`${cleanUrl(baseUrl)}/api/tags`) const response = await fetch(`${cleanUrl(baseUrl)}/api/tags`)
if (!response.ok) { if (!response.ok) {
if (returnEmpty) {
return []
}
throw new Error(response.statusText) throw new Error(response.statusText)
} }
const json = await response.json() const json = await response.json()
@ -127,8 +134,8 @@ export const fetchChatModels = async () => {
modified_at: string modified_at: string
size: number size: number
digest: string digest: string
details: { details?: {
parent_model: string parent_model?: string
format: string format: string
family: string family: string
families: string[] families: string[]
@ -136,7 +143,7 @@ export const fetchChatModels = async () => {
quantization_level: string quantization_level: string
} }
}[] }[]
return models.filter((model) => { return models?.filter((model) => {
return ( return (
!model?.details?.families?.includes("bert") && !model?.details?.families?.includes("bert") &&
!model?.details?.families?.includes("nomic-bert") !model?.details?.families?.includes("nomic-bert")
@ -144,7 +151,7 @@ export const fetchChatModels = async () => {
}) })
} catch (e) { } catch (e) {
console.error(e) console.error(e)
return [] return await getAllModels({ returnEmpty })
} }
} }

View File

@ -36,6 +36,9 @@ export const setVoice = async (voice: string) => {
export const isTTSEnabled = async () => { export const isTTSEnabled = async () => {
const data = await storage.get("isTTSEnabled") const data = await storage.get("isTTSEnabled")
if(!data || data.length === 0) {
return true
}
return data === "true" return data === "true"
} }
@ -45,6 +48,9 @@ export const setTTSEnabled = async (isTTSEnabled: boolean) => {
export const isSSMLEnabled = async () => { export const isSSMLEnabled = async () => {
const data = await storage.get("isSSMLEnabled") const data = await storage.get("isSSMLEnabled")
if(!data || data.length === 0) {
return true
}
return data === "true" return data === "true"
} }

View File

@ -12,6 +12,6 @@ export const useWebUI = create<State>((set) => ({
sendWhenEnter: true, sendWhenEnter: true,
setSendWhenEnter: (sendWhenEnter) => set({ sendWhenEnter }), setSendWhenEnter: (sendWhenEnter) => set({ sendWhenEnter }),
ttsEnabled: false, ttsEnabled: true,
setTTSEnabled: (ttsEnabled) => set({ ttsEnabled }) setTTSEnabled: (ttsEnabled) => set({ ttsEnabled })
})) }))