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

from signalwire.livewire import Agent, AgentSession, AgentServer, JobContext, run_app
server = AgentServer()
class LoggingAgent(Agent):
async def on_user_turn_completed(self, turn_ctx=None, new_message=None):
print(f"User finished speaking.")
print(f"Conversation history: {self.session.history}")
@server.rtc_session()
async def entrypoint(ctx: JobContext):
await ctx.connect()
agent = LoggingAgent(instructions="You are a helpful assistant.")
session = AgentSession()
await session.start(agent, room=ctx.room)
run_app(server)