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.

6.0 KB
Raw Blame
'use client';

import { isDesktop } from '@lobechat/const';
import { TITLE_BAR_HEIGHT } from '@lobechat/desktop-bridge';
import { type SkillResourceTreeNode } from '@lobechat/types';
import {
  Alert,
  Button,
  confirmModal,
  createStaticStyles,
  Drawer,
  Flexbox,
  toast,
} from '@lobehub/ui';
import { useForm } from '@lobehub/ui/form';
import { memo, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { ArticleSkeleton } from '@/components/Skeleton';
import ContentViewer from '@/features/AgentSkillDetail/ContentViewer';
import FileTree from '@/features/FileTree';
import { usePermission } from '@/hooks/usePermission';
import { useToolStore } from '@/store/tool';

import SkillEditForm, { type SkillEditFormValues } from './SkillEditForm';

const styles = createStaticStyles(({ css, cssVar }) => ({
  divider: css`
    flex-shrink: 0;
    width: 1px;
    background: ${cssVar.colorBorderSecondary};
  `,
  left: css`
    overflow-y: auto;
    flex-shrink: 0;
    width: 240px;
    padding: 8px;
  `,
  right: css`
    container-type: size;
    overflow: auto;
    flex: 1;
  `,
}));

const buildContentMap = (nodes: SkillResourceTreeNode[]): Record<string, string> => {
  const map: Record<string, string> = {};
  const walk = (items: SkillResourceTreeNode[]) => {
    for (const node of items) {
      if (node.type === 'file' && node.content !== undefined) {
        map[node.path] = node.content;
      } else if (node.children) {
        walk(node.children);
      }
    }
  };
  walk(nodes);
  return map;
};

interface AgentSkillEditProps {
  onClose: () => void;
  open: boolean;
  skillId: string;
}

const AgentSkillEdit = memo<AgentSkillEditProps>(({ skillId, open, onClose }) => {
  const { t } = useTranslation('setting');
  const { t: tp } = useTranslation('plugin');
  const { t: tc } = useTranslation('common');

  const { allowed: canEdit } = usePermission('edit_own_content');

  const [selectedFile, setSelectedFile] = useState('SKILL.md');
  const [saving, setSaving] = useState(false);

  const { data, isLoading } = useToolStore((s) => s.useFetchAgentSkillDetail)(
    open ? skillId : undefined,
  );
  const updateAgentSkill = useToolStore((s) => s.updateAgentSkill);
  const deleteAgentSkill = useToolStore((s) => s.deleteAgentSkill);

  const skillDetail = data?.skillDetail;
  const resourceTree = useMemo(() => data?.resourceTree ?? [], [data?.resourceTree]);
  const contentMap = useMemo(() => buildContentMap(resourceTree), [resourceTree]);

  const initialValues: SkillEditFormValues = useMemo(
    () => ({
      content: skillDetail?.content || '',
      description: skillDetail?.description || skillDetail?.manifest?.description || '',
    }),
    [skillDetail],
  );

  const handleSubmit = async (values: SkillEditFormValues) => {
    if (!canEdit) return;
    setSaving(true);
    try {
      await updateAgentSkill({
        content: values.content,
        id: skillId,
        manifest: { description: values.description },
      });
      toast.success(t('agentSkillEdit.saveSuccess'));
      onClose();
    } finally {
      setSaving(false);
    }
  };

  const form = useForm<SkillEditFormValues>({ initialValues, onSubmit: handleSubmit });

  const handleDelete = async () => {
    if (!canEdit) return;
    await deleteAgentSkill(skillId);
    toast.success(tp('dev.deleteSuccess'));
    onClose();
  };

  const footer = (
    <Flexbox horizontal flex={1} gap={12} justify={'space-between'}>
      <Button
        danger
        disabled={!canEdit}
        onClick={() =>
          confirmModal({
            cancelText: tc('cancel'),
            okButtonProps: { danger: true },
            okText: tc('ok'),
            onOk: handleDelete,
            content: tp('dev.confirmDeleteDevPlugin'),
            title: tc('delete'),
          })
        }
      >
        {tc('delete')}
      </Button>
      <Flexbox horizontal gap={12}>
        <Button onClick={onClose}>{tc('cancel')}</Button>
        <Button
          disabled={!canEdit}
          loading={saving}
          type={'primary'}
          onClick={() => {
            form.submit();
          }}
        >
          {tp('dev.update')}
        </Button>
      </Flexbox>
    </Flexbox>
  );

  return (
    <Drawer
      containerMaxWidth={'auto'}
      footer={footer}
      height={isDesktop ? `calc(100vh - ${TITLE_BAR_HEIGHT}px)` : '100vh'}
      open={open}
      placement={'bottom'}
      push={false}
      title={t('agentSkillEdit.title')}
      styles={{
        bodyContent: { height: '100%', padding: 0 },
      }}
      onClose={onClose}
    >
      {isLoading ? (
        <ArticleSkeleton rows={8} style={{ padding: 16 }} />
      ) : (
        <Flexbox
          horizontal
          height={'100%'}
          onClick={(e) => {
            e.stopPropagation();
          }}
        >
          <div className={styles.left}>
            <FileTree
              resourceTree={resourceTree}
              selectedFile={selectedFile}
              onSelectFile={setSelectedFile}
            />
          </div>
          <div className={styles.divider} />
          <div className={styles.right}>
            <div
              style={{
                display: selectedFile === 'SKILL.md' ? undefined : 'none',
                height: '100%',
                overflow: 'auto',
              }}
            >
              <SkillEditForm
                disabled={!canEdit}
                form={form}
                initialValues={initialValues}
                name={skillDetail?.name}
              />
            </div>
            {selectedFile !== 'SKILL.md' && (
              <>
                <Alert banner showIcon message={t('agentSkillEdit.fileReadonly')} type="info" />
                <ContentViewer
                  contentMap={contentMap}
                  key={selectedFile}
                  selectedFile={selectedFile}
                  skillDetail={skillDetail}
                />
              </>
            )}
          </div>
        </Flexbox>
      )}
    </Drawer>
  );
});

AgentSkillEdit.displayName = 'AgentSkillEdit';

export default AgentSkillEdit;