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.

11.3 KB
Raw Blame
'use client';

import { AGENT_CHAT_URL, DEFAULT_INBOX_AVATAR } from '@lobechat/const';
import { Claude, Cline, Cursor, OpenAI } from '@lobehub/icons';
import {
  Avatar,
  Block,
  Button,
  // oxlint-disable-next-line stylex-migration/no-emotion-styles -- Highlighter renders its pre without a slot
  createStaticStyles,
  Divider,
  Flexbox,
  Highlighter,
  Icon,
  Markdown,
  Select,
  Tabs,
  Text,
} from '@lobehub/ui';
import { BotIcon, UserRoundIcon } from 'lucide-react';
import { memo, useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { useAgentStore } from '@/store/agent';
import { builtinAgentSelectors } from '@/store/agent/selectors';
import { useChatStore } from '@/store/chat';
import { densityVars } from '@/styles/densityVars';

import { useDetailActionContext } from '../../DetailProvider';
import { getSkillInstructionsUrl } from './getSkillInstructionsUrl';
import VsCodeIcon from './VsCodeIcon';

const fallback = createStaticStyles(({ css }) => ({
  lite: css`
    pre {
      padding: ${densityVars.contentGap} !important;
    }
  `,
}));

const Title = ({ children }: { children?: React.ReactNode }) => (
  <Text as={'h3'} fontSize={16} style={{ margin: 0, paddingInline: 8 }} weight={600}>
    {children}
  </Text>
);

type GuideMode = 'agent' | 'human';

enum PlatformType {
  Claude = 'claude',
  Cline = 'cline',
  Codex = 'codex',
  Cursor = 'cursor',
  LobeHub = 'lobehub',
  VsCode = 'vscode',
}

interface PlatformProps {
  downloadUrl?: string;
  expandCodeByDefault?: boolean;
  identifier?: string;
  lite?: boolean;
  mobile?: boolean;
}

const genInstallCommand = (identifier?: string, platform?: PlatformType) => {
  const id = identifier || '<skill-identifier>';

  const agentMap: Record<PlatformType, string> = {
    [PlatformType.Claude]: 'claude-code',
    [PlatformType.Cline]: 'cline',
    [PlatformType.Cursor]: 'cursor',
    [PlatformType.LobeHub]: 'lobehub',
    [PlatformType.Codex]: 'codex',
    [PlatformType.VsCode]: 'vscode',
  };

  switch (platform) {
    case PlatformType.Cursor:
    case PlatformType.Claude:
    case PlatformType.Cline:
    case PlatformType.VsCode: {
      return `npx -y @lobehub/market-cli skills install ${id} --agent ${agentMap[platform]}`;
    }
    case PlatformType.Codex: {
      return `npx -y @lobehub/market-cli skills install ${id} --agent ${agentMap[platform]}`;
    }
    default: {
      return `# Recommended for LobeHub users:
# Open the marketplace page and install with one click:
# https://lobechat.com/community/skills/${id}`;
    }
  }
};

const genLayout = (
  identifier: string | undefined,
  platform: PlatformType,
  i18nText: {
    lobehub: string;
    resourcesHint: string;
  },
) => {
  const id = identifier || '<skill-identifier>';
  const basePathMap: Record<PlatformType, string> = {
    [PlatformType.Claude]: `~/.claude/skills/${id}`,
    [PlatformType.Cline]: `~/.cline/skills/${id}`,
    [PlatformType.Cursor]: `~/.cursor/skills/${id}`,
    [PlatformType.LobeHub]: `<managed-by-lobehub>`,
    [PlatformType.Codex]: `~/.agents/skills/${id}`,
    [PlatformType.VsCode]: `./.vscode/skills/${id}`,
  };
  const basePath = basePathMap[platform];

  if (platform === PlatformType.LobeHub) {
    return i18nText.lobehub;
  }

  return `${basePath}
β”œβ”€β”€ SKILL.md
└── ... (${i18nText.resourcesHint})`;
};

const Platform = memo<PlatformProps>(
  ({ lite, identifier, mobile, expandCodeByDefault, downloadUrl }) => {
    const { t } = useTranslation('discover');
    const navigate = useWorkspaceAwareNavigate();
    const { close } = useDetailActionContext();
    const inboxAgentId = useAgentStore(builtinAgentSelectors.inboxAgentId);
    const sendMessage = useChatStore((s) => s.sendMessage);
    const [active, setActive] = useState<PlatformType>(PlatformType.Claude);
    const [mode, setMode] = useState<GuideMode>('agent');

    const options = [
      {
        icon: <Claude.Color className={'anticon'} size={18} />,
        label: 'Claude Code',
        value: PlatformType.Claude,
      },
      {
        icon: <OpenAI className={'anticon'} size={18} />,
        label: 'Codex',
        value: PlatformType.Codex,
      },
      {
        icon: <Cursor className={'anticon'} size={18} />,
        label: 'Cursor',
        value: PlatformType.Cursor,
      },
      {
        icon: <VsCodeIcon className={'anticon'} size={18} />,
        label: 'VsCode',
        value: PlatformType.VsCode,
      },
      {
        icon: <Cline className={'anticon'} size={18} />,
        label: 'Cline',
        value: PlatformType.Cline,
      },
    ];

    const { platform, steps } = useMemo(() => {
      switch (active) {
        case PlatformType.LobeHub: {
          return {
            platform: 'LobeHub',
            steps: t('skills.details.sidebar.platform.steps.lobehub'),
          };
        }
        case PlatformType.Claude: {
          return {
            platform: 'Claude',
            steps: t('skills.details.sidebar.platform.steps.claude'),
          };
        }
        case PlatformType.Codex: {
          return {
            platform: 'Codex',
            steps: t('skills.details.sidebar.platform.steps.codex'),
          };
        }
        case PlatformType.Cursor: {
          return {
            platform: 'Cursor',
            steps: t('skills.details.sidebar.platform.steps.cursor'),
          };
        }
        case PlatformType.Cline: {
          return {
            platform: 'Cline',
            steps: t('skills.details.sidebar.platform.steps.cline'),
          };
        }
        case PlatformType.VsCode: {
          return {
            platform: 'VS Code',
            steps: t('skills.details.sidebar.platform.steps.vscode'),
          };
        }
      }
    }, [active, t]);

    const command = genInstallCommand(identifier, active);

    const agentPrompt = `Curl ${getSkillInstructionsUrl(identifier || '<skill-identifier>')}, then follow the instructions to set up LobeHub Skills Marketplace and install the skill. Once installed, read the SKILL.md file in the installed directory and follow its instructions to complete the task.`;

    const handleUseOnLobeAI = useCallback(() => {
      if (!inboxAgentId) return;

      // Send message to LobeAI
      sendMessage({
        context: { agentId: inboxAgentId },
        message: agentPrompt,
      });

      // Navigate to LobeAI chat session; in the modal the host outlives the
      // router, so dismiss the detail alongside the navigation
      navigate(AGENT_CHAT_URL(inboxAgentId, mobile));
      close?.();
    }, [agentPrompt, close, inboxAgentId, mobile, navigate, sendMessage]);

    return (
      <Block gap={lite ? 0 : 16} padding={4} variant={lite ? 'outlined' : 'borderless'}>
        <Tabs
          activeKey={mode}
          style={{ marginBottom: 8 }}
          items={[
            {
              icon: <Icon icon={BotIcon} />,
              key: 'agent',
              label: t('skills.hero.guide.agent'),
            },
            {
              icon: <Icon icon={UserRoundIcon} />,
              key: 'human',
              label: t('skills.hero.guide.human'),
            },
          ]}
          styles={{
            list: { display: 'flex', width: '100%' },
            tab: { flex: 1 },
          }}
          onChange={(key) => setMode(key as GuideMode)}
        />

        {mode === 'agent' ? (
          <Flexbox gap={mobile || lite ? 0 : 16}>
            {mobile || lite ? (
              <Text align={'center'} as={'h3'} fontSize={14} style={{ padding: 8 }} weight={500}>
                {t('skills.details.sidebar.agent.title')}
              </Text>
            ) : (
              <Title>{t('skills.details.sidebar.agent.title')}</Title>
            )}
            <Highlighter
              fullFeatured
              wrap
              className={lite ? fallback.lite : undefined}
              defaultExpand={expandCodeByDefault ?? false}
              fileName={'Agent prompt'}
              language={'bash'}
              style={{ fontSize: 12 }}
              variant={lite ? 'borderless' : 'outlined'}
            >
              {agentPrompt}
            </Highlighter>
            <Flexbox padding={8}>
              <Button
                block
                icon={<Avatar avatar={DEFAULT_INBOX_AVATAR} size={18} />}
                size={'large'}
                type={'primary'}
                onClick={handleUseOnLobeAI}
              >
                {t('skills.details.sidebar.agent.useOnLobeAI')}
              </Button>
            </Flexbox>
          </Flexbox>
        ) : (
          <>
            {mobile || lite ? (
              <Select
                value={active}
                variant={'filled'}
                options={options.map((item) => ({
                  ...item,
                  label: (
                    <Flexbox horizontal align={'center'} gap={8}>
                      {item.icon} {item.label}
                    </Flexbox>
                  ),
                }))}
                onSelect={(v) => setActive(v as PlatformType)}
              />
            ) : (
              <Tabs
                activeKey={active}
                items={options.map((opt) => ({
                  icon: opt.icon,
                  key: opt.value,
                  label: opt.label,
                }))}
                styles={{
                  list: { display: 'flex', width: '100%' },
                  tab: { flex: 1 },
                }}
                onChange={(key) => setActive(key as PlatformType)}
              />
            )}
            <Flexbox>
              {!lite && <Title>{t('skills.details.sidebar.platform.title', { platform })}</Title>}
              <Markdown variant={'chat'}>{steps}</Markdown>
            </Flexbox>
            {lite && <Divider dashed />}
            <Highlighter
              fullFeatured
              className={lite ? fallback.lite : undefined}
              defaultExpand={expandCodeByDefault ?? false}
              fileName={t('skills.details.sidebar.installCommand')}
              language={'bash'}
              style={{ fontSize: 12 }}
              variant={lite ? 'borderless' : 'outlined'}
            >
              {command}
            </Highlighter>
            {lite && <Divider dashed />}
            <Highlighter
              fullFeatured
              className={lite ? fallback.lite : undefined}
              defaultExpand={false}
              fileName={t('skills.details.sidebar.directoryLayout')}
              language={'text'}
              style={{ fontSize: 12 }}
              variant={lite ? 'borderless' : 'outlined'}
            >
              {genLayout(identifier, active, {
                lobehub: t('skills.details.sidebar.platform.layout.lobehub'),
                resourcesHint: t('skills.details.sidebar.platform.layout.resourcesHint'),
              })}
            </Highlighter>
            {downloadUrl && (
              <>
                <Divider dashed />
                <Flexbox padding={8}>
                  <Button
                    block
                    href={downloadUrl}
                    size={'large'}
                    target={'_blank'}
                    type={'primary'}
                  >
                    {t('skills.details.sidebar.downloadSkill')}
                  </Button>
                </Flexbox>
              </>
            )}
          </>
        )}
      </Block>
    );
  },
);

export default Platform;