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.

8.1 KB
Raw Blame
import {
  type AgentCreateResponse,
  type AgentItemDetail,
  type AgentListResponse,
} from '@lobehub/market-sdk';

import { type MarketUserProfile } from '@/layout/AuthProvider/MarketAuth/types';
import { lambdaClient } from '@/libs/trpc/client';
import { discoverService } from '@/services/discover';
import {
  type AgentForkBatchInput,
  type AgentForkSourceResponse,
  type AgentForksResponse,
  type AgentGroupForkRequest,
  type AgentGroupForkResponse,
  type AgentGroupForkSourceResponse,
  type AgentGroupForksResponse,
  type SkillSorts,
} from '@/types/discover';
import { isTrpcErrorCode } from '@/utils/trpcError';

/** One text file of a Market skill package, path relative to the package root. */
export interface MarketSkillPackageFile {
  content: string;
  path: string;
}

interface GetOwnAgentsParams {
  page?: number;
  pageSize?: number;
}

export class MarketApiService {
  // Create new agent
  async createAgent(agentData: {
    actAs?: number;
    homepage?: string;
    identifier: string;
    isFeatured?: boolean;
    name: string;
    status?: 'published' | 'unpublished' | 'archived' | 'deprecated';
    tokenUsage?: number;
    visibility?: 'public' | 'private' | 'internal';
  }): Promise<AgentCreateResponse> {
    return lambdaClient.market.agent.createAgent.mutate(agentData);
  }

  // Get agent detail by identifier
  async getAgentDetail(
    identifier: string,
  ): Promise<AgentItemDetail & { forkedFromAgentId?: string }> {
    return lambdaClient.market.agent.getAgentDetail.query({
      identifier,
    }) as Promise<AgentItemDetail>;
  }

  // Check if agent exists (returns true if exists, false if not)
  async checkAgentExists(identifier: string): Promise<boolean> {
    try {
      await this.getAgentDetail(identifier);
      return true;
    } catch {
      return false;
    }
  }

  // Create agent version
  async createAgentVersion(versionData: {
    actAs?: number;
    a2aProtocolVersion?: string;
    avatar?: string;
    category?: string;
    changelog?: string;
    config?: Record<string, any>;
    defaultInputModes?: string[];
    defaultOutputModes?: string[];
    description?: string;
    documentationUrl?: string;
    extensions?: Record<string, any>[];
    hasPushNotifications?: boolean;
    hasStateTransitionHistory?: boolean;
    hasStreaming?: boolean;
    identifier: string;
    interfaces?: Record<string, any>[];
    name?: string;
    preferredTransport?: string;
    providerId?: number;
    securityRequirements?: Record<string, any>[];
    securitySchemes?: Record<string, any>;
    setAsCurrent?: boolean;
    summary?: string;
    supportsAuthenticatedExtendedCard?: boolean;
    tokenUsage?: number;
    url?: string;
  }) {
    return lambdaClient.market.agent.createAgentVersion.mutate(versionData);
  }

  // Deprecate agent (permanently hide, cannot be republished)
  async deprecateAgent(identifier: string): Promise<void> {
    await lambdaClient.market.agent.deprecateAgent.mutate({ identifier });
  }

  // Get own agents (requires authentication)
  async getOwnAgents(params?: GetOwnAgentsParams): Promise<AgentListResponse> {
    return lambdaClient.market.agent.getOwnAgents.query(params) as Promise<AgentListResponse>;
  }

  // ==================== Fork Agent API ====================

  /**
   * Fork one or more agents in a single batch.
   *
   * Best-effort: each item is reported individually with `success` true or
   * false. A failed item does not abort the rest of the batch.
   */
  async forkAgent(items: AgentForkBatchInput[]) {
    return lambdaClient.market.agent.forkAgent.mutate({
      items,
    });
  }

  /**
   * Get all forks of an agent
   * @param identifier - Agent identifier
   */
  async getAgentForks(identifier: string): Promise<AgentForksResponse> {
    return lambdaClient.market.agent.getAgentForks.query({ identifier });
  }

  /**
   * Get the fork source of an agent
   * @param identifier - Agent identifier
   * @returns Fork source information (null if not a fork)
   */
  async getAgentForkSource(identifier: string): Promise<AgentForkSourceResponse> {
    return lambdaClient.market.agent.getAgentForkSource.query({ identifier });
  }

