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.

4.8 KB
Raw Blame
from typing import cast

from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    build_endpoint_schemas,
    detect,
)
from products.warehouse_sources.sources.sim._config import SimSourceConfig
from products.warehouse_sources.sources.sim.canonical_descriptions import CANONICAL_DESCRIPTIONS
from products.warehouse_sources.sources.sim.settings import ENDPOINTS, INCREMENTAL_FIELDS
from products.warehouse_sources.sources.sim.sim import (
    AUTH_ERROR,
    PERMISSION_ERROR,
    WORKSPACE_ERROR,
    SimResumeConfig,
    sim_source,
    validate_credentials as validate_sim_credentials,
)


@SourceRegistry.register
class SimSource(ResumableSource[SimSourceConfig, SimResumeConfig]):
    codebase_detection = detect(
        env=("^SIM_API_KEY$", "^SIM_BASE_URL$", "^SIMSTUDIO_API_KEY$"),
        npm=("simstudio-ts-sdk",),
        pypi=("simstudio-sdk",),
    )

    lists_tables_without_credentials = True
    supported_versions = ("v2",)
    default_version = "v2"
    api_docs_url = "https://docs.sim.ai/api-reference/getting-started"

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

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "401 Client Error:": AUTH_ERROR,
            "403 Client Error:": PERMISSION_ERROR,
            "404 Client Error:": WORKSPACE_ERROR,
        }

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        return CANONICAL_DESCRIPTIONS

    def validate_credentials(
        self,
        config: SimSourceConfig,
        team_id: int,
        schema_name: str | None = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        if schema_name is not None and schema_name not in ENDPOINTS:
            return False, f"Unknown Sim table: {schema_name}. Select one of: {', '.join(ENDPOINTS)}."
        return validate_sim_credentials(config, self.resolve_api_version(api_version), schema_name)

    def get_schemas(
        self,
        config: SimSourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        return build_endpoint_schemas(
            ENDPOINTS,
            INCREMENTAL_FIELDS,
            names,
            # usage_window rereads the watermark's whole day, and append does not deduplicate primary keys.
            merge_only=("usage_events",),
        )

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

    def source_for_pipeline(
        self,
        config: SimSourceConfig,
        resumable_source_manager: ResumableSourceManager[SimResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        return sim_source(config, inputs, resumable_source_manager, self.resolve_api_version(inputs.api_version))

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.SIM,
            category=DataWarehouseSourceCategory.PRODUCTIVITY,
            label="Sim",
            iconPath="/static/services/sim.png",
            keywords=["ai agents", "workflow", "automation", "logs"],
            docsUrl="https://posthog.com/docs/cdp/sources/sim",
            caption="Create a personal or workspace API key in Sim settings with read access to the workspace. "
            "Find the workspace ID in the URL: `https://www.sim.ai/workspace/{workspaceId}/w/{workflowId}`. "
            "The usage events table needs a personal API key from an organization admin.",
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        placeholder="",
                        required=True,
                        secret=True,
                    ),
                    SourceFieldInputConfig(
                        name="workspace_id",
                        label="Workspace ID",
                        type=SourceFieldInputConfigType.TEXT,
                        placeholder="",
                        required=True,
                        secret=False,
                    ),
                ],
            ),
            releaseStatus=ReleaseStatus.BETA,
        )