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.2 KB
Raw Blame
"""Manage an account's conversations in an admitted installation."""

from services.conversation_command_service import ConversationCommandService
from services.conversation_name_service import ConversationNameService
from services.conversation_query_service import ConversationQueryService
from services.entities.conversation_entities import (
    ConversationPage,
    ConversationRecord,
    InstalledAppConversationRef,
    InstalledAppConversationScope,
)
from services.installed_app_access_service import InstalledAppRef


class ConversationNotChatAppError(ValueError):
    """The installed app does not support conversation management."""


class InstalledAppConversationService:
    def __init__(
        self,
        *,
        queries: ConversationQueryService,
        names: ConversationNameService,
        commands: ConversationCommandService,
    ) -> None:
        self._queries = queries
        self._names = names
        self._commands = commands

    def get_page(
        self, *, installed_app: InstalledAppRef, account_id: str, last_id: str | None, limit: int, pinned: bool | None
    ) -> ConversationPage:
        self._get_chat_mode(installed_app)
        return self._queries.get_page(
            scope=InstalledAppConversationScope(installed_app=installed_app, account_id=account_id),
            invoke_from="explore",
            last_id=last_id,
            limit=limit,
            pinned=pinned,
            sort_by="-updated_at",
        )

    def rename(
        self,
        *,
        installed_app: InstalledAppRef,
        account_id: str,
        conversation_id: str,
        name: str | None,
        auto_generate: bool,
    ) -> ConversationRecord:
        """Rename a conversation manually or generate its name.

        When auto_generate is False, name is required and cannot be None.
        When auto_generate is True, name is ignored.
        """
        app_mode = self._get_chat_mode(installed_app)
        return self._names.rename(
            ref=InstalledAppConversationRef(
                installed_app=installed_app, account_id=account_id, conversation_id=conversation_id
            ),
            app_mode=app_mode,
            name=name,
            auto_generate=auto_generate,
        )

    def delete(self, *, installed_app: InstalledAppRef, account_id: str, conversation_id: str) -> None:
        self._get_chat_mode(installed_app)
        self._commands.delete(
            ref=InstalledAppConversationRef(
                installed_app=installed_app, account_id=account_id, conversation_id=conversation_id
            )
        )

    def set_pinned(
        self, *, installed_app: InstalledAppRef, account_id: str, conversation_id: str, is_pinned: bool
    ) -> None:
        self._get_chat_mode(installed_app)
        self._commands.set_pinned(
            ref=InstalledAppConversationRef(
                installed_app=installed_app, account_id=account_id, conversation_id=conversation_id
            ),
            is_pinned=is_pinned,
        )

    @staticmethod
    def _get_chat_mode(installed_app: InstalledAppRef) -> str:
        mode = installed_app.app_mode
        if mode not in {"chat", "agent-chat", "advanced-chat"}:
            raise ConversationNotChatAppError(f"App {installed_app.app_id} is not a chat app")
        return mode