> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # BatchPartsProcessor `BatchPartsProcessor` 是一個 **output processor**,會將多個串流部分批次合併,以減少串流期間的發送頻率。此 Processor 可減少網絡開銷、透過整合細小的文字區塊改善用戶體驗,並藉由控制何時向用戶端發送各部分來最佳化串流效能。 ## 使用範例 ```typescript import { BatchPartsProcessor } from '@mastra/core/processors' const processor = new BatchPartsProcessor({ batchSize: 5, maxWaitTime: 100, emitOnNonText: true, }) ``` ## 建構函數參數 **options** (`Options`): 批次處理串流部分的設定選項 **options.batchSize** (`number`): 發送前要合併成一個批次的部分數目 **options.maxWaitTime** (`number`): 發送批次前的最長等候時間(以毫秒為單位)。如已設定,即使目前批次尚未達到 batchSize,亦會將其發送 **options.emitOnNonText** (`boolean`): 遇到非文字部分時是否立即發送 ## 傳回值 **id** (`string`): Processor 識別符,設定為 'batch-parts' **name** (`string`): 可選的 Processor 顯示名稱 **processOutputStream** (`(args: { part: ChunkType; streamParts: ChunkType[]; state: Record; abort: (reason?: string) => never }) => Promise`): 處理串流輸出部分,將它們批次合併 **flush** (`(state?: BatchPartsState) => ChunkType | null`): 串流結束時,強制發送所有剩餘的已批次合併部分 ## 進階使用範例 ```typescript import { Agent } from '@mastra/core/agent' import { BatchPartsProcessor } from '@mastra/core/processors' export const agent = new Agent({ id: 'batched-agent', name: 'batched-agent', instructions: 'You are a helpful assistant', model: 'openai/gpt-5.6-sol', outputProcessors: [ new BatchPartsProcessor({ batchSize: 5, maxWaitTime: 100, emitOnNonText: true, }), ], }) ``` ## 相關內容 - [Guardrails](https://mastra.zisheng.pro/zh-HK/docs/agents/guardrails)