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.2 KB
Raw Blame
"""What a valid `scanner_config` looks like for each scanner type.

Its own module because both the API serializers and Max validate a config before saving or scanning
with it, and neither should import the other."""

from typing import Any, cast

from pydantic import ValidationError as PydanticValidationError

from posthog.event_usage import AnalyticsProps, EventSource
from posthog.models.user import User

from products.replay_vision.backend.models.replay_scanner import ScannerType
from products.replay_vision.backend.tags import slugify_tag
from products.replay_vision.backend.temporal.scanners import validate_scanner_config

MAX_PROMPT_LENGTH = 20_000
MAX_TAGS = 100
MAX_TAG_LENGTH = 100


def scanner_config_error(scanner_type: ScannerType, scanner_config: Any) -> str | None:
    if not isinstance(scanner_config, dict):
        return "Scanner configuration must be a JSON object."
    prompt = scanner_config.get("prompt")
    if not isinstance(prompt, str) or not prompt.strip():
        return "Prompt is required."
    if len(prompt) > MAX_PROMPT_LENGTH:
        return f"Prompt can be at most {MAX_PROMPT_LENGTH:,} characters."
    if scanner_type == ScannerType.CLASSIFIER:
        tags = scanner_config.get("tags") or []
        if not isinstance(tags, list):
            return "Categories must be a list."
        if len(tags) == 0:
            return "Add at least one category."
        if len(tags) > MAX_TAGS:
            return f"You can have at most {MAX_TAGS} categories."
        if any(not isinstance(t, str) or not t.strip() for t in tags):
            return "Categories can't be blank."
        if any(len(t) > MAX_TAG_LENGTH for t in tags):
            return f"Categories can be at most {MAX_TAG_LENGTH} characters."
        # Uniqueness on the slug, since filtering/stripping/search all compare slugified tags downstream.
        slugged: dict[str, str] = {}
        for t in tags:
            slug = slugify_tag(t)
            if not slug:
                return "Categories must contain letters or numbers."
            if slug in slugged:
                return f"Categories must be unique: '{slugged[slug]}' and '{t}' are the same category."
            slugged[slug] = t
    if scanner_type == ScannerType.EXPERIMENT:
        experiment_id = scanner_config.get("experiment_id")
        if isinstance(experiment_id, bool) or not isinstance(experiment_id, int) or experiment_id < 1:
            return "Experiment is required."
        variants = scanner_config.get("variants")
        if variants is not None:
            if not isinstance(variants, list) or not variants:
                return "Variants must be a non-empty list, or null to watch every variant."
            if any(not isinstance(v, str) or not v.strip() for v in variants):
                return "Variants can't be blank."
            if len(set(variants)) != len(variants):
                return "Variants must be unique."
        balance_variants = scanner_config.get("balance_variants")
        if balance_variants is not None and not isinstance(balance_variants, bool):
            return "balance_variants must be true or false."
        start_on_launch = scanner_config.get("start_on_launch")
        if start_on_launch is not None and not isinstance(start_on_launch, bool):
            return "start_on_launch must be true or false."
    if scanner_type == ScannerType.SCORER:
        scale = scanner_config.get("scale")
        if not isinstance(scale, dict):
            return "Scale is required."
        min_v, max_v = scale.get("min"), scale.get("max")
        if not isinstance(min_v, (int, float)) or not isinstance(max_v, (int, float)):
            return "Scale min and max must be numbers."
        if min_v >= max_v:
            return "Scale max must be greater than min."
    try:
        scanner = validate_scanner_config(scanner_config=scanner_config, scanner_type=scanner_type)
    except (ValueError, PydanticValidationError):
        return "Scanner configuration is invalid."
    # The pydantic models ignore extra keys — reject here so typos and junk don't snapshot onto every observation.
    unknown = set(scanner_config) - (set(type(scanner).model_fields) - type(scanner).session_fields)
    if unknown:
        return f"Unknown scanner configuration keys: {', '.join(sorted(unknown))}."
    return None


def acting_user(context: dict[str, Any]) -> User:
    """Who a serializer write is on behalf of.

    Max has no DRF request, so it passes `user` in the context directly. Everything else that reads the
    request stays optional, since `report_user_action` treats a missing one as "no request-derived
    properties" rather than an error.
    """
    request = context.get("request")
    return cast(User, context.get("user") or (request.user if request is not None else None))


def analytics_source_kwargs(context: dict[str, Any]) -> dict[str, Any]:
    """`report_user_action` kwarg that stamps `source`: the request, else the declared `event_source`."""
    request = context.get("request")
    if request is not None:
        return {"request": request}
    event_source: EventSource | None = context.get("event_source")
    if event_source is not None:
        analytics_props: AnalyticsProps = {"source": event_source}
        return {"analytics_props": analytics_props}
    return {}