@@ -235,6 +235,32 @@ export function createBlockReplyPipeline(params: {
|
235 | 235 | bufferedPayloadKeys.clear(); |
236 | 236 | }; |
237 | 237 | |
| 238 | +const enqueueCoalescedPayload = (payload: ReplyPayload) => { |
| 239 | +if (!coalescer) { |
| 240 | +return; |
| 241 | +} |
| 242 | +const assistantMessageIndex = getReplyPayloadMetadata(payload)?.assistantMessageIndex; |
| 243 | +if ( |
| 244 | +assistantMessageIndex !== undefined && |
| 245 | +bufferedAssistantMessageIndex !== undefined && |
| 246 | +assistantMessageIndex !== bufferedAssistantMessageIndex && |
| 247 | +coalescer.hasBuffered() |
| 248 | +) { |
| 249 | +// Logical assistant blocks must not be merged together by the generic |
| 250 | +// coalescer. Force-flush the previous buffered block before starting a |
| 251 | +// new assistant-message block. |
| 252 | +flushBufferedAssistantBlock(); |
| 253 | +} |
| 254 | +const payloadKey = createBlockReplyPayloadKey(payload); |
| 255 | +if (hasSeenOrQueuedPayloadKey(payloadKey) || bufferedKeys.has(payloadKey)) { |
| 256 | +return; |
| 257 | +} |
| 258 | +seenKeys.add(payloadKey); |
| 259 | +bufferedKeys.add(payloadKey); |
| 260 | +bufferedAssistantMessageIndex = assistantMessageIndex; |
| 261 | +coalescer.enqueue(payload); |
| 262 | +}; |
| 263 | + |
238 | 264 | const enqueue = (payload: ReplyPayload) => { |
239 | 265 | if (aborted) { |
240 | 266 | return; |
@@ -248,23 +274,7 @@ export function createBlockReplyPipeline(params: {
|
248 | 274 | { trimText: true }, |
249 | 275 | ); |
250 | 276 | if (reply.hasMedia && coalescer && !hasNonTextContent) { |
251 | | -const assistantMessageIndex = getReplyPayloadMetadata(payload)?.assistantMessageIndex; |
252 | | -if ( |
253 | | -assistantMessageIndex !== undefined && |
254 | | -bufferedAssistantMessageIndex !== undefined && |
255 | | -assistantMessageIndex !== bufferedAssistantMessageIndex && |
256 | | -coalescer.hasBuffered() |
257 | | -) { |
258 | | -flushBufferedAssistantBlock(); |
259 | | -} |
260 | | -const payloadKey = createBlockReplyPayloadKey(payload); |
261 | | -if (hasSeenOrQueuedPayloadKey(payloadKey) || bufferedKeys.has(payloadKey)) { |
262 | | -return; |
263 | | -} |
264 | | -seenKeys.add(payloadKey); |
265 | | -bufferedKeys.add(payloadKey); |
266 | | -bufferedAssistantMessageIndex = assistantMessageIndex; |
267 | | -coalescer.enqueue(payload); |
| 277 | +enqueueCoalescedPayload(payload); |
268 | 278 | return; |
269 | 279 | } |
270 | 280 | if (reply.hasMedia || hasNonTextContent) { |
@@ -273,26 +283,7 @@ export function createBlockReplyPipeline(params: {
|
273 | 283 | return; |
274 | 284 | } |
275 | 285 | if (coalescer) { |
276 | | -const assistantMessageIndex = getReplyPayloadMetadata(payload)?.assistantMessageIndex; |
277 | | -if ( |
278 | | -assistantMessageIndex !== undefined && |
279 | | -bufferedAssistantMessageIndex !== undefined && |
280 | | -assistantMessageIndex !== bufferedAssistantMessageIndex && |
281 | | -coalescer.hasBuffered() |
282 | | -) { |
283 | | -// Logical assistant blocks must not be merged together by the generic |
284 | | -// coalescer. Force-flush the previous buffered block before starting a |
285 | | -// new assistant-message block. |
286 | | -flushBufferedAssistantBlock(); |
287 | | -} |
288 | | -const payloadKey = createBlockReplyPayloadKey(payload); |
289 | | -if (hasSeenOrQueuedPayloadKey(payloadKey) || bufferedKeys.has(payloadKey)) { |
290 | | -return; |
291 | | -} |
292 | | -seenKeys.add(payloadKey); |
293 | | -bufferedKeys.add(payloadKey); |
294 | | -bufferedAssistantMessageIndex = assistantMessageIndex; |
295 | | -coalescer.enqueue(payload); |
| 286 | +enqueueCoalescedPayload(payload); |
296 | 287 | return; |
297 | 288 | } |
298 | 289 | sendPayload(payload, /* bypassSeenCheck */ false); |
|