diff --git a/utils/fastapi_utils.py b/utils/fastapi_utils.py index ac4c5c3..5dd8709 100644 --- a/utils/fastapi_utils.py +++ b/utils/fastapi_utils.py @@ -615,13 +615,19 @@ async def fetch_bot_config_from_db(bot_user_id: str, user_identifier: Optional[s if not config.get("api_key") and user_identifier: async with pool.connection() as conn: async with conn.cursor() as cursor: + # Sub-accounts should fall back to the parent account's API token await cursor.execute( - "SELECT new_api_token FROM agent_user WHERE username = %s", + """ + SELECT u.new_api_token, parent.new_api_token + FROM agent_user u + LEFT JOIN agent_user parent ON u.parent_id = parent.id + WHERE u.username = %s + """, (user_identifier,) ) row = await cursor.fetchone() - if row and row[0]: - config["api_key"] = row[0] + if row: + config["api_key"] = row[1] or row[0] or "" logger.info( f"Fetched general agent config: model={config['model']}, " f"api_key={'*' + config['api_key'][-4:] if config.get('api_key') else 'N/A'}"