hallowed-book-84502
08/21/2025, 9:23 AMtall-airline-16756
08/21/2025, 9:24 AMtall-airline-16756
08/21/2025, 9:25 AMon_conversation_item_added function to save conversations in files. This handy function listens for the conversation_item_added event, which happens whenever something new is added to the chat history. From there, you can set up your own logic to save those items to a file.
If you'd like, I’m happy to share more details or examples to help you get started—just let me know!hallowed-book-84502
08/21/2025, 9:25 AMtall-airline-16756
08/21/2025, 9:26 AMon_conversation_item_added event in LiveKit Cloud. This handy event lets you grab each conversation item as it pops up in the chat history, so you can pull out the info you need and store it right away.
Here’s a simple way to get started:
• Set up an event listener for conversation_item_added to catch those conversation items as they come in.
• Pull out the key details from the event, like role, text_content, and other types of content (think images or audio).
• Use your favorite database library (like SQLAlchemy) to save everything asynchronously—this way, your agent keeps running smoothly without any hiccups.
If you'd like, I can share a detailed code example or help you with the database setup. Just give me a shout!hallowed-book-84502
08/21/2025, 9:27 AMtall-airline-16756
08/21/2025, 9:27 AMon_conversation_item_added event in LiveKit Cloud. We’re using SQLAlchemy here for database operations, but feel free to tweak it to fit your favorite database library:
import asyncio
from datetime import datetime
from livekit.agents import ConversationItemAddedEvent
from sqlalchemy import create_engine, Column, String, Text, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# Database setup
Base = declarative_base()
engine = create_engine('sqlite:///conversations.db') # Swap this out with your own database URL
Session = sessionmaker(bind=engine)
db_session = Session()
# Define a table for storing conversation data
class ConversationItem(Base):
__tablename__ = 'conversation_items'
id = Column(String, primary_key=True)
role = Column(String)
text_content = Column(Text)
timestamp = Column(DateTime, default=datetime.utcnow)
# Create the table
Base.metadata.create_all(engine)
# Function to save conversation item to the database
async def save_to_database(chat_message):
try:
conversation_item = ConversationItem(
id=chat_message.id, # Make sure each message has a unique ID
role=chat_message.role,
text_content=chat_message.text_content
)
db_session.add(conversation_item)
db_session.commit()
except Exception as e:
print(f"Error saving to database: {e}")
# Event listener for conversation_item_added
@session.on("conversation_item_added")
def on_conversation_item_added(event: ConversationItemAddedEvent):
asyncio.create_task(save_to_database(event.item))
Here’s what this example covers:
• Setting up a database table to keep your conversation items organized.
• Saving important details like the role and text content into the database.
• Handling database operations asynchronously so your app keeps running smoothly.
If you want to customize this further or have any specific needs, just give me a shout—I’m here to help!hallowed-book-84502
08/21/2025, 9:28 AMtall-airline-16756
08/21/2025, 9:29 AMhallowed-book-84502
08/21/2025, 9:30 AMtall-airline-16756
08/21/2025, 9:30 AM