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.

3.4 KB
Raw Blame
"""Activity-log hooks for business knowledge sources and libraries."""

import dataclasses
from typing import Any

import structlog

from posthog.models.activity_logging.activity_log import (
    ActivityContextBase,
    ActivityScope,
    Detail,
    changes_between,
    log_activity,
)
from posthog.models.activity_logging.model_activity import get_current_trigger
from posthog.models.signals import model_activity_signal, mutable_receiver
from posthog.models.user import User

from .models import KnowledgeLibrary, KnowledgeSource

logger = structlog.get_logger(__name__)


@dataclasses.dataclass(frozen=True)
class KnowledgeSourceContext(ActivityContextBase):
    team_id: int
    source_type: str
    name: str


@mutable_receiver(model_activity_signal, sender=KnowledgeSource)
def handle_knowledge_source_change(
    sender: type[KnowledgeSource],
    scope: ActivityScope,
    before_update: KnowledgeSource | None,
    after_update: KnowledgeSource | None,
    activity: str,
    user: User | None,
    was_impersonated: bool = False,
    **kwargs: Any,
) -> None:
    instance: KnowledgeSource | None = after_update or before_update
    if instance is None:
        return

    context = KnowledgeSourceContext(
        team_id=instance.team_id,
        source_type=instance.source_type,
        name=instance.name,
    )

    if activity == "created":
        detail_name = f"Knowledge source '{instance.name}' added"
    elif activity == "deleted":
        detail_name = f"Knowledge source '{instance.name}' deleted"
    else:
        detail_name = f"Knowledge source '{instance.name}' updated"

    log_activity(
        organization_id=None,
        team_id=instance.team_id,
        user=user,
        was_impersonated=was_impersonated,
        item_id=instance.id,
        scope=scope,
        activity=activity,
        detail=Detail(
            changes=changes_between(scope, previous=before_update, current=after_update),
            name=detail_name,
            # Job-driven saves stash a Trigger so a user-less row still names the job.
            trigger=get_current_trigger(),
            context=context,
        ),
    )


@dataclasses.dataclass(frozen=True)
class KnowledgeLibraryContext(ActivityContextBase):
    team_id: int
    name: str


@mutable_receiver(model_activity_signal, sender=KnowledgeLibrary)
def handle_knowledge_library_change(
    sender: type[KnowledgeLibrary],
    scope: ActivityScope,
    before_update: KnowledgeLibrary | None,
    after_update: KnowledgeLibrary | None,
    activity: str,
    user: User | None,
    was_impersonated: bool = False,
    **kwargs: Any,
) -> None:
    instance: KnowledgeLibrary | None = after_update or before_update
    if instance is None:
        return

    if activity == "created":
        detail_name = f"Library '{instance.name}' added"
    elif activity == "deleted":
        detail_name = f"Library '{instance.name}' deleted"
    else:
        detail_name = f"Library '{instance.name}' updated"

    log_activity(
        organization_id=None,
        team_id=instance.team_id,
        user=user,
        was_impersonated=was_impersonated,
        item_id=instance.id,
        scope=scope,
        activity=activity,
        detail=Detail(
            changes=changes_between(scope, previous=before_update, current=after_update),
            name=detail_name,
            trigger=get_current_trigger(),
            context=KnowledgeLibraryContext(team_id=instance.team_id, name=instance.name),
        ),
    )