5050from utils .config_manager import get_config_manager
5151from utils .tokenize import truncate_to_tokens as _tt
5252from main_logic .agent_event_bus import AgentServerEventBridge
53+ from plugin .server .application .plugins .voice_contracts import (
54+ VOICE_TRANSCRIPT_EVENT_ID ,
55+ VOICE_TRANSCRIPT_EVENT_TYPE ,
56+ arbitrate_voice_transcript_results ,
57+ voice_transcript_custom_event_args ,
58+ voice_transcript_noop ,
59+ voice_transcript_request_has_text ,
60+ )
5361try :
5462 from brain .computer_use import ComputerUseAdapter
5563 from brain .browser_use_adapter import BrowserUseAdapter
@@ -1651,83 +1659,11 @@ async def _emit_agent_status_update(lanlan_name: Optional[str] = None) -> None:
16511659 pass
16521660
16531661
1654- VOICE_TRANSCRIPT_CUSTOM_EVENT_TYPE = "voice_transcript"
1655- VOICE_TRANSCRIPT_CUSTOM_EVENT_ID = "handle_transcript"
16561662VOICE_TRANSCRIPT_CUSTOM_EVENT_TIMEOUT_SECONDS = 1.0
16571663
16581664
1659- def _voice_bridge_noop (reason : str , ** extra : object ) -> Dict [str , Any ]:
1660- return {
1661- "action" : "noop" ,
1662- "reason" : str (reason or "noop" ),
1663- ** extra ,
1664- }
1665-
1666-
1667- def _voice_transcript_request_has_text (event : Mapping [str , object ] | None ) -> bool :
1668- if not isinstance (event , Mapping ):
1669- return False
1670- return bool (str (event .get ("transcript" ) or "" ).strip ())
1671-
1672-
1673- def _voice_transcript_custom_event_args (event : Mapping [str , object ]) -> Dict [str , object ]:
1674- metadata = event .get ("metadata" )
1675- return {
1676- "transcript" : str (event .get ("transcript" ) or "" ).strip (),
1677- "lanlan_name" : str (event .get ("lanlan_name" ) or "" ),
1678- "metadata" : dict (metadata ) if isinstance (metadata , Mapping ) else {},
1679- }
1680-
1681-
16821665def _voice_bridge_action_from_dispatch_results (dispatch_results : object ) -> Dict [str , Any ]:
1683- if not isinstance (dispatch_results , list ) or not dispatch_results :
1684- return _voice_bridge_noop ("no_subscribers" )
1685-
1686- from plugin .server .application .plugins .voice_contracts import (
1687- arbitrate_voice_transcript_results ,
1688- )
1689-
1690- arbitration_items : list [dict [str , object ]] = []
1691- failure_count = 0
1692- for item in dispatch_results :
1693- if not isinstance (item , Mapping ):
1694- continue
1695- if not bool (item .get ("success" )):
1696- failure_count += 1
1697- continue
1698- result = item .get ("result" )
1699- if not isinstance (result , Mapping ):
1700- continue
1701- action = str (result .get ("action" ) or "" ).strip ()
1702- if not action :
1703- continue
1704- payload : Dict [str , Any ] = dict (result )
1705- payload ["action" ] = action
1706- plugin_id = str (item .get ("plugin_id" ) or "" ).strip ()
1707- if plugin_id :
1708- payload .setdefault ("source_plugin" , plugin_id )
1709- source_event_id = str (item .get ("event_id" ) or "" ).strip ()
1710- if source_event_id :
1711- payload .setdefault ("source_event_id" , source_event_id )
1712- arbitration_items .append (
1713- {
1714- "plugin_id" : payload .get ("source_plugin" ) or plugin_id ,
1715- "event_id" : payload .get ("source_event_id" ) or source_event_id ,
1716- "success" : True ,
1717- "result" : payload ,
1718- }
1719- )
1720-
1721- if not arbitration_items :
1722- return _voice_bridge_noop ("no_handler_result" , failures = failure_count )
1723- payload = arbitrate_voice_transcript_results (arbitration_items )
1724- if failure_count :
1725- try :
1726- existing_failures = int (payload .get ("failures" ) or 0 )
1727- except (TypeError , ValueError ):
1728- existing_failures = 0
1729- payload ["failures" ] = existing_failures + failure_count
1730- return payload
1666+ return arbitrate_voice_transcript_results (dispatch_results )
17311667
17321668
17331669async def _dispatch_voice_transcript_custom_event (
@@ -1736,9 +1672,9 @@ async def _dispatch_voice_transcript_custom_event(
17361672 from plugin .server .application .plugins .dispatch_service import PluginDispatchService
17371673
17381674 dispatch_results = await PluginDispatchService ().trigger_custom_event_subscribers (
1739- event_type = VOICE_TRANSCRIPT_CUSTOM_EVENT_TYPE ,
1740- event_id = VOICE_TRANSCRIPT_CUSTOM_EVENT_ID ,
1741- args = _voice_transcript_custom_event_args (event ),
1675+ event_type = VOICE_TRANSCRIPT_EVENT_TYPE ,
1676+ event_id = VOICE_TRANSCRIPT_EVENT_ID ,
1677+ args = voice_transcript_custom_event_args (event ),
17421678 timeout = VOICE_TRANSCRIPT_CUSTOM_EVENT_TIMEOUT_SECONDS ,
17431679 )
17441680 return _voice_bridge_action_from_dispatch_results (dispatch_results )
@@ -1747,24 +1683,19 @@ async def _dispatch_voice_transcript_custom_event(
17471683async def _handle_voice_transcript_request (event : Dict [str , Any ]) -> None :
17481684 event_id = str ((event or {}).get ("event_id" ) or "" )
17491685 lanlan_name = (event or {}).get ("lanlan_name" )
1750- result : Dict [str , Any ] = _voice_bridge_noop ("unavailable" )
1686+ result : Dict [str , Any ] = voice_transcript_noop ("unavailable" )
17511687
17521688 try :
1753- if not _voice_transcript_request_has_text (event ):
1754- result = _voice_bridge_noop ("empty_transcript" )
1689+ if not voice_transcript_request_has_text (event ):
1690+ result = voice_transcript_noop ("empty_transcript" )
17551691 elif not Modules .analyzer_enabled :
1756- result = _voice_bridge_noop ("agent_disabled" )
1692+ result = voice_transcript_noop ("agent_disabled" )
17571693 elif not Modules .agent_flags .get ("user_plugin_enabled" , False ):
1758- result = _voice_bridge_noop ("user_plugin_disabled" )
1694+ result = voice_transcript_noop ("user_plugin_disabled" )
1695+ elif not bool (Modules .plugin_lifecycle_started ):
1696+ result = voice_transcript_noop ("plugin_warming_up" )
17591697 else :
1760- lifecycle_ready = bool (Modules .plugin_lifecycle_started )
1761- if not lifecycle_ready :
1762- lifecycle_ready = await _ensure_plugin_lifecycle_started ()
1763-
1764- if not lifecycle_ready :
1765- result = _voice_bridge_noop ("plugin_lifecycle_start_failed" )
1766- else :
1767- result = await _dispatch_voice_transcript_custom_event (event )
1698+ result = await _dispatch_voice_transcript_custom_event (event )
17681699 except asyncio .CancelledError :
17691700 raise
17701701 except Exception as exc :
@@ -1774,11 +1705,10 @@ async def _handle_voice_transcript_request(event: Dict[str, Any]) -> None:
17741705 lanlan_name ,
17751706 exc ,
17761707 )
1777- result = {
1778- "action" : "noop" ,
1779- "reason" : "dispatch_failed" ,
1780- "error_type" : type (exc ).__name__ ,
1781- }
1708+ result = voice_transcript_noop (
1709+ "dispatch_failed" ,
1710+ error_type = type (exc ).__name__ ,
1711+ )
17821712
17831713 await _emit_main_event (
17841714 "voice_bridge_result" ,
0 commit comments