Files
replypier/backend/tests/agents/test_chat_agent_transfer_prompt.py
T
WangShen007 20fef2b9f3 chore: settle product naming across app, widget and deployments
One word everywhere: ReplyPier in UI copy, package and image names, the
replypier.site domains (app/api/docs/help), REPLYPIER_* settings and the
--rp-* design tokens.

- public widget API is window.ReplyPier with replypierId / replypierBaseUrl /
  replypierConfig / replypierToken, loader at webclient/replypier.min.js;
  both bundles rebuilt from source
- NOTICE, llms.txt, CONTRIBUTING, HELP_CENTER_INFRA and the demo runtime
  config rewritten; the demo config ships no third-party keys
- migration adds the ReplyPier managed model value to the AI model enum
- local compose project, database, role, volume and Docker network carry the
  same name
2026-10-26 00:57:22 +08:00

89 lines
3.6 KiB
Python

"""
Copyright 2024-2026 ReplyPier
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from unittest.mock import MagicMock, Mock, patch
from uuid import uuid4
import pytest
from sqlalchemy.orm import Session
from app.agents.chat_agent import ChatAgent
def build_agent(transfer_to_human: bool) -> ChatAgent:
"""A ChatAgent with everything external stubbed, to read the prompt it built."""
agent_data = Mock()
agent_data.name = "Support"
agent_data.jira_enabled = False
agent_data.transfer_to_human = transfer_to_human
agent_data.ask_for_rating = False
agent_data.ticketing_enabled = False
agent_data.instructions = ["You are a helpful assistant"]
storage = Mock()
storage.get_session_state.return_value = {"status": "active"}
with patch('app.agents.chat_agent.get_db') as mock_get_db, \
patch('app.agents.chat_agent.JiraRepository') as mock_jira_repo, \
patch('app.tools.knowledge_search_byagent.KnowledgeSearchByAgent', return_value=Mock()), \
patch('app.agents.chat_agent.EncryptedPostgresAgentStorage', return_value=storage), \
patch('app.agents.chat_agent.settings.DATABASE_URL', "mock://test"), \
patch.dict('os.environ', {}, clear=True):
mock_get_db.return_value = iter([MagicMock(spec=Session)])
mock_jira_repo.return_value.get_agent_with_jira_config.return_value = agent_data
return ChatAgent(
api_key="test-key",
model_name="gpt-4",
model_type="openai",
org_id=str(uuid4()),
agent_id=str(uuid4()),
customer_id=str(uuid4()),
session_id=str(uuid4()),
)
def prompt_of(agent: ChatAgent) -> str:
"""The built prompt on one line: the blocks are indented heredocs, so a
sentence is only contiguous once the wrapping whitespace is collapsed."""
instructions = agent.agent.instructions
text = "\n".join(instructions) if isinstance(instructions, list) else instructions
return " ".join((text or "").split())
class TestTransferToHumanPrompt:
"""The switch is a setting on one agent. Said plainly ("transfer to human is
disabled"), the model repeats it back as a fact about the business - an agent
answering questions about its own product then tells customers the product
cannot hand chats to a human, which is false."""
def test_disabled_transfer_is_scoped_to_this_chat(self):
prompt = prompt_of(build_agent(transfer_to_human=False)).lower()
assert "cannot hand this conversation to a human" in prompt
# It must say what this is NOT, or the model generalises it
assert "not a fact about the business or its product" in prompt
# and leave the customer somewhere to go
assert "open a ticket" in prompt
# Capability questions are answered from the knowledge base, not the switch
assert "answer from the knowledge base, not from this setting" in prompt
def test_enabled_transfer_still_offers_the_handover(self):
prompt = prompt_of(build_agent(transfer_to_human=True)).lower()
assert "ability to transfer this conversation to a human agent" in prompt
assert "cannot hand this conversation to a human" not in prompt