feat: LLM-driven driver message cleanup for phone numbers and names
Browse files- Add driver_message parameter to add_trip_by_driver tool schema
- Update system_driver.md prompt to instruct LLM to clean messages
(remove phone numbers, surrounding text, and driver names)
- Update handler to use LLM-provided driver_message
- Add app/utils/phone.py with regex-based phone number detection
- Add tests/test_phone.py with 25 test cases
- Add test_add_trip_by_driver_uses_llm_cleaned_message test
Files changed (6)
242 unmodified lines243244245246247248242 unmodified lines"minimum": 0,"description": "Optional trip price.",},},"required": ["departure", "destination", "departure_date", "departure_time"],"additionalProperties": False,242 unmodified lines243244245246247248249250251252253254255256242 unmodified lines"minimum": 0,"description": "Optional trip price.",},"driver_message": {"type": "string","description": ("Optional cleaned version of the driver's original message. ""Remove phone numbers, personal info, and driver name. ""Keep emojis and decorations. Only set when >2 emojis."),},},"required": ["departure", "destination", "departure_date", "departure_time"],"additionalProperties": False,
586 unmodified lines587588589590591592593586 unmodified linesif self.current_message:raw_msg = self.current_message.get("message") or ""if count_emojis(raw_msg) > 2:driver_message = raw_msguse_driver_message = Truetrip = await self.repository.create_driver_trip(586 unmodified lines587588589590591592593594586 unmodified linesif self.current_message:raw_msg = self.current_message.get("message") or ""if count_emojis(raw_msg) > 2:llm_message = _optional_string(arguments.get("driver_message"))driver_message = llm_message or raw_msguse_driver_message = Truetrip = await self.repository.create_driver_trip(
1234567891011121314151617181920212223242526272829import re
_PHONE_RE = re.compile( r"(?:\+|00)\d[\d\s\-\.\(\)]{6,18}\d")
_LOCAL_PHONE_RE = re.compile( r"(?<!\d)" r"(?:\d[\d\s\-\.\(\)]{7,18}\d)" r"(?!\d)")
def count_digits(text: str) -> int: return sum(1 for c in text if c.isdigit())
def strip_phone_numbers(text: str, replacement: str = "[ุฑูู
ุงููุงุชู]") -> str: if not text: return text
def _replace(match: re.Match) -> str: if count_digits(match.group()) >= 7: return replacement return match.group()
result = _PHONE_RE.sub(_replace, text) result = _LOCAL_PHONE_RE.sub(_replace, result) return result7 unmodified lines89107 unmodified lines- For delete/modify: initiate_trip_action (sends numbered list). Driver replies with number.- delete_trip_by_number(trip_number) / modify_trip_by_number(trip_number, field, value). No trip_id.- To search/book as traveler: switch_to_passenger (name optional).7 unmodified lines891011121314151617187 unmodified lines- For delete/modify: initiate_trip_action (sends numbered list). Driver replies with number.- delete_trip_by_number(trip_number) / modify_trip_by_number(trip_number, field, value). No trip_id.- To search/book as traveler: switch_to_passenger (name optional).Driver message cleanup:- When the driver's message has more than 2 emojis, set `driver_message` in add_trip_by_driver.- Remove ALL phone numbers AND the surrounding text (e.g. "ููุชูุงุตู ุงูุงุชุตุงู ุนูู ุงูุฑูู ุงูุขุชู๐:-770026665" โ remove entirely).- Remove the driver's name wherever it appears in the message.- Keep emojis, decorative elements, route info, dates, times, and seat info.- Example: "๐ุจุต ููุฏ ู ุฑูุญ ๐. ูุงุฒู ุงูุฌู ุนุฉ ูง ุตุจุงุญุง ๐ุชุฑูู ุงูู ููุง๐ ุจู ุจูุฑ ๐770026665 ๐ุจู ุจูุฑ๐"โ "๐ุจุต ููุฏ ู ุฑูุญ ๐. ูุงุฒู ุงูุฌู ุนุฉ ูง ุตุจุงุญุง ๐ุชุฑูู ุงูู ููุง๐ ๐ซต๐ป ๐ ๐"
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100from app.utils.phone import count_digits, strip_phone_numbers
class TestCountDigits: def test_no_digits(self): assert count_digits("hello world") == 0
def test_all_digits(self): assert count_digits("1234567") == 7
def test_mixed(self): assert count_digits("abc123def456") == 6
def test_empty(self): assert count_digits("") == 0
class TestStripPhoneNumbers: def test_yemeni_number_with_plus(self): result = strip_phone_numbers("ุงุชุตู ุนูู +967712345678") assert result == "ุงุชุตู ุนูู [ุฑูู
ุงููุงุชู]"
def test_yemeni_number_with_00(self): result = strip_phone_numbers("ุงุชุตู ุนูู 00967712345678") assert result == "ุงุชุตู ุนูู [ุฑูู
ุงููุงุชู]"
def test_international_with_dashes(self): result = strip_phone_numbers("Call +1-234-567-8901") assert result == "Call [ุฑูู
ุงููุงุชู]"
def test_international_with_spaces(self): result = strip_phone_numbers("Call +44 20 7946 0958") assert result == "Call [ุฑูู
ุงููุงุชู]"
def test_international_with_dots(self): result = strip_phone_numbers("Call +49.30.1234567") assert result == "Call [ุฑูู
ุงููุงุชู]"
def test_international_with_parentheses(self): result = strip_phone_numbers("Call +1 (234) 567-8901") assert result == "Call [ุฑูู
ุงููุงุชู]"
def test_multiple_numbers(self): result = strip_phone_numbers("Call +967712345678 or +967776543210") assert result == "Call [ุฑูู
ุงููุงุชู] or [ุฑูู
ุงููุงุชู]"
def test_no_phone_numbers(self): text = "ุฑุญูุฉ ู
ู ุตูุนุงุก ุฅูู ุชุนุฒ" result = strip_phone_numbers(text) assert result == text
def test_short_number_not_matched(self): result = strip_phone_numbers("Room 123") assert result == "Room 123"
def test_empty_string(self): assert strip_phone_numbers("") == ""
def test_none_returns_none(self): assert strip_phone_numbers(None) is None
def test_custom_replacement(self): result = strip_phone_numbers("Call +967712345678", replacement="***") assert result == "Call ***"
def test_number_at_start(self): result = strip_phone_numbers("+967712345678 ุงุชุตู") assert result == "[ุฑูู
ุงููุงุชู] ุงุชุตู"
def test_number_at_end(self): result = strip_phone_numbers("ุงุชุตู +967712345678") assert result == "ุงุชุตู [ุฑูู
ุงููุงุชู]"
def test_number_in_middle_of_text(self): result = strip_phone_numbers("ุงุชุตู ุนูู +967712345678 ููุญุฌุฒ") assert result == "ุงุชุตู ุนูู [ุฑูู
ุงููุงุชู] ููุญุฌุฒ"
def test_yemeni_local_format(self): result = strip_phone_numbers("0712345678") assert result == "[ุฑูู
ุงููุงุชู]"
def test_number_with_country_code_no_plus(self): result = strip_phone_numbers("967712345678") assert result == "[ุฑูู
ุงููุงุชู]"
def test_long_number_with_separators(self): result = strip_phone_numbers("+1 (555) 123-4567 ext 890") assert result == "[ุฑูู
ุงููุงุชู] ext 890"
def test_local_yemeni_9_digit(self): result = strip_phone_numbers("770026665") assert result == "[ุฑูู
ุงููุงุชู]"
def test_local_yemeni_in_message(self): result = strip_phone_numbers("ููุชูุงุตู ุนูู ุงูุฑูู
770026665") assert result == "ููุชูุงุตู ุนูู ุงูุฑูู
[ุฑูู
ุงููุงุชู]"
def test_local_yemeni_with_dash_prefix(self): result = strip_phone_numbers("ุงูุฑูู
:-770026665") assert result == "ุงูุฑูู
:-[ุฑูู
ุงููุงุชู]"624 unmodified lines625626627628629630624 unmodified linesassert created_trip["driver_message"] == "ุจุงุต ู ู ุนุฏู ุงูู ุงูู ููุง ๐๐๐"@pytest.mark.asyncioasync def test_add_trip_by_driver_no_driver_message_with_few_emojis():repository = FakeRepository()624 unmodified lines625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667624 unmodified linesassert created_trip["driver_message"] == "ุจุงุต ู ู ุนุฏู ุงูู ุงูู ููุง ๐๐๐"@pytest.mark.asyncioasync def test_add_trip_by_driver_uses_llm_cleaned_message():repository = FakeRepository()repository.drivers_by_remote_jid["967700000010"] = {"id": "driver-1","name": "Ali","remoteJid": "967700000010",}repository.driver_cars_by_driver["driver-1"] = [{"id": "car-1", "car_type": "SUV", "seat_count": 4},]raw_msg = "๐ุจุต ููุฏ ู ุฑูุญ ๐. ูุงุฒู ุงูุฌู ุนุฉ ูง ุตุจุงุญุง ๐ุชุฑูู ุงูู ููุง๐ ุจู ุจูุฑ ๐770026665 ๐ุจู ุจูุฑ๐"cleaned_msg = "๐ุจุต ููุฏ ู ุฑูุญ ๐. ูุงุฒู ุงูุฌู ุนุฉ ูง ุตุจุงุญุง ๐ุชุฑูู ุงูู ููุง๐ ๐"current_message = {"message": raw_msg}handlers = make_handlers(repository=repository, sender_phone="967700000010")handlers.current_message = current_messageresult = await handlers.add_trip_by_driver({"departure": "ุชุฑูู ","destination": "ุงูู ููุง","departure_date": "2026-06-10","departure_time": "morning","vehicle_type": "SUV","available_seats": 2,"total_seats": 4,"price": 50,"driver_message": cleaned_msg,})assert result.ok is Truecreated_trip = repository.created_trips[-1]assert created_trip["use_driver_message"] is Trueassert created_trip["driver_message"] == cleaned_msg@pytest.mark.asyncioasync def test_add_trip_by_driver_no_driver_message_with_few_emojis():repository = FakeRepository()
app/ai/tool_schemas.py+8
app/tools/handlers.py+2-1
app/utils/phone.pyAdded+29
prompts/system_driver.md+8
tests/test_phone.pyAdded+100
tests/test_tools.py+37