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.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
refactor(api): migrate trial audio through shared app audio (#42672)
非法操作27ea5e579024
diff --git a/api/.importlinter b/api/.importlinterindex 875b8237054..ea5b463ff11 100644--- a/api/.importlinter+++ b/api/.importlinter@@ -648,3 +648,19 @@ forbidden_modules =sqlalchemytaskswerkzeug++[importlinter:contract:app-audio-boundary]+name = App audio operations and values are framework and persistence neutral+type = forbidden+source_modules =+ services.app_audio_service+ services.audio_types+forbidden_modules =+ controllers+ extensions+ flask+ models+ repositories+ services.audio_service+ sqlalchemy+ werkzeugdiff --git a/api/controllers/common/audio_response.py b/api/controllers/common/audio_response.pynew file mode 100644index 00000000000..b744cc54793--- /dev/null+++ b/api/controllers/common/audio_response.py@@ -0,0 +1,27 @@+"""Create binary audio responses and own their stream cleanup."""++from flask import Response, stream_with_context++from core.base.tts.audio_mime import inspect_audio_stream, resolve_audio_mime_type+from services.audio_types import AudioOutput+++def audio_binary_response(output: AudioOutput | None) -> Response | None:+ """Preserve audio MIME and request context while releasing provider streams."""+ if output is None:+ return None+ if isinstance(output.data, (bytes, bytearray, memoryview)):+ data = bytes(output.data)+ return Response(data, content_type=resolve_audio_mime_type(data, output.mime_type))++ audio_stream, mime_type = inspect_audio_stream(output.data, output.mime_type)+ try:+ response = Response(+ stream_with_context(audio_stream), # pyrefly: ignore[no-matching-overload]+ content_type=mime_type,+ )+ response.call_on_close(audio_stream.close)+ return response+ except BaseException:+ audio_stream.close()+ raisediff --git a/api/controllers/console/explore/trial.py b/api/controllers/console/explore/trial.pyindex e7e02a0b14a..4ff3fed3bf5 100644--- a/api/controllers/console/explore/trial.py+++ b/api/controllers/console/explore/trial.py@@ -1,17 +1,19 @@import logging-from collections.abc import Mapping+from collections.abc import Callable, Mappingfrom dataclasses import asdictfrom datetime import datetime+from functools import wrapsfrom typing import Any, Literalfrom uuid import UUIDfrom flask import Response, requestfrom flask_restx import Resourcefrom pydantic import AliasChoices, BaseModel, Field, field_validator-from werkzeug.exceptions import InternalServerError, NotFound, Unauthorized+from werkzeug.exceptions import HTTPException, InternalServerError, NotFound, Unauthorizedimport servicesfrom configs import dify_config+from controllers.common.audio_response import audio_binary_responsefrom controllers.common.fields import (AudioBinaryResponse,AudioTranscriptResponse,@@ -55,11 +57,10 @@ from controllers.console.explore.error import (NotWorkflowAppError,)from controllers.console.explore.trial_app_admission import get_trial_app-from controllers.console.explore.wraps import TrialAppResourcefrom controllers.console.files import FILE_UPLOAD_PARAMS, upload_file_from_request_contextfrom controllers.console.flask_admission import console_account_admissionfrom controllers.console.remote_files import RemoteFileUploadPayload, upload_remote_file-from controllers.console.wraps import cloud_edition_billing_resource_check, model_validate, with_current_user+from controllers.console.wraps import cloud_edition_billing_resource_check, model_validatefrom controllers.web.error import InvokeRateLimitError as InvokeRateLimitHttpErrorfrom core.errors.error import (AppInvokeQuotaExceededError,@@ -70,7 +71,6 @@ from core.errors.error import (from core.helper import encrypterfrom core.workflow.llm_environment_variable import LLMEnvironmentVariable, dump_environment_variablefrom extensions.ext_application_services import application_services-from extensions.ext_database import dbfrom fields.base import ResponseModelfrom fields.conversation_variable_fields import WorkflowConversationVariableResponsefrom fields.file_fields import FileResponse, FileWithSignedUrl@@ -79,9 +79,9 @@ from graphon.model_runtime.errors.invoke import InvokeErrorfrom graphon.variables import SecretVariable, VariableBasefrom libs import helperfrom libs.helper import dump_response, to_timestamp, uuid_value+from libs.stream import close_streamfrom libs.url_utils import normalize_api_base_urlfrom machinery.context import RequestContext-from models import Accountfrom models.enums import CreatorUserRolefrom services.account_errors import AccountNotFoundErrorfrom services.app_definition_query_service import AppDefinitionUnavailableError@@ -91,8 +91,7 @@ from services.app_preview_query_service import (AppPreviewSiteUnavailableError,AppPreviewUnavailableError,)-from services.app_ref_service import AppRefService-from services.audio_service import AudioService+from services.audio_types import AudioAppRef, AudioUploadfrom services.errors.audio import (AudioTooLargeServiceError,NoAudioUploadedServiceError,@@ -642,112 +641,86 @@ class TrialMessageSuggestedQuestionApi(Resource):return dump_response(SuggestedQuestionsResponse, {"data": questions})-class TrialChatAudioApi(TrialAppResource):+def _trial_audio_errors[**P, R](view: Callable[P, R]) -> Callable[P, R]:+ @wraps(view)+ def decorated(*args: P.args, **kwargs: P.kwargs) -> R:+ try:+ return view(*args, **kwargs)+ except (AppDefinitionUnavailableError, services.errors.app_model_config.AppModelConfigBrokenError) as error:+ raise AppUnavailableError() from error+ except NoAudioUploadedServiceError as error:+ raise NoAudioUploadedError() from error+ except AudioTooLargeServiceError as error:+ raise AudioTooLargeError(str(error)) from error+ except UnsupportedAudioTypeServiceError as error:+ raise UnsupportedAudioTypeError() from error+ except ProviderNotSupportSpeechToTextServiceError as error:+ raise ProviderNotSupportSpeechToTextError() from error+ except SpeechToTextDisabledServiceError as error:+ raise SpeechToTextDisabledError() from error+ except ProviderTokenNotInitError as error:+ raise ProviderNotInitializeError(error.description) from error+ except QuotaExceededError as error:+ raise ProviderQuotaExceededError() from error+ except ModelCurrentlyNotSupportError as error:+ raise ProviderModelCurrentlyNotSupportError() from error+ except InvokeError as error:+ raise CompletionRequestError(error.description) from error+ except (HTTPException, ValueError):+ raise+ except Exception as error:+ logger.exception("Trial audio operation failed")+ raise InternalServerError() from error++ return decorated+++class TrialChatAudioApi(Resource):@console_ns.response(200, "Success", console_ns.models[AudioTranscriptResponse.__name__])- @with_current_user- def post(self, current_user: Account, trial_app):- app_model = trial_app-+ @console_account_admission()+ @get_trial_app+ @_trial_audio_errors+ def post(self, request_context: RequestContext, trial_app: TrialAppRef) -> dict[str, object]:file = request.files.get("file")-- try:- # Get IDs before they might be detached from session- app_id = app_model.id- user_id = current_user.id-- response = AudioService.transcript_asr(- app_model=app_model,- file=file,- session=db.session(),- end_user=None,- )- application_services().trial_app_usage.record(app_id=app_id, account_id=user_id)- return response- except services.errors.app_model_config.AppModelConfigBrokenError:- logger.exception("App model config broken.")- raise AppUnavailableError()- except NoAudioUploadedServiceError:- raise NoAudioUploadedError()- except AudioTooLargeServiceError as e:- raise AudioTooLargeError(str(e))- except UnsupportedAudioTypeServiceError:- raise UnsupportedAudioTypeError()- except ProviderNotSupportSpeechToTextServiceError:- raise ProviderNotSupportSpeechToTextError()- except SpeechToTextDisabledServiceError:- raise SpeechToTextDisabledError()- except ProviderTokenNotInitError as ex:- raise ProviderNotInitializeError(ex.description)- except QuotaExceededError:- raise ProviderQuotaExceededError()- except ModelCurrentlyNotSupportError:- raise ProviderModelCurrentlyNotSupportError()- except InvokeError as e:- raise CompletionRequestError(e.description)- except ValueError as e:- raise e- except Exception as e:- logger.exception("internal server error.")- raise InternalServerError()+ audio = AudioUpload(stream=file.stream, mime_type=file.mimetype) if file is not None else None+ transcript = application_services().app_audio.transcript_asr(+ app=AudioAppRef(app_id=trial_app.app_id, tenant_id=trial_app.tenant_id, app_mode=trial_app.app_mode),+ audio=audio,+ )+ application_services().trial_app_usage.record(app_id=trial_app.app_id, account_id=request_context.account_id)+ return dump_response(AudioTranscriptResponse, transcript)-class TrialChatTextApi(TrialAppResource):+class TrialChatTextApi(Resource):@console_ns.expect(console_ns.models[TextToSpeechRequest.__name__])@console_ns.response(200, "Success", console_ns.models[AudioBinaryResponse.__name__])- @with_current_user+ @console_account_admission()+ @get_trial_app@model_validate(TextToSpeechRequest)- def post(self, req_data: TextToSpeechRequest, current_user: Account, trial_app):- app_model = trial_app+ @_trial_audio_errors+ def post(+ self, req_data: TextToSpeechRequest, request_context: RequestContext, trial_app: TrialAppRef+ ) -> Response | None:+ output = application_services().app_audio.transcript_tts(+ app=AudioAppRef(app_id=trial_app.app_id, tenant_id=trial_app.tenant_id, app_mode=trial_app.app_mode),+ account_id=request_context.account_id,+ text=req_data.text,+ voice=req_data.voice,+ message_id=req_data.message_id,+ )+ response = audio_binary_response(output)try:- message_id = req_data.message_id- text = req_data.text- voice = req_data.voice- message_ref = None- if message_id:- app_ref = AppRefService.create_app_ref(app_model)- message_ref = AppRefService.create_message_ref(- app_ref,- message_id,- account_id=current_user.id,- )-- # Get IDs before they might be detached from session- app_id = app_model.id- user_id = current_user.id-- response = AudioService.transcript_tts(- app_model=app_model,- session=db.session(),- text=text,- voice=voice,- message_ref=message_ref,+ # Preserve usage after MIME inspection, including a missing message's+ # null response. Early provider/MIME failures do not consume a trial.+ application_services().trial_app_usage.record(+ app_id=trial_app.app_id, account_id=request_context.account_id)- application_services().trial_app_usage.record(app_id=app_id, account_id=user_id)- return response- except services.errors.app_model_config.AppModelConfigBrokenError:- logger.exception("App model config broken.")- raise AppUnavailableError()- except NoAudioUploadedServiceError:- raise NoAudioUploadedError()- except AudioTooLargeServiceError as e:- raise AudioTooLargeError(str(e))- except UnsupportedAudioTypeServiceError:- raise UnsupportedAudioTypeError()- except ProviderNotSupportSpeechToTextServiceError:- raise ProviderNotSupportSpeechToTextError()- except ProviderTokenNotInitError as ex:- raise ProviderNotInitializeError(ex.description)- except QuotaExceededError:- raise ProviderQuotaExceededError()- except ModelCurrentlyNotSupportError:- raise ProviderModelCurrentlyNotSupportError()- except InvokeError as e:- raise CompletionRequestError(e.description)- except ValueError as e:- raise e- except Exception as e:- logger.exception("internal server error.")- raise InternalServerError()+ except BaseException:+ if response is not None:+ close_stream(response)+ raise+ # response-contract:ignore audio_binary_response+ return responseclass TrialCompletionApi(Resource):diff --git a/api/controllers/console/explore/wraps.py b/api/controllers/console/explore/wraps.pyindex 6e9faa1a6fa..f10cb000c53 100644--- a/api/controllers/console/explore/wraps.py+++ b/api/controllers/console/explore/wraps.py@@ -6,16 +6,11 @@ from flask_restx import Resourcefrom sqlalchemy import selectfrom werkzeug.exceptions import NotFound-from controllers.console.explore.error import (- AppAccessDeniedError,- TrialAppLimitExceeded,- TrialAppNotAllowed,-)-from controllers.console.explore.trial_app_admission import trial_feature_enable+from controllers.console.explore.error import AppAccessDeniedErrorfrom controllers.console.wraps import account_initialization_requiredfrom extensions.ext_database import dbfrom libs.login import current_account_with_tenant, login_required-from models import AccountTrialAppRecord, App, InstalledApp, TrialApp+from models import InstalledAppfrom services.enterprise.enterprise_service import EnterpriseServicefrom services.system_feature_service import SystemFeatureService@@ -72,40 +67,6 @@ def user_allowed_to_access_app[**P, R](view: Callable[Concatenate[InstalledApp,return decorator-def trial_app_required[**P, R](view: Callable[Concatenate[App, P], R] | None = None):- def decorator(view: Callable[Concatenate[App, P], R]):- @wraps(view)- def decorated(app_id: str, *args: P.args, **kwargs: P.kwargs):- current_user, _ = current_account_with_tenant()- session = db.session()-- trial_app = session.scalar(select(TrialApp).where(TrialApp.app_id == str(app_id)).limit(1))-- if trial_app is None:- raise TrialAppNotAllowed()- app = trial_app.app_with_session(session=session)-- if app is None:- raise TrialAppNotAllowed()-- account_trial_app_record = session.scalar(- select(AccountTrialAppRecord)- .where(AccountTrialAppRecord.account_id == current_user.id, AccountTrialAppRecord.app_id == app_id)- .limit(1)- )- if account_trial_app_record:- if account_trial_app_record.count >= trial_app.trial_limit:- raise TrialAppLimitExceeded()-- return view(app, *args, **kwargs)-- return decorated-- if view:- return decorator(view)- return decorator--class InstalledAppResource(Resource):# must be reversed if there are multiple decorators@@ -115,14 +76,3 @@ class InstalledAppResource(Resource):account_initialization_required,login_required,]---class TrialAppResource(Resource):- # must be reversed if there are multiple decorators-- method_decorators = [- trial_app_required,- trial_feature_enable,- account_initialization_required,- login_required,- ]diff --git a/api/core/base/tts/audio_mime.py b/api/core/base/tts/audio_mime.pyindex cd2c86a6b8a..1c0b73c4500 100644--- a/api/core/base/tts/audio_mime.py+++ b/api/core/base/tts/audio_mime.py@@ -1,11 +1,12 @@import logging-from collections.abc import Generator, Iterable+from collections.abc import Generator, Iterable, Iteratorfrom itertools import chain-from typing import TYPE_CHECKING+from typing import TYPE_CHECKING, overridefrom core.plugin.entities.plugin_daemon import TTSAudioChunkfrom graphon.model_runtime.entities.model_entities import ModelPropertyKeyfrom graphon.model_runtime.errors.invoke import InvokeBadRequestError+from libs.stream import close_streamif TYPE_CHECKING:from core.model_manager import ModelInstance@@ -140,32 +141,73 @@ def resolve_audio_mime_type(return normalized_declared_mime_type or DEFAULT_TTS_AUDIO_MIME_TYPE+type _AudioChunk = bytes | bytearray | memoryview | TTSAudioChunk+++class AudioStream(Iterator[bytes]):+ """Own a MIME-checked stream, including resources opened while peeking."""++ def __init__(+ self, chunks: Generator[bytes, None, None], *, source: Iterable[_AudioChunk], iterator: Iterator[_AudioChunk]+ ) -> None:+ self._chunks: Generator[bytes, None, None] = chunks+ self._source: Iterable[_AudioChunk] = source+ self._iterator: Iterator[_AudioChunk] = iterator+ self._closed: bool = False++ @override+ def __next__(self) -> bytes:+ try:+ return next(self._chunks)+ except BaseException:+ self.close()+ raise++ def close(self) -> None:+ if self._closed:+ return+ self._closed = True+ # The validation generator may never have started, but peeking has+ # already opened the provider. Close its source explicitly as well.+ close_stream(self._chunks)+ close_stream(self._iterator)+ if self._source is not self._iterator:+ close_stream(self._source)++def inspect_audio_stream(- audio_stream: Iterable[bytes | bytearray | memoryview | TTSAudioChunk], declared_mime_type: str | None = None-) -> tuple[Generator[bytes, None, None], str]:- """Peek at and validate a TTS stream without dropping its leading bytes."""- iterator = iter(audio_stream)+ audio_stream: Iterable[_AudioChunk], declared_mime_type: str | None = None+) -> tuple[AudioStream, str]:+ """Peek and validate without dropping bytes; the returned stream owns cleanup."""+ iterator: Iterator[_AudioChunk] | None = Noneleading_chunks: list[bytes] = []signature = bytearray()reported_mime_type: str | None = None- while len(signature) < _SIGNATURE_SIZE:- try:- chunk, chunk_mime_type = _extract_audio_chunk(next(iterator))- except StopIteration:- break- normalized_chunk_mime_type = _normalize_reported_mime_type(chunk_mime_type, "chunk")- if normalized_chunk_mime_type:- if reported_mime_type and reported_mime_type != normalized_chunk_mime_type:- raise InvokeBadRequestError(- "TTS provider changed MIME type within one audio response: "- f"{reported_mime_type} then {normalized_chunk_mime_type}"- )- reported_mime_type = normalized_chunk_mime_type- leading_chunks.append(chunk)- signature.extend(chunk[: _SIGNATURE_SIZE - len(signature)])-- mime_type = resolve_audio_mime_type(signature, declared_mime_type, reported_mime_type)+ try:+ iterator = iter(audio_stream)+ while len(signature) < _SIGNATURE_SIZE:+ try:+ chunk, chunk_mime_type = _extract_audio_chunk(next(iterator))+ except StopIteration:+ break+ normalized_chunk_mime_type = _normalize_reported_mime_type(chunk_mime_type, "chunk")+ if normalized_chunk_mime_type:+ if reported_mime_type and reported_mime_type != normalized_chunk_mime_type:+ raise InvokeBadRequestError(+ "TTS provider changed MIME type within one audio response: "+ f"{reported_mime_type} then {normalized_chunk_mime_type}"+ )+ reported_mime_type = normalized_chunk_mime_type+ leading_chunks.append(chunk)+ signature.extend(chunk[: _SIGNATURE_SIZE - len(signature)])++ mime_type = resolve_audio_mime_type(signature, declared_mime_type, reported_mime_type)+ except BaseException:+ close_stream(iterator)+ if audio_stream is not iterator:+ close_stream(audio_stream)+ raisedef validated_stream() -> Generator[bytes, None, None]:for chunk in chain(leading_chunks, iterator):@@ -178,4 +220,4 @@ def inspect_audio_stream()yield audio- return validated_stream(), mime_type+ return AudioStream(validated_stream(), source=audio_stream, iterator=iterator), mime_typediff --git a/api/extensions/ext_application_services.py b/api/extensions/ext_application_services.pyindex 7053ed02caf..8169e98c18f 100644--- a/api/extensions/ext_application_services.py+++ b/api/extensions/ext_application_services.py@@ -136,6 +136,8 @@ from services.account_oauth_service import AccountOAuthService, OAuthProviderGatfrom services.account_password_hasher import DefaultAccountPasswordHasherfrom services.account_password_service import AccountPasswordServicefrom services.account_profile_service import AccountProfileService+from services.app_audio_adapters import AppAudioRuntime+from services.app_audio_service import AppAudiofrom services.app_definition_query_service import AppDefinitionQueryServicefrom services.app_preview_details_adapters import AppPreviewDetailsRuntimefrom services.app_preview_details_service import AppPreviewDetails@@ -296,6 +298,7 @@ class ApplicationServices:remote_files: RemoteFileServiceapp_tasks: AppTaskControlServicetrial_app_access: TrialAppAccessService+ app_audio: AppAudiotrial_app_generation: TrialAppGenerationServicetrial_app_usage: TrialAppUsageRecorderworkflow_run_archives: WorkflowRunArchiveService@@ -737,6 +740,7 @@ def build_application_services(remote_files=remote_file_service,app_tasks=AppTaskControlService(redis_client=redis),trial_app_access=TrialAppAccessService(apps=trial_apps),+ app_audio=AppAudioRuntime(session_factory=database_client),trial_app_generation=TrialAppGenerationService(runtime=AppGenerateServiceRuntime(session_factory=database_client), usage=trial_apps),diff --git a/api/libs/stream.py b/api/libs/stream.pynew file mode 100644index 00000000000..0472db8a6b0--- /dev/null+++ b/api/libs/stream.py@@ -0,0 +1,20 @@+"""Release optional stream resources without replacing an invocation error."""++import logging+from typing import Protocol, runtime_checkable++logger = logging.getLogger(__name__)+++@runtime_checkable+class _Closable(Protocol):+ def close(self) -> None: ...+++def close_stream(stream: object) -> None:+ """Close resource-owning streams; plain iterables need no cleanup."""+ if isinstance(stream, _Closable):+ try:+ stream.close()+ except Exception:+ logger.warning("Failed to close stream", exc_info=True)diff --git a/api/services/app_audio_adapters.py b/api/services/app_audio_adapters.pynew file mode 100644index 00000000000..b7f3c84258b--- /dev/null+++ b/api/services/app_audio_adapters.py@@ -0,0 +1,68 @@+"""Bridge app references to the shared audio configuration and provider owners.++AudioService still owns ORM configuration policy. Prepare with a bounded+session here, then release it before reading uploads or invoking providers.+"""++from typing import override++from sqlalchemy import select+from sqlalchemy.orm import Session, sessionmaker++from models import App+from services.app_audio_service import AppAudio+from services.app_definition_query_service import AppDefinitionUnavailableError+from services.app_ref_service import AppRefService+from services.audio_service import AudioService+from services.audio_types import AudioAppRef, AudioOutput, AudioUpload+++class AppAudioRuntime(AppAudio):+ def __init__(self, *, session_factory: sessionmaker[Session]) -> None:+ self._session_factory: sessionmaker[Session] = session_factory++ @override+ def transcript_asr(self, *, app: AudioAppRef, audio: AudioUpload | None) -> dict[str, str]:+ with self._session_factory(expire_on_commit=False) as session:+ app_model = self._get_app(session=session, app=app)+ AudioService.prepare_asr(app_model, session=session)++ return AudioService.invoke_speech_to_text(app_model, audio, end_user=None)++ @override+ def transcript_tts(+ self,+ *,+ app: AudioAppRef,+ account_id: str,+ text: str | None,+ voice: str | None,+ message_id: str | None,+ ) -> AudioOutput | None:+ with self._session_factory(expire_on_commit=False) as session:+ app_model = self._get_app(session=session, app=app)+ message_ref = (+ AppRefService.create_message_ref(+ AppRefService.create_app_ref(app_model), message_id, account_id=account_id+ )+ if message_id+ else None+ )+ prepared = AudioService.prepare_tts(+ app_model, session=session, text=text, voice=voice, message_ref=message_ref+ )++ if prepared is None:+ return None+ return AudioService.invoke_tts(app_model, text=prepared.text, voice=prepared.voice, end_user=None)++ @staticmethod+ def _get_app(*, session: Session, app: AudioAppRef) -> App:+ app_model = session.scalar(select(App).where(App.id == app.app_id, App.tenant_id == app.tenant_id))+ if app_model is None:+ raise AppDefinitionUnavailableError(f"App {app.app_id} no longer exists in tenant {app.tenant_id}")+ if app_model.mode != app.app_mode:+ raise AppDefinitionUnavailableError(+ f"App {app.app_id} mode changed from {app.app_mode} to {app_model.mode} after admission"+ )+ return app_modeldiff --git a/api/services/app_audio_service.py b/api/services/app_audio_service.pynew file mode 100644index 00000000000..7757fef6120--- /dev/null+++ b/api/services/app_audio_service.py@@ -0,0 +1,19 @@+"""Audio operations for an admitted app, independent of its entry point."""++from typing import Protocol++from services.audio_types import AudioAppRef, AudioOutput, AudioUpload+++class AppAudio(Protocol):+ def transcript_asr(self, *, app: AudioAppRef, audio: AudioUpload | None) -> dict[str, str]: ...++ def transcript_tts(+ self,+ *,+ app: AudioAppRef,+ account_id: str,+ text: str | None,+ voice: str | None,+ message_id: str | None,+ ) -> AudioOutput | None: ...diff --git a/api/services/audio_provider_gateway.py b/api/services/audio_provider_gateway.pynew file mode 100644index 00000000000..882dc6879b1--- /dev/null+++ b/api/services/audio_provider_gateway.py@@ -0,0 +1,68 @@+"""Invoke the existing model runtime using plain audio values.++Provider credential queries retain their existing scoped-session behavior.+Admitted app runtimes close their configuration sessions before entering this+module; legacy callers continue to own their request session.+"""++import io++from core.app.entities.app_invoke_entities import get_credit_usage_app_type+from core.base.tts.audio_mime import get_model_audio_mime_type+from core.credit_usage import CreditUsageCreatedBy+from core.model_manager import ModelManager+from graphon.model_runtime.entities.model_entities import ModelType+from graphon.model_runtime.protocols.tts_runtime import TTSModelVoice+from services.audio_types import AudioAppRef, AudioOutput+from services.errors.audio import (+ ProviderNotSupportSpeechToTextServiceError,+ ProviderNotSupportTextToSpeechServiceError,+)+++def speech_to_text(*, app: AudioAppRef, content: bytes, end_user: str | None) -> str:+ model_manager = ModelManager.for_tenant(+ tenant_id=app.tenant_id,+ user_id=end_user,+ request_metadata={+ "app_type": get_credit_usage_app_type(app.app_mode),+ "created_by": CreditUsageCreatedBy.AUDIO,+ },+ )+ model_instance = model_manager.get_default_model_instance(tenant_id=app.tenant_id, model_type=ModelType.SPEECH2TEXT)+ if model_instance is None:+ raise ProviderNotSupportSpeechToTextServiceError()+ buffer = io.BytesIO(content)+ buffer.name = "temp.mp3"+ return model_instance.invoke_speech2text(file=buffer)+++def text_to_speech(*, app: AudioAppRef, text: str, voice: str | None, end_user: str | None) -> AudioOutput:+ model_manager = ModelManager.for_tenant(+ tenant_id=app.tenant_id,+ user_id=end_user,+ request_metadata={+ "app_type": get_credit_usage_app_type(app.app_mode),+ "created_by": CreditUsageCreatedBy.AUDIO,+ },+ )+ model_instance = model_manager.get_default_model_instance(tenant_id=app.tenant_id, model_type=ModelType.TTS)+ if not voice:+ voices = model_instance.get_tts_voices()+ if not voices or not (voice := voices[0].get("value")):+ raise ValueError("Sorry, no voice available.")+ return AudioOutput(+ data=model_instance.invoke_tts(content_text=text, voice=voice),+ mime_type=get_model_audio_mime_type(model_instance),+ )+++def get_voices(*, tenant_id: str, language: str) -> list[TTSModelVoice]:+ model_manager = ModelManager.for_tenant(+ tenant_id=tenant_id,+ request_metadata={"created_by": CreditUsageCreatedBy.AUDIO},+ )+ model_instance = model_manager.get_default_model_instance(tenant_id=tenant_id, model_type=ModelType.TTS)+ if model_instance is None:+ raise ProviderNotSupportTextToSpeechServiceError()+ return model_instance.get_tts_voices(language)diff --git a/api/services/audio_service.py b/api/services/audio_service.pyindex 9b97ee2fe3f..bf33746296c 100644--- a/api/services/audio_service.py+++ b/api/services/audio_service.py@@ -1,7 +1,15 @@-import io+"""Shared audio policy and compatibility entry points for existing callers.++TODO: Migrate configuration reads with the Agent/Workflow query owners before+replacing these entry points. Keep feature precedence and message eligibility+here so admitted app runtimes and legacy callers share one implementation.+Provider SDK calls use the plain values in audio_types.+"""+import loggingimport uuidfrom collections.abc import Iterable+from dataclasses import dataclassfrom typing import castfrom flask import Response, stream_with_context@@ -11,21 +19,18 @@ from werkzeug.datastructures import FileStoragefrom constants import AUDIO_EXTENSIONSfrom core.app.apps.agent_app.app_feature_projection import merge_agent_app_features-from core.app.entities.app_invoke_entities import get_credit_usage_app_type-from core.base.tts.audio_mime import get_model_audio_mime_type, inspect_audio_stream, resolve_audio_mime_type-from core.credit_usage import CreditUsageCreatedBy-from core.model_manager import ModelManager-from graphon.model_runtime.entities.model_entities import ModelType+from core.base.tts.audio_mime import inspect_audio_stream, resolve_audio_mime_type+from graphon.model_runtime.protocols.tts_runtime import TTSModelVoicefrom models.agent_config_entities import AgentSoulConfigfrom models.enums import MessageStatusfrom models.model import App, AppMode, Message, load_annotation_reply_config+from services import audio_provider_gatewayfrom services.agent.roster_service import AgentRosterServicefrom services.app_ref_service import MessageRef+from services.audio_types import AudioAppRef, AudioOutput, AudioUploadfrom services.errors.audio import (AudioTooLargeServiceError,NoAudioUploadedServiceError,- ProviderNotSupportSpeechToTextServiceError,- ProviderNotSupportTextToSpeechServiceError,SpeechToTextDisabledServiceError,UnsupportedAudioTypeServiceError,)@@ -40,6 +45,14 @@ _ASR_MIME_TYPE_ALIASES = {logger = logging.getLogger(__name__)+@dataclass(frozen=True, slots=True)+class PreparedTextToAudio:+ """Message text and configured voice resolved before contacting a provider."""++ text: str+ voice: str | None++def _create_tts_response(audio: Iterable[bytes] | bytes | bytearray | memoryview, declared_mime_type: str | None) -> Response:@@ -49,10 +62,12 @@ def _create_tts_response(return Response(audio_bytes, content_type=resolve_audio_mime_type(audio_bytes, declared_mime_type))audio_stream, mime_type = inspect_audio_stream(audio, declared_mime_type)- return Response(+ response = Response(stream_with_context(audio_stream), # pyrefly: ignore[no-matching-overload]content_type=mime_type,)+ response.call_on_close(audio_stream.close)+ return responseclass AudioService:@@ -85,19 +100,28 @@ class AudioService:Raises:SpeechToTextDisabledServiceError: If the effective feature configuration disables STT."""+ cls.prepare_asr(app_model, session=session)+ return cls.invoke_speech_to_text(+ app_model=app_model,+ audio=AudioUpload(stream=file.stream, mime_type=file.mimetype) if file is not None else None,+ end_user=end_user,+ )++ @classmethod+ def prepare_asr(cls, app_model: App, *, session: Session) -> None:+ """Validate the effective published ASR feature using the caller's session."""if app_model.mode == AppMode.AGENT:agent_soul = AgentRosterService(session).get_published_agent_soul_for_app(tenant_id=app_model.tenant_id,app_id=app_model.id,)if agent_soul is not None:- return cls.transcript_agent_asr(+ cls._prepare_agent_asr(app_model=app_model,agent_soul=agent_soul,- file=file,session=session,- end_user=end_user,)+ returnif app_model.mode in {AppMode.ADVANCED_CHAT, AppMode.WORKFLOW}:workflow = app_model.workflow_with_session(session=session)@@ -115,8 +139,6 @@ class AudioService:if not app_model_config.speech_to_text_dict["enabled"]:raise SpeechToTextDisabledServiceError()- return cls._invoke_speech_to_text(app_model=app_model, file=file, end_user=end_user)-@classmethoddef transcript_agent_asr(cls,@@ -132,6 +154,15 @@ class AudioService:Raises:SpeechToTextDisabledServiceError: If the merged Agent feature configuration disables STT."""+ cls._prepare_agent_asr(app_model=app_model, agent_soul=agent_soul, session=session)+ return cls.invoke_speech_to_text(+ app_model=app_model,+ audio=AudioUpload(stream=file.stream, mime_type=file.mimetype) if file is not None else None,+ end_user=end_user,+ )++ @staticmethod+ def _prepare_agent_asr(app_model: App, agent_soul: AgentSoulConfig, *, session: Session) -> None:app_model_config = app_model.app_model_config_with_session(session=session)annotation_reply = load_annotation_reply_config(session, app_model.id) if app_model_config else Nonefeatures = merge_agent_app_features(@@ -142,44 +173,27 @@ class AudioService:if not features.get("speech_to_text", {}).get("enabled"):raise SpeechToTextDisabledServiceError()- return cls._invoke_speech_to_text(app_model=app_model, file=file, end_user=end_user)-@classmethod- def _invoke_speech_to_text(- cls, app_model: App, file: FileStorage | None, end_user: str | None = None+ def invoke_speech_to_text(+ cls, app_model: App, audio: AudioUpload | None, *, end_user: str | None = None) -> dict[str, str]:- if file is None:+ """Validate the upload and invoke ASR after application configuration is resolved."""+ if audio is None:raise NoAudioUploadedServiceError()- mimetype = _ASR_MIME_TYPE_ALIASES.get(file.mimetype, file.mimetype)+ mimetype = _ASR_MIME_TYPE_ALIASES.get(audio.mime_type, audio.mime_type)if mimetype not in [f"audio/{ext}" for ext in AUDIO_EXTENSIONS]:raise UnsupportedAudioTypeServiceError()- file_content = file.stream.read()+ file_content = audio.stream.read()file_size = len(file_content)if file_size > FILE_SIZE_LIMIT:message = f"Audio size larger than {FILE_SIZE} mb"raise AudioTooLargeServiceError(message)- model_manager = ModelManager.for_tenant(- tenant_id=app_model.tenant_id,- user_id=end_user,- request_metadata={- "app_type": get_credit_usage_app_type(app_model.mode),- "created_by": CreditUsageCreatedBy.AUDIO,- },- )- model_instance = model_manager.get_default_model_instance(- tenant_id=app_model.tenant_id, model_type=ModelType.SPEECH2TEXT- )- if model_instance is None:- raise ProviderNotSupportSpeechToTextServiceError()-- buffer = io.BytesIO(file_content)- buffer.name = "temp.mp3"-- return {"text": model_instance.invoke_speech2text(file=buffer)}+ app_ref = AudioAppRef(app_id=app_model.id, tenant_id=app_model.tenant_id, app_mode=app_model.mode.value)+ return {"text": audio_provider_gateway.speech_to_text(app=app_ref, content=file_content, end_user=end_user)}@classmethoddef transcript_tts(@@ -192,62 +206,32 @@ class AudioService:end_user: str | None = None,message_ref: MessageRef | None = None,is_draft: bool = False,- ):- def invoke_tts(text_content: str, app_model: App, voice: str | None = None, is_draft: bool = False):- if voice is None:- if app_model.mode in {AppMode.ADVANCED_CHAT, AppMode.WORKFLOW}:- if is_draft:- workflow = WorkflowService().get_draft_workflow(app_model=app_model, session=session)- else:- workflow = app_model.workflow_with_session(session=session)- if (- workflow is None- or "text_to_speech" not in workflow.features_dict- or not workflow.features_dict["text_to_speech"].get("enabled")- ):- raise ValueError("TTS is not enabled")-- voice = workflow.features_dict["text_to_speech"].get("voice")- else:- if not is_draft:- app_model_config = app_model.app_model_config_with_session(session=session)- if app_model_config is None:- raise ValueError("AppModelConfig not found")- text_to_speech_dict = app_model_config.text_to_speech_dict-- if not text_to_speech_dict.get("enabled"):- raise ValueError("TTS is not enabled")-- voice = cast(str | None, text_to_speech_dict.get("voice"))-- model_manager = ModelManager.for_tenant(- tenant_id=app_model.tenant_id,- user_id=end_user,- request_metadata={- "app_type": get_credit_usage_app_type(app_model.mode),- "created_by": CreditUsageCreatedBy.AUDIO,- },- )- model_instance = model_manager.get_default_model_instance(- tenant_id=app_model.tenant_id, model_type=ModelType.TTS- )- try:- if not voice:- voices = model_instance.get_tts_voices()- if voices:- voice = voices[0].get("value")- if not voice:- raise ValueError("Sorry, no voice available.")- else:- raise ValueError("Sorry, no voice available.")-- return (- model_instance.invoke_tts(content_text=text_content.strip(), voice=voice),- get_model_audio_mime_type(model_instance),- )- except Exception as e:- raise e+ ) -> Response | None:+ prepared = cls.prepare_tts(+ app_model,+ session=session,+ text=text,+ voice=voice,+ message_ref=message_ref,+ is_draft=is_draft,+ )+ if prepared is None:+ return None+ output = cls.invoke_tts(app_model, text=prepared.text, voice=prepared.voice, end_user=end_user)+ return _create_tts_response(output.data, output.mime_type)+ @classmethod+ def prepare_tts(+ cls,+ app_model: App,+ *,+ session: Session,+ text: str | None = None,+ voice: str | None = None,+ message_ref: MessageRef | None = None,+ is_draft: bool = False,+ ) -> PreparedTextToAudio | None:+ """Resolve owned message text and configured voice without invoking TTS."""if message_ref:try:uuid.UUID(message_ref.message_id)@@ -258,31 +242,50 @@ class AudioService:return Noneif message.answer == "" and message.status in {MessageStatus.NORMAL, MessageStatus.PAUSED}:return None+ text = message.answer+ elif text is None:+ raise ValueError("Text is required")++ if voice is None:+ if app_model.mode in {AppMode.ADVANCED_CHAT, AppMode.WORKFLOW}:+ if is_draft:+ workflow = WorkflowService().get_draft_workflow(app_model=app_model, session=session)+ else:+ workflow = app_model.workflow_with_session(session=session)+ if (+ workflow is None+ or "text_to_speech" not in workflow.features_dict+ or not workflow.features_dict["text_to_speech"].get("enabled")+ ):+ raise ValueError("TTS is not enabled")- else:- response, declared_mime_type = invoke_tts(- text_content=message.answer, app_model=app_model, voice=voice, is_draft=is_draft- )- return _create_tts_response(response, declared_mime_type)- else:- if text is None:- raise ValueError("Text is required")- response, declared_mime_type = invoke_tts(- text_content=text, app_model=app_model, voice=voice, is_draft=is_draft- )- return _create_tts_response(response, declared_mime_type)+ voice = workflow.features_dict["text_to_speech"].get("voice")+ elif not is_draft:+ app_model_config = app_model.app_model_config_with_session(session=session)+ if app_model_config is None:+ raise ValueError("AppModelConfig not found")+ text_to_speech_dict = app_model_config.text_to_speech_dict++ if not text_to_speech_dict.get("enabled"):+ raise ValueError("TTS is not enabled")++ voice = cast(str | None, text_to_speech_dict.get("voice"))++ return PreparedTextToAudio(text=text, voice=voice)@classmethod- def transcript_tts_voices(cls, tenant_id: str, language: str):- model_manager = ModelManager.for_tenant(- tenant_id=tenant_id,- request_metadata={"created_by": CreditUsageCreatedBy.AUDIO},- )- model_instance = model_manager.get_default_model_instance(tenant_id=tenant_id, model_type=ModelType.TTS)- if model_instance is None:- raise ProviderNotSupportTextToSpeechServiceError()-- try:- return model_instance.get_tts_voices(language)- except Exception as e:- raise e+ def invoke_tts(+ cls,+ app_model: App,+ *,+ text: str,+ voice: str | None,+ end_user: str | None = None,+ ) -> AudioOutput:+ """Invoke the provider using text and voice resolved by preparation."""+ app_ref = AudioAppRef(app_id=app_model.id, tenant_id=app_model.tenant_id, app_mode=app_model.mode.value)+ return audio_provider_gateway.text_to_speech(app=app_ref, text=text.strip(), voice=voice, end_user=end_user)++ @classmethod+ def transcript_tts_voices(cls, tenant_id: str, language: str) -> list[TTSModelVoice]:+ return audio_provider_gateway.get_voices(tenant_id=tenant_id, language=language)diff --git a/api/services/audio_types.py b/api/services/audio_types.pynew file mode 100644index 00000000000..e3c1e88656b--- /dev/null+++ b/api/services/audio_types.py@@ -0,0 +1,26 @@+"""Plain audio values shared by the application and provider boundary."""++from collections.abc import Iterable+from dataclasses import dataclass+from typing import IO+++@dataclass(frozen=True, slots=True)+class AudioAppRef:+ """App identity and mode snapshot; tenant_id identifies the app's owner."""++ app_id: str+ tenant_id: str+ app_mode: str+++@dataclass(frozen=True, slots=True)+class AudioUpload:+ stream: IO[bytes]+ mime_type: str+++@dataclass(frozen=True, slots=True)+class AudioOutput:+ data: Iterable[bytes] | bytes | bytearray | memoryview+ mime_type: str | Nonediff --git a/api/tests/test_containers_integration_tests/services/test_audio_service_db.py b/api/tests/test_containers_integration_tests/services/test_audio_service_db.pyindex ab1be297368..198e8a6e953 100644--- a/api/tests/test_containers_integration_tests/services/test_audio_service_db.py+++ b/api/tests/test_containers_integration_tests/services/test_audio_service_db.py@@ -156,7 +156,7 @@ class TestAudioServiceTranscriptTTSMessageLookup:mock_model_manager = MagicMock()mock_model_manager.get_default_model_instance.return_value = mock_model_instance- with patch("services.audio_service.ModelManager.for_tenant", return_value=mock_model_manager):+ with patch("services.audio_provider_gateway.ModelManager.for_tenant", return_value=mock_model_manager):result = AudioService.transcript_tts(app_model=app,session=db_session_with_containers,diff --git a/api/tests/unit_tests/controllers/common/test_audio_response.py b/api/tests/unit_tests/controllers/common/test_audio_response.pynew file mode 100644index 00000000000..d647c5bbd32--- /dev/null+++ b/api/tests/unit_tests/controllers/common/test_audio_response.py@@ -0,0 +1,183 @@+"""HTTP audio response ownership without changing MIME or request context."""++from collections.abc import Callable, Generator, Iterator+from dataclasses import dataclass, field+from typing import override++import pytest+from flask import Flask, Response, g, has_request_context, request++import controllers.common.audio_response as response_module+from controllers.common.audio_response import audio_binary_response+from graphon.model_runtime.errors.invoke import InvokeBadRequestError+from services.audio_types import AudioOutput++_WAV = b"RIFF\x24\x00\x00\x00WAVE" + b"\x00" * 32+++@dataclass+class _Stream(Iterator[bytes]):+ chunks: Iterator[bytes] = field(default_factory=lambda: iter([_WAV, b"remaining"]))+ close_calls: int = 0+ close_error: Exception | None = None++ @override+ def __next__(self) -> bytes:+ return next(self.chunks)++ def close(self) -> None:+ self.close_calls += 1+ if self.close_error is not None:+ raise self.close_error+++def test_missing_output_preserves_null_response() -> None:+ assert audio_binary_response(None) is None++[email protected]("convert", [bytes, bytearray, memoryview])+def test_binary_response_preserves_content_and_detected_mime(+ convert: Callable[[bytes], bytes | bytearray | memoryview],+) -> None:+ response = audio_binary_response(AudioOutput(data=convert(_WAV), mime_type="audio/x-wav"))++ assert response is not None+ assert response.status_code == 200+ assert response.data == _WAV+ assert dict(response.headers) == {"Content-Type": "audio/wav", "Content-Length": str(len(_WAV))}++[email protected]("consumed_chunks", [0, 1, 2, None])+def test_response_owns_stream_before_consumption_and_after_partial_or_complete_reads(+ consumed_chunks: int | None,+) -> None:+ source = _Stream()+ app = Flask(__name__)+ with app.test_request_context("/audio"):+ response = audio_binary_response(AudioOutput(data=source, mime_type="audio/x-wav"))++ assert response is not None+ assert response.status_code == 200+ assert dict(response.headers) == {"Content-Type": "audio/wav"}+ assert source.close_calls == 0+ try:+ iterator = iter(response.response)+ if consumed_chunks is None:+ assert list(iterator) == [_WAV, b"remaining"]+ assert source.close_calls == 1+ else:+ assert [next(iterator) for _ in range(consumed_chunks)] == [_WAV, b"remaining"][:consumed_chunks]+ assert source.close_calls == 0+ finally:+ response.close()+ response.close()+ assert source.close_calls == 1+ assert not has_request_context()+++def test_http_partial_consumption_preserves_and_releases_request_context() -> None:+ closed: list[tuple[str, str]] = []++ def audio() -> Generator[bytes, None, None]:+ try:+ assert request.path == "/audio"+ assert g.audio_marker == "request marker"+ yield _WAV+ assert request.path == "/audio"+ assert g.audio_marker == "request marker"+ yield b"unconsumed"+ finally:+ closed.append((request.path, g.audio_marker))++ source = audio()+ app = Flask(__name__)++ @app.get("/audio")+ def get_audio() -> Response:+ g.audio_marker = "request marker"+ response = audio_binary_response(AudioOutput(data=source, mime_type=None))+ assert response is not None+ return response++ response = app.test_client().get("/audio")+ assert response.status_code == 200+ assert response.headers["Content-Type"] == "audio/wav"+ assert "Content-Length" not in response.headers+ assert next(iter(response.response)) == _WAV+ assert closed == []+ response.close()+ assert closed == [("/audio", "request marker")]+ assert not has_request_context()++[email protected]("failure", ["mime", "first-chunk"])[email protected]("cleanup_fails", [False, True])+def test_inspection_failure_closes_input_exactly_once_and_preserves_error(+ failure: str,+ cleanup_fails: bool,+) -> None:+ error = InvokeBadRequestError("provider disconnected while reading first chunk")++ def broken_chunks() -> Generator[bytes, None, None]:+ raise error+ yield b"unreachable"++ source = _Stream(+ chunks=iter([_WAV]) if failure == "mime" else broken_chunks(),+ close_error=RuntimeError("cleanup failed") if cleanup_fails else None,+ )+ with pytest.raises(InvokeBadRequestError) as caught:+ audio_binary_response(AudioOutput(data=source, mime_type="audio/mpeg"))++ if failure == "first-chunk":+ assert caught.value is error+ else:+ assert "MIME does not match" in str(caught.value)+ assert source.close_calls == 1+++def test_missing_request_context_closes_owned_stream() -> None:+ source = _Stream()+ assert not has_request_context()+ with pytest.raises(RuntimeError, match="request context is active"):+ audio_binary_response(AudioOutput(data=source, mime_type=None))+ assert source.close_calls == 1+++def test_response_construction_failure_closes_owned_stream(monkeypatch: pytest.MonkeyPatch) -> None:+ source = _Stream()+ error = RuntimeError("HTTP response construction failed")++ def fail_response(*_args: object, **_kwargs: object) -> Response:+ raise error++ monkeypatch.setattr(response_module, "Response", fail_response)+ app = Flask(__name__)+ with app.test_request_context("/audio"):+ with pytest.raises(RuntimeError) as caught:+ audio_binary_response(AudioOutput(data=source, mime_type=None))+ assert caught.value is error+ assert source.close_calls == 1+ assert not has_request_context()+++def test_late_provider_error_closes_stream_and_releases_context() -> None:+ error = RuntimeError("provider disconnected after first chunk")++ def broken_chunks() -> Generator[bytes, None, None]:+ yield _WAV+ raise error++ source = _Stream(chunks=broken_chunks())+ app = Flask(__name__)+ with app.test_request_context("/audio"):+ response = audio_binary_response(AudioOutput(data=source, mime_type=None))+ assert response is not None+ iterator = iter(response.response)+ assert next(iterator) == _WAV+ with pytest.raises(RuntimeError) as caught:+ next(iterator)+ response.close()+ assert caught.value is error+ assert source.close_calls == 1+ assert not has_request_context()diff --git a/api/tests/unit_tests/controllers/console/explore/test_trial.py b/api/tests/unit_tests/controllers/console/explore/test_trial.pyindex 8d96a37e5da..c7b57eb326e 100644--- a/api/tests/unit_tests/controllers/console/explore/test_trial.py+++ b/api/tests/unit_tests/controllers/console/explore/test_trial.py@@ -1,604 +1,6 @@-from inspect import unwrap as inspect_unwrap-from io import BytesIO-from types import SimpleNamespace-from typing import Any-from unittest.mock import MagicMock, patch--import pytest-from flask import Flask, request-import controllers.console.explore.trial as module-from controllers.console.app.error import (- CompletionRequestError,- ProviderModelCurrentlyNotSupportError,- ProviderNotInitializeError,- ProviderQuotaExceededError,- SpeechToTextDisabledError,-)-from controllers.console.explore.trial import TextToSpeechRequest-from core.errors.error import (- ModelCurrentlyNotSupportError,- ProviderTokenNotInitError,- QuotaExceededError,-)-from graphon.model_runtime.errors.invoke import InvokeError-from models import Account-from models.model import App, AppMode-from services.app_ref_service import AppRef, MessageRef-from services.errors.audio import SpeechToTextDisabledServiceError-from tests.unit_tests.model_factories import make_app--unwrap: Any = inspect_unwrap---def account() -> Account:- acc = Account(name="User", email="[email protected]")- acc.id = "u1"- return acc---def trial_app_usage(monkeypatch: pytest.MonkeyPatch) -> MagicMock:- usage = MagicMock()- monkeypatch.setattr(- module,- "application_services",- MagicMock(return_value=SimpleNamespace(trial_app_usage=usage)),- )- return usage---def _app(*, app_id: str, mode: AppMode, tenant_id: str = "tenant-1") -> App:- return make_app(app_id=app_id, tenant_id=tenant_id, name="Trial App", mode=mode, icon_type=None, enable_api=False)---def _file_data() -> Any:- file_data: Any = BytesIO(b"fake audio data")- file_data.filename = "test.wav"- return file_data---def trial_app_chat() -> App:- return _app(app_id="a-chat", mode=AppMode.CHAT)def test_trial_workflow_uses_trial_scoped_simple_account_model() -> None:assert module.simple_account_model.name == "TrialSimpleAccount"assert module.simple_account_model.__schema__["properties"].keys() >= {"id", "name", "email"}---class TestTrialChatAudioApi:- def test_success(- self,- app: Flask,- trial_app_chat: App,- account: Account,- trial_app_usage: MagicMock,- ) -> None:- api = module.TrialChatAudioApi()- method = unwrap(api.post)-- file_data = _file_data()-- with (- app.test_request_context(- "/", method="POST", data={"file": (file_data, "test.wav")}, content_type="multipart/form-data"- ),- patch.object(module.AudioService, "transcript_asr", return_value={"text": "hello"}),- ):- result = method(api, account, trial_app_chat)-- assert result == {"text": "hello"}- trial_app_usage.record.assert_called_once_with(app_id="a-chat", account_id="u1")-- def test_app_config_broken(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatAudioApi()- method = unwrap(api.post)-- file_data = _file_data()-- with (- app.test_request_context(- "/", method="POST", data={"file": (file_data, "test.wav")}, content_type="multipart/form-data"- ),- patch.object(- module.AudioService,- "transcript_asr",- side_effect=module.services.errors.app_model_config.AppModelConfigBrokenError(),- ),- ):- with pytest.raises(module.AppUnavailableError):- method(- api,- account,- trial_app_chat,- )-- def test_no_audio_uploaded(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatAudioApi()- method = unwrap(api.post)-- file_data = _file_data()-- with (- app.test_request_context(- "/", method="POST", data={"file": (file_data, "test.wav")}, content_type="multipart/form-data"- ),- patch.object(- module.AudioService,- "transcript_asr",- side_effect=module.services.errors.audio.NoAudioUploadedServiceError(),- ),- ):- with pytest.raises(module.NoAudioUploadedError):- method(- api,- account,- trial_app_chat,- )-- def test_missing_file_field_returns_400(self, app: Flask, trial_app_chat: App, account: Account) -> None:- """A multipart POST with no `file` field must surface as 400, not 500.-- Verifies the controller passes file=None to AudioService.transcript_asr- instead of raising a KeyError that would yield HTTP 500.- """-- def fake_asr(*args, **kwargs):- assert kwargs["file"] is None- raise module.services.errors.audio.NoAudioUploadedServiceError()-- api = module.TrialChatAudioApi()- method = unwrap(api.post)-- with (- app.test_request_context("/", method="POST", data={}, content_type="multipart/form-data"),- patch.object(module.AudioService, "transcript_asr", side_effect=fake_asr),- ):- with pytest.raises(module.NoAudioUploadedError) as exc_info:- method(- api,- account,- trial_app_chat,- )-- assert exc_info.value.code == 400-- def test_audio_too_large(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatAudioApi()- method = unwrap(api.post)-- file_data = _file_data()-- with (- app.test_request_context(- "/", method="POST", data={"file": (file_data, "test.wav")}, content_type="multipart/form-data"- ),- patch.object(- module.AudioService,- "transcript_asr",- side_effect=module.services.errors.audio.AudioTooLargeServiceError("Too large"),- ),- ):- with pytest.raises(module.AudioTooLargeError):- method(- api,- account,- trial_app_chat,- )-- def test_unsupported_audio_type(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatAudioApi()- method = unwrap(api.post)-- file_data = _file_data()-- with (- app.test_request_context(- "/", method="POST", data={"file": (file_data, "test.wav")}, content_type="multipart/form-data"- ),- patch.object(- module.AudioService,- "transcript_asr",- side_effect=module.services.errors.audio.UnsupportedAudioTypeServiceError(),- ),- ):- with pytest.raises(module.UnsupportedAudioTypeError):- method(- api,- account,- trial_app_chat,- )-- def test_provider_not_support_tts(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatAudioApi()- method = unwrap(api.post)-- file_data = _file_data()-- with (- app.test_request_context(- "/", method="POST", data={"file": (file_data, "test.wav")}, content_type="multipart/form-data"- ),- patch.object(- module.AudioService,- "transcript_asr",- side_effect=module.services.errors.audio.ProviderNotSupportSpeechToTextServiceError(),- ),- ):- with pytest.raises(module.ProviderNotSupportSpeechToTextError):- method(- api,- account,- trial_app_chat,- )-- def test_speech_to_text_disabled(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatAudioApi()- method = unwrap(api.post)- file_data = _file_data()-- with (- app.test_request_context(- "/", method="POST", data={"file": (file_data, "test.wav")}, content_type="multipart/form-data"- ),- patch.object(- module.AudioService,- "transcript_asr",- side_effect=SpeechToTextDisabledServiceError(),- ),- ):- with pytest.raises(SpeechToTextDisabledError):- method(- api,- account,- trial_app_chat,- )-- def test_provider_not_init(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatAudioApi()- method = unwrap(api.post)-- file_data = _file_data()-- with (- app.test_request_context(- "/", method="POST", data={"file": (file_data, "test.wav")}, content_type="multipart/form-data"- ),- patch.object(module.AudioService, "transcript_asr", side_effect=ProviderTokenNotInitError("test")),- ):- with pytest.raises(ProviderNotInitializeError):- method(- api,- account,- trial_app_chat,- )-- def test_quota_exceeded(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatAudioApi()- method = unwrap(api.post)-- file_data = _file_data()-- with (- app.test_request_context(- "/", method="POST", data={"file": (file_data, "test.wav")}, content_type="multipart/form-data"- ),- patch.object(module.AudioService, "transcript_asr", side_effect=QuotaExceededError()),- ):- with pytest.raises(ProviderQuotaExceededError):- method(- api,- account,- trial_app_chat,- )---class TestTrialChatTextApi:- def test_success(- self,- app: Flask,- trial_app_chat: App,- account: Account,- trial_app_usage: MagicMock,- ) -> None:- api = module.TrialChatTextApi()- method = unwrap(api.post)-- with (- app.test_request_context("/", json={"text": "hello", "voice": "en-US"}),- patch.object(module.AudioService, "transcript_tts", return_value={"audio": "base64_data"}),- ):- result = method(- api, TextToSpeechRequest.model_validate(request.get_json(silent=True) or {}), account, trial_app_chat- )-- assert result == {"audio": "base64_data"}- trial_app_usage.record.assert_called_once_with(app_id="a-chat", account_id="u1")-- def test_success_with_message_ref(- self,- app: Flask,- trial_app_chat: App,- account: Account,- trial_app_usage: MagicMock,- ) -> None:- api = module.TrialChatTextApi()- method = unwrap(api.post)- transcript_tts = MagicMock(return_value={"audio": "base64_data"})- trial_app_chat.tenant_id = "tenant-1"-- with (- app.test_request_context("/", json={"text": "hello", "message_id": "message-1"}),- patch.object(module.AudioService, "transcript_tts", transcript_tts),- ):- result = method(- api, TextToSpeechRequest.model_validate(request.get_json(silent=True) or {}), account, trial_app_chat- )-- assert result == {"audio": "base64_data"}- assert transcript_tts.call_args.kwargs["message_ref"] == MessageRef(- AppRef("tenant-1", "a-chat"),- "message-1",- account_id="u1",- )- trial_app_usage.record.assert_called_once_with(app_id="a-chat", account_id="u1")-- def test_app_config_broken(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatTextApi()- method = unwrap(api.post)-- with (- app.test_request_context("/", json={"text": "hello", "voice": "en-US"}),- patch.object(- module.AudioService,- "transcript_tts",- side_effect=module.services.errors.app_model_config.AppModelConfigBrokenError(),- ),- ):- with pytest.raises(module.AppUnavailableError):- method(- api,- TextToSpeechRequest.model_validate(request.get_json(silent=True) or {}),- account,- trial_app_chat,- )-- def test_provider_not_support(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatTextApi()- method = unwrap(api.post)-- with (- app.test_request_context("/", json={"text": "hello", "voice": "en-US"}),- patch.object(- module.AudioService,- "transcript_tts",- side_effect=module.services.errors.audio.ProviderNotSupportSpeechToTextServiceError(),- ),- ):- with pytest.raises(module.ProviderNotSupportSpeechToTextError):- method(- api,- TextToSpeechRequest.model_validate(request.get_json(silent=True) or {}),- account,- trial_app_chat,- )-- def test_audio_too_large(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatTextApi()- method = unwrap(api.post)-- with (- app.test_request_context("/", json={"text": "hello", "voice": "en-US"}),- patch.object(- module.AudioService,- "transcript_tts",- side_effect=module.services.errors.audio.AudioTooLargeServiceError("Too large"),- ),- ):- with pytest.raises(module.AudioTooLargeError):- method(- api,- TextToSpeechRequest.model_validate(request.get_json(silent=True) or {}),- account,- trial_app_chat,- )-- def test_no_audio_uploaded(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatTextApi()- method = unwrap(api.post)-- with (- app.test_request_context("/", json={"text": "hello", "voice": "en-US"}),- patch.object(- module.AudioService,- "transcript_tts",- side_effect=module.services.errors.audio.NoAudioUploadedServiceError(),- ),- ):- with pytest.raises(module.NoAudioUploadedError):- method(- api,- TextToSpeechRequest.model_validate(request.get_json(silent=True) or {}),- account,- trial_app_chat,- )-- def test_provider_not_init(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatTextApi()- method = unwrap(api.post)-- with (- app.test_request_context("/", json={"text": "hello", "voice": "en-US"}),- patch.object(module.AudioService, "transcript_tts", side_effect=ProviderTokenNotInitError("test")),- ):- with pytest.raises(ProviderNotInitializeError):- method(- api,- TextToSpeechRequest.model_validate(request.get_json(silent=True) or {}),- account,- trial_app_chat,- )-- def test_quota_exceeded(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatTextApi()- method = unwrap(api.post)-- with (- app.test_request_context("/", json={"text": "hello", "voice": "en-US"}),- patch.object(module.AudioService, "transcript_tts", side_effect=QuotaExceededError()),- ):- with pytest.raises(ProviderQuotaExceededError):- method(- api,- TextToSpeechRequest.model_validate(request.get_json(silent=True) or {}),- account,- trial_app_chat,- )-- def test_model_not_support(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatTextApi()- method = unwrap(api.post)-- with (- app.test_request_context("/", json={"text": "hello", "voice": "en-US"}),- patch.object(module.AudioService, "transcript_tts", side_effect=ModelCurrentlyNotSupportError()),- ):- with pytest.raises(ProviderModelCurrentlyNotSupportError):- method(- api,- TextToSpeechRequest.model_validate(request.get_json(silent=True) or {}),- account,- trial_app_chat,- )-- def test_invoke_error(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatTextApi()- method = unwrap(api.post)-- with (- app.test_request_context("/", json={"text": "hello", "voice": "en-US"}),- patch.object(module.AudioService, "transcript_tts", side_effect=InvokeError("test error")),- ):- with pytest.raises(CompletionRequestError):- method(- api,- TextToSpeechRequest.model_validate(request.get_json(silent=True) or {}),- account,- trial_app_chat,- )---class TestTrialChatAudioApiExceptionHandlers:- def test_provider_not_init(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatAudioApi()- method = unwrap(api.post)-- file_data = _file_data()-- with (- app.test_request_context(- "/", method="POST", data={"file": (file_data, "test.wav")}, content_type="multipart/form-data"- ),- patch.object(- module.AudioService,- "transcript_asr",- side_effect=ProviderTokenNotInitError("test"),- ),- ):- with pytest.raises(ProviderNotInitializeError):- method(- api,- account,- trial_app_chat,- )-- def test_quota_exceeded(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatAudioApi()- method = unwrap(api.post)-- file_data = _file_data()-- with (- app.test_request_context(- "/", method="POST", data={"file": (file_data, "test.wav")}, content_type="multipart/form-data"- ),- patch.object(- module.AudioService,- "transcript_asr",- side_effect=QuotaExceededError(),- ),- ):- with pytest.raises(ProviderQuotaExceededError):- method(- api,- account,- trial_app_chat,- )-- def test_invoke_error(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatAudioApi()- method = unwrap(api.post)-- file_data = _file_data()-- with (- app.test_request_context(- "/", method="POST", data={"file": (file_data, "test.wav")}, content_type="multipart/form-data"- ),- patch.object(- module.AudioService,- "transcript_asr",- side_effect=InvokeError("test error"),- ),- ):- with pytest.raises(CompletionRequestError):- method(- api,- account,- trial_app_chat,- )---class TestTrialChatTextApiExceptionHandlers:- def test_app_config_broken(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatTextApi()- method = unwrap(api.post)-- with (- app.test_request_context("/", json={"text": "hello", "voice": "en-US"}),- patch.object(- module.AudioService,- "transcript_tts",- side_effect=module.services.errors.app_model_config.AppModelConfigBrokenError(),- ),- ):- with pytest.raises(module.AppUnavailableError):- method(- api,- TextToSpeechRequest.model_validate(request.get_json(silent=True) or {}),- account,- trial_app_chat,- )-- def test_unsupported_audio_type(self, app: Flask, trial_app_chat: App, account: Account) -> None:- api = module.TrialChatTextApi()- method = unwrap(api.post)-- with (- app.test_request_context("/", json={"text": "hello", "voice": "en-US"}),- patch.object(- module.AudioService,- "transcript_tts",- side_effect=module.services.errors.audio.UnsupportedAudioTypeServiceError("test"),- ),- ):- with pytest.raises(module.UnsupportedAudioTypeError):- method(- api,- TextToSpeechRequest.model_validate(request.get_json(silent=True) or {}),- account,- trial_app_chat,- )diff --git a/api/tests/unit_tests/controllers/console/explore/test_trial_audio.py b/api/tests/unit_tests/controllers/console/explore/test_trial_audio.pynew file mode 100644index 00000000000..057abae7872--- /dev/null+++ b/api/tests/unit_tests/controllers/console/explore/test_trial_audio.py@@ -0,0 +1,474 @@+"""Trial audio admission, response and usage contracts through HTTP and SQLite."""++from collections.abc import Callable, Iterator, Mapping+from dataclasses import dataclass, field+from io import BytesIO+from typing import override+from uuid import uuid4++import pytest+from flask import Flask, Request+from sqlalchemy import Connection, event, select+from sqlalchemy.orm import Mapper, Session, SessionTransaction, sessionmaker+from werkzeug.exceptions import Unauthorized+from werkzeug.test import TestResponse++import controllers.console.explore.trial as trial_module+import controllers.console.explore.trial_app_admission as admission_module+import controllers.console.wraps as console_wraps+import libs.login as login_module+from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError+from enums import DeploymentEdition+from extensions.ext_login import DifyLoginManager, unauthorized_handler+from graphon.model_runtime.errors.invoke import InvokeBadRequestError, InvokeError+from libs.external_api import ExternalApi+from models import Account, AccountTrialAppRecord, App, AppMode, Tenant, TrialApp+from models.account import AccountStatus+from repositories.trial_app_repository import TrialAppRepository+from services.app_definition_query_service import AppDefinitionUnavailableError+from services.audio_types import AudioAppRef, AudioOutput, AudioUpload+from services.errors.app_model_config import AppModelConfigBrokenError+from services.errors.audio import (+ AudioTooLargeServiceError,+ NoAudioUploadedServiceError,+ ProviderNotSupportSpeechToTextServiceError,+ SpeechToTextDisabledServiceError,+ UnsupportedAudioTypeServiceError,+)+from services.trial_app_access_service import TrialAppAccessService++_ASR = "audio-to-text"+_TTS = "text-to-audio"+_WAV = b"RIFF\x24\x00\x00\x00WAVEfmt " + b"a" * 32+++@dataclass+class _FeatureState:+ enabled: bool = True+ setup_completed: bool = True+ events: list[str] = field(default_factory=list)++ def is_trial_enabled(self) -> bool:+ self.events.append("feature")+ return self.enabled+++@dataclass(frozen=True)+class _AsrCall:+ app: AudioAppRef+ content: bytes | None+ mime_type: str | None+++@dataclass(frozen=True)+class _TtsCall:+ app: AudioAppRef+ account_id: str+ text: str | None+ voice: str | None+ message_id: str | None+++@dataclass+class _Stream(Iterator[bytes]):+ chunks: Iterator[bytes] = field(default_factory=lambda: iter([b"a" * 32, b"remaining-audio"]))+ reads: int = 0+ close_calls: int = 0+ close_error: Exception | None = None++ @override+ def __next__(self) -> bytes:+ chunk = next(self.chunks)+ self.reads += 1+ return chunk++ def close(self) -> None:+ self.close_calls += 1+ if self.close_error is not None:+ raise self.close_error+++@dataclass+class _Runtime:+ sessions: list[Session]+ output: AudioOutput | None = field(default_factory=lambda: AudioOutput(data=_WAV, mime_type=None))+ error: Exception | None = None+ calls: list[_AsrCall | _TtsCall] = field(default_factory=list)++ def _assert_admission_closed(self) -> None:+ assert self.sessions+ assert all(not session.in_transaction() and not session.identity_map for session in self.sessions)++ def transcript_asr(self, *, app: AudioAppRef, audio: AudioUpload | None) -> dict[str, str]:+ self._assert_admission_closed()+ self.calls.append(_AsrCall(app, audio.stream.read() if audio else None, audio.mime_type if audio else None))+ if self.error is not None:+ raise self.error+ if audio is None:+ raise NoAudioUploadedServiceError()+ return {"text": " transcript "}++ def transcript_tts(+ self,+ *,+ app: AudioAppRef,+ account_id: str,+ text: str | None,+ voice: str | None,+ message_id: str | None,+ ) -> AudioOutput | None:+ self._assert_admission_closed()+ self.calls.append(_TtsCall(app, account_id, text, voice, message_id))+ if self.error is not None:+ raise self.error+ return self.output+++@dataclass(frozen=True)+class _ApplicationServices:+ trial_app_access: TrialAppAccessService+ trial_app_usage: TrialAppRepository+ app_audio: _Runtime+ recommended_app_queries: _FeatureState+++@dataclass(frozen=True)+class _Harness:+ app: Flask+ account: Account+ target: App+ trial: TrialApp+ factory: sessionmaker[Session]+ runtime: _Runtime+ state: _FeatureState++ def post(self, endpoint: str, *, payload: Mapping[str, object] | None = None) -> TestResponse:+ client = self.app.test_client()+ url = f"/trial-apps/{self.target.id}/{endpoint}"+ if endpoint == _ASR:+ return client.post(url, data={"file": (BytesIO(b"audio input"), "recording.m4a", "audio/x-m4a")})+ return client.post(url, json=dict(payload) if payload is not None else {"text": "Hello"})++ def usage(self) -> int | None:+ with self.factory() as session:+ return session.scalar(+ select(AccountTrialAppRecord.count).where(+ AccountTrialAppRecord.app_id == self.target.id,+ AccountTrialAppRecord.account_id == self.account.id,+ )+ )+++def harness(+ monkeypatch: pytest.MonkeyPatch,+ config_overrides: Callable[..., None],+ sqlite_session_factory: sessionmaker[Session],+) -> _Harness:+ config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY, INIT_PASSWORD="", LOGIN_DISABLED=False)+ state = _FeatureState()+ account = Account(name="Viewer", email="[email protected]")+ account.status = AccountStatus.ACTIVE+ account._current_tenant = Tenant(name="Viewer workspace")+ target = App(+ id=str(uuid4()), tenant_id=str(uuid4()), name="Trial app", mode=AppMode.CHAT, enable_site=True, enable_api=True+ )+ trial = TrialApp(app_id=target.id, tenant_id=target.tenant_id, trial_limit=3)+ with sqlite_session_factory.begin() as session:+ session.add_all([target, trial])++ sessions: list[Session] = []+ repository_factory = sessionmaker(bind=sqlite_session_factory.kw["bind"], expire_on_commit=False)++ @event.listens_for(repository_factory, "after_begin")+ def track_session(session: Session, _transaction: SessionTransaction, _connection: Connection) -> None:+ sessions.append(session)++ runtime = _Runtime(sessions=sessions)+ repository = TrialAppRepository(session_factory=repository_factory)+ services = _ApplicationServices(TrialAppAccessService(apps=repository), repository, runtime, state)++ def setup_completed() -> bool:+ state.events.append("setup")+ return state.setup_completed++ def csrf(_request: Request, account_id: str) -> None:+ assert account_id == account.id+ state.events.append("csrf")++ for module in (trial_module, admission_module):+ monkeypatch.setattr(module, "application_services", lambda: services)+ monkeypatch.setattr(console_wraps, "_is_setup_completed", setup_completed)+ monkeypatch.setattr(login_module, "current_user", account)+ monkeypatch.setattr(login_module, "check_csrf_token", csrf)+ app = Flask(__name__)+ app.config.update(TESTING=True, RESTX_ERROR_404_HELP=False)+ login_manager = DifyLoginManager()+ login_manager.init_app(app)+ login_manager.unauthorized_handler(unauthorized_handler)+ api = ExternalApi(app)+ api.add_resource(trial_module.TrialChatAudioApi, "/trial-apps/<uuid:app_id>/audio-to-text")+ api.add_resource(trial_module.TrialChatTextApi, "/trial-apps/<uuid:app_id>/text-to-audio")+ return _Harness(app, account, target, trial, sqlite_session_factory, runtime, state)+++def _assert_error(response: TestResponse, status: int, code: str) -> Mapping[str, object]:+ assert response.status_code == status+ body = response.get_json()+ assert isinstance(body, dict)+ assert body["code"] == code+ assert body["status"] == status+ assert response.headers["Content-Type"] == "application/json"+ assert isinstance(body["message"], str)+ assert body["message"]+ return body+++def test_asr_upload_keeps_transcript_and_owner_tenant(harness: _Harness) -> None:+ response = harness.post(_ASR)++ assert response.status_code == 200+ assert response.get_json() == {"text": " transcript "}+ assert response.headers["Content-Type"] == "application/json"+ assert int(response.headers["Content-Length"]) == len(response.data)+ assert harness.runtime.calls == [+ _AsrCall(AudioAppRef(harness.target.id, harness.target.tenant_id, "chat"), b"audio input", "audio/x-m4a")+ ]+ assert harness.target.tenant_id != harness.account.current_tenant_id+ assert harness.state.events == ["setup", "csrf", "feature"]+ assert harness.usage() == 1++[email protected]("streaming", [None, False, True])+def test_tts_preserves_payload_actor_and_binary_response_ignoring_streaming_flag(+ harness: _Harness, streaming: bool | None+) -> None:+ message_id = str(uuid4())+ response = harness.post(+ _TTS, payload={"text": " hello ", "voice": "voice-1", "message_id": message_id, "streaming": streaming}+ )++ assert response.status_code == 200+ assert response.data == _WAV+ assert response.headers["Content-Type"] == "audio/wav"+ assert int(response.headers["Content-Length"]) == len(_WAV)+ assert harness.runtime.calls == [+ _TtsCall(+ AudioAppRef(harness.target.id, harness.target.tenant_id, "chat"),+ harness.account.id,+ " hello ",+ "voice-1",+ message_id,+ )+ ]+ assert harness.usage() == 1++[email protected]("null_fields", [False, True])+def test_missing_message_null_response_still_records_usage(harness: _Harness, null_fields: bool) -> None:+ harness.runtime.output = None+ payload: dict[str, object] = {"text": None, "voice": None, "message_id": None} if null_fields else {}+ response = harness.post(_TTS, payload=payload)++ assert response.status_code == 200+ assert response.get_json() is None+ assert response.headers["Content-Type"] == "application/json"+ assert harness.runtime.calls == [+ _TtsCall(AudioAppRef(harness.target.id, harness.target.tenant_id, "chat"), harness.account.id, None, None, None)+ ]+ assert harness.usage() == 1+++def test_missing_upload_passes_none_and_does_not_record_usage(harness: _Harness) -> None:+ response = harness.app.test_client().post(f"/trial-apps/{harness.target.id}/{_ASR}", data={})++ _assert_error(response, 400, "no_audio_uploaded")+ assert harness.runtime.calls == [+ _AsrCall(AudioAppRef(harness.target.id, harness.target.tenant_id, "chat"), None, None)+ ]+ assert harness.usage() is None++[email protected]("endpoint", [_ASR, _TTS])+ ("error", "status", "code"),+ [+ (AppDefinitionUnavailableError("app removed"), 400, "app_unavailable"),+ (AppModelConfigBrokenError(), 400, "app_unavailable"),+ (NoAudioUploadedServiceError(), 400, "no_audio_uploaded"),+ (AudioTooLargeServiceError("Audio size larger than 30 mb"), 413, "audio_too_large"),+ (UnsupportedAudioTypeServiceError(), 415, "unsupported_audio_type"),+ (ProviderNotSupportSpeechToTextServiceError(), 400, "provider_not_support_speech_to_text"),+ (SpeechToTextDisabledServiceError(), 400, "speech_to_text_disabled"),+ (ProviderTokenNotInitError("provider key missing"), 400, "provider_not_initialize"),+ (QuotaExceededError(), 400, "provider_quota_exceeded"),+ (ModelCurrentlyNotSupportError(), 400, "model_currently_not_support"),+ (InvokeError("provider rejected audio"), 400, "completion_request_error"),+ (ValueError("Text is required"), 400, "invalid_param"),+ (Unauthorized("session expired"), 401, "unauthorized"),+ (RuntimeError("private provider failure"), 500, "internal_server_error"),+ ],+)+def test_runtime_errors_keep_specific_codes_without_recording_usage(+ harness: _Harness, endpoint: str, error: Exception, status: int, code: str+) -> None:+ harness.runtime.error = error+ response = harness.post(endpoint)++ body = _assert_error(response, status, code)+ if isinstance(error, (ProviderTokenNotInitError, InvokeError)):+ assert body["message"] == error.description+ elif code in {"audio_too_large", "invalid_param"}:+ assert str(error) in str(body["message"])+ assert harness.usage() is None++[email protected]("field", ["text", "voice", "message_id", "streaming"])+def test_invalid_tts_payload_keeps_field_context(harness: _Harness, field: str) -> None:+ response = harness.post(_TTS, payload={field: ["invalid"]})++ body = _assert_error(response, 422, "unprocessable_entity")+ assert field in str(body)+ assert harness.runtime.calls == []+ assert harness.usage() is None++[email protected]("endpoint", [_ASR, _TTS])+ ("gate", "status", "code"),+ [+ ("setup", 401, "not_setup"),+ ("login", 401, "unauthorized"),+ ("csrf", 401, "unauthorized"),+ ("initialization", 400, "account_not_initialized"),+ ("feature", 403, "trial_app_feature_disabled"),+ ],+)+def test_console_and_trial_gates_run_before_app_lookup(+ harness: _Harness, monkeypatch: pytest.MonkeyPatch, endpoint: str, gate: str, status: int, code: str+) -> None:+ if gate == "setup":+ harness.state.setup_completed = False+ elif gate == "login":+ monkeypatch.setattr(login_module, "current_user", None)+ elif gate == "csrf":++ def reject_csrf(_request: Request, _account_id: str) -> None:+ raise Unauthorized("invalid csrf token")++ monkeypatch.setattr(login_module, "check_csrf_token", reject_csrf)+ elif gate == "initialization":+ harness.account.status = AccountStatus.UNINITIALIZED+ else:+ harness.state.enabled = False+ response = harness.post(endpoint)++ if gate == "login":+ assert response.status_code == 401+ assert response.get_json() == {"code": "unauthorized", "message": "Unauthorized."}+ assert response.headers["Content-Type"] == "application/json"+ else:+ _assert_error(response, status, code)+ assert harness.runtime.calls == []+ assert harness.runtime.sessions == []+ assert harness.usage() is None++[email protected]("endpoint", [_ASR, _TTS])[email protected]("gate", ["membership", "missing-app", "quota"])+def test_trial_membership_and_account_quota_block_audio(harness: _Harness, endpoint: str, gate: str) -> None:+ with harness.factory.begin() as session:+ if gate == "membership":+ trial = session.get(TrialApp, harness.trial.id)+ assert trial is not None+ session.delete(trial)+ elif gate == "missing-app":+ app = session.get(App, harness.target.id)+ assert app is not None+ session.delete(app)+ else:+ session.add(AccountTrialAppRecord(app_id=harness.target.id, account_id=harness.account.id, count=3))+ response = harness.post(endpoint)++ _assert_error(response, 403, "trial_app_limit_exceeded" if gate == "quota" else "trial_app_not_allowed")+ assert harness.runtime.calls == []+ assert harness.usage() == (3 if gate == "quota" else None)++[email protected]("endpoint", [_ASR, _TTS])+def test_usage_is_isolated_to_current_account(harness: _Harness, endpoint: str) -> None:+ other_id = str(uuid4())+ with harness.factory.begin() as session:+ session.add_all(+ [+ AccountTrialAppRecord(app_id=harness.target.id, account_id=harness.account.id, count=1),+ AccountTrialAppRecord(app_id=harness.target.id, account_id=other_id, count=99),+ ]+ )+ response = harness.post(endpoint)++ assert response.status_code == 200+ assert harness.usage() == 2+ with harness.factory() as session:+ assert (+ session.scalar(select(AccountTrialAppRecord.count).where(AccountTrialAppRecord.account_id == other_id))+ == 99+ )++[email protected]("failure", ["mime", "first-chunk"])+def test_stream_preparation_failure_closes_provider_without_consuming_trial(harness: _Harness, failure: str) -> None:+ def broken_chunks() -> Iterator[bytes]:+ raise InvokeBadRequestError("first audio chunk failed")+ yield b"unreachable"++ stream = _Stream(chunks=iter([_WAV]) if failure == "mime" else broken_chunks())+ harness.runtime.output = AudioOutput(data=stream, mime_type="audio/mpeg")+ response = harness.post(_TTS)++ body = _assert_error(response, 400, "completion_request_error")+ assert ("MIME" if failure == "mime" else "first audio chunk failed") in str(body["message"])+ assert stream.close_calls == 1+ assert harness.usage() is None++[email protected]("cleanup_fails", [False, True])+def test_usage_write_failure_closes_stream_without_hiding_database_error(+ harness: _Harness, cleanup_fails: bool, caplog: pytest.LogCaptureFixture+) -> None:+ stream = _Stream(close_error=RuntimeError("cleanup failed") if cleanup_fails else None)+ harness.runtime.output = AudioOutput(data=stream, mime_type="audio/mpeg")+ failure = RuntimeError("usage database unavailable")++ def reject_usage_insert(+ _mapper: Mapper[AccountTrialAppRecord], _connection: Connection, _record: AccountTrialAppRecord+ ) -> None:+ raise failure++ event.listen(AccountTrialAppRecord, "before_insert", reject_usage_insert)+ try:+ response = harness.post(_TTS)+ finally:+ event.remove(AccountTrialAppRecord, "before_insert", reject_usage_insert)++ _assert_error(response, 500, "internal_server_error")+ assert stream.reads == 1+ assert stream.close_calls == 1+ assert harness.usage() is None+ assert any(record.exc_info is not None and record.exc_info[1] is failure for record in caplog.records)+++def test_partial_http_consumption_closes_provider_stream(harness: _Harness) -> None:+ stream = _Stream()+ harness.runtime.output = AudioOutput(data=stream, mime_type="audio/mpeg")+ response = harness.post(_TTS)++ assert response.status_code == 200+ assert response.headers["Content-Type"] == "audio/mpeg"+ assert "Content-Length" not in response.headers+ assert next(iter(response.response)) == b"a" * 32+ assert stream.reads == 1+ assert stream.close_calls == 0+ response.close()+ assert stream.close_calls == 1+ assert harness.usage() == 1diff --git a/api/tests/unit_tests/controllers/console/explore/test_wraps.py b/api/tests/unit_tests/controllers/console/explore/test_wraps.pyindex a285ec582c8..5b1113a605d 100644--- a/api/tests/unit_tests/controllers/console/explore/test_wraps.py+++ b/api/tests/unit_tests/controllers/console/explore/test_wraps.py@@ -10,18 +10,14 @@ import models.model as model_modulefrom controllers.console.explore.error import (AppAccessDeniedError,TrialAppFeatureDisabledError,- TrialAppLimitExceeded,- TrialAppNotAllowed,)from controllers.console.explore.trial_app_admission import trial_feature_enablefrom controllers.console.explore.wraps import (InstalledAppResource,- TrialAppResource,installed_app_required,- trial_app_required,user_allowed_to_access_app,)-from models import Account, AccountTrialAppRecord, App, InstalledApp, TrialApp+from models import Account, App, InstalledAppfrom tests.unit_tests.model_factories import make_account, make_app@@ -172,76 +168,6 @@ def test_user_allowed_to_access_app_success():assert view(installed_app) == "ok"[email protected]("sqlite_session", [(TrialApp, App, AccountTrialAppRecord)], indirect=True)-def test_trial_app_required_not_allowed(- monkeypatch: pytest.MonkeyPatch,- sqlite_session: Session,-):- _bind_database(monkeypatch, sqlite_session)-- @trial_app_required- def view(app):- return "ok"-- with patch(- "controllers.console.explore.wraps.current_account_with_tenant",- return_value=(_account(account_id=str(uuid4())), None),- ):- with pytest.raises(TrialAppNotAllowed):- view(str(uuid4()))--[email protected]("sqlite_session", [(TrialApp, App, AccountTrialAppRecord)], indirect=True)-def test_trial_app_required_limit_exceeded(- monkeypatch: pytest.MonkeyPatch,- sqlite_session: Session,-):- account_id = str(uuid4())- app = _app()- trial_app = TrialApp(app_id=app.id, tenant_id=app.tenant_id, trial_limit=1)- record = AccountTrialAppRecord(account_id=account_id, app_id=app.id, count=1)- sqlite_session.add_all([app, trial_app, record])- sqlite_session.commit()- _bind_database(monkeypatch, sqlite_session)-- @trial_app_required- def view(app):- return "ok"-- with patch(- "controllers.console.explore.wraps.current_account_with_tenant",- return_value=(_account(account_id=account_id), None),- ):- with pytest.raises(TrialAppLimitExceeded):- view(app.id)--[email protected]("sqlite_session", [(TrialApp, App, AccountTrialAppRecord)], indirect=True)-def test_trial_app_required_success(- monkeypatch: pytest.MonkeyPatch,- sqlite_session: Session,-):- account_id = str(uuid4())- app = _app()- trial_app = TrialApp(app_id=app.id, tenant_id=app.tenant_id, trial_limit=2)- record = AccountTrialAppRecord(account_id=account_id, app_id=app.id, count=1)- sqlite_session.add_all([app, trial_app, record])- sqlite_session.commit()- _bind_database(monkeypatch, sqlite_session)-- @trial_app_required- def view(app):- return app-- with patch(- "controllers.console.explore.wraps.current_account_with_tenant",- return_value=(_account(account_id=account_id), None),- ):- result = view(app.id)-- assert result.id == app.id--def test_trial_feature_enable_disabled() -> None:@trial_feature_enabledef view() -> str:@@ -274,12 +200,3 @@ def test_trial_feature_enable_enabled() -> None:def test_installed_app_resource_decorators():decorators = InstalledAppResource.method_decoratorsassert len(decorators) == 4---def test_trial_app_resource_decorators():- assert TrialAppResource.method_decorators == [- trial_app_required,- trial_feature_enable,- wraps_module.account_initialization_required,- wraps_module.login_required,- ]diff --git a/api/tests/unit_tests/core/base/tts/test_audio_mime.py b/api/tests/unit_tests/core/base/tts/test_audio_mime.pyindex f2ffe15f6c8..53d40d21e8d 100644--- a/api/tests/unit_tests/core/base/tts/test_audio_mime.py+++ b/api/tests/unit_tests/core/base/tts/test_audio_mime.py@@ -1,5 +1,6 @@+from collections.abc import Generator, Iteratorfrom types import SimpleNamespace-from typing import cast+from typing import cast, overrideimport pytest@@ -64,3 +65,148 @@ def test_id3_metadata_is_not_treated_as_proof_of_an_mp3_codec() -> None:assert sniff_audio_mime_type(signature) is Noneassert resolve_audio_mime_type(signature, reported_mime_type="audio/aac") == "audio/aac"+++class _TrackedStream(Iterator[bytes]):+ def __init__(self, chunks: Iterator[bytes], *, close_error: Exception | None = None) -> None:+ self._chunks: Iterator[bytes] = chunks+ self._close_error: Exception | None = close_error+ self.close_calls: int = 0++ @override+ def __next__(self) -> bytes:+ return next(self._chunks)++ def close(self) -> None:+ self.close_calls += 1+ if self._close_error is not None:+ raise self._close_error++[email protected]("consumed_chunks", [0, 1, 2, 3])+def test_inspected_stream_closes_source_once_even_before_consumption(consumed_chunks: int) -> None:+ chunks = [b"x" * 32, b"middle", b"last"]+ source = _TrackedStream(iter(chunks))+ stream, mime_type = inspect_audio_stream(source)++ assert mime_type == "audio/mpeg"+ assert [next(stream) for _ in range(consumed_chunks)] == chunks[:consumed_chunks]+ assert source.close_calls == 0+ stream.close()+ stream.close()++ assert source.close_calls == 1+ assert list(stream) == []++[email protected]("chunks", [[], [b"short"], [b"x" * 32, b"last"]])+def test_exhausting_inspected_stream_closes_source(chunks: list[bytes]) -> None:+ source = _TrackedStream(iter(chunks))+ stream, _ = inspect_audio_stream(source)++ assert list(stream) == chunks+ assert source.close_calls == 1+ stream.close()+ assert source.close_calls == 1+++def test_closing_inspected_stream_runs_provider_finally_without_gc() -> None:+ released: list[bool] = []++ def provider() -> Generator[bytes, None, None]:+ try:+ yield b"x" * 32+ yield b"unread"+ finally:+ released.append(True)++ source = provider()+ stream, _ = inspect_audio_stream(source)+ assert released == []++ stream.close()++ assert released == [True]+ assert list(source) == []++[email protected]("after_prefix", [False, True])+def test_provider_error_closes_source_without_being_replaced_by_cleanup_error(+ after_prefix: bool, caplog: pytest.LogCaptureFixture+) -> None:+ provider_error = RuntimeError("provider interrupted")++ def provider() -> Generator[bytes, None, None]:+ if after_prefix:+ yield b"x" * 32+ raise provider_error++ source = _TrackedStream(provider(), close_error=RuntimeError("cleanup failed"))+ with pytest.raises(RuntimeError) as raised:+ list(inspect_audio_stream(source)[0])++ assert raised.value is provider_error+ assert source.close_calls == 1+ assert "cleanup failed" in caplog.text+++ "chunks",+ [+ [TTSAudioChunk(b"x", "application/octet-stream")],+ [TTSAudioChunk(b"RIFF\x24\x00\x00\x00WAVEfmt ", "audio/mpeg")],+ [TTSAudioChunk(b"x", "audio/mpeg"), TTSAudioChunk(b"y", "audio/ogg")],+ [b"x" * 32, TTSAudioChunk(b"later", "audio/ogg")],+ ],+)+def test_mime_rejection_closes_provider_during_peek_or_later(chunks: list[bytes]) -> None:+ source = _TrackedStream(iter(chunks))++ with pytest.raises(InvokeBadRequestError):+ list(inspect_audio_stream(source)[0])++ assert source.close_calls == 1+++def test_closing_inspected_stream_closes_distinct_iterable_owner_and_iterator() -> None:+ iterator = _TrackedStream(iter([b"x" * 32, b"remaining"]))++ class ProviderResponse:+ def __init__(self) -> None:+ self.close_calls: int = 0++ def __iter__(self) -> Iterator[bytes]:+ return iterator++ def close(self) -> None:+ self.close_calls += 1++ source = ProviderResponse()+ stream, _ = inspect_audio_stream(source)++ stream.close()+ stream.close()++ assert iterator.close_calls == 1+ assert source.close_calls == 1+++def test_iterator_creation_failure_closes_provider_response() -> None:+ error = RuntimeError("cannot start reading")++ class ProviderResponse:+ def __init__(self) -> None:+ self.closed: bool = False++ def __iter__(self) -> Iterator[bytes]:+ raise error++ def close(self) -> None:+ self.closed = True++ source = ProviderResponse()+ with pytest.raises(RuntimeError) as raised:+ inspect_audio_stream(source)++ assert raised.value is error+ assert source.closeddiff --git a/api/tests/unit_tests/extensions/test_ext_application_services.py b/api/tests/unit_tests/extensions/test_ext_application_services.pyindex 59540ab86fe..0e502ec7a9f 100644--- a/api/tests/unit_tests/extensions/test_ext_application_services.py+++ b/api/tests/unit_tests/extensions/test_ext_application_services.py@@ -1,6 +1,7 @@"""Tests for application-service dependency wiring."""import json+from io import BytesIOfrom types import SimpleNamespacefrom typing import castfrom unittest.mock import MagicMock, call, patch@@ -20,7 +21,7 @@ from extensions.ext_redis import RedisClientWrapperfrom machinery.context import RequestContextfrom models.account import Accountfrom models.enums import CustomizeTokenStrategy-from models.model import AccountTrialAppRecord, App, DifySetup, Site, TrialApp+from models.model import AccountTrialAppRecord, App, AppModelConfig, DifySetup, Site, TrialAppfrom repositories.account_activation_repository import SQLAlchemyAccountActivationRepositoryfrom repositories.account_integration_repository import SQLAlchemyAccountIntegrationRepositoryfrom repositories.account_oauth_repository import (@@ -40,7 +41,7 @@ from repositories.sqlalchemy_api_workflow_run_repository import DifyAPISQLAlchemfrom repositories.upload_file_delivery_repository import UploadFileDeliveryQueryRepositoryfrom repositories.workflow_app_log_query_repository import WorkflowAppLogQueryRepositoryfrom repositories.workflow_run_archive_repository import WorkflowRunArchiveBundleQueryRepository-from services import account_forgot_password_service, recommended_app_catalog_gateway+from services import account_forgot_password_service, audio_provider_gateway, recommended_app_catalog_gatewayfrom services.account_adapters import (BillingAccountActivationEligibility,BillingWorkspaceMembershipCache,@@ -69,6 +70,7 @@ from services.app_preview_query_service import AppPreviewRef, AppPreviewUnavailafrom services.app_site_service import AppSiteServicefrom services.app_tracing_config_gateway import OpsTraceManagerGatewayfrom services.app_tracing_config_service import AppTracingConfigService+from services.audio_types import AudioAppRef, AudioOutput, AudioUploadfrom services.auth.data_source_api_key_auth_service import DataSourceApiKeyAuthServicefrom services.billing_portal_service import BillingPortalServicefrom services.billing_service import BillingService@@ -739,6 +741,60 @@ def test_trial_generation_uses_configured_access_runtime_and_usage(assert record.count == 1+def test_app_audio_uses_the_configured_database_and_app_owner(+ sqlite_session_factory: sessionmaker[Session], monkeypatch: pytest.MonkeyPatch+) -> None:+ services = ext_application_services.build_application_services(+ database_client=sqlite_session_factory,+ deployment_edition=DeploymentEdition.COMMUNITY,+ initialization_password="",+ redis=MagicMock(spec=RedisClientWrapper),+ )+ app_id, tenant_id, account_id = str(uuid4()), str(uuid4()), str(uuid4())+ with sqlite_session_factory.begin() as session:+ config = AppModelConfig(app_id=app_id, speech_to_text='{"enabled":true}')+ session.add(config)+ session.flush()+ session.add(+ App(+ id=app_id,+ tenant_id=tenant_id,+ name="Trial",+ mode="chat",+ app_model_config_id=config.id,+ enable_site=True,+ enable_api=False,+ )+ )+ session.add(TrialApp(app_id=app_id, tenant_id=tenant_id))++ def transcribe(*, app: AudioAppRef, content: bytes, end_user: str | None) -> str:+ assert app == AudioAppRef(app_id=app_id, tenant_id=tenant_id, app_mode="chat")+ assert content == b"audio"+ assert end_user is None+ return "transcript"++ def synthesize(*, app: AudioAppRef, text: str, voice: str | None, end_user: str | None) -> AudioOutput:+ assert app == AudioAppRef(app_id=app_id, tenant_id=tenant_id, app_mode="chat")+ assert (text, voice, end_user) == ("read", "voice", None)+ return AudioOutput(data=b"audio", mime_type="audio/mpeg")++ monkeypatch.setattr(audio_provider_gateway, "speech_to_text", transcribe)+ monkeypatch.setattr(audio_provider_gateway, "text_to_speech", synthesize)+ admitted = services.trial_app_access.get_access(app_id=app_id, account_id=account_id)+ assert services.app_audio.transcript_asr(+ app=AudioAppRef(admitted.app_id, admitted.tenant_id, admitted.app_mode),+ audio=AudioUpload(stream=BytesIO(b"audio"), mime_type="audio/mp3"),+ ) == {"text": "transcript"}+ assert services.app_audio.transcript_tts(+ app=AudioAppRef(admitted.app_id, admitted.tenant_id, admitted.app_mode),+ account_id=account_id,+ text=" read ",+ voice="voice",+ message_id=None,+ ) == AudioOutput(data=b"audio", mime_type="audio/mpeg")++def test_app_previews_use_the_configured_catalog_and_app_owner(sqlite_session_factory: sessionmaker[Session], monkeypatch: pytest.MonkeyPatch) -> None:diff --git a/api/tests/unit_tests/services/test_app_audio_adapters.py b/api/tests/unit_tests/services/test_app_audio_adapters.pynew file mode 100644index 00000000000..9dbe7d0c985--- /dev/null+++ b/api/tests/unit_tests/services/test_app_audio_adapters.py@@ -0,0 +1,339 @@+import io+import json+from collections.abc import Iterator+from dataclasses import dataclass, field, replace+from decimal import Decimal+from typing import cast, override+from uuid import uuid4++import pytest+from sqlalchemy import Engine, select+from sqlalchemy.orm import Session, sessionmaker+from sqlalchemy.pool import QueuePool++from models import App, AppMode, AppModelConfig, Message+from models.agent import Agent, AgentConfigSnapshot, AgentScope, AgentSource+from models.agent_config_entities import AgentSoulConfig+from models.enums import ConversationFromSource, MessageStatus+from models.model import AccountTrialAppRecord+from models.workflow import Workflow, WorkflowType+from services import audio_provider_gateway+from services.app_audio_adapters import AppAudioRuntime+from services.app_definition_query_service import AppDefinitionUnavailableError+from services.audio_types import AudioAppRef, AudioOutput, AudioUpload+from services.errors.audio import NoAudioUploadedServiceError, SpeechToTextDisabledServiceError++_ACCOUNT_ID = "11111111-1111-4111-8111-111111111111"+++@dataclass+class _Harness:+ runtime: AppAudioRuntime+ app: AudioAppRef+ config_id: str+ message_id: str+ engine: Engine+ closed_sessions: list[Session]+ asr_calls: list[tuple[AudioAppRef, bytes, str | None]] = field(default_factory=list)+ tts_calls: list[tuple[AudioAppRef, str, str | None, str | None]] = field(default_factory=list)+ output: AudioOutput = field(default_factory=lambda: AudioOutput(data=b"audio", mime_type="audio/wav"))++ def assert_database_closed(self) -> None:+ assert self.closed_sessions+ assert all(not session.in_transaction() and not session.identity_map for session in self.closed_sessions)+ assert cast(QueuePool, self.engine.pool).checkedout() == 0++ def tts(+ self, *, text: str | None = " Text input ", voice: str | None = None, message_id: str | None = None+ ) -> AudioOutput | None:+ return self.runtime.transcript_tts(+ app=self.app, account_id=_ACCOUNT_ID, text=text, voice=voice, message_id=message_id+ )++ def asr(self) -> dict[str, str]:+ return self.runtime.transcript_asr(+ app=self.app, audio=AudioUpload(stream=io.BytesIO(b"uploaded"), mime_type="audio/mp3")+ )+++def harness(+ sqlite_engine: Engine, sqlite_session_factory: sessionmaker[Session], monkeypatch: pytest.MonkeyPatch+) -> _Harness:+ with sqlite_session_factory.begin() as session:+ app = App(tenant_id=str(uuid4()), name="Audio app", mode=AppMode.CHAT, enable_site=True, enable_api=False)+ session.add(app)+ session.flush()+ config = AppModelConfig(+ app_id=app.id,+ speech_to_text='{"enabled":true}',+ text_to_speech='{"enabled":true,"voice":"configured"}',+ )+ session.add(config)+ session.flush()+ app.app_model_config_id = config.id+ message = Message(+ app_id=app.id,+ conversation_id=str(uuid4()),+ query="Question",+ answer=" Message answer ",+ inputs={},+ message={},+ message_unit_price=Decimal(0),+ answer_unit_price=Decimal(0),+ currency="USD",+ from_source=ConversationFromSource.CONSOLE,+ from_account_id=_ACCOUNT_ID,+ status=MessageStatus.NORMAL,+ )+ session.add(message)+ session.flush()++ closed_sessions: list[Session] = []++ class TrackedSession(Session):+ @override+ def commit(self) -> None:+ pytest.fail("Audio preparation must not commit")++ @override+ def close(self) -> None:+ super().close()+ closed_sessions.append(self)++ factory = sessionmaker(bind=sqlite_engine, class_=TrackedSession, expire_on_commit=True)+ state = _Harness(+ runtime=AppAudioRuntime(session_factory=cast(sessionmaker[Session], factory)),+ app=AudioAppRef(app_id=app.id, tenant_id=app.tenant_id, app_mode=app.mode),+ config_id=config.id,+ message_id=message.id,+ engine=sqlite_engine,+ closed_sessions=closed_sessions,+ )++ def transcribe(*, app: AudioAppRef, content: bytes, end_user: str | None) -> str:+ state.assert_database_closed()+ state.asr_calls.append((app, content, end_user))+ return " transcript "++ def synthesize(*, app: AudioAppRef, text: str, voice: str | None, end_user: str | None) -> AudioOutput:+ state.assert_database_closed()+ state.tts_calls.append((app, text, voice, end_user))+ return state.output++ monkeypatch.setattr(audio_provider_gateway, "speech_to_text", transcribe)+ monkeypatch.setattr(audio_provider_gateway, "text_to_speech", synthesize)+ return state+++def test_asr_reads_upload_and_invokes_provider_after_releasing_session(+ harness: _Harness, sqlite_session_factory: sessionmaker[Session]+) -> None:+ class CheckedUpload(io.BytesIO):+ @override+ def read(self, size: int | None = -1) -> bytes:+ harness.assert_database_closed()+ return super().read(size)++ assert harness.runtime.transcript_asr(+ app=harness.app,+ audio=AudioUpload(stream=CheckedUpload(b"uploaded"), mime_type="audio/x-m4a"),+ ) == {"text": " transcript "}+ assert harness.asr_calls == [+ (AudioAppRef(harness.app.app_id, harness.app.tenant_id, harness.app.app_mode), b"uploaded", None)+ ]+ assert len(harness.closed_sessions) == 1+ with sqlite_session_factory() as session:+ assert session.scalar(select(AccountTrialAppRecord)) is None++[email protected]("disabled", [False, True])+def test_asr_feature_error_precedes_missing_upload(+ harness: _Harness, sqlite_session_factory: sessionmaker[Session], disabled: bool+) -> None:+ with sqlite_session_factory.begin() as session:+ config = session.get(AppModelConfig, harness.config_id)+ assert config is not None+ config.speech_to_text = json.dumps({"enabled": not disabled})+ error = SpeechToTextDisabledServiceError if disabled else NoAudioUploadedServiceError+ with pytest.raises(error):+ harness.runtime.transcript_asr(app=harness.app, audio=None)+ harness.assert_database_closed()+ assert harness.asr_calls == []+++def test_tts_prioritizes_owned_message_and_preserves_provider_output(harness: _Harness) -> None:+ assert harness.tts(message_id=harness.message_id) is harness.output+ assert harness.tts_calls == [+ (+ AudioAppRef(harness.app.app_id, harness.app.tenant_id, harness.app.app_mode),+ "Message answer",+ "configured",+ None,+ )+ ]+ assert len(harness.closed_sessions) == 1++[email protected]("case", ["malformed", "missing", "app", "account", "empty-normal", "empty-paused"])+def test_tts_unavailable_message_does_not_fall_back_to_text_or_invoke_provider(+ harness: _Harness, sqlite_session_factory: sessionmaker[Session], case: str+) -> None:+ message_id = harness.message_id+ if case == "malformed":+ message_id = "invalid-uuid"+ elif case == "missing":+ message_id = str(uuid4())+ else:+ with sqlite_session_factory.begin() as session:+ message = session.get(Message, message_id)+ assert message is not None+ if case == "app":+ message.app_id = str(uuid4())+ elif case == "account":+ message.from_account_id = str(uuid4())+ else:+ message.answer = ""+ message.status = MessageStatus.NORMAL if case == "empty-normal" else MessageStatus.PAUSED+ assert harness.tts(message_id=message_id) is None+ assert harness.tts_calls == []+ harness.assert_database_closed()++[email protected]("case", ["id", "missing", "tenant", "mode"])+def test_both_methods_revalidate_app_owner_and_mode_before_audio_work(+ harness: _Harness, sqlite_session_factory: sessionmaker[Session], case: str+) -> None:+ with sqlite_session_factory.begin() as session:+ app = session.get(App, harness.app.app_id)+ assert app is not None+ if case == "id":+ # Leave a valid app in the database to lock down the app_id predicate.+ harness.app = replace(harness.app, app_id=str(uuid4()))+ elif case == "missing":+ session.delete(app)+ elif case == "tenant":+ app.tenant_id = str(uuid4())+ else:+ app.mode = AppMode.COMPLETION+ with pytest.raises(AppDefinitionUnavailableError):+ harness.asr()+ with pytest.raises(AppDefinitionUnavailableError):+ harness.tts(voice="explicit")+ assert harness.asr_calls == harness.tts_calls == []+ harness.assert_database_closed()++[email protected]("mode", [AppMode.CHAT, AppMode.COMPLETION])[email protected]("voice", [None, "explicit", ""])+def test_tts_disabled_config_rejects_implicit_voice_and_keeps_explicit_bypass(+ harness: _Harness, sqlite_session_factory: sessionmaker[Session], mode: AppMode, voice: str | None+) -> None:+ with sqlite_session_factory.begin() as session:+ app = session.get(App, harness.app.app_id)+ config = session.get(AppModelConfig, harness.config_id)+ assert app is not None+ assert config is not None+ app.mode = mode+ config.text_to_speech = '{"enabled":false,"voice":"disabled"}'+ harness.app = replace(harness.app, app_mode=mode)+ if voice is None:+ with pytest.raises(ValueError, match="^TTS is not enabled$"):+ harness.tts(voice=voice)+ assert harness.tts_calls == []+ else:+ assert harness.tts(voice=voice) is harness.output+ assert harness.tts_calls[0][1:] == ("Text input", voice, None)+ harness.assert_database_closed()++[email protected]("published", [False, True])+def test_both_methods_use_published_workflow_not_draft(+ harness: _Harness, sqlite_session_factory: sessionmaker[Session], published: bool+) -> None:+ with sqlite_session_factory.begin() as session:+ app = session.get(App, harness.app.app_id)+ assert app is not None+ app.mode = AppMode.ADVANCED_CHAT+ workflow = Workflow(+ tenant_id=app.tenant_id,+ app_id=app.id,+ type=WorkflowType.CHAT,+ version="published" if published else Workflow.VERSION_DRAFT,+ graph='{"nodes":[],"edges":[]}',+ _features='{"speech_to_text":{"enabled":true},"text_to_speech":{"enabled":true,"voice":"workflow"}}',+ created_by=_ACCOUNT_ID,+ )+ session.add(workflow)+ session.flush()+ if published:+ app.workflow_id = workflow.id+ harness.app = replace(harness.app, app_mode=AppMode.ADVANCED_CHAT)+ if published:+ assert harness.tts() is harness.output+ assert harness.tts_calls[0][1:] == ("Text input", "workflow", None)+ assert harness.asr() == {"text": " transcript "}+ else:+ with pytest.raises(ValueError, match="^TTS is not enabled$"):+ harness.tts()+ with pytest.raises(SpeechToTextDisabledServiceError):+ harness.asr()+ assert harness.asr_calls == harness.tts_calls == []+ harness.assert_database_closed()++[email protected]("enabled", [False, True])+def test_asr_uses_published_agent_soul_over_model_config(+ harness: _Harness, sqlite_session_factory: sessionmaker[Session], enabled: bool+) -> None:+ with sqlite_session_factory.begin() as session:+ app = session.get(App, harness.app.app_id)+ config = session.get(AppModelConfig, harness.config_id)+ assert app is not None+ assert config is not None+ app.mode = AppMode.AGENT+ config.speech_to_text = json.dumps({"enabled": not enabled})+ agent = Agent(+ tenant_id=app.tenant_id,+ name="Audio Agent",+ app_id=app.id,+ scope=AgentScope.ROSTER,+ source=AgentSource.AGENT_APP,+ )+ session.add(agent)+ session.flush()+ snapshot = AgentConfigSnapshot(+ tenant_id=app.tenant_id,+ agent_id=agent.id,+ version=1,+ config_snapshot=AgentSoulConfig.model_validate({"app_features": {"speech_to_text": {"enabled": enabled}}}),+ )+ session.add(snapshot)+ session.flush()+ agent.active_config_snapshot_id = snapshot.id+ harness.app = replace(harness.app, app_mode=AppMode.AGENT)+ if enabled:+ assert harness.asr() == {"text": " transcript "}+ assert harness.asr_calls[0][0].app_mode == AppMode.AGENT+ else:+ with pytest.raises(SpeechToTextDisabledServiceError):+ harness.asr()+ assert harness.asr_calls == []+ harness.assert_database_closed()+++def test_tts_stream_is_unconsumed_and_sessions_stay_closed_during_iteration(harness: _Harness) -> None:+ chunks_read: list[bytes] = []++ def stream() -> Iterator[bytes]:+ harness.assert_database_closed()+ chunks_read.append(b"first")+ yield b"first"+ yield b"second"++ chunks = stream()+ harness.output = AudioOutput(data=chunks, mime_type="audio/wav")+ assert harness.tts() is harness.output+ assert chunks_read == []+ assert list(chunks) == [b"first", b"second"]+ assert chunks_read == [b"first"]diff --git a/api/tests/unit_tests/services/test_audio_inputs.py b/api/tests/unit_tests/services/test_audio_inputs.pynew file mode 100644index 00000000000..23280c34bac--- /dev/null+++ b/api/tests/unit_tests/services/test_audio_inputs.py@@ -0,0 +1,71 @@+"""Validate plain audio uploads before calling the shared provider boundary."""++from io import BytesIO++import pytest++from models import App, AppMode+from services import audio_provider_gateway+from services.audio_service import FILE_SIZE_LIMIT, AudioService+from services.audio_types import AudioAppRef, AudioUpload+from services.errors.audio import (+ AudioTooLargeServiceError,+ NoAudioUploadedServiceError,+ UnsupportedAudioTypeServiceError,+)++[email protected]("size", [0, FILE_SIZE_LIMIT, FILE_SIZE_LIMIT + 1])+def test_asr_accepts_empty_and_exact_limit_but_rejects_oversized_upload(+ monkeypatch: pytest.MonkeyPatch, size: int+) -> None:+ app = App(id="app", tenant_id="owner", mode=AppMode.CHAT)+ contents: list[bytes] = []++ def transcribe(*, app: AudioAppRef, content: bytes, end_user: str | None) -> str:+ assert app == AudioAppRef(app_id="app", tenant_id="owner", app_mode="chat")+ assert end_user == "provider-user"+ contents.append(content)+ return " transcript "++ monkeypatch.setattr(audio_provider_gateway, "speech_to_text", transcribe)+ content = b"a" * size+ upload = AudioUpload(stream=BytesIO(content), mime_type="audio/x-m4a")+ if size > FILE_SIZE_LIMIT:+ with pytest.raises(AudioTooLargeServiceError, match="Audio size larger than 30 mb"):+ AudioService.invoke_speech_to_text(app, upload, end_user="provider-user")+ assert contents == []+ else:+ assert AudioService.invoke_speech_to_text(app, upload, end_user="provider-user") == {"text": " transcript "}+ assert contents == [content]++[email protected]("missing", [False, True])+def test_invalid_upload_does_not_read_stream_or_invoke_provider(monkeypatch: pytest.MonkeyPatch, missing: bool) -> None:+ def unexpected_provider(*, app: AudioAppRef, content: bytes, end_user: str | None) -> str:+ del app, content, end_user+ pytest.fail("Invalid audio reached the provider")++ monkeypatch.setattr(audio_provider_gateway, "speech_to_text", unexpected_provider)+ app = App(id="app", tenant_id="owner", mode=AppMode.CHAT)+ stream = BytesIO(b"unread")+ audio = None if missing else AudioUpload(stream=stream, mime_type="application/octet-stream")+ error = NoAudioUploadedServiceError if missing else UnsupportedAudioTypeServiceError+ with pytest.raises(error):+ AudioService.invoke_speech_to_text(app, audio)+ assert stream.tell() == 0+++def test_provider_exception_propagates_without_losing_details(monkeypatch: pytest.MonkeyPatch) -> None:+ error = RuntimeError("provider transcription failed: timeout")++ def transcribe(*, app: AudioAppRef, content: bytes, end_user: str | None) -> str:+ del app, content, end_user+ raise error++ monkeypatch.setattr(audio_provider_gateway, "speech_to_text", transcribe)+ app = App(id="app", tenant_id="owner", mode=AppMode.CHAT)+ audio = AudioUpload(stream=BytesIO(b"input"), mime_type="audio/mp3")+ with pytest.raises(RuntimeError) as caught:+ AudioService.invoke_speech_to_text(app, audio)+ assert caught.value is errordiff --git a/api/tests/unit_tests/services/test_audio_service.py b/api/tests/unit_tests/services/test_audio_service.pyindex 88c4fe20964..d545709d7ac 100644--- a/api/tests/unit_tests/services/test_audio_service.py+++ b/api/tests/unit_tests/services/test_audio_service.py@@ -54,25 +54,27 @@ Tests available voice retrieval:"""import json+from collections.abc import Generatorfrom decimal import Decimal-from typing import Anyfrom unittest.mock import MagicMock, Mock, patchfrom uuid import uuid4import pytest-from flask import Flask+from flask import Flask, has_request_context, requestfrom sqlalchemy.orm import Sessionfrom werkzeug.datastructures import FileStoragefrom core.credit_usage import CreditUsageAppType, CreditUsageCreatedByfrom core.plugin.entities.plugin_daemon import TTSAudioChunk+from extensions.ext_database import dbfrom graphon.model_runtime.errors.invoke import InvokeBadRequestError+from models.agent import Agent, AgentConfigSnapshot, AgentScope, AgentSourcefrom models.agent_config_entities import AgentSoulConfigfrom models.enums import ConversationFromSource, MessageStatusfrom models.model import App, AppMode, AppModelConfig, Messagefrom models.workflow import Workflow, WorkflowTypefrom services.app_ref_service import AppRef, MessageRef-from services.audio_service import AudioService+from services.audio_service import AudioService, _create_tts_responsefrom services.errors.audio import (AudioTooLargeServiceError,NoAudioUploadedServiceError,@@ -127,7 +129,10 @@ class AudioServiceTestDataFactory:app_id: str = APP_ID,mode: AppMode = AppMode.CHAT,tenant_id: str = TENANT_ID,- **kwargs,+ *,+ workflow: Workflow | None = None,+ app_model_config: AppModelConfig | None = None,+ **kwargs: object,) -> App:"""Create and persist an App model.@@ -141,8 +146,6 @@ class AudioServiceTestDataFactory:Returns:Persisted App model with specified attributes"""- workflow = kwargs.pop("workflow", None)- app_model_config = kwargs.pop("app_model_config", None)app = App(id=app_id,tenant_id=tenant_id,@@ -163,7 +166,7 @@ class AudioServiceTestDataFactory:self.session.commit()return app- def create_workflow_mock(self, features_dict: dict[str, Any] | None = None, **kwargs) -> Workflow:+ def create_workflow_mock(self, features_dict: dict[str, object] | None = None, **kwargs: object) -> Workflow:"""Create and persist a Workflow model.@@ -192,9 +195,11 @@ class AudioServiceTestDataFactory:def create_app_model_config_mock(self,- speech_to_text_dict: dict[str, Any] | None = None,- text_to_speech_dict: dict[str, Any] | None = None,- **kwargs,+ speech_to_text_dict: dict[str, object] | None = None,+ text_to_speech_dict: dict[str, object] | None = None,+ *,+ app_id: str = APP_ID,+ **kwargs: object,) -> AppModelConfig:"""Create and persist an AppModelConfig model.@@ -208,7 +213,7 @@ class AudioServiceTestDataFactory:Persisted AppModelConfig model with specified attributes"""config = AppModelConfig(- app_id=kwargs.pop("app_id", APP_ID),+ app_id=app_id,speech_to_text=json.dumps(speech_to_text_dict or {"enabled": False}),text_to_speech=json.dumps(text_to_speech_dict or {"enabled": False}),)@@ -223,7 +228,7 @@ class AudioServiceTestDataFactory:filename: str = "test.mp3",mimetype: str = "audio/mp3",content: bytes = b"fake audio content",- **kwargs,+ **kwargs: object,) -> Mock:"""Create a mock FileStorage object.@@ -253,6 +258,46 @@ def factory(sqlite_session: Session) -> AudioServiceTestDataFactory:return AudioServiceTestDataFactory(sqlite_session)[email protected]("consumed_chunks", [0, 1, 2, None])+def test_tts_response_closes_retained_provider_stream(consumed_chunks: int | None) -> None:+ closed: list[bool] = []+ audio_chunks = [b"RIFF\x24\x00\x00\x00WAVE" + b"\x00" * 32, b"first-tail", b"second-tail"]++ def chunks() -> Generator[bytes]:+ try:+ for chunk in audio_chunks:+ assert has_request_context()+ assert request.path == "/text-to-audio"+ yield chunk+ finally:+ closed.append(True)++ source = chunks()+ app = Flask(__name__)+ with app.test_request_context("/text-to-audio", method="POST"):+ response = _create_tts_response(source, "audio/x-wav")++ assert response.status_code == 200+ assert dict(response.headers) == {"Content-Type": "audio/wav"}+ assert response.is_streamed+ assert closed == []+ try:+ iterator = iter(response.response)+ if consumed_chunks is None:+ assert list(iterator) == audio_chunks+ assert closed == [True]+ else:+ assert [next(iterator) for _ in range(consumed_chunks)] == audio_chunks[:consumed_chunks]+ assert closed == []+ finally:+ response.close()+ response.close()++ assert closed == [True]+ with pytest.raises(StopIteration):+ next(source)++class TestAudioServiceASR:"""Test speech-to-text (ASR) operations."""@@ -260,8 +305,10 @@ class TestAudioServiceASR:def _bind_sqlite_session(self, sqlite_session: Session) -> None:self.session = sqlite_session- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)- def test_transcript_asr_success_chat_mode(self, mock_model_manager_class, factory: AudioServiceTestDataFactory):+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)+ def test_transcript_asr_success_chat_mode(+ self, mock_model_manager_class: MagicMock, factory: AudioServiceTestDataFactory+ ) -> None:"""Test successful ASR transcription in CHAT mode."""# Arrangeapp_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})@@ -292,10 +339,10 @@ class TestAudioServiceASR:},)- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_asr_accepts_x_m4a_mimetype(- self, mock_model_manager_class, factory: AudioServiceTestDataFactory- ):+ self, mock_model_manager_class: MagicMock, factory: AudioServiceTestDataFactory+ ) -> None:"""Test that the x-m4a MIME alias follows the normal m4a transcription flow."""# Arrangeapp_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})@@ -312,10 +359,10 @@ class TestAudioServiceASR:assert result == {"text": "M4A transcript"}mock_model_instance.invoke_speech2text.assert_called_once()- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_asr_success_advanced_chat_mode(- self, mock_model_manager_class, factory: AudioServiceTestDataFactory- ):+ self, mock_model_manager_class: MagicMock, factory: AudioServiceTestDataFactory+ ) -> None:"""Test successful ASR transcription in ADVANCED_CHAT mode."""# Arrangeworkflow = factory.create_workflow_mock(features_dict={"speech_to_text": {"enabled": True}})@@ -337,18 +384,31 @@ class TestAudioServiceASR:# Assertassert result == {"text": "Workflow transcribed text"}- @patch("services.audio_service.AgentRosterService", autospec=True)- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_asr_success_published_agent_mode(self,- mock_model_manager_class,- mock_roster_service_class,+ mock_model_manager_class: MagicMock,factory: AudioServiceTestDataFactory,- ):+ ) -> None:app = factory.create_app_mock(mode=AppMode.AGENT)file = factory.create_file_storage_mock()agent_soul = AgentSoulConfig.model_validate({"app_features": {"speech_to_text": {"enabled": True}}})- mock_roster_service_class.return_value.get_published_agent_soul_for_app.return_value = agent_soul+ agent = Agent(+ tenant_id=app.tenant_id,+ app_id=app.id,+ name="Audio agent",+ scope=AgentScope.ROSTER,+ source=AgentSource.AGENT_APP,+ )+ self.session.add(agent)+ self.session.flush()+ snapshot = AgentConfigSnapshot(+ tenant_id=app.tenant_id, agent_id=agent.id, version=1, config_snapshot=agent_soul+ )+ self.session.add(snapshot)+ self.session.flush()+ agent.active_config_snapshot_id = snapshot.id+ self.session.commit()mock_model_instance = MagicMock()mock_model_instance.invoke_speech2text.return_value = "Published Agent transcript"mock_model_manager_class.return_value.get_default_model_instance.return_value = mock_model_instance@@ -356,23 +416,24 @@ class TestAudioServiceASR:result = AudioService.transcript_asr(app_model=app, file=file, session=self.session, end_user="end-user-1")assert result == {"text": "Published Agent transcript"}- mock_roster_service_class.return_value.get_published_agent_soul_for_app.assert_called_once_with(+ mock_model_manager_class.assert_called_once_with(tenant_id=app.tenant_id,- app_id=app.id,+ user_id="end-user-1",+ request_metadata={+ "app_type": CreditUsageAppType.AGENT_V2,+ "created_by": CreditUsageCreatedBy.AUDIO,+ },)- @patch("services.audio_service.AgentRosterService", autospec=True)- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_asr_legacy_agent_falls_back_to_app_model_config(self,- mock_model_manager_class,- mock_roster_service_class,+ mock_model_manager_class: MagicMock,factory: AudioServiceTestDataFactory,- ):+ ) -> None:app_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})app = factory.create_app_mock(mode=AppMode.AGENT, app_model_config=app_model_config)file = factory.create_file_storage_mock()- mock_roster_service_class.return_value.get_published_agent_soul_for_app.return_value = Nonemock_model_instance = MagicMock()mock_model_instance.invoke_speech2text.return_value = "Legacy Agent transcript"mock_model_manager_class.return_value.get_default_model_instance.return_value = mock_model_instance@@ -381,10 +442,10 @@ class TestAudioServiceASR:assert result == {"text": "Legacy Agent transcript"}- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_agent_asr_uses_agent_soul_feature(- self, mock_model_manager_class, factory: AudioServiceTestDataFactory- ):+ self, mock_model_manager_class: MagicMock, factory: AudioServiceTestDataFactory+ ) -> None:app = factory.create_app_mock(mode=AppMode.AGENT)file = factory.create_file_storage_mock()agent_soul = AgentSoulConfig.model_validate({"app_features": {"speech_to_text": {"enabled": True}}})@@ -419,17 +480,17 @@ class TestAudioServiceASR:)def test_transcript_agent_asr_rejects_disabled_feature(self, factory: AudioServiceTestDataFactory, agent_soul: AgentSoulConfig- ):+ ) -> None:app = factory.create_app_mock(mode=AppMode.AGENT)file = factory.create_file_storage_mock()with pytest.raises(SpeechToTextDisabledServiceError):AudioService.transcript_agent_asr(app_model=app, agent_soul=agent_soul, file=file, session=self.session)- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_agent_asr_preserves_legacy_feature_fallback(- self, mock_model_manager_class, factory: AudioServiceTestDataFactory- ):+ self, mock_model_manager_class: MagicMock, factory: AudioServiceTestDataFactory+ ) -> None:app_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})app = factory.create_app_mock(mode=AppMode.AGENT, app_model_config=app_model_config)file = factory.create_file_storage_mock()@@ -446,7 +507,9 @@ class TestAudioServiceASR:assert result == {"text": "Legacy feature transcript"}- def test_transcript_agent_asr_soul_disabled_overrides_legacy_feature(self, factory: AudioServiceTestDataFactory):+ def test_transcript_agent_asr_soul_disabled_overrides_legacy_feature(+ self, factory: AudioServiceTestDataFactory+ ) -> None:app_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})app = factory.create_app_mock(mode=AppMode.AGENT, app_model_config=app_model_config)file = factory.create_file_storage_mock()@@ -455,7 +518,9 @@ class TestAudioServiceASR:with pytest.raises(SpeechToTextDisabledServiceError):AudioService.transcript_agent_asr(app_model=app, agent_soul=agent_soul, file=file, session=self.session)- def test_transcript_asr_raises_error_when_feature_disabled_chat_mode(self, factory: AudioServiceTestDataFactory):+ def test_transcript_asr_raises_error_when_feature_disabled_chat_mode(+ self, factory: AudioServiceTestDataFactory+ ) -> None:"""Test that ASR raises error when speech-to-text is disabled in CHAT mode."""# Arrangeapp_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": False})@@ -471,7 +536,7 @@ class TestAudioServiceASR:def test_transcript_asr_raises_error_when_feature_disabled_workflow_mode(self, factory: AudioServiceTestDataFactory- ):+ ) -> None:"""Test that ASR raises error when speech-to-text is disabled in WORKFLOW mode."""# Arrangeworkflow = factory.create_workflow_mock(features_dict={"speech_to_text": {"enabled": False}})@@ -485,7 +550,7 @@ class TestAudioServiceASR:with pytest.raises(SpeechToTextDisabledServiceError):AudioService.transcript_asr(app_model=app, file=file, session=self.session)- def test_transcript_asr_raises_error_when_workflow_missing(self, factory: AudioServiceTestDataFactory):+ def test_transcript_asr_raises_error_when_workflow_missing(self, factory: AudioServiceTestDataFactory) -> None:"""Test that ASR raises error when workflow is missing in WORKFLOW mode."""# Arrangeapp = factory.create_app_mock(@@ -498,7 +563,7 @@ class TestAudioServiceASR:with pytest.raises(SpeechToTextDisabledServiceError):AudioService.transcript_asr(app_model=app, file=file, session=self.session)- def test_transcript_asr_raises_error_when_no_file_uploaded(self, factory: AudioServiceTestDataFactory):+ def test_transcript_asr_raises_error_when_no_file_uploaded(self, factory: AudioServiceTestDataFactory) -> None:"""Test that ASR raises error when no file is uploaded."""# Arrangeapp_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})@@ -511,7 +576,7 @@ class TestAudioServiceASR:with pytest.raises(NoAudioUploadedServiceError):AudioService.transcript_asr(app_model=app, file=None, session=self.session)- def test_transcript_asr_raises_error_for_unsupported_audio_type(self, factory: AudioServiceTestDataFactory):+ def test_transcript_asr_raises_error_for_unsupported_audio_type(self, factory: AudioServiceTestDataFactory) -> None:"""Test that ASR raises error for unsupported audio file types."""# Arrangeapp_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})@@ -525,7 +590,7 @@ class TestAudioServiceASR:with pytest.raises(UnsupportedAudioTypeServiceError):AudioService.transcript_asr(app_model=app, file=file, session=self.session)- def test_transcript_asr_raises_error_for_large_file(self, factory: AudioServiceTestDataFactory):+ def test_transcript_asr_raises_error_for_large_file(self, factory: AudioServiceTestDataFactory) -> None:"""Test that ASR raises error when file exceeds size limit (30MB)."""# Arrangeapp_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})@@ -541,10 +606,10 @@ class TestAudioServiceASR:with pytest.raises(AudioTooLargeServiceError, match="Audio size larger than 30 mb"):AudioService.transcript_asr(app_model=app, file=file, session=self.session)- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_asr_raises_error_when_no_model_instance(- self, mock_model_manager_class, factory: AudioServiceTestDataFactory- ):+ self, mock_model_manager_class: MagicMock, factory: AudioServiceTestDataFactory+ ) -> None:"""Test that ASR raises error when no model instance is available."""# Arrangeapp_model_config = factory.create_app_model_config_mock(speech_to_text_dict={"enabled": True})@@ -566,13 +631,42 @@ class TestAudioServiceASR:class TestAudioServiceTTS:"""Test text-to-speech (TTS) operations."""- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)+ def test_legacy_tts_preserves_the_callers_uncommitted_transaction(+ self,+ mock_model_manager_class: MagicMock,+ factory: AudioServiceTestDataFactory,+ sqlite_session: Session,+ ) -> None:+ app = factory.create_app_mock()+ sqlite_session.add(_message())+ sqlite_session.flush()+ mock_model_instance = mock_model_manager_class.return_value.get_default_model_instance.return_value+ mock_model_instance.invoke_tts.return_value = b"pending message audio"++ result = AudioService.transcript_tts(+ app_model=app,+ session=sqlite_session,+ message_ref=MessageRef(+ app=AppRef(tenant_id=TENANT_ID, app_id=APP_ID), message_id=MESSAGE_ID, account_id=ACCOUNT_ID+ ),+ voice="pending-voice",+ )++ assert result is not None+ assert result.get_data() == b"pending message audio"+ assert sqlite_session.in_transaction()+ mock_model_instance.invoke_tts.assert_called_once_with(content_text="Message answer", voice="pending-voice")+ sqlite_session.rollback()+ assert sqlite_session.get(Message, MESSAGE_ID) is None++ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_tts_with_text_success(self,- mock_model_manager_class,+ mock_model_manager_class: MagicMock,factory: AudioServiceTestDataFactory,sqlite_session: Session,- ):+ ) -> None:"""Test successful TTS with text input."""# Arrangeapp_model_config = factory.create_app_model_config_mock(@@ -599,6 +693,7 @@ class TestAudioServiceTTS:)# Assert+ assert result is not Noneassert result.content_type == "audio/mpeg"assert result.get_data() == b"audio data"mock_model_manager_class.assert_called_once_with(@@ -614,13 +709,13 @@ class TestAudioServiceTTS:voice="en-US-Neural",)- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_tts_with_default_voice(self,- mock_model_manager_class,+ mock_model_manager_class: MagicMock,factory: AudioServiceTestDataFactory,sqlite_session: Session,- ):+ ) -> None:"""Test TTS uses default voice when none specified."""# Arrangeapp_model_config = factory.create_app_model_config_mock(@@ -645,19 +740,20 @@ class TestAudioServiceTTS:)# Assert+ assert result is not Noneassert result.content_type == "audio/mpeg"assert result.get_data() == b"audio data"# Verify default voice was usedcall_args = mock_model_instance.invoke_tts.call_argsassert call_args.kwargs["voice"] == "default-voice"- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_tts_gets_first_available_voice_when_none_configured(self,- mock_model_manager_class,+ mock_model_manager_class: MagicMock,factory: AudioServiceTestDataFactory,sqlite_session: Session,- ):+ ) -> None:"""Test TTS gets first available voice when none is configured."""# Arrangeapp_model_config = factory.create_app_model_config_mock(@@ -683,58 +779,61 @@ class TestAudioServiceTTS:)# Assert+ assert result is not Noneassert result.content_type == "audio/mpeg"assert result.get_data() == b"audio data"call_args = mock_model_instance.invoke_tts.call_argsassert call_args.kwargs["voice"] == "auto-voice"- @patch("services.audio_service.WorkflowService", autospec=True)- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_tts_workflow_mode_with_draft(self,- mock_model_manager_class,- mock_workflow_service_class,+ mock_model_manager_class: MagicMock,factory: AudioServiceTestDataFactory,sqlite_session: Session,- ):+ ) -> None:"""Test TTS in WORKFLOW mode with draft workflow."""# Arrange- draft_workflow = factory.create_workflow_mock(- features_dict={"text_to_speech": {"enabled": True, "voice": "draft-voice"}}- )+ factory.create_workflow_mock(features_dict={"text_to_speech": {"enabled": True, "voice": "draft-voice"}})app = factory.create_app_mock(mode=AppMode.WORKFLOW,)- # Mock WorkflowService- mock_workflow_service = mock_workflow_service_class.return_value- mock_workflow_service.get_draft_workflow.return_value = draft_workflow-# Mock ModelManagermock_model_manager = mock_model_manager_class.return_valuemock_model_instance = MagicMock()mock_model_instance.invoke_tts.return_value = b"draft audio"mock_model_manager.get_default_model_instance.return_value = mock_model_instance- # Act- result = AudioService.transcript_tts(- app_model=app,- session=sqlite_session,- text="Draft test",- is_draft=True,- )+ # WorkflowService constructs its default repository from db.engine.+ # Leave that database empty so the draft must come from sqlite_session.+ flask_app = Flask(__name__)+ flask_app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"+ db.init_app(flask_app)+ with flask_app.app_context():+ try:+ result = AudioService.transcript_tts(+ app_model=app,+ session=sqlite_session,+ text="Draft test",+ is_draft=True,+ )+ finally:+ db.session.remove()+ db.engine.dispose()# Assert+ assert result is not Noneassert result.content_type == "audio/mpeg"assert result.get_data() == b"draft audio"- mock_workflow_service.get_draft_workflow.assert_called_once_with(app_model=app, session=sqlite_session)+ mock_model_instance.invoke_tts.assert_called_once_with(content_text="Draft test", voice="draft-voice")- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_tts_message_id_uses_provided_session(self,- mock_model_manager_class,+ mock_model_manager_class: MagicMock,factory: AudioServiceTestDataFactory,sqlite_session: Session,- ):+ ) -> None:"""Test TTS message lookup uses the injected session."""# Arrangeapp = factory.create_app_mock(app_id=APP_ID, tenant_id=TENANT_ID, mode=AppMode.CHAT)@@ -791,6 +890,7 @@ class TestAudioServiceTTS:)# Assert+ assert result is not Noneassert result.content_type == "audio/mpeg"assert result.get_data() == b"message audio"mock_model_instance.invoke_tts.assert_called_once_with(@@ -798,14 +898,14 @@ class TestAudioServiceTTS:voice="message-voice",)- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_tts_uses_detected_wav_mime_type_for_streams(self,- mock_model_manager_class,+ mock_model_manager_class: MagicMock,factory: AudioServiceTestDataFactory,sqlite_session: Session,app: Flask,- ):+ ) -> None:app_model_config = factory.create_app_model_config_mock(text_to_speech_dict={"enabled": True, "voice": "en-US-Neural"})@@ -824,17 +924,18 @@ class TestAudioServiceTTS:voice="en-US-Neural",)+ assert result is not Noneassert result.content_type == "audio/wav"assert result.get_data() == b"RIFF\x24\x00\x00\x00WAVEfmt \x10\x00\x00\x00audio-data"- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_tts_returns_provider_output_error_for_mime_magic_mismatch(self,- mock_model_manager_class,+ mock_model_manager_class: MagicMock,factory: AudioServiceTestDataFactory,sqlite_session: Session,app: Flask,- ):+ ) -> None:app_model_config = factory.create_app_model_config_mock(text_to_speech_dict={"enabled": True, "voice": "en-US-Neural"})@@ -858,7 +959,7 @@ class TestAudioServiceTTS:self,factory: AudioServiceTestDataFactory,sqlite_session: Session,- ):+ ) -> None:"""Test that TTS raises error when text is missing."""# Arrangeapp = factory.create_app_mock()@@ -867,13 +968,13 @@ class TestAudioServiceTTS:with pytest.raises(ValueError, match="Text is required"):AudioService.transcript_tts(app_model=app, session=sqlite_session, text=None)- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_tts_raises_error_when_no_voices_available(self,- mock_model_manager_class,+ mock_model_manager_class: MagicMock,factory: AudioServiceTestDataFactory,sqlite_session: Session,- ):+ ) -> None:"""Test that TTS raises error when no voices are available."""# Arrangeapp_model_config = factory.create_app_model_config_mock(@@ -887,7 +988,7 @@ class TestAudioServiceTTS:# Mock ModelManagermock_model_manager = mock_model_manager_class.return_valuemock_model_instance = MagicMock()- mock_model_instance.get_tts_voices.return_value = [] # No voices available+ mock_model_instance.get_tts_voices.return_value = list[dict[str, str]]() # No voices availablemock_model_manager.get_default_model_instance.return_value = mock_model_instance# Act & Assert@@ -898,8 +999,8 @@ class TestAudioServiceTTS:class TestAudioServiceTTSVoices:"""Test TTS voice listing operations."""- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)- def test_transcript_tts_voices_success(self, mock_model_manager_class, factory: AudioServiceTestDataFactory):+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)+ def test_transcript_tts_voices_success(self, mock_model_manager_class: MagicMock) -> None:"""Test successful retrieval of TTS voices."""# Arrangetenant_id = "tenant-123"@@ -923,10 +1024,10 @@ class TestAudioServiceTTSVoices:assert result == expected_voicesmock_model_instance.get_tts_voices.assert_called_once_with(language)- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)def test_transcript_tts_voices_raises_error_when_no_model_instance(- self, mock_model_manager_class, factory: AudioServiceTestDataFactory- ):+ self, mock_model_manager_class: MagicMock+ ) -> None:"""Test that TTS voices raises error when no model instance is available."""# Arrangetenant_id = "tenant-123"@@ -940,10 +1041,8 @@ class TestAudioServiceTTSVoices:with pytest.raises(ProviderNotSupportTextToSpeechServiceError):AudioService.transcript_tts_voices(tenant_id=tenant_id, language=language)- @patch("services.audio_service.ModelManager.for_tenant", autospec=True)- def test_transcript_tts_voices_propagates_exceptions(- self, mock_model_manager_class, factory: AudioServiceTestDataFactory- ):+ @patch("services.audio_provider_gateway.ModelManager.for_tenant", autospec=True)+ def test_transcript_tts_voices_propagates_exceptions(self, mock_model_manager_class: MagicMock) -> None:"""Test that TTS voices propagates exceptions from model instance."""# Arrangetenant_id = "tenant-123"