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.

refactor(api): share suggested questions runtime for trial apps (#42635)

非法操作f25c4a3eaf62
diff --git a/api/.importlinter b/api/.importlinter
index cee3d8ca3d9..33c4afdedd4 100644
--- a/api/.importlinter
+++ b/api/.importlinter
@@ -584,6 +584,21 @@ forbidden_modules =
sqlalchemy
werkzeug
+[importlinter:contract:message-suggested-questions-boundary]
+name = Message suggested questions port is framework and persistence neutral
+type = forbidden
+source_modules =
+ services.message_suggested_questions_service
+forbidden_modules =
+ controllers
+ extensions
+ flask
+ models
+ repositories
+ services.message_service
+ sqlalchemy
+ werkzeug
+
[importlinter:contract:billing-application-boundary]
name = Billing application services and ports are framework and infrastructure neutral
type = forbidden
diff --git a/api/controllers/console/explore/trial.py b/api/controllers/console/explore/trial.py
index cb0f29a2910..876709af3a5 100644
--- a/api/controllers/console/explore/trial.py
+++ b/api/controllers/console/explore/trial.py
@@ -2,6 +2,7 @@ import logging
from dataclasses import dataclass
from datetime import datetime
from typing import Any, Literal
+from uuid import UUID
from flask import Response, request
from flask_restx import Resource
@@ -53,7 +54,6 @@ from controllers.console.flask_admission import console_account_admission
from controllers.console.remote_files import RemoteFileUploadPayload, upload_remote_file
from controllers.console.wraps import cloud_edition_billing_resource_check, model_validate, with_current_user
from controllers.web.error import InvokeRateLimitError as InvokeRateLimitHttpError
-from core.app.entities.app_invoke_entities import InvokeFrom
from core.errors.error import (
AppInvokeQuotaExceededError,
ModelCurrentlyNotSupportError,
@@ -76,7 +76,7 @@ from machinery.context import RequestContext
from models import Account
from models.account import TenantStatus
from models.enums import CreatorUserRole
-from models.model import AppMode, Site
+from models.model import Site
from models.workflow import Workflow
from services.account_errors import AccountNotFoundError
from services.account_service import TenantService
@@ -99,7 +99,7 @@ from services.errors.message import (
SuggestedQuestionsAfterAnswerDisabledError,
)
from services.file_service import FileUploadActor
-from services.message_service import MessageService
+from services.message_suggested_questions_service import SuggestedQuestionsAccount, SuggestedQuestionsActorNotFoundError
from services.trial_app_access_service import TrialAppRef
from services.trial_app_generation_service import (
TrialAppNotChatError,
@@ -609,25 +609,26 @@ class TrialChatApi(Resource):
raise InternalServerError()
-class TrialMessageSuggestedQuestionApi(TrialAppResource):
+class TrialMessageSuggestedQuestionApi(Resource):
@console_ns.response(200, "Success", console_ns.models[SuggestedQuestionsResponse.__name__])
- @with_current_user
- def get(self, current_user: Account, trial_app, message_id):
- app_model = trial_app
- app_mode = AppMode.value_of(app_model.mode)
- if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT}:
+ @console_account_admission()
+ @get_trial_app
+ def get(self, request_context: RequestContext, trial_app: TrialAppRef, message_id: UUID) -> dict[str, object]:
+ if trial_app.app_mode not in {"chat", "agent-chat", "advanced-chat"}:
raise NotChatAppError()
- message_id = str(message_id)
-
try:
- questions = MessageService.get_suggested_questions_after_answer(
- app_model=app_model,
- user=current_user,
- message_id=message_id,
- invoke_from=InvokeFrom.EXPLORE,
- session=db.session(),
+ questions = application_services().message_suggested_questions.get_suggested_questions(
+ app_id=trial_app.app_id,
+ app_owner_tenant_id=trial_app.tenant_id,
+ expected_app_mode=trial_app.app_mode,
+ actor=SuggestedQuestionsAccount(account_id=request_context.account_id, invoke_from="explore"),
+ message_id=str(message_id),
)
+ except AppDefinitionUnavailableError as error:
+ raise AppUnavailableError() from error
+ except SuggestedQuestionsActorNotFoundError as error:
+ raise Unauthorized("Account no longer exists.") from error
except MessageNotExistsError:
raise NotFound("Message not found")
except ConversationNotExistsError:
@@ -646,7 +647,7 @@ class TrialMessageSuggestedQuestionApi(TrialAppResource):
logger.exception("internal server error.")
raise InternalServerError()
- return {"data": questions}
+ return dump_response(SuggestedQuestionsResponse, {"data": questions})
class TrialChatAudioApi(TrialAppResource):
diff --git a/api/extensions/ext_application_services.py b/api/extensions/ext_application_services.py
index d0ccf52e4dc..64595b1a1ef 100644
--- a/api/extensions/ext_application_services.py
+++ b/api/extensions/ext_application_services.py
@@ -163,6 +163,8 @@ from services.human_input_file_upload_service import HumanInputFileUploadService
from services.init_validation_service import InitValidationService
from services.inner_mail_service import InnerMailService
from services.message_file_preview_service import MessageFilePreviewService
+from services.message_suggested_questions_adapters import MessageSuggestedQuestionsRuntime
+from services.message_suggested_questions_service import MessageSuggestedQuestions
from services.notification_gateway import BillingNotificationGateway
from services.notification_service import NotificationService
from services.notion_data_source_gateway import NotionDataSourceGateway
@@ -275,6 +277,7 @@ class ApplicationServices:
files: FileService
human_input_file_uploads: HumanInputFileUploadService
message_file_previews: MessageFilePreviewService
+ message_suggested_questions: MessageSuggestedQuestions
plugin_file_uploads: PluginFileUploadService
tool_file_downloads: ToolFileDownloadService
upload_file_delivery: UploadFileDeliveryService
@@ -685,6 +688,7 @@ def build_application_services(
files=MessageFilePreviewQueryRepository(session_factory=database_client),
storage=storage,
),
+ message_suggested_questions=MessageSuggestedQuestionsRuntime(session_factory=database_client),
plugin_file_uploads=PluginFileUploadService(
owners=SQLAlchemyPluginFileUploadOwnerRepository(session_factory=database_client),
files=ToolFilePluginUploadGateway(tool_files=ToolFileManager()),
diff --git a/api/services/message_suggested_questions_adapters.py b/api/services/message_suggested_questions_adapters.py
new file mode 100644
index 00000000000..7c0490e227b
--- /dev/null
+++ b/api/services/message_suggested_questions_adapters.py
@@ -0,0 +1,78 @@
+"""Bridge admitted apps and actors to the existing suggested-question runtime.
+
+TODO: Remove this bridge when message configuration, history and model lookups
+share a framework-neutral application boundary. Reuse their current policy here.
+"""
+
+from typing import override
+
+from flask import current_app
+from sqlalchemy import select
+from sqlalchemy.orm import Session, sessionmaker
+
+from core.app.entities.app_invoke_entities import InvokeFrom
+from extensions.ext_database import db
+from models import Account, App
+from models.model import EndUser
+from services.app_definition_query_service import AppDefinitionUnavailableError
+from services.message_service import MessageService
+from services.message_suggested_questions_service import (
+ MessageSuggestedQuestions,
+ SuggestedQuestionsAccount,
+ SuggestedQuestionsActor,
+ SuggestedQuestionsActorNotFoundError,
+)
+
+
+class MessageSuggestedQuestionsRuntime(MessageSuggestedQuestions):
+ def __init__(self, *, session_factory: sessionmaker[Session]) -> None:
+ self._session_factory: sessionmaker[Session] = session_factory
+
+ @override
+ def get_suggested_questions(
+ self,
+ *,
+ app_id: str,
+ app_owner_tenant_id: str,
+ expected_app_mode: str,
+ actor: SuggestedQuestionsActor,
+ message_id: str,
+ ) -> list[str]:
+ with self._session_factory(expire_on_commit=False) as session:
+ app_model = session.get(App, app_id)
+ if app_model is None or app_model.tenant_id != app_owner_tenant_id:
+ raise AppDefinitionUnavailableError(f"App {app_id} no longer exists in tenant {app_owner_tenant_id}")
+ if app_model.mode != expected_app_mode:
+ raise AppDefinitionUnavailableError(
+ f"App {app_id} mode changed from {expected_app_mode} to {app_model.mode} after admission"
+ )
+
+ user: Account | EndUser | None
+ if isinstance(actor, SuggestedQuestionsAccount):
+ user = session.get(Account, actor.account_id)
+ if user is None:
+ raise SuggestedQuestionsActorNotFoundError(f"Account {actor.account_id} no longer exists")
+ else:
+ user = session.scalar(
+ select(EndUser).where(
+ EndUser.id == actor.end_user_id,
+ EndUser.app_id == app_id,
+ EndUser.tenant_id == app_owner_tenant_id,
+ )
+ )
+ if user is None:
+ raise SuggestedQuestionsActorNotFoundError(
+ f"End user {actor.end_user_id} does not exist for app {app_id} in tenant {app_owner_tenant_id}"
+ )
+
+ # Legacy history/model lookups still use db.session internally. Give them
+ # one scoped session, released on success or failure without removing the
+ # caller's session. They may still hold it during provider I/O.
+ with current_app.app_context():
+ return MessageService.get_suggested_questions_after_answer(
+ app_model=app_model,
+ user=user,
+ message_id=message_id,
+ invoke_from=InvokeFrom(actor.invoke_from),
+ session=db.session(),
+ )
diff --git a/api/services/message_suggested_questions_service.py b/api/services/message_suggested_questions_service.py
new file mode 100644
index 00000000000..94486556969
--- /dev/null
+++ b/api/services/message_suggested_questions_service.py
@@ -0,0 +1,39 @@
+"""Shared suggested-question capability for an admitted app and message actor.
+
+Callers own access admission, supported app modes, quotas and HTTP errors.
+The app owner tenant is independent of an account's active workspace.
+"""
+
+from dataclasses import dataclass
+from typing import Literal, Protocol
+
+
+@dataclass(frozen=True, slots=True)
+class SuggestedQuestionsAccount:
+ account_id: str
+ invoke_from: Literal["explore", "debugger"]
+
+
+@dataclass(frozen=True, slots=True)
+class SuggestedQuestionsEndUser:
+ end_user_id: str
+ invoke_from: Literal["web-app", "service-api"]
+
+
+type SuggestedQuestionsActor = SuggestedQuestionsAccount | SuggestedQuestionsEndUser
+
+
+class SuggestedQuestionsActorNotFoundError(LookupError):
+ """The account is missing, or the end user is outside the admitted app scope."""
+
+
+class MessageSuggestedQuestions(Protocol):
+ def get_suggested_questions(
+ self,
+ *,
+ app_id: str,
+ app_owner_tenant_id: str,
+ expected_app_mode: str,
+ actor: SuggestedQuestionsActor,
+ message_id: str,
+ ) -> list[str]: ...
diff --git a/api/tests/unit_tests/controllers/console/explore/test_trial.py b/api/tests/unit_tests/controllers/console/explore/test_trial.py
index 75c55fe1f57..661adea9a72 100644
--- a/api/tests/unit_tests/controllers/console/explore/test_trial.py
+++ b/api/tests/unit_tests/controllers/console/explore/test_trial.py
@@ -10,7 +10,7 @@ from uuid import uuid4
import pytest
from flask import Flask, request
from sqlalchemy.orm import Session
-from werkzeug.exceptions import Forbidden, NotFound
+from werkzeug.exceptions import Forbidden
import controllers.console.explore.trial as module
from controllers.console.app.error import (
@@ -21,7 +21,6 @@ from controllers.console.app.error import (
ProviderQuotaExceededError,
SpeechToTextDisabledError,
)
-from controllers.console.explore.error import NotChatAppError
from controllers.console.explore.trial import TextToSpeechRequest
from core.app.app_config.common.parameters_mapping import get_parameters_from_feature_dict
from core.errors.error import (
@@ -41,7 +40,6 @@ from models.tools import WorkflowToolProvider
from models.workflow import Workflow
from services.app_ref_service import AppRef, MessageRef
from services.errors.audio import SpeechToTextDisabledServiceError
-from services.errors.conversation import ConversationNotExistsError
from tests.unit_tests.model_factories import make_app
unwrap: Any = inspect_unwrap
@@ -178,47 +176,6 @@ def test_trial_app_detail_serializes_with_explicit_session(
module.TrialAppDetailResponse.model_validate.assert_called_once_with(response_view, from_attributes=True)
-class TestTrialMessageSuggestedQuestionApi:
- def test_not_chat_app(self, app: Flask, account: Account) -> None:
- api = module.TrialMessageSuggestedQuestionApi()
- method = unwrap(api.get)
-
- with app.test_request_context("/"):
- with pytest.raises(NotChatAppError):
- method(api, account, _app(app_id="not-chat", mode=AppMode.COMPLETION), str(uuid4()))
-
- def test_success(self, app: Flask, trial_app_chat: App, account: Account) -> None:
- api = module.TrialMessageSuggestedQuestionApi()
- method = unwrap(api.get)
-
- with (
- app.test_request_context("/"),
- patch.object(
- module.MessageService,
- "get_suggested_questions_after_answer",
- return_value=["q1", "q2"],
- ),
- ):
- result = method(api, account, trial_app_chat, str(uuid4()))
-
- assert result == {"data": ["q1", "q2"]}
-
- def test_conversation_not_exists(self, app: Flask, trial_app_chat: App, account: Account) -> None:
- api = module.TrialMessageSuggestedQuestionApi()
- method = unwrap(api.get)
-
- with (
- app.test_request_context("/"),
- patch.object(
- module.MessageService,
- "get_suggested_questions_after_answer",
- side_effect=ConversationNotExistsError(),
- ),
- ):
- with pytest.raises(NotFound):
- method(api, account, trial_app_chat, str(uuid4()))
-
-
class TestTrialAppParameterApi:
def test_app_unavailable(self, unbound_session: Session) -> None:
api = module.TrialAppParameterApi()
diff --git a/api/tests/unit_tests/controllers/console/explore/test_trial_suggested_questions.py b/api/tests/unit_tests/controllers/console/explore/test_trial_suggested_questions.py
new file mode 100644
index 00000000000..debd05ac16b
--- /dev/null
+++ b/api/tests/unit_tests/controllers/console/explore/test_trial_suggested_questions.py
@@ -0,0 +1,526 @@
+"""Trial suggested questions through HTTP, real ownership queries, and SQLite."""
+
+from collections.abc import Callable, Iterator, Sequence
+from dataclasses import dataclass, field
+from decimal import Decimal
+from typing import cast
+from uuid import uuid4
+
+import pytest
+from flask import Flask, Request
+from sqlalchemy import Connection, Engine, delete, event, select, update
+from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
+from werkzeug.test import TestResponse
+
+import controllers.console.explore.trial as trial_module
+import controllers.console.explore.trial_app_admission as admission_module
+import controllers.console.wraps as console_wraps
+import libs.login as login_module
+import services.message_service as message_module
+from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
+from core.model_context import get_credit_usage_metadata
+from core.model_manager import ModelInstance
+from core.ops.ops_trace_manager import TraceTask
+from enums import DeploymentEdition
+from extensions.ext_database import db
+from extensions.ext_login import DifyLoginManager, unauthorized_handler
+from graphon.model_runtime.entities import PromptMessage
+from graphon.model_runtime.entities.model_entities import ModelType
+from graphon.model_runtime.errors.invoke import InvokeError
+from libs.external_api import ExternalApi
+from models import Account, AccountTrialAppRecord, App, AppMode, Conversation, Message, Tenant, TrialApp
+from models.account import AccountStatus
+from models.enums import ConversationFromSource
+from models.model import AppModelConfig
+from repositories.trial_app_repository import TrialAppRepository
+from services.app_definition_query_service import AppDefinitionUnavailableError
+from services.message_suggested_questions_adapters import MessageSuggestedQuestionsRuntime
+from services.message_suggested_questions_service import (
+ SuggestedQuestionsAccount,
+ SuggestedQuestionsActor,
+ SuggestedQuestionsActorNotFoundError,
+)
+from services.trial_app_access_service import TrialAppAccessService
+
+
+@dataclass
+class _Features:
+ enabled: bool = True
+ setup_completed: bool = True
+ events: list[str] = field(default_factory=list)
+
+ def is_trial_enabled(self) -> bool:
+ self.events.append("feature")
+ return self.enabled
+
+
+@dataclass
+class _Provider:
+ read_sessions: list[Session]
+ tenant_ids: list[str] = field(default_factory=list)
+ histories: list[str] = field(default_factory=list)
+ traces: list[TraceTask] = field(default_factory=list)
+ questions: list[str] = field(default_factory=lambda: ["Next question?", "Another question?"])
+ failure: Exception | None = None
+ missing_model: bool = False
+ app_type: str = "chatbot"
+
+ def get_default_model_instance(self, *, tenant_id: str, model_type: ModelType) -> ModelInstance:
+ assert model_type == ModelType.LLM
+ self.tenant_ids.append(tenant_id)
+ if self.missing_model:
+ raise ProviderTokenNotInitError("No default model")
+ return cast(ModelInstance, self)
+
+ def get_llm_num_tokens(self, prompt_messages: Sequence[PromptMessage]) -> int:
+ return len(prompt_messages)
+
+ def generate(
+ self, *, tenant_id: str, histories: str, instruction_prompt: str | None, model_config: object
+ ) -> Sequence[str]:
+ # Admission and ORM reload finish before the legacy runtime's model I/O.
+ assert len(self.read_sessions) == 2
+ assert all(not session.in_transaction() and not session.identity_map for session in self.read_sessions)
+ assert self.tenant_ids == [tenant_id]
+ assert instruction_prompt == "Suggest concise follow-ups"
+ assert model_config is None
+ assert get_credit_usage_metadata() == {"app_type": self.app_type}
+ self.histories.append(histories)
+ if self.failure is not None:
+ raise self.failure
+ return self.questions
+
+ def add_trace_task(self, task: TraceTask) -> None:
+ self.traces.append(task)
+
+
+@dataclass(frozen=True)
+class _ApplicationServices:
+ trial_app_access: TrialAppAccessService
+ recommended_app_queries: _Features
+ message_suggested_questions: MessageSuggestedQuestionsRuntime
+
+
+@dataclass(frozen=True)
+class _Harness:
+ app: Flask
+ account: Account
+ target: App
+ trial: TrialApp
+ conversation: Conversation
+ message: Message
+ config: AppModelConfig
+ factory: sessionmaker[Session]
+ features: _Features
+ provider: _Provider
+ legacy_sessions: list[Session]
+
+ def get(self, *, app_id: str | None = None, message_id: str | None = None) -> TestResponse:
+ return self.app.test_client().get(
+ f"/trial-apps/{app_id or self.target.id}/messages/{message_id or self.message.id}/suggested-questions"
+ )
+
+ def assert_closed(self) -> None:
+ assert all(
+ not session.in_transaction() and not session.identity_map
+ for session in self.provider.read_sessions + self.legacy_sessions
+ )
+
+ def usage(self) -> int | None:
+ with self.factory() as session:
+ return session.scalar(
+ select(AccountTrialAppRecord.count).where(
+ AccountTrialAppRecord.app_id == self.target.id,
+ AccountTrialAppRecord.account_id == self.account.id,
+ )
+ )
+
+
+def harness(
+ monkeypatch: pytest.MonkeyPatch,
+ config_overrides: Callable[..., None],
+ sqlite_session_factory: sessionmaker[Session],
+ sqlite_engine: Engine,
+) -> Iterator[_Harness]:
+ config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY, INIT_PASSWORD="", LOGIN_DISABLED=False)
+ features = _Features()
+ account = Account(name="Trial viewer", email="[email protected]")
+ account._current_tenant = Tenant(name="Viewer workspace")
+ target = App(tenant_id=str(uuid4()), name="Trial app", mode=AppMode.CHAT, enable_site=True, enable_api=False)
+ target.id = str(uuid4())
+ trial = TrialApp(app_id=target.id, tenant_id=str(uuid4()), trial_limit=3)
+ config = AppModelConfig(
+ app_id=target.id,
+ suggested_questions_after_answer='{"enabled":true,"prompt":"Suggest concise follow-ups"}',
+ )
+ with sqlite_session_factory.begin() as session:
+ session.add_all([account, target, trial, config])
+ session.flush()
+ target.app_model_config_id = config.id
+ conversation = Conversation(
+ app_id=target.id,
+ app_model_config_id=config.id,
+ mode=AppMode.CHAT,
+ name="Trial conversation",
+ inputs={},
+ from_source=ConversationFromSource.CONSOLE,
+ from_account_id=account.id,
+ )
+ session.add(conversation)
+ session.flush()
+ message = Message(
+ app_id=target.id,
+ conversation_id=conversation.id,
+ inputs={},
+ query="What is a trial?",
+ message={},
+ message_unit_price=Decimal(0),
+ answer="A way to try an app.",
+ answer_unit_price=Decimal(0),
+ currency="USD",
+ from_source=ConversationFromSource.CONSOLE,
+ from_account_id=account.id,
+ )
+ session.add(message)
+
+ read_sessions: list[Session] = []
+ read_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
+
+ @event.listens_for(read_factory, "after_begin")
+ def track_read(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
+ read_sessions.append(session)
+
+ provider = _Provider(read_sessions)
+ services = _ApplicationServices(
+ trial_app_access=TrialAppAccessService(apps=TrialAppRepository(session_factory=read_factory)),
+ recommended_app_queries=features,
+ message_suggested_questions=MessageSuggestedQuestionsRuntime(session_factory=read_factory),
+ )
+
+ def setup_completed() -> bool:
+ features.events.append("setup")
+ return features.setup_completed
+
+ def csrf(_request: Request, account_id: str) -> None:
+ assert account_id == account.id
+ features.events.append("csrf")
+
+ for module in (trial_module, admission_module):
+ monkeypatch.setattr(module, "application_services", lambda: services)
+ monkeypatch.setattr(console_wraps, "_is_setup_completed", setup_completed)
+ monkeypatch.setattr(login_module, "current_user", account)
+ monkeypatch.setattr(login_module, "check_csrf_token", csrf)
+
+ def model_manager(*, tenant_id: str) -> _Provider:
+ assert tenant_id == target.tenant_id
+ return provider
+
+ def trace_manager(*, app_id: str) -> _Provider:
+ assert app_id == target.id
+ return provider
+
+ monkeypatch.setattr(message_module.ModelManager, "for_tenant", model_manager)
+ monkeypatch.setattr(message_module.LLMGenerator, "generate_suggested_questions_after_answer", provider.generate)
+ monkeypatch.setattr(message_module, "TraceQueueManager", trace_manager)
+
+ app = Flask(__name__)
+ app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
+ db.init_app(app)
+ legacy_sessions: list[Session] = []
+
+ def track_legacy(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
+ legacy_sessions.append(session)
+
+ event.listen(db.session.session_factory, "after_begin", track_legacy)
+ login_manager = DifyLoginManager()
+ login_manager.init_app(app)
+ login_manager.unauthorized_handler(unauthorized_handler)
+ api = ExternalApi(app)
+ api.add_resource(
+ trial_module.TrialMessageSuggestedQuestionApi,
+ "/trial-apps/<uuid:app_id>/messages/<uuid:message_id>/suggested-questions",
+ )
+ yield _Harness(
+ app,
+ account,
+ target,
+ trial,
+ conversation,
+ message,
+ config,
+ sqlite_session_factory,
+ features,
+ provider,
+ legacy_sessions,
+ )
+ event.remove(db.session.session_factory, "after_begin", track_legacy)
+ with app.app_context():
+ db.session.remove()
+ db.engine.dispose()
+
+
+def _assert_error(response: TestResponse, status: int, code: str, message: str | None = None) -> None:
+ assert response.status_code == status, response.get_json()
+ body = response.get_json()
+ assert body["code"] == code
+ assert body["status"] == status
+ assert body["message"]
+ if message is not None:
+ assert body["message"] == message
+ assert response.headers["Content-Type"] == "application/json"
+
+
[email protected]("mode", [AppMode.CHAT, AppMode.AGENT_CHAT])
[email protected]("questions", [["Next question?", "Another question?"], []])
+def test_questions_keep_response_history_owner_and_usage(
+ harness: _Harness, questions: list[str], mode: AppMode
+) -> None:
+ harness.provider.questions = questions
+ harness.provider.app_type = "chatbot" if mode == AppMode.CHAT else "agent"
+ with harness.factory.begin() as session:
+ session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
+ session.execute(update(Conversation).where(Conversation.id == harness.conversation.id).values(mode=mode))
+ with harness.factory.begin() as session:
+ session.add_all(
+ [
+ AccountTrialAppRecord(app_id=harness.target.id, account_id=harness.account.id, count=1),
+ AccountTrialAppRecord(app_id=harness.target.id, account_id=str(uuid4()), count=3),
+ ]
+ )
+
+ response = harness.get()
+
+ assert response.status_code == 200, response.get_json()
+ assert response.get_json() == {"data": questions}
+ assert response.headers["Content-Type"] == "application/json"
+ assert int(response.headers["Content-Length"]) == len(response.data)
+ assert harness.provider.histories == ["Human: What is a trial?\nAssistant: A way to try an app."]
+ assert harness.provider.tenant_ids == [harness.target.tenant_id]
+ assert harness.target.tenant_id not in {harness.trial.tenant_id, harness.account.current_tenant_id}
+ assert len(harness.provider.traces) == 1
+ assert harness.features.events == ["setup", "csrf", "feature"]
+ assert harness.usage() == 1
+ assert len(harness.legacy_sessions) == 1
+ harness.assert_closed()
+
+
[email protected]("entity", ["message", "conversation"])
[email protected]("mismatch", ["missing", "app", "account", "source", "end-user"])
+def test_message_and_conversation_require_complete_owner_chain(harness: _Harness, entity: str, mismatch: str) -> None:
+ message_id = harness.message.id
+ with harness.factory.begin() as session:
+ record = (
+ session.get(Message, harness.message.id)
+ if entity == "message"
+ else session.get(Conversation, harness.conversation.id)
+ )
+ assert record is not None
+ match mismatch:
+ case "missing":
+ if entity == "message":
+ message_id = str(uuid4())
+ else:
+ session.delete(record)
+ case "app":
+ record.app_id = str(uuid4())
+ case "account":
+ record.from_account_id = str(uuid4())
+ case "source":
+ record.from_source = ConversationFromSource.API
+ case "end-user":
+ record.from_end_user_id = str(uuid4())
+
+ response = harness.get(message_id=message_id)
+
+ _assert_error(response, 404, "not_found", "Message not found" if entity == "message" else "Conversation not found")
+ assert harness.provider.histories == []
+ assert harness.usage() is None
+ harness.assert_closed()
+
+
+def test_deleted_conversation_is_not_visible(harness: _Harness) -> None:
+ with harness.factory.begin() as session:
+ session.execute(update(Conversation).where(Conversation.id == harness.conversation.id).values(is_deleted=True))
+ _assert_error(harness.get(), 404, "not_found", "Conversation not found")
+ assert harness.provider.histories == []
+ harness.assert_closed()
+
+
[email protected]("config", [None, '{"enabled":false}'])
+def test_disabled_or_unset_suggestions_have_specific_error(harness: _Harness, config: str | None) -> None:
+ with harness.factory.begin() as session:
+ session.execute(
+ update(AppModelConfig)
+ .where(AppModelConfig.id == harness.config.id)
+ .values(suggested_questions_after_answer=config)
+ )
+ _assert_error(harness.get(), 403, "app_suggested_questions_after_answer_disabled")
+ assert harness.provider.histories == []
+ harness.assert_closed()
+
+
+def test_absent_default_model_keeps_empty_success(harness: _Harness) -> None:
+ harness.provider.missing_model = True
+ response = harness.get()
+ assert response.status_code == 200
+ assert response.get_json() == {"data": []}
+ assert harness.provider.histories == []
+ assert harness.provider.traces == []
+ harness.assert_closed()
+
+
[email protected]("mode", [AppMode.COMPLETION, AppMode.WORKFLOW, AppMode.AGENT])
+def test_unsupported_app_modes_fail_before_runtime(harness: _Harness, mode: AppMode) -> None:
+ with harness.factory.begin() as session:
+ session.execute(update(App).where(App.id == harness.target.id).values(mode=mode))
+ _assert_error(harness.get(), 400, "not_chat_app")
+ assert len(harness.provider.read_sessions) == 1
+ assert harness.legacy_sessions == []
+ harness.assert_closed()
+
+
+ ("denial", "status", "code"),
+ [
+ ("setup", 401, "not_setup"),
+ ("feature", 403, "trial_app_feature_disabled"),
+ ("missing_trial", 403, "trial_app_not_allowed"),
+ ("missing_app", 403, "trial_app_not_allowed"),
+ ("quota", 403, "trial_app_limit_exceeded"),
+ ("uninitialized", 400, "account_not_initialized"),
+ ("unauthenticated", 401, "unauthorized"),
+ ],
+)
+def test_admission_blocks_before_runtime(
+ harness: _Harness, monkeypatch: pytest.MonkeyPatch, denial: str, status: int, code: str
+) -> None:
+ if denial == "setup":
+ harness.features.setup_completed = False
+ elif denial == "feature":
+ harness.features.enabled = False
+ elif denial == "uninitialized":
+ harness.account.status = AccountStatus.UNINITIALIZED
+ elif denial == "unauthenticated":
+ monkeypatch.setattr(login_module, "current_user", None)
+ else:
+ with harness.factory.begin() as session:
+ if denial == "missing_trial":
+ session.execute(delete(TrialApp).where(TrialApp.app_id == harness.target.id))
+ elif denial == "missing_app":
+ session.execute(delete(App).where(App.id == harness.target.id))
+ else:
+ session.add(AccountTrialAppRecord(app_id=harness.target.id, account_id=harness.account.id, count=3))
+ response = harness.get()
+ if denial == "unauthenticated":
+ assert response.status_code == 401
+ assert response.get_json() == {"code": "unauthorized", "message": "Unauthorized."}
+ else:
+ _assert_error(response, status, code)
+ assert harness.legacy_sessions == []
+ assert harness.provider.histories == []
+ harness.assert_closed()
+
+
+ ("failure", "code", "message"),
+ [
+ (
+ ProviderTokenNotInitError("Missing tenant credentials"),
+ "provider_not_initialize",
+ "Missing tenant credentials",
+ ),
+ (QuotaExceededError(), "provider_quota_exceeded", None),
+ (ModelCurrentlyNotSupportError(), "model_currently_not_support", None),
+ (InvokeError("Provider unavailable"), "completion_request_error", "Provider unavailable"),
+ ],
+)
+def test_provider_failures_preserve_specific_errors_and_close_session(
+ harness: _Harness, failure: Exception, code: str, message: str | None
+) -> None:
+ harness.provider.failure = failure
+ _assert_error(harness.get(), 400, code, message)
+ assert len(harness.provider.histories) == 1
+ assert harness.provider.traces == []
+ assert harness.usage() is None
+ harness.assert_closed()
+
+
[email protected]("failure", [None, InvokeError("Provider unavailable")])
+def test_runtime_preserves_outer_request_session(harness: _Harness, failure: Exception | None) -> None:
+ harness.provider.failure = failure
+ with harness.app.app_context():
+ outer_session = db.session()
+ app_model = outer_session.get(App, harness.target.id)
+ assert app_model is not None
+ app_model.name = "Pending caller change"
+
+ response = harness.get()
+
+ assert response.status_code == (200 if failure is None else 400)
+ assert db.session() is outer_session
+ assert outer_session.in_transaction()
+ assert app_model in outer_session.dirty
+ assert app_model.name == "Pending caller change"
+ assert len(harness.legacy_sessions) == 2
+ inner_session = harness.legacy_sessions[1]
+ assert inner_session is not outer_session
+ assert not inner_session.in_transaction()
+ assert not inner_session.identity_map
+ harness.assert_closed()
+ with harness.factory() as session:
+ saved_app = session.get(App, harness.target.id)
+ assert saved_app is not None
+ assert saved_app.name == "Trial app"
+
+
+def test_advanced_chat_without_published_workflow_keeps_empty_success(harness: _Harness) -> None:
+ with harness.factory.begin() as session:
+ session.execute(update(App).where(App.id == harness.target.id).values(mode=AppMode.ADVANCED_CHAT))
+ response = harness.get()
+ assert response.status_code == 200
+ assert response.get_json() == {"data": []}
+ assert len(harness.provider.read_sessions) == 2
+ assert harness.provider.histories == []
+ assert harness.usage() is None
+ harness.assert_closed()
+
+
+ ("failure", "status", "code", "message"),
+ [
+ (AppDefinitionUnavailableError("App changed after admission"), 400, "app_unavailable", None),
+ (SuggestedQuestionsActorNotFoundError("Account disappeared"), 401, "unauthorized", "Account no longer exists."),
+ ],
+)
+def test_reload_errors_have_explicit_http_mapping(
+ harness: _Harness,
+ monkeypatch: pytest.MonkeyPatch,
+ failure: Exception,
+ status: int,
+ code: str,
+ message: str | None,
+) -> None:
+ def reject_reload(
+ _self: MessageSuggestedQuestionsRuntime,
+ *,
+ app_id: str,
+ app_owner_tenant_id: str,
+ expected_app_mode: str,
+ actor: SuggestedQuestionsActor,
+ message_id: str,
+ ) -> list[str]:
+ assert (app_id, app_owner_tenant_id, expected_app_mode, message_id) == (
+ harness.target.id,
+ harness.target.tenant_id,
+ harness.target.mode,
+ harness.message.id,
+ )
+ assert actor == SuggestedQuestionsAccount(account_id=harness.account.id, invoke_from="explore")
+ raise failure
+
+ monkeypatch.setattr(MessageSuggestedQuestionsRuntime, "get_suggested_questions", reject_reload)
+ _assert_error(harness.get(), status, code, message)
+ assert harness.provider.histories == []
+ assert harness.usage() is None
+ harness.assert_closed()
diff --git a/api/tests/unit_tests/services/test_message_suggested_questions_adapters.py b/api/tests/unit_tests/services/test_message_suggested_questions_adapters.py
new file mode 100644
index 00000000000..2f2cea6ef4b
--- /dev/null
+++ b/api/tests/unit_tests/services/test_message_suggested_questions_adapters.py
@@ -0,0 +1,324 @@
+"""Shared runtime ownership, configuration selection, and session lifecycle."""
+
+import json
+from collections.abc import Iterator, Sequence
+from dataclasses import dataclass, field
+from decimal import Decimal
+from typing import Literal, cast
+from uuid import uuid4
+
+import pytest
+from flask import Flask
+from sqlalchemy import Connection, Engine, event, select, update
+from sqlalchemy.orm import Session, SessionTransaction, sessionmaker
+
+import services.message_service as message_module
+from core.model_manager import ModelInstance
+from core.ops.ops_trace_manager import TraceTask
+from extensions.ext_database import db
+from graphon.model_runtime.entities import PromptMessage
+from graphon.model_runtime.entities.model_entities import ModelType
+from models import Account, App, AppMode, Conversation, Message
+from models.enums import ConversationFromSource, EndUserType
+from models.model import AppModelConfig, EndUser
+from models.workflow import Workflow, WorkflowType
+from services.app_definition_query_service import AppDefinitionUnavailableError
+from services.errors.conversation import ConversationNotExistsError
+from services.errors.message import MessageNotExistsError
+from services.message_suggested_questions_adapters import MessageSuggestedQuestionsRuntime
+from services.message_suggested_questions_service import (
+ SuggestedQuestionsAccount,
+ SuggestedQuestionsActor,
+ SuggestedQuestionsActorNotFoundError,
+ SuggestedQuestionsEndUser,
+)
+
+
+@dataclass
+class _Provider:
+ read_sessions: list[Session]
+ tenant_id: str
+ prompts: list[str | None] = field(default_factory=list)
+ histories: list[str] = field(default_factory=list)
+ traces: list[TraceTask] = field(default_factory=list)
+ failure: Exception | None = None
+
+ def get_default_model_instance(self, *, tenant_id: str, model_type: ModelType) -> ModelInstance:
+ assert tenant_id == self.tenant_id
+ assert model_type == ModelType.LLM
+ assert self.read_sessions
+ assert all(not session.in_transaction() and not session.identity_map for session in self.read_sessions)
+ return cast(ModelInstance, self)
+
+ def get_llm_num_tokens(self, prompt_messages: Sequence[PromptMessage]) -> int:
+ return len(prompt_messages)
+
+ def generate(
+ self, *, tenant_id: str, histories: str, instruction_prompt: str | None, model_config: object
+ ) -> list[str]:
+ assert tenant_id == self.tenant_id
+ assert model_config is None
+ self.prompts.append(instruction_prompt)
+ self.histories.append(histories)
+ if self.failure is not None:
+ raise self.failure
+ return ["What next?"]
+
+ def add_trace_task(self, task: TraceTask) -> None:
+ self.traces.append(task)
+
+
+@dataclass(frozen=True)
+class _Harness:
+ flask_app: Flask
+ target: App
+ account: Account
+ end_user: EndUser
+ conversation: Conversation
+ message: Message
+ factory: sessionmaker[Session]
+ runtime: MessageSuggestedQuestionsRuntime
+ provider: _Provider
+ legacy_sessions: list[Session]
+
+ def get(self, actor: SuggestedQuestionsActor) -> list[str]:
+ return self.runtime.get_suggested_questions(
+ app_id=self.target.id,
+ app_owner_tenant_id=self.target.tenant_id,
+ expected_app_mode=self.target.mode,
+ actor=actor,
+ message_id=self.message.id,
+ )
+
+ def assert_closed(self) -> None:
+ assert all(
+ not session.in_transaction() and not session.identity_map
+ for session in self.provider.read_sessions + self.legacy_sessions
+ )
+
+
+def harness(
+ monkeypatch: pytest.MonkeyPatch,
+ sqlite_engine: Engine,
+ sqlite_session_factory: sessionmaker[Session],
+) -> Iterator[_Harness]:
+ target = App(
+ id=str(uuid4()), tenant_id=str(uuid4()), name="Shared", mode=AppMode.CHAT, enable_site=True, enable_api=True
+ )
+ account = Account(name="Debugger", email="[email protected]")
+ end_user = EndUser(
+ tenant_id=target.tenant_id, app_id=target.id, type=EndUserType.SERVICE_API, session_id=str(uuid4())
+ )
+ config = AppModelConfig(
+ app_id=target.id, suggested_questions_after_answer='{"enabled":true,"prompt":"Published questions"}'
+ )
+ with sqlite_session_factory.begin() as session:
+ session.add_all([target, account, end_user, config])
+ session.flush()
+ target.app_model_config_id = config.id
+ conversation = Conversation(
+ app_id=target.id,
+ app_model_config_id=config.id,
+ mode=AppMode.CHAT,
+ name="End-user conversation",
+ inputs={},
+ from_source=ConversationFromSource.API,
+ from_end_user_id=end_user.id,
+ )
+ session.add(conversation)
+ session.flush()
+ message = Message(
+ app_id=target.id,
+ conversation_id=conversation.id,
+ inputs={},
+ query="How does this work?",
+ message={},
+ message_unit_price=Decimal(0),
+ answer="Like this.",
+ answer_unit_price=Decimal(0),
+ currency="USD",
+ from_source=ConversationFromSource.API,
+ from_end_user_id=end_user.id,
+ )
+ session.add(message)
+
+ read_factory = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
+ read_sessions: list[Session] = []
+
+ @event.listens_for(read_factory, "after_begin")
+ def track_read(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
+ read_sessions.append(session)
+
+ provider = _Provider(read_sessions, target.tenant_id)
+
+ def model_manager(*, tenant_id: str) -> _Provider:
+ assert tenant_id == target.tenant_id
+ return provider
+
+ def trace_manager(*, app_id: str) -> _Provider:
+ assert app_id == target.id
+ return provider
+
+ monkeypatch.setattr(message_module.ModelManager, "for_tenant", model_manager)
+ monkeypatch.setattr(message_module.LLMGenerator, "generate_suggested_questions_after_answer", provider.generate)
+ monkeypatch.setattr(message_module, "TraceQueueManager", trace_manager)
+ flask_app = Flask(__name__)
+ flask_app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))
+ db.init_app(flask_app)
+ legacy_sessions: list[Session] = []
+
+ def track_legacy(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:
+ legacy_sessions.append(session)
+
+ event.listen(db.session.session_factory, "after_begin", track_legacy)
+ yield _Harness(
+ flask_app,
+ target,
+ account,
+ end_user,
+ conversation,
+ message,
+ sqlite_session_factory,
+ MessageSuggestedQuestionsRuntime(session_factory=read_factory),
+ provider,
+ legacy_sessions,
+ )
+ event.remove(db.session.session_factory, "after_begin", track_legacy)
+ with flask_app.app_context():
+ db.session.remove()
+ db.engine.dispose()
+
+
[email protected]("change", ["app_id", "tenant", "mode", "account"])
+def test_runtime_rejects_stale_reference_before_legacy_queries(
+ harness: _Harness, change: Literal["app_id", "tenant", "mode", "account"]
+) -> None:
+ account_id = str(uuid4()) if change == "account" else harness.account.id
+ app_id = str(uuid4()) if change == "app_id" else harness.target.id
+ error_type = SuggestedQuestionsActorNotFoundError if change == "account" else AppDefinitionUnavailableError
+ with pytest.raises(error_type, match=account_id if change == "account" else app_id):
+ harness.runtime.get_suggested_questions(
+ app_id=app_id,
+ app_owner_tenant_id=str(uuid4()) if change == "tenant" else harness.target.tenant_id,
+ expected_app_mode=AppMode.ADVANCED_CHAT if change == "mode" else harness.target.mode,
+ actor=SuggestedQuestionsAccount(account_id=account_id, invoke_from="explore"),
+ message_id=harness.message.id,
+ )
+ assert len(harness.provider.read_sessions) == 1
+ assert not harness.legacy_sessions
+ harness.assert_closed()
+
+
[email protected]("change", ["missing", "tenant", "app"])
+def test_end_user_reload_requires_the_admitted_app_owner(
+ harness: _Harness, change: Literal["missing", "tenant", "app"]
+) -> None:
+ end_user_id = str(uuid4()) if change == "missing" else harness.end_user.id
+ if change != "missing":
+ values = {"tenant_id" if change == "tenant" else "app_id": str(uuid4())}
+ with harness.factory.begin() as session:
+ session.execute(update(EndUser).where(EndUser.id == end_user_id).values(values))
+ with pytest.raises(SuggestedQuestionsActorNotFoundError, match=end_user_id):
+ harness.get(SuggestedQuestionsEndUser(end_user_id=end_user_id, invoke_from="service-api"))
+ assert not harness.legacy_sessions
+ harness.assert_closed()
+
+
[email protected]("invoke_from", ["web-app", "service-api"])
[email protected]("provider_failure", [False, True])
+def test_end_user_runtime_preserves_history_and_caller_session(
+ harness: _Harness, invoke_from: Literal["web-app", "service-api"], provider_failure: bool
+) -> None:
+ if provider_failure:
+ harness.provider.failure = RuntimeError("Provider unavailable")
+ actor = SuggestedQuestionsEndUser(end_user_id=harness.end_user.id, invoke_from=invoke_from)
+ with harness.flask_app.app_context():
+ caller_session = db.session()
+ assert caller_session.scalar(select(App.id).where(App.id == harness.target.id)) == harness.target.id
+ harness.legacy_sessions.clear()
+ if provider_failure:
+ with pytest.raises(RuntimeError, match="Provider unavailable"):
+ harness.get(actor)
+ else:
+ assert harness.get(actor) == ["What next?"]
+ assert db.session() is caller_session
+ assert caller_session.in_transaction()
+ assert len(harness.legacy_sessions) == 1
+ harness.assert_closed()
+ assert harness.provider.prompts == ["Published questions"]
+ assert harness.provider.histories == ["Human: How does this work?\nAssistant: Like this."]
+ assert len(harness.provider.traces) == (0 if provider_failure else 1)
+
+
[email protected]("entity", ["message", "conversation"])
[email protected]("change", ["id", "app_id", "from_account_id", "from_end_user_id", "from_source"])
+def test_end_user_message_and_conversation_ownership_are_enforced(
+ harness: _Harness,
+ entity: Literal["message", "conversation"],
+ change: Literal["id", "app_id", "from_account_id", "from_end_user_id", "from_source"],
+) -> None:
+ model = Message if entity == "message" else Conversation
+ record_id = harness.message.id if entity == "message" else harness.conversation.id
+ value = ConversationFromSource.CONSOLE if change == "from_source" else str(uuid4())
+ with harness.factory.begin() as session:
+ session.execute(update(model).where(model.id == record_id).values({change: value}))
+ error_type = MessageNotExistsError if entity == "message" else ConversationNotExistsError
+ with harness.flask_app.app_context(), pytest.raises(error_type):
+ harness.get(SuggestedQuestionsEndUser(end_user_id=harness.end_user.id, invoke_from="service-api"))
+ assert not harness.provider.histories
+ harness.assert_closed()
+
+
+def test_end_user_cannot_read_a_deleted_conversation(harness: _Harness) -> None:
+ with harness.factory.begin() as session:
+ session.execute(update(Conversation).where(Conversation.id == harness.conversation.id).values(is_deleted=True))
+ with harness.flask_app.app_context(), pytest.raises(ConversationNotExistsError):
+ harness.get(SuggestedQuestionsEndUser(end_user_id=harness.end_user.id, invoke_from="web-app"))
+ assert not harness.provider.histories
+ harness.assert_closed()
+
+
[email protected]("invoke_from", ["debugger", "explore"])
+def test_advanced_chat_debugger_uses_draft_while_explore_uses_published_workflow(
+ harness: _Harness, invoke_from: Literal["debugger", "explore"]
+) -> None:
+ with harness.factory.begin() as session:
+ for version, prompt in ((Workflow.VERSION_DRAFT, "Draft questions"), ("published", "Published questions")):
+ workflow = Workflow(
+ tenant_id=harness.target.tenant_id,
+ app_id=harness.target.id,
+ type=WorkflowType.CHAT,
+ version=version,
+ graph='{"nodes":[],"edges":[]}',
+ features=json.dumps({"suggested_questions_after_answer": {"enabled": True, "prompt": prompt}}),
+ created_by=harness.account.id,
+ )
+ session.add(workflow)
+ session.flush()
+ if version != Workflow.VERSION_DRAFT:
+ session.execute(update(App).where(App.id == harness.target.id).values({App.workflow_id: workflow.id}))
+ session.execute(update(App).where(App.id == harness.target.id).values(mode=AppMode.ADVANCED_CHAT))
+ for model, record_id in ((Message, harness.message.id), (Conversation, harness.conversation.id)):
+ session.execute(
+ update(model)
+ .where(model.id == record_id)
+ .values(
+ from_source=ConversationFromSource.CONSOLE,
+ from_account_id=harness.account.id,
+ from_end_user_id=None,
+ )
+ )
+ session.execute(
+ update(Conversation).where(Conversation.id == harness.conversation.id).values(mode=AppMode.ADVANCED_CHAT)
+ )
+ with harness.flask_app.app_context():
+ assert harness.runtime.get_suggested_questions(
+ app_id=harness.target.id,
+ app_owner_tenant_id=harness.target.tenant_id,
+ expected_app_mode=AppMode.ADVANCED_CHAT,
+ actor=SuggestedQuestionsAccount(account_id=harness.account.id, invoke_from=invoke_from),
+ message_id=harness.message.id,
+ ) == ["What next?"]
+ assert harness.provider.prompts == ["Draft questions" if invoke_from == "debugger" else "Published questions"]
+ harness.assert_closed()