Files
agent-framework/python/packages/devui/tests/test_schema_generation.py
T
Victor Dibia 94eae24082 Python: DevUI: Add OpenAI Responses API proxy support + HIL for Workflows (#1737)
* DevUI: Add OpenAI Responses API proxy support with enhanced UI features

This commit adds support for proxying requests to OpenAI's Responses API,
allowing DevUI to route conversations to OpenAI models when configured to enable testing.

Backend changes:
- Add OpenAI proxy executor with conversation routing logic
- Enhance event mapper to support OpenAI Responses API format
- Extend server endpoints to handle OpenAI proxy mode
- Update models with OpenAI-specific response types
- Remove emojis from logging and CLI output for cleaner text

Frontend changes:
- Add settings modal with OpenAI proxy configuration UI
- Enhance agent and workflow views with improved state management
- Add new UI components (separator, switch) for settings
- Update debug panel with better event filtering
- Improve message renderers for OpenAI content types
- Update types and API client for OpenAI integration

* update ui, settings modal and workflow input form, add register cleanup hooks.

* add workflow HIL support, user mode, other fixes

* feat(devui): add human-in-the-loop (HIL) support with dynamic response schemas

Implement  HIL workflow support allowing workflows to pause for user input
with dynamically generated JSON schemas based on response handler type hints.

Key Features:
- Automatic response schema extraction from @response_handler decorators
- Dynamic form generation in UI based on Pydantic/dataclass response types
- Checkpoint-based conversation storage for HIL requests/responses
- Resume workflow execution after user provides HIL response

Backend Changes:
- Add extract_response_type_from_executor() to introspect response handlers
- Enrich RequestInfoEvent with response_schema via _enrich_request_info_event_with_response_schema()
- Map RequestInfoEvent to response.input.requested OpenAI event format
- Store HIL responses in conversation history and restore checkpoints

Frontend Changes:
- Add HILInputModal component with SchemaFormRenderer for dynamic forms
- Support Pydantic BaseModel and dataclass response types
- Render enum fields as dropdowns, strings as text/textarea, numbers, booleans, arrays, objects
- Display original request context alongside response form

Testing:
- Add  tests for checkpoint storage (test_checkpoints.py)
- Add schema generation tests for all input types (test_schema_generation.py)
- Validate end-to-end HIL flow with spam workflow sample

This enables workflows to seamlessly pause execution and request structured user input
with type-safe, validated forms generated automatically from response type annotations.

* improve HIL support, improve workflow execution view

* ui updates

* ui updates

* improve HIL for workflows, add auth and view modes

* update workflow

* security improvements , ui fixes

* fix mypy error

* update loading spinner in ui

---------

Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
2025-11-07 23:28:32 +00:00

232 lines
7.2 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
"""Test schema generation for different input types."""
import sys
from dataclasses import dataclass
from pathlib import Path
from typing import Literal
import pytest
# Add parent package to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from agent_framework_devui._utils import extract_response_type_from_executor, generate_input_schema
@dataclass
class InputData:
text: str
source: str
@dataclass
class Address:
street: str
city: str
zipcode: str
@dataclass
class PersonData:
name: str
age: int
address: Address
def test_builtin_types_schema_generation():
"""Test schema generation for built-in types."""
# Test str schema
str_schema = generate_input_schema(str)
assert str_schema is not None
assert isinstance(str_schema, dict)
# Test dict schema
dict_schema = generate_input_schema(dict)
assert dict_schema is not None
assert isinstance(dict_schema, dict)
# Test int schema
int_schema = generate_input_schema(int)
assert int_schema is not None
assert isinstance(int_schema, dict)
def test_dataclass_schema_generation():
"""Test schema generation for dataclass."""
schema = generate_input_schema(InputData)
assert schema is not None
assert isinstance(schema, dict)
# Basic schema structure checks
if "properties" in schema:
properties = schema["properties"]
assert "text" in properties
assert "source" in properties
def test_chat_message_schema_generation():
"""Test schema generation for ChatMessage (SerializationMixin)."""
try:
from agent_framework import ChatMessage
schema = generate_input_schema(ChatMessage)
assert schema is not None
assert isinstance(schema, dict)
except ImportError:
pytest.skip("ChatMessage not available - agent_framework not installed")
def test_pydantic_model_schema_generation():
"""Test schema generation for Pydantic models."""
try:
from pydantic import BaseModel, Field
class UserInput(BaseModel):
name: str = Field(description="User's name")
age: int = Field(description="User's age")
email: str | None = Field(default=None, description="Optional email")
schema = generate_input_schema(UserInput)
assert schema is not None
assert isinstance(schema, dict)
# Check if properties exist
if "properties" in schema:
properties = schema["properties"]
assert "name" in properties
assert "age" in properties
assert "email" in properties
except ImportError:
pytest.skip("Pydantic not available")
def test_nested_dataclass_schema_generation():
"""Test schema generation for nested dataclass."""
schema = generate_input_schema(PersonData)
assert schema is not None
assert isinstance(schema, dict)
# Basic schema structure checks
if "properties" in schema:
properties = schema["properties"]
assert "name" in properties
assert "age" in properties
assert "address" in properties
def test_schema_generation_error_handling():
"""Test schema generation with invalid inputs."""
# Test with a non-type object - should handle gracefully
try:
# Use a non-type object that might cause issues
schema = generate_input_schema("not_a_type") # type: ignore
# If it doesn't raise an exception, the result should be valid
if schema is not None:
assert isinstance(schema, dict)
except (TypeError, ValueError, AttributeError):
# It's acceptable for this to raise an error
pass
def test_extract_response_type_from_executor():
"""Test extraction of response type from @response_handler methods."""
try:
from agent_framework import Executor, WorkflowContext, handler, response_handler
from pydantic import BaseModel, Field
# Define test request and response types
@dataclass
class TestApprovalRequest:
"""Test request for approval."""
prompt: str
context: str
class TestDecision(BaseModel):
"""Test decision response."""
decision: Literal["approve", "reject"] = Field(description="User's decision")
reason: str = Field(description="Reason for decision", default="")
# Create test executor with @response_handler
class TestExecutor(Executor):
"""Test executor with response handler."""
def __init__(self):
super().__init__(id="test_executor")
@handler
async def handle_message(self, message: str, ctx: WorkflowContext) -> None:
"""Regular handler to satisfy executor requirements."""
# Request info that will be handled by response_handler
request = TestApprovalRequest(prompt="Test", context="Test context")
await ctx.request_info(request, TestDecision)
@response_handler
async def handle_approval(
self, original_request: TestApprovalRequest, response: TestDecision, ctx: WorkflowContext
) -> None:
"""Handle approval response."""
pass
# Test extraction
executor = TestExecutor()
extracted_type = extract_response_type_from_executor(executor, TestApprovalRequest)
# Verify correct type was extracted
assert extracted_type is not None, "Should extract response type from @response_handler"
assert extracted_type == TestDecision, f"Expected TestDecision, got {extracted_type}"
# Test full schema generation pipeline
schema = generate_input_schema(extracted_type)
assert schema is not None
assert isinstance(schema, dict)
assert "properties" in schema
assert "decision" in schema["properties"]
assert "enum" in schema["properties"]["decision"]
assert schema["properties"]["decision"]["enum"] == ["approve", "reject"]
except ImportError as e:
pytest.skip(f"Required dependencies not available: {e}")
def test_extract_response_type_no_match():
"""Test that extraction returns None when no matching handler exists."""
try:
from agent_framework import Executor, WorkflowContext, handler
@dataclass
class UnmatchedRequest:
"""Request type with no handler."""
data: str
class MinimalExecutor(Executor):
"""Executor with a handler but no matching response_handler."""
def __init__(self):
super().__init__(id="minimal_executor")
@handler
async def handle_message(self, message: str, ctx: WorkflowContext) -> None:
"""Regular handler."""
pass
executor = MinimalExecutor()
extracted_type = extract_response_type_from_executor(executor, UnmatchedRequest)
assert extracted_type is None, "Should return None when no matching handler exists"
except ImportError as e:
pytest.skip(f"Required dependencies not available: {e}")
if __name__ == "__main__":
# Simple test runner for manual execution
pytest.main([__file__, "-v"])