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.

5.3 KB
Raw Blame
from typing import cast

from products.warehouse_sources.sources.promptwatch._config import PromptWatchSourceConfig
from products.warehouse_sources.sources.promptwatch.canonical_descriptions import CANONICAL_DESCRIPTIONS
from products.warehouse_sources.sources.promptwatch.promptwatch import (
    PromptWatchResumeConfig,
    promptwatch_source,
    validate_credentials,
)
from products.warehouse_sources.sources.promptwatch.settings import (
    CREATED_AT_SORTED_ENDPOINTS,
    ENDPOINTS,
    INCREMENTAL_FIELDS,
    MERGE_ONLY_ENDPOINTS,
    NON_RETRYABLE_ERRORS,
    PAGINATED_ENDPOINTS,
    PRIMARY_KEYS,
)
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,
    schema_for_resource,
)


@SourceRegistry.register
class PromptWatchSource(ResumableSource[PromptWatchSourceConfig, PromptWatchResumeConfig]):
    codebase_detection = detect(env=("^(PROMPT_WATCH|PROMPTWATCH)_",), script_hosts=("ingest.promptwatch.com",))

    lists_tables_without_credentials = True
    supported_versions = ("v2",)
    default_version = "v2"
    api_docs_url = "https://promptwatch.com/docs/v2/openapi.json"

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

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return dict(NON_RETRYABLE_ERRORS)

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        return CANONICAL_DESCRIPTIONS

    def get_schemas(
        self,
        config: PromptWatchSourceConfig,
        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, merge_only=MERGE_ONLY_ENDPOINTS)

    def validate_credentials(
        self,
        config: PromptWatchSourceConfig,
        team_id: int,
        schema_name: str | None = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_credentials(config, self.resolve_api_version(api_version))

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

    def source_for_pipeline(
        self,
        config: PromptWatchSourceConfig,
        resumable_source_manager: ResumableSourceManager[PromptWatchResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        schema_for_resource(CANONICAL_DESCRIPTIONS, inputs.schema_name)
        return SourceResponse(
            name=inputs.schema_name,
            items=lambda: promptwatch_source(
                config, inputs, resumable_source_manager, self.resolve_api_version(inputs.api_version)
            ),
            primary_keys=PRIMARY_KEYS.get(inputs.schema_name, ["id"]),
            sort_mode="asc" if inputs.schema_name in CREATED_AT_SORTED_ENDPOINTS else None,
            supports_resume=inputs.schema_name in PAGINATED_ENDPOINTS,
        )

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.PROMPTWATCH,
            category=DataWarehouseSourceCategory.ANALYTICS,
            label="Promptwatch",
            iconPath="/static/services/promptwatch.png",
            releaseStatus=ReleaseStatus.BETA,
            caption=(
                "Create a read-only API key in Promptwatch under Settings > API Keys. "
                "Requires the Explore plan or higher. Syncs use your account's API units (hourly request quota). "
                "Each paginated table allows up to 1,000 rows per sync. Responses start from your chosen date."
            ),
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="api_key",
                        label="API key",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                    SourceFieldInputConfig(
                        name="project_id",
                        secret=False,
                        label="Project ID (required for organization API keys)",
                        type=SourceFieldInputConfigType.TEXT,
                        required=False,
                        placeholder="00000000-0000-0000-0000-000000000000",
                    ),
                    SourceFieldInputConfig(
                        name="start_date",
                        sync_setting=True,
                        secret=False,
                        label="Response start date",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="YYYY-MM-DD",
                    ),
                ],
            ),
        )