From f2110b821f9f7cdc48440bc78593ce7dcb0c5cfd Mon Sep 17 00:00:00 2001 From: Paul Ccari <46382556+paulclindo@users.noreply.github.com> Date: Fri, 15 Nov 2024 00:52:34 -0500 Subject: [PATCH] feat: implement AI Agents (#527) * feat: add server state for agents feature * implement list/create/remove agents * fixes * fixes * fixes * fixes * refactor * improvements * improvements * fixes * updates * update creation tool * update custom prompt * feat: agents as experimental feature * feat: edit agent * feat: edit agent --------- Co-authored-by: Nico Arqueros --- .../src/components/agent/add-agent.tsx | 533 +++++++++++++++++ .../src/components/agent/agents.tsx | 297 ++++++++++ .../src/components/agent/edit-agent.tsx | 544 ++++++++++++++++++ .../ai-update-selection-action-bar.tsx | 40 +- .../components/chat/conversation-footer.tsx | 19 +- .../onboarding-checklist/onboarding.tsx | 2 +- .../pages/{create-agent.tsx => add-ai.tsx} | 4 +- .../src/pages/{agents.tsx => ais.tsx} | 314 ++++++---- .../src/pages/chat/empty-message.tsx | 2 +- .../shinkai-desktop/src/pages/create-tool.tsx | 5 + .../src/pages/layout/main-layout.tsx | 2 +- .../{ai-model-locally.tsx => local-ais.tsx} | 6 +- apps/shinkai-desktop/src/pages/tools.tsx | 22 +- apps/shinkai-desktop/src/routes/index.tsx | 18 +- apps/shinkai-desktop/src/routes/name.ts | 2 +- .../src/api/agents/index.ts | 87 +++ .../src/api/agents/types.ts | 29 + libs/shinkai-message-ts/src/api/jobs/types.ts | 1 + libs/shinkai-node-state/src/v2/constants.ts | 2 + .../src/v2/mutations/createAgent/index.ts | 14 + .../src/v2/mutations/createAgent/types.ts | 12 + .../mutations/createAgent/useCreateAgent.ts | 33 ++ .../src/v2/mutations/removeAgent/index.ts | 14 + .../src/v2/mutations/removeAgent/types.ts | 8 + .../mutations/removeAgent/useRemoveAgent.ts | 30 + .../src/v2/mutations/updateAgent/index.ts | 14 + .../src/v2/mutations/updateAgent/types.ts | 12 + .../mutations/updateAgent/useUpdateAgent.ts | 33 ++ .../src/v2/queries/getAgent/index.ts | 14 + .../src/v2/queries/getAgent/types.ts | 9 + .../src/v2/queries/getAgent/useGetAgent.ts | 13 + .../src/v2/queries/getAgents/index.ts | 8 + .../src/v2/queries/getAgents/types.ts | 8 + .../src/v2/queries/getAgents/useGetAgents.ts | 13 + .../getLLMProviders/useGetLLMProviders.ts | 2 +- .../queries/getToolsList/useGetToolsList.ts | 1 - libs/shinkai-ui/src/assets/icons/general.tsx | 79 +++ 37 files changed, 2098 insertions(+), 148 deletions(-) create mode 100644 apps/shinkai-desktop/src/components/agent/add-agent.tsx create mode 100644 apps/shinkai-desktop/src/components/agent/agents.tsx create mode 100644 apps/shinkai-desktop/src/components/agent/edit-agent.tsx rename apps/shinkai-desktop/src/pages/{create-agent.tsx => add-ai.tsx} (99%) rename apps/shinkai-desktop/src/pages/{agents.tsx => ais.tsx} (57%) create mode 100644 apps/shinkai-desktop/src/pages/create-tool.tsx rename apps/shinkai-desktop/src/pages/{ai-model-locally.tsx => local-ais.tsx} (93%) create mode 100644 libs/shinkai-message-ts/src/api/agents/index.ts create mode 100644 libs/shinkai-message-ts/src/api/agents/types.ts create mode 100644 libs/shinkai-node-state/src/v2/mutations/createAgent/index.ts create mode 100644 libs/shinkai-node-state/src/v2/mutations/createAgent/types.ts create mode 100644 libs/shinkai-node-state/src/v2/mutations/createAgent/useCreateAgent.ts create mode 100644 libs/shinkai-node-state/src/v2/mutations/removeAgent/index.ts create mode 100644 libs/shinkai-node-state/src/v2/mutations/removeAgent/types.ts create mode 100644 libs/shinkai-node-state/src/v2/mutations/removeAgent/useRemoveAgent.ts create mode 100644 libs/shinkai-node-state/src/v2/mutations/updateAgent/index.ts create mode 100644 libs/shinkai-node-state/src/v2/mutations/updateAgent/types.ts create mode 100644 libs/shinkai-node-state/src/v2/mutations/updateAgent/useUpdateAgent.ts create mode 100644 libs/shinkai-node-state/src/v2/queries/getAgent/index.ts create mode 100644 libs/shinkai-node-state/src/v2/queries/getAgent/types.ts create mode 100644 libs/shinkai-node-state/src/v2/queries/getAgent/useGetAgent.ts create mode 100644 libs/shinkai-node-state/src/v2/queries/getAgents/index.ts create mode 100644 libs/shinkai-node-state/src/v2/queries/getAgents/types.ts create mode 100644 libs/shinkai-node-state/src/v2/queries/getAgents/useGetAgents.ts diff --git a/apps/shinkai-desktop/src/components/agent/add-agent.tsx b/apps/shinkai-desktop/src/components/agent/add-agent.tsx new file mode 100644 index 000000000..0a291cf7f --- /dev/null +++ b/apps/shinkai-desktop/src/components/agent/add-agent.tsx @@ -0,0 +1,533 @@ +import { zodResolver } from '@hookform/resolvers/zod'; +import { useTranslation } from '@shinkai_network/shinkai-i18n'; +import { DEFAULT_CHAT_CONFIG } from '@shinkai_network/shinkai-node-state/v2/constants'; +import { useCreateAgent } from '@shinkai_network/shinkai-node-state/v2/mutations/createAgent/useCreateAgent'; +import { useGetLLMProviders } from '@shinkai_network/shinkai-node-state/v2/queries/getLLMProviders/useGetLLMProviders'; +import { useGetTools } from '@shinkai_network/shinkai-node-state/v2/queries/getToolsList/useGetToolsList'; +import { + Button, + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, + HoverCard, + HoverCardContent, + HoverCardTrigger, + Label, + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, + Slider, + Switch, + Textarea, + TextField, + Tooltip, + TooltipContent, + TooltipPortal, + TooltipProvider, + TooltipTrigger, +} from '@shinkai_network/shinkai-ui'; +import { formatText } from '@shinkai_network/shinkai-ui/helpers'; +import { PlusIcon } from 'lucide-react'; +import { useForm } from 'react-hook-form'; +import { useNavigate } from 'react-router-dom'; +import { toast } from 'sonner'; +import { z } from 'zod'; + +import { SubpageLayout } from '../../pages/layout/simple-layout'; +import { useAuth } from '../../store/auth'; +import { useSettings } from '../../store/settings'; + +const addAgentFormSchema = z.object({ + name: z + .string() + .regex( + /^[a-zA-Z0-9]+(_[a-zA-Z0-9]+)*$/, + 'It just accepts alphanumeric characters and underscores', + ), + llmProviderId: z.string(), + uiDescription: z.string(), + storage_path: z.string(), + knowledge: z.array(z.string()), + tools: z.array(z.string()), + debugMode: z.boolean(), + config: z + .object({ + custom_prompt: z.string(), + custom_system_prompt: z.string(), + temperature: z.number(), + top_k: z.number(), + top_p: z.number(), + stream: z.boolean(), + other_model_params: z.record(z.string()), + }) + .nullable(), +}); + +type AddAgentFormValues = z.infer; + +function AddAgentPage() { + const defaultAgentId = useSettings((state) => state.defaultAgentId); + const auth = useAuth((state) => state.auth); + const navigate = useNavigate(); + const { t } = useTranslation(); + const form = useForm({ + resolver: zodResolver(addAgentFormSchema), + defaultValues: { + name: '', + uiDescription: '', + storage_path: '', + knowledge: [], + tools: [], + debugMode: false, + config: { + stream: false, // disable stream by default for tooling + temperature: DEFAULT_CHAT_CONFIG.temperature, + top_p: DEFAULT_CHAT_CONFIG.top_p, + top_k: DEFAULT_CHAT_CONFIG.top_k, + custom_prompt: '', + custom_system_prompt: '', + other_model_params: {}, + }, + llmProviderId: defaultAgentId, + }, + }); + + const { data: toolsList } = useGetTools({ + nodeAddress: auth?.node_address ?? '', + token: auth?.api_v2_key ?? '', + }); + + const { mutateAsync: createAgent, isPending } = useCreateAgent({ + onError: (error) => { + toast.error('Failed to create agent', { + description: error.response?.data?.message ?? error.message, + }); + }, + onSuccess: () => { + navigate('/ais?tab=agents'); + }, + }); + + const { llmProviders } = useGetLLMProviders({ + nodeAddress: auth?.node_address ?? '', + token: auth?.api_v2_key ?? '', + }); + + const submit = async (values: AddAgentFormValues) => { + await createAgent({ + nodeAddress: auth?.node_address ?? '', + token: auth?.api_v2_key ?? '', + agent: { + agent_id: values.name, + full_identity_name: `${auth?.shinkai_identity}/main/agent/${values.name}`, + llm_provider_id: values.llmProviderId, + ui_description: values.uiDescription, + storage_path: values.storage_path, + knowledge: values.knowledge, + tools: values.tools, + debug_mode: values.debugMode, + config: values.config, + name: values.name, + }, + }); + }; + + return ( + +

+ Create and explore custom AI agents with tailored instructions and + diverse skills. +

+
+ +
+
+ ( + { + const value = e.target.value; + const alphanumericValue = value.replace( + /[^a-zA-Z0-9_]/g, + '_', + ); + field.onChange({ + ...e, + target: { + value: alphanumericValue, + }, + }); + }, + }} + label="Agent Name" + /> + )} + /> + ( + + Description + +