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 conversation list queries (#43996)

非法操作1f500e52cc30
diff --git a/api/.importlinter b/api/.importlinter
index 9c3cdb6aa8e..7da95e59cf6 100644
--- a/api/.importlinter
+++ b/api/.importlinter
@@ -1040,11 +1040,12 @@ forbidden_modules =
werkzeug
[importlinter:contract:conversation-name-boundary]
-name = Conversation naming and commands are framework and persistence neutral
+name = Conversation naming, commands and queries are framework and persistence neutral
type = forbidden
source_modules =
services.conversation_name_service
services.conversation_command_service
+ services.conversation_query_service
forbidden_modules =
controllers
extensions
diff --git a/api/controllers/common/errors.py b/api/controllers/common/errors.py
index 2985a625f42..ce50ccf3213 100644
--- a/api/controllers/common/errors.py
+++ b/api/controllers/common/errors.py
@@ -99,6 +99,12 @@ class ConversationNotFoundHTTPError(BaseHTTPException):
code = HTTPStatus.NOT_FOUND
+class ConversationCursorNotFoundHTTPError(BaseHTTPException):
+ error_code = "conversation_cursor_not_found"
+ description = "The conversation cursor is not in the current list. Refresh the list and try again."
+ code = HTTPStatus.NOT_FOUND
+
+
class ConversationFirstMessageNotFoundHTTPError(BaseHTTPException):
error_code = "conversation_first_message_not_found"
description = "The conversation has no message from which to generate a name."
diff --git a/api/controllers/service_api/app/conversation.py b/api/controllers/service_api/app/conversation.py
index 3f6cc2bd051..c3d9863c94c 100644
--- a/api/controllers/service_api/app/conversation.py
+++ b/api/controllers/service_api/app/conversation.py
@@ -4,13 +4,16 @@ from typing import Annotated, Any, Literal
from uuid import UUID
from flask_restx import Resource
-from pydantic import BaseModel, Field, TypeAdapter, WithJsonSchema, field_validator
-from sqlalchemy.orm import sessionmaker
+from pydantic import BaseModel, Field, WithJsonSchema, field_validator
from werkzeug.exceptions import BadRequest, NotFound
import services
from controllers.common.controller_schemas import ConversationRenamePayload
-from controllers.common.errors import ConversationFirstMessageNotFoundHTTPError, ConversationNotFoundHTTPError
+from controllers.common.errors import (
+ ConversationCursorNotFoundHTTPError,
+ ConversationFirstMessageNotFoundHTTPError,
+ ConversationNotFoundHTTPError,
+)
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from controllers.console.wraps import model_validate
from controllers.service_api import service_api_ns
@@ -18,14 +21,12 @@ from controllers.service_api.app.error import NotChatAppError
from controllers.service_api.flask_admission import service_api_end_user_admission
from controllers.service_api.schema import expect_user_json, expect_with_user
from controllers.service_api.wraps import FetchUserArg, WhereisUserArg, validate_app_token
-from core.app.entities.app_invoke_entities import InvokeFrom
from extensions.ext_application_services import application_services
from extensions.ext_database import db
from fields._value_type_serializer import serialize_value_type
from fields.base import ResponseModel
from fields.conversation_fields import (
ConversationInfiniteScrollPagination,
- ConversationResponseSource,
SimpleConversation,
)
from graphon.variables.types import SegmentType
@@ -34,7 +35,7 @@ from libs.helper import UUIDStrOrEmpty, dump_response, to_timestamp
from machinery.context import ServiceApiEndUserContext
from models.model import App, AppMode, EndUser
from services.conversation_service import ConversationService
-from services.entities.conversation_entities import EndUserConversationRef
+from services.entities.conversation_entities import EndUserConversationRef, EndUserConversationScope
from services.errors.message import MessageNotExistsError
UUIDString = Annotated[str, WithJsonSchema({"format": "uuid", "type": "string"})]
@@ -166,69 +167,56 @@ register_response_schema_models(
@service_api_ns.route("/conversations")
class ConversationApi(Resource):
- @error_responses(NotChatAppError)
+ @error_responses(NotChatAppError, ConversationCursorNotFoundHTTPError, ConversationNotFoundHTTPError)
@service_api_ns.doc(
summary="List Conversations",
description="Retrieve the conversation list for the current user, ordered by most recently active.",
tags=["Conversations"],
responses={
- 200: "Successfully retrieved conversations list.",
- 400: "`not_chat_app` : App mode does not match the API route.",
- 404: "`not_found` : Last conversation does not exist (invalid `last_id`).",
+ HTTPStatus.OK: "Conversations retrieved successfully.",
+ HTTPStatus.BAD_REQUEST: "Invalid app selection or app mode does not match the API route.",
+ HTTPStatus.UNAUTHORIZED: "Invalid API credentials.",
+ HTTPStatus.FORBIDDEN: "API credential, app or workspace access is unavailable.",
+ HTTPStatus.NOT_FOUND: (
+ "`conversation_cursor_not_found`: Cursor is outside the current list. "
+ "`conversation_not_found`: The app or user is no longer available."
+ ),
+ HTTPStatus.UNPROCESSABLE_ENTITY: "Invalid conversation list query.",
},
)
@service_api_ns.doc("list_conversations")
- @service_api_ns.doc(description="List all conversations for the current user")
@service_api_ns.doc(params=query_params_from_model(ConversationListQuery))
- @service_api_ns.doc(
- responses={
- 200: "Conversations retrieved successfully",
- 401: "Unauthorized - invalid API token",
- 404: "Last conversation not found",
- }
- )
@service_api_ns.response(
- 200,
+ HTTPStatus.OK,
"Conversations retrieved successfully",
service_api_ns.models[ConversationInfiniteScrollPagination.__name__],
)
- @validate_app_token(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))
+ @service_api_end_user_admission(fetch_user_arg=FetchUserArg(fetch_from=WhereisUserArg.QUERY))
@model_validate(ConversationListQuery)
- def get(self, query_args: ConversationListQuery, app_model: App, end_user: EndUser):
- """List all conversations for the current user.
-
- Supports pagination using last_id and limit parameters.
- """
- app_mode = AppMode.value_of(app_model.mode)
+ def get(self, query_args: ConversationListQuery, context: ServiceApiEndUserContext) -> dict[str, object]:
+ """List the current user's conversations, optionally continuing from a cursor."""
+ app_mode = AppMode.value_of(context.app_mode)
if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT}:
raise NotChatAppError()
- last_id = query_args.last_id or None
-
try:
- with sessionmaker(db.engine).begin() as session:
- pagination = ConversationService.pagination_by_last_id(
- session=session,
- app_model=app_model,
- user=end_user,
- last_id=last_id,
- limit=query_args.limit,
- invoke_from=InvokeFrom.SERVICE_API,
- sort_by=query_args.sort_by,
- )
- adapter = TypeAdapter(SimpleConversation)
- conversations = [
- adapter.validate_python(
- ConversationResponseSource(item, session=session),
- from_attributes=True,
- )
- for item in pagination.data
- ]
- return ConversationInfiniteScrollPagination(
- limit=pagination.limit, has_more=pagination.has_more, data=conversations
- ).model_dump(mode="json")
- except services.errors.conversation.LastConversationNotExistsError as caught_error:
- raise NotFound("Last Conversation Not Exists.") from caught_error
+ pagination = application_services().conversation_queries.get_page(
+ scope=EndUserConversationScope(
+ app_id=context.app_id,
+ tenant_id=context.tenant_id,
+ end_user_id=context.end_user_id,
+ ),
+ invoke_from="service-api",
+ last_id=query_args.last_id or None,
+ limit=query_args.limit,
+ pinned=None,
+ sort_by=query_args.sort_by,
+ )
+ return dump_response(ConversationInfiniteScrollPagination, pagination)
+ except services.errors.conversation.LastConversationNotExistsError as error:
+ raise ConversationCursorNotFoundHTTPError() from error
+ except services.errors.conversation.ConversationNotExistsError as error:
+ raise ConversationNotFoundHTTPError() from error
@service_api_ns.route("/conversations/<uuid:conversation_id>")
diff --git a/api/controllers/web/conversation.py b/api/controllers/web/conversation.py
index 4e373ec4b42..b1315c3fa15 100644
--- a/api/controllers/web/conversation.py
+++ b/api/controllers/web/conversation.py
@@ -3,33 +3,31 @@ from typing import Literal
from uuid import UUID
from flask import request
-from pydantic import BaseModel, Field, TypeAdapter, field_validator
-from sqlalchemy.orm import sessionmaker
-from werkzeug.exceptions import NotFound
+from pydantic import BaseModel, Field, field_validator
from controllers.common.controller_schemas import ConversationRenamePayload
-from controllers.common.errors import ConversationFirstMessageNotFoundHTTPError, ConversationNotFoundHTTPError
+from controllers.common.errors import (
+ ConversationCursorNotFoundHTTPError,
+ ConversationFirstMessageNotFoundHTTPError,
+ ConversationNotFoundHTTPError,
+)
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
from controllers.console.wraps import model_validate
from controllers.web import web_ns
from controllers.web.error import NotChatAppError
from controllers.web.wraps import WebApiResource
-from core.app.entities.app_invoke_entities import InvokeFrom
from extensions.ext_application_services import application_services
-from extensions.ext_database import db
from fields.conversation_fields import (
ConversationInfiniteScrollPagination,
- ConversationResponseSource,
ResultResponse,
SimpleConversation,
)
from libs.external_api import error_responses
from libs.helper import dump_response, uuid_value
from models.model import App, AppMode, EndUser
-from services.entities.conversation_entities import EndUserConversationRef
+from services.entities.conversation_entities import EndUserConversationRef, EndUserConversationScope
from services.errors.conversation import ConversationNotExistsError, LastConversationNotExistsError
from services.errors.message import MessageNotExistsError
-from services.web_conversation_service import WebConversationService
class ConversationListQuery(BaseModel):
@@ -52,55 +50,47 @@ register_response_schema_models(web_ns, ConversationInfiniteScrollPagination, Re
@web_ns.route("/conversations")
class ConversationListApi(WebApiResource):
- @error_responses(NotChatAppError)
+ @error_responses(NotChatAppError, ConversationCursorNotFoundHTTPError, ConversationNotFoundHTTPError)
@web_ns.doc("Get Conversation List")
@web_ns.doc(description="Retrieve paginated list of conversations for a chat application.")
@web_ns.doc(params=query_params_from_model(ConversationListQuery))
@web_ns.doc(
responses={
- 200: "Success",
- 400: "Bad Request",
- 401: "Unauthorized",
- 404: "App Not Found or Not a Chat App",
- 500: "Internal Server Error",
+ HTTPStatus.OK: "Success",
+ HTTPStatus.BAD_REQUEST: "Invalid conversation list query or app mode does not match the API route.",
+ HTTPStatus.UNAUTHORIZED: "Unauthorized",
+ HTTPStatus.NOT_FOUND: (
+ "`conversation_cursor_not_found`: Cursor is outside the current list. "
+ "`conversation_not_found`: The app or user is no longer available."
+ ),
+ HTTPStatus.INTERNAL_SERVER_ERROR: "Internal Server Error",
}
)
- @web_ns.response(200, "Success", web_ns.models[ConversationInfiniteScrollPagination.__name__])
- def get(self, app_model: App, end_user: EndUser):
+ @web_ns.response(HTTPStatus.OK, "Success", web_ns.models[ConversationInfiniteScrollPagination.__name__])
+ def get(self, app_model: App, end_user: EndUser) -> dict[str, object]:
app_mode = AppMode.value_of(app_model.mode)
if app_mode not in {AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT}:
raise NotChatAppError()
- raw_args = request.args.to_dict()
- query = ConversationListQuery.model_validate(raw_args)
-
+ query = ConversationListQuery.model_validate(request.args.to_dict(flat=True))
try:
- with sessionmaker(db.engine).begin() as session:
- pagination = WebConversationService.pagination_by_last_id(
- session=session,
- app_model=app_model,
- user=end_user,
- last_id=query.last_id,
- limit=query.limit,
- invoke_from=InvokeFrom.WEB_APP,
- pinned=query.pinned,
- sort_by=query.sort_by,
- )
- adapter = TypeAdapter(SimpleConversation)
- conversations = [
- adapter.validate_python(
- ConversationResponseSource(item, session=session),
- from_attributes=True,
- )
- for item in pagination.data
- ]
- return ConversationInfiniteScrollPagination(
- limit=pagination.limit,
- has_more=pagination.has_more,
- data=conversations,
- ).model_dump(mode="json")
- except LastConversationNotExistsError as caught_error:
- raise NotFound("Last Conversation Not Exists.") from caught_error
+ pagination = application_services().conversation_queries.get_page(
+ scope=EndUserConversationScope(
+ app_id=app_model.id,
+ tenant_id=app_model.tenant_id,
+ end_user_id=end_user.id,
+ ),
+ invoke_from="web-app",
+ last_id=query.last_id,
+ limit=query.limit,
+ pinned=query.pinned,
+ sort_by=query.sort_by,
+ )
+ return dump_response(ConversationInfiniteScrollPagination, pagination)
+ except LastConversationNotExistsError as error:
+ raise ConversationCursorNotFoundHTTPError() from error
+ except ConversationNotExistsError as error:
+ raise ConversationNotFoundHTTPError() from error
@web_ns.route("/conversations/<uuid:c_id>")
diff --git a/api/extensions/application_services/installed_app.py b/api/extensions/application_services/installed_app.py
index 96a7e2ebf69..afe9418ddb2 100644
--- a/api/extensions/application_services/installed_app.py
+++ b/api/extensions/application_services/installed_app.py
@@ -7,8 +7,9 @@ from sqlalchemy.orm import Session, sessionmaker
from repositories.installed_app_repository import SQLAlchemyInstalledAppRepository
from services.conversation_command_service import ConversationCommandService
from services.conversation_name_service import ConversationNameService
+from services.conversation_query_service import ConversationQueryService
from services.installed_app_access_service import InstalledAppAccessService
-from services.installed_app_conversation_service import InstalledAppConversationService, InstalledAppConversationStore
+from services.installed_app_conversation_service import InstalledAppConversationService
from services.installed_app_generation_adapters import AppGenerateServiceRuntime
from services.installed_app_generation_service import InstalledAppGenerationService
from services.installed_app_service import InstalledAppService, WorkspaceRoleLookup
@@ -26,7 +27,7 @@ class InstalledAppServices:
def build_installed_app_services(
*,
database_client: sessionmaker[Session],
- conversations: InstalledAppConversationStore,
+ conversation_queries: ConversationQueryService,
conversation_names: ConversationNameService,
conversation_commands: ConversationCommandService,
webapp_access: WebAppAccessQueryService,
@@ -52,7 +53,7 @@ def build_installed_app_services(
runtime=AppGenerateServiceRuntime(session_factory=database_client),
),
conversations=InstalledAppConversationService(
- conversations=conversations,
+ queries=conversation_queries,
names=conversation_names,
commands=conversation_commands,
),
diff --git a/api/extensions/ext_application_services.py b/api/extensions/ext_application_services.py
index 607de199039..22d3378e2c3 100644
--- a/api/extensions/ext_application_services.py
+++ b/api/extensions/ext_application_services.py
@@ -50,6 +50,7 @@ from extensions.ext_storage import storage
from libs.helper import RateLimiter
from libs.passport import PassportService
from models.model import EndUser
+from models.utils.file_input_compat import restore_input_files
from repositories.account.repository import SQLAlchemyAccountRepository
from repositories.account_activation_repository import SQLAlchemyAccountActivationRepository
from repositories.account_integration_repository import SQLAlchemyAccountIntegrationRepository
@@ -139,6 +140,7 @@ from services.compliance_download_service import ComplianceDownloadService
from services.conversation_command_service import ConversationCommandService
from services.conversation_name_generator import generate_conversation_name
from services.conversation_name_service import ConversationNameService
+from services.conversation_query_service import ConversationQueryService
from services.conversation_service import ConversationService
from services.conversation_variable_query import ConversationVariableQuery
from services.credentials.query import CredentialQuery
@@ -317,6 +319,7 @@ class ApplicationServices:
compliance_downloads: ComplianceDownloadService
conversation_names: ConversationNameService
conversation_commands: ConversationCommandService
+ conversation_queries: ConversationQueryService
data_sources: DataSourceServices
knowledge: KnowledgeServices
app_scoped_end_users: AppScopedEndUserServices
@@ -583,6 +586,7 @@ def build_application_services(
conversation_commands = ConversationCommandService(
conversations=conversations, enqueue_delete_cleanup=ConversationService.enqueue_delete_cleanup
)
+ conversation_queries = ConversationQueryService(conversations=conversations, restore_inputs=restore_input_files)
annotations = AnnotationRepository(session_factory=database_client)
messages = MessageRepository(session_factory=database_client)
suggested_questions = SuggestedQuestionsQuery(
@@ -620,6 +624,7 @@ def build_application_services(
rbac=rbac,
conversation_names=conversation_names,
conversation_commands=conversation_commands,
+ conversation_queries=conversation_queries,
accounts=account_services,
apps=apps,
credential_queries=CredentialQueryRepository(session_factory=database_client),
@@ -693,7 +698,7 @@ def build_application_services(
private_app_access_enabled=deployment_edition == DeploymentEdition.ENTERPRISE,
),
installed_apps=build_installed_app_services(
- conversations=conversations,
+ conversation_queries=conversation_queries,
conversation_names=conversation_names,
conversation_commands=conversation_commands,
database_client=database_client,
diff --git a/api/models/utils/file_input_compat.py b/api/models/utils/file_input_compat.py
index 938ee6e7dd1..e37f230cf74 100644
--- a/api/models/utils/file_input_compat.py
+++ b/api/models/utils/file_input_compat.py
@@ -4,6 +4,8 @@ from collections.abc import Callable, Mapping
from functools import lru_cache
from typing import Any
+from pydantic import JsonValue
+
from core.workflow.file_reference import parse_file_reference
from graphon.file import File, FileTransferMethod, FileType
from graphon.file.constants import FILE_MODEL_IDENTITY, maybe_file_object
@@ -234,3 +236,36 @@ def build_file_from_input_mapping(
file_mapping=file_mapping,
tenant_id=tenant_id,
)
+
+
+def restore_input_files(
+ *,
+ inputs: Mapping[str, JsonValue | File | list[File]],
+ tenant_resolver: Callable[[], str],
+) -> dict[str, JsonValue | File | list[File]]:
+ """Restore top-level file inputs, preserving mixed lists and ordinary mappings.
+
+ Resolve the app tenant lazily: legacy files may already carry their tenant,
+ and external remote files do not need an owner lookup.
+ """
+ restored = dict(inputs)
+ for key, value in inputs.items():
+ match value:
+ case dict() if value.get("dify_model_identity") == FILE_MODEL_IDENTITY:
+ restored[key] = build_file_from_input_mapping(
+ file_mapping=value,
+ tenant_resolver=tenant_resolver,
+ )
+ case list():
+ if all(
+ isinstance(item, dict) and item.get("dify_model_identity") == FILE_MODEL_IDENTITY for item in value
+ ):
+ restored[key] = [
+ build_file_from_input_mapping(
+ file_mapping=item,
+ tenant_resolver=tenant_resolver,
+ )
+ for item in value
+ if isinstance(item, dict)
+ ]
+ return restored
diff --git a/api/openapi/markdown/service-openapi.md b/api/openapi/markdown/service-openapi.md
index d45868ada8e..a769b78ecf7 100644
--- a/api/openapi/markdown/service-openapi.md
+++ b/api/openapi/markdown/service-openapi.md
@@ -2286,11 +2286,12 @@ Retrieve the conversation list for the current user, ordered by most recently ac
| Code | Description | Schema |
| ---- | ----------- | ------ |
-| 200 | Successfully retrieved conversations list. | **application/json**: [ConversationInfiniteScrollPagination](#conversationinfinitescrollpagination)<br> |
-| 400 | `not_chat_app` : App mode does not match the API route. | **application/json**: [NotChatAppError](#notchatapperror)<br>[ErrorResponse](#errorresponse)<br> |
-| 401 | Unauthorized - invalid API token | **application/json**: [ErrorResponse](#errorresponse)<br> |
-| 403 | Forbidden - token scope, app, dataset, or workspace access denied | **application/json**: [ErrorResponse](#errorresponse)<br> |
-| 404 | `not_found` : Last conversation does not exist (invalid `last_id`). | **application/json**: [ErrorResponse](#errorresponse)<br> |
+| 200 | Conversations retrieved successfully. | **application/json**: [ConversationInfiniteScrollPagination](#conversationinfinitescrollpagination)<br> |
+| 400 | Invalid app selection or app mode does not match the API route. | **application/json**: [NotChatAppError](#notchatapperror)<br>[ErrorResponse](#errorresponse)<br> |
+| 401 | Invalid API credentials. | **application/json**: [ErrorResponse](#errorresponse)<br> |
+| 403 | API credential, app or workspace access is unavailable. | **application/json**: [AppAbnormalStatusError](#appabnormalstatuserror)<br>[AppApiDisabledError](#appapidisablederror)<br>[AppNotFoundError](#appnotfounderror)<br>[ForbiddenError](#forbiddenerror)<br>[WorkspaceArchivedError](#workspacearchivederror)<br>[WorkspaceNotFoundError](#workspacenotfounderror)<br>[ErrorResponse](#errorresponse)<br> |
+| 404 | `conversation_cursor_not_found`: Cursor is outside the current list. `conversation_not_found`: The app or user is no longer available. | **application/json**: [ConversationCursorNotFoundError](#conversationcursornotfounderror)<br>[ConversationNotFoundError](#conversationnotfounderror)<br>[ErrorResponse](#errorresponse)<br> |
+| 422 | Invalid conversation list query. | **application/json**: [ErrorResponse](#errorresponse)<br> |
| default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |
### [DELETE] /conversations/{conversation_id}
@@ -4523,6 +4524,14 @@ Condition detail
| name | string | Metadata field name to compare against. | Yes |
| value | string<br>[ string ]<br>number | Value to compare against. Type depends on `comparison_operator`: string for most string operators, array of strings for `in` and `not in`, number for numeric operators, and omit or use `null` for `empty` and `not empty`. | No |
+#### ConversationCursorNotFoundError
+
+The conversation cursor is not in the current list. Refresh the list and try again.
+
+| Name | Type | Description | Required |
+| ---- | ---- | ----------- | -------- |
+| ConversationCursorNotFoundError | | The conversation cursor is not in the current list. Refresh the list and try again. | |
+
#### ConversationFirstMessageNotFoundError
The conversation has no message from which to generate a name.
diff --git a/api/openapi/markdown/web-openapi.md b/api/openapi/markdown/web-openapi.md
index a83031fba6b..7a308a8d7b4 100644
--- a/api/openapi/markdown/web-openapi.md
+++ b/api/openapi/markdown/web-openapi.md
@@ -127,9 +127,9 @@ Retrieve paginated list of conversations for a chat application.
| Code | Description | Schema |
| ---- | ----------- | ------ |
| 200 | Success | **application/json**: [ConversationInfiniteScrollPagination](#conversationinfinitescrollpagination)<br> |
-| 400 | Bad Request | **application/json**: [NotChatAppError](#notchatapperror)<br>[ErrorResponse](#errorresponse)<br> |
+| 400 | Invalid conversation list query or app mode does not match the API route. | **application/json**: [NotChatAppError](#notchatapperror)<br>[ErrorResponse](#errorresponse)<br> |
| 401 | Unauthorized | **application/json**: [ErrorResponse](#errorresponse)<br> |
-| 404 | App Not Found or Not a Chat App | **application/json**: [ErrorResponse](#errorresponse)<br> |
+| 404 | `conversation_cursor_not_found`: Cursor is outside the current list. `conversation_not_found`: The app or user is no longer available. | **application/json**: [ConversationCursorNotFoundError](#conversationcursornotfounderror)<br>[ConversationNotFoundError](#conversationnotfounderror)<br>[ErrorResponse](#errorresponse)<br> |
| 500 | Internal Server Error | **application/json**: [ErrorResponse](#errorresponse)<br> |
| default | Error | **application/json**: [ErrorResponse](#errorresponse)<br> |
@@ -1161,6 +1161,14 @@ Completion request failed.
| ---- | ---- | ----------- | -------- |
| CompletionRequestError | | Completion request failed. | |
+#### ConversationCursorNotFoundError
+
+The conversation cursor is not in the current list. Refresh the list and try again.
+
+| Name | Type | Description | Required |
+| ---- | ---- | ----------- | -------- |
+| ConversationCursorNotFoundError | | The conversation cursor is not in the current list. Refresh the list and try again. | |
+
#### ConversationFirstMessageNotFoundError
The conversation has no message from which to generate a name.
diff --git a/api/repositories/conversation_repository.py b/api/repositories/conversation_repository.py
index b6c95ee836d..2724d1e3619 100644
--- a/api/repositories/conversation_repository.py
+++ b/api/repositories/conversation_repository.py
@@ -3,12 +3,10 @@
from copy import deepcopy
from dataclasses import replace
from datetime import datetime
-from typing import cast
from sqlalchemy import Select, exists, or_, select
from sqlalchemy.orm import Session, sessionmaker
-from core.app.entities.app_invoke_entities import InvokeFrom
from models.enums import ConversationFromSource, CreatorUserRole
from models.model import App, Conversation, EndUser, InstalledApp, Message
from models.web import PinnedConversation
@@ -16,11 +14,15 @@ from repositories.conversation_lifecycle import retire_conversation
from services.entities.conversation_entities import (
ConversationDeletion,
ConversationInputValue,
+ ConversationListSource,
ConversationNameSource,
ConversationPage,
ConversationRecord,
ConversationRef,
+ ConversationScope,
+ ConversationSort,
InstalledAppConversationRef,
+ InstalledAppConversationScope,
)
from services.errors.conversation import ConversationNotExistsError, LastConversationNotExistsError
from services.errors.message import MessageNotExistsError
@@ -34,47 +36,96 @@ class ConversationRepository:
def get(self, *, ref: ConversationRef) -> ConversationRecord:
with self._session_factory() as session:
_, conversation = self._resolve(session=session, ref=ref)
- return self._to_record(conversation=conversation, session=session)
+ return self._to_record(conversation=conversation, inputs=conversation.inputs_with_session(session=session))
def get_page(
- self, *, installed_app: InstalledAppRef, account_id: str, last_id: str | None, limit: int, pinned: bool | None
+ self,
+ *,
+ scope: ConversationScope,
+ invoke_from: ConversationListSource,
+ last_id: str | None,
+ limit: int,
+ pinned: bool | None,
+ sort_by: ConversationSort,
) -> ConversationPage:
with self._session_factory() as session:
- app = self._get_app(session=session, installed_app=installed_app)
- statement = self._conversation_statement(app_id=app.id, account_id=account_id).where(
- or_(Conversation.invoke_from.is_(None), Conversation.invoke_from == InvokeFrom.EXPLORE)
+ if isinstance(scope, InstalledAppConversationScope):
+ app = self._get_app(session=session, installed_app=scope.installed_app)
+ statement = self._conversation_statement(app_id=app.id, account_id=scope.account_id)
+ role, actor_id = CreatorUserRole.ACCOUNT, scope.account_id
+ else:
+ app = session.scalar(
+ select(App)
+ .join(EndUser, EndUser.app_id == App.id)
+ .where(
+ App.id == scope.app_id,
+ App.tenant_id == scope.tenant_id,
+ EndUser.id == scope.end_user_id,
+ EndUser.tenant_id == App.tenant_id,
+ )
+ )
+ if app is None:
+ raise ConversationNotExistsError(
+ f"Conversations for app {scope.app_id} are unavailable for this end user and workspace."
+ )
+ statement = select(Conversation).where(
+ Conversation.app_id == app.id,
+ Conversation.from_source == ConversationFromSource.API,
+ Conversation.from_end_user_id == scope.end_user_id,
+ Conversation.from_account_id.is_(None),
+ Conversation.is_deleted.is_(False),
+ )
+ role, actor_id = CreatorUserRole.END_USER, scope.end_user_id
+ statement = statement.where(
+ or_(Conversation.invoke_from.is_(None), Conversation.invoke_from == invoke_from)
)
if pinned is not None:
pinned_ids = session.scalars(
select(PinnedConversation.conversation_id).where(
PinnedConversation.app_id == app.id,
- PinnedConversation.created_by_role == CreatorUserRole.ACCOUNT,
- PinnedConversation.created_by == account_id,
+ PinnedConversation.created_by_role == role,
+ PinnedConversation.created_by == actor_id,
)
).all()
if pinned:
if not pinned_ids:
- return ConversationPage(limit=limit, has_more=False, data=())
+ return ConversationPage(tenant_id=app.tenant_id, limit=limit, has_more=False, data=())
statement = statement.where(Conversation.id.in_(pinned_ids))
elif pinned_ids:
statement = statement.where(Conversation.id.not_in(pinned_ids))
+ by_created_at = sort_by in {"created_at", "-created_at"}
+ column = Conversation.created_at if by_created_at else Conversation.updated_at
+ descending = sort_by.startswith("-")
if last_id:
last_conversation = session.scalar(statement.where(Conversation.id == last_id))
if last_conversation is None:
raise LastConversationNotExistsError("The last_id cursor does not belong to these conversations.")
- statement = statement.where(Conversation.updated_at < last_conversation.updated_at)
+ timestamp = last_conversation.created_at if by_created_at else last_conversation.updated_at
+ statement = statement.where(column < timestamp if descending else column > timestamp)
- conversations = session.scalars(statement.order_by(Conversation.updated_at.desc()).limit(limit)).all()
+ conversations = session.scalars(
+ statement.order_by(column.desc() if descending else column.asc()).limit(limit)
+ ).all()
# Keep the existing strict timestamp cursor, including tied timestamps.
- has_more = len(conversations) == limit and bool(
- session.scalar(select(exists(statement.where(Conversation.updated_at < conversations[-1].updated_at))))
- )
+ has_more = False
+ if conversations and len(conversations) == limit:
+ last = conversations[-1]
+ timestamp = last.created_at if by_created_at else last.updated_at
+ has_more = bool(
+ session.scalar(
+ select(exists(statement.where(column < timestamp if descending else column > timestamp)))
+ )
+ )
return ConversationPage(
+ tenant_id=app.tenant_id,
limit=limit,
has_more=has_more,
+ # Input restoration can load file metadata. The shared query service
+ # performs it after this read transaction has released its connection.
data=tuple(
- self._to_record(conversation=conversation, session=session) for conversation in conversations
+ self._to_record(conversation=conversation, inputs=conversation._inputs)
+ for conversation in conversations
),
)
@@ -96,7 +147,9 @@ class ConversationRepository:
def rename(self, *, ref: ConversationRef, name: str, updated_at: datetime | None) -> ConversationRecord:
with self._session_factory.begin() as session:
_, conversation = self._resolve(session=session, ref=ref)
- record = self._to_record(conversation=conversation, session=session)
+ record = self._to_record(
+ conversation=conversation, inputs=conversation.inputs_with_session(session=session)
+ )
conversation.name = name
if updated_at is not None:
conversation.updated_at = updated_at
@@ -230,13 +283,11 @@ class ConversationRepository:
return conversation
@staticmethod
- def _to_record(*, conversation: Conversation, session: Session) -> ConversationRecord:
- # Reuse the model's input/file restoration until that legacy I/O boundary
- # is migrated; do not duplicate its reconstruction logic in this repository.
+ def _to_record(*, conversation: Conversation, inputs: dict[str, ConversationInputValue]) -> ConversationRecord:
return ConversationRecord(
id=conversation.id,
name=conversation.name,
- inputs=deepcopy(cast(dict[str, ConversationInputValue], conversation.inputs_with_session(session=session))),
+ inputs=deepcopy(inputs),
status=conversation.status,
introduction=conversation.introduction,
created_at=conversation.created_at,
diff --git a/api/services/conversation_query_service.py b/api/services/conversation_query_service.py
new file mode 100644
index 00000000000..d606804fb07
--- /dev/null
+++ b/api/services/conversation_query_service.py
@@ -0,0 +1,64 @@
+"""Read owned conversations and restore their inputs outside the query transaction."""
+
+from collections.abc import Callable, Mapping
+from dataclasses import replace
+from typing import Protocol
+
+from services.entities.conversation_entities import (
+ ConversationInputValue,
+ ConversationListSource,
+ ConversationPage,
+ ConversationScope,
+ ConversationSort,
+)
+
+
+class ConversationQueryStore(Protocol):
+ def get_page(
+ self,
+ *,
+ scope: ConversationScope,
+ invoke_from: ConversationListSource,
+ last_id: str | None,
+ limit: int,
+ pinned: bool | None,
+ sort_by: ConversationSort,
+ ) -> ConversationPage: ...
+
+
+class ConversationInputRestorer(Protocol):
+ def __call__(
+ self, *, inputs: Mapping[str, ConversationInputValue], tenant_resolver: Callable[[], str]
+ ) -> dict[str, ConversationInputValue]: ...
+
+
+class ConversationQueryService:
+ def __init__(self, *, conversations: ConversationQueryStore, restore_inputs: ConversationInputRestorer) -> None:
+ self._conversations = conversations
+ self._restore_inputs = restore_inputs
+
+ def get_page(
+ self,
+ *,
+ scope: ConversationScope,
+ invoke_from: ConversationListSource,
+ last_id: str | None,
+ limit: int,
+ pinned: bool | None,
+ sort_by: ConversationSort,
+ ) -> ConversationPage:
+ page = self._conversations.get_page(
+ scope=scope, invoke_from=invoke_from, last_id=last_id, limit=limit, pinned=pinned, sort_by=sort_by
+ )
+ # Legacy file reconstruction may query metadata. Keep that compatibility
+ # outside the repository transaction.
+ return replace(
+ page,
+ data=tuple(
+ replace(
+ record,
+ inputs=self._restore_inputs(inputs=record.inputs, tenant_resolver=lambda: page.tenant_id),
+ )
+ for record in page.data
+ ),
+ )
diff --git a/api/services/conversation_service.py b/api/services/conversation_service.py
index 345eed9c75a..e5fd0f0a39f 100644
--- a/api/services/conversation_service.py
+++ b/api/services/conversation_service.py
@@ -1,12 +1,10 @@
import logging
-from collections.abc import Callable, Sequence
from typing import Any
-from sqlalchemy import asc, desc, func, or_, select
+from sqlalchemy import func, select
from sqlalchemy.orm import Session
from configs import dify_config
-from core.app.entities.app_invoke_entities import InvokeFrom
from factories import variable_factory
from graphon.variables.types import SegmentType
from libs.datetime_utils import naive_utc_now
@@ -18,7 +16,6 @@ from services.errors.conversation import (
ConversationNotExistsError,
ConversationVariableNotExistsError,
ConversationVariableTypeMismatchError,
- LastConversationNotExistsError,
)
from tasks.collect_agent_resources_task import enqueue_agent_resource_collection
from tasks.delete_conversation_task import delete_conversation_related_data
@@ -27,89 +24,6 @@ logger = logging.getLogger(__name__)
class ConversationService:
- @classmethod
- def pagination_by_last_id(
- cls,
- *,
- session: Session,
- app_model: App,
- user: Account | EndUser | None,
- last_id: str | None,
- limit: int,
- invoke_from: InvokeFrom,
- include_ids: Sequence[str] | None = None,
- exclude_ids: Sequence[str] | None = None,
- sort_by: str = "-updated_at",
- ) -> InfiniteScrollPagination:
- if not user:
- return InfiniteScrollPagination(data=[], limit=limit, has_more=False)
-
- stmt = select(Conversation).where(
- Conversation.is_deleted.is_(False),
- Conversation.app_id == app_model.id,
- Conversation.from_source == ("api" if isinstance(user, EndUser) else "console"),
- Conversation.from_end_user_id == (user.id if isinstance(user, EndUser) else None),
- Conversation.from_account_id == (user.id if isinstance(user, Account) else None),
- or_(Conversation.invoke_from.is_(None), Conversation.invoke_from == invoke_from.value),
- )
- # Check if include_ids is not None to apply filter
- if include_ids is not None:
- if len(include_ids) == 0:
- # If include_ids is empty, return empty result
- return InfiniteScrollPagination(data=[], limit=limit, has_more=False)
- stmt = stmt.where(Conversation.id.in_(include_ids))
- # Check if exclude_ids is not None to apply filter
- if exclude_ids is not None:
- if len(exclude_ids) > 0:
- stmt = stmt.where(~Conversation.id.in_(exclude_ids))
-
- # define sort fields and directions
- sort_field, sort_direction = cls._get_sort_params(sort_by)
-
- if last_id:
- last_conversation = session.scalar(stmt.where(Conversation.id == last_id))
- if not last_conversation:
- raise LastConversationNotExistsError()
-
- # build filters based on sorting
- filter_condition = cls._build_filter_condition(
- sort_field=sort_field,
- sort_direction=sort_direction,
- reference_conversation=last_conversation,
- )
- stmt = stmt.where(filter_condition)
- query_stmt = stmt.order_by(sort_direction(getattr(Conversation, sort_field))).limit(limit)
- conversations = session.scalars(query_stmt).all()
-
- has_more = False
- if len(conversations) == limit:
- current_page_last_conversation = conversations[-1]
- rest_filter_condition = cls._build_filter_condition(
- sort_field=sort_field,
- sort_direction=sort_direction,
- reference_conversation=current_page_last_conversation,
- )
- count_stmt = select(func.count()).select_from(stmt.where(rest_filter_condition).subquery())
- rest_count = session.scalar(count_stmt) or 0
- if rest_count > 0:
- has_more = True
-
- return InfiniteScrollPagination(data=conversations, limit=limit, has_more=has_more)
-
- @classmethod
- def _get_sort_params(cls, sort_by: str):
- if sort_by.startswith("-"):
- return sort_by[1:], desc
- return sort_by, asc
-
- @classmethod
- def _build_filter_condition(cls, sort_field: str, sort_direction: Callable, reference_conversation: Conversation):
- field_value = getattr(reference_conversation, sort_field)
- if sort_direction is desc:
- return getattr(Conversation, sort_field) < field_value
-
- return getattr(Conversation, sort_field) > field_value
-
@classmethod
def get_conversation(
cls, app_model: App, conversation_id: str, user: Account | EndUser | None, *, session: Session
diff --git a/api/services/entities/conversation_entities.py b/api/services/entities/conversation_entities.py
index 9ce6948fc8c..bf03ac43288 100644
--- a/api/services/entities/conversation_entities.py
+++ b/api/services/entities/conversation_entities.py
@@ -2,6 +2,7 @@
from dataclasses import dataclass
from datetime import datetime
+from typing import Literal
from pydantic import JsonValue
@@ -9,6 +10,8 @@ from graphon.file import File
from services.installed_app_access_service import InstalledAppRef
type ConversationInputValue = JsonValue | File | list[File]
+type ConversationListSource = Literal["web-app", "service-api", "explore"]
+type ConversationSort = Literal["created_at", "-created_at", "updated_at", "-updated_at"]
@dataclass(frozen=True, slots=True)
@@ -29,30 +32,40 @@ class ConversationNameSource:
@dataclass(frozen=True, slots=True)
-class EndUserConversationRef:
+class EndUserConversationScope:
app_id: str
tenant_id: str
end_user_id: str
+
+
+@dataclass(frozen=True, slots=True)
+class EndUserConversationRef(EndUserConversationScope):
conversation_id: str
@dataclass(frozen=True, slots=True)
-class InstalledAppConversationRef:
+class InstalledAppConversationScope:
# An installation can belong to a different workspace than its underlying app.
installed_app: InstalledAppRef
account_id: str
- conversation_id: str
@property
def app_id(self) -> str:
return self.installed_app.app_id
+@dataclass(frozen=True, slots=True)
+class InstalledAppConversationRef(InstalledAppConversationScope):
+ conversation_id: str
+
+
+type ConversationScope = EndUserConversationScope | InstalledAppConversationScope
type ConversationRef = EndUserConversationRef | InstalledAppConversationRef
@dataclass(frozen=True, slots=True)
class ConversationPage:
+ tenant_id: str
limit: int
has_more: bool
data: tuple[ConversationRecord, ...]
diff --git a/api/services/installed_app_conversation_service.py b/api/services/installed_app_conversation_service.py
index cd741185b3b..80fd8db85ea 100644
--- a/api/services/installed_app_conversation_service.py
+++ b/api/services/installed_app_conversation_service.py
@@ -1,13 +1,13 @@
"""Manage an account's conversations in an admitted installation."""
-from typing import Protocol
-
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
@@ -16,21 +16,15 @@ class ConversationNotChatAppError(ValueError):
"""The installed app does not support conversation management."""
-class InstalledAppConversationStore(Protocol):
- def get_page(
- self, *, installed_app: InstalledAppRef, account_id: str, last_id: str | None, limit: int, pinned: bool | None
- ) -> ConversationPage: ...
-
-
class InstalledAppConversationService:
def __init__(
self,
*,
- conversations: InstalledAppConversationStore,
+ queries: ConversationQueryService,
names: ConversationNameService,
commands: ConversationCommandService,
) -> None:
- self._conversations: InstalledAppConversationStore = conversations
+ self._queries = queries
self._names = names
self._commands = commands
@@ -38,8 +32,13 @@ class InstalledAppConversationService:
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._conversations.get_page(
- installed_app=installed_app, account_id=account_id, last_id=last_id, limit=limit, pinned=pinned
+ 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(
diff --git a/api/services/web_conversation_service.py b/api/services/web_conversation_service.py
deleted file mode 100644
index d6d5cda594c..00000000000
--- a/api/services/web_conversation_service.py
+++ /dev/null
@@ -1,57 +0,0 @@
-from sqlalchemy import select
-from sqlalchemy.orm import Session
-
-from core.app.entities.app_invoke_entities import InvokeFrom
-from libs.infinite_scroll_pagination import InfiniteScrollPagination
-from models import Account
-from models.model import App, EndUser
-from models.web import PinnedConversation
-from services.conversation_service import ConversationService
-
-
-class WebConversationService:
- @classmethod
- def pagination_by_last_id(
- cls,
- *,
- session: Session,
- app_model: App,
- user: Account | EndUser | None,
- last_id: str | None,
- limit: int,
- invoke_from: InvokeFrom,
- pinned: bool | None = None,
- sort_by="-updated_at",
- ) -> InfiniteScrollPagination:
- if not user:
- raise ValueError("User is required")
- include_ids = None
- exclude_ids = None
- if pinned is not None and user:
- stmt = (
- select(PinnedConversation.conversation_id)
- .where(
- PinnedConversation.app_id == app_model.id,
- PinnedConversation.created_by_role == ("account" if isinstance(user, Account) else "end_user"),
- PinnedConversation.created_by == user.id,
- )
- .order_by(PinnedConversation.created_at.desc())
- )
- pinned_conversation_ids = session.scalars(stmt).all()
-
- if pinned:
- include_ids = pinned_conversation_ids
- else:
- exclude_ids = pinned_conversation_ids
-
- return ConversationService.pagination_by_last_id(
- session=session,
- app_model=app_model,
- user=user,
- last_id=last_id,
- limit=limit,
- invoke_from=invoke_from,
- include_ids=include_ids,
- exclude_ids=exclude_ids,
- sort_by=sort_by,
- )
diff --git a/api/tests/e2e_tests/containers/controllers/web/test_conversation.py b/api/tests/e2e_tests/containers/controllers/web/test_conversation.py
deleted file mode 100644
index aa199e179ff..00000000000
--- a/api/tests/e2e_tests/containers/controllers/web/test_conversation.py
+++ /dev/null
@@ -1,56 +0,0 @@
-"""Testcontainers integration tests for controllers.web.conversation endpoints."""
-
-from __future__ import annotations
-
-from types import SimpleNamespace
-from unittest.mock import MagicMock, patch
-from uuid import uuid4
-
-import pytest
-from flask import Flask
-
-from controllers.web.conversation import ConversationListApi
-from controllers.web.error import NotChatAppError
-
-
-def _chat_app() -> SimpleNamespace:
- return SimpleNamespace(id="app-1", mode="chat")
-
-
-def _completion_app() -> SimpleNamespace:
- return SimpleNamespace(id="app-1", mode="completion")
-
-
-def _end_user() -> SimpleNamespace:
- return SimpleNamespace(id="eu-1")
-
-
-class TestConversationListApi:
- @pytest.fixture
- def app(self, flask_app_with_containers: Flask):
- return flask_app_with_containers
-
- def test_non_chat_mode_raises(self, app: Flask) -> None:
- with app.test_request_context("/conversations"):
- with pytest.raises(NotChatAppError):
- ConversationListApi().get(_completion_app(), _end_user())
-
- @patch("controllers.web.conversation.WebConversationService.pagination_by_last_id")
- def test_happy_path(self, mock_paginate: MagicMock, app: Flask) -> None:
- conv_id = str(uuid4())
- conv = SimpleNamespace(
- id=conv_id,
- name="Test",
- inputs={},
- status="normal",
- introduction="",
- created_at=1700000000,
- updated_at=1700000000,
- )
- mock_paginate.return_value = SimpleNamespace(limit=20, has_more=False, data=[conv])
-
- with app.test_request_context("/conversations?limit=20"):
- result = ConversationListApi().get(_chat_app(), _end_user())
-
- assert result["limit"] == 20
- assert result["has_more"] is False
diff --git a/api/tests/e2e_tests/containers/pyrefly.toml b/api/tests/e2e_tests/containers/pyrefly.toml
index 779ac89b7a9..f16ba47a7bc 100644
--- a/api/tests/e2e_tests/containers/pyrefly.toml
+++ b/api/tests/e2e_tests/containers/pyrefly.toml
@@ -23,7 +23,6 @@ project-excludes = [
"controllers/openapi/test_files.py",
"controllers/openapi/test_workspaces.py",
"controllers/service_api/dataset/test_dataset.py",
- "controllers/web/test_conversation.py",
"controllers/web/test_site.py",
"controllers/web/test_wraps.py",
"core/app/layers/test_pause_state_persist_layer.py",
@@ -86,7 +85,6 @@ project-excludes = [
"services/test_model_provider_service.py",
"services/test_restore_archived_workflow_run.py",
"services/test_schedule_service.py",
- "services/test_web_conversation_service.py",
"services/test_webapp_auth_service.py",
"services/test_webhook_service.py",
"services/test_webhook_service_relationships.py",
diff --git a/api/tests/e2e_tests/containers/services/test_conversation_service.py b/api/tests/e2e_tests/containers/services/test_conversation_service.py
index ead1419394d..1838195d656 100644
--- a/api/tests/e2e_tests/containers/services/test_conversation_service.py
+++ b/api/tests/e2e_tests/containers/services/test_conversation_service.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from datetime import datetime, timedelta
+from datetime import datetime
from decimal import Decimal
from unittest.mock import patch
from uuid import uuid4
@@ -166,147 +166,6 @@ class ConversationServiceIntegrationTestDataFactory:
return message
-class TestConversationServicePagination:
- """Test conversation pagination operations."""
-
- def test_pagination_with_non_empty_include_ids(self, db_session_with_containers: Session):
- """
- Test that non-empty include_ids filters properly.
-
- When include_ids contains conversation IDs, the query should filter
- to only return conversations matching those IDs.
- """
- # Arrange - Set up test data and mocks
- app_model, user = ConversationServiceIntegrationTestDataFactory.create_app_and_account(
- db_session_with_containers
- )
- conversations = [
- ConversationServiceIntegrationTestDataFactory.create_conversation(
- db_session_with_containers, app_model, user
- )
- for _ in range(3)
- ]
-
- # Act
- result = ConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app_model,
- user=user,
- last_id=None,
- limit=20,
- invoke_from=InvokeFrom.WEB_APP,
- include_ids=[conversations[0].id, conversations[1].id],
- exclude_ids=None,
- )
-
- # Assert
- returned_ids = {conversation.id for conversation in result.data}
- assert returned_ids == {conversations[0].id, conversations[1].id}
-
- def test_pagination_with_empty_exclude_ids(self, db_session_with_containers: Session):
- """
- Test that empty exclude_ids doesn't filter.
-
- When exclude_ids is an empty list, the query should not filter out
- any conversations.
- """
- # Arrange
- app_model, user = ConversationServiceIntegrationTestDataFactory.create_app_and_account(
- db_session_with_containers
- )
- conversations = [
- ConversationServiceIntegrationTestDataFactory.create_conversation(
- db_session_with_containers, app_model, user
- )
- for _ in range(5)
- ]
-
- # Act
- result = ConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app_model,
- user=user,
- last_id=None,
- limit=20,
- invoke_from=InvokeFrom.WEB_APP,
- include_ids=None,
- exclude_ids=[],
- )
-
- # Assert
- assert len(result.data) == len(conversations)
-
- def test_pagination_with_non_empty_exclude_ids(self, db_session_with_containers: Session):
- """
- Test that non-empty exclude_ids filters properly.
-
- When exclude_ids contains conversation IDs, the query should filter
- out conversations matching those IDs.
- """
- # Arrange
- app_model, user = ConversationServiceIntegrationTestDataFactory.create_app_and_account(
- db_session_with_containers
- )
- conversations = [
- ConversationServiceIntegrationTestDataFactory.create_conversation(
- db_session_with_containers, app_model, user
- )
- for _ in range(3)
- ]
-
- # Act
- result = ConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app_model,
- user=user,
- last_id=None,
- limit=20,
- invoke_from=InvokeFrom.WEB_APP,
- include_ids=None,
- exclude_ids=[conversations[0].id, conversations[1].id],
- )
-
- # Assert
- returned_ids = {conversation.id for conversation in result.data}
- assert returned_ids == {conversations[2].id}
-
- def test_pagination_with_sorting_descending(self, db_session_with_containers: Session):
- """
- Test pagination with descending sort order.
-
- Verifies that conversations are sorted by updated_at in descending order (newest first).
- """
- # Arrange
- app_model, user = ConversationServiceIntegrationTestDataFactory.create_app_and_account(
- db_session_with_containers
- )
-
- base_time = datetime(2024, 1, 1, 12, 0, 0)
- for i in range(3):
- ConversationServiceIntegrationTestDataFactory.create_conversation(
- db_session_with_containers,
- app_model,
- user,
- updated_at=base_time + timedelta(minutes=i),
- )
-
- # Act
- result = ConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app_model,
- user=user,
- last_id=None,
- limit=20,
- invoke_from=InvokeFrom.WEB_APP,
- sort_by="-updated_at",
- )
-
- # Assert
- assert len(result.data) == 3
- assert result.data[0].updated_at >= result.data[1].updated_at
- assert result.data[1].updated_at >= result.data[2].updated_at
-
-
class TestConversationServiceRetrieval:
"""
Test conversation and message retrieval operations.
diff --git a/api/tests/e2e_tests/containers/services/test_conversation_service_variables.py b/api/tests/e2e_tests/containers/services/test_conversation_service_variables.py
index 2113ffedff1..14a27d0244a 100644
--- a/api/tests/e2e_tests/containers/services/test_conversation_service_variables.py
+++ b/api/tests/e2e_tests/containers/services/test_conversation_service_variables.py
@@ -18,7 +18,6 @@ from services.conversation_service import ConversationService
from services.errors.conversation import (
ConversationVariableNotExistsError,
ConversationVariableTypeMismatchError,
- LastConversationNotExistsError,
)
@@ -388,138 +387,3 @@ class TestConversationServiceVariables:
assert persisted is not None
assert persisted.to_variable().value == 42
assert result["value"] == 42
-
-
-class TestConversationServicePaginationWithContainers:
- def test_pagination_by_last_id_raises_error_when_last_id_missing(self, db_session_with_containers: Session):
- factory = ConversationServiceVariableIntegrationFactory
- app, account = factory.create_app_and_account(db_session_with_containers)
-
- with pytest.raises(LastConversationNotExistsError):
- ConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app,
- user=account,
- last_id=str(uuid4()),
- limit=20,
- invoke_from=InvokeFrom.WEB_APP,
- )
-
- def test_pagination_by_last_id_with_default_desc_updated_at(self, db_session_with_containers: Session):
- factory = ConversationServiceVariableIntegrationFactory
- app, account = factory.create_app_and_account(db_session_with_containers)
- base_time = datetime(2024, 1, 1, 8, 0, 0)
- newest = factory.create_conversation(
- db_session_with_containers,
- app,
- account,
- name="Newest",
- updated_at=base_time + timedelta(minutes=2),
- )
- middle = factory.create_conversation(
- db_session_with_containers,
- app,
- account,
- name="Middle",
- updated_at=base_time + timedelta(minutes=1),
- )
- oldest = factory.create_conversation(
- db_session_with_containers,
- app,
- account,
- name="Oldest",
- updated_at=base_time,
- )
-
- result = ConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app,
- user=account,
- last_id=middle.id,
- limit=10,
- invoke_from=InvokeFrom.WEB_APP,
- )
-
- assert newest.id != middle.id
- assert [conversation.id for conversation in result.data] == [oldest.id]
-
- def test_pagination_by_last_id_with_name_sort(self, db_session_with_containers: Session):
- factory = ConversationServiceVariableIntegrationFactory
- app, account = factory.create_app_and_account(db_session_with_containers)
- alpha = factory.create_conversation(db_session_with_containers, app, account, name="Alpha")
- beta = factory.create_conversation(db_session_with_containers, app, account, name="Beta")
- gamma = factory.create_conversation(db_session_with_containers, app, account, name="Gamma")
-
- result = ConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app,
- user=account,
- last_id=beta.id,
- limit=10,
- invoke_from=InvokeFrom.WEB_APP,
- sort_by="name",
- )
-
- assert alpha.id != beta.id
- assert [conversation.id for conversation in result.data] == [gamma.id]
-
- def test_pagination_filters_to_end_user_api_source(self, db_session_with_containers: Session):
- factory = ConversationServiceVariableIntegrationFactory
- app, account = factory.create_app_and_account(db_session_with_containers)
- end_user = factory.create_end_user(db_session_with_containers, app)
- account_conversation = factory.create_conversation(
- db_session_with_containers,
- app,
- account,
- name="Console Conversation",
- invoke_from=InvokeFrom.WEB_APP,
- )
- end_user_conversation = factory.create_conversation(
- db_session_with_containers,
- app,
- end_user,
- name="API Conversation",
- invoke_from=InvokeFrom.SERVICE_API,
- )
-
- result = ConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app,
- user=end_user,
- last_id=None,
- limit=20,
- invoke_from=InvokeFrom.SERVICE_API,
- )
-
- assert account_conversation.id != end_user_conversation.id
- assert [conversation.id for conversation in result.data] == [end_user_conversation.id]
-
- def test_pagination_filters_to_account_console_source(self, db_session_with_containers: Session):
- factory = ConversationServiceVariableIntegrationFactory
- app, account = factory.create_app_and_account(db_session_with_containers)
- end_user = factory.create_end_user(db_session_with_containers, app)
- account_conversation = factory.create_conversation(
- db_session_with_containers,
- app,
- account,
- name="Console Conversation",
- invoke_from=InvokeFrom.WEB_APP,
- )
- factory.create_conversation(
- db_session_with_containers,
- app,
- end_user,
- name="API Conversation",
- invoke_from=InvokeFrom.SERVICE_API,
- )
-
- result = ConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app,
- user=account,
- last_id=None,
- limit=20,
- invoke_from=InvokeFrom.WEB_APP,
- )
-
- assert [conversation.id for conversation in result.data] == [account_conversation.id]
diff --git a/api/tests/e2e_tests/containers/services/test_web_conversation_service.py b/api/tests/e2e_tests/containers/services/test_web_conversation_service.py
deleted file mode 100644
index 28aa37324a6..00000000000
--- a/api/tests/e2e_tests/containers/services/test_web_conversation_service.py
+++ /dev/null
@@ -1,321 +0,0 @@
-from unittest.mock import patch
-
-import pytest
-from faker import Faker
-from sqlalchemy.orm import Session
-
-from core.app.entities.app_invoke_entities import InvokeFrom
-from models import Account, App
-from models.enums import ConversationFromSource, EndUserType
-from models.model import Conversation, EndUser
-from models.web import PinnedConversation
-from services.app_service import AppService, CreateAppParams
-from services.web_conversation_service import WebConversationService
-from tests.e2e_tests.containers.helpers import accounts as account_fixtures
-from tests.e2e_tests.containers.helpers import generate_valid_password
-
-
-class TestWebConversationService:
- """Integration tests for WebConversationService using testcontainers."""
-
- @pytest.fixture
- def mock_external_service_dependencies(self):
- """Mock setup for external service dependencies."""
- with (
- patch("services.app_service.SystemFeatureService") as mock_feature_service,
- patch("services.app_service.EnterpriseService") as mock_enterprise_service,
- patch("services.app_service.ModelManager.for_tenant") as mock_model_manager,
- patch("services.account.login_adapters.SystemFeatureService") as mock_account_feature_service,
- ):
- # Setup default mock returns for app service
- mock_feature_service.is_webapp_auth_enabled.return_value = False
- mock_enterprise_service.WebAppAuth.update_app_access_mode.return_value = None
- mock_enterprise_service.WebAppAuth.cleanup_webapp.return_value = None
-
- # Setup default mock returns for account service
- mock_account_feature_service.is_registration_allowed.return_value = True
-
- # Mock ModelManager for model configuration
- mock_model_instance = mock_model_manager.return_value
- mock_model_instance.get_default_model_instance.return_value = None
- mock_model_instance.get_default_provider_model_name.return_value = ("openai", "gpt-3.5-turbo")
-
- yield {
- "feature_service": mock_feature_service,
- "enterprise_service": mock_enterprise_service,
- "model_manager": mock_model_manager,
- "account_feature_service": mock_account_feature_service,
- }
-
- def _create_test_app_and_account(self, db_session_with_containers: Session, mock_external_service_dependencies):
- """
- Helper method to create a test app and account for testing.
-
- Args:
- db_session_with_containers: Database session from testcontainers infrastructure
- mock_external_service_dependencies: Mock dependencies
-
- Returns:
- tuple: (app, account) - Created app and account instances
- """
- fake = Faker()
-
- # Setup mocks for account creation
- mock_external_service_dependencies["account_feature_service"].is_registration_allowed.return_value = True
-
- # Create account and tenant
- account = account_fixtures.create_account(
- email=fake.email(),
- name=fake.name(),
- interface_language="en-US",
- password=generate_valid_password(fake),
- session=db_session_with_containers,
- )
- account_fixtures.create_owner_workspace(account, name=fake.company(), session=db_session_with_containers)
- tenant = account.current_tenant
-
- # Create app with realistic data
- app_args = CreateAppParams(
- name=fake.company(),
- description=fake.text(max_nb_chars=100),
- mode="chat",
- icon_type="emoji",
- icon="🤖",
- icon_background="#FF6B6B",
- api_rph=100,
- api_rpm=10,
- )
-
- app_service = AppService()
- app = app_service.create_app(tenant.id, app_args, account, session=db_session_with_containers)
-
- return app, account
-
- def _create_test_end_user(self, db_session_with_containers: Session, app: App):
- """
- Helper method to create a test end user for testing.
-
- Args:
- db_session_with_containers: Database session from testcontainers infrastructure
- app: App instance
-
- Returns:
- EndUser: Created end user instance
- """
- fake = Faker()
-
- end_user = EndUser(
- session_id=fake.uuid4(),
- app_id=app.id,
- type=EndUserType.BROWSER,
- _is_anonymous=False,
- tenant_id=app.tenant_id,
- )
-
- db_session_with_containers.add(end_user)
- db_session_with_containers.commit()
-
- return end_user
-
- def _create_test_conversation(self, db_session_with_containers: Session, app, user, fake):
- """
- Helper method to create a test conversation for testing.
-
- Args:
- db_session_with_containers: Database session from testcontainers infrastructure
- app: App instance
- user: User instance (Account or EndUser)
- fake: Faker instance
-
- Returns:
- Conversation: Created conversation instance
- """
- conversation = Conversation(
- app_id=app.id,
- app_model_config_id=app.app_model_config_id,
- model_provider="openai",
- model_id="gpt-3.5-turbo",
- mode="chat",
- name=fake.sentence(nb_words=3),
- summary=fake.text(max_nb_chars=100),
- _inputs={},
- introduction=fake.text(max_nb_chars=200),
- system_instruction=fake.text(max_nb_chars=300),
- system_instruction_tokens=50,
- status="normal",
- invoke_from=InvokeFrom.WEB_APP,
- from_source=ConversationFromSource.CONSOLE if isinstance(user, Account) else ConversationFromSource.API,
- from_end_user_id=user.id if isinstance(user, EndUser) else None,
- from_account_id=user.id if isinstance(user, Account) else None,
- dialogue_count=0,
- is_deleted=False,
- )
-
- db_session_with_containers.add(conversation)
- db_session_with_containers.commit()
-
- return conversation
-
- def test_pagination_by_last_id_success(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test successful pagination by last ID with basic parameters.
- """
- fake = Faker()
- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
-
- # Create multiple conversations
- conversations = []
- for _i in range(5):
- conversation = self._create_test_conversation(db_session_with_containers, app, account, fake)
- conversations.append(conversation)
-
- # Test pagination without pinned filter
- result = WebConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app,
- user=account,
- last_id=None,
- limit=3,
- invoke_from=InvokeFrom.WEB_APP,
- pinned=None,
- sort_by="-updated_at",
- )
-
- # Verify results
- assert result.limit == 3
- assert len(result.data) == 3
- assert result.has_more is True
-
- # Verify conversations are in descending order by updated_at
- assert result.data[0].updated_at >= result.data[1].updated_at
- assert result.data[1].updated_at >= result.data[2].updated_at
-
- def test_pagination_by_last_id_with_pinned_filter(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test pagination by last ID with pinned conversation filter.
- """
- fake = Faker()
- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
-
- # Create conversations
- conversations = []
- for _i in range(5):
- conversation = self._create_test_conversation(db_session_with_containers, app, account, fake)
- conversations.append(conversation)
-
- # Pin some conversations
- pinned_conversation1 = PinnedConversation(
- app_id=app.id,
- conversation_id=conversations[0].id,
- created_by_role="account",
- created_by=account.id,
- )
- pinned_conversation2 = PinnedConversation(
- app_id=app.id,
- conversation_id=conversations[2].id,
- created_by_role="account",
- created_by=account.id,
- )
-
- db_session_with_containers.add(pinned_conversation1)
- db_session_with_containers.add(pinned_conversation2)
- db_session_with_containers.commit()
-
- # Test pagination with pinned filter
- result = WebConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app,
- user=account,
- last_id=None,
- limit=10,
- invoke_from=InvokeFrom.WEB_APP,
- pinned=True,
- sort_by="-updated_at",
- )
-
- # Verify only pinned conversations are returned
- assert result.limit == 10
- assert len(result.data) == 2
- assert result.has_more is False
-
- # Verify the returned conversations are the pinned ones
- returned_ids = [conv.id for conv in result.data]
- expected_ids = [conversations[0].id, conversations[2].id]
- assert set(returned_ids) == set(expected_ids)
-
- def test_pagination_by_last_id_with_unpinned_filter(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test pagination by last ID with unpinned conversation filter.
- """
- fake = Faker()
- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
-
- # Create conversations
- conversations = []
- for _i in range(5):
- conversation = self._create_test_conversation(db_session_with_containers, app, account, fake)
- conversations.append(conversation)
-
- # Pin one conversation
- pinned_conversation = PinnedConversation(
- app_id=app.id,
- conversation_id=conversations[0].id,
- created_by_role="account",
- created_by=account.id,
- )
-
- db_session_with_containers.add(pinned_conversation)
- db_session_with_containers.commit()
-
- # Test pagination with unpinned filter
- result = WebConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app,
- user=account,
- last_id=None,
- limit=10,
- invoke_from=InvokeFrom.WEB_APP,
- pinned=False,
- sort_by="-updated_at",
- )
-
- # Verify unpinned conversations are returned (should be 4 out of 5)
- assert result.limit == 10
- assert len(result.data) == 4
- assert result.has_more is False
-
- # Verify the pinned conversation is not in the results
- returned_ids = [conv.id for conv in result.data]
- assert conversations[0].id not in returned_ids
-
- # Verify all other conversations are in the results
- expected_unpinned_ids = [conv.id for conv in conversations[1:]]
- assert set(returned_ids) == set(expected_unpinned_ids)
-
- def test_pagination_by_last_id_user_required_error(
- self, db_session_with_containers: Session, mock_external_service_dependencies
- ):
- """
- Test that pagination_by_last_id raises ValueError when user is None.
- """
-
- app, account = self._create_test_app_and_account(db_session_with_containers, mock_external_service_dependencies)
-
- # Test with None user
- with pytest.raises(ValueError, match="User is required"):
- WebConversationService.pagination_by_last_id(
- session=db_session_with_containers,
- app_model=app,
- user=None,
- last_id=None,
- limit=10,
- invoke_from=InvokeFrom.WEB_APP,
- pinned=None,
- sort_by="-updated_at",
- )
diff --git a/api/tests/e2e_tests/contracts/controllers/service_api/app/test_conversation_list.py b/api/tests/e2e_tests/contracts/controllers/service_api/app/test_conversation_list.py
new file mode 100644
index 00000000000..9bd67138274
--- /dev/null
+++ b/api/tests/e2e_tests/contracts/controllers/service_api/app/test_conversation_list.py
@@ -0,0 +1,281 @@
+"""List contracts with real token admission, shared queries, and SQLite."""
+
+from collections.abc import Mapping
+from dataclasses import dataclass
+from datetime import timedelta
+from http import HTTPStatus
+from uuid import uuid4
+
+import pytest
+from sqlalchemy import event, select, update
+from sqlalchemy.orm import Session, SessionTransaction
+from werkzeug.test import TestResponse
+
+from constants.resource_access_token import ResourceAccessTokenResourceType
+from controllers.service_api.app.conversation import ConversationApi
+from machinery.context import RequestContext
+from models import Tenant, TenantStatus
+from models.enums import ConversationFromSource, InvokeFrom
+from models.model import App, AppMode, Conversation, EndUser
+from models.resource_access_token import ResourceAccessToken
+from models.utils.file_input_compat import restore_input_files
+from repositories.conversation_repository import ConversationRepository
+from services.auth.resource_access_token_contracts import ResourceAccessTokenResource
+from services.conversation_query_service import ConversationQueryService
+from services.entities.conversation_entities import ConversationSort
+from tests.e2e_tests.contracts.controllers.service_api.app import test_conversation_rename as rename_tests
+from tests.e2e_tests.contracts.controllers.service_api.app.test_conversation_rename import _Harness as RenameHarness
+from tests.e2e_tests.contracts.controllers.service_api.app.test_conversation_rename import _Services as RenameServices
+from tests.helpers.unit.model_factories import make_app, make_conversation
+
+rename_harness = rename_tests.harness
+
+
+@dataclass(frozen=True)
+class _Services(RenameServices):
+ conversation_queries: ConversationQueryService
+
+
+@dataclass(frozen=True)
+class _Harness:
+ source: RenameHarness
+
+ def get(
+ self,
+ query: Mapping[str, str | int] | None = None,
+ *,
+ user: str | None = "alice",
+ authorization: str | None = "Bearer rename-token",
+ requested_app_id: str | None = None,
+ ) -> TestResponse:
+ headers = {"Authorization": authorization} if authorization is not None else {}
+ if requested_app_id is not None:
+ headers["X-Dify-App-ID"] = requested_app_id
+ args = dict(query or {})
+ if user is not None:
+ args["user"] = user
+ return self.source.app.test_client().get("/conversations", query_string=args, headers=headers)
+
+
+def harness(rename_harness: RenameHarness) -> _Harness:
+ original = rename_harness.app.extensions["application_services"]
+ assert isinstance(original, RenameServices)
+ rename_harness.app.extensions["application_services"] = _Services(
+ app_definitions=original.app_definitions,
+ app_scoped_end_users=original.app_scoped_end_users,
+ conversation_names=original.conversation_names,
+ resource_access_tokens=original.resource_access_tokens,
+ conversation_queries=ConversationQueryService(
+ conversations=ConversationRepository(session_factory=rename_harness.repository_factory),
+ restore_inputs=restore_input_files,
+ ),
+ )
+ rename_harness.api.add_resource(ConversationApi, "/conversations")
+ return _Harness(rename_harness)
+
+
[email protected]("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])
+def test_list_preserves_wire_contract_and_closes_admission_before_query(harness: _Harness, mode: AppMode) -> None:
+ source = harness.source
+ with source.factory.begin() as session:
+ session.execute(update(App).where(App.id == source.target.id).values(mode=mode))
+ response = harness.get()
+ assert response.status_code == HTTPStatus.OK
+ assert response.headers["Content-Type"] == "application/json"
+ assert response.get_json() == {
+ "limit": 20,
+ "has_more": False,
+ "data": [
+ {
+ "id": source.conversation.id,
+ "name": "Original name",
+ "inputs": {"text": "", "list": [], "zero": 0, "flag": False, "null": None},
+ "status": "normal",
+ "introduction": None,
+ "created_at": int(source.conversation.created_at.timestamp()),
+ "updated_at": int(source.conversation.updated_at.timestamp()),
+ }
+ ],
+ }
+ assert source.admissions
+ assert source.provisions
+ assert source.queries
+ assert source.scoped_sessions == []
+ source.assert_closed()
+
+
[email protected]("sort_by", ["created_at", "-created_at", "updated_at", "-updated_at"])
+def test_sort_cursor_and_empty_last_id_follow_existing_contract(harness: _Harness, sort_by: ConversationSort) -> None:
+ source = harness.source
+ newest = make_conversation(
+ conversation_id=str(uuid4()),
+ app_id=source.target.id,
+ from_source=ConversationFromSource.API,
+ from_end_user_id=source.end_user.id,
+ inputs={},
+ )
+ newest.created_at = source.conversation.created_at + timedelta(days=1)
+ newest.updated_at = source.conversation.updated_at + timedelta(days=1)
+ newest.invoke_from = InvokeFrom.SERVICE_API
+ with source.factory.begin() as session:
+ session.add(newest)
+ ordered_ids = [source.conversation.id, newest.id]
+ if sort_by.startswith("-"):
+ ordered_ids.reverse()
+ first = harness.get({"sort_by": sort_by, "limit": 1, "last_id": ""})
+ assert first.status_code == HTTPStatus.OK
+ assert first.get_json()["has_more"] is True
+ assert [item["id"] for item in first.get_json()["data"]] == ordered_ids[:1]
+ second = harness.get({"sort_by": sort_by, "limit": 1, "last_id": ordered_ids[0]})
+ assert second.status_code == HTTPStatus.OK
+ assert second.get_json()["has_more"] is False
+ assert [item["id"] for item in second.get_json()["data"]] == ordered_ids[1:]
+ source.assert_closed()
+
+
+ "field", ["app_id", "from_end_user_id", "from_source", "from_account_id", "is_deleted", "invoke_from"]
+)
+def test_list_and_cursor_do_not_cross_user_app_or_invocation_scope(harness: _Harness, field: str) -> None:
+ source = harness.source
+ value: str | bool
+ if field == "is_deleted":
+ value = True
+ elif field == "from_source":
+ value = ConversationFromSource.CONSOLE
+ elif field == "invoke_from":
+ value = InvokeFrom.WEB_APP.value
+ else:
+ value = str(uuid4())
+ with source.factory.begin() as session:
+ session.execute(update(Conversation).where(Conversation.id == source.conversation.id).values({field: value}))
+ response = harness.get()
+ assert response.status_code == HTTPStatus.OK
+ assert response.get_json() == {"limit": 20, "has_more": False, "data": []}
+ cursor = harness.get({"last_id": source.conversation.id})
+ assert cursor.status_code == HTTPStatus.NOT_FOUND
+ assert cursor.get_json() == {
+ "code": "conversation_cursor_not_found",
+ "message": "The conversation cursor is not in the current list. Refresh the list and try again.",
+ "status": HTTPStatus.NOT_FOUND,
+ }
+ source.assert_closed()
+
+
[email protected]("query", [{"limit": "0"}, {"limit": "101"}, {"last_id": "invalid"}, {"sort_by": "name"}])
+def test_invalid_query_returns_422_before_querying(harness: _Harness, query: dict[str, str]) -> None:
+ response = harness.get(query)
+ assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
+ assert response.get_json()["code"] == "unprocessable_entity"
+ assert harness.source.queries == []
+ harness.source.assert_closed()
+
+
[email protected]("user", [None, "", "another-user"])
+def test_new_or_anonymous_user_receives_empty_list(harness: _Harness, user: str | None) -> None:
+ response = harness.get(user=user)
+ assert response.status_code == HTTPStatus.OK
+ assert response.get_json() == {"limit": 20, "has_more": False, "data": []}
+ with harness.source.factory() as session:
+ assert len(session.scalars(select(EndUser)).all()) == 2
+ harness.source.assert_closed()
+
+
[email protected]("authorization", [None, "Basic rename-token", "Bearer invalid-token"])
+def test_invalid_token_preserves_authentication_error_and_header(harness: _Harness, authorization: str | None) -> None:
+ source = harness.source
+ response = harness.get(authorization=authorization)
+ assert response.status_code == HTTPStatus.UNAUTHORIZED
+ assert response.headers["WWW-Authenticate"] == 'Bearer realm="api"'
+ assert response.get_json()["code"] == "unauthorized"
+ assert source.admissions == source.provisions == source.queries == []
+ source.assert_closed()
+
+
[email protected]("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
+def test_non_chat_mode_is_rejected_before_querying(harness: _Harness, mode: AppMode) -> None:
+ source = harness.source
+ with source.factory.begin() as session:
+ session.execute(update(App).where(App.id == source.target.id).values(mode=mode))
+ response = harness.get()
+ assert response.status_code == HTTPStatus.BAD_REQUEST
+ assert response.get_json()["code"] == "not_chat_app"
+ assert source.queries == []
+ source.assert_closed()
+
+
[email protected]("change_app_too", [False, True], ids=["end-user-workspace", "app-workspace"])
+def test_workspace_change_after_admission_prevents_list(harness: _Harness, change_app_too: bool) -> None:
+ source = harness.source
+
+ def change_owner(_session: Session, _transaction: SessionTransaction) -> None:
+ with source.factory.begin() as session:
+ end_user = session.get(EndUser, source.end_user.id)
+ assert end_user is not None
+ end_user.tenant_id = str(uuid4())
+ if change_app_too:
+ target = session.get(App, source.target.id)
+ assert target is not None
+ target.tenant_id = end_user.tenant_id
+
+ event.listen(source.provision_factory, "after_transaction_end", change_owner, once=True)
+ try:
+ response = harness.get()
+ finally:
+ event.remove(source.provision_factory, "after_transaction_end", change_owner)
+ assert response.status_code == HTTPStatus.NOT_FOUND
+ assert response.get_json()["code"] == "conversation_not_found"
+ source.assert_closed()
+
+
+ ("failure", "code"), [("disabled_api", "app_api_disabled"), ("archived_workspace", "workspace_archived")]
+)
+def test_disabled_app_or_workspace_is_rejected_before_provisioning(harness: _Harness, failure: str, code: str) -> None:
+ source = harness.source
+ with source.factory.begin() as session:
+ if failure == "disabled_api":
+ session.execute(update(App).where(App.id == source.target.id).values(enable_api=False))
+ else:
+ session.execute(update(Tenant).where(Tenant.id == source.tenant.id).values(status=TenantStatus.ARCHIVE))
+ response = harness.get(user="new-user")
+ assert response.status_code == HTTPStatus.FORBIDDEN
+ assert response.get_json()["code"] == code
+ assert source.provisions == source.queries == []
+ source.assert_closed()
+
+
[email protected]("multiple_apps", [False, True], ids=["implicit-single-app", "explicit-multiple-apps"])
+def test_resource_token_lists_bound_app_and_records_usage(harness: _Harness, multiple_apps: bool) -> None:
+ source = harness.source
+ app_ids = (source.target.id,)
+ if multiple_apps:
+ other = make_app(app_id=str(uuid4()), tenant_id=source.tenant.id)
+ with source.factory.begin() as session:
+ session.add(other)
+ app_ids = (other.id, source.target.id)
+ services = source.app.extensions["application_services"]
+ assert isinstance(services, _Services)
+ token = services.resource_access_tokens.create(
+ RequestContext(
+ request_id="list-resource-token",
+ trace_id=None,
+ account_id=str(uuid4()),
+ active_workspace_id=source.tenant.id,
+ ),
+ name="List app token",
+ resources=tuple(ResourceAccessTokenResource(ResourceAccessTokenResourceType.APP, app_id) for app_id in app_ids),
+ )
+ response = harness.get(
+ authorization=f"Bearer {token.token}",
+ requested_app_id=source.target.id if multiple_apps else None,
+ )
+ assert response.status_code == HTTPStatus.OK
+ assert [item["id"] for item in response.get_json()["data"]] == [source.conversation.id]
+ source.assert_closed()
+ with source.factory() as session:
+ stored_token = session.get(ResourceAccessToken, token.token_id)
+ assert stored_token is not None
+ assert stored_token.last_used_at is not None
+ assert session.scalars(select(EndUser.id)).all() == [source.end_user.id]
diff --git a/api/tests/e2e_tests/contracts/controllers/web/test_conversation_list.py b/api/tests/e2e_tests/contracts/controllers/web/test_conversation_list.py
new file mode 100644
index 00000000000..8612830865d
--- /dev/null
+++ b/api/tests/e2e_tests/contracts/controllers/web/test_conversation_list.py
@@ -0,0 +1,295 @@
+"""Conversation lists through real Web admission, queries, and SQLite reads."""
+
+from collections.abc import Callable, Mapping
+from dataclasses import dataclass
+from datetime import timedelta
+from http import HTTPStatus
+from uuid import uuid4
+
+import pytest
+from pydantic import JsonValue
+from sqlalchemy import Engine, event, update
+from sqlalchemy.orm import Session, SessionTransaction
+from werkzeug.test import TestResponse
+
+from constants import HEADER_NAME_APP_CODE, HEADER_NAME_PASSPORT
+from controllers.web.conversation import ConversationListApi
+from graphon.file import File, FileTransferMethod, FileType
+from models.enums import ConversationFromSource, CreatorUserRole, InvokeFrom
+from models.model import App, AppMode, Conversation, EndUser
+from models.utils.file_input_compat import restore_input_files
+from models.web import PinnedConversation
+from repositories.conversation_repository import ConversationRepository
+from services.conversation_query_service import ConversationQueryService
+from services.entities.conversation_entities import ConversationSort
+from tests.e2e_tests.contracts.controllers.web import test_conversation_rename as rename_tests
+from tests.e2e_tests.contracts.controllers.web.test_conversation_rename import _Harness as RenameHarness
+from tests.helpers.unit.model_factories import make_conversation
+
+rename_harness = rename_tests.harness
+
+
+@dataclass(frozen=True)
+class _Services:
+ conversation_queries: ConversationQueryService
+
+
+@dataclass(frozen=True)
+class _Harness:
+ source: RenameHarness
+ restored_inputs: list[dict[str, JsonValue | File | list[File]]]
+
+ def get(self, query: Mapping[str, str | int] | None = None) -> TestResponse:
+ return self.source.app.test_client().get(
+ "/conversations",
+ query_string=query,
+ headers={HEADER_NAME_APP_CODE: "public-app", HEADER_NAME_PASSPORT: self.source.passport},
+ )
+
+
+def harness(rename_harness: RenameHarness) -> _Harness:
+ restored_inputs: list[dict[str, JsonValue | File | list[File]]] = []
+
+ def restore(
+ *,
+ inputs: Mapping[str, JsonValue | File | list[File]],
+ tenant_resolver: Callable[[], str],
+ ) -> dict[str, JsonValue | File | list[File]]:
+ # Exercise the real restoration, observing its transaction boundary.
+ rename_harness.assert_closed()
+ restored = restore_input_files(inputs=inputs, tenant_resolver=tenant_resolver)
+ restored_inputs.append(restored)
+ return restored
+
+ rename_harness.app.extensions["application_services"] = _Services(
+ conversation_queries=ConversationQueryService(
+ conversations=ConversationRepository(session_factory=rename_harness.repository_factory),
+ restore_inputs=restore,
+ )
+ )
+ rename_harness.api.add_resource(ConversationListApi, "/conversations")
+ return _Harness(rename_harness, restored_inputs)
+
+
[email protected]("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])
+def test_list_preserves_wire_contract_and_restores_inputs_after_session_close(harness: _Harness, mode: AppMode) -> None:
+ source = harness.source
+ rename_tests._publish_mode(source, mode)
+ response = harness.get()
+ assert response.status_code == HTTPStatus.OK
+ assert response.headers["Content-Type"] == "application/json"
+ assert response.get_json() == {
+ "limit": 20,
+ "has_more": False,
+ "data": [
+ {
+ "id": source.conversation.id,
+ "name": "Original title",
+ "inputs": {"text": "", "list": [], "zero": 0, "flag": False, "null": None},
+ "status": "normal",
+ "introduction": None,
+ "created_at": int(source.conversation.created_at.timestamp()),
+ "updated_at": int(source.conversation.updated_at.timestamp()),
+ }
+ ],
+ }
+ assert len(harness.restored_inputs) == 1
+ assert source.repository_sessions
+ source.assert_closed()
+
+
[email protected]("sort_by", ["created_at", "-created_at", "updated_at", "-updated_at"])
+def test_sort_and_cursor_return_the_next_page(harness: _Harness, sort_by: ConversationSort) -> None:
+ source = harness.source
+ newest = make_conversation(
+ conversation_id=str(uuid4()),
+ app_id=source.target.id,
+ from_source=ConversationFromSource.API,
+ from_end_user_id=source.end_user.id,
+ inputs={},
+ )
+ newest.created_at = source.conversation.created_at + timedelta(days=1)
+ newest.updated_at = source.conversation.updated_at + timedelta(days=1)
+ newest.invoke_from = InvokeFrom.WEB_APP
+ with source.factory.begin() as session:
+ session.add(newest)
+ ordered_ids = [source.conversation.id, newest.id]
+ if sort_by.startswith("-"):
+ ordered_ids.reverse()
+
+ first = harness.get({"sort_by": sort_by, "limit": 1, "last_id": ""})
+ assert first.status_code == HTTPStatus.OK
+ assert first.get_json()["has_more"] is True
+ assert [item["id"] for item in first.get_json()["data"]] == ordered_ids[:1]
+ second = harness.get({"sort_by": sort_by, "limit": 1, "last_id": ordered_ids[0]})
+ assert second.status_code == HTTPStatus.OK
+ assert second.get_json()["has_more"] is False
+ assert [item["id"] for item in second.get_json()["data"]] == ordered_ids[1:]
+ last = harness.get({"sort_by": sort_by, "last_id": ordered_ids[1]})
+ assert last.get_json() == {"limit": 20, "has_more": False, "data": []}
+ source.assert_closed()
+
+
+ "field", ["app_id", "from_end_user_id", "from_source", "from_account_id", "is_deleted", "invoke_from"]
+)
+def test_list_and_cursor_cannot_expose_other_owners_or_invocation_sources(harness: _Harness, field: str) -> None:
+ source = harness.source
+ value: str | bool
+ if field == "is_deleted":
+ value = True
+ elif field == "from_source":
+ value = ConversationFromSource.CONSOLE
+ elif field == "invoke_from":
+ value = InvokeFrom.SERVICE_API.value
+ else:
+ value = str(uuid4())
+ with source.factory.begin() as session:
+ session.execute(update(Conversation).where(Conversation.id == source.conversation.id).values({field: value}))
+ response = harness.get()
+ assert response.status_code == HTTPStatus.OK
+ assert response.get_json() == {"limit": 20, "has_more": False, "data": []}
+ cursor = harness.get({"last_id": source.conversation.id})
+ assert cursor.status_code == HTTPStatus.NOT_FOUND
+ assert cursor.get_json()["code"] == "conversation_cursor_not_found"
+ source.assert_closed()
+
+
[email protected]("pinned", [True, False])
+def test_pin_filter_is_scoped_to_app_actor_and_role(harness: _Harness, pinned: bool) -> None:
+ source = harness.source
+ with source.factory.begin() as session:
+ session.add(
+ PinnedConversation(
+ app_id=source.target.id,
+ conversation_id=source.conversation.id,
+ created_by_role=CreatorUserRole.END_USER,
+ created_by=source.end_user.id,
+ )
+ )
+ response = harness.get({"pinned": str(pinned).lower()})
+ assert response.status_code == HTTPStatus.OK
+ assert [item["id"] for item in response.get_json()["data"]] == ([source.conversation.id] if pinned else [])
+ source.assert_closed()
+
+
[email protected]("field", ["app_id", "created_by", "created_by_role"])
+def test_another_owner_pin_does_not_hide_the_unpinned_conversation(harness: _Harness, field: str) -> None:
+ source = harness.source
+ pin = PinnedConversation(
+ app_id=source.target.id,
+ conversation_id=source.conversation.id,
+ created_by_role=CreatorUserRole.END_USER,
+ created_by=source.end_user.id,
+ )
+ if field == "created_by_role":
+ pin.created_by_role = CreatorUserRole.ACCOUNT
+ elif field == "created_by":
+ pin.created_by = str(uuid4())
+ else:
+ pin.app_id = str(uuid4())
+ with source.factory.begin() as session:
+ session.add(pin)
+ response = harness.get({"pinned": "false"})
+ assert response.status_code == HTTPStatus.OK
+ assert [item["id"] for item in response.get_json()["data"]] == [source.conversation.id]
+ # Existing behavior: a requested pinned list with no pins is empty even
+ # when a cursor is supplied; it does not first try to resolve that cursor.
+ empty = harness.get({"pinned": "true", "last_id": str(uuid4())})
+ assert empty.status_code == HTTPStatus.OK
+ assert empty.get_json() == {"limit": 20, "has_more": False, "data": []}
+ source.assert_closed()
+
+
[email protected]("query", [{"limit": "0"}, {"limit": "101"}, {"last_id": "invalid"}, {"sort_by": "name"}])
+def test_invalid_query_preserves_400_before_querying(harness: _Harness, query: dict[str, str]) -> None:
+ response = harness.get(query)
+ assert response.status_code == HTTPStatus.BAD_REQUEST
+ assert response.get_json()["code"] == "invalid_param"
+ assert not harness.source.repository_sessions
+ harness.source.assert_closed()
+
+
[email protected]("mode", [AppMode.COMPLETION, AppMode.WORKFLOW])
+def test_non_chat_mode_is_rejected_before_querying(harness: _Harness, mode: AppMode) -> None:
+ source = harness.source
+ rename_tests._publish_mode(source, mode)
+ response = harness.get()
+ assert response.status_code == HTTPStatus.BAD_REQUEST
+ assert response.get_json()["code"] == "not_chat_app"
+ assert not source.repository_sessions
+ source.assert_closed()
+
+
[email protected]("field", ["tenant_id", "app_id"])
+def test_end_user_outside_app_owner_scope_is_rejected(harness: _Harness, field: str) -> None:
+ source = harness.source
+ with source.factory.begin() as session:
+ session.execute(update(EndUser).where(EndUser.id == source.end_user.id).values({field: str(uuid4())}))
+ response = harness.get()
+ assert response.status_code == HTTPStatus.NOT_FOUND
+ assert response.get_json()["code"] == "not_found"
+ source.assert_closed()
+
+
[email protected]("change_app_too", [False, True], ids=["end-user-workspace", "app-workspace"])
+def test_workspace_change_after_admission_returns_conversation_not_found(
+ harness: _Harness, sqlite_engine: Engine, change_app_too: bool
+) -> None:
+ source = harness.source
+ owner_changed = False
+
+ def change_owner(session: Session, _transaction: SessionTransaction) -> None:
+ nonlocal owner_changed
+ assert session in source.admission_sessions
+ assert not source.repository_sessions
+ # Change persisted ownership after passport resolution has closed its transaction.
+ new_tenant_id = str(uuid4())
+ with Session(sqlite_engine) as writer, writer.begin():
+ writer.execute(update(EndUser).where(EndUser.id == source.end_user.id).values(tenant_id=new_tenant_id))
+ if change_app_too:
+ writer.execute(update(App).where(App.id == source.target.id).values(tenant_id=new_tenant_id))
+ owner_changed = True
+
+ event.listen(source.factory, "after_transaction_end", change_owner, once=True)
+ try:
+ response = harness.get()
+ finally:
+ event.remove(source.factory, "after_transaction_end", change_owner)
+
+ assert owner_changed
+ assert source.repository_sessions
+ assert response.status_code == HTTPStatus.NOT_FOUND
+ assert response.get_json() == {
+ "code": "conversation_not_found",
+ "message": "The conversation was not found for this user and app.",
+ "status": HTTPStatus.NOT_FOUND,
+ }
+ assert harness.restored_inputs == []
+ source.assert_closed()
+
+
+def test_remote_file_input_keeps_its_existing_wire_shape(harness: _Harness) -> None:
+ source = harness.source
+ file = File(
+ file_id=str(uuid4()),
+ file_type=FileType.DOCUMENT,
+ transfer_method=FileTransferMethod.REMOTE_URL,
+ remote_url="https://example.com/document.pdf",
+ filename="document.pdf",
+ extension=".pdf",
+ mime_type="application/pdf",
+ size=12,
+ )
+ with source.factory.begin() as session:
+ conversation = session.get(Conversation, source.conversation.id)
+ assert conversation is not None
+ conversation.inputs = {"document": file, "documents": [file]}
+ response = harness.get()
+ assert response.status_code == HTTPStatus.OK
+ assert response.get_json()["data"][0]["inputs"] == {
+ "document": dict(file.to_dict()),
+ "documents": [dict(file.to_dict())],
+ }
+ source.assert_closed()
diff --git a/api/tests/unit_tests/controllers/console/explore/test_conversation.py b/api/tests/unit_tests/controllers/console/explore/test_conversation.py
index 948bcc984ec..1a1236ad04a 100644
--- a/api/tests/unit_tests/controllers/console/explore/test_conversation.py
+++ b/api/tests/unit_tests/controllers/console/explore/test_conversation.py
@@ -17,10 +17,12 @@ from core.app.entities.app_invoke_entities import InvokeFrom
from models.enums import ConversationFromSource, ConversationStatus, CreatorUserRole
from models.enums import InvokeFrom as ModelInvokeFrom
from models.model import App, AppMode, Conversation, Message
+from models.utils.file_input_compat import restore_input_files
from models.web import PinnedConversation
from repositories.conversation_repository import ConversationRepository
from services.conversation_command_service import ConversationCommandService
from services.conversation_name_service import ConversationNameService
+from services.conversation_query_service import ConversationQueryService
from services.installed_app_conversation_service import InstalledAppConversationService
from tests.unit_tests.controllers.console.explore.test_installed_app_admission import (
_assert_json_response,
@@ -119,7 +121,9 @@ def conversations(
services = _Services(
installed_apps=_InstalledAppServices(
conversations=InstalledAppConversationService(
- conversations=ConversationRepository(session_factory=factory),
+ queries=ConversationQueryService(
+ conversations=ConversationRepository(session_factory=factory), restore_inputs=restore_input_files
+ ),
names=ConversationNameService(
conversations=ConversationRepository(session_factory=factory), generate_name=state.generate_name
),
diff --git a/api/tests/unit_tests/controllers/service_api/app/test_conversation.py b/api/tests/unit_tests/controllers/service_api/app/test_conversation.py
index 08825d53d45..71ac5d074b9 100644
--- a/api/tests/unit_tests/controllers/service_api/app/test_conversation.py
+++ b/api/tests/unit_tests/controllers/service_api/app/test_conversation.py
@@ -1,21 +1,16 @@
-import sys
import uuid
from datetime import UTC, datetime
from inspect import unwrap
from types import SimpleNamespace
-from unittest.mock import Mock, patch
import pytest
from flask import Flask, request
from pydantic import ValidationError
-from sqlalchemy.engine import Engine
-from sqlalchemy.orm import Session
from werkzeug.exceptions import BadRequest, NotFound
import services
from controllers.console.explore.conversation import ConversationRenamePayload as ConsolePayload
from controllers.service_api.app.conversation import (
- ConversationApi,
ConversationListQuery,
ConversationRenamePayload,
ConversationVariableDetailApi,
@@ -489,11 +484,6 @@ class TestConversationCases:
class TestConversationService:
"""Test ConversationService integration patterns."""
- def test_pagination_by_last_id_method_exists(self):
- """Test that ConversationService.pagination_by_last_id exists."""
- assert hasattr(ConversationService, "pagination_by_last_id")
- assert callable(ConversationService.pagination_by_last_id)
-
def test_delete_method_exists(self):
"""Test that ConversationService.delete exists."""
assert hasattr(ConversationService, "delete")
@@ -509,28 +499,6 @@ class TestConversationCases:
assert hasattr(ConversationService, "update_conversation_variable")
assert callable(ConversationService.update_conversation_variable)
- @patch.object(ConversationService, "pagination_by_last_id")
- def test_pagination_returns_expected_format(self, mock_pagination):
- """Test pagination returns expected data format."""
- mock_result = Mock()
- mock_result.data = []
- mock_result.limit = 20
- mock_result.has_more = False
- mock_pagination.return_value = mock_result
-
- result = ConversationService.pagination_by_last_id(
- app_model=_app(),
- user=_end_user(),
- last_id=None,
- limit=20,
- invoke_from=Mock(),
- sort_by="-updated_at",
- )
-
- assert hasattr(result, "data")
- assert hasattr(result, "limit")
- assert hasattr(result, "has_more")
-
class TestConversationPayloadsController:
def test_rename_requires_name(self) -> None:
with pytest.raises(
@@ -546,54 +514,6 @@ class TestConversationCases:
with pytest.raises(ValueError, match="1 validation error for ConversationVariablesQuery\\\nvariable_name"):
ConversationVariablesQuery(variable_name="bad;")
- class TestConversationApiController:
- def test_list_not_chat(self, app: Flask) -> None:
- api = ConversationApi()
- handler = unwrap(api.get)
- app_model = _app(mode=AppMode.COMPLETION)
- end_user = _end_user()
-
- with app.test_request_context("/conversations", method="GET"):
- with pytest.raises(NotChatAppError):
- handler(
- api,
- ConversationListQuery.model_validate(request.args.to_dict(flat=True)),
- app_model=app_model,
- end_user=end_user,
- )
-
- def test_list_last_not_found(
- self,
- app: Flask,
- monkeypatch: pytest.MonkeyPatch,
- sqlite_engine: Engine,
- sqlite_session: Session,
- ) -> None:
- last_id = "00000000-0000-0000-0000-000000000001"
- # The id exists for a different app, proving pagination cannot cross app boundaries.
- sqlite_session.add(_conversation(conversation_id=last_id, app_id="other-app"))
- sqlite_session.commit()
-
- conversation_module = sys.modules["controllers.service_api.app.conversation"]
- monkeypatch.setattr(conversation_module, "db", SimpleNamespace(engine=sqlite_engine))
-
- api = ConversationApi()
- handler = unwrap(api.get)
- app_model = _app()
- end_user = _end_user()
-
- with app.test_request_context(
- f"/conversations?last_id={last_id}&limit=20",
- method="GET",
- ):
- with pytest.raises(NotFound):
- handler(
- api,
- ConversationListQuery.model_validate(request.args.to_dict(flat=True)),
- app_model=app_model,
- end_user=end_user,
- )
-
class TestConversationVariablesApiController:
def test_not_chat(self, app: Flask) -> None:
api = ConversationVariablesApi()
diff --git a/api/tests/unit_tests/models/utils/test_file_input_compat.py b/api/tests/unit_tests/models/utils/test_file_input_compat.py
index 6d6f2b42201..8fbd5e0d4c4 100644
--- a/api/tests/unit_tests/models/utils/test_file_input_compat.py
+++ b/api/tests/unit_tests/models/utils/test_file_input_compat.py
@@ -1,12 +1,15 @@
from unittest.mock import MagicMock
import pytest
+from pydantic import JsonValue
from graphon.file import File, FileTransferMethod, FileType
from models.utils.file_input_compat import (
build_file_from_mapping_without_lookup,
build_file_from_stored_mapping,
rebuild_serialized_graph_files_without_lookup,
+ resolve_file_mapping_tenant_id,
+ restore_input_files,
)
@@ -149,3 +152,106 @@ def test_build_file_from_stored_mapping_rebuilds_remote_urls_without_record_look
build_without_lookup.assert_called_once()
assert result is rebuilt_file
+
+
+def _unexpected_tenant_lookup() -> str:
+ raise AssertionError("These file inputs must not resolve the app tenant")
+
+
+def _remote_file_mapping() -> dict[str, JsonValue]:
+ return {
+ "dify_model_identity": "__dify__file__",
+ "type": "image",
+ "transfer_method": "remote_url",
+ "url": "https://example.com/image.png",
+ "filename": "image.png",
+ }
+
+
+def test_restore_input_files_rebuilds_single_file_and_file_list_without_tenant_lookup() -> None:
+ mapping = _remote_file_mapping()
+ inputs: dict[str, JsonValue | File | list[File]] = {"file": mapping, "files": [mapping]}
+
+ restored = restore_input_files(inputs=inputs, tenant_resolver=_unexpected_tenant_lookup)
+
+ file = restored["file"]
+ assert isinstance(file, File)
+ assert file.remote_url == "https://example.com/image.png"
+ files = restored["files"]
+ assert isinstance(files, list)
+ assert len(files) == 1
+ assert isinstance(files[0], File)
+ assert files[0].model_dump() == file.model_dump()
+ assert inputs == {"file": mapping, "files": [mapping]}
+ assert restored is not inputs
+
+
+def test_restore_input_files_preserves_mixed_lists_nested_mappings_and_scalar_values() -> None:
+ mapping = _remote_file_mapping()
+ inputs: dict[str, JsonValue | File | list[File]] = {
+ "mixed": [mapping, "ordinary-value"],
+ "nested": {"file": mapping},
+ "empty": [],
+ "null": None,
+ "zero": 0,
+ "false": False,
+ "string": "",
+ }
+
+ restored = restore_input_files(inputs=inputs, tenant_resolver=_unexpected_tenant_lookup)
+
+ assert restored == inputs
+ assert restored["mixed"] is inputs["mixed"]
+ assert restored["nested"] is inputs["nested"]
+
+
+def test_restore_input_files_preserves_existing_file_objects() -> None:
+ file = build_file_from_mapping_without_lookup(file_mapping=_remote_file_mapping())
+ files = [file]
+
+ restored = restore_input_files(
+ inputs={"file": file, "files": files},
+ tenant_resolver=_unexpected_tenant_lookup,
+ )
+
+ assert restored["file"] is file
+ assert restored["files"] is files
+
+
+def test_restore_input_files_propagates_missing_owner_for_storage_backed_files() -> None:
+ def resolve_missing_tenant() -> str:
+ raise ValueError("Unable to resolve tenant_id for app app-1")
+
+ with pytest.raises(ValueError, match="Unable to resolve tenant_id for app app-1"):
+ restore_input_files(
+ inputs={
+ "file": {
+ "dify_model_identity": "__dify__file__",
+ "type": "document",
+ "transfer_method": "local_file",
+ "related_id": "upload-file-id",
+ }
+ },
+ tenant_resolver=resolve_missing_tenant,
+ )
+
+
+def test_resolve_file_mapping_tenant_id_preserves_legacy_serialized_owner() -> None:
+ assert (
+ resolve_file_mapping_tenant_id(
+ file_mapping={"tenant_id": "serialized-tenant"},
+ tenant_resolver=_unexpected_tenant_lookup,
+ )
+ == "serialized-tenant"
+ )
+
+
[email protected]("tenant_id", [None, "", 42])
+def test_resolve_file_mapping_tenant_id_falls_back_for_missing_or_invalid_owner(tenant_id: JsonValue) -> None:
+ assert (
+ resolve_file_mapping_tenant_id(
+ file_mapping={"tenant_id": tenant_id},
+ tenant_resolver=lambda: "app-tenant",
+ )
+ == "app-tenant"
+ )
diff --git a/api/tests/unit_tests/repositories/test_conversation_repository.py b/api/tests/unit_tests/repositories/test_conversation_repository.py
index 80c4be6f6e5..150874d6c16 100644
--- a/api/tests/unit_tests/repositories/test_conversation_repository.py
+++ b/api/tests/unit_tests/repositories/test_conversation_repository.py
@@ -16,9 +16,13 @@ from models.model import App, AppMode, Conversation, EndUser, InstalledApp, Mess
from models.web import PinnedConversation
from repositories.conversation_repository import ConversationRepository
from services.entities.conversation_entities import (
+ ConversationListSource,
ConversationPage,
+ ConversationSort,
EndUserConversationRef,
+ EndUserConversationScope,
InstalledAppConversationRef,
+ InstalledAppConversationScope,
)
from services.errors.conversation import ConversationNotExistsError, LastConversationNotExistsError
from services.errors.message import MessageNotExistsError
@@ -116,14 +120,29 @@ def test_page_preserves_empty_result_and_pinned_empty_short_circuit(
) -> None:
repository = ConversationRepository(session_factory=sqlite_session_factory)
assert repository.get_page(
- installed_app=installation, account_id=_ACCOUNT_ID, last_id=None, limit=20, pinned=None
- ) == ConversationPage(limit=20, has_more=False, data=())
+ scope=InstalledAppConversationScope(installed_app=installation, account_id=_ACCOUNT_ID),
+ invoke_from="explore",
+ sort_by="-updated_at",
+ last_id=None,
+ limit=20,
+ pinned=None,
+ ) == ConversationPage(tenant_id=_OWNER_TENANT_ID, limit=20, has_more=False, data=())
assert repository.get_page(
- installed_app=installation, account_id=_ACCOUNT_ID, last_id=str(uuid4()), limit=20, pinned=True
- ) == ConversationPage(limit=20, has_more=False, data=())
+ scope=InstalledAppConversationScope(installed_app=installation, account_id=_ACCOUNT_ID),
+ invoke_from="explore",
+ sort_by="-updated_at",
+ last_id=str(uuid4()),
+ limit=20,
+ pinned=True,
+ ) == ConversationPage(tenant_id=_OWNER_TENANT_ID, limit=20, has_more=False, data=())
with pytest.raises(LastConversationNotExistsError):
repository.get_page(
- installed_app=installation, account_id=_ACCOUNT_ID, last_id=str(uuid4()), limit=20, pinned=None
+ scope=InstalledAppConversationScope(installed_app=installation, account_id=_ACCOUNT_ID),
+ invoke_from="explore",
+ sort_by="-updated_at",
+ last_id=str(uuid4()),
+ limit=20,
+ pinned=None,
)
@@ -137,10 +156,20 @@ def test_page_keeps_updated_order_cursor_and_detached_inputs(
repository = ConversationRepository(session_factory=sqlite_session_factory)
first_page = repository.get_page(
- installed_app=installation, account_id=_ACCOUNT_ID, last_id=None, limit=2, pinned=None
+ scope=InstalledAppConversationScope(installed_app=installation, account_id=_ACCOUNT_ID),
+ invoke_from="explore",
+ sort_by="-updated_at",
+ last_id=None,
+ limit=2,
+ pinned=None,
)
final_page = repository.get_page(
- installed_app=installation, account_id=_ACCOUNT_ID, last_id=middle.id, limit=1, pinned=None
+ scope=InstalledAppConversationScope(installed_app=installation, account_id=_ACCOUNT_ID),
+ invoke_from="explore",
+ sort_by="-updated_at",
+ last_id=middle.id,
+ limit=1,
+ pinned=None,
)
assert [item.id for item in first_page.data] == [newest.id, middle.id]
@@ -165,12 +194,24 @@ def test_page_keeps_existing_strict_timestamp_behavior_for_ties(
_conversation(session, installation)
_conversation(session, installation)
repository = ConversationRepository(session_factory=sqlite_session_factory)
- page = repository.get_page(installed_app=installation, account_id=_ACCOUNT_ID, last_id=None, limit=1, pinned=None)
+ page = repository.get_page(
+ scope=InstalledAppConversationScope(installed_app=installation, account_id=_ACCOUNT_ID),
+ invoke_from="explore",
+ sort_by="-updated_at",
+ last_id=None,
+ limit=1,
+ pinned=None,
+ )
assert len(page.data) == 1
assert page.has_more is False
assert (
repository.get_page(
- installed_app=installation, account_id=_ACCOUNT_ID, last_id=page.data[0].id, limit=1, pinned=None
+ scope=InstalledAppConversationScope(installed_app=installation, account_id=_ACCOUNT_ID),
+ invoke_from="explore",
+ sort_by="-updated_at",
+ last_id=page.data[0].id,
+ limit=1,
+ pinned=None,
).data
== ()
)
@@ -191,14 +232,24 @@ def test_page_filters_invoke_source_and_scopes_pins_to_account_role_and_owner(
for pinned, expected in [(None, [legacy.id, explore.id]), (True, [legacy.id]), (False, [explore.id])]:
page = repository.get_page(
- installed_app=installation, account_id=_ACCOUNT_ID, last_id=None, limit=20, pinned=pinned
+ scope=InstalledAppConversationScope(installed_app=installation, account_id=_ACCOUNT_ID),
+ invoke_from="explore",
+ sort_by="-updated_at",
+ last_id=None,
+ limit=20,
+ pinned=pinned,
)
assert [item.id for item in page.data] == expected
assert page.has_more is False
for cursor, pinned in [(debugging.id, None), (explore.id, True), (legacy.id, False)]:
with pytest.raises(LastConversationNotExistsError):
repository.get_page(
- installed_app=installation, account_id=_ACCOUNT_ID, last_id=cursor, limit=20, pinned=pinned
+ scope=InstalledAppConversationScope(installed_app=installation, account_id=_ACCOUNT_ID),
+ invoke_from="explore",
+ sort_by="-updated_at",
+ last_id=cursor,
+ limit=20,
+ pinned=pinned,
)
@@ -217,13 +268,23 @@ def test_every_conversation_ownership_predicate_protects_reads_and_mutations(
assert (
repository.get_page(
- installed_app=installation, account_id=_ACCOUNT_ID, last_id=None, limit=20, pinned=None
+ scope=InstalledAppConversationScope(installed_app=installation, account_id=_ACCOUNT_ID),
+ invoke_from="explore",
+ sort_by="-updated_at",
+ last_id=None,
+ limit=20,
+ pinned=None,
).data
== ()
)
with pytest.raises(LastConversationNotExistsError):
repository.get_page(
- installed_app=installation, account_id=_ACCOUNT_ID, last_id=conversation_id, limit=20, pinned=None
+ scope=InstalledAppConversationScope(installed_app=installation, account_id=_ACCOUNT_ID),
+ invoke_from="explore",
+ sort_by="-updated_at",
+ last_id=conversation_id,
+ limit=20,
+ pinned=None,
)
with pytest.raises(ConversationNotExistsError):
repository.get(
@@ -285,7 +346,14 @@ def test_all_operations_revalidate_complete_installation_and_actual_app(
repository = ConversationRepository(session_factory=sqlite_session_factory)
with pytest.raises(InstalledAppNotFoundError):
- repository.get_page(installed_app=installation, account_id=_ACCOUNT_ID, last_id=None, limit=20, pinned=True)
+ repository.get_page(
+ scope=InstalledAppConversationScope(installed_app=installation, account_id=_ACCOUNT_ID),
+ invoke_from="explore",
+ sort_by="-updated_at",
+ last_id=None,
+ limit=20,
+ pinned=True,
+ )
with pytest.raises(InstalledAppNotFoundError):
repository.get_name_source(
ref=InstalledAppConversationRef(
@@ -620,3 +688,237 @@ def test_end_user_and_installed_account_refs_never_cross_owner_domains(
repository.set_pinned(ref=foreign_ref, is_pinned=True)
assert repository.get(ref=account_ref).name == "Original name"
assert repository.get(ref=end_user_ref).name == "End-user conversation"
+
+
+def end_user_scope(
+ sqlite_session_factory: sessionmaker[Session], installation: InstalledAppRef
+) -> EndUserConversationScope:
+ with sqlite_session_factory.begin() as session:
+ user = EndUser(
+ id=_ACCOUNT_ID,
+ tenant_id=installation.app_owner_tenant_id,
+ app_id=installation.app_id,
+ type=EndUserType.BROWSER,
+ session_id=str(uuid4()),
+ )
+ session.add(user)
+ session.flush()
+ assert user.app_id is not None
+ return EndUserConversationScope(app_id=user.app_id, tenant_id=user.tenant_id, end_user_id=user.id)
+
+
+def _end_user_conversation(
+ session: Session,
+ scope: EndUserConversationScope,
+ *,
+ created_at: datetime = _TIME,
+ updated_at: datetime = _TIME,
+ invoke_from: InvokeFrom | None = InvokeFrom.WEB_APP,
+) -> Conversation:
+ conversation = Conversation(
+ app_id=scope.app_id,
+ mode=AppMode.CHAT,
+ name="End-user conversation",
+ _inputs={"empty": "", "flag": False, "zero": 0, "nested": {"list": [None, 0]}},
+ from_source=ConversationFromSource.API,
+ from_account_id=None,
+ from_end_user_id=scope.end_user_id,
+ invoke_from=ModelInvokeFrom(invoke_from) if invoke_from is not None else None,
+ is_deleted=False,
+ )
+ conversation.created_at = created_at
+ conversation.updated_at = updated_at
+ session.add(conversation)
+ session.flush()
+ return conversation
+
+
[email protected]("sort_by", ["created_at", "-created_at", "updated_at", "-updated_at"])
+def test_end_user_page_orders_and_advances_using_selected_timestamp(
+ sqlite_session_factory: sessionmaker[Session], end_user_scope: EndUserConversationScope, sort_by: ConversationSort
+) -> None:
+ with sqlite_session_factory.begin() as session:
+ first_created = _end_user_conversation(
+ session, end_user_scope, created_at=_TIME - timedelta(hours=1), updated_at=_TIME + timedelta(hours=1)
+ )
+ second_created = _end_user_conversation(
+ session, end_user_scope, created_at=_TIME, updated_at=_TIME - timedelta(hours=1)
+ )
+ last_created = _end_user_conversation(
+ session, end_user_scope, created_at=_TIME + timedelta(hours=1), updated_at=_TIME
+ )
+ chronological_ids = (
+ [first_created.id, second_created.id, last_created.id]
+ if sort_by.endswith("created_at")
+ else [second_created.id, last_created.id, first_created.id]
+ )
+ expected = list(reversed(chronological_ids)) if sort_by.startswith("-") else chronological_ids
+ repository = ConversationRepository(session_factory=sqlite_session_factory)
+ first_page = repository.get_page(
+ scope=end_user_scope, invoke_from="web-app", last_id=None, limit=2, pinned=None, sort_by=sort_by
+ )
+ second_page = repository.get_page(
+ scope=end_user_scope, invoke_from="web-app", last_id=expected[1], limit=2, pinned=None, sort_by=sort_by
+ )
+ assert first_page.tenant_id == end_user_scope.tenant_id
+ assert [record.id for record in first_page.data] == expected[:2]
+ assert first_page.has_more is True
+ assert [record.id for record in second_page.data] == expected[2:]
+ assert second_page.has_more is False
+
+
[email protected]("sort_by", ["created_at", "-created_at", "updated_at", "-updated_at"])
+def test_end_user_page_preserves_strict_timestamp_cursor_for_ties(
+ sqlite_session_factory: sessionmaker[Session], end_user_scope: EndUserConversationScope, sort_by: ConversationSort
+) -> None:
+ with sqlite_session_factory.begin() as session:
+ _end_user_conversation(session, end_user_scope)
+ _end_user_conversation(session, end_user_scope)
+ repository = ConversationRepository(session_factory=sqlite_session_factory)
+ page = repository.get_page(
+ scope=end_user_scope, invoke_from="web-app", last_id=None, limit=1, pinned=None, sort_by=sort_by
+ )
+ assert len(page.data) == 1
+ assert page.has_more is False
+ following_page = repository.get_page(
+ scope=end_user_scope,
+ invoke_from="web-app",
+ last_id=page.data[0].id,
+ limit=1,
+ pinned=None,
+ sort_by=sort_by,
+ )
+ assert following_page.data == ()
+ assert following_page.has_more is False
+
+
[email protected]("invoke_from", ["web-app", "service-api"])
+def test_end_user_page_filters_invocation_and_scopes_pins_to_app_actor_and_role(
+ sqlite_session_factory: sessionmaker[Session],
+ installation: InstalledAppRef,
+ end_user_scope: EndUserConversationScope,
+ invoke_from: ConversationListSource,
+) -> None:
+ with sqlite_session_factory.begin() as session:
+ legacy = _end_user_conversation(
+ session, end_user_scope, invoke_from=None, updated_at=_TIME + timedelta(hours=1)
+ )
+ matching = _end_user_conversation(session, end_user_scope, invoke_from=InvokeFrom(invoke_from))
+ different_source = _end_user_conversation(
+ session,
+ end_user_scope,
+ invoke_from=InvokeFrom.SERVICE_API if invoke_from == "web-app" else InvokeFrom.WEB_APP,
+ )
+ _conversation(session, installation, invoke_from=InvokeFrom(invoke_from))
+ _pin(session, installation, legacy.id, role=CreatorUserRole.END_USER)
+ _pin(session, installation, matching.id, role=CreatorUserRole.ACCOUNT)
+ _pin(session, installation, matching.id, role=CreatorUserRole.END_USER, account_id=str(uuid4()))
+ _pin(session, replace(installation, app_id=str(uuid4())), matching.id, role=CreatorUserRole.END_USER)
+ repository = ConversationRepository(session_factory=sqlite_session_factory)
+ for pinned, expected in [(None, [legacy.id, matching.id]), (True, [legacy.id]), (False, [matching.id])]:
+ page = repository.get_page(
+ scope=end_user_scope, invoke_from=invoke_from, last_id=None, limit=20, pinned=pinned, sort_by="-updated_at"
+ )
+ assert [record.id for record in page.data] == expected
+ for cursor, pinned in [(different_source.id, None), (matching.id, True), (legacy.id, False)]:
+ with pytest.raises(LastConversationNotExistsError):
+ repository.get_page(
+ scope=end_user_scope,
+ invoke_from=invoke_from,
+ last_id=cursor,
+ limit=20,
+ pinned=pinned,
+ sort_by="-updated_at",
+ )
+
+
[email protected]("mismatch", ["app_id", "from_end_user_id", "from_source", "from_account_id", "is_deleted"])
+def test_end_user_page_and_cursor_reject_every_conversation_owner_mismatch(
+ sqlite_session_factory: sessionmaker[Session], end_user_scope: EndUserConversationScope, mismatch: str
+) -> None:
+ with sqlite_session_factory.begin() as session:
+ matching = _end_user_conversation(session, end_user_scope)
+ unrelated = _end_user_conversation(session, end_user_scope)
+ value: str | bool = True if mismatch == "is_deleted" else str(uuid4())
+ if mismatch == "from_source":
+ value = ConversationFromSource.CONSOLE
+ session.execute(update(Conversation).where(Conversation.id == unrelated.id).values({mismatch: value}))
+ repository = ConversationRepository(session_factory=sqlite_session_factory)
+ page = repository.get_page(
+ scope=end_user_scope, invoke_from="web-app", last_id=None, limit=20, pinned=None, sort_by="-updated_at"
+ )
+ assert [record.id for record in page.data] == [matching.id]
+ with pytest.raises(LastConversationNotExistsError):
+ repository.get_page(
+ scope=end_user_scope,
+ invoke_from="web-app",
+ last_id=unrelated.id,
+ limit=20,
+ pinned=None,
+ sort_by="-updated_at",
+ )
+
+
+ "mismatch", ["app_id", "tenant_id", "end_user_id", "user_app", "user_tenant", "missing_user", "missing_app"]
+)
+def test_end_user_page_revalidates_complete_app_and_user_owner_chain(
+ sqlite_session_factory: sessionmaker[Session], end_user_scope: EndUserConversationScope, mismatch: str
+) -> None:
+ with sqlite_session_factory.begin() as session:
+ _end_user_conversation(session, end_user_scope)
+ if mismatch == "missing_app":
+ app = session.get(App, end_user_scope.app_id)
+ assert app is not None
+ session.delete(app)
+ elif mismatch.startswith("user_") or mismatch == "missing_user":
+ user = session.get(EndUser, end_user_scope.end_user_id)
+ assert user is not None
+ if mismatch == "missing_user":
+ session.delete(user)
+ elif mismatch == "user_app":
+ user.app_id = str(uuid4())
+ else:
+ user.tenant_id = str(uuid4())
+ else:
+ end_user_scope = replace(end_user_scope, **{mismatch: str(uuid4())})
+ repository = ConversationRepository(session_factory=sqlite_session_factory)
+ with pytest.raises(ConversationNotExistsError):
+ repository.get_page(
+ scope=end_user_scope,
+ invoke_from="web-app",
+ last_id=None,
+ limit=20,
+ pinned=True,
+ sort_by="-updated_at",
+ )
+
+
+def test_end_user_empty_page_keeps_missing_cursor_and_empty_pin_semantics(
+ sqlite_session_factory: sessionmaker[Session], end_user_scope: EndUserConversationScope
+) -> None:
+ repository = ConversationRepository(session_factory=sqlite_session_factory)
+ assert repository.get_page(
+ scope=end_user_scope, invoke_from="web-app", last_id=None, limit=20, pinned=None, sort_by="-updated_at"
+ ) == ConversationPage(tenant_id=end_user_scope.tenant_id, limit=20, has_more=False, data=())
+ assert (
+ repository.get_page(
+ scope=end_user_scope,
+ invoke_from="web-app",
+ last_id=str(uuid4()),
+ limit=20,
+ pinned=True,
+ sort_by="-updated_at",
+ ).data
+ == ()
+ )
+ with pytest.raises(LastConversationNotExistsError):
+ repository.get_page(
+ scope=end_user_scope,
+ invoke_from="web-app",
+ last_id=str(uuid4()),
+ limit=20,
+ pinned=False,
+ sort_by="-updated_at",
+ )
diff --git a/api/tests/unit_tests/services/test_conversation_query_service.py b/api/tests/unit_tests/services/test_conversation_query_service.py
new file mode 100644
index 00000000000..6b97739774c
--- /dev/null
+++ b/api/tests/unit_tests/services/test_conversation_query_service.py
@@ -0,0 +1,244 @@
+"""Conversation listing materializes data before restoring file inputs."""
+
+from collections.abc import Callable, Mapping
+from copy import deepcopy
+from dataclasses import dataclass
+from uuid import uuid4
+
+import pytest
+from pydantic import JsonValue
+from sqlalchemy import Engine, event, select
+from sqlalchemy.orm import Session, sessionmaker
+
+from graphon.file import FILE_MODEL_IDENTITY, File
+from models.enums import ConversationFromSource, EndUserType, InvokeFrom
+from models.model import App, AppMode, Conversation, EndUser, InstalledApp
+from models.utils.file_input_compat import restore_input_files
+from repositories.conversation_repository import ConversationRepository
+from services.conversation_query_service import ConversationQueryService
+from services.entities.conversation_entities import (
+ ConversationInputValue,
+ ConversationListSource,
+ ConversationScope,
+ EndUserConversationScope,
+ InstalledAppConversationScope,
+)
+from services.installed_app_access_service import InstalledAppRef
+
+
+@dataclass(frozen=True)
+class QueryContext:
+ scope: ConversationScope
+ tenant_id: str
+ conversation_id: str
+ invoke_from: ConversationListSource
+
+
[email protected](params=["installed", "end_user"])
+def query_context(request: pytest.FixtureRequest, sqlite_session_factory: sessionmaker[Session]) -> QueryContext:
+ with sqlite_session_factory.begin() as session:
+ app = App(tenant_id=str(uuid4()), name="Chat", mode=AppMode.CHAT, enable_site=True, enable_api=True)
+ session.add(app)
+ session.flush()
+ account_id = str(uuid4())
+ if request.param == "installed":
+ installation = InstalledApp(
+ tenant_id=str(uuid4()), app_id=app.id, app_owner_tenant_id=app.tenant_id, position=0, is_pinned=False
+ )
+ session.add(installation)
+ session.flush()
+ scope: ConversationScope = InstalledAppConversationScope(
+ installed_app=InstalledAppRef(
+ id=installation.id,
+ tenant_id=installation.tenant_id,
+ app_id=app.id,
+ app_owner_tenant_id=app.tenant_id,
+ app_mode=app.mode.value,
+ ),
+ account_id=account_id,
+ )
+ invoke_from: ConversationListSource = "explore"
+ conversation = Conversation(
+ app_id=app.id,
+ mode=AppMode.CHAT,
+ name="Original",
+ _inputs={},
+ from_source=ConversationFromSource.CONSOLE,
+ from_account_id=account_id,
+ from_end_user_id=None,
+ invoke_from=InvokeFrom(invoke_from),
+ is_deleted=False,
+ )
+ else:
+ user = EndUser(tenant_id=app.tenant_id, app_id=app.id, type=EndUserType.BROWSER, session_id=str(uuid4()))
+ session.add(user)
+ session.flush()
+ scope = EndUserConversationScope(app_id=app.id, tenant_id=app.tenant_id, end_user_id=user.id)
+ invoke_from = "web-app"
+ conversation = Conversation(
+ app_id=app.id,
+ mode=AppMode.CHAT,
+ name="Original",
+ _inputs={},
+ from_source=ConversationFromSource.API,
+ from_account_id=None,
+ from_end_user_id=user.id,
+ invoke_from=InvokeFrom(invoke_from),
+ is_deleted=False,
+ )
+ session.add(conversation)
+ session.flush()
+ return QueryContext(
+ scope=scope, tenant_id=app.tenant_id, conversation_id=conversation.id, invoke_from=invoke_from
+ )
+
+
+def _record_connections(engine: Engine) -> list[str]:
+ events: list[str] = []
+
+ @event.listens_for(engine, "checkout")
+ def checkout(_connection: object, _record: object, _proxy: object) -> None:
+ events.append("checkout")
+
+ @event.listens_for(engine, "checkin")
+ def checkin(_connection: object, _record: object) -> None:
+ events.append("checkin")
+
+ return events
+
+
+def test_query_restores_real_file_inputs_after_returning_database_connection(
+ sqlite_session_factory: sessionmaker[Session], sqlite_engine: Engine, query_context: QueryContext
+) -> None:
+ file_mapping: dict[str, JsonValue] = {
+ "dify_model_identity": FILE_MODEL_IDENTITY,
+ "transfer_method": "remote_url",
+ "type": "document",
+ "url": "https://example.com/report.pdf",
+ "filename": "report.pdf",
+ "extension": ".pdf",
+ "mime_type": "application/pdf",
+ "size": 1,
+ }
+ inputs: dict[str, ConversationInputValue] = {
+ "file": file_mapping,
+ "files": [file_mapping],
+ "mixed": [file_mapping, "unchanged"],
+ "nested": {"file": file_mapping},
+ "empty": [],
+ "zero": 0,
+ "flag": False,
+ "none": None,
+ }
+ with sqlite_session_factory.begin() as session:
+ conversation = session.get(Conversation, query_context.conversation_id)
+ assert conversation is not None
+ conversation.inputs = deepcopy(inputs)
+ connection_events = _record_connections(sqlite_engine)
+
+ def restore(
+ *, inputs: Mapping[str, ConversationInputValue], tenant_resolver: Callable[[], str]
+ ) -> dict[str, ConversationInputValue]:
+ assert connection_events == ["checkout", "checkin"]
+ assert tenant_resolver() == query_context.tenant_id
+ connection_events.append("restore")
+ return restore_input_files(inputs=inputs, tenant_resolver=tenant_resolver)
+
+ service = ConversationQueryService(
+ conversations=ConversationRepository(session_factory=sqlite_session_factory), restore_inputs=restore
+ )
+ result = service.get_page(
+ scope=query_context.scope,
+ invoke_from=query_context.invoke_from,
+ last_id=None,
+ limit=20,
+ pinned=None,
+ sort_by="-updated_at",
+ )
+ assert connection_events == ["checkout", "checkin", "restore"]
+ assert result.tenant_id == query_context.tenant_id
+ assert len(result.data) == 1
+ restored = result.data[0]
+ assert restored.id == query_context.conversation_id
+ assert restored.name == "Original"
+ assert restored.status == "normal"
+ file = restored.inputs["file"]
+ assert isinstance(file, File)
+ assert file.remote_url == "https://example.com/report.pdf"
+ assert restored.inputs["files"] == [file]
+ assert restored.inputs["mixed"] == inputs["mixed"]
+ assert restored.inputs["nested"] == inputs["nested"]
+ assert restored.inputs["empty"] == []
+ assert restored.inputs["zero"] == 0
+ assert restored.inputs["flag"] is False
+ assert restored.inputs["none"] is None
+ with sqlite_session_factory() as session:
+ stored = session.get(Conversation, query_context.conversation_id)
+ assert stored is not None
+ assert stored._inputs == inputs
+
+
+def test_repository_returns_detached_raw_file_inputs_without_restoring_or_committing(
+ sqlite_session_factory: sessionmaker[Session], sqlite_engine: Engine, query_context: QueryContext
+) -> None:
+ # Invalid file metadata would fail if the repository accidentally restored it.
+ inputs: dict[str, ConversationInputValue] = {
+ "file": {"dify_model_identity": FILE_MODEL_IDENTITY, "transfer_method": "invalid"},
+ "nested": {"values": [0, None]},
+ }
+ with sqlite_session_factory.begin() as session:
+ conversation = session.get(Conversation, query_context.conversation_id)
+ assert conversation is not None
+ conversation.inputs = inputs
+ connection_events = _record_connections(sqlite_engine)
+ commits: list[bool] = []
+
+ @event.listens_for(sqlite_session_factory, "after_commit")
+ def committed(_session: Session) -> None:
+ commits.append(True)
+
+ result = ConversationRepository(session_factory=sqlite_session_factory).get_page(
+ scope=query_context.scope,
+ invoke_from=query_context.invoke_from,
+ last_id=None,
+ limit=20,
+ pinned=None,
+ sort_by="-updated_at",
+ )
+ assert connection_events == ["checkout", "checkin"]
+ assert commits == []
+ assert result.data[0].inputs == inputs
+ nested = result.data[0].inputs["nested"]
+ assert isinstance(nested, dict)
+ nested["values"] = ["changed"]
+ with sqlite_session_factory() as session:
+ stored = session.get(Conversation, query_context.conversation_id)
+ assert stored is not None
+ assert stored._inputs == inputs
+
+
+def test_invalid_file_input_error_propagates_after_repository_connection_is_closed(
+ sqlite_session_factory: sessionmaker[Session], sqlite_engine: Engine, query_context: QueryContext
+) -> None:
+ with sqlite_session_factory.begin() as session:
+ conversation = session.get(Conversation, query_context.conversation_id)
+ assert conversation is not None
+ conversation.inputs = {"file": {"dify_model_identity": FILE_MODEL_IDENTITY, "transfer_method": "invalid"}}
+ connection_events = _record_connections(sqlite_engine)
+ service = ConversationQueryService(
+ conversations=ConversationRepository(session_factory=sqlite_session_factory), restore_inputs=restore_input_files
+ )
+ with pytest.raises(ValueError, match="'invalid' is not a valid FileTransferMethod"):
+ service.get_page(
+ scope=query_context.scope,
+ invoke_from=query_context.invoke_from,
+ last_id=None,
+ limit=20,
+ pinned=None,
+ sort_by="-updated_at",
+ )
+ assert connection_events == ["checkout", "checkin"]
+ with sqlite_session_factory() as session:
+ assert (
+ session.scalar(select(Conversation.id).where(Conversation.id == query_context.conversation_id)) is not None
+ )
diff --git a/api/tests/unit_tests/services/test_conversation_service.py b/api/tests/unit_tests/services/test_conversation_service.py
index 197fa6b83a3..4ff1941450b 100644
--- a/api/tests/unit_tests/services/test_conversation_service.py
+++ b/api/tests/unit_tests/services/test_conversation_service.py
@@ -14,10 +14,9 @@ from unittest.mock import MagicMock
from uuid import UUID
import pytest
-from sqlalchemy import asc, desc, event
+from sqlalchemy import event
from sqlalchemy.orm import Session
-from core.app.entities.app_invoke_entities import InvokeFrom
from factories import variable_factory
from libs.datetime_utils import naive_utc_now
from models import Account, ConversationVariable
@@ -393,134 +392,6 @@ def test_cleanup_propagates_agent_enqueue_failure_before_conversation_enqueue(mo
delete_related.assert_not_called()
-class TestConversationServicePagination:
- """Test conversation pagination operations."""
-
- def test_pagination_with_empty_include_ids(self, sqlite_session: Session):
- """
- Test that empty include_ids returns empty result.
-
- When include_ids is an empty list, the service should short-circuit
- and return empty results without querying the database.
- """
- # Arrange - Set up test data
- app_model = ConversationServiceTestDataFactory.create_app()
- user = ConversationServiceTestDataFactory.create_account()
-
- # Act - Call the service method with empty include_ids
- result = ConversationService.pagination_by_last_id(
- session=sqlite_session,
- app_model=app_model,
- user=user,
- last_id=None,
- limit=20,
- invoke_from=InvokeFrom.WEB_APP,
- include_ids=[], # Empty list should trigger early return
- exclude_ids=None,
- )
-
- # Assert - Verify empty result without database query
- assert result.data == [] # No conversations returned
- assert result.has_more is False # No more pages available
- assert result.limit == 20 # Limit preserved in response
- assert not sqlite_session.in_transaction()
-
- def test_pagination_returns_empty_when_user_is_none(self, sqlite_session: Session):
- """
- Test that pagination returns empty result when user is None.
-
- This ensures proper handling of unauthenticated requests.
- """
- # Arrange
- app_model = ConversationServiceTestDataFactory.create_app()
-
- # Act
- result = ConversationService.pagination_by_last_id(
- session=sqlite_session,
- app_model=app_model,
- user=None, # No user provided
- last_id=None,
- limit=20,
- invoke_from=InvokeFrom.WEB_APP,
- )
-
- # Assert - should return empty result without querying database
- assert result.data == []
- assert result.has_more is False
- assert result.limit == 20
- assert not sqlite_session.in_transaction()
-
-
-class TestConversationServiceHelpers:
- """Test helper methods in ConversationService."""
-
- def test_get_sort_params_with_descending_sort(self):
- """
- Test _get_sort_params with descending sort prefix.
-
- When sort_by starts with '-', should return field name and desc function.
- """
- # Act
- field, direction = ConversationService._get_sort_params("-updated_at")
-
- # Assert
- assert field == "updated_at"
- assert direction == desc
-
- def test_get_sort_params_with_ascending_sort(self):
- """
- Test _get_sort_params with ascending sort.
-
- When sort_by doesn't start with '-', should return field name and asc function.
- """
- # Act
- field, direction = ConversationService._get_sort_params("created_at")
-
- # Assert
- assert field == "created_at"
- assert direction == asc
-
- def test_build_filter_condition_with_descending_sort(self):
- """
- Test _build_filter_condition with descending sort direction.
-
- Should create a less-than filter condition.
- """
- # Arrange
- conversation = ConversationServiceTestDataFactory.create_conversation()
-
- # Act
- condition = ConversationService._build_filter_condition(
- sort_field="updated_at",
- sort_direction=desc,
- reference_conversation=conversation,
- )
-
- # Assert
- # The condition should be a comparison expression
- assert condition is not None
-
- def test_build_filter_condition_with_ascending_sort(self):
- """
- Test _build_filter_condition with ascending sort direction.
-
- Should create a greater-than filter condition.
- """
- # Arrange
- conversation = ConversationServiceTestDataFactory.create_conversation()
-
- # Act
- condition = ConversationService._build_filter_condition(
- sort_field="created_at",
- sort_direction=asc,
- reference_conversation=conversation,
- )
-
- # Assert
- # The condition should be a comparison expression
- assert condition is not None
-
-
class TestConversationServiceConversationalVariable:
"""Test conversational variable operations."""
diff --git a/api/tests/unit_tests/services/test_installed_app_conversation_service.py b/api/tests/unit_tests/services/test_installed_app_conversation_service.py
index f5010633dad..e6a3faadeaf 100644
--- a/api/tests/unit_tests/services/test_installed_app_conversation_service.py
+++ b/api/tests/unit_tests/services/test_installed_app_conversation_service.py
@@ -9,8 +9,9 @@ import pytest
from sqlalchemy import Engine, delete, event, select, update
from sqlalchemy.orm import Session, sessionmaker
-from models.enums import ConversationFromSource
+from models.enums import ConversationFromSource, InvokeFrom
from models.model import App, AppMode, Conversation, InstalledApp, Message
+from models.utils.file_input_compat import restore_input_files
from repositories.conversation_repository import ConversationRepository
from services.conversation_command_service import ConversationCleanup, ConversationCommandService
from services.conversation_name_service import (
@@ -18,6 +19,7 @@ from services.conversation_name_service import (
ConversationNameRequiredError,
ConversationNameService,
)
+from services.conversation_query_service import ConversationQueryService
from services.entities.conversation_entities import InstalledAppConversationRef
from services.errors.conversation import ConversationNotExistsError
from services.installed_app_access_service import InstalledAppNotFoundError, InstalledAppRef
@@ -95,7 +97,7 @@ def _service(
) -> InstalledAppConversationService:
repository = ConversationRepository(session_factory=factory)
return InstalledAppConversationService(
- conversations=repository,
+ queries=ConversationQueryService(conversations=repository, restore_inputs=restore_input_files),
names=ConversationNameService(conversations=repository, generate_name=generate_name),
commands=ConversationCommandService(conversations=repository, enqueue_delete_cleanup=cleanup),
)
@@ -287,3 +289,36 @@ def test_non_chat_mode_is_rejected_before_rename_or_delete(
service.delete(installed_app=installed_app, account_id=ref.account_id, conversation_id=ref.conversation_id)
stored = ConversationRepository(session_factory=sqlite_session_factory).get(ref=ref)
assert stored.name == "Original"
+
+
+def test_list_uses_shared_query_and_explore_scope(
+ sqlite_session_factory: sessionmaker[Session], ref: InstalledAppConversationRef
+) -> None:
+ with sqlite_session_factory.begin() as session:
+ session.add(
+ Conversation(
+ app_id=ref.app_id,
+ mode=AppMode.CHAT,
+ name="Other invocation",
+ _inputs={},
+ from_source=ConversationFromSource.CONSOLE,
+ from_account_id=ref.account_id,
+ from_end_user_id=None,
+ invoke_from=InvokeFrom.DEBUGGER,
+ is_deleted=False,
+ )
+ )
+ service = _service(sqlite_session_factory, generate_name=_unexpected_generation, cleanup=_unexpected_cleanup)
+ page = service.get_page(
+ installed_app=ref.installed_app, account_id=ref.account_id, last_id=None, limit=20, pinned=None
+ )
+ assert [record.id for record in page.data] == [ref.conversation_id]
+ assert page.data[0].inputs == {"count": 0}
+ assert page.tenant_id == ref.installed_app.app_owner_tenant_id
+ assert page.has_more is False
+ assert (
+ service.get_page(
+ installed_app=ref.installed_app, account_id=ref.account_id, last_id=None, limit=20, pinned=True
+ ).data
+ == ()
+ )
diff --git a/packages/contracts/generated/api/service/errors.gen.ts b/packages/contracts/generated/api/service/errors.gen.ts
index 6a63831bf55..984fe8ecf8f 100644
--- a/packages/contracts/generated/api/service/errors.gen.ts
+++ b/packages/contracts/generated/api/service/errors.gen.ts
@@ -90,6 +90,14 @@ const zCompletionRequestError = /* @__PURE__ */ (() =>
}),
))()
+const zConversationCursorNotFoundError = /* @__PURE__ */ (() =>
+ zErrorResponse.and(
+ z.object({
+ code: z.literal('conversation_cursor_not_found'),
+ status: z.literal(404),
+ }),
+ ))()
+
const zConversationFirstMessageNotFoundError = /* @__PURE__ */ (() =>
zErrorResponse.and(
z.object({
@@ -576,7 +584,17 @@ export const delete2Errors = {
workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },
}
-export const get6Errors = { not_chat_app: { status: 400, data: zNotChatAppError } }
+export const get6Errors = {
+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },
+ app_api_disabled: { status: 403, data: zAppApiDisabledError },
+ app_not_found: { status: 403, data: zAppNotFoundError },
+ conversation_cursor_not_found: { status: 404, data: zConversationCursorNotFoundError },
+ conversation_not_found: { status: 404, data: zConversationNotFoundError },
+ forbidden: { status: 403, data: zForbiddenError },
+ not_chat_app: { status: 400, data: zNotChatAppError },
+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },
+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },
+}
export const post9Errors = {
file_too_large: { status: 413, data: zFileTooLargeError },
diff --git a/packages/contracts/generated/api/service/orpc.gen.ts b/packages/contracts/generated/api/service/orpc.gen.ts
index aaf85135abb..9b1233490d6 100644
--- a/packages/contracts/generated/api/service/orpc.gen.ts
+++ b/packages/contracts/generated/api/service/orpc.gen.ts
@@ -13,6 +13,7 @@ import {
zArchivedDocumentImmutableError,
zAudioTooLargeError,
zCompletionRequestError,
+ zConversationCursorNotFoundError,
zConversationFirstMessageNotFoundError,
zConversationNotFoundError,
zDatasetNameDuplicateError,
@@ -1051,7 +1052,17 @@ export const get6 = oc
})
.input(z.object({ query: zGetConversationsQuery.optional() }).optional())
.output(zGetConversationsResponse)
- .errors({ not_chat_app: { status: 400, data: zNotChatAppError } })
+ .errors({
+ app_abnormal_status: { status: 403, data: zAppAbnormalStatusError },
+ app_api_disabled: { status: 403, data: zAppApiDisabledError },
+ app_not_found: { status: 403, data: zAppNotFoundError },
+ conversation_cursor_not_found: { status: 404, data: zConversationCursorNotFoundError },
+ conversation_not_found: { status: 404, data: zConversationNotFoundError },
+ forbidden: { status: 403, data: zForbiddenError },
+ not_chat_app: { status: 400, data: zNotChatAppError },
+ workspace_archived: { status: 403, data: zWorkspaceArchivedError },
+ workspace_not_found: { status: 403, data: zWorkspaceNotFoundError },
+ })
export const conversations = {
get: get6,
diff --git a/packages/contracts/generated/api/service/types.gen.ts b/packages/contracts/generated/api/service/types.gen.ts
index d375781fdc9..e4ab8b59d61 100644
--- a/packages/contracts/generated/api/service/types.gen.ts
+++ b/packages/contracts/generated/api/service/types.gen.ts
@@ -507,6 +507,11 @@ export type Condition = {
value?: string | Array<string> | number | null
}
+export type ConversationCursorNotFoundError = ErrorResponse & {
+ code: 'conversation_cursor_not_found'
+ status: 404
+}
+
export type ConversationFirstMessageNotFoundError = ErrorResponse & {
code: 'conversation_first_message_not_found'
status: 404
@@ -4983,8 +4988,16 @@ export type GetConversationsData = {
export type GetConversationsErrors = {
400: NotChatAppError | ErrorResponse
401: ErrorResponse
- 403: ErrorResponse
- 404: ErrorResponse
+ 403:
+ | AppAbnormalStatusError
+ | AppApiDisabledError
+ | AppNotFoundError
+ | ForbiddenError
+ | WorkspaceArchivedError
+ | WorkspaceNotFoundError
+ | ErrorResponse
+ 404: ConversationCursorNotFoundError | ConversationNotFoundError | ErrorResponse
+ 422: ErrorResponse
default: ErrorResponse
}
diff --git a/packages/contracts/generated/api/service/zod.gen.ts b/packages/contracts/generated/api/service/zod.gen.ts
index 8aa6a115612..ae3467596e4 100644
--- a/packages/contracts/generated/api/service/zod.gen.ts
+++ b/packages/contracts/generated/api/service/zod.gen.ts
@@ -1052,6 +1052,18 @@ export const zCompletionRequestError = zErrorResponse.and(
}),
)
+/**
+ * ConversationCursorNotFoundError
+ *
+ * The conversation cursor is not in the current list. Refresh the list and try again.
+ */
+export const zConversationCursorNotFoundError = zErrorResponse.and(
+ z.object({
+ code: z.literal('conversation_cursor_not_found'),
+ status: z.literal(404),
+ }),
+)
+
/**
* ConversationFirstMessageNotFoundError
*
@@ -6198,7 +6210,7 @@ export const zGetConversationsQuery = z.object({
})
/**
- * Successfully retrieved conversations list.
+ * Conversations retrieved successfully.
*/
export const zGetConversationsResponse = zConversationInfiniteScrollPagination
diff --git a/packages/contracts/generated/api/web/errors.gen.ts b/packages/contracts/generated/api/web/errors.gen.ts
index 7648c520cd0..68bbe73227c 100644
--- a/packages/contracts/generated/api/web/errors.gen.ts
+++ b/packages/contracts/generated/api/web/errors.gen.ts
@@ -82,6 +82,14 @@ const zCompletionRequestError = /* @__PURE__ */ (() =>
}),
))()
+const zConversationCursorNotFoundError = /* @__PURE__ */ (() =>
+ zErrorResponse.and(
+ z.object({
+ code: z.literal('conversation_cursor_not_found'),
+ status: z.literal(404),
+ }),
+ ))()
+
const zConversationFirstMessageNotFoundError = /* @__PURE__ */ (() =>
zErrorResponse.and(
z.object({
@@ -479,7 +487,11 @@ export const delete_Errors = {
not_chat_app: { status: 400, data: zNotChatAppError },
}
-export const getErrors = { not_chat_app: { status: 400, data: zNotChatAppError } }
+export const getErrors = {
+ conversation_cursor_not_found: { status: 404, data: zConversationCursorNotFoundError },
+ conversation_not_found: { status: 404, data: zConversationNotFoundError },
+ not_chat_app: { status: 400, data: zNotChatAppError },
+}
export const post7Errors = {
account_banned: { status: 400, data: zAccountBannedError },
diff --git a/packages/contracts/generated/api/web/orpc.gen.ts b/packages/contracts/generated/api/web/orpc.gen.ts
index be955cc6272..70d6b7d01fe 100644
--- a/packages/contracts/generated/api/web/orpc.gen.ts
+++ b/packages/contracts/generated/api/web/orpc.gen.ts
@@ -12,6 +12,7 @@ import {
zAudioTooLargeError,
zAuthenticationFailedError,
zCompletionRequestError,
+ zConversationCursorNotFoundError,
zConversationFirstMessageNotFoundError,
zConversationNotFoundError,
zEmailCodeError,
@@ -406,7 +407,11 @@ export const get = oc
})
.input(z.object({ query: zGetConversationsQuery.optional() }).optional())
.output(zGetConversationsResponse)
- .errors({ not_chat_app: { status: 400, data: zNotChatAppError } })
+ .errors({
+ conversation_cursor_not_found: { status: 404, data: zConversationCursorNotFoundError },
+ conversation_not_found: { status: 404, data: zConversationNotFoundError },
+ not_chat_app: { status: 400, data: zNotChatAppError },
+ })
export const conversations = {
get,
diff --git a/packages/contracts/generated/api/web/types.gen.ts b/packages/contracts/generated/api/web/types.gen.ts
index bd8492280c7..827f4079b16 100644
--- a/packages/contracts/generated/api/web/types.gen.ts
+++ b/packages/contracts/generated/api/web/types.gen.ts
@@ -148,6 +148,11 @@ export type CompletionRequestError = ErrorResponse & {
status: 400
}
+export type ConversationCursorNotFoundError = ErrorResponse & {
+ code: 'conversation_cursor_not_found'
+ status: 404
+}
+
export type ConversationFirstMessageNotFoundError = ErrorResponse & {
code: 'conversation_first_message_not_found'
status: 404
@@ -1494,7 +1499,7 @@ export type GetConversationsData = {
export type GetConversationsErrors = {
400: NotChatAppError | ErrorResponse
401: ErrorResponse
- 404: ErrorResponse
+ 404: ConversationCursorNotFoundError | ConversationNotFoundError | ErrorResponse
500: ErrorResponse
default: ErrorResponse
}
diff --git a/packages/contracts/generated/api/web/zod.gen.ts b/packages/contracts/generated/api/web/zod.gen.ts
index d311cf95697..71f4f2892bc 100644
--- a/packages/contracts/generated/api/web/zod.gen.ts
+++ b/packages/contracts/generated/api/web/zod.gen.ts
@@ -314,6 +314,18 @@ export const zCompletionRequestError = zErrorResponse.and(
}),
)
+/**
+ * ConversationCursorNotFoundError
+ *
+ * The conversation cursor is not in the current list. Refresh the list and try again.
+ */
+export const zConversationCursorNotFoundError = zErrorResponse.and(
+ z.object({
+ code: z.literal('conversation_cursor_not_found'),
+ status: z.literal(404),
+ }),
+)
+
/**
* ConversationFirstMessageNotFoundError
*