跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

BatchPartsProcessor

The BatchPartsProcessor is an output processor that batches multiple stream parts together to reduce the frequency of emissions during streaming. This processor is useful for reducing network overhead and consolidating small text chunks for users, while controlling emissions to optimize streaming performance.

Usage example
Usage example的直接链接

import { BatchPartsProcessor } from '@mastra/core/processors'

const processor = new BatchPartsProcessor({
batchSize: 5,
maxWaitTime: 100,
emitOnNonText: true,
})

Constructor parameters
Constructor parameters的直接链接

options?:

Options
Configuration options for batching stream parts
Options

batchSize?:

number
Number of parts to batch together before emitting

maxWaitTime?:

number
Maximum time to wait before emitting a batch (in milliseconds). If set, will emit the current batch even if it hasn't reached batchSize

emitOnNonText?:

boolean
Whether to emit immediately when a non-text part is encountered

Returns
Returns的直接链接

id:

string
Processor identifier set to 'batch-parts'

name:

string
Processor display name. Always Batch Parts.

processOutputStream:

(args: { part: ChunkType; streamParts: ChunkType[]; state: Record<string, any>; abort: (reason?: string) => never }) => Promise<ChunkType | null>
Processes streaming output parts to batch them together

flush:

(state?: BatchPartsState) => ChunkType | null
Force flush any remaining batched parts when the stream ends

Extended usage example
Extended usage example的直接链接

src/mastra/agents/batched-agent.ts
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,
}),
],
})