From f2fcbe485f6cf097149e8b39a9dffbbb1e079662 Mon Sep 17 00:00:00 2001 From: Xin Wang Date: Tue, 16 Dec 2025 17:31:17 +0800 Subject: [PATCH] return random phone number and id card number --- agents/my_basic_agent_1_2_9.py | 66 ++++++++--- src/components/playground/PhoneSimulator.tsx | 117 +++++++++++-------- 2 files changed, 115 insertions(+), 68 deletions(-) diff --git a/agents/my_basic_agent_1_2_9.py b/agents/my_basic_agent_1_2_9.py index 0c6097a..fd2b0eb 100644 --- a/agents/my_basic_agent_1_2_9.py +++ b/agents/my_basic_agent_1_2_9.py @@ -4,6 +4,7 @@ import base64 import json import logging import os +import random import sys import re from dataclasses import asdict, dataclass @@ -104,6 +105,8 @@ DEFAULT_INSTRUCTIONS = """# 角色 - 你已经说过下面的开场白所以不需要重复说:“您好,这里是无锡交警,我将为您远程处理交通事故。请将人员撤离至路侧安全区域,开启危险报警双闪灯、放置三角警告牌、做好安全防护,谨防二次事故伤害。若您已经准备好了,请点击继续办理,如需人工服务,请说转人工。” """ +DEFAULT_TALKING_MODE = 'push_to_talk' + # ## 黄金对话路径示例 (GOLDEN_CONVERSATION_PATH) # ``` @@ -606,11 +609,15 @@ class MyAgent(Agent): f"│ plate: \"{normalized_plate}\"\n" "└───────────────" ) - # Dummy fixed response (placeholder backend) + # Generate random mobile number (11 digits: 1[3-9] + 9 random digits) + mobile_prefix = random.choice(['13', '14', '15', '16', '17', '18', '19']) + mobile_suffix = ''.join([str(random.randint(0, 9)) for _ in range(9)]) + random_mobile = f"{mobile_prefix}{mobile_suffix}" + return { "success": True, "plate": normalized_plate, - "mobile": "13800001234", + "mobile": random_mobile, } @function_tool() @@ -631,11 +638,24 @@ class MyAgent(Agent): f"│ plate: \"{normalized_plate}\"\n" "└───────────────" ) - # Dummy fixed response (placeholder backend) + # Generate random ID card number (18 digits: 6-digit area code + 8-digit birth date + 3-digit sequence + 1 check digit) + # Area code: random 6 digits (typically 110000-659999 for Chinese ID cards) + area_code = random.randint(110000, 659999) + # Birth date: random date between 1950-01-01 and 2000-12-31 + year = random.randint(1950, 2000) + month = random.randint(1, 12) + day = random.randint(1, 28) # Use 28 to avoid month-specific day issues + birth_date = f"{year:04d}{month:02d}{day:02d}" + # Sequence number: 3 random digits + sequence = random.randint(100, 999) + # Check digit: random digit or X (10% chance of X) + check_digit = 'X' if random.random() < 0.1 else str(random.randint(0, 9)) + random_id_card = f"{area_code}{birth_date}{sequence}{check_digit}" + return { "success": True, "plate": normalized_plate, - "id_card": "320101198001011234", + "id_card": random_id_card, } @function_tool() @@ -749,7 +769,7 @@ class MyAgent(Agent): raise ToolError(f"Unable to hang up call: {str(e)}") @function_tool() - async def ask_important_question(self, context: RunContext, message: str, options: Optional[List[str]] = None): + async def ask_important_question(self, context: RunContext, message: str, options: Optional[List[str]] | str = None): """询问关键问题并等待用户选择选项,返回用户的选择结果。 参数: @@ -759,7 +779,12 @@ class MyAgent(Agent): 返回: str: 用户选择的文本内容。 """ - await self._send_chat_message(f"🔨 Call: ask_important_question\n • message: \"{message}\"\n • options: {options}") + await self._send_chat_message( + "┌─🔨 Call: ask_important_question\n" + f"│ message: \"{message}\"\n" + f"│ options: {options}\n" + "└───────────────" + ) try: room = get_job_context().room participant_identity = next(iter(room.remote_participants)) @@ -805,7 +830,11 @@ class MyAgent(Agent): user_selection = response_data.get("selection", "确认") logger.info(f"User selected: {user_selection}") - await self._send_chat_message(f"✅ Result: ask_important_question\n • selection: \"{user_selection}\"") + await self._send_chat_message( + "┌─✅ Result: ask_important_question\n" + f"│ selection: \"{user_selection}\"\n" + "└───────────────" + ) return f"用户选择了: {user_selection}" except json.JSONDecodeError: logger.error(f"Failed to parse response: {response}") @@ -1015,10 +1044,11 @@ async def entrypoint(ctx: JobContext, avatar_dispatcher_url: str = None, vision_ ) # disable input audio at the start - session.input.set_audio_enabled(False) - - # Track current audio state for mode switching - _audio_enabled_state = False + _talking_mode = DEFAULT_TALKING_MODE + if _talking_mode == "push_to_talk": + session.input.set_audio_enabled(False) + else: + session.input.set_audio_enabled(True) @ctx.room.local_participant.register_rpc_method("start_turn") async def start_turn(data: rtc.RpcInvocationData): @@ -1058,13 +1088,13 @@ async def entrypoint(ctx: JobContext, avatar_dispatcher_url: str = None, vision_ @ctx.room.local_participant.register_rpc_method("switch_ptt_and_rt") async def switch_ptt_and_rt(data: rtc.RpcInvocationData): - nonlocal _audio_enabled_state - # Toggle audio input state - _audio_enabled_state = not _audio_enabled_state - session.input.set_audio_enabled(_audio_enabled_state) - mode = "push-to-talk" if not _audio_enabled_state else "realtime" - logger.info(f"Switched to {mode} mode (audio enabled: {_audio_enabled_state})") - return json.dumps({"success": True, "mode": mode, "audio_enabled": _audio_enabled_state}) + nonlocal _talking_mode + _talking_mode = "push_to_talk" if _talking_mode == "realtime" else "realtime" + if _talking_mode == "push_to_talk": + session.input.set_audio_enabled(False) + else: + session.input.set_audio_enabled(True) + return json.dumps({"success": True, "mode": _talking_mode}) if __name__ == "__main__": parser = argparse.ArgumentParser() diff --git a/src/components/playground/PhoneSimulator.tsx b/src/components/playground/PhoneSimulator.tsx index 8eb9f03..797152c 100644 --- a/src/components/playground/PhoneSimulator.tsx +++ b/src/components/playground/PhoneSimulator.tsx @@ -1063,61 +1063,66 @@ export function PhoneSimulator({ )} {/* Push-to-Talk Mode Layout */} - {isPushToTalkMode && phoneMode !== "important_message" && phoneMode !== "hand_off" && voiceAssistant.agent && ( + {isPushToTalkMode && phoneMode !== "hand_off" && voiceAssistant.agent && (
- {/* Camera Switch Button - Left */} -
+ {/* Camera Switch Button - Left (hidden in important_message mode) */} + {phoneMode !== "important_message" && ( +
+ + {showCameraMenu && ( +
+ {cameras.length === 0 ? ( +
+ No cameras found +
+ ) : ( + cameras.map((device) => ( + + )) + )} +
+ )} +
+ )} + + {/* Large Push-to-Talk Button - Center (hidden in important_message mode) */} + {phoneMode !== "important_message" && ( - {showCameraMenu && ( -
- {cameras.length === 0 ? ( -
- No cameras found -
- ) : ( - cameras.map((device) => ( - - )) - )} -
- )} -
+ )} - {/* Large Push-to-Talk Button - Center */} - - - {/* End Call Button - Right */} + {/* End Call Button - Right (always shown in PTT mode) */}
)} + + {/* Hand Off Mode - Show only End Call Button */} + {phoneMode === "hand_off" && ( +
+ +
+ )} )