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.7 KB
Raw Blame
import { DEFAULT_INBOX_AVATAR, DEFAULT_INBOX_TITLE } from '@lobechat/const';
import type { ScreenCaptureAgentOption } from '@lobechat/electron-client-ipc';
import { agentDisplayName } from '@lobechat/types';

import { isGeneratedAgentAvatar } from '@/components/AgentAvatar/resolve';

const LOBE_AI_TITLE = DEFAULT_INBOX_TITLE;
const UNTITLED_AGENT_TITLE = 'Untitled Agent';

interface OverlayAgentSource {
  avatar?: unknown;
  backgroundColor?: string | null;
  heterogeneousType?: string | null;
  id: string;
  name?: string | null;
  title?: string | null;
}

interface OverlayInboxMeta {
  avatar?: string | null;
  backgroundColor?: string | null;
  title?: string | null;
}

interface ResolveOverlayAgentOptionsParams {
  agents: readonly OverlayAgentSource[];
  inboxAgentId?: string;
  inboxMeta?: OverlayInboxMeta;
}

interface ResolveOverlayDefaultAgentIdParams {
  activeAgentId?: string;
  agentOptions: readonly ScreenCaptureAgentOption[];
  inboxAgentId?: string;
}

const toOverlayAgentOption = ({
  avatar,
  backgroundColor,
  heterogeneousType,
  id,
  name,
  title,
}: OverlayAgentSource): ScreenCaptureAgentOption => ({
  avatar: typeof avatar === 'string' && !isGeneratedAgentAvatar(avatar) ? avatar : undefined,
  backgroundColor: backgroundColor ?? undefined,
  heterogeneousType,
  id,
  title: agentDisplayName({ name, title }, UNTITLED_AGENT_TITLE),
});

const createInboxOverlayAgentOption = (
  inboxAgentId: string,
  inboxMeta?: OverlayInboxMeta,
): ScreenCaptureAgentOption => ({
  // The overlay draws emoji and images only; a picked character falls back to the inbox image.
  avatar:
    inboxMeta?.avatar && !isGeneratedAgentAvatar(inboxMeta.avatar)
      ? inboxMeta.avatar
      : DEFAULT_INBOX_AVATAR,
  backgroundColor: inboxMeta?.backgroundColor ?? undefined,
  id: inboxAgentId,
  title: agentDisplayName(inboxMeta, LOBE_AI_TITLE),
});

export const resolveOverlayAgentOptions = ({
  agents,
  inboxAgentId,
  inboxMeta,
}: ResolveOverlayAgentOptionsParams): ScreenCaptureAgentOption[] => {
  const agentOptions = agents.map((item) => toOverlayAgentOption(item));

  if (!inboxAgentId) return agentOptions;
  if (agentOptions.some((item) => item.id === inboxAgentId)) return agentOptions;

  return [createInboxOverlayAgentOption(inboxAgentId, inboxMeta), ...agentOptions];
};

export const resolveOverlayDefaultAgentId = ({
  activeAgentId,
  agentOptions,
  inboxAgentId,
}: ResolveOverlayDefaultAgentIdParams): string | undefined => {
  if (activeAgentId && agentOptions.some((item) => item.id === activeAgentId)) {
    return activeAgentId;
  }

  if (inboxAgentId && agentOptions.some((item) => item.id === inboxAgentId)) {
    return inboxAgentId;
  }

  return agentOptions[0]?.id;
};