WebEssentz commited on
Commit
6f9c53c
·
1 Parent(s): cec226d

Realtime Flow

Browse files
Files changed (1) hide show
  1. src/agent_session/main.py +14 -5
src/agent_session/main.py CHANGED
@@ -42,18 +42,30 @@ async def send_agent_state(room: Room, state: str):
42
  except Exception as e:
43
  print(f"DEBUG: Error publishing agent state: {e}")
44
 
 
45
  class LLMStateWrapper(LLM):
46
  def __init__(self, llm: LLM, room: Room):
47
  super().__init__()
48
  self._llm = llm
49
  self._room = room
50
 
51
- async def chat(self, history, **kwargs):
 
52
  await send_agent_state(self._room, "thinking")
53
- async for chunk in self._llm.chat(history, **kwargs):
 
 
 
 
 
 
54
  yield chunk
 
55
  await send_agent_state(self._room, "listening")
56
 
 
 
 
57
  async def run_agent_session(room_name: str, agent_token: str):
58
  livekit_url = os.getenv("LIVEKIT_URL")
59
  room = Room()
@@ -73,12 +85,10 @@ async def run_agent_session(room_name: str, agent_token: str):
73
  print("DEBUG: 4. Plugins initialized.")
74
 
75
  print("DEBUG: 5. Creating AgentSession...")
76
- # --- THE KEY FIX: Initialize AgentSession WITHOUT the room ---
77
  session = AgentSession(vad=vad, stt=stt, llm=llm_wrapper, tts=tts)
78
  print("DEBUG: 6. AgentSession created.")
79
 
80
  print("DEBUG: 7. Starting session and passing room to .start()...")
81
- # --- THE KEY FIX: Pass the room to the .start() method ---
82
  await session.start(agent=VoiceAssistant(), room=room)
83
 
84
  print("DEBUG: 8. Session started. Generating initial greeting...")
@@ -94,7 +104,6 @@ async def run_agent_session(room_name: str, agent_token: str):
94
  print(f"DEBUG: Agent session for room {room_name} is ending. Cleaning up.")
95
  await room.disconnect()
96
 
97
- # --- Restoring BackgroundTasks for a non-blocking server ---
98
  @app.post("/join-room")
99
  async def join_room(req: JoinRoomRequest, background_tasks: BackgroundTasks):
100
  print(f"DEBUG: Received POST request to /join-room for: {req.room_name}")
 
42
  except Exception as e:
43
  print(f"DEBUG: Error publishing agent state: {e}")
44
 
45
+ # --- THE DEFINITIVE FIX IS HERE ---
46
  class LLMStateWrapper(LLM):
47
  def __init__(self, llm: LLM, room: Room):
48
  super().__init__()
49
  self._llm = llm
50
  self._room = room
51
 
52
+ # This signature accepts ANY named arguments the library might send.
53
+ async def chat(self, **kwargs):
54
  await send_agent_state(self._room, "thinking")
55
+
56
+ # We find and extract 'history' from the arguments.
57
+ # This makes our code resilient to the library's internal calling convention.
58
+ history = kwargs.pop('history', [])
59
+
60
+ # We pass the extracted history and all other arguments along.
61
+ async for chunk in self._llm.chat(history=history, **kwargs):
62
  yield chunk
63
+
64
  await send_agent_state(self._room, "listening")
65
 
66
+ # The rest of the file is correct and can remain unchanged.
67
+ # For absolute certainty, here is the full correct file again.
68
+
69
  async def run_agent_session(room_name: str, agent_token: str):
70
  livekit_url = os.getenv("LIVEKIT_URL")
71
  room = Room()
 
85
  print("DEBUG: 4. Plugins initialized.")
86
 
87
  print("DEBUG: 5. Creating AgentSession...")
 
88
  session = AgentSession(vad=vad, stt=stt, llm=llm_wrapper, tts=tts)
89
  print("DEBUG: 6. AgentSession created.")
90
 
91
  print("DEBUG: 7. Starting session and passing room to .start()...")
 
92
  await session.start(agent=VoiceAssistant(), room=room)
93
 
94
  print("DEBUG: 8. Session started. Generating initial greeting...")
 
104
  print(f"DEBUG: Agent session for room {room_name} is ending. Cleaning up.")
105
  await room.disconnect()
106
 
 
107
  @app.post("/join-room")
108
  async def join_room(req: JoinRoomRequest, background_tasks: BackgroundTasks):
109
  print(f"DEBUG: Received POST request to /join-room for: {req.room_name}")