import os
from typing import Annotated
from line import CallRequest
from line.llm_agent import (
LlmAgent, LlmConfig, loopback_tool, passthrough_tool,
agent_as_handoff, end_call
)
from line.events import AgentSendText, AgentTransferCall
from line.voice_agent_app import AgentEnv, VoiceAgentApp
# Loopback tool: Fetch order info for LLM to contextualize
@loopback_tool
async def get_order_status(ctx, order_id: Annotated[str, "The order ID"]):
"""Look up order status by ID."""
order = await db.get_order(order_id)
return f"Order {order_id}: {order.status}, delivers {order.delivery_date}"
# Passthrough tool: Deterministic transfer action
@passthrough_tool
async def transfer_to_human(ctx):
"""Transfer to a human agent."""
yield AgentSendText(text="Let me connect you with a team member who can help further.")
yield AgentTransferCall(target_phone_number="+18005551234")
SYSTEM_PROMPT = """You are a friendly customer service agent for Acme Corp.
You can:
- Look up order status using get_order_status
- Transfer to a human agent using transfer_to_human
- Transfer to Spanish support using transfer_to_spanish
- End calls politely using end_call
Rules:
- Always confirm the order ID before looking it up
- Offer to transfer to a human if you can't resolve the issue
- Transfer to Spanish support if the user speaks Spanish or requests it
- Be empathetic and professional
"""
async def get_agent(env: AgentEnv, call_request: CallRequest):
# Spanish-speaking specialist agent
spanish_agent = LlmAgent(
model="gpt-5-nano",
api_key=os.getenv("OPENAI_API_KEY"),
tools=[get_order_status, transfer_to_human, end_call],
config=LlmConfig(
system_prompt="Eres un agente de servicio al cliente amigable para Acme Corp. Habla solo en español.",
introduction="¡Hola! Gracias por llamar a Acme Corp. ¿Cómo puedo ayudarte hoy?",
),
)
# Main English-speaking agent with handoff capability
return LlmAgent(
model="anthropic/claude-haiku-4-5-20251001",
api_key=os.getenv("ANTHROPIC_API_KEY"),
tools=[
get_order_status,
transfer_to_human,
agent_as_handoff(
spanish_agent,
handoff_message="Transferring you to our Spanish-speaking team...",
name="transfer_to_spanish",
description="Transfer to Spanish support when user speaks Spanish or requests it.",
),
end_call,
],
config=LlmConfig(
system_prompt=SYSTEM_PROMPT,
introduction="Hi! Thanks for calling Acme Corp. How can I help you today?",
),
)
app = VoiceAgentApp(get_agent=get_agent)
if __name__ == "__main__":
app.run()