on_user_turn_completed

View as MarkdownOpen in Claude

Lifecycle hook called when the user finishes speaking. Override in a subclass to inspect or modify the conversation before the LLM generates a reply.

Parameters

turn_ctx
AnyDefaults to None

Turn context object. Accepted for API compatibility.

new_message
AnyDefaults to None

The new message from the user. Accepted for API compatibility.

Returns

None

Example

1from signalwire.livewire import Agent, AgentSession, AgentServer, JobContext, run_app
2
3server = AgentServer()
4
5class LoggingAgent(Agent):
6 async def on_user_turn_completed(self, turn_ctx=None, new_message=None):
7 print(f"User finished speaking.")
8 print(f"Conversation history: {self.session.history}")
9
10@server.rtc_session()
11async def entrypoint(ctx: JobContext):
12 await ctx.connect()
13 agent = LoggingAgent(instructions="You are a helpful assistant.")
14 session = AgentSession()
15 await session.start(agent, room=ctx.room)
16
17run_app(server)