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.

2.8 KB
Raw Blame
import type { AgentTemplate, MarketplaceCategory } from '../types';
import { MARKETPLACE_CATEGORY_VALUES } from '../types';

const MARKETPLACE_CATEGORY_SET = new Set<string>(MARKETPLACE_CATEGORY_VALUES);

export interface RawAgentTemplate {
  author?: { name?: string } | string;
  avatar?: string;
  description?: string;
  forkCount?: number;
  identifier?: string;
  name?: string;
  pluginCount?: number;
}

/** API response shape: top-level keys are MarketplaceCategory slugs. */
export type OnboardingFullResponse = Record<string, RawAgentTemplate[]>;

export const normalizeAgentTemplate = (
  item: RawAgentTemplate,
  category: string,
): AgentTemplate | undefined => {
  if (!item.identifier || !item.name) return undefined;
  if (!MARKETPLACE_CATEGORY_SET.has(category)) return undefined;
  const author = typeof item.author === 'string' ? item.author : item.author?.name;
  return {
    ...(author ? { author } : {}),
    avatar: item.avatar,
    category: category as MarketplaceCategory,
    description: item.description,
    ...(typeof item.forkCount === 'number' ? { forkCount: item.forkCount } : {}),
    id: item.identifier,
    ...(typeof item.pluginCount === 'number' ? { pluginCount: item.pluginCount } : {}),
    title: item.name,
  };
};

export interface FetchAgentTemplatesOptions {
  signal?: AbortSignal;
}

export type AgentTemplateFetcher = (
  options?: FetchAgentTemplatesOptions,
) => Promise<AgentTemplate[]>;

export const getAgentTemplatesSWRKey = (locale?: string) =>
  `builtin-tool-web-onboarding/agent-marketplace/onboarding-templates/${locale ?? 'default'}`;

const defaultFetcher: AgentTemplateFetcher = async () => {
  throw new Error(
    '[AgentMarketplace] Agent templates fetcher is not configured. ' +
      'Call setAgentTemplatesFetcher in your app initialization.',
  );
};

let currentFetcher: AgentTemplateFetcher = defaultFetcher;

export const setAgentTemplatesFetcher = (fetcher: AgentTemplateFetcher): void => {
  currentFetcher = fetcher;
};

export const fetchAgentTemplates: AgentTemplateFetcher = (options) => currentFetcher(options);

export const getTemplatesByCategories = (
  templates: AgentTemplate[],
  categories: string[],
): AgentTemplate[] => {
  if (categories.length === 0) return templates;
  const set = new Set(categories);
  return templates.filter((t) => set.has(t.category));
};

export const getTemplatesByCategoryPriority = (
  templates: AgentTemplate[],
  categories: string[],
): AgentTemplate[] => {
  if (categories.length === 0) return templates;

  const priority = new Map(categories.map((category, index) => [category, index]));

  return [...templates].sort((a, b) => {
    const aPriority = priority.get(a.category) ?? Number.MAX_SAFE_INTEGER;
    const bPriority = priority.get(b.category) ?? Number.MAX_SAFE_INTEGER;

    return aPriority - bPriority;
  });
};