Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

3.7 KB
Raw Blame
import type { NormalizedRemoteMessage } from '@/session/messageNormalize';
import type {
  MobileMessageRenderItem,
  MobileWorkChildItem,
} from '@/session/messageRenderModel';
import { projectConversationShareMessage } from '@/session/conversationShareProjection';
import type { ConversationShareMessage } from '@/session/conversationShareWebViewHtml';
import { isShareableMessage } from '@/session/shareSelectionStore';
import {
  AUTOMATION_USER_MESSAGE_COLLAPSED_LINES,
  AUTOMATION_USER_MESSAGE_VISUAL_LINE_THRESHOLD,
  LONG_USER_MESSAGE_COLLAPSED_LINES,
  LONG_USER_MESSAGE_VISUAL_LINE_THRESHOLD,
  MIN_HALF_WIDTH_UNITS_PER_VISUAL_LINE,
  mayExceedVisualLineThreshold,
} from '@/session/userMessageCollapse';

type ConversationShareRenderItem =
  MobileMessageRenderItem | MobileWorkChildItem;

/** 收集会影响分享候选集的折叠卡 key,供会话页订阅展开态变化。 */
export function collectConversationShareBlockIds(
  items: readonly MobileMessageRenderItem[],
): string[] {
  const blockIds: string[] = [];
  const visit = (item: ConversationShareRenderItem): void => {
    if (item.type === 'work_group') {
      blockIds.push(item.key);
      item.children.forEach(visit);
      return;
    }
    if (item.type === 'subagent_group') {
      blockIds.push(item.key);
      item.childItems.forEach(visit);
    }
  };
  items.forEach(visit);
  return blockIds;
}

/**
 * 展平当前已展开的工作组 / 子 Agent,投影用户实际看得到的消息用于图片导出。
 * 分享图不显示任何消息来源(自动化 / 任务 / 设备 / 插件 / 共享任务作者);
 * 自动化消息只沿用会话里的收起阈值,让分享内容与屏幕上看到的一致。
 */
export function collectConversationShareMessages(
  items: readonly MobileMessageRenderItem[],
  isBlockExpanded: (blockId: string) => boolean,
  /** 与聊天气泡同一判据:自己发的消息才用「我的消息气泡颜色」。 */
  isOwnUserMessage?: (message: NormalizedRemoteMessage) => boolean,
): ConversationShareMessage[] {
  const messages: ConversationShareMessage[] = [];
  const visit = (item: ConversationShareRenderItem): void => {
    if (item.type === 'message') {
      if (!isShareableMessage(item.message)) return;
      const clientId =
        item.message.source.clientId ||
        item.message.source.id ||
        item.message.key;
      const collapseThreshold = item.message.automationOrigin
        ? AUTOMATION_USER_MESSAGE_VISUAL_LINE_THRESHOLD
        : LONG_USER_MESSAGE_VISUAL_LINE_THRESHOLD;
      const maxVisibleLines = item.message.kind === 'user'
        && mayExceedVisualLineThreshold(item.message.body, collapseThreshold)
        ? item.message.automationOrigin
          ? AUTOMATION_USER_MESSAGE_COLLAPSED_LINES
          : LONG_USER_MESSAGE_COLLAPSED_LINES
        : undefined;
      const projected = projectConversationShareMessage(clientId, item.message, {
        maxVisibleLines,
        visualLineCapacity: maxVisibleLines ? MIN_HALF_WIDTH_UNITS_PER_VISUAL_LINE : undefined,
      });
      // IM 来源卡在聊天中不使用个人气泡色,即便其正文属于当前用户。
      if (projected?.kind === 'user' && !item.message.hookSource && isOwnUserMessage?.(item.message)) {
        projected.ownUserBubble = true;
      }
      if (projected) {
        projected.richStateKey = item.message.source.clientId ?? item.message.key;
        messages.push(projected);
      }
      return;
    }
    if (item.type === 'work_group') {
      if (isBlockExpanded(item.key)) item.children.forEach(visit);
      return;
    }
    if (item.type === 'subagent_group' && isBlockExpanded(item.key)) {
      item.childItems.forEach(visit);
    }
  };
  items.forEach(visit);
  return messages;
}