11# -*- coding: utf-8 -*-
2- import asyncio
32import logging
43import types
5- from contextlib import asynccontextmanager
6- from typing import Optional , Any , Callable , List
4+ from typing import Optional , Callable , List
75
86import uvicorn
9- from fastapi import FastAPI
107from pydantic import BaseModel
118
129from .base_app import BaseApp
@@ -43,6 +40,7 @@ def __init__(
4340 broker_url : Optional [str ] = None ,
4441 backend_url : Optional [str ] = None ,
4542 runner : Optional [Runner ] = None ,
43+ enable_embedded_worker : bool = False ,
4644 ** kwargs ,
4745 ):
4846 """
@@ -61,6 +59,7 @@ def __init__(
6159 self .after_finish = after_finish
6260 self .broker_url = broker_url
6361 self .backend_url = backend_url
62+ self .enable_embedded_worker = enable_embedded_worker
6463
6564 self ._runner = runner
6665 self .custom_endpoints = [] # Store custom endpoints
@@ -79,33 +78,16 @@ def __init__(
7978 response_protocol = ResponseAPIDefaultAdapter ()
8079 self .protocol_adapters = [a2a_protocol , response_protocol ]
8180
82- @asynccontextmanager
83- async def lifespan (app : FastAPI ) -> Any :
84- """Manage the application lifespan."""
85- if hasattr (self , "before_start" ) and self .before_start :
86- if asyncio .iscoroutinefunction (self .before_start ):
87- await self .before_start (app , ** getattr (self , "kwargs" , {}))
88- else :
89- self .before_start (app , ** getattr (self , "kwargs" , {}))
90- yield
91- if hasattr (self , "after_finish" ) and self .after_finish :
92- if asyncio .iscoroutinefunction (self .after_finish ):
93- await self .after_finish (app , ** getattr (self , "kwargs" , {}))
94- else :
95- self .after_finish (app , ** getattr (self , "kwargs" , {}))
96-
97- kwargs = {
81+ self ._app_kwargs = {
9882 "title" : "Agent Service" ,
9983 "version" : __version__ ,
10084 "description" : "Production-ready Agent Service API" ,
101- "lifespan" : lifespan ,
10285 ** kwargs ,
10386 }
10487
10588 super ().__init__ (
10689 broker_url = broker_url ,
10790 backend_url = backend_url ,
108- ** kwargs ,
10991 )
11092
11193 # Store custom endpoints and tasks for deployment
@@ -167,7 +149,6 @@ def run(
167149 self ,
168150 host = "0.0.0.0" ,
169151 port = 8090 ,
170- embed_task_processor = False ,
171152 ** kwargs ,
172153 ):
173154 """
@@ -176,7 +157,6 @@ def run(
176157 Args:
177158 host: Host to bind to
178159 port: Port to bind to
179- embed_task_processor: Whether to embed task processor
180160 **kwargs: Additional keyword arguments
181161 """
182162 # Build runner
@@ -186,24 +166,7 @@ def run(
186166 logger .info (
187167 "[AgentApp] Starting AgentApp with FastAPIAppFactory..." ,
188168 )
189-
190- # Create FastAPI application using the factory
191- fastapi_app = FastAPIAppFactory .create_app (
192- runner = self ._runner ,
193- endpoint_path = self .endpoint_path ,
194- request_model = self .request_model ,
195- response_type = self .response_type ,
196- stream = self .stream ,
197- before_start = self .before_start ,
198- after_finish = self .after_finish ,
199- mode = DeploymentMode .DAEMON_THREAD ,
200- protocol_adapters = self .protocol_adapters ,
201- custom_endpoints = self .custom_endpoints ,
202- broker_url = self .broker_url ,
203- backend_url = self .backend_url ,
204- enable_embedded_worker = embed_task_processor ,
205- ** kwargs ,
206- )
169+ fastapi_app = self .get_fastapi_app (** kwargs )
207170
208171 logger .info (f"[AgentApp] Starting server on { host } :{ port } " )
209172
@@ -220,6 +183,29 @@ def run(
220183 logger .error (f"[AgentApp] Error while running: { e } " )
221184 raise
222185
186+ def get_fastapi_app (self , ** kwargs ):
187+ """Get the FastAPI application"""
188+
189+ self ._build_runner ()
190+
191+ return FastAPIAppFactory .create_app (
192+ runner = self ._runner ,
193+ endpoint_path = self .endpoint_path ,
194+ request_model = self .request_model ,
195+ response_type = self .response_type ,
196+ stream = self .stream ,
197+ before_start = self .before_start ,
198+ after_finish = self .after_finish ,
199+ mode = DeploymentMode .DAEMON_THREAD ,
200+ protocol_adapters = self .protocol_adapters ,
201+ custom_endpoints = self .custom_endpoints ,
202+ broker_url = self .broker_url ,
203+ backend_url = self .backend_url ,
204+ enable_embedded_worker = self .enable_embedded_worker ,
205+ app_kwargs = self ._app_kwargs ,
206+ ** kwargs ,
207+ )
208+
223209 async def deploy (self , deployer : DeployManager , ** kwargs ):
224210 """Deploy the agent app with custom endpoints support"""
225211 # Pass custom endpoints and tasks to the deployer
0 commit comments