WebEssentz commited on
Commit
c61d9e2
·
1 Parent(s): 68a0b31

Realtime Flow

Browse files
Files changed (1) hide show
  1. src/agent_session/main.py +39 -34
src/agent_session/main.py CHANGED
@@ -87,40 +87,45 @@ async def run_agent_session(room_name: str, agent_token: str):
87
  await room.connect(livekit_url, agent_token)
88
  print("DEBUG: 2. Connection successful.")
89
 
90
- # 1. Instantiate all plugins
91
- vad = VAD.load(min_speech_duration=0.1, min_silence_duration=0.5)
92
- tts = TTS(
93
- voice=VoiceByName(name="Tiktok Fashion Influencer", provider=VoiceProvider.hume),
94
- instant_mode=True,
95
- )
96
- stt = StreamAdapter(stt=STT(model="whisper-large-v3-turbo"), vad=vad)
97
- google_llm = GoogleLLM(model="gemini-2.5-flash")
98
- llm_wrapper = LLMStateWrapper(llm=google_llm, room=room)
99
-
100
- # 2. Create the AgentSession, passing the plugins to the constructor
101
- session = AgentSession(
102
- stt=stt,
103
- llm=llm_wrapper,
104
- tts=tts,
105
- vad=vad,
106
- )
107
-
108
- # 3. Create an instance of your agent
109
- agent = VoiceAssistant()
110
-
111
- # 4. Start the session. This runs the main loop until disconnection.
112
- print("DEBUG: Starting AgentSession...")
113
- # This will internally call agent.on_connected after connecting.
114
- task = asyncio.create_task(session.start(agent=agent, room=room))
115
-
116
- # 5. Generate the initial greeting after the session has started
117
- print("DEBUG: Generating initial greeting...")
118
- await send_agent_state(room, "speaking")
119
- await session.generate_reply(instructions="hello")
120
- print("DEBUG: Initial greeting complete. Agent is now fully operational.")
121
-
122
- # Await the main session task to keep it running
123
- await task
 
 
 
 
 
124
 
125
  except Exception as e:
126
  print(f"FATAL ERROR in agent session: {e}")
 
87
  await room.connect(livekit_url, agent_token)
88
  print("DEBUG: 2. Connection successful.")
89
 
90
+ # Create HTTP session for TTS
91
+ async with aiohttp.ClientSession() as session:
92
+ # 1. Instantiate all plugins
93
+ vad = VAD.load(min_speech_duration=0.1, min_silence_duration=0.5)
94
+ tts = TTS(
95
+ voice=VoiceByName(
96
+ name="Tiktok Fashion Influencer",
97
+ provider=VoiceProvider.hume
98
+ ),
99
+ instant_mode=True,
100
+ http_session=session, # Pass the session here
101
+ )
102
+ stt = StreamAdapter(stt=STT(model="whisper-large-v3-turbo"), vad=vad)
103
+ google_llm = GoogleLLM(model="gemini-2.5-flash")
104
+ llm_wrapper = LLMStateWrapper(llm=google_llm, room=room)
105
+
106
+ # 2. Create the AgentSession
107
+ session_agent = AgentSession(
108
+ stt=stt,
109
+ llm=llm_wrapper,
110
+ tts=tts,
111
+ vad=vad,
112
+ )
113
+
114
+ # 3. Create an instance of your agent
115
+ agent = VoiceAssistant()
116
+
117
+ # 4. Start the session
118
+ print("DEBUG: Starting AgentSession...")
119
+ task = asyncio.create_task(session_agent.start(agent=agent, room=room))
120
+
121
+ # 5. Generate the initial greeting
122
+ print("DEBUG: Generating initial greeting...")
123
+ await send_agent_state(room, "speaking")
124
+ await session_agent.generate_reply(instructions="hello")
125
+ print("DEBUG: Initial greeting complete. Agent is now fully operational.")
126
+
127
+ # Await the main session task
128
+ await task
129
 
130
  except Exception as e:
131
  print(f"FATAL ERROR in agent session: {e}")