mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
c341ee7ed2
* Python: DevUI - Internal Refactor, Conversations API support, and performance improvements Comprehensive refactor of DevUI package including samples relocation, frontend reorganization, OpenAI Conversations API support, and critical performance and code quality improvements. Key Changes: Architecture & Organization - Moved DevUI samples to python/samples/getting_started/devui/ - Consolidated with other framework samples for better discoverability - Added .env.example files and comprehensive README - Restructured frontend components into feature-based folders (agent, workflow, gallery, layout) - Created new OpenAI-compliant message renderers (devui should render oai responses types primarily) New Features - Added _conversations.py (467 lines) - Full conversation storage abstraction, replaces the /threads endpoint to better match oai conversations api - Implements OpenAI Conversations API for thread management, Supports in-memory and extensible storage backends API Simplification - Use 'model' field as entity_id (agent/workflow name) instead of extra_body - Use standard OpenAI 'conversation' field for conversation context. Performance & Quality Improvements - Improved context management in MessageMapper with bounded memory (~500KB max) - Implemented hybrid LRU + cleanup approach to prevent unbounded memory growth - General QOL improvement - Eliminated ~150 lines of dead/duplicate code, Consolidated helper functions into _utils.py, Extracted magic numbers to module-level constants, Optimized conversation item lookups with index-based approach Testing - Added test_conversations.py (13 tests) - Added test_performance_fixes.py (9 tests) - Updated existing tests for code consolidation - 53 tests passing Impact: 76 files changed: +4,106 insertions, -2,373 deletions All linting and formatting checks passing. No breaking changes - backward compatible. Migration: Samples moved to python/samples/getting_started/devui/ * readme lint fixes * initial support for function approval and minor ui fixes
336 lines
11 KiB
Python
336 lines
11 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
"""Tests for conversation store implementation."""
|
|
|
|
from typing import cast
|
|
|
|
import pytest
|
|
from openai.types.conversations import InputFileContent, InputImageContent, InputTextContent
|
|
|
|
from agent_framework_devui._conversations import InMemoryConversationStore
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_conversation():
|
|
"""Test creating a conversation."""
|
|
store = InMemoryConversationStore()
|
|
|
|
conversation = store.create_conversation(metadata={"agent_id": "test_agent"})
|
|
|
|
assert conversation.id.startswith("conv_")
|
|
assert conversation.object == "conversation"
|
|
assert conversation.metadata == {"agent_id": "test_agent"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_conversation():
|
|
"""Test retrieving a conversation."""
|
|
store = InMemoryConversationStore()
|
|
|
|
# Create conversation
|
|
created = store.create_conversation(metadata={"agent_id": "test_agent"})
|
|
|
|
# Retrieve it
|
|
retrieved = store.get_conversation(created.id)
|
|
|
|
assert retrieved is not None
|
|
assert retrieved.id == created.id
|
|
assert retrieved.metadata == {"agent_id": "test_agent"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_conversation_not_found():
|
|
"""Test retrieving non-existent conversation."""
|
|
store = InMemoryConversationStore()
|
|
|
|
conversation = store.get_conversation("conv_nonexistent")
|
|
|
|
assert conversation is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_conversation():
|
|
"""Test updating conversation metadata."""
|
|
store = InMemoryConversationStore()
|
|
|
|
# Create conversation
|
|
created = store.create_conversation(metadata={"agent_id": "test_agent"})
|
|
|
|
# Update metadata
|
|
updated = store.update_conversation(created.id, metadata={"agent_id": "new_agent", "session_id": "sess_123"})
|
|
|
|
assert updated.id == created.id
|
|
assert updated.metadata == {"agent_id": "new_agent", "session_id": "sess_123"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_delete_conversation():
|
|
"""Test deleting a conversation."""
|
|
store = InMemoryConversationStore()
|
|
|
|
# Create conversation
|
|
created = store.create_conversation(metadata={"agent_id": "test_agent"})
|
|
|
|
# Delete it
|
|
result = store.delete_conversation(created.id)
|
|
|
|
assert result.id == created.id
|
|
assert result.deleted is True
|
|
assert result.object == "conversation.deleted"
|
|
|
|
# Verify it's gone
|
|
assert store.get_conversation(created.id) is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_thread():
|
|
"""Test getting underlying AgentThread."""
|
|
store = InMemoryConversationStore()
|
|
|
|
# Create conversation
|
|
conversation = store.create_conversation(metadata={"agent_id": "test_agent"})
|
|
|
|
# Get thread
|
|
thread = store.get_thread(conversation.id)
|
|
|
|
assert thread is not None
|
|
# AgentThread should have message_store
|
|
assert hasattr(thread, "message_store")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_thread_not_found():
|
|
"""Test getting thread for non-existent conversation."""
|
|
store = InMemoryConversationStore()
|
|
|
|
thread = store.get_thread("conv_nonexistent")
|
|
|
|
assert thread is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_conversations_by_metadata():
|
|
"""Test filtering conversations by metadata."""
|
|
store = InMemoryConversationStore()
|
|
|
|
# Create multiple conversations
|
|
_conv1 = store.create_conversation(metadata={"agent_id": "agent1"})
|
|
_conv2 = store.create_conversation(metadata={"agent_id": "agent2"})
|
|
conv3 = store.create_conversation(metadata={"agent_id": "agent1", "session_id": "sess_1"})
|
|
|
|
# Filter by agent_id
|
|
results = store.list_conversations_by_metadata({"agent_id": "agent1"})
|
|
|
|
assert len(results) == 2
|
|
assert all(cast(dict[str, str], c.metadata).get("agent_id") == "agent1" for c in results if c.metadata)
|
|
|
|
# Filter by agent_id and session_id
|
|
results = store.list_conversations_by_metadata({"agent_id": "agent1", "session_id": "sess_1"})
|
|
|
|
assert len(results) == 1
|
|
assert results[0].id == conv3.id
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_add_items():
|
|
"""Test adding items to conversation."""
|
|
store = InMemoryConversationStore()
|
|
|
|
# Create conversation
|
|
conversation = store.create_conversation(metadata={"agent_id": "test_agent"})
|
|
|
|
# Add items
|
|
items = [{"role": "user", "content": [{"type": "text", "text": "Hello"}]}]
|
|
|
|
conv_items = await store.add_items(conversation.id, items=items)
|
|
|
|
assert len(conv_items) == 1
|
|
# Message is a ConversationItem type - check standard OpenAI fields
|
|
assert conv_items[0].type == "message"
|
|
assert conv_items[0].role == "user"
|
|
assert conv_items[0].status == "completed"
|
|
assert len(conv_items[0].content) == 1
|
|
assert conv_items[0].content[0].type == "text"
|
|
text_content = cast(InputTextContent, conv_items[0].content[0])
|
|
assert text_content.text == "Hello"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_items():
|
|
"""Test listing conversation items."""
|
|
store = InMemoryConversationStore()
|
|
|
|
# Create conversation
|
|
conversation = store.create_conversation(metadata={"agent_id": "test_agent"})
|
|
|
|
# Add items
|
|
items = [
|
|
{"role": "user", "content": [{"type": "text", "text": "Hello"}]},
|
|
{"role": "assistant", "content": [{"type": "text", "text": "Hi there"}]},
|
|
]
|
|
await store.add_items(conversation.id, items=items)
|
|
|
|
# List items
|
|
retrieved_items, has_more = await store.list_items(conversation.id)
|
|
|
|
assert len(retrieved_items) >= 2 # At least the items we added
|
|
assert has_more is False
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_items_pagination():
|
|
"""Test pagination when listing items."""
|
|
store = InMemoryConversationStore()
|
|
|
|
# Create conversation
|
|
conversation = store.create_conversation(metadata={"agent_id": "test_agent"})
|
|
|
|
# Add multiple items
|
|
items = [{"role": "user", "content": [{"type": "text", "text": f"Message {i}"}]} for i in range(5)]
|
|
await store.add_items(conversation.id, items=items)
|
|
|
|
# List with limit
|
|
retrieved_items, has_more = await store.list_items(conversation.id, limit=3)
|
|
|
|
assert len(retrieved_items) == 3
|
|
assert has_more is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_items_converts_function_calls():
|
|
"""Test that list_items properly converts function calls to ResponseFunctionToolCallItem."""
|
|
from agent_framework import ChatMessage, ChatMessageStore, Role
|
|
|
|
store = InMemoryConversationStore()
|
|
|
|
# Create conversation
|
|
conversation = store.create_conversation(metadata={"agent_id": "test_agent"})
|
|
|
|
# Get the underlying thread and set up message store
|
|
thread = store.get_thread(conversation.id)
|
|
assert thread is not None
|
|
|
|
# Initialize message store if not present
|
|
if thread.message_store is None:
|
|
thread.message_store = ChatMessageStore()
|
|
|
|
# Simulate messages from agent execution with function calls
|
|
messages = [
|
|
ChatMessage(role=Role.USER, contents=[{"type": "text", "text": "What's the weather in SF?"}]),
|
|
ChatMessage(
|
|
role=Role.ASSISTANT,
|
|
contents=[
|
|
{
|
|
"type": "function_call",
|
|
"name": "get_weather",
|
|
"arguments": '{"city": "San Francisco"}',
|
|
"call_id": "call_test123",
|
|
}
|
|
],
|
|
),
|
|
ChatMessage(
|
|
role=Role.TOOL,
|
|
contents=[
|
|
{
|
|
"type": "function_result",
|
|
"call_id": "call_test123",
|
|
"output": '{"temperature": 65, "condition": "sunny"}',
|
|
}
|
|
],
|
|
),
|
|
ChatMessage(role=Role.ASSISTANT, contents=[{"type": "text", "text": "The weather is sunny, 65°F"}]),
|
|
]
|
|
|
|
# Add messages to thread
|
|
await thread.on_new_messages(messages)
|
|
|
|
# List conversation items
|
|
items, has_more = await store.list_items(conversation.id)
|
|
|
|
# Verify we got the right number and types of items
|
|
assert len(items) == 4, f"Expected 4 items, got {len(items)}"
|
|
assert has_more is False
|
|
|
|
# Check item types
|
|
assert items[0].type == "message", "First item should be a message"
|
|
assert items[0].role == "user"
|
|
assert len(items[0].content) == 1
|
|
text_content_0 = cast(InputTextContent, items[0].content[0])
|
|
assert text_content_0.text == "What's the weather in SF?"
|
|
|
|
assert items[1].type == "function_call", "Second item should be a function_call"
|
|
assert items[1].call_id == "call_test123"
|
|
assert items[1].name == "get_weather"
|
|
assert items[1].arguments == '{"city": "San Francisco"}'
|
|
assert items[1].status == "completed"
|
|
|
|
assert items[2].type == "function_call_output", "Third item should be a function_call_output"
|
|
assert items[2].call_id == "call_test123"
|
|
assert items[2].output == '{"temperature": 65, "condition": "sunny"}'
|
|
assert items[2].status == "completed"
|
|
|
|
assert items[3].type == "message", "Fourth item should be a message"
|
|
assert items[3].role == "assistant"
|
|
assert len(items[3].content) == 1
|
|
text_content_3 = cast(InputTextContent, items[3].content[0])
|
|
assert text_content_3.text == "The weather is sunny, 65°F"
|
|
|
|
# CRITICAL: Ensure no empty message items
|
|
for item in items:
|
|
if item.type == "message":
|
|
assert len(item.content) > 0, f"Message item {item.id} has empty content!"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_items_handles_images_and_files():
|
|
"""Test that list_items properly converts data content (images/files) to OpenAI types."""
|
|
from agent_framework import ChatMessage, ChatMessageStore, Role
|
|
|
|
store = InMemoryConversationStore()
|
|
|
|
# Create conversation
|
|
conversation = store.create_conversation(metadata={"agent_id": "test_agent"})
|
|
|
|
# Get the underlying thread
|
|
thread = store.get_thread(conversation.id)
|
|
assert thread is not None
|
|
|
|
if thread.message_store is None:
|
|
thread.message_store = ChatMessageStore()
|
|
|
|
# Simulate message with image and file
|
|
messages = [
|
|
ChatMessage(
|
|
role=Role.USER,
|
|
contents=[
|
|
{"type": "text", "text": "Check this image and PDF"},
|
|
{"type": "data", "uri": "data:image/png;base64,iVBORw0KGgo=", "media_type": "image/png"},
|
|
{"type": "data", "uri": "data:application/pdf;base64,JVBERi0=", "media_type": "application/pdf"},
|
|
],
|
|
),
|
|
]
|
|
|
|
await thread.on_new_messages(messages)
|
|
|
|
# List items
|
|
items, has_more = await store.list_items(conversation.id)
|
|
|
|
assert len(items) == 1
|
|
assert items[0].type == "message"
|
|
assert items[0].role == "user"
|
|
assert len(items[0].content) == 3
|
|
|
|
# Check content types
|
|
assert items[0].content[0].type == "text"
|
|
text_content = cast(InputTextContent, items[0].content[0])
|
|
assert text_content.text == "Check this image and PDF"
|
|
|
|
assert items[0].content[1].type == "input_image"
|
|
image_content = cast(InputImageContent, items[0].content[1])
|
|
assert image_content.image_url == "data:image/png;base64,iVBORw0KGgo="
|
|
assert image_content.detail == "auto"
|
|
|
|
assert items[0].content[2].type == "input_file"
|
|
file_content = cast(InputFileContent, items[0].content[2])
|
|
assert file_content.file_url == "data:application/pdf;base64,JVBERi0="
|