Skip to content

Commit

Permalink
Merge pull request #165 from crestalnetwork/fix/twitter-link
Browse files Browse the repository at this point in the history
Fix: twitter auth link
  • Loading branch information
hyacinthus authored Feb 4, 2025
2 parents bd6244b + d42cb68 commit 2037644
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
49 changes: 49 additions & 0 deletions app/core/client.py
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()
2 changes: 1 addition & 1 deletion app/services/tg/bot/kind/ai_relayer/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from aiogram.types import Message

from abstracts.engine import AgentMessageInput
from app.core.engine import execute_agent
from app.core.client import execute_agent
from app.services.tg.bot import pool
from app.services.tg.bot.filter.chat_type import GroupOnlyFilter
from app.services.tg.bot.filter.content_type import TextOnlyFilter
Expand Down
62 changes: 62 additions & 0 deletions app/singleton.py
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)

0 comments on commit 2037644

Please sign in to comment.