Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
import { readChatReplyQuote, type ChatReplyQuote } from '@cindy/maker-shared/chat-reply-quote';
import { forwardedRequestDisplayText } from '@cindy/maker-shared/message-source';
import { formatDuration } from '@cindy/maker-shared/message-render';
import { parseMcpAppHistory } from '@cindy/maker-shared/mcp-apps';
import { collectPluginInvocations, type PluginInvocation } from './pluginInvocations';
import { extractPayloadToolResultFiles, extractPayloadToolCardIds, type PayloadToolFile } from '@cindy/maker-shared/payload-summary';
import { placeBotTaskCardsAfterIntroduction, readBotCollaborationMeta, type BotCollaborationMeta } from '@cindy/maker-shared/botCollaboration';
import { readBotDirectMessageMeta, type BotDirectMessageMeta } from '@cindy/maker-shared/botDirectMessage';
import type { RemoteMessage, RemoteMessageRole } from '@/session/types';
import type { MobileSystemCardType } from '@/session/systemCard';
import { contentToPreview } from '@/utils/contentPreview';
import { i18n } from '@/i18n';
import { describeAgentAuthError } from '@/device-link/remoteStatus';
import {
buildMessageToolResultPairing,
messageNormalizeKey,
parseMessageToolUse,
sortMessagesByCreatedAt,
type MessageNormalizeToolUse,
type MessageToolResultPairing,
} from '@cindy/maker-shared/message-normalize';
import { collectMobileMarkdownImages } from '@/session/messageMarkdown';
import {
normalizeAgentTaskTerminalStatus,
type AgentTaskTerminalStatus,
} from '@cindy/maker-shared/agent-task';
import { isSyntheticTriggerText, parseInputHandoffEndRecord } from '@cindy/maker-shared/synthetic-trigger';
import {
isHookSchedulerOrigin,
readMessageSourceDevice,
readMessageSourceGroup,
readMessageSourcePlugin,
sanitizeSourceName,
type MessageSourceDevice,
type MessageSourceGroup,
type MessageSourcePlugin,
} from '@cindy/maker-shared/message-source';
import { sharedTaskAuthorAccountId, sharedTaskAuthorMemberId, sharedTaskAuthorName } from '@cindy/maker-shared';
import { stripGoalVerdictBlock } from '@cindy/maker-shared/goal-verdict';
import {
formatToolResultCompactionBytes,
parseToolResultCompactionMarker,
} from '@cindy/maker-shared/tool-result-compaction';
import {
buildPayloadToolDiff,
extractPayloadToolResultMedia,
formatPayloadToolUseSummary,
} from '@/session/messagePayload';
import {
buildOrcaDispatchCard,
parseOrcaPersistedMessage,
type OrcaCollabCard,
} from '@/session/orcaCollab';
import { isMobileImPlatform, type MobileImPlatform } from '@/session/messageSourceLabels';
import {
parseMobilePersistedSessionReferenceMetadata,
type MobilePersistedSessionReferenceMetadata,
} from '@/session/sessionReferences';
import {
readSentPastedTextRanges,
readSentSlashCommandRanges,
} from '@/session/sentMessageAtoms';
import {
readAgentInputReferences,
type AgentInputReference,
} from '@cindy/maker-shared/agent-input-projection';
import {
normalizeRemoteMoney,
type RemoteMoney,
} from '@/session/remoteMoney';
import {
localizeAgentError,
localizeUnclassifiedAgentError,
unclassifiedAgentErrorI18nKey,
parseMobileToolLoopErrorDetails,
} from '@/session/agentErrorI18n';
import type { MobileToolInputProjection } from '@/session/messageToolPayloadProjection';
export type NormalizedRemoteMessageKind =
| 'user'
| 'assistant'
| 'tool'
| 'thinking'
| 'ask_user'
| 'plan_review'
| 'system';
export interface NormalizedRemoteMessage {
key: string;
source: RemoteMessage;
kind: NormalizedRemoteMessageKind;
role: RemoteMessageRole;
label: string;
body: string;
/** Original forwarded envelope for fork / rewind; display-only decoding must not change drafts. */
draftBody?: string;
rawError?: string;
errorSummaryKey?: string;
/** user 消息正文包含产品引用编码;驱动跨端 marker/legacy 解析。 */
quotesEncoded?: boolean;
/** user 长文本粘贴原子的精确 wire ranges;正文仍保留完整 Agent payload。 */
pastedTextRanges?: Array<{ start: number; end: number; display: string }>;
/** user Composer 确认过的 Slash ranges;空数组用于关闭历史启发式。 */
slashCommandRanges?: Array<{ start: number; end: number }>;
/** user Composer 的结构化语义引用;用于 fork / rewind 恢复同款 chip。 */
agentReferences?: AgentInputReference[];
/** user 专用:伙伴私聊的引用回复(信封 replyQuote),气泡正文上方显示引用块。 */
replyQuote?: ChatReplyQuote;
secondaryBody?: string;
authorization?: Record<string, unknown>;
systemCardData?: Record<string, unknown>;
systemCardType?: MobileSystemCardType;
attachments?: NormalizedAttachment[];
/** user 专用:目标桌面落库的引用范围摘要,不含被引用消息正文。 */
sessionReferences?: MobilePersistedSessionReferenceMetadata[];
media?: NormalizedToolMedia[];
files?: PayloadToolFile[];
cardIds?: string[];
pluginInvocations?: PluginInvocation[];
diff?: NormalizedToolDiff;
align: 'user' | 'agent';
createdAt: string;
/**
* tool 消息专用:配对 tool_result 的落库时刻(ISO),即这次调用的结束时刻。渲染层用它做
* 历史空洞判定的锚点(见共享 `MessageRenderNormalizedMessage.settledAt`)。
*/
settledAt?: string;
isStreaming?: boolean;
/** Host 在 SDK done 边界写入;后台自动续跑时同一 turn 可有多次 seal,最后一次是最终答复。 */
turnCompleted?: boolean;
failedSamplingAttempt?: boolean;
turnMoney?: RemoteMoney;
/** 旧 Desktop 消息兼容字段。 */
turnCostUsd?: number;
/**
* 本轮 token 总量(agentMeta.turnUsageDetails.totalTokens)。桌面算不出模型报价时
* 只落这一份用量事实,操作行据此退回显示 token 而不是空着一格。
*/
turnTotalTokens?: number;
/** assistant 专用:本轮模型降级标记(agentMeta.modelMismatch,桌面 main 在 turn 结束检测命中时落库)。 */
modelMismatch?: { selected: string; actual: string };
/** Orca 协同卡片(Lead 派活 / Lead↔worker 互发消息);存在时由 MessageRenderer 渲染成专属卡片而非普通气泡。 */
orcaCard?: OrcaCollabCard;
companion?: { kind: 'task'; meta: BotCollaborationMeta } | { kind: 'direct'; meta: BotDirectMessageMeta };
/** tool 消息专用:tool_result 是否已到达(含被隐藏的 orca 空结果),驱动工具行 running/done 状态。 */
toolSettled?: boolean;
/** Large settled tool input is fetched only when the user asks to view it. */
toolInputProjection?: MobileToolInputProjection;
/** Durable Agent/Task terminal lifecycle restored from tool_use metadata. */
agentTaskStatus?: AgentTaskTerminalStatus;
/** assistant 专用:是否本轮收尾正文(操作行只挂在收尾正文上,对齐桌面 #456);由 messageRenderModel 标注。 */
isTurnFinalAssistant?: boolean;
/** user 专用:scheduler 注入的消息来源(agentMeta.origin);驱动更紧的收起阈值与来源标签。 */
automationOrigin?: NormalizedAutomationOrigin;
/**
* user 专用:另一个任务经工具(send_to_session / 伙伴委派 / Orca 协同等)发来的消息来源
* (agentMeta.origin kind=session,或带 senderSessionId 的 orca);渲染可点击的来源标签。
* 不进分享投影:来源任务标题属于用户本机上下文,不随分享图外发。
*/
sessionOrigin?: NormalizedSessionOrigin;
/**
* IM 来源(共享 relay 的 agentMeta.hookSource,或本机 IM 渠道的 agentMeta.imSource);
* 渲染「Cindy · 来自 X」卡片。不进分享投影。
*/
hookSource?: NormalizedHookSource;
/**
* user 专用:手机或另一台电脑远程操作主机时由主机盖章的控制端设备(agentMeta.sourceDevice)。
* 是否显示由渲染层按查看者设备判定(shouldShowSourceDevice),不进分享投影。
*/
sourceDevice?: MessageSourceDevice;
/** user 专用:插件任务派发的消息(agentMeta.sourcePlugin);与本轮插件调用头(pluginInvocations)无关。 */
sourcePlugin?: MessageSourcePlugin;
/** Group source of an explicitly sent private assistant message. */
sourceGroup?: MessageSourceGroup;
/** Host-stamped delivery remains visible after source identity is redacted. */
explicitDelivery?: boolean;
/** user 专用:共享任务成员发送的消息作者名(agentMeta.sharedTaskAuthor);气泡上方标签。 */
sharedAuthorName?: string;
/** 共享任务成员 id(作者标签长按显示,与模型 `[消息来源]` 的 member_id 同源)。 */
sharedAuthorMemberId?: string;
/** 共享任务作者账号 id;只用于展示归属(如「我的消息气泡颜色」只染自己的消息),不作权限依据。 */
sharedAuthorAccountId?: string;
/**
* user 专用:合成 UI 指令行(桌面「失败后继续 / 中断续跑」等隐藏 prompt,
* `[UI_ACTION_TRIGGER]` 前缀,对齐桌面 makerChatStore 同名标记)。保留在
* normalized 列表里参与 turn 边界判定(markTurnFinalAssistants /
* scopeUnsettledToolsToActiveTail 需要它作为新一轮的 user 边界),但
* messageRenderModel 会把它从 render items 里剔除,用户不可见。
*/
isSyntheticTrigger?: boolean;
}
/** 另一个任务经工具发来的消息来源(对齐桌面 MessageSessionOrigin)。 */
export interface NormalizedSessionOrigin {
/** 共享任务访客收到的来源已由主机脱敏、不带 id:只显示通用文案,不可点按。 */
senderSessionId?: string;
senderSessionTitle?: string;
/** 来源任务属于伙伴时的伙伴名快照;有则标签显示伙伴名。 */
senderBotName?: string;
}
/**
* scheduler 注入消息的来源标记(对齐桌面 MessageAutomationOrigin)。共享任务访客收到的来源
* 已由主机脱敏(只有 kind:'scheduler',没有 scheduleId / scheduleName):仍标「由自动化发送」,
* 并沿用自动化消息的收起阈值。
*/
export interface NormalizedAutomationOrigin {
scheduleId?: string;
scheduleName?: string;
}
export interface NormalizedHookSource {
im: MobileImPlatform;
channelName?: string;
userText: string;
/**
* 落库正文就是用户原文(本机 IM 的 `contentFormat:'user-text'`):保留普通用户消息的复制 /
* 分叉 / 回退 / 删除 / 分享等操作。为 false 时落库的是拼好的 Agent prompt(旧 Hook),
* 渲染层降级为系统卡、不挂用户操作,也不进分享。
*/
userTextContent: boolean;
threadContext?: Array<{ author: string; text: string; isBot?: boolean }>;
}
export interface NormalizedAttachment {
kind: 'image' | 'file';
name: string;
uri?: string;
path?: string;
mimeType?: string;
sha256?: string;
previewable: boolean;
}
export interface NormalizedToolMedia {
kind: 'image' | 'video' | 'audio';
url: string;
mimeType?: string;
title?: string;
previewable: boolean;
actions?: NormalizedToolMediaActions;
}
export interface NormalizedToolMediaActionButton {
customId: string;
label?: string;
emoji?: string;
}
export interface NormalizedToolMediaActions {
provider: 'mivo';
jobId: string;
buttons: NormalizedToolMediaActionButton[];
}
export interface NormalizedToolDiff {
filePath: string;
segments: Array<{ key: string; oldString: string; newString: string; label?: string }>;
insertions: number;
deletions: number;
}
interface ToolUsePayload extends MessageNormalizeToolUse {
summary: string;
diff?: NormalizedToolDiff;
}
const toolResultPreviewByContent = new WeakMap<object, { language: string; preview: string }>();
const toolUsePayloadByMessage = new WeakMap<RemoteMessage, ToolUsePayload>();
export function normalizeRemoteMessages(
messages: readonly RemoteMessage[],
options: { preserveSourceOrder?: boolean; sessionSource?: string | null } = {},
): NormalizedRemoteMessage[] {
// History views already place live tails after their persisted prefix. A live
// row's provisional timestamp must not undo that order during normalization.
const sorted = placeBotTaskCardsAfterIntroduction(
options.preserveSourceOrder ? messages : sortMessagesByCreatedAt(messages),
(message) => {
if (message.role === 'user') {
return message.agentMeta?.delivery !== 'steer' || message.agentMeta?.synthetic
? 'boundary' : 'other';
}
if (message.role !== 'assistant' || message.agentMeta?.parentUuid || message.systemCardType)
return 'other';
const task = readBotCollaborationMeta(message.agentMeta?.botCollaboration);
if (task?.role === 'delegation-request') return 'task';
if (task || message.agentMeta?.botDirectMessage || message.agentMeta?.botAuthorization)
return 'other';
return typeof message.content === 'string' && message.content.trim() ? 'prose' : 'other';
},
);
const toolResultPairing = buildMessageToolResultPairing(sorted, {
contentToPreview: toolResultContentToPreview,
});
const result: NormalizedRemoteMessage[] = [];
for (const message of sorted) {
if (message.role === 'tool_result') continue;
if (message.role === 'assistant') {
const app = message.agentMeta?.mcpApp === true ? parseMcpAppHistory(message.content) : undefined;
if (app) {
result.push({ key: messageNormalizeKey(message), source: message, kind: 'system', role: message.role,
label: 'system:mcp-app', body: '', systemCardType: 'mcp-app',
systemCardData: { title: app.title, fallbackText: app.text }, align: 'agent', createdAt: message.createdAt });
continue;
}
const authorization = readRecord(message.agentMeta?.botAuthorization);
const snapshot = readRecord(authorization?.snapshot);
if (authorization?.v === 1 && authorization.sessionId === message.sessionId && snapshot?.kind === 'plugin_setup') {
result.push({ key: messageNormalizeKey(message), source: message, kind: 'system', role: message.role,
label: 'authorization', body: typeof message.content === 'string' ? message.content : '', align: 'agent',
createdAt: message.createdAt, authorization: snapshot });
continue;
}
const task = readBotCollaborationMeta(message.agentMeta?.botCollaboration);
const direct = readBotDirectMessageMeta(message.agentMeta?.botDirectMessage);
const isTaskTrace = task?.role === 'delegation-request' || task?.role === 'delegation-result' || task?.role === 'interjection';
if (isTaskTrace || direct) {
result.push({
key: messageNormalizeKey(message), source: message, kind: 'system', role: message.role,
// Task traces are status-only, including legacy rows containing execution instructions.
label: 'companion', body: isTaskTrace ? '' : typeof message.content === 'string' ? message.content : '',
align: 'agent', createdAt: message.createdAt,
companion: isTaskTrace
? { kind: 'task', meta: task } : { kind: 'direct', meta: direct! },
});
continue;
}
}
if (message.role === 'tool_use') {
const tool = parseToolUse(message);
const toolInputProjection = message.mobileToolInputProjection;
const agentTaskStatus = normalizeAgentTaskTerminalStatus(
message.agentMeta?.agentTaskStatus,
);
if (tool.toolName === 'AskUserQuestion' || tool.toolName === 'ExitPlanMode') continue;
// Lead 派活(create_worker / send_to_worker)→ 渲染成 dispatch 卡片(kind:'system' 使其成为
// 独立卡片而非折叠进 tool_group),其余 tool 照常走下面的 tool 渲染。
const dispatchCard = buildOrcaDispatchCard(tool.toolName, tool.input);
if (dispatchCard) {
result.push({
key: messageNormalizeKey(message),
source: message,
kind: 'system',
role: message.role,
label: dispatchCard.title,
body: dispatchCard.body,
orcaCard: dispatchCard,
align: 'agent',
createdAt: message.createdAt,
});
continue;
}
const secondaryBody = toolResultContentFor(message, tool, toolResultPairing);
result.push({
key: messageNormalizeKey(message),
source: message,
kind: 'tool',
role: message.role,
label: tool.toolName || 'tool_use',
body: tool.summary,
secondaryBody,
media: extractToolResultMedia(secondaryBody ?? ''),
files: extractPayloadToolResultFiles(secondaryBody ?? ''),
cardIds: extractPayloadToolCardIds(secondaryBody ?? ''),
diff: tool.diff,
align: 'agent',
createdAt: message.createdAt,
// 结束时刻(配对 tool_result 落库时间)驱动渲染层的历史空洞判定,详见共享类型上的说明。
settledAt: toolResultPairing.resultCreatedAtFor(message, tool),
toolSettled: toolResultPairing.hasResultFor(message, tool),
...(toolInputProjection ? { toolInputProjection } : {}),
...(agentTaskStatus ? { agentTaskStatus } : {}),
});
continue;
}
if (message.role === 'ask_user') {
const ask = normalizeAskUser(message);
if (ask) result.push(ask);
continue;
}
if (message.role === 'plan_review') {
result.push(normalizePlanReview(message));
continue;
}
if (message.role === 'thinking') {
const thinking = normalizeThinking(message);
if (thinking) result.push(thinking);
continue;
}
// turn 失败终态的持久化行(desktop main 落库):content = { message, reason? },
// 提取 message 文案按 system 样式展示 —— 不加分支会 fall through 到通用兜底,
// body 变成整段生 JSON。稳定的 tool-loop reason/toolLoop 走本地化,agent 未鉴权错误
// 换成本地化引导(describeAgentAuthError),其余未知错误使用本地化摘要,原文留给折叠详情。
if (message.role === 'error') {
const c = parseMaybeJsonObject(message.content);
const rawText = typeof c?.message === 'string' ? c.message : contentToPreview(message.content);
const toolLoop = parseMobileToolLoopErrorDetails(c?.toolLoop);
const guidance = describeAgentAuthError(rawText) ?? localizeAgentError(c?.reason, toolLoop);
const errText = guidance ?? localizeUnclassifiedAgentError(rawText, options.sessionSource);
result.push({
key: messageNormalizeKey(message),
source: message,
kind: 'system',
role: message.role,
label: 'error',
body: errText,
rawError: rawText,
...(!guidance ? { errorSummaryKey: unclassifiedAgentErrorI18nKey(rawText, options.sessionSource) } : {}),
align: 'agent',
createdAt: message.createdAt,
});
continue;
}
if (message.role === 'assistant' && readRecord(message.agentMeta?.modelChange)) {
result.push({
key: messageNormalizeKey(message), source: message, kind: 'system',
role: message.role, label: 'system:agent-switch', body: '',
systemCardType: 'agent-switch', systemCardData: readRecord(message.agentMeta?.modelChange)!,
align: 'agent', createdAt: message.createdAt,
});
continue;
}
// Desktop persists context rebuilds as empty assistant rows with metadata.
if (message.role === 'assistant') {
const rebuild = readRecord(message.agentMeta?.contextRebuild);
if (rebuild) {
result.push({
key: messageNormalizeKey(message),
source: message,
kind: 'system',
role: message.role,
label: 'system:context-rebuild',
body: '',
systemCardType: 'context-rebuild',
systemCardData: {
reason: typeof rebuild.reason === 'string' ? rebuild.reason : 'context-overflow',
handoff: typeof rebuild.handoff === 'string' ? rebuild.handoff : '',
},
align: 'agent',
createdAt: message.createdAt,
});
continue;
}
}
// /goal 持久记录(桌面 goal-host 落库:role 'assistant' + 空 content + agentMeta 标记)
// → goal 系统卡。不加分支会 fall through 到通用 assistant 处理,渲染成空白气泡。
if (message.role === 'assistant') {
const goalCard = normalizeGoalCard(message);
if (goalCard) {
result.push(goalCard);
continue;
}
}
// session-agent-switch 边界行(desktop 落库 role='agent_switch') → 'agent-switch'
// 系统卡。不加分支会 fall through 到末尾通用处理,渲染成生 JSON 的 system 气泡。
if (message.role === 'agent_switch') {
const c = readRecord(message.content) ?? {};
result.push({
key: messageNormalizeKey(message),
source: message,
kind: 'system',
role: message.role,
label: 'system:agent-switch',
body: '',
systemCardType: 'agent-switch',
systemCardData: c,
align: 'agent',
createdAt: message.createdAt,
});
continue;
}
const systemCardType = normalizeSystemCardType(message.systemCardType);
if (systemCardType) {
result.push({
key: messageNormalizeKey(message),
source: message,
kind: 'system',
role: message.role,
label: `system:${systemCardType}`,
body: '',
systemCardType,
systemCardData: readRecord(message.systemCardData) ?? {},
align: 'agent',
createdAt: message.createdAt,
});
continue;
}
// Orca 互发:user 消息 content = {orcaSource:'worker'|'lead',content} → 卡片;非该格式回退普通文本。
if (message.role === 'user') {
const orcaOrigin = readRecord(message.agentMeta?.origin);
const reportCard = parseOrcaPersistedMessage(
message.content,
orcaOrigin?.kind === 'orca' ? readString(orcaOrigin.senderLabel) ?? undefined : undefined,
);
if (reportCard) {
result.push({
key: messageNormalizeKey(message),
source: message,
kind: 'user',
role: message.role,
// worker 回报不是 Lead 的真实用户输入(历史口径:不切 turn);Lead 发来的消息在
// worker 任务里就是新一轮的输入,保持 'user' 以维持 turn 边界(与改卡片前一致)。
label: reportCard.variant === 'report' ? 'orca:report' : 'user',
body: reportCard.body,
orcaCard: reportCard,
align: 'agent',
createdAt: message.createdAt,
...readSessionOrigin(message),
});
continue;
}
}
// silent-stop 自动续跑注入的「继续」(agentMeta.autoResume,桌面 main 守卫落库):
// 不渲染用户气泡,渲染「连接中断,已自动继续」分隔卡(对齐桌面);kind/label 保持
// user 以保留 turn 边界(上一段被截断 turn 的工具行按历史收敛),align 'agent'
// 让卡片走系统卡的左侧版式而不是右侧用户气泡。
if (message.role === 'user' && message.agentMeta?.autoResume === true) {
const autoResumeInfo = readRecord(message.agentMeta.autoResumeInfo) ?? {};
const autoResumeOutcome = message.agentMeta.autoResumeOutcome;
result.push({
key: messageNormalizeKey(message),
source: message,
kind: 'user',
role: message.role,
label: 'user',
body: '',
systemCardType: 'auto-resume',
isSyntheticTrigger: true,
systemCardData: {
...autoResumeInfo,
...(autoResumeOutcome === 'succeeded' || autoResumeOutcome === 'failed'
? { outcome: autoResumeOutcome }
: {}),
},
align: 'agent',
createdAt: message.createdAt,
});
continue;
}
const userContent = message.role === 'user' ? parseUserContent(message.content) : null;
// 合成 UI 指令行(隐藏续跑 prompt 等):打标 + body 置空,不渲染但保留 turn 边界。
if (userContent && isSyntheticTriggerText(userContent.text)) {
const handoffRecord = parseInputHandoffEndRecord(userContent.text);
if (handoffRecord) {
// 代输入结束:只留一条无值的记录行(对齐桌面),turn 边界照旧。
result.push({
key: messageNormalizeKey(message),
source: message,
kind: 'user',
role: message.role,
label: 'user',
body: '',
systemCardType: 'input-handoff',
isSyntheticTrigger: true,
systemCardData: { ...handoffRecord, at: message.createdAt },
align: 'agent',
createdAt: message.createdAt,
});
continue;
}
result.push({
key: messageNormalizeKey(message),
source: message,
kind: 'user',
role: message.role,
label: 'user',
body: '',
isSyntheticTrigger: true,
align: 'user',
createdAt: message.createdAt,
});
continue;
}
const rawBody = userContent ? userContent.text : contentToPreview(message.content);
const hookSource = message.role === 'user' ? readHookSource(message, rawBody) : undefined;
// /goal 裁决块只给 Desktop 驱动续跑,与 Desktop 一致只在显示层剥掉(原文仍在 source)。
// 本机 IM 落库的就是用户原文:正文保持完整落库内容,分叉 / 回退据此恢复草稿。
const body = hookSource && !hookSource.userTextContent
? hookSource.userText
: message.role === 'assistant' ? stripGoalVerdictBlock(rawBody)
: readRecord(message.agentMeta?.origin)?.kind === 'session' ? forwardedRequestDisplayText(rawBody) : rawBody;
const turnCost = readTurnCost(message);
result.push({
key: messageNormalizeKey(message),
source: message,
kind: message.role === 'user' ? 'user' : message.role === 'assistant' ? 'assistant' : 'system',
role: message.role,
label: message.role,
body,
...(message.role === 'user' && !hookSource && body !== rawBody ? { draftBody: rawBody } : {}),
attachments: userContent?.attachments,
...(userContent?.quotesEncoded === true ? { quotesEncoded: true } : {}),
sessionReferences: userContent?.sessionReferences,
...(userContent?.pastedTextRanges?.length
? { pastedTextRanges: userContent.pastedTextRanges }
: {}),
...(userContent?.slashCommandRanges !== undefined
? { slashCommandRanges: userContent.slashCommandRanges }
: {}),
...(userContent?.agentReferences?.length
? { agentReferences: userContent.agentReferences }
: {}),
...(userContent?.replyQuote ? { replyQuote: userContent.replyQuote } : {}),
align: message.role === 'user' && hookSource === undefined ? 'user' : 'agent',
createdAt: message.createdAt,
isStreaming: readMessageStreaming(message) || undefined,
...(remoteMessageCompletesTurn(message, turnCost)
? { turnCompleted: true }
: {}),
...turnCost,
...readModelMismatch(message),
...(message.role === 'assistant' && message.agentMeta?.failedSamplingAttempt ? { failedSamplingAttempt: true } : {}),
...(message.role === 'assistant' ? { sourceGroup: readMessageSourceGroup(message.agentMeta) } : {}),
...(message.role === 'assistant' && message.agentMeta?.explicitDelivery === true ? { explicitDelivery: true } : {}),
...(message.role === 'user' ? readAutomationOrigin(message) : {}),
...(message.role === 'user' ? readSessionOrigin(message) : {}),
...(message.role === 'user' ? readMessageSourceFields(message) : {}),
...(hookSource ? { hookSource } : {}),
});
const quotaResumeInfo = readRecord(message.agentMeta?.autoResumeInfo);
if (message.role === 'user' && quotaResumeInfo?.reason === 'usage-limit-reset') {
// Same durable row, two visible parts: retain the user's original input
// and show the existing recovery activity without creating another turn.
result.push({
key: `${messageNormalizeKey(message)}:usage-limit-resume`,
source: message,
kind: 'system',
role: message.role,
label: 'system:auto-resume',
body: '',
systemCardType: 'auto-resume',
systemCardData: quotaResumeInfo,
isSyntheticTrigger: true,
align: 'agent',
createdAt: message.createdAt,
});
}
}
const pluginInvocations = collectPluginInvocations(sorted, toolResultPairing);
for (const row of result) {
if (row.kind === 'user' && !row.isSyntheticTrigger && !row.hookSource && !row.automationOrigin) {
row.pluginInvocations = pluginInvocations.get(row.source.clientId || row.source.id);
}
}
dedupeToolImagesAgainstAssistantMarkdown(result);
return result;
}
/**
* 与 Desktop 同口径:Agent 正文内联同一 URL 时由正文负责排版;否则保留
* tool_result 图片作为可靠兜底。只在同一真实 user turn 内去重。
*/
function dedupeToolImagesAgainstAssistantMarkdown(
messages: NormalizedRemoteMessage[],
): void {
const dedupeTurn = (lo: number, hi: number): void => {
if (hi <= lo) return;
const inlineUrls = new Set<string>();
for (const message of messages.slice(lo, hi)) {
if (message.kind !== 'assistant') continue;
for (const image of collectMobileMarkdownImages(message.body)) inlineUrls.add(image.url);
}
const cards = new Set<string>();
for (const message of messages.slice(lo, hi)) {
if (message.kind !== 'tool') continue;
message.cardIds = message.cardIds?.filter((id) => {
if (cards.has(id)) return false;
cards.add(id);
return true;
});
message.media = message.media?.filter(
(item) => item.kind !== 'image' || !inlineUrls.has(item.url),
);
}
};
let turnStart = 0;
for (let index = 0; index <= messages.length; index += 1) {
const message = messages[index];
const isBoundary =
message?.kind === 'user' &&
!message.isSyntheticTrigger &&
message.source.agentMeta?.delivery !== 'steer';
if (isBoundary && index > turnStart) {
dedupeTurn(turnStart, index);
turnStart = index;
}
if (index === messages.length) dedupeTurn(turnStart, index);
}
}
function toolResultContentToPreview(content: unknown): string {
if (content !== null && typeof content === 'object') {
const language = i18n.resolvedLanguage ?? i18n.language;
const cached = toolResultPreviewByContent.get(content);
if (cached?.language === language) return cached.preview;
const preview = uncachedToolResultContentToPreview(content);
toolResultPreviewByContent.set(content, { language, preview });
return preview;
}
return uncachedToolResultContentToPreview(content);
}
function uncachedToolResultContentToPreview(content: unknown): string {
const compacted = parseToolResultCompactionMarker(content);
if (!compacted) return contentToPreview(content);
return i18n.t('message.renderer.toolResultCompacted', {
size: formatToolResultCompactionBytes(compacted.originalBytes),
});
}
function toolResultContentFor(
message: RemoteMessage,
tool: ToolUsePayload,
pairing: MessageToolResultPairing<RemoteMessage>,
): string | undefined {
return pairing.resultContentFor(message, tool);
}
function parseToolUse(message: RemoteMessage): ToolUsePayload {
const cached = toolUsePayloadByMessage.get(message);
if (cached) return cached;
const sharedTool = parseMessageToolUse(message);
const projection = message.mobileToolInputProjection;
if (projection) {
const payload = {
...sharedTool,
toolName: projection.toolName,
summary: projection.summary,
};
toolUsePayloadByMessage.set(message, payload);
return payload;
}
const { toolName, input } = sharedTool;
const summary = toolName ? formatToolUseSummary(toolName, input) : contentToPreview(message.content);
const diff = buildToolDiff(toolName, input);
const payload = { ...sharedTool, summary, diff };
toolUsePayloadByMessage.set(message, payload);
return payload;
}
function parseUserContent(content: unknown): {
text: string;
attachments: NormalizedAttachment[];
quotesEncoded: boolean;
sessionReferences: MobilePersistedSessionReferenceMetadata[];
pastedTextRanges?: Array<{ start: number; end: number; display: string }>;
slashCommandRanges?: Array<{ start: number; end: number }>;
agentReferences?: AgentInputReference[];
replyQuote?: ChatReplyQuote;
} {
const parsed = parseMaybeJsonObject(content);
if (!parsed) {
return {
text: contentToPreview(content),
attachments: [],
quotesEncoded: false,
sessionReferences: [],
};
}
const text = typeof parsed.text === 'string' ? parsed.text : contentToPreview(content);
const pastedTextRanges = readSentPastedTextRanges(parsed.pastedTextRanges, text);
const slashCommandRanges = readSentSlashCommandRanges(parsed.slashCommandRanges, text);
const agentReferences = readAgentInputReferences(parsed.agentReferences, text);
const replyQuote = readChatReplyQuote(parsed.replyQuote);
return {
text,
...(replyQuote ? { replyQuote } : {}),
quotesEncoded: parsed.quotesEncoded === true,
...(pastedTextRanges ? { pastedTextRanges } : {}),
...(slashCommandRanges !== undefined ? { slashCommandRanges } : {}),
...(agentReferences.length > 0 ? { agentReferences } : {}),
attachments: [
...readImageAttachments(parsed.images),
...readFileAttachments(parsed.files),
],
sessionReferences: parseMobilePersistedSessionReferenceMetadata(parsed.sessionReferences),
};
}
function readImageAttachments(value: unknown): NormalizedAttachment[] {
if (!Array.isArray(value)) return [];
return value.flatMap((item, index) => {
const record = readRecord(item);
if (!record) return [];
const url = readString(record.url);
const base64 = readString(record.base64);
const mimeType = readString(record.mimeType) ?? readString(record.type) ?? 'image/png';
const name = readString(record.originalName) ?? readString(record.name) ?? `image-${index + 1}`;
const sha256 = readString(record.sha256);
const uri = url ?? (base64 ? `data:${mimeType};base64,${base64}` : undefined);
if (!uri) return [];
return [{
kind: 'image' as const,
name,
uri,
mimeType,
...(sha256 ? { sha256 } : {}),
previewable: isPreviewableUri(uri),
}];
});
}
function readFileAttachments(value: unknown): NormalizedAttachment[] {
if (!Array.isArray(value)) return [];
return value.flatMap((item, index) => {
const record = readRecord(item);
if (!record) return [];
const path = readString(record.path) ?? readString(record.url);
const name = readString(record.name) ?? readString(record.originalName) ?? path?.split(/[\\/]/).pop() ?? `file-${index + 1}`;
if (!path && !name) return [];
return [{
kind: 'file' as const,
name,
path: path ?? undefined,
mimeType: readString(record.mimeType) ?? undefined,
previewable: false,
}];
});
}
/**
* /goal 持久记录 → goal 系统卡(对齐桌面 makerChatStore 从 agentMeta.goalCompletion /
* goalNotice 派生 'goal-complete' / 'goal-resumed' system card 的逻辑)。非 goal 记录
* 返回 null,走通用 assistant 处理。
*/
function normalizeGoalCard(message: RemoteMessage): NormalizedRemoteMessage | null {
const meta = message.agentMeta;
if (!meta) return null;
const completion = readRecord(meta.goalCompletion);
const notice = typeof meta.goalNotice === 'string' ? meta.goalNotice : null;
if (!completion && !notice) return null;
const systemCardType = completion ? ('goal-complete' as const) : ('goal-resumed' as const);
return {
key: messageNormalizeKey(message),
source: message,
kind: 'system',
role: message.role,
label: `system:${systemCardType}`,
body: '',
systemCardType,
systemCardData: completion ?? { kind: notice },
align: 'agent',
createdAt: message.createdAt,
};
}
function normalizeAskUser(message: RemoteMessage): NormalizedRemoteMessage | null {
const content = readRecord(message.content);
if (!content) return null;
if (content.status !== 'answered') return null;
const questions = readQuestionTexts(content.questions);
const answers = readStringRecord(content.answers);
const pairs = questions.length > 0
? questions.map((question) => ({ question, answer: answers?.[question] ?? '' }))
: [{
question: readString(content.question) ?? contentToPreview(message.content),
answer: readString(content.reply) ?? '',
}];
const body = pairs
.filter((pair) => pair.question || pair.answer)
.map((pair) => `Q: ${pair.question}\nA: ${pair.answer || '(skipped)'}`)
.join('\n\n');
if (!body) return null;
return {
key: messageNormalizeKey(message),
source: message,
kind: 'ask_user',
role: message.role,
label: 'ask_user',
body,
align: 'agent',
createdAt: message.createdAt,
};
}
function normalizePlanReview(message: RemoteMessage): NormalizedRemoteMessage {
const content = readRecord(message.content);
const rawStatus = readString(content?.status);
// 'cancelled' 是桌面写侧的一等状态(用户主动取消审阅);漏枚举会被静默降级成
// 'expired'(系统过期),语义错标。
const status = rawStatus === 'approved' || rawStatus === 'revised' || rawStatus === 'pending' || rawStatus === 'cancelled'
? rawStatus
: 'expired';
const plan = readString(content?.plan) ?? '';
const feedback = readString(content?.feedback) ?? '';
const summary = summarizePlan(plan);
const body = status === 'revised'
? (feedback || summary)
: summary;
return {
key: messageNormalizeKey(message),
source: message,
kind: 'plan_review',
role: message.role,
label: `plan_review:${status}`,
body,
secondaryBody: status === 'revised' && feedback && summary ? summary : undefined,
align: 'agent',
createdAt: message.createdAt,
};
}
function normalizeThinking(message: RemoteMessage): NormalizedRemoteMessage | null {
const content = readRecord(message.content);
const text = readString(content?.text) ?? '';
const durationMs = readNumber(content?.durationMs) ?? 0;
const redacted = content?.isRedacted === true;
// Opus 4.8+ / Fable 5 的 omitted thinking 占位块(空文本 + 零时长):上游只回带
// 签名的空块,渲染出来就是满屏"思考 1s"噪音,直接不进渲染流(对齐桌面 #467 的
// isOmittedThinkingPlaceholder 判定;redacted 块与真实流过增量的空块不受影响)。
if (!redacted && text === '' && durationMs === 0) return null;
return {
key: messageNormalizeKey(message),
source: message,
kind: 'thinking',
role: message.role,
label: durationMs > 0 ? `thinking ${formatDuration(durationMs)}` : 'thinking',
body: redacted ? 'Thinking hidden' : text,
align: 'agent',
createdAt: normalizeThinkingCreatedAt(message.createdAt, content, durationMs),
// 流式标记必须随 thinking 透传:ThinkingCard 的「思考中 Xs」实时计时以
// message.isStreaming 为运行判定,丢掉它计时器永远不启动(review #643 实锤)。
isStreaming: readMessageStreaming(message) || undefined,
};
}
function formatToolUseSummary(toolName: string, input: unknown): string {
return formatPayloadToolUseSummary(toolName, input);
}
function normalizeThinkingCreatedAt(
createdAt: string,
content: Record<string, unknown> | null,
durationMs: number,
): string {
const finishedAt = readTimestamp(content?.finishedAt) ?? readTimestamp(createdAt);
if (finishedAt === null || durationMs <= 0) return createdAt;
return new Date(finishedAt - durationMs).toISOString();
}
function buildToolDiff(toolName: string, input: unknown): NormalizedToolDiff | undefined {
return buildPayloadToolDiff(toolName, input);
}
export function extractToolResultMedia(toolResult: string): NormalizedToolMedia[] {
return extractPayloadToolResultMedia(toolResult);
}
function readQuestionTexts(value: unknown): string[] {
if (!Array.isArray(value)) return [];
return value
.map((item) => readString(readRecord(item)?.question))
.filter((item): item is string => !!item);
}
function readStringRecord(value: unknown): Record<string, string> | null {
const record = readRecord(value);
if (!record) return null;
const out: Record<string, string> = {};
for (const [key, raw] of Object.entries(record)) {
out[key] = typeof raw === 'string' ? raw : raw == null ? '' : String(raw);
}
return out;
}
function summarizePlan(plan: string, maxLines = 3): string {
const lines = plan.split('\n').map((line) => line.trim()).filter(Boolean);
const head = lines.slice(0, maxLines).join('\n');
return lines.length > maxLines ? `${head}\n...` : head;
}
function readRecord(value: unknown): Record<string, unknown> | null {
return value && typeof value === 'object' && !Array.isArray(value)
? value as Record<string, unknown>
: null;
}
function parseMaybeJsonObject(value: unknown): Record<string, unknown> | null {
if (typeof value === 'string') return parseJsonObject(value);
return readRecord(value);
}
function parseJsonObject(value: string): Record<string, unknown> | null {
try {
return readRecord(JSON.parse(value));
} catch {
return null;
}
}
function readString(value: unknown): string | null {
return typeof value === 'string' && value.length > 0 ? value : null;
}
function normalizeSystemCardType(value: unknown): MobileSystemCardType | null {
return value === 'help'
|| value === 'context'
|| value === 'cost'
|| value === 'pwd'
|| value === 'status'
|| value === 'compact'
|| value === 'context-rebuild'
|| value === 'cmd'
|| value === 'learn'
? value
: null;
}
function readNumber(value: unknown): number | null {
return typeof value === 'number' && Number.isFinite(value) ? value : null;
}
function readTimestamp(value: unknown): number | null {
if (typeof value === 'number' && Number.isFinite(value)) return value;
if (typeof value !== 'string' || !value) return null;
const timestamp = new Date(value).getTime();
return Number.isFinite(timestamp) ? timestamp : null;
}
/**
* agentMeta 里一对「金额 + 旧版 USD 数字 + 估算标记」→ 操作行可显示的金额投影。
* 用户轮累计与当前 segment 走同一个实现,免得两处各写一份判据后漂移。
*/
function projectTurnMoney(
money: unknown,
legacyUsd: unknown,
isEstimateFlag: boolean,
): Pick<NormalizedRemoteMessage, 'turnMoney' | 'turnCostUsd'> | null {
const normalized = normalizeRemoteMoney(money);
if (normalized && normalized.amount > 0) {
const isEstimate = isEstimateFlag || normalized.kind === 'value-estimate';
// NormalizedRemoteMessage is a display projection, not the accounting record. Fold the
// separate wire flag into turnMoney so visible text and accessibility cannot choose
// different estimate semantics for mixed actual-cost + value-estimate user-turn totals.
const displayMoney: RemoteMoney = isEstimate
? { ...normalized, approximate: true, kind: 'value-estimate' }
: normalized;
return {
turnMoney: displayMoney,
...(displayMoney.currency === 'USD' ? { turnCostUsd: displayMoney.amount } : {}),
};
}
const cost = readNumber(legacyUsd);
if (cost === null || cost <= 0) return null;
return {
turnMoney: {
amount: cost,
currency: 'USD',
approximate: isEstimateFlag,
kind: isEstimateFlag ? 'value-estimate' : 'actual-cost',
},
turnCostUsd: cost,
};
}
/** Same completion boundary for normalization and streaming-prefix invalidation. */
export function remoteMessageCompletesTurn(
message: RemoteMessage,
turnCost = readTurnCost(message),
): boolean {
if (message.agentMeta?.failedSamplingAttempt) return false;
return message.role === 'assistant' && (
message.agentMeta?.turnCompleted === true
|| (turnCost.turnMoney?.amount ?? 0) > 0
// Usage without a price is also written only when the turn ends.
|| turnCost.turnTotalTokens !== undefined
);
}
function readTurnCost(
message: RemoteMessage,
): Pick<
NormalizedRemoteMessage,
'turnMoney' | 'turnCostUsd' | 'turnTotalTokens'
> {
if (message.role !== 'assistant') return {};
// 用量与金额分开读:桌面算不出报价的轮次只落 turnUsageDetails,操作行退回显示 token。
const totalTokens = readNumber(readRecord(message.agentMeta?.turnUsageDetails)?.totalTokens);
const usage: Pick<NormalizedRemoteMessage, 'turnTotalTokens'> =
totalTokens !== null && totalTokens > 0 ? { turnTotalTokens: totalTokens } : {};
// 整轮累计优先于当前 segment(与桌面 MessageActionBar 的 displayedMoney 同口径):
// 一次用户请求含多个自动续跑 segment 时,操作行只挂在收尾正文上,而它要承载整轮总额;
// 收尾 segment 缺报价的轮次更是只有 userTurnCost。两者独立判定,不互为前提
// (不变量正本见 apps/desktop/src/shared/turnCostPayload.ts)。
const projected =
projectTurnMoney(
message.agentMeta?.userTurnCost,
message.agentMeta?.userTurnCostUsd,
message.agentMeta?.userTurnCostIsEstimate === true,
) ??
projectTurnMoney(
message.agentMeta?.turnCost,
message.agentMeta?.turnCostUsd,
message.agentMeta?.turnCostIsEstimate === true,
);
return projected ? { ...usage, ...projected } : usage;
}
// 桌面 main 在 turn 结束检测到模型被上游降级时写 agentMeta.modelMismatch =
// { selected, actual }(modelMismatchBroadcaster);字段不全的一律忽略。
function readModelMismatch(message: RemoteMessage): Pick<NormalizedRemoteMessage, 'modelMismatch'> {
if (message.role !== 'assistant') return {};
const mm = readRecord(message.agentMeta?.modelMismatch);
if (!mm) return {};
const selected = readString(mm.selected);
const actual = readString(mm.actual);
if (!selected || !actual) return {};
return { modelMismatch: { selected, actual } };
}
// 工具投递落库时在 agentMeta.origin 写 { kind:'session', senderSessionId, senderSessionTitle? };
// Orca 互发写 { kind:'orca', senderSessionId? }(老数据没有 senderSessionId,不出标签)。
function readSessionOrigin(message: RemoteMessage): Pick<NormalizedRemoteMessage, 'sessionOrigin'> {
const origin = readRecord(message.agentMeta?.origin);
if (!origin || (origin.kind !== 'session' && origin.kind !== 'orca')) return {};
const senderSessionId = readString(origin.senderSessionId)?.trim();
if (!senderSessionId) return origin.kind === 'session' ? { sessionOrigin: {} } : {};
const senderSessionTitle = origin.kind === 'session' ? readString(origin.senderSessionTitle)?.trim() : undefined;
const senderBotName = origin.kind === 'session' && readString(origin.senderBotId)
? (readString(origin.senderBotName)?.trim() || readString(origin.senderBotId)?.trim())
: undefined;
return {
sessionOrigin: {
senderSessionId,
...(senderSessionTitle ? { senderSessionTitle } : {}),
...(senderBotName ? { senderBotName } : {}),
},
};
}
// scheduler runner 落库时在 agentMeta.origin 写 { kind:'scheduler', scheduleId, scheduleName? }
// (见桌面 MessageAutomationOrigin)。共享任务访客收到的是脱敏的 { kind:'scheduler' }:
// 仍返回来源(不带 id / 名字),标签显示「由自动化发送」,收起阈值与自动化消息一致。
// 没有 scheduleId 时一并丢弃名字,不展示未经主机脱敏规则确认的名字。
function readAutomationOrigin(message: RemoteMessage): Pick<NormalizedRemoteMessage, 'automationOrigin'> {
const origin = readRecord(message.agentMeta?.origin);
if (!origin || origin.kind !== 'scheduler') return {};
const scheduleId = readString(origin.scheduleId);
// Hook 渠道消息复用 scheduler 形态(scheduleId 为 `hook:<连接>`),不是自动化:不出自动化标签
// (有 hookSource 时由渠道卡片表明来源)。
if (isHookSchedulerOrigin(origin)) return {};
if (!scheduleId) return { automationOrigin: {} };
const scheduleName = readString(origin.scheduleName);
return {
automationOrigin: {
scheduleId,
...(scheduleName ? { scheduleName } : {}),
},
};
}
// 主机盖章的「谁 / 在哪台设备」来源:设备、插件、共享任务作者。读取与名字净化走共享
// message-source 助手,与发给模型的来源说明同一份事实。
function readMessageSourceFields(
message: RemoteMessage,
): Pick<NormalizedRemoteMessage, 'sourceDevice' | 'sourcePlugin' | 'sharedAuthorName' | 'sharedAuthorMemberId' | 'sharedAuthorAccountId'> {
const sourceDevice = readMessageSourceDevice(message.agentMeta);
const sourcePlugin = readMessageSourcePlugin(message.agentMeta);
const sharedAuthorName = sanitizeSourceName(sharedTaskAuthorName(message.agentMeta));
const sharedAuthorMemberId = sharedAuthorName ? sharedTaskAuthorMemberId(message.agentMeta) : undefined;
const sharedAuthorAccountId = sharedTaskAuthorAccountId(message.agentMeta);
return {
...(sourceDevice ? { sourceDevice } : {}),
...(sourcePlugin ? { sourcePlugin } : {}),
...(sharedAuthorName ? { sharedAuthorName } : {}),
...(sharedAuthorMemberId ? { sharedAuthorMemberId } : {}),
...(sharedAuthorAccountId ? { sharedAuthorAccountId } : {}),
};
}
/**
* 本机 IM 写 agentMeta.imSource(优先),共享 relay Hook 写 agentMeta.hookSource,两者同一
* 展示结构(对齐桌面 makerChatStore 的 `imSource ?? hookSource`)。
* Fail closed on unknown providers and bound all server-controlled display fields.
*/
function readHookSource(message: RemoteMessage, fallbackBody: string): NormalizedHookSource | undefined {
const source = readRecord(message.agentMeta?.imSource) ?? readRecord(message.agentMeta?.hookSource);
if (!source || !isMobileImPlatform(source.im)) {
return undefined;
}
const userText = (
typeof source.userText === 'string' ? source.userText : fallbackBody
).slice(0, 20_000);
const channelName = readString(source.channelName)?.slice(0, 160);
const rawContext = Array.isArray(source.threadContext) ? source.threadContext.slice(0, 20) : [];
const threadContext = rawContext.flatMap((value) => {
const entry = readRecord(value);
const author = readString(entry?.author)?.slice(0, 128);
const text = readString(entry?.text)?.slice(0, 4_000);
if (!author || text == null) return [];
return [{ author, text, ...(entry?.isBot === true ? { isBot: true } : {}) }];
});
return {
im: source.im,
userText,
userTextContent: source.contentFormat === 'user-text',
...(channelName ? { channelName } : {}),
...(threadContext.length > 0 ? { threadContext } : {}),
};
}
function readMessageStreaming(message: RemoteMessage): boolean {
if (message.agentMeta?.isStreaming === true || message.agentMeta?.streaming === true) return true;
const content = readRecord(message.content);
return content?.isStreaming === true || content?.streaming === true;
}
function isPreviewableUri(uri: string): boolean {
return uri.startsWith('http://') || uri.startsWith('https://') || uri.startsWith('data:image/');
}