97 lines
3.3 KiB
Python
97 lines
3.3 KiB
Python
|
|
from app.schemas.steward import StewardRuntimeDecisionRequest
|
||
|
|
from app.services.steward_runtime_decision_agent import (
|
||
|
|
STEWARD_RUNTIME_DECISION_FUNCTION_NAME,
|
||
|
|
StewardRuntimeDecisionAgent,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class _FakeToolCall:
|
||
|
|
def __init__(self, name, arguments):
|
||
|
|
self.name = name
|
||
|
|
self.arguments = arguments
|
||
|
|
|
||
|
|
|
||
|
|
class _FakeRuntimeResult:
|
||
|
|
def __init__(self, tool_call=None):
|
||
|
|
self.tool_call = tool_call
|
||
|
|
|
||
|
|
def calls_as_dicts(self):
|
||
|
|
return [{"tool": self.tool_call.name if self.tool_call else ""}]
|
||
|
|
|
||
|
|
|
||
|
|
class _FakeRuntime:
|
||
|
|
def __init__(self, payload):
|
||
|
|
self.payload = payload
|
||
|
|
self.last_messages = []
|
||
|
|
self.last_tools = []
|
||
|
|
self.last_tool_choice = None
|
||
|
|
|
||
|
|
def complete_with_tool_call(self, messages, tools, tool_choice, **kwargs):
|
||
|
|
self.last_messages = messages
|
||
|
|
self.last_tools = tools
|
||
|
|
self.last_tool_choice = tool_choice
|
||
|
|
if self.payload is None:
|
||
|
|
return _FakeRuntimeResult()
|
||
|
|
return _FakeRuntimeResult(_FakeToolCall(STEWARD_RUNTIME_DECISION_FUNCTION_NAME, self.payload))
|
||
|
|
|
||
|
|
|
||
|
|
def test_steward_runtime_decision_uses_function_calling_context():
|
||
|
|
runtime = _FakeRuntime(
|
||
|
|
{
|
||
|
|
"next_action": "submit_current_application",
|
||
|
|
"target_task_id": "task-application-beijing",
|
||
|
|
"target_message_id": "msg-application-preview",
|
||
|
|
"field_key": "",
|
||
|
|
"field_value": "",
|
||
|
|
"confirmation_required": False,
|
||
|
|
"question": "",
|
||
|
|
"response_text": "",
|
||
|
|
"rationale": "用户确认当前申请核对表无误。",
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
result = StewardRuntimeDecisionAgent(runtime).decide(
|
||
|
|
StewardRuntimeDecisionRequest(
|
||
|
|
user_message="确认",
|
||
|
|
runtime_state={
|
||
|
|
"waiting_for": "application_submit_confirmation",
|
||
|
|
"pending_application": {
|
||
|
|
"message_id": "msg-application-preview",
|
||
|
|
"task_id": "task-application-beijing",
|
||
|
|
"ready_to_submit": True,
|
||
|
|
},
|
||
|
|
"remaining_tasks": [
|
||
|
|
{"task_id": "task-reimbursement-meal", "task_type": "reimbursement"}
|
||
|
|
],
|
||
|
|
},
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
assert result.decision_source == "llm_function_call"
|
||
|
|
assert result.next_action == "submit_current_application"
|
||
|
|
assert result.target_message_id == "msg-application-preview"
|
||
|
|
assert result.target_task_id == "task-application-beijing"
|
||
|
|
assert runtime.last_tool_choice["function"]["name"] == STEWARD_RUNTIME_DECISION_FUNCTION_NAME
|
||
|
|
assert "runtime_state" in runtime.last_messages[-1]["content"]
|
||
|
|
|
||
|
|
|
||
|
|
def test_steward_runtime_decision_fallback_keeps_current_context():
|
||
|
|
runtime = _FakeRuntime(None)
|
||
|
|
|
||
|
|
result = StewardRuntimeDecisionAgent(runtime).decide(
|
||
|
|
StewardRuntimeDecisionRequest(
|
||
|
|
user_message="确认",
|
||
|
|
runtime_state={
|
||
|
|
"pending_steward_action": {
|
||
|
|
"message_id": "msg-next-task",
|
||
|
|
"target_task_id": "task-reimbursement-meal",
|
||
|
|
}
|
||
|
|
},
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
assert result.decision_source == "rule_fallback"
|
||
|
|
assert result.next_action == "continue_next_task"
|
||
|
|
assert result.target_message_id == "msg-next-task"
|
||
|
|
assert result.target_task_id == "task-reimbursement-meal"
|