Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

6.8 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.e2b._config import E2BSourceConfig
from products.warehouse_sources.sources.e2b.e2b import (
    INVALID_CREDENTIALS_ERROR,
    NO_ACCESS_ERROR,
    TEAM_ID_INVALID_ERROR,
    TEAM_ID_REQUIRED_ERROR,
    E2BResumeConfig,
    e2b_source,
    validate_credentials as validate_e2b_credentials,
)
from products.warehouse_sources.sources.e2b.settings import E2B_ENDPOINTS, ENDPOINTS, INCREMENTAL_FIELDS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    detect,
)


@SourceRegistry.register
class E2BSource(ResumableSource[E2BSourceConfig, E2BResumeConfig]):
    codebase_detection = detect(
        env=("^E2B_",),
        npm=("e2b", "@e2b/code-interpreter", "@e2b/desktop"),
        pypi=("e2b", "e2b-code-interpreter", "e2b-desktop"),
    )

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    supported_versions = ("v2",)
    default_version = "v2"
    api_docs_url = "https://e2b.dev/docs"

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.E2B

    @property
    def credential_kind(self) -> str | None:
        return "e2b"

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.E2B,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="E2B",
            releaseStatus=ReleaseStatus.GA,
            caption="""Enter your E2B API key to sync your sandbox infrastructure data into the PostHog Data warehouse.

You can create a team-scoped API key (prefixed `e2b_`) in your [E2B dashboard](https://e2b.dev/dashboard).

The team ID is only needed for the team metrics table. Every other table syncs with the API key alone.""",
            iconPath="/static/services/e2b.png",
            docsUrl="https://posthog.com/docs/cdp/sources/e2b",
            keywords=["sandbox", "ai agents", "code execution", "infrastructure"],
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="e2b_...",
                        secret=True,
                    ),
                    SourceFieldInputConfig(
                        name="team_id",
                        label="Team ID",
                        type=SourceFieldInputConfigType.TEXT,
                        required=False,
                        placeholder="prj_...",
                        secret=False,
                    ),
                ],
            ),
        )

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.e2b.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            # An invalid or revoked API key surfaces as an HTTPError when `_fetch_page` calls
            # `raise_for_status()`. Retrying can never satisfy a credential problem, so stop the sync.
            "401 Client Error: Unauthorized for url: https://api.e2b.app": INVALID_CREDENTIALS_ERROR,
            "403 Client Error: Forbidden for url: https://api.e2b.app": NO_ACCESS_ERROR,
            # A missing or malformed team ID can only be fixed in the source settings, so retrying
            # the sync just burns attempts until the job gives up.
            TEAM_ID_REQUIRED_ERROR: TEAM_ID_REQUIRED_ERROR,
            TEAM_ID_INVALID_ERROR: TEAM_ID_INVALID_ERROR,
        }

    def get_schemas(
        self,
        config: E2BSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        # Every E2B list endpoint is a point-in-time inventory with no server-side timestamp filter,
        # so all are full refresh (no incremental / append).
        has_team_id = bool((config.team_id or "").strip())

        def _build_schema(endpoint: str) -> SourceSchema:
            endpoint_config = E2B_ENDPOINTS[endpoint]
            return SourceSchema(
                name=endpoint,
                supports_incremental=False,
                supports_append=False,
                incremental_fields=INCREMENTAL_FIELDS.get(endpoint, []),
                # A table whose team ID is missing starts unselected rather than failing its first sync.
                should_sync_default=endpoint_config.should_sync_default
                and (has_team_id or not endpoint_config.requires_team_id),
            )

        schemas = [_build_schema(endpoint) for endpoint in ENDPOINTS]
        if names is not None:
            names_set = set(names)
            schemas = [s for s in schemas if s.name in names_set]
        return schemas

    def validate_credentials(
        self, config: E2BSourceConfig, team_id: int, schema_name: Optional[str] = None, api_version: str | None = None
    ) -> tuple[bool, str | None]:
        endpoint = E2B_ENDPOINTS.get(schema_name) if schema_name else None
        if endpoint is not None and endpoint.requires_team_id and not (config.team_id or "").strip():
            return False, TEAM_ID_REQUIRED_ERROR
        try:
            return validate_e2b_credentials(config.api_key)
        except Exception:
            # The probe couldn't reach E2B (timeout, rate limit, or a 5xx). That says nothing about the
            # key, so point the user at retrying rather than at their credentials.
            return (
                False,
                "Couldn't reach E2B to validate your API key. This is usually temporary, so try again in a moment.",
            )

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[E2BResumeConfig]:
        return ResumableSourceManager[E2BResumeConfig](inputs, E2BResumeConfig)

    def source_for_pipeline(
        self,
        config: E2BSourceConfig,
        resumable_source_manager: ResumableSourceManager[E2BResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return e2b_source(
            api_key=config.api_key,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
            resumable_source_manager=resumable_source_manager,
            e2b_team_id=config.team_id,
        )