Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions src/backend/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def _check_input_for_harmful_content(message: str) -> tuple[bool, str]:
r"You are an Image Content Agent",
r"You are a Compliance Agent",
# Handoff instructions (match both underscore and hyphen agent names)
r"hand off to \w+[_\-]agent",
r"hand back to \w+[_\-]agent",
r"hand off to [\w\-]+[_\-]agent",
r"hand back to [\w\-]+[_\-]agent",
Comment thread
Akhileswara-Microsoft marked this conversation as resolved.
r"may hand off to",
r"After (?:generating|completing|validation|parsing)",
# Internal workflow markers
Expand Down Expand Up @@ -545,11 +545,6 @@ def _get_chat_client(self):
if not azure_endpoint:
raise ValueError("AZURE_OPENAI_ENDPOINT is required for Foundry mode chat completions")

def get_token() -> str:
"""Token provider callable - invoked for each request to ensure fresh tokens."""
token = self._credential.get_token(TOKEN_ENDPOINT)
return token.token

model_deployment = app_settings.ai_foundry.model_deployment or app_settings.azure_openai.gpt_model
api_version = app_settings.azure_openai.api_version

Expand All @@ -558,25 +553,20 @@ def get_token() -> str:
azure_endpoint=azure_endpoint,
model=model_deployment,
api_version=api_version,
credential=get_token,
credential=self._credential,
)
else:
# Azure OpenAI Direct mode
endpoint = app_settings.azure_openai.endpoint
if not endpoint:
raise ValueError("AZURE_OPENAI_ENDPOINT is not configured")

def get_token() -> str:
"""Token provider callable - invoked for each request to ensure fresh tokens."""
token = self._credential.get_token(TOKEN_ENDPOINT)
return token.token

logger.info("Using Azure OpenAI Direct mode with DefaultAzureCredential token provider")
logger.info("Using Azure OpenAI Direct mode with DefaultAzureCredential")
self._chat_client = OpenAIChatCompletionClient(
azure_endpoint=endpoint,
model=app_settings.azure_openai.gpt_model,
api_version=app_settings.azure_openai.api_version,
credential=get_token,
credential=self._credential,
)
return self._chat_client

Expand Down
11 changes: 1 addition & 10 deletions src/backend/services/title_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

logger = logging.getLogger(__name__)

# Token endpoint for Azure OpenAI authentication
TOKEN_ENDPOINT = "https://cognitiveservices.azure.com/.default"

# Title generation instructions (from MS reference accelerator)
TITLE_INSTRUCTIONS = """Summarize the conversation so far into a 4-word or less title.
Do not use any quotation marks or punctuation.
Expand Down Expand Up @@ -58,17 +55,11 @@ def initialize(self) -> None:

api_version = app_settings.azure_openai.api_version

# Create token provider function
def get_token() -> str:
"""Token provider callable - invoked for each request to ensure fresh tokens."""
token = self._credential.get_token(TOKEN_ENDPOINT)
return token.token

chat_client = OpenAIChatCompletionClient(
azure_endpoint=endpoint,
model=deployment,
api_version=api_version,
credential=get_token,
credential=self._credential,
)
Comment thread
Akhileswara-Microsoft marked this conversation as resolved.

self._agent = Agent(
Expand Down
18 changes: 18 additions & 0 deletions src/tests/services/test_orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ def test_filter_system_prompt_handoff():
assert "text_content_agent" not in filtered


def test_filter_system_prompt_handoff_hyphenated():
"""Test filtering of handoff instructions with hyphenated agent names."""

response = "I'll hand off to text-content-agent now"
filtered = _filter_system_prompt_from_response(response)

assert "text-content-agent" not in filtered


def test_filter_system_prompt_handback_hyphenated():
"""Test filtering of hand back instructions with hyphenated agent names."""

response = "Let me hand back to triage-agent with results"
filtered = _filter_system_prompt_from_response(response)

assert "triage-agent" not in filtered


def test_filter_system_prompt_critical():
"""Test filtering of critical instruction markers."""

Expand Down
64 changes: 64 additions & 0 deletions src/tests/test_title_generation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,67 @@ def test_returns_existing_instance(self, mock_existing):
mock_existing.__bool__ = lambda self: True
result = get_title_service()
assert result is mock_existing


# ---------------------------------------------------------------------------
# TitleService.initialize wiring
# ---------------------------------------------------------------------------


class TestTitleServiceInitialize:
"""Tests that initialize wires the chat client with correct config."""

@patch("services.title_service.app_settings")
@patch("services.title_service.DefaultAzureCredential")
@patch("services.title_service.OpenAIChatCompletionClient")
@patch("services.title_service.Agent")
def test_initialize_wires_credential_direct_mode(
self, mock_agent, mock_client, mock_cred_cls, mock_settings
):
"""Test that initialize passes credential directly to chat client."""
mock_credential = MagicMock()
mock_cred_cls.return_value = mock_credential

mock_settings.ai_foundry.use_foundry = False
mock_settings.azure_openai.endpoint = "https://test.openai.azure.com"
mock_settings.azure_openai.gpt_model = "gpt-4o"
mock_settings.azure_openai.api_version = "2024-02-15"

svc = TitleService()
svc.initialize()

mock_client.assert_called_once_with(
azure_endpoint="https://test.openai.azure.com",
model="gpt-4o",
api_version="2024-02-15",
credential=mock_credential,
)
assert svc._initialized is True

@patch("services.title_service.app_settings")
@patch("services.title_service.DefaultAzureCredential")
@patch("services.title_service.OpenAIChatCompletionClient")
@patch("services.title_service.Agent")
def test_initialize_wires_credential_foundry_mode(
self, mock_agent, mock_client, mock_cred_cls, mock_settings
):
"""Test that initialize uses Foundry endpoint and model."""
mock_credential = MagicMock()
mock_cred_cls.return_value = mock_credential

mock_settings.ai_foundry.use_foundry = True
mock_settings.azure_openai.endpoint = "https://foundry.openai.azure.com"
mock_settings.ai_foundry.model_deployment = "gpt-4o-foundry"
mock_settings.azure_openai.gpt_model = "gpt-4o"
mock_settings.azure_openai.api_version = "2024-02-15"

svc = TitleService()
svc.initialize()

mock_client.assert_called_once_with(
azure_endpoint="https://foundry.openai.azure.com",
model="gpt-4o-foundry",
api_version="2024-02-15",
credential=mock_credential,
)
assert svc._initialized is True