Python: Merge main into feature-durabletask-python branch (#3261)

* Python: Add factory pattern to concurrent orchestration builder (#2738)

* Add factory pattern to concurrent orchestration builder

* Update readme

* Address AI comments

* Fix unit tests

* Fix import

* Prevent multiple calls to set participants or factories

* Add comments

* Mitigate warnings

* Fix mypy

* Address comments

* Address Copilot comments

* Fix tests

* Python: fix: GroupChat ManagerSelectionResponse JSON Schema for OpenAI Structured Outpu… (#2750)

* fix: ManagerSelectionResponse JSON Schema for OpenAI Structured Output Strict Mode

* refactor: install pre-commit then commit again

* Capture file IDs from code interpreter in streaming responses (#2741)

* .NET: [BREAKING] Prevent nulls in AIAgent property (#2719)

* prevent nulls in AIAgent property

* address feedback

* code ql sm04598 (#2723)

Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>

* .NET: Add Conversation State Sample (Step05) (#2697)

* Initial plan

* Add Agent_OpenAI_Step05_Conversation sample for conversation state management

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* Update Program.cs comment to accurately describe the sample

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* Update the code to use the ConversationClient more in line with the samples in OpenAI

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Changing sample to use ChatClientAgent and conversationId in GetNewThread

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Bump AWSSDK.Extensions.Bedrock.MEAI from 4.0.4.7 to 4.0.4.11 (#2777)

---
updated-dependencies:
- dependency-name: AWSSDK.Extensions.Bedrock.MEAI
  dependency-version: 4.0.4.11
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump Azure.Identity from 1.17.0 to 1.17.1 (#2780)

---
updated-dependencies:
- dependency-name: Azure.Identity
  dependency-version: 1.17.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Azure.Identity
  dependency-version: 1.17.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Azure.Identity
  dependency-version: 1.17.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Azure.Identity
  dependency-version: 1.17.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump Azure.AI.AgentServer.AgentFramework from 1.0.0-beta.4 to 1.0.0-beta.5 (#2778)

---
updated-dependencies:
- dependency-name: Azure.AI.AgentServer.AgentFramework
  dependency-version: 1.0.0-beta.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Azure.AI.AgentServer.AgentFramework
  dependency-version: 1.0.0-beta.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Azure.AI.AgentServer.AgentFramework
  dependency-version: 1.0.0-beta.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Python: added more complete parsing for mcp tool arguments (#2756)

* added more complete parsing for mcp tool arguments

* fixed mypy

* added nonlocal model counter, and some fixes

* fixes in naming logic

* extracted json parsing function, added parametrized test and checked coverage

* Python: Updated package versions (#2784)

* Updated package versions

* Small fix

* Bump actions/checkout from 5 to 6 (#2404)

Bumps [actions/checkout](https://github.com/actions/checkout) from 5 to 6.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v5...v6)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* .NET: adds support for labels in edges,  fixes rendering of labels in dot a… (#1507)

* adds support for labels in edges,  fixes rendering of labels in dot and mermaid, adds rendering of labels in edges

* Update dotnet/src/Microsoft.Agents.AI.Workflows/Visualization/WorkflowVisualizer.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* escaping edge labels, adding tests for labels containing strange characters that would break the diagram and enabling the previous signature so the API has backwards compatibility.

* Unify label in EdgeData

* Edge API adjustments, removed useless "sanitizer"

* fixed test

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Jacob Alber <jaalber@microsoft.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* Python: Added custom args and thread object to ai_function kwargs (#2769)

* Added an example of using kwargs in ai_function

* Added thread object to ai_function kwargs

* Updated docs

* Small fix

* Added thread parameter filtering

* Fix WorkflowAgent to include thread convo history. Enable checkpointing. (#2774)

* Update OpenAIResponses.yaml to match AgentSchema (#2598)

1. Update `connection` child types --  `kind: ApiKey` to `kind: key` otherwise schema will fail: https://microsoft.github.io/AgentSchema/reference/apikeyconnection/

2.  Update `outputSchema`'s `PropertySchema` to be `kind` instead of `type` otherwise schema will fail: https://microsoft.github.io/AgentSchema/reference/propertyschema/

* Python: Remove warnings from workflow builder on not using factories (#2808)

* Revert concurrent

* Fix comments

* Python: Filter framework kwargs from MCP tool invocations (#2870)

* Filter framework kwargs from MCP tool invocations

* Fixes

* Python: Fix WorkflowAgent to emit yield_output as agent response (#2866)

* Fix WorkflowAgent to emit yield_output as agent response

* use raw_representation

* Raw representation handling

* Python: Use agent description in HandoffBuilder auto-generated tools (#2713) (#2714)

## Summary
Enhanced `HandoffBuilder._apply_auto_tools` to use the target agent's
description when creating handoff tools, providing more informative tool
descriptions for LLMs.

## Changes
- Modified `_apply_auto_tools` to extract `description` from
  `AgentExecutor._agent` when available
- Updated iteration to use `.items()` for more efficient dict traversal
- Handoff tools now use agent descriptions instead of generic placeholders

## Example
Before: "Handoff to the refund_agent agent."
After: "You handle refund requests. Ask for order details and process refunds."

## Testing
- All handoff tests pass (20/20)
- No breaking changes to existing API

Fixes #2713

Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>

* Python: [BREAKING] Observability updates (#2782)

* fixes Python: Add env_file_path parameter to setup_observability() similar to AzureOpenAIChatClient
Fixes #2186

* WIP on updates using configure_azure_monitor

* improved setup and clarity

* fixed root .env.example

* revert changes

* updated files

* updated sample

* updated zero code

* test fixes and fixed links

* fix devui

* removed planning docs

* added enable method and updated readme and samples

* clarified docstring

* add return annotation

* updated naming

* update capatilized version

* updated readme and some fixes

* updated decorator name inline with the rest

* feedback from comments addressed

* Python: Fix middleware terminate flag to exit function calling loop immediately (#2868)

* Fix middleware terminate flag to exit function calling loop immediately

* Eliminating duck typing

* Improve function exec result handling

* Fix race condition

* Fix mypy issues

* Python: Fix context duplication in handoff workflows when restoring from checkpoint (#2867)

* Fix context duplication in handoff workflows when restoring from checkpoint

* Address Copilot PR review

* .NET: Update to latest Azure.AI.*, OpenAI, and M.E.AI* (#2850)

* Update to latest Azure.AI.*, OpenAI, and M.E.AI*

Absorb breaking changes in Responses surface area

* Update dotnet/samples/AgentWebChat/AgentWebChat.AgentHost/Utilities/ChatClientExtensions.cs

* Update dotnet/samples/AgentWebChat/AgentWebChat.AgentHost/Utilities/ChatClientExtensions.cs

* Update dotnet/samples/AgentWebChat/AgentWebChat.AgentHost/Utilities/ChatClientExtensions.cs

* Update dotnet/samples/GettingStarted/AgentWithOpenAI/Agent_OpenAI_Step04_CreateFromOpenAIResponseClient/Program.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Using patch to remove the model is necessary, updated the response client to actually use the the ForAgent

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>

* Bump actions/download-artifact from 6 to 7 (#2862)

Bumps [actions/download-artifact](https://github.com/actions/download-artifact) from 6 to 7.
- [Release notes](https://github.com/actions/download-artifact/releases)
- [Commits](https://github.com/actions/download-artifact/compare/v6...v7)

---
updated-dependencies:
- dependency-name: actions/download-artifact
  dependency-version: '7'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump actions/cache from 4 to 5 (#2861)

Bumps [actions/cache](https://github.com/actions/cache) from 4 to 5.
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](https://github.com/actions/cache/compare/v4...v5)

---
updated-dependencies:
- dependency-name: actions/cache
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump actions/upload-artifact from 5 to 6 (#2860)

Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 5 to 6.
- [Release notes](https://github.com/actions/upload-artifact/releases)
- [Commits](https://github.com/actions/upload-artifact/compare/v5...v6)

---
updated-dependencies:
- dependency-name: actions/upload-artifact
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Python : Ollama Connector for Agent Framework (#1104)

* Initial Commit for Olama Connector

* Added Olama Sample

* Add Sample & Fixed Open Telemetry

* Fixed Spelling from Olama to Ollama

* remove"opentelemetry-semantic-conventions-ai ~=0.4.13" since its handled in a different pr

* Added Tool Calling

* Finalizing test cases

* Adjust samples to be more reliable

* Update python/packages/ollama/agent_framework_ollama/_chat_client.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update python/packages/ollama/pyproject.toml

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update python/packages/ollama/tests/test_ollama_chat_client.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update python/packages/ollama/agent_framework_ollama/_chat_client.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Improved Docstrings & Sample

* Update python/packages/ollama/agent_framework_ollama/_chat_client.py

Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>

* Integrate PR Feedback
- Divided Streaming and Non-Streaming into independent Methods
- Catch Ollama Validation Error
- Add OTEL Provider Name
- Checked Ollama Messages
- Add Usage Statistics

* Revert setting, so it can be none

* Validate Message formatting between AF and Ollama

* Catch Ollama Error and raise a ServiceResponse Error

* Fix mypy error

* remove .vscode comma

* Add Reasoning support & adjust to new structure

* Add Ollama Multimodality and Reasoning

* Add test cases for reasoning

* Add Tests for Error Handling in Ollama Client

* Update python/samples/getting_started/multimodal_input/ollama_chat_multimodal.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Integrated Copilot Feedback

* Implement first PR Feedback

* Adjust Readme files for examples

* Adjust argument passing via additional chat options

* Implemented PR Feedback

* Removing Ollama Package from Core and moving samples

* Fix Link & Adding Samples to Main Sample Readme

* Fixing Links in Readme

* Moved Multimodal and Chat Example

* Fixed Link in ChatClient to Ollama

* Fix AgentFramework Links in Ollama Project

* Fix observability breaking change

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>

* Skip failing IT (#2904)

* .NET: Cosmos DB UT Fast Skip (For Non-Configured Local envs) (#2906)

* Cosmos DB UT Fast Skip (Non-Configured Local envs) + Long running UT skip in pipeline when no CosmosDB changes happened

* Force a CosmosDB source code change to trigger the pipeline

* Address possible string boolean mismatch

* Add debug

* Enabling emulator always when running IT

* .NET: Add TTLs to durable agent sessions (#2679)

* .NET: Add TTLs to durable agent sessions

* Remove unnecessary async

* PR feedback: clarify UTC

* PR feedback: limit minimum signal delay to <= 5 minutes

* PR feedback: Fix TTL disablement

* Linter: use auto-property

* Fix build break from OpenAI SDK change

* Updated CHANGELOG.md

* PR feedback

* Reduce default TTL to 14 days to work around DTS bug

* Python:  Update Mem0Provider to use v2 search API `filters` parameter (#2766)

* short fix to move id parameters to filters object

* added tests

* small fix

* mem0 dependency update

* Updated package versions (#2913)

* .NET: Switch to new "Run" method name. (#2843)

* Switch to new "RunAgent" method name.

* Try to disable false positive naming warning.

* Add comment about disabled warnings.

* Rename `RunAgent` to just `Run`.

* Update CHANGELOG.

* Python: Switch to new "run" method name. (#2890)

* Switch to `run` method.

* Add support for deprecated `run_agent`.

* Fix entity method name.

* Fix method name and improve tests.

* Update comment.

* Update Python CHANGELOG.

* [BREAKING] Python: Add factory pattern to handoff orchestration builder (#2844)

* WIP: Factory pattern to handoff

* Add factory pattern to concurrent orchestration builder; Next: tests and sample verification

* Add tests and improve comments

* Fix mypy

* Simplify handoff_simple.py

* Simplify handoff_autonoumous.py and bug fix

* Update readme

* Address Copilot comments

* Python: Flow custom kwargs to agents via Workflow SharedState (#2894)

* Flow custom kwargs to agents via SharedState

* Address Copilot feedback

* Improve sample typing

* Fix test

* Fix Pydantic error when using Literal type for tool params (#2893)

* Updated Ollama package version (#2920)

* Python: Azure AI Agent with Bing Grounding Citations Sample (#2892)

* bing grounding sample with citations

* small fix

* fix

* .NET: Make DelegatingAIAgent abstract (#2797)

* Initial plan

* Make DelegatingAIAgent abstract

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Added additional arguments for Azure AI agent (#2922)

* Python: Correction of MCP image type conversion in  _mcp.py (#2901)

* Correction of MCP image type conversion in  _mcp.py

* Added a new overload to the init function of the DataContent() type of the Agent Framework, edited the test case to correctly test the usage of the data and uri fields while using DataContent()

* Fixed tests related to the changes of the DataContent type, added testing for both string and byte representations

* Pass kwargs into subworkflows (#2923)

* Python: Move ollama samples to samples getting started dir (#2921)

* Move ollama samples to samples getting started dir

* Address feedback

* Python: fix: correct BadRequestError when using Pydantic model in response_fo… (#1843)

* fix: correct BadRequestError when using Pydantic model in response_format

* Fix lint

---------

Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>

* .NET: [Breaking] Delete display name property (#2758)

* delete the AIAgent.DisplayName property

* use agent name as a first value for activity display name

* Update dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/HandoffAgentExecutor.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Python: cleanup and refactoring of chat clients (#2937)

* refactoring and unifying naming schemes of internal methods of chat clients

* set tool_choice to auto

* fix for mypy

* added note on naming and fix #2951

* fix responses

* fixes in azure ai agents client

* Python: Workflow add option to visualize internal executors (#2917)

* Workflow add option to visualize internal executors

* Address Copilot comments

* Python: Fixes Run ID and Thread ID casing to align with AG-UI Typescript SDK (#2948)

* added camelCase input to run id and thread id aligning with @ag-ui/core

* fixed per copilot suggestions

* Python: Add workflow cancellation sample (#2732)

* Add workflow cancellation sample

Add sample demonstrating how to cancel a running workflow using asyncio
tasks. Shows both cancellation mid-execution and normal completion paths.
Useful for implementing timeouts, graceful shutdown, or A2A executors.

* update docstring

* .NET: Update Anthropic package to version 12.0.0 (#2914)

* Initial plan

* Update Anthropic package to version 12.0.0

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

* Python: Add Azure Managed Redis Support with Credential Provider (#2887)

* azure redis support

* small fixes

* azure managed redis sample

* fixes

* Bump CommunityToolkit.Aspire.OllamaSharp from 13.0.0-beta.440 to 13.0.0 (#2856)

---
updated-dependencies:
- dependency-name: CommunityToolkit.Aspire.OllamaSharp
  dependency-version: 13.0.0
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump AWSSDK.Extensions.Bedrock.MEAI from 4.0.4.11 to 4.0.5 (#2853)

---
updated-dependencies:
- dependency-name: AWSSDK.Extensions.Bedrock.MEAI
  dependency-version: 4.0.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>

* Bump Azure.AI.AgentServer.AgentFramework from 1.0.0-beta.4 to 1.0.0-beta.5 (#2854)

---
updated-dependencies:
- dependency-name: Azure.AI.AgentServer.AgentFramework
  dependency-version: 1.0.0-beta.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Azure.AI.AgentServer.AgentFramework
  dependency-version: 1.0.0-beta.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* Python: Fix WorkflowAgent event handling and kwargs forwarding (#2946)

* Fix kwargs propagation through workflow.as_agent()

* Fix WorkflowAgent to respect AgentExecutor output_response setting

* .NET: Use GrpcEntityRunner instead of TaskEntityDispatcher (#2759)

* Use GrpcEntityRunner instead of TaskEntityDispatcher

* Pin to Durable worker 1.11.0

* Set the invocation result

* Update all Durable packages

* Update changelog, rename dispatcher to encondedEntityRequest

* Python: Bump Py version to 1.0.0b251218 for a release. Update CHANGELOG (#2968)

* Bump Py version to 1.0.0b251218 for a release. Update CHANGELOG

* update lock

* Fix formatting

* Fix ChatKit typing

* Python: Introducing Foundry Local Chat Clients (#2915)

* redo foundry local chat client

* fix mypy and spelling

* better docstring, updated sample

* fixed tests and added tests

* small sample update

* Updated package versions (#2978)

* Python: Added GitHub MCP sample with PAT (#2967)

* added github mcp sample with PAT

* addressed copilot fixes

* env fix

* Python: Preserve reasoning blocks with OpenRouter (#2950)

* Preserve reasoning blocks with OpenRouter

* Put encrypted reasoning in TextReasoningContent

* Remove unneccessary change

* Fix docs

* Support streaming

* Fix handling None in TextReasoningContent.text

* Python: Added response.created and response.in_progress event process to OpenAIBaseResponseClient (#2975)

* added response.created and response.in_progress to include response.id

* better doc string

* added tests for the new streaming event types

* Python: Introducing support for Bedrock-hosted models (Anthropic, Cohere, etc.) (#2610)

* Pushing the bedrock related changes to the new branch after addressing the review comments

* 2524 Addressed the second round review comments

* 2524 Addressed few more minor comments on the PR

* resolving the merge conflict

* 2524 resolved the uv.lock conflicts

* 2524 addressed more comments

* 2524 removed the print statement to fix the checks failure

* 2524 resolved the CI failure issues

* 2524 fixing the CI breaks

* 2524 Addressed the review comment

* 2524 resolved conflict

---------

Co-authored-by: Sunil Dutta <sunil.dutta@penske.com>
Co-authored-by: budgetboardingai <apurva.sharma31@gmail.com>

* .NET: [Durable Agents] Reliable streaming sample (#2942)

* .NET: [Durable Agents] Reliable streaming sample

* Add automated validation for new sample

* Address Copilot PR feedback

* Fix typo in README.md about agent definitions (#2634)

* Fix typo in README.md about agent definitions

* Update agent-samples/README.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Python: latency improvements (#3014)

* latency improvements

* fixed mypy, added coding standards and instructions

* slight logic improvement

* Python: Updated package versions (#3024)

* Updated package versions

* Updated changelog

* Python: add powerfx safe mode (#3028)

* add powerfx safe mode

* improved docstring and aligned env_file loading

* ensured test uses reset

* .NET: [Breaking] Introduce RunCoreAsync/RunCoreStreamingAsync delegation pattern in AIAgent (#2749)

* Initial plan

* Refactor AIAgent: Make RunAsync and RunStreamingAsync non-abstract, add RunCoreAsync and RunCoreStreamingAsync

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Fix infinite recursion in test implementations

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Make RunAsync and RunStreamingAsync non-virtual as requested

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Fix DelegatingAIAgent subclasses to use RunCoreAsync/RunCoreStreamingAsync

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Fix XML documentation references in AnonymousDelegatingAIAgent

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Restore <see cref> tags with proper qualified signatures in AnonymousDelegatingAIAgent

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Rollback unnecessary XML documentation changes in AnonymousDelegatingAIAgent

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Remove pragma and update crefs to RunCoreAsync/RunCoreStreamingAsync

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Fix EntityAgentWrapper to call base.RunCoreAsync/RunCoreStreamingAsync

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* fix compilation issues

* fix compilatio issue

* fix tests

* fix unit tests

* fix unit test

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <sergemenshikh@gmail.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* add issue template and additional labeling (#3006)

* fix and extra int test (#3037)

* .NET: [BREAKING] Refactor ChatMessageStore methods to be similar to AIContextProvider and add filtering support (#2604)

* Refactor ChatMessageStore methods to be similar to AIContextProvider

* Fix file encoding

* Ensure that AIContextProvider messages area also persisted.

* Update formatting and seal context classes

* Improve formatting

* Remove optional messages from constructor and add unit test

* Add ChatMessageStore filtering via a decorator

* Update sample and cosmos message store to store AIContextProvider messages in right order. Fix unit tests.

* Update Workflowmessage store to use aicontext provider messages.

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Improve xml docs messaging

* Address code review comments.

* Also notify message store on failure

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* [BREAKING] Remove unused AgentThreadMetadata (#3067)

* Remove unused AgentThreadMetadata

* Update DurableTask Changelog

* Python: Fix AzureAIClient failure when conversation history contains assistant messages (#3076)

* Fix AzureAIClient failure when conversation history contains assistant messages

* Address PR review feedback: improve docstring and test assertions

* Remove redundant cast

* Fix: Update OTLP exporter protocol conditions (#3070)

* Python: Fix ExecutorInvokedEvent and ExecutorCompletedEvent observability data (#3090)

* Fix ExecutorInvokedEvent.data mutation bug

* Fix bug related to not yielding output type

* .NET: Seal ChatClientAgentThread (#2842)

* Initial plan

* Seal ChatClientAgentThread class

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* Fix broken strands urls. (#3102)

* Fix broken strands urls.

* Fix typos

* .NET: Fix message ordering inconsistency when using AIContextProvider (#2659)

* Initial plan

* Fix message ordering inconsistency when using AIContextProvider

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

* Revert to original message ordering: Input, AIContextProvider, Response

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

* Reorder messages to ChatClient to match MessageStore order: Existing, Input, AIContextProvider

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

* Remove redundant test methods as existing tests already verify the behavior

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>
Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* fix: tool_choice parameter not being honored when passed to agent.run() (#3095)

* sharepoint sample fix (#3108)

* Bump versions to 1.0.0b260106 for a release. Update CHANGELOG.md (#3109)

* Bump Bedrock version to latest (#3110)

* Python: Fix MCP tool result serialization for list[TextContent] (#2523)

* Fix MCP tool result serialization for list[TextContent]

When MCP tools return results containing list[TextContent], they were
incorrectly serialized to object repr strings like:
'[<agent_framework._types.TextContent object at 0x...>]'

This fix properly extracts text content from list items by:
1. Checking if items have a 'text' attribute (TextContent)
2. Using model_dump() for items that support it
3. Falling back to str() for other types
4. Joining single items as plain text, multiple items as JSON array

Fixes #2509

* Address PR review feedback for MCP tool result serialization

- Extract serialize_content_result() to shared _utils.py
- Fix logic: use texts[0] instead of join for single item
- Add type annotation: texts: list[str] = []
- Return empty string for empty list instead of '[]'
- Move import json to file top level
- Add comprehensive unit tests for serialization

* Address PR review feedback: fix type checking and double serialization

- Add isinstance(item.text, str) check to ensure text attribute is a string
- Fix double-serialization issue by keeping model_dump results as dicts
  until final json.dumps (removes escaped JSON strings in arrays)
- Improve docstring with detailed return value documentation
- Add test for non-string text attribute handling
- Add tests for list type tool results in _events.py path

* Simplify PR: minimal changes to fix MCP tool result serialization

Addresses reviewer feedback about excessive refactoring:
- Reset _events.py to original structure
- Only add import and use serialize_content_result in one location
- All review comments addressed in serialize_content_result():
  - Added isinstance(item.text, str) check
  - Use model_dump(mode="json") to avoid double-serialization
  - Improved docstring with explicit return value documentation
  - Empty list returns "" instead of "[]"

* Refactor: Move MCP TextContent serialization to core prepare_function_call_results

Per reviewer feedback, moved the TextContent serialization logic from
ag-ui's serialize_content_result to the core package's
prepare_function_call_results function.

Changes:
- Added handling for objects with 'text' attribute (like MCP TextContent)
  in _prepare_function_call_results_as_dumpable
- Removed serialize_content_result from ag-ui/_utils.py
- Updated _events.py and _message_adapters.py to use
  prepare_function_call_results from core package
- Updated tests to match the core function's behavior

* Fix failing tests for prepare_function_call_results behavior

- test_tool_result_with_none: Update expected value to 'null' (JSON serialization of None)
- test_tool_result_with_model_dump_objects: Use Pydantic BaseModel instead of plain class

* Fix B903 linter error: Convert MockTextContent to dataclass

The ruff linter was reporting B903 (class could be dataclass or namedtuple)
for the MockTextContent test helper classes. This commit converts them to
dataclasses to satisfy the linter check.

* Python: Improve DevUI, add Context Inspector view as new tab under traces (#2742)

* Improve DevUI, add Context Inspector view as new tab under traces

* fix mypy errors

* fix: Handle stale MCP connections in DevUI executor

MCP tools can become stale when HTTP streaming responses end - the underlying
stdio streams close but `is_connected` remains True. This causes subsequent
requests to fail with `ClosedResourceError`.

Add `_ensure_mcp_connections()` to detect and reconnect stale MCP tools before
agent execution. This is a workaround for an upstream Agent Framework issue
where connection state isn't properly tracked.

Fixes MCP tools failing on second HTTP request in DevUI.

fixes  #1476 #1515 #2865

* fix #1572 report import dependency errors more clearly

* Ensure there is streaming toggle where users can select streaming vs non streaming mode in devui . Fixes .NET: [Python] DevUI tool call rendering in non-streaming mode?

* remove unused dead code

* improve ux - workflows with agents show a chat component in execution timelien, also ensure magentic final output shows correctly

* update ui build

* update devui to use instrumentation instead of tracing, other instrumentation and type/instance check fixes

* .NET: Seal factory contexts and add non JSO deserialize overloads (#3066)

* Seal factory contexts and add non JSO deserialize overloads

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Enable blank issues in issue template configuration

Need to re-enable creating blank issues

* updated templates (#3106)

* updated templates

* enabled blank and fixed triage

* made language optional and moved to the bottom for features

* Python: Streaming sample for azurefunctions (#3057)

* Streaming sample for azurefunctions

* Fixed links and sample name

* Addressed feedback

* Addressed feedback

* Fixed integration tests

* Updated test

* Python: fix(azure-ai): Fix response_format handling for structured outputs (#3114)

* fix(azure-ai): read response_format from chat_options instead of run_options

* refactor: use explicit None checks for response_format

* Fix mypy error

* Mypy fix

* Python: Bump python version to 1.0.0b260107 for a release (#3128)

* Bump python version to 1.0.0b260107 for a release

* Update changelog

* Make A2AAgent public, so that it's concrete implementation methods can be used. (#3119)

* .NET: Map additional props <-> A2A metadata (#3137)

* map additional props from agent run options to a2a request metadata

* small touches

* add unit tests for new extension methods

* Sort using

* add unit test

* add additiona unit tests

* special case json element to avoid unnecessary serialization

* Python: Fix Anthropic streaming response bugs (#3141)

* test commit identity

* fix(anthropic): fix raw_representation and finish_reason in streaming

* lint fix

* Bump AWSSDK.Extensions.Bedrock.MEAI from 4.0.5 to 4.0.5.1 (#2994)

---
updated-dependencies:
- dependency-name: AWSSDK.Extensions.Bedrock.MEAI
  dependency-version: 4.0.5.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* Bump Anthropic from 12.0.0 to 12.0.1 (#2993)

---
updated-dependencies:
- dependency-name: Anthropic
  dependency-version: 12.0.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* .NET: [Breaking] Prevent loss of input messages & streamed updates when resuming streaming (#2748)

* save input messages and stream updates to the continuation token to be able to use them in the last successful stream resumption call.

* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentContinuationToken.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentContinuationToken.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_BackgroundResponsesTests.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentContinuationToken.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentContinuationToken.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix typo

* init continuation token from chat response

* remove unnecessary types for source generation

* remove check for continuation token passed at initial run

* remove check for continuation token pass at initial run

* centralize continuation token parsing

* update xml comments

* use readonly collection instead of enumerable

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* .NET: fix: Expose WorkflowErrorEvent as ErrorContent (#2762)

* fix: Expose WorkflowErrorEvent as ErrorContent

When hosted using .AsAgent(), Workflows were not exposing inner errors coming as Exceptions (through the WorkflowErrorEvent)

The fix is to convert their message to an ErrorContent on the way out, rather than rely on the default "empty update" to collect the raw event.

* feat: Add a way to show/suppress exception information

* Bump Microsoft.Agents.AI.Workflows from 1.0.0-preview.251125.1 to 1.0.0-preview.251219.1 (#2997)

---
updated-dependencies:
- dependency-name: Microsoft.Agents.AI.Workflows
  dependency-version: 1.0.0-preview.251219.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>

* .NET: Add Run overloads to expose ChatClientAgentRunOptions in IntelliSense (#3115)

* Initial plan

* Add ChatClientAgentExtensions for improved discoverability of ChatClientAgentRunOptions

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

* Address code review feedback - use collection expression syntax

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

* Apply suggestion from @westey-m

* Fix issues with Copilot implementation

* Add additional tests for structured output overloads.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>

* Python: Add tool call/result content types and update connectors and samples (#2971)

* Add new AI content types and image tool support

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Add Python content types for tool calls/results and image generation tool support

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Address review feedback for tool content and samples

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Tighten image generation typing and sample tools list

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Align image generation output typing

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Handle MCP naming, image options mapping, and connector tool content

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Allow MCP call in function approval request

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Remove raw image_generation tool remapping

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Restore Anthropic tool_use to function calls unless code execution

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Fix lint issues for hosted file docstring and MCP parsing

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Import ChatResponse types in Anthropic client

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Fix Anthropics citation type imports and MCP typing for handoff/tools

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Skip lightning tests without agentlightning and fix function call import

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* fix lint on lab package

* rebuilt anthropic parsing

* redid anthropic parsing

* typo

* updated parsing and added missing docstrings

* fix tests

* mypy fixes

* second mypy fix

* add new class to other samples

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>
Co-authored-by: eavanvalkenburg <github@vanvalkenburg.eu>

* Bump Google.GenAI from 0.6.0 to 0.9.0 (#2995)

---
updated-dependencies:
- dependency-name: Google.GenAI
  dependency-version: 0.9.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>

* Bump js-yaml from 4.1.0 to 4.1.1 in /python/packages/devui/frontend (#3123)

Bumps [js-yaml](https://github.com/nodeca/js-yaml) from 4.1.0 to 4.1.1.
- [Changelog](https://github.com/nodeca/js-yaml/blob/master/CHANGELOG.md)
- [Commits](https://github.com/nodeca/js-yaml/compare/4.1.0...4.1.1)

---
updated-dependencies:
- dependency-name: js-yaml
  dependency-version: 4.1.1
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Updated package versions (#3144)

* .NET: Bump Microsoft.Agents.AI.OpenAI and Microsoft.Extensions.AI.OpenAI (#2996)

* Bump Microsoft.Agents.AI.OpenAI and Microsoft.Extensions.AI.OpenAI

Bumps Microsoft.Agents.AI.OpenAI from 1.0.0-preview.251125.1 to 1.0.0-preview.251219.1
Bumps Microsoft.Extensions.AI.OpenAI from 10.1.0-preview.1.25608.1 to 10.1.1-preview.1.25612.2

---
updated-dependencies:
- dependency-name: Microsoft.Agents.AI.OpenAI
  dependency-version: 1.0.0-preview.251219.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Microsoft.Extensions.AI.OpenAI
  dependency-version: 10.1.1-preview.1.25612.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Microsoft.Agents.AI.OpenAI
  dependency-version: 1.0.0-preview.251219.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: Microsoft.Extensions.AI.OpenAI
  dependency-version: 10.1.1-preview.1.25612.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* Fixed samples

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>

* Python: fix(ag-ui): Execute tools with approval_mode, fix shared state, code cleanup  (#3079)

* fix(ag-ui): execute tools after approval in human-in-the-loop flow

* Fix shared state bug

* Bug fix finalized

* Refactoring to clean up code

* Code cleanup

* More fixes

* More code cleanup

* Add version detection in __init__.py to ruff ignore list

* Track agent name with updates for workflow agent (#3146)

* Python: Fix AzureAIClient tool call bug for AG-UI use (#3148)

* Fiz AzureAIClient tool call bug

* Address copilot feedback

* Python: multiple bug fixes (#3150)

* fix Python: kwargs are not passed to _prepare_thread_and_messages in ChatAgent.run
Fixes #3118

* fix Python: [Bug]: model_id versus model_deployment_name is confusing in Azure AI Agents
Fixes #3147

* add types

* fixed type and docstring

* fix(anthropic): fix duplicate ToolCallStartEvent in streaming tool calls (#3051)

When processing `input_json_delta` events, the Anthropic client was
passing the tool name from the previous `tool_use` event. This caused
ag-ui's `_handle_function_call_content` to emit a `ToolCallStartEvent`
for every streaming chunk (since it triggers on `if content.name:`).

This fix changes the behavior to pass an empty string for `name` in
`input_json_delta` events, matching OpenAI's behavior where streaming
argument chunks have `name=""`. The initial `tool_use` event still
provides the tool name, so only one `ToolCallStartEvent` is emitted.

Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>

* .NET: [BREAKING] Change GetNewThread and DeserializeThread to async (#3152)

* Change GetNewThread and DeserializeThread plus ChatMessageStore and AIContextProvider Factories to async

* Merge fixes

* Fix Ollama model env var in documentation (#3156)

Signed-off-by: Dina Suehiro Jones <dina.s.jones@intel.com>

* Python: Add Pydantic request model and OpenAPI tags support to AG-UI FastAPI endpoint (#2522)

* feat(ag-ui): Add Pydantic request model and OpenAPI tags support

- Add AGUIRequest Pydantic model in _types.py with field descriptions
- Update add_agent_framework_fastapi_endpoint() to accept tags parameter
- Use AGUIRequest model for automatic validation and OpenAPI schema generation
- Export AGUIRequest and DEFAULT_TAGS in __init__.py
- Update test_endpoint.py to expect 422 for invalid requests
- Add tests for OpenAPI schema, default tags, custom tags, and validation

Benefits:
- Better API documentation with complete request schema in Swagger UI
- Automatic request validation with Pydantic
- Organized endpoints under 'AG-UI' tag instead of 'default'
- Improved developer experience and type safety

Fixes #<issue-number>

* test(ag-ui): Add test for internal error handling to achieve 100% coverage

- Add test_endpoint_internal_error_handling() to cover exception handling code
- Mock copy.deepcopy to simulate internal error during default_state processing
- Add type: ignore for FastAPI tags parameter (known pyright compatibility issue)
- Achieves 100% test coverage for _endpoint.py (previously missing lines 103-105)

* .NET: Improve resolving `AITool` from DI (#3175)

* remove localagenttoolregistry

* also give the factory method API

* Python: Fix MCPStreamableHTTPTool to use new streamable_http_client API (#3088)

* Fix MCPStreamableHTTPTool to use new streamable_http_client API with proper httpx client cleanup

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Update docstring to reflect new streamable_http_client API usage

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Refactor MCPStreamableHTTPTool to accept optional http_client parameter and delegate client creation to streamable_http_client

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Update mcp package minimum version to 1.24.0 for streamable_http_client API support

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Fix critical bugs: apply headers/timeout/sse_read_timeout when creating httpx client, add version constraint <2, and properly manage client lifecycle

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Simplify implementation: remove headers/timeout/sse_read_timeout params, remove kwargs, remove close() override per feedback

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Add back **kwargs parameter for backward compatibility (accepted but not used)

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Remove unused httpx import from test file

Note: The uv.lock file needs to be updated with 'uv sync' to reflect the mcp version constraint change (>=1.24.0,<2)

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* cicd fixes

* udpated samples with headers examples

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>
Co-authored-by: eavanvalkenburg <github@vanvalkenburg.eu>

* azureai direct a2a endpoint support (#3127)

* Python: [BREAKING]: removed display_name, renamed context_providers, middleware and AggregateContextProvider (#3139)

* removed display_name, renamed context_providers, middleware and AggregateContextProvider

* fixes

* fixed test

* testfix

* removed mistakenly put back test

* updated new test

* rename middlewares to middleware

* middleware fixes

* Python: MCP Improvements: improved connection loss behavior, pagination for loading and a param to control representation (#3154)

* pagination support (#2848) added a parse_tool_result param and connection loss (#2884)

* fix #3153

* improved connection handling

* improved logic

* Python: Add declarative workflow runtime (#2815)

* Further support for declarative python workflows

* Add tests. Clean up for typing and formatting

* Improvements and cleanup

* Typing cleanup. Improve docstrings

* Proper code in docstrings

* Fix malformed code-block directive in docstring

* Remove dead links

* PR feedback

* Address PR feedback

* Address PR feedback

* Remove sl

* Update devui frontend

* More cleanup

* Fix uv lock

* Skip Py 3.14 tests as powerfx doesn't support it

* Fix mypy error

* Fix for tool calls

* Removed stale docstring

* Fix lint

* Standardize on .NET namespaces. Revert DevUI changes (bring in later)

* Implement remaining items for Python declarative support to match dotnet

* point URL to agent, not to agentcard (#3176)

* Python: [BREAKING]: Introducing Options as TypedDict and Generic (#3140)

* WIP typeddict for options

* updated all clients and ChatAgents

* updated everything

* added ADR

* fix mypy

* proper typevar imports

* fixed import

* fixed other imports

* slight update in the sample

* updated from feedback

* fixes

* fixed missing covariants and test fixes

* fixed typing

* updated anthropic thinking config

* ruff fixes

* fixed int tests

* fix tests and mypy

* updated integration tests

* updated docstring and test fix

* improved options handling in obser

* mypy fix

* updated a host of integration tests

* fix tests

* bedrock fix

* [BREAKING] Python: Refactor orchestrations (#3023)

* Group chat refactoring Part 1; Next: HIL and handoff

* Add agent approval flow; next samples

* WIP: samples

* WIP: HIL samples

* Group chat HIL working; next: handoff

* Fix group chat tool approval sample

* WIP: refactor handoff; next handoff handling

* Handoff done; next handoff samples and concurrent and sequential

* Handoff samples, concurrent, and sequential done; next Magentic

* WIP: magentic; next test with samples + HIL

* Magentic Working; next fix all samples and tests

* Fix handoff samples; next tests

* WIP: fixing tests; some orchestration as agent samples are failing

* Group chat unit tests done

* Handoff  unit tests done

* Remove old orchestration_request_info and fix related tests

* Magentic unit tests done

* Fix samples

* Fix test

* Fix test 2

* mypy

* Address comments

* Update readme

* Address comments

* Address comments 2

* Replace display name

* Python: ADR for create/get agent API (#2618)

* ADR for create/get agent API

* Updated ADR with implementation options

* Small updates

* Updated decision outcome section

* Updated broken links

* Small updates

* Fixed merge conflicts

* Small fix

* Updated decision outcome section

* Small fixes

* Updated provider naming based on client SDK

* Add ignored parameter for CodeQL in workflow (#3204)

* Implement IReadOnlyList on InMemoryChatMessageStore (#3205)

* .NET: Make ChatMessageStore and AIContextProvider context props settable (#3196)

* Make ChatMessageStore and AIContextProvider context props setable

* Add validation to preserve non-null requirement of certain properties.

* Fix broken tests.

* Python: Add dependencies param to ag-ui FastAPI endpoint (#3191)

* Add dependencies param to ag-ui FastAPI endpoint

* Address Copilot feedback

* renamed all (#3207)

* Python: ADR for simplified get response (#3098)

* ADR for simplified get response

* updated some language, added agent option and code comparison

* small update in sample

* added workflows and expanded some points

* changed decision and number

* updated with stream=False default

* .NET: [Breaking] Rename`AgentRunResponse` and `AgentRunResponseUpdate` classes (#3197)

* rename AgentRunResponse and AgentRunResponseUpdate classes - part1

* rename varialbles, parameters, methods and tests

* rollback unnecessary changes

* .NET: [Breaking] Rename AgentRunResponseEvent and AgentRunUpdateEvent classes (#3214)

* rename AgentRunResponseEvent and AgentRunUpdateEvent classes

* rollback unnecessary changes

* Python: Create/Get Agent API for Azure V2 (#3059)

* Added get_agent method to Azure AI V2

* Small fixes

* Small fix

* Removed AzureAIAgentProvider

* Added create_agent method

* Small fixes

* Fixed code interpreter tool mapping

* Added agent provider for V2 client

* Updated response format handling

* Added provider example

* Fixed errors

* Update python/samples/getting_started/agents/azure_ai/README.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Small fix

* Updates from merge

* Resolved comments

* Resolved comments

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Python: Add more specific exceptions to Workflow (#3188)

* Add more specifc workflow exceptions

* Fix tests

* AI comments

* Misc

* Python: Added AzureAI sample for downloading code interpreter generated files (#3189)

* added azure ai code interpreter file download sample

* copilot fix suggestions

* function name fixes + readme update

* small fix

* update package versions (#3223)

Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>

* Python: fix(core): correct FunctionResultContent ordering in WorkflowAgent.merge_updates (#3168)

* fix(core): simplify FunctionResultContent ordering in WorkflowAgent.merge_updates

* improve comment

* Fix name

* fix(workflows): rename WorkflowOutputEvent.source_executor_id to executor_id for API consistency (#3166)

* Python: fix(ag-ui): add MCP tool support for AG-UI approval flows (#3212)

* add MCP tool support for AG-UI approval flows

* use attribute in place of property

* Python: Properly configure structured outputs based on new options dict (#3213)

* Properly configure structured outputs based on new options dict

* Fix mypy

* .NET: Merge AgentRunOptions.AdditionalProperties into ChatOptions.AdditionalProperties (#3184)

* Merge AgentRunOptions.AdditionalProperties into ChatOptions.AdditionalProperties

* Fix namespace and typo.

* .NET: Update Google.GenAI to 0.11.0 and remove polyfill implementations (#3232)

* Initial plan

* Update Google.GenAI to 0.11.0 and remove polyfill files

Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>

* .NET: [BREAKING] Renamed CreateAIAgent/GetAIAgent to AsAIAgent (#3222)

* Renamed chat client extension method

* Additional renaming

* Updated documentation

* Fixed tests

* Small fix

* Small fix

* Updated DurableAIAgent and fixed integration tests (#3241)

* Python: Create/Get Agent API for Azure V1 (#3192)

* Added provider implementation for Azure AI V1

* Small fixes

* Fixed OpenAPI example

* Fixed local MCP example

* Fixed hosted MCP example

* Fixed file search sample

* Small fixes

* Resolved comments

* Doc updates

* Bump azure-core from 1.37.0 to 1.38.0 in /python (#3209)

Bumps [azure-core](https://github.com/Azure/azure-sdk-for-python) from 1.37.0 to 1.38.0.
- [Release notes](https://github.com/Azure/azure-sdk-for-python/releases)
- [Commits](https://github.com/Azure/azure-sdk-for-python/compare/azure-core_1.37.0...azure-core_1.38.0)

---
updated-dependencies:
- dependency-name: azure-core
  dependency-version: 1.38.0
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Python: Create/Get Agent API for OpenAI Assistants (#3208)

* Added provider implementation

* Added example with response format

* Small improvements

* Python: (AG-UI) Support service-managed thread on AG-UI  (#3136)

* added service thread support

* set service_thread_id to only supplied_thread_id

* uses raw_representation to extract the conversation_id

* removed accidental edit

* updated test to use raw_representation

* resolves copilot review feedback

* revert back StubAgent, since not used

* removed relative module import

* removed hasattr check per PR feedback

* Create/Get Agent API - fixes and example improvements (#3246)

* Fix merge conflicts

---------

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Dina Suehiro Jones <dina.s.jones@intel.com>
Co-authored-by: Tao Chen <taochen@microsoft.com>
Co-authored-by: Kurt <65111699+q33566@users.noreply.github.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
Co-authored-by: Korolev Dmitry <deagle.gross@gmail.com>
Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>
Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
Co-authored-by: Jose Luis Latorre Millas <joslat@gmail.com>
Co-authored-by: Jacob Alber <jaalber@microsoft.com>
Co-authored-by: Richard Ortega <richardjortega@gmail.com>
Co-authored-by: 刘邦学AI <lbbniu@gmail.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
Co-authored-by: Nico Möller <nkm-moeller@mail.de>
Co-authored-by: Chris Gillum <cgillum@microsoft.com>
Co-authored-by: Giles Odigwe <79032838+giles17@users.noreply.github.com>
Co-authored-by: Phillip Hoff <phillip.hoff@gmail.com>
Co-authored-by: Ege Ozan Özyedek <36128615+egeozanozyedek@users.noreply.github.com>
Co-authored-by: samueljohnsiby <66901393+samueljohnsiby@users.noreply.github.com>
Co-authored-by: Evan Mattson <evan.mattson@microsoft.com>
Co-authored-by: Hao Luo <338265+howlowck@users.noreply.github.com>
Co-authored-by: Victor Dibia <chuvidi2003@gmail.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: Jacob Viau <javia@microsoft.com>
Co-authored-by: SuperKenVery <39673849+SuperKenVery@users.noreply.github.com>
Co-authored-by: Sunil Dutta <dutta.2003@gmail.com>
Co-authored-by: Sunil Dutta <sunil.dutta@penske.com>
Co-authored-by: budgetboardingai <apurva.sharma31@gmail.com>
Co-authored-by: Syrine Chelly <62653967+SyChell@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <sergemenshikh@gmail.com>
Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
Co-authored-by: takanori-terai <123897708+takanori-terai@users.noreply.github.com>
Co-authored-by: claude89757 <138977524+claude89757@users.noreply.github.com>
Co-authored-by: Gavin Aguiar <80794152+gavin-aguiar@users.noreply.github.com>
Co-authored-by: Sukeesh <vsukeeshbabu@gmail.com>
Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>
Co-authored-by: eavanvalkenburg <github@vanvalkenburg.eu>
Co-authored-by: Ao Chen <chenao3220@gmail.com>
Co-authored-by: Dina Suehiro Jones <dina.s.jones@intel.com>
This commit is contained in:
Laveesh Rohra
2026-01-16 16:59:49 -08:00
committed by GitHub
Unverified
parent 3df916064c
commit cd77193742
789 changed files with 43657 additions and 21320 deletions
@@ -0,0 +1,20 @@
# Copyright (c) Microsoft. All rights reserved.
"""Pytest configuration for declarative tests."""
import sys
import pytest
# Skip all tests in this directory on Python 3.14+ because powerfx doesn't support it yet
if sys.version_info >= (3, 14):
collect_ignore_glob = ["test_*.py"]
def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None:
"""Skip all declarative tests on Python 3.14+ due to powerfx incompatibility."""
if sys.version_info >= (3, 14):
skip_marker = pytest.mark.skip(reason="powerfx does not support Python 3.14+")
for item in items:
if "declarative" in str(item.fspath):
item.add_marker(skip_marker)
@@ -0,0 +1,348 @@
# Copyright (c) Microsoft. All rights reserved.
"""Tests for additional action handlers (conversation, variables, etc.)."""
import pytest
import agent_framework_declarative._workflows._actions_basic # noqa: F401
import agent_framework_declarative._workflows._actions_control_flow # noqa: F401
from agent_framework_declarative._workflows._handlers import get_action_handler
from agent_framework_declarative._workflows._state import WorkflowState
def create_action_context(action: dict, state: WorkflowState | None = None):
"""Create a minimal action context for testing."""
from agent_framework_declarative._workflows._handlers import ActionContext
if state is None:
state = WorkflowState()
async def execute_actions(actions, state):
for act in actions:
handler = get_action_handler(act.get("kind"))
if handler:
async for event in handler(
ActionContext(
state=state,
action=act,
execute_actions=execute_actions,
agents={},
bindings={},
)
):
yield event
return ActionContext(
state=state,
action=action,
execute_actions=execute_actions,
agents={},
bindings={},
)
class TestSetTextVariableHandler:
"""Tests for SetTextVariable action handler."""
@pytest.mark.asyncio
async def test_set_text_variable_simple(self):
"""Test setting a simple text variable."""
ctx = create_action_context({
"kind": "SetTextVariable",
"variable": "Local.greeting",
"value": "Hello, World!",
})
handler = get_action_handler("SetTextVariable")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.greeting") == "Hello, World!"
@pytest.mark.asyncio
async def test_set_text_variable_with_interpolation(self):
"""Test setting text with variable interpolation."""
state = WorkflowState()
state.set("Local.name", "Alice")
ctx = create_action_context(
{
"kind": "SetTextVariable",
"variable": "Local.message",
"value": "Hello, {Local.name}!",
},
state=state,
)
handler = get_action_handler("SetTextVariable")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.message") == "Hello, Alice!"
class TestResetVariableHandler:
"""Tests for ResetVariable action handler."""
@pytest.mark.asyncio
async def test_reset_variable(self):
"""Test resetting a variable to None."""
state = WorkflowState()
state.set("Local.counter", 5)
ctx = create_action_context(
{
"kind": "ResetVariable",
"variable": "Local.counter",
},
state=state,
)
handler = get_action_handler("ResetVariable")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.counter") is None
class TestSetMultipleVariablesHandler:
"""Tests for SetMultipleVariables action handler."""
@pytest.mark.asyncio
async def test_set_multiple_variables(self):
"""Test setting multiple variables at once."""
ctx = create_action_context({
"kind": "SetMultipleVariables",
"variables": [
{"variable": "Local.a", "value": 1},
{"variable": "Local.b", "value": 2},
{"variable": "Local.c", "value": "three"},
],
})
handler = get_action_handler("SetMultipleVariables")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.a") == 1
assert ctx.state.get("Local.b") == 2
assert ctx.state.get("Local.c") == "three"
class TestClearAllVariablesHandler:
"""Tests for ClearAllVariables action handler."""
@pytest.mark.asyncio
async def test_clear_all_variables(self):
"""Test clearing all turn-scoped variables."""
state = WorkflowState()
state.set("Local.a", 1)
state.set("Local.b", 2)
state.set("Workflow.Outputs.result", "kept")
ctx = create_action_context(
{
"kind": "ClearAllVariables",
},
state=state,
)
handler = get_action_handler("ClearAllVariables")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.a") is None
assert ctx.state.get("Local.b") is None
# Workflow outputs should be preserved
assert ctx.state.get("Workflow.Outputs.result") == "kept"
class TestCreateConversationHandler:
"""Tests for CreateConversation action handler."""
@pytest.mark.asyncio
async def test_create_conversation_with_output_binding(self):
"""Test creating a new conversation with output variable binding.
The conversationId field specifies the OUTPUT variable where the
auto-generated conversation ID is stored.
"""
ctx = create_action_context({
"kind": "CreateConversation",
"conversationId": "Local.myConvId", # Output variable
})
handler = get_action_handler("CreateConversation")
_events = [e async for e in handler(ctx)] # noqa: F841
# Check conversation was created with auto-generated ID
conversations = ctx.state.get("System.conversations")
assert conversations is not None
assert len(conversations) == 1
# Get the generated ID
generated_id = list(conversations.keys())[0]
assert conversations[generated_id]["messages"] == []
# Check output binding - the ID should be stored in the specified variable
assert ctx.state.get("Local.myConvId") == generated_id
@pytest.mark.asyncio
async def test_create_conversation_legacy_output(self):
"""Test creating a conversation with legacy output binding."""
ctx = create_action_context({
"kind": "CreateConversation",
"output": {
"conversationId": "Local.myConvId",
},
})
handler = get_action_handler("CreateConversation")
_events = [e async for e in handler(ctx)] # noqa: F841
# Check conversation was created
conversations = ctx.state.get("System.conversations")
assert conversations is not None
assert len(conversations) == 1
# Get the generated ID
generated_id = list(conversations.keys())[0]
# Check legacy output binding
assert ctx.state.get("Local.myConvId") == generated_id
@pytest.mark.asyncio
async def test_create_conversation_auto_id(self):
"""Test creating a conversation with auto-generated ID."""
ctx = create_action_context({
"kind": "CreateConversation",
})
handler = get_action_handler("CreateConversation")
_events = [e async for e in handler(ctx)] # noqa: F841
# Check conversation was created with some ID
conversations = ctx.state.get("System.conversations")
assert conversations is not None
assert len(conversations) == 1
class TestAddConversationMessageHandler:
"""Tests for AddConversationMessage action handler."""
@pytest.mark.asyncio
async def test_add_conversation_message(self):
"""Test adding a message to a conversation."""
state = WorkflowState()
state.set(
"System.conversations",
{
"conv-123": {"id": "conv-123", "messages": []},
},
)
ctx = create_action_context(
{
"kind": "AddConversationMessage",
"conversationId": "conv-123",
"message": {
"role": "user",
"content": "Hello!",
},
},
state=state,
)
handler = get_action_handler("AddConversationMessage")
_events = [e async for e in handler(ctx)] # noqa: F841
conversations = ctx.state.get("System.conversations")
assert len(conversations["conv-123"]["messages"]) == 1
assert conversations["conv-123"]["messages"][0]["content"] == "Hello!"
class TestEndWorkflowHandler:
"""Tests for EndWorkflow action handler."""
@pytest.mark.asyncio
async def test_end_workflow_signal(self):
"""Test that EndWorkflow emits correct signal."""
from agent_framework_declarative._workflows._actions_control_flow import EndWorkflowSignal
ctx = create_action_context({
"kind": "EndWorkflow",
"reason": "Completed successfully",
})
handler = get_action_handler("EndWorkflow")
events = [e async for e in handler(ctx)]
assert len(events) == 1
assert isinstance(events[0], EndWorkflowSignal)
assert events[0].reason == "Completed successfully"
class TestEndConversationHandler:
"""Tests for EndConversation action handler."""
@pytest.mark.asyncio
async def test_end_conversation_signal(self):
"""Test that EndConversation emits correct signal."""
from agent_framework_declarative._workflows._actions_control_flow import EndConversationSignal
ctx = create_action_context({
"kind": "EndConversation",
"conversationId": "conv-123",
})
handler = get_action_handler("EndConversation")
events = [e async for e in handler(ctx)]
assert len(events) == 1
assert isinstance(events[0], EndConversationSignal)
assert events[0].conversation_id == "conv-123"
class TestConditionGroupWithElseActions:
"""Tests for ConditionGroup with elseActions."""
@pytest.mark.asyncio
async def test_condition_group_else_actions(self):
"""Test that elseActions execute when no condition matches."""
ctx = create_action_context({
"kind": "ConditionGroup",
"conditions": [
{
"condition": False,
"actions": [
{"kind": "SetValue", "path": "Local.result", "value": "matched"},
],
},
],
"elseActions": [
{"kind": "SetValue", "path": "Local.result", "value": "else"},
],
})
handler = get_action_handler("ConditionGroup")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.result") == "else"
@pytest.mark.asyncio
async def test_condition_group_match_skips_else(self):
"""Test that elseActions don't execute when a condition matches."""
ctx = create_action_context({
"kind": "ConditionGroup",
"conditions": [
{
"condition": True,
"actions": [
{"kind": "SetValue", "path": "Local.result", "value": "matched"},
],
},
],
"elseActions": [
{"kind": "SetValue", "path": "Local.result", "value": "else"},
],
})
handler = get_action_handler("ConditionGroup")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.result") == "matched"
@@ -39,6 +39,13 @@ from agent_framework_declarative._models import (
pytestmark = pytest.mark.skipif(sys.version_info >= (3, 14), reason="Skipping on Python 3.14+")
try:
import powerfx # noqa: F401
_powerfx_available = True
except (ImportError, RuntimeError):
_powerfx_available = False
@pytest.mark.parametrize(
"yaml_content,expected_type,expected_attributes",
@@ -456,6 +463,98 @@ def test_agent_schema_dispatch_agent_samples(yaml_file: Path, agent_samples_dir:
assert result is not None, f"agent_schema_dispatch returned None for {yaml_file.relative_to(agent_samples_dir)}"
class TestAgentFactoryCreateFromDict:
"""Tests for AgentFactory.create_agent_from_dict method."""
def test_create_agent_from_dict_parses_prompt_agent(self):
"""Test that create_agent_from_dict correctly parses a PromptAgent definition."""
from unittest.mock import MagicMock
from agent_framework_declarative import AgentFactory
agent_def = {
"kind": "Prompt",
"name": "TestAgent",
"description": "A test agent",
"instructions": "You are a helpful assistant.",
}
# Use a pre-configured chat client to avoid needing model
mock_client = MagicMock()
mock_client.create_agent.return_value = MagicMock()
factory = AgentFactory(chat_client=mock_client)
agent = factory.create_agent_from_dict(agent_def)
assert agent is not None
def test_create_agent_from_dict_matches_yaml(self):
"""Test that create_agent_from_dict produces same result as create_agent_from_yaml."""
from unittest.mock import MagicMock
from agent_framework_declarative import AgentFactory
yaml_content = """
kind: Prompt
name: TestAgent
description: A test agent
instructions: You are a helpful assistant.
"""
agent_def = {
"kind": "Prompt",
"name": "TestAgent",
"description": "A test agent",
"instructions": "You are a helpful assistant.",
}
# Use a pre-configured chat client to avoid needing model
mock_client = MagicMock()
mock_client.create_agent.return_value = MagicMock()
factory = AgentFactory(chat_client=mock_client)
# Create from YAML string
agent_from_yaml = factory.create_agent_from_yaml(yaml_content)
# Create from dict
agent_from_dict = factory.create_agent_from_dict(agent_def)
# Both should produce agents with same name
assert agent_from_yaml.name == agent_from_dict.name
assert agent_from_yaml.description == agent_from_dict.description
def test_create_agent_from_dict_invalid_kind_raises(self):
"""Test that non-PromptAgent kind raises DeclarativeLoaderError."""
from agent_framework_declarative import AgentFactory
from agent_framework_declarative._loader import DeclarativeLoaderError
# Resource kind (not PromptAgent)
agent_def = {
"kind": "Resource",
"name": "TestResource",
}
factory = AgentFactory()
with pytest.raises(DeclarativeLoaderError, match="Only definitions for a PromptAgent are supported"):
factory.create_agent_from_dict(agent_def)
def test_create_agent_from_dict_without_model_or_client_raises(self):
"""Test that missing both model and chat_client raises DeclarativeLoaderError."""
from agent_framework_declarative import AgentFactory
from agent_framework_declarative._loader import DeclarativeLoaderError
agent_def = {
"kind": "Prompt",
"name": "TestAgent",
"instructions": "You are helpful.",
}
factory = AgentFactory()
with pytest.raises(DeclarativeLoaderError, match="ChatClient must be provided"):
factory.create_agent_from_dict(agent_def)
class TestAgentFactorySafeMode:
"""Tests for AgentFactory safe_mode parameter."""
@@ -499,6 +598,7 @@ instructions: Hello world
# The description should NOT be resolved from env (PowerFx fails, returns original)
assert agent.description == "=Env.TEST_DESCRIPTION"
@pytest.mark.skipif(not _powerfx_available, reason="PowerFx engine not available")
def test_agent_factory_safe_mode_false_allows_env_in_yaml(self, monkeypatch):
"""Test that safe_mode=False allows environment variable access in YAML parsing."""
from unittest.mock import MagicMock
@@ -558,6 +658,7 @@ model:
finally:
_safe_mode_context.reset(token)
@pytest.mark.skipif(not _powerfx_available, reason="PowerFx engine not available")
def test_agent_factory_safe_mode_false_resolves_api_key(self, monkeypatch):
"""Test safe_mode=False resolves API key from environment."""
from agent_framework_declarative._models import _safe_mode_context
@@ -839,6 +839,16 @@ class TestEnvironmentVariable:
assert env_var.value == "secret123"
# Check if PowerFx is available
try:
from powerfx import Engine as _PfxEngine
_PfxEngine()
_powerfx_available = True
except (ImportError, RuntimeError):
_powerfx_available = False
class TestTryPowerfxEval:
"""Tests for _try_powerfx_eval function."""
@@ -856,6 +866,7 @@ class TestTryPowerfxEval:
"""Test that empty strings are returned as empty."""
assert _try_powerfx_eval("") == ""
@pytest.mark.skipif(not _powerfx_available, reason="PowerFx engine not available")
def test_simple_powerfx_expressions(self):
"""Test simple PowerFx expressions."""
from decimal import Decimal
@@ -868,6 +879,7 @@ class TestTryPowerfxEval:
assert _try_powerfx_eval('="hello"') == "hello"
assert _try_powerfx_eval('="test value"') == "test value"
@pytest.mark.skipif(not _powerfx_available, reason="PowerFx engine not available")
def test_env_variable_access(self, monkeypatch):
"""Test accessing environment variables using =Env.<name> pattern."""
# Set up test environment variables
@@ -885,6 +897,7 @@ class TestTryPowerfxEval:
finally:
_safe_mode_context.reset(token)
@pytest.mark.skipif(not _powerfx_available, reason="PowerFx engine not available")
def test_env_variable_with_string_concatenation(self, monkeypatch):
"""Test env variables with string concatenation operator."""
monkeypatch.setenv("BASE_URL", "https://api.example.com")
@@ -903,6 +916,7 @@ class TestTryPowerfxEval:
finally:
_safe_mode_context.reset(token)
@pytest.mark.skipif(not _powerfx_available, reason="PowerFx engine not available")
def test_string_comparison_operators(self, monkeypatch):
"""Test PowerFx string comparison operators."""
monkeypatch.setenv("ENV_MODE", "production")
@@ -920,6 +934,7 @@ class TestTryPowerfxEval:
finally:
_safe_mode_context.reset(token)
@pytest.mark.skipif(not _powerfx_available, reason="PowerFx engine not available")
def test_string_in_operator(self):
"""Test PowerFx 'in' operator for substring testing (case-insensitive)."""
# Substring test - case insensitive - returns bool
@@ -927,6 +942,7 @@ class TestTryPowerfxEval:
assert _try_powerfx_eval('="THE" in "The keyboard and the monitor"') is True
assert _try_powerfx_eval('="xyz" in "The keyboard and the monitor"') is False
@pytest.mark.skipif(not _powerfx_available, reason="PowerFx engine not available")
def test_string_exactin_operator(self):
"""Test PowerFx 'exactin' operator for substring testing (case-sensitive)."""
# Substring test - case sensitive - returns bool
@@ -934,6 +950,7 @@ class TestTryPowerfxEval:
assert _try_powerfx_eval('="windows" exactin "To display windows in the Windows operating system"') is True
assert _try_powerfx_eval('="WINDOWS" exactin "To display windows in the Windows operating system"') is False
@pytest.mark.skipif(not _powerfx_available, reason="PowerFx engine not available")
def test_logical_operators_with_strings(self):
"""Test PowerFx logical operators (And, Or, Not) with string comparisons."""
# And operator - returns bool
@@ -957,6 +974,7 @@ class TestTryPowerfxEval:
# ! operator (alternative syntax) - returns bool
assert _try_powerfx_eval('=!("a" = "b")') is True
@pytest.mark.skipif(not _powerfx_available, reason="PowerFx engine not available")
def test_parentheses_for_precedence(self):
"""Test using parentheses to control operator precedence."""
from decimal import Decimal
@@ -969,6 +987,7 @@ class TestTryPowerfxEval:
result = _try_powerfx_eval('=("a" = "a" Or "b" = "c") And "d" = "d"')
assert result is True
@pytest.mark.skipif(not _powerfx_available, reason="PowerFx engine not available")
def test_env_with_special_characters(self, monkeypatch):
"""Test env variables containing special characters in values."""
monkeypatch.setenv("URL_WITH_QUERY", "https://example.com?param=value")
@@ -999,6 +1018,7 @@ class TestTryPowerfxEval:
finally:
_safe_mode_context.reset(token)
@pytest.mark.skipif(not _powerfx_available, reason="PowerFx engine not available")
def test_safe_mode_context_isolation(self, monkeypatch):
"""Test that safe_mode context variable properly isolates env access."""
monkeypatch.setenv("TEST_VAR", "test_value")
@@ -0,0 +1,286 @@
# Copyright (c) Microsoft. All rights reserved.
"""Tests for human-in-the-loop action handlers."""
import pytest
from agent_framework_declarative._workflows._handlers import ActionContext, get_action_handler
from agent_framework_declarative._workflows._human_input import (
QuestionRequest,
process_external_loop,
validate_input_response,
)
from agent_framework_declarative._workflows._state import WorkflowState
def create_action_context(action: dict, state: WorkflowState | None = None):
"""Create a minimal action context for testing."""
if state is None:
state = WorkflowState()
async def execute_actions(actions, state):
for act in actions:
handler = get_action_handler(act.get("kind"))
if handler:
async for event in handler(
ActionContext(
state=state,
action=act,
execute_actions=execute_actions,
agents={},
bindings={},
)
):
yield event
return ActionContext(
state=state,
action=action,
execute_actions=execute_actions,
agents={},
bindings={},
)
class TestQuestionHandler:
"""Tests for Question action handler."""
@pytest.mark.asyncio
async def test_question_emits_request_info_event(self):
"""Test that Question handler emits QuestionRequest."""
ctx = create_action_context({
"kind": "Question",
"id": "ask_name",
"variable": "Local.userName",
"prompt": "What is your name?",
})
handler = get_action_handler("Question")
events = [e async for e in handler(ctx)]
assert len(events) == 1
assert isinstance(events[0], QuestionRequest)
assert events[0].request_id == "ask_name"
assert events[0].prompt == "What is your name?"
assert events[0].variable == "Local.userName"
@pytest.mark.asyncio
async def test_question_with_choices(self):
"""Test Question with multiple choice options."""
ctx = create_action_context({
"kind": "Question",
"id": "ask_choice",
"variable": "Local.selection",
"prompt": "Select an option:",
"choices": ["Option A", "Option B", "Option C"],
"default": "Option A",
})
handler = get_action_handler("Question")
events = [e async for e in handler(ctx)]
assert len(events) == 1
event = events[0]
assert isinstance(event, QuestionRequest)
assert event.choices == ["Option A", "Option B", "Option C"]
assert event.default_value == "Option A"
@pytest.mark.asyncio
async def test_question_with_validation(self):
"""Test Question with validation rules."""
ctx = create_action_context({
"kind": "Question",
"id": "ask_email",
"variable": "Local.email",
"prompt": "Enter your email:",
"validation": {
"required": True,
"pattern": r"^[\w\.-]+@[\w\.-]+\.\w+$",
},
})
handler = get_action_handler("Question")
events = [e async for e in handler(ctx)]
assert len(events) == 1
event = events[0]
assert event.validation == {
"required": True,
"pattern": r"^[\w\.-]+@[\w\.-]+\.\w+$",
}
class TestRequestExternalInputHandler:
"""Tests for RequestExternalInput action handler."""
@pytest.mark.asyncio
async def test_request_external_input(self):
"""Test RequestExternalInput handler emits event."""
ctx = create_action_context({
"kind": "RequestExternalInput",
"id": "get_approval",
"variable": "Local.approval",
"prompt": "Please approve or reject",
"timeout": 300,
})
handler = get_action_handler("RequestExternalInput")
events = [e async for e in handler(ctx)]
assert len(events) == 1
event = events[0]
assert isinstance(event, QuestionRequest)
assert event.request_id == "get_approval"
assert event.variable == "Local.approval"
assert event.validation == {"timeout": 300}
class TestWaitForInputHandler:
"""Tests for WaitForInput action handler."""
@pytest.mark.asyncio
async def test_wait_for_input(self):
"""Test WaitForInput handler."""
ctx = create_action_context({
"kind": "WaitForInput",
"id": "wait",
"variable": "Local.response",
"message": "Waiting...",
})
handler = get_action_handler("WaitForInput")
events = [e async for e in handler(ctx)]
assert len(events) == 1
event = events[0]
assert isinstance(event, QuestionRequest)
assert event.request_id == "wait"
assert event.prompt == "Waiting..."
class TestProcessExternalLoop:
"""Tests for process_external_loop helper function."""
def test_no_external_loop(self):
"""Test when no external loop is configured."""
state = WorkflowState()
result, expr = process_external_loop({}, state)
assert result is False
assert expr is None
def test_external_loop_true_condition(self):
"""Test when external loop condition evaluates to true."""
state = WorkflowState()
state.set("Local.isComplete", False)
input_config = {
"externalLoop": {
"when": "=!Local.isComplete",
},
}
result, expr = process_external_loop(input_config, state)
# !False = True, so loop should continue
assert result is True
assert expr == "=!Local.isComplete"
def test_external_loop_false_condition(self):
"""Test when external loop condition evaluates to false."""
state = WorkflowState()
state.set("Local.isComplete", True)
input_config = {
"externalLoop": {
"when": "=!Local.isComplete",
},
}
result, expr = process_external_loop(input_config, state)
# !True = False, so loop should stop
assert result is False
class TestValidateInputResponse:
"""Tests for validate_input_response helper function."""
def test_no_validation(self):
"""Test with no validation rules."""
is_valid, error = validate_input_response("any value", None)
assert is_valid is True
assert error is None
def test_required_valid(self):
"""Test required validation with valid value."""
is_valid, error = validate_input_response("value", {"required": True})
assert is_valid is True
assert error is None
def test_required_empty_string(self):
"""Test required validation with empty string."""
is_valid, error = validate_input_response("", {"required": True})
assert is_valid is False
assert "required" in error.lower()
def test_required_none(self):
"""Test required validation with None."""
is_valid, error = validate_input_response(None, {"required": True})
assert is_valid is False
assert "required" in error.lower()
def test_min_length_valid(self):
"""Test minLength validation with valid value."""
is_valid, error = validate_input_response("hello", {"minLength": 3})
assert is_valid is True
def test_min_length_invalid(self):
"""Test minLength validation with too short value."""
is_valid, error = validate_input_response("hi", {"minLength": 3})
assert is_valid is False
assert "minimum length" in error.lower()
def test_max_length_valid(self):
"""Test maxLength validation with valid value."""
is_valid, error = validate_input_response("hello", {"maxLength": 10})
assert is_valid is True
def test_max_length_invalid(self):
"""Test maxLength validation with too long value."""
is_valid, error = validate_input_response("hello world", {"maxLength": 5})
assert is_valid is False
assert "maximum length" in error.lower()
def test_min_value_valid(self):
"""Test min validation for numbers."""
is_valid, error = validate_input_response(10, {"min": 5})
assert is_valid is True
def test_min_value_invalid(self):
"""Test min validation with too small number."""
is_valid, error = validate_input_response(3, {"min": 5})
assert is_valid is False
assert "minimum value" in error.lower()
def test_max_value_valid(self):
"""Test max validation for numbers."""
is_valid, error = validate_input_response(5, {"max": 10})
assert is_valid is True
def test_max_value_invalid(self):
"""Test max validation with too large number."""
is_valid, error = validate_input_response(15, {"max": 10})
assert is_valid is False
assert "maximum value" in error.lower()
def test_pattern_valid(self):
"""Test pattern validation with matching value."""
is_valid, error = validate_input_response("test@example.com", {"pattern": r"^[\w\.-]+@[\w\.-]+\.\w+$"})
assert is_valid is True
def test_pattern_invalid(self):
"""Test pattern validation with non-matching value."""
is_valid, error = validate_input_response("not-an-email", {"pattern": r"^[\w\.-]+@[\w\.-]+\.\w+$"})
assert is_valid is False
assert "pattern" in error.lower()
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,332 @@
# Copyright (c) Microsoft. All rights reserved.
"""Integration tests for declarative workflows.
These tests verify:
- End-to-end workflow execution
- Checkpointing at action boundaries
- WorkflowFactory creating graph-based workflows
- Pause/resume capabilities
"""
import pytest
from agent_framework_declarative._workflows import (
ActionTrigger,
DeclarativeWorkflowBuilder,
)
from agent_framework_declarative._workflows._factory import WorkflowFactory
class TestGraphBasedWorkflowExecution:
"""Integration tests for graph-based workflow execution."""
@pytest.mark.asyncio
async def test_simple_sequential_workflow(self):
"""Test a simple sequential workflow with SendActivity actions."""
yaml_def = {
"name": "simple_workflow",
"actions": [
{"kind": "SendActivity", "id": "greet", "activity": {"text": "Hello!"}},
{"kind": "SetValue", "id": "set_count", "path": "Local.count", "value": 1},
{"kind": "SendActivity", "id": "done", "activity": {"text": "Done!"}},
],
}
builder = DeclarativeWorkflowBuilder(yaml_def)
workflow = builder.build()
# Run the workflow
events = await workflow.run(ActionTrigger())
# Verify outputs were produced
outputs = events.get_outputs()
assert "Hello!" in outputs
assert "Done!" in outputs
@pytest.mark.asyncio
async def test_workflow_with_conditional(self):
"""Test workflow with If conditional branching."""
yaml_def = {
"name": "conditional_workflow",
"actions": [
{"kind": "SetValue", "id": "set_flag", "path": "Local.flag", "value": True},
{
"kind": "If",
"id": "check_flag",
"condition": "=Local.flag",
"then": [
{"kind": "SendActivity", "id": "say_yes", "activity": {"text": "Flag is true!"}},
],
"else": [
{"kind": "SendActivity", "id": "say_no", "activity": {"text": "Flag is false!"}},
],
},
],
}
builder = DeclarativeWorkflowBuilder(yaml_def)
workflow = builder.build()
# Run the workflow
events = await workflow.run(ActionTrigger())
outputs = events.get_outputs()
# Should take the "then" branch since flag is True
assert "Flag is true!" in outputs
assert "Flag is false!" not in outputs
@pytest.mark.asyncio
async def test_workflow_with_foreach_loop(self):
"""Test workflow with Foreach loop."""
yaml_def = {
"name": "loop_workflow",
"actions": [
{"kind": "SetValue", "id": "set_items", "path": "Local.items", "value": ["a", "b", "c"]},
{
"kind": "Foreach",
"id": "process_items",
"itemsSource": "=Local.items",
"iteratorVariable": "Local.item",
"actions": [
{"kind": "SendActivity", "id": "show_item", "activity": {"text": "=Local.item"}},
],
},
],
}
builder = DeclarativeWorkflowBuilder(yaml_def)
workflow = builder.build()
# Run the workflow
events = await workflow.run(ActionTrigger())
outputs = events.get_outputs()
# Should output each item
assert "a" in outputs
assert "b" in outputs
assert "c" in outputs
@pytest.mark.asyncio
async def test_workflow_with_switch(self):
"""Test workflow with Switch/ConditionGroup."""
yaml_def = {
"name": "switch_workflow",
"actions": [
{"kind": "SetValue", "id": "set_level", "path": "Local.level", "value": 2},
{
"kind": "Switch",
"id": "check_level",
"conditions": [
{
"condition": "=Local.level = 1",
"actions": [
{"kind": "SendActivity", "id": "level_1", "activity": {"text": "Level 1"}},
],
},
{
"condition": "=Local.level = 2",
"actions": [
{"kind": "SendActivity", "id": "level_2", "activity": {"text": "Level 2"}},
],
},
],
"else": [
{"kind": "SendActivity", "id": "default", "activity": {"text": "Other level"}},
],
},
],
}
builder = DeclarativeWorkflowBuilder(yaml_def)
workflow = builder.build()
# Run the workflow
events = await workflow.run(ActionTrigger())
outputs = events.get_outputs()
# Should take the level 2 branch
assert "Level 2" in outputs
assert "Level 1" not in outputs
assert "Other level" not in outputs
class TestWorkflowFactory:
"""Tests for WorkflowFactory."""
def test_factory_creates_workflow(self):
"""Test creating workflow."""
factory = WorkflowFactory()
yaml_content = """
name: test_workflow
actions:
- kind: SendActivity
id: greet
activity:
text: "Hello from graph mode!"
- kind: SetValue
id: set_val
path: Local.result
value: 42
"""
workflow = factory.create_workflow_from_yaml(yaml_content)
assert workflow is not None
assert hasattr(workflow, "_declarative_agents")
@pytest.mark.asyncio
async def test_workflow_execution(self):
"""Test executing a workflow."""
factory = WorkflowFactory()
yaml_content = """
name: graph_execution_test
actions:
- kind: SendActivity
id: start
activity:
text: "Starting workflow"
- kind: SetValue
id: set_message
path: Local.message
value: "Hello World"
- kind: SendActivity
id: end
activity:
text: "Workflow complete"
"""
workflow = factory.create_workflow_from_yaml(yaml_content)
# Execute the workflow
events = await workflow.run(ActionTrigger())
outputs = events.get_outputs()
assert "Starting workflow" in outputs
assert "Workflow complete" in outputs
class TestGraphWorkflowCheckpointing:
"""Tests for checkpointing capabilities of graph-based workflows."""
def test_workflow_has_multiple_executors(self):
"""Test that graph-based workflow creates multiple executor nodes."""
yaml_def = {
"name": "multi_executor_workflow",
"actions": [
{"kind": "SetValue", "id": "step1", "path": "Local.a", "value": 1},
{"kind": "SetValue", "id": "step2", "path": "Local.b", "value": 2},
{"kind": "SetValue", "id": "step3", "path": "Local.c", "value": 3},
],
}
builder = DeclarativeWorkflowBuilder(yaml_def)
_workflow = builder.build() # noqa: F841
# Verify multiple executors were created
assert "step1" in builder._executors
assert "step2" in builder._executors
assert "step3" in builder._executors
assert len(builder._executors) == 3
def test_workflow_executor_connectivity(self):
"""Test that executors are properly connected in sequence."""
yaml_def = {
"name": "connected_workflow",
"actions": [
{"kind": "SendActivity", "id": "a", "activity": {"text": "A"}},
{"kind": "SendActivity", "id": "b", "activity": {"text": "B"}},
{"kind": "SendActivity", "id": "c", "activity": {"text": "C"}},
],
}
builder = DeclarativeWorkflowBuilder(yaml_def)
workflow = builder.build()
# Verify all executors exist
assert len(builder._executors) == 3
# Verify the workflow can be inspected
assert workflow is not None
class TestGraphWorkflowVisualization:
"""Tests for workflow visualization capabilities."""
def test_workflow_can_be_built(self):
"""Test that complex workflows can be built successfully."""
yaml_def = {
"name": "complex_workflow",
"actions": [
{"kind": "SendActivity", "id": "intro", "activity": {"text": "Starting"}},
{
"kind": "If",
"id": "branch",
"condition": "=true",
"then": [
{"kind": "SendActivity", "id": "then_msg", "activity": {"text": "Then branch"}},
],
"else": [
{"kind": "SendActivity", "id": "else_msg", "activity": {"text": "Else branch"}},
],
},
{"kind": "SendActivity", "id": "outro", "activity": {"text": "Done"}},
],
}
builder = DeclarativeWorkflowBuilder(yaml_def)
workflow = builder.build()
# Verify the workflow was built
assert workflow is not None
# Verify expected executors exist
# intro, branch_condition, then_msg, else_msg, branch_join, outro
assert "intro" in builder._executors
assert "then_msg" in builder._executors
assert "else_msg" in builder._executors
assert "outro" in builder._executors
class TestGraphWorkflowStateManagement:
"""Tests for state management across graph executor nodes."""
@pytest.mark.asyncio
async def test_state_persists_across_executors(self):
"""Test that state set in one executor is available in the next."""
yaml_def = {
"name": "state_test",
"actions": [
{"kind": "SetValue", "id": "set", "path": "Local.value", "value": "test_data"},
{"kind": "SendActivity", "id": "send", "activity": {"text": "=Local.value"}},
],
}
builder = DeclarativeWorkflowBuilder(yaml_def)
workflow = builder.build()
events = await workflow.run(ActionTrigger())
outputs = events.get_outputs()
# The SendActivity should have access to the value set by SetValue
assert "test_data" in outputs
@pytest.mark.asyncio
async def test_multiple_variables(self):
"""Test setting and using multiple variables."""
yaml_def = {
"name": "multi_var_test",
"actions": [
{"kind": "SetValue", "id": "set_a", "path": "Local.a", "value": "Hello"},
{"kind": "SetValue", "id": "set_b", "path": "Local.b", "value": "World"},
{"kind": "SendActivity", "id": "send", "activity": {"text": "=Local.a"}},
],
}
builder = DeclarativeWorkflowBuilder(yaml_def)
workflow = builder.build()
events = await workflow.run(ActionTrigger())
outputs = events.get_outputs()
assert "Hello" in outputs
@@ -0,0 +1,242 @@
# Copyright (c) Microsoft. All rights reserved.
"""Tests for custom PowerFx-like functions."""
from agent_framework_declarative._workflows._powerfx_functions import (
CUSTOM_FUNCTIONS,
assistant_message,
concat_text,
count_rows,
find,
first,
is_blank,
last,
lower,
message_text,
search_table,
system_message,
upper,
user_message,
)
class TestMessageText:
"""Tests for MessageText function."""
def test_message_text_from_string(self):
"""Test extracting text from a plain string."""
assert message_text("Hello") == "Hello"
def test_message_text_from_single_dict(self):
"""Test extracting text from a single message dict."""
msg = {"role": "assistant", "content": "Hello world"}
assert message_text(msg) == "Hello world"
def test_message_text_from_list(self):
"""Test extracting text from a list of messages."""
msgs = [
{"role": "user", "content": "Hi"},
{"role": "assistant", "content": "Hello"},
]
assert message_text(msgs) == "Hi Hello"
def test_message_text_from_none(self):
"""Test that None returns empty string."""
assert message_text(None) == ""
def test_message_text_empty_list(self):
"""Test that empty list returns empty string."""
assert message_text([]) == ""
class TestUserMessage:
"""Tests for UserMessage function."""
def test_user_message_creates_dict(self):
"""Test that UserMessage creates correct dict."""
msg = user_message("Hello")
assert msg == {"role": "user", "content": "Hello"}
def test_user_message_with_none(self):
"""Test UserMessage with None."""
msg = user_message(None)
assert msg == {"role": "user", "content": ""}
class TestAssistantMessage:
"""Tests for AssistantMessage function."""
def test_assistant_message_creates_dict(self):
"""Test that AssistantMessage creates correct dict."""
msg = assistant_message("Hello")
assert msg == {"role": "assistant", "content": "Hello"}
class TestSystemMessage:
"""Tests for SystemMessage function."""
def test_system_message_creates_dict(self):
"""Test that SystemMessage creates correct dict."""
msg = system_message("You are helpful")
assert msg == {"role": "system", "content": "You are helpful"}
class TestIsBlank:
"""Tests for IsBlank function."""
def test_is_blank_none(self):
"""Test that None is blank."""
assert is_blank(None) is True
def test_is_blank_empty_string(self):
"""Test that empty string is blank."""
assert is_blank("") is True
def test_is_blank_whitespace(self):
"""Test that whitespace-only string is blank."""
assert is_blank(" ") is True
def test_is_blank_empty_list(self):
"""Test that empty list is blank."""
assert is_blank([]) is True
def test_is_blank_non_empty(self):
"""Test that non-empty values are not blank."""
assert is_blank("hello") is False
assert is_blank([1, 2, 3]) is False
assert is_blank(0) is False
class TestCountRows:
"""Tests for CountRows function."""
def test_count_rows_list(self):
"""Test counting list items."""
assert count_rows([1, 2, 3]) == 3
def test_count_rows_empty(self):
"""Test counting empty list."""
assert count_rows([]) == 0
def test_count_rows_none(self):
"""Test counting None."""
assert count_rows(None) == 0
class TestFirstLast:
"""Tests for First and Last functions."""
def test_first_returns_first_item(self):
"""Test that First returns first item."""
assert first([1, 2, 3]) == 1
def test_last_returns_last_item(self):
"""Test that Last returns last item."""
assert last([1, 2, 3]) == 3
def test_first_empty_returns_none(self):
"""Test that First returns None for empty list."""
assert first([]) is None
def test_last_empty_returns_none(self):
"""Test that Last returns None for empty list."""
assert last([]) is None
class TestFind:
"""Tests for Find function."""
def test_find_substring(self):
"""Test finding a substring."""
result = find("world", "Hello world")
assert result == 7 # 1-based index
def test_find_not_found(self):
"""Test when substring not found - returns Blank (None) per PowerFx semantics."""
result = find("xyz", "Hello world")
assert result is None
def test_find_at_start(self):
"""Test finding at start of string."""
result = find("Hello", "Hello world")
assert result == 1
class TestUpperLower:
"""Tests for Upper and Lower functions."""
def test_upper(self):
"""Test uppercase conversion."""
assert upper("hello") == "HELLO"
def test_lower(self):
"""Test lowercase conversion."""
assert lower("HELLO") == "hello"
def test_upper_none(self):
"""Test upper with None."""
assert upper(None) == ""
class TestConcatText:
"""Tests for Concat function."""
def test_concat_simple_list(self):
"""Test concatenating simple list."""
assert concat_text(["a", "b", "c"], separator=", ") == "a, b, c"
def test_concat_with_field(self):
"""Test concatenating with field extraction."""
items = [{"name": "Alice"}, {"name": "Bob"}]
assert concat_text(items, field="name", separator=", ") == "Alice, Bob"
class TestSearchTable:
"""Tests for Search function."""
def test_search_finds_matching(self):
"""Test search finds matching items."""
items = [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
{"name": "Charlie", "age": 35},
]
result = search_table(items, "Bob", "name")
assert len(result) == 1
assert result[0]["name"] == "Bob"
def test_search_case_insensitive(self):
"""Test search is case insensitive."""
items = [{"name": "Alice"}]
result = search_table(items, "alice", "name")
assert len(result) == 1
def test_search_partial_match(self):
"""Test search finds partial matches."""
items = [{"name": "Alice Smith"}, {"name": "Bob Jones"}]
result = search_table(items, "Smith", "name")
assert len(result) == 1
class TestCustomFunctionsRegistry:
"""Tests for the CUSTOM_FUNCTIONS registry."""
def test_all_functions_registered(self):
"""Test that all functions are in the registry."""
expected = [
"MessageText",
"UserMessage",
"AssistantMessage",
"SystemMessage",
"IsBlank",
"CountRows",
"First",
"Last",
"Find",
"Upper",
"Lower",
"Concat",
"Search",
]
for name in expected:
assert name in CUSTOM_FUNCTIONS
@@ -0,0 +1,581 @@
# Copyright (c) Microsoft. All rights reserved.
"""Tests to ensure PowerFx evaluation supports all expressions used in declarative YAML workflows.
This test suite validates that all PowerFx expressions found in the sample YAML workflows
under samples/getting_started/workflows/declarative/ work correctly with our implementation.
Coverage includes:
- Built-in PowerFx functions: Concat, If, IsBlank, Not, Or, Upper, Find
- Custom functions: UserMessage, MessageText
- System variables: System.ConversationId, System.LastMessage.Text
- Local/turn variables with nested access
- Comparison operators: <, >, <=, >=, <>, =
- Logical operators: And, Or, Not, !
- Arithmetic operators: +, -, *, /
- String interpolation: {Variable.Path}
"""
from unittest.mock import AsyncMock, MagicMock
import pytest
from agent_framework_declarative._workflows._declarative_base import (
DeclarativeWorkflowState,
)
class TestPowerFxBuiltinFunctions:
"""Test PowerFx built-in functions used in YAML workflows."""
@pytest.fixture
def mock_shared_state(self):
"""Create a mock shared state with async get/set methods."""
shared_state = MagicMock()
shared_state._data = {}
async def mock_get(key):
if key not in shared_state._data:
raise KeyError(key)
return shared_state._data[key]
async def mock_set(key, value):
shared_state._data[key] = value
shared_state.get = AsyncMock(side_effect=mock_get)
shared_state.set = AsyncMock(side_effect=mock_set)
return shared_state
async def test_concat_simple(self, mock_shared_state):
"""Test Concat function with simple strings."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =Concat("Nice to meet you, ", Local.userName, "!")
await state.set("Local.userName", "Alice")
result = await state.eval('=Concat("Nice to meet you, ", Local.userName, "!")')
assert result == "Nice to meet you, Alice!"
async def test_concat_multiple_args(self, mock_shared_state):
"""Test Concat with multiple arguments."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =Concat(Local.greeting, ", ", Local.name, "!")
await state.set("Local.greeting", "Hello")
await state.set("Local.name", "World")
result = await state.eval('=Concat(Local.greeting, ", ", Local.name, "!")')
assert result == "Hello, World!"
async def test_concat_with_local_namespace(self, mock_shared_state):
"""Test Concat using Local.* namespace (maps to Local.*)."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =Concat("Starting math coaching session for: ", Local.Problem)
await state.set("Local.Problem", "2 + 2")
result = await state.eval('=Concat("Starting math coaching session for: ", Local.Problem)')
assert result == "Starting math coaching session for: 2 + 2"
async def test_if_with_isblank(self, mock_shared_state):
"""Test If function with IsBlank."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize({"name": ""})
# From YAML: =If(IsBlank(inputs.name), "World", inputs.name)
# When input is blank
result = await state.eval('=If(IsBlank(Workflow.Inputs.name), "World", Workflow.Inputs.name)')
assert result == "World"
# When input is provided
await state.initialize({"name": "Alice"})
result = await state.eval('=If(IsBlank(Workflow.Inputs.name), "World", Workflow.Inputs.name)')
assert result == "Alice"
async def test_not_function(self, mock_shared_state):
"""Test Not function."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =Not(Local.EscalationParameters.IsComplete)
await state.set("Local.EscalationParameters", {"IsComplete": False})
result = await state.eval("=Not(Local.EscalationParameters.IsComplete)")
assert result is True
await state.set("Local.EscalationParameters", {"IsComplete": True})
result = await state.eval("=Not(Local.EscalationParameters.IsComplete)")
assert result is False
async def test_or_function(self, mock_shared_state):
"""Test Or function."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =Or(Local.feeling = "great", Local.feeling = "good")
await state.set("Local.feeling", "great")
result = await state.eval('=Or(Local.feeling = "great", Local.feeling = "good")')
assert result is True
await state.set("Local.feeling", "good")
result = await state.eval('=Or(Local.feeling = "great", Local.feeling = "good")')
assert result is True
await state.set("Local.feeling", "bad")
result = await state.eval('=Or(Local.feeling = "great", Local.feeling = "good")')
assert result is False
async def test_upper_function(self, mock_shared_state):
"""Test Upper function."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =Upper(System.LastMessage.Text)
await state.set("System.LastMessage", {"Text": "hello world"})
result = await state.eval("=Upper(System.LastMessage.Text)")
assert result == "HELLO WORLD"
async def test_find_function(self, mock_shared_state):
"""Test Find function."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =!IsBlank(Find("CONGRATULATIONS", Upper(Local.TeacherResponse)))
await state.set("Local.TeacherResponse", "CONGRATULATIONS! You solved it!")
result = await state.eval('=Not(IsBlank(Find("CONGRATULATIONS", Upper(Local.TeacherResponse))))')
assert result is True
await state.set("Local.TeacherResponse", "Try again")
result = await state.eval('=Not(IsBlank(Find("CONGRATULATIONS", Upper(Local.TeacherResponse))))')
assert result is False
class TestPowerFxSystemVariables:
"""Test System.* variable access."""
@pytest.fixture
def mock_shared_state(self):
"""Create a mock shared state."""
shared_state = MagicMock()
shared_state._data = {}
async def mock_get(key):
if key not in shared_state._data:
raise KeyError(key)
return shared_state._data[key]
async def mock_set(key, value):
shared_state._data[key] = value
shared_state.get = AsyncMock(side_effect=mock_get)
shared_state.set = AsyncMock(side_effect=mock_set)
return shared_state
async def test_system_conversation_id(self, mock_shared_state):
"""Test System.ConversationId access."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: conversationId: =System.ConversationId
await state.set("System.ConversationId", "conv-12345")
result = await state.eval("=System.ConversationId")
assert result == "conv-12345"
async def test_system_last_message_text(self, mock_shared_state):
"""Test System.LastMessage.Text access."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =Upper(System.LastMessage.Text) <> "EXIT"
await state.set("System.LastMessage", {"Text": "Hello"})
result = await state.eval("=System.LastMessage.Text")
assert result == "Hello"
async def test_system_last_message_exit_check(self, mock_shared_state):
"""Test the exit check pattern from YAML."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: when: =Upper(System.LastMessage.Text) <> "EXIT"
await state.set("System.LastMessage", {"Text": "hello"})
result = await state.eval('=Upper(System.LastMessage.Text) <> "EXIT"')
assert result is True
await state.set("System.LastMessage", {"Text": "exit"})
result = await state.eval('=Upper(System.LastMessage.Text) <> "EXIT"')
assert result is False
class TestPowerFxComparisonOperators:
"""Test comparison operators used in YAML workflows."""
@pytest.fixture
def mock_shared_state(self):
"""Create a mock shared state."""
shared_state = MagicMock()
shared_state._data = {}
async def mock_get(key):
if key not in shared_state._data:
raise KeyError(key)
return shared_state._data[key]
async def mock_set(key, value):
shared_state._data[key] = value
shared_state.get = AsyncMock(side_effect=mock_get)
shared_state.set = AsyncMock(side_effect=mock_set)
return shared_state
async def test_less_than(self, mock_shared_state):
"""Test < operator."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: condition: =Local.age < 65
await state.set("Local.age", 30)
assert await state.eval("=Local.age < 65") is True
await state.set("Local.age", 70)
assert await state.eval("=Local.age < 65") is False
async def test_less_than_with_local(self, mock_shared_state):
"""Test < with Local namespace."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: condition: =Local.TurnCount < 4
await state.set("Local.TurnCount", 2)
assert await state.eval("=Local.TurnCount < 4") is True
await state.set("Local.TurnCount", 5)
assert await state.eval("=Local.TurnCount < 4") is False
async def test_equality(self, mock_shared_state):
"""Test = equality operator."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =Local.feeling = "great"
await state.set("Local.feeling", "great")
assert await state.eval('=Local.feeling = "great"') is True
await state.set("Local.feeling", "bad")
assert await state.eval('=Local.feeling = "great"') is False
async def test_inequality(self, mock_shared_state):
"""Test <> inequality operator."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =Upper(System.LastMessage.Text) <> "EXIT"
await state.set("Local.status", "active")
assert await state.eval('=Local.status <> "done"') is True
assert await state.eval('=Local.status <> "active"') is False
class TestPowerFxArithmetic:
"""Test arithmetic operations."""
@pytest.fixture
def mock_shared_state(self):
"""Create a mock shared state."""
shared_state = MagicMock()
shared_state._data = {}
async def mock_get(key):
if key not in shared_state._data:
raise KeyError(key)
return shared_state._data[key]
async def mock_set(key, value):
shared_state._data[key] = value
shared_state.get = AsyncMock(side_effect=mock_get)
shared_state.set = AsyncMock(side_effect=mock_set)
return shared_state
async def test_addition(self, mock_shared_state):
"""Test + operator."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: value: =Local.TurnCount + 1
await state.set("Local.TurnCount", 3)
result = await state.eval("=Local.TurnCount + 1")
assert result == 4
class TestPowerFxCustomFunctions:
"""Test custom functions (UserMessage, MessageText, AgentMessage)."""
@pytest.fixture
def mock_shared_state(self):
"""Create a mock shared state."""
shared_state = MagicMock()
shared_state._data = {}
async def mock_get(key):
if key not in shared_state._data:
raise KeyError(key)
return shared_state._data[key]
async def mock_set(key, value):
shared_state._data[key] = value
shared_state.get = AsyncMock(side_effect=mock_get)
shared_state.set = AsyncMock(side_effect=mock_set)
return shared_state
@pytest.mark.asyncio
async def test_agent_message_function(self, mock_shared_state):
"""Test AgentMessage function (.NET compatibility alias for AssistantMessage)."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From .NET YAML: messages: =AgentMessage(Local.Response)
await state.set("Local.Response", "Here is the analysis result")
result = await state.eval("=AgentMessage(Local.Response)")
assert isinstance(result, dict)
assert result["role"] == "assistant"
assert result["text"] == "Here is the analysis result"
@pytest.mark.asyncio
async def test_agent_message_with_empty_string(self, mock_shared_state):
"""Test AgentMessage with empty string."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
await state.set("Local.Response", "")
result = await state.eval("=AgentMessage(Local.Response)")
assert result["role"] == "assistant"
assert result["text"] == ""
@pytest.mark.asyncio
async def test_user_message_with_variable(self, mock_shared_state):
"""Test UserMessage function with variable reference."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: messages: =UserMessage(Local.ServiceParameters.IssueDescription)
await state.set("Local.ServiceParameters", {"IssueDescription": "My computer won't boot"})
result = await state.eval("=UserMessage(Local.ServiceParameters.IssueDescription)")
assert isinstance(result, dict)
assert result["role"] == "user"
assert result["text"] == "My computer won't boot"
async def test_user_message_with_simple_variable(self, mock_shared_state):
"""Test UserMessage with simple variable."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: messages: =Local.Problem
await state.set("Local.Problem", "What is 2+2?")
result = await state.eval("=UserMessage(Local.Problem)")
assert result["role"] == "user"
assert result["text"] == "What is 2+2?"
async def test_message_text_with_list(self, mock_shared_state):
"""Test MessageText extracts text from message list."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
await state.set(
"Local.messages",
[
{"role": "user", "text": "Hello"},
{"role": "assistant", "text": "Hi there!"},
],
)
result = await state.eval("=MessageText(Local.messages)")
assert result == "Hi there!"
async def test_message_text_empty_list(self, mock_shared_state):
"""Test MessageText with empty list returns empty string."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
await state.set("Local.messages", [])
result = await state.eval("=MessageText(Local.messages)")
assert result == ""
class TestPowerFxNestedVariables:
"""Test nested variable access patterns from YAML."""
@pytest.fixture
def mock_shared_state(self):
"""Create a mock shared state."""
shared_state = MagicMock()
shared_state._data = {}
async def mock_get(key):
if key not in shared_state._data:
raise KeyError(key)
return shared_state._data[key]
async def mock_set(key, value):
shared_state._data[key] = value
shared_state.get = AsyncMock(side_effect=mock_get)
shared_state.set = AsyncMock(side_effect=mock_set)
return shared_state
async def test_nested_local_variable(self, mock_shared_state):
"""Test nested Local.* variable access."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =Local.ServiceParameters.IssueDescription
await state.set("Local.ServiceParameters", {"IssueDescription": "Screen is black"})
result = await state.eval("=Local.ServiceParameters.IssueDescription")
assert result == "Screen is black"
async def test_nested_routing_parameters(self, mock_shared_state):
"""Test RoutingParameters access."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =Local.RoutingParameters.TeamName
await state.set("Local.RoutingParameters", {"TeamName": "Windows Support"})
result = await state.eval("=Local.RoutingParameters.TeamName")
assert result == "Windows Support"
async def test_nested_ticket_parameters(self, mock_shared_state):
"""Test TicketParameters access."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: =Local.TicketParameters.TicketId
await state.set("Local.TicketParameters", {"TicketId": "TKT-12345"})
result = await state.eval("=Local.TicketParameters.TicketId")
assert result == "TKT-12345"
class TestPowerFxUndefinedVariables:
"""Test graceful handling of undefined variables."""
@pytest.fixture
def mock_shared_state(self):
"""Create a mock shared state."""
shared_state = MagicMock()
shared_state._data = {}
async def mock_get(key):
if key not in shared_state._data:
raise KeyError(key)
return shared_state._data[key]
async def mock_set(key, value):
shared_state._data[key] = value
shared_state.get = AsyncMock(side_effect=mock_get)
shared_state.set = AsyncMock(side_effect=mock_set)
return shared_state
async def test_undefined_local_variable_returns_none(self, mock_shared_state):
"""Test that undefined Local.* variables return None."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# Variable not set - should return None (not raise)
result = await state.eval("=Local.UndefinedVariable")
assert result is None
async def test_undefined_nested_variable_returns_none(self, mock_shared_state):
"""Test that undefined nested variables return None."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# Nested undefined variable
result = await state.eval("=Local.Something.Nested.Deep")
assert result is None
class TestStringInterpolation:
"""Test string interpolation patterns."""
@pytest.fixture
def mock_shared_state(self):
"""Create a mock shared state."""
shared_state = MagicMock()
shared_state._data = {}
async def mock_get(key):
if key not in shared_state._data:
raise KeyError(key)
return shared_state._data[key]
async def mock_set(key, value):
shared_state._data[key] = value
shared_state.get = AsyncMock(side_effect=mock_get)
shared_state.set = AsyncMock(side_effect=mock_set)
return shared_state
async def test_interpolate_local_variable(self, mock_shared_state):
"""Test {Local.Variable} interpolation."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: activity: "Created ticket #{Local.TicketParameters.TicketId}"
await state.set("Local.TicketParameters", {"TicketId": "TKT-999"})
result = await state.interpolate_string("Created ticket #{Local.TicketParameters.TicketId}")
assert result == "Created ticket #TKT-999"
async def test_interpolate_routing_team(self, mock_shared_state):
"""Test routing team interpolation."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize()
# From YAML: activity: Routing to {Local.RoutingParameters.TeamName}
await state.set("Local.RoutingParameters", {"TeamName": "Linux Support"})
result = await state.interpolate_string("Routing to {Local.RoutingParameters.TeamName}")
assert result == "Routing to Linux Support"
class TestWorkflowInputsAccess:
"""Test Workflow.Inputs access patterns."""
@pytest.fixture
def mock_shared_state(self):
"""Create a mock shared state."""
shared_state = MagicMock()
shared_state._data = {}
async def mock_get(key):
if key not in shared_state._data:
raise KeyError(key)
return shared_state._data[key]
async def mock_set(key, value):
shared_state._data[key] = value
shared_state.get = AsyncMock(side_effect=mock_get)
shared_state.set = AsyncMock(side_effect=mock_set)
return shared_state
async def test_inputs_name(self, mock_shared_state):
"""Test inputs.name access."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize({"name": "Alice", "age": 25})
# .NET style (standard)
result = await state.eval("=Workflow.Inputs.name")
assert result == "Alice"
# Also test inputs.name shorthand
result = await state.eval("=inputs.name")
assert result == "Alice"
async def test_inputs_problem(self, mock_shared_state):
"""Test inputs.problem access."""
state = DeclarativeWorkflowState(mock_shared_state)
await state.initialize({"problem": "What is 5 * 6?"})
# .NET style (standard)
result = await state.eval("=Workflow.Inputs.problem")
assert result == "What is 5 * 6?"
@@ -0,0 +1,279 @@
# Copyright (c) Microsoft. All rights reserved.
"""Unit tests for WorkflowFactory."""
import pytest
from agent_framework_declarative._workflows._factory import (
DeclarativeWorkflowError,
WorkflowFactory,
)
class TestWorkflowFactoryValidation:
"""Tests for workflow definition validation."""
def test_missing_actions_raises(self):
"""Test that missing 'actions' field raises an error."""
factory = WorkflowFactory()
with pytest.raises(DeclarativeWorkflowError, match="must have 'actions' field"):
factory.create_workflow_from_yaml("""
name: test-workflow
description: A test
# Missing 'actions' field
""")
def test_actions_not_list_raises(self):
"""Test that non-list 'actions' field raises an error."""
factory = WorkflowFactory()
with pytest.raises(DeclarativeWorkflowError, match="'actions' must be a list"):
factory.create_workflow_from_yaml("""
name: test-workflow
actions: "not a list"
""")
def test_action_missing_kind_raises(self):
"""Test that actions without 'kind' field raise an error."""
factory = WorkflowFactory()
with pytest.raises(DeclarativeWorkflowError, match="missing 'kind' field"):
factory.create_workflow_from_yaml("""
name: test-workflow
actions:
- path: Local.value
value: test
""")
def test_valid_minimal_workflow(self):
"""Test creating a valid minimal workflow."""
factory = WorkflowFactory()
workflow = factory.create_workflow_from_yaml("""
name: minimal-workflow
actions:
- kind: SetValue
path: Local.result
value: done
""")
assert workflow is not None
assert workflow.name == "minimal-workflow"
class TestWorkflowFactoryExecution:
"""Tests for workflow execution."""
@pytest.mark.asyncio
async def test_execute_set_value_workflow(self):
"""Test executing a simple SetValue workflow."""
factory = WorkflowFactory()
workflow = factory.create_workflow_from_yaml("""
name: set-value-test
actions:
- kind: SetValue
path: Local.greeting
value: Hello
- kind: SendActivity
activity:
text: Done
""")
result = await workflow.run({"input": "test"})
outputs = result.get_outputs()
# The workflow should produce output from SendActivity
assert len(outputs) > 0
@pytest.mark.asyncio
async def test_execute_send_activity_workflow(self):
"""Test executing a workflow that sends activities."""
factory = WorkflowFactory()
workflow = factory.create_workflow_from_yaml("""
name: send-activity-test
actions:
- kind: SendActivity
activity:
text: Hello, world!
""")
result = await workflow.run({"input": "test"})
outputs = result.get_outputs()
# Should have a TextOutputEvent
assert len(outputs) >= 1
@pytest.mark.asyncio
async def test_execute_foreach_workflow(self):
"""Test executing a workflow with foreach."""
factory = WorkflowFactory()
workflow = factory.create_workflow_from_yaml("""
name: foreach-test
actions:
- kind: Foreach
source:
- apple
- banana
- cherry
itemName: fruit
actions:
- kind: AppendValue
path: Local.fruits
value: processed
""")
_result = await workflow.run({}) # noqa: F841
# The foreach should have processed 3 items
# We can check this by examining the workflow outputs
@pytest.mark.asyncio
async def test_execute_if_workflow(self):
"""Test executing a workflow with conditional branching."""
factory = WorkflowFactory()
workflow = factory.create_workflow_from_yaml("""
name: if-test
actions:
- kind: If
condition: true
then:
- kind: SendActivity
activity:
text: Condition was true
else:
- kind: SendActivity
activity:
text: Condition was false
""")
result = await workflow.run({})
outputs = result.get_outputs()
# Check for the expected text in WorkflowOutputEvent
_text_outputs = [str(o) for o in outputs if isinstance(o, str) or hasattr(o, "data")] # noqa: F841
assert any("Condition was true" in str(o) for o in outputs)
class TestWorkflowFactoryAgentRegistration:
"""Tests for agent registration."""
def test_register_agent(self):
"""Test registering an agent with the factory."""
class MockAgent:
name = "mock-agent"
factory = WorkflowFactory()
factory.register_agent("myAgent", MockAgent())
assert "myAgent" in factory._agents
def test_register_binding(self):
"""Test registering a binding with the factory."""
def my_function(x):
return x * 2
factory = WorkflowFactory()
factory.register_binding("double", my_function)
assert "double" in factory._bindings
assert factory._bindings["double"](5) == 10
class TestWorkflowFactoryFromPath:
"""Tests for loading workflows from file paths."""
def test_nonexistent_file_raises(self, tmp_path):
"""Test that loading from a nonexistent file raises FileNotFoundError."""
factory = WorkflowFactory()
with pytest.raises(FileNotFoundError):
factory.create_workflow_from_yaml_path(tmp_path / "nonexistent.yaml")
def test_load_from_file(self, tmp_path):
"""Test loading a workflow from a file."""
workflow_file = tmp_path / "Workflow.yaml"
workflow_file.write_text("""
name: file-workflow
actions:
- kind: SetValue
path: Local.loaded
value: true
""")
factory = WorkflowFactory()
workflow = factory.create_workflow_from_yaml_path(workflow_file)
assert workflow is not None
assert workflow.name == "file-workflow"
class TestDisplayNameMetadata:
"""Tests for displayName metadata support."""
@pytest.mark.asyncio
async def test_action_with_display_name(self):
"""Test executing an action with displayName metadata."""
factory = WorkflowFactory()
workflow = factory.create_workflow_from_yaml("""
name: display-name-test
actions:
- kind: SetValue
id: set_greeting
displayName: Set the greeting message
path: Local.greeting
value: Hello
- kind: SendActivity
id: send_greeting
displayName: Send greeting to user
activity:
text: Hello, world!
""")
result = await workflow.run({"input": "test"})
outputs = result.get_outputs()
# Should execute successfully with displayName metadata
assert len(outputs) >= 1
def test_action_context_display_name_property(self):
"""Test that ActionContext provides displayName property."""
from agent_framework_declarative._workflows._handlers import ActionContext
from agent_framework_declarative._workflows._state import WorkflowState
state = WorkflowState()
ctx = ActionContext(
state=state,
action={
"kind": "SetValue",
"id": "test_action",
"displayName": "Test Action Display Name",
"path": "Local.value",
"value": "test",
},
execute_actions=lambda a, s: None,
agents={},
bindings={},
)
assert ctx.action_id == "test_action"
assert ctx.display_name == "Test Action Display Name"
assert ctx.action_kind == "SetValue"
def test_action_context_without_display_name(self):
"""Test ActionContext when displayName is not provided."""
from agent_framework_declarative._workflows._handlers import ActionContext
from agent_framework_declarative._workflows._state import WorkflowState
state = WorkflowState()
ctx = ActionContext(
state=state,
action={
"kind": "SetValue",
"path": "Local.value",
"value": "test",
},
execute_actions=lambda a, s: None,
agents={},
bindings={},
)
assert ctx.action_id is None
assert ctx.display_name is None
assert ctx.action_kind == "SetValue"
@@ -0,0 +1,424 @@
# Copyright (c) Microsoft. All rights reserved.
"""Unit tests for action handlers."""
from collections.abc import AsyncGenerator
from typing import Any
import pytest
# Import handlers to register them
from agent_framework_declarative._workflows import (
_actions_basic, # noqa: F401
_actions_control_flow, # noqa: F401
_actions_error, # noqa: F401
)
from agent_framework_declarative._workflows._handlers import (
ActionContext,
CustomEvent,
TextOutputEvent,
WorkflowEvent,
get_action_handler,
list_action_handlers,
)
from agent_framework_declarative._workflows._state import WorkflowState
def create_action_context(
action: dict[str, Any],
inputs: dict[str, Any] | None = None,
agents: dict[str, Any] | None = None,
bindings: dict[str, Any] | None = None,
) -> ActionContext:
"""Helper to create an ActionContext for testing."""
state = WorkflowState(inputs=inputs or {})
async def execute_actions(
actions: list[dict[str, Any]], state: WorkflowState
) -> AsyncGenerator[WorkflowEvent, None]:
"""Mock execute_actions that runs handlers for nested actions."""
for nested_action in actions:
action_kind = nested_action.get("kind")
handler = get_action_handler(action_kind)
if handler:
ctx = ActionContext(
state=state,
action=nested_action,
execute_actions=execute_actions,
agents=agents or {},
bindings=bindings or {},
)
async for event in handler(ctx):
yield event
return ActionContext(
state=state,
action=action,
execute_actions=execute_actions,
agents=agents or {},
bindings=bindings or {},
)
class TestActionHandlerRegistry:
"""Tests for action handler registration."""
def test_basic_handlers_registered(self):
"""Test that basic handlers are registered."""
handlers = list_action_handlers()
assert "SetValue" in handlers
assert "AppendValue" in handlers
assert "SendActivity" in handlers
assert "EmitEvent" in handlers
def test_control_flow_handlers_registered(self):
"""Test that control flow handlers are registered."""
handlers = list_action_handlers()
assert "Foreach" in handlers
assert "If" in handlers
assert "Switch" in handlers
assert "RepeatUntil" in handlers
assert "BreakLoop" in handlers
assert "ContinueLoop" in handlers
def test_error_handlers_registered(self):
"""Test that error handlers are registered."""
handlers = list_action_handlers()
assert "ThrowException" in handlers
assert "TryCatch" in handlers
def test_get_unknown_handler_returns_none(self):
"""Test that getting an unknown handler returns None."""
assert get_action_handler("UnknownAction") is None
class TestSetValueHandler:
"""Tests for SetValue action handler."""
@pytest.mark.asyncio
async def test_set_simple_value(self):
"""Test setting a simple value."""
ctx = create_action_context({
"kind": "SetValue",
"path": "Local.result",
"value": "test value",
})
handler = get_action_handler("SetValue")
events = [e async for e in handler(ctx)]
assert len(events) == 0 # SetValue doesn't emit events
assert ctx.state.get("Local.result") == "test value"
@pytest.mark.asyncio
async def test_set_value_from_input(self):
"""Test setting a value from workflow inputs."""
ctx = create_action_context(
{
"kind": "SetValue",
"path": "Local.copy",
"value": "literal",
},
inputs={"original": "from input"},
)
handler = get_action_handler("SetValue")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.copy") == "literal"
class TestAppendValueHandler:
"""Tests for AppendValue action handler."""
@pytest.mark.asyncio
async def test_append_to_new_list(self):
"""Test appending to a non-existent list creates it."""
ctx = create_action_context({
"kind": "AppendValue",
"path": "Local.results",
"value": "item1",
})
handler = get_action_handler("AppendValue")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.results") == ["item1"]
@pytest.mark.asyncio
async def test_append_to_existing_list(self):
"""Test appending to an existing list."""
ctx = create_action_context({
"kind": "AppendValue",
"path": "Local.results",
"value": "item2",
})
ctx.state.set("Local.results", ["item1"])
handler = get_action_handler("AppendValue")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.results") == ["item1", "item2"]
class TestSendActivityHandler:
"""Tests for SendActivity action handler."""
@pytest.mark.asyncio
async def test_send_text_activity(self):
"""Test sending a text activity."""
ctx = create_action_context({
"kind": "SendActivity",
"activity": {
"text": "Hello, world!",
},
})
handler = get_action_handler("SendActivity")
events = [e async for e in handler(ctx)]
assert len(events) == 1
assert isinstance(events[0], TextOutputEvent)
assert events[0].text == "Hello, world!"
class TestEmitEventHandler:
"""Tests for EmitEvent action handler."""
@pytest.mark.asyncio
async def test_emit_custom_event(self):
"""Test emitting a custom event."""
ctx = create_action_context({
"kind": "EmitEvent",
"event": {
"name": "myEvent",
"data": {"key": "value"},
},
})
handler = get_action_handler("EmitEvent")
events = [e async for e in handler(ctx)]
assert len(events) == 1
assert isinstance(events[0], CustomEvent)
assert events[0].name == "myEvent"
assert events[0].data == {"key": "value"}
class TestForeachHandler:
"""Tests for Foreach action handler."""
@pytest.mark.asyncio
async def test_foreach_basic_iteration(self):
"""Test basic foreach iteration."""
ctx = create_action_context({
"kind": "Foreach",
"source": ["a", "b", "c"],
"itemName": "letter",
"actions": [
{
"kind": "AppendValue",
"path": "Local.results",
"value": "processed",
}
],
})
handler = get_action_handler("Foreach")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.results") == ["processed", "processed", "processed"]
@pytest.mark.asyncio
async def test_foreach_sets_item_and_index(self):
"""Test that foreach sets item and index variables."""
ctx = create_action_context({
"kind": "Foreach",
"source": ["x", "y"],
"itemName": "item",
"indexName": "idx",
"actions": [],
})
# We'll check the last values after iteration
handler = get_action_handler("Foreach")
_events = [e async for e in handler(ctx)] # noqa: F841
# After iteration, the last item/index should be set
assert ctx.state.get("Local.item") == "y"
assert ctx.state.get("Local.idx") == 1
class TestIfHandler:
"""Tests for If action handler."""
@pytest.mark.asyncio
async def test_if_true_branch(self):
"""Test that the 'then' branch executes when condition is true."""
ctx = create_action_context({
"kind": "If",
"condition": True,
"then": [
{"kind": "SetValue", "path": "Local.branch", "value": "then"},
],
"else": [
{"kind": "SetValue", "path": "Local.branch", "value": "else"},
],
})
handler = get_action_handler("If")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.branch") == "then"
@pytest.mark.asyncio
async def test_if_false_branch(self):
"""Test that the 'else' branch executes when condition is false."""
ctx = create_action_context({
"kind": "If",
"condition": False,
"then": [
{"kind": "SetValue", "path": "Local.branch", "value": "then"},
],
"else": [
{"kind": "SetValue", "path": "Local.branch", "value": "else"},
],
})
handler = get_action_handler("If")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.branch") == "else"
class TestSwitchHandler:
"""Tests for Switch action handler."""
@pytest.mark.asyncio
async def test_switch_matching_case(self):
"""Test switch with a matching case."""
ctx = create_action_context({
"kind": "Switch",
"value": "option2",
"cases": [
{
"match": "option1",
"actions": [{"kind": "SetValue", "path": "Local.result", "value": "one"}],
},
{
"match": "option2",
"actions": [{"kind": "SetValue", "path": "Local.result", "value": "two"}],
},
],
"default": [{"kind": "SetValue", "path": "Local.result", "value": "default"}],
})
handler = get_action_handler("Switch")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.result") == "two"
@pytest.mark.asyncio
async def test_switch_default_case(self):
"""Test switch falls through to default."""
ctx = create_action_context({
"kind": "Switch",
"value": "unknown",
"cases": [
{
"match": "option1",
"actions": [{"kind": "SetValue", "path": "Local.result", "value": "one"}],
},
],
"default": [{"kind": "SetValue", "path": "Local.result", "value": "default"}],
})
handler = get_action_handler("Switch")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.result") == "default"
class TestRepeatUntilHandler:
"""Tests for RepeatUntil action handler."""
@pytest.mark.asyncio
async def test_repeat_until_condition_met(self):
"""Test repeat until condition becomes true."""
ctx = create_action_context({
"kind": "RepeatUntil",
"condition": False, # Will be evaluated each iteration
"maxIterations": 3,
"actions": [
{"kind": "SetValue", "path": "Local.count", "value": 1},
],
})
# Set up a counter that will cause the loop to exit
ctx.state.set("Local.count", 0)
handler = get_action_handler("RepeatUntil")
_events = [e async for e in handler(ctx)] # noqa: F841
# With condition=False (literal), it will run maxIterations times
assert ctx.state.get("Local.iteration") == 3
class TestTryCatchHandler:
"""Tests for TryCatch action handler."""
@pytest.mark.asyncio
async def test_try_without_error(self):
"""Test try block without errors."""
ctx = create_action_context({
"kind": "TryCatch",
"try": [
{"kind": "SetValue", "path": "Local.result", "value": "success"},
],
"catch": [
{"kind": "SetValue", "path": "Local.result", "value": "caught"},
],
})
handler = get_action_handler("TryCatch")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.result") == "success"
@pytest.mark.asyncio
async def test_try_with_throw_exception(self):
"""Test catching a thrown exception."""
ctx = create_action_context({
"kind": "TryCatch",
"try": [
{"kind": "ThrowException", "message": "Test error", "code": "ERR001"},
],
"catch": [
{"kind": "SetValue", "path": "Local.result", "value": "caught"},
],
})
handler = get_action_handler("TryCatch")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.result") == "caught"
assert ctx.state.get("Local.error.message") == "Test error"
assert ctx.state.get("Local.error.code") == "ERR001"
@pytest.mark.asyncio
async def test_finally_always_executes(self):
"""Test that finally block always executes."""
ctx = create_action_context({
"kind": "TryCatch",
"try": [
{"kind": "SetValue", "path": "Local.try", "value": "ran"},
],
"finally": [
{"kind": "SetValue", "path": "Local.finally", "value": "ran"},
],
})
handler = get_action_handler("TryCatch")
_events = [e async for e in handler(ctx)] # noqa: F841
assert ctx.state.get("Local.try") == "ran"
assert ctx.state.get("Local.finally") == "ran"
@@ -0,0 +1,268 @@
# Copyright (c) Microsoft. All rights reserved.
"""Integration tests for workflow samples.
These tests verify that the workflow samples from workflow-samples/ directory
can be parsed and validated by the WorkflowFactory.
"""
from pathlib import Path
import pytest
import yaml
# Path to workflow samples - navigate from tests dir up to repo root
# tests/test_*.py -> packages/declarative/tests/ -> packages/declarative/ -> packages/ -> python/ -> repo root
WORKFLOW_SAMPLES_DIR = Path(__file__).parent.parent.parent.parent.parent / "workflow-samples"
def get_workflow_sample_files():
"""Get all .yaml files from the workflow-samples directory."""
if not WORKFLOW_SAMPLES_DIR.exists():
return []
return list(WORKFLOW_SAMPLES_DIR.glob("*.yaml"))
class TestWorkflowSampleParsing:
"""Tests that verify workflow samples can be parsed correctly."""
@pytest.fixture
def sample_files(self):
"""Get list of sample files."""
return get_workflow_sample_files()
def test_samples_directory_exists(self):
"""Verify the workflow-samples directory exists."""
assert WORKFLOW_SAMPLES_DIR.exists(), f"Workflow samples directory not found at {WORKFLOW_SAMPLES_DIR}"
def test_samples_exist(self, sample_files):
"""Verify there are workflow sample files."""
assert len(sample_files) > 0, "No workflow sample files found"
@pytest.mark.parametrize("yaml_file", get_workflow_sample_files(), ids=lambda f: f.name)
def test_sample_yaml_is_valid(self, yaml_file):
"""Test that each sample YAML file can be parsed."""
with open(yaml_file) as f:
data = yaml.safe_load(f)
assert data is not None, f"Failed to parse {yaml_file.name}"
assert "kind" in data, f"Missing 'kind' field in {yaml_file.name}"
assert data["kind"] == "Workflow", f"Expected kind: Workflow in {yaml_file.name}"
@pytest.mark.parametrize("yaml_file", get_workflow_sample_files(), ids=lambda f: f.name)
def test_sample_has_trigger(self, yaml_file):
"""Test that each sample has a trigger defined."""
with open(yaml_file) as f:
data = yaml.safe_load(f)
assert "trigger" in data, f"Missing 'trigger' field in {yaml_file.name}"
trigger = data["trigger"]
assert trigger is not None, f"Trigger is empty in {yaml_file.name}"
@pytest.mark.parametrize("yaml_file", get_workflow_sample_files(), ids=lambda f: f.name)
def test_sample_has_actions(self, yaml_file):
"""Test that each sample has actions defined."""
with open(yaml_file) as f:
data = yaml.safe_load(f)
trigger = data.get("trigger", {})
actions = trigger.get("actions", [])
assert len(actions) > 0, f"No actions defined in {yaml_file.name}"
@pytest.mark.parametrize("yaml_file", get_workflow_sample_files(), ids=lambda f: f.name)
def test_sample_actions_have_kind(self, yaml_file):
"""Test that each action has a 'kind' field."""
with open(yaml_file) as f:
data = yaml.safe_load(f)
def check_actions(actions, path=""):
for i, action in enumerate(actions):
action_path = f"{path}[{i}]"
assert "kind" in action, f"Action missing 'kind' at {action_path} in {yaml_file.name}"
# Check nested actions
for nested_key in ["actions", "elseActions", "thenActions"]:
if nested_key in action:
check_actions(action[nested_key], f"{action_path}.{nested_key}")
# Check conditions
if "conditions" in action:
for j, cond in enumerate(action["conditions"]):
if "actions" in cond:
check_actions(cond["actions"], f"{action_path}.conditions[{j}].actions")
# Check cases
if "cases" in action:
for j, case in enumerate(action["cases"]):
if "actions" in case:
check_actions(case["actions"], f"{action_path}.cases[{j}].actions")
trigger = data.get("trigger", {})
actions = trigger.get("actions", [])
check_actions(actions, "trigger.actions")
class TestWorkflowDefinitionParsing:
"""Tests for parsing workflow definitions into structured objects."""
@pytest.mark.parametrize("yaml_file", get_workflow_sample_files(), ids=lambda f: f.name)
def test_extract_actions_from_sample(self, yaml_file):
"""Test extracting all actions from a workflow sample."""
with open(yaml_file) as f:
data = yaml.safe_load(f)
# Collect all action kinds used
action_kinds: set[str] = set()
def collect_actions(actions):
for action in actions:
action_kinds.add(action.get("kind", "Unknown"))
# Collect from nested actions
for nested_key in ["actions", "elseActions", "thenActions"]:
if nested_key in action:
collect_actions(action[nested_key])
if "conditions" in action:
for cond in action["conditions"]:
if "actions" in cond:
collect_actions(cond["actions"])
if "cases" in action:
for case in action["cases"]:
if "actions" in case:
collect_actions(case["actions"])
trigger = data.get("trigger", {})
actions = trigger.get("actions", [])
collect_actions(actions)
# Verify we found some actions
assert len(action_kinds) > 0, f"No action kinds found in {yaml_file.name}"
@pytest.mark.parametrize("yaml_file", get_workflow_sample_files(), ids=lambda f: f.name)
def test_extract_agent_names_from_sample(self, yaml_file):
"""Test extracting agent names referenced in a workflow sample."""
with open(yaml_file) as f:
data = yaml.safe_load(f)
agent_names: set[str] = set()
def collect_agents(actions):
for action in actions:
kind = action.get("kind", "")
if kind in ("InvokeAzureAgent", "InvokePromptAgent"):
agent_config = action.get("agent", {})
name = agent_config.get("name") if isinstance(agent_config, dict) else agent_config
if name and not str(name).startswith("="):
agent_names.add(name)
# Collect from nested actions
for nested_key in ["actions", "elseActions", "thenActions"]:
if nested_key in action:
collect_agents(action[nested_key])
if "conditions" in action:
for cond in action["conditions"]:
if "actions" in cond:
collect_agents(cond["actions"])
if "cases" in action:
for case in action["cases"]:
if "actions" in case:
collect_agents(case["actions"])
trigger = data.get("trigger", {})
actions = trigger.get("actions", [])
collect_agents(actions)
# Log the agents found (some workflows may not use agents)
# Agent names: {agent_names}
class TestHandlerCoverage:
"""Tests to verify handler coverage for workflow actions."""
@pytest.fixture
def all_action_kinds(self):
"""Collect all action kinds used across all samples."""
action_kinds: set[str] = set()
def collect_actions(actions):
for action in actions:
action_kinds.add(action.get("kind", "Unknown"))
for nested_key in ["actions", "elseActions", "thenActions"]:
if nested_key in action:
collect_actions(action[nested_key])
if "conditions" in action:
for cond in action["conditions"]:
if "actions" in cond:
collect_actions(cond["actions"])
if "cases" in action:
for case in action["cases"]:
if "actions" in case:
collect_actions(case["actions"])
for yaml_file in get_workflow_sample_files():
with open(yaml_file) as f:
data = yaml.safe_load(f)
trigger = data.get("trigger", {})
actions = trigger.get("actions", [])
collect_actions(actions)
return action_kinds
def test_handlers_exist_for_sample_actions(self, all_action_kinds):
"""Test that handlers exist for all action kinds in samples."""
from agent_framework_declarative._workflows._handlers import list_action_handlers
registered_handlers = set(list_action_handlers())
# Handlers we expect but may not be in samples
expected_handlers = {
"SetValue",
"SetVariable",
"SetTextVariable",
"SetMultipleVariables",
"ResetVariable",
"ClearAllVariables",
"AppendValue",
"SendActivity",
"EmitEvent",
"Foreach",
"If",
"Switch",
"ConditionGroup",
"GotoAction",
"BreakLoop",
"ContinueLoop",
"RepeatUntil",
"TryCatch",
"ThrowException",
"EndWorkflow",
"EndConversation",
"InvokeAzureAgent",
"InvokePromptAgent",
"CreateConversation",
"AddConversationMessage",
"CopyConversationMessages",
"RetrieveConversationMessages",
"Question",
"RequestExternalInput",
"WaitForInput",
}
# Check that sample action kinds have handlers
missing_handlers = all_action_kinds - registered_handlers - {"OnConversationStart"} # Trigger kind, not action
if missing_handlers:
# Informational, not a failure, as some actions may be future work
pass
# Check that we have handlers for the expected core set
core_handlers = registered_handlers & expected_handlers
assert len(core_handlers) > 10, "Expected more core handlers to be registered"
@@ -0,0 +1,225 @@
# Copyright (c) Microsoft. All rights reserved.
"""Unit tests for WorkflowState class."""
import pytest
from agent_framework_declarative._workflows._state import WorkflowState
class TestWorkflowStateInitialization:
"""Tests for WorkflowState initialization."""
def test_empty_initialization(self):
"""Test creating a WorkflowState with no inputs."""
state = WorkflowState()
assert state.inputs == {}
assert state.outputs == {}
assert state.local == {}
assert state.agent == {}
def test_initialization_with_inputs(self):
"""Test creating a WorkflowState with inputs."""
state = WorkflowState(inputs={"query": "Hello", "count": 5})
assert state.inputs == {"query": "Hello", "count": 5}
assert state.outputs == {}
def test_inputs_are_immutable(self):
"""Test that inputs cannot be modified through set()."""
state = WorkflowState(inputs={"query": "Hello"})
with pytest.raises(ValueError, match="Cannot modify Workflow.Inputs"):
state.set("Workflow.Inputs.query", "Modified")
class TestWorkflowStateGetSet:
"""Tests for get and set operations."""
def test_set_and_get_turn_variable(self):
"""Test setting and getting a turn variable."""
state = WorkflowState()
state.set("Local.counter", 10)
assert state.get("Local.counter") == 10
def test_set_and_get_nested_turn_variable(self):
"""Test setting and getting a nested turn variable."""
state = WorkflowState()
state.set("Local.data.nested.value", "test")
assert state.get("Local.data.nested.value") == "test"
def test_set_and_get_workflow_output(self):
"""Test setting and getting workflow output."""
state = WorkflowState()
state.set("Workflow.Outputs.result", "success")
assert state.get("Workflow.Outputs.result") == "success"
assert state.outputs["result"] == "success"
def test_get_with_default(self):
"""Test get with default value."""
state = WorkflowState()
assert state.get("Local.nonexistent") is None
assert state.get("Local.nonexistent", "default") == "default"
def test_get_workflow_inputs(self):
"""Test getting workflow inputs."""
state = WorkflowState(inputs={"query": "test"})
assert state.get("Workflow.Inputs.query") == "test"
def test_set_custom_namespace(self):
"""Test setting a custom namespace variable."""
state = WorkflowState()
state.set("custom.myvar", "value")
assert state.get("custom.myvar") == "value"
class TestWorkflowStateAppend:
"""Tests for append operation."""
def test_append_to_nonexistent_list(self):
"""Test appending to a path that doesn't exist yet."""
state = WorkflowState()
state.append("Local.results", "item1")
assert state.get("Local.results") == ["item1"]
def test_append_to_existing_list(self):
"""Test appending to an existing list."""
state = WorkflowState()
state.set("Local.results", ["item1"])
state.append("Local.results", "item2")
assert state.get("Local.results") == ["item1", "item2"]
def test_append_to_non_list_raises(self):
"""Test that appending to a non-list raises ValueError."""
state = WorkflowState()
state.set("Local.value", "not a list")
with pytest.raises(ValueError, match="Cannot append to non-list"):
state.append("Local.value", "item")
class TestWorkflowStateAgentResult:
"""Tests for agent result management."""
def test_set_agent_result(self):
"""Test setting agent result."""
state = WorkflowState()
state.set_agent_result(
text="Agent response",
messages=[{"role": "assistant", "content": "Hello"}],
tool_calls=[{"name": "tool1"}],
)
assert state.agent["text"] == "Agent response"
assert len(state.agent["messages"]) == 1
assert len(state.agent["toolCalls"]) == 1
def test_get_agent_result_via_path(self):
"""Test getting agent result via path."""
state = WorkflowState()
state.set_agent_result(text="Response")
assert state.get("Agent.text") == "Response"
def test_reset_agent(self):
"""Test resetting agent result."""
state = WorkflowState()
state.set_agent_result(text="Response")
state.reset_agent()
assert state.agent == {}
class TestWorkflowStateConversation:
"""Tests for conversation management."""
def test_add_conversation_message(self):
"""Test adding a conversation message."""
state = WorkflowState()
message = {"role": "user", "content": "Hello"}
state.add_conversation_message(message)
assert len(state.conversation["messages"]) == 1
assert state.conversation["messages"][0] == message
def test_get_conversation_history(self):
"""Test getting conversation history."""
state = WorkflowState()
state.add_conversation_message({"role": "user", "content": "Hi"})
state.add_conversation_message({"role": "assistant", "content": "Hello"})
assert len(state.get("Conversation.history")) == 2
class TestWorkflowStatePowerFx:
"""Tests for PowerFx expression evaluation."""
def test_eval_non_expression(self):
"""Test that non-expressions are returned as-is."""
state = WorkflowState()
assert state.eval("plain text") == "plain text"
def test_eval_if_expression_with_literal(self):
"""Test eval_if_expression with a literal value."""
state = WorkflowState()
assert state.eval_if_expression(42) == 42
assert state.eval_if_expression(["a", "b"]) == ["a", "b"]
def test_eval_if_expression_with_non_expression_string(self):
"""Test eval_if_expression with a non-expression string."""
state = WorkflowState()
assert state.eval_if_expression("plain text") == "plain text"
def test_to_powerfx_symbols(self):
"""Test converting state to PowerFx symbols."""
state = WorkflowState(inputs={"query": "test"})
state.set("Local.counter", 5)
state.set("Workflow.Outputs.result", "done")
symbols = state.to_powerfx_symbols()
assert symbols["Workflow"]["Inputs"]["query"] == "test"
assert symbols["Workflow"]["Outputs"]["result"] == "done"
assert symbols["Local"]["counter"] == 5
class TestWorkflowStateClone:
"""Tests for state cloning."""
def test_clone_creates_copy(self):
"""Test that clone creates a copy of the state."""
state = WorkflowState(inputs={"query": "test"})
state.set("Local.counter", 5)
cloned = state.clone()
assert cloned.get("Workflow.Inputs.query") == "test"
assert cloned.get("Local.counter") == 5
def test_clone_is_independent(self):
"""Test that modifications to clone don't affect original."""
state = WorkflowState()
state.set("Local.value", "original")
cloned = state.clone()
cloned.set("Local.value", "modified")
assert state.get("Local.value") == "original"
assert cloned.get("Local.value") == "modified"
class TestWorkflowStateResetTurn:
"""Tests for turn reset."""
def test_reset_local_clears_turn_variables(self):
"""Test that reset_local clears turn variables."""
state = WorkflowState()
state.set("Local.var1", "value1")
state.set("Local.var2", "value2")
state.reset_local()
assert state.get("Local.var1") is None
assert state.get("Local.var2") is None
assert state.local == {}
def test_reset_local_preserves_other_state(self):
"""Test that reset_local preserves other state."""
state = WorkflowState(inputs={"query": "test"})
state.set("Workflow.Outputs.result", "done")
state.set("Local.temp", "will be cleared")
state.reset_local()
assert state.get("Workflow.Inputs.query") == "test"
assert state.get("Workflow.Outputs.result") == "done"