mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
[BREAKING] Python: Refactor workflow events to unified discriminated union pattern (#3690)
* Refactor events * Merge main * Fixes * Cleanup * Update samples and tests * Remove unused imports * PR feedback * Merge main. Add properties for events to help typing * Formatting * Cleanup * use builtins.type to avoid shadowing by WorkflowEvent.type attribute * Final improvements
This commit is contained in:
committed by
GitHub
Unverified
parent
09f59b21ad
commit
0f3f4dbcaf
@@ -26,7 +26,6 @@ import logging
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
from agent_framework import RequestInfoEvent, WorkflowOutputEvent
|
||||
from agent_framework.azure import AzureOpenAIChatClient
|
||||
from agent_framework.declarative import (
|
||||
AgentExternalInputRequest,
|
||||
@@ -259,7 +258,7 @@ async def main() -> None:
|
||||
stream = workflow.run(user_input, stream=True)
|
||||
|
||||
async for event in stream:
|
||||
if isinstance(event, WorkflowOutputEvent):
|
||||
if event.type == "output":
|
||||
data = event.data
|
||||
source_id = getattr(event, "source_executor_id", "")
|
||||
|
||||
@@ -286,7 +285,7 @@ async def main() -> None:
|
||||
else:
|
||||
accumulated_response += str(data)
|
||||
|
||||
elif isinstance(event, RequestInfoEvent) and isinstance(event.data, AgentExternalInputRequest):
|
||||
elif event.type == "request_info" and isinstance(event.data, AgentExternalInputRequest):
|
||||
request = event.data
|
||||
|
||||
# The agent_response from the request contains the structured response
|
||||
|
||||
@@ -24,7 +24,6 @@ Usage:
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
|
||||
from agent_framework import WorkflowOutputEvent
|
||||
from agent_framework.azure import AzureOpenAIChatClient
|
||||
from agent_framework.declarative import WorkflowFactory
|
||||
from azure.identity import AzureCliCredential
|
||||
@@ -193,7 +192,7 @@ async def main() -> None:
|
||||
task = "What is the weather like in Seattle and how does it compare to the average for this time of year?"
|
||||
|
||||
async for event in workflow.run(task, stream=True):
|
||||
if isinstance(event, WorkflowOutputEvent):
|
||||
if event.type == "output":
|
||||
print(f"{event.data}", end="", flush=True)
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
|
||||
@@ -10,7 +10,7 @@ from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Annotated, Any
|
||||
|
||||
from agent_framework import FileCheckpointStorage, RequestInfoEvent, WorkflowOutputEvent, tool
|
||||
from agent_framework import FileCheckpointStorage, tool
|
||||
from agent_framework.azure import AzureOpenAIChatClient
|
||||
from agent_framework_declarative import ExternalInputRequest, ExternalInputResponse, WorkflowFactory
|
||||
from azure.identity import AzureCliCredential
|
||||
@@ -98,12 +98,12 @@ async def main():
|
||||
first_response = True
|
||||
|
||||
async for event in stream:
|
||||
if isinstance(event, WorkflowOutputEvent) and isinstance(event.data, str):
|
||||
if event.type == "output" and isinstance(event.data, str):
|
||||
if first_response:
|
||||
print("MenuAgent: ", end="")
|
||||
first_response = False
|
||||
print(event.data, end="", flush=True)
|
||||
elif isinstance(event, RequestInfoEvent) and isinstance(event.data, ExternalInputRequest):
|
||||
elif event.type == "request_info" and isinstance(event.data, ExternalInputRequest):
|
||||
pending_request_id = event.request_id
|
||||
|
||||
print()
|
||||
|
||||
@@ -15,7 +15,7 @@ In a production scenario, you would integrate with a real UI or chat interface.
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
|
||||
from agent_framework import Workflow, WorkflowOutputEvent
|
||||
from agent_framework import Workflow
|
||||
from agent_framework.declarative import ExternalInputRequest, WorkflowFactory
|
||||
from agent_framework_declarative._workflows._handlers import TextOutputEvent
|
||||
|
||||
@@ -27,7 +27,7 @@ async def run_with_streaming(workflow: Workflow) -> None:
|
||||
|
||||
async for event in workflow.run({}, stream=True):
|
||||
# WorkflowOutputEvent wraps the actual output data
|
||||
if isinstance(event, WorkflowOutputEvent):
|
||||
if event.type == "output":
|
||||
data = event.data
|
||||
if isinstance(data, TextOutputEvent):
|
||||
print(f"[Bot]: {data.text}")
|
||||
|
||||
@@ -15,7 +15,6 @@ Demonstrates sequential multi-agent pipeline:
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
|
||||
from agent_framework import WorkflowOutputEvent
|
||||
from agent_framework.azure import AzureOpenAIChatClient
|
||||
from agent_framework.declarative import WorkflowFactory
|
||||
from azure.identity import AzureCliCredential
|
||||
@@ -85,7 +84,7 @@ async def main() -> None:
|
||||
product = "An eco-friendly stainless steel water bottle that keeps drinks cold for 24 hours."
|
||||
|
||||
async for event in workflow.run(product, stream=True):
|
||||
if isinstance(event, WorkflowOutputEvent):
|
||||
if event.type == "output":
|
||||
print(f"{event.data}", end="", flush=True)
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
|
||||
@@ -22,7 +22,6 @@ Prerequisites:
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
|
||||
from agent_framework import WorkflowOutputEvent
|
||||
from agent_framework.azure import AzureOpenAIChatClient
|
||||
from agent_framework.declarative import WorkflowFactory
|
||||
from azure.identity import AzureCliCredential
|
||||
@@ -82,7 +81,7 @@ async def main() -> None:
|
||||
print("=" * 50)
|
||||
|
||||
async for event in workflow.run("How would you compute the value of PI?", stream=True):
|
||||
if isinstance(event, WorkflowOutputEvent):
|
||||
if event.type == "output":
|
||||
print(f"{event.data}", flush=True, end="")
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
|
||||
Reference in New Issue
Block a user