export function getStatusPluginMessagePreviewSource(): string[] { return [ '', 'function capMessagePartText(text) {', ' return text.length > MESSAGE_PART_MAX_CHARS ? text.slice(0, MESSAGE_PART_MAX_CHARS) : text;', '}', '', 'async function postMessagePart(properties, factoryID) {', ' while (messagePartPostInFlight) await messagePartPostInFlight;', ' const delivery = post("MessagePart", properties);', ' messagePartPostInFlight = delivery;', ' try {', ' const delivered = await delivery;', ' if (delivered) deliveredMessagePartFactoryID = factoryID;', ' } finally {', ' if (messagePartPostInFlight === delivery) messagePartPostInFlight = null;', ' }', '}', '', 'async function flushPendingAssistantPart(force = false) {', ' if (assistantPartFlushTimer) {', ' clearTimeout(assistantPartFlushTimer);', ' assistantPartFlushTimer = null;', ' }', ' // Why: an idle/waiting transition must wait for every older preview;', ' // keep one post in flight while later snapshots coalesce in memory.', ' while (messagePartPostInFlight) await messagePartPostInFlight;', ' const pending = pendingAssistantPart;', ' pendingAssistantPart = null;', ' if (!pending) return;', ' if (', ' !activeFactoryIDs.has(pending.factoryID) ||', ' disposingFactoryIDs.has(pending.factoryID)', ' ) return;', ' if (!force && pending.authorityRevision !== stateArrivalRevision) return;', ' lastAssistantPartPostAt = Date.now();', ' await postMessagePart({', ' role: pending.role,', ' text: capMessagePartText(pending.text),', ' messageID: pending.messageID,', ' sessionID: pending.sessionID,', ' }, pending.factoryID);', '}', '', 'function queueAssistantPart(part) {', ' // Why: keep only the latest snapshot — each event already contains the', ' // full accumulated text, so intermediate snapshots are pure waste.', ' pendingAssistantPart = part;', ' const sinceLastPost = Date.now() - lastAssistantPartPostAt;', ' if (sinceLastPost >= MESSAGE_PART_THROTTLE_MS) {', ' void flushPendingAssistantPart();', ' return;', ' }', ' if (!assistantPartFlushTimer) {', ' assistantPartFlushTimer = setTimeout(() => {', ' void flushPendingAssistantPart();', ' }, MESSAGE_PART_THROTTLE_MS - sinceLastPost);', ' if (assistantPartFlushTimer.unref) assistantPartFlushTimer.unref();', ' }', '}', '', '// Why: message.part.updated fires for every Part (text, tool, reasoning)', '// but does not include the message role — that lives on the parent', '// message.updated event. Cache the role per messageID so the plugin can', '// tag a TextPart as user vs assistant when POSTing. Capped at 128 entries', '// so long-running sessions do not grow this map unboundedly.', 'const messageRoleById = new Map();', 'function rememberMessageRole(messageID, role) {', ' if (!messageID || !role) return;', ' if (messageRoleById.size >= 128) {', ' const first = messageRoleById.keys().next().value;', ' if (first !== undefined) messageRoleById.delete(first);', ' }', ' messageRoleById.set(messageID, role);', '}', '' ] }