from conversimple import ConversimpleAgent, tool
class LifecycleAgent(ConversimpleAgent):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.active_conversations = {}
def on_conversation_started(self, conversation_id: str):
"""Conversation begins"""
self.active_conversations[conversation_id] = {
"start_time": datetime.now(),
"tool_calls": 0,
"context": {}
}
print(f"🎤 Started: {conversation_id}")
def on_tool_called(self, tool_call):
"""Tool is about to execute"""
conv_id = tool_call.conversation_id
self.active_conversations[conv_id]["tool_calls"] += 1
print(f"🔧 Tool: {tool_call.tool_name}")
def on_tool_completed(self, call_id: str, result):
"""Tool finished executing"""
print(f"✅ Completed: {call_id}")
def on_conversation_ended(self, conversation_id: str):
"""Conversation ends"""
if conversation_id in self.active_conversations:
data = self.active_conversations.pop(conversation_id)
duration = (datetime.now() - data['start_time']).total_seconds()
print(f"📞 Ended: {conversation_id}")
print(f" Duration: {duration:.1f}s")
print(f" Tool calls: {data['tool_calls']}")
def on_error(self, error_type: str, message: str, details: dict):
"""Error occurred"""
print(f"❌ Error: {error_type} - {message}")
# Handle error appropriately