MastraModelOutput
MastraModelOutput 类由 .stream() 返回,提供基于流和基于 Promise 的模型输出访问。它支持结构化输出生成、工具调用、推断以及全面的使用跟踪。
🌐 The MastraModelOutput class is returned by .stream() and provides both streaming and promise-based access to model outputs. It supports structured output generation, tool calls, reasoning, and comprehensive usage tracking.
// MastraModelOutput is returned by agent.stream()
const stream = await agent.stream("Hello world");
有关设置和基本使用,请参阅 .stream() 方法的文档。
🌐 For setup and basic usage, see the .stream() method documentation.
流式属性Direct link to 流式属性
🌐 Streaming Properties
这些属性可以在生成模型输出时实时访问它们:
🌐 These properties provide real-time access to model outputs as they're generated:
fullStream:
ChunkType:
textStream:
objectStream:
PartialSchemaOutput:
elementStream:
基于 Promise 的属性Direct link to 基于 Promise 的属性
🌐 Promise-based Properties
这些属性在流完成后会解析为最终值:
🌐 These properties resolve to final values after the stream completes:
text:
object:
InferSchemaOutput:
reasoning:
reasoningText:
toolCalls:
type:
runId:
from:
payload:
toolResults:
type:
runId:
from:
payload:
usage:
inputTokens:
outputTokens:
totalTokens:
reasoningTokens?:
cachedInputTokens?:
finishReason:
stop:
length:
tool_calls:
content_filter:
response:
id?:
timestamp?:
modelId?:
headers?:
messages?:
uiMessages?:
错误属性Direct link to 错误属性
🌐 Error Properties
error:
方法Direct link to 方法
🌐 Methods
getFullOutput:
text:
object?:
toolCalls:
toolResults:
usage:
reasoning?:
finishReason?:
response:
consumeStream:
onError?:
使用示例Direct link to 使用示例
🌐 Usage Examples
基本文本流Direct link to 基本文本流
🌐 Basic Text Streaming
const stream = await agent.stream("Write a haiku");
// Stream text as it's generated
for await (const text of stream.textStream) {
process.stdout.write(text);
}
// Or get the complete text
const fullText = await stream.text;
console.log(fullText);
结构化输出流Direct link to 结构化输出流
🌐 Structured Output Streaming
const stream = await agent.stream("Generate user data", {
structuredOutput: {
schema: z.object({
name: z.string(),
age: z.number(),
email: z.string(),
}),
},
});
// Stream partial objects
for await (const partial of stream.objectStream) {
console.log("Progress:", partial); // { name: "John" }, { name: "John", age: 30 }, ...
}
// Get final validated object
const user = await stream.object;
console.log("Final:", user); // { name: "John", age: 30, email: "john@example.com" }
### Tool Calls and Results
```typescript
const stream = await agent.stream("What's the weather in NYC?", {
tools: { weather: weatherTool }
});
// Monitor tool calls
const toolCalls = await stream.toolCalls;
const toolResults = await stream.toolResults;
console.log("Tools called:", toolCalls);
console.log("Results:", toolResults);
完全输出访问Direct link to 完全输出访问
🌐 Complete Output Access
const stream = await agent.stream("Analyze this data");
const output = await stream.getFullOutput();
console.log({
text: output.text,
usage: output.usage,
reasoning: output.reasoning,
finishReason: output.finishReason,
});
全流处理Direct link to 全流处理
🌐 Full Stream Processing
const stream = await agent.stream("Complex task");
for await (const chunk of stream.fullStream) {
switch (chunk.type) {
case "text-delta":
process.stdout.write(chunk.payload.text);
break;
case "tool-call":
console.log(`Calling ${chunk.payload.toolName}...`);
break;
case "reasoning-delta":
console.log(`Reasoning: ${chunk.payload.text}`);
break;
case "finish":
console.log(`Done! Reason: ${chunk.payload.stepResult.reason}`);
// Access response messages with any metadata added by output processors
const uiMessages = chunk.payload.response?.uiMessages;
if (uiMessages) {
console.log("Response messages:", uiMessages);
}
break;
}
}
错误处理Direct link to 错误处理
🌐 Error Handling
const stream = await agent.stream("Analyze this data");
try {
// Option 1: Handle errors in consumeStream
await stream.consumeStream({
onError: (error) => {
console.error("Stream error:", error);
},
});
const result = await stream.text;
} catch (error) {
console.error("Failed to get result:", error);
}
// Option 2: Check error property
const result = await stream.getFullOutput();
if (stream.error) {
console.error("Stream had errors:", stream.error);
}
相关类型Direct link to 相关类型
🌐 Related Types