Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement change agent in job #254

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 83 additions & 25 deletions apps/shinkai-desktop/src/pages/chat/chat-conversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
import { useSendMessageToJob } from '@shinkai_network/shinkai-node-state/lib/mutations/sendMessageToJob/useSendMessageToJob';
import { useSendMessageToInbox } from '@shinkai_network/shinkai-node-state/lib/mutations/sendMesssageToInbox/useSendMessageToInbox';
import { useSendMessageWithFilesToInbox } from '@shinkai_network/shinkai-node-state/lib/mutations/sendMesssageWithFilesToInbox/useSendMessageWithFilesToInbox';
import { useUpdateAgentInJob } from '@shinkai_network/shinkai-node-state/lib/mutations/updateAgentInJob/useUpdateAgentInJob';
import { useAgents } from '@shinkai_network/shinkai-node-state/lib/queries/getAgents/useGetAgents';
import { useGetChatConversationWithPagination } from '@shinkai_network/shinkai-node-state/lib/queries/getChatConversation/useGetChatConversationWithPagination';
import {
Alert,
Expand All @@ -21,6 +23,11 @@ import {
Badge,
Button,
ChatInputArea,
DropdownMenu,
DropdownMenuContent,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuTrigger,
Form,
FormControl,
FormField,
Expand All @@ -33,13 +40,9 @@ import {
SheetHeader,
SheetTitle,
SheetTrigger,
Tooltip,
TooltipContent,
TooltipPortal,
TooltipProvider,
TooltipTrigger,
} from '@shinkai_network/shinkai-ui';
import {
AgentIcon,
DirectoryTypeIcon,
FileTypeIcon,
} from '@shinkai_network/shinkai-ui/assets';
Expand All @@ -50,6 +53,7 @@ import { useEffect, useMemo, useRef } from 'react';
import { useDropzone } from 'react-dropzone';
import { useForm } from 'react-hook-form';
import { useParams } from 'react-router-dom';
import { toast } from 'sonner';

import { useGetCurrentInbox } from '../../hooks/use-current-inbox';
import { useAuth } from '../../store/auth';
Expand Down Expand Up @@ -331,6 +335,77 @@ const ChatConversation = () => {

export default ChatConversation;

function AgentSelection() {
const auth = useAuth((state) => state.auth);
const currentInbox = useGetCurrentInbox();
const { agents } = useAgents({
nodeAddress: auth?.node_address ?? '',
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 { mutateAsync: updateAgentInJob } = useUpdateAgentInJob({
onError: (error) => {
toast.error('Failed to update agent', {
description: error.message,
});
},
});
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Badge className="hover:bg-gray-350 inline-flex cursor-pointer items-center gap-1 truncate bg-gray-300 text-start text-xs font-normal text-gray-50 shadow-none hover:text-white">
{/*<AgentIcon className="h-3 w-3" />*/}
<span>{currentInbox?.agent?.id}</span>
</Badge>
</DropdownMenuTrigger>
<DropdownMenuContent
align="start"
className="max-h-[300px] w-[300px] overflow-y-auto bg-gray-300 p-1 py-2"
>
<DropdownMenuRadioGroup
onValueChange={async (value) => {
const jobId = extractJobIdFromInbox(currentInbox?.inbox_id ?? '');
await updateAgentInJob({
nodeAddress: auth?.node_address ?? '',
shinkaiIdentity: auth?.shinkai_identity ?? '',
profile: auth?.profile ?? '',
jobId: jobId,
newAgentId: value,
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 ?? '',
});
}}
value={currentInbox?.agent?.id ?? ''}
>
{agents.map((agent) => (
<DropdownMenuRadioItem
className="flex cursor-pointer items-center gap-2 rounded-md px-3 py-2 text-white transition-colors hover:bg-gray-200 aria-checked:bg-gray-200"
key={agent.id}
value={agent.id}
>
<AgentIcon className="h-4 w-4" />
<div className="flex flex-col gap-1">
<span className="text-xs">{agent.id}</span>
{/*<span className="text-gray-80 text-xs">{agent.model}</span>*/}
</div>
</DropdownMenuRadioItem>
))}
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
);
}

export const ConversationHeader = () => {
const currentInbox = useGetCurrentInbox();

Expand All @@ -342,28 +417,11 @@ export const ConversationHeader = () => {

return (
<div className="flex h-[58px] items-center justify-between border-b border-gray-400 px-4 py-2">
<div className="space-x-2.5">
<span className="line-clamp-1 inline text-sm font-medium capitalize text-white">
<div className="inline-flex items-center">
<span className="mr-2.5 line-clamp-1 inline text-sm font-medium capitalize text-white">
{currentInbox?.custom_name || currentInbox?.inbox_id}
</span>
<TooltipProvider delayDuration={0}>
<Tooltip>
<TooltipTrigger asChild>
<Badge className="text-gray-80 inline cursor-pointer truncate bg-gray-400 text-start text-xs font-normal shadow-none">
{currentInbox?.agent?.id}
</Badge>
</TooltipTrigger>
<TooltipPortal>
<TooltipContent
className="flex items-center gap-2"
sideOffset={5}
>
<span className="text-gray-80">Model: </span>
{currentInbox?.agent?.model}
</TooltipContent>
</TooltipPortal>
</Tooltip>
</TooltipProvider>
<AgentSelection />
</div>
{hasConversationContext && (
<Sheet>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { zodResolver } from '@hookform/resolvers/zod';
import { AgentInbox } from '@shinkai_network/shinkai-message-ts/models';
import { extractJobIdFromInbox } from '@shinkai_network/shinkai-message-ts/utils';
import {
UpdateInboxNameFormSchema,
updateInboxNameFormSchema,
} from '@shinkai_network/shinkai-node-state/forms/chat/inbox';
import { useUpdateAgentInJob } from '@shinkai_network/shinkai-node-state/lib/mutations/updateAgentInJob/useUpdateAgentInJob';
import { useUpdateInboxName } from '@shinkai_network/shinkai-node-state/lib/mutations/updateInboxName/useUpdateInboxName';
import { useAgents } from '@shinkai_network/shinkai-node-state/lib/queries/getAgents/useGetAgents';
import {
Badge,
Button,
Expand All @@ -13,16 +16,99 @@ import {
DrawerContent,
DrawerHeader,
DrawerTitle,
DropdownMenu,
DropdownMenuContent,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuTrigger,
Form,
FormField,
TextField,
} from '@shinkai_network/shinkai-ui';
import { AgentIcon } from '@shinkai_network/shinkai-ui/assets';
import { ChevronDown } from 'lucide-react';
import { useEffect } from 'react';
import { useForm } from 'react-hook-form';
import { FormattedMessage } from 'react-intl';
import { toast } from 'sonner';

import { useGetCurrentInbox } from '../../hooks/use-current-inbox';
import { useAuth } from '../../store/auth/auth';

function AgentSelection() {
const auth = useAuth((state) => state.auth);
const currentInbox = useGetCurrentInbox();
const { agents } = useAgents({
nodeAddress: auth?.node_address ?? '',
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 { mutateAsync: updateAgentInJob } = useUpdateAgentInJob({
onError: (error) => {
toast.error('Failed to update agent', {
description: error.message,
});
},
});
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Badge className="hover:bg-gray-350 inline-flex cursor-pointer items-center gap-3 self-start truncate rounded-xl bg-gray-300 px-1.5 py-1 text-start text-xs font-normal text-gray-50 shadow-none hover:text-white [&[data-state=open]>svg]:rotate-180">
<div className="flex items-center gap-1">
<AgentIcon className="h-4 w-4" />
<span>{currentInbox?.agent?.id}</span>
</div>
<ChevronDown className="h-3 w-3" />
</Badge>
</DropdownMenuTrigger>
<DropdownMenuContent
align="start"
className="max-h-[300px] w-[300px] overflow-y-auto bg-gray-300 p-1 py-2"
>
<DropdownMenuRadioGroup
onValueChange={async (value) => {
const jobId = extractJobIdFromInbox(currentInbox?.inbox_id ?? '');
await updateAgentInJob({
nodeAddress: auth?.node_address ?? '',
shinkaiIdentity: auth?.shinkai_identity ?? '',
profile: auth?.profile ?? '',
jobId: jobId,
newAgentId: value,
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 ?? '',
});
}}
value={currentInbox?.agent?.id ?? ''}
>
{agents.map((agent) => (
<DropdownMenuRadioItem
className="flex cursor-pointer items-center gap-2 rounded-md px-3 py-2 text-white transition-colors hover:bg-gray-200 aria-checked:bg-gray-200"
key={agent.id}
value={agent.id}
>
<AgentIcon className="h-4 w-4" />
<div className="flex flex-col gap-1">
<span className="text-xs">{agent.id}</span>
{/*<span className="text-gray-80 text-xs">{agent.model}</span>*/}
</div>
</DropdownMenuRadioItem>
))}
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
);
}

export type EditInboxNameDialogProps = {
open: boolean;
onCancel: () => void;
Expand Down Expand Up @@ -89,12 +175,7 @@ export const EditInboxNameDialog = ({
</DrawerHeader>
<div className="mb-4 flex justify-between text-sm">
<span className="text-gray-80 flex">Current AI Agent</span>
<div className="space-x-2.5">
<span className="font-medium">{currentAgent?.id}</span>
<Badge className="text-gray-80 truncate bg-gray-400 text-start text-xs font-normal shadow-none">
{currentAgent?.model}
</Badge>
</div>
<AgentSelection />
</div>
<Form {...form}>
<form
Expand Down
2 changes: 1 addition & 1 deletion apps/shinkai-visor/src/components/nav/nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ export default function NavBar() {
};

return (
<nav className="">
<nav className="pb-2.5">
<AlertDialog open={isConfirmLogoutDialogOpened}>
<AlertDialogContent className="w-[75%]">
<AlertDialogHeader>
Expand Down
35 changes: 35 additions & 0 deletions libs/shinkai-message-ts/src/api/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,41 @@ export const createJob = async (
const data = response.data;
return data.data;
};

export const updateAgentInJob = async (
nodeAddress: string,
jobId: string,
newAgentId: string,
sender: string,
sender_subidentity: string,
receiver: string,
receiver_subidentity: string,
setupDetailsState: CredentialsPayload,
): Promise<{ data: string; status: string }> => {
const messageStr = ShinkaiMessageBuilderWrapper.updateAgentInJob(
setupDetailsState.profile_encryption_sk,
setupDetailsState.profile_identity_sk,
setupDetailsState.node_encryption_pk,
jobId,
newAgentId,
sender,
sender_subidentity,
receiver,
receiver_subidentity,
);

const message = JSON.parse(messageStr);

const response = await httpClient.post(
urlJoin(nodeAddress, '/v1/change_job_agent'),
message,
{
responseType: 'json',
},
);
const data = response.data;
return data;
};
export const sendMessageToJob = async (
nodeAddress: string,
jobId: string,
Expand Down
1 change: 1 addition & 0 deletions libs/shinkai-message-ts/src/models/SchemaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export enum MessageSchemaType {
TextContent = 'TextContent',
SymmetricKeyExchange = 'SymmetricKeyExchange',
APIFinishJob = 'APIFinishJob',
ChangeJobAgentRequest = 'ChangeJobAgentRequest',
Empty = '',
ChangeNodesName = 'ChangeNodesName',
VecFsRetrievePathSimplifiedJson = 'VecFsRetrievePathSimplifiedJson',
Expand Down
Loading
Loading