import { useEffect, useState } from 'react'; import { useForm, FormProvider } from 'react-hook-form'; import { AuthTypeEnum, AuthorizationTypeEnum, TokenExchangeMethodEnum, } from 'librechat-data-provider'; import type { AssistantPanelProps, ActionAuthForm } from '~/common'; import { useAssistantsMapContext, useToastContext } from '~/Providers'; import { Dialog, DialogTrigger, OGDialog, OGDialogTrigger, Label } from '~/components/ui'; import OGDialogTemplate from '~/components/ui/OGDialogTemplate'; import { useDeleteAction } from '~/data-provider'; import { TrashIcon } from '~/components/svg'; import useLocalize from '~/hooks/useLocalize'; import ActionsInput from './ActionsInput'; import ActionsAuth from './ActionsAuth'; import { Panel } from '~/common'; export default function ActionsPanel({ // activePanel, action, endpoint, version, setAction, assistant_id, setActivePanel, }: AssistantPanelProps) { const localize = useLocalize(); const { showToast } = useToastContext(); const assistantMap = useAssistantsMapContext(); const [openAuthDialog, setOpenAuthDialog] = useState(false); const deleteAction = useDeleteAction({ onSuccess: () => { showToast({ message: localize('com_assistants_delete_actions_success'), status: 'success', }); setActivePanel(Panel.builder); setAction(undefined); }, onError(error) { showToast({ message: (error as Error)?.message ?? localize('com_assistants_delete_actions_error'), status: 'error', }); }, }); const methods = useForm({ defaultValues: { /* General */ type: AuthTypeEnum.None, saved_auth_fields: false, /* API key */ api_key: '', authorization_type: AuthorizationTypeEnum.Basic, custom_auth_header: '', /* OAuth */ oauth_client_id: '', oauth_client_secret: '', authorization_url: '', client_url: '', scope: '', token_exchange_method: TokenExchangeMethodEnum.DefaultPost, }, }); const { reset, watch } = methods; const type = watch('type'); useEffect(() => { if (action?.metadata?.auth) { reset({ type: action.metadata.auth.type || AuthTypeEnum.None, saved_auth_fields: false, api_key: action.metadata.api_key ?? '', authorization_type: action.metadata.auth.authorization_type || AuthorizationTypeEnum.Basic, oauth_client_id: action.metadata.oauth_client_id ?? '', oauth_client_secret: action.metadata.oauth_client_secret ?? '', authorization_url: action.metadata.auth.authorization_url ?? '', client_url: action.metadata.auth.client_url ?? '', scope: action.metadata.auth.scope ?? '', token_exchange_method: action.metadata.auth.token_exchange_method ?? TokenExchangeMethodEnum.DefaultPost, }); } }, [action, reset]); return (
{!!action && (
{localize('com_ui_delete_action_confirm')} } selection={{ selectHandler: () => { if (!assistant_id) { return showToast({ message: 'No assistant_id found, is the assistant created?', status: 'error', }); } deleteAction.mutate({ model: assistantMap[endpoint][assistant_id].model, action_id: action.action_id, assistant_id, endpoint, }); }, selectClasses: 'bg-red-700 dark:bg-red-600 hover:bg-red-800 dark:hover:bg-red-800 transition-color duration-200 text-white', selectText: localize('com_ui_delete'), }} />
)}
{(action ? 'Edit' : 'Add') + ' ' + 'actions'}
{localize('com_assistants_actions_info')}
{/* */}
{type}
); }