Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat]: Monorepo -- Checkpoint 4 #2488

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions agenta-backend/.oss.env.dev.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
POSTGRES_URI=postgresql+asyncpg://username:password@postgres:5432/agenta_oss
REDIS_URL=redis://redis:6379/0
ENVIRONMENT=development
BARE_DOMAIN_NAME=localhost
DOMAIN_NAME=http://localhost
ALEMBIC_CFG_PATH=/app/agenta_backend/migrations/postgres/alembic.oss.ini
AGENTA_AUTO_MIGRATIONS=false
DOMAIN_NAME=${DOMAIN_NAME:-http://localhost}
WEBSITE_DOMAIN_NAME=${WEBSITE_DOMAIN_NAME:-http://localhost}
FEATURE_FLAG=oss
AGENTA_TEMPLATE_REPO=agentaai/templates_v2
POSTHOG_API_KEY=phc_hmVSxIjTW1REBHXgj2aw4HW9X6CXb6FzerBgP9XenC7
CELERY_BROKER_URL=amqp://guest@rabbitmq//
CELERY_RESULT_BACKEND=redis://redis:6379/0
REGISTRY_REPO_NAME=agentaai
DOCKER_HUB_URL=https://hub.docker.com/v2/repositories
AGENTA_PORT=${AGENTA_PORT:-80}
SERVICE_URL_TEMPLATE=${DOMAIN_NAME:-http://localhost}:${AGENTA_PORT:-80}/services/{path}
SUPERTOKENS_CONNECTION_URI=value
SUPERTOKENS_API_KEY=value
GOOGLE_OAUTH_CLIENT_ID=value
GOOGLE_OAUTH_CLIENT_SECRET=value
GITHUB_OAUTH_CLIENT_ID=value
GITHUB_OAUTH_CLIENT_SECRET=value
AGENTA_ENCRYPTION_KEY=value
199 changes: 197 additions & 2 deletions agenta-backend/agenta_backend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,201 @@
import os
from typing import Optional, Any, Dict, Union

import sentry_sdk
from supertokens_python import init, InputAppInfo, SupertokensConfig
from supertokens_python.recipe.thirdparty import (
ProviderInput,
ProviderConfig,
ProviderClientConfig,
SignInAndUpFeature,
)
from supertokens_python.recipe import (
passwordless,
session,
dashboard,
thirdparty,
)
from supertokens_python.recipe.passwordless import ContactEmailOnlyConfig
from supertokens_python.recipe.thirdparty.provider import Provider, RedirectUriInfo
from supertokens_python.recipe.session import SessionContainer
from supertokens_python.recipe.thirdparty.interfaces import APIOptions
from supertokens_python.recipe.passwordless.interfaces import (
RecipeInterface as PasswordlessRecipeInterface,
ConsumeCodeOkResult,
)
from supertokens_python.recipe.thirdparty.interfaces import (
APIInterface as ThirdPartyAPIInterface,
SignInUpPostOkResult,
)

from agenta_backend.utils.common import isCloudEE

if isCloudEE():
from agenta_backend.commons.services.commoners import create_accounts
else:
from agenta_backend.services.db_manager import create_accounts


if os.environ.get("FEATURE_FLAG") in ["cloud", "cloud-dev"]:
import agenta_backend.cloud.__init__
if os.environ.get("FEATURE_FLAG") in ["ee"]:
import agenta_backend.ee.__init__


# MODE DSN to env vars
sentry_sdk.init(
dsn=os.getenv("SENTRY_SDK", None),
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
traces_sample_rate=1.0,
# Set profiles_sample_rate to 1.0 to profile 100%
# of sampled transactions.
# We recommend adjusting this value in production.
profiles_sample_rate=1.0,
)


def override_passwordless_apis(
original_implementation: PasswordlessRecipeInterface,
):
original_consume_code_post = original_implementation.consume_code

async def consume_code_post(
pre_auth_session_id: str,
user_input_code: Union[str, None],
device_id: Union[str, None],
link_code: Union[str, None],
tenant_id: str,
user_context: Dict[str, Any],
session: Optional[SessionContainer] = None,
should_try_linking_with_session_user: Optional[bool] = None,
):
# First we call the original implementation of consume_code_post.
response = await original_consume_code_post(
pre_auth_session_id,
user_input_code,
device_id,
link_code,
session,
should_try_linking_with_session_user,
tenant_id,
user_context,
)

# Post sign up response, we check if it was successful
if isinstance(response, ConsumeCodeOkResult):
payload = {
"uid": response.user.id,
"email": response.user.emails[0],
}
await create_accounts(payload)

return response

original_implementation.consume_code = consume_code_post
return original_implementation


def override_thirdparty_apis(original_implementation: ThirdPartyAPIInterface):
original_sign_in_up = original_implementation.sign_in_up_post

async def thirdparty_sign_in_up_post(
provider: Provider,
tenant_id: str,
api_options: APIOptions,
user_context: Dict[str, Any],
redirect_uri_info: Optional[RedirectUriInfo] = None,
oauth_tokens: Optional[Dict[str, Any]] = None,
session: Optional[SessionContainer] = None,
should_try_linking_with_session_user: Optional[bool] = None,
):
# Call the original implementation if needed
response = await original_sign_in_up(
provider,
redirect_uri_info,
oauth_tokens,
session,
should_try_linking_with_session_user,
tenant_id,
api_options,
user_context,
)

# Post sign up response, we check if it was successful
if isinstance(response, SignInUpPostOkResult):
payload = {
"uid": response.user.id,
"email": response.user.emails[0],
}
await create_accounts(payload)

return response

original_implementation.sign_in_up_post = thirdparty_sign_in_up_post

return original_implementation


init(
# debug=True,
app_info=InputAppInfo(
app_name="agenta",
api_domain=os.environ["DOMAIN_NAME"],
website_domain=(
os.environ.get("WEBSITE_DOMAIN_NAME", os.environ["DOMAIN_NAME"])
),
# the fact that both are localhost is causing problems with
# displaying the dashboard to manage users
api_gateway_path="/api/",
api_base_path="/auth/",
website_base_path="/auth",
),
supertokens_config=SupertokensConfig(
connection_uri=os.environ["SUPERTOKENS_CONNECTION_URI"],
api_key=os.environ["SUPERTOKENS_API_KEY"],
),
framework="fastapi",
recipe_list=[
thirdparty.init(
sign_in_and_up_feature=SignInAndUpFeature(
providers=[
ProviderInput(
config=ProviderConfig(
third_party_id="google",
clients=[
ProviderClientConfig(
client_id=os.environ["GOOGLE_OAUTH_CLIENT_ID"],
client_secret=os.environ[
"GOOGLE_OAUTH_CLIENT_SECRET"
],
),
],
),
),
ProviderInput(
config=ProviderConfig(
third_party_id="github",
clients=[
ProviderClientConfig(
client_id=os.environ["GITHUB_OAUTH_CLIENT_ID"],
client_secret=os.environ[
"GITHUB_OAUTH_CLIENT_SECRET"
],
)
],
),
),
],
),
override=thirdparty.InputOverrideConfig(apis=override_thirdparty_apis),
),
passwordless.init(
flow_type="USER_INPUT_CODE",
contact_config=ContactEmailOnlyConfig(),
override=passwordless.InputOverrideConfig(
functions=override_passwordless_apis
),
),
session.init(expose_access_token_to_frontend_in_cookie_based_auth=True),
dashboard.init(),
],
mode="asgi",
)
Empty file.
Loading
Loading