Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

3.1 KB
Raw Blame
"""Own short inbox transactions around the persistence repository."""

from datetime import datetime

from sqlalchemy.exc import IntegrityError, SQLAlchemyError
from sqlalchemy.orm import Session, sessionmaker

from core.human_input_v2.im_integration.adapters.entities import AuthenticatedIMEvent
from core.human_input_v2.im_message_inbox.contracts import (
    IMInboxRecord,
    IMInboxRecordId,
    InboxAcceptance,
    InboxPersistenceError,
)
from repositories.human_input_v2.im_channel_repository import IMChannelId
from repositories.human_input_v2.im_message_inbox.repository import SQLAlchemyIMMessageInboxRepository


class IMInboxStorage:
    """Commit intake before transport ACK, with no Provider I/O in transactions."""

    def __init__(self, session_factory: sessionmaker[Session]) -> None:
        self._session_factory = session_factory

    def insert_or_resolve(
        self,
        channel_id: IMChannelId,
        event: AuthenticatedIMEvent,
        *,
        now: datetime,
        request_ip_address: str | None = None,
    ) -> InboxAcceptance:
        try:
            return self._insert(channel_id, event, now=now, request_ip_address=request_ip_address)
        except IntegrityError as error:
            if event.event_id is None:
                raise InboxPersistenceError("failed to persist unidentified IM callback") from error
            # A competing intake may insert after the initial lookup. Its commit
            # is visible only after rolling back our failed transaction.
            try:
                return self._insert(channel_id, event, now=now, request_ip_address=request_ip_address)
            except SQLAlchemyError as retry_error:
                raise InboxPersistenceError("failed to resolve duplicate IM callback") from retry_error
        except SQLAlchemyError as error:
            raise InboxPersistenceError("failed to persist IM callback") from error

    def _insert(
        self,
        channel_id: IMChannelId,
        event: AuthenticatedIMEvent,
        *,
        now: datetime,
        request_ip_address: str | None,
    ) -> InboxAcceptance:
        with self._session_factory() as session, session.begin():
            return SQLAlchemyIMMessageInboxRepository(session).insert_or_resolve(
                channel_id, event, now=now, request_ip_address=request_ip_address
            )

    def get(self, record_id: IMInboxRecordId) -> IMInboxRecord | None:
        try:
            with self._session_factory() as session:
                return SQLAlchemyIMMessageInboxRepository(session).get(record_id)
        except SQLAlchemyError as error:
            raise InboxPersistenceError("failed to load IM callback") from error

    def mark_processed(self, record_id: IMInboxRecordId, *, processed_at: datetime) -> None:
        try:
            with self._session_factory() as session, session.begin():
                SQLAlchemyIMMessageInboxRepository(session).mark_processed(record_id, processed_at=processed_at)
        except SQLAlchemyError as error:
            raise InboxPersistenceError("failed to mark IM callback as processed") from error