()
| 5 | import { updateModel } from "../lib/models"; |
| 6 | |
| 7 | export function useModels() { |
| 8 | const [models, setModels] = useState<Model[]>([]); |
| 9 | const [isLoading, setIsLoading] = useState<boolean>(true); |
| 10 | const [error, setError] = useState<string>(); |
| 11 | |
| 12 | const loadModels = async () => { |
| 13 | // Get the model settings |
| 14 | setIsLoading(true); |
| 15 | const items = await LocalStorage.allItems(); |
| 16 | const modelObjs = Object.entries(items) |
| 17 | .filter(([key]) => key.startsWith("--model-")) |
| 18 | .map(([, value]) => JSON.parse(value)); |
| 19 | setModels(modelObjs); |
| 20 | setIsLoading(false); |
| 21 | }; |
| 22 | |
| 23 | useEffect(() => { |
| 24 | Promise.resolve(installDefaults()).then(() => { |
| 25 | Promise.resolve(loadModels()); |
| 26 | }); |
| 27 | }, []); |
| 28 | |
| 29 | const revalidate = async () => { |
| 30 | return loadModels(); |
| 31 | }; |
| 32 | |
| 33 | const dummyModel = (): Model => { |
| 34 | return { |
| 35 | name: "", |
| 36 | description: "", |
| 37 | icon: Icon.Gear, |
| 38 | iconColor: Color.Red, |
| 39 | favorited: false, |
| 40 | endpoint: "", |
| 41 | authType: "", |
| 42 | apiKey: "", |
| 43 | inputSchema: "", |
| 44 | outputKeyPath: "", |
| 45 | outputTiming: "sync", |
| 46 | lengthLimit: "2500", |
| 47 | id: "", |
| 48 | notes: "", |
| 49 | isDefault: false, |
| 50 | temperature: "1.0", |
| 51 | }; |
| 52 | }; |
| 53 | |
| 54 | const createModel = async (newData: Model & { [key: string]: string | boolean }) => { |
| 55 | // Check if the name is empty |
| 56 | if (!newData.name) { |
| 57 | setError("Name cannot be empty."); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // Check if a model with that name already exists |
| 62 | if (models.find((model) => model.name == newData.name)) { |
| 63 | setError("A model with that name already exists."); |
| 64 | return false; |
no test coverage detected