本檔深入瞭解 Microsoft Agent Framework 中工作流程的 事件 系統。
概觀
有內建事件可為工作流程執行提供可觀察性。
內建事件類型
// Workflow lifecycle events
WorkflowStartedEvent // Workflow execution begins
WorkflowOutputEvent // Workflow outputs data
WorkflowErrorEvent // Workflow encounters an error
WorkflowWarningEvent // Workflow encountered a warning
// Executor events
ExecutorInvokedEvent // Executor starts processing
ExecutorCompletedEvent // Executor finishes processing
ExecutorFailedEvent // Executor encounters an error
AgentRunResponseEvent // An agent run produces output
AgentRunUpdateEvent // An agent run produces a streaming update
// Superstep events
SuperStepStartedEvent // Superstep begins
SuperStepCompletedEvent // Superstep completes
// Request events
RequestInfoEvent // A request is issued
# Workflow lifecycle events
WorkflowStartedEvent # Workflow execution begins
WorkflowOutputEvent # Workflow produces an output
WorkflowErrorEvent # Workflow encounters an error
WorkflowWarningEvent # Workflow encountered a warning
# Executor events
ExecutorInvokedEvent # Executor starts processing
ExecutorCompletedEvent # Executor finishes processing
ExecutorFailedEvent # Executor encounters an error
AgentRunEvent # An agent run produces output
AgentRunUpdateEvent # An agent run produces a streaming update
# Superstep events
SuperStepStartedEvent # Superstep begins
SuperStepCompletedEvent # Superstep completes
# Request events
RequestInfoEvent # A request is issued
消費事件
using Microsoft.Agents.AI.Workflows;
await foreach (WorkflowEvent evt in run.WatchStreamAsync())
{
switch (evt)
{
case ExecutorInvokedEvent invoke:
Console.WriteLine($"Starting {invoke.ExecutorId}");
break;
case ExecutorCompletedEvent complete:
Console.WriteLine($"Completed {complete.ExecutorId}: {complete.Data}");
break;
case WorkflowOutputEvent output:
Console.WriteLine($"Workflow output: {output.Data}");
return;
case WorkflowErrorEvent error:
Console.WriteLine($"Workflow error: {error.Exception}");
return;
}
}
from agent_framework import (
ExecutorCompleteEvent,
ExecutorInvokeEvent,
WorkflowOutputEvent,
WorkflowErrorEvent,
)
async for event in workflow.run_stream(input_message):
match event:
case ExecutorInvokeEvent() as invoke:
print(f"Starting {invoke.executor_id}")
case ExecutorCompleteEvent() as complete:
print(f"Completed {complete.executor_id}: {complete.data}")
case WorkflowOutputEvent() as output:
print(f"Workflow produced output: {output.data}")
return
case WorkflowErrorEvent() as error:
print(f"Workflow error: {error.exception}")
return
自訂事件
使用者可以在工作流程執行期間定義和發出自訂事件,以增強可觀察性。
using Microsoft.Agents.AI.Workflows;
using Microsoft.Agents.AI.Workflows.Reflection;
internal sealed class CustomEvent(string message) : WorkflowEvent(message) { }
internal sealed class CustomExecutor() : ReflectingExecutor<CustomExecutor>("CustomExecutor"), IMessageHandler<string>
{
public async ValueTask HandleAsync(string message, IWorkflowContext context)
{
await context.AddEventAsync(new CustomEvent($"Processing message: {message}"));
// Executor logic...
}
}
from agent_framework import (
handler,
Executor,
WorkflowContext,
WorkflowEvent,
)
class CustomEvent(WorkflowEvent):
def __init__(self, message: str):
super().__init__(message)
class CustomExecutor(Executor):
@handler
async def handle(self, message: str, ctx: WorkflowContext[str]) -> None:
await ctx.add_event(CustomEvent(f"Processing message: {message}"))
# Executor logic...
後續步驟
- 了解如何在工作流程中使用代理程式 來建立智慧型工作流程。
- 瞭解如何使用工作流程作為代理程式。
- 瞭解如何在工作流程中處理要求和回應 。
- 瞭解如何在工作流程中管理狀態 。
- 學習如何建立檢查點並從中恢復。
- 學習如何監控工作流程。
- 了解工作流程中的狀態隔離。
- 學習如何視覺化工作流程。