-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from crestalnetwork/fix/twitter-link
Fix: twitter auth link
- Loading branch information
Showing
3 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Core Client Module. | ||
This module provides client functions for core API endpoints with environment-aware routing. | ||
""" | ||
|
||
import httpx | ||
|
||
from abstracts.engine import AgentMessageInput | ||
from app.config.config import config | ||
from app.core.engine import execute_agent as local_execute_agent | ||
|
||
|
||
async def execute_agent( | ||
aid: str, message: AgentMessageInput, thread_id: str | ||
) -> list[str]: | ||
"""Execute an agent with environment-aware routing. | ||
In local environment, directly calls the local execute_agent function. | ||
In other environments, makes HTTP request to the core API endpoint. | ||
Args: | ||
aid (str): Agent ID to execute | ||
message (AgentMessageInput): Input message containing text and optional images | ||
thread_id (str): Thread ID for conversation tracking | ||
Returns: | ||
list[str]: Formatted response lines from agent execution | ||
Raises: | ||
HTTPException: For API errors (in non-local environment) | ||
Exception: For other execution errors | ||
""" | ||
if config.env == "local": | ||
return await local_execute_agent(aid, message, thread_id) | ||
|
||
# Make HTTP request in non-local environment | ||
url = f"{config.internal_base_url}/core/execute" | ||
async with httpx.AsyncClient() as client: | ||
response = await client.post( | ||
url, | ||
json={ | ||
"aid": aid, | ||
"message": message.model_dump(), | ||
"thread_id": thread_id, | ||
}, | ||
timeout=180, | ||
) | ||
response.raise_for_status() | ||
return response.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""API server module. | ||
This module initializes and configures the FastAPI application, | ||
including routers, middleware, and startup/shutdown events. | ||
The API server provides endpoints for agent execution and management. | ||
""" | ||
|
||
import logging | ||
from contextlib import asynccontextmanager | ||
|
||
import sentry_sdk | ||
from fastapi import FastAPI | ||
|
||
from app.admin.api import admin_router, admin_router_readonly | ||
from app.admin.health import health_router | ||
from app.config.config import config | ||
from app.services.twitter.oauth2 import router as twitter_oauth2_router | ||
from app.services.twitter.oauth2_callback import router as twitter_callback_router | ||
from models.db import init_db | ||
|
||
# init logger | ||
logger = logging.getLogger(__name__) | ||
|
||
if config.sentry_dsn: | ||
sentry_sdk.init( | ||
dsn=config.sentry_dsn, | ||
traces_sample_rate=config.sentry_traces_sample_rate, | ||
profiles_sample_rate=config.sentry_profiles_sample_rate, | ||
environment=config.env, | ||
release=config.release, | ||
) | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
"""Manage application lifecycle. | ||
This context manager: | ||
1. Initializes database connection | ||
2. Performs any necessary startup tasks | ||
3. Handles graceful shutdown | ||
Args: | ||
app: FastAPI application instance | ||
""" | ||
# Initialize database | ||
await init_db(**config.db) | ||
|
||
logger.info("API server start") | ||
yield | ||
# Clean up will run after the API server shutdown | ||
logger.info("Cleaning up and shutdown...") | ||
|
||
|
||
app = FastAPI(lifespan=lifespan) | ||
|
||
app.include_router(admin_router) | ||
app.include_router(admin_router_readonly) | ||
app.include_router(twitter_callback_router) | ||
app.include_router(twitter_oauth2_router) | ||
app.include_router(health_router) |