from __future__ import annotations import unittest from types import SimpleNamespace from unittest.mock import patch from models import AssistantConfig from services.workflow_router import WorkflowLLMRouter class WorkflowLLMRouterTest(unittest.IsolatedAsyncioTestCase): async def test_uses_required_tool_choice_without_developer_messages(self): requests = [] class FakeCompletions: async def create(self, **kwargs): requests.append(kwargs) return SimpleNamespace( choices=[ SimpleNamespace( message=SimpleNamespace( tool_calls=[ SimpleNamespace( function=SimpleNamespace(name="goto_age", arguments="{}") ) ] ) ) ] ) class FakeClient: def __init__(self, **_kwargs): self.chat = SimpleNamespace(completions=FakeCompletions()) self.closed = False async def close(self): self.closed = True cfg = AssistantConfig( type="workflow", model="deepseek-chat", llm_api_key="secret", llm_base_url="https://llm.test/v1", ) router = WorkflowLLMRouter(cfg) edges = [ { "id": "age", "data": {"condition": "用户已经回答姓名", "priority": 10}, } ] with patch("services.workflow_router.AsyncOpenAI", FakeClient): selected = await router.select_edge( node_name="询问姓名", node_prompt="询问用户姓名", edges=edges, history=[{"role": "user", "message": "我叫李白"}], variables={"customer_type": "new"}, edge_name=lambda _edge: "goto_age", edge_description=lambda _edge: "用户已经回答姓名", ) self.assertEqual(selected, "goto_age") self.assertEqual(requests[0]["tool_choice"], "required") self.assertEqual( [message["role"] for message in requests[0]["messages"]], ["system", "user"], ) self.assertNotIn("developer", str(requests[0]["messages"])) if __name__ == "__main__": unittest.main()