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。

6.5 KB
Raw Blame
import { forwardedRequestDisplayText, isHookSchedulerOrigin } from '@cindy/maker-shared/message-source';
import { queueItemVisibleText } from '@cindy/maker-shared/queue';
import { UI_ACTION_TRIGGER_PREFIX, type QueuedMessage } from '@/lib/makerChatStore';
import { stripChatQuoteMarkerLines } from '@/lib/chatQuotes';
import {
  CONTINUE_AFTER_APP_EXIT_PROMPT,
  CONTINUE_AFTER_ERROR_PROMPT,
} from '../../../shared/interruptedTurn';

export interface PendingQueueRowPresentation {
  isOrca: boolean;
  /**
   * scheduler 撞忙排队的心跳消息(origin.kind === 'scheduler'):机器生成,
   * 保留重排/删除、禁止编辑/steer;行首显示任务名徽标,让用户
   * 一眼看出"这是排队等待的自动化任务"。
   */
  isScheduler: boolean;
  /**
   * 另一个任务经工具(send_to_session / 伙伴委派等)排进来的消息:同属机器生成,
   * 保留重排/删除、禁止编辑/steer(改写后气泡的来源标签就不再属实);行首显示来源任务。
   */
  isSession: boolean;
  /**
   * 插件任务派发的消息(host 盖章的 sourcePlugin):同属机器生成,保留重排/删除、
   * 禁止编辑/steer(改写后「由插件「X」发送」就不再属实);行首显示插件名。
   */
  isPlugin: boolean;
  /** 来源名称;任务来源缺标题快照时为 null,面板显示通用文案。 */
  senderLabel: string | null;
  /** 任务来源属于伙伴时的伙伴 id:面板行首显示伙伴头像,来源名为伙伴名。 */
  senderBotId: string | null;
  displayText: string;
  canEdit: boolean;
  canSteer: boolean;
  /**
   * 带 [UI_ACTION_TRIGGER] 前缀的合成指令行(续跑重试、Mivo 图片按钮等都可能
   * 在 session 忙时排进 pendingQueue,drain 前的 projection 或 drain 被 busy
   * 挡住时会短暂/持续出现在队列面板)。裸 prompt 不能给用户看,面板按此标记
   * 显示 i18n 遮蔽标签;也不许编辑/steer(任何合成指令都不该被改写)。
   */
  isSyntheticTrigger: boolean;
  /**
   * 遮蔽标签语义(review P2:不能把「续跑」文案套到所有合成触发上——Mivo
   * 图片按钮的合成消息语义是图片操作):
   *  - 'continue':error-tail 续跑指令(精确匹配共享常量)→「继续未完成的任务」;
   *  - 'generic' :其它合成触发 →「系统指令」中性标签。
   */
  syntheticKind: 'continue' | 'generic' | null;
}

/**
 * Resolves row-level presentation and permissions for the pending queue.
 * Orca messages are generated by another agent, so they keep reorder/remove/steer
 * affordances but cannot be edited like a local user draft.
 * Scheduler heartbeat prompts and messages sent by another session through a
 * tool follow the same machine-generated rules; scheduler
 * display text uses persistedContent (the raw schedule prompt) so the hidden
 * silent-run protocol suffix in `text` never surfaces in the queue panel.
 */
export function getPendingQueueRowPresentation(entry: QueuedMessage): PendingQueueRowPresentation {
  const origin = entry.origin;
  const isOrca = origin?.kind === 'orca';
  // Hook 渠道消息复用 scheduler 形态,不是自动化:不显示自动化来源行(与历史气泡一致)。
  const isHookOrigin = isHookSchedulerOrigin(origin);
  const isScheduler = origin?.kind === 'scheduler' && !isHookOrigin;
  // 插件优先(与 messageSourceSenderFromMeta 同序):插件在某任务里派发时同时带来源任务 origin。
  const isPlugin = !isOrca && origin?.kind !== 'scheduler' && Boolean(entry.sourcePlugin?.pluginId);
  const isSession = origin?.kind === 'session' && !isPlugin;
  // Hook 渠道条目同属机器投递(渠道原话),不显示来源行但同样不可编辑/插话。
  const isMachineGenerated = isOrca || isScheduler || isSession || isPlugin || isHookOrigin;
  const isSyntheticTrigger = entry.text.startsWith(UI_ACTION_TRIGGER_PREFIX);
  const isContinueTrigger =
    entry.text === CONTINUE_AFTER_APP_EXIT_PROMPT || entry.text === CONTINUE_AFTER_ERROR_PROMPT;
  const isPendingEnqueue = entry.isPendingEnqueue === true;

  return {
    isOrca,
    isScheduler,
    isSession,
    isPlugin,
    senderLabel: isOrca
      ? origin.senderLabel
      : isScheduler
        ? origin.scheduleName
        : isSession
          ? (origin.senderBotId ? origin.senderBotName?.trim() : undefined) ||
            origin.senderSessionTitle?.trim() ||
            null
          : isPlugin
            ? entry.sourcePlugin?.name?.trim() || null
            : null,
    senderBotId: isSession && origin?.kind === 'session' ? (origin.senderBotId ?? null) : null,
    displayText: isOrca
      ? (origin.displayText ?? entry.text)
      : isScheduler || isSession || isPlugin
        ? (isSession ? forwardedRequestDisplayText(queueItemVisibleText(entry)) : queueItemVisibleText(entry))
        : entry.chatMessage.quotesEncoded === true
          ? stripChatQuoteMarkerLines(entry.text)
          : entry.text,
    canEdit: !isPendingEnqueue && !isMachineGenerated && !isSyntheticTrigger,
    // 协同消息可以转为插话:插话不改正文与来源,主进程在接受后结清协同回报。
    canSteer: !isPendingEnqueue && (isOrca || !isMachineGenerated) && !isSyntheticTrigger,
    isSyntheticTrigger,
    syntheticKind: isSyntheticTrigger ? (isContinueTrigger ? 'continue' : 'generic') : null,
  };
}

/**
 * 空输入框里按 Cmd/Ctrl+Enter 时可直接插话的队列条目:仅当队列恰好一条且该行此刻
 * 能插话(与行内 ⬆️ 同一判据)时返回其 clientId。多条时无法判断用户指哪条,返回 null,
 * 仍需把焦点放到具体行上再按快捷键。
 */
export function resolveSoleSteerableQueuedMessage(
  queue: readonly QueuedMessage[],
  state: { editingClientId: string | null; steeringClientIds: readonly string[] },
): string | null {
  if (queue.length !== 1 || state.editingClientId !== null) return null;
  const [entry] = queue;
  if (!entry || !getPendingQueueRowPresentation(entry).canSteer) return null;
  if (state.steeringClientIds.includes(entry.clientId)) return null;
  return entry.clientId;
}

/**
 * Keep an untouched encoded quote body byte-for-byte stable. The queue editor
 * shows the markerless projection, so submitting that projection as a rewrite
 * would otherwise discard the private markers even when the user changed
 * nothing. A real edit intentionally submits the visible markerless text.
 */
export function resolvePendingQueueEditSubmission(
  entry: QueuedMessage,
  visibleText: string,
): string | null {
  return visibleText === getPendingQueueRowPresentation(entry).displayText ? null : visibleText;
}