From 13097291cc0c6d844d639aff24ec6b70c99369a9 Mon Sep 17 00:00:00 2001 From: Alfredo Gallardo Date: Thu, 9 Nov 2023 12:45:24 -0300 Subject: [PATCH] - feature: default agent settings (#78) * - feature: added default agent setting * - fix: default agent race --- .../src/components/create-job/create-job.tsx | 72 +++++++++--- .../src/components/settings/settings.tsx | 104 +++++++++++++++++- apps/shinkai-visor/src/lang/en.json | 3 +- apps/shinkai-visor/src/lang/es.json | 3 +- .../src/store/settings/settings.ts | 28 +++++ 5 files changed, 191 insertions(+), 19 deletions(-) create mode 100644 apps/shinkai-visor/src/store/settings/settings.ts diff --git a/apps/shinkai-visor/src/components/create-job/create-job.tsx b/apps/shinkai-visor/src/components/create-job/create-job.tsx index bd3d21ba5..9204bf598 100644 --- a/apps/shinkai-visor/src/components/create-job/create-job.tsx +++ b/apps/shinkai-visor/src/components/create-job/create-job.tsx @@ -11,6 +11,7 @@ import { z } from 'zod'; import { useQuery } from '../../hooks/use-query'; import { useAuth } from '../../store/auth/auth'; +import { useSettings } from '../../store/settings/settings'; import { FileInput } from '../file-input/file-input'; import { Header } from '../header/header'; import { Button } from '../ui/button'; @@ -44,9 +45,10 @@ type FormSchemaType = z.infer; export const CreateJob = () => { const history = useHistory(); const intl = useIntl(); - const location = useLocation<{ files: File[], agentName: string }>(); + const location = useLocation<{ files: File[]; agentName: string }>(); const query = useQuery(); const auth = useAuth((state) => state.auth); + const settings = useSettings((state) => state.settings); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { @@ -72,26 +74,59 @@ export const CreateJob = () => { history.replace(`/inboxes/${jobId}`); }, }); - const extensions = [".eml", ".html", ".json", ".md", ".msg", ".rst", ".rtf", ".txt", ".xml", ".jpeg", ".png", ".csv", ".doc", ".docx", ".epub", ".odt", ".pdf", ".ppt", ".pptx", ".tsv", ".xlsx"]; - - //TODO: Replace this assigment with a configured default agent - useEffect(() => { - if (agents?.length) { - const defaultAgentId = agents[0].id; - form.setValue('agent', defaultAgentId); - } - }, [agents, form]); + const extensions = [ + '.eml', + '.html', + '.json', + '.md', + '.msg', + '.rst', + '.rtf', + '.txt', + '.xml', + '.jpeg', + '.png', + '.csv', + '.doc', + '.docx', + '.epub', + '.odt', + '.pdf', + '.ppt', + '.pptx', + '.tsv', + '.xlsx', + ]; useEffect(() => { form.setValue('files', location?.state?.files || []); }, [location, form]); useEffect(() => { - if (location?.state?.agentName) { - const agent = agents.find(agent => (agent.full_identity_name as any)?.subidentity_name === location.state.agentName); - if (agent) { - form.setValue('agent', agent.id); - } + if (!location?.state?.agentName) { + return; + } + const agent = agents.find( + (agent) => + (agent.full_identity_name as any)?.subidentity_name === + location.state.agentName + ); + if (agent) { + form.setValue('agent', agent.id); } }, [form, location, agents]); + useEffect(() => { + if (form.getValues().agent) { + return; + } + let defaultAgentId = ''; + defaultAgentId = + defaultAgentId || + (settings?.defaultAgentId && + agents.find((agent) => agent.id === settings.defaultAgentId) + ? settings.defaultAgentId + : ''); + defaultAgentId = defaultAgentId || (agents?.length ? agents[0].id : ''); + form.setValue('agent', defaultAgentId); + }, [form, location, agents, settings]); const submit = (values: FormSchemaType) => { if (!auth) return; let content = values.content; @@ -170,7 +205,12 @@ export const CreateJob = () => { - + diff --git a/apps/shinkai-visor/src/components/settings/settings.tsx b/apps/shinkai-visor/src/components/settings/settings.tsx index c6f45f925..d2370130e 100644 --- a/apps/shinkai-visor/src/components/settings/settings.tsx +++ b/apps/shinkai-visor/src/components/settings/settings.tsx @@ -1,22 +1,124 @@ +import { zodResolver } from '@hookform/resolvers/zod'; +import { useAgents } from '@shinkai_network/shinkai-node-state/lib/queries/getAgents/useGetAgents'; import { FileKey, SettingsIcon } from 'lucide-react'; +import { useEffect } from 'react'; +import { useForm, useWatch } from 'react-hook-form'; import { FormattedMessage } from 'react-intl'; import { useHistory } from 'react-router'; +import { z } from 'zod'; +import { useAuth } from '../../store/auth/auth'; +import { useSettings } from '../../store/settings/settings'; import { Header } from '../header/header'; import { Button } from '../ui/button'; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from '../ui/form'; +import { + Select, + SelectContent, + SelectItem, + SelectPortal, + SelectTrigger, + SelectValue, +} from '../ui/select'; + +const formSchema = z.object({ + defaultAgentId: z.string().optional(), +}); + +type FormSchemaType = z.infer; export const Settings = () => { const history = useHistory(); + const auth = useAuth((authStore) => authStore.auth); + const { settings, setSettings } = useSettings( + (settingsStore) => settingsStore + ); + const form = useForm({ + resolver: zodResolver(formSchema), + defaultValues: { + defaultAgentId: '', + }, + }); + const defaultAgentId = useWatch({ name: 'defaultAgentId', control: form.control }); + const { agents } = useAgents({ + sender: auth?.shinkai_identity ?? '', + senderSubidentity: `${auth?.profile}`, + shinkaiIdentity: auth?.shinkai_identity ?? '', + my_device_encryption_sk: auth?.profile_encryption_sk ?? '', + my_device_identity_sk: auth?.profile_identity_sk ?? '', + node_encryption_pk: auth?.node_encryption_pk ?? '', + profile_encryption_sk: auth?.profile_encryption_sk ?? '', + profile_identity_sk: auth?.profile_identity_sk ?? '', + }); const exportConnection = () => { history.push('settings/export-connection'); }; + useEffect(() => { + if (defaultAgentId) { + setSettings({ defaultAgentId }) + } + }, [defaultAgentId, setSettings]); + useEffect(() => { + form.setValue('defaultAgentId', settings?.defaultAgentId); + }, [settings, form]); return (
} title={} /> -
+
+
+ + ( + + + + + + + + )} + /> + + +