  // ==================== Agent Group Status Management ====================

  // Get agent group detail by identifier
  async getAgentGroupDetail(identifier: string): Promise<any> {
    return lambdaClient.market.agentGroup.getAgentGroupDetail.query({
      identifier,
    }) as Promise<any>;
  }

  async deprecateAgentGroup(identifier: string): Promise<void> {
    await lambdaClient.market.agentGroup.deprecateAgentGroup.mutate({ identifier });
  }

  // ==================== Fork Agent Group API ====================

  /**
   * Fork an agent group
   * @param sourceIdentifier - Source agent group identifier
   * @param forkData - Fork request parameters
   */
  async forkAgentGroup(
    sourceIdentifier: string,
    forkData: AgentGroupForkRequest,
  ): Promise<AgentGroupForkResponse> {
    return lambdaClient.market.agentGroup.forkAgentGroup.mutate({
      sourceIdentifier,
      ...forkData,
    });
  }

  /**
   * Get all forks of an agent group
   * @param identifier - Agent group identifier
   */
  async getAgentGroupForks(identifier: string): Promise<AgentGroupForksResponse> {
    return lambdaClient.market.agentGroup.getAgentGroupForks.query({ identifier });
  }

  /**
   * Get the fork source of an agent group
   * @param identifier - Agent group identifier
   * @returns Fork source information (null if not a fork)
   */
  async getAgentGroupForkSource(identifier: string): Promise<AgentGroupForkSourceResponse> {
    return lambdaClient.market.agentGroup.getAgentGroupForkSource.query({ identifier });
  }

  // ==================== Skills API ====================

  /**
   * Search for skills in the LobeHub Market
   */
  async searchSkill(params: {
    category?: string;
    locale?: string;
    order?: 'asc' | 'desc';
    page?: number;
    pageSize?: number;
    q?: string;
    sort?: SkillSorts;
  }) {
    await discoverService.safeInjectMPToken();

    return lambdaClient.market.skill.getSkillList.query(params);
  }

  /**
   * Get skill download URL from market
   */
  getSkillDownloadUrl(identifier: string): string {
    const marketBaseUrl = process.env.NEXT_PUBLIC_MARKET_BASE_URL || 'https://market.lobehub.com';
    return `${marketBaseUrl}/api/v1/skills/${identifier}/download`;
  }

  /**
   * Download a skill package and return its text files, without the top-level
   * directory prefix, `SKILL.md` (the detail endpoint serves it) and macOS
   * metadata entries.
   */
  async getSkillPackageFiles(identifier: string): Promise<MarketSkillPackageFile[]> {
    const res = await fetch(this.getSkillDownloadUrl(encodeURIComponent(identifier)));
    if (!res.ok) throw new Error(`Skill package download failed: ${res.status}`);
    const buf = await res.arrayBuffer();
    const { unzip } = await import('fflate');

    return new Promise((resolve, reject) => {
      unzip(new Uint8Array(buf), (err, entries) => {
        if (err) return reject(err);

        const decoder = new TextDecoder();
        const files: MarketSkillPackageFile[] = [];
        for (const [rawPath, data] of Object.entries(entries)) {
          if (rawPath.endsWith('/') || rawPath.includes('__MACOSX')) continue;

          // Strip the top-level directory prefix (e.g. "skill-name/")
          const slashIdx = rawPath.indexOf('/');
          const path = slashIdx >= 0 ? rawPath.slice(slashIdx + 1) : rawPath;
          if (!path || path === 'SKILL.md') continue;

          files.push({ content: decoder.decode(data), path });
        }

        files.sort((a, b) => a.path.localeCompare(b.path));
        resolve(files);
      });
    });
  }

  /**
   * The Market profile of a username, or `null` when that account has not
   * created a Market profile yet (the server answers NOT_FOUND).
   */
  async getUserProfile(username: string): Promise<MarketUserProfile | null> {
    try {
      return (await lambdaClient.market.user.getUserByUsername.query({
        username,
      })) as MarketUserProfile;
    } catch (error) {
      if (isTrpcErrorCode(error, 'NOT_FOUND')) return null;
      throw error;
    }
  }
}

export const marketApiService = new MarketApiService();