From 399650001644ff8757781b2cddc93555c87f5c8f Mon Sep 17 00:00:00 2001 From: Harish Mohan Raj Date: Mon, 22 Apr 2024 11:49:11 +0530 Subject: [PATCH] WIP --- app/src/client/app/ModelsPage.tsx | 68 +++++++++++++++---- .../client/components/DynamicFormBuilder.tsx | 19 +++++- .../client/components/ModelFormContainer.tsx | 9 ++- app/src/client/interfaces/models.ts | 8 +++ .../client/tests/DynamicFormBuilder.test.tsx | 12 ++++ app/src/server/queries.ts | 1 + 6 files changed, 97 insertions(+), 20 deletions(-) diff --git a/app/src/client/app/ModelsPage.tsx b/app/src/client/app/ModelsPage.tsx index 285577c..0ffbdfa 100644 --- a/app/src/client/app/ModelsPage.tsx +++ b/app/src/client/app/ModelsPage.tsx @@ -1,4 +1,4 @@ -import { useState } from 'react'; +import { useState, useEffect } from 'react'; import { getModels, useQuery, updateUserModels } from 'wasp/client/operations'; import CustomLayout from './layout/CustomLayout'; @@ -10,18 +10,20 @@ import { getAvailableModels } from '../services/modelService'; import { ModelSchema, JsonSchema } from '../interfaces/models'; import ModelFormContainer from '../components/ModelFormContainer'; import NotificationBox from '../components/NotificationBox'; +import { UpdateExistingModelType } from '../interfaces/models'; const ModelsPage = () => { const [modelsSchema, setModelsSchema] = useState(null); const [initialModelSchema, setInitialModelSchema] = useState(null); const [selectedModel, setSelectedModel] = useState(''); + const [updateExistingModel, setUpdateExistingModel] = useState(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [showAddModel, setShowAddModel] = useState(false); - const { data: modelsList, refetch: refetchModels } = useQuery(getModels); + const { data: modelsList, refetch: refetchModels, isLoading: getModelsIsLoading } = useQuery(getModels); const fetchData = async () => { - setShowAddModel(true); + // setShowAddModel(true); setIsLoading(true); try { const response = await getAvailableModels(); @@ -35,9 +37,9 @@ const ModelsPage = () => { setIsLoading(false); }; - // useEffect(() => { - // fetchData(); - // }, []); + useEffect(() => { + fetchData(); + }, []); const handleModelChange = (newModel: string) => { const foundSchema = modelsSchema?.schemas.find((schema) => schema.name === newModel); @@ -57,6 +59,20 @@ const ModelsPage = () => { setShowAddModel(false); }; + const updateSelectedModel = (index: number) => { + if (modelsList) { + const selectedModel = modelsList[index]; + const selectedModelSchemaName = selectedModel.api_type === 'openai' ? 'openai' : 'azureoai'; + const foundSchema = modelsSchema?.schemas.find((schema) => schema.name.toLowerCase() === selectedModelSchemaName); + if (foundSchema) { + setInitialModelSchema(foundSchema.json_schema); + setSelectedModel(foundSchema.name); + setUpdateExistingModel(selectedModel); + setShowAddModel(true); + } + } + }; + return ( @@ -64,8 +80,16 @@ const ModelsPage = () => {
-
-
{!showAddModel ? ( @@ -78,6 +102,8 @@ const ModelsPage = () => {
updateSelectedModel(i)} >
@@ -126,17 +152,28 @@ const ModelsPage = () => { ) : (

Available Models

-

Please add a new model...

+

No models available. Please add one.

) ) : ( modelsSchema && ( <> - + {initialModelSchema && ( @@ -149,11 +186,12 @@ const ModelsPage = () => {
- {isLoading && ( -
- -
- )} + {isLoading || + (getModelsIsLoading && ( +
+ +
+ ))}
); diff --git a/app/src/client/components/DynamicFormBuilder.tsx b/app/src/client/components/DynamicFormBuilder.tsx index d9076d1..fc566b8 100644 --- a/app/src/client/components/DynamicFormBuilder.tsx +++ b/app/src/client/components/DynamicFormBuilder.tsx @@ -7,9 +7,12 @@ import { validateForm } from '../services/commonService'; import { parseValidationErrors } from '../app/utils/formHelpers'; import Loader from '../admin/common/Loader'; +import { UpdateExistingModelType } from '../interfaces/models'; + interface DynamicFormBuilderProps { jsonSchema: JsonSchema; validationURL: string; + updateExistingModel: UpdateExistingModelType | null; onSuccessCallback: (data: any) => void; onCancelCallback: (data: any) => void; } @@ -17,6 +20,7 @@ interface DynamicFormBuilderProps { const DynamicFormBuilder: React.FC = ({ jsonSchema, validationURL, + updateExistingModel, onSuccessCallback, onCancelCallback, }) => { @@ -36,7 +40,6 @@ const DynamicFormBuilder: React.FC = ({ } setIsLoading(false); }; - return ( <>
@@ -47,14 +50,24 @@ const DynamicFormBuilder: React.FC = ({ {property.enum ? ( handleChange(key, value)} /> ) : ( handleChange(key, value)} /> diff --git a/app/src/client/components/ModelFormContainer.tsx b/app/src/client/components/ModelFormContainer.tsx index 10131a2..e73ef86 100644 --- a/app/src/client/components/ModelFormContainer.tsx +++ b/app/src/client/components/ModelFormContainer.tsx @@ -3,19 +3,24 @@ import React from 'react'; import { ModelSchema } from '../interfaces/models'; interface ModelFormContainerProps { + selectedModel: string | null; modelsSchema: ModelSchema; onModelChange: (selectedModel: string) => void; } -const ModelFormContainer: React.FC = ({ modelsSchema, onModelChange }) => { +const ModelFormContainer: React.FC = ({ selectedModel, modelsSchema, onModelChange }) => { return (
- +

Add a new model

+