MCPcopy Index your code
hub / github.com/tiann/hapi / collectBatch

Method collectBatch

cli/src/utils/MessageQueue2.ts:341–384  ·  view source on GitHub ↗

* Collect a batch of messages with the same mode, respecting isolation requirements

()

Source from the content-addressed store, hash-verified

339 * Collect a batch of messages with the same mode, respecting isolation requirements
340 */
341 private collectBatch(): { message: string, mode: T, hash: string, isolate: boolean } | null {
342 if (this.queue.length === 0) {
343 return null;
344 }
345
346 const firstItem = this.queue[0];
347 const sameModeMessages: string[] = [];
348 const consumedLocalIds: string[] = [];
349 let mode = firstItem.mode;
350 let isolate = firstItem.isolate ?? false;
351 const targetModeHash = firstItem.modeHash;
352
353 // If the first message requires isolation, only process it alone
354 if (firstItem.isolate) {
355 const item = this.queue.shift()!;
356 sameModeMessages.push(item.message);
357 if (item.localId) consumedLocalIds.push(item.localId);
358 logger.debug(`[MessageQueue2] Collected isolated message with mode hash: ${targetModeHash}`);
359 } else {
360 // Collect all messages with the same mode until we hit an isolated message
361 while (this.queue.length > 0 &&
362 this.queue[0].modeHash === targetModeHash &&
363 !this.queue[0].isolate) {
364 const item = this.queue.shift()!;
365 sameModeMessages.push(item.message);
366 if (item.localId) consumedLocalIds.push(item.localId);
367 }
368 logger.debug(`[MessageQueue2] Collected batch of ${sameModeMessages.length} messages with mode hash: ${targetModeHash}`);
369 }
370
371 // Join all messages with newlines
372 const combinedMessage = sameModeMessages.join('\n');
373
374 if (consumedLocalIds.length > 0) {
375 this.onBatchConsumed?.(consumedLocalIds);
376 }
377
378 return {
379 message: combinedMessage,
380 mode,
381 hash: targetModeHash,
382 isolate
383 };
384 }
385
386 /**
387 * Wait for messages to arrive

Callers 1

Calls 2

debugMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected