(messages)
| 1749 | }, |
| 1750 | sessionIngressUrl, |
| 1751 | writeMessages(messages) { |
| 1752 | // Filter to user/assistant messages that haven't already been sent. |
| 1753 | // Two layers of dedup: |
| 1754 | // - initialMessageUUIDs: messages sent as session creation events |
| 1755 | // - recentPostedUUIDs: messages recently sent via POST |
| 1756 | const filtered = messages.filter( |
| 1757 | m => |
| 1758 | isEligibleBridgeMessage(m) && |
| 1759 | !initialMessageUUIDs.has(m.uuid) && |
| 1760 | !recentPostedUUIDs.has(m.uuid), |
| 1761 | ) |
| 1762 | if (filtered.length === 0) return |
| 1763 | |
| 1764 | // Fire onUserMessage for title derivation. Scan before the flushGate |
| 1765 | // check — prompts are title-worthy even if they queue behind the |
| 1766 | // initial history flush. Keeps calling on every title-worthy message |
| 1767 | // until the callback returns true; the caller owns the policy. |
| 1768 | if (!userMessageCallbackDone) { |
| 1769 | for (const m of filtered) { |
| 1770 | const text = extractTitleText(m) |
| 1771 | if (text !== undefined && onUserMessage?.(text, currentSessionId)) { |
| 1772 | userMessageCallbackDone = true |
| 1773 | break |
| 1774 | } |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | // Queue messages while the initial flush is in progress to prevent |
| 1779 | // them from arriving at the server interleaved with history. |
| 1780 | if (flushGate.enqueue(...filtered)) { |
| 1781 | logForDebugging( |
| 1782 | `[bridge:repl] Queued ${filtered.length} message(s) during initial flush`, |
| 1783 | ) |
| 1784 | return |
| 1785 | } |
| 1786 | |
| 1787 | if (!transport) { |
| 1788 | const types = filtered.map(m => m.type).join(',') |
| 1789 | logForDebugging( |
| 1790 | `[bridge:repl] Transport not configured, dropping ${filtered.length} message(s) [${types}] for session=${currentSessionId}`, |
| 1791 | { level: 'warn' }, |
| 1792 | ) |
| 1793 | return |
| 1794 | } |
| 1795 | |
| 1796 | // Track in the bounded ring buffer for echo filtering and dedup. |
| 1797 | for (const msg of filtered) { |
| 1798 | recentPostedUUIDs.add(msg.uuid) |
| 1799 | } |
| 1800 | |
| 1801 | logForDebugging( |
| 1802 | `[bridge:repl] Sending ${filtered.length} message(s) via transport`, |
| 1803 | ) |
| 1804 | |
| 1805 | // Convert to SDK format and send via HTTP POST (HybridTransport). |
| 1806 | // The web UI receives them via the subscribe WebSocket. |
| 1807 | const sdkMessages = toSDKMessages(filtered) |
| 1808 | const events: TransportMessage[] = sdkMessages.map(sdkMsg => ({ |
nothing calls this directly
no test coverage detected