How can I retrieve the call summary of a session e...
# ask-ai
m
How can I retrieve the call summary of a session even if the user disconnects unexpectedly
t
Thanks for reaching out, Rajadurai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I completely understand how important it is to retrieve the call summary, even if the user disconnects unexpectedly. A great way to handle this is by using the session's close event. This event helps capture the entire chat history—including all messages, function calls, and outputs—so nothing gets lost, even during abrupt disconnections. Here’s a simple way to set it up: 1. First, set up a close event listener on the session to catch the chat history when the session ends. For example:
Copy code
@session.on("close")
def on_close(ev: CloseEvent):
    print("Session closed, reason:", ev.reason)
    for item in session.history.items:
        if item.type == "message":
            print(f"{item.role}: {item.text_content}")
        elif item.type == "function_call":
            print(f"Function call: {item.name} with arguments {item.arguments}")
        elif item.type == "function_call_output":
            print(f"Function output: {item.name} => {item.output}")
1. Make sure automatic session closure is enabled (this is usually the default) so the history is preserved when disconnections happen. 2. Optionally, you can also process the chat history programmatically—for example, summarize it with an LLM or save it for future reference. This approach really helps ensure you have access to the full session data, no matter how the disconnection occurs. If you have any questions or need further help setting this up, feel free to ask—I’m here for you! Sources: examples/voice_agents/multi_agent.py | Understanding the STATE_MISMATCH Disconnect Reason | How to capture real-time agent transcriptions during conversations | Agent-assisted warm transfer | LiveKit Docs
m
I want ask the my Agent to give the summary of the session. when closing
t
I'm happy to guide you through how to ask your Agent to provide a session summary when closing. It’s a great way to wrap things up clearly! Here’s a simple process you can follow: 1. Use a Summary Agent: You can create a specialized agent that generates and shares the session summary right before closing. For example:
Copy code
class SummaryAgent(BaseAgent):
    def __init__(self, job_context: JobContext):
        super().__init__(
            job_context=job_context,
            instructions="Summarize all responses and end the session."
        )

    async def on_enter(self):
        summary = "Thank you for the session! Here is a summary:\n"
        for item in self.session.history.items:
            if item.type == "message":
                summary += f"- {item.role}: {item.text_content}\n"
        await self.session.say(summary)
        await self.session.aclose()
1. Use the
on_exit
Callback:
Another option is to override the
on_exit
method in your agent to generate and deliver the summary just before the session closes. 2. Access Chat History: You can use
session.history.items
to gather all messages and interactions for a complete summary. 3. Close the Session: Once the summary is delivered, simply call
await session.aclose()
to end the session smoothly. Following these steps helps ensure your user feels well-informed and appreciated with a clear summary before the session wraps up. If you have any questions or need further help, feel free to ask! Sources: flows/multi_stage_flow.py | README.md