BatchPartsProcessor
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, improving user experience by consolidating small text chunks, and optimizing streaming performance by controlling when parts are emitted to the client.
使用示例Direct link to 使用示例
🌐 Usage example
import { BatchPartsProcessor } from "@mastra/core/processors";
const processor = new BatchPartsProcessor({
batchSize: 5,
maxWaitTime: 100,
emitOnNonText: true
});
构造函数参数Direct link to 构造函数参数
🌐 Constructor parameters
options?:
Options
Configuration options for batching stream parts
选项Direct link to 选项
🌐 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
返回Direct link to 返回
🌐 Returns
id:
string
Processor identifier set to 'batch-parts'
name?:
string
Optional processor display name
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
扩展使用示例Direct link to 扩展使用示例
🌐 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({
name: "batched-agent",
instructions: "You are a helpful assistant",
model: "openai/gpt-5.1",
outputProcessors: [
new BatchPartsProcessor({
batchSize: 5,
maxWaitTime: 100,
emitOnNonText: true
})
]
});
相关Direct link to 相关
🌐 Related