import type { Message } from '../types.js'; export const BACKGROUND_WAKE_MARKER = '[系统事件 · 不是用户输入]'; type NoticeRoute = NonNullable['taskNotice']>; export function noticeRoute(deliveryId: string | undefined, activeId: string | undefined): NoticeRoute { return { deliveryId, origin: !deliveryId || !activeId ? 'unknown' : deliveryId === activeId ? 'current' : 'prior' }; } export const noticeOriginLabel = { current: '当前目标', prior: '先前目标', unknown: '来源待确认' } as const; export function isPriorTaskNotice(message: Message): boolean { if (message.role !== 'user' || !/^(agent|proc)-notice:/.test(message.id ?? '')) return false; if (message.taskNotice) return message.taskNotice.origin === 'prior'; // Only historical notice envelopes predate routing metadata. return typeof message.content === 'string' && message.content.split('\n', 3)[2]?.startsWith('来源:先前目标') === true; } /** One model projection for request construction, compaction and window estimates. */ export function isModelHistoryMessage(message: Message): boolean { return !(message.role === 'system' && message._ephemeral) && !isPriorTaskNotice(message) && !(message.role === 'user' && message.taskNotice?.accounted === true); } /** Host events use stable IDs, not a provider role pretending to be a human. */ export function isSystemEventMessage(message: Message): boolean { if (message.role !== 'user') return false; if (message.inputOrigin) return true; if (/^(?:agent-notice:|proc-notice:|proc-wake:)/.test(message.id ?? '')) return true; // Compatibility for wake stubs persisted before IDs were tagged. const text = typeof message.content === 'string' ? message.content : message.content.find(part => part.type === 'text')?.text ?? ''; return [BACKGROUND_WAKE_MARKER, 'This session is being continued from a previous conversation', '(系统提示:用户在工作过程中发送了新的消息。', '(自动压缩失败:', '(LLM 摘要压缩失败,已使用规则式截断继续会话)', '(LLM 返回了无效摘要,已使用规则式截断继续会话)', '【实况图像】上面是刚才工具执行后附带的真实图像', '【实况图像】(此前的实况图像已从上下文移除以节省空间。)', ].some(prefix => text.startsWith(prefix)); }