refac
This commit is contained in:
1 parent
60ea4214aa
commit
f152ad36b3
2 files changed
+18
-3
No files matched your search
@@ -707,8 +707,6 @@ async def lifespan(app: FastAPI):
|
|||||||
|
|
||||||
# Pre-fetch tool server specs so the first request doesn't pay the latency cost
|
# Pre-fetch tool server specs so the first request doesn't pay the latency cost
|
||||||
if len(app.state.config.TOOL_SERVER_CONNECTIONS) > 0:
|
if len(app.state.config.TOOL_SERVER_CONNECTIONS) > 0:
|
||||||
log.info('Initializing tool servers...')
|
|
||||||
try:
|
|
||||||
mock_request = Request(
|
mock_request = Request(
|
||||||
{
|
{
|
||||||
'type': 'http',
|
'type': 'http',
|
||||||
@@ -724,13 +722,19 @@ async def lifespan(app: FastAPI):
|
|||||||
'app': app,
|
'app': app,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
log.info('Initializing tool servers...')
|
||||||
|
try:
|
||||||
await set_tool_servers(mock_request)
|
await set_tool_servers(mock_request)
|
||||||
log.info(f'Initialized {len(app.state.TOOL_SERVERS)} tool server(s)')
|
log.info(f'Initialized {len(app.state.TOOL_SERVERS)} tool server(s)')
|
||||||
|
except Exception as e:
|
||||||
|
log.warning(f'Failed to initialize tool servers at startup: {e}')
|
||||||
|
|
||||||
|
try:
|
||||||
await set_terminal_servers(mock_request)
|
await set_terminal_servers(mock_request)
|
||||||
log.info(f'Initialized {len(app.state.TERMINAL_SERVERS)} terminal server(s)')
|
log.info(f'Initialized {len(app.state.TERMINAL_SERVERS)} terminal server(s)')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.warning(f'Failed to initialize tool/terminal servers at startup: {e}')
|
log.warning(f'Failed to initialize terminal servers at startup: {e}')
|
||||||
|
|
||||||
# Mark application as ready to accept traffic from a startup perspective.
|
# Mark application as ready to accept traffic from a startup perspective.
|
||||||
app.state.startup_complete = True
|
app.state.startup_complete = True
|
||||||
|
|||||||
@@ -872,17 +872,25 @@ def convert_openapi_to_tool_payload(openapi_spec):
|
|||||||
|
|
||||||
|
|
||||||
async def set_tool_servers(request: Request):
|
async def set_tool_servers(request: Request):
|
||||||
|
try:
|
||||||
request.app.state.TOOL_SERVERS = await get_tool_servers_data(request.app.state.config.TOOL_SERVER_CONNECTIONS)
|
request.app.state.TOOL_SERVERS = await get_tool_servers_data(request.app.state.config.TOOL_SERVER_CONNECTIONS)
|
||||||
|
except Exception as e:
|
||||||
|
log.error(f'Error fetching tool server data: {e}')
|
||||||
|
request.app.state.TOOL_SERVERS = getattr(request.app.state, 'TOOL_SERVERS', None) or []
|
||||||
|
|
||||||
|
try:
|
||||||
if request.app.state.redis is not None:
|
if request.app.state.redis is not None:
|
||||||
await request.app.state.redis.set(
|
await request.app.state.redis.set(
|
||||||
f'{REDIS_KEY_PREFIX}:tool_servers', json.dumps(request.app.state.TOOL_SERVERS)
|
f'{REDIS_KEY_PREFIX}:tool_servers', json.dumps(request.app.state.TOOL_SERVERS)
|
||||||
)
|
)
|
||||||
|
except Exception as e:
|
||||||
|
log.error(f'Error caching tool_servers to Redis: {e}')
|
||||||
|
|
||||||
return request.app.state.TOOL_SERVERS
|
return request.app.state.TOOL_SERVERS
|
||||||
|
|
||||||
|
|
||||||
async def get_tool_servers(request: Request):
|
async def get_tool_servers(request: Request):
|
||||||
|
try:
|
||||||
tool_servers = []
|
tool_servers = []
|
||||||
if request.app.state.redis is not None:
|
if request.app.state.redis is not None:
|
||||||
try:
|
try:
|
||||||
@@ -895,6 +903,9 @@ async def get_tool_servers(request: Request):
|
|||||||
tool_servers = await set_tool_servers(request)
|
tool_servers = await set_tool_servers(request)
|
||||||
|
|
||||||
return tool_servers
|
return tool_servers
|
||||||
|
except Exception as e:
|
||||||
|
log.error(f'Failed to load tool servers, skipping: {e}')
|
||||||
|
return getattr(request.app.state, 'TOOL_SERVERS', None) or []
|
||||||
|
|
||||||
|
|
||||||
async def get_terminal_cwd(
|
async def get_terminal_cwd(
|
||||||
|
|||||||
Reference in new issue
Block a user