- Introduce WorkflowAgentStage to manage agent stage configurations and enhance interaction with the workflow engine. - Implement WorkflowEdgeEvaluator for priority-aware edge evaluation, improving routing decisions based on conditions and user turns. - Update WorkflowBrain to handle user turns and routing more effectively, ensuring agents cannot have only one default path. - Enhance CallEndCoordinator to track speech events and manage call termination based on queued speech. - Add new models and output handling for workflow interactions, improving clarity and maintainability. - Update tests to validate the new routing logic and agent behavior under various scenarios.
78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
from models import AssistantConfig
|
|
from services.workflow.models import RouteStatus
|
|
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.status, RouteStatus.MATCHED)
|
|
self.assertEqual(selected.function_name, "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()
|