Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
import { formatLocalizedDuration, formatLocalizedSeconds } from './sessionDurationFormat';
import type { MobileSlashCommand } from '@/device-link/mobileMakerTransport';
import type { InputProjection, RemoteSession } from '@/session/types';
import {
DEFAULT_LOCAL_SYSTEM_COMMANDS,
buildSystemCardData,
formatSystemCard,
mergeLocalSlashCommands,
parseLocalSystemCommand,
type SystemCardPresentation,
type SystemCardType,
} from '@cindy/maker-shared/system-card';
import { i18n } from '@/i18n';
import {
mobileAgentLabelFromUnknown,
readAgentSwitchLocationTarget,
} from '@/session/sessionAgentSwitch';
import { formatCompactTokens } from '@cindy/maker-shared/usage-format';
/**
* 手机端系统卡类型 = 共享 slash 命令卡 + goal 持久记录卡 + silent-stop 自动续跑卡。
* goal 两种不来自 slash 命令(桌面 goal-host 落库 agentMeta.goalCompletion /
* goalNotice,读侧派生),shared 的 formatSystemCard 不认识它们,由本模块特判
* 格式化。历史上手机端不认这两种卡,/goal 达成与用量恢复记录被渲染成空白
* assistant 气泡(2026-07 排查发现)。auto-resume 同为读侧派生(user 行
* agentMeta.autoResume,桌面 silent-stop 守卫落库),历史上被手机渲染成一条
* 用户没发过的「继续」气泡(2026-07 排查发现)。
*/
export type MobileSystemCardType =
| SystemCardType
| 'goal-complete'
| 'goal-resumed'
| 'context-rebuild'
| 'auto-resume'
| 'learn'
// session-agent-switch 边界卡(desktop 落库 role='agent_switch',读侧派生)。
| 'agent-switch'
// 代输入结束留痕(隐藏续接行派生)。
| 'input-handoff'
| 'mcp-app';
export type MobileSystemCardPresentation = SystemCardPresentation;
/** goal 达成记录文案(对齐桌面 GoalCompleteCard 的 goal.complete.record)。 */
function formatGoalCompleteCard(data: Record<string, unknown> | undefined): SystemCardPresentation {
const turns = typeof data?.turnsUsed === 'number' ? data.turnsUsed : 0;
const elapsedMs = typeof data?.elapsedMs === 'number' ? data.elapsedMs : 0;
const reason = typeof data?.reason === 'string' ? data.reason : '';
return {
title: i18n.t('message.systemCard.goalComplete', { turns, duration: formatGoalDuration(elapsedMs) }),
...(reason ? { body: reason } : {}),
rows: [],
};
}
function formatGoalDuration(ms: number): string {
return formatLocalizedSeconds(Math.max(0, Math.round(ms / 1000)));
}
export const MOBILE_LOCAL_SYSTEM_COMMANDS = DEFAULT_LOCAL_SYSTEM_COMMANDS as MobileSlashCommand[];
export function parseMobileLocalSystemCommand(text: string): MobileSystemCardType | null {
return parseLocalSystemCommand(text, MOBILE_LOCAL_SYSTEM_COMMANDS);
}
/**
* 出卡前需要「会话在被控端已存在」的本地命令。
*
* /context 要向被控端取上下文用量(maker.getContextUsage);/help /cost /pwd /status
* 都只是本地 projection 与会话行的视图,合成行上照样出得来,不该挡——挡了反而破坏
* 新建会话「一切正常」的观感。
*/
export const MOBILE_REMOTE_BACKED_LOCAL_COMMANDS: ReadonlySet<MobileSystemCardType> = new Set(['context']);
/**
* 这次发送要执行的命令是否需要远端会话已经存在。
*
* desktop 命令一律算:手机端白名单(MOBILE_SUPPORTED_DESKTOP_COMMANDS)只有 /learn,
* 而它就是打被控端蒸馏管线的。会话还没建成时这类命令必须挡住而不是执行 —— 也不能
* 排队,outbox 的派发动作是「enqueue 一条消息」,命令原样入队 agent 只会当普通文本
* 忽略(review P1)。
*/
export function commandNeedsRemoteSession(
localCommand: MobileSystemCardType | null,
desktopCommand: { name: string } | null,
): boolean {
if (desktopCommand) return true;
return localCommand !== null && MOBILE_REMOTE_BACKED_LOCAL_COMMANDS.has(localCommand);
}
export function mergeMobileLocalSlashCommands(
remoteCommands: readonly MobileSlashCommand[],
): MobileSlashCommand[] {
return mergeLocalSlashCommands(remoteCommands, MOBILE_LOCAL_SYSTEM_COMMANDS) as MobileSlashCommand[];
}
export function buildMobileSystemCardData(
type: MobileSystemCardType,
options: {
contextUsage?: unknown;
contextError?: string;
projection?: InputProjection;
remoteCommands?: readonly MobileSlashCommand[];
session: RemoteSession | null;
},
): Record<string, unknown> {
// goal / auto-resume / agent-switch 卡的数据由桌面落库行派生,learn 卡的数据由
// 发送侧 buildLearnCardData 直接组装,都不走本地 slash 命令的数据组装。
if (type === 'goal-complete' || type === 'goal-resumed' || type === 'context-rebuild' || type === 'auto-resume' || type === 'learn' || type === 'agent-switch' || type === 'input-handoff' || type === 'mcp-app') return {};
return buildSystemCardData(type, {
...options,
localCommands: MOBILE_LOCAL_SYSTEM_COMMANDS,
remoteCommands: options.remoteCommands,
});
}
export function formatMobileSystemCard(
type: MobileSystemCardType,
data: Record<string, unknown> | undefined,
): MobileSystemCardPresentation {
if (type === 'compact') return formatCompactCard(data);
if (type === 'context-rebuild') return formatContextRebuildCard(data);
if (type === 'goal-complete') return formatGoalCompleteCard(data);
if (type === 'goal-resumed') {
// 两种续跑原因共用这张卡, 但说法必须分开(与桌面 GoalResumedCard 同口径):
// 上游过载那条只是干等了 60s、**没有**任何容量探测, 说「用量已恢复」是假信息;
// 账号限流那条的重置时刻来自账号额度信息, 有依据(review #844 codex P1)。
const isCapacity = data?.kind === 'capacity-resumed';
return {
title: i18n.t(
isCapacity ? 'message.systemCard.goalCapacityRetry' : 'message.systemCard.goalResumed',
),
rows: [],
};
}
if (type === 'auto-resume') return formatAutoResumeCard(data);
if (type === 'agent-switch') return formatAgentSwitchCard(data);
if (type === 'learn') return formatLearnCard(data);
// 代输入留痕由 InputHandoffRecordRow 渲染,不走通用卡片。
if (type === 'input-handoff' || type === 'mcp-app') return { title: '', rows: [] };
return formatSystemCard(type, data);
}
function formatCompactCard(data: Record<string, unknown> | undefined): SystemCardPresentation {
const nonNegativeNumber = (value: unknown) =>
typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : undefined;
const preTokens = nonNegativeNumber(data?.preTokens);
const postTokens = nonNegativeNumber(data?.postTokens);
const durationMs = nonNegativeNumber(data?.durationMs);
const parts = [i18n.t(data?.trigger === 'manual'
? 'message.systemCard.compact.manual'
: 'message.systemCard.compact.auto')];
if (preTokens !== undefined && postTokens !== undefined && preTokens > postTokens) {
parts.push(i18n.t('message.systemCard.compact.savedTokens', { tokens: formatCompactTokens(preTokens - postTokens) }));
}
if (durationMs) {
parts.push(durationMs >= 3_600_000
? formatLocalizedDuration(durationMs)
: `${(durationMs / 1000).toFixed(1)}s`);
}
return { title: parts.join(' · '), rows: [] };
}
function formatContextRebuildCard(data: Record<string, unknown> | undefined): SystemCardPresentation {
const handoff = typeof data?.handoff === 'string' ? data.handoff : '';
const titleKey = data?.reason === 'pi-prompt-timeout'
? 'labelTimeout'
: data?.reason === 'codex-history-strip' ? 'labelStrip' : 'labelOverflow';
// Same terminal marker as desktop: old Chinese handoffs must not be labelled English.
const englishSource = handoff.trimEnd().endsWith("; the user's new message follows ==");
return {
title: i18n.t(`message.systemCard.contextRebuild.${titleKey}`),
...(handoff.trim() ? {
subtitle: i18n.t(englishSource
? 'message.systemCard.contextRebuild.handoffTitleEnglishSource'
: 'message.systemCard.contextRebuild.handoffTitle'),
body: handoff,
} : {}),
rows: [],
};
}
function formatAutoResumeCard(data: Record<string, unknown> | undefined): SystemCardPresentation {
const number = (value: unknown) => typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : undefined;
const error = typeof data?.error === 'string' && data.error.trim() ? data.error : undefined;
const attempt = number(data?.attempt);
const maxAttempts = number(data?.maxAttempts);
const sessionTotal = number(data?.sessionTotal);
const outcome = data?.outcome === 'succeeded' || data?.outcome === 'failed' ? data.outcome : undefined;
const hasInterruptionContext = !!(data?.live === true || error || attempt || maxAttempts || sessionTotal || outcome);
if (!hasInterruptionContext) return { title: i18n.t('message.systemCard.autoResume.separator'), rows: [] };
// 供应商组正在换电脑:错误先不呈现,只写「正在换一台电脑继续」。
const groupSwitchPending = data?.live === true && !!data.groupSwitchPending && typeof data.groupSwitchPending === 'object';
const title = data?.live === true
? groupSwitchPending
? i18n.t('message.systemCard.autoResume.groupSwitchPending')
: attempt && maxAttempts
? i18n.t('message.systemCard.autoResume.pendingWithProgress', { attempt, total: maxAttempts })
: i18n.t('message.systemCard.autoResume.pending')
: i18n.t(`message.systemCard.autoResume.${outcome ?? 'neutral'}`);
const subtitle = attempt && maxAttempts && sessionTotal
? i18n.t('message.systemCard.autoResume.details', { attempt, total: maxAttempts, count: sessionTotal }) : undefined;
return { title, ...(error && !groupSwitchPending ? { body: error } : {}), subtitle, rows: [] };
}
type AgentSwitchTranslate = (key: string, options?: Record<string, unknown>) => string;
/**
* 远程 Agent:边界行带 toAgentDeviceId 键 = 这次切换把 Agent 换到了另一台电脑上运行
* (null = 被控电脑本机),标题改说位置;引擎同时换了再以「 · 目标引擎」带上。没换电脑
* 返回 null,调用方沿用「已从 X 切换到 Y」。名称是切换时的快照,缺名时按位置给通用说法。
* 用户确认为了先用那台的配额换过去的(switchReason: 'quota-rebalance')最后写明原因,与桌面一致。
*/
export function formatAgentSwitchLocationLabel(
data: Record<string, unknown> | undefined,
translate: AgentSwitchTranslate,
): string | null {
const location = readAgentSwitchLocationTarget(data);
if (!location) return null;
const place = location.kind === 'named'
? translate('message.systemCard.agentSwitchLocation', { device: location.name })
: translate(location.kind === 'thisComputer'
? 'message.systemCard.agentSwitchLocationThisComputer'
: 'message.systemCard.agentSwitchLocationOtherComputer');
const from = mobileAgentLabelFromUnknown(data?.fromAgentKind);
const to = mobileAgentLabelFromUnknown(data?.toAgentKind);
const label = from === to ? place : `${place} · ${to}`;
return data?.switchReason === 'quota-rebalance'
? `${label} · ${translate('message.systemCard.agentSwitchLocationQuotaRebalance')}`
: label;
}
/**
* session-agent-switch 边界卡的纯数据兜底(标题 + 目标模型行)。
* 注意:实际渲染已改由 MessageRenderer 的 MobileAgentSwitchCard 直接读 data
* 走「分隔线 + 药丸 + 可展开交接」1:1 对齐桌面 AgentSwitchCard,不再经过本函数;
* 这里仅保留为 formatMobileSystemCard 在该 union 分支上的类型完备兜底。
*/
function formatAgentSwitchCard(data: Record<string, unknown> | undefined): SystemCardPresentation {
const from = mobileAgentLabelFromUnknown(data?.fromAgentKind);
const to = mobileAgentLabelFromUnknown(data?.toAgentKind);
const toModel = typeof data?.toModel === 'string' ? data.toModel : '';
const rows = toModel ? [{ label: i18n.t('message.systemCard.modelLabel'), value: toModel }] : [];
// Phase 2:resumed = 目标引擎续接了自己的停泊原生会话(增量交接)。
if (data?.nativeHistory === 'fallback') rows.push({ label: i18n.t('message.systemCard.sessionLabel'), value: i18n.t('message.renderer.sessionFallbackPill') });
else if (data?.resumed === true) rows.push({ label: i18n.t('message.systemCard.sessionLabel'), value: i18n.t('message.systemCard.sessionResumed') });
return {
title: formatAgentSwitchLocationLabel(data, (key, options) => i18n.t(key, options))
?? i18n.t('message.systemCard.agentSwitch', { from, to }),
rows,
};
}
/**
* /learn 启动反馈卡(移动端本地卡,数据来自 desktopSlashCommands.buildLearnCardData)。
* 蒸馏与评审全在被控端 learn-host:移动端暂无评审 UI,成功态明确引导回桌面端。
*/
function formatLearnCard(data: Record<string, unknown> | undefined): SystemCardPresentation {
const runId = typeof data?.runId === 'string' ? data.runId : '';
if (runId) {
return {
title: i18n.t('message.systemCard.learnStarted'),
rows: [{ label: 'run', value: runId.slice(0, 8) }],
body: i18n.t('message.systemCard.learnStartedBody'),
};
}
const error = typeof data?.error === 'string' ? data.error : 'learn-failed';
return {
title: i18n.t('message.systemCard.learnFailed'),
rows: [],
body: error === 'learn-busy'
? i18n.t('message.systemCard.learnBusyBody')
: i18n.t('message.systemCard.learnFailedBody', { detail: typeof data?.detail === 'string' && data.detail ? data.detail : i18n.t('message.systemCard.unknownError') }),
};
}