From 582820019796eb4dd348c6b79c43dab85e26388d Mon Sep 17 00:00:00 2001 From: Danny Avila <110412045+danny-avila@users.noreply.github.com> Date: Sat, 5 Aug 2023 12:10:36 -0400 Subject: [PATCH] refactor(types): use zod for better type safety, style(Messages): new scroll behavior, style(Buttons): match ChatGPT (#761) * feat: add zod schemas for better type safety * refactor(useSetOptions): remove 'as Type' in favor of zod schema * fix: descendant console error, change

tag to

tag for content in PluginTooltip component * style(MessagesView): instant/snappier scroll behavior matching official site * fix(Messages): add null check for scrollableRef before accessing its properties in handleScroll and useEffect * fix(messageSchema.js): change type of invocationId from string to number fix(schemas.ts): make authenticated property in tPluginSchema optional fix(schemas.ts): make isButton property in tPluginSchema optional fix(schemas.ts): make messages property in tConversationSchema optional and change its type to array of strings fix(schemas.ts): make systemMessage property in tConversationSchema nullable and optional fix(schemas.ts): make modelLabel property in tConversationSchema nullable and optional fix(schemas.ts): make chatGptLabel property in tConversationSchema nullable and optional fix(schemas.ts): make promptPrefix property in tConversationSchema nullable and optional fix(schemas.ts): make context property in tConversationSchema nullable and optional fix(schemas.ts): make jailbreakConversationId property in tConversationSchema nullable and optional fix(schemas.ts): make conversationSignature property in tConversationSchema nullable and optional fix(schemas.ts): make clientId property * refactor(types): replace main types with zod schemas and inferred types * refactor(types/schemas): use schemas for better type safety of main types * style(ModelSelect/Buttons): remove shadow and transition * style(ModelSelect): button changes to closer match OpenAI * style(ModelSelect): remove green rings which flicker * style(scrollToBottom): add two separate scrolling functions * fix(OptionsBar.tsx): handle onFocus and onBlur events to update opacityClass fix(Messages/index.jsx): increase debounce time for scrollIntoView function --- api/models/schema/messageSchema.js | 2 +- .../Endpoints/EndpointOptionsDialog.tsx | 13 +- .../Endpoints/SaveAsPresetDialog.tsx | 4 +- .../Endpoints/Settings/Examples.tsx | 4 +- .../Input/ModelSelect/Anthropic.tsx | 2 +- .../components/Input/ModelSelect/BingAI.tsx | 6 +- .../components/Input/ModelSelect/ChatGPT.tsx | 2 +- .../components/Input/ModelSelect/Google.tsx | 2 +- .../components/Input/ModelSelect/OpenAI.tsx | 2 +- .../components/Input/ModelSelect/Plugins.tsx | 16 +-- client/src/components/Input/OptionsBar.tsx | 21 ++- client/src/components/Messages/index.jsx | 21 ++- .../Plugins/Store/PluginStoreDialog.tsx | 13 +- .../Plugins/Store/PluginTooltip.tsx | 4 +- client/src/components/ui/Dropdown.jsx | 2 +- .../src/components/ui/MultiSelectDropDown.tsx | 2 +- client/src/components/ui/SelectDropDown.tsx | 2 +- client/src/components/ui/Tabs.tsx | 6 +- client/src/hooks/usePresetOptions.ts | 100 ++++++------- client/src/hooks/useSetOptions.ts | 78 +++++----- client/src/utils/cleanupPreset.ts | 6 +- client/src/utils/index.ts | 2 +- e2e/setup/authenticate.ts | 4 + package-lock.json | 6 +- packages/data-provider/package.json | 5 +- packages/data-provider/src/createPayload.ts | 5 +- packages/data-provider/src/data-service.ts | 15 +- .../data-provider/src/react-query-service.ts | 45 +++--- packages/data-provider/src/schemas.ts | 121 ++++++++++++++++ packages/data-provider/src/types.ts | 135 +----------------- 30 files changed, 329 insertions(+), 317 deletions(-) create mode 100644 packages/data-provider/src/schemas.ts diff --git a/api/models/schema/messageSchema.js b/api/models/schema/messageSchema.js index 6c0c1490a8..792b2d545a 100644 --- a/api/models/schema/messageSchema.js +++ b/api/models/schema/messageSchema.js @@ -25,7 +25,7 @@ const messageSchema = mongoose.Schema( type: String, }, invocationId: { - type: String, + type: Number, }, parentMessageId: { type: String, diff --git a/client/src/components/Endpoints/EndpointOptionsDialog.tsx b/client/src/components/Endpoints/EndpointOptionsDialog.tsx index 79050829e9..ff3efc51a7 100644 --- a/client/src/components/Endpoints/EndpointOptionsDialog.tsx +++ b/client/src/components/Endpoints/EndpointOptionsDialog.tsx @@ -1,7 +1,7 @@ import exportFromJSON from 'export-from-json'; import { useEffect, useState } from 'react'; import { useRecoilValue, useRecoilState } from 'recoil'; -import { EditPresetProps, SetOption, TPreset } from 'librechat-data-provider'; +import { EditPresetProps, SetOption, tPresetSchema } from 'librechat-data-provider'; import { Dialog, DialogButton } from '~/components/ui'; import DialogTemplate from '~/components/ui/DialogTemplate'; import SaveAsPresetDialog from './SaveAsPresetDialog'; @@ -21,12 +21,11 @@ const EndpointOptionsDialog = ({ open, onOpenChange, preset: _preset, title }: E const setOption: SetOption = (param) => (newValue) => { const update = {}; update[param] = newValue; - setPreset( - (prevState) => - ({ - ...prevState, - ...update, - } as TPreset), + setPreset((prevState) => + tPresetSchema.parse({ + ...prevState, + ...update, + }), ); }; diff --git a/client/src/components/Endpoints/SaveAsPresetDialog.tsx b/client/src/components/Endpoints/SaveAsPresetDialog.tsx index b9b81fb846..cd9f535f4e 100644 --- a/client/src/components/Endpoints/SaveAsPresetDialog.tsx +++ b/client/src/components/Endpoints/SaveAsPresetDialog.tsx @@ -1,6 +1,6 @@ import React, { useEffect, useState } from 'react'; import { useRecoilValue } from 'recoil'; -import { useCreatePresetMutation, EditPresetProps, TPreset } from 'librechat-data-provider'; +import { useCreatePresetMutation, EditPresetProps } from 'librechat-data-provider'; import { Dialog, Input, Label } from '~/components/ui/'; import DialogTemplate from '~/components/ui/DialogTemplate'; import { cn, defaultTextPropsLabel, removeFocusOutlines, cleanupPreset } from '~/utils/'; @@ -20,7 +20,7 @@ const SaveAsPresetDialog = ({ open, onOpenChange, preset }: EditPresetProps) => title, }, endpointsConfig, - }) as TPreset; + }); createPresetMutation.mutate(_preset); }; diff --git a/client/src/components/Endpoints/Settings/Examples.tsx b/client/src/components/Endpoints/Settings/Examples.tsx index fd737bfd56..ee697f75be 100644 --- a/client/src/components/Endpoints/Settings/Examples.tsx +++ b/client/src/components/Endpoints/Settings/Examples.tsx @@ -68,14 +68,14 @@ function Examples({ readonly, examples, setExample, addExample, removeExample }: