import asyncio
from datetime import datetime
from tyler import Agent, Thread, Message, ThreadStore, FileStore
from lye import WEB_TOOLS
class SupportAgent:
def __init__(self):
self.agent = None
self.thread_store = None
self.file_store = None
async def initialize(self):
# Use Narrator's Docker PostgreSQL (after running: uv run narrator docker-setup)
self.thread_store = await ThreadStore.create(
"postgresql+asyncpg://narrator:narrator_dev@localhost:5432/narrator"
)
# Or use your own PostgreSQL instance:
# self.thread_store = await ThreadStore.create(
# "postgresql+asyncpg://localhost/support"
# )
self.file_store = await FileStore.create("./support_files")
self.agent = Agent(
name="support-agent",
model_name="gpt-4",
purpose="To help customers with product issues",
tools=[*WEB_TOOLS],
thread_store=self.thread_store,
file_store=self.file_store
)
async def handle_ticket(self, ticket_id: str, customer_id: str, issue: str):
# Create thread for this ticket
thread = Thread(
id=f"ticket-{ticket_id}",
metadata={
"ticket_id": ticket_id,
"customer_id": customer_id,
"status": "open",
"created_at": datetime.now().isoformat()
}
)
# Add initial message
thread.add_message(Message(
role="user",
content=f"Customer {customer_id} reports: {issue}"
))
# Process with agent
result = await self.agent.run(thread)
thread = result.thread
# Save thread
await self.thread_store.save_thread(thread)
# Return response
return messages[-1].content if messages else "No response"
async def get_ticket_history(self, ticket_id: str):
thread = await self.thread_store.get_thread(f"ticket-{ticket_id}")
return thread.messages
# Usage
agent = SupportAgent()
await agent.initialize()
response = await agent.handle_ticket(
ticket_id="12345",
customer_id="cust-789",
issue="Cannot login to my account"
)