Agent.streamUntilIdle()
加入版本: @mastra/core@1.29.0
streamUntilIdle() 已棄用。請改用帶有 untilIdle 選項的 stream():
const result = await agent.stream('Research solana for me', {
untilIdle: true,
memory: { thread: 't1', resource: 'u1' },
})
傳入 untilIdle: { maxIdleMs: 60_000 } 可配置閒置逾時。
streamUntilIdle() 會串流 Agent 的回應,並讓串流保持開啟,直至該次 run 期間分派的每個背景任務都完成。任務完成時,其結果會寫入記憶體,而 agentic loop 會自動重新進入,讓 LLM 可以作出回應。當沒有任務正在執行,亦沒有已排隊的完成事件時,串流便會關閉。
當 Agent 會分派背景任務(一般是長時間執行的 Tool 或子代理),而你希望單一串流涵蓋初始回應以及每個任務完成後觸發的所有後續執行時,可使用此方法。若 run 只包含前景操作,或你希望手動管理後續執行(手動提示 Agent 處理結果),請使用 Agent.stream()。
使用範例使用範例 的直接連結
const stream = await agent.streamUntilIdle('Research solana for me', {
memory: { thread: 't1', resource: 'u1' },
})
for await (const chunk of stream.fullStream) {
// chunks from the initial turn AND any continuation turns triggered by
// background task completions flow through here
}
streamUntilIdle() 同時需要 BackgroundTaskManager 及記憶體後端。如欠缺其中任何一項,它便會使用一般的 agent.stream() 呼叫。
參數參數 的直接連結
messages:
options?:
maxIdleMs?:
memory?:
structuredOutput?:
其他所有選項(maxSteps、modelSettings、toolChoice、outputProcessors、onFinish、onChunk 等)請參閱 Agent.stream() 參數。streamUntilIdle() 會將它們轉交給初始 turn。
傳回值傳回值 的直接連結
stream:
彙總屬性的注意事項彙總屬性的注意事項 的直接連結
streamUntilIdle() 會傳回第一個 turn 的 MastraModelOutput proxy。只有 fullStream 會被替換為涵蓋每個後續執行的合併串流。所有其他屬性(text、toolCalls、toolResults、finishReason、messageList 及 getFullOutput())均會根據第一個 turn 的內部 buffer 解析。
如需所有後續執行的彙總檢視,請自行取用 fullStream 並累積資料。
後續執行行為後續執行行為 的直接連結
在內部,streamUntilIdle() 會:
- 透過
agent.stream(...)執行初始 turn,並將其fullStream傳送至外層串流。 - 訂閱已解析記憶體範圍的背景任務完成事件。
- 將每個終止事件(
background-task-completed、background-task-failed、background-task-cancelled)排入佇列;當外層 wrapper 在各 turn 之間閒置時,以列出已完成toolCallId的指示再次叫用agent.stream([], ...)。後續 turn 會流入同一個外層串流。 - 當沒有任務正在執行,亦沒有已排隊的完成事件時,關閉外層串流。
延伸使用範例延伸使用範例 的直接連結
限制各 turn 之間的閒置時間限制各 turn 之間的閒置時間 的直接連結
const stream = await agent.streamUntilIdle('Kick off the long jobs', {
memory: { thread: 't1', resource: 'u1' },
maxIdleMs: 60_000, // close the stream after 1 minute of idleness between turns
})
for await (const chunk of stream.fullStream) {
if (chunk.type === 'background-task-completed') {
console.log('Task complete:', chunk.payload.taskId)
}
}
彙總所有後續執行的文字彙總所有後續執行的文字 的直接連結
const stream = await agent.streamUntilIdle('Research and summarize', {
memory: { thread: 't1', resource: 'u1' },
})
let fullText = ''
for await (const chunk of stream.fullStream) {
if (chunk.type === 'text-delta') {
fullText += chunk.payload.text
}
}