> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # BatchPartsProcessor `BatchPartsProcessor` 是一種**輸出處理器**,會將多個串流部分批次合併,以降低串流期間的發送頻率。此處理器能減少網路負擔、將零碎的文字區塊合併以改善使用者體驗,並透過控制各部分傳送至使用者端的時機來最佳化串流效能。 ## 使用範例 ```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`): 設為 'batch-parts' 的處理器識別碼 **name** (`string`): 選用的處理器顯示名稱 **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, }), ], }) ``` ## 相關資源 - [防護機制](https://mastra.zisheng.pro/zh-TW/docs/agents/guardrails)