Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.5 KB
Raw Blame
'use client';

import { Button, Flexbox, Text, useModalContext } from '@lobehub/ui';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import AgentAvatar from '@/components/AgentAvatar';
import { createAgentCharacterTab } from '@/components/AgentAvatar/pickerTab';
import EmojiPicker from '@/components/EmojiPicker';
import CreateVisibilityField, { useCreateVisibility } from '@/features/CreateVisibilityField';
import { useGlobalStore } from '@/store/global';
import { globalGeneralSelectors } from '@/store/global/selectors';

import BlindBoxAvatar from './BlindBoxAvatar';
import IdentityFields from './IdentityFields';
import {
  useCreateAgentIdentityForm,
  type UseCreateAgentIdentityFormOptions,
} from './useCreateAgentIdentityForm';

export type CreateAgentIdentityContentProps = UseCreateAgentIdentityFormOptions;

/**
 * Creating an agent is filling in who it is: avatar on top, then the same
 * identity fields the edit form has. Everything else (prompt, model, skills) is
 * configured on the profile page the new agent opens on.
 */
const CreateAgentIdentityContent = memo<CreateAgentIdentityContentProps>(
  ({ groupId, onCreated, visibility }) => {
    const { t } = useTranslation(['setting', 'common']);
    const { close } = useModalContext();
    const locale = useGlobalStore(globalGeneralSelectors.currentLanguage);
    // `visibility` is only the starting point; in a workspace the user sees and
    // can change who the agent is for before it exists.
    const visibilityState = useCreateVisibility({ groupId, visibility });
    /** Shown under the avatar picker: the modal stays open, so the refusal belongs next to the control. */
    const [avatarError, setAvatarError] = useState('');
    const form = useCreateAgentIdentityForm({
      groupId,
      onCreated: (agentId) => {
        close();
        onCreated(agentId);
      },
      visibility: visibilityState.value,
    });

    return (
      <Flexbox gap={20} padding={20}>
        <Flexbox align={'center'} gap={8}>
          <EmojiPicker
            allowUpload
            allowDelete={!!form.avatar}
            customTabs={[createAgentCharacterTab()]}
            loading={form.uploading}
            locale={locale}
            popupProps={{ placement: 'bottom' }}
            shape={'square'}
            size={72}
            value={form.avatar}
            // Same avatar the agent wears everywhere else. Without an emoji or
            // image it is the generated character — which the agent's future id
            // decides, so the preview cross-fades as a blind box rather than
            // promising one character.
            customRender={() =>
              form.avatar ? (
                <AgentAvatar
                  avatar={form.avatar}
                  className={'lobe-emoji-picker-avatar'}
                  name={form.name}
                  size={72}
                />
              ) : (
                <BlindBoxAvatar className={'lobe-emoji-picker-avatar'} name={form.name} size={72} />
              )
            }
            onDelete={() => form.setAvatar(undefined)}
            onChange={(avatar) => {
              setAvatarError('');
              form.setAvatar(avatar);
            }}
            onUpload={async (file) => {
              setAvatarError('');
              const outcome = await form.uploadAvatar(file);
              if (outcome === 'tooLarge')
                setAvatarError(t('settingAgent.avatar.sizeExceeded', { ns: 'setting' }));
              if (outcome === 'failed')
                setAvatarError(t('settingAgent.avatar.uploadFailed', { ns: 'setting' }));
            }}
          />
          {avatarError && (
            <Text fontSize={12} role={'alert'} type={'danger'}>
              {avatarError}
            </Text>
          )}
        </Flexbox>
        <IdentityFields
          form={form}
          slugPlaceholder={t('settingAgent.slug.autoPlaceholder', { ns: 'setting' })}
        />
        <CreateVisibilityField state={visibilityState} />
        <Flexbox horizontal gap={8} justify={'flex-end'}>
          <Button disabled={form.saving} onClick={() => close()}>
            {t('cancel', { ns: 'common' })}
          </Button>
          <Button
            disabled={form.saving || form.uploading}
            loading={form.saving}
            type={'primary'}
            onClick={() => {
              void form.submit();
            }}
          >
            {t('settingAgent.identity.create', { ns: 'setting' })}
          </Button>
        </Flexbox>
      </Flexbox>
    );
  },
);

export default CreateAgentIdentityContent;