|
| 1 | +import logging |
| 2 | + |
| 3 | +import uvicorn |
| 4 | +from starlette.requests import Request |
| 5 | +from starlette.responses import JSONResponse |
| 6 | +from starlette.routing import Route |
| 7 | +from textwrap import dedent |
| 8 | + |
| 9 | +from a2a.server.agent_execution import AgentExecutor, RequestContext |
| 10 | +from a2a.server.apps import A2AStarletteApplication |
| 11 | +from a2a.server.events.event_queue import EventQueue |
| 12 | +from a2a.server.request_handlers import DefaultRequestHandler |
| 13 | +from a2a.server.tasks import InMemoryTaskStore, TaskUpdater |
| 14 | +from a2a.types import AgentCapabilities, AgentCard, AgentSkill, TaskState, TextPart |
| 15 | +from a2a.utils import new_agent_text_message, new_task |
| 16 | + |
| 17 | +from cheerup_agent.cheerup_llm import chat |
| 18 | + |
| 19 | +logging.basicConfig(level=logging.DEBUG) |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +def get_agent_card(host: str, port: int): |
| 24 | + """Returns the Agent Card for the Cheerup Agent.""" |
| 25 | + capabilities = AgentCapabilities(streaming=False) |
| 26 | + skill = AgentSkill( |
| 27 | + id="cheerup_companion", |
| 28 | + name="Cheerup Companion", |
| 29 | + description="**Cheerup Companion** - A warm, uplifting friend that brightens your day with humor, encouragement, and positive vibes.", |
| 30 | + tags=["cheerful", "motivation", "humor", "wellbeing"], |
| 31 | + examples=[ |
| 32 | + "I'm having a rough day", |
| 33 | + "Tell me something to make me smile", |
| 34 | + "I need a pep talk", |
| 35 | + "Make me laugh!", |
| 36 | + ], |
| 37 | + ) |
| 38 | + return AgentCard( |
| 39 | + name="Cheerup Companion", |
| 40 | + description=dedent( |
| 41 | + """\ |
| 42 | + A cheerful conversational companion designed to brighten your day |
| 43 | + and put you in a great mood. |
| 44 | +
|
| 45 | + ## What I can do |
| 46 | + - Share jokes and fun facts to make you smile |
| 47 | + - Give you a pep talk when you need encouragement |
| 48 | + - Celebrate your wins, big or small |
| 49 | + - Be a positive, supportive friend to chat with |
| 50 | + """, |
| 51 | + ), |
| 52 | + url=f"http://{host}:{port}/", |
| 53 | + version="1.0.0", |
| 54 | + default_input_modes=["text"], |
| 55 | + default_output_modes=["text"], |
| 56 | + capabilities=capabilities, |
| 57 | + skills=[skill], |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +class CheerupExecutor(AgentExecutor): |
| 62 | + """Handles cheerup agent execution for A2A.""" |
| 63 | + |
| 64 | + async def execute(self, context: RequestContext, event_queue: EventQueue): |
| 65 | + task = context.current_task |
| 66 | + if not task: |
| 67 | + task = new_task(context.message) |
| 68 | + await event_queue.enqueue_event(task) |
| 69 | + task_updater = TaskUpdater(event_queue, task.id, task.context_id) |
| 70 | + |
| 71 | + user_input = context.get_user_input() |
| 72 | + logger.info("Cheerup agent received: %s (context=%s)", user_input, task.context_id) |
| 73 | + |
| 74 | + await task_updater.update_status( |
| 75 | + TaskState.working, |
| 76 | + new_agent_text_message( |
| 77 | + "Brewing up some good vibes...", |
| 78 | + task_updater.context_id, |
| 79 | + task_updater.task_id, |
| 80 | + ), |
| 81 | + ) |
| 82 | + |
| 83 | + try: |
| 84 | + reply = await chat(task.context_id, user_input) |
| 85 | + |
| 86 | + parts = [TextPart(text=reply)] |
| 87 | + await task_updater.add_artifact(parts) |
| 88 | + await task_updater.update_status( |
| 89 | + TaskState.input_required, |
| 90 | + new_agent_text_message( |
| 91 | + reply, |
| 92 | + task_updater.context_id, |
| 93 | + task_updater.task_id, |
| 94 | + ), |
| 95 | + ) |
| 96 | + except Exception as e: |
| 97 | + logger.error("Cheerup agent error: %s", e) |
| 98 | + parts = [TextPart(text="Sorry, something went wrong. Please try again.")] |
| 99 | + await task_updater.add_artifact(parts) |
| 100 | + await task_updater.failed() |
| 101 | + |
| 102 | + async def cancel(self, context: RequestContext, event_queue: EventQueue) -> None: |
| 103 | + raise Exception("cancel not supported") |
| 104 | + |
| 105 | + |
| 106 | +async def health(request: Request) -> JSONResponse: |
| 107 | + return JSONResponse({"status": "ok"}) |
| 108 | + |
| 109 | + |
| 110 | +async def agent_card_compat(request: Request) -> JSONResponse: |
| 111 | + """Serve agent card at /.well-known/agent-card.json for Kagenti backend compatibility.""" |
| 112 | + card = get_agent_card(host="0.0.0.0", port=8000) |
| 113 | + return JSONResponse(card.model_dump(mode="json", exclude_none=True)) |
| 114 | + |
| 115 | + |
| 116 | +def run(): |
| 117 | + """Runs the A2A Agent application.""" |
| 118 | + agent_card = get_agent_card(host="0.0.0.0", port=8000) |
| 119 | + |
| 120 | + request_handler = DefaultRequestHandler( |
| 121 | + agent_executor=CheerupExecutor(), |
| 122 | + task_store=InMemoryTaskStore(), |
| 123 | + ) |
| 124 | + |
| 125 | + server = A2AStarletteApplication( |
| 126 | + agent_card=agent_card, |
| 127 | + http_handler=request_handler, |
| 128 | + ) |
| 129 | + |
| 130 | + app = server.build() |
| 131 | + |
| 132 | + # Add custom routes |
| 133 | + app.routes.insert(0, Route("/health", health, methods=["GET"])) |
| 134 | + app.routes.insert(0, Route("/.well-known/agent-card.json", agent_card_compat, methods=["GET"])) |
| 135 | + |
| 136 | + uvicorn.run(app, host="0.0.0.0", port=8000) |
0 commit comments