refactor: share gateway chat text normalization

This commit is contained in:
Peter Steinberger
2026-03-14 00:22:58 +00:00
parent 1886fe5fd9
commit 42f9737e59

View File

@@ -390,12 +390,7 @@ export function createAgentEventHandler({
nodeSendToSession(sessionKey, "chat", payload);
};
const flushBufferedChatDeltaIfNeeded = (
sessionKey: string,
clientRunId: string,
sourceRunId: string,
seq: number,
) => {
const resolveBufferedChatTextState = (clientRunId: string, sourceRunId: string) => {
const bufferedText = stripInlineDirectiveTagsForDisplay(
chatRunState.buffers.get(clientRunId) ?? "",
).text.trim();
@@ -407,6 +402,16 @@ export function createAgentEventHandler({
const text = normalizedHeartbeatText.text.trim();
const shouldSuppressSilent =
normalizedHeartbeatText.suppress || isSilentReplyText(text, SILENT_REPLY_TOKEN);
return { text, shouldSuppressSilent };
};
const flushBufferedChatDeltaIfNeeded = (
sessionKey: string,
clientRunId: string,
sourceRunId: string,
seq: number,
) => {
const { text, shouldSuppressSilent } = resolveBufferedChatTextState(clientRunId, sourceRunId);
const shouldSuppressSilentLeadFragment = isSilentReplyLeadFragment(text);
const shouldSuppressHeartbeatStreaming = shouldHideHeartbeatChatOutput(
clientRunId,
@@ -453,17 +458,7 @@ export function createAgentEventHandler({
error?: unknown,
stopReason?: string,
) => {
const bufferedText = stripInlineDirectiveTagsForDisplay(
chatRunState.buffers.get(clientRunId) ?? "",
).text.trim();
const normalizedHeartbeatText = normalizeHeartbeatChatFinalText({
runId: clientRunId,
sourceRunId,
text: bufferedText,
});
const text = normalizedHeartbeatText.text.trim();
const shouldSuppressSilent =
normalizedHeartbeatText.suppress || isSilentReplyText(text, SILENT_REPLY_TOKEN);
const { text, shouldSuppressSilent } = resolveBufferedChatTextState(clientRunId, sourceRunId);
// Flush any throttled delta so streaming clients receive the complete text
// before the final event. The 150 ms throttle in emitChatDelta may have
// suppressed the most recent chunk, leaving the client with stale text.