mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Durable Support for Workflows (#3630)
* Add workflow support for Azure Functions * fix compatability with latest framework changes and add integration tests * refactor code * remove white space Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * align help text with actual port used Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * replace instance id with a place holder Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * remove unused import Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * remove redundant typing import and fix SIM115 * fix latest breaking changes * fix mypy issues * clean up imports * define source marker strings as constants * fix json module name * refactor _extract_message_content_from_dict * refactor serialization * add helper method for error response construction and remove _extract_message_content_from_dict since it is not needed * use strict tpe checking for edges * change how duplicate agent registrations are handled * cancel approval_task on HITL timeout * update docstring * fix: align azurefunctions package with core API changes after rebase - State.import_state/export_state are now sync (removed await) - Add State.commit() before export_state() in activity execution - Rename executor parameter shared_state -> state - Rename ctx.set_shared_state/get_shared_state -> set_state/get_state (sync) - WorkflowBuilder now takes start_executor as constructor kwarg - Update WorkflowOutputEvent -> WorkflowEvent with type='output' - Update RequestInfoEvent -> WorkflowEvent[Any] - Update SharedState -> State in test imports - Update duplicate agent name tests to match new warning behavior - Update sample README API references * fix sample check errors * fix mypy issues * fix trailing white spaces * fix test imports * feat: add durable workflow samples and adapt to main branch changes - Add workflow samples 09-12 to 04-hosting/azure_functions/ - Adapt to ChatMessage -> Message rename from main - Adapt to pickle-based checkpoint encoding from main - Simplify _serialization.py to delegate to core encode/decode - Fix Message -> WorkflowMessage disambiguation in _context.py - Remove non-existent _checkpoint_summary import * fix: update create_checkpoint signature to match superclass * fix: correct relative link in HITL sample README * fix: resolve import breakage after rebase (State, DurableAgentThread, get_logger) --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot
Dmytro Struk
parent
9a369c69c0
commit
bb3d3c2efc
@@ -1317,5 +1317,129 @@ class TestAgentFunctionAppErrorPaths:
|
||||
assert app._coerce_to_bool([]) is False
|
||||
|
||||
|
||||
class TestAgentFunctionAppWorkflow:
|
||||
"""Test suite for AgentFunctionApp workflow support."""
|
||||
|
||||
def test_init_with_workflow_stores_workflow(self) -> None:
|
||||
"""Test that workflow is stored when provided."""
|
||||
mock_workflow = Mock()
|
||||
mock_workflow.executors = {}
|
||||
|
||||
with (
|
||||
patch.object(AgentFunctionApp, "_setup_executor_activity"),
|
||||
patch.object(AgentFunctionApp, "_setup_workflow_orchestration"),
|
||||
):
|
||||
app = AgentFunctionApp(workflow=mock_workflow)
|
||||
|
||||
assert app.workflow is mock_workflow
|
||||
|
||||
def test_init_with_workflow_extracts_agents(self) -> None:
|
||||
"""Test that agents are extracted from workflow executors."""
|
||||
from agent_framework import AgentExecutor
|
||||
|
||||
mock_agent = Mock()
|
||||
mock_agent.name = "WorkflowAgent"
|
||||
|
||||
mock_executor = Mock(spec=AgentExecutor)
|
||||
mock_executor.agent = mock_agent
|
||||
|
||||
mock_workflow = Mock()
|
||||
mock_workflow.executors = {"WorkflowAgent": mock_executor}
|
||||
|
||||
with (
|
||||
patch.object(AgentFunctionApp, "_setup_executor_activity"),
|
||||
patch.object(AgentFunctionApp, "_setup_workflow_orchestration"),
|
||||
patch.object(AgentFunctionApp, "_setup_agent_functions"),
|
||||
):
|
||||
app = AgentFunctionApp(workflow=mock_workflow)
|
||||
|
||||
assert "WorkflowAgent" in app.agents
|
||||
|
||||
def test_init_with_workflow_calls_setup_methods(self) -> None:
|
||||
"""Test that workflow setup methods are called."""
|
||||
mock_executor = Mock()
|
||||
mock_executor.id = "TestExecutor"
|
||||
|
||||
mock_workflow = Mock()
|
||||
# Include a non-AgentExecutor so _setup_executor_activity is called
|
||||
mock_workflow.executors = {"TestExecutor": mock_executor}
|
||||
|
||||
with (
|
||||
patch.object(AgentFunctionApp, "_setup_executor_activity") as setup_exec,
|
||||
patch.object(AgentFunctionApp, "_setup_workflow_orchestration") as setup_orch,
|
||||
):
|
||||
AgentFunctionApp(workflow=mock_workflow)
|
||||
|
||||
setup_exec.assert_called_once()
|
||||
setup_orch.assert_called_once()
|
||||
|
||||
def test_init_without_workflow_does_not_call_workflow_setup(self) -> None:
|
||||
"""Test that workflow setup is not called when no workflow provided."""
|
||||
mock_agent = Mock()
|
||||
mock_agent.name = "TestAgent"
|
||||
|
||||
with (
|
||||
patch.object(AgentFunctionApp, "_setup_executor_activity") as setup_exec,
|
||||
patch.object(AgentFunctionApp, "_setup_workflow_orchestration") as setup_orch,
|
||||
):
|
||||
AgentFunctionApp(agents=[mock_agent])
|
||||
|
||||
setup_exec.assert_not_called()
|
||||
setup_orch.assert_not_called()
|
||||
|
||||
def test_init_with_workflow_deduplicates_agents(self) -> None:
|
||||
"""Test that agents in both 'agents' and workflow are not double-registered."""
|
||||
from agent_framework import AgentExecutor
|
||||
|
||||
mock_agent = Mock()
|
||||
mock_agent.name = "SharedAgent"
|
||||
|
||||
mock_executor = Mock(spec=AgentExecutor)
|
||||
mock_executor.agent = mock_agent
|
||||
|
||||
mock_workflow = Mock()
|
||||
mock_workflow.executors = {"SharedAgent": mock_executor}
|
||||
|
||||
with (
|
||||
patch.object(AgentFunctionApp, "_setup_executor_activity"),
|
||||
patch.object(AgentFunctionApp, "_setup_workflow_orchestration"),
|
||||
patch.object(AgentFunctionApp, "_setup_agent_functions"),
|
||||
):
|
||||
# Same agent passed explicitly AND present in workflow — should not raise
|
||||
app = AgentFunctionApp(agents=[mock_agent], workflow=mock_workflow)
|
||||
|
||||
assert "SharedAgent" in app.agents
|
||||
|
||||
def test_build_status_url(self) -> None:
|
||||
"""Test _build_status_url constructs correct URL."""
|
||||
mock_workflow = Mock()
|
||||
mock_workflow.executors = {}
|
||||
|
||||
with (
|
||||
patch.object(AgentFunctionApp, "_setup_executor_activity"),
|
||||
patch.object(AgentFunctionApp, "_setup_workflow_orchestration"),
|
||||
):
|
||||
app = AgentFunctionApp(workflow=mock_workflow)
|
||||
|
||||
url = app._build_status_url("http://localhost:7071/api/workflow/run", "instance-123")
|
||||
|
||||
assert url == "http://localhost:7071/api/workflow/status/instance-123"
|
||||
|
||||
def test_build_status_url_handles_trailing_slash(self) -> None:
|
||||
"""Test _build_status_url handles URLs without /api/ correctly."""
|
||||
mock_workflow = Mock()
|
||||
mock_workflow.executors = {}
|
||||
|
||||
with (
|
||||
patch.object(AgentFunctionApp, "_setup_executor_activity"),
|
||||
patch.object(AgentFunctionApp, "_setup_workflow_orchestration"),
|
||||
):
|
||||
app = AgentFunctionApp(workflow=mock_workflow)
|
||||
|
||||
url = app._build_status_url("http://localhost:7071/", "instance-456")
|
||||
|
||||
assert "instance-456" in url
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v", "--tb=short"])
|
||||
|
||||
Reference in New Issue
Block a